pcie-hisi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for HiSilicon SoCs
  4. *
  5. * Copyright (C) 2015 HiSilicon Co., Ltd. http://www.hisilicon.com
  6. *
  7. * Authors: Zhou Wang <wangzhou1@hisilicon.com>
  8. * Dacai Zhu <zhudacai@hisilicon.com>
  9. * Gabriele Paoloni <gabriele.paoloni@huawei.com>
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/init.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_pci.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_device.h>
  18. #include <linux/pci.h>
  19. #include <linux/pci-acpi.h>
  20. #include <linux/pci-ecam.h>
  21. #include <linux/regmap.h>
  22. #include "../../pci.h"
  23. #if defined(CONFIG_PCI_HISI) || (defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS))
  24. static int hisi_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  25. int size, u32 *val)
  26. {
  27. struct pci_config_window *cfg = bus->sysdata;
  28. int dev = PCI_SLOT(devfn);
  29. if (bus->number == cfg->busr.start) {
  30. /* access only one slot on each root port */
  31. if (dev > 0)
  32. return PCIBIOS_DEVICE_NOT_FOUND;
  33. else
  34. return pci_generic_config_read32(bus, devfn, where,
  35. size, val);
  36. }
  37. return pci_generic_config_read(bus, devfn, where, size, val);
  38. }
  39. static int hisi_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  40. int where, int size, u32 val)
  41. {
  42. struct pci_config_window *cfg = bus->sysdata;
  43. int dev = PCI_SLOT(devfn);
  44. if (bus->number == cfg->busr.start) {
  45. /* access only one slot on each root port */
  46. if (dev > 0)
  47. return PCIBIOS_DEVICE_NOT_FOUND;
  48. else
  49. return pci_generic_config_write32(bus, devfn, where,
  50. size, val);
  51. }
  52. return pci_generic_config_write(bus, devfn, where, size, val);
  53. }
  54. static void __iomem *hisi_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
  55. int where)
  56. {
  57. struct pci_config_window *cfg = bus->sysdata;
  58. void __iomem *reg_base = cfg->priv;
  59. if (bus->number == cfg->busr.start)
  60. return reg_base + where;
  61. else
  62. return pci_ecam_map_bus(bus, devfn, where);
  63. }
  64. #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
  65. static int hisi_pcie_init(struct pci_config_window *cfg)
  66. {
  67. struct device *dev = cfg->parent;
  68. struct acpi_device *adev = to_acpi_device(dev);
  69. struct acpi_pci_root *root = acpi_driver_data(adev);
  70. struct resource *res;
  71. void __iomem *reg_base;
  72. int ret;
  73. /*
  74. * Retrieve RC base and size from a HISI0081 device with _UID
  75. * matching our segment.
  76. */
  77. res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
  78. if (!res)
  79. return -ENOMEM;
  80. ret = acpi_get_rc_resources(dev, "HISI0081", root->segment, res);
  81. if (ret) {
  82. dev_err(dev, "can't get rc base address\n");
  83. return -ENOMEM;
  84. }
  85. reg_base = devm_pci_remap_cfgspace(dev, res->start, resource_size(res));
  86. if (!reg_base)
  87. return -ENOMEM;
  88. cfg->priv = reg_base;
  89. return 0;
  90. }
  91. struct pci_ecam_ops hisi_pcie_ops = {
  92. .bus_shift = 20,
  93. .init = hisi_pcie_init,
  94. .pci_ops = {
  95. .map_bus = hisi_pcie_map_bus,
  96. .read = hisi_pcie_rd_conf,
  97. .write = hisi_pcie_wr_conf,
  98. }
  99. };
  100. #endif
  101. #ifdef CONFIG_PCI_HISI
  102. #include "pcie-designware.h"
  103. #define PCIE_SUBCTRL_SYS_STATE4_REG 0x6818
  104. #define PCIE_HIP06_CTRL_OFF 0x1000
  105. #define PCIE_SYS_STATE4 (PCIE_HIP06_CTRL_OFF + 0x31c)
  106. #define PCIE_LTSSM_LINKUP_STATE 0x11
  107. #define PCIE_LTSSM_STATE_MASK 0x3F
  108. #define to_hisi_pcie(x) dev_get_drvdata((x)->dev)
  109. struct hisi_pcie;
  110. struct pcie_soc_ops {
  111. int (*hisi_pcie_link_up)(struct hisi_pcie *hisi_pcie);
  112. };
  113. struct hisi_pcie {
  114. struct dw_pcie *pci;
  115. struct regmap *subctrl;
  116. u32 port_id;
  117. const struct pcie_soc_ops *soc_ops;
  118. };
  119. /* HipXX PCIe host only supports 32-bit config access */
  120. static int hisi_pcie_cfg_read(struct pcie_port *pp, int where, int size,
  121. u32 *val)
  122. {
  123. u32 reg;
  124. u32 reg_val;
  125. void *walker = &reg_val;
  126. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  127. walker += (where & 0x3);
  128. reg = where & ~0x3;
  129. reg_val = dw_pcie_readl_dbi(pci, reg);
  130. if (size == 1)
  131. *val = *(u8 __force *) walker;
  132. else if (size == 2)
  133. *val = *(u16 __force *) walker;
  134. else if (size == 4)
  135. *val = reg_val;
  136. else
  137. return PCIBIOS_BAD_REGISTER_NUMBER;
  138. return PCIBIOS_SUCCESSFUL;
  139. }
  140. /* HipXX PCIe host only supports 32-bit config access */
  141. static int hisi_pcie_cfg_write(struct pcie_port *pp, int where, int size,
  142. u32 val)
  143. {
  144. u32 reg_val;
  145. u32 reg;
  146. void *walker = &reg_val;
  147. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  148. walker += (where & 0x3);
  149. reg = where & ~0x3;
  150. if (size == 4)
  151. dw_pcie_writel_dbi(pci, reg, val);
  152. else if (size == 2) {
  153. reg_val = dw_pcie_readl_dbi(pci, reg);
  154. *(u16 __force *) walker = val;
  155. dw_pcie_writel_dbi(pci, reg, reg_val);
  156. } else if (size == 1) {
  157. reg_val = dw_pcie_readl_dbi(pci, reg);
  158. *(u8 __force *) walker = val;
  159. dw_pcie_writel_dbi(pci, reg, reg_val);
  160. } else
  161. return PCIBIOS_BAD_REGISTER_NUMBER;
  162. return PCIBIOS_SUCCESSFUL;
  163. }
  164. static int hisi_pcie_link_up_hip05(struct hisi_pcie *hisi_pcie)
  165. {
  166. u32 val;
  167. regmap_read(hisi_pcie->subctrl, PCIE_SUBCTRL_SYS_STATE4_REG +
  168. 0x100 * hisi_pcie->port_id, &val);
  169. return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
  170. }
  171. static int hisi_pcie_link_up_hip06(struct hisi_pcie *hisi_pcie)
  172. {
  173. struct dw_pcie *pci = hisi_pcie->pci;
  174. u32 val;
  175. val = dw_pcie_readl_dbi(pci, PCIE_SYS_STATE4);
  176. return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
  177. }
  178. static int hisi_pcie_link_up(struct dw_pcie *pci)
  179. {
  180. struct hisi_pcie *hisi_pcie = to_hisi_pcie(pci);
  181. return hisi_pcie->soc_ops->hisi_pcie_link_up(hisi_pcie);
  182. }
  183. static const struct dw_pcie_host_ops hisi_pcie_host_ops = {
  184. .rd_own_conf = hisi_pcie_cfg_read,
  185. .wr_own_conf = hisi_pcie_cfg_write,
  186. };
  187. static int hisi_add_pcie_port(struct hisi_pcie *hisi_pcie,
  188. struct platform_device *pdev)
  189. {
  190. struct dw_pcie *pci = hisi_pcie->pci;
  191. struct pcie_port *pp = &pci->pp;
  192. struct device *dev = &pdev->dev;
  193. int ret;
  194. u32 port_id;
  195. if (of_property_read_u32(dev->of_node, "port-id", &port_id)) {
  196. dev_err(dev, "failed to read port-id\n");
  197. return -EINVAL;
  198. }
  199. if (port_id > 3) {
  200. dev_err(dev, "Invalid port-id: %d\n", port_id);
  201. return -EINVAL;
  202. }
  203. hisi_pcie->port_id = port_id;
  204. pp->ops = &hisi_pcie_host_ops;
  205. ret = dw_pcie_host_init(pp);
  206. if (ret) {
  207. dev_err(dev, "failed to initialize host\n");
  208. return ret;
  209. }
  210. return 0;
  211. }
  212. static const struct dw_pcie_ops dw_pcie_ops = {
  213. .link_up = hisi_pcie_link_up,
  214. };
  215. static int hisi_pcie_probe(struct platform_device *pdev)
  216. {
  217. struct device *dev = &pdev->dev;
  218. struct dw_pcie *pci;
  219. struct hisi_pcie *hisi_pcie;
  220. struct resource *reg;
  221. int ret;
  222. hisi_pcie = devm_kzalloc(dev, sizeof(*hisi_pcie), GFP_KERNEL);
  223. if (!hisi_pcie)
  224. return -ENOMEM;
  225. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  226. if (!pci)
  227. return -ENOMEM;
  228. pci->dev = dev;
  229. pci->ops = &dw_pcie_ops;
  230. hisi_pcie->pci = pci;
  231. hisi_pcie->soc_ops = of_device_get_match_data(dev);
  232. hisi_pcie->subctrl =
  233. syscon_regmap_lookup_by_compatible("hisilicon,pcie-sas-subctrl");
  234. if (IS_ERR(hisi_pcie->subctrl)) {
  235. dev_err(dev, "cannot get subctrl base\n");
  236. return PTR_ERR(hisi_pcie->subctrl);
  237. }
  238. reg = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbi");
  239. pci->dbi_base = devm_pci_remap_cfg_resource(dev, reg);
  240. if (IS_ERR(pci->dbi_base))
  241. return PTR_ERR(pci->dbi_base);
  242. platform_set_drvdata(pdev, hisi_pcie);
  243. ret = hisi_add_pcie_port(hisi_pcie, pdev);
  244. if (ret)
  245. return ret;
  246. return 0;
  247. }
  248. static struct pcie_soc_ops hip05_ops = {
  249. &hisi_pcie_link_up_hip05
  250. };
  251. static struct pcie_soc_ops hip06_ops = {
  252. &hisi_pcie_link_up_hip06
  253. };
  254. static const struct of_device_id hisi_pcie_of_match[] = {
  255. {
  256. .compatible = "hisilicon,hip05-pcie",
  257. .data = (void *) &hip05_ops,
  258. },
  259. {
  260. .compatible = "hisilicon,hip06-pcie",
  261. .data = (void *) &hip06_ops,
  262. },
  263. {},
  264. };
  265. static struct platform_driver hisi_pcie_driver = {
  266. .probe = hisi_pcie_probe,
  267. .driver = {
  268. .name = "hisi-pcie",
  269. .of_match_table = hisi_pcie_of_match,
  270. .suppress_bind_attrs = true,
  271. },
  272. };
  273. builtin_platform_driver(hisi_pcie_driver);
  274. static int hisi_pcie_almost_ecam_probe(struct platform_device *pdev)
  275. {
  276. struct device *dev = &pdev->dev;
  277. struct pci_ecam_ops *ops;
  278. ops = (struct pci_ecam_ops *)of_device_get_match_data(dev);
  279. return pci_host_common_probe(pdev, ops);
  280. }
  281. static int hisi_pcie_platform_init(struct pci_config_window *cfg)
  282. {
  283. struct device *dev = cfg->parent;
  284. struct platform_device *pdev = to_platform_device(dev);
  285. struct resource *res;
  286. void __iomem *reg_base;
  287. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  288. if (!res) {
  289. dev_err(dev, "missing \"reg[1]\"property\n");
  290. return -EINVAL;
  291. }
  292. reg_base = devm_pci_remap_cfgspace(dev, res->start, resource_size(res));
  293. if (!reg_base)
  294. return -ENOMEM;
  295. cfg->priv = reg_base;
  296. return 0;
  297. }
  298. struct pci_ecam_ops hisi_pcie_platform_ops = {
  299. .bus_shift = 20,
  300. .init = hisi_pcie_platform_init,
  301. .pci_ops = {
  302. .map_bus = hisi_pcie_map_bus,
  303. .read = hisi_pcie_rd_conf,
  304. .write = hisi_pcie_wr_conf,
  305. }
  306. };
  307. static const struct of_device_id hisi_pcie_almost_ecam_of_match[] = {
  308. {
  309. .compatible = "hisilicon,hip06-pcie-ecam",
  310. .data = (void *) &hisi_pcie_platform_ops,
  311. },
  312. {
  313. .compatible = "hisilicon,hip07-pcie-ecam",
  314. .data = (void *) &hisi_pcie_platform_ops,
  315. },
  316. {},
  317. };
  318. static struct platform_driver hisi_pcie_almost_ecam_driver = {
  319. .probe = hisi_pcie_almost_ecam_probe,
  320. .driver = {
  321. .name = "hisi-pcie-almost-ecam",
  322. .of_match_table = hisi_pcie_almost_ecam_of_match,
  323. .suppress_bind_attrs = true,
  324. },
  325. };
  326. builtin_platform_driver(hisi_pcie_almost_ecam_driver);
  327. #endif
  328. #endif