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