2 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3 * Copyright (c) 2010-2011 NVIDIA Corporation
4 * NVIDIA Corporation <www.nvidia.com>
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/arch/clock.h>
14 #include <asm/arch/funcmux.h>
15 #include <asm/arch/gpio.h>
16 #include <asm/arch/pinmux.h>
17 #include <asm/arch-tegra/clk_rst.h>
18 #include <asm/arch-tegra/tegra_i2c.h>
20 DECLARE_GLOBAL_DATA_PTR;
22 static unsigned int i2c_bus_num;
24 /* Information about i2c controller */
27 enum periph_id periph_id;
30 struct i2c_control *control;
31 struct i2c_ctlr *regs;
32 int is_dvc; /* DVC type, rather than I2C */
33 int is_scs; /* single clock source (T114+) */
34 int inited; /* bus is inited */
37 static struct i2c_bus i2c_controllers[TEGRA_I2C_NUM_CONTROLLERS];
39 static void set_packet_mode(struct i2c_bus *i2c_bus)
43 config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK;
45 if (i2c_bus->is_dvc) {
46 struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
48 writel(config, &dvc->cnfg);
50 writel(config, &i2c_bus->regs->cnfg);
52 * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe
53 * issues, i.e., some slaves may be wrongly detected.
55 setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK);
59 static void i2c_reset_controller(struct i2c_bus *i2c_bus)
61 /* Reset I2C controller. */
62 reset_periph(i2c_bus->periph_id, 1);
64 /* re-program config register to packet mode */
65 set_packet_mode(i2c_bus);
68 static void i2c_init_controller(struct i2c_bus *i2c_bus)
71 * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8
72 * here, in section 23.3.1, but in fact we seem to need a factor of
73 * 16 to get the right frequency.
75 clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
76 i2c_bus->speed * 2 * 8);
78 if (i2c_bus->is_scs) {
80 * T114 I2C went to a single clock source for standard/fast and
81 * HS clock speeds. The new clock rate setting calculation is:
82 * SCL = CLK_SOURCE.I2C /
83 * (CLK_MULT_STD_FAST_MODE * (I2C_CLK_DIV_STD_FAST_MODE+1) *
84 * I2C FREQUENCY DIVISOR) as per the T114 TRM (sec 30.3.1).
86 * NOTE: We do this here, after the initial clock/pll start,
87 * because if we read the clk_div reg before the controller
88 * is running, we hang, and we need it for the new calc.
90 int clk_div_stdfst_mode = readl(&i2c_bus->regs->clk_div) >> 16;
91 debug("%s: CLK_DIV_STD_FAST_MODE setting = %d\n", __func__,
94 clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
95 CLK_MULT_STD_FAST_MODE * (clk_div_stdfst_mode + 1) *
99 /* Reset I2C controller. */
100 i2c_reset_controller(i2c_bus);
102 /* Configure I2C controller. */
103 if (i2c_bus->is_dvc) { /* only for DVC I2C */
104 struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
106 setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK);
109 funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config);
112 static void send_packet_headers(
113 struct i2c_bus *i2c_bus,
114 struct i2c_trans_info *trans,
119 /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */
120 data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT;
121 data |= packet_id << PKT_HDR1_PKT_ID_SHIFT;
122 data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT;
123 writel(data, &i2c_bus->control->tx_fifo);
124 debug("pkt header 1 sent (0x%x)\n", data);
126 /* prepare header2 */
127 data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT;
128 writel(data, &i2c_bus->control->tx_fifo);
129 debug("pkt header 2 sent (0x%x)\n", data);
131 /* prepare IO specific header: configure the slave address */
132 data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT;
134 /* Enable Read if it is not a write transaction */
135 if (!(trans->flags & I2C_IS_WRITE))
136 data |= PKT_HDR3_READ_MODE_MASK;
138 /* Write I2C specific header */
139 writel(data, &i2c_bus->control->tx_fifo);
140 debug("pkt header 3 sent (0x%x)\n", data);
143 static int wait_for_tx_fifo_empty(struct i2c_control *control)
146 int timeout_us = I2C_TIMEOUT_USEC;
148 while (timeout_us >= 0) {
149 count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK)
150 >> TX_FIFO_EMPTY_CNT_SHIFT;
151 if (count == I2C_FIFO_DEPTH)
160 static int wait_for_rx_fifo_notempty(struct i2c_control *control)
163 int timeout_us = I2C_TIMEOUT_USEC;
165 while (timeout_us >= 0) {
166 count = (readl(&control->fifo_status) & TX_FIFO_FULL_CNT_MASK)
167 >> TX_FIFO_FULL_CNT_SHIFT;
177 static int wait_for_transfer_complete(struct i2c_control *control)
180 int timeout_us = I2C_TIMEOUT_USEC;
182 while (timeout_us >= 0) {
183 int_status = readl(&control->int_status);
184 if (int_status & I2C_INT_NO_ACK_MASK)
186 if (int_status & I2C_INT_ARBITRATION_LOST_MASK)
188 if (int_status & I2C_INT_XFER_COMPLETE_MASK)
198 static int send_recv_packets(struct i2c_bus *i2c_bus,
199 struct i2c_trans_info *trans)
201 struct i2c_control *control = i2c_bus->control;
208 int is_write = trans->flags & I2C_IS_WRITE;
210 /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */
211 int_status = readl(&control->int_status);
212 writel(int_status, &control->int_status);
214 send_packet_headers(i2c_bus, trans, 1);
216 words = DIV_ROUND_UP(trans->num_bytes, 4);
217 last_bytes = trans->num_bytes & 3;
221 u32 *wptr = (u32 *)dptr;
224 /* deal with word alignment */
225 if ((unsigned)dptr & 3) {
226 memcpy(&local, dptr, sizeof(u32));
227 writel(local, &control->tx_fifo);
228 debug("pkt data sent (0x%x)\n", local);
230 writel(*wptr, &control->tx_fifo);
231 debug("pkt data sent (0x%x)\n", *wptr);
233 if (!wait_for_tx_fifo_empty(control)) {
238 if (!wait_for_rx_fifo_notempty(control)) {
243 * for the last word, we read into our local buffer,
244 * in case that caller did not provide enough buffer.
246 local = readl(&control->rx_fifo);
247 if ((words == 1) && last_bytes)
248 memcpy(dptr, (char *)&local, last_bytes);
249 else if ((unsigned)dptr & 3)
250 memcpy(dptr, &local, sizeof(u32));
253 debug("pkt data received (0x%x)\n", local);
259 if (wait_for_transfer_complete(control)) {
265 /* error, reset the controller. */
266 i2c_reset_controller(i2c_bus);
271 static int tegra_i2c_write_data(u32 addr, u8 *data, u32 len)
274 struct i2c_trans_info trans_info;
276 trans_info.address = addr;
277 trans_info.buf = data;
278 trans_info.flags = I2C_IS_WRITE;
279 trans_info.num_bytes = len;
280 trans_info.is_10bit_address = 0;
282 error = send_recv_packets(&i2c_controllers[i2c_bus_num], &trans_info);
284 debug("tegra_i2c_write_data: Error (%d) !!!\n", error);
289 static int tegra_i2c_read_data(u32 addr, u8 *data, u32 len)
292 struct i2c_trans_info trans_info;
294 trans_info.address = addr | 1;
295 trans_info.buf = data;
296 trans_info.flags = 0;
297 trans_info.num_bytes = len;
298 trans_info.is_10bit_address = 0;
300 error = send_recv_packets(&i2c_controllers[i2c_bus_num], &trans_info);
302 debug("tegra_i2c_read_data: Error (%d) !!!\n", error);
307 #ifndef CONFIG_OF_CONTROL
308 #error "Please enable device tree support to use this driver"
311 unsigned int i2c_get_bus_speed(void)
313 return i2c_controllers[i2c_bus_num].speed;
316 int i2c_set_bus_speed(unsigned int speed)
318 struct i2c_bus *i2c_bus;
320 i2c_bus = &i2c_controllers[i2c_bus_num];
321 i2c_bus->speed = speed;
322 i2c_init_controller(i2c_bus);
327 static int i2c_get_config(const void *blob, int node, struct i2c_bus *i2c_bus)
329 i2c_bus->regs = (struct i2c_ctlr *)fdtdec_get_addr(blob, node, "reg");
332 * We don't have a binding for pinmux yet. Leave it out for now. So
333 * far no one needs anything other than the default.
335 i2c_bus->pinmux_config = FUNCMUX_DEFAULT;
336 i2c_bus->speed = fdtdec_get_int(blob, node, "clock-frequency", 0);
337 i2c_bus->periph_id = clock_decode_periph_id(blob, node);
340 * We can't specify the pinmux config in the fdt, so I2C2 will not
341 * work on Seaboard. It normally has no devices on it anyway.
342 * You could add in this little hack if you need to use it.
343 * The correct solution is a pinmux binding in the fdt.
345 * if (i2c_bus->periph_id == PERIPH_ID_I2C2)
346 * i2c_bus->pinmux_config = FUNCMUX_I2C2_PTA;
348 if (i2c_bus->periph_id == -1)
349 return -FDT_ERR_NOTFOUND;
355 * Process a list of nodes, adding them to our list of I2C ports.
357 * @param blob fdt blob
358 * @param node_list list of nodes to process (any <=0 are ignored)
359 * @param count number of nodes to process
360 * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
361 * @param is_scs 1 if this HW uses a single clock source (T114+)
362 * @return 0 if ok, -1 on error
364 static int process_nodes(const void *blob, int node_list[], int count,
365 int is_dvc, int is_scs)
367 struct i2c_bus *i2c_bus;
370 /* build the i2c_controllers[] for each controller */
371 for (i = 0; i < count; i++) {
372 int node = node_list[i];
377 i2c_bus = &i2c_controllers[i];
380 if (i2c_get_config(blob, node, i2c_bus)) {
381 printf("i2c_init_board: failed to decode bus %d\n", i);
385 i2c_bus->is_scs = is_scs;
387 i2c_bus->is_dvc = is_dvc;
390 &((struct dvc_ctlr *)i2c_bus->regs)->control;
392 i2c_bus->control = &i2c_bus->regs->control;
394 debug("%s: controller bus %d at %p, periph_id %d, speed %d: ",
395 is_dvc ? "dvc" : "i2c", i, i2c_bus->regs,
396 i2c_bus->periph_id, i2c_bus->speed);
397 i2c_init_controller(i2c_bus);
401 /* Mark position as used */
408 /* Sadly there is no error return from this function */
409 void i2c_init_board(void)
411 int node_list[TEGRA_I2C_NUM_CONTROLLERS];
412 const void *blob = gd->fdt_blob;
415 /* First check for newer (T114+) I2C ports */
416 count = fdtdec_find_aliases_for_id(blob, "i2c",
417 COMPAT_NVIDIA_TEGRA114_I2C, node_list,
418 TEGRA_I2C_NUM_CONTROLLERS);
419 if (process_nodes(blob, node_list, count, 0, 1))
422 /* Now get the older (T20/T30) normal I2C ports */
423 count = fdtdec_find_aliases_for_id(blob, "i2c",
424 COMPAT_NVIDIA_TEGRA20_I2C, node_list,
425 TEGRA_I2C_NUM_CONTROLLERS);
426 if (process_nodes(blob, node_list, count, 0, 0))
429 /* Now look for dvc ports */
430 count = fdtdec_add_aliases_for_id(blob, "i2c",
431 COMPAT_NVIDIA_TEGRA20_DVC, node_list,
432 TEGRA_I2C_NUM_CONTROLLERS);
433 if (process_nodes(blob, node_list, count, 1, 0))
437 void i2c_init(int speed, int slaveaddr)
439 /* This will override the speed selected in the fdt for that port */
440 debug("i2c_init(speed=%u, slaveaddr=0x%x)\n", speed, slaveaddr);
441 i2c_set_bus_speed(speed);
444 /* i2c write version without the register address */
445 int i2c_write_data(uchar chip, uchar *buffer, int len)
449 debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
450 debug("write_data: ");
451 /* use rc for counter */
452 for (rc = 0; rc < len; ++rc)
453 debug(" 0x%02x", buffer[rc]);
456 /* Shift 7-bit address over for lower-level i2c functions */
457 rc = tegra_i2c_write_data(chip << 1, buffer, len);
459 debug("i2c_write_data(): rc=%d\n", rc);
464 /* i2c read version without the register address */
465 int i2c_read_data(uchar chip, uchar *buffer, int len)
469 debug("inside i2c_read_data():\n");
470 /* Shift 7-bit address over for lower-level i2c functions */
471 rc = tegra_i2c_read_data(chip << 1, buffer, len);
473 debug("i2c_read_data(): rc=%d\n", rc);
477 debug("i2c_read_data: ");
478 /* reuse rc for counter*/
479 for (rc = 0; rc < len; ++rc)
480 debug(" 0x%02x", buffer[rc]);
486 /* Probe to see if a chip is present. */
487 int i2c_probe(uchar chip)
492 debug("i2c_probe: addr=0x%x\n", chip);
494 rc = i2c_write_data(chip, ®, 1);
496 debug("Error probing 0x%x.\n", chip);
502 static int i2c_addr_ok(const uint addr, const int alen)
504 /* We support 7 or 10 bit addresses, so one or two bytes each */
505 return alen == 1 || alen == 2;
509 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
514 debug("i2c_read: chip=0x%x, addr=0x%x, len=0x%x\n",
516 if (!i2c_addr_ok(addr, alen)) {
517 debug("i2c_read: Bad address %x.%d.\n", addr, alen);
520 for (offset = 0; offset < len; offset++) {
523 for (i = 0; i < alen; i++) {
525 (addr + offset) >> (8 * i);
527 if (i2c_write_data(chip, data, alen)) {
528 debug("i2c_read: error sending (0x%x)\n",
533 if (i2c_read_data(chip, buffer + offset, 1)) {
534 debug("i2c_read: error reading (0x%x)\n", addr);
543 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
548 debug("i2c_write: chip=0x%x, addr=0x%x, len=0x%x\n",
550 if (!i2c_addr_ok(addr, alen)) {
551 debug("i2c_write: Bad address %x.%d.\n", addr, alen);
554 for (offset = 0; offset < len; offset++) {
555 uchar data[alen + 1];
556 for (i = 0; i < alen; i++)
557 data[alen - i - 1] = (addr + offset) >> (8 * i);
558 data[alen] = buffer[offset];
559 if (i2c_write_data(chip, data, alen + 1)) {
560 debug("i2c_write: error sending (0x%x)\n", addr);
568 #if defined(CONFIG_I2C_MULTI_BUS)
570 * Functions for multiple I2C bus handling
572 unsigned int i2c_get_bus_num(void)
577 int i2c_set_bus_num(unsigned int bus)
579 if (bus >= TEGRA_I2C_NUM_CONTROLLERS || !i2c_controllers[bus].inited)
587 int tegra_i2c_get_dvc_bus_num(void)
591 for (i = 0; i < CONFIG_SYS_MAX_I2C_BUS; i++) {
592 struct i2c_bus *bus = &i2c_controllers[i];
594 if (bus->inited && bus->is_dvc)