pcie-octeon.c 41 KB

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