pci-aardvark.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  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/irqdomain.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <linux/pci-ecam.h>
  20. #include <linux/init.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/msi.h>
  24. #include <linux/of_address.h>
  25. #include <linux/of_pci.h>
  26. #include "../pci.h"
  27. #include "../pci-bridge-emul.h"
  28. /* PCIe core registers */
  29. #define PCIE_CORE_DEV_ID_REG 0x0
  30. #define PCIE_CORE_CMD_STATUS_REG 0x4
  31. #define PCIE_CORE_DEV_REV_REG 0x8
  32. #define PCIE_CORE_SSDEV_ID_REG 0x2c
  33. #define PCIE_CORE_PCIEXP_CAP 0xc0
  34. #define PCIE_CORE_PCIERR_CAP 0x100
  35. #define PCIE_CORE_ERR_CAPCTL_REG 0x118
  36. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5)
  37. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6)
  38. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7)
  39. #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8)
  40. /* PIO registers base address and register offsets */
  41. #define PIO_BASE_ADDR 0x4000
  42. #define PIO_CTRL (PIO_BASE_ADDR + 0x0)
  43. #define PIO_CTRL_TYPE_MASK GENMASK(3, 0)
  44. #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24)
  45. #define PIO_STAT (PIO_BASE_ADDR + 0x4)
  46. #define PIO_COMPLETION_STATUS_SHIFT 7
  47. #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7)
  48. #define PIO_COMPLETION_STATUS_OK 0
  49. #define PIO_COMPLETION_STATUS_UR 1
  50. #define PIO_COMPLETION_STATUS_RRS 2
  51. #define PIO_COMPLETION_STATUS_CA 4
  52. #define PIO_NON_POSTED_REQ BIT(10)
  53. #define PIO_ERR_STATUS BIT(11)
  54. #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8)
  55. #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc)
  56. #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10)
  57. #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14)
  58. #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18)
  59. #define PIO_START (PIO_BASE_ADDR + 0x1c)
  60. #define PIO_ISR (PIO_BASE_ADDR + 0x20)
  61. #define PIO_ISRM (PIO_BASE_ADDR + 0x24)
  62. /* Aardvark Control registers */
  63. #define CONTROL_BASE_ADDR 0x4800
  64. #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0)
  65. #define PCIE_GEN_SEL_MSK 0x3
  66. #define PCIE_GEN_SEL_SHIFT 0x0
  67. #define SPEED_GEN_1 0
  68. #define SPEED_GEN_2 1
  69. #define SPEED_GEN_3 2
  70. #define IS_RC_MSK 1
  71. #define IS_RC_SHIFT 2
  72. #define LANE_CNT_MSK 0x18
  73. #define LANE_CNT_SHIFT 0x3
  74. #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT)
  75. #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT)
  76. #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT)
  77. #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT)
  78. #define LINK_TRAINING_EN BIT(6)
  79. #define LEGACY_INTA BIT(28)
  80. #define LEGACY_INTB BIT(29)
  81. #define LEGACY_INTC BIT(30)
  82. #define LEGACY_INTD BIT(31)
  83. #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4)
  84. #define HOT_RESET_GEN BIT(0)
  85. #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8)
  86. #define PCIE_CORE_CTRL2_RESERVED 0x7
  87. #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4)
  88. #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5)
  89. #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6)
  90. #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10)
  91. #define PCIE_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14)
  92. #define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1)
  93. #define PCIE_CORE_REF_CLK_RX_ENABLE BIT(2)
  94. #define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30)
  95. #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40)
  96. #define PCIE_MSG_PM_PME_MASK BIT(7)
  97. #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44)
  98. #define PCIE_ISR0_MSI_INT_PENDING BIT(24)
  99. #define PCIE_ISR0_CORR_ERR BIT(11)
  100. #define PCIE_ISR0_NFAT_ERR BIT(12)
  101. #define PCIE_ISR0_FAT_ERR BIT(13)
  102. #define PCIE_ISR0_ERR_MASK GENMASK(13, 11)
  103. #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val))
  104. #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val))
  105. #define PCIE_ISR0_ALL_MASK GENMASK(31, 0)
  106. #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48)
  107. #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C)
  108. #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4)
  109. #define PCIE_ISR1_FLUSH BIT(5)
  110. #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val))
  111. #define PCIE_ISR1_ALL_MASK GENMASK(31, 0)
  112. #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50)
  113. #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54)
  114. #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58)
  115. #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C)
  116. #define PCIE_MSI_ALL_MASK GENMASK(31, 0)
  117. #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C)
  118. #define PCIE_MSI_DATA_MASK GENMASK(15, 0)
  119. /* PCIe window configuration */
  120. #define OB_WIN_BASE_ADDR 0x4c00
  121. #define OB_WIN_BLOCK_SIZE 0x20
  122. #define OB_WIN_COUNT 8
  123. #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \
  124. OB_WIN_BLOCK_SIZE * (win) + \
  125. (offset))
  126. #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00)
  127. #define OB_WIN_ENABLE BIT(0)
  128. #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04)
  129. #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08)
  130. #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c)
  131. #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10)
  132. #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14)
  133. #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18)
  134. #define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4)
  135. #define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24)
  136. #define OB_WIN_FUNC_NUM_SHIFT 24
  137. #define OB_WIN_FUNC_NUM_ENABLE BIT(23)
  138. #define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20)
  139. #define OB_WIN_BUS_NUM_BITS_SHIFT 20
  140. #define OB_WIN_MSG_CODE_ENABLE BIT(22)
  141. #define OB_WIN_MSG_CODE_MASK GENMASK(21, 14)
  142. #define OB_WIN_MSG_CODE_SHIFT 14
  143. #define OB_WIN_MSG_PAYLOAD_LEN BIT(12)
  144. #define OB_WIN_ATTR_ENABLE BIT(11)
  145. #define OB_WIN_ATTR_TC_MASK GENMASK(10, 8)
  146. #define OB_WIN_ATTR_TC_SHIFT 8
  147. #define OB_WIN_ATTR_RELAXED BIT(7)
  148. #define OB_WIN_ATTR_NOSNOOP BIT(6)
  149. #define OB_WIN_ATTR_POISON BIT(5)
  150. #define OB_WIN_ATTR_IDO BIT(4)
  151. #define OB_WIN_TYPE_MASK GENMASK(3, 0)
  152. #define OB_WIN_TYPE_SHIFT 0
  153. #define OB_WIN_TYPE_MEM 0x0
  154. #define OB_WIN_TYPE_IO 0x4
  155. #define OB_WIN_TYPE_CONFIG_TYPE0 0x8
  156. #define OB_WIN_TYPE_CONFIG_TYPE1 0x9
  157. #define OB_WIN_TYPE_MSG 0xc
  158. /* LMI registers base address and register offsets */
  159. #define LMI_BASE_ADDR 0x6000
  160. #define CFG_REG (LMI_BASE_ADDR + 0x0)
  161. #define LTSSM_SHIFT 24
  162. #define LTSSM_MASK 0x3f
  163. #define RC_BAR_CONFIG 0x300
  164. /* LTSSM values in CFG_REG */
  165. enum {
  166. LTSSM_DETECT_QUIET = 0x0,
  167. LTSSM_DETECT_ACTIVE = 0x1,
  168. LTSSM_POLLING_ACTIVE = 0x2,
  169. LTSSM_POLLING_COMPLIANCE = 0x3,
  170. LTSSM_POLLING_CONFIGURATION = 0x4,
  171. LTSSM_CONFIG_LINKWIDTH_START = 0x5,
  172. LTSSM_CONFIG_LINKWIDTH_ACCEPT = 0x6,
  173. LTSSM_CONFIG_LANENUM_ACCEPT = 0x7,
  174. LTSSM_CONFIG_LANENUM_WAIT = 0x8,
  175. LTSSM_CONFIG_COMPLETE = 0x9,
  176. LTSSM_CONFIG_IDLE = 0xa,
  177. LTSSM_RECOVERY_RCVR_LOCK = 0xb,
  178. LTSSM_RECOVERY_SPEED = 0xc,
  179. LTSSM_RECOVERY_RCVR_CFG = 0xd,
  180. LTSSM_RECOVERY_IDLE = 0xe,
  181. LTSSM_L0 = 0x10,
  182. LTSSM_RX_L0S_ENTRY = 0x11,
  183. LTSSM_RX_L0S_IDLE = 0x12,
  184. LTSSM_RX_L0S_FTS = 0x13,
  185. LTSSM_TX_L0S_ENTRY = 0x14,
  186. LTSSM_TX_L0S_IDLE = 0x15,
  187. LTSSM_TX_L0S_FTS = 0x16,
  188. LTSSM_L1_ENTRY = 0x17,
  189. LTSSM_L1_IDLE = 0x18,
  190. LTSSM_L2_IDLE = 0x19,
  191. LTSSM_L2_TRANSMIT_WAKE = 0x1a,
  192. LTSSM_DISABLED = 0x20,
  193. LTSSM_LOOPBACK_ENTRY_MASTER = 0x21,
  194. LTSSM_LOOPBACK_ACTIVE_MASTER = 0x22,
  195. LTSSM_LOOPBACK_EXIT_MASTER = 0x23,
  196. LTSSM_LOOPBACK_ENTRY_SLAVE = 0x24,
  197. LTSSM_LOOPBACK_ACTIVE_SLAVE = 0x25,
  198. LTSSM_LOOPBACK_EXIT_SLAVE = 0x26,
  199. LTSSM_HOT_RESET = 0x27,
  200. LTSSM_RECOVERY_EQUALIZATION_PHASE0 = 0x28,
  201. LTSSM_RECOVERY_EQUALIZATION_PHASE1 = 0x29,
  202. LTSSM_RECOVERY_EQUALIZATION_PHASE2 = 0x2a,
  203. LTSSM_RECOVERY_EQUALIZATION_PHASE3 = 0x2b,
  204. };
  205. #define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44)
  206. /* PCIe core controller registers */
  207. #define CTRL_CORE_BASE_ADDR 0x18000
  208. #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0)
  209. #define CTRL_MODE_SHIFT 0x0
  210. #define CTRL_MODE_MASK 0x1
  211. #define PCIE_CORE_MODE_DIRECT 0x0
  212. #define PCIE_CORE_MODE_COMMAND 0x1
  213. /* PCIe Central Interrupts Registers */
  214. #define CENTRAL_INT_BASE_ADDR 0x1b000
  215. #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0)
  216. #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4)
  217. #define PCIE_IRQ_CMDQ_INT BIT(0)
  218. #define PCIE_IRQ_MSI_STATUS_INT BIT(1)
  219. #define PCIE_IRQ_CMD_SENT_DONE BIT(3)
  220. #define PCIE_IRQ_DMA_INT BIT(4)
  221. #define PCIE_IRQ_IB_DXFERDONE BIT(5)
  222. #define PCIE_IRQ_OB_DXFERDONE BIT(6)
  223. #define PCIE_IRQ_OB_RXFERDONE BIT(7)
  224. #define PCIE_IRQ_COMPQ_INT BIT(12)
  225. #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13)
  226. #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14)
  227. #define PCIE_IRQ_CORE_INT BIT(16)
  228. #define PCIE_IRQ_CORE_INT_PIO BIT(17)
  229. #define PCIE_IRQ_DPMU_INT BIT(18)
  230. #define PCIE_IRQ_PCIE_MIS_INT BIT(19)
  231. #define PCIE_IRQ_MSI_INT1_DET BIT(20)
  232. #define PCIE_IRQ_MSI_INT2_DET BIT(21)
  233. #define PCIE_IRQ_RC_DBELL_DET BIT(22)
  234. #define PCIE_IRQ_EP_STATUS BIT(23)
  235. #define PCIE_IRQ_ALL_MASK GENMASK(31, 0)
  236. #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT
  237. /* Transaction types */
  238. #define PCIE_CONFIG_RD_TYPE0 0x8
  239. #define PCIE_CONFIG_RD_TYPE1 0x9
  240. #define PCIE_CONFIG_WR_TYPE0 0xa
  241. #define PCIE_CONFIG_WR_TYPE1 0xb
  242. #define PIO_RETRY_CNT 750000 /* 1.5 s */
  243. #define PIO_RETRY_DELAY 2 /* 2 us*/
  244. #define LINK_WAIT_MAX_RETRIES 10
  245. #define LINK_WAIT_USLEEP_MIN 90000
  246. #define LINK_WAIT_USLEEP_MAX 100000
  247. #define RETRAIN_WAIT_MAX_RETRIES 10
  248. #define RETRAIN_WAIT_USLEEP_US 2000
  249. #define MSI_IRQ_NUM 32
  250. #define CFG_RD_RRS_VAL 0xffff0001
  251. struct advk_pcie {
  252. struct platform_device *pdev;
  253. void __iomem *base;
  254. struct {
  255. phys_addr_t match;
  256. phys_addr_t remap;
  257. phys_addr_t mask;
  258. u32 actions;
  259. } wins[OB_WIN_COUNT];
  260. u8 wins_count;
  261. struct irq_domain *rp_irq_domain;
  262. struct irq_domain *irq_domain;
  263. struct irq_chip irq_chip;
  264. raw_spinlock_t irq_lock;
  265. struct irq_domain *msi_domain;
  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 void advk_msi_top_irq_mask(struct irq_data *d)
  1174. {
  1175. pci_msi_mask_irq(d);
  1176. irq_chip_mask_parent(d);
  1177. }
  1178. static void advk_msi_top_irq_unmask(struct irq_data *d)
  1179. {
  1180. pci_msi_unmask_irq(d);
  1181. irq_chip_unmask_parent(d);
  1182. }
  1183. static struct irq_chip advk_msi_bottom_irq_chip = {
  1184. .name = "MSI",
  1185. .irq_compose_msi_msg = advk_msi_irq_compose_msi_msg,
  1186. .irq_mask = advk_msi_irq_mask,
  1187. .irq_unmask = advk_msi_irq_unmask,
  1188. };
  1189. static int advk_msi_irq_domain_alloc(struct irq_domain *domain,
  1190. unsigned int virq,
  1191. unsigned int nr_irqs, void *args)
  1192. {
  1193. struct advk_pcie *pcie = domain->host_data;
  1194. int hwirq, i;
  1195. mutex_lock(&pcie->msi_used_lock);
  1196. hwirq = bitmap_find_free_region(pcie->msi_used, MSI_IRQ_NUM,
  1197. order_base_2(nr_irqs));
  1198. mutex_unlock(&pcie->msi_used_lock);
  1199. if (hwirq < 0)
  1200. return -ENOSPC;
  1201. for (i = 0; i < nr_irqs; i++)
  1202. irq_domain_set_info(domain, virq + i, hwirq + i,
  1203. &advk_msi_bottom_irq_chip,
  1204. domain->host_data, handle_simple_irq,
  1205. NULL, NULL);
  1206. return 0;
  1207. }
  1208. static void advk_msi_irq_domain_free(struct irq_domain *domain,
  1209. unsigned int virq, unsigned int nr_irqs)
  1210. {
  1211. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  1212. struct advk_pcie *pcie = domain->host_data;
  1213. mutex_lock(&pcie->msi_used_lock);
  1214. bitmap_release_region(pcie->msi_used, d->hwirq, order_base_2(nr_irqs));
  1215. mutex_unlock(&pcie->msi_used_lock);
  1216. }
  1217. static const struct irq_domain_ops advk_msi_domain_ops = {
  1218. .alloc = advk_msi_irq_domain_alloc,
  1219. .free = advk_msi_irq_domain_free,
  1220. };
  1221. static void advk_pcie_irq_mask(struct irq_data *d)
  1222. {
  1223. struct advk_pcie *pcie = d->domain->host_data;
  1224. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1225. unsigned long flags;
  1226. u32 mask;
  1227. raw_spin_lock_irqsave(&pcie->irq_lock, flags);
  1228. mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  1229. mask |= PCIE_ISR1_INTX_ASSERT(hwirq);
  1230. advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
  1231. raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
  1232. }
  1233. static void advk_pcie_irq_unmask(struct irq_data *d)
  1234. {
  1235. struct advk_pcie *pcie = d->domain->host_data;
  1236. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  1237. unsigned long flags;
  1238. u32 mask;
  1239. raw_spin_lock_irqsave(&pcie->irq_lock, flags);
  1240. mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  1241. mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq);
  1242. advk_writel(pcie, mask, PCIE_ISR1_MASK_REG);
  1243. raw_spin_unlock_irqrestore(&pcie->irq_lock, flags);
  1244. }
  1245. static int advk_pcie_irq_map(struct irq_domain *h,
  1246. unsigned int virq, irq_hw_number_t hwirq)
  1247. {
  1248. struct advk_pcie *pcie = h->host_data;
  1249. irq_set_status_flags(virq, IRQ_LEVEL);
  1250. irq_set_chip_and_handler(virq, &pcie->irq_chip,
  1251. handle_level_irq);
  1252. irq_set_chip_data(virq, pcie);
  1253. return 0;
  1254. }
  1255. static const struct irq_domain_ops advk_pcie_irq_domain_ops = {
  1256. .map = advk_pcie_irq_map,
  1257. .xlate = irq_domain_xlate_onecell,
  1258. };
  1259. static struct irq_chip advk_msi_irq_chip = {
  1260. .name = "advk-MSI",
  1261. .irq_mask = advk_msi_top_irq_mask,
  1262. .irq_unmask = advk_msi_top_irq_unmask,
  1263. };
  1264. static struct msi_domain_info advk_msi_domain_info = {
  1265. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  1266. MSI_FLAG_NO_AFFINITY | MSI_FLAG_MULTI_PCI_MSI |
  1267. MSI_FLAG_PCI_MSIX,
  1268. .chip = &advk_msi_irq_chip,
  1269. };
  1270. static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie)
  1271. {
  1272. struct device *dev = &pcie->pdev->dev;
  1273. raw_spin_lock_init(&pcie->msi_irq_lock);
  1274. mutex_init(&pcie->msi_used_lock);
  1275. pcie->msi_inner_domain =
  1276. irq_domain_add_linear(NULL, MSI_IRQ_NUM,
  1277. &advk_msi_domain_ops, pcie);
  1278. if (!pcie->msi_inner_domain)
  1279. return -ENOMEM;
  1280. pcie->msi_domain =
  1281. pci_msi_create_irq_domain(dev_fwnode(dev),
  1282. &advk_msi_domain_info,
  1283. pcie->msi_inner_domain);
  1284. if (!pcie->msi_domain) {
  1285. irq_domain_remove(pcie->msi_inner_domain);
  1286. return -ENOMEM;
  1287. }
  1288. return 0;
  1289. }
  1290. static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie)
  1291. {
  1292. irq_domain_remove(pcie->msi_domain);
  1293. irq_domain_remove(pcie->msi_inner_domain);
  1294. }
  1295. static int advk_pcie_init_irq_domain(struct advk_pcie *pcie)
  1296. {
  1297. struct device *dev = &pcie->pdev->dev;
  1298. struct device_node *node = dev->of_node;
  1299. struct device_node *pcie_intc_node;
  1300. struct irq_chip *irq_chip;
  1301. int ret = 0;
  1302. raw_spin_lock_init(&pcie->irq_lock);
  1303. pcie_intc_node = of_get_next_child(node, NULL);
  1304. if (!pcie_intc_node) {
  1305. dev_err(dev, "No PCIe Intc node found\n");
  1306. return -ENODEV;
  1307. }
  1308. irq_chip = &pcie->irq_chip;
  1309. irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq",
  1310. dev_name(dev));
  1311. if (!irq_chip->name) {
  1312. ret = -ENOMEM;
  1313. goto out_put_node;
  1314. }
  1315. irq_chip->irq_mask = advk_pcie_irq_mask;
  1316. irq_chip->irq_unmask = advk_pcie_irq_unmask;
  1317. pcie->irq_domain =
  1318. irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
  1319. &advk_pcie_irq_domain_ops, pcie);
  1320. if (!pcie->irq_domain) {
  1321. dev_err(dev, "Failed to get a INTx IRQ domain\n");
  1322. ret = -ENOMEM;
  1323. goto out_put_node;
  1324. }
  1325. out_put_node:
  1326. of_node_put(pcie_intc_node);
  1327. return ret;
  1328. }
  1329. static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie)
  1330. {
  1331. irq_domain_remove(pcie->irq_domain);
  1332. }
  1333. static struct irq_chip advk_rp_irq_chip = {
  1334. .name = "advk-RP",
  1335. };
  1336. static int advk_pcie_rp_irq_map(struct irq_domain *h,
  1337. unsigned int virq, irq_hw_number_t hwirq)
  1338. {
  1339. struct advk_pcie *pcie = h->host_data;
  1340. irq_set_chip_and_handler(virq, &advk_rp_irq_chip, handle_simple_irq);
  1341. irq_set_chip_data(virq, pcie);
  1342. return 0;
  1343. }
  1344. static const struct irq_domain_ops advk_pcie_rp_irq_domain_ops = {
  1345. .map = advk_pcie_rp_irq_map,
  1346. .xlate = irq_domain_xlate_onecell,
  1347. };
  1348. static int advk_pcie_init_rp_irq_domain(struct advk_pcie *pcie)
  1349. {
  1350. pcie->rp_irq_domain = irq_domain_add_linear(NULL, 1,
  1351. &advk_pcie_rp_irq_domain_ops,
  1352. pcie);
  1353. if (!pcie->rp_irq_domain) {
  1354. dev_err(&pcie->pdev->dev, "Failed to add Root Port IRQ domain\n");
  1355. return -ENOMEM;
  1356. }
  1357. return 0;
  1358. }
  1359. static void advk_pcie_remove_rp_irq_domain(struct advk_pcie *pcie)
  1360. {
  1361. irq_domain_remove(pcie->rp_irq_domain);
  1362. }
  1363. static void advk_pcie_handle_pme(struct advk_pcie *pcie)
  1364. {
  1365. u32 requester = advk_readl(pcie, PCIE_MSG_LOG_REG) >> 16;
  1366. advk_writel(pcie, PCIE_MSG_PM_PME_MASK, PCIE_ISR0_REG);
  1367. /*
  1368. * PCIE_MSG_LOG_REG contains the last inbound message, so store
  1369. * the requester ID only when PME was not asserted yet.
  1370. * Also do not trigger PME interrupt when PME is still asserted.
  1371. */
  1372. if (!(le32_to_cpu(pcie->bridge.pcie_conf.rootsta) & PCI_EXP_RTSTA_PME)) {
  1373. pcie->bridge.pcie_conf.rootsta = cpu_to_le32(requester | PCI_EXP_RTSTA_PME);
  1374. /*
  1375. * Trigger PME interrupt only if PMEIE bit in Root Control is set.
  1376. * Aardvark HW returns zero for PCI_EXP_FLAGS_IRQ, so use PCIe interrupt 0.
  1377. */
  1378. if (!(le16_to_cpu(pcie->bridge.pcie_conf.rootctl) & PCI_EXP_RTCTL_PMEIE))
  1379. return;
  1380. if (generic_handle_domain_irq(pcie->rp_irq_domain, 0) == -EINVAL)
  1381. dev_err_ratelimited(&pcie->pdev->dev, "unhandled PME IRQ\n");
  1382. }
  1383. }
  1384. static void advk_pcie_handle_msi(struct advk_pcie *pcie)
  1385. {
  1386. u32 msi_val, msi_mask, msi_status, msi_idx;
  1387. msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG);
  1388. msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG);
  1389. msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK);
  1390. for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) {
  1391. if (!(BIT(msi_idx) & msi_status))
  1392. continue;
  1393. advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG);
  1394. if (generic_handle_domain_irq(pcie->msi_inner_domain, msi_idx) == -EINVAL)
  1395. dev_err_ratelimited(&pcie->pdev->dev, "unexpected MSI 0x%02x\n", msi_idx);
  1396. }
  1397. advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING,
  1398. PCIE_ISR0_REG);
  1399. }
  1400. static void advk_pcie_handle_int(struct advk_pcie *pcie)
  1401. {
  1402. u32 isr0_val, isr0_mask, isr0_status;
  1403. u32 isr1_val, isr1_mask, isr1_status;
  1404. int i;
  1405. isr0_val = advk_readl(pcie, PCIE_ISR0_REG);
  1406. isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG);
  1407. isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK);
  1408. isr1_val = advk_readl(pcie, PCIE_ISR1_REG);
  1409. isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG);
  1410. isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK);
  1411. /* Process PME interrupt as the first one to do not miss PME requester id */
  1412. if (isr0_status & PCIE_MSG_PM_PME_MASK)
  1413. advk_pcie_handle_pme(pcie);
  1414. /* Process ERR interrupt */
  1415. if (isr0_status & PCIE_ISR0_ERR_MASK) {
  1416. advk_writel(pcie, PCIE_ISR0_ERR_MASK, PCIE_ISR0_REG);
  1417. /*
  1418. * Aardvark HW returns zero for PCI_ERR_ROOT_AER_IRQ, so use
  1419. * PCIe interrupt 0
  1420. */
  1421. if (generic_handle_domain_irq(pcie->rp_irq_domain, 0) == -EINVAL)
  1422. dev_err_ratelimited(&pcie->pdev->dev, "unhandled ERR IRQ\n");
  1423. }
  1424. /* Process MSI interrupts */
  1425. if (isr0_status & PCIE_ISR0_MSI_INT_PENDING)
  1426. advk_pcie_handle_msi(pcie);
  1427. /* Process legacy interrupts */
  1428. for (i = 0; i < PCI_NUM_INTX; i++) {
  1429. if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i)))
  1430. continue;
  1431. advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i),
  1432. PCIE_ISR1_REG);
  1433. if (generic_handle_domain_irq(pcie->irq_domain, i) == -EINVAL)
  1434. dev_err_ratelimited(&pcie->pdev->dev, "unexpected INT%c IRQ\n",
  1435. (char)i + 'A');
  1436. }
  1437. }
  1438. static irqreturn_t advk_pcie_irq_handler(int irq, void *arg)
  1439. {
  1440. struct advk_pcie *pcie = arg;
  1441. u32 status;
  1442. status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG);
  1443. if (!(status & PCIE_IRQ_CORE_INT))
  1444. return IRQ_NONE;
  1445. advk_pcie_handle_int(pcie);
  1446. /* Clear interrupt */
  1447. advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG);
  1448. return IRQ_HANDLED;
  1449. }
  1450. static int advk_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  1451. {
  1452. struct advk_pcie *pcie = dev->bus->sysdata;
  1453. /*
  1454. * Emulated root bridge has its own emulated irq chip and irq domain.
  1455. * Argument pin is the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD) and
  1456. * hwirq for irq_create_mapping() is indexed from zero.
  1457. */
  1458. if (pci_is_root_bus(dev->bus))
  1459. return irq_create_mapping(pcie->rp_irq_domain, pin - 1);
  1460. else
  1461. return of_irq_parse_and_map_pci(dev, slot, pin);
  1462. }
  1463. static void advk_pcie_disable_phy(struct advk_pcie *pcie)
  1464. {
  1465. phy_power_off(pcie->phy);
  1466. phy_exit(pcie->phy);
  1467. }
  1468. static int advk_pcie_enable_phy(struct advk_pcie *pcie)
  1469. {
  1470. int ret;
  1471. if (!pcie->phy)
  1472. return 0;
  1473. ret = phy_init(pcie->phy);
  1474. if (ret)
  1475. return ret;
  1476. ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE);
  1477. if (ret) {
  1478. phy_exit(pcie->phy);
  1479. return ret;
  1480. }
  1481. ret = phy_power_on(pcie->phy);
  1482. if (ret) {
  1483. phy_exit(pcie->phy);
  1484. return ret;
  1485. }
  1486. return 0;
  1487. }
  1488. static int advk_pcie_setup_phy(struct advk_pcie *pcie)
  1489. {
  1490. struct device *dev = &pcie->pdev->dev;
  1491. struct device_node *node = dev->of_node;
  1492. int ret = 0;
  1493. pcie->phy = devm_of_phy_get(dev, node, NULL);
  1494. if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER))
  1495. return PTR_ERR(pcie->phy);
  1496. /* Old bindings miss the PHY handle */
  1497. if (IS_ERR(pcie->phy)) {
  1498. dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy));
  1499. pcie->phy = NULL;
  1500. return 0;
  1501. }
  1502. ret = advk_pcie_enable_phy(pcie);
  1503. if (ret)
  1504. dev_err(dev, "Failed to initialize PHY (%d)\n", ret);
  1505. return ret;
  1506. }
  1507. static int advk_pcie_probe(struct platform_device *pdev)
  1508. {
  1509. struct device *dev = &pdev->dev;
  1510. struct advk_pcie *pcie;
  1511. struct pci_host_bridge *bridge;
  1512. struct resource_entry *entry;
  1513. int ret, irq;
  1514. bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie));
  1515. if (!bridge)
  1516. return -ENOMEM;
  1517. pcie = pci_host_bridge_priv(bridge);
  1518. pcie->pdev = pdev;
  1519. platform_set_drvdata(pdev, pcie);
  1520. resource_list_for_each_entry(entry, &bridge->windows) {
  1521. resource_size_t start = entry->res->start;
  1522. resource_size_t size = resource_size(entry->res);
  1523. unsigned long type = resource_type(entry->res);
  1524. u64 win_size;
  1525. /*
  1526. * Aardvark hardware allows to configure also PCIe window
  1527. * for config type 0 and type 1 mapping, but driver uses
  1528. * only PIO for issuing configuration transfers which does
  1529. * not use PCIe window configuration.
  1530. */
  1531. if (type != IORESOURCE_MEM && type != IORESOURCE_IO)
  1532. continue;
  1533. /*
  1534. * Skip transparent memory resources. Default outbound access
  1535. * configuration is set to transparent memory access so it
  1536. * does not need window configuration.
  1537. */
  1538. if (type == IORESOURCE_MEM && entry->offset == 0)
  1539. continue;
  1540. /*
  1541. * The n-th PCIe window is configured by tuple (match, remap, mask)
  1542. * and an access to address A uses this window if A matches the
  1543. * match with given mask.
  1544. * So every PCIe window size must be a power of two and every start
  1545. * address must be aligned to window size. Minimal size is 64 KiB
  1546. * because lower 16 bits of mask must be zero. Remapped address
  1547. * may have set only bits from the mask.
  1548. */
  1549. while (pcie->wins_count < OB_WIN_COUNT && size > 0) {
  1550. /* Calculate the largest aligned window size */
  1551. win_size = (1ULL << (fls64(size)-1)) |
  1552. (start ? (1ULL << __ffs64(start)) : 0);
  1553. win_size = 1ULL << __ffs64(win_size);
  1554. if (win_size < 0x10000)
  1555. break;
  1556. dev_dbg(dev,
  1557. "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n",
  1558. pcie->wins_count, (unsigned long long)start,
  1559. (unsigned long long)start + win_size, type);
  1560. if (type == IORESOURCE_IO) {
  1561. pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO;
  1562. pcie->wins[pcie->wins_count].match = pci_pio_to_address(start);
  1563. } else {
  1564. pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM;
  1565. pcie->wins[pcie->wins_count].match = start;
  1566. }
  1567. pcie->wins[pcie->wins_count].remap = start - entry->offset;
  1568. pcie->wins[pcie->wins_count].mask = ~(win_size - 1);
  1569. if (pcie->wins[pcie->wins_count].remap & (win_size - 1))
  1570. break;
  1571. start += win_size;
  1572. size -= win_size;
  1573. pcie->wins_count++;
  1574. }
  1575. if (size > 0) {
  1576. dev_err(&pcie->pdev->dev,
  1577. "Invalid PCIe region [0x%llx-0x%llx]\n",
  1578. (unsigned long long)entry->res->start,
  1579. (unsigned long long)entry->res->end + 1);
  1580. return -EINVAL;
  1581. }
  1582. }
  1583. pcie->base = devm_platform_ioremap_resource(pdev, 0);
  1584. if (IS_ERR(pcie->base))
  1585. return PTR_ERR(pcie->base);
  1586. irq = platform_get_irq(pdev, 0);
  1587. if (irq < 0)
  1588. return irq;
  1589. ret = devm_request_irq(dev, irq, advk_pcie_irq_handler,
  1590. IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie",
  1591. pcie);
  1592. if (ret) {
  1593. dev_err(dev, "Failed to register interrupt\n");
  1594. return ret;
  1595. }
  1596. pcie->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
  1597. ret = PTR_ERR_OR_ZERO(pcie->reset_gpio);
  1598. if (ret) {
  1599. if (ret != -EPROBE_DEFER)
  1600. dev_err(dev, "Failed to get reset-gpio: %i\n", ret);
  1601. return ret;
  1602. }
  1603. ret = gpiod_set_consumer_name(pcie->reset_gpio, "pcie1-reset");
  1604. if (ret) {
  1605. dev_err(dev, "Failed to set reset gpio name: %d\n", ret);
  1606. return ret;
  1607. }
  1608. ret = of_pci_get_max_link_speed(dev->of_node);
  1609. if (ret <= 0 || ret > 3)
  1610. pcie->link_gen = 3;
  1611. else
  1612. pcie->link_gen = ret;
  1613. ret = advk_pcie_setup_phy(pcie);
  1614. if (ret)
  1615. return ret;
  1616. advk_pcie_setup_hw(pcie);
  1617. ret = advk_sw_pci_bridge_init(pcie);
  1618. if (ret) {
  1619. dev_err(dev, "Failed to register emulated root PCI bridge\n");
  1620. return ret;
  1621. }
  1622. ret = advk_pcie_init_irq_domain(pcie);
  1623. if (ret) {
  1624. dev_err(dev, "Failed to initialize irq\n");
  1625. return ret;
  1626. }
  1627. ret = advk_pcie_init_msi_irq_domain(pcie);
  1628. if (ret) {
  1629. dev_err(dev, "Failed to initialize irq\n");
  1630. advk_pcie_remove_irq_domain(pcie);
  1631. return ret;
  1632. }
  1633. ret = advk_pcie_init_rp_irq_domain(pcie);
  1634. if (ret) {
  1635. dev_err(dev, "Failed to initialize irq\n");
  1636. advk_pcie_remove_msi_irq_domain(pcie);
  1637. advk_pcie_remove_irq_domain(pcie);
  1638. return ret;
  1639. }
  1640. bridge->sysdata = pcie;
  1641. bridge->ops = &advk_pcie_ops;
  1642. bridge->map_irq = advk_pcie_map_irq;
  1643. ret = pci_host_probe(bridge);
  1644. if (ret < 0) {
  1645. advk_pcie_remove_rp_irq_domain(pcie);
  1646. advk_pcie_remove_msi_irq_domain(pcie);
  1647. advk_pcie_remove_irq_domain(pcie);
  1648. return ret;
  1649. }
  1650. return 0;
  1651. }
  1652. static void advk_pcie_remove(struct platform_device *pdev)
  1653. {
  1654. struct advk_pcie *pcie = platform_get_drvdata(pdev);
  1655. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
  1656. u32 val;
  1657. int i;
  1658. /* Remove PCI bus with all devices */
  1659. pci_lock_rescan_remove();
  1660. pci_stop_root_bus(bridge->bus);
  1661. pci_remove_root_bus(bridge->bus);
  1662. pci_unlock_rescan_remove();
  1663. /* Disable Root Bridge I/O space, memory space and bus mastering */
  1664. val = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG);
  1665. val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  1666. advk_writel(pcie, val, PCIE_CORE_CMD_STATUS_REG);
  1667. /* Disable MSI */
  1668. val = advk_readl(pcie, PCIE_CORE_CTRL2_REG);
  1669. val &= ~PCIE_CORE_CTRL2_MSI_ENABLE;
  1670. advk_writel(pcie, val, PCIE_CORE_CTRL2_REG);
  1671. /* Clear MSI address */
  1672. advk_writel(pcie, 0, PCIE_MSI_ADDR_LOW_REG);
  1673. advk_writel(pcie, 0, PCIE_MSI_ADDR_HIGH_REG);
  1674. /* Mask all interrupts */
  1675. advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG);
  1676. advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG);
  1677. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG);
  1678. advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_MASK_REG);
  1679. /* Clear all interrupts */
  1680. advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG);
  1681. advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG);
  1682. advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG);
  1683. advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG);
  1684. /* Remove IRQ domains */
  1685. advk_pcie_remove_rp_irq_domain(pcie);
  1686. advk_pcie_remove_msi_irq_domain(pcie);
  1687. advk_pcie_remove_irq_domain(pcie);
  1688. /* Free config space for emulated root bridge */
  1689. pci_bridge_emul_cleanup(&pcie->bridge);
  1690. /* Assert PERST# signal which prepares PCIe card for power down */
  1691. if (pcie->reset_gpio)
  1692. gpiod_set_value_cansleep(pcie->reset_gpio, 1);
  1693. /* Disable link training */
  1694. val = advk_readl(pcie, PCIE_CORE_CTRL0_REG);
  1695. val &= ~LINK_TRAINING_EN;
  1696. advk_writel(pcie, val, PCIE_CORE_CTRL0_REG);
  1697. /* Disable outbound address windows mapping */
  1698. for (i = 0; i < OB_WIN_COUNT; i++)
  1699. advk_pcie_disable_ob_win(pcie, i);
  1700. /* Disable phy */
  1701. advk_pcie_disable_phy(pcie);
  1702. }
  1703. static const struct of_device_id advk_pcie_of_match_table[] = {
  1704. { .compatible = "marvell,armada-3700-pcie", },
  1705. {},
  1706. };
  1707. MODULE_DEVICE_TABLE(of, advk_pcie_of_match_table);
  1708. static struct platform_driver advk_pcie_driver = {
  1709. .driver = {
  1710. .name = "advk-pcie",
  1711. .of_match_table = advk_pcie_of_match_table,
  1712. },
  1713. .probe = advk_pcie_probe,
  1714. .remove_new = advk_pcie_remove,
  1715. };
  1716. module_platform_driver(advk_pcie_driver);
  1717. MODULE_DESCRIPTION("Aardvark PCIe controller");
  1718. MODULE_LICENSE("GPL v2");