pcie-iproc.c 41 KB

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