pci-keystone.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Texas Instruments Keystone SoCs
  4. *
  5. * Copyright (C) 2013-2014 Texas Instruments., Ltd.
  6. * http://www.ti.com
  7. *
  8. * Author: Murali Karicheri <m-karicheri2@ti.com>
  9. * Implementation based on pci-exynos.c and pcie-designware.c
  10. */
  11. #include <linux/irqchip/chained_irq.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/init.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/msi.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of.h>
  21. #include <linux/of_pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/phy/phy.h>
  24. #include <linux/regmap.h>
  25. #include <linux/resource.h>
  26. #include <linux/signal.h>
  27. #include "pcie-designware.h"
  28. #define DRIVER_NAME "keystone-pcie"
  29. #define PCIE_VENDORID_MASK 0xffff
  30. #define PCIE_DEVICEID_SHIFT 16
  31. /* DEV_STAT_CTRL */
  32. #define PCIE_CAP_BASE 0x70
  33. /* Application register defines */
  34. #define LTSSM_EN_VAL BIT(0)
  35. #define LTSSM_STATE_MASK 0x1f
  36. #define LTSSM_STATE_L0 0x11
  37. #define DBI_CS2_EN_VAL 0x20
  38. #define OB_XLAT_EN_VAL BIT(1)
  39. /* Application registers */
  40. #define CMD_STATUS 0x004
  41. #define CFG_SETUP 0x008
  42. #define CFG_BUS(x) (((x) & 0xff) << 16)
  43. #define CFG_DEVICE(x) (((x) & 0x1f) << 8)
  44. #define CFG_FUNC(x) ((x) & 0x7)
  45. #define CFG_TYPE1 BIT(24)
  46. #define OB_SIZE 0x030
  47. #define SPACE0_REMOTE_CFG_OFFSET 0x1000
  48. #define OB_OFFSET_INDEX(n) (0x200 + (8 * (n)))
  49. #define OB_OFFSET_HI(n) (0x204 + (8 * (n)))
  50. #define OB_ENABLEN BIT(0)
  51. #define OB_WIN_SIZE 8 /* 8MB */
  52. /* IRQ register defines */
  53. #define IRQ_EOI 0x050
  54. #define IRQ_STATUS 0x184
  55. #define IRQ_ENABLE_SET 0x188
  56. #define IRQ_ENABLE_CLR 0x18c
  57. #define MSI_IRQ 0x054
  58. #define MSI0_IRQ_STATUS 0x104
  59. #define MSI0_IRQ_ENABLE_SET 0x108
  60. #define MSI0_IRQ_ENABLE_CLR 0x10c
  61. #define IRQ_STATUS 0x184
  62. #define MSI_IRQ_OFFSET 4
  63. /* Error IRQ bits */
  64. #define ERR_AER BIT(5) /* ECRC error */
  65. #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
  66. #define ERR_CORR BIT(3) /* Correctable error */
  67. #define ERR_NONFATAL BIT(2) /* Non-fatal error */
  68. #define ERR_FATAL BIT(1) /* Fatal error */
  69. #define ERR_SYS BIT(0) /* System (fatal, non-fatal, or correctable) */
  70. #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
  71. ERR_NONFATAL | ERR_FATAL | ERR_SYS)
  72. #define ERR_FATAL_IRQ (ERR_FATAL | ERR_AXI)
  73. #define ERR_IRQ_STATUS_RAW 0x1c0
  74. #define ERR_IRQ_STATUS 0x1c4
  75. #define ERR_IRQ_ENABLE_SET 0x1c8
  76. #define ERR_IRQ_ENABLE_CLR 0x1cc
  77. /* Config space registers */
  78. #define DEBUG0 0x728
  79. #define MAX_MSI_HOST_IRQS 8
  80. /* PCIE controller device IDs */
  81. #define PCIE_RC_K2HK 0xb008
  82. #define PCIE_RC_K2E 0xb009
  83. #define PCIE_RC_K2L 0xb00a
  84. #define PCIE_RC_K2G 0xb00b
  85. #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
  86. struct keystone_pcie {
  87. struct dw_pcie *pci;
  88. /* PCI Device ID */
  89. u32 device_id;
  90. int num_legacy_host_irqs;
  91. int legacy_host_irqs[PCI_NUM_INTX];
  92. struct device_node *legacy_intc_np;
  93. int num_msi_host_irqs;
  94. int msi_host_irqs[MAX_MSI_HOST_IRQS];
  95. int num_lanes;
  96. u32 num_viewport;
  97. struct phy **phy;
  98. struct device_link **link;
  99. struct device_node *msi_intc_np;
  100. struct irq_domain *legacy_irq_domain;
  101. struct device_node *np;
  102. int error_irq;
  103. /* Application register space */
  104. void __iomem *va_app_base; /* DT 1st resource */
  105. struct resource app;
  106. };
  107. static inline void update_reg_offset_bit_pos(u32 offset, u32 *reg_offset,
  108. u32 *bit_pos)
  109. {
  110. *reg_offset = offset % 8;
  111. *bit_pos = offset >> 3;
  112. }
  113. static phys_addr_t ks_pcie_get_msi_addr(struct pcie_port *pp)
  114. {
  115. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  116. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  117. return ks_pcie->app.start + MSI_IRQ;
  118. }
  119. static u32 ks_pcie_app_readl(struct keystone_pcie *ks_pcie, u32 offset)
  120. {
  121. return readl(ks_pcie->va_app_base + offset);
  122. }
  123. static void ks_pcie_app_writel(struct keystone_pcie *ks_pcie, u32 offset,
  124. u32 val)
  125. {
  126. writel(val, ks_pcie->va_app_base + offset);
  127. }
  128. static void ks_pcie_handle_msi_irq(struct keystone_pcie *ks_pcie, int offset)
  129. {
  130. struct dw_pcie *pci = ks_pcie->pci;
  131. struct pcie_port *pp = &pci->pp;
  132. struct device *dev = pci->dev;
  133. u32 pending, vector;
  134. int src, virq;
  135. pending = ks_pcie_app_readl(ks_pcie, MSI0_IRQ_STATUS + (offset << 4));
  136. /*
  137. * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
  138. * shows 1, 9, 17, 25 and so forth
  139. */
  140. for (src = 0; src < 4; src++) {
  141. if (BIT(src) & pending) {
  142. vector = offset + (src << 3);
  143. virq = irq_linear_revmap(pp->irq_domain, vector);
  144. dev_dbg(dev, "irq: bit %d, vector %d, virq %d\n",
  145. src, vector, virq);
  146. generic_handle_irq(virq);
  147. }
  148. }
  149. }
  150. static void ks_pcie_msi_irq_ack(int irq, struct pcie_port *pp)
  151. {
  152. u32 reg_offset, bit_pos;
  153. struct keystone_pcie *ks_pcie;
  154. struct dw_pcie *pci;
  155. pci = to_dw_pcie_from_pp(pp);
  156. ks_pcie = to_keystone_pcie(pci);
  157. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  158. ks_pcie_app_writel(ks_pcie, MSI0_IRQ_STATUS + (reg_offset << 4),
  159. BIT(bit_pos));
  160. ks_pcie_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET);
  161. }
  162. static void ks_pcie_msi_set_irq(struct pcie_port *pp, int irq)
  163. {
  164. u32 reg_offset, bit_pos;
  165. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  166. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  167. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  168. ks_pcie_app_writel(ks_pcie, MSI0_IRQ_ENABLE_SET + (reg_offset << 4),
  169. BIT(bit_pos));
  170. }
  171. static void ks_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
  172. {
  173. u32 reg_offset, bit_pos;
  174. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  175. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  176. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  177. ks_pcie_app_writel(ks_pcie, MSI0_IRQ_ENABLE_CLR + (reg_offset << 4),
  178. BIT(bit_pos));
  179. }
  180. static int ks_pcie_msi_host_init(struct pcie_port *pp)
  181. {
  182. return dw_pcie_allocate_domains(pp);
  183. }
  184. static void ks_pcie_enable_legacy_irqs(struct keystone_pcie *ks_pcie)
  185. {
  186. int i;
  187. for (i = 0; i < PCI_NUM_INTX; i++)
  188. ks_pcie_app_writel(ks_pcie, IRQ_ENABLE_SET + (i << 4), 0x1);
  189. }
  190. static void ks_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie,
  191. int offset)
  192. {
  193. struct dw_pcie *pci = ks_pcie->pci;
  194. struct device *dev = pci->dev;
  195. u32 pending;
  196. int virq;
  197. pending = ks_pcie_app_readl(ks_pcie, IRQ_STATUS + (offset << 4));
  198. if (BIT(0) & pending) {
  199. virq = irq_linear_revmap(ks_pcie->legacy_irq_domain, offset);
  200. dev_dbg(dev, ": irq: irq_offset %d, virq %d\n", offset, virq);
  201. generic_handle_irq(virq);
  202. }
  203. /* EOI the INTx interrupt */
  204. ks_pcie_app_writel(ks_pcie, IRQ_EOI, offset);
  205. }
  206. static void ks_pcie_enable_error_irq(struct keystone_pcie *ks_pcie)
  207. {
  208. ks_pcie_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL);
  209. }
  210. static irqreturn_t ks_pcie_handle_error_irq(struct keystone_pcie *ks_pcie)
  211. {
  212. u32 status;
  213. status = ks_pcie_app_readl(ks_pcie, ERR_IRQ_STATUS_RAW) & ERR_IRQ_ALL;
  214. if (!status)
  215. return IRQ_NONE;
  216. if (status & ERR_FATAL_IRQ)
  217. dev_err(ks_pcie->pci->dev, "fatal error (status %#010x)\n",
  218. status);
  219. /* Ack the IRQ; status bits are RW1C */
  220. ks_pcie_app_writel(ks_pcie, ERR_IRQ_STATUS, status);
  221. return IRQ_HANDLED;
  222. }
  223. static void ks_pcie_ack_legacy_irq(struct irq_data *d)
  224. {
  225. }
  226. static void ks_pcie_mask_legacy_irq(struct irq_data *d)
  227. {
  228. }
  229. static void ks_pcie_unmask_legacy_irq(struct irq_data *d)
  230. {
  231. }
  232. static struct irq_chip ks_pcie_legacy_irq_chip = {
  233. .name = "Keystone-PCI-Legacy-IRQ",
  234. .irq_ack = ks_pcie_ack_legacy_irq,
  235. .irq_mask = ks_pcie_mask_legacy_irq,
  236. .irq_unmask = ks_pcie_unmask_legacy_irq,
  237. };
  238. static int ks_pcie_init_legacy_irq_map(struct irq_domain *d,
  239. unsigned int irq,
  240. irq_hw_number_t hw_irq)
  241. {
  242. irq_set_chip_and_handler(irq, &ks_pcie_legacy_irq_chip,
  243. handle_level_irq);
  244. irq_set_chip_data(irq, d->host_data);
  245. return 0;
  246. }
  247. static const struct irq_domain_ops ks_pcie_legacy_irq_domain_ops = {
  248. .map = ks_pcie_init_legacy_irq_map,
  249. .xlate = irq_domain_xlate_onetwocell,
  250. };
  251. /**
  252. * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
  253. * registers
  254. *
  255. * Since modification of dbi_cs2 involves different clock domain, read the
  256. * status back to ensure the transition is complete.
  257. */
  258. static void ks_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie)
  259. {
  260. u32 val;
  261. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  262. ks_pcie_app_writel(ks_pcie, CMD_STATUS, DBI_CS2_EN_VAL | val);
  263. do {
  264. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  265. } while (!(val & DBI_CS2_EN_VAL));
  266. }
  267. /**
  268. * ks_pcie_clear_dbi_mode() - Disable DBI mode
  269. *
  270. * Since modification of dbi_cs2 involves different clock domain, read the
  271. * status back to ensure the transition is complete.
  272. */
  273. static void ks_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie)
  274. {
  275. u32 val;
  276. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  277. ks_pcie_app_writel(ks_pcie, CMD_STATUS, ~DBI_CS2_EN_VAL & val);
  278. do {
  279. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  280. } while (val & DBI_CS2_EN_VAL);
  281. }
  282. static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
  283. {
  284. u32 val;
  285. u32 num_viewport = ks_pcie->num_viewport;
  286. struct dw_pcie *pci = ks_pcie->pci;
  287. struct pcie_port *pp = &pci->pp;
  288. u64 start = pp->mem->start;
  289. u64 end = pp->mem->end;
  290. int i;
  291. /* Disable BARs for inbound access */
  292. ks_pcie_set_dbi_mode(ks_pcie);
  293. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
  294. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0);
  295. ks_pcie_clear_dbi_mode(ks_pcie);
  296. val = ilog2(OB_WIN_SIZE);
  297. ks_pcie_app_writel(ks_pcie, OB_SIZE, val);
  298. /* Using Direct 1:1 mapping of RC <-> PCI memory space */
  299. for (i = 0; i < num_viewport && (start < end); i++) {
  300. ks_pcie_app_writel(ks_pcie, OB_OFFSET_INDEX(i),
  301. lower_32_bits(start) | OB_ENABLEN);
  302. ks_pcie_app_writel(ks_pcie, OB_OFFSET_HI(i),
  303. upper_32_bits(start));
  304. start += OB_WIN_SIZE;
  305. }
  306. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  307. val |= OB_XLAT_EN_VAL;
  308. ks_pcie_app_writel(ks_pcie, CMD_STATUS, val);
  309. }
  310. static int ks_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  311. unsigned int devfn, int where, int size,
  312. u32 *val)
  313. {
  314. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  315. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  316. u32 reg;
  317. reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
  318. CFG_FUNC(PCI_FUNC(devfn));
  319. if (bus->parent->number != pp->root_bus_nr)
  320. reg |= CFG_TYPE1;
  321. ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
  322. return dw_pcie_read(pp->va_cfg0_base + where, size, val);
  323. }
  324. static int ks_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  325. unsigned int devfn, int where, int size,
  326. u32 val)
  327. {
  328. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  329. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  330. u32 reg;
  331. reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
  332. CFG_FUNC(PCI_FUNC(devfn));
  333. if (bus->parent->number != pp->root_bus_nr)
  334. reg |= CFG_TYPE1;
  335. ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
  336. return dw_pcie_write(pp->va_cfg0_base + where, size, val);
  337. }
  338. /**
  339. * ks_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
  340. *
  341. * This sets BAR0 to enable inbound access for MSI_IRQ register
  342. */
  343. static void ks_pcie_v3_65_scan_bus(struct pcie_port *pp)
  344. {
  345. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  346. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  347. /* Configure and set up BAR0 */
  348. ks_pcie_set_dbi_mode(ks_pcie);
  349. /* Enable BAR0 */
  350. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1);
  351. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1);
  352. ks_pcie_clear_dbi_mode(ks_pcie);
  353. /*
  354. * For BAR0, just setting bus address for inbound writes (MSI) should
  355. * be sufficient. Use physical address to avoid any conflicts.
  356. */
  357. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start);
  358. }
  359. /**
  360. * ks_pcie_link_up() - Check if link up
  361. */
  362. static int ks_pcie_link_up(struct dw_pcie *pci)
  363. {
  364. u32 val;
  365. val = dw_pcie_readl_dbi(pci, DEBUG0);
  366. return (val & LTSSM_STATE_MASK) == LTSSM_STATE_L0;
  367. }
  368. static void ks_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
  369. {
  370. u32 val;
  371. /* Disable Link training */
  372. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  373. val &= ~LTSSM_EN_VAL;
  374. ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
  375. /* Initiate Link Training */
  376. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  377. ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
  378. }
  379. /**
  380. * ks_pcie_dw_host_init() - initialize host for v3_65 dw hardware
  381. *
  382. * Ioremap the register resources, initialize legacy irq domain
  383. * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
  384. * PCI host controller.
  385. */
  386. static int __init ks_pcie_dw_host_init(struct keystone_pcie *ks_pcie)
  387. {
  388. struct dw_pcie *pci = ks_pcie->pci;
  389. struct pcie_port *pp = &pci->pp;
  390. struct device *dev = pci->dev;
  391. struct platform_device *pdev = to_platform_device(dev);
  392. struct resource *res;
  393. /* Index 0 is the config reg. space address */
  394. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  395. pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
  396. if (IS_ERR(pci->dbi_base))
  397. return PTR_ERR(pci->dbi_base);
  398. /*
  399. * We set these same and is used in pcie rd/wr_other_conf
  400. * functions
  401. */
  402. pp->va_cfg0_base = pci->dbi_base + SPACE0_REMOTE_CFG_OFFSET;
  403. pp->va_cfg1_base = pp->va_cfg0_base;
  404. /* Index 1 is the application reg. space address */
  405. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  406. ks_pcie->va_app_base = devm_ioremap_resource(dev, res);
  407. if (IS_ERR(ks_pcie->va_app_base))
  408. return PTR_ERR(ks_pcie->va_app_base);
  409. ks_pcie->app = *res;
  410. /* Create legacy IRQ domain */
  411. ks_pcie->legacy_irq_domain =
  412. irq_domain_add_linear(ks_pcie->legacy_intc_np,
  413. PCI_NUM_INTX,
  414. &ks_pcie_legacy_irq_domain_ops,
  415. NULL);
  416. if (!ks_pcie->legacy_irq_domain) {
  417. dev_err(dev, "Failed to add irq domain for legacy irqs\n");
  418. return -EINVAL;
  419. }
  420. return dw_pcie_host_init(pp);
  421. }
  422. static void ks_pcie_quirk(struct pci_dev *dev)
  423. {
  424. struct pci_bus *bus = dev->bus;
  425. struct pci_dev *bridge;
  426. static const struct pci_device_id rc_pci_devids[] = {
  427. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
  428. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  429. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
  430. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  431. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
  432. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  433. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G),
  434. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  435. { 0, },
  436. };
  437. if (pci_is_root_bus(bus))
  438. bridge = dev;
  439. /* look for the host bridge */
  440. while (!pci_is_root_bus(bus)) {
  441. bridge = bus->self;
  442. bus = bus->parent;
  443. }
  444. if (!bridge)
  445. return;
  446. /*
  447. * Keystone PCI controller has a h/w limitation of
  448. * 256 bytes maximum read request size. It can't handle
  449. * anything higher than this. So force this limit on
  450. * all downstream devices.
  451. */
  452. if (pci_match_id(rc_pci_devids, bridge)) {
  453. if (pcie_get_readrq(dev) > 256) {
  454. dev_info(&dev->dev, "limiting MRRS to 256\n");
  455. pcie_set_readrq(dev, 256);
  456. }
  457. }
  458. }
  459. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, ks_pcie_quirk);
  460. static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
  461. {
  462. struct dw_pcie *pci = ks_pcie->pci;
  463. struct device *dev = pci->dev;
  464. if (dw_pcie_link_up(pci)) {
  465. dev_info(dev, "Link already up\n");
  466. return 0;
  467. }
  468. ks_pcie_initiate_link_train(ks_pcie);
  469. /* check if the link is up or not */
  470. if (!dw_pcie_wait_for_link(pci))
  471. return 0;
  472. dev_err(dev, "phy link never came up\n");
  473. return -ETIMEDOUT;
  474. }
  475. static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
  476. {
  477. unsigned int irq = irq_desc_get_irq(desc);
  478. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  479. u32 offset = irq - ks_pcie->msi_host_irqs[0];
  480. struct dw_pcie *pci = ks_pcie->pci;
  481. struct device *dev = pci->dev;
  482. struct irq_chip *chip = irq_desc_get_chip(desc);
  483. dev_dbg(dev, "%s, irq %d\n", __func__, irq);
  484. /*
  485. * The chained irq handler installation would have replaced normal
  486. * interrupt driver handler so we need to take care of mask/unmask and
  487. * ack operation.
  488. */
  489. chained_irq_enter(chip, desc);
  490. ks_pcie_handle_msi_irq(ks_pcie, offset);
  491. chained_irq_exit(chip, desc);
  492. }
  493. /**
  494. * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
  495. * @irq: IRQ line for legacy interrupts
  496. * @desc: Pointer to irq descriptor
  497. *
  498. * Traverse through pending legacy interrupts and invoke handler for each. Also
  499. * takes care of interrupt controller level mask/ack operation.
  500. */
  501. static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
  502. {
  503. unsigned int irq = irq_desc_get_irq(desc);
  504. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  505. struct dw_pcie *pci = ks_pcie->pci;
  506. struct device *dev = pci->dev;
  507. u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
  508. struct irq_chip *chip = irq_desc_get_chip(desc);
  509. dev_dbg(dev, ": Handling legacy irq %d\n", irq);
  510. /*
  511. * The chained irq handler installation would have replaced normal
  512. * interrupt driver handler so we need to take care of mask/unmask and
  513. * ack operation.
  514. */
  515. chained_irq_enter(chip, desc);
  516. ks_pcie_handle_legacy_irq(ks_pcie, irq_offset);
  517. chained_irq_exit(chip, desc);
  518. }
  519. static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
  520. char *controller, int *num_irqs)
  521. {
  522. int temp, max_host_irqs, legacy = 1, *host_irqs;
  523. struct device *dev = ks_pcie->pci->dev;
  524. struct device_node *np_pcie = dev->of_node, **np_temp;
  525. if (!strcmp(controller, "msi-interrupt-controller"))
  526. legacy = 0;
  527. if (legacy) {
  528. np_temp = &ks_pcie->legacy_intc_np;
  529. max_host_irqs = PCI_NUM_INTX;
  530. host_irqs = &ks_pcie->legacy_host_irqs[0];
  531. } else {
  532. np_temp = &ks_pcie->msi_intc_np;
  533. max_host_irqs = MAX_MSI_HOST_IRQS;
  534. host_irqs = &ks_pcie->msi_host_irqs[0];
  535. }
  536. /* interrupt controller is in a child node */
  537. *np_temp = of_get_child_by_name(np_pcie, controller);
  538. if (!(*np_temp)) {
  539. dev_err(dev, "Node for %s is absent\n", controller);
  540. return -EINVAL;
  541. }
  542. temp = of_irq_count(*np_temp);
  543. if (!temp) {
  544. dev_err(dev, "No IRQ entries in %s\n", controller);
  545. of_node_put(*np_temp);
  546. return -EINVAL;
  547. }
  548. if (temp > max_host_irqs)
  549. dev_warn(dev, "Too many %s interrupts defined %u\n",
  550. (legacy ? "legacy" : "MSI"), temp);
  551. /*
  552. * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
  553. * 7 (MSI)
  554. */
  555. for (temp = 0; temp < max_host_irqs; temp++) {
  556. host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
  557. if (!host_irqs[temp])
  558. break;
  559. }
  560. of_node_put(*np_temp);
  561. if (temp) {
  562. *num_irqs = temp;
  563. return 0;
  564. }
  565. return -EINVAL;
  566. }
  567. static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
  568. {
  569. int i;
  570. /* Legacy IRQ */
  571. for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
  572. irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
  573. ks_pcie_legacy_irq_handler,
  574. ks_pcie);
  575. }
  576. ks_pcie_enable_legacy_irqs(ks_pcie);
  577. /* MSI IRQ */
  578. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  579. for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
  580. irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
  581. ks_pcie_msi_irq_handler,
  582. ks_pcie);
  583. }
  584. }
  585. if (ks_pcie->error_irq > 0)
  586. ks_pcie_enable_error_irq(ks_pcie);
  587. }
  588. /*
  589. * When a PCI device does not exist during config cycles, keystone host gets a
  590. * bus error instead of returning 0xffffffff. This handler always returns 0
  591. * for this kind of faults.
  592. */
  593. static int ks_pcie_fault(unsigned long addr, unsigned int fsr,
  594. struct pt_regs *regs)
  595. {
  596. unsigned long instr = *(unsigned long *) instruction_pointer(regs);
  597. if ((instr & 0x0e100090) == 0x00100090) {
  598. int reg = (instr >> 12) & 15;
  599. regs->uregs[reg] = -1;
  600. regs->ARM_pc += 4;
  601. }
  602. return 0;
  603. }
  604. static int __init ks_pcie_init_id(struct keystone_pcie *ks_pcie)
  605. {
  606. int ret;
  607. unsigned int id;
  608. struct regmap *devctrl_regs;
  609. struct dw_pcie *pci = ks_pcie->pci;
  610. struct device *dev = pci->dev;
  611. struct device_node *np = dev->of_node;
  612. devctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-id");
  613. if (IS_ERR(devctrl_regs))
  614. return PTR_ERR(devctrl_regs);
  615. ret = regmap_read(devctrl_regs, 0, &id);
  616. if (ret)
  617. return ret;
  618. dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, id & PCIE_VENDORID_MASK);
  619. dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, id >> PCIE_DEVICEID_SHIFT);
  620. return 0;
  621. }
  622. static int __init ks_pcie_host_init(struct pcie_port *pp)
  623. {
  624. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  625. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  626. int ret;
  627. dw_pcie_setup_rc(pp);
  628. ks_pcie_establish_link(ks_pcie);
  629. ks_pcie_setup_rc_app_regs(ks_pcie);
  630. ks_pcie_setup_interrupts(ks_pcie);
  631. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  632. pci->dbi_base + PCI_IO_BASE);
  633. ret = ks_pcie_init_id(ks_pcie);
  634. if (ret < 0)
  635. return ret;
  636. /*
  637. * PCIe access errors that result into OCP errors are caught by ARM as
  638. * "External aborts"
  639. */
  640. hook_fault_code(17, ks_pcie_fault, SIGBUS, 0,
  641. "Asynchronous external abort");
  642. return 0;
  643. }
  644. static const struct dw_pcie_host_ops ks_pcie_host_ops = {
  645. .rd_other_conf = ks_pcie_rd_other_conf,
  646. .wr_other_conf = ks_pcie_wr_other_conf,
  647. .host_init = ks_pcie_host_init,
  648. .msi_set_irq = ks_pcie_msi_set_irq,
  649. .msi_clear_irq = ks_pcie_msi_clear_irq,
  650. .get_msi_addr = ks_pcie_get_msi_addr,
  651. .msi_host_init = ks_pcie_msi_host_init,
  652. .msi_irq_ack = ks_pcie_msi_irq_ack,
  653. .scan_bus = ks_pcie_v3_65_scan_bus,
  654. };
  655. static irqreturn_t ks_pcie_err_irq_handler(int irq, void *priv)
  656. {
  657. struct keystone_pcie *ks_pcie = priv;
  658. return ks_pcie_handle_error_irq(ks_pcie);
  659. }
  660. static int __init ks_pcie_add_pcie_port(struct keystone_pcie *ks_pcie,
  661. struct platform_device *pdev)
  662. {
  663. struct dw_pcie *pci = ks_pcie->pci;
  664. struct pcie_port *pp = &pci->pp;
  665. struct device *dev = &pdev->dev;
  666. int ret;
  667. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  668. "legacy-interrupt-controller",
  669. &ks_pcie->num_legacy_host_irqs);
  670. if (ret)
  671. return ret;
  672. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  673. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  674. "msi-interrupt-controller",
  675. &ks_pcie->num_msi_host_irqs);
  676. if (ret)
  677. return ret;
  678. }
  679. /*
  680. * Index 0 is the platform interrupt for error interrupt
  681. * from RC. This is optional.
  682. */
  683. ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
  684. if (ks_pcie->error_irq <= 0)
  685. dev_info(dev, "no error IRQ defined\n");
  686. else {
  687. ret = request_irq(ks_pcie->error_irq, ks_pcie_err_irq_handler,
  688. IRQF_SHARED, "pcie-error-irq", ks_pcie);
  689. if (ret < 0) {
  690. dev_err(dev, "failed to request error IRQ %d\n",
  691. ks_pcie->error_irq);
  692. return ret;
  693. }
  694. }
  695. pp->ops = &ks_pcie_host_ops;
  696. ret = ks_pcie_dw_host_init(ks_pcie);
  697. if (ret) {
  698. dev_err(dev, "failed to initialize host\n");
  699. return ret;
  700. }
  701. return 0;
  702. }
  703. static const struct of_device_id ks_pcie_of_match[] = {
  704. {
  705. .type = "pci",
  706. .compatible = "ti,keystone-pcie",
  707. },
  708. { },
  709. };
  710. static const struct dw_pcie_ops ks_pcie_dw_pcie_ops = {
  711. .link_up = ks_pcie_link_up,
  712. };
  713. static void ks_pcie_disable_phy(struct keystone_pcie *ks_pcie)
  714. {
  715. int num_lanes = ks_pcie->num_lanes;
  716. while (num_lanes--) {
  717. phy_power_off(ks_pcie->phy[num_lanes]);
  718. phy_exit(ks_pcie->phy[num_lanes]);
  719. }
  720. }
  721. static int ks_pcie_enable_phy(struct keystone_pcie *ks_pcie)
  722. {
  723. int i;
  724. int ret;
  725. int num_lanes = ks_pcie->num_lanes;
  726. for (i = 0; i < num_lanes; i++) {
  727. ret = phy_init(ks_pcie->phy[i]);
  728. if (ret < 0)
  729. goto err_phy;
  730. ret = phy_power_on(ks_pcie->phy[i]);
  731. if (ret < 0) {
  732. phy_exit(ks_pcie->phy[i]);
  733. goto err_phy;
  734. }
  735. }
  736. return 0;
  737. err_phy:
  738. while (--i >= 0) {
  739. phy_power_off(ks_pcie->phy[i]);
  740. phy_exit(ks_pcie->phy[i]);
  741. }
  742. return ret;
  743. }
  744. static int __init ks_pcie_probe(struct platform_device *pdev)
  745. {
  746. struct device *dev = &pdev->dev;
  747. struct device_node *np = dev->of_node;
  748. struct dw_pcie *pci;
  749. struct keystone_pcie *ks_pcie;
  750. struct device_link **link;
  751. u32 num_viewport;
  752. struct phy **phy;
  753. u32 num_lanes;
  754. char name[10];
  755. int ret;
  756. int i;
  757. ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
  758. if (!ks_pcie)
  759. return -ENOMEM;
  760. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  761. if (!pci)
  762. return -ENOMEM;
  763. pci->dev = dev;
  764. pci->ops = &ks_pcie_dw_pcie_ops;
  765. ret = of_property_read_u32(np, "num-viewport", &num_viewport);
  766. if (ret < 0) {
  767. dev_err(dev, "unable to read *num-viewport* property\n");
  768. return ret;
  769. }
  770. ret = of_property_read_u32(np, "num-lanes", &num_lanes);
  771. if (ret)
  772. num_lanes = 1;
  773. phy = devm_kzalloc(dev, sizeof(*phy) * num_lanes, GFP_KERNEL);
  774. if (!phy)
  775. return -ENOMEM;
  776. link = devm_kzalloc(dev, sizeof(*link) * num_lanes, GFP_KERNEL);
  777. if (!link)
  778. return -ENOMEM;
  779. for (i = 0; i < num_lanes; i++) {
  780. snprintf(name, sizeof(name), "pcie-phy%d", i);
  781. phy[i] = devm_phy_optional_get(dev, name);
  782. if (IS_ERR(phy[i])) {
  783. ret = PTR_ERR(phy[i]);
  784. goto err_link;
  785. }
  786. if (!phy[i])
  787. continue;
  788. link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
  789. if (!link[i]) {
  790. ret = -EINVAL;
  791. goto err_link;
  792. }
  793. }
  794. ks_pcie->np = np;
  795. ks_pcie->pci = pci;
  796. ks_pcie->link = link;
  797. ks_pcie->num_lanes = num_lanes;
  798. ks_pcie->num_viewport = num_viewport;
  799. ks_pcie->phy = phy;
  800. ret = ks_pcie_enable_phy(ks_pcie);
  801. if (ret) {
  802. dev_err(dev, "failed to enable phy\n");
  803. goto err_link;
  804. }
  805. platform_set_drvdata(pdev, ks_pcie);
  806. pm_runtime_enable(dev);
  807. ret = pm_runtime_get_sync(dev);
  808. if (ret < 0) {
  809. dev_err(dev, "pm_runtime_get_sync failed\n");
  810. goto err_get_sync;
  811. }
  812. ret = ks_pcie_add_pcie_port(ks_pcie, pdev);
  813. if (ret < 0)
  814. goto err_get_sync;
  815. return 0;
  816. err_get_sync:
  817. pm_runtime_put(dev);
  818. pm_runtime_disable(dev);
  819. ks_pcie_disable_phy(ks_pcie);
  820. err_link:
  821. while (--i >= 0 && link[i])
  822. device_link_del(link[i]);
  823. return ret;
  824. }
  825. static int __exit ks_pcie_remove(struct platform_device *pdev)
  826. {
  827. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  828. struct device_link **link = ks_pcie->link;
  829. int num_lanes = ks_pcie->num_lanes;
  830. struct device *dev = &pdev->dev;
  831. pm_runtime_put(dev);
  832. pm_runtime_disable(dev);
  833. ks_pcie_disable_phy(ks_pcie);
  834. while (num_lanes--)
  835. device_link_del(link[num_lanes]);
  836. return 0;
  837. }
  838. static struct platform_driver ks_pcie_driver __refdata = {
  839. .probe = ks_pcie_probe,
  840. .remove = __exit_p(ks_pcie_remove),
  841. .driver = {
  842. .name = "keystone-pcie",
  843. .of_match_table = of_match_ptr(ks_pcie_of_match),
  844. },
  845. };
  846. builtin_platform_driver(ks_pcie_driver);