pcie-hisi.c 9.4 KB

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