xref: /openbmc/linux/drivers/uio/uio_pruss.c (revision d07479b2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Programmable Real-Time Unit Sub System (PRUSS) UIO driver (uio_pruss)
4  *
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
7  *
8  * Copyright (C) 2010-11 Texas Instruments Incorporated - http://www.ti.com/
9  */
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>
16 #include <linux/io.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>
22 
23 #define DRV_NAME "pruss_uio"
24 #define DRV_VERSION "1.0"
25 
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 ");
29 
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");
33 
34 /*
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
39  * 3	PRU_EVTOUT0
40  * 4	PRU_EVTOUT1
41  * 5	PRU_EVTOUT2
42  * 6	PRU_EVTOUT3
43  * 7	PRU_EVTOUT4
44  * 8	PRU_EVTOUT5
45  * 9	PRU_EVTOUT6
46  * 10	PRU_EVTOUT7
47 */
48 #define MAX_PRUSS_EVT	8
49 
50 #define PINTC_HIDISR	0x0038
51 #define PINTC_HIPIR	0x0900
52 #define HIPIR_NOPEND	0x80000000
53 #define PINTC_HIER	0x1500
54 
55 struct uio_pruss_dev {
56 	struct uio_info *info;
57 	struct clk *pruss_clk;
58 	dma_addr_t sram_paddr;
59 	dma_addr_t ddr_paddr;
60 	void __iomem *prussio_vaddr;
61 	unsigned long sram_vaddr;
62 	void *ddr_vaddr;
63 	unsigned int hostirq_start;
64 	unsigned int pintc_base;
65 	struct gen_pool *sram_pool;
66 };
67 
68 static irqreturn_t pruss_handler(int irq, struct uio_info *info)
69 {
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);
77 
78 	val = ioread32(intren_reg);
79 	/* Is interrupt enabled and active ? */
80 	if (!(val & intr_mask) && (ioread32(intrstat_reg) & HIPIR_NOPEND))
81 		return IRQ_NONE;
82 	/* Disable interrupt */
83 	iowrite32(intr_bit, intrdis_reg);
84 	return IRQ_HANDLED;
85 }
86 
87 static void pruss_cleanup(struct device *dev, struct uio_pruss_dev *gdev)
88 {
89 	int cnt;
90 	struct uio_info *p = gdev->info;
91 
92 	for (cnt = 0; cnt < MAX_PRUSS_EVT; cnt++, p++) {
93 		uio_unregister_device(p);
94 	}
95 	iounmap(gdev->prussio_vaddr);
96 	if (gdev->ddr_vaddr) {
97 		dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr,
98 			gdev->ddr_paddr);
99 	}
100 	if (gdev->sram_vaddr)
101 		gen_pool_free(gdev->sram_pool,
102 			      gdev->sram_vaddr,
103 			      sram_pool_sz);
104 	clk_disable(gdev->pruss_clk);
105 }
106 
107 static int pruss_probe(struct platform_device *pdev)
108 {
109 	struct uio_info *p;
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);
115 
116 	gdev = devm_kzalloc(dev, sizeof(struct uio_pruss_dev), GFP_KERNEL);
117 	if (!gdev)
118 		return -ENOMEM;
119 
120 	gdev->info = devm_kcalloc(dev, MAX_PRUSS_EVT, sizeof(*p), GFP_KERNEL);
121 	if (!gdev->info)
122 		return -ENOMEM;
123 
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);
129 	}
130 
131 	ret = clk_enable(gdev->pruss_clk);
132 	if (ret) {
133 		dev_err(dev, "Failed to enable clock\n");
134 		return ret;
135 	}
136 
137 	regs_prussio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
138 	if (!regs_prussio) {
139 		dev_err(dev, "No PRUSS I/O resource specified\n");
140 		ret = -EIO;
141 		goto err_clk_disable;
142 	}
143 
144 	if (!regs_prussio->start) {
145 		dev_err(dev, "Invalid memory resource\n");
146 		ret = -EIO;
147 		goto err_clk_disable;
148 	}
149 
150 	if (pdata->sram_pool) {
151 		gdev->sram_pool = pdata->sram_pool;
152 		gdev->sram_vaddr =
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");
157 			ret = -ENOMEM;
158 			goto err_clk_disable;
159 		}
160 	}
161 
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");
166 		ret = -ENOMEM;
167 		goto err_free_sram;
168 	}
169 
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");
174 		ret = -ENOMEM;
175 		goto err_free_ddr_vaddr;
176 	}
177 
178 	gdev->pintc_base = pdata->pintc_base;
179 	gdev->hostirq_start = platform_get_irq(pdev, 0);
180 
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;
185 
186 		p->mem[1].addr = gdev->sram_paddr;
187 		p->mem[1].size = sram_pool_sz;
188 		p->mem[1].memtype = UIO_MEM_PHYS;
189 
190 		p->mem[2].addr = gdev->ddr_paddr;
191 		p->mem[2].size = extram_pool_sz;
192 		p->mem[2].memtype = UIO_MEM_PHYS;
193 
194 		p->name = devm_kasprintf(dev, GFP_KERNEL, "pruss_evt%d", cnt);
195 		p->version = DRV_VERSION;
196 
197 		/* Register PRUSS IRQ lines */
198 		p->irq = gdev->hostirq_start + cnt;
199 		p->handler = pruss_handler;
200 		p->priv = gdev;
201 
202 		ret = uio_register_device(dev, p);
203 		if (ret < 0)
204 			goto err_unloop;
205 	}
206 
207 	platform_set_drvdata(pdev, gdev);
208 	return 0;
209 
210 err_unloop:
211 	for (i = 0, p = gdev->info; i < cnt; i++, p++) {
212 		uio_unregister_device(p);
213 	}
214 	iounmap(gdev->prussio_vaddr);
215 err_free_ddr_vaddr:
216 	dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr,
217 			  gdev->ddr_paddr);
218 err_free_sram:
219 	if (pdata->sram_pool)
220 		gen_pool_free(gdev->sram_pool, gdev->sram_vaddr, sram_pool_sz);
221 err_clk_disable:
222 	clk_disable(gdev->pruss_clk);
223 
224 	return ret;
225 }
226 
227 static int pruss_remove(struct platform_device *dev)
228 {
229 	struct uio_pruss_dev *gdev = platform_get_drvdata(dev);
230 
231 	pruss_cleanup(&dev->dev, gdev);
232 	return 0;
233 }
234 
235 static struct platform_driver pruss_driver = {
236 	.probe = pruss_probe,
237 	.remove = pruss_remove,
238 	.driver = {
239 		   .name = DRV_NAME,
240 		   },
241 };
242 
243 module_platform_driver(pruss_driver);
244 
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>");
249