pci-keystone.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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/msi.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/resource.h>
  24. #include <linux/signal.h>
  25. #include "pcie-designware.h"
  26. #define DRIVER_NAME "keystone-pcie"
  27. /* DEV_STAT_CTRL */
  28. #define PCIE_CAP_BASE 0x70
  29. /* Application register defines */
  30. #define LTSSM_EN_VAL BIT(0)
  31. #define LTSSM_STATE_MASK 0x1f
  32. #define LTSSM_STATE_L0 0x11
  33. #define DBI_CS2_EN_VAL 0x20
  34. #define OB_XLAT_EN_VAL 2
  35. /* Application registers */
  36. #define CMD_STATUS 0x004
  37. #define CFG_SETUP 0x008
  38. #define OB_SIZE 0x030
  39. #define CFG_PCIM_WIN_SZ_IDX 3
  40. #define CFG_PCIM_WIN_CNT 32
  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. /* IRQ register defines */
  45. #define IRQ_EOI 0x050
  46. #define IRQ_STATUS 0x184
  47. #define IRQ_ENABLE_SET 0x188
  48. #define IRQ_ENABLE_CLR 0x18c
  49. #define MSI_IRQ 0x054
  50. #define MSI0_IRQ_STATUS 0x104
  51. #define MSI0_IRQ_ENABLE_SET 0x108
  52. #define MSI0_IRQ_ENABLE_CLR 0x10c
  53. #define IRQ_STATUS 0x184
  54. #define MSI_IRQ_OFFSET 4
  55. /* Error IRQ bits */
  56. #define ERR_AER BIT(5) /* ECRC error */
  57. #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
  58. #define ERR_CORR BIT(3) /* Correctable error */
  59. #define ERR_NONFATAL BIT(2) /* Non-fatal error */
  60. #define ERR_FATAL BIT(1) /* Fatal error */
  61. #define ERR_SYS BIT(0) /* System (fatal, non-fatal, or correctable) */
  62. #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
  63. ERR_NONFATAL | ERR_FATAL | ERR_SYS)
  64. #define ERR_FATAL_IRQ (ERR_FATAL | ERR_AXI)
  65. #define ERR_IRQ_STATUS_RAW 0x1c0
  66. #define ERR_IRQ_STATUS 0x1c4
  67. #define ERR_IRQ_ENABLE_SET 0x1c8
  68. #define ERR_IRQ_ENABLE_CLR 0x1cc
  69. /* Config space registers */
  70. #define DEBUG0 0x728
  71. #define MAX_MSI_HOST_IRQS 8
  72. /* PCIE controller device IDs */
  73. #define PCIE_RC_K2HK 0xb008
  74. #define PCIE_RC_K2E 0xb009
  75. #define PCIE_RC_K2L 0xb00a
  76. #define PCIE_RC_K2G 0xb00b
  77. #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
  78. struct keystone_pcie {
  79. struct dw_pcie *pci;
  80. struct clk *clk;
  81. /* PCI Device ID */
  82. u32 device_id;
  83. int num_legacy_host_irqs;
  84. int legacy_host_irqs[PCI_NUM_INTX];
  85. struct device_node *legacy_intc_np;
  86. int num_msi_host_irqs;
  87. int msi_host_irqs[MAX_MSI_HOST_IRQS];
  88. struct device_node *msi_intc_np;
  89. struct irq_domain *legacy_irq_domain;
  90. struct device_node *np;
  91. int error_irq;
  92. /* Application register space */
  93. void __iomem *va_app_base; /* DT 1st resource */
  94. struct resource app;
  95. };
  96. static inline void update_reg_offset_bit_pos(u32 offset, u32 *reg_offset,
  97. u32 *bit_pos)
  98. {
  99. *reg_offset = offset % 8;
  100. *bit_pos = offset >> 3;
  101. }
  102. static phys_addr_t ks_dw_pcie_get_msi_addr(struct pcie_port *pp)
  103. {
  104. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  105. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  106. return ks_pcie->app.start + MSI_IRQ;
  107. }
  108. static u32 ks_dw_app_readl(struct keystone_pcie *ks_pcie, u32 offset)
  109. {
  110. return readl(ks_pcie->va_app_base + offset);
  111. }
  112. static void ks_dw_app_writel(struct keystone_pcie *ks_pcie, u32 offset, u32 val)
  113. {
  114. writel(val, ks_pcie->va_app_base + offset);
  115. }
  116. static void ks_dw_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_dw_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_dw_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_dw_app_writel(ks_pcie, MSI0_IRQ_STATUS + (reg_offset << 4),
  147. BIT(bit_pos));
  148. ks_dw_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET);
  149. }
  150. static void ks_dw_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_dw_app_writel(ks_pcie, MSI0_IRQ_ENABLE_SET + (reg_offset << 4),
  157. BIT(bit_pos));
  158. }
  159. static void ks_dw_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_dw_app_writel(ks_pcie, MSI0_IRQ_ENABLE_CLR + (reg_offset << 4),
  166. BIT(bit_pos));
  167. }
  168. static int ks_dw_pcie_msi_host_init(struct pcie_port *pp)
  169. {
  170. return dw_pcie_allocate_domains(pp);
  171. }
  172. static void ks_dw_pcie_enable_legacy_irqs(struct keystone_pcie *ks_pcie)
  173. {
  174. int i;
  175. for (i = 0; i < PCI_NUM_INTX; i++)
  176. ks_dw_app_writel(ks_pcie, IRQ_ENABLE_SET + (i << 4), 0x1);
  177. }
  178. static void ks_dw_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_dw_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_dw_app_writel(ks_pcie, IRQ_EOI, offset);
  193. }
  194. static void ks_dw_pcie_enable_error_irq(struct keystone_pcie *ks_pcie)
  195. {
  196. ks_dw_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL);
  197. }
  198. static irqreturn_t ks_dw_pcie_handle_error_irq(struct keystone_pcie *ks_pcie)
  199. {
  200. u32 status;
  201. status = ks_dw_app_readl(ks_pcie, ERR_IRQ_STATUS_RAW) & ERR_IRQ_ALL;
  202. if (!status)
  203. return IRQ_NONE;
  204. if (status & ERR_FATAL_IRQ)
  205. dev_err(ks_pcie->pci->dev, "fatal error (status %#010x)\n",
  206. status);
  207. /* Ack the IRQ; status bits are RW1C */
  208. ks_dw_app_writel(ks_pcie, ERR_IRQ_STATUS, status);
  209. return IRQ_HANDLED;
  210. }
  211. static void ks_dw_pcie_ack_legacy_irq(struct irq_data *d)
  212. {
  213. }
  214. static void ks_dw_pcie_mask_legacy_irq(struct irq_data *d)
  215. {
  216. }
  217. static void ks_dw_pcie_unmask_legacy_irq(struct irq_data *d)
  218. {
  219. }
  220. static struct irq_chip ks_dw_pcie_legacy_irq_chip = {
  221. .name = "Keystone-PCI-Legacy-IRQ",
  222. .irq_ack = ks_dw_pcie_ack_legacy_irq,
  223. .irq_mask = ks_dw_pcie_mask_legacy_irq,
  224. .irq_unmask = ks_dw_pcie_unmask_legacy_irq,
  225. };
  226. static int ks_dw_pcie_init_legacy_irq_map(struct irq_domain *d,
  227. unsigned int irq,
  228. irq_hw_number_t hw_irq)
  229. {
  230. irq_set_chip_and_handler(irq, &ks_dw_pcie_legacy_irq_chip,
  231. handle_level_irq);
  232. irq_set_chip_data(irq, d->host_data);
  233. return 0;
  234. }
  235. static const struct irq_domain_ops ks_dw_pcie_legacy_irq_domain_ops = {
  236. .map = ks_dw_pcie_init_legacy_irq_map,
  237. .xlate = irq_domain_xlate_onetwocell,
  238. };
  239. /**
  240. * ks_dw_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
  241. * registers
  242. *
  243. * Since modification of dbi_cs2 involves different clock domain, read the
  244. * status back to ensure the transition is complete.
  245. */
  246. static void ks_dw_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie)
  247. {
  248. u32 val;
  249. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  250. ks_dw_app_writel(ks_pcie, CMD_STATUS, DBI_CS2_EN_VAL | val);
  251. do {
  252. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  253. } while (!(val & DBI_CS2_EN_VAL));
  254. }
  255. /**
  256. * ks_dw_pcie_clear_dbi_mode() - Disable DBI mode
  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_dw_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie)
  262. {
  263. u32 val;
  264. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  265. ks_dw_app_writel(ks_pcie, CMD_STATUS, ~DBI_CS2_EN_VAL & val);
  266. do {
  267. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  268. } while (val & DBI_CS2_EN_VAL);
  269. }
  270. static void ks_dw_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
  271. {
  272. struct dw_pcie *pci = ks_pcie->pci;
  273. struct pcie_port *pp = &pci->pp;
  274. u32 start = pp->mem->start, end = pp->mem->end;
  275. int i, tr_size;
  276. u32 val;
  277. /* Disable BARs for inbound access */
  278. ks_dw_pcie_set_dbi_mode(ks_pcie);
  279. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
  280. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0);
  281. ks_dw_pcie_clear_dbi_mode(ks_pcie);
  282. /* Set outbound translation size per window division */
  283. ks_dw_app_writel(ks_pcie, OB_SIZE, CFG_PCIM_WIN_SZ_IDX & 0x7);
  284. tr_size = (1 << (CFG_PCIM_WIN_SZ_IDX & 0x7)) * SZ_1M;
  285. /* Using Direct 1:1 mapping of RC <-> PCI memory space */
  286. for (i = 0; (i < CFG_PCIM_WIN_CNT) && (start < end); i++) {
  287. ks_dw_app_writel(ks_pcie, OB_OFFSET_INDEX(i), start | 1);
  288. ks_dw_app_writel(ks_pcie, OB_OFFSET_HI(i), 0);
  289. start += tr_size;
  290. }
  291. /* Enable OB translation */
  292. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  293. ks_dw_app_writel(ks_pcie, CMD_STATUS, OB_XLAT_EN_VAL | val);
  294. }
  295. /**
  296. * ks_pcie_cfg_setup() - Set up configuration space address for a device
  297. *
  298. * @ks_pcie: ptr to keystone_pcie structure
  299. * @bus: Bus number the device is residing on
  300. * @devfn: device, function number info
  301. *
  302. * Forms and returns the address of configuration space mapped in PCIESS
  303. * address space 0. Also configures CFG_SETUP for remote configuration space
  304. * access.
  305. *
  306. * The address space has two regions to access configuration - local and remote.
  307. * We access local region for bus 0 (as RC is attached on bus 0) and remote
  308. * region for others with TYPE 1 access when bus > 1. As for device on bus = 1,
  309. * we will do TYPE 0 access as it will be on our secondary bus (logical).
  310. * CFG_SETUP is needed only for remote configuration access.
  311. */
  312. static void __iomem *ks_pcie_cfg_setup(struct keystone_pcie *ks_pcie, u8 bus,
  313. unsigned int devfn)
  314. {
  315. u8 device = PCI_SLOT(devfn), function = PCI_FUNC(devfn);
  316. struct dw_pcie *pci = ks_pcie->pci;
  317. struct pcie_port *pp = &pci->pp;
  318. u32 regval;
  319. if (bus == 0)
  320. return pci->dbi_base;
  321. regval = (bus << 16) | (device << 8) | function;
  322. /*
  323. * Since Bus#1 will be a virtual bus, we need to have TYPE0
  324. * access only.
  325. * TYPE 1
  326. */
  327. if (bus != 1)
  328. regval |= BIT(24);
  329. ks_dw_app_writel(ks_pcie, CFG_SETUP, regval);
  330. return pp->va_cfg0_base;
  331. }
  332. static int ks_dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  333. unsigned int devfn, int where, int size,
  334. u32 *val)
  335. {
  336. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  337. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  338. u8 bus_num = bus->number;
  339. void __iomem *addr;
  340. addr = ks_pcie_cfg_setup(ks_pcie, bus_num, devfn);
  341. return dw_pcie_read(addr + where, size, val);
  342. }
  343. static int ks_dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  344. unsigned int devfn, int where, int size,
  345. u32 val)
  346. {
  347. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  348. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  349. u8 bus_num = bus->number;
  350. void __iomem *addr;
  351. addr = ks_pcie_cfg_setup(ks_pcie, bus_num, devfn);
  352. return dw_pcie_write(addr + where, size, val);
  353. }
  354. /**
  355. * ks_dw_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
  356. *
  357. * This sets BAR0 to enable inbound access for MSI_IRQ register
  358. */
  359. static void ks_dw_pcie_v3_65_scan_bus(struct pcie_port *pp)
  360. {
  361. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  362. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  363. /* Configure and set up BAR0 */
  364. ks_dw_pcie_set_dbi_mode(ks_pcie);
  365. /* Enable BAR0 */
  366. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1);
  367. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1);
  368. ks_dw_pcie_clear_dbi_mode(ks_pcie);
  369. /*
  370. * For BAR0, just setting bus address for inbound writes (MSI) should
  371. * be sufficient. Use physical address to avoid any conflicts.
  372. */
  373. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start);
  374. }
  375. /**
  376. * ks_dw_pcie_link_up() - Check if link up
  377. */
  378. static int ks_dw_pcie_link_up(struct dw_pcie *pci)
  379. {
  380. u32 val;
  381. val = dw_pcie_readl_dbi(pci, DEBUG0);
  382. return (val & LTSSM_STATE_MASK) == LTSSM_STATE_L0;
  383. }
  384. static void ks_dw_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
  385. {
  386. u32 val;
  387. /* Disable Link training */
  388. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  389. val &= ~LTSSM_EN_VAL;
  390. ks_dw_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
  391. /* Initiate Link Training */
  392. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  393. ks_dw_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
  394. }
  395. /**
  396. * ks_dw_pcie_host_init() - initialize host for v3_65 dw hardware
  397. *
  398. * Ioremap the register resources, initialize legacy irq domain
  399. * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
  400. * PCI host controller.
  401. */
  402. static int __init ks_dw_pcie_host_init(struct keystone_pcie *ks_pcie)
  403. {
  404. struct dw_pcie *pci = ks_pcie->pci;
  405. struct pcie_port *pp = &pci->pp;
  406. struct device *dev = pci->dev;
  407. struct platform_device *pdev = to_platform_device(dev);
  408. struct resource *res;
  409. /* Index 0 is the config reg. space address */
  410. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  411. pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
  412. if (IS_ERR(pci->dbi_base))
  413. return PTR_ERR(pci->dbi_base);
  414. /*
  415. * We set these same and is used in pcie rd/wr_other_conf
  416. * functions
  417. */
  418. pp->va_cfg0_base = pci->dbi_base + SPACE0_REMOTE_CFG_OFFSET;
  419. pp->va_cfg1_base = pp->va_cfg0_base;
  420. /* Index 1 is the application reg. space address */
  421. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  422. ks_pcie->va_app_base = devm_ioremap_resource(dev, res);
  423. if (IS_ERR(ks_pcie->va_app_base))
  424. return PTR_ERR(ks_pcie->va_app_base);
  425. ks_pcie->app = *res;
  426. /* Create legacy IRQ domain */
  427. ks_pcie->legacy_irq_domain =
  428. irq_domain_add_linear(ks_pcie->legacy_intc_np,
  429. PCI_NUM_INTX,
  430. &ks_dw_pcie_legacy_irq_domain_ops,
  431. NULL);
  432. if (!ks_pcie->legacy_irq_domain) {
  433. dev_err(dev, "Failed to add irq domain for legacy irqs\n");
  434. return -EINVAL;
  435. }
  436. return dw_pcie_host_init(pp);
  437. }
  438. static void quirk_limit_mrrs(struct pci_dev *dev)
  439. {
  440. struct pci_bus *bus = dev->bus;
  441. struct pci_dev *bridge;
  442. static const struct pci_device_id rc_pci_devids[] = {
  443. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
  444. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  445. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
  446. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  447. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
  448. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  449. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G),
  450. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  451. { 0, },
  452. };
  453. if (pci_is_root_bus(bus))
  454. bridge = dev;
  455. /* look for the host bridge */
  456. while (!pci_is_root_bus(bus)) {
  457. bridge = bus->self;
  458. bus = bus->parent;
  459. }
  460. if (!bridge)
  461. return;
  462. /*
  463. * Keystone PCI controller has a h/w limitation of
  464. * 256 bytes maximum read request size. It can't handle
  465. * anything higher than this. So force this limit on
  466. * all downstream devices.
  467. */
  468. if (pci_match_id(rc_pci_devids, bridge)) {
  469. if (pcie_get_readrq(dev) > 256) {
  470. dev_info(&dev->dev, "limiting MRRS to 256\n");
  471. pcie_set_readrq(dev, 256);
  472. }
  473. }
  474. }
  475. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, quirk_limit_mrrs);
  476. static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
  477. {
  478. struct dw_pcie *pci = ks_pcie->pci;
  479. struct device *dev = pci->dev;
  480. if (dw_pcie_link_up(pci)) {
  481. dev_info(dev, "Link already up\n");
  482. return 0;
  483. }
  484. ks_dw_pcie_initiate_link_train(ks_pcie);
  485. /* check if the link is up or not */
  486. if (!dw_pcie_wait_for_link(pci))
  487. return 0;
  488. dev_err(dev, "phy link never came up\n");
  489. return -ETIMEDOUT;
  490. }
  491. static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
  492. {
  493. unsigned int irq = irq_desc_get_irq(desc);
  494. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  495. u32 offset = irq - ks_pcie->msi_host_irqs[0];
  496. struct dw_pcie *pci = ks_pcie->pci;
  497. struct device *dev = pci->dev;
  498. struct irq_chip *chip = irq_desc_get_chip(desc);
  499. dev_dbg(dev, "%s, irq %d\n", __func__, irq);
  500. /*
  501. * The chained irq handler installation would have replaced normal
  502. * interrupt driver handler so we need to take care of mask/unmask and
  503. * ack operation.
  504. */
  505. chained_irq_enter(chip, desc);
  506. ks_dw_pcie_handle_msi_irq(ks_pcie, offset);
  507. chained_irq_exit(chip, desc);
  508. }
  509. /**
  510. * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
  511. * @irq: IRQ line for legacy interrupts
  512. * @desc: Pointer to irq descriptor
  513. *
  514. * Traverse through pending legacy interrupts and invoke handler for each. Also
  515. * takes care of interrupt controller level mask/ack operation.
  516. */
  517. static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
  518. {
  519. unsigned int irq = irq_desc_get_irq(desc);
  520. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  521. struct dw_pcie *pci = ks_pcie->pci;
  522. struct device *dev = pci->dev;
  523. u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
  524. struct irq_chip *chip = irq_desc_get_chip(desc);
  525. dev_dbg(dev, ": Handling legacy irq %d\n", irq);
  526. /*
  527. * The chained irq handler installation would have replaced normal
  528. * interrupt driver handler so we need to take care of mask/unmask and
  529. * ack operation.
  530. */
  531. chained_irq_enter(chip, desc);
  532. ks_dw_pcie_handle_legacy_irq(ks_pcie, irq_offset);
  533. chained_irq_exit(chip, desc);
  534. }
  535. static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
  536. char *controller, int *num_irqs)
  537. {
  538. int temp, max_host_irqs, legacy = 1, *host_irqs;
  539. struct device *dev = ks_pcie->pci->dev;
  540. struct device_node *np_pcie = dev->of_node, **np_temp;
  541. if (!strcmp(controller, "msi-interrupt-controller"))
  542. legacy = 0;
  543. if (legacy) {
  544. np_temp = &ks_pcie->legacy_intc_np;
  545. max_host_irqs = PCI_NUM_INTX;
  546. host_irqs = &ks_pcie->legacy_host_irqs[0];
  547. } else {
  548. np_temp = &ks_pcie->msi_intc_np;
  549. max_host_irqs = MAX_MSI_HOST_IRQS;
  550. host_irqs = &ks_pcie->msi_host_irqs[0];
  551. }
  552. /* interrupt controller is in a child node */
  553. *np_temp = of_get_child_by_name(np_pcie, controller);
  554. if (!(*np_temp)) {
  555. dev_err(dev, "Node for %s is absent\n", controller);
  556. return -EINVAL;
  557. }
  558. temp = of_irq_count(*np_temp);
  559. if (!temp) {
  560. dev_err(dev, "No IRQ entries in %s\n", controller);
  561. of_node_put(*np_temp);
  562. return -EINVAL;
  563. }
  564. if (temp > max_host_irqs)
  565. dev_warn(dev, "Too many %s interrupts defined %u\n",
  566. (legacy ? "legacy" : "MSI"), temp);
  567. /*
  568. * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
  569. * 7 (MSI)
  570. */
  571. for (temp = 0; temp < max_host_irqs; temp++) {
  572. host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
  573. if (!host_irqs[temp])
  574. break;
  575. }
  576. of_node_put(*np_temp);
  577. if (temp) {
  578. *num_irqs = temp;
  579. return 0;
  580. }
  581. return -EINVAL;
  582. }
  583. static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
  584. {
  585. int i;
  586. /* Legacy IRQ */
  587. for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
  588. irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
  589. ks_pcie_legacy_irq_handler,
  590. ks_pcie);
  591. }
  592. ks_dw_pcie_enable_legacy_irqs(ks_pcie);
  593. /* MSI IRQ */
  594. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  595. for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
  596. irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
  597. ks_pcie_msi_irq_handler,
  598. ks_pcie);
  599. }
  600. }
  601. if (ks_pcie->error_irq > 0)
  602. ks_dw_pcie_enable_error_irq(ks_pcie);
  603. }
  604. /*
  605. * When a PCI device does not exist during config cycles, keystone host gets a
  606. * bus error instead of returning 0xffffffff. This handler always returns 0
  607. * for this kind of faults.
  608. */
  609. static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
  610. struct pt_regs *regs)
  611. {
  612. unsigned long instr = *(unsigned long *) instruction_pointer(regs);
  613. if ((instr & 0x0e100090) == 0x00100090) {
  614. int reg = (instr >> 12) & 15;
  615. regs->uregs[reg] = -1;
  616. regs->ARM_pc += 4;
  617. }
  618. return 0;
  619. }
  620. static int __init ks_pcie_host_init(struct pcie_port *pp)
  621. {
  622. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  623. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  624. dw_pcie_setup_rc(pp);
  625. ks_pcie_establish_link(ks_pcie);
  626. ks_dw_pcie_setup_rc_app_regs(ks_pcie);
  627. ks_pcie_setup_interrupts(ks_pcie);
  628. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  629. pci->dbi_base + PCI_IO_BASE);
  630. /* update the Vendor ID */
  631. writew(ks_pcie->device_id, pci->dbi_base + PCI_DEVICE_ID);
  632. /*
  633. * PCIe access errors that result into OCP errors are caught by ARM as
  634. * "External aborts"
  635. */
  636. hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
  637. "Asynchronous external abort");
  638. return 0;
  639. }
  640. static const struct dw_pcie_host_ops keystone_pcie_host_ops = {
  641. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  642. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  643. .host_init = ks_pcie_host_init,
  644. .msi_set_irq = ks_dw_pcie_msi_set_irq,
  645. .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
  646. .get_msi_addr = ks_dw_pcie_get_msi_addr,
  647. .msi_host_init = ks_dw_pcie_msi_host_init,
  648. .msi_irq_ack = ks_dw_pcie_msi_irq_ack,
  649. .scan_bus = ks_dw_pcie_v3_65_scan_bus,
  650. };
  651. static irqreturn_t pcie_err_irq_handler(int irq, void *priv)
  652. {
  653. struct keystone_pcie *ks_pcie = priv;
  654. return ks_dw_pcie_handle_error_irq(ks_pcie);
  655. }
  656. static int __init ks_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, 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 = &keystone_pcie_host_ops;
  692. ret = ks_dw_pcie_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 dw_pcie_ops = {
  707. .link_up = ks_dw_pcie_link_up,
  708. };
  709. static int __exit ks_pcie_remove(struct platform_device *pdev)
  710. {
  711. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  712. clk_disable_unprepare(ks_pcie->clk);
  713. return 0;
  714. }
  715. static int __init ks_pcie_probe(struct platform_device *pdev)
  716. {
  717. struct device *dev = &pdev->dev;
  718. struct dw_pcie *pci;
  719. struct keystone_pcie *ks_pcie;
  720. struct resource *res;
  721. void __iomem *reg_p;
  722. struct phy *phy;
  723. int ret;
  724. ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
  725. if (!ks_pcie)
  726. return -ENOMEM;
  727. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  728. if (!pci)
  729. return -ENOMEM;
  730. pci->dev = dev;
  731. pci->ops = &dw_pcie_ops;
  732. ks_pcie->pci = pci;
  733. /* initialize SerDes Phy if present */
  734. phy = devm_phy_get(dev, "pcie-phy");
  735. if (PTR_ERR_OR_ZERO(phy) == -EPROBE_DEFER)
  736. return PTR_ERR(phy);
  737. if (!IS_ERR_OR_NULL(phy)) {
  738. ret = phy_init(phy);
  739. if (ret < 0)
  740. return ret;
  741. }
  742. /* index 2 is to read PCI DEVICE_ID */
  743. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  744. reg_p = devm_ioremap_resource(dev, res);
  745. if (IS_ERR(reg_p))
  746. return PTR_ERR(reg_p);
  747. ks_pcie->device_id = readl(reg_p) >> 16;
  748. devm_iounmap(dev, reg_p);
  749. devm_release_mem_region(dev, res->start, resource_size(res));
  750. ks_pcie->np = dev->of_node;
  751. platform_set_drvdata(pdev, ks_pcie);
  752. ks_pcie->clk = devm_clk_get(dev, "pcie");
  753. if (IS_ERR(ks_pcie->clk)) {
  754. dev_err(dev, "Failed to get pcie rc clock\n");
  755. return PTR_ERR(ks_pcie->clk);
  756. }
  757. ret = clk_prepare_enable(ks_pcie->clk);
  758. if (ret)
  759. return ret;
  760. ret = ks_add_pcie_port(ks_pcie, pdev);
  761. if (ret < 0)
  762. goto fail_clk;
  763. return 0;
  764. fail_clk:
  765. clk_disable_unprepare(ks_pcie->clk);
  766. return ret;
  767. }
  768. static struct platform_driver ks_pcie_driver __refdata = {
  769. .probe = ks_pcie_probe,
  770. .remove = __exit_p(ks_pcie_remove),
  771. .driver = {
  772. .name = "keystone-pcie",
  773. .of_match_table = of_match_ptr(ks_pcie_of_match),
  774. },
  775. };
  776. builtin_platform_driver(ks_pcie_driver);