18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2013 Intel Corporation. All rights reserved.
38c2ecf20Sopenharmony_ci * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
48c2ecf20Sopenharmony_ci * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
78c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
88c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
98c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
108c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
138c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
148c2ecf20Sopenharmony_ci *     conditions are met:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
178c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
188c2ecf20Sopenharmony_ci *        disclaimer.
198c2ecf20Sopenharmony_ci *
208c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
218c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
228c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
238c2ecf20Sopenharmony_ci *        provided with the distribution.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
268c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
278c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
288c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
298c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
308c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
318c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
328c2ecf20Sopenharmony_ci * SOFTWARE.
338c2ecf20Sopenharmony_ci */
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * This file contains all of the code that is specific to the SerDes
368c2ecf20Sopenharmony_ci * on the QLogic_IB 7220 chip.
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include <linux/pci.h>
408c2ecf20Sopenharmony_ci#include <linux/delay.h>
418c2ecf20Sopenharmony_ci#include <linux/module.h>
428c2ecf20Sopenharmony_ci#include <linux/firmware.h>
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#include "qib.h"
458c2ecf20Sopenharmony_ci#include "qib_7220.h"
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define SD7220_FW_NAME "qlogic/sd7220.fw"
488c2ecf20Sopenharmony_ciMODULE_FIRMWARE(SD7220_FW_NAME);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/*
518c2ecf20Sopenharmony_ci * Same as in qib_iba7220.c, but just the registers needed here.
528c2ecf20Sopenharmony_ci * Could move whole set to qib_7220.h, but decided better to keep
538c2ecf20Sopenharmony_ci * local.
548c2ecf20Sopenharmony_ci */
558c2ecf20Sopenharmony_ci#define KREG_IDX(regname) (QIB_7220_##regname##_OFFS / sizeof(u64))
568c2ecf20Sopenharmony_ci#define kr_hwerrclear KREG_IDX(HwErrClear)
578c2ecf20Sopenharmony_ci#define kr_hwerrmask KREG_IDX(HwErrMask)
588c2ecf20Sopenharmony_ci#define kr_hwerrstatus KREG_IDX(HwErrStatus)
598c2ecf20Sopenharmony_ci#define kr_ibcstatus KREG_IDX(IBCStatus)
608c2ecf20Sopenharmony_ci#define kr_ibserdesctrl KREG_IDX(IBSerDesCtrl)
618c2ecf20Sopenharmony_ci#define kr_scratch KREG_IDX(Scratch)
628c2ecf20Sopenharmony_ci#define kr_xgxs_cfg KREG_IDX(XGXSCfg)
638c2ecf20Sopenharmony_ci/* these are used only here, not in qib_iba7220.c */
648c2ecf20Sopenharmony_ci#define kr_ibsd_epb_access_ctrl KREG_IDX(ibsd_epb_access_ctrl)
658c2ecf20Sopenharmony_ci#define kr_ibsd_epb_transaction_reg KREG_IDX(ibsd_epb_transaction_reg)
668c2ecf20Sopenharmony_ci#define kr_pciesd_epb_transaction_reg KREG_IDX(pciesd_epb_transaction_reg)
678c2ecf20Sopenharmony_ci#define kr_pciesd_epb_access_ctrl KREG_IDX(pciesd_epb_access_ctrl)
688c2ecf20Sopenharmony_ci#define kr_serdes_ddsrxeq0 KREG_IDX(SerDes_DDSRXEQ0)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * The IBSerDesMappTable is a memory that holds values to be stored in
728c2ecf20Sopenharmony_ci * various SerDes registers by IBC.
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_ci#define kr_serdes_maptable KREG_IDX(IBSerDesMappTable)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/*
778c2ecf20Sopenharmony_ci * Below used for sdnum parameter, selecting one of the two sections
788c2ecf20Sopenharmony_ci * used for PCIe, or the single SerDes used for IB.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_ci#define PCIE_SERDES0 0
818c2ecf20Sopenharmony_ci#define PCIE_SERDES1 1
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/*
848c2ecf20Sopenharmony_ci * The EPB requires addressing in a particular form. EPB_LOC() is intended
858c2ecf20Sopenharmony_ci * to make #definitions a little more readable.
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_ci#define EPB_ADDR_SHF 8
888c2ecf20Sopenharmony_ci#define EPB_LOC(chn, elt, reg) \
898c2ecf20Sopenharmony_ci	(((elt & 0xf) | ((chn & 7) << 4) | ((reg & 0x3f) << 9)) << \
908c2ecf20Sopenharmony_ci	 EPB_ADDR_SHF)
918c2ecf20Sopenharmony_ci#define EPB_IB_QUAD0_CS_SHF (25)
928c2ecf20Sopenharmony_ci#define EPB_IB_QUAD0_CS (1U <<  EPB_IB_QUAD0_CS_SHF)
938c2ecf20Sopenharmony_ci#define EPB_IB_UC_CS_SHF (26)
948c2ecf20Sopenharmony_ci#define EPB_PCIE_UC_CS_SHF (27)
958c2ecf20Sopenharmony_ci#define EPB_GLOBAL_WR (1U << (EPB_ADDR_SHF + 8))
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/* Forward declarations. */
988c2ecf20Sopenharmony_cistatic int qib_sd7220_reg_mod(struct qib_devdata *dd, int sdnum, u32 loc,
998c2ecf20Sopenharmony_ci			      u32 data, u32 mask);
1008c2ecf20Sopenharmony_cistatic int ibsd_mod_allchnls(struct qib_devdata *dd, int loc, int val,
1018c2ecf20Sopenharmony_ci			     int mask);
1028c2ecf20Sopenharmony_cistatic int qib_sd_trimdone_poll(struct qib_devdata *dd);
1038c2ecf20Sopenharmony_cistatic void qib_sd_trimdone_monitor(struct qib_devdata *dd, const char *where);
1048c2ecf20Sopenharmony_cistatic int qib_sd_setvals(struct qib_devdata *dd);
1058c2ecf20Sopenharmony_cistatic int qib_sd_early(struct qib_devdata *dd);
1068c2ecf20Sopenharmony_cistatic int qib_sd_dactrim(struct qib_devdata *dd);
1078c2ecf20Sopenharmony_cistatic int qib_internal_presets(struct qib_devdata *dd);
1088c2ecf20Sopenharmony_ci/* Tweak the register (CMUCTRL5) that contains the TRIMSELF controls */
1098c2ecf20Sopenharmony_cistatic int qib_sd_trimself(struct qib_devdata *dd, int val);
1108c2ecf20Sopenharmony_cistatic int epb_access(struct qib_devdata *dd, int sdnum, int claim);
1118c2ecf20Sopenharmony_cistatic int qib_sd7220_ib_load(struct qib_devdata *dd,
1128c2ecf20Sopenharmony_ci			      const struct firmware *fw);
1138c2ecf20Sopenharmony_cistatic int qib_sd7220_ib_vfy(struct qib_devdata *dd,
1148c2ecf20Sopenharmony_ci			     const struct firmware *fw);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/*
1178c2ecf20Sopenharmony_ci * Below keeps track of whether the "once per power-on" initialization has
1188c2ecf20Sopenharmony_ci * been done, because uC code Version 1.32.17 or higher allows the uC to
1198c2ecf20Sopenharmony_ci * be reset at will, and Automatic Equalization may require it. So the
1208c2ecf20Sopenharmony_ci * state of the reset "pin", is no longer valid. Instead, we check for the
1218c2ecf20Sopenharmony_ci * actual uC code having been loaded.
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_cistatic int qib_ibsd_ucode_loaded(struct qib_pportdata *ppd,
1248c2ecf20Sopenharmony_ci				 const struct firmware *fw)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct qib_devdata *dd = ppd->dd;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (!dd->cspec->serdes_first_init_done &&
1298c2ecf20Sopenharmony_ci	    qib_sd7220_ib_vfy(dd, fw) > 0)
1308c2ecf20Sopenharmony_ci		dd->cspec->serdes_first_init_done = 1;
1318c2ecf20Sopenharmony_ci	return dd->cspec->serdes_first_init_done;
1328c2ecf20Sopenharmony_ci}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/* repeat #define for local use. "Real" #define is in qib_iba7220.c */
1358c2ecf20Sopenharmony_ci#define QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR      0x0000004000000000ULL
1368c2ecf20Sopenharmony_ci#define IB_MPREG5 (EPB_LOC(6, 0, 0xE) | (1L << EPB_IB_UC_CS_SHF))
1378c2ecf20Sopenharmony_ci#define IB_MPREG6 (EPB_LOC(6, 0, 0xF) | (1U << EPB_IB_UC_CS_SHF))
1388c2ecf20Sopenharmony_ci#define UC_PAR_CLR_D 8
1398c2ecf20Sopenharmony_ci#define UC_PAR_CLR_M 0xC
1408c2ecf20Sopenharmony_ci#define IB_CTRL2(chn) (EPB_LOC(chn, 7, 3) | EPB_IB_QUAD0_CS)
1418c2ecf20Sopenharmony_ci#define START_EQ1(chan) EPB_LOC(chan, 7, 0x27)
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_civoid qib_sd7220_clr_ibpar(struct qib_devdata *dd)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	int ret;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* clear, then re-enable parity errs */
1488c2ecf20Sopenharmony_ci	ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6,
1498c2ecf20Sopenharmony_ci		UC_PAR_CLR_D, UC_PAR_CLR_M);
1508c2ecf20Sopenharmony_ci	if (ret < 0) {
1518c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed clearing IBSerDes Parity err\n");
1528c2ecf20Sopenharmony_ci		goto bail;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci	ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0,
1558c2ecf20Sopenharmony_ci		UC_PAR_CLR_M);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	qib_read_kreg32(dd, kr_scratch);
1588c2ecf20Sopenharmony_ci	udelay(4);
1598c2ecf20Sopenharmony_ci	qib_write_kreg(dd, kr_hwerrclear,
1608c2ecf20Sopenharmony_ci		QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);
1618c2ecf20Sopenharmony_ci	qib_read_kreg32(dd, kr_scratch);
1628c2ecf20Sopenharmony_cibail:
1638c2ecf20Sopenharmony_ci	return;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/*
1678c2ecf20Sopenharmony_ci * After a reset or other unusual event, the epb interface may need
1688c2ecf20Sopenharmony_ci * to be re-synchronized, between the host and the uC.
1698c2ecf20Sopenharmony_ci * returns <0 for failure to resync within IBSD_RESYNC_TRIES (not expected)
1708c2ecf20Sopenharmony_ci */
1718c2ecf20Sopenharmony_ci#define IBSD_RESYNC_TRIES 3
1728c2ecf20Sopenharmony_ci#define IB_PGUDP(chn) (EPB_LOC((chn), 2, 1) | EPB_IB_QUAD0_CS)
1738c2ecf20Sopenharmony_ci#define IB_CMUDONE(chn) (EPB_LOC((chn), 7, 0xF) | EPB_IB_QUAD0_CS)
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic int qib_resync_ibepb(struct qib_devdata *dd)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	int ret, pat, tries, chn;
1788c2ecf20Sopenharmony_ci	u32 loc;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	ret = -1;
1818c2ecf20Sopenharmony_ci	chn = 0;
1828c2ecf20Sopenharmony_ci	for (tries = 0; tries < (4 * IBSD_RESYNC_TRIES); ++tries) {
1838c2ecf20Sopenharmony_ci		loc = IB_PGUDP(chn);
1848c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);
1858c2ecf20Sopenharmony_ci		if (ret < 0) {
1868c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed read in resync\n");
1878c2ecf20Sopenharmony_ci			continue;
1888c2ecf20Sopenharmony_ci		}
1898c2ecf20Sopenharmony_ci		if (ret != 0xF0 && ret != 0x55 && tries == 0)
1908c2ecf20Sopenharmony_ci			qib_dev_err(dd, "unexpected pattern in resync\n");
1918c2ecf20Sopenharmony_ci		pat = ret ^ 0xA5; /* alternate F0 and 55 */
1928c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, pat, 0xFF);
1938c2ecf20Sopenharmony_ci		if (ret < 0) {
1948c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed write in resync\n");
1958c2ecf20Sopenharmony_ci			continue;
1968c2ecf20Sopenharmony_ci		}
1978c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);
1988c2ecf20Sopenharmony_ci		if (ret < 0) {
1998c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed re-read in resync\n");
2008c2ecf20Sopenharmony_ci			continue;
2018c2ecf20Sopenharmony_ci		}
2028c2ecf20Sopenharmony_ci		if (ret != pat) {
2038c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed compare1 in resync\n");
2048c2ecf20Sopenharmony_ci			continue;
2058c2ecf20Sopenharmony_ci		}
2068c2ecf20Sopenharmony_ci		loc = IB_CMUDONE(chn);
2078c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, 0, 0);
2088c2ecf20Sopenharmony_ci		if (ret < 0) {
2098c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed CMUDONE rd in resync\n");
2108c2ecf20Sopenharmony_ci			continue;
2118c2ecf20Sopenharmony_ci		}
2128c2ecf20Sopenharmony_ci		if ((ret & 0x70) != ((chn << 4) | 0x40)) {
2138c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Bad CMUDONE value %02X, chn %d\n",
2148c2ecf20Sopenharmony_ci				    ret, chn);
2158c2ecf20Sopenharmony_ci			continue;
2168c2ecf20Sopenharmony_ci		}
2178c2ecf20Sopenharmony_ci		if (++chn == 4)
2188c2ecf20Sopenharmony_ci			break;  /* Success */
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci	return (ret > 0) ? 0 : ret;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci/*
2248c2ecf20Sopenharmony_ci * Localize the stuff that should be done to change IB uC reset
2258c2ecf20Sopenharmony_ci * returns <0 for errors.
2268c2ecf20Sopenharmony_ci */
2278c2ecf20Sopenharmony_cistatic int qib_ibsd_reset(struct qib_devdata *dd, int assert_rst)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	u64 rst_val;
2308c2ecf20Sopenharmony_ci	int ret = 0;
2318c2ecf20Sopenharmony_ci	unsigned long flags;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	rst_val = qib_read_kreg64(dd, kr_ibserdesctrl);
2348c2ecf20Sopenharmony_ci	if (assert_rst) {
2358c2ecf20Sopenharmony_ci		/*
2368c2ecf20Sopenharmony_ci		 * Vendor recommends "interrupting" uC before reset, to
2378c2ecf20Sopenharmony_ci		 * minimize possible glitches.
2388c2ecf20Sopenharmony_ci		 */
2398c2ecf20Sopenharmony_ci		spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
2408c2ecf20Sopenharmony_ci		epb_access(dd, IB_7220_SERDES, 1);
2418c2ecf20Sopenharmony_ci		rst_val |= 1ULL;
2428c2ecf20Sopenharmony_ci		/* Squelch possible parity error from _asserting_ reset */
2438c2ecf20Sopenharmony_ci		qib_write_kreg(dd, kr_hwerrmask,
2448c2ecf20Sopenharmony_ci			       dd->cspec->hwerrmask &
2458c2ecf20Sopenharmony_ci			       ~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);
2468c2ecf20Sopenharmony_ci		qib_write_kreg(dd, kr_ibserdesctrl, rst_val);
2478c2ecf20Sopenharmony_ci		/* flush write, delay to ensure it took effect */
2488c2ecf20Sopenharmony_ci		qib_read_kreg32(dd, kr_scratch);
2498c2ecf20Sopenharmony_ci		udelay(2);
2508c2ecf20Sopenharmony_ci		/* once it's reset, can remove interrupt */
2518c2ecf20Sopenharmony_ci		epb_access(dd, IB_7220_SERDES, -1);
2528c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
2538c2ecf20Sopenharmony_ci	} else {
2548c2ecf20Sopenharmony_ci		/*
2558c2ecf20Sopenharmony_ci		 * Before we de-assert reset, we need to deal with
2568c2ecf20Sopenharmony_ci		 * possible glitch on the Parity-error line.
2578c2ecf20Sopenharmony_ci		 * Suppress it around the reset, both in chip-level
2588c2ecf20Sopenharmony_ci		 * hwerrmask and in IB uC control reg. uC will allow
2598c2ecf20Sopenharmony_ci		 * it again during startup.
2608c2ecf20Sopenharmony_ci		 */
2618c2ecf20Sopenharmony_ci		u64 val;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci		rst_val &= ~(1ULL);
2648c2ecf20Sopenharmony_ci		qib_write_kreg(dd, kr_hwerrmask,
2658c2ecf20Sopenharmony_ci			       dd->cspec->hwerrmask &
2668c2ecf20Sopenharmony_ci			       ~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR);
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci		ret = qib_resync_ibepb(dd);
2698c2ecf20Sopenharmony_ci		if (ret < 0)
2708c2ecf20Sopenharmony_ci			qib_dev_err(dd, "unable to re-sync IB EPB\n");
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		/* set uC control regs to suppress parity errs */
2738c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG5, 1, 1);
2748c2ecf20Sopenharmony_ci		if (ret < 0)
2758c2ecf20Sopenharmony_ci			goto bail;
2768c2ecf20Sopenharmony_ci		/* IB uC code past Version 1.32.17 allow suppression of wdog */
2778c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0x80,
2788c2ecf20Sopenharmony_ci			0x80);
2798c2ecf20Sopenharmony_ci		if (ret < 0) {
2808c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed to set WDOG disable\n");
2818c2ecf20Sopenharmony_ci			goto bail;
2828c2ecf20Sopenharmony_ci		}
2838c2ecf20Sopenharmony_ci		qib_write_kreg(dd, kr_ibserdesctrl, rst_val);
2848c2ecf20Sopenharmony_ci		/* flush write, delay for startup */
2858c2ecf20Sopenharmony_ci		qib_read_kreg32(dd, kr_scratch);
2868c2ecf20Sopenharmony_ci		udelay(1);
2878c2ecf20Sopenharmony_ci		/* clear, then re-enable parity errs */
2888c2ecf20Sopenharmony_ci		qib_sd7220_clr_ibpar(dd);
2898c2ecf20Sopenharmony_ci		val = qib_read_kreg64(dd, kr_hwerrstatus);
2908c2ecf20Sopenharmony_ci		if (val & QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR) {
2918c2ecf20Sopenharmony_ci			qib_dev_err(dd, "IBUC Parity still set after RST\n");
2928c2ecf20Sopenharmony_ci			dd->cspec->hwerrmask &=
2938c2ecf20Sopenharmony_ci				~QLOGIC_IB_HWE_IB_UC_MEMORYPARITYERR;
2948c2ecf20Sopenharmony_ci		}
2958c2ecf20Sopenharmony_ci		qib_write_kreg(dd, kr_hwerrmask,
2968c2ecf20Sopenharmony_ci			dd->cspec->hwerrmask);
2978c2ecf20Sopenharmony_ci	}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cibail:
3008c2ecf20Sopenharmony_ci	return ret;
3018c2ecf20Sopenharmony_ci}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_cistatic void qib_sd_trimdone_monitor(struct qib_devdata *dd,
3048c2ecf20Sopenharmony_ci	const char *where)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	int ret, chn, baduns;
3078c2ecf20Sopenharmony_ci	u64 val;
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	if (!where)
3108c2ecf20Sopenharmony_ci		where = "?";
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* give time for reset to settle out in EPB */
3138c2ecf20Sopenharmony_ci	udelay(2);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	ret = qib_resync_ibepb(dd);
3168c2ecf20Sopenharmony_ci	if (ret < 0)
3178c2ecf20Sopenharmony_ci		qib_dev_err(dd, "not able to re-sync IB EPB (%s)\n", where);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	/* Do "sacrificial read" to get EPB in sane state after reset */
3208c2ecf20Sopenharmony_ci	ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_CTRL2(0), 0, 0);
3218c2ecf20Sopenharmony_ci	if (ret < 0)
3228c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed TRIMDONE 1st read, (%s)\n", where);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	/* Check/show "summary" Trim-done bit in IBCStatus */
3258c2ecf20Sopenharmony_ci	val = qib_read_kreg64(dd, kr_ibcstatus);
3268c2ecf20Sopenharmony_ci	if (!(val & (1ULL << 11)))
3278c2ecf20Sopenharmony_ci		qib_dev_err(dd, "IBCS TRIMDONE clear (%s)\n", where);
3288c2ecf20Sopenharmony_ci	/*
3298c2ecf20Sopenharmony_ci	 * Do "dummy read/mod/wr" to get EPB in sane state after reset
3308c2ecf20Sopenharmony_ci	 * The default value for MPREG6 is 0.
3318c2ecf20Sopenharmony_ci	 */
3328c2ecf20Sopenharmony_ci	udelay(2);
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, IB_MPREG6, 0x80, 0x80);
3358c2ecf20Sopenharmony_ci	if (ret < 0)
3368c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed Dummy RMW, (%s)\n", where);
3378c2ecf20Sopenharmony_ci	udelay(10);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	baduns = 0;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	for (chn = 3; chn >= 0; --chn) {
3428c2ecf20Sopenharmony_ci		/* Read CTRL reg for each channel to check TRIMDONE */
3438c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
3448c2ecf20Sopenharmony_ci			IB_CTRL2(chn), 0, 0);
3458c2ecf20Sopenharmony_ci		if (ret < 0)
3468c2ecf20Sopenharmony_ci			qib_dev_err(dd,
3478c2ecf20Sopenharmony_ci				"Failed checking TRIMDONE, chn %d (%s)\n",
3488c2ecf20Sopenharmony_ci				chn, where);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci		if (!(ret & 0x10)) {
3518c2ecf20Sopenharmony_ci			int probe;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci			baduns |= (1 << chn);
3548c2ecf20Sopenharmony_ci			qib_dev_err(dd,
3558c2ecf20Sopenharmony_ci				"TRIMDONE cleared on chn %d (%02X). (%s)\n",
3568c2ecf20Sopenharmony_ci				chn, ret, where);
3578c2ecf20Sopenharmony_ci			probe = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
3588c2ecf20Sopenharmony_ci				IB_PGUDP(0), 0, 0);
3598c2ecf20Sopenharmony_ci			qib_dev_err(dd, "probe is %d (%02X)\n",
3608c2ecf20Sopenharmony_ci				probe, probe);
3618c2ecf20Sopenharmony_ci			probe = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
3628c2ecf20Sopenharmony_ci				IB_CTRL2(chn), 0, 0);
3638c2ecf20Sopenharmony_ci			qib_dev_err(dd, "re-read: %d (%02X)\n",
3648c2ecf20Sopenharmony_ci				probe, probe);
3658c2ecf20Sopenharmony_ci			ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
3668c2ecf20Sopenharmony_ci				IB_CTRL2(chn), 0x10, 0x10);
3678c2ecf20Sopenharmony_ci			if (ret < 0)
3688c2ecf20Sopenharmony_ci				qib_dev_err(dd,
3698c2ecf20Sopenharmony_ci					"Err on TRIMDONE rewrite1\n");
3708c2ecf20Sopenharmony_ci		}
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci	for (chn = 3; chn >= 0; --chn) {
3738c2ecf20Sopenharmony_ci		/* Read CTRL reg for each channel to check TRIMDONE */
3748c2ecf20Sopenharmony_ci		if (baduns & (1 << chn)) {
3758c2ecf20Sopenharmony_ci			qib_dev_err(dd,
3768c2ecf20Sopenharmony_ci				"Resetting TRIMDONE on chn %d (%s)\n",
3778c2ecf20Sopenharmony_ci				chn, where);
3788c2ecf20Sopenharmony_ci			ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
3798c2ecf20Sopenharmony_ci				IB_CTRL2(chn), 0x10, 0x10);
3808c2ecf20Sopenharmony_ci			if (ret < 0)
3818c2ecf20Sopenharmony_ci				qib_dev_err(dd,
3828c2ecf20Sopenharmony_ci					"Failed re-setting TRIMDONE, chn %d (%s)\n",
3838c2ecf20Sopenharmony_ci					chn, where);
3848c2ecf20Sopenharmony_ci		}
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci/*
3898c2ecf20Sopenharmony_ci * Below is portion of IBA7220-specific bringup_serdes() that actually
3908c2ecf20Sopenharmony_ci * deals with registers and memory within the SerDes itself.
3918c2ecf20Sopenharmony_ci * Post IB uC code version 1.32.17, was_reset being 1 is not really
3928c2ecf20Sopenharmony_ci * informative, so we double-check.
3938c2ecf20Sopenharmony_ci */
3948c2ecf20Sopenharmony_ciint qib_sd7220_init(struct qib_devdata *dd)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	const struct firmware *fw;
3978c2ecf20Sopenharmony_ci	int ret = 1; /* default to failure */
3988c2ecf20Sopenharmony_ci	int first_reset, was_reset;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	/* SERDES MPU reset recorded in D0 */
4018c2ecf20Sopenharmony_ci	was_reset = (qib_read_kreg64(dd, kr_ibserdesctrl) & 1);
4028c2ecf20Sopenharmony_ci	if (!was_reset) {
4038c2ecf20Sopenharmony_ci		/* entered with reset not asserted, we need to do it */
4048c2ecf20Sopenharmony_ci		qib_ibsd_reset(dd, 1);
4058c2ecf20Sopenharmony_ci		qib_sd_trimdone_monitor(dd, "Driver-reload");
4068c2ecf20Sopenharmony_ci	}
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	ret = request_firmware(&fw, SD7220_FW_NAME, &dd->pcidev->dev);
4098c2ecf20Sopenharmony_ci	if (ret) {
4108c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed to load IB SERDES image\n");
4118c2ecf20Sopenharmony_ci		goto done;
4128c2ecf20Sopenharmony_ci	}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* Substitute our deduced value for was_reset */
4158c2ecf20Sopenharmony_ci	ret = qib_ibsd_ucode_loaded(dd->pport, fw);
4168c2ecf20Sopenharmony_ci	if (ret < 0)
4178c2ecf20Sopenharmony_ci		goto bail;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	first_reset = !ret; /* First reset if IBSD uCode not yet loaded */
4208c2ecf20Sopenharmony_ci	/*
4218c2ecf20Sopenharmony_ci	 * Alter some regs per vendor latest doc, reset-defaults
4228c2ecf20Sopenharmony_ci	 * are not right for IB.
4238c2ecf20Sopenharmony_ci	 */
4248c2ecf20Sopenharmony_ci	ret = qib_sd_early(dd);
4258c2ecf20Sopenharmony_ci	if (ret < 0) {
4268c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed to set IB SERDES early defaults\n");
4278c2ecf20Sopenharmony_ci		goto bail;
4288c2ecf20Sopenharmony_ci	}
4298c2ecf20Sopenharmony_ci	/*
4308c2ecf20Sopenharmony_ci	 * Set DAC manual trim IB.
4318c2ecf20Sopenharmony_ci	 * We only do this once after chip has been reset (usually
4328c2ecf20Sopenharmony_ci	 * same as once per system boot).
4338c2ecf20Sopenharmony_ci	 */
4348c2ecf20Sopenharmony_ci	if (first_reset) {
4358c2ecf20Sopenharmony_ci		ret = qib_sd_dactrim(dd);
4368c2ecf20Sopenharmony_ci		if (ret < 0) {
4378c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed IB SERDES DAC trim\n");
4388c2ecf20Sopenharmony_ci			goto bail;
4398c2ecf20Sopenharmony_ci		}
4408c2ecf20Sopenharmony_ci	}
4418c2ecf20Sopenharmony_ci	/*
4428c2ecf20Sopenharmony_ci	 * Set various registers (DDS and RXEQ) that will be
4438c2ecf20Sopenharmony_ci	 * controlled by IBC (in 1.2 mode) to reasonable preset values
4448c2ecf20Sopenharmony_ci	 * Calling the "internal" version avoids the "check for needed"
4458c2ecf20Sopenharmony_ci	 * and "trimdone monitor" that might be counter-productive.
4468c2ecf20Sopenharmony_ci	 */
4478c2ecf20Sopenharmony_ci	ret = qib_internal_presets(dd);
4488c2ecf20Sopenharmony_ci	if (ret < 0) {
4498c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed to set IB SERDES presets\n");
4508c2ecf20Sopenharmony_ci		goto bail;
4518c2ecf20Sopenharmony_ci	}
4528c2ecf20Sopenharmony_ci	ret = qib_sd_trimself(dd, 0x80);
4538c2ecf20Sopenharmony_ci	if (ret < 0) {
4548c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed to set IB SERDES TRIMSELF\n");
4558c2ecf20Sopenharmony_ci		goto bail;
4568c2ecf20Sopenharmony_ci	}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	/* Load image, then try to verify */
4598c2ecf20Sopenharmony_ci	ret = 0;        /* Assume success */
4608c2ecf20Sopenharmony_ci	if (first_reset) {
4618c2ecf20Sopenharmony_ci		int vfy;
4628c2ecf20Sopenharmony_ci		int trim_done;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci		ret = qib_sd7220_ib_load(dd, fw);
4658c2ecf20Sopenharmony_ci		if (ret < 0) {
4668c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed to load IB SERDES image\n");
4678c2ecf20Sopenharmony_ci			goto bail;
4688c2ecf20Sopenharmony_ci		} else {
4698c2ecf20Sopenharmony_ci			/* Loaded image, try to verify */
4708c2ecf20Sopenharmony_ci			vfy = qib_sd7220_ib_vfy(dd, fw);
4718c2ecf20Sopenharmony_ci			if (vfy != ret) {
4728c2ecf20Sopenharmony_ci				qib_dev_err(dd, "SERDES PRAM VFY failed\n");
4738c2ecf20Sopenharmony_ci				goto bail;
4748c2ecf20Sopenharmony_ci			} /* end if verified */
4758c2ecf20Sopenharmony_ci		} /* end if loaded */
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci		/*
4788c2ecf20Sopenharmony_ci		 * Loaded and verified. Almost good...
4798c2ecf20Sopenharmony_ci		 * hold "success" in ret
4808c2ecf20Sopenharmony_ci		 */
4818c2ecf20Sopenharmony_ci		ret = 0;
4828c2ecf20Sopenharmony_ci		/*
4838c2ecf20Sopenharmony_ci		 * Prev steps all worked, continue bringup
4848c2ecf20Sopenharmony_ci		 * De-assert RESET to uC, only in first reset, to allow
4858c2ecf20Sopenharmony_ci		 * trimming.
4868c2ecf20Sopenharmony_ci		 *
4878c2ecf20Sopenharmony_ci		 * Since our default setup sets START_EQ1 to
4888c2ecf20Sopenharmony_ci		 * PRESET, we need to clear that for this very first run.
4898c2ecf20Sopenharmony_ci		 */
4908c2ecf20Sopenharmony_ci		ret = ibsd_mod_allchnls(dd, START_EQ1(0), 0, 0x38);
4918c2ecf20Sopenharmony_ci		if (ret < 0) {
4928c2ecf20Sopenharmony_ci			qib_dev_err(dd, "Failed clearing START_EQ1\n");
4938c2ecf20Sopenharmony_ci			goto bail;
4948c2ecf20Sopenharmony_ci		}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci		qib_ibsd_reset(dd, 0);
4978c2ecf20Sopenharmony_ci		/*
4988c2ecf20Sopenharmony_ci		 * If this is not the first reset, trimdone should be set
4998c2ecf20Sopenharmony_ci		 * already. We may need to check about this.
5008c2ecf20Sopenharmony_ci		 */
5018c2ecf20Sopenharmony_ci		trim_done = qib_sd_trimdone_poll(dd);
5028c2ecf20Sopenharmony_ci		/*
5038c2ecf20Sopenharmony_ci		 * Whether or not trimdone succeeded, we need to put the
5048c2ecf20Sopenharmony_ci		 * uC back into reset to avoid a possible fight with the
5058c2ecf20Sopenharmony_ci		 * IBC state-machine.
5068c2ecf20Sopenharmony_ci		 */
5078c2ecf20Sopenharmony_ci		qib_ibsd_reset(dd, 1);
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci		if (!trim_done) {
5108c2ecf20Sopenharmony_ci			qib_dev_err(dd, "No TRIMDONE seen\n");
5118c2ecf20Sopenharmony_ci			goto bail;
5128c2ecf20Sopenharmony_ci		}
5138c2ecf20Sopenharmony_ci		/*
5148c2ecf20Sopenharmony_ci		 * DEBUG: check each time we reset if trimdone bits have
5158c2ecf20Sopenharmony_ci		 * gotten cleared, and re-set them.
5168c2ecf20Sopenharmony_ci		 */
5178c2ecf20Sopenharmony_ci		qib_sd_trimdone_monitor(dd, "First-reset");
5188c2ecf20Sopenharmony_ci		/* Remember so we do not re-do the load, dactrim, etc. */
5198c2ecf20Sopenharmony_ci		dd->cspec->serdes_first_init_done = 1;
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci	/*
5228c2ecf20Sopenharmony_ci	 * setup for channel training and load values for
5238c2ecf20Sopenharmony_ci	 * RxEq and DDS in tables used by IBC in IB1.2 mode
5248c2ecf20Sopenharmony_ci	 */
5258c2ecf20Sopenharmony_ci	ret = 0;
5268c2ecf20Sopenharmony_ci	if (qib_sd_setvals(dd) >= 0)
5278c2ecf20Sopenharmony_ci		goto done;
5288c2ecf20Sopenharmony_cibail:
5298c2ecf20Sopenharmony_ci	ret = 1;
5308c2ecf20Sopenharmony_cidone:
5318c2ecf20Sopenharmony_ci	/* start relock timer regardless, but start at 1 second */
5328c2ecf20Sopenharmony_ci	set_7220_relock_poll(dd, -1);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	release_firmware(fw);
5358c2ecf20Sopenharmony_ci	return ret;
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci#define EPB_ACC_REQ 1
5398c2ecf20Sopenharmony_ci#define EPB_ACC_GNT 0x100
5408c2ecf20Sopenharmony_ci#define EPB_DATA_MASK 0xFF
5418c2ecf20Sopenharmony_ci#define EPB_RD (1ULL << 24)
5428c2ecf20Sopenharmony_ci#define EPB_TRANS_RDY (1ULL << 31)
5438c2ecf20Sopenharmony_ci#define EPB_TRANS_ERR (1ULL << 30)
5448c2ecf20Sopenharmony_ci#define EPB_TRANS_TRIES 5
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci/*
5478c2ecf20Sopenharmony_ci * query, claim, release ownership of the EPB (External Parallel Bus)
5488c2ecf20Sopenharmony_ci * for a specified SERDES.
5498c2ecf20Sopenharmony_ci * the "claim" parameter is >0 to claim, <0 to release, 0 to query.
5508c2ecf20Sopenharmony_ci * Returns <0 for errors, >0 if we had ownership, else 0.
5518c2ecf20Sopenharmony_ci */
5528c2ecf20Sopenharmony_cistatic int epb_access(struct qib_devdata *dd, int sdnum, int claim)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	u16 acc;
5558c2ecf20Sopenharmony_ci	u64 accval;
5568c2ecf20Sopenharmony_ci	int owned = 0;
5578c2ecf20Sopenharmony_ci	u64 oct_sel = 0;
5588c2ecf20Sopenharmony_ci
5598c2ecf20Sopenharmony_ci	switch (sdnum) {
5608c2ecf20Sopenharmony_ci	case IB_7220_SERDES:
5618c2ecf20Sopenharmony_ci		/*
5628c2ecf20Sopenharmony_ci		 * The IB SERDES "ownership" is fairly simple. A single each
5638c2ecf20Sopenharmony_ci		 * request/grant.
5648c2ecf20Sopenharmony_ci		 */
5658c2ecf20Sopenharmony_ci		acc = kr_ibsd_epb_access_ctrl;
5668c2ecf20Sopenharmony_ci		break;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	case PCIE_SERDES0:
5698c2ecf20Sopenharmony_ci	case PCIE_SERDES1:
5708c2ecf20Sopenharmony_ci		/* PCIe SERDES has two "octants", need to select which */
5718c2ecf20Sopenharmony_ci		acc = kr_pciesd_epb_access_ctrl;
5728c2ecf20Sopenharmony_ci		oct_sel = (2 << (sdnum - PCIE_SERDES0));
5738c2ecf20Sopenharmony_ci		break;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	default:
5768c2ecf20Sopenharmony_ci		return 0;
5778c2ecf20Sopenharmony_ci	}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	/* Make sure any outstanding transaction was seen */
5808c2ecf20Sopenharmony_ci	qib_read_kreg32(dd, kr_scratch);
5818c2ecf20Sopenharmony_ci	udelay(15);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	accval = qib_read_kreg32(dd, acc);
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	owned = !!(accval & EPB_ACC_GNT);
5868c2ecf20Sopenharmony_ci	if (claim < 0) {
5878c2ecf20Sopenharmony_ci		/* Need to release */
5888c2ecf20Sopenharmony_ci		u64 pollval;
5898c2ecf20Sopenharmony_ci		/*
5908c2ecf20Sopenharmony_ci		 * The only writeable bits are the request and CS.
5918c2ecf20Sopenharmony_ci		 * Both should be clear
5928c2ecf20Sopenharmony_ci		 */
5938c2ecf20Sopenharmony_ci		u64 newval = 0;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci		qib_write_kreg(dd, acc, newval);
5968c2ecf20Sopenharmony_ci		/* First read after write is not trustworthy */
5978c2ecf20Sopenharmony_ci		pollval = qib_read_kreg32(dd, acc);
5988c2ecf20Sopenharmony_ci		udelay(5);
5998c2ecf20Sopenharmony_ci		pollval = qib_read_kreg32(dd, acc);
6008c2ecf20Sopenharmony_ci		if (pollval & EPB_ACC_GNT)
6018c2ecf20Sopenharmony_ci			owned = -1;
6028c2ecf20Sopenharmony_ci	} else if (claim > 0) {
6038c2ecf20Sopenharmony_ci		/* Need to claim */
6048c2ecf20Sopenharmony_ci		u64 pollval;
6058c2ecf20Sopenharmony_ci		u64 newval = EPB_ACC_REQ | oct_sel;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci		qib_write_kreg(dd, acc, newval);
6088c2ecf20Sopenharmony_ci		/* First read after write is not trustworthy */
6098c2ecf20Sopenharmony_ci		pollval = qib_read_kreg32(dd, acc);
6108c2ecf20Sopenharmony_ci		udelay(5);
6118c2ecf20Sopenharmony_ci		pollval = qib_read_kreg32(dd, acc);
6128c2ecf20Sopenharmony_ci		if (!(pollval & EPB_ACC_GNT))
6138c2ecf20Sopenharmony_ci			owned = -1;
6148c2ecf20Sopenharmony_ci	}
6158c2ecf20Sopenharmony_ci	return owned;
6168c2ecf20Sopenharmony_ci}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci/*
6198c2ecf20Sopenharmony_ci * Lemma to deal with race condition of write..read to epb regs
6208c2ecf20Sopenharmony_ci */
6218c2ecf20Sopenharmony_cistatic int epb_trans(struct qib_devdata *dd, u16 reg, u64 i_val, u64 *o_vp)
6228c2ecf20Sopenharmony_ci{
6238c2ecf20Sopenharmony_ci	int tries;
6248c2ecf20Sopenharmony_ci	u64 transval;
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	qib_write_kreg(dd, reg, i_val);
6278c2ecf20Sopenharmony_ci	/* Throw away first read, as RDY bit may be stale */
6288c2ecf20Sopenharmony_ci	transval = qib_read_kreg64(dd, reg);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	for (tries = EPB_TRANS_TRIES; tries; --tries) {
6318c2ecf20Sopenharmony_ci		transval = qib_read_kreg32(dd, reg);
6328c2ecf20Sopenharmony_ci		if (transval & EPB_TRANS_RDY)
6338c2ecf20Sopenharmony_ci			break;
6348c2ecf20Sopenharmony_ci		udelay(5);
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci	if (transval & EPB_TRANS_ERR)
6378c2ecf20Sopenharmony_ci		return -1;
6388c2ecf20Sopenharmony_ci	if (tries > 0 && o_vp)
6398c2ecf20Sopenharmony_ci		*o_vp = transval;
6408c2ecf20Sopenharmony_ci	return tries;
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci/**
6448c2ecf20Sopenharmony_ci * qib_sd7220_reg_mod - modify SERDES register
6458c2ecf20Sopenharmony_ci * @dd: the qlogic_ib device
6468c2ecf20Sopenharmony_ci * @sdnum: which SERDES to access
6478c2ecf20Sopenharmony_ci * @loc: location - channel, element, register, as packed by EPB_LOC() macro.
6488c2ecf20Sopenharmony_ci * @wd: Write Data - value to set in register
6498c2ecf20Sopenharmony_ci * @mask: ones where data should be spliced into reg.
6508c2ecf20Sopenharmony_ci *
6518c2ecf20Sopenharmony_ci * Basic register read/modify/write, with un-needed acesses elided. That is,
6528c2ecf20Sopenharmony_ci * a mask of zero will prevent write, while a mask of 0xFF will prevent read.
6538c2ecf20Sopenharmony_ci * returns current (presumed, if a write was done) contents of selected
6548c2ecf20Sopenharmony_ci * register, or <0 if errors.
6558c2ecf20Sopenharmony_ci */
6568c2ecf20Sopenharmony_cistatic int qib_sd7220_reg_mod(struct qib_devdata *dd, int sdnum, u32 loc,
6578c2ecf20Sopenharmony_ci			      u32 wd, u32 mask)
6588c2ecf20Sopenharmony_ci{
6598c2ecf20Sopenharmony_ci	u16 trans;
6608c2ecf20Sopenharmony_ci	u64 transval;
6618c2ecf20Sopenharmony_ci	int owned;
6628c2ecf20Sopenharmony_ci	int tries, ret;
6638c2ecf20Sopenharmony_ci	unsigned long flags;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	switch (sdnum) {
6668c2ecf20Sopenharmony_ci	case IB_7220_SERDES:
6678c2ecf20Sopenharmony_ci		trans = kr_ibsd_epb_transaction_reg;
6688c2ecf20Sopenharmony_ci		break;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	case PCIE_SERDES0:
6718c2ecf20Sopenharmony_ci	case PCIE_SERDES1:
6728c2ecf20Sopenharmony_ci		trans = kr_pciesd_epb_transaction_reg;
6738c2ecf20Sopenharmony_ci		break;
6748c2ecf20Sopenharmony_ci
6758c2ecf20Sopenharmony_ci	default:
6768c2ecf20Sopenharmony_ci		return -1;
6778c2ecf20Sopenharmony_ci	}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	/*
6808c2ecf20Sopenharmony_ci	 * All access is locked in software (vs other host threads) and
6818c2ecf20Sopenharmony_ci	 * hardware (vs uC access).
6828c2ecf20Sopenharmony_ci	 */
6838c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	owned = epb_access(dd, sdnum, 1);
6868c2ecf20Sopenharmony_ci	if (owned < 0) {
6878c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
6888c2ecf20Sopenharmony_ci		return -1;
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci	ret = 0;
6918c2ecf20Sopenharmony_ci	for (tries = EPB_TRANS_TRIES; tries; --tries) {
6928c2ecf20Sopenharmony_ci		transval = qib_read_kreg32(dd, trans);
6938c2ecf20Sopenharmony_ci		if (transval & EPB_TRANS_RDY)
6948c2ecf20Sopenharmony_ci			break;
6958c2ecf20Sopenharmony_ci		udelay(5);
6968c2ecf20Sopenharmony_ci	}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	if (tries > 0) {
6998c2ecf20Sopenharmony_ci		tries = 1;      /* to make read-skip work */
7008c2ecf20Sopenharmony_ci		if (mask != 0xFF) {
7018c2ecf20Sopenharmony_ci			/*
7028c2ecf20Sopenharmony_ci			 * Not a pure write, so need to read.
7038c2ecf20Sopenharmony_ci			 * loc encodes chip-select as well as address
7048c2ecf20Sopenharmony_ci			 */
7058c2ecf20Sopenharmony_ci			transval = loc | EPB_RD;
7068c2ecf20Sopenharmony_ci			tries = epb_trans(dd, trans, transval, &transval);
7078c2ecf20Sopenharmony_ci		}
7088c2ecf20Sopenharmony_ci		if (tries > 0 && mask != 0) {
7098c2ecf20Sopenharmony_ci			/*
7108c2ecf20Sopenharmony_ci			 * Not a pure read, so need to write.
7118c2ecf20Sopenharmony_ci			 */
7128c2ecf20Sopenharmony_ci			wd = (wd & mask) | (transval & ~mask);
7138c2ecf20Sopenharmony_ci			transval = loc | (wd & EPB_DATA_MASK);
7148c2ecf20Sopenharmony_ci			tries = epb_trans(dd, trans, transval, &transval);
7158c2ecf20Sopenharmony_ci		}
7168c2ecf20Sopenharmony_ci	}
7178c2ecf20Sopenharmony_ci	/* else, failed to see ready, what error-handling? */
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	/*
7208c2ecf20Sopenharmony_ci	 * Release bus. Failure is an error.
7218c2ecf20Sopenharmony_ci	 */
7228c2ecf20Sopenharmony_ci	if (epb_access(dd, sdnum, -1) < 0)
7238c2ecf20Sopenharmony_ci		ret = -1;
7248c2ecf20Sopenharmony_ci	else
7258c2ecf20Sopenharmony_ci		ret = transval & EPB_DATA_MASK;
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
7288c2ecf20Sopenharmony_ci	if (tries <= 0)
7298c2ecf20Sopenharmony_ci		ret = -1;
7308c2ecf20Sopenharmony_ci	return ret;
7318c2ecf20Sopenharmony_ci}
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci#define EPB_ROM_R (2)
7348c2ecf20Sopenharmony_ci#define EPB_ROM_W (1)
7358c2ecf20Sopenharmony_ci/*
7368c2ecf20Sopenharmony_ci * Below, all uC-related, use appropriate UC_CS, depending
7378c2ecf20Sopenharmony_ci * on which SerDes is used.
7388c2ecf20Sopenharmony_ci */
7398c2ecf20Sopenharmony_ci#define EPB_UC_CTL EPB_LOC(6, 0, 0)
7408c2ecf20Sopenharmony_ci#define EPB_MADDRL EPB_LOC(6, 0, 2)
7418c2ecf20Sopenharmony_ci#define EPB_MADDRH EPB_LOC(6, 0, 3)
7428c2ecf20Sopenharmony_ci#define EPB_ROMDATA EPB_LOC(6, 0, 4)
7438c2ecf20Sopenharmony_ci#define EPB_RAMDATA EPB_LOC(6, 0, 5)
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_ci/* Transfer date to/from uC Program RAM of IB or PCIe SerDes */
7468c2ecf20Sopenharmony_cistatic int qib_sd7220_ram_xfer(struct qib_devdata *dd, int sdnum, u32 loc,
7478c2ecf20Sopenharmony_ci			       u8 *buf, int cnt, int rd_notwr)
7488c2ecf20Sopenharmony_ci{
7498c2ecf20Sopenharmony_ci	u16 trans;
7508c2ecf20Sopenharmony_ci	u64 transval;
7518c2ecf20Sopenharmony_ci	u64 csbit;
7528c2ecf20Sopenharmony_ci	int owned;
7538c2ecf20Sopenharmony_ci	int tries;
7548c2ecf20Sopenharmony_ci	int sofar;
7558c2ecf20Sopenharmony_ci	int addr;
7568c2ecf20Sopenharmony_ci	int ret;
7578c2ecf20Sopenharmony_ci	unsigned long flags;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	/* Pick appropriate transaction reg and "Chip select" for this serdes */
7608c2ecf20Sopenharmony_ci	switch (sdnum) {
7618c2ecf20Sopenharmony_ci	case IB_7220_SERDES:
7628c2ecf20Sopenharmony_ci		csbit = 1ULL << EPB_IB_UC_CS_SHF;
7638c2ecf20Sopenharmony_ci		trans = kr_ibsd_epb_transaction_reg;
7648c2ecf20Sopenharmony_ci		break;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	case PCIE_SERDES0:
7678c2ecf20Sopenharmony_ci	case PCIE_SERDES1:
7688c2ecf20Sopenharmony_ci		/* PCIe SERDES has uC "chip select" in different bit, too */
7698c2ecf20Sopenharmony_ci		csbit = 1ULL << EPB_PCIE_UC_CS_SHF;
7708c2ecf20Sopenharmony_ci		trans = kr_pciesd_epb_transaction_reg;
7718c2ecf20Sopenharmony_ci		break;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	default:
7748c2ecf20Sopenharmony_ci		return -1;
7758c2ecf20Sopenharmony_ci	}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	spin_lock_irqsave(&dd->cspec->sdepb_lock, flags);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	owned = epb_access(dd, sdnum, 1);
7808c2ecf20Sopenharmony_ci	if (owned < 0) {
7818c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
7828c2ecf20Sopenharmony_ci		return -1;
7838c2ecf20Sopenharmony_ci	}
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci	/*
7868c2ecf20Sopenharmony_ci	 * In future code, we may need to distinguish several address ranges,
7878c2ecf20Sopenharmony_ci	 * and select various memories based on this. For now, just trim
7888c2ecf20Sopenharmony_ci	 * "loc" (location including address and memory select) to
7898c2ecf20Sopenharmony_ci	 * "addr" (address within memory). we will only support PRAM
7908c2ecf20Sopenharmony_ci	 * The memory is 8KB.
7918c2ecf20Sopenharmony_ci	 */
7928c2ecf20Sopenharmony_ci	addr = loc & 0x1FFF;
7938c2ecf20Sopenharmony_ci	for (tries = EPB_TRANS_TRIES; tries; --tries) {
7948c2ecf20Sopenharmony_ci		transval = qib_read_kreg32(dd, trans);
7958c2ecf20Sopenharmony_ci		if (transval & EPB_TRANS_RDY)
7968c2ecf20Sopenharmony_ci			break;
7978c2ecf20Sopenharmony_ci		udelay(5);
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	sofar = 0;
8018c2ecf20Sopenharmony_ci	if (tries > 0) {
8028c2ecf20Sopenharmony_ci		/*
8038c2ecf20Sopenharmony_ci		 * Every "memory" access is doubly-indirect.
8048c2ecf20Sopenharmony_ci		 * We set two bytes of address, then read/write
8058c2ecf20Sopenharmony_ci		 * one or mores bytes of data.
8068c2ecf20Sopenharmony_ci		 */
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_ci		/* First, we set control to "Read" or "Write" */
8098c2ecf20Sopenharmony_ci		transval = csbit | EPB_UC_CTL |
8108c2ecf20Sopenharmony_ci			(rd_notwr ? EPB_ROM_R : EPB_ROM_W);
8118c2ecf20Sopenharmony_ci		tries = epb_trans(dd, trans, transval, &transval);
8128c2ecf20Sopenharmony_ci		while (tries > 0 && sofar < cnt) {
8138c2ecf20Sopenharmony_ci			if (!sofar) {
8148c2ecf20Sopenharmony_ci				/* Only set address at start of chunk */
8158c2ecf20Sopenharmony_ci				int addrbyte = (addr + sofar) >> 8;
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci				transval = csbit | EPB_MADDRH | addrbyte;
8188c2ecf20Sopenharmony_ci				tries = epb_trans(dd, trans, transval,
8198c2ecf20Sopenharmony_ci						  &transval);
8208c2ecf20Sopenharmony_ci				if (tries <= 0)
8218c2ecf20Sopenharmony_ci					break;
8228c2ecf20Sopenharmony_ci				addrbyte = (addr + sofar) & 0xFF;
8238c2ecf20Sopenharmony_ci				transval = csbit | EPB_MADDRL | addrbyte;
8248c2ecf20Sopenharmony_ci				tries = epb_trans(dd, trans, transval,
8258c2ecf20Sopenharmony_ci						 &transval);
8268c2ecf20Sopenharmony_ci				if (tries <= 0)
8278c2ecf20Sopenharmony_ci					break;
8288c2ecf20Sopenharmony_ci			}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci			if (rd_notwr)
8318c2ecf20Sopenharmony_ci				transval = csbit | EPB_ROMDATA | EPB_RD;
8328c2ecf20Sopenharmony_ci			else
8338c2ecf20Sopenharmony_ci				transval = csbit | EPB_ROMDATA | buf[sofar];
8348c2ecf20Sopenharmony_ci			tries = epb_trans(dd, trans, transval, &transval);
8358c2ecf20Sopenharmony_ci			if (tries <= 0)
8368c2ecf20Sopenharmony_ci				break;
8378c2ecf20Sopenharmony_ci			if (rd_notwr)
8388c2ecf20Sopenharmony_ci				buf[sofar] = transval & EPB_DATA_MASK;
8398c2ecf20Sopenharmony_ci			++sofar;
8408c2ecf20Sopenharmony_ci		}
8418c2ecf20Sopenharmony_ci		/* Finally, clear control-bit for Read or Write */
8428c2ecf20Sopenharmony_ci		transval = csbit | EPB_UC_CTL;
8438c2ecf20Sopenharmony_ci		tries = epb_trans(dd, trans, transval, &transval);
8448c2ecf20Sopenharmony_ci	}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	ret = sofar;
8478c2ecf20Sopenharmony_ci	/* Release bus. Failure is an error */
8488c2ecf20Sopenharmony_ci	if (epb_access(dd, sdnum, -1) < 0)
8498c2ecf20Sopenharmony_ci		ret = -1;
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&dd->cspec->sdepb_lock, flags);
8528c2ecf20Sopenharmony_ci	if (tries <= 0)
8538c2ecf20Sopenharmony_ci		ret = -1;
8548c2ecf20Sopenharmony_ci	return ret;
8558c2ecf20Sopenharmony_ci}
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci#define PROG_CHUNK 64
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic int qib_sd7220_prog_ld(struct qib_devdata *dd, int sdnum,
8608c2ecf20Sopenharmony_ci			      const u8 *img, int len, int offset)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	int cnt, sofar, req;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	sofar = 0;
8658c2ecf20Sopenharmony_ci	while (sofar < len) {
8668c2ecf20Sopenharmony_ci		req = len - sofar;
8678c2ecf20Sopenharmony_ci		if (req > PROG_CHUNK)
8688c2ecf20Sopenharmony_ci			req = PROG_CHUNK;
8698c2ecf20Sopenharmony_ci		cnt = qib_sd7220_ram_xfer(dd, sdnum, offset + sofar,
8708c2ecf20Sopenharmony_ci					  (u8 *)img + sofar, req, 0);
8718c2ecf20Sopenharmony_ci		if (cnt < req) {
8728c2ecf20Sopenharmony_ci			sofar = -1;
8738c2ecf20Sopenharmony_ci			break;
8748c2ecf20Sopenharmony_ci		}
8758c2ecf20Sopenharmony_ci		sofar += req;
8768c2ecf20Sopenharmony_ci	}
8778c2ecf20Sopenharmony_ci	return sofar;
8788c2ecf20Sopenharmony_ci}
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci#define VFY_CHUNK 64
8818c2ecf20Sopenharmony_ci#define SD_PRAM_ERROR_LIMIT 42
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_cistatic int qib_sd7220_prog_vfy(struct qib_devdata *dd, int sdnum,
8848c2ecf20Sopenharmony_ci			       const u8 *img, int len, int offset)
8858c2ecf20Sopenharmony_ci{
8868c2ecf20Sopenharmony_ci	int cnt, sofar, req, idx, errors;
8878c2ecf20Sopenharmony_ci	unsigned char readback[VFY_CHUNK];
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_ci	errors = 0;
8908c2ecf20Sopenharmony_ci	sofar = 0;
8918c2ecf20Sopenharmony_ci	while (sofar < len) {
8928c2ecf20Sopenharmony_ci		req = len - sofar;
8938c2ecf20Sopenharmony_ci		if (req > VFY_CHUNK)
8948c2ecf20Sopenharmony_ci			req = VFY_CHUNK;
8958c2ecf20Sopenharmony_ci		cnt = qib_sd7220_ram_xfer(dd, sdnum, sofar + offset,
8968c2ecf20Sopenharmony_ci					  readback, req, 1);
8978c2ecf20Sopenharmony_ci		if (cnt < req) {
8988c2ecf20Sopenharmony_ci			/* failed in read itself */
8998c2ecf20Sopenharmony_ci			sofar = -1;
9008c2ecf20Sopenharmony_ci			break;
9018c2ecf20Sopenharmony_ci		}
9028c2ecf20Sopenharmony_ci		for (idx = 0; idx < cnt; ++idx) {
9038c2ecf20Sopenharmony_ci			if (readback[idx] != img[idx+sofar])
9048c2ecf20Sopenharmony_ci				++errors;
9058c2ecf20Sopenharmony_ci		}
9068c2ecf20Sopenharmony_ci		sofar += cnt;
9078c2ecf20Sopenharmony_ci	}
9088c2ecf20Sopenharmony_ci	return errors ? -errors : sofar;
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_cistatic int
9128c2ecf20Sopenharmony_ciqib_sd7220_ib_load(struct qib_devdata *dd, const struct firmware *fw)
9138c2ecf20Sopenharmony_ci{
9148c2ecf20Sopenharmony_ci	return qib_sd7220_prog_ld(dd, IB_7220_SERDES, fw->data, fw->size, 0);
9158c2ecf20Sopenharmony_ci}
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_cistatic int
9188c2ecf20Sopenharmony_ciqib_sd7220_ib_vfy(struct qib_devdata *dd, const struct firmware *fw)
9198c2ecf20Sopenharmony_ci{
9208c2ecf20Sopenharmony_ci	return qib_sd7220_prog_vfy(dd, IB_7220_SERDES, fw->data, fw->size, 0);
9218c2ecf20Sopenharmony_ci}
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci/*
9248c2ecf20Sopenharmony_ci * IRQ not set up at this point in init, so we poll.
9258c2ecf20Sopenharmony_ci */
9268c2ecf20Sopenharmony_ci#define IB_SERDES_TRIM_DONE (1ULL << 11)
9278c2ecf20Sopenharmony_ci#define TRIM_TMO (15)
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_cistatic int qib_sd_trimdone_poll(struct qib_devdata *dd)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	int trim_tmo, ret;
9328c2ecf20Sopenharmony_ci	uint64_t val;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	/*
9358c2ecf20Sopenharmony_ci	 * Default to failure, so IBC will not start
9368c2ecf20Sopenharmony_ci	 * without IB_SERDES_TRIM_DONE.
9378c2ecf20Sopenharmony_ci	 */
9388c2ecf20Sopenharmony_ci	ret = 0;
9398c2ecf20Sopenharmony_ci	for (trim_tmo = 0; trim_tmo < TRIM_TMO; ++trim_tmo) {
9408c2ecf20Sopenharmony_ci		val = qib_read_kreg64(dd, kr_ibcstatus);
9418c2ecf20Sopenharmony_ci		if (val & IB_SERDES_TRIM_DONE) {
9428c2ecf20Sopenharmony_ci			ret = 1;
9438c2ecf20Sopenharmony_ci			break;
9448c2ecf20Sopenharmony_ci		}
9458c2ecf20Sopenharmony_ci		msleep(20);
9468c2ecf20Sopenharmony_ci	}
9478c2ecf20Sopenharmony_ci	if (trim_tmo >= TRIM_TMO) {
9488c2ecf20Sopenharmony_ci		qib_dev_err(dd, "No TRIMDONE in %d tries\n", trim_tmo);
9498c2ecf20Sopenharmony_ci		ret = 0;
9508c2ecf20Sopenharmony_ci	}
9518c2ecf20Sopenharmony_ci	return ret;
9528c2ecf20Sopenharmony_ci}
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci#define TX_FAST_ELT (9)
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci/*
9578c2ecf20Sopenharmony_ci * Set the "negotiation" values for SERDES. These are used by the IB1.2
9588c2ecf20Sopenharmony_ci * link negotiation. Macros below are attempt to keep the values a
9598c2ecf20Sopenharmony_ci * little more human-editable.
9608c2ecf20Sopenharmony_ci * First, values related to Drive De-emphasis Settings.
9618c2ecf20Sopenharmony_ci */
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci#define NUM_DDS_REGS 6
9648c2ecf20Sopenharmony_ci#define DDS_REG_MAP 0x76A910 /* LSB-first list of regs (in elt 9) to mod */
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci#define DDS_VAL(amp_d, main_d, ipst_d, ipre_d, amp_s, main_s, ipst_s, ipre_s) \
9678c2ecf20Sopenharmony_ci	{ { ((amp_d & 0x1F) << 1) | 1, ((amp_s & 0x1F) << 1) | 1, \
9688c2ecf20Sopenharmony_ci	  (main_d << 3) | 4 | (ipre_d >> 2), \
9698c2ecf20Sopenharmony_ci	  (main_s << 3) | 4 | (ipre_s >> 2), \
9708c2ecf20Sopenharmony_ci	  ((ipst_d & 0xF) << 1) | ((ipre_d & 3) << 6) | 0x21, \
9718c2ecf20Sopenharmony_ci	  ((ipst_s & 0xF) << 1) | ((ipre_s & 3) << 6) | 0x21 } }
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_cistatic struct dds_init {
9748c2ecf20Sopenharmony_ci	uint8_t reg_vals[NUM_DDS_REGS];
9758c2ecf20Sopenharmony_ci} dds_init_vals[] = {
9768c2ecf20Sopenharmony_ci	/*       DDR(FDR)       SDR(HDR)   */
9778c2ecf20Sopenharmony_ci	/* Vendor recommends below for 3m cable */
9788c2ecf20Sopenharmony_ci#define DDS_3M 0
9798c2ecf20Sopenharmony_ci	DDS_VAL(31, 19, 12, 0, 29, 22,  9, 0),
9808c2ecf20Sopenharmony_ci	DDS_VAL(31, 12, 15, 4, 31, 15, 15, 1),
9818c2ecf20Sopenharmony_ci	DDS_VAL(31, 13, 15, 3, 31, 16, 15, 0),
9828c2ecf20Sopenharmony_ci	DDS_VAL(31, 14, 15, 2, 31, 17, 14, 0),
9838c2ecf20Sopenharmony_ci	DDS_VAL(31, 15, 15, 1, 31, 18, 13, 0),
9848c2ecf20Sopenharmony_ci	DDS_VAL(31, 16, 15, 0, 31, 19, 12, 0),
9858c2ecf20Sopenharmony_ci	DDS_VAL(31, 17, 14, 0, 31, 20, 11, 0),
9868c2ecf20Sopenharmony_ci	DDS_VAL(31, 18, 13, 0, 30, 21, 10, 0),
9878c2ecf20Sopenharmony_ci	DDS_VAL(31, 20, 11, 0, 28, 23,  8, 0),
9888c2ecf20Sopenharmony_ci	DDS_VAL(31, 21, 10, 0, 27, 24,  7, 0),
9898c2ecf20Sopenharmony_ci	DDS_VAL(31, 22,  9, 0, 26, 25,  6, 0),
9908c2ecf20Sopenharmony_ci	DDS_VAL(30, 23,  8, 0, 25, 26,  5, 0),
9918c2ecf20Sopenharmony_ci	DDS_VAL(29, 24,  7, 0, 23, 27,  4, 0),
9928c2ecf20Sopenharmony_ci	/* Vendor recommends below for 1m cable */
9938c2ecf20Sopenharmony_ci#define DDS_1M 13
9948c2ecf20Sopenharmony_ci	DDS_VAL(28, 25,  6, 0, 21, 28,  3, 0),
9958c2ecf20Sopenharmony_ci	DDS_VAL(27, 26,  5, 0, 19, 29,  2, 0),
9968c2ecf20Sopenharmony_ci	DDS_VAL(25, 27,  4, 0, 17, 30,  1, 0)
9978c2ecf20Sopenharmony_ci};
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci/*
10008c2ecf20Sopenharmony_ci * Now the RXEQ section of the table.
10018c2ecf20Sopenharmony_ci */
10028c2ecf20Sopenharmony_ci/* Hardware packs an element number and register address thus: */
10038c2ecf20Sopenharmony_ci#define RXEQ_INIT_RDESC(elt, addr) (((elt) & 0xF) | ((addr) << 4))
10048c2ecf20Sopenharmony_ci#define RXEQ_VAL(elt, adr, val0, val1, val2, val3) \
10058c2ecf20Sopenharmony_ci	{RXEQ_INIT_RDESC((elt), (adr)), {(val0), (val1), (val2), (val3)} }
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci#define RXEQ_VAL_ALL(elt, adr, val)  \
10088c2ecf20Sopenharmony_ci	{RXEQ_INIT_RDESC((elt), (adr)), {(val), (val), (val), (val)} }
10098c2ecf20Sopenharmony_ci
10108c2ecf20Sopenharmony_ci#define RXEQ_SDR_DFELTH 0
10118c2ecf20Sopenharmony_ci#define RXEQ_SDR_TLTH 0
10128c2ecf20Sopenharmony_ci#define RXEQ_SDR_G1CNT_Z1CNT 0x11
10138c2ecf20Sopenharmony_ci#define RXEQ_SDR_ZCNT 23
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_cistatic struct rxeq_init {
10168c2ecf20Sopenharmony_ci	u16 rdesc;      /* in form used in SerDesDDSRXEQ */
10178c2ecf20Sopenharmony_ci	u8  rdata[4];
10188c2ecf20Sopenharmony_ci} rxeq_init_vals[] = {
10198c2ecf20Sopenharmony_ci	/* Set Rcv Eq. to Preset node */
10208c2ecf20Sopenharmony_ci	RXEQ_VAL_ALL(7, 0x27, 0x10),
10218c2ecf20Sopenharmony_ci	/* Set DFELTHFDR/HDR thresholds */
10228c2ecf20Sopenharmony_ci	RXEQ_VAL(7, 8,    0, 0, 0, 0), /* FDR, was 0, 1, 2, 3 */
10238c2ecf20Sopenharmony_ci	RXEQ_VAL(7, 0x21, 0, 0, 0, 0), /* HDR */
10248c2ecf20Sopenharmony_ci	/* Set TLTHFDR/HDR theshold */
10258c2ecf20Sopenharmony_ci	RXEQ_VAL(7, 9,    2, 2, 2, 2), /* FDR, was 0, 2, 4, 6 */
10268c2ecf20Sopenharmony_ci	RXEQ_VAL(7, 0x23, 2, 2, 2, 2), /* HDR, was  0, 1, 2, 3 */
10278c2ecf20Sopenharmony_ci	/* Set Preamp setting 2 (ZFR/ZCNT) */
10288c2ecf20Sopenharmony_ci	RXEQ_VAL(7, 0x1B, 12, 12, 12, 12), /* FDR, was 12, 16, 20, 24 */
10298c2ecf20Sopenharmony_ci	RXEQ_VAL(7, 0x1C, 12, 12, 12, 12), /* HDR, was 12, 16, 20, 24 */
10308c2ecf20Sopenharmony_ci	/* Set Preamp DC gain and Setting 1 (GFR/GHR) */
10318c2ecf20Sopenharmony_ci	RXEQ_VAL(7, 0x1E, 16, 16, 16, 16), /* FDR, was 16, 17, 18, 20 */
10328c2ecf20Sopenharmony_ci	RXEQ_VAL(7, 0x1F, 16, 16, 16, 16), /* HDR, was 16, 17, 18, 20 */
10338c2ecf20Sopenharmony_ci	/* Toggle RELOCK (in VCDL_CTRL0) to lock to data */
10348c2ecf20Sopenharmony_ci	RXEQ_VAL_ALL(6, 6, 0x20), /* Set D5 High */
10358c2ecf20Sopenharmony_ci	RXEQ_VAL_ALL(6, 6, 0), /* Set D5 Low */
10368c2ecf20Sopenharmony_ci};
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci/* There are 17 values from vendor, but IBC only accesses the first 16 */
10398c2ecf20Sopenharmony_ci#define DDS_ROWS (16)
10408c2ecf20Sopenharmony_ci#define RXEQ_ROWS ARRAY_SIZE(rxeq_init_vals)
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_cistatic int qib_sd_setvals(struct qib_devdata *dd)
10438c2ecf20Sopenharmony_ci{
10448c2ecf20Sopenharmony_ci	int idx, midx;
10458c2ecf20Sopenharmony_ci	int min_idx;     /* Minimum index for this portion of table */
10468c2ecf20Sopenharmony_ci	uint32_t dds_reg_map;
10478c2ecf20Sopenharmony_ci	u64 __iomem *taddr, *iaddr;
10488c2ecf20Sopenharmony_ci	uint64_t data;
10498c2ecf20Sopenharmony_ci	uint64_t sdctl;
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_ci	taddr = dd->kregbase + kr_serdes_maptable;
10528c2ecf20Sopenharmony_ci	iaddr = dd->kregbase + kr_serdes_ddsrxeq0;
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	/*
10558c2ecf20Sopenharmony_ci	 * Init the DDS section of the table.
10568c2ecf20Sopenharmony_ci	 * Each "row" of the table provokes NUM_DDS_REG writes, to the
10578c2ecf20Sopenharmony_ci	 * registers indicated in DDS_REG_MAP.
10588c2ecf20Sopenharmony_ci	 */
10598c2ecf20Sopenharmony_ci	sdctl = qib_read_kreg64(dd, kr_ibserdesctrl);
10608c2ecf20Sopenharmony_ci	sdctl = (sdctl & ~(0x1f << 8)) | (NUM_DDS_REGS << 8);
10618c2ecf20Sopenharmony_ci	sdctl = (sdctl & ~(0x1f << 13)) | (RXEQ_ROWS << 13);
10628c2ecf20Sopenharmony_ci	qib_write_kreg(dd, kr_ibserdesctrl, sdctl);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	/*
10658c2ecf20Sopenharmony_ci	 * Iterate down table within loop for each register to store.
10668c2ecf20Sopenharmony_ci	 */
10678c2ecf20Sopenharmony_ci	dds_reg_map = DDS_REG_MAP;
10688c2ecf20Sopenharmony_ci	for (idx = 0; idx < NUM_DDS_REGS; ++idx) {
10698c2ecf20Sopenharmony_ci		data = ((dds_reg_map & 0xF) << 4) | TX_FAST_ELT;
10708c2ecf20Sopenharmony_ci		writeq(data, iaddr + idx);
10718c2ecf20Sopenharmony_ci		qib_read_kreg32(dd, kr_scratch);
10728c2ecf20Sopenharmony_ci		dds_reg_map >>= 4;
10738c2ecf20Sopenharmony_ci		for (midx = 0; midx < DDS_ROWS; ++midx) {
10748c2ecf20Sopenharmony_ci			u64 __iomem *daddr = taddr + ((midx << 4) + idx);
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci			data = dds_init_vals[midx].reg_vals[idx];
10778c2ecf20Sopenharmony_ci			writeq(data, daddr);
10788c2ecf20Sopenharmony_ci			qib_read_kreg32(dd, kr_scratch);
10798c2ecf20Sopenharmony_ci		} /* End inner for (vals for this reg, each row) */
10808c2ecf20Sopenharmony_ci	} /* end outer for (regs to be stored) */
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci	/*
10838c2ecf20Sopenharmony_ci	 * Init the RXEQ section of the table.
10848c2ecf20Sopenharmony_ci	 * This runs in a different order, as the pattern of
10858c2ecf20Sopenharmony_ci	 * register references is more complex, but there are only
10868c2ecf20Sopenharmony_ci	 * four "data" values per register.
10878c2ecf20Sopenharmony_ci	 */
10888c2ecf20Sopenharmony_ci	min_idx = idx; /* RXEQ indices pick up where DDS left off */
10898c2ecf20Sopenharmony_ci	taddr += 0x100; /* RXEQ data is in second half of table */
10908c2ecf20Sopenharmony_ci	/* Iterate through RXEQ register addresses */
10918c2ecf20Sopenharmony_ci	for (idx = 0; idx < RXEQ_ROWS; ++idx) {
10928c2ecf20Sopenharmony_ci		int didx; /* "destination" */
10938c2ecf20Sopenharmony_ci		int vidx;
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci		/* didx is offset by min_idx to address RXEQ range of regs */
10968c2ecf20Sopenharmony_ci		didx = idx + min_idx;
10978c2ecf20Sopenharmony_ci		/* Store the next RXEQ register address */
10988c2ecf20Sopenharmony_ci		writeq(rxeq_init_vals[idx].rdesc, iaddr + didx);
10998c2ecf20Sopenharmony_ci		qib_read_kreg32(dd, kr_scratch);
11008c2ecf20Sopenharmony_ci		/* Iterate through RXEQ values */
11018c2ecf20Sopenharmony_ci		for (vidx = 0; vidx < 4; vidx++) {
11028c2ecf20Sopenharmony_ci			data = rxeq_init_vals[idx].rdata[vidx];
11038c2ecf20Sopenharmony_ci			writeq(data, taddr + (vidx << 6) + idx);
11048c2ecf20Sopenharmony_ci			qib_read_kreg32(dd, kr_scratch);
11058c2ecf20Sopenharmony_ci		}
11068c2ecf20Sopenharmony_ci	} /* end outer for (Reg-writes for RXEQ) */
11078c2ecf20Sopenharmony_ci	return 0;
11088c2ecf20Sopenharmony_ci}
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci#define CMUCTRL5 EPB_LOC(7, 0, 0x15)
11118c2ecf20Sopenharmony_ci#define RXHSCTRL0(chan) EPB_LOC(chan, 6, 0)
11128c2ecf20Sopenharmony_ci#define VCDL_DAC2(chan) EPB_LOC(chan, 6, 5)
11138c2ecf20Sopenharmony_ci#define VCDL_CTRL0(chan) EPB_LOC(chan, 6, 6)
11148c2ecf20Sopenharmony_ci#define VCDL_CTRL2(chan) EPB_LOC(chan, 6, 8)
11158c2ecf20Sopenharmony_ci#define START_EQ2(chan) EPB_LOC(chan, 7, 0x28)
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci/*
11188c2ecf20Sopenharmony_ci * Repeat a "store" across all channels of the IB SerDes.
11198c2ecf20Sopenharmony_ci * Although nominally it inherits the "read value" of the last
11208c2ecf20Sopenharmony_ci * channel it modified, the only really useful return is <0 for
11218c2ecf20Sopenharmony_ci * failure, >= 0 for success. The parameter 'loc' is assumed to
11228c2ecf20Sopenharmony_ci * be the location in some channel of the register to be modified
11238c2ecf20Sopenharmony_ci * The caller can specify use of the "gang write" option of EPB,
11248c2ecf20Sopenharmony_ci * in which case we use the specified channel data for any fields
11258c2ecf20Sopenharmony_ci * not explicitely written.
11268c2ecf20Sopenharmony_ci */
11278c2ecf20Sopenharmony_cistatic int ibsd_mod_allchnls(struct qib_devdata *dd, int loc, int val,
11288c2ecf20Sopenharmony_ci			     int mask)
11298c2ecf20Sopenharmony_ci{
11308c2ecf20Sopenharmony_ci	int ret = -1;
11318c2ecf20Sopenharmony_ci	int chnl;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	if (loc & EPB_GLOBAL_WR) {
11348c2ecf20Sopenharmony_ci		/*
11358c2ecf20Sopenharmony_ci		 * Our caller has assured us that we can set all four
11368c2ecf20Sopenharmony_ci		 * channels at once. Trust that. If mask is not 0xFF,
11378c2ecf20Sopenharmony_ci		 * we will read the _specified_ channel for our starting
11388c2ecf20Sopenharmony_ci		 * value.
11398c2ecf20Sopenharmony_ci		 */
11408c2ecf20Sopenharmony_ci		loc |= (1U << EPB_IB_QUAD0_CS_SHF);
11418c2ecf20Sopenharmony_ci		chnl = (loc >> (4 + EPB_ADDR_SHF)) & 7;
11428c2ecf20Sopenharmony_ci		if (mask != 0xFF) {
11438c2ecf20Sopenharmony_ci			ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES,
11448c2ecf20Sopenharmony_ci						 loc & ~EPB_GLOBAL_WR, 0, 0);
11458c2ecf20Sopenharmony_ci			if (ret < 0) {
11468c2ecf20Sopenharmony_ci				int sloc = loc >> EPB_ADDR_SHF;
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci				qib_dev_err(dd,
11498c2ecf20Sopenharmony_ci					"pre-read failed: elt %d, addr 0x%X, chnl %d\n",
11508c2ecf20Sopenharmony_ci					(sloc & 0xF),
11518c2ecf20Sopenharmony_ci					(sloc >> 9) & 0x3f, chnl);
11528c2ecf20Sopenharmony_ci				return ret;
11538c2ecf20Sopenharmony_ci			}
11548c2ecf20Sopenharmony_ci			val = (ret & ~mask) | (val & mask);
11558c2ecf20Sopenharmony_ci		}
11568c2ecf20Sopenharmony_ci		loc &=  ~(7 << (4+EPB_ADDR_SHF));
11578c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);
11588c2ecf20Sopenharmony_ci		if (ret < 0) {
11598c2ecf20Sopenharmony_ci			int sloc = loc >> EPB_ADDR_SHF;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci			qib_dev_err(dd,
11628c2ecf20Sopenharmony_ci				"Global WR failed: elt %d, addr 0x%X, val %02X\n",
11638c2ecf20Sopenharmony_ci				(sloc & 0xF), (sloc >> 9) & 0x3f, val);
11648c2ecf20Sopenharmony_ci		}
11658c2ecf20Sopenharmony_ci		return ret;
11668c2ecf20Sopenharmony_ci	}
11678c2ecf20Sopenharmony_ci	/* Clear "channel" and set CS so we can simply iterate */
11688c2ecf20Sopenharmony_ci	loc &=  ~(7 << (4+EPB_ADDR_SHF));
11698c2ecf20Sopenharmony_ci	loc |= (1U << EPB_IB_QUAD0_CS_SHF);
11708c2ecf20Sopenharmony_ci	for (chnl = 0; chnl < 4; ++chnl) {
11718c2ecf20Sopenharmony_ci		int cloc = loc | (chnl << (4+EPB_ADDR_SHF));
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_ci		ret = qib_sd7220_reg_mod(dd, IB_7220_SERDES, cloc, val, mask);
11748c2ecf20Sopenharmony_ci		if (ret < 0) {
11758c2ecf20Sopenharmony_ci			int sloc = loc >> EPB_ADDR_SHF;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci			qib_dev_err(dd,
11788c2ecf20Sopenharmony_ci				"Write failed: elt %d, addr 0x%X, chnl %d, val 0x%02X, mask 0x%02X\n",
11798c2ecf20Sopenharmony_ci				(sloc & 0xF), (sloc >> 9) & 0x3f, chnl,
11808c2ecf20Sopenharmony_ci				val & 0xFF, mask & 0xFF);
11818c2ecf20Sopenharmony_ci			break;
11828c2ecf20Sopenharmony_ci		}
11838c2ecf20Sopenharmony_ci	}
11848c2ecf20Sopenharmony_ci	return ret;
11858c2ecf20Sopenharmony_ci}
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci/*
11888c2ecf20Sopenharmony_ci * Set the Tx values normally modified by IBC in IB1.2 mode to default
11898c2ecf20Sopenharmony_ci * values, as gotten from first row of init table.
11908c2ecf20Sopenharmony_ci */
11918c2ecf20Sopenharmony_cistatic int set_dds_vals(struct qib_devdata *dd, struct dds_init *ddi)
11928c2ecf20Sopenharmony_ci{
11938c2ecf20Sopenharmony_ci	int ret;
11948c2ecf20Sopenharmony_ci	int idx, reg, data;
11958c2ecf20Sopenharmony_ci	uint32_t regmap;
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	regmap = DDS_REG_MAP;
11988c2ecf20Sopenharmony_ci	for (idx = 0; idx < NUM_DDS_REGS; ++idx) {
11998c2ecf20Sopenharmony_ci		reg = (regmap & 0xF);
12008c2ecf20Sopenharmony_ci		regmap >>= 4;
12018c2ecf20Sopenharmony_ci		data = ddi->reg_vals[idx];
12028c2ecf20Sopenharmony_ci		/* Vendor says RMW not needed for these regs, use 0xFF mask */
12038c2ecf20Sopenharmony_ci		ret = ibsd_mod_allchnls(dd, EPB_LOC(0, 9, reg), data, 0xFF);
12048c2ecf20Sopenharmony_ci		if (ret < 0)
12058c2ecf20Sopenharmony_ci			break;
12068c2ecf20Sopenharmony_ci	}
12078c2ecf20Sopenharmony_ci	return ret;
12088c2ecf20Sopenharmony_ci}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci/*
12118c2ecf20Sopenharmony_ci * Set the Rx values normally modified by IBC in IB1.2 mode to default
12128c2ecf20Sopenharmony_ci * values, as gotten from selected column of init table.
12138c2ecf20Sopenharmony_ci */
12148c2ecf20Sopenharmony_cistatic int set_rxeq_vals(struct qib_devdata *dd, int vsel)
12158c2ecf20Sopenharmony_ci{
12168c2ecf20Sopenharmony_ci	int ret;
12178c2ecf20Sopenharmony_ci	int ridx;
12188c2ecf20Sopenharmony_ci	int cnt = ARRAY_SIZE(rxeq_init_vals);
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	for (ridx = 0; ridx < cnt; ++ridx) {
12218c2ecf20Sopenharmony_ci		int elt, reg, val, loc;
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci		elt = rxeq_init_vals[ridx].rdesc & 0xF;
12248c2ecf20Sopenharmony_ci		reg = rxeq_init_vals[ridx].rdesc >> 4;
12258c2ecf20Sopenharmony_ci		loc = EPB_LOC(0, elt, reg);
12268c2ecf20Sopenharmony_ci		val = rxeq_init_vals[ridx].rdata[vsel];
12278c2ecf20Sopenharmony_ci		/* mask of 0xFF, because hardware does full-byte store. */
12288c2ecf20Sopenharmony_ci		ret = ibsd_mod_allchnls(dd, loc, val, 0xFF);
12298c2ecf20Sopenharmony_ci		if (ret < 0)
12308c2ecf20Sopenharmony_ci			break;
12318c2ecf20Sopenharmony_ci	}
12328c2ecf20Sopenharmony_ci	return ret;
12338c2ecf20Sopenharmony_ci}
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci/*
12368c2ecf20Sopenharmony_ci * Set the default values (row 0) for DDR Driver Demphasis.
12378c2ecf20Sopenharmony_ci * we do this initially and whenever we turn off IB-1.2
12388c2ecf20Sopenharmony_ci *
12398c2ecf20Sopenharmony_ci * The "default" values for Rx equalization are also stored to
12408c2ecf20Sopenharmony_ci * SerDes registers. Formerly (and still default), we used set 2.
12418c2ecf20Sopenharmony_ci * For experimenting with cables and link-partners, we allow changing
12428c2ecf20Sopenharmony_ci * that via a module parameter.
12438c2ecf20Sopenharmony_ci */
12448c2ecf20Sopenharmony_cistatic unsigned qib_rxeq_set = 2;
12458c2ecf20Sopenharmony_cimodule_param_named(rxeq_default_set, qib_rxeq_set, uint,
12468c2ecf20Sopenharmony_ci		   S_IWUSR | S_IRUGO);
12478c2ecf20Sopenharmony_ciMODULE_PARM_DESC(rxeq_default_set,
12488c2ecf20Sopenharmony_ci		 "Which set [0..3] of Rx Equalization values is default");
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_cistatic int qib_internal_presets(struct qib_devdata *dd)
12518c2ecf20Sopenharmony_ci{
12528c2ecf20Sopenharmony_ci	int ret = 0;
12538c2ecf20Sopenharmony_ci
12548c2ecf20Sopenharmony_ci	ret = set_dds_vals(dd, dds_init_vals + DDS_3M);
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	if (ret < 0)
12578c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed to set default DDS values\n");
12588c2ecf20Sopenharmony_ci	ret = set_rxeq_vals(dd, qib_rxeq_set & 3);
12598c2ecf20Sopenharmony_ci	if (ret < 0)
12608c2ecf20Sopenharmony_ci		qib_dev_err(dd, "Failed to set default RXEQ values\n");
12618c2ecf20Sopenharmony_ci	return ret;
12628c2ecf20Sopenharmony_ci}
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_ciint qib_sd7220_presets(struct qib_devdata *dd)
12658c2ecf20Sopenharmony_ci{
12668c2ecf20Sopenharmony_ci	int ret = 0;
12678c2ecf20Sopenharmony_ci
12688c2ecf20Sopenharmony_ci	if (!dd->cspec->presets_needed)
12698c2ecf20Sopenharmony_ci		return ret;
12708c2ecf20Sopenharmony_ci	dd->cspec->presets_needed = 0;
12718c2ecf20Sopenharmony_ci	/* Assert uC reset, so we don't clash with it. */
12728c2ecf20Sopenharmony_ci	qib_ibsd_reset(dd, 1);
12738c2ecf20Sopenharmony_ci	udelay(2);
12748c2ecf20Sopenharmony_ci	qib_sd_trimdone_monitor(dd, "link-down");
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_ci	ret = qib_internal_presets(dd);
12778c2ecf20Sopenharmony_ci	return ret;
12788c2ecf20Sopenharmony_ci}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_cistatic int qib_sd_trimself(struct qib_devdata *dd, int val)
12818c2ecf20Sopenharmony_ci{
12828c2ecf20Sopenharmony_ci	int loc = CMUCTRL5 | (1U << EPB_IB_QUAD0_CS_SHF);
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	return qib_sd7220_reg_mod(dd, IB_7220_SERDES, loc, val, 0xFF);
12858c2ecf20Sopenharmony_ci}
12868c2ecf20Sopenharmony_ci
12878c2ecf20Sopenharmony_cistatic int qib_sd_early(struct qib_devdata *dd)
12888c2ecf20Sopenharmony_ci{
12898c2ecf20Sopenharmony_ci	int ret;
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, RXHSCTRL0(0) | EPB_GLOBAL_WR, 0xD4, 0xFF);
12928c2ecf20Sopenharmony_ci	if (ret < 0)
12938c2ecf20Sopenharmony_ci		goto bail;
12948c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, START_EQ1(0) | EPB_GLOBAL_WR, 0x10, 0xFF);
12958c2ecf20Sopenharmony_ci	if (ret < 0)
12968c2ecf20Sopenharmony_ci		goto bail;
12978c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, START_EQ2(0) | EPB_GLOBAL_WR, 0x30, 0xFF);
12988c2ecf20Sopenharmony_cibail:
12998c2ecf20Sopenharmony_ci	return ret;
13008c2ecf20Sopenharmony_ci}
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci#define BACTRL(chnl) EPB_LOC(chnl, 6, 0x0E)
13038c2ecf20Sopenharmony_ci#define LDOUTCTRL1(chnl) EPB_LOC(chnl, 7, 6)
13048c2ecf20Sopenharmony_ci#define RXHSSTATUS(chnl) EPB_LOC(chnl, 6, 0xF)
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_cistatic int qib_sd_dactrim(struct qib_devdata *dd)
13078c2ecf20Sopenharmony_ci{
13088c2ecf20Sopenharmony_ci	int ret;
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, VCDL_DAC2(0) | EPB_GLOBAL_WR, 0x2D, 0xFF);
13118c2ecf20Sopenharmony_ci	if (ret < 0)
13128c2ecf20Sopenharmony_ci		goto bail;
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	/* more fine-tuning of what will be default */
13158c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, VCDL_CTRL2(0), 3, 0xF);
13168c2ecf20Sopenharmony_ci	if (ret < 0)
13178c2ecf20Sopenharmony_ci		goto bail;
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, BACTRL(0) | EPB_GLOBAL_WR, 0x40, 0xFF);
13208c2ecf20Sopenharmony_ci	if (ret < 0)
13218c2ecf20Sopenharmony_ci		goto bail;
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x04, 0xFF);
13248c2ecf20Sopenharmony_ci	if (ret < 0)
13258c2ecf20Sopenharmony_ci		goto bail;
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, RXHSSTATUS(0) | EPB_GLOBAL_WR, 0x04, 0xFF);
13288c2ecf20Sopenharmony_ci	if (ret < 0)
13298c2ecf20Sopenharmony_ci		goto bail;
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_ci	/*
13328c2ecf20Sopenharmony_ci	 * Delay for max possible number of steps, with slop.
13338c2ecf20Sopenharmony_ci	 * Each step is about 4usec.
13348c2ecf20Sopenharmony_ci	 */
13358c2ecf20Sopenharmony_ci	udelay(415);
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, LDOUTCTRL1(0) | EPB_GLOBAL_WR, 0x00, 0xFF);
13388c2ecf20Sopenharmony_ci
13398c2ecf20Sopenharmony_cibail:
13408c2ecf20Sopenharmony_ci	return ret;
13418c2ecf20Sopenharmony_ci}
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci#define RELOCK_FIRST_MS 3
13448c2ecf20Sopenharmony_ci#define RXLSPPM(chan) EPB_LOC(chan, 0, 2)
13458c2ecf20Sopenharmony_civoid toggle_7220_rclkrls(struct qib_devdata *dd)
13468c2ecf20Sopenharmony_ci{
13478c2ecf20Sopenharmony_ci	int loc = RXLSPPM(0) | EPB_GLOBAL_WR;
13488c2ecf20Sopenharmony_ci	int ret;
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);
13518c2ecf20Sopenharmony_ci	if (ret < 0)
13528c2ecf20Sopenharmony_ci		qib_dev_err(dd, "RCLKRLS failed to clear D7\n");
13538c2ecf20Sopenharmony_ci	else {
13548c2ecf20Sopenharmony_ci		udelay(1);
13558c2ecf20Sopenharmony_ci		ibsd_mod_allchnls(dd, loc, 0x80, 0x80);
13568c2ecf20Sopenharmony_ci	}
13578c2ecf20Sopenharmony_ci	/* And again for good measure */
13588c2ecf20Sopenharmony_ci	udelay(1);
13598c2ecf20Sopenharmony_ci	ret = ibsd_mod_allchnls(dd, loc, 0, 0x80);
13608c2ecf20Sopenharmony_ci	if (ret < 0)
13618c2ecf20Sopenharmony_ci		qib_dev_err(dd, "RCLKRLS failed to clear D7\n");
13628c2ecf20Sopenharmony_ci	else {
13638c2ecf20Sopenharmony_ci		udelay(1);
13648c2ecf20Sopenharmony_ci		ibsd_mod_allchnls(dd, loc, 0x80, 0x80);
13658c2ecf20Sopenharmony_ci	}
13668c2ecf20Sopenharmony_ci	/* Now reset xgxs and IBC to complete the recovery */
13678c2ecf20Sopenharmony_ci	dd->f_xgxs_reset(dd->pport);
13688c2ecf20Sopenharmony_ci}
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci/*
13718c2ecf20Sopenharmony_ci * Shut down the timer that polls for relock occasions, if needed
13728c2ecf20Sopenharmony_ci * this is "hooked" from qib_7220_quiet_serdes(), which is called
13738c2ecf20Sopenharmony_ci * just before qib_shutdown_device() in qib_driver.c shuts down all
13748c2ecf20Sopenharmony_ci * the other timers
13758c2ecf20Sopenharmony_ci */
13768c2ecf20Sopenharmony_civoid shutdown_7220_relock_poll(struct qib_devdata *dd)
13778c2ecf20Sopenharmony_ci{
13788c2ecf20Sopenharmony_ci	if (dd->cspec->relock_timer_active)
13798c2ecf20Sopenharmony_ci		del_timer_sync(&dd->cspec->relock_timer);
13808c2ecf20Sopenharmony_ci}
13818c2ecf20Sopenharmony_ci
13828c2ecf20Sopenharmony_cistatic unsigned qib_relock_by_timer = 1;
13838c2ecf20Sopenharmony_cimodule_param_named(relock_by_timer, qib_relock_by_timer, uint,
13848c2ecf20Sopenharmony_ci		   S_IWUSR | S_IRUGO);
13858c2ecf20Sopenharmony_ciMODULE_PARM_DESC(relock_by_timer, "Allow relock attempt if link not up");
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_cistatic void qib_run_relock(struct timer_list *t)
13888c2ecf20Sopenharmony_ci{
13898c2ecf20Sopenharmony_ci	struct qib_chip_specific *cs = from_timer(cs, t, relock_timer);
13908c2ecf20Sopenharmony_ci	struct qib_devdata *dd = cs->dd;
13918c2ecf20Sopenharmony_ci	struct qib_pportdata *ppd = dd->pport;
13928c2ecf20Sopenharmony_ci	int timeoff;
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	/*
13958c2ecf20Sopenharmony_ci	 * Check link-training state for "stuck" state, when down.
13968c2ecf20Sopenharmony_ci	 * if found, try relock and schedule another try at
13978c2ecf20Sopenharmony_ci	 * exponentially growing delay, maxed at one second.
13988c2ecf20Sopenharmony_ci	 * if not stuck, our work is done.
13998c2ecf20Sopenharmony_ci	 */
14008c2ecf20Sopenharmony_ci	if ((dd->flags & QIB_INITTED) && !(ppd->lflags &
14018c2ecf20Sopenharmony_ci	    (QIBL_IB_AUTONEG_INPROG | QIBL_LINKINIT | QIBL_LINKARMED |
14028c2ecf20Sopenharmony_ci	     QIBL_LINKACTIVE))) {
14038c2ecf20Sopenharmony_ci		if (qib_relock_by_timer) {
14048c2ecf20Sopenharmony_ci			if (!(ppd->lflags & QIBL_IB_LINK_DISABLED))
14058c2ecf20Sopenharmony_ci				toggle_7220_rclkrls(dd);
14068c2ecf20Sopenharmony_ci		}
14078c2ecf20Sopenharmony_ci		/* re-set timer for next check */
14088c2ecf20Sopenharmony_ci		timeoff = cs->relock_interval << 1;
14098c2ecf20Sopenharmony_ci		if (timeoff > HZ)
14108c2ecf20Sopenharmony_ci			timeoff = HZ;
14118c2ecf20Sopenharmony_ci		cs->relock_interval = timeoff;
14128c2ecf20Sopenharmony_ci	} else
14138c2ecf20Sopenharmony_ci		timeoff = HZ;
14148c2ecf20Sopenharmony_ci	mod_timer(&cs->relock_timer, jiffies + timeoff);
14158c2ecf20Sopenharmony_ci}
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_civoid set_7220_relock_poll(struct qib_devdata *dd, int ibup)
14188c2ecf20Sopenharmony_ci{
14198c2ecf20Sopenharmony_ci	struct qib_chip_specific *cs = dd->cspec;
14208c2ecf20Sopenharmony_ci
14218c2ecf20Sopenharmony_ci	if (ibup) {
14228c2ecf20Sopenharmony_ci		/* We are now up, relax timer to 1 second interval */
14238c2ecf20Sopenharmony_ci		if (cs->relock_timer_active) {
14248c2ecf20Sopenharmony_ci			cs->relock_interval = HZ;
14258c2ecf20Sopenharmony_ci			mod_timer(&cs->relock_timer, jiffies + HZ);
14268c2ecf20Sopenharmony_ci		}
14278c2ecf20Sopenharmony_ci	} else {
14288c2ecf20Sopenharmony_ci		/* Transition to down, (re-)set timer to short interval. */
14298c2ecf20Sopenharmony_ci		unsigned int timeout;
14308c2ecf20Sopenharmony_ci
14318c2ecf20Sopenharmony_ci		timeout = msecs_to_jiffies(RELOCK_FIRST_MS);
14328c2ecf20Sopenharmony_ci		if (timeout == 0)
14338c2ecf20Sopenharmony_ci			timeout = 1;
14348c2ecf20Sopenharmony_ci		/* If timer has not yet been started, do so. */
14358c2ecf20Sopenharmony_ci		if (!cs->relock_timer_active) {
14368c2ecf20Sopenharmony_ci			cs->relock_timer_active = 1;
14378c2ecf20Sopenharmony_ci			timer_setup(&cs->relock_timer, qib_run_relock, 0);
14388c2ecf20Sopenharmony_ci			cs->relock_interval = timeout;
14398c2ecf20Sopenharmony_ci			cs->relock_timer.expires = jiffies + timeout;
14408c2ecf20Sopenharmony_ci			add_timer(&cs->relock_timer);
14418c2ecf20Sopenharmony_ci		} else {
14428c2ecf20Sopenharmony_ci			cs->relock_interval = timeout;
14438c2ecf20Sopenharmony_ci			mod_timer(&cs->relock_timer, jiffies + timeout);
14448c2ecf20Sopenharmony_ci		}
14458c2ecf20Sopenharmony_ci	}
14468c2ecf20Sopenharmony_ci}
1447