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 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/arch/clock.h>
30 #include <asm/arch/funcmux.h>
31 #include <asm/arch/gpio.h>
32 #include <asm/arch/pinmux.h>
33 #include <asm/arch-tegra/clk_rst.h>
34 #include <asm/arch-tegra/tegra_i2c.h>
36 DECLARE_GLOBAL_DATA_PTR;
38 static unsigned int i2c_bus_num;
40 /* Information about i2c controller */
43 enum periph_id periph_id;
46 struct i2c_control *control;
47 struct i2c_ctlr *regs;
48 int is_dvc; /* DVC type, rather than I2C */
49 int is_scs; /* single clock source (T114+) */
50 int inited; /* bus is inited */
53 static struct i2c_bus i2c_controllers[TEGRA_I2C_NUM_CONTROLLERS];
55 static void set_packet_mode(struct i2c_bus *i2c_bus)
59 config = I2C_CNFG_NEW_MASTER_FSM_MASK | I2C_CNFG_PACKET_MODE_MASK;
61 if (i2c_bus->is_dvc) {
62 struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
64 writel(config, &dvc->cnfg);
66 writel(config, &i2c_bus->regs->cnfg);
68 * program I2C_SL_CNFG.NEWSL to ENABLE. This fixes probe
69 * issues, i.e., some slaves may be wrongly detected.
71 setbits_le32(&i2c_bus->regs->sl_cnfg, I2C_SL_CNFG_NEWSL_MASK);
75 static void i2c_reset_controller(struct i2c_bus *i2c_bus)
77 /* Reset I2C controller. */
78 reset_periph(i2c_bus->periph_id, 1);
80 /* re-program config register to packet mode */
81 set_packet_mode(i2c_bus);
84 static void i2c_init_controller(struct i2c_bus *i2c_bus)
87 * Use PLLP - DP-04508-001_v06 datasheet indicates a divisor of 8
88 * here, in section 23.3.1, but in fact we seem to need a factor of
89 * 16 to get the right frequency.
91 clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
92 i2c_bus->speed * 2 * 8);
94 if (i2c_bus->is_scs) {
96 * T114 I2C went to a single clock source for standard/fast and
97 * HS clock speeds. The new clock rate setting calculation is:
98 * SCL = CLK_SOURCE.I2C /
99 * (CLK_MULT_STD_FAST_MODE * (I2C_CLK_DIV_STD_FAST_MODE+1) *
100 * I2C FREQUENCY DIVISOR) as per the T114 TRM (sec 30.3.1).
102 * NOTE: We do this here, after the initial clock/pll start,
103 * because if we read the clk_div reg before the controller
104 * is running, we hang, and we need it for the new calc.
106 int clk_div_stdfst_mode = readl(&i2c_bus->regs->clk_div) >> 16;
107 debug("%s: CLK_DIV_STD_FAST_MODE setting = %d\n", __func__,
108 clk_div_stdfst_mode);
110 clock_start_periph_pll(i2c_bus->periph_id, CLOCK_ID_PERIPH,
111 CLK_MULT_STD_FAST_MODE * (clk_div_stdfst_mode + 1) *
115 /* Reset I2C controller. */
116 i2c_reset_controller(i2c_bus);
118 /* Configure I2C controller. */
119 if (i2c_bus->is_dvc) { /* only for DVC I2C */
120 struct dvc_ctlr *dvc = (struct dvc_ctlr *)i2c_bus->regs;
122 setbits_le32(&dvc->ctrl3, DVC_CTRL_REG3_I2C_HW_SW_PROG_MASK);
125 funcmux_select(i2c_bus->periph_id, i2c_bus->pinmux_config);
128 static void send_packet_headers(
129 struct i2c_bus *i2c_bus,
130 struct i2c_trans_info *trans,
135 /* prepare header1: Header size = 0 Protocol = I2C, pktType = 0 */
136 data = PROTOCOL_TYPE_I2C << PKT_HDR1_PROTOCOL_SHIFT;
137 data |= packet_id << PKT_HDR1_PKT_ID_SHIFT;
138 data |= i2c_bus->id << PKT_HDR1_CTLR_ID_SHIFT;
139 writel(data, &i2c_bus->control->tx_fifo);
140 debug("pkt header 1 sent (0x%x)\n", data);
142 /* prepare header2 */
143 data = (trans->num_bytes - 1) << PKT_HDR2_PAYLOAD_SIZE_SHIFT;
144 writel(data, &i2c_bus->control->tx_fifo);
145 debug("pkt header 2 sent (0x%x)\n", data);
147 /* prepare IO specific header: configure the slave address */
148 data = trans->address << PKT_HDR3_SLAVE_ADDR_SHIFT;
150 /* Enable Read if it is not a write transaction */
151 if (!(trans->flags & I2C_IS_WRITE))
152 data |= PKT_HDR3_READ_MODE_MASK;
154 /* Write I2C specific header */
155 writel(data, &i2c_bus->control->tx_fifo);
156 debug("pkt header 3 sent (0x%x)\n", data);
159 static int wait_for_tx_fifo_empty(struct i2c_control *control)
162 int timeout_us = I2C_TIMEOUT_USEC;
164 while (timeout_us >= 0) {
165 count = (readl(&control->fifo_status) & TX_FIFO_EMPTY_CNT_MASK)
166 >> TX_FIFO_EMPTY_CNT_SHIFT;
167 if (count == I2C_FIFO_DEPTH)
176 static int wait_for_rx_fifo_notempty(struct i2c_control *control)
179 int timeout_us = I2C_TIMEOUT_USEC;
181 while (timeout_us >= 0) {
182 count = (readl(&control->fifo_status) & TX_FIFO_FULL_CNT_MASK)
183 >> TX_FIFO_FULL_CNT_SHIFT;
193 static int wait_for_transfer_complete(struct i2c_control *control)
196 int timeout_us = I2C_TIMEOUT_USEC;
198 while (timeout_us >= 0) {
199 int_status = readl(&control->int_status);
200 if (int_status & I2C_INT_NO_ACK_MASK)
202 if (int_status & I2C_INT_ARBITRATION_LOST_MASK)
204 if (int_status & I2C_INT_XFER_COMPLETE_MASK)
214 static int send_recv_packets(struct i2c_bus *i2c_bus,
215 struct i2c_trans_info *trans)
217 struct i2c_control *control = i2c_bus->control;
224 int is_write = trans->flags & I2C_IS_WRITE;
226 /* clear status from previous transaction, XFER_COMPLETE, NOACK, etc. */
227 int_status = readl(&control->int_status);
228 writel(int_status, &control->int_status);
230 send_packet_headers(i2c_bus, trans, 1);
232 words = DIV_ROUND_UP(trans->num_bytes, 4);
233 last_bytes = trans->num_bytes & 3;
237 u32 *wptr = (u32 *)dptr;
240 /* deal with word alignment */
241 if ((unsigned)dptr & 3) {
242 memcpy(&local, dptr, sizeof(u32));
243 writel(local, &control->tx_fifo);
244 debug("pkt data sent (0x%x)\n", local);
246 writel(*wptr, &control->tx_fifo);
247 debug("pkt data sent (0x%x)\n", *wptr);
249 if (!wait_for_tx_fifo_empty(control)) {
254 if (!wait_for_rx_fifo_notempty(control)) {
259 * for the last word, we read into our local buffer,
260 * in case that caller did not provide enough buffer.
262 local = readl(&control->rx_fifo);
263 if ((words == 1) && last_bytes)
264 memcpy(dptr, (char *)&local, last_bytes);
265 else if ((unsigned)dptr & 3)
266 memcpy(dptr, &local, sizeof(u32));
269 debug("pkt data received (0x%x)\n", local);
275 if (wait_for_transfer_complete(control)) {
281 /* error, reset the controller. */
282 i2c_reset_controller(i2c_bus);
287 static int tegra_i2c_write_data(struct i2c_bus *bus, u32 addr, u8 *data,
291 struct i2c_trans_info trans_info;
293 trans_info.address = addr;
294 trans_info.buf = data;
295 trans_info.flags = I2C_IS_WRITE;
296 trans_info.num_bytes = len;
297 trans_info.is_10bit_address = 0;
299 error = send_recv_packets(bus, &trans_info);
301 debug("tegra_i2c_write_data: Error (%d) !!!\n", error);
306 static int tegra_i2c_read_data(struct i2c_bus *bus, u32 addr, u8 *data,
310 struct i2c_trans_info trans_info;
312 trans_info.address = addr | 1;
313 trans_info.buf = data;
314 trans_info.flags = 0;
315 trans_info.num_bytes = len;
316 trans_info.is_10bit_address = 0;
318 error = send_recv_packets(bus, &trans_info);
320 debug("tegra_i2c_read_data: Error (%d) !!!\n", error);
325 #ifndef CONFIG_OF_CONTROL
326 #error "Please enable device tree support to use this driver"
330 * Check that a bus number is valid and return a pointer to it
332 * @param bus_num Bus number to check / return
333 * @return pointer to bus, if valid, else NULL
335 static struct i2c_bus *tegra_i2c_get_bus(unsigned int bus_num)
339 if (bus_num >= TEGRA_I2C_NUM_CONTROLLERS) {
340 debug("%s: Invalid bus number %u\n", __func__, bus_num);
343 bus = &i2c_controllers[bus_num];
345 debug("%s: Bus %u not available\n", __func__, bus_num);
352 unsigned int i2c_get_bus_speed(void)
356 bus = tegra_i2c_get_bus(i2c_bus_num);
362 int i2c_set_bus_speed(unsigned int speed)
366 bus = tegra_i2c_get_bus(i2c_bus_num);
370 i2c_init_controller(bus);
375 static int i2c_get_config(const void *blob, int node, struct i2c_bus *i2c_bus)
377 i2c_bus->regs = (struct i2c_ctlr *)fdtdec_get_addr(blob, node, "reg");
380 * We don't have a binding for pinmux yet. Leave it out for now. So
381 * far no one needs anything other than the default.
383 i2c_bus->pinmux_config = FUNCMUX_DEFAULT;
384 i2c_bus->speed = fdtdec_get_int(blob, node, "clock-frequency", 0);
385 i2c_bus->periph_id = clock_decode_periph_id(blob, node);
388 * We can't specify the pinmux config in the fdt, so I2C2 will not
389 * work on Seaboard. It normally has no devices on it anyway.
390 * You could add in this little hack if you need to use it.
391 * The correct solution is a pinmux binding in the fdt.
393 * if (i2c_bus->periph_id == PERIPH_ID_I2C2)
394 * i2c_bus->pinmux_config = FUNCMUX_I2C2_PTA;
396 if (i2c_bus->periph_id == -1)
397 return -FDT_ERR_NOTFOUND;
403 * Process a list of nodes, adding them to our list of I2C ports.
405 * @param blob fdt blob
406 * @param node_list list of nodes to process (any <=0 are ignored)
407 * @param count number of nodes to process
408 * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
409 * @param is_scs 1 if this HW uses a single clock source (T114+)
410 * @return 0 if ok, -1 on error
412 static int process_nodes(const void *blob, int node_list[], int count,
413 int is_dvc, int is_scs)
415 struct i2c_bus *i2c_bus;
418 /* build the i2c_controllers[] for each controller */
419 for (i = 0; i < count; i++) {
420 int node = node_list[i];
425 i2c_bus = &i2c_controllers[i];
428 if (i2c_get_config(blob, node, i2c_bus)) {
429 printf("i2c_init_board: failed to decode bus %d\n", i);
433 i2c_bus->is_scs = is_scs;
435 i2c_bus->is_dvc = is_dvc;
438 &((struct dvc_ctlr *)i2c_bus->regs)->control;
440 i2c_bus->control = &i2c_bus->regs->control;
442 debug("%s: controller bus %d at %p, periph_id %d, speed %d: ",
443 is_dvc ? "dvc" : "i2c", i, i2c_bus->regs,
444 i2c_bus->periph_id, i2c_bus->speed);
445 i2c_init_controller(i2c_bus);
449 /* Mark position as used */
456 /* Sadly there is no error return from this function */
457 void i2c_init_board(void)
459 int node_list[TEGRA_I2C_NUM_CONTROLLERS];
460 const void *blob = gd->fdt_blob;
463 /* First check for newer (T114+) I2C ports */
464 count = fdtdec_find_aliases_for_id(blob, "i2c",
465 COMPAT_NVIDIA_TEGRA114_I2C, node_list,
466 TEGRA_I2C_NUM_CONTROLLERS);
467 if (process_nodes(blob, node_list, count, 0, 1))
470 /* Now get the older (T20/T30) normal I2C ports */
471 count = fdtdec_find_aliases_for_id(blob, "i2c",
472 COMPAT_NVIDIA_TEGRA20_I2C, node_list,
473 TEGRA_I2C_NUM_CONTROLLERS);
474 if (process_nodes(blob, node_list, count, 0, 0))
477 /* Now look for dvc ports */
478 count = fdtdec_add_aliases_for_id(blob, "i2c",
479 COMPAT_NVIDIA_TEGRA20_DVC, node_list,
480 TEGRA_I2C_NUM_CONTROLLERS);
481 if (process_nodes(blob, node_list, count, 1, 0))
485 void i2c_init(int speed, int slaveaddr)
487 /* This will override the speed selected in the fdt for that port */
488 debug("i2c_init(speed=%u, slaveaddr=0x%x)\n", speed, slaveaddr);
489 i2c_set_bus_speed(speed);
492 /* i2c write version without the register address */
493 int i2c_write_data(struct i2c_bus *bus, uchar chip, uchar *buffer, int len)
497 debug("i2c_write_data: chip=0x%x, len=0x%x\n", chip, len);
498 debug("write_data: ");
499 /* use rc for counter */
500 for (rc = 0; rc < len; ++rc)
501 debug(" 0x%02x", buffer[rc]);
504 /* Shift 7-bit address over for lower-level i2c functions */
505 rc = tegra_i2c_write_data(bus, chip << 1, buffer, len);
507 debug("i2c_write_data(): rc=%d\n", rc);
512 /* i2c read version without the register address */
513 int i2c_read_data(struct i2c_bus *bus, uchar chip, uchar *buffer, int len)
517 debug("inside i2c_read_data():\n");
518 /* Shift 7-bit address over for lower-level i2c functions */
519 rc = tegra_i2c_read_data(bus, chip << 1, buffer, len);
521 debug("i2c_read_data(): rc=%d\n", rc);
525 debug("i2c_read_data: ");
526 /* reuse rc for counter*/
527 for (rc = 0; rc < len; ++rc)
528 debug(" 0x%02x", buffer[rc]);
534 /* Probe to see if a chip is present. */
535 int i2c_probe(uchar chip)
541 debug("i2c_probe: addr=0x%x\n", chip);
542 bus = tegra_i2c_get_bus(i2c_get_bus_num());
546 rc = i2c_write_data(bus, chip, ®, 1);
548 debug("Error probing 0x%x.\n", chip);
554 static int i2c_addr_ok(const uint addr, const int alen)
556 /* We support 7 or 10 bit addresses, so one or two bytes each */
557 return alen == 1 || alen == 2;
561 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
567 debug("i2c_read: chip=0x%x, addr=0x%x, len=0x%x\n",
569 bus = tegra_i2c_get_bus(i2c_bus_num);
572 if (!i2c_addr_ok(addr, alen)) {
573 debug("i2c_read: Bad address %x.%d.\n", addr, alen);
576 for (offset = 0; offset < len; offset++) {
579 for (i = 0; i < alen; i++) {
581 (addr + offset) >> (8 * i);
583 if (i2c_write_data(bus, chip, data, alen)) {
584 debug("i2c_read: error sending (0x%x)\n",
589 if (i2c_read_data(bus, chip, buffer + offset, 1)) {
590 debug("i2c_read: error reading (0x%x)\n", addr);
599 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
605 debug("i2c_write: chip=0x%x, addr=0x%x, len=0x%x\n",
607 bus = tegra_i2c_get_bus(i2c_bus_num);
610 if (!i2c_addr_ok(addr, alen)) {
611 debug("i2c_write: Bad address %x.%d.\n", addr, alen);
614 for (offset = 0; offset < len; offset++) {
615 uchar data[alen + 1];
616 for (i = 0; i < alen; i++)
617 data[alen - i - 1] = (addr + offset) >> (8 * i);
618 data[alen] = buffer[offset];
619 if (i2c_write_data(bus, chip, data, alen + 1)) {
620 debug("i2c_write: error sending (0x%x)\n", addr);
628 #if defined(CONFIG_I2C_MULTI_BUS)
630 * Functions for multiple I2C bus handling
632 unsigned int i2c_get_bus_num(void)
637 int i2c_set_bus_num(unsigned int bus)
639 if (bus >= TEGRA_I2C_NUM_CONTROLLERS || !i2c_controllers[bus].inited)
647 int tegra_i2c_get_dvc_bus_num(void)
651 for (i = 0; i < CONFIG_SYS_MAX_I2C_BUS; i++) {
652 struct i2c_bus *bus = &i2c_controllers[i];
654 if (bus->inited && bus->is_dvc)