pcie-iproc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  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. return PCIBIOS_SUCCESSFUL;
  506. }
  507. /**
  508. * Note access to the configuration registers are protected at the higher layer
  509. * by 'pci_lock' in drivers/pci/access.c
  510. */
  511. static void __iomem *iproc_pcie_map_cfg_bus(struct iproc_pcie *pcie,
  512. int busno, unsigned int devfn,
  513. int where)
  514. {
  515. unsigned slot = PCI_SLOT(devfn);
  516. unsigned fn = PCI_FUNC(devfn);
  517. u16 offset;
  518. /* root complex access */
  519. if (busno == 0) {
  520. if (slot > 0 || fn > 0)
  521. return NULL;
  522. iproc_pcie_write_reg(pcie, IPROC_PCIE_CFG_IND_ADDR,
  523. where & CFG_IND_ADDR_MASK);
  524. offset = iproc_pcie_reg_offset(pcie, IPROC_PCIE_CFG_IND_DATA);
  525. if (iproc_pcie_reg_is_invalid(offset))
  526. return NULL;
  527. else
  528. return (pcie->base + offset);
  529. }
  530. /*
  531. * PAXC is connected to an internally emulated EP within the SoC. It
  532. * allows only one device.
  533. */
  534. if (pcie->ep_is_internal)
  535. if (slot > 0)
  536. return NULL;
  537. return iproc_pcie_map_ep_cfg_reg(pcie, busno, slot, fn, where);
  538. }
  539. static void __iomem *iproc_pcie_bus_map_cfg_bus(struct pci_bus *bus,
  540. unsigned int devfn,
  541. int where)
  542. {
  543. return iproc_pcie_map_cfg_bus(iproc_data(bus), bus->number, devfn,
  544. where);
  545. }
  546. static int iproc_pci_raw_config_read32(struct iproc_pcie *pcie,
  547. unsigned int devfn, int where,
  548. int size, u32 *val)
  549. {
  550. void __iomem *addr;
  551. addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
  552. if (!addr) {
  553. *val = ~0;
  554. return PCIBIOS_DEVICE_NOT_FOUND;
  555. }
  556. *val = readl(addr);
  557. if (size <= 2)
  558. *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
  559. return PCIBIOS_SUCCESSFUL;
  560. }
  561. static int iproc_pci_raw_config_write32(struct iproc_pcie *pcie,
  562. unsigned int devfn, int where,
  563. int size, u32 val)
  564. {
  565. void __iomem *addr;
  566. u32 mask, tmp;
  567. addr = iproc_pcie_map_cfg_bus(pcie, 0, devfn, where & ~0x3);
  568. if (!addr)
  569. return PCIBIOS_DEVICE_NOT_FOUND;
  570. if (size == 4) {
  571. writel(val, addr);
  572. return PCIBIOS_SUCCESSFUL;
  573. }
  574. mask = ~(((1 << (size * 8)) - 1) << ((where & 0x3) * 8));
  575. tmp = readl(addr) & mask;
  576. tmp |= val << ((where & 0x3) * 8);
  577. writel(tmp, addr);
  578. return PCIBIOS_SUCCESSFUL;
  579. }
  580. static int iproc_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
  581. int where, int size, u32 *val)
  582. {
  583. int ret;
  584. struct iproc_pcie *pcie = iproc_data(bus);
  585. iproc_pcie_apb_err_disable(bus, true);
  586. if (pcie->type == IPROC_PCIE_PAXB_V2)
  587. ret = iproc_pcie_config_read(bus, devfn, where, size, val);
  588. else
  589. ret = pci_generic_config_read32(bus, devfn, where, size, val);
  590. iproc_pcie_apb_err_disable(bus, false);
  591. return ret;
  592. }
  593. static int iproc_pcie_config_write32(struct pci_bus *bus, unsigned int devfn,
  594. int where, int size, u32 val)
  595. {
  596. int ret;
  597. iproc_pcie_apb_err_disable(bus, true);
  598. ret = pci_generic_config_write32(bus, devfn, where, size, val);
  599. iproc_pcie_apb_err_disable(bus, false);
  600. return ret;
  601. }
  602. static struct pci_ops iproc_pcie_ops = {
  603. .map_bus = iproc_pcie_bus_map_cfg_bus,
  604. .read = iproc_pcie_config_read32,
  605. .write = iproc_pcie_config_write32,
  606. };
  607. static void iproc_pcie_perst_ctrl(struct iproc_pcie *pcie, bool assert)
  608. {
  609. u32 val;
  610. /*
  611. * PAXC and the internal emulated endpoint device downstream should not
  612. * be reset. If firmware has been loaded on the endpoint device at an
  613. * earlier boot stage, reset here causes issues.
  614. */
  615. if (pcie->ep_is_internal)
  616. return;
  617. if (assert) {
  618. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
  619. val &= ~EP_PERST_SOURCE_SELECT & ~EP_MODE_SURVIVE_PERST &
  620. ~RC_PCIE_RST_OUTPUT;
  621. iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
  622. udelay(250);
  623. } else {
  624. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_CLK_CTRL);
  625. val |= RC_PCIE_RST_OUTPUT;
  626. iproc_pcie_write_reg(pcie, IPROC_PCIE_CLK_CTRL, val);
  627. msleep(100);
  628. }
  629. }
  630. int iproc_pcie_shutdown(struct iproc_pcie *pcie)
  631. {
  632. iproc_pcie_perst_ctrl(pcie, true);
  633. msleep(500);
  634. return 0;
  635. }
  636. EXPORT_SYMBOL_GPL(iproc_pcie_shutdown);
  637. static int iproc_pcie_check_link(struct iproc_pcie *pcie)
  638. {
  639. struct device *dev = pcie->dev;
  640. u32 hdr_type, link_ctrl, link_status, class, val;
  641. bool link_is_active = false;
  642. /*
  643. * PAXC connects to emulated endpoint devices directly and does not
  644. * have a Serdes. Therefore skip the link detection logic here.
  645. */
  646. if (pcie->ep_is_internal)
  647. return 0;
  648. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_LINK_STATUS);
  649. if (!(val & PCIE_PHYLINKUP) || !(val & PCIE_DL_ACTIVE)) {
  650. dev_err(dev, "PHY or data link is INACTIVE!\n");
  651. return -ENODEV;
  652. }
  653. /* make sure we are not in EP mode */
  654. iproc_pci_raw_config_read32(pcie, 0, PCI_HEADER_TYPE, 1, &hdr_type);
  655. if ((hdr_type & 0x7f) != PCI_HEADER_TYPE_BRIDGE) {
  656. dev_err(dev, "in EP mode, hdr=%#02x\n", hdr_type);
  657. return -EFAULT;
  658. }
  659. /* force class to PCI_CLASS_BRIDGE_PCI (0x0604) */
  660. #define PCI_BRIDGE_CTRL_REG_OFFSET 0x43c
  661. #define PCI_CLASS_BRIDGE_MASK 0xffff00
  662. #define PCI_CLASS_BRIDGE_SHIFT 8
  663. iproc_pci_raw_config_read32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
  664. 4, &class);
  665. class &= ~PCI_CLASS_BRIDGE_MASK;
  666. class |= (PCI_CLASS_BRIDGE_PCI << PCI_CLASS_BRIDGE_SHIFT);
  667. iproc_pci_raw_config_write32(pcie, 0, PCI_BRIDGE_CTRL_REG_OFFSET,
  668. 4, class);
  669. /* check link status to see if link is active */
  670. iproc_pci_raw_config_read32(pcie, 0, IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
  671. 2, &link_status);
  672. if (link_status & PCI_EXP_LNKSTA_NLW)
  673. link_is_active = true;
  674. if (!link_is_active) {
  675. /* try GEN 1 link speed */
  676. #define PCI_TARGET_LINK_SPEED_MASK 0xf
  677. #define PCI_TARGET_LINK_SPEED_GEN2 0x2
  678. #define PCI_TARGET_LINK_SPEED_GEN1 0x1
  679. iproc_pci_raw_config_read32(pcie, 0,
  680. IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2,
  681. 4, &link_ctrl);
  682. if ((link_ctrl & PCI_TARGET_LINK_SPEED_MASK) ==
  683. PCI_TARGET_LINK_SPEED_GEN2) {
  684. link_ctrl &= ~PCI_TARGET_LINK_SPEED_MASK;
  685. link_ctrl |= PCI_TARGET_LINK_SPEED_GEN1;
  686. iproc_pci_raw_config_write32(pcie, 0,
  687. IPROC_PCI_EXP_CAP + PCI_EXP_LNKCTL2,
  688. 4, link_ctrl);
  689. msleep(100);
  690. iproc_pci_raw_config_read32(pcie, 0,
  691. IPROC_PCI_EXP_CAP + PCI_EXP_LNKSTA,
  692. 2, &link_status);
  693. if (link_status & PCI_EXP_LNKSTA_NLW)
  694. link_is_active = true;
  695. }
  696. }
  697. dev_info(dev, "link: %s\n", link_is_active ? "UP" : "DOWN");
  698. return link_is_active ? 0 : -ENODEV;
  699. }
  700. static void iproc_pcie_enable(struct iproc_pcie *pcie)
  701. {
  702. iproc_pcie_write_reg(pcie, IPROC_PCIE_INTX_EN, SYS_RC_INTX_MASK);
  703. }
  704. static inline bool iproc_pcie_ob_is_valid(struct iproc_pcie *pcie,
  705. int window_idx)
  706. {
  707. u32 val;
  708. val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_OARR0, window_idx));
  709. return !!(val & OARR_VALID);
  710. }
  711. static inline int iproc_pcie_ob_write(struct iproc_pcie *pcie, int window_idx,
  712. int size_idx, u64 axi_addr, u64 pci_addr)
  713. {
  714. struct device *dev = pcie->dev;
  715. u16 oarr_offset, omap_offset;
  716. /*
  717. * Derive the OARR/OMAP offset from the first pair (OARR0/OMAP0) based
  718. * on window index.
  719. */
  720. oarr_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OARR0,
  721. window_idx));
  722. omap_offset = iproc_pcie_reg_offset(pcie, MAP_REG(IPROC_PCIE_OMAP0,
  723. window_idx));
  724. if (iproc_pcie_reg_is_invalid(oarr_offset) ||
  725. iproc_pcie_reg_is_invalid(omap_offset))
  726. return -EINVAL;
  727. /*
  728. * Program the OARR registers. The upper 32-bit OARR register is
  729. * always right after the lower 32-bit OARR register.
  730. */
  731. writel(lower_32_bits(axi_addr) | (size_idx << OARR_SIZE_CFG_SHIFT) |
  732. OARR_VALID, pcie->base + oarr_offset);
  733. writel(upper_32_bits(axi_addr), pcie->base + oarr_offset + 4);
  734. /* now program the OMAP registers */
  735. writel(lower_32_bits(pci_addr), pcie->base + omap_offset);
  736. writel(upper_32_bits(pci_addr), pcie->base + omap_offset + 4);
  737. dev_info(dev, "ob window [%d]: offset 0x%x axi %pap pci %pap\n",
  738. window_idx, oarr_offset, &axi_addr, &pci_addr);
  739. dev_info(dev, "oarr lo 0x%x oarr hi 0x%x\n",
  740. readl(pcie->base + oarr_offset),
  741. readl(pcie->base + oarr_offset + 4));
  742. dev_info(dev, "omap lo 0x%x omap hi 0x%x\n",
  743. readl(pcie->base + omap_offset),
  744. readl(pcie->base + omap_offset + 4));
  745. return 0;
  746. }
  747. /**
  748. * Some iProc SoCs require the SW to configure the outbound address mapping
  749. *
  750. * Outbound address translation:
  751. *
  752. * iproc_pcie_address = axi_address - axi_offset
  753. * OARR = iproc_pcie_address
  754. * OMAP = pci_addr
  755. *
  756. * axi_addr -> iproc_pcie_address -> OARR -> OMAP -> pci_address
  757. */
  758. static int iproc_pcie_setup_ob(struct iproc_pcie *pcie, u64 axi_addr,
  759. u64 pci_addr, resource_size_t size)
  760. {
  761. struct iproc_pcie_ob *ob = &pcie->ob;
  762. struct device *dev = pcie->dev;
  763. int ret = -EINVAL, window_idx, size_idx;
  764. if (axi_addr < ob->axi_offset) {
  765. dev_err(dev, "axi address %pap less than offset %pap\n",
  766. &axi_addr, &ob->axi_offset);
  767. return -EINVAL;
  768. }
  769. /*
  770. * Translate the AXI address to the internal address used by the iProc
  771. * PCIe core before programming the OARR
  772. */
  773. axi_addr -= ob->axi_offset;
  774. /* iterate through all OARR/OMAP mapping windows */
  775. for (window_idx = ob->nr_windows - 1; window_idx >= 0; window_idx--) {
  776. const struct iproc_pcie_ob_map *ob_map =
  777. &pcie->ob_map[window_idx];
  778. /*
  779. * If current outbound window is already in use, move on to the
  780. * next one.
  781. */
  782. if (iproc_pcie_ob_is_valid(pcie, window_idx))
  783. continue;
  784. /*
  785. * Iterate through all supported window sizes within the
  786. * OARR/OMAP pair to find a match. Go through the window sizes
  787. * in a descending order.
  788. */
  789. for (size_idx = ob_map->nr_sizes - 1; size_idx >= 0;
  790. size_idx--) {
  791. resource_size_t window_size =
  792. ob_map->window_sizes[size_idx] * SZ_1M;
  793. if (size < window_size)
  794. continue;
  795. if (!IS_ALIGNED(axi_addr, window_size) ||
  796. !IS_ALIGNED(pci_addr, window_size)) {
  797. dev_err(dev,
  798. "axi %pap or pci %pap not aligned\n",
  799. &axi_addr, &pci_addr);
  800. return -EINVAL;
  801. }
  802. /*
  803. * Match found! Program both OARR and OMAP and mark
  804. * them as a valid entry.
  805. */
  806. ret = iproc_pcie_ob_write(pcie, window_idx, size_idx,
  807. axi_addr, pci_addr);
  808. if (ret)
  809. goto err_ob;
  810. size -= window_size;
  811. if (size == 0)
  812. return 0;
  813. /*
  814. * If we are here, we are done with the current window,
  815. * but not yet finished all mappings. Need to move on
  816. * to the next window.
  817. */
  818. axi_addr += window_size;
  819. pci_addr += window_size;
  820. break;
  821. }
  822. }
  823. err_ob:
  824. dev_err(dev, "unable to configure outbound mapping\n");
  825. dev_err(dev,
  826. "axi %pap, axi offset %pap, pci %pap, res size %pap\n",
  827. &axi_addr, &ob->axi_offset, &pci_addr, &size);
  828. return ret;
  829. }
  830. static int iproc_pcie_map_ranges(struct iproc_pcie *pcie,
  831. struct list_head *resources)
  832. {
  833. struct device *dev = pcie->dev;
  834. struct resource_entry *window;
  835. int ret;
  836. resource_list_for_each_entry(window, resources) {
  837. struct resource *res = window->res;
  838. u64 res_type = resource_type(res);
  839. switch (res_type) {
  840. case IORESOURCE_IO:
  841. case IORESOURCE_BUS:
  842. break;
  843. case IORESOURCE_MEM:
  844. ret = iproc_pcie_setup_ob(pcie, res->start,
  845. res->start - window->offset,
  846. resource_size(res));
  847. if (ret)
  848. return ret;
  849. break;
  850. default:
  851. dev_err(dev, "invalid resource %pR\n", res);
  852. return -EINVAL;
  853. }
  854. }
  855. return 0;
  856. }
  857. static inline bool iproc_pcie_ib_is_in_use(struct iproc_pcie *pcie,
  858. int region_idx)
  859. {
  860. const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
  861. u32 val;
  862. val = iproc_pcie_read_reg(pcie, MAP_REG(IPROC_PCIE_IARR0, region_idx));
  863. return !!(val & (BIT(ib_map->nr_sizes) - 1));
  864. }
  865. static inline bool iproc_pcie_ib_check_type(const struct iproc_pcie_ib_map *ib_map,
  866. enum iproc_pcie_ib_map_type type)
  867. {
  868. return !!(ib_map->type == type);
  869. }
  870. static int iproc_pcie_ib_write(struct iproc_pcie *pcie, int region_idx,
  871. int size_idx, int nr_windows, u64 axi_addr,
  872. u64 pci_addr, resource_size_t size)
  873. {
  874. struct device *dev = pcie->dev;
  875. const struct iproc_pcie_ib_map *ib_map = &pcie->ib_map[region_idx];
  876. u16 iarr_offset, imap_offset;
  877. u32 val;
  878. int window_idx;
  879. iarr_offset = iproc_pcie_reg_offset(pcie,
  880. MAP_REG(IPROC_PCIE_IARR0, region_idx));
  881. imap_offset = iproc_pcie_reg_offset(pcie,
  882. MAP_REG(IPROC_PCIE_IMAP0, region_idx));
  883. if (iproc_pcie_reg_is_invalid(iarr_offset) ||
  884. iproc_pcie_reg_is_invalid(imap_offset))
  885. return -EINVAL;
  886. dev_info(dev, "ib region [%d]: offset 0x%x axi %pap pci %pap\n",
  887. region_idx, iarr_offset, &axi_addr, &pci_addr);
  888. /*
  889. * Program the IARR registers. The upper 32-bit IARR register is
  890. * always right after the lower 32-bit IARR register.
  891. */
  892. writel(lower_32_bits(pci_addr) | BIT(size_idx),
  893. pcie->base + iarr_offset);
  894. writel(upper_32_bits(pci_addr), pcie->base + iarr_offset + 4);
  895. dev_info(dev, "iarr lo 0x%x iarr hi 0x%x\n",
  896. readl(pcie->base + iarr_offset),
  897. readl(pcie->base + iarr_offset + 4));
  898. /*
  899. * Now program the IMAP registers. Each IARR region may have one or
  900. * more IMAP windows.
  901. */
  902. size >>= ilog2(nr_windows);
  903. for (window_idx = 0; window_idx < nr_windows; window_idx++) {
  904. val = readl(pcie->base + imap_offset);
  905. val |= lower_32_bits(axi_addr) | IMAP_VALID;
  906. writel(val, pcie->base + imap_offset);
  907. writel(upper_32_bits(axi_addr),
  908. pcie->base + imap_offset + ib_map->imap_addr_offset);
  909. dev_info(dev, "imap window [%d] lo 0x%x hi 0x%x\n",
  910. window_idx, readl(pcie->base + imap_offset),
  911. readl(pcie->base + imap_offset +
  912. ib_map->imap_addr_offset));
  913. imap_offset += ib_map->imap_window_offset;
  914. axi_addr += size;
  915. }
  916. return 0;
  917. }
  918. static int iproc_pcie_setup_ib(struct iproc_pcie *pcie,
  919. struct of_pci_range *range,
  920. enum iproc_pcie_ib_map_type type)
  921. {
  922. struct device *dev = pcie->dev;
  923. struct iproc_pcie_ib *ib = &pcie->ib;
  924. int ret;
  925. unsigned int region_idx, size_idx;
  926. u64 axi_addr = range->cpu_addr, pci_addr = range->pci_addr;
  927. resource_size_t size = range->size;
  928. /* iterate through all IARR mapping regions */
  929. for (region_idx = 0; region_idx < ib->nr_regions; region_idx++) {
  930. const struct iproc_pcie_ib_map *ib_map =
  931. &pcie->ib_map[region_idx];
  932. /*
  933. * If current inbound region is already in use or not a
  934. * compatible type, move on to the next.
  935. */
  936. if (iproc_pcie_ib_is_in_use(pcie, region_idx) ||
  937. !iproc_pcie_ib_check_type(ib_map, type))
  938. continue;
  939. /* iterate through all supported region sizes to find a match */
  940. for (size_idx = 0; size_idx < ib_map->nr_sizes; size_idx++) {
  941. resource_size_t region_size =
  942. ib_map->region_sizes[size_idx] * ib_map->size_unit;
  943. if (size != region_size)
  944. continue;
  945. if (!IS_ALIGNED(axi_addr, region_size) ||
  946. !IS_ALIGNED(pci_addr, region_size)) {
  947. dev_err(dev,
  948. "axi %pap or pci %pap not aligned\n",
  949. &axi_addr, &pci_addr);
  950. return -EINVAL;
  951. }
  952. /* Match found! Program IARR and all IMAP windows. */
  953. ret = iproc_pcie_ib_write(pcie, region_idx, size_idx,
  954. ib_map->nr_windows, axi_addr,
  955. pci_addr, size);
  956. if (ret)
  957. goto err_ib;
  958. else
  959. return 0;
  960. }
  961. }
  962. ret = -EINVAL;
  963. err_ib:
  964. dev_err(dev, "unable to configure inbound mapping\n");
  965. dev_err(dev, "axi %pap, pci %pap, res size %pap\n",
  966. &axi_addr, &pci_addr, &size);
  967. return ret;
  968. }
  969. static int iproc_pcie_map_dma_ranges(struct iproc_pcie *pcie)
  970. {
  971. struct of_pci_range range;
  972. struct of_pci_range_parser parser;
  973. int ret;
  974. /* Get the dma-ranges from DT */
  975. ret = of_pci_dma_range_parser_init(&parser, pcie->dev->of_node);
  976. if (ret)
  977. return ret;
  978. for_each_of_pci_range(&parser, &range) {
  979. /* Each range entry corresponds to an inbound mapping region */
  980. ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_MEM);
  981. if (ret)
  982. return ret;
  983. }
  984. return 0;
  985. }
  986. static int iproce_pcie_get_msi(struct iproc_pcie *pcie,
  987. struct device_node *msi_node,
  988. u64 *msi_addr)
  989. {
  990. struct device *dev = pcie->dev;
  991. int ret;
  992. struct resource res;
  993. /*
  994. * Check if 'msi-map' points to ARM GICv3 ITS, which is the only
  995. * supported external MSI controller that requires steering.
  996. */
  997. if (!of_device_is_compatible(msi_node, "arm,gic-v3-its")) {
  998. dev_err(dev, "unable to find compatible MSI controller\n");
  999. return -ENODEV;
  1000. }
  1001. /* derive GITS_TRANSLATER address from GICv3 */
  1002. ret = of_address_to_resource(msi_node, 0, &res);
  1003. if (ret < 0) {
  1004. dev_err(dev, "unable to obtain MSI controller resources\n");
  1005. return ret;
  1006. }
  1007. *msi_addr = res.start + GITS_TRANSLATER;
  1008. return 0;
  1009. }
  1010. static int iproc_pcie_paxb_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr)
  1011. {
  1012. int ret;
  1013. struct of_pci_range range;
  1014. memset(&range, 0, sizeof(range));
  1015. range.size = SZ_32K;
  1016. range.pci_addr = range.cpu_addr = msi_addr & ~(range.size - 1);
  1017. ret = iproc_pcie_setup_ib(pcie, &range, IPROC_PCIE_IB_MAP_IO);
  1018. return ret;
  1019. }
  1020. static void iproc_pcie_paxc_v2_msi_steer(struct iproc_pcie *pcie, u64 msi_addr,
  1021. bool enable)
  1022. {
  1023. u32 val;
  1024. if (!enable) {
  1025. /*
  1026. * Disable PAXC MSI steering. All write transfers will be
  1027. * treated as non-MSI transfers
  1028. */
  1029. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
  1030. val &= ~MSI_ENABLE_CFG;
  1031. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
  1032. return;
  1033. }
  1034. /*
  1035. * Program bits [43:13] of address of GITS_TRANSLATER register into
  1036. * bits [30:0] of the MSI base address register. In fact, in all iProc
  1037. * based SoCs, all I/O register bases are well below the 32-bit
  1038. * boundary, so we can safely assume bits [43:32] are always zeros.
  1039. */
  1040. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_BASE_ADDR,
  1041. (u32)(msi_addr >> 13));
  1042. /* use a default 8K window size */
  1043. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_WINDOW_SIZE, 0);
  1044. /* steering MSI to GICv3 ITS */
  1045. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_GIC_MODE);
  1046. val |= GIC_V3_CFG;
  1047. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_GIC_MODE, val);
  1048. /*
  1049. * Program bits [43:2] of address of GITS_TRANSLATER register into the
  1050. * iProc MSI address registers.
  1051. */
  1052. msi_addr >>= 2;
  1053. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_HI,
  1054. upper_32_bits(msi_addr));
  1055. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_ADDR_LO,
  1056. lower_32_bits(msi_addr));
  1057. /* enable MSI */
  1058. val = iproc_pcie_read_reg(pcie, IPROC_PCIE_MSI_EN_CFG);
  1059. val |= MSI_ENABLE_CFG;
  1060. iproc_pcie_write_reg(pcie, IPROC_PCIE_MSI_EN_CFG, val);
  1061. }
  1062. static int iproc_pcie_msi_steer(struct iproc_pcie *pcie,
  1063. struct device_node *msi_node)
  1064. {
  1065. struct device *dev = pcie->dev;
  1066. int ret;
  1067. u64 msi_addr;
  1068. ret = iproce_pcie_get_msi(pcie, msi_node, &msi_addr);
  1069. if (ret < 0) {
  1070. dev_err(dev, "msi steering failed\n");
  1071. return ret;
  1072. }
  1073. switch (pcie->type) {
  1074. case IPROC_PCIE_PAXB_V2:
  1075. ret = iproc_pcie_paxb_v2_msi_steer(pcie, msi_addr);
  1076. if (ret)
  1077. return ret;
  1078. break;
  1079. case IPROC_PCIE_PAXC_V2:
  1080. iproc_pcie_paxc_v2_msi_steer(pcie, msi_addr, true);
  1081. break;
  1082. default:
  1083. return -EINVAL;
  1084. }
  1085. return 0;
  1086. }
  1087. static int iproc_pcie_msi_enable(struct iproc_pcie *pcie)
  1088. {
  1089. struct device_node *msi_node;
  1090. int ret;
  1091. /*
  1092. * Either the "msi-parent" or the "msi-map" phandle needs to exist
  1093. * for us to obtain the MSI node.
  1094. */
  1095. msi_node = of_parse_phandle(pcie->dev->of_node, "msi-parent", 0);
  1096. if (!msi_node) {
  1097. const __be32 *msi_map = NULL;
  1098. int len;
  1099. u32 phandle;
  1100. msi_map = of_get_property(pcie->dev->of_node, "msi-map", &len);
  1101. if (!msi_map)
  1102. return -ENODEV;
  1103. phandle = be32_to_cpup(msi_map + 1);
  1104. msi_node = of_find_node_by_phandle(phandle);
  1105. if (!msi_node)
  1106. return -ENODEV;
  1107. }
  1108. /*
  1109. * Certain revisions of the iProc PCIe controller require additional
  1110. * configurations to steer the MSI writes towards an external MSI
  1111. * controller.
  1112. */
  1113. if (pcie->need_msi_steer) {
  1114. ret = iproc_pcie_msi_steer(pcie, msi_node);
  1115. if (ret)
  1116. return ret;
  1117. }
  1118. /*
  1119. * If another MSI controller is being used, the call below should fail
  1120. * but that is okay
  1121. */
  1122. return iproc_msi_init(pcie, msi_node);
  1123. }
  1124. static void iproc_pcie_msi_disable(struct iproc_pcie *pcie)
  1125. {
  1126. iproc_msi_exit(pcie);
  1127. }
  1128. static int iproc_pcie_rev_init(struct iproc_pcie *pcie)
  1129. {
  1130. struct device *dev = pcie->dev;
  1131. unsigned int reg_idx;
  1132. const u16 *regs;
  1133. switch (pcie->type) {
  1134. case IPROC_PCIE_PAXB_BCMA:
  1135. regs = iproc_pcie_reg_paxb_bcma;
  1136. break;
  1137. case IPROC_PCIE_PAXB:
  1138. regs = iproc_pcie_reg_paxb;
  1139. pcie->has_apb_err_disable = true;
  1140. if (pcie->need_ob_cfg) {
  1141. pcie->ob_map = paxb_ob_map;
  1142. pcie->ob.nr_windows = ARRAY_SIZE(paxb_ob_map);
  1143. }
  1144. break;
  1145. case IPROC_PCIE_PAXB_V2:
  1146. regs = iproc_pcie_reg_paxb_v2;
  1147. pcie->has_apb_err_disable = true;
  1148. if (pcie->need_ob_cfg) {
  1149. pcie->ob_map = paxb_v2_ob_map;
  1150. pcie->ob.nr_windows = ARRAY_SIZE(paxb_v2_ob_map);
  1151. }
  1152. pcie->ib.nr_regions = ARRAY_SIZE(paxb_v2_ib_map);
  1153. pcie->ib_map = paxb_v2_ib_map;
  1154. pcie->need_msi_steer = true;
  1155. dev_warn(dev, "reads of config registers that contain %#x return incorrect data\n",
  1156. CFG_RETRY_STATUS);
  1157. break;
  1158. case IPROC_PCIE_PAXC:
  1159. regs = iproc_pcie_reg_paxc;
  1160. pcie->ep_is_internal = true;
  1161. break;
  1162. case IPROC_PCIE_PAXC_V2:
  1163. regs = iproc_pcie_reg_paxc_v2;
  1164. pcie->ep_is_internal = true;
  1165. pcie->need_msi_steer = true;
  1166. break;
  1167. default:
  1168. dev_err(dev, "incompatible iProc PCIe interface\n");
  1169. return -EINVAL;
  1170. }
  1171. pcie->reg_offsets = devm_kcalloc(dev, IPROC_PCIE_MAX_NUM_REG,
  1172. sizeof(*pcie->reg_offsets),
  1173. GFP_KERNEL);
  1174. if (!pcie->reg_offsets)
  1175. return -ENOMEM;
  1176. /* go through the register table and populate all valid registers */
  1177. pcie->reg_offsets[0] = (pcie->type == IPROC_PCIE_PAXC_V2) ?
  1178. IPROC_PCIE_REG_INVALID : regs[0];
  1179. for (reg_idx = 1; reg_idx < IPROC_PCIE_MAX_NUM_REG; reg_idx++)
  1180. pcie->reg_offsets[reg_idx] = regs[reg_idx] ?
  1181. regs[reg_idx] : IPROC_PCIE_REG_INVALID;
  1182. return 0;
  1183. }
  1184. int iproc_pcie_setup(struct iproc_pcie *pcie, struct list_head *res)
  1185. {
  1186. struct device *dev;
  1187. int ret;
  1188. struct pci_bus *child;
  1189. struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
  1190. dev = pcie->dev;
  1191. ret = iproc_pcie_rev_init(pcie);
  1192. if (ret) {
  1193. dev_err(dev, "unable to initialize controller parameters\n");
  1194. return ret;
  1195. }
  1196. ret = devm_request_pci_bus_resources(dev, res);
  1197. if (ret)
  1198. return ret;
  1199. ret = phy_init(pcie->phy);
  1200. if (ret) {
  1201. dev_err(dev, "unable to initialize PCIe PHY\n");
  1202. return ret;
  1203. }
  1204. ret = phy_power_on(pcie->phy);
  1205. if (ret) {
  1206. dev_err(dev, "unable to power on PCIe PHY\n");
  1207. goto err_exit_phy;
  1208. }
  1209. iproc_pcie_perst_ctrl(pcie, true);
  1210. iproc_pcie_perst_ctrl(pcie, false);
  1211. if (pcie->need_ob_cfg) {
  1212. ret = iproc_pcie_map_ranges(pcie, res);
  1213. if (ret) {
  1214. dev_err(dev, "map failed\n");
  1215. goto err_power_off_phy;
  1216. }
  1217. }
  1218. if (pcie->need_ib_cfg) {
  1219. ret = iproc_pcie_map_dma_ranges(pcie);
  1220. if (ret && ret != -ENOENT)
  1221. goto err_power_off_phy;
  1222. }
  1223. ret = iproc_pcie_check_link(pcie);
  1224. if (ret) {
  1225. dev_err(dev, "no PCIe EP device detected\n");
  1226. goto err_power_off_phy;
  1227. }
  1228. iproc_pcie_enable(pcie);
  1229. if (IS_ENABLED(CONFIG_PCI_MSI))
  1230. if (iproc_pcie_msi_enable(pcie))
  1231. dev_info(dev, "not using iProc MSI\n");
  1232. list_splice_init(res, &host->windows);
  1233. host->busnr = 0;
  1234. host->dev.parent = dev;
  1235. host->ops = &iproc_pcie_ops;
  1236. host->sysdata = pcie;
  1237. host->map_irq = pcie->map_irq;
  1238. host->swizzle_irq = pci_common_swizzle;
  1239. ret = pci_scan_root_bus_bridge(host);
  1240. if (ret < 0) {
  1241. dev_err(dev, "failed to scan host: %d\n", ret);
  1242. goto err_power_off_phy;
  1243. }
  1244. pci_assign_unassigned_bus_resources(host->bus);
  1245. pcie->root_bus = host->bus;
  1246. list_for_each_entry(child, &host->bus->children, node)
  1247. pcie_bus_configure_settings(child);
  1248. pci_bus_add_devices(host->bus);
  1249. return 0;
  1250. err_power_off_phy:
  1251. phy_power_off(pcie->phy);
  1252. err_exit_phy:
  1253. phy_exit(pcie->phy);
  1254. return ret;
  1255. }
  1256. EXPORT_SYMBOL(iproc_pcie_setup);
  1257. int iproc_pcie_remove(struct iproc_pcie *pcie)
  1258. {
  1259. pci_stop_root_bus(pcie->root_bus);
  1260. pci_remove_root_bus(pcie->root_bus);
  1261. iproc_pcie_msi_disable(pcie);
  1262. phy_power_off(pcie->phy);
  1263. phy_exit(pcie->phy);
  1264. return 0;
  1265. }
  1266. EXPORT_SYMBOL(iproc_pcie_remove);
  1267. /*
  1268. * The MSI parsing logic in certain revisions of Broadcom PAXC based root
  1269. * complex does not work and needs to be disabled
  1270. */
  1271. static void quirk_paxc_disable_msi_parsing(struct pci_dev *pdev)
  1272. {
  1273. struct iproc_pcie *pcie = iproc_data(pdev->bus);
  1274. if (pdev->hdr_type == PCI_HEADER_TYPE_BRIDGE)
  1275. iproc_pcie_paxc_v2_msi_steer(pcie, 0, false);
  1276. }
  1277. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0x16f0,
  1278. quirk_paxc_disable_msi_parsing);
  1279. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd802,
  1280. quirk_paxc_disable_msi_parsing);
  1281. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_BROADCOM, 0xd804,
  1282. quirk_paxc_disable_msi_parsing);
  1283. MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
  1284. MODULE_DESCRIPTION("Broadcom iPROC PCIe common driver");
  1285. MODULE_LICENSE("GPL v2");