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 _cards; 79 80 // Utility methods. 81 82 uint32_t get_field(uint32_t reg, uint32_t mask, uint8_t shift); 83 void set_field(uint32_t reg, uint32_t mask, uint8_t shift, uint32_t value); 84 85 // Low-level operations. 86 87 void ack_irq(uint32_t flags); 88 void unmask_irq(uint32_t flags); 89 void reset(); 90 void start_clock(); 91 void stop_clock(); 92 93 // Command properties. 94 95 bool command_will_write(uint8_t index); 96 bool command_with_data(uint8_t index); 97 bool command_uses_busy(uint8_t index); 98 uint8_t get_response_format(uint8_t index); 99 uint8_t get_app_response_format(uint8_t index); 100 101 // Command initiation. 102 103 bool send_app_command(uint8_t index, uint32_t arg); 104 bool send_command(uint8_t index, uint32_t arg); 105 bool send_command(uint8_t index, uint32_t arg, uint8_t response_format, 106 bool data, bool write, bool busy); 107 108 // Response handling. 109 110 bool have_response(); 111 void read_response(uint16_t *buffer, uint8_t units); 112 bool wait_for_irq(uint32_t flags); 113 bool wait_for_irq(uint32_t flags, unsigned int timeout); 114 115 // Initialisation operations. 116 117 bool check_sd(); 118 void init_sdio(); 119 void init_sdmem(); 120 void init_mmc(); 121 void identify_cards(); 122 void query_cards(); 123 124 // Transfer operations. 125 126 uint32_t recv_data(l4re_dma_space_dma_addr_t paddr, uint32_t count); 127 uint32_t send_data(l4re_dma_space_dma_addr_t paddr, uint32_t count); 128 129 virtual uint32_t transfer(l4re_dma_space_dma_addr_t from_paddr, 130 l4re_dma_space_dma_addr_t to_paddr, 131 bool recv, uint32_t count) = 0; 132 133 public: 134 explicit Msc_channel(l4_addr_t msc_start, l4_addr_t start, l4_cap_idx_t irq); 135 136 virtual ~Msc_channel(); 137 138 void enable(); 139 140 uint32_t get_status(); 141 142 uint32_t read_block(uint8_t card, l4re_dma_space_dma_addr_t paddr); 143 }; 144 145 146 147 // MMC/SD controller device control. 148 149 class Msc_chip 150 { 151 protected: 152 l4_addr_t _msc_start, _start, _end; 153 154 virtual unsigned int num_channels() = 0; 155 156 public: 157 explicit Msc_chip(l4_addr_t msc_start, l4_addr_t start, l4_addr_t end); 158 }; 159 160 #endif /* __cplusplus */