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> 28 #include <linux/genalloc.h> 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 /* 42 * Host event IRQ numbers from PRUSS - PRUSS can generate up to 8 interrupt 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; 68 unsigned long sram_vaddr; 69 void *ddr_vaddr; 70 unsigned int hostirq_start; 71 unsigned int pintc_base; 72 struct gen_pool *sram_pool; 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 94 static void pruss_cleanup(struct platform_device *dev, 95 struct uio_pruss_dev *gdev) 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) { 106 dma_free_coherent(&dev->dev, extram_pool_sz, gdev->ddr_vaddr, 107 gdev->ddr_paddr); 108 } 109 if (gdev->sram_vaddr) 110 gen_pool_free(gdev->sram_pool, 111 gdev->sram_vaddr, 112 sram_pool_sz); 113 kfree(gdev->info); 114 clk_put(gdev->pruss_clk); 115 kfree(gdev); 116 } 117 118 static int pruss_probe(struct platform_device *dev) 119 { 120 struct uio_info *p; 121 struct uio_pruss_dev *gdev; 122 struct resource *regs_prussio; 123 int ret = -ENODEV, cnt = 0, len; 124 struct uio_pruss_pdata *pdata = dev_get_platdata(&dev->dev); 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 } 135 /* Power on PRU in case its not done as part of boot-loader */ 136 gdev->pruss_clk = clk_get(&dev->dev, "pruss"); 137 if (IS_ERR(gdev->pruss_clk)) { 138 dev_err(&dev->dev, "Failed to get clock\n"); 139 ret = PTR_ERR(gdev->pruss_clk); 140 kfree(gdev->info); 141 kfree(gdev); 142 return ret; 143 } else { 144 clk_enable(gdev->pruss_clk); 145 } 146 147 regs_prussio = platform_get_resource(dev, IORESOURCE_MEM, 0); 148 if (!regs_prussio) { 149 dev_err(&dev->dev, "No PRUSS I/O resource specified\n"); 150 goto out_free; 151 } 152 153 if (!regs_prussio->start) { 154 dev_err(&dev->dev, "Invalid memory resource\n"); 155 goto out_free; 156 } 157 158 if (pdata->sram_pool) { 159 gdev->sram_pool = pdata->sram_pool; 160 gdev->sram_vaddr = 161 gen_pool_alloc(gdev->sram_pool, sram_pool_sz); 162 if (!gdev->sram_vaddr) { 163 dev_err(&dev->dev, "Could not allocate SRAM pool\n"); 164 goto out_free; 165 } 166 gdev->sram_paddr = 167 gen_pool_virt_to_phys(gdev->sram_pool, 168 gdev->sram_vaddr); 169 } 170 171 gdev->ddr_vaddr = dma_alloc_coherent(&dev->dev, extram_pool_sz, 172 &(gdev->ddr_paddr), GFP_KERNEL | GFP_DMA); 173 if (!gdev->ddr_vaddr) { 174 dev_err(&dev->dev, "Could not allocate external memory\n"); 175 goto out_free; 176 } 177 178 len = resource_size(regs_prussio); 179 gdev->prussio_vaddr = ioremap(regs_prussio->start, len); 180 if (!gdev->prussio_vaddr) { 181 dev_err(&dev->dev, "Can't remap PRUSS I/O address range\n"); 182 goto out_free; 183 } 184 185 gdev->pintc_base = pdata->pintc_base; 186 gdev->hostirq_start = platform_get_irq(dev, 0); 187 188 for (cnt = 0, p = gdev->info; cnt < MAX_PRUSS_EVT; cnt++, p++) { 189 p->mem[0].addr = regs_prussio->start; 190 p->mem[0].size = resource_size(regs_prussio); 191 p->mem[0].memtype = UIO_MEM_PHYS; 192 193 p->mem[1].addr = gdev->sram_paddr; 194 p->mem[1].size = sram_pool_sz; 195 p->mem[1].memtype = UIO_MEM_PHYS; 196 197 p->mem[2].addr = gdev->ddr_paddr; 198 p->mem[2].size = extram_pool_sz; 199 p->mem[2].memtype = UIO_MEM_PHYS; 200 201 p->name = kasprintf(GFP_KERNEL, "pruss_evt%d", cnt); 202 p->version = DRV_VERSION; 203 204 /* Register PRUSS IRQ lines */ 205 p->irq = gdev->hostirq_start + cnt; 206 p->handler = pruss_handler; 207 p->priv = gdev; 208 209 ret = uio_register_device(&dev->dev, p); 210 if (ret < 0) 211 goto out_free; 212 } 213 214 platform_set_drvdata(dev, gdev); 215 return 0; 216 217 out_free: 218 pruss_cleanup(dev, gdev); 219 return ret; 220 } 221 222 static int pruss_remove(struct platform_device *dev) 223 { 224 struct uio_pruss_dev *gdev = platform_get_drvdata(dev); 225 226 pruss_cleanup(dev, gdev); 227 return 0; 228 } 229 230 static struct platform_driver pruss_driver = { 231 .probe = pruss_probe, 232 .remove = pruss_remove, 233 .driver = { 234 .name = DRV_NAME, 235 .owner = THIS_MODULE, 236 }, 237 }; 238 239 module_platform_driver(pruss_driver); 240 241 MODULE_LICENSE("GPL v2"); 242 MODULE_VERSION(DRV_VERSION); 243 MODULE_AUTHOR("Amit Chatterjee <amit.chatterjee@ti.com>"); 244 MODULE_AUTHOR("Pratheesh Gangadhar <pratheesh@ti.com>"); 245