Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 2 | /* |
Nobuhiro Iwamatsu | b55b8ee | 2013-10-11 16:23:54 +0900 | [diff] [blame] | 3 | * Copyright (C) 2011, 2013 Renesas Solutions Corp. |
| 4 | * Copyright (C) 2011, 2013 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com> |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 5 | * |
Simon Glass | 2852709 | 2016-11-23 06:34:44 -0700 | [diff] [blame] | 6 | * NOTE: This driver should be converted to driver model before June 2017. |
Heinrich Schuchardt | 2799a69 | 2020-02-25 21:35:39 +0100 | [diff] [blame] | 7 | * Please see doc/driver-model/i2c-howto.rst for instructions. |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 11 | #include <i2c.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 14 | #include <asm/io.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 16 | |
Nobuhiro Iwamatsu | b55b8ee | 2013-10-11 16:23:54 +0900 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 19 | /* Every register is 32bit aligned, but only 8bits in size */ |
| 20 | #define ureg(name) u8 name; u8 __pad_##name##0; u16 __pad_##name##1; |
| 21 | struct sh_i2c { |
| 22 | ureg(icdr); |
| 23 | ureg(iccr); |
| 24 | ureg(icsr); |
| 25 | ureg(icic); |
| 26 | ureg(iccl); |
| 27 | ureg(icch); |
| 28 | }; |
| 29 | #undef ureg |
| 30 | |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 31 | /* ICCR */ |
| 32 | #define SH_I2C_ICCR_ICE (1 << 7) |
| 33 | #define SH_I2C_ICCR_RACK (1 << 6) |
| 34 | #define SH_I2C_ICCR_RTS (1 << 4) |
| 35 | #define SH_I2C_ICCR_BUSY (1 << 2) |
| 36 | #define SH_I2C_ICCR_SCP (1 << 0) |
| 37 | |
| 38 | /* ICSR / ICIC */ |
Tetsuyuki Kobayashi | 57d7c80 | 2012-09-13 19:07:57 +0000 | [diff] [blame] | 39 | #define SH_IC_BUSY (1 << 4) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 40 | #define SH_IC_TACK (1 << 2) |
| 41 | #define SH_IC_WAIT (1 << 1) |
| 42 | #define SH_IC_DTE (1 << 0) |
| 43 | |
Tetsuyuki Kobayashi | b1af67f | 2012-09-13 19:07:56 +0000 | [diff] [blame] | 44 | #ifdef CONFIG_SH_I2C_8BIT |
| 45 | /* store 8th bit of iccl and icch in ICIC register */ |
| 46 | #define SH_I2C_ICIC_ICCLB8 (1 << 7) |
| 47 | #define SH_I2C_ICIC_ICCHB8 (1 << 6) |
| 48 | #endif |
| 49 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 50 | static const struct sh_i2c *i2c_dev[CONFIG_SYS_I2C_SH_NUM_CONTROLLERS] = { |
| 51 | (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE0, |
| 52 | #ifdef CONFIG_SYS_I2C_SH_BASE1 |
| 53 | (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE1, |
| 54 | #endif |
| 55 | #ifdef CONFIG_SYS_I2C_SH_BASE2 |
| 56 | (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE2, |
| 57 | #endif |
| 58 | #ifdef CONFIG_SYS_I2C_SH_BASE3 |
| 59 | (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE3, |
| 60 | #endif |
| 61 | #ifdef CONFIG_SYS_I2C_SH_BASE4 |
| 62 | (struct sh_i2c *)CONFIG_SYS_I2C_SH_BASE4, |
| 63 | #endif |
| 64 | }; |
| 65 | |
Tetsuyuki Kobayashi | b1af67f | 2012-09-13 19:07:56 +0000 | [diff] [blame] | 66 | static u16 iccl, icch; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 67 | |
| 68 | #define IRQ_WAIT 1000 |
| 69 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 70 | static void sh_irq_dte(struct sh_i2c *dev) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 71 | { |
| 72 | int i; |
| 73 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 74 | for (i = 0; i < IRQ_WAIT; i++) { |
| 75 | if (SH_IC_DTE & readb(&dev->icsr)) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 76 | break; |
| 77 | udelay(10); |
| 78 | } |
| 79 | } |
| 80 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 81 | static int sh_irq_dte_with_tack(struct sh_i2c *dev) |
Tetsuyuki Kobayashi | d042d71 | 2012-09-13 19:08:00 +0000 | [diff] [blame] | 82 | { |
| 83 | int i; |
| 84 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 85 | for (i = 0; i < IRQ_WAIT; i++) { |
| 86 | if (SH_IC_DTE & readb(&dev->icsr)) |
Tetsuyuki Kobayashi | d042d71 | 2012-09-13 19:08:00 +0000 | [diff] [blame] | 87 | break; |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 88 | if (SH_IC_TACK & readb(&dev->icsr)) |
Tetsuyuki Kobayashi | d042d71 | 2012-09-13 19:08:00 +0000 | [diff] [blame] | 89 | return -1; |
| 90 | udelay(10); |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 95 | static void sh_irq_busy(struct sh_i2c *dev) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 96 | { |
| 97 | int i; |
| 98 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 99 | for (i = 0; i < IRQ_WAIT; i++) { |
| 100 | if (!(SH_IC_BUSY & readb(&dev->icsr))) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 101 | break; |
| 102 | udelay(10); |
| 103 | } |
| 104 | } |
| 105 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 106 | static int sh_i2c_set_addr(struct sh_i2c *dev, u8 chip, u8 addr, int stop) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 107 | { |
Tetsuyuki Kobayashi | d042d71 | 2012-09-13 19:08:00 +0000 | [diff] [blame] | 108 | u8 icic = SH_IC_TACK; |
Tetsuyuki Kobayashi | b1af67f | 2012-09-13 19:07:56 +0000 | [diff] [blame] | 109 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 110 | debug("%s: chip: %x, addr: %x iccl: %x, icch %x\n", |
| 111 | __func__, chip, addr, iccl, icch); |
| 112 | clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE); |
| 113 | setbits_8(&dev->iccr, SH_I2C_ICCR_ICE); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 114 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 115 | writeb(iccl & 0xff, &dev->iccl); |
| 116 | writeb(icch & 0xff, &dev->icch); |
Tetsuyuki Kobayashi | b1af67f | 2012-09-13 19:07:56 +0000 | [diff] [blame] | 117 | #ifdef CONFIG_SH_I2C_8BIT |
| 118 | if (iccl > 0xff) |
| 119 | icic |= SH_I2C_ICIC_ICCLB8; |
| 120 | if (icch > 0xff) |
| 121 | icic |= SH_I2C_ICIC_ICCHB8; |
| 122 | #endif |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 123 | writeb(icic, &dev->icic); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 124 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 125 | writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr); |
| 126 | sh_irq_dte(dev); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 127 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 128 | clrbits_8(&dev->icsr, SH_IC_TACK); |
| 129 | writeb(chip << 1, &dev->icdr); |
| 130 | if (sh_irq_dte_with_tack(dev) != 0) |
Tetsuyuki Kobayashi | d042d71 | 2012-09-13 19:08:00 +0000 | [diff] [blame] | 131 | return -1; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 132 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 133 | writeb(addr, &dev->icdr); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 134 | if (stop) |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 135 | writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS), &dev->iccr); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 136 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 137 | if (sh_irq_dte_with_tack(dev) != 0) |
Tetsuyuki Kobayashi | d042d71 | 2012-09-13 19:08:00 +0000 | [diff] [blame] | 138 | return -1; |
| 139 | return 0; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 142 | static void sh_i2c_finish(struct sh_i2c *dev) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 143 | { |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 144 | writeb(0, &dev->icsr); |
| 145 | clrbits_8(&dev->iccr, SH_I2C_ICCR_ICE); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 148 | static int |
| 149 | sh_i2c_raw_write(struct sh_i2c *dev, u8 chip, uint addr, u8 val) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 150 | { |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 151 | int ret = -1; |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 152 | if (sh_i2c_set_addr(dev, chip, addr, 0) != 0) |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 153 | goto exit0; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 154 | udelay(10); |
| 155 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 156 | writeb(val, &dev->icdr); |
| 157 | if (sh_irq_dte_with_tack(dev) != 0) |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 158 | goto exit0; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 159 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 160 | writeb((SH_I2C_ICCR_ICE | SH_I2C_ICCR_RTS), &dev->iccr); |
| 161 | if (sh_irq_dte_with_tack(dev) != 0) |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 162 | goto exit0; |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 163 | sh_irq_busy(dev); |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 164 | ret = 0; |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 165 | |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 166 | exit0: |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 167 | sh_i2c_finish(dev); |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 168 | return ret; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 171 | static int sh_i2c_raw_read(struct sh_i2c *dev, u8 chip, u8 addr) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 172 | { |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 173 | int ret = -1; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 174 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 175 | if (sh_i2c_set_addr(dev, chip, addr, 1) != 0) |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 176 | goto exit0; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 177 | udelay(100); |
| 178 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 179 | writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RTS|SH_I2C_ICCR_BUSY), &dev->iccr); |
| 180 | sh_irq_dte(dev); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 181 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 182 | writeb(chip << 1 | 0x01, &dev->icdr); |
| 183 | if (sh_irq_dte_with_tack(dev) != 0) |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 184 | goto exit0; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 185 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 186 | writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_SCP), &dev->iccr); |
| 187 | if (sh_irq_dte_with_tack(dev) != 0) |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 188 | goto exit0; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 189 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 190 | ret = readb(&dev->icdr) & 0xff; |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 191 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 192 | writeb((SH_I2C_ICCR_ICE|SH_I2C_ICCR_RACK), &dev->iccr); |
| 193 | readb(&dev->icdr); /* Dummy read */ |
| 194 | sh_irq_busy(dev); |
| 195 | |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 196 | exit0: |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 197 | sh_i2c_finish(dev); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 198 | |
| 199 | return ret; |
| 200 | } |
| 201 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 202 | static void |
| 203 | sh_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 204 | { |
| 205 | int num, denom, tmp; |
| 206 | |
Nobuhiro Iwamatsu | b55b8ee | 2013-10-11 16:23:54 +0900 | [diff] [blame] | 207 | /* No i2c support prior to relocation */ |
| 208 | if (!(gd->flags & GD_FLG_RELOC)) |
| 209 | return; |
| 210 | |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 211 | /* |
| 212 | * Calculate the value for iccl. From the data sheet: |
| 213 | * iccl = (p-clock / transfer-rate) * (L / (L + H)) |
| 214 | * where L and H are the SCL low and high ratio. |
| 215 | */ |
| 216 | num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_LOW; |
| 217 | denom = speed * (CONFIG_SH_I2C_DATA_HIGH + CONFIG_SH_I2C_DATA_LOW); |
| 218 | tmp = num * 10 / denom; |
| 219 | if (tmp % 10 >= 5) |
Tetsuyuki Kobayashi | b1af67f | 2012-09-13 19:07:56 +0000 | [diff] [blame] | 220 | iccl = (u16)((num/denom) + 1); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 221 | else |
Tetsuyuki Kobayashi | b1af67f | 2012-09-13 19:07:56 +0000 | [diff] [blame] | 222 | iccl = (u16)(num/denom); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 223 | |
| 224 | /* Calculate the value for icch. From the data sheet: |
| 225 | icch = (p clock / transfer rate) * (H / (L + H)) */ |
| 226 | num = CONFIG_SH_I2C_CLOCK * CONFIG_SH_I2C_DATA_HIGH; |
| 227 | tmp = num * 10 / denom; |
| 228 | if (tmp % 10 >= 5) |
Tetsuyuki Kobayashi | b1af67f | 2012-09-13 19:07:56 +0000 | [diff] [blame] | 229 | icch = (u16)((num/denom) + 1); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 230 | else |
Tetsuyuki Kobayashi | b1af67f | 2012-09-13 19:07:56 +0000 | [diff] [blame] | 231 | icch = (u16)(num/denom); |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 232 | |
| 233 | debug("clock: %d, speed %d, iccl: %x, icch: %x\n", |
| 234 | CONFIG_SH_I2C_CLOCK, speed, iccl, icch); |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 237 | static int sh_i2c_read(struct i2c_adapter *adap, uint8_t chip, |
| 238 | uint addr, int alen, u8 *data, int len) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 239 | { |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 240 | int ret, i; |
| 241 | struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr]; |
| 242 | |
| 243 | for (i = 0; i < len; i++) { |
| 244 | ret = sh_i2c_raw_read(dev, chip, addr + i); |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 245 | if (ret < 0) |
| 246 | return -1; |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 247 | |
| 248 | data[i] = ret & 0xff; |
| 249 | debug("%s: data[%d]: %02x\n", __func__, i, data[i]); |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int sh_i2c_write(struct i2c_adapter *adap, uint8_t chip, uint addr, |
| 256 | int alen, u8 *data, int len) |
| 257 | { |
| 258 | struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr]; |
| 259 | int i; |
| 260 | |
| 261 | for (i = 0; i < len; i++) { |
| 262 | debug("%s: data[%d]: %02x\n", __func__, i, data[i]); |
| 263 | if (sh_i2c_raw_write(dev, chip, addr + i, data[i]) != 0) |
| 264 | return -1; |
Tetsuyuki Kobayashi | 0e5fb33 | 2012-09-13 19:08:01 +0000 | [diff] [blame] | 265 | } |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 269 | static int |
| 270 | sh_i2c_probe(struct i2c_adapter *adap, u8 dev) |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 271 | { |
Tetsuyuki Kobayashi | 7a65768 | 2014-04-14 17:13:57 +0900 | [diff] [blame] | 272 | u8 dummy[1]; |
| 273 | |
| 274 | return sh_i2c_read(adap, dev, 0, 0, dummy, sizeof dummy); |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | static unsigned int sh_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 278 | unsigned int speed) |
| 279 | { |
| 280 | struct sh_i2c *dev = (struct sh_i2c *)i2c_dev[adap->hwadapnr]; |
| 281 | |
| 282 | sh_i2c_finish(dev); |
| 283 | sh_i2c_init(adap, speed, 0); |
| 284 | |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 289 | * Register RCAR i2c adapters |
Nobuhiro Iwamatsu | 3dab3e0 | 2011-11-14 18:27:04 +0000 | [diff] [blame] | 290 | */ |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 291 | U_BOOT_I2C_ADAP_COMPLETE(sh_0, sh_i2c_init, sh_i2c_probe, sh_i2c_read, |
Tom Rini | 6aa0754 | 2021-08-18 23:12:34 -0400 | [diff] [blame] | 292 | sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 0) |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 293 | #ifdef CONFIG_SYS_I2C_SH_BASE1 |
| 294 | U_BOOT_I2C_ADAP_COMPLETE(sh_1, sh_i2c_init, sh_i2c_probe, sh_i2c_read, |
Tom Rini | 6aa0754 | 2021-08-18 23:12:34 -0400 | [diff] [blame] | 295 | sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 1) |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 296 | #endif |
| 297 | #ifdef CONFIG_SYS_I2C_SH_BASE2 |
| 298 | U_BOOT_I2C_ADAP_COMPLETE(sh_2, sh_i2c_init, sh_i2c_probe, sh_i2c_read, |
Tom Rini | 6aa0754 | 2021-08-18 23:12:34 -0400 | [diff] [blame] | 299 | sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 2) |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 300 | #endif |
| 301 | #ifdef CONFIG_SYS_I2C_SH_BASE3 |
| 302 | U_BOOT_I2C_ADAP_COMPLETE(sh_3, sh_i2c_init, sh_i2c_probe, sh_i2c_read, |
Tom Rini | 6aa0754 | 2021-08-18 23:12:34 -0400 | [diff] [blame] | 303 | sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 3) |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 304 | #endif |
| 305 | #ifdef CONFIG_SYS_I2C_SH_BASE4 |
| 306 | U_BOOT_I2C_ADAP_COMPLETE(sh_4, sh_i2c_init, sh_i2c_probe, sh_i2c_read, |
Tom Rini | 6aa0754 | 2021-08-18 23:12:34 -0400 | [diff] [blame] | 307 | sh_i2c_write, sh_i2c_set_bus_speed, CONFIG_SYS_I2C_SPEED, 0, 4) |
Nobuhiro Iwamatsu | 2035d77 | 2013-10-29 13:33:51 +0900 | [diff] [blame] | 308 | #endif |