pcie-mobiveil.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe host controller driver for Mobiveil PCIe Host controller
  4. *
  5. * Copyright (c) 2018 Mobiveil Inc.
  6. * Author: Subrahmanya Lingappa <l.subrahmanya@mobiveil.co.in>
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/irq.h>
  12. #include <linux/irqchip/chained_irq.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/msi.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/pci.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/slab.h>
  24. #include "../pci.h"
  25. /* register offsets and bit positions */
  26. /*
  27. * translation tables are grouped into windows, each window registers are
  28. * grouped into blocks of 4 or 16 registers each
  29. */
  30. #define PAB_REG_BLOCK_SIZE 16
  31. #define PAB_EXT_REG_BLOCK_SIZE 4
  32. #define PAB_REG_ADDR(offset, win) (offset + (win * PAB_REG_BLOCK_SIZE))
  33. #define PAB_EXT_REG_ADDR(offset, win) (offset + (win * PAB_EXT_REG_BLOCK_SIZE))
  34. #define LTSSM_STATUS 0x0404
  35. #define LTSSM_STATUS_L0_MASK 0x3f
  36. #define LTSSM_STATUS_L0 0x2d
  37. #define PAB_CTRL 0x0808
  38. #define AMBA_PIO_ENABLE_SHIFT 0
  39. #define PEX_PIO_ENABLE_SHIFT 1
  40. #define PAGE_SEL_SHIFT 13
  41. #define PAGE_SEL_MASK 0x3f
  42. #define PAGE_LO_MASK 0x3ff
  43. #define PAGE_SEL_EN 0xc00
  44. #define PAGE_SEL_OFFSET_SHIFT 10
  45. #define PAB_AXI_PIO_CTRL 0x0840
  46. #define APIO_EN_MASK 0xf
  47. #define PAB_PEX_PIO_CTRL 0x08c0
  48. #define PIO_ENABLE_SHIFT 0
  49. #define PAB_INTP_AMBA_MISC_ENB 0x0b0c
  50. #define PAB_INTP_AMBA_MISC_STAT 0x0b1c
  51. #define PAB_INTP_INTX_MASK 0x01e0
  52. #define PAB_INTP_MSI_MASK 0x8
  53. #define PAB_AXI_AMAP_CTRL(win) PAB_REG_ADDR(0x0ba0, win)
  54. #define WIN_ENABLE_SHIFT 0
  55. #define WIN_TYPE_SHIFT 1
  56. #define PAB_EXT_AXI_AMAP_SIZE(win) PAB_EXT_REG_ADDR(0xbaf0, win)
  57. #define PAB_AXI_AMAP_AXI_WIN(win) PAB_REG_ADDR(0x0ba4, win)
  58. #define AXI_WINDOW_ALIGN_MASK 3
  59. #define PAB_AXI_AMAP_PEX_WIN_L(win) PAB_REG_ADDR(0x0ba8, win)
  60. #define PAB_BUS_SHIFT 24
  61. #define PAB_DEVICE_SHIFT 19
  62. #define PAB_FUNCTION_SHIFT 16
  63. #define PAB_AXI_AMAP_PEX_WIN_H(win) PAB_REG_ADDR(0x0bac, win)
  64. #define PAB_INTP_AXI_PIO_CLASS 0x474
  65. #define PAB_PEX_AMAP_CTRL(win) PAB_REG_ADDR(0x4ba0, win)
  66. #define AMAP_CTRL_EN_SHIFT 0
  67. #define AMAP_CTRL_TYPE_SHIFT 1
  68. #define PAB_EXT_PEX_AMAP_SIZEN(win) PAB_EXT_REG_ADDR(0xbef0, win)
  69. #define PAB_PEX_AMAP_AXI_WIN(win) PAB_REG_ADDR(0x4ba4, win)
  70. #define PAB_PEX_AMAP_PEX_WIN_L(win) PAB_REG_ADDR(0x4ba8, win)
  71. #define PAB_PEX_AMAP_PEX_WIN_H(win) PAB_REG_ADDR(0x4bac, win)
  72. /* starting offset of INTX bits in status register */
  73. #define PAB_INTX_START 5
  74. /* supported number of MSI interrupts */
  75. #define PCI_NUM_MSI 16
  76. /* MSI registers */
  77. #define MSI_BASE_LO_OFFSET 0x04
  78. #define MSI_BASE_HI_OFFSET 0x08
  79. #define MSI_SIZE_OFFSET 0x0c
  80. #define MSI_ENABLE_OFFSET 0x14
  81. #define MSI_STATUS_OFFSET 0x18
  82. #define MSI_DATA_OFFSET 0x20
  83. #define MSI_ADDR_L_OFFSET 0x24
  84. #define MSI_ADDR_H_OFFSET 0x28
  85. /* outbound and inbound window definitions */
  86. #define WIN_NUM_0 0
  87. #define WIN_NUM_1 1
  88. #define CFG_WINDOW_TYPE 0
  89. #define IO_WINDOW_TYPE 1
  90. #define MEM_WINDOW_TYPE 2
  91. #define IB_WIN_SIZE ((u64)256 * 1024 * 1024 * 1024)
  92. #define MAX_PIO_WINDOWS 8
  93. /* Parameters for the waiting for link up routine */
  94. #define LINK_WAIT_MAX_RETRIES 10
  95. #define LINK_WAIT_MIN 90000
  96. #define LINK_WAIT_MAX 100000
  97. struct mobiveil_msi { /* MSI information */
  98. struct mutex lock; /* protect bitmap variable */
  99. struct irq_domain *msi_domain;
  100. struct irq_domain *dev_domain;
  101. phys_addr_t msi_pages_phys;
  102. int num_of_vectors;
  103. DECLARE_BITMAP(msi_irq_in_use, PCI_NUM_MSI);
  104. };
  105. struct mobiveil_pcie {
  106. struct platform_device *pdev;
  107. struct list_head resources;
  108. void __iomem *config_axi_slave_base; /* endpoint config base */
  109. void __iomem *csr_axi_slave_base; /* root port config base */
  110. void __iomem *apb_csr_base; /* MSI register base */
  111. phys_addr_t pcie_reg_base; /* Physical PCIe Controller Base */
  112. struct irq_domain *intx_domain;
  113. raw_spinlock_t intx_mask_lock;
  114. int irq;
  115. int apio_wins;
  116. int ppio_wins;
  117. int ob_wins_configured; /* configured outbound windows */
  118. int ib_wins_configured; /* configured inbound windows */
  119. struct resource *ob_io_res;
  120. char root_bus_nr;
  121. struct mobiveil_msi msi;
  122. };
  123. static inline void csr_writel(struct mobiveil_pcie *pcie, const u32 value,
  124. const u32 reg)
  125. {
  126. writel_relaxed(value, pcie->csr_axi_slave_base + reg);
  127. }
  128. static inline u32 csr_readl(struct mobiveil_pcie *pcie, const u32 reg)
  129. {
  130. return readl_relaxed(pcie->csr_axi_slave_base + reg);
  131. }
  132. static bool mobiveil_pcie_link_up(struct mobiveil_pcie *pcie)
  133. {
  134. return (csr_readl(pcie, LTSSM_STATUS) &
  135. LTSSM_STATUS_L0_MASK) == LTSSM_STATUS_L0;
  136. }
  137. static bool mobiveil_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
  138. {
  139. struct mobiveil_pcie *pcie = bus->sysdata;
  140. /* Only one device down on each root port */
  141. if ((bus->number == pcie->root_bus_nr) && (devfn > 0))
  142. return false;
  143. /*
  144. * Do not read more than one device on the bus directly
  145. * attached to RC
  146. */
  147. if ((bus->primary == pcie->root_bus_nr) && (devfn > 0))
  148. return false;
  149. return true;
  150. }
  151. /*
  152. * mobiveil_pcie_map_bus - routine to get the configuration base of either
  153. * root port or endpoint
  154. */
  155. static void __iomem *mobiveil_pcie_map_bus(struct pci_bus *bus,
  156. unsigned int devfn, int where)
  157. {
  158. struct mobiveil_pcie *pcie = bus->sysdata;
  159. if (!mobiveil_pcie_valid_device(bus, devfn))
  160. return NULL;
  161. if (bus->number == pcie->root_bus_nr) {
  162. /* RC config access */
  163. return pcie->csr_axi_slave_base + where;
  164. }
  165. /*
  166. * EP config access (in Config/APIO space)
  167. * Program PEX Address base (31..16 bits) with appropriate value
  168. * (BDF) in PAB_AXI_AMAP_PEX_WIN_L0 Register.
  169. * Relies on pci_lock serialization
  170. */
  171. csr_writel(pcie, bus->number << PAB_BUS_SHIFT |
  172. PCI_SLOT(devfn) << PAB_DEVICE_SHIFT |
  173. PCI_FUNC(devfn) << PAB_FUNCTION_SHIFT,
  174. PAB_AXI_AMAP_PEX_WIN_L(WIN_NUM_0));
  175. return pcie->config_axi_slave_base + where;
  176. }
  177. static struct pci_ops mobiveil_pcie_ops = {
  178. .map_bus = mobiveil_pcie_map_bus,
  179. .read = pci_generic_config_read,
  180. .write = pci_generic_config_write,
  181. };
  182. static void mobiveil_pcie_isr(struct irq_desc *desc)
  183. {
  184. struct irq_chip *chip = irq_desc_get_chip(desc);
  185. struct mobiveil_pcie *pcie = irq_desc_get_handler_data(desc);
  186. struct device *dev = &pcie->pdev->dev;
  187. struct mobiveil_msi *msi = &pcie->msi;
  188. u32 msi_data, msi_addr_lo, msi_addr_hi;
  189. u32 intr_status, msi_status;
  190. unsigned long shifted_status;
  191. u32 bit, virq, val, mask;
  192. /*
  193. * The core provides a single interrupt for both INTx/MSI messages.
  194. * So we'll read both INTx and MSI status
  195. */
  196. chained_irq_enter(chip, desc);
  197. /* read INTx status */
  198. val = csr_readl(pcie, PAB_INTP_AMBA_MISC_STAT);
  199. mask = csr_readl(pcie, PAB_INTP_AMBA_MISC_ENB);
  200. intr_status = val & mask;
  201. /* Handle INTx */
  202. if (intr_status & PAB_INTP_INTX_MASK) {
  203. shifted_status = csr_readl(pcie, PAB_INTP_AMBA_MISC_STAT) >>
  204. PAB_INTX_START;
  205. do {
  206. for_each_set_bit(bit, &shifted_status, PCI_NUM_INTX) {
  207. virq = irq_find_mapping(pcie->intx_domain,
  208. bit + 1);
  209. if (virq)
  210. generic_handle_irq(virq);
  211. else
  212. dev_err_ratelimited(dev,
  213. "unexpected IRQ, INT%d\n", bit);
  214. /* clear interrupt */
  215. csr_writel(pcie,
  216. shifted_status << PAB_INTX_START,
  217. PAB_INTP_AMBA_MISC_STAT);
  218. }
  219. } while ((shifted_status >> PAB_INTX_START) != 0);
  220. }
  221. /* read extra MSI status register */
  222. msi_status = readl_relaxed(pcie->apb_csr_base + MSI_STATUS_OFFSET);
  223. /* handle MSI interrupts */
  224. while (msi_status & 1) {
  225. msi_data = readl_relaxed(pcie->apb_csr_base
  226. + MSI_DATA_OFFSET);
  227. /*
  228. * MSI_STATUS_OFFSET register gets updated to zero
  229. * once we pop not only the MSI data but also address
  230. * from MSI hardware FIFO. So keeping these following
  231. * two dummy reads.
  232. */
  233. msi_addr_lo = readl_relaxed(pcie->apb_csr_base +
  234. MSI_ADDR_L_OFFSET);
  235. msi_addr_hi = readl_relaxed(pcie->apb_csr_base +
  236. MSI_ADDR_H_OFFSET);
  237. dev_dbg(dev, "MSI registers, data: %08x, addr: %08x:%08x\n",
  238. msi_data, msi_addr_hi, msi_addr_lo);
  239. virq = irq_find_mapping(msi->dev_domain, msi_data);
  240. if (virq)
  241. generic_handle_irq(virq);
  242. msi_status = readl_relaxed(pcie->apb_csr_base +
  243. MSI_STATUS_OFFSET);
  244. }
  245. /* Clear the interrupt status */
  246. csr_writel(pcie, intr_status, PAB_INTP_AMBA_MISC_STAT);
  247. chained_irq_exit(chip, desc);
  248. }
  249. static int mobiveil_pcie_parse_dt(struct mobiveil_pcie *pcie)
  250. {
  251. struct device *dev = &pcie->pdev->dev;
  252. struct platform_device *pdev = pcie->pdev;
  253. struct device_node *node = dev->of_node;
  254. struct resource *res;
  255. /* map config resource */
  256. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  257. "config_axi_slave");
  258. pcie->config_axi_slave_base = devm_pci_remap_cfg_resource(dev, res);
  259. if (IS_ERR(pcie->config_axi_slave_base))
  260. return PTR_ERR(pcie->config_axi_slave_base);
  261. pcie->ob_io_res = res;
  262. /* map csr resource */
  263. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  264. "csr_axi_slave");
  265. pcie->csr_axi_slave_base = devm_pci_remap_cfg_resource(dev, res);
  266. if (IS_ERR(pcie->csr_axi_slave_base))
  267. return PTR_ERR(pcie->csr_axi_slave_base);
  268. pcie->pcie_reg_base = res->start;
  269. /* map MSI config resource */
  270. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "apb_csr");
  271. pcie->apb_csr_base = devm_pci_remap_cfg_resource(dev, res);
  272. if (IS_ERR(pcie->apb_csr_base))
  273. return PTR_ERR(pcie->apb_csr_base);
  274. /* read the number of windows requested */
  275. if (of_property_read_u32(node, "apio-wins", &pcie->apio_wins))
  276. pcie->apio_wins = MAX_PIO_WINDOWS;
  277. if (of_property_read_u32(node, "ppio-wins", &pcie->ppio_wins))
  278. pcie->ppio_wins = MAX_PIO_WINDOWS;
  279. pcie->irq = platform_get_irq(pdev, 0);
  280. if (pcie->irq <= 0) {
  281. dev_err(dev, "failed to map IRQ: %d\n", pcie->irq);
  282. return -ENODEV;
  283. }
  284. irq_set_chained_handler_and_data(pcie->irq, mobiveil_pcie_isr, pcie);
  285. return 0;
  286. }
  287. /*
  288. * select_paged_register - routine to access paged register of root complex
  289. *
  290. * registers of RC are paged, for this scheme to work
  291. * extracted higher 6 bits of the offset will be written to pg_sel
  292. * field of PAB_CTRL register and rest of the lower 10 bits enabled with
  293. * PAGE_SEL_EN are used as offset of the register.
  294. */
  295. static void select_paged_register(struct mobiveil_pcie *pcie, u32 offset)
  296. {
  297. int pab_ctrl_dw, pg_sel;
  298. /* clear pg_sel field */
  299. pab_ctrl_dw = csr_readl(pcie, PAB_CTRL);
  300. pab_ctrl_dw = (pab_ctrl_dw & ~(PAGE_SEL_MASK << PAGE_SEL_SHIFT));
  301. /* set pg_sel field */
  302. pg_sel = (offset >> PAGE_SEL_OFFSET_SHIFT) & PAGE_SEL_MASK;
  303. pab_ctrl_dw |= ((pg_sel << PAGE_SEL_SHIFT));
  304. csr_writel(pcie, pab_ctrl_dw, PAB_CTRL);
  305. }
  306. static void write_paged_register(struct mobiveil_pcie *pcie,
  307. u32 val, u32 offset)
  308. {
  309. u32 off = (offset & PAGE_LO_MASK) | PAGE_SEL_EN;
  310. select_paged_register(pcie, offset);
  311. csr_writel(pcie, val, off);
  312. }
  313. static u32 read_paged_register(struct mobiveil_pcie *pcie, u32 offset)
  314. {
  315. u32 off = (offset & PAGE_LO_MASK) | PAGE_SEL_EN;
  316. select_paged_register(pcie, offset);
  317. return csr_readl(pcie, off);
  318. }
  319. static void program_ib_windows(struct mobiveil_pcie *pcie, int win_num,
  320. int pci_addr, u32 type, u64 size)
  321. {
  322. int pio_ctrl_val;
  323. int amap_ctrl_dw;
  324. u64 size64 = ~(size - 1);
  325. if ((pcie->ib_wins_configured + 1) > pcie->ppio_wins) {
  326. dev_err(&pcie->pdev->dev,
  327. "ERROR: max inbound windows reached !\n");
  328. return;
  329. }
  330. pio_ctrl_val = csr_readl(pcie, PAB_PEX_PIO_CTRL);
  331. csr_writel(pcie,
  332. pio_ctrl_val | (1 << PIO_ENABLE_SHIFT), PAB_PEX_PIO_CTRL);
  333. amap_ctrl_dw = read_paged_register(pcie, PAB_PEX_AMAP_CTRL(win_num));
  334. amap_ctrl_dw = (amap_ctrl_dw | (type << AMAP_CTRL_TYPE_SHIFT));
  335. amap_ctrl_dw = (amap_ctrl_dw | (1 << AMAP_CTRL_EN_SHIFT));
  336. write_paged_register(pcie, amap_ctrl_dw | lower_32_bits(size64),
  337. PAB_PEX_AMAP_CTRL(win_num));
  338. write_paged_register(pcie, upper_32_bits(size64),
  339. PAB_EXT_PEX_AMAP_SIZEN(win_num));
  340. write_paged_register(pcie, pci_addr, PAB_PEX_AMAP_AXI_WIN(win_num));
  341. write_paged_register(pcie, pci_addr, PAB_PEX_AMAP_PEX_WIN_L(win_num));
  342. write_paged_register(pcie, 0, PAB_PEX_AMAP_PEX_WIN_H(win_num));
  343. }
  344. /*
  345. * routine to program the outbound windows
  346. */
  347. static void program_ob_windows(struct mobiveil_pcie *pcie, int win_num,
  348. u64 cpu_addr, u64 pci_addr, u32 config_io_bit, u64 size)
  349. {
  350. u32 value, type;
  351. u64 size64 = ~(size - 1);
  352. if ((pcie->ob_wins_configured + 1) > pcie->apio_wins) {
  353. dev_err(&pcie->pdev->dev,
  354. "ERROR: max outbound windows reached !\n");
  355. return;
  356. }
  357. /*
  358. * program Enable Bit to 1, Type Bit to (00) base 2, AXI Window Size Bit
  359. * to 4 KB in PAB_AXI_AMAP_CTRL register
  360. */
  361. type = config_io_bit;
  362. value = csr_readl(pcie, PAB_AXI_AMAP_CTRL(win_num));
  363. csr_writel(pcie, 1 << WIN_ENABLE_SHIFT | type << WIN_TYPE_SHIFT |
  364. lower_32_bits(size64), PAB_AXI_AMAP_CTRL(win_num));
  365. write_paged_register(pcie, upper_32_bits(size64),
  366. PAB_EXT_AXI_AMAP_SIZE(win_num));
  367. /*
  368. * program AXI window base with appropriate value in
  369. * PAB_AXI_AMAP_AXI_WIN0 register
  370. */
  371. value = csr_readl(pcie, PAB_AXI_AMAP_AXI_WIN(win_num));
  372. csr_writel(pcie, cpu_addr & (~AXI_WINDOW_ALIGN_MASK),
  373. PAB_AXI_AMAP_AXI_WIN(win_num));
  374. value = csr_readl(pcie, PAB_AXI_AMAP_PEX_WIN_H(win_num));
  375. csr_writel(pcie, lower_32_bits(pci_addr),
  376. PAB_AXI_AMAP_PEX_WIN_L(win_num));
  377. csr_writel(pcie, upper_32_bits(pci_addr),
  378. PAB_AXI_AMAP_PEX_WIN_H(win_num));
  379. pcie->ob_wins_configured++;
  380. }
  381. static int mobiveil_bringup_link(struct mobiveil_pcie *pcie)
  382. {
  383. int retries;
  384. /* check if the link is up or not */
  385. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  386. if (mobiveil_pcie_link_up(pcie))
  387. return 0;
  388. usleep_range(LINK_WAIT_MIN, LINK_WAIT_MAX);
  389. }
  390. dev_err(&pcie->pdev->dev, "link never came up\n");
  391. return -ETIMEDOUT;
  392. }
  393. static void mobiveil_pcie_enable_msi(struct mobiveil_pcie *pcie)
  394. {
  395. phys_addr_t msg_addr = pcie->pcie_reg_base;
  396. struct mobiveil_msi *msi = &pcie->msi;
  397. pcie->msi.num_of_vectors = PCI_NUM_MSI;
  398. msi->msi_pages_phys = (phys_addr_t)msg_addr;
  399. writel_relaxed(lower_32_bits(msg_addr),
  400. pcie->apb_csr_base + MSI_BASE_LO_OFFSET);
  401. writel_relaxed(upper_32_bits(msg_addr),
  402. pcie->apb_csr_base + MSI_BASE_HI_OFFSET);
  403. writel_relaxed(4096, pcie->apb_csr_base + MSI_SIZE_OFFSET);
  404. writel_relaxed(1, pcie->apb_csr_base + MSI_ENABLE_OFFSET);
  405. }
  406. static int mobiveil_host_init(struct mobiveil_pcie *pcie)
  407. {
  408. u32 value, pab_ctrl, type = 0;
  409. int err;
  410. struct resource_entry *win, *tmp;
  411. err = mobiveil_bringup_link(pcie);
  412. if (err) {
  413. dev_info(&pcie->pdev->dev, "link bring-up failed\n");
  414. return err;
  415. }
  416. /*
  417. * program Bus Master Enable Bit in Command Register in PAB Config
  418. * Space
  419. */
  420. value = csr_readl(pcie, PCI_COMMAND);
  421. csr_writel(pcie, value | PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  422. PCI_COMMAND_MASTER, PCI_COMMAND);
  423. /*
  424. * program PIO Enable Bit to 1 (and PEX PIO Enable to 1) in PAB_CTRL
  425. * register
  426. */
  427. pab_ctrl = csr_readl(pcie, PAB_CTRL);
  428. csr_writel(pcie, pab_ctrl | (1 << AMBA_PIO_ENABLE_SHIFT) |
  429. (1 << PEX_PIO_ENABLE_SHIFT), PAB_CTRL);
  430. csr_writel(pcie, (PAB_INTP_INTX_MASK | PAB_INTP_MSI_MASK),
  431. PAB_INTP_AMBA_MISC_ENB);
  432. /*
  433. * program PIO Enable Bit to 1 and Config Window Enable Bit to 1 in
  434. * PAB_AXI_PIO_CTRL Register
  435. */
  436. value = csr_readl(pcie, PAB_AXI_PIO_CTRL);
  437. csr_writel(pcie, value | APIO_EN_MASK, PAB_AXI_PIO_CTRL);
  438. /*
  439. * we'll program one outbound window for config reads and
  440. * another default inbound window for all the upstream traffic
  441. * rest of the outbound windows will be configured according to
  442. * the "ranges" field defined in device tree
  443. */
  444. /* config outbound translation window */
  445. program_ob_windows(pcie, pcie->ob_wins_configured,
  446. pcie->ob_io_res->start, 0, CFG_WINDOW_TYPE,
  447. resource_size(pcie->ob_io_res));
  448. /* memory inbound translation window */
  449. program_ib_windows(pcie, WIN_NUM_1, 0, MEM_WINDOW_TYPE, IB_WIN_SIZE);
  450. /* Get the I/O and memory ranges from DT */
  451. resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
  452. type = 0;
  453. if (resource_type(win->res) == IORESOURCE_MEM)
  454. type = MEM_WINDOW_TYPE;
  455. if (resource_type(win->res) == IORESOURCE_IO)
  456. type = IO_WINDOW_TYPE;
  457. if (type) {
  458. /* configure outbound translation window */
  459. program_ob_windows(pcie, pcie->ob_wins_configured,
  460. win->res->start, 0, type,
  461. resource_size(win->res));
  462. }
  463. }
  464. /* setup MSI hardware registers */
  465. mobiveil_pcie_enable_msi(pcie);
  466. return err;
  467. }
  468. static void mobiveil_mask_intx_irq(struct irq_data *data)
  469. {
  470. struct irq_desc *desc = irq_to_desc(data->irq);
  471. struct mobiveil_pcie *pcie;
  472. unsigned long flags;
  473. u32 mask, shifted_val;
  474. pcie = irq_desc_get_chip_data(desc);
  475. mask = 1 << ((data->hwirq + PAB_INTX_START) - 1);
  476. raw_spin_lock_irqsave(&pcie->intx_mask_lock, flags);
  477. shifted_val = csr_readl(pcie, PAB_INTP_AMBA_MISC_ENB);
  478. csr_writel(pcie, (shifted_val & (~mask)), PAB_INTP_AMBA_MISC_ENB);
  479. raw_spin_unlock_irqrestore(&pcie->intx_mask_lock, flags);
  480. }
  481. static void mobiveil_unmask_intx_irq(struct irq_data *data)
  482. {
  483. struct irq_desc *desc = irq_to_desc(data->irq);
  484. struct mobiveil_pcie *pcie;
  485. unsigned long flags;
  486. u32 shifted_val, mask;
  487. pcie = irq_desc_get_chip_data(desc);
  488. mask = 1 << ((data->hwirq + PAB_INTX_START) - 1);
  489. raw_spin_lock_irqsave(&pcie->intx_mask_lock, flags);
  490. shifted_val = csr_readl(pcie, PAB_INTP_AMBA_MISC_ENB);
  491. csr_writel(pcie, (shifted_val | mask), PAB_INTP_AMBA_MISC_ENB);
  492. raw_spin_unlock_irqrestore(&pcie->intx_mask_lock, flags);
  493. }
  494. static struct irq_chip intx_irq_chip = {
  495. .name = "mobiveil_pcie:intx",
  496. .irq_enable = mobiveil_unmask_intx_irq,
  497. .irq_disable = mobiveil_mask_intx_irq,
  498. .irq_mask = mobiveil_mask_intx_irq,
  499. .irq_unmask = mobiveil_unmask_intx_irq,
  500. };
  501. /* routine to setup the INTx related data */
  502. static int mobiveil_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  503. irq_hw_number_t hwirq)
  504. {
  505. irq_set_chip_and_handler(irq, &intx_irq_chip, handle_level_irq);
  506. irq_set_chip_data(irq, domain->host_data);
  507. return 0;
  508. }
  509. /* INTx domain operations structure */
  510. static const struct irq_domain_ops intx_domain_ops = {
  511. .map = mobiveil_pcie_intx_map,
  512. };
  513. static struct irq_chip mobiveil_msi_irq_chip = {
  514. .name = "Mobiveil PCIe MSI",
  515. .irq_mask = pci_msi_mask_irq,
  516. .irq_unmask = pci_msi_unmask_irq,
  517. };
  518. static struct msi_domain_info mobiveil_msi_domain_info = {
  519. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  520. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
  521. .chip = &mobiveil_msi_irq_chip,
  522. };
  523. static void mobiveil_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  524. {
  525. struct mobiveil_pcie *pcie = irq_data_get_irq_chip_data(data);
  526. phys_addr_t addr = pcie->pcie_reg_base + (data->hwirq * sizeof(int));
  527. msg->address_lo = lower_32_bits(addr);
  528. msg->address_hi = upper_32_bits(addr);
  529. msg->data = data->hwirq;
  530. dev_dbg(&pcie->pdev->dev, "msi#%d address_hi %#x address_lo %#x\n",
  531. (int)data->hwirq, msg->address_hi, msg->address_lo);
  532. }
  533. static int mobiveil_msi_set_affinity(struct irq_data *irq_data,
  534. const struct cpumask *mask, bool force)
  535. {
  536. return -EINVAL;
  537. }
  538. static struct irq_chip mobiveil_msi_bottom_irq_chip = {
  539. .name = "Mobiveil MSI",
  540. .irq_compose_msi_msg = mobiveil_compose_msi_msg,
  541. .irq_set_affinity = mobiveil_msi_set_affinity,
  542. };
  543. static int mobiveil_irq_msi_domain_alloc(struct irq_domain *domain,
  544. unsigned int virq, unsigned int nr_irqs, void *args)
  545. {
  546. struct mobiveil_pcie *pcie = domain->host_data;
  547. struct mobiveil_msi *msi = &pcie->msi;
  548. unsigned long bit;
  549. WARN_ON(nr_irqs != 1);
  550. mutex_lock(&msi->lock);
  551. bit = find_first_zero_bit(msi->msi_irq_in_use, msi->num_of_vectors);
  552. if (bit >= msi->num_of_vectors) {
  553. mutex_unlock(&msi->lock);
  554. return -ENOSPC;
  555. }
  556. set_bit(bit, msi->msi_irq_in_use);
  557. mutex_unlock(&msi->lock);
  558. irq_domain_set_info(domain, virq, bit, &mobiveil_msi_bottom_irq_chip,
  559. domain->host_data, handle_level_irq,
  560. NULL, NULL);
  561. return 0;
  562. }
  563. static void mobiveil_irq_msi_domain_free(struct irq_domain *domain,
  564. unsigned int virq, unsigned int nr_irqs)
  565. {
  566. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  567. struct mobiveil_pcie *pcie = irq_data_get_irq_chip_data(d);
  568. struct mobiveil_msi *msi = &pcie->msi;
  569. mutex_lock(&msi->lock);
  570. if (!test_bit(d->hwirq, msi->msi_irq_in_use)) {
  571. dev_err(&pcie->pdev->dev, "trying to free unused MSI#%lu\n",
  572. d->hwirq);
  573. } else {
  574. __clear_bit(d->hwirq, msi->msi_irq_in_use);
  575. }
  576. mutex_unlock(&msi->lock);
  577. }
  578. static const struct irq_domain_ops msi_domain_ops = {
  579. .alloc = mobiveil_irq_msi_domain_alloc,
  580. .free = mobiveil_irq_msi_domain_free,
  581. };
  582. static int mobiveil_allocate_msi_domains(struct mobiveil_pcie *pcie)
  583. {
  584. struct device *dev = &pcie->pdev->dev;
  585. struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
  586. struct mobiveil_msi *msi = &pcie->msi;
  587. mutex_init(&pcie->msi.lock);
  588. msi->dev_domain = irq_domain_add_linear(NULL, msi->num_of_vectors,
  589. &msi_domain_ops, pcie);
  590. if (!msi->dev_domain) {
  591. dev_err(dev, "failed to create IRQ domain\n");
  592. return -ENOMEM;
  593. }
  594. msi->msi_domain = pci_msi_create_irq_domain(fwnode,
  595. &mobiveil_msi_domain_info, msi->dev_domain);
  596. if (!msi->msi_domain) {
  597. dev_err(dev, "failed to create MSI domain\n");
  598. irq_domain_remove(msi->dev_domain);
  599. return -ENOMEM;
  600. }
  601. return 0;
  602. }
  603. static int mobiveil_pcie_init_irq_domain(struct mobiveil_pcie *pcie)
  604. {
  605. struct device *dev = &pcie->pdev->dev;
  606. struct device_node *node = dev->of_node;
  607. int ret;
  608. /* setup INTx */
  609. pcie->intx_domain = irq_domain_add_linear(node,
  610. PCI_NUM_INTX, &intx_domain_ops, pcie);
  611. if (!pcie->intx_domain) {
  612. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  613. return -ENODEV;
  614. }
  615. raw_spin_lock_init(&pcie->intx_mask_lock);
  616. /* setup MSI */
  617. ret = mobiveil_allocate_msi_domains(pcie);
  618. if (ret)
  619. return ret;
  620. return 0;
  621. }
  622. static int mobiveil_pcie_probe(struct platform_device *pdev)
  623. {
  624. struct mobiveil_pcie *pcie;
  625. struct pci_bus *bus;
  626. struct pci_bus *child;
  627. struct pci_host_bridge *bridge;
  628. struct device *dev = &pdev->dev;
  629. resource_size_t iobase;
  630. int ret;
  631. /* allocate the PCIe port */
  632. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  633. if (!bridge)
  634. return -ENODEV;
  635. pcie = pci_host_bridge_priv(bridge);
  636. if (!pcie)
  637. return -ENOMEM;
  638. pcie->pdev = pdev;
  639. ret = mobiveil_pcie_parse_dt(pcie);
  640. if (ret) {
  641. dev_err(dev, "Parsing DT failed, ret: %x\n", ret);
  642. return ret;
  643. }
  644. INIT_LIST_HEAD(&pcie->resources);
  645. /* parse the host bridge base addresses from the device tree file */
  646. ret = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
  647. &pcie->resources, &iobase);
  648. if (ret) {
  649. dev_err(dev, "Getting bridge resources failed\n");
  650. return -ENOMEM;
  651. }
  652. /*
  653. * configure all inbound and outbound windows and prepare the RC for
  654. * config access
  655. */
  656. ret = mobiveil_host_init(pcie);
  657. if (ret) {
  658. dev_err(dev, "Failed to initialize host\n");
  659. goto error;
  660. }
  661. /* fixup for PCIe class register */
  662. csr_writel(pcie, 0x060402ab, PAB_INTP_AXI_PIO_CLASS);
  663. /* initialize the IRQ domains */
  664. ret = mobiveil_pcie_init_irq_domain(pcie);
  665. if (ret) {
  666. dev_err(dev, "Failed creating IRQ Domain\n");
  667. goto error;
  668. }
  669. ret = devm_request_pci_bus_resources(dev, &pcie->resources);
  670. if (ret)
  671. goto error;
  672. /* Initialize bridge */
  673. list_splice_init(&pcie->resources, &bridge->windows);
  674. bridge->dev.parent = dev;
  675. bridge->sysdata = pcie;
  676. bridge->busnr = pcie->root_bus_nr;
  677. bridge->ops = &mobiveil_pcie_ops;
  678. bridge->map_irq = of_irq_parse_and_map_pci;
  679. bridge->swizzle_irq = pci_common_swizzle;
  680. /* setup the kernel resources for the newly added PCIe root bus */
  681. ret = pci_scan_root_bus_bridge(bridge);
  682. if (ret)
  683. goto error;
  684. bus = bridge->bus;
  685. pci_assign_unassigned_bus_resources(bus);
  686. list_for_each_entry(child, &bus->children, node)
  687. pcie_bus_configure_settings(child);
  688. pci_bus_add_devices(bus);
  689. return 0;
  690. error:
  691. pci_free_resource_list(&pcie->resources);
  692. return ret;
  693. }
  694. static const struct of_device_id mobiveil_pcie_of_match[] = {
  695. {.compatible = "mbvl,gpex40-pcie",},
  696. {},
  697. };
  698. MODULE_DEVICE_TABLE(of, mobiveil_pcie_of_match);
  699. static struct platform_driver mobiveil_pcie_driver = {
  700. .probe = mobiveil_pcie_probe,
  701. .driver = {
  702. .name = "mobiveil-pcie",
  703. .of_match_table = mobiveil_pcie_of_match,
  704. .suppress_bind_attrs = true,
  705. },
  706. };
  707. builtin_platform_driver(mobiveil_pcie_driver);
  708. MODULE_LICENSE("GPL v2");
  709. MODULE_DESCRIPTION("Mobiveil PCIe host controller driver");
  710. MODULE_AUTHOR("Subrahmanya Lingappa <l.subrahmanya@mobiveil.co.in>");