pci-keystone.c 26 KB

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