ppc4xx_msi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Adding PCI-E MSI support for PPC4XX SoCs.
  3. *
  4. * Copyright (c) 2010, Applied Micro Circuits Corporation
  5. * Authors: Tirumala R Marri <tmarri@apm.com>
  6. * Feng Kan <fkan@apm.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <linux/irq.h>
  24. #include <linux/bootmem.h>
  25. #include <linux/pci.h>
  26. #include <linux/msi.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/export.h>
  30. #include <asm/prom.h>
  31. #include <asm/hw_irq.h>
  32. #include <asm/ppc-pci.h>
  33. #include <boot/dcr.h>
  34. #include <asm/dcr-regs.h>
  35. #include <asm/msi_bitmap.h>
  36. #define PEIH_TERMADH 0x00
  37. #define PEIH_TERMADL 0x08
  38. #define PEIH_MSIED 0x10
  39. #define PEIH_MSIMK 0x18
  40. #define PEIH_MSIASS 0x20
  41. #define PEIH_FLUSH0 0x30
  42. #define PEIH_FLUSH1 0x38
  43. #define PEIH_CNTRST 0x48
  44. #define NR_MSI_IRQS 4
  45. struct ppc4xx_msi {
  46. u32 msi_addr_lo;
  47. u32 msi_addr_hi;
  48. void __iomem *msi_regs;
  49. int msi_virqs[NR_MSI_IRQS];
  50. struct msi_bitmap bitmap;
  51. struct device_node *msi_dev;
  52. };
  53. static struct ppc4xx_msi ppc4xx_msi;
  54. static int ppc4xx_msi_init_allocator(struct platform_device *dev,
  55. struct ppc4xx_msi *msi_data)
  56. {
  57. int err;
  58. err = msi_bitmap_alloc(&msi_data->bitmap, NR_MSI_IRQS,
  59. dev->dev.of_node);
  60. if (err)
  61. return err;
  62. err = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
  63. if (err < 0) {
  64. msi_bitmap_free(&msi_data->bitmap);
  65. return err;
  66. }
  67. return 0;
  68. }
  69. static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  70. {
  71. int int_no = -ENOMEM;
  72. unsigned int virq;
  73. struct msi_msg msg;
  74. struct msi_desc *entry;
  75. struct ppc4xx_msi *msi_data = &ppc4xx_msi;
  76. list_for_each_entry(entry, &dev->msi_list, list) {
  77. int_no = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
  78. if (int_no >= 0)
  79. break;
  80. if (int_no < 0) {
  81. pr_debug("%s: fail allocating msi interrupt\n",
  82. __func__);
  83. }
  84. virq = irq_of_parse_and_map(msi_data->msi_dev, int_no);
  85. if (virq == NO_IRQ) {
  86. dev_err(&dev->dev, "%s: fail mapping irq\n", __func__);
  87. msi_bitmap_free_hwirqs(&msi_data->bitmap, int_no, 1);
  88. return -ENOSPC;
  89. }
  90. dev_dbg(&dev->dev, "%s: virq = %d\n", __func__, virq);
  91. /* Setup msi address space */
  92. msg.address_hi = msi_data->msi_addr_hi;
  93. msg.address_lo = msi_data->msi_addr_lo;
  94. irq_set_msi_desc(virq, entry);
  95. msg.data = int_no;
  96. write_msi_msg(virq, &msg);
  97. }
  98. return 0;
  99. }
  100. void ppc4xx_teardown_msi_irqs(struct pci_dev *dev)
  101. {
  102. struct msi_desc *entry;
  103. struct ppc4xx_msi *msi_data = &ppc4xx_msi;
  104. dev_dbg(&dev->dev, "PCIE-MSI: tearing down msi irqs\n");
  105. list_for_each_entry(entry, &dev->msi_list, list) {
  106. if (entry->irq == NO_IRQ)
  107. continue;
  108. irq_set_msi_desc(entry->irq, NULL);
  109. msi_bitmap_free_hwirqs(&msi_data->bitmap,
  110. virq_to_hw(entry->irq), 1);
  111. irq_dispose_mapping(entry->irq);
  112. }
  113. }
  114. static int ppc4xx_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  115. {
  116. dev_dbg(&pdev->dev, "PCIE-MSI:%s called. vec %x type %d\n",
  117. __func__, nvec, type);
  118. if (type == PCI_CAP_ID_MSIX)
  119. pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n");
  120. return 0;
  121. }
  122. static int ppc4xx_setup_pcieh_hw(struct platform_device *dev,
  123. struct resource res, struct ppc4xx_msi *msi)
  124. {
  125. const u32 *msi_data;
  126. const u32 *msi_mask;
  127. const u32 *sdr_addr;
  128. dma_addr_t msi_phys;
  129. void *msi_virt;
  130. sdr_addr = of_get_property(dev->dev.of_node, "sdr-base", NULL);
  131. if (!sdr_addr)
  132. return -1;
  133. SDR0_WRITE(sdr_addr, (u64)res.start >> 32); /*HIGH addr */
  134. SDR0_WRITE(sdr_addr + 1, res.start & 0xFFFFFFFF); /* Low addr */
  135. msi->msi_dev = of_find_node_by_name(NULL, "ppc4xx-msi");
  136. if (msi->msi_dev)
  137. return -ENODEV;
  138. msi->msi_regs = of_iomap(msi->msi_dev, 0);
  139. if (!msi->msi_regs) {
  140. dev_err(&dev->dev, "of_iomap problem failed\n");
  141. return -ENOMEM;
  142. }
  143. dev_dbg(&dev->dev, "PCIE-MSI: msi register mapped 0x%x 0x%x\n",
  144. (u32) (msi->msi_regs + PEIH_TERMADH), (u32) (msi->msi_regs));
  145. msi_virt = dma_alloc_coherent(&dev->dev, 64, &msi_phys, GFP_KERNEL);
  146. msi->msi_addr_hi = 0x0;
  147. msi->msi_addr_lo = (u32) msi_phys;
  148. dev_dbg(&dev->dev, "PCIE-MSI: msi address 0x%x\n", msi->msi_addr_lo);
  149. /* Progam the Interrupt handler Termination addr registers */
  150. out_be32(msi->msi_regs + PEIH_TERMADH, msi->msi_addr_hi);
  151. out_be32(msi->msi_regs + PEIH_TERMADL, msi->msi_addr_lo);
  152. msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL);
  153. if (!msi_data)
  154. return -1;
  155. msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL);
  156. if (!msi_mask)
  157. return -1;
  158. /* Program MSI Expected data and Mask bits */
  159. out_be32(msi->msi_regs + PEIH_MSIED, *msi_data);
  160. out_be32(msi->msi_regs + PEIH_MSIMK, *msi_mask);
  161. return 0;
  162. }
  163. static int ppc4xx_of_msi_remove(struct platform_device *dev)
  164. {
  165. struct ppc4xx_msi *msi = dev->dev.platform_data;
  166. int i;
  167. int virq;
  168. for (i = 0; i < NR_MSI_IRQS; i++) {
  169. virq = msi->msi_virqs[i];
  170. if (virq != NO_IRQ)
  171. irq_dispose_mapping(virq);
  172. }
  173. if (msi->bitmap.bitmap)
  174. msi_bitmap_free(&msi->bitmap);
  175. iounmap(msi->msi_regs);
  176. of_node_put(msi->msi_dev);
  177. kfree(msi);
  178. return 0;
  179. }
  180. static int __devinit ppc4xx_msi_probe(struct platform_device *dev)
  181. {
  182. struct ppc4xx_msi *msi;
  183. struct resource res;
  184. int err = 0;
  185. msi = &ppc4xx_msi;/*keep the msi data for further use*/
  186. dev_dbg(&dev->dev, "PCIE-MSI: Setting up MSI support...\n");
  187. msi = kzalloc(sizeof(struct ppc4xx_msi), GFP_KERNEL);
  188. if (!msi) {
  189. dev_err(&dev->dev, "No memory for MSI structure\n");
  190. return -ENOMEM;
  191. }
  192. dev->dev.platform_data = msi;
  193. /* Get MSI ranges */
  194. err = of_address_to_resource(dev->dev.of_node, 0, &res);
  195. if (err) {
  196. dev_err(&dev->dev, "%s resource error!\n",
  197. dev->dev.of_node->full_name);
  198. goto error_out;
  199. }
  200. if (ppc4xx_setup_pcieh_hw(dev, res, msi))
  201. goto error_out;
  202. err = ppc4xx_msi_init_allocator(dev, msi);
  203. if (err) {
  204. dev_err(&dev->dev, "Error allocating MSI bitmap\n");
  205. goto error_out;
  206. }
  207. ppc_md.setup_msi_irqs = ppc4xx_setup_msi_irqs;
  208. ppc_md.teardown_msi_irqs = ppc4xx_teardown_msi_irqs;
  209. ppc_md.msi_check_device = ppc4xx_msi_check_device;
  210. return err;
  211. error_out:
  212. ppc4xx_of_msi_remove(dev);
  213. return err;
  214. }
  215. static const struct of_device_id ppc4xx_msi_ids[] = {
  216. {
  217. .compatible = "amcc,ppc4xx-msi",
  218. },
  219. {}
  220. };
  221. static struct platform_driver ppc4xx_msi_driver = {
  222. .probe = ppc4xx_msi_probe,
  223. .remove = ppc4xx_of_msi_remove,
  224. .driver = {
  225. .name = "ppc4xx-msi",
  226. .owner = THIS_MODULE,
  227. .of_match_table = ppc4xx_msi_ids,
  228. },
  229. };
  230. static __init int ppc4xx_msi_init(void)
  231. {
  232. return platform_driver_register(&ppc4xx_msi_driver);
  233. }
  234. subsys_initcall(ppc4xx_msi_init);