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