1 // SPDX-License-Identifier: GPL-2.0+
3 * Microchip I2C controller driver
5 * Copyright (C) 2021-2022 Microchip Technology Inc.
6 * Padmarao Begari <padmarao.begari@microchip.com>
7 * Conor Dooley <conor.dooley@microchip.com>
14 #include <dm/device_compat.h>
15 #include <linux/bitops.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
19 #define MICROCHIP_I2C_TIMEOUT (1000 * 60)
21 #define MPFS_I2C_CTRL (0x00)
22 #define CTRL_CR0 (0x00)
23 #define CTRL_CR1 (0x01)
24 #define CTRL_AA BIT(2)
25 #define CTRL_SI BIT(3)
26 #define CTRL_STO BIT(4)
27 #define CTRL_STA BIT(5)
28 #define CTRL_ENS1 BIT(6)
29 #define CTRL_CR2 (0x07)
30 #define MPFS_I2C_STATUS (0x04)
31 #define STATUS_BUS_ERROR (0x00)
32 #define STATUS_M_START_SENT (0x08)
33 #define STATUS_M_REPEATED_START_SENT (0x10)
34 #define STATUS_M_SLAW_ACK (0x18)
35 #define STATUS_M_SLAW_NACK (0x20)
36 #define STATUS_M_TX_DATA_ACK (0x28)
37 #define STATUS_M_TX_DATA_NACK (0x30)
38 #define STATUS_M_ARB_LOST (0x38)
39 #define STATUS_M_SLAR_ACK (0x40)
40 #define STATUS_M_SLAR_NACK (0x48)
41 #define STATUS_M_RX_DATA_ACKED (0x50)
42 #define STATUS_M_RX_DATA_NACKED (0x58)
43 #define STATUS_S_SLAW_ACKED (0x60)
44 #define STATUS_S_ARB_LOST_SLAW_ACKED (0x68)
45 #define STATUS_S_GENERAL_CALL_ACKED (0x70)
46 #define STATUS_S_ARB_LOST_GENERAL_CALL_ACKED (0x78)
47 #define STATUS_S_RX_DATA_ACKED (0x80)
48 #define STATUS_S_RX_DATA_NACKED (0x88)
49 #define STATUS_S_GENERAL_CALL_RX_DATA_ACKED (0x90)
50 #define STATUS_S_GENERAL_CALL_RX_DATA_NACKED (0x98)
51 #define STATUS_S_RX_STOP (0xA0)
52 #define STATUS_S_SLAR_ACKED (0xA8)
53 #define STATUS_S_ARB_LOST_SLAR_ACKED (0xB0)
54 #define STATUS_S_TX_DATA_ACK (0xb8)
55 #define STATUS_S_TX_DATA_NACK (0xC0)
56 #define STATUS_LAST_DATA_ACK (0xC8)
57 #define STATUS_M_SMB_MASTER_RESET (0xD0)
58 #define STATUS_S_SCL_LOW_TIMEOUT (0xD8)
59 #define STATUS_NO_STATE_INFO (0xF8)
60 #define MPFS_I2C_DATA (0x08)
61 #define MPFS_I2C_SLAVE0_ADDR (0x0c)
62 #define MPFS_I2C_SMBUS (0x10)
63 #define MPFS_I2C_FREQ (0x14)
64 #define MPFS_I2C_GLITCHREG (0x18)
65 #define MPFS_I2C_SLAVE1_ADDR (0x1c)
67 #define PCLK_DIV_256 ((0 << CTRL_CR0) | (0 << CTRL_CR1) | (0 << CTRL_CR2))
68 #define PCLK_DIV_224 ((1 << CTRL_CR0) | (0 << CTRL_CR1) | (0 << CTRL_CR2))
69 #define PCLK_DIV_192 ((0 << CTRL_CR0) | (1 << CTRL_CR1) | (0 << CTRL_CR2))
70 #define PCLK_DIV_160 ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (0 << CTRL_CR2))
71 #define PCLK_DIV_960 ((0 << CTRL_CR0) | (0 << CTRL_CR1) | (1 << CTRL_CR2))
72 #define PCLK_DIV_120 ((1 << CTRL_CR0) | (0 << CTRL_CR1) | (1 << CTRL_CR2))
73 #define PCLK_DIV_60 ((0 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
74 #define BCLK_DIV_8 ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
75 #define CLK_MASK ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
78 * mpfs_i2c_bus - I2C bus context
79 * @base: pointer to register struct
80 * @msg_len: number of bytes transferred in msg
81 * @msg_err: error code for completed message
82 * @i2c_clk: clock reference for i2c input clock
83 * @clk_rate: current i2c bus clock rate
84 * @buf: ptr to msg buffer for easier use.
86 * @isr_status: cached copy of local ISR status.
99 static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
101 return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
104 static void mpfs_i2c_int_clear(struct mpfs_i2c_bus *bus)
106 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
109 writel(ctrl, bus->base + MPFS_I2C_CTRL);
112 static void mpfs_i2c_core_disable(struct mpfs_i2c_bus *bus)
114 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
117 writel(ctrl, bus->base + MPFS_I2C_CTRL);
120 static void mpfs_i2c_core_enable(struct mpfs_i2c_bus *bus)
122 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
125 writel(ctrl, bus->base + MPFS_I2C_CTRL);
128 static void mpfs_i2c_reset(struct mpfs_i2c_bus *bus)
130 mpfs_i2c_core_disable(bus);
131 mpfs_i2c_core_enable(bus);
134 static inline void mpfs_i2c_stop(struct mpfs_i2c_bus *bus)
136 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
139 writel(ctrl, bus->base + MPFS_I2C_CTRL);
142 static inline int mpfs_generate_divisor(u32 rate, u8 *code)
147 *code = PCLK_DIV_960;
148 else if (rate >= 256)
149 *code = PCLK_DIV_256;
150 else if (rate >= 224)
151 *code = PCLK_DIV_224;
152 else if (rate >= 192)
153 *code = PCLK_DIV_192;
154 else if (rate >= 160)
155 *code = PCLK_DIV_160;
156 else if (rate >= 120)
157 *code = PCLK_DIV_120;
168 static int mpfs_i2c_init(struct mpfs_i2c_bus *bus, struct udevice *dev)
170 u32 clk_rate, divisor;
174 ret = clk_get_by_index(dev, 0, &bus->i2c_clk);
178 ret = clk_enable(&bus->i2c_clk);
182 clk_rate = clk_get_rate(&bus->i2c_clk);
186 clk_free(&bus->i2c_clk);
188 divisor = clk_rate / bus->clk_rate;
190 ctrl = readl(bus->base + MPFS_I2C_CTRL);
194 ret = mpfs_generate_divisor(divisor, &clkval);
200 writel(ctrl, bus->base + MPFS_I2C_CTRL);
202 ctrl = readl(bus->base + MPFS_I2C_CTRL);
210 static void mpfs_i2c_transfer(struct mpfs_i2c_bus *bus, u32 data)
212 if (bus->msg_len > 0)
213 writel(data, bus->base + MPFS_I2C_DATA);
216 static void mpfs_i2c_empty_rx(struct mpfs_i2c_bus *bus)
221 if (bus->msg_len > 0) {
222 data_read = readl(bus->base + MPFS_I2C_DATA);
223 *bus->buf++ = data_read;
227 if (bus->msg_len <= 1) {
228 ctrl = readl(bus->base + MPFS_I2C_CTRL);
230 writel(ctrl, bus->base + MPFS_I2C_CTRL);
234 static int mpfs_i2c_fill_tx(struct mpfs_i2c_bus *bus)
236 mpfs_i2c_transfer(bus, *bus->buf++);
242 static int mpfs_i2c_service_handler(struct mpfs_i2c_bus *bus)
248 status = bus->isr_status;
251 case STATUS_M_START_SENT:
252 case STATUS_M_REPEATED_START_SENT:
253 ctrl = readl(bus->base + MPFS_I2C_CTRL);
255 writel(bus->addr, bus->base + MPFS_I2C_DATA);
256 writel(ctrl, bus->base + MPFS_I2C_CTRL);
258 case STATUS_M_SLAW_ACK:
259 case STATUS_M_TX_DATA_ACK:
260 if (bus->msg_len > 0) {
261 mpfs_i2c_fill_tx(bus);
263 /* On the last byte to be transmitted, send STOP */
268 case STATUS_M_SLAR_ACK:
269 if (bus->msg_len > 1u) {
270 ctrl = readl(bus->base + MPFS_I2C_CTRL);
272 writel(ctrl, bus->base + MPFS_I2C_CTRL);
273 } else if (bus->msg_len == 1u) {
274 ctrl = readl(bus->base + MPFS_I2C_CTRL);
276 writel(ctrl, bus->base + MPFS_I2C_CTRL);
278 ctrl = readl(bus->base + MPFS_I2C_CTRL);
280 writel(ctrl, bus->base + MPFS_I2C_CTRL);
281 /* On the last byte to be transmitted, send STOP */
286 case STATUS_M_RX_DATA_ACKED:
287 mpfs_i2c_empty_rx(bus);
289 case STATUS_M_RX_DATA_NACKED:
290 mpfs_i2c_empty_rx(bus);
291 if (bus->msg_len == 0) {
292 /* On the last byte to be transmitted, send STOP */
297 case STATUS_M_TX_DATA_NACK:
298 case STATUS_M_SLAR_NACK:
299 case STATUS_M_SLAW_NACK:
300 bus->msg_err = -ENXIO;
305 case STATUS_M_ARB_LOST:
306 /* Handle Lost Arbitration */
307 bus->msg_err = -EAGAIN;
315 ctrl = readl(bus->base + MPFS_I2C_CTRL);
317 writel(ctrl, bus->base + MPFS_I2C_CTRL);
324 static int mpfs_i2c_service(struct mpfs_i2c_bus *bus)
329 si_bit = readl(bus->base + MPFS_I2C_CTRL);
330 if (si_bit & CTRL_SI) {
331 bus->isr_status = readl(bus->base + MPFS_I2C_STATUS);
332 ret = mpfs_i2c_service_handler(bus);
334 /* Clear the si flag */
335 mpfs_i2c_int_clear(bus);
336 si_bit = readl(bus->base + MPFS_I2C_CTRL);
341 static int mpfs_i2c_check_service_change(struct mpfs_i2c_bus *bus)
347 ctrl = readl(bus->base + MPFS_I2C_CTRL);
352 if (count == MICROCHIP_I2C_TIMEOUT)
358 static int mpfs_i2c_poll_device(struct mpfs_i2c_bus *bus)
363 ret = mpfs_i2c_check_service_change(bus);
367 ret = mpfs_i2c_service(bus);
369 /* all messages have been transferred */
374 static int mpfs_i2c_xfer_msg(struct mpfs_i2c_bus *bus, struct i2c_msg *msg)
379 if (!msg->len || !msg->buf)
382 bus->addr = i2c_8bit_addr_from_msg(msg);
383 bus->msg_len = msg->len;
387 mpfs_i2c_core_enable(bus);
389 ctrl = readl(bus->base + MPFS_I2C_CTRL);
393 writel(ctrl, bus->base + MPFS_I2C_CTRL);
395 ret = mpfs_i2c_poll_device(bus);
402 static int mpfs_i2c_xfer(struct udevice *dev, struct i2c_msg *msgs, int num_msgs)
404 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
407 if (!msgs || !num_msgs)
410 for (idx = 0; idx < num_msgs; idx++) {
411 ret = mpfs_i2c_xfer_msg(bus, msgs++);
419 static int mpfs_i2c_probe_chip(struct udevice *dev, uint addr, uint flags)
421 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
426 * Send the chip address and verify that the
427 * address was <ACK>ed.
429 bus->addr = addr << 1 | I2C_M_RD;
434 mpfs_i2c_core_enable(bus);
436 ctrl = readl(bus->base + MPFS_I2C_CTRL);
440 writel(ctrl, bus->base + MPFS_I2C_CTRL);
442 ret = mpfs_i2c_poll_device(bus);
449 static int mpfs_i2c_probe(struct udevice *dev)
453 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
455 bus->base = dev_read_addr_ptr(dev);
459 val = dev_read_u32(dev, "clock-frequency", &bus->clk_rate);
461 printf("Default to 100kHz\n");
462 /* default clock rate */
463 bus->clk_rate = 100000;
466 if (bus->clk_rate > 400000 || bus->clk_rate <= 0) {
467 printf("Invalid clock-frequency %d\n", bus->clk_rate);
471 ret = mpfs_i2c_init(bus, dev);
476 static const struct dm_i2c_ops mpfs_i2c_ops = {
477 .xfer = mpfs_i2c_xfer,
478 .probe_chip = mpfs_i2c_probe_chip,
481 static const struct udevice_id mpfs_i2c_ids[] = {
482 {.compatible = "microchip,mpfs-i2c"},
486 U_BOOT_DRIVER(mpfs_i2c) = {
489 .of_match = mpfs_i2c_ids,
490 .ops = &mpfs_i2c_ops,
491 .probe = mpfs_i2c_probe,
492 .priv_auto = sizeof(struct mpfs_i2c_bus),