pci-layerscape.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/pci.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/resource.h>
  23. #include <linux/mfd/syscon.h>
  24. #include <linux/regmap.h>
  25. #include "pcie-designware.h"
  26. /* PEX1/2 Misc Ports Status Register */
  27. #define SCFG_PEXMSCPORTSR(pex_idx) (0x94 + (pex_idx) * 4)
  28. #define LTSSM_STATE_SHIFT 20
  29. #define LTSSM_STATE_MASK 0x3f
  30. #define LTSSM_PCIE_L0 0x11 /* L0 state */
  31. /* Symbol Timer Register and Filter Mask Register 1 */
  32. #define PCIE_STRFMR1 0x71c
  33. struct ls_pcie {
  34. struct list_head node;
  35. struct device *dev;
  36. struct pci_bus *bus;
  37. void __iomem *dbi;
  38. struct regmap *scfg;
  39. struct pcie_port pp;
  40. int index;
  41. int msi_irq;
  42. };
  43. #define to_ls_pcie(x) container_of(x, struct ls_pcie, pp)
  44. static int ls_pcie_link_up(struct pcie_port *pp)
  45. {
  46. u32 state;
  47. struct ls_pcie *pcie = to_ls_pcie(pp);
  48. regmap_read(pcie->scfg, SCFG_PEXMSCPORTSR(pcie->index), &state);
  49. state = (state >> LTSSM_STATE_SHIFT) & LTSSM_STATE_MASK;
  50. if (state < LTSSM_PCIE_L0)
  51. return 0;
  52. return 1;
  53. }
  54. static void ls_pcie_host_init(struct pcie_port *pp)
  55. {
  56. struct ls_pcie *pcie = to_ls_pcie(pp);
  57. int count = 0;
  58. u32 val;
  59. dw_pcie_setup_rc(pp);
  60. while (!ls_pcie_link_up(pp)) {
  61. usleep_range(100, 1000);
  62. count++;
  63. if (count >= 200) {
  64. dev_err(pp->dev, "phy link never came up\n");
  65. return;
  66. }
  67. }
  68. /*
  69. * LS1021A Workaround for internal TKT228622
  70. * to fix the INTx hang issue
  71. */
  72. val = ioread32(pcie->dbi + PCIE_STRFMR1);
  73. val &= 0xffff;
  74. iowrite32(val, pcie->dbi + PCIE_STRFMR1);
  75. }
  76. static struct pcie_host_ops ls_pcie_host_ops = {
  77. .link_up = ls_pcie_link_up,
  78. .host_init = ls_pcie_host_init,
  79. };
  80. static int ls_add_pcie_port(struct ls_pcie *pcie)
  81. {
  82. struct pcie_port *pp;
  83. int ret;
  84. pp = &pcie->pp;
  85. pp->dev = pcie->dev;
  86. pp->dbi_base = pcie->dbi;
  87. pp->root_bus_nr = -1;
  88. pp->ops = &ls_pcie_host_ops;
  89. ret = dw_pcie_host_init(pp);
  90. if (ret) {
  91. dev_err(pp->dev, "failed to initialize host\n");
  92. return ret;
  93. }
  94. return 0;
  95. }
  96. static int __init ls_pcie_probe(struct platform_device *pdev)
  97. {
  98. struct ls_pcie *pcie;
  99. struct resource *dbi_base;
  100. u32 index[2];
  101. int ret;
  102. pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL);
  103. if (!pcie)
  104. return -ENOMEM;
  105. pcie->dev = &pdev->dev;
  106. dbi_base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  107. pcie->dbi = devm_ioremap_resource(&pdev->dev, dbi_base);
  108. if (IS_ERR(pcie->dbi)) {
  109. dev_err(&pdev->dev, "missing *regs* space\n");
  110. return PTR_ERR(pcie->dbi);
  111. }
  112. pcie->scfg = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  113. "fsl,pcie-scfg");
  114. if (IS_ERR(pcie->scfg)) {
  115. dev_err(&pdev->dev, "No syscfg phandle specified\n");
  116. return PTR_ERR(pcie->scfg);
  117. }
  118. ret = of_property_read_u32_array(pdev->dev.of_node,
  119. "fsl,pcie-scfg", index, 2);
  120. if (ret)
  121. return ret;
  122. pcie->index = index[1];
  123. ret = ls_add_pcie_port(pcie);
  124. if (ret < 0)
  125. return ret;
  126. platform_set_drvdata(pdev, pcie);
  127. return 0;
  128. }
  129. static const struct of_device_id ls_pcie_of_match[] = {
  130. { .compatible = "fsl,ls1021a-pcie" },
  131. { },
  132. };
  133. MODULE_DEVICE_TABLE(of, ls_pcie_of_match);
  134. static struct platform_driver ls_pcie_driver = {
  135. .driver = {
  136. .name = "layerscape-pcie",
  137. .of_match_table = ls_pcie_of_match,
  138. },
  139. };
  140. module_platform_driver_probe(ls_pcie_driver, ls_pcie_probe);
  141. MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@freescale.com>");
  142. MODULE_DESCRIPTION("Freescale Layerscape PCIe host controller driver");
  143. MODULE_LICENSE("GPL v2");