1 /* 2 * MSC (MMC/SD controller) peripheral support. 3 * 4 * Copyright (C) 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #pragma once 23 24 #include <l4/re/c/dma_space.h> 25 #include <l4/sys/types.h> 26 #include <stdint.h> 27 28 29 30 #ifdef __cplusplus 31 32 #include <l4/devices/hw_mmio_register_block.h> 33 34 35 36 // MMC/SD structures. 37 38 struct CID 39 { 40 uint16_t month:4, year:8, reserved:4; 41 uint32_t serial; 42 uint8_t revision; 43 char name[5]; 44 uint16_t oem; 45 uint8_t manufacturer; 46 } __attribute__((packed)); 47 48 struct CSD 49 { 50 uint8_t reserved0:2, format:2, temp_write_prot:1, perm_write_prot:1, copy:1, format_group:1; 51 uint16_t reserved1:5, write_block_partial:1, write_blocklen:4, write_time_factor:3, 52 reserved2:2, write_prot_group_enable:1; 53 uint64_t write_prot_group_size:7, erase_sector_size:7, erase_single_block_enable:1, 54 device_size_multiplier:3, max_write_current_max:3, max_write_current_min:3, 55 max_read_current_max:3, max_read_current_min:3, device_size:12, 56 reserved3:2, dsr_implemented:1, read_block_misalign:1, write_block_misalign:1, 57 read_block_partial:1, read_blocklen:4, card_command_classes:12; 58 uint8_t tran_speed, data_read_access_time_2, data_read_access_time_1, 59 reserved4:6, csd:2; 60 } __attribute__((packed)); 61 62 63 64 // MMC/SD controller channel. 65 66 class Msc_channel 67 { 68 protected: 69 l4_addr_t _msc_start; 70 Hw::Register_block<32> _regs; 71 l4_cap_idx_t _irq; 72 73 // Support eight CID/CSD entries of 120 bits and RCA entries of 16 bits. 74 75 struct CID _cid[8]; 76 struct CSD _csd[8]; 77 uint16_t _rca[8], _current_rca; 78 uint8_t _bus_width[8], _current_bus_width; 79 uint8_t _cards; 80 81 // Utility methods. 82 83 uint32_t get_field(uint32_t reg, uint32_t mask, uint8_t shift); 84 void set_field(uint32_t reg, uint32_t mask, uint8_t shift, uint32_t value); 85 86 // Low-level operations. 87 88 void ack_irq(uint32_t flags); 89 void unmask_irq(uint32_t flags); 90 void reset(); 91 void start_clock(); 92 void stop_clock(); 93 94 // Command properties. 95 96 bool command_will_write(uint8_t index); 97 bool command_with_data(uint8_t index); 98 bool command_uses_busy(uint8_t index); 99 uint8_t get_response_format(uint8_t index); 100 101 bool app_command_will_write(uint8_t index); 102 bool app_command_with_data(uint8_t index); 103 bool app_command_uses_busy(uint8_t index); 104 uint8_t get_app_response_format(uint8_t index); 105 106 // Command initiation. 107 108 bool send_app_command(uint8_t index, uint32_t arg); 109 bool send_command(uint8_t index, uint32_t arg); 110 bool send_command(uint8_t index, uint32_t arg, uint8_t response_format, 111 bool data, bool write, bool busy); 112 113 // Response handling. 114 115 bool have_response(); 116 void read_response(uint16_t *buffer, uint8_t units); 117 bool wait_for_irq(uint32_t flags); 118 bool wait_for_irq(uint32_t flags, unsigned int timeout); 119 120 // Initialisation operations. 121 122 bool check_sd(); 123 void init_sdio(); 124 void init_sdmem(); 125 void init_mmc(); 126 void identify_cards(); 127 void query_cards(); 128 129 // Transfer operations. 130 131 uint32_t recv_data(l4re_dma_space_dma_addr_t paddr, uint32_t count); 132 uint32_t send_data(l4re_dma_space_dma_addr_t paddr, uint32_t count); 133 134 virtual uint32_t transfer(l4re_dma_space_dma_addr_t from_paddr, 135 l4re_dma_space_dma_addr_t to_paddr, 136 bool recv, uint32_t count) = 0; 137 138 public: 139 explicit Msc_channel(l4_addr_t msc_start, l4_addr_t start, l4_cap_idx_t irq); 140 141 virtual ~Msc_channel(); 142 143 void enable(); 144 145 uint32_t get_status(); 146 147 uint32_t read_block(uint8_t card, l4re_dma_space_dma_addr_t paddr); 148 }; 149 150 151 152 // MMC/SD controller device control. 153 154 class Msc_chip 155 { 156 protected: 157 l4_addr_t _msc_start, _start, _end; 158 159 virtual unsigned int num_channels() = 0; 160 161 public: 162 explicit Msc_chip(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end); 163 }; 164 165 #endif /* __cplusplus */