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 axgbe_i2c_clear_isr_interrupts(pdata, isr);
167 if (isr & AXGBE_INTR_TX_ABRT) {
168 axgbe_i2c_disable_interrupts(pdata);
174 /* Check for data in the Rx fifo */
175 axgbe_i2c_read(pdata);
177 /* Fill up the Tx fifo next */
178 axgbe_i2c_write(pdata);
181 /* Complete on an error or STOP condition */
182 if (state->ret || XI2C_GET_BITS(isr, IC_RAW_INTR_STAT, STOP_DET))
188 static void axgbe_i2c_set_mode(struct axgbe_port *pdata)
192 reg = XI2C_IOREAD(pdata, IC_CON);
193 XI2C_SET_BITS(reg, IC_CON, MASTER_MODE, 1);
194 XI2C_SET_BITS(reg, IC_CON, SLAVE_DISABLE, 1);
195 XI2C_SET_BITS(reg, IC_CON, RESTART_EN, 1);
196 XI2C_SET_BITS(reg, IC_CON, SPEED, AXGBE_STD_SPEED);
197 XI2C_SET_BITS(reg, IC_CON, RX_FIFO_FULL_HOLD, 1);
198 XI2C_IOWRITE(pdata, IC_CON, reg);
201 static void axgbe_i2c_get_features(struct axgbe_port *pdata)
203 struct axgbe_i2c *i2c = &pdata->i2c;
206 reg = XI2C_IOREAD(pdata, IC_COMP_PARAM_1);
207 i2c->max_speed_mode = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
209 i2c->rx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
211 i2c->tx_fifo_size = XI2C_GET_BITS(reg, IC_COMP_PARAM_1,
215 static void axgbe_i2c_set_target(struct axgbe_port *pdata, unsigned int addr)
217 XI2C_IOWRITE(pdata, IC_TAR, addr);
220 static int axgbe_i2c_xfer(struct axgbe_port *pdata, struct axgbe_i2c_op *op)
222 struct axgbe_i2c_op_state *state = &pdata->i2c.op_state;
226 pthread_mutex_lock(&pdata->i2c_mutex);
227 ret = axgbe_i2c_disable(pdata);
229 PMD_DRV_LOG(ERR, "failed to disable i2c master\n");
233 axgbe_i2c_set_target(pdata, op->target);
235 memset(state, 0, sizeof(*state));
237 state->tx_len = op->len;
238 state->tx_buf = (unsigned char *)op->buf;
239 state->rx_len = op->len;
240 state->rx_buf = (unsigned char *)op->buf;
242 axgbe_i2c_clear_all_interrupts(pdata);
243 ret = axgbe_i2c_enable(pdata);
245 PMD_DRV_LOG(ERR, "failed to enable i2c master\n");
249 /* Enabling the interrupts will cause the TX FIFO empty interrupt to
250 * fire and begin to process the command via the ISR.
252 axgbe_i2c_enable_interrupts(pdata);
253 timeout = rte_get_timer_cycles() + rte_get_timer_hz();
255 while (time_before(rte_get_timer_cycles(), timeout)) {
257 if (XI2C_IOREAD(pdata, IC_RAW_INTR_STAT)) {
258 if (axgbe_i2c_isr(pdata))
263 PMD_DRV_LOG(ERR, "i2c operation timed out\n");
264 axgbe_i2c_disable_interrupts(pdata);
265 axgbe_i2c_disable(pdata);
272 if (state->tx_abort_source & IC_TX_ABRT_7B_ADDR_NOACK)
274 else if (state->tx_abort_source & IC_TX_ABRT_ARB_LOST)
279 pthread_mutex_unlock(&pdata->i2c_mutex);
283 static void axgbe_i2c_stop(struct axgbe_port *pdata)
285 if (!pdata->i2c.started)
288 pdata->i2c.started = 0;
289 axgbe_i2c_disable_interrupts(pdata);
290 axgbe_i2c_disable(pdata);
291 axgbe_i2c_clear_all_interrupts(pdata);
294 static int axgbe_i2c_start(struct axgbe_port *pdata)
296 if (pdata->i2c.started)
299 pdata->i2c.started = 1;
304 static int axgbe_i2c_init(struct axgbe_port *pdata)
308 axgbe_i2c_disable_interrupts(pdata);
310 ret = axgbe_i2c_disable(pdata);
312 PMD_DRV_LOG(ERR, "failed to disable i2c master\n");
316 axgbe_i2c_get_features(pdata);
318 axgbe_i2c_set_mode(pdata);
320 axgbe_i2c_clear_all_interrupts(pdata);
325 void axgbe_init_function_ptrs_i2c(struct axgbe_i2c_if *i2c_if)
327 i2c_if->i2c_init = axgbe_i2c_init;
328 i2c_if->i2c_start = axgbe_i2c_start;
329 i2c_if->i2c_stop = axgbe_i2c_stop;
330 i2c_if->i2c_xfer = axgbe_i2c_xfer;