pci-aardvark.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /*
  2. * Driver for the Aardvark PCIe controller, used on Marvell Armada
  3. * 3700.
  4. *
  5. * Copyright (C) 2016 Marvell
  6. *
  7. * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/init.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_pci.h>
  23. /* PCIe core registers */
  24. #define PCIE_CORE_CMD_STATUS_REG 0x4
  25. #define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0)
  26. #define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1)
  27. #define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2)
  28. #define PCIE_CORE_DEV_CTRL_STATS_REG 0xc8
  29. #define PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE (0 << 4)
  30. #define PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT 5
  31. #define PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE (0 << 11)
  32. #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SIZE_SHIFT 12
  33. #define PCIE_CORE_DEV_CTRL_STATS_MAX_RD_REQ_SZ 0x2
  34. #define PCIE_CORE_LINK_CTRL_STAT_REG 0xd0
  35. #define PCIE_CORE_LINK_L0S_ENTRY BIT(0)
  36. #define PCIE_CORE_LINK_TRAINING BIT(5)
  37. #define PCIE_CORE_LINK_WIDTH_SHIFT 20
  38. #define PCIE_CORE_ERR_CAPCTL_REG 0x118
  39. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
  40. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
  41. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
  42. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
  43. /* PIO registers base address and register offsets */
  44. #define PIO_BASE_ADDR 0x4000
  45. #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
  46. #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
  47. #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
  48. #define PIO_STAT (PIO_BASE_ADDR + 0x4)
  49. #define PIO_COMPLETION_STATUS_SHIFT 7
  50. #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
  51. #define PIO_COMPLETION_STATUS_OK 0
  52. #define PIO_COMPLETION_STATUS_UR 1
  53. #define PIO_COMPLETION_STATUS_CRS 2
  54. #define PIO_COMPLETION_STATUS_CA 4
  55. #define PIO_NON_POSTED_REQ BIT(0)
  56. #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
  57. #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
  58. #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
  59. #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
  60. #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
  61. #define PIO_START (PIO_BASE_ADDR + 0x1c)
  62. #define PIO_ISR (PIO_BASE_ADDR + 0x20)
  63. #define PIO_ISRM (PIO_BASE_ADDR + 0x24)
  64. /* Aardvark Control registers */
  65. #define CONTROL_BASE_ADDR 0x4800
  66. #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
  67. #define PCIE_GEN_SEL_MSK 0x3
  68. #define PCIE_GEN_SEL_SHIFT 0x0
  69. #define SPEED_GEN_1 0
  70. #define SPEED_GEN_2 1
  71. #define SPEED_GEN_3 2
  72. #define IS_RC_MSK 1
  73. #define IS_RC_SHIFT 2
  74. #define LANE_CNT_MSK 0x18
  75. #define LANE_CNT_SHIFT 0x3
  76. #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
  77. #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
  78. #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
  79. #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
  80. #define LINK_TRAINING_EN BIT(6)
  81. #define LEGACY_INTA BIT(28)
  82. #define LEGACY_INTB BIT(29)
  83. #define LEGACY_INTC BIT(30)
  84. #define LEGACY_INTD BIT(31)
  85. #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
  86. #define HOT_RESET_GEN BIT(0)
  87. #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
  88. #define PCIE_CORE_CTRL2_RESERVED 0x7
  89. #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
  90. #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
  91. #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
  92. #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
  93. #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
  94. #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
  95. #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
  96. #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
  97. #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
  98. #define PCIE_ISR0_ALL_MASK GENMASK(26, 0)
  99. #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
  100. #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
  101. #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
  102. #define PCIE_ISR1_FLUSH BIT(5)
  103. #define PCIE_ISR1_ALL_MASK GENMASK(5, 4)
  104. #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
  105. #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
  106. #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
  107. #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
  108. #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
  109. /* PCIe window configuration */
  110. #define OB_WIN_BASE_ADDR 0x4c00
  111. #define OB_WIN_BLOCK_SIZE 0x20
  112. #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
  113. OB_WIN_BLOCK_SIZE * (win) + \
  114. (offset))
  115. #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
  116. #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
  117. #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
  118. #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
  119. #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
  120. #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
  121. #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
  122. /* PCIe window types */
  123. #define OB_PCIE_MEM 0x0
  124. #define OB_PCIE_IO 0x4
  125. /* LMI registers base address and register offsets */
  126. #define LMI_BASE_ADDR 0x6000
  127. #define CFG_REG (LMI_BASE_ADDR + 0x0)
  128. #define LTSSM_SHIFT 24
  129. #define LTSSM_MASK 0x3f
  130. #define LTSSM_L0 0x10
  131. #define RC_BAR_CONFIG 0x300
  132. /* PCIe core controller registers */
  133. #define CTRL_CORE_BASE_ADDR 0x18000
  134. #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
  135. #define CTRL_MODE_SHIFT 0x0
  136. #define CTRL_MODE_MASK 0x1
  137. #define PCIE_CORE_MODE_DIRECT 0x0
  138. #define PCIE_CORE_MODE_COMMAND 0x1
  139. /* PCIe Central Interrupts Registers */
  140. #define CENTRAL_INT_BASE_ADDR 0x1b000
  141. #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
  142. #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
  143. #define PCIE_IRQ_CMDQ_INT BIT(0)
  144. #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
  145. #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
  146. #define PCIE_IRQ_DMA_INT BIT(4)
  147. #define PCIE_IRQ_IB_DXFERDONE BIT(5)
  148. #define PCIE_IRQ_OB_DXFERDONE BIT(6)
  149. #define PCIE_IRQ_OB_RXFERDONE BIT(7)
  150. #define PCIE_IRQ_COMPQ_INT BIT(12)
  151. #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
  152. #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
  153. #define PCIE_IRQ_CORE_INT BIT(16)
  154. #define PCIE_IRQ_CORE_INT_PIO BIT(17)
  155. #define PCIE_IRQ_DPMU_INT BIT(18)
  156. #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
  157. #define PCIE_IRQ_MSI_INT1_DET BIT(20)
  158. #define PCIE_IRQ_MSI_INT2_DET BIT(21)
  159. #define PCIE_IRQ_RC_DBELL_DET BIT(22)
  160. #define PCIE_IRQ_EP_STATUS BIT(23)
  161. #define PCIE_IRQ_ALL_MASK 0xfff0fb
  162. #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
  163. /* Transaction types */
  164. #define PCIE_CONFIG_RD_TYPE0 0x8
  165. #define PCIE_CONFIG_RD_TYPE1 0x9
  166. #define PCIE_CONFIG_WR_TYPE0 0xa
  167. #define PCIE_CONFIG_WR_TYPE1 0xb
  168. #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
  169. #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
  170. #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
  171. #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
  172. #define PCIE_CONF_ADDR(bus, devfn, where) \
  173. (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
  174. PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
  175. #define PIO_TIMEOUT_MS 1
  176. #define LINK_WAIT_MAX_RETRIES 10
  177. #define LINK_WAIT_USLEEP_MIN 90000
  178. #define LINK_WAIT_USLEEP_MAX 100000
  179. #define LEGACY_IRQ_NUM 4
  180. #define MSI_IRQ_NUM 32
  181. struct advk_pcie {
  182. struct platform_device *pdev;
  183. void __iomem *base;
  184. struct list_head resources;
  185. struct irq_domain *irq_domain;
  186. struct irq_chip irq_chip;
  187. struct msi_controller msi;
  188. struct irq_domain *msi_domain;
  189. struct irq_chip msi_irq_chip;
  190. DECLARE_BITMAP(msi_irq_in_use, 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 int advk_pcie_alloc_msi(struct advk_pcie *pcie)
  466. {
  467. int hwirq;
  468. mutex_lock(&pcie->msi_used_lock);
  469. hwirq = find_first_zero_bit(pcie->msi_irq_in_use, MSI_IRQ_NUM);
  470. if (hwirq >= MSI_IRQ_NUM)
  471. hwirq = -ENOSPC;
  472. else
  473. set_bit(hwirq, pcie->msi_irq_in_use);
  474. mutex_unlock(&pcie->msi_used_lock);
  475. return hwirq;
  476. }
  477. static void advk_pcie_free_msi(struct advk_pcie *pcie, int hwirq)
  478. {
  479. struct device *dev = &pcie->pdev->dev;
  480. mutex_lock(&pcie->msi_used_lock);
  481. if (!test_bit(hwirq, pcie->msi_irq_in_use))
  482. dev_err(dev, "trying to free unused MSI#%d\n", hwirq);
  483. else
  484. clear_bit(hwirq, pcie->msi_irq_in_use);
  485. mutex_unlock(&pcie->msi_used_lock);
  486. }
  487. static int advk_pcie_setup_msi_irq(struct msi_controller *chip,
  488. struct pci_dev *pdev,
  489. struct msi_desc *desc)
  490. {
  491. struct advk_pcie *pcie = pdev->bus->sysdata;
  492. struct msi_msg msg;
  493. int virq, hwirq;
  494. phys_addr_t msi_msg_phys;
  495. /* We support MSI, but not MSI-X */
  496. if (desc->msi_attrib.is_msix)
  497. return -EINVAL;
  498. hwirq = advk_pcie_alloc_msi(pcie);
  499. if (hwirq < 0)
  500. return hwirq;
  501. virq = irq_create_mapping(pcie->msi_domain, hwirq);
  502. if (!virq) {
  503. advk_pcie_free_msi(pcie, hwirq);
  504. return -EINVAL;
  505. }
  506. irq_set_msi_desc(virq, desc);
  507. msi_msg_phys = virt_to_phys(&pcie->msi_msg);
  508. msg.address_lo = lower_32_bits(msi_msg_phys);
  509. msg.address_hi = upper_32_bits(msi_msg_phys);
  510. msg.data = virq;
  511. pci_write_msi_msg(virq, &msg);
  512. return 0;
  513. }
  514. static void advk_pcie_teardown_msi_irq(struct msi_controller *chip,
  515. unsigned int irq)
  516. {
  517. struct irq_data *d = irq_get_irq_data(irq);
  518. struct msi_desc *msi = irq_data_get_msi_desc(d);
  519. struct advk_pcie *pcie = msi_desc_to_pci_sysdata(msi);
  520. unsigned long hwirq = d->hwirq;
  521. irq_dispose_mapping(irq);
  522. advk_pcie_free_msi(pcie, hwirq);
  523. }
  524. static int advk_pcie_msi_map(struct irq_domain *domain,
  525. unsigned int virq, irq_hw_number_t hw)
  526. {
  527. struct advk_pcie *pcie = domain->host_data;
  528. irq_set_chip_and_handler(virq, &pcie->msi_irq_chip,
  529. handle_simple_irq);
  530. return 0;
  531. }
  532. static const struct irq_domain_ops advk_pcie_msi_irq_ops = {
  533. .map = advk_pcie_msi_map,
  534. };
  535. static void advk_pcie_irq_mask(struct irq_data *d)
  536. {
  537. struct advk_pcie *pcie = d->domain->host_data;
  538. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  539. u32 mask;
  540. mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  541. mask |= PCIE_ISR0_INTX_ASSERT(hwirq);
  542. advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
  543. }
  544. static void advk_pcie_irq_unmask(struct irq_data *d)
  545. {
  546. struct advk_pcie *pcie = d->domain->host_data;
  547. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  548. u32 mask;
  549. mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  550. mask &= ~PCIE_ISR0_INTX_ASSERT(hwirq);
  551. advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
  552. }
  553. static int advk_pcie_irq_map(struct irq_domain *h,
  554. unsigned int virq, irq_hw_number_t hwirq)
  555. {
  556. struct advk_pcie *pcie = h->host_data;
  557. advk_pcie_irq_mask(irq_get_irq_data(virq));
  558. irq_set_status_flags(virq, IRQ_LEVEL);
  559. irq_set_chip_and_handler(virq, &pcie->irq_chip,
  560. handle_level_irq);
  561. irq_set_chip_data(virq, pcie);
  562. return 0;
  563. }
  564. static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
  565. .map = advk_pcie_irq_map,
  566. .xlate = irq_domain_xlate_onecell,
  567. };
  568. static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
  569. {
  570. struct device *dev = &pcie->pdev->dev;
  571. struct device_node *node = dev->of_node;
  572. struct irq_chip *msi_irq_chip;
  573. struct msi_controller *msi;
  574. phys_addr_t msi_msg_phys;
  575. int ret;
  576. msi_irq_chip = &pcie->msi_irq_chip;
  577. msi_irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-msi",
  578. dev_name(dev));
  579. if (!msi_irq_chip->name)
  580. return -ENOMEM;
  581. msi_irq_chip->irq_enable = pci_msi_unmask_irq;
  582. msi_irq_chip->irq_disable = pci_msi_mask_irq;
  583. msi_irq_chip->irq_mask = pci_msi_mask_irq;
  584. msi_irq_chip->irq_unmask = pci_msi_unmask_irq;
  585. msi = &pcie->msi;
  586. msi->setup_irq = advk_pcie_setup_msi_irq;
  587. msi->teardown_irq = advk_pcie_teardown_msi_irq;
  588. msi->of_node = node;
  589. mutex_init(&pcie->msi_used_lock);
  590. msi_msg_phys = virt_to_phys(&pcie->msi_msg);
  591. advk_writel(pcie, lower_32_bits(msi_msg_phys),
  592. PCIE_MSI_ADDR_LOW_REG);
  593. advk_writel(pcie, upper_32_bits(msi_msg_phys),
  594. PCIE_MSI_ADDR_HIGH_REG);
  595. pcie->msi_domain =
  596. irq_domain_add_linear(NULL, MSI_IRQ_NUM,
  597. &advk_pcie_msi_irq_ops, pcie);
  598. if (!pcie->msi_domain)
  599. return -ENOMEM;
  600. ret = of_pci_msi_chip_add(msi);
  601. if (ret < 0) {
  602. irq_domain_remove(pcie->msi_domain);
  603. return ret;
  604. }
  605. return 0;
  606. }
  607. static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
  608. {
  609. of_pci_msi_chip_remove(&pcie->msi);
  610. irq_domain_remove(pcie->msi_domain);
  611. }
  612. static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
  613. {
  614. struct device *dev = &pcie->pdev->dev;
  615. struct device_node *node = dev->of_node;
  616. struct device_node *pcie_intc_node;
  617. struct irq_chip *irq_chip;
  618. pcie_intc_node = of_get_next_child(node, NULL);
  619. if (!pcie_intc_node) {
  620. dev_err(dev, "No PCIe Intc node found\n");
  621. return -ENODEV;
  622. }
  623. irq_chip = &pcie->irq_chip;
  624. irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
  625. dev_name(dev));
  626. if (!irq_chip->name) {
  627. of_node_put(pcie_intc_node);
  628. return -ENOMEM;
  629. }
  630. irq_chip->irq_mask = advk_pcie_irq_mask;
  631. irq_chip->irq_mask_ack = advk_pcie_irq_mask;
  632. irq_chip->irq_unmask = advk_pcie_irq_unmask;
  633. pcie->irq_domain =
  634. irq_domain_add_linear(pcie_intc_node, LEGACY_IRQ_NUM,
  635. &advk_pcie_irq_domain_ops, pcie);
  636. if (!pcie->irq_domain) {
  637. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  638. of_node_put(pcie_intc_node);
  639. return -ENOMEM;
  640. }
  641. return 0;
  642. }
  643. static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
  644. {
  645. irq_domain_remove(pcie->irq_domain);
  646. }
  647. static void advk_pcie_handle_msi(struct advk_pcie *pcie)
  648. {
  649. u32 msi_val, msi_mask, msi_status, msi_idx;
  650. u16 msi_data;
  651. msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
  652. msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
  653. msi_status = msi_val & ~msi_mask;
  654. for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
  655. if (!(BIT(msi_idx) & msi_status))
  656. continue;
  657. advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
  658. msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
  659. generic_handle_irq(msi_data);
  660. }
  661. advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
  662. PCIE_ISR0_REG);
  663. }
  664. static void advk_pcie_handle_int(struct advk_pcie *pcie)
  665. {
  666. u32 val, mask, status;
  667. int i, virq;
  668. val = advk_readl(pcie, PCIE_ISR0_REG);
  669. mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  670. status = val & ((~mask) & PCIE_ISR0_ALL_MASK);
  671. if (!status) {
  672. advk_writel(pcie, val, PCIE_ISR0_REG);
  673. return;
  674. }
  675. /* Process MSI interrupts */
  676. if (status & PCIE_ISR0_MSI_INT_PENDING)
  677. advk_pcie_handle_msi(pcie);
  678. /* Process legacy interrupts */
  679. for (i = 0; i < LEGACY_IRQ_NUM; i++) {
  680. if (!(status & PCIE_ISR0_INTX_ASSERT(i)))
  681. continue;
  682. advk_writel(pcie, PCIE_ISR0_INTX_ASSERT(i),
  683. PCIE_ISR0_REG);
  684. virq = irq_find_mapping(pcie->irq_domain, i);
  685. generic_handle_irq(virq);
  686. }
  687. }
  688. static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
  689. {
  690. struct advk_pcie *pcie = arg;
  691. u32 status;
  692. status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
  693. if (!(status & PCIE_IRQ_CORE_INT))
  694. return IRQ_NONE;
  695. advk_pcie_handle_int(pcie);
  696. /* Clear interrupt */
  697. advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
  698. return IRQ_HANDLED;
  699. }
  700. static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
  701. {
  702. int err, res_valid = 0;
  703. struct device *dev = &pcie->pdev->dev;
  704. struct device_node *np = dev->of_node;
  705. struct resource_entry *win, *tmp;
  706. resource_size_t iobase;
  707. INIT_LIST_HEAD(&pcie->resources);
  708. err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
  709. &iobase);
  710. if (err)
  711. return err;
  712. err = devm_request_pci_bus_resources(dev, &pcie->resources);
  713. if (err)
  714. goto out_release_res;
  715. resource_list_for_each_entry_safe(win, tmp, &pcie->resources) {
  716. struct resource *res = win->res;
  717. switch (resource_type(res)) {
  718. case IORESOURCE_IO:
  719. advk_pcie_set_ob_win(pcie, 1,
  720. upper_32_bits(res->start),
  721. lower_32_bits(res->start),
  722. 0, 0xF8000000, 0,
  723. lower_32_bits(res->start),
  724. OB_PCIE_IO);
  725. err = pci_remap_iospace(res, iobase);
  726. if (err) {
  727. dev_warn(dev, "error %d: failed to map resource %pR\n",
  728. err, res);
  729. resource_list_destroy_entry(win);
  730. }
  731. break;
  732. case IORESOURCE_MEM:
  733. advk_pcie_set_ob_win(pcie, 0,
  734. upper_32_bits(res->start),
  735. lower_32_bits(res->start),
  736. 0x0, 0xF8000000, 0,
  737. lower_32_bits(res->start),
  738. (2 << 20) | OB_PCIE_MEM);
  739. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  740. break;
  741. case IORESOURCE_BUS:
  742. pcie->root_bus_nr = res->start;
  743. break;
  744. }
  745. }
  746. if (!res_valid) {
  747. dev_err(dev, "non-prefetchable memory resource required\n");
  748. err = -EINVAL;
  749. goto out_release_res;
  750. }
  751. return 0;
  752. out_release_res:
  753. pci_free_resource_list(&pcie->resources);
  754. return err;
  755. }
  756. static int advk_pcie_probe(struct platform_device *pdev)
  757. {
  758. struct device *dev = &pdev->dev;
  759. struct advk_pcie *pcie;
  760. struct resource *res;
  761. struct pci_bus *bus, *child;
  762. struct msi_controller *msi;
  763. struct device_node *msi_node;
  764. int ret, irq;
  765. pcie = devm_kzalloc(dev, sizeof(struct advk_pcie), GFP_KERNEL);
  766. if (!pcie)
  767. return -ENOMEM;
  768. pcie->pdev = pdev;
  769. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  770. pcie->base = devm_ioremap_resource(dev, res);
  771. if (IS_ERR(pcie->base))
  772. return PTR_ERR(pcie->base);
  773. irq = platform_get_irq(pdev, 0);
  774. ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
  775. IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
  776. pcie);
  777. if (ret) {
  778. dev_err(dev, "Failed to register interrupt\n");
  779. return ret;
  780. }
  781. ret = advk_pcie_parse_request_of_pci_ranges(pcie);
  782. if (ret) {
  783. dev_err(dev, "Failed to parse resources\n");
  784. return ret;
  785. }
  786. advk_pcie_setup_hw(pcie);
  787. ret = advk_pcie_init_irq_domain(pcie);
  788. if (ret) {
  789. dev_err(dev, "Failed to initialize irq\n");
  790. return ret;
  791. }
  792. ret = advk_pcie_init_msi_irq_domain(pcie);
  793. if (ret) {
  794. dev_err(dev, "Failed to initialize irq\n");
  795. advk_pcie_remove_irq_domain(pcie);
  796. return ret;
  797. }
  798. msi_node = of_parse_phandle(dev->of_node, "msi-parent", 0);
  799. if (msi_node)
  800. msi = of_pci_find_msi_chip_by_node(msi_node);
  801. else
  802. msi = NULL;
  803. bus = pci_scan_root_bus_msi(dev, 0, &advk_pcie_ops,
  804. pcie, &pcie->resources, &pcie->msi);
  805. if (!bus) {
  806. advk_pcie_remove_msi_irq_domain(pcie);
  807. advk_pcie_remove_irq_domain(pcie);
  808. return -ENOMEM;
  809. }
  810. pci_bus_assign_resources(bus);
  811. list_for_each_entry(child, &bus->children, node)
  812. pcie_bus_configure_settings(child);
  813. pci_bus_add_devices(bus);
  814. return 0;
  815. }
  816. static const struct of_device_id advk_pcie_of_match_table[] = {
  817. { .compatible = "marvell,armada-3700-pcie", },
  818. {},
  819. };
  820. static struct platform_driver advk_pcie_driver = {
  821. .driver = {
  822. .name = "advk-pcie",
  823. .of_match_table = advk_pcie_of_match_table,
  824. /* Driver unloading/unbinding currently not supported */
  825. .suppress_bind_attrs = true,
  826. },
  827. .probe = advk_pcie_probe,
  828. };
  829. builtin_platform_driver(advk_pcie_driver);