pci-aardvark.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. 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(&pcie->pdev->dev, "link up\n");
  218. return 0;
  219. }
  220. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  221. }
  222. dev_err(&pcie->pdev->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_SIZE_SHIFT;
  271. advk_writel(pcie, reg, PCIE_CORE_DEV_CTRL_STATS_REG);
  272. /* Program PCIe Control 2 to disable strict ordering */
  273. reg = PCIE_CORE_CTRL2_RESERVED |
  274. PCIE_CORE_CTRL2_TD_ENABLE;
  275. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  276. /* Set GEN2 */
  277. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  278. reg &= ~PCIE_GEN_SEL_MSK;
  279. reg |= SPEED_GEN_2;
  280. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  281. /* Set lane X1 */
  282. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  283. reg &= ~LANE_CNT_MSK;
  284. reg |= LANE_COUNT_1;
  285. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  286. /* Enable link training */
  287. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  288. reg |= LINK_TRAINING_EN;
  289. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  290. /* Enable MSI */
  291. reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
  292. reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
  293. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  294. /* Clear all interrupts */
  295. advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
  296. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
  297. advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
  298. /* Disable All ISR0/1 Sources */
  299. reg = PCIE_ISR0_ALL_MASK;
  300. reg &= ~PCIE_ISR0_MSI_INT_PENDING;
  301. advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
  302. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
  303. /* Unmask all MSI's */
  304. advk_writel(pcie, 0, PCIE_MSI_MASK_REG);
  305. /* Enable summary interrupt for GIC SPI source */
  306. reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
  307. advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
  308. reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
  309. reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
  310. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  311. /* Bypass the address window mapping for PIO */
  312. reg = advk_readl(pcie, PIO_CTRL);
  313. reg |= PIO_CTRL_ADDR_WIN_DISABLE;
  314. advk_writel(pcie, reg, PIO_CTRL);
  315. /* Start link training */
  316. reg = advk_readl(pcie, PCIE_CORE_LINK_CTRL_STAT_REG);
  317. reg |= PCIE_CORE_LINK_TRAINING;
  318. advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
  319. advk_pcie_wait_for_link(pcie);
  320. reg = PCIE_CORE_LINK_L0S_ENTRY |
  321. (1 << PCIE_CORE_LINK_WIDTH_SHIFT);
  322. advk_writel(pcie, reg, PCIE_CORE_LINK_CTRL_STAT_REG);
  323. reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
  324. reg |= PCIE_CORE_CMD_MEM_ACCESS_EN |
  325. PCIE_CORE_CMD_IO_ACCESS_EN |
  326. PCIE_CORE_CMD_MEM_IO_REQ_EN;
  327. advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
  328. }
  329. static void advk_pcie_check_pio_status(struct advk_pcie *pcie)
  330. {
  331. u32 reg;
  332. unsigned int status;
  333. char *strcomp_status, *str_posted;
  334. reg = advk_readl(pcie, PIO_STAT);
  335. status = (reg & PIO_COMPLETION_STATUS_MASK) >>
  336. PIO_COMPLETION_STATUS_SHIFT;
  337. if (!status)
  338. return;
  339. switch (status) {
  340. case PIO_COMPLETION_STATUS_UR:
  341. strcomp_status = "UR";
  342. break;
  343. case PIO_COMPLETION_STATUS_CRS:
  344. strcomp_status = "CRS";
  345. break;
  346. case PIO_COMPLETION_STATUS_CA:
  347. strcomp_status = "CA";
  348. break;
  349. default:
  350. strcomp_status = "Unknown";
  351. break;
  352. }
  353. if (reg & PIO_NON_POSTED_REQ)
  354. str_posted = "Non-posted";
  355. else
  356. str_posted = "Posted";
  357. dev_err(&pcie->pdev->dev, "%s PIO Response Status: %s, %#x @ %#x\n",
  358. str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
  359. }
  360. static int advk_pcie_wait_pio(struct advk_pcie *pcie)
  361. {
  362. unsigned long timeout;
  363. timeout = jiffies + msecs_to_jiffies(PIO_TIMEOUT_MS);
  364. while (time_before(jiffies, timeout)) {
  365. u32 start, isr;
  366. start = advk_readl(pcie, PIO_START);
  367. isr = advk_readl(pcie, PIO_ISR);
  368. if (!start && isr)
  369. return 0;
  370. }
  371. dev_err(&pcie->pdev->dev, "config read/write timed out\n");
  372. return -ETIMEDOUT;
  373. }
  374. static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
  375. int where, int size, u32 *val)
  376. {
  377. struct advk_pcie *pcie = bus->sysdata;
  378. u32 reg;
  379. int ret;
  380. if (PCI_SLOT(devfn) != 0) {
  381. *val = 0xffffffff;
  382. return PCIBIOS_DEVICE_NOT_FOUND;
  383. }
  384. /* Start PIO */
  385. advk_writel(pcie, 0, PIO_START);
  386. advk_writel(pcie, 1, PIO_ISR);
  387. /* Program the control register */
  388. reg = advk_readl(pcie, PIO_CTRL);
  389. reg &= ~PIO_CTRL_TYPE_MASK;
  390. if (bus->number == pcie->root_bus_nr)
  391. reg |= PCIE_CONFIG_RD_TYPE0;
  392. else
  393. reg |= PCIE_CONFIG_RD_TYPE1;
  394. advk_writel(pcie, reg, PIO_CTRL);
  395. /* Program the address registers */
  396. reg = PCIE_BDF(devfn) | PCIE_CONF_REG(where);
  397. advk_writel(pcie, reg, PIO_ADDR_LS);
  398. advk_writel(pcie, 0, PIO_ADDR_MS);
  399. /* Program the data strobe */
  400. advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
  401. /* Start the transfer */
  402. advk_writel(pcie, 1, PIO_START);
  403. ret = advk_pcie_wait_pio(pcie);
  404. if (ret < 0)
  405. return PCIBIOS_SET_FAILED;
  406. advk_pcie_check_pio_status(pcie);
  407. /* Get the read result */
  408. *val = advk_readl(pcie, PIO_RD_DATA);
  409. if (size == 1)
  410. *val = (*val >> (8 * (where & 3))) & 0xff;
  411. else if (size == 2)
  412. *val = (*val >> (8 * (where & 3))) & 0xffff;
  413. return PCIBIOS_SUCCESSFUL;
  414. }
  415. static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  416. int where, int size, u32 val)
  417. {
  418. struct advk_pcie *pcie = bus->sysdata;
  419. u32 reg;
  420. u32 data_strobe = 0x0;
  421. int offset;
  422. int ret;
  423. if (PCI_SLOT(devfn) != 0)
  424. return PCIBIOS_DEVICE_NOT_FOUND;
  425. if (where % size)
  426. return PCIBIOS_SET_FAILED;
  427. /* Start PIO */
  428. advk_writel(pcie, 0, PIO_START);
  429. advk_writel(pcie, 1, PIO_ISR);
  430. /* Program the control register */
  431. reg = advk_readl(pcie, PIO_CTRL);
  432. reg &= ~PIO_CTRL_TYPE_MASK;
  433. if (bus->number == pcie->root_bus_nr)
  434. reg |= PCIE_CONFIG_WR_TYPE0;
  435. else
  436. reg |= PCIE_CONFIG_WR_TYPE1;
  437. advk_writel(pcie, reg, PIO_CTRL);
  438. /* Program the address registers */
  439. reg = PCIE_CONF_ADDR(bus->number, devfn, where);
  440. advk_writel(pcie, reg, PIO_ADDR_LS);
  441. advk_writel(pcie, 0, PIO_ADDR_MS);
  442. /* Calculate the write strobe */
  443. offset = where & 0x3;
  444. reg = val << (8 * offset);
  445. data_strobe = GENMASK(size - 1, 0) << offset;
  446. /* Program the data register */
  447. advk_writel(pcie, reg, PIO_WR_DATA);
  448. /* Program the data strobe */
  449. advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
  450. /* Start the transfer */
  451. advk_writel(pcie, 1, PIO_START);
  452. ret = advk_pcie_wait_pio(pcie);
  453. if (ret < 0)
  454. return PCIBIOS_SET_FAILED;
  455. advk_pcie_check_pio_status(pcie);
  456. return PCIBIOS_SUCCESSFUL;
  457. }
  458. static struct pci_ops advk_pcie_ops = {
  459. .read = advk_pcie_rd_conf,
  460. .write = advk_pcie_wr_conf,
  461. };
  462. static int advk_pcie_alloc_msi(struct advk_pcie *pcie)
  463. {
  464. int hwirq;
  465. mutex_lock(&pcie->msi_used_lock);
  466. hwirq = find_first_zero_bit(pcie->msi_irq_in_use, MSI_IRQ_NUM);
  467. if (hwirq >= MSI_IRQ_NUM)
  468. hwirq = -ENOSPC;
  469. else
  470. set_bit(hwirq, pcie->msi_irq_in_use);
  471. mutex_unlock(&pcie->msi_used_lock);
  472. return hwirq;
  473. }
  474. static void advk_pcie_free_msi(struct advk_pcie *pcie, int hwirq)
  475. {
  476. mutex_lock(&pcie->msi_used_lock);
  477. if (!test_bit(hwirq, pcie->msi_irq_in_use))
  478. dev_err(&pcie->pdev->dev, "trying to free unused MSI#%d\n",
  479. hwirq);
  480. else
  481. clear_bit(hwirq, pcie->msi_irq_in_use);
  482. mutex_unlock(&pcie->msi_used_lock);
  483. }
  484. static int advk_pcie_setup_msi_irq(struct msi_controller *chip,
  485. struct pci_dev *pdev,
  486. struct msi_desc *desc)
  487. {
  488. struct advk_pcie *pcie = pdev->bus->sysdata;
  489. struct msi_msg msg;
  490. int virq, hwirq;
  491. phys_addr_t msi_msg_phys;
  492. /* We support MSI, but not MSI-X */
  493. if (desc->msi_attrib.is_msix)
  494. return -EINVAL;
  495. hwirq = advk_pcie_alloc_msi(pcie);
  496. if (hwirq < 0)
  497. return hwirq;
  498. virq = irq_create_mapping(pcie->msi_domain, hwirq);
  499. if (!virq) {
  500. advk_pcie_free_msi(pcie, hwirq);
  501. return -EINVAL;
  502. }
  503. irq_set_msi_desc(virq, desc);
  504. msi_msg_phys = virt_to_phys(&pcie->msi_msg);
  505. msg.address_lo = lower_32_bits(msi_msg_phys);
  506. msg.address_hi = upper_32_bits(msi_msg_phys);
  507. msg.data = virq;
  508. pci_write_msi_msg(virq, &msg);
  509. return 0;
  510. }
  511. static void advk_pcie_teardown_msi_irq(struct msi_controller *chip,
  512. unsigned int irq)
  513. {
  514. struct irq_data *d = irq_get_irq_data(irq);
  515. struct msi_desc *msi = irq_data_get_msi_desc(d);
  516. struct advk_pcie *pcie = msi_desc_to_pci_sysdata(msi);
  517. unsigned long hwirq = d->hwirq;
  518. irq_dispose_mapping(irq);
  519. advk_pcie_free_msi(pcie, hwirq);
  520. }
  521. static int advk_pcie_msi_map(struct irq_domain *domain,
  522. unsigned int virq, irq_hw_number_t hw)
  523. {
  524. struct advk_pcie *pcie = domain->host_data;
  525. irq_set_chip_and_handler(virq, &pcie->msi_irq_chip,
  526. handle_simple_irq);
  527. return 0;
  528. }
  529. static const struct irq_domain_ops advk_pcie_msi_irq_ops = {
  530. .map = advk_pcie_msi_map,
  531. };
  532. static void advk_pcie_irq_mask(struct irq_data *d)
  533. {
  534. struct advk_pcie *pcie = d->domain->host_data;
  535. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  536. u32 mask;
  537. mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  538. mask |= PCIE_ISR0_INTX_ASSERT(hwirq);
  539. advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
  540. }
  541. static void advk_pcie_irq_unmask(struct irq_data *d)
  542. {
  543. struct advk_pcie *pcie = d->domain->host_data;
  544. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  545. u32 mask;
  546. mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  547. mask &= ~PCIE_ISR0_INTX_ASSERT(hwirq);
  548. advk_writel(pcie, mask, PCIE_ISR0_MASK_REG);
  549. }
  550. static int advk_pcie_irq_map(struct irq_domain *h,
  551. unsigned int virq, irq_hw_number_t hwirq)
  552. {
  553. struct advk_pcie *pcie = h->host_data;
  554. advk_pcie_irq_mask(irq_get_irq_data(virq));
  555. irq_set_status_flags(virq, IRQ_LEVEL);
  556. irq_set_chip_and_handler(virq, &pcie->irq_chip,
  557. handle_level_irq);
  558. irq_set_chip_data(virq, pcie);
  559. return 0;
  560. }
  561. static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
  562. .map = advk_pcie_irq_map,
  563. .xlate = irq_domain_xlate_onecell,
  564. };
  565. static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
  566. {
  567. struct device *dev = &pcie->pdev->dev;
  568. struct device_node *node = dev->of_node;
  569. struct irq_chip *msi_irq_chip;
  570. struct msi_controller *msi;
  571. phys_addr_t msi_msg_phys;
  572. int ret;
  573. msi_irq_chip = &pcie->msi_irq_chip;
  574. msi_irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-msi",
  575. dev_name(dev));
  576. if (!msi_irq_chip->name)
  577. return -ENOMEM;
  578. msi_irq_chip->irq_enable = pci_msi_unmask_irq;
  579. msi_irq_chip->irq_disable = pci_msi_mask_irq;
  580. msi_irq_chip->irq_mask = pci_msi_mask_irq;
  581. msi_irq_chip->irq_unmask = pci_msi_unmask_irq;
  582. msi = &pcie->msi;
  583. msi->setup_irq = advk_pcie_setup_msi_irq;
  584. msi->teardown_irq = advk_pcie_teardown_msi_irq;
  585. msi->of_node = node;
  586. mutex_init(&pcie->msi_used_lock);
  587. msi_msg_phys = virt_to_phys(&pcie->msi_msg);
  588. advk_writel(pcie, lower_32_bits(msi_msg_phys),
  589. PCIE_MSI_ADDR_LOW_REG);
  590. advk_writel(pcie, upper_32_bits(msi_msg_phys),
  591. PCIE_MSI_ADDR_HIGH_REG);
  592. pcie->msi_domain =
  593. irq_domain_add_linear(NULL, MSI_IRQ_NUM,
  594. &advk_pcie_msi_irq_ops, pcie);
  595. if (!pcie->msi_domain)
  596. return -ENOMEM;
  597. ret = of_pci_msi_chip_add(msi);
  598. if (ret < 0) {
  599. irq_domain_remove(pcie->msi_domain);
  600. return ret;
  601. }
  602. return 0;
  603. }
  604. static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
  605. {
  606. of_pci_msi_chip_remove(&pcie->msi);
  607. irq_domain_remove(pcie->msi_domain);
  608. }
  609. static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
  610. {
  611. struct device *dev = &pcie->pdev->dev;
  612. struct device_node *node = dev->of_node;
  613. struct device_node *pcie_intc_node;
  614. struct irq_chip *irq_chip;
  615. pcie_intc_node = of_get_next_child(node, NULL);
  616. if (!pcie_intc_node) {
  617. dev_err(dev, "No PCIe Intc node found\n");
  618. return -ENODEV;
  619. }
  620. irq_chip = &pcie->irq_chip;
  621. irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
  622. dev_name(dev));
  623. if (!irq_chip->name) {
  624. of_node_put(pcie_intc_node);
  625. return -ENOMEM;
  626. }
  627. irq_chip->irq_mask = advk_pcie_irq_mask;
  628. irq_chip->irq_mask_ack = advk_pcie_irq_mask;
  629. irq_chip->irq_unmask = advk_pcie_irq_unmask;
  630. pcie->irq_domain =
  631. irq_domain_add_linear(pcie_intc_node, LEGACY_IRQ_NUM,
  632. &advk_pcie_irq_domain_ops, pcie);
  633. if (!pcie->irq_domain) {
  634. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  635. of_node_put(pcie_intc_node);
  636. return -ENOMEM;
  637. }
  638. return 0;
  639. }
  640. static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
  641. {
  642. irq_domain_remove(pcie->irq_domain);
  643. }
  644. static void advk_pcie_handle_msi(struct advk_pcie *pcie)
  645. {
  646. u32 msi_val, msi_mask, msi_status, msi_idx;
  647. u16 msi_data;
  648. msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
  649. msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
  650. msi_status = msi_val & ~msi_mask;
  651. for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
  652. if (!(BIT(msi_idx) & msi_status))
  653. continue;
  654. advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
  655. msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF;
  656. generic_handle_irq(msi_data);
  657. }
  658. advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
  659. PCIE_ISR0_REG);
  660. }
  661. static void advk_pcie_handle_int(struct advk_pcie *pcie)
  662. {
  663. u32 val, mask, status;
  664. int i, virq;
  665. val = advk_readl(pcie, PCIE_ISR0_REG);
  666. mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  667. status = val & ((~mask) & PCIE_ISR0_ALL_MASK);
  668. if (!status) {
  669. advk_writel(pcie, val, PCIE_ISR0_REG);
  670. return;
  671. }
  672. /* Process MSI interrupts */
  673. if (status & PCIE_ISR0_MSI_INT_PENDING)
  674. advk_pcie_handle_msi(pcie);
  675. /* Process legacy interrupts */
  676. for (i = 0; i < LEGACY_IRQ_NUM; i++) {
  677. if (!(status & PCIE_ISR0_INTX_ASSERT(i)))
  678. continue;
  679. advk_writel(pcie, PCIE_ISR0_INTX_ASSERT(i),
  680. PCIE_ISR0_REG);
  681. virq = irq_find_mapping(pcie->irq_domain, i);
  682. generic_handle_irq(virq);
  683. }
  684. }
  685. static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
  686. {
  687. struct advk_pcie *pcie = arg;
  688. u32 status;
  689. status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
  690. if (!(status & PCIE_IRQ_CORE_INT))
  691. return IRQ_NONE;
  692. advk_pcie_handle_int(pcie);
  693. /* Clear interrupt */
  694. advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
  695. return IRQ_HANDLED;
  696. }
  697. static int advk_pcie_parse_request_of_pci_ranges(struct advk_pcie *pcie)
  698. {
  699. int err, res_valid = 0;
  700. struct device *dev = &pcie->pdev->dev;
  701. struct device_node *np = dev->of_node;
  702. struct resource_entry *win;
  703. resource_size_t iobase;
  704. INIT_LIST_HEAD(&pcie->resources);
  705. err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pcie->resources,
  706. &iobase);
  707. if (err)
  708. return err;
  709. err = devm_request_pci_bus_resources(dev, &pcie->resources);
  710. if (err)
  711. goto out_release_res;
  712. resource_list_for_each_entry(win, &pcie->resources) {
  713. struct resource *res = win->res;
  714. switch (resource_type(res)) {
  715. case IORESOURCE_IO:
  716. advk_pcie_set_ob_win(pcie, 1,
  717. upper_32_bits(res->start),
  718. lower_32_bits(res->start),
  719. 0, 0xF8000000, 0,
  720. lower_32_bits(res->start),
  721. OB_PCIE_IO);
  722. err = pci_remap_iospace(res, iobase);
  723. if (err)
  724. dev_warn(dev, "error %d: failed to map resource %pR\n",
  725. err, res);
  726. break;
  727. case IORESOURCE_MEM:
  728. advk_pcie_set_ob_win(pcie, 0,
  729. upper_32_bits(res->start),
  730. lower_32_bits(res->start),
  731. 0x0, 0xF8000000, 0,
  732. lower_32_bits(res->start),
  733. (2 << 20) | OB_PCIE_MEM);
  734. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  735. break;
  736. case IORESOURCE_BUS:
  737. pcie->root_bus_nr = res->start;
  738. break;
  739. }
  740. }
  741. if (!res_valid) {
  742. dev_err(dev, "non-prefetchable memory resource required\n");
  743. err = -EINVAL;
  744. goto out_release_res;
  745. }
  746. return 0;
  747. out_release_res:
  748. pci_free_resource_list(&pcie->resources);
  749. return err;
  750. }
  751. static int advk_pcie_probe(struct platform_device *pdev)
  752. {
  753. struct advk_pcie *pcie;
  754. struct resource *res;
  755. struct pci_bus *bus, *child;
  756. struct msi_controller *msi;
  757. struct device_node *msi_node;
  758. int ret, irq;
  759. pcie = devm_kzalloc(&pdev->dev, sizeof(struct advk_pcie),
  760. GFP_KERNEL);
  761. if (!pcie)
  762. return -ENOMEM;
  763. pcie->pdev = pdev;
  764. platform_set_drvdata(pdev, pcie);
  765. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  766. pcie->base = devm_ioremap_resource(&pdev->dev, res);
  767. if (IS_ERR(pcie->base)) {
  768. dev_err(&pdev->dev, "Failed to map registers\n");
  769. return PTR_ERR(pcie->base);
  770. }
  771. irq = platform_get_irq(pdev, 0);
  772. ret = devm_request_irq(&pdev->dev, irq, advk_pcie_irq_handler,
  773. IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
  774. pcie);
  775. if (ret) {
  776. dev_err(&pdev->dev, "Failed to register interrupt\n");
  777. return ret;
  778. }
  779. ret = advk_pcie_parse_request_of_pci_ranges(pcie);
  780. if (ret) {
  781. dev_err(&pdev->dev, "Failed to parse resources\n");
  782. return ret;
  783. }
  784. advk_pcie_setup_hw(pcie);
  785. ret = advk_pcie_init_irq_domain(pcie);
  786. if (ret) {
  787. dev_err(&pdev->dev, "Failed to initialize irq\n");
  788. return ret;
  789. }
  790. ret = advk_pcie_init_msi_irq_domain(pcie);
  791. if (ret) {
  792. dev_err(&pdev->dev, "Failed to initialize irq\n");
  793. advk_pcie_remove_irq_domain(pcie);
  794. return ret;
  795. }
  796. msi_node = of_parse_phandle(pdev->dev.of_node, "msi-parent", 0);
  797. if (msi_node)
  798. msi = of_pci_find_msi_chip_by_node(msi_node);
  799. else
  800. msi = NULL;
  801. bus = pci_scan_root_bus_msi(&pdev->dev, 0, &advk_pcie_ops,
  802. pcie, &pcie->resources, &pcie->msi);
  803. if (!bus) {
  804. advk_pcie_remove_msi_irq_domain(pcie);
  805. advk_pcie_remove_irq_domain(pcie);
  806. return -ENOMEM;
  807. }
  808. pci_bus_assign_resources(bus);
  809. list_for_each_entry(child, &bus->children, node)
  810. pcie_bus_configure_settings(child);
  811. pci_bus_add_devices(bus);
  812. return 0;
  813. }
  814. static const struct of_device_id advk_pcie_of_match_table[] = {
  815. { .compatible = "marvell,armada-3700-pcie", },
  816. {},
  817. };
  818. static struct platform_driver advk_pcie_driver = {
  819. .driver = {
  820. .name = "advk-pcie",
  821. .of_match_table = advk_pcie_of_match_table,
  822. /* Driver unloading/unbinding currently not supported */
  823. .suppress_bind_attrs = true,
  824. },
  825. .probe = advk_pcie_probe,
  826. };
  827. builtin_platform_driver(advk_pcie_driver);