pcie-hisi.c 5.7 KB

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