uio_pruss.c 6.5 KB

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