pci-aardvark.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Aardvark PCIe controller, used on Marvell Armada
  4. * 3700.
  5. *
  6. * Copyright (C) 2016 Marvell
  7. *
  8. * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/kernel.h>
  15. #include <linux/pci.h>
  16. #include <linux/init.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_pci.h>
  20. #include "../pci.h"
  21. /* PCIe core registers */
  22. #define PCIE_CORE_CMD_STATUS_REG 0x4
  23. #define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
  24. #define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
  25. #define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
  26. #define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
  27. #define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
  28. #define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT 5
  29. #define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
  30. #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
  31. #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ 0x2
  32. #define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
  33. #define PCIE_CORE_LINK_L0S_ENTRY BIT(0)
  34. #define PCIE_CORE_LINK_TRAINING BIT(5)
  35. #define PCIE_CORE_LINK_WIDTH_SHIFT 20
  36. #define PCIE_CORE_ERR_CAPCTL_REG 0x118
  37. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
  38. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
  39. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
  40. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
  41. /* PIO registers base address and register offsets */
  42. #define PIO_BASE_ADDR 0x4000
  43. #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
  44. #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
  45. #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
  46. #define PIO_STAT (PIO_BASE_ADDR + 0x4)
  47. #define PIO_COMPLETION_STATUS_SHIFT 7
  48. #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
  49. #define PIO_COMPLETION_STATUS_OK 0
  50. #define PIO_COMPLETION_STATUS_UR 1
  51. #define PIO_COMPLETION_STATUS_CRS 2
  52. #define PIO_COMPLETION_STATUS_CA 4
  53. #define PIO_NON_POSTED_REQ BIT(0)
  54. #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
  55. #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
  56. #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
  57. #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
  58. #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
  59. #define PIO_START (PIO_BASE_ADDR + 0x1c)
  60. #define PIO_ISR (PIO_BASE_ADDR + 0x20)
  61. #define PIO_ISRM (PIO_BASE_ADDR + 0x24)
  62. /* Aardvark Control registers */
  63. #define CONTROL_BASE_ADDR 0x4800
  64. #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
  65. #define PCIE_GEN_SEL_MSK 0x3
  66. #define PCIE_GEN_SEL_SHIFT 0x0
  67. #define SPEED_GEN_1 0
  68. #define SPEED_GEN_2 1
  69. #define SPEED_GEN_3 2
  70. #define IS_RC_MSK 1
  71. #define IS_RC_SHIFT 2
  72. #define LANE_CNT_MSK 0x18
  73. #define LANE_CNT_SHIFT 0x3
  74. #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
  75. #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
  76. #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
  77. #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
  78. #define LINK_TRAINING_EN BIT(6)
  79. #define LEGACY_INTA BIT(28)
  80. #define LEGACY_INTB BIT(29)
  81. #define LEGACY_INTC BIT(30)
  82. #define LEGACY_INTD BIT(31)
  83. #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
  84. #define HOT_RESET_GEN BIT(0)
  85. #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
  86. #define PCIE_CORE_CTRL2_RESERVED 0x7
  87. #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
  88. #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
  89. #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
  90. #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
  91. #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
  92. #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
  93. #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
  94. #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
  95. #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
  96. #define PCIE_ISR0_ALL_MASK GENMASK(26, 0)
  97. #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
  98. #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
  99. #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
  100. #define PCIE_ISR1_FLUSH BIT(5)
  101. #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
  102. #define PCIE_ISR1_ALL_MASK GENMASK(11, 4)
  103. #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
  104. #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
  105. #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
  106. #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
  107. #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
  108. /* PCIe window configuration */
  109. #define OB_WIN_BASE_ADDR 0x4c00
  110. #define OB_WIN_BLOCK_SIZE 0x20
  111. #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
  112. OB_WIN_BLOCK_SIZE * (win) + \
  113. (offset))
  114. #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
  115. #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
  116. #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
  117. #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
  118. #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
  119. #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
  120. #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
  121. /* PCIe window types */
  122. #define OB_PCIE_MEM 0x0
  123. #define OB_PCIE_IO 0x4
  124. /* LMI registers base address and register offsets */
  125. #define LMI_BASE_ADDR 0x6000
  126. #define CFG_REG (LMI_BASE_ADDR + 0x0)
  127. #define LTSSM_SHIFT 24
  128. #define LTSSM_MASK 0x3f
  129. #define LTSSM_L0 0x10
  130. #define RC_BAR_CONFIG 0x300
  131. /* PCIe core controller registers */
  132. #define CTRL_CORE_BASE_ADDR 0x18000
  133. #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
  134. #define CTRL_MODE_SHIFT 0x0
  135. #define CTRL_MODE_MASK 0x1
  136. #define PCIE_CORE_MODE_DIRECT 0x0
  137. #define PCIE_CORE_MODE_COMMAND 0x1
  138. /* PCIe Central Interrupts Registers */
  139. #define CENTRAL_INT_BASE_ADDR 0x1b000
  140. #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
  141. #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
  142. #define PCIE_IRQ_CMDQ_INT BIT(0)
  143. #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
  144. #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
  145. #define PCIE_IRQ_DMA_INT BIT(4)
  146. #define PCIE_IRQ_IB_DXFERDONE BIT(5)
  147. #define PCIE_IRQ_OB_DXFERDONE BIT(6)
  148. #define PCIE_IRQ_OB_RXFERDONE BIT(7)
  149. #define PCIE_IRQ_COMPQ_INT BIT(12)
  150. #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
  151. #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
  152. #define PCIE_IRQ_CORE_INT BIT(16)
  153. #define PCIE_IRQ_CORE_INT_PIO BIT(17)
  154. #define PCIE_IRQ_DPMU_INT BIT(18)
  155. #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
  156. #define PCIE_IRQ_MSI_INT1_DET BIT(20)
  157. #define PCIE_IRQ_MSI_INT2_DET BIT(21)
  158. #define PCIE_IRQ_RC_DBELL_DET BIT(22)
  159. #define PCIE_IRQ_EP_STATUS BIT(23)
  160. #define PCIE_IRQ_ALL_MASK 0xfff0fb
  161. #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
  162. /* Transaction types */
  163. #define PCIE_CONFIG_RD_TYPE0 0x8
  164. #define PCIE_CONFIG_RD_TYPE1 0x9
  165. #define PCIE_CONFIG_WR_TYPE0 0xa
  166. #define PCIE_CONFIG_WR_TYPE1 0xb
  167. #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
  168. #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
  169. #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
  170. #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
  171. #define PCIE_CONF_ADDR(bus, devfn, where) \
  172. (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
  173. PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
  174. #define PIO_TIMEOUT_MS 1
  175. #define LINK_WAIT_MAX_RETRIES 10
  176. #define LINK_WAIT_USLEEP_MIN 90000
  177. #define LINK_WAIT_USLEEP_MAX 100000
  178. #define MSI_IRQ_NUM 32
  179. struct advk_pcie {
  180. struct platform_device *pdev;
  181. void __iomem *base;
  182. struct list_head resources;
  183. struct irq_domain *irq_domain;
  184. struct irq_chip irq_chip;
  185. struct irq_domain *msi_domain;
  186. struct irq_domain *msi_inner_domain;
  187. struct irq_chip msi_bottom_irq_chip;
  188. struct irq_chip msi_irq_chip;
  189. struct msi_domain_info msi_domain_info;
  190. DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
  191. struct mutex msi_used_lock;
  192. u16 msi_msg;
  193. int root_bus_nr;
  194. };
  195. static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
  196. {
  197. writel(val, pcie->base + reg);
  198. }
  199. static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
  200. {
  201. return readl(pcie->base + reg);
  202. }
  203. static int advk_pcie_link_up(struct advk_pcie *pcie)
  204. {
  205. u32 val, ltssm_state;
  206. val = advk_readl(pcie, CFG_REG);
  207. ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
  208. return ltssm_state >= LTSSM_L0;
  209. }
  210. static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
  211. {
  212. struct device *dev = &pcie->pdev->dev;
  213. int retries;
  214. /* check if the link is up or not */
  215. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  216. if (advk_pcie_link_up(pcie)) {
  217. dev_info(dev, "link up\n");
  218. return 0;
  219. }
  220. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  221. }
  222. dev_err(dev, "link never came up\n");
  223. return -ETIMEDOUT;
  224. }
  225. /*
  226. * Set PCIe address window register which could be used for memory
  227. * mapping.
  228. */
  229. static void advk_pcie_set_ob_win(struct advk_pcie *pcie,
  230. u32 win_num, u32 match_ms,
  231. u32 match_ls, u32 mask_ms,
  232. u32 mask_ls, u32 remap_ms,
  233. u32 remap_ls, u32 action)
  234. {
  235. advk_writel(pcie, match_ls, OB_WIN_MATCH_LS(win_num));
  236. advk_writel(pcie, match_ms, OB_WIN_MATCH_MS(win_num));
  237. advk_writel(pcie, mask_ms, OB_WIN_MASK_MS(win_num));
  238. advk_writel(pcie, mask_ls, OB_WIN_MASK_LS(win_num));
  239. advk_writel(pcie, remap_ms, OB_WIN_REMAP_MS(win_num));
  240. advk_writel(pcie, remap_ls, OB_WIN_REMAP_LS(win_num));
  241. advk_writel(pcie, action, OB_WIN_ACTIONS(win_num));
  242. advk_writel(pcie, match_ls | BIT(0), OB_WIN_MATCH_LS(win_num));
  243. }
  244. static void advk_pcie_setup_hw(struct advk_pcie *pcie)
  245. {
  246. u32 reg;
  247. int i;
  248. /* Point PCIe unit MBUS decode windows to DRAM space */
  249. for (i = 0; i < 8; i++)
  250. advk_pcie_set_ob_win(pcie, i, 0, 0, 0, 0, 0, 0, 0);
  251. /* Set to Direct mode */
  252. reg = advk_readl(pcie, CTRL_CONFIG_REG);
  253. reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
  254. reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
  255. advk_writel(pcie, reg, CTRL_CONFIG_REG);
  256. /* Set PCI global control register to RC mode */
  257. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  258. reg |= (IS_RC_MSK << IS_RC_SHIFT);
  259. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  260. /* Set Advanced Error Capabilities and Control PF0 register */
  261. reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
  262. PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
  263. PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
  264. PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
  265. advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
  266. /* Set PCIe Device Control and Status 1 PF0 register */
  267. reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
  268. (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
  269. PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
  270. (PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ <<
  271. PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT);
  272. advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
  273. /* Program PCIe Control 2 to disable strict ordering */
  274. reg = PCIE_CORE_CTRL2_RESERVED |
  275. PCIE_CORE_CTRL2_TD_ENABLE;
  276. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  277. /* Set GEN2 */
  278. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  279. reg &= ~PCIE_GEN_SEL_MSK;
  280. reg |= SPEED_GEN_2;
  281. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  282. /* Set lane X1 */
  283. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  284. reg &= ~LANE_CNT_MSK;
  285. reg |= LANE_COUNT_1;
  286. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  287. /* Enable link training */
  288. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  289. reg |= LINK_TRAINING_EN;
  290. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  291. /* Enable MSI */
  292. reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
  293. reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
  294. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  295. /* Clear all interrupts */
  296. advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
  297. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
  298. advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
  299. /* Disable All ISR0/1 Sources */
  300. reg = PCIE_ISR0_ALL_MASK;
  301. reg &= ~PCIE_ISR0_MSI_INT_PENDING;
  302. advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
  303. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
  304. /* Unmask all MSI's */
  305. advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
  306. /* Enable summary interrupt for GIC SPI source */
  307. reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
  308. advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
  309. reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
  310. reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
  311. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  312. /* Bypass the address window mapping for PIO */
  313. reg = advk_readl(pcie, PIO_CTRL);
  314. reg |= PIO_CTRL_ADDR_WIN_DISABLE;
  315. advk_writel(pcie, reg, PIO_CTRL);
  316. /* Start link training */
  317. reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
  318. reg |= PCIE_CORE_LINK_TRAINING;
  319. advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
  320. advk_pcie_wait_for_link(pcie);
  321. reg = PCIE_CORE_LINK_L0S_ENTRY |
  322. (1 << PCIE_CORE_LINK_WIDTH_SHIFT);
  323. advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
  324. reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
  325. reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
  326. PCIE_CORE_CMD_IO_ACCESS_EN |
  327. PCIE_CORE_CMD_MEM_IO_REQ_EN;
  328. advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
  329. }
  330. static void advk_pcie_check_pio_status(struct advk_pcie *pcie)
  331. {
  332. struct device *dev = &pcie->pdev->dev;
  333. u32 reg;
  334. unsigned int status;
  335. char *strcomp_status, *str_posted;
  336. reg = advk_readl(pcie, PIO_STAT);
  337. status = (reg & PIO_COMPLETION_STATUS_MASK) >>
  338. PIO_COMPLETION_STATUS_SHIFT;
  339. if (!status)
  340. return;
  341. switch (status) {
  342. case PIO_COMPLETION_STATUS_UR:
  343. strcomp_status = "UR";
  344. break;
  345. case PIO_COMPLETION_STATUS_CRS:
  346. strcomp_status = "CRS";
  347. break;
  348. case PIO_COMPLETION_STATUS_CA:
  349. strcomp_status = "CA";
  350. break;
  351. default:
  352. strcomp_status = "Unknown";
  353. break;
  354. }
  355. if (reg & PIO_NON_POSTED_REQ)
  356. str_posted = "Non-posted";
  357. else
  358. str_posted = "Posted";
  359. dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
  360. str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
  361. }
  362. static int advk_pcie_wait_pio(struct advk_pcie *pcie)
  363. {
  364. struct device *dev = &pcie->pdev->dev;
  365. unsigned long timeout;
  366. timeout = jiffies + msecs_to_jiffies(PIO_TIMEOUT_MS);
  367. while (time_before(jiffies, timeout)) {
  368. u32 start, isr;
  369. start = advk_readl(pcie, PIO_START);
  370. isr = advk_readl(pcie, PIO_ISR);
  371. if (!start && isr)
  372. return 0;
  373. }
  374. dev_err(dev, "config read/write timed out\n");
  375. return -ETIMEDOUT;
  376. }
  377. static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
  378. int where, int size, u32 *val)
  379. {
  380. struct advk_pcie *pcie = bus->sysdata;
  381. u32 reg;
  382. int ret;
  383. if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0) {
  384. *val = 0xffffffff;
  385. return PCIBIOS_DEVICE_NOT_FOUND;
  386. }
  387. /* Start PIO */
  388. advk_writel(pcie, 0, PIO_START);
  389. advk_writel(pcie, 1, PIO_ISR);
  390. /* Program the control register */
  391. reg = advk_readl(pcie, PIO_CTRL);
  392. reg &= ~PIO_CTRL_TYPE_MASK;
  393. if (bus->number == pcie->root_bus_nr)
  394. reg |= PCIE_CONFIG_RD_TYPE0;
  395. else
  396. reg |= PCIE_CONFIG_RD_TYPE1;
  397. advk_writel(pcie, reg, PIO_CTRL);
  398. /* Program the address registers */
  399. reg = PCIE_CONF_ADDR(bus->number, devfn, where);
  400. advk_writel(pcie, reg, PIO_ADDR_LS);
  401. advk_writel(pcie, 0, PIO_ADDR_MS);
  402. /* Program the data strobe */
  403. advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
  404. /* Start the transfer */
  405. advk_writel(pcie, 1, PIO_START);
  406. ret = advk_pcie_wait_pio(pcie);
  407. if (ret < 0)
  408. return PCIBIOS_SET_FAILED;
  409. advk_pcie_check_pio_status(pcie);
  410. /* Get the read result */
  411. *val = advk_readl(pcie, PIO_RD_DATA);
  412. if (size == 1)
  413. *val = (*val >> (8 * (where & 3))) & 0xff;
  414. else if (size == 2)
  415. *val = (*val >> (8 * (where & 3))) & 0xffff;
  416. return PCIBIOS_SUCCESSFUL;
  417. }
  418. static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  419. int where, int size, u32 val)
  420. {
  421. struct advk_pcie *pcie = bus->sysdata;
  422. u32 reg;
  423. u32 data_strobe = 0x0;
  424. int offset;
  425. int ret;
  426. if ((bus->number == pcie->root_bus_nr) && PCI_SLOT(devfn) != 0)
  427. return PCIBIOS_DEVICE_NOT_FOUND;
  428. if (where % size)
  429. return PCIBIOS_SET_FAILED;
  430. /* Start PIO */
  431. advk_writel(pcie, 0, PIO_START);
  432. advk_writel(pcie, 1, PIO_ISR);
  433. /* Program the control register */
  434. reg = advk_readl(pcie, PIO_CTRL);
  435. reg &= ~PIO_CTRL_TYPE_MASK;
  436. if (bus->number == pcie->root_bus_nr)
  437. reg |= PCIE_CONFIG_WR_TYPE0;
  438. else
  439. reg |= PCIE_CONFIG_WR_TYPE1;
  440. advk_writel(pcie, reg, PIO_CTRL);
  441. /* Program the address registers */
  442. reg = PCIE_CONF_ADDR(bus->number, devfn, where);
  443. advk_writel(pcie, reg, PIO_ADDR_LS);
  444. advk_writel(pcie, 0, PIO_ADDR_MS);
  445. /* Calculate the write strobe */
  446. offset = where & 0x3;
  447. reg = val << (8 * offset);
  448. data_strobe = GENMASK(size - 1, 0) << offset;
  449. /* Program the data register */
  450. advk_writel(pcie, reg, PIO_WR_DATA);
  451. /* Program the data strobe */
  452. advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
  453. /* Start the transfer */
  454. advk_writel(pcie, 1, PIO_START);
  455. ret = advk_pcie_wait_pio(pcie);
  456. if (ret < 0)
  457. return PCIBIOS_SET_FAILED;
  458. advk_pcie_check_pio_status(pcie);
  459. return PCIBIOS_SUCCESSFUL;
  460. }
  461. static struct pci_ops advk_pcie_ops = {
  462. .read = advk_pcie_rd_conf,
  463. .write = advk_pcie_wr_conf,
  464. };
  465. static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
  466. struct msi_msg *msg)
  467. {
  468. struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
  469. phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg);
  470. msg->address_lo = lower_32_bits(msi_msg);
  471. msg->address_hi = upper_32_bits(msi_msg);
  472. msg->data = data->irq;
  473. }
  474. static int advk_msi_set_affinity(struct irq_data *irq_data,
  475. const struct cpumask *mask, bool force)
  476. {
  477. return -EINVAL;
  478. }
  479. static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
  480. unsigned int virq,
  481. unsigned int nr_irqs, void *args)
  482. {
  483. struct advk_pcie *pcie = domain->host_data;
  484. int hwirq, i;
  485. mutex_lock(&pcie->msi_used_lock);
  486. hwirq = bitmap_find_next_zero_area(pcie->msi_used, MSI_IRQ_NUM,
  487. 0, nr_irqs, 0);
  488. if (hwirq >= MSI_IRQ_NUM) {
  489. mutex_unlock(&pcie->msi_used_lock);
  490. return -ENOSPC;
  491. }
  492. bitmap_set(pcie->msi_used, hwirq, nr_irqs);
  493. mutex_unlock(&pcie->msi_used_lock);
  494. for (i = 0; i < nr_irqs; i++)
  495. irq_domain_set_info(domain, virq + i, hwirq + i,
  496. &pcie->msi_bottom_irq_chip,
  497. domain->host_data, handle_simple_irq,
  498. NULL, NULL);
  499. return hwirq;
  500. }
  501. static void advk_msi_irq_domain_free(struct irq_domain *domain,
  502. unsigned int virq, unsigned int nr_irqs)
  503. {
  504. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  505. struct advk_pcie *pcie = domain->host_data;
  506. mutex_lock(&pcie->msi_used_lock);
  507. bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs);
  508. mutex_unlock(&pcie->msi_used_lock);
  509. }
  510. static const struct irq_domain_ops advk_msi_domain_ops = {
  511. .alloc = advk_msi_irq_domain_alloc,
  512. .free = advk_msi_irq_domain_free,
  513. };
  514. static void advk_pcie_irq_mask(struct irq_data *d)
  515. {
  516. struct advk_pcie *pcie = d->domain->host_data;
  517. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  518. u32 mask;
  519. mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  520. mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
  521. advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
  522. }
  523. static void advk_pcie_irq_unmask(struct irq_data *d)
  524. {
  525. struct advk_pcie *pcie = d->domain->host_data;
  526. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  527. u32 mask;
  528. mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  529. mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
  530. advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
  531. }
  532. static int advk_pcie_irq_map(struct irq_domain *h,
  533. unsigned int virq, irq_hw_number_t hwirq)
  534. {
  535. struct advk_pcie *pcie = h->host_data;
  536. advk_pcie_irq_mask(irq_get_irq_data(virq));
  537. irq_set_status_flags(virq, IRQ_LEVEL);
  538. irq_set_chip_and_handler(virq, &pcie->irq_chip,
  539. handle_level_irq);
  540. irq_set_chip_data(virq, pcie);
  541. return 0;
  542. }
  543. static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
  544. .map = advk_pcie_irq_map,
  545. .xlate = irq_domain_xlate_onecell,
  546. };
  547. static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
  548. {
  549. struct device *dev = &pcie->pdev->dev;
  550. struct device_node *node = dev->of_node;
  551. struct irq_chip *bottom_ic, *msi_ic;
  552. struct msi_domain_info *msi_di;
  553. phys_addr_t msi_msg_phys;
  554. mutex_init(&pcie->msi_used_lock);
  555. bottom_ic = &pcie->msi_bottom_irq_chip;
  556. bottom_ic->name = "MSI";
  557. bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg;
  558. bottom_ic->irq_set_affinity = advk_msi_set_affinity;
  559. msi_ic = &pcie->msi_irq_chip;
  560. msi_ic->name = "advk-MSI";
  561. msi_di = &pcie->msi_domain_info;
  562. msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  563. MSI_FLAG_MULTI_PCI_MSI;
  564. msi_di->chip = msi_ic;
  565. msi_msg_phys = virt_to_phys(&pcie->msi_msg);
  566. advk_writel(pcie, lower_32_bits(msi_msg_phys),
  567. PCIE_MSI_ADDR_LOW_REG);
  568. advk_writel(pcie, upper_32_bits(msi_msg_phys),
  569. PCIE_MSI_ADDR_HIGH_REG);
  570. pcie->msi_inner_domain =
  571. irq_domain_add_linear(NULL, MSI_IRQ_NUM,
  572. &advk_msi_domain_ops, pcie);
  573. if (!pcie->msi_inner_domain)
  574. return -ENOMEM;
  575. pcie->msi_domain =
  576. pci_msi_create_irq_domain(of_node_to_fwnode(node),
  577. msi_di, pcie->msi_inner_domain);
  578. if (!pcie->msi_domain) {
  579. irq_domain_remove(pcie->msi_inner_domain);
  580. return -ENOMEM;
  581. }
  582. return 0;
  583. }
  584. static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
  585. {
  586. irq_domain_remove(pcie->msi_domain);
  587. irq_domain_remove(pcie->msi_inner_domain);
  588. }
  589. static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
  590. {
  591. struct device *dev = &pcie->pdev->dev;
  592. struct device_node *node = dev->of_node;
  593. struct device_node *pcie_intc_node;
  594. struct irq_chip *irq_chip;
  595. pcie_intc_node = of_get_next_child(node, NULL);
  596. if (!pcie_intc_node) {
  597. dev_err(dev, "No PCIe Intc node found\n");
  598. return -ENODEV;
  599. }
  600. irq_chip = &pcie->irq_chip;
  601. irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
  602. dev_name(dev));
  603. if (!irq_chip->name) {
  604. of_node_put(pcie_intc_node);
  605. return -ENOMEM;
  606. }
  607. irq_chip->irq_mask = advk_pcie_irq_mask;
  608. irq_chip->irq_mask_ack = advk_pcie_irq_mask;
  609. irq_chip->irq_unmask = advk_pcie_irq_unmask;
  610. pcie->irq_domain =
  611. irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
  612. &advk_pcie_irq_domain_ops, pcie);
  613. if (!pcie->irq_domain) {
  614. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  615. of_node_put(pcie_intc_node);
  616. return -ENOMEM;
  617. }
  618. return 0;
  619. }
  620. static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
  621. {
  622. irq_domain_remove(pcie->irq_domain);
  623. }
  624. static void advk_pcie_handle_msi(struct advk_pcie *pcie)
  625. {
  626. u32 msi_val, msi_mask, msi_status, msi_idx;
  627. u16 msi_data;
  628. msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
  629. msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
  630. msi_status = msi_val & ~msi_mask;
  631. for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
  632. if (!(BIT(msi_idx) & msi_status))
  633. continue;
  634. advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
  635. msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
  636. generic_handle_irq(msi_data);
  637. }
  638. advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
  639. PCIE_ISR0_REG);
  640. }
  641. static void advk_pcie_handle_int(struct advk_pcie *pcie)
  642. {
  643. u32 isr0_val, isr0_mask, isr0_status;
  644. u32 isr1_val, isr1_mask, isr1_status;
  645. int i, virq;
  646. isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
  647. isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  648. isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
  649. isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
  650. isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  651. isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
  652. if (!isr0_status && !isr1_status) {
  653. advk_writel(pcie, isr0_val, PCIE_ISR0_REG);
  654. advk_writel(pcie, isr1_val, PCIE_ISR1_REG);
  655. return;
  656. }
  657. /* Process MSI interrupts */
  658. if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
  659. advk_pcie_handle_msi(pcie);
  660. /* Process legacy interrupts */
  661. for (i = 0; i < PCI_NUM_INTX; i++) {
  662. if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
  663. continue;
  664. advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
  665. PCIE_ISR1_REG);
  666. virq = irq_find_mapping(pcie->irq_domain, i);
  667. generic_handle_irq(virq);
  668. }
  669. }
  670. static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
  671. {
  672. struct advk_pcie *pcie = arg;
  673. u32 status;
  674. status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
  675. if (!(status & PCIE_IRQ_CORE_INT))
  676. return IRQ_NONE;
  677. advk_pcie_handle_int(pcie);
  678. /* Clear interrupt */
  679. advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
  680. return IRQ_HANDLED;
  681. }
  682. static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
  683. {
  684. int err, res_valid = 0;
  685. struct device *dev = &pcie->pdev->dev;
  686. struct resource_entry *win, *tmp;
  687. resource_size_t iobase;
  688. INIT_LIST_HEAD(&pcie->resources);
  689. err = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
  690. &pcie->resources, &iobase);
  691. if (err)
  692. return err;
  693. err = devm_request_pci_bus_resources(dev, &pcie->resources);
  694. if (err)
  695. goto out_release_res;
  696. resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
  697. struct resource *res = win->res;
  698. switch (resource_type(res)) {
  699. case IORESOURCE_IO:
  700. advk_pcie_set_ob_win(pcie, 1,
  701. upper_32_bits(res->start),
  702. lower_32_bits(res->start),
  703. 0, 0xF8000000, 0,
  704. lower_32_bits(res->start),
  705. OB_PCIE_IO);
  706. err = pci_remap_iospace(res, iobase);
  707. if (err) {
  708. dev_warn(dev, "error %d: failed to map resource %pR\n",
  709. err, res);
  710. resource_list_destroy_entry(win);
  711. }
  712. break;
  713. case IORESOURCE_MEM:
  714. advk_pcie_set_ob_win(pcie, 0,
  715. upper_32_bits(res->start),
  716. lower_32_bits(res->start),
  717. 0x0, 0xF8000000, 0,
  718. lower_32_bits(res->start),
  719. (2 << 20) | OB_PCIE_MEM);
  720. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  721. break;
  722. case IORESOURCE_BUS:
  723. pcie->root_bus_nr = res->start;
  724. break;
  725. }
  726. }
  727. if (!res_valid) {
  728. dev_err(dev, "non-prefetchable memory resource required\n");
  729. err = -EINVAL;
  730. goto out_release_res;
  731. }
  732. return 0;
  733. out_release_res:
  734. pci_free_resource_list(&pcie->resources);
  735. return err;
  736. }
  737. static int advk_pcie_probe(struct platform_device *pdev)
  738. {
  739. struct device *dev = &pdev->dev;
  740. struct advk_pcie *pcie;
  741. struct resource *res;
  742. struct pci_bus *bus, *child;
  743. struct pci_host_bridge *bridge;
  744. int ret, irq;
  745. bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
  746. if (!bridge)
  747. return -ENOMEM;
  748. pcie = pci_host_bridge_priv(bridge);
  749. pcie->pdev = pdev;
  750. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  751. pcie->base = devm_ioremap_resource(dev, res);
  752. if (IS_ERR(pcie->base))
  753. return PTR_ERR(pcie->base);
  754. irq = platform_get_irq(pdev, 0);
  755. ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
  756. IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
  757. pcie);
  758. if (ret) {
  759. dev_err(dev, "Failed to register interrupt\n");
  760. return ret;
  761. }
  762. ret = advk_pcie_parse_request_of_pci_ranges(pcie);
  763. if (ret) {
  764. dev_err(dev, "Failed to parse resources\n");
  765. return ret;
  766. }
  767. advk_pcie_setup_hw(pcie);
  768. ret = advk_pcie_init_irq_domain(pcie);
  769. if (ret) {
  770. dev_err(dev, "Failed to initialize irq\n");
  771. return ret;
  772. }
  773. ret = advk_pcie_init_msi_irq_domain(pcie);
  774. if (ret) {
  775. dev_err(dev, "Failed to initialize irq\n");
  776. advk_pcie_remove_irq_domain(pcie);
  777. return ret;
  778. }
  779. list_splice_init(&pcie->resources, &bridge->windows);
  780. bridge->dev.parent = dev;
  781. bridge->sysdata = pcie;
  782. bridge->busnr = 0;
  783. bridge->ops = &advk_pcie_ops;
  784. bridge->map_irq = of_irq_parse_and_map_pci;
  785. bridge->swizzle_irq = pci_common_swizzle;
  786. ret = pci_scan_root_bus_bridge(bridge);
  787. if (ret < 0) {
  788. advk_pcie_remove_msi_irq_domain(pcie);
  789. advk_pcie_remove_irq_domain(pcie);
  790. return ret;
  791. }
  792. bus = bridge->bus;
  793. pci_bus_assign_resources(bus);
  794. list_for_each_entry(child, &bus->children, node)
  795. pcie_bus_configure_settings(child);
  796. pci_bus_add_devices(bus);
  797. return 0;
  798. }
  799. static const struct of_device_id advk_pcie_of_match_table[] = {
  800. { .compatible = "marvell,armada-3700-pcie", },
  801. {},
  802. };
  803. static struct platform_driver advk_pcie_driver = {
  804. .driver = {
  805. .name = "advk-pcie",
  806. .of_match_table = advk_pcie_of_match_table,
  807. /* Driver unloading/unbinding currently not supported */
  808. .suppress_bind_attrs = true,
  809. },
  810. .probe = advk_pcie_probe,
  811. };
  812. builtin_platform_driver(advk_pcie_driver);