paul@214 | 1 | /* |
paul@214 | 2 | * DMA support for the JZ4780. |
paul@214 | 3 | * NOTE: This should be combined with the X1600 support. |
paul@214 | 4 | * |
paul@271 | 5 | * Copyright (C) 2021, 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@214 | 6 | * |
paul@214 | 7 | * This program is free software; you can redistribute it and/or |
paul@214 | 8 | * modify it under the terms of the GNU General Public License as |
paul@214 | 9 | * published by the Free Software Foundation; either version 2 of |
paul@214 | 10 | * the License, or (at your option) any later version. |
paul@214 | 11 | * |
paul@214 | 12 | * This program is distributed in the hope that it will be useful, |
paul@214 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@214 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@214 | 15 | * GNU General Public License for more details. |
paul@214 | 16 | * |
paul@214 | 17 | * You should have received a copy of the GNU General Public License |
paul@214 | 18 | * along with this program; if not, write to the Free Software |
paul@214 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@214 | 20 | * Boston, MA 02110-1301, USA |
paul@214 | 21 | */ |
paul@214 | 22 | |
paul@214 | 23 | #include <l4/devices/dma-jz4780.h> |
paul@214 | 24 | #include <l4/devices/hw_mmio_register_block.h> |
paul@214 | 25 | |
paul@223 | 26 | #include <l4/sys/cache.h> |
paul@214 | 27 | #include <l4/sys/ipc.h> |
paul@214 | 28 | #include <l4/sys/irq.h> |
paul@268 | 29 | #include <l4/sys/rcv_endpoint.h> |
paul@214 | 30 | #include <l4/util/util.h> |
paul@214 | 31 | |
paul@271 | 32 | #include <systypes/thread.h> |
paul@268 | 33 | |
paul@214 | 34 | #include <stdio.h> |
paul@214 | 35 | |
paul@214 | 36 | |
paul@214 | 37 | |
paul@214 | 38 | enum Global_regs |
paul@214 | 39 | { |
paul@214 | 40 | Dma_control = 0x1000, // DMAC |
paul@214 | 41 | Dma_irq_pending = 0x1004, // DIRQP |
paul@223 | 42 | Dma_doorbell = 0x1008, // DDB |
paul@223 | 43 | Dma_doorbell_set = 0x100c, // DDS |
paul@223 | 44 | Dma_channel_programmable = 0x101c, // DMACP |
paul@223 | 45 | Dma_soft_irq_pending = 0x1020, // DSIRQP |
paul@223 | 46 | Dma_soft_irq_mask = 0x1024, // DSIRQM |
paul@223 | 47 | Dma_mcu_irq_pending = 0x1028, // DCIRQP |
paul@223 | 48 | Dma_mcu_irq_mask = 0x102c, // DCIRQM |
paul@214 | 49 | }; |
paul@214 | 50 | |
paul@214 | 51 | enum Channel_regs |
paul@214 | 52 | { |
paul@214 | 53 | Dma_source = 0x00, // DSA |
paul@214 | 54 | Dma_destination = 0x04, // DTA |
paul@214 | 55 | Dma_transfer_count = 0x08, // DTC |
paul@214 | 56 | Dma_request_source = 0x0c, // DRT |
paul@214 | 57 | Dma_control_status = 0x10, // DCS |
paul@214 | 58 | Dma_command = 0x14, // DCM |
paul@214 | 59 | Dma_descriptor_address = 0x18, // DDA |
paul@214 | 60 | Dma_stride = 0x1c, // DSD |
paul@214 | 61 | }; |
paul@214 | 62 | |
paul@214 | 63 | enum Dma_control_bits : unsigned |
paul@214 | 64 | { |
paul@214 | 65 | Dma_fast_msc_transfer = 0x80000000, // FMSC |
paul@214 | 66 | Dma_fast_ssi_transfer = 0x40000000, // FSSI |
paul@214 | 67 | Dma_fast_tssi_transfer = 0x20000000, // FTSSI |
paul@214 | 68 | Dma_fast_uart_transfer = 0x10000000, // FUART |
paul@214 | 69 | Dma_fast_aic_transfer = 0x08000000, // FAIC |
paul@214 | 70 | |
paul@214 | 71 | Dma_intc_irq_channel_mask = 0x003e0000, // INTCC |
paul@214 | 72 | Dma_intc_irq_channel_bind = 0x00010000, // INTCE |
paul@214 | 73 | |
paul@214 | 74 | Dma_control_trans_halted = 0x00000008, // HLT |
paul@214 | 75 | Dma_control_address_error = 0x00000004, // AR |
paul@214 | 76 | Dma_control_special_ch01 = 0x00000002, // CH01 |
paul@214 | 77 | Dma_control_enable = 0x00000001, // DMAE |
paul@214 | 78 | }; |
paul@214 | 79 | |
paul@214 | 80 | enum Dma_transfer_count_bits : unsigned |
paul@214 | 81 | { |
paul@223 | 82 | Dma_descriptor_offset_mask = 0xff000000, // DOA (in DES3) |
paul@214 | 83 | Dma_transfer_count_mask = 0x00ffffff, |
paul@223 | 84 | |
paul@223 | 85 | Dma_descriptor_offset_shift = 24, |
paul@214 | 86 | }; |
paul@214 | 87 | |
paul@214 | 88 | enum Dma_request_source_bits : unsigned |
paul@214 | 89 | { |
paul@214 | 90 | Dma_request_type_mask = 0x0000003f, |
paul@214 | 91 | }; |
paul@214 | 92 | |
paul@214 | 93 | enum Dma_control_status_bits : unsigned |
paul@214 | 94 | { |
paul@214 | 95 | Dma_no_descriptor_transfer = 0x80000000, |
paul@214 | 96 | Dma_8word_descriptor = 0x40000000, |
paul@223 | 97 | Dma_4word_descriptor = 0x00000000, |
paul@214 | 98 | Dma_copy_offset_mask = 0x0000ff00, |
paul@214 | 99 | Dma_address_error = 0x00000010, |
paul@214 | 100 | Dma_trans_completed = 0x00000008, |
paul@214 | 101 | Dma_trans_halted = 0x00000004, |
paul@214 | 102 | Dma_channel_enable = 0x00000001, |
paul@214 | 103 | |
paul@214 | 104 | Dma_copy_offset_shift = 8, |
paul@214 | 105 | }; |
paul@214 | 106 | |
paul@214 | 107 | enum Dma_command_bits : unsigned |
paul@214 | 108 | { |
paul@214 | 109 | Dma_special_source_mask = 0x0c000000, |
paul@214 | 110 | Dma_special_source_tcsm = 0x00000000, |
paul@214 | 111 | Dma_special_source_bch_nemc = 0x04000000, |
paul@214 | 112 | Dma_special_source_reserved_ddr = 0x08000000, |
paul@214 | 113 | |
paul@214 | 114 | Dma_special_destination_mask = 0x03000000, |
paul@214 | 115 | Dma_special_destination_tcsm = 0x00000000, |
paul@214 | 116 | Dma_special_destination_bch_nemc = 0x01000000, |
paul@214 | 117 | Dma_special_destination_reserved_ddr = 0x02000000, |
paul@214 | 118 | |
paul@214 | 119 | Dma_source_address_increment = 0x00800000, |
paul@214 | 120 | Dma_source_address_no_increment = 0x00000000, |
paul@214 | 121 | Dma_destination_address_increment = 0x00400000, |
paul@214 | 122 | Dma_destination_address_no_increment = 0x00000000, |
paul@214 | 123 | |
paul@214 | 124 | Dma_recommended_data_unit_size_mask = 0x000f0000, |
paul@214 | 125 | Dma_source_port_width_mask = 0x0000c000, |
paul@214 | 126 | Dma_destination_port_width_mask = 0x00003000, |
paul@214 | 127 | Dma_transfer_unit_size_mask = 0x00000f00, |
paul@214 | 128 | |
paul@214 | 129 | Dma_trans_unit_size_32_bit = 0x00000000, |
paul@214 | 130 | Dma_trans_unit_size_8_bit = 0x00000100, |
paul@214 | 131 | Dma_trans_unit_size_16_bit = 0x00000200, |
paul@214 | 132 | Dma_trans_unit_size_16_byte = 0x00000300, |
paul@214 | 133 | Dma_trans_unit_size_32_byte = 0x00000400, |
paul@214 | 134 | Dma_trans_unit_size_64_byte = 0x00000500, |
paul@214 | 135 | Dma_trans_unit_size_128_byte = 0x00000600, |
paul@214 | 136 | Dma_trans_unit_size_autonomous = 0x00000700, |
paul@214 | 137 | |
paul@214 | 138 | Dma_stride_enable = 0x00000004, |
paul@214 | 139 | Dma_transfer_irq_enable = 0x00000002, |
paul@214 | 140 | Dma_descriptor_link_enable = 0x00000001, |
paul@214 | 141 | |
paul@214 | 142 | Dma_recommended_data_unit_size_shift = 16, |
paul@214 | 143 | Dma_source_port_width_shift = 14, |
paul@214 | 144 | Dma_destination_port_width_shift = 12, |
paul@214 | 145 | Dma_transfer_unit_size_shift = 8, |
paul@214 | 146 | }; |
paul@214 | 147 | |
paul@214 | 148 | enum Dma_port_width_values : unsigned |
paul@214 | 149 | { |
paul@214 | 150 | Dma_port_width_32_bit = 0, |
paul@214 | 151 | Dma_port_width_8_bit = 1, |
paul@214 | 152 | Dma_port_width_16_bit = 2, |
paul@214 | 153 | }; |
paul@214 | 154 | |
paul@214 | 155 | |
paul@214 | 156 | |
paul@214 | 157 | // Initialise a channel. |
paul@214 | 158 | |
paul@214 | 159 | Dma_jz4780_channel::Dma_jz4780_channel(Dma_jz4780_chip *chip, uint8_t channel, |
paul@214 | 160 | l4_addr_t start, l4_cap_idx_t irq) |
paul@214 | 161 | : _chip(chip), _channel(channel), _irq(irq) |
paul@214 | 162 | { |
paul@214 | 163 | _regs = new Hw::Mmio_register_block<32>(start); |
paul@214 | 164 | |
paul@214 | 165 | // Initialise the transfer count. |
paul@214 | 166 | |
paul@214 | 167 | _regs[Dma_transfer_count] = 0; |
paul@214 | 168 | } |
paul@214 | 169 | |
paul@214 | 170 | // Return the closest interval length greater than or equal to the number of |
paul@214 | 171 | // units given encoded in the request detection interval length field of the |
paul@214 | 172 | // control/status register. |
paul@214 | 173 | |
paul@214 | 174 | uint32_t |
paul@214 | 175 | Dma_jz4780_channel::encode_req_detect_int_length(uint8_t units) |
paul@214 | 176 | { |
paul@214 | 177 | static uint8_t lengths[] = {0, 1, 2, 3, 4, 8, 16, 32, 64, 128}; |
paul@214 | 178 | int i; |
paul@214 | 179 | |
paul@214 | 180 | if (!units) |
paul@214 | 181 | return 0; |
paul@214 | 182 | |
paul@214 | 183 | for (i = 0; i <= 9; i++) |
paul@214 | 184 | { |
paul@214 | 185 | if (lengths[i] >= units) |
paul@214 | 186 | break; |
paul@214 | 187 | } |
paul@214 | 188 | |
paul@214 | 189 | return i << Dma_recommended_data_unit_size_shift; |
paul@214 | 190 | } |
paul@214 | 191 | |
paul@214 | 192 | // Encode the appropriate source port width for the given request type. |
paul@214 | 193 | |
paul@214 | 194 | uint32_t |
paul@214 | 195 | Dma_jz4780_channel::encode_source_port_width(uint8_t width) |
paul@214 | 196 | { |
paul@214 | 197 | switch (width) |
paul@214 | 198 | { |
paul@214 | 199 | case 1: |
paul@214 | 200 | return Dma_port_width_8_bit << Dma_source_port_width_shift; |
paul@214 | 201 | |
paul@214 | 202 | case 2: |
paul@214 | 203 | return Dma_port_width_16_bit << Dma_source_port_width_shift; |
paul@214 | 204 | |
paul@214 | 205 | default: |
paul@214 | 206 | return Dma_port_width_32_bit << Dma_source_port_width_shift; |
paul@214 | 207 | } |
paul@214 | 208 | } |
paul@214 | 209 | |
paul@214 | 210 | // Encode the appropriate destination port width for the given request type. |
paul@214 | 211 | |
paul@214 | 212 | uint32_t |
paul@214 | 213 | Dma_jz4780_channel::encode_destination_port_width(uint8_t width) |
paul@214 | 214 | { |
paul@214 | 215 | switch (width) |
paul@214 | 216 | { |
paul@214 | 217 | case 1: |
paul@214 | 218 | return Dma_port_width_8_bit << Dma_destination_port_width_shift; |
paul@214 | 219 | |
paul@214 | 220 | case 2: |
paul@214 | 221 | return Dma_port_width_16_bit << Dma_destination_port_width_shift; |
paul@214 | 222 | |
paul@214 | 223 | default: |
paul@214 | 224 | return Dma_port_width_32_bit << Dma_destination_port_width_shift; |
paul@214 | 225 | } |
paul@214 | 226 | } |
paul@214 | 227 | |
paul@214 | 228 | // Encode the transfer unit size. |
paul@214 | 229 | // NOTE: This does not handle the external case. |
paul@214 | 230 | |
paul@214 | 231 | uint32_t |
paul@214 | 232 | Dma_jz4780_channel::encode_transfer_unit_size(uint8_t size) |
paul@214 | 233 | { |
paul@214 | 234 | switch (size) |
paul@214 | 235 | { |
paul@214 | 236 | case 0: |
paul@214 | 237 | return Dma_trans_unit_size_autonomous; |
paul@214 | 238 | |
paul@214 | 239 | case 1: |
paul@214 | 240 | return Dma_trans_unit_size_8_bit; |
paul@214 | 241 | |
paul@214 | 242 | case 2: |
paul@214 | 243 | return Dma_trans_unit_size_16_bit; |
paul@214 | 244 | |
paul@214 | 245 | case 16: |
paul@214 | 246 | return Dma_trans_unit_size_16_byte; |
paul@214 | 247 | |
paul@214 | 248 | case 32: |
paul@214 | 249 | return Dma_trans_unit_size_32_byte; |
paul@214 | 250 | |
paul@214 | 251 | case 64: |
paul@214 | 252 | return Dma_trans_unit_size_64_byte; |
paul@214 | 253 | |
paul@214 | 254 | case 128: |
paul@214 | 255 | return Dma_trans_unit_size_128_byte; |
paul@214 | 256 | |
paul@214 | 257 | default: |
paul@214 | 258 | return Dma_trans_unit_size_32_bit; |
paul@214 | 259 | } |
paul@214 | 260 | } |
paul@214 | 261 | |
paul@214 | 262 | // Transfer data between memory locations. |
paul@214 | 263 | |
paul@214 | 264 | unsigned int |
paul@214 | 265 | Dma_jz4780_channel::transfer(uint32_t source, uint32_t destination, |
paul@214 | 266 | unsigned int count, |
paul@214 | 267 | bool source_increment, bool destination_increment, |
paul@214 | 268 | uint8_t source_width, uint8_t destination_width, |
paul@214 | 269 | uint8_t transfer_unit_size, |
paul@223 | 270 | enum Dma_jz4780_request_type type, |
paul@223 | 271 | l4_addr_t desc_vaddr, |
paul@223 | 272 | l4re_dma_space_dma_addr_t desc_paddr) |
paul@214 | 273 | { |
paul@214 | 274 | printf("transfer:%s%s%s%s\n", error() ? " error" : "", |
paul@214 | 275 | halted() ? " halted" : "", |
paul@214 | 276 | completed() ? " completed" : "", |
paul@214 | 277 | _regs[Dma_transfer_count] ? " count" : ""); |
paul@214 | 278 | |
paul@214 | 279 | // Ensure an absence of address error and halt conditions globally and in this channel. |
paul@214 | 280 | |
paul@214 | 281 | if (error() || halted()) |
paul@214 | 282 | return 0; |
paul@214 | 283 | |
paul@260 | 284 | // Ensure a zero transfer count for this channel. |
paul@214 | 285 | |
paul@260 | 286 | if (_regs[Dma_transfer_count]) |
paul@214 | 287 | return 0; |
paul@214 | 288 | |
paul@214 | 289 | // Disable the channel. |
paul@214 | 290 | |
paul@214 | 291 | _regs[Dma_control_status] = _regs[Dma_control_status] & ~Dma_channel_enable; |
paul@214 | 292 | |
paul@214 | 293 | // Set transfer count to the number of units. |
paul@214 | 294 | |
paul@214 | 295 | unsigned int units = count < Dma_transfer_count_mask ? count : Dma_transfer_count_mask; |
paul@214 | 296 | |
paul@223 | 297 | // NOTE: Request detection interval length (for autonomous mode) not considered. |
paul@214 | 298 | |
paul@223 | 299 | uint32_t command = (source_increment ? Dma_source_address_increment : |
paul@223 | 300 | Dma_source_address_no_increment) | |
paul@223 | 301 | (destination_increment ? Dma_destination_address_increment : |
paul@223 | 302 | Dma_destination_address_no_increment) | |
paul@223 | 303 | encode_source_port_width(source_width) | |
paul@223 | 304 | encode_destination_port_width(destination_width) | |
paul@223 | 305 | encode_transfer_unit_size(transfer_unit_size) | |
paul@223 | 306 | Dma_transfer_irq_enable; |
paul@214 | 307 | |
paul@223 | 308 | // Populate the descriptor, largely corresponding to the population of |
paul@223 | 309 | // registers when descriptors are not being used. |
paul@214 | 310 | |
paul@223 | 311 | if (desc_vaddr) |
paul@223 | 312 | { |
paul@223 | 313 | struct jz4780_dma_descriptor *desc = (struct jz4780_dma_descriptor *) desc_vaddr; |
paul@223 | 314 | |
paul@223 | 315 | // NOTE: Linking to the same descriptor. |
paul@223 | 316 | |
paul@223 | 317 | uint32_t descriptor_offset = 0; |
paul@214 | 318 | |
paul@223 | 319 | desc->command = command | Dma_descriptor_link_enable; |
paul@223 | 320 | desc->source = source; |
paul@223 | 321 | desc->destination = destination; |
paul@223 | 322 | desc->transfer_count = (units & Dma_transfer_count_mask) | |
paul@223 | 323 | (descriptor_offset << Dma_descriptor_offset_shift); |
paul@223 | 324 | desc->request_source = type; |
paul@223 | 325 | |
paul@223 | 326 | // NOTE: Stride not supported yet. |
paul@223 | 327 | |
paul@223 | 328 | l4_cache_clean_data((unsigned long) desc_vaddr, |
paul@223 | 329 | (unsigned long) desc_vaddr + sizeof(*desc)); |
paul@223 | 330 | |
paul@223 | 331 | // Commit the descriptor. |
paul@214 | 332 | |
paul@223 | 333 | _regs[Dma_descriptor_address] = desc_paddr; |
paul@223 | 334 | _chip->commit_descriptor(_channel); |
paul@223 | 335 | } |
paul@223 | 336 | |
paul@223 | 337 | // Otherwise, populate the registers for a one-off transfer. |
paul@214 | 338 | |
paul@223 | 339 | else |
paul@223 | 340 | { |
paul@223 | 341 | _regs[Dma_command] = command; |
paul@223 | 342 | _regs[Dma_source] = source; |
paul@223 | 343 | _regs[Dma_destination] = destination; |
paul@223 | 344 | _regs[Dma_transfer_count] = units & Dma_transfer_count_mask; |
paul@223 | 345 | _regs[Dma_request_source] = type; |
paul@223 | 346 | } |
paul@214 | 347 | |
paul@223 | 348 | // Enable the channel with descriptor transfer configured if appropriate. |
paul@214 | 349 | |
paul@223 | 350 | _regs[Dma_control_status] = (desc_vaddr ? Dma_8word_descriptor : |
paul@223 | 351 | Dma_no_descriptor_transfer) | |
paul@214 | 352 | Dma_channel_enable; |
paul@214 | 353 | |
paul@214 | 354 | // Return the number of units to transfer. |
paul@214 | 355 | |
paul@214 | 356 | return units; |
paul@214 | 357 | } |
paul@214 | 358 | |
paul@214 | 359 | unsigned int |
paul@214 | 360 | Dma_jz4780_channel::wait() |
paul@214 | 361 | { |
paul@214 | 362 | // An interrupt will occur upon completion, the completion flag will be set |
paul@214 | 363 | // and the transfer count will be zero. |
paul@214 | 364 | |
paul@214 | 365 | unsigned int remaining = 0; |
paul@214 | 366 | |
paul@214 | 367 | do |
paul@214 | 368 | { |
paul@214 | 369 | if (!wait_for_irq(1000000)) |
paul@214 | 370 | printf("status = %x\n", (uint32_t) _regs[Dma_control_status]); |
paul@214 | 371 | else |
paul@214 | 372 | { |
paul@214 | 373 | printf("status = %x\n", (uint32_t) _regs[Dma_control_status]); |
paul@214 | 374 | remaining = _regs[Dma_transfer_count]; |
paul@214 | 375 | ack_irq(); |
paul@214 | 376 | break; |
paul@214 | 377 | } |
paul@214 | 378 | } |
paul@214 | 379 | while (!error() && !halted() && !completed()); |
paul@214 | 380 | |
paul@214 | 381 | // Reset the channel status. |
paul@214 | 382 | |
paul@214 | 383 | _regs[Dma_control_status] = _regs[Dma_control_status] & ~(Dma_channel_enable | |
paul@214 | 384 | Dma_trans_completed | Dma_address_error | |
paul@214 | 385 | Dma_trans_halted); |
paul@214 | 386 | _regs[Dma_transfer_count] = 0; |
paul@214 | 387 | |
paul@214 | 388 | // Return the number of remaining units. |
paul@214 | 389 | |
paul@214 | 390 | return remaining; |
paul@214 | 391 | } |
paul@214 | 392 | |
paul@214 | 393 | // Wait indefinitely for an interrupt request, returning true if one was delivered. |
paul@214 | 394 | |
paul@214 | 395 | bool |
paul@214 | 396 | Dma_jz4780_channel::wait_for_irq() |
paul@214 | 397 | { |
paul@225 | 398 | if (l4_is_valid_cap(_irq)) |
paul@268 | 399 | { |
paul@271 | 400 | if (l4_error(l4_rcv_ep_bind_thread(_irq, get_current_thread(), 0))) |
paul@268 | 401 | return false; |
paul@268 | 402 | |
paul@225 | 403 | return !l4_error(l4_irq_receive(_irq, L4_IPC_NEVER)) && _chip->have_interrupt(_channel); |
paul@268 | 404 | } |
paul@225 | 405 | else |
paul@225 | 406 | return true; |
paul@214 | 407 | } |
paul@214 | 408 | |
paul@214 | 409 | // Wait up to the given timeout (in microseconds) for an interrupt request, |
paul@214 | 410 | // returning true if one was delivered. |
paul@214 | 411 | |
paul@214 | 412 | bool |
paul@214 | 413 | Dma_jz4780_channel::wait_for_irq(unsigned int timeout) |
paul@214 | 414 | { |
paul@225 | 415 | if (l4_is_valid_cap(_irq)) |
paul@268 | 416 | { |
paul@271 | 417 | if (l4_error(l4_rcv_ep_bind_thread(_irq, get_current_thread(), 0))) |
paul@268 | 418 | return false; |
paul@268 | 419 | |
paul@225 | 420 | return !l4_error(l4_irq_receive(_irq, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4util_micros2l4to(timeout)))) && _chip->have_interrupt(_channel); |
paul@268 | 421 | } |
paul@225 | 422 | else |
paul@225 | 423 | return true; |
paul@214 | 424 | } |
paul@214 | 425 | |
paul@214 | 426 | // Acknowledge an interrupt condition. |
paul@214 | 427 | |
paul@214 | 428 | void |
paul@214 | 429 | Dma_jz4780_channel::ack_irq() |
paul@214 | 430 | { |
paul@214 | 431 | _chip->ack_irq(_channel); |
paul@214 | 432 | } |
paul@214 | 433 | |
paul@214 | 434 | // Return whether a transfer has completed. |
paul@214 | 435 | |
paul@214 | 436 | bool |
paul@214 | 437 | Dma_jz4780_channel::completed() |
paul@214 | 438 | { |
paul@214 | 439 | return _regs[Dma_control_status] & Dma_trans_completed ? true : false; |
paul@214 | 440 | } |
paul@214 | 441 | |
paul@214 | 442 | // Return whether an address error condition has arisen. |
paul@214 | 443 | |
paul@214 | 444 | bool |
paul@214 | 445 | Dma_jz4780_channel::error() |
paul@214 | 446 | { |
paul@214 | 447 | return _chip->error() || (_regs[Dma_control_status] & Dma_address_error ? true : false); |
paul@214 | 448 | } |
paul@214 | 449 | |
paul@214 | 450 | // Return whether a transfer has halted. |
paul@214 | 451 | |
paul@214 | 452 | bool |
paul@214 | 453 | Dma_jz4780_channel::halted() |
paul@214 | 454 | { |
paul@214 | 455 | return _chip->halted() || (_regs[Dma_control_status] & Dma_trans_halted ? true : false); |
paul@214 | 456 | } |
paul@214 | 457 | |
paul@214 | 458 | |
paul@214 | 459 | |
paul@214 | 460 | // Initialise the I2C controller. |
paul@214 | 461 | |
paul@214 | 462 | Dma_jz4780_chip::Dma_jz4780_chip(l4_addr_t start, l4_addr_t end, |
paul@214 | 463 | Cpm_jz4780_chip *cpm) |
paul@214 | 464 | : _start(start), _end(end), _cpm(cpm) |
paul@214 | 465 | { |
paul@214 | 466 | _regs = new Hw::Mmio_register_block<32>(start); |
paul@214 | 467 | } |
paul@214 | 468 | |
paul@214 | 469 | // Enable the peripheral. |
paul@214 | 470 | |
paul@214 | 471 | void |
paul@214 | 472 | Dma_jz4780_chip::enable() |
paul@214 | 473 | { |
paul@214 | 474 | // Make sure that the DMA clock is available. |
paul@214 | 475 | |
paul@214 | 476 | _cpm->start_clock(Clock_dma); |
paul@214 | 477 | |
paul@214 | 478 | _regs[Dma_control] = Dma_control_enable; |
paul@214 | 479 | while (!(_regs[Dma_control] & Dma_control_enable)); |
paul@214 | 480 | } |
paul@214 | 481 | |
paul@214 | 482 | // Disable the channel. |
paul@214 | 483 | |
paul@214 | 484 | void |
paul@214 | 485 | Dma_jz4780_chip::disable() |
paul@214 | 486 | { |
paul@214 | 487 | _regs[Dma_control] = 0; |
paul@214 | 488 | while (_regs[Dma_control] & Dma_control_enable); |
paul@214 | 489 | } |
paul@214 | 490 | |
paul@214 | 491 | // Obtain a channel object. |
paul@214 | 492 | |
paul@214 | 493 | Dma_jz4780_channel * |
paul@214 | 494 | Dma_jz4780_chip::get_channel(uint8_t channel, l4_cap_idx_t irq) |
paul@214 | 495 | { |
paul@214 | 496 | if (channel < 32) |
paul@214 | 497 | return new Dma_jz4780_channel(this, channel, _start + 0x20 * channel, irq); |
paul@214 | 498 | else |
paul@214 | 499 | throw -L4_EINVAL; |
paul@214 | 500 | } |
paul@214 | 501 | |
paul@214 | 502 | // Return whether an interrupt is pending on the given channel. |
paul@214 | 503 | |
paul@214 | 504 | bool |
paul@214 | 505 | Dma_jz4780_chip::have_interrupt(uint8_t channel) |
paul@214 | 506 | { |
paul@214 | 507 | return _regs[Dma_irq_pending] & (1 << channel) ? true : false; |
paul@214 | 508 | } |
paul@214 | 509 | |
paul@214 | 510 | // Acknowledge an interrupt condition on the given channel. |
paul@214 | 511 | |
paul@214 | 512 | void |
paul@214 | 513 | Dma_jz4780_chip::ack_irq(uint8_t channel) |
paul@214 | 514 | { |
paul@214 | 515 | _regs[Dma_irq_pending] = _regs[Dma_irq_pending] & ~(1 << channel); |
paul@214 | 516 | } |
paul@214 | 517 | |
paul@214 | 518 | // Return whether an address error condition has arisen. |
paul@214 | 519 | |
paul@214 | 520 | bool |
paul@214 | 521 | Dma_jz4780_chip::error() |
paul@214 | 522 | { |
paul@214 | 523 | return _regs[Dma_control] & Dma_control_address_error ? true : false; |
paul@214 | 524 | } |
paul@214 | 525 | |
paul@214 | 526 | // Return whether a transfer has halted. |
paul@214 | 527 | |
paul@214 | 528 | bool |
paul@214 | 529 | Dma_jz4780_chip::halted() |
paul@214 | 530 | { |
paul@214 | 531 | return _regs[Dma_control] & Dma_control_trans_halted ? true : false; |
paul@214 | 532 | } |
paul@214 | 533 | |
paul@223 | 534 | void |
paul@223 | 535 | Dma_jz4780_chip::commit_descriptor(uint8_t channel) |
paul@223 | 536 | { |
paul@223 | 537 | _regs[Dma_doorbell_set] = (1 << channel); |
paul@223 | 538 | } |
paul@223 | 539 | |
paul@214 | 540 | |
paul@214 | 541 | |
paul@214 | 542 | // C language interface functions. |
paul@214 | 543 | |
paul@214 | 544 | void *jz4780_dma_init(l4_addr_t start, l4_addr_t end, void *cpm) |
paul@214 | 545 | { |
paul@214 | 546 | return (void *) new Dma_jz4780_chip(start, end, static_cast<Cpm_jz4780_chip *>(cpm)); |
paul@214 | 547 | } |
paul@214 | 548 | |
paul@214 | 549 | void jz4780_dma_disable(void *dma_chip) |
paul@214 | 550 | { |
paul@214 | 551 | static_cast<Dma_jz4780_chip *>(dma_chip)->disable(); |
paul@214 | 552 | } |
paul@214 | 553 | |
paul@214 | 554 | void jz4780_dma_enable(void *dma_chip) |
paul@214 | 555 | { |
paul@214 | 556 | static_cast<Dma_jz4780_chip *>(dma_chip)->enable(); |
paul@214 | 557 | } |
paul@214 | 558 | |
paul@214 | 559 | void *jz4780_dma_get_channel(void *dma, uint8_t channel, l4_cap_idx_t irq) |
paul@214 | 560 | { |
paul@214 | 561 | return static_cast<Dma_jz4780_chip *>(dma)->get_channel(channel, irq); |
paul@214 | 562 | } |
paul@214 | 563 | |
paul@214 | 564 | unsigned int jz4780_dma_transfer(void *dma_channel, |
paul@223 | 565 | uint32_t source, uint32_t destination, |
paul@223 | 566 | unsigned int count, |
paul@223 | 567 | int source_increment, int destination_increment, |
paul@223 | 568 | uint8_t source_width, uint8_t destination_width, |
paul@223 | 569 | uint8_t transfer_unit_size, |
paul@223 | 570 | enum Dma_jz4780_request_type type) |
paul@214 | 571 | { |
paul@214 | 572 | return static_cast<Dma_jz4780_channel *>(dma_channel)->transfer(source, |
paul@214 | 573 | destination, count, source_increment, destination_increment, source_width, |
paul@214 | 574 | destination_width, transfer_unit_size, type); |
paul@214 | 575 | } |
paul@214 | 576 | |
paul@223 | 577 | unsigned int jz4780_dma_transfer_descriptor(void *dma_channel, |
paul@223 | 578 | uint32_t source, uint32_t destination, |
paul@223 | 579 | unsigned int count, |
paul@223 | 580 | int source_increment, int destination_increment, |
paul@223 | 581 | uint8_t source_width, uint8_t destination_width, |
paul@223 | 582 | uint8_t transfer_unit_size, |
paul@223 | 583 | enum Dma_jz4780_request_type type, |
paul@223 | 584 | l4_addr_t desc_vaddr, |
paul@223 | 585 | l4re_dma_space_dma_addr_t desc_paddr) |
paul@223 | 586 | { |
paul@223 | 587 | return static_cast<Dma_jz4780_channel *>(dma_channel)->transfer(source, |
paul@223 | 588 | destination, count, source_increment, destination_increment, source_width, |
paul@223 | 589 | destination_width, transfer_unit_size, type, desc_vaddr, desc_paddr); |
paul@223 | 590 | } |
paul@223 | 591 | |
paul@214 | 592 | unsigned int jz4780_dma_wait(void *dma_channel) |
paul@214 | 593 | { |
paul@214 | 594 | return static_cast<Dma_jz4780_channel *>(dma_channel)->wait(); |
paul@214 | 595 | } |