pci-keystone.c 25 KB

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