pci-keystone-dw.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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/delay.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/irqreturn.h>
  18. #include <linux/module.h>
  19. #include <linux/of.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/pci.h>
  22. #include <linux/platform_device.h>
  23. #include "pcie-designware.h"
  24. #include "pci-keystone.h"
  25. /* Application register defines */
  26. #define LTSSM_EN_VAL 1
  27. #define LTSSM_STATE_MASK 0x1f
  28. #define LTSSM_STATE_L0 0x11
  29. #define DBI_CS2_EN_VAL 0x20
  30. #define OB_XLAT_EN_VAL 2
  31. #define IB_XLAT_EN_VAL 4
  32. /* Application registers */
  33. #define CMD_STATUS 0x004
  34. #define CFG_SETUP 0x008
  35. #define OB_SIZE 0x030
  36. #define CFG_PCIM_WIN_SZ_IDX 3
  37. #define CFG_PCIM_WIN_CNT 32
  38. #define SPACE0_REMOTE_CFG_OFFSET 0x1000
  39. #define OB_OFFSET_INDEX(n) (0x200 + (8 * n))
  40. #define OB_OFFSET_HI(n) (0x204 + (8 * n))
  41. #define IB_BAR(n) (0x300 + (0x10 * (n)))
  42. #define IB_START_LO(n) (0x304 + (0x10 * (n)))
  43. #define IB_START_HI(n) (0x308 + (0x10 * (n)))
  44. #define IB_OFFSET(n) (0x30c + (0x10 * (n)))
  45. /* IRQ register defines */
  46. #define IRQ_EOI 0x050
  47. #define IRQ_STATUS 0x184
  48. #define IRQ_ENABLE_SET 0x188
  49. #define IRQ_ENABLE_CLR 0x18c
  50. #define MSI_IRQ 0x054
  51. #define MSI0_IRQ_STATUS 0x104
  52. #define MSI0_IRQ_ENABLE_SET 0x108
  53. #define MSI0_IRQ_ENABLE_CLR 0x10c
  54. #define IRQ_STATUS 0x184
  55. #define MSI_IRQ_OFFSET 4
  56. /* Error IRQ bits */
  57. #define ERR_AER BIT(5) /* ECRC error */
  58. #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */
  59. #define ERR_CORR BIT(3) /* Correctable error */
  60. #define ERR_NONFATAL BIT(2) /* Non-fatal error */
  61. #define ERR_FATAL BIT(1) /* Fatal error */
  62. #define ERR_SYS BIT(0) /* System (fatal, non-fatal, or correctable) */
  63. #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \
  64. ERR_NONFATAL | ERR_FATAL | ERR_SYS)
  65. #define ERR_FATAL_IRQ (ERR_FATAL | ERR_AXI)
  66. #define ERR_IRQ_STATUS_RAW 0x1c0
  67. #define ERR_IRQ_STATUS 0x1c4
  68. #define ERR_IRQ_ENABLE_SET 0x1c8
  69. #define ERR_IRQ_ENABLE_CLR 0x1cc
  70. /* Config space registers */
  71. #define DEBUG0 0x728
  72. #define APP_ADDR_SPACE_0 (16 * SZ_1K)
  73. #define WIN_INDEX_MASK 0x1f
  74. #define WIN_INDEX_SHIFT 20
  75. #define WIN_SIZE SZ_1M
  76. #define PCIE_LEGACY_IRQ_ENABLE_SET(n) (0x188 + (0x10 * ((n) - 1)))
  77. #define PCIE_LEGACY_IRQ_ENABLE_CLR(n) (0x18c + (0x10 * ((n) - 1)))
  78. #define PCIE_EP_IRQ_SET 0x64
  79. #define PCIE_EP_IRQ_CLR 0x68
  80. #define OB_ENABLEN 0x1
  81. #define INT_ENABLE 0x1
  82. #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
  83. static void ks_dw_app_writel(struct keystone_pcie *ks_pcie, u32 offset,
  84. u32 val);
  85. static u32 ks_dw_app_readl(struct keystone_pcie *ks_pcie, u32 offset);
  86. static void ks_dw_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie);
  87. static void ks_dw_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie);
  88. void ks_dw_pcie_ep_init(struct dw_pcie_ep *ep)
  89. {
  90. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  91. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  92. int flags;
  93. u32 val;
  94. ep->page_size = WIN_SIZE;
  95. flags = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32;
  96. dw_pcie_writel_dbi2(pci, PCI_BASE_ADDRESS_0, APP_ADDR_SPACE_0 - 1);
  97. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, flags);
  98. ks_dw_app_writel(ks_pcie, OB_SIZE, 0x0);
  99. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  100. ks_dw_app_writel(ks_pcie, CMD_STATUS, val | OB_XLAT_EN_VAL |
  101. IB_XLAT_EN_VAL);
  102. }
  103. void ks_dw_pcie_disable_outbound_atu(struct keystone_pcie *ks_pcie,
  104. phys_addr_t addr)
  105. {
  106. u8 index;
  107. u8 regions;
  108. u64 cpu_addr;
  109. index = (addr >> WIN_INDEX_SHIFT) & WIN_INDEX_MASK;
  110. regions = ks_pcie->ob_win[index].no_of_regions;
  111. cpu_addr = ks_pcie->ob_win[index].cpu_addr;
  112. WARN_ON(cpu_addr != addr);
  113. while (regions--) {
  114. ks_dw_app_writel(ks_pcie, OB_OFFSET_INDEX(index), 0x0);
  115. clear_bit(index++, &ks_pcie->ob_window_map);
  116. }
  117. }
  118. void ks_dw_pcie_disable_inbound_atu(struct keystone_pcie *ks_pcie, int index)
  119. {
  120. ks_dw_app_writel(ks_pcie, IB_BAR(index), 0x0);
  121. ks_dw_app_writel(ks_pcie, IB_START_LO(index), 0x0);
  122. ks_dw_app_writel(ks_pcie, IB_START_HI(index), 0x0);
  123. ks_dw_app_writel(ks_pcie, IB_OFFSET(index), 0x0);
  124. }
  125. void ks_dw_pcie_disable_atu(struct dw_pcie *pci, phys_addr_t addr, int index,
  126. enum dw_pcie_region_type type)
  127. {
  128. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  129. switch (type) {
  130. case DW_PCIE_REGION_INBOUND:
  131. ks_dw_pcie_disable_inbound_atu(ks_pcie, index);
  132. break;
  133. case DW_PCIE_REGION_OUTBOUND:
  134. ks_dw_pcie_disable_outbound_atu(ks_pcie, addr);
  135. break;
  136. default:
  137. return;
  138. }
  139. }
  140. int ks_dw_pcie_inbound_atu(struct dw_pcie *pci, u32 index,
  141. enum pci_barno bar, dma_addr_t cpu_addr)
  142. {
  143. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  144. struct device *dev = pci->dev;
  145. if (bar == BAR_0) {
  146. dev_err(dev, "BAR_0 is reserved\n");
  147. return -EINVAL;
  148. }
  149. ks_dw_app_writel(ks_pcie, IB_BAR(index), bar);
  150. ks_dw_app_writel(ks_pcie, IB_OFFSET(index), lower_32_bits(cpu_addr));
  151. return 0;
  152. }
  153. static int ks_dw_pcie_check_free(struct keystone_pcie *ks_pcie, u8 index,
  154. u8 regions)
  155. {
  156. while (regions--) {
  157. if (test_bit(index++, &ks_pcie->ob_window_map))
  158. return false;
  159. }
  160. return true;
  161. }
  162. int ks_dw_pcie_outbound_atu(struct dw_pcie *pci, u64 cpu_addr, u64 pci_addr,
  163. size_t size)
  164. {
  165. u8 index;
  166. u8 regions;
  167. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  168. index = (cpu_addr >> WIN_INDEX_SHIFT) & WIN_INDEX_MASK;
  169. regions = ((size - 1) >> WIN_INDEX_SHIFT) + 1;
  170. if (!ks_dw_pcie_check_free(ks_pcie, index, regions))
  171. return -ENOMEM;
  172. if (index + regions > OUTBOUND_WINDOWS)
  173. return -ENOMEM;
  174. ks_pcie->ob_win[index].cpu_addr = cpu_addr;
  175. ks_pcie->ob_win[index].no_of_regions = regions;
  176. while (regions--) {
  177. ks_dw_app_writel(ks_pcie, OB_OFFSET_INDEX(index),
  178. lower_32_bits(pci_addr) | OB_ENABLEN);
  179. ks_dw_app_writel(ks_pcie, OB_OFFSET_HI(index),
  180. upper_32_bits(pci_addr));
  181. set_bit(index++, &ks_pcie->ob_window_map);
  182. pci_addr += WIN_SIZE;
  183. }
  184. return 0;
  185. }
  186. void ks_dw_pcie_raise_legacy_irq(struct keystone_pcie *ks_pcie)
  187. {
  188. struct dw_pcie *pci = ks_pcie->pci;
  189. u8 int_pin;
  190. int_pin = dw_pcie_readb_dbi(pci, PCI_INTERRUPT_PIN);
  191. if (int_pin == 0 || int_pin > 4)
  192. return;
  193. ks_dw_app_writel(ks_pcie, PCIE_LEGACY_IRQ_ENABLE_SET(int_pin),
  194. INT_ENABLE);
  195. ks_dw_app_writel(ks_pcie, PCIE_EP_IRQ_SET, INT_ENABLE);
  196. mdelay(1);
  197. ks_dw_app_writel(ks_pcie, PCIE_EP_IRQ_CLR, INT_ENABLE);
  198. ks_dw_app_writel(ks_pcie, PCIE_LEGACY_IRQ_ENABLE_CLR(int_pin),
  199. INT_ENABLE);
  200. }
  201. u32 ks_dw_pcie_read_dbi2(struct dw_pcie *pci, void __iomem *base,
  202. u32 reg, size_t size)
  203. {
  204. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  205. u32 val;
  206. ks_dw_pcie_set_dbi_mode(ks_pcie);
  207. dw_pcie_read(base + reg, size, &val);
  208. ks_dw_pcie_clear_dbi_mode(ks_pcie);
  209. return val;
  210. }
  211. void ks_dw_pcie_write_dbi2(struct dw_pcie *pci, void __iomem *base,
  212. u32 reg, size_t size, u32 val)
  213. {
  214. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  215. ks_dw_pcie_set_dbi_mode(ks_pcie);
  216. dw_pcie_write(base + reg, size, val);
  217. ks_dw_pcie_clear_dbi_mode(ks_pcie);
  218. }
  219. static inline void update_reg_offset_bit_pos(u32 offset, u32 *reg_offset,
  220. u32 *bit_pos)
  221. {
  222. *reg_offset = offset % 8;
  223. *bit_pos = offset >> 3;
  224. }
  225. phys_addr_t ks_dw_pcie_get_msi_addr(struct pcie_port *pp)
  226. {
  227. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  228. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  229. return ks_pcie->app.start + MSI_IRQ;
  230. }
  231. static u32 ks_dw_app_readl(struct keystone_pcie *ks_pcie, u32 offset)
  232. {
  233. return readl(ks_pcie->va_app_base + offset);
  234. }
  235. static void ks_dw_app_writel(struct keystone_pcie *ks_pcie, u32 offset, u32 val)
  236. {
  237. writel(val, ks_pcie->va_app_base + offset);
  238. }
  239. void ks_dw_pcie_handle_msi_irq(struct keystone_pcie *ks_pcie, int offset)
  240. {
  241. struct dw_pcie *pci = ks_pcie->pci;
  242. struct pcie_port *pp = &pci->pp;
  243. struct device *dev = pci->dev;
  244. u32 pending, vector;
  245. int src, virq;
  246. pending = ks_dw_app_readl(ks_pcie, MSI0_IRQ_STATUS + (offset << 4));
  247. /*
  248. * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit
  249. * shows 1, 9, 17, 25 and so forth
  250. */
  251. for (src = 0; src < 4; src++) {
  252. if (BIT(src) & pending) {
  253. vector = offset + (src << 3);
  254. virq = irq_linear_revmap(pp->irq_domain, vector);
  255. dev_dbg(dev, "irq: bit %d, vector %d, virq %d\n",
  256. src, vector, virq);
  257. generic_handle_irq(virq);
  258. }
  259. }
  260. }
  261. static void ks_dw_pcie_msi_irq_ack(struct irq_data *d)
  262. {
  263. u32 offset, reg_offset, bit_pos;
  264. struct keystone_pcie *ks_pcie;
  265. struct msi_desc *msi;
  266. struct pcie_port *pp;
  267. struct dw_pcie *pci;
  268. msi = irq_data_get_msi_desc(d);
  269. pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
  270. pci = to_dw_pcie_from_pp(pp);
  271. ks_pcie = to_keystone_pcie(pci);
  272. offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
  273. update_reg_offset_bit_pos(offset, &reg_offset, &bit_pos);
  274. ks_dw_app_writel(ks_pcie, MSI0_IRQ_STATUS + (reg_offset << 4),
  275. BIT(bit_pos));
  276. ks_dw_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET);
  277. }
  278. void ks_dw_pcie_msi_set_irq(struct pcie_port *pp, int irq)
  279. {
  280. u32 reg_offset, bit_pos;
  281. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  282. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  283. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  284. ks_dw_app_writel(ks_pcie, MSI0_IRQ_ENABLE_SET + (reg_offset << 4),
  285. BIT(bit_pos));
  286. }
  287. void ks_dw_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
  288. {
  289. u32 reg_offset, bit_pos;
  290. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  291. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  292. update_reg_offset_bit_pos(irq, &reg_offset, &bit_pos);
  293. ks_dw_app_writel(ks_pcie, MSI0_IRQ_ENABLE_CLR + (reg_offset << 4),
  294. BIT(bit_pos));
  295. }
  296. static void ks_dw_pcie_msi_irq_mask(struct irq_data *d)
  297. {
  298. struct keystone_pcie *ks_pcie;
  299. struct msi_desc *msi;
  300. struct pcie_port *pp;
  301. struct dw_pcie *pci;
  302. u32 offset;
  303. msi = irq_data_get_msi_desc(d);
  304. pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
  305. pci = to_dw_pcie_from_pp(pp);
  306. ks_pcie = to_keystone_pcie(pci);
  307. offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
  308. /* Mask the end point if PVM implemented */
  309. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  310. if (msi->msi_attrib.maskbit)
  311. pci_msi_mask_irq(d);
  312. }
  313. ks_dw_pcie_msi_clear_irq(pp, offset);
  314. }
  315. static void ks_dw_pcie_msi_irq_unmask(struct irq_data *d)
  316. {
  317. struct keystone_pcie *ks_pcie;
  318. struct msi_desc *msi;
  319. struct pcie_port *pp;
  320. struct dw_pcie *pci;
  321. u32 offset;
  322. msi = irq_data_get_msi_desc(d);
  323. pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
  324. pci = to_dw_pcie_from_pp(pp);
  325. ks_pcie = to_keystone_pcie(pci);
  326. offset = d->irq - irq_linear_revmap(pp->irq_domain, 0);
  327. /* Mask the end point if PVM implemented */
  328. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  329. if (msi->msi_attrib.maskbit)
  330. pci_msi_unmask_irq(d);
  331. }
  332. ks_dw_pcie_msi_set_irq(pp, offset);
  333. }
  334. static struct irq_chip ks_dw_pcie_msi_irq_chip = {
  335. .name = "Keystone-PCIe-MSI-IRQ",
  336. .irq_ack = ks_dw_pcie_msi_irq_ack,
  337. .irq_mask = ks_dw_pcie_msi_irq_mask,
  338. .irq_unmask = ks_dw_pcie_msi_irq_unmask,
  339. };
  340. static int ks_dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  341. irq_hw_number_t hwirq)
  342. {
  343. irq_set_chip_and_handler(irq, &ks_dw_pcie_msi_irq_chip,
  344. handle_level_irq);
  345. irq_set_chip_data(irq, domain->host_data);
  346. return 0;
  347. }
  348. static const struct irq_domain_ops ks_dw_pcie_msi_domain_ops = {
  349. .map = ks_dw_pcie_msi_map,
  350. };
  351. int ks_dw_pcie_msi_host_init(struct pcie_port *pp, struct msi_controller *chip)
  352. {
  353. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  354. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  355. struct device *dev = pci->dev;
  356. int i;
  357. pp->irq_domain = irq_domain_add_linear(ks_pcie->msi_intc_np,
  358. MAX_MSI_IRQS,
  359. &ks_dw_pcie_msi_domain_ops,
  360. chip);
  361. if (!pp->irq_domain) {
  362. dev_err(dev, "irq domain init failed\n");
  363. return -ENXIO;
  364. }
  365. for (i = 0; i < MAX_MSI_IRQS; i++)
  366. irq_create_mapping(pp->irq_domain, i);
  367. return 0;
  368. }
  369. void ks_dw_pcie_enable_legacy_irqs(struct keystone_pcie *ks_pcie)
  370. {
  371. int i;
  372. for (i = 0; i < MAX_LEGACY_IRQS; i++)
  373. ks_dw_app_writel(ks_pcie, IRQ_ENABLE_SET + (i << 4), 0x1);
  374. }
  375. void ks_dw_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie, int offset)
  376. {
  377. struct dw_pcie *pci = ks_pcie->pci;
  378. struct device *dev = pci->dev;
  379. u32 pending;
  380. int virq;
  381. pending = ks_dw_app_readl(ks_pcie, IRQ_STATUS + (offset << 4));
  382. if (BIT(0) & pending) {
  383. virq = irq_linear_revmap(ks_pcie->legacy_irq_domain, offset);
  384. dev_dbg(dev, ": irq: irq_offset %d, virq %d\n", offset, virq);
  385. generic_handle_irq(virq);
  386. }
  387. /* EOI the INTx interrupt */
  388. ks_dw_app_writel(ks_pcie, IRQ_EOI, offset);
  389. }
  390. void ks_dw_pcie_enable_error_irq(struct keystone_pcie *ks_pcie)
  391. {
  392. ks_dw_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL);
  393. }
  394. irqreturn_t ks_dw_pcie_handle_error_irq(struct keystone_pcie *ks_pcie)
  395. {
  396. u32 status;
  397. status = ks_dw_app_readl(ks_pcie, ERR_IRQ_STATUS_RAW) & ERR_IRQ_ALL;
  398. if (!status)
  399. return IRQ_NONE;
  400. if (status & ERR_FATAL_IRQ)
  401. dev_err(ks_pcie->pci->dev, "fatal error (status %#010x)\n",
  402. status);
  403. /* Ack the IRQ; status bits are RW1C */
  404. ks_dw_app_writel(ks_pcie, ERR_IRQ_STATUS, status);
  405. return IRQ_HANDLED;
  406. }
  407. static void ks_dw_pcie_ack_legacy_irq(struct irq_data *d)
  408. {
  409. }
  410. static void ks_dw_pcie_mask_legacy_irq(struct irq_data *d)
  411. {
  412. }
  413. static void ks_dw_pcie_unmask_legacy_irq(struct irq_data *d)
  414. {
  415. }
  416. static struct irq_chip ks_dw_pcie_legacy_irq_chip = {
  417. .name = "Keystone-PCI-Legacy-IRQ",
  418. .irq_ack = ks_dw_pcie_ack_legacy_irq,
  419. .irq_mask = ks_dw_pcie_mask_legacy_irq,
  420. .irq_unmask = ks_dw_pcie_unmask_legacy_irq,
  421. };
  422. static int ks_dw_pcie_init_legacy_irq_map(struct irq_domain *d,
  423. unsigned int irq, irq_hw_number_t hw_irq)
  424. {
  425. irq_set_chip_and_handler(irq, &ks_dw_pcie_legacy_irq_chip,
  426. handle_level_irq);
  427. irq_set_chip_data(irq, d->host_data);
  428. return 0;
  429. }
  430. static const struct irq_domain_ops ks_dw_pcie_legacy_irq_domain_ops = {
  431. .map = ks_dw_pcie_init_legacy_irq_map,
  432. .xlate = irq_domain_xlate_onetwocell,
  433. };
  434. /**
  435. * ks_dw_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask
  436. * registers
  437. *
  438. * Since modification of dbi_cs2 involves different clock domain, read the
  439. * status back to ensure the transition is complete.
  440. */
  441. static void ks_dw_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie)
  442. {
  443. u32 val;
  444. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  445. ks_dw_app_writel(ks_pcie, CMD_STATUS, DBI_CS2_EN_VAL | val);
  446. do {
  447. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  448. } while (!(val & DBI_CS2_EN_VAL));
  449. }
  450. /**
  451. * ks_dw_pcie_clear_dbi_mode() - Disable DBI mode
  452. *
  453. * Since modification of dbi_cs2 involves different clock domain, read the
  454. * status back to ensure the transition is complete.
  455. */
  456. static void ks_dw_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie)
  457. {
  458. u32 val;
  459. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  460. ks_dw_app_writel(ks_pcie, CMD_STATUS, ~DBI_CS2_EN_VAL & val);
  461. do {
  462. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  463. } while (val & DBI_CS2_EN_VAL);
  464. }
  465. void ks_dw_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie)
  466. {
  467. struct dw_pcie *pci = ks_pcie->pci;
  468. struct pcie_port *pp = &pci->pp;
  469. u32 start = pp->mem->start, end = pp->mem->end;
  470. int i, tr_size;
  471. u32 val;
  472. /* Disable BARs for inbound access */
  473. ks_dw_pcie_set_dbi_mode(ks_pcie);
  474. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
  475. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0);
  476. ks_dw_pcie_clear_dbi_mode(ks_pcie);
  477. /* Set outbound translation size per window division */
  478. ks_dw_app_writel(ks_pcie, OB_SIZE, CFG_PCIM_WIN_SZ_IDX & 0x7);
  479. tr_size = (1 << (CFG_PCIM_WIN_SZ_IDX & 0x7)) * SZ_1M;
  480. /* Using Direct 1:1 mapping of RC <-> PCI memory space */
  481. for (i = 0; (i < CFG_PCIM_WIN_CNT) && (start < end); i++) {
  482. ks_dw_app_writel(ks_pcie, OB_OFFSET_INDEX(i), start | 1);
  483. ks_dw_app_writel(ks_pcie, OB_OFFSET_HI(i), 0);
  484. start += tr_size;
  485. }
  486. /* Enable OB translation */
  487. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  488. ks_dw_app_writel(ks_pcie, CMD_STATUS, OB_XLAT_EN_VAL | val);
  489. }
  490. /**
  491. * ks_pcie_cfg_setup() - Set up configuration space address for a device
  492. *
  493. * @ks_pcie: ptr to keystone_pcie structure
  494. * @bus: Bus number the device is residing on
  495. * @devfn: device, function number info
  496. *
  497. * Forms and returns the address of configuration space mapped in PCIESS
  498. * address space 0. Also configures CFG_SETUP for remote configuration space
  499. * access.
  500. *
  501. * The address space has two regions to access configuration - local and remote.
  502. * We access local region for bus 0 (as RC is attached on bus 0) and remote
  503. * region for others with TYPE 1 access when bus > 1. As for device on bus = 1,
  504. * we will do TYPE 0 access as it will be on our secondary bus (logical).
  505. * CFG_SETUP is needed only for remote configuration access.
  506. */
  507. static void __iomem *ks_pcie_cfg_setup(struct keystone_pcie *ks_pcie, u8 bus,
  508. unsigned int devfn)
  509. {
  510. u8 device = PCI_SLOT(devfn), function = PCI_FUNC(devfn);
  511. struct dw_pcie *pci = ks_pcie->pci;
  512. struct pcie_port *pp = &pci->pp;
  513. u32 regval;
  514. if (bus == 0)
  515. return pci->dbi_base;
  516. regval = (bus << 16) | (device << 8) | function;
  517. /*
  518. * Since Bus#1 will be a virtual bus, we need to have TYPE0
  519. * access only.
  520. * TYPE 1
  521. */
  522. if (bus != 1)
  523. regval |= BIT(24);
  524. ks_dw_app_writel(ks_pcie, CFG_SETUP, regval);
  525. return pp->va_cfg0_base;
  526. }
  527. int ks_dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  528. unsigned int devfn, int where, int size, u32 *val)
  529. {
  530. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  531. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  532. u8 bus_num = bus->number;
  533. void __iomem *addr;
  534. addr = ks_pcie_cfg_setup(ks_pcie, bus_num, devfn);
  535. return dw_pcie_read(addr + where, size, val);
  536. }
  537. int ks_dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  538. unsigned int devfn, int where, int size, u32 val)
  539. {
  540. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  541. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  542. u8 bus_num = bus->number;
  543. void __iomem *addr;
  544. addr = ks_pcie_cfg_setup(ks_pcie, bus_num, devfn);
  545. return dw_pcie_write(addr + where, size, val);
  546. }
  547. /**
  548. * ks_dw_pcie_v3_65_scan_bus() - keystone scan_bus post initialization
  549. *
  550. * This sets BAR0 to enable inbound access for MSI_IRQ register
  551. */
  552. void ks_dw_pcie_v3_65_scan_bus(struct pcie_port *pp)
  553. {
  554. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  555. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  556. /* Configure and set up BAR0 */
  557. ks_dw_pcie_set_dbi_mode(ks_pcie);
  558. /* Enable BAR0 */
  559. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1);
  560. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1);
  561. ks_dw_pcie_clear_dbi_mode(ks_pcie);
  562. /*
  563. * For BAR0, just setting bus address for inbound writes (MSI) should
  564. * be sufficient. Use physical address to avoid any conflicts.
  565. */
  566. dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start);
  567. }
  568. /**
  569. * ks_dw_pcie_link_up() - Check if link up
  570. */
  571. int ks_dw_pcie_link_up(struct dw_pcie *pci)
  572. {
  573. u32 val;
  574. val = dw_pcie_readl_dbi(pci, DEBUG0);
  575. return (val & LTSSM_STATE_MASK) == LTSSM_STATE_L0;
  576. }
  577. void ks_dw_pcie_initiate_link_train(struct keystone_pcie *ks_pcie)
  578. {
  579. u32 val;
  580. /* Disable Link training */
  581. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  582. val &= ~LTSSM_EN_VAL;
  583. ks_dw_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
  584. /* Initiate Link Training */
  585. val = ks_dw_app_readl(ks_pcie, CMD_STATUS);
  586. ks_dw_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val);
  587. }
  588. /**
  589. * ks_dw_pcie_host_init() - initialize host for v3_65 dw hardware
  590. *
  591. * Ioremap the register resources, initialize legacy irq domain
  592. * and call dw_pcie_v3_65_host_init() API to initialize the Keystone
  593. * PCI host controller.
  594. */
  595. int __init ks_dw_pcie_host_init(struct keystone_pcie *ks_pcie,
  596. struct device_node *msi_intc_np)
  597. {
  598. struct dw_pcie *pci = ks_pcie->pci;
  599. struct pcie_port *pp = &pci->pp;
  600. struct device *dev = pci->dev;
  601. struct platform_device *pdev = to_platform_device(dev);
  602. struct resource *res;
  603. /* Index 0 is the config reg. space address */
  604. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  605. pci->dbi_base = devm_ioremap_resource(dev, res);
  606. if (IS_ERR(pci->dbi_base))
  607. return PTR_ERR(pci->dbi_base);
  608. /*
  609. * We set these same and is used in pcie rd/wr_other_conf
  610. * functions
  611. */
  612. pp->va_cfg0_base = pci->dbi_base + SPACE0_REMOTE_CFG_OFFSET;
  613. pp->va_cfg1_base = pp->va_cfg0_base;
  614. /* Create legacy IRQ domain */
  615. ks_pcie->legacy_irq_domain =
  616. irq_domain_add_linear(ks_pcie->legacy_intc_np,
  617. MAX_LEGACY_IRQS,
  618. &ks_dw_pcie_legacy_irq_domain_ops,
  619. NULL);
  620. if (!ks_pcie->legacy_irq_domain) {
  621. dev_err(dev, "Failed to add irq domain for legacy irqs\n");
  622. return -EINVAL;
  623. }
  624. return dw_pcie_host_init(pp);
  625. }