1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
5 * Copyright (C) 2016-2020 Mellanox Technologies
8 #include <linux/delay.h>
10 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/platform_data/mlxreg.h>
15 #include <linux/platform_device.h>
16 #include <linux/regmap.h>
19 #define MLXPLAT_CPLD_LPC_I2C_BASE_ADDR 0x2000
20 #define MLXCPLD_I2C_DEVICE_NAME "i2c_mlxcpld"
21 #define MLXCPLD_I2C_VALID_FLAG (I2C_M_RECV_LEN | I2C_M_RD)
22 #define MLXCPLD_I2C_BUS_NUM 1
23 #define MLXCPLD_I2C_DATA_REG_SZ 36
24 #define MLXCPLD_I2C_DATA_SZ_BIT BIT(5)
25 #define MLXCPLD_I2C_DATA_SZ_MASK GENMASK(6, 5)
26 #define MLXCPLD_I2C_SMBUS_BLK_BIT BIT(7)
27 #define MLXCPLD_I2C_MAX_ADDR_LEN 4
28 #define MLXCPLD_I2C_RETR_NUM 2
29 #define MLXCPLD_I2C_XFER_TO 500000 /* usec */
30 #define MLXCPLD_I2C_POLL_TIME 200 /* usec */
32 /* LPC I2C registers */
33 #define MLXCPLD_LPCI2C_CPBLTY_REG 0x0
34 #define MLXCPLD_LPCI2C_CTRL_REG 0x1
35 #define MLXCPLD_LPCI2C_HALF_CYC_REG 0x4
36 #define MLXCPLD_LPCI2C_I2C_HOLD_REG 0x5
37 #define MLXCPLD_LPCI2C_CMD_REG 0x6
38 #define MLXCPLD_LPCI2C_NUM_DAT_REG 0x7
39 #define MLXCPLD_LPCI2C_NUM_ADDR_REG 0x8
40 #define MLXCPLD_LPCI2C_STATUS_REG 0x9
41 #define MLXCPLD_LPCI2C_DATA_REG 0xa
43 /* LPC I2C masks and parametres */
44 #define MLXCPLD_LPCI2C_RST_SEL_MASK 0x1
45 #define MLXCPLD_LPCI2C_TRANS_END 0x1
46 #define MLXCPLD_LPCI2C_STATUS_NACK 0x10
47 #define MLXCPLD_LPCI2C_NO_IND 0
48 #define MLXCPLD_LPCI2C_ACK_IND 1
49 #define MLXCPLD_LPCI2C_NACK_IND 2
51 #define MLXCPLD_I2C_FREQ_1000KHZ_SET 0x04
52 #define MLXCPLD_I2C_FREQ_400KHZ_SET 0x0c
53 #define MLXCPLD_I2C_FREQ_100KHZ_SET 0x42
55 enum mlxcpld_i2c_frequency {
56 MLXCPLD_I2C_FREQ_1000KHZ = 1,
57 MLXCPLD_I2C_FREQ_400KHZ = 2,
58 MLXCPLD_I2C_FREQ_100KHZ = 3,
61 struct mlxcpld_i2c_curr_xfer {
69 struct mlxcpld_i2c_priv {
70 struct i2c_adapter adap;
73 struct mlxcpld_i2c_curr_xfer xfer;
79 static void mlxcpld_i2c_lpc_write_buf(u8 *data, u8 len, u32 addr)
83 for (i = 0; i < len - len % 4; i += 4)
84 outl(*(u32 *)(data + i), addr + i);
86 outb(*(data + i), addr + i);
89 static void mlxcpld_i2c_lpc_read_buf(u8 *data, u8 len, u32 addr)
93 for (i = 0; i < len - len % 4; i += 4)
94 *(u32 *)(data + i) = inl(addr + i);
96 *(data + i) = inb(addr + i);
99 static void mlxcpld_i2c_read_comm(struct mlxcpld_i2c_priv *priv, u8 offs,
100 u8 *data, u8 datalen)
102 u32 addr = priv->base_addr + offs;
109 *((u16 *)data) = inw(addr);
112 *((u16 *)data) = inw(addr);
113 *(data + 2) = inb(addr + 2);
116 *((u32 *)data) = inl(addr);
119 mlxcpld_i2c_lpc_read_buf(data, datalen, addr);
124 static void mlxcpld_i2c_write_comm(struct mlxcpld_i2c_priv *priv, u8 offs,
125 u8 *data, u8 datalen)
127 u32 addr = priv->base_addr + offs;
134 outw(*((u16 *)data), addr);
137 outw(*((u16 *)data), addr);
138 outb(*(data + 2), addr + 2);
141 outl(*((u32 *)data), addr);
144 mlxcpld_i2c_lpc_write_buf(data, datalen, addr);
150 * Check validity of received i2c messages parameters.
151 * Returns 0 if OK, other - in case of invalid parameters.
153 static int mlxcpld_i2c_check_msg_params(struct mlxcpld_i2c_priv *priv,
154 struct i2c_msg *msgs, int num)
159 dev_err(priv->dev, "Incorrect 0 num of messages\n");
163 if (unlikely(msgs[0].addr > 0x7f)) {
164 dev_err(priv->dev, "Invalid address 0x%03x\n",
169 for (i = 0; i < num; ++i) {
170 if (unlikely(!msgs[i].buf)) {
171 dev_err(priv->dev, "Invalid buf in msg[%d]\n",
175 if (unlikely(msgs[0].addr != msgs[i].addr)) {
176 dev_err(priv->dev, "Invalid addr in msg[%d]\n",
186 * Check if transfer is completed and status of operation.
187 * Returns 0 - transfer completed (both ACK or NACK),
188 * negative - transfer isn't finished.
190 static int mlxcpld_i2c_check_status(struct mlxcpld_i2c_priv *priv, int *status)
194 mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_STATUS_REG, &val, 1);
196 if (val & MLXCPLD_LPCI2C_TRANS_END) {
197 if (val & MLXCPLD_LPCI2C_STATUS_NACK)
199 * The slave is unable to accept the data. No such
200 * slave, command not understood, or unable to accept
203 *status = MLXCPLD_LPCI2C_NACK_IND;
205 *status = MLXCPLD_LPCI2C_ACK_IND;
208 *status = MLXCPLD_LPCI2C_NO_IND;
213 static void mlxcpld_i2c_set_transf_data(struct mlxcpld_i2c_priv *priv,
214 struct i2c_msg *msgs, int num,
217 priv->xfer.msg = msgs;
218 priv->xfer.msg_num = num;
221 * All upper layers currently are never use transfer with more than
222 * 2 messages. Actually, it's also not so relevant in Mellanox systems
223 * because of HW limitation. Max size of transfer is not more than 32
224 * or 68 bytes in the current x86 LPCI2C bridge.
226 priv->xfer.cmd = msgs[num - 1].flags & I2C_M_RD;
228 if (priv->xfer.cmd == I2C_M_RD && comm_len != msgs[0].len) {
229 priv->xfer.addr_width = msgs[0].len;
230 priv->xfer.data_len = comm_len - priv->xfer.addr_width;
232 priv->xfer.addr_width = 0;
233 priv->xfer.data_len = comm_len;
237 /* Reset CPLD LPCI2C block */
238 static void mlxcpld_i2c_reset(struct mlxcpld_i2c_priv *priv)
242 mutex_lock(&priv->lock);
244 mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_CTRL_REG, &val, 1);
245 val &= ~MLXCPLD_LPCI2C_RST_SEL_MASK;
246 mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_CTRL_REG, &val, 1);
248 mutex_unlock(&priv->lock);
251 /* Make sure the CPLD is ready to start transmitting. */
252 static int mlxcpld_i2c_check_busy(struct mlxcpld_i2c_priv *priv)
256 mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_STATUS_REG, &val, 1);
258 if (val & MLXCPLD_LPCI2C_TRANS_END)
264 static int mlxcpld_i2c_wait_for_free(struct mlxcpld_i2c_priv *priv)
269 if (!mlxcpld_i2c_check_busy(priv))
271 usleep_range(priv->polling_time / 2, priv->polling_time);
272 timeout += priv->polling_time;
273 } while (timeout <= MLXCPLD_I2C_XFER_TO);
275 if (timeout > MLXCPLD_I2C_XFER_TO)
282 * Wait for master transfer to complete.
283 * It puts current process to sleep until we get interrupt or timeout expires.
284 * Returns the number of transferred or read bytes or error (<0).
286 static int mlxcpld_i2c_wait_for_tc(struct mlxcpld_i2c_priv *priv)
288 int status, i, timeout = 0;
292 usleep_range(priv->polling_time / 2, priv->polling_time);
293 if (!mlxcpld_i2c_check_status(priv, &status))
295 timeout += priv->polling_time;
296 } while (status == 0 && timeout < MLXCPLD_I2C_XFER_TO);
299 case MLXCPLD_LPCI2C_NO_IND:
302 case MLXCPLD_LPCI2C_ACK_IND:
303 if (priv->xfer.cmd != I2C_M_RD)
304 return (priv->xfer.addr_width + priv->xfer.data_len);
306 if (priv->xfer.msg_num == 1)
311 if (!priv->xfer.msg[i].buf)
315 * Actual read data len will be always the same as
316 * requested len. 0xff (line pull-up) will be returned
317 * if slave has no data to return. Thus don't read
318 * MLXCPLD_LPCI2C_NUM_DAT_REG reg from CPLD. Only in case of
319 * SMBus block read transaction data len can be different,
322 mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_NUM_ADDR_REG, &val,
324 if (priv->smbus_block && (val & MLXCPLD_I2C_SMBUS_BLK_BIT)) {
325 mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_NUM_DAT_REG,
327 if (unlikely(datalen > I2C_SMBUS_BLOCK_MAX)) {
328 dev_err(priv->dev, "Incorrect smbus block read message len\n");
332 datalen = priv->xfer.data_len;
335 mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_DATA_REG,
336 priv->xfer.msg[i].buf, datalen);
340 case MLXCPLD_LPCI2C_NACK_IND:
348 static void mlxcpld_i2c_xfer_msg(struct mlxcpld_i2c_priv *priv)
353 mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_NUM_DAT_REG,
354 &priv->xfer.data_len, 1);
356 val = priv->xfer.addr_width;
357 /* Notify HW about SMBus block read transaction */
358 if (priv->smbus_block && priv->xfer.msg_num >= 2 &&
359 priv->xfer.msg[1].len == 1 &&
360 (priv->xfer.msg[1].flags & I2C_M_RECV_LEN) &&
361 (priv->xfer.msg[1].flags & I2C_M_RD))
362 val |= MLXCPLD_I2C_SMBUS_BLK_BIT;
364 mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_NUM_ADDR_REG, &val, 1);
366 for (i = 0; i < priv->xfer.msg_num; i++) {
367 if ((priv->xfer.msg[i].flags & I2C_M_RD) != I2C_M_RD) {
368 /* Don't write to CPLD buffer in read transaction */
369 mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_DATA_REG +
370 len, priv->xfer.msg[i].buf,
371 priv->xfer.msg[i].len);
372 len += priv->xfer.msg[i].len;
377 * Set target slave address with command for master transfer.
378 * It should be latest executed function before CPLD transaction.
380 cmd = (priv->xfer.msg[0].addr << 1) | priv->xfer.cmd;
381 mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_CMD_REG, &cmd, 1);
385 * Generic lpc-i2c transfer.
386 * Returns the number of processed messages or error (<0).
388 static int mlxcpld_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
391 struct mlxcpld_i2c_priv *priv = i2c_get_adapdata(adap);
395 err = mlxcpld_i2c_check_msg_params(priv, msgs, num);
397 dev_err(priv->dev, "Incorrect message\n");
401 for (i = 0; i < num; ++i)
402 comm_len += msgs[i].len;
404 /* Check bus state */
405 if (mlxcpld_i2c_wait_for_free(priv)) {
406 dev_err(priv->dev, "LPCI2C bridge is busy\n");
409 * Usually it means something serious has happened.
410 * We can not have unfinished previous transfer
411 * so it doesn't make any sense to try to stop it.
412 * Probably we were not able to recover from the
414 * The only reasonable thing - is soft reset.
416 mlxcpld_i2c_reset(priv);
417 if (mlxcpld_i2c_check_busy(priv)) {
418 dev_err(priv->dev, "LPCI2C bridge is busy after reset\n");
423 mlxcpld_i2c_set_transf_data(priv, msgs, num, comm_len);
425 mutex_lock(&priv->lock);
427 /* Do real transfer. Can't fail */
428 mlxcpld_i2c_xfer_msg(priv);
430 /* Wait for transaction complete */
431 err = mlxcpld_i2c_wait_for_tc(priv);
433 mutex_unlock(&priv->lock);
435 return err < 0 ? err : num;
438 static u32 mlxcpld_i2c_func(struct i2c_adapter *adap)
440 struct mlxcpld_i2c_priv *priv = i2c_get_adapdata(adap);
442 if (priv->smbus_block)
443 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
444 I2C_FUNC_SMBUS_I2C_BLOCK | I2C_FUNC_SMBUS_BLOCK_DATA;
446 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
447 I2C_FUNC_SMBUS_I2C_BLOCK;
450 static const struct i2c_algorithm mlxcpld_i2c_algo = {
451 .master_xfer = mlxcpld_i2c_xfer,
452 .functionality = mlxcpld_i2c_func
455 static const struct i2c_adapter_quirks mlxcpld_i2c_quirks = {
456 .flags = I2C_AQ_COMB_WRITE_THEN_READ,
457 .max_read_len = MLXCPLD_I2C_DATA_REG_SZ - MLXCPLD_I2C_MAX_ADDR_LEN,
458 .max_write_len = MLXCPLD_I2C_DATA_REG_SZ,
459 .max_comb_1st_msg_len = 4,
462 static const struct i2c_adapter_quirks mlxcpld_i2c_quirks_ext = {
463 .flags = I2C_AQ_COMB_WRITE_THEN_READ,
464 .max_read_len = MLXCPLD_I2C_DATA_REG_SZ * 2 - MLXCPLD_I2C_MAX_ADDR_LEN,
465 .max_write_len = MLXCPLD_I2C_DATA_REG_SZ * 2,
466 .max_comb_1st_msg_len = 4,
469 static struct i2c_adapter mlxcpld_i2c_adapter = {
470 .owner = THIS_MODULE,
471 .name = "i2c-mlxcpld",
472 .class = I2C_CLASS_HWMON | I2C_CLASS_SPD,
473 .algo = &mlxcpld_i2c_algo,
474 .quirks = &mlxcpld_i2c_quirks,
475 .retries = MLXCPLD_I2C_RETR_NUM,
476 .nr = MLXCPLD_I2C_BUS_NUM,
480 mlxcpld_i2c_set_frequency(struct mlxcpld_i2c_priv *priv,
481 struct mlxreg_core_hotplug_platform_data *pdata)
483 struct mlxreg_core_item *item = pdata->items;
484 struct mlxreg_core_data *data;
492 /* Read frequency setting. */
494 err = regmap_read(pdata->regmap, data->reg, ®val);
498 /* Set frequency only if it is not 100KHz, which is default. */
499 switch ((regval & data->mask) >> data->bit) {
500 case MLXCPLD_I2C_FREQ_1000KHZ:
501 freq = MLXCPLD_I2C_FREQ_1000KHZ_SET;
502 priv->polling_time /= 4;
504 case MLXCPLD_I2C_FREQ_400KHZ:
505 freq = MLXCPLD_I2C_FREQ_400KHZ_SET;
506 priv->polling_time /= 4;
512 mlxcpld_i2c_write_comm(priv, MLXCPLD_LPCI2C_HALF_CYC_REG, &freq, 1);
517 static int mlxcpld_i2c_probe(struct platform_device *pdev)
519 struct mlxreg_core_hotplug_platform_data *pdata;
520 struct mlxcpld_i2c_priv *priv;
524 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
528 mutex_init(&priv->lock);
529 platform_set_drvdata(pdev, priv);
531 priv->dev = &pdev->dev;
532 priv->base_addr = MLXPLAT_CPLD_LPC_I2C_BASE_ADDR;
533 priv->polling_time = MLXCPLD_I2C_POLL_TIME;
535 /* Set I2C bus frequency if platform data provides this info. */
536 pdata = dev_get_platdata(&pdev->dev);
538 err = mlxcpld_i2c_set_frequency(priv, pdata);
540 goto mlxcpld_i2_probe_failed;
543 /* Register with i2c layer */
544 mlxcpld_i2c_adapter.timeout = usecs_to_jiffies(MLXCPLD_I2C_XFER_TO);
545 /* Read capability register */
546 mlxcpld_i2c_read_comm(priv, MLXCPLD_LPCI2C_CPBLTY_REG, &val, 1);
547 /* Check support for extended transaction length */
548 if ((val & MLXCPLD_I2C_DATA_SZ_MASK) == MLXCPLD_I2C_DATA_SZ_BIT)
549 mlxcpld_i2c_adapter.quirks = &mlxcpld_i2c_quirks_ext;
550 /* Check support for smbus block transaction */
551 if (val & MLXCPLD_I2C_SMBUS_BLK_BIT)
552 priv->smbus_block = true;
554 mlxcpld_i2c_adapter.nr = pdev->id;
555 priv->adap = mlxcpld_i2c_adapter;
556 priv->adap.dev.parent = &pdev->dev;
557 i2c_set_adapdata(&priv->adap, priv);
559 err = i2c_add_numbered_adapter(&priv->adap);
561 goto mlxcpld_i2_probe_failed;
565 mlxcpld_i2_probe_failed:
566 mutex_destroy(&priv->lock);
570 static int mlxcpld_i2c_remove(struct platform_device *pdev)
572 struct mlxcpld_i2c_priv *priv = platform_get_drvdata(pdev);
574 i2c_del_adapter(&priv->adap);
575 mutex_destroy(&priv->lock);
580 static struct platform_driver mlxcpld_i2c_driver = {
581 .probe = mlxcpld_i2c_probe,
582 .remove = mlxcpld_i2c_remove,
584 .name = MLXCPLD_I2C_DEVICE_NAME,
588 module_platform_driver(mlxcpld_i2c_driver);
591 MODULE_DESCRIPTION("Mellanox I2C-CPLD controller driver");
592 MODULE_LICENSE("Dual BSD/GPL");
593 MODULE_ALIAS("platform:i2c-mlxcpld");