blob: 256db150818a98753a26591bd8ca259bb4f46f4c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Troy Kiskyd4fdc992012-07-19 08:18:25 +00002/*
3 * Copyright (C) 2012 Boundary Devices Inc.
Troy Kiskyd4fdc992012-07-19 08:18:25 +00004 */
Simon Glass5c05d672014-10-02 17:17:23 +03005#include <malloc.h>
Tom Rinidec7ea02024-05-20 13:35:03 -06006#include <time.h>
Troy Kiskyd4fdc992012-07-19 08:18:25 +00007#include <asm/arch/clock.h>
8#include <asm/arch/imx-regs.h>
Simon Glassdbd79542020-05-10 11:40:11 -06009#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090010#include <linux/errno.h>
Troy Kiskyd4fdc992012-07-19 08:18:25 +000011#include <asm/gpio.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020012#include <asm/mach-imx/mxc_i2c.h>
Troy Kiskyd4fdc992012-07-19 08:18:25 +000013#include <watchdog.h>
14
Peng Fan8262cb12015-05-15 07:29:12 +080015int force_idle_bus(void *priv)
Troy Kiskyd4fdc992012-07-19 08:18:25 +000016{
17 int i;
18 int sda, scl;
19 ulong elapsed, start_time;
20 struct i2c_pads_info *p = (struct i2c_pads_info *)priv;
21 int ret = 0;
22
23 gpio_direction_input(p->sda.gp);
24 gpio_direction_input(p->scl.gp);
25
26 imx_iomux_v3_setup_pad(p->sda.gpio_mode);
27 imx_iomux_v3_setup_pad(p->scl.gpio_mode);
28
29 sda = gpio_get_value(p->sda.gp);
30 scl = gpio_get_value(p->scl.gp);
31 if ((sda & scl) == 1)
32 goto exit; /* Bus is idle already */
33
34 printf("%s: sda=%d scl=%d sda.gp=0x%x scl.gp=0x%x\n", __func__,
35 sda, scl, p->sda.gp, p->scl.gp);
36 /* Send high and low on the SCL line */
37 for (i = 0; i < 9; i++) {
38 gpio_direction_output(p->scl.gp, 0);
39 udelay(50);
40 gpio_direction_input(p->scl.gp);
41 udelay(50);
42 }
43 start_time = get_timer(0);
44 for (;;) {
45 sda = gpio_get_value(p->sda.gp);
46 scl = gpio_get_value(p->scl.gp);
47 if ((sda & scl) == 1)
48 break;
Stefan Roese80877fa2022-09-02 14:10:46 +020049 schedule();
Troy Kiskyd4fdc992012-07-19 08:18:25 +000050 elapsed = get_timer(start_time);
51 if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */
52 ret = -EBUSY;
53 printf("%s: failed to clear bus, sda=%d scl=%d\n",
54 __func__, sda, scl);
55 break;
56 }
57 }
58exit:
59 imx_iomux_v3_setup_pad(p->sda.i2c_mode);
60 imx_iomux_v3_setup_pad(p->scl.i2c_mode);
61 return ret;
62}
63
64static void * const i2c_bases[] = {
65 (void *)I2C1_BASE_ADDR,
66 (void *)I2C2_BASE_ADDR,
67#ifdef I2C3_BASE_ADDR
68 (void *)I2C3_BASE_ADDR,
69#endif
Heiko Schocher5c4b1e92015-05-18 10:56:24 +020070#ifdef I2C4_BASE_ADDR
71 (void *)I2C4_BASE_ADDR,
72#endif
Martyn Welchc445dc42022-10-25 10:54:59 +010073#ifdef I2C5_BASE_ADDR
74 (void *)I2C5_BASE_ADDR,
75#endif
76#ifdef I2C6_BASE_ADDR
77 (void *)I2C6_BASE_ADDR,
78#endif
Troy Kiskyd4fdc992012-07-19 08:18:25 +000079};
80
Heiko Schocher5c4b1e92015-05-18 10:56:24 +020081/* i2c_index can be from 0 - 3 */
Simon Glass8d914362014-10-01 19:57:24 -060082int setup_i2c(unsigned i2c_index, int speed, int slave_addr,
83 struct i2c_pads_info *p)
Troy Kiskyd4fdc992012-07-19 08:18:25 +000084{
Marek Vasut5209fb02014-12-16 14:09:16 +010085 char name[9];
Simon Glass8d914362014-10-01 19:57:24 -060086 int ret;
87
Troy Kiskyd4fdc992012-07-19 08:18:25 +000088 if (i2c_index >= ARRAY_SIZE(i2c_bases))
Simon Glass8d914362014-10-01 19:57:24 -060089 return -EINVAL;
Simon Glass5c05d672014-10-02 17:17:23 +030090
Marek Vasut5209fb02014-12-16 14:09:16 +010091 snprintf(name, sizeof(name), "i2c_sda%01d", i2c_index);
92 ret = gpio_request(p->sda.gp, name);
Simon Glass5c05d672014-10-02 17:17:23 +030093 if (ret)
Marek Vasut5209fb02014-12-16 14:09:16 +010094 return ret;
Simon Glass5c05d672014-10-02 17:17:23 +030095
Marek Vasut5209fb02014-12-16 14:09:16 +010096 snprintf(name, sizeof(name), "i2c_scl%01d", i2c_index);
97 ret = gpio_request(p->scl.gp, name);
Simon Glass5c05d672014-10-02 17:17:23 +030098 if (ret)
Marek Vasut5209fb02014-12-16 14:09:16 +010099 goto err_req;
Simon Glass5c05d672014-10-02 17:17:23 +0300100
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000101 /* Enable i2c clock */
Simon Glass8d914362014-10-01 19:57:24 -0600102 ret = enable_i2c_clk(1, i2c_index);
103 if (ret)
104 goto err_clk;
105
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000106 /* Make sure bus is idle */
Simon Glass8d914362014-10-01 19:57:24 -0600107 ret = force_idle_bus(p);
108 if (ret)
109 goto err_idle;
110
Igor Opaniukf7c91762021-02-09 13:52:45 +0200111#if !CONFIG_IS_ENABLED(DM_I2C)
Peng Fan8262cb12015-05-15 07:29:12 +0800112 bus_i2c_init(i2c_index, speed, slave_addr, force_idle_bus, p);
113#endif
Simon Glass8d914362014-10-01 19:57:24 -0600114
115 return 0;
116
117err_idle:
118err_clk:
Simon Glass5c05d672014-10-02 17:17:23 +0300119 gpio_free(p->scl.gp);
Marek Vasut5209fb02014-12-16 14:09:16 +0100120err_req:
Simon Glass5c05d672014-10-02 17:17:23 +0300121 gpio_free(p->sda.gp);
Simon Glass5c05d672014-10-02 17:17:23 +0300122
Simon Glass8d914362014-10-01 19:57:24 -0600123 return ret;
Troy Kiskyd4fdc992012-07-19 08:18:25 +0000124}