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@258 | 24 | #include <l4/re/c/dma_space.h> |
paul@258 | 25 | #include <l4/sys/types.h> |
paul@258 | 26 | #include <stdint.h> |
paul@258 | 27 | |
paul@258 | 28 | |
paul@258 | 29 | |
paul@262 | 30 | /* MMC/SD structures. */ |
paul@258 | 31 | |
paul@258 | 32 | struct CID |
paul@258 | 33 | { |
paul@258 | 34 | uint16_t month:4, year:8, reserved:4; |
paul@258 | 35 | uint32_t serial; |
paul@258 | 36 | uint8_t revision; |
paul@258 | 37 | char name[5]; |
paul@258 | 38 | uint16_t oem; |
paul@258 | 39 | uint8_t manufacturer; |
paul@258 | 40 | } __attribute__((packed)); |
paul@258 | 41 | |
paul@258 | 42 | struct CSD |
paul@258 | 43 | { |
paul@258 | 44 | uint8_t reserved0:2, format:2, temp_write_prot:1, perm_write_prot:1, copy:1, format_group:1; |
paul@258 | 45 | uint16_t reserved1:5, write_block_partial:1, write_blocklen:4, write_time_factor:3, |
paul@258 | 46 | reserved2:2, write_prot_group_enable:1; |
paul@258 | 47 | uint64_t write_prot_group_size:7, erase_sector_size:7, erase_single_block_enable:1, |
paul@258 | 48 | device_size_multiplier:3, max_write_current_max:3, max_write_current_min:3, |
paul@258 | 49 | max_read_current_max:3, max_read_current_min:3, device_size:12, |
paul@258 | 50 | reserved3:2, dsr_implemented:1, read_block_misalign:1, write_block_misalign:1, |
paul@258 | 51 | read_block_partial:1, read_blocklen:4, card_command_classes:12; |
paul@258 | 52 | uint8_t tran_speed, data_read_access_time_2, data_read_access_time_1, |
paul@258 | 53 | reserved4:6, csd:2; |
paul@258 | 54 | } __attribute__((packed)); |
paul@258 | 55 | |
paul@258 | 56 | |
paul@258 | 57 | |
paul@262 | 58 | /* Generic card structure. */ |
paul@262 | 59 | |
paul@262 | 60 | struct msc_card |
paul@262 | 61 | { |
paul@262 | 62 | uint16_t rca; |
paul@262 | 63 | uint32_t ocr; |
paul@262 | 64 | uint8_t bus_width; |
paul@262 | 65 | struct CID cid; |
paul@262 | 66 | struct CSD csd; |
paul@262 | 67 | }; |
paul@262 | 68 | |
paul@262 | 69 | |
paul@262 | 70 | |
paul@262 | 71 | #ifdef __cplusplus |
paul@262 | 72 | |
paul@262 | 73 | #include <l4/devices/hw_mmio_register_block.h> |
paul@262 | 74 | |
paul@262 | 75 | |
paul@262 | 76 | |
paul@258 | 77 | // MMC/SD controller channel. |
paul@258 | 78 | |
paul@258 | 79 | class Msc_channel |
paul@258 | 80 | { |
paul@258 | 81 | protected: |
paul@258 | 82 | l4_addr_t _msc_start; |
paul@258 | 83 | Hw::Register_block<32> _regs; |
paul@258 | 84 | l4_cap_idx_t _irq; |
paul@258 | 85 | |
paul@262 | 86 | // Support eight cards. |
paul@258 | 87 | |
paul@262 | 88 | struct msc_card _cards[8]; |
paul@262 | 89 | uint8_t _num_cards; |
paul@262 | 90 | int _card; |
paul@258 | 91 | |
paul@258 | 92 | // Utility methods. |
paul@258 | 93 | |
paul@258 | 94 | uint32_t get_field(uint32_t reg, uint32_t mask, uint8_t shift); |
paul@258 | 95 | void set_field(uint32_t reg, uint32_t mask, uint8_t shift, uint32_t value); |
paul@258 | 96 | |
paul@258 | 97 | // Low-level operations. |
paul@258 | 98 | |
paul@258 | 99 | void ack_irq(uint32_t flags); |
paul@258 | 100 | void unmask_irq(uint32_t flags); |
paul@258 | 101 | void reset(); |
paul@258 | 102 | void start_clock(); |
paul@258 | 103 | void stop_clock(); |
paul@258 | 104 | |
paul@258 | 105 | // Command properties. |
paul@258 | 106 | |
paul@258 | 107 | bool command_will_write(uint8_t index); |
paul@258 | 108 | bool command_with_data(uint8_t index); |
paul@258 | 109 | bool command_uses_busy(uint8_t index); |
paul@258 | 110 | uint8_t get_response_format(uint8_t index); |
paul@259 | 111 | |
paul@259 | 112 | bool app_command_will_write(uint8_t index); |
paul@259 | 113 | bool app_command_with_data(uint8_t index); |
paul@259 | 114 | bool app_command_uses_busy(uint8_t index); |
paul@258 | 115 | uint8_t get_app_response_format(uint8_t index); |
paul@258 | 116 | |
paul@258 | 117 | // Command initiation. |
paul@258 | 118 | |
paul@258 | 119 | bool send_app_command(uint8_t index, uint32_t arg); |
paul@258 | 120 | bool send_command(uint8_t index, uint32_t arg); |
paul@258 | 121 | bool send_command(uint8_t index, uint32_t arg, uint8_t response_format, |
paul@258 | 122 | bool data, bool write, bool busy); |
paul@258 | 123 | |
paul@258 | 124 | // Response handling. |
paul@258 | 125 | |
paul@258 | 126 | bool have_response(); |
paul@258 | 127 | void read_response(uint16_t *buffer, uint8_t units); |
paul@258 | 128 | bool wait_for_irq(uint32_t flags); |
paul@258 | 129 | bool wait_for_irq(uint32_t flags, unsigned int timeout); |
paul@258 | 130 | |
paul@258 | 131 | // Initialisation operations. |
paul@258 | 132 | |
paul@258 | 133 | bool check_sd(); |
paul@258 | 134 | void init_sdio(); |
paul@258 | 135 | void init_sdmem(); |
paul@258 | 136 | void init_mmc(); |
paul@258 | 137 | void identify_cards(); |
paul@258 | 138 | void query_cards(); |
paul@258 | 139 | |
paul@258 | 140 | // Transfer operations. |
paul@258 | 141 | |
paul@258 | 142 | uint32_t recv_data(l4re_dma_space_dma_addr_t paddr, uint32_t count); |
paul@258 | 143 | uint32_t send_data(l4re_dma_space_dma_addr_t paddr, uint32_t count); |
paul@258 | 144 | |
paul@258 | 145 | virtual uint32_t transfer(l4re_dma_space_dma_addr_t from_paddr, |
paul@258 | 146 | l4re_dma_space_dma_addr_t to_paddr, |
paul@258 | 147 | bool recv, uint32_t count) = 0; |
paul@258 | 148 | |
paul@258 | 149 | public: |
paul@258 | 150 | explicit Msc_channel(l4_addr_t msc_start, l4_addr_t start, l4_cap_idx_t irq); |
paul@258 | 151 | |
paul@258 | 152 | virtual ~Msc_channel(); |
paul@258 | 153 | |
paul@258 | 154 | void enable(); |
paul@258 | 155 | |
paul@258 | 156 | uint32_t get_status(); |
paul@258 | 157 | |
paul@261 | 158 | uint32_t read_blocks(uint8_t card, l4re_dma_space_dma_addr_t paddr, |
paul@261 | 159 | uint32_t block_address, uint32_t block_count); |
paul@258 | 160 | }; |
paul@258 | 161 | |
paul@258 | 162 | |
paul@258 | 163 | |
paul@258 | 164 | // MMC/SD controller device control. |
paul@258 | 165 | |
paul@258 | 166 | class Msc_chip |
paul@258 | 167 | { |
paul@258 | 168 | protected: |
paul@258 | 169 | l4_addr_t _msc_start, _start, _end; |
paul@258 | 170 | |
paul@258 | 171 | virtual unsigned int num_channels() = 0; |
paul@258 | 172 | |
paul@258 | 173 | public: |
paul@258 | 174 | explicit Msc_chip(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end); |
paul@258 | 175 | }; |
paul@258 | 176 | |
paul@258 | 177 | #endif /* __cplusplus */ |