pcie-octeon.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2007, 2008, 2009, 2010, 2011 Cavium Networks
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/pci.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/time.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <asm/octeon/octeon.h>
  16. #include <asm/octeon/cvmx-npei-defs.h>
  17. #include <asm/octeon/cvmx-pciercx-defs.h>
  18. #include <asm/octeon/cvmx-pescx-defs.h>
  19. #include <asm/octeon/cvmx-pexp-defs.h>
  20. #include <asm/octeon/cvmx-pemx-defs.h>
  21. #include <asm/octeon/cvmx-dpi-defs.h>
  22. #include <asm/octeon/cvmx-sli-defs.h>
  23. #include <asm/octeon/cvmx-sriox-defs.h>
  24. #include <asm/octeon/cvmx-helper-errata.h>
  25. #include <asm/octeon/pci-octeon.h>
  26. #define MRRS_CN5XXX 0 /* 128 byte Max Read Request Size */
  27. #define MPS_CN5XXX 0 /* 128 byte Max Packet Size (Limit of most PCs) */
  28. #define MRRS_CN6XXX 3 /* 1024 byte Max Read Request Size */
  29. #define MPS_CN6XXX 0 /* 128 byte Max Packet Size (Limit of most PCs) */
  30. /* Module parameter to disable PCI probing */
  31. static int pcie_disable;
  32. module_param(pcie_disable, int, S_IRUGO);
  33. static int enable_pcie_14459_war;
  34. static int enable_pcie_bus_num_war[2];
  35. union cvmx_pcie_address {
  36. uint64_t u64;
  37. struct {
  38. uint64_t upper:2; /* Normally 2 for XKPHYS */
  39. uint64_t reserved_49_61:13; /* Must be zero */
  40. uint64_t io:1; /* 1 for IO space access */
  41. uint64_t did:5; /* PCIe DID = 3 */
  42. uint64_t subdid:3; /* PCIe SubDID = 1 */
  43. uint64_t reserved_36_39:4; /* Must be zero */
  44. uint64_t es:2; /* Endian swap = 1 */
  45. uint64_t port:2; /* PCIe port 0,1 */
  46. uint64_t reserved_29_31:3; /* Must be zero */
  47. /*
  48. * Selects the type of the configuration request (0 = type 0,
  49. * 1 = type 1).
  50. */
  51. uint64_t ty:1;
  52. /* Target bus number sent in the ID in the request. */
  53. uint64_t bus:8;
  54. /*
  55. * Target device number sent in the ID in the
  56. * request. Note that Dev must be zero for type 0
  57. * configuration requests.
  58. */
  59. uint64_t dev:5;
  60. /* Target function number sent in the ID in the request. */
  61. uint64_t func:3;
  62. /*
  63. * Selects a register in the configuration space of
  64. * the target.
  65. */
  66. uint64_t reg:12;
  67. } config;
  68. struct {
  69. uint64_t upper:2; /* Normally 2 for XKPHYS */
  70. uint64_t reserved_49_61:13; /* Must be zero */
  71. uint64_t io:1; /* 1 for IO space access */
  72. uint64_t did:5; /* PCIe DID = 3 */
  73. uint64_t subdid:3; /* PCIe SubDID = 2 */
  74. uint64_t reserved_36_39:4; /* Must be zero */
  75. uint64_t es:2; /* Endian swap = 1 */
  76. uint64_t port:2; /* PCIe port 0,1 */
  77. uint64_t address:32; /* PCIe IO address */
  78. } io;
  79. struct {
  80. uint64_t upper:2; /* Normally 2 for XKPHYS */
  81. uint64_t reserved_49_61:13; /* Must be zero */
  82. uint64_t io:1; /* 1 for IO space access */
  83. uint64_t did:5; /* PCIe DID = 3 */
  84. uint64_t subdid:3; /* PCIe SubDID = 3-6 */
  85. uint64_t reserved_36_39:4; /* Must be zero */
  86. uint64_t address:36; /* PCIe Mem address */
  87. } mem;
  88. };
  89. static int cvmx_pcie_rc_initialize(int pcie_port);
  90. #include <dma-coherence.h>
  91. /**
  92. * Return the Core virtual base address for PCIe IO access. IOs are
  93. * read/written as an offset from this address.
  94. *
  95. * @pcie_port: PCIe port the IO is for
  96. *
  97. * Returns 64bit Octeon IO base address for read/write
  98. */
  99. static inline uint64_t cvmx_pcie_get_io_base_address(int pcie_port)
  100. {
  101. union cvmx_pcie_address pcie_addr;
  102. pcie_addr.u64 = 0;
  103. pcie_addr.io.upper = 0;
  104. pcie_addr.io.io = 1;
  105. pcie_addr.io.did = 3;
  106. pcie_addr.io.subdid = 2;
  107. pcie_addr.io.es = 1;
  108. pcie_addr.io.port = pcie_port;
  109. return pcie_addr.u64;
  110. }
  111. /**
  112. * Size of the IO address region returned at address
  113. * cvmx_pcie_get_io_base_address()
  114. *
  115. * @pcie_port: PCIe port the IO is for
  116. *
  117. * Returns Size of the IO window
  118. */
  119. static inline uint64_t cvmx_pcie_get_io_size(int pcie_port)
  120. {
  121. return 1ull << 32;
  122. }
  123. /**
  124. * Return the Core virtual base address for PCIe MEM access. Memory is
  125. * read/written as an offset from this address.
  126. *
  127. * @pcie_port: PCIe port the IO is for
  128. *
  129. * Returns 64bit Octeon IO base address for read/write
  130. */
  131. static inline uint64_t cvmx_pcie_get_mem_base_address(int pcie_port)
  132. {
  133. union cvmx_pcie_address pcie_addr;
  134. pcie_addr.u64 = 0;
  135. pcie_addr.mem.upper = 0;
  136. pcie_addr.mem.io = 1;
  137. pcie_addr.mem.did = 3;
  138. pcie_addr.mem.subdid = 3 + pcie_port;
  139. return pcie_addr.u64;
  140. }
  141. /**
  142. * Size of the Mem address region returned at address
  143. * cvmx_pcie_get_mem_base_address()
  144. *
  145. * @pcie_port: PCIe port the IO is for
  146. *
  147. * Returns Size of the Mem window
  148. */
  149. static inline uint64_t cvmx_pcie_get_mem_size(int pcie_port)
  150. {
  151. return 1ull << 36;
  152. }
  153. /**
  154. * Read a PCIe config space register indirectly. This is used for
  155. * registers of the form PCIEEP_CFG??? and PCIERC?_CFG???.
  156. *
  157. * @pcie_port: PCIe port to read from
  158. * @cfg_offset: Address to read
  159. *
  160. * Returns Value read
  161. */
  162. static uint32_t cvmx_pcie_cfgx_read(int pcie_port, uint32_t cfg_offset)
  163. {
  164. if (octeon_has_feature(OCTEON_FEATURE_NPEI)) {
  165. union cvmx_pescx_cfg_rd pescx_cfg_rd;
  166. pescx_cfg_rd.u64 = 0;
  167. pescx_cfg_rd.s.addr = cfg_offset;
  168. cvmx_write_csr(CVMX_PESCX_CFG_RD(pcie_port), pescx_cfg_rd.u64);
  169. pescx_cfg_rd.u64 = cvmx_read_csr(CVMX_PESCX_CFG_RD(pcie_port));
  170. return pescx_cfg_rd.s.data;
  171. } else {
  172. union cvmx_pemx_cfg_rd pemx_cfg_rd;
  173. pemx_cfg_rd.u64 = 0;
  174. pemx_cfg_rd.s.addr = cfg_offset;
  175. cvmx_write_csr(CVMX_PEMX_CFG_RD(pcie_port), pemx_cfg_rd.u64);
  176. pemx_cfg_rd.u64 = cvmx_read_csr(CVMX_PEMX_CFG_RD(pcie_port));
  177. return pemx_cfg_rd.s.data;
  178. }
  179. }
  180. /**
  181. * Write a PCIe config space register indirectly. This is used for
  182. * registers of the form PCIEEP_CFG??? and PCIERC?_CFG???.
  183. *
  184. * @pcie_port: PCIe port to write to
  185. * @cfg_offset: Address to write
  186. * @val: Value to write
  187. */
  188. static void cvmx_pcie_cfgx_write(int pcie_port, uint32_t cfg_offset,
  189. uint32_t val)
  190. {
  191. if (octeon_has_feature(OCTEON_FEATURE_NPEI)) {
  192. union cvmx_pescx_cfg_wr pescx_cfg_wr;
  193. pescx_cfg_wr.u64 = 0;
  194. pescx_cfg_wr.s.addr = cfg_offset;
  195. pescx_cfg_wr.s.data = val;
  196. cvmx_write_csr(CVMX_PESCX_CFG_WR(pcie_port), pescx_cfg_wr.u64);
  197. } else {
  198. union cvmx_pemx_cfg_wr pemx_cfg_wr;
  199. pemx_cfg_wr.u64 = 0;
  200. pemx_cfg_wr.s.addr = cfg_offset;
  201. pemx_cfg_wr.s.data = val;
  202. cvmx_write_csr(CVMX_PEMX_CFG_WR(pcie_port), pemx_cfg_wr.u64);
  203. }
  204. }
  205. /**
  206. * Build a PCIe config space request address for a device
  207. *
  208. * @pcie_port: PCIe port to access
  209. * @bus: Sub bus
  210. * @dev: Device ID
  211. * @fn: Device sub function
  212. * @reg: Register to access
  213. *
  214. * Returns 64bit Octeon IO address
  215. */
  216. static inline uint64_t __cvmx_pcie_build_config_addr(int pcie_port, int bus,
  217. int dev, int fn, int reg)
  218. {
  219. union cvmx_pcie_address pcie_addr;
  220. union cvmx_pciercx_cfg006 pciercx_cfg006;
  221. pciercx_cfg006.u32 =
  222. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG006(pcie_port));
  223. if ((bus <= pciercx_cfg006.s.pbnum) && (dev != 0))
  224. return 0;
  225. pcie_addr.u64 = 0;
  226. pcie_addr.config.upper = 2;
  227. pcie_addr.config.io = 1;
  228. pcie_addr.config.did = 3;
  229. pcie_addr.config.subdid = 1;
  230. pcie_addr.config.es = 1;
  231. pcie_addr.config.port = pcie_port;
  232. pcie_addr.config.ty = (bus > pciercx_cfg006.s.pbnum);
  233. pcie_addr.config.bus = bus;
  234. pcie_addr.config.dev = dev;
  235. pcie_addr.config.func = fn;
  236. pcie_addr.config.reg = reg;
  237. return pcie_addr.u64;
  238. }
  239. /**
  240. * Read 8bits from a Device's config space
  241. *
  242. * @pcie_port: PCIe port the device is on
  243. * @bus: Sub bus
  244. * @dev: Device ID
  245. * @fn: Device sub function
  246. * @reg: Register to access
  247. *
  248. * Returns Result of the read
  249. */
  250. static uint8_t cvmx_pcie_config_read8(int pcie_port, int bus, int dev,
  251. int fn, int reg)
  252. {
  253. uint64_t address =
  254. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  255. if (address)
  256. return cvmx_read64_uint8(address);
  257. else
  258. return 0xff;
  259. }
  260. /**
  261. * Read 16bits from a Device's config space
  262. *
  263. * @pcie_port: PCIe port the device is on
  264. * @bus: Sub bus
  265. * @dev: Device ID
  266. * @fn: Device sub function
  267. * @reg: Register to access
  268. *
  269. * Returns Result of the read
  270. */
  271. static uint16_t cvmx_pcie_config_read16(int pcie_port, int bus, int dev,
  272. int fn, int reg)
  273. {
  274. uint64_t address =
  275. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  276. if (address)
  277. return le16_to_cpu(cvmx_read64_uint16(address));
  278. else
  279. return 0xffff;
  280. }
  281. /**
  282. * Read 32bits from a Device's config space
  283. *
  284. * @pcie_port: PCIe port the device is on
  285. * @bus: Sub bus
  286. * @dev: Device ID
  287. * @fn: Device sub function
  288. * @reg: Register to access
  289. *
  290. * Returns Result of the read
  291. */
  292. static uint32_t cvmx_pcie_config_read32(int pcie_port, int bus, int dev,
  293. int fn, int reg)
  294. {
  295. uint64_t address =
  296. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  297. if (address)
  298. return le32_to_cpu(cvmx_read64_uint32(address));
  299. else
  300. return 0xffffffff;
  301. }
  302. /**
  303. * Write 8bits to a Device's config space
  304. *
  305. * @pcie_port: PCIe port the device is on
  306. * @bus: Sub bus
  307. * @dev: Device ID
  308. * @fn: Device sub function
  309. * @reg: Register to access
  310. * @val: Value to write
  311. */
  312. static void cvmx_pcie_config_write8(int pcie_port, int bus, int dev, int fn,
  313. int reg, uint8_t val)
  314. {
  315. uint64_t address =
  316. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  317. if (address)
  318. cvmx_write64_uint8(address, val);
  319. }
  320. /**
  321. * Write 16bits to a Device's config space
  322. *
  323. * @pcie_port: PCIe port the device is on
  324. * @bus: Sub bus
  325. * @dev: Device ID
  326. * @fn: Device sub function
  327. * @reg: Register to access
  328. * @val: Value to write
  329. */
  330. static void cvmx_pcie_config_write16(int pcie_port, int bus, int dev, int fn,
  331. int reg, uint16_t val)
  332. {
  333. uint64_t address =
  334. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  335. if (address)
  336. cvmx_write64_uint16(address, cpu_to_le16(val));
  337. }
  338. /**
  339. * Write 32bits to a Device's config space
  340. *
  341. * @pcie_port: PCIe port the device is on
  342. * @bus: Sub bus
  343. * @dev: Device ID
  344. * @fn: Device sub function
  345. * @reg: Register to access
  346. * @val: Value to write
  347. */
  348. static void cvmx_pcie_config_write32(int pcie_port, int bus, int dev, int fn,
  349. int reg, uint32_t val)
  350. {
  351. uint64_t address =
  352. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  353. if (address)
  354. cvmx_write64_uint32(address, cpu_to_le32(val));
  355. }
  356. /**
  357. * Initialize the RC config space CSRs
  358. *
  359. * @pcie_port: PCIe port to initialize
  360. */
  361. static void __cvmx_pcie_rc_initialize_config_space(int pcie_port)
  362. {
  363. union cvmx_pciercx_cfg030 pciercx_cfg030;
  364. union cvmx_pciercx_cfg070 pciercx_cfg070;
  365. union cvmx_pciercx_cfg001 pciercx_cfg001;
  366. union cvmx_pciercx_cfg032 pciercx_cfg032;
  367. union cvmx_pciercx_cfg006 pciercx_cfg006;
  368. union cvmx_pciercx_cfg008 pciercx_cfg008;
  369. union cvmx_pciercx_cfg009 pciercx_cfg009;
  370. union cvmx_pciercx_cfg010 pciercx_cfg010;
  371. union cvmx_pciercx_cfg011 pciercx_cfg011;
  372. union cvmx_pciercx_cfg035 pciercx_cfg035;
  373. union cvmx_pciercx_cfg075 pciercx_cfg075;
  374. union cvmx_pciercx_cfg034 pciercx_cfg034;
  375. /* Max Payload Size (PCIE*_CFG030[MPS]) */
  376. /* Max Read Request Size (PCIE*_CFG030[MRRS]) */
  377. /* Relaxed-order, no-snoop enables (PCIE*_CFG030[RO_EN,NS_EN] */
  378. /* Error Message Enables (PCIE*_CFG030[CE_EN,NFE_EN,FE_EN,UR_EN]) */
  379. pciercx_cfg030.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG030(pcie_port));
  380. if (OCTEON_IS_MODEL(OCTEON_CN5XXX)) {
  381. pciercx_cfg030.s.mps = MPS_CN5XXX;
  382. pciercx_cfg030.s.mrrs = MRRS_CN5XXX;
  383. } else {
  384. pciercx_cfg030.s.mps = MPS_CN6XXX;
  385. pciercx_cfg030.s.mrrs = MRRS_CN6XXX;
  386. }
  387. /*
  388. * Enable relaxed order processing. This will allow devices to
  389. * affect read response ordering.
  390. */
  391. pciercx_cfg030.s.ro_en = 1;
  392. /* Enable no snoop processing. Not used by Octeon */
  393. pciercx_cfg030.s.ns_en = 1;
  394. /* Correctable error reporting enable. */
  395. pciercx_cfg030.s.ce_en = 1;
  396. /* Non-fatal error reporting enable. */
  397. pciercx_cfg030.s.nfe_en = 1;
  398. /* Fatal error reporting enable. */
  399. pciercx_cfg030.s.fe_en = 1;
  400. /* Unsupported request reporting enable. */
  401. pciercx_cfg030.s.ur_en = 1;
  402. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG030(pcie_port), pciercx_cfg030.u32);
  403. if (octeon_has_feature(OCTEON_FEATURE_NPEI)) {
  404. union cvmx_npei_ctl_status2 npei_ctl_status2;
  405. /*
  406. * Max Payload Size (NPEI_CTL_STATUS2[MPS]) must match
  407. * PCIE*_CFG030[MPS]. Max Read Request Size
  408. * (NPEI_CTL_STATUS2[MRRS]) must not exceed
  409. * PCIE*_CFG030[MRRS]
  410. */
  411. npei_ctl_status2.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_STATUS2);
  412. /* Max payload size = 128 bytes for best Octeon DMA performance */
  413. npei_ctl_status2.s.mps = MPS_CN5XXX;
  414. /* Max read request size = 128 bytes for best Octeon DMA performance */
  415. npei_ctl_status2.s.mrrs = MRRS_CN5XXX;
  416. if (pcie_port)
  417. npei_ctl_status2.s.c1_b1_s = 3; /* Port1 BAR1 Size 256MB */
  418. else
  419. npei_ctl_status2.s.c0_b1_s = 3; /* Port0 BAR1 Size 256MB */
  420. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_STATUS2, npei_ctl_status2.u64);
  421. } else {
  422. /*
  423. * Max Payload Size (DPI_SLI_PRTX_CFG[MPS]) must match
  424. * PCIE*_CFG030[MPS]. Max Read Request Size
  425. * (DPI_SLI_PRTX_CFG[MRRS]) must not exceed
  426. * PCIE*_CFG030[MRRS].
  427. */
  428. union cvmx_dpi_sli_prtx_cfg prt_cfg;
  429. union cvmx_sli_s2m_portx_ctl sli_s2m_portx_ctl;
  430. prt_cfg.u64 = cvmx_read_csr(CVMX_DPI_SLI_PRTX_CFG(pcie_port));
  431. prt_cfg.s.mps = MPS_CN6XXX;
  432. prt_cfg.s.mrrs = MRRS_CN6XXX;
  433. /* Max outstanding load request. */
  434. prt_cfg.s.molr = 32;
  435. cvmx_write_csr(CVMX_DPI_SLI_PRTX_CFG(pcie_port), prt_cfg.u64);
  436. sli_s2m_portx_ctl.u64 = cvmx_read_csr(CVMX_PEXP_SLI_S2M_PORTX_CTL(pcie_port));
  437. sli_s2m_portx_ctl.s.mrrs = MRRS_CN6XXX;
  438. cvmx_write_csr(CVMX_PEXP_SLI_S2M_PORTX_CTL(pcie_port), sli_s2m_portx_ctl.u64);
  439. }
  440. /* ECRC Generation (PCIE*_CFG070[GE,CE]) */
  441. pciercx_cfg070.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG070(pcie_port));
  442. pciercx_cfg070.s.ge = 1; /* ECRC generation enable. */
  443. pciercx_cfg070.s.ce = 1; /* ECRC check enable. */
  444. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG070(pcie_port), pciercx_cfg070.u32);
  445. /*
  446. * Access Enables (PCIE*_CFG001[MSAE,ME])
  447. * ME and MSAE should always be set.
  448. * Interrupt Disable (PCIE*_CFG001[I_DIS])
  449. * System Error Message Enable (PCIE*_CFG001[SEE])
  450. */
  451. pciercx_cfg001.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG001(pcie_port));
  452. pciercx_cfg001.s.msae = 1; /* Memory space enable. */
  453. pciercx_cfg001.s.me = 1; /* Bus master enable. */
  454. pciercx_cfg001.s.i_dis = 1; /* INTx assertion disable. */
  455. pciercx_cfg001.s.see = 1; /* SERR# enable */
  456. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG001(pcie_port), pciercx_cfg001.u32);
  457. /* Advanced Error Recovery Message Enables */
  458. /* (PCIE*_CFG066,PCIE*_CFG067,PCIE*_CFG069) */
  459. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG066(pcie_port), 0);
  460. /* Use CVMX_PCIERCX_CFG067 hardware default */
  461. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG069(pcie_port), 0);
  462. /* Active State Power Management (PCIE*_CFG032[ASLPC]) */
  463. pciercx_cfg032.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG032(pcie_port));
  464. pciercx_cfg032.s.aslpc = 0; /* Active state Link PM control. */
  465. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG032(pcie_port), pciercx_cfg032.u32);
  466. /*
  467. * Link Width Mode (PCIERCn_CFG452[LME]) - Set during
  468. * cvmx_pcie_rc_initialize_link()
  469. *
  470. * Primary Bus Number (PCIERCn_CFG006[PBNUM])
  471. *
  472. * We set the primary bus number to 1 so IDT bridges are
  473. * happy. They don't like zero.
  474. */
  475. pciercx_cfg006.u32 = 0;
  476. pciercx_cfg006.s.pbnum = 1;
  477. pciercx_cfg006.s.sbnum = 1;
  478. pciercx_cfg006.s.subbnum = 1;
  479. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG006(pcie_port), pciercx_cfg006.u32);
  480. /*
  481. * Memory-mapped I/O BAR (PCIERCn_CFG008)
  482. * Most applications should disable the memory-mapped I/O BAR by
  483. * setting PCIERCn_CFG008[ML_ADDR] < PCIERCn_CFG008[MB_ADDR]
  484. */
  485. pciercx_cfg008.u32 = 0;
  486. pciercx_cfg008.s.mb_addr = 0x100;
  487. pciercx_cfg008.s.ml_addr = 0;
  488. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG008(pcie_port), pciercx_cfg008.u32);
  489. /*
  490. * Prefetchable BAR (PCIERCn_CFG009,PCIERCn_CFG010,PCIERCn_CFG011)
  491. * Most applications should disable the prefetchable BAR by setting
  492. * PCIERCn_CFG011[UMEM_LIMIT],PCIERCn_CFG009[LMEM_LIMIT] <
  493. * PCIERCn_CFG010[UMEM_BASE],PCIERCn_CFG009[LMEM_BASE]
  494. */
  495. pciercx_cfg009.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG009(pcie_port));
  496. pciercx_cfg010.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG010(pcie_port));
  497. pciercx_cfg011.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG011(pcie_port));
  498. pciercx_cfg009.s.lmem_base = 0x100;
  499. pciercx_cfg009.s.lmem_limit = 0;
  500. pciercx_cfg010.s.umem_base = 0x100;
  501. pciercx_cfg011.s.umem_limit = 0;
  502. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG009(pcie_port), pciercx_cfg009.u32);
  503. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG010(pcie_port), pciercx_cfg010.u32);
  504. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG011(pcie_port), pciercx_cfg011.u32);
  505. /*
  506. * System Error Interrupt Enables (PCIERCn_CFG035[SECEE,SEFEE,SENFEE])
  507. * PME Interrupt Enables (PCIERCn_CFG035[PMEIE])
  508. */
  509. pciercx_cfg035.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG035(pcie_port));
  510. pciercx_cfg035.s.secee = 1; /* System error on correctable error enable. */
  511. pciercx_cfg035.s.sefee = 1; /* System error on fatal error enable. */
  512. pciercx_cfg035.s.senfee = 1; /* System error on non-fatal error enable. */
  513. pciercx_cfg035.s.pmeie = 1; /* PME interrupt enable. */
  514. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG035(pcie_port), pciercx_cfg035.u32);
  515. /*
  516. * Advanced Error Recovery Interrupt Enables
  517. * (PCIERCn_CFG075[CERE,NFERE,FERE])
  518. */
  519. pciercx_cfg075.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG075(pcie_port));
  520. pciercx_cfg075.s.cere = 1; /* Correctable error reporting enable. */
  521. pciercx_cfg075.s.nfere = 1; /* Non-fatal error reporting enable. */
  522. pciercx_cfg075.s.fere = 1; /* Fatal error reporting enable. */
  523. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG075(pcie_port), pciercx_cfg075.u32);
  524. /*
  525. * HP Interrupt Enables (PCIERCn_CFG034[HPINT_EN],
  526. * PCIERCn_CFG034[DLLS_EN,CCINT_EN])
  527. */
  528. pciercx_cfg034.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG034(pcie_port));
  529. pciercx_cfg034.s.hpint_en = 1; /* Hot-plug interrupt enable. */
  530. pciercx_cfg034.s.dlls_en = 1; /* Data Link Layer state changed enable */
  531. pciercx_cfg034.s.ccint_en = 1; /* Command completed interrupt enable. */
  532. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG034(pcie_port), pciercx_cfg034.u32);
  533. }
  534. /**
  535. * Initialize a host mode PCIe gen 1 link. This function takes a PCIe
  536. * port from reset to a link up state. Software can then begin
  537. * configuring the rest of the link.
  538. *
  539. * @pcie_port: PCIe port to initialize
  540. *
  541. * Returns Zero on success
  542. */
  543. static int __cvmx_pcie_rc_initialize_link_gen1(int pcie_port)
  544. {
  545. uint64_t start_cycle;
  546. union cvmx_pescx_ctl_status pescx_ctl_status;
  547. union cvmx_pciercx_cfg452 pciercx_cfg452;
  548. union cvmx_pciercx_cfg032 pciercx_cfg032;
  549. union cvmx_pciercx_cfg448 pciercx_cfg448;
  550. /* Set the lane width */
  551. pciercx_cfg452.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG452(pcie_port));
  552. pescx_ctl_status.u64 = cvmx_read_csr(CVMX_PESCX_CTL_STATUS(pcie_port));
  553. if (pescx_ctl_status.s.qlm_cfg == 0)
  554. /* We're in 8 lane (56XX) or 4 lane (54XX) mode */
  555. pciercx_cfg452.s.lme = 0xf;
  556. else
  557. /* We're in 4 lane (56XX) or 2 lane (52XX) mode */
  558. pciercx_cfg452.s.lme = 0x7;
  559. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG452(pcie_port), pciercx_cfg452.u32);
  560. /*
  561. * CN52XX pass 1.x has an errata where length mismatches on UR
  562. * responses can cause bus errors on 64bit memory
  563. * reads. Turning off length error checking fixes this.
  564. */
  565. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  566. union cvmx_pciercx_cfg455 pciercx_cfg455;
  567. pciercx_cfg455.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG455(pcie_port));
  568. pciercx_cfg455.s.m_cpl_len_err = 1;
  569. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG455(pcie_port), pciercx_cfg455.u32);
  570. }
  571. /* Lane swap needs to be manually enabled for CN52XX */
  572. if (OCTEON_IS_MODEL(OCTEON_CN52XX) && (pcie_port == 1)) {
  573. pescx_ctl_status.s.lane_swp = 1;
  574. cvmx_write_csr(CVMX_PESCX_CTL_STATUS(pcie_port), pescx_ctl_status.u64);
  575. }
  576. /* Bring up the link */
  577. pescx_ctl_status.u64 = cvmx_read_csr(CVMX_PESCX_CTL_STATUS(pcie_port));
  578. pescx_ctl_status.s.lnk_enb = 1;
  579. cvmx_write_csr(CVMX_PESCX_CTL_STATUS(pcie_port), pescx_ctl_status.u64);
  580. /*
  581. * CN52XX pass 1.0: Due to a bug in 2nd order CDR, it needs to
  582. * be disabled.
  583. */
  584. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_0))
  585. __cvmx_helper_errata_qlm_disable_2nd_order_cdr(0);
  586. /* Wait for the link to come up */
  587. start_cycle = cvmx_get_cycle();
  588. do {
  589. if (cvmx_get_cycle() - start_cycle > 2 * octeon_get_clock_rate()) {
  590. cvmx_dprintf("PCIe: Port %d link timeout\n", pcie_port);
  591. return -1;
  592. }
  593. cvmx_wait(10000);
  594. pciercx_cfg032.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG032(pcie_port));
  595. } while (pciercx_cfg032.s.dlla == 0);
  596. /* Clear all pending errors */
  597. cvmx_write_csr(CVMX_PEXP_NPEI_INT_SUM, cvmx_read_csr(CVMX_PEXP_NPEI_INT_SUM));
  598. /*
  599. * Update the Replay Time Limit. Empirically, some PCIe
  600. * devices take a little longer to respond than expected under
  601. * load. As a workaround for this we configure the Replay Time
  602. * Limit to the value expected for a 512 byte MPS instead of
  603. * our actual 256 byte MPS. The numbers below are directly
  604. * from the PCIe spec table 3-4.
  605. */
  606. pciercx_cfg448.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG448(pcie_port));
  607. switch (pciercx_cfg032.s.nlw) {
  608. case 1: /* 1 lane */
  609. pciercx_cfg448.s.rtl = 1677;
  610. break;
  611. case 2: /* 2 lanes */
  612. pciercx_cfg448.s.rtl = 867;
  613. break;
  614. case 4: /* 4 lanes */
  615. pciercx_cfg448.s.rtl = 462;
  616. break;
  617. case 8: /* 8 lanes */
  618. pciercx_cfg448.s.rtl = 258;
  619. break;
  620. }
  621. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG448(pcie_port), pciercx_cfg448.u32);
  622. return 0;
  623. }
  624. static void __cvmx_increment_ba(union cvmx_sli_mem_access_subidx *pmas)
  625. {
  626. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  627. pmas->cn68xx.ba++;
  628. else
  629. pmas->cn63xx.ba++;
  630. }
  631. /**
  632. * Initialize a PCIe gen 1 port for use in host(RC) mode. It doesn't
  633. * enumerate the bus.
  634. *
  635. * @pcie_port: PCIe port to initialize
  636. *
  637. * Returns Zero on success
  638. */
  639. static int __cvmx_pcie_rc_initialize_gen1(int pcie_port)
  640. {
  641. int i;
  642. int base;
  643. u64 addr_swizzle;
  644. union cvmx_ciu_soft_prst ciu_soft_prst;
  645. union cvmx_pescx_bist_status pescx_bist_status;
  646. union cvmx_pescx_bist_status2 pescx_bist_status2;
  647. union cvmx_npei_ctl_status npei_ctl_status;
  648. union cvmx_npei_mem_access_ctl npei_mem_access_ctl;
  649. union cvmx_npei_mem_access_subidx mem_access_subid;
  650. union cvmx_npei_dbg_data npei_dbg_data;
  651. union cvmx_pescx_ctl_status2 pescx_ctl_status2;
  652. union cvmx_pciercx_cfg032 pciercx_cfg032;
  653. union cvmx_npei_bar1_indexx bar1_index;
  654. retry:
  655. /*
  656. * Make sure we aren't trying to setup a target mode interface
  657. * in host mode.
  658. */
  659. npei_ctl_status.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_STATUS);
  660. if ((pcie_port == 0) && !npei_ctl_status.s.host_mode) {
  661. cvmx_dprintf("PCIe: Port %d in endpoint mode\n", pcie_port);
  662. return -1;
  663. }
  664. /*
  665. * Make sure a CN52XX isn't trying to bring up port 1 when it
  666. * is disabled.
  667. */
  668. if (OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  669. npei_dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  670. if ((pcie_port == 1) && npei_dbg_data.cn52xx.qlm0_link_width) {
  671. cvmx_dprintf("PCIe: ERROR: cvmx_pcie_rc_initialize() called on port1, but port1 is disabled\n");
  672. return -1;
  673. }
  674. }
  675. /*
  676. * PCIe switch arbitration mode. '0' == fixed priority NPEI,
  677. * PCIe0, then PCIe1. '1' == round robin.
  678. */
  679. npei_ctl_status.s.arb = 1;
  680. /* Allow up to 0x20 config retries */
  681. npei_ctl_status.s.cfg_rtry = 0x20;
  682. /*
  683. * CN52XX pass1.x has an errata where P0_NTAGS and P1_NTAGS
  684. * don't reset.
  685. */
  686. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  687. npei_ctl_status.s.p0_ntags = 0x20;
  688. npei_ctl_status.s.p1_ntags = 0x20;
  689. }
  690. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_STATUS, npei_ctl_status.u64);
  691. /* Bring the PCIe out of reset */
  692. if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_EBH5200) {
  693. /*
  694. * The EBH5200 board swapped the PCIe reset lines on
  695. * the board. As a workaround for this bug, we bring
  696. * both PCIe ports out of reset at the same time
  697. * instead of on separate calls. So for port 0, we
  698. * bring both out of reset and do nothing on port 1
  699. */
  700. if (pcie_port == 0) {
  701. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  702. /*
  703. * After a chip reset the PCIe will also be in
  704. * reset. If it isn't, most likely someone is
  705. * trying to init it again without a proper
  706. * PCIe reset.
  707. */
  708. if (ciu_soft_prst.s.soft_prst == 0) {
  709. /* Reset the ports */
  710. ciu_soft_prst.s.soft_prst = 1;
  711. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  712. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  713. ciu_soft_prst.s.soft_prst = 1;
  714. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  715. /* Wait until pcie resets the ports. */
  716. udelay(2000);
  717. }
  718. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  719. ciu_soft_prst.s.soft_prst = 0;
  720. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  721. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  722. ciu_soft_prst.s.soft_prst = 0;
  723. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  724. }
  725. } else {
  726. /*
  727. * The normal case: The PCIe ports are completely
  728. * separate and can be brought out of reset
  729. * independently.
  730. */
  731. if (pcie_port)
  732. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  733. else
  734. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  735. /*
  736. * After a chip reset the PCIe will also be in
  737. * reset. If it isn't, most likely someone is trying
  738. * to init it again without a proper PCIe reset.
  739. */
  740. if (ciu_soft_prst.s.soft_prst == 0) {
  741. /* Reset the port */
  742. ciu_soft_prst.s.soft_prst = 1;
  743. if (pcie_port)
  744. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  745. else
  746. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  747. /* Wait until pcie resets the ports. */
  748. udelay(2000);
  749. }
  750. if (pcie_port) {
  751. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  752. ciu_soft_prst.s.soft_prst = 0;
  753. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  754. } else {
  755. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  756. ciu_soft_prst.s.soft_prst = 0;
  757. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  758. }
  759. }
  760. /*
  761. * Wait for PCIe reset to complete. Due to errata PCIE-700, we
  762. * don't poll PESCX_CTL_STATUS2[PCIERST], but simply wait a
  763. * fixed number of cycles.
  764. */
  765. cvmx_wait(400000);
  766. /*
  767. * PESCX_BIST_STATUS2[PCLK_RUN] was missing on pass 1 of
  768. * CN56XX and CN52XX, so we only probe it on newer chips
  769. */
  770. if (!OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X) && !OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  771. /* Clear PCLK_RUN so we can check if the clock is running */
  772. pescx_ctl_status2.u64 = cvmx_read_csr(CVMX_PESCX_CTL_STATUS2(pcie_port));
  773. pescx_ctl_status2.s.pclk_run = 1;
  774. cvmx_write_csr(CVMX_PESCX_CTL_STATUS2(pcie_port), pescx_ctl_status2.u64);
  775. /* Now that we cleared PCLK_RUN, wait for it to be set
  776. * again telling us the clock is running
  777. */
  778. if (CVMX_WAIT_FOR_FIELD64(CVMX_PESCX_CTL_STATUS2(pcie_port),
  779. union cvmx_pescx_ctl_status2, pclk_run, ==, 1, 10000)) {
  780. cvmx_dprintf("PCIe: Port %d isn't clocked, skipping.\n", pcie_port);
  781. return -1;
  782. }
  783. }
  784. /*
  785. * Check and make sure PCIe came out of reset. If it doesn't
  786. * the board probably hasn't wired the clocks up and the
  787. * interface should be skipped.
  788. */
  789. pescx_ctl_status2.u64 = cvmx_read_csr(CVMX_PESCX_CTL_STATUS2(pcie_port));
  790. if (pescx_ctl_status2.s.pcierst) {
  791. cvmx_dprintf("PCIe: Port %d stuck in reset, skipping.\n", pcie_port);
  792. return -1;
  793. }
  794. /*
  795. * Check BIST2 status. If any bits are set skip this
  796. * interface. This is an attempt to catch PCIE-813 on pass 1
  797. * parts.
  798. */
  799. pescx_bist_status2.u64 = cvmx_read_csr(CVMX_PESCX_BIST_STATUS2(pcie_port));
  800. if (pescx_bist_status2.u64) {
  801. cvmx_dprintf("PCIe: Port %d BIST2 failed. Most likely this port isn't hooked up, skipping.\n",
  802. pcie_port);
  803. return -1;
  804. }
  805. /* Check BIST status */
  806. pescx_bist_status.u64 = cvmx_read_csr(CVMX_PESCX_BIST_STATUS(pcie_port));
  807. if (pescx_bist_status.u64)
  808. cvmx_dprintf("PCIe: BIST FAILED for port %d (0x%016llx)\n",
  809. pcie_port, CAST64(pescx_bist_status.u64));
  810. /* Initialize the config space CSRs */
  811. __cvmx_pcie_rc_initialize_config_space(pcie_port);
  812. /* Bring the link up */
  813. if (__cvmx_pcie_rc_initialize_link_gen1(pcie_port)) {
  814. cvmx_dprintf("PCIe: Failed to initialize port %d, probably the slot is empty\n",
  815. pcie_port);
  816. return -1;
  817. }
  818. /* Store merge control (NPEI_MEM_ACCESS_CTL[TIMER,MAX_WORD]) */
  819. npei_mem_access_ctl.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_MEM_ACCESS_CTL);
  820. npei_mem_access_ctl.s.max_word = 0; /* Allow 16 words to combine */
  821. npei_mem_access_ctl.s.timer = 127; /* Wait up to 127 cycles for more data */
  822. cvmx_write_csr(CVMX_PEXP_NPEI_MEM_ACCESS_CTL, npei_mem_access_ctl.u64);
  823. /* Setup Mem access SubDIDs */
  824. mem_access_subid.u64 = 0;
  825. mem_access_subid.s.port = pcie_port; /* Port the request is sent to. */
  826. mem_access_subid.s.nmerge = 1; /* Due to an errata on pass 1 chips, no merging is allowed. */
  827. mem_access_subid.s.esr = 1; /* Endian-swap for Reads. */
  828. mem_access_subid.s.esw = 1; /* Endian-swap for Writes. */
  829. mem_access_subid.s.nsr = 0; /* Enable Snooping for Reads. Octeon doesn't care, but devices might want this more conservative setting */
  830. mem_access_subid.s.nsw = 0; /* Enable Snoop for Writes. */
  831. mem_access_subid.s.ror = 0; /* Disable Relaxed Ordering for Reads. */
  832. mem_access_subid.s.row = 0; /* Disable Relaxed Ordering for Writes. */
  833. mem_access_subid.s.ba = 0; /* PCIe Adddress Bits <63:34>. */
  834. /*
  835. * Setup mem access 12-15 for port 0, 16-19 for port 1,
  836. * supplying 36 bits of address space.
  837. */
  838. for (i = 12 + pcie_port * 4; i < 16 + pcie_port * 4; i++) {
  839. cvmx_write_csr(CVMX_PEXP_NPEI_MEM_ACCESS_SUBIDX(i), mem_access_subid.u64);
  840. mem_access_subid.s.ba += 1; /* Set each SUBID to extend the addressable range */
  841. }
  842. /*
  843. * Disable the peer to peer forwarding register. This must be
  844. * setup by the OS after it enumerates the bus and assigns
  845. * addresses to the PCIe busses.
  846. */
  847. for (i = 0; i < 4; i++) {
  848. cvmx_write_csr(CVMX_PESCX_P2P_BARX_START(i, pcie_port), -1);
  849. cvmx_write_csr(CVMX_PESCX_P2P_BARX_END(i, pcie_port), -1);
  850. }
  851. /* Set Octeon's BAR0 to decode 0-16KB. It overlaps with Bar2 */
  852. cvmx_write_csr(CVMX_PESCX_P2N_BAR0_START(pcie_port), 0);
  853. /* BAR1 follows BAR2 with a gap so it has the same address as for gen2. */
  854. cvmx_write_csr(CVMX_PESCX_P2N_BAR1_START(pcie_port), CVMX_PCIE_BAR1_RC_BASE);
  855. bar1_index.u32 = 0;
  856. bar1_index.s.addr_idx = (CVMX_PCIE_BAR1_PHYS_BASE >> 22);
  857. bar1_index.s.ca = 1; /* Not Cached */
  858. bar1_index.s.end_swp = 1; /* Endian Swap mode */
  859. bar1_index.s.addr_v = 1; /* Valid entry */
  860. base = pcie_port ? 16 : 0;
  861. /* Big endian swizzle for 32-bit PEXP_NCB register. */
  862. #ifdef __MIPSEB__
  863. addr_swizzle = 4;
  864. #else
  865. addr_swizzle = 0;
  866. #endif
  867. for (i = 0; i < 16; i++) {
  868. cvmx_write64_uint32((CVMX_PEXP_NPEI_BAR1_INDEXX(base) ^ addr_swizzle),
  869. bar1_index.u32);
  870. base++;
  871. /* 256MB / 16 >> 22 == 4 */
  872. bar1_index.s.addr_idx += (((1ull << 28) / 16ull) >> 22);
  873. }
  874. /*
  875. * Set Octeon's BAR2 to decode 0-2^39. Bar0 and Bar1 take
  876. * precedence where they overlap. It also overlaps with the
  877. * device addresses, so make sure the peer to peer forwarding
  878. * is set right.
  879. */
  880. cvmx_write_csr(CVMX_PESCX_P2N_BAR2_START(pcie_port), 0);
  881. /*
  882. * Setup BAR2 attributes
  883. *
  884. * Relaxed Ordering (NPEI_CTL_PORTn[PTLP_RO,CTLP_RO, WAIT_COM])
  885. * - PTLP_RO,CTLP_RO should normally be set (except for debug).
  886. * - WAIT_COM=0 will likely work for all applications.
  887. *
  888. * Load completion relaxed ordering (NPEI_CTL_PORTn[WAITL_COM]).
  889. */
  890. if (pcie_port) {
  891. union cvmx_npei_ctl_port1 npei_ctl_port;
  892. npei_ctl_port.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_PORT1);
  893. npei_ctl_port.s.bar2_enb = 1;
  894. npei_ctl_port.s.bar2_esx = 1;
  895. npei_ctl_port.s.bar2_cax = 0;
  896. npei_ctl_port.s.ptlp_ro = 1;
  897. npei_ctl_port.s.ctlp_ro = 1;
  898. npei_ctl_port.s.wait_com = 0;
  899. npei_ctl_port.s.waitl_com = 0;
  900. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_PORT1, npei_ctl_port.u64);
  901. } else {
  902. union cvmx_npei_ctl_port0 npei_ctl_port;
  903. npei_ctl_port.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_PORT0);
  904. npei_ctl_port.s.bar2_enb = 1;
  905. npei_ctl_port.s.bar2_esx = 1;
  906. npei_ctl_port.s.bar2_cax = 0;
  907. npei_ctl_port.s.ptlp_ro = 1;
  908. npei_ctl_port.s.ctlp_ro = 1;
  909. npei_ctl_port.s.wait_com = 0;
  910. npei_ctl_port.s.waitl_com = 0;
  911. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_PORT0, npei_ctl_port.u64);
  912. }
  913. /*
  914. * Both pass 1 and pass 2 of CN52XX and CN56XX have an errata
  915. * that causes TLP ordering to not be preserved after multiple
  916. * PCIe port resets. This code detects this fault and corrects
  917. * it by aligning the TLP counters properly. Another link
  918. * reset is then performed. See PCIE-13340
  919. */
  920. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS2_X) ||
  921. OCTEON_IS_MODEL(OCTEON_CN52XX_PASS2_X) ||
  922. OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X) ||
  923. OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  924. union cvmx_npei_dbg_data dbg_data;
  925. int old_in_fif_p_count;
  926. int in_fif_p_count;
  927. int out_p_count;
  928. int in_p_offset = (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X) || OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X)) ? 4 : 1;
  929. int i;
  930. /*
  931. * Choose a write address of 1MB. It should be
  932. * harmless as all bars haven't been setup.
  933. */
  934. uint64_t write_address = (cvmx_pcie_get_mem_base_address(pcie_port) + 0x100000) | (1ull<<63);
  935. /*
  936. * Make sure at least in_p_offset have been executed before we try and
  937. * read in_fif_p_count
  938. */
  939. i = in_p_offset;
  940. while (i--) {
  941. cvmx_write64_uint32(write_address, 0);
  942. cvmx_wait(10000);
  943. }
  944. /*
  945. * Read the IN_FIF_P_COUNT from the debug
  946. * select. IN_FIF_P_COUNT can be unstable sometimes so
  947. * read it twice with a write between the reads. This
  948. * way we can tell the value is good as it will
  949. * increment by one due to the write
  950. */
  951. cvmx_write_csr(CVMX_PEXP_NPEI_DBG_SELECT, (pcie_port) ? 0xd7fc : 0xcffc);
  952. cvmx_read_csr(CVMX_PEXP_NPEI_DBG_SELECT);
  953. do {
  954. dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  955. old_in_fif_p_count = dbg_data.s.data & 0xff;
  956. cvmx_write64_uint32(write_address, 0);
  957. cvmx_wait(10000);
  958. dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  959. in_fif_p_count = dbg_data.s.data & 0xff;
  960. } while (in_fif_p_count != ((old_in_fif_p_count+1) & 0xff));
  961. /* Update in_fif_p_count for it's offset with respect to out_p_count */
  962. in_fif_p_count = (in_fif_p_count + in_p_offset) & 0xff;
  963. /* Read the OUT_P_COUNT from the debug select */
  964. cvmx_write_csr(CVMX_PEXP_NPEI_DBG_SELECT, (pcie_port) ? 0xd00f : 0xc80f);
  965. cvmx_read_csr(CVMX_PEXP_NPEI_DBG_SELECT);
  966. dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  967. out_p_count = (dbg_data.s.data>>1) & 0xff;
  968. /* Check that the two counters are aligned */
  969. if (out_p_count != in_fif_p_count) {
  970. cvmx_dprintf("PCIe: Port %d aligning TLP counters as workaround to maintain ordering\n", pcie_port);
  971. while (in_fif_p_count != 0) {
  972. cvmx_write64_uint32(write_address, 0);
  973. cvmx_wait(10000);
  974. in_fif_p_count = (in_fif_p_count + 1) & 0xff;
  975. }
  976. /*
  977. * The EBH5200 board swapped the PCIe reset
  978. * lines on the board. This means we must
  979. * bring both links down and up, which will
  980. * cause the PCIe0 to need alignment
  981. * again. Lots of messages will be displayed,
  982. * but everything should work
  983. */
  984. if ((cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_EBH5200) &&
  985. (pcie_port == 1))
  986. cvmx_pcie_rc_initialize(0);
  987. /* Rety bringing this port up */
  988. goto retry;
  989. }
  990. }
  991. /* Display the link status */
  992. pciercx_cfg032.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG032(pcie_port));
  993. cvmx_dprintf("PCIe: Port %d link active, %d lanes\n", pcie_port, pciercx_cfg032.s.nlw);
  994. return 0;
  995. }
  996. /**
  997. * Initialize a host mode PCIe gen 2 link. This function takes a PCIe
  998. * port from reset to a link up state. Software can then begin
  999. * configuring the rest of the link.
  1000. *
  1001. * @pcie_port: PCIe port to initialize
  1002. *
  1003. * Return Zero on success.
  1004. */
  1005. static int __cvmx_pcie_rc_initialize_link_gen2(int pcie_port)
  1006. {
  1007. uint64_t start_cycle;
  1008. union cvmx_pemx_ctl_status pem_ctl_status;
  1009. union cvmx_pciercx_cfg032 pciercx_cfg032;
  1010. union cvmx_pciercx_cfg448 pciercx_cfg448;
  1011. /* Bring up the link */
  1012. pem_ctl_status.u64 = cvmx_read_csr(CVMX_PEMX_CTL_STATUS(pcie_port));
  1013. pem_ctl_status.s.lnk_enb = 1;
  1014. cvmx_write_csr(CVMX_PEMX_CTL_STATUS(pcie_port), pem_ctl_status.u64);
  1015. /* Wait for the link to come up */
  1016. start_cycle = cvmx_get_cycle();
  1017. do {
  1018. if (cvmx_get_cycle() - start_cycle > octeon_get_clock_rate())
  1019. return -1;
  1020. cvmx_wait(10000);
  1021. pciercx_cfg032.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG032(pcie_port));
  1022. } while ((pciercx_cfg032.s.dlla == 0) || (pciercx_cfg032.s.lt == 1));
  1023. /*
  1024. * Update the Replay Time Limit. Empirically, some PCIe
  1025. * devices take a little longer to respond than expected under
  1026. * load. As a workaround for this we configure the Replay Time
  1027. * Limit to the value expected for a 512 byte MPS instead of
  1028. * our actual 256 byte MPS. The numbers below are directly
  1029. * from the PCIe spec table 3-4
  1030. */
  1031. pciercx_cfg448.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG448(pcie_port));
  1032. switch (pciercx_cfg032.s.nlw) {
  1033. case 1: /* 1 lane */
  1034. pciercx_cfg448.s.rtl = 1677;
  1035. break;
  1036. case 2: /* 2 lanes */
  1037. pciercx_cfg448.s.rtl = 867;
  1038. break;
  1039. case 4: /* 4 lanes */
  1040. pciercx_cfg448.s.rtl = 462;
  1041. break;
  1042. case 8: /* 8 lanes */
  1043. pciercx_cfg448.s.rtl = 258;
  1044. break;
  1045. }
  1046. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG448(pcie_port), pciercx_cfg448.u32);
  1047. return 0;
  1048. }
  1049. /**
  1050. * Initialize a PCIe gen 2 port for use in host(RC) mode. It doesn't enumerate
  1051. * the bus.
  1052. *
  1053. * @pcie_port: PCIe port to initialize
  1054. *
  1055. * Returns Zero on success.
  1056. */
  1057. static int __cvmx_pcie_rc_initialize_gen2(int pcie_port)
  1058. {
  1059. int i;
  1060. union cvmx_ciu_soft_prst ciu_soft_prst;
  1061. union cvmx_mio_rst_ctlx mio_rst_ctl;
  1062. union cvmx_pemx_bar_ctl pemx_bar_ctl;
  1063. union cvmx_pemx_ctl_status pemx_ctl_status;
  1064. union cvmx_pemx_bist_status pemx_bist_status;
  1065. union cvmx_pemx_bist_status2 pemx_bist_status2;
  1066. union cvmx_pciercx_cfg032 pciercx_cfg032;
  1067. union cvmx_pciercx_cfg515 pciercx_cfg515;
  1068. union cvmx_sli_ctl_portx sli_ctl_portx;
  1069. union cvmx_sli_mem_access_ctl sli_mem_access_ctl;
  1070. union cvmx_sli_mem_access_subidx mem_access_subid;
  1071. union cvmx_sriox_status_reg sriox_status_reg;
  1072. union cvmx_pemx_bar1_indexx bar1_index;
  1073. if (octeon_has_feature(OCTEON_FEATURE_SRIO)) {
  1074. /* Make sure this interface isn't SRIO */
  1075. if (OCTEON_IS_MODEL(OCTEON_CN66XX)) {
  1076. /*
  1077. * The CN66XX requires reading the
  1078. * MIO_QLMX_CFG register to figure out the
  1079. * port type.
  1080. */
  1081. union cvmx_mio_qlmx_cfg qlmx_cfg;
  1082. qlmx_cfg.u64 = cvmx_read_csr(CVMX_MIO_QLMX_CFG(pcie_port));
  1083. if (qlmx_cfg.s.qlm_spd == 15) {
  1084. pr_notice("PCIe: Port %d is disabled, skipping.\n", pcie_port);
  1085. return -1;
  1086. }
  1087. switch (qlmx_cfg.s.qlm_spd) {
  1088. case 0x1: /* SRIO 1x4 short */
  1089. case 0x3: /* SRIO 1x4 long */
  1090. case 0x4: /* SRIO 2x2 short */
  1091. case 0x6: /* SRIO 2x2 long */
  1092. pr_notice("PCIe: Port %d is SRIO, skipping.\n", pcie_port);
  1093. return -1;
  1094. case 0x9: /* SGMII */
  1095. pr_notice("PCIe: Port %d is SGMII, skipping.\n", pcie_port);
  1096. return -1;
  1097. case 0xb: /* XAUI */
  1098. pr_notice("PCIe: Port %d is XAUI, skipping.\n", pcie_port);
  1099. return -1;
  1100. case 0x0: /* PCIE gen2 */
  1101. case 0x8: /* PCIE gen2 (alias) */
  1102. case 0x2: /* PCIE gen1 */
  1103. case 0xa: /* PCIE gen1 (alias) */
  1104. break;
  1105. default:
  1106. pr_notice("PCIe: Port %d is unknown, skipping.\n", pcie_port);
  1107. return -1;
  1108. }
  1109. } else {
  1110. sriox_status_reg.u64 = cvmx_read_csr(CVMX_SRIOX_STATUS_REG(pcie_port));
  1111. if (sriox_status_reg.s.srio) {
  1112. pr_notice("PCIe: Port %d is SRIO, skipping.\n", pcie_port);
  1113. return -1;
  1114. }
  1115. }
  1116. }
  1117. #if 0
  1118. /* This code is so that the PCIe analyzer is able to see 63XX traffic */
  1119. pr_notice("PCIE : init for pcie analyzer.\n");
  1120. cvmx_helper_qlm_jtag_init();
  1121. cvmx_helper_qlm_jtag_shift_zeros(pcie_port, 85);
  1122. cvmx_helper_qlm_jtag_shift(pcie_port, 1, 1);
  1123. cvmx_helper_qlm_jtag_shift_zeros(pcie_port, 300-86);
  1124. cvmx_helper_qlm_jtag_shift_zeros(pcie_port, 85);
  1125. cvmx_helper_qlm_jtag_shift(pcie_port, 1, 1);
  1126. cvmx_helper_qlm_jtag_shift_zeros(pcie_port, 300-86);
  1127. cvmx_helper_qlm_jtag_shift_zeros(pcie_port, 85);
  1128. cvmx_helper_qlm_jtag_shift(pcie_port, 1, 1);
  1129. cvmx_helper_qlm_jtag_shift_zeros(pcie_port, 300-86);
  1130. cvmx_helper_qlm_jtag_shift_zeros(pcie_port, 85);
  1131. cvmx_helper_qlm_jtag_shift(pcie_port, 1, 1);
  1132. cvmx_helper_qlm_jtag_shift_zeros(pcie_port, 300-86);
  1133. cvmx_helper_qlm_jtag_update(pcie_port);
  1134. #endif
  1135. /* Make sure we aren't trying to setup a target mode interface in host mode */
  1136. mio_rst_ctl.u64 = cvmx_read_csr(CVMX_MIO_RST_CTLX(pcie_port));
  1137. if (!mio_rst_ctl.s.host_mode) {
  1138. pr_notice("PCIe: Port %d in endpoint mode.\n", pcie_port);
  1139. return -1;
  1140. }
  1141. /* CN63XX Pass 1.0 errata G-14395 requires the QLM De-emphasis be programmed */
  1142. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_0)) {
  1143. if (pcie_port) {
  1144. union cvmx_ciu_qlm1 ciu_qlm;
  1145. ciu_qlm.u64 = cvmx_read_csr(CVMX_CIU_QLM1);
  1146. ciu_qlm.s.txbypass = 1;
  1147. ciu_qlm.s.txdeemph = 5;
  1148. ciu_qlm.s.txmargin = 0x17;
  1149. cvmx_write_csr(CVMX_CIU_QLM1, ciu_qlm.u64);
  1150. } else {
  1151. union cvmx_ciu_qlm0 ciu_qlm;
  1152. ciu_qlm.u64 = cvmx_read_csr(CVMX_CIU_QLM0);
  1153. ciu_qlm.s.txbypass = 1;
  1154. ciu_qlm.s.txdeemph = 5;
  1155. ciu_qlm.s.txmargin = 0x17;
  1156. cvmx_write_csr(CVMX_CIU_QLM0, ciu_qlm.u64);
  1157. }
  1158. }
  1159. /* Bring the PCIe out of reset */
  1160. if (pcie_port)
  1161. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  1162. else
  1163. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  1164. /*
  1165. * After a chip reset the PCIe will also be in reset. If it
  1166. * isn't, most likely someone is trying to init it again
  1167. * without a proper PCIe reset
  1168. */
  1169. if (ciu_soft_prst.s.soft_prst == 0) {
  1170. /* Reset the port */
  1171. ciu_soft_prst.s.soft_prst = 1;
  1172. if (pcie_port)
  1173. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  1174. else
  1175. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  1176. /* Wait until pcie resets the ports. */
  1177. udelay(2000);
  1178. }
  1179. if (pcie_port) {
  1180. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  1181. ciu_soft_prst.s.soft_prst = 0;
  1182. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  1183. } else {
  1184. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  1185. ciu_soft_prst.s.soft_prst = 0;
  1186. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  1187. }
  1188. /* Wait for PCIe reset to complete */
  1189. udelay(1000);
  1190. /*
  1191. * Check and make sure PCIe came out of reset. If it doesn't
  1192. * the board probably hasn't wired the clocks up and the
  1193. * interface should be skipped.
  1194. */
  1195. if (CVMX_WAIT_FOR_FIELD64(CVMX_MIO_RST_CTLX(pcie_port), union cvmx_mio_rst_ctlx, rst_done, ==, 1, 10000)) {
  1196. pr_notice("PCIe: Port %d stuck in reset, skipping.\n", pcie_port);
  1197. return -1;
  1198. }
  1199. /* Check BIST status */
  1200. pemx_bist_status.u64 = cvmx_read_csr(CVMX_PEMX_BIST_STATUS(pcie_port));
  1201. if (pemx_bist_status.u64)
  1202. pr_notice("PCIe: BIST FAILED for port %d (0x%016llx)\n", pcie_port, CAST64(pemx_bist_status.u64));
  1203. pemx_bist_status2.u64 = cvmx_read_csr(CVMX_PEMX_BIST_STATUS2(pcie_port));
  1204. /* Errata PCIE-14766 may cause the lower 6 bits to be randomly set on CN63XXp1 */
  1205. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X))
  1206. pemx_bist_status2.u64 &= ~0x3full;
  1207. if (pemx_bist_status2.u64)
  1208. pr_notice("PCIe: BIST2 FAILED for port %d (0x%016llx)\n", pcie_port, CAST64(pemx_bist_status2.u64));
  1209. /* Initialize the config space CSRs */
  1210. __cvmx_pcie_rc_initialize_config_space(pcie_port);
  1211. /* Enable gen2 speed selection */
  1212. pciercx_cfg515.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG515(pcie_port));
  1213. pciercx_cfg515.s.dsc = 1;
  1214. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG515(pcie_port), pciercx_cfg515.u32);
  1215. /* Bring the link up */
  1216. if (__cvmx_pcie_rc_initialize_link_gen2(pcie_port)) {
  1217. /*
  1218. * Some gen1 devices don't handle the gen 2 training
  1219. * correctly. Disable gen2 and try again with only
  1220. * gen1
  1221. */
  1222. union cvmx_pciercx_cfg031 pciercx_cfg031;
  1223. pciercx_cfg031.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG031(pcie_port));
  1224. pciercx_cfg031.s.mls = 1;
  1225. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG031(pcie_port), pciercx_cfg031.u32);
  1226. if (__cvmx_pcie_rc_initialize_link_gen2(pcie_port)) {
  1227. pr_notice("PCIe: Link timeout on port %d, probably the slot is empty\n", pcie_port);
  1228. return -1;
  1229. }
  1230. }
  1231. /* Store merge control (SLI_MEM_ACCESS_CTL[TIMER,MAX_WORD]) */
  1232. sli_mem_access_ctl.u64 = cvmx_read_csr(CVMX_PEXP_SLI_MEM_ACCESS_CTL);
  1233. sli_mem_access_ctl.s.max_word = 0; /* Allow 16 words to combine */
  1234. sli_mem_access_ctl.s.timer = 127; /* Wait up to 127 cycles for more data */
  1235. cvmx_write_csr(CVMX_PEXP_SLI_MEM_ACCESS_CTL, sli_mem_access_ctl.u64);
  1236. /* Setup Mem access SubDIDs */
  1237. mem_access_subid.u64 = 0;
  1238. mem_access_subid.s.port = pcie_port; /* Port the request is sent to. */
  1239. mem_access_subid.s.nmerge = 0; /* Allow merging as it works on CN6XXX. */
  1240. mem_access_subid.s.esr = 1; /* Endian-swap for Reads. */
  1241. mem_access_subid.s.esw = 1; /* Endian-swap for Writes. */
  1242. mem_access_subid.s.wtype = 0; /* "No snoop" and "Relaxed ordering" are not set */
  1243. mem_access_subid.s.rtype = 0; /* "No snoop" and "Relaxed ordering" are not set */
  1244. /* PCIe Adddress Bits <63:34>. */
  1245. if (OCTEON_IS_MODEL(OCTEON_CN68XX))
  1246. mem_access_subid.cn68xx.ba = 0;
  1247. else
  1248. mem_access_subid.cn63xx.ba = 0;
  1249. /*
  1250. * Setup mem access 12-15 for port 0, 16-19 for port 1,
  1251. * supplying 36 bits of address space.
  1252. */
  1253. for (i = 12 + pcie_port * 4; i < 16 + pcie_port * 4; i++) {
  1254. cvmx_write_csr(CVMX_PEXP_SLI_MEM_ACCESS_SUBIDX(i), mem_access_subid.u64);
  1255. /* Set each SUBID to extend the addressable range */
  1256. __cvmx_increment_ba(&mem_access_subid);
  1257. }
  1258. /*
  1259. * Disable the peer to peer forwarding register. This must be
  1260. * setup by the OS after it enumerates the bus and assigns
  1261. * addresses to the PCIe busses.
  1262. */
  1263. for (i = 0; i < 4; i++) {
  1264. cvmx_write_csr(CVMX_PEMX_P2P_BARX_START(i, pcie_port), -1);
  1265. cvmx_write_csr(CVMX_PEMX_P2P_BARX_END(i, pcie_port), -1);
  1266. }
  1267. /* Set Octeon's BAR0 to decode 0-16KB. It overlaps with Bar2 */
  1268. cvmx_write_csr(CVMX_PEMX_P2N_BAR0_START(pcie_port), 0);
  1269. /*
  1270. * Set Octeon's BAR2 to decode 0-2^41. Bar0 and Bar1 take
  1271. * precedence where they overlap. It also overlaps with the
  1272. * device addresses, so make sure the peer to peer forwarding
  1273. * is set right.
  1274. */
  1275. cvmx_write_csr(CVMX_PEMX_P2N_BAR2_START(pcie_port), 0);
  1276. /*
  1277. * Setup BAR2 attributes
  1278. * Relaxed Ordering (NPEI_CTL_PORTn[PTLP_RO,CTLP_RO, WAIT_COM])
  1279. * - PTLP_RO,CTLP_RO should normally be set (except for debug).
  1280. * - WAIT_COM=0 will likely work for all applications.
  1281. * Load completion relaxed ordering (NPEI_CTL_PORTn[WAITL_COM])
  1282. */
  1283. pemx_bar_ctl.u64 = cvmx_read_csr(CVMX_PEMX_BAR_CTL(pcie_port));
  1284. pemx_bar_ctl.s.bar1_siz = 3; /* 256MB BAR1*/
  1285. pemx_bar_ctl.s.bar2_enb = 1;
  1286. pemx_bar_ctl.s.bar2_esx = 1;
  1287. pemx_bar_ctl.s.bar2_cax = 0;
  1288. cvmx_write_csr(CVMX_PEMX_BAR_CTL(pcie_port), pemx_bar_ctl.u64);
  1289. sli_ctl_portx.u64 = cvmx_read_csr(CVMX_PEXP_SLI_CTL_PORTX(pcie_port));
  1290. sli_ctl_portx.s.ptlp_ro = 1;
  1291. sli_ctl_portx.s.ctlp_ro = 1;
  1292. sli_ctl_portx.s.wait_com = 0;
  1293. sli_ctl_portx.s.waitl_com = 0;
  1294. cvmx_write_csr(CVMX_PEXP_SLI_CTL_PORTX(pcie_port), sli_ctl_portx.u64);
  1295. /* BAR1 follows BAR2 */
  1296. cvmx_write_csr(CVMX_PEMX_P2N_BAR1_START(pcie_port), CVMX_PCIE_BAR1_RC_BASE);
  1297. bar1_index.u64 = 0;
  1298. bar1_index.s.addr_idx = (CVMX_PCIE_BAR1_PHYS_BASE >> 22);
  1299. bar1_index.s.ca = 1; /* Not Cached */
  1300. bar1_index.s.end_swp = 1; /* Endian Swap mode */
  1301. bar1_index.s.addr_v = 1; /* Valid entry */
  1302. for (i = 0; i < 16; i++) {
  1303. cvmx_write_csr(CVMX_PEMX_BAR1_INDEXX(i, pcie_port), bar1_index.u64);
  1304. /* 256MB / 16 >> 22 == 4 */
  1305. bar1_index.s.addr_idx += (((1ull << 28) / 16ull) >> 22);
  1306. }
  1307. /*
  1308. * Allow config retries for 250ms. Count is based off the 5Ghz
  1309. * SERDES clock.
  1310. */
  1311. pemx_ctl_status.u64 = cvmx_read_csr(CVMX_PEMX_CTL_STATUS(pcie_port));
  1312. pemx_ctl_status.s.cfg_rtry = 250 * 5000000 / 0x10000;
  1313. cvmx_write_csr(CVMX_PEMX_CTL_STATUS(pcie_port), pemx_ctl_status.u64);
  1314. /* Display the link status */
  1315. pciercx_cfg032.u32 = cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG032(pcie_port));
  1316. pr_notice("PCIe: Port %d link active, %d lanes, speed gen%d\n", pcie_port, pciercx_cfg032.s.nlw, pciercx_cfg032.s.ls);
  1317. return 0;
  1318. }
  1319. /**
  1320. * Initialize a PCIe port for use in host(RC) mode. It doesn't enumerate the bus.
  1321. *
  1322. * @pcie_port: PCIe port to initialize
  1323. *
  1324. * Returns Zero on success
  1325. */
  1326. static int cvmx_pcie_rc_initialize(int pcie_port)
  1327. {
  1328. int result;
  1329. if (octeon_has_feature(OCTEON_FEATURE_NPEI))
  1330. result = __cvmx_pcie_rc_initialize_gen1(pcie_port);
  1331. else
  1332. result = __cvmx_pcie_rc_initialize_gen2(pcie_port);
  1333. return result;
  1334. }
  1335. /* Above was cvmx-pcie.c, below original pcie.c */
  1336. /**
  1337. * Map a PCI device to the appropriate interrupt line
  1338. *
  1339. * @dev: The Linux PCI device structure for the device to map
  1340. * @slot: The slot number for this device on __BUS 0__. Linux
  1341. * enumerates through all the bridges and figures out the
  1342. * slot on Bus 0 where this device eventually hooks to.
  1343. * @pin: The PCI interrupt pin read from the device, then swizzled
  1344. * as it goes through each bridge.
  1345. * Returns Interrupt number for the device
  1346. */
  1347. int __init octeon_pcie_pcibios_map_irq(const struct pci_dev *dev,
  1348. u8 slot, u8 pin)
  1349. {
  1350. /*
  1351. * The EBH5600 board with the PCI to PCIe bridge mistakenly
  1352. * wires the first slot for both device id 2 and interrupt
  1353. * A. According to the PCI spec, device id 2 should be C. The
  1354. * following kludge attempts to fix this.
  1355. */
  1356. if (strstr(octeon_board_type_string(), "EBH5600") &&
  1357. dev->bus && dev->bus->parent) {
  1358. /*
  1359. * Iterate all the way up the device chain and find
  1360. * the root bus.
  1361. */
  1362. while (dev->bus && dev->bus->parent)
  1363. dev = to_pci_dev(dev->bus->bridge);
  1364. /*
  1365. * If the root bus is number 0 and the PEX 8114 is the
  1366. * root, assume we are behind the miswired bus. We
  1367. * need to correct the swizzle level by two. Yuck.
  1368. */
  1369. if ((dev->bus->number == 1) &&
  1370. (dev->vendor == 0x10b5) && (dev->device == 0x8114)) {
  1371. /*
  1372. * The pin field is one based, not zero. We
  1373. * need to swizzle it by minus two.
  1374. */
  1375. pin = ((pin - 3) & 3) + 1;
  1376. }
  1377. }
  1378. /*
  1379. * The -1 is because pin starts with one, not zero. It might
  1380. * be that this equation needs to include the slot number, but
  1381. * I don't have hardware to check that against.
  1382. */
  1383. return pin - 1 + OCTEON_IRQ_PCI_INT0;
  1384. }
  1385. static void set_cfg_read_retry(u32 retry_cnt)
  1386. {
  1387. union cvmx_pemx_ctl_status pemx_ctl;
  1388. pemx_ctl.u64 = cvmx_read_csr(CVMX_PEMX_CTL_STATUS(1));
  1389. pemx_ctl.s.cfg_rtry = retry_cnt;
  1390. cvmx_write_csr(CVMX_PEMX_CTL_STATUS(1), pemx_ctl.u64);
  1391. }
  1392. static u32 disable_cfg_read_retry(void)
  1393. {
  1394. u32 retry_cnt;
  1395. union cvmx_pemx_ctl_status pemx_ctl;
  1396. pemx_ctl.u64 = cvmx_read_csr(CVMX_PEMX_CTL_STATUS(1));
  1397. retry_cnt = pemx_ctl.s.cfg_rtry;
  1398. pemx_ctl.s.cfg_rtry = 0;
  1399. cvmx_write_csr(CVMX_PEMX_CTL_STATUS(1), pemx_ctl.u64);
  1400. return retry_cnt;
  1401. }
  1402. static int is_cfg_retry(void)
  1403. {
  1404. union cvmx_pemx_int_sum pemx_int_sum;
  1405. pemx_int_sum.u64 = cvmx_read_csr(CVMX_PEMX_INT_SUM(1));
  1406. if (pemx_int_sum.s.crs_dr)
  1407. return 1;
  1408. return 0;
  1409. }
  1410. /*
  1411. * Read a value from configuration space
  1412. *
  1413. */
  1414. static int octeon_pcie_read_config(unsigned int pcie_port, struct pci_bus *bus,
  1415. unsigned int devfn, int reg, int size,
  1416. u32 *val)
  1417. {
  1418. union octeon_cvmemctl cvmmemctl;
  1419. union octeon_cvmemctl cvmmemctl_save;
  1420. int bus_number = bus->number;
  1421. int cfg_retry = 0;
  1422. int retry_cnt = 0;
  1423. int max_retry_cnt = 10;
  1424. u32 cfg_retry_cnt = 0;
  1425. cvmmemctl_save.u64 = 0;
  1426. BUG_ON(pcie_port >= ARRAY_SIZE(enable_pcie_bus_num_war));
  1427. /*
  1428. * For the top level bus make sure our hardware bus number
  1429. * matches the software one
  1430. */
  1431. if (bus->parent == NULL) {
  1432. if (enable_pcie_bus_num_war[pcie_port])
  1433. bus_number = 0;
  1434. else {
  1435. union cvmx_pciercx_cfg006 pciercx_cfg006;
  1436. pciercx_cfg006.u32 = cvmx_pcie_cfgx_read(pcie_port,
  1437. CVMX_PCIERCX_CFG006(pcie_port));
  1438. if (pciercx_cfg006.s.pbnum != bus_number) {
  1439. pciercx_cfg006.s.pbnum = bus_number;
  1440. pciercx_cfg006.s.sbnum = bus_number;
  1441. pciercx_cfg006.s.subbnum = bus_number;
  1442. cvmx_pcie_cfgx_write(pcie_port,
  1443. CVMX_PCIERCX_CFG006(pcie_port),
  1444. pciercx_cfg006.u32);
  1445. }
  1446. }
  1447. }
  1448. /*
  1449. * PCIe only has a single device connected to Octeon. It is
  1450. * always device ID 0. Don't bother doing reads for other
  1451. * device IDs on the first segment.
  1452. */
  1453. if ((bus->parent == NULL) && (devfn >> 3 != 0))
  1454. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1455. /*
  1456. * The following is a workaround for the CN57XX, CN56XX,
  1457. * CN55XX, and CN54XX errata with PCIe config reads from non
  1458. * existent devices. These chips will hang the PCIe link if a
  1459. * config read is performed that causes a UR response.
  1460. */
  1461. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1) ||
  1462. OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_1)) {
  1463. /*
  1464. * For our EBH5600 board, port 0 has a bridge with two
  1465. * PCI-X slots. We need a new special checks to make
  1466. * sure we only probe valid stuff. The PCIe->PCI-X
  1467. * bridge only respondes to device ID 0, function
  1468. * 0-1
  1469. */
  1470. if ((bus->parent == NULL) && (devfn >= 2))
  1471. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1472. /*
  1473. * The PCI-X slots are device ID 2,3. Choose one of
  1474. * the below "if" blocks based on what is plugged into
  1475. * the board.
  1476. */
  1477. #if 1
  1478. /* Use this option if you aren't using either slot */
  1479. if (bus_number == 2)
  1480. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1481. #elif 0
  1482. /*
  1483. * Use this option if you are using the first slot but
  1484. * not the second.
  1485. */
  1486. if ((bus_number == 2) && (devfn >> 3 != 2))
  1487. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1488. #elif 0
  1489. /*
  1490. * Use this option if you are using the second slot
  1491. * but not the first.
  1492. */
  1493. if ((bus_number == 2) && (devfn >> 3 != 3))
  1494. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1495. #elif 0
  1496. /* Use this opion if you are using both slots */
  1497. if ((bus_number == 2) &&
  1498. !((devfn == (2 << 3)) || (devfn == (3 << 3))))
  1499. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1500. #endif
  1501. /* The following #if gives a more complicated example. This is
  1502. the required checks for running a Nitrox CN16XX-NHBX in the
  1503. slot of the EBH5600. This card has a PLX PCIe bridge with
  1504. four Nitrox PLX parts behind it */
  1505. #if 0
  1506. /* PLX bridge with 4 ports */
  1507. if ((bus_number == 4) &&
  1508. !((devfn >> 3 >= 1) && (devfn >> 3 <= 4)))
  1509. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1510. /* Nitrox behind PLX 1 */
  1511. if ((bus_number == 5) && (devfn >> 3 != 0))
  1512. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1513. /* Nitrox behind PLX 2 */
  1514. if ((bus_number == 6) && (devfn >> 3 != 0))
  1515. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1516. /* Nitrox behind PLX 3 */
  1517. if ((bus_number == 7) && (devfn >> 3 != 0))
  1518. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1519. /* Nitrox behind PLX 4 */
  1520. if ((bus_number == 8) && (devfn >> 3 != 0))
  1521. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1522. #endif
  1523. /*
  1524. * Shorten the DID timeout so bus errors for PCIe
  1525. * config reads from non existent devices happen
  1526. * faster. This allows us to continue booting even if
  1527. * the above "if" checks are wrong. Once one of these
  1528. * errors happens, the PCIe port is dead.
  1529. */
  1530. cvmmemctl_save.u64 = __read_64bit_c0_register($11, 7);
  1531. cvmmemctl.u64 = cvmmemctl_save.u64;
  1532. cvmmemctl.s.didtto = 2;
  1533. __write_64bit_c0_register($11, 7, cvmmemctl.u64);
  1534. }
  1535. if ((OCTEON_IS_MODEL(OCTEON_CN63XX)) && (enable_pcie_14459_war))
  1536. cfg_retry_cnt = disable_cfg_read_retry();
  1537. pr_debug("pcie_cfg_rd port=%d b=%d devfn=0x%03x reg=0x%03x"
  1538. " size=%d ", pcie_port, bus_number, devfn, reg, size);
  1539. do {
  1540. switch (size) {
  1541. case 4:
  1542. *val = cvmx_pcie_config_read32(pcie_port, bus_number,
  1543. devfn >> 3, devfn & 0x7, reg);
  1544. break;
  1545. case 2:
  1546. *val = cvmx_pcie_config_read16(pcie_port, bus_number,
  1547. devfn >> 3, devfn & 0x7, reg);
  1548. break;
  1549. case 1:
  1550. *val = cvmx_pcie_config_read8(pcie_port, bus_number,
  1551. devfn >> 3, devfn & 0x7, reg);
  1552. break;
  1553. default:
  1554. if (OCTEON_IS_MODEL(OCTEON_CN63XX))
  1555. set_cfg_read_retry(cfg_retry_cnt);
  1556. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1557. }
  1558. if ((OCTEON_IS_MODEL(OCTEON_CN63XX)) &&
  1559. (enable_pcie_14459_war)) {
  1560. cfg_retry = is_cfg_retry();
  1561. retry_cnt++;
  1562. if (retry_cnt > max_retry_cnt) {
  1563. pr_err(" pcie cfg_read retries failed. retry_cnt=%d\n",
  1564. retry_cnt);
  1565. cfg_retry = 0;
  1566. }
  1567. }
  1568. } while (cfg_retry);
  1569. if ((OCTEON_IS_MODEL(OCTEON_CN63XX)) && (enable_pcie_14459_war))
  1570. set_cfg_read_retry(cfg_retry_cnt);
  1571. pr_debug("val=%08x : tries=%02d\n", *val, retry_cnt);
  1572. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1) ||
  1573. OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_1))
  1574. write_c0_cvmmemctl(cvmmemctl_save.u64);
  1575. return PCIBIOS_SUCCESSFUL;
  1576. }
  1577. static int octeon_pcie0_read_config(struct pci_bus *bus, unsigned int devfn,
  1578. int reg, int size, u32 *val)
  1579. {
  1580. return octeon_pcie_read_config(0, bus, devfn, reg, size, val);
  1581. }
  1582. static int octeon_pcie1_read_config(struct pci_bus *bus, unsigned int devfn,
  1583. int reg, int size, u32 *val)
  1584. {
  1585. return octeon_pcie_read_config(1, bus, devfn, reg, size, val);
  1586. }
  1587. static int octeon_dummy_read_config(struct pci_bus *bus, unsigned int devfn,
  1588. int reg, int size, u32 *val)
  1589. {
  1590. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1591. }
  1592. /*
  1593. * Write a value to PCI configuration space
  1594. */
  1595. static int octeon_pcie_write_config(unsigned int pcie_port, struct pci_bus *bus,
  1596. unsigned int devfn, int reg,
  1597. int size, u32 val)
  1598. {
  1599. int bus_number = bus->number;
  1600. BUG_ON(pcie_port >= ARRAY_SIZE(enable_pcie_bus_num_war));
  1601. if ((bus->parent == NULL) && (enable_pcie_bus_num_war[pcie_port]))
  1602. bus_number = 0;
  1603. pr_debug("pcie_cfg_wr port=%d b=%d devfn=0x%03x"
  1604. " reg=0x%03x size=%d val=%08x\n", pcie_port, bus_number, devfn,
  1605. reg, size, val);
  1606. switch (size) {
  1607. case 4:
  1608. cvmx_pcie_config_write32(pcie_port, bus_number, devfn >> 3,
  1609. devfn & 0x7, reg, val);
  1610. break;
  1611. case 2:
  1612. cvmx_pcie_config_write16(pcie_port, bus_number, devfn >> 3,
  1613. devfn & 0x7, reg, val);
  1614. break;
  1615. case 1:
  1616. cvmx_pcie_config_write8(pcie_port, bus_number, devfn >> 3,
  1617. devfn & 0x7, reg, val);
  1618. break;
  1619. default:
  1620. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1621. }
  1622. #if PCI_CONFIG_SPACE_DELAY
  1623. /*
  1624. * Delay on writes so that devices have time to come up. Some
  1625. * bridges need this to allow time for the secondary busses to
  1626. * work
  1627. */
  1628. udelay(PCI_CONFIG_SPACE_DELAY);
  1629. #endif
  1630. return PCIBIOS_SUCCESSFUL;
  1631. }
  1632. static int octeon_pcie0_write_config(struct pci_bus *bus, unsigned int devfn,
  1633. int reg, int size, u32 val)
  1634. {
  1635. return octeon_pcie_write_config(0, bus, devfn, reg, size, val);
  1636. }
  1637. static int octeon_pcie1_write_config(struct pci_bus *bus, unsigned int devfn,
  1638. int reg, int size, u32 val)
  1639. {
  1640. return octeon_pcie_write_config(1, bus, devfn, reg, size, val);
  1641. }
  1642. static int octeon_dummy_write_config(struct pci_bus *bus, unsigned int devfn,
  1643. int reg, int size, u32 val)
  1644. {
  1645. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1646. }
  1647. static struct pci_ops octeon_pcie0_ops = {
  1648. octeon_pcie0_read_config,
  1649. octeon_pcie0_write_config,
  1650. };
  1651. static struct resource octeon_pcie0_mem_resource = {
  1652. .name = "Octeon PCIe0 MEM",
  1653. .flags = IORESOURCE_MEM,
  1654. };
  1655. static struct resource octeon_pcie0_io_resource = {
  1656. .name = "Octeon PCIe0 IO",
  1657. .flags = IORESOURCE_IO,
  1658. };
  1659. static struct pci_controller octeon_pcie0_controller = {
  1660. .pci_ops = &octeon_pcie0_ops,
  1661. .mem_resource = &octeon_pcie0_mem_resource,
  1662. .io_resource = &octeon_pcie0_io_resource,
  1663. };
  1664. static struct pci_ops octeon_pcie1_ops = {
  1665. octeon_pcie1_read_config,
  1666. octeon_pcie1_write_config,
  1667. };
  1668. static struct resource octeon_pcie1_mem_resource = {
  1669. .name = "Octeon PCIe1 MEM",
  1670. .flags = IORESOURCE_MEM,
  1671. };
  1672. static struct resource octeon_pcie1_io_resource = {
  1673. .name = "Octeon PCIe1 IO",
  1674. .flags = IORESOURCE_IO,
  1675. };
  1676. static struct pci_controller octeon_pcie1_controller = {
  1677. .pci_ops = &octeon_pcie1_ops,
  1678. .mem_resource = &octeon_pcie1_mem_resource,
  1679. .io_resource = &octeon_pcie1_io_resource,
  1680. };
  1681. static struct pci_ops octeon_dummy_ops = {
  1682. octeon_dummy_read_config,
  1683. octeon_dummy_write_config,
  1684. };
  1685. static struct resource octeon_dummy_mem_resource = {
  1686. .name = "Virtual PCIe MEM",
  1687. .flags = IORESOURCE_MEM,
  1688. };
  1689. static struct resource octeon_dummy_io_resource = {
  1690. .name = "Virtual PCIe IO",
  1691. .flags = IORESOURCE_IO,
  1692. };
  1693. static struct pci_controller octeon_dummy_controller = {
  1694. .pci_ops = &octeon_dummy_ops,
  1695. .mem_resource = &octeon_dummy_mem_resource,
  1696. .io_resource = &octeon_dummy_io_resource,
  1697. };
  1698. static int device_needs_bus_num_war(uint32_t deviceid)
  1699. {
  1700. #define IDT_VENDOR_ID 0x111d
  1701. if ((deviceid & 0xffff) == IDT_VENDOR_ID)
  1702. return 1;
  1703. return 0;
  1704. }
  1705. /**
  1706. * Initialize the Octeon PCIe controllers
  1707. *
  1708. * Returns
  1709. */
  1710. static int __init octeon_pcie_setup(void)
  1711. {
  1712. int result;
  1713. int host_mode;
  1714. int srio_war15205 = 0, port;
  1715. union cvmx_sli_ctl_portx sli_ctl_portx;
  1716. union cvmx_sriox_status_reg sriox_status_reg;
  1717. /* These chips don't have PCIe */
  1718. if (!octeon_has_feature(OCTEON_FEATURE_PCIE))
  1719. return 0;
  1720. /* No PCIe simulation */
  1721. if (octeon_is_simulation())
  1722. return 0;
  1723. /* Disable PCI if instructed on the command line */
  1724. if (pcie_disable)
  1725. return 0;
  1726. /* Point pcibios_map_irq() to the PCIe version of it */
  1727. octeon_pcibios_map_irq = octeon_pcie_pcibios_map_irq;
  1728. /*
  1729. * PCIe I/O range. It is based on port 0 but includes up until
  1730. * port 1's end.
  1731. */
  1732. set_io_port_base(CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address(0)));
  1733. ioport_resource.start = 0;
  1734. ioport_resource.end =
  1735. cvmx_pcie_get_io_base_address(1) -
  1736. cvmx_pcie_get_io_base_address(0) + cvmx_pcie_get_io_size(1) - 1;
  1737. /*
  1738. * Create a dummy PCIe controller to swallow up bus 0. IDT bridges
  1739. * don't work if the primary bus number is zero. Here we add a fake
  1740. * PCIe controller that the kernel will give bus 0. This allows
  1741. * us to not change the normal kernel bus enumeration
  1742. */
  1743. octeon_dummy_controller.io_map_base = -1;
  1744. octeon_dummy_controller.mem_resource->start = (1ull<<48);
  1745. octeon_dummy_controller.mem_resource->end = (1ull<<48);
  1746. register_pci_controller(&octeon_dummy_controller);
  1747. if (octeon_has_feature(OCTEON_FEATURE_NPEI)) {
  1748. union cvmx_npei_ctl_status npei_ctl_status;
  1749. npei_ctl_status.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_STATUS);
  1750. host_mode = npei_ctl_status.s.host_mode;
  1751. octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_PCIE;
  1752. } else {
  1753. union cvmx_mio_rst_ctlx mio_rst_ctl;
  1754. mio_rst_ctl.u64 = cvmx_read_csr(CVMX_MIO_RST_CTLX(0));
  1755. host_mode = mio_rst_ctl.s.host_mode;
  1756. octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_PCIE2;
  1757. }
  1758. if (host_mode) {
  1759. pr_notice("PCIe: Initializing port 0\n");
  1760. /* CN63XX pass 1_x/2.0 errata PCIe-15205 */
  1761. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X) ||
  1762. OCTEON_IS_MODEL(OCTEON_CN63XX_PASS2_0)) {
  1763. sriox_status_reg.u64 = cvmx_read_csr(CVMX_SRIOX_STATUS_REG(0));
  1764. if (sriox_status_reg.s.srio) {
  1765. srio_war15205 += 1; /* Port is SRIO */
  1766. port = 0;
  1767. }
  1768. }
  1769. result = cvmx_pcie_rc_initialize(0);
  1770. if (result == 0) {
  1771. uint32_t device0;
  1772. /* Memory offsets are physical addresses */
  1773. octeon_pcie0_controller.mem_offset =
  1774. cvmx_pcie_get_mem_base_address(0);
  1775. /* IO offsets are Mips virtual addresses */
  1776. octeon_pcie0_controller.io_map_base =
  1777. CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address
  1778. (0));
  1779. octeon_pcie0_controller.io_offset = 0;
  1780. /*
  1781. * To keep things similar to PCI, we start
  1782. * device addresses at the same place as PCI
  1783. * uisng big bar support. This normally
  1784. * translates to 4GB-256MB, which is the same
  1785. * as most x86 PCs.
  1786. */
  1787. octeon_pcie0_controller.mem_resource->start =
  1788. cvmx_pcie_get_mem_base_address(0) +
  1789. (4ul << 30) - (OCTEON_PCI_BAR1_HOLE_SIZE << 20);
  1790. octeon_pcie0_controller.mem_resource->end =
  1791. cvmx_pcie_get_mem_base_address(0) +
  1792. cvmx_pcie_get_mem_size(0) - 1;
  1793. /*
  1794. * Ports must be above 16KB for the ISA bus
  1795. * filtering in the PCI-X to PCI bridge.
  1796. */
  1797. octeon_pcie0_controller.io_resource->start = 4 << 10;
  1798. octeon_pcie0_controller.io_resource->end =
  1799. cvmx_pcie_get_io_size(0) - 1;
  1800. msleep(100); /* Some devices need extra time */
  1801. register_pci_controller(&octeon_pcie0_controller);
  1802. device0 = cvmx_pcie_config_read32(0, 0, 0, 0, 0);
  1803. enable_pcie_bus_num_war[0] =
  1804. device_needs_bus_num_war(device0);
  1805. }
  1806. } else {
  1807. pr_notice("PCIe: Port 0 in endpoint mode, skipping.\n");
  1808. /* CN63XX pass 1_x/2.0 errata PCIe-15205 */
  1809. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X) ||
  1810. OCTEON_IS_MODEL(OCTEON_CN63XX_PASS2_0)) {
  1811. srio_war15205 += 1;
  1812. port = 0;
  1813. }
  1814. }
  1815. if (octeon_has_feature(OCTEON_FEATURE_NPEI)) {
  1816. host_mode = 1;
  1817. /* Skip the 2nd port on CN52XX if port 0 is in 4 lane mode */
  1818. if (OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  1819. union cvmx_npei_dbg_data dbg_data;
  1820. dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  1821. if (dbg_data.cn52xx.qlm0_link_width)
  1822. host_mode = 0;
  1823. }
  1824. } else {
  1825. union cvmx_mio_rst_ctlx mio_rst_ctl;
  1826. mio_rst_ctl.u64 = cvmx_read_csr(CVMX_MIO_RST_CTLX(1));
  1827. host_mode = mio_rst_ctl.s.host_mode;
  1828. }
  1829. if (host_mode) {
  1830. pr_notice("PCIe: Initializing port 1\n");
  1831. /* CN63XX pass 1_x/2.0 errata PCIe-15205 */
  1832. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X) ||
  1833. OCTEON_IS_MODEL(OCTEON_CN63XX_PASS2_0)) {
  1834. sriox_status_reg.u64 = cvmx_read_csr(CVMX_SRIOX_STATUS_REG(1));
  1835. if (sriox_status_reg.s.srio) {
  1836. srio_war15205 += 1; /* Port is SRIO */
  1837. port = 1;
  1838. }
  1839. }
  1840. result = cvmx_pcie_rc_initialize(1);
  1841. if (result == 0) {
  1842. uint32_t device0;
  1843. /* Memory offsets are physical addresses */
  1844. octeon_pcie1_controller.mem_offset =
  1845. cvmx_pcie_get_mem_base_address(1);
  1846. /*
  1847. * To calculate the address for accessing the 2nd PCIe device,
  1848. * either 'io_map_base' (pci_iomap()), or 'mips_io_port_base'
  1849. * (ioport_map()) value is added to
  1850. * pci_resource_start(dev,bar)). The 'mips_io_port_base' is set
  1851. * only once based on first PCIe. Also changing 'io_map_base'
  1852. * based on first slot's value so that both the routines will
  1853. * work properly.
  1854. */
  1855. octeon_pcie1_controller.io_map_base =
  1856. CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address(0));
  1857. /* IO offsets are Mips virtual addresses */
  1858. octeon_pcie1_controller.io_offset =
  1859. cvmx_pcie_get_io_base_address(1) -
  1860. cvmx_pcie_get_io_base_address(0);
  1861. /*
  1862. * To keep things similar to PCI, we start device
  1863. * addresses at the same place as PCI uisng big bar
  1864. * support. This normally translates to 4GB-256MB,
  1865. * which is the same as most x86 PCs.
  1866. */
  1867. octeon_pcie1_controller.mem_resource->start =
  1868. cvmx_pcie_get_mem_base_address(1) + (4ul << 30) -
  1869. (OCTEON_PCI_BAR1_HOLE_SIZE << 20);
  1870. octeon_pcie1_controller.mem_resource->end =
  1871. cvmx_pcie_get_mem_base_address(1) +
  1872. cvmx_pcie_get_mem_size(1) - 1;
  1873. /*
  1874. * Ports must be above 16KB for the ISA bus filtering
  1875. * in the PCI-X to PCI bridge.
  1876. */
  1877. octeon_pcie1_controller.io_resource->start =
  1878. cvmx_pcie_get_io_base_address(1) -
  1879. cvmx_pcie_get_io_base_address(0);
  1880. octeon_pcie1_controller.io_resource->end =
  1881. octeon_pcie1_controller.io_resource->start +
  1882. cvmx_pcie_get_io_size(1) - 1;
  1883. msleep(100); /* Some devices need extra time */
  1884. register_pci_controller(&octeon_pcie1_controller);
  1885. device0 = cvmx_pcie_config_read32(1, 0, 0, 0, 0);
  1886. enable_pcie_bus_num_war[1] =
  1887. device_needs_bus_num_war(device0);
  1888. }
  1889. } else {
  1890. pr_notice("PCIe: Port 1 not in root complex mode, skipping.\n");
  1891. /* CN63XX pass 1_x/2.0 errata PCIe-15205 */
  1892. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X) ||
  1893. OCTEON_IS_MODEL(OCTEON_CN63XX_PASS2_0)) {
  1894. srio_war15205 += 1;
  1895. port = 1;
  1896. }
  1897. }
  1898. /*
  1899. * CN63XX pass 1_x/2.0 errata PCIe-15205 requires setting all
  1900. * of SRIO MACs SLI_CTL_PORT*[INT*_MAP] to similar value and
  1901. * all of PCIe Macs SLI_CTL_PORT*[INT*_MAP] to different value
  1902. * from the previous set values
  1903. */
  1904. if (OCTEON_IS_MODEL(OCTEON_CN63XX_PASS1_X) ||
  1905. OCTEON_IS_MODEL(OCTEON_CN63XX_PASS2_0)) {
  1906. if (srio_war15205 == 1) {
  1907. sli_ctl_portx.u64 = cvmx_read_csr(CVMX_PEXP_SLI_CTL_PORTX(port));
  1908. sli_ctl_portx.s.inta_map = 1;
  1909. sli_ctl_portx.s.intb_map = 1;
  1910. sli_ctl_portx.s.intc_map = 1;
  1911. sli_ctl_portx.s.intd_map = 1;
  1912. cvmx_write_csr(CVMX_PEXP_SLI_CTL_PORTX(port), sli_ctl_portx.u64);
  1913. sli_ctl_portx.u64 = cvmx_read_csr(CVMX_PEXP_SLI_CTL_PORTX(!port));
  1914. sli_ctl_portx.s.inta_map = 0;
  1915. sli_ctl_portx.s.intb_map = 0;
  1916. sli_ctl_portx.s.intc_map = 0;
  1917. sli_ctl_portx.s.intd_map = 0;
  1918. cvmx_write_csr(CVMX_PEXP_SLI_CTL_PORTX(!port), sli_ctl_portx.u64);
  1919. }
  1920. }
  1921. octeon_pci_dma_init();
  1922. return 0;
  1923. }
  1924. arch_initcall(octeon_pcie_setup);