pci-layerscape.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Freescale Layerscape SoCs
  4. *
  5. * Copyright (C) 2014 Freescale Semiconductor.
  6. *
  7. * Author: Minghuan Lian <Minghuan.Lian@freescale.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/init.h>
  12. #include <linux/of_pci.h>
  13. #include <linux/of_platform.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/of_address.h>
  16. #include <linux/pci.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/resource.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/regmap.h>
  21. #include "pcie-designware.h"
  22. /* PEX1/2 Misc Ports Status Register */
  23. #define SCFG_PEXMSCPORTSR(pex_idx) (0x94 + (pex_idx) * 4)
  24. #define LTSSM_STATE_SHIFT 20
  25. #define LTSSM_STATE_MASK 0x3f
  26. #define LTSSM_PCIE_L0 0x11 /* L0 state */
  27. /* PEX Internal Configuration Registers */
  28. #define PCIE_STRFMR1 0x71c /* Symbol Timer & Filter Mask Register1 */
  29. #define PCIE_ABSERR 0x8d0 /* Bridge Slave Error Response Register */
  30. #define PCIE_ABSERR_SETTING 0x9401 /* Forward error of non-posted request */
  31. #define PCIE_IATU_NUM 6
  32. struct ls_pcie_drvdata {
  33. u32 lut_offset;
  34. u32 ltssm_shift;
  35. u32 lut_dbg;
  36. const struct dw_pcie_host_ops *ops;
  37. const struct dw_pcie_ops *dw_pcie_ops;
  38. };
  39. struct ls_pcie {
  40. struct dw_pcie *pci;
  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) dev_get_drvdata((x)->dev)
  47. static bool ls_pcie_is_bridge(struct ls_pcie *pcie)
  48. {
  49. struct dw_pcie *pci = pcie->pci;
  50. u32 header_type;
  51. header_type = ioread8(pci->dbi_base + PCI_HEADER_TYPE);
  52. header_type &= 0x7f;
  53. return header_type == PCI_HEADER_TYPE_BRIDGE;
  54. }
  55. /* Clear multi-function bit */
  56. static void ls_pcie_clear_multifunction(struct ls_pcie *pcie)
  57. {
  58. struct dw_pcie *pci = pcie->pci;
  59. iowrite8(PCI_HEADER_TYPE_BRIDGE, pci->dbi_base + PCI_HEADER_TYPE);
  60. }
  61. /* Drop MSG TLP except for Vendor MSG */
  62. static void ls_pcie_drop_msg_tlp(struct ls_pcie *pcie)
  63. {
  64. u32 val;
  65. struct dw_pcie *pci = pcie->pci;
  66. val = ioread32(pci->dbi_base + PCIE_STRFMR1);
  67. val &= 0xDFFFFFFF;
  68. iowrite32(val, pci->dbi_base + PCIE_STRFMR1);
  69. }
  70. static void ls_pcie_disable_outbound_atus(struct ls_pcie *pcie)
  71. {
  72. int i;
  73. for (i = 0; i < PCIE_IATU_NUM; i++)
  74. dw_pcie_disable_atu(pcie->pci, i, DW_PCIE_REGION_OUTBOUND);
  75. }
  76. static int ls1021_pcie_link_up(struct dw_pcie *pci)
  77. {
  78. u32 state;
  79. struct ls_pcie *pcie = to_ls_pcie(pci);
  80. if (!pcie->scfg)
  81. return 0;
  82. regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
  83. state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
  84. if (state < LTSSM_PCIE_L0)
  85. return 0;
  86. return 1;
  87. }
  88. static int ls_pcie_link_up(struct dw_pcie *pci)
  89. {
  90. struct ls_pcie *pcie = to_ls_pcie(pci);
  91. u32 state;
  92. state = (ioread32(pcie->lut + pcie->drvdata->lut_dbg) >>
  93. pcie->drvdata->ltssm_shift) &
  94. LTSSM_STATE_MASK;
  95. if (state < LTSSM_PCIE_L0)
  96. return 0;
  97. return 1;
  98. }
  99. /* Forward error response of outbound non-posted requests */
  100. static void ls_pcie_fix_error_response(struct ls_pcie *pcie)
  101. {
  102. struct dw_pcie *pci = pcie->pci;
  103. iowrite32(PCIE_ABSERR_SETTING, pci->dbi_base + PCIE_ABSERR);
  104. }
  105. static int ls_pcie_host_init(struct pcie_port *pp)
  106. {
  107. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  108. struct ls_pcie *pcie = to_ls_pcie(pci);
  109. /*
  110. * Disable outbound windows configured by the bootloader to avoid
  111. * one transaction hitting multiple outbound windows.
  112. * dw_pcie_setup_rc() will reconfigure the outbound windows.
  113. */
  114. ls_pcie_disable_outbound_atus(pcie);
  115. ls_pcie_fix_error_response(pcie);
  116. dw_pcie_dbi_ro_wr_en(pci);
  117. ls_pcie_clear_multifunction(pcie);
  118. dw_pcie_dbi_ro_wr_dis(pci);
  119. ls_pcie_drop_msg_tlp(pcie);
  120. dw_pcie_setup_rc(pp);
  121. return 0;
  122. }
  123. static int ls1021_pcie_host_init(struct pcie_port *pp)
  124. {
  125. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  126. struct ls_pcie *pcie = to_ls_pcie(pci);
  127. struct device *dev = pci->dev;
  128. u32 index[2];
  129. int ret;
  130. pcie->scfg = syscon_regmap_lookup_by_phandle(dev->of_node,
  131. "fsl,pcie-scfg");
  132. if (IS_ERR(pcie->scfg)) {
  133. ret = PTR_ERR(pcie->scfg);
  134. dev_err(dev, "No syscfg phandle specified\n");
  135. pcie->scfg = NULL;
  136. return ret;
  137. }
  138. if (of_property_read_u32_array(dev->of_node,
  139. "fsl,pcie-scfg", index, 2)) {
  140. pcie->scfg = NULL;
  141. return -EINVAL;
  142. }
  143. pcie->index = index[1];
  144. return ls_pcie_host_init(pp);
  145. }
  146. static int ls_pcie_msi_host_init(struct pcie_port *pp)
  147. {
  148. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  149. struct device *dev = pci->dev;
  150. struct device_node *np = dev->of_node;
  151. struct device_node *msi_node;
  152. /*
  153. * The MSI domain is set by the generic of_msi_configure(). This
  154. * .msi_host_init() function keeps us from doing the default MSI
  155. * domain setup in dw_pcie_host_init() and also enforces the
  156. * requirement that "msi-parent" exists.
  157. */
  158. msi_node = of_parse_phandle(np, "msi-parent", 0);
  159. if (!msi_node) {
  160. dev_err(dev, "failed to find msi-parent\n");
  161. return -EINVAL;
  162. }
  163. return 0;
  164. }
  165. static const struct dw_pcie_host_ops ls1021_pcie_host_ops = {
  166. .host_init = ls1021_pcie_host_init,
  167. .msi_host_init = ls_pcie_msi_host_init,
  168. };
  169. static const struct dw_pcie_host_ops ls_pcie_host_ops = {
  170. .host_init = ls_pcie_host_init,
  171. .msi_host_init = ls_pcie_msi_host_init,
  172. };
  173. static const struct dw_pcie_ops dw_ls1021_pcie_ops = {
  174. .link_up = ls1021_pcie_link_up,
  175. };
  176. static const struct dw_pcie_ops dw_ls_pcie_ops = {
  177. .link_up = ls_pcie_link_up,
  178. };
  179. static struct ls_pcie_drvdata ls1021_drvdata = {
  180. .ops = &ls1021_pcie_host_ops,
  181. .dw_pcie_ops = &dw_ls1021_pcie_ops,
  182. };
  183. static struct ls_pcie_drvdata ls1043_drvdata = {
  184. .lut_offset = 0x10000,
  185. .ltssm_shift = 24,
  186. .lut_dbg = 0x7fc,
  187. .ops = &ls_pcie_host_ops,
  188. .dw_pcie_ops = &dw_ls_pcie_ops,
  189. };
  190. static struct ls_pcie_drvdata ls1046_drvdata = {
  191. .lut_offset = 0x80000,
  192. .ltssm_shift = 24,
  193. .lut_dbg = 0x407fc,
  194. .ops = &ls_pcie_host_ops,
  195. .dw_pcie_ops = &dw_ls_pcie_ops,
  196. };
  197. static struct ls_pcie_drvdata ls2080_drvdata = {
  198. .lut_offset = 0x80000,
  199. .ltssm_shift = 0,
  200. .lut_dbg = 0x7fc,
  201. .ops = &ls_pcie_host_ops,
  202. .dw_pcie_ops = &dw_ls_pcie_ops,
  203. };
  204. static struct ls_pcie_drvdata ls2088_drvdata = {
  205. .lut_offset = 0x80000,
  206. .ltssm_shift = 0,
  207. .lut_dbg = 0x407fc,
  208. .ops = &ls_pcie_host_ops,
  209. .dw_pcie_ops = &dw_ls_pcie_ops,
  210. };
  211. static const struct of_device_id ls_pcie_of_match[] = {
  212. { .compatible = "fsl,ls1012a-pcie", .data = &ls1046_drvdata },
  213. { .compatible = "fsl,ls1021a-pcie", .data = &ls1021_drvdata },
  214. { .compatible = "fsl,ls1043a-pcie", .data = &ls1043_drvdata },
  215. { .compatible = "fsl,ls1046a-pcie", .data = &ls1046_drvdata },
  216. { .compatible = "fsl,ls2080a-pcie", .data = &ls2080_drvdata },
  217. { .compatible = "fsl,ls2085a-pcie", .data = &ls2080_drvdata },
  218. { .compatible = "fsl,ls2088a-pcie", .data = &ls2088_drvdata },
  219. { .compatible = "fsl,ls1088a-pcie", .data = &ls2088_drvdata },
  220. { },
  221. };
  222. static int __init ls_add_pcie_port(struct ls_pcie *pcie)
  223. {
  224. struct dw_pcie *pci = pcie->pci;
  225. struct pcie_port *pp = &pci->pp;
  226. struct device *dev = pci->dev;
  227. int ret;
  228. pp->ops = pcie->drvdata->ops;
  229. ret = dw_pcie_host_init(pp);
  230. if (ret) {
  231. dev_err(dev, "failed to initialize host\n");
  232. return ret;
  233. }
  234. return 0;
  235. }
  236. static int __init ls_pcie_probe(struct platform_device *pdev)
  237. {
  238. struct device *dev = &pdev->dev;
  239. struct dw_pcie *pci;
  240. struct ls_pcie *pcie;
  241. struct resource *dbi_base;
  242. int ret;
  243. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  244. if (!pcie)
  245. return -ENOMEM;
  246. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  247. if (!pci)
  248. return -ENOMEM;
  249. pcie->drvdata = of_device_get_match_data(dev);
  250. pci->dev = dev;
  251. pci->ops = pcie->drvdata->dw_pcie_ops;
  252. pcie->pci = pci;
  253. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  254. pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_base);
  255. if (IS_ERR(pci->dbi_base))
  256. return PTR_ERR(pci->dbi_base);
  257. pcie->lut = pci->dbi_base + pcie->drvdata->lut_offset;
  258. if (!ls_pcie_is_bridge(pcie))
  259. return -ENODEV;
  260. platform_set_drvdata(pdev, pcie);
  261. ret = ls_add_pcie_port(pcie);
  262. if (ret < 0)
  263. return ret;
  264. return 0;
  265. }
  266. static struct platform_driver ls_pcie_driver = {
  267. .driver = {
  268. .name = "layerscape-pcie",
  269. .of_match_table = ls_pcie_of_match,
  270. .suppress_bind_attrs = true,
  271. },
  272. };
  273. builtin_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);