qib_sd7220.c 40 KB

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