pci-keystone.c 26 KB

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