pcie-brcmstb.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* Copyright (C) 2009 - 2019 Broadcom */
  3. #include <linux/bitfield.h>
  4. #include <linux/bitops.h>
  5. #include <linux/clk.h>
  6. #include <linux/compiler.h>
  7. #include <linux/delay.h>
  8. #include <linux/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/io.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/ioport.h>
  13. #include <linux/irqchip/chained_irq.h>
  14. #include <linux/irqchip/irq-msi-lib.h>
  15. #include <linux/irqdomain.h>
  16. #include <linux/kdebug.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/log2.h>
  20. #include <linux/module.h>
  21. #include <linux/msi.h>
  22. #include <linux/notifier.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_irq.h>
  25. #include <linux/of_pci.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/panic_notifier.h>
  28. #include <linux/pci.h>
  29. #include <linux/pci-ecam.h>
  30. #include <linux/printk.h>
  31. #include <linux/regulator/consumer.h>
  32. #include <linux/reset.h>
  33. #include <linux/sizes.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/string.h>
  37. #include <linux/string_choices.h>
  38. #include <linux/types.h>
  39. #include "../pci.h"
  40. /* BRCM_PCIE_CAP_REGS - Offset for the mandatory capability config regs */
  41. #define BRCM_PCIE_CAP_REGS 0x00ac
  42. /* Broadcom STB PCIe Register Offsets */
  43. #define PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1 0x0188
  44. #define PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK 0xc
  45. #define PCIE_RC_CFG_VENDOR_SPECIFIC_REG1_LITTLE_ENDIAN 0x0
  46. #define PCIE_RC_CFG_PRIV1_ID_VAL3 0x043c
  47. #define PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK 0xffffff
  48. #define PCIE_RC_CFG_PRIV1_LINK_CAPABILITY 0x04dc
  49. #define PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_MAX_LINK_WIDTH_MASK 0x1f0
  50. #define PCIE_RC_CFG_PRIV1_ROOT_CAP 0x4f8
  51. #define PCIE_RC_CFG_PRIV1_ROOT_CAP_L1SS_MODE_MASK 0xf8
  52. #define PCIE_RC_DL_MDIO_ADDR 0x1100
  53. #define PCIE_RC_DL_MDIO_WR_DATA 0x1104
  54. #define PCIE_RC_DL_MDIO_RD_DATA 0x1108
  55. #define PCIE_RC_PL_REG_PHY_CTL_1 0x1804
  56. #define PCIE_RC_PL_REG_PHY_CTL_1_REG_P2_POWERDOWN_ENA_NOSYNC_MASK 0x8
  57. #define PCIE_RC_PL_PHY_CTL_15 0x184c
  58. #define PCIE_RC_PL_PHY_CTL_15_DIS_PLL_PD_MASK 0x400000
  59. #define PCIE_RC_PL_PHY_CTL_15_PM_CLK_PERIOD_MASK 0xff
  60. #define PCIE_MISC_MISC_CTRL 0x4008
  61. #define PCIE_MISC_MISC_CTRL_PCIE_RCB_64B_MODE_MASK 0x80
  62. #define PCIE_MISC_MISC_CTRL_PCIE_RCB_MPS_MODE_MASK 0x400
  63. #define PCIE_MISC_MISC_CTRL_SCB_ACCESS_EN_MASK 0x1000
  64. #define PCIE_MISC_MISC_CTRL_CFG_READ_UR_MODE_MASK 0x2000
  65. #define PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_MASK 0x300000
  66. #define PCIE_MISC_MISC_CTRL_SCB0_SIZE_MASK 0xf8000000
  67. #define PCIE_MISC_MISC_CTRL_SCB1_SIZE_MASK 0x07c00000
  68. #define PCIE_MISC_MISC_CTRL_SCB2_SIZE_MASK 0x0000001f
  69. #define SCB_SIZE_MASK(x) PCIE_MISC_MISC_CTRL_SCB ## x ## _SIZE_MASK
  70. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO 0x400c
  71. #define PCIE_MEM_WIN0_LO(win) \
  72. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LO + ((win) * 8)
  73. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI 0x4010
  74. #define PCIE_MEM_WIN0_HI(win) \
  75. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_HI + ((win) * 8)
  76. /*
  77. * NOTE: You may see the term "BAR" in a number of register names used by
  78. * this driver. The term is an artifact of when the HW core was an
  79. * endpoint device (EP). Now it is a root complex (RC) and anywhere a
  80. * register has the term "BAR" it is related to an inbound window.
  81. */
  82. #define PCIE_BRCM_MAX_INBOUND_WINS 16
  83. #define PCIE_MISC_RC_BAR1_CONFIG_LO 0x402c
  84. #define PCIE_MISC_RC_BAR1_CONFIG_LO_SIZE_MASK 0x1f
  85. #define PCIE_MISC_RC_BAR4_CONFIG_LO 0x40d4
  86. #define PCIE_MISC_MSI_BAR_CONFIG_LO 0x4044
  87. #define PCIE_MISC_MSI_BAR_CONFIG_HI 0x4048
  88. #define PCIE_MISC_MSI_DATA_CONFIG 0x404c
  89. #define PCIE_MISC_MSI_DATA_CONFIG_VAL_32 0xffe06540
  90. #define PCIE_MISC_MSI_DATA_CONFIG_VAL_8 0xfff86540
  91. #define PCIE_MISC_PCIE_CTRL 0x4064
  92. #define PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK 0x1
  93. #define PCIE_MISC_PCIE_CTRL_PCIE_PERSTB_MASK 0x4
  94. #define PCIE_MISC_PCIE_STATUS 0x4068
  95. #define PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK 0x80
  96. #define PCIE_MISC_PCIE_STATUS_PCIE_DL_ACTIVE_MASK 0x20
  97. #define PCIE_MISC_PCIE_STATUS_PCIE_PHYLINKUP_MASK 0x10
  98. #define PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK 0x40
  99. #define PCIE_MISC_REVISION 0x406c
  100. #define BRCM_PCIE_HW_REV_33 0x0303
  101. #define BRCM_PCIE_HW_REV_3_20 0x0320
  102. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT 0x4070
  103. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_LIMIT_MASK 0xfff00000
  104. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK 0xfff0
  105. #define PCIE_MEM_WIN0_BASE_LIMIT(win) \
  106. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT + ((win) * 4)
  107. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI 0x4080
  108. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI_BASE_MASK 0xff
  109. #define PCIE_MEM_WIN0_BASE_HI(win) \
  110. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI + ((win) * 8)
  111. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI 0x4084
  112. #define PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK 0xff
  113. #define PCIE_MEM_WIN0_LIMIT_HI(win) \
  114. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI + ((win) * 8)
  115. #define PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK 0x2
  116. #define PCIE_MISC_HARD_PCIE_HARD_DEBUG_L1SS_ENABLE_MASK 0x200000
  117. #define PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK 0x08000000
  118. #define PCIE_BMIPS_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK 0x00800000
  119. #define PCIE_CLKREQ_MASK \
  120. (PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK | \
  121. PCIE_MISC_HARD_PCIE_HARD_DEBUG_L1SS_ENABLE_MASK)
  122. #define PCIE_MISC_UBUS_BAR1_CONFIG_REMAP 0x40ac
  123. #define PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_ACCESS_EN_MASK BIT(0)
  124. #define PCIE_MISC_UBUS_BAR4_CONFIG_REMAP 0x410c
  125. #define PCIE_MSI_INTR2_BASE 0x4500
  126. /* Offsets from INTR2_CPU and MSI_INTR2 BASE offsets */
  127. #define MSI_INT_STATUS 0x0
  128. #define MSI_INT_CLR 0x8
  129. #define MSI_INT_MASK_SET 0x10
  130. #define MSI_INT_MASK_CLR 0x14
  131. /* Error report registers */
  132. #define PCIE_OUTB_ERR_TREAT 0x6000
  133. #define PCIE_OUTB_ERR_TREAT_CONFIG 0x1
  134. #define PCIE_OUTB_ERR_TREAT_MEM 0x2
  135. #define PCIE_OUTB_ERR_VALID 0x6004
  136. #define PCIE_OUTB_ERR_CLEAR 0x6008
  137. #define PCIE_OUTB_ERR_ACC_INFO 0x600c
  138. #define PCIE_OUTB_ERR_ACC_INFO_CFG_ERR BIT(0)
  139. #define PCIE_OUTB_ERR_ACC_INFO_MEM_ERR BIT(1)
  140. #define PCIE_OUTB_ERR_ACC_INFO_TYPE_64 BIT(2)
  141. #define PCIE_OUTB_ERR_ACC_INFO_DIR_WRITE BIT(4)
  142. #define PCIE_OUTB_ERR_ACC_INFO_BYTE_LANES 0xff00
  143. #define PCIE_OUTB_ERR_ACC_ADDR 0x6010
  144. #define PCIE_OUTB_ERR_ACC_ADDR_BUS 0xff00000
  145. #define PCIE_OUTB_ERR_ACC_ADDR_DEV 0xf8000
  146. #define PCIE_OUTB_ERR_ACC_ADDR_FUNC 0x7000
  147. #define PCIE_OUTB_ERR_ACC_ADDR_REG 0xfff
  148. #define PCIE_OUTB_ERR_CFG_CAUSE 0x6014
  149. #define PCIE_OUTB_ERR_CFG_CAUSE_TIMEOUT BIT(6)
  150. #define PCIE_OUTB_ERR_CFG_CAUSE_ABORT BIT(5)
  151. #define PCIE_OUTB_ERR_CFG_CAUSE_UNSUPP_REQ BIT(4)
  152. #define PCIE_OUTB_ERR_CFG_CAUSE_ACC_TIMEOUT BIT(2)
  153. #define PCIE_OUTB_ERR_CFG_CAUSE_ACC_DISABLED BIT(1)
  154. #define PCIE_OUTB_ERR_CFG_CAUSE_ACC_64BIT BIT(0)
  155. #define PCIE_OUTB_ERR_MEM_ADDR_LO 0x6018
  156. #define PCIE_OUTB_ERR_MEM_ADDR_HI 0x601c
  157. #define PCIE_OUTB_ERR_MEM_CAUSE 0x6020
  158. #define PCIE_OUTB_ERR_MEM_CAUSE_TIMEOUT BIT(6)
  159. #define PCIE_OUTB_ERR_MEM_CAUSE_ABORT BIT(5)
  160. #define PCIE_OUTB_ERR_MEM_CAUSE_UNSUPP_REQ BIT(4)
  161. #define PCIE_OUTB_ERR_MEM_CAUSE_ACC_DISABLED BIT(1)
  162. #define PCIE_OUTB_ERR_MEM_CAUSE_BAD_ADDR BIT(0)
  163. #define PCIE_RGR1_SW_INIT_1_PERST_MASK 0x1
  164. #define RGR1_SW_INIT_1_INIT_GENERIC_MASK 0x2
  165. #define RGR1_SW_INIT_1_INIT_GENERIC_SHIFT 0x1
  166. #define RGR1_SW_INIT_1_INIT_7278_MASK 0x1
  167. #define RGR1_SW_INIT_1_INIT_7278_SHIFT 0x0
  168. /* PCIe parameters */
  169. #define BRCM_NUM_PCIE_OUT_WINS 0x4
  170. #define BRCM_INT_PCI_MSI_NR 32
  171. #define BRCM_INT_PCI_MSI_LEGACY_NR 8
  172. #define BRCM_INT_PCI_MSI_SHIFT 0
  173. #define BRCM_INT_PCI_MSI_MASK GENMASK(BRCM_INT_PCI_MSI_NR - 1, 0)
  174. #define BRCM_INT_PCI_MSI_LEGACY_MASK GENMASK(31, \
  175. 32 - BRCM_INT_PCI_MSI_LEGACY_NR)
  176. /* MSI target addresses */
  177. #define BRCM_MSI_TARGET_ADDR_LT_4GB 0x0fffffffcULL
  178. #define BRCM_MSI_TARGET_ADDR_GT_4GB 0xffffffffcULL
  179. /* MDIO registers */
  180. #define MDIO_PORT0 0x0
  181. #define MDIO_DATA_MASK 0x7fffffff
  182. #define MDIO_PORT_MASK 0xf0000
  183. #define MDIO_PORT_EXT_MASK 0x200000
  184. #define MDIO_REGAD_MASK 0xffff
  185. #define MDIO_CMD_MASK 0x00100000
  186. #define MDIO_CMD_READ 0x1
  187. #define MDIO_CMD_WRITE 0x0
  188. #define MDIO_DATA_DONE_MASK 0x80000000
  189. #define MDIO_RD_DONE(x) (((x) & MDIO_DATA_DONE_MASK) ? 1 : 0)
  190. #define MDIO_WT_DONE(x) (((x) & MDIO_DATA_DONE_MASK) ? 0 : 1)
  191. #define SSC_REGS_ADDR 0x1100
  192. #define SET_ADDR_OFFSET 0x1f
  193. #define SSC_CNTL_OFFSET 0x2
  194. #define SSC_CNTL_OVRD_EN_MASK 0x8000
  195. #define SSC_CNTL_OVRD_VAL_MASK 0x4000
  196. #define SSC_STATUS_OFFSET 0x1
  197. #define SSC_STATUS_SSC_MASK 0x400
  198. #define SSC_STATUS_PLL_LOCK_MASK 0x800
  199. #define PCIE_BRCM_MAX_MEMC 3
  200. #define IDX_ADDR(pcie) ((pcie)->cfg->offsets[EXT_CFG_INDEX])
  201. #define DATA_ADDR(pcie) ((pcie)->cfg->offsets[EXT_CFG_DATA])
  202. #define PCIE_RGR1_SW_INIT_1(pcie) ((pcie)->cfg->offsets[RGR1_SW_INIT_1])
  203. #define HARD_DEBUG(pcie) ((pcie)->cfg->offsets[PCIE_HARD_DEBUG])
  204. #define INTR2_CPU_BASE(pcie) ((pcie)->cfg->offsets[PCIE_INTR2_CPU_BASE])
  205. /* Rescal registers */
  206. #define PCIE_DVT_PMU_PCIE_PHY_CTRL 0xc700
  207. #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS 0x3
  208. #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_MASK 0x4
  209. #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_SHIFT 0x2
  210. #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_MASK 0x2
  211. #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_SHIFT 0x1
  212. #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_MASK 0x1
  213. #define PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_SHIFT 0x0
  214. /* Forward declarations */
  215. struct brcm_pcie;
  216. enum {
  217. RGR1_SW_INIT_1,
  218. EXT_CFG_INDEX,
  219. EXT_CFG_DATA,
  220. PCIE_HARD_DEBUG,
  221. PCIE_INTR2_CPU_BASE,
  222. };
  223. enum pcie_soc_base {
  224. GENERIC,
  225. BCM2711,
  226. BCM4908,
  227. BCM7278,
  228. BCM7425,
  229. BCM7435,
  230. BCM7712,
  231. };
  232. struct inbound_win {
  233. u64 size;
  234. u64 pci_offset;
  235. u64 cpu_addr;
  236. };
  237. /*
  238. * The RESCAL block is tied to PCIe controller #1, regardless of the number of
  239. * controllers, and turning off PCIe controller #1 prevents access to the RESCAL
  240. * register blocks, therefore no other controller can access this register
  241. * space, and depending upon the bus fabric we may get a timeout (UBUS/GISB),
  242. * or a hang (AXI).
  243. */
  244. #define CFG_QUIRK_AVOID_BRIDGE_SHUTDOWN BIT(0)
  245. struct pcie_cfg_data {
  246. const int *offsets;
  247. const enum pcie_soc_base soc_base;
  248. const bool has_phy;
  249. const u32 quirks;
  250. u8 num_inbound_wins;
  251. int (*perst_set)(struct brcm_pcie *pcie, u32 val);
  252. int (*bridge_sw_init_set)(struct brcm_pcie *pcie, u32 val);
  253. int (*post_setup)(struct brcm_pcie *pcie);
  254. bool has_err_report;
  255. };
  256. struct subdev_regulators {
  257. unsigned int num_supplies;
  258. struct regulator_bulk_data supplies[];
  259. };
  260. struct brcm_msi {
  261. struct device *dev;
  262. void __iomem *base;
  263. struct device_node *np;
  264. struct irq_domain *inner_domain;
  265. struct mutex lock; /* guards the alloc/free operations */
  266. u64 target_addr;
  267. int irq;
  268. DECLARE_BITMAP(used, BRCM_INT_PCI_MSI_NR);
  269. bool legacy;
  270. /* Some chips have MSIs in bits [31..24] of a shared register. */
  271. int legacy_shift;
  272. int nr; /* No. of MSI available, depends on chip */
  273. /* This is the base pointer for interrupt status/set/clr regs */
  274. void __iomem *intr_base;
  275. };
  276. /* Internal PCIe Host Controller Information.*/
  277. struct brcm_pcie {
  278. struct device *dev;
  279. void __iomem *base;
  280. struct clk *clk;
  281. struct device_node *np;
  282. bool ssc;
  283. int gen;
  284. u64 msi_target_addr;
  285. struct brcm_msi *msi;
  286. struct reset_control *rescal;
  287. struct reset_control *perst_reset;
  288. struct reset_control *bridge_reset;
  289. struct reset_control *swinit_reset;
  290. int num_memc;
  291. u64 memc_size[PCIE_BRCM_MAX_MEMC];
  292. u32 hw_rev;
  293. struct subdev_regulators *sr;
  294. bool ep_wakeup_capable;
  295. const struct pcie_cfg_data *cfg;
  296. bool bridge_in_reset;
  297. struct notifier_block die_notifier;
  298. struct notifier_block panic_notifier;
  299. spinlock_t bridge_lock;
  300. };
  301. static inline bool is_bmips(const struct brcm_pcie *pcie)
  302. {
  303. return pcie->cfg->soc_base == BCM7435 || pcie->cfg->soc_base == BCM7425;
  304. }
  305. static int brcm_pcie_bridge_sw_init_set(struct brcm_pcie *pcie, u32 val)
  306. {
  307. unsigned long flags;
  308. int ret;
  309. if (pcie->cfg->has_err_report)
  310. spin_lock_irqsave(&pcie->bridge_lock, flags);
  311. ret = pcie->cfg->bridge_sw_init_set(pcie, val);
  312. /* If we fail, assume the bridge is in reset (off) */
  313. pcie->bridge_in_reset = ret ? true : val;
  314. if (pcie->cfg->has_err_report)
  315. spin_unlock_irqrestore(&pcie->bridge_lock, flags);
  316. return ret;
  317. }
  318. /*
  319. * This is to convert the size of the inbound "BAR" region to the
  320. * non-linear values of PCIE_X_MISC_RC_BAR[123]_CONFIG_LO.SIZE
  321. */
  322. static int brcm_pcie_encode_ibar_size(u64 size)
  323. {
  324. int log2_in = ilog2(size);
  325. if (log2_in >= 12 && log2_in <= 15)
  326. /* Covers 4KB to 32KB (inclusive) */
  327. return (log2_in - 12) + 0x1c;
  328. else if (log2_in >= 16 && log2_in <= 36)
  329. /* Covers 64KB to 64GB, (inclusive) */
  330. return log2_in - 15;
  331. /* Something is awry so disable */
  332. return 0;
  333. }
  334. static u32 brcm_pcie_mdio_form_pkt(int port, int regad, int cmd)
  335. {
  336. u32 pkt = 0;
  337. pkt |= FIELD_PREP(MDIO_PORT_EXT_MASK, port >> 4);
  338. pkt |= FIELD_PREP(MDIO_PORT_MASK, port);
  339. pkt |= FIELD_PREP(MDIO_REGAD_MASK, regad);
  340. pkt |= FIELD_PREP(MDIO_CMD_MASK, cmd);
  341. return pkt;
  342. }
  343. /* negative return value indicates error */
  344. static int brcm_pcie_mdio_read(void __iomem *base, u8 port, u8 regad, u32 *val)
  345. {
  346. u32 data;
  347. int err;
  348. writel(brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_READ),
  349. base + PCIE_RC_DL_MDIO_ADDR);
  350. readl(base + PCIE_RC_DL_MDIO_ADDR);
  351. err = readl_poll_timeout_atomic(base + PCIE_RC_DL_MDIO_RD_DATA, data,
  352. MDIO_RD_DONE(data), 10, 100);
  353. *val = FIELD_GET(MDIO_DATA_MASK, data);
  354. return err;
  355. }
  356. /* negative return value indicates error */
  357. static int brcm_pcie_mdio_write(void __iomem *base, u8 port,
  358. u8 regad, u16 wrdata)
  359. {
  360. u32 data;
  361. int err;
  362. writel(brcm_pcie_mdio_form_pkt(port, regad, MDIO_CMD_WRITE),
  363. base + PCIE_RC_DL_MDIO_ADDR);
  364. readl(base + PCIE_RC_DL_MDIO_ADDR);
  365. writel(MDIO_DATA_DONE_MASK | wrdata, base + PCIE_RC_DL_MDIO_WR_DATA);
  366. err = readl_poll_timeout_atomic(base + PCIE_RC_DL_MDIO_WR_DATA, data,
  367. MDIO_WT_DONE(data), 10, 100);
  368. return err;
  369. }
  370. /*
  371. * Configures device for Spread Spectrum Clocking (SSC) mode; a negative
  372. * return value indicates error.
  373. */
  374. static int brcm_pcie_set_ssc(struct brcm_pcie *pcie)
  375. {
  376. int pll, ssc;
  377. int ret;
  378. u32 tmp;
  379. ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET,
  380. SSC_REGS_ADDR);
  381. if (ret < 0)
  382. return ret;
  383. ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
  384. SSC_CNTL_OFFSET, &tmp);
  385. if (ret < 0)
  386. return ret;
  387. u32p_replace_bits(&tmp, 1, SSC_CNTL_OVRD_EN_MASK);
  388. u32p_replace_bits(&tmp, 1, SSC_CNTL_OVRD_VAL_MASK);
  389. ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0,
  390. SSC_CNTL_OFFSET, tmp);
  391. if (ret < 0)
  392. return ret;
  393. usleep_range(1000, 2000);
  394. ret = brcm_pcie_mdio_read(pcie->base, MDIO_PORT0,
  395. SSC_STATUS_OFFSET, &tmp);
  396. if (ret < 0)
  397. return ret;
  398. ssc = FIELD_GET(SSC_STATUS_SSC_MASK, tmp);
  399. pll = FIELD_GET(SSC_STATUS_PLL_LOCK_MASK, tmp);
  400. return ssc && pll ? 0 : -EIO;
  401. }
  402. /* Limits operation to a specific generation (1, 2, or 3) */
  403. static void brcm_pcie_set_gen(struct brcm_pcie *pcie, int gen)
  404. {
  405. u16 lnkctl2 = readw(pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2);
  406. u32 lnkcap = readl(pcie->base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
  407. u32p_replace_bits(&lnkcap, gen, PCI_EXP_LNKCAP_SLS);
  408. writel(lnkcap, pcie->base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
  409. u16p_replace_bits(&lnkctl2, gen, PCI_EXP_LNKCTL2_TLS);
  410. writew(lnkctl2, pcie->base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKCTL2);
  411. }
  412. static void brcm_pcie_set_outbound_win(struct brcm_pcie *pcie,
  413. u8 win, u64 cpu_addr,
  414. u64 pcie_addr, u64 size)
  415. {
  416. u32 cpu_addr_mb_high, limit_addr_mb_high;
  417. phys_addr_t cpu_addr_mb, limit_addr_mb;
  418. int high_addr_shift;
  419. u32 tmp;
  420. /* Set the base of the pcie_addr window */
  421. writel(lower_32_bits(pcie_addr), pcie->base + PCIE_MEM_WIN0_LO(win));
  422. writel(upper_32_bits(pcie_addr), pcie->base + PCIE_MEM_WIN0_HI(win));
  423. /* Write the addr base & limit lower bits (in MBs) */
  424. cpu_addr_mb = cpu_addr / SZ_1M;
  425. limit_addr_mb = (cpu_addr + size - 1) / SZ_1M;
  426. tmp = readl(pcie->base + PCIE_MEM_WIN0_BASE_LIMIT(win));
  427. u32p_replace_bits(&tmp, cpu_addr_mb,
  428. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK);
  429. u32p_replace_bits(&tmp, limit_addr_mb,
  430. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_LIMIT_MASK);
  431. writel(tmp, pcie->base + PCIE_MEM_WIN0_BASE_LIMIT(win));
  432. if (is_bmips(pcie))
  433. return;
  434. /* Write the cpu & limit addr upper bits */
  435. high_addr_shift =
  436. HWEIGHT32(PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_LIMIT_BASE_MASK);
  437. cpu_addr_mb_high = cpu_addr_mb >> high_addr_shift;
  438. tmp = readl(pcie->base + PCIE_MEM_WIN0_BASE_HI(win));
  439. u32p_replace_bits(&tmp, cpu_addr_mb_high,
  440. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_BASE_HI_BASE_MASK);
  441. writel(tmp, pcie->base + PCIE_MEM_WIN0_BASE_HI(win));
  442. limit_addr_mb_high = limit_addr_mb >> high_addr_shift;
  443. tmp = readl(pcie->base + PCIE_MEM_WIN0_LIMIT_HI(win));
  444. u32p_replace_bits(&tmp, limit_addr_mb_high,
  445. PCIE_MISC_CPU_2_PCIE_MEM_WIN0_LIMIT_HI_LIMIT_MASK);
  446. writel(tmp, pcie->base + PCIE_MEM_WIN0_LIMIT_HI(win));
  447. }
  448. #define BRCM_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  449. MSI_FLAG_USE_DEF_CHIP_OPS | \
  450. MSI_FLAG_NO_AFFINITY)
  451. #define BRCM_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \
  452. MSI_FLAG_MULTI_PCI_MSI)
  453. static const struct msi_parent_ops brcm_msi_parent_ops = {
  454. .required_flags = BRCM_MSI_FLAGS_REQUIRED,
  455. .supported_flags = BRCM_MSI_FLAGS_SUPPORTED,
  456. .bus_select_token = DOMAIN_BUS_PCI_MSI,
  457. .chip_flags = MSI_CHIP_FLAG_SET_ACK,
  458. .prefix = "BRCM-",
  459. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  460. };
  461. static void brcm_pcie_msi_isr(struct irq_desc *desc)
  462. {
  463. struct irq_chip *chip = irq_desc_get_chip(desc);
  464. unsigned long status;
  465. struct brcm_msi *msi;
  466. struct device *dev;
  467. u32 bit;
  468. chained_irq_enter(chip, desc);
  469. msi = irq_desc_get_handler_data(desc);
  470. dev = msi->dev;
  471. status = readl(msi->intr_base + MSI_INT_STATUS);
  472. status >>= msi->legacy_shift;
  473. for_each_set_bit(bit, &status, msi->nr) {
  474. int ret;
  475. ret = generic_handle_domain_irq(msi->inner_domain, bit);
  476. if (ret)
  477. dev_dbg(dev, "unexpected MSI\n");
  478. }
  479. chained_irq_exit(chip, desc);
  480. }
  481. static void brcm_msi_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  482. {
  483. struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
  484. msg->address_lo = lower_32_bits(msi->target_addr);
  485. msg->address_hi = upper_32_bits(msi->target_addr);
  486. msg->data = (0xffff & PCIE_MISC_MSI_DATA_CONFIG_VAL_32) | data->hwirq;
  487. }
  488. static void brcm_msi_ack_irq(struct irq_data *data)
  489. {
  490. struct brcm_msi *msi = irq_data_get_irq_chip_data(data);
  491. const int shift_amt = data->hwirq + msi->legacy_shift;
  492. writel(1 << shift_amt, msi->intr_base + MSI_INT_CLR);
  493. }
  494. static struct irq_chip brcm_msi_bottom_irq_chip = {
  495. .name = "BRCM STB MSI",
  496. .irq_compose_msi_msg = brcm_msi_compose_msi_msg,
  497. .irq_ack = brcm_msi_ack_irq,
  498. };
  499. static int brcm_msi_alloc(struct brcm_msi *msi, unsigned int nr_irqs)
  500. {
  501. int hwirq;
  502. mutex_lock(&msi->lock);
  503. hwirq = bitmap_find_free_region(msi->used, msi->nr,
  504. order_base_2(nr_irqs));
  505. mutex_unlock(&msi->lock);
  506. return hwirq;
  507. }
  508. static void brcm_msi_free(struct brcm_msi *msi, unsigned long hwirq,
  509. unsigned int nr_irqs)
  510. {
  511. mutex_lock(&msi->lock);
  512. bitmap_release_region(msi->used, hwirq, order_base_2(nr_irqs));
  513. mutex_unlock(&msi->lock);
  514. }
  515. static int brcm_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  516. unsigned int nr_irqs, void *args)
  517. {
  518. struct brcm_msi *msi = domain->host_data;
  519. int hwirq, i;
  520. hwirq = brcm_msi_alloc(msi, nr_irqs);
  521. if (hwirq < 0)
  522. return hwirq;
  523. for (i = 0; i < nr_irqs; i++)
  524. irq_domain_set_info(domain, virq + i, (irq_hw_number_t)hwirq + i,
  525. &brcm_msi_bottom_irq_chip, domain->host_data,
  526. handle_edge_irq, NULL, NULL);
  527. return 0;
  528. }
  529. static void brcm_irq_domain_free(struct irq_domain *domain,
  530. unsigned int virq, unsigned int nr_irqs)
  531. {
  532. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  533. struct brcm_msi *msi = irq_data_get_irq_chip_data(d);
  534. brcm_msi_free(msi, d->hwirq, nr_irqs);
  535. }
  536. static const struct irq_domain_ops msi_domain_ops = {
  537. .alloc = brcm_irq_domain_alloc,
  538. .free = brcm_irq_domain_free,
  539. };
  540. static int brcm_allocate_domains(struct brcm_msi *msi)
  541. {
  542. struct device *dev = msi->dev;
  543. struct irq_domain_info info = {
  544. .fwnode = of_fwnode_handle(msi->np),
  545. .ops = &msi_domain_ops,
  546. .host_data = msi,
  547. .size = msi->nr,
  548. };
  549. msi->inner_domain = msi_create_parent_irq_domain(&info, &brcm_msi_parent_ops);
  550. if (!msi->inner_domain) {
  551. dev_err(dev, "failed to create MSI domain\n");
  552. return -ENOMEM;
  553. }
  554. return 0;
  555. }
  556. static void brcm_free_domains(struct brcm_msi *msi)
  557. {
  558. irq_domain_remove(msi->inner_domain);
  559. }
  560. static void brcm_msi_remove(struct brcm_pcie *pcie)
  561. {
  562. struct brcm_msi *msi = pcie->msi;
  563. if (!msi)
  564. return;
  565. irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
  566. brcm_free_domains(msi);
  567. }
  568. static void brcm_msi_set_regs(struct brcm_msi *msi)
  569. {
  570. u32 val = msi->legacy ? BRCM_INT_PCI_MSI_LEGACY_MASK :
  571. BRCM_INT_PCI_MSI_MASK;
  572. writel(val, msi->intr_base + MSI_INT_MASK_CLR);
  573. writel(val, msi->intr_base + MSI_INT_CLR);
  574. /*
  575. * The 0 bit of PCIE_MISC_MSI_BAR_CONFIG_LO is repurposed to MSI
  576. * enable, which we set to 1.
  577. */
  578. writel(lower_32_bits(msi->target_addr) | 0x1,
  579. msi->base + PCIE_MISC_MSI_BAR_CONFIG_LO);
  580. writel(upper_32_bits(msi->target_addr),
  581. msi->base + PCIE_MISC_MSI_BAR_CONFIG_HI);
  582. val = msi->legacy ? PCIE_MISC_MSI_DATA_CONFIG_VAL_8 : PCIE_MISC_MSI_DATA_CONFIG_VAL_32;
  583. writel(val, msi->base + PCIE_MISC_MSI_DATA_CONFIG);
  584. }
  585. static int brcm_pcie_enable_msi(struct brcm_pcie *pcie)
  586. {
  587. struct brcm_msi *msi;
  588. int irq, ret;
  589. struct device *dev = pcie->dev;
  590. irq = irq_of_parse_and_map(dev->of_node, 1);
  591. if (irq <= 0) {
  592. dev_err(dev, "cannot map MSI interrupt\n");
  593. return -ENODEV;
  594. }
  595. msi = devm_kzalloc(dev, sizeof(struct brcm_msi), GFP_KERNEL);
  596. if (!msi)
  597. return -ENOMEM;
  598. mutex_init(&msi->lock);
  599. msi->dev = dev;
  600. msi->base = pcie->base;
  601. msi->np = pcie->np;
  602. msi->target_addr = pcie->msi_target_addr;
  603. msi->irq = irq;
  604. msi->legacy = pcie->hw_rev < BRCM_PCIE_HW_REV_33;
  605. /*
  606. * Sanity check to make sure that the 'used' bitmap in struct brcm_msi
  607. * is large enough.
  608. */
  609. BUILD_BUG_ON(BRCM_INT_PCI_MSI_LEGACY_NR > BRCM_INT_PCI_MSI_NR);
  610. if (msi->legacy) {
  611. msi->intr_base = msi->base + INTR2_CPU_BASE(pcie);
  612. msi->nr = BRCM_INT_PCI_MSI_LEGACY_NR;
  613. msi->legacy_shift = 24;
  614. } else {
  615. msi->intr_base = msi->base + PCIE_MSI_INTR2_BASE;
  616. msi->nr = BRCM_INT_PCI_MSI_NR;
  617. msi->legacy_shift = 0;
  618. }
  619. ret = brcm_allocate_domains(msi);
  620. if (ret)
  621. return ret;
  622. irq_set_chained_handler_and_data(msi->irq, brcm_pcie_msi_isr, msi);
  623. brcm_msi_set_regs(msi);
  624. pcie->msi = msi;
  625. return 0;
  626. }
  627. /* The controller is capable of serving in both RC and EP roles */
  628. static bool brcm_pcie_rc_mode(struct brcm_pcie *pcie)
  629. {
  630. void __iomem *base = pcie->base;
  631. u32 val = readl(base + PCIE_MISC_PCIE_STATUS);
  632. return !!FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PORT_MASK, val);
  633. }
  634. static bool brcm_pcie_link_up(struct brcm_pcie *pcie)
  635. {
  636. u32 val = readl(pcie->base + PCIE_MISC_PCIE_STATUS);
  637. u32 dla = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_DL_ACTIVE_MASK, val);
  638. u32 plu = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_PHYLINKUP_MASK, val);
  639. return dla && plu;
  640. }
  641. static void __iomem *brcm_pcie_map_bus(struct pci_bus *bus,
  642. unsigned int devfn, int where)
  643. {
  644. struct brcm_pcie *pcie = bus->sysdata;
  645. void __iomem *base = pcie->base;
  646. int idx;
  647. /* Accesses to the RC go right to the RC registers if !devfn */
  648. if (pci_is_root_bus(bus))
  649. return devfn ? NULL : base + PCIE_ECAM_REG(where);
  650. /* An access to our HW w/o link-up will cause a CPU Abort */
  651. if (!brcm_pcie_link_up(pcie))
  652. return NULL;
  653. /* For devices, write to the config space index register */
  654. idx = PCIE_ECAM_OFFSET(bus->number, devfn, 0);
  655. writel(idx, base + IDX_ADDR(pcie));
  656. return base + DATA_ADDR(pcie) + PCIE_ECAM_REG(where);
  657. }
  658. static void __iomem *brcm7425_pcie_map_bus(struct pci_bus *bus,
  659. unsigned int devfn, int where)
  660. {
  661. struct brcm_pcie *pcie = bus->sysdata;
  662. void __iomem *base = pcie->base;
  663. int idx;
  664. /* Accesses to the RC go right to the RC registers if !devfn */
  665. if (pci_is_root_bus(bus))
  666. return devfn ? NULL : base + PCIE_ECAM_REG(where);
  667. /* An access to our HW w/o link-up will cause a CPU Abort */
  668. if (!brcm_pcie_link_up(pcie))
  669. return NULL;
  670. /* For devices, write to the config space index register */
  671. idx = PCIE_ECAM_OFFSET(bus->number, devfn, where);
  672. writel(idx, base + IDX_ADDR(pcie));
  673. return base + DATA_ADDR(pcie);
  674. }
  675. static int brcm_pcie_bridge_sw_init_set_generic(struct brcm_pcie *pcie, u32 val)
  676. {
  677. u32 tmp, mask = RGR1_SW_INIT_1_INIT_GENERIC_MASK;
  678. u32 shift = RGR1_SW_INIT_1_INIT_GENERIC_SHIFT;
  679. int ret = 0;
  680. if (pcie->bridge_reset) {
  681. if (val)
  682. ret = reset_control_assert(pcie->bridge_reset);
  683. else
  684. ret = reset_control_deassert(pcie->bridge_reset);
  685. if (ret)
  686. dev_err(pcie->dev, "failed to %s 'bridge' reset, err=%d\n",
  687. val ? "assert" : "deassert", ret);
  688. return ret;
  689. }
  690. tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
  691. tmp = (tmp & ~mask) | ((val << shift) & mask);
  692. writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
  693. return ret;
  694. }
  695. static int brcm_pcie_bridge_sw_init_set_7278(struct brcm_pcie *pcie, u32 val)
  696. {
  697. u32 tmp, mask = RGR1_SW_INIT_1_INIT_7278_MASK;
  698. u32 shift = RGR1_SW_INIT_1_INIT_7278_SHIFT;
  699. tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
  700. tmp = (tmp & ~mask) | ((val << shift) & mask);
  701. writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
  702. return 0;
  703. }
  704. static int brcm_pcie_perst_set_4908(struct brcm_pcie *pcie, u32 val)
  705. {
  706. int ret;
  707. if (WARN_ONCE(!pcie->perst_reset, "missing PERST# reset controller\n"))
  708. return -EINVAL;
  709. if (val)
  710. ret = reset_control_assert(pcie->perst_reset);
  711. else
  712. ret = reset_control_deassert(pcie->perst_reset);
  713. if (ret)
  714. dev_err(pcie->dev, "failed to %s 'perst' reset, err=%d\n",
  715. val ? "assert" : "deassert", ret);
  716. return ret;
  717. }
  718. static int brcm_pcie_perst_set_7278(struct brcm_pcie *pcie, u32 val)
  719. {
  720. u32 tmp;
  721. /* Perst bit has moved and assert value is 0 */
  722. tmp = readl(pcie->base + PCIE_MISC_PCIE_CTRL);
  723. u32p_replace_bits(&tmp, !val, PCIE_MISC_PCIE_CTRL_PCIE_PERSTB_MASK);
  724. writel(tmp, pcie->base + PCIE_MISC_PCIE_CTRL);
  725. return 0;
  726. }
  727. static int brcm_pcie_perst_set_generic(struct brcm_pcie *pcie, u32 val)
  728. {
  729. u32 tmp;
  730. tmp = readl(pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
  731. u32p_replace_bits(&tmp, val, PCIE_RGR1_SW_INIT_1_PERST_MASK);
  732. writel(tmp, pcie->base + PCIE_RGR1_SW_INIT_1(pcie));
  733. return 0;
  734. }
  735. static int brcm_pcie_post_setup_bcm2712(struct brcm_pcie *pcie)
  736. {
  737. static const u16 data[] = { 0x50b9, 0xbda1, 0x0094, 0x97b4, 0x5030,
  738. 0x5030, 0x0007 };
  739. static const u8 regs[] = { 0x16, 0x17, 0x18, 0x19, 0x1b, 0x1c, 0x1e };
  740. int ret, i;
  741. u32 tmp;
  742. /* Allow a 54MHz (xosc) refclk source */
  743. ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, SET_ADDR_OFFSET, 0x1600);
  744. if (ret < 0)
  745. return ret;
  746. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  747. ret = brcm_pcie_mdio_write(pcie->base, MDIO_PORT0, regs[i], data[i]);
  748. if (ret < 0)
  749. return ret;
  750. }
  751. usleep_range(100, 200);
  752. /*
  753. * Set L1SS sub-state timers to avoid lengthy state transitions,
  754. * PM clock period is 18.52ns (1/54MHz, round down).
  755. */
  756. tmp = readl(pcie->base + PCIE_RC_PL_PHY_CTL_15);
  757. tmp &= ~PCIE_RC_PL_PHY_CTL_15_PM_CLK_PERIOD_MASK;
  758. tmp |= 0x12;
  759. writel(tmp, pcie->base + PCIE_RC_PL_PHY_CTL_15);
  760. return 0;
  761. }
  762. static void add_inbound_win(struct inbound_win *b, u8 *count, u64 size,
  763. u64 cpu_addr, u64 pci_offset)
  764. {
  765. b->size = size;
  766. b->cpu_addr = cpu_addr;
  767. b->pci_offset = pci_offset;
  768. (*count)++;
  769. }
  770. static int brcm_pcie_get_inbound_wins(struct brcm_pcie *pcie,
  771. struct inbound_win inbound_wins[])
  772. {
  773. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
  774. u64 pci_offset, cpu_addr, size = 0, tot_size = 0;
  775. struct resource_entry *entry;
  776. struct device *dev = pcie->dev;
  777. u64 lowest_pcie_addr = ~(u64)0;
  778. int ret, i = 0;
  779. u8 n = 0;
  780. /*
  781. * The HW registers (and PCIe) use order-1 numbering for BARs. As such,
  782. * we have inbound_wins[0] unused and BAR1 starts at inbound_wins[1].
  783. */
  784. struct inbound_win *b_begin = &inbound_wins[1];
  785. struct inbound_win *b = b_begin;
  786. /*
  787. * STB chips beside 7712 disable the first inbound window default.
  788. * Rather being mapped to system memory it is mapped to the
  789. * internal registers of the SoC. This feature is deprecated, has
  790. * security considerations, and is not implemented in our modern
  791. * SoCs.
  792. */
  793. if (pcie->cfg->soc_base != BCM7712)
  794. add_inbound_win(b++, &n, 0, 0, 0);
  795. resource_list_for_each_entry(entry, &bridge->dma_ranges) {
  796. u64 pcie_start = entry->res->start - entry->offset;
  797. u64 cpu_start = entry->res->start;
  798. size = resource_size(entry->res);
  799. tot_size += size;
  800. if (pcie_start < lowest_pcie_addr)
  801. lowest_pcie_addr = pcie_start;
  802. /*
  803. * 7712 and newer chips may have many BARs, with each
  804. * offering a non-overlapping viewport to system memory.
  805. * That being said, each BARs size must still be a power of
  806. * two.
  807. */
  808. if (pcie->cfg->soc_base == BCM7712)
  809. add_inbound_win(b++, &n, size, cpu_start, pcie_start);
  810. if (n > pcie->cfg->num_inbound_wins)
  811. break;
  812. }
  813. if (lowest_pcie_addr == ~(u64)0) {
  814. dev_err(dev, "DT node has no dma-ranges\n");
  815. return -EINVAL;
  816. }
  817. /*
  818. * 7712 and newer chips do not have an internal memory mapping system
  819. * that enables multiple memory controllers. As such, it can return
  820. * now w/o doing special configuration.
  821. */
  822. if (pcie->cfg->soc_base == BCM7712)
  823. return n;
  824. ret = of_property_read_variable_u64_array(pcie->np, "brcm,scb-sizes", pcie->memc_size, 1,
  825. PCIE_BRCM_MAX_MEMC);
  826. if (ret <= 0) {
  827. /* Make an educated guess */
  828. pcie->num_memc = 1;
  829. pcie->memc_size[0] = 1ULL << fls64(tot_size - 1);
  830. } else {
  831. pcie->num_memc = ret;
  832. }
  833. /* Each memc is viewed through a "port" that is a power of 2 */
  834. for (i = 0, size = 0; i < pcie->num_memc; i++)
  835. size += pcie->memc_size[i];
  836. /* Our HW mandates that the window size must be a power of 2 */
  837. size = 1ULL << fls64(size - 1);
  838. /*
  839. * For STB chips, the BAR2 cpu_addr is hardwired to the start
  840. * of system memory, so we set it to 0.
  841. */
  842. cpu_addr = 0;
  843. pci_offset = lowest_pcie_addr;
  844. /*
  845. * We validate the inbound memory view even though we should trust
  846. * whatever the device-tree provides. This is because of an HW issue on
  847. * early Raspberry Pi 4's revisions (bcm2711). It turns out its
  848. * firmware has to dynamically edit dma-ranges due to a bug on the
  849. * PCIe controller integration, which prohibits any access above the
  850. * lower 3GB of memory. Given this, we decided to keep the dma-ranges
  851. * in check, avoiding hard to debug device-tree related issues in the
  852. * future:
  853. *
  854. * The PCIe host controller by design must set the inbound viewport to
  855. * be a contiguous arrangement of all of the system's memory. In
  856. * addition, its size must be a power of two. To further complicate
  857. * matters, the viewport must start on a pcie-address that is aligned
  858. * on a multiple of its size. If a portion of the viewport does not
  859. * represent system memory -- e.g. 3GB of memory requires a 4GB
  860. * viewport -- we can map the outbound memory in or after 3GB and even
  861. * though the viewport will overlap the outbound memory the controller
  862. * will know to send outbound memory downstream and everything else
  863. * upstream.
  864. *
  865. * For example:
  866. *
  867. * - The best-case scenario, memory up to 3GB, is to place the inbound
  868. * region in the first 4GB of pcie-space, as some legacy devices can
  869. * only address 32bits. We would also like to put the MSI under 4GB
  870. * as well, since some devices require a 32bit MSI target address.
  871. *
  872. * - If the system memory is 4GB or larger we cannot start the inbound
  873. * region at location 0 (since we have to allow some space for
  874. * outbound memory @ 3GB). So instead it will start at the 1x
  875. * multiple of its size
  876. */
  877. if (!size || (pci_offset & (size - 1)) ||
  878. (pci_offset < SZ_4G && pci_offset > SZ_2G)) {
  879. dev_err(dev, "Invalid inbound_win2_offset/size: size 0x%llx, off 0x%llx\n",
  880. size, pci_offset);
  881. return -EINVAL;
  882. }
  883. /* Enable inbound window 2, the main inbound window for STB chips */
  884. add_inbound_win(b++, &n, size, cpu_addr, pci_offset);
  885. /*
  886. * Disable inbound window 3. On some chips presents the same
  887. * window as #2 but the data appears in a settable endianness.
  888. */
  889. add_inbound_win(b++, &n, 0, 0, 0);
  890. return n;
  891. }
  892. static u32 brcm_bar_reg_offset(int bar)
  893. {
  894. if (bar <= 3)
  895. return PCIE_MISC_RC_BAR1_CONFIG_LO + 8 * (bar - 1);
  896. else
  897. return PCIE_MISC_RC_BAR4_CONFIG_LO + 8 * (bar - 4);
  898. }
  899. static u32 brcm_ubus_reg_offset(int bar)
  900. {
  901. if (bar <= 3)
  902. return PCIE_MISC_UBUS_BAR1_CONFIG_REMAP + 8 * (bar - 1);
  903. else
  904. return PCIE_MISC_UBUS_BAR4_CONFIG_REMAP + 8 * (bar - 4);
  905. }
  906. static void set_inbound_win_registers(struct brcm_pcie *pcie,
  907. const struct inbound_win *inbound_wins,
  908. u8 num_inbound_wins)
  909. {
  910. void __iomem *base = pcie->base;
  911. int i;
  912. for (i = 1; i <= num_inbound_wins; i++) {
  913. u64 pci_offset = inbound_wins[i].pci_offset;
  914. u64 cpu_addr = inbound_wins[i].cpu_addr;
  915. u64 size = inbound_wins[i].size;
  916. u32 reg_offset = brcm_bar_reg_offset(i);
  917. u32 tmp = lower_32_bits(pci_offset);
  918. u32p_replace_bits(&tmp, brcm_pcie_encode_ibar_size(size),
  919. PCIE_MISC_RC_BAR1_CONFIG_LO_SIZE_MASK);
  920. /* Write low */
  921. writel_relaxed(tmp, base + reg_offset);
  922. /* Write high */
  923. writel_relaxed(upper_32_bits(pci_offset), base + reg_offset + 4);
  924. /*
  925. * Most STB chips:
  926. * Do nothing.
  927. * 7712:
  928. * All of their BARs need to be set.
  929. */
  930. if (pcie->cfg->soc_base == BCM7712) {
  931. /* BUS remap register settings */
  932. reg_offset = brcm_ubus_reg_offset(i);
  933. tmp = lower_32_bits(cpu_addr) & ~0xfff;
  934. tmp |= PCIE_MISC_UBUS_BAR1_CONFIG_REMAP_ACCESS_EN_MASK;
  935. writel_relaxed(tmp, base + reg_offset);
  936. tmp = upper_32_bits(cpu_addr);
  937. writel_relaxed(tmp, base + reg_offset + 4);
  938. }
  939. }
  940. }
  941. static int brcm_pcie_setup(struct brcm_pcie *pcie)
  942. {
  943. struct inbound_win inbound_wins[PCIE_BRCM_MAX_INBOUND_WINS];
  944. void __iomem *base = pcie->base;
  945. struct pci_host_bridge *bridge;
  946. struct resource_entry *entry;
  947. u32 tmp, burst, num_lanes, num_lanes_cap;
  948. u8 num_out_wins = 0;
  949. int num_inbound_wins = 0;
  950. int memc, ret;
  951. /* Reset the bridge */
  952. ret = brcm_pcie_bridge_sw_init_set(pcie, 1);
  953. if (ret)
  954. return ret;
  955. /* Ensure that PERST# is asserted; some bootloaders may deassert it. */
  956. if (pcie->cfg->soc_base == BCM2711) {
  957. ret = pcie->cfg->perst_set(pcie, 1);
  958. if (ret) {
  959. pcie->cfg->bridge_sw_init_set(pcie, 0);
  960. return ret;
  961. }
  962. }
  963. usleep_range(100, 200);
  964. /* Take the bridge out of reset */
  965. ret = brcm_pcie_bridge_sw_init_set(pcie, 0);
  966. if (ret)
  967. return ret;
  968. tmp = readl(base + HARD_DEBUG(pcie));
  969. if (is_bmips(pcie))
  970. tmp &= ~PCIE_BMIPS_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK;
  971. else
  972. tmp &= ~PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK;
  973. writel(tmp, base + HARD_DEBUG(pcie));
  974. /* Wait for SerDes to be stable */
  975. usleep_range(100, 200);
  976. /*
  977. * SCB_MAX_BURST_SIZE is a two bit field. For GENERIC chips it
  978. * is encoded as 0=128, 1=256, 2=512, 3=Rsvd, for BCM7278 it
  979. * is encoded as 0=Rsvd, 1=128, 2=256, 3=512.
  980. */
  981. if (is_bmips(pcie))
  982. burst = 0x1; /* 256 bytes */
  983. else if (pcie->cfg->soc_base == BCM2711)
  984. burst = 0x0; /* 128 bytes */
  985. else if (pcie->cfg->soc_base == BCM7278)
  986. burst = 0x3; /* 512 bytes */
  987. else
  988. burst = 0x2; /* 512 bytes */
  989. /*
  990. * Set SCB_MAX_BURST_SIZE, CFG_READ_UR_MODE, SCB_ACCESS_EN,
  991. * RCB_MPS_MODE, RCB_64B_MODE
  992. */
  993. tmp = readl(base + PCIE_MISC_MISC_CTRL);
  994. u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_SCB_ACCESS_EN_MASK);
  995. u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_CFG_READ_UR_MODE_MASK);
  996. u32p_replace_bits(&tmp, burst, PCIE_MISC_MISC_CTRL_MAX_BURST_SIZE_MASK);
  997. u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_PCIE_RCB_MPS_MODE_MASK);
  998. u32p_replace_bits(&tmp, 1, PCIE_MISC_MISC_CTRL_PCIE_RCB_64B_MODE_MASK);
  999. writel(tmp, base + PCIE_MISC_MISC_CTRL);
  1000. num_inbound_wins = brcm_pcie_get_inbound_wins(pcie, inbound_wins);
  1001. if (num_inbound_wins < 0)
  1002. return num_inbound_wins;
  1003. set_inbound_win_registers(pcie, inbound_wins, num_inbound_wins);
  1004. if (!brcm_pcie_rc_mode(pcie)) {
  1005. dev_err(pcie->dev, "PCIe RC controller misconfigured as Endpoint\n");
  1006. return -EINVAL;
  1007. }
  1008. tmp = readl(base + PCIE_MISC_MISC_CTRL);
  1009. for (memc = 0; memc < pcie->num_memc; memc++) {
  1010. u32 scb_size_val = ilog2(pcie->memc_size[memc]) - 15;
  1011. if (memc == 0)
  1012. u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(0));
  1013. else if (memc == 1)
  1014. u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(1));
  1015. else if (memc == 2)
  1016. u32p_replace_bits(&tmp, scb_size_val, SCB_SIZE_MASK(2));
  1017. }
  1018. writel(tmp, base + PCIE_MISC_MISC_CTRL);
  1019. /*
  1020. * We ideally want the MSI target address to be located in the 32bit
  1021. * addressable memory area. Some devices might depend on it. This is
  1022. * possible either when the inbound window is located above the lower
  1023. * 4GB or when the inbound area is smaller than 4GB (taking into
  1024. * account the rounding-up we're forced to perform).
  1025. */
  1026. if (inbound_wins[2].pci_offset >= SZ_4G ||
  1027. (inbound_wins[2].size + inbound_wins[2].pci_offset) < SZ_4G)
  1028. pcie->msi_target_addr = BRCM_MSI_TARGET_ADDR_LT_4GB;
  1029. else
  1030. pcie->msi_target_addr = BRCM_MSI_TARGET_ADDR_GT_4GB;
  1031. /* Don't advertise L0s capability if 'aspm-no-l0s' */
  1032. tmp = readl(base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
  1033. if (of_property_read_bool(pcie->np, "aspm-no-l0s"))
  1034. tmp &= ~PCI_EXP_LNKCAP_ASPM_L0S;
  1035. writel(tmp, base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
  1036. /* 'tmp' still holds the contents of PRIV1_LINK_CAPABILITY */
  1037. num_lanes_cap = u32_get_bits(tmp, PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_MAX_LINK_WIDTH_MASK);
  1038. num_lanes = 0;
  1039. /*
  1040. * Use hardware negotiated Max Link Width value by default. If the
  1041. * "num-lanes" DT property is present, assume that the chip's default
  1042. * link width capability information is incorrect/undesired and use the
  1043. * specified value instead.
  1044. */
  1045. if (!of_property_read_u32(pcie->np, "num-lanes", &num_lanes) &&
  1046. num_lanes && num_lanes <= 4 && num_lanes_cap != num_lanes) {
  1047. u32p_replace_bits(&tmp, num_lanes,
  1048. PCIE_RC_CFG_PRIV1_LINK_CAPABILITY_MAX_LINK_WIDTH_MASK);
  1049. writel(tmp, base + PCIE_RC_CFG_PRIV1_LINK_CAPABILITY);
  1050. tmp = readl(base + PCIE_RC_PL_REG_PHY_CTL_1);
  1051. u32p_replace_bits(&tmp, 1,
  1052. PCIE_RC_PL_REG_PHY_CTL_1_REG_P2_POWERDOWN_ENA_NOSYNC_MASK);
  1053. writel(tmp, base + PCIE_RC_PL_REG_PHY_CTL_1);
  1054. }
  1055. /*
  1056. * For config space accesses on the RC, show the right class for
  1057. * a PCIe-PCIe bridge (the default setting is to be EP mode).
  1058. */
  1059. tmp = readl(base + PCIE_RC_CFG_PRIV1_ID_VAL3);
  1060. u32p_replace_bits(&tmp, 0x060400,
  1061. PCIE_RC_CFG_PRIV1_ID_VAL3_CLASS_CODE_MASK);
  1062. writel(tmp, base + PCIE_RC_CFG_PRIV1_ID_VAL3);
  1063. bridge = pci_host_bridge_from_priv(pcie);
  1064. resource_list_for_each_entry(entry, &bridge->windows) {
  1065. struct resource *res = entry->res;
  1066. if (resource_type(res) != IORESOURCE_MEM)
  1067. continue;
  1068. if (num_out_wins >= BRCM_NUM_PCIE_OUT_WINS) {
  1069. dev_err(pcie->dev, "too many outbound wins\n");
  1070. return -EINVAL;
  1071. }
  1072. if (is_bmips(pcie)) {
  1073. u64 start = res->start;
  1074. unsigned int j, nwins = resource_size(res) / SZ_128M;
  1075. /* bmips PCIe outbound windows have a 128MB max size */
  1076. if (nwins > BRCM_NUM_PCIE_OUT_WINS)
  1077. nwins = BRCM_NUM_PCIE_OUT_WINS;
  1078. for (j = 0; j < nwins; j++, start += SZ_128M)
  1079. brcm_pcie_set_outbound_win(pcie, j, start,
  1080. start - entry->offset,
  1081. SZ_128M);
  1082. break;
  1083. }
  1084. brcm_pcie_set_outbound_win(pcie, num_out_wins, res->start,
  1085. res->start - entry->offset,
  1086. resource_size(res));
  1087. num_out_wins++;
  1088. }
  1089. /* PCIe->SCB endian mode for inbound window */
  1090. tmp = readl(base + PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1);
  1091. u32p_replace_bits(&tmp, PCIE_RC_CFG_VENDOR_SPECIFIC_REG1_LITTLE_ENDIAN,
  1092. PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1_ENDIAN_MODE_BAR2_MASK);
  1093. writel(tmp, base + PCIE_RC_CFG_VENDOR_VENDOR_SPECIFIC_REG1);
  1094. if (pcie->cfg->post_setup) {
  1095. ret = pcie->cfg->post_setup(pcie);
  1096. if (ret < 0)
  1097. return ret;
  1098. }
  1099. return 0;
  1100. }
  1101. /*
  1102. * This extends the timeout period for an access to an internal bus. This
  1103. * access timeout may occur during L1SS sleep periods, even without the
  1104. * presence of a PCIe access.
  1105. */
  1106. static void brcm_extend_rbus_timeout(struct brcm_pcie *pcie)
  1107. {
  1108. /* TIMEOUT register is two registers before RGR1_SW_INIT_1 */
  1109. const unsigned int REG_OFFSET = PCIE_RGR1_SW_INIT_1(pcie) - 8;
  1110. u32 timeout_us = 4000000; /* 4 seconds, our setting for L1SS */
  1111. /* 7712 does not have this (RGR1) timer */
  1112. if (pcie->cfg->soc_base == BCM7712)
  1113. return;
  1114. /* Each unit in timeout register is 1/216,000,000 seconds */
  1115. writel(216 * timeout_us, pcie->base + REG_OFFSET);
  1116. }
  1117. static void brcm_config_clkreq(struct brcm_pcie *pcie)
  1118. {
  1119. static const char err_msg[] = "invalid 'brcm,clkreq-mode' DT string\n";
  1120. const char *mode = "default";
  1121. u32 clkreq_cntl;
  1122. int ret, tmp;
  1123. ret = of_property_read_string(pcie->np, "brcm,clkreq-mode", &mode);
  1124. if (ret && ret != -EINVAL) {
  1125. dev_err(pcie->dev, err_msg);
  1126. mode = "safe";
  1127. }
  1128. /* Start out assuming safe mode (both mode bits cleared) */
  1129. clkreq_cntl = readl(pcie->base + HARD_DEBUG(pcie));
  1130. clkreq_cntl &= ~PCIE_CLKREQ_MASK;
  1131. if (strcmp(mode, "no-l1ss") == 0) {
  1132. /*
  1133. * "no-l1ss" -- Provides Clock Power Management, L0s, and
  1134. * L1, but cannot provide L1 substate (L1SS) power
  1135. * savings. If the downstream device connected to the RC is
  1136. * L1SS capable AND the OS enables L1SS, all PCIe traffic
  1137. * may abruptly halt, potentially hanging the system.
  1138. */
  1139. clkreq_cntl |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_CLKREQ_DEBUG_ENABLE_MASK;
  1140. /*
  1141. * We want to un-advertise L1 substates because if the OS
  1142. * tries to configure the controller into using L1 substate
  1143. * power savings it may fail or hang when the RC HW is in
  1144. * "no-l1ss" mode.
  1145. */
  1146. tmp = readl(pcie->base + PCIE_RC_CFG_PRIV1_ROOT_CAP);
  1147. u32p_replace_bits(&tmp, 2, PCIE_RC_CFG_PRIV1_ROOT_CAP_L1SS_MODE_MASK);
  1148. writel(tmp, pcie->base + PCIE_RC_CFG_PRIV1_ROOT_CAP);
  1149. } else if (strcmp(mode, "default") == 0) {
  1150. /*
  1151. * "default" -- Provides L0s, L1, and L1SS, but not
  1152. * compliant to provide Clock Power Management;
  1153. * specifically, may not be able to meet the Tclron max
  1154. * timing of 400ns as specified in "Dynamic Clock Control",
  1155. * section 3.2.5.2.2 of the PCIe spec. This situation is
  1156. * atypical and should happen only with older devices.
  1157. */
  1158. clkreq_cntl |= PCIE_MISC_HARD_PCIE_HARD_DEBUG_L1SS_ENABLE_MASK;
  1159. brcm_extend_rbus_timeout(pcie);
  1160. } else {
  1161. /*
  1162. * "safe" -- No power savings; refclk is driven by RC
  1163. * unconditionally.
  1164. */
  1165. if (strcmp(mode, "safe") != 0)
  1166. dev_err(pcie->dev, err_msg);
  1167. mode = "safe";
  1168. }
  1169. writel(clkreq_cntl, pcie->base + HARD_DEBUG(pcie));
  1170. dev_info(pcie->dev, "clkreq-mode set to %s\n", mode);
  1171. }
  1172. static int brcm_pcie_start_link(struct brcm_pcie *pcie)
  1173. {
  1174. struct device *dev = pcie->dev;
  1175. void __iomem *base = pcie->base;
  1176. u16 nlw, cls, lnksta;
  1177. bool ssc_good = false;
  1178. int ret, i;
  1179. /* Limit the generation if specified */
  1180. if (pcie->gen)
  1181. brcm_pcie_set_gen(pcie, pcie->gen);
  1182. /* Unassert the fundamental reset */
  1183. ret = pcie->cfg->perst_set(pcie, 0);
  1184. if (ret)
  1185. return ret;
  1186. msleep(PCIE_RESET_CONFIG_WAIT_MS);
  1187. /*
  1188. * Give the RC/EP even more time to wake up, before trying to
  1189. * configure RC. Intermittently check status for link-up, up to a
  1190. * total of 100ms.
  1191. */
  1192. for (i = 0; i < 100 && !brcm_pcie_link_up(pcie); i += 5)
  1193. msleep(5);
  1194. if (!brcm_pcie_link_up(pcie)) {
  1195. dev_err(dev, "link down\n");
  1196. return -ENODEV;
  1197. }
  1198. brcm_config_clkreq(pcie);
  1199. if (pcie->ssc) {
  1200. ret = brcm_pcie_set_ssc(pcie);
  1201. if (ret == 0)
  1202. ssc_good = true;
  1203. else
  1204. dev_err(dev, "failed attempt to enter ssc mode\n");
  1205. }
  1206. lnksta = readw(base + BRCM_PCIE_CAP_REGS + PCI_EXP_LNKSTA);
  1207. cls = FIELD_GET(PCI_EXP_LNKSTA_CLS, lnksta);
  1208. nlw = FIELD_GET(PCI_EXP_LNKSTA_NLW, lnksta);
  1209. dev_info(dev, "link up, %s x%u %s\n",
  1210. pci_speed_string(pcie_link_speed[cls]), nlw,
  1211. ssc_good ? "(SSC)" : "(!SSC)");
  1212. return 0;
  1213. }
  1214. static const char * const supplies[] = {
  1215. "vpcie3v3",
  1216. "vpcie3v3aux",
  1217. "vpcie12v",
  1218. };
  1219. static void *alloc_subdev_regulators(struct device *dev)
  1220. {
  1221. const size_t size = sizeof(struct subdev_regulators) +
  1222. sizeof(struct regulator_bulk_data) * ARRAY_SIZE(supplies);
  1223. struct subdev_regulators *sr;
  1224. int i;
  1225. sr = devm_kzalloc(dev, size, GFP_KERNEL);
  1226. if (sr) {
  1227. sr->num_supplies = ARRAY_SIZE(supplies);
  1228. for (i = 0; i < ARRAY_SIZE(supplies); i++)
  1229. sr->supplies[i].supply = supplies[i];
  1230. }
  1231. return sr;
  1232. }
  1233. static int brcm_pcie_add_bus(struct pci_bus *bus)
  1234. {
  1235. struct brcm_pcie *pcie = bus->sysdata;
  1236. struct device *dev = &bus->dev;
  1237. struct subdev_regulators *sr;
  1238. int ret;
  1239. if (!bus->parent || !pci_is_root_bus(bus->parent))
  1240. return 0;
  1241. if (dev->of_node) {
  1242. sr = alloc_subdev_regulators(dev);
  1243. if (!sr) {
  1244. dev_info(dev, "Can't allocate regulators for downstream device\n");
  1245. goto no_regulators;
  1246. }
  1247. pcie->sr = sr;
  1248. ret = regulator_bulk_get(dev, sr->num_supplies, sr->supplies);
  1249. if (ret) {
  1250. dev_info(dev, "Did not get regulators, err=%d\n", ret);
  1251. pcie->sr = NULL;
  1252. goto no_regulators;
  1253. }
  1254. ret = regulator_bulk_enable(sr->num_supplies, sr->supplies);
  1255. if (ret) {
  1256. dev_err(dev, "Can't enable regulators for downstream device\n");
  1257. regulator_bulk_free(sr->num_supplies, sr->supplies);
  1258. pcie->sr = NULL;
  1259. }
  1260. }
  1261. no_regulators:
  1262. brcm_pcie_start_link(pcie);
  1263. return 0;
  1264. }
  1265. static void brcm_pcie_remove_bus(struct pci_bus *bus)
  1266. {
  1267. struct brcm_pcie *pcie = bus->sysdata;
  1268. struct subdev_regulators *sr = pcie->sr;
  1269. struct device *dev = &bus->dev;
  1270. if (!sr || !bus->parent || !pci_is_root_bus(bus->parent))
  1271. return;
  1272. if (regulator_bulk_disable(sr->num_supplies, sr->supplies))
  1273. dev_err(dev, "Failed to disable regulators for downstream device\n");
  1274. regulator_bulk_free(sr->num_supplies, sr->supplies);
  1275. pcie->sr = NULL;
  1276. }
  1277. /* L23 is a low-power PCIe link state */
  1278. static void brcm_pcie_enter_l23(struct brcm_pcie *pcie)
  1279. {
  1280. void __iomem *base = pcie->base;
  1281. int l23, i;
  1282. u32 tmp;
  1283. /* Assert request for L23 */
  1284. tmp = readl(base + PCIE_MISC_PCIE_CTRL);
  1285. u32p_replace_bits(&tmp, 1, PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK);
  1286. writel(tmp, base + PCIE_MISC_PCIE_CTRL);
  1287. /* Wait up to 36 msec for L23 */
  1288. tmp = readl(base + PCIE_MISC_PCIE_STATUS);
  1289. l23 = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK, tmp);
  1290. for (i = 0; i < 15 && !l23; i++) {
  1291. usleep_range(2000, 2400);
  1292. tmp = readl(base + PCIE_MISC_PCIE_STATUS);
  1293. l23 = FIELD_GET(PCIE_MISC_PCIE_STATUS_PCIE_LINK_IN_L23_MASK,
  1294. tmp);
  1295. }
  1296. if (!l23)
  1297. dev_err(pcie->dev, "failed to enter low-power link state\n");
  1298. }
  1299. static int brcm_phy_cntl(struct brcm_pcie *pcie, const int start)
  1300. {
  1301. static const u32 shifts[PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS] = {
  1302. PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_SHIFT,
  1303. PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_SHIFT,
  1304. PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_SHIFT,};
  1305. static const u32 masks[PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS] = {
  1306. PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_PWRDN_MASK,
  1307. PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_RESET_MASK,
  1308. PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_DIG_RESET_MASK,};
  1309. const int beg = start ? 0 : PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS - 1;
  1310. const int end = start ? PCIE_DVT_PMU_PCIE_PHY_CTRL_DAST_NFLDS : -1;
  1311. u32 tmp, combined_mask = 0;
  1312. u32 val;
  1313. void __iomem *base = pcie->base;
  1314. int i, ret;
  1315. for (i = beg; i != end; start ? i++ : i--) {
  1316. val = start ? BIT_MASK(shifts[i]) : 0;
  1317. tmp = readl(base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
  1318. tmp = (tmp & ~masks[i]) | (val & masks[i]);
  1319. writel(tmp, base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
  1320. usleep_range(50, 200);
  1321. combined_mask |= masks[i];
  1322. }
  1323. tmp = readl(base + PCIE_DVT_PMU_PCIE_PHY_CTRL);
  1324. val = start ? combined_mask : 0;
  1325. ret = (tmp & combined_mask) == val ? 0 : -EIO;
  1326. if (ret)
  1327. dev_err(pcie->dev, "failed to %s phy\n", (start ? "start" : "stop"));
  1328. return ret;
  1329. }
  1330. static inline int brcm_phy_start(struct brcm_pcie *pcie)
  1331. {
  1332. return pcie->cfg->has_phy ? brcm_phy_cntl(pcie, 1) : 0;
  1333. }
  1334. static inline int brcm_phy_stop(struct brcm_pcie *pcie)
  1335. {
  1336. return pcie->cfg->has_phy ? brcm_phy_cntl(pcie, 0) : 0;
  1337. }
  1338. static int brcm_pcie_turn_off(struct brcm_pcie *pcie)
  1339. {
  1340. void __iomem *base = pcie->base;
  1341. int tmp, ret;
  1342. if (brcm_pcie_link_up(pcie))
  1343. brcm_pcie_enter_l23(pcie);
  1344. /* Assert fundamental reset */
  1345. ret = pcie->cfg->perst_set(pcie, 1);
  1346. if (ret)
  1347. return ret;
  1348. /* Deassert request for L23 in case it was asserted */
  1349. tmp = readl(base + PCIE_MISC_PCIE_CTRL);
  1350. u32p_replace_bits(&tmp, 0, PCIE_MISC_PCIE_CTRL_PCIE_L23_REQUEST_MASK);
  1351. writel(tmp, base + PCIE_MISC_PCIE_CTRL);
  1352. /* Turn off SerDes */
  1353. tmp = readl(base + HARD_DEBUG(pcie));
  1354. u32p_replace_bits(&tmp, 1, PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK);
  1355. writel(tmp, base + HARD_DEBUG(pcie));
  1356. if (!(pcie->cfg->quirks & CFG_QUIRK_AVOID_BRIDGE_SHUTDOWN))
  1357. /* Shutdown PCIe bridge */
  1358. ret = brcm_pcie_bridge_sw_init_set(pcie, 1);
  1359. return ret;
  1360. }
  1361. static int pci_dev_may_wakeup(struct pci_dev *dev, void *data)
  1362. {
  1363. bool *ret = data;
  1364. if (device_may_wakeup(&dev->dev)) {
  1365. *ret = true;
  1366. dev_info(&dev->dev, "Possible wake-up device; regulators will not be disabled\n");
  1367. }
  1368. return (int) *ret;
  1369. }
  1370. static int brcm_pcie_suspend_noirq(struct device *dev)
  1371. {
  1372. struct brcm_pcie *pcie = dev_get_drvdata(dev);
  1373. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
  1374. int ret, rret;
  1375. ret = brcm_pcie_turn_off(pcie);
  1376. if (ret)
  1377. return ret;
  1378. /*
  1379. * If brcm_phy_stop() returns an error, just dev_err(). If we
  1380. * return the error it will cause the suspend to fail and this is a
  1381. * forgivable offense that will probably be erased on resume.
  1382. */
  1383. if (brcm_phy_stop(pcie))
  1384. dev_err(dev, "Could not stop phy for suspend\n");
  1385. ret = reset_control_rearm(pcie->rescal);
  1386. if (ret) {
  1387. dev_err(dev, "Could not rearm rescal reset\n");
  1388. return ret;
  1389. }
  1390. if (pcie->sr) {
  1391. /*
  1392. * Now turn off the regulators, but if at least one
  1393. * downstream device is enabled as a wake-up source, do not
  1394. * turn off regulators.
  1395. */
  1396. pcie->ep_wakeup_capable = false;
  1397. pci_walk_bus(bridge->bus, pci_dev_may_wakeup,
  1398. &pcie->ep_wakeup_capable);
  1399. if (!pcie->ep_wakeup_capable) {
  1400. ret = regulator_bulk_disable(pcie->sr->num_supplies,
  1401. pcie->sr->supplies);
  1402. if (ret) {
  1403. dev_err(dev, "Could not turn off regulators\n");
  1404. rret = reset_control_reset(pcie->rescal);
  1405. if (rret)
  1406. dev_err(dev, "failed to reset 'rascal' controller ret=%d\n",
  1407. rret);
  1408. return ret;
  1409. }
  1410. }
  1411. }
  1412. clk_disable_unprepare(pcie->clk);
  1413. return 0;
  1414. }
  1415. static int brcm_pcie_resume_noirq(struct device *dev)
  1416. {
  1417. struct brcm_pcie *pcie = dev_get_drvdata(dev);
  1418. void __iomem *base;
  1419. u32 tmp;
  1420. int ret, rret;
  1421. base = pcie->base;
  1422. ret = clk_prepare_enable(pcie->clk);
  1423. if (ret)
  1424. return ret;
  1425. ret = reset_control_reset(pcie->rescal);
  1426. if (ret)
  1427. goto err_disable_clk;
  1428. ret = brcm_phy_start(pcie);
  1429. if (ret)
  1430. goto err_reset;
  1431. /* Take bridge out of reset so we can access the SERDES reg */
  1432. ret = brcm_pcie_bridge_sw_init_set(pcie, 0);
  1433. if (ret)
  1434. goto err_reset;
  1435. /* SERDES_IDDQ = 0 */
  1436. tmp = readl(base + HARD_DEBUG(pcie));
  1437. u32p_replace_bits(&tmp, 0, PCIE_MISC_HARD_PCIE_HARD_DEBUG_SERDES_IDDQ_MASK);
  1438. writel(tmp, base + HARD_DEBUG(pcie));
  1439. /* wait for serdes to be stable */
  1440. udelay(100);
  1441. ret = brcm_pcie_setup(pcie);
  1442. if (ret)
  1443. goto err_reset;
  1444. if (pcie->sr) {
  1445. if (pcie->ep_wakeup_capable) {
  1446. /*
  1447. * We are resuming from a suspend. In the suspend we
  1448. * did not disable the power supplies, so there is
  1449. * no need to enable them (and falsely increase their
  1450. * usage count).
  1451. */
  1452. pcie->ep_wakeup_capable = false;
  1453. } else {
  1454. ret = regulator_bulk_enable(pcie->sr->num_supplies,
  1455. pcie->sr->supplies);
  1456. if (ret) {
  1457. dev_err(dev, "Could not turn on regulators\n");
  1458. goto err_reset;
  1459. }
  1460. }
  1461. }
  1462. ret = brcm_pcie_start_link(pcie);
  1463. if (ret)
  1464. goto err_regulator;
  1465. if (pcie->msi)
  1466. brcm_msi_set_regs(pcie->msi);
  1467. return 0;
  1468. err_regulator:
  1469. if (pcie->sr)
  1470. regulator_bulk_disable(pcie->sr->num_supplies, pcie->sr->supplies);
  1471. err_reset:
  1472. rret = reset_control_rearm(pcie->rescal);
  1473. if (rret)
  1474. dev_err(pcie->dev, "failed to rearm 'rescal' reset, err=%d\n", rret);
  1475. err_disable_clk:
  1476. clk_disable_unprepare(pcie->clk);
  1477. return ret;
  1478. }
  1479. /* Dump out PCIe errors on die or panic */
  1480. static int brcm_pcie_dump_err(struct brcm_pcie *pcie,
  1481. const char *type)
  1482. {
  1483. void __iomem *base = pcie->base;
  1484. int i, is_cfg_err, is_mem_err, lanes;
  1485. const char *width_str, *direction_str;
  1486. u32 info, cfg_addr, cfg_cause, mem_cause, lo, hi;
  1487. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
  1488. unsigned long flags;
  1489. char lanes_str[9];
  1490. spin_lock_irqsave(&pcie->bridge_lock, flags);
  1491. /* Don't access registers when the bridge is off */
  1492. if (pcie->bridge_in_reset || readl(base + PCIE_OUTB_ERR_VALID) == 0) {
  1493. spin_unlock_irqrestore(&pcie->bridge_lock, flags);
  1494. return NOTIFY_DONE;
  1495. }
  1496. /* Read all necessary registers so we can release the spinlock ASAP */
  1497. info = readl(base + PCIE_OUTB_ERR_ACC_INFO);
  1498. is_cfg_err = !!(info & PCIE_OUTB_ERR_ACC_INFO_CFG_ERR);
  1499. is_mem_err = !!(info & PCIE_OUTB_ERR_ACC_INFO_MEM_ERR);
  1500. if (is_cfg_err) {
  1501. cfg_addr = readl(base + PCIE_OUTB_ERR_ACC_ADDR);
  1502. cfg_cause = readl(base + PCIE_OUTB_ERR_CFG_CAUSE);
  1503. }
  1504. if (is_mem_err) {
  1505. mem_cause = readl(base + PCIE_OUTB_ERR_MEM_CAUSE);
  1506. lo = readl(base + PCIE_OUTB_ERR_MEM_ADDR_LO);
  1507. hi = readl(base + PCIE_OUTB_ERR_MEM_ADDR_HI);
  1508. }
  1509. /* We've got all of the info, clear the error */
  1510. writel(1, base + PCIE_OUTB_ERR_CLEAR);
  1511. spin_unlock_irqrestore(&pcie->bridge_lock, flags);
  1512. dev_err(pcie->dev, "reporting PCIe info which may be related to %s error\n",
  1513. type);
  1514. width_str = (info & PCIE_OUTB_ERR_ACC_INFO_TYPE_64) ? "64bit" : "32bit";
  1515. direction_str = str_read_write(!(info & PCIE_OUTB_ERR_ACC_INFO_DIR_WRITE));
  1516. lanes = FIELD_GET(PCIE_OUTB_ERR_ACC_INFO_BYTE_LANES, info);
  1517. for (i = 0, lanes_str[8] = 0; i < 8; i++)
  1518. lanes_str[i] = (lanes & (1 << i)) ? '1' : '0';
  1519. if (is_cfg_err) {
  1520. int bus = FIELD_GET(PCIE_OUTB_ERR_ACC_ADDR_BUS, cfg_addr);
  1521. int dev = FIELD_GET(PCIE_OUTB_ERR_ACC_ADDR_DEV, cfg_addr);
  1522. int func = FIELD_GET(PCIE_OUTB_ERR_ACC_ADDR_FUNC, cfg_addr);
  1523. int reg = FIELD_GET(PCIE_OUTB_ERR_ACC_ADDR_REG, cfg_addr);
  1524. dev_err(pcie->dev, "Error: CFG Acc, %s, %s (%04x:%02x:%02x.%d) reg=0x%x, lanes=%s\n",
  1525. width_str, direction_str, bridge->domain_nr, bus, dev,
  1526. func, reg, lanes_str);
  1527. dev_err(pcie->dev, " Type: TO=%d Abt=%d UnsupReq=%d AccTO=%d AccDsbld=%d Acc64bit=%d\n",
  1528. !!(cfg_cause & PCIE_OUTB_ERR_CFG_CAUSE_TIMEOUT),
  1529. !!(cfg_cause & PCIE_OUTB_ERR_CFG_CAUSE_ABORT),
  1530. !!(cfg_cause & PCIE_OUTB_ERR_CFG_CAUSE_UNSUPP_REQ),
  1531. !!(cfg_cause & PCIE_OUTB_ERR_CFG_CAUSE_ACC_TIMEOUT),
  1532. !!(cfg_cause & PCIE_OUTB_ERR_CFG_CAUSE_ACC_DISABLED),
  1533. !!(cfg_cause & PCIE_OUTB_ERR_CFG_CAUSE_ACC_64BIT));
  1534. }
  1535. if (is_mem_err) {
  1536. u64 addr = ((u64)hi << 32) | (u64)lo;
  1537. dev_err(pcie->dev, "Error: Mem Acc, %s, %s, @0x%llx, lanes=%s\n",
  1538. width_str, direction_str, addr, lanes_str);
  1539. dev_err(pcie->dev, " Type: TO=%d Abt=%d UnsupReq=%d AccDsble=%d BadAddr=%d\n",
  1540. !!(mem_cause & PCIE_OUTB_ERR_MEM_CAUSE_TIMEOUT),
  1541. !!(mem_cause & PCIE_OUTB_ERR_MEM_CAUSE_ABORT),
  1542. !!(mem_cause & PCIE_OUTB_ERR_MEM_CAUSE_UNSUPP_REQ),
  1543. !!(mem_cause & PCIE_OUTB_ERR_MEM_CAUSE_ACC_DISABLED),
  1544. !!(mem_cause & PCIE_OUTB_ERR_MEM_CAUSE_BAD_ADDR));
  1545. }
  1546. return NOTIFY_DONE;
  1547. }
  1548. static int brcm_pcie_die_notify_cb(struct notifier_block *self,
  1549. unsigned long v, void *p)
  1550. {
  1551. struct brcm_pcie *pcie =
  1552. container_of(self, struct brcm_pcie, die_notifier);
  1553. return brcm_pcie_dump_err(pcie, "Die");
  1554. }
  1555. static int brcm_pcie_panic_notify_cb(struct notifier_block *self,
  1556. unsigned long v, void *p)
  1557. {
  1558. struct brcm_pcie *pcie =
  1559. container_of(self, struct brcm_pcie, panic_notifier);
  1560. return brcm_pcie_dump_err(pcie, "Panic");
  1561. }
  1562. static void brcm_register_die_notifiers(struct brcm_pcie *pcie)
  1563. {
  1564. pcie->panic_notifier.notifier_call = brcm_pcie_panic_notify_cb;
  1565. atomic_notifier_chain_register(&panic_notifier_list,
  1566. &pcie->panic_notifier);
  1567. pcie->die_notifier.notifier_call = brcm_pcie_die_notify_cb;
  1568. register_die_notifier(&pcie->die_notifier);
  1569. }
  1570. static void brcm_unregister_die_notifiers(struct brcm_pcie *pcie)
  1571. {
  1572. unregister_die_notifier(&pcie->die_notifier);
  1573. atomic_notifier_chain_unregister(&panic_notifier_list,
  1574. &pcie->panic_notifier);
  1575. }
  1576. static void __brcm_pcie_remove(struct brcm_pcie *pcie)
  1577. {
  1578. brcm_msi_remove(pcie);
  1579. brcm_pcie_turn_off(pcie);
  1580. if (brcm_phy_stop(pcie))
  1581. dev_err(pcie->dev, "Could not stop phy\n");
  1582. if (reset_control_rearm(pcie->rescal))
  1583. dev_err(pcie->dev, "Could not rearm rescal reset\n");
  1584. clk_disable_unprepare(pcie->clk);
  1585. }
  1586. static void brcm_pcie_remove(struct platform_device *pdev)
  1587. {
  1588. struct brcm_pcie *pcie = platform_get_drvdata(pdev);
  1589. struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
  1590. pci_stop_root_bus(bridge->bus);
  1591. pci_remove_root_bus(bridge->bus);
  1592. if (pcie->cfg->has_err_report)
  1593. brcm_unregister_die_notifiers(pcie);
  1594. __brcm_pcie_remove(pcie);
  1595. }
  1596. static const int pcie_offsets[] = {
  1597. [RGR1_SW_INIT_1] = 0x9210,
  1598. [EXT_CFG_INDEX] = 0x9000,
  1599. [EXT_CFG_DATA] = 0x8000,
  1600. [PCIE_HARD_DEBUG] = 0x4204,
  1601. [PCIE_INTR2_CPU_BASE] = 0x4300,
  1602. };
  1603. static const int pcie_offsets_bcm7278[] = {
  1604. [RGR1_SW_INIT_1] = 0xc010,
  1605. [EXT_CFG_INDEX] = 0x9000,
  1606. [EXT_CFG_DATA] = 0x8000,
  1607. [PCIE_HARD_DEBUG] = 0x4204,
  1608. [PCIE_INTR2_CPU_BASE] = 0x4300,
  1609. };
  1610. static const int pcie_offsets_bcm7425[] = {
  1611. [RGR1_SW_INIT_1] = 0x8010,
  1612. [EXT_CFG_INDEX] = 0x8300,
  1613. [EXT_CFG_DATA] = 0x8304,
  1614. [PCIE_HARD_DEBUG] = 0x4204,
  1615. [PCIE_INTR2_CPU_BASE] = 0x4300,
  1616. };
  1617. static const int pcie_offsets_bcm7712[] = {
  1618. [RGR1_SW_INIT_1] = 0x9210,
  1619. [EXT_CFG_INDEX] = 0x9000,
  1620. [EXT_CFG_DATA] = 0x8000,
  1621. [PCIE_HARD_DEBUG] = 0x4304,
  1622. [PCIE_INTR2_CPU_BASE] = 0x4400,
  1623. };
  1624. static const struct pcie_cfg_data generic_cfg = {
  1625. .offsets = pcie_offsets,
  1626. .soc_base = GENERIC,
  1627. .perst_set = brcm_pcie_perst_set_generic,
  1628. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
  1629. .num_inbound_wins = 3,
  1630. };
  1631. static const struct pcie_cfg_data bcm2711_cfg = {
  1632. .offsets = pcie_offsets,
  1633. .soc_base = BCM2711,
  1634. .perst_set = brcm_pcie_perst_set_generic,
  1635. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
  1636. .num_inbound_wins = 3,
  1637. };
  1638. static const struct pcie_cfg_data bcm2712_cfg = {
  1639. .offsets = pcie_offsets_bcm7712,
  1640. .soc_base = BCM7712,
  1641. .perst_set = brcm_pcie_perst_set_7278,
  1642. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
  1643. .post_setup = brcm_pcie_post_setup_bcm2712,
  1644. .quirks = CFG_QUIRK_AVOID_BRIDGE_SHUTDOWN,
  1645. .num_inbound_wins = 10,
  1646. };
  1647. static const struct pcie_cfg_data bcm4908_cfg = {
  1648. .offsets = pcie_offsets,
  1649. .soc_base = BCM4908,
  1650. .perst_set = brcm_pcie_perst_set_4908,
  1651. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
  1652. .num_inbound_wins = 3,
  1653. };
  1654. static const struct pcie_cfg_data bcm7278_cfg = {
  1655. .offsets = pcie_offsets_bcm7278,
  1656. .soc_base = BCM7278,
  1657. .perst_set = brcm_pcie_perst_set_7278,
  1658. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_7278,
  1659. .num_inbound_wins = 3,
  1660. };
  1661. static const struct pcie_cfg_data bcm7425_cfg = {
  1662. .offsets = pcie_offsets_bcm7425,
  1663. .soc_base = BCM7425,
  1664. .perst_set = brcm_pcie_perst_set_generic,
  1665. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
  1666. .num_inbound_wins = 3,
  1667. };
  1668. static const struct pcie_cfg_data bcm7435_cfg = {
  1669. .offsets = pcie_offsets,
  1670. .soc_base = BCM7435,
  1671. .perst_set = brcm_pcie_perst_set_generic,
  1672. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
  1673. .num_inbound_wins = 3,
  1674. };
  1675. static const struct pcie_cfg_data bcm7216_cfg = {
  1676. .offsets = pcie_offsets_bcm7278,
  1677. .soc_base = BCM7278,
  1678. .perst_set = brcm_pcie_perst_set_7278,
  1679. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_7278,
  1680. .has_phy = true,
  1681. .num_inbound_wins = 3,
  1682. .has_err_report = true,
  1683. };
  1684. static const struct pcie_cfg_data bcm7712_cfg = {
  1685. .offsets = pcie_offsets_bcm7712,
  1686. .perst_set = brcm_pcie_perst_set_7278,
  1687. .bridge_sw_init_set = brcm_pcie_bridge_sw_init_set_generic,
  1688. .soc_base = BCM7712,
  1689. .num_inbound_wins = 10,
  1690. };
  1691. static const struct of_device_id brcm_pcie_match[] = {
  1692. { .compatible = "brcm,bcm2711-pcie", .data = &bcm2711_cfg },
  1693. { .compatible = "brcm,bcm2712-pcie", .data = &bcm2712_cfg },
  1694. { .compatible = "brcm,bcm4908-pcie", .data = &bcm4908_cfg },
  1695. { .compatible = "brcm,bcm7211-pcie", .data = &generic_cfg },
  1696. { .compatible = "brcm,bcm7216-pcie", .data = &bcm7216_cfg },
  1697. { .compatible = "brcm,bcm7278-pcie", .data = &bcm7278_cfg },
  1698. { .compatible = "brcm,bcm7425-pcie", .data = &bcm7425_cfg },
  1699. { .compatible = "brcm,bcm7435-pcie", .data = &bcm7435_cfg },
  1700. { .compatible = "brcm,bcm7445-pcie", .data = &generic_cfg },
  1701. { .compatible = "brcm,bcm7712-pcie", .data = &bcm7712_cfg },
  1702. {},
  1703. };
  1704. static struct pci_ops brcm_pcie_ops = {
  1705. .map_bus = brcm_pcie_map_bus,
  1706. .read = pci_generic_config_read,
  1707. .write = pci_generic_config_write,
  1708. .add_bus = brcm_pcie_add_bus,
  1709. .remove_bus = brcm_pcie_remove_bus,
  1710. };
  1711. static struct pci_ops brcm7425_pcie_ops = {
  1712. .map_bus = brcm7425_pcie_map_bus,
  1713. .read = pci_generic_config_read32,
  1714. .write = pci_generic_config_write32,
  1715. .add_bus = brcm_pcie_add_bus,
  1716. .remove_bus = brcm_pcie_remove_bus,
  1717. };
  1718. static int brcm_pcie_probe(struct platform_device *pdev)
  1719. {
  1720. struct device_node *np = pdev->dev.of_node;
  1721. struct pci_host_bridge *bridge;
  1722. const struct pcie_cfg_data *data;
  1723. struct brcm_pcie *pcie;
  1724. int ret;
  1725. bridge = devm_pci_alloc_host_bridge(&pdev->dev, sizeof(*pcie));
  1726. if (!bridge)
  1727. return -ENOMEM;
  1728. data = of_device_get_match_data(&pdev->dev);
  1729. if (!data) {
  1730. pr_err("failed to look up compatible string\n");
  1731. return -EINVAL;
  1732. }
  1733. pcie = pci_host_bridge_priv(bridge);
  1734. pcie->dev = &pdev->dev;
  1735. pcie->np = np;
  1736. pcie->cfg = data;
  1737. pcie->base = devm_platform_ioremap_resource(pdev, 0);
  1738. if (IS_ERR(pcie->base))
  1739. return PTR_ERR(pcie->base);
  1740. pcie->clk = devm_clk_get_optional(&pdev->dev, "sw_pcie");
  1741. if (IS_ERR(pcie->clk))
  1742. return PTR_ERR(pcie->clk);
  1743. ret = of_pci_get_max_link_speed(np);
  1744. pcie->gen = (ret < 0) ? 0 : ret;
  1745. pcie->ssc = of_property_read_bool(np, "brcm,enable-ssc");
  1746. pcie->rescal = devm_reset_control_get_optional_shared(&pdev->dev, "rescal");
  1747. if (IS_ERR(pcie->rescal))
  1748. return PTR_ERR(pcie->rescal);
  1749. pcie->perst_reset = devm_reset_control_get_optional_exclusive(&pdev->dev, "perst");
  1750. if (IS_ERR(pcie->perst_reset))
  1751. return PTR_ERR(pcie->perst_reset);
  1752. pcie->bridge_reset = devm_reset_control_get_optional_exclusive(&pdev->dev, "bridge");
  1753. if (IS_ERR(pcie->bridge_reset))
  1754. return PTR_ERR(pcie->bridge_reset);
  1755. pcie->swinit_reset = devm_reset_control_get_optional_exclusive(&pdev->dev, "swinit");
  1756. if (IS_ERR(pcie->swinit_reset))
  1757. return PTR_ERR(pcie->swinit_reset);
  1758. ret = clk_prepare_enable(pcie->clk);
  1759. if (ret)
  1760. return dev_err_probe(&pdev->dev, ret, "could not enable clock\n");
  1761. ret = brcm_pcie_bridge_sw_init_set(pcie, 0);
  1762. if (ret)
  1763. return dev_err_probe(&pdev->dev, ret,
  1764. "could not de-assert bridge reset\n");
  1765. if (pcie->swinit_reset) {
  1766. ret = reset_control_assert(pcie->swinit_reset);
  1767. if (ret) {
  1768. clk_disable_unprepare(pcie->clk);
  1769. return dev_err_probe(&pdev->dev, ret,
  1770. "could not assert reset 'swinit'\n");
  1771. }
  1772. /* HW team recommends 1us for proper sync and propagation of reset */
  1773. udelay(1);
  1774. ret = reset_control_deassert(pcie->swinit_reset);
  1775. if (ret) {
  1776. clk_disable_unprepare(pcie->clk);
  1777. return dev_err_probe(&pdev->dev, ret,
  1778. "could not de-assert reset 'swinit'\n");
  1779. }
  1780. }
  1781. ret = reset_control_reset(pcie->rescal);
  1782. if (ret) {
  1783. clk_disable_unprepare(pcie->clk);
  1784. return dev_err_probe(&pdev->dev, ret, "failed to deassert 'rescal'\n");
  1785. }
  1786. ret = brcm_phy_start(pcie);
  1787. if (ret) {
  1788. reset_control_rearm(pcie->rescal);
  1789. clk_disable_unprepare(pcie->clk);
  1790. return ret;
  1791. }
  1792. ret = brcm_pcie_setup(pcie);
  1793. if (ret)
  1794. goto fail;
  1795. pcie->hw_rev = readl(pcie->base + PCIE_MISC_REVISION);
  1796. if (pcie->cfg->soc_base == BCM4908 &&
  1797. pcie->hw_rev >= BRCM_PCIE_HW_REV_3_20) {
  1798. dev_err(pcie->dev, "hardware revision with unsupported PERST# setup\n");
  1799. ret = -ENODEV;
  1800. goto fail;
  1801. }
  1802. if (pci_msi_enabled()) {
  1803. struct device_node *msi_np = of_parse_phandle(pcie->np, "msi-parent", 0);
  1804. if (msi_np == pcie->np)
  1805. ret = brcm_pcie_enable_msi(pcie);
  1806. of_node_put(msi_np);
  1807. if (ret) {
  1808. dev_err(pcie->dev, "probe of internal MSI failed");
  1809. goto fail;
  1810. }
  1811. }
  1812. bridge->ops = pcie->cfg->soc_base == BCM7425 ?
  1813. &brcm7425_pcie_ops : &brcm_pcie_ops;
  1814. bridge->sysdata = pcie;
  1815. platform_set_drvdata(pdev, pcie);
  1816. ret = pci_host_probe(bridge);
  1817. if (!ret && !brcm_pcie_link_up(pcie))
  1818. ret = -ENODEV;
  1819. if (ret) {
  1820. brcm_pcie_remove(pdev);
  1821. return ret;
  1822. }
  1823. if (pcie->cfg->has_err_report) {
  1824. spin_lock_init(&pcie->bridge_lock);
  1825. brcm_register_die_notifiers(pcie);
  1826. }
  1827. return 0;
  1828. fail:
  1829. __brcm_pcie_remove(pcie);
  1830. return ret;
  1831. }
  1832. MODULE_DEVICE_TABLE(of, brcm_pcie_match);
  1833. static const struct dev_pm_ops brcm_pcie_pm_ops = {
  1834. .suspend_noirq = brcm_pcie_suspend_noirq,
  1835. .resume_noirq = brcm_pcie_resume_noirq,
  1836. };
  1837. static struct platform_driver brcm_pcie_driver = {
  1838. .probe = brcm_pcie_probe,
  1839. .remove = brcm_pcie_remove,
  1840. .driver = {
  1841. .name = "brcm-pcie",
  1842. .of_match_table = brcm_pcie_match,
  1843. .pm = &brcm_pcie_pm_ops,
  1844. },
  1845. };
  1846. module_platform_driver(brcm_pcie_driver);
  1847. MODULE_LICENSE("GPL");
  1848. MODULE_DESCRIPTION("Broadcom STB PCIe RC driver");
  1849. MODULE_AUTHOR("Broadcom");
  1850. MODULE_SOFTDEP("pre: irq_bcm2712_mip");