qib_sd7220.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450
  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. 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. /* Pick appropriate transaction reg and "Chip select" for this serdes */
  690. switch (sdnum) {
  691. case IB_7220_SERDES:
  692. csbit = 1ULL << EPB_IB_UC_CS_SHF;
  693. trans = kr_ibsd_epb_transaction_reg;
  694. break;
  695. case PCIE_SERDES0:
  696. case PCIE_SERDES1:
  697. /* PCIe SERDES has uC "chip select" in different bit, too */
  698. csbit = 1ULL << EPB_PCIE_UC_CS_SHF;
  699. trans = kr_pciesd_epb_transaction_reg;
  700. break;
  701. default:
  702. return -1;
  703. }
  704. spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
  705. owned = epb_access(dd, sdnum, 1);
  706. if (owned < 0) {
  707. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  708. return -1;
  709. }
  710. /*
  711. * In future code, we may need to distinguish several address ranges,
  712. * and select various memories based on this. For now, just trim
  713. * "loc" (location including address and memory select) to
  714. * "addr" (address within memory). we will only support PRAM
  715. * The memory is 8KB.
  716. */
  717. addr = loc & 0x1FFF;
  718. for (tries = EPB_TRANS_TRIES; tries; --tries) {
  719. transval = qib_read_kreg32(dd, trans);
  720. if (transval & EPB_TRANS_RDY)
  721. break;
  722. udelay(5);
  723. }
  724. sofar = 0;
  725. if (tries > 0) {
  726. /*
  727. * Every "memory" access is doubly-indirect.
  728. * We set two bytes of address, then read/write
  729. * one or mores bytes of data.
  730. */
  731. /* First, we set control to "Read" or "Write" */
  732. transval = csbit | EPB_UC_CTL |
  733. (rd_notwr ? EPB_ROM_R : EPB_ROM_W);
  734. tries = epb_trans(dd, trans, transval, &transval);
  735. while (tries > 0 && sofar < cnt) {
  736. if (!sofar) {
  737. /* Only set address at start of chunk */
  738. int addrbyte = (addr + sofar) >> 8;
  739. transval = csbit | EPB_MADDRH | addrbyte;
  740. tries = epb_trans(dd, trans, transval,
  741. &transval);
  742. if (tries <= 0)
  743. break;
  744. addrbyte = (addr + sofar) & 0xFF;
  745. transval = csbit | EPB_MADDRL | addrbyte;
  746. tries = epb_trans(dd, trans, transval,
  747. &transval);
  748. if (tries <= 0)
  749. break;
  750. }
  751. if (rd_notwr)
  752. transval = csbit | EPB_ROMDATA | EPB_RD;
  753. else
  754. transval = csbit | EPB_ROMDATA | buf[sofar];
  755. tries = epb_trans(dd, trans, transval, &transval);
  756. if (tries <= 0)
  757. break;
  758. if (rd_notwr)
  759. buf[sofar] = transval & EPB_DATA_MASK;
  760. ++sofar;
  761. }
  762. /* Finally, clear control-bit for Read or Write */
  763. transval = csbit | EPB_UC_CTL;
  764. tries = epb_trans(dd, trans, transval, &transval);
  765. }
  766. ret = sofar;
  767. /* Release bus. Failure is an error */
  768. if (epb_access(dd, sdnum, -1) < 0)
  769. ret = -1;
  770. spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
  771. if (tries <= 0)
  772. ret = -1;
  773. return ret;
  774. }
  775. #define PROG_CHUNK 64
  776. static int qib_sd7220_prog_ld(struct qib_devdata *dd, int sdnum,
  777. const u8 *img, int len, int offset)
  778. {
  779. int cnt, sofar, req;
  780. sofar = 0;
  781. while (sofar < len) {
  782. req = len - sofar;
  783. if (req > PROG_CHUNK)
  784. req = PROG_CHUNK;
  785. cnt = qib_sd7220_ram_xfer(dd, sdnum, offset + sofar,
  786. (u8 *)img + sofar, req, 0);
  787. if (cnt < req) {
  788. sofar = -1;
  789. break;
  790. }
  791. sofar += req;
  792. }
  793. return sofar;
  794. }
  795. #define VFY_CHUNK 64
  796. #define SD_PRAM_ERROR_LIMIT 42
  797. static int qib_sd7220_prog_vfy(struct qib_devdata *dd, int sdnum,
  798. const u8 *img, int len, int offset)
  799. {
  800. int cnt, sofar, req, idx, errors;
  801. unsigned char readback[VFY_CHUNK];
  802. errors = 0;
  803. sofar = 0;
  804. while (sofar < len) {
  805. req = len - sofar;
  806. if (req > VFY_CHUNK)
  807. req = VFY_CHUNK;
  808. cnt = qib_sd7220_ram_xfer(dd, sdnum, sofar + offset,
  809. readback, req, 1);
  810. if (cnt < req) {
  811. /* failed in read itself */
  812. sofar = -1;
  813. break;
  814. }
  815. for (idx = 0; idx < cnt; ++idx) {
  816. if (readback[idx] != img[idx+sofar])
  817. ++errors;
  818. }
  819. sofar += cnt;
  820. }
  821. return errors ? -errors : sofar;
  822. }
  823. static int
  824. qib_sd7220_ib_load(struct qib_devdata *dd, const struct firmware *fw)
  825. {
  826. return qib_sd7220_prog_ld(dd, IB_7220_SERDES, fw->data, fw->size, 0);
  827. }
  828. static int
  829. qib_sd7220_ib_vfy(struct qib_devdata *dd, const struct firmware *fw)
  830. {
  831. return qib_sd7220_prog_vfy(dd, IB_7220_SERDES, fw->data, fw->size, 0);
  832. }
  833. /*
  834. * IRQ not set up at this point in init, so we poll.
  835. */
  836. #define IB_SERDES_TRIM_DONE (1ULL << 11)
  837. #define TRIM_TMO (15)
  838. static int qib_sd_trimdone_poll(struct qib_devdata *dd)
  839. {
  840. int trim_tmo, ret;
  841. uint64_t val;
  842. /*
  843. * Default to failure, so IBC will not start
  844. * without IB_SERDES_TRIM_DONE.
  845. */
  846. ret = 0;
  847. for (trim_tmo = 0; trim_tmo < TRIM_TMO; ++trim_tmo) {
  848. val = qib_read_kreg64(dd, kr_ibcstatus);
  849. if (val & IB_SERDES_TRIM_DONE) {
  850. ret = 1;
  851. break;
  852. }
  853. msleep(20);
  854. }
  855. if (trim_tmo >= TRIM_TMO) {
  856. qib_dev_err(dd, "No TRIMDONE in %d tries\n", trim_tmo);
  857. ret = 0;
  858. }
  859. return ret;
  860. }
  861. #define TX_FAST_ELT (9)
  862. /*
  863. * Set the "negotiation" values for SERDES. These are used by the IB1.2
  864. * link negotiation. Macros below are attempt to keep the values a
  865. * little more human-editable.
  866. * First, values related to Drive De-emphasis Settings.
  867. */
  868. #define NUM_DDS_REGS 6
  869. #define DDS_REG_MAP 0x76A910 /* LSB-first list of regs (in elt 9) to mod */
  870. #define DDS_VAL(amp_d, main_d, ipst_d, ipre_d, amp_s, main_s, ipst_s, ipre_s) \
  871. { { ((amp_d & 0x1F) << 1) | 1, ((amp_s & 0x1F) << 1) | 1, \
  872. (main_d << 3) | 4 | (ipre_d >> 2), \
  873. (main_s << 3) | 4 | (ipre_s >> 2), \
  874. ((ipst_d & 0xF) << 1) | ((ipre_d & 3) << 6) | 0x21, \
  875. ((ipst_s & 0xF) << 1) | ((ipre_s & 3) << 6) | 0x21 } }
  876. static struct dds_init {
  877. uint8_t reg_vals[NUM_DDS_REGS];
  878. } dds_init_vals[] = {
  879. /* DDR(FDR) SDR(HDR) */
  880. /* Vendor recommends below for 3m cable */
  881. #define DDS_3M 0
  882. DDS_VAL(31, 19, 12, 0, 29, 22, 9, 0),
  883. DDS_VAL(31, 12, 15, 4, 31, 15, 15, 1),
  884. DDS_VAL(31, 13, 15, 3, 31, 16, 15, 0),
  885. DDS_VAL(31, 14, 15, 2, 31, 17, 14, 0),
  886. DDS_VAL(31, 15, 15, 1, 31, 18, 13, 0),
  887. DDS_VAL(31, 16, 15, 0, 31, 19, 12, 0),
  888. DDS_VAL(31, 17, 14, 0, 31, 20, 11, 0),
  889. DDS_VAL(31, 18, 13, 0, 30, 21, 10, 0),
  890. DDS_VAL(31, 20, 11, 0, 28, 23, 8, 0),
  891. DDS_VAL(31, 21, 10, 0, 27, 24, 7, 0),
  892. DDS_VAL(31, 22, 9, 0, 26, 25, 6, 0),
  893. DDS_VAL(30, 23, 8, 0, 25, 26, 5, 0),
  894. DDS_VAL(29, 24, 7, 0, 23, 27, 4, 0),
  895. /* Vendor recommends below for 1m cable */
  896. #define DDS_1M 13
  897. DDS_VAL(28, 25, 6, 0, 21, 28, 3, 0),
  898. DDS_VAL(27, 26, 5, 0, 19, 29, 2, 0),
  899. DDS_VAL(25, 27, 4, 0, 17, 30, 1, 0)
  900. };
  901. /*
  902. * Now the RXEQ section of the table.
  903. */
  904. /* Hardware packs an element number and register address thus: */
  905. #define RXEQ_INIT_RDESC(elt, addr) (((elt) & 0xF) | ((addr) << 4))
  906. #define RXEQ_VAL(elt, adr, val0, val1, val2, val3) \
  907. {RXEQ_INIT_RDESC((elt), (adr)), {(val0), (val1), (val2), (val3)} }
  908. #define RXEQ_VAL_ALL(elt, adr, val) \
  909. {RXEQ_INIT_RDESC((elt), (adr)), {(val), (val), (val), (val)} }
  910. #define RXEQ_SDR_DFELTH 0
  911. #define RXEQ_SDR_TLTH 0
  912. #define RXEQ_SDR_G1CNT_Z1CNT 0x11
  913. #define RXEQ_SDR_ZCNT 23
  914. static struct rxeq_init {
  915. u16 rdesc; /* in form used in SerDesDDSRXEQ */
  916. u8 rdata[4];
  917. } rxeq_init_vals[] = {
  918. /* Set Rcv Eq. to Preset node */
  919. RXEQ_VAL_ALL(7, 0x27, 0x10),
  920. /* Set DFELTHFDR/HDR thresholds */
  921. RXEQ_VAL(7, 8, 0, 0, 0, 0), /* FDR, was 0, 1, 2, 3 */
  922. RXEQ_VAL(7, 0x21, 0, 0, 0, 0), /* HDR */
  923. /* Set TLTHFDR/HDR theshold */
  924. RXEQ_VAL(7, 9, 2, 2, 2, 2), /* FDR, was 0, 2, 4, 6 */
  925. RXEQ_VAL(7, 0x23, 2, 2, 2, 2), /* HDR, was 0, 1, 2, 3 */
  926. /* Set Preamp setting 2 (ZFR/ZCNT) */
  927. RXEQ_VAL(7, 0x1B, 12, 12, 12, 12), /* FDR, was 12, 16, 20, 24 */
  928. RXEQ_VAL(7, 0x1C, 12, 12, 12, 12), /* HDR, was 12, 16, 20, 24 */
  929. /* Set Preamp DC gain and Setting 1 (GFR/GHR) */
  930. RXEQ_VAL(7, 0x1E, 16, 16, 16, 16), /* FDR, was 16, 17, 18, 20 */
  931. RXEQ_VAL(7, 0x1F, 16, 16, 16, 16), /* HDR, was 16, 17, 18, 20 */
  932. /* Toggle RELOCK (in VCDL_CTRL0) to lock to data */
  933. RXEQ_VAL_ALL(6, 6, 0x20), /* Set D5 High */
  934. RXEQ_VAL_ALL(6, 6, 0), /* Set D5 Low */
  935. };
  936. /* There are 17 values from vendor, but IBC only accesses the first 16 */
  937. #define DDS_ROWS (16)
  938. #define RXEQ_ROWS ARRAY_SIZE(rxeq_init_vals)
  939. static int qib_sd_setvals(struct qib_devdata *dd)
  940. {
  941. int idx, midx;
  942. int min_idx; /* Minimum index for this portion of table */
  943. uint32_t dds_reg_map;
  944. u64 __iomem *taddr, *iaddr;
  945. uint64_t data;
  946. uint64_t sdctl;
  947. taddr = dd->kregbase + kr_serdes_maptable;
  948. iaddr = dd->kregbase + kr_serdes_ddsrxeq0;
  949. /*
  950. * Init the DDS section of the table.
  951. * Each "row" of the table provokes NUM_DDS_REG writes, to the
  952. * registers indicated in DDS_REG_MAP.
  953. */
  954. sdctl = qib_read_kreg64(dd, kr_ibserdesctrl);
  955. sdctl = (sdctl & ~(0x1f << 8)) | (NUM_DDS_REGS << 8);
  956. sdctl = (sdctl & ~(0x1f << 13)) | (RXEQ_ROWS << 13);
  957. qib_write_kreg(dd, kr_ibserdesctrl, sdctl);
  958. /*
  959. * Iterate down table within loop for each register to store.
  960. */
  961. dds_reg_map = DDS_REG_MAP;
  962. for (idx = 0; idx < NUM_DDS_REGS; ++idx) {
  963. data = ((dds_reg_map & 0xF) << 4) | TX_FAST_ELT;
  964. writeq(data, iaddr + idx);
  965. mmiowb();
  966. qib_read_kreg32(dd, kr_scratch);
  967. dds_reg_map >>= 4;
  968. for (midx = 0; midx < DDS_ROWS; ++midx) {
  969. u64 __iomem *daddr = taddr + ((midx << 4) + idx);
  970. data = dds_init_vals[midx].reg_vals[idx];
  971. writeq(data, daddr);
  972. mmiowb();
  973. qib_read_kreg32(dd, kr_scratch);
  974. } /* End inner for (vals for this reg, each row) */
  975. } /* end outer for (regs to be stored) */
  976. /*
  977. * Init the RXEQ section of the table.
  978. * This runs in a different order, as the pattern of
  979. * register references is more complex, but there are only
  980. * four "data" values per register.
  981. */
  982. min_idx = idx; /* RXEQ indices pick up where DDS left off */
  983. taddr += 0x100; /* RXEQ data is in second half of table */
  984. /* Iterate through RXEQ register addresses */
  985. for (idx = 0; idx < RXEQ_ROWS; ++idx) {
  986. int didx; /* "destination" */
  987. int vidx;
  988. /* didx is offset by min_idx to address RXEQ range of regs */
  989. didx = idx + min_idx;
  990. /* Store the next RXEQ register address */
  991. writeq(rxeq_init_vals[idx].rdesc, iaddr + didx);
  992. mmiowb();
  993. qib_read_kreg32(dd, kr_scratch);
  994. /* Iterate through RXEQ values */
  995. for (vidx = 0; vidx < 4; vidx++) {
  996. data = rxeq_init_vals[idx].rdata[vidx];
  997. writeq(data, taddr + (vidx << 6) + idx);
  998. mmiowb();
  999. qib_read_kreg32(dd, kr_scratch);
  1000. }
  1001. } /* end outer for (Reg-writes for RXEQ) */
  1002. return 0;
  1003. }
  1004. #define CMUCTRL5 EPB_LOC(7, 0, 0x15)
  1005. #define RXHSCTRL0(chan) EPB_LOC(chan, 6, 0)
  1006. #define VCDL_DAC2(chan) EPB_LOC(chan, 6, 5)
  1007. #define VCDL_CTRL0(chan) EPB_LOC(chan, 6, 6)
  1008. #define VCDL_CTRL2(chan) EPB_LOC(chan, 6, 8)
  1009. #define START_EQ2(chan) EPB_LOC(chan, 7, 0x28)
  1010. /*
  1011. * Repeat a "store" across all channels of the IB SerDes.
  1012. * Although nominally it inherits the "read value" of the last
  1013. * channel it modified, the only really useful return is <0 for
  1014. * failure, >= 0 for success. The parameter 'loc' is assumed to
  1015. * be the location in some channel of the register to be modified
  1016. * The caller can specify use of the "gang write" option of EPB,
  1017. * in which case we use the specified channel data for any fields
  1018. * not explicitely written.
  1019. */
  1020. static int ibsd_mod_allchnls(struct qib_devdata *dd, int loc, int val,
  1021. int mask)
  1022. {
  1023. int ret = -1;
  1024. int chnl;
  1025. if (loc & EPB_GLOBAL_WR) {
  1026. /*
  1027. * Our caller has assured us that we can set all four
  1028. * channels at once. Trust that. If mask is not 0xFF,
  1029. * we will read the _specified_ channel for our starting
  1030. * value.
  1031. */
  1032. loc |= (1U << EPB_IB_QUAD0_CS_SHF);
  1033. chnl = (loc >> (4 + EPB_ADDR_SHF)) & 7;
  1034. if (mask != 0xFF) {
  1035. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
  1036. loc & ~EPB_GLOBAL_WR, 0, 0);
  1037. if (ret < 0) {
  1038. int sloc = loc >> EPB_ADDR_SHF;
  1039. qib_dev_err(dd,
  1040. "pre-read failed: elt %d, addr 0x%X, chnl %d\n",
  1041. (sloc & 0xF),
  1042. (sloc >> 9) & 0x3f, chnl);
  1043. return ret;
  1044. }
  1045. val = (ret & ~mask) | (val & mask);
  1046. }
  1047. loc &= ~(7 << (4+EPB_ADDR_SHF));
  1048. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);
  1049. if (ret < 0) {
  1050. int sloc = loc >> EPB_ADDR_SHF;
  1051. qib_dev_err(dd,
  1052. "Global WR failed: elt %d, addr 0x%X, val %02X\n",
  1053. (sloc & 0xF), (sloc >> 9) & 0x3f, val);
  1054. }
  1055. return ret;
  1056. }
  1057. /* Clear "channel" and set CS so we can simply iterate */
  1058. loc &= ~(7 << (4+EPB_ADDR_SHF));
  1059. loc |= (1U << EPB_IB_QUAD0_CS_SHF);
  1060. for (chnl = 0; chnl < 4; ++chnl) {
  1061. int cloc = loc | (chnl << (4+EPB_ADDR_SHF));
  1062. ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, cloc, val, mask);
  1063. if (ret < 0) {
  1064. int sloc = loc >> EPB_ADDR_SHF;
  1065. qib_dev_err(dd,
  1066. "Write failed: elt %d, addr 0x%X, chnl %d, val 0x%02X, mask 0x%02X\n",
  1067. (sloc & 0xF), (sloc >> 9) & 0x3f, chnl,
  1068. val & 0xFF, mask & 0xFF);
  1069. break;
  1070. }
  1071. }
  1072. return ret;
  1073. }
  1074. /*
  1075. * Set the Tx values normally modified by IBC in IB1.2 mode to default
  1076. * values, as gotten from first row of init table.
  1077. */
  1078. static int set_dds_vals(struct qib_devdata *dd, struct dds_init *ddi)
  1079. {
  1080. int ret;
  1081. int idx, reg, data;
  1082. uint32_t regmap;
  1083. regmap = DDS_REG_MAP;
  1084. for (idx = 0; idx < NUM_DDS_REGS; ++idx) {
  1085. reg = (regmap & 0xF);
  1086. regmap >>= 4;
  1087. data = ddi->reg_vals[idx];
  1088. /* Vendor says RMW not needed for these regs, use 0xFF mask */
  1089. ret = ibsd_mod_allchnls(dd, EPB_LOC(0, 9, reg), data, 0xFF);
  1090. if (ret < 0)
  1091. break;
  1092. }
  1093. return ret;
  1094. }
  1095. /*
  1096. * Set the Rx values normally modified by IBC in IB1.2 mode to default
  1097. * values, as gotten from selected column of init table.
  1098. */
  1099. static int set_rxeq_vals(struct qib_devdata *dd, int vsel)
  1100. {
  1101. int ret;
  1102. int ridx;
  1103. int cnt = ARRAY_SIZE(rxeq_init_vals);
  1104. for (ridx = 0; ridx < cnt; ++ridx) {
  1105. int elt, reg, val, loc;
  1106. elt = rxeq_init_vals[ridx].rdesc & 0xF;
  1107. reg = rxeq_init_vals[ridx].rdesc >> 4;
  1108. loc = EPB_LOC(0, elt, reg);
  1109. val = rxeq_init_vals[ridx].rdata[vsel];
  1110. /* mask of 0xFF, because hardware does full-byte store. */
  1111. ret = ibsd_mod_allchnls(dd, loc, val, 0xFF);
  1112. if (ret < 0)
  1113. break;
  1114. }
  1115. return ret;
  1116. }
  1117. /*
  1118. * Set the default values (row 0) for DDR Driver Demphasis.
  1119. * we do this initially and whenever we turn off IB-1.2
  1120. *
  1121. * The "default" values for Rx equalization are also stored to
  1122. * SerDes registers. Formerly (and still default), we used set 2.
  1123. * For experimenting with cables and link-partners, we allow changing
  1124. * that via a module parameter.
  1125. */
  1126. static unsigned qib_rxeq_set = 2;
  1127. module_param_named(rxeq_default_set, qib_rxeq_set, uint,
  1128. S_IWUSR | S_IRUGO);
  1129. MODULE_PARM_DESC(rxeq_default_set,
  1130. "Which set [0..3] of Rx Equalization values is default");
  1131. static int qib_internal_presets(struct qib_devdata *dd)
  1132. {
  1133. int ret = 0;
  1134. ret = set_dds_vals(dd, dds_init_vals + DDS_3M);
  1135. if (ret < 0)
  1136. qib_dev_err(dd, "Failed to set default DDS values\n");
  1137. ret = set_rxeq_vals(dd, qib_rxeq_set & 3);
  1138. if (ret < 0)
  1139. qib_dev_err(dd, "Failed to set default RXEQ values\n");
  1140. return ret;
  1141. }
  1142. int qib_sd7220_presets(struct qib_devdata *dd)
  1143. {
  1144. int ret = 0;
  1145. if (!dd->cspec->presets_needed)
  1146. return ret;
  1147. dd->cspec->presets_needed = 0;
  1148. /* Assert uC reset, so we don't clash with it. */
  1149. qib_ibsd_reset(dd, 1);
  1150. udelay(2);
  1151. qib_sd_trimdone_monitor(dd, "link-down");
  1152. ret = qib_internal_presets(dd);
  1153. return ret;
  1154. }
  1155. static int qib_sd_trimself(struct qib_devdata *dd, int val)
  1156. {
  1157. int loc = CMUCTRL5 | (1U << EPB_IB_QUAD0_CS_SHF);
  1158. return qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);
  1159. }
  1160. static int qib_sd_early(struct qib_devdata *dd)
  1161. {
  1162. int ret;
  1163. ret = ibsd_mod_allchnls(dd, RXHSCTRL0(0) | EPB_GLOBAL_WR, 0xD4, 0xFF);
  1164. if (ret < 0)
  1165. goto bail;
  1166. ret = ibsd_mod_allchnls(dd, START_EQ1(0) | EPB_GLOBAL_WR, 0x10, 0xFF);
  1167. if (ret < 0)
  1168. goto bail;
  1169. ret = ibsd_mod_allchnls(dd, START_EQ2(0) | EPB_GLOBAL_WR, 0x30, 0xFF);
  1170. bail:
  1171. return ret;
  1172. }
  1173. #define BACTRL(chnl) EPB_LOC(chnl, 6, 0x0E)
  1174. #define LDOUTCTRL1(chnl) EPB_LOC(chnl, 7, 6)
  1175. #define RXHSSTATUS(chnl) EPB_LOC(chnl, 6, 0xF)
  1176. static int qib_sd_dactrim(struct qib_devdata *dd)
  1177. {
  1178. int ret;
  1179. ret = ibsd_mod_allchnls(dd, VCDL_DAC2(0) | EPB_GLOBAL_WR, 0x2D, 0xFF);
  1180. if (ret < 0)
  1181. goto bail;
  1182. /* more fine-tuning of what will be default */
  1183. ret = ibsd_mod_allchnls(dd, VCDL_CTRL2(0), 3, 0xF);
  1184. if (ret < 0)
  1185. goto bail;
  1186. ret = ibsd_mod_allchnls(dd, BACTRL(0) | EPB_GLOBAL_WR, 0x40, 0xFF);
  1187. if (ret < 0)
  1188. goto bail;
  1189. ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x04, 0xFF);
  1190. if (ret < 0)
  1191. goto bail;
  1192. ret = ibsd_mod_allchnls(dd, RXHSSTATUS(0) | EPB_GLOBAL_WR, 0x04, 0xFF);
  1193. if (ret < 0)
  1194. goto bail;
  1195. /*
  1196. * Delay for max possible number of steps, with slop.
  1197. * Each step is about 4usec.
  1198. */
  1199. udelay(415);
  1200. ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x00, 0xFF);
  1201. bail:
  1202. return ret;
  1203. }
  1204. #define RELOCK_FIRST_MS 3
  1205. #define RXLSPPM(chan) EPB_LOC(chan, 0, 2)
  1206. void toggle_7220_rclkrls(struct qib_devdata *dd)
  1207. {
  1208. int loc = RXLSPPM(0) | EPB_GLOBAL_WR;
  1209. int ret;
  1210. ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);
  1211. if (ret < 0)
  1212. qib_dev_err(dd, "RCLKRLS failed to clear D7\n");
  1213. else {
  1214. udelay(1);
  1215. ibsd_mod_allchnls(dd, loc, 0x80, 0x80);
  1216. }
  1217. /* And again for good measure */
  1218. udelay(1);
  1219. ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);
  1220. if (ret < 0)
  1221. qib_dev_err(dd, "RCLKRLS failed to clear D7\n");
  1222. else {
  1223. udelay(1);
  1224. ibsd_mod_allchnls(dd, loc, 0x80, 0x80);
  1225. }
  1226. /* Now reset xgxs and IBC to complete the recovery */
  1227. dd->f_xgxs_reset(dd->pport);
  1228. }
  1229. /*
  1230. * Shut down the timer that polls for relock occasions, if needed
  1231. * this is "hooked" from qib_7220_quiet_serdes(), which is called
  1232. * just before qib_shutdown_device() in qib_driver.c shuts down all
  1233. * the other timers
  1234. */
  1235. void shutdown_7220_relock_poll(struct qib_devdata *dd)
  1236. {
  1237. if (dd->cspec->relock_timer_active)
  1238. del_timer_sync(&dd->cspec->relock_timer);
  1239. }
  1240. static unsigned qib_relock_by_timer = 1;
  1241. module_param_named(relock_by_timer, qib_relock_by_timer, uint,
  1242. S_IWUSR | S_IRUGO);
  1243. MODULE_PARM_DESC(relock_by_timer, "Allow relock attempt if link not up");
  1244. static void qib_run_relock(struct timer_list *t)
  1245. {
  1246. struct qib_chip_specific *cs = from_timer(cs, t, relock_timer);
  1247. struct qib_devdata *dd = cs->dd;
  1248. struct qib_pportdata *ppd = dd->pport;
  1249. int timeoff;
  1250. /*
  1251. * Check link-training state for "stuck" state, when down.
  1252. * if found, try relock and schedule another try at
  1253. * exponentially growing delay, maxed at one second.
  1254. * if not stuck, our work is done.
  1255. */
  1256. if ((dd->flags & QIB_INITTED) && !(ppd->lflags &
  1257. (QIBL_IB_AUTONEG_INPROG | QIBL_LINKINIT | QIBL_LINKARMED |
  1258. QIBL_LINKACTIVE))) {
  1259. if (qib_relock_by_timer) {
  1260. if (!(ppd->lflags & QIBL_IB_LINK_DISABLED))
  1261. toggle_7220_rclkrls(dd);
  1262. }
  1263. /* re-set timer for next check */
  1264. timeoff = cs->relock_interval << 1;
  1265. if (timeoff > HZ)
  1266. timeoff = HZ;
  1267. cs->relock_interval = timeoff;
  1268. } else
  1269. timeoff = HZ;
  1270. mod_timer(&cs->relock_timer, jiffies + timeoff);
  1271. }
  1272. void set_7220_relock_poll(struct qib_devdata *dd, int ibup)
  1273. {
  1274. struct qib_chip_specific *cs = dd->cspec;
  1275. if (ibup) {
  1276. /* We are now up, relax timer to 1 second interval */
  1277. if (cs->relock_timer_active) {
  1278. cs->relock_interval = HZ;
  1279. mod_timer(&cs->relock_timer, jiffies + HZ);
  1280. }
  1281. } else {
  1282. /* Transition to down, (re-)set timer to short interval. */
  1283. unsigned int timeout;
  1284. timeout = msecs_to_jiffies(RELOCK_FIRST_MS);
  1285. if (timeout == 0)
  1286. timeout = 1;
  1287. /* If timer has not yet been started, do so. */
  1288. if (!cs->relock_timer_active) {
  1289. cs->relock_timer_active = 1;
  1290. timer_setup(&cs->relock_timer, qib_run_relock, 0);
  1291. cs->relock_interval = timeout;
  1292. cs->relock_timer.expires = jiffies + timeout;
  1293. add_timer(&cs->relock_timer);
  1294. } else {
  1295. cs->relock_interval = timeout;
  1296. mod_timer(&cs->relock_timer, jiffies + timeout);
  1297. }
  1298. }
  1299. }