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, 109 l4_cap_idx_t irq = L4_INVALID_CAP); 110 111 unsigned int transfer(uint32_t source, uint32_t destination, 112 unsigned int count, 113 bool source_increment, bool destination_increment, 114 uint8_t source_width, uint8_t destination_width, 115 uint8_t transfer_unit_size, 116 enum Dma_jz4780_request_type type=Dma_request_auto, 117 l4_addr_t desc_vaddr = 0, 118 l4re_dma_space_dma_addr_t desc_paddr = 0); 119 120 unsigned int wait(); 121 122 protected: 123 // Transfer property configuration. 124 125 uint32_t encode_req_detect_int_length(uint8_t units); 126 127 uint32_t encode_source_port_width(uint8_t width); 128 129 uint32_t encode_destination_port_width(uint8_t width); 130 131 uint32_t encode_transfer_unit_size(uint8_t size); 132 133 // Transaction control. 134 135 void ack_irq(); 136 137 bool completed(); 138 139 bool error(); 140 141 bool halted(); 142 143 bool wait_for_irq(); 144 145 bool wait_for_irq(unsigned int timeout); 146 }; 147 148 // DMA device control. 149 150 class Dma_jz4780_chip 151 { 152 private: 153 Hw::Register_block<32> _regs; 154 l4_addr_t _start, _end; 155 Cpm_jz4780_chip *_cpm; 156 157 public: 158 Dma_jz4780_chip(l4_addr_t start, l4_addr_t end, Cpm_jz4780_chip *cpm); 159 160 void disable(); 161 162 void enable(); 163 164 Dma_jz4780_channel *get_channel(uint8_t channel, 165 l4_cap_idx_t irq = L4_INVALID_CAP); 166 167 // Transaction control. 168 169 void ack_irq(uint8_t channel); 170 171 bool error(); 172 173 bool halted(); 174 175 bool have_interrupt(uint8_t channel); 176 177 // Descriptor operations. 178 179 void commit_descriptor(uint8_t channel); 180 }; 181 182 #endif /* __cplusplus */ 183 184 185 186 /* C language interface. */ 187 188 EXTERN_C_BEGIN 189 190 void *jz4780_dma_init(l4_addr_t start, l4_addr_t end, void *cpm); 191 192 void jz4780_dma_disable(void *dma_chip); 193 194 void jz4780_dma_enable(void *dma_chip); 195 196 void *jz4780_dma_get_channel(void *dma, uint8_t channel, l4_cap_idx_t irq); 197 198 unsigned int jz4780_dma_transfer(void *dma_channel, 199 uint32_t source, uint32_t destination, 200 unsigned int count, 201 int source_increment, int destination_increment, 202 uint8_t source_width, uint8_t destination_width, 203 uint8_t transfer_unit_size, 204 enum Dma_jz4780_request_type type); 205 206 unsigned int jz4780_dma_transfer_descriptor(void *dma_channel, 207 uint32_t source, uint32_t destination, 208 unsigned int count, 209 int source_increment, int destination_increment, 210 uint8_t source_width, uint8_t destination_width, 211 uint8_t transfer_unit_size, 212 enum Dma_jz4780_request_type type, 213 l4_addr_t desc_vaddr, 214 l4re_dma_space_dma_addr_t desc_paddr); 215 216 unsigned int jz4780_dma_wait(void *dma_channel); 217 218 EXTERN_C_END