pci-keystone.c 25 KB

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