Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 1 | /* |
| 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 Black | 6541b28 | 2014-03-26 21:43:53 -0700 | [diff] [blame] | 29 | static int tegra_i2c_send_recv(int bus, int read, |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 30 | uint32_t *headers, int header_words, |
| 31 | uint8_t *data, int data_len) |
| 32 | { |
Gabe Black | 6541b28 | 2014-03-26 21:43:53 -0700 | [diff] [blame] | 33 | struct tegra_i2c_bus_info *info = &tegra_i2c_info[bus]; |
| 34 | struct tegra_i2c_regs * const regs = info->base; |
| 35 | |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 36 | while (data_len) { |
| 37 | uint32_t status = read32(®s->fifo_status); |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 38 | 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 Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 42 | |
| 43 | while (header_words && tx_empty) { |
| 44 | write32(*headers++, ®s->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(®s->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, ®s->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(®s->packet_transfer_status); |
| 76 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 77 | if (transfer_status & I2C_PKT_STATUS_NOACK_ADDR) { |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 78 | printk(BIOS_ERR, |
| 79 | "%s: The address was not acknowledged.\n", |
| 80 | __func__); |
Gabe Black | 6541b28 | 2014-03-26 21:43:53 -0700 | [diff] [blame] | 81 | info->reset_func(info->reset_bit); |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 82 | return -1; |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 83 | } else if (transfer_status & I2C_PKT_STATUS_NOACK_DATA) { |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 84 | printk(BIOS_ERR, |
| 85 | "%s: The data was not acknowledged.\n", |
| 86 | __func__); |
Gabe Black | 6541b28 | 2014-03-26 21:43:53 -0700 | [diff] [blame] | 87 | info->reset_func(info->reset_bit); |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 88 | return -1; |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 89 | } else if (transfer_status & I2C_PKT_STATUS_ARB_LOST) { |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 90 | printk(BIOS_ERR, |
| 91 | "%s: Lost arbitration.\n", |
| 92 | __func__); |
Gabe Black | 6541b28 | 2014-03-26 21:43:53 -0700 | [diff] [blame] | 93 | info->reset_func(info->reset_bit); |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 94 | return -1; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int tegra_i2c_request(int bus, unsigned chip, int cont, int restart, |
| 102 | int read, void *data, int data_len) |
| 103 | { |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 104 | 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 Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 112 | 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 Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 116 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 117 | headers[1] = (data_len - 1) << IOHEADER_PAYLOADSIZE_SHIFT; |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 118 | |
| 119 | uint32_t slave_addr = (chip << 1) | (read ? 1 : 0); |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 120 | headers[2] = IOHEADER_I2C_REQ_ADDR_MODE_7BIT | |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 121 | (slave_addr << IOHEADER_I2C_REQ_SLAVE_ADDR_SHIFT); |
| 122 | if (read) |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 123 | headers[2] |= IOHEADER_I2C_REQ_READ; |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 124 | if (restart) |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 125 | headers[2] |= IOHEADER_I2C_REQ_REPEAT_START; |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 126 | if (cont) |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 127 | headers[2] |= IOHEADER_I2C_REQ_CONTINUE_XFER; |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 128 | |
Gabe Black | 6541b28 | 2014-03-26 21:43:53 -0700 | [diff] [blame] | 129 | return tegra_i2c_send_recv(bus, read, headers, ARRAY_SIZE(headers), |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 130 | data, data_len); |
| 131 | } |
| 132 | |
| 133 | static 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 Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 137 | (IOHEADER_PAYLOADSIZE_MASK + 1) >> IOHEADER_PAYLOADSIZE_SHIFT; |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 138 | 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 | |
| 160 | int 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 | |
| 166 | int 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 | |
| 172 | void i2c_init(unsigned bus) |
| 173 | { |
Gabe Black | 6541b28 | 2014-03-26 21:43:53 -0700 | [diff] [blame] | 174 | struct tegra_i2c_regs * const regs = tegra_i2c_info[bus].base; |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 175 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 176 | write32(I2C_CNFG_PACKET_MODE_EN, ®s->cnfg); |
Gabe Black | 14eb43b | 2013-10-07 01:57:42 -0700 | [diff] [blame] | 177 | } |