Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Boundary Devices Inc. |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 4 | */ |
| 5 | #include <common.h> |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 6 | #include <malloc.h> |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 7 | #include <asm/arch/clock.h> |
| 8 | #include <asm/arch/imx-regs.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 9 | #include <linux/errno.h> |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 10 | #include <asm/gpio.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 11 | #include <asm/mach-imx/mxc_i2c.h> |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 12 | #include <watchdog.h> |
| 13 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 14 | int force_idle_bus(void *priv) |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 15 | { |
| 16 | int i; |
| 17 | int sda, scl; |
| 18 | ulong elapsed, start_time; |
| 19 | struct i2c_pads_info *p = (struct i2c_pads_info *)priv; |
| 20 | int ret = 0; |
| 21 | |
| 22 | gpio_direction_input(p->sda.gp); |
| 23 | gpio_direction_input(p->scl.gp); |
| 24 | |
| 25 | imx_iomux_v3_setup_pad(p->sda.gpio_mode); |
| 26 | imx_iomux_v3_setup_pad(p->scl.gpio_mode); |
| 27 | |
| 28 | sda = gpio_get_value(p->sda.gp); |
| 29 | scl = gpio_get_value(p->scl.gp); |
| 30 | if ((sda & scl) == 1) |
| 31 | goto exit; /* Bus is idle already */ |
| 32 | |
| 33 | printf("%s: sda=%d scl=%d sda.gp=0x%x scl.gp=0x%x\n", __func__, |
| 34 | sda, scl, p->sda.gp, p->scl.gp); |
| 35 | /* Send high and low on the SCL line */ |
| 36 | for (i = 0; i < 9; i++) { |
| 37 | gpio_direction_output(p->scl.gp, 0); |
| 38 | udelay(50); |
| 39 | gpio_direction_input(p->scl.gp); |
| 40 | udelay(50); |
| 41 | } |
| 42 | start_time = get_timer(0); |
| 43 | for (;;) { |
| 44 | sda = gpio_get_value(p->sda.gp); |
| 45 | scl = gpio_get_value(p->scl.gp); |
| 46 | if ((sda & scl) == 1) |
| 47 | break; |
| 48 | WATCHDOG_RESET(); |
| 49 | elapsed = get_timer(start_time); |
| 50 | if (elapsed > (CONFIG_SYS_HZ / 5)) { /* .2 seconds */ |
| 51 | ret = -EBUSY; |
| 52 | printf("%s: failed to clear bus, sda=%d scl=%d\n", |
| 53 | __func__, sda, scl); |
| 54 | break; |
| 55 | } |
| 56 | } |
| 57 | exit: |
| 58 | imx_iomux_v3_setup_pad(p->sda.i2c_mode); |
| 59 | imx_iomux_v3_setup_pad(p->scl.i2c_mode); |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | static void * const i2c_bases[] = { |
| 64 | (void *)I2C1_BASE_ADDR, |
| 65 | (void *)I2C2_BASE_ADDR, |
| 66 | #ifdef I2C3_BASE_ADDR |
| 67 | (void *)I2C3_BASE_ADDR, |
| 68 | #endif |
Heiko Schocher | 21a2694 | 2015-05-18 10:56:24 +0200 | [diff] [blame] | 69 | #ifdef I2C4_BASE_ADDR |
| 70 | (void *)I2C4_BASE_ADDR, |
| 71 | #endif |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Heiko Schocher | 21a2694 | 2015-05-18 10:56:24 +0200 | [diff] [blame] | 74 | /* i2c_index can be from 0 - 3 */ |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 75 | int setup_i2c(unsigned i2c_index, int speed, int slave_addr, |
| 76 | struct i2c_pads_info *p) |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 77 | { |
Marek Vasut | cd3c589 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 78 | char name[9]; |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 79 | int ret; |
| 80 | |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 81 | if (i2c_index >= ARRAY_SIZE(i2c_bases)) |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 82 | return -EINVAL; |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 83 | |
Marek Vasut | cd3c589 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 84 | snprintf(name, sizeof(name), "i2c_sda%01d", i2c_index); |
| 85 | ret = gpio_request(p->sda.gp, name); |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 86 | if (ret) |
Marek Vasut | cd3c589 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 87 | return ret; |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 88 | |
Marek Vasut | cd3c589 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 89 | snprintf(name, sizeof(name), "i2c_scl%01d", i2c_index); |
| 90 | ret = gpio_request(p->scl.gp, name); |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 91 | if (ret) |
Marek Vasut | cd3c589 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 92 | goto err_req; |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 93 | |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 94 | /* Enable i2c clock */ |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 95 | ret = enable_i2c_clk(1, i2c_index); |
| 96 | if (ret) |
| 97 | goto err_clk; |
| 98 | |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 99 | /* Make sure bus is idle */ |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 100 | ret = force_idle_bus(p); |
| 101 | if (ret) |
| 102 | goto err_idle; |
| 103 | |
Peng Fan | 71204e9 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 104 | #ifndef CONFIG_DM_I2C |
| 105 | bus_i2c_init(i2c_index, speed, slave_addr, force_idle_bus, p); |
| 106 | #endif |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | |
| 110 | err_idle: |
| 111 | err_clk: |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 112 | gpio_free(p->scl.gp); |
Marek Vasut | cd3c589 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 113 | err_req: |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 114 | gpio_free(p->sda.gp); |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 115 | |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 116 | return ret; |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 117 | } |