/*1* AMD 10Gb Ethernet driver2*3* Copyright (c) 2020 Advanced Micro Devices, Inc.4*5* This file is available to you under your choice of the following two6* licenses:7*8* License 1: GPLv29*10* This file is free software; you may copy, redistribute and/or modify11* it under the terms of the GNU General Public License as published by12* the Free Software Foundation, either version 2 of the License, or (at13* your option) any later version.14*15* This file is distributed in the hope that it will be useful, but16* WITHOUT ANY WARRANTY; without even the implied warranty of17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU18* General Public License for more details.19*20* You should have received a copy of the GNU General Public License21* along with this program. If not, see <http://www.gnu.org/licenses/>.22*23* This file incorporates work covered by the following copyright and24* permission notice:25* The Synopsys DWC ETHER XGMAC Software Driver and documentation26* (hereinafter "Software") is an unsupported proprietary work of Synopsys,27* Inc. unless otherwise expressly agreed to in writing between Synopsys28* and you.29*30* The Software IS NOT an item of Licensed Software or Licensed Product31* under any End User Software License Agreement or Agreement for Licensed32* Product with Synopsys or any supplement thereto. Permission is hereby33* granted, free of charge, to any person obtaining a copy of this software34* annotated with this license and the Software, to deal in the Software35* without restriction, including without limitation the rights to use,36* copy, modify, merge, publish, distribute, sublicense, and/or sell copies37* of the Software, and to permit persons to whom the Software is furnished38* to do so, subject to the following conditions:39*40* The above copyright notice and this permission notice shall be included41* in all copies or substantial portions of the Software.42*43* THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"44* BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED45* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A46* PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS47* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR48* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF49* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS50* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN51* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)52* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF53* THE POSSIBILITY OF SUCH DAMAGE.54*55*56* License 2: Modified BSD57*58* Redistribution and use in source and binary forms, with or without59* modification, are permitted provided that the following conditions are met:60* * Redistributions of source code must retain the above copyright61* notice, this list of conditions and the following disclaimer.62* * Redistributions in binary form must reproduce the above copyright63* notice, this list of conditions and the following disclaimer in the64* documentation and/or other materials provided with the distribution.65* * Neither the name of Advanced Micro Devices, Inc. nor the66* names of its contributors may be used to endorse or promote products67* derived from this software without specific prior written permission.68*69* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"70* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE71* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE72* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY73* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES74* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;75* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND76* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT77* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF78* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.79*80* This file incorporates work covered by the following copyright and81* permission notice:82* The Synopsys DWC ETHER XGMAC Software Driver and documentation83* (hereinafter "Software") is an unsupported proprietary work of Synopsys,84* Inc. unless otherwise expressly agreed to in writing between Synopsys85* and you.86*87* The Software IS NOT an item of Licensed Software or Licensed Product88* under any End User Software License Agreement or Agreement for Licensed89* Product with Synopsys or any supplement thereto. Permission is hereby90* granted, free of charge, to any person obtaining a copy of this software91* annotated with this license and the Software, to deal in the Software92* without restriction, including without limitation the rights to use,93* copy, modify, merge, publish, distribute, sublicense, and/or sell copies94* of the Software, and to permit persons to whom the Software is furnished95* to do so, subject to the following conditions:96*97* The above copyright notice and this permission notice shall be included98* in all copies or substantial portions of the Software.99*100* THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"101* BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED102* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A103* PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS104* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR105* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF106* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS107* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN108* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)109* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF110* THE POSSIBILITY OF SUCH DAMAGE.111*/112113#include <sys/cdefs.h>114#include "xgbe.h"115#include "xgbe-common.h"116117#define XGBE_ABORT_COUNT 500118#define XGBE_DISABLE_COUNT 1000119120#define XGBE_STD_SPEED 1121122#define XGBE_INTR_RX_FULL BIT(IC_RAW_INTR_STAT_RX_FULL_INDEX)123#define XGBE_INTR_TX_EMPTY BIT(IC_RAW_INTR_STAT_TX_EMPTY_INDEX)124#define XGBE_INTR_TX_ABRT BIT(IC_RAW_INTR_STAT_TX_ABRT_INDEX)125#define XGBE_INTR_STOP_DET BIT(IC_RAW_INTR_STAT_STOP_DET_INDEX)126#define XGBE_DEFAULT_INT_MASK (XGBE_INTR_RX_FULL | \127XGBE_INTR_TX_EMPTY | \128XGBE_INTR_TX_ABRT | \129XGBE_INTR_STOP_DET)130131#define XGBE_I2C_READ BIT(8)132#define XGBE_I2C_STOP BIT(9)133134static int135xgbe_i2c_abort(struct xgbe_prv_data *pdata)136{137unsigned int wait = XGBE_ABORT_COUNT;138139/* Must be enabled to recognize the abort request */140XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, 1);141142/* Issue the abort */143XI2C_IOWRITE_BITS(pdata, IC_ENABLE, ABORT, 1);144145while (wait--) {146if (!XI2C_IOREAD_BITS(pdata, IC_ENABLE, ABORT))147return (0);148149DELAY(500);150}151152return (-EBUSY);153}154155static int156xgbe_i2c_set_enable(struct xgbe_prv_data *pdata, bool enable)157{158unsigned int wait = XGBE_DISABLE_COUNT;159unsigned int mode = enable ? 1 : 0;160161while (wait--) {162XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, mode);163if (XI2C_IOREAD_BITS(pdata, IC_ENABLE_STATUS, EN) == mode)164return (0);165166DELAY(100);167}168169return (-EBUSY);170}171172static int173xgbe_i2c_disable(struct xgbe_prv_data *pdata)174{175unsigned int ret;176177ret = xgbe_i2c_set_enable(pdata, false);178if (ret) {179/* Disable failed, try an abort */180ret = xgbe_i2c_abort(pdata);181if (ret) {182axgbe_error("%s: i2c_abort %d\n", __func__, ret);183return (ret);184}185186/* Abort succeeded, try to disable again */187ret = xgbe_i2c_set_enable(pdata, false);188}189190axgbe_printf(3, "%s: final i2c_disable %d\n", __func__, ret);191return (ret);192}193194static int195xgbe_i2c_enable(struct xgbe_prv_data *pdata)196{197return (xgbe_i2c_set_enable(pdata, true));198}199200static void201xgbe_i2c_clear_all_interrupts(struct xgbe_prv_data *pdata)202{203XI2C_IOREAD(pdata, IC_CLR_INTR);204}205206static void207xgbe_i2c_disable_interrupts(struct xgbe_prv_data *pdata)208{209XI2C_IOWRITE(pdata, IC_INTR_MASK, 0);210}211212static void213xgbe_i2c_enable_interrupts(struct xgbe_prv_data *pdata)214{215XI2C_IOWRITE(pdata, IC_INTR_MASK, XGBE_DEFAULT_INT_MASK);216}217218static void219xgbe_i2c_write(struct xgbe_prv_data *pdata)220{221struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;222unsigned int tx_slots, cmd;223224/* Configured to never receive Rx overflows, so fill up Tx fifo */225tx_slots = pdata->i2c.tx_fifo_size - XI2C_IOREAD(pdata, IC_TXFLR);226axgbe_printf(3, "%s: tx_slots %d tx_len %d\n", __func__, tx_slots,227state->tx_len);228229while (tx_slots && state->tx_len) {230if (state->op->cmd == XGBE_I2C_CMD_READ)231cmd = XGBE_I2C_READ;232else233cmd = *state->tx_buf++;234235axgbe_printf(3, "%s: cmd %d tx_len %d\n", __func__, cmd,236state->tx_len);237238if (state->tx_len == 1)239XI2C_SET_BITS(cmd, IC_DATA_CMD, STOP, 1);240241XI2C_IOWRITE(pdata, IC_DATA_CMD, cmd);242243tx_slots--;244state->tx_len--;245}246247/* No more Tx operations, so ignore TX_EMPTY and return */248if (!state->tx_len)249XI2C_IOWRITE_BITS(pdata, IC_INTR_MASK, TX_EMPTY, 0);250}251252static void253xgbe_i2c_read(struct xgbe_prv_data *pdata)254{255struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;256unsigned int rx_slots;257258/* Anything to be read? */259axgbe_printf(3, "%s: op cmd %d\n", __func__, state->op->cmd);260if (state->op->cmd != XGBE_I2C_CMD_READ)261return;262263rx_slots = XI2C_IOREAD(pdata, IC_RXFLR);264axgbe_printf(3, "%s: rx_slots %d rx_len %d\n", __func__, rx_slots,265state->rx_len);266267while (rx_slots && state->rx_len) {268*state->rx_buf++ = XI2C_IOREAD(pdata, IC_DATA_CMD);269state->rx_len--;270rx_slots--;271}272}273274static void275xgbe_i2c_clear_isr_interrupts(struct xgbe_prv_data *pdata, unsigned int isr)276{277struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;278279if (isr & XGBE_INTR_TX_ABRT) {280state->tx_abort_source = XI2C_IOREAD(pdata, IC_TX_ABRT_SOURCE);281XI2C_IOREAD(pdata, IC_CLR_TX_ABRT);282}283284if (isr & XGBE_INTR_STOP_DET)285XI2C_IOREAD(pdata, IC_CLR_STOP_DET);286}287288static void289xgbe_i2c_isr(void *data)290{291struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data;292struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;293unsigned int isr;294295isr = XI2C_IOREAD(pdata, IC_RAW_INTR_STAT);296axgbe_printf(3, "%s: isr 0x%x\n", __func__, isr);297if (!isr)298goto reissue_check;299300axgbe_printf(3, "%s: I2C interrupt status=%#010x\n", __func__, isr);301302xgbe_i2c_clear_isr_interrupts(pdata, isr);303304if (isr & XGBE_INTR_TX_ABRT) {305axgbe_printf(1, "%s: I2C TX_ABRT received (%#010x) for target "306"%#04x\n", __func__, state->tx_abort_source,307state->op->target);308309xgbe_i2c_disable_interrupts(pdata);310311state->ret = -EIO;312goto out;313}314315/* Check for data in the Rx fifo */316xgbe_i2c_read(pdata);317318/* Fill up the Tx fifo next */319xgbe_i2c_write(pdata);320321out:322/* Complete on an error or STOP condition */323axgbe_printf(3, "%s: ret %d stop %d\n", __func__, state->ret,324XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET));325326if (state->ret || XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET))327pdata->i2c_complete = true;328329reissue_check:330/* Reissue interrupt if status is not clear */331if (pdata->vdata->irq_reissue_support)332XP_IOWRITE(pdata, XP_INT_REISSUE_EN, 1 << 2);333}334335static void336xgbe_i2c_set_mode(struct xgbe_prv_data *pdata)337{338unsigned int reg;339340reg = XI2C_IOREAD(pdata, IC_CON);341XI2C_SET_BITS(reg, IC_CON, MASTER_MODE, 1);342XI2C_SET_BITS(reg, IC_CON, SLAVE_DISABLE, 1);343XI2C_SET_BITS(reg, IC_CON, RESTART_EN, 1);344XI2C_SET_BITS(reg, IC_CON, SPEED, XGBE_STD_SPEED);345XI2C_SET_BITS(reg, IC_CON, RX_FIFO_FULL_HOLD, 1);346XI2C_IOWRITE(pdata, IC_CON, reg);347}348349static void350xgbe_i2c_get_features(struct xgbe_prv_data *pdata)351{352struct xgbe_i2c *i2c = &pdata->i2c;353unsigned int reg;354355reg = XI2C_IOREAD(pdata, IC_COMP_PARAM_1);356i2c->max_speed_mode = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,357MAX_SPEED_MODE);358i2c->rx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,359RX_BUFFER_DEPTH);360i2c->tx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,361TX_BUFFER_DEPTH);362363axgbe_printf(3, "%s: I2C features: %s=%u, %s=%u, %s=%u\n", __func__,364"MAX_SPEED_MODE", i2c->max_speed_mode,365"RX_BUFFER_DEPTH", i2c->rx_fifo_size,366"TX_BUFFER_DEPTH", i2c->tx_fifo_size);367}368369static void370xgbe_i2c_set_target(struct xgbe_prv_data *pdata, unsigned int addr)371{372XI2C_IOWRITE(pdata, IC_TAR, addr);373}374375static void376xgbe_i2c_combined_isr(struct xgbe_prv_data *pdata)377{378xgbe_i2c_isr(pdata);379}380381static int382xgbe_i2c_xfer(struct xgbe_prv_data *pdata, struct xgbe_i2c_op *op)383{384struct xgbe_i2c_op_state *state = &pdata->i2c.op_state;385unsigned long timeout;386int ret;387388mtx_lock(&pdata->i2c_mutex);389390axgbe_printf(3, "i2c xfer started ---->>>\n");391392ret = xgbe_i2c_disable(pdata);393if (ret) {394axgbe_error("failed to disable i2c master\n");395goto out;396}397398xgbe_i2c_set_target(pdata, op->target);399400memset(state, 0, sizeof(*state));401state->op = op;402state->tx_len = op->len;403state->tx_buf = op->buf;404state->rx_len = op->len;405state->rx_buf = op->buf;406407xgbe_i2c_clear_all_interrupts(pdata);408ret = xgbe_i2c_enable(pdata);409if (ret) {410axgbe_error("failed to enable i2c master\n");411goto out;412}413414/* Enabling the interrupts will cause the TX FIFO empty interrupt to415* fire and begin to process the command via the ISR.416*/417xgbe_i2c_enable_interrupts(pdata);418419timeout = ticks + (20 * hz);420while (ticks < timeout) {421422if (!pdata->i2c_complete) {423DELAY(200);424continue;425}426427axgbe_printf(1, "%s: I2C OP complete\n", __func__);428break;429}430431if ((ticks >= timeout) && !pdata->i2c_complete) {432axgbe_error("%s: operation timed out\n", __func__);433ret = -ETIMEDOUT;434goto disable;435}436437ret = state->ret;438axgbe_printf(3, "%s: i2c xfer ret %d abrt_source 0x%x\n", __func__,439ret, state->tx_abort_source);440if (ret) {441axgbe_printf(1, "%s: i2c xfer ret %d abrt_source 0x%x\n", __func__,442ret, state->tx_abort_source);443if (state->tx_abort_source & IC_TX_ABRT_7B_ADDR_NOACK)444ret = -ENOTCONN;445else if (state->tx_abort_source & IC_TX_ABRT_ARB_LOST)446ret = -EAGAIN;447}448449axgbe_printf(3, "i2c xfer finished ---->>>\n");450451disable:452pdata->i2c_complete = false;453xgbe_i2c_disable_interrupts(pdata);454xgbe_i2c_disable(pdata);455456out:457mtx_unlock(&pdata->i2c_mutex);458return (ret);459}460461static void462xgbe_i2c_stop(struct xgbe_prv_data *pdata)463{464if (!pdata->i2c.started)465return;466467axgbe_printf(3, "stopping I2C\n");468469pdata->i2c.started = 0;470471xgbe_i2c_disable_interrupts(pdata);472xgbe_i2c_disable(pdata);473xgbe_i2c_clear_all_interrupts(pdata);474}475476static int477xgbe_i2c_start(struct xgbe_prv_data *pdata)478{479if (pdata->i2c.started)480return (0);481482pdata->i2c.started = 1;483484return (0);485}486487static int488xgbe_i2c_init(struct xgbe_prv_data *pdata)489{490int ret;491492/* initialize lock for i2c */493mtx_init(&pdata->i2c_mutex, "xgbe i2c mutex lock", NULL, MTX_DEF);494pdata->i2c_complete = false;495496xgbe_i2c_disable_interrupts(pdata);497498ret = xgbe_i2c_disable(pdata);499if (ret) {500axgbe_error("failed to disable i2c master\n");501return (ret);502}503504xgbe_i2c_get_features(pdata);505506xgbe_i2c_set_mode(pdata);507508xgbe_i2c_clear_all_interrupts(pdata);509510xgbe_dump_i2c_registers(pdata);511512return (0);513}514515void516xgbe_init_function_ptrs_i2c(struct xgbe_i2c_if *i2c_if)517{518i2c_if->i2c_init = xgbe_i2c_init;519520i2c_if->i2c_start = xgbe_i2c_start;521i2c_if->i2c_stop = xgbe_i2c_stop;522523i2c_if->i2c_xfer = xgbe_i2c_xfer;524525i2c_if->i2c_isr = xgbe_i2c_combined_isr;526}527528529