pcie-hisi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/module.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_LTSSM_LINKUP_STATE 0x11
  24. #define PCIE_LTSSM_STATE_MASK 0x3F
  25. #define PCIE_SUBCTRL_SYS_STATE4_REG 0x6818
  26. #define PCIE_SYS_STATE4 0x31c
  27. #define PCIE_HIP06_CTRL_OFF 0x1000
  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 *pcie);
  32. };
  33. struct hisi_pcie {
  34. struct regmap *subctrl;
  35. void __iomem *reg_base;
  36. u32 port_id;
  37. struct pcie_port pp;
  38. struct pcie_soc_ops *soc_ops;
  39. };
  40. static inline void hisi_pcie_apb_writel(struct hisi_pcie *pcie,
  41. u32 val, u32 reg)
  42. {
  43. writel(val, pcie->reg_base + reg);
  44. }
  45. static inline u32 hisi_pcie_apb_readl(struct hisi_pcie *pcie, u32 reg)
  46. {
  47. return readl(pcie->reg_base + reg);
  48. }
  49. /* HipXX PCIe host only supports 32-bit config access */
  50. static int hisi_pcie_cfg_read(struct pcie_port *pp, int where, int size,
  51. u32 *val)
  52. {
  53. u32 reg;
  54. u32 reg_val;
  55. struct hisi_pcie *pcie = to_hisi_pcie(pp);
  56. void *walker = &reg_val;
  57. walker += (where & 0x3);
  58. reg = where & ~0x3;
  59. reg_val = hisi_pcie_apb_readl(pcie, reg);
  60. if (size == 1)
  61. *val = *(u8 __force *) walker;
  62. else if (size == 2)
  63. *val = *(u16 __force *) walker;
  64. else if (size == 4)
  65. *val = reg_val;
  66. else
  67. return PCIBIOS_BAD_REGISTER_NUMBER;
  68. return PCIBIOS_SUCCESSFUL;
  69. }
  70. /* HipXX PCIe host only supports 32-bit config access */
  71. static int hisi_pcie_cfg_write(struct pcie_port *pp, int where, int size,
  72. u32 val)
  73. {
  74. u32 reg_val;
  75. u32 reg;
  76. struct hisi_pcie *pcie = to_hisi_pcie(pp);
  77. void *walker = &reg_val;
  78. walker += (where & 0x3);
  79. reg = where & ~0x3;
  80. if (size == 4)
  81. hisi_pcie_apb_writel(pcie, val, reg);
  82. else if (size == 2) {
  83. reg_val = hisi_pcie_apb_readl(pcie, reg);
  84. *(u16 __force *) walker = val;
  85. hisi_pcie_apb_writel(pcie, reg_val, reg);
  86. } else if (size == 1) {
  87. reg_val = hisi_pcie_apb_readl(pcie, reg);
  88. *(u8 __force *) walker = val;
  89. hisi_pcie_apb_writel(pcie, reg_val, reg);
  90. } else
  91. return PCIBIOS_BAD_REGISTER_NUMBER;
  92. return PCIBIOS_SUCCESSFUL;
  93. }
  94. static int hisi_pcie_link_up_hip05(struct hisi_pcie *hisi_pcie)
  95. {
  96. u32 val;
  97. regmap_read(hisi_pcie->subctrl, PCIE_SUBCTRL_SYS_STATE4_REG +
  98. 0x100 * hisi_pcie->port_id, &val);
  99. return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
  100. }
  101. static int hisi_pcie_link_up_hip06(struct hisi_pcie *hisi_pcie)
  102. {
  103. u32 val;
  104. val = hisi_pcie_apb_readl(hisi_pcie, PCIE_HIP06_CTRL_OFF +
  105. PCIE_SYS_STATE4);
  106. return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
  107. }
  108. static int hisi_pcie_link_up(struct pcie_port *pp)
  109. {
  110. struct hisi_pcie *hisi_pcie = to_hisi_pcie(pp);
  111. return hisi_pcie->soc_ops->hisi_pcie_link_up(hisi_pcie);
  112. }
  113. static struct pcie_host_ops hisi_pcie_host_ops = {
  114. .rd_own_conf = hisi_pcie_cfg_read,
  115. .wr_own_conf = hisi_pcie_cfg_write,
  116. .link_up = hisi_pcie_link_up,
  117. };
  118. static int hisi_add_pcie_port(struct pcie_port *pp,
  119. struct platform_device *pdev)
  120. {
  121. int ret;
  122. u32 port_id;
  123. struct hisi_pcie *hisi_pcie = to_hisi_pcie(pp);
  124. if (of_property_read_u32(pdev->dev.of_node, "port-id", &port_id)) {
  125. dev_err(&pdev->dev, "failed to read port-id\n");
  126. return -EINVAL;
  127. }
  128. if (port_id > 3) {
  129. dev_err(&pdev->dev, "Invalid port-id: %d\n", port_id);
  130. return -EINVAL;
  131. }
  132. hisi_pcie->port_id = port_id;
  133. pp->ops = &hisi_pcie_host_ops;
  134. ret = dw_pcie_host_init(pp);
  135. if (ret) {
  136. dev_err(&pdev->dev, "failed to initialize host\n");
  137. return ret;
  138. }
  139. return 0;
  140. }
  141. static int hisi_pcie_probe(struct platform_device *pdev)
  142. {
  143. struct hisi_pcie *hisi_pcie;
  144. struct pcie_port *pp;
  145. const struct of_device_id *match;
  146. struct resource *reg;
  147. struct device_driver *driver;
  148. int ret;
  149. hisi_pcie = devm_kzalloc(&pdev->dev, sizeof(*hisi_pcie), GFP_KERNEL);
  150. if (!hisi_pcie)
  151. return -ENOMEM;
  152. pp = &hisi_pcie->pp;
  153. pp->dev = &pdev->dev;
  154. driver = (pdev->dev).driver;
  155. match = of_match_device(driver->of_match_table, &pdev->dev);
  156. hisi_pcie->soc_ops = (struct pcie_soc_ops *) match->data;
  157. hisi_pcie->subctrl =
  158. syscon_regmap_lookup_by_compatible("hisilicon,pcie-sas-subctrl");
  159. if (IS_ERR(hisi_pcie->subctrl)) {
  160. dev_err(pp->dev, "cannot get subctrl base\n");
  161. return PTR_ERR(hisi_pcie->subctrl);
  162. }
  163. reg = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbi");
  164. hisi_pcie->reg_base = devm_ioremap_resource(&pdev->dev, reg);
  165. if (IS_ERR(hisi_pcie->reg_base)) {
  166. dev_err(pp->dev, "cannot get rc_dbi base\n");
  167. return PTR_ERR(hisi_pcie->reg_base);
  168. }
  169. hisi_pcie->pp.dbi_base = hisi_pcie->reg_base;
  170. ret = hisi_add_pcie_port(pp, pdev);
  171. if (ret)
  172. return ret;
  173. platform_set_drvdata(pdev, hisi_pcie);
  174. dev_warn(pp->dev, "only 32-bit config accesses supported; smaller writes may corrupt adjacent RW1C fields\n");
  175. return 0;
  176. }
  177. static struct pcie_soc_ops hip05_ops = {
  178. &hisi_pcie_link_up_hip05
  179. };
  180. static struct pcie_soc_ops hip06_ops = {
  181. &hisi_pcie_link_up_hip06
  182. };
  183. static const struct of_device_id hisi_pcie_of_match[] = {
  184. {
  185. .compatible = "hisilicon,hip05-pcie",
  186. .data = (void *) &hip05_ops,
  187. },
  188. {
  189. .compatible = "hisilicon,hip06-pcie",
  190. .data = (void *) &hip06_ops,
  191. },
  192. {},
  193. };
  194. MODULE_DEVICE_TABLE(of, hisi_pcie_of_match);
  195. static struct platform_driver hisi_pcie_driver = {
  196. .probe = hisi_pcie_probe,
  197. .driver = {
  198. .name = "hisi-pcie",
  199. .of_match_table = hisi_pcie_of_match,
  200. },
  201. };
  202. module_platform_driver(hisi_pcie_driver);
  203. MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>");
  204. MODULE_AUTHOR("Dacai Zhu <zhudacai@hisilicon.com>");
  205. MODULE_AUTHOR("Gabriele Paoloni <gabriele.paoloni@huawei.com>");
  206. MODULE_LICENSE("GPL v2");