pci-layerscape.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * PCIe host controller driver for Freescale Layerscape SoCs
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor.
  5. *
  6. * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/init.h>
  15. #include <linux/of_pci.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_address.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/resource.h>
  22. #include <linux/mfd/syscon.h>
  23. #include <linux/regmap.h>
  24. #include "pcie-designware.h"
  25. /* PEX1/2 Misc Ports Status Register */
  26. #define SCFG_PEXMSCPORTSR(pex_idx) (0x94 + (pex_idx) * 4)
  27. #define LTSSM_STATE_SHIFT 20
  28. #define LTSSM_STATE_MASK 0x3f
  29. #define LTSSM_PCIE_L0 0x11 /* L0 state */
  30. /* PEX Internal Configuration Registers */
  31. #define PCIE_STRFMR1 0x71c /* Symbol Timer & Filter Mask Register1 */
  32. #define PCIE_DBI_RO_WR_EN 0x8bc /* DBI Read-Only Write Enable Register */
  33. struct ls_pcie_drvdata {
  34. u32 lut_offset;
  35. u32 ltssm_shift;
  36. u32 lut_dbg;
  37. struct pcie_host_ops *ops;
  38. };
  39. struct ls_pcie {
  40. struct pcie_port pp; /* pp.dbi_base is DT regs */
  41. void __iomem *lut;
  42. struct regmap *scfg;
  43. const struct ls_pcie_drvdata *drvdata;
  44. int index;
  45. };
  46. #define to_ls_pcie(x) container_of(x, struct ls_pcie, pp)
  47. static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
  48. {
  49. u32 header_type;
  50. header_type = ioread8(pcie->pp.dbi_base + PCI_HEADER_TYPE);
  51. header_type &= 0x7f;
  52. return header_type == PCI_HEADER_TYPE_BRIDGE;
  53. }
  54. /* Clear multi-function bit */
  55. static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
  56. {
  57. iowrite8(PCI_HEADER_TYPE_BRIDGE, pcie->pp.dbi_base + PCI_HEADER_TYPE);
  58. }
  59. /* Fix class value */
  60. static void ls_pcie_fix_class(struct ls_pcie *pcie)
  61. {
  62. iowrite16(PCI_CLASS_BRIDGE_PCI, pcie->pp.dbi_base + PCI_CLASS_DEVICE);
  63. }
  64. /* Drop MSG TLP except for Vendor MSG */
  65. static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
  66. {
  67. u32 val;
  68. val = ioread32(pcie->pp.dbi_base + PCIE_STRFMR1);
  69. val &= 0xDFFFFFFF;
  70. iowrite32(val, pcie->pp.dbi_base + PCIE_STRFMR1);
  71. }
  72. static int ls1021_pcie_link_up(struct pcie_port *pp)
  73. {
  74. u32 state;
  75. struct ls_pcie *pcie = to_ls_pcie(pp);
  76. if (!pcie->scfg)
  77. return 0;
  78. regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
  79. state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
  80. if (state < LTSSM_PCIE_L0)
  81. return 0;
  82. return 1;
  83. }
  84. static void ls1021_pcie_host_init(struct pcie_port *pp)
  85. {
  86. struct device *dev = pp->dev;
  87. struct ls_pcie *pcie = to_ls_pcie(pp);
  88. u32 index[2];
  89. pcie->scfg = syscon_regmap_lookup_by_phandle(dev->of_node,
  90. "fsl,pcie-scfg");
  91. if (IS_ERR(pcie->scfg)) {
  92. dev_err(dev, "No syscfg phandle specified\n");
  93. pcie->scfg = NULL;
  94. return;
  95. }
  96. if (of_property_read_u32_array(dev->of_node,
  97. "fsl,pcie-scfg", index, 2)) {
  98. pcie->scfg = NULL;
  99. return;
  100. }
  101. pcie->index = index[1];
  102. dw_pcie_setup_rc(pp);
  103. ls_pcie_drop_msg_tlp(pcie);
  104. }
  105. static int ls_pcie_link_up(struct pcie_port *pp)
  106. {
  107. struct ls_pcie *pcie = to_ls_pcie(pp);
  108. u32 state;
  109. state = (ioread32(pcie->lut + pcie->drvdata->lut_dbg) >>
  110. pcie->drvdata->ltssm_shift) &
  111. LTSSM_STATE_MASK;
  112. if (state < LTSSM_PCIE_L0)
  113. return 0;
  114. return 1;
  115. }
  116. static void ls_pcie_host_init(struct pcie_port *pp)
  117. {
  118. struct ls_pcie *pcie = to_ls_pcie(pp);
  119. iowrite32(1, pcie->pp.dbi_base + PCIE_DBI_RO_WR_EN);
  120. ls_pcie_fix_class(pcie);
  121. ls_pcie_clear_multifunction(pcie);
  122. ls_pcie_drop_msg_tlp(pcie);
  123. iowrite32(0, pcie->pp.dbi_base + PCIE_DBI_RO_WR_EN);
  124. }
  125. static int ls_pcie_msi_host_init(struct pcie_port *pp,
  126. struct msi_controller *chip)
  127. {
  128. struct device *dev = pp->dev;
  129. struct device_node *np = dev->of_node;
  130. struct device_node *msi_node;
  131. /*
  132. * The MSI domain is set by the generic of_msi_configure(). This
  133. * .msi_host_init() function keeps us from doing the default MSI
  134. * domain setup in dw_pcie_host_init() and also enforces the
  135. * requirement that "msi-parent" exists.
  136. */
  137. msi_node = of_parse_phandle(np, "msi-parent", 0);
  138. if (!msi_node) {
  139. dev_err(dev, "failed to find msi-parent\n");
  140. return -EINVAL;
  141. }
  142. return 0;
  143. }
  144. static struct pcie_host_ops ls1021_pcie_host_ops = {
  145. .link_up = ls1021_pcie_link_up,
  146. .host_init = ls1021_pcie_host_init,
  147. .msi_host_init = ls_pcie_msi_host_init,
  148. };
  149. static struct pcie_host_ops ls_pcie_host_ops = {
  150. .link_up = ls_pcie_link_up,
  151. .host_init = ls_pcie_host_init,
  152. .msi_host_init = ls_pcie_msi_host_init,
  153. };
  154. static struct ls_pcie_drvdata ls1021_drvdata = {
  155. .ops = &ls1021_pcie_host_ops,
  156. };
  157. static struct ls_pcie_drvdata ls1043_drvdata = {
  158. .lut_offset = 0x10000,
  159. .ltssm_shift = 24,
  160. .lut_dbg = 0x7fc,
  161. .ops = &ls_pcie_host_ops,
  162. };
  163. static struct ls_pcie_drvdata ls1046_drvdata = {
  164. .lut_offset = 0x80000,
  165. .ltssm_shift = 24,
  166. .lut_dbg = 0x407fc,
  167. .ops = &ls_pcie_host_ops,
  168. };
  169. static struct ls_pcie_drvdata ls2080_drvdata = {
  170. .lut_offset = 0x80000,
  171. .ltssm_shift = 0,
  172. .lut_dbg = 0x7fc,
  173. .ops = &ls_pcie_host_ops,
  174. };
  175. static const struct of_device_id ls_pcie_of_match[] = {
  176. { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
  177. { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
  178. { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
  179. { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
  180. { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
  181. { },
  182. };
  183. static int __init ls_add_pcie_port(struct ls_pcie *pcie)
  184. {
  185. struct pcie_port *pp = &pcie->pp;
  186. struct device *dev = pp->dev;
  187. int ret;
  188. ret = dw_pcie_host_init(pp);
  189. if (ret) {
  190. dev_err(dev, "failed to initialize host\n");
  191. return ret;
  192. }
  193. return 0;
  194. }
  195. static int __init ls_pcie_probe(struct platform_device *pdev)
  196. {
  197. struct device *dev = &pdev->dev;
  198. const struct of_device_id *match;
  199. struct ls_pcie *pcie;
  200. struct pcie_port *pp;
  201. struct resource *dbi_base;
  202. int ret;
  203. match = of_match_device(ls_pcie_of_match, dev);
  204. if (!match)
  205. return -ENODEV;
  206. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  207. if (!pcie)
  208. return -ENOMEM;
  209. pp = &pcie->pp;
  210. pp->dev = dev;
  211. pcie->drvdata = match->data;
  212. pp->ops = pcie->drvdata->ops;
  213. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  214. pcie->pp.dbi_base = devm_ioremap_resource(dev, dbi_base);
  215. if (IS_ERR(pcie->pp.dbi_base))
  216. return PTR_ERR(pcie->pp.dbi_base);
  217. pcie->lut = pcie->pp.dbi_base + pcie->drvdata->lut_offset;
  218. if (!ls_pcie_is_bridge(pcie))
  219. return -ENODEV;
  220. ret = ls_add_pcie_port(pcie);
  221. if (ret < 0)
  222. return ret;
  223. return 0;
  224. }
  225. static struct platform_driver ls_pcie_driver = {
  226. .driver = {
  227. .name = "layerscape-pcie",
  228. .of_match_table = ls_pcie_of_match,
  229. },
  230. };
  231. builtin_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);