2019-05-30 02:57:47 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2011-05-06 02:10:00 +04:00
|
|
|
/*
|
|
|
|
*
|
2016-05-05 10:57:56 +03:00
|
|
|
* Copyright (C) 2011 John Crispin <john@phrozen.org>
|
2011-05-06 02:10:00 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
2017-01-29 05:05:57 +03:00
|
|
|
#include <linux/export.h>
|
2016-12-30 14:18:27 +03:00
|
|
|
#include <linux/spinlock.h>
|
2012-04-12 23:12:19 +04:00
|
|
|
#include <linux/clk.h>
|
2021-09-15 00:20:58 +03:00
|
|
|
#include <linux/delay.h>
|
2013-01-21 14:08:56 +04:00
|
|
|
#include <linux/err.h>
|
2018-09-13 11:31:09 +03:00
|
|
|
#include <linux/of.h>
|
2011-05-06 02:10:00 +04:00
|
|
|
|
|
|
|
#include <lantiq_soc.h>
|
|
|
|
#include <xway_dma.h>
|
|
|
|
|
2012-11-09 15:16:14 +04:00
|
|
|
#define LTQ_DMA_ID 0x08
|
2011-05-06 02:10:00 +04:00
|
|
|
#define LTQ_DMA_CTRL 0x10
|
|
|
|
#define LTQ_DMA_CPOLL 0x14
|
|
|
|
#define LTQ_DMA_CS 0x18
|
|
|
|
#define LTQ_DMA_CCTRL 0x1C
|
|
|
|
#define LTQ_DMA_CDBA 0x20
|
|
|
|
#define LTQ_DMA_CDLEN 0x24
|
|
|
|
#define LTQ_DMA_CIS 0x28
|
|
|
|
#define LTQ_DMA_CIE 0x2C
|
|
|
|
#define LTQ_DMA_PS 0x40
|
|
|
|
#define LTQ_DMA_PCTRL 0x44
|
|
|
|
#define LTQ_DMA_IRNEN 0xf4
|
|
|
|
|
2021-09-15 00:20:59 +03:00
|
|
|
#define DMA_ID_CHNR GENMASK(26, 20) /* channel number */
|
2011-05-06 02:10:00 +04:00
|
|
|
#define DMA_DESCPT BIT(3) /* descriptor complete irq */
|
|
|
|
#define DMA_TX BIT(8) /* TX channel direction */
|
|
|
|
#define DMA_CHAN_ON BIT(0) /* channel on / off bit */
|
|
|
|
#define DMA_PDEN BIT(6) /* enable packet drop */
|
|
|
|
#define DMA_CHAN_RST BIT(1) /* channel on / off bit */
|
|
|
|
#define DMA_RESET BIT(0) /* channel on / off bit */
|
|
|
|
#define DMA_IRQ_ACK 0x7e /* IRQ status register */
|
|
|
|
#define DMA_POLL BIT(31) /* turn on channel polling */
|
|
|
|
#define DMA_CLK_DIV4 BIT(6) /* polling clock divider */
|
2021-09-15 00:21:00 +03:00
|
|
|
#define DMA_PCTRL_2W_BURST 0x1 /* 2 word burst length */
|
|
|
|
#define DMA_PCTRL_4W_BURST 0x2 /* 4 word burst length */
|
|
|
|
#define DMA_PCTRL_8W_BURST 0x3 /* 8 word burst length */
|
|
|
|
#define DMA_TX_BURST_SHIFT 4 /* tx burst shift */
|
|
|
|
#define DMA_RX_BURST_SHIFT 2 /* rx burst shift */
|
2012-11-22 20:05:13 +04:00
|
|
|
#define DMA_ETOP_ENDIANNESS (0xf << 8) /* endianness swap etop channels */
|
2011-05-06 02:10:00 +04:00
|
|
|
#define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */
|
|
|
|
|
|
|
|
#define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x))
|
|
|
|
#define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y))
|
|
|
|
#define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \
|
|
|
|
ltq_dma_membase + (z))
|
|
|
|
|
|
|
|
static void __iomem *ltq_dma_membase;
|
2016-12-30 14:18:27 +03:00
|
|
|
static DEFINE_SPINLOCK(ltq_dma_lock);
|
2011-05-06 02:10:00 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
ltq_dma_enable_irq(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_lock_irqsave(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(ch->nr, LTQ_DMA_CS);
|
|
|
|
ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_unlock_irqrestore(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_enable_irq);
|
|
|
|
|
|
|
|
void
|
|
|
|
ltq_dma_disable_irq(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_lock_irqsave(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(ch->nr, LTQ_DMA_CS);
|
|
|
|
ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_unlock_irqrestore(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_disable_irq);
|
|
|
|
|
|
|
|
void
|
|
|
|
ltq_dma_ack_irq(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_lock_irqsave(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(ch->nr, LTQ_DMA_CS);
|
|
|
|
ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS);
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_unlock_irqrestore(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_ack_irq);
|
|
|
|
|
|
|
|
void
|
|
|
|
ltq_dma_open(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
unsigned long flag;
|
|
|
|
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_lock_irqsave(<q_dma_lock, flag);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(ch->nr, LTQ_DMA_CS);
|
|
|
|
ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL);
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_unlock_irqrestore(<q_dma_lock, flag);
|
2011-05-06 02:10:00 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_open);
|
|
|
|
|
|
|
|
void
|
|
|
|
ltq_dma_close(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
unsigned long flag;
|
|
|
|
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_lock_irqsave(<q_dma_lock, flag);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(ch->nr, LTQ_DMA_CS);
|
|
|
|
ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
|
2016-12-30 14:18:27 +03:00
|
|
|
ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN);
|
|
|
|
spin_unlock_irqrestore(<q_dma_lock, flag);
|
2011-05-06 02:10:00 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_close);
|
|
|
|
|
|
|
|
static void
|
|
|
|
ltq_dma_alloc(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
ch->desc = 0;
|
cross-tree: phase out dma_zalloc_coherent()
We already need to zero out memory for dma_alloc_coherent(), as such
using dma_zalloc_coherent() is superflous. Phase it out.
This change was generated with the following Coccinelle SmPL patch:
@ replace_dma_zalloc_coherent @
expression dev, size, data, handle, flags;
@@
-dma_zalloc_coherent(dev, size, handle, flags)
+dma_alloc_coherent(dev, size, handle, flags)
Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
[hch: re-ran the script on the latest tree]
Signed-off-by: Christoph Hellwig <hch@lst.de>
2019-01-04 11:23:09 +03:00
|
|
|
ch->desc_base = dma_alloc_coherent(ch->dev,
|
|
|
|
LTQ_DESC_NUM * LTQ_DESC_SIZE,
|
|
|
|
&ch->phys, GFP_ATOMIC);
|
2011-05-06 02:10:00 +04:00
|
|
|
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_lock_irqsave(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(ch->nr, LTQ_DMA_CS);
|
|
|
|
ltq_dma_w32(ch->phys, LTQ_DMA_CDBA);
|
|
|
|
ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN);
|
|
|
|
ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
|
|
|
|
wmb();
|
|
|
|
ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL);
|
|
|
|
while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST)
|
|
|
|
;
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_unlock_irqrestore(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ltq_dma_alloc_tx(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
ltq_dma_alloc(ch);
|
|
|
|
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_lock_irqsave(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
|
|
|
|
ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
|
|
|
|
ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL);
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_unlock_irqrestore(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx);
|
|
|
|
|
|
|
|
void
|
|
|
|
ltq_dma_alloc_rx(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
ltq_dma_alloc(ch);
|
|
|
|
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_lock_irqsave(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE);
|
|
|
|
ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN);
|
|
|
|
ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL);
|
2016-12-30 14:18:27 +03:00
|
|
|
spin_unlock_irqrestore(<q_dma_lock, flags);
|
2011-05-06 02:10:00 +04:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx);
|
|
|
|
|
|
|
|
void
|
|
|
|
ltq_dma_free(struct ltq_dma_channel *ch)
|
|
|
|
{
|
|
|
|
if (!ch->desc_base)
|
|
|
|
return;
|
|
|
|
ltq_dma_close(ch);
|
2018-09-09 22:26:23 +03:00
|
|
|
dma_free_coherent(ch->dev, LTQ_DESC_NUM * LTQ_DESC_SIZE,
|
2011-05-06 02:10:00 +04:00
|
|
|
ch->desc_base, ch->phys);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_free);
|
|
|
|
|
|
|
|
void
|
2021-09-15 00:21:01 +03:00
|
|
|
ltq_dma_init_port(int p, int tx_burst, int rx_burst)
|
2011-05-06 02:10:00 +04:00
|
|
|
{
|
|
|
|
ltq_dma_w32(p, LTQ_DMA_PS);
|
|
|
|
switch (p) {
|
|
|
|
case DMA_PORT_ETOP:
|
|
|
|
/*
|
2012-11-22 20:05:13 +04:00
|
|
|
* Tell the DMA engine to swap the endianness of data frames and
|
2011-05-06 02:10:00 +04:00
|
|
|
* drop packets if the channel arbitration fails.
|
|
|
|
*/
|
2021-09-15 00:21:01 +03:00
|
|
|
ltq_dma_w32_mask(0, (DMA_ETOP_ENDIANNESS | DMA_PDEN),
|
2011-05-06 02:10:00 +04:00
|
|
|
LTQ_DMA_PCTRL);
|
|
|
|
break;
|
|
|
|
|
2021-09-15 00:21:01 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (rx_burst) {
|
|
|
|
case 8:
|
|
|
|
ltq_dma_w32_mask(0x0c, (DMA_PCTRL_8W_BURST << DMA_RX_BURST_SHIFT),
|
2011-05-06 02:10:00 +04:00
|
|
|
LTQ_DMA_PCTRL);
|
|
|
|
break;
|
2021-09-15 00:21:01 +03:00
|
|
|
case 4:
|
|
|
|
ltq_dma_w32_mask(0x0c, (DMA_PCTRL_4W_BURST << DMA_RX_BURST_SHIFT),
|
|
|
|
LTQ_DMA_PCTRL);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ltq_dma_w32_mask(0x0c, (DMA_PCTRL_2W_BURST << DMA_RX_BURST_SHIFT),
|
|
|
|
LTQ_DMA_PCTRL);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2011-05-06 02:10:00 +04:00
|
|
|
|
2021-09-15 00:21:01 +03:00
|
|
|
switch (tx_burst) {
|
|
|
|
case 8:
|
|
|
|
ltq_dma_w32_mask(0x30, (DMA_PCTRL_8W_BURST << DMA_TX_BURST_SHIFT),
|
|
|
|
LTQ_DMA_PCTRL);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
ltq_dma_w32_mask(0x30, (DMA_PCTRL_4W_BURST << DMA_TX_BURST_SHIFT),
|
|
|
|
LTQ_DMA_PCTRL);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ltq_dma_w32_mask(0x30, (DMA_PCTRL_2W_BURST << DMA_TX_BURST_SHIFT),
|
|
|
|
LTQ_DMA_PCTRL);
|
|
|
|
break;
|
2011-05-06 02:10:00 +04:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(ltq_dma_init_port);
|
|
|
|
|
2012-12-22 02:04:39 +04:00
|
|
|
static int
|
2012-04-12 23:12:19 +04:00
|
|
|
ltq_dma_init(struct platform_device *pdev)
|
2011-05-06 02:10:00 +04:00
|
|
|
{
|
2012-04-12 23:12:19 +04:00
|
|
|
struct clk *clk;
|
|
|
|
struct resource *res;
|
2021-09-15 00:20:59 +03:00
|
|
|
unsigned int id, nchannels;
|
2011-05-06 02:10:00 +04:00
|
|
|
int i;
|
|
|
|
|
2012-04-12 23:12:19 +04:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2013-01-21 14:08:56 +04:00
|
|
|
ltq_dma_membase = devm_ioremap_resource(&pdev->dev, res);
|
|
|
|
if (IS_ERR(ltq_dma_membase))
|
2012-04-12 23:12:19 +04:00
|
|
|
panic("Failed to remap dma resource");
|
2011-05-06 02:10:00 +04:00
|
|
|
|
|
|
|
/* power up and reset the dma engine */
|
2012-04-12 23:12:19 +04:00
|
|
|
clk = clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(clk))
|
|
|
|
panic("Failed to get dma clock");
|
|
|
|
|
|
|
|
clk_enable(clk);
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL);
|
|
|
|
|
2021-09-15 00:20:58 +03:00
|
|
|
usleep_range(1, 10);
|
|
|
|
|
2011-05-06 02:10:00 +04:00
|
|
|
/* disable all interrupts */
|
|
|
|
ltq_dma_w32(0, LTQ_DMA_IRNEN);
|
|
|
|
|
|
|
|
/* reset/configure each channel */
|
2021-09-15 00:20:59 +03:00
|
|
|
id = ltq_dma_r32(LTQ_DMA_ID);
|
|
|
|
nchannels = ((id & DMA_ID_CHNR) >> 20);
|
|
|
|
for (i = 0; i < nchannels; i++) {
|
2011-05-06 02:10:00 +04:00
|
|
|
ltq_dma_w32(i, LTQ_DMA_CS);
|
|
|
|
ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL);
|
|
|
|
ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL);
|
|
|
|
ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL);
|
|
|
|
}
|
2012-11-09 15:16:14 +04:00
|
|
|
|
|
|
|
dev_info(&pdev->dev,
|
|
|
|
"Init done - hw rev: %X, ports: %d, channels: %d\n",
|
2021-09-15 00:20:59 +03:00
|
|
|
id & 0x1f, (id >> 16) & 0xf, nchannels);
|
2012-11-09 15:16:14 +04:00
|
|
|
|
2011-05-06 02:10:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-12 23:12:19 +04:00
|
|
|
static const struct of_device_id dma_match[] = {
|
|
|
|
{ .compatible = "lantiq,dma-xway" },
|
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct platform_driver dma_driver = {
|
|
|
|
.probe = ltq_dma_init,
|
|
|
|
.driver = {
|
|
|
|
.name = "dma-xway",
|
|
|
|
.of_match_table = dma_match,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
int __init
|
|
|
|
dma_init(void)
|
|
|
|
{
|
|
|
|
return platform_driver_register(&dma_driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
postcore_initcall(dma_init);
|