pcie-hisi.c 5.4 KB

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