1 // SPDX-License-Identifier: GPL-2.0+
3 * Microchip I2C controller driver
5 * Copyright (C) 2021 Microchip Technology Inc.
6 * Padmarao Begari <padmarao.begari@microchip.com>
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 clk_free(&bus->i2c_clk);
187 divisor = clk_rate / bus->clk_rate;
189 ctrl = readl(bus->base + MPFS_I2C_CTRL);
193 ret = mpfs_generate_divisor(divisor, &clkval);
199 writel(ctrl, bus->base + MPFS_I2C_CTRL);
201 ctrl = readl(bus->base + MPFS_I2C_CTRL);
209 static void mpfs_i2c_transfer(struct mpfs_i2c_bus *bus, u32 data)
211 if (bus->msg_len > 0)
212 writel(data, bus->base + MPFS_I2C_DATA);
215 static void mpfs_i2c_empty_rx(struct mpfs_i2c_bus *bus)
220 if (bus->msg_len > 0) {
221 data_read = readl(bus->base + MPFS_I2C_DATA);
222 *bus->buf++ = data_read;
226 if (bus->msg_len == 0) {
227 ctrl = readl(bus->base + MPFS_I2C_CTRL);
229 writel(ctrl, bus->base + MPFS_I2C_CTRL);
233 static int mpfs_i2c_fill_tx(struct mpfs_i2c_bus *bus)
235 mpfs_i2c_transfer(bus, *bus->buf++);
241 static int mpfs_i2c_service_handler(struct mpfs_i2c_bus *bus)
247 status = bus->isr_status;
250 case STATUS_M_START_SENT:
251 case STATUS_M_REPEATED_START_SENT:
252 ctrl = readl(bus->base + MPFS_I2C_CTRL);
254 writel(bus->addr, bus->base + MPFS_I2C_DATA);
255 writel(ctrl, bus->base + MPFS_I2C_CTRL);
257 case STATUS_M_SLAW_ACK:
258 case STATUS_M_TX_DATA_ACK:
259 if (bus->msg_len > 0) {
260 mpfs_i2c_fill_tx(bus);
262 /* On the last byte to be transmitted, send STOP */
267 case STATUS_M_SLAR_ACK:
268 ctrl = readl(bus->base + MPFS_I2C_CTRL);
270 writel(ctrl, bus->base + MPFS_I2C_CTRL);
271 if (bus->msg_len == 0) {
272 /* On the last byte to be transmitted, send STOP */
277 case STATUS_M_RX_DATA_ACKED:
278 mpfs_i2c_empty_rx(bus);
279 if (bus->msg_len == 0) {
280 /* On the last byte to be transmitted, send STOP */
285 case STATUS_M_TX_DATA_NACK:
286 case STATUS_M_RX_DATA_NACKED:
287 case STATUS_M_SLAR_NACK:
288 case STATUS_M_SLAW_NACK:
289 bus->msg_err = -ENXIO;
294 case STATUS_M_ARB_LOST:
295 /* Handle Lost Arbitration */
296 bus->msg_err = -EAGAIN;
304 ctrl = readl(bus->base + MPFS_I2C_CTRL);
306 writel(ctrl, bus->base + MPFS_I2C_CTRL);
313 static int mpfs_i2c_service(struct mpfs_i2c_bus *bus)
318 si_bit = readl(bus->base + MPFS_I2C_CTRL);
319 if (si_bit & CTRL_SI) {
320 bus->isr_status = readl(bus->base + MPFS_I2C_STATUS);
321 ret = mpfs_i2c_service_handler(bus);
323 /* Clear the si flag */
324 mpfs_i2c_int_clear(bus);
325 si_bit = readl(bus->base + MPFS_I2C_CTRL);
330 static int mpfs_i2c_check_service_change(struct mpfs_i2c_bus *bus)
336 ctrl = readl(bus->base + MPFS_I2C_CTRL);
341 if (count == MICROCHIP_I2C_TIMEOUT)
347 static int mpfs_i2c_poll_device(struct mpfs_i2c_bus *bus)
352 ret = mpfs_i2c_check_service_change(bus);
356 ret = mpfs_i2c_service(bus);
358 /* all messages have been transferred */
363 static int mpfs_i2c_xfer_msg(struct mpfs_i2c_bus *bus, struct i2c_msg *msg)
368 if (!msg->len || !msg->buf)
371 bus->addr = i2c_8bit_addr_from_msg(msg);
372 bus->msg_len = msg->len;
376 mpfs_i2c_core_enable(bus);
378 ctrl = readl(bus->base + MPFS_I2C_CTRL);
382 writel(ctrl, bus->base + MPFS_I2C_CTRL);
384 ret = mpfs_i2c_poll_device(bus);
391 static int mpfs_i2c_xfer(struct udevice *dev, struct i2c_msg *msgs, int num_msgs)
393 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
396 if (!msgs || !num_msgs)
399 for (idx = 0; idx < num_msgs; idx++) {
400 ret = mpfs_i2c_xfer_msg(bus, msgs++);
408 static int mpfs_i2c_probe_chip(struct udevice *dev, uint addr, uint flags)
410 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
415 * Send the chip address and verify that the
416 * address was <ACK>ed.
418 bus->addr = addr << 1 | I2C_M_RD;
423 mpfs_i2c_core_enable(bus);
425 ctrl = readl(bus->base + MPFS_I2C_CTRL);
429 writel(ctrl, bus->base + MPFS_I2C_CTRL);
431 ret = mpfs_i2c_poll_device(bus);
438 static int mpfs_i2c_probe(struct udevice *dev)
442 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
444 bus->base = dev_read_addr_ptr(dev);
448 val = dev_read_u32(dev, "clock-frequency", &bus->clk_rate);
450 printf("Default to 100kHz\n");
451 /* default clock rate */
452 bus->clk_rate = 100000;
455 if (bus->clk_rate > 400000 || bus->clk_rate <= 0) {
456 printf("Invalid clock-frequency %d\n", bus->clk_rate);
460 ret = mpfs_i2c_init(bus, dev);
465 static const struct dm_i2c_ops mpfs_i2c_ops = {
466 .xfer = mpfs_i2c_xfer,
467 .probe_chip = mpfs_i2c_probe_chip,
470 static const struct udevice_id mpfs_i2c_ids[] = {
471 {.compatible = "microchip,mpfs-i2c"},
475 U_BOOT_DRIVER(mpfs_i2c) = {
478 .of_match = mpfs_i2c_ids,
479 .ops = &mpfs_i2c_ops,
480 .probe = mpfs_i2c_probe,
481 .priv_auto = sizeof(struct mpfs_i2c_bus),