blob: 8c8a3727989e9809760b4e25024467549af0683d [file] [log] [blame]
Gabe Black14eb43b2013-10-07 01:57:42 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2013 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <arch/io.h>
21#include <console/console.h>
22#include <device/i2c.h>
23#include <stdlib.h>
24#include <string.h>
25#include <soc/addressmap.h>
26
27#include "i2c.h"
28
Gabe Black6541b282014-03-26 21:43:53 -070029static int tegra_i2c_send_recv(int bus, int read,
Gabe Black14eb43b2013-10-07 01:57:42 -070030 uint32_t *headers, int header_words,
31 uint8_t *data, int data_len)
32{
Gabe Black6541b282014-03-26 21:43:53 -070033 struct tegra_i2c_bus_info *info = &tegra_i2c_info[bus];
34 struct tegra_i2c_regs * const regs = info->base;
35
Gabe Black14eb43b2013-10-07 01:57:42 -070036 while (data_len) {
37 uint32_t status = read32(&regs->fifo_status);
Gabe Blackd40be112013-10-09 23:45:07 -070038 int tx_empty = status & I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT_MASK;
39 tx_empty >>= I2C_FIFO_STATUS_TX_FIFO_EMPTY_CNT_SHIFT;
40 int rx_full = status & I2C_FIFO_STATUS_RX_FIFO_FULL_CNT_MASK;
41 rx_full >>= I2C_FIFO_STATUS_RX_FIFO_FULL_CNT_SHIFT;
Gabe Black14eb43b2013-10-07 01:57:42 -070042
43 while (header_words && tx_empty) {
44 write32(*headers++, &regs->tx_packet_fifo);
45 header_words--;
46 tx_empty--;
47 }
48
49 if (!header_words) {
50 if (read) {
51 while (data_len && rx_full) {
52 uint32_t word = read32(&regs->rx_fifo);
53 int todo = MIN(data_len, sizeof(word));
54
55 memcpy(data, &word, todo);
56 data_len -= todo;
57 data += sizeof(word);
58 rx_full--;
59 }
60 } else {
61 while (data_len && tx_empty) {
62 uint32_t word;
63 int todo = MIN(data_len, sizeof(word));
64
65 memcpy(&word, data, todo);
66 write32(word, &regs->tx_packet_fifo);
67 data_len -= todo;
68 data += sizeof(word);
69 tx_empty--;
70 }
71 }
72 }
73
74 uint32_t transfer_status =
75 read32(&regs->packet_transfer_status);
76
Gabe Blackd40be112013-10-09 23:45:07 -070077 if (transfer_status & I2C_PKT_STATUS_NOACK_ADDR) {
Gabe Black14eb43b2013-10-07 01:57:42 -070078 printk(BIOS_ERR,
79 "%s: The address was not acknowledged.\n",
80 __func__);
Gabe Black6541b282014-03-26 21:43:53 -070081 info->reset_func(info->reset_bit);
Gabe Black14eb43b2013-10-07 01:57:42 -070082 return -1;
Gabe Blackd40be112013-10-09 23:45:07 -070083 } else if (transfer_status & I2C_PKT_STATUS_NOACK_DATA) {
Gabe Black14eb43b2013-10-07 01:57:42 -070084 printk(BIOS_ERR,
85 "%s: The data was not acknowledged.\n",
86 __func__);
Gabe Black6541b282014-03-26 21:43:53 -070087 info->reset_func(info->reset_bit);
Gabe Black14eb43b2013-10-07 01:57:42 -070088 return -1;
Gabe Blackd40be112013-10-09 23:45:07 -070089 } else if (transfer_status & I2C_PKT_STATUS_ARB_LOST) {
Gabe Black14eb43b2013-10-07 01:57:42 -070090 printk(BIOS_ERR,
91 "%s: Lost arbitration.\n",
92 __func__);
Gabe Black6541b282014-03-26 21:43:53 -070093 info->reset_func(info->reset_bit);
Gabe Black14eb43b2013-10-07 01:57:42 -070094 return -1;
95 }
96 }
97
98 return 0;
99}
100
101static int tegra_i2c_request(int bus, unsigned chip, int cont, int restart,
102 int read, void *data, int data_len)
103{
Gabe Black14eb43b2013-10-07 01:57:42 -0700104 uint32_t headers[3];
105
106 if (restart && cont) {
107 printk(BIOS_ERR, "%s: Repeat start and continue xfer are "
108 "mutually exclusive.\n", __func__);
109 return -1;
110 }
111
Gabe Blackd40be112013-10-09 23:45:07 -0700112 headers[0] = (0 << IOHEADER_PROTHDRSZ_SHIFT) |
113 (1 << IOHEADER_PKTID_SHIFT) |
114 (bus << IOHEADER_CONTROLLER_ID_SHIFT) |
115 IOHEADER_PROTOCOL_I2C | IOHEADER_PKTTYPE_REQUEST;
Gabe Black14eb43b2013-10-07 01:57:42 -0700116
Gabe Blackd40be112013-10-09 23:45:07 -0700117 headers[1] = (data_len - 1) << IOHEADER_PAYLOADSIZE_SHIFT;
Gabe Black14eb43b2013-10-07 01:57:42 -0700118
119 uint32_t slave_addr = (chip << 1) | (read ? 1 : 0);
Gabe Blackd40be112013-10-09 23:45:07 -0700120 headers[2] = IOHEADER_I2C_REQ_ADDR_MODE_7BIT |
Gabe Black14eb43b2013-10-07 01:57:42 -0700121 (slave_addr << IOHEADER_I2C_REQ_SLAVE_ADDR_SHIFT);
122 if (read)
Gabe Blackd40be112013-10-09 23:45:07 -0700123 headers[2] |= IOHEADER_I2C_REQ_READ;
Gabe Black14eb43b2013-10-07 01:57:42 -0700124 if (restart)
Gabe Blackd40be112013-10-09 23:45:07 -0700125 headers[2] |= IOHEADER_I2C_REQ_REPEAT_START;
Gabe Black14eb43b2013-10-07 01:57:42 -0700126 if (cont)
Gabe Blackd40be112013-10-09 23:45:07 -0700127 headers[2] |= IOHEADER_I2C_REQ_CONTINUE_XFER;
Gabe Black14eb43b2013-10-07 01:57:42 -0700128
Gabe Black6541b282014-03-26 21:43:53 -0700129 return tegra_i2c_send_recv(bus, read, headers, ARRAY_SIZE(headers),
Gabe Black14eb43b2013-10-07 01:57:42 -0700130 data, data_len);
131}
132
133static int i2c_readwrite(unsigned bus, unsigned chip, unsigned addr,
134 unsigned alen, uint8_t *buf, unsigned len, int read)
135{
136 const uint32_t max_payload =
Gabe Blackd40be112013-10-09 23:45:07 -0700137 (IOHEADER_PAYLOADSIZE_MASK + 1) >> IOHEADER_PAYLOADSIZE_SHIFT;
Gabe Black14eb43b2013-10-07 01:57:42 -0700138 uint8_t abuf[sizeof(addr)];
139
140 int i;
141 for (i = 0; i < alen; i++)
142 abuf[i] = addr >> ((alen - i - 1) * 8);
143
144 if (tegra_i2c_request(bus, chip, !read, 0, 0, abuf, alen))
145 return -1;
146
147 while (len) {
148 int todo = MIN(len, max_payload);
149 int cont = (todo < len);
150 if (tegra_i2c_request(bus, chip, cont, 0, read, buf, todo)) {
151 // We should reset the controller here.
152 return -1;
153 }
154 len -= todo;
155 buf += todo;
156 }
157 return 0;
158}
159
160int i2c_read(unsigned bus, unsigned chip, unsigned addr,
161 unsigned alen, uint8_t *buf, unsigned len)
162{
163 return i2c_readwrite(bus, chip, addr, alen, buf, len, 1);
164}
165
166int i2c_write(unsigned bus, unsigned chip, unsigned addr,
167 unsigned alen, const uint8_t *buf, unsigned len)
168{
169 return i2c_readwrite(bus, chip, addr, alen, (void *)buf, len, 0);
170}
171
172void i2c_init(unsigned bus)
173{
Gabe Black6541b282014-03-26 21:43:53 -0700174 struct tegra_i2c_regs * const regs = tegra_i2c_info[bus].base;
Gabe Black14eb43b2013-10-07 01:57:42 -0700175
Gabe Blackd40be112013-10-09 23:45:07 -0700176 write32(I2C_CNFG_PACKET_MODE_EN, &regs->cnfg);
Gabe Black14eb43b2013-10-07 01:57:42 -0700177}