pci-keystone-dw.c 14 KB

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