pcie-octeon.c 67 KB

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