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