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 * 15*97b92108SJohn 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> 22ddd4eecaSJohn Crispin #include <linux/module.h> 23ddd4eecaSJohn Crispin #include <linux/clk.h> 247c390a7eSThierry Reding #include <linux/err.h> 25dfec1a82SJohn Crispin 26dfec1a82SJohn Crispin #include <lantiq_soc.h> 27dfec1a82SJohn Crispin #include <xway_dma.h> 28dfec1a82SJohn Crispin 29b8b3acbeSJohn Crispin #define LTQ_DMA_ID 0x08 30dfec1a82SJohn Crispin #define LTQ_DMA_CTRL 0x10 31dfec1a82SJohn Crispin #define LTQ_DMA_CPOLL 0x14 32dfec1a82SJohn Crispin #define LTQ_DMA_CS 0x18 33dfec1a82SJohn Crispin #define LTQ_DMA_CCTRL 0x1C 34dfec1a82SJohn Crispin #define LTQ_DMA_CDBA 0x20 35dfec1a82SJohn Crispin #define LTQ_DMA_CDLEN 0x24 36dfec1a82SJohn Crispin #define LTQ_DMA_CIS 0x28 37dfec1a82SJohn Crispin #define LTQ_DMA_CIE 0x2C 38dfec1a82SJohn Crispin #define LTQ_DMA_PS 0x40 39dfec1a82SJohn Crispin #define LTQ_DMA_PCTRL 0x44 40dfec1a82SJohn Crispin #define LTQ_DMA_IRNEN 0xf4 41dfec1a82SJohn Crispin 42dfec1a82SJohn Crispin #define DMA_DESCPT BIT(3) /* descriptor complete irq */ 43dfec1a82SJohn Crispin #define DMA_TX BIT(8) /* TX channel direction */ 44dfec1a82SJohn Crispin #define DMA_CHAN_ON BIT(0) /* channel on / off bit */ 45dfec1a82SJohn Crispin #define DMA_PDEN BIT(6) /* enable packet drop */ 46dfec1a82SJohn Crispin #define DMA_CHAN_RST BIT(1) /* channel on / off bit */ 47dfec1a82SJohn Crispin #define DMA_RESET BIT(0) /* channel on / off bit */ 48dfec1a82SJohn Crispin #define DMA_IRQ_ACK 0x7e /* IRQ status register */ 49dfec1a82SJohn Crispin #define DMA_POLL BIT(31) /* turn on channel polling */ 50dfec1a82SJohn Crispin #define DMA_CLK_DIV4 BIT(6) /* polling clock divider */ 51dfec1a82SJohn Crispin #define DMA_2W_BURST BIT(1) /* 2 word burst length */ 52dfec1a82SJohn Crispin #define DMA_MAX_CHANNEL 20 /* the soc has 20 channels */ 53d08be0dbSMasanari Iida #define DMA_ETOP_ENDIANNESS (0xf << 8) /* endianness swap etop channels */ 54dfec1a82SJohn Crispin #define DMA_WEIGHT (BIT(17) | BIT(16)) /* default channel wheight */ 55dfec1a82SJohn Crispin 56dfec1a82SJohn Crispin #define ltq_dma_r32(x) ltq_r32(ltq_dma_membase + (x)) 57dfec1a82SJohn Crispin #define ltq_dma_w32(x, y) ltq_w32(x, ltq_dma_membase + (y)) 58dfec1a82SJohn Crispin #define ltq_dma_w32_mask(x, y, z) ltq_w32_mask(x, y, \ 59dfec1a82SJohn Crispin ltq_dma_membase + (z)) 60dfec1a82SJohn Crispin 61dfec1a82SJohn Crispin static void __iomem *ltq_dma_membase; 62dfec1a82SJohn Crispin 63dfec1a82SJohn Crispin void 64dfec1a82SJohn Crispin ltq_dma_enable_irq(struct ltq_dma_channel *ch) 65dfec1a82SJohn Crispin { 66dfec1a82SJohn Crispin unsigned long flags; 67dfec1a82SJohn Crispin 68dfec1a82SJohn Crispin local_irq_save(flags); 69dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 70dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 71dfec1a82SJohn Crispin local_irq_restore(flags); 72dfec1a82SJohn Crispin } 73dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_enable_irq); 74dfec1a82SJohn Crispin 75dfec1a82SJohn Crispin void 76dfec1a82SJohn Crispin ltq_dma_disable_irq(struct ltq_dma_channel *ch) 77dfec1a82SJohn Crispin { 78dfec1a82SJohn Crispin unsigned long flags; 79dfec1a82SJohn Crispin 80dfec1a82SJohn Crispin local_irq_save(flags); 81dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 82dfec1a82SJohn Crispin ltq_dma_w32_mask(1 << ch->nr, 0, LTQ_DMA_IRNEN); 83dfec1a82SJohn Crispin local_irq_restore(flags); 84dfec1a82SJohn Crispin } 85dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_disable_irq); 86dfec1a82SJohn Crispin 87dfec1a82SJohn Crispin void 88dfec1a82SJohn Crispin ltq_dma_ack_irq(struct ltq_dma_channel *ch) 89dfec1a82SJohn Crispin { 90dfec1a82SJohn Crispin unsigned long flags; 91dfec1a82SJohn Crispin 92dfec1a82SJohn Crispin local_irq_save(flags); 93dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 94dfec1a82SJohn Crispin ltq_dma_w32(DMA_IRQ_ACK, LTQ_DMA_CIS); 95dfec1a82SJohn Crispin local_irq_restore(flags); 96dfec1a82SJohn Crispin } 97dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_ack_irq); 98dfec1a82SJohn Crispin 99dfec1a82SJohn Crispin void 100dfec1a82SJohn Crispin ltq_dma_open(struct ltq_dma_channel *ch) 101dfec1a82SJohn Crispin { 102dfec1a82SJohn Crispin unsigned long flag; 103dfec1a82SJohn Crispin 104dfec1a82SJohn Crispin local_irq_save(flag); 105dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 106dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_CHAN_ON, LTQ_DMA_CCTRL); 107dfec1a82SJohn Crispin ltq_dma_enable_irq(ch); 108dfec1a82SJohn Crispin local_irq_restore(flag); 109dfec1a82SJohn Crispin } 110dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_open); 111dfec1a82SJohn Crispin 112dfec1a82SJohn Crispin void 113dfec1a82SJohn Crispin ltq_dma_close(struct ltq_dma_channel *ch) 114dfec1a82SJohn Crispin { 115dfec1a82SJohn Crispin unsigned long flag; 116dfec1a82SJohn Crispin 117dfec1a82SJohn Crispin local_irq_save(flag); 118dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 119dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 120dfec1a82SJohn Crispin ltq_dma_disable_irq(ch); 121dfec1a82SJohn Crispin local_irq_restore(flag); 122dfec1a82SJohn Crispin } 123dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_close); 124dfec1a82SJohn Crispin 125dfec1a82SJohn Crispin static void 126dfec1a82SJohn Crispin ltq_dma_alloc(struct ltq_dma_channel *ch) 127dfec1a82SJohn Crispin { 128dfec1a82SJohn Crispin unsigned long flags; 129dfec1a82SJohn Crispin 130dfec1a82SJohn Crispin ch->desc = 0; 131dfec1a82SJohn Crispin ch->desc_base = dma_alloc_coherent(NULL, 132dfec1a82SJohn Crispin LTQ_DESC_NUM * LTQ_DESC_SIZE, 133dfec1a82SJohn Crispin &ch->phys, GFP_ATOMIC); 134dfec1a82SJohn Crispin memset(ch->desc_base, 0, LTQ_DESC_NUM * LTQ_DESC_SIZE); 135dfec1a82SJohn Crispin 136dfec1a82SJohn Crispin local_irq_save(flags); 137dfec1a82SJohn Crispin ltq_dma_w32(ch->nr, LTQ_DMA_CS); 138dfec1a82SJohn Crispin ltq_dma_w32(ch->phys, LTQ_DMA_CDBA); 139dfec1a82SJohn Crispin ltq_dma_w32(LTQ_DESC_NUM, LTQ_DMA_CDLEN); 140dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 141dfec1a82SJohn Crispin wmb(); 142dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_CHAN_RST, LTQ_DMA_CCTRL); 143dfec1a82SJohn Crispin while (ltq_dma_r32(LTQ_DMA_CCTRL) & DMA_CHAN_RST) 144dfec1a82SJohn Crispin ; 145dfec1a82SJohn Crispin local_irq_restore(flags); 146dfec1a82SJohn Crispin } 147dfec1a82SJohn Crispin 148dfec1a82SJohn Crispin void 149dfec1a82SJohn Crispin ltq_dma_alloc_tx(struct ltq_dma_channel *ch) 150dfec1a82SJohn Crispin { 151dfec1a82SJohn Crispin unsigned long flags; 152dfec1a82SJohn Crispin 153dfec1a82SJohn Crispin ltq_dma_alloc(ch); 154dfec1a82SJohn Crispin 155dfec1a82SJohn Crispin local_irq_save(flags); 156dfec1a82SJohn Crispin ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE); 157dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 158dfec1a82SJohn Crispin ltq_dma_w32(DMA_WEIGHT | DMA_TX, LTQ_DMA_CCTRL); 159dfec1a82SJohn Crispin local_irq_restore(flags); 160dfec1a82SJohn Crispin } 161dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_alloc_tx); 162dfec1a82SJohn Crispin 163dfec1a82SJohn Crispin void 164dfec1a82SJohn Crispin ltq_dma_alloc_rx(struct ltq_dma_channel *ch) 165dfec1a82SJohn Crispin { 166dfec1a82SJohn Crispin unsigned long flags; 167dfec1a82SJohn Crispin 168dfec1a82SJohn Crispin ltq_dma_alloc(ch); 169dfec1a82SJohn Crispin 170dfec1a82SJohn Crispin local_irq_save(flags); 171dfec1a82SJohn Crispin ltq_dma_w32(DMA_DESCPT, LTQ_DMA_CIE); 172dfec1a82SJohn Crispin ltq_dma_w32_mask(0, 1 << ch->nr, LTQ_DMA_IRNEN); 173dfec1a82SJohn Crispin ltq_dma_w32(DMA_WEIGHT, LTQ_DMA_CCTRL); 174dfec1a82SJohn Crispin local_irq_restore(flags); 175dfec1a82SJohn Crispin } 176dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_alloc_rx); 177dfec1a82SJohn Crispin 178dfec1a82SJohn Crispin void 179dfec1a82SJohn Crispin ltq_dma_free(struct ltq_dma_channel *ch) 180dfec1a82SJohn Crispin { 181dfec1a82SJohn Crispin if (!ch->desc_base) 182dfec1a82SJohn Crispin return; 183dfec1a82SJohn Crispin ltq_dma_close(ch); 184dfec1a82SJohn Crispin dma_free_coherent(NULL, LTQ_DESC_NUM * LTQ_DESC_SIZE, 185dfec1a82SJohn Crispin ch->desc_base, ch->phys); 186dfec1a82SJohn Crispin } 187dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_free); 188dfec1a82SJohn Crispin 189dfec1a82SJohn Crispin void 190dfec1a82SJohn Crispin ltq_dma_init_port(int p) 191dfec1a82SJohn Crispin { 192dfec1a82SJohn Crispin ltq_dma_w32(p, LTQ_DMA_PS); 193dfec1a82SJohn Crispin switch (p) { 194dfec1a82SJohn Crispin case DMA_PORT_ETOP: 195dfec1a82SJohn Crispin /* 196d08be0dbSMasanari Iida * Tell the DMA engine to swap the endianness of data frames and 197dfec1a82SJohn Crispin * drop packets if the channel arbitration fails. 198dfec1a82SJohn Crispin */ 199d08be0dbSMasanari Iida ltq_dma_w32_mask(0, DMA_ETOP_ENDIANNESS | DMA_PDEN, 200dfec1a82SJohn Crispin LTQ_DMA_PCTRL); 201dfec1a82SJohn Crispin break; 202dfec1a82SJohn Crispin 203dfec1a82SJohn Crispin case DMA_PORT_DEU: 204dfec1a82SJohn Crispin ltq_dma_w32((DMA_2W_BURST << 4) | (DMA_2W_BURST << 2), 205dfec1a82SJohn Crispin LTQ_DMA_PCTRL); 206dfec1a82SJohn Crispin break; 207dfec1a82SJohn Crispin 208dfec1a82SJohn Crispin default: 209dfec1a82SJohn Crispin break; 210dfec1a82SJohn Crispin } 211dfec1a82SJohn Crispin } 212dfec1a82SJohn Crispin EXPORT_SYMBOL_GPL(ltq_dma_init_port); 213dfec1a82SJohn Crispin 21428eb0e46SGreg Kroah-Hartman static int 215ddd4eecaSJohn Crispin ltq_dma_init(struct platform_device *pdev) 216dfec1a82SJohn Crispin { 217ddd4eecaSJohn Crispin struct clk *clk; 218ddd4eecaSJohn Crispin struct resource *res; 219b8b3acbeSJohn Crispin unsigned id; 220dfec1a82SJohn Crispin int i; 221dfec1a82SJohn Crispin 222ddd4eecaSJohn Crispin res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2237c390a7eSThierry Reding ltq_dma_membase = devm_ioremap_resource(&pdev->dev, res); 2247c390a7eSThierry Reding if (IS_ERR(ltq_dma_membase)) 225ddd4eecaSJohn Crispin panic("Failed to remap dma resource"); 226dfec1a82SJohn Crispin 227dfec1a82SJohn Crispin /* power up and reset the dma engine */ 228ddd4eecaSJohn Crispin clk = clk_get(&pdev->dev, NULL); 229ddd4eecaSJohn Crispin if (IS_ERR(clk)) 230ddd4eecaSJohn Crispin panic("Failed to get dma clock"); 231ddd4eecaSJohn Crispin 232ddd4eecaSJohn Crispin clk_enable(clk); 233dfec1a82SJohn Crispin ltq_dma_w32_mask(0, DMA_RESET, LTQ_DMA_CTRL); 234dfec1a82SJohn Crispin 235dfec1a82SJohn Crispin /* disable all interrupts */ 236dfec1a82SJohn Crispin ltq_dma_w32(0, LTQ_DMA_IRNEN); 237dfec1a82SJohn Crispin 238dfec1a82SJohn Crispin /* reset/configure each channel */ 239dfec1a82SJohn Crispin for (i = 0; i < DMA_MAX_CHANNEL; i++) { 240dfec1a82SJohn Crispin ltq_dma_w32(i, LTQ_DMA_CS); 241dfec1a82SJohn Crispin ltq_dma_w32(DMA_CHAN_RST, LTQ_DMA_CCTRL); 242dfec1a82SJohn Crispin ltq_dma_w32(DMA_POLL | DMA_CLK_DIV4, LTQ_DMA_CPOLL); 243dfec1a82SJohn Crispin ltq_dma_w32_mask(DMA_CHAN_ON, 0, LTQ_DMA_CCTRL); 244dfec1a82SJohn Crispin } 245b8b3acbeSJohn Crispin 246b8b3acbeSJohn Crispin id = ltq_dma_r32(LTQ_DMA_ID); 247b8b3acbeSJohn Crispin dev_info(&pdev->dev, 248b8b3acbeSJohn Crispin "Init done - hw rev: %X, ports: %d, channels: %d\n", 249b8b3acbeSJohn Crispin id & 0x1f, (id >> 16) & 0xf, id >> 20); 250b8b3acbeSJohn Crispin 251dfec1a82SJohn Crispin return 0; 252dfec1a82SJohn Crispin } 253dfec1a82SJohn Crispin 254ddd4eecaSJohn Crispin static const struct of_device_id dma_match[] = { 255ddd4eecaSJohn Crispin { .compatible = "lantiq,dma-xway" }, 256ddd4eecaSJohn Crispin {}, 257ddd4eecaSJohn Crispin }; 258ddd4eecaSJohn Crispin MODULE_DEVICE_TABLE(of, dma_match); 259ddd4eecaSJohn Crispin 260ddd4eecaSJohn Crispin static struct platform_driver dma_driver = { 261ddd4eecaSJohn Crispin .probe = ltq_dma_init, 262ddd4eecaSJohn Crispin .driver = { 263ddd4eecaSJohn Crispin .name = "dma-xway", 264ddd4eecaSJohn Crispin .of_match_table = dma_match, 265ddd4eecaSJohn Crispin }, 266ddd4eecaSJohn Crispin }; 267ddd4eecaSJohn Crispin 268ddd4eecaSJohn Crispin int __init 269ddd4eecaSJohn Crispin dma_init(void) 270ddd4eecaSJohn Crispin { 271ddd4eecaSJohn Crispin return platform_driver_register(&dma_driver); 272ddd4eecaSJohn Crispin } 273ddd4eecaSJohn Crispin 274ddd4eecaSJohn Crispin postcore_initcall(dma_init); 275