1 // SPDX-License-Identifier: GPL-2.0-only
3 * Programmable Real-Time Unit Sub System (PRUSS) UIO driver (uio_pruss)
5 * This driver exports PRUSS host event out interrupts and PRUSS, L3 RAM,
6 * and DDR RAM to user space for applications interacting with PRUSS firmware
8 * Copyright (C) 2010-11 Texas Instruments Incorporated - http://www.ti.com/
10 #include <linux/device.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/platform_device.h>
14 #include <linux/uio_driver.h>
15 #include <linux/platform_data/uio_pruss.h>
17 #include <linux/clk.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/sizes.h>
20 #include <linux/slab.h>
21 #include <linux/genalloc.h>
23 #define DRV_NAME "pruss_uio"
24 #define DRV_VERSION "1.0"
26 static int sram_pool_sz = SZ_16K;
27 module_param(sram_pool_sz, int, 0);
28 MODULE_PARM_DESC(sram_pool_sz, "sram pool size to allocate ");
30 static int extram_pool_sz = SZ_256K;
31 module_param(extram_pool_sz, int, 0);
32 MODULE_PARM_DESC(extram_pool_sz, "external ram pool size to allocate");
35 * Host event IRQ numbers from PRUSS - PRUSS can generate up to 8 interrupt
36 * events to AINTC of ARM host processor - which can be used for IPC b/w PRUSS
37 * firmware and user space application, async notification from PRU firmware
38 * to user space application
48 #define MAX_PRUSS_EVT 8
50 #define PINTC_HIDISR 0x0038
51 #define PINTC_HIPIR 0x0900
52 #define HIPIR_NOPEND 0x80000000
53 #define PINTC_HIER 0x1500
55 struct uio_pruss_dev {
56 struct uio_info *info;
57 struct clk *pruss_clk;
58 dma_addr_t sram_paddr;
60 void __iomem *prussio_vaddr;
61 unsigned long sram_vaddr;
63 unsigned int hostirq_start;
64 unsigned int pintc_base;
65 struct gen_pool *sram_pool;
68 static irqreturn_t pruss_handler(int irq, struct uio_info *info)
70 struct uio_pruss_dev *gdev = info->priv;
71 int intr_bit = (irq - gdev->hostirq_start + 2);
72 int val, intr_mask = (1 << intr_bit);
73 void __iomem *base = gdev->prussio_vaddr + gdev->pintc_base;
74 void __iomem *intren_reg = base + PINTC_HIER;
75 void __iomem *intrdis_reg = base + PINTC_HIDISR;
76 void __iomem *intrstat_reg = base + PINTC_HIPIR + (intr_bit << 2);
78 val = ioread32(intren_reg);
79 /* Is interrupt enabled and active ? */
80 if (!(val & intr_mask) && (ioread32(intrstat_reg) & HIPIR_NOPEND))
82 /* Disable interrupt */
83 iowrite32(intr_bit, intrdis_reg);
87 static void pruss_cleanup(struct device *dev, struct uio_pruss_dev *gdev)
90 struct uio_info *p = gdev->info;
92 for (cnt = 0; cnt < MAX_PRUSS_EVT; cnt++, p++) {
93 uio_unregister_device(p);
95 iounmap(gdev->prussio_vaddr);
96 if (gdev->ddr_vaddr) {
97 dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr,
100 if (gdev->sram_vaddr)
101 gen_pool_free(gdev->sram_pool,
104 clk_disable(gdev->pruss_clk);
107 static int pruss_probe(struct platform_device *pdev)
110 struct uio_pruss_dev *gdev;
111 struct resource *regs_prussio;
112 struct device *dev = &pdev->dev;
113 int ret, cnt, i, len;
114 struct uio_pruss_pdata *pdata = dev_get_platdata(dev);
116 gdev = devm_kzalloc(dev, sizeof(struct uio_pruss_dev), GFP_KERNEL);
120 gdev->info = devm_kcalloc(dev, MAX_PRUSS_EVT, sizeof(*p), GFP_KERNEL);
124 /* Power on PRU in case its not done as part of boot-loader */
125 gdev->pruss_clk = devm_clk_get(dev, "pruss");
126 if (IS_ERR(gdev->pruss_clk)) {
127 dev_err(dev, "Failed to get clock\n");
128 return PTR_ERR(gdev->pruss_clk);
131 ret = clk_enable(gdev->pruss_clk);
133 dev_err(dev, "Failed to enable clock\n");
137 regs_prussio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139 dev_err(dev, "No PRUSS I/O resource specified\n");
141 goto err_clk_disable;
144 if (!regs_prussio->start) {
145 dev_err(dev, "Invalid memory resource\n");
147 goto err_clk_disable;
150 if (pdata->sram_pool) {
151 gdev->sram_pool = pdata->sram_pool;
153 (unsigned long)gen_pool_dma_alloc(gdev->sram_pool,
154 sram_pool_sz, &gdev->sram_paddr);
155 if (!gdev->sram_vaddr) {
156 dev_err(dev, "Could not allocate SRAM pool\n");
158 goto err_clk_disable;
162 gdev->ddr_vaddr = dma_alloc_coherent(dev, extram_pool_sz,
163 &(gdev->ddr_paddr), GFP_KERNEL | GFP_DMA);
164 if (!gdev->ddr_vaddr) {
165 dev_err(dev, "Could not allocate external memory\n");
170 len = resource_size(regs_prussio);
171 gdev->prussio_vaddr = ioremap(regs_prussio->start, len);
172 if (!gdev->prussio_vaddr) {
173 dev_err(dev, "Can't remap PRUSS I/O address range\n");
175 goto err_free_ddr_vaddr;
178 gdev->pintc_base = pdata->pintc_base;
179 gdev->hostirq_start = platform_get_irq(pdev, 0);
181 for (cnt = 0, p = gdev->info; cnt < MAX_PRUSS_EVT; cnt++, p++) {
182 p->mem[0].addr = regs_prussio->start;
183 p->mem[0].size = resource_size(regs_prussio);
184 p->mem[0].memtype = UIO_MEM_PHYS;
186 p->mem[1].addr = gdev->sram_paddr;
187 p->mem[1].size = sram_pool_sz;
188 p->mem[1].memtype = UIO_MEM_PHYS;
190 p->mem[2].addr = gdev->ddr_paddr;
191 p->mem[2].size = extram_pool_sz;
192 p->mem[2].memtype = UIO_MEM_PHYS;
194 p->name = devm_kasprintf(dev, GFP_KERNEL, "pruss_evt%d", cnt);
195 p->version = DRV_VERSION;
197 /* Register PRUSS IRQ lines */
198 p->irq = gdev->hostirq_start + cnt;
199 p->handler = pruss_handler;
202 ret = uio_register_device(dev, p);
207 platform_set_drvdata(pdev, gdev);
211 for (i = 0, p = gdev->info; i < cnt; i++, p++) {
212 uio_unregister_device(p);
214 iounmap(gdev->prussio_vaddr);
216 dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr,
219 if (pdata->sram_pool)
220 gen_pool_free(gdev->sram_pool, gdev->sram_vaddr, sram_pool_sz);
222 clk_disable(gdev->pruss_clk);
227 static int pruss_remove(struct platform_device *dev)
229 struct uio_pruss_dev *gdev = platform_get_drvdata(dev);
231 pruss_cleanup(&dev->dev, gdev);
235 static struct platform_driver pruss_driver = {
236 .probe = pruss_probe,
237 .remove = pruss_remove,
243 module_platform_driver(pruss_driver);
245 MODULE_LICENSE("GPL v2");
246 MODULE_VERSION(DRV_VERSION);
247 MODULE_AUTHOR("Amit Chatterjee <amit.chatterjee@ti.com>");
248 MODULE_AUTHOR("Pratheesh Gangadhar <pratheesh@ti.com>");