pcie-iproc.c 41 KB

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