Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Boundary Devices Inc. |
| 3 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 7 | #include <malloc.h> |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 8 | #include <asm/arch/clock.h> |
| 9 | #include <asm/arch/imx-regs.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 11 | #include <asm/gpio.h> |
| 12 | #include <asm/imx-common/mxc_i2c.h> |
| 13 | #include <watchdog.h> |
| 14 | |
Peng Fan | 8262cb1 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 15 | int force_idle_bus(void *priv) |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 16 | { |
| 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; |
| 49 | WATCHDOG_RESET(); |
| 50 | 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 | } |
| 58 | exit: |
| 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 | |
| 64 | static 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 Schocher | 5c4b1e9 | 2015-05-18 10:56:24 +0200 | [diff] [blame] | 70 | #ifdef I2C4_BASE_ADDR |
| 71 | (void *)I2C4_BASE_ADDR, |
| 72 | #endif |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Heiko Schocher | 5c4b1e9 | 2015-05-18 10:56:24 +0200 | [diff] [blame] | 75 | /* i2c_index can be from 0 - 3 */ |
Simon Glass | 8d91436 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 76 | int setup_i2c(unsigned i2c_index, int speed, int slave_addr, |
| 77 | struct i2c_pads_info *p) |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 78 | { |
Marek Vasut | 5209fb0 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 79 | char name[9]; |
Simon Glass | 8d91436 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 80 | int ret; |
| 81 | |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 82 | if (i2c_index >= ARRAY_SIZE(i2c_bases)) |
Simon Glass | 8d91436 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 83 | return -EINVAL; |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 84 | |
Marek Vasut | 5209fb0 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 85 | snprintf(name, sizeof(name), "i2c_sda%01d", i2c_index); |
| 86 | ret = gpio_request(p->sda.gp, name); |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 87 | if (ret) |
Marek Vasut | 5209fb0 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 88 | return ret; |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 89 | |
Marek Vasut | 5209fb0 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 90 | snprintf(name, sizeof(name), "i2c_scl%01d", i2c_index); |
| 91 | ret = gpio_request(p->scl.gp, name); |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 92 | if (ret) |
Marek Vasut | 5209fb0 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 93 | goto err_req; |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 94 | |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 95 | /* Enable i2c clock */ |
Simon Glass | 8d91436 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 96 | ret = enable_i2c_clk(1, i2c_index); |
| 97 | if (ret) |
| 98 | goto err_clk; |
| 99 | |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 100 | /* Make sure bus is idle */ |
Simon Glass | 8d91436 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 101 | ret = force_idle_bus(p); |
| 102 | if (ret) |
| 103 | goto err_idle; |
| 104 | |
Peng Fan | 8262cb1 | 2015-05-15 07:29:12 +0800 | [diff] [blame] | 105 | #ifndef CONFIG_DM_I2C |
| 106 | bus_i2c_init(i2c_index, speed, slave_addr, force_idle_bus, p); |
| 107 | #endif |
Simon Glass | 8d91436 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | |
| 111 | err_idle: |
| 112 | err_clk: |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 113 | gpio_free(p->scl.gp); |
Marek Vasut | 5209fb0 | 2014-12-16 14:09:16 +0100 | [diff] [blame] | 114 | err_req: |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 115 | gpio_free(p->sda.gp); |
Simon Glass | 5c05d67 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 116 | |
Simon Glass | 8d91436 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 117 | return ret; |
Troy Kisky | d4fdc99 | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 118 | } |