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 "qlogic/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. goto done;
  374. /* Substitute our deduced value for was_reset */
  375. ret = qib_ibsd_ucode_loaded(dd->pport, fw);
  376. if (ret < 0)
  377. goto bail;
  378. first_reset = !ret; /* First reset if IBSD uCode not yet loaded */
  379. /*
  380. * Alter some regs per vendor latest doc, reset-defaults
  381. * are not right for IB.
  382. */
  383. ret = qib_sd_early(dd);
  384. if (ret < 0) {
  385. qib_dev_err(dd, "Failed to set IB SERDES early defaults\n");
  386. goto bail;
  387. }
  388. /*
  389. * Set DAC manual trim IB.
  390. * We only do this once after chip has been reset (usually
  391. * same as once per system boot).
  392. */
  393. if (first_reset) {
  394. ret = qib_sd_dactrim(dd);
  395. if (ret < 0) {
  396. qib_dev_err(dd, "Failed IB SERDES DAC trim\n");
  397. goto bail;
  398. }
  399. }
  400. /*
  401. * Set various registers (DDS and RXEQ) that will be
  402. * controlled by IBC (in 1.2 mode) to reasonable preset values
  403. * Calling the "internal" version avoids the "check for needed"
  404. * and "trimdone monitor" that might be counter-productive.
  405. */
  406. ret = qib_internal_presets(dd);
  407. if (ret < 0) {
  408. qib_dev_err(dd, "Failed to set IB SERDES presets\n");
  409. goto bail;
  410. }
  411. ret = qib_sd_trimself(dd, 0x80);
  412. if (ret < 0) {
  413. qib_dev_err(dd, "Failed to set IB SERDES TRIMSELF\n");
  414. goto bail;
  415. }
  416. /* Load image, then try to verify */
  417. ret = 0; /* Assume success */
  418. if (first_reset) {
  419. int vfy;
  420. int trim_done;
  421. ret = qib_sd7220_ib_load(dd, fw);
  422. if (ret < 0) {
  423. qib_dev_err(dd, "Failed to load IB SERDES image\n");
  424. goto bail;
  425. } else {
  426. /* Loaded image, try to verify */
  427. vfy = qib_sd7220_ib_vfy(dd, fw);
  428. if (vfy != ret) {
  429. qib_dev_err(dd, "SERDES PRAM VFY failed\n");
  430. goto bail;
  431. } /* end if verified */
  432. } /* end if loaded */
  433. /*
  434. * Loaded and verified. Almost good...
  435. * hold "success" in ret
  436. */
  437. ret = 0;
  438. /*
  439. * Prev steps all worked, continue bringup
  440. * De-assert RESET to uC, only in first reset, to allow
  441. * trimming.
  442. *
  443. * Since our default setup sets START_EQ1 to
  444. * PRESET, we need to clear that for this very first run.
  445. */
  446. ret = ibsd_mod_allchnls(dd, START_EQ1(0), 0, 0x38);
  447. if (ret < 0) {
  448. qib_dev_err(dd, "Failed clearing START_EQ1\n");
  449. goto bail;
  450. }
  451. qib_ibsd_reset(dd, 0);
  452. /*
  453. * If this is not the first reset, trimdone should be set
  454. * already. We may need to check about this.
  455. */
  456. trim_done = qib_sd_trimdone_poll(dd);
  457. /*
  458. * Whether or not trimdone succeeded, we need to put the
  459. * uC back into reset to avoid a possible fight with the
  460. * IBC state-machine.
  461. */
  462. qib_ibsd_reset(dd, 1);
  463. if (!trim_done) {
  464. qib_dev_err(dd, "No TRIMDONE seen\n");
  465. goto bail;
  466. }
  467. /*
  468. * DEBUG: check each time we reset if trimdone bits have
  469. * gotten cleared, and re-set them.
  470. */
  471. qib_sd_trimdone_monitor(dd, "First-reset");
  472. /* Remember so we do not re-do the load, dactrim, etc. */
  473. dd->cspec->serdes_first_init_done = 1;
  474. }
  475. /*
  476. * setup for channel training and load values for
  477. * RxEq and DDS in tables used by IBC in IB1.2 mode
  478. */
  479. ret = 0;
  480. if (qib_sd_setvals(dd) >= 0)
  481. goto done;
  482. bail:
  483. ret = 1;
  484. done:
  485. /* start relock timer regardless, but start at 1 second */
  486. set_7220_relock_poll(dd, -1);
  487. release_firmware(fw);
  488. return ret;
  489. }
  490. #define EPB_ACC_REQ 1
  491. #define EPB_ACC_GNT 0x100
  492. #define EPB_DATA_MASK 0xFF
  493. #define EPB_RD (1ULL << 24)
  494. #define EPB_TRANS_RDY (1ULL << 31)
  495. #define EPB_TRANS_ERR (1ULL << 30)
  496. #define EPB_TRANS_TRIES 5
  497. /*
  498. * query, claim, release ownership of the EPB (External Parallel Bus)
  499. * for a specified SERDES.
  500. * the "claim" parameter is >0 to claim, <0 to release, 0 to query.
  501. * Returns <0 for errors, >0 if we had ownership, else 0.
  502. */
  503. static int epb_access(struct qib_devdata *dd, int sdnum, int claim)
  504. {
  505. u16 acc;
  506. u64 accval;
  507. int owned = 0;
  508. u64 oct_sel = 0;
  509. switch (sdnum) {
  510. case IB_7220_SERDES:
  511. /*
  512. * The IB SERDES "ownership" is fairly simple. A single each
  513. * request/grant.
  514. */
  515. acc = kr_ibsd_epb_access_ctrl;
  516. break;
  517. case PCIE_SERDES0:
  518. case PCIE_SERDES1:
  519. /* PCIe SERDES has two "octants", need to select which */
  520. acc = kr_pciesd_epb_access_ctrl;
  521. oct_sel = (2 << (sdnum - PCIE_SERDES0));
  522. break;
  523. default:
  524. return 0;
  525. }
  526. /* Make sure any outstanding transaction was seen */
  527. qib_read_kreg32(dd, kr_scratch);
  528. udelay(15);
  529. accval = qib_read_kreg32(dd, acc);
  530. owned = !!(accval & EPB_ACC_GNT);
  531. if (claim < 0) {
  532. /* Need to release */
  533. u64 pollval;
  534. /*
  535. * The only writeable bits are the request and CS.
  536. * Both should be clear
  537. */
  538. u64 newval = 0;
  539. qib_write_kreg(dd, acc, newval);
  540. /* First read after write is not trustworthy */
  541. pollval = qib_read_kreg32(dd, acc);
  542. udelay(5);
  543. pollval = qib_read_kreg32(dd, acc);
  544. if (pollval & EPB_ACC_GNT)
  545. owned = -1;
  546. } else if (claim > 0) {
  547. /* Need to claim */
  548. u64 pollval;
  549. u64 newval = EPB_ACC_REQ | oct_sel;
  550. qib_write_kreg(dd, acc, newval);
  551. /* First read after write is not trustworthy */
  552. pollval = qib_read_kreg32(dd, acc);
  553. udelay(5);
  554. pollval = qib_read_kreg32(dd, acc);
  555. if (!(pollval & EPB_ACC_GNT))
  556. owned = -1;
  557. }
  558. return owned;
  559. }
  560. /*
  561. * Lemma to deal with race condition of write..read to epb regs
  562. */
  563. static int epb_trans(struct qib_devdata *dd, u16 reg, u64 i_val, u64 *o_vp)
  564. {
  565. int tries;
  566. u64 transval;
  567. qib_write_kreg(dd, reg, i_val);
  568. /* Throw away first read, as RDY bit may be stale */
  569. transval = qib_read_kreg64(dd, reg);
  570. for (tries = EPB_TRANS_TRIES; tries; --tries) {
  571. transval = qib_read_kreg32(dd, reg);
  572. if (transval & EPB_TRANS_RDY)
  573. break;
  574. udelay(5);
  575. }
  576. if (transval & EPB_TRANS_ERR)
  577. return -1;
  578. if (tries > 0 && o_vp)
  579. *o_vp = transval;
  580. return tries;
  581. }
  582. /**
  583. * qib_sd7220_reg_mod - modify SERDES register
  584. * @dd: the qlogic_ib device
  585. * @sdnum: which SERDES to access
  586. * @loc: location - channel, element, register, as packed by EPB_LOC() macro.
  587. * @wd: Write Data - value to set in register
  588. * @mask: ones where data should be spliced into reg.
  589. *
  590. * Basic register read/modify/write, with un-needed acesses elided. That is,
  591. * a mask of zero will prevent write, while a mask of 0xFF will prevent read.
  592. * returns current (presumed, if a write was done) contents of selected
  593. * register, or <0 if errors.
  594. */
  595. static int qib_sd7220_reg_mod(struct qib_devdata *dd, int sdnum, u32 loc,
  596. u32 wd, u32 mask)
  597. {
  598. u16 trans;
  599. u64 transval;
  600. int owned;
  601. int tries, ret;
  602. unsigned long flags;
  603. switch (sdnum) {
  604. case IB_7220_SERDES:
  605. trans = kr_ibsd_epb_transaction_reg;
  606. break;
  607. case PCIE_SERDES0:
  608. case PCIE_SERDES1:
  609. trans = kr_pciesd_epb_transaction_reg;
  610. break;
  611. default:
  612. return -1;
  613. }
  614. /*
  615. * All access is locked in software (vs other host threads) and
  616. * hardware (vs uC access).
  617. */
  618. spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
  619. owned = epb_access(dd, sdnum, 1);
  620. if (owned < 0) {
  621. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  622. return -1;
  623. }
  624. ret = 0;
  625. for (tries = EPB_TRANS_TRIES; tries; --tries) {
  626. transval = qib_read_kreg32(dd, trans);
  627. if (transval & EPB_TRANS_RDY)
  628. break;
  629. udelay(5);
  630. }
  631. if (tries > 0) {
  632. tries = 1; /* to make read-skip work */
  633. if (mask != 0xFF) {
  634. /*
  635. * Not a pure write, so need to read.
  636. * loc encodes chip-select as well as address
  637. */
  638. transval = loc | EPB_RD;
  639. tries = epb_trans(dd, trans, transval, &transval);
  640. }
  641. if (tries > 0 && mask != 0) {
  642. /*
  643. * Not a pure read, so need to write.
  644. */
  645. wd = (wd & mask) | (transval & ~mask);
  646. transval = loc | (wd & EPB_DATA_MASK);
  647. tries = epb_trans(dd, trans, transval, &transval);
  648. }
  649. }
  650. /* else, failed to see ready, what error-handling? */
  651. /*
  652. * Release bus. Failure is an error.
  653. */
  654. if (epb_access(dd, sdnum, -1) < 0)
  655. ret = -1;
  656. else
  657. ret = transval & EPB_DATA_MASK;
  658. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  659. if (tries <= 0)
  660. ret = -1;
  661. return ret;
  662. }
  663. #define EPB_ROM_R (2)
  664. #define EPB_ROM_W (1)
  665. /*
  666. * Below, all uC-related, use appropriate UC_CS, depending
  667. * on which SerDes is used.
  668. */
  669. #define EPB_UC_CTL EPB_LOC(6, 0, 0)
  670. #define EPB_MADDRL EPB_LOC(6, 0, 2)
  671. #define EPB_MADDRH EPB_LOC(6, 0, 3)
  672. #define EPB_ROMDATA EPB_LOC(6, 0, 4)
  673. #define EPB_RAMDATA EPB_LOC(6, 0, 5)
  674. /* Transfer date to/from uC Program RAM of IB or PCIe SerDes */
  675. static int qib_sd7220_ram_xfer(struct qib_devdata *dd, int sdnum, u32 loc,
  676. u8 *buf, int cnt, int rd_notwr)
  677. {
  678. u16 trans;
  679. u64 transval;
  680. u64 csbit;
  681. int owned;
  682. int tries;
  683. int sofar;
  684. int addr;
  685. int ret;
  686. unsigned long flags;
  687. /* Pick appropriate transaction reg and "Chip select" for this serdes */
  688. switch (sdnum) {
  689. case IB_7220_SERDES:
  690. csbit = 1ULL << EPB_IB_UC_CS_SHF;
  691. trans = kr_ibsd_epb_transaction_reg;
  692. break;
  693. case PCIE_SERDES0:
  694. case PCIE_SERDES1:
  695. /* PCIe SERDES has uC "chip select" in different bit, too */
  696. csbit = 1ULL << EPB_PCIE_UC_CS_SHF;
  697. trans = kr_pciesd_epb_transaction_reg;
  698. break;
  699. default:
  700. return -1;
  701. }
  702. spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
  703. owned = epb_access(dd, sdnum, 1);
  704. if (owned < 0) {
  705. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  706. return -1;
  707. }
  708. /*
  709. * In future code, we may need to distinguish several address ranges,
  710. * and select various memories based on this. For now, just trim
  711. * "loc" (location including address and memory select) to
  712. * "addr" (address within memory). we will only support PRAM
  713. * The memory is 8KB.
  714. */
  715. addr = loc & 0x1FFF;
  716. for (tries = EPB_TRANS_TRIES; tries; --tries) {
  717. transval = qib_read_kreg32(dd, trans);
  718. if (transval & EPB_TRANS_RDY)
  719. break;
  720. udelay(5);
  721. }
  722. sofar = 0;
  723. if (tries > 0) {
  724. /*
  725. * Every "memory" access is doubly-indirect.
  726. * We set two bytes of address, then read/write
  727. * one or mores bytes of data.
  728. */
  729. /* First, we set control to "Read" or "Write" */
  730. transval = csbit | EPB_UC_CTL |
  731. (rd_notwr ? EPB_ROM_R : EPB_ROM_W);
  732. tries = epb_trans(dd, trans, transval, &transval);
  733. while (tries > 0 && sofar < cnt) {
  734. if (!sofar) {
  735. /* Only set address at start of chunk */
  736. int addrbyte = (addr + sofar) >> 8;
  737. transval = csbit | EPB_MADDRH | addrbyte;
  738. tries = epb_trans(dd, trans, transval,
  739. &transval);
  740. if (tries <= 0)
  741. break;
  742. addrbyte = (addr + sofar) & 0xFF;
  743. transval = csbit | EPB_MADDRL | addrbyte;
  744. tries = epb_trans(dd, trans, transval,
  745. &transval);
  746. if (tries <= 0)
  747. break;
  748. }
  749. if (rd_notwr)
  750. transval = csbit | EPB_ROMDATA | EPB_RD;
  751. else
  752. transval = csbit | EPB_ROMDATA | buf[sofar];
  753. tries = epb_trans(dd, trans, transval, &transval);
  754. if (tries <= 0)
  755. break;
  756. if (rd_notwr)
  757. buf[sofar] = transval & EPB_DATA_MASK;
  758. ++sofar;
  759. }
  760. /* Finally, clear control-bit for Read or Write */
  761. transval = csbit | EPB_UC_CTL;
  762. tries = epb_trans(dd, trans, transval, &transval);
  763. }
  764. ret = sofar;
  765. /* Release bus. Failure is an error */
  766. if (epb_access(dd, sdnum, -1) < 0)
  767. ret = -1;
  768. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  769. if (tries <= 0)
  770. ret = -1;
  771. return ret;
  772. }
  773. #define PROG_CHUNK 64
  774. static int qib_sd7220_prog_ld(struct qib_devdata *dd, int sdnum,
  775. const u8 *img, int len, int offset)
  776. {
  777. int cnt, sofar, req;
  778. sofar = 0;
  779. while (sofar < len) {
  780. req = len - sofar;
  781. if (req > PROG_CHUNK)
  782. req = PROG_CHUNK;
  783. cnt = qib_sd7220_ram_xfer(dd, sdnum, offset + sofar,
  784. (u8 *)img + sofar, req, 0);
  785. if (cnt < req) {
  786. sofar = -1;
  787. break;
  788. }
  789. sofar += req;
  790. }
  791. return sofar;
  792. }
  793. #define VFY_CHUNK 64
  794. #define SD_PRAM_ERROR_LIMIT 42
  795. static int qib_sd7220_prog_vfy(struct qib_devdata *dd, int sdnum,
  796. const u8 *img, int len, int offset)
  797. {
  798. int cnt, sofar, req, idx, errors;
  799. unsigned char readback[VFY_CHUNK];
  800. errors = 0;
  801. sofar = 0;
  802. while (sofar < len) {
  803. req = len - sofar;
  804. if (req > VFY_CHUNK)
  805. req = VFY_CHUNK;
  806. cnt = qib_sd7220_ram_xfer(dd, sdnum, sofar + offset,
  807. readback, req, 1);
  808. if (cnt < req) {
  809. /* failed in read itself */
  810. sofar = -1;
  811. break;
  812. }
  813. for (idx = 0; idx < cnt; ++idx) {
  814. if (readback[idx] != img[idx+sofar])
  815. ++errors;
  816. }
  817. sofar += cnt;
  818. }
  819. return errors ? -errors : sofar;
  820. }
  821. static int
  822. qib_sd7220_ib_load(struct qib_devdata *dd, const struct firmware *fw)
  823. {
  824. return qib_sd7220_prog_ld(dd, IB_7220_SERDES, fw->data, fw->size, 0);
  825. }
  826. static int
  827. qib_sd7220_ib_vfy(struct qib_devdata *dd, const struct firmware *fw)
  828. {
  829. return qib_sd7220_prog_vfy(dd, IB_7220_SERDES, fw->data, fw->size, 0);
  830. }
  831. /*
  832. * IRQ not set up at this point in init, so we poll.
  833. */
  834. #define IB_SERDES_TRIM_DONE (1ULL << 11)
  835. #define TRIM_TMO (15)
  836. static int qib_sd_trimdone_poll(struct qib_devdata *dd)
  837. {
  838. int trim_tmo, ret;
  839. uint64_t val;
  840. /*
  841. * Default to failure, so IBC will not start
  842. * without IB_SERDES_TRIM_DONE.
  843. */
  844. ret = 0;
  845. for (trim_tmo = 0; trim_tmo < TRIM_TMO; ++trim_tmo) {
  846. val = qib_read_kreg64(dd, kr_ibcstatus);
  847. if (val & IB_SERDES_TRIM_DONE) {
  848. ret = 1;
  849. break;
  850. }
  851. msleep(20);
  852. }
  853. if (trim_tmo >= TRIM_TMO) {
  854. qib_dev_err(dd, "No TRIMDONE in %d tries\n", trim_tmo);
  855. ret = 0;
  856. }
  857. return ret;
  858. }
  859. #define TX_FAST_ELT (9)
  860. /*
  861. * Set the "negotiation" values for SERDES. These are used by the IB1.2
  862. * link negotiation. Macros below are attempt to keep the values a
  863. * little more human-editable.
  864. * First, values related to Drive De-emphasis Settings.
  865. */
  866. #define NUM_DDS_REGS 6
  867. #define DDS_REG_MAP 0x76A910 /* LSB-first list of regs (in elt 9) to mod */
  868. #define DDS_VAL(amp_d, main_d, ipst_d, ipre_d, amp_s, main_s, ipst_s, ipre_s) \
  869. { { ((amp_d & 0x1F) << 1) | 1, ((amp_s & 0x1F) << 1) | 1, \
  870. (main_d << 3) | 4 | (ipre_d >> 2), \
  871. (main_s << 3) | 4 | (ipre_s >> 2), \
  872. ((ipst_d & 0xF) << 1) | ((ipre_d & 3) << 6) | 0x21, \
  873. ((ipst_s & 0xF) << 1) | ((ipre_s & 3) << 6) | 0x21 } }
  874. static struct dds_init {
  875. uint8_t reg_vals[NUM_DDS_REGS];
  876. } dds_init_vals[] = {
  877. /* DDR(FDR) SDR(HDR) */
  878. /* Vendor recommends below for 3m cable */
  879. #define DDS_3M 0
  880. DDS_VAL(31, 19, 12, 0, 29, 22, 9, 0),
  881. DDS_VAL(31, 12, 15, 4, 31, 15, 15, 1),
  882. DDS_VAL(31, 13, 15, 3, 31, 16, 15, 0),
  883. DDS_VAL(31, 14, 15, 2, 31, 17, 14, 0),
  884. DDS_VAL(31, 15, 15, 1, 31, 18, 13, 0),
  885. DDS_VAL(31, 16, 15, 0, 31, 19, 12, 0),
  886. DDS_VAL(31, 17, 14, 0, 31, 20, 11, 0),
  887. DDS_VAL(31, 18, 13, 0, 30, 21, 10, 0),
  888. DDS_VAL(31, 20, 11, 0, 28, 23, 8, 0),
  889. DDS_VAL(31, 21, 10, 0, 27, 24, 7, 0),
  890. DDS_VAL(31, 22, 9, 0, 26, 25, 6, 0),
  891. DDS_VAL(30, 23, 8, 0, 25, 26, 5, 0),
  892. DDS_VAL(29, 24, 7, 0, 23, 27, 4, 0),
  893. /* Vendor recommends below for 1m cable */
  894. #define DDS_1M 13
  895. DDS_VAL(28, 25, 6, 0, 21, 28, 3, 0),
  896. DDS_VAL(27, 26, 5, 0, 19, 29, 2, 0),
  897. DDS_VAL(25, 27, 4, 0, 17, 30, 1, 0)
  898. };
  899. /*
  900. * Now the RXEQ section of the table.
  901. */
  902. /* Hardware packs an element number and register address thus: */
  903. #define RXEQ_INIT_RDESC(elt, addr) (((elt) & 0xF) | ((addr) << 4))
  904. #define RXEQ_VAL(elt, adr, val0, val1, val2, val3) \
  905. {RXEQ_INIT_RDESC((elt), (adr)), {(val0), (val1), (val2), (val3)} }
  906. #define RXEQ_VAL_ALL(elt, adr, val) \
  907. {RXEQ_INIT_RDESC((elt), (adr)), {(val), (val), (val), (val)} }
  908. #define RXEQ_SDR_DFELTH 0
  909. #define RXEQ_SDR_TLTH 0
  910. #define RXEQ_SDR_G1CNT_Z1CNT 0x11
  911. #define RXEQ_SDR_ZCNT 23
  912. static struct rxeq_init {
  913. u16 rdesc; /* in form used in SerDesDDSRXEQ */
  914. u8 rdata[4];
  915. } rxeq_init_vals[] = {
  916. /* Set Rcv Eq. to Preset node */
  917. RXEQ_VAL_ALL(7, 0x27, 0x10),
  918. /* Set DFELTHFDR/HDR thresholds */
  919. RXEQ_VAL(7, 8, 0, 0, 0, 0), /* FDR, was 0, 1, 2, 3 */
  920. RXEQ_VAL(7, 0x21, 0, 0, 0, 0), /* HDR */
  921. /* Set TLTHFDR/HDR theshold */
  922. RXEQ_VAL(7, 9, 2, 2, 2, 2), /* FDR, was 0, 2, 4, 6 */
  923. RXEQ_VAL(7, 0x23, 2, 2, 2, 2), /* HDR, was 0, 1, 2, 3 */
  924. /* Set Preamp setting 2 (ZFR/ZCNT) */
  925. RXEQ_VAL(7, 0x1B, 12, 12, 12, 12), /* FDR, was 12, 16, 20, 24 */
  926. RXEQ_VAL(7, 0x1C, 12, 12, 12, 12), /* HDR, was 12, 16, 20, 24 */
  927. /* Set Preamp DC gain and Setting 1 (GFR/GHR) */
  928. RXEQ_VAL(7, 0x1E, 16, 16, 16, 16), /* FDR, was 16, 17, 18, 20 */
  929. RXEQ_VAL(7, 0x1F, 16, 16, 16, 16), /* HDR, was 16, 17, 18, 20 */
  930. /* Toggle RELOCK (in VCDL_CTRL0) to lock to data */
  931. RXEQ_VAL_ALL(6, 6, 0x20), /* Set D5 High */
  932. RXEQ_VAL_ALL(6, 6, 0), /* Set D5 Low */
  933. };
  934. /* There are 17 values from vendor, but IBC only accesses the first 16 */
  935. #define DDS_ROWS (16)
  936. #define RXEQ_ROWS ARRAY_SIZE(rxeq_init_vals)
  937. static int qib_sd_setvals(struct qib_devdata *dd)
  938. {
  939. int idx, midx;
  940. int min_idx; /* Minimum index for this portion of table */
  941. uint32_t dds_reg_map;
  942. u64 __iomem *taddr, *iaddr;
  943. uint64_t data;
  944. uint64_t sdctl;
  945. taddr = dd->kregbase + kr_serdes_maptable;
  946. iaddr = dd->kregbase + kr_serdes_ddsrxeq0;
  947. /*
  948. * Init the DDS section of the table.
  949. * Each "row" of the table provokes NUM_DDS_REG writes, to the
  950. * registers indicated in DDS_REG_MAP.
  951. */
  952. sdctl = qib_read_kreg64(dd, kr_ibserdesctrl);
  953. sdctl = (sdctl & ~(0x1f << 8)) | (NUM_DDS_REGS << 8);
  954. sdctl = (sdctl & ~(0x1f << 13)) | (RXEQ_ROWS << 13);
  955. qib_write_kreg(dd, kr_ibserdesctrl, sdctl);
  956. /*
  957. * Iterate down table within loop for each register to store.
  958. */
  959. dds_reg_map = DDS_REG_MAP;
  960. for (idx = 0; idx < NUM_DDS_REGS; ++idx) {
  961. data = ((dds_reg_map & 0xF) << 4) | TX_FAST_ELT;
  962. writeq(data, iaddr + idx);
  963. mmiowb();
  964. qib_read_kreg32(dd, kr_scratch);
  965. dds_reg_map >>= 4;
  966. for (midx = 0; midx < DDS_ROWS; ++midx) {
  967. u64 __iomem *daddr = taddr + ((midx << 4) + idx);
  968. data = dds_init_vals[midx].reg_vals[idx];
  969. writeq(data, daddr);
  970. mmiowb();
  971. qib_read_kreg32(dd, kr_scratch);
  972. } /* End inner for (vals for this reg, each row) */
  973. } /* end outer for (regs to be stored) */
  974. /*
  975. * Init the RXEQ section of the table.
  976. * This runs in a different order, as the pattern of
  977. * register references is more complex, but there are only
  978. * four "data" values per register.
  979. */
  980. min_idx = idx; /* RXEQ indices pick up where DDS left off */
  981. taddr += 0x100; /* RXEQ data is in second half of table */
  982. /* Iterate through RXEQ register addresses */
  983. for (idx = 0; idx < RXEQ_ROWS; ++idx) {
  984. int didx; /* "destination" */
  985. int vidx;
  986. /* didx is offset by min_idx to address RXEQ range of regs */
  987. didx = idx + min_idx;
  988. /* Store the next RXEQ register address */
  989. writeq(rxeq_init_vals[idx].rdesc, iaddr + didx);
  990. mmiowb();
  991. qib_read_kreg32(dd, kr_scratch);
  992. /* Iterate through RXEQ values */
  993. for (vidx = 0; vidx < 4; vidx++) {
  994. data = rxeq_init_vals[idx].rdata[vidx];
  995. writeq(data, taddr + (vidx << 6) + idx);
  996. mmiowb();
  997. qib_read_kreg32(dd, kr_scratch);
  998. }
  999. } /* end outer for (Reg-writes for RXEQ) */
  1000. return 0;
  1001. }
  1002. #define CMUCTRL5 EPB_LOC(7, 0, 0x15)
  1003. #define RXHSCTRL0(chan) EPB_LOC(chan, 6, 0)
  1004. #define VCDL_DAC2(chan) EPB_LOC(chan, 6, 5)
  1005. #define VCDL_CTRL0(chan) EPB_LOC(chan, 6, 6)
  1006. #define VCDL_CTRL2(chan) EPB_LOC(chan, 6, 8)
  1007. #define START_EQ2(chan) EPB_LOC(chan, 7, 0x28)
  1008. /*
  1009. * Repeat a "store" across all channels of the IB SerDes.
  1010. * Although nominally it inherits the "read value" of the last
  1011. * channel it modified, the only really useful return is <0 for
  1012. * failure, >= 0 for success. The parameter 'loc' is assumed to
  1013. * be the location in some channel of the register to be modified
  1014. * The caller can specify use of the "gang write" option of EPB,
  1015. * in which case we use the specified channel data for any fields
  1016. * not explicitely written.
  1017. */
  1018. static int ibsd_mod_allchnls(struct qib_devdata *dd, int loc, int val,
  1019. int mask)
  1020. {
  1021. int ret = -1;
  1022. int chnl;
  1023. if (loc & EPB_GLOBAL_WR) {
  1024. /*
  1025. * Our caller has assured us that we can set all four
  1026. * channels at once. Trust that. If mask is not 0xFF,
  1027. * we will read the _specified_ channel for our starting
  1028. * value.
  1029. */
  1030. loc |= (1U << EPB_IB_QUAD0_CS_SHF);
  1031. chnl = (loc >> (4 + EPB_ADDR_SHF)) & 7;
  1032. if (mask != 0xFF) {
  1033. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
  1034. loc & ~EPB_GLOBAL_WR, 0, 0);
  1035. if (ret < 0) {
  1036. int sloc = loc >> EPB_ADDR_SHF;
  1037. qib_dev_err(dd,
  1038. "pre-read failed: elt %d, addr 0x%X, chnl %d\n",
  1039. (sloc & 0xF),
  1040. (sloc >> 9) & 0x3f, chnl);
  1041. return ret;
  1042. }
  1043. val = (ret & ~mask) | (val & mask);
  1044. }
  1045. loc &= ~(7 << (4+EPB_ADDR_SHF));
  1046. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);
  1047. if (ret < 0) {
  1048. int sloc = loc >> EPB_ADDR_SHF;
  1049. qib_dev_err(dd,
  1050. "Global WR failed: elt %d, addr 0x%X, val %02X\n",
  1051. (sloc & 0xF), (sloc >> 9) & 0x3f, val);
  1052. }
  1053. return ret;
  1054. }
  1055. /* Clear "channel" and set CS so we can simply iterate */
  1056. loc &= ~(7 << (4+EPB_ADDR_SHF));
  1057. loc |= (1U << EPB_IB_QUAD0_CS_SHF);
  1058. for (chnl = 0; chnl < 4; ++chnl) {
  1059. int cloc = loc | (chnl << (4+EPB_ADDR_SHF));
  1060. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, cloc, val, mask);
  1061. if (ret < 0) {
  1062. int sloc = loc >> EPB_ADDR_SHF;
  1063. qib_dev_err(dd,
  1064. "Write failed: elt %d, addr 0x%X, chnl %d, val 0x%02X, mask 0x%02X\n",
  1065. (sloc & 0xF), (sloc >> 9) & 0x3f, chnl,
  1066. val & 0xFF, mask & 0xFF);
  1067. break;
  1068. }
  1069. }
  1070. return ret;
  1071. }
  1072. /*
  1073. * Set the Tx values normally modified by IBC in IB1.2 mode to default
  1074. * values, as gotten from first row of init table.
  1075. */
  1076. static int set_dds_vals(struct qib_devdata *dd, struct dds_init *ddi)
  1077. {
  1078. int ret;
  1079. int idx, reg, data;
  1080. uint32_t regmap;
  1081. regmap = DDS_REG_MAP;
  1082. for (idx = 0; idx < NUM_DDS_REGS; ++idx) {
  1083. reg = (regmap & 0xF);
  1084. regmap >>= 4;
  1085. data = ddi->reg_vals[idx];
  1086. /* Vendor says RMW not needed for these regs, use 0xFF mask */
  1087. ret = ibsd_mod_allchnls(dd, EPB_LOC(0, 9, reg), data, 0xFF);
  1088. if (ret < 0)
  1089. break;
  1090. }
  1091. return ret;
  1092. }
  1093. /*
  1094. * Set the Rx values normally modified by IBC in IB1.2 mode to default
  1095. * values, as gotten from selected column of init table.
  1096. */
  1097. static int set_rxeq_vals(struct qib_devdata *dd, int vsel)
  1098. {
  1099. int ret;
  1100. int ridx;
  1101. int cnt = ARRAY_SIZE(rxeq_init_vals);
  1102. for (ridx = 0; ridx < cnt; ++ridx) {
  1103. int elt, reg, val, loc;
  1104. elt = rxeq_init_vals[ridx].rdesc & 0xF;
  1105. reg = rxeq_init_vals[ridx].rdesc >> 4;
  1106. loc = EPB_LOC(0, elt, reg);
  1107. val = rxeq_init_vals[ridx].rdata[vsel];
  1108. /* mask of 0xFF, because hardware does full-byte store. */
  1109. ret = ibsd_mod_allchnls(dd, loc, val, 0xFF);
  1110. if (ret < 0)
  1111. break;
  1112. }
  1113. return ret;
  1114. }
  1115. /*
  1116. * Set the default values (row 0) for DDR Driver Demphasis.
  1117. * we do this initially and whenever we turn off IB-1.2
  1118. *
  1119. * The "default" values for Rx equalization are also stored to
  1120. * SerDes registers. Formerly (and still default), we used set 2.
  1121. * For experimenting with cables and link-partners, we allow changing
  1122. * that via a module parameter.
  1123. */
  1124. static unsigned qib_rxeq_set = 2;
  1125. module_param_named(rxeq_default_set, qib_rxeq_set, uint,
  1126. S_IWUSR | S_IRUGO);
  1127. MODULE_PARM_DESC(rxeq_default_set,
  1128. "Which set [0..3] of Rx Equalization values is default");
  1129. static int qib_internal_presets(struct qib_devdata *dd)
  1130. {
  1131. int ret = 0;
  1132. ret = set_dds_vals(dd, dds_init_vals + DDS_3M);
  1133. if (ret < 0)
  1134. qib_dev_err(dd, "Failed to set default DDS values\n");
  1135. ret = set_rxeq_vals(dd, qib_rxeq_set & 3);
  1136. if (ret < 0)
  1137. qib_dev_err(dd, "Failed to set default RXEQ values\n");
  1138. return ret;
  1139. }
  1140. int qib_sd7220_presets(struct qib_devdata *dd)
  1141. {
  1142. int ret = 0;
  1143. if (!dd->cspec->presets_needed)
  1144. return ret;
  1145. dd->cspec->presets_needed = 0;
  1146. /* Assert uC reset, so we don't clash with it. */
  1147. qib_ibsd_reset(dd, 1);
  1148. udelay(2);
  1149. qib_sd_trimdone_monitor(dd, "link-down");
  1150. ret = qib_internal_presets(dd);
  1151. return ret;
  1152. }
  1153. static int qib_sd_trimself(struct qib_devdata *dd, int val)
  1154. {
  1155. int loc = CMUCTRL5 | (1U << EPB_IB_QUAD0_CS_SHF);
  1156. return qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);
  1157. }
  1158. static int qib_sd_early(struct qib_devdata *dd)
  1159. {
  1160. int ret;
  1161. ret = ibsd_mod_allchnls(dd, RXHSCTRL0(0) | EPB_GLOBAL_WR, 0xD4, 0xFF);
  1162. if (ret < 0)
  1163. goto bail;
  1164. ret = ibsd_mod_allchnls(dd, START_EQ1(0) | EPB_GLOBAL_WR, 0x10, 0xFF);
  1165. if (ret < 0)
  1166. goto bail;
  1167. ret = ibsd_mod_allchnls(dd, START_EQ2(0) | EPB_GLOBAL_WR, 0x30, 0xFF);
  1168. bail:
  1169. return ret;
  1170. }
  1171. #define BACTRL(chnl) EPB_LOC(chnl, 6, 0x0E)
  1172. #define LDOUTCTRL1(chnl) EPB_LOC(chnl, 7, 6)
  1173. #define RXHSSTATUS(chnl) EPB_LOC(chnl, 6, 0xF)
  1174. static int qib_sd_dactrim(struct qib_devdata *dd)
  1175. {
  1176. int ret;
  1177. ret = ibsd_mod_allchnls(dd, VCDL_DAC2(0) | EPB_GLOBAL_WR, 0x2D, 0xFF);
  1178. if (ret < 0)
  1179. goto bail;
  1180. /* more fine-tuning of what will be default */
  1181. ret = ibsd_mod_allchnls(dd, VCDL_CTRL2(0), 3, 0xF);
  1182. if (ret < 0)
  1183. goto bail;
  1184. ret = ibsd_mod_allchnls(dd, BACTRL(0) | EPB_GLOBAL_WR, 0x40, 0xFF);
  1185. if (ret < 0)
  1186. goto bail;
  1187. ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x04, 0xFF);
  1188. if (ret < 0)
  1189. goto bail;
  1190. ret = ibsd_mod_allchnls(dd, RXHSSTATUS(0) | EPB_GLOBAL_WR, 0x04, 0xFF);
  1191. if (ret < 0)
  1192. goto bail;
  1193. /*
  1194. * Delay for max possible number of steps, with slop.
  1195. * Each step is about 4usec.
  1196. */
  1197. udelay(415);
  1198. ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x00, 0xFF);
  1199. bail:
  1200. return ret;
  1201. }
  1202. #define RELOCK_FIRST_MS 3
  1203. #define RXLSPPM(chan) EPB_LOC(chan, 0, 2)
  1204. void toggle_7220_rclkrls(struct qib_devdata *dd)
  1205. {
  1206. int loc = RXLSPPM(0) | EPB_GLOBAL_WR;
  1207. int ret;
  1208. ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);
  1209. if (ret < 0)
  1210. qib_dev_err(dd, "RCLKRLS failed to clear D7\n");
  1211. else {
  1212. udelay(1);
  1213. ibsd_mod_allchnls(dd, loc, 0x80, 0x80);
  1214. }
  1215. /* And again for good measure */
  1216. udelay(1);
  1217. ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);
  1218. if (ret < 0)
  1219. qib_dev_err(dd, "RCLKRLS failed to clear D7\n");
  1220. else {
  1221. udelay(1);
  1222. ibsd_mod_allchnls(dd, loc, 0x80, 0x80);
  1223. }
  1224. /* Now reset xgxs and IBC to complete the recovery */
  1225. dd->f_xgxs_reset(dd->pport);
  1226. }
  1227. /*
  1228. * Shut down the timer that polls for relock occasions, if needed
  1229. * this is "hooked" from qib_7220_quiet_serdes(), which is called
  1230. * just before qib_shutdown_device() in qib_driver.c shuts down all
  1231. * the other timers
  1232. */
  1233. void shutdown_7220_relock_poll(struct qib_devdata *dd)
  1234. {
  1235. if (dd->cspec->relock_timer_active)
  1236. del_timer_sync(&dd->cspec->relock_timer);
  1237. }
  1238. static unsigned qib_relock_by_timer = 1;
  1239. module_param_named(relock_by_timer, qib_relock_by_timer, uint,
  1240. S_IWUSR | S_IRUGO);
  1241. MODULE_PARM_DESC(relock_by_timer, "Allow relock attempt if link not up");
  1242. static void qib_run_relock(struct timer_list *t)
  1243. {
  1244. struct qib_chip_specific *cs = from_timer(cs, t, relock_timer);
  1245. struct qib_devdata *dd = cs->dd;
  1246. struct qib_pportdata *ppd = dd->pport;
  1247. int timeoff;
  1248. /*
  1249. * Check link-training state for "stuck" state, when down.
  1250. * if found, try relock and schedule another try at
  1251. * exponentially growing delay, maxed at one second.
  1252. * if not stuck, our work is done.
  1253. */
  1254. if ((dd->flags & QIB_INITTED) && !(ppd->lflags &
  1255. (QIBL_IB_AUTONEG_INPROG | QIBL_LINKINIT | QIBL_LINKARMED |
  1256. QIBL_LINKACTIVE))) {
  1257. if (qib_relock_by_timer) {
  1258. if (!(ppd->lflags & QIBL_IB_LINK_DISABLED))
  1259. toggle_7220_rclkrls(dd);
  1260. }
  1261. /* re-set timer for next check */
  1262. timeoff = cs->relock_interval << 1;
  1263. if (timeoff > HZ)
  1264. timeoff = HZ;
  1265. cs->relock_interval = timeoff;
  1266. } else
  1267. timeoff = HZ;
  1268. mod_timer(&cs->relock_timer, jiffies + timeoff);
  1269. }
  1270. void set_7220_relock_poll(struct qib_devdata *dd, int ibup)
  1271. {
  1272. struct qib_chip_specific *cs = dd->cspec;
  1273. if (ibup) {
  1274. /* We are now up, relax timer to 1 second interval */
  1275. if (cs->relock_timer_active) {
  1276. cs->relock_interval = HZ;
  1277. mod_timer(&cs->relock_timer, jiffies + HZ);
  1278. }
  1279. } else {
  1280. /* Transition to down, (re-)set timer to short interval. */
  1281. unsigned int timeout;
  1282. timeout = msecs_to_jiffies(RELOCK_FIRST_MS);
  1283. if (timeout == 0)
  1284. timeout = 1;
  1285. /* If timer has not yet been started, do so. */
  1286. if (!cs->relock_timer_active) {
  1287. cs->relock_timer_active = 1;
  1288. timer_setup(&cs->relock_timer, qib_run_relock, 0);
  1289. cs->relock_interval = timeout;
  1290. cs->relock_timer.expires = jiffies + timeout;
  1291. add_timer(&cs->relock_timer);
  1292. } else {
  1293. cs->relock_interval = timeout;
  1294. mod_timer(&cs->relock_timer, jiffies + timeout);
  1295. }
  1296. }
  1297. }