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_jz4780_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 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_jz4780_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 91 92 // C language interface functions. 93 94 void *jz4780_msc_init(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end, 95 void *cpm) 96 { 97 return (void *) new Msc_jz4780_chip(msc_start, start, end, 98 static_cast<Cpm_jz4780_chip *>(cpm)); 99 } 100 101 void *jz4780_msc_get_channel(void *msc, uint8_t channel, l4_cap_idx_t irq, 102 void *dma) 103 { 104 return static_cast<Msc_jz4780_chip *>(msc)->get_channel(channel, irq, 105 static_cast<Dma_jz4780_channel *>(dma)); 106 } 107 108 struct msc_card *jz4780_msc_get_cards(void *msc_channel) 109 { 110 return static_cast<Msc_channel *>(msc_channel)->get_cards(); 111 } 112 113 uint8_t jz4780_msc_num_cards(void *msc_channel) 114 { 115 return static_cast<Msc_channel *>(msc_channel)->num_cards(); 116 } 117 118 void jz4780_msc_enable(void *msc_channel) 119 { 120 static_cast<Msc_channel *>(msc_channel)->enable(); 121 } 122 123 uint32_t jz4780_msc_read(void *msc_channel, uint8_t card, 124 struct dma_region *region, 125 uint32_t address, uint32_t count) 126 { 127 return static_cast<Msc_channel *>(msc_channel)->read(card, region, 128 address, count); 129 } 130 131 uint32_t jz4780_msc_read_blocks(void *msc_channel, uint8_t card, 132 struct dma_region *region, 133 uint32_t block_address, uint32_t block_count) 134 { 135 return static_cast<Msc_channel *>(msc_channel)->read_blocks(card, region, 136 block_address, block_count); 137 }