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