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 = 10, 37 Dma_request_can1_in = 11, 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_msc0_out = 52, 55 Dma_request_msc0_in = 53, 56 Dma_request_msc1_out = 54, 57 Dma_request_msc1_in = 55, 58 Dma_request_sadc_in = 56, 59 Dma_request_aic_loop_out = 61, 60 Dma_request_aic_out = 62, 61 Dma_request_aic_in = 63, 62 }; 63 64 65 66 #ifdef __cplusplus 67 68 #include <l4/devices/cpm-x1600.h> 69 #include <l4/devices/hw_mmio_register_block.h> 70 71 // Forward declaration. 72 73 class Dma_x1600_chip; 74 75 76 77 // DMA channel. 78 79 class Dma_x1600_channel 80 { 81 private: 82 Hw::Register_block<32> _regs; 83 Dma_x1600_chip *_chip; 84 uint8_t _channel; 85 l4_cap_idx_t _irq = L4_INVALID_CAP; 86 87 public: 88 Dma_x1600_channel(Dma_x1600_chip *chip, uint8_t channel, l4_addr_t start, l4_cap_idx_t irq); 89 90 unsigned int transfer(uint32_t source, uint32_t destination, 91 unsigned int count, 92 bool source_increment, bool destination_increment, 93 uint8_t source_width, uint8_t destination_width, 94 uint8_t transfer_unit_size, 95 enum Dma_x1600_request_type type=Dma_request_auto); 96 97 unsigned int wait(); 98 99 protected: 100 // Transfer property configuration. 101 102 uint32_t encode_req_detect_int_length(uint8_t units); 103 104 uint32_t encode_source_port_width(uint8_t width); 105 106 uint32_t encode_destination_port_width(uint8_t width); 107 108 uint32_t encode_transfer_unit_size(uint8_t size); 109 110 // Transaction control. 111 112 void ack_irq(); 113 114 bool completed(); 115 116 bool error(); 117 118 bool halted(); 119 120 bool wait_for_irq(); 121 122 bool wait_for_irq(unsigned int timeout); 123 }; 124 125 // DMA device control. 126 127 class Dma_x1600_chip 128 { 129 private: 130 Hw::Register_block<32> _regs; 131 l4_addr_t _start, _end; 132 Cpm_x1600_chip *_cpm; 133 134 public: 135 Dma_x1600_chip(l4_addr_t start, l4_addr_t end, Cpm_x1600_chip *cpm); 136 137 void disable(); 138 139 void enable(); 140 141 Dma_x1600_channel *get_channel(uint8_t channel, l4_cap_idx_t irq); 142 143 // Transaction control. 144 145 void ack_irq(uint8_t channel); 146 147 bool error(); 148 149 bool halted(); 150 151 bool have_interrupt(uint8_t channel); 152 }; 153 154 #endif /* __cplusplus */ 155 156 157 158 /* C language interface. */ 159 160 EXTERN_C_BEGIN 161 162 void *x1600_dma_init(l4_addr_t start, l4_addr_t end, void *cpm); 163 164 void x1600_dma_disable(void *dma_chip); 165 166 void x1600_dma_enable(void *dma_chip); 167 168 void *x1600_dma_get_channel(void *dma, uint8_t channel, l4_cap_idx_t irq); 169 170 unsigned int x1600_dma_transfer(void *dma_channel, 171 uint32_t source, uint32_t destination, 172 unsigned int count, 173 int source_increment, int destination_increment, 174 uint8_t source_width, uint8_t destination_width, 175 uint8_t transfer_unit_size, 176 enum Dma_x1600_request_type type); 177 178 unsigned int x1600_dma_wait(void *dma_channel); 179 180 EXTERN_C_END