Path: blob/master/drivers/infiniband/hw/qib/qib_sd7220.c
15112 views
/*1* Copyright (c) 2006, 2007, 2008, 2009, 2010 QLogic Corporation.2* All rights reserved.3* Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.4*5* This software is available to you under a choice of one of two6* licenses. You may choose to be licensed under the terms of the GNU7* General Public License (GPL) Version 2, available from the file8* COPYING in the main directory of this source tree, or the9* OpenIB.org BSD license below:10*11* Redistribution and use in source and binary forms, with or12* without modification, are permitted provided that the following13* conditions are met:14*15* - Redistributions of source code must retain the above16* copyright notice, this list of conditions and the following17* disclaimer.18*19* - Redistributions in binary form must reproduce the above20* copyright notice, this list of conditions and the following21* disclaimer in the documentation and/or other materials22* provided with the distribution.23*24* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,25* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF26* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND27* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS28* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN29* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN30* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE31* SOFTWARE.32*/33/*34* This file contains all of the code that is specific to the SerDes35* on the QLogic_IB 7220 chip.36*/3738#include <linux/pci.h>39#include <linux/delay.h>40#include <linux/firmware.h>4142#include "qib.h"43#include "qib_7220.h"4445#define SD7220_FW_NAME "qlogic/sd7220.fw"46MODULE_FIRMWARE(SD7220_FW_NAME);4748/*49* Same as in qib_iba7220.c, but just the registers needed here.50* Could move whole set to qib_7220.h, but decided better to keep51* local.52*/53#define KREG_IDX(regname) (QIB_7220_##regname##_OFFS / sizeof(u64))54#define kr_hwerrclear KREG_IDX(HwErrClear)55#define kr_hwerrmask KREG_IDX(HwErrMask)56#define kr_hwerrstatus KREG_IDX(HwErrStatus)57#define kr_ibcstatus KREG_IDX(IBCStatus)58#define kr_ibserdesctrl KREG_IDX(IBSerDesCtrl)59#define kr_scratch KREG_IDX(Scratch)60#define kr_xgxs_cfg KREG_IDX(XGXSCfg)61/* these are used only here, not in qib_iba7220.c */62#define kr_ibsd_epb_access_ctrl KREG_IDX(ibsd_epb_access_ctrl)63#define kr_ibsd_epb_transaction_reg KREG_IDX(ibsd_epb_transaction_reg)64#define kr_pciesd_epb_transaction_reg KREG_IDX(pciesd_epb_transaction_reg)65#define kr_pciesd_epb_access_ctrl KREG_IDX(pciesd_epb_access_ctrl)66#define kr_serdes_ddsrxeq0 KREG_IDX(SerDes_DDSRXEQ0)6768/*69* The IBSerDesMappTable is a memory that holds values to be stored in70* various SerDes registers by IBC.71*/72#define kr_serdes_maptable KREG_IDX(IBSerDesMappTable)7374/*75* Below used for sdnum parameter, selecting one of the two sections76* used for PCIe, or the single SerDes used for IB.77*/78#define PCIE_SERDES0 079#define PCIE_SERDES1 18081/*82* The EPB requires addressing in a particular form. EPB_LOC() is intended83* to make #definitions a little more readable.84*/85#define EPB_ADDR_SHF 886#define EPB_LOC(chn, elt, reg) \87(((elt & 0xf) | ((chn & 7) << 4) | ((reg & 0x3f) << 9)) << \88EPB_ADDR_SHF)89#define EPB_IB_QUAD0_CS_SHF (25)90#define EPB_IB_QUAD0_CS (1U << EPB_IB_QUAD0_CS_SHF)91#define EPB_IB_UC_CS_SHF (26)92#define EPB_PCIE_UC_CS_SHF (27)93#define EPB_GLOBAL_WR (1U << (EPB_ADDR_SHF + 8))9495/* Forward declarations. */96static int qib_sd7220_reg_mod(struct qib_devdata *dd, int sdnum, u32 loc,97u32 data, u32 mask);98static int ibsd_mod_allchnls(struct qib_devdata *dd, int loc, int val,99int mask);100static int qib_sd_trimdone_poll(struct qib_devdata *dd);101static void qib_sd_trimdone_monitor(struct qib_devdata *dd, const char *where);102static int qib_sd_setvals(struct qib_devdata *dd);103static int qib_sd_early(struct qib_devdata *dd);104static int qib_sd_dactrim(struct qib_devdata *dd);105static int qib_internal_presets(struct qib_devdata *dd);106/* Tweak the register (CMUCTRL5) that contains the TRIMSELF controls */107static int qib_sd_trimself(struct qib_devdata *dd, int val);108static int epb_access(struct qib_devdata *dd, int sdnum, int claim);109static int qib_sd7220_ib_load(struct qib_devdata *dd,110const struct firmware *fw);111static int qib_sd7220_ib_vfy(struct qib_devdata *dd,112const struct firmware *fw);113114/*115* Below keeps track of whether the "once per power-on" initialization has116* been done, because uC code Version 1.32.17 or higher allows the uC to117* be reset at will, and Automatic Equalization may require it. So the118* state of the reset "pin", is no longer valid. Instead, we check for the119* actual uC code having been loaded.120*/121static int qib_ibsd_ucode_loaded(struct qib_pportdata *ppd,122const struct firmware *fw)123{124struct qib_devdata *dd = ppd->dd;125126if (!dd->cspec->serdes_first_init_done &&127qib_sd7220_ib_vfy(dd, fw) > 0)128dd->cspec->serdes_first_init_done = 1;129return dd->cspec->serdes_first_init_done;130}131132/* repeat #define for local use. "Real" #define is in qib_iba7220.c */133#define QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR 0x0000004000000000ULL134#define IB_MPREG5 (EPB_LOC(6, 0, 0xE) | (1L << EPB_IB_UC_CS_SHF))135#define IB_MPREG6 (EPB_LOC(6, 0, 0xF) | (1U << EPB_IB_UC_CS_SHF))136#define UC_PAR_CLR_D 8137#define UC_PAR_CLR_M 0xC138#define IB_CTRL2(chn) (EPB_LOC(chn, 7, 3) | EPB_IB_QUAD0_CS)139#define START_EQ1(chan) EPB_LOC(chan, 7, 0x27)140141void qib_sd7220_clr_ibpar(struct qib_devdata *dd)142{143int ret;144145/* clear, then re-enable parity errs */146ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6,147UC_PAR_CLR_D, UC_PAR_CLR_M);148if (ret < 0) {149qib_dev_err(dd, "Failed clearing IBSerDes Parity err\n");150goto bail;151}152ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0,153UC_PAR_CLR_M);154155qib_read_kreg32(dd, kr_scratch);156udelay(4);157qib_write_kreg(dd, kr_hwerrclear,158QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);159qib_read_kreg32(dd, kr_scratch);160bail:161return;162}163164/*165* After a reset or other unusual event, the epb interface may need166* to be re-synchronized, between the host and the uC.167* returns <0 for failure to resync within IBSD_RESYNC_TRIES (not expected)168*/169#define IBSD_RESYNC_TRIES 3170#define IB_PGUDP(chn) (EPB_LOC((chn), 2, 1) | EPB_IB_QUAD0_CS)171#define IB_CMUDONE(chn) (EPB_LOC((chn), 7, 0xF) | EPB_IB_QUAD0_CS)172173static int qib_resync_ibepb(struct qib_devdata *dd)174{175int ret, pat, tries, chn;176u32 loc;177178ret = -1;179chn = 0;180for (tries = 0; tries < (4 * IBSD_RESYNC_TRIES); ++tries) {181loc = IB_PGUDP(chn);182ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);183if (ret < 0) {184qib_dev_err(dd, "Failed read in resync\n");185continue;186}187if (ret != 0xF0 && ret != 0x55 && tries == 0)188qib_dev_err(dd, "unexpected pattern in resync\n");189pat = ret ^ 0xA5; /* alternate F0 and 55 */190ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, pat, 0xFF);191if (ret < 0) {192qib_dev_err(dd, "Failed write in resync\n");193continue;194}195ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);196if (ret < 0) {197qib_dev_err(dd, "Failed re-read in resync\n");198continue;199}200if (ret != pat) {201qib_dev_err(dd, "Failed compare1 in resync\n");202continue;203}204loc = IB_CMUDONE(chn);205ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);206if (ret < 0) {207qib_dev_err(dd, "Failed CMUDONE rd in resync\n");208continue;209}210if ((ret & 0x70) != ((chn << 4) | 0x40)) {211qib_dev_err(dd, "Bad CMUDONE value %02X, chn %d\n",212ret, chn);213continue;214}215if (++chn == 4)216break; /* Success */217}218return (ret > 0) ? 0 : ret;219}220221/*222* Localize the stuff that should be done to change IB uC reset223* returns <0 for errors.224*/225static int qib_ibsd_reset(struct qib_devdata *dd, int assert_rst)226{227u64 rst_val;228int ret = 0;229unsigned long flags;230231rst_val = qib_read_kreg64(dd, kr_ibserdesctrl);232if (assert_rst) {233/*234* Vendor recommends "interrupting" uC before reset, to235* minimize possible glitches.236*/237spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);238epb_access(dd, IB_7220_SERDES, 1);239rst_val |= 1ULL;240/* Squelch possible parity error from _asserting_ reset */241qib_write_kreg(dd, kr_hwerrmask,242dd->cspec->hwerrmask &243~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);244qib_write_kreg(dd, kr_ibserdesctrl, rst_val);245/* flush write, delay to ensure it took effect */246qib_read_kreg32(dd, kr_scratch);247udelay(2);248/* once it's reset, can remove interrupt */249epb_access(dd, IB_7220_SERDES, -1);250spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);251} else {252/*253* Before we de-assert reset, we need to deal with254* possible glitch on the Parity-error line.255* Suppress it around the reset, both in chip-level256* hwerrmask and in IB uC control reg. uC will allow257* it again during startup.258*/259u64 val;260rst_val &= ~(1ULL);261qib_write_kreg(dd, kr_hwerrmask,262dd->cspec->hwerrmask &263~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);264265ret = qib_resync_ibepb(dd);266if (ret < 0)267qib_dev_err(dd, "unable to re-sync IB EPB\n");268269/* set uC control regs to suppress parity errs */270ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG5, 1, 1);271if (ret < 0)272goto bail;273/* IB uC code past Version 1.32.17 allow suppression of wdog */274ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0x80,2750x80);276if (ret < 0) {277qib_dev_err(dd, "Failed to set WDOG disable\n");278goto bail;279}280qib_write_kreg(dd, kr_ibserdesctrl, rst_val);281/* flush write, delay for startup */282qib_read_kreg32(dd, kr_scratch);283udelay(1);284/* clear, then re-enable parity errs */285qib_sd7220_clr_ibpar(dd);286val = qib_read_kreg64(dd, kr_hwerrstatus);287if (val & QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR) {288qib_dev_err(dd, "IBUC Parity still set after RST\n");289dd->cspec->hwerrmask &=290~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR;291}292qib_write_kreg(dd, kr_hwerrmask,293dd->cspec->hwerrmask);294}295296bail:297return ret;298}299300static void qib_sd_trimdone_monitor(struct qib_devdata *dd,301const char *where)302{303int ret, chn, baduns;304u64 val;305306if (!where)307where = "?";308309/* give time for reset to settle out in EPB */310udelay(2);311312ret = qib_resync_ibepb(dd);313if (ret < 0)314qib_dev_err(dd, "not able to re-sync IB EPB (%s)\n", where);315316/* Do "sacrificial read" to get EPB in sane state after reset */317ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_CTRL2(0), 0, 0);318if (ret < 0)319qib_dev_err(dd, "Failed TRIMDONE 1st read, (%s)\n", where);320321/* Check/show "summary" Trim-done bit in IBCStatus */322val = qib_read_kreg64(dd, kr_ibcstatus);323if (!(val & (1ULL << 11)))324qib_dev_err(dd, "IBCS TRIMDONE clear (%s)\n", where);325/*326* Do "dummy read/mod/wr" to get EPB in sane state after reset327* The default value for MPREG6 is 0.328*/329udelay(2);330331ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0x80, 0x80);332if (ret < 0)333qib_dev_err(dd, "Failed Dummy RMW, (%s)\n", where);334udelay(10);335336baduns = 0;337338for (chn = 3; chn >= 0; --chn) {339/* Read CTRL reg for each channel to check TRIMDONE */340ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,341IB_CTRL2(chn), 0, 0);342if (ret < 0)343qib_dev_err(dd, "Failed checking TRIMDONE, chn %d"344" (%s)\n", chn, where);345346if (!(ret & 0x10)) {347int probe;348349baduns |= (1 << chn);350qib_dev_err(dd, "TRIMDONE cleared on chn %d (%02X)."351" (%s)\n", chn, ret, where);352probe = qib_sd7220_reg_mod(dd, IB_7220_SERDES,353IB_PGUDP(0), 0, 0);354qib_dev_err(dd, "probe is %d (%02X)\n",355probe, probe);356probe = qib_sd7220_reg_mod(dd, IB_7220_SERDES,357IB_CTRL2(chn), 0, 0);358qib_dev_err(dd, "re-read: %d (%02X)\n",359probe, probe);360ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,361IB_CTRL2(chn), 0x10, 0x10);362if (ret < 0)363qib_dev_err(dd,364"Err on TRIMDONE rewrite1\n");365}366}367for (chn = 3; chn >= 0; --chn) {368/* Read CTRL reg for each channel to check TRIMDONE */369if (baduns & (1 << chn)) {370qib_dev_err(dd,371"Reseting TRIMDONE on chn %d (%s)\n",372chn, where);373ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,374IB_CTRL2(chn), 0x10, 0x10);375if (ret < 0)376qib_dev_err(dd, "Failed re-setting "377"TRIMDONE, chn %d (%s)\n",378chn, where);379}380}381}382383/*384* Below is portion of IBA7220-specific bringup_serdes() that actually385* deals with registers and memory within the SerDes itself.386* Post IB uC code version 1.32.17, was_reset being 1 is not really387* informative, so we double-check.388*/389int qib_sd7220_init(struct qib_devdata *dd)390{391const struct firmware *fw;392int ret = 1; /* default to failure */393int first_reset, was_reset;394395/* SERDES MPU reset recorded in D0 */396was_reset = (qib_read_kreg64(dd, kr_ibserdesctrl) & 1);397if (!was_reset) {398/* entered with reset not asserted, we need to do it */399qib_ibsd_reset(dd, 1);400qib_sd_trimdone_monitor(dd, "Driver-reload");401}402403ret = request_firmware(&fw, SD7220_FW_NAME, &dd->pcidev->dev);404if (ret) {405qib_dev_err(dd, "Failed to load IB SERDES image\n");406goto done;407}408409/* Substitute our deduced value for was_reset */410ret = qib_ibsd_ucode_loaded(dd->pport, fw);411if (ret < 0)412goto bail;413414first_reset = !ret; /* First reset if IBSD uCode not yet loaded */415/*416* Alter some regs per vendor latest doc, reset-defaults417* are not right for IB.418*/419ret = qib_sd_early(dd);420if (ret < 0) {421qib_dev_err(dd, "Failed to set IB SERDES early defaults\n");422goto bail;423}424/*425* Set DAC manual trim IB.426* We only do this once after chip has been reset (usually427* same as once per system boot).428*/429if (first_reset) {430ret = qib_sd_dactrim(dd);431if (ret < 0) {432qib_dev_err(dd, "Failed IB SERDES DAC trim\n");433goto bail;434}435}436/*437* Set various registers (DDS and RXEQ) that will be438* controlled by IBC (in 1.2 mode) to reasonable preset values439* Calling the "internal" version avoids the "check for needed"440* and "trimdone monitor" that might be counter-productive.441*/442ret = qib_internal_presets(dd);443if (ret < 0) {444qib_dev_err(dd, "Failed to set IB SERDES presets\n");445goto bail;446}447ret = qib_sd_trimself(dd, 0x80);448if (ret < 0) {449qib_dev_err(dd, "Failed to set IB SERDES TRIMSELF\n");450goto bail;451}452453/* Load image, then try to verify */454ret = 0; /* Assume success */455if (first_reset) {456int vfy;457int trim_done;458459ret = qib_sd7220_ib_load(dd, fw);460if (ret < 0) {461qib_dev_err(dd, "Failed to load IB SERDES image\n");462goto bail;463} else {464/* Loaded image, try to verify */465vfy = qib_sd7220_ib_vfy(dd, fw);466if (vfy != ret) {467qib_dev_err(dd, "SERDES PRAM VFY failed\n");468goto bail;469} /* end if verified */470} /* end if loaded */471472/*473* Loaded and verified. Almost good...474* hold "success" in ret475*/476ret = 0;477/*478* Prev steps all worked, continue bringup479* De-assert RESET to uC, only in first reset, to allow480* trimming.481*482* Since our default setup sets START_EQ1 to483* PRESET, we need to clear that for this very first run.484*/485ret = ibsd_mod_allchnls(dd, START_EQ1(0), 0, 0x38);486if (ret < 0) {487qib_dev_err(dd, "Failed clearing START_EQ1\n");488goto bail;489}490491qib_ibsd_reset(dd, 0);492/*493* If this is not the first reset, trimdone should be set494* already. We may need to check about this.495*/496trim_done = qib_sd_trimdone_poll(dd);497/*498* Whether or not trimdone succeeded, we need to put the499* uC back into reset to avoid a possible fight with the500* IBC state-machine.501*/502qib_ibsd_reset(dd, 1);503504if (!trim_done) {505qib_dev_err(dd, "No TRIMDONE seen\n");506goto bail;507}508/*509* DEBUG: check each time we reset if trimdone bits have510* gotten cleared, and re-set them.511*/512qib_sd_trimdone_monitor(dd, "First-reset");513/* Remember so we do not re-do the load, dactrim, etc. */514dd->cspec->serdes_first_init_done = 1;515}516/*517* setup for channel training and load values for518* RxEq and DDS in tables used by IBC in IB1.2 mode519*/520ret = 0;521if (qib_sd_setvals(dd) >= 0)522goto done;523bail:524ret = 1;525done:526/* start relock timer regardless, but start at 1 second */527set_7220_relock_poll(dd, -1);528529release_firmware(fw);530return ret;531}532533#define EPB_ACC_REQ 1534#define EPB_ACC_GNT 0x100535#define EPB_DATA_MASK 0xFF536#define EPB_RD (1ULL << 24)537#define EPB_TRANS_RDY (1ULL << 31)538#define EPB_TRANS_ERR (1ULL << 30)539#define EPB_TRANS_TRIES 5540541/*542* query, claim, release ownership of the EPB (External Parallel Bus)543* for a specified SERDES.544* the "claim" parameter is >0 to claim, <0 to release, 0 to query.545* Returns <0 for errors, >0 if we had ownership, else 0.546*/547static int epb_access(struct qib_devdata *dd, int sdnum, int claim)548{549u16 acc;550u64 accval;551int owned = 0;552u64 oct_sel = 0;553554switch (sdnum) {555case IB_7220_SERDES:556/*557* The IB SERDES "ownership" is fairly simple. A single each558* request/grant.559*/560acc = kr_ibsd_epb_access_ctrl;561break;562563case PCIE_SERDES0:564case PCIE_SERDES1:565/* PCIe SERDES has two "octants", need to select which */566acc = kr_pciesd_epb_access_ctrl;567oct_sel = (2 << (sdnum - PCIE_SERDES0));568break;569570default:571return 0;572}573574/* Make sure any outstanding transaction was seen */575qib_read_kreg32(dd, kr_scratch);576udelay(15);577578accval = qib_read_kreg32(dd, acc);579580owned = !!(accval & EPB_ACC_GNT);581if (claim < 0) {582/* Need to release */583u64 pollval;584/*585* The only writeable bits are the request and CS.586* Both should be clear587*/588u64 newval = 0;589qib_write_kreg(dd, acc, newval);590/* First read after write is not trustworthy */591pollval = qib_read_kreg32(dd, acc);592udelay(5);593pollval = qib_read_kreg32(dd, acc);594if (pollval & EPB_ACC_GNT)595owned = -1;596} else if (claim > 0) {597/* Need to claim */598u64 pollval;599u64 newval = EPB_ACC_REQ | oct_sel;600qib_write_kreg(dd, acc, newval);601/* First read after write is not trustworthy */602pollval = qib_read_kreg32(dd, acc);603udelay(5);604pollval = qib_read_kreg32(dd, acc);605if (!(pollval & EPB_ACC_GNT))606owned = -1;607}608return owned;609}610611/*612* Lemma to deal with race condition of write..read to epb regs613*/614static int epb_trans(struct qib_devdata *dd, u16 reg, u64 i_val, u64 *o_vp)615{616int tries;617u64 transval;618619qib_write_kreg(dd, reg, i_val);620/* Throw away first read, as RDY bit may be stale */621transval = qib_read_kreg64(dd, reg);622623for (tries = EPB_TRANS_TRIES; tries; --tries) {624transval = qib_read_kreg32(dd, reg);625if (transval & EPB_TRANS_RDY)626break;627udelay(5);628}629if (transval & EPB_TRANS_ERR)630return -1;631if (tries > 0 && o_vp)632*o_vp = transval;633return tries;634}635636/**637* qib_sd7220_reg_mod - modify SERDES register638* @dd: the qlogic_ib device639* @sdnum: which SERDES to access640* @loc: location - channel, element, register, as packed by EPB_LOC() macro.641* @wd: Write Data - value to set in register642* @mask: ones where data should be spliced into reg.643*644* Basic register read/modify/write, with un-needed acesses elided. That is,645* a mask of zero will prevent write, while a mask of 0xFF will prevent read.646* returns current (presumed, if a write was done) contents of selected647* register, or <0 if errors.648*/649static int qib_sd7220_reg_mod(struct qib_devdata *dd, int sdnum, u32 loc,650u32 wd, u32 mask)651{652u16 trans;653u64 transval;654int owned;655int tries, ret;656unsigned long flags;657658switch (sdnum) {659case IB_7220_SERDES:660trans = kr_ibsd_epb_transaction_reg;661break;662663case PCIE_SERDES0:664case PCIE_SERDES1:665trans = kr_pciesd_epb_transaction_reg;666break;667668default:669return -1;670}671672/*673* All access is locked in software (vs other host threads) and674* hardware (vs uC access).675*/676spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);677678owned = epb_access(dd, sdnum, 1);679if (owned < 0) {680spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);681return -1;682}683ret = 0;684for (tries = EPB_TRANS_TRIES; tries; --tries) {685transval = qib_read_kreg32(dd, trans);686if (transval & EPB_TRANS_RDY)687break;688udelay(5);689}690691if (tries > 0) {692tries = 1; /* to make read-skip work */693if (mask != 0xFF) {694/*695* Not a pure write, so need to read.696* loc encodes chip-select as well as address697*/698transval = loc | EPB_RD;699tries = epb_trans(dd, trans, transval, &transval);700}701if (tries > 0 && mask != 0) {702/*703* Not a pure read, so need to write.704*/705wd = (wd & mask) | (transval & ~mask);706transval = loc | (wd & EPB_DATA_MASK);707tries = epb_trans(dd, trans, transval, &transval);708}709}710/* else, failed to see ready, what error-handling? */711712/*713* Release bus. Failure is an error.714*/715if (epb_access(dd, sdnum, -1) < 0)716ret = -1;717else718ret = transval & EPB_DATA_MASK;719720spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);721if (tries <= 0)722ret = -1;723return ret;724}725726#define EPB_ROM_R (2)727#define EPB_ROM_W (1)728/*729* Below, all uC-related, use appropriate UC_CS, depending730* on which SerDes is used.731*/732#define EPB_UC_CTL EPB_LOC(6, 0, 0)733#define EPB_MADDRL EPB_LOC(6, 0, 2)734#define EPB_MADDRH EPB_LOC(6, 0, 3)735#define EPB_ROMDATA EPB_LOC(6, 0, 4)736#define EPB_RAMDATA EPB_LOC(6, 0, 5)737738/* Transfer date to/from uC Program RAM of IB or PCIe SerDes */739static int qib_sd7220_ram_xfer(struct qib_devdata *dd, int sdnum, u32 loc,740u8 *buf, int cnt, int rd_notwr)741{742u16 trans;743u64 transval;744u64 csbit;745int owned;746int tries;747int sofar;748int addr;749int ret;750unsigned long flags;751const char *op;752753/* Pick appropriate transaction reg and "Chip select" for this serdes */754switch (sdnum) {755case IB_7220_SERDES:756csbit = 1ULL << EPB_IB_UC_CS_SHF;757trans = kr_ibsd_epb_transaction_reg;758break;759760case PCIE_SERDES0:761case PCIE_SERDES1:762/* PCIe SERDES has uC "chip select" in different bit, too */763csbit = 1ULL << EPB_PCIE_UC_CS_SHF;764trans = kr_pciesd_epb_transaction_reg;765break;766767default:768return -1;769}770771op = rd_notwr ? "Rd" : "Wr";772spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);773774owned = epb_access(dd, sdnum, 1);775if (owned < 0) {776spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);777return -1;778}779780/*781* In future code, we may need to distinguish several address ranges,782* and select various memories based on this. For now, just trim783* "loc" (location including address and memory select) to784* "addr" (address within memory). we will only support PRAM785* The memory is 8KB.786*/787addr = loc & 0x1FFF;788for (tries = EPB_TRANS_TRIES; tries; --tries) {789transval = qib_read_kreg32(dd, trans);790if (transval & EPB_TRANS_RDY)791break;792udelay(5);793}794795sofar = 0;796if (tries > 0) {797/*798* Every "memory" access is doubly-indirect.799* We set two bytes of address, then read/write800* one or mores bytes of data.801*/802803/* First, we set control to "Read" or "Write" */804transval = csbit | EPB_UC_CTL |805(rd_notwr ? EPB_ROM_R : EPB_ROM_W);806tries = epb_trans(dd, trans, transval, &transval);807while (tries > 0 && sofar < cnt) {808if (!sofar) {809/* Only set address at start of chunk */810int addrbyte = (addr + sofar) >> 8;811transval = csbit | EPB_MADDRH | addrbyte;812tries = epb_trans(dd, trans, transval,813&transval);814if (tries <= 0)815break;816addrbyte = (addr + sofar) & 0xFF;817transval = csbit | EPB_MADDRL | addrbyte;818tries = epb_trans(dd, trans, transval,819&transval);820if (tries <= 0)821break;822}823824if (rd_notwr)825transval = csbit | EPB_ROMDATA | EPB_RD;826else827transval = csbit | EPB_ROMDATA | buf[sofar];828tries = epb_trans(dd, trans, transval, &transval);829if (tries <= 0)830break;831if (rd_notwr)832buf[sofar] = transval & EPB_DATA_MASK;833++sofar;834}835/* Finally, clear control-bit for Read or Write */836transval = csbit | EPB_UC_CTL;837tries = epb_trans(dd, trans, transval, &transval);838}839840ret = sofar;841/* Release bus. Failure is an error */842if (epb_access(dd, sdnum, -1) < 0)843ret = -1;844845spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);846if (tries <= 0)847ret = -1;848return ret;849}850851#define PROG_CHUNK 64852853static int qib_sd7220_prog_ld(struct qib_devdata *dd, int sdnum,854const u8 *img, int len, int offset)855{856int cnt, sofar, req;857858sofar = 0;859while (sofar < len) {860req = len - sofar;861if (req > PROG_CHUNK)862req = PROG_CHUNK;863cnt = qib_sd7220_ram_xfer(dd, sdnum, offset + sofar,864(u8 *)img + sofar, req, 0);865if (cnt < req) {866sofar = -1;867break;868}869sofar += req;870}871return sofar;872}873874#define VFY_CHUNK 64875#define SD_PRAM_ERROR_LIMIT 42876877static int qib_sd7220_prog_vfy(struct qib_devdata *dd, int sdnum,878const u8 *img, int len, int offset)879{880int cnt, sofar, req, idx, errors;881unsigned char readback[VFY_CHUNK];882883errors = 0;884sofar = 0;885while (sofar < len) {886req = len - sofar;887if (req > VFY_CHUNK)888req = VFY_CHUNK;889cnt = qib_sd7220_ram_xfer(dd, sdnum, sofar + offset,890readback, req, 1);891if (cnt < req) {892/* failed in read itself */893sofar = -1;894break;895}896for (idx = 0; idx < cnt; ++idx) {897if (readback[idx] != img[idx+sofar])898++errors;899}900sofar += cnt;901}902return errors ? -errors : sofar;903}904905static int906qib_sd7220_ib_load(struct qib_devdata *dd, const struct firmware *fw)907{908return qib_sd7220_prog_ld(dd, IB_7220_SERDES, fw->data, fw->size, 0);909}910911static int912qib_sd7220_ib_vfy(struct qib_devdata *dd, const struct firmware *fw)913{914return qib_sd7220_prog_vfy(dd, IB_7220_SERDES, fw->data, fw->size, 0);915}916917/*918* IRQ not set up at this point in init, so we poll.919*/920#define IB_SERDES_TRIM_DONE (1ULL << 11)921#define TRIM_TMO (30)922923static int qib_sd_trimdone_poll(struct qib_devdata *dd)924{925int trim_tmo, ret;926uint64_t val;927928/*929* Default to failure, so IBC will not start930* without IB_SERDES_TRIM_DONE.931*/932ret = 0;933for (trim_tmo = 0; trim_tmo < TRIM_TMO; ++trim_tmo) {934val = qib_read_kreg64(dd, kr_ibcstatus);935if (val & IB_SERDES_TRIM_DONE) {936ret = 1;937break;938}939msleep(10);940}941if (trim_tmo >= TRIM_TMO) {942qib_dev_err(dd, "No TRIMDONE in %d tries\n", trim_tmo);943ret = 0;944}945return ret;946}947948#define TX_FAST_ELT (9)949950/*951* Set the "negotiation" values for SERDES. These are used by the IB1.2952* link negotiation. Macros below are attempt to keep the values a953* little more human-editable.954* First, values related to Drive De-emphasis Settings.955*/956957#define NUM_DDS_REGS 6958#define DDS_REG_MAP 0x76A910 /* LSB-first list of regs (in elt 9) to mod */959960#define DDS_VAL(amp_d, main_d, ipst_d, ipre_d, amp_s, main_s, ipst_s, ipre_s) \961{ { ((amp_d & 0x1F) << 1) | 1, ((amp_s & 0x1F) << 1) | 1, \962(main_d << 3) | 4 | (ipre_d >> 2), \963(main_s << 3) | 4 | (ipre_s >> 2), \964((ipst_d & 0xF) << 1) | ((ipre_d & 3) << 6) | 0x21, \965((ipst_s & 0xF) << 1) | ((ipre_s & 3) << 6) | 0x21 } }966967static struct dds_init {968uint8_t reg_vals[NUM_DDS_REGS];969} dds_init_vals[] = {970/* DDR(FDR) SDR(HDR) */971/* Vendor recommends below for 3m cable */972#define DDS_3M 0973DDS_VAL(31, 19, 12, 0, 29, 22, 9, 0),974DDS_VAL(31, 12, 15, 4, 31, 15, 15, 1),975DDS_VAL(31, 13, 15, 3, 31, 16, 15, 0),976DDS_VAL(31, 14, 15, 2, 31, 17, 14, 0),977DDS_VAL(31, 15, 15, 1, 31, 18, 13, 0),978DDS_VAL(31, 16, 15, 0, 31, 19, 12, 0),979DDS_VAL(31, 17, 14, 0, 31, 20, 11, 0),980DDS_VAL(31, 18, 13, 0, 30, 21, 10, 0),981DDS_VAL(31, 20, 11, 0, 28, 23, 8, 0),982DDS_VAL(31, 21, 10, 0, 27, 24, 7, 0),983DDS_VAL(31, 22, 9, 0, 26, 25, 6, 0),984DDS_VAL(30, 23, 8, 0, 25, 26, 5, 0),985DDS_VAL(29, 24, 7, 0, 23, 27, 4, 0),986/* Vendor recommends below for 1m cable */987#define DDS_1M 13988DDS_VAL(28, 25, 6, 0, 21, 28, 3, 0),989DDS_VAL(27, 26, 5, 0, 19, 29, 2, 0),990DDS_VAL(25, 27, 4, 0, 17, 30, 1, 0)991};992993/*994* Now the RXEQ section of the table.995*/996/* Hardware packs an element number and register address thus: */997#define RXEQ_INIT_RDESC(elt, addr) (((elt) & 0xF) | ((addr) << 4))998#define RXEQ_VAL(elt, adr, val0, val1, val2, val3) \999{RXEQ_INIT_RDESC((elt), (adr)), {(val0), (val1), (val2), (val3)} }10001001#define RXEQ_VAL_ALL(elt, adr, val) \1002{RXEQ_INIT_RDESC((elt), (adr)), {(val), (val), (val), (val)} }10031004#define RXEQ_SDR_DFELTH 01005#define RXEQ_SDR_TLTH 01006#define RXEQ_SDR_G1CNT_Z1CNT 0x111007#define RXEQ_SDR_ZCNT 2310081009static struct rxeq_init {1010u16 rdesc; /* in form used in SerDesDDSRXEQ */1011u8 rdata[4];1012} rxeq_init_vals[] = {1013/* Set Rcv Eq. to Preset node */1014RXEQ_VAL_ALL(7, 0x27, 0x10),1015/* Set DFELTHFDR/HDR thresholds */1016RXEQ_VAL(7, 8, 0, 0, 0, 0), /* FDR, was 0, 1, 2, 3 */1017RXEQ_VAL(7, 0x21, 0, 0, 0, 0), /* HDR */1018/* Set TLTHFDR/HDR theshold */1019RXEQ_VAL(7, 9, 2, 2, 2, 2), /* FDR, was 0, 2, 4, 6 */1020RXEQ_VAL(7, 0x23, 2, 2, 2, 2), /* HDR, was 0, 1, 2, 3 */1021/* Set Preamp setting 2 (ZFR/ZCNT) */1022RXEQ_VAL(7, 0x1B, 12, 12, 12, 12), /* FDR, was 12, 16, 20, 24 */1023RXEQ_VAL(7, 0x1C, 12, 12, 12, 12), /* HDR, was 12, 16, 20, 24 */1024/* Set Preamp DC gain and Setting 1 (GFR/GHR) */1025RXEQ_VAL(7, 0x1E, 16, 16, 16, 16), /* FDR, was 16, 17, 18, 20 */1026RXEQ_VAL(7, 0x1F, 16, 16, 16, 16), /* HDR, was 16, 17, 18, 20 */1027/* Toggle RELOCK (in VCDL_CTRL0) to lock to data */1028RXEQ_VAL_ALL(6, 6, 0x20), /* Set D5 High */1029RXEQ_VAL_ALL(6, 6, 0), /* Set D5 Low */1030};10311032/* There are 17 values from vendor, but IBC only accesses the first 16 */1033#define DDS_ROWS (16)1034#define RXEQ_ROWS ARRAY_SIZE(rxeq_init_vals)10351036static int qib_sd_setvals(struct qib_devdata *dd)1037{1038int idx, midx;1039int min_idx; /* Minimum index for this portion of table */1040uint32_t dds_reg_map;1041u64 __iomem *taddr, *iaddr;1042uint64_t data;1043uint64_t sdctl;10441045taddr = dd->kregbase + kr_serdes_maptable;1046iaddr = dd->kregbase + kr_serdes_ddsrxeq0;10471048/*1049* Init the DDS section of the table.1050* Each "row" of the table provokes NUM_DDS_REG writes, to the1051* registers indicated in DDS_REG_MAP.1052*/1053sdctl = qib_read_kreg64(dd, kr_ibserdesctrl);1054sdctl = (sdctl & ~(0x1f << 8)) | (NUM_DDS_REGS << 8);1055sdctl = (sdctl & ~(0x1f << 13)) | (RXEQ_ROWS << 13);1056qib_write_kreg(dd, kr_ibserdesctrl, sdctl);10571058/*1059* Iterate down table within loop for each register to store.1060*/1061dds_reg_map = DDS_REG_MAP;1062for (idx = 0; idx < NUM_DDS_REGS; ++idx) {1063data = ((dds_reg_map & 0xF) << 4) | TX_FAST_ELT;1064writeq(data, iaddr + idx);1065mmiowb();1066qib_read_kreg32(dd, kr_scratch);1067dds_reg_map >>= 4;1068for (midx = 0; midx < DDS_ROWS; ++midx) {1069u64 __iomem *daddr = taddr + ((midx << 4) + idx);1070data = dds_init_vals[midx].reg_vals[idx];1071writeq(data, daddr);1072mmiowb();1073qib_read_kreg32(dd, kr_scratch);1074} /* End inner for (vals for this reg, each row) */1075} /* end outer for (regs to be stored) */10761077/*1078* Init the RXEQ section of the table.1079* This runs in a different order, as the pattern of1080* register references is more complex, but there are only1081* four "data" values per register.1082*/1083min_idx = idx; /* RXEQ indices pick up where DDS left off */1084taddr += 0x100; /* RXEQ data is in second half of table */1085/* Iterate through RXEQ register addresses */1086for (idx = 0; idx < RXEQ_ROWS; ++idx) {1087int didx; /* "destination" */1088int vidx;10891090/* didx is offset by min_idx to address RXEQ range of regs */1091didx = idx + min_idx;1092/* Store the next RXEQ register address */1093writeq(rxeq_init_vals[idx].rdesc, iaddr + didx);1094mmiowb();1095qib_read_kreg32(dd, kr_scratch);1096/* Iterate through RXEQ values */1097for (vidx = 0; vidx < 4; vidx++) {1098data = rxeq_init_vals[idx].rdata[vidx];1099writeq(data, taddr + (vidx << 6) + idx);1100mmiowb();1101qib_read_kreg32(dd, kr_scratch);1102}1103} /* end outer for (Reg-writes for RXEQ) */1104return 0;1105}11061107#define CMUCTRL5 EPB_LOC(7, 0, 0x15)1108#define RXHSCTRL0(chan) EPB_LOC(chan, 6, 0)1109#define VCDL_DAC2(chan) EPB_LOC(chan, 6, 5)1110#define VCDL_CTRL0(chan) EPB_LOC(chan, 6, 6)1111#define VCDL_CTRL2(chan) EPB_LOC(chan, 6, 8)1112#define START_EQ2(chan) EPB_LOC(chan, 7, 0x28)11131114/*1115* Repeat a "store" across all channels of the IB SerDes.1116* Although nominally it inherits the "read value" of the last1117* channel it modified, the only really useful return is <0 for1118* failure, >= 0 for success. The parameter 'loc' is assumed to1119* be the location in some channel of the register to be modified1120* The caller can specify use of the "gang write" option of EPB,1121* in which case we use the specified channel data for any fields1122* not explicitely written.1123*/1124static int ibsd_mod_allchnls(struct qib_devdata *dd, int loc, int val,1125int mask)1126{1127int ret = -1;1128int chnl;11291130if (loc & EPB_GLOBAL_WR) {1131/*1132* Our caller has assured us that we can set all four1133* channels at once. Trust that. If mask is not 0xFF,1134* we will read the _specified_ channel for our starting1135* value.1136*/1137loc |= (1U << EPB_IB_QUAD0_CS_SHF);1138chnl = (loc >> (4 + EPB_ADDR_SHF)) & 7;1139if (mask != 0xFF) {1140ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,1141loc & ~EPB_GLOBAL_WR, 0, 0);1142if (ret < 0) {1143int sloc = loc >> EPB_ADDR_SHF;11441145qib_dev_err(dd, "pre-read failed: elt %d,"1146" addr 0x%X, chnl %d\n",1147(sloc & 0xF),1148(sloc >> 9) & 0x3f, chnl);1149return ret;1150}1151val = (ret & ~mask) | (val & mask);1152}1153loc &= ~(7 << (4+EPB_ADDR_SHF));1154ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);1155if (ret < 0) {1156int sloc = loc >> EPB_ADDR_SHF;11571158qib_dev_err(dd, "Global WR failed: elt %d,"1159" addr 0x%X, val %02X\n",1160(sloc & 0xF), (sloc >> 9) & 0x3f, val);1161}1162return ret;1163}1164/* Clear "channel" and set CS so we can simply iterate */1165loc &= ~(7 << (4+EPB_ADDR_SHF));1166loc |= (1U << EPB_IB_QUAD0_CS_SHF);1167for (chnl = 0; chnl < 4; ++chnl) {1168int cloc = loc | (chnl << (4+EPB_ADDR_SHF));11691170ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, cloc, val, mask);1171if (ret < 0) {1172int sloc = loc >> EPB_ADDR_SHF;11731174qib_dev_err(dd, "Write failed: elt %d,"1175" addr 0x%X, chnl %d, val 0x%02X,"1176" mask 0x%02X\n",1177(sloc & 0xF), (sloc >> 9) & 0x3f, chnl,1178val & 0xFF, mask & 0xFF);1179break;1180}1181}1182return ret;1183}11841185/*1186* Set the Tx values normally modified by IBC in IB1.2 mode to default1187* values, as gotten from first row of init table.1188*/1189static int set_dds_vals(struct qib_devdata *dd, struct dds_init *ddi)1190{1191int ret;1192int idx, reg, data;1193uint32_t regmap;11941195regmap = DDS_REG_MAP;1196for (idx = 0; idx < NUM_DDS_REGS; ++idx) {1197reg = (regmap & 0xF);1198regmap >>= 4;1199data = ddi->reg_vals[idx];1200/* Vendor says RMW not needed for these regs, use 0xFF mask */1201ret = ibsd_mod_allchnls(dd, EPB_LOC(0, 9, reg), data, 0xFF);1202if (ret < 0)1203break;1204}1205return ret;1206}12071208/*1209* Set the Rx values normally modified by IBC in IB1.2 mode to default1210* values, as gotten from selected column of init table.1211*/1212static int set_rxeq_vals(struct qib_devdata *dd, int vsel)1213{1214int ret;1215int ridx;1216int cnt = ARRAY_SIZE(rxeq_init_vals);12171218for (ridx = 0; ridx < cnt; ++ridx) {1219int elt, reg, val, loc;12201221elt = rxeq_init_vals[ridx].rdesc & 0xF;1222reg = rxeq_init_vals[ridx].rdesc >> 4;1223loc = EPB_LOC(0, elt, reg);1224val = rxeq_init_vals[ridx].rdata[vsel];1225/* mask of 0xFF, because hardware does full-byte store. */1226ret = ibsd_mod_allchnls(dd, loc, val, 0xFF);1227if (ret < 0)1228break;1229}1230return ret;1231}12321233/*1234* Set the default values (row 0) for DDR Driver Demphasis.1235* we do this initially and whenever we turn off IB-1.21236*1237* The "default" values for Rx equalization are also stored to1238* SerDes registers. Formerly (and still default), we used set 2.1239* For experimenting with cables and link-partners, we allow changing1240* that via a module parameter.1241*/1242static unsigned qib_rxeq_set = 2;1243module_param_named(rxeq_default_set, qib_rxeq_set, uint,1244S_IWUSR | S_IRUGO);1245MODULE_PARM_DESC(rxeq_default_set,1246"Which set [0..3] of Rx Equalization values is default");12471248static int qib_internal_presets(struct qib_devdata *dd)1249{1250int ret = 0;12511252ret = set_dds_vals(dd, dds_init_vals + DDS_3M);12531254if (ret < 0)1255qib_dev_err(dd, "Failed to set default DDS values\n");1256ret = set_rxeq_vals(dd, qib_rxeq_set & 3);1257if (ret < 0)1258qib_dev_err(dd, "Failed to set default RXEQ values\n");1259return ret;1260}12611262int qib_sd7220_presets(struct qib_devdata *dd)1263{1264int ret = 0;12651266if (!dd->cspec->presets_needed)1267return ret;1268dd->cspec->presets_needed = 0;1269/* Assert uC reset, so we don't clash with it. */1270qib_ibsd_reset(dd, 1);1271udelay(2);1272qib_sd_trimdone_monitor(dd, "link-down");12731274ret = qib_internal_presets(dd);1275return ret;1276}12771278static int qib_sd_trimself(struct qib_devdata *dd, int val)1279{1280int loc = CMUCTRL5 | (1U << EPB_IB_QUAD0_CS_SHF);12811282return qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);1283}12841285static int qib_sd_early(struct qib_devdata *dd)1286{1287int ret;12881289ret = ibsd_mod_allchnls(dd, RXHSCTRL0(0) | EPB_GLOBAL_WR, 0xD4, 0xFF);1290if (ret < 0)1291goto bail;1292ret = ibsd_mod_allchnls(dd, START_EQ1(0) | EPB_GLOBAL_WR, 0x10, 0xFF);1293if (ret < 0)1294goto bail;1295ret = ibsd_mod_allchnls(dd, START_EQ2(0) | EPB_GLOBAL_WR, 0x30, 0xFF);1296bail:1297return ret;1298}12991300#define BACTRL(chnl) EPB_LOC(chnl, 6, 0x0E)1301#define LDOUTCTRL1(chnl) EPB_LOC(chnl, 7, 6)1302#define RXHSSTATUS(chnl) EPB_LOC(chnl, 6, 0xF)13031304static int qib_sd_dactrim(struct qib_devdata *dd)1305{1306int ret;13071308ret = ibsd_mod_allchnls(dd, VCDL_DAC2(0) | EPB_GLOBAL_WR, 0x2D, 0xFF);1309if (ret < 0)1310goto bail;13111312/* more fine-tuning of what will be default */1313ret = ibsd_mod_allchnls(dd, VCDL_CTRL2(0), 3, 0xF);1314if (ret < 0)1315goto bail;13161317ret = ibsd_mod_allchnls(dd, BACTRL(0) | EPB_GLOBAL_WR, 0x40, 0xFF);1318if (ret < 0)1319goto bail;13201321ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x04, 0xFF);1322if (ret < 0)1323goto bail;13241325ret = ibsd_mod_allchnls(dd, RXHSSTATUS(0) | EPB_GLOBAL_WR, 0x04, 0xFF);1326if (ret < 0)1327goto bail;13281329/*1330* Delay for max possible number of steps, with slop.1331* Each step is about 4usec.1332*/1333udelay(415);13341335ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x00, 0xFF);13361337bail:1338return ret;1339}13401341#define RELOCK_FIRST_MS 31342#define RXLSPPM(chan) EPB_LOC(chan, 0, 2)1343void toggle_7220_rclkrls(struct qib_devdata *dd)1344{1345int loc = RXLSPPM(0) | EPB_GLOBAL_WR;1346int ret;13471348ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);1349if (ret < 0)1350qib_dev_err(dd, "RCLKRLS failed to clear D7\n");1351else {1352udelay(1);1353ibsd_mod_allchnls(dd, loc, 0x80, 0x80);1354}1355/* And again for good measure */1356udelay(1);1357ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);1358if (ret < 0)1359qib_dev_err(dd, "RCLKRLS failed to clear D7\n");1360else {1361udelay(1);1362ibsd_mod_allchnls(dd, loc, 0x80, 0x80);1363}1364/* Now reset xgxs and IBC to complete the recovery */1365dd->f_xgxs_reset(dd->pport);1366}13671368/*1369* Shut down the timer that polls for relock occasions, if needed1370* this is "hooked" from qib_7220_quiet_serdes(), which is called1371* just before qib_shutdown_device() in qib_driver.c shuts down all1372* the other timers1373*/1374void shutdown_7220_relock_poll(struct qib_devdata *dd)1375{1376if (dd->cspec->relock_timer_active)1377del_timer_sync(&dd->cspec->relock_timer);1378}13791380static unsigned qib_relock_by_timer = 1;1381module_param_named(relock_by_timer, qib_relock_by_timer, uint,1382S_IWUSR | S_IRUGO);1383MODULE_PARM_DESC(relock_by_timer, "Allow relock attempt if link not up");13841385static void qib_run_relock(unsigned long opaque)1386{1387struct qib_devdata *dd = (struct qib_devdata *)opaque;1388struct qib_pportdata *ppd = dd->pport;1389struct qib_chip_specific *cs = dd->cspec;1390int timeoff;13911392/*1393* Check link-training state for "stuck" state, when down.1394* if found, try relock and schedule another try at1395* exponentially growing delay, maxed at one second.1396* if not stuck, our work is done.1397*/1398if ((dd->flags & QIB_INITTED) && !(ppd->lflags &1399(QIBL_IB_AUTONEG_INPROG | QIBL_LINKINIT | QIBL_LINKARMED |1400QIBL_LINKACTIVE))) {1401if (qib_relock_by_timer) {1402if (!(ppd->lflags & QIBL_IB_LINK_DISABLED))1403toggle_7220_rclkrls(dd);1404}1405/* re-set timer for next check */1406timeoff = cs->relock_interval << 1;1407if (timeoff > HZ)1408timeoff = HZ;1409cs->relock_interval = timeoff;1410} else1411timeoff = HZ;1412mod_timer(&cs->relock_timer, jiffies + timeoff);1413}14141415void set_7220_relock_poll(struct qib_devdata *dd, int ibup)1416{1417struct qib_chip_specific *cs = dd->cspec;14181419if (ibup) {1420/* We are now up, relax timer to 1 second interval */1421if (cs->relock_timer_active) {1422cs->relock_interval = HZ;1423mod_timer(&cs->relock_timer, jiffies + HZ);1424}1425} else {1426/* Transition to down, (re-)set timer to short interval. */1427unsigned int timeout;14281429timeout = msecs_to_jiffies(RELOCK_FIRST_MS);1430if (timeout == 0)1431timeout = 1;1432/* If timer has not yet been started, do so. */1433if (!cs->relock_timer_active) {1434cs->relock_timer_active = 1;1435init_timer(&cs->relock_timer);1436cs->relock_timer.function = qib_run_relock;1437cs->relock_timer.data = (unsigned long) dd;1438cs->relock_interval = timeout;1439cs->relock_timer.expires = jiffies + timeout;1440add_timer(&cs->relock_timer);1441} else {1442cs->relock_interval = timeout;1443mod_timer(&cs->relock_timer, jiffies + timeout);1444}1445}1446}144714481449