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