qib_sd7220.c 40 KB

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