pcie-iproc.c 36 KB

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