ppc4xx_msi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 <linux/kernel.h>
  31. #include <asm/prom.h>
  32. #include <asm/hw_irq.h>
  33. #include <asm/ppc-pci.h>
  34. #include <asm/dcr.h>
  35. #include <asm/dcr-regs.h>
  36. #include <asm/msi_bitmap.h>
  37. #define PEIH_TERMADH 0x00
  38. #define PEIH_TERMADL 0x08
  39. #define PEIH_MSIED 0x10
  40. #define PEIH_MSIMK 0x18
  41. #define PEIH_MSIASS 0x20
  42. #define PEIH_FLUSH0 0x30
  43. #define PEIH_FLUSH1 0x38
  44. #define PEIH_CNTRST 0x48
  45. static int msi_irqs;
  46. struct ppc4xx_msi {
  47. u32 msi_addr_lo;
  48. u32 msi_addr_hi;
  49. void __iomem *msi_regs;
  50. int *msi_virqs;
  51. struct msi_bitmap bitmap;
  52. struct device_node *msi_dev;
  53. };
  54. static struct ppc4xx_msi ppc4xx_msi;
  55. static int ppc4xx_msi_init_allocator(struct platform_device *dev,
  56. struct ppc4xx_msi *msi_data)
  57. {
  58. int err;
  59. err = msi_bitmap_alloc(&msi_data->bitmap, msi_irqs,
  60. dev->dev.of_node);
  61. if (err)
  62. return err;
  63. err = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
  64. if (err < 0) {
  65. msi_bitmap_free(&msi_data->bitmap);
  66. return err;
  67. }
  68. return 0;
  69. }
  70. static int ppc4xx_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  71. {
  72. int int_no = -ENOMEM;
  73. unsigned int virq;
  74. struct msi_msg msg;
  75. struct msi_desc *entry;
  76. struct ppc4xx_msi *msi_data = &ppc4xx_msi;
  77. msi_data->msi_virqs = kmalloc((msi_irqs) * sizeof(int),
  78. GFP_KERNEL);
  79. if (!msi_data->msi_virqs)
  80. return -ENOMEM;
  81. list_for_each_entry(entry, &dev->msi_list, list) {
  82. int_no = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
  83. if (int_no >= 0)
  84. break;
  85. if (int_no < 0) {
  86. pr_debug("%s: fail allocating msi interrupt\n",
  87. __func__);
  88. }
  89. virq = irq_of_parse_and_map(msi_data->msi_dev, int_no);
  90. if (virq == NO_IRQ) {
  91. dev_err(&dev->dev, "%s: fail mapping irq\n", __func__);
  92. msi_bitmap_free_hwirqs(&msi_data->bitmap, int_no, 1);
  93. return -ENOSPC;
  94. }
  95. dev_dbg(&dev->dev, "%s: virq = %d\n", __func__, virq);
  96. /* Setup msi address space */
  97. msg.address_hi = msi_data->msi_addr_hi;
  98. msg.address_lo = msi_data->msi_addr_lo;
  99. irq_set_msi_desc(virq, entry);
  100. msg.data = int_no;
  101. write_msi_msg(virq, &msg);
  102. }
  103. return 0;
  104. }
  105. void ppc4xx_teardown_msi_irqs(struct pci_dev *dev)
  106. {
  107. struct msi_desc *entry;
  108. struct ppc4xx_msi *msi_data = &ppc4xx_msi;
  109. dev_dbg(&dev->dev, "PCIE-MSI: tearing down msi irqs\n");
  110. list_for_each_entry(entry, &dev->msi_list, list) {
  111. if (entry->irq == NO_IRQ)
  112. continue;
  113. irq_set_msi_desc(entry->irq, NULL);
  114. msi_bitmap_free_hwirqs(&msi_data->bitmap,
  115. virq_to_hw(entry->irq), 1);
  116. irq_dispose_mapping(entry->irq);
  117. }
  118. }
  119. static int ppc4xx_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  120. {
  121. dev_dbg(&pdev->dev, "PCIE-MSI:%s called. vec %x type %d\n",
  122. __func__, nvec, type);
  123. if (type == PCI_CAP_ID_MSIX)
  124. pr_debug("ppc4xx msi: MSI-X untested, trying anyway.\n");
  125. return 0;
  126. }
  127. static int ppc4xx_setup_pcieh_hw(struct platform_device *dev,
  128. struct resource res, struct ppc4xx_msi *msi)
  129. {
  130. const u32 *msi_data;
  131. const u32 *msi_mask;
  132. const u32 *sdr_addr;
  133. dma_addr_t msi_phys;
  134. void *msi_virt;
  135. sdr_addr = of_get_property(dev->dev.of_node, "sdr-base", NULL);
  136. if (!sdr_addr)
  137. return -1;
  138. mtdcri(SDR0, *sdr_addr, upper_32_bits(res.start)); /*HIGH addr */
  139. mtdcri(SDR0, *sdr_addr + 1, lower_32_bits(res.start)); /* Low addr */
  140. msi->msi_dev = of_find_node_by_name(NULL, "ppc4xx-msi");
  141. if (!msi->msi_dev)
  142. return -ENODEV;
  143. msi->msi_regs = of_iomap(msi->msi_dev, 0);
  144. if (!msi->msi_regs) {
  145. dev_err(&dev->dev, "of_iomap problem failed\n");
  146. return -ENOMEM;
  147. }
  148. dev_dbg(&dev->dev, "PCIE-MSI: msi register mapped 0x%x 0x%x\n",
  149. (u32) (msi->msi_regs + PEIH_TERMADH), (u32) (msi->msi_regs));
  150. msi_virt = dma_alloc_coherent(&dev->dev, 64, &msi_phys, GFP_KERNEL);
  151. if (!msi_virt)
  152. return -ENOMEM;
  153. msi->msi_addr_hi = upper_32_bits(msi_phys);
  154. msi->msi_addr_lo = lower_32_bits(msi_phys & 0xffffffff);
  155. dev_dbg(&dev->dev, "PCIE-MSI: msi address high 0x%x, low 0x%x\n",
  156. msi->msi_addr_hi, msi->msi_addr_lo);
  157. /* Progam the Interrupt handler Termination addr registers */
  158. out_be32(msi->msi_regs + PEIH_TERMADH, msi->msi_addr_hi);
  159. out_be32(msi->msi_regs + PEIH_TERMADL, msi->msi_addr_lo);
  160. msi_data = of_get_property(dev->dev.of_node, "msi-data", NULL);
  161. if (!msi_data)
  162. return -1;
  163. msi_mask = of_get_property(dev->dev.of_node, "msi-mask", NULL);
  164. if (!msi_mask)
  165. return -1;
  166. /* Program MSI Expected data and Mask bits */
  167. out_be32(msi->msi_regs + PEIH_MSIED, *msi_data);
  168. out_be32(msi->msi_regs + PEIH_MSIMK, *msi_mask);
  169. dma_free_coherent(&dev->dev, 64, msi_virt, msi_phys);
  170. return 0;
  171. }
  172. static int ppc4xx_of_msi_remove(struct platform_device *dev)
  173. {
  174. struct ppc4xx_msi *msi = dev->dev.platform_data;
  175. int i;
  176. int virq;
  177. for (i = 0; i < msi_irqs; i++) {
  178. virq = msi->msi_virqs[i];
  179. if (virq != NO_IRQ)
  180. irq_dispose_mapping(virq);
  181. }
  182. if (msi->bitmap.bitmap)
  183. msi_bitmap_free(&msi->bitmap);
  184. iounmap(msi->msi_regs);
  185. of_node_put(msi->msi_dev);
  186. kfree(msi);
  187. return 0;
  188. }
  189. static int __devinit ppc4xx_msi_probe(struct platform_device *dev)
  190. {
  191. struct ppc4xx_msi *msi;
  192. struct resource res;
  193. int err = 0;
  194. dev_dbg(&dev->dev, "PCIE-MSI: Setting up MSI support...\n");
  195. msi = kzalloc(sizeof(struct ppc4xx_msi), GFP_KERNEL);
  196. if (!msi) {
  197. dev_err(&dev->dev, "No memory for MSI structure\n");
  198. return -ENOMEM;
  199. }
  200. dev->dev.platform_data = msi;
  201. /* Get MSI ranges */
  202. err = of_address_to_resource(dev->dev.of_node, 0, &res);
  203. if (err) {
  204. dev_err(&dev->dev, "%s resource error!\n",
  205. dev->dev.of_node->full_name);
  206. goto error_out;
  207. }
  208. msi_irqs = of_irq_count(dev->dev.of_node);
  209. if (!msi_irqs)
  210. return -ENODEV;
  211. if (ppc4xx_setup_pcieh_hw(dev, res, msi))
  212. goto error_out;
  213. err = ppc4xx_msi_init_allocator(dev, msi);
  214. if (err) {
  215. dev_err(&dev->dev, "Error allocating MSI bitmap\n");
  216. goto error_out;
  217. }
  218. ppc4xx_msi = *msi;
  219. ppc_md.setup_msi_irqs = ppc4xx_setup_msi_irqs;
  220. ppc_md.teardown_msi_irqs = ppc4xx_teardown_msi_irqs;
  221. ppc_md.msi_check_device = ppc4xx_msi_check_device;
  222. return err;
  223. error_out:
  224. ppc4xx_of_msi_remove(dev);
  225. return err;
  226. }
  227. static const struct of_device_id ppc4xx_msi_ids[] = {
  228. {
  229. .compatible = "amcc,ppc4xx-msi",
  230. },
  231. {}
  232. };
  233. static struct platform_driver ppc4xx_msi_driver = {
  234. .probe = ppc4xx_msi_probe,
  235. .remove = ppc4xx_of_msi_remove,
  236. .driver = {
  237. .name = "ppc4xx-msi",
  238. .owner = THIS_MODULE,
  239. .of_match_table = ppc4xx_msi_ids,
  240. },
  241. };
  242. static __init int ppc4xx_msi_init(void)
  243. {
  244. return platform_driver_register(&ppc4xx_msi_driver);
  245. }
  246. subsys_initcall(ppc4xx_msi_init);