blob: 8d3e0555a179c26244962022e5fddb4456496bbe [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Peng Fand684adb2017-02-24 09:54:18 +08002/*
3 * Copyright 2016 Freescale Semiconductors, Inc.
Peng Fand684adb2017-02-24 09:54:18 +08004 */
5
6#include <common.h>
7#include <errno.h>
8#include <asm/io.h>
9#include <asm/arch/clock.h>
10#include <asm/arch/imx-regs.h>
11#include <asm/arch/imx_lpi2c.h>
12#include <asm/arch/sys_proto.h>
13#include <dm.h>
14#include <fdtdec.h>
15#include <i2c.h>
16
Peng Fand684adb2017-02-24 09:54:18 +080017#define LPI2C_FIFO_SIZE 4
18#define LPI2C_TIMEOUT_MS 100
19
20/* Weak linked function for overridden by some SoC power function */
21int __weak init_i2c_power(unsigned i2c_num)
22{
23 return 0;
24}
25
Simon Glassba1dea42017-05-17 17:18:05 -060026static int imx_lpci2c_check_busy_bus(const struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080027{
Peng Fand684adb2017-02-24 09:54:18 +080028 lpi2c_status_t result = LPI2C_SUCESS;
29 u32 status;
30
31 status = readl(&regs->msr);
32
33 if ((status & LPI2C_MSR_BBF_MASK) && !(status & LPI2C_MSR_MBF_MASK))
34 result = LPI2C_BUSY;
35
36 return result;
37}
38
Simon Glassba1dea42017-05-17 17:18:05 -060039static int imx_lpci2c_check_clear_error(struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080040{
Peng Fand684adb2017-02-24 09:54:18 +080041 lpi2c_status_t result = LPI2C_SUCESS;
42 u32 val, status;
43
44 status = readl(&regs->msr);
45 /* errors to check for */
46 status &= LPI2C_MSR_NDF_MASK | LPI2C_MSR_ALF_MASK |
47 LPI2C_MSR_FEF_MASK | LPI2C_MSR_PLTF_MASK;
48
49 if (status) {
50 if (status & LPI2C_MSR_PLTF_MASK)
51 result = LPI2C_PIN_LOW_TIMEOUT_ERR;
52 else if (status & LPI2C_MSR_ALF_MASK)
53 result = LPI2C_ARB_LOST_ERR;
54 else if (status & LPI2C_MSR_NDF_MASK)
55 result = LPI2C_NAK_ERR;
56 else if (status & LPI2C_MSR_FEF_MASK)
57 result = LPI2C_FIFO_ERR;
58
59 /* clear status flags */
60 writel(0x7f00, &regs->msr);
61 /* reset fifos */
62 val = readl(&regs->mcr);
63 val |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK;
64 writel(val, &regs->mcr);
65 }
66
67 return result;
68}
69
Simon Glassba1dea42017-05-17 17:18:05 -060070static int bus_i2c_wait_for_tx_ready(struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +080071{
Peng Fand684adb2017-02-24 09:54:18 +080072 lpi2c_status_t result = LPI2C_SUCESS;
73 u32 txcount = 0;
74 ulong start_time = get_timer(0);
75
76 do {
77 txcount = LPI2C_MFSR_TXCOUNT(readl(&regs->mfsr));
78 txcount = LPI2C_FIFO_SIZE - txcount;
Simon Glassba1dea42017-05-17 17:18:05 -060079 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +080080 if (result) {
81 debug("i2c: wait for tx ready: result 0x%x\n", result);
82 return result;
83 }
84 if (get_timer(start_time) > LPI2C_TIMEOUT_MS) {
85 debug("i2c: wait for tx ready: timeout\n");
86 return -1;
87 }
88 } while (!txcount);
89
90 return result;
91}
92
Simon Glassba1dea42017-05-17 17:18:05 -060093static int bus_i2c_send(struct imx_lpi2c_reg *regs, u8 *txbuf, int len)
Peng Fand684adb2017-02-24 09:54:18 +080094{
Peng Fand684adb2017-02-24 09:54:18 +080095 lpi2c_status_t result = LPI2C_SUCESS;
96
97 /* empty tx */
98 if (!len)
99 return result;
100
101 while (len--) {
Simon Glassba1dea42017-05-17 17:18:05 -0600102 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800103 if (result) {
104 debug("i2c: send wait fot tx ready: %d\n", result);
105 return result;
106 }
107 writel(*txbuf++, &regs->mtdr);
108 }
109
110 return result;
111}
112
Simon Glassba1dea42017-05-17 17:18:05 -0600113static int bus_i2c_receive(struct imx_lpi2c_reg *regs, u8 *rxbuf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800114{
Peng Fand684adb2017-02-24 09:54:18 +0800115 lpi2c_status_t result = LPI2C_SUCESS;
116 u32 val;
117 ulong start_time = get_timer(0);
118
119 /* empty read */
120 if (!len)
121 return result;
122
Simon Glassba1dea42017-05-17 17:18:05 -0600123 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800124 if (result) {
125 debug("i2c: receive wait fot tx ready: %d\n", result);
126 return result;
127 }
128
129 /* clear all status flags */
130 writel(0x7f00, &regs->msr);
131 /* send receive command */
132 val = LPI2C_MTDR_CMD(0x1) | LPI2C_MTDR_DATA(len - 1);
133 writel(val, &regs->mtdr);
134
135 while (len--) {
136 do {
Simon Glassba1dea42017-05-17 17:18:05 -0600137 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800138 if (result) {
Simon Glassba1dea42017-05-17 17:18:05 -0600139 debug("i2c: receive check clear error: %d\n",
140 result);
Peng Fand684adb2017-02-24 09:54:18 +0800141 return result;
142 }
143 if (get_timer(start_time) > LPI2C_TIMEOUT_MS) {
144 debug("i2c: receive mrdr: timeout\n");
145 return -1;
146 }
147 val = readl(&regs->mrdr);
148 } while (val & LPI2C_MRDR_RXEMPTY_MASK);
149 *rxbuf++ = LPI2C_MRDR_DATA(val);
150 }
151
152 return result;
153}
154
Simon Glassba1dea42017-05-17 17:18:05 -0600155static int bus_i2c_start(struct imx_lpi2c_reg *regs, u8 addr, u8 dir)
Peng Fand684adb2017-02-24 09:54:18 +0800156{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100157 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800158 u32 val;
159
Simon Glassba1dea42017-05-17 17:18:05 -0600160 result = imx_lpci2c_check_busy_bus(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800161 if (result) {
162 debug("i2c: start check busy bus: 0x%x\n", result);
163 return result;
164 }
165 /* clear all status flags */
166 writel(0x7f00, &regs->msr);
167 /* turn off auto-stop condition */
168 val = readl(&regs->mcfgr1) & ~LPI2C_MCFGR1_AUTOSTOP_MASK;
169 writel(val, &regs->mcfgr1);
170 /* wait tx fifo ready */
Simon Glassba1dea42017-05-17 17:18:05 -0600171 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800172 if (result) {
173 debug("i2c: start wait for tx ready: 0x%x\n", result);
174 return result;
175 }
176 /* issue start command */
177 val = LPI2C_MTDR_CMD(0x4) | (addr << 0x1) | dir;
178 writel(val, &regs->mtdr);
179
180 return result;
181}
Simon Glassba1dea42017-05-17 17:18:05 -0600182
183static int bus_i2c_stop(struct imx_lpi2c_reg *regs)
Peng Fand684adb2017-02-24 09:54:18 +0800184{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100185 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800186 u32 status;
187
Simon Glassba1dea42017-05-17 17:18:05 -0600188 result = bus_i2c_wait_for_tx_ready(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800189 if (result) {
190 debug("i2c: stop wait for tx ready: 0x%x\n", result);
191 return result;
192 }
193
194 /* send stop command */
195 writel(LPI2C_MTDR_CMD(0x2), &regs->mtdr);
196
197 while (result == LPI2C_SUCESS) {
198 status = readl(&regs->msr);
Simon Glassba1dea42017-05-17 17:18:05 -0600199 result = imx_lpci2c_check_clear_error(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800200 /* stop detect flag */
201 if (status & LPI2C_MSR_SDF_MASK) {
202 /* clear stop flag */
203 status &= LPI2C_MSR_SDF_MASK;
204 writel(status, &regs->msr);
205 break;
206 }
207 }
208
209 return result;
210}
211
Simon Glassba1dea42017-05-17 17:18:05 -0600212static int bus_i2c_read(struct imx_lpi2c_reg *regs, u32 chip, u8 *buf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800213{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100214 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800215
Simon Glassba1dea42017-05-17 17:18:05 -0600216 result = bus_i2c_start(regs, chip, 1);
Peng Fand684adb2017-02-24 09:54:18 +0800217 if (result)
218 return result;
Simon Glassba1dea42017-05-17 17:18:05 -0600219 result = bus_i2c_receive(regs, buf, len);
Peng Fand684adb2017-02-24 09:54:18 +0800220 if (result)
221 return result;
Simon Glassba1dea42017-05-17 17:18:05 -0600222 result = bus_i2c_stop(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800223 if (result)
224 return result;
225
226 return result;
227}
228
Simon Glassba1dea42017-05-17 17:18:05 -0600229static int bus_i2c_write(struct imx_lpi2c_reg *regs, u32 chip, u8 *buf, int len)
Peng Fand684adb2017-02-24 09:54:18 +0800230{
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100231 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800232
Simon Glassba1dea42017-05-17 17:18:05 -0600233 result = bus_i2c_start(regs, chip, 0);
Peng Fand684adb2017-02-24 09:54:18 +0800234 if (result)
235 return result;
Simon Glassba1dea42017-05-17 17:18:05 -0600236 result = bus_i2c_send(regs, buf, len);
Peng Fand684adb2017-02-24 09:54:18 +0800237 if (result)
238 return result;
Simon Glassba1dea42017-05-17 17:18:05 -0600239 result = bus_i2c_stop(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800240 if (result)
241 return result;
242
243 return result;
244}
245
246
247static int bus_i2c_set_bus_speed(struct udevice *bus, int speed)
248{
Simon Glassba1dea42017-05-17 17:18:05 -0600249 struct imx_lpi2c_reg *regs;
Peng Fand684adb2017-02-24 09:54:18 +0800250 u32 val;
251 u32 preescale = 0, best_pre = 0, clkhi = 0;
252 u32 best_clkhi = 0, abs_error = 0, rate;
253 u32 error = 0xffffffff;
254 u32 clock_rate;
255 bool mode;
256 int i;
257
Simon Glassba1dea42017-05-17 17:18:05 -0600258 regs = (struct imx_lpi2c_reg *)devfdt_get_addr(bus);
Peng Fan32306dd2018-01-02 15:41:52 +0800259 clock_rate = imx_get_i2cclk(bus->seq);
Peng Fand684adb2017-02-24 09:54:18 +0800260 if (!clock_rate)
261 return -EPERM;
262
263 mode = (readl(&regs->mcr) & LPI2C_MCR_MEN_MASK) >> LPI2C_MCR_MEN_SHIFT;
264 /* disable master mode */
265 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
266 writel(val | LPI2C_MCR_MEN(0), &regs->mcr);
267
268 for (preescale = 1; (preescale <= 128) &&
269 (error != 0); preescale = 2 * preescale) {
270 for (clkhi = 1; clkhi < 32; clkhi++) {
271 if (clkhi == 1)
272 rate = (clock_rate / preescale) / (1 + 3 + 2 + 2 / preescale);
273 else
274 rate = (clock_rate / preescale / (3 * clkhi + 2 + 2 / preescale));
275
276 abs_error = speed > rate ? speed - rate : rate - speed;
277
278 if (abs_error < error) {
279 best_pre = preescale;
280 best_clkhi = clkhi;
281 error = abs_error;
282 if (abs_error == 0)
283 break;
284 }
285 }
286 }
287
288 /* Standard, fast, fast mode plus and ultra-fast transfers. */
289 val = LPI2C_MCCR0_CLKHI(best_clkhi);
290 if (best_clkhi < 2)
291 val |= LPI2C_MCCR0_CLKLO(3) | LPI2C_MCCR0_SETHOLD(2) | LPI2C_MCCR0_DATAVD(1);
292 else
293 val |= LPI2C_MCCR0_CLKLO(2 * best_clkhi) | LPI2C_MCCR0_SETHOLD(best_clkhi) |
294 LPI2C_MCCR0_DATAVD(best_clkhi / 2);
295 writel(val, &regs->mccr0);
296
297 for (i = 0; i < 8; i++) {
298 if (best_pre == (1 << i)) {
299 best_pre = i;
300 break;
301 }
302 }
303
304 val = readl(&regs->mcfgr1) & ~LPI2C_MCFGR1_PRESCALE_MASK;
305 writel(val | LPI2C_MCFGR1_PRESCALE(best_pre), &regs->mcfgr1);
306
307 if (mode) {
308 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
309 writel(val | LPI2C_MCR_MEN(1), &regs->mcr);
310 }
311
312 return 0;
313}
314
315static int bus_i2c_init(struct udevice *bus, int speed)
316{
Simon Glassba1dea42017-05-17 17:18:05 -0600317 struct imx_lpi2c_reg *regs;
Peng Fand684adb2017-02-24 09:54:18 +0800318 u32 val;
319 int ret;
320
Simon Glassba1dea42017-05-17 17:18:05 -0600321 regs = (struct imx_lpi2c_reg *)devfdt_get_addr(bus);
Peng Fand684adb2017-02-24 09:54:18 +0800322 /* reset peripheral */
323 writel(LPI2C_MCR_RST_MASK, &regs->mcr);
324 writel(0x0, &regs->mcr);
325 /* Disable Dozen mode */
326 writel(LPI2C_MCR_DBGEN(0) | LPI2C_MCR_DOZEN(1), &regs->mcr);
327 /* host request disable, active high, external pin */
328 val = readl(&regs->mcfgr0);
329 val &= (~(LPI2C_MCFGR0_HREN_MASK | LPI2C_MCFGR0_HRPOL_MASK |
330 LPI2C_MCFGR0_HRSEL_MASK));
331 val |= LPI2C_MCFGR0_HRPOL(0x1);
332 writel(val, &regs->mcfgr0);
333 /* pincfg and ignore ack */
334 val = readl(&regs->mcfgr1);
335 val &= ~(LPI2C_MCFGR1_PINCFG_MASK | LPI2C_MCFGR1_IGNACK_MASK);
336 val |= LPI2C_MCFGR1_PINCFG(0x0); /* 2 pin open drain */
337 val |= LPI2C_MCFGR1_IGNACK(0x0); /* ignore nack */
338 writel(val, &regs->mcfgr1);
339
340 ret = bus_i2c_set_bus_speed(bus, speed);
341
342 /* enable lpi2c in master mode */
343 val = readl(&regs->mcr) & ~LPI2C_MCR_MEN_MASK;
344 writel(val | LPI2C_MCR_MEN(1), &regs->mcr);
345
346 debug("i2c : controller bus %d, speed %d:\n", bus->seq, speed);
347
348 return ret;
349}
350
351static int imx_lpi2c_probe_chip(struct udevice *bus, u32 chip,
352 u32 chip_flags)
353{
Simon Glassba1dea42017-05-17 17:18:05 -0600354 struct imx_lpi2c_reg *regs;
Heinrich Schuchardt1a5d4852018-03-18 11:14:56 +0100355 lpi2c_status_t result;
Peng Fand684adb2017-02-24 09:54:18 +0800356
Simon Glassba1dea42017-05-17 17:18:05 -0600357 regs = (struct imx_lpi2c_reg *)devfdt_get_addr(bus);
358 result = bus_i2c_start(regs, chip, 0);
Peng Fand684adb2017-02-24 09:54:18 +0800359 if (result) {
Simon Glassba1dea42017-05-17 17:18:05 -0600360 bus_i2c_stop(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800361 bus_i2c_init(bus, 100000);
362 return result;
363 }
364
Simon Glassba1dea42017-05-17 17:18:05 -0600365 result = bus_i2c_stop(regs);
Peng Fand684adb2017-02-24 09:54:18 +0800366 if (result) {
367 bus_i2c_init(bus, 100000);
368 return -result;
369 }
370
371 return result;
372}
373
374static int imx_lpi2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
375{
Simon Glassba1dea42017-05-17 17:18:05 -0600376 struct imx_lpi2c_reg *regs;
Peng Fand684adb2017-02-24 09:54:18 +0800377 int ret = 0;
378
Simon Glassba1dea42017-05-17 17:18:05 -0600379 regs = (struct imx_lpi2c_reg *)devfdt_get_addr(bus);
Peng Fand684adb2017-02-24 09:54:18 +0800380 for (; nmsgs > 0; nmsgs--, msg++) {
381 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
382 if (msg->flags & I2C_M_RD)
Simon Glassba1dea42017-05-17 17:18:05 -0600383 ret = bus_i2c_read(regs, msg->addr, msg->buf, msg->len);
Peng Fand684adb2017-02-24 09:54:18 +0800384 else {
Simon Glassba1dea42017-05-17 17:18:05 -0600385 ret = bus_i2c_write(regs, msg->addr, msg->buf,
Peng Fand684adb2017-02-24 09:54:18 +0800386 msg->len);
387 if (ret)
388 break;
389 }
390 }
391
392 if (ret)
393 debug("i2c_write: error sending\n");
394
395 return ret;
396}
397
398static int imx_lpi2c_set_bus_speed(struct udevice *bus, unsigned int speed)
399{
400 return bus_i2c_set_bus_speed(bus, speed);
401}
402
403static int imx_lpi2c_probe(struct udevice *bus)
404{
405 struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus);
406 fdt_addr_t addr;
407 int ret;
408
409 i2c_bus->driver_data = dev_get_driver_data(bus);
410
Simon Glassba1dea42017-05-17 17:18:05 -0600411 addr = devfdt_get_addr(bus);
Peng Fand684adb2017-02-24 09:54:18 +0800412 if (addr == FDT_ADDR_T_NONE)
Simon Glassf44b4bf2017-09-17 16:54:53 -0600413 return -EINVAL;
Peng Fand684adb2017-02-24 09:54:18 +0800414
415 i2c_bus->base = addr;
416 i2c_bus->index = bus->seq;
417 i2c_bus->bus = bus;
418
419 /* power up i2c resource */
Peng Fan32306dd2018-01-02 15:41:52 +0800420 ret = init_i2c_power(bus->seq);
Peng Fand684adb2017-02-24 09:54:18 +0800421 if (ret) {
422 debug("init_i2c_power err = %d\n", ret);
423 return ret;
424 }
425
Peng Fan32306dd2018-01-02 15:41:52 +0800426 /* To i.MX7ULP, only i2c4-7 can be handled by A7 core */
427 ret = enable_i2c_clk(1, bus->seq);
Peng Fand684adb2017-02-24 09:54:18 +0800428 if (ret < 0)
429 return ret;
430
431 ret = bus_i2c_init(bus, 100000);
432 if (ret < 0)
433 return ret;
434
435 debug("i2c : controller bus %d at %lu , speed %d: ",
436 bus->seq, i2c_bus->base,
437 i2c_bus->speed);
438
439 return 0;
440}
441
442static const struct dm_i2c_ops imx_lpi2c_ops = {
443 .xfer = imx_lpi2c_xfer,
444 .probe_chip = imx_lpi2c_probe_chip,
445 .set_bus_speed = imx_lpi2c_set_bus_speed,
446};
447
448static const struct udevice_id imx_lpi2c_ids[] = {
449 { .compatible = "fsl,imx7ulp-lpi2c", },
450 {}
451};
452
453U_BOOT_DRIVER(imx_lpi2c) = {
454 .name = "imx_lpi2c",
455 .id = UCLASS_I2C,
456 .of_match = imx_lpi2c_ids,
457 .probe = imx_lpi2c_probe,
458 .priv_auto_alloc_size = sizeof(struct imx_lpi2c_bus),
459 .ops = &imx_lpi2c_ops,
460};