1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright 2014-2015 Freescale 3 // Copyright 2018 NXP 4 5 /* 6 * Driver for NXP Layerscape Queue Direct Memory Access Controller 7 * 8 * Author: 9 * Wen He <wen.he_1@nxp.com> 10 * Jiaheng Fan <jiaheng.fan@nxp.com> 11 * 12 */ 13 14 #include <linux/module.h> 15 #include <linux/delay.h> 16 #include <linux/of.h> 17 #include <linux/of_dma.h> 18 #include <linux/dma-mapping.h> 19 #include <linux/platform_device.h> 20 21 #include "virt-dma.h" 22 #include "fsldma.h" 23 24 /* Register related definition */ 25 #define FSL_QDMA_DMR 0x0 26 #define FSL_QDMA_DSR 0x4 27 #define FSL_QDMA_DEIER 0xe00 28 #define FSL_QDMA_DEDR 0xe04 29 #define FSL_QDMA_DECFDW0R 0xe10 30 #define FSL_QDMA_DECFDW1R 0xe14 31 #define FSL_QDMA_DECFDW2R 0xe18 32 #define FSL_QDMA_DECFDW3R 0xe1c 33 #define FSL_QDMA_DECFQIDR 0xe30 34 #define FSL_QDMA_DECBR 0xe34 35 36 #define FSL_QDMA_BCQMR(x) (0xc0 + 0x100 * (x)) 37 #define FSL_QDMA_BCQSR(x) (0xc4 + 0x100 * (x)) 38 #define FSL_QDMA_BCQEDPA_SADDR(x) (0xc8 + 0x100 * (x)) 39 #define FSL_QDMA_BCQDPA_SADDR(x) (0xcc + 0x100 * (x)) 40 #define FSL_QDMA_BCQEEPA_SADDR(x) (0xd0 + 0x100 * (x)) 41 #define FSL_QDMA_BCQEPA_SADDR(x) (0xd4 + 0x100 * (x)) 42 #define FSL_QDMA_BCQIER(x) (0xe0 + 0x100 * (x)) 43 #define FSL_QDMA_BCQIDR(x) (0xe4 + 0x100 * (x)) 44 45 #define FSL_QDMA_SQDPAR 0x80c 46 #define FSL_QDMA_SQEPAR 0x814 47 #define FSL_QDMA_BSQMR 0x800 48 #define FSL_QDMA_BSQSR 0x804 49 #define FSL_QDMA_BSQICR 0x828 50 #define FSL_QDMA_CQMR 0xa00 51 #define FSL_QDMA_CQDSCR1 0xa08 52 #define FSL_QDMA_CQDSCR2 0xa0c 53 #define FSL_QDMA_CQIER 0xa10 54 #define FSL_QDMA_CQEDR 0xa14 55 #define FSL_QDMA_SQCCMR 0xa20 56 57 /* Registers for bit and genmask */ 58 #define FSL_QDMA_CQIDR_SQT BIT(15) 59 #define QDMA_CCDF_FORMAT BIT(29) 60 #define QDMA_CCDF_SER BIT(30) 61 #define QDMA_SG_FIN BIT(30) 62 #define QDMA_SG_LEN_MASK GENMASK(29, 0) 63 #define QDMA_CCDF_MASK GENMASK(28, 20) 64 65 #define FSL_QDMA_DEDR_CLEAR GENMASK(31, 0) 66 #define FSL_QDMA_BCQIDR_CLEAR GENMASK(31, 0) 67 #define FSL_QDMA_DEIER_CLEAR GENMASK(31, 0) 68 69 #define FSL_QDMA_BCQIER_CQTIE BIT(15) 70 #define FSL_QDMA_BCQIER_CQPEIE BIT(23) 71 #define FSL_QDMA_BSQICR_ICEN BIT(31) 72 73 #define FSL_QDMA_BSQICR_ICST(x) ((x) << 16) 74 #define FSL_QDMA_CQIER_MEIE BIT(31) 75 #define FSL_QDMA_CQIER_TEIE BIT(0) 76 #define FSL_QDMA_SQCCMR_ENTER_WM BIT(21) 77 78 #define FSL_QDMA_BCQMR_EN BIT(31) 79 #define FSL_QDMA_BCQMR_EI BIT(30) 80 #define FSL_QDMA_BCQMR_CD_THLD(x) ((x) << 20) 81 #define FSL_QDMA_BCQMR_CQ_SIZE(x) ((x) << 16) 82 83 #define FSL_QDMA_BCQSR_QF BIT(16) 84 #define FSL_QDMA_BCQSR_XOFF BIT(0) 85 86 #define FSL_QDMA_BSQMR_EN BIT(31) 87 #define FSL_QDMA_BSQMR_DI BIT(30) 88 #define FSL_QDMA_BSQMR_CQ_SIZE(x) ((x) << 16) 89 90 #define FSL_QDMA_BSQSR_QE BIT(17) 91 92 #define FSL_QDMA_DMR_DQD BIT(30) 93 #define FSL_QDMA_DSR_DB BIT(31) 94 95 /* Size related definition */ 96 #define FSL_QDMA_QUEUE_MAX 8 97 #define FSL_QDMA_COMMAND_BUFFER_SIZE 64 98 #define FSL_QDMA_DESCRIPTOR_BUFFER_SIZE 32 99 #define FSL_QDMA_CIRCULAR_DESC_SIZE_MIN 64 100 #define FSL_QDMA_CIRCULAR_DESC_SIZE_MAX 16384 101 #define FSL_QDMA_QUEUE_NUM_MAX 8 102 103 /* Field definition for CMD */ 104 #define FSL_QDMA_CMD_RWTTYPE 0x4 105 #define FSL_QDMA_CMD_LWC 0x2 106 #define FSL_QDMA_CMD_RWTTYPE_OFFSET 28 107 #define FSL_QDMA_CMD_NS_OFFSET 27 108 #define FSL_QDMA_CMD_DQOS_OFFSET 24 109 #define FSL_QDMA_CMD_WTHROTL_OFFSET 20 110 #define FSL_QDMA_CMD_DSEN_OFFSET 19 111 #define FSL_QDMA_CMD_LWC_OFFSET 16 112 113 /* Field definition for Descriptor status */ 114 #define QDMA_CCDF_STATUS_RTE BIT(5) 115 #define QDMA_CCDF_STATUS_WTE BIT(4) 116 #define QDMA_CCDF_STATUS_CDE BIT(2) 117 #define QDMA_CCDF_STATUS_SDE BIT(1) 118 #define QDMA_CCDF_STATUS_DDE BIT(0) 119 #define QDMA_CCDF_STATUS_MASK (QDMA_CCDF_STATUS_RTE | \ 120 QDMA_CCDF_STATUS_WTE | \ 121 QDMA_CCDF_STATUS_CDE | \ 122 QDMA_CCDF_STATUS_SDE | \ 123 QDMA_CCDF_STATUS_DDE) 124 125 /* Field definition for Descriptor offset */ 126 #define QDMA_CCDF_OFFSET 20 127 #define QDMA_SDDF_CMD(x) (((u64)(x)) << 32) 128 129 /* Field definition for safe loop count*/ 130 #define FSL_QDMA_HALT_COUNT 1500 131 #define FSL_QDMA_MAX_SIZE 16385 132 #define FSL_QDMA_COMP_TIMEOUT 1000 133 #define FSL_COMMAND_QUEUE_OVERFLLOW 10 134 135 #define FSL_QDMA_BLOCK_BASE_OFFSET(fsl_qdma_engine, x) \ 136 (((fsl_qdma_engine)->block_offset) * (x)) 137 138 /** 139 * struct fsl_qdma_format - This is the struct holding describing compound 140 * descriptor format with qDMA. 141 * @status: Command status and enqueue status notification. 142 * @cfg: Frame offset and frame format. 143 * @addr_lo: Holding the compound descriptor of the lower 144 * 32-bits address in memory 40-bit address. 145 * @addr_hi: Same as above member, but point high 8-bits in 146 * memory 40-bit address. 147 * @__reserved1: Reserved field. 148 * @cfg8b_w1: Compound descriptor command queue origin produced 149 * by qDMA and dynamic debug field. 150 * @data: Pointer to the memory 40-bit address, describes DMA 151 * source information and DMA destination information. 152 */ 153 struct fsl_qdma_format { 154 __le32 status; 155 __le32 cfg; 156 union { 157 struct { 158 __le32 addr_lo; 159 u8 addr_hi; 160 u8 __reserved1[2]; 161 u8 cfg8b_w1; 162 } __packed; 163 __le64 data; 164 }; 165 } __packed; 166 167 /* qDMA status notification pre information */ 168 struct fsl_pre_status { 169 u64 addr; 170 u8 queue; 171 }; 172 173 static DEFINE_PER_CPU(struct fsl_pre_status, pre); 174 175 struct fsl_qdma_chan { 176 struct virt_dma_chan vchan; 177 struct virt_dma_desc vdesc; 178 enum dma_status status; 179 struct fsl_qdma_engine *qdma; 180 struct fsl_qdma_queue *queue; 181 }; 182 183 struct fsl_qdma_queue { 184 struct fsl_qdma_format *virt_head; 185 struct fsl_qdma_format *virt_tail; 186 struct list_head comp_used; 187 struct list_head comp_free; 188 struct dma_pool *comp_pool; 189 struct dma_pool *desc_pool; 190 spinlock_t queue_lock; 191 dma_addr_t bus_addr; 192 u32 n_cq; 193 u32 id; 194 struct fsl_qdma_format *cq; 195 void __iomem *block_base; 196 }; 197 198 struct fsl_qdma_comp { 199 dma_addr_t bus_addr; 200 dma_addr_t desc_bus_addr; 201 struct fsl_qdma_format *virt_addr; 202 struct fsl_qdma_format *desc_virt_addr; 203 struct fsl_qdma_chan *qchan; 204 struct virt_dma_desc vdesc; 205 struct list_head list; 206 }; 207 208 struct fsl_qdma_engine { 209 struct dma_device dma_dev; 210 void __iomem *ctrl_base; 211 void __iomem *status_base; 212 void __iomem *block_base; 213 u32 n_chans; 214 u32 n_queues; 215 struct mutex fsl_qdma_mutex; 216 int error_irq; 217 int *queue_irq; 218 u32 feature; 219 struct fsl_qdma_queue *queue; 220 struct fsl_qdma_queue **status; 221 struct fsl_qdma_chan *chans; 222 int block_number; 223 int block_offset; 224 int irq_base; 225 int desc_allocated; 226 227 }; 228 229 static inline u64 230 qdma_ccdf_addr_get64(const struct fsl_qdma_format *ccdf) 231 { 232 return le64_to_cpu(ccdf->data) & (U64_MAX >> 24); 233 } 234 235 static inline void 236 qdma_desc_addr_set64(struct fsl_qdma_format *ccdf, u64 addr) 237 { 238 ccdf->addr_hi = upper_32_bits(addr); 239 ccdf->addr_lo = cpu_to_le32(lower_32_bits(addr)); 240 } 241 242 static inline u8 243 qdma_ccdf_get_queue(const struct fsl_qdma_format *ccdf) 244 { 245 return ccdf->cfg8b_w1 & U8_MAX; 246 } 247 248 static inline int 249 qdma_ccdf_get_offset(const struct fsl_qdma_format *ccdf) 250 { 251 return (le32_to_cpu(ccdf->cfg) & QDMA_CCDF_MASK) >> QDMA_CCDF_OFFSET; 252 } 253 254 static inline void 255 qdma_ccdf_set_format(struct fsl_qdma_format *ccdf, int offset) 256 { 257 ccdf->cfg = cpu_to_le32(QDMA_CCDF_FORMAT | 258 (offset << QDMA_CCDF_OFFSET)); 259 } 260 261 static inline int 262 qdma_ccdf_get_status(const struct fsl_qdma_format *ccdf) 263 { 264 return (le32_to_cpu(ccdf->status) & QDMA_CCDF_STATUS_MASK); 265 } 266 267 static inline void 268 qdma_ccdf_set_ser(struct fsl_qdma_format *ccdf, int status) 269 { 270 ccdf->status = cpu_to_le32(QDMA_CCDF_SER | status); 271 } 272 273 static inline void qdma_csgf_set_len(struct fsl_qdma_format *csgf, int len) 274 { 275 csgf->cfg = cpu_to_le32(len & QDMA_SG_LEN_MASK); 276 } 277 278 static inline void qdma_csgf_set_f(struct fsl_qdma_format *csgf, int len) 279 { 280 csgf->cfg = cpu_to_le32(QDMA_SG_FIN | (len & QDMA_SG_LEN_MASK)); 281 } 282 283 static u32 qdma_readl(struct fsl_qdma_engine *qdma, void __iomem *addr) 284 { 285 return FSL_DMA_IN(qdma, addr, 32); 286 } 287 288 static void qdma_writel(struct fsl_qdma_engine *qdma, u32 val, 289 void __iomem *addr) 290 { 291 FSL_DMA_OUT(qdma, addr, val, 32); 292 } 293 294 static struct fsl_qdma_chan *to_fsl_qdma_chan(struct dma_chan *chan) 295 { 296 return container_of(chan, struct fsl_qdma_chan, vchan.chan); 297 } 298 299 static struct fsl_qdma_comp *to_fsl_qdma_comp(struct virt_dma_desc *vd) 300 { 301 return container_of(vd, struct fsl_qdma_comp, vdesc); 302 } 303 304 static void fsl_qdma_free_chan_resources(struct dma_chan *chan) 305 { 306 struct fsl_qdma_chan *fsl_chan = to_fsl_qdma_chan(chan); 307 struct fsl_qdma_queue *fsl_queue = fsl_chan->queue; 308 struct fsl_qdma_engine *fsl_qdma = fsl_chan->qdma; 309 struct fsl_qdma_comp *comp_temp, *_comp_temp; 310 unsigned long flags; 311 LIST_HEAD(head); 312 313 spin_lock_irqsave(&fsl_chan->vchan.lock, flags); 314 vchan_get_all_descriptors(&fsl_chan->vchan, &head); 315 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags); 316 317 vchan_dma_desc_free_list(&fsl_chan->vchan, &head); 318 319 if (!fsl_queue->comp_pool && !fsl_queue->desc_pool) 320 return; 321 322 list_for_each_entry_safe(comp_temp, _comp_temp, 323 &fsl_queue->comp_used, list) { 324 dma_pool_free(fsl_queue->comp_pool, 325 comp_temp->virt_addr, 326 comp_temp->bus_addr); 327 dma_pool_free(fsl_queue->desc_pool, 328 comp_temp->desc_virt_addr, 329 comp_temp->desc_bus_addr); 330 list_del(&comp_temp->list); 331 kfree(comp_temp); 332 } 333 334 list_for_each_entry_safe(comp_temp, _comp_temp, 335 &fsl_queue->comp_free, list) { 336 dma_pool_free(fsl_queue->comp_pool, 337 comp_temp->virt_addr, 338 comp_temp->bus_addr); 339 dma_pool_free(fsl_queue->desc_pool, 340 comp_temp->desc_virt_addr, 341 comp_temp->desc_bus_addr); 342 list_del(&comp_temp->list); 343 kfree(comp_temp); 344 } 345 346 dma_pool_destroy(fsl_queue->comp_pool); 347 dma_pool_destroy(fsl_queue->desc_pool); 348 349 fsl_qdma->desc_allocated--; 350 fsl_queue->comp_pool = NULL; 351 fsl_queue->desc_pool = NULL; 352 } 353 354 static void fsl_qdma_comp_fill_memcpy(struct fsl_qdma_comp *fsl_comp, 355 dma_addr_t dst, dma_addr_t src, u32 len) 356 { 357 u32 cmd; 358 struct fsl_qdma_format *sdf, *ddf; 359 struct fsl_qdma_format *ccdf, *csgf_desc, *csgf_src, *csgf_dest; 360 361 ccdf = fsl_comp->virt_addr; 362 csgf_desc = fsl_comp->virt_addr + 1; 363 csgf_src = fsl_comp->virt_addr + 2; 364 csgf_dest = fsl_comp->virt_addr + 3; 365 sdf = fsl_comp->desc_virt_addr; 366 ddf = fsl_comp->desc_virt_addr + 1; 367 368 memset(fsl_comp->virt_addr, 0, FSL_QDMA_COMMAND_BUFFER_SIZE); 369 memset(fsl_comp->desc_virt_addr, 0, FSL_QDMA_DESCRIPTOR_BUFFER_SIZE); 370 /* Head Command Descriptor(Frame Descriptor) */ 371 qdma_desc_addr_set64(ccdf, fsl_comp->bus_addr + 16); 372 qdma_ccdf_set_format(ccdf, qdma_ccdf_get_offset(ccdf)); 373 qdma_ccdf_set_ser(ccdf, qdma_ccdf_get_status(ccdf)); 374 /* Status notification is enqueued to status queue. */ 375 /* Compound Command Descriptor(Frame List Table) */ 376 qdma_desc_addr_set64(csgf_desc, fsl_comp->desc_bus_addr); 377 /* It must be 32 as Compound S/G Descriptor */ 378 qdma_csgf_set_len(csgf_desc, 32); 379 qdma_desc_addr_set64(csgf_src, src); 380 qdma_csgf_set_len(csgf_src, len); 381 qdma_desc_addr_set64(csgf_dest, dst); 382 qdma_csgf_set_len(csgf_dest, len); 383 /* This entry is the last entry. */ 384 qdma_csgf_set_f(csgf_dest, len); 385 /* Descriptor Buffer */ 386 cmd = cpu_to_le32(FSL_QDMA_CMD_RWTTYPE << 387 FSL_QDMA_CMD_RWTTYPE_OFFSET); 388 sdf->data = QDMA_SDDF_CMD(cmd); 389 390 cmd = cpu_to_le32(FSL_QDMA_CMD_RWTTYPE << 391 FSL_QDMA_CMD_RWTTYPE_OFFSET); 392 cmd |= cpu_to_le32(FSL_QDMA_CMD_LWC << FSL_QDMA_CMD_LWC_OFFSET); 393 ddf->data = QDMA_SDDF_CMD(cmd); 394 } 395 396 /* 397 * Pre-request full command descriptor for enqueue. 398 */ 399 static int fsl_qdma_pre_request_enqueue_desc(struct fsl_qdma_queue *queue) 400 { 401 int i; 402 struct fsl_qdma_comp *comp_temp, *_comp_temp; 403 404 for (i = 0; i < queue->n_cq + FSL_COMMAND_QUEUE_OVERFLLOW; i++) { 405 comp_temp = kzalloc(sizeof(*comp_temp), GFP_KERNEL); 406 if (!comp_temp) 407 goto err_alloc; 408 comp_temp->virt_addr = 409 dma_pool_alloc(queue->comp_pool, GFP_KERNEL, 410 &comp_temp->bus_addr); 411 if (!comp_temp->virt_addr) 412 goto err_dma_alloc; 413 414 comp_temp->desc_virt_addr = 415 dma_pool_alloc(queue->desc_pool, GFP_KERNEL, 416 &comp_temp->desc_bus_addr); 417 if (!comp_temp->desc_virt_addr) 418 goto err_desc_dma_alloc; 419 420 list_add_tail(&comp_temp->list, &queue->comp_free); 421 } 422 423 return 0; 424 425 err_desc_dma_alloc: 426 dma_pool_free(queue->comp_pool, comp_temp->virt_addr, 427 comp_temp->bus_addr); 428 429 err_dma_alloc: 430 kfree(comp_temp); 431 432 err_alloc: 433 list_for_each_entry_safe(comp_temp, _comp_temp, 434 &queue->comp_free, list) { 435 if (comp_temp->virt_addr) 436 dma_pool_free(queue->comp_pool, 437 comp_temp->virt_addr, 438 comp_temp->bus_addr); 439 if (comp_temp->desc_virt_addr) 440 dma_pool_free(queue->desc_pool, 441 comp_temp->desc_virt_addr, 442 comp_temp->desc_bus_addr); 443 444 list_del(&comp_temp->list); 445 kfree(comp_temp); 446 } 447 448 return -ENOMEM; 449 } 450 451 /* 452 * Request a command descriptor for enqueue. 453 */ 454 static struct fsl_qdma_comp 455 *fsl_qdma_request_enqueue_desc(struct fsl_qdma_chan *fsl_chan) 456 { 457 unsigned long flags; 458 struct fsl_qdma_comp *comp_temp; 459 int timeout = FSL_QDMA_COMP_TIMEOUT; 460 struct fsl_qdma_queue *queue = fsl_chan->queue; 461 462 while (timeout--) { 463 spin_lock_irqsave(&queue->queue_lock, flags); 464 if (!list_empty(&queue->comp_free)) { 465 comp_temp = list_first_entry(&queue->comp_free, 466 struct fsl_qdma_comp, 467 list); 468 list_del(&comp_temp->list); 469 470 spin_unlock_irqrestore(&queue->queue_lock, flags); 471 comp_temp->qchan = fsl_chan; 472 return comp_temp; 473 } 474 spin_unlock_irqrestore(&queue->queue_lock, flags); 475 udelay(1); 476 } 477 478 return NULL; 479 } 480 481 static struct fsl_qdma_queue 482 *fsl_qdma_alloc_queue_resources(struct platform_device *pdev, 483 struct fsl_qdma_engine *fsl_qdma) 484 { 485 int ret, len, i, j; 486 int queue_num, block_number; 487 unsigned int queue_size[FSL_QDMA_QUEUE_MAX]; 488 struct fsl_qdma_queue *queue_head, *queue_temp; 489 490 queue_num = fsl_qdma->n_queues; 491 block_number = fsl_qdma->block_number; 492 493 if (queue_num > FSL_QDMA_QUEUE_MAX) 494 queue_num = FSL_QDMA_QUEUE_MAX; 495 len = sizeof(*queue_head) * queue_num * block_number; 496 queue_head = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); 497 if (!queue_head) 498 return NULL; 499 500 ret = device_property_read_u32_array(&pdev->dev, "queue-sizes", 501 queue_size, queue_num); 502 if (ret) { 503 dev_err(&pdev->dev, "Can't get queue-sizes.\n"); 504 return NULL; 505 } 506 for (j = 0; j < block_number; j++) { 507 for (i = 0; i < queue_num; i++) { 508 if (queue_size[i] > FSL_QDMA_CIRCULAR_DESC_SIZE_MAX || 509 queue_size[i] < FSL_QDMA_CIRCULAR_DESC_SIZE_MIN) { 510 dev_err(&pdev->dev, 511 "Get wrong queue-sizes.\n"); 512 return NULL; 513 } 514 queue_temp = queue_head + i + (j * queue_num); 515 516 queue_temp->cq = 517 dmam_alloc_coherent(&pdev->dev, 518 sizeof(struct fsl_qdma_format) * 519 queue_size[i], 520 &queue_temp->bus_addr, 521 GFP_KERNEL); 522 if (!queue_temp->cq) 523 return NULL; 524 queue_temp->block_base = fsl_qdma->block_base + 525 FSL_QDMA_BLOCK_BASE_OFFSET(fsl_qdma, j); 526 queue_temp->n_cq = queue_size[i]; 527 queue_temp->id = i; 528 queue_temp->virt_head = queue_temp->cq; 529 queue_temp->virt_tail = queue_temp->cq; 530 /* 531 * List for queue command buffer 532 */ 533 INIT_LIST_HEAD(&queue_temp->comp_used); 534 spin_lock_init(&queue_temp->queue_lock); 535 } 536 } 537 return queue_head; 538 } 539 540 static struct fsl_qdma_queue 541 *fsl_qdma_prep_status_queue(struct platform_device *pdev) 542 { 543 int ret; 544 unsigned int status_size; 545 struct fsl_qdma_queue *status_head; 546 struct device_node *np = pdev->dev.of_node; 547 548 ret = of_property_read_u32(np, "status-sizes", &status_size); 549 if (ret) { 550 dev_err(&pdev->dev, "Can't get status-sizes.\n"); 551 return NULL; 552 } 553 if (status_size > FSL_QDMA_CIRCULAR_DESC_SIZE_MAX || 554 status_size < FSL_QDMA_CIRCULAR_DESC_SIZE_MIN) { 555 dev_err(&pdev->dev, "Get wrong status_size.\n"); 556 return NULL; 557 } 558 status_head = devm_kzalloc(&pdev->dev, 559 sizeof(*status_head), GFP_KERNEL); 560 if (!status_head) 561 return NULL; 562 563 /* 564 * Buffer for queue command 565 */ 566 status_head->cq = dmam_alloc_coherent(&pdev->dev, 567 sizeof(struct fsl_qdma_format) * 568 status_size, 569 &status_head->bus_addr, 570 GFP_KERNEL); 571 if (!status_head->cq) 572 return NULL; 573 574 status_head->n_cq = status_size; 575 status_head->virt_head = status_head->cq; 576 status_head->virt_tail = status_head->cq; 577 status_head->comp_pool = NULL; 578 579 return status_head; 580 } 581 582 static int fsl_qdma_halt(struct fsl_qdma_engine *fsl_qdma) 583 { 584 u32 reg; 585 int i, j, count = FSL_QDMA_HALT_COUNT; 586 void __iomem *block, *ctrl = fsl_qdma->ctrl_base; 587 588 /* Disable the command queue and wait for idle state. */ 589 reg = qdma_readl(fsl_qdma, ctrl + FSL_QDMA_DMR); 590 reg |= FSL_QDMA_DMR_DQD; 591 qdma_writel(fsl_qdma, reg, ctrl + FSL_QDMA_DMR); 592 for (j = 0; j < fsl_qdma->block_number; j++) { 593 block = fsl_qdma->block_base + 594 FSL_QDMA_BLOCK_BASE_OFFSET(fsl_qdma, j); 595 for (i = 0; i < FSL_QDMA_QUEUE_NUM_MAX; i++) 596 qdma_writel(fsl_qdma, 0, block + FSL_QDMA_BCQMR(i)); 597 } 598 while (1) { 599 reg = qdma_readl(fsl_qdma, ctrl + FSL_QDMA_DSR); 600 if (!(reg & FSL_QDMA_DSR_DB)) 601 break; 602 if (count-- < 0) 603 return -EBUSY; 604 udelay(100); 605 } 606 607 for (j = 0; j < fsl_qdma->block_number; j++) { 608 block = fsl_qdma->block_base + 609 FSL_QDMA_BLOCK_BASE_OFFSET(fsl_qdma, j); 610 611 /* Disable status queue. */ 612 qdma_writel(fsl_qdma, 0, block + FSL_QDMA_BSQMR); 613 614 /* 615 * clear the command queue interrupt detect register for 616 * all queues. 617 */ 618 qdma_writel(fsl_qdma, FSL_QDMA_BCQIDR_CLEAR, 619 block + FSL_QDMA_BCQIDR(0)); 620 } 621 622 return 0; 623 } 624 625 static int 626 fsl_qdma_queue_transfer_complete(struct fsl_qdma_engine *fsl_qdma, 627 void *block, 628 int id) 629 { 630 bool duplicate; 631 u32 reg, i, count; 632 u8 completion_status; 633 struct fsl_qdma_queue *temp_queue; 634 struct fsl_qdma_format *status_addr; 635 struct fsl_qdma_comp *fsl_comp = NULL; 636 struct fsl_qdma_queue *fsl_queue = fsl_qdma->queue; 637 struct fsl_qdma_queue *fsl_status = fsl_qdma->status[id]; 638 639 count = FSL_QDMA_MAX_SIZE; 640 641 while (count--) { 642 duplicate = 0; 643 reg = qdma_readl(fsl_qdma, block + FSL_QDMA_BSQSR); 644 if (reg & FSL_QDMA_BSQSR_QE) 645 return 0; 646 647 status_addr = fsl_status->virt_head; 648 649 if (qdma_ccdf_get_queue(status_addr) == 650 __this_cpu_read(pre.queue) && 651 qdma_ccdf_addr_get64(status_addr) == 652 __this_cpu_read(pre.addr)) 653 duplicate = 1; 654 i = qdma_ccdf_get_queue(status_addr) + 655 id * fsl_qdma->n_queues; 656 __this_cpu_write(pre.addr, qdma_ccdf_addr_get64(status_addr)); 657 __this_cpu_write(pre.queue, qdma_ccdf_get_queue(status_addr)); 658 temp_queue = fsl_queue + i; 659 660 spin_lock(&temp_queue->queue_lock); 661 if (list_empty(&temp_queue->comp_used)) { 662 if (!duplicate) { 663 spin_unlock(&temp_queue->queue_lock); 664 return -EAGAIN; 665 } 666 } else { 667 fsl_comp = list_first_entry(&temp_queue->comp_used, 668 struct fsl_qdma_comp, list); 669 if (fsl_comp->bus_addr + 16 != 670 __this_cpu_read(pre.addr)) { 671 if (!duplicate) { 672 spin_unlock(&temp_queue->queue_lock); 673 return -EAGAIN; 674 } 675 } 676 } 677 678 if (duplicate) { 679 reg = qdma_readl(fsl_qdma, block + FSL_QDMA_BSQMR); 680 reg |= FSL_QDMA_BSQMR_DI; 681 qdma_desc_addr_set64(status_addr, 0x0); 682 fsl_status->virt_head++; 683 if (fsl_status->virt_head == fsl_status->cq 684 + fsl_status->n_cq) 685 fsl_status->virt_head = fsl_status->cq; 686 qdma_writel(fsl_qdma, reg, block + FSL_QDMA_BSQMR); 687 spin_unlock(&temp_queue->queue_lock); 688 continue; 689 } 690 list_del(&fsl_comp->list); 691 692 completion_status = qdma_ccdf_get_status(status_addr); 693 694 reg = qdma_readl(fsl_qdma, block + FSL_QDMA_BSQMR); 695 reg |= FSL_QDMA_BSQMR_DI; 696 qdma_desc_addr_set64(status_addr, 0x0); 697 fsl_status->virt_head++; 698 if (fsl_status->virt_head == fsl_status->cq + fsl_status->n_cq) 699 fsl_status->virt_head = fsl_status->cq; 700 qdma_writel(fsl_qdma, reg, block + FSL_QDMA_BSQMR); 701 spin_unlock(&temp_queue->queue_lock); 702 703 /* The completion_status is evaluated here 704 * (outside of spin lock) 705 */ 706 if (completion_status) { 707 /* A completion error occurred! */ 708 if (completion_status & QDMA_CCDF_STATUS_WTE) { 709 /* Write transaction error */ 710 fsl_comp->vdesc.tx_result.result = 711 DMA_TRANS_WRITE_FAILED; 712 } else if (completion_status & QDMA_CCDF_STATUS_RTE) { 713 /* Read transaction error */ 714 fsl_comp->vdesc.tx_result.result = 715 DMA_TRANS_READ_FAILED; 716 } else { 717 /* Command/source/destination 718 * description error 719 */ 720 fsl_comp->vdesc.tx_result.result = 721 DMA_TRANS_ABORTED; 722 dev_err(fsl_qdma->dma_dev.dev, 723 "DMA status descriptor error %x\n", 724 completion_status); 725 } 726 } 727 728 spin_lock(&fsl_comp->qchan->vchan.lock); 729 vchan_cookie_complete(&fsl_comp->vdesc); 730 fsl_comp->qchan->status = DMA_COMPLETE; 731 spin_unlock(&fsl_comp->qchan->vchan.lock); 732 } 733 734 return 0; 735 } 736 737 static irqreturn_t fsl_qdma_error_handler(int irq, void *dev_id) 738 { 739 unsigned int intr; 740 struct fsl_qdma_engine *fsl_qdma = dev_id; 741 void __iomem *status = fsl_qdma->status_base; 742 unsigned int decfdw0r; 743 unsigned int decfdw1r; 744 unsigned int decfdw2r; 745 unsigned int decfdw3r; 746 747 intr = qdma_readl(fsl_qdma, status + FSL_QDMA_DEDR); 748 749 if (intr) { 750 decfdw0r = qdma_readl(fsl_qdma, status + FSL_QDMA_DECFDW0R); 751 decfdw1r = qdma_readl(fsl_qdma, status + FSL_QDMA_DECFDW1R); 752 decfdw2r = qdma_readl(fsl_qdma, status + FSL_QDMA_DECFDW2R); 753 decfdw3r = qdma_readl(fsl_qdma, status + FSL_QDMA_DECFDW3R); 754 dev_err(fsl_qdma->dma_dev.dev, 755 "DMA transaction error! (%x: %x-%x-%x-%x)\n", 756 intr, decfdw0r, decfdw1r, decfdw2r, decfdw3r); 757 } 758 759 qdma_writel(fsl_qdma, FSL_QDMA_DEDR_CLEAR, status + FSL_QDMA_DEDR); 760 return IRQ_HANDLED; 761 } 762 763 static irqreturn_t fsl_qdma_queue_handler(int irq, void *dev_id) 764 { 765 int id; 766 unsigned int intr, reg; 767 struct fsl_qdma_engine *fsl_qdma = dev_id; 768 void __iomem *block, *ctrl = fsl_qdma->ctrl_base; 769 770 id = irq - fsl_qdma->irq_base; 771 if (id < 0 && id > fsl_qdma->block_number) { 772 dev_err(fsl_qdma->dma_dev.dev, 773 "irq %d is wrong irq_base is %d\n", 774 irq, fsl_qdma->irq_base); 775 } 776 777 block = fsl_qdma->block_base + 778 FSL_QDMA_BLOCK_BASE_OFFSET(fsl_qdma, id); 779 780 intr = qdma_readl(fsl_qdma, block + FSL_QDMA_BCQIDR(0)); 781 782 if ((intr & FSL_QDMA_CQIDR_SQT) != 0) 783 intr = fsl_qdma_queue_transfer_complete(fsl_qdma, block, id); 784 785 if (intr != 0) { 786 reg = qdma_readl(fsl_qdma, ctrl + FSL_QDMA_DMR); 787 reg |= FSL_QDMA_DMR_DQD; 788 qdma_writel(fsl_qdma, reg, ctrl + FSL_QDMA_DMR); 789 qdma_writel(fsl_qdma, 0, block + FSL_QDMA_BCQIER(0)); 790 dev_err(fsl_qdma->dma_dev.dev, "QDMA: status err!\n"); 791 } 792 793 /* Clear all detected events and interrupts. */ 794 qdma_writel(fsl_qdma, FSL_QDMA_BCQIDR_CLEAR, 795 block + FSL_QDMA_BCQIDR(0)); 796 797 return IRQ_HANDLED; 798 } 799 800 static int 801 fsl_qdma_irq_init(struct platform_device *pdev, 802 struct fsl_qdma_engine *fsl_qdma) 803 { 804 int i; 805 int cpu; 806 int ret; 807 char irq_name[32]; 808 809 fsl_qdma->error_irq = 810 platform_get_irq_byname(pdev, "qdma-error"); 811 if (fsl_qdma->error_irq < 0) 812 return fsl_qdma->error_irq; 813 814 ret = devm_request_irq(&pdev->dev, fsl_qdma->error_irq, 815 fsl_qdma_error_handler, 0, 816 "qDMA error", fsl_qdma); 817 if (ret) { 818 dev_err(&pdev->dev, "Can't register qDMA controller IRQ.\n"); 819 return ret; 820 } 821 822 for (i = 0; i < fsl_qdma->block_number; i++) { 823 sprintf(irq_name, "qdma-queue%d", i); 824 fsl_qdma->queue_irq[i] = 825 platform_get_irq_byname(pdev, irq_name); 826 827 if (fsl_qdma->queue_irq[i] < 0) 828 return fsl_qdma->queue_irq[i]; 829 830 ret = devm_request_irq(&pdev->dev, 831 fsl_qdma->queue_irq[i], 832 fsl_qdma_queue_handler, 833 0, 834 "qDMA queue", 835 fsl_qdma); 836 if (ret) { 837 dev_err(&pdev->dev, 838 "Can't register qDMA queue IRQ.\n"); 839 return ret; 840 } 841 842 cpu = i % num_online_cpus(); 843 ret = irq_set_affinity_hint(fsl_qdma->queue_irq[i], 844 get_cpu_mask(cpu)); 845 if (ret) { 846 dev_err(&pdev->dev, 847 "Can't set cpu %d affinity to IRQ %d.\n", 848 cpu, 849 fsl_qdma->queue_irq[i]); 850 return ret; 851 } 852 } 853 854 return 0; 855 } 856 857 static void fsl_qdma_irq_exit(struct platform_device *pdev, 858 struct fsl_qdma_engine *fsl_qdma) 859 { 860 int i; 861 862 devm_free_irq(&pdev->dev, fsl_qdma->error_irq, fsl_qdma); 863 for (i = 0; i < fsl_qdma->block_number; i++) 864 devm_free_irq(&pdev->dev, fsl_qdma->queue_irq[i], fsl_qdma); 865 } 866 867 static int fsl_qdma_reg_init(struct fsl_qdma_engine *fsl_qdma) 868 { 869 u32 reg; 870 int i, j, ret; 871 struct fsl_qdma_queue *temp; 872 void __iomem *status = fsl_qdma->status_base; 873 void __iomem *block, *ctrl = fsl_qdma->ctrl_base; 874 struct fsl_qdma_queue *fsl_queue = fsl_qdma->queue; 875 876 /* Try to halt the qDMA engine first. */ 877 ret = fsl_qdma_halt(fsl_qdma); 878 if (ret) { 879 dev_err(fsl_qdma->dma_dev.dev, "DMA halt failed!"); 880 return ret; 881 } 882 883 for (i = 0; i < fsl_qdma->block_number; i++) { 884 /* 885 * Clear the command queue interrupt detect register for 886 * all queues. 887 */ 888 889 block = fsl_qdma->block_base + 890 FSL_QDMA_BLOCK_BASE_OFFSET(fsl_qdma, i); 891 qdma_writel(fsl_qdma, FSL_QDMA_BCQIDR_CLEAR, 892 block + FSL_QDMA_BCQIDR(0)); 893 } 894 895 for (j = 0; j < fsl_qdma->block_number; j++) { 896 block = fsl_qdma->block_base + 897 FSL_QDMA_BLOCK_BASE_OFFSET(fsl_qdma, j); 898 for (i = 0; i < fsl_qdma->n_queues; i++) { 899 temp = fsl_queue + i + (j * fsl_qdma->n_queues); 900 /* 901 * Initialize Command Queue registers to 902 * point to the first 903 * command descriptor in memory. 904 * Dequeue Pointer Address Registers 905 * Enqueue Pointer Address Registers 906 */ 907 908 qdma_writel(fsl_qdma, temp->bus_addr, 909 block + FSL_QDMA_BCQDPA_SADDR(i)); 910 qdma_writel(fsl_qdma, temp->bus_addr, 911 block + FSL_QDMA_BCQEPA_SADDR(i)); 912 913 /* Initialize the queue mode. */ 914 reg = FSL_QDMA_BCQMR_EN; 915 reg |= FSL_QDMA_BCQMR_CD_THLD(ilog2(temp->n_cq) - 4); 916 reg |= FSL_QDMA_BCQMR_CQ_SIZE(ilog2(temp->n_cq) - 6); 917 qdma_writel(fsl_qdma, reg, block + FSL_QDMA_BCQMR(i)); 918 } 919 920 /* 921 * Workaround for erratum: ERR010812. 922 * We must enable XOFF to avoid the enqueue rejection occurs. 923 * Setting SQCCMR ENTER_WM to 0x20. 924 */ 925 926 qdma_writel(fsl_qdma, FSL_QDMA_SQCCMR_ENTER_WM, 927 block + FSL_QDMA_SQCCMR); 928 929 /* 930 * Initialize status queue registers to point to the first 931 * command descriptor in memory. 932 * Dequeue Pointer Address Registers 933 * Enqueue Pointer Address Registers 934 */ 935 936 qdma_writel(fsl_qdma, fsl_qdma->status[j]->bus_addr, 937 block + FSL_QDMA_SQEPAR); 938 qdma_writel(fsl_qdma, fsl_qdma->status[j]->bus_addr, 939 block + FSL_QDMA_SQDPAR); 940 /* Initialize status queue interrupt. */ 941 qdma_writel(fsl_qdma, FSL_QDMA_BCQIER_CQTIE, 942 block + FSL_QDMA_BCQIER(0)); 943 qdma_writel(fsl_qdma, FSL_QDMA_BSQICR_ICEN | 944 FSL_QDMA_BSQICR_ICST(5) | 0x8000, 945 block + FSL_QDMA_BSQICR); 946 qdma_writel(fsl_qdma, FSL_QDMA_CQIER_MEIE | 947 FSL_QDMA_CQIER_TEIE, 948 block + FSL_QDMA_CQIER); 949 950 /* Initialize the status queue mode. */ 951 reg = FSL_QDMA_BSQMR_EN; 952 reg |= FSL_QDMA_BSQMR_CQ_SIZE(ilog2 953 (fsl_qdma->status[j]->n_cq) - 6); 954 955 qdma_writel(fsl_qdma, reg, block + FSL_QDMA_BSQMR); 956 reg = qdma_readl(fsl_qdma, block + FSL_QDMA_BSQMR); 957 } 958 959 /* Initialize controller interrupt register. */ 960 qdma_writel(fsl_qdma, FSL_QDMA_DEDR_CLEAR, status + FSL_QDMA_DEDR); 961 qdma_writel(fsl_qdma, FSL_QDMA_DEIER_CLEAR, status + FSL_QDMA_DEIER); 962 963 reg = qdma_readl(fsl_qdma, ctrl + FSL_QDMA_DMR); 964 reg &= ~FSL_QDMA_DMR_DQD; 965 qdma_writel(fsl_qdma, reg, ctrl + FSL_QDMA_DMR); 966 967 return 0; 968 } 969 970 static struct dma_async_tx_descriptor * 971 fsl_qdma_prep_memcpy(struct dma_chan *chan, dma_addr_t dst, 972 dma_addr_t src, size_t len, unsigned long flags) 973 { 974 struct fsl_qdma_comp *fsl_comp; 975 struct fsl_qdma_chan *fsl_chan = to_fsl_qdma_chan(chan); 976 977 fsl_comp = fsl_qdma_request_enqueue_desc(fsl_chan); 978 979 if (!fsl_comp) 980 return NULL; 981 982 fsl_qdma_comp_fill_memcpy(fsl_comp, dst, src, len); 983 984 return vchan_tx_prep(&fsl_chan->vchan, &fsl_comp->vdesc, flags); 985 } 986 987 static void fsl_qdma_enqueue_desc(struct fsl_qdma_chan *fsl_chan) 988 { 989 u32 reg; 990 struct virt_dma_desc *vdesc; 991 struct fsl_qdma_comp *fsl_comp; 992 struct fsl_qdma_queue *fsl_queue = fsl_chan->queue; 993 void __iomem *block = fsl_queue->block_base; 994 995 reg = qdma_readl(fsl_chan->qdma, block + FSL_QDMA_BCQSR(fsl_queue->id)); 996 if (reg & (FSL_QDMA_BCQSR_QF | FSL_QDMA_BCQSR_XOFF)) 997 return; 998 vdesc = vchan_next_desc(&fsl_chan->vchan); 999 if (!vdesc) 1000 return; 1001 list_del(&vdesc->node); 1002 fsl_comp = to_fsl_qdma_comp(vdesc); 1003 1004 memcpy(fsl_queue->virt_head++, 1005 fsl_comp->virt_addr, sizeof(struct fsl_qdma_format)); 1006 if (fsl_queue->virt_head == fsl_queue->cq + fsl_queue->n_cq) 1007 fsl_queue->virt_head = fsl_queue->cq; 1008 1009 list_add_tail(&fsl_comp->list, &fsl_queue->comp_used); 1010 barrier(); 1011 reg = qdma_readl(fsl_chan->qdma, block + FSL_QDMA_BCQMR(fsl_queue->id)); 1012 reg |= FSL_QDMA_BCQMR_EI; 1013 qdma_writel(fsl_chan->qdma, reg, block + FSL_QDMA_BCQMR(fsl_queue->id)); 1014 fsl_chan->status = DMA_IN_PROGRESS; 1015 } 1016 1017 static void fsl_qdma_free_desc(struct virt_dma_desc *vdesc) 1018 { 1019 unsigned long flags; 1020 struct fsl_qdma_comp *fsl_comp; 1021 struct fsl_qdma_queue *fsl_queue; 1022 1023 fsl_comp = to_fsl_qdma_comp(vdesc); 1024 fsl_queue = fsl_comp->qchan->queue; 1025 1026 spin_lock_irqsave(&fsl_queue->queue_lock, flags); 1027 list_add_tail(&fsl_comp->list, &fsl_queue->comp_free); 1028 spin_unlock_irqrestore(&fsl_queue->queue_lock, flags); 1029 } 1030 1031 static void fsl_qdma_issue_pending(struct dma_chan *chan) 1032 { 1033 unsigned long flags; 1034 struct fsl_qdma_chan *fsl_chan = to_fsl_qdma_chan(chan); 1035 struct fsl_qdma_queue *fsl_queue = fsl_chan->queue; 1036 1037 spin_lock_irqsave(&fsl_queue->queue_lock, flags); 1038 spin_lock(&fsl_chan->vchan.lock); 1039 if (vchan_issue_pending(&fsl_chan->vchan)) 1040 fsl_qdma_enqueue_desc(fsl_chan); 1041 spin_unlock(&fsl_chan->vchan.lock); 1042 spin_unlock_irqrestore(&fsl_queue->queue_lock, flags); 1043 } 1044 1045 static void fsl_qdma_synchronize(struct dma_chan *chan) 1046 { 1047 struct fsl_qdma_chan *fsl_chan = to_fsl_qdma_chan(chan); 1048 1049 vchan_synchronize(&fsl_chan->vchan); 1050 } 1051 1052 static int fsl_qdma_terminate_all(struct dma_chan *chan) 1053 { 1054 LIST_HEAD(head); 1055 unsigned long flags; 1056 struct fsl_qdma_chan *fsl_chan = to_fsl_qdma_chan(chan); 1057 1058 spin_lock_irqsave(&fsl_chan->vchan.lock, flags); 1059 vchan_get_all_descriptors(&fsl_chan->vchan, &head); 1060 spin_unlock_irqrestore(&fsl_chan->vchan.lock, flags); 1061 vchan_dma_desc_free_list(&fsl_chan->vchan, &head); 1062 return 0; 1063 } 1064 1065 static int fsl_qdma_alloc_chan_resources(struct dma_chan *chan) 1066 { 1067 int ret; 1068 struct fsl_qdma_chan *fsl_chan = to_fsl_qdma_chan(chan); 1069 struct fsl_qdma_engine *fsl_qdma = fsl_chan->qdma; 1070 struct fsl_qdma_queue *fsl_queue = fsl_chan->queue; 1071 1072 if (fsl_queue->comp_pool && fsl_queue->desc_pool) 1073 return fsl_qdma->desc_allocated; 1074 1075 INIT_LIST_HEAD(&fsl_queue->comp_free); 1076 1077 /* 1078 * The dma pool for queue command buffer 1079 */ 1080 fsl_queue->comp_pool = 1081 dma_pool_create("comp_pool", 1082 chan->device->dev, 1083 FSL_QDMA_COMMAND_BUFFER_SIZE, 1084 64, 0); 1085 if (!fsl_queue->comp_pool) 1086 return -ENOMEM; 1087 1088 /* 1089 * The dma pool for Descriptor(SD/DD) buffer 1090 */ 1091 fsl_queue->desc_pool = 1092 dma_pool_create("desc_pool", 1093 chan->device->dev, 1094 FSL_QDMA_DESCRIPTOR_BUFFER_SIZE, 1095 32, 0); 1096 if (!fsl_queue->desc_pool) 1097 goto err_desc_pool; 1098 1099 ret = fsl_qdma_pre_request_enqueue_desc(fsl_queue); 1100 if (ret) { 1101 dev_err(chan->device->dev, 1102 "failed to alloc dma buffer for S/G descriptor\n"); 1103 goto err_mem; 1104 } 1105 1106 fsl_qdma->desc_allocated++; 1107 return fsl_qdma->desc_allocated; 1108 1109 err_mem: 1110 dma_pool_destroy(fsl_queue->desc_pool); 1111 err_desc_pool: 1112 dma_pool_destroy(fsl_queue->comp_pool); 1113 return -ENOMEM; 1114 } 1115 1116 static int fsl_qdma_probe(struct platform_device *pdev) 1117 { 1118 int ret, i; 1119 int blk_num, blk_off; 1120 u32 len, chans, queues; 1121 struct fsl_qdma_chan *fsl_chan; 1122 struct fsl_qdma_engine *fsl_qdma; 1123 struct device_node *np = pdev->dev.of_node; 1124 1125 ret = of_property_read_u32(np, "dma-channels", &chans); 1126 if (ret) { 1127 dev_err(&pdev->dev, "Can't get dma-channels.\n"); 1128 return ret; 1129 } 1130 1131 ret = of_property_read_u32(np, "block-offset", &blk_off); 1132 if (ret) { 1133 dev_err(&pdev->dev, "Can't get block-offset.\n"); 1134 return ret; 1135 } 1136 1137 ret = of_property_read_u32(np, "block-number", &blk_num); 1138 if (ret) { 1139 dev_err(&pdev->dev, "Can't get block-number.\n"); 1140 return ret; 1141 } 1142 1143 blk_num = min_t(int, blk_num, num_online_cpus()); 1144 1145 len = sizeof(*fsl_qdma); 1146 fsl_qdma = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); 1147 if (!fsl_qdma) 1148 return -ENOMEM; 1149 1150 len = sizeof(*fsl_chan) * chans; 1151 fsl_qdma->chans = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); 1152 if (!fsl_qdma->chans) 1153 return -ENOMEM; 1154 1155 len = sizeof(struct fsl_qdma_queue *) * blk_num; 1156 fsl_qdma->status = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); 1157 if (!fsl_qdma->status) 1158 return -ENOMEM; 1159 1160 len = sizeof(int) * blk_num; 1161 fsl_qdma->queue_irq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL); 1162 if (!fsl_qdma->queue_irq) 1163 return -ENOMEM; 1164 1165 ret = of_property_read_u32(np, "fsl,dma-queues", &queues); 1166 if (ret) { 1167 dev_err(&pdev->dev, "Can't get queues.\n"); 1168 return ret; 1169 } 1170 1171 fsl_qdma->desc_allocated = 0; 1172 fsl_qdma->n_chans = chans; 1173 fsl_qdma->n_queues = queues; 1174 fsl_qdma->block_number = blk_num; 1175 fsl_qdma->block_offset = blk_off; 1176 1177 mutex_init(&fsl_qdma->fsl_qdma_mutex); 1178 1179 for (i = 0; i < fsl_qdma->block_number; i++) { 1180 fsl_qdma->status[i] = fsl_qdma_prep_status_queue(pdev); 1181 if (!fsl_qdma->status[i]) 1182 return -ENOMEM; 1183 } 1184 fsl_qdma->ctrl_base = devm_platform_ioremap_resource(pdev, 0); 1185 if (IS_ERR(fsl_qdma->ctrl_base)) 1186 return PTR_ERR(fsl_qdma->ctrl_base); 1187 1188 fsl_qdma->status_base = devm_platform_ioremap_resource(pdev, 1); 1189 if (IS_ERR(fsl_qdma->status_base)) 1190 return PTR_ERR(fsl_qdma->status_base); 1191 1192 fsl_qdma->block_base = devm_platform_ioremap_resource(pdev, 2); 1193 if (IS_ERR(fsl_qdma->block_base)) 1194 return PTR_ERR(fsl_qdma->block_base); 1195 fsl_qdma->queue = fsl_qdma_alloc_queue_resources(pdev, fsl_qdma); 1196 if (!fsl_qdma->queue) 1197 return -ENOMEM; 1198 1199 ret = fsl_qdma_irq_init(pdev, fsl_qdma); 1200 if (ret) 1201 return ret; 1202 1203 fsl_qdma->irq_base = platform_get_irq_byname(pdev, "qdma-queue0"); 1204 if (fsl_qdma->irq_base < 0) 1205 return fsl_qdma->irq_base; 1206 1207 fsl_qdma->feature = of_property_read_bool(np, "big-endian"); 1208 INIT_LIST_HEAD(&fsl_qdma->dma_dev.channels); 1209 1210 for (i = 0; i < fsl_qdma->n_chans; i++) { 1211 struct fsl_qdma_chan *fsl_chan = &fsl_qdma->chans[i]; 1212 1213 fsl_chan->qdma = fsl_qdma; 1214 fsl_chan->queue = fsl_qdma->queue + i % (fsl_qdma->n_queues * 1215 fsl_qdma->block_number); 1216 fsl_chan->vchan.desc_free = fsl_qdma_free_desc; 1217 vchan_init(&fsl_chan->vchan, &fsl_qdma->dma_dev); 1218 } 1219 1220 dma_cap_set(DMA_MEMCPY, fsl_qdma->dma_dev.cap_mask); 1221 1222 fsl_qdma->dma_dev.dev = &pdev->dev; 1223 fsl_qdma->dma_dev.device_free_chan_resources = 1224 fsl_qdma_free_chan_resources; 1225 fsl_qdma->dma_dev.device_alloc_chan_resources = 1226 fsl_qdma_alloc_chan_resources; 1227 fsl_qdma->dma_dev.device_tx_status = dma_cookie_status; 1228 fsl_qdma->dma_dev.device_prep_dma_memcpy = fsl_qdma_prep_memcpy; 1229 fsl_qdma->dma_dev.device_issue_pending = fsl_qdma_issue_pending; 1230 fsl_qdma->dma_dev.device_synchronize = fsl_qdma_synchronize; 1231 fsl_qdma->dma_dev.device_terminate_all = fsl_qdma_terminate_all; 1232 1233 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40)); 1234 if (ret) { 1235 dev_err(&pdev->dev, "dma_set_mask failure.\n"); 1236 return ret; 1237 } 1238 1239 platform_set_drvdata(pdev, fsl_qdma); 1240 1241 ret = dma_async_device_register(&fsl_qdma->dma_dev); 1242 if (ret) { 1243 dev_err(&pdev->dev, 1244 "Can't register NXP Layerscape qDMA engine.\n"); 1245 return ret; 1246 } 1247 1248 ret = fsl_qdma_reg_init(fsl_qdma); 1249 if (ret) { 1250 dev_err(&pdev->dev, "Can't Initialize the qDMA engine.\n"); 1251 return ret; 1252 } 1253 1254 return 0; 1255 } 1256 1257 static void fsl_qdma_cleanup_vchan(struct dma_device *dmadev) 1258 { 1259 struct fsl_qdma_chan *chan, *_chan; 1260 1261 list_for_each_entry_safe(chan, _chan, 1262 &dmadev->channels, vchan.chan.device_node) { 1263 list_del(&chan->vchan.chan.device_node); 1264 tasklet_kill(&chan->vchan.task); 1265 } 1266 } 1267 1268 static void fsl_qdma_remove(struct platform_device *pdev) 1269 { 1270 struct device_node *np = pdev->dev.of_node; 1271 struct fsl_qdma_engine *fsl_qdma = platform_get_drvdata(pdev); 1272 1273 fsl_qdma_irq_exit(pdev, fsl_qdma); 1274 fsl_qdma_cleanup_vchan(&fsl_qdma->dma_dev); 1275 of_dma_controller_free(np); 1276 dma_async_device_unregister(&fsl_qdma->dma_dev); 1277 } 1278 1279 static const struct of_device_id fsl_qdma_dt_ids[] = { 1280 { .compatible = "fsl,ls1021a-qdma", }, 1281 { /* sentinel */ } 1282 }; 1283 MODULE_DEVICE_TABLE(of, fsl_qdma_dt_ids); 1284 1285 static struct platform_driver fsl_qdma_driver = { 1286 .driver = { 1287 .name = "fsl-qdma", 1288 .of_match_table = fsl_qdma_dt_ids, 1289 }, 1290 .probe = fsl_qdma_probe, 1291 .remove_new = fsl_qdma_remove, 1292 }; 1293 1294 module_platform_driver(fsl_qdma_driver); 1295 1296 MODULE_ALIAS("platform:fsl-qdma"); 1297 MODULE_LICENSE("GPL v2"); 1298 MODULE_DESCRIPTION("NXP Layerscape qDMA engine driver"); 1299