uio_pruss.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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/sizes.h>
  28. #include <linux/slab.h>
  29. #include <linux/genalloc.h>
  30. #define DRV_NAME "pruss_uio"
  31. #define DRV_VERSION "1.0"
  32. static int sram_pool_sz = SZ_16K;
  33. module_param(sram_pool_sz, int, 0);
  34. MODULE_PARM_DESC(sram_pool_sz, "sram pool size to allocate ");
  35. static int extram_pool_sz = SZ_256K;
  36. module_param(extram_pool_sz, int, 0);
  37. MODULE_PARM_DESC(extram_pool_sz, "external ram pool size to allocate");
  38. /*
  39. * Host event IRQ numbers from PRUSS - PRUSS can generate up to 8 interrupt
  40. * events to AINTC of ARM host processor - which can be used for IPC b/w PRUSS
  41. * firmware and user space application, async notification from PRU firmware
  42. * to user space application
  43. * 3 PRU_EVTOUT0
  44. * 4 PRU_EVTOUT1
  45. * 5 PRU_EVTOUT2
  46. * 6 PRU_EVTOUT3
  47. * 7 PRU_EVTOUT4
  48. * 8 PRU_EVTOUT5
  49. * 9 PRU_EVTOUT6
  50. * 10 PRU_EVTOUT7
  51. */
  52. #define MAX_PRUSS_EVT 8
  53. #define PINTC_HIDISR 0x0038
  54. #define PINTC_HIPIR 0x0900
  55. #define HIPIR_NOPEND 0x80000000
  56. #define PINTC_HIER 0x1500
  57. struct uio_pruss_dev {
  58. struct uio_info *info;
  59. struct clk *pruss_clk;
  60. dma_addr_t sram_paddr;
  61. dma_addr_t ddr_paddr;
  62. void __iomem *prussio_vaddr;
  63. unsigned long sram_vaddr;
  64. void *ddr_vaddr;
  65. unsigned int hostirq_start;
  66. unsigned int pintc_base;
  67. struct gen_pool *sram_pool;
  68. };
  69. static irqreturn_t pruss_handler(int irq, struct uio_info *info)
  70. {
  71. struct uio_pruss_dev *gdev = info->priv;
  72. int intr_bit = (irq - gdev->hostirq_start + 2);
  73. int val, intr_mask = (1 << intr_bit);
  74. void __iomem *base = gdev->prussio_vaddr + gdev->pintc_base;
  75. void __iomem *intren_reg = base + PINTC_HIER;
  76. void __iomem *intrdis_reg = base + PINTC_HIDISR;
  77. 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))
  81. return IRQ_NONE;
  82. /* Disable interrupt */
  83. iowrite32(intr_bit, intrdis_reg);
  84. return IRQ_HANDLED;
  85. }
  86. static void pruss_cleanup(struct device *dev, struct uio_pruss_dev *gdev)
  87. {
  88. int cnt;
  89. struct uio_info *p = gdev->info;
  90. for (cnt = 0; cnt < MAX_PRUSS_EVT; cnt++, p++) {
  91. uio_unregister_device(p);
  92. kfree(p->name);
  93. }
  94. iounmap(gdev->prussio_vaddr);
  95. if (gdev->ddr_vaddr) {
  96. dma_free_coherent(dev, extram_pool_sz, gdev->ddr_vaddr,
  97. gdev->ddr_paddr);
  98. }
  99. if (gdev->sram_vaddr)
  100. gen_pool_free(gdev->sram_pool,
  101. gdev->sram_vaddr,
  102. sram_pool_sz);
  103. kfree(gdev->info);
  104. clk_put(gdev->pruss_clk);
  105. kfree(gdev);
  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 = -ENODEV, cnt = 0, len;
  114. struct uio_pruss_pdata *pdata = dev_get_platdata(dev);
  115. gdev = kzalloc(sizeof(struct uio_pruss_dev), GFP_KERNEL);
  116. if (!gdev)
  117. return -ENOMEM;
  118. gdev->info = kzalloc(sizeof(*p) * MAX_PRUSS_EVT, GFP_KERNEL);
  119. if (!gdev->info) {
  120. kfree(gdev);
  121. return -ENOMEM;
  122. }
  123. /* Power on PRU in case its not done as part of boot-loader */
  124. gdev->pruss_clk = clk_get(dev, "pruss");
  125. if (IS_ERR(gdev->pruss_clk)) {
  126. dev_err(dev, "Failed to get clock\n");
  127. ret = PTR_ERR(gdev->pruss_clk);
  128. kfree(gdev->info);
  129. kfree(gdev);
  130. return ret;
  131. } else {
  132. clk_enable(gdev->pruss_clk);
  133. }
  134. regs_prussio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  135. if (!regs_prussio) {
  136. dev_err(dev, "No PRUSS I/O resource specified\n");
  137. goto out_free;
  138. }
  139. if (!regs_prussio->start) {
  140. dev_err(dev, "Invalid memory resource\n");
  141. goto out_free;
  142. }
  143. if (pdata->sram_pool) {
  144. gdev->sram_pool = pdata->sram_pool;
  145. gdev->sram_vaddr =
  146. (unsigned long)gen_pool_dma_alloc(gdev->sram_pool,
  147. sram_pool_sz, &gdev->sram_paddr);
  148. if (!gdev->sram_vaddr) {
  149. dev_err(dev, "Could not allocate SRAM pool\n");
  150. goto out_free;
  151. }
  152. }
  153. gdev->ddr_vaddr = dma_alloc_coherent(dev, extram_pool_sz,
  154. &(gdev->ddr_paddr), GFP_KERNEL | GFP_DMA);
  155. if (!gdev->ddr_vaddr) {
  156. dev_err(dev, "Could not allocate external memory\n");
  157. goto out_free;
  158. }
  159. len = resource_size(regs_prussio);
  160. gdev->prussio_vaddr = ioremap(regs_prussio->start, len);
  161. if (!gdev->prussio_vaddr) {
  162. dev_err(dev, "Can't remap PRUSS I/O address range\n");
  163. goto out_free;
  164. }
  165. gdev->pintc_base = pdata->pintc_base;
  166. gdev->hostirq_start = platform_get_irq(pdev, 0);
  167. for (cnt = 0, p = gdev->info; cnt < MAX_PRUSS_EVT; cnt++, p++) {
  168. p->mem[0].addr = regs_prussio->start;
  169. p->mem[0].size = resource_size(regs_prussio);
  170. p->mem[0].memtype = UIO_MEM_PHYS;
  171. p->mem[1].addr = gdev->sram_paddr;
  172. p->mem[1].size = sram_pool_sz;
  173. p->mem[1].memtype = UIO_MEM_PHYS;
  174. p->mem[2].addr = gdev->ddr_paddr;
  175. p->mem[2].size = extram_pool_sz;
  176. p->mem[2].memtype = UIO_MEM_PHYS;
  177. p->name = kasprintf(GFP_KERNEL, "pruss_evt%d", cnt);
  178. p->version = DRV_VERSION;
  179. /* Register PRUSS IRQ lines */
  180. p->irq = gdev->hostirq_start + cnt;
  181. p->handler = pruss_handler;
  182. p->priv = gdev;
  183. ret = uio_register_device(dev, p);
  184. if (ret < 0)
  185. goto out_free;
  186. }
  187. platform_set_drvdata(pdev, gdev);
  188. return 0;
  189. out_free:
  190. pruss_cleanup(dev, gdev);
  191. return ret;
  192. }
  193. static int pruss_remove(struct platform_device *dev)
  194. {
  195. struct uio_pruss_dev *gdev = platform_get_drvdata(dev);
  196. pruss_cleanup(&dev->dev, gdev);
  197. return 0;
  198. }
  199. static struct platform_driver pruss_driver = {
  200. .probe = pruss_probe,
  201. .remove = pruss_remove,
  202. .driver = {
  203. .name = DRV_NAME,
  204. },
  205. };
  206. module_platform_driver(pruss_driver);
  207. MODULE_LICENSE("GPL v2");
  208. MODULE_VERSION(DRV_VERSION);
  209. MODULE_AUTHOR("Amit Chatterjee <amit.chatterjee@ti.com>");
  210. MODULE_AUTHOR("Pratheesh Gangadhar <pratheesh@ti.com>");