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