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