Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Atmel I2C driver. |
| 4 | * |
| 5 | * (C) Copyright 2016 Songjun Wu <songjun.wu@atmel.com> |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 10 | #include <asm/io.h> |
| 11 | #include <common.h> |
Wenyou Yang | 3529b64 | 2016-09-13 10:40:31 +0800 | [diff] [blame] | 12 | #include <clk.h> |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 13 | #include <dm.h> |
| 14 | #include <errno.h> |
| 15 | #include <fdtdec.h> |
| 16 | #include <i2c.h> |
| 17 | #include <linux/bitops.h> |
| 18 | #include <mach/clk.h> |
| 19 | |
| 20 | #include "at91_i2c.h" |
| 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | #define I2C_TIMEOUT_MS 100 |
| 25 | |
| 26 | static int at91_wait_for_xfer(struct at91_i2c_bus *bus, u32 status) |
| 27 | { |
| 28 | struct at91_i2c_regs *reg = bus->regs; |
| 29 | ulong start_time = get_timer(0); |
| 30 | u32 sr; |
| 31 | |
| 32 | bus->status = 0; |
| 33 | |
| 34 | do { |
| 35 | sr = readl(®->sr); |
| 36 | bus->status |= sr; |
| 37 | |
| 38 | if (sr & TWI_SR_NACK) |
| 39 | return -EREMOTEIO; |
| 40 | else if (sr & status) |
| 41 | return 0; |
| 42 | } while (get_timer(start_time) < I2C_TIMEOUT_MS); |
| 43 | |
| 44 | return -ETIMEDOUT; |
| 45 | } |
| 46 | |
| 47 | static int at91_i2c_xfer_msg(struct at91_i2c_bus *bus, struct i2c_msg *msg) |
| 48 | { |
| 49 | struct at91_i2c_regs *reg = bus->regs; |
| 50 | bool is_read = msg->flags & I2C_M_RD; |
| 51 | u32 i; |
| 52 | int ret = 0; |
| 53 | |
Eugen Hristev | fcbb39d | 2020-12-04 18:06:55 +0200 | [diff] [blame] | 54 | /* if there is no message to send/receive, just exit quietly */ |
| 55 | if (msg->len == 0) |
| 56 | return ret; |
| 57 | |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 58 | readl(®->sr); |
| 59 | if (is_read) { |
| 60 | writel(TWI_CR_START, ®->cr); |
| 61 | |
| 62 | for (i = 0; !ret && i < (msg->len - 1); i++) { |
| 63 | ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY); |
| 64 | msg->buf[i] = readl(®->rhr); |
| 65 | } |
| 66 | |
| 67 | if (ret) |
| 68 | goto error; |
| 69 | |
| 70 | writel(TWI_CR_STOP, ®->cr); |
| 71 | |
| 72 | ret = at91_wait_for_xfer(bus, TWI_SR_RXRDY); |
| 73 | if (ret) |
| 74 | goto error; |
| 75 | |
| 76 | msg->buf[i] = readl(®->rhr); |
| 77 | |
| 78 | } else { |
| 79 | writel(msg->buf[0], ®->thr); |
Alan Ott | 59596e5 | 2017-11-28 22:25:23 -0500 | [diff] [blame] | 80 | ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY); |
| 81 | |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 82 | for (i = 1; !ret && (i < msg->len); i++) { |
| 83 | writel(msg->buf[i], ®->thr); |
| 84 | ret = at91_wait_for_xfer(bus, TWI_SR_TXRDY); |
| 85 | } |
| 86 | |
| 87 | if (ret) |
| 88 | goto error; |
| 89 | |
| 90 | writel(TWI_CR_STOP, ®->cr); |
| 91 | } |
| 92 | |
| 93 | if (!ret) |
| 94 | ret = at91_wait_for_xfer(bus, TWI_SR_TXCOMP); |
| 95 | |
| 96 | if (ret) |
| 97 | goto error; |
| 98 | |
| 99 | if (bus->status & (TWI_SR_OVRE | TWI_SR_UNRE | TWI_SR_LOCK)) { |
| 100 | ret = -EIO; |
| 101 | goto error; |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | |
| 106 | error: |
| 107 | if (bus->status & TWI_SR_LOCK) |
| 108 | writel(TWI_CR_LOCKCLR, ®->cr); |
| 109 | |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | static int at91_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs) |
| 114 | { |
| 115 | struct at91_i2c_bus *bus = dev_get_priv(dev); |
| 116 | struct at91_i2c_regs *reg = bus->regs; |
| 117 | struct i2c_msg *m_start = msg; |
| 118 | bool is_read; |
| 119 | u32 int_addr_flag = 0; |
| 120 | int ret = 0; |
| 121 | |
| 122 | if (nmsgs == 2) { |
| 123 | int internal_address = 0; |
| 124 | int i; |
| 125 | |
| 126 | /* 1st msg is put into the internal address, start with 2nd */ |
| 127 | m_start = &msg[1]; |
| 128 | |
| 129 | /* the max length of internal address is 3 bytes */ |
| 130 | if (msg->len > 3) |
| 131 | return -EFAULT; |
| 132 | |
| 133 | for (i = 0; i < msg->len; ++i) { |
| 134 | const unsigned addr = msg->buf[msg->len - 1 - i]; |
| 135 | |
| 136 | internal_address |= addr << (8 * i); |
| 137 | int_addr_flag += TWI_MMR_IADRSZ_1; |
| 138 | } |
| 139 | |
| 140 | writel(internal_address, ®->iadr); |
| 141 | } |
| 142 | |
| 143 | is_read = m_start->flags & I2C_M_RD; |
| 144 | |
| 145 | writel((m_start->addr << 16) | int_addr_flag | |
| 146 | (is_read ? TWI_MMR_MREAD : 0), ®->mmr); |
| 147 | |
| 148 | ret = at91_i2c_xfer_msg(bus, m_start); |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Calculate symmetric clock as stated in datasheet: |
| 155 | * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset)) |
| 156 | */ |
| 157 | static void at91_calc_i2c_clock(struct udevice *dev, int i2c_clk) |
| 158 | { |
| 159 | struct at91_i2c_bus *bus = dev_get_priv(dev); |
| 160 | const struct at91_i2c_pdata *pdata = bus->pdata; |
| 161 | int offset = pdata->clk_offset; |
| 162 | int max_ckdiv = pdata->clk_max_div; |
| 163 | int ckdiv, cdiv, div; |
| 164 | unsigned long src_rate; |
| 165 | |
| 166 | src_rate = bus->bus_clk_rate; |
| 167 | |
| 168 | div = max(0, (int)DIV_ROUND_UP(src_rate, 2 * i2c_clk) - offset); |
| 169 | ckdiv = fls(div >> 8); |
| 170 | cdiv = div >> ckdiv; |
| 171 | |
| 172 | if (ckdiv > max_ckdiv) { |
| 173 | ckdiv = max_ckdiv; |
| 174 | cdiv = 255; |
| 175 | } |
| 176 | |
| 177 | bus->speed = DIV_ROUND_UP(src_rate, |
| 178 | (cdiv * (1 << ckdiv) + offset) * 2); |
| 179 | |
| 180 | bus->cwgr_val = (ckdiv << 16) | (cdiv << 8) | cdiv; |
| 181 | } |
| 182 | |
| 183 | static int at91_i2c_enable_clk(struct udevice *dev) |
| 184 | { |
| 185 | struct at91_i2c_bus *bus = dev_get_priv(dev); |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 186 | struct clk clk; |
| 187 | ulong clk_rate; |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 188 | int ret; |
| 189 | |
| 190 | ret = clk_get_by_index(dev, 0, &clk); |
| 191 | if (ret) |
| 192 | return -EINVAL; |
| 193 | |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 194 | ret = clk_enable(&clk); |
| 195 | if (ret) |
| 196 | return ret; |
| 197 | |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 198 | clk_rate = clk_get_rate(&clk); |
| 199 | if (!clk_rate) |
Wenyou Yang | 66d9e1f | 2016-09-27 11:00:32 +0800 | [diff] [blame] | 200 | return -EINVAL; |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 201 | |
| 202 | bus->bus_clk_rate = clk_rate; |
| 203 | |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 204 | return 0; |
| 205 | } |
| 206 | |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 207 | static int at91_i2c_set_bus_speed(struct udevice *dev, unsigned int speed) |
| 208 | { |
| 209 | struct at91_i2c_bus *bus = dev_get_priv(dev); |
| 210 | |
| 211 | at91_calc_i2c_clock(dev, speed); |
| 212 | |
| 213 | writel(bus->cwgr_val, &bus->regs->cwgr); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | int at91_i2c_get_bus_speed(struct udevice *dev) |
| 219 | { |
| 220 | struct at91_i2c_bus *bus = dev_get_priv(dev); |
| 221 | |
| 222 | return bus->speed; |
| 223 | } |
| 224 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 225 | static int at91_i2c_of_to_plat(struct udevice *dev) |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 226 | { |
| 227 | const void *blob = gd->fdt_blob; |
| 228 | struct at91_i2c_bus *bus = dev_get_priv(dev); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 229 | int node = dev_of_offset(dev); |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 230 | |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 231 | bus->regs = dev_read_addr_ptr(dev); |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 232 | bus->pdata = (struct at91_i2c_pdata *)dev_get_driver_data(dev); |
| 233 | bus->clock_frequency = fdtdec_get_int(blob, node, |
| 234 | "clock-frequency", 100000); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static const struct dm_i2c_ops at91_i2c_ops = { |
| 240 | .xfer = at91_i2c_xfer, |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 241 | .set_bus_speed = at91_i2c_set_bus_speed, |
| 242 | .get_bus_speed = at91_i2c_get_bus_speed, |
| 243 | }; |
| 244 | |
Wenyou.Yang@microchip.com | 37f1987 | 2017-07-31 09:56:27 +0800 | [diff] [blame] | 245 | static int at91_i2c_probe(struct udevice *dev) |
| 246 | { |
| 247 | struct at91_i2c_bus *bus = dev_get_priv(dev); |
| 248 | struct at91_i2c_regs *reg = bus->regs; |
| 249 | int ret; |
| 250 | |
| 251 | ret = at91_i2c_enable_clk(dev); |
| 252 | if (ret) |
| 253 | return ret; |
| 254 | |
| 255 | writel(TWI_CR_SWRST, ®->cr); |
| 256 | |
| 257 | at91_calc_i2c_clock(dev, bus->clock_frequency); |
| 258 | |
| 259 | writel(bus->cwgr_val, ®->cwgr); |
| 260 | writel(TWI_CR_MSEN, ®->cr); |
| 261 | writel(TWI_CR_SVDIS, ®->cr); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 266 | static const struct at91_i2c_pdata at91rm9200_config = { |
| 267 | .clk_max_div = 5, |
| 268 | .clk_offset = 3, |
| 269 | }; |
| 270 | |
| 271 | static const struct at91_i2c_pdata at91sam9261_config = { |
| 272 | .clk_max_div = 5, |
| 273 | .clk_offset = 4, |
| 274 | }; |
| 275 | |
| 276 | static const struct at91_i2c_pdata at91sam9260_config = { |
| 277 | .clk_max_div = 7, |
| 278 | .clk_offset = 4, |
| 279 | }; |
| 280 | |
| 281 | static const struct at91_i2c_pdata at91sam9g20_config = { |
| 282 | .clk_max_div = 7, |
| 283 | .clk_offset = 4, |
| 284 | }; |
| 285 | |
| 286 | static const struct at91_i2c_pdata at91sam9g10_config = { |
| 287 | .clk_max_div = 7, |
| 288 | .clk_offset = 4, |
| 289 | }; |
| 290 | |
| 291 | static const struct at91_i2c_pdata at91sam9x5_config = { |
| 292 | .clk_max_div = 7, |
| 293 | .clk_offset = 4, |
| 294 | }; |
| 295 | |
| 296 | static const struct at91_i2c_pdata sama5d4_config = { |
| 297 | .clk_max_div = 7, |
| 298 | .clk_offset = 4, |
| 299 | }; |
| 300 | |
| 301 | static const struct at91_i2c_pdata sama5d2_config = { |
| 302 | .clk_max_div = 7, |
| 303 | .clk_offset = 3, |
| 304 | }; |
| 305 | |
Eugen Hristev | fb80575 | 2022-01-04 18:21:02 +0200 | [diff] [blame] | 306 | static const struct at91_i2c_pdata sam9x60_config = { |
| 307 | .clk_max_div = 7, |
| 308 | .clk_offset = 3, |
| 309 | }; |
| 310 | |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 311 | static const struct udevice_id at91_i2c_ids[] = { |
| 312 | { .compatible = "atmel,at91rm9200-i2c", .data = (long)&at91rm9200_config }, |
| 313 | { .compatible = "atmel,at91sam9260-i2c", .data = (long)&at91sam9260_config }, |
| 314 | { .compatible = "atmel,at91sam9261-i2c", .data = (long)&at91sam9261_config }, |
| 315 | { .compatible = "atmel,at91sam9g20-i2c", .data = (long)&at91sam9g20_config }, |
| 316 | { .compatible = "atmel,at91sam9g10-i2c", .data = (long)&at91sam9g10_config }, |
| 317 | { .compatible = "atmel,at91sam9x5-i2c", .data = (long)&at91sam9x5_config }, |
| 318 | { .compatible = "atmel,sama5d4-i2c", .data = (long)&sama5d4_config }, |
| 319 | { .compatible = "atmel,sama5d2-i2c", .data = (long)&sama5d2_config }, |
Eugen Hristev | fb80575 | 2022-01-04 18:21:02 +0200 | [diff] [blame] | 320 | { .compatible = "microchip,sam9x60-i2c", .data = (long)&sam9x60_config }, |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 321 | { } |
| 322 | }; |
| 323 | |
| 324 | U_BOOT_DRIVER(i2c_at91) = { |
| 325 | .name = "i2c_at91", |
| 326 | .id = UCLASS_I2C, |
| 327 | .of_match = at91_i2c_ids, |
Wenyou.Yang@microchip.com | 37f1987 | 2017-07-31 09:56:27 +0800 | [diff] [blame] | 328 | .probe = at91_i2c_probe, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 329 | .of_to_plat = at91_i2c_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 330 | .per_child_auto = sizeof(struct dm_i2c_chip), |
| 331 | .priv_auto = sizeof(struct at91_i2c_bus), |
Songjun Wu | 26d8828 | 2016-06-20 13:22:38 +0800 | [diff] [blame] | 332 | .ops = &at91_i2c_ops, |
| 333 | }; |