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 | #pragma once |
paul@258 | 23 | |
paul@264 | 24 | #include <l4/devices/clocks.h> |
paul@264 | 25 | #include <l4/devices/cpm.h> |
paul@264 | 26 | #include <l4/devices/dma.h> |
paul@263 | 27 | #include <l4/devices/msc.h> |
paul@258 | 28 | #include <l4/sys/types.h> |
paul@258 | 29 | #include <stdint.h> |
paul@258 | 30 | |
paul@258 | 31 | |
paul@258 | 32 | |
paul@262 | 33 | #ifdef __cplusplus |
paul@262 | 34 | |
paul@262 | 35 | #include <l4/devices/hw_mmio_register_block.h> |
paul@262 | 36 | |
paul@262 | 37 | |
paul@262 | 38 | |
paul@258 | 39 | // MMC/SD controller channel. |
paul@258 | 40 | |
paul@258 | 41 | class Msc_channel |
paul@258 | 42 | { |
paul@258 | 43 | protected: |
paul@258 | 44 | l4_addr_t _msc_start; |
paul@258 | 45 | Hw::Register_block<32> _regs; |
paul@258 | 46 | l4_cap_idx_t _irq; |
paul@264 | 47 | Cpm_chip *_cpm; |
paul@264 | 48 | enum Clock_identifiers _clock; |
paul@258 | 49 | |
paul@262 | 50 | // Support eight cards. |
paul@258 | 51 | |
paul@262 | 52 | struct msc_card _cards[8]; |
paul@262 | 53 | uint8_t _num_cards; |
paul@262 | 54 | int _card; |
paul@258 | 55 | |
paul@258 | 56 | // Utility methods. |
paul@258 | 57 | |
paul@258 | 58 | uint32_t get_field(uint32_t reg, uint32_t mask, uint8_t shift); |
paul@258 | 59 | void set_field(uint32_t reg, uint32_t mask, uint8_t shift, uint32_t value); |
paul@258 | 60 | |
paul@258 | 61 | // Low-level operations. |
paul@258 | 62 | |
paul@258 | 63 | void ack_irq(uint32_t flags); |
paul@258 | 64 | void unmask_irq(uint32_t flags); |
paul@264 | 65 | |
paul@264 | 66 | virtual void reset(); |
paul@264 | 67 | |
paul@264 | 68 | bool set_clock_frequency(uint64_t frequency); |
paul@258 | 69 | void start_clock(); |
paul@258 | 70 | void stop_clock(); |
paul@258 | 71 | |
paul@266 | 72 | // Hardware properties. |
paul@266 | 73 | |
paul@266 | 74 | virtual bool have_dma_enable_in_command(); |
paul@266 | 75 | virtual bool have_dma_selection(); |
paul@266 | 76 | |
paul@258 | 77 | // Command properties. |
paul@258 | 78 | |
paul@258 | 79 | bool command_will_write(uint8_t index); |
paul@258 | 80 | bool command_with_data(uint8_t index); |
paul@258 | 81 | bool command_uses_busy(uint8_t index); |
paul@258 | 82 | uint8_t get_response_format(uint8_t index); |
paul@259 | 83 | |
paul@259 | 84 | bool app_command_will_write(uint8_t index); |
paul@259 | 85 | bool app_command_with_data(uint8_t index); |
paul@259 | 86 | bool app_command_uses_busy(uint8_t index); |
paul@258 | 87 | uint8_t get_app_response_format(uint8_t index); |
paul@258 | 88 | |
paul@258 | 89 | // Command initiation. |
paul@258 | 90 | |
paul@258 | 91 | bool send_app_command(uint8_t index, uint32_t arg); |
paul@258 | 92 | bool send_command(uint8_t index, uint32_t arg); |
paul@258 | 93 | bool send_command(uint8_t index, uint32_t arg, uint8_t response_format, |
paul@258 | 94 | bool data, bool write, bool busy); |
paul@258 | 95 | |
paul@258 | 96 | // Response handling. |
paul@258 | 97 | |
paul@258 | 98 | bool have_response(); |
paul@258 | 99 | void read_response(uint16_t *buffer, uint8_t units); |
paul@258 | 100 | bool wait_for_irq(uint32_t flags); |
paul@258 | 101 | bool wait_for_irq(uint32_t flags, unsigned int timeout); |
paul@258 | 102 | |
paul@258 | 103 | // Initialisation operations. |
paul@258 | 104 | |
paul@258 | 105 | bool check_sd(); |
paul@258 | 106 | void init_sdio(); |
paul@258 | 107 | void init_sdmem(); |
paul@258 | 108 | void init_mmc(); |
paul@258 | 109 | void identify_cards(); |
paul@258 | 110 | void query_cards(); |
paul@258 | 111 | |
paul@258 | 112 | // Transfer operations. |
paul@258 | 113 | |
paul@264 | 114 | uint32_t recv_data(struct dma_region *region, uint32_t count); |
paul@264 | 115 | uint32_t send_data(struct dma_region *region, uint32_t count); |
paul@258 | 116 | |
paul@258 | 117 | virtual uint32_t transfer(l4re_dma_space_dma_addr_t from_paddr, |
paul@258 | 118 | l4re_dma_space_dma_addr_t to_paddr, |
paul@258 | 119 | bool recv, uint32_t count) = 0; |
paul@258 | 120 | |
paul@258 | 121 | public: |
paul@264 | 122 | explicit Msc_channel(l4_addr_t msc_start, l4_addr_t start, l4_cap_idx_t irq, |
paul@264 | 123 | Cpm_chip *cpm, enum Clock_identifiers clock); |
paul@258 | 124 | |
paul@258 | 125 | virtual ~Msc_channel(); |
paul@258 | 126 | |
paul@258 | 127 | void enable(); |
paul@258 | 128 | |
paul@263 | 129 | msc_card *get_cards(); |
paul@263 | 130 | |
paul@263 | 131 | uint8_t num_cards(); |
paul@258 | 132 | |
paul@268 | 133 | uint32_t read(uint8_t card, struct dma_region *region, |
paul@268 | 134 | uint32_t address, uint32_t count); |
paul@268 | 135 | |
paul@264 | 136 | uint32_t read_blocks(uint8_t card, struct dma_region *region, |
paul@261 | 137 | uint32_t block_address, uint32_t block_count); |
paul@258 | 138 | }; |
paul@258 | 139 | |
paul@258 | 140 | |
paul@258 | 141 | |
paul@258 | 142 | // MMC/SD controller device control. |
paul@258 | 143 | |
paul@258 | 144 | class Msc_chip |
paul@258 | 145 | { |
paul@258 | 146 | protected: |
paul@258 | 147 | l4_addr_t _msc_start, _start, _end; |
paul@264 | 148 | Cpm_chip *_cpm; |
paul@258 | 149 | |
paul@258 | 150 | virtual unsigned int num_channels() = 0; |
paul@258 | 151 | |
paul@258 | 152 | public: |
paul@264 | 153 | explicit Msc_chip(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end, |
paul@264 | 154 | Cpm_chip *cpm); |
paul@258 | 155 | }; |
paul@258 | 156 | |
paul@258 | 157 | #endif /* __cplusplus */ |