blob: d453e243d6f2b24e94433c304ff6c7a684a1dde3 [file] [log] [blame]
Padmarao Begari7ddb4ec2021-11-17 18:21:16 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Microchip I2C controller driver
4 *
Conor Dooleybc55c6c2022-10-26 08:49:18 +01005 * Copyright (C) 2021-2022 Microchip Technology Inc.
Padmarao Begari7ddb4ec2021-11-17 18:21:16 +05306 * Padmarao Begari <padmarao.begari@microchip.com>
Conor Dooleybc55c6c2022-10-26 08:49:18 +01007 * Conor Dooley <conor.dooley@microchip.com>
Padmarao Begari7ddb4ec2021-11-17 18:21:16 +05308 */
9#include <common.h>
10#include <clk.h>
11#include <dm.h>
12#include <i2c.h>
13#include <asm/io.h>
14#include <dm/device_compat.h>
15#include <linux/bitops.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18
19#define MICROCHIP_I2C_TIMEOUT (1000 * 60)
20
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)
66
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))
76
77/*
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.
85 * @addr: i2c address.
86 * @isr_status: cached copy of local ISR status.
87 */
88struct mpfs_i2c_bus {
89 void __iomem *base;
90 size_t msg_len;
91 int msg_err;
92 struct clk i2c_clk;
93 u32 clk_rate;
94 u8 *buf;
95 u8 addr;
96 u32 isr_status;
97};
98
99static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
100{
101 return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
102}
103
104static void mpfs_i2c_int_clear(struct mpfs_i2c_bus *bus)
105{
106 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
107
108 ctrl &= ~CTRL_SI;
109 writel(ctrl, bus->base + MPFS_I2C_CTRL);
110}
111
112static void mpfs_i2c_core_disable(struct mpfs_i2c_bus *bus)
113{
114 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
115
116 ctrl &= ~CTRL_ENS1;
117 writel(ctrl, bus->base + MPFS_I2C_CTRL);
118}
119
120static void mpfs_i2c_core_enable(struct mpfs_i2c_bus *bus)
121{
122 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
123
124 ctrl |= CTRL_ENS1;
125 writel(ctrl, bus->base + MPFS_I2C_CTRL);
126}
127
128static void mpfs_i2c_reset(struct mpfs_i2c_bus *bus)
129{
130 mpfs_i2c_core_disable(bus);
131 mpfs_i2c_core_enable(bus);
132}
133
134static inline void mpfs_i2c_stop(struct mpfs_i2c_bus *bus)
135{
136 u8 ctrl = readl(bus->base + MPFS_I2C_CTRL);
137
138 ctrl |= CTRL_STO;
139 writel(ctrl, bus->base + MPFS_I2C_CTRL);
140}
141
142static inline int mpfs_generate_divisor(u32 rate, u8 *code)
143{
144 int ret = 0;
145
146 if (rate >= 960)
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;
158 else if (rate >= 60)
159 *code = PCLK_DIV_60;
160 else if (rate >= 8)
161 *code = BCLK_DIV_8;
162 else
163 ret = -EINVAL;
164
165 return ret;
166}
167
168static int mpfs_i2c_init(struct mpfs_i2c_bus *bus, struct udevice *dev)
169{
170 u32 clk_rate, divisor;
171 u8 clkval, ctrl;
172 int ret;
173
174 ret = clk_get_by_index(dev, 0, &bus->i2c_clk);
175 if (ret)
176 return -EINVAL;
177
178 ret = clk_enable(&bus->i2c_clk);
179 if (ret)
180 return ret;
181
182 clk_rate = clk_get_rate(&bus->i2c_clk);
183 if (!clk_rate)
184 return -EINVAL;
185
Padmarao Begari7ddb4ec2021-11-17 18:21:16 +0530186 divisor = clk_rate / bus->clk_rate;
187
188 ctrl = readl(bus->base + MPFS_I2C_CTRL);
189
190 ctrl &= ~CLK_MASK;
191
192 ret = mpfs_generate_divisor(divisor, &clkval);
193 if (ret)
194 return -EINVAL;
195
196 ctrl |= clkval;
197
198 writel(ctrl, bus->base + MPFS_I2C_CTRL);
199
200 ctrl = readl(bus->base + MPFS_I2C_CTRL);
201
202 /* Reset I2C core */
203 mpfs_i2c_reset(bus);
204
205 return 0;
206}
207
208static void mpfs_i2c_transfer(struct mpfs_i2c_bus *bus, u32 data)
209{
210 if (bus->msg_len > 0)
211 writel(data, bus->base + MPFS_I2C_DATA);
212}
213
214static void mpfs_i2c_empty_rx(struct mpfs_i2c_bus *bus)
215{
216 u8 ctrl;
217 u8 data_read;
218
219 if (bus->msg_len > 0) {
220 data_read = readl(bus->base + MPFS_I2C_DATA);
221 *bus->buf++ = data_read;
222 bus->msg_len--;
223 }
224
Conor Dooley77229522022-10-26 08:49:19 +0100225 if (bus->msg_len <= 1) {
Padmarao Begari7ddb4ec2021-11-17 18:21:16 +0530226 ctrl = readl(bus->base + MPFS_I2C_CTRL);
227 ctrl &= ~CTRL_AA;
228 writel(ctrl, bus->base + MPFS_I2C_CTRL);
229 }
230}
231
232static int mpfs_i2c_fill_tx(struct mpfs_i2c_bus *bus)
233{
234 mpfs_i2c_transfer(bus, *bus->buf++);
235 bus->msg_len--;
236
237 return 0;
238}
239
240static int mpfs_i2c_service_handler(struct mpfs_i2c_bus *bus)
241{
242 bool finish = false;
243 u32 status;
244 u8 ctrl;
245
246 status = bus->isr_status;
247
248 switch (status) {
249 case STATUS_M_START_SENT:
250 case STATUS_M_REPEATED_START_SENT:
251 ctrl = readl(bus->base + MPFS_I2C_CTRL);
252 ctrl &= ~CTRL_STA;
253 writel(bus->addr, bus->base + MPFS_I2C_DATA);
254 writel(ctrl, bus->base + MPFS_I2C_CTRL);
255 break;
256 case STATUS_M_SLAW_ACK:
257 case STATUS_M_TX_DATA_ACK:
258 if (bus->msg_len > 0) {
259 mpfs_i2c_fill_tx(bus);
260 } else {
261 /* On the last byte to be transmitted, send STOP */
262 mpfs_i2c_stop(bus);
263 finish = true;
264 }
265 break;
266 case STATUS_M_SLAR_ACK:
Conor Dooleybc55c6c2022-10-26 08:49:18 +0100267 if (bus->msg_len > 1u) {
268 ctrl = readl(bus->base + MPFS_I2C_CTRL);
269 ctrl |= CTRL_AA;
270 writel(ctrl, bus->base + MPFS_I2C_CTRL);
271 } else if (bus->msg_len == 1u) {
272 ctrl = readl(bus->base + MPFS_I2C_CTRL);
273 ctrl &= ~CTRL_AA;
274 writel(ctrl, bus->base + MPFS_I2C_CTRL);
275 } else {
276 ctrl = readl(bus->base + MPFS_I2C_CTRL);
277 ctrl |= CTRL_AA;
278 writel(ctrl, bus->base + MPFS_I2C_CTRL);
Padmarao Begari7ddb4ec2021-11-17 18:21:16 +0530279 /* On the last byte to be transmitted, send STOP */
280 mpfs_i2c_stop(bus);
281 finish = true;
282 }
283 break;
284 case STATUS_M_RX_DATA_ACKED:
285 mpfs_i2c_empty_rx(bus);
Conor Dooleybc55c6c2022-10-26 08:49:18 +0100286 break;
287 case STATUS_M_RX_DATA_NACKED:
288 mpfs_i2c_empty_rx(bus);
Padmarao Begari7ddb4ec2021-11-17 18:21:16 +0530289 if (bus->msg_len == 0) {
290 /* On the last byte to be transmitted, send STOP */
291 mpfs_i2c_stop(bus);
292 finish = true;
293 }
294 break;
295 case STATUS_M_TX_DATA_NACK:
Padmarao Begari7ddb4ec2021-11-17 18:21:16 +0530296 case STATUS_M_SLAR_NACK:
297 case STATUS_M_SLAW_NACK:
298 bus->msg_err = -ENXIO;
299 mpfs_i2c_stop(bus);
300 finish = true;
301 break;
302
303 case STATUS_M_ARB_LOST:
304 /* Handle Lost Arbitration */
305 bus->msg_err = -EAGAIN;
306 finish = true;
307 break;
308 default:
309 break;
310 }
311
312 if (finish) {
313 ctrl = readl(bus->base + MPFS_I2C_CTRL);
314 ctrl &= ~CTRL_AA;
315 writel(ctrl, bus->base + MPFS_I2C_CTRL);
316 return 0;
317 }
318
319 return 1;
320}
321
322static int mpfs_i2c_service(struct mpfs_i2c_bus *bus)
323{
324 int ret = 0;
325 int si_bit;
326
327 si_bit = readl(bus->base + MPFS_I2C_CTRL);
328 if (si_bit & CTRL_SI) {
329 bus->isr_status = readl(bus->base + MPFS_I2C_STATUS);
330 ret = mpfs_i2c_service_handler(bus);
331 }
332 /* Clear the si flag */
333 mpfs_i2c_int_clear(bus);
334 si_bit = readl(bus->base + MPFS_I2C_CTRL);
335
336 return ret;
337}
338
339static int mpfs_i2c_check_service_change(struct mpfs_i2c_bus *bus)
340{
341 u8 ctrl;
342 u32 count = 0;
343
344 while (1) {
345 ctrl = readl(bus->base + MPFS_I2C_CTRL);
346 if (ctrl & CTRL_SI)
347 break;
348 udelay(1);
349 count += 1;
350 if (count == MICROCHIP_I2C_TIMEOUT)
351 return -ETIMEDOUT;
352 }
353 return 0;
354}
355
356static int mpfs_i2c_poll_device(struct mpfs_i2c_bus *bus)
357{
358 int ret;
359
360 while (1) {
361 ret = mpfs_i2c_check_service_change(bus);
362 if (ret)
363 return ret;
364
365 ret = mpfs_i2c_service(bus);
366 if (!ret)
367 /* all messages have been transferred */
368 return ret;
369 }
370}
371
372static int mpfs_i2c_xfer_msg(struct mpfs_i2c_bus *bus, struct i2c_msg *msg)
373{
374 u8 ctrl;
375 int ret;
376
377 if (!msg->len || !msg->buf)
378 return -EINVAL;
379
380 bus->addr = i2c_8bit_addr_from_msg(msg);
381 bus->msg_len = msg->len;
382 bus->buf = msg->buf;
383 bus->msg_err = 0;
384
385 mpfs_i2c_core_enable(bus);
386
387 ctrl = readl(bus->base + MPFS_I2C_CTRL);
388
389 ctrl |= CTRL_STA;
390
391 writel(ctrl, bus->base + MPFS_I2C_CTRL);
392
393 ret = mpfs_i2c_poll_device(bus);
394 if (ret)
395 return ret;
396
397 return bus->msg_err;
398}
399
400static int mpfs_i2c_xfer(struct udevice *dev, struct i2c_msg *msgs, int num_msgs)
401{
402 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
403 int idx, ret;
404
405 if (!msgs || !num_msgs)
406 return -EINVAL;
407
408 for (idx = 0; idx < num_msgs; idx++) {
409 ret = mpfs_i2c_xfer_msg(bus, msgs++);
410 if (ret)
411 return ret;
412 }
413
414 return ret;
415}
416
417static int mpfs_i2c_probe_chip(struct udevice *dev, uint addr, uint flags)
418{
419 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
420 int ret;
421 u8 ctrl, reg = 0;
422
423 /*
424 * Send the chip address and verify that the
425 * address was <ACK>ed.
426 */
427 bus->addr = addr << 1 | I2C_M_RD;
428 bus->buf = &reg;
429 bus->msg_len = 0;
430 bus->msg_err = 0;
431
432 mpfs_i2c_core_enable(bus);
433
434 ctrl = readl(bus->base + MPFS_I2C_CTRL);
435
436 ctrl |= CTRL_STA;
437
438 writel(ctrl, bus->base + MPFS_I2C_CTRL);
439
440 ret = mpfs_i2c_poll_device(bus);
441 if (ret)
442 return ret;
443
444 return bus->msg_err;
445}
446
447static int mpfs_i2c_probe(struct udevice *dev)
448{
449 int ret;
450 u32 val;
451 struct mpfs_i2c_bus *bus = dev_get_priv(dev);
452
453 bus->base = dev_read_addr_ptr(dev);
454 if (!bus->base)
455 return -EINVAL;
456
457 val = dev_read_u32(dev, "clock-frequency", &bus->clk_rate);
458 if (val) {
459 printf("Default to 100kHz\n");
460 /* default clock rate */
461 bus->clk_rate = 100000;
462 }
463
464 if (bus->clk_rate > 400000 || bus->clk_rate <= 0) {
465 printf("Invalid clock-frequency %d\n", bus->clk_rate);
466 return -EINVAL;
467 }
468
469 ret = mpfs_i2c_init(bus, dev);
470
471 return ret;
472}
473
474static const struct dm_i2c_ops mpfs_i2c_ops = {
475 .xfer = mpfs_i2c_xfer,
476 .probe_chip = mpfs_i2c_probe_chip,
477};
478
479static const struct udevice_id mpfs_i2c_ids[] = {
480 {.compatible = "microchip,mpfs-i2c"},
481 {}
482};
483
484U_BOOT_DRIVER(mpfs_i2c) = {
485 .name = "mpfs_i2c",
486 .id = UCLASS_I2C,
487 .of_match = mpfs_i2c_ids,
488 .ops = &mpfs_i2c_ops,
489 .probe = mpfs_i2c_probe,
490 .priv_auto = sizeof(struct mpfs_i2c_bus),
491};