pcie-brcmstb.c 55 KB

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