pcie-iproc.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Hauke Mehrtens <hauke@hauke-m.de>
  4. * Copyright (C) 2015 Broadcom Corporation
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/pci.h>
  8. #include <linux/msi.h>
  9. #include <linux/clk.h>
  10. #include <linux/module.h>
  11. #include <linux/mbus.h>
  12. #include <linux/slab.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irqchip/arm-gic-v3.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_pci.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/phy/phy.h>
  22. #include "pcie-iproc.h"
  23. #define EP_PERST_SOURCE_SELECT_SHIFT 2
  24. #define EP_PERST_SOURCE_SELECT BIT(EP_PERST_SOURCE_SELECT_SHIFT)
  25. #define EP_MODE_SURVIVE_PERST_SHIFT 1
  26. #define EP_MODE_SURVIVE_PERST BIT(EP_MODE_SURVIVE_PERST_SHIFT)
  27. #define RC_PCIE_RST_OUTPUT_SHIFT 0
  28. #define RC_PCIE_RST_OUTPUT BIT(RC_PCIE_RST_OUTPUT_SHIFT)
  29. #define PAXC_RESET_MASK 0x7f
  30. #define GIC_V3_CFG_SHIFT 0
  31. #define GIC_V3_CFG BIT(GIC_V3_CFG_SHIFT)
  32. #define MSI_ENABLE_CFG_SHIFT 0
  33. #define MSI_ENABLE_CFG BIT(MSI_ENABLE_CFG_SHIFT)
  34. #define CFG_IND_ADDR_MASK 0x00001ffc
  35. #define CFG_ADDR_BUS_NUM_SHIFT 20
  36. #define CFG_ADDR_BUS_NUM_MASK 0x0ff00000
  37. #define CFG_ADDR_DEV_NUM_SHIFT 15
  38. #define CFG_ADDR_DEV_NUM_MASK 0x000f8000
  39. #define CFG_ADDR_FUNC_NUM_SHIFT 12
  40. #define CFG_ADDR_FUNC_NUM_MASK 0x00007000
  41. #define CFG_ADDR_REG_NUM_SHIFT 2
  42. #define CFG_ADDR_REG_NUM_MASK 0x00000ffc
  43. #define CFG_ADDR_CFG_TYPE_SHIFT 0
  44. #define CFG_ADDR_CFG_TYPE_MASK 0x00000003
  45. #define SYS_RC_INTX_MASK 0xf
  46. #define PCIE_PHYLINKUP_SHIFT 3
  47. #define PCIE_PHYLINKUP BIT(PCIE_PHYLINKUP_SHIFT)
  48. #define PCIE_DL_ACTIVE_SHIFT 2
  49. #define PCIE_DL_ACTIVE BIT(PCIE_DL_ACTIVE_SHIFT)
  50. #define APB_ERR_EN_SHIFT 0
  51. #define APB_ERR_EN BIT(APB_ERR_EN_SHIFT)
  52. #define CFG_RETRY_STATUS 0xffff0001
  53. #define CFG_RETRY_STATUS_TIMEOUT_US 500000 /* 500 milliseconds */
  54. /* derive the enum index of the outbound/inbound mapping registers */
  55. #define MAP_REG(base_reg, index) ((base_reg) + (index) * 2)
  56. /*
  57. * Maximum number of outbound mapping window sizes that can be supported by any
  58. * OARR/OMAP mapping pair
  59. */
  60. #define MAX_NUM_OB_WINDOW_SIZES 4
  61. #define OARR_VALID_SHIFT 0
  62. #define OARR_VALID BIT(OARR_VALID_SHIFT)
  63. #define OARR_SIZE_CFG_SHIFT 1
  64. /*
  65. * Maximum number of inbound mapping region sizes that can be supported by an
  66. * IARR
  67. */
  68. #define MAX_NUM_IB_REGION_SIZES 9
  69. #define IMAP_VALID_SHIFT 0
  70. #define IMAP_VALID BIT(IMAP_VALID_SHIFT)
  71. #define IPROC_PCI_PM_CAP 0x48
  72. #define IPROC_PCI_PM_CAP_MASK 0xffff
  73. #define IPROC_PCI_EXP_CAP 0xac
  74. #define IPROC_PCIE_REG_INVALID 0xffff
  75. /**
  76. * iProc PCIe outbound mapping controller specific parameters
  77. *
  78. * @window_sizes: list of supported outbound mapping window sizes in MB
  79. * @nr_sizes: number of supported outbound mapping window sizes
  80. */
  81. struct iproc_pcie_ob_map {
  82. resource_size_t window_sizes[MAX_NUM_OB_WINDOW_SIZES];
  83. unsigned int nr_sizes;
  84. };
  85. static const struct iproc_pcie_ob_map paxb_ob_map[] = {
  86. {
  87. /* OARR0/OMAP0 */
  88. .window_sizes = { 128, 256 },
  89. .nr_sizes = 2,
  90. },
  91. {
  92. /* OARR1/OMAP1 */
  93. .window_sizes = { 128, 256 },
  94. .nr_sizes = 2,
  95. },
  96. };
  97. static const struct iproc_pcie_ob_map paxb_v2_ob_map[] = {
  98. {
  99. /* OARR0/OMAP0 */
  100. .window_sizes = { 128, 256 },
  101. .nr_sizes = 2,
  102. },
  103. {
  104. /* OARR1/OMAP1 */
  105. .window_sizes = { 128, 256 },
  106. .nr_sizes = 2,
  107. },
  108. {
  109. /* OARR2/OMAP2 */
  110. .window_sizes = { 128, 256, 512, 1024 },
  111. .nr_sizes = 4,
  112. },
  113. {
  114. /* OARR3/OMAP3 */
  115. .window_sizes = { 128, 256, 512, 1024 },
  116. .nr_sizes = 4,
  117. },
  118. };
  119. /**
  120. * iProc PCIe inbound mapping type
  121. */
  122. enum iproc_pcie_ib_map_type {
  123. /* for DDR memory */
  124. IPROC_PCIE_IB_MAP_MEM = 0,
  125. /* for device I/O memory */
  126. IPROC_PCIE_IB_MAP_IO,
  127. /* invalid or unused */
  128. IPROC_PCIE_IB_MAP_INVALID
  129. };
  130. /**
  131. * iProc PCIe inbound mapping controller specific parameters
  132. *
  133. * @type: inbound mapping region type
  134. * @size_unit: inbound mapping region size unit, could be SZ_1K, SZ_1M, or
  135. * SZ_1G
  136. * @region_sizes: list of supported inbound mapping region sizes in KB, MB, or
  137. * GB, depedning on the size unit
  138. * @nr_sizes: number of supported inbound mapping region sizes
  139. * @nr_windows: number of supported inbound mapping windows for the region
  140. * @imap_addr_offset: register offset between the upper and lower 32-bit
  141. * IMAP address registers
  142. * @imap_window_offset: register offset between each IMAP window
  143. */
  144. struct iproc_pcie_ib_map {
  145. enum iproc_pcie_ib_map_type type;
  146. unsigned int size_unit;
  147. resource_size_t region_sizes[MAX_NUM_IB_REGION_SIZES];
  148. unsigned int nr_sizes;
  149. unsigned int nr_windows;
  150. u16 imap_addr_offset;
  151. u16 imap_window_offset;
  152. };
  153. static const struct iproc_pcie_ib_map paxb_v2_ib_map[] = {
  154. {
  155. /* IARR0/IMAP0 */
  156. .type = IPROC_PCIE_IB_MAP_IO,
  157. .size_unit = SZ_1K,
  158. .region_sizes = { 32 },
  159. .nr_sizes = 1,
  160. .nr_windows = 8,
  161. .imap_addr_offset = 0x40,
  162. .imap_window_offset = 0x4,
  163. },
  164. {
  165. /* IARR1/IMAP1 (currently unused) */
  166. .type = IPROC_PCIE_IB_MAP_INVALID,
  167. },
  168. {
  169. /* IARR2/IMAP2 */
  170. .type = IPROC_PCIE_IB_MAP_MEM,
  171. .size_unit = SZ_1M,
  172. .region_sizes = { 64, 128, 256, 512, 1024, 2048, 4096, 8192,
  173. 16384 },
  174. .nr_sizes = 9,
  175. .nr_windows = 1,
  176. .imap_addr_offset = 0x4,
  177. .imap_window_offset = 0x8,
  178. },
  179. {
  180. /* IARR3/IMAP3 */
  181. .type = IPROC_PCIE_IB_MAP_MEM,
  182. .size_unit = SZ_1G,
  183. .region_sizes = { 1, 2, 4, 8, 16, 32 },
  184. .nr_sizes = 6,
  185. .nr_windows = 8,
  186. .imap_addr_offset = 0x4,
  187. .imap_window_offset = 0x8,
  188. },
  189. {
  190. /* IARR4/IMAP4 */
  191. .type = IPROC_PCIE_IB_MAP_MEM,
  192. .size_unit = SZ_1G,
  193. .region_sizes = { 32, 64, 128, 256, 512 },
  194. .nr_sizes = 5,
  195. .nr_windows = 8,
  196. .imap_addr_offset = 0x4,
  197. .imap_window_offset = 0x8,
  198. },
  199. };
  200. /*
  201. * iProc PCIe host registers
  202. */
  203. enum iproc_pcie_reg {
  204. /* clock/reset signal control */
  205. IPROC_PCIE_CLK_CTRL = 0,
  206. /*
  207. * To allow MSI to be steered to an external MSI controller (e.g., ARM
  208. * GICv3 ITS)
  209. */
  210. IPROC_PCIE_MSI_GIC_MODE,
  211. /*
  212. * IPROC_PCIE_MSI_BASE_ADDR and IPROC_PCIE_MSI_WINDOW_SIZE define the
  213. * window where the MSI posted writes are written, for the writes to be
  214. * interpreted as MSI writes.
  215. */
  216. IPROC_PCIE_MSI_BASE_ADDR,
  217. IPROC_PCIE_MSI_WINDOW_SIZE,
  218. /*
  219. * To hold the address of the register where the MSI writes are
  220. * programed. When ARM GICv3 ITS is used, this should be programmed
  221. * with the address of the GITS_TRANSLATER register.
  222. */
  223. IPROC_PCIE_MSI_ADDR_LO,
  224. IPROC_PCIE_MSI_ADDR_HI,
  225. /* enable MSI */
  226. IPROC_PCIE_MSI_EN_CFG,
  227. /* allow access to root complex configuration space */
  228. IPROC_PCIE_CFG_IND_ADDR,
  229. IPROC_PCIE_CFG_IND_DATA,
  230. /* allow access to device configuration space */
  231. IPROC_PCIE_CFG_ADDR,
  232. IPROC_PCIE_CFG_DATA,
  233. /* enable INTx */
  234. IPROC_PCIE_INTX_EN,
  235. /* outbound address mapping */
  236. IPROC_PCIE_OARR0,
  237. IPROC_PCIE_OMAP0,
  238. IPROC_PCIE_OARR1,
  239. IPROC_PCIE_OMAP1,
  240. IPROC_PCIE_OARR2,
  241. IPROC_PCIE_OMAP2,
  242. IPROC_PCIE_OARR3,
  243. IPROC_PCIE_OMAP3,
  244. /* inbound address mapping */
  245. IPROC_PCIE_IARR0,
  246. IPROC_PCIE_IMAP0,
  247. IPROC_PCIE_IARR1,
  248. IPROC_PCIE_IMAP1,
  249. IPROC_PCIE_IARR2,
  250. IPROC_PCIE_IMAP2,
  251. IPROC_PCIE_IARR3,
  252. IPROC_PCIE_IMAP3,
  253. IPROC_PCIE_IARR4,
  254. IPROC_PCIE_IMAP4,
  255. /* link status */
  256. IPROC_PCIE_LINK_STATUS,
  257. /* enable APB error for unsupported requests */
  258. IPROC_PCIE_APB_ERR_EN,
  259. /* total number of core registers */
  260. IPROC_PCIE_MAX_NUM_REG,
  261. };
  262. /* iProc PCIe PAXB BCMA registers */
  263. static const u16 iproc_pcie_reg_paxb_bcma[] = {
  264. [IPROC_PCIE_CLK_CTRL] = 0x000,
  265. [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
  266. [IPROC_PCIE_CFG_IND_DATA] = 0x124,
  267. [IPROC_PCIE_CFG_ADDR] = 0x1f8,
  268. [IPROC_PCIE_CFG_DATA] = 0x1fc,
  269. [IPROC_PCIE_INTX_EN] = 0x330,
  270. [IPROC_PCIE_LINK_STATUS] = 0xf0c,
  271. };
  272. /* iProc PCIe PAXB registers */
  273. static const u16 iproc_pcie_reg_paxb[] = {
  274. [IPROC_PCIE_CLK_CTRL] = 0x000,
  275. [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
  276. [IPROC_PCIE_CFG_IND_DATA] = 0x124,
  277. [IPROC_PCIE_CFG_ADDR] = 0x1f8,
  278. [IPROC_PCIE_CFG_DATA] = 0x1fc,
  279. [IPROC_PCIE_INTX_EN] = 0x330,
  280. [IPROC_PCIE_OARR0] = 0xd20,
  281. [IPROC_PCIE_OMAP0] = 0xd40,
  282. [IPROC_PCIE_OARR1] = 0xd28,
  283. [IPROC_PCIE_OMAP1] = 0xd48,
  284. [IPROC_PCIE_LINK_STATUS] = 0xf0c,
  285. [IPROC_PCIE_APB_ERR_EN] = 0xf40,
  286. };
  287. /* iProc PCIe PAXB v2 registers */
  288. static const u16 iproc_pcie_reg_paxb_v2[] = {
  289. [IPROC_PCIE_CLK_CTRL] = 0x000,
  290. [IPROC_PCIE_CFG_IND_ADDR] = 0x120,
  291. [IPROC_PCIE_CFG_IND_DATA] = 0x124,
  292. [IPROC_PCIE_CFG_ADDR] = 0x1f8,
  293. [IPROC_PCIE_CFG_DATA] = 0x1fc,
  294. [IPROC_PCIE_INTX_EN] = 0x330,
  295. [IPROC_PCIE_OARR0] = 0xd20,
  296. [IPROC_PCIE_OMAP0] = 0xd40,
  297. [IPROC_PCIE_OARR1] = 0xd28,
  298. [IPROC_PCIE_OMAP1] = 0xd48,
  299. [IPROC_PCIE_OARR2] = 0xd60,
  300. [IPROC_PCIE_OMAP2] = 0xd68,
  301. [IPROC_PCIE_OARR3] = 0xdf0,
  302. [IPROC_PCIE_OMAP3] = 0xdf8,
  303. [IPROC_PCIE_IARR0] = 0xd00,
  304. [IPROC_PCIE_IMAP0] = 0xc00,
  305. [IPROC_PCIE_IARR2] = 0xd10,
  306. [IPROC_PCIE_IMAP2] = 0xcc0,
  307. [IPROC_PCIE_IARR3] = 0xe00,
  308. [IPROC_PCIE_IMAP3] = 0xe08,
  309. [IPROC_PCIE_IARR4] = 0xe68,
  310. [IPROC_PCIE_IMAP4] = 0xe70,
  311. [IPROC_PCIE_LINK_STATUS] = 0xf0c,
  312. [IPROC_PCIE_APB_ERR_EN] = 0xf40,
  313. };
  314. /* iProc PCIe PAXC v1 registers */
  315. static const u16 iproc_pcie_reg_paxc[] = {
  316. [IPROC_PCIE_CLK_CTRL] = 0x000,
  317. [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
  318. [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
  319. [IPROC_PCIE_CFG_ADDR] = 0x1f8,
  320. [IPROC_PCIE_CFG_DATA] = 0x1fc,
  321. };
  322. /* iProc PCIe PAXC v2 registers */
  323. static const u16 iproc_pcie_reg_paxc_v2[] = {
  324. [IPROC_PCIE_MSI_GIC_MODE] = 0x050,
  325. [IPROC_PCIE_MSI_BASE_ADDR] = 0x074,
  326. [IPROC_PCIE_MSI_WINDOW_SIZE] = 0x078,
  327. [IPROC_PCIE_MSI_ADDR_LO] = 0x07c,
  328. [IPROC_PCIE_MSI_ADDR_HI] = 0x080,
  329. [IPROC_PCIE_MSI_EN_CFG] = 0x09c,
  330. [IPROC_PCIE_CFG_IND_ADDR] = 0x1f0,
  331. [IPROC_PCIE_CFG_IND_DATA] = 0x1f4,
  332. [IPROC_PCIE_CFG_ADDR] = 0x1f8,
  333. [IPROC_PCIE_CFG_DATA] = 0x1fc,
  334. };
  335. /*
  336. * List of device IDs of controllers that have corrupted capability list that
  337. * require SW fixup
  338. */
  339. static const u16 iproc_pcie_corrupt_cap_did[] = {
  340. 0x16cd,
  341. 0x16f0,
  342. 0xd802,
  343. 0xd804
  344. };
  345. static inline struct iproc_pcie *iproc_data(struct pci_bus *bus)
  346. {
  347. struct iproc_pcie *pcie = bus->sysdata;
  348. return pcie;
  349. }
  350. static inline bool iproc_pcie_reg_is_invalid(u16 reg_offset)
  351. {
  352. return !!(reg_offset == IPROC_PCIE_REG_INVALID);
  353. }
  354. static inline u16 iproc_pcie_reg_offset(struct iproc_pcie *pcie,
  355. enum iproc_pcie_reg reg)
  356. {
  357. return pcie->reg_offsets[reg];
  358. }
  359. static inline u32 iproc_pcie_read_reg(struct iproc_pcie *pcie,
  360. enum iproc_pcie_reg reg)
  361. {
  362. u16 offset = iproc_pcie_reg_offset(pcie, reg);
  363. if (iproc_pcie_reg_is_invalid(offset))
  364. return 0;
  365. return readl(pcie->base + offset);
  366. }
  367. static inline void iproc_pcie_write_reg(struct iproc_pcie *pcie,
  368. enum iproc_pcie_reg reg, u32 val)
  369. {
  370. u16 offset = iproc_pcie_reg_offset(pcie, reg);
  371. if (iproc_pcie_reg_is_invalid(offset))
  372. return;
  373. writel(val, pcie->base + offset);
  374. }
  375. /**
  376. * APB error forwarding can be disabled during access of configuration
  377. * registers of the endpoint device, to prevent unsupported requests
  378. * (typically seen during enumeration with multi-function devices) from
  379. * triggering a system exception.
  380. */
  381. static inline void iproc_pcie_apb_err_disable(struct pci_bus *bus,
  382. bool disable)
  383. {
  384. struct iproc_pcie *pcie = iproc_data(bus);
  385. u32 val;
  386. if (bus->number && pcie->has_apb_err_disable) {
  387. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_APB_ERR_EN);
  388. if (disable)
  389. val &= ~APB_ERR_EN;
  390. else
  391. val |= APB_ERR_EN;
  392. iproc_pcie_write_reg(pcie, IPROC_PCIE_APB_ERR_EN, val);
  393. }
  394. }
  395. static void __iomem *iproc_pcie_map_ep_cfg_reg(struct iproc_pcie *pcie,
  396. unsigned int busno,
  397. unsigned int slot,
  398. unsigned int fn,
  399. int where)
  400. {
  401. u16 offset;
  402. u32 val;
  403. /* EP device access */
  404. val = (busno << CFG_ADDR_BUS_NUM_SHIFT) |
  405. (slot << CFG_ADDR_DEV_NUM_SHIFT) |
  406. (fn << CFG_ADDR_FUNC_NUM_SHIFT) |
  407. (where & CFG_ADDR_REG_NUM_MASK) |
  408. (1 & CFG_ADDR_CFG_TYPE_MASK);
  409. iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_ADDR, val);
  410. offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_DATA);
  411. if (iproc_pcie_reg_is_invalid(offset))
  412. return NULL;
  413. return (pcie->base + offset);
  414. }
  415. static unsigned int iproc_pcie_cfg_retry(void __iomem *cfg_data_p)
  416. {
  417. int timeout = CFG_RETRY_STATUS_TIMEOUT_US;
  418. unsigned int data;
  419. /*
  420. * As per PCIe spec r3.1, sec 2.3.2, CRS Software Visibility only
  421. * affects config reads of the Vendor ID. For config writes or any
  422. * other config reads, the Root may automatically reissue the
  423. * configuration request again as a new request.
  424. *
  425. * For config reads, this hardware returns CFG_RETRY_STATUS data
  426. * when it receives a CRS completion, regardless of the address of
  427. * the read or the CRS Software Visibility Enable bit. As a
  428. * partial workaround for this, we retry in software any read that
  429. * returns CFG_RETRY_STATUS.
  430. *
  431. * Note that a non-Vendor ID config register may have a value of
  432. * CFG_RETRY_STATUS. If we read that, we can't distinguish it from
  433. * a CRS completion, so we will incorrectly retry the read and
  434. * eventually return the wrong data (0xffffffff).
  435. */
  436. data = readl(cfg_data_p);
  437. while (data == CFG_RETRY_STATUS && timeout--) {
  438. udelay(1);
  439. data = readl(cfg_data_p);
  440. }
  441. if (data == CFG_RETRY_STATUS)
  442. data = 0xffffffff;
  443. return data;
  444. }
  445. static void iproc_pcie_fix_cap(struct iproc_pcie *pcie, int where, u32 *val)
  446. {
  447. u32 i, dev_id;
  448. switch (where & ~0x3) {
  449. case PCI_VENDOR_ID:
  450. dev_id = *val >> 16;
  451. /*
  452. * Activate fixup for those controllers that have corrupted
  453. * capability list registers
  454. */
  455. for (i = 0; i < ARRAY_SIZE(iproc_pcie_corrupt_cap_did); i++)
  456. if (dev_id == iproc_pcie_corrupt_cap_did[i])
  457. pcie->fix_paxc_cap = true;
  458. break;
  459. case IPROC_PCI_PM_CAP:
  460. if (pcie->fix_paxc_cap) {
  461. /* advertise PM, force next capability to PCIe */
  462. *val &= ~IPROC_PCI_PM_CAP_MASK;
  463. *val |= IPROC_PCI_EXP_CAP << 8 | PCI_CAP_ID_PM;
  464. }
  465. break;
  466. case IPROC_PCI_EXP_CAP:
  467. if (pcie->fix_paxc_cap) {
  468. /* advertise root port, version 2, terminate here */
  469. *val = (PCI_EXP_TYPE_ROOT_PORT << 4 | 2) << 16 |
  470. PCI_CAP_ID_EXP;
  471. }
  472. break;
  473. case IPROC_PCI_EXP_CAP + PCI_EXP_RTCTL:
  474. /* Don't advertise CRS SV support */
  475. *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
  476. break;
  477. default:
  478. break;
  479. }
  480. }
  481. static int iproc_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
  482. int where, int size, u32 *val)
  483. {
  484. struct iproc_pcie *pcie = iproc_data(bus);
  485. unsigned int slot = PCI_SLOT(devfn);
  486. unsigned int fn = PCI_FUNC(devfn);
  487. unsigned int busno = bus->number;
  488. void __iomem *cfg_data_p;
  489. unsigned int data;
  490. int ret;
  491. /* root complex access */
  492. if (busno == 0) {
  493. ret = pci_generic_config_read32(bus, devfn, where, size, val);
  494. if (ret == PCIBIOS_SUCCESSFUL)
  495. iproc_pcie_fix_cap(pcie, where, val);
  496. return ret;
  497. }
  498. cfg_data_p = iproc_pcie_map_ep_cfg_reg(pcie, busno, slot, fn, where);
  499. if (!cfg_data_p)
  500. return PCIBIOS_DEVICE_NOT_FOUND;
  501. data = iproc_pcie_cfg_retry(cfg_data_p);
  502. *val = data;
  503. if (size <= 2)
  504. *val = (data >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
  505. /*
  506. * For PAXC and PAXCv2, the total number of PFs that one can enumerate
  507. * depends on the firmware configuration. Unfortunately, due to an ASIC
  508. * bug, unconfigured PFs cannot be properly hidden from the root
  509. * complex. As a result, write access to these PFs will cause bus lock
  510. * up on the embedded processor
  511. *
  512. * Since all unconfigured PFs are left with an incorrect, staled device
  513. * ID of 0x168e (PCI_DEVICE_ID_NX2_57810), we try to catch those access
  514. * early here and reject them all
  515. */
  516. #define DEVICE_ID_MASK 0xffff0000
  517. #define DEVICE_ID_SHIFT 16
  518. if (pcie->rej_unconfig_pf &&
  519. (where & CFG_ADDR_REG_NUM_MASK) == PCI_VENDOR_ID)
  520. if ((*val & DEVICE_ID_MASK) ==
  521. (PCI_DEVICE_ID_NX2_57810 << DEVICE_ID_SHIFT))
  522. return PCIBIOS_FUNC_NOT_SUPPORTED;
  523. return PCIBIOS_SUCCESSFUL;
  524. }
  525. /**
  526. * Note access to the configuration registers are protected at the higher layer
  527. * by 'pci_lock' in drivers/pci/access.c
  528. */
  529. static void __iomem *iproc_pcie_map_cfg_bus(struct iproc_pcie *pcie,
  530. int busno, unsigned int devfn,
  531. int where)
  532. {
  533. unsigned slot = PCI_SLOT(devfn);
  534. unsigned fn = PCI_FUNC(devfn);
  535. u16 offset;
  536. /* root complex access */
  537. if (busno == 0) {
  538. if (slot > 0 || fn > 0)
  539. return NULL;
  540. iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
  541. where & CFG_IND_ADDR_MASK);
  542. offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
  543. if (iproc_pcie_reg_is_invalid(offset))
  544. return NULL;
  545. else
  546. return (pcie->base + offset);
  547. }
  548. /*
  549. * PAXC is connected to an internally emulated EP within the SoC. It
  550. * allows only one device.
  551. */
  552. if (pcie->ep_is_internal)
  553. if (slot > 0)
  554. return NULL;
  555. return iproc_pcie_map_ep_cfg_reg(pcie, busno, slot, fn, where);
  556. }
  557. static void __iomem *iproc_pcie_bus_map_cfg_bus(struct pci_bus *bus,
  558. unsigned int devfn,
  559. int where)
  560. {
  561. return iproc_pcie_map_cfg_bus(iproc_data(bus), bus->number, devfn,
  562. where);
  563. }
  564. static int iproc_pci_raw_config_read32(struct iproc_pcie *pcie,
  565. unsigned int devfn, int where,
  566. int size, u32 *val)
  567. {
  568. void __iomem *addr;
  569. addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
  570. if (!addr) {
  571. *val = ~0;
  572. return PCIBIOS_DEVICE_NOT_FOUND;
  573. }
  574. *val = readl(addr);
  575. if (size <= 2)
  576. *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
  577. return PCIBIOS_SUCCESSFUL;
  578. }
  579. static int iproc_pci_raw_config_write32(struct iproc_pcie *pcie,
  580. unsigned int devfn, int where,
  581. int size, u32 val)
  582. {
  583. void __iomem *addr;
  584. u32 mask, tmp;
  585. addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
  586. if (!addr)
  587. return PCIBIOS_DEVICE_NOT_FOUND;
  588. if (size == 4) {
  589. writel(val, addr);
  590. return PCIBIOS_SUCCESSFUL;
  591. }
  592. mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
  593. tmp = readl(addr) & mask;
  594. tmp |= val << ((where & 0x3) * 8);
  595. writel(tmp, addr);
  596. return PCIBIOS_SUCCESSFUL;
  597. }
  598. static int iproc_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
  599. int where, int size, u32 *val)
  600. {
  601. int ret;
  602. struct iproc_pcie *pcie = iproc_data(bus);
  603. iproc_pcie_apb_err_disable(bus, true);
  604. if (pcie->iproc_cfg_read)
  605. ret = iproc_pcie_config_read(bus, devfn, where, size, val);
  606. else
  607. ret = pci_generic_config_read32(bus, devfn, where, size, val);
  608. iproc_pcie_apb_err_disable(bus, false);
  609. return ret;
  610. }
  611. static int iproc_pcie_config_write32(struct pci_bus *bus, unsigned int devfn,
  612. int where, int size, u32 val)
  613. {
  614. int ret;
  615. iproc_pcie_apb_err_disable(bus, true);
  616. ret = pci_generic_config_write32(bus, devfn, where, size, val);
  617. iproc_pcie_apb_err_disable(bus, false);
  618. return ret;
  619. }
  620. static struct pci_ops iproc_pcie_ops = {
  621. .map_bus = iproc_pcie_bus_map_cfg_bus,
  622. .read = iproc_pcie_config_read32,
  623. .write = iproc_pcie_config_write32,
  624. };
  625. static void iproc_pcie_perst_ctrl(struct iproc_pcie *pcie, bool assert)
  626. {
  627. u32 val;
  628. /*
  629. * PAXC and the internal emulated endpoint device downstream should not
  630. * be reset. If firmware has been loaded on the endpoint device at an
  631. * earlier boot stage, reset here causes issues.
  632. */
  633. if (pcie->ep_is_internal)
  634. return;
  635. if (assert) {
  636. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
  637. val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
  638. ~RC_PCIE_RST_OUTPUT;
  639. iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
  640. udelay(250);
  641. } else {
  642. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
  643. val |= RC_PCIE_RST_OUTPUT;
  644. iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
  645. msleep(100);
  646. }
  647. }
  648. int iproc_pcie_shutdown(struct iproc_pcie *pcie)
  649. {
  650. iproc_pcie_perst_ctrl(pcie, true);
  651. msleep(500);
  652. return 0;
  653. }
  654. EXPORT_SYMBOL_GPL(iproc_pcie_shutdown);
  655. static int iproc_pcie_check_link(struct iproc_pcie *pcie)
  656. {
  657. struct device *dev = pcie->dev;
  658. u32 hdr_type, link_ctrl, link_status, class, val;
  659. bool link_is_active = false;
  660. /*
  661. * PAXC connects to emulated endpoint devices directly and does not
  662. * have a Serdes. Therefore skip the link detection logic here.
  663. */
  664. if (pcie->ep_is_internal)
  665. return 0;
  666. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
  667. if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) {
  668. dev_err(dev, "PHY or data link is INACTIVE!\n");
  669. return -ENODEV;
  670. }
  671. /* make sure we are not in EP mode */
  672. iproc_pci_raw_config_read32(pcie, 0, PCI_HEADER_TYPE, 1, &hdr_type);
  673. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
  674. dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type);
  675. return -EFAULT;
  676. }
  677. /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
  678. #define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
  679. #define PCI_CLASS_BRIDGE_MASK 0xffff00
  680. #define PCI_CLASS_BRIDGE_SHIFT 8
  681. iproc_pci_raw_config_read32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
  682. 4, &class);
  683. class &= ~PCI_CLASS_BRIDGE_MASK;
  684. class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
  685. iproc_pci_raw_config_write32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
  686. 4, class);
  687. /* check link status to see if link is active */
  688. iproc_pci_raw_config_read32(pcie, 0, IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
  689. 2, &link_status);
  690. if (link_status & PCI_EXP_LNKSTA_NLW)
  691. link_is_active = true;
  692. if (!link_is_active) {
  693. /* try GEN 1 link speed */
  694. #define PCI_TARGET_LINK_SPEED_MASK 0xf
  695. #define PCI_TARGET_LINK_SPEED_GEN2 0x2
  696. #define PCI_TARGET_LINK_SPEED_GEN1 0x1
  697. iproc_pci_raw_config_read32(pcie, 0,
  698. IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2,
  699. 4, &link_ctrl);
  700. if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
  701. PCI_TARGET_LINK_SPEED_GEN2) {
  702. link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
  703. link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
  704. iproc_pci_raw_config_write32(pcie, 0,
  705. IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2,
  706. 4, link_ctrl);
  707. msleep(100);
  708. iproc_pci_raw_config_read32(pcie, 0,
  709. IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
  710. 2, &link_status);
  711. if (link_status & PCI_EXP_LNKSTA_NLW)
  712. link_is_active = true;
  713. }
  714. }
  715. dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
  716. return link_is_active ? 0 : -ENODEV;
  717. }
  718. static void iproc_pcie_enable(struct iproc_pcie *pcie)
  719. {
  720. iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK);
  721. }
  722. static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie,
  723. int window_idx)
  724. {
  725. u32 val;
  726. val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx));
  727. return !!(val & OARR_VALID);
  728. }
  729. static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx,
  730. int size_idx, u64 axi_addr, u64 pci_addr)
  731. {
  732. struct device *dev = pcie->dev;
  733. u16 oarr_offset, omap_offset;
  734. /*
  735. * Derive the OARR/OMAP offset from the first pair (OARR0/OMAP0) based
  736. * on window index.
  737. */
  738. oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0,
  739. window_idx));
  740. omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0,
  741. window_idx));
  742. if (iproc_pcie_reg_is_invalid(oarr_offset) ||
  743. iproc_pcie_reg_is_invalid(omap_offset))
  744. return -EINVAL;
  745. /*
  746. * Program the OARR registers. The upper 32-bit OARR register is
  747. * always right after the lower 32-bit OARR register.
  748. */
  749. writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) |
  750. OARR_VALID, pcie->base + oarr_offset);
  751. writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4);
  752. /* now program the OMAP registers */
  753. writel(lower_32_bits(pci_addr), pcie->base + omap_offset);
  754. writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4);
  755. dev_dbg(dev, "ob window [%d]: offset 0x%x axi %pap pci %pap\n",
  756. window_idx, oarr_offset, &axi_addr, &pci_addr);
  757. dev_dbg(dev, "oarr lo 0x%x oarr hi 0x%x\n",
  758. readl(pcie->base + oarr_offset),
  759. readl(pcie->base + oarr_offset + 4));
  760. dev_dbg(dev, "omap lo 0x%x omap hi 0x%x\n",
  761. readl(pcie->base + omap_offset),
  762. readl(pcie->base + omap_offset + 4));
  763. return 0;
  764. }
  765. /**
  766. * Some iProc SoCs require the SW to configure the outbound address mapping
  767. *
  768. * Outbound address translation:
  769. *
  770. * iproc_pcie_address = axi_address - axi_offset
  771. * OARR = iproc_pcie_address
  772. * OMAP = pci_addr
  773. *
  774. * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
  775. */
  776. static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
  777. u64 pci_addr, resource_size_t size)
  778. {
  779. struct iproc_pcie_ob *ob = &pcie->ob;
  780. struct device *dev = pcie->dev;
  781. int ret = -EINVAL, window_idx, size_idx;
  782. if (axi_addr < ob->axi_offset) {
  783. dev_err(dev, "axi address %pap less than offset %pap\n",
  784. &axi_addr, &ob->axi_offset);
  785. return -EINVAL;
  786. }
  787. /*
  788. * Translate the AXI address to the internal address used by the iProc
  789. * PCIe core before programming the OARR
  790. */
  791. axi_addr -= ob->axi_offset;
  792. /* iterate through all OARR/OMAP mapping windows */
  793. for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
  794. const struct iproc_pcie_ob_map *ob_map =
  795. &pcie->ob_map[window_idx];
  796. /*
  797. * If current outbound window is already in use, move on to the
  798. * next one.
  799. */
  800. if (iproc_pcie_ob_is_valid(pcie, window_idx))
  801. continue;
  802. /*
  803. * Iterate through all supported window sizes within the
  804. * OARR/OMAP pair to find a match. Go through the window sizes
  805. * in a descending order.
  806. */
  807. for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0;
  808. size_idx--) {
  809. resource_size_t window_size =
  810. ob_map->window_sizes[size_idx] * SZ_1M;
  811. if (size < window_size)
  812. continue;
  813. if (!IS_ALIGNED(axi_addr, window_size) ||
  814. !IS_ALIGNED(pci_addr, window_size)) {
  815. dev_err(dev,
  816. "axi %pap or pci %pap not aligned\n",
  817. &axi_addr, &pci_addr);
  818. return -EINVAL;
  819. }
  820. /*
  821. * Match found! Program both OARR and OMAP and mark
  822. * them as a valid entry.
  823. */
  824. ret = iproc_pcie_ob_write(pcie, window_idx, size_idx,
  825. axi_addr, pci_addr);
  826. if (ret)
  827. goto err_ob;
  828. size -= window_size;
  829. if (size == 0)
  830. return 0;
  831. /*
  832. * If we are here, we are done with the current window,
  833. * but not yet finished all mappings. Need to move on
  834. * to the next window.
  835. */
  836. axi_addr += window_size;
  837. pci_addr += window_size;
  838. break;
  839. }
  840. }
  841. err_ob:
  842. dev_err(dev, "unable to configure outbound mapping\n");
  843. dev_err(dev,
  844. "axi %pap, axi offset %pap, pci %pap, res size %pap\n",
  845. &axi_addr, &ob->axi_offset, &pci_addr, &size);
  846. return ret;
  847. }
  848. static int iproc_pcie_map_ranges(struct iproc_pcie *pcie,
  849. struct list_head *resources)
  850. {
  851. struct device *dev = pcie->dev;
  852. struct resource_entry *window;
  853. int ret;
  854. resource_list_for_each_entry(window, resources) {
  855. struct resource *res = window->res;
  856. u64 res_type = resource_type(res);
  857. switch (res_type) {
  858. case IORESOURCE_IO:
  859. case IORESOURCE_BUS:
  860. break;
  861. case IORESOURCE_MEM:
  862. ret = iproc_pcie_setup_ob(pcie, res->start,
  863. res->start - window->offset,
  864. resource_size(res));
  865. if (ret)
  866. return ret;
  867. break;
  868. default:
  869. dev_err(dev, "invalid resource %pR\n", res);
  870. return -EINVAL;
  871. }
  872. }
  873. return 0;
  874. }
  875. static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie,
  876. int region_idx)
  877. {
  878. const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
  879. u32 val;
  880. val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx));
  881. return !!(val & (BIT(ib_map->nr_sizes) - 1));
  882. }
  883. static inline bool iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map,
  884. enum iproc_pcie_ib_map_type type)
  885. {
  886. return !!(ib_map->type == type);
  887. }
  888. static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx,
  889. int size_idx, int nr_windows, u64 axi_addr,
  890. u64 pci_addr, resource_size_t size)
  891. {
  892. struct device *dev = pcie->dev;
  893. const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
  894. u16 iarr_offset, imap_offset;
  895. u32 val;
  896. int window_idx;
  897. iarr_offset = iproc_pcie_reg_offset(pcie,
  898. MAP_REG(IPROC_PCIE_IARR0, region_idx));
  899. imap_offset = iproc_pcie_reg_offset(pcie,
  900. MAP_REG(IPROC_PCIE_IMAP0, region_idx));
  901. if (iproc_pcie_reg_is_invalid(iarr_offset) ||
  902. iproc_pcie_reg_is_invalid(imap_offset))
  903. return -EINVAL;
  904. dev_dbg(dev, "ib region [%d]: offset 0x%x axi %pap pci %pap\n",
  905. region_idx, iarr_offset, &axi_addr, &pci_addr);
  906. /*
  907. * Program the IARR registers. The upper 32-bit IARR register is
  908. * always right after the lower 32-bit IARR register.
  909. */
  910. writel(lower_32_bits(pci_addr) | BIT(size_idx),
  911. pcie->base + iarr_offset);
  912. writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4);
  913. dev_dbg(dev, "iarr lo 0x%x iarr hi 0x%x\n",
  914. readl(pcie->base + iarr_offset),
  915. readl(pcie->base + iarr_offset + 4));
  916. /*
  917. * Now program the IMAP registers. Each IARR region may have one or
  918. * more IMAP windows.
  919. */
  920. size >>= ilog2(nr_windows);
  921. for (window_idx = 0; window_idx < nr_windows; window_idx++) {
  922. val = readl(pcie->base + imap_offset);
  923. val |= lower_32_bits(axi_addr) | IMAP_VALID;
  924. writel(val, pcie->base + imap_offset);
  925. writel(upper_32_bits(axi_addr),
  926. pcie->base + imap_offset + ib_map->imap_addr_offset);
  927. dev_dbg(dev, "imap window [%d] lo 0x%x hi 0x%x\n",
  928. window_idx, readl(pcie->base + imap_offset),
  929. readl(pcie->base + imap_offset +
  930. ib_map->imap_addr_offset));
  931. imap_offset += ib_map->imap_window_offset;
  932. axi_addr += size;
  933. }
  934. return 0;
  935. }
  936. static int iproc_pcie_setup_ib(struct iproc_pcie *pcie,
  937. struct of_pci_range *range,
  938. enum iproc_pcie_ib_map_type type)
  939. {
  940. struct device *dev = pcie->dev;
  941. struct iproc_pcie_ib *ib = &pcie->ib;
  942. int ret;
  943. unsigned int region_idx, size_idx;
  944. u64 axi_addr = range->cpu_addr, pci_addr = range->pci_addr;
  945. resource_size_t size = range->size;
  946. /* iterate through all IARR mapping regions */
  947. for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
  948. const struct iproc_pcie_ib_map *ib_map =
  949. &pcie->ib_map[region_idx];
  950. /*
  951. * If current inbound region is already in use or not a
  952. * compatible type, move on to the next.
  953. */
  954. if (iproc_pcie_ib_is_in_use(pcie, region_idx) ||
  955. !iproc_pcie_ib_check_type(ib_map, type))
  956. continue;
  957. /* iterate through all supported region sizes to find a match */
  958. for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) {
  959. resource_size_t region_size =
  960. ib_map->region_sizes[size_idx] * ib_map->size_unit;
  961. if (size != region_size)
  962. continue;
  963. if (!IS_ALIGNED(axi_addr, region_size) ||
  964. !IS_ALIGNED(pci_addr, region_size)) {
  965. dev_err(dev,
  966. "axi %pap or pci %pap not aligned\n",
  967. &axi_addr, &pci_addr);
  968. return -EINVAL;
  969. }
  970. /* Match found! Program IARR and all IMAP windows. */
  971. ret = iproc_pcie_ib_write(pcie, region_idx, size_idx,
  972. ib_map->nr_windows, axi_addr,
  973. pci_addr, size);
  974. if (ret)
  975. goto err_ib;
  976. else
  977. return 0;
  978. }
  979. }
  980. ret = -EINVAL;
  981. err_ib:
  982. dev_err(dev, "unable to configure inbound mapping\n");
  983. dev_err(dev, "axi %pap, pci %pap, res size %pap\n",
  984. &axi_addr, &pci_addr, &size);
  985. return ret;
  986. }
  987. static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie)
  988. {
  989. struct of_pci_range range;
  990. struct of_pci_range_parser parser;
  991. int ret;
  992. /* Get the dma-ranges from DT */
  993. ret = of_pci_dma_range_parser_init(&parser, pcie->dev->of_node);
  994. if (ret)
  995. return ret;
  996. for_each_of_pci_range(&parser, &range) {
  997. /* Each range entry corresponds to an inbound mapping region */
  998. ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_MEM);
  999. if (ret)
  1000. return ret;
  1001. }
  1002. return 0;
  1003. }
  1004. static int iproce_pcie_get_msi(struct iproc_pcie *pcie,
  1005. struct device_node *msi_node,
  1006. u64 *msi_addr)
  1007. {
  1008. struct device *dev = pcie->dev;
  1009. int ret;
  1010. struct resource res;
  1011. /*
  1012. * Check if 'msi-map' points to ARM GICv3 ITS, which is the only
  1013. * supported external MSI controller that requires steering.
  1014. */
  1015. if (!of_device_is_compatible(msi_node, "arm,gic-v3-its")) {
  1016. dev_err(dev, "unable to find compatible MSI controller\n");
  1017. return -ENODEV;
  1018. }
  1019. /* derive GITS_TRANSLATER address from GICv3 */
  1020. ret = of_address_to_resource(msi_node, 0, &res);
  1021. if (ret < 0) {
  1022. dev_err(dev, "unable to obtain MSI controller resources\n");
  1023. return ret;
  1024. }
  1025. *msi_addr = res.start + GITS_TRANSLATER;
  1026. return 0;
  1027. }
  1028. static int iproc_pcie_paxb_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr)
  1029. {
  1030. int ret;
  1031. struct of_pci_range range;
  1032. memset(&range, 0, sizeof(range));
  1033. range.size = SZ_32K;
  1034. range.pci_addr = range.cpu_addr = msi_addr & ~(range.size - 1);
  1035. ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_IO);
  1036. return ret;
  1037. }
  1038. static void iproc_pcie_paxc_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr,
  1039. bool enable)
  1040. {
  1041. u32 val;
  1042. if (!enable) {
  1043. /*
  1044. * Disable PAXC MSI steering. All write transfers will be
  1045. * treated as non-MSI transfers
  1046. */
  1047. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
  1048. val &= ~MSI_ENABLE_CFG;
  1049. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
  1050. return;
  1051. }
  1052. /*
  1053. * Program bits [43:13] of address of GITS_TRANSLATER register into
  1054. * bits [30:0] of the MSI base address register. In fact, in all iProc
  1055. * based SoCs, all I/O register bases are well below the 32-bit
  1056. * boundary, so we can safely assume bits [43:32] are always zeros.
  1057. */
  1058. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_BASE_ADDR,
  1059. (u32)(msi_addr >> 13));
  1060. /* use a default 8K window size */
  1061. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_WINDOW_SIZE, 0);
  1062. /* steering MSI to GICv3 ITS */
  1063. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_GIC_MODE);
  1064. val |= GIC_V3_CFG;
  1065. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_GIC_MODE, val);
  1066. /*
  1067. * Program bits [43:2] of address of GITS_TRANSLATER register into the
  1068. * iProc MSI address registers.
  1069. */
  1070. msi_addr >>= 2;
  1071. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_HI,
  1072. upper_32_bits(msi_addr));
  1073. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_LO,
  1074. lower_32_bits(msi_addr));
  1075. /* enable MSI */
  1076. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
  1077. val |= MSI_ENABLE_CFG;
  1078. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
  1079. }
  1080. static int iproc_pcie_msi_steer(struct iproc_pcie *pcie,
  1081. struct device_node *msi_node)
  1082. {
  1083. struct device *dev = pcie->dev;
  1084. int ret;
  1085. u64 msi_addr;
  1086. ret = iproce_pcie_get_msi(pcie, msi_node, &msi_addr);
  1087. if (ret < 0) {
  1088. dev_err(dev, "msi steering failed\n");
  1089. return ret;
  1090. }
  1091. switch (pcie->type) {
  1092. case IPROC_PCIE_PAXB_V2:
  1093. ret = iproc_pcie_paxb_v2_msi_steer(pcie, msi_addr);
  1094. if (ret)
  1095. return ret;
  1096. break;
  1097. case IPROC_PCIE_PAXC_V2:
  1098. iproc_pcie_paxc_v2_msi_steer(pcie, msi_addr, true);
  1099. break;
  1100. default:
  1101. return -EINVAL;
  1102. }
  1103. return 0;
  1104. }
  1105. static int iproc_pcie_msi_enable(struct iproc_pcie *pcie)
  1106. {
  1107. struct device_node *msi_node;
  1108. int ret;
  1109. /*
  1110. * Either the "msi-parent" or the "msi-map" phandle needs to exist
  1111. * for us to obtain the MSI node.
  1112. */
  1113. msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0);
  1114. if (!msi_node) {
  1115. const __be32 *msi_map = NULL;
  1116. int len;
  1117. u32 phandle;
  1118. msi_map = of_get_property(pcie->dev->of_node, "msi-map", &len);
  1119. if (!msi_map)
  1120. return -ENODEV;
  1121. phandle = be32_to_cpup(msi_map + 1);
  1122. msi_node = of_find_node_by_phandle(phandle);
  1123. if (!msi_node)
  1124. return -ENODEV;
  1125. }
  1126. /*
  1127. * Certain revisions of the iProc PCIe controller require additional
  1128. * configurations to steer the MSI writes towards an external MSI
  1129. * controller.
  1130. */
  1131. if (pcie->need_msi_steer) {
  1132. ret = iproc_pcie_msi_steer(pcie, msi_node);
  1133. if (ret)
  1134. return ret;
  1135. }
  1136. /*
  1137. * If another MSI controller is being used, the call below should fail
  1138. * but that is okay
  1139. */
  1140. return iproc_msi_init(pcie, msi_node);
  1141. }
  1142. static void iproc_pcie_msi_disable(struct iproc_pcie *pcie)
  1143. {
  1144. iproc_msi_exit(pcie);
  1145. }
  1146. static int iproc_pcie_rev_init(struct iproc_pcie *pcie)
  1147. {
  1148. struct device *dev = pcie->dev;
  1149. unsigned int reg_idx;
  1150. const u16 *regs;
  1151. switch (pcie->type) {
  1152. case IPROC_PCIE_PAXB_BCMA:
  1153. regs = iproc_pcie_reg_paxb_bcma;
  1154. break;
  1155. case IPROC_PCIE_PAXB:
  1156. regs = iproc_pcie_reg_paxb;
  1157. pcie->iproc_cfg_read = true;
  1158. pcie->has_apb_err_disable = true;
  1159. if (pcie->need_ob_cfg) {
  1160. pcie->ob_map = paxb_ob_map;
  1161. pcie->ob.nr_windows = ARRAY_SIZE(paxb_ob_map);
  1162. }
  1163. break;
  1164. case IPROC_PCIE_PAXB_V2:
  1165. regs = iproc_pcie_reg_paxb_v2;
  1166. pcie->has_apb_err_disable = true;
  1167. if (pcie->need_ob_cfg) {
  1168. pcie->ob_map = paxb_v2_ob_map;
  1169. pcie->ob.nr_windows = ARRAY_SIZE(paxb_v2_ob_map);
  1170. }
  1171. pcie->ib.nr_regions = ARRAY_SIZE(paxb_v2_ib_map);
  1172. pcie->ib_map = paxb_v2_ib_map;
  1173. pcie->need_msi_steer = true;
  1174. dev_warn(dev, "reads of config registers that contain %#x return incorrect data\n",
  1175. CFG_RETRY_STATUS);
  1176. break;
  1177. case IPROC_PCIE_PAXC:
  1178. regs = iproc_pcie_reg_paxc;
  1179. pcie->ep_is_internal = true;
  1180. pcie->iproc_cfg_read = true;
  1181. pcie->rej_unconfig_pf = true;
  1182. break;
  1183. case IPROC_PCIE_PAXC_V2:
  1184. regs = iproc_pcie_reg_paxc_v2;
  1185. pcie->ep_is_internal = true;
  1186. pcie->iproc_cfg_read = true;
  1187. pcie->rej_unconfig_pf = true;
  1188. pcie->need_msi_steer = true;
  1189. break;
  1190. default:
  1191. dev_err(dev, "incompatible iProc PCIe interface\n");
  1192. return -EINVAL;
  1193. }
  1194. pcie->reg_offsets = devm_kcalloc(dev, IPROC_PCIE_MAX_NUM_REG,
  1195. sizeof(*pcie->reg_offsets),
  1196. GFP_KERNEL);
  1197. if (!pcie->reg_offsets)
  1198. return -ENOMEM;
  1199. /* go through the register table and populate all valid registers */
  1200. pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ?
  1201. IPROC_PCIE_REG_INVALID : regs[0];
  1202. for (reg_idx = 1; reg_idx < IPROC_PCIE_MAX_NUM_REG; reg_idx++)
  1203. pcie->reg_offsets[reg_idx] = regs[reg_idx] ?
  1204. regs[reg_idx] : IPROC_PCIE_REG_INVALID;
  1205. return 0;
  1206. }
  1207. int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
  1208. {
  1209. struct device *dev;
  1210. int ret;
  1211. struct pci_bus *child;
  1212. struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
  1213. dev = pcie->dev;
  1214. ret = iproc_pcie_rev_init(pcie);
  1215. if (ret) {
  1216. dev_err(dev, "unable to initialize controller parameters\n");
  1217. return ret;
  1218. }
  1219. ret = devm_request_pci_bus_resources(dev, res);
  1220. if (ret)
  1221. return ret;
  1222. ret = phy_init(pcie->phy);
  1223. if (ret) {
  1224. dev_err(dev, "unable to initialize PCIe PHY\n");
  1225. return ret;
  1226. }
  1227. ret = phy_power_on(pcie->phy);
  1228. if (ret) {
  1229. dev_err(dev, "unable to power on PCIe PHY\n");
  1230. goto err_exit_phy;
  1231. }
  1232. iproc_pcie_perst_ctrl(pcie, true);
  1233. iproc_pcie_perst_ctrl(pcie, false);
  1234. if (pcie->need_ob_cfg) {
  1235. ret = iproc_pcie_map_ranges(pcie, res);
  1236. if (ret) {
  1237. dev_err(dev, "map failed\n");
  1238. goto err_power_off_phy;
  1239. }
  1240. }
  1241. if (pcie->need_ib_cfg) {
  1242. ret = iproc_pcie_map_dma_ranges(pcie);
  1243. if (ret && ret != -ENOENT)
  1244. goto err_power_off_phy;
  1245. }
  1246. ret = iproc_pcie_check_link(pcie);
  1247. if (ret) {
  1248. dev_err(dev, "no PCIe EP device detected\n");
  1249. goto err_power_off_phy;
  1250. }
  1251. iproc_pcie_enable(pcie);
  1252. if (IS_ENABLED(CONFIG_PCI_MSI))
  1253. if (iproc_pcie_msi_enable(pcie))
  1254. dev_info(dev, "not using iProc MSI\n");
  1255. list_splice_init(res, &host->windows);
  1256. host->busnr = 0;
  1257. host->dev.parent = dev;
  1258. host->ops = &iproc_pcie_ops;
  1259. host->sysdata = pcie;
  1260. host->map_irq = pcie->map_irq;
  1261. host->swizzle_irq = pci_common_swizzle;
  1262. ret = pci_scan_root_bus_bridge(host);
  1263. if (ret < 0) {
  1264. dev_err(dev, "failed to scan host: %d\n", ret);
  1265. goto err_power_off_phy;
  1266. }
  1267. pci_assign_unassigned_bus_resources(host->bus);
  1268. pcie->root_bus = host->bus;
  1269. list_for_each_entry(child, &host->bus->children, node)
  1270. pcie_bus_configure_settings(child);
  1271. pci_bus_add_devices(host->bus);
  1272. return 0;
  1273. err_power_off_phy:
  1274. phy_power_off(pcie->phy);
  1275. err_exit_phy:
  1276. phy_exit(pcie->phy);
  1277. return ret;
  1278. }
  1279. EXPORT_SYMBOL(iproc_pcie_setup);
  1280. int iproc_pcie_remove(struct iproc_pcie *pcie)
  1281. {
  1282. pci_stop_root_bus(pcie->root_bus);
  1283. pci_remove_root_bus(pcie->root_bus);
  1284. iproc_pcie_msi_disable(pcie);
  1285. phy_power_off(pcie->phy);
  1286. phy_exit(pcie->phy);
  1287. return 0;
  1288. }
  1289. EXPORT_SYMBOL(iproc_pcie_remove);
  1290. /*
  1291. * The MSI parsing logic in certain revisions of Broadcom PAXC based root
  1292. * complex does not work and needs to be disabled
  1293. */
  1294. static void quirk_paxc_disable_msi_parsing(struct pci_dev *pdev)
  1295. {
  1296. struct iproc_pcie *pcie = iproc_data(pdev->bus);
  1297. if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  1298. iproc_pcie_paxc_v2_msi_steer(pcie, 0, false);
  1299. }
  1300. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0,
  1301. quirk_paxc_disable_msi_parsing);
  1302. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802,
  1303. quirk_paxc_disable_msi_parsing);
  1304. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804,
  1305. quirk_paxc_disable_msi_parsing);
  1306. MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
  1307. MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
  1308. MODULE_LICENSE("GPL v2");