Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 3 | * Copyright (c) 2010-2011 NVIDIA Corporation |
| 4 | * NVIDIA Corporation <www.nvidia.com> |
| 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 10 | #include <dm.h> |
| 11 | #include <errno.h> |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 12 | #include <fdtdec.h> |
| 13 | #include <i2c.h> |
| 14 | #include <asm/io.h> |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 15 | #ifdef CONFIG_TEGRA186 |
| 16 | #include <clk.h> |
| 17 | #include <reset.h> |
| 18 | #else |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 19 | #include <asm/arch/clock.h> |
| 20 | #include <asm/arch/funcmux.h> |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 21 | #include <asm/arch/pinmux.h> |
Tom Warren | 150c249 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 22 | #include <asm/arch-tegra/clk_rst.h> |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 23 | #endif |
| 24 | #include <asm/arch/gpio.h> |
Tom Warren | 150c249 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 25 | #include <asm/arch-tegra/tegra_i2c.h> |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 26 | |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 27 | /* |
| 28 | * FIXME: TODO: This driver contains a number of ifdef CONFIG_TEGRA186 that |
| 29 | * should not be present. These are needed because newer Tegra SoCs support |
| 30 | * only the standard clock/reset APIs, whereas older Tegra SoCs support only |
| 31 | * a custom Tegra-specific API. ASAP the older Tegra SoCs' code should be |
| 32 | * fixed to implement the standard APIs, and all drivers converted to solely |
| 33 | * use the new standard APIs, with no ifdefs. |
| 34 | */ |
| 35 | |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 36 | DECLARE_GLOBAL_DATA_PTR; |
| 37 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 38 | enum i2c_type { |
| 39 | TYPE_114, |
| 40 | TYPE_STD, |
| 41 | TYPE_DVC, |
| 42 | }; |
| 43 | |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 44 | /* Information about i2c controller */ |
| 45 | struct i2c_bus { |
| 46 | int id; |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 47 | #ifdef CONFIG_TEGRA186 |
| 48 | struct reset_ctl reset_ctl; |
| 49 | struct clk clk; |
| 50 | #else |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 51 | enum periph_id periph_id; |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 52 | #endif |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 53 | int speed; |
| 54 | int pinmux_config; |
| 55 | struct i2c_control *control; |
| 56 | struct i2c_ctlr *regs; |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 57 | enum i2c_type type; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 58 | int inited; /* bus is inited */ |
| 59 | }; |
| 60 | |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 61 | static void set_packet_mode(struct i2c_bus *i2c_bus) |
| 62 | { |
| 63 | u32 config; |
| 64 | |
| 65 | config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK; |
| 66 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 67 | if (i2c_bus->type == TYPE_DVC) { |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 68 | struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs; |
| 69 | |
| 70 | writel(config, &dvc->cnfg); |
| 71 | } else { |
| 72 | writel(config, &i2c_bus->regs->cnfg); |
| 73 | /* |
| 74 | * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe |
| 75 | * issues, i.e., some slaves may be wrongly detected. |
| 76 | */ |
| 77 | setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | static void i2c_reset_controller(struct i2c_bus *i2c_bus) |
| 82 | { |
| 83 | /* Reset I2C controller. */ |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 84 | #ifdef CONFIG_TEGRA186 |
| 85 | reset_assert(&i2c_bus->reset_ctl); |
| 86 | udelay(1); |
| 87 | reset_deassert(&i2c_bus->reset_ctl); |
| 88 | udelay(1); |
| 89 | #else |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 90 | reset_periph(i2c_bus->periph_id, 1); |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 91 | #endif |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 92 | |
| 93 | /* re-program config register to packet mode */ |
| 94 | set_packet_mode(i2c_bus); |
| 95 | } |
| 96 | |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 97 | #ifdef CONFIG_TEGRA186 |
| 98 | static int i2c_init_clock(struct i2c_bus *i2c_bus, unsigned rate) |
| 99 | { |
| 100 | int ret; |
| 101 | |
| 102 | ret = reset_assert(&i2c_bus->reset_ctl); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | ret = clk_enable(&i2c_bus->clk); |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | ret = clk_set_rate(&i2c_bus->clk, rate); |
| 109 | if (IS_ERR_VALUE(ret)) |
| 110 | return ret; |
| 111 | ret = reset_deassert(&i2c_bus->reset_ctl); |
| 112 | if (ret) |
| 113 | return ret; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | #endif |
| 118 | |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 119 | static void i2c_init_controller(struct i2c_bus *i2c_bus) |
| 120 | { |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 121 | if (!i2c_bus->speed) |
| 122 | return; |
| 123 | debug("%s: speed=%d\n", __func__, i2c_bus->speed); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 124 | /* |
| 125 | * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8 |
| 126 | * here, in section 23.3.1, but in fact we seem to need a factor of |
| 127 | * 16 to get the right frequency. |
| 128 | */ |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 129 | #ifdef CONFIG_TEGRA186 |
| 130 | i2c_init_clock(i2c_bus, i2c_bus->speed * 2 * 8); |
| 131 | #else |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 132 | clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH, |
Tom Warren | e32624e | 2013-02-08 07:25:30 +0000 | [diff] [blame] | 133 | i2c_bus->speed * 2 * 8); |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 134 | #endif |
Tom Warren | e32624e | 2013-02-08 07:25:30 +0000 | [diff] [blame] | 135 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 136 | if (i2c_bus->type == TYPE_114) { |
Tom Warren | e32624e | 2013-02-08 07:25:30 +0000 | [diff] [blame] | 137 | /* |
| 138 | * T114 I2C went to a single clock source for standard/fast and |
| 139 | * HS clock speeds. The new clock rate setting calculation is: |
| 140 | * SCL = CLK_SOURCE.I2C / |
| 141 | * (CLK_MULT_STD_FAST_MODE * (I2C_CLK_DIV_STD_FAST_MODE+1) * |
| 142 | * I2C FREQUENCY DIVISOR) as per the T114 TRM (sec 30.3.1). |
| 143 | * |
| 144 | * NOTE: We do this here, after the initial clock/pll start, |
| 145 | * because if we read the clk_div reg before the controller |
| 146 | * is running, we hang, and we need it for the new calc. |
| 147 | */ |
| 148 | int clk_div_stdfst_mode = readl(&i2c_bus->regs->clk_div) >> 16; |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 149 | unsigned rate = CLK_MULT_STD_FAST_MODE * |
| 150 | (clk_div_stdfst_mode + 1) * i2c_bus->speed * 2; |
Tom Warren | e32624e | 2013-02-08 07:25:30 +0000 | [diff] [blame] | 151 | debug("%s: CLK_DIV_STD_FAST_MODE setting = %d\n", __func__, |
| 152 | clk_div_stdfst_mode); |
| 153 | |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 154 | #ifdef CONFIG_TEGRA186 |
| 155 | i2c_init_clock(i2c_bus, rate); |
| 156 | #else |
Tom Warren | e32624e | 2013-02-08 07:25:30 +0000 | [diff] [blame] | 157 | clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH, |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 158 | rate); |
| 159 | #endif |
Tom Warren | e32624e | 2013-02-08 07:25:30 +0000 | [diff] [blame] | 160 | } |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 161 | |
| 162 | /* Reset I2C controller. */ |
| 163 | i2c_reset_controller(i2c_bus); |
| 164 | |
| 165 | /* Configure I2C controller. */ |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 166 | if (i2c_bus->type == TYPE_DVC) { /* only for DVC I2C */ |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 167 | struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs; |
| 168 | |
| 169 | setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK); |
| 170 | } |
| 171 | |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 172 | #ifndef CONFIG_TEGRA186 |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 173 | funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config); |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 174 | #endif |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | static void send_packet_headers( |
| 178 | struct i2c_bus *i2c_bus, |
| 179 | struct i2c_trans_info *trans, |
Stephen Warren | 68049a0 | 2014-06-25 10:57:27 -0600 | [diff] [blame] | 180 | u32 packet_id, |
| 181 | bool end_with_repeated_start) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 182 | { |
| 183 | u32 data; |
| 184 | |
| 185 | /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */ |
| 186 | data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT; |
| 187 | data |= packet_id << PKT_HDR1_PKT_ID_SHIFT; |
| 188 | data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT; |
| 189 | writel(data, &i2c_bus->control->tx_fifo); |
| 190 | debug("pkt header 1 sent (0x%x)\n", data); |
| 191 | |
| 192 | /* prepare header2 */ |
| 193 | data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT; |
| 194 | writel(data, &i2c_bus->control->tx_fifo); |
| 195 | debug("pkt header 2 sent (0x%x)\n", data); |
| 196 | |
| 197 | /* prepare IO specific header: configure the slave address */ |
| 198 | data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT; |
| 199 | |
| 200 | /* Enable Read if it is not a write transaction */ |
| 201 | if (!(trans->flags & I2C_IS_WRITE)) |
| 202 | data |= PKT_HDR3_READ_MODE_MASK; |
Stephen Warren | 68049a0 | 2014-06-25 10:57:27 -0600 | [diff] [blame] | 203 | if (end_with_repeated_start) |
| 204 | data |= PKT_HDR3_REPEAT_START_MASK; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 205 | |
| 206 | /* Write I2C specific header */ |
| 207 | writel(data, &i2c_bus->control->tx_fifo); |
| 208 | debug("pkt header 3 sent (0x%x)\n", data); |
| 209 | } |
| 210 | |
| 211 | static int wait_for_tx_fifo_empty(struct i2c_control *control) |
| 212 | { |
| 213 | u32 count; |
| 214 | int timeout_us = I2C_TIMEOUT_USEC; |
| 215 | |
| 216 | while (timeout_us >= 0) { |
| 217 | count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK) |
| 218 | >> TX_FIFO_EMPTY_CNT_SHIFT; |
| 219 | if (count == I2C_FIFO_DEPTH) |
| 220 | return 1; |
| 221 | udelay(10); |
| 222 | timeout_us -= 10; |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static int wait_for_rx_fifo_notempty(struct i2c_control *control) |
| 229 | { |
| 230 | u32 count; |
| 231 | int timeout_us = I2C_TIMEOUT_USEC; |
| 232 | |
| 233 | while (timeout_us >= 0) { |
| 234 | count = (readl(&control->fifo_status) & TX_FIFO_FULL_CNT_MASK) |
| 235 | >> TX_FIFO_FULL_CNT_SHIFT; |
| 236 | if (count) |
| 237 | return 1; |
| 238 | udelay(10); |
| 239 | timeout_us -= 10; |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int wait_for_transfer_complete(struct i2c_control *control) |
| 246 | { |
| 247 | int int_status; |
| 248 | int timeout_us = I2C_TIMEOUT_USEC; |
| 249 | |
| 250 | while (timeout_us >= 0) { |
| 251 | int_status = readl(&control->int_status); |
| 252 | if (int_status & I2C_INT_NO_ACK_MASK) |
| 253 | return -int_status; |
| 254 | if (int_status & I2C_INT_ARBITRATION_LOST_MASK) |
| 255 | return -int_status; |
| 256 | if (int_status & I2C_INT_XFER_COMPLETE_MASK) |
| 257 | return 0; |
| 258 | |
| 259 | udelay(10); |
| 260 | timeout_us -= 10; |
| 261 | } |
| 262 | |
| 263 | return -1; |
| 264 | } |
| 265 | |
| 266 | static int send_recv_packets(struct i2c_bus *i2c_bus, |
| 267 | struct i2c_trans_info *trans) |
| 268 | { |
| 269 | struct i2c_control *control = i2c_bus->control; |
| 270 | u32 int_status; |
| 271 | u32 words; |
| 272 | u8 *dptr; |
| 273 | u32 local; |
| 274 | uchar last_bytes; |
| 275 | int error = 0; |
| 276 | int is_write = trans->flags & I2C_IS_WRITE; |
| 277 | |
| 278 | /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */ |
| 279 | int_status = readl(&control->int_status); |
| 280 | writel(int_status, &control->int_status); |
| 281 | |
Stephen Warren | 68049a0 | 2014-06-25 10:57:27 -0600 | [diff] [blame] | 282 | send_packet_headers(i2c_bus, trans, 1, |
| 283 | trans->flags & I2C_USE_REPEATED_START); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 284 | |
| 285 | words = DIV_ROUND_UP(trans->num_bytes, 4); |
| 286 | last_bytes = trans->num_bytes & 3; |
| 287 | dptr = trans->buf; |
| 288 | |
| 289 | while (words) { |
| 290 | u32 *wptr = (u32 *)dptr; |
| 291 | |
| 292 | if (is_write) { |
| 293 | /* deal with word alignment */ |
Stephen Warren | 981b14f | 2014-06-25 10:57:28 -0600 | [diff] [blame] | 294 | if ((words == 1) && last_bytes) { |
| 295 | local = 0; |
| 296 | memcpy(&local, dptr, last_bytes); |
Thierry Reding | 8e67c5d | 2015-07-22 15:33:22 -0600 | [diff] [blame] | 297 | } else if ((unsigned long)dptr & 3) { |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 298 | memcpy(&local, dptr, sizeof(u32)); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 299 | } else { |
Stephen Warren | 981b14f | 2014-06-25 10:57:28 -0600 | [diff] [blame] | 300 | local = *wptr; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 301 | } |
Stephen Warren | 981b14f | 2014-06-25 10:57:28 -0600 | [diff] [blame] | 302 | writel(local, &control->tx_fifo); |
| 303 | debug("pkt data sent (0x%x)\n", local); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 304 | if (!wait_for_tx_fifo_empty(control)) { |
| 305 | error = -1; |
| 306 | goto exit; |
| 307 | } |
| 308 | } else { |
| 309 | if (!wait_for_rx_fifo_notempty(control)) { |
| 310 | error = -1; |
| 311 | goto exit; |
| 312 | } |
| 313 | /* |
| 314 | * for the last word, we read into our local buffer, |
| 315 | * in case that caller did not provide enough buffer. |
| 316 | */ |
| 317 | local = readl(&control->rx_fifo); |
| 318 | if ((words == 1) && last_bytes) |
| 319 | memcpy(dptr, (char *)&local, last_bytes); |
Thierry Reding | 8e67c5d | 2015-07-22 15:33:22 -0600 | [diff] [blame] | 320 | else if ((unsigned long)dptr & 3) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 321 | memcpy(dptr, &local, sizeof(u32)); |
| 322 | else |
| 323 | *wptr = local; |
| 324 | debug("pkt data received (0x%x)\n", local); |
| 325 | } |
| 326 | words--; |
| 327 | dptr += sizeof(u32); |
| 328 | } |
| 329 | |
| 330 | if (wait_for_transfer_complete(control)) { |
| 331 | error = -1; |
| 332 | goto exit; |
| 333 | } |
| 334 | return 0; |
| 335 | exit: |
| 336 | /* error, reset the controller. */ |
| 337 | i2c_reset_controller(i2c_bus); |
| 338 | |
| 339 | return error; |
| 340 | } |
| 341 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 342 | static int tegra_i2c_write_data(struct i2c_bus *i2c_bus, u32 addr, u8 *data, |
Stephen Warren | 68049a0 | 2014-06-25 10:57:27 -0600 | [diff] [blame] | 343 | u32 len, bool end_with_repeated_start) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 344 | { |
| 345 | int error; |
| 346 | struct i2c_trans_info trans_info; |
| 347 | |
| 348 | trans_info.address = addr; |
| 349 | trans_info.buf = data; |
| 350 | trans_info.flags = I2C_IS_WRITE; |
Stephen Warren | 68049a0 | 2014-06-25 10:57:27 -0600 | [diff] [blame] | 351 | if (end_with_repeated_start) |
| 352 | trans_info.flags |= I2C_USE_REPEATED_START; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 353 | trans_info.num_bytes = len; |
| 354 | trans_info.is_10bit_address = 0; |
| 355 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 356 | error = send_recv_packets(i2c_bus, &trans_info); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 357 | if (error) |
Tom Warren | 29f3e3f | 2012-09-04 17:00:24 -0700 | [diff] [blame] | 358 | debug("tegra_i2c_write_data: Error (%d) !!!\n", error); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 359 | |
| 360 | return error; |
| 361 | } |
| 362 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 363 | static int tegra_i2c_read_data(struct i2c_bus *i2c_bus, u32 addr, u8 *data, |
Simon Glass | d84eb85 | 2012-10-30 07:28:52 +0000 | [diff] [blame] | 364 | u32 len) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 365 | { |
| 366 | int error; |
| 367 | struct i2c_trans_info trans_info; |
| 368 | |
| 369 | trans_info.address = addr | 1; |
| 370 | trans_info.buf = data; |
| 371 | trans_info.flags = 0; |
| 372 | trans_info.num_bytes = len; |
| 373 | trans_info.is_10bit_address = 0; |
| 374 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 375 | error = send_recv_packets(i2c_bus, &trans_info); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 376 | if (error) |
Tom Warren | 29f3e3f | 2012-09-04 17:00:24 -0700 | [diff] [blame] | 377 | debug("tegra_i2c_read_data: Error (%d) !!!\n", error); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 378 | |
| 379 | return error; |
| 380 | } |
| 381 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 382 | static int tegra_i2c_set_bus_speed(struct udevice *dev, unsigned int speed) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 383 | { |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 384 | struct i2c_bus *i2c_bus = dev_get_priv(dev); |
Simon Glass | d84eb85 | 2012-10-30 07:28:52 +0000 | [diff] [blame] | 385 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 386 | i2c_bus->speed = speed; |
| 387 | i2c_init_controller(i2c_bus); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 392 | static int tegra_i2c_probe(struct udevice *dev) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 393 | { |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 394 | struct i2c_bus *i2c_bus = dev_get_priv(dev); |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 395 | #ifdef CONFIG_TEGRA186 |
| 396 | int ret; |
| 397 | #else |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 398 | const void *blob = gd->fdt_blob; |
| 399 | int node = dev->of_offset; |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 400 | #endif |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 401 | bool is_dvc; |
| 402 | |
| 403 | i2c_bus->id = dev->seq; |
Simon Glass | 39de843 | 2015-03-25 12:21:55 -0600 | [diff] [blame] | 404 | i2c_bus->type = dev_get_driver_data(dev); |
Simon Glass | 4e9838c | 2015-08-11 08:33:29 -0600 | [diff] [blame] | 405 | i2c_bus->regs = (struct i2c_ctlr *)dev_get_addr(dev); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 406 | |
| 407 | /* |
| 408 | * We don't have a binding for pinmux yet. Leave it out for now. So |
| 409 | * far no one needs anything other than the default. |
| 410 | */ |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 411 | #ifdef CONFIG_TEGRA186 |
| 412 | ret = reset_get_by_name(dev, "i2c", &i2c_bus->reset_ctl); |
| 413 | if (ret) { |
| 414 | error("reset_get_by_name() failed: %d\n", ret); |
| 415 | return ret; |
| 416 | } |
Stephen Warren | b4ee081 | 2016-08-18 11:08:43 -0600 | [diff] [blame] | 417 | ret = clk_get_by_name(dev, "div-clk", &i2c_bus->clk); |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 418 | if (ret) { |
| 419 | error("clk_get_by_name() failed: %d\n", ret); |
| 420 | return ret; |
| 421 | } |
| 422 | #else |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 423 | i2c_bus->pinmux_config = FUNCMUX_DEFAULT; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 424 | i2c_bus->periph_id = clock_decode_periph_id(blob, node); |
| 425 | |
| 426 | /* |
| 427 | * We can't specify the pinmux config in the fdt, so I2C2 will not |
| 428 | * work on Seaboard. It normally has no devices on it anyway. |
| 429 | * You could add in this little hack if you need to use it. |
| 430 | * The correct solution is a pinmux binding in the fdt. |
| 431 | * |
| 432 | * if (i2c_bus->periph_id == PERIPH_ID_I2C2) |
| 433 | * i2c_bus->pinmux_config = FUNCMUX_I2C2_PTA; |
| 434 | */ |
| 435 | if (i2c_bus->periph_id == -1) |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 436 | return -EINVAL; |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 437 | #endif |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 438 | |
Simon Glass | 39de843 | 2015-03-25 12:21:55 -0600 | [diff] [blame] | 439 | is_dvc = dev_get_driver_data(dev) == TYPE_DVC; |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 440 | if (is_dvc) { |
| 441 | i2c_bus->control = |
| 442 | &((struct dvc_ctlr *)i2c_bus->regs)->control; |
| 443 | } else { |
| 444 | i2c_bus->control = &i2c_bus->regs->control; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 445 | } |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 446 | i2c_init_controller(i2c_bus); |
| 447 | debug("%s: controller bus %d at %p, periph_id %d, speed %d: ", |
| 448 | is_dvc ? "dvc" : "i2c", dev->seq, i2c_bus->regs, |
Bryan Wu | 3c27fa2 | 2016-08-05 16:10:35 -0600 | [diff] [blame] | 449 | #ifndef CONFIG_TEGRA186 |
| 450 | i2c_bus->periph_id, |
| 451 | #else |
| 452 | -1, |
| 453 | #endif |
| 454 | i2c_bus->speed); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 459 | /* i2c write version without the register address */ |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 460 | static int i2c_write_data(struct i2c_bus *i2c_bus, uchar chip, uchar *buffer, |
Jeroen Hofstee | 19d7bf3 | 2014-10-08 22:57:46 +0200 | [diff] [blame] | 461 | int len, bool end_with_repeated_start) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 462 | { |
| 463 | int rc; |
| 464 | |
| 465 | debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len); |
| 466 | debug("write_data: "); |
| 467 | /* use rc for counter */ |
| 468 | for (rc = 0; rc < len; ++rc) |
| 469 | debug(" 0x%02x", buffer[rc]); |
| 470 | debug("\n"); |
| 471 | |
| 472 | /* Shift 7-bit address over for lower-level i2c functions */ |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 473 | rc = tegra_i2c_write_data(i2c_bus, chip << 1, buffer, len, |
Stephen Warren | 68049a0 | 2014-06-25 10:57:27 -0600 | [diff] [blame] | 474 | end_with_repeated_start); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 475 | if (rc) |
| 476 | debug("i2c_write_data(): rc=%d\n", rc); |
| 477 | |
| 478 | return rc; |
| 479 | } |
| 480 | |
| 481 | /* i2c read version without the register address */ |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 482 | static int i2c_read_data(struct i2c_bus *i2c_bus, uchar chip, uchar *buffer, |
| 483 | int len) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 484 | { |
| 485 | int rc; |
| 486 | |
| 487 | debug("inside i2c_read_data():\n"); |
| 488 | /* Shift 7-bit address over for lower-level i2c functions */ |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 489 | rc = tegra_i2c_read_data(i2c_bus, chip << 1, buffer, len); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 490 | if (rc) { |
| 491 | debug("i2c_read_data(): rc=%d\n", rc); |
| 492 | return rc; |
| 493 | } |
| 494 | |
| 495 | debug("i2c_read_data: "); |
| 496 | /* reuse rc for counter*/ |
| 497 | for (rc = 0; rc < len; ++rc) |
| 498 | debug(" 0x%02x", buffer[rc]); |
| 499 | debug("\n"); |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | /* Probe to see if a chip is present. */ |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 505 | static int tegra_i2c_probe_chip(struct udevice *bus, uint chip_addr, |
| 506 | uint chip_flags) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 507 | { |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 508 | struct i2c_bus *i2c_bus = dev_get_priv(bus); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 509 | int rc; |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 510 | u8 reg; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 511 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 512 | /* Shift 7-bit address over for lower-level i2c functions */ |
| 513 | rc = tegra_i2c_write_data(i2c_bus, chip_addr << 1, ®, sizeof(reg), |
| 514 | false); |
| 515 | |
| 516 | return rc; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 519 | static int tegra_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 520 | int nmsgs) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 521 | { |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 522 | struct i2c_bus *i2c_bus = dev_get_priv(bus); |
| 523 | int ret; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 524 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 525 | debug("i2c_xfer: %d messages\n", nmsgs); |
| 526 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 527 | bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 528 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 529 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 530 | if (msg->flags & I2C_M_RD) { |
| 531 | ret = i2c_read_data(i2c_bus, msg->addr, msg->buf, |
| 532 | msg->len); |
| 533 | } else { |
| 534 | ret = i2c_write_data(i2c_bus, msg->addr, msg->buf, |
| 535 | msg->len, next_is_read); |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 536 | } |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 537 | if (ret) { |
| 538 | debug("i2c_write: error sending\n"); |
| 539 | return -EREMOTEIO; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 546 | int tegra_i2c_get_dvc_bus(struct udevice **busp) |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 547 | { |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 548 | struct udevice *bus; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 549 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 550 | for (uclass_first_device(UCLASS_I2C, &bus); |
| 551 | bus; |
| 552 | uclass_next_device(&bus)) { |
Simon Glass | 39de843 | 2015-03-25 12:21:55 -0600 | [diff] [blame] | 553 | if (dev_get_driver_data(bus) == TYPE_DVC) { |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 554 | *busp = bus; |
| 555 | return 0; |
Yen Lin | 96a78ac | 2012-03-06 19:00:23 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 559 | return -ENODEV; |
| 560 | } |
| 561 | |
| 562 | static const struct dm_i2c_ops tegra_i2c_ops = { |
| 563 | .xfer = tegra_i2c_xfer, |
| 564 | .probe_chip = tegra_i2c_probe_chip, |
| 565 | .set_bus_speed = tegra_i2c_set_bus_speed, |
| 566 | }; |
| 567 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 568 | static const struct udevice_id tegra_i2c_ids[] = { |
| 569 | { .compatible = "nvidia,tegra114-i2c", .data = TYPE_114 }, |
| 570 | { .compatible = "nvidia,tegra20-i2c", .data = TYPE_STD }, |
| 571 | { .compatible = "nvidia,tegra20-i2c-dvc", .data = TYPE_DVC }, |
| 572 | { } |
| 573 | }; |
Simon Glass | e31c1e5 | 2012-04-02 13:19:01 +0000 | [diff] [blame] | 574 | |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 575 | U_BOOT_DRIVER(i2c_tegra) = { |
| 576 | .name = "i2c_tegra", |
| 577 | .id = UCLASS_I2C, |
| 578 | .of_match = tegra_i2c_ids, |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 579 | .probe = tegra_i2c_probe, |
Simon Glass | b0e6ef4 | 2014-12-10 08:55:57 -0700 | [diff] [blame] | 580 | .priv_auto_alloc_size = sizeof(struct i2c_bus), |
| 581 | .ops = &tegra_i2c_ops, |
| 582 | }; |