pci-keystone.c 24 KB

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