pcie-octeon.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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 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 <asm/octeon/octeon.h>
  15. #include <asm/octeon/cvmx-npei-defs.h>
  16. #include <asm/octeon/cvmx-pciercx-defs.h>
  17. #include <asm/octeon/cvmx-pescx-defs.h>
  18. #include <asm/octeon/cvmx-pexp-defs.h>
  19. #include <asm/octeon/cvmx-helper-errata.h>
  20. #include <asm/octeon/pci-octeon.h>
  21. union cvmx_pcie_address {
  22. uint64_t u64;
  23. struct {
  24. uint64_t upper:2; /* Normally 2 for XKPHYS */
  25. uint64_t reserved_49_61:13; /* Must be zero */
  26. uint64_t io:1; /* 1 for IO space access */
  27. uint64_t did:5; /* PCIe DID = 3 */
  28. uint64_t subdid:3; /* PCIe SubDID = 1 */
  29. uint64_t reserved_36_39:4; /* Must be zero */
  30. uint64_t es:2; /* Endian swap = 1 */
  31. uint64_t port:2; /* PCIe port 0,1 */
  32. uint64_t reserved_29_31:3; /* Must be zero */
  33. /*
  34. * Selects the type of the configuration request (0 = type 0,
  35. * 1 = type 1).
  36. */
  37. uint64_t ty:1;
  38. /* Target bus number sent in the ID in the request. */
  39. uint64_t bus:8;
  40. /*
  41. * Target device number sent in the ID in the
  42. * request. Note that Dev must be zero for type 0
  43. * configuration requests.
  44. */
  45. uint64_t dev:5;
  46. /* Target function number sent in the ID in the request. */
  47. uint64_t func:3;
  48. /*
  49. * Selects a register in the configuration space of
  50. * the target.
  51. */
  52. uint64_t reg:12;
  53. } config;
  54. struct {
  55. uint64_t upper:2; /* Normally 2 for XKPHYS */
  56. uint64_t reserved_49_61:13; /* Must be zero */
  57. uint64_t io:1; /* 1 for IO space access */
  58. uint64_t did:5; /* PCIe DID = 3 */
  59. uint64_t subdid:3; /* PCIe SubDID = 2 */
  60. uint64_t reserved_36_39:4; /* Must be zero */
  61. uint64_t es:2; /* Endian swap = 1 */
  62. uint64_t port:2; /* PCIe port 0,1 */
  63. uint64_t address:32; /* PCIe IO address */
  64. } io;
  65. struct {
  66. uint64_t upper:2; /* Normally 2 for XKPHYS */
  67. uint64_t reserved_49_61:13; /* Must be zero */
  68. uint64_t io:1; /* 1 for IO space access */
  69. uint64_t did:5; /* PCIe DID = 3 */
  70. uint64_t subdid:3; /* PCIe SubDID = 3-6 */
  71. uint64_t reserved_36_39:4; /* Must be zero */
  72. uint64_t address:36; /* PCIe Mem address */
  73. } mem;
  74. };
  75. /**
  76. * Return the Core virtual base address for PCIe IO access. IOs are
  77. * read/written as an offset from this address.
  78. *
  79. * @pcie_port: PCIe port the IO is for
  80. *
  81. * Returns 64bit Octeon IO base address for read/write
  82. */
  83. static inline uint64_t cvmx_pcie_get_io_base_address(int pcie_port)
  84. {
  85. union cvmx_pcie_address pcie_addr;
  86. pcie_addr.u64 = 0;
  87. pcie_addr.io.upper = 0;
  88. pcie_addr.io.io = 1;
  89. pcie_addr.io.did = 3;
  90. pcie_addr.io.subdid = 2;
  91. pcie_addr.io.es = 1;
  92. pcie_addr.io.port = pcie_port;
  93. return pcie_addr.u64;
  94. }
  95. /**
  96. * Size of the IO address region returned at address
  97. * cvmx_pcie_get_io_base_address()
  98. *
  99. * @pcie_port: PCIe port the IO is for
  100. *
  101. * Returns Size of the IO window
  102. */
  103. static inline uint64_t cvmx_pcie_get_io_size(int pcie_port)
  104. {
  105. return 1ull << 32;
  106. }
  107. /**
  108. * Return the Core virtual base address for PCIe MEM access. Memory is
  109. * read/written as an offset from this address.
  110. *
  111. * @pcie_port: PCIe port the IO is for
  112. *
  113. * Returns 64bit Octeon IO base address for read/write
  114. */
  115. static inline uint64_t cvmx_pcie_get_mem_base_address(int pcie_port)
  116. {
  117. union cvmx_pcie_address pcie_addr;
  118. pcie_addr.u64 = 0;
  119. pcie_addr.mem.upper = 0;
  120. pcie_addr.mem.io = 1;
  121. pcie_addr.mem.did = 3;
  122. pcie_addr.mem.subdid = 3 + pcie_port;
  123. return pcie_addr.u64;
  124. }
  125. /**
  126. * Size of the Mem address region returned at address
  127. * cvmx_pcie_get_mem_base_address()
  128. *
  129. * @pcie_port: PCIe port the IO is for
  130. *
  131. * Returns Size of the Mem window
  132. */
  133. static inline uint64_t cvmx_pcie_get_mem_size(int pcie_port)
  134. {
  135. return 1ull << 36;
  136. }
  137. /**
  138. * Read a PCIe config space register indirectly. This is used for
  139. * registers of the form PCIEEP_CFG??? and PCIERC?_CFG???.
  140. *
  141. * @pcie_port: PCIe port to read from
  142. * @cfg_offset: Address to read
  143. *
  144. * Returns Value read
  145. */
  146. static uint32_t cvmx_pcie_cfgx_read(int pcie_port, uint32_t cfg_offset)
  147. {
  148. union cvmx_pescx_cfg_rd pescx_cfg_rd;
  149. pescx_cfg_rd.u64 = 0;
  150. pescx_cfg_rd.s.addr = cfg_offset;
  151. cvmx_write_csr(CVMX_PESCX_CFG_RD(pcie_port), pescx_cfg_rd.u64);
  152. pescx_cfg_rd.u64 = cvmx_read_csr(CVMX_PESCX_CFG_RD(pcie_port));
  153. return pescx_cfg_rd.s.data;
  154. }
  155. /**
  156. * Write a PCIe config space register indirectly. This is used for
  157. * registers of the form PCIEEP_CFG??? and PCIERC?_CFG???.
  158. *
  159. * @pcie_port: PCIe port to write to
  160. * @cfg_offset: Address to write
  161. * @val: Value to write
  162. */
  163. static void cvmx_pcie_cfgx_write(int pcie_port, uint32_t cfg_offset,
  164. uint32_t val)
  165. {
  166. union cvmx_pescx_cfg_wr pescx_cfg_wr;
  167. pescx_cfg_wr.u64 = 0;
  168. pescx_cfg_wr.s.addr = cfg_offset;
  169. pescx_cfg_wr.s.data = val;
  170. cvmx_write_csr(CVMX_PESCX_CFG_WR(pcie_port), pescx_cfg_wr.u64);
  171. }
  172. /**
  173. * Build a PCIe config space request address for a device
  174. *
  175. * @pcie_port: PCIe port to access
  176. * @bus: Sub bus
  177. * @dev: Device ID
  178. * @fn: Device sub function
  179. * @reg: Register to access
  180. *
  181. * Returns 64bit Octeon IO address
  182. */
  183. static inline uint64_t __cvmx_pcie_build_config_addr(int pcie_port, int bus,
  184. int dev, int fn, int reg)
  185. {
  186. union cvmx_pcie_address pcie_addr;
  187. union cvmx_pciercx_cfg006 pciercx_cfg006;
  188. pciercx_cfg006.u32 =
  189. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG006(pcie_port));
  190. if ((bus <= pciercx_cfg006.s.pbnum) && (dev != 0))
  191. return 0;
  192. pcie_addr.u64 = 0;
  193. pcie_addr.config.upper = 2;
  194. pcie_addr.config.io = 1;
  195. pcie_addr.config.did = 3;
  196. pcie_addr.config.subdid = 1;
  197. pcie_addr.config.es = 1;
  198. pcie_addr.config.port = pcie_port;
  199. pcie_addr.config.ty = (bus > pciercx_cfg006.s.pbnum);
  200. pcie_addr.config.bus = bus;
  201. pcie_addr.config.dev = dev;
  202. pcie_addr.config.func = fn;
  203. pcie_addr.config.reg = reg;
  204. return pcie_addr.u64;
  205. }
  206. /**
  207. * Read 8bits from a Device's config space
  208. *
  209. * @pcie_port: PCIe port the device is on
  210. * @bus: Sub bus
  211. * @dev: Device ID
  212. * @fn: Device sub function
  213. * @reg: Register to access
  214. *
  215. * Returns Result of the read
  216. */
  217. static uint8_t cvmx_pcie_config_read8(int pcie_port, int bus, int dev,
  218. int fn, int reg)
  219. {
  220. uint64_t address =
  221. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  222. if (address)
  223. return cvmx_read64_uint8(address);
  224. else
  225. return 0xff;
  226. }
  227. /**
  228. * Read 16bits from a Device's config space
  229. *
  230. * @pcie_port: PCIe port the device is on
  231. * @bus: Sub bus
  232. * @dev: Device ID
  233. * @fn: Device sub function
  234. * @reg: Register to access
  235. *
  236. * Returns Result of the read
  237. */
  238. static uint16_t cvmx_pcie_config_read16(int pcie_port, int bus, int dev,
  239. int fn, int reg)
  240. {
  241. uint64_t address =
  242. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  243. if (address)
  244. return le16_to_cpu(cvmx_read64_uint16(address));
  245. else
  246. return 0xffff;
  247. }
  248. /**
  249. * Read 32bits from a Device's config space
  250. *
  251. * @pcie_port: PCIe port the device is on
  252. * @bus: Sub bus
  253. * @dev: Device ID
  254. * @fn: Device sub function
  255. * @reg: Register to access
  256. *
  257. * Returns Result of the read
  258. */
  259. static uint32_t cvmx_pcie_config_read32(int pcie_port, int bus, int dev,
  260. int fn, int reg)
  261. {
  262. uint64_t address =
  263. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  264. if (address)
  265. return le32_to_cpu(cvmx_read64_uint32(address));
  266. else
  267. return 0xffffffff;
  268. }
  269. /**
  270. * Write 8bits to a Device's config space
  271. *
  272. * @pcie_port: PCIe port the device is on
  273. * @bus: Sub bus
  274. * @dev: Device ID
  275. * @fn: Device sub function
  276. * @reg: Register to access
  277. * @val: Value to write
  278. */
  279. static void cvmx_pcie_config_write8(int pcie_port, int bus, int dev, int fn,
  280. int reg, uint8_t val)
  281. {
  282. uint64_t address =
  283. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  284. if (address)
  285. cvmx_write64_uint8(address, val);
  286. }
  287. /**
  288. * Write 16bits to a Device's config space
  289. *
  290. * @pcie_port: PCIe port the device is on
  291. * @bus: Sub bus
  292. * @dev: Device ID
  293. * @fn: Device sub function
  294. * @reg: Register to access
  295. * @val: Value to write
  296. */
  297. static void cvmx_pcie_config_write16(int pcie_port, int bus, int dev, int fn,
  298. int reg, uint16_t val)
  299. {
  300. uint64_t address =
  301. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  302. if (address)
  303. cvmx_write64_uint16(address, cpu_to_le16(val));
  304. }
  305. /**
  306. * Write 32bits to a Device's config space
  307. *
  308. * @pcie_port: PCIe port the device is on
  309. * @bus: Sub bus
  310. * @dev: Device ID
  311. * @fn: Device sub function
  312. * @reg: Register to access
  313. * @val: Value to write
  314. */
  315. static void cvmx_pcie_config_write32(int pcie_port, int bus, int dev, int fn,
  316. int reg, uint32_t val)
  317. {
  318. uint64_t address =
  319. __cvmx_pcie_build_config_addr(pcie_port, bus, dev, fn, reg);
  320. if (address)
  321. cvmx_write64_uint32(address, cpu_to_le32(val));
  322. }
  323. /**
  324. * Initialize the RC config space CSRs
  325. *
  326. * @pcie_port: PCIe port to initialize
  327. */
  328. static void __cvmx_pcie_rc_initialize_config_space(int pcie_port)
  329. {
  330. union cvmx_pciercx_cfg030 pciercx_cfg030;
  331. union cvmx_npei_ctl_status2 npei_ctl_status2;
  332. union cvmx_pciercx_cfg070 pciercx_cfg070;
  333. union cvmx_pciercx_cfg001 pciercx_cfg001;
  334. union cvmx_pciercx_cfg032 pciercx_cfg032;
  335. union cvmx_pciercx_cfg006 pciercx_cfg006;
  336. union cvmx_pciercx_cfg008 pciercx_cfg008;
  337. union cvmx_pciercx_cfg009 pciercx_cfg009;
  338. union cvmx_pciercx_cfg010 pciercx_cfg010;
  339. union cvmx_pciercx_cfg011 pciercx_cfg011;
  340. union cvmx_pciercx_cfg035 pciercx_cfg035;
  341. union cvmx_pciercx_cfg075 pciercx_cfg075;
  342. union cvmx_pciercx_cfg034 pciercx_cfg034;
  343. /* Max Payload Size (PCIE*_CFG030[MPS]) */
  344. /* Max Read Request Size (PCIE*_CFG030[MRRS]) */
  345. /* Relaxed-order, no-snoop enables (PCIE*_CFG030[RO_EN,NS_EN] */
  346. /* Error Message Enables (PCIE*_CFG030[CE_EN,NFE_EN,FE_EN,UR_EN]) */
  347. pciercx_cfg030.u32 =
  348. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG030(pcie_port));
  349. /*
  350. * Max payload size = 128 bytes for best Octeon DMA
  351. * performance.
  352. */
  353. pciercx_cfg030.s.mps = 0;
  354. /*
  355. * Max read request size = 128 bytes for best Octeon DMA
  356. * performance.
  357. */
  358. pciercx_cfg030.s.mrrs = 0;
  359. /* Enable relaxed ordering. */
  360. pciercx_cfg030.s.ro_en = 1;
  361. /* Enable no snoop. */
  362. pciercx_cfg030.s.ns_en = 1;
  363. /* Correctable error reporting enable. */
  364. pciercx_cfg030.s.ce_en = 1;
  365. /* Non-fatal error reporting enable. */
  366. pciercx_cfg030.s.nfe_en = 1;
  367. /* Fatal error reporting enable. */
  368. pciercx_cfg030.s.fe_en = 1;
  369. /* Unsupported request reporting enable. */
  370. pciercx_cfg030.s.ur_en = 1;
  371. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG030(pcie_port),
  372. pciercx_cfg030.u32);
  373. /*
  374. * Max Payload Size (NPEI_CTL_STATUS2[MPS]) must match
  375. * PCIE*_CFG030[MPS]
  376. *
  377. * Max Read Request Size (NPEI_CTL_STATUS2[MRRS]) must not
  378. * exceed PCIE*_CFG030[MRRS].
  379. */
  380. npei_ctl_status2.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_STATUS2);
  381. /* Max payload size = 128 bytes for best Octeon DMA performance */
  382. npei_ctl_status2.s.mps = 0;
  383. /* Max read request size = 128 bytes for best Octeon DMA performance */
  384. npei_ctl_status2.s.mrrs = 0;
  385. if (pcie_port)
  386. npei_ctl_status2.s.c1_b1_s = 3; /* Port1 BAR1 Size 256MB */
  387. else
  388. npei_ctl_status2.s.c0_b1_s = 3; /* Port0 BAR1 Size 256MB */
  389. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_STATUS2, npei_ctl_status2.u64);
  390. /* ECRC Generation (PCIE*_CFG070[GE,CE]) */
  391. pciercx_cfg070.u32 =
  392. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG070(pcie_port));
  393. pciercx_cfg070.s.ge = 1; /* ECRC generation enable. */
  394. pciercx_cfg070.s.ce = 1; /* ECRC check enable. */
  395. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG070(pcie_port),
  396. pciercx_cfg070.u32);
  397. /*
  398. * Access Enables (PCIE*_CFG001[MSAE,ME]) ME and MSAE should
  399. * always be set.
  400. *
  401. * Interrupt Disable (PCIE*_CFG001[I_DIS]) System Error
  402. * Message Enable (PCIE*_CFG001[SEE])
  403. */
  404. pciercx_cfg001.u32 =
  405. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG001(pcie_port));
  406. pciercx_cfg001.s.msae = 1; /* Memory space enable. */
  407. pciercx_cfg001.s.me = 1; /* Bus master enable. */
  408. pciercx_cfg001.s.i_dis = 1; /* INTx assertion disable. */
  409. pciercx_cfg001.s.see = 1; /* SERR# enable */
  410. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG001(pcie_port),
  411. pciercx_cfg001.u32);
  412. /* Advanced Error Recovery Message Enables */
  413. /* (PCIE*_CFG066,PCIE*_CFG067,PCIE*_CFG069) */
  414. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG066(pcie_port), 0);
  415. /* Use CVMX_PCIERCX_CFG067 hardware default */
  416. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG069(pcie_port), 0);
  417. /* Active State Power Management (PCIE*_CFG032[ASLPC]) */
  418. pciercx_cfg032.u32 =
  419. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG032(pcie_port));
  420. pciercx_cfg032.s.aslpc = 0; /* Active state Link PM control. */
  421. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG032(pcie_port),
  422. pciercx_cfg032.u32);
  423. /* Entrance Latencies (PCIE*_CFG451[L0EL,L1EL]) */
  424. /*
  425. * Link Width Mode (PCIERCn_CFG452[LME]) - Set during
  426. * cvmx_pcie_rc_initialize_link()
  427. *
  428. * Primary Bus Number (PCIERCn_CFG006[PBNUM])
  429. *
  430. * We set the primary bus number to 1 so IDT bridges are
  431. * happy. They don't like zero.
  432. */
  433. pciercx_cfg006.u32 = 0;
  434. pciercx_cfg006.s.pbnum = 1;
  435. pciercx_cfg006.s.sbnum = 1;
  436. pciercx_cfg006.s.subbnum = 1;
  437. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG006(pcie_port),
  438. pciercx_cfg006.u32);
  439. /*
  440. * Memory-mapped I/O BAR (PCIERCn_CFG008)
  441. * Most applications should disable the memory-mapped I/O BAR by
  442. * setting PCIERCn_CFG008[ML_ADDR] < PCIERCn_CFG008[MB_ADDR]
  443. */
  444. pciercx_cfg008.u32 = 0;
  445. pciercx_cfg008.s.mb_addr = 0x100;
  446. pciercx_cfg008.s.ml_addr = 0;
  447. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG008(pcie_port),
  448. pciercx_cfg008.u32);
  449. /*
  450. * Prefetchable BAR (PCIERCn_CFG009,PCIERCn_CFG010,PCIERCn_CFG011)
  451. * Most applications should disable the prefetchable BAR by setting
  452. * PCIERCn_CFG011[UMEM_LIMIT],PCIERCn_CFG009[LMEM_LIMIT] <
  453. * PCIERCn_CFG010[UMEM_BASE],PCIERCn_CFG009[LMEM_BASE]
  454. */
  455. pciercx_cfg009.u32 =
  456. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG009(pcie_port));
  457. pciercx_cfg010.u32 =
  458. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG010(pcie_port));
  459. pciercx_cfg011.u32 =
  460. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG011(pcie_port));
  461. pciercx_cfg009.s.lmem_base = 0x100;
  462. pciercx_cfg009.s.lmem_limit = 0;
  463. pciercx_cfg010.s.umem_base = 0x100;
  464. pciercx_cfg011.s.umem_limit = 0;
  465. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG009(pcie_port),
  466. pciercx_cfg009.u32);
  467. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG010(pcie_port),
  468. pciercx_cfg010.u32);
  469. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG011(pcie_port),
  470. pciercx_cfg011.u32);
  471. /*
  472. * System Error Interrupt Enables (PCIERCn_CFG035[SECEE,SEFEE,SENFEE])
  473. * PME Interrupt Enables (PCIERCn_CFG035[PMEIE])
  474. */
  475. pciercx_cfg035.u32 =
  476. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG035(pcie_port));
  477. /* System error on correctable error enable. */
  478. pciercx_cfg035.s.secee = 1;
  479. /* System error on fatal error enable. */
  480. pciercx_cfg035.s.sefee = 1;
  481. /* System error on non-fatal error enable. */
  482. pciercx_cfg035.s.senfee = 1;
  483. /* PME interrupt enable. */
  484. pciercx_cfg035.s.pmeie = 1;
  485. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG035(pcie_port),
  486. pciercx_cfg035.u32);
  487. /*
  488. * Advanced Error Recovery Interrupt Enables
  489. * (PCIERCn_CFG075[CERE,NFERE,FERE])
  490. */
  491. pciercx_cfg075.u32 =
  492. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG075(pcie_port));
  493. /* Correctable error reporting enable. */
  494. pciercx_cfg075.s.cere = 1;
  495. /* Non-fatal error reporting enable. */
  496. pciercx_cfg075.s.nfere = 1;
  497. /* Fatal error reporting enable. */
  498. pciercx_cfg075.s.fere = 1;
  499. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG075(pcie_port),
  500. pciercx_cfg075.u32);
  501. /* HP Interrupt Enables (PCIERCn_CFG034[HPINT_EN],
  502. * PCIERCn_CFG034[DLLS_EN,CCINT_EN])
  503. */
  504. pciercx_cfg034.u32 =
  505. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG034(pcie_port));
  506. /* Hot-plug interrupt enable. */
  507. pciercx_cfg034.s.hpint_en = 1;
  508. /* Data Link Layer state changed enable */
  509. pciercx_cfg034.s.dlls_en = 1;
  510. /* Command completed interrupt enable. */
  511. pciercx_cfg034.s.ccint_en = 1;
  512. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG034(pcie_port),
  513. pciercx_cfg034.u32);
  514. }
  515. /**
  516. * Initialize a host mode PCIe link. This function takes a PCIe
  517. * port from reset to a link up state. Software can then begin
  518. * configuring the rest of the link.
  519. *
  520. * @pcie_port: PCIe port to initialize
  521. *
  522. * Returns Zero on success
  523. */
  524. static int __cvmx_pcie_rc_initialize_link(int pcie_port)
  525. {
  526. uint64_t start_cycle;
  527. union cvmx_pescx_ctl_status pescx_ctl_status;
  528. union cvmx_pciercx_cfg452 pciercx_cfg452;
  529. union cvmx_pciercx_cfg032 pciercx_cfg032;
  530. union cvmx_pciercx_cfg448 pciercx_cfg448;
  531. /* Set the lane width */
  532. pciercx_cfg452.u32 =
  533. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG452(pcie_port));
  534. pescx_ctl_status.u64 = cvmx_read_csr(CVMX_PESCX_CTL_STATUS(pcie_port));
  535. if (pescx_ctl_status.s.qlm_cfg == 0) {
  536. /* We're in 8 lane (56XX) or 4 lane (54XX) mode */
  537. pciercx_cfg452.s.lme = 0xf;
  538. } else {
  539. /* We're in 4 lane (56XX) or 2 lane (52XX) mode */
  540. pciercx_cfg452.s.lme = 0x7;
  541. }
  542. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG452(pcie_port),
  543. pciercx_cfg452.u32);
  544. /*
  545. * CN52XX pass 1.x has an errata where length mismatches on UR
  546. * responses can cause bus errors on 64bit memory
  547. * reads. Turning off length error checking fixes this.
  548. */
  549. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  550. union cvmx_pciercx_cfg455 pciercx_cfg455;
  551. pciercx_cfg455.u32 =
  552. cvmx_pcie_cfgx_read(pcie_port,
  553. CVMX_PCIERCX_CFG455(pcie_port));
  554. pciercx_cfg455.s.m_cpl_len_err = 1;
  555. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG455(pcie_port),
  556. pciercx_cfg455.u32);
  557. }
  558. /* Lane swap needs to be manually enabled for CN52XX */
  559. if (OCTEON_IS_MODEL(OCTEON_CN52XX) && (pcie_port == 1)) {
  560. pescx_ctl_status.s.lane_swp = 1;
  561. cvmx_write_csr(CVMX_PESCX_CTL_STATUS(pcie_port),
  562. pescx_ctl_status.u64);
  563. }
  564. /* Bring up the link */
  565. pescx_ctl_status.u64 = cvmx_read_csr(CVMX_PESCX_CTL_STATUS(pcie_port));
  566. pescx_ctl_status.s.lnk_enb = 1;
  567. cvmx_write_csr(CVMX_PESCX_CTL_STATUS(pcie_port), pescx_ctl_status.u64);
  568. /*
  569. * CN52XX pass 1.0: Due to a bug in 2nd order CDR, it needs to
  570. * be disabled.
  571. */
  572. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_0))
  573. __cvmx_helper_errata_qlm_disable_2nd_order_cdr(0);
  574. /* Wait for the link to come up */
  575. cvmx_dprintf("PCIe: Waiting for port %d link\n", pcie_port);
  576. start_cycle = cvmx_get_cycle();
  577. do {
  578. if (cvmx_get_cycle() - start_cycle >
  579. 2 * cvmx_sysinfo_get()->cpu_clock_hz) {
  580. cvmx_dprintf("PCIe: Port %d link timeout\n",
  581. pcie_port);
  582. return -1;
  583. }
  584. cvmx_wait(10000);
  585. pciercx_cfg032.u32 =
  586. cvmx_pcie_cfgx_read(pcie_port,
  587. CVMX_PCIERCX_CFG032(pcie_port));
  588. } while (pciercx_cfg032.s.dlla == 0);
  589. /* Display the link status */
  590. cvmx_dprintf("PCIe: Port %d link active, %d lanes\n", pcie_port,
  591. pciercx_cfg032.s.nlw);
  592. /*
  593. * Update the Replay Time Limit. Empirically, some PCIe
  594. * devices take a little longer to respond than expected under
  595. * load. As a workaround for this we configure the Replay Time
  596. * Limit to the value expected for a 512 byte MPS instead of
  597. * our actual 256 byte MPS. The numbers below are directly
  598. * from the PCIe spec table 3-4.
  599. */
  600. pciercx_cfg448.u32 =
  601. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG448(pcie_port));
  602. switch (pciercx_cfg032.s.nlw) {
  603. case 1: /* 1 lane */
  604. pciercx_cfg448.s.rtl = 1677;
  605. break;
  606. case 2: /* 2 lanes */
  607. pciercx_cfg448.s.rtl = 867;
  608. break;
  609. case 4: /* 4 lanes */
  610. pciercx_cfg448.s.rtl = 462;
  611. break;
  612. case 8: /* 8 lanes */
  613. pciercx_cfg448.s.rtl = 258;
  614. break;
  615. }
  616. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG448(pcie_port),
  617. pciercx_cfg448.u32);
  618. return 0;
  619. }
  620. /**
  621. * Initialize a PCIe port for use in host(RC) mode. It doesn't
  622. * enumerate the bus.
  623. *
  624. * @pcie_port: PCIe port to initialize
  625. *
  626. * Returns Zero on success
  627. */
  628. static int cvmx_pcie_rc_initialize(int pcie_port)
  629. {
  630. int i;
  631. int base;
  632. u64 addr_swizzle;
  633. union cvmx_ciu_soft_prst ciu_soft_prst;
  634. union cvmx_pescx_bist_status pescx_bist_status;
  635. union cvmx_pescx_bist_status2 pescx_bist_status2;
  636. union cvmx_npei_ctl_status npei_ctl_status;
  637. union cvmx_npei_mem_access_ctl npei_mem_access_ctl;
  638. union cvmx_npei_mem_access_subidx mem_access_subid;
  639. union cvmx_npei_dbg_data npei_dbg_data;
  640. union cvmx_pescx_ctl_status2 pescx_ctl_status2;
  641. union cvmx_npei_bar1_indexx bar1_index;
  642. /*
  643. * Make sure we aren't trying to setup a target mode interface
  644. * in host mode.
  645. */
  646. npei_ctl_status.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_STATUS);
  647. if ((pcie_port == 0) && !npei_ctl_status.s.host_mode) {
  648. cvmx_dprintf("PCIe: ERROR: cvmx_pcie_rc_initialize() called "
  649. "on port0, but port0 is not in host mode\n");
  650. return -1;
  651. }
  652. /*
  653. * Make sure a CN52XX isn't trying to bring up port 1 when it
  654. * is disabled.
  655. */
  656. if (OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  657. npei_dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  658. if ((pcie_port == 1) && npei_dbg_data.cn52xx.qlm0_link_width) {
  659. cvmx_dprintf("PCIe: ERROR: cvmx_pcie_rc_initialize() "
  660. "called on port1, but port1 is "
  661. "disabled\n");
  662. return -1;
  663. }
  664. }
  665. /*
  666. * PCIe switch arbitration mode. '0' == fixed priority NPEI,
  667. * PCIe0, then PCIe1. '1' == round robin.
  668. */
  669. npei_ctl_status.s.arb = 1;
  670. /* Allow up to 0x20 config retries */
  671. npei_ctl_status.s.cfg_rtry = 0x20;
  672. /*
  673. * CN52XX pass1.x has an errata where P0_NTAGS and P1_NTAGS
  674. * don't reset.
  675. */
  676. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  677. npei_ctl_status.s.p0_ntags = 0x20;
  678. npei_ctl_status.s.p1_ntags = 0x20;
  679. }
  680. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_STATUS, npei_ctl_status.u64);
  681. /* Bring the PCIe out of reset */
  682. if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_EBH5200) {
  683. /*
  684. * The EBH5200 board swapped the PCIe reset lines on
  685. * the board. As a workaround for this bug, we bring
  686. * both PCIe ports out of reset at the same time
  687. * instead of on separate calls. So for port 0, we
  688. * bring both out of reset and do nothing on port 1.
  689. */
  690. if (pcie_port == 0) {
  691. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  692. /*
  693. * After a chip reset the PCIe will also be in
  694. * reset. If it isn't, most likely someone is
  695. * trying to init it again without a proper
  696. * PCIe reset.
  697. */
  698. if (ciu_soft_prst.s.soft_prst == 0) {
  699. /* Reset the ports */
  700. ciu_soft_prst.s.soft_prst = 1;
  701. cvmx_write_csr(CVMX_CIU_SOFT_PRST,
  702. ciu_soft_prst.u64);
  703. ciu_soft_prst.u64 =
  704. cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  705. ciu_soft_prst.s.soft_prst = 1;
  706. cvmx_write_csr(CVMX_CIU_SOFT_PRST1,
  707. ciu_soft_prst.u64);
  708. /* Wait until pcie resets the ports. */
  709. udelay(2000);
  710. }
  711. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  712. ciu_soft_prst.s.soft_prst = 0;
  713. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  714. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  715. ciu_soft_prst.s.soft_prst = 0;
  716. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  717. }
  718. } else {
  719. /*
  720. * The normal case: The PCIe ports are completely
  721. * separate and can be brought out of reset
  722. * independently.
  723. */
  724. if (pcie_port)
  725. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  726. else
  727. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  728. /*
  729. * After a chip reset the PCIe will also be in
  730. * reset. If it isn't, most likely someone is trying
  731. * to init it again without a proper PCIe reset.
  732. */
  733. if (ciu_soft_prst.s.soft_prst == 0) {
  734. /* Reset the port */
  735. ciu_soft_prst.s.soft_prst = 1;
  736. if (pcie_port)
  737. cvmx_write_csr(CVMX_CIU_SOFT_PRST1,
  738. ciu_soft_prst.u64);
  739. else
  740. cvmx_write_csr(CVMX_CIU_SOFT_PRST,
  741. ciu_soft_prst.u64);
  742. /* Wait until pcie resets the ports. */
  743. udelay(2000);
  744. }
  745. if (pcie_port) {
  746. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  747. ciu_soft_prst.s.soft_prst = 0;
  748. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  749. } else {
  750. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  751. ciu_soft_prst.s.soft_prst = 0;
  752. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  753. }
  754. }
  755. /*
  756. * Wait for PCIe reset to complete. Due to errata PCIE-700, we
  757. * don't poll PESCX_CTL_STATUS2[PCIERST], but simply wait a
  758. * fixed number of cycles.
  759. */
  760. cvmx_wait(400000);
  761. /* PESCX_BIST_STATUS2[PCLK_RUN] was missing on pass 1 of CN56XX and
  762. CN52XX, so we only probe it on newer chips */
  763. if (!OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X)
  764. && !OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  765. /* Clear PCLK_RUN so we can check if the clock is running */
  766. pescx_ctl_status2.u64 =
  767. cvmx_read_csr(CVMX_PESCX_CTL_STATUS2(pcie_port));
  768. pescx_ctl_status2.s.pclk_run = 1;
  769. cvmx_write_csr(CVMX_PESCX_CTL_STATUS2(pcie_port),
  770. pescx_ctl_status2.u64);
  771. /*
  772. * Now that we cleared PCLK_RUN, wait for it to be set
  773. * again telling us the clock is running.
  774. */
  775. if (CVMX_WAIT_FOR_FIELD64(CVMX_PESCX_CTL_STATUS2(pcie_port),
  776. union cvmx_pescx_ctl_status2,
  777. pclk_run, ==, 1, 10000)) {
  778. cvmx_dprintf("PCIe: Port %d isn't clocked, skipping.\n",
  779. pcie_port);
  780. return -1;
  781. }
  782. }
  783. /*
  784. * Check and make sure PCIe came out of reset. If it doesn't
  785. * the board probably hasn't wired the clocks up and the
  786. * interface should be skipped.
  787. */
  788. pescx_ctl_status2.u64 =
  789. 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",
  792. pcie_port);
  793. return -1;
  794. }
  795. /*
  796. * Check BIST2 status. If any bits are set skip this interface. This
  797. * is an attempt to catch PCIE-813 on pass 1 parts.
  798. */
  799. pescx_bist_status2.u64 =
  800. cvmx_read_csr(CVMX_PESCX_BIST_STATUS2(pcie_port));
  801. if (pescx_bist_status2.u64) {
  802. cvmx_dprintf("PCIe: Port %d BIST2 failed. Most likely this "
  803. "port isn't hooked up, skipping.\n",
  804. pcie_port);
  805. return -1;
  806. }
  807. /* Check BIST status */
  808. pescx_bist_status.u64 =
  809. cvmx_read_csr(CVMX_PESCX_BIST_STATUS(pcie_port));
  810. if (pescx_bist_status.u64)
  811. cvmx_dprintf("PCIe: BIST FAILED for port %d (0x%016llx)\n",
  812. pcie_port, CAST64(pescx_bist_status.u64));
  813. /* Initialize the config space CSRs */
  814. __cvmx_pcie_rc_initialize_config_space(pcie_port);
  815. /* Bring the link up */
  816. if (__cvmx_pcie_rc_initialize_link(pcie_port)) {
  817. cvmx_dprintf
  818. ("PCIe: ERROR: cvmx_pcie_rc_initialize_link() failed\n");
  819. return -1;
  820. }
  821. /* Store merge control (NPEI_MEM_ACCESS_CTL[TIMER,MAX_WORD]) */
  822. npei_mem_access_ctl.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_MEM_ACCESS_CTL);
  823. /* Allow 16 words to combine */
  824. npei_mem_access_ctl.s.max_word = 0;
  825. /* Wait up to 127 cycles for more data */
  826. npei_mem_access_ctl.s.timer = 127;
  827. cvmx_write_csr(CVMX_PEXP_NPEI_MEM_ACCESS_CTL, npei_mem_access_ctl.u64);
  828. /* Setup Mem access SubDIDs */
  829. mem_access_subid.u64 = 0;
  830. /* Port the request is sent to. */
  831. mem_access_subid.s.port = pcie_port;
  832. /* Due to an errata on pass 1 chips, no merging is allowed. */
  833. mem_access_subid.s.nmerge = 1;
  834. /* Endian-swap for Reads. */
  835. mem_access_subid.s.esr = 1;
  836. /* Endian-swap for Writes. */
  837. mem_access_subid.s.esw = 1;
  838. /* No Snoop for Reads. */
  839. mem_access_subid.s.nsr = 1;
  840. /* No Snoop for Writes. */
  841. mem_access_subid.s.nsw = 1;
  842. /* Disable Relaxed Ordering for Reads. */
  843. mem_access_subid.s.ror = 0;
  844. /* Disable Relaxed Ordering for Writes. */
  845. mem_access_subid.s.row = 0;
  846. /* PCIe Adddress Bits <63:34>. */
  847. mem_access_subid.s.ba = 0;
  848. /*
  849. * Setup mem access 12-15 for port 0, 16-19 for port 1,
  850. * supplying 36 bits of address space.
  851. */
  852. for (i = 12 + pcie_port * 4; i < 16 + pcie_port * 4; i++) {
  853. cvmx_write_csr(CVMX_PEXP_NPEI_MEM_ACCESS_SUBIDX(i),
  854. mem_access_subid.u64);
  855. /* Set each SUBID to extend the addressable range */
  856. mem_access_subid.s.ba += 1;
  857. }
  858. /*
  859. * Disable the peer to peer forwarding register. This must be
  860. * setup by the OS after it enumerates the bus and assigns
  861. * addresses to the PCIe busses.
  862. */
  863. for (i = 0; i < 4; i++) {
  864. cvmx_write_csr(CVMX_PESCX_P2P_BARX_START(i, pcie_port), -1);
  865. cvmx_write_csr(CVMX_PESCX_P2P_BARX_END(i, pcie_port), -1);
  866. }
  867. /* Set Octeon's BAR0 to decode 0-16KB. It overlaps with Bar2 */
  868. cvmx_write_csr(CVMX_PESCX_P2N_BAR0_START(pcie_port), 0);
  869. /* BAR1 follows BAR2 with a gap. */
  870. cvmx_write_csr(CVMX_PESCX_P2N_BAR1_START(pcie_port), CVMX_PCIE_BAR1_RC_BASE);
  871. bar1_index.u32 = 0;
  872. bar1_index.s.addr_idx = (CVMX_PCIE_BAR1_PHYS_BASE >> 22);
  873. bar1_index.s.ca = 1; /* Not Cached */
  874. bar1_index.s.end_swp = 1; /* Endian Swap mode */
  875. bar1_index.s.addr_v = 1; /* Valid entry */
  876. base = pcie_port ? 16 : 0;
  877. /* Big endian swizzle for 32-bit PEXP_NCB register. */
  878. #ifdef __MIPSEB__
  879. addr_swizzle = 4;
  880. #else
  881. addr_swizzle = 0;
  882. #endif
  883. for (i = 0; i < 16; i++) {
  884. cvmx_write64_uint32((CVMX_PEXP_NPEI_BAR1_INDEXX(base) ^ addr_swizzle),
  885. bar1_index.u32);
  886. base++;
  887. /* 256MB / 16 >> 22 == 4 */
  888. bar1_index.s.addr_idx += (((1ull << 28) / 16ull) >> 22);
  889. }
  890. /*
  891. * Set Octeon's BAR2 to decode 0-2^39. Bar0 and Bar1 take
  892. * precedence where they overlap. It also overlaps with the
  893. * device addresses, so make sure the peer to peer forwarding
  894. * is set right.
  895. */
  896. cvmx_write_csr(CVMX_PESCX_P2N_BAR2_START(pcie_port), 0);
  897. /*
  898. * Setup BAR2 attributes
  899. *
  900. * Relaxed Ordering (NPEI_CTL_PORTn[PTLP_RO,CTLP_RO, WAIT_COM])
  901. * - PTLP_RO,CTLP_RO should normally be set (except for debug).
  902. * - WAIT_COM=0 will likely work for all applications.
  903. *
  904. * Load completion relaxed ordering (NPEI_CTL_PORTn[WAITL_COM]).
  905. */
  906. if (pcie_port) {
  907. union cvmx_npei_ctl_port1 npei_ctl_port;
  908. npei_ctl_port.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_PORT1);
  909. npei_ctl_port.s.bar2_enb = 1;
  910. npei_ctl_port.s.bar2_esx = 1;
  911. npei_ctl_port.s.bar2_cax = 0;
  912. npei_ctl_port.s.ptlp_ro = 1;
  913. npei_ctl_port.s.ctlp_ro = 1;
  914. npei_ctl_port.s.wait_com = 0;
  915. npei_ctl_port.s.waitl_com = 0;
  916. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_PORT1, npei_ctl_port.u64);
  917. } else {
  918. union cvmx_npei_ctl_port0 npei_ctl_port;
  919. npei_ctl_port.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_PORT0);
  920. npei_ctl_port.s.bar2_enb = 1;
  921. npei_ctl_port.s.bar2_esx = 1;
  922. npei_ctl_port.s.bar2_cax = 0;
  923. npei_ctl_port.s.ptlp_ro = 1;
  924. npei_ctl_port.s.ctlp_ro = 1;
  925. npei_ctl_port.s.wait_com = 0;
  926. npei_ctl_port.s.waitl_com = 0;
  927. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_PORT0, npei_ctl_port.u64);
  928. }
  929. return 0;
  930. }
  931. /* Above was cvmx-pcie.c, below original pcie.c */
  932. /**
  933. * Map a PCI device to the appropriate interrupt line
  934. *
  935. * @dev: The Linux PCI device structure for the device to map
  936. * @slot: The slot number for this device on __BUS 0__. Linux
  937. * enumerates through all the bridges and figures out the
  938. * slot on Bus 0 where this device eventually hooks to.
  939. * @pin: The PCI interrupt pin read from the device, then swizzled
  940. * as it goes through each bridge.
  941. * Returns Interrupt number for the device
  942. */
  943. int __init octeon_pcie_pcibios_map_irq(const struct pci_dev *dev,
  944. u8 slot, u8 pin)
  945. {
  946. /*
  947. * The EBH5600 board with the PCI to PCIe bridge mistakenly
  948. * wires the first slot for both device id 2 and interrupt
  949. * A. According to the PCI spec, device id 2 should be C. The
  950. * following kludge attempts to fix this.
  951. */
  952. if (strstr(octeon_board_type_string(), "EBH5600") &&
  953. dev->bus && dev->bus->parent) {
  954. /*
  955. * Iterate all the way up the device chain and find
  956. * the root bus.
  957. */
  958. while (dev->bus && dev->bus->parent)
  959. dev = to_pci_dev(dev->bus->bridge);
  960. /* If the root bus is number 0 and the PEX 8114 is the
  961. * root, assume we are behind the miswired bus. We
  962. * need to correct the swizzle level by two. Yuck.
  963. */
  964. if ((dev->bus->number == 0) &&
  965. (dev->vendor == 0x10b5) && (dev->device == 0x8114)) {
  966. /*
  967. * The pin field is one based, not zero. We
  968. * need to swizzle it by minus two.
  969. */
  970. pin = ((pin - 3) & 3) + 1;
  971. }
  972. }
  973. /*
  974. * The -1 is because pin starts with one, not zero. It might
  975. * be that this equation needs to include the slot number, but
  976. * I don't have hardware to check that against.
  977. */
  978. return pin - 1 + OCTEON_IRQ_PCI_INT0;
  979. }
  980. /**
  981. * Read a value from configuration space
  982. *
  983. * @bus:
  984. * @devfn:
  985. * @reg:
  986. * @size:
  987. * @val:
  988. * Returns
  989. */
  990. static inline int octeon_pcie_read_config(int pcie_port, struct pci_bus *bus,
  991. unsigned int devfn, int reg, int size,
  992. u32 *val)
  993. {
  994. union octeon_cvmemctl cvmmemctl;
  995. union octeon_cvmemctl cvmmemctl_save;
  996. int bus_number = bus->number;
  997. /*
  998. * For the top level bus make sure our hardware bus number
  999. * matches the software one.
  1000. */
  1001. if (bus->parent == NULL) {
  1002. union cvmx_pciercx_cfg006 pciercx_cfg006;
  1003. pciercx_cfg006.u32 = cvmx_pcie_cfgx_read(pcie_port,
  1004. CVMX_PCIERCX_CFG006(pcie_port));
  1005. if (pciercx_cfg006.s.pbnum != bus_number) {
  1006. pciercx_cfg006.s.pbnum = bus_number;
  1007. pciercx_cfg006.s.sbnum = bus_number;
  1008. pciercx_cfg006.s.subbnum = bus_number;
  1009. cvmx_pcie_cfgx_write(pcie_port,
  1010. CVMX_PCIERCX_CFG006(pcie_port),
  1011. pciercx_cfg006.u32);
  1012. }
  1013. }
  1014. /*
  1015. * PCIe only has a single device connected to Octeon. It is
  1016. * always device ID 0. Don't bother doing reads for other
  1017. * device IDs on the first segment.
  1018. */
  1019. if ((bus->parent == NULL) && (devfn >> 3 != 0))
  1020. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1021. /*
  1022. * The following is a workaround for the CN57XX, CN56XX,
  1023. * CN55XX, and CN54XX errata with PCIe config reads from non
  1024. * existent devices. These chips will hang the PCIe link if a
  1025. * config read is performed that causes a UR response.
  1026. */
  1027. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1) ||
  1028. OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_1)) {
  1029. /*
  1030. * For our EBH5600 board, port 0 has a bridge with two
  1031. * PCI-X slots. We need a new special checks to make
  1032. * sure we only probe valid stuff. The PCIe->PCI-X
  1033. * bridge only respondes to device ID 0, function
  1034. * 0-1
  1035. */
  1036. if ((bus->parent == NULL) && (devfn >= 2))
  1037. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1038. /*
  1039. * The PCI-X slots are device ID 2,3. Choose one of
  1040. * the below "if" blocks based on what is plugged into
  1041. * the board.
  1042. */
  1043. #if 1
  1044. /* Use this option if you aren't using either slot */
  1045. if (bus_number == 1)
  1046. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1047. #elif 0
  1048. /*
  1049. * Use this option if you are using the first slot but
  1050. * not the second.
  1051. */
  1052. if ((bus_number == 1) && (devfn >> 3 != 2))
  1053. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1054. #elif 0
  1055. /*
  1056. * Use this option if you are using the second slot
  1057. * but not the first.
  1058. */
  1059. if ((bus_number == 1) && (devfn >> 3 != 3))
  1060. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1061. #elif 0
  1062. /* Use this opion if you are using both slots */
  1063. if ((bus_number == 1) &&
  1064. !((devfn == (2 << 3)) || (devfn == (3 << 3))))
  1065. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1066. #endif
  1067. /*
  1068. * Shorten the DID timeout so bus errors for PCIe
  1069. * config reads from non existent devices happen
  1070. * faster. This allows us to continue booting even if
  1071. * the above "if" checks are wrong. Once one of these
  1072. * errors happens, the PCIe port is dead.
  1073. */
  1074. cvmmemctl_save.u64 = __read_64bit_c0_register($11, 7);
  1075. cvmmemctl.u64 = cvmmemctl_save.u64;
  1076. cvmmemctl.s.didtto = 2;
  1077. __write_64bit_c0_register($11, 7, cvmmemctl.u64);
  1078. }
  1079. switch (size) {
  1080. case 4:
  1081. *val = cvmx_pcie_config_read32(pcie_port, bus_number,
  1082. devfn >> 3, devfn & 0x7, reg);
  1083. break;
  1084. case 2:
  1085. *val = cvmx_pcie_config_read16(pcie_port, bus_number,
  1086. devfn >> 3, devfn & 0x7, reg);
  1087. break;
  1088. case 1:
  1089. *val = cvmx_pcie_config_read8(pcie_port, bus_number, devfn >> 3,
  1090. devfn & 0x7, reg);
  1091. break;
  1092. default:
  1093. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1094. }
  1095. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1) ||
  1096. OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_1))
  1097. __write_64bit_c0_register($11, 7, cvmmemctl_save.u64);
  1098. return PCIBIOS_SUCCESSFUL;
  1099. }
  1100. static int octeon_pcie0_read_config(struct pci_bus *bus, unsigned int devfn,
  1101. int reg, int size, u32 *val)
  1102. {
  1103. return octeon_pcie_read_config(0, bus, devfn, reg, size, val);
  1104. }
  1105. static int octeon_pcie1_read_config(struct pci_bus *bus, unsigned int devfn,
  1106. int reg, int size, u32 *val)
  1107. {
  1108. return octeon_pcie_read_config(1, bus, devfn, reg, size, val);
  1109. }
  1110. /**
  1111. * Write a value to PCI configuration space
  1112. *
  1113. * @bus:
  1114. * @devfn:
  1115. * @reg:
  1116. * @size:
  1117. * @val:
  1118. * Returns
  1119. */
  1120. static inline int octeon_pcie_write_config(int pcie_port, struct pci_bus *bus,
  1121. unsigned int devfn, int reg,
  1122. int size, u32 val)
  1123. {
  1124. int bus_number = bus->number;
  1125. switch (size) {
  1126. case 4:
  1127. cvmx_pcie_config_write32(pcie_port, bus_number, devfn >> 3,
  1128. devfn & 0x7, reg, val);
  1129. return PCIBIOS_SUCCESSFUL;
  1130. case 2:
  1131. cvmx_pcie_config_write16(pcie_port, bus_number, devfn >> 3,
  1132. devfn & 0x7, reg, val);
  1133. return PCIBIOS_SUCCESSFUL;
  1134. case 1:
  1135. cvmx_pcie_config_write8(pcie_port, bus_number, devfn >> 3,
  1136. devfn & 0x7, reg, val);
  1137. return PCIBIOS_SUCCESSFUL;
  1138. }
  1139. #if PCI_CONFIG_SPACE_DELAY
  1140. udelay(PCI_CONFIG_SPACE_DELAY);
  1141. #endif
  1142. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1143. }
  1144. static int octeon_pcie0_write_config(struct pci_bus *bus, unsigned int devfn,
  1145. int reg, int size, u32 val)
  1146. {
  1147. return octeon_pcie_write_config(0, bus, devfn, reg, size, val);
  1148. }
  1149. static int octeon_pcie1_write_config(struct pci_bus *bus, unsigned int devfn,
  1150. int reg, int size, u32 val)
  1151. {
  1152. return octeon_pcie_write_config(1, bus, devfn, reg, size, val);
  1153. }
  1154. static struct pci_ops octeon_pcie0_ops = {
  1155. octeon_pcie0_read_config,
  1156. octeon_pcie0_write_config,
  1157. };
  1158. static struct resource octeon_pcie0_mem_resource = {
  1159. .name = "Octeon PCIe0 MEM",
  1160. .flags = IORESOURCE_MEM,
  1161. };
  1162. static struct resource octeon_pcie0_io_resource = {
  1163. .name = "Octeon PCIe0 IO",
  1164. .flags = IORESOURCE_IO,
  1165. };
  1166. static struct pci_controller octeon_pcie0_controller = {
  1167. .pci_ops = &octeon_pcie0_ops,
  1168. .mem_resource = &octeon_pcie0_mem_resource,
  1169. .io_resource = &octeon_pcie0_io_resource,
  1170. };
  1171. static struct pci_ops octeon_pcie1_ops = {
  1172. octeon_pcie1_read_config,
  1173. octeon_pcie1_write_config,
  1174. };
  1175. static struct resource octeon_pcie1_mem_resource = {
  1176. .name = "Octeon PCIe1 MEM",
  1177. .flags = IORESOURCE_MEM,
  1178. };
  1179. static struct resource octeon_pcie1_io_resource = {
  1180. .name = "Octeon PCIe1 IO",
  1181. .flags = IORESOURCE_IO,
  1182. };
  1183. static struct pci_controller octeon_pcie1_controller = {
  1184. .pci_ops = &octeon_pcie1_ops,
  1185. .mem_resource = &octeon_pcie1_mem_resource,
  1186. .io_resource = &octeon_pcie1_io_resource,
  1187. };
  1188. /**
  1189. * Initialize the Octeon PCIe controllers
  1190. *
  1191. * Returns
  1192. */
  1193. static int __init octeon_pcie_setup(void)
  1194. {
  1195. union cvmx_npei_ctl_status npei_ctl_status;
  1196. int result;
  1197. /* These chips don't have PCIe */
  1198. if (!octeon_has_feature(OCTEON_FEATURE_PCIE))
  1199. return 0;
  1200. /* Point pcibios_map_irq() to the PCIe version of it */
  1201. octeon_pcibios_map_irq = octeon_pcie_pcibios_map_irq;
  1202. /* Use the PCIe based DMA mappings */
  1203. octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_PCIE;
  1204. /*
  1205. * PCIe I/O range. It is based on port 0 but includes up until
  1206. * port 1's end.
  1207. */
  1208. set_io_port_base(CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address(0)));
  1209. ioport_resource.start = 0;
  1210. ioport_resource.end =
  1211. cvmx_pcie_get_io_base_address(1) -
  1212. cvmx_pcie_get_io_base_address(0) + cvmx_pcie_get_io_size(1) - 1;
  1213. npei_ctl_status.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_STATUS);
  1214. if (npei_ctl_status.s.host_mode) {
  1215. pr_notice("PCIe: Initializing port 0\n");
  1216. result = cvmx_pcie_rc_initialize(0);
  1217. if (result == 0) {
  1218. /* Memory offsets are physical addresses */
  1219. octeon_pcie0_controller.mem_offset =
  1220. cvmx_pcie_get_mem_base_address(0);
  1221. /* IO offsets are Mips virtual addresses */
  1222. octeon_pcie0_controller.io_map_base =
  1223. CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address
  1224. (0));
  1225. octeon_pcie0_controller.io_offset = 0;
  1226. /*
  1227. * To keep things similar to PCI, we start
  1228. * device addresses at the same place as PCI
  1229. * uisng big bar support. This normally
  1230. * translates to 4GB-256MB, which is the same
  1231. * as most x86 PCs.
  1232. */
  1233. octeon_pcie0_controller.mem_resource->start =
  1234. cvmx_pcie_get_mem_base_address(0) +
  1235. (4ul << 30) - (OCTEON_PCI_BAR1_HOLE_SIZE << 20);
  1236. octeon_pcie0_controller.mem_resource->end =
  1237. cvmx_pcie_get_mem_base_address(0) +
  1238. cvmx_pcie_get_mem_size(0) - 1;
  1239. /*
  1240. * Ports must be above 16KB for the ISA bus
  1241. * filtering in the PCI-X to PCI bridge.
  1242. */
  1243. octeon_pcie0_controller.io_resource->start = 4 << 10;
  1244. octeon_pcie0_controller.io_resource->end =
  1245. cvmx_pcie_get_io_size(0) - 1;
  1246. register_pci_controller(&octeon_pcie0_controller);
  1247. }
  1248. } else {
  1249. pr_notice("PCIe: Port 0 in endpoint mode, skipping.\n");
  1250. }
  1251. /* Skip the 2nd port on CN52XX if port 0 is in 4 lane mode */
  1252. if (OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  1253. union cvmx_npei_dbg_data npei_dbg_data;
  1254. npei_dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  1255. if (npei_dbg_data.cn52xx.qlm0_link_width)
  1256. return 0;
  1257. }
  1258. pr_notice("PCIe: Initializing port 1\n");
  1259. result = cvmx_pcie_rc_initialize(1);
  1260. if (result == 0) {
  1261. /* Memory offsets are physical addresses */
  1262. octeon_pcie1_controller.mem_offset =
  1263. cvmx_pcie_get_mem_base_address(1);
  1264. /* IO offsets are Mips virtual addresses */
  1265. octeon_pcie1_controller.io_map_base =
  1266. CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address(1));
  1267. octeon_pcie1_controller.io_offset =
  1268. cvmx_pcie_get_io_base_address(1) -
  1269. cvmx_pcie_get_io_base_address(0);
  1270. /*
  1271. * To keep things similar to PCI, we start device
  1272. * addresses at the same place as PCI uisng big bar
  1273. * support. This normally translates to 4GB-256MB,
  1274. * which is the same as most x86 PCs.
  1275. */
  1276. octeon_pcie1_controller.mem_resource->start =
  1277. cvmx_pcie_get_mem_base_address(1) + (4ul << 30) -
  1278. (OCTEON_PCI_BAR1_HOLE_SIZE << 20);
  1279. octeon_pcie1_controller.mem_resource->end =
  1280. cvmx_pcie_get_mem_base_address(1) +
  1281. cvmx_pcie_get_mem_size(1) - 1;
  1282. /*
  1283. * Ports must be above 16KB for the ISA bus filtering
  1284. * in the PCI-X to PCI bridge.
  1285. */
  1286. octeon_pcie1_controller.io_resource->start =
  1287. cvmx_pcie_get_io_base_address(1) -
  1288. cvmx_pcie_get_io_base_address(0);
  1289. octeon_pcie1_controller.io_resource->end =
  1290. octeon_pcie1_controller.io_resource->start +
  1291. cvmx_pcie_get_io_size(1) - 1;
  1292. register_pci_controller(&octeon_pcie1_controller);
  1293. }
  1294. return 0;
  1295. }
  1296. arch_initcall(octeon_pcie_setup);