pcie-octeon.c 67 KB

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