Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Driver for the Zynq-7000 PS I2C controller |
| 4 | * IP from Cadence (ID T-CS-PE-0007-100, Version R1p10f2) |
| 5 | * |
| 6 | * Author: Joe Hershberger <joe.hershberger@ni.com> |
| 7 | * Copyright (c) 2012 Joe Hershberger. |
| 8 | * |
| 9 | * Copyright (c) 2012-2013 Xilinx, Michal Simek |
| 10 | * |
Simon Glass | cb052ff | 2016-11-23 06:34:44 -0700 | [diff] [blame] | 11 | * NOTE: This driver should be converted to driver model before June 2017. |
| 12 | * Please see doc/driver-model/i2c-howto.txt for instructions. |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <common.h> |
| 16 | #include <asm/io.h> |
| 17 | #include <i2c.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 19 | #include <asm/arch/hardware.h> |
| 20 | |
| 21 | /* i2c register set */ |
| 22 | struct zynq_i2c_registers { |
| 23 | u32 control; |
| 24 | u32 status; |
| 25 | u32 address; |
| 26 | u32 data; |
| 27 | u32 interrupt_status; |
| 28 | u32 transfer_size; |
| 29 | u32 slave_mon_pause; |
| 30 | u32 time_out; |
| 31 | u32 interrupt_mask; |
| 32 | u32 interrupt_enable; |
| 33 | u32 interrupt_disable; |
| 34 | }; |
| 35 | |
| 36 | /* Control register fields */ |
| 37 | #define ZYNQ_I2C_CONTROL_RW 0x00000001 |
| 38 | #define ZYNQ_I2C_CONTROL_MS 0x00000002 |
| 39 | #define ZYNQ_I2C_CONTROL_NEA 0x00000004 |
| 40 | #define ZYNQ_I2C_CONTROL_ACKEN 0x00000008 |
| 41 | #define ZYNQ_I2C_CONTROL_HOLD 0x00000010 |
| 42 | #define ZYNQ_I2C_CONTROL_SLVMON 0x00000020 |
| 43 | #define ZYNQ_I2C_CONTROL_CLR_FIFO 0x00000040 |
| 44 | #define ZYNQ_I2C_CONTROL_DIV_B_SHIFT 8 |
| 45 | #define ZYNQ_I2C_CONTROL_DIV_B_MASK 0x00003F00 |
| 46 | #define ZYNQ_I2C_CONTROL_DIV_A_SHIFT 14 |
| 47 | #define ZYNQ_I2C_CONTROL_DIV_A_MASK 0x0000C000 |
| 48 | |
| 49 | /* Status register values */ |
| 50 | #define ZYNQ_I2C_STATUS_RXDV 0x00000020 |
| 51 | #define ZYNQ_I2C_STATUS_TXDV 0x00000040 |
| 52 | #define ZYNQ_I2C_STATUS_RXOVF 0x00000080 |
| 53 | #define ZYNQ_I2C_STATUS_BA 0x00000100 |
| 54 | |
| 55 | /* Interrupt register fields */ |
| 56 | #define ZYNQ_I2C_INTERRUPT_COMP 0x00000001 |
| 57 | #define ZYNQ_I2C_INTERRUPT_DATA 0x00000002 |
| 58 | #define ZYNQ_I2C_INTERRUPT_NACK 0x00000004 |
| 59 | #define ZYNQ_I2C_INTERRUPT_TO 0x00000008 |
| 60 | #define ZYNQ_I2C_INTERRUPT_SLVRDY 0x00000010 |
| 61 | #define ZYNQ_I2C_INTERRUPT_RXOVF 0x00000020 |
| 62 | #define ZYNQ_I2C_INTERRUPT_TXOVF 0x00000040 |
| 63 | #define ZYNQ_I2C_INTERRUPT_RXUNF 0x00000080 |
| 64 | #define ZYNQ_I2C_INTERRUPT_ARBLOST 0x00000200 |
| 65 | |
| 66 | #define ZYNQ_I2C_FIFO_DEPTH 16 |
| 67 | #define ZYNQ_I2C_TRANSFERT_SIZE_MAX 255 /* Controller transfer limit */ |
| 68 | |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 69 | static struct zynq_i2c_registers *i2c_select(struct i2c_adapter *adap) |
| 70 | { |
| 71 | return adap->hwadapnr ? |
| 72 | /* Zynq PS I2C1 */ |
| 73 | (struct zynq_i2c_registers *)ZYNQ_I2C_BASEADDR1 : |
| 74 | /* Zynq PS I2C0 */ |
| 75 | (struct zynq_i2c_registers *)ZYNQ_I2C_BASEADDR0; |
| 76 | } |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 77 | |
| 78 | /* I2C init called by cmd_i2c when doing 'i2c reset'. */ |
Heiko Schocher | 465819a | 2013-11-08 07:30:53 +0100 | [diff] [blame] | 79 | static void zynq_i2c_init(struct i2c_adapter *adap, int requested_speed, |
| 80 | int slaveadd) |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 81 | { |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 82 | struct zynq_i2c_registers *zynq_i2c = i2c_select(adap); |
| 83 | |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 84 | /* 111MHz / ( (3 * 17) * 22 ) = ~100KHz */ |
| 85 | writel((16 << ZYNQ_I2C_CONTROL_DIV_B_SHIFT) | |
| 86 | (2 << ZYNQ_I2C_CONTROL_DIV_A_SHIFT), &zynq_i2c->control); |
| 87 | |
| 88 | /* Enable master mode, ack, and 7-bit addressing */ |
| 89 | setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_MS | |
| 90 | ZYNQ_I2C_CONTROL_ACKEN | ZYNQ_I2C_CONTROL_NEA); |
| 91 | } |
| 92 | |
| 93 | #ifdef DEBUG |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 94 | static void zynq_i2c_debug_status(struct zynq_i2c_registers *zynq_i2c) |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 95 | { |
| 96 | int int_status; |
| 97 | int status; |
| 98 | int_status = readl(&zynq_i2c->interrupt_status); |
| 99 | |
| 100 | status = readl(&zynq_i2c->status); |
| 101 | if (int_status || status) { |
| 102 | debug("Status: "); |
| 103 | if (int_status & ZYNQ_I2C_INTERRUPT_COMP) |
| 104 | debug("COMP "); |
| 105 | if (int_status & ZYNQ_I2C_INTERRUPT_DATA) |
| 106 | debug("DATA "); |
| 107 | if (int_status & ZYNQ_I2C_INTERRUPT_NACK) |
| 108 | debug("NACK "); |
| 109 | if (int_status & ZYNQ_I2C_INTERRUPT_TO) |
| 110 | debug("TO "); |
| 111 | if (int_status & ZYNQ_I2C_INTERRUPT_SLVRDY) |
| 112 | debug("SLVRDY "); |
| 113 | if (int_status & ZYNQ_I2C_INTERRUPT_RXOVF) |
| 114 | debug("RXOVF "); |
| 115 | if (int_status & ZYNQ_I2C_INTERRUPT_TXOVF) |
| 116 | debug("TXOVF "); |
| 117 | if (int_status & ZYNQ_I2C_INTERRUPT_RXUNF) |
| 118 | debug("RXUNF "); |
| 119 | if (int_status & ZYNQ_I2C_INTERRUPT_ARBLOST) |
| 120 | debug("ARBLOST "); |
| 121 | if (status & ZYNQ_I2C_STATUS_RXDV) |
| 122 | debug("RXDV "); |
| 123 | if (status & ZYNQ_I2C_STATUS_TXDV) |
| 124 | debug("TXDV "); |
| 125 | if (status & ZYNQ_I2C_STATUS_RXOVF) |
| 126 | debug("RXOVF "); |
| 127 | if (status & ZYNQ_I2C_STATUS_BA) |
| 128 | debug("BA "); |
| 129 | debug("TS%d ", readl(&zynq_i2c->transfer_size)); |
| 130 | debug("\n"); |
| 131 | } |
| 132 | } |
| 133 | #endif |
| 134 | |
| 135 | /* Wait for an interrupt */ |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 136 | static u32 zynq_i2c_wait(struct zynq_i2c_registers *zynq_i2c, u32 mask) |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 137 | { |
| 138 | int timeout, int_status; |
| 139 | |
| 140 | for (timeout = 0; timeout < 100; timeout++) { |
| 141 | udelay(100); |
| 142 | int_status = readl(&zynq_i2c->interrupt_status); |
| 143 | if (int_status & mask) |
| 144 | break; |
| 145 | } |
| 146 | #ifdef DEBUG |
Jesper B. Christensen | 6eaec4c | 2014-04-25 15:46:17 +0200 | [diff] [blame] | 147 | zynq_i2c_debug_status(zynq_i2c); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 148 | #endif |
| 149 | /* Clear interrupt status flags */ |
| 150 | writel(int_status & mask, &zynq_i2c->interrupt_status); |
| 151 | |
| 152 | return int_status & mask; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * I2C probe called by cmd_i2c when doing 'i2c probe'. |
| 157 | * Begin read, nak data byte, end. |
| 158 | */ |
Heiko Schocher | 465819a | 2013-11-08 07:30:53 +0100 | [diff] [blame] | 159 | static int zynq_i2c_probe(struct i2c_adapter *adap, u8 dev) |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 160 | { |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 161 | struct zynq_i2c_registers *zynq_i2c = i2c_select(adap); |
| 162 | |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 163 | /* Attempt to read a byte */ |
| 164 | setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO | |
| 165 | ZYNQ_I2C_CONTROL_RW); |
| 166 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD); |
| 167 | writel(0xFF, &zynq_i2c->interrupt_status); |
| 168 | writel(dev, &zynq_i2c->address); |
| 169 | writel(1, &zynq_i2c->transfer_size); |
| 170 | |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 171 | return (zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP | |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 172 | ZYNQ_I2C_INTERRUPT_NACK) & |
| 173 | ZYNQ_I2C_INTERRUPT_COMP) ? 0 : -ETIMEDOUT; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c |
| 178 | * Begin write, send address byte(s), begin read, receive data bytes, end. |
| 179 | */ |
Heiko Schocher | 465819a | 2013-11-08 07:30:53 +0100 | [diff] [blame] | 180 | static int zynq_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, |
| 181 | int alen, u8 *data, int length) |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 182 | { |
| 183 | u32 status; |
| 184 | u32 i = 0; |
| 185 | u8 *cur_data = data; |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 186 | struct zynq_i2c_registers *zynq_i2c = i2c_select(adap); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 187 | |
| 188 | /* Check the hardware can handle the requested bytes */ |
| 189 | if ((length < 0) || (length > ZYNQ_I2C_TRANSFERT_SIZE_MAX)) |
| 190 | return -EINVAL; |
| 191 | |
| 192 | /* Write the register address */ |
| 193 | setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO | |
| 194 | ZYNQ_I2C_CONTROL_HOLD); |
| 195 | /* |
| 196 | * Temporarily disable restart (by clearing hold) |
| 197 | * It doesn't seem to work. |
| 198 | */ |
Michael Burr | d7de189 | 2014-01-22 09:46:07 +0100 | [diff] [blame] | 199 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 200 | writel(0xFF, &zynq_i2c->interrupt_status); |
Michael Burr | d7de189 | 2014-01-22 09:46:07 +0100 | [diff] [blame] | 201 | if (alen) { |
| 202 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_RW); |
| 203 | writel(dev, &zynq_i2c->address); |
| 204 | while (alen--) |
| 205 | writel(addr >> (8 * alen), &zynq_i2c->data); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 206 | |
Michael Burr | d7de189 | 2014-01-22 09:46:07 +0100 | [diff] [blame] | 207 | /* Wait for the address to be sent */ |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 208 | if (!zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP)) { |
Michael Burr | d7de189 | 2014-01-22 09:46:07 +0100 | [diff] [blame] | 209 | /* Release the bus */ |
| 210 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD); |
| 211 | return -ETIMEDOUT; |
| 212 | } |
| 213 | debug("Device acked address\n"); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 214 | } |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 215 | |
| 216 | setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO | |
| 217 | ZYNQ_I2C_CONTROL_RW); |
| 218 | /* Start reading data */ |
| 219 | writel(dev, &zynq_i2c->address); |
| 220 | writel(length, &zynq_i2c->transfer_size); |
| 221 | |
| 222 | /* Wait for data */ |
| 223 | do { |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 224 | status = zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP | |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 225 | ZYNQ_I2C_INTERRUPT_DATA); |
| 226 | if (!status) { |
| 227 | /* Release the bus */ |
| 228 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD); |
| 229 | return -ETIMEDOUT; |
| 230 | } |
| 231 | debug("Read %d bytes\n", |
| 232 | length - readl(&zynq_i2c->transfer_size)); |
| 233 | for (; i < length - readl(&zynq_i2c->transfer_size); i++) |
| 234 | *(cur_data++) = readl(&zynq_i2c->data); |
| 235 | } while (readl(&zynq_i2c->transfer_size) != 0); |
| 236 | /* All done... release the bus */ |
| 237 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD); |
| 238 | |
| 239 | #ifdef DEBUG |
Jesper B. Christensen | 6eaec4c | 2014-04-25 15:46:17 +0200 | [diff] [blame] | 240 | zynq_i2c_debug_status(zynq_i2c); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 241 | #endif |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c |
| 247 | * Begin write, send address byte(s), send data bytes, end. |
| 248 | */ |
Heiko Schocher | 465819a | 2013-11-08 07:30:53 +0100 | [diff] [blame] | 249 | static int zynq_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, |
| 250 | int alen, u8 *data, int length) |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 251 | { |
| 252 | u8 *cur_data = data; |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 253 | struct zynq_i2c_registers *zynq_i2c = i2c_select(adap); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 254 | |
| 255 | /* Write the register address */ |
| 256 | setbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_CLR_FIFO | |
| 257 | ZYNQ_I2C_CONTROL_HOLD); |
| 258 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_RW); |
| 259 | writel(0xFF, &zynq_i2c->interrupt_status); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 260 | writel(dev, &zynq_i2c->address); |
Michael Burr | d7de189 | 2014-01-22 09:46:07 +0100 | [diff] [blame] | 261 | if (alen) { |
| 262 | while (alen--) |
| 263 | writel(addr >> (8 * alen), &zynq_i2c->data); |
| 264 | /* Start the tranfer */ |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 265 | if (!zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP)) { |
Michael Burr | d7de189 | 2014-01-22 09:46:07 +0100 | [diff] [blame] | 266 | /* Release the bus */ |
| 267 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD); |
| 268 | return -ETIMEDOUT; |
| 269 | } |
| 270 | debug("Device acked address\n"); |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 271 | } |
| 272 | |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 273 | while (length--) { |
| 274 | writel(*(cur_data++), &zynq_i2c->data); |
| 275 | if (readl(&zynq_i2c->transfer_size) == ZYNQ_I2C_FIFO_DEPTH) { |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 276 | if (!zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP)) { |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 277 | /* Release the bus */ |
| 278 | clrbits_le32(&zynq_i2c->control, |
| 279 | ZYNQ_I2C_CONTROL_HOLD); |
| 280 | return -ETIMEDOUT; |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* All done... release the bus */ |
| 286 | clrbits_le32(&zynq_i2c->control, ZYNQ_I2C_CONTROL_HOLD); |
| 287 | /* Wait for the address and data to be sent */ |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 288 | if (!zynq_i2c_wait(zynq_i2c, ZYNQ_I2C_INTERRUPT_COMP)) |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 289 | return -ETIMEDOUT; |
| 290 | return 0; |
| 291 | } |
| 292 | |
Heiko Schocher | 465819a | 2013-11-08 07:30:53 +0100 | [diff] [blame] | 293 | static unsigned int zynq_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 294 | unsigned int speed) |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 295 | { |
Heiko Schocher | 465819a | 2013-11-08 07:30:53 +0100 | [diff] [blame] | 296 | if (speed != 1000000) |
| 297 | return -EINVAL; |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 298 | |
Michal Simek | beedbcf | 2013-04-22 15:21:33 +0200 | [diff] [blame] | 299 | return 0; |
| 300 | } |
Heiko Schocher | 465819a | 2013-11-08 07:30:53 +0100 | [diff] [blame] | 301 | |
Michal Simek | ccc63f5 | 2015-10-27 16:02:36 +0100 | [diff] [blame] | 302 | #ifdef CONFIG_ZYNQ_I2C0 |
Heiko Schocher | 465819a | 2013-11-08 07:30:53 +0100 | [diff] [blame] | 303 | U_BOOT_I2C_ADAP_COMPLETE(zynq_0, zynq_i2c_init, zynq_i2c_probe, zynq_i2c_read, |
| 304 | zynq_i2c_write, zynq_i2c_set_bus_speed, |
| 305 | CONFIG_SYS_I2C_ZYNQ_SPEED, CONFIG_SYS_I2C_ZYNQ_SLAVE, |
| 306 | 0) |
Michal Simek | ccc63f5 | 2015-10-27 16:02:36 +0100 | [diff] [blame] | 307 | #endif |
| 308 | #ifdef CONFIG_ZYNQ_I2C1 |
Michael Burr | 91fae47 | 2014-01-22 09:46:08 +0100 | [diff] [blame] | 309 | U_BOOT_I2C_ADAP_COMPLETE(zynq_1, zynq_i2c_init, zynq_i2c_probe, zynq_i2c_read, |
| 310 | zynq_i2c_write, zynq_i2c_set_bus_speed, |
| 311 | CONFIG_SYS_I2C_ZYNQ_SPEED, CONFIG_SYS_I2C_ZYNQ_SLAVE, |
| 312 | 1) |
Michal Simek | ccc63f5 | 2015-10-27 16:02:36 +0100 | [diff] [blame] | 313 | #endif |