Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Boundary Devices Inc. |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 7 | #include <malloc.h> |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 8 | #include <asm/arch/clock.h> |
| 9 | #include <asm/arch/imx-regs.h> |
| 10 | #include <asm/errno.h> |
| 11 | #include <asm/gpio.h> |
| 12 | #include <asm/imx-common/mxc_i2c.h> |
| 13 | #include <watchdog.h> |
| 14 | |
| 15 | static int force_idle_bus(void *priv) |
| 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 |
| 70 | }; |
| 71 | |
| 72 | /* i2c_index can be from 0 - 2 */ |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 73 | int setup_i2c(unsigned i2c_index, int speed, int slave_addr, |
| 74 | struct i2c_pads_info *p) |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 75 | { |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 76 | char *name1, *name2; |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 77 | int ret; |
| 78 | |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 79 | if (i2c_index >= ARRAY_SIZE(i2c_bases)) |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 80 | return -EINVAL; |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 81 | |
| 82 | name1 = malloc(9); |
| 83 | name2 = malloc(9); |
| 84 | if (!name1 || !name2) |
| 85 | return -ENOMEM; |
| 86 | |
| 87 | sprintf(name1, "i2c_sda%d", i2c_index); |
| 88 | sprintf(name2, "i2c_scl%d", i2c_index); |
| 89 | ret = gpio_request(p->sda.gp, name1); |
| 90 | if (ret) |
| 91 | goto err_req1; |
| 92 | |
| 93 | ret = gpio_request(p->scl.gp, name2); |
| 94 | if (ret) |
| 95 | goto err_req2; |
| 96 | |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 97 | /* Enable i2c clock */ |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 98 | ret = enable_i2c_clk(1, i2c_index); |
| 99 | if (ret) |
| 100 | goto err_clk; |
| 101 | |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 102 | /* Make sure bus is idle */ |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 103 | ret = force_idle_bus(p); |
| 104 | if (ret) |
| 105 | goto err_idle; |
| 106 | |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 107 | bus_i2c_init(i2c_bases[i2c_index], speed, slave_addr, |
| 108 | force_idle_bus, p); |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | |
| 112 | err_idle: |
| 113 | err_clk: |
Simon Glass | c6f3f32 | 2014-10-02 17:17:23 +0300 | [diff] [blame] | 114 | gpio_free(p->scl.gp); |
| 115 | err_req2: |
| 116 | gpio_free(p->sda.gp); |
| 117 | err_req1: |
| 118 | free(name1); |
| 119 | free(name2); |
| 120 | |
Simon Glass | edbf8b4 | 2014-10-01 19:57:24 -0600 | [diff] [blame] | 121 | return ret; |
Troy Kisky | cc54a0f | 2012-07-19 08:18:25 +0000 | [diff] [blame] | 122 | } |