1dfec1a82SJohn Crispin /* 2dfec1a82SJohn Crispin * This program is free software; you can redistribute it and/or modify it 3dfec1a82SJohn Crispin * under the terms of the GNU General Public License version 2 as published 4dfec1a82SJohn Crispin * by the Free Software Foundation. 5dfec1a82SJohn Crispin * 6dfec1a82SJohn Crispin * This program is distributed in the hope that it will be useful, 7dfec1a82SJohn Crispin * but WITHOUT ANY WARRANTY; without even the implied warranty of 8dfec1a82SJohn Crispin * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9dfec1a82SJohn Crispin * GNU General Public License for more details. 10dfec1a82SJohn Crispin * 11dfec1a82SJohn Crispin * You should have received a copy of the GNU General Public License 12dfec1a82SJohn Crispin * along with this program; if not, write to the Free Software 13dfec1a82SJohn Crispin * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 14dfec1a82SJohn Crispin * 1597b92108SJohn Crispin * Copyright (C) 2011 John Crispin <john@phrozen.org> 16dfec1a82SJohn Crispin */ 17dfec1a82SJohn Crispin 18dfec1a82SJohn Crispin #include <linux/init.h> 19dfec1a82SJohn Crispin #include <linux/platform_device.h> 20dfec1a82SJohn Crispin #include <linux/io.h> 21dfec1a82SJohn Crispin #include <linux/dma-mapping.h> 22*26dd3e4fSPaul Gortmaker #include <linux/export.h> 2398e58b01SHauke Mehrtens #include <linux/spinlock.h> 24ddd4eecaSJohn Crispin #include <linux/clk.h> 257c390a7eSThierry Reding #include <linux/err.h> 26dfec1a82SJohn Crispin 27dfec1a82SJohn Crispin #include <lantiq_soc.h> 28dfec1a82SJohn Crispin #include <xway_dma.h> 29dfec1a82SJohn Crispin 30b8b3acbeSJohn Crispin #define LTQ_DMA_ID 0x08 31dfec1a82SJohn Crispin #define LTQ_DMA_CTRL 0x10 32dfec1a82SJohn Crispin #define LTQ_DMA_CPOLL 0x14 33dfec1a82SJohn Crispin #define LTQ_DMA_CS 0x18 34dfec1a82SJohn Crispin #define LTQ_DMA_CCTRL 0x1C 35dfec1a82SJohn Crispin #define LTQ_DMA_CDBA 0x20 36dfec1a82SJohn Crispin #define LTQ_DMA_CDLEN 0x24 37dfec1a82SJohn Crispin #define LTQ_DMA_CIS 0x28 38dfec1a82SJohn Crispin #define LTQ_DMA_CIE 0x2C 39dfec1a82SJohn Crispin #define LTQ_DMA_PS 0x40 40dfec1a82SJohn Crispin #define LTQ_DMA_PCTRL 0x44 41dfec1a82SJohn Crispin #define LTQ_DMA_IRNEN 0xf4 42dfec1a82SJohn Crispin 43dfec1a82SJohn Crispin #define DMA_DESCPT BIT(3) /* descriptor complete irq */ 44dfec1a82SJohn Crispin #define DMA_TX BIT(8) /* TX channel direction */ 45dfec1a82SJohn Crispin #define DMA_CHAN_ON BIT(0) /* channel on / off bit */ 46dfec1a82SJohn Crispin #define DMA_PDEN BIT(6) /* enable packet drop */ 47dfec1a82SJohn Crispin #define DMA_CHAN_RST BIT(1) /* channel on / off bit */ 48dfec1a82SJohn Crispin #define DMA_RESET BIT(0) /* channel on / off bit */ 49dfec1a82SJohn Crispin #define DMA_IRQ_ACK 0x7e /* IRQ status register */ 50dfec1a82SJohn Crispin #define DMA_POLL BIT(31) /* turn on channel polling */ 51dfec1a82SJohn Crispin #define DMA_CLK_DIV4 BIT(6) /* polling clock divider */ 52dfec1a82SJohn Crispin #define DMA_2W_BURST BIT(1) /* 2 word burst length */ 53dfec1a82SJohn Crispin #define DMA_MAX_CHANNEL 20 /* the soc has 20 channels */ 54d08be0dbSMasanari Iida #define DMA_ETOP_ENDIANNESS (0xf << 8) /* endianness swap etop channels */ 55dfec1a82SJohn Crispin #define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */ 56dfec1a82SJohn Crispin 57dfec1a82SJohn Crispin #define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x)) 58dfec1a82SJohn Crispin #define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y)) 59dfec1a82SJohn Crispin #define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \ 60dfec1a82SJohn Crispin ltq_dma_membase + (z)) 61dfec1a82SJohn Crispin 62dfec1a82SJohn Crispin static void __iomem *ltq_dma_membase; 6398e58b01SHauke Mehrtens static DEFINE_SPINLOCK(ltq_dma_lock); 64dfec1a82SJohn Crispin 65dfec1a82SJohn Crispin void 66dfec1a82SJohn Crispin ltq_dma_enable_irq(struct ltq_dma_channel *ch) 67dfec1a82SJohn Crispin { 68dfec1a82SJohn Crispin unsigned long flags; 69dfec1a82SJohn Crispin 7098e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 71dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 72dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 7398e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 74dfec1a82SJohn Crispin } 75dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_enable_irq); 76dfec1a82SJohn Crispin 77dfec1a82SJohn Crispin void 78dfec1a82SJohn Crispin ltq_dma_disable_irq(struct ltq_dma_channel *ch) 79dfec1a82SJohn Crispin { 80dfec1a82SJohn Crispin unsigned long flags; 81dfec1a82SJohn Crispin 8298e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 83dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 84dfec1a82SJohn Crispin ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN); 8598e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 86dfec1a82SJohn Crispin } 87dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_disable_irq); 88dfec1a82SJohn Crispin 89dfec1a82SJohn Crispin void 90dfec1a82SJohn Crispin ltq_dma_ack_irq(struct ltq_dma_channel *ch) 91dfec1a82SJohn Crispin { 92dfec1a82SJohn Crispin unsigned long flags; 93dfec1a82SJohn Crispin 9498e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 95dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 96dfec1a82SJohn Crispin ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS); 9798e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 98dfec1a82SJohn Crispin } 99dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_ack_irq); 100dfec1a82SJohn Crispin 101dfec1a82SJohn Crispin void 102dfec1a82SJohn Crispin ltq_dma_open(struct ltq_dma_channel *ch) 103dfec1a82SJohn Crispin { 104dfec1a82SJohn Crispin unsigned long flag; 105dfec1a82SJohn Crispin 10698e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flag); 107dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 108dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL); 10998e58b01SHauke Mehrtens ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 11098e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flag); 111dfec1a82SJohn Crispin } 112dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_open); 113dfec1a82SJohn Crispin 114dfec1a82SJohn Crispin void 115dfec1a82SJohn Crispin ltq_dma_close(struct ltq_dma_channel *ch) 116dfec1a82SJohn Crispin { 117dfec1a82SJohn Crispin unsigned long flag; 118dfec1a82SJohn Crispin 11998e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flag); 120dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 121dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 12298e58b01SHauke Mehrtens ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN); 12398e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flag); 124dfec1a82SJohn Crispin } 125dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_close); 126dfec1a82SJohn Crispin 127dfec1a82SJohn Crispin static void 128dfec1a82SJohn Crispin ltq_dma_alloc(struct ltq_dma_channel *ch) 129dfec1a82SJohn Crispin { 130dfec1a82SJohn Crispin unsigned long flags; 131dfec1a82SJohn Crispin 132dfec1a82SJohn Crispin ch->desc = 0; 133dfec1a82SJohn Crispin ch->desc_base = dma_alloc_coherent(NULL, 134dfec1a82SJohn Crispin LTQ_DESC_NUM * LTQ_DESC_SIZE, 135dfec1a82SJohn Crispin &ch->phys, GFP_ATOMIC); 136dfec1a82SJohn Crispin memset(ch->desc_base, 0, LTQ_DESC_NUM * LTQ_DESC_SIZE); 137dfec1a82SJohn Crispin 13898e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 139dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 140dfec1a82SJohn Crispin ltq_dma_w32(ch->phys, LTQ_DMA_CDBA); 141dfec1a82SJohn Crispin ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN); 142dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 143dfec1a82SJohn Crispin wmb(); 144dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL); 145dfec1a82SJohn Crispin while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST) 146dfec1a82SJohn Crispin ; 14798e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 148dfec1a82SJohn Crispin } 149dfec1a82SJohn Crispin 150dfec1a82SJohn Crispin void 151dfec1a82SJohn Crispin ltq_dma_alloc_tx(struct ltq_dma_channel *ch) 152dfec1a82SJohn Crispin { 153dfec1a82SJohn Crispin unsigned long flags; 154dfec1a82SJohn Crispin 155dfec1a82SJohn Crispin ltq_dma_alloc(ch); 156dfec1a82SJohn Crispin 15798e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 158dfec1a82SJohn Crispin ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE); 159dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 160dfec1a82SJohn Crispin ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL); 16198e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 162dfec1a82SJohn Crispin } 163dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx); 164dfec1a82SJohn Crispin 165dfec1a82SJohn Crispin void 166dfec1a82SJohn Crispin ltq_dma_alloc_rx(struct ltq_dma_channel *ch) 167dfec1a82SJohn Crispin { 168dfec1a82SJohn Crispin unsigned long flags; 169dfec1a82SJohn Crispin 170dfec1a82SJohn Crispin ltq_dma_alloc(ch); 171dfec1a82SJohn Crispin 17298e58b01SHauke Mehrtens spin_lock_irqsave(<q_dma_lock, flags); 173dfec1a82SJohn Crispin ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE); 174dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 175dfec1a82SJohn Crispin ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL); 17698e58b01SHauke Mehrtens spin_unlock_irqrestore(<q_dma_lock, flags); 177dfec1a82SJohn Crispin } 178dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx); 179dfec1a82SJohn Crispin 180dfec1a82SJohn Crispin void 181dfec1a82SJohn Crispin ltq_dma_free(struct ltq_dma_channel *ch) 182dfec1a82SJohn Crispin { 183dfec1a82SJohn Crispin if (!ch->desc_base) 184dfec1a82SJohn Crispin return; 185dfec1a82SJohn Crispin ltq_dma_close(ch); 186dfec1a82SJohn Crispin dma_free_coherent(NULL, LTQ_DESC_NUM * LTQ_DESC_SIZE, 187dfec1a82SJohn Crispin ch->desc_base, ch->phys); 188dfec1a82SJohn Crispin } 189dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_free); 190dfec1a82SJohn Crispin 191dfec1a82SJohn Crispin void 192dfec1a82SJohn Crispin ltq_dma_init_port(int p) 193dfec1a82SJohn Crispin { 194dfec1a82SJohn Crispin ltq_dma_w32(p, LTQ_DMA_PS); 195dfec1a82SJohn Crispin switch (p) { 196dfec1a82SJohn Crispin case DMA_PORT_ETOP: 197dfec1a82SJohn Crispin /* 198d08be0dbSMasanari Iida * Tell the DMA engine to swap the endianness of data frames and 199dfec1a82SJohn Crispin * drop packets if the channel arbitration fails. 200dfec1a82SJohn Crispin */ 201d08be0dbSMasanari Iida ltq_dma_w32_mask(0, DMA_ETOP_ENDIANNESS | DMA_PDEN, 202dfec1a82SJohn Crispin LTQ_DMA_PCTRL); 203dfec1a82SJohn Crispin break; 204dfec1a82SJohn Crispin 205dfec1a82SJohn Crispin case DMA_PORT_DEU: 206dfec1a82SJohn Crispin ltq_dma_w32((DMA_2W_BURST << 4) | (DMA_2W_BURST << 2), 207dfec1a82SJohn Crispin LTQ_DMA_PCTRL); 208dfec1a82SJohn Crispin break; 209dfec1a82SJohn Crispin 210dfec1a82SJohn Crispin default: 211dfec1a82SJohn Crispin break; 212dfec1a82SJohn Crispin } 213dfec1a82SJohn Crispin } 214dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_init_port); 215dfec1a82SJohn Crispin 21628eb0e46SGreg Kroah-Hartman static int 217ddd4eecaSJohn Crispin ltq_dma_init(struct platform_device *pdev) 218dfec1a82SJohn Crispin { 219ddd4eecaSJohn Crispin struct clk *clk; 220ddd4eecaSJohn Crispin struct resource *res; 221b8b3acbeSJohn Crispin unsigned id; 222dfec1a82SJohn Crispin int i; 223dfec1a82SJohn Crispin 224ddd4eecaSJohn Crispin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2257c390a7eSThierry Reding ltq_dma_membase = devm_ioremap_resource(&pdev->dev, res); 2267c390a7eSThierry Reding if (IS_ERR(ltq_dma_membase)) 227ddd4eecaSJohn Crispin panic("Failed to remap dma resource"); 228dfec1a82SJohn Crispin 229dfec1a82SJohn Crispin /* power up and reset the dma engine */ 230ddd4eecaSJohn Crispin clk = clk_get(&pdev->dev, NULL); 231ddd4eecaSJohn Crispin if (IS_ERR(clk)) 232ddd4eecaSJohn Crispin panic("Failed to get dma clock"); 233ddd4eecaSJohn Crispin 234ddd4eecaSJohn Crispin clk_enable(clk); 235dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL); 236dfec1a82SJohn Crispin 237dfec1a82SJohn Crispin /* disable all interrupts */ 238dfec1a82SJohn Crispin ltq_dma_w32(0, LTQ_DMA_IRNEN); 239dfec1a82SJohn Crispin 240dfec1a82SJohn Crispin /* reset/configure each channel */ 241dfec1a82SJohn Crispin for (i = 0; i < DMA_MAX_CHANNEL; i++) { 242dfec1a82SJohn Crispin ltq_dma_w32(i, LTQ_DMA_CS); 243dfec1a82SJohn Crispin ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL); 244dfec1a82SJohn Crispin ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL); 245dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 246dfec1a82SJohn Crispin } 247b8b3acbeSJohn Crispin 248b8b3acbeSJohn Crispin id = ltq_dma_r32(LTQ_DMA_ID); 249b8b3acbeSJohn Crispin dev_info(&pdev->dev, 250b8b3acbeSJohn Crispin "Init done - hw rev: %X, ports: %d, channels: %d\n", 251b8b3acbeSJohn Crispin id & 0x1f, (id >> 16) & 0xf, id >> 20); 252b8b3acbeSJohn Crispin 253dfec1a82SJohn Crispin return 0; 254dfec1a82SJohn Crispin } 255dfec1a82SJohn Crispin 256ddd4eecaSJohn Crispin static const struct of_device_id dma_match[] = { 257ddd4eecaSJohn Crispin { .compatible = "lantiq,dma-xway" }, 258ddd4eecaSJohn Crispin {}, 259ddd4eecaSJohn Crispin }; 260ddd4eecaSJohn Crispin 261ddd4eecaSJohn Crispin static struct platform_driver dma_driver = { 262ddd4eecaSJohn Crispin .probe = ltq_dma_init, 263ddd4eecaSJohn Crispin .driver = { 264ddd4eecaSJohn Crispin .name = "dma-xway", 265ddd4eecaSJohn Crispin .of_match_table = dma_match, 266ddd4eecaSJohn Crispin }, 267ddd4eecaSJohn Crispin }; 268ddd4eecaSJohn Crispin 269ddd4eecaSJohn Crispin int __init 270ddd4eecaSJohn Crispin dma_init(void) 271ddd4eecaSJohn Crispin { 272ddd4eecaSJohn Crispin return platform_driver_register(&dma_driver); 273ddd4eecaSJohn Crispin } 274ddd4eecaSJohn Crispin 275ddd4eecaSJohn Crispin postcore_initcall(dma_init); 276