blob: ca9e2fafb0b6c05571b2652e5dcb45b12257b7a9 [file] [log] [blame]
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +05301/*
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. Kornilov87672672015-06-10 20:00:19 +030027#include <linux/sizes.h>
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +053028#include <linux/slab.h>
Matt Porter2eb24782012-10-05 13:04:40 -040029#include <linux/genalloc.h>
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +053030
31#define DRV_NAME "pruss_uio"
32#define DRV_VERSION "1.0"
33
34static int sram_pool_sz = SZ_16K;
35module_param(sram_pool_sz, int, 0);
36MODULE_PARM_DESC(sram_pool_sz, "sram pool size to allocate ");
37
38static int extram_pool_sz = SZ_256K;
39module_param(extram_pool_sz, int, 0);
40MODULE_PARM_DESC(extram_pool_sz, "external ram pool size to allocate");
41
42/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030043 * Host event IRQ numbers from PRUSS - PRUSS can generate up to 8 interrupt
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +053044 * 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
63struct 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 Porter2eb24782012-10-05 13:04:40 -040069 unsigned long sram_vaddr;
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +053070 void *ddr_vaddr;
71 unsigned int hostirq_start;
72 unsigned int pintc_base;
Matt Porter2eb24782012-10-05 13:04:40 -040073 struct gen_pool *sram_pool;
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +053074};
75
76static 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 Heider4719ebf2014-06-29 18:21:35 +020095static void pruss_cleanup(struct device *dev, struct uio_pruss_dev *gdev)
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +053096{
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 Heider4719ebf2014-06-29 18:21:35 +0200106 dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr,
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530107 gdev->ddr_paddr);
108 }
109 if (gdev->sram_vaddr)
Matt Porter2eb24782012-10-05 13:04:40 -0400110 gen_pool_free(gdev->sram_pool,
111 gdev->sram_vaddr,
112 sram_pool_sz);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530113 kfree(gdev->info);
114 clk_put(gdev->pruss_clk);
115 kfree(gdev);
116}
117
Andre Heider4719ebf2014-06-29 18:21:35 +0200118static int pruss_probe(struct platform_device *pdev)
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530119{
120 struct uio_info *p;
121 struct uio_pruss_dev *gdev;
122 struct resource *regs_prussio;
Andre Heider4719ebf2014-06-29 18:21:35 +0200123 struct device *dev = &pdev->dev;
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530124 int ret = -ENODEV, cnt = 0, len;
Andre Heider4719ebf2014-06-29 18:21:35 +0200125 struct uio_pruss_pdata *pdata = dev_get_platdata(dev);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530126
127 gdev = kzalloc(sizeof(struct uio_pruss_dev), GFP_KERNEL);
128 if (!gdev)
129 return -ENOMEM;
130
131 gdev->info = kzalloc(sizeof(*p) * MAX_PRUSS_EVT, GFP_KERNEL);
132 if (!gdev->info) {
133 kfree(gdev);
134 return -ENOMEM;
135 }
Andre Heider4719ebf2014-06-29 18:21:35 +0200136
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530137 /* Power on PRU in case its not done as part of boot-loader */
Andre Heider4719ebf2014-06-29 18:21:35 +0200138 gdev->pruss_clk = clk_get(dev, "pruss");
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530139 if (IS_ERR(gdev->pruss_clk)) {
Andre Heider4719ebf2014-06-29 18:21:35 +0200140 dev_err(dev, "Failed to get clock\n");
Emil Goodecb3771b2013-06-06 15:55:28 +0200141 ret = PTR_ERR(gdev->pruss_clk);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530142 kfree(gdev->info);
143 kfree(gdev);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530144 return ret;
145 } else {
146 clk_enable(gdev->pruss_clk);
147 }
148
Andre Heider4719ebf2014-06-29 18:21:35 +0200149 regs_prussio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530150 if (!regs_prussio) {
Andre Heider4719ebf2014-06-29 18:21:35 +0200151 dev_err(dev, "No PRUSS I/O resource specified\n");
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530152 goto out_free;
153 }
154
155 if (!regs_prussio->start) {
Andre Heider4719ebf2014-06-29 18:21:35 +0200156 dev_err(dev, "Invalid memory resource\n");
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530157 goto out_free;
158 }
159
Matt Porter2eb24782012-10-05 13:04:40 -0400160 if (pdata->sram_pool) {
161 gdev->sram_pool = pdata->sram_pool;
162 gdev->sram_vaddr =
Nicolin Chen288342e2013-11-12 15:09:57 -0800163 (unsigned long)gen_pool_dma_alloc(gdev->sram_pool,
164 sram_pool_sz, &gdev->sram_paddr);
Matt Porter2eb24782012-10-05 13:04:40 -0400165 if (!gdev->sram_vaddr) {
Andre Heider4719ebf2014-06-29 18:21:35 +0200166 dev_err(dev, "Could not allocate SRAM pool\n");
Matt Porter2eb24782012-10-05 13:04:40 -0400167 goto out_free;
168 }
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530169 }
170
Andre Heider4719ebf2014-06-29 18:21:35 +0200171 gdev->ddr_vaddr = dma_alloc_coherent(dev, extram_pool_sz,
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530172 &(gdev->ddr_paddr), GFP_KERNEL | GFP_DMA);
173 if (!gdev->ddr_vaddr) {
Andre Heider4719ebf2014-06-29 18:21:35 +0200174 dev_err(dev, "Could not allocate external memory\n");
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530175 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) {
Andre Heider4719ebf2014-06-29 18:21:35 +0200181 dev_err(dev, "Can't remap PRUSS I/O address range\n");
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530182 goto out_free;
183 }
184
185 gdev->pintc_base = pdata->pintc_base;
Andre Heider4719ebf2014-06-29 18:21:35 +0200186 gdev->hostirq_start = platform_get_irq(pdev, 0);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530187
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
Andre Heider4719ebf2014-06-29 18:21:35 +0200209 ret = uio_register_device(dev, p);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530210 if (ret < 0)
211 goto out_free;
212 }
213
Andre Heider4719ebf2014-06-29 18:21:35 +0200214 platform_set_drvdata(pdev, gdev);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530215 return 0;
216
217out_free:
218 pruss_cleanup(dev, gdev);
219 return ret;
220}
221
Bill Pemberton9b96c312012-11-19 13:26:19 -0500222static int pruss_remove(struct platform_device *dev)
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530223{
224 struct uio_pruss_dev *gdev = platform_get_drvdata(dev);
225
Andre Heider4719ebf2014-06-29 18:21:35 +0200226 pruss_cleanup(&dev->dev, gdev);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530227 return 0;
228}
229
230static struct platform_driver pruss_driver = {
231 .probe = pruss_probe,
Bill Pemberton5a595092012-11-19 13:21:07 -0500232 .remove = pruss_remove,
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530233 .driver = {
234 .name = DRV_NAME,
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530235 },
236};
237
Axel Lin11e31232011-11-26 15:18:55 +0800238module_platform_driver(pruss_driver);
Pratheesh Gangadharf1a304e2011-03-05 04:30:17 +0530239
240MODULE_LICENSE("GPL v2");
241MODULE_VERSION(DRV_VERSION);
242MODULE_AUTHOR("Amit Chatterjee <amit.chatterjee@ti.com>");
243MODULE_AUTHOR("Pratheesh Gangadhar <pratheesh@ti.com>");