1 /* 2 * DMA support for the JZ4780. 3 * 4 * Copyright (C) 2021, 2023 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 /* Enumerated types for various transfer parameters. */ 31 32 enum Dma_jz4780_request_type 33 { 34 Dma_request_i2s1_out = 4, 35 Dma_request_i2s1_in = 5, 36 Dma_request_i2s0_out = 6, 37 Dma_request_i2s0_in = 7, 38 Dma_request_auto = 8, 39 Dma_request_sadc_in = 9, 40 Dma_request_uart4_out = 12, 41 Dma_request_uart4_in = 13, 42 Dma_request_uart3_out = 14, 43 Dma_request_uart3_in = 15, 44 Dma_request_uart2_out = 16, 45 Dma_request_uart2_in = 17, 46 Dma_request_uart1_out = 18, 47 Dma_request_uart1_in = 19, 48 Dma_request_uart0_out = 20, 49 Dma_request_uart0_in = 21, 50 Dma_request_ssi0_out = 22, 51 Dma_request_ssi0_in = 23, 52 Dma_request_ssi1_out = 24, 53 Dma_request_ssi1_in = 25, 54 Dma_request_msc0_out = 26, 55 Dma_request_msc0_in = 27, 56 Dma_request_msc1_out = 28, 57 Dma_request_msc1_in = 29, 58 Dma_request_msc2_out = 30, 59 Dma_request_msc2_in = 31, 60 Dma_request_pcm0_out = 32, 61 Dma_request_pcm0_in = 33, 62 Dma_request_i2c0_out = 36, 63 Dma_request_i2c0_in = 37, 64 Dma_request_i2c1_out = 38, 65 Dma_request_i2c1_in = 39, 66 Dma_request_i2c2_out = 40, 67 Dma_request_i2c2_in = 41, 68 Dma_request_i2c3_out = 42, 69 Dma_request_i2c3_in = 43, 70 Dma_request_i2c4_out = 44, 71 Dma_request_i2c4_in = 45, 72 Dma_request_des_out = 46, 73 Dma_request_des_in = 47, 74 }; 75 76 /* Descriptor structure. */ 77 78 struct jz4780_dma_descriptor 79 { 80 uint32_t command, source, destination, transfer_count, 81 stride, request_source, reserved0, reserved1; 82 }; 83 84 85 86 #ifdef __cplusplus 87 88 #include <l4/devices/cpm-jz4780.h> 89 #include <l4/devices/hw_mmio_register_block.h> 90 91 // Forward declaration. 92 93 class Dma_jz4780_chip; 94 95 96 97 // DMA channel. 98 99 class Dma_jz4780_channel 100 { 101 private: 102 Hw::Register_block<32> _regs; 103 Dma_jz4780_chip *_chip; 104 uint8_t _channel; 105 l4_cap_idx_t _irq = L4_INVALID_CAP; 106 107 public: 108 Dma_jz4780_channel(Dma_jz4780_chip *chip, uint8_t channel, l4_addr_t start, l4_cap_idx_t irq); 109 110 unsigned int transfer(uint32_t source, uint32_t destination, 111 unsigned int count, 112 bool source_increment, bool destination_increment, 113 uint8_t source_width, uint8_t destination_width, 114 uint8_t transfer_unit_size, 115 enum Dma_jz4780_request_type type=Dma_request_auto, 116 l4_addr_t desc_vaddr = 0, 117 l4re_dma_space_dma_addr_t desc_paddr = 0); 118 119 unsigned int wait(); 120 121 protected: 122 // Transfer property configuration. 123 124 uint32_t encode_req_detect_int_length(uint8_t units); 125 126 uint32_t encode_source_port_width(uint8_t width); 127 128 uint32_t encode_destination_port_width(uint8_t width); 129 130 uint32_t encode_transfer_unit_size(uint8_t size); 131 132 // Transaction control. 133 134 void ack_irq(); 135 136 bool completed(); 137 138 bool error(); 139 140 bool halted(); 141 142 bool wait_for_irq(); 143 144 bool wait_for_irq(unsigned int timeout); 145 }; 146 147 // DMA device control. 148 149 class Dma_jz4780_chip 150 { 151 private: 152 Hw::Register_block<32> _regs; 153 l4_addr_t _start, _end; 154 Cpm_jz4780_chip *_cpm; 155 156 public: 157 Dma_jz4780_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm); 158 159 void disable(); 160 161 void enable(); 162 163 Dma_jz4780_channel *get_channel(uint8_t channel, l4_cap_idx_t irq); 164 165 // Transaction control. 166 167 void ack_irq(uint8_t channel); 168 169 bool error(); 170 171 bool halted(); 172 173 bool have_interrupt(uint8_t channel); 174 175 // Descriptor operations. 176 177 void commit_descriptor(uint8_t channel); 178 }; 179 180 #endif /* __cplusplus */ 181 182 183 184 /* C language interface. */ 185 186 EXTERN_C_BEGIN 187 188 void *jz4780_dma_init(l4_addr_t start, l4_addr_t end, void *cpm); 189 190 void jz4780_dma_disable(void *dma_chip); 191 192 void jz4780_dma_enable(void *dma_chip); 193 194 void *jz4780_dma_get_channel(void *dma, uint8_t channel, l4_cap_idx_t irq); 195 196 unsigned int jz4780_dma_transfer(void *dma_channel, 197 uint32_t source, uint32_t destination, 198 unsigned int count, 199 int source_increment, int destination_increment, 200 uint8_t source_width, uint8_t destination_width, 201 uint8_t transfer_unit_size, 202 enum Dma_jz4780_request_type type); 203 204 unsigned int jz4780_dma_transfer_descriptor(void *dma_channel, 205 uint32_t source, uint32_t destination, 206 unsigned int count, 207 int source_increment, int destination_increment, 208 uint8_t source_width, uint8_t destination_width, 209 uint8_t transfer_unit_size, 210 enum Dma_jz4780_request_type type, 211 l4_addr_t desc_vaddr, 212 l4re_dma_space_dma_addr_t desc_paddr); 213 214 unsigned int jz4780_dma_wait(void *dma_channel); 215 216 EXTERN_C_END