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> |
Matwey V. Kornilov | 8767267 | 2015-06-10 20:00:19 +0300 | [diff] [blame] | 27 | #include <linux/sizes.h> |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 28 | #include <linux/slab.h> |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 29 | #include <linux/genalloc.h> |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 30 | |
| 31 | #define DRV_NAME "pruss_uio" |
| 32 | #define DRV_VERSION "1.0" |
| 33 | |
| 34 | static int sram_pool_sz = SZ_16K; |
| 35 | module_param(sram_pool_sz, int, 0); |
| 36 | MODULE_PARM_DESC(sram_pool_sz, "sram pool size to allocate "); |
| 37 | |
| 38 | static int extram_pool_sz = SZ_256K; |
| 39 | module_param(extram_pool_sz, int, 0); |
| 40 | MODULE_PARM_DESC(extram_pool_sz, "external ram pool size to allocate"); |
| 41 | |
| 42 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 43 | * 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] | 44 | * events to AINTC of ARM host processor - which can be used for IPC b/w PRUSS |
| 45 | * firmware and user space application, async notification from PRU firmware |
| 46 | * to user space application |
| 47 | * 3 PRU_EVTOUT0 |
| 48 | * 4 PRU_EVTOUT1 |
| 49 | * 5 PRU_EVTOUT2 |
| 50 | * 6 PRU_EVTOUT3 |
| 51 | * 7 PRU_EVTOUT4 |
| 52 | * 8 PRU_EVTOUT5 |
| 53 | * 9 PRU_EVTOUT6 |
| 54 | * 10 PRU_EVTOUT7 |
| 55 | */ |
| 56 | #define MAX_PRUSS_EVT 8 |
| 57 | |
| 58 | #define PINTC_HIDISR 0x0038 |
| 59 | #define PINTC_HIPIR 0x0900 |
| 60 | #define HIPIR_NOPEND 0x80000000 |
| 61 | #define PINTC_HIER 0x1500 |
| 62 | |
| 63 | struct uio_pruss_dev { |
| 64 | struct uio_info *info; |
| 65 | struct clk *pruss_clk; |
| 66 | dma_addr_t sram_paddr; |
| 67 | dma_addr_t ddr_paddr; |
| 68 | void __iomem *prussio_vaddr; |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 69 | unsigned long sram_vaddr; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 70 | void *ddr_vaddr; |
| 71 | unsigned int hostirq_start; |
| 72 | unsigned int pintc_base; |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 73 | struct gen_pool *sram_pool; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | static irqreturn_t pruss_handler(int irq, struct uio_info *info) |
| 77 | { |
| 78 | struct uio_pruss_dev *gdev = info->priv; |
| 79 | int intr_bit = (irq - gdev->hostirq_start + 2); |
| 80 | int val, intr_mask = (1 << intr_bit); |
| 81 | void __iomem *base = gdev->prussio_vaddr + gdev->pintc_base; |
| 82 | void __iomem *intren_reg = base + PINTC_HIER; |
| 83 | void __iomem *intrdis_reg = base + PINTC_HIDISR; |
| 84 | void __iomem *intrstat_reg = base + PINTC_HIPIR + (intr_bit << 2); |
| 85 | |
| 86 | val = ioread32(intren_reg); |
| 87 | /* Is interrupt enabled and active ? */ |
| 88 | if (!(val & intr_mask) && (ioread32(intrstat_reg) & HIPIR_NOPEND)) |
| 89 | return IRQ_NONE; |
| 90 | /* Disable interrupt */ |
| 91 | iowrite32(intr_bit, intrdis_reg); |
| 92 | return IRQ_HANDLED; |
| 93 | } |
| 94 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 95 | static void pruss_cleanup(struct device *dev, struct uio_pruss_dev *gdev) |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 96 | { |
| 97 | int cnt; |
| 98 | struct uio_info *p = gdev->info; |
| 99 | |
| 100 | for (cnt = 0; cnt < MAX_PRUSS_EVT; cnt++, p++) { |
| 101 | uio_unregister_device(p); |
| 102 | kfree(p->name); |
| 103 | } |
| 104 | iounmap(gdev->prussio_vaddr); |
| 105 | if (gdev->ddr_vaddr) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 106 | dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr, |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 107 | gdev->ddr_paddr); |
| 108 | } |
| 109 | if (gdev->sram_vaddr) |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 110 | gen_pool_free(gdev->sram_pool, |
| 111 | gdev->sram_vaddr, |
| 112 | sram_pool_sz); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 113 | kfree(gdev->info); |
Alexey Khoroshilov | e663c5d | 2016-11-26 00:55:30 +0300 | [diff] [blame] | 114 | clk_disable(gdev->pruss_clk); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 115 | clk_put(gdev->pruss_clk); |
| 116 | kfree(gdev); |
| 117 | } |
| 118 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 119 | static int pruss_probe(struct platform_device *pdev) |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 120 | { |
| 121 | struct uio_info *p; |
| 122 | struct uio_pruss_dev *gdev; |
| 123 | struct resource *regs_prussio; |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 124 | struct device *dev = &pdev->dev; |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 125 | int ret, cnt, i, len; |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 126 | struct uio_pruss_pdata *pdata = dev_get_platdata(dev); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 127 | |
| 128 | gdev = kzalloc(sizeof(struct uio_pruss_dev), GFP_KERNEL); |
| 129 | if (!gdev) |
| 130 | return -ENOMEM; |
| 131 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 132 | gdev->info = kcalloc(MAX_PRUSS_EVT, sizeof(*p), GFP_KERNEL); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 133 | if (!gdev->info) { |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 134 | ret = -ENOMEM; |
| 135 | goto err_free_gdev; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 136 | } |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 137 | |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 138 | /* 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] | 139 | gdev->pruss_clk = clk_get(dev, "pruss"); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 140 | if (IS_ERR(gdev->pruss_clk)) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 141 | dev_err(dev, "Failed to get clock\n"); |
Emil Goode | cb3771b | 2013-06-06 15:55:28 +0200 | [diff] [blame] | 142 | ret = PTR_ERR(gdev->pruss_clk); |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 143 | goto err_free_info; |
| 144 | } |
| 145 | |
| 146 | ret = clk_enable(gdev->pruss_clk); |
| 147 | if (ret) { |
| 148 | dev_err(dev, "Failed to enable clock\n"); |
| 149 | goto err_clk_put; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 150 | } |
| 151 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 152 | regs_prussio = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 153 | if (!regs_prussio) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 154 | dev_err(dev, "No PRUSS I/O resource specified\n"); |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 155 | ret = -EIO; |
| 156 | goto err_clk_disable; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if (!regs_prussio->start) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 160 | dev_err(dev, "Invalid memory resource\n"); |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 161 | ret = -EIO; |
| 162 | goto err_clk_disable; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 163 | } |
| 164 | |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 165 | if (pdata->sram_pool) { |
| 166 | gdev->sram_pool = pdata->sram_pool; |
| 167 | gdev->sram_vaddr = |
Nicolin Chen | 288342e | 2013-11-12 15:09:57 -0800 | [diff] [blame] | 168 | (unsigned long)gen_pool_dma_alloc(gdev->sram_pool, |
| 169 | sram_pool_sz, &gdev->sram_paddr); |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 170 | if (!gdev->sram_vaddr) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 171 | dev_err(dev, "Could not allocate SRAM pool\n"); |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 172 | ret = -ENOMEM; |
| 173 | goto err_clk_disable; |
Matt Porter | 2eb2478 | 2012-10-05 13:04:40 -0400 | [diff] [blame] | 174 | } |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 175 | } |
| 176 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 177 | gdev->ddr_vaddr = dma_alloc_coherent(dev, extram_pool_sz, |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 178 | &(gdev->ddr_paddr), GFP_KERNEL | GFP_DMA); |
| 179 | if (!gdev->ddr_vaddr) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 180 | dev_err(dev, "Could not allocate external memory\n"); |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 181 | ret = -ENOMEM; |
| 182 | goto err_free_sram; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | len = resource_size(regs_prussio); |
| 186 | gdev->prussio_vaddr = ioremap(regs_prussio->start, len); |
| 187 | if (!gdev->prussio_vaddr) { |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 188 | dev_err(dev, "Can't remap PRUSS I/O address range\n"); |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 189 | ret = -ENOMEM; |
| 190 | goto err_free_ddr_vaddr; |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | gdev->pintc_base = pdata->pintc_base; |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 194 | gdev->hostirq_start = platform_get_irq(pdev, 0); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 195 | |
| 196 | for (cnt = 0, p = gdev->info; cnt < MAX_PRUSS_EVT; cnt++, p++) { |
| 197 | p->mem[0].addr = regs_prussio->start; |
| 198 | p->mem[0].size = resource_size(regs_prussio); |
| 199 | p->mem[0].memtype = UIO_MEM_PHYS; |
| 200 | |
| 201 | p->mem[1].addr = gdev->sram_paddr; |
| 202 | p->mem[1].size = sram_pool_sz; |
| 203 | p->mem[1].memtype = UIO_MEM_PHYS; |
| 204 | |
| 205 | p->mem[2].addr = gdev->ddr_paddr; |
| 206 | p->mem[2].size = extram_pool_sz; |
| 207 | p->mem[2].memtype = UIO_MEM_PHYS; |
| 208 | |
| 209 | p->name = kasprintf(GFP_KERNEL, "pruss_evt%d", cnt); |
| 210 | p->version = DRV_VERSION; |
| 211 | |
| 212 | /* Register PRUSS IRQ lines */ |
| 213 | p->irq = gdev->hostirq_start + cnt; |
| 214 | p->handler = pruss_handler; |
| 215 | p->priv = gdev; |
| 216 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 217 | ret = uio_register_device(dev, p); |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 218 | if (ret < 0) { |
| 219 | kfree(p->name); |
| 220 | goto err_unloop; |
| 221 | } |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 222 | } |
| 223 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 224 | platform_set_drvdata(pdev, gdev); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 225 | return 0; |
| 226 | |
Dan Carpenter | 9588367 | 2018-08-02 10:15:58 +0300 | [diff] [blame] | 227 | err_unloop: |
| 228 | for (i = 0, p = gdev->info; i < cnt; i++, p++) { |
| 229 | uio_unregister_device(p); |
| 230 | kfree(p->name); |
| 231 | } |
| 232 | iounmap(gdev->prussio_vaddr); |
| 233 | err_free_ddr_vaddr: |
| 234 | dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr, |
| 235 | gdev->ddr_paddr); |
| 236 | err_free_sram: |
| 237 | if (pdata->sram_pool) |
| 238 | gen_pool_free(gdev->sram_pool, gdev->sram_vaddr, sram_pool_sz); |
| 239 | err_clk_disable: |
| 240 | clk_disable(gdev->pruss_clk); |
| 241 | err_clk_put: |
| 242 | clk_put(gdev->pruss_clk); |
| 243 | err_free_info: |
| 244 | kfree(gdev->info); |
| 245 | err_free_gdev: |
| 246 | kfree(gdev); |
| 247 | |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
Bill Pemberton | 9b96c31 | 2012-11-19 13:26:19 -0500 | [diff] [blame] | 251 | static int pruss_remove(struct platform_device *dev) |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 252 | { |
| 253 | struct uio_pruss_dev *gdev = platform_get_drvdata(dev); |
| 254 | |
Andre Heider | 4719ebf | 2014-06-29 18:21:35 +0200 | [diff] [blame] | 255 | pruss_cleanup(&dev->dev, gdev); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static struct platform_driver pruss_driver = { |
| 260 | .probe = pruss_probe, |
Bill Pemberton | 5a59509 | 2012-11-19 13:21:07 -0500 | [diff] [blame] | 261 | .remove = pruss_remove, |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 262 | .driver = { |
| 263 | .name = DRV_NAME, |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 264 | }, |
| 265 | }; |
| 266 | |
Axel Lin | 11e3123 | 2011-11-26 15:18:55 +0800 | [diff] [blame] | 267 | module_platform_driver(pruss_driver); |
Pratheesh Gangadhar | f1a304e | 2011-03-05 04:30:17 +0530 | [diff] [blame] | 268 | |
| 269 | MODULE_LICENSE("GPL v2"); |
| 270 | MODULE_VERSION(DRV_VERSION); |
| 271 | MODULE_AUTHOR("Amit Chatterjee <amit.chatterjee@ti.com>"); |
| 272 | MODULE_AUTHOR("Pratheesh Gangadhar <pratheesh@ti.com>"); |