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-x1600.h" 26 27 28 29 // Channel abstraction. 30 31 Msc_x1600_channel::Msc_x1600_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_x1600_channel *dma, 35 enum Dma_x1600_request_type request_type_in, 36 enum Dma_x1600_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 // Reset for X1600 and other recent SoCs only. 45 46 void 47 Msc_x1600_channel::reset() 48 { 49 _regs[Msc_control] = _regs[Msc_control] | Control_reset; 50 51 // NOTE: On the X1600, Msc_control tends to read as zero, anyway, making this 52 // NOTE: equivalent to clearing the register. 53 54 _regs[Msc_control] = _regs[Msc_control] & ~Control_reset; 55 56 // Sufficient for other SoCs... 57 58 while (_regs[Msc_status] & Status_resetting); 59 } 60 61 // Request the transfer of the indicated number of bytes between two physical 62 // addresses in the indicated direction, returning the number of bytes to be 63 // transferred. 64 // NOTE: To be consolidated into a generic method that uses generic request types. 65 66 uint32_t 67 Msc_x1600_channel::transfer(l4re_dma_space_dma_addr_t from_paddr, 68 l4re_dma_space_dma_addr_t to_paddr, 69 bool recv, uint32_t count) 70 { 71 uint32_t unit_size = 32; 72 uint32_t unit_count = count / unit_size; 73 uint32_t to_transfer; 74 75 to_transfer = _dma->transfer(from_paddr, to_paddr, 76 unit_count, 77 recv ? false : true, // increment source if sending 78 recv ? true : false, // increment destination if receiving 79 4, 4, unit_size, 80 recv ? _request_type_in : _request_type_out); 81 82 return to_transfer * unit_size; 83 } 84 85 86 87 // Peripheral abstraction. 88 89 Msc_x1600_chip::Msc_x1600_chip(l4_addr_t msc_start, l4_addr_t start, 90 l4_addr_t end, Cpm_chip *cpm) 91 : Msc_chip(msc_start, start, end, cpm) 92 { 93 } 94 95 Msc_channel *Msc_x1600_chip::get_channel(uint8_t channel, l4_cap_idx_t irq, 96 Dma_x1600_channel *dma) 97 { 98 if (channel < num_channels()) 99 return new Msc_x1600_channel(_msc_start + channel * Msc_channel_offset, 100 _start + channel * Msc_channel_offset, 101 irq, _cpm, _clocks[channel], dma, 102 _in_types[channel], _out_types[channel]); 103 else 104 throw -L4_EINVAL; 105 } 106 107 108 109 // C language interface functions. 110 111 void *x1600_msc_init(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end, 112 void *cpm) 113 { 114 return (void *) new Msc_x1600_chip(msc_start, start, end, 115 static_cast<Cpm_x1600_chip *>(cpm)); 116 } 117 118 void *x1600_msc_get_channel(void *msc, uint8_t channel, l4_cap_idx_t irq, 119 void *dma) 120 { 121 return static_cast<Msc_x1600_chip *>(msc)->get_channel(channel, irq, 122 static_cast<Dma_x1600_channel *>(dma)); 123 } 124 125 struct msc_card *x1600_msc_get_cards(void *msc_channel) 126 { 127 return static_cast<Msc_channel *>(msc_channel)->get_cards(); 128 } 129 130 uint8_t x1600_msc_num_cards(void *msc_channel) 131 { 132 return static_cast<Msc_channel *>(msc_channel)->num_cards(); 133 } 134 135 void x1600_msc_enable(void *msc_channel) 136 { 137 static_cast<Msc_channel *>(msc_channel)->enable(); 138 } 139 140 uint32_t x1600_msc_read(void *msc_channel, uint8_t card, 141 struct dma_region *region, 142 uint32_t address, uint32_t count) 143 { 144 return static_cast<Msc_channel *>(msc_channel)->read(card, region, 145 address, count); 146 } 147 148 uint32_t x1600_msc_read_blocks(void *msc_channel, uint8_t card, 149 struct dma_region *region, 150 uint32_t block_address, uint32_t block_count) 151 { 152 return static_cast<Msc_channel *>(msc_channel)->read_blocks(card, region, 153 block_address, block_count); 154 }