pcie-octeon.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_STATUS2, npei_ctl_status2.u64);
  386. /* ECRC Generation (PCIE*_CFG070[GE,CE]) */
  387. pciercx_cfg070.u32 =
  388. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG070(pcie_port));
  389. pciercx_cfg070.s.ge = 1; /* ECRC generation enable. */
  390. pciercx_cfg070.s.ce = 1; /* ECRC check enable. */
  391. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG070(pcie_port),
  392. pciercx_cfg070.u32);
  393. /*
  394. * Access Enables (PCIE*_CFG001[MSAE,ME]) ME and MSAE should
  395. * always be set.
  396. *
  397. * Interrupt Disable (PCIE*_CFG001[I_DIS]) System Error
  398. * Message Enable (PCIE*_CFG001[SEE])
  399. */
  400. pciercx_cfg001.u32 =
  401. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG001(pcie_port));
  402. pciercx_cfg001.s.msae = 1; /* Memory space enable. */
  403. pciercx_cfg001.s.me = 1; /* Bus master enable. */
  404. pciercx_cfg001.s.i_dis = 1; /* INTx assertion disable. */
  405. pciercx_cfg001.s.see = 1; /* SERR# enable */
  406. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG001(pcie_port),
  407. pciercx_cfg001.u32);
  408. /* Advanced Error Recovery Message Enables */
  409. /* (PCIE*_CFG066,PCIE*_CFG067,PCIE*_CFG069) */
  410. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG066(pcie_port), 0);
  411. /* Use CVMX_PCIERCX_CFG067 hardware default */
  412. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG069(pcie_port), 0);
  413. /* Active State Power Management (PCIE*_CFG032[ASLPC]) */
  414. pciercx_cfg032.u32 =
  415. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG032(pcie_port));
  416. pciercx_cfg032.s.aslpc = 0; /* Active state Link PM control. */
  417. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG032(pcie_port),
  418. pciercx_cfg032.u32);
  419. /* Entrance Latencies (PCIE*_CFG451[L0EL,L1EL]) */
  420. /*
  421. * Link Width Mode (PCIERCn_CFG452[LME]) - Set during
  422. * cvmx_pcie_rc_initialize_link()
  423. *
  424. * Primary Bus Number (PCIERCn_CFG006[PBNUM])
  425. *
  426. * We set the primary bus number to 1 so IDT bridges are
  427. * happy. They don't like zero.
  428. */
  429. pciercx_cfg006.u32 = 0;
  430. pciercx_cfg006.s.pbnum = 1;
  431. pciercx_cfg006.s.sbnum = 1;
  432. pciercx_cfg006.s.subbnum = 1;
  433. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG006(pcie_port),
  434. pciercx_cfg006.u32);
  435. /*
  436. * Memory-mapped I/O BAR (PCIERCn_CFG008)
  437. * Most applications should disable the memory-mapped I/O BAR by
  438. * setting PCIERCn_CFG008[ML_ADDR] < PCIERCn_CFG008[MB_ADDR]
  439. */
  440. pciercx_cfg008.u32 = 0;
  441. pciercx_cfg008.s.mb_addr = 0x100;
  442. pciercx_cfg008.s.ml_addr = 0;
  443. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG008(pcie_port),
  444. pciercx_cfg008.u32);
  445. /*
  446. * Prefetchable BAR (PCIERCn_CFG009,PCIERCn_CFG010,PCIERCn_CFG011)
  447. * Most applications should disable the prefetchable BAR by setting
  448. * PCIERCn_CFG011[UMEM_LIMIT],PCIERCn_CFG009[LMEM_LIMIT] <
  449. * PCIERCn_CFG010[UMEM_BASE],PCIERCn_CFG009[LMEM_BASE]
  450. */
  451. pciercx_cfg009.u32 =
  452. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG009(pcie_port));
  453. pciercx_cfg010.u32 =
  454. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG010(pcie_port));
  455. pciercx_cfg011.u32 =
  456. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG011(pcie_port));
  457. pciercx_cfg009.s.lmem_base = 0x100;
  458. pciercx_cfg009.s.lmem_limit = 0;
  459. pciercx_cfg010.s.umem_base = 0x100;
  460. pciercx_cfg011.s.umem_limit = 0;
  461. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG009(pcie_port),
  462. pciercx_cfg009.u32);
  463. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG010(pcie_port),
  464. pciercx_cfg010.u32);
  465. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG011(pcie_port),
  466. pciercx_cfg011.u32);
  467. /*
  468. * System Error Interrupt Enables (PCIERCn_CFG035[SECEE,SEFEE,SENFEE])
  469. * PME Interrupt Enables (PCIERCn_CFG035[PMEIE])
  470. */
  471. pciercx_cfg035.u32 =
  472. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG035(pcie_port));
  473. /* System error on correctable error enable. */
  474. pciercx_cfg035.s.secee = 1;
  475. /* System error on fatal error enable. */
  476. pciercx_cfg035.s.sefee = 1;
  477. /* System error on non-fatal error enable. */
  478. pciercx_cfg035.s.senfee = 1;
  479. /* PME interrupt enable. */
  480. pciercx_cfg035.s.pmeie = 1;
  481. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG035(pcie_port),
  482. pciercx_cfg035.u32);
  483. /*
  484. * Advanced Error Recovery Interrupt Enables
  485. * (PCIERCn_CFG075[CERE,NFERE,FERE])
  486. */
  487. pciercx_cfg075.u32 =
  488. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG075(pcie_port));
  489. /* Correctable error reporting enable. */
  490. pciercx_cfg075.s.cere = 1;
  491. /* Non-fatal error reporting enable. */
  492. pciercx_cfg075.s.nfere = 1;
  493. /* Fatal error reporting enable. */
  494. pciercx_cfg075.s.fere = 1;
  495. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG075(pcie_port),
  496. pciercx_cfg075.u32);
  497. /* HP Interrupt Enables (PCIERCn_CFG034[HPINT_EN],
  498. * PCIERCn_CFG034[DLLS_EN,CCINT_EN])
  499. */
  500. pciercx_cfg034.u32 =
  501. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG034(pcie_port));
  502. /* Hot-plug interrupt enable. */
  503. pciercx_cfg034.s.hpint_en = 1;
  504. /* Data Link Layer state changed enable */
  505. pciercx_cfg034.s.dlls_en = 1;
  506. /* Command completed interrupt enable. */
  507. pciercx_cfg034.s.ccint_en = 1;
  508. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG034(pcie_port),
  509. pciercx_cfg034.u32);
  510. }
  511. /**
  512. * Initialize a host mode PCIe link. This function takes a PCIe
  513. * port from reset to a link up state. Software can then begin
  514. * configuring the rest of the link.
  515. *
  516. * @pcie_port: PCIe port to initialize
  517. *
  518. * Returns Zero on success
  519. */
  520. static int __cvmx_pcie_rc_initialize_link(int pcie_port)
  521. {
  522. uint64_t start_cycle;
  523. union cvmx_pescx_ctl_status pescx_ctl_status;
  524. union cvmx_pciercx_cfg452 pciercx_cfg452;
  525. union cvmx_pciercx_cfg032 pciercx_cfg032;
  526. union cvmx_pciercx_cfg448 pciercx_cfg448;
  527. /* Set the lane width */
  528. pciercx_cfg452.u32 =
  529. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG452(pcie_port));
  530. pescx_ctl_status.u64 = cvmx_read_csr(CVMX_PESCX_CTL_STATUS(pcie_port));
  531. if (pescx_ctl_status.s.qlm_cfg == 0) {
  532. /* We're in 8 lane (56XX) or 4 lane (54XX) mode */
  533. pciercx_cfg452.s.lme = 0xf;
  534. } else {
  535. /* We're in 4 lane (56XX) or 2 lane (52XX) mode */
  536. pciercx_cfg452.s.lme = 0x7;
  537. }
  538. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG452(pcie_port),
  539. pciercx_cfg452.u32);
  540. /*
  541. * CN52XX pass 1.x has an errata where length mismatches on UR
  542. * responses can cause bus errors on 64bit memory
  543. * reads. Turning off length error checking fixes this.
  544. */
  545. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  546. union cvmx_pciercx_cfg455 pciercx_cfg455;
  547. pciercx_cfg455.u32 =
  548. cvmx_pcie_cfgx_read(pcie_port,
  549. CVMX_PCIERCX_CFG455(pcie_port));
  550. pciercx_cfg455.s.m_cpl_len_err = 1;
  551. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG455(pcie_port),
  552. pciercx_cfg455.u32);
  553. }
  554. /* Lane swap needs to be manually enabled for CN52XX */
  555. if (OCTEON_IS_MODEL(OCTEON_CN52XX) && (pcie_port == 1)) {
  556. pescx_ctl_status.s.lane_swp = 1;
  557. cvmx_write_csr(CVMX_PESCX_CTL_STATUS(pcie_port),
  558. pescx_ctl_status.u64);
  559. }
  560. /* Bring up the link */
  561. pescx_ctl_status.u64 = cvmx_read_csr(CVMX_PESCX_CTL_STATUS(pcie_port));
  562. pescx_ctl_status.s.lnk_enb = 1;
  563. cvmx_write_csr(CVMX_PESCX_CTL_STATUS(pcie_port), pescx_ctl_status.u64);
  564. /*
  565. * CN52XX pass 1.0: Due to a bug in 2nd order CDR, it needs to
  566. * be disabled.
  567. */
  568. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_0))
  569. __cvmx_helper_errata_qlm_disable_2nd_order_cdr(0);
  570. /* Wait for the link to come up */
  571. cvmx_dprintf("PCIe: Waiting for port %d link\n", pcie_port);
  572. start_cycle = cvmx_get_cycle();
  573. do {
  574. if (cvmx_get_cycle() - start_cycle >
  575. 2 * cvmx_sysinfo_get()->cpu_clock_hz) {
  576. cvmx_dprintf("PCIe: Port %d link timeout\n",
  577. pcie_port);
  578. return -1;
  579. }
  580. cvmx_wait(10000);
  581. pciercx_cfg032.u32 =
  582. cvmx_pcie_cfgx_read(pcie_port,
  583. CVMX_PCIERCX_CFG032(pcie_port));
  584. } while (pciercx_cfg032.s.dlla == 0);
  585. /* Display the link status */
  586. cvmx_dprintf("PCIe: Port %d link active, %d lanes\n", pcie_port,
  587. pciercx_cfg032.s.nlw);
  588. /*
  589. * Update the Replay Time Limit. Empirically, some PCIe
  590. * devices take a little longer to respond than expected under
  591. * load. As a workaround for this we configure the Replay Time
  592. * Limit to the value expected for a 512 byte MPS instead of
  593. * our actual 256 byte MPS. The numbers below are directly
  594. * from the PCIe spec table 3-4.
  595. */
  596. pciercx_cfg448.u32 =
  597. cvmx_pcie_cfgx_read(pcie_port, CVMX_PCIERCX_CFG448(pcie_port));
  598. switch (pciercx_cfg032.s.nlw) {
  599. case 1: /* 1 lane */
  600. pciercx_cfg448.s.rtl = 1677;
  601. break;
  602. case 2: /* 2 lanes */
  603. pciercx_cfg448.s.rtl = 867;
  604. break;
  605. case 4: /* 4 lanes */
  606. pciercx_cfg448.s.rtl = 462;
  607. break;
  608. case 8: /* 8 lanes */
  609. pciercx_cfg448.s.rtl = 258;
  610. break;
  611. }
  612. cvmx_pcie_cfgx_write(pcie_port, CVMX_PCIERCX_CFG448(pcie_port),
  613. pciercx_cfg448.u32);
  614. return 0;
  615. }
  616. /**
  617. * Initialize a PCIe port for use in host(RC) mode. It doesn't
  618. * enumerate the bus.
  619. *
  620. * @pcie_port: PCIe port to initialize
  621. *
  622. * Returns Zero on success
  623. */
  624. static int cvmx_pcie_rc_initialize(int pcie_port)
  625. {
  626. int i;
  627. union cvmx_ciu_soft_prst ciu_soft_prst;
  628. union cvmx_pescx_bist_status pescx_bist_status;
  629. union cvmx_pescx_bist_status2 pescx_bist_status2;
  630. union cvmx_npei_ctl_status npei_ctl_status;
  631. union cvmx_npei_mem_access_ctl npei_mem_access_ctl;
  632. union cvmx_npei_mem_access_subidx mem_access_subid;
  633. union cvmx_npei_dbg_data npei_dbg_data;
  634. union cvmx_pescx_ctl_status2 pescx_ctl_status2;
  635. /*
  636. * Make sure we aren't trying to setup a target mode interface
  637. * in host mode.
  638. */
  639. npei_ctl_status.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_STATUS);
  640. if ((pcie_port == 0) && !npei_ctl_status.s.host_mode) {
  641. cvmx_dprintf("PCIe: ERROR: cvmx_pcie_rc_initialize() called "
  642. "on port0, but port0 is not in host mode\n");
  643. return -1;
  644. }
  645. /*
  646. * Make sure a CN52XX isn't trying to bring up port 1 when it
  647. * is disabled.
  648. */
  649. if (OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  650. npei_dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  651. if ((pcie_port == 1) && npei_dbg_data.cn52xx.qlm0_link_width) {
  652. cvmx_dprintf("PCIe: ERROR: cvmx_pcie_rc_initialize() "
  653. "called on port1, but port1 is "
  654. "disabled\n");
  655. return -1;
  656. }
  657. }
  658. /*
  659. * PCIe switch arbitration mode. '0' == fixed priority NPEI,
  660. * PCIe0, then PCIe1. '1' == round robin.
  661. */
  662. npei_ctl_status.s.arb = 1;
  663. /* Allow up to 0x20 config retries */
  664. npei_ctl_status.s.cfg_rtry = 0x20;
  665. /*
  666. * CN52XX pass1.x has an errata where P0_NTAGS and P1_NTAGS
  667. * don't reset.
  668. */
  669. if (OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  670. npei_ctl_status.s.p0_ntags = 0x20;
  671. npei_ctl_status.s.p1_ntags = 0x20;
  672. }
  673. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_STATUS, npei_ctl_status.u64);
  674. /* Bring the PCIe out of reset */
  675. if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_EBH5200) {
  676. /*
  677. * The EBH5200 board swapped the PCIe reset lines on
  678. * the board. As a workaround for this bug, we bring
  679. * both PCIe ports out of reset at the same time
  680. * instead of on separate calls. So for port 0, we
  681. * bring both out of reset and do nothing on port 1.
  682. */
  683. if (pcie_port == 0) {
  684. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  685. /*
  686. * After a chip reset the PCIe will also be in
  687. * reset. If it isn't, most likely someone is
  688. * trying to init it again without a proper
  689. * PCIe reset.
  690. */
  691. if (ciu_soft_prst.s.soft_prst == 0) {
  692. /* Reset the ports */
  693. ciu_soft_prst.s.soft_prst = 1;
  694. cvmx_write_csr(CVMX_CIU_SOFT_PRST,
  695. ciu_soft_prst.u64);
  696. ciu_soft_prst.u64 =
  697. cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  698. ciu_soft_prst.s.soft_prst = 1;
  699. cvmx_write_csr(CVMX_CIU_SOFT_PRST1,
  700. ciu_soft_prst.u64);
  701. /* Wait until pcie resets the ports. */
  702. udelay(2000);
  703. }
  704. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  705. ciu_soft_prst.s.soft_prst = 0;
  706. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  707. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  708. ciu_soft_prst.s.soft_prst = 0;
  709. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  710. }
  711. } else {
  712. /*
  713. * The normal case: The PCIe ports are completely
  714. * separate and can be brought out of reset
  715. * independently.
  716. */
  717. if (pcie_port)
  718. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  719. else
  720. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  721. /*
  722. * After a chip reset the PCIe will also be in
  723. * reset. If it isn't, most likely someone is trying
  724. * to init it again without a proper PCIe reset.
  725. */
  726. if (ciu_soft_prst.s.soft_prst == 0) {
  727. /* Reset the port */
  728. ciu_soft_prst.s.soft_prst = 1;
  729. if (pcie_port)
  730. cvmx_write_csr(CVMX_CIU_SOFT_PRST1,
  731. ciu_soft_prst.u64);
  732. else
  733. cvmx_write_csr(CVMX_CIU_SOFT_PRST,
  734. ciu_soft_prst.u64);
  735. /* Wait until pcie resets the ports. */
  736. udelay(2000);
  737. }
  738. if (pcie_port) {
  739. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST1);
  740. ciu_soft_prst.s.soft_prst = 0;
  741. cvmx_write_csr(CVMX_CIU_SOFT_PRST1, ciu_soft_prst.u64);
  742. } else {
  743. ciu_soft_prst.u64 = cvmx_read_csr(CVMX_CIU_SOFT_PRST);
  744. ciu_soft_prst.s.soft_prst = 0;
  745. cvmx_write_csr(CVMX_CIU_SOFT_PRST, ciu_soft_prst.u64);
  746. }
  747. }
  748. /*
  749. * Wait for PCIe reset to complete. Due to errata PCIE-700, we
  750. * don't poll PESCX_CTL_STATUS2[PCIERST], but simply wait a
  751. * fixed number of cycles.
  752. */
  753. cvmx_wait(400000);
  754. /* PESCX_BIST_STATUS2[PCLK_RUN] was missing on pass 1 of CN56XX and
  755. CN52XX, so we only probe it on newer chips */
  756. if (!OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X)
  757. && !OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) {
  758. /* Clear PCLK_RUN so we can check if the clock is running */
  759. pescx_ctl_status2.u64 =
  760. cvmx_read_csr(CVMX_PESCX_CTL_STATUS2(pcie_port));
  761. pescx_ctl_status2.s.pclk_run = 1;
  762. cvmx_write_csr(CVMX_PESCX_CTL_STATUS2(pcie_port),
  763. pescx_ctl_status2.u64);
  764. /*
  765. * Now that we cleared PCLK_RUN, wait for it to be set
  766. * again telling us the clock is running.
  767. */
  768. if (CVMX_WAIT_FOR_FIELD64(CVMX_PESCX_CTL_STATUS2(pcie_port),
  769. union cvmx_pescx_ctl_status2,
  770. pclk_run, ==, 1, 10000)) {
  771. cvmx_dprintf("PCIe: Port %d isn't clocked, skipping.\n",
  772. pcie_port);
  773. return -1;
  774. }
  775. }
  776. /*
  777. * Check and make sure PCIe came out of reset. If it doesn't
  778. * the board probably hasn't wired the clocks up and the
  779. * interface should be skipped.
  780. */
  781. pescx_ctl_status2.u64 =
  782. cvmx_read_csr(CVMX_PESCX_CTL_STATUS2(pcie_port));
  783. if (pescx_ctl_status2.s.pcierst) {
  784. cvmx_dprintf("PCIe: Port %d stuck in reset, skipping.\n",
  785. pcie_port);
  786. return -1;
  787. }
  788. /*
  789. * Check BIST2 status. If any bits are set skip this interface. This
  790. * is an attempt to catch PCIE-813 on pass 1 parts.
  791. */
  792. pescx_bist_status2.u64 =
  793. cvmx_read_csr(CVMX_PESCX_BIST_STATUS2(pcie_port));
  794. if (pescx_bist_status2.u64) {
  795. cvmx_dprintf("PCIe: Port %d BIST2 failed. Most likely this "
  796. "port isn't hooked up, skipping.\n",
  797. pcie_port);
  798. return -1;
  799. }
  800. /* Check BIST status */
  801. pescx_bist_status.u64 =
  802. cvmx_read_csr(CVMX_PESCX_BIST_STATUS(pcie_port));
  803. if (pescx_bist_status.u64)
  804. cvmx_dprintf("PCIe: BIST FAILED for port %d (0x%016llx)\n",
  805. pcie_port, CAST64(pescx_bist_status.u64));
  806. /* Initialize the config space CSRs */
  807. __cvmx_pcie_rc_initialize_config_space(pcie_port);
  808. /* Bring the link up */
  809. if (__cvmx_pcie_rc_initialize_link(pcie_port)) {
  810. cvmx_dprintf
  811. ("PCIe: ERROR: cvmx_pcie_rc_initialize_link() failed\n");
  812. return -1;
  813. }
  814. /* Store merge control (NPEI_MEM_ACCESS_CTL[TIMER,MAX_WORD]) */
  815. npei_mem_access_ctl.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_MEM_ACCESS_CTL);
  816. /* Allow 16 words to combine */
  817. npei_mem_access_ctl.s.max_word = 0;
  818. /* Wait up to 127 cycles for more data */
  819. npei_mem_access_ctl.s.timer = 127;
  820. cvmx_write_csr(CVMX_PEXP_NPEI_MEM_ACCESS_CTL, npei_mem_access_ctl.u64);
  821. /* Setup Mem access SubDIDs */
  822. mem_access_subid.u64 = 0;
  823. /* Port the request is sent to. */
  824. mem_access_subid.s.port = pcie_port;
  825. /* Due to an errata on pass 1 chips, no merging is allowed. */
  826. mem_access_subid.s.nmerge = 1;
  827. /* Endian-swap for Reads. */
  828. mem_access_subid.s.esr = 1;
  829. /* Endian-swap for Writes. */
  830. mem_access_subid.s.esw = 1;
  831. /* No Snoop for Reads. */
  832. mem_access_subid.s.nsr = 1;
  833. /* No Snoop for Writes. */
  834. mem_access_subid.s.nsw = 1;
  835. /* Disable Relaxed Ordering for Reads. */
  836. mem_access_subid.s.ror = 0;
  837. /* Disable Relaxed Ordering for Writes. */
  838. mem_access_subid.s.row = 0;
  839. /* PCIe Adddress Bits <63:34>. */
  840. mem_access_subid.s.ba = 0;
  841. /*
  842. * Setup mem access 12-15 for port 0, 16-19 for port 1,
  843. * supplying 36 bits of address space.
  844. */
  845. for (i = 12 + pcie_port * 4; i < 16 + pcie_port * 4; i++) {
  846. cvmx_write_csr(CVMX_PEXP_NPEI_MEM_ACCESS_SUBIDX(i),
  847. mem_access_subid.u64);
  848. /* Set each SUBID to extend the addressable range */
  849. mem_access_subid.s.ba += 1;
  850. }
  851. /*
  852. * Disable the peer to peer forwarding register. This must be
  853. * setup by the OS after it enumerates the bus and assigns
  854. * addresses to the PCIe busses.
  855. */
  856. for (i = 0; i < 4; i++) {
  857. cvmx_write_csr(CVMX_PESCX_P2P_BARX_START(i, pcie_port), -1);
  858. cvmx_write_csr(CVMX_PESCX_P2P_BARX_END(i, pcie_port), -1);
  859. }
  860. /* Set Octeon's BAR0 to decode 0-16KB. It overlaps with Bar2 */
  861. cvmx_write_csr(CVMX_PESCX_P2N_BAR0_START(pcie_port), 0);
  862. /*
  863. * Disable Octeon's BAR1. It isn't needed in RC mode since
  864. * BAR2 maps all of memory. BAR2 also maps 256MB-512MB into
  865. * the 2nd 256MB of memory.
  866. */
  867. cvmx_write_csr(CVMX_PESCX_P2N_BAR1_START(pcie_port), -1);
  868. /*
  869. * Set Octeon's BAR2 to decode 0-2^39. Bar0 and Bar1 take
  870. * precedence where they overlap. It also overlaps with the
  871. * device addresses, so make sure the peer to peer forwarding
  872. * is set right.
  873. */
  874. cvmx_write_csr(CVMX_PESCX_P2N_BAR2_START(pcie_port), 0);
  875. /*
  876. * Setup BAR2 attributes
  877. *
  878. * Relaxed Ordering (NPEI_CTL_PORTn[PTLP_RO,CTLP_RO, WAIT_COM])
  879. * - PTLP_RO,CTLP_RO should normally be set (except for debug).
  880. * - WAIT_COM=0 will likely work for all applications.
  881. *
  882. * Load completion relaxed ordering (NPEI_CTL_PORTn[WAITL_COM]).
  883. */
  884. if (pcie_port) {
  885. union cvmx_npei_ctl_port1 npei_ctl_port;
  886. npei_ctl_port.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_PORT1);
  887. npei_ctl_port.s.bar2_enb = 1;
  888. npei_ctl_port.s.bar2_esx = 1;
  889. npei_ctl_port.s.bar2_cax = 0;
  890. npei_ctl_port.s.ptlp_ro = 1;
  891. npei_ctl_port.s.ctlp_ro = 1;
  892. npei_ctl_port.s.wait_com = 0;
  893. npei_ctl_port.s.waitl_com = 0;
  894. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_PORT1, npei_ctl_port.u64);
  895. } else {
  896. union cvmx_npei_ctl_port0 npei_ctl_port;
  897. npei_ctl_port.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_PORT0);
  898. npei_ctl_port.s.bar2_enb = 1;
  899. npei_ctl_port.s.bar2_esx = 1;
  900. npei_ctl_port.s.bar2_cax = 0;
  901. npei_ctl_port.s.ptlp_ro = 1;
  902. npei_ctl_port.s.ctlp_ro = 1;
  903. npei_ctl_port.s.wait_com = 0;
  904. npei_ctl_port.s.waitl_com = 0;
  905. cvmx_write_csr(CVMX_PEXP_NPEI_CTL_PORT0, npei_ctl_port.u64);
  906. }
  907. return 0;
  908. }
  909. /* Above was cvmx-pcie.c, below original pcie.c */
  910. /**
  911. * Map a PCI device to the appropriate interrupt line
  912. *
  913. * @dev: The Linux PCI device structure for the device to map
  914. * @slot: The slot number for this device on __BUS 0__. Linux
  915. * enumerates through all the bridges and figures out the
  916. * slot on Bus 0 where this device eventually hooks to.
  917. * @pin: The PCI interrupt pin read from the device, then swizzled
  918. * as it goes through each bridge.
  919. * Returns Interrupt number for the device
  920. */
  921. int __init octeon_pcie_pcibios_map_irq(const struct pci_dev *dev,
  922. u8 slot, u8 pin)
  923. {
  924. /*
  925. * The EBH5600 board with the PCI to PCIe bridge mistakenly
  926. * wires the first slot for both device id 2 and interrupt
  927. * A. According to the PCI spec, device id 2 should be C. The
  928. * following kludge attempts to fix this.
  929. */
  930. if (strstr(octeon_board_type_string(), "EBH5600") &&
  931. dev->bus && dev->bus->parent) {
  932. /*
  933. * Iterate all the way up the device chain and find
  934. * the root bus.
  935. */
  936. while (dev->bus && dev->bus->parent)
  937. dev = to_pci_dev(dev->bus->bridge);
  938. /* If the root bus is number 0 and the PEX 8114 is the
  939. * root, assume we are behind the miswired bus. We
  940. * need to correct the swizzle level by two. Yuck.
  941. */
  942. if ((dev->bus->number == 0) &&
  943. (dev->vendor == 0x10b5) && (dev->device == 0x8114)) {
  944. /*
  945. * The pin field is one based, not zero. We
  946. * need to swizzle it by minus two.
  947. */
  948. pin = ((pin - 3) & 3) + 1;
  949. }
  950. }
  951. /*
  952. * The -1 is because pin starts with one, not zero. It might
  953. * be that this equation needs to include the slot number, but
  954. * I don't have hardware to check that against.
  955. */
  956. return pin - 1 + OCTEON_IRQ_PCI_INT0;
  957. }
  958. /**
  959. * Read a value from configuration space
  960. *
  961. * @bus:
  962. * @devfn:
  963. * @reg:
  964. * @size:
  965. * @val:
  966. * Returns
  967. */
  968. static inline int octeon_pcie_read_config(int pcie_port, struct pci_bus *bus,
  969. unsigned int devfn, int reg, int size,
  970. u32 *val)
  971. {
  972. union octeon_cvmemctl cvmmemctl;
  973. union octeon_cvmemctl cvmmemctl_save;
  974. int bus_number = bus->number;
  975. /*
  976. * We need to force the bus number to be zero on the root
  977. * bus. Linux numbers the 2nd root bus to start after all
  978. * buses on root 0.
  979. */
  980. if (bus->parent == NULL)
  981. bus_number = 0;
  982. /*
  983. * PCIe only has a single device connected to Octeon. It is
  984. * always device ID 0. Don't bother doing reads for other
  985. * device IDs on the first segment.
  986. */
  987. if ((bus_number == 0) && (devfn >> 3 != 0))
  988. return PCIBIOS_FUNC_NOT_SUPPORTED;
  989. /*
  990. * The following is a workaround for the CN57XX, CN56XX,
  991. * CN55XX, and CN54XX errata with PCIe config reads from non
  992. * existent devices. These chips will hang the PCIe link if a
  993. * config read is performed that causes a UR response.
  994. */
  995. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1) ||
  996. OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_1)) {
  997. /*
  998. * For our EBH5600 board, port 0 has a bridge with two
  999. * PCI-X slots. We need a new special checks to make
  1000. * sure we only probe valid stuff. The PCIe->PCI-X
  1001. * bridge only respondes to device ID 0, function
  1002. * 0-1
  1003. */
  1004. if ((bus_number == 0) && (devfn >= 2))
  1005. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1006. /*
  1007. * The PCI-X slots are device ID 2,3. Choose one of
  1008. * the below "if" blocks based on what is plugged into
  1009. * the board.
  1010. */
  1011. #if 1
  1012. /* Use this option if you aren't using either slot */
  1013. if (bus_number == 1)
  1014. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1015. #elif 0
  1016. /*
  1017. * Use this option if you are using the first slot but
  1018. * not the second.
  1019. */
  1020. if ((bus_number == 1) && (devfn >> 3 != 2))
  1021. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1022. #elif 0
  1023. /*
  1024. * Use this option if you are using the second slot
  1025. * but not the first.
  1026. */
  1027. if ((bus_number == 1) && (devfn >> 3 != 3))
  1028. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1029. #elif 0
  1030. /* Use this opion if you are using both slots */
  1031. if ((bus_number == 1) &&
  1032. !((devfn == (2 << 3)) || (devfn == (3 << 3))))
  1033. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1034. #endif
  1035. /*
  1036. * Shorten the DID timeout so bus errors for PCIe
  1037. * config reads from non existent devices happen
  1038. * faster. This allows us to continue booting even if
  1039. * the above "if" checks are wrong. Once one of these
  1040. * errors happens, the PCIe port is dead.
  1041. */
  1042. cvmmemctl_save.u64 = __read_64bit_c0_register($11, 7);
  1043. cvmmemctl.u64 = cvmmemctl_save.u64;
  1044. cvmmemctl.s.didtto = 2;
  1045. __write_64bit_c0_register($11, 7, cvmmemctl.u64);
  1046. }
  1047. switch (size) {
  1048. case 4:
  1049. *val = cvmx_pcie_config_read32(pcie_port, bus_number,
  1050. devfn >> 3, devfn & 0x7, reg);
  1051. break;
  1052. case 2:
  1053. *val = cvmx_pcie_config_read16(pcie_port, bus_number,
  1054. devfn >> 3, devfn & 0x7, reg);
  1055. break;
  1056. case 1:
  1057. *val = cvmx_pcie_config_read8(pcie_port, bus_number, devfn >> 3,
  1058. devfn & 0x7, reg);
  1059. break;
  1060. default:
  1061. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1062. }
  1063. if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1) ||
  1064. OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_1))
  1065. __write_64bit_c0_register($11, 7, cvmmemctl_save.u64);
  1066. return PCIBIOS_SUCCESSFUL;
  1067. }
  1068. static int octeon_pcie0_read_config(struct pci_bus *bus, unsigned int devfn,
  1069. int reg, int size, u32 *val)
  1070. {
  1071. return octeon_pcie_read_config(0, bus, devfn, reg, size, val);
  1072. }
  1073. static int octeon_pcie1_read_config(struct pci_bus *bus, unsigned int devfn,
  1074. int reg, int size, u32 *val)
  1075. {
  1076. return octeon_pcie_read_config(1, bus, devfn, reg, size, val);
  1077. }
  1078. /**
  1079. * Write a value to PCI configuration space
  1080. *
  1081. * @bus:
  1082. * @devfn:
  1083. * @reg:
  1084. * @size:
  1085. * @val:
  1086. * Returns
  1087. */
  1088. static inline int octeon_pcie_write_config(int pcie_port, struct pci_bus *bus,
  1089. unsigned int devfn, int reg,
  1090. int size, u32 val)
  1091. {
  1092. int bus_number = bus->number;
  1093. /*
  1094. * We need to force the bus number to be zero on the root
  1095. * bus. Linux numbers the 2nd root bus to start after all
  1096. * busses on root 0.
  1097. */
  1098. if (bus->parent == NULL)
  1099. bus_number = 0;
  1100. switch (size) {
  1101. case 4:
  1102. cvmx_pcie_config_write32(pcie_port, bus_number, devfn >> 3,
  1103. devfn & 0x7, reg, val);
  1104. return PCIBIOS_SUCCESSFUL;
  1105. case 2:
  1106. cvmx_pcie_config_write16(pcie_port, bus_number, devfn >> 3,
  1107. devfn & 0x7, reg, val);
  1108. return PCIBIOS_SUCCESSFUL;
  1109. case 1:
  1110. cvmx_pcie_config_write8(pcie_port, bus_number, devfn >> 3,
  1111. devfn & 0x7, reg, val);
  1112. return PCIBIOS_SUCCESSFUL;
  1113. }
  1114. #if PCI_CONFIG_SPACE_DELAY
  1115. udelay(PCI_CONFIG_SPACE_DELAY);
  1116. #endif
  1117. return PCIBIOS_FUNC_NOT_SUPPORTED;
  1118. }
  1119. static int octeon_pcie0_write_config(struct pci_bus *bus, unsigned int devfn,
  1120. int reg, int size, u32 val)
  1121. {
  1122. return octeon_pcie_write_config(0, bus, devfn, reg, size, val);
  1123. }
  1124. static int octeon_pcie1_write_config(struct pci_bus *bus, unsigned int devfn,
  1125. int reg, int size, u32 val)
  1126. {
  1127. return octeon_pcie_write_config(1, bus, devfn, reg, size, val);
  1128. }
  1129. static struct pci_ops octeon_pcie0_ops = {
  1130. octeon_pcie0_read_config,
  1131. octeon_pcie0_write_config,
  1132. };
  1133. static struct resource octeon_pcie0_mem_resource = {
  1134. .name = "Octeon PCIe0 MEM",
  1135. .flags = IORESOURCE_MEM,
  1136. };
  1137. static struct resource octeon_pcie0_io_resource = {
  1138. .name = "Octeon PCIe0 IO",
  1139. .flags = IORESOURCE_IO,
  1140. };
  1141. static struct pci_controller octeon_pcie0_controller = {
  1142. .pci_ops = &octeon_pcie0_ops,
  1143. .mem_resource = &octeon_pcie0_mem_resource,
  1144. .io_resource = &octeon_pcie0_io_resource,
  1145. };
  1146. static struct pci_ops octeon_pcie1_ops = {
  1147. octeon_pcie1_read_config,
  1148. octeon_pcie1_write_config,
  1149. };
  1150. static struct resource octeon_pcie1_mem_resource = {
  1151. .name = "Octeon PCIe1 MEM",
  1152. .flags = IORESOURCE_MEM,
  1153. };
  1154. static struct resource octeon_pcie1_io_resource = {
  1155. .name = "Octeon PCIe1 IO",
  1156. .flags = IORESOURCE_IO,
  1157. };
  1158. static struct pci_controller octeon_pcie1_controller = {
  1159. .pci_ops = &octeon_pcie1_ops,
  1160. .mem_resource = &octeon_pcie1_mem_resource,
  1161. .io_resource = &octeon_pcie1_io_resource,
  1162. };
  1163. /**
  1164. * Initialize the Octeon PCIe controllers
  1165. *
  1166. * Returns
  1167. */
  1168. static int __init octeon_pcie_setup(void)
  1169. {
  1170. union cvmx_npei_ctl_status npei_ctl_status;
  1171. int result;
  1172. /* These chips don't have PCIe */
  1173. if (!octeon_has_feature(OCTEON_FEATURE_PCIE))
  1174. return 0;
  1175. /* Point pcibios_map_irq() to the PCIe version of it */
  1176. octeon_pcibios_map_irq = octeon_pcie_pcibios_map_irq;
  1177. /* Use the PCIe based DMA mappings */
  1178. octeon_dma_bar_type = OCTEON_DMA_BAR_TYPE_PCIE;
  1179. /*
  1180. * PCIe I/O range. It is based on port 0 but includes up until
  1181. * port 1's end.
  1182. */
  1183. set_io_port_base(CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address(0)));
  1184. ioport_resource.start = 0;
  1185. ioport_resource.end =
  1186. cvmx_pcie_get_io_base_address(1) -
  1187. cvmx_pcie_get_io_base_address(0) + cvmx_pcie_get_io_size(1) - 1;
  1188. npei_ctl_status.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_CTL_STATUS);
  1189. if (npei_ctl_status.s.host_mode) {
  1190. pr_notice("PCIe: Initializing port 0\n");
  1191. result = cvmx_pcie_rc_initialize(0);
  1192. if (result == 0) {
  1193. /* Memory offsets are physical addresses */
  1194. octeon_pcie0_controller.mem_offset =
  1195. cvmx_pcie_get_mem_base_address(0);
  1196. /* IO offsets are Mips virtual addresses */
  1197. octeon_pcie0_controller.io_map_base =
  1198. CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address
  1199. (0));
  1200. octeon_pcie0_controller.io_offset = 0;
  1201. /*
  1202. * To keep things similar to PCI, we start
  1203. * device addresses at the same place as PCI
  1204. * uisng big bar support. This normally
  1205. * translates to 4GB-256MB, which is the same
  1206. * as most x86 PCs.
  1207. */
  1208. octeon_pcie0_controller.mem_resource->start =
  1209. cvmx_pcie_get_mem_base_address(0) +
  1210. (4ul << 30) - (OCTEON_PCI_BAR1_HOLE_SIZE << 20);
  1211. octeon_pcie0_controller.mem_resource->end =
  1212. cvmx_pcie_get_mem_base_address(0) +
  1213. cvmx_pcie_get_mem_size(0) - 1;
  1214. /*
  1215. * Ports must be above 16KB for the ISA bus
  1216. * filtering in the PCI-X to PCI bridge.
  1217. */
  1218. octeon_pcie0_controller.io_resource->start = 4 << 10;
  1219. octeon_pcie0_controller.io_resource->end =
  1220. cvmx_pcie_get_io_size(0) - 1;
  1221. register_pci_controller(&octeon_pcie0_controller);
  1222. }
  1223. } else {
  1224. pr_notice("PCIe: Port 0 in endpoint mode, skipping.\n");
  1225. }
  1226. /* Skip the 2nd port on CN52XX if port 0 is in 4 lane mode */
  1227. if (OCTEON_IS_MODEL(OCTEON_CN52XX)) {
  1228. union cvmx_npei_dbg_data npei_dbg_data;
  1229. npei_dbg_data.u64 = cvmx_read_csr(CVMX_PEXP_NPEI_DBG_DATA);
  1230. if (npei_dbg_data.cn52xx.qlm0_link_width)
  1231. return 0;
  1232. }
  1233. pr_notice("PCIe: Initializing port 1\n");
  1234. result = cvmx_pcie_rc_initialize(1);
  1235. if (result == 0) {
  1236. /* Memory offsets are physical addresses */
  1237. octeon_pcie1_controller.mem_offset =
  1238. cvmx_pcie_get_mem_base_address(1);
  1239. /* IO offsets are Mips virtual addresses */
  1240. octeon_pcie1_controller.io_map_base =
  1241. CVMX_ADD_IO_SEG(cvmx_pcie_get_io_base_address(1));
  1242. octeon_pcie1_controller.io_offset =
  1243. cvmx_pcie_get_io_base_address(1) -
  1244. cvmx_pcie_get_io_base_address(0);
  1245. /*
  1246. * To keep things similar to PCI, we start device
  1247. * addresses at the same place as PCI uisng big bar
  1248. * support. This normally translates to 4GB-256MB,
  1249. * which is the same as most x86 PCs.
  1250. */
  1251. octeon_pcie1_controller.mem_resource->start =
  1252. cvmx_pcie_get_mem_base_address(1) + (4ul << 30) -
  1253. (OCTEON_PCI_BAR1_HOLE_SIZE << 20);
  1254. octeon_pcie1_controller.mem_resource->end =
  1255. cvmx_pcie_get_mem_base_address(1) +
  1256. cvmx_pcie_get_mem_size(1) - 1;
  1257. /*
  1258. * Ports must be above 16KB for the ISA bus filtering
  1259. * in the PCI-X to PCI bridge.
  1260. */
  1261. octeon_pcie1_controller.io_resource->start =
  1262. cvmx_pcie_get_io_base_address(1) -
  1263. cvmx_pcie_get_io_base_address(0);
  1264. octeon_pcie1_controller.io_resource->end =
  1265. octeon_pcie1_controller.io_resource->start +
  1266. cvmx_pcie_get_io_size(1) - 1;
  1267. register_pci_controller(&octeon_pcie1_controller);
  1268. }
  1269. return 0;
  1270. }
  1271. arch_initcall(octeon_pcie_setup);