1 /* 2 * DMA support for the X1600. 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_x1600_request_type 32 { 33 Dma_request_auto = 8, 34 Dma_request_can0_out = 10, 35 Dma_request_can0_in = 11, 36 Dma_request_can1_out = 12, 37 Dma_request_can1_in = 13, 38 Dma_request_uart3_out = 14, 39 Dma_request_uart3_in = 15, 40 Dma_request_uart2_out = 16, 41 Dma_request_uart2_in = 17, 42 Dma_request_uart1_out = 18, 43 Dma_request_uart1_in = 19, 44 Dma_request_uart0_out = 20, 45 Dma_request_uart0_in = 21, 46 Dma_request_ssi0_out = 22, 47 Dma_request_ssi0_in = 23, 48 Dma_request_i2c0_out = 36, 49 Dma_request_i2c0_in = 37, 50 Dma_request_i2c1_out = 38, 51 Dma_request_i2c1_in = 39, 52 Dma_request_ssi_slv_out = 42, 53 Dma_request_ssi_slv_in = 43, 54 Dma_request_pwm0_out = 44, 55 Dma_request_pwm1_out = 45, 56 Dma_request_pwm2_out = 46, 57 Dma_request_pwm3_out = 47, 58 Dma_request_pwm4_out = 48, 59 Dma_request_pwm5_out = 49, 60 Dma_request_pwm6_out = 50, 61 Dma_request_pwm7_out = 51, 62 Dma_request_msc0_out = 52, 63 Dma_request_msc0_in = 53, 64 Dma_request_msc1_out = 54, 65 Dma_request_msc1_in = 55, 66 Dma_request_sadc_in = 56, 67 Dma_request_aic_loop_out = 61, 68 Dma_request_aic_out = 62, 69 Dma_request_aic_in = 63, 70 }; 71 72 73 74 #ifdef __cplusplus 75 76 #include <l4/devices/cpm-x1600.h> 77 #include <l4/devices/hw_mmio_register_block.h> 78 79 // Forward declaration. 80 81 class Dma_x1600_chip; 82 83 84 85 // DMA channel. 86 87 class Dma_x1600_channel 88 { 89 private: 90 Hw::Register_block<32> _regs; 91 Dma_x1600_chip *_chip; 92 uint8_t _channel; 93 l4_cap_idx_t _irq = L4_INVALID_CAP; 94 95 public: 96 Dma_x1600_channel(Dma_x1600_chip *chip, uint8_t channel, l4_addr_t start, l4_cap_idx_t irq); 97 98 unsigned int transfer(uint32_t source, uint32_t destination, 99 unsigned int count, 100 bool source_increment, bool destination_increment, 101 uint8_t source_width, uint8_t destination_width, 102 uint8_t transfer_unit_size, 103 enum Dma_x1600_request_type type=Dma_request_auto); 104 105 unsigned int wait(); 106 107 protected: 108 // Transfer property configuration. 109 110 uint32_t encode_req_detect_int_length(uint8_t units); 111 112 uint32_t encode_source_port_width(uint8_t width); 113 114 uint32_t encode_destination_port_width(uint8_t width); 115 116 uint32_t encode_transfer_unit_size(uint8_t size); 117 118 // Transaction control. 119 120 void ack_irq(); 121 122 bool completed(); 123 124 bool error(); 125 126 bool halted(); 127 128 bool wait_for_irq(); 129 130 bool wait_for_irq(unsigned int timeout); 131 }; 132 133 // DMA device control. 134 135 class Dma_x1600_chip 136 { 137 private: 138 Hw::Register_block<32> _regs; 139 l4_addr_t _start, _end; 140 Cpm_x1600_chip *_cpm; 141 142 public: 143 Dma_x1600_chip(l4_addr_t start, l4_addr_t end, Cpm_x1600_chip *cpm); 144 145 void disable(); 146 147 void enable(); 148 149 Dma_x1600_channel *get_channel(uint8_t channel, l4_cap_idx_t irq); 150 151 // Transaction control. 152 153 void ack_irq(uint8_t channel); 154 155 bool error(); 156 157 bool halted(); 158 159 bool have_interrupt(uint8_t channel); 160 }; 161 162 #endif /* __cplusplus */ 163 164 165 166 /* C language interface. */ 167 168 EXTERN_C_BEGIN 169 170 void *x1600_dma_init(l4_addr_t start, l4_addr_t end, void *cpm); 171 172 void x1600_dma_disable(void *dma_chip); 173 174 void x1600_dma_enable(void *dma_chip); 175 176 void *x1600_dma_get_channel(void *dma, uint8_t channel, l4_cap_idx_t irq); 177 178 unsigned int x1600_dma_transfer(void *dma_channel, 179 uint32_t source, uint32_t destination, 180 unsigned int count, 181 int source_increment, int destination_increment, 182 uint8_t source_width, uint8_t destination_width, 183 uint8_t transfer_unit_size, 184 enum Dma_x1600_request_type type); 185 186 unsigned int x1600_dma_wait(void *dma_channel); 187 188 EXTERN_C_END