Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Programmable Real-Time Unit Sub System (PRUSS) UIO driver (uio_pruss) |
| 3 | * |
| 4 | * This driver exports PRUSS host event out interrupts and PRUSS, L3 RAM, |
| 5 | * and DDR RAM to user space for applications interacting with PRUSS firmware |
| 6 | * |
| 7 | * Copyright (C) 2010-11 Texas Instruments Incorporated - http://www.ti.com/ |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation version 2. |
| 12 | * |
| 13 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any |
| 14 | * kind, whether express or implied; without even the implied warranty |
| 15 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/moduleparam.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/uio_driver.h> |
| 23 | #include <linux/platform_data/uio_pruss.h> |
| 24 | #include <linux/io.h> |
| 25 | #include <linux/clk.h> |
| 26 | #include <linux/dma-mapping.h> |
| 27 | #include <linux/slab.h> |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 28 | #include <linux/genalloc.h> |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 29 | |
| 30 | #define DRV_NAME "pruss_uio" |
| 31 | #define DRV_VERSION "1.0" |
| 32 | |
| 33 | static int sram_pool_sz = SZ_16K; |
| 34 | module_param(sram_pool_sz, int, 0); |
| 35 | MODULE_PARM_DESC(sram_pool_sz, "sram pool size to allocate "); |
| 36 | |
| 37 | static int extram_pool_sz = SZ_256K; |
| 38 | module_param(extram_pool_sz, int, 0); |
| 39 | MODULE_PARM_DESC(extram_pool_sz, "external ram pool size to allocate"); |
| 40 | |
| 41 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 42 | * Host event IRQ numbers from PRUSS - PRUSS can generate up to 8 interrupt |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 43 | * events to AINTC of ARM host processor - which can be used for IPC b/w PRUSS |
| 44 | * firmware and user space application, async notification from PRU firmware |
| 45 | * to user space application |
| 46 | * 3 PRU_EVTOUT0 |
| 47 | * 4 PRU_EVTOUT1 |
| 48 | * 5 PRU_EVTOUT2 |
| 49 | * 6 PRU_EVTOUT3 |
| 50 | * 7 PRU_EVTOUT4 |
| 51 | * 8 PRU_EVTOUT5 |
| 52 | * 9 PRU_EVTOUT6 |
| 53 | * 10 PRU_EVTOUT7 |
| 54 | */ |
| 55 | #define MAX_PRUSS_EVT 8 |
| 56 | |
| 57 | #define PINTC_HIDISR 0x0038 |
| 58 | #define PINTC_HIPIR 0x0900 |
| 59 | #define HIPIR_NOPEND 0x80000000 |
| 60 | #define PINTC_HIER 0x1500 |
| 61 | |
| 62 | struct uio_pruss_dev { |
| 63 | struct uio_info *info; |
| 64 | struct clk *pruss_clk; |
| 65 | dma_addr_t sram_paddr; |
| 66 | dma_addr_t ddr_paddr; |
| 67 | void __iomem *prussio_vaddr; |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 68 | unsigned long sram_vaddr; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 69 | void *ddr_vaddr; |
| 70 | unsigned int hostirq_start; |
| 71 | unsigned int pintc_base; |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 72 | struct gen_pool *sram_pool; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | static irqreturn_t pruss_handler(int irq, struct uio_info *info) |
| 76 | { |
| 77 | struct uio_pruss_dev *gdev = info->priv; |
| 78 | int intr_bit = (irq - gdev->hostirq_start + 2); |
| 79 | int val, intr_mask = (1 << intr_bit); |
| 80 | void __iomem *base = gdev->prussio_vaddr + gdev->pintc_base; |
| 81 | void __iomem *intren_reg = base + PINTC_HIER; |
| 82 | void __iomem *intrdis_reg = base + PINTC_HIDISR; |
| 83 | void __iomem *intrstat_reg = base + PINTC_HIPIR + (intr_bit << 2); |
| 84 | |
| 85 | val = ioread32(intren_reg); |
| 86 | /* Is interrupt enabled and active ? */ |
| 87 | if (!(val & intr_mask) && (ioread32(intrstat_reg) & HIPIR_NOPEND)) |
| 88 | return IRQ_NONE; |
| 89 | /* Disable interrupt */ |
| 90 | iowrite32(intr_bit, intrdis_reg); |
| 91 | return IRQ_HANDLED; |
| 92 | } |
| 93 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 94 | static void pruss_cleanup(struct device *dev, struct uio_pruss_dev *gdev) |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 95 | { |
| 96 | int cnt; |
| 97 | struct uio_info *p = gdev->info; |
| 98 | |
| 99 | for (cnt = 0; cnt < MAX_PRUSS_EVT; cnt++, p++) { |
| 100 | uio_unregister_device(p); |
| 101 | kfree(p->name); |
| 102 | } |
| 103 | iounmap(gdev->prussio_vaddr); |
| 104 | if (gdev->ddr_vaddr) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 105 | dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr, |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 106 | gdev->ddr_paddr); |
| 107 | } |
| 108 | if (gdev->sram_vaddr) |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 109 | gen_pool_free(gdev->sram_pool, |
| 110 | gdev->sram_vaddr, |
| 111 | sram_pool_sz); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 112 | kfree(gdev->info); |
| 113 | clk_put(gdev->pruss_clk); |
| 114 | kfree(gdev); |
| 115 | } |
| 116 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 117 | static int pruss_probe(struct platform_device *pdev) |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 118 | { |
| 119 | struct uio_info *p; |
| 120 | struct uio_pruss_dev *gdev; |
| 121 | struct resource *regs_prussio; |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 122 | struct device *dev = &pdev->dev; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 123 | int ret = -ENODEV, cnt = 0, len; |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 124 | struct uio_pruss_pdata *pdata = dev_get_platdata(dev); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 125 | |
| 126 | gdev = kzalloc(sizeof(struct uio_pruss_dev), GFP_KERNEL); |
| 127 | if (!gdev) |
| 128 | return -ENOMEM; |
| 129 | |
| 130 | gdev->info = kzalloc(sizeof(*p) * MAX_PRUSS_EVT, GFP_KERNEL); |
| 131 | if (!gdev->info) { |
| 132 | kfree(gdev); |
| 133 | return -ENOMEM; |
| 134 | } |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 135 | |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 136 | /* Power on PRU in case its not done as part of boot-loader */ |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 137 | gdev->pruss_clk = clk_get(dev, "pruss"); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 138 | if (IS_ERR(gdev->pruss_clk)) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 139 | dev_err(dev, "Failed to get clock\n"); |
Emil Goode | cb3771b | 2013-06-06 15:55:28 +0200 | [diff] [blame] | 140 | ret = PTR_ERR(gdev->pruss_clk); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 141 | kfree(gdev->info); |
| 142 | kfree(gdev); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 143 | return ret; |
| 144 | } else { |
| 145 | clk_enable(gdev->pruss_clk); |
| 146 | } |
| 147 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 148 | regs_prussio = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 149 | if (!regs_prussio) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 150 | dev_err(dev, "No PRUSS I/O resource specified\n"); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 151 | goto out_free; |
| 152 | } |
| 153 | |
| 154 | if (!regs_prussio->start) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 155 | dev_err(dev, "Invalid memory resource\n"); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 156 | goto out_free; |
| 157 | } |
| 158 | |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 159 | if (pdata->sram_pool) { |
| 160 | gdev->sram_pool = pdata->sram_pool; |
| 161 | gdev->sram_vaddr = |
Nicolin Chen | 288342e | 2013-11-12 15:09:57 -0800 | [diff] [blame] | 162 | (unsigned long)gen_pool_dma_alloc(gdev->sram_pool, |
| 163 | sram_pool_sz, &gdev->sram_paddr); |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 164 | if (!gdev->sram_vaddr) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 165 | dev_err(dev, "Could not allocate SRAM pool\n"); |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 166 | goto out_free; |
| 167 | } |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 168 | } |
| 169 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 170 | gdev->ddr_vaddr = dma_alloc_coherent(dev, extram_pool_sz, |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 171 | &(gdev->ddr_paddr), GFP_KERNEL | GFP_DMA); |
| 172 | if (!gdev->ddr_vaddr) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 173 | dev_err(dev, "Could not allocate external memory\n"); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 174 | goto out_free; |
| 175 | } |
| 176 | |
| 177 | len = resource_size(regs_prussio); |
| 178 | gdev->prussio_vaddr = ioremap(regs_prussio->start, len); |
| 179 | if (!gdev->prussio_vaddr) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 180 | dev_err(dev, "Can't remap PRUSS I/O address range\n"); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 181 | goto out_free; |
| 182 | } |
| 183 | |
| 184 | gdev->pintc_base = pdata->pintc_base; |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 185 | gdev->hostirq_start = platform_get_irq(pdev, 0); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 186 | |
| 187 | for (cnt = 0, p = gdev->info; cnt < MAX_PRUSS_EVT; cnt++, p++) { |
| 188 | p->mem[0].addr = regs_prussio->start; |
| 189 | p->mem[0].size = resource_size(regs_prussio); |
| 190 | p->mem[0].memtype = UIO_MEM_PHYS; |
| 191 | |
| 192 | p->mem[1].addr = gdev->sram_paddr; |
| 193 | p->mem[1].size = sram_pool_sz; |
| 194 | p->mem[1].memtype = UIO_MEM_PHYS; |
| 195 | |
| 196 | p->mem[2].addr = gdev->ddr_paddr; |
| 197 | p->mem[2].size = extram_pool_sz; |
| 198 | p->mem[2].memtype = UIO_MEM_PHYS; |
| 199 | |
| 200 | p->name = kasprintf(GFP_KERNEL, "pruss_evt%d", cnt); |
| 201 | p->version = DRV_VERSION; |
| 202 | |
| 203 | /* Register PRUSS IRQ lines */ |
| 204 | p->irq = gdev->hostirq_start + cnt; |
| 205 | p->handler = pruss_handler; |
| 206 | p->priv = gdev; |
| 207 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 208 | ret = uio_register_device(dev, p); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 209 | if (ret < 0) |
| 210 | goto out_free; |
| 211 | } |
| 212 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 213 | platform_set_drvdata(pdev, gdev); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 214 | return 0; |
| 215 | |
| 216 | out_free: |
| 217 | pruss_cleanup(dev, gdev); |
| 218 | return ret; |
| 219 | } |
| 220 | |
Bill Pemberton | 9b96c31 | 2012-11-19 13:26:19 -0500 | [diff] [blame] | 221 | static int pruss_remove(struct platform_device *dev) |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 222 | { |
| 223 | struct uio_pruss_dev *gdev = platform_get_drvdata(dev); |
| 224 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 225 | pruss_cleanup(&dev->dev, gdev); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static struct platform_driver pruss_driver = { |
| 230 | .probe = pruss_probe, |
Bill Pemberton | 5a59509 | 2012-11-19 13:21:07 -0500 | [diff] [blame] | 231 | .remove = pruss_remove, |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 232 | .driver = { |
| 233 | .name = DRV_NAME, |
| 234 | .owner = THIS_MODULE, |
| 235 | }, |
| 236 | }; |
| 237 | |
Axel Lin | 11e3123 | 2011-11-26 15:18:55 +0800 | [diff] [blame] | 238 | module_platform_driver(pruss_driver); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 239 | |
| 240 | MODULE_LICENSE("GPL v2"); |
| 241 | MODULE_VERSION(DRV_VERSION); |
| 242 | MODULE_AUTHOR("Amit Chatterjee <amit.chatterjee@ti.com>"); |
| 243 | MODULE_AUTHOR("Pratheesh Gangadhar <pratheesh@ti.com>"); |