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