pci-keystone-dw.c 13 KB

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