1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2016 Freescale Semiconductors, Inc.
10 #include <asm/arch/clock.h>
11 #include <asm/arch/imx-regs.h>
12 #include <imx_lpi2c.h>
13 #include <asm/arch/sys_proto.h>
17 #include <dm/device_compat.h>
19 #define LPI2C_FIFO_SIZE 4
20 #define LPI2C_NACK_TOUT_MS 1
21 #define LPI2C_TIMEOUT_MS 100
23 static int bus_i2c_init(struct udevice *bus, int speed);
25 /* Weak linked function for overridden by some SoC power function */
26 int __weak init_i2c_power(unsigned i2c_num)
31 static int imx_lpci2c_check_busy_bus(const struct imx_lpi2c_reg *regs)
33 lpi2c_status_t result = LPI2C_SUCESS;
36 status = readl(®s->msr);
38 if ((status & LPI2C_MSR_BBF_MASK) && !(status & LPI2C_MSR_MBF_MASK))
44 static int imx_lpci2c_check_clear_error(struct imx_lpi2c_reg *regs)
46 lpi2c_status_t result = LPI2C_SUCESS;
49 status = readl(®s->msr);
50 /* errors to check for */
51 status &= LPI2C_MSR_NDF_MASK | LPI2C_MSR_ALF_MASK |
52 LPI2C_MSR_FEF_MASK | LPI2C_MSR_PLTF_MASK;
55 if (status & LPI2C_MSR_PLTF_MASK)
56 result = LPI2C_PIN_LOW_TIMEOUT_ERR;
57 else if (status & LPI2C_MSR_ALF_MASK)
58 result = LPI2C_ARB_LOST_ERR;
59 else if (status & LPI2C_MSR_NDF_MASK)
60 result = LPI2C_NAK_ERR;
61 else if (status & LPI2C_MSR_FEF_MASK)
62 result = LPI2C_FIFO_ERR;
64 /* clear status flags */
65 writel(0x7f00, ®s->msr);
67 val = readl(®s->mcr);
68 val |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK;
69 writel(val, ®s->mcr);
75 static int bus_i2c_wait_for_tx_ready(struct imx_lpi2c_reg *regs)
77 lpi2c_status_t result = LPI2C_SUCESS;
79 ulong start_time = get_timer(0);
82 txcount = LPI2C_MFSR_TXCOUNT(readl(®s->mfsr));
83 txcount = LPI2C_FIFO_SIZE - txcount;
84 result = imx_lpci2c_check_clear_error(regs);
86 debug("i2c: wait for tx ready: result 0x%x\n", result);
89 if (get_timer(start_time) > LPI2C_TIMEOUT_MS) {
90 debug("i2c: wait for tx ready: timeout\n");
98 static int bus_i2c_send(struct udevice *bus, u8 *txbuf, int len)
100 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
101 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
102 lpi2c_status_t result = LPI2C_SUCESS;
109 result = bus_i2c_wait_for_tx_ready(regs);
111 debug("i2c: send wait for tx ready: %d\n", result);
114 writel(*txbuf++, ®s->mtdr);
120 static int bus_i2c_receive(struct udevice *bus, u8 *rxbuf, int len)
122 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
123 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
124 lpi2c_status_t result = LPI2C_SUCESS;
126 ulong start_time = get_timer(0);
132 result = bus_i2c_wait_for_tx_ready(regs);
134 debug("i2c: receive wait fot tx ready: %d\n", result);
138 /* clear all status flags */
139 writel(0x7f00, ®s->msr);
140 /* send receive command */
141 val = LPI2C_MTDR_CMD(0x1) | LPI2C_MTDR_DATA(len - 1);
142 writel(val, ®s->mtdr);
146 result = imx_lpci2c_check_clear_error(regs);
148 debug("i2c: receive check clear error: %d\n",
152 if (get_timer(start_time) > LPI2C_TIMEOUT_MS) {
153 debug("i2c: receive mrdr: timeout\n");
156 val = readl(®s->mrdr);
157 } while (val & LPI2C_MRDR_RXEMPTY_MASK);
158 *rxbuf++ = LPI2C_MRDR_DATA(val);
164 static int bus_i2c_start(struct udevice *bus, u8 addr, u8 dir)
166 lpi2c_status_t result;
167 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
168 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
171 result = imx_lpci2c_check_busy_bus(regs);
173 debug("i2c: start check busy bus: 0x%x\n", result);
175 /* Try to init the lpi2c then check the bus busy again */
176 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
177 result = imx_lpci2c_check_busy_bus(regs);
179 printf("i2c: Error check busy bus: 0x%x\n", result);
183 /* clear all status flags */
184 writel(0x7f00, ®s->msr);
185 /* turn off auto-stop condition */
186 val = readl(®s->mcfgr1) & ~LPI2C_MCFGR1_AUTOSTOP_MASK;
187 writel(val, ®s->mcfgr1);
188 /* wait tx fifo ready */
189 result = bus_i2c_wait_for_tx_ready(regs);
191 debug("i2c: start wait for tx ready: 0x%x\n", result);
194 /* issue start command */
195 val = LPI2C_MTDR_CMD(0x4) | (addr << 0x1) | dir;
196 writel(val, ®s->mtdr);
201 static int bus_i2c_stop(struct udevice *bus)
203 lpi2c_status_t result;
204 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
205 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
209 result = bus_i2c_wait_for_tx_ready(regs);
211 debug("i2c: stop wait for tx ready: 0x%x\n", result);
215 /* send stop command */
216 writel(LPI2C_MTDR_CMD(0x2), ®s->mtdr);
218 start_time = get_timer(0);
220 status = readl(®s->msr);
221 result = imx_lpci2c_check_clear_error(regs);
222 /* stop detect flag */
223 if (status & LPI2C_MSR_SDF_MASK) {
224 /* clear stop flag */
225 status &= LPI2C_MSR_SDF_MASK;
226 writel(status, ®s->msr);
230 if (get_timer(start_time) > LPI2C_NACK_TOUT_MS) {
231 debug("stop timeout\n");
239 static int bus_i2c_read(struct udevice *bus, u32 chip, u8 *buf, int len)
241 lpi2c_status_t result;
243 result = bus_i2c_start(bus, chip, 1);
246 result = bus_i2c_receive(bus, buf, len);
253 static int bus_i2c_write(struct udevice *bus, u32 chip, u8 *buf, int len)
255 lpi2c_status_t result;
257 result = bus_i2c_start(bus, chip, 0);
260 result = bus_i2c_send(bus, buf, len);
268 u32 __weak imx_get_i2cclk(u32 i2c_num)
273 static int bus_i2c_set_bus_speed(struct udevice *bus, int speed)
275 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
276 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
278 u32 preescale = 0, best_pre = 0, clkhi = 0;
279 u32 best_clkhi = 0, abs_error = 0, rate;
280 u32 error = 0xffffffff;
285 if (IS_ENABLED(CONFIG_CLK)) {
286 clock_rate = clk_get_rate(&i2c_bus->per_clk);
287 if (clock_rate <= 0) {
288 dev_err(bus, "Failed to get i2c clk: %d\n", clock_rate);
292 clock_rate = imx_get_i2cclk(bus->seq);
297 mode = (readl(®s->mcr) & LPI2C_MCR_MEN_MASK) >> LPI2C_MCR_MEN_SHIFT;
298 /* disable master mode */
299 val = readl(®s->mcr) & ~LPI2C_MCR_MEN_MASK;
300 writel(val | LPI2C_MCR_MEN(0), ®s->mcr);
302 for (preescale = 1; (preescale <= 128) &&
303 (error != 0); preescale = 2 * preescale) {
304 for (clkhi = 1; clkhi < 32; clkhi++) {
306 rate = (clock_rate / preescale) / (1 + 3 + 2 + 2 / preescale);
308 rate = (clock_rate / preescale / (3 * clkhi + 2 + 2 / preescale));
310 abs_error = speed > rate ? speed - rate : rate - speed;
312 if (abs_error < error) {
313 best_pre = preescale;
322 /* Standard, fast, fast mode plus and ultra-fast transfers. */
323 val = LPI2C_MCCR0_CLKHI(best_clkhi);
325 val |= LPI2C_MCCR0_CLKLO(3) | LPI2C_MCCR0_SETHOLD(2) | LPI2C_MCCR0_DATAVD(1);
327 val |= LPI2C_MCCR0_CLKLO(2 * best_clkhi) | LPI2C_MCCR0_SETHOLD(best_clkhi) |
328 LPI2C_MCCR0_DATAVD(best_clkhi / 2);
329 writel(val, ®s->mccr0);
331 for (i = 0; i < 8; i++) {
332 if (best_pre == (1 << i)) {
338 val = readl(®s->mcfgr1) & ~LPI2C_MCFGR1_PRESCALE_MASK;
339 writel(val | LPI2C_MCFGR1_PRESCALE(best_pre), ®s->mcfgr1);
342 val = readl(®s->mcr) & ~LPI2C_MCR_MEN_MASK;
343 writel(val | LPI2C_MCR_MEN(1), ®s->mcr);
349 static int bus_i2c_init(struct udevice *bus, int speed)
354 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
355 struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base);
356 /* reset peripheral */
357 writel(LPI2C_MCR_RST_MASK, ®s->mcr);
358 writel(0x0, ®s->mcr);
359 /* Disable Dozen mode */
360 writel(LPI2C_MCR_DBGEN(0) | LPI2C_MCR_DOZEN(1), ®s->mcr);
361 /* host request disable, active high, external pin */
362 val = readl(®s->mcfgr0);
363 val &= (~(LPI2C_MCFGR0_HREN_MASK | LPI2C_MCFGR0_HRPOL_MASK |
364 LPI2C_MCFGR0_HRSEL_MASK));
365 val |= LPI2C_MCFGR0_HRPOL(0x1);
366 writel(val, ®s->mcfgr0);
367 /* pincfg and ignore ack */
368 val = readl(®s->mcfgr1);
369 val &= ~(LPI2C_MCFGR1_PINCFG_MASK | LPI2C_MCFGR1_IGNACK_MASK);
370 val |= LPI2C_MCFGR1_PINCFG(0x0); /* 2 pin open drain */
371 val |= LPI2C_MCFGR1_IGNACK(0x0); /* ignore nack */
372 writel(val, ®s->mcfgr1);
374 ret = bus_i2c_set_bus_speed(bus, speed);
376 /* enable lpi2c in master mode */
377 val = readl(®s->mcr) & ~LPI2C_MCR_MEN_MASK;
378 writel(val | LPI2C_MCR_MEN(1), ®s->mcr);
380 debug("i2c : controller bus %d, speed %d:\n", bus->seq, speed);
385 static int imx_lpi2c_probe_chip(struct udevice *bus, u32 chip,
388 lpi2c_status_t result;
390 result = bus_i2c_start(bus, chip, 0);
393 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
397 result = bus_i2c_stop(bus);
399 bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
404 static int imx_lpi2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
406 int ret = 0, ret_stop;
408 for (; nmsgs > 0; nmsgs--, msg++) {
409 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
410 if (msg->flags & I2C_M_RD)
411 ret = bus_i2c_read(bus, msg->addr, msg->buf, msg->len);
413 ret = bus_i2c_write(bus, msg->addr, msg->buf,
421 debug("i2c_write: error sending\n");
423 ret_stop = bus_i2c_stop(bus);
425 debug("i2c_xfer: stop bus error\n");
432 static int imx_lpi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
434 return bus_i2c_set_bus_speed(bus, speed);
437 __weak int enable_i2c_clk(unsigned char enable, unsigned int i2c_num)
442 static int imx_lpi2c_probe(struct udevice *bus)
444 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
448 i2c_bus->driver_data = dev_get_driver_data(bus);
450 addr = dev_read_addr(bus);
451 if (addr == FDT_ADDR_T_NONE)
454 i2c_bus->base = addr;
455 i2c_bus->index = bus->seq;
458 /* power up i2c resource */
459 ret = init_i2c_power(bus->seq);
461 debug("init_i2c_power err = %d\n", ret);
465 if (IS_ENABLED(CONFIG_CLK)) {
466 ret = clk_get_by_name(bus, "per", &i2c_bus->per_clk);
468 dev_err(bus, "Failed to get per clk\n");
471 ret = clk_enable(&i2c_bus->per_clk);
473 dev_err(bus, "Failed to enable per clk\n");
477 ret = clk_get_by_name(bus, "ipg", &i2c_bus->ipg_clk);
479 dev_err(bus, "Failed to get ipg clk\n");
482 ret = clk_enable(&i2c_bus->ipg_clk);
484 dev_err(bus, "Failed to enable ipg clk\n");
488 /* To i.MX7ULP, only i2c4-7 can be handled by A7 core */
489 ret = enable_i2c_clk(1, bus->seq);
494 ret = bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE);
498 debug("i2c : controller bus %d at 0x%lx , speed %d: ",
499 bus->seq, i2c_bus->base,
505 static const struct dm_i2c_ops imx_lpi2c_ops = {
506 .xfer = imx_lpi2c_xfer,
507 .probe_chip = imx_lpi2c_probe_chip,
508 .set_bus_speed = imx_lpi2c_set_bus_speed,
511 static const struct udevice_id imx_lpi2c_ids[] = {
512 { .compatible = "fsl,imx7ulp-lpi2c", },
513 { .compatible = "fsl,imx8qm-lpi2c", },
517 U_BOOT_DRIVER(imx_lpi2c) = {
520 .of_match = imx_lpi2c_ids,
521 .probe = imx_lpi2c_probe,
522 .priv_auto = sizeof(struct imx_lpi2c_bus),
523 .ops = &imx_lpi2c_ops,