pcie-al.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Amazon's Annapurna Labs IP (used in chips
  4. * such as Graviton and Alpine)
  5. *
  6. * Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  7. *
  8. * Author: Jonathan Chocron <[email protected]>
  9. */
  10. #include <linux/pci.h>
  11. #include <linux/pci-ecam.h>
  12. #include <linux/pci-acpi.h>
  13. #include "../../pci.h"
  14. #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
  15. struct al_pcie_acpi {
  16. void __iomem *dbi_base;
  17. };
  18. static void __iomem *al_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
  19. int where)
  20. {
  21. struct pci_config_window *cfg = bus->sysdata;
  22. struct al_pcie_acpi *pcie = cfg->priv;
  23. void __iomem *dbi_base = pcie->dbi_base;
  24. if (bus->number == cfg->busr.start) {
  25. /*
  26. * The DW PCIe core doesn't filter out transactions to other
  27. * devices/functions on the root bus num, so we do this here.
  28. */
  29. if (PCI_SLOT(devfn) > 0)
  30. return NULL;
  31. else
  32. return dbi_base + where;
  33. }
  34. return pci_ecam_map_bus(bus, devfn, where);
  35. }
  36. static int al_pcie_init(struct pci_config_window *cfg)
  37. {
  38. struct device *dev = cfg->parent;
  39. struct acpi_device *adev = to_acpi_device(dev);
  40. struct acpi_pci_root *root = acpi_driver_data(adev);
  41. struct al_pcie_acpi *al_pcie;
  42. struct resource *res;
  43. int ret;
  44. al_pcie = devm_kzalloc(dev, sizeof(*al_pcie), GFP_KERNEL);
  45. if (!al_pcie)
  46. return -ENOMEM;
  47. res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
  48. if (!res)
  49. return -ENOMEM;
  50. ret = acpi_get_rc_resources(dev, "AMZN0001", root->segment, res);
  51. if (ret) {
  52. dev_err(dev, "can't get rc dbi base address for SEG %d\n",
  53. root->segment);
  54. return ret;
  55. }
  56. dev_dbg(dev, "Root port dbi res: %pR\n", res);
  57. al_pcie->dbi_base = devm_pci_remap_cfg_resource(dev, res);
  58. if (IS_ERR(al_pcie->dbi_base))
  59. return PTR_ERR(al_pcie->dbi_base);
  60. cfg->priv = al_pcie;
  61. return 0;
  62. }
  63. const struct pci_ecam_ops al_pcie_ops = {
  64. .init = al_pcie_init,
  65. .pci_ops = {
  66. .map_bus = al_pcie_map_bus,
  67. .read = pci_generic_config_read,
  68. .write = pci_generic_config_write,
  69. }
  70. };
  71. #endif /* defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS) */
  72. #ifdef CONFIG_PCIE_AL
  73. #include <linux/of_pci.h>
  74. #include "pcie-designware.h"
  75. #define AL_PCIE_REV_ID_2 2
  76. #define AL_PCIE_REV_ID_3 3
  77. #define AL_PCIE_REV_ID_4 4
  78. #define AXI_BASE_OFFSET 0x0
  79. #define DEVICE_ID_OFFSET 0x16c
  80. #define DEVICE_REV_ID 0x0
  81. #define DEVICE_REV_ID_DEV_ID_MASK GENMASK(31, 16)
  82. #define DEVICE_REV_ID_DEV_ID_X4 0
  83. #define DEVICE_REV_ID_DEV_ID_X8 2
  84. #define DEVICE_REV_ID_DEV_ID_X16 4
  85. #define OB_CTRL_REV1_2_OFFSET 0x0040
  86. #define OB_CTRL_REV3_5_OFFSET 0x0030
  87. #define CFG_TARGET_BUS 0x0
  88. #define CFG_TARGET_BUS_MASK_MASK GENMASK(7, 0)
  89. #define CFG_TARGET_BUS_BUSNUM_MASK GENMASK(15, 8)
  90. #define CFG_CONTROL 0x4
  91. #define CFG_CONTROL_SUBBUS_MASK GENMASK(15, 8)
  92. #define CFG_CONTROL_SEC_BUS_MASK GENMASK(23, 16)
  93. struct al_pcie_reg_offsets {
  94. unsigned int ob_ctrl;
  95. };
  96. struct al_pcie_target_bus_cfg {
  97. u8 reg_val;
  98. u8 reg_mask;
  99. u8 ecam_mask;
  100. };
  101. struct al_pcie {
  102. struct dw_pcie *pci;
  103. void __iomem *controller_base; /* base of PCIe unit (not DW core) */
  104. struct device *dev;
  105. resource_size_t ecam_size;
  106. unsigned int controller_rev_id;
  107. struct al_pcie_reg_offsets reg_offsets;
  108. struct al_pcie_target_bus_cfg target_bus_cfg;
  109. };
  110. #define to_al_pcie(x) dev_get_drvdata((x)->dev)
  111. static inline u32 al_pcie_controller_readl(struct al_pcie *pcie, u32 offset)
  112. {
  113. return readl_relaxed(pcie->controller_base + offset);
  114. }
  115. static inline void al_pcie_controller_writel(struct al_pcie *pcie, u32 offset,
  116. u32 val)
  117. {
  118. writel_relaxed(val, pcie->controller_base + offset);
  119. }
  120. static int al_pcie_rev_id_get(struct al_pcie *pcie, unsigned int *rev_id)
  121. {
  122. u32 dev_rev_id_val;
  123. u32 dev_id_val;
  124. dev_rev_id_val = al_pcie_controller_readl(pcie, AXI_BASE_OFFSET +
  125. DEVICE_ID_OFFSET +
  126. DEVICE_REV_ID);
  127. dev_id_val = FIELD_GET(DEVICE_REV_ID_DEV_ID_MASK, dev_rev_id_val);
  128. switch (dev_id_val) {
  129. case DEVICE_REV_ID_DEV_ID_X4:
  130. *rev_id = AL_PCIE_REV_ID_2;
  131. break;
  132. case DEVICE_REV_ID_DEV_ID_X8:
  133. *rev_id = AL_PCIE_REV_ID_3;
  134. break;
  135. case DEVICE_REV_ID_DEV_ID_X16:
  136. *rev_id = AL_PCIE_REV_ID_4;
  137. break;
  138. default:
  139. dev_err(pcie->dev, "Unsupported dev_id_val (0x%x)\n",
  140. dev_id_val);
  141. return -EINVAL;
  142. }
  143. dev_dbg(pcie->dev, "dev_id_val: 0x%x\n", dev_id_val);
  144. return 0;
  145. }
  146. static int al_pcie_reg_offsets_set(struct al_pcie *pcie)
  147. {
  148. switch (pcie->controller_rev_id) {
  149. case AL_PCIE_REV_ID_2:
  150. pcie->reg_offsets.ob_ctrl = OB_CTRL_REV1_2_OFFSET;
  151. break;
  152. case AL_PCIE_REV_ID_3:
  153. case AL_PCIE_REV_ID_4:
  154. pcie->reg_offsets.ob_ctrl = OB_CTRL_REV3_5_OFFSET;
  155. break;
  156. default:
  157. dev_err(pcie->dev, "Unsupported controller rev_id: 0x%x\n",
  158. pcie->controller_rev_id);
  159. return -EINVAL;
  160. }
  161. return 0;
  162. }
  163. static inline void al_pcie_target_bus_set(struct al_pcie *pcie,
  164. u8 target_bus,
  165. u8 mask_target_bus)
  166. {
  167. u32 reg;
  168. reg = FIELD_PREP(CFG_TARGET_BUS_MASK_MASK, mask_target_bus) |
  169. FIELD_PREP(CFG_TARGET_BUS_BUSNUM_MASK, target_bus);
  170. al_pcie_controller_writel(pcie, AXI_BASE_OFFSET +
  171. pcie->reg_offsets.ob_ctrl + CFG_TARGET_BUS,
  172. reg);
  173. }
  174. static void __iomem *al_pcie_conf_addr_map_bus(struct pci_bus *bus,
  175. unsigned int devfn, int where)
  176. {
  177. struct dw_pcie_rp *pp = bus->sysdata;
  178. struct al_pcie *pcie = to_al_pcie(to_dw_pcie_from_pp(pp));
  179. unsigned int busnr = bus->number;
  180. struct al_pcie_target_bus_cfg *target_bus_cfg = &pcie->target_bus_cfg;
  181. unsigned int busnr_ecam = busnr & target_bus_cfg->ecam_mask;
  182. unsigned int busnr_reg = busnr & target_bus_cfg->reg_mask;
  183. if (busnr_reg != target_bus_cfg->reg_val) {
  184. dev_dbg(pcie->pci->dev, "Changing target bus busnum val from 0x%x to 0x%x\n",
  185. target_bus_cfg->reg_val, busnr_reg);
  186. target_bus_cfg->reg_val = busnr_reg;
  187. al_pcie_target_bus_set(pcie,
  188. target_bus_cfg->reg_val,
  189. target_bus_cfg->reg_mask);
  190. }
  191. return pp->va_cfg0_base + PCIE_ECAM_OFFSET(busnr_ecam, devfn, where);
  192. }
  193. static struct pci_ops al_child_pci_ops = {
  194. .map_bus = al_pcie_conf_addr_map_bus,
  195. .read = pci_generic_config_read,
  196. .write = pci_generic_config_write,
  197. };
  198. static void al_pcie_config_prepare(struct al_pcie *pcie)
  199. {
  200. struct al_pcie_target_bus_cfg *target_bus_cfg;
  201. struct dw_pcie_rp *pp = &pcie->pci->pp;
  202. unsigned int ecam_bus_mask;
  203. u32 cfg_control_offset;
  204. u8 subordinate_bus;
  205. u8 secondary_bus;
  206. u32 cfg_control;
  207. u32 reg;
  208. struct resource *bus = resource_list_first_type(&pp->bridge->windows, IORESOURCE_BUS)->res;
  209. target_bus_cfg = &pcie->target_bus_cfg;
  210. ecam_bus_mask = (pcie->ecam_size >> PCIE_ECAM_BUS_SHIFT) - 1;
  211. if (ecam_bus_mask > 255) {
  212. dev_warn(pcie->dev, "ECAM window size is larger than 256MB. Cutting off at 256\n");
  213. ecam_bus_mask = 255;
  214. }
  215. /* This portion is taken from the transaction address */
  216. target_bus_cfg->ecam_mask = ecam_bus_mask;
  217. /* This portion is taken from the cfg_target_bus reg */
  218. target_bus_cfg->reg_mask = ~target_bus_cfg->ecam_mask;
  219. target_bus_cfg->reg_val = bus->start & target_bus_cfg->reg_mask;
  220. al_pcie_target_bus_set(pcie, target_bus_cfg->reg_val,
  221. target_bus_cfg->reg_mask);
  222. secondary_bus = bus->start + 1;
  223. subordinate_bus = bus->end;
  224. /* Set the valid values of secondary and subordinate buses */
  225. cfg_control_offset = AXI_BASE_OFFSET + pcie->reg_offsets.ob_ctrl +
  226. CFG_CONTROL;
  227. cfg_control = al_pcie_controller_readl(pcie, cfg_control_offset);
  228. reg = cfg_control &
  229. ~(CFG_CONTROL_SEC_BUS_MASK | CFG_CONTROL_SUBBUS_MASK);
  230. reg |= FIELD_PREP(CFG_CONTROL_SUBBUS_MASK, subordinate_bus) |
  231. FIELD_PREP(CFG_CONTROL_SEC_BUS_MASK, secondary_bus);
  232. al_pcie_controller_writel(pcie, cfg_control_offset, reg);
  233. }
  234. static int al_pcie_host_init(struct dw_pcie_rp *pp)
  235. {
  236. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  237. struct al_pcie *pcie = to_al_pcie(pci);
  238. int rc;
  239. pp->bridge->child_ops = &al_child_pci_ops;
  240. rc = al_pcie_rev_id_get(pcie, &pcie->controller_rev_id);
  241. if (rc)
  242. return rc;
  243. rc = al_pcie_reg_offsets_set(pcie);
  244. if (rc)
  245. return rc;
  246. al_pcie_config_prepare(pcie);
  247. return 0;
  248. }
  249. static const struct dw_pcie_host_ops al_pcie_host_ops = {
  250. .host_init = al_pcie_host_init,
  251. };
  252. static int al_pcie_probe(struct platform_device *pdev)
  253. {
  254. struct device *dev = &pdev->dev;
  255. struct resource *controller_res;
  256. struct resource *ecam_res;
  257. struct al_pcie *al_pcie;
  258. struct dw_pcie *pci;
  259. al_pcie = devm_kzalloc(dev, sizeof(*al_pcie), GFP_KERNEL);
  260. if (!al_pcie)
  261. return -ENOMEM;
  262. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  263. if (!pci)
  264. return -ENOMEM;
  265. pci->dev = dev;
  266. pci->pp.ops = &al_pcie_host_ops;
  267. al_pcie->pci = pci;
  268. al_pcie->dev = dev;
  269. ecam_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
  270. if (!ecam_res) {
  271. dev_err(dev, "couldn't find 'config' reg in DT\n");
  272. return -ENOENT;
  273. }
  274. al_pcie->ecam_size = resource_size(ecam_res);
  275. controller_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  276. "controller");
  277. al_pcie->controller_base = devm_ioremap_resource(dev, controller_res);
  278. if (IS_ERR(al_pcie->controller_base)) {
  279. dev_err(dev, "couldn't remap controller base %pR\n",
  280. controller_res);
  281. return PTR_ERR(al_pcie->controller_base);
  282. }
  283. dev_dbg(dev, "From DT: controller_base: %pR\n", controller_res);
  284. platform_set_drvdata(pdev, al_pcie);
  285. return dw_pcie_host_init(&pci->pp);
  286. }
  287. static const struct of_device_id al_pcie_of_match[] = {
  288. { .compatible = "amazon,al-alpine-v2-pcie",
  289. },
  290. { .compatible = "amazon,al-alpine-v3-pcie",
  291. },
  292. {},
  293. };
  294. static struct platform_driver al_pcie_driver = {
  295. .driver = {
  296. .name = "al-pcie",
  297. .of_match_table = al_pcie_of_match,
  298. .suppress_bind_attrs = true,
  299. },
  300. .probe = al_pcie_probe,
  301. };
  302. builtin_platform_driver(al_pcie_driver);
  303. #endif /* CONFIG_PCIE_AL*/