pci-aardvark.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for the Aardvark PCIe controller, used on Marvell Armada
  4. * 3700.
  5. *
  6. * Copyright (C) 2016 Marvell
  7. *
  8. * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com>
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/delay.h>
  12. #include <linux/gpio/consumer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/irq.h>
  15. #include <linux/irqchip/irq-msi-lib.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/pci-ecam.h>
  21. #include <linux/init.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/msi.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_pci.h>
  27. #include "../pci.h"
  28. #include "../pci-bridge-emul.h"
  29. /* PCIe core registers */
  30. #define PCIE_CORE_DEV_ID_REG 0x0
  31. #define PCIE_CORE_CMD_STATUS_REG 0x4
  32. #define PCIE_CORE_DEV_REV_REG 0x8
  33. #define PCIE_CORE_SSDEV_ID_REG 0x2c
  34. #define PCIE_CORE_PCIEXP_CAP 0xc0
  35. #define PCIE_CORE_PCIERR_CAP 0x100
  36. #define PCIE_CORE_ERR_CAPCTL_REG 0x118
  37. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
  38. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
  39. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
  40. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
  41. /* PIO registers base address and register offsets */
  42. #define PIO_BASE_ADDR 0x4000
  43. #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
  44. #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
  45. #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
  46. #define PIO_STAT (PIO_BASE_ADDR + 0x4)
  47. #define PIO_COMPLETION_STATUS_SHIFT 7
  48. #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
  49. #define PIO_COMPLETION_STATUS_OK 0
  50. #define PIO_COMPLETION_STATUS_UR 1
  51. #define PIO_COMPLETION_STATUS_RRS 2
  52. #define PIO_COMPLETION_STATUS_CA 4
  53. #define PIO_NON_POSTED_REQ BIT(10)
  54. #define PIO_ERR_STATUS BIT(11)
  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_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14)
  93. #define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1)
  94. #define PCIE_CORE_REF_CLK_RX_ENABLE BIT(2)
  95. #define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30)
  96. #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
  97. #define PCIE_MSG_PM_PME_MASK BIT(7)
  98. #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
  99. #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
  100. #define PCIE_ISR0_CORR_ERR BIT(11)
  101. #define PCIE_ISR0_NFAT_ERR BIT(12)
  102. #define PCIE_ISR0_FAT_ERR BIT(13)
  103. #define PCIE_ISR0_ERR_MASK GENMASK(13, 11)
  104. #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
  105. #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
  106. #define PCIE_ISR0_ALL_MASK GENMASK(31, 0)
  107. #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
  108. #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
  109. #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
  110. #define PCIE_ISR1_FLUSH BIT(5)
  111. #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
  112. #define PCIE_ISR1_ALL_MASK GENMASK(31, 0)
  113. #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
  114. #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
  115. #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
  116. #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
  117. #define PCIE_MSI_ALL_MASK GENMASK(31, 0)
  118. #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
  119. #define PCIE_MSI_DATA_MASK GENMASK(15, 0)
  120. /* PCIe window configuration */
  121. #define OB_WIN_BASE_ADDR 0x4c00
  122. #define OB_WIN_BLOCK_SIZE 0x20
  123. #define OB_WIN_COUNT 8
  124. #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
  125. OB_WIN_BLOCK_SIZE * (win) + \
  126. (offset))
  127. #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
  128. #define OB_WIN_ENABLE BIT(0)
  129. #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
  130. #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
  131. #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
  132. #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
  133. #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
  134. #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
  135. #define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
  136. #define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
  137. #define OB_WIN_FUNC_NUM_SHIFT 24
  138. #define OB_WIN_FUNC_NUM_ENABLE BIT(23)
  139. #define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
  140. #define OB_WIN_BUS_NUM_BITS_SHIFT 20
  141. #define OB_WIN_MSG_CODE_ENABLE BIT(22)
  142. #define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
  143. #define OB_WIN_MSG_CODE_SHIFT 14
  144. #define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
  145. #define OB_WIN_ATTR_ENABLE BIT(11)
  146. #define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
  147. #define OB_WIN_ATTR_TC_SHIFT 8
  148. #define OB_WIN_ATTR_RELAXED BIT(7)
  149. #define OB_WIN_ATTR_NOSNOOP BIT(6)
  150. #define OB_WIN_ATTR_POISON BIT(5)
  151. #define OB_WIN_ATTR_IDO BIT(4)
  152. #define OB_WIN_TYPE_MASK GENMASK(3, 0)
  153. #define OB_WIN_TYPE_SHIFT 0
  154. #define OB_WIN_TYPE_MEM 0x0
  155. #define OB_WIN_TYPE_IO 0x4
  156. #define OB_WIN_TYPE_CONFIG_TYPE0 0x8
  157. #define OB_WIN_TYPE_CONFIG_TYPE1 0x9
  158. #define OB_WIN_TYPE_MSG 0xc
  159. /* LMI registers base address and register offsets */
  160. #define LMI_BASE_ADDR 0x6000
  161. #define CFG_REG (LMI_BASE_ADDR + 0x0)
  162. #define LTSSM_SHIFT 24
  163. #define LTSSM_MASK 0x3f
  164. #define RC_BAR_CONFIG 0x300
  165. /* LTSSM values in CFG_REG */
  166. enum {
  167. LTSSM_DETECT_QUIET = 0x0,
  168. LTSSM_DETECT_ACTIVE = 0x1,
  169. LTSSM_POLLING_ACTIVE = 0x2,
  170. LTSSM_POLLING_COMPLIANCE = 0x3,
  171. LTSSM_POLLING_CONFIGURATION = 0x4,
  172. LTSSM_CONFIG_LINKWIDTH_START = 0x5,
  173. LTSSM_CONFIG_LINKWIDTH_ACCEPT = 0x6,
  174. LTSSM_CONFIG_LANENUM_ACCEPT = 0x7,
  175. LTSSM_CONFIG_LANENUM_WAIT = 0x8,
  176. LTSSM_CONFIG_COMPLETE = 0x9,
  177. LTSSM_CONFIG_IDLE = 0xa,
  178. LTSSM_RECOVERY_RCVR_LOCK = 0xb,
  179. LTSSM_RECOVERY_SPEED = 0xc,
  180. LTSSM_RECOVERY_RCVR_CFG = 0xd,
  181. LTSSM_RECOVERY_IDLE = 0xe,
  182. LTSSM_L0 = 0x10,
  183. LTSSM_RX_L0S_ENTRY = 0x11,
  184. LTSSM_RX_L0S_IDLE = 0x12,
  185. LTSSM_RX_L0S_FTS = 0x13,
  186. LTSSM_TX_L0S_ENTRY = 0x14,
  187. LTSSM_TX_L0S_IDLE = 0x15,
  188. LTSSM_TX_L0S_FTS = 0x16,
  189. LTSSM_L1_ENTRY = 0x17,
  190. LTSSM_L1_IDLE = 0x18,
  191. LTSSM_L2_IDLE = 0x19,
  192. LTSSM_L2_TRANSMIT_WAKE = 0x1a,
  193. LTSSM_DISABLED = 0x20,
  194. LTSSM_LOOPBACK_ENTRY_MASTER = 0x21,
  195. LTSSM_LOOPBACK_ACTIVE_MASTER = 0x22,
  196. LTSSM_LOOPBACK_EXIT_MASTER = 0x23,
  197. LTSSM_LOOPBACK_ENTRY_SLAVE = 0x24,
  198. LTSSM_LOOPBACK_ACTIVE_SLAVE = 0x25,
  199. LTSSM_LOOPBACK_EXIT_SLAVE = 0x26,
  200. LTSSM_HOT_RESET = 0x27,
  201. LTSSM_RECOVERY_EQUALIZATION_PHASE0 = 0x28,
  202. LTSSM_RECOVERY_EQUALIZATION_PHASE1 = 0x29,
  203. LTSSM_RECOVERY_EQUALIZATION_PHASE2 = 0x2a,
  204. LTSSM_RECOVERY_EQUALIZATION_PHASE3 = 0x2b,
  205. };
  206. #define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
  207. /* PCIe core controller registers */
  208. #define CTRL_CORE_BASE_ADDR 0x18000
  209. #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
  210. #define CTRL_MODE_SHIFT 0x0
  211. #define CTRL_MODE_MASK 0x1
  212. #define PCIE_CORE_MODE_DIRECT 0x0
  213. #define PCIE_CORE_MODE_COMMAND 0x1
  214. /* PCIe Central Interrupts Registers */
  215. #define CENTRAL_INT_BASE_ADDR 0x1b000
  216. #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
  217. #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
  218. #define PCIE_IRQ_CMDQ_INT BIT(0)
  219. #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
  220. #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
  221. #define PCIE_IRQ_DMA_INT BIT(4)
  222. #define PCIE_IRQ_IB_DXFERDONE BIT(5)
  223. #define PCIE_IRQ_OB_DXFERDONE BIT(6)
  224. #define PCIE_IRQ_OB_RXFERDONE BIT(7)
  225. #define PCIE_IRQ_COMPQ_INT BIT(12)
  226. #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
  227. #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
  228. #define PCIE_IRQ_CORE_INT BIT(16)
  229. #define PCIE_IRQ_CORE_INT_PIO BIT(17)
  230. #define PCIE_IRQ_DPMU_INT BIT(18)
  231. #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
  232. #define PCIE_IRQ_MSI_INT1_DET BIT(20)
  233. #define PCIE_IRQ_MSI_INT2_DET BIT(21)
  234. #define PCIE_IRQ_RC_DBELL_DET BIT(22)
  235. #define PCIE_IRQ_EP_STATUS BIT(23)
  236. #define PCIE_IRQ_ALL_MASK GENMASK(31, 0)
  237. #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
  238. /* Transaction types */
  239. #define PCIE_CONFIG_RD_TYPE0 0x8
  240. #define PCIE_CONFIG_RD_TYPE1 0x9
  241. #define PCIE_CONFIG_WR_TYPE0 0xa
  242. #define PCIE_CONFIG_WR_TYPE1 0xb
  243. #define PIO_RETRY_CNT 750000 /* 1.5 s */
  244. #define PIO_RETRY_DELAY 2 /* 2 us*/
  245. #define LINK_WAIT_MAX_RETRIES 10
  246. #define LINK_WAIT_USLEEP_MIN 90000
  247. #define LINK_WAIT_USLEEP_MAX 100000
  248. #define RETRAIN_WAIT_MAX_RETRIES 10
  249. #define RETRAIN_WAIT_USLEEP_US 2000
  250. #define MSI_IRQ_NUM 32
  251. #define CFG_RD_RRS_VAL 0xffff0001
  252. struct advk_pcie {
  253. struct platform_device *pdev;
  254. void __iomem *base;
  255. struct {
  256. phys_addr_t match;
  257. phys_addr_t remap;
  258. phys_addr_t mask;
  259. u32 actions;
  260. } wins[OB_WIN_COUNT];
  261. u8 wins_count;
  262. struct irq_domain *rp_irq_domain;
  263. struct irq_domain *irq_domain;
  264. struct irq_chip irq_chip;
  265. raw_spinlock_t irq_lock;
  266. struct irq_domain *msi_inner_domain;
  267. raw_spinlock_t msi_irq_lock;
  268. DECLARE_BITMAP(msi_used, MSI_IRQ_NUM);
  269. struct mutex msi_used_lock;
  270. int link_gen;
  271. struct pci_bridge_emul bridge;
  272. struct gpio_desc *reset_gpio;
  273. struct phy *phy;
  274. };
  275. static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg)
  276. {
  277. writel(val, pcie->base + reg);
  278. }
  279. static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg)
  280. {
  281. return readl(pcie->base + reg);
  282. }
  283. static u8 advk_pcie_ltssm_state(struct advk_pcie *pcie)
  284. {
  285. u32 val;
  286. u8 ltssm_state;
  287. val = advk_readl(pcie, CFG_REG);
  288. ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK;
  289. return ltssm_state;
  290. }
  291. static inline bool advk_pcie_link_up(struct advk_pcie *pcie)
  292. {
  293. /* check if LTSSM is in normal operation - some L* state */
  294. u8 ltssm_state = advk_pcie_ltssm_state(pcie);
  295. return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED;
  296. }
  297. static inline bool advk_pcie_link_active(struct advk_pcie *pcie)
  298. {
  299. /*
  300. * According to PCIe Base specification 3.0, Table 4-14: Link
  301. * Status Mapped to the LTSSM, and 4.2.6.3.6 Configuration.Idle
  302. * is Link Up mapped to LTSSM Configuration.Idle, Recovery, L0,
  303. * L0s, L1 and L2 states. And according to 3.2.1. Data Link
  304. * Control and Management State Machine Rules is DL Up status
  305. * reported in DL Active state.
  306. */
  307. u8 ltssm_state = advk_pcie_ltssm_state(pcie);
  308. return ltssm_state >= LTSSM_CONFIG_IDLE && ltssm_state < LTSSM_DISABLED;
  309. }
  310. static inline bool advk_pcie_link_training(struct advk_pcie *pcie)
  311. {
  312. /*
  313. * According to PCIe Base specification 3.0, Table 4-14: Link
  314. * Status Mapped to the LTSSM is Link Training mapped to LTSSM
  315. * Configuration and Recovery states.
  316. */
  317. u8 ltssm_state = advk_pcie_ltssm_state(pcie);
  318. return ((ltssm_state >= LTSSM_CONFIG_LINKWIDTH_START &&
  319. ltssm_state < LTSSM_L0) ||
  320. (ltssm_state >= LTSSM_RECOVERY_EQUALIZATION_PHASE0 &&
  321. ltssm_state <= LTSSM_RECOVERY_EQUALIZATION_PHASE3));
  322. }
  323. static int advk_pcie_wait_for_link(struct advk_pcie *pcie)
  324. {
  325. int retries;
  326. /* check if the link is up or not */
  327. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  328. if (advk_pcie_link_up(pcie))
  329. return 0;
  330. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  331. }
  332. return -ETIMEDOUT;
  333. }
  334. static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie)
  335. {
  336. size_t retries;
  337. for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) {
  338. if (advk_pcie_link_training(pcie))
  339. break;
  340. udelay(RETRAIN_WAIT_USLEEP_US);
  341. }
  342. }
  343. static void advk_pcie_issue_perst(struct advk_pcie *pcie)
  344. {
  345. if (!pcie->reset_gpio)
  346. return;
  347. /* 10ms delay is needed for some cards */
  348. dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n");
  349. gpiod_set_value_cansleep(pcie->reset_gpio, 1);
  350. usleep_range(10000, 11000);
  351. gpiod_set_value_cansleep(pcie->reset_gpio, 0);
  352. }
  353. static void advk_pcie_train_link(struct advk_pcie *pcie)
  354. {
  355. struct device *dev = &pcie->pdev->dev;
  356. u32 reg;
  357. int ret;
  358. /*
  359. * Setup PCIe rev / gen compliance based on device tree property
  360. * 'max-link-speed' which also forces maximal link speed.
  361. */
  362. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  363. reg &= ~PCIE_GEN_SEL_MSK;
  364. if (pcie->link_gen == 3)
  365. reg |= SPEED_GEN_3;
  366. else if (pcie->link_gen == 2)
  367. reg |= SPEED_GEN_2;
  368. else
  369. reg |= SPEED_GEN_1;
  370. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  371. /*
  372. * Set maximal link speed value also into PCIe Link Control 2 register.
  373. * Armada 3700 Functional Specification says that default value is based
  374. * on SPEED_GEN but tests showed that default value is always 8.0 GT/s.
  375. */
  376. reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
  377. reg &= ~PCI_EXP_LNKCTL2_TLS;
  378. if (pcie->link_gen == 3)
  379. reg |= PCI_EXP_LNKCTL2_TLS_8_0GT;
  380. else if (pcie->link_gen == 2)
  381. reg |= PCI_EXP_LNKCTL2_TLS_5_0GT;
  382. else
  383. reg |= PCI_EXP_LNKCTL2_TLS_2_5GT;
  384. advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2);
  385. /* Enable link training after selecting PCIe generation */
  386. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  387. reg |= LINK_TRAINING_EN;
  388. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  389. /*
  390. * Reset PCIe card via PERST# signal. Some cards are not detected
  391. * during link training when they are in some non-initial state.
  392. */
  393. advk_pcie_issue_perst(pcie);
  394. /*
  395. * PERST# signal could have been asserted by pinctrl subsystem before
  396. * probe() callback has been called or issued explicitly by reset gpio
  397. * function advk_pcie_issue_perst(), making the endpoint going into
  398. * fundamental reset. As required by PCI Express spec (PCI Express
  399. * Base Specification, REV. 4.0 PCI Express, February 19 2014, 6.6.1
  400. * Conventional Reset) a delay for at least 100ms after such a reset
  401. * before sending a Configuration Request to the device is needed.
  402. * So wait until PCIe link is up. Function advk_pcie_wait_for_link()
  403. * waits for link at least 900ms.
  404. */
  405. ret = advk_pcie_wait_for_link(pcie);
  406. if (ret < 0)
  407. dev_err(dev, "link never came up\n");
  408. else
  409. dev_info(dev, "link up\n");
  410. }
  411. /*
  412. * Set PCIe address window register which could be used for memory
  413. * mapping.
  414. */
  415. static void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num,
  416. phys_addr_t match, phys_addr_t remap,
  417. phys_addr_t mask, u32 actions)
  418. {
  419. advk_writel(pcie, OB_WIN_ENABLE |
  420. lower_32_bits(match), OB_WIN_MATCH_LS(win_num));
  421. advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num));
  422. advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num));
  423. advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num));
  424. advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num));
  425. advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num));
  426. advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num));
  427. }
  428. static void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num)
  429. {
  430. advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num));
  431. advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num));
  432. advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num));
  433. advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num));
  434. advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num));
  435. advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num));
  436. advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num));
  437. }
  438. static void advk_pcie_setup_hw(struct advk_pcie *pcie)
  439. {
  440. phys_addr_t msi_addr;
  441. u32 reg;
  442. int i;
  443. /*
  444. * Configure PCIe Reference clock. Direction is from the PCIe
  445. * controller to the endpoint card, so enable transmitting of
  446. * Reference clock differential signal off-chip and disable
  447. * receiving off-chip differential signal.
  448. */
  449. reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG);
  450. reg |= PCIE_CORE_REF_CLK_TX_ENABLE;
  451. reg &= ~PCIE_CORE_REF_CLK_RX_ENABLE;
  452. advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG);
  453. /* Set to Direct mode */
  454. reg = advk_readl(pcie, CTRL_CONFIG_REG);
  455. reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT);
  456. reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT);
  457. advk_writel(pcie, reg, CTRL_CONFIG_REG);
  458. /* Set PCI global control register to RC mode */
  459. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  460. reg |= (IS_RC_MSK << IS_RC_SHIFT);
  461. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  462. /*
  463. * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab.
  464. * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor
  465. * id in high 16 bits. Updating this register changes readback value of
  466. * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround
  467. * for erratum 4.1: "The value of device and vendor ID is incorrect".
  468. */
  469. reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL;
  470. advk_writel(pcie, reg, VENDOR_ID_REG);
  471. /*
  472. * Change Class Code of PCI Bridge device to PCI Bridge (0x600400),
  473. * because the default value is Mass storage controller (0x010400).
  474. *
  475. * Note that this Aardvark PCI Bridge does not have compliant Type 1
  476. * Configuration Space and it even cannot be accessed via Aardvark's
  477. * PCI config space access method. Something like config space is
  478. * available in internal Aardvark registers starting at offset 0x0
  479. * and is reported as Type 0. In range 0x10 - 0x34 it has totally
  480. * different registers.
  481. *
  482. * Therefore driver uses emulation of PCI Bridge which emulates
  483. * access to configuration space via internal Aardvark registers or
  484. * emulated configuration buffer.
  485. */
  486. reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG);
  487. reg &= ~0xffffff00;
  488. reg |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8;
  489. advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG);
  490. /* Disable Root Bridge I/O space, memory space and bus mastering */
  491. reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
  492. reg &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  493. advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG);
  494. /* Set Advanced Error Capabilities and Control PF0 register */
  495. reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX |
  496. PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN |
  497. PCIE_CORE_ERR_CAPCTL_ECRC_CHCK |
  498. PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV;
  499. advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG);
  500. /* Set PCIe Device Control register */
  501. reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
  502. reg &= ~PCI_EXP_DEVCTL_RELAX_EN;
  503. reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
  504. reg &= ~PCI_EXP_DEVCTL_PAYLOAD;
  505. reg &= ~PCI_EXP_DEVCTL_READRQ;
  506. reg |= PCI_EXP_DEVCTL_PAYLOAD_512B;
  507. reg |= PCI_EXP_DEVCTL_READRQ_512B;
  508. advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL);
  509. /* Program PCIe Control 2 to disable strict ordering */
  510. reg = PCIE_CORE_CTRL2_RESERVED |
  511. PCIE_CORE_CTRL2_TD_ENABLE;
  512. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  513. /* Set lane X1 */
  514. reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  515. reg &= ~LANE_CNT_MSK;
  516. reg |= LANE_COUNT_1;
  517. advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG);
  518. /* Set MSI address */
  519. msi_addr = virt_to_phys(pcie);
  520. advk_writel(pcie, lower_32_bits(msi_addr), PCIE_MSI_ADDR_LOW_REG);
  521. advk_writel(pcie, upper_32_bits(msi_addr), PCIE_MSI_ADDR_HIGH_REG);
  522. /* Enable MSI */
  523. reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
  524. reg |= PCIE_CORE_CTRL2_MSI_ENABLE;
  525. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  526. /* Clear all interrupts */
  527. advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
  528. advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
  529. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
  530. advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
  531. /* Disable All ISR0/1 and MSI Sources */
  532. advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG);
  533. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
  534. advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
  535. /* Unmask summary MSI interrupt */
  536. reg = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  537. reg &= ~PCIE_ISR0_MSI_INT_PENDING;
  538. advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
  539. /* Unmask PME interrupt for processing of PME requester */
  540. reg = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  541. reg &= ~PCIE_MSG_PM_PME_MASK;
  542. advk_writel(pcie, reg, PCIE_ISR0_MASK_REG);
  543. /* Enable summary interrupt for GIC SPI source */
  544. reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK);
  545. advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG);
  546. /*
  547. * Enable AXI address window location generation:
  548. * When it is enabled, the default outbound window
  549. * configurations (Default User Field: 0xD0074CFC)
  550. * are used to transparent address translation for
  551. * the outbound transactions. Thus, PCIe address
  552. * windows are not required for transparent memory
  553. * access when default outbound window configuration
  554. * is set for memory access.
  555. */
  556. reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
  557. reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE;
  558. advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG);
  559. /*
  560. * Set memory access in Default User Field so it
  561. * is not required to configure PCIe address for
  562. * transparent memory access.
  563. */
  564. advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS);
  565. /*
  566. * Bypass the address window mapping for PIO:
  567. * Since PIO access already contains all required
  568. * info over AXI interface by PIO registers, the
  569. * address window is not required.
  570. */
  571. reg = advk_readl(pcie, PIO_CTRL);
  572. reg |= PIO_CTRL_ADDR_WIN_DISABLE;
  573. advk_writel(pcie, reg, PIO_CTRL);
  574. /*
  575. * Configure PCIe address windows for non-memory or
  576. * non-transparent access as by default PCIe uses
  577. * transparent memory access.
  578. */
  579. for (i = 0; i < pcie->wins_count; i++)
  580. advk_pcie_set_ob_win(pcie, i,
  581. pcie->wins[i].match, pcie->wins[i].remap,
  582. pcie->wins[i].mask, pcie->wins[i].actions);
  583. /* Disable remaining PCIe outbound windows */
  584. for (i = pcie->wins_count; i < OB_WIN_COUNT; i++)
  585. advk_pcie_disable_ob_win(pcie, i);
  586. advk_pcie_train_link(pcie);
  587. }
  588. static int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_rrs, u32 *val)
  589. {
  590. struct device *dev = &pcie->pdev->dev;
  591. u32 reg;
  592. unsigned int status;
  593. char *strcomp_status, *str_posted;
  594. int ret;
  595. reg = advk_readl(pcie, PIO_STAT);
  596. status = (reg & PIO_COMPLETION_STATUS_MASK) >>
  597. PIO_COMPLETION_STATUS_SHIFT;
  598. /*
  599. * According to HW spec, the PIO status check sequence as below:
  600. * 1) even if COMPLETION_STATUS(bit9:7) indicates successful,
  601. * it still needs to check Error Status(bit11), only when this bit
  602. * indicates no error happen, the operation is successful.
  603. * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only
  604. * means a PIO write error, and for PIO read it is successful with
  605. * a read value of 0xFFFFFFFF.
  606. * 3) value Config Request Retry Status(RRS) of COMPLETION_STATUS(bit9:7)
  607. * only means a PIO write error, and for PIO read it is successful
  608. * with a read value of 0xFFFF0001.
  609. * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means
  610. * error for both PIO read and PIO write operation.
  611. * 5) other errors are indicated as 'unknown'.
  612. */
  613. switch (status) {
  614. case PIO_COMPLETION_STATUS_OK:
  615. if (reg & PIO_ERR_STATUS) {
  616. strcomp_status = "COMP_ERR";
  617. ret = -EFAULT;
  618. break;
  619. }
  620. /* Get the read result */
  621. if (val)
  622. *val = advk_readl(pcie, PIO_RD_DATA);
  623. /* No error */
  624. strcomp_status = NULL;
  625. ret = 0;
  626. break;
  627. case PIO_COMPLETION_STATUS_UR:
  628. strcomp_status = "UR";
  629. ret = -EOPNOTSUPP;
  630. break;
  631. case PIO_COMPLETION_STATUS_RRS:
  632. if (allow_rrs && val) {
  633. /* PCIe r6.0, sec 2.3.2, says:
  634. * If Configuration RRS Software Visibility is enabled:
  635. * For a Configuration Read Request that includes both
  636. * bytes of the Vendor ID field of a device Function's
  637. * Configuration Space Header, the Root Complex must
  638. * complete the Request to the host by returning a
  639. * read-data value of 0001h for the Vendor ID field and
  640. * all '1's for any additional bytes included in the
  641. * request.
  642. *
  643. * So RRS in this case is not an error status.
  644. */
  645. *val = CFG_RD_RRS_VAL;
  646. strcomp_status = NULL;
  647. ret = 0;
  648. break;
  649. }
  650. /* PCIe r6.0, sec 2.3.2, says:
  651. * If RRS Software Visibility is not enabled, the Root Complex
  652. * must re-issue the Configuration Request as a new Request.
  653. * If RRS Software Visibility is enabled: For a Configuration
  654. * Write Request or for any other Configuration Read Request,
  655. * the Root Complex must re-issue the Configuration Request as
  656. * a new Request.
  657. * A Root Complex implementation may choose to limit the number
  658. * of Configuration Request/RRS Completion Status loops before
  659. * determining that something is wrong with the target of the
  660. * Request and taking appropriate action, e.g., complete the
  661. * Request to the host as a failed transaction.
  662. *
  663. * So return -EAGAIN and caller (pci-aardvark.c driver) will
  664. * re-issue request again up to the PIO_RETRY_CNT retries.
  665. */
  666. strcomp_status = "RRS";
  667. ret = -EAGAIN;
  668. break;
  669. case PIO_COMPLETION_STATUS_CA:
  670. strcomp_status = "CA";
  671. ret = -ECANCELED;
  672. break;
  673. default:
  674. strcomp_status = "Unknown";
  675. ret = -EINVAL;
  676. break;
  677. }
  678. if (!strcomp_status)
  679. return ret;
  680. if (reg & PIO_NON_POSTED_REQ)
  681. str_posted = "Non-posted";
  682. else
  683. str_posted = "Posted";
  684. dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n",
  685. str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS));
  686. return ret;
  687. }
  688. static int advk_pcie_wait_pio(struct advk_pcie *pcie)
  689. {
  690. struct device *dev = &pcie->pdev->dev;
  691. int i;
  692. for (i = 1; i <= PIO_RETRY_CNT; i++) {
  693. u32 start, isr;
  694. start = advk_readl(pcie, PIO_START);
  695. isr = advk_readl(pcie, PIO_ISR);
  696. if (!start && isr)
  697. return i;
  698. udelay(PIO_RETRY_DELAY);
  699. }
  700. dev_err(dev, "PIO read/write transfer time out\n");
  701. return -ETIMEDOUT;
  702. }
  703. static pci_bridge_emul_read_status_t
  704. advk_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge,
  705. int reg, u32 *value)
  706. {
  707. struct advk_pcie *pcie = bridge->data;
  708. switch (reg) {
  709. case PCI_COMMAND:
  710. *value = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
  711. return PCI_BRIDGE_EMUL_HANDLED;
  712. case PCI_INTERRUPT_LINE: {
  713. /*
  714. * From the whole 32bit register we support reading from HW only
  715. * two bits: PCI_BRIDGE_CTL_BUS_RESET and PCI_BRIDGE_CTL_SERR.
  716. * Other bits are retrieved only from emulated config buffer.
  717. */
  718. __le32 *cfgspace = (__le32 *)&bridge->conf;
  719. u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]);
  720. if (advk_readl(pcie, PCIE_ISR0_MASK_REG) & PCIE_ISR0_ERR_MASK)
  721. val &= ~(PCI_BRIDGE_CTL_SERR << 16);
  722. else
  723. val |= PCI_BRIDGE_CTL_SERR << 16;
  724. if (advk_readl(pcie, PCIE_CORE_CTRL1_REG) & HOT_RESET_GEN)
  725. val |= PCI_BRIDGE_CTL_BUS_RESET << 16;
  726. else
  727. val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16);
  728. *value = val;
  729. return PCI_BRIDGE_EMUL_HANDLED;
  730. }
  731. default:
  732. return PCI_BRIDGE_EMUL_NOT_HANDLED;
  733. }
  734. }
  735. static void
  736. advk_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge,
  737. int reg, u32 old, u32 new, u32 mask)
  738. {
  739. struct advk_pcie *pcie = bridge->data;
  740. switch (reg) {
  741. case PCI_COMMAND:
  742. advk_writel(pcie, new, PCIE_CORE_CMD_STATUS_REG);
  743. break;
  744. case PCI_INTERRUPT_LINE:
  745. /*
  746. * According to Figure 6-3: Pseudo Logic Diagram for Error
  747. * Message Controls in PCIe base specification, SERR# Enable bit
  748. * in Bridge Control register enable receiving of ERR_* messages
  749. */
  750. if (mask & (PCI_BRIDGE_CTL_SERR << 16)) {
  751. u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  752. if (new & (PCI_BRIDGE_CTL_SERR << 16))
  753. val &= ~PCIE_ISR0_ERR_MASK;
  754. else
  755. val |= PCIE_ISR0_ERR_MASK;
  756. advk_writel(pcie, val, PCIE_ISR0_MASK_REG);
  757. }
  758. if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) {
  759. u32 val = advk_readl(pcie, PCIE_CORE_CTRL1_REG);
  760. if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16))
  761. val |= HOT_RESET_GEN;
  762. else
  763. val &= ~HOT_RESET_GEN;
  764. advk_writel(pcie, val, PCIE_CORE_CTRL1_REG);
  765. }
  766. break;
  767. default:
  768. break;
  769. }
  770. }
  771. static pci_bridge_emul_read_status_t
  772. advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge,
  773. int reg, u32 *value)
  774. {
  775. struct advk_pcie *pcie = bridge->data;
  776. switch (reg) {
  777. /*
  778. * PCI_EXP_SLTCAP, PCI_EXP_SLTCTL, PCI_EXP_RTCTL and PCI_EXP_RTSTA are
  779. * also supported, but do not need to be handled here, because their
  780. * values are stored in emulated config space buffer, and we read them
  781. * from there when needed.
  782. */
  783. case PCI_EXP_LNKCAP: {
  784. u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
  785. /*
  786. * PCI_EXP_LNKCAP_DLLLARC bit is hardwired in aardvark HW to 0.
  787. * But support for PCI_EXP_LNKSTA_DLLLA is emulated via ltssm
  788. * state so explicitly enable PCI_EXP_LNKCAP_DLLLARC flag.
  789. */
  790. val |= PCI_EXP_LNKCAP_DLLLARC;
  791. *value = val;
  792. return PCI_BRIDGE_EMUL_HANDLED;
  793. }
  794. case PCI_EXP_LNKCTL: {
  795. /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */
  796. u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) &
  797. ~(PCI_EXP_LNKSTA_LT << 16);
  798. if (advk_pcie_link_training(pcie))
  799. val |= (PCI_EXP_LNKSTA_LT << 16);
  800. if (advk_pcie_link_active(pcie))
  801. val |= (PCI_EXP_LNKSTA_DLLLA << 16);
  802. *value = val;
  803. return PCI_BRIDGE_EMUL_HANDLED;
  804. }
  805. case PCI_EXP_DEVCAP:
  806. case PCI_EXP_DEVCTL:
  807. case PCI_EXP_DEVCAP2:
  808. case PCI_EXP_DEVCTL2:
  809. case PCI_EXP_LNKCAP2:
  810. case PCI_EXP_LNKCTL2:
  811. *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg);
  812. return PCI_BRIDGE_EMUL_HANDLED;
  813. default:
  814. return PCI_BRIDGE_EMUL_NOT_HANDLED;
  815. }
  816. }
  817. static void
  818. advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge,
  819. int reg, u32 old, u32 new, u32 mask)
  820. {
  821. struct advk_pcie *pcie = bridge->data;
  822. switch (reg) {
  823. case PCI_EXP_LNKCTL:
  824. advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
  825. if (new & PCI_EXP_LNKCTL_RL)
  826. advk_pcie_wait_for_retrain(pcie);
  827. break;
  828. case PCI_EXP_RTCTL: {
  829. u16 rootctl = le16_to_cpu(bridge->pcie_conf.rootctl);
  830. /* Only emulation of PMEIE and RRS_SVE bits is provided */
  831. rootctl &= PCI_EXP_RTCTL_PMEIE | PCI_EXP_RTCTL_RRS_SVE;
  832. bridge->pcie_conf.rootctl = cpu_to_le16(rootctl);
  833. break;
  834. }
  835. /*
  836. * PCI_EXP_RTSTA is also supported, but does not need to be handled
  837. * here, because its value is stored in emulated config space buffer,
  838. * and we write it there when needed.
  839. */
  840. case PCI_EXP_DEVCTL:
  841. case PCI_EXP_DEVCTL2:
  842. case PCI_EXP_LNKCTL2:
  843. advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg);
  844. break;
  845. default:
  846. break;
  847. }
  848. }
  849. static pci_bridge_emul_read_status_t
  850. advk_pci_bridge_emul_ext_conf_read(struct pci_bridge_emul *bridge,
  851. int reg, u32 *value)
  852. {
  853. struct advk_pcie *pcie = bridge->data;
  854. switch (reg) {
  855. case 0:
  856. *value = advk_readl(pcie, PCIE_CORE_PCIERR_CAP + reg);
  857. /*
  858. * PCI_EXT_CAP_NEXT bits are set to offset 0x150, but Armada
  859. * 3700 Functional Specification does not document registers
  860. * at those addresses.
  861. *
  862. * Thus we clear PCI_EXT_CAP_NEXT bits to make Advanced Error
  863. * Reporting Capability header the last Extended Capability.
  864. * If we obtain documentation for those registers in the
  865. * future, this can be changed.
  866. */
  867. *value &= 0x000fffff;
  868. return PCI_BRIDGE_EMUL_HANDLED;
  869. case PCI_ERR_UNCOR_STATUS:
  870. case PCI_ERR_UNCOR_MASK:
  871. case PCI_ERR_UNCOR_SEVER:
  872. case PCI_ERR_COR_STATUS:
  873. case PCI_ERR_COR_MASK:
  874. case PCI_ERR_CAP:
  875. case PCI_ERR_HEADER_LOG + 0:
  876. case PCI_ERR_HEADER_LOG + 4:
  877. case PCI_ERR_HEADER_LOG + 8:
  878. case PCI_ERR_HEADER_LOG + 12:
  879. case PCI_ERR_ROOT_COMMAND:
  880. case PCI_ERR_ROOT_STATUS:
  881. case PCI_ERR_ROOT_ERR_SRC:
  882. *value = advk_readl(pcie, PCIE_CORE_PCIERR_CAP + reg);
  883. return PCI_BRIDGE_EMUL_HANDLED;
  884. default:
  885. return PCI_BRIDGE_EMUL_NOT_HANDLED;
  886. }
  887. }
  888. static void
  889. advk_pci_bridge_emul_ext_conf_write(struct pci_bridge_emul *bridge,
  890. int reg, u32 old, u32 new, u32 mask)
  891. {
  892. struct advk_pcie *pcie = bridge->data;
  893. switch (reg) {
  894. /* These are W1C registers, so clear other bits */
  895. case PCI_ERR_UNCOR_STATUS:
  896. case PCI_ERR_COR_STATUS:
  897. case PCI_ERR_ROOT_STATUS:
  898. new &= mask;
  899. fallthrough;
  900. case PCI_ERR_UNCOR_MASK:
  901. case PCI_ERR_UNCOR_SEVER:
  902. case PCI_ERR_COR_MASK:
  903. case PCI_ERR_CAP:
  904. case PCI_ERR_HEADER_LOG + 0:
  905. case PCI_ERR_HEADER_LOG + 4:
  906. case PCI_ERR_HEADER_LOG + 8:
  907. case PCI_ERR_HEADER_LOG + 12:
  908. case PCI_ERR_ROOT_COMMAND:
  909. case PCI_ERR_ROOT_ERR_SRC:
  910. advk_writel(pcie, new, PCIE_CORE_PCIERR_CAP + reg);
  911. break;
  912. default:
  913. break;
  914. }
  915. }
  916. static const struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = {
  917. .read_base = advk_pci_bridge_emul_base_conf_read,
  918. .write_base = advk_pci_bridge_emul_base_conf_write,
  919. .read_pcie = advk_pci_bridge_emul_pcie_conf_read,
  920. .write_pcie = advk_pci_bridge_emul_pcie_conf_write,
  921. .read_ext = advk_pci_bridge_emul_ext_conf_read,
  922. .write_ext = advk_pci_bridge_emul_ext_conf_write,
  923. };
  924. /*
  925. * Initialize the configuration space of the PCI-to-PCI bridge
  926. * associated with the given PCIe interface.
  927. */
  928. static int advk_sw_pci_bridge_init(struct advk_pcie *pcie)
  929. {
  930. struct pci_bridge_emul *bridge = &pcie->bridge;
  931. bridge->conf.vendor =
  932. cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff);
  933. bridge->conf.device =
  934. cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16);
  935. bridge->conf.class_revision =
  936. cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff);
  937. /* Support 32 bits I/O addressing */
  938. bridge->conf.iobase = PCI_IO_RANGE_TYPE_32;
  939. bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32;
  940. /* Support 64 bits memory pref */
  941. bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
  942. bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64);
  943. /* Support interrupt A for MSI feature */
  944. bridge->conf.intpin = PCI_INTERRUPT_INTA;
  945. /*
  946. * Aardvark HW provides PCIe Capability structure in version 2 and
  947. * indicate slot support, which is emulated.
  948. */
  949. bridge->pcie_conf.cap = cpu_to_le16(2 | PCI_EXP_FLAGS_SLOT);
  950. /*
  951. * Set Presence Detect State bit permanently since there is no support
  952. * for unplugging the card nor detecting whether it is plugged. (If a
  953. * platform exists in the future that supports it, via a GPIO for
  954. * example, it should be implemented via this bit.)
  955. *
  956. * Set physical slot number to 1 since there is only one port and zero
  957. * value is reserved for ports within the same silicon as Root Port
  958. * which is not our case.
  959. */
  960. bridge->pcie_conf.slotcap = cpu_to_le32(FIELD_PREP(PCI_EXP_SLTCAP_PSN,
  961. 1));
  962. bridge->pcie_conf.slotsta = cpu_to_le16(PCI_EXP_SLTSTA_PDS);
  963. /* Indicates supports for Completion Retry Status */
  964. bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_RRS_SV);
  965. bridge->subsystem_vendor_id = advk_readl(pcie, PCIE_CORE_SSDEV_ID_REG) & 0xffff;
  966. bridge->subsystem_id = advk_readl(pcie, PCIE_CORE_SSDEV_ID_REG) >> 16;
  967. bridge->has_pcie = true;
  968. bridge->pcie_start = PCIE_CORE_PCIEXP_CAP;
  969. bridge->data = pcie;
  970. bridge->ops = &advk_pci_bridge_emul_ops;
  971. return pci_bridge_emul_init(bridge, 0);
  972. }
  973. static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus,
  974. int devfn)
  975. {
  976. if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0)
  977. return false;
  978. /*
  979. * If the link goes down after we check for link-up, we have a problem:
  980. * if a PIO request is executed while link-down, the whole controller
  981. * gets stuck in a non-functional state, and even after link comes up
  982. * again, PIO requests won't work anymore, and a reset of the whole PCIe
  983. * controller is needed. Therefore we need to prevent sending PIO
  984. * requests while the link is down.
  985. */
  986. if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie))
  987. return false;
  988. return true;
  989. }
  990. static bool advk_pcie_pio_is_running(struct advk_pcie *pcie)
  991. {
  992. struct device *dev = &pcie->pdev->dev;
  993. /*
  994. * Trying to start a new PIO transfer when previous has not completed
  995. * cause External Abort on CPU which results in kernel panic:
  996. *
  997. * SError Interrupt on CPU0, code 0xbf000002 -- SError
  998. * Kernel panic - not syncing: Asynchronous SError Interrupt
  999. *
  1000. * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected
  1001. * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent
  1002. * concurrent calls at the same time. But because PIO transfer may take
  1003. * about 1.5s when link is down or card is disconnected, it means that
  1004. * advk_pcie_wait_pio() does not always have to wait for completion.
  1005. *
  1006. * Some versions of ARM Trusted Firmware handles this External Abort at
  1007. * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit:
  1008. * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50
  1009. */
  1010. if (advk_readl(pcie, PIO_START)) {
  1011. dev_err(dev, "Previous PIO read/write transfer is still running\n");
  1012. return true;
  1013. }
  1014. return false;
  1015. }
  1016. static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn,
  1017. int where, int size, u32 *val)
  1018. {
  1019. struct advk_pcie *pcie = bus->sysdata;
  1020. int retry_count;
  1021. bool allow_rrs;
  1022. u32 reg;
  1023. int ret;
  1024. if (!advk_pcie_valid_device(pcie, bus, devfn))
  1025. return PCIBIOS_DEVICE_NOT_FOUND;
  1026. if (pci_is_root_bus(bus))
  1027. return pci_bridge_emul_conf_read(&pcie->bridge, where,
  1028. size, val);
  1029. /*
  1030. * Configuration Request Retry Status (RRS) is possible to return
  1031. * only when reading both bytes from PCI_VENDOR_ID at once and
  1032. * RRS_SVE flag on Root Port is enabled.
  1033. */
  1034. allow_rrs = (where == PCI_VENDOR_ID) && (size >= 2) &&
  1035. (le16_to_cpu(pcie->bridge.pcie_conf.rootctl) &
  1036. PCI_EXP_RTCTL_RRS_SVE);
  1037. if (advk_pcie_pio_is_running(pcie))
  1038. goto try_rrs;
  1039. /* Program the control register */
  1040. reg = advk_readl(pcie, PIO_CTRL);
  1041. reg &= ~PIO_CTRL_TYPE_MASK;
  1042. if (pci_is_root_bus(bus->parent))
  1043. reg |= PCIE_CONFIG_RD_TYPE0;
  1044. else
  1045. reg |= PCIE_CONFIG_RD_TYPE1;
  1046. advk_writel(pcie, reg, PIO_CTRL);
  1047. /* Program the address registers */
  1048. reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
  1049. advk_writel(pcie, reg, PIO_ADDR_LS);
  1050. advk_writel(pcie, 0, PIO_ADDR_MS);
  1051. /* Program the data strobe */
  1052. advk_writel(pcie, 0xf, PIO_WR_DATA_STRB);
  1053. retry_count = 0;
  1054. do {
  1055. /* Clear PIO DONE ISR and start the transfer */
  1056. advk_writel(pcie, 1, PIO_ISR);
  1057. advk_writel(pcie, 1, PIO_START);
  1058. ret = advk_pcie_wait_pio(pcie);
  1059. if (ret < 0)
  1060. goto try_rrs;
  1061. retry_count += ret;
  1062. /* Check PIO status and get the read result */
  1063. ret = advk_pcie_check_pio_status(pcie, allow_rrs, val);
  1064. } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT);
  1065. if (ret < 0)
  1066. goto fail;
  1067. if (size == 1)
  1068. *val = (*val >> (8 * (where & 3))) & 0xff;
  1069. else if (size == 2)
  1070. *val = (*val >> (8 * (where & 3))) & 0xffff;
  1071. return PCIBIOS_SUCCESSFUL;
  1072. try_rrs:
  1073. /*
  1074. * If it is possible, return Configuration Request Retry Status so
  1075. * that caller tries to issue the request again instead of failing.
  1076. */
  1077. if (allow_rrs) {
  1078. *val = CFG_RD_RRS_VAL;
  1079. return PCIBIOS_SUCCESSFUL;
  1080. }
  1081. fail:
  1082. *val = 0xffffffff;
  1083. return PCIBIOS_SET_FAILED;
  1084. }
  1085. static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  1086. int where, int size, u32 val)
  1087. {
  1088. struct advk_pcie *pcie = bus->sysdata;
  1089. u32 reg;
  1090. u32 data_strobe = 0x0;
  1091. int retry_count;
  1092. int offset;
  1093. int ret;
  1094. if (!advk_pcie_valid_device(pcie, bus, devfn))
  1095. return PCIBIOS_DEVICE_NOT_FOUND;
  1096. if (pci_is_root_bus(bus))
  1097. return pci_bridge_emul_conf_write(&pcie->bridge, where,
  1098. size, val);
  1099. if (where % size)
  1100. return PCIBIOS_SET_FAILED;
  1101. if (advk_pcie_pio_is_running(pcie))
  1102. return PCIBIOS_SET_FAILED;
  1103. /* Program the control register */
  1104. reg = advk_readl(pcie, PIO_CTRL);
  1105. reg &= ~PIO_CTRL_TYPE_MASK;
  1106. if (pci_is_root_bus(bus->parent))
  1107. reg |= PCIE_CONFIG_WR_TYPE0;
  1108. else
  1109. reg |= PCIE_CONFIG_WR_TYPE1;
  1110. advk_writel(pcie, reg, PIO_CTRL);
  1111. /* Program the address registers */
  1112. reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4);
  1113. advk_writel(pcie, reg, PIO_ADDR_LS);
  1114. advk_writel(pcie, 0, PIO_ADDR_MS);
  1115. /* Calculate the write strobe */
  1116. offset = where & 0x3;
  1117. reg = val << (8 * offset);
  1118. data_strobe = GENMASK(size - 1, 0) << offset;
  1119. /* Program the data register */
  1120. advk_writel(pcie, reg, PIO_WR_DATA);
  1121. /* Program the data strobe */
  1122. advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB);
  1123. retry_count = 0;
  1124. do {
  1125. /* Clear PIO DONE ISR and start the transfer */
  1126. advk_writel(pcie, 1, PIO_ISR);
  1127. advk_writel(pcie, 1, PIO_START);
  1128. ret = advk_pcie_wait_pio(pcie);
  1129. if (ret < 0)
  1130. return PCIBIOS_SET_FAILED;
  1131. retry_count += ret;
  1132. ret = advk_pcie_check_pio_status(pcie, false, NULL);
  1133. } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT);
  1134. return ret < 0 ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
  1135. }
  1136. static struct pci_ops advk_pcie_ops = {
  1137. .read = advk_pcie_rd_conf,
  1138. .write = advk_pcie_wr_conf,
  1139. };
  1140. static void advk_msi_irq_compose_msi_msg(struct irq_data *data,
  1141. struct msi_msg *msg)
  1142. {
  1143. struct advk_pcie *pcie = irq_data_get_irq_chip_data(data);
  1144. phys_addr_t msi_addr = virt_to_phys(pcie);
  1145. msg->address_lo = lower_32_bits(msi_addr);
  1146. msg->address_hi = upper_32_bits(msi_addr);
  1147. msg->data = data->hwirq;
  1148. }
  1149. static void advk_msi_irq_mask(struct irq_data *d)
  1150. {
  1151. struct advk_pcie *pcie = d->domain->host_data;
  1152. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1153. unsigned long flags;
  1154. u32 mask;
  1155. raw_spin_lock_irqsave(&pcie->msi_irq_lock, flags);
  1156. mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
  1157. mask |= BIT(hwirq);
  1158. advk_writel(pcie, mask, PCIE_MSI_MASK_REG);
  1159. raw_spin_unlock_irqrestore(&pcie->msi_irq_lock, flags);
  1160. }
  1161. static void advk_msi_irq_unmask(struct irq_data *d)
  1162. {
  1163. struct advk_pcie *pcie = d->domain->host_data;
  1164. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1165. unsigned long flags;
  1166. u32 mask;
  1167. raw_spin_lock_irqsave(&pcie->msi_irq_lock, flags);
  1168. mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
  1169. mask &= ~BIT(hwirq);
  1170. advk_writel(pcie, mask, PCIE_MSI_MASK_REG);
  1171. raw_spin_unlock_irqrestore(&pcie->msi_irq_lock, flags);
  1172. }
  1173. static struct irq_chip advk_msi_bottom_irq_chip = {
  1174. .name = "MSI",
  1175. .irq_compose_msi_msg = advk_msi_irq_compose_msi_msg,
  1176. .irq_mask = advk_msi_irq_mask,
  1177. .irq_unmask = advk_msi_irq_unmask,
  1178. };
  1179. static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
  1180. unsigned int virq,
  1181. unsigned int nr_irqs, void *args)
  1182. {
  1183. struct advk_pcie *pcie = domain->host_data;
  1184. int hwirq, i;
  1185. mutex_lock(&pcie->msi_used_lock);
  1186. hwirq = bitmap_find_free_region(pcie->msi_used, MSI_IRQ_NUM,
  1187. order_base_2(nr_irqs));
  1188. mutex_unlock(&pcie->msi_used_lock);
  1189. if (hwirq < 0)
  1190. return -ENOSPC;
  1191. for (i = 0; i < nr_irqs; i++)
  1192. irq_domain_set_info(domain, virq + i, hwirq + i,
  1193. &advk_msi_bottom_irq_chip,
  1194. domain->host_data, handle_simple_irq,
  1195. NULL, NULL);
  1196. return 0;
  1197. }
  1198. static void advk_msi_irq_domain_free(struct irq_domain *domain,
  1199. unsigned int virq, unsigned int nr_irqs)
  1200. {
  1201. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  1202. struct advk_pcie *pcie = domain->host_data;
  1203. mutex_lock(&pcie->msi_used_lock);
  1204. bitmap_release_region(pcie->msi_used, d->hwirq, order_base_2(nr_irqs));
  1205. mutex_unlock(&pcie->msi_used_lock);
  1206. }
  1207. static const struct irq_domain_ops advk_msi_domain_ops = {
  1208. .alloc = advk_msi_irq_domain_alloc,
  1209. .free = advk_msi_irq_domain_free,
  1210. };
  1211. static void advk_pcie_irq_mask(struct irq_data *d)
  1212. {
  1213. struct advk_pcie *pcie = d->domain->host_data;
  1214. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1215. unsigned long flags;
  1216. u32 mask;
  1217. raw_spin_lock_irqsave(&pcie->irq_lock, flags);
  1218. mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  1219. mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
  1220. advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
  1221. raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
  1222. }
  1223. static void advk_pcie_irq_unmask(struct irq_data *d)
  1224. {
  1225. struct advk_pcie *pcie = d->domain->host_data;
  1226. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1227. unsigned long flags;
  1228. u32 mask;
  1229. raw_spin_lock_irqsave(&pcie->irq_lock, flags);
  1230. mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  1231. mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
  1232. advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
  1233. raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
  1234. }
  1235. static int advk_pcie_irq_map(struct irq_domain *h,
  1236. unsigned int virq, irq_hw_number_t hwirq)
  1237. {
  1238. struct advk_pcie *pcie = h->host_data;
  1239. irq_set_status_flags(virq, IRQ_LEVEL);
  1240. irq_set_chip_and_handler(virq, &pcie->irq_chip,
  1241. handle_level_irq);
  1242. irq_set_chip_data(virq, pcie);
  1243. return 0;
  1244. }
  1245. static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
  1246. .map = advk_pcie_irq_map,
  1247. .xlate = irq_domain_xlate_onecell,
  1248. };
  1249. #define ADVK_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  1250. MSI_FLAG_USE_DEF_CHIP_OPS | \
  1251. MSI_FLAG_PCI_MSI_MASK_PARENT | \
  1252. MSI_FLAG_NO_AFFINITY)
  1253. #define ADVK_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
  1254. MSI_FLAG_PCI_MSIX | \
  1255. MSI_FLAG_MULTI_PCI_MSI)
  1256. static const struct msi_parent_ops advk_msi_parent_ops = {
  1257. .required_flags = ADVK_MSI_FLAGS_REQUIRED,
  1258. .supported_flags = ADVK_MSI_FLAGS_SUPPORTED,
  1259. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  1260. .prefix = "advk-",
  1261. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  1262. };
  1263. static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
  1264. {
  1265. struct device *dev = &pcie->pdev->dev;
  1266. raw_spin_lock_init(&pcie->msi_irq_lock);
  1267. mutex_init(&pcie->msi_used_lock);
  1268. struct irq_domain_info info = {
  1269. .fwnode = dev_fwnode(dev),
  1270. .ops = &advk_msi_domain_ops,
  1271. .host_data = pcie,
  1272. .size = MSI_IRQ_NUM,
  1273. };
  1274. pcie->msi_inner_domain = msi_create_parent_irq_domain(&info, &advk_msi_parent_ops);
  1275. if (!pcie->msi_inner_domain)
  1276. return -ENOMEM;
  1277. return 0;
  1278. }
  1279. static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
  1280. {
  1281. irq_domain_remove(pcie->msi_inner_domain);
  1282. }
  1283. static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
  1284. {
  1285. struct device *dev = &pcie->pdev->dev;
  1286. struct device_node *node = dev->of_node;
  1287. struct device_node *pcie_intc_node;
  1288. struct irq_chip *irq_chip;
  1289. int ret = 0;
  1290. raw_spin_lock_init(&pcie->irq_lock);
  1291. pcie_intc_node = of_get_next_child(node, NULL);
  1292. if (!pcie_intc_node) {
  1293. dev_err(dev, "No PCIe Intc node found\n");
  1294. return -ENODEV;
  1295. }
  1296. irq_chip = &pcie->irq_chip;
  1297. irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
  1298. dev_name(dev));
  1299. if (!irq_chip->name) {
  1300. ret = -ENOMEM;
  1301. goto out_put_node;
  1302. }
  1303. irq_chip->irq_mask = advk_pcie_irq_mask;
  1304. irq_chip->irq_unmask = advk_pcie_irq_unmask;
  1305. pcie->irq_domain = irq_domain_create_linear(of_fwnode_handle(pcie_intc_node), PCI_NUM_INTX,
  1306. &advk_pcie_irq_domain_ops, pcie);
  1307. if (!pcie->irq_domain) {
  1308. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  1309. ret = -ENOMEM;
  1310. goto out_put_node;
  1311. }
  1312. out_put_node:
  1313. of_node_put(pcie_intc_node);
  1314. return ret;
  1315. }
  1316. static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
  1317. {
  1318. irq_domain_remove(pcie->irq_domain);
  1319. }
  1320. static struct irq_chip advk_rp_irq_chip = {
  1321. .name = "advk-RP",
  1322. };
  1323. static int advk_pcie_rp_irq_map(struct irq_domain *h,
  1324. unsigned int virq, irq_hw_number_t hwirq)
  1325. {
  1326. struct advk_pcie *pcie = h->host_data;
  1327. irq_set_chip_and_handler(virq, &advk_rp_irq_chip, handle_simple_irq);
  1328. irq_set_chip_data(virq, pcie);
  1329. return 0;
  1330. }
  1331. static const struct irq_domain_ops advk_pcie_rp_irq_domain_ops = {
  1332. .map = advk_pcie_rp_irq_map,
  1333. .xlate = irq_domain_xlate_onecell,
  1334. };
  1335. static int advk_pcie_init_rp_irq_domain(struct advk_pcie *pcie)
  1336. {
  1337. pcie->rp_irq_domain = irq_domain_create_linear(NULL, 1, &advk_pcie_rp_irq_domain_ops, pcie);
  1338. if (!pcie->rp_irq_domain) {
  1339. dev_err(&pcie->pdev->dev, "Failed to add Root Port IRQ domain\n");
  1340. return -ENOMEM;
  1341. }
  1342. return 0;
  1343. }
  1344. static void advk_pcie_remove_rp_irq_domain(struct advk_pcie *pcie)
  1345. {
  1346. irq_domain_remove(pcie->rp_irq_domain);
  1347. }
  1348. static void advk_pcie_handle_pme(struct advk_pcie *pcie)
  1349. {
  1350. u32 requester = advk_readl(pcie, PCIE_MSG_LOG_REG) >> 16;
  1351. advk_writel(pcie, PCIE_MSG_PM_PME_MASK, PCIE_ISR0_REG);
  1352. /*
  1353. * PCIE_MSG_LOG_REG contains the last inbound message, so store
  1354. * the requester ID only when PME was not asserted yet.
  1355. * Also do not trigger PME interrupt when PME is still asserted.
  1356. */
  1357. if (!(le32_to_cpu(pcie->bridge.pcie_conf.rootsta) & PCI_EXP_RTSTA_PME)) {
  1358. pcie->bridge.pcie_conf.rootsta = cpu_to_le32(requester | PCI_EXP_RTSTA_PME);
  1359. /*
  1360. * Trigger PME interrupt only if PMEIE bit in Root Control is set.
  1361. * Aardvark HW returns zero for PCI_EXP_FLAGS_IRQ, so use PCIe interrupt 0.
  1362. */
  1363. if (!(le16_to_cpu(pcie->bridge.pcie_conf.rootctl) & PCI_EXP_RTCTL_PMEIE))
  1364. return;
  1365. if (generic_handle_domain_irq(pcie->rp_irq_domain, 0) == -EINVAL)
  1366. dev_err_ratelimited(&pcie->pdev->dev, "unhandled PME IRQ\n");
  1367. }
  1368. }
  1369. static void advk_pcie_handle_msi(struct advk_pcie *pcie)
  1370. {
  1371. u32 msi_val, msi_mask, msi_status, msi_idx;
  1372. msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
  1373. msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
  1374. msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK);
  1375. for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
  1376. if (!(BIT(msi_idx) & msi_status))
  1377. continue;
  1378. advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
  1379. if (generic_handle_domain_irq(pcie->msi_inner_domain, msi_idx) == -EINVAL)
  1380. dev_err_ratelimited(&pcie->pdev->dev, "unexpected MSI 0x%02x\n", msi_idx);
  1381. }
  1382. advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
  1383. PCIE_ISR0_REG);
  1384. }
  1385. static void advk_pcie_handle_int(struct advk_pcie *pcie)
  1386. {
  1387. u32 isr0_val, isr0_mask, isr0_status;
  1388. u32 isr1_val, isr1_mask, isr1_status;
  1389. int i;
  1390. isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
  1391. isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  1392. isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
  1393. isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
  1394. isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  1395. isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
  1396. /* Process PME interrupt as the first one to do not miss PME requester id */
  1397. if (isr0_status & PCIE_MSG_PM_PME_MASK)
  1398. advk_pcie_handle_pme(pcie);
  1399. /* Process ERR interrupt */
  1400. if (isr0_status & PCIE_ISR0_ERR_MASK) {
  1401. advk_writel(pcie, PCIE_ISR0_ERR_MASK, PCIE_ISR0_REG);
  1402. /*
  1403. * Aardvark HW returns zero for PCI_ERR_ROOT_AER_IRQ, so use
  1404. * PCIe interrupt 0
  1405. */
  1406. if (generic_handle_domain_irq(pcie->rp_irq_domain, 0) == -EINVAL)
  1407. dev_err_ratelimited(&pcie->pdev->dev, "unhandled ERR IRQ\n");
  1408. }
  1409. /* Process MSI interrupts */
  1410. if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
  1411. advk_pcie_handle_msi(pcie);
  1412. /* Process legacy interrupts */
  1413. for (i = 0; i < PCI_NUM_INTX; i++) {
  1414. if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
  1415. continue;
  1416. advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
  1417. PCIE_ISR1_REG);
  1418. if (generic_handle_domain_irq(pcie->irq_domain, i) == -EINVAL)
  1419. dev_err_ratelimited(&pcie->pdev->dev, "unexpected INT%c IRQ\n",
  1420. (char)i + 'A');
  1421. }
  1422. }
  1423. static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
  1424. {
  1425. struct advk_pcie *pcie = arg;
  1426. u32 status;
  1427. status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
  1428. if (!(status & PCIE_IRQ_CORE_INT))
  1429. return IRQ_NONE;
  1430. advk_pcie_handle_int(pcie);
  1431. /* Clear interrupt */
  1432. advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
  1433. return IRQ_HANDLED;
  1434. }
  1435. static int advk_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  1436. {
  1437. struct advk_pcie *pcie = dev->bus->sysdata;
  1438. /*
  1439. * Emulated root bridge has its own emulated irq chip and irq domain.
  1440. * Argument pin is the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD) and
  1441. * hwirq for irq_create_mapping() is indexed from zero.
  1442. */
  1443. if (pci_is_root_bus(dev->bus))
  1444. return irq_create_mapping(pcie->rp_irq_domain, pin - 1);
  1445. else
  1446. return of_irq_parse_and_map_pci(dev, slot, pin);
  1447. }
  1448. static void advk_pcie_disable_phy(struct advk_pcie *pcie)
  1449. {
  1450. phy_power_off(pcie->phy);
  1451. phy_exit(pcie->phy);
  1452. }
  1453. static int advk_pcie_enable_phy(struct advk_pcie *pcie)
  1454. {
  1455. int ret;
  1456. if (!pcie->phy)
  1457. return 0;
  1458. ret = phy_init(pcie->phy);
  1459. if (ret)
  1460. return ret;
  1461. ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE);
  1462. if (ret) {
  1463. phy_exit(pcie->phy);
  1464. return ret;
  1465. }
  1466. ret = phy_power_on(pcie->phy);
  1467. if (ret) {
  1468. phy_exit(pcie->phy);
  1469. return ret;
  1470. }
  1471. return 0;
  1472. }
  1473. static int advk_pcie_setup_phy(struct advk_pcie *pcie)
  1474. {
  1475. struct device *dev = &pcie->pdev->dev;
  1476. struct device_node *node = dev->of_node;
  1477. int ret = 0;
  1478. pcie->phy = devm_of_phy_get(dev, node, NULL);
  1479. if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER))
  1480. return PTR_ERR(pcie->phy);
  1481. /* Old bindings miss the PHY handle */
  1482. if (IS_ERR(pcie->phy)) {
  1483. dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy));
  1484. pcie->phy = NULL;
  1485. return 0;
  1486. }
  1487. ret = advk_pcie_enable_phy(pcie);
  1488. if (ret)
  1489. dev_err(dev, "Failed to initialize PHY (%d)\n", ret);
  1490. return ret;
  1491. }
  1492. static int advk_pcie_probe(struct platform_device *pdev)
  1493. {
  1494. struct device *dev = &pdev->dev;
  1495. struct advk_pcie *pcie;
  1496. struct pci_host_bridge *bridge;
  1497. struct resource_entry *entry;
  1498. int ret, irq;
  1499. bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
  1500. if (!bridge)
  1501. return -ENOMEM;
  1502. pcie = pci_host_bridge_priv(bridge);
  1503. pcie->pdev = pdev;
  1504. platform_set_drvdata(pdev, pcie);
  1505. resource_list_for_each_entry(entry, &bridge->windows) {
  1506. resource_size_t start = entry->res->start;
  1507. resource_size_t size = resource_size(entry->res);
  1508. unsigned long type = resource_type(entry->res);
  1509. u64 win_size;
  1510. /*
  1511. * Aardvark hardware allows to configure also PCIe window
  1512. * for config type 0 and type 1 mapping, but driver uses
  1513. * only PIO for issuing configuration transfers which does
  1514. * not use PCIe window configuration.
  1515. */
  1516. if (type != IORESOURCE_MEM && type != IORESOURCE_IO)
  1517. continue;
  1518. /*
  1519. * Skip transparent memory resources. Default outbound access
  1520. * configuration is set to transparent memory access so it
  1521. * does not need window configuration.
  1522. */
  1523. if (type == IORESOURCE_MEM && entry->offset == 0)
  1524. continue;
  1525. /*
  1526. * The n-th PCIe window is configured by tuple (match, remap, mask)
  1527. * and an access to address A uses this window if A matches the
  1528. * match with given mask.
  1529. * So every PCIe window size must be a power of two and every start
  1530. * address must be aligned to window size. Minimal size is 64 KiB
  1531. * because lower 16 bits of mask must be zero. Remapped address
  1532. * may have set only bits from the mask.
  1533. */
  1534. while (pcie->wins_count < OB_WIN_COUNT && size > 0) {
  1535. /* Calculate the largest aligned window size */
  1536. win_size = (1ULL << (fls64(size)-1)) |
  1537. (start ? (1ULL << __ffs64(start)) : 0);
  1538. win_size = 1ULL << __ffs64(win_size);
  1539. if (win_size < 0x10000)
  1540. break;
  1541. dev_dbg(dev,
  1542. "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n",
  1543. pcie->wins_count, (unsigned long long)start,
  1544. (unsigned long long)start + win_size, type);
  1545. if (type == IORESOURCE_IO) {
  1546. pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO;
  1547. pcie->wins[pcie->wins_count].match = pci_pio_to_address(start);
  1548. } else {
  1549. pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM;
  1550. pcie->wins[pcie->wins_count].match = start;
  1551. }
  1552. pcie->wins[pcie->wins_count].remap = start - entry->offset;
  1553. pcie->wins[pcie->wins_count].mask = ~(win_size - 1);
  1554. if (pcie->wins[pcie->wins_count].remap & (win_size - 1))
  1555. break;
  1556. start += win_size;
  1557. size -= win_size;
  1558. pcie->wins_count++;
  1559. }
  1560. if (size > 0) {
  1561. dev_err(&pcie->pdev->dev,
  1562. "Invalid PCIe region [0x%llx-0x%llx]\n",
  1563. (unsigned long long)entry->res->start,
  1564. (unsigned long long)entry->res->end + 1);
  1565. return -EINVAL;
  1566. }
  1567. }
  1568. pcie->base = devm_platform_ioremap_resource(pdev, 0);
  1569. if (IS_ERR(pcie->base))
  1570. return PTR_ERR(pcie->base);
  1571. irq = platform_get_irq(pdev, 0);
  1572. if (irq < 0)
  1573. return irq;
  1574. ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
  1575. IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
  1576. pcie);
  1577. if (ret) {
  1578. dev_err(dev, "Failed to register interrupt\n");
  1579. return ret;
  1580. }
  1581. pcie->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  1582. ret = PTR_ERR_OR_ZERO(pcie->reset_gpio);
  1583. if (ret) {
  1584. if (ret != -EPROBE_DEFER)
  1585. dev_err(dev, "Failed to get reset-gpio: %i\n", ret);
  1586. return ret;
  1587. }
  1588. ret = gpiod_set_consumer_name(pcie->reset_gpio, "pcie1-reset");
  1589. if (ret) {
  1590. dev_err(dev, "Failed to set reset gpio name: %d\n", ret);
  1591. return ret;
  1592. }
  1593. ret = of_pci_get_max_link_speed(dev->of_node);
  1594. if (ret <= 0 || ret > 3)
  1595. pcie->link_gen = 3;
  1596. else
  1597. pcie->link_gen = ret;
  1598. ret = advk_pcie_setup_phy(pcie);
  1599. if (ret)
  1600. return ret;
  1601. advk_pcie_setup_hw(pcie);
  1602. ret = advk_sw_pci_bridge_init(pcie);
  1603. if (ret) {
  1604. dev_err(dev, "Failed to register emulated root PCI bridge\n");
  1605. return ret;
  1606. }
  1607. ret = advk_pcie_init_irq_domain(pcie);
  1608. if (ret) {
  1609. dev_err(dev, "Failed to initialize irq\n");
  1610. return ret;
  1611. }
  1612. ret = advk_pcie_init_msi_irq_domain(pcie);
  1613. if (ret) {
  1614. dev_err(dev, "Failed to initialize irq\n");
  1615. advk_pcie_remove_irq_domain(pcie);
  1616. return ret;
  1617. }
  1618. ret = advk_pcie_init_rp_irq_domain(pcie);
  1619. if (ret) {
  1620. dev_err(dev, "Failed to initialize irq\n");
  1621. advk_pcie_remove_msi_irq_domain(pcie);
  1622. advk_pcie_remove_irq_domain(pcie);
  1623. return ret;
  1624. }
  1625. bridge->sysdata = pcie;
  1626. bridge->ops = &advk_pcie_ops;
  1627. bridge->map_irq = advk_pcie_map_irq;
  1628. ret = pci_host_probe(bridge);
  1629. if (ret < 0) {
  1630. advk_pcie_remove_rp_irq_domain(pcie);
  1631. advk_pcie_remove_msi_irq_domain(pcie);
  1632. advk_pcie_remove_irq_domain(pcie);
  1633. return ret;
  1634. }
  1635. return 0;
  1636. }
  1637. static void advk_pcie_remove(struct platform_device *pdev)
  1638. {
  1639. struct advk_pcie *pcie = platform_get_drvdata(pdev);
  1640. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
  1641. u32 val;
  1642. int i;
  1643. /* Remove PCI bus with all devices */
  1644. pci_lock_rescan_remove();
  1645. pci_stop_root_bus(bridge->bus);
  1646. pci_remove_root_bus(bridge->bus);
  1647. pci_unlock_rescan_remove();
  1648. /* Disable Root Bridge I/O space, memory space and bus mastering */
  1649. val = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
  1650. val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  1651. advk_writel(pcie, val, PCIE_CORE_CMD_STATUS_REG);
  1652. /* Disable MSI */
  1653. val = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
  1654. val &= ~PCIE_CORE_CTRL2_MSI_ENABLE;
  1655. advk_writel(pcie, val, PCIE_CORE_CTRL2_REG);
  1656. /* Clear MSI address */
  1657. advk_writel(pcie, 0, PCIE_MSI_ADDR_LOW_REG);
  1658. advk_writel(pcie, 0, PCIE_MSI_ADDR_HIGH_REG);
  1659. /* Mask all interrupts */
  1660. advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
  1661. advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG);
  1662. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
  1663. advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_MASK_REG);
  1664. /* Clear all interrupts */
  1665. advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
  1666. advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
  1667. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
  1668. advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
  1669. /* Remove IRQ domains */
  1670. advk_pcie_remove_rp_irq_domain(pcie);
  1671. advk_pcie_remove_msi_irq_domain(pcie);
  1672. advk_pcie_remove_irq_domain(pcie);
  1673. /* Free config space for emulated root bridge */
  1674. pci_bridge_emul_cleanup(&pcie->bridge);
  1675. /* Assert PERST# signal which prepares PCIe card for power down */
  1676. if (pcie->reset_gpio)
  1677. gpiod_set_value_cansleep(pcie->reset_gpio, 1);
  1678. /* Disable link training */
  1679. val = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  1680. val &= ~LINK_TRAINING_EN;
  1681. advk_writel(pcie, val, PCIE_CORE_CTRL0_REG);
  1682. /* Disable outbound address windows mapping */
  1683. for (i = 0; i < OB_WIN_COUNT; i++)
  1684. advk_pcie_disable_ob_win(pcie, i);
  1685. /* Disable phy */
  1686. advk_pcie_disable_phy(pcie);
  1687. }
  1688. static const struct of_device_id advk_pcie_of_match_table[] = {
  1689. { .compatible = "marvell,armada-3700-pcie", },
  1690. {},
  1691. };
  1692. MODULE_DEVICE_TABLE(of, advk_pcie_of_match_table);
  1693. static struct platform_driver advk_pcie_driver = {
  1694. .driver = {
  1695. .name = "advk-pcie",
  1696. .of_match_table = advk_pcie_of_match_table,
  1697. },
  1698. .probe = advk_pcie_probe,
  1699. .remove = advk_pcie_remove,
  1700. };
  1701. module_platform_driver(advk_pcie_driver);
  1702. MODULE_DESCRIPTION("Aardvark PCIe controller");
  1703. MODULE_LICENSE("GPL v2");