1 // SPDX-License-Identifier: GPL-2.0+
5 * Derived from linux/drivers/dma/bcm63xx-iudma.c:
8 * Derived from linux/drivers/net/ethernet/broadcom/bcm63xx_enet.c:
11 * Derived from bcm963xx_4.12L.06B_consumer/shared/opensource/include/bcm963xx/63268_map_part.h:
12 * Copyright (C) 2000-2010 Broadcom Corporation
14 * Derived from bcm963xx_4.12L.06B_consumer/bcmdrivers/opensource/net/enet/impl4/bcmenet.c:
15 * Copyright (C) 2010 Broadcom Corporation
22 #include <dma-uclass.h>
29 #include <linux/bitops.h>
30 #include <linux/delay.h>
31 #include <linux/printk.h>
37 #define DMA_CHAN_FLOWC(x) ((x) >> 1)
38 #define DMA_CHAN_MAX 16
39 #define DMA_CHAN_SIZE 0x10
40 #define DMA_CHAN_TOUT 500
42 /* DMA Global Configuration register */
43 #define DMA_CFG_REG 0x00
44 #define DMA_CFG_ENABLE_SHIFT 0
45 #define DMA_CFG_ENABLE_MASK (1 << DMA_CFG_ENABLE_SHIFT)
46 #define DMA_CFG_FLOWC_ENABLE(x) BIT(DMA_CHAN_FLOWC(x) + 1)
47 #define DMA_CFG_NCHANS_SHIFT 24
48 #define DMA_CFG_NCHANS_MASK (0xf << DMA_CFG_NCHANS_SHIFT)
50 /* DMA Global Flow Control registers */
51 #define DMA_FLOWC_THR_LO_REG(x) (0x04 + DMA_CHAN_FLOWC(x) * 0x0c)
52 #define DMA_FLOWC_THR_HI_REG(x) (0x08 + DMA_CHAN_FLOWC(x) * 0x0c)
53 #define DMA_FLOWC_ALLOC_REG(x) (0x0c + DMA_CHAN_FLOWC(x) * 0x0c)
54 #define DMA_FLOWC_ALLOC_FORCE_SHIFT 31
55 #define DMA_FLOWC_ALLOC_FORCE_MASK (1 << DMA_FLOWC_ALLOC_FORCE_SHIFT)
57 /* DMA Global Reset register */
58 #define DMA_RST_REG 0x34
59 #define DMA_RST_CHAN_SHIFT 0
60 #define DMA_RST_CHAN_MASK(x) (1 << x)
62 /* DMA Channel Configuration register */
63 #define DMAC_CFG_REG(x) (DMA_CHAN_SIZE * (x) + 0x00)
64 #define DMAC_CFG_ENABLE_SHIFT 0
65 #define DMAC_CFG_ENABLE_MASK (1 << DMAC_CFG_ENABLE_SHIFT)
66 #define DMAC_CFG_PKT_HALT_SHIFT 1
67 #define DMAC_CFG_PKT_HALT_MASK (1 << DMAC_CFG_PKT_HALT_SHIFT)
68 #define DMAC_CFG_BRST_HALT_SHIFT 2
69 #define DMAC_CFG_BRST_HALT_MASK (1 << DMAC_CFG_BRST_HALT_SHIFT)
71 /* DMA Channel Max Burst Length register */
72 #define DMAC_BURST_REG(x) (DMA_CHAN_SIZE * (x) + 0x0c)
74 /* DMA SRAM Descriptor Ring Start register */
75 #define DMAS_RSTART_REG(x) (DMA_CHAN_SIZE * (x) + 0x00)
77 /* DMA SRAM State/Bytes done/ring offset register */
78 #define DMAS_STATE_DATA_REG(x) (DMA_CHAN_SIZE * (x) + 0x04)
80 /* DMA SRAM Buffer Descriptor status and length register */
81 #define DMAS_DESC_LEN_STATUS_REG(x) (DMA_CHAN_SIZE * (x) + 0x08)
83 /* DMA SRAM Buffer Descriptor status and length register */
84 #define DMAS_DESC_BASE_BUFPTR_REG(x) (DMA_CHAN_SIZE * (x) + 0x0c)
86 /* DMA Descriptor Status */
87 #define DMAD_ST_CRC_SHIFT 8
88 #define DMAD_ST_CRC_MASK (1 << DMAD_ST_CRC_SHIFT)
89 #define DMAD_ST_WRAP_SHIFT 12
90 #define DMAD_ST_WRAP_MASK (1 << DMAD_ST_WRAP_SHIFT)
91 #define DMAD_ST_SOP_SHIFT 13
92 #define DMAD_ST_SOP_MASK (1 << DMAD_ST_SOP_SHIFT)
93 #define DMAD_ST_EOP_SHIFT 14
94 #define DMAD_ST_EOP_MASK (1 << DMAD_ST_EOP_SHIFT)
95 #define DMAD_ST_OWN_SHIFT 15
96 #define DMAD_ST_OWN_MASK (1 << DMAD_ST_OWN_SHIFT)
98 #define DMAD6348_ST_OV_ERR_SHIFT 0
99 #define DMAD6348_ST_OV_ERR_MASK (1 << DMAD6348_ST_OV_ERR_SHIFT)
100 #define DMAD6348_ST_CRC_ERR_SHIFT 1
101 #define DMAD6348_ST_CRC_ERR_MASK (1 << DMAD6348_ST_CRC_ERR_SHIFT)
102 #define DMAD6348_ST_RX_ERR_SHIFT 2
103 #define DMAD6348_ST_RX_ERR_MASK (1 << DMAD6348_ST_RX_ERR_SHIFT)
104 #define DMAD6348_ST_OS_ERR_SHIFT 4
105 #define DMAD6348_ST_OS_ERR_MASK (1 << DMAD6348_ST_OS_ERR_SHIFT)
106 #define DMAD6348_ST_UN_ERR_SHIFT 9
107 #define DMAD6348_ST_UN_ERR_MASK (1 << DMAD6348_ST_UN_ERR_SHIFT)
109 struct bcm6348_dma_desc {
115 struct bcm6348_chan_priv {
116 void __iomem *dma_ring;
117 uint8_t dma_ring_size;
124 struct bcm6348_iudma_hw {
128 struct bcm6348_iudma_priv {
129 const struct bcm6348_iudma_hw *hw;
133 struct bcm6348_chan_priv **ch_priv;
137 static inline bool bcm6348_iudma_chan_is_rx(uint8_t ch)
142 static inline void bcm6348_iudma_fdc(void *ptr, ulong size)
144 ulong start = (ulong) ptr;
146 flush_dcache_range(start, start + size);
149 static inline void bcm6348_iudma_idc(void *ptr, ulong size)
151 ulong start = (ulong) ptr;
153 invalidate_dcache_range(start, start + size);
156 static void bcm6348_iudma_chan_stop(struct bcm6348_iudma_priv *priv,
159 unsigned int timeout = DMA_CHAN_TOUT;
164 if (timeout > DMA_CHAN_TOUT / 2)
165 halt = DMAC_CFG_PKT_HALT_MASK;
167 halt = DMAC_CFG_BRST_HALT_MASK;
169 /* try to stop dma channel */
170 writel_be(halt, priv->chan + DMAC_CFG_REG(ch));
173 /* check if channel was stopped */
174 cfg = readl_be(priv->chan + DMAC_CFG_REG(ch));
175 if (!(cfg & DMAC_CFG_ENABLE_MASK))
182 pr_err("unable to stop channel %u\n", ch);
184 /* reset dma channel */
185 setbits_be32(priv->base + DMA_RST_REG, DMA_RST_CHAN_MASK(ch));
187 clrbits_be32(priv->base + DMA_RST_REG, DMA_RST_CHAN_MASK(ch));
190 static int bcm6348_iudma_disable(struct dma *dma)
192 struct bcm6348_iudma_priv *priv = dev_get_priv(dma->dev);
193 struct bcm6348_chan_priv *ch_priv = priv->ch_priv[dma->id];
195 /* stop dma channel */
196 bcm6348_iudma_chan_stop(priv, dma->id);
198 /* dma flow control */
199 if (bcm6348_iudma_chan_is_rx(dma->id))
200 writel_be(DMA_FLOWC_ALLOC_FORCE_MASK,
201 DMA_FLOWC_ALLOC_REG(dma->id));
203 /* init channel config */
204 ch_priv->running = false;
205 ch_priv->desc_id = 0;
206 if (bcm6348_iudma_chan_is_rx(dma->id))
207 ch_priv->desc_cnt = 0;
209 ch_priv->desc_cnt = ch_priv->dma_ring_size;
214 static int bcm6348_iudma_enable(struct dma *dma)
216 const struct bcm6348_iudma_priv *priv = dev_get_priv(dma->dev);
217 struct bcm6348_chan_priv *ch_priv = priv->ch_priv[dma->id];
218 struct bcm6348_dma_desc *dma_desc = ch_priv->dma_ring;
222 for (i = 0; i < ch_priv->desc_cnt; i++) {
223 if (bcm6348_iudma_chan_is_rx(dma->id)) {
224 ch_priv->busy_desc[i] = false;
225 dma_desc->status |= DMAD_ST_OWN_MASK;
227 dma_desc->status = 0;
228 dma_desc->length = 0;
229 dma_desc->address = 0;
232 if (i == ch_priv->desc_cnt - 1)
233 dma_desc->status |= DMAD_ST_WRAP_MASK;
238 /* init to first descriptor */
239 ch_priv->desc_id = 0;
241 /* force cache writeback */
242 bcm6348_iudma_fdc(ch_priv->dma_ring,
243 sizeof(*dma_desc) * ch_priv->desc_cnt);
246 writel_be(0, priv->sram + DMAS_STATE_DATA_REG(dma->id));
247 writel_be(0, priv->sram + DMAS_DESC_LEN_STATUS_REG(dma->id));
248 writel_be(0, priv->sram + DMAS_DESC_BASE_BUFPTR_REG(dma->id));
250 /* set dma ring start */
251 writel_be(virt_to_phys(ch_priv->dma_ring),
252 priv->sram + DMAS_RSTART_REG(dma->id));
254 /* set flow control */
255 if (bcm6348_iudma_chan_is_rx(dma->id)) {
258 setbits_be32(priv->base + DMA_CFG_REG,
259 DMA_CFG_FLOWC_ENABLE(dma->id));
261 val = ch_priv->desc_cnt / 3;
262 writel_be(val, priv->base + DMA_FLOWC_THR_LO_REG(dma->id));
264 val = (ch_priv->desc_cnt * 2) / 3;
265 writel_be(val, priv->base + DMA_FLOWC_THR_HI_REG(dma->id));
267 writel_be(0, priv->base + DMA_FLOWC_ALLOC_REG(dma->id));
270 /* set dma max burst */
271 writel_be(ch_priv->desc_cnt,
272 priv->chan + DMAC_BURST_REG(dma->id));
274 /* kick rx dma channel */
275 if (bcm6348_iudma_chan_is_rx(dma->id))
276 setbits_be32(priv->chan + DMAC_CFG_REG(dma->id),
277 DMAC_CFG_ENABLE_MASK);
279 /* channel is now enabled */
280 ch_priv->running = true;
285 static int bcm6348_iudma_request(struct dma *dma)
287 const struct bcm6348_iudma_priv *priv = dev_get_priv(dma->dev);
288 struct bcm6348_chan_priv *ch_priv;
290 /* check if channel is valid */
291 if (dma->id >= priv->n_channels)
294 /* alloc channel private data */
295 priv->ch_priv[dma->id] = calloc(1, sizeof(struct bcm6348_chan_priv));
296 if (!priv->ch_priv[dma->id])
298 ch_priv = priv->ch_priv[dma->id];
301 if (bcm6348_iudma_chan_is_rx(dma->id))
302 ch_priv->dma_ring_size = DMA_RX_DESC;
304 ch_priv->dma_ring_size = DMA_TX_DESC;
307 malloc_cache_aligned(sizeof(struct bcm6348_dma_desc) *
308 ch_priv->dma_ring_size);
309 if (!ch_priv->dma_ring)
312 /* init channel config */
313 ch_priv->running = false;
314 ch_priv->desc_id = 0;
315 if (bcm6348_iudma_chan_is_rx(dma->id)) {
316 ch_priv->desc_cnt = 0;
317 ch_priv->busy_desc = NULL;
319 ch_priv->desc_cnt = ch_priv->dma_ring_size;
320 ch_priv->busy_desc = calloc(ch_priv->desc_cnt, sizeof(bool));
326 static int bcm6348_iudma_receive(struct dma *dma, void **dst, void *metadata)
328 const struct bcm6348_iudma_priv *priv = dev_get_priv(dma->dev);
329 const struct bcm6348_iudma_hw *hw = priv->hw;
330 struct bcm6348_chan_priv *ch_priv = priv->ch_priv[dma->id];
331 struct bcm6348_dma_desc *dma_desc = dma_desc = ch_priv->dma_ring;
334 if (!ch_priv->running)
337 /* get dma ring descriptor address */
338 dma_desc += ch_priv->desc_id;
340 /* invalidate cache data */
341 bcm6348_iudma_idc(dma_desc, sizeof(*dma_desc));
344 if (dma_desc->status & DMAD_ST_OWN_MASK)
348 if (!(dma_desc->status & DMAD_ST_EOP_MASK) ||
349 !(dma_desc->status & DMAD_ST_SOP_MASK) ||
350 (dma_desc->status & hw->err_mask)) {
351 pr_err("invalid pkt received (ch=%ld desc=%u) (st=%04x)\n",
352 dma->id, ch_priv->desc_id, dma_desc->status);
355 /* set dma buffer address */
356 *dst = phys_to_virt(dma_desc->address);
358 /* invalidate cache data */
359 bcm6348_iudma_idc(*dst, dma_desc->length);
361 /* return packet length */
362 ret = dma_desc->length;
365 /* busy dma descriptor */
366 ch_priv->busy_desc[ch_priv->desc_id] = true;
368 /* increment dma descriptor */
369 ch_priv->desc_id = (ch_priv->desc_id + 1) % ch_priv->desc_cnt;
374 static int bcm6348_iudma_send(struct dma *dma, void *src, size_t len,
377 const struct bcm6348_iudma_priv *priv = dev_get_priv(dma->dev);
378 struct bcm6348_chan_priv *ch_priv = priv->ch_priv[dma->id];
379 struct bcm6348_dma_desc *dma_desc;
382 if (!ch_priv->running)
386 bcm6348_iudma_fdc(src, len);
388 /* get dma ring descriptor address */
389 dma_desc = ch_priv->dma_ring;
390 dma_desc += ch_priv->desc_id;
392 /* config dma descriptor */
393 status = (DMAD_ST_OWN_MASK |
397 if (ch_priv->desc_id == ch_priv->desc_cnt - 1)
398 status |= DMAD_ST_WRAP_MASK;
400 /* set dma descriptor */
401 dma_desc->address = virt_to_phys(src);
402 dma_desc->length = len;
403 dma_desc->status = status;
406 bcm6348_iudma_fdc(dma_desc, sizeof(*dma_desc));
408 /* kick tx dma channel */
409 setbits_be32(priv->chan + DMAC_CFG_REG(dma->id), DMAC_CFG_ENABLE_MASK);
411 /* poll dma status */
413 /* invalidate cache */
414 bcm6348_iudma_idc(dma_desc, sizeof(*dma_desc));
416 if (!(dma_desc->status & DMAD_ST_OWN_MASK))
420 /* increment dma descriptor */
421 ch_priv->desc_id = (ch_priv->desc_id + 1) % ch_priv->desc_cnt;
426 static int bcm6348_iudma_free_rcv_buf(struct dma *dma, void *dst, size_t size)
428 const struct bcm6348_iudma_priv *priv = dev_get_priv(dma->dev);
429 struct bcm6348_chan_priv *ch_priv = priv->ch_priv[dma->id];
430 struct bcm6348_dma_desc *dma_desc = ch_priv->dma_ring;
435 /* get dirty dma descriptor */
436 for (i = 0; i < ch_priv->desc_cnt; i++) {
437 if (phys_to_virt(dma_desc->address) == dst)
443 /* dma descriptor not found */
444 if (i == ch_priv->desc_cnt) {
445 pr_err("dirty dma descriptor not found\n");
449 /* invalidate cache */
450 bcm6348_iudma_idc(ch_priv->dma_ring,
451 sizeof(*dma_desc) * ch_priv->desc_cnt);
453 /* free dma descriptor */
454 ch_priv->busy_desc[i] = false;
456 status = DMAD_ST_OWN_MASK;
457 if (i == ch_priv->desc_cnt - 1)
458 status |= DMAD_ST_WRAP_MASK;
460 dma_desc->status |= status;
461 dma_desc->length = PKTSIZE_ALIGN;
463 /* tell dma we allocated one buffer */
464 writel_be(1, DMA_FLOWC_ALLOC_REG(dma->id));
467 bcm6348_iudma_fdc(ch_priv->dma_ring,
468 sizeof(*dma_desc) * ch_priv->desc_cnt);
470 /* kick rx dma channel if disabled */
471 cfg = readl_be(priv->chan + DMAC_CFG_REG(dma->id));
472 if (!(cfg & DMAC_CFG_ENABLE_MASK))
473 setbits_be32(priv->chan + DMAC_CFG_REG(dma->id),
474 DMAC_CFG_ENABLE_MASK);
479 static int bcm6348_iudma_add_rcv_buf(struct dma *dma, void *dst, size_t size)
481 const struct bcm6348_iudma_priv *priv = dev_get_priv(dma->dev);
482 struct bcm6348_chan_priv *ch_priv = priv->ch_priv[dma->id];
483 struct bcm6348_dma_desc *dma_desc = ch_priv->dma_ring;
485 /* no more dma descriptors available */
486 if (ch_priv->desc_cnt == ch_priv->dma_ring_size) {
487 pr_err("max number of buffers reached\n");
491 /* get next dma descriptor */
492 dma_desc += ch_priv->desc_cnt;
494 /* init dma descriptor */
495 dma_desc->address = virt_to_phys(dst);
496 dma_desc->length = size;
497 dma_desc->status = 0;
500 bcm6348_iudma_fdc(dma_desc, sizeof(*dma_desc));
502 /* increment dma descriptors */
508 static int bcm6348_iudma_prepare_rcv_buf(struct dma *dma, void *dst,
511 const struct bcm6348_iudma_priv *priv = dev_get_priv(dma->dev);
512 struct bcm6348_chan_priv *ch_priv = priv->ch_priv[dma->id];
514 /* only add new rx buffers if channel isn't running */
515 if (ch_priv->running)
516 return bcm6348_iudma_free_rcv_buf(dma, dst, size);
518 return bcm6348_iudma_add_rcv_buf(dma, dst, size);
521 static const struct dma_ops bcm6348_iudma_ops = {
522 .disable = bcm6348_iudma_disable,
523 .enable = bcm6348_iudma_enable,
524 .prepare_rcv_buf = bcm6348_iudma_prepare_rcv_buf,
525 .request = bcm6348_iudma_request,
526 .receive = bcm6348_iudma_receive,
527 .send = bcm6348_iudma_send,
530 static const struct bcm6348_iudma_hw bcm6348_hw = {
531 .err_mask = (DMAD6348_ST_OV_ERR_MASK |
532 DMAD6348_ST_CRC_ERR_MASK |
533 DMAD6348_ST_RX_ERR_MASK |
534 DMAD6348_ST_OS_ERR_MASK |
535 DMAD6348_ST_UN_ERR_MASK),
538 static const struct bcm6348_iudma_hw bcm6368_hw = {
542 static const struct udevice_id bcm6348_iudma_ids[] = {
544 .compatible = "brcm,bcm6348-iudma",
545 .data = (ulong)&bcm6348_hw,
547 .compatible = "brcm,bcm6368-iudma",
548 .data = (ulong)&bcm6368_hw,
549 }, { /* sentinel */ }
552 static int bcm6348_iudma_probe(struct udevice *dev)
554 struct dma_dev_priv *uc_priv = dev_get_uclass_priv(dev);
555 struct bcm6348_iudma_priv *priv = dev_get_priv(dev);
556 const struct bcm6348_iudma_hw *hw =
557 (const struct bcm6348_iudma_hw *)dev_get_driver_data(dev);
561 uc_priv->supported = (DMA_SUPPORTS_DEV_TO_MEM |
562 DMA_SUPPORTS_MEM_TO_DEV);
565 /* dma global base address */
566 priv->base = dev_remap_addr_name(dev, "dma");
570 /* dma channels base address */
571 priv->chan = dev_remap_addr_name(dev, "dma-channels");
575 /* dma sram base address */
576 priv->sram = dev_remap_addr_name(dev, "dma-sram");
580 /* get number of channels */
581 priv->n_channels = dev_read_u32_default(dev, "dma-channels", 8);
582 if (priv->n_channels > DMA_CHAN_MAX)
585 /* try to enable clocks */
590 ret = clk_get_by_index(dev, i, &clk);
594 ret = clk_enable(&clk);
596 pr_err("error enabling clock %d\n", i);
601 /* try to perform resets */
603 struct reset_ctl reset;
606 ret = reset_get_by_index(dev, i, &reset);
610 ret = reset_deassert(&reset);
612 pr_err("error deasserting reset %d\n", i);
616 ret = reset_free(&reset);
618 pr_err("error freeing reset %d\n", i);
623 /* disable dma controller */
624 clrbits_be32(priv->base + DMA_CFG_REG, DMA_CFG_ENABLE_MASK);
626 /* alloc channel private data pointers */
627 priv->ch_priv = calloc(priv->n_channels,
628 sizeof(struct bcm6348_chan_priv*));
632 /* stop dma channels */
633 for (ch = 0; ch < priv->n_channels; ch++)
634 bcm6348_iudma_chan_stop(priv, ch);
636 /* enable dma controller */
637 setbits_be32(priv->base + DMA_CFG_REG, DMA_CFG_ENABLE_MASK);
642 U_BOOT_DRIVER(bcm6348_iudma) = {
643 .name = "bcm6348_iudma",
645 .of_match = bcm6348_iudma_ids,
646 .ops = &bcm6348_iudma_ops,
647 .priv_auto = sizeof(struct bcm6348_iudma_priv),
648 .probe = bcm6348_iudma_probe,