pcie-al.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <jonnyc@amazon.com>
  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 int 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. struct resource_entry *ft;
  204. u32 cfg_control_offset;
  205. struct resource *bus;
  206. u8 subordinate_bus;
  207. u8 secondary_bus;
  208. u32 cfg_control;
  209. u32 reg;
  210. ft = resource_list_first_type(&pp->bridge->windows, IORESOURCE_BUS);
  211. if (!ft)
  212. return -ENODEV;
  213. bus = ft->res;
  214. target_bus_cfg = &pcie->target_bus_cfg;
  215. ecam_bus_mask = (pcie->ecam_size >> PCIE_ECAM_BUS_SHIFT) - 1;
  216. if (ecam_bus_mask > 255) {
  217. dev_warn(pcie->dev, "ECAM window size is larger than 256MB. Cutting off at 256\n");
  218. ecam_bus_mask = 255;
  219. }
  220. /* This portion is taken from the transaction address */
  221. target_bus_cfg->ecam_mask = ecam_bus_mask;
  222. /* This portion is taken from the cfg_target_bus reg */
  223. target_bus_cfg->reg_mask = ~target_bus_cfg->ecam_mask;
  224. target_bus_cfg->reg_val = bus->start & target_bus_cfg->reg_mask;
  225. al_pcie_target_bus_set(pcie, target_bus_cfg->reg_val,
  226. target_bus_cfg->reg_mask);
  227. secondary_bus = bus->start + 1;
  228. subordinate_bus = bus->end;
  229. /* Set the valid values of secondary and subordinate buses */
  230. cfg_control_offset = AXI_BASE_OFFSET + pcie->reg_offsets.ob_ctrl +
  231. CFG_CONTROL;
  232. cfg_control = al_pcie_controller_readl(pcie, cfg_control_offset);
  233. reg = cfg_control &
  234. ~(CFG_CONTROL_SEC_BUS_MASK | CFG_CONTROL_SUBBUS_MASK);
  235. reg |= FIELD_PREP(CFG_CONTROL_SUBBUS_MASK, subordinate_bus) |
  236. FIELD_PREP(CFG_CONTROL_SEC_BUS_MASK, secondary_bus);
  237. al_pcie_controller_writel(pcie, cfg_control_offset, reg);
  238. return 0;
  239. }
  240. static int al_pcie_host_init(struct dw_pcie_rp *pp)
  241. {
  242. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  243. struct al_pcie *pcie = to_al_pcie(pci);
  244. int rc;
  245. pp->bridge->child_ops = &al_child_pci_ops;
  246. rc = al_pcie_rev_id_get(pcie, &pcie->controller_rev_id);
  247. if (rc)
  248. return rc;
  249. rc = al_pcie_reg_offsets_set(pcie);
  250. if (rc)
  251. return rc;
  252. rc = al_pcie_config_prepare(pcie);
  253. if (rc)
  254. return rc;
  255. return 0;
  256. }
  257. static const struct dw_pcie_host_ops al_pcie_host_ops = {
  258. .init = al_pcie_host_init,
  259. };
  260. static int al_pcie_probe(struct platform_device *pdev)
  261. {
  262. struct device *dev = &pdev->dev;
  263. struct resource *controller_res;
  264. struct resource *ecam_res;
  265. struct al_pcie *al_pcie;
  266. struct dw_pcie *pci;
  267. al_pcie = devm_kzalloc(dev, sizeof(*al_pcie), GFP_KERNEL);
  268. if (!al_pcie)
  269. return -ENOMEM;
  270. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  271. if (!pci)
  272. return -ENOMEM;
  273. pci->dev = dev;
  274. pci->pp.ops = &al_pcie_host_ops;
  275. al_pcie->pci = pci;
  276. al_pcie->dev = dev;
  277. ecam_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
  278. if (!ecam_res) {
  279. dev_err(dev, "couldn't find 'config' reg in DT\n");
  280. return -ENOENT;
  281. }
  282. al_pcie->ecam_size = resource_size(ecam_res);
  283. pci->pp.native_ecam = true;
  284. controller_res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  285. "controller");
  286. al_pcie->controller_base = devm_ioremap_resource(dev, controller_res);
  287. if (IS_ERR(al_pcie->controller_base)) {
  288. dev_err(dev, "couldn't remap controller base %pR\n",
  289. controller_res);
  290. return PTR_ERR(al_pcie->controller_base);
  291. }
  292. dev_dbg(dev, "From DT: controller_base: %pR\n", controller_res);
  293. platform_set_drvdata(pdev, al_pcie);
  294. return dw_pcie_host_init(&pci->pp);
  295. }
  296. static const struct of_device_id al_pcie_of_match[] = {
  297. { .compatible = "amazon,al-alpine-v2-pcie",
  298. },
  299. { .compatible = "amazon,al-alpine-v3-pcie",
  300. },
  301. {},
  302. };
  303. static struct platform_driver al_pcie_driver = {
  304. .driver = {
  305. .name = "al-pcie",
  306. .of_match_table = al_pcie_of_match,
  307. .suppress_bind_attrs = true,
  308. },
  309. .probe = al_pcie_probe,
  310. };
  311. builtin_platform_driver(al_pcie_driver);
  312. #endif /* CONFIG_PCIE_AL*/