17113afc8SEmmanuel Vadot /* 27113afc8SEmmanuel Vadot * AMD 10Gb Ethernet driver 37113afc8SEmmanuel Vadot * 47113afc8SEmmanuel Vadot * Copyright (c) 2020 Advanced Micro Devices, Inc. 57113afc8SEmmanuel Vadot * 67113afc8SEmmanuel Vadot * This file is available to you under your choice of the following two 77113afc8SEmmanuel Vadot * licenses: 87113afc8SEmmanuel Vadot * 97113afc8SEmmanuel Vadot * License 1: GPLv2 107113afc8SEmmanuel Vadot * 117113afc8SEmmanuel Vadot * This file is free software; you may copy, redistribute and/or modify 127113afc8SEmmanuel Vadot * it under the terms of the GNU General Public License as published by 137113afc8SEmmanuel Vadot * the Free Software Foundation, either version 2 of the License, or (at 147113afc8SEmmanuel Vadot * your option) any later version. 157113afc8SEmmanuel Vadot * 167113afc8SEmmanuel Vadot * This file is distributed in the hope that it will be useful, but 177113afc8SEmmanuel Vadot * WITHOUT ANY WARRANTY; without even the implied warranty of 187113afc8SEmmanuel Vadot * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 197113afc8SEmmanuel Vadot * General Public License for more details. 207113afc8SEmmanuel Vadot * 217113afc8SEmmanuel Vadot * You should have received a copy of the GNU General Public License 227113afc8SEmmanuel Vadot * along with this program. If not, see <http://www.gnu.org/licenses/>. 237113afc8SEmmanuel Vadot * 247113afc8SEmmanuel Vadot * This file incorporates work covered by the following copyright and 257113afc8SEmmanuel Vadot * permission notice: 267113afc8SEmmanuel Vadot * The Synopsys DWC ETHER XGMAC Software Driver and documentation 277113afc8SEmmanuel Vadot * (hereinafter "Software") is an unsupported proprietary work of Synopsys, 287113afc8SEmmanuel Vadot * Inc. unless otherwise expressly agreed to in writing between Synopsys 297113afc8SEmmanuel Vadot * and you. 307113afc8SEmmanuel Vadot * 317113afc8SEmmanuel Vadot * The Software IS NOT an item of Licensed Software or Licensed Product 327113afc8SEmmanuel Vadot * under any End User Software License Agreement or Agreement for Licensed 337113afc8SEmmanuel Vadot * Product with Synopsys or any supplement thereto. Permission is hereby 347113afc8SEmmanuel Vadot * granted, free of charge, to any person obtaining a copy of this software 357113afc8SEmmanuel Vadot * annotated with this license and the Software, to deal in the Software 367113afc8SEmmanuel Vadot * without restriction, including without limitation the rights to use, 377113afc8SEmmanuel Vadot * copy, modify, merge, publish, distribute, sublicense, and/or sell copies 387113afc8SEmmanuel Vadot * of the Software, and to permit persons to whom the Software is furnished 397113afc8SEmmanuel Vadot * to do so, subject to the following conditions: 407113afc8SEmmanuel Vadot * 417113afc8SEmmanuel Vadot * The above copyright notice and this permission notice shall be included 427113afc8SEmmanuel Vadot * in all copies or substantial portions of the Software. 437113afc8SEmmanuel Vadot * 447113afc8SEmmanuel Vadot * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" 457113afc8SEmmanuel Vadot * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 467113afc8SEmmanuel Vadot * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 477113afc8SEmmanuel Vadot * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS 487113afc8SEmmanuel Vadot * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 497113afc8SEmmanuel Vadot * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 507113afc8SEmmanuel Vadot * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 517113afc8SEmmanuel Vadot * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 527113afc8SEmmanuel Vadot * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 537113afc8SEmmanuel Vadot * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 547113afc8SEmmanuel Vadot * THE POSSIBILITY OF SUCH DAMAGE. 557113afc8SEmmanuel Vadot * 567113afc8SEmmanuel Vadot * 577113afc8SEmmanuel Vadot * License 2: Modified BSD 587113afc8SEmmanuel Vadot * 597113afc8SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 607113afc8SEmmanuel Vadot * modification, are permitted provided that the following conditions are met: 617113afc8SEmmanuel Vadot * * Redistributions of source code must retain the above copyright 627113afc8SEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 637113afc8SEmmanuel Vadot * * Redistributions in binary form must reproduce the above copyright 647113afc8SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 657113afc8SEmmanuel Vadot * documentation and/or other materials provided with the distribution. 667113afc8SEmmanuel Vadot * * Neither the name of Advanced Micro Devices, Inc. nor the 677113afc8SEmmanuel Vadot * names of its contributors may be used to endorse or promote products 687113afc8SEmmanuel Vadot * derived from this software without specific prior written permission. 697113afc8SEmmanuel Vadot * 707113afc8SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 717113afc8SEmmanuel Vadot * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 727113afc8SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 737113afc8SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 747113afc8SEmmanuel Vadot * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 757113afc8SEmmanuel Vadot * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 767113afc8SEmmanuel Vadot * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 777113afc8SEmmanuel Vadot * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 787113afc8SEmmanuel Vadot * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 797113afc8SEmmanuel Vadot * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 807113afc8SEmmanuel Vadot * 817113afc8SEmmanuel Vadot * This file incorporates work covered by the following copyright and 827113afc8SEmmanuel Vadot * permission notice: 837113afc8SEmmanuel Vadot * The Synopsys DWC ETHER XGMAC Software Driver and documentation 847113afc8SEmmanuel Vadot * (hereinafter "Software") is an unsupported proprietary work of Synopsys, 857113afc8SEmmanuel Vadot * Inc. unless otherwise expressly agreed to in writing between Synopsys 867113afc8SEmmanuel Vadot * and you. 877113afc8SEmmanuel Vadot * 887113afc8SEmmanuel Vadot * The Software IS NOT an item of Licensed Software or Licensed Product 897113afc8SEmmanuel Vadot * under any End User Software License Agreement or Agreement for Licensed 907113afc8SEmmanuel Vadot * Product with Synopsys or any supplement thereto. Permission is hereby 917113afc8SEmmanuel Vadot * granted, free of charge, to any person obtaining a copy of this software 927113afc8SEmmanuel Vadot * annotated with this license and the Software, to deal in the Software 937113afc8SEmmanuel Vadot * without restriction, including without limitation the rights to use, 947113afc8SEmmanuel Vadot * copy, modify, merge, publish, distribute, sublicense, and/or sell copies 957113afc8SEmmanuel Vadot * of the Software, and to permit persons to whom the Software is furnished 967113afc8SEmmanuel Vadot * to do so, subject to the following conditions: 977113afc8SEmmanuel Vadot * 987113afc8SEmmanuel Vadot * The above copyright notice and this permission notice shall be included 997113afc8SEmmanuel Vadot * in all copies or substantial portions of the Software. 1007113afc8SEmmanuel Vadot * 1017113afc8SEmmanuel Vadot * THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS" 1027113afc8SEmmanuel Vadot * BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1037113afc8SEmmanuel Vadot * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 1047113afc8SEmmanuel Vadot * PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS 1057113afc8SEmmanuel Vadot * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1067113afc8SEmmanuel Vadot * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1077113afc8SEmmanuel Vadot * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1087113afc8SEmmanuel Vadot * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1097113afc8SEmmanuel Vadot * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1107113afc8SEmmanuel Vadot * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 1117113afc8SEmmanuel Vadot * THE POSSIBILITY OF SUCH DAMAGE. 1127113afc8SEmmanuel Vadot */ 1137113afc8SEmmanuel Vadot 1147113afc8SEmmanuel Vadot #include <sys/cdefs.h> 1157113afc8SEmmanuel Vadot #include "xgbe.h" 1167113afc8SEmmanuel Vadot #include "xgbe-common.h" 1177113afc8SEmmanuel Vadot 1187113afc8SEmmanuel Vadot #define XGBE_ABORT_COUNT 500 1197113afc8SEmmanuel Vadot #define XGBE_DISABLE_COUNT 1000 1207113afc8SEmmanuel Vadot 1217113afc8SEmmanuel Vadot #define XGBE_STD_SPEED 1 1227113afc8SEmmanuel Vadot 1237113afc8SEmmanuel Vadot #define XGBE_INTR_RX_FULL BIT(IC_RAW_INTR_STAT_RX_FULL_INDEX) 1247113afc8SEmmanuel Vadot #define XGBE_INTR_TX_EMPTY BIT(IC_RAW_INTR_STAT_TX_EMPTY_INDEX) 1257113afc8SEmmanuel Vadot #define XGBE_INTR_TX_ABRT BIT(IC_RAW_INTR_STAT_TX_ABRT_INDEX) 1267113afc8SEmmanuel Vadot #define XGBE_INTR_STOP_DET BIT(IC_RAW_INTR_STAT_STOP_DET_INDEX) 1277113afc8SEmmanuel Vadot #define XGBE_DEFAULT_INT_MASK (XGBE_INTR_RX_FULL | \ 1287113afc8SEmmanuel Vadot XGBE_INTR_TX_EMPTY | \ 1297113afc8SEmmanuel Vadot XGBE_INTR_TX_ABRT | \ 1307113afc8SEmmanuel Vadot XGBE_INTR_STOP_DET) 1317113afc8SEmmanuel Vadot 1327113afc8SEmmanuel Vadot #define XGBE_I2C_READ BIT(8) 1337113afc8SEmmanuel Vadot #define XGBE_I2C_STOP BIT(9) 1347113afc8SEmmanuel Vadot 1357113afc8SEmmanuel Vadot static int 1367113afc8SEmmanuel Vadot xgbe_i2c_abort(struct xgbe_prv_data *pdata) 1377113afc8SEmmanuel Vadot { 1387113afc8SEmmanuel Vadot unsigned int wait = XGBE_ABORT_COUNT; 1397113afc8SEmmanuel Vadot 1407113afc8SEmmanuel Vadot /* Must be enabled to recognize the abort request */ 1417113afc8SEmmanuel Vadot XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, 1); 1427113afc8SEmmanuel Vadot 1437113afc8SEmmanuel Vadot /* Issue the abort */ 1447113afc8SEmmanuel Vadot XI2C_IOWRITE_BITS(pdata, IC_ENABLE, ABORT, 1); 1457113afc8SEmmanuel Vadot 1467113afc8SEmmanuel Vadot while (wait--) { 1477113afc8SEmmanuel Vadot if (!XI2C_IOREAD_BITS(pdata, IC_ENABLE, ABORT)) 1487113afc8SEmmanuel Vadot return (0); 1497113afc8SEmmanuel Vadot 1507113afc8SEmmanuel Vadot DELAY(500); 1517113afc8SEmmanuel Vadot } 1527113afc8SEmmanuel Vadot 1537113afc8SEmmanuel Vadot return (-EBUSY); 1547113afc8SEmmanuel Vadot } 1557113afc8SEmmanuel Vadot 1567113afc8SEmmanuel Vadot static int 1577113afc8SEmmanuel Vadot xgbe_i2c_set_enable(struct xgbe_prv_data *pdata, bool enable) 1587113afc8SEmmanuel Vadot { 1597113afc8SEmmanuel Vadot unsigned int wait = XGBE_DISABLE_COUNT; 1607113afc8SEmmanuel Vadot unsigned int mode = enable ? 1 : 0; 1617113afc8SEmmanuel Vadot 1627113afc8SEmmanuel Vadot while (wait--) { 1637113afc8SEmmanuel Vadot XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, mode); 1647113afc8SEmmanuel Vadot if (XI2C_IOREAD_BITS(pdata, IC_ENABLE_STATUS, EN) == mode) 1657113afc8SEmmanuel Vadot return (0); 1667113afc8SEmmanuel Vadot 1677113afc8SEmmanuel Vadot DELAY(100); 1687113afc8SEmmanuel Vadot } 1697113afc8SEmmanuel Vadot 1707113afc8SEmmanuel Vadot return (-EBUSY); 1717113afc8SEmmanuel Vadot } 1727113afc8SEmmanuel Vadot 1737113afc8SEmmanuel Vadot static int 1747113afc8SEmmanuel Vadot xgbe_i2c_disable(struct xgbe_prv_data *pdata) 1757113afc8SEmmanuel Vadot { 1767113afc8SEmmanuel Vadot unsigned int ret; 1777113afc8SEmmanuel Vadot 1787113afc8SEmmanuel Vadot ret = xgbe_i2c_set_enable(pdata, false); 1797113afc8SEmmanuel Vadot if (ret) { 1807113afc8SEmmanuel Vadot /* Disable failed, try an abort */ 1817113afc8SEmmanuel Vadot ret = xgbe_i2c_abort(pdata); 1827113afc8SEmmanuel Vadot if (ret) { 1837113afc8SEmmanuel Vadot axgbe_error("%s: i2c_abort %d\n", __func__, ret); 1847113afc8SEmmanuel Vadot return (ret); 1857113afc8SEmmanuel Vadot } 1867113afc8SEmmanuel Vadot 1877113afc8SEmmanuel Vadot /* Abort succeeded, try to disable again */ 1887113afc8SEmmanuel Vadot ret = xgbe_i2c_set_enable(pdata, false); 1897113afc8SEmmanuel Vadot } 1907113afc8SEmmanuel Vadot 1917113afc8SEmmanuel Vadot axgbe_printf(3, "%s: final i2c_disable %d\n", __func__, ret); 1927113afc8SEmmanuel Vadot return (ret); 1937113afc8SEmmanuel Vadot } 1947113afc8SEmmanuel Vadot 1957113afc8SEmmanuel Vadot static int 1967113afc8SEmmanuel Vadot xgbe_i2c_enable(struct xgbe_prv_data *pdata) 1977113afc8SEmmanuel Vadot { 1987113afc8SEmmanuel Vadot return (xgbe_i2c_set_enable(pdata, true)); 1997113afc8SEmmanuel Vadot } 2007113afc8SEmmanuel Vadot 2017113afc8SEmmanuel Vadot static void 2027113afc8SEmmanuel Vadot xgbe_i2c_clear_all_interrupts(struct xgbe_prv_data *pdata) 2037113afc8SEmmanuel Vadot { 2047113afc8SEmmanuel Vadot XI2C_IOREAD(pdata, IC_CLR_INTR); 2057113afc8SEmmanuel Vadot } 2067113afc8SEmmanuel Vadot 2077113afc8SEmmanuel Vadot static void 2087113afc8SEmmanuel Vadot xgbe_i2c_disable_interrupts(struct xgbe_prv_data *pdata) 2097113afc8SEmmanuel Vadot { 2107113afc8SEmmanuel Vadot XI2C_IOWRITE(pdata, IC_INTR_MASK, 0); 2117113afc8SEmmanuel Vadot } 2127113afc8SEmmanuel Vadot 2137113afc8SEmmanuel Vadot static void 2147113afc8SEmmanuel Vadot xgbe_i2c_enable_interrupts(struct xgbe_prv_data *pdata) 2157113afc8SEmmanuel Vadot { 2167113afc8SEmmanuel Vadot XI2C_IOWRITE(pdata, IC_INTR_MASK, XGBE_DEFAULT_INT_MASK); 2177113afc8SEmmanuel Vadot } 2187113afc8SEmmanuel Vadot 2197113afc8SEmmanuel Vadot static void 2207113afc8SEmmanuel Vadot xgbe_i2c_write(struct xgbe_prv_data *pdata) 2217113afc8SEmmanuel Vadot { 2227113afc8SEmmanuel Vadot struct xgbe_i2c_op_state *state = &pdata->i2c.op_state; 2237113afc8SEmmanuel Vadot unsigned int tx_slots, cmd; 2247113afc8SEmmanuel Vadot 2257113afc8SEmmanuel Vadot /* Configured to never receive Rx overflows, so fill up Tx fifo */ 2267113afc8SEmmanuel Vadot tx_slots = pdata->i2c.tx_fifo_size - XI2C_IOREAD(pdata, IC_TXFLR); 2277113afc8SEmmanuel Vadot axgbe_printf(3, "%s: tx_slots %d tx_len %d\n", __func__, tx_slots, 2287113afc8SEmmanuel Vadot state->tx_len); 2297113afc8SEmmanuel Vadot 2307113afc8SEmmanuel Vadot while (tx_slots && state->tx_len) { 2317113afc8SEmmanuel Vadot if (state->op->cmd == XGBE_I2C_CMD_READ) 2327113afc8SEmmanuel Vadot cmd = XGBE_I2C_READ; 2337113afc8SEmmanuel Vadot else 2347113afc8SEmmanuel Vadot cmd = *state->tx_buf++; 2357113afc8SEmmanuel Vadot 2367113afc8SEmmanuel Vadot axgbe_printf(3, "%s: cmd %d tx_len %d\n", __func__, cmd, 2377113afc8SEmmanuel Vadot state->tx_len); 2387113afc8SEmmanuel Vadot 2397113afc8SEmmanuel Vadot if (state->tx_len == 1) 2407113afc8SEmmanuel Vadot XI2C_SET_BITS(cmd, IC_DATA_CMD, STOP, 1); 2417113afc8SEmmanuel Vadot 2427113afc8SEmmanuel Vadot XI2C_IOWRITE(pdata, IC_DATA_CMD, cmd); 2437113afc8SEmmanuel Vadot 2447113afc8SEmmanuel Vadot tx_slots--; 2457113afc8SEmmanuel Vadot state->tx_len--; 2467113afc8SEmmanuel Vadot } 2477113afc8SEmmanuel Vadot 2487113afc8SEmmanuel Vadot /* No more Tx operations, so ignore TX_EMPTY and return */ 2497113afc8SEmmanuel Vadot if (!state->tx_len) 2507113afc8SEmmanuel Vadot XI2C_IOWRITE_BITS(pdata, IC_INTR_MASK, TX_EMPTY, 0); 2517113afc8SEmmanuel Vadot } 2527113afc8SEmmanuel Vadot 2537113afc8SEmmanuel Vadot static void 2547113afc8SEmmanuel Vadot xgbe_i2c_read(struct xgbe_prv_data *pdata) 2557113afc8SEmmanuel Vadot { 2567113afc8SEmmanuel Vadot struct xgbe_i2c_op_state *state = &pdata->i2c.op_state; 2577113afc8SEmmanuel Vadot unsigned int rx_slots; 2587113afc8SEmmanuel Vadot 2597113afc8SEmmanuel Vadot /* Anything to be read? */ 2607113afc8SEmmanuel Vadot axgbe_printf(3, "%s: op cmd %d\n", __func__, state->op->cmd); 2617113afc8SEmmanuel Vadot if (state->op->cmd != XGBE_I2C_CMD_READ) 2627113afc8SEmmanuel Vadot return; 2637113afc8SEmmanuel Vadot 2647113afc8SEmmanuel Vadot rx_slots = XI2C_IOREAD(pdata, IC_RXFLR); 2657113afc8SEmmanuel Vadot axgbe_printf(3, "%s: rx_slots %d rx_len %d\n", __func__, rx_slots, 2667113afc8SEmmanuel Vadot state->rx_len); 2677113afc8SEmmanuel Vadot 2687113afc8SEmmanuel Vadot while (rx_slots && state->rx_len) { 2697113afc8SEmmanuel Vadot *state->rx_buf++ = XI2C_IOREAD(pdata, IC_DATA_CMD); 2707113afc8SEmmanuel Vadot state->rx_len--; 2717113afc8SEmmanuel Vadot rx_slots--; 2727113afc8SEmmanuel Vadot } 2737113afc8SEmmanuel Vadot } 2747113afc8SEmmanuel Vadot 2757113afc8SEmmanuel Vadot static void 2767113afc8SEmmanuel Vadot xgbe_i2c_clear_isr_interrupts(struct xgbe_prv_data *pdata, unsigned int isr) 2777113afc8SEmmanuel Vadot { 2787113afc8SEmmanuel Vadot struct xgbe_i2c_op_state *state = &pdata->i2c.op_state; 2797113afc8SEmmanuel Vadot 2807113afc8SEmmanuel Vadot if (isr & XGBE_INTR_TX_ABRT) { 2817113afc8SEmmanuel Vadot state->tx_abort_source = XI2C_IOREAD(pdata, IC_TX_ABRT_SOURCE); 2827113afc8SEmmanuel Vadot XI2C_IOREAD(pdata, IC_CLR_TX_ABRT); 2837113afc8SEmmanuel Vadot } 2847113afc8SEmmanuel Vadot 2857113afc8SEmmanuel Vadot if (isr & XGBE_INTR_STOP_DET) 2867113afc8SEmmanuel Vadot XI2C_IOREAD(pdata, IC_CLR_STOP_DET); 2877113afc8SEmmanuel Vadot } 2887113afc8SEmmanuel Vadot 2897113afc8SEmmanuel Vadot static void 2907113afc8SEmmanuel Vadot xgbe_i2c_isr(void *data) 2917113afc8SEmmanuel Vadot { 2927113afc8SEmmanuel Vadot struct xgbe_prv_data *pdata = (struct xgbe_prv_data *)data; 2937113afc8SEmmanuel Vadot struct xgbe_i2c_op_state *state = &pdata->i2c.op_state; 2947113afc8SEmmanuel Vadot unsigned int isr; 2957113afc8SEmmanuel Vadot 2967113afc8SEmmanuel Vadot isr = XI2C_IOREAD(pdata, IC_RAW_INTR_STAT); 2977113afc8SEmmanuel Vadot axgbe_printf(3, "%s: isr 0x%x\n", __func__, isr); 2987113afc8SEmmanuel Vadot if (!isr) 2997113afc8SEmmanuel Vadot goto reissue_check; 3007113afc8SEmmanuel Vadot 3017113afc8SEmmanuel Vadot axgbe_printf(3, "%s: I2C interrupt status=%#010x\n", __func__, isr); 3027113afc8SEmmanuel Vadot 3037113afc8SEmmanuel Vadot xgbe_i2c_clear_isr_interrupts(pdata, isr); 3047113afc8SEmmanuel Vadot 3057113afc8SEmmanuel Vadot if (isr & XGBE_INTR_TX_ABRT) { 3067113afc8SEmmanuel Vadot axgbe_printf(1, "%s: I2C TX_ABRT received (%#010x) for target " 3077113afc8SEmmanuel Vadot "%#04x\n", __func__, state->tx_abort_source, 3087113afc8SEmmanuel Vadot state->op->target); 3097113afc8SEmmanuel Vadot 3107113afc8SEmmanuel Vadot xgbe_i2c_disable_interrupts(pdata); 3117113afc8SEmmanuel Vadot 3127113afc8SEmmanuel Vadot state->ret = -EIO; 3137113afc8SEmmanuel Vadot goto out; 3147113afc8SEmmanuel Vadot } 3157113afc8SEmmanuel Vadot 3167113afc8SEmmanuel Vadot /* Check for data in the Rx fifo */ 3177113afc8SEmmanuel Vadot xgbe_i2c_read(pdata); 3187113afc8SEmmanuel Vadot 3197113afc8SEmmanuel Vadot /* Fill up the Tx fifo next */ 3207113afc8SEmmanuel Vadot xgbe_i2c_write(pdata); 3217113afc8SEmmanuel Vadot 3227113afc8SEmmanuel Vadot out: 3237113afc8SEmmanuel Vadot /* Complete on an error or STOP condition */ 3247113afc8SEmmanuel Vadot axgbe_printf(3, "%s: ret %d stop %d\n", __func__, state->ret, 3257113afc8SEmmanuel Vadot XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET)); 3267113afc8SEmmanuel Vadot 3277113afc8SEmmanuel Vadot if (state->ret || XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET)) 3287113afc8SEmmanuel Vadot pdata->i2c_complete = true; 3297113afc8SEmmanuel Vadot 3307113afc8SEmmanuel Vadot reissue_check: 3317113afc8SEmmanuel Vadot /* Reissue interrupt if status is not clear */ 3327113afc8SEmmanuel Vadot if (pdata->vdata->irq_reissue_support) 3337113afc8SEmmanuel Vadot XP_IOWRITE(pdata, XP_INT_REISSUE_EN, 1 << 2); 3347113afc8SEmmanuel Vadot } 3357113afc8SEmmanuel Vadot 3367113afc8SEmmanuel Vadot static void 3377113afc8SEmmanuel Vadot xgbe_i2c_set_mode(struct xgbe_prv_data *pdata) 3387113afc8SEmmanuel Vadot { 3397113afc8SEmmanuel Vadot unsigned int reg; 3407113afc8SEmmanuel Vadot 3417113afc8SEmmanuel Vadot reg = XI2C_IOREAD(pdata, IC_CON); 3427113afc8SEmmanuel Vadot XI2C_SET_BITS(reg, IC_CON, MASTER_MODE, 1); 3437113afc8SEmmanuel Vadot XI2C_SET_BITS(reg, IC_CON, SLAVE_DISABLE, 1); 3447113afc8SEmmanuel Vadot XI2C_SET_BITS(reg, IC_CON, RESTART_EN, 1); 3457113afc8SEmmanuel Vadot XI2C_SET_BITS(reg, IC_CON, SPEED, XGBE_STD_SPEED); 3467113afc8SEmmanuel Vadot XI2C_SET_BITS(reg, IC_CON, RX_FIFO_FULL_HOLD, 1); 3477113afc8SEmmanuel Vadot XI2C_IOWRITE(pdata, IC_CON, reg); 3487113afc8SEmmanuel Vadot } 3497113afc8SEmmanuel Vadot 3507113afc8SEmmanuel Vadot static void 3517113afc8SEmmanuel Vadot xgbe_i2c_get_features(struct xgbe_prv_data *pdata) 3527113afc8SEmmanuel Vadot { 3537113afc8SEmmanuel Vadot struct xgbe_i2c *i2c = &pdata->i2c; 3547113afc8SEmmanuel Vadot unsigned int reg; 3557113afc8SEmmanuel Vadot 3567113afc8SEmmanuel Vadot reg = XI2C_IOREAD(pdata, IC_COMP_PARAM_1); 3577113afc8SEmmanuel Vadot i2c->max_speed_mode = XI2C_GET_BITS(reg, IC_COMP_PARAM_1, 3587113afc8SEmmanuel Vadot MAX_SPEED_MODE); 3597113afc8SEmmanuel Vadot i2c->rx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1, 3607113afc8SEmmanuel Vadot RX_BUFFER_DEPTH); 3617113afc8SEmmanuel Vadot i2c->tx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1, 3627113afc8SEmmanuel Vadot TX_BUFFER_DEPTH); 3637113afc8SEmmanuel Vadot 3647113afc8SEmmanuel Vadot axgbe_printf(3, "%s: I2C features: %s=%u, %s=%u, %s=%u\n", __func__, 3657113afc8SEmmanuel Vadot "MAX_SPEED_MODE", i2c->max_speed_mode, 3667113afc8SEmmanuel Vadot "RX_BUFFER_DEPTH", i2c->rx_fifo_size, 3677113afc8SEmmanuel Vadot "TX_BUFFER_DEPTH", i2c->tx_fifo_size); 3687113afc8SEmmanuel Vadot } 3697113afc8SEmmanuel Vadot 3707113afc8SEmmanuel Vadot static void 3717113afc8SEmmanuel Vadot xgbe_i2c_set_target(struct xgbe_prv_data *pdata, unsigned int addr) 3727113afc8SEmmanuel Vadot { 3737113afc8SEmmanuel Vadot XI2C_IOWRITE(pdata, IC_TAR, addr); 3747113afc8SEmmanuel Vadot } 3757113afc8SEmmanuel Vadot 3767113afc8SEmmanuel Vadot static void 3777113afc8SEmmanuel Vadot xgbe_i2c_combined_isr(struct xgbe_prv_data *pdata) 3787113afc8SEmmanuel Vadot { 3797113afc8SEmmanuel Vadot xgbe_i2c_isr(pdata); 3807113afc8SEmmanuel Vadot } 3817113afc8SEmmanuel Vadot 3827113afc8SEmmanuel Vadot static int 3837113afc8SEmmanuel Vadot xgbe_i2c_xfer(struct xgbe_prv_data *pdata, struct xgbe_i2c_op *op) 3847113afc8SEmmanuel Vadot { 3857113afc8SEmmanuel Vadot struct xgbe_i2c_op_state *state = &pdata->i2c.op_state; 3867113afc8SEmmanuel Vadot unsigned long timeout; 3877113afc8SEmmanuel Vadot int ret; 3887113afc8SEmmanuel Vadot 3897113afc8SEmmanuel Vadot mtx_lock(&pdata->i2c_mutex); 3907113afc8SEmmanuel Vadot 3917113afc8SEmmanuel Vadot axgbe_printf(3, "i2c xfer started ---->>>\n"); 3927113afc8SEmmanuel Vadot 3937113afc8SEmmanuel Vadot ret = xgbe_i2c_disable(pdata); 3947113afc8SEmmanuel Vadot if (ret) { 3957113afc8SEmmanuel Vadot axgbe_error("failed to disable i2c master\n"); 3967113afc8SEmmanuel Vadot goto out; 3977113afc8SEmmanuel Vadot } 3987113afc8SEmmanuel Vadot 3997113afc8SEmmanuel Vadot xgbe_i2c_set_target(pdata, op->target); 4007113afc8SEmmanuel Vadot 4017113afc8SEmmanuel Vadot memset(state, 0, sizeof(*state)); 4027113afc8SEmmanuel Vadot state->op = op; 4037113afc8SEmmanuel Vadot state->tx_len = op->len; 4047113afc8SEmmanuel Vadot state->tx_buf = op->buf; 4057113afc8SEmmanuel Vadot state->rx_len = op->len; 4067113afc8SEmmanuel Vadot state->rx_buf = op->buf; 4077113afc8SEmmanuel Vadot 4087113afc8SEmmanuel Vadot xgbe_i2c_clear_all_interrupts(pdata); 4097113afc8SEmmanuel Vadot ret = xgbe_i2c_enable(pdata); 4107113afc8SEmmanuel Vadot if (ret) { 4117113afc8SEmmanuel Vadot axgbe_error("failed to enable i2c master\n"); 4127113afc8SEmmanuel Vadot goto out; 4137113afc8SEmmanuel Vadot } 4147113afc8SEmmanuel Vadot 4157113afc8SEmmanuel Vadot /* Enabling the interrupts will cause the TX FIFO empty interrupt to 4167113afc8SEmmanuel Vadot * fire and begin to process the command via the ISR. 4177113afc8SEmmanuel Vadot */ 4187113afc8SEmmanuel Vadot xgbe_i2c_enable_interrupts(pdata); 4197113afc8SEmmanuel Vadot 4207113afc8SEmmanuel Vadot timeout = ticks + (20 * hz); 4217113afc8SEmmanuel Vadot while (ticks < timeout) { 4227113afc8SEmmanuel Vadot 4237113afc8SEmmanuel Vadot if (!pdata->i2c_complete) { 4247113afc8SEmmanuel Vadot DELAY(200); 4257113afc8SEmmanuel Vadot continue; 4267113afc8SEmmanuel Vadot } 4277113afc8SEmmanuel Vadot 4287113afc8SEmmanuel Vadot axgbe_printf(1, "%s: I2C OP complete\n", __func__); 4297113afc8SEmmanuel Vadot break; 4307113afc8SEmmanuel Vadot } 4317113afc8SEmmanuel Vadot 4327113afc8SEmmanuel Vadot if ((ticks >= timeout) && !pdata->i2c_complete) { 4337113afc8SEmmanuel Vadot axgbe_error("%s: operation timed out\n", __func__); 4347113afc8SEmmanuel Vadot ret = -ETIMEDOUT; 4357113afc8SEmmanuel Vadot goto disable; 4367113afc8SEmmanuel Vadot } 4377113afc8SEmmanuel Vadot 4387113afc8SEmmanuel Vadot ret = state->ret; 4397113afc8SEmmanuel Vadot axgbe_printf(3, "%s: i2c xfer ret %d abrt_source 0x%x\n", __func__, 4407113afc8SEmmanuel Vadot ret, state->tx_abort_source); 4417113afc8SEmmanuel Vadot if (ret) { 442*445bed5cSStephan de Wit axgbe_printf(1, "%s: i2c xfer ret %d abrt_source 0x%x\n", __func__, 4437113afc8SEmmanuel Vadot ret, state->tx_abort_source); 4447113afc8SEmmanuel Vadot if (state->tx_abort_source & IC_TX_ABRT_7B_ADDR_NOACK) 4457113afc8SEmmanuel Vadot ret = -ENOTCONN; 4467113afc8SEmmanuel Vadot else if (state->tx_abort_source & IC_TX_ABRT_ARB_LOST) 4477113afc8SEmmanuel Vadot ret = -EAGAIN; 4487113afc8SEmmanuel Vadot } 4497113afc8SEmmanuel Vadot 4507113afc8SEmmanuel Vadot axgbe_printf(3, "i2c xfer finished ---->>>\n"); 4517113afc8SEmmanuel Vadot 4527113afc8SEmmanuel Vadot disable: 4537113afc8SEmmanuel Vadot pdata->i2c_complete = false; 4547113afc8SEmmanuel Vadot xgbe_i2c_disable_interrupts(pdata); 4557113afc8SEmmanuel Vadot xgbe_i2c_disable(pdata); 4567113afc8SEmmanuel Vadot 4577113afc8SEmmanuel Vadot out: 4587113afc8SEmmanuel Vadot mtx_unlock(&pdata->i2c_mutex); 4597113afc8SEmmanuel Vadot return (ret); 4607113afc8SEmmanuel Vadot } 4617113afc8SEmmanuel Vadot 4627113afc8SEmmanuel Vadot static void 4637113afc8SEmmanuel Vadot xgbe_i2c_stop(struct xgbe_prv_data *pdata) 4647113afc8SEmmanuel Vadot { 4657113afc8SEmmanuel Vadot if (!pdata->i2c.started) 4667113afc8SEmmanuel Vadot return; 4677113afc8SEmmanuel Vadot 4687113afc8SEmmanuel Vadot axgbe_printf(3, "stopping I2C\n"); 4697113afc8SEmmanuel Vadot 4707113afc8SEmmanuel Vadot pdata->i2c.started = 0; 4717113afc8SEmmanuel Vadot 4727113afc8SEmmanuel Vadot xgbe_i2c_disable_interrupts(pdata); 4737113afc8SEmmanuel Vadot xgbe_i2c_disable(pdata); 4747113afc8SEmmanuel Vadot xgbe_i2c_clear_all_interrupts(pdata); 4757113afc8SEmmanuel Vadot } 4767113afc8SEmmanuel Vadot 4777113afc8SEmmanuel Vadot static int 4787113afc8SEmmanuel Vadot xgbe_i2c_start(struct xgbe_prv_data *pdata) 4797113afc8SEmmanuel Vadot { 4807113afc8SEmmanuel Vadot if (pdata->i2c.started) 4817113afc8SEmmanuel Vadot return (0); 4827113afc8SEmmanuel Vadot 4837113afc8SEmmanuel Vadot pdata->i2c.started = 1; 4847113afc8SEmmanuel Vadot 4857113afc8SEmmanuel Vadot return (0); 4867113afc8SEmmanuel Vadot } 4877113afc8SEmmanuel Vadot 4887113afc8SEmmanuel Vadot static int 4897113afc8SEmmanuel Vadot xgbe_i2c_init(struct xgbe_prv_data *pdata) 4907113afc8SEmmanuel Vadot { 4917113afc8SEmmanuel Vadot int ret; 4927113afc8SEmmanuel Vadot 4937113afc8SEmmanuel Vadot /* initialize lock for i2c */ 4947113afc8SEmmanuel Vadot mtx_init(&pdata->i2c_mutex, "xgbe i2c mutex lock", NULL, MTX_DEF); 4957113afc8SEmmanuel Vadot pdata->i2c_complete = false; 4967113afc8SEmmanuel Vadot 4977113afc8SEmmanuel Vadot xgbe_i2c_disable_interrupts(pdata); 4987113afc8SEmmanuel Vadot 4997113afc8SEmmanuel Vadot ret = xgbe_i2c_disable(pdata); 5007113afc8SEmmanuel Vadot if (ret) { 5017113afc8SEmmanuel Vadot axgbe_error("failed to disable i2c master\n"); 5027113afc8SEmmanuel Vadot return (ret); 5037113afc8SEmmanuel Vadot } 5047113afc8SEmmanuel Vadot 5057113afc8SEmmanuel Vadot xgbe_i2c_get_features(pdata); 5067113afc8SEmmanuel Vadot 5077113afc8SEmmanuel Vadot xgbe_i2c_set_mode(pdata); 5087113afc8SEmmanuel Vadot 5097113afc8SEmmanuel Vadot xgbe_i2c_clear_all_interrupts(pdata); 5107113afc8SEmmanuel Vadot 5117113afc8SEmmanuel Vadot xgbe_dump_i2c_registers(pdata); 5127113afc8SEmmanuel Vadot 5137113afc8SEmmanuel Vadot return (0); 5147113afc8SEmmanuel Vadot } 5157113afc8SEmmanuel Vadot 5167113afc8SEmmanuel Vadot void 5177113afc8SEmmanuel Vadot xgbe_init_function_ptrs_i2c(struct xgbe_i2c_if *i2c_if) 5187113afc8SEmmanuel Vadot { 5197113afc8SEmmanuel Vadot i2c_if->i2c_init = xgbe_i2c_init; 5207113afc8SEmmanuel Vadot 5217113afc8SEmmanuel Vadot i2c_if->i2c_start = xgbe_i2c_start; 5227113afc8SEmmanuel Vadot i2c_if->i2c_stop = xgbe_i2c_stop; 5237113afc8SEmmanuel Vadot 5247113afc8SEmmanuel Vadot i2c_if->i2c_xfer = xgbe_i2c_xfer; 5257113afc8SEmmanuel Vadot 5267113afc8SEmmanuel Vadot i2c_if->i2c_isr = xgbe_i2c_combined_isr; 5277113afc8SEmmanuel Vadot } 528