pcie-hisi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * PCIe host controller driver for HiSilicon Hip05 SoC
  3. *
  4. * Copyright (C) 2015 HiSilicon Co., Ltd. http://www.hisilicon.com
  5. *
  6. * Author: Zhou Wang <wangzhou1@hisilicon.com>
  7. * Dacai Zhu <zhudacai@hisilicon.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/interrupt.h>
  14. #include <linux/module.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_pci.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/regmap.h>
  20. #include "pcie-designware.h"
  21. #define PCIE_SUBCTRL_SYS_STATE4_REG 0x6818
  22. #define PCIE_LTSSM_LINKUP_STATE 0x11
  23. #define PCIE_LTSSM_STATE_MASK 0x3F
  24. #define to_hisi_pcie(x) container_of(x, struct hisi_pcie, pp)
  25. struct hisi_pcie {
  26. struct regmap *subctrl;
  27. void __iomem *reg_base;
  28. u32 port_id;
  29. struct pcie_port pp;
  30. };
  31. static inline void hisi_pcie_apb_writel(struct hisi_pcie *pcie,
  32. u32 val, u32 reg)
  33. {
  34. writel(val, pcie->reg_base + reg);
  35. }
  36. static inline u32 hisi_pcie_apb_readl(struct hisi_pcie *pcie, u32 reg)
  37. {
  38. return readl(pcie->reg_base + reg);
  39. }
  40. /* Hip05 PCIe host only supports 32-bit config access */
  41. static int hisi_pcie_cfg_read(struct pcie_port *pp, int where, int size,
  42. u32 *val)
  43. {
  44. u32 reg;
  45. u32 reg_val;
  46. struct hisi_pcie *pcie = to_hisi_pcie(pp);
  47. void *walker = &reg_val;
  48. walker += (where & 0x3);
  49. reg = where & ~0x3;
  50. reg_val = hisi_pcie_apb_readl(pcie, reg);
  51. if (size == 1)
  52. *val = *(u8 __force *) walker;
  53. else if (size == 2)
  54. *val = *(u16 __force *) walker;
  55. else if (size == 4)
  56. *val = reg_val;
  57. else
  58. return PCIBIOS_BAD_REGISTER_NUMBER;
  59. return PCIBIOS_SUCCESSFUL;
  60. }
  61. /* Hip05 PCIe host only supports 32-bit config access */
  62. static int hisi_pcie_cfg_write(struct pcie_port *pp, int where, int size,
  63. u32 val)
  64. {
  65. u32 reg_val;
  66. u32 reg;
  67. struct hisi_pcie *pcie = to_hisi_pcie(pp);
  68. void *walker = &reg_val;
  69. walker += (where & 0x3);
  70. reg = where & ~0x3;
  71. if (size == 4)
  72. hisi_pcie_apb_writel(pcie, val, reg);
  73. else if (size == 2) {
  74. reg_val = hisi_pcie_apb_readl(pcie, reg);
  75. *(u16 __force *) walker = val;
  76. hisi_pcie_apb_writel(pcie, reg_val, reg);
  77. } else if (size == 1) {
  78. reg_val = hisi_pcie_apb_readl(pcie, reg);
  79. *(u8 __force *) walker = val;
  80. hisi_pcie_apb_writel(pcie, reg_val, reg);
  81. } else
  82. return PCIBIOS_BAD_REGISTER_NUMBER;
  83. return PCIBIOS_SUCCESSFUL;
  84. }
  85. static int hisi_pcie_link_up(struct pcie_port *pp)
  86. {
  87. u32 val;
  88. struct hisi_pcie *hisi_pcie = to_hisi_pcie(pp);
  89. regmap_read(hisi_pcie->subctrl, PCIE_SUBCTRL_SYS_STATE4_REG +
  90. 0x100 * hisi_pcie->port_id, &val);
  91. return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
  92. }
  93. static struct pcie_host_ops hisi_pcie_host_ops = {
  94. .rd_own_conf = hisi_pcie_cfg_read,
  95. .wr_own_conf = hisi_pcie_cfg_write,
  96. .link_up = hisi_pcie_link_up,
  97. };
  98. static int hisi_add_pcie_port(struct pcie_port *pp,
  99. struct platform_device *pdev)
  100. {
  101. int ret;
  102. u32 port_id;
  103. struct hisi_pcie *hisi_pcie = to_hisi_pcie(pp);
  104. if (of_property_read_u32(pdev->dev.of_node, "port-id", &port_id)) {
  105. dev_err(&pdev->dev, "failed to read port-id\n");
  106. return -EINVAL;
  107. }
  108. if (port_id > 3) {
  109. dev_err(&pdev->dev, "Invalid port-id: %d\n", port_id);
  110. return -EINVAL;
  111. }
  112. hisi_pcie->port_id = port_id;
  113. pp->ops = &hisi_pcie_host_ops;
  114. ret = dw_pcie_host_init(pp);
  115. if (ret) {
  116. dev_err(&pdev->dev, "failed to initialize host\n");
  117. return ret;
  118. }
  119. return 0;
  120. }
  121. static int hisi_pcie_probe(struct platform_device *pdev)
  122. {
  123. struct hisi_pcie *hisi_pcie;
  124. struct pcie_port *pp;
  125. struct resource *reg;
  126. int ret;
  127. hisi_pcie = devm_kzalloc(&pdev->dev, sizeof(*hisi_pcie), GFP_KERNEL);
  128. if (!hisi_pcie)
  129. return -ENOMEM;
  130. pp = &hisi_pcie->pp;
  131. pp->dev = &pdev->dev;
  132. hisi_pcie->subctrl =
  133. syscon_regmap_lookup_by_compatible("hisilicon,pcie-sas-subctrl");
  134. if (IS_ERR(hisi_pcie->subctrl)) {
  135. dev_err(pp->dev, "cannot get subctrl base\n");
  136. return PTR_ERR(hisi_pcie->subctrl);
  137. }
  138. reg = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbi");
  139. hisi_pcie->reg_base = devm_ioremap_resource(&pdev->dev, reg);
  140. if (IS_ERR(hisi_pcie->reg_base)) {
  141. dev_err(pp->dev, "cannot get rc_dbi base\n");
  142. return PTR_ERR(hisi_pcie->reg_base);
  143. }
  144. hisi_pcie->pp.dbi_base = hisi_pcie->reg_base;
  145. ret = hisi_add_pcie_port(pp, pdev);
  146. if (ret)
  147. return ret;
  148. platform_set_drvdata(pdev, hisi_pcie);
  149. dev_warn(pp->dev, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n");
  150. return 0;
  151. }
  152. static const struct of_device_id hisi_pcie_of_match[] = {
  153. {.compatible = "hisilicon,hip05-pcie",},
  154. {},
  155. };
  156. MODULE_DEVICE_TABLE(of, hisi_pcie_of_match);
  157. static struct platform_driver hisi_pcie_driver = {
  158. .probe = hisi_pcie_probe,
  159. .driver = {
  160. .name = "hisi-pcie",
  161. .of_match_table = hisi_pcie_of_match,
  162. },
  163. };
  164. module_platform_driver(hisi_pcie_driver);