pcie-mobiveil.c 24 KB

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