pcie-mobiveil.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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) && (PCI_SLOT(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. const char *type;
  256. type = of_get_property(node, "device_type", NULL);
  257. if (!type || strcmp(type, "pci")) {
  258. dev_err(dev, "invalid \"device_type\" %s\n", type);
  259. return -EINVAL;
  260. }
  261. /* map config resource */
  262. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  263. "config_axi_slave");
  264. pcie->config_axi_slave_base = devm_pci_remap_cfg_resource(dev, res);
  265. if (IS_ERR(pcie->config_axi_slave_base))
  266. return PTR_ERR(pcie->config_axi_slave_base);
  267. pcie->ob_io_res = res;
  268. /* map csr resource */
  269. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  270. "csr_axi_slave");
  271. pcie->csr_axi_slave_base = devm_pci_remap_cfg_resource(dev, res);
  272. if (IS_ERR(pcie->csr_axi_slave_base))
  273. return PTR_ERR(pcie->csr_axi_slave_base);
  274. pcie->pcie_reg_base = res->start;
  275. /* map MSI config resource */
  276. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "apb_csr");
  277. pcie->apb_csr_base = devm_pci_remap_cfg_resource(dev, res);
  278. if (IS_ERR(pcie->apb_csr_base))
  279. return PTR_ERR(pcie->apb_csr_base);
  280. /* read the number of windows requested */
  281. if (of_property_read_u32(node, "apio-wins", &pcie->apio_wins))
  282. pcie->apio_wins = MAX_PIO_WINDOWS;
  283. if (of_property_read_u32(node, "ppio-wins", &pcie->ppio_wins))
  284. pcie->ppio_wins = MAX_PIO_WINDOWS;
  285. pcie->irq = platform_get_irq(pdev, 0);
  286. if (pcie->irq <= 0) {
  287. dev_err(dev, "failed to map IRQ: %d\n", pcie->irq);
  288. return -ENODEV;
  289. }
  290. irq_set_chained_handler_and_data(pcie->irq, mobiveil_pcie_isr, pcie);
  291. return 0;
  292. }
  293. /*
  294. * select_paged_register - routine to access paged register of root complex
  295. *
  296. * registers of RC are paged, for this scheme to work
  297. * extracted higher 6 bits of the offset will be written to pg_sel
  298. * field of PAB_CTRL register and rest of the lower 10 bits enabled with
  299. * PAGE_SEL_EN are used as offset of the register.
  300. */
  301. static void select_paged_register(struct mobiveil_pcie *pcie, u32 offset)
  302. {
  303. int pab_ctrl_dw, pg_sel;
  304. /* clear pg_sel field */
  305. pab_ctrl_dw = csr_readl(pcie, PAB_CTRL);
  306. pab_ctrl_dw = (pab_ctrl_dw & ~(PAGE_SEL_MASK << PAGE_SEL_SHIFT));
  307. /* set pg_sel field */
  308. pg_sel = (offset >> PAGE_SEL_OFFSET_SHIFT) & PAGE_SEL_MASK;
  309. pab_ctrl_dw |= ((pg_sel << PAGE_SEL_SHIFT));
  310. csr_writel(pcie, pab_ctrl_dw, PAB_CTRL);
  311. }
  312. static void write_paged_register(struct mobiveil_pcie *pcie,
  313. u32 val, u32 offset)
  314. {
  315. u32 off = (offset & PAGE_LO_MASK) | PAGE_SEL_EN;
  316. select_paged_register(pcie, offset);
  317. csr_writel(pcie, val, off);
  318. }
  319. static u32 read_paged_register(struct mobiveil_pcie *pcie, u32 offset)
  320. {
  321. u32 off = (offset & PAGE_LO_MASK) | PAGE_SEL_EN;
  322. select_paged_register(pcie, offset);
  323. return csr_readl(pcie, off);
  324. }
  325. static void program_ib_windows(struct mobiveil_pcie *pcie, int win_num,
  326. int pci_addr, u32 type, u64 size)
  327. {
  328. int pio_ctrl_val;
  329. int amap_ctrl_dw;
  330. u64 size64 = ~(size - 1);
  331. if (win_num >= pcie->ppio_wins) {
  332. dev_err(&pcie->pdev->dev,
  333. "ERROR: max inbound windows reached !\n");
  334. return;
  335. }
  336. pio_ctrl_val = csr_readl(pcie, PAB_PEX_PIO_CTRL);
  337. csr_writel(pcie,
  338. pio_ctrl_val | (1 << PIO_ENABLE_SHIFT), PAB_PEX_PIO_CTRL);
  339. amap_ctrl_dw = read_paged_register(pcie, PAB_PEX_AMAP_CTRL(win_num));
  340. amap_ctrl_dw = (amap_ctrl_dw | (type << AMAP_CTRL_TYPE_SHIFT));
  341. amap_ctrl_dw = (amap_ctrl_dw | (1 << AMAP_CTRL_EN_SHIFT));
  342. write_paged_register(pcie, amap_ctrl_dw | lower_32_bits(size64),
  343. PAB_PEX_AMAP_CTRL(win_num));
  344. write_paged_register(pcie, upper_32_bits(size64),
  345. PAB_EXT_PEX_AMAP_SIZEN(win_num));
  346. write_paged_register(pcie, pci_addr, PAB_PEX_AMAP_AXI_WIN(win_num));
  347. write_paged_register(pcie, pci_addr, PAB_PEX_AMAP_PEX_WIN_L(win_num));
  348. write_paged_register(pcie, 0, PAB_PEX_AMAP_PEX_WIN_H(win_num));
  349. }
  350. /*
  351. * routine to program the outbound windows
  352. */
  353. static void program_ob_windows(struct mobiveil_pcie *pcie, int win_num,
  354. u64 cpu_addr, u64 pci_addr, u32 config_io_bit, u64 size)
  355. {
  356. u32 value, type;
  357. u64 size64 = ~(size - 1);
  358. if (win_num >= pcie->apio_wins) {
  359. dev_err(&pcie->pdev->dev,
  360. "ERROR: max outbound windows reached !\n");
  361. return;
  362. }
  363. /*
  364. * program Enable Bit to 1, Type Bit to (00) base 2, AXI Window Size Bit
  365. * to 4 KB in PAB_AXI_AMAP_CTRL register
  366. */
  367. type = config_io_bit;
  368. value = csr_readl(pcie, PAB_AXI_AMAP_CTRL(win_num));
  369. csr_writel(pcie, 1 << WIN_ENABLE_SHIFT | type << WIN_TYPE_SHIFT |
  370. lower_32_bits(size64), PAB_AXI_AMAP_CTRL(win_num));
  371. write_paged_register(pcie, upper_32_bits(size64),
  372. PAB_EXT_AXI_AMAP_SIZE(win_num));
  373. /*
  374. * program AXI window base with appropriate value in
  375. * PAB_AXI_AMAP_AXI_WIN0 register
  376. */
  377. value = csr_readl(pcie, PAB_AXI_AMAP_AXI_WIN(win_num));
  378. csr_writel(pcie, cpu_addr & (~AXI_WINDOW_ALIGN_MASK),
  379. PAB_AXI_AMAP_AXI_WIN(win_num));
  380. value = csr_readl(pcie, PAB_AXI_AMAP_PEX_WIN_H(win_num));
  381. csr_writel(pcie, lower_32_bits(pci_addr),
  382. PAB_AXI_AMAP_PEX_WIN_L(win_num));
  383. csr_writel(pcie, upper_32_bits(pci_addr),
  384. PAB_AXI_AMAP_PEX_WIN_H(win_num));
  385. pcie->ob_wins_configured++;
  386. }
  387. static int mobiveil_bringup_link(struct mobiveil_pcie *pcie)
  388. {
  389. int retries;
  390. /* check if the link is up or not */
  391. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  392. if (mobiveil_pcie_link_up(pcie))
  393. return 0;
  394. usleep_range(LINK_WAIT_MIN, LINK_WAIT_MAX);
  395. }
  396. dev_err(&pcie->pdev->dev, "link never came up\n");
  397. return -ETIMEDOUT;
  398. }
  399. static void mobiveil_pcie_enable_msi(struct mobiveil_pcie *pcie)
  400. {
  401. phys_addr_t msg_addr = pcie->pcie_reg_base;
  402. struct mobiveil_msi *msi = &pcie->msi;
  403. pcie->msi.num_of_vectors = PCI_NUM_MSI;
  404. msi->msi_pages_phys = (phys_addr_t)msg_addr;
  405. writel_relaxed(lower_32_bits(msg_addr),
  406. pcie->apb_csr_base + MSI_BASE_LO_OFFSET);
  407. writel_relaxed(upper_32_bits(msg_addr),
  408. pcie->apb_csr_base + MSI_BASE_HI_OFFSET);
  409. writel_relaxed(4096, pcie->apb_csr_base + MSI_SIZE_OFFSET);
  410. writel_relaxed(1, pcie->apb_csr_base + MSI_ENABLE_OFFSET);
  411. }
  412. static int mobiveil_host_init(struct mobiveil_pcie *pcie)
  413. {
  414. u32 value, pab_ctrl, type = 0;
  415. int err;
  416. struct resource_entry *win, *tmp;
  417. err = mobiveil_bringup_link(pcie);
  418. if (err) {
  419. dev_info(&pcie->pdev->dev, "link bring-up failed\n");
  420. return err;
  421. }
  422. /* setup bus numbers */
  423. value = csr_readl(pcie, PCI_PRIMARY_BUS);
  424. value &= 0xff000000;
  425. value |= 0x00ff0100;
  426. csr_writel(pcie, value, PCI_PRIMARY_BUS);
  427. /*
  428. * program Bus Master Enable Bit in Command Register in PAB Config
  429. * Space
  430. */
  431. value = csr_readl(pcie, PCI_COMMAND);
  432. csr_writel(pcie, value | PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  433. PCI_COMMAND_MASTER, PCI_COMMAND);
  434. /*
  435. * program PIO Enable Bit to 1 (and PEX PIO Enable to 1) in PAB_CTRL
  436. * register
  437. */
  438. pab_ctrl = csr_readl(pcie, PAB_CTRL);
  439. csr_writel(pcie, pab_ctrl | (1 << AMBA_PIO_ENABLE_SHIFT) |
  440. (1 << PEX_PIO_ENABLE_SHIFT), PAB_CTRL);
  441. csr_writel(pcie, (PAB_INTP_INTX_MASK | PAB_INTP_MSI_MASK),
  442. PAB_INTP_AMBA_MISC_ENB);
  443. /*
  444. * program PIO Enable Bit to 1 and Config Window Enable Bit to 1 in
  445. * PAB_AXI_PIO_CTRL Register
  446. */
  447. value = csr_readl(pcie, PAB_AXI_PIO_CTRL);
  448. csr_writel(pcie, value | APIO_EN_MASK, PAB_AXI_PIO_CTRL);
  449. /*
  450. * we'll program one outbound window for config reads and
  451. * another default inbound window for all the upstream traffic
  452. * rest of the outbound windows will be configured according to
  453. * the "ranges" field defined in device tree
  454. */
  455. /* config outbound translation window */
  456. program_ob_windows(pcie, pcie->ob_wins_configured,
  457. pcie->ob_io_res->start, 0, CFG_WINDOW_TYPE,
  458. resource_size(pcie->ob_io_res));
  459. /* memory inbound translation window */
  460. program_ib_windows(pcie, WIN_NUM_0, 0, MEM_WINDOW_TYPE, IB_WIN_SIZE);
  461. /* Get the I/O and memory ranges from DT */
  462. resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
  463. type = 0;
  464. if (resource_type(win->res) == IORESOURCE_MEM)
  465. type = MEM_WINDOW_TYPE;
  466. if (resource_type(win->res) == IORESOURCE_IO)
  467. type = IO_WINDOW_TYPE;
  468. if (type) {
  469. /* configure outbound translation window */
  470. program_ob_windows(pcie, pcie->ob_wins_configured,
  471. win->res->start,
  472. win->res->start - win->offset,
  473. type, resource_size(win->res));
  474. }
  475. }
  476. /* fixup for PCIe class register */
  477. value = csr_readl(pcie, PAB_INTP_AXI_PIO_CLASS);
  478. value &= 0xff;
  479. value |= (PCI_CLASS_BRIDGE_PCI << 16);
  480. csr_writel(pcie, value, PAB_INTP_AXI_PIO_CLASS);
  481. /* setup MSI hardware registers */
  482. mobiveil_pcie_enable_msi(pcie);
  483. return err;
  484. }
  485. static void mobiveil_mask_intx_irq(struct irq_data *data)
  486. {
  487. struct irq_desc *desc = irq_to_desc(data->irq);
  488. struct mobiveil_pcie *pcie;
  489. unsigned long flags;
  490. u32 mask, shifted_val;
  491. pcie = irq_desc_get_chip_data(desc);
  492. mask = 1 << ((data->hwirq + PAB_INTX_START) - 1);
  493. raw_spin_lock_irqsave(&pcie->intx_mask_lock, flags);
  494. shifted_val = csr_readl(pcie, PAB_INTP_AMBA_MISC_ENB);
  495. csr_writel(pcie, (shifted_val & (~mask)), PAB_INTP_AMBA_MISC_ENB);
  496. raw_spin_unlock_irqrestore(&pcie->intx_mask_lock, flags);
  497. }
  498. static void mobiveil_unmask_intx_irq(struct irq_data *data)
  499. {
  500. struct irq_desc *desc = irq_to_desc(data->irq);
  501. struct mobiveil_pcie *pcie;
  502. unsigned long flags;
  503. u32 shifted_val, mask;
  504. pcie = irq_desc_get_chip_data(desc);
  505. mask = 1 << ((data->hwirq + PAB_INTX_START) - 1);
  506. raw_spin_lock_irqsave(&pcie->intx_mask_lock, flags);
  507. shifted_val = csr_readl(pcie, PAB_INTP_AMBA_MISC_ENB);
  508. csr_writel(pcie, (shifted_val | mask), PAB_INTP_AMBA_MISC_ENB);
  509. raw_spin_unlock_irqrestore(&pcie->intx_mask_lock, flags);
  510. }
  511. static struct irq_chip intx_irq_chip = {
  512. .name = "mobiveil_pcie:intx",
  513. .irq_enable = mobiveil_unmask_intx_irq,
  514. .irq_disable = mobiveil_mask_intx_irq,
  515. .irq_mask = mobiveil_mask_intx_irq,
  516. .irq_unmask = mobiveil_unmask_intx_irq,
  517. };
  518. /* routine to setup the INTx related data */
  519. static int mobiveil_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
  520. irq_hw_number_t hwirq)
  521. {
  522. irq_set_chip_and_handler(irq, &intx_irq_chip, handle_level_irq);
  523. irq_set_chip_data(irq, domain->host_data);
  524. return 0;
  525. }
  526. /* INTx domain operations structure */
  527. static const struct irq_domain_ops intx_domain_ops = {
  528. .map = mobiveil_pcie_intx_map,
  529. };
  530. static struct irq_chip mobiveil_msi_irq_chip = {
  531. .name = "Mobiveil PCIe MSI",
  532. .irq_mask = pci_msi_mask_irq,
  533. .irq_unmask = pci_msi_unmask_irq,
  534. };
  535. static struct msi_domain_info mobiveil_msi_domain_info = {
  536. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  537. MSI_FLAG_PCI_MSIX),
  538. .chip = &mobiveil_msi_irq_chip,
  539. };
  540. static void mobiveil_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  541. {
  542. struct mobiveil_pcie *pcie = irq_data_get_irq_chip_data(data);
  543. phys_addr_t addr = pcie->pcie_reg_base + (data->hwirq * sizeof(int));
  544. msg->address_lo = lower_32_bits(addr);
  545. msg->address_hi = upper_32_bits(addr);
  546. msg->data = data->hwirq;
  547. dev_dbg(&pcie->pdev->dev, "msi#%d address_hi %#x address_lo %#x\n",
  548. (int)data->hwirq, msg->address_hi, msg->address_lo);
  549. }
  550. static int mobiveil_msi_set_affinity(struct irq_data *irq_data,
  551. const struct cpumask *mask, bool force)
  552. {
  553. return -EINVAL;
  554. }
  555. static struct irq_chip mobiveil_msi_bottom_irq_chip = {
  556. .name = "Mobiveil MSI",
  557. .irq_compose_msi_msg = mobiveil_compose_msi_msg,
  558. .irq_set_affinity = mobiveil_msi_set_affinity,
  559. };
  560. static int mobiveil_irq_msi_domain_alloc(struct irq_domain *domain,
  561. unsigned int virq, unsigned int nr_irqs, void *args)
  562. {
  563. struct mobiveil_pcie *pcie = domain->host_data;
  564. struct mobiveil_msi *msi = &pcie->msi;
  565. unsigned long bit;
  566. WARN_ON(nr_irqs != 1);
  567. mutex_lock(&msi->lock);
  568. bit = find_first_zero_bit(msi->msi_irq_in_use, msi->num_of_vectors);
  569. if (bit >= msi->num_of_vectors) {
  570. mutex_unlock(&msi->lock);
  571. return -ENOSPC;
  572. }
  573. set_bit(bit, msi->msi_irq_in_use);
  574. mutex_unlock(&msi->lock);
  575. irq_domain_set_info(domain, virq, bit, &mobiveil_msi_bottom_irq_chip,
  576. domain->host_data, handle_level_irq,
  577. NULL, NULL);
  578. return 0;
  579. }
  580. static void mobiveil_irq_msi_domain_free(struct irq_domain *domain,
  581. unsigned int virq, unsigned int nr_irqs)
  582. {
  583. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  584. struct mobiveil_pcie *pcie = irq_data_get_irq_chip_data(d);
  585. struct mobiveil_msi *msi = &pcie->msi;
  586. mutex_lock(&msi->lock);
  587. if (!test_bit(d->hwirq, msi->msi_irq_in_use)) {
  588. dev_err(&pcie->pdev->dev, "trying to free unused MSI#%lu\n",
  589. d->hwirq);
  590. } else {
  591. __clear_bit(d->hwirq, msi->msi_irq_in_use);
  592. }
  593. mutex_unlock(&msi->lock);
  594. }
  595. static const struct irq_domain_ops msi_domain_ops = {
  596. .alloc = mobiveil_irq_msi_domain_alloc,
  597. .free = mobiveil_irq_msi_domain_free,
  598. };
  599. static int mobiveil_allocate_msi_domains(struct mobiveil_pcie *pcie)
  600. {
  601. struct device *dev = &pcie->pdev->dev;
  602. struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
  603. struct mobiveil_msi *msi = &pcie->msi;
  604. mutex_init(&pcie->msi.lock);
  605. msi->dev_domain = irq_domain_add_linear(NULL, msi->num_of_vectors,
  606. &msi_domain_ops, pcie);
  607. if (!msi->dev_domain) {
  608. dev_err(dev, "failed to create IRQ domain\n");
  609. return -ENOMEM;
  610. }
  611. msi->msi_domain = pci_msi_create_irq_domain(fwnode,
  612. &mobiveil_msi_domain_info, msi->dev_domain);
  613. if (!msi->msi_domain) {
  614. dev_err(dev, "failed to create MSI domain\n");
  615. irq_domain_remove(msi->dev_domain);
  616. return -ENOMEM;
  617. }
  618. return 0;
  619. }
  620. static int mobiveil_pcie_init_irq_domain(struct mobiveil_pcie *pcie)
  621. {
  622. struct device *dev = &pcie->pdev->dev;
  623. struct device_node *node = dev->of_node;
  624. int ret;
  625. /* setup INTx */
  626. pcie->intx_domain = irq_domain_add_linear(node,
  627. PCI_NUM_INTX, &intx_domain_ops, pcie);
  628. if (!pcie->intx_domain) {
  629. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  630. return -ENODEV;
  631. }
  632. raw_spin_lock_init(&pcie->intx_mask_lock);
  633. /* setup MSI */
  634. ret = mobiveil_allocate_msi_domains(pcie);
  635. if (ret)
  636. return ret;
  637. return 0;
  638. }
  639. static int mobiveil_pcie_probe(struct platform_device *pdev)
  640. {
  641. struct mobiveil_pcie *pcie;
  642. struct pci_bus *bus;
  643. struct pci_bus *child;
  644. struct pci_host_bridge *bridge;
  645. struct device *dev = &pdev->dev;
  646. resource_size_t iobase;
  647. int ret;
  648. /* allocate the PCIe port */
  649. bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  650. if (!bridge)
  651. return -ENODEV;
  652. pcie = pci_host_bridge_priv(bridge);
  653. if (!pcie)
  654. return -ENOMEM;
  655. pcie->pdev = pdev;
  656. ret = mobiveil_pcie_parse_dt(pcie);
  657. if (ret) {
  658. dev_err(dev, "Parsing DT failed, ret: %x\n", ret);
  659. return ret;
  660. }
  661. INIT_LIST_HEAD(&pcie->resources);
  662. /* parse the host bridge base addresses from the device tree file */
  663. ret = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
  664. &pcie->resources, &iobase);
  665. if (ret) {
  666. dev_err(dev, "Getting bridge resources failed\n");
  667. return -ENOMEM;
  668. }
  669. /*
  670. * configure all inbound and outbound windows and prepare the RC for
  671. * config access
  672. */
  673. ret = mobiveil_host_init(pcie);
  674. if (ret) {
  675. dev_err(dev, "Failed to initialize host\n");
  676. goto error;
  677. }
  678. /* initialize the IRQ domains */
  679. ret = mobiveil_pcie_init_irq_domain(pcie);
  680. if (ret) {
  681. dev_err(dev, "Failed creating IRQ Domain\n");
  682. goto error;
  683. }
  684. ret = devm_request_pci_bus_resources(dev, &pcie->resources);
  685. if (ret)
  686. goto error;
  687. /* Initialize bridge */
  688. list_splice_init(&pcie->resources, &bridge->windows);
  689. bridge->dev.parent = dev;
  690. bridge->sysdata = pcie;
  691. bridge->busnr = pcie->root_bus_nr;
  692. bridge->ops = &mobiveil_pcie_ops;
  693. bridge->map_irq = of_irq_parse_and_map_pci;
  694. bridge->swizzle_irq = pci_common_swizzle;
  695. /* setup the kernel resources for the newly added PCIe root bus */
  696. ret = pci_scan_root_bus_bridge(bridge);
  697. if (ret)
  698. goto error;
  699. bus = bridge->bus;
  700. pci_assign_unassigned_bus_resources(bus);
  701. list_for_each_entry(child, &bus->children, node)
  702. pcie_bus_configure_settings(child);
  703. pci_bus_add_devices(bus);
  704. return 0;
  705. error:
  706. pci_free_resource_list(&pcie->resources);
  707. return ret;
  708. }
  709. static const struct of_device_id mobiveil_pcie_of_match[] = {
  710. {.compatible = "mbvl,gpex40-pcie",},
  711. {},
  712. };
  713. MODULE_DEVICE_TABLE(of, mobiveil_pcie_of_match);
  714. static struct platform_driver mobiveil_pcie_driver = {
  715. .probe = mobiveil_pcie_probe,
  716. .driver = {
  717. .name = "mobiveil-pcie",
  718. .of_match_table = mobiveil_pcie_of_match,
  719. .suppress_bind_attrs = true,
  720. },
  721. };
  722. builtin_platform_driver(mobiveil_pcie_driver);
  723. MODULE_LICENSE("GPL v2");
  724. MODULE_DESCRIPTION("Mobiveil PCIe host controller driver");
  725. MODULE_AUTHOR("Subrahmanya Lingappa <l.subrahmanya@mobiveil.co.in>");