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@258 | 24 | #include "msc-jz4780.h" |
paul@258 | 25 | |
paul@258 | 26 | |
paul@258 | 27 | |
paul@258 | 28 | // Register locations for each channel. |
paul@258 | 29 | |
paul@258 | 30 | enum Regs : unsigned |
paul@258 | 31 | { |
paul@258 | 32 | // Channel block size/offset. |
paul@258 | 33 | |
paul@258 | 34 | Msc_channel_offset = 0x10000, |
paul@258 | 35 | }; |
paul@258 | 36 | |
paul@258 | 37 | |
paul@258 | 38 | |
paul@258 | 39 | // Channel abstraction. |
paul@258 | 40 | |
paul@258 | 41 | Msc_jz4780_channel::Msc_jz4780_channel(l4_addr_t msc_start, l4_addr_t addr, |
paul@258 | 42 | l4_cap_idx_t irq, Dma_jz4780_channel *dma, |
paul@258 | 43 | enum Dma_jz4780_request_type request_type_in, |
paul@258 | 44 | enum Dma_jz4780_request_type request_type_out) |
paul@258 | 45 | : Msc_channel(msc_start, addr, irq), |
paul@258 | 46 | _dma(dma), |
paul@258 | 47 | _request_type_in(request_type_in), |
paul@258 | 48 | _request_type_out(request_type_out) |
paul@258 | 49 | { |
paul@258 | 50 | } |
paul@258 | 51 | |
paul@258 | 52 | // Request the transfer of the indicated number of bytes between two physical |
paul@261 | 53 | // addresses in the indicated direction, returning the number of bytes to be |
paul@258 | 54 | // transferred. |
paul@258 | 55 | // NOTE: To be consolidated into a generic method that uses generic request types. |
paul@258 | 56 | |
paul@258 | 57 | uint32_t |
paul@258 | 58 | Msc_jz4780_channel::transfer(l4re_dma_space_dma_addr_t from_paddr, |
paul@258 | 59 | l4re_dma_space_dma_addr_t to_paddr, |
paul@258 | 60 | bool recv, uint32_t count) |
paul@258 | 61 | { |
paul@258 | 62 | uint32_t unit_size = 32; |
paul@258 | 63 | uint32_t unit_count = count / unit_size; |
paul@258 | 64 | uint32_t to_transfer; |
paul@258 | 65 | |
paul@258 | 66 | to_transfer = _dma->transfer(from_paddr, to_paddr, |
paul@258 | 67 | unit_count, |
paul@258 | 68 | recv ? false : true, // increment source if sending |
paul@258 | 69 | recv ? true : false, // increment destination if receiving |
paul@258 | 70 | 4, 4, unit_size, |
paul@258 | 71 | recv ? _request_type_in : _request_type_out); |
paul@258 | 72 | |
paul@261 | 73 | return to_transfer * unit_size; |
paul@258 | 74 | } |
paul@258 | 75 | |
paul@258 | 76 | |
paul@258 | 77 | |
paul@258 | 78 | // Peripheral abstraction. |
paul@258 | 79 | |
paul@258 | 80 | Msc_jz4780_chip::Msc_jz4780_chip(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end) |
paul@258 | 81 | : Msc_chip(msc_start, start, end) |
paul@258 | 82 | { |
paul@258 | 83 | } |
paul@258 | 84 | |
paul@258 | 85 | Msc_channel *Msc_jz4780_chip::get_channel(uint8_t channel, l4_cap_idx_t irq, |
paul@258 | 86 | Dma_jz4780_channel *dma) |
paul@258 | 87 | { |
paul@258 | 88 | const enum Dma_jz4780_request_type in_types[] = |
paul@258 | 89 | {Dma_request_msc0_in, Dma_request_msc1_in, Dma_request_msc2_in}; |
paul@258 | 90 | |
paul@258 | 91 | const enum Dma_jz4780_request_type out_types[] = |
paul@258 | 92 | {Dma_request_msc0_out, Dma_request_msc1_out, Dma_request_msc2_out}; |
paul@258 | 93 | |
paul@258 | 94 | if (channel < num_channels()) |
paul@258 | 95 | return new Msc_jz4780_channel(_msc_start + channel * Msc_channel_offset, |
paul@258 | 96 | _start + channel * Msc_channel_offset, |
paul@258 | 97 | irq, dma, |
paul@258 | 98 | in_types[channel], out_types[channel]); |
paul@258 | 99 | else |
paul@258 | 100 | throw -L4_EINVAL; |
paul@258 | 101 | } |
paul@258 | 102 | |
paul@258 | 103 | |
paul@258 | 104 | |
paul@258 | 105 | // C language interface functions. |
paul@258 | 106 | |
paul@258 | 107 | void *jz4780_msc_init(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end) |
paul@258 | 108 | { |
paul@258 | 109 | return (void *) new Msc_jz4780_chip(msc_start, start, end); |
paul@258 | 110 | } |
paul@258 | 111 | |
paul@258 | 112 | void *jz4780_msc_get_channel(void *msc, uint8_t channel, l4_cap_idx_t irq, |
paul@258 | 113 | void *dma) |
paul@258 | 114 | { |
paul@258 | 115 | return static_cast<Msc_jz4780_chip *>(msc)->get_channel(channel, irq, |
paul@258 | 116 | static_cast<Dma_jz4780_channel *>(dma)); |
paul@258 | 117 | } |
paul@258 | 118 | |
paul@258 | 119 | uint32_t jz4780_msc_get_status(void *msc_channel) |
paul@258 | 120 | { |
paul@258 | 121 | return static_cast<Msc_channel *>(msc_channel)->get_status(); |
paul@258 | 122 | } |
paul@258 | 123 | |
paul@258 | 124 | void jz4780_msc_enable(void *msc_channel) |
paul@258 | 125 | { |
paul@258 | 126 | static_cast<Msc_channel *>(msc_channel)->enable(); |
paul@258 | 127 | } |
paul@258 | 128 | |
paul@261 | 129 | uint32_t jz4780_msc_read_blocks(void *msc_channel, uint8_t card, |
paul@261 | 130 | l4re_dma_space_dma_addr_t paddr, |
paul@261 | 131 | uint32_t block_address, uint32_t block_count) |
paul@258 | 132 | { |
paul@261 | 133 | return static_cast<Msc_channel *>(msc_channel)->read_blocks(card, paddr, |
paul@261 | 134 | block_address, block_count); |
paul@258 | 135 | } |