pcie-hisi.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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_ACPI) && defined(CONFIG_PCI_QUIRKS)
  27. static int hisi_pcie_acpi_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_acpi_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. static int hisi_pcie_init(struct pci_config_window *cfg)
  68. {
  69. struct device *dev = cfg->parent;
  70. struct acpi_device *adev = to_acpi_device(dev);
  71. struct acpi_pci_root *root = acpi_driver_data(adev);
  72. struct resource *res;
  73. void __iomem *reg_base;
  74. int ret;
  75. /*
  76. * Retrieve RC base and size from a HISI0081 device with _UID
  77. * matching our segment.
  78. */
  79. res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
  80. if (!res)
  81. return -ENOMEM;
  82. ret = acpi_get_rc_resources(dev, "HISI0081", root->segment, res);
  83. if (ret) {
  84. dev_err(dev, "can't get rc base address\n");
  85. return -ENOMEM;
  86. }
  87. reg_base = devm_ioremap(dev, res->start, resource_size(res));
  88. if (!reg_base)
  89. return -ENOMEM;
  90. cfg->priv = reg_base;
  91. return 0;
  92. }
  93. struct pci_ecam_ops hisi_pcie_ops = {
  94. .bus_shift = 20,
  95. .init = hisi_pcie_init,
  96. .pci_ops = {
  97. .map_bus = hisi_pcie_map_bus,
  98. .read = hisi_pcie_acpi_rd_conf,
  99. .write = hisi_pcie_acpi_wr_conf,
  100. }
  101. };
  102. #endif
  103. #ifdef CONFIG_PCI_HISI
  104. #include "pcie-designware.h"
  105. #define PCIE_SUBCTRL_SYS_STATE4_REG 0x6818
  106. #define PCIE_HIP06_CTRL_OFF 0x1000
  107. #define PCIE_SYS_STATE4 (PCIE_HIP06_CTRL_OFF + 0x31c)
  108. #define PCIE_LTSSM_LINKUP_STATE 0x11
  109. #define PCIE_LTSSM_STATE_MASK 0x3F
  110. #define to_hisi_pcie(x) container_of(x, struct hisi_pcie, pp)
  111. struct hisi_pcie;
  112. struct pcie_soc_ops {
  113. int (*hisi_pcie_link_up)(struct hisi_pcie *hisi_pcie);
  114. };
  115. struct hisi_pcie {
  116. struct pcie_port pp; /* pp.dbi_base is DT rc_dbi */
  117. struct regmap *subctrl;
  118. u32 port_id;
  119. struct pcie_soc_ops *soc_ops;
  120. };
  121. /* HipXX PCIe host only supports 32-bit config access */
  122. static int hisi_pcie_cfg_read(struct pcie_port *pp, int where, int size,
  123. u32 *val)
  124. {
  125. u32 reg;
  126. u32 reg_val;
  127. void *walker = &reg_val;
  128. walker += (where & 0x3);
  129. reg = where & ~0x3;
  130. reg_val = dw_pcie_readl_rc(pp, reg);
  131. if (size == 1)
  132. *val = *(u8 __force *) walker;
  133. else if (size == 2)
  134. *val = *(u16 __force *) walker;
  135. else if (size == 4)
  136. *val = reg_val;
  137. else
  138. return PCIBIOS_BAD_REGISTER_NUMBER;
  139. return PCIBIOS_SUCCESSFUL;
  140. }
  141. /* HipXX PCIe host only supports 32-bit config access */
  142. static int hisi_pcie_cfg_write(struct pcie_port *pp, int where, int size,
  143. u32 val)
  144. {
  145. u32 reg_val;
  146. u32 reg;
  147. void *walker = &reg_val;
  148. walker += (where & 0x3);
  149. reg = where & ~0x3;
  150. if (size == 4)
  151. dw_pcie_writel_rc(pp, reg, val);
  152. else if (size == 2) {
  153. reg_val = dw_pcie_readl_rc(pp, reg);
  154. *(u16 __force *) walker = val;
  155. dw_pcie_writel_rc(pp, reg, reg_val);
  156. } else if (size == 1) {
  157. reg_val = dw_pcie_readl_rc(pp, reg);
  158. *(u8 __force *) walker = val;
  159. dw_pcie_writel_rc(pp, 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 pcie_port *pp = &hisi_pcie->pp;
  174. u32 val;
  175. val = dw_pcie_readl_rc(pp, PCIE_SYS_STATE4);
  176. return ((val & PCIE_LTSSM_STATE_MASK) == PCIE_LTSSM_LINKUP_STATE);
  177. }
  178. static int hisi_pcie_link_up(struct pcie_port *pp)
  179. {
  180. struct hisi_pcie *hisi_pcie = to_hisi_pcie(pp);
  181. return hisi_pcie->soc_ops->hisi_pcie_link_up(hisi_pcie);
  182. }
  183. static struct pcie_host_ops hisi_pcie_host_ops = {
  184. .rd_own_conf = hisi_pcie_cfg_read,
  185. .wr_own_conf = hisi_pcie_cfg_write,
  186. .link_up = hisi_pcie_link_up,
  187. };
  188. static int hisi_add_pcie_port(struct hisi_pcie *hisi_pcie,
  189. struct platform_device *pdev)
  190. {
  191. struct pcie_port *pp = &hisi_pcie->pp;
  192. struct device *dev = pp->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 int hisi_pcie_probe(struct platform_device *pdev)
  213. {
  214. struct device *dev = &pdev->dev;
  215. struct hisi_pcie *hisi_pcie;
  216. struct pcie_port *pp;
  217. const struct of_device_id *match;
  218. struct resource *reg;
  219. struct device_driver *driver;
  220. int ret;
  221. hisi_pcie = devm_kzalloc(dev, sizeof(*hisi_pcie), GFP_KERNEL);
  222. if (!hisi_pcie)
  223. return -ENOMEM;
  224. pp = &hisi_pcie->pp;
  225. pp->dev = dev;
  226. driver = dev->driver;
  227. match = of_match_device(driver->of_match_table, dev);
  228. hisi_pcie->soc_ops = (struct pcie_soc_ops *) match->data;
  229. hisi_pcie->subctrl =
  230. syscon_regmap_lookup_by_compatible("hisilicon,pcie-sas-subctrl");
  231. if (IS_ERR(hisi_pcie->subctrl)) {
  232. dev_err(dev, "cannot get subctrl base\n");
  233. return PTR_ERR(hisi_pcie->subctrl);
  234. }
  235. reg = platform_get_resource_byname(pdev, IORESOURCE_MEM, "rc_dbi");
  236. pp->dbi_base = devm_ioremap_resource(dev, reg);
  237. if (IS_ERR(pp->dbi_base))
  238. return PTR_ERR(pp->dbi_base);
  239. ret = hisi_add_pcie_port(hisi_pcie, pdev);
  240. if (ret)
  241. return ret;
  242. return 0;
  243. }
  244. static struct pcie_soc_ops hip05_ops = {
  245. &hisi_pcie_link_up_hip05
  246. };
  247. static struct pcie_soc_ops hip06_ops = {
  248. &hisi_pcie_link_up_hip06
  249. };
  250. static const struct of_device_id hisi_pcie_of_match[] = {
  251. {
  252. .compatible = "hisilicon,hip05-pcie",
  253. .data = (void *) &hip05_ops,
  254. },
  255. {
  256. .compatible = "hisilicon,hip06-pcie",
  257. .data = (void *) &hip06_ops,
  258. },
  259. {},
  260. };
  261. static struct platform_driver hisi_pcie_driver = {
  262. .probe = hisi_pcie_probe,
  263. .driver = {
  264. .name = "hisi-pcie",
  265. .of_match_table = hisi_pcie_of_match,
  266. },
  267. };
  268. builtin_platform_driver(hisi_pcie_driver);
  269. #endif