qib_sd7220.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. /*
  34. * This file contains all of the code that is specific to the SerDes
  35. * on the QLogic_IB 7220 chip.
  36. */
  37. #include <linux/pci.h>
  38. #include <linux/delay.h>
  39. #include "qib.h"
  40. #include "qib_7220.h"
  41. /*
  42. * Same as in qib_iba7220.c, but just the registers needed here.
  43. * Could move whole set to qib_7220.h, but decided better to keep
  44. * local.
  45. */
  46. #define KREG_IDX(regname) (QIB_7220_##regname##_OFFS / sizeof(u64))
  47. #define kr_hwerrclear KREG_IDX(HwErrClear)
  48. #define kr_hwerrmask KREG_IDX(HwErrMask)
  49. #define kr_hwerrstatus KREG_IDX(HwErrStatus)
  50. #define kr_ibcstatus KREG_IDX(IBCStatus)
  51. #define kr_ibserdesctrl KREG_IDX(IBSerDesCtrl)
  52. #define kr_scratch KREG_IDX(Scratch)
  53. #define kr_xgxs_cfg KREG_IDX(XGXSCfg)
  54. /* these are used only here, not in qib_iba7220.c */
  55. #define kr_ibsd_epb_access_ctrl KREG_IDX(ibsd_epb_access_ctrl)
  56. #define kr_ibsd_epb_transaction_reg KREG_IDX(ibsd_epb_transaction_reg)
  57. #define kr_pciesd_epb_transaction_reg KREG_IDX(pciesd_epb_transaction_reg)
  58. #define kr_pciesd_epb_access_ctrl KREG_IDX(pciesd_epb_access_ctrl)
  59. #define kr_serdes_ddsrxeq0 KREG_IDX(SerDes_DDSRXEQ0)
  60. /*
  61. * The IBSerDesMappTable is a memory that holds values to be stored in
  62. * various SerDes registers by IBC.
  63. */
  64. #define kr_serdes_maptable KREG_IDX(IBSerDesMappTable)
  65. /*
  66. * Below used for sdnum parameter, selecting one of the two sections
  67. * used for PCIe, or the single SerDes used for IB.
  68. */
  69. #define PCIE_SERDES0 0
  70. #define PCIE_SERDES1 1
  71. /*
  72. * The EPB requires addressing in a particular form. EPB_LOC() is intended
  73. * to make #definitions a little more readable.
  74. */
  75. #define EPB_ADDR_SHF 8
  76. #define EPB_LOC(chn, elt, reg) \
  77. (((elt & 0xf) | ((chn & 7) << 4) | ((reg & 0x3f) << 9)) << \
  78. EPB_ADDR_SHF)
  79. #define EPB_IB_QUAD0_CS_SHF (25)
  80. #define EPB_IB_QUAD0_CS (1U << EPB_IB_QUAD0_CS_SHF)
  81. #define EPB_IB_UC_CS_SHF (26)
  82. #define EPB_PCIE_UC_CS_SHF (27)
  83. #define EPB_GLOBAL_WR (1U << (EPB_ADDR_SHF + 8))
  84. /* Forward declarations. */
  85. static int qib_sd7220_reg_mod(struct qib_devdata *dd, int sdnum, u32 loc,
  86. u32 data, u32 mask);
  87. static int ibsd_mod_allchnls(struct qib_devdata *dd, int loc, int val,
  88. int mask);
  89. static int qib_sd_trimdone_poll(struct qib_devdata *dd);
  90. static void qib_sd_trimdone_monitor(struct qib_devdata *dd, const char *where);
  91. static int qib_sd_setvals(struct qib_devdata *dd);
  92. static int qib_sd_early(struct qib_devdata *dd);
  93. static int qib_sd_dactrim(struct qib_devdata *dd);
  94. static int qib_internal_presets(struct qib_devdata *dd);
  95. /* Tweak the register (CMUCTRL5) that contains the TRIMSELF controls */
  96. static int qib_sd_trimself(struct qib_devdata *dd, int val);
  97. static int epb_access(struct qib_devdata *dd, int sdnum, int claim);
  98. /*
  99. * Below keeps track of whether the "once per power-on" initialization has
  100. * been done, because uC code Version 1.32.17 or higher allows the uC to
  101. * be reset at will, and Automatic Equalization may require it. So the
  102. * state of the reset "pin", is no longer valid. Instead, we check for the
  103. * actual uC code having been loaded.
  104. */
  105. static int qib_ibsd_ucode_loaded(struct qib_pportdata *ppd)
  106. {
  107. struct qib_devdata *dd = ppd->dd;
  108. if (!dd->cspec->serdes_first_init_done && (qib_sd7220_ib_vfy(dd) > 0))
  109. dd->cspec->serdes_first_init_done = 1;
  110. return dd->cspec->serdes_first_init_done;
  111. }
  112. /* repeat #define for local use. "Real" #define is in qib_iba7220.c */
  113. #define QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR 0x0000004000000000ULL
  114. #define IB_MPREG5 (EPB_LOC(6, 0, 0xE) | (1L << EPB_IB_UC_CS_SHF))
  115. #define IB_MPREG6 (EPB_LOC(6, 0, 0xF) | (1U << EPB_IB_UC_CS_SHF))
  116. #define UC_PAR_CLR_D 8
  117. #define UC_PAR_CLR_M 0xC
  118. #define IB_CTRL2(chn) (EPB_LOC(chn, 7, 3) | EPB_IB_QUAD0_CS)
  119. #define START_EQ1(chan) EPB_LOC(chan, 7, 0x27)
  120. void qib_sd7220_clr_ibpar(struct qib_devdata *dd)
  121. {
  122. int ret;
  123. /* clear, then re-enable parity errs */
  124. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6,
  125. UC_PAR_CLR_D, UC_PAR_CLR_M);
  126. if (ret < 0) {
  127. qib_dev_err(dd, "Failed clearing IBSerDes Parity err\n");
  128. goto bail;
  129. }
  130. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0,
  131. UC_PAR_CLR_M);
  132. qib_read_kreg32(dd, kr_scratch);
  133. udelay(4);
  134. qib_write_kreg(dd, kr_hwerrclear,
  135. QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);
  136. qib_read_kreg32(dd, kr_scratch);
  137. bail:
  138. return;
  139. }
  140. /*
  141. * After a reset or other unusual event, the epb interface may need
  142. * to be re-synchronized, between the host and the uC.
  143. * returns <0 for failure to resync within IBSD_RESYNC_TRIES (not expected)
  144. */
  145. #define IBSD_RESYNC_TRIES 3
  146. #define IB_PGUDP(chn) (EPB_LOC((chn), 2, 1) | EPB_IB_QUAD0_CS)
  147. #define IB_CMUDONE(chn) (EPB_LOC((chn), 7, 0xF) | EPB_IB_QUAD0_CS)
  148. static int qib_resync_ibepb(struct qib_devdata *dd)
  149. {
  150. int ret, pat, tries, chn;
  151. u32 loc;
  152. ret = -1;
  153. chn = 0;
  154. for (tries = 0; tries < (4 * IBSD_RESYNC_TRIES); ++tries) {
  155. loc = IB_PGUDP(chn);
  156. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);
  157. if (ret < 0) {
  158. qib_dev_err(dd, "Failed read in resync\n");
  159. continue;
  160. }
  161. if (ret != 0xF0 && ret != 0x55 && tries == 0)
  162. qib_dev_err(dd, "unexpected pattern in resync\n");
  163. pat = ret ^ 0xA5; /* alternate F0 and 55 */
  164. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, pat, 0xFF);
  165. if (ret < 0) {
  166. qib_dev_err(dd, "Failed write in resync\n");
  167. continue;
  168. }
  169. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);
  170. if (ret < 0) {
  171. qib_dev_err(dd, "Failed re-read in resync\n");
  172. continue;
  173. }
  174. if (ret != pat) {
  175. qib_dev_err(dd, "Failed compare1 in resync\n");
  176. continue;
  177. }
  178. loc = IB_CMUDONE(chn);
  179. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);
  180. if (ret < 0) {
  181. qib_dev_err(dd, "Failed CMUDONE rd in resync\n");
  182. continue;
  183. }
  184. if ((ret & 0x70) != ((chn << 4) | 0x40)) {
  185. qib_dev_err(dd, "Bad CMUDONE value %02X, chn %d\n",
  186. ret, chn);
  187. continue;
  188. }
  189. if (++chn == 4)
  190. break; /* Success */
  191. }
  192. return (ret > 0) ? 0 : ret;
  193. }
  194. /*
  195. * Localize the stuff that should be done to change IB uC reset
  196. * returns <0 for errors.
  197. */
  198. static int qib_ibsd_reset(struct qib_devdata *dd, int assert_rst)
  199. {
  200. u64 rst_val;
  201. int ret = 0;
  202. unsigned long flags;
  203. rst_val = qib_read_kreg64(dd, kr_ibserdesctrl);
  204. if (assert_rst) {
  205. /*
  206. * Vendor recommends "interrupting" uC before reset, to
  207. * minimize possible glitches.
  208. */
  209. spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
  210. epb_access(dd, IB_7220_SERDES, 1);
  211. rst_val |= 1ULL;
  212. /* Squelch possible parity error from _asserting_ reset */
  213. qib_write_kreg(dd, kr_hwerrmask,
  214. dd->cspec->hwerrmask &
  215. ~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);
  216. qib_write_kreg(dd, kr_ibserdesctrl, rst_val);
  217. /* flush write, delay to ensure it took effect */
  218. qib_read_kreg32(dd, kr_scratch);
  219. udelay(2);
  220. /* once it's reset, can remove interrupt */
  221. epb_access(dd, IB_7220_SERDES, -1);
  222. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  223. } else {
  224. /*
  225. * Before we de-assert reset, we need to deal with
  226. * possible glitch on the Parity-error line.
  227. * Suppress it around the reset, both in chip-level
  228. * hwerrmask and in IB uC control reg. uC will allow
  229. * it again during startup.
  230. */
  231. u64 val;
  232. rst_val &= ~(1ULL);
  233. qib_write_kreg(dd, kr_hwerrmask,
  234. dd->cspec->hwerrmask &
  235. ~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);
  236. ret = qib_resync_ibepb(dd);
  237. if (ret < 0)
  238. qib_dev_err(dd, "unable to re-sync IB EPB\n");
  239. /* set uC control regs to suppress parity errs */
  240. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG5, 1, 1);
  241. if (ret < 0)
  242. goto bail;
  243. /* IB uC code past Version 1.32.17 allow suppression of wdog */
  244. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0x80,
  245. 0x80);
  246. if (ret < 0) {
  247. qib_dev_err(dd, "Failed to set WDOG disable\n");
  248. goto bail;
  249. }
  250. qib_write_kreg(dd, kr_ibserdesctrl, rst_val);
  251. /* flush write, delay for startup */
  252. qib_read_kreg32(dd, kr_scratch);
  253. udelay(1);
  254. /* clear, then re-enable parity errs */
  255. qib_sd7220_clr_ibpar(dd);
  256. val = qib_read_kreg64(dd, kr_hwerrstatus);
  257. if (val & QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR) {
  258. qib_dev_err(dd, "IBUC Parity still set after RST\n");
  259. dd->cspec->hwerrmask &=
  260. ~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR;
  261. }
  262. qib_write_kreg(dd, kr_hwerrmask,
  263. dd->cspec->hwerrmask);
  264. }
  265. bail:
  266. return ret;
  267. }
  268. static void qib_sd_trimdone_monitor(struct qib_devdata *dd,
  269. const char *where)
  270. {
  271. int ret, chn, baduns;
  272. u64 val;
  273. if (!where)
  274. where = "?";
  275. /* give time for reset to settle out in EPB */
  276. udelay(2);
  277. ret = qib_resync_ibepb(dd);
  278. if (ret < 0)
  279. qib_dev_err(dd, "not able to re-sync IB EPB (%s)\n", where);
  280. /* Do "sacrificial read" to get EPB in sane state after reset */
  281. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_CTRL2(0), 0, 0);
  282. if (ret < 0)
  283. qib_dev_err(dd, "Failed TRIMDONE 1st read, (%s)\n", where);
  284. /* Check/show "summary" Trim-done bit in IBCStatus */
  285. val = qib_read_kreg64(dd, kr_ibcstatus);
  286. if (!(val & (1ULL << 11)))
  287. qib_dev_err(dd, "IBCS TRIMDONE clear (%s)\n", where);
  288. /*
  289. * Do "dummy read/mod/wr" to get EPB in sane state after reset
  290. * The default value for MPREG6 is 0.
  291. */
  292. udelay(2);
  293. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0x80, 0x80);
  294. if (ret < 0)
  295. qib_dev_err(dd, "Failed Dummy RMW, (%s)\n", where);
  296. udelay(10);
  297. baduns = 0;
  298. for (chn = 3; chn >= 0; --chn) {
  299. /* Read CTRL reg for each channel to check TRIMDONE */
  300. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
  301. IB_CTRL2(chn), 0, 0);
  302. if (ret < 0)
  303. qib_dev_err(dd, "Failed checking TRIMDONE, chn %d"
  304. " (%s)\n", chn, where);
  305. if (!(ret & 0x10)) {
  306. int probe;
  307. baduns |= (1 << chn);
  308. qib_dev_err(dd, "TRIMDONE cleared on chn %d (%02X)."
  309. " (%s)\n", chn, ret, where);
  310. probe = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
  311. IB_PGUDP(0), 0, 0);
  312. qib_dev_err(dd, "probe is %d (%02X)\n",
  313. probe, probe);
  314. probe = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
  315. IB_CTRL2(chn), 0, 0);
  316. qib_dev_err(dd, "re-read: %d (%02X)\n",
  317. probe, probe);
  318. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
  319. IB_CTRL2(chn), 0x10, 0x10);
  320. if (ret < 0)
  321. qib_dev_err(dd,
  322. "Err on TRIMDONE rewrite1\n");
  323. }
  324. }
  325. for (chn = 3; chn >= 0; --chn) {
  326. /* Read CTRL reg for each channel to check TRIMDONE */
  327. if (baduns & (1 << chn)) {
  328. qib_dev_err(dd,
  329. "Reseting TRIMDONE on chn %d (%s)\n",
  330. chn, where);
  331. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
  332. IB_CTRL2(chn), 0x10, 0x10);
  333. if (ret < 0)
  334. qib_dev_err(dd, "Failed re-setting "
  335. "TRIMDONE, chn %d (%s)\n",
  336. chn, where);
  337. }
  338. }
  339. }
  340. /*
  341. * Below is portion of IBA7220-specific bringup_serdes() that actually
  342. * deals with registers and memory within the SerDes itself.
  343. * Post IB uC code version 1.32.17, was_reset being 1 is not really
  344. * informative, so we double-check.
  345. */
  346. int qib_sd7220_init(struct qib_devdata *dd)
  347. {
  348. int ret = 1; /* default to failure */
  349. int first_reset, was_reset;
  350. /* SERDES MPU reset recorded in D0 */
  351. was_reset = (qib_read_kreg64(dd, kr_ibserdesctrl) & 1);
  352. if (!was_reset) {
  353. /* entered with reset not asserted, we need to do it */
  354. qib_ibsd_reset(dd, 1);
  355. qib_sd_trimdone_monitor(dd, "Driver-reload");
  356. }
  357. /* Substitute our deduced value for was_reset */
  358. ret = qib_ibsd_ucode_loaded(dd->pport);
  359. if (ret < 0)
  360. goto bail;
  361. first_reset = !ret; /* First reset if IBSD uCode not yet loaded */
  362. /*
  363. * Alter some regs per vendor latest doc, reset-defaults
  364. * are not right for IB.
  365. */
  366. ret = qib_sd_early(dd);
  367. if (ret < 0) {
  368. qib_dev_err(dd, "Failed to set IB SERDES early defaults\n");
  369. goto bail;
  370. }
  371. /*
  372. * Set DAC manual trim IB.
  373. * We only do this once after chip has been reset (usually
  374. * same as once per system boot).
  375. */
  376. if (first_reset) {
  377. ret = qib_sd_dactrim(dd);
  378. if (ret < 0) {
  379. qib_dev_err(dd, "Failed IB SERDES DAC trim\n");
  380. goto bail;
  381. }
  382. }
  383. /*
  384. * Set various registers (DDS and RXEQ) that will be
  385. * controlled by IBC (in 1.2 mode) to reasonable preset values
  386. * Calling the "internal" version avoids the "check for needed"
  387. * and "trimdone monitor" that might be counter-productive.
  388. */
  389. ret = qib_internal_presets(dd);
  390. if (ret < 0) {
  391. qib_dev_err(dd, "Failed to set IB SERDES presets\n");
  392. goto bail;
  393. }
  394. ret = qib_sd_trimself(dd, 0x80);
  395. if (ret < 0) {
  396. qib_dev_err(dd, "Failed to set IB SERDES TRIMSELF\n");
  397. goto bail;
  398. }
  399. /* Load image, then try to verify */
  400. ret = 0; /* Assume success */
  401. if (first_reset) {
  402. int vfy;
  403. int trim_done;
  404. ret = qib_sd7220_ib_load(dd);
  405. if (ret < 0) {
  406. qib_dev_err(dd, "Failed to load IB SERDES image\n");
  407. goto bail;
  408. } else {
  409. /* Loaded image, try to verify */
  410. vfy = qib_sd7220_ib_vfy(dd);
  411. if (vfy != ret) {
  412. qib_dev_err(dd, "SERDES PRAM VFY failed\n");
  413. goto bail;
  414. } /* end if verified */
  415. } /* end if loaded */
  416. /*
  417. * Loaded and verified. Almost good...
  418. * hold "success" in ret
  419. */
  420. ret = 0;
  421. /*
  422. * Prev steps all worked, continue bringup
  423. * De-assert RESET to uC, only in first reset, to allow
  424. * trimming.
  425. *
  426. * Since our default setup sets START_EQ1 to
  427. * PRESET, we need to clear that for this very first run.
  428. */
  429. ret = ibsd_mod_allchnls(dd, START_EQ1(0), 0, 0x38);
  430. if (ret < 0) {
  431. qib_dev_err(dd, "Failed clearing START_EQ1\n");
  432. goto bail;
  433. }
  434. qib_ibsd_reset(dd, 0);
  435. /*
  436. * If this is not the first reset, trimdone should be set
  437. * already. We may need to check about this.
  438. */
  439. trim_done = qib_sd_trimdone_poll(dd);
  440. /*
  441. * Whether or not trimdone succeeded, we need to put the
  442. * uC back into reset to avoid a possible fight with the
  443. * IBC state-machine.
  444. */
  445. qib_ibsd_reset(dd, 1);
  446. if (!trim_done) {
  447. qib_dev_err(dd, "No TRIMDONE seen\n");
  448. goto bail;
  449. }
  450. /*
  451. * DEBUG: check each time we reset if trimdone bits have
  452. * gotten cleared, and re-set them.
  453. */
  454. qib_sd_trimdone_monitor(dd, "First-reset");
  455. /* Remember so we do not re-do the load, dactrim, etc. */
  456. dd->cspec->serdes_first_init_done = 1;
  457. }
  458. /*
  459. * setup for channel training and load values for
  460. * RxEq and DDS in tables used by IBC in IB1.2 mode
  461. */
  462. ret = 0;
  463. if (qib_sd_setvals(dd) >= 0)
  464. goto done;
  465. bail:
  466. ret = 1;
  467. done:
  468. /* start relock timer regardless, but start at 1 second */
  469. set_7220_relock_poll(dd, -1);
  470. return ret;
  471. }
  472. #define EPB_ACC_REQ 1
  473. #define EPB_ACC_GNT 0x100
  474. #define EPB_DATA_MASK 0xFF
  475. #define EPB_RD (1ULL << 24)
  476. #define EPB_TRANS_RDY (1ULL << 31)
  477. #define EPB_TRANS_ERR (1ULL << 30)
  478. #define EPB_TRANS_TRIES 5
  479. /*
  480. * query, claim, release ownership of the EPB (External Parallel Bus)
  481. * for a specified SERDES.
  482. * the "claim" parameter is >0 to claim, <0 to release, 0 to query.
  483. * Returns <0 for errors, >0 if we had ownership, else 0.
  484. */
  485. static int epb_access(struct qib_devdata *dd, int sdnum, int claim)
  486. {
  487. u16 acc;
  488. u64 accval;
  489. int owned = 0;
  490. u64 oct_sel = 0;
  491. switch (sdnum) {
  492. case IB_7220_SERDES:
  493. /*
  494. * The IB SERDES "ownership" is fairly simple. A single each
  495. * request/grant.
  496. */
  497. acc = kr_ibsd_epb_access_ctrl;
  498. break;
  499. case PCIE_SERDES0:
  500. case PCIE_SERDES1:
  501. /* PCIe SERDES has two "octants", need to select which */
  502. acc = kr_pciesd_epb_access_ctrl;
  503. oct_sel = (2 << (sdnum - PCIE_SERDES0));
  504. break;
  505. default:
  506. return 0;
  507. }
  508. /* Make sure any outstanding transaction was seen */
  509. qib_read_kreg32(dd, kr_scratch);
  510. udelay(15);
  511. accval = qib_read_kreg32(dd, acc);
  512. owned = !!(accval & EPB_ACC_GNT);
  513. if (claim < 0) {
  514. /* Need to release */
  515. u64 pollval;
  516. /*
  517. * The only writeable bits are the request and CS.
  518. * Both should be clear
  519. */
  520. u64 newval = 0;
  521. qib_write_kreg(dd, acc, newval);
  522. /* First read after write is not trustworthy */
  523. pollval = qib_read_kreg32(dd, acc);
  524. udelay(5);
  525. pollval = qib_read_kreg32(dd, acc);
  526. if (pollval & EPB_ACC_GNT)
  527. owned = -1;
  528. } else if (claim > 0) {
  529. /* Need to claim */
  530. u64 pollval;
  531. u64 newval = EPB_ACC_REQ | oct_sel;
  532. qib_write_kreg(dd, acc, newval);
  533. /* First read after write is not trustworthy */
  534. pollval = qib_read_kreg32(dd, acc);
  535. udelay(5);
  536. pollval = qib_read_kreg32(dd, acc);
  537. if (!(pollval & EPB_ACC_GNT))
  538. owned = -1;
  539. }
  540. return owned;
  541. }
  542. /*
  543. * Lemma to deal with race condition of write..read to epb regs
  544. */
  545. static int epb_trans(struct qib_devdata *dd, u16 reg, u64 i_val, u64 *o_vp)
  546. {
  547. int tries;
  548. u64 transval;
  549. qib_write_kreg(dd, reg, i_val);
  550. /* Throw away first read, as RDY bit may be stale */
  551. transval = qib_read_kreg64(dd, reg);
  552. for (tries = EPB_TRANS_TRIES; tries; --tries) {
  553. transval = qib_read_kreg32(dd, reg);
  554. if (transval & EPB_TRANS_RDY)
  555. break;
  556. udelay(5);
  557. }
  558. if (transval & EPB_TRANS_ERR)
  559. return -1;
  560. if (tries > 0 && o_vp)
  561. *o_vp = transval;
  562. return tries;
  563. }
  564. /**
  565. * qib_sd7220_reg_mod - modify SERDES register
  566. * @dd: the qlogic_ib device
  567. * @sdnum: which SERDES to access
  568. * @loc: location - channel, element, register, as packed by EPB_LOC() macro.
  569. * @wd: Write Data - value to set in register
  570. * @mask: ones where data should be spliced into reg.
  571. *
  572. * Basic register read/modify/write, with un-needed acesses elided. That is,
  573. * a mask of zero will prevent write, while a mask of 0xFF will prevent read.
  574. * returns current (presumed, if a write was done) contents of selected
  575. * register, or <0 if errors.
  576. */
  577. static int qib_sd7220_reg_mod(struct qib_devdata *dd, int sdnum, u32 loc,
  578. u32 wd, u32 mask)
  579. {
  580. u16 trans;
  581. u64 transval;
  582. int owned;
  583. int tries, ret;
  584. unsigned long flags;
  585. switch (sdnum) {
  586. case IB_7220_SERDES:
  587. trans = kr_ibsd_epb_transaction_reg;
  588. break;
  589. case PCIE_SERDES0:
  590. case PCIE_SERDES1:
  591. trans = kr_pciesd_epb_transaction_reg;
  592. break;
  593. default:
  594. return -1;
  595. }
  596. /*
  597. * All access is locked in software (vs other host threads) and
  598. * hardware (vs uC access).
  599. */
  600. spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
  601. owned = epb_access(dd, sdnum, 1);
  602. if (owned < 0) {
  603. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  604. return -1;
  605. }
  606. ret = 0;
  607. for (tries = EPB_TRANS_TRIES; tries; --tries) {
  608. transval = qib_read_kreg32(dd, trans);
  609. if (transval & EPB_TRANS_RDY)
  610. break;
  611. udelay(5);
  612. }
  613. if (tries > 0) {
  614. tries = 1; /* to make read-skip work */
  615. if (mask != 0xFF) {
  616. /*
  617. * Not a pure write, so need to read.
  618. * loc encodes chip-select as well as address
  619. */
  620. transval = loc | EPB_RD;
  621. tries = epb_trans(dd, trans, transval, &transval);
  622. }
  623. if (tries > 0 && mask != 0) {
  624. /*
  625. * Not a pure read, so need to write.
  626. */
  627. wd = (wd & mask) | (transval & ~mask);
  628. transval = loc | (wd & EPB_DATA_MASK);
  629. tries = epb_trans(dd, trans, transval, &transval);
  630. }
  631. }
  632. /* else, failed to see ready, what error-handling? */
  633. /*
  634. * Release bus. Failure is an error.
  635. */
  636. if (epb_access(dd, sdnum, -1) < 0)
  637. ret = -1;
  638. else
  639. ret = transval & EPB_DATA_MASK;
  640. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  641. if (tries <= 0)
  642. ret = -1;
  643. return ret;
  644. }
  645. #define EPB_ROM_R (2)
  646. #define EPB_ROM_W (1)
  647. /*
  648. * Below, all uC-related, use appropriate UC_CS, depending
  649. * on which SerDes is used.
  650. */
  651. #define EPB_UC_CTL EPB_LOC(6, 0, 0)
  652. #define EPB_MADDRL EPB_LOC(6, 0, 2)
  653. #define EPB_MADDRH EPB_LOC(6, 0, 3)
  654. #define EPB_ROMDATA EPB_LOC(6, 0, 4)
  655. #define EPB_RAMDATA EPB_LOC(6, 0, 5)
  656. /* Transfer date to/from uC Program RAM of IB or PCIe SerDes */
  657. static int qib_sd7220_ram_xfer(struct qib_devdata *dd, int sdnum, u32 loc,
  658. u8 *buf, int cnt, int rd_notwr)
  659. {
  660. u16 trans;
  661. u64 transval;
  662. u64 csbit;
  663. int owned;
  664. int tries;
  665. int sofar;
  666. int addr;
  667. int ret;
  668. unsigned long flags;
  669. const char *op;
  670. /* Pick appropriate transaction reg and "Chip select" for this serdes */
  671. switch (sdnum) {
  672. case IB_7220_SERDES:
  673. csbit = 1ULL << EPB_IB_UC_CS_SHF;
  674. trans = kr_ibsd_epb_transaction_reg;
  675. break;
  676. case PCIE_SERDES0:
  677. case PCIE_SERDES1:
  678. /* PCIe SERDES has uC "chip select" in different bit, too */
  679. csbit = 1ULL << EPB_PCIE_UC_CS_SHF;
  680. trans = kr_pciesd_epb_transaction_reg;
  681. break;
  682. default:
  683. return -1;
  684. }
  685. op = rd_notwr ? "Rd" : "Wr";
  686. spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
  687. owned = epb_access(dd, sdnum, 1);
  688. if (owned < 0) {
  689. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  690. return -1;
  691. }
  692. /*
  693. * In future code, we may need to distinguish several address ranges,
  694. * and select various memories based on this. For now, just trim
  695. * "loc" (location including address and memory select) to
  696. * "addr" (address within memory). we will only support PRAM
  697. * The memory is 8KB.
  698. */
  699. addr = loc & 0x1FFF;
  700. for (tries = EPB_TRANS_TRIES; tries; --tries) {
  701. transval = qib_read_kreg32(dd, trans);
  702. if (transval & EPB_TRANS_RDY)
  703. break;
  704. udelay(5);
  705. }
  706. sofar = 0;
  707. if (tries > 0) {
  708. /*
  709. * Every "memory" access is doubly-indirect.
  710. * We set two bytes of address, then read/write
  711. * one or mores bytes of data.
  712. */
  713. /* First, we set control to "Read" or "Write" */
  714. transval = csbit | EPB_UC_CTL |
  715. (rd_notwr ? EPB_ROM_R : EPB_ROM_W);
  716. tries = epb_trans(dd, trans, transval, &transval);
  717. while (tries > 0 && sofar < cnt) {
  718. if (!sofar) {
  719. /* Only set address at start of chunk */
  720. int addrbyte = (addr + sofar) >> 8;
  721. transval = csbit | EPB_MADDRH | addrbyte;
  722. tries = epb_trans(dd, trans, transval,
  723. &transval);
  724. if (tries <= 0)
  725. break;
  726. addrbyte = (addr + sofar) & 0xFF;
  727. transval = csbit | EPB_MADDRL | addrbyte;
  728. tries = epb_trans(dd, trans, transval,
  729. &transval);
  730. if (tries <= 0)
  731. break;
  732. }
  733. if (rd_notwr)
  734. transval = csbit | EPB_ROMDATA | EPB_RD;
  735. else
  736. transval = csbit | EPB_ROMDATA | buf[sofar];
  737. tries = epb_trans(dd, trans, transval, &transval);
  738. if (tries <= 0)
  739. break;
  740. if (rd_notwr)
  741. buf[sofar] = transval & EPB_DATA_MASK;
  742. ++sofar;
  743. }
  744. /* Finally, clear control-bit for Read or Write */
  745. transval = csbit | EPB_UC_CTL;
  746. tries = epb_trans(dd, trans, transval, &transval);
  747. }
  748. ret = sofar;
  749. /* Release bus. Failure is an error */
  750. if (epb_access(dd, sdnum, -1) < 0)
  751. ret = -1;
  752. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  753. if (tries <= 0)
  754. ret = -1;
  755. return ret;
  756. }
  757. #define PROG_CHUNK 64
  758. int qib_sd7220_prog_ld(struct qib_devdata *dd, int sdnum,
  759. u8 *img, int len, int offset)
  760. {
  761. int cnt, sofar, req;
  762. sofar = 0;
  763. while (sofar < len) {
  764. req = len - sofar;
  765. if (req > PROG_CHUNK)
  766. req = PROG_CHUNK;
  767. cnt = qib_sd7220_ram_xfer(dd, sdnum, offset + sofar,
  768. img + sofar, req, 0);
  769. if (cnt < req) {
  770. sofar = -1;
  771. break;
  772. }
  773. sofar += req;
  774. }
  775. return sofar;
  776. }
  777. #define VFY_CHUNK 64
  778. #define SD_PRAM_ERROR_LIMIT 42
  779. int qib_sd7220_prog_vfy(struct qib_devdata *dd, int sdnum,
  780. const u8 *img, int len, int offset)
  781. {
  782. int cnt, sofar, req, idx, errors;
  783. unsigned char readback[VFY_CHUNK];
  784. errors = 0;
  785. sofar = 0;
  786. while (sofar < len) {
  787. req = len - sofar;
  788. if (req > VFY_CHUNK)
  789. req = VFY_CHUNK;
  790. cnt = qib_sd7220_ram_xfer(dd, sdnum, sofar + offset,
  791. readback, req, 1);
  792. if (cnt < req) {
  793. /* failed in read itself */
  794. sofar = -1;
  795. break;
  796. }
  797. for (idx = 0; idx < cnt; ++idx) {
  798. if (readback[idx] != img[idx+sofar])
  799. ++errors;
  800. }
  801. sofar += cnt;
  802. }
  803. return errors ? -errors : sofar;
  804. }
  805. /*
  806. * IRQ not set up at this point in init, so we poll.
  807. */
  808. #define IB_SERDES_TRIM_DONE (1ULL << 11)
  809. #define TRIM_TMO (30)
  810. static int qib_sd_trimdone_poll(struct qib_devdata *dd)
  811. {
  812. int trim_tmo, ret;
  813. uint64_t val;
  814. /*
  815. * Default to failure, so IBC will not start
  816. * without IB_SERDES_TRIM_DONE.
  817. */
  818. ret = 0;
  819. for (trim_tmo = 0; trim_tmo < TRIM_TMO; ++trim_tmo) {
  820. val = qib_read_kreg64(dd, kr_ibcstatus);
  821. if (val & IB_SERDES_TRIM_DONE) {
  822. ret = 1;
  823. break;
  824. }
  825. msleep(10);
  826. }
  827. if (trim_tmo >= TRIM_TMO) {
  828. qib_dev_err(dd, "No TRIMDONE in %d tries\n", trim_tmo);
  829. ret = 0;
  830. }
  831. return ret;
  832. }
  833. #define TX_FAST_ELT (9)
  834. /*
  835. * Set the "negotiation" values for SERDES. These are used by the IB1.2
  836. * link negotiation. Macros below are attempt to keep the values a
  837. * little more human-editable.
  838. * First, values related to Drive De-emphasis Settings.
  839. */
  840. #define NUM_DDS_REGS 6
  841. #define DDS_REG_MAP 0x76A910 /* LSB-first list of regs (in elt 9) to mod */
  842. #define DDS_VAL(amp_d, main_d, ipst_d, ipre_d, amp_s, main_s, ipst_s, ipre_s) \
  843. { { ((amp_d & 0x1F) << 1) | 1, ((amp_s & 0x1F) << 1) | 1, \
  844. (main_d << 3) | 4 | (ipre_d >> 2), \
  845. (main_s << 3) | 4 | (ipre_s >> 2), \
  846. ((ipst_d & 0xF) << 1) | ((ipre_d & 3) << 6) | 0x21, \
  847. ((ipst_s & 0xF) << 1) | ((ipre_s & 3) << 6) | 0x21 } }
  848. static struct dds_init {
  849. uint8_t reg_vals[NUM_DDS_REGS];
  850. } dds_init_vals[] = {
  851. /* DDR(FDR) SDR(HDR) */
  852. /* Vendor recommends below for 3m cable */
  853. #define DDS_3M 0
  854. DDS_VAL(31, 19, 12, 0, 29, 22, 9, 0),
  855. DDS_VAL(31, 12, 15, 4, 31, 15, 15, 1),
  856. DDS_VAL(31, 13, 15, 3, 31, 16, 15, 0),
  857. DDS_VAL(31, 14, 15, 2, 31, 17, 14, 0),
  858. DDS_VAL(31, 15, 15, 1, 31, 18, 13, 0),
  859. DDS_VAL(31, 16, 15, 0, 31, 19, 12, 0),
  860. DDS_VAL(31, 17, 14, 0, 31, 20, 11, 0),
  861. DDS_VAL(31, 18, 13, 0, 30, 21, 10, 0),
  862. DDS_VAL(31, 20, 11, 0, 28, 23, 8, 0),
  863. DDS_VAL(31, 21, 10, 0, 27, 24, 7, 0),
  864. DDS_VAL(31, 22, 9, 0, 26, 25, 6, 0),
  865. DDS_VAL(30, 23, 8, 0, 25, 26, 5, 0),
  866. DDS_VAL(29, 24, 7, 0, 23, 27, 4, 0),
  867. /* Vendor recommends below for 1m cable */
  868. #define DDS_1M 13
  869. DDS_VAL(28, 25, 6, 0, 21, 28, 3, 0),
  870. DDS_VAL(27, 26, 5, 0, 19, 29, 2, 0),
  871. DDS_VAL(25, 27, 4, 0, 17, 30, 1, 0)
  872. };
  873. /*
  874. * Now the RXEQ section of the table.
  875. */
  876. /* Hardware packs an element number and register address thus: */
  877. #define RXEQ_INIT_RDESC(elt, addr) (((elt) & 0xF) | ((addr) << 4))
  878. #define RXEQ_VAL(elt, adr, val0, val1, val2, val3) \
  879. {RXEQ_INIT_RDESC((elt), (adr)), {(val0), (val1), (val2), (val3)} }
  880. #define RXEQ_VAL_ALL(elt, adr, val) \
  881. {RXEQ_INIT_RDESC((elt), (adr)), {(val), (val), (val), (val)} }
  882. #define RXEQ_SDR_DFELTH 0
  883. #define RXEQ_SDR_TLTH 0
  884. #define RXEQ_SDR_G1CNT_Z1CNT 0x11
  885. #define RXEQ_SDR_ZCNT 23
  886. static struct rxeq_init {
  887. u16 rdesc; /* in form used in SerDesDDSRXEQ */
  888. u8 rdata[4];
  889. } rxeq_init_vals[] = {
  890. /* Set Rcv Eq. to Preset node */
  891. RXEQ_VAL_ALL(7, 0x27, 0x10),
  892. /* Set DFELTHFDR/HDR thresholds */
  893. RXEQ_VAL(7, 8, 0, 0, 0, 0), /* FDR, was 0, 1, 2, 3 */
  894. RXEQ_VAL(7, 0x21, 0, 0, 0, 0), /* HDR */
  895. /* Set TLTHFDR/HDR theshold */
  896. RXEQ_VAL(7, 9, 2, 2, 2, 2), /* FDR, was 0, 2, 4, 6 */
  897. RXEQ_VAL(7, 0x23, 2, 2, 2, 2), /* HDR, was 0, 1, 2, 3 */
  898. /* Set Preamp setting 2 (ZFR/ZCNT) */
  899. RXEQ_VAL(7, 0x1B, 12, 12, 12, 12), /* FDR, was 12, 16, 20, 24 */
  900. RXEQ_VAL(7, 0x1C, 12, 12, 12, 12), /* HDR, was 12, 16, 20, 24 */
  901. /* Set Preamp DC gain and Setting 1 (GFR/GHR) */
  902. RXEQ_VAL(7, 0x1E, 16, 16, 16, 16), /* FDR, was 16, 17, 18, 20 */
  903. RXEQ_VAL(7, 0x1F, 16, 16, 16, 16), /* HDR, was 16, 17, 18, 20 */
  904. /* Toggle RELOCK (in VCDL_CTRL0) to lock to data */
  905. RXEQ_VAL_ALL(6, 6, 0x20), /* Set D5 High */
  906. RXEQ_VAL_ALL(6, 6, 0), /* Set D5 Low */
  907. };
  908. /* There are 17 values from vendor, but IBC only accesses the first 16 */
  909. #define DDS_ROWS (16)
  910. #define RXEQ_ROWS ARRAY_SIZE(rxeq_init_vals)
  911. static int qib_sd_setvals(struct qib_devdata *dd)
  912. {
  913. int idx, midx;
  914. int min_idx; /* Minimum index for this portion of table */
  915. uint32_t dds_reg_map;
  916. u64 __iomem *taddr, *iaddr;
  917. uint64_t data;
  918. uint64_t sdctl;
  919. taddr = dd->kregbase + kr_serdes_maptable;
  920. iaddr = dd->kregbase + kr_serdes_ddsrxeq0;
  921. /*
  922. * Init the DDS section of the table.
  923. * Each "row" of the table provokes NUM_DDS_REG writes, to the
  924. * registers indicated in DDS_REG_MAP.
  925. */
  926. sdctl = qib_read_kreg64(dd, kr_ibserdesctrl);
  927. sdctl = (sdctl & ~(0x1f << 8)) | (NUM_DDS_REGS << 8);
  928. sdctl = (sdctl & ~(0x1f << 13)) | (RXEQ_ROWS << 13);
  929. qib_write_kreg(dd, kr_ibserdesctrl, sdctl);
  930. /*
  931. * Iterate down table within loop for each register to store.
  932. */
  933. dds_reg_map = DDS_REG_MAP;
  934. for (idx = 0; idx < NUM_DDS_REGS; ++idx) {
  935. data = ((dds_reg_map & 0xF) << 4) | TX_FAST_ELT;
  936. writeq(data, iaddr + idx);
  937. mmiowb();
  938. qib_read_kreg32(dd, kr_scratch);
  939. dds_reg_map >>= 4;
  940. for (midx = 0; midx < DDS_ROWS; ++midx) {
  941. u64 __iomem *daddr = taddr + ((midx << 4) + idx);
  942. data = dds_init_vals[midx].reg_vals[idx];
  943. writeq(data, daddr);
  944. mmiowb();
  945. qib_read_kreg32(dd, kr_scratch);
  946. } /* End inner for (vals for this reg, each row) */
  947. } /* end outer for (regs to be stored) */
  948. /*
  949. * Init the RXEQ section of the table.
  950. * This runs in a different order, as the pattern of
  951. * register references is more complex, but there are only
  952. * four "data" values per register.
  953. */
  954. min_idx = idx; /* RXEQ indices pick up where DDS left off */
  955. taddr += 0x100; /* RXEQ data is in second half of table */
  956. /* Iterate through RXEQ register addresses */
  957. for (idx = 0; idx < RXEQ_ROWS; ++idx) {
  958. int didx; /* "destination" */
  959. int vidx;
  960. /* didx is offset by min_idx to address RXEQ range of regs */
  961. didx = idx + min_idx;
  962. /* Store the next RXEQ register address */
  963. writeq(rxeq_init_vals[idx].rdesc, iaddr + didx);
  964. mmiowb();
  965. qib_read_kreg32(dd, kr_scratch);
  966. /* Iterate through RXEQ values */
  967. for (vidx = 0; vidx < 4; vidx++) {
  968. data = rxeq_init_vals[idx].rdata[vidx];
  969. writeq(data, taddr + (vidx << 6) + idx);
  970. mmiowb();
  971. qib_read_kreg32(dd, kr_scratch);
  972. }
  973. } /* end outer for (Reg-writes for RXEQ) */
  974. return 0;
  975. }
  976. #define CMUCTRL5 EPB_LOC(7, 0, 0x15)
  977. #define RXHSCTRL0(chan) EPB_LOC(chan, 6, 0)
  978. #define VCDL_DAC2(chan) EPB_LOC(chan, 6, 5)
  979. #define VCDL_CTRL0(chan) EPB_LOC(chan, 6, 6)
  980. #define VCDL_CTRL2(chan) EPB_LOC(chan, 6, 8)
  981. #define START_EQ2(chan) EPB_LOC(chan, 7, 0x28)
  982. /*
  983. * Repeat a "store" across all channels of the IB SerDes.
  984. * Although nominally it inherits the "read value" of the last
  985. * channel it modified, the only really useful return is <0 for
  986. * failure, >= 0 for success. The parameter 'loc' is assumed to
  987. * be the location in some channel of the register to be modified
  988. * The caller can specify use of the "gang write" option of EPB,
  989. * in which case we use the specified channel data for any fields
  990. * not explicitely written.
  991. */
  992. static int ibsd_mod_allchnls(struct qib_devdata *dd, int loc, int val,
  993. int mask)
  994. {
  995. int ret = -1;
  996. int chnl;
  997. if (loc & EPB_GLOBAL_WR) {
  998. /*
  999. * Our caller has assured us that we can set all four
  1000. * channels at once. Trust that. If mask is not 0xFF,
  1001. * we will read the _specified_ channel for our starting
  1002. * value.
  1003. */
  1004. loc |= (1U << EPB_IB_QUAD0_CS_SHF);
  1005. chnl = (loc >> (4 + EPB_ADDR_SHF)) & 7;
  1006. if (mask != 0xFF) {
  1007. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
  1008. loc & ~EPB_GLOBAL_WR, 0, 0);
  1009. if (ret < 0) {
  1010. int sloc = loc >> EPB_ADDR_SHF;
  1011. qib_dev_err(dd, "pre-read failed: elt %d,"
  1012. " addr 0x%X, chnl %d\n",
  1013. (sloc & 0xF),
  1014. (sloc >> 9) & 0x3f, chnl);
  1015. return ret;
  1016. }
  1017. val = (ret & ~mask) | (val & mask);
  1018. }
  1019. loc &= ~(7 << (4+EPB_ADDR_SHF));
  1020. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);
  1021. if (ret < 0) {
  1022. int sloc = loc >> EPB_ADDR_SHF;
  1023. qib_dev_err(dd, "Global WR failed: elt %d,"
  1024. " addr 0x%X, val %02X\n",
  1025. (sloc & 0xF), (sloc >> 9) & 0x3f, val);
  1026. }
  1027. return ret;
  1028. }
  1029. /* Clear "channel" and set CS so we can simply iterate */
  1030. loc &= ~(7 << (4+EPB_ADDR_SHF));
  1031. loc |= (1U << EPB_IB_QUAD0_CS_SHF);
  1032. for (chnl = 0; chnl < 4; ++chnl) {
  1033. int cloc = loc | (chnl << (4+EPB_ADDR_SHF));
  1034. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, cloc, val, mask);
  1035. if (ret < 0) {
  1036. int sloc = loc >> EPB_ADDR_SHF;
  1037. qib_dev_err(dd, "Write failed: elt %d,"
  1038. " addr 0x%X, chnl %d, val 0x%02X,"
  1039. " mask 0x%02X\n",
  1040. (sloc & 0xF), (sloc >> 9) & 0x3f, chnl,
  1041. val & 0xFF, mask & 0xFF);
  1042. break;
  1043. }
  1044. }
  1045. return ret;
  1046. }
  1047. /*
  1048. * Set the Tx values normally modified by IBC in IB1.2 mode to default
  1049. * values, as gotten from first row of init table.
  1050. */
  1051. static int set_dds_vals(struct qib_devdata *dd, struct dds_init *ddi)
  1052. {
  1053. int ret;
  1054. int idx, reg, data;
  1055. uint32_t regmap;
  1056. regmap = DDS_REG_MAP;
  1057. for (idx = 0; idx < NUM_DDS_REGS; ++idx) {
  1058. reg = (regmap & 0xF);
  1059. regmap >>= 4;
  1060. data = ddi->reg_vals[idx];
  1061. /* Vendor says RMW not needed for these regs, use 0xFF mask */
  1062. ret = ibsd_mod_allchnls(dd, EPB_LOC(0, 9, reg), data, 0xFF);
  1063. if (ret < 0)
  1064. break;
  1065. }
  1066. return ret;
  1067. }
  1068. /*
  1069. * Set the Rx values normally modified by IBC in IB1.2 mode to default
  1070. * values, as gotten from selected column of init table.
  1071. */
  1072. static int set_rxeq_vals(struct qib_devdata *dd, int vsel)
  1073. {
  1074. int ret;
  1075. int ridx;
  1076. int cnt = ARRAY_SIZE(rxeq_init_vals);
  1077. for (ridx = 0; ridx < cnt; ++ridx) {
  1078. int elt, reg, val, loc;
  1079. elt = rxeq_init_vals[ridx].rdesc & 0xF;
  1080. reg = rxeq_init_vals[ridx].rdesc >> 4;
  1081. loc = EPB_LOC(0, elt, reg);
  1082. val = rxeq_init_vals[ridx].rdata[vsel];
  1083. /* mask of 0xFF, because hardware does full-byte store. */
  1084. ret = ibsd_mod_allchnls(dd, loc, val, 0xFF);
  1085. if (ret < 0)
  1086. break;
  1087. }
  1088. return ret;
  1089. }
  1090. /*
  1091. * Set the default values (row 0) for DDR Driver Demphasis.
  1092. * we do this initially and whenever we turn off IB-1.2
  1093. *
  1094. * The "default" values for Rx equalization are also stored to
  1095. * SerDes registers. Formerly (and still default), we used set 2.
  1096. * For experimenting with cables and link-partners, we allow changing
  1097. * that via a module parameter.
  1098. */
  1099. static unsigned qib_rxeq_set = 2;
  1100. module_param_named(rxeq_default_set, qib_rxeq_set, uint,
  1101. S_IWUSR | S_IRUGO);
  1102. MODULE_PARM_DESC(rxeq_default_set,
  1103. "Which set [0..3] of Rx Equalization values is default");
  1104. static int qib_internal_presets(struct qib_devdata *dd)
  1105. {
  1106. int ret = 0;
  1107. ret = set_dds_vals(dd, dds_init_vals + DDS_3M);
  1108. if (ret < 0)
  1109. qib_dev_err(dd, "Failed to set default DDS values\n");
  1110. ret = set_rxeq_vals(dd, qib_rxeq_set & 3);
  1111. if (ret < 0)
  1112. qib_dev_err(dd, "Failed to set default RXEQ values\n");
  1113. return ret;
  1114. }
  1115. int qib_sd7220_presets(struct qib_devdata *dd)
  1116. {
  1117. int ret = 0;
  1118. if (!dd->cspec->presets_needed)
  1119. return ret;
  1120. dd->cspec->presets_needed = 0;
  1121. /* Assert uC reset, so we don't clash with it. */
  1122. qib_ibsd_reset(dd, 1);
  1123. udelay(2);
  1124. qib_sd_trimdone_monitor(dd, "link-down");
  1125. ret = qib_internal_presets(dd);
  1126. return ret;
  1127. }
  1128. static int qib_sd_trimself(struct qib_devdata *dd, int val)
  1129. {
  1130. int loc = CMUCTRL5 | (1U << EPB_IB_QUAD0_CS_SHF);
  1131. return qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);
  1132. }
  1133. static int qib_sd_early(struct qib_devdata *dd)
  1134. {
  1135. int ret;
  1136. ret = ibsd_mod_allchnls(dd, RXHSCTRL0(0) | EPB_GLOBAL_WR, 0xD4, 0xFF);
  1137. if (ret < 0)
  1138. goto bail;
  1139. ret = ibsd_mod_allchnls(dd, START_EQ1(0) | EPB_GLOBAL_WR, 0x10, 0xFF);
  1140. if (ret < 0)
  1141. goto bail;
  1142. ret = ibsd_mod_allchnls(dd, START_EQ2(0) | EPB_GLOBAL_WR, 0x30, 0xFF);
  1143. bail:
  1144. return ret;
  1145. }
  1146. #define BACTRL(chnl) EPB_LOC(chnl, 6, 0x0E)
  1147. #define LDOUTCTRL1(chnl) EPB_LOC(chnl, 7, 6)
  1148. #define RXHSSTATUS(chnl) EPB_LOC(chnl, 6, 0xF)
  1149. static int qib_sd_dactrim(struct qib_devdata *dd)
  1150. {
  1151. int ret;
  1152. ret = ibsd_mod_allchnls(dd, VCDL_DAC2(0) | EPB_GLOBAL_WR, 0x2D, 0xFF);
  1153. if (ret < 0)
  1154. goto bail;
  1155. /* more fine-tuning of what will be default */
  1156. ret = ibsd_mod_allchnls(dd, VCDL_CTRL2(0), 3, 0xF);
  1157. if (ret < 0)
  1158. goto bail;
  1159. ret = ibsd_mod_allchnls(dd, BACTRL(0) | EPB_GLOBAL_WR, 0x40, 0xFF);
  1160. if (ret < 0)
  1161. goto bail;
  1162. ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x04, 0xFF);
  1163. if (ret < 0)
  1164. goto bail;
  1165. ret = ibsd_mod_allchnls(dd, RXHSSTATUS(0) | EPB_GLOBAL_WR, 0x04, 0xFF);
  1166. if (ret < 0)
  1167. goto bail;
  1168. /*
  1169. * Delay for max possible number of steps, with slop.
  1170. * Each step is about 4usec.
  1171. */
  1172. udelay(415);
  1173. ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x00, 0xFF);
  1174. bail:
  1175. return ret;
  1176. }
  1177. #define RELOCK_FIRST_MS 3
  1178. #define RXLSPPM(chan) EPB_LOC(chan, 0, 2)
  1179. void toggle_7220_rclkrls(struct qib_devdata *dd)
  1180. {
  1181. int loc = RXLSPPM(0) | EPB_GLOBAL_WR;
  1182. int ret;
  1183. ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);
  1184. if (ret < 0)
  1185. qib_dev_err(dd, "RCLKRLS failed to clear D7\n");
  1186. else {
  1187. udelay(1);
  1188. ibsd_mod_allchnls(dd, loc, 0x80, 0x80);
  1189. }
  1190. /* And again for good measure */
  1191. udelay(1);
  1192. ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);
  1193. if (ret < 0)
  1194. qib_dev_err(dd, "RCLKRLS failed to clear D7\n");
  1195. else {
  1196. udelay(1);
  1197. ibsd_mod_allchnls(dd, loc, 0x80, 0x80);
  1198. }
  1199. /* Now reset xgxs and IBC to complete the recovery */
  1200. dd->f_xgxs_reset(dd->pport);
  1201. }
  1202. /*
  1203. * Shut down the timer that polls for relock occasions, if needed
  1204. * this is "hooked" from qib_7220_quiet_serdes(), which is called
  1205. * just before qib_shutdown_device() in qib_driver.c shuts down all
  1206. * the other timers
  1207. */
  1208. void shutdown_7220_relock_poll(struct qib_devdata *dd)
  1209. {
  1210. if (dd->cspec->relock_timer_active)
  1211. del_timer_sync(&dd->cspec->relock_timer);
  1212. }
  1213. static unsigned qib_relock_by_timer = 1;
  1214. module_param_named(relock_by_timer, qib_relock_by_timer, uint,
  1215. S_IWUSR | S_IRUGO);
  1216. MODULE_PARM_DESC(relock_by_timer, "Allow relock attempt if link not up");
  1217. static void qib_run_relock(unsigned long opaque)
  1218. {
  1219. struct qib_devdata *dd = (struct qib_devdata *)opaque;
  1220. struct qib_pportdata *ppd = dd->pport;
  1221. struct qib_chip_specific *cs = dd->cspec;
  1222. int timeoff;
  1223. /*
  1224. * Check link-training state for "stuck" state, when down.
  1225. * if found, try relock and schedule another try at
  1226. * exponentially growing delay, maxed at one second.
  1227. * if not stuck, our work is done.
  1228. */
  1229. if ((dd->flags & QIB_INITTED) && !(ppd->lflags &
  1230. (QIBL_IB_AUTONEG_INPROG | QIBL_LINKINIT | QIBL_LINKARMED |
  1231. QIBL_LINKACTIVE))) {
  1232. if (qib_relock_by_timer) {
  1233. if (!(ppd->lflags & QIBL_IB_LINK_DISABLED))
  1234. toggle_7220_rclkrls(dd);
  1235. }
  1236. /* re-set timer for next check */
  1237. timeoff = cs->relock_interval << 1;
  1238. if (timeoff > HZ)
  1239. timeoff = HZ;
  1240. cs->relock_interval = timeoff;
  1241. } else
  1242. timeoff = HZ;
  1243. mod_timer(&cs->relock_timer, jiffies + timeoff);
  1244. }
  1245. void set_7220_relock_poll(struct qib_devdata *dd, int ibup)
  1246. {
  1247. struct qib_chip_specific *cs = dd->cspec;
  1248. if (ibup) {
  1249. /* We are now up, relax timer to 1 second interval */
  1250. if (cs->relock_timer_active) {
  1251. cs->relock_interval = HZ;
  1252. mod_timer(&cs->relock_timer, jiffies + HZ);
  1253. }
  1254. } else {
  1255. /* Transition to down, (re-)set timer to short interval. */
  1256. unsigned int timeout;
  1257. timeout = msecs_to_jiffies(RELOCK_FIRST_MS);
  1258. if (timeout == 0)
  1259. timeout = 1;
  1260. /* If timer has not yet been started, do so. */
  1261. if (!cs->relock_timer_active) {
  1262. cs->relock_timer_active = 1;
  1263. init_timer(&cs->relock_timer);
  1264. cs->relock_timer.function = qib_run_relock;
  1265. cs->relock_timer.data = (unsigned long) dd;
  1266. cs->relock_interval = timeout;
  1267. cs->relock_timer.expires = jiffies + timeout;
  1268. add_timer(&cs->relock_timer);
  1269. } else {
  1270. cs->relock_interval = timeout;
  1271. mod_timer(&cs->relock_timer, jiffies + timeout);
  1272. }
  1273. }
  1274. }