pci-aardvark.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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_LINK_CTRL_STAT_REG 0xd0
  34. #define PCIE_CORE_LINK_L0S_ENTRY BIT(0)
  35. #define PCIE_CORE_LINK_TRAINING BIT(5)
  36. #define PCIE_CORE_LINK_WIDTH_SHIFT 20
  37. #define PCIE_CORE_ERR_CAPCTL_REG 0x118
  38. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
  39. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
  40. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
  41. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
  42. /* PIO registers base address and register offsets */
  43. #define PIO_BASE_ADDR 0x4000
  44. #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
  45. #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
  46. #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
  47. #define PIO_STAT (PIO_BASE_ADDR + 0x4)
  48. #define PIO_COMPLETION_STATUS_SHIFT 7
  49. #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
  50. #define PIO_COMPLETION_STATUS_OK 0
  51. #define PIO_COMPLETION_STATUS_UR 1
  52. #define PIO_COMPLETION_STATUS_CRS 2
  53. #define PIO_COMPLETION_STATUS_CA 4
  54. #define PIO_NON_POSTED_REQ BIT(0)
  55. #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
  56. #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
  57. #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
  58. #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
  59. #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
  60. #define PIO_START (PIO_BASE_ADDR + 0x1c)
  61. #define PIO_ISR (PIO_BASE_ADDR + 0x20)
  62. #define PIO_ISRM (PIO_BASE_ADDR + 0x24)
  63. /* Aardvark Control registers */
  64. #define CONTROL_BASE_ADDR 0x4800
  65. #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
  66. #define PCIE_GEN_SEL_MSK 0x3
  67. #define PCIE_GEN_SEL_SHIFT 0x0
  68. #define SPEED_GEN_1 0
  69. #define SPEED_GEN_2 1
  70. #define SPEED_GEN_3 2
  71. #define IS_RC_MSK 1
  72. #define IS_RC_SHIFT 2
  73. #define LANE_CNT_MSK 0x18
  74. #define LANE_CNT_SHIFT 0x3
  75. #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
  76. #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
  77. #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
  78. #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
  79. #define LINK_TRAINING_EN BIT(6)
  80. #define LEGACY_INTA BIT(28)
  81. #define LEGACY_INTB BIT(29)
  82. #define LEGACY_INTC BIT(30)
  83. #define LEGACY_INTD BIT(31)
  84. #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
  85. #define HOT_RESET_GEN BIT(0)
  86. #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
  87. #define PCIE_CORE_CTRL2_RESERVED 0x7
  88. #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
  89. #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
  90. #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
  91. #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
  92. #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
  93. #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
  94. #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
  95. #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
  96. #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
  97. #define PCIE_ISR0_ALL_MASK GENMASK(26, 0)
  98. #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
  99. #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
  100. #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
  101. #define PCIE_ISR1_FLUSH BIT(5)
  102. #define PCIE_ISR1_ALL_MASK GENMASK(5, 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. /* PCI_BDF shifts 8bit, so we need extra 4bit shift */
  168. #define PCIE_BDF(dev) (dev << 4)
  169. #define PCIE_CONF_BUS(bus) (((bus) & 0xff) << 20)
  170. #define PCIE_CONF_DEV(dev) (((dev) & 0x1f) << 15)
  171. #define PCIE_CONF_FUNC(fun) (((fun) & 0x7) << 12)
  172. #define PCIE_CONF_REG(reg) ((reg) & 0xffc)
  173. #define PCIE_CONF_ADDR(bus, devfn, where) \
  174. (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
  175. PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where))
  176. #define PIO_TIMEOUT_MS 1
  177. #define LINK_WAIT_MAX_RETRIES 10
  178. #define LINK_WAIT_USLEEP_MIN 90000
  179. #define LINK_WAIT_USLEEP_MAX 100000
  180. #define LEGACY_IRQ_NUM 4
  181. #define MSI_IRQ_NUM 32
  182. struct advk_pcie {
  183. struct platform_device *pdev;
  184. void __iomem *base;
  185. struct list_head resources;
  186. struct irq_domain *irq_domain;
  187. struct irq_chip irq_chip;
  188. struct msi_controller msi;
  189. struct irq_domain *msi_domain;
  190. struct irq_chip msi_irq_chip;
  191. DECLARE_BITMAP(msi_irq_in_use, MSI_IRQ_NUM);
  192. struct mutex msi_used_lock;
  193. u16 msi_msg;
  194. int root_bus_nr;
  195. };
  196. static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
  197. {
  198. writel(val, pcie->base + reg);
  199. }
  200. static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
  201. {
  202. return readl(pcie->base + reg);
  203. }
  204. static int advk_pcie_link_up(struct advk_pcie *pcie)
  205. {
  206. u32 val, ltssm_state;
  207. val = advk_readl(pcie, CFG_REG);
  208. ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
  209. return ltssm_state >= LTSSM_L0;
  210. }
  211. static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
  212. {
  213. struct device *dev = &pcie->pdev->dev;
  214. int retries;
  215. /* check if the link is up or not */
  216. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  217. if (advk_pcie_link_up(pcie)) {
  218. dev_info(dev, "link up\n");
  219. return 0;
  220. }
  221. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  222. }
  223. dev_err(dev, "link never came up\n");
  224. return -ETIMEDOUT;
  225. }
  226. /*
  227. * Set PCIe address window register which could be used for memory
  228. * mapping.
  229. */
  230. static void advk_pcie_set_ob_win(struct advk_pcie *pcie,
  231. u32 win_num, u32 match_ms,
  232. u32 match_ls, u32 mask_ms,
  233. u32 mask_ls, u32 remap_ms,
  234. u32 remap_ls, u32 action)
  235. {
  236. advk_writel(pcie, match_ls, OB_WIN_MATCH_LS(win_num));
  237. advk_writel(pcie, match_ms, OB_WIN_MATCH_MS(win_num));
  238. advk_writel(pcie, mask_ms, OB_WIN_MASK_MS(win_num));
  239. advk_writel(pcie, mask_ls, OB_WIN_MASK_LS(win_num));
  240. advk_writel(pcie, remap_ms, OB_WIN_REMAP_MS(win_num));
  241. advk_writel(pcie, remap_ls, OB_WIN_REMAP_LS(win_num));
  242. advk_writel(pcie, action, OB_WIN_ACTIONS(win_num));
  243. advk_writel(pcie, match_ls | BIT(0), OB_WIN_MATCH_LS(win_num));
  244. }
  245. static void advk_pcie_setup_hw(struct advk_pcie *pcie)
  246. {
  247. u32 reg;
  248. int i;
  249. /* Point PCIe unit MBUS decode windows to DRAM space */
  250. for (i = 0; i < 8; i++)
  251. advk_pcie_set_ob_win(pcie, i, 0, 0, 0, 0, 0, 0, 0);
  252. /* Set to Direct mode */
  253. reg = advk_readl(pcie, CTRL_CONFIG_REG);
  254. reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
  255. reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
  256. advk_writel(pcie, reg, CTRL_CONFIG_REG);
  257. /* Set PCI global control register to RC mode */
  258. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  259. reg |= (IS_RC_MSK << IS_RC_SHIFT);
  260. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  261. /* Set Advanced Error Capabilities and Control PF0 register */
  262. reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
  263. PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
  264. PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
  265. PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
  266. advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
  267. /* Set PCIe Device Control and Status 1 PF0 register */
  268. reg = PCIE_CORE_DEV_CTRL_STATS_RELAX_ORDER_DISABLE |
  269. (7 << PCIE_CORE_DEV_CTRL_STATS_MAX_PAYLOAD_SZ_SHIFT) |
  270. PCIE_CORE_DEV_CTRL_STATS_SNOOP_DISABLE |
  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 (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_BDF(devfn) | PCIE_CONF_REG(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 (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);