1 // SPDX-License-Identifier: GPL-2.0+
3 * Microchip I2C controller driver
5 * Copyright (C) 2021-2022 Microchip Technology Inc.
13 #include <dm/device_compat.h>
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
16 #include <linux/err.h>
18 #define MICROCHIP_I2C_TIMEOUT (1000 * 60)
20 #define MPFS_I2C_CTRL (0x00)
21 #define CTRL_CR0 (0x00)
22 #define CTRL_CR1 (0x01)
23 #define CTRL_AA BIT(2)
24 #define CTRL_SI BIT(3)
25 #define CTRL_STO BIT(4)
26 #define CTRL_STA BIT(5)
27 #define CTRL_ENS1 BIT(6)
28 #define CTRL_CR2 (0x07)
29 #define MPFS_I2C_STATUS (0x04)
30 #define STATUS_BUS_ERROR (0x00)
31 #define STATUS_M_START_SENT (0x08)
32 #define STATUS_M_REPEATED_START_SENT (0x10)
33 #define STATUS_M_SLAW_ACK (0x18)
34 #define STATUS_M_SLAW_NACK (0x20)
35 #define STATUS_M_TX_DATA_ACK (0x28)
36 #define STATUS_M_TX_DATA_NACK (0x30)
37 #define STATUS_M_ARB_LOST (0x38)
38 #define STATUS_M_SLAR_ACK (0x40)
39 #define STATUS_M_SLAR_NACK (0x48)
40 #define STATUS_M_RX_DATA_ACKED (0x50)
41 #define STATUS_M_RX_DATA_NACKED (0x58)
42 #define STATUS_S_SLAW_ACKED (0x60)
43 #define STATUS_S_ARB_LOST_SLAW_ACKED (0x68)
44 #define STATUS_S_GENERAL_CALL_ACKED (0x70)
45 #define STATUS_S_ARB_LOST_GENERAL_CALL_ACKED (0x78)
46 #define STATUS_S_RX_DATA_ACKED (0x80)
47 #define STATUS_S_RX_DATA_NACKED (0x88)
48 #define STATUS_S_GENERAL_CALL_RX_DATA_ACKED (0x90)
49 #define STATUS_S_GENERAL_CALL_RX_DATA_NACKED (0x98)
50 #define STATUS_S_RX_STOP (0xA0)
51 #define STATUS_S_SLAR_ACKED (0xA8)
52 #define STATUS_S_ARB_LOST_SLAR_ACKED (0xB0)
53 #define STATUS_S_TX_DATA_ACK (0xb8)
54 #define STATUS_S_TX_DATA_NACK (0xC0)
55 #define STATUS_LAST_DATA_ACK (0xC8)
56 #define STATUS_M_SMB_MASTER_RESET (0xD0)
57 #define STATUS_S_SCL_LOW_TIMEOUT (0xD8)
58 #define STATUS_NO_STATE_INFO (0xF8)
59 #define MPFS_I2C_DATA (0x08)
60 #define MPFS_I2C_SLAVE0_ADDR (0x0c)
61 #define MPFS_I2C_SMBUS (0x10)
62 #define MPFS_I2C_FREQ (0x14)
63 #define MPFS_I2C_GLITCHREG (0x18)
64 #define MPFS_I2C_SLAVE1_ADDR (0x1c)
66 #define PCLK_DIV_256 ((0 << CTRL_CR0) | (0 << CTRL_CR1) | (0 << CTRL_CR2))
67 #define PCLK_DIV_224 ((1 << CTRL_CR0) | (0 << CTRL_CR1) | (0 << CTRL_CR2))
68 #define PCLK_DIV_192 ((0 << CTRL_CR0) | (1 << CTRL_CR1) | (0 << CTRL_CR2))
69 #define PCLK_DIV_160 ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (0 << CTRL_CR2))
70 #define PCLK_DIV_960 ((0 << CTRL_CR0) | (0 << CTRL_CR1) | (1 << CTRL_CR2))
71 #define PCLK_DIV_120 ((1 << CTRL_CR0) | (0 << CTRL_CR1) | (1 << CTRL_CR2))
72 #define PCLK_DIV_60 ((0 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
73 #define BCLK_DIV_8 ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
74 #define CLK_MASK ((1 << CTRL_CR0) | (1 << CTRL_CR1) | (1 << CTRL_CR2))
77 * mpfs_i2c_bus - I2C bus context
78 * @base: pointer to register struct
79 * @msg_len: number of bytes transferred in msg
80 * @msg_err: error code for completed message
81 * @i2c_clk: clock reference for i2c input clock
82 * @clk_rate: current i2c bus clock rate
83 * @buf: ptr to msg buffer for easier use.
85 * @isr_status: cached copy of local ISR status.
98 static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
100 return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
103 static void mpfs_i2c_int_clear(struct mpfs_i2c_bus *bus)
105 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
108 writel(ctrl, bus->base + MPFS_I2C_CTRL);
111 static void mpfs_i2c_core_disable(struct mpfs_i2c_bus *bus)
113 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
116 writel(ctrl, bus->base + MPFS_I2C_CTRL);
119 static void mpfs_i2c_core_enable(struct mpfs_i2c_bus *bus)
121 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
124 writel(ctrl, bus->base + MPFS_I2C_CTRL);
127 static void mpfs_i2c_reset(struct mpfs_i2c_bus *bus)
129 mpfs_i2c_core_disable(bus);
130 mpfs_i2c_core_enable(bus);
133 static inline void mpfs_i2c_stop(struct mpfs_i2c_bus *bus)
135 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
138 writel(ctrl, bus->base + MPFS_I2C_CTRL);
141 static inline int mpfs_generate_divisor(u32 rate, u8 *code)
146 *code = PCLK_DIV_960;
147 else if (rate >= 256)
148 *code = PCLK_DIV_256;
149 else if (rate >= 224)
150 *code = PCLK_DIV_224;
151 else if (rate >= 192)
152 *code = PCLK_DIV_192;
153 else if (rate >= 160)
154 *code = PCLK_DIV_160;
155 else if (rate >= 120)
156 *code = PCLK_DIV_120;
167 static int mpfs_i2c_init(struct mpfs_i2c_bus *bus, struct udevice *dev)
169 u32 clk_rate, divisor;
173 ret = clk_get_by_index(dev, 0, &bus->i2c_clk);
177 ret = clk_enable(&bus->i2c_clk);
181 clk_rate = clk_get_rate(&bus->i2c_clk);
185 divisor = clk_rate / bus->clk_rate;
187 ctrl = readl(bus->base + MPFS_I2C_CTRL);
191 ret = mpfs_generate_divisor(divisor, &clkval);
197 writel(ctrl, bus->base + MPFS_I2C_CTRL);
199 ctrl = readl(bus->base + MPFS_I2C_CTRL);
207 static void mpfs_i2c_transfer(struct mpfs_i2c_bus *bus, u32 data)
209 if (bus->msg_len > 0)
210 writel(data, bus->base + MPFS_I2C_DATA);
213 static void mpfs_i2c_empty_rx(struct mpfs_i2c_bus *bus)
218 if (bus->msg_len > 0) {
219 data_read = readl(bus->base + MPFS_I2C_DATA);
220 *bus->buf++ = data_read;
224 if (bus->msg_len <= 1) {
225 ctrl = readl(bus->base + MPFS_I2C_CTRL);
227 writel(ctrl, bus->base + MPFS_I2C_CTRL);
231 static int mpfs_i2c_fill_tx(struct mpfs_i2c_bus *bus)
233 mpfs_i2c_transfer(bus, *bus->buf++);
239 static int mpfs_i2c_service_handler(struct mpfs_i2c_bus *bus)
245 status = bus->isr_status;
248 case STATUS_M_START_SENT:
249 case STATUS_M_REPEATED_START_SENT:
250 ctrl = readl(bus->base + MPFS_I2C_CTRL);
252 writel(bus->addr, bus->base + MPFS_I2C_DATA);
253 writel(ctrl, bus->base + MPFS_I2C_CTRL);
255 case STATUS_M_SLAW_ACK:
256 case STATUS_M_TX_DATA_ACK:
257 if (bus->msg_len > 0) {
258 mpfs_i2c_fill_tx(bus);
260 /* On the last byte to be transmitted, send STOP */
265 case STATUS_M_SLAR_ACK:
266 if (bus->msg_len > 1u) {
267 ctrl = readl(bus->base + MPFS_I2C_CTRL);
269 writel(ctrl, bus->base + MPFS_I2C_CTRL);
270 } else if (bus->msg_len == 1u) {
271 ctrl = readl(bus->base + MPFS_I2C_CTRL);
273 writel(ctrl, bus->base + MPFS_I2C_CTRL);
275 ctrl = readl(bus->base + MPFS_I2C_CTRL);
277 writel(ctrl, bus->base + MPFS_I2C_CTRL);
278 /* On the last byte to be transmitted, send STOP */
283 case STATUS_M_RX_DATA_ACKED:
284 mpfs_i2c_empty_rx(bus);
286 case STATUS_M_RX_DATA_NACKED:
287 mpfs_i2c_empty_rx(bus);
288 if (bus->msg_len == 0) {
289 /* On the last byte to be transmitted, send STOP */
294 case STATUS_M_TX_DATA_NACK:
295 case STATUS_M_SLAR_NACK:
296 case STATUS_M_SLAW_NACK:
297 bus->msg_err = -ENXIO;
302 case STATUS_M_ARB_LOST:
303 /* Handle Lost Arbitration */
304 bus->msg_err = -EAGAIN;
312 ctrl = readl(bus->base + MPFS_I2C_CTRL);
314 writel(ctrl, bus->base + MPFS_I2C_CTRL);
321 static int mpfs_i2c_service(struct mpfs_i2c_bus *bus)
326 si_bit = readl(bus->base + MPFS_I2C_CTRL);
327 if (si_bit & CTRL_SI) {
328 bus->isr_status = readl(bus->base + MPFS_I2C_STATUS);
329 ret = mpfs_i2c_service_handler(bus);
331 /* Clear the si flag */
332 mpfs_i2c_int_clear(bus);
333 si_bit = readl(bus->base + MPFS_I2C_CTRL);
338 static int mpfs_i2c_check_service_change(struct mpfs_i2c_bus *bus)
344 ctrl = readl(bus->base + MPFS_I2C_CTRL);
349 if (count == MICROCHIP_I2C_TIMEOUT)
355 static int mpfs_i2c_poll_device(struct mpfs_i2c_bus *bus)
360 ret = mpfs_i2c_check_service_change(bus);
364 ret = mpfs_i2c_service(bus);
366 /* all messages have been transferred */
371 static int mpfs_i2c_xfer_msg(struct mpfs_i2c_bus *bus, struct i2c_msg *msg)
376 if (!msg->len || !msg->buf)
379 bus->addr = i2c_8bit_addr_from_msg(msg);
380 bus->msg_len = msg->len;
384 mpfs_i2c_core_enable(bus);
386 ctrl = readl(bus->base + MPFS_I2C_CTRL);
390 writel(ctrl, bus->base + MPFS_I2C_CTRL);
392 ret = mpfs_i2c_poll_device(bus);
399 static int mpfs_i2c_xfer(struct udevice *dev, struct i2c_msg *msgs, int num_msgs)
401 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
404 if (!msgs || !num_msgs)
407 for (idx = 0; idx < num_msgs; idx++) {
408 ret = mpfs_i2c_xfer_msg(bus, msgs++);
416 static int mpfs_i2c_probe_chip(struct udevice *dev, uint addr, uint flags)
418 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
423 * Send the chip address and verify that the
424 * address was <ACK>ed.
426 bus->addr = addr << 1 | I2C_M_RD;
431 mpfs_i2c_core_enable(bus);
433 ctrl = readl(bus->base + MPFS_I2C_CTRL);
437 writel(ctrl, bus->base + MPFS_I2C_CTRL);
439 ret = mpfs_i2c_poll_device(bus);
446 static int mpfs_i2c_probe(struct udevice *dev)
450 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
452 bus->base = dev_read_addr_ptr(dev);
456 val = dev_read_u32(dev, "clock-frequency", &bus->clk_rate);
458 printf("Default to 100kHz\n");
459 /* default clock rate */
460 bus->clk_rate = 100000;
463 if (bus->clk_rate > 400000 || bus->clk_rate <= 0) {
464 printf("Invalid clock-frequency %d\n", bus->clk_rate);
468 ret = mpfs_i2c_init(bus, dev);
473 static const struct dm_i2c_ops mpfs_i2c_ops = {
474 .xfer = mpfs_i2c_xfer,
475 .probe_chip = mpfs_i2c_probe_chip,
478 static const struct udevice_id mpfs_i2c_ids[] = {
479 {.compatible = "microchip,mpfs-i2c"},
483 U_BOOT_DRIVER(mpfs_i2c) = {
486 .of_match = mpfs_i2c_ids,
487 .ops = &mpfs_i2c_ops,
488 .probe = mpfs_i2c_probe,
489 .priv_auto = sizeof(struct mpfs_i2c_bus),