pci-keystone.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irqchip/chained_irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/msi.h>
  19. #include <linux/of.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_pci.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. #include <linux/resource.h>
  26. #include <linux/signal.h>
  27. #include "pcie-designware.h"
  28. #define PCIE_VENDORID_MASK 0xffff
  29. #define PCIE_DEVICEID_SHIFT 16
  30. /* Application registers */
  31. #define CMD_STATUS 0x004
  32. #define LTSSM_EN_VAL BIT(0)
  33. #define OB_XLAT_EN_VAL BIT(1)
  34. #define DBI_CS2 BIT(5)
  35. #define CFG_SETUP 0x008
  36. #define CFG_BUS(x) (((x) & 0xff) << 16)
  37. #define CFG_DEVICE(x) (((x) & 0x1f) << 8)
  38. #define CFG_FUNC(x) ((x) & 0x7)
  39. #define CFG_TYPE1 BIT(24)
  40. #define OB_SIZE 0x030
  41. #define SPACE0_REMOTE_CFG_OFFSET 0x1000
  42. #define OB_OFFSET_INDEX(n) (0x200 + (8 * (n)))
  43. #define OB_OFFSET_HI(n) (0x204 + (8 * (n)))
  44. #define OB_ENABLEN BIT(0)
  45. #define OB_WIN_SIZE 8 /* 8MB */
  46. /* IRQ register defines */
  47. #define IRQ_EOI 0x050
  48. #define IRQ_STATUS 0x184
  49. #define IRQ_ENABLE_SET 0x188
  50. #define IRQ_ENABLE_CLR 0x18c
  51. #define MSI_IRQ 0x054
  52. #define MSI0_IRQ_STATUS 0x104
  53. #define MSI0_IRQ_ENABLE_SET 0x108
  54. #define MSI0_IRQ_ENABLE_CLR 0x10c
  55. #define IRQ_STATUS 0x184
  56. #define MSI_IRQ_OFFSET 4
  57. #define ERR_IRQ_STATUS 0x1c4
  58. #define ERR_IRQ_ENABLE_SET 0x1c8
  59. #define ERR_AER BIT(5) /* ECRC error */
  60. #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
  61. #define ERR_CORR BIT(3) /* Correctable error */
  62. #define ERR_NONFATAL BIT(2) /* Non-fatal error */
  63. #define ERR_FATAL BIT(1) /* Fatal error */
  64. #define ERR_SYS BIT(0) /* System error */
  65. #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
  66. ERR_NONFATAL | ERR_FATAL | ERR_SYS)
  67. #define MAX_MSI_HOST_IRQS 8
  68. /* PCIE controller device IDs */
  69. #define PCIE_RC_K2HK 0xb008
  70. #define PCIE_RC_K2E 0xb009
  71. #define PCIE_RC_K2L 0xb00a
  72. #define PCIE_RC_K2G 0xb00b
  73. #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
  74. struct keystone_pcie {
  75. struct dw_pcie *pci;
  76. /* PCI Device ID */
  77. u32 device_id;
  78. int num_legacy_host_irqs;
  79. int legacy_host_irqs[PCI_NUM_INTX];
  80. struct device_node *legacy_intc_np;
  81. int num_msi_host_irqs;
  82. int msi_host_irqs[MAX_MSI_HOST_IRQS];
  83. int num_lanes;
  84. u32 num_viewport;
  85. struct phy **phy;
  86. struct device_link **link;
  87. struct device_node *msi_intc_np;
  88. struct irq_domain *legacy_irq_domain;
  89. struct device_node *np;
  90. int error_irq;
  91. /* Application register space */
  92. void __iomem *va_app_base; /* DT 1st resource */
  93. struct resource app;
  94. };
  95. static inline void update_reg_offset_bit_pos(u32 offset, u32 *reg_offset,
  96. u32 *bit_pos)
  97. {
  98. *reg_offset = offset % 8;
  99. *bit_pos = offset >> 3;
  100. }
  101. static phys_addr_t ks_pcie_get_msi_addr(struct pcie_port *pp)
  102. {
  103. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  104. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  105. return ks_pcie->app.start + MSI_IRQ;
  106. }
  107. static u32 ks_pcie_app_readl(struct keystone_pcie *ks_pcie, u32 offset)
  108. {
  109. return readl(ks_pcie->va_app_base + offset);
  110. }
  111. static void ks_pcie_app_writel(struct keystone_pcie *ks_pcie, u32 offset,
  112. u32 val)
  113. {
  114. writel(val, ks_pcie->va_app_base + offset);
  115. }
  116. static void ks_pcie_handle_msi_irq(struct keystone_pcie *ks_pcie, int offset)
  117. {
  118. struct dw_pcie *pci = ks_pcie->pci;
  119. struct pcie_port *pp = &pci->pp;
  120. struct device *dev = pci->dev;
  121. u32 pending, vector;
  122. int src, virq;
  123. pending = ks_pcie_app_readl(ks_pcie, MSI0_IRQ_STATUS + (offset << 4));
  124. /*
  125. * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
  126. * shows 1, 9, 17, 25 and so forth
  127. */
  128. for (src = 0; src < 4; src++) {
  129. if (BIT(src) & pending) {
  130. vector = offset + (src << 3);
  131. virq = irq_linear_revmap(pp->irq_domain, vector);
  132. dev_dbg(dev, "irq: bit %d, vector %d, virq %d\n",
  133. src, vector, virq);
  134. generic_handle_irq(virq);
  135. }
  136. }
  137. }
  138. static void ks_pcie_msi_irq_ack(int irq, struct pcie_port *pp)
  139. {
  140. u32 reg_offset, bit_pos;
  141. struct keystone_pcie *ks_pcie;
  142. struct dw_pcie *pci;
  143. pci = to_dw_pcie_from_pp(pp);
  144. ks_pcie = to_keystone_pcie(pci);
  145. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  146. ks_pcie_app_writel(ks_pcie, MSI0_IRQ_STATUS + (reg_offset << 4),
  147. BIT(bit_pos));
  148. ks_pcie_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET);
  149. }
  150. static void ks_pcie_msi_set_irq(struct pcie_port *pp, int irq)
  151. {
  152. u32 reg_offset, bit_pos;
  153. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  154. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  155. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  156. ks_pcie_app_writel(ks_pcie, MSI0_IRQ_ENABLE_SET + (reg_offset << 4),
  157. BIT(bit_pos));
  158. }
  159. static void ks_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
  160. {
  161. u32 reg_offset, bit_pos;
  162. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  163. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  164. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  165. ks_pcie_app_writel(ks_pcie, MSI0_IRQ_ENABLE_CLR + (reg_offset << 4),
  166. BIT(bit_pos));
  167. }
  168. static int ks_pcie_msi_host_init(struct pcie_port *pp)
  169. {
  170. return dw_pcie_allocate_domains(pp);
  171. }
  172. static void ks_pcie_enable_legacy_irqs(struct keystone_pcie *ks_pcie)
  173. {
  174. int i;
  175. for (i = 0; i < PCI_NUM_INTX; i++)
  176. ks_pcie_app_writel(ks_pcie, IRQ_ENABLE_SET + (i << 4), 0x1);
  177. }
  178. static void ks_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie,
  179. int offset)
  180. {
  181. struct dw_pcie *pci = ks_pcie->pci;
  182. struct device *dev = pci->dev;
  183. u32 pending;
  184. int virq;
  185. pending = ks_pcie_app_readl(ks_pcie, IRQ_STATUS + (offset << 4));
  186. if (BIT(0) & pending) {
  187. virq = irq_linear_revmap(ks_pcie->legacy_irq_domain, offset);
  188. dev_dbg(dev, ": irq: irq_offset %d, virq %d\n", offset, virq);
  189. generic_handle_irq(virq);
  190. }
  191. /* EOI the INTx interrupt */
  192. ks_pcie_app_writel(ks_pcie, IRQ_EOI, offset);
  193. }
  194. static void ks_pcie_enable_error_irq(struct keystone_pcie *ks_pcie)
  195. {
  196. ks_pcie_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL);
  197. }
  198. static irqreturn_t ks_pcie_handle_error_irq(struct keystone_pcie *ks_pcie)
  199. {
  200. u32 reg;
  201. struct device *dev = ks_pcie->pci->dev;
  202. reg = ks_pcie_app_readl(ks_pcie, ERR_IRQ_STATUS);
  203. if (!reg)
  204. return IRQ_NONE;
  205. if (reg & ERR_SYS)
  206. dev_err(dev, "System Error\n");
  207. if (reg & ERR_FATAL)
  208. dev_err(dev, "Fatal Error\n");
  209. if (reg & ERR_NONFATAL)
  210. dev_dbg(dev, "Non Fatal Error\n");
  211. if (reg & ERR_CORR)
  212. dev_dbg(dev, "Correctable Error\n");
  213. if (reg & ERR_AXI)
  214. dev_err(dev, "AXI tag lookup fatal Error\n");
  215. if (reg & ERR_AER)
  216. dev_err(dev, "ECRC Error\n");
  217. ks_pcie_app_writel(ks_pcie, ERR_IRQ_STATUS, reg);
  218. return IRQ_HANDLED;
  219. }
  220. static void ks_pcie_ack_legacy_irq(struct irq_data *d)
  221. {
  222. }
  223. static void ks_pcie_mask_legacy_irq(struct irq_data *d)
  224. {
  225. }
  226. static void ks_pcie_unmask_legacy_irq(struct irq_data *d)
  227. {
  228. }
  229. static struct irq_chip ks_pcie_legacy_irq_chip = {
  230. .name = "Keystone-PCI-Legacy-IRQ",
  231. .irq_ack = ks_pcie_ack_legacy_irq,
  232. .irq_mask = ks_pcie_mask_legacy_irq,
  233. .irq_unmask = ks_pcie_unmask_legacy_irq,
  234. };
  235. static int ks_pcie_init_legacy_irq_map(struct irq_domain *d,
  236. unsigned int irq,
  237. irq_hw_number_t hw_irq)
  238. {
  239. irq_set_chip_and_handler(irq, &ks_pcie_legacy_irq_chip,
  240. handle_level_irq);
  241. irq_set_chip_data(irq, d->host_data);
  242. return 0;
  243. }
  244. static const struct irq_domain_ops ks_pcie_legacy_irq_domain_ops = {
  245. .map = ks_pcie_init_legacy_irq_map,
  246. .xlate = irq_domain_xlate_onetwocell,
  247. };
  248. /**
  249. * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
  250. * registers
  251. *
  252. * Since modification of dbi_cs2 involves different clock domain, read the
  253. * status back to ensure the transition is complete.
  254. */
  255. static void ks_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie)
  256. {
  257. u32 val;
  258. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  259. val |= DBI_CS2;
  260. ks_pcie_app_writel(ks_pcie, CMD_STATUS, val);
  261. do {
  262. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  263. } while (!(val & DBI_CS2));
  264. }
  265. /**
  266. * ks_pcie_clear_dbi_mode() - Disable DBI mode
  267. *
  268. * Since modification of dbi_cs2 involves different clock domain, read the
  269. * status back to ensure the transition is complete.
  270. */
  271. static void ks_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie)
  272. {
  273. u32 val;
  274. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  275. val &= ~DBI_CS2;
  276. ks_pcie_app_writel(ks_pcie, CMD_STATUS, val);
  277. do {
  278. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  279. } while (val & DBI_CS2);
  280. }
  281. static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
  282. {
  283. u32 val;
  284. u32 num_viewport = ks_pcie->num_viewport;
  285. struct dw_pcie *pci = ks_pcie->pci;
  286. struct pcie_port *pp = &pci->pp;
  287. u64 start = pp->mem->start;
  288. u64 end = pp->mem->end;
  289. int i;
  290. /* Disable BARs for inbound access */
  291. ks_pcie_set_dbi_mode(ks_pcie);
  292. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
  293. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0);
  294. ks_pcie_clear_dbi_mode(ks_pcie);
  295. val = ilog2(OB_WIN_SIZE);
  296. ks_pcie_app_writel(ks_pcie, OB_SIZE, val);
  297. /* Using Direct 1:1 mapping of RC <-> PCI memory space */
  298. for (i = 0; i < num_viewport && (start < end); i++) {
  299. ks_pcie_app_writel(ks_pcie, OB_OFFSET_INDEX(i),
  300. lower_32_bits(start) | OB_ENABLEN);
  301. ks_pcie_app_writel(ks_pcie, OB_OFFSET_HI(i),
  302. upper_32_bits(start));
  303. start += OB_WIN_SIZE;
  304. }
  305. val = ks_pcie_app_readl(ks_pcie, CMD_STATUS);
  306. val |= OB_XLAT_EN_VAL;
  307. ks_pcie_app_writel(ks_pcie, CMD_STATUS, val);
  308. }
  309. static int ks_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  310. unsigned int devfn, int where, int size,
  311. u32 *val)
  312. {
  313. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  314. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  315. u32 reg;
  316. reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
  317. CFG_FUNC(PCI_FUNC(devfn));
  318. if (bus->parent->number != pp->root_bus_nr)
  319. reg |= CFG_TYPE1;
  320. ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
  321. return dw_pcie_read(pp->va_cfg0_base + where, size, val);
  322. }
  323. static int ks_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  324. unsigned int devfn, int where, int size,
  325. u32 val)
  326. {
  327. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  328. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  329. u32 reg;
  330. reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) |
  331. CFG_FUNC(PCI_FUNC(devfn));
  332. if (bus->parent->number != pp->root_bus_nr)
  333. reg |= CFG_TYPE1;
  334. ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg);
  335. return dw_pcie_write(pp->va_cfg0_base + where, size, val);
  336. }
  337. /**
  338. * ks_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
  339. *
  340. * This sets BAR0 to enable inbound access for MSI_IRQ register
  341. */
  342. static void ks_pcie_v3_65_scan_bus(struct pcie_port *pp)
  343. {
  344. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  345. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  346. /* Configure and set up BAR0 */
  347. ks_pcie_set_dbi_mode(ks_pcie);
  348. /* Enable BAR0 */
  349. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1);
  350. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1);
  351. ks_pcie_clear_dbi_mode(ks_pcie);
  352. /*
  353. * For BAR0, just setting bus address for inbound writes (MSI) should
  354. * be sufficient. Use physical address to avoid any conflicts.
  355. */
  356. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start);
  357. }
  358. /**
  359. * ks_pcie_link_up() - Check if link up
  360. */
  361. static int ks_pcie_link_up(struct dw_pcie *pci)
  362. {
  363. u32 val;
  364. val = dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0);
  365. val &= PORT_LOGIC_LTSSM_STATE_MASK;
  366. return (val == PORT_LOGIC_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);