paul@258 | 1 | /* |
paul@258 | 2 | * MSC (MMC/SD controller) peripheral support. |
paul@258 | 3 | * |
paul@258 | 4 | * Copyright (C) 2024 Paul Boddie <paul@boddie.org.uk> |
paul@258 | 5 | * |
paul@258 | 6 | * This program is free software; you can redistribute it and/or |
paul@258 | 7 | * modify it under the terms of the GNU General Public License as |
paul@258 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@258 | 9 | * the License, or (at your option) any later version. |
paul@258 | 10 | * |
paul@258 | 11 | * This program is distributed in the hope that it will be useful, |
paul@258 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@258 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@258 | 14 | * GNU General Public License for more details. |
paul@258 | 15 | * |
paul@258 | 16 | * You should have received a copy of the GNU General Public License |
paul@258 | 17 | * along with this program; if not, write to the Free Software |
paul@258 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@258 | 19 | * Boston, MA 02110-1301, USA |
paul@258 | 20 | */ |
paul@258 | 21 | |
paul@258 | 22 | #include <l4/sys/err.h> |
paul@258 | 23 | |
paul@264 | 24 | #include "msc-defs.h" |
paul@258 | 25 | #include "msc-jz4780.h" |
paul@258 | 26 | |
paul@258 | 27 | |
paul@258 | 28 | |
paul@258 | 29 | // Channel abstraction. |
paul@258 | 30 | |
paul@258 | 31 | Msc_jz4780_channel::Msc_jz4780_channel(l4_addr_t msc_start, l4_addr_t addr, |
paul@264 | 32 | l4_cap_idx_t irq, |
paul@264 | 33 | Cpm_chip *cpm, enum Clock_identifiers clock, |
paul@264 | 34 | Dma_jz4780_channel *dma, |
paul@258 | 35 | enum Dma_jz4780_request_type request_type_in, |
paul@258 | 36 | enum Dma_jz4780_request_type request_type_out) |
paul@264 | 37 | : Msc_channel(msc_start, addr, irq, cpm, clock), |
paul@258 | 38 | _dma(dma), |
paul@258 | 39 | _request_type_in(request_type_in), |
paul@258 | 40 | _request_type_out(request_type_out) |
paul@258 | 41 | { |
paul@258 | 42 | } |
paul@258 | 43 | |
paul@258 | 44 | // Request the transfer of the indicated number of bytes between two physical |
paul@261 | 45 | // addresses in the indicated direction, returning the number of bytes to be |
paul@258 | 46 | // transferred. |
paul@258 | 47 | // NOTE: To be consolidated into a generic method that uses generic request types. |
paul@258 | 48 | |
paul@258 | 49 | uint32_t |
paul@258 | 50 | Msc_jz4780_channel::transfer(l4re_dma_space_dma_addr_t from_paddr, |
paul@258 | 51 | l4re_dma_space_dma_addr_t to_paddr, |
paul@258 | 52 | bool recv, uint32_t count) |
paul@258 | 53 | { |
paul@258 | 54 | uint32_t unit_size = 32; |
paul@258 | 55 | uint32_t unit_count = count / unit_size; |
paul@258 | 56 | uint32_t to_transfer; |
paul@258 | 57 | |
paul@258 | 58 | to_transfer = _dma->transfer(from_paddr, to_paddr, |
paul@258 | 59 | unit_count, |
paul@258 | 60 | recv ? false : true, // increment source if sending |
paul@258 | 61 | recv ? true : false, // increment destination if receiving |
paul@258 | 62 | 4, 4, unit_size, |
paul@258 | 63 | recv ? _request_type_in : _request_type_out); |
paul@258 | 64 | |
paul@261 | 65 | return to_transfer * unit_size; |
paul@258 | 66 | } |
paul@258 | 67 | |
paul@258 | 68 | |
paul@258 | 69 | |
paul@258 | 70 | // Peripheral abstraction. |
paul@258 | 71 | |
paul@264 | 72 | Msc_jz4780_chip::Msc_jz4780_chip(l4_addr_t msc_start, l4_addr_t start, |
paul@264 | 73 | l4_addr_t end, Cpm_chip *cpm) |
paul@264 | 74 | : Msc_chip(msc_start, start, end, cpm) |
paul@258 | 75 | { |
paul@258 | 76 | } |
paul@258 | 77 | |
paul@258 | 78 | Msc_channel *Msc_jz4780_chip::get_channel(uint8_t channel, l4_cap_idx_t irq, |
paul@258 | 79 | Dma_jz4780_channel *dma) |
paul@258 | 80 | { |
paul@258 | 81 | if (channel < num_channels()) |
paul@258 | 82 | return new Msc_jz4780_channel(_msc_start + channel * Msc_channel_offset, |
paul@258 | 83 | _start + channel * Msc_channel_offset, |
paul@264 | 84 | irq, _cpm, _clocks[channel], dma, |
paul@264 | 85 | _in_types[channel], _out_types[channel]); |
paul@258 | 86 | else |
paul@258 | 87 | throw -L4_EINVAL; |
paul@258 | 88 | } |
paul@258 | 89 | |
paul@258 | 90 | |
paul@258 | 91 | |
paul@258 | 92 | // C language interface functions. |
paul@258 | 93 | |
paul@264 | 94 | void *jz4780_msc_init(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end, |
paul@264 | 95 | void *cpm) |
paul@258 | 96 | { |
paul@264 | 97 | return (void *) new Msc_jz4780_chip(msc_start, start, end, |
paul@264 | 98 | static_cast<Cpm_jz4780_chip *>(cpm)); |
paul@258 | 99 | } |
paul@258 | 100 | |
paul@258 | 101 | void *jz4780_msc_get_channel(void *msc, uint8_t channel, l4_cap_idx_t irq, |
paul@258 | 102 | void *dma) |
paul@258 | 103 | { |
paul@258 | 104 | return static_cast<Msc_jz4780_chip *>(msc)->get_channel(channel, irq, |
paul@258 | 105 | static_cast<Dma_jz4780_channel *>(dma)); |
paul@258 | 106 | } |
paul@258 | 107 | |
paul@263 | 108 | struct msc_card *jz4780_msc_get_cards(void *msc_channel) |
paul@258 | 109 | { |
paul@263 | 110 | return static_cast<Msc_channel *>(msc_channel)->get_cards(); |
paul@263 | 111 | } |
paul@263 | 112 | |
paul@263 | 113 | uint8_t jz4780_msc_num_cards(void *msc_channel) |
paul@263 | 114 | { |
paul@263 | 115 | return static_cast<Msc_channel *>(msc_channel)->num_cards(); |
paul@258 | 116 | } |
paul@258 | 117 | |
paul@258 | 118 | void jz4780_msc_enable(void *msc_channel) |
paul@258 | 119 | { |
paul@258 | 120 | static_cast<Msc_channel *>(msc_channel)->enable(); |
paul@258 | 121 | } |
paul@258 | 122 | |
paul@268 | 123 | uint32_t jz4780_msc_read(void *msc_channel, uint8_t card, |
paul@268 | 124 | struct dma_region *region, |
paul@268 | 125 | uint32_t address, uint32_t count) |
paul@268 | 126 | { |
paul@268 | 127 | return static_cast<Msc_channel *>(msc_channel)->read(card, region, |
paul@268 | 128 | address, count); |
paul@268 | 129 | } |
paul@268 | 130 | |
paul@261 | 131 | uint32_t jz4780_msc_read_blocks(void *msc_channel, uint8_t card, |
paul@264 | 132 | struct dma_region *region, |
paul@261 | 133 | uint32_t block_address, uint32_t block_count) |
paul@258 | 134 | { |
paul@264 | 135 | return static_cast<Msc_channel *>(msc_channel)->read_blocks(card, region, |
paul@261 | 136 | block_address, block_count); |
paul@258 | 137 | } |