1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Advanced Micro Devices, Inc. All rights reserved.
3 * Copyright(c) 2018 Synopsys, Inc. All rights reserved.
6 #include "axgbe_ethdev.h"
7 #include "axgbe_common.h"
9 #define AXGBE_ABORT_COUNT 500
10 #define AXGBE_DISABLE_COUNT 1000
12 #define AXGBE_STD_SPEED 1
14 #define AXGBE_INTR_RX_FULL BIT(IC_RAW_INTR_STAT_RX_FULL_INDEX)
15 #define AXGBE_INTR_TX_EMPTY BIT(IC_RAW_INTR_STAT_TX_EMPTY_INDEX)
16 #define AXGBE_INTR_TX_ABRT BIT(IC_RAW_INTR_STAT_TX_ABRT_INDEX)
17 #define AXGBE_INTR_STOP_DET BIT(IC_RAW_INTR_STAT_STOP_DET_INDEX)
18 #define AXGBE_DEFAULT_INT_MASK (AXGBE_INTR_RX_FULL | \
19 AXGBE_INTR_TX_EMPTY | \
20 AXGBE_INTR_TX_ABRT | \
23 #define AXGBE_I2C_READ BIT(8)
24 #define AXGBE_I2C_STOP BIT(9)
26 static int axgbe_i2c_abort(struct axgbe_port *pdata)
28 unsigned int wait = AXGBE_ABORT_COUNT;
30 /* Must be enabled to recognize the abort request */
31 XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, 1);
34 XI2C_IOWRITE_BITS(pdata, IC_ENABLE, ABORT, 1);
37 if (!XI2C_IOREAD_BITS(pdata, IC_ENABLE, ABORT))
45 static int axgbe_i2c_set_enable(struct axgbe_port *pdata, bool enable)
47 unsigned int wait = AXGBE_DISABLE_COUNT;
48 unsigned int mode = enable ? 1 : 0;
51 XI2C_IOWRITE_BITS(pdata, IC_ENABLE, EN, mode);
52 if (XI2C_IOREAD_BITS(pdata, IC_ENABLE_STATUS, EN) == mode)
61 static int axgbe_i2c_disable(struct axgbe_port *pdata)
65 ret = axgbe_i2c_set_enable(pdata, false);
67 /* Disable failed, try an abort */
68 ret = axgbe_i2c_abort(pdata);
72 /* Abort succeeded, try to disable again */
73 ret = axgbe_i2c_set_enable(pdata, false);
79 static int axgbe_i2c_enable(struct axgbe_port *pdata)
81 return axgbe_i2c_set_enable(pdata, true);
84 static void axgbe_i2c_clear_all_interrupts(struct axgbe_port *pdata)
86 XI2C_IOREAD(pdata, IC_CLR_INTR);
89 static void axgbe_i2c_disable_interrupts(struct axgbe_port *pdata)
91 XI2C_IOWRITE(pdata, IC_INTR_MASK, 0);
94 static void axgbe_i2c_enable_interrupts(struct axgbe_port *pdata)
96 XI2C_IOWRITE(pdata, IC_INTR_MASK, AXGBE_DEFAULT_INT_MASK);
99 static void axgbe_i2c_write(struct axgbe_port *pdata)
101 struct axgbe_i2c_op_state *state = &pdata->i2c.op_state;
102 unsigned int tx_slots;
105 /* Configured to never receive Rx overflows, so fill up Tx fifo */
106 tx_slots = pdata->i2c.tx_fifo_size - XI2C_IOREAD(pdata, IC_TXFLR);
107 while (tx_slots && state->tx_len) {
108 if (state->op->cmd == AXGBE_I2C_CMD_READ)
109 cmd = AXGBE_I2C_READ;
111 cmd = *state->tx_buf++;
113 if (state->tx_len == 1)
114 XI2C_SET_BITS(cmd, IC_DATA_CMD, STOP, 1);
116 XI2C_IOWRITE(pdata, IC_DATA_CMD, cmd);
122 /* No more Tx operations, so ignore TX_EMPTY and return */
124 XI2C_IOWRITE_BITS(pdata, IC_INTR_MASK, TX_EMPTY, 0);
127 static void axgbe_i2c_read(struct axgbe_port *pdata)
129 struct axgbe_i2c_op_state *state = &pdata->i2c.op_state;
130 unsigned int rx_slots;
132 /* Anything to be read? */
133 if (state->op->cmd != AXGBE_I2C_CMD_READ)
136 rx_slots = XI2C_IOREAD(pdata, IC_RXFLR);
137 while (rx_slots && state->rx_len) {
138 *state->rx_buf++ = XI2C_IOREAD(pdata, IC_DATA_CMD);
144 static void axgbe_i2c_clear_isr_interrupts(struct axgbe_port *pdata,
147 struct axgbe_i2c_op_state *state = &pdata->i2c.op_state;
149 if (isr & AXGBE_INTR_TX_ABRT) {
150 state->tx_abort_source = XI2C_IOREAD(pdata, IC_TX_ABRT_SOURCE);
151 XI2C_IOREAD(pdata, IC_CLR_TX_ABRT);
154 if (isr & AXGBE_INTR_STOP_DET)
155 XI2C_IOREAD(pdata, IC_CLR_STOP_DET);
158 static int axgbe_i2c_isr(struct axgbe_port *pdata)
160 struct axgbe_i2c_op_state *state = &pdata->i2c.op_state;
163 isr = XI2C_IOREAD(pdata, IC_RAW_INTR_STAT);
165 PMD_DRV_LOG(DEBUG, "I2C interrupt received: status=%#010x\n", isr);
167 axgbe_i2c_clear_isr_interrupts(pdata, isr);
169 if (isr & AXGBE_INTR_TX_ABRT) {
171 "I2C TX_ABRT received (%#010x) for target %#04x\n",
172 state->tx_abort_source, state->op->target);
174 axgbe_i2c_disable_interrupts(pdata);
180 /* Check for data in the Rx fifo */
181 axgbe_i2c_read(pdata);
183 /* Fill up the Tx fifo next */
184 axgbe_i2c_write(pdata);
187 /* Complete on an error or STOP condition */
188 if (state->ret || XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET))
194 static void axgbe_i2c_set_mode(struct axgbe_port *pdata)
198 reg = XI2C_IOREAD(pdata, IC_CON);
199 XI2C_SET_BITS(reg, IC_CON, MASTER_MODE, 1);
200 XI2C_SET_BITS(reg, IC_CON, SLAVE_DISABLE, 1);
201 XI2C_SET_BITS(reg, IC_CON, RESTART_EN, 1);
202 XI2C_SET_BITS(reg, IC_CON, SPEED, AXGBE_STD_SPEED);
203 XI2C_SET_BITS(reg, IC_CON, RX_FIFO_FULL_HOLD, 1);
204 XI2C_IOWRITE(pdata, IC_CON, reg);
207 static void axgbe_i2c_get_features(struct axgbe_port *pdata)
209 struct axgbe_i2c *i2c = &pdata->i2c;
212 reg = XI2C_IOREAD(pdata, IC_COMP_PARAM_1);
213 i2c->max_speed_mode = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
215 i2c->rx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
217 i2c->tx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
221 static void axgbe_i2c_set_target(struct axgbe_port *pdata, unsigned int addr)
223 XI2C_IOWRITE(pdata, IC_TAR, addr);
226 static int axgbe_i2c_xfer(struct axgbe_port *pdata, struct axgbe_i2c_op *op)
228 struct axgbe_i2c_op_state *state = &pdata->i2c.op_state;
232 pthread_mutex_lock(&pdata->i2c_mutex);
233 ret = axgbe_i2c_disable(pdata);
235 PMD_DRV_LOG(ERR, "failed to disable i2c master\n");
236 pthread_mutex_unlock(&pdata->i2c_mutex);
240 axgbe_i2c_set_target(pdata, op->target);
242 memset(state, 0, sizeof(*state));
244 state->tx_len = op->len;
245 state->tx_buf = (unsigned char *)op->buf;
246 state->rx_len = op->len;
247 state->rx_buf = (unsigned char *)op->buf;
249 axgbe_i2c_clear_all_interrupts(pdata);
250 ret = axgbe_i2c_enable(pdata);
252 PMD_DRV_LOG(ERR, "failed to enable i2c master\n");
253 pthread_mutex_unlock(&pdata->i2c_mutex);
257 /* Enabling the interrupts will cause the TX FIFO empty interrupt to
258 * fire and begin to process the command via the ISR.
260 axgbe_i2c_enable_interrupts(pdata);
261 timeout = rte_get_timer_cycles() + rte_get_timer_hz();
263 while (time_before(rte_get_timer_cycles(), timeout)) {
265 if (XI2C_IOREAD(pdata, IC_RAW_INTR_STAT)) {
266 if (axgbe_i2c_isr(pdata))
271 PMD_DRV_LOG(ERR, "i2c operation timed out\n");
272 axgbe_i2c_disable_interrupts(pdata);
273 axgbe_i2c_disable(pdata);
280 if (state->tx_abort_source & IC_TX_ABRT_7B_ADDR_NOACK)
282 else if (state->tx_abort_source & IC_TX_ABRT_ARB_LOST)
287 pthread_mutex_unlock(&pdata->i2c_mutex);
291 static void axgbe_i2c_stop(struct axgbe_port *pdata)
293 if (!pdata->i2c.started)
296 PMD_DRV_LOG(DEBUG, "stopping I2C\n");
298 pdata->i2c.started = 0;
299 axgbe_i2c_disable_interrupts(pdata);
300 axgbe_i2c_disable(pdata);
301 axgbe_i2c_clear_all_interrupts(pdata);
304 static int axgbe_i2c_start(struct axgbe_port *pdata)
306 if (pdata->i2c.started)
309 PMD_DRV_LOG(DEBUG, "starting I2C\n");
311 pdata->i2c.started = 1;
316 static int axgbe_i2c_init(struct axgbe_port *pdata)
320 axgbe_i2c_disable_interrupts(pdata);
322 ret = axgbe_i2c_disable(pdata);
324 PMD_DRV_LOG(ERR, "failed to disable i2c master\n");
328 axgbe_i2c_get_features(pdata);
330 axgbe_i2c_set_mode(pdata);
332 axgbe_i2c_clear_all_interrupts(pdata);
337 void axgbe_init_function_ptrs_i2c(struct axgbe_i2c_if *i2c_if)
339 i2c_if->i2c_init = axgbe_i2c_init;
340 i2c_if->i2c_start = axgbe_i2c_start;
341 i2c_if->i2c_stop = axgbe_i2c_stop;
342 i2c_if->i2c_xfer = axgbe_i2c_xfer;