1*45051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2dfec1a82SJohn Crispin /* 3dfec1a82SJohn Crispin * 497b92108SJohn Crispin * Copyright (C) 2011 John Crispin <john@phrozen.org> 5dfec1a82SJohn Crispin */ 6dfec1a82SJohn Crispin 7dfec1a82SJohn Crispin #include <linux/init.h> 8dfec1a82SJohn Crispin #include <linux/platform_device.h> 9dfec1a82SJohn Crispin #include <linux/io.h> 10dfec1a82SJohn Crispin #include <linux/dma-mapping.h> 1126dd3e4fSPaul Gortmaker #include <linux/export.h> 1298e58b01SHauke Mehrtens #include <linux/spinlock.h> 13ddd4eecaSJohn Crispin #include <linux/clk.h> 147c390a7eSThierry Reding #include <linux/err.h> 15dfec1a82SJohn Crispin 16dfec1a82SJohn Crispin #include <lantiq_soc.h> 17dfec1a82SJohn Crispin #include <xway_dma.h> 18dfec1a82SJohn Crispin 19b8b3acbeSJohn Crispin #define LTQ_DMA_ID 0x08 20dfec1a82SJohn Crispin #define LTQ_DMA_CTRL 0x10 21dfec1a82SJohn Crispin #define LTQ_DMA_CPOLL 0x14 22dfec1a82SJohn Crispin #define LTQ_DMA_CS 0x18 23dfec1a82SJohn Crispin #define LTQ_DMA_CCTRL 0x1C 24dfec1a82SJohn Crispin #define LTQ_DMA_CDBA 0x20 25dfec1a82SJohn Crispin #define LTQ_DMA_CDLEN 0x24 26dfec1a82SJohn Crispin #define LTQ_DMA_CIS 0x28 27dfec1a82SJohn Crispin #define LTQ_DMA_CIE 0x2C 28dfec1a82SJohn Crispin #define LTQ_DMA_PS 0x40 29dfec1a82SJohn Crispin #define LTQ_DMA_PCTRL 0x44 30dfec1a82SJohn Crispin #define LTQ_DMA_IRNEN 0xf4 31dfec1a82SJohn Crispin 32dfec1a82SJohn Crispin #define DMA_DESCPT BIT(3) /* descriptor complete irq */ 33dfec1a82SJohn Crispin #define DMA_TX BIT(8) /* TX channel direction */ 34dfec1a82SJohn Crispin #define DMA_CHAN_ON BIT(0) /* channel on / off bit */ 35dfec1a82SJohn Crispin #define DMA_PDEN BIT(6) /* enable packet drop */ 36dfec1a82SJohn Crispin #define DMA_CHAN_RST BIT(1) /* channel on / off bit */ 37dfec1a82SJohn Crispin #define DMA_RESET BIT(0) /* channel on / off bit */ 38dfec1a82SJohn Crispin #define DMA_IRQ_ACK 0x7e /* IRQ status register */ 39dfec1a82SJohn Crispin #define DMA_POLL BIT(31) /* turn on channel polling */ 40dfec1a82SJohn Crispin #define DMA_CLK_DIV4 BIT(6) /* polling clock divider */ 41dfec1a82SJohn Crispin #define DMA_2W_BURST BIT(1) /* 2 word burst length */ 42dfec1a82SJohn Crispin #define DMA_MAX_CHANNEL 20 /* the soc has 20 channels */ 43d08be0dbSMasanari Iida #define DMA_ETOP_ENDIANNESS (0xf << 8) /* endianness swap etop channels */ 44dfec1a82SJohn Crispin #define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */ 45dfec1a82SJohn Crispin 46dfec1a82SJohn Crispin #define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x)) 47dfec1a82SJohn Crispin #define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y)) 48dfec1a82SJohn Crispin #define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \ 49dfec1a82SJohn Crispin ltq_dma_membase + (z)) 50dfec1a82SJohn Crispin 51dfec1a82SJohn Crispin static void __iomem *ltq_dma_membase; 5298e58b01SHauke Mehrtens static DEFINE_SPINLOCK(ltq_dma_lock); 53dfec1a82SJohn Crispin 54dfec1a82SJohn Crispin void 55dfec1a82SJohn Crispin ltq_dma_enable_irq(struct ltq_dma_channel *ch) 56dfec1a82SJohn Crispin { 57dfec1a82SJohn Crispin unsigned long flags; 58dfec1a82SJohn Crispin 5998e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 60dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 61dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 6298e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 63dfec1a82SJohn Crispin } 64dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_enable_irq); 65dfec1a82SJohn Crispin 66dfec1a82SJohn Crispin void 67dfec1a82SJohn Crispin ltq_dma_disable_irq(struct ltq_dma_channel *ch) 68dfec1a82SJohn Crispin { 69dfec1a82SJohn Crispin unsigned long flags; 70dfec1a82SJohn Crispin 7198e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 72dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 73dfec1a82SJohn Crispin ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN); 7498e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 75dfec1a82SJohn Crispin } 76dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_disable_irq); 77dfec1a82SJohn Crispin 78dfec1a82SJohn Crispin void 79dfec1a82SJohn Crispin ltq_dma_ack_irq(struct ltq_dma_channel *ch) 80dfec1a82SJohn Crispin { 81dfec1a82SJohn Crispin unsigned long flags; 82dfec1a82SJohn Crispin 8398e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 84dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 85dfec1a82SJohn Crispin ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS); 8698e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 87dfec1a82SJohn Crispin } 88dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_ack_irq); 89dfec1a82SJohn Crispin 90dfec1a82SJohn Crispin void 91dfec1a82SJohn Crispin ltq_dma_open(struct ltq_dma_channel *ch) 92dfec1a82SJohn Crispin { 93dfec1a82SJohn Crispin unsigned long flag; 94dfec1a82SJohn Crispin 9598e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flag); 96dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 97dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL); 9898e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flag); 99dfec1a82SJohn Crispin } 100dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_open); 101dfec1a82SJohn Crispin 102dfec1a82SJohn Crispin void 103dfec1a82SJohn Crispin ltq_dma_close(struct ltq_dma_channel *ch) 104dfec1a82SJohn Crispin { 105dfec1a82SJohn Crispin unsigned long flag; 106dfec1a82SJohn Crispin 10798e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flag); 108dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 109dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 11098e58b01SHauke Mehrtens ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN); 11198e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flag); 112dfec1a82SJohn Crispin } 113dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_close); 114dfec1a82SJohn Crispin 115dfec1a82SJohn Crispin static void 116dfec1a82SJohn Crispin ltq_dma_alloc(struct ltq_dma_channel *ch) 117dfec1a82SJohn Crispin { 118dfec1a82SJohn Crispin unsigned long flags; 119dfec1a82SJohn Crispin 120dfec1a82SJohn Crispin ch->desc = 0; 121750afb08SLuis Chamberlain ch->desc_base = dma_alloc_coherent(ch->dev, 122dfec1a82SJohn Crispin LTQ_DESC_NUM * LTQ_DESC_SIZE, 123dfec1a82SJohn Crispin &ch->phys, GFP_ATOMIC); 124dfec1a82SJohn Crispin 12598e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 126dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 127dfec1a82SJohn Crispin ltq_dma_w32(ch->phys, LTQ_DMA_CDBA); 128dfec1a82SJohn Crispin ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN); 129dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 130dfec1a82SJohn Crispin wmb(); 131dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL); 132dfec1a82SJohn Crispin while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST) 133dfec1a82SJohn Crispin ; 13498e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 135dfec1a82SJohn Crispin } 136dfec1a82SJohn Crispin 137dfec1a82SJohn Crispin void 138dfec1a82SJohn Crispin ltq_dma_alloc_tx(struct ltq_dma_channel *ch) 139dfec1a82SJohn Crispin { 140dfec1a82SJohn Crispin unsigned long flags; 141dfec1a82SJohn Crispin 142dfec1a82SJohn Crispin ltq_dma_alloc(ch); 143dfec1a82SJohn Crispin 14498e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 145dfec1a82SJohn Crispin ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE); 146dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 147dfec1a82SJohn Crispin ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL); 14898e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 149dfec1a82SJohn Crispin } 150dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx); 151dfec1a82SJohn Crispin 152dfec1a82SJohn Crispin void 153dfec1a82SJohn Crispin ltq_dma_alloc_rx(struct ltq_dma_channel *ch) 154dfec1a82SJohn Crispin { 155dfec1a82SJohn Crispin unsigned long flags; 156dfec1a82SJohn Crispin 157dfec1a82SJohn Crispin ltq_dma_alloc(ch); 158dfec1a82SJohn Crispin 15998e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 160dfec1a82SJohn Crispin ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE); 161dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 162dfec1a82SJohn Crispin ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL); 16398e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 164dfec1a82SJohn Crispin } 165dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx); 166dfec1a82SJohn Crispin 167dfec1a82SJohn Crispin void 168dfec1a82SJohn Crispin ltq_dma_free(struct ltq_dma_channel *ch) 169dfec1a82SJohn Crispin { 170dfec1a82SJohn Crispin if (!ch->desc_base) 171dfec1a82SJohn Crispin return; 172dfec1a82SJohn Crispin ltq_dma_close(ch); 1732d946e5bSHauke Mehrtens dma_free_coherent(ch->dev, LTQ_DESC_NUM * LTQ_DESC_SIZE, 174dfec1a82SJohn Crispin ch->desc_base, ch->phys); 175dfec1a82SJohn Crispin } 176dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_free); 177dfec1a82SJohn Crispin 178dfec1a82SJohn Crispin void 179dfec1a82SJohn Crispin ltq_dma_init_port(int p) 180dfec1a82SJohn Crispin { 181dfec1a82SJohn Crispin ltq_dma_w32(p, LTQ_DMA_PS); 182dfec1a82SJohn Crispin switch (p) { 183dfec1a82SJohn Crispin case DMA_PORT_ETOP: 184dfec1a82SJohn Crispin /* 185d08be0dbSMasanari Iida * Tell the DMA engine to swap the endianness of data frames and 186dfec1a82SJohn Crispin * drop packets if the channel arbitration fails. 187dfec1a82SJohn Crispin */ 188d08be0dbSMasanari Iida ltq_dma_w32_mask(0, DMA_ETOP_ENDIANNESS | DMA_PDEN, 189dfec1a82SJohn Crispin LTQ_DMA_PCTRL); 190dfec1a82SJohn Crispin break; 191dfec1a82SJohn Crispin 192dfec1a82SJohn Crispin case DMA_PORT_DEU: 193dfec1a82SJohn Crispin ltq_dma_w32((DMA_2W_BURST << 4) | (DMA_2W_BURST << 2), 194dfec1a82SJohn Crispin LTQ_DMA_PCTRL); 195dfec1a82SJohn Crispin break; 196dfec1a82SJohn Crispin 197dfec1a82SJohn Crispin default: 198dfec1a82SJohn Crispin break; 199dfec1a82SJohn Crispin } 200dfec1a82SJohn Crispin } 201dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_init_port); 202dfec1a82SJohn Crispin 20328eb0e46SGreg Kroah-Hartman static int 204ddd4eecaSJohn Crispin ltq_dma_init(struct platform_device *pdev) 205dfec1a82SJohn Crispin { 206ddd4eecaSJohn Crispin struct clk *clk; 207ddd4eecaSJohn Crispin struct resource *res; 208b8b3acbeSJohn Crispin unsigned id; 209dfec1a82SJohn Crispin int i; 210dfec1a82SJohn Crispin 211ddd4eecaSJohn Crispin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2127c390a7eSThierry Reding ltq_dma_membase = devm_ioremap_resource(&pdev->dev, res); 2137c390a7eSThierry Reding if (IS_ERR(ltq_dma_membase)) 214ddd4eecaSJohn Crispin panic("Failed to remap dma resource"); 215dfec1a82SJohn Crispin 216dfec1a82SJohn Crispin /* power up and reset the dma engine */ 217ddd4eecaSJohn Crispin clk = clk_get(&pdev->dev, NULL); 218ddd4eecaSJohn Crispin if (IS_ERR(clk)) 219ddd4eecaSJohn Crispin panic("Failed to get dma clock"); 220ddd4eecaSJohn Crispin 221ddd4eecaSJohn Crispin clk_enable(clk); 222dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL); 223dfec1a82SJohn Crispin 224dfec1a82SJohn Crispin /* disable all interrupts */ 225dfec1a82SJohn Crispin ltq_dma_w32(0, LTQ_DMA_IRNEN); 226dfec1a82SJohn Crispin 227dfec1a82SJohn Crispin /* reset/configure each channel */ 228dfec1a82SJohn Crispin for (i = 0; i < DMA_MAX_CHANNEL; i++) { 229dfec1a82SJohn Crispin ltq_dma_w32(i, LTQ_DMA_CS); 230dfec1a82SJohn Crispin ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL); 231dfec1a82SJohn Crispin ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL); 232dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 233dfec1a82SJohn Crispin } 234b8b3acbeSJohn Crispin 235b8b3acbeSJohn Crispin id = ltq_dma_r32(LTQ_DMA_ID); 236b8b3acbeSJohn Crispin dev_info(&pdev->dev, 237b8b3acbeSJohn Crispin "Init done - hw rev: %X, ports: %d, channels: %d\n", 238b8b3acbeSJohn Crispin id & 0x1f, (id >> 16) & 0xf, id >> 20); 239b8b3acbeSJohn Crispin 240dfec1a82SJohn Crispin return 0; 241dfec1a82SJohn Crispin } 242dfec1a82SJohn Crispin 243ddd4eecaSJohn Crispin static const struct of_device_id dma_match[] = { 244ddd4eecaSJohn Crispin { .compatible = "lantiq,dma-xway" }, 245ddd4eecaSJohn Crispin {}, 246ddd4eecaSJohn Crispin }; 247ddd4eecaSJohn Crispin 248ddd4eecaSJohn Crispin static struct platform_driver dma_driver = { 249ddd4eecaSJohn Crispin .probe = ltq_dma_init, 250ddd4eecaSJohn Crispin .driver = { 251ddd4eecaSJohn Crispin .name = "dma-xway", 252ddd4eecaSJohn Crispin .of_match_table = dma_match, 253ddd4eecaSJohn Crispin }, 254ddd4eecaSJohn Crispin }; 255ddd4eecaSJohn Crispin 256ddd4eecaSJohn Crispin int __init 257ddd4eecaSJohn Crispin dma_init(void) 258ddd4eecaSJohn Crispin { 259ddd4eecaSJohn Crispin return platform_driver_register(&dma_driver); 260ddd4eecaSJohn Crispin } 261ddd4eecaSJohn Crispin 262ddd4eecaSJohn Crispin postcore_initcall(dma_init); 263