pci-keystone-dw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * Designware application register space functions for Keystone PCI controller
  3. *
  4. * Copyright (C) 2013-2014 Texas Instruments., Ltd.
  5. * http://www.ti.com
  6. *
  7. * Author: Murali Karicheri <m-karicheri2@ti.com>
  8. *
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/irq.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/irqreturn.h>
  17. #include <linux/module.h>
  18. #include <linux/of.h>
  19. #include <linux/of_pci.h>
  20. #include <linux/pci.h>
  21. #include <linux/platform_device.h>
  22. #include "pcie-designware.h"
  23. #include "pci-keystone.h"
  24. /* Application register defines */
  25. #define LTSSM_EN_VAL 1
  26. #define LTSSM_STATE_MASK 0x1f
  27. #define LTSSM_STATE_L0 0x11
  28. #define DBI_CS2_EN_VAL 0x20
  29. #define OB_XLAT_EN_VAL 2
  30. /* Application registers */
  31. #define CMD_STATUS 0x004
  32. #define CFG_SETUP 0x008
  33. #define OB_SIZE 0x030
  34. #define CFG_PCIM_WIN_SZ_IDX 3
  35. #define CFG_PCIM_WIN_CNT 32
  36. #define SPACE0_REMOTE_CFG_OFFSET 0x1000
  37. #define OB_OFFSET_INDEX(n) (0x200 + (8 * n))
  38. #define OB_OFFSET_HI(n) (0x204 + (8 * n))
  39. /* IRQ register defines */
  40. #define IRQ_EOI 0x050
  41. #define IRQ_STATUS 0x184
  42. #define IRQ_ENABLE_SET 0x188
  43. #define IRQ_ENABLE_CLR 0x18c
  44. #define MSI_IRQ 0x054
  45. #define MSI0_IRQ_STATUS 0x104
  46. #define MSI0_IRQ_ENABLE_SET 0x108
  47. #define MSI0_IRQ_ENABLE_CLR 0x10c
  48. #define IRQ_STATUS 0x184
  49. #define MSI_IRQ_OFFSET 4
  50. /* Error IRQ bits */
  51. #define ERR_AER BIT(5) /* ECRC error */
  52. #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
  53. #define ERR_CORR BIT(3) /* Correctable error */
  54. #define ERR_NONFATAL BIT(2) /* Non-fatal error */
  55. #define ERR_FATAL BIT(1) /* Fatal error */
  56. #define ERR_SYS BIT(0) /* System (fatal, non-fatal, or correctable) */
  57. #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
  58. ERR_NONFATAL | ERR_FATAL | ERR_SYS)
  59. #define ERR_FATAL_IRQ (ERR_FATAL | ERR_AXI)
  60. #define ERR_IRQ_STATUS_RAW 0x1c0
  61. #define ERR_IRQ_STATUS 0x1c4
  62. #define ERR_IRQ_ENABLE_SET 0x1c8
  63. #define ERR_IRQ_ENABLE_CLR 0x1cc
  64. /* Config space registers */
  65. #define DEBUG0 0x728
  66. #define to_keystone_pcie(x) container_of(x, struct keystone_pcie, pp)
  67. static inline void update_reg_offset_bit_pos(u32 offset, u32 *reg_offset,
  68. u32 *bit_pos)
  69. {
  70. *reg_offset = offset % 8;
  71. *bit_pos = offset >> 3;
  72. }
  73. phys_addr_t ks_dw_pcie_get_msi_addr(struct pcie_port *pp)
  74. {
  75. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  76. return ks_pcie->app.start + MSI_IRQ;
  77. }
  78. void ks_dw_pcie_handle_msi_irq(struct keystone_pcie *ks_pcie, int offset)
  79. {
  80. struct pcie_port *pp = &ks_pcie->pp;
  81. u32 pending, vector;
  82. int src, virq;
  83. pending = readl(ks_pcie->va_app_base + MSI0_IRQ_STATUS + (offset << 4));
  84. /*
  85. * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
  86. * shows 1, 9, 17, 25 and so forth
  87. */
  88. for (src = 0; src < 4; src++) {
  89. if (BIT(src) & pending) {
  90. vector = offset + (src << 3);
  91. virq = irq_linear_revmap(pp->irq_domain, vector);
  92. dev_dbg(pp->dev, "irq: bit %d, vector %d, virq %d\n",
  93. src, vector, virq);
  94. generic_handle_irq(virq);
  95. }
  96. }
  97. }
  98. static void ks_dw_pcie_msi_irq_ack(struct irq_data *d)
  99. {
  100. u32 offset, reg_offset, bit_pos;
  101. struct keystone_pcie *ks_pcie;
  102. struct msi_desc *msi;
  103. struct pcie_port *pp;
  104. msi = irq_data_get_msi_desc(d);
  105. pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
  106. ks_pcie = to_keystone_pcie(pp);
  107. offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
  108. update_reg_offset_bit_pos(offset, &reg_offset, &bit_pos);
  109. writel(BIT(bit_pos),
  110. ks_pcie->va_app_base + MSI0_IRQ_STATUS + (reg_offset << 4));
  111. writel(reg_offset + MSI_IRQ_OFFSET, ks_pcie->va_app_base + IRQ_EOI);
  112. }
  113. void ks_dw_pcie_msi_set_irq(struct pcie_port *pp, int irq)
  114. {
  115. u32 reg_offset, bit_pos;
  116. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  117. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  118. writel(BIT(bit_pos),
  119. ks_pcie->va_app_base + MSI0_IRQ_ENABLE_SET + (reg_offset << 4));
  120. }
  121. void ks_dw_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
  122. {
  123. u32 reg_offset, bit_pos;
  124. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  125. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  126. writel(BIT(bit_pos),
  127. ks_pcie->va_app_base + MSI0_IRQ_ENABLE_CLR + (reg_offset << 4));
  128. }
  129. static void ks_dw_pcie_msi_irq_mask(struct irq_data *d)
  130. {
  131. struct keystone_pcie *ks_pcie;
  132. struct msi_desc *msi;
  133. struct pcie_port *pp;
  134. u32 offset;
  135. msi = irq_data_get_msi_desc(d);
  136. pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
  137. ks_pcie = to_keystone_pcie(pp);
  138. offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
  139. /* Mask the end point if PVM implemented */
  140. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  141. if (msi->msi_attrib.maskbit)
  142. pci_msi_mask_irq(d);
  143. }
  144. ks_dw_pcie_msi_clear_irq(pp, offset);
  145. }
  146. static void ks_dw_pcie_msi_irq_unmask(struct irq_data *d)
  147. {
  148. struct keystone_pcie *ks_pcie;
  149. struct msi_desc *msi;
  150. struct pcie_port *pp;
  151. u32 offset;
  152. msi = irq_data_get_msi_desc(d);
  153. pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
  154. ks_pcie = to_keystone_pcie(pp);
  155. offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
  156. /* Mask the end point if PVM implemented */
  157. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  158. if (msi->msi_attrib.maskbit)
  159. pci_msi_unmask_irq(d);
  160. }
  161. ks_dw_pcie_msi_set_irq(pp, offset);
  162. }
  163. static struct irq_chip ks_dw_pcie_msi_irq_chip = {
  164. .name = "Keystone-PCIe-MSI-IRQ",
  165. .irq_ack = ks_dw_pcie_msi_irq_ack,
  166. .irq_mask = ks_dw_pcie_msi_irq_mask,
  167. .irq_unmask = ks_dw_pcie_msi_irq_unmask,
  168. };
  169. static int ks_dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  170. irq_hw_number_t hwirq)
  171. {
  172. irq_set_chip_and_handler(irq, &ks_dw_pcie_msi_irq_chip,
  173. handle_level_irq);
  174. irq_set_chip_data(irq, domain->host_data);
  175. return 0;
  176. }
  177. static const struct irq_domain_ops ks_dw_pcie_msi_domain_ops = {
  178. .map = ks_dw_pcie_msi_map,
  179. };
  180. int ks_dw_pcie_msi_host_init(struct pcie_port *pp, struct msi_controller *chip)
  181. {
  182. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  183. int i;
  184. pp->irq_domain = irq_domain_add_linear(ks_pcie->msi_intc_np,
  185. MAX_MSI_IRQS,
  186. &ks_dw_pcie_msi_domain_ops,
  187. chip);
  188. if (!pp->irq_domain) {
  189. dev_err(pp->dev, "irq domain init failed\n");
  190. return -ENXIO;
  191. }
  192. for (i = 0; i < MAX_MSI_IRQS; i++)
  193. irq_create_mapping(pp->irq_domain, i);
  194. return 0;
  195. }
  196. void ks_dw_pcie_enable_legacy_irqs(struct keystone_pcie *ks_pcie)
  197. {
  198. int i;
  199. for (i = 0; i < MAX_LEGACY_IRQS; i++)
  200. writel(0x1, ks_pcie->va_app_base + IRQ_ENABLE_SET + (i << 4));
  201. }
  202. void ks_dw_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie, int offset)
  203. {
  204. struct pcie_port *pp = &ks_pcie->pp;
  205. u32 pending;
  206. int virq;
  207. pending = readl(ks_pcie->va_app_base + IRQ_STATUS + (offset << 4));
  208. if (BIT(0) & pending) {
  209. virq = irq_linear_revmap(ks_pcie->legacy_irq_domain, offset);
  210. dev_dbg(pp->dev, ": irq: irq_offset %d, virq %d\n", offset,
  211. virq);
  212. generic_handle_irq(virq);
  213. }
  214. /* EOI the INTx interrupt */
  215. writel(offset, ks_pcie->va_app_base + IRQ_EOI);
  216. }
  217. void ks_dw_pcie_enable_error_irq(void __iomem *reg_base)
  218. {
  219. writel(ERR_IRQ_ALL, reg_base + ERR_IRQ_ENABLE_SET);
  220. }
  221. irqreturn_t ks_dw_pcie_handle_error_irq(struct device *dev,
  222. void __iomem *reg_base)
  223. {
  224. u32 status;
  225. status = readl(reg_base + ERR_IRQ_STATUS_RAW) & ERR_IRQ_ALL;
  226. if (!status)
  227. return IRQ_NONE;
  228. if (status & ERR_FATAL_IRQ)
  229. dev_err(dev, "fatal error (status %#010x)\n", status);
  230. /* Ack the IRQ; status bits are RW1C */
  231. writel(status, reg_base + ERR_IRQ_STATUS);
  232. return IRQ_HANDLED;
  233. }
  234. static void ks_dw_pcie_ack_legacy_irq(struct irq_data *d)
  235. {
  236. }
  237. static void ks_dw_pcie_mask_legacy_irq(struct irq_data *d)
  238. {
  239. }
  240. static void ks_dw_pcie_unmask_legacy_irq(struct irq_data *d)
  241. {
  242. }
  243. static struct irq_chip ks_dw_pcie_legacy_irq_chip = {
  244. .name = "Keystone-PCI-Legacy-IRQ",
  245. .irq_ack = ks_dw_pcie_ack_legacy_irq,
  246. .irq_mask = ks_dw_pcie_mask_legacy_irq,
  247. .irq_unmask = ks_dw_pcie_unmask_legacy_irq,
  248. };
  249. static int ks_dw_pcie_init_legacy_irq_map(struct irq_domain *d,
  250. unsigned int irq, irq_hw_number_t hw_irq)
  251. {
  252. irq_set_chip_and_handler(irq, &ks_dw_pcie_legacy_irq_chip,
  253. handle_level_irq);
  254. irq_set_chip_data(irq, d->host_data);
  255. return 0;
  256. }
  257. static const struct irq_domain_ops ks_dw_pcie_legacy_irq_domain_ops = {
  258. .map = ks_dw_pcie_init_legacy_irq_map,
  259. .xlate = irq_domain_xlate_onetwocell,
  260. };
  261. /**
  262. * ks_dw_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
  263. * registers
  264. *
  265. * Since modification of dbi_cs2 involves different clock domain, read the
  266. * status back to ensure the transition is complete.
  267. */
  268. static void ks_dw_pcie_set_dbi_mode(void __iomem *reg_virt)
  269. {
  270. u32 val;
  271. writel(DBI_CS2_EN_VAL | readl(reg_virt + CMD_STATUS),
  272. reg_virt + CMD_STATUS);
  273. do {
  274. val = readl(reg_virt + CMD_STATUS);
  275. } while (!(val & DBI_CS2_EN_VAL));
  276. }
  277. /**
  278. * ks_dw_pcie_clear_dbi_mode() - Disable DBI mode
  279. *
  280. * Since modification of dbi_cs2 involves different clock domain, read the
  281. * status back to ensure the transition is complete.
  282. */
  283. static void ks_dw_pcie_clear_dbi_mode(void __iomem *reg_virt)
  284. {
  285. u32 val;
  286. writel(~DBI_CS2_EN_VAL & readl(reg_virt + CMD_STATUS),
  287. reg_virt + CMD_STATUS);
  288. do {
  289. val = readl(reg_virt + CMD_STATUS);
  290. } while (val & DBI_CS2_EN_VAL);
  291. }
  292. void ks_dw_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
  293. {
  294. struct pcie_port *pp = &ks_pcie->pp;
  295. u32 start = pp->mem->start, end = pp->mem->end;
  296. int i, tr_size;
  297. /* Disable BARs for inbound access */
  298. ks_dw_pcie_set_dbi_mode(ks_pcie->va_app_base);
  299. writel(0, pp->dbi_base + PCI_BASE_ADDRESS_0);
  300. writel(0, pp->dbi_base + PCI_BASE_ADDRESS_1);
  301. ks_dw_pcie_clear_dbi_mode(ks_pcie->va_app_base);
  302. /* Set outbound translation size per window division */
  303. writel(CFG_PCIM_WIN_SZ_IDX & 0x7, ks_pcie->va_app_base + OB_SIZE);
  304. tr_size = (1 << (CFG_PCIM_WIN_SZ_IDX & 0x7)) * SZ_1M;
  305. /* Using Direct 1:1 mapping of RC <-> PCI memory space */
  306. for (i = 0; (i < CFG_PCIM_WIN_CNT) && (start < end); i++) {
  307. writel(start | 1, ks_pcie->va_app_base + OB_OFFSET_INDEX(i));
  308. writel(0, ks_pcie->va_app_base + OB_OFFSET_HI(i));
  309. start += tr_size;
  310. }
  311. /* Enable OB translation */
  312. writel(OB_XLAT_EN_VAL | readl(ks_pcie->va_app_base + CMD_STATUS),
  313. ks_pcie->va_app_base + CMD_STATUS);
  314. }
  315. /**
  316. * ks_pcie_cfg_setup() - Set up configuration space address for a device
  317. *
  318. * @ks_pcie: ptr to keystone_pcie structure
  319. * @bus: Bus number the device is residing on
  320. * @devfn: device, function number info
  321. *
  322. * Forms and returns the address of configuration space mapped in PCIESS
  323. * address space 0. Also configures CFG_SETUP for remote configuration space
  324. * access.
  325. *
  326. * The address space has two regions to access configuration - local and remote.
  327. * We access local region for bus 0 (as RC is attached on bus 0) and remote
  328. * region for others with TYPE 1 access when bus > 1. As for device on bus = 1,
  329. * we will do TYPE 0 access as it will be on our secondary bus (logical).
  330. * CFG_SETUP is needed only for remote configuration access.
  331. */
  332. static void __iomem *ks_pcie_cfg_setup(struct keystone_pcie *ks_pcie, u8 bus,
  333. unsigned int devfn)
  334. {
  335. u8 device = PCI_SLOT(devfn), function = PCI_FUNC(devfn);
  336. struct pcie_port *pp = &ks_pcie->pp;
  337. u32 regval;
  338. if (bus == 0)
  339. return pp->dbi_base;
  340. regval = (bus << 16) | (device << 8) | function;
  341. /*
  342. * Since Bus#1 will be a virtual bus, we need to have TYPE0
  343. * access only.
  344. * TYPE 1
  345. */
  346. if (bus != 1)
  347. regval |= BIT(24);
  348. writel(regval, ks_pcie->va_app_base + CFG_SETUP);
  349. return pp->va_cfg0_base;
  350. }
  351. int ks_dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  352. unsigned int devfn, int where, int size, u32 *val)
  353. {
  354. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  355. u8 bus_num = bus->number;
  356. void __iomem *addr;
  357. addr = ks_pcie_cfg_setup(ks_pcie, bus_num, devfn);
  358. return dw_pcie_cfg_read(addr + where, size, val);
  359. }
  360. int ks_dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  361. unsigned int devfn, int where, int size, u32 val)
  362. {
  363. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  364. u8 bus_num = bus->number;
  365. void __iomem *addr;
  366. addr = ks_pcie_cfg_setup(ks_pcie, bus_num, devfn);
  367. return dw_pcie_cfg_write(addr + where, size, val);
  368. }
  369. /**
  370. * ks_dw_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
  371. *
  372. * This sets BAR0 to enable inbound access for MSI_IRQ register
  373. */
  374. void ks_dw_pcie_v3_65_scan_bus(struct pcie_port *pp)
  375. {
  376. struct keystone_pcie *ks_pcie = to_keystone_pcie(pp);
  377. /* Configure and set up BAR0 */
  378. ks_dw_pcie_set_dbi_mode(ks_pcie->va_app_base);
  379. /* Enable BAR0 */
  380. writel(1, pp->dbi_base + PCI_BASE_ADDRESS_0);
  381. writel(SZ_4K - 1, pp->dbi_base + PCI_BASE_ADDRESS_0);
  382. ks_dw_pcie_clear_dbi_mode(ks_pcie->va_app_base);
  383. /*
  384. * For BAR0, just setting bus address for inbound writes (MSI) should
  385. * be sufficient. Use physical address to avoid any conflicts.
  386. */
  387. writel(ks_pcie->app.start, pp->dbi_base + PCI_BASE_ADDRESS_0);
  388. }
  389. /**
  390. * ks_dw_pcie_link_up() - Check if link up
  391. */
  392. int ks_dw_pcie_link_up(struct pcie_port *pp)
  393. {
  394. u32 val = readl(pp->dbi_base + DEBUG0);
  395. return (val & LTSSM_STATE_MASK) == LTSSM_STATE_L0;
  396. }
  397. void ks_dw_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
  398. {
  399. u32 val;
  400. /* Disable Link training */
  401. val = readl(ks_pcie->va_app_base + CMD_STATUS);
  402. val &= ~LTSSM_EN_VAL;
  403. writel(LTSSM_EN_VAL | val, ks_pcie->va_app_base + CMD_STATUS);
  404. /* Initiate Link Training */
  405. val = readl(ks_pcie->va_app_base + CMD_STATUS);
  406. writel(LTSSM_EN_VAL | val, ks_pcie->va_app_base + CMD_STATUS);
  407. }
  408. /**
  409. * ks_dw_pcie_host_init() - initialize host for v3_65 dw hardware
  410. *
  411. * Ioremap the register resources, initialize legacy irq domain
  412. * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
  413. * PCI host controller.
  414. */
  415. int __init ks_dw_pcie_host_init(struct keystone_pcie *ks_pcie,
  416. struct device_node *msi_intc_np)
  417. {
  418. struct pcie_port *pp = &ks_pcie->pp;
  419. struct platform_device *pdev = to_platform_device(pp->dev);
  420. struct resource *res;
  421. /* Index 0 is the config reg. space address */
  422. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  423. pp->dbi_base = devm_ioremap_resource(pp->dev, res);
  424. if (IS_ERR(pp->dbi_base))
  425. return PTR_ERR(pp->dbi_base);
  426. /*
  427. * We set these same and is used in pcie rd/wr_other_conf
  428. * functions
  429. */
  430. pp->va_cfg0_base = pp->dbi_base + SPACE0_REMOTE_CFG_OFFSET;
  431. pp->va_cfg1_base = pp->va_cfg0_base;
  432. /* Index 1 is the application reg. space address */
  433. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  434. ks_pcie->va_app_base = devm_ioremap_resource(pp->dev, res);
  435. if (IS_ERR(ks_pcie->va_app_base))
  436. return PTR_ERR(ks_pcie->va_app_base);
  437. ks_pcie->app = *res;
  438. /* Create legacy IRQ domain */
  439. ks_pcie->legacy_irq_domain =
  440. irq_domain_add_linear(ks_pcie->legacy_intc_np,
  441. MAX_LEGACY_IRQS,
  442. &ks_dw_pcie_legacy_irq_domain_ops,
  443. NULL);
  444. if (!ks_pcie->legacy_irq_domain) {
  445. dev_err(pp->dev, "Failed to add irq domain for legacy irqs\n");
  446. return -EINVAL;
  447. }
  448. return dw_pcie_host_init(pp);
  449. }