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