pcie-iproc.c 40 KB

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