Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 - Beniamino Galvani <b.galvani@gmail.com> |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 4 | */ |
| 5 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 6 | #include <log.h> |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 7 | #include <asm/io.h> |
Beniamino Galvani | 6c9c980 | 2018-06-14 13:43:40 +0200 | [diff] [blame] | 8 | #include <clk.h> |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 9 | #include <dm.h> |
| 10 | #include <i2c.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 11 | #include <linux/err.h> |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 12 | |
Beniamino Galvani | cf0a7a9 | 2017-11-26 17:40:55 +0100 | [diff] [blame] | 13 | #define I2C_TIMEOUT_MS 100 |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 14 | |
| 15 | /* Control register fields */ |
| 16 | #define REG_CTRL_START BIT(0) |
| 17 | #define REG_CTRL_ACK_IGNORE BIT(1) |
| 18 | #define REG_CTRL_STATUS BIT(2) |
| 19 | #define REG_CTRL_ERROR BIT(3) |
| 20 | #define REG_CTRL_CLKDIV_SHIFT 12 |
| 21 | #define REG_CTRL_CLKDIV_MASK GENMASK(21, 12) |
| 22 | #define REG_CTRL_CLKDIVEXT_SHIFT 28 |
| 23 | #define REG_CTRL_CLKDIVEXT_MASK GENMASK(29, 28) |
| 24 | |
| 25 | enum { |
| 26 | TOKEN_END = 0, |
| 27 | TOKEN_START, |
| 28 | TOKEN_SLAVE_ADDR_WRITE, |
| 29 | TOKEN_SLAVE_ADDR_READ, |
| 30 | TOKEN_DATA, |
| 31 | TOKEN_DATA_LAST, |
| 32 | TOKEN_STOP, |
| 33 | }; |
| 34 | |
| 35 | struct i2c_regs { |
| 36 | u32 ctrl; |
| 37 | u32 slave_addr; |
| 38 | u32 tok_list0; |
| 39 | u32 tok_list1; |
| 40 | u32 tok_wdata0; |
| 41 | u32 tok_wdata1; |
| 42 | u32 tok_rdata0; |
| 43 | u32 tok_rdata1; |
| 44 | }; |
| 45 | |
Guillaume La Roque | b83dac6 | 2019-03-22 14:49:32 +0100 | [diff] [blame] | 46 | struct meson_i2c_data { |
| 47 | unsigned char div_factor; |
| 48 | }; |
| 49 | |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 50 | struct meson_i2c { |
Guillaume La Roque | b83dac6 | 2019-03-22 14:49:32 +0100 | [diff] [blame] | 51 | const struct meson_i2c_data *data; |
Beniamino Galvani | 6c9c980 | 2018-06-14 13:43:40 +0200 | [diff] [blame] | 52 | struct clk clk; |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 53 | struct i2c_regs *regs; |
Beniamino Galvani | 9ba506d | 2017-11-26 17:40:57 +0100 | [diff] [blame] | 54 | struct i2c_msg *msg; /* Current I2C message */ |
| 55 | bool last; /* Whether the message is the last */ |
| 56 | uint count; /* Number of bytes in the current transfer */ |
| 57 | uint pos; /* Position of current transfer in message */ |
| 58 | u32 tokens[2]; /* Sequence of tokens to be written */ |
| 59 | uint num_tokens; /* Number of tokens to be written */ |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | static void meson_i2c_reset_tokens(struct meson_i2c *i2c) |
| 63 | { |
| 64 | i2c->tokens[0] = 0; |
| 65 | i2c->tokens[1] = 0; |
| 66 | i2c->num_tokens = 0; |
| 67 | } |
| 68 | |
| 69 | static void meson_i2c_add_token(struct meson_i2c *i2c, int token) |
| 70 | { |
| 71 | if (i2c->num_tokens < 8) |
| 72 | i2c->tokens[0] |= (token & 0xf) << (i2c->num_tokens * 4); |
| 73 | else |
| 74 | i2c->tokens[1] |= (token & 0xf) << ((i2c->num_tokens % 8) * 4); |
| 75 | |
| 76 | i2c->num_tokens++; |
| 77 | } |
| 78 | |
Beniamino Galvani | 9ba506d | 2017-11-26 17:40:57 +0100 | [diff] [blame] | 79 | /* |
| 80 | * Retrieve data for the current transfer (which can be at most 8 |
| 81 | * bytes) from the device internal buffer. |
| 82 | */ |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 83 | static void meson_i2c_get_data(struct meson_i2c *i2c, u8 *buf, int len) |
| 84 | { |
| 85 | u32 rdata0, rdata1; |
| 86 | int i; |
| 87 | |
| 88 | rdata0 = readl(&i2c->regs->tok_rdata0); |
| 89 | rdata1 = readl(&i2c->regs->tok_rdata1); |
| 90 | |
| 91 | debug("meson i2c: read data %08x %08x len %d\n", rdata0, rdata1, len); |
| 92 | |
| 93 | for (i = 0; i < min(4, len); i++) |
| 94 | *buf++ = (rdata0 >> i * 8) & 0xff; |
| 95 | |
| 96 | for (i = 4; i < min(8, len); i++) |
| 97 | *buf++ = (rdata1 >> (i - 4) * 8) & 0xff; |
| 98 | } |
| 99 | |
Beniamino Galvani | 9ba506d | 2017-11-26 17:40:57 +0100 | [diff] [blame] | 100 | /* |
| 101 | * Write data for the current transfer (which can be at most 8 bytes) |
| 102 | * to the device internal buffer. |
| 103 | */ |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 104 | static void meson_i2c_put_data(struct meson_i2c *i2c, u8 *buf, int len) |
| 105 | { |
| 106 | u32 wdata0 = 0, wdata1 = 0; |
| 107 | int i; |
| 108 | |
| 109 | for (i = 0; i < min(4, len); i++) |
| 110 | wdata0 |= *buf++ << (i * 8); |
| 111 | |
| 112 | for (i = 4; i < min(8, len); i++) |
| 113 | wdata1 |= *buf++ << ((i - 4) * 8); |
| 114 | |
| 115 | writel(wdata0, &i2c->regs->tok_wdata0); |
| 116 | writel(wdata1, &i2c->regs->tok_wdata1); |
| 117 | |
| 118 | debug("meson i2c: write data %08x %08x len %d\n", wdata0, wdata1, len); |
| 119 | } |
| 120 | |
Beniamino Galvani | 9ba506d | 2017-11-26 17:40:57 +0100 | [diff] [blame] | 121 | /* |
| 122 | * Prepare the next transfer: pick the next 8 bytes in the remaining |
| 123 | * part of message and write tokens and data (if needed) to the |
| 124 | * device. |
| 125 | */ |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 126 | static void meson_i2c_prepare_xfer(struct meson_i2c *i2c) |
| 127 | { |
| 128 | bool write = !(i2c->msg->flags & I2C_M_RD); |
| 129 | int i; |
| 130 | |
| 131 | i2c->count = min(i2c->msg->len - i2c->pos, 8u); |
| 132 | |
| 133 | for (i = 0; i + 1 < i2c->count; i++) |
| 134 | meson_i2c_add_token(i2c, TOKEN_DATA); |
| 135 | |
| 136 | if (i2c->count) { |
| 137 | if (write || i2c->pos + i2c->count < i2c->msg->len) |
| 138 | meson_i2c_add_token(i2c, TOKEN_DATA); |
| 139 | else |
| 140 | meson_i2c_add_token(i2c, TOKEN_DATA_LAST); |
| 141 | } |
| 142 | |
| 143 | if (write) |
| 144 | meson_i2c_put_data(i2c, i2c->msg->buf + i2c->pos, i2c->count); |
| 145 | |
| 146 | if (i2c->last && i2c->pos + i2c->count >= i2c->msg->len) |
| 147 | meson_i2c_add_token(i2c, TOKEN_STOP); |
| 148 | |
| 149 | writel(i2c->tokens[0], &i2c->regs->tok_list0); |
| 150 | writel(i2c->tokens[1], &i2c->regs->tok_list1); |
| 151 | } |
| 152 | |
| 153 | static void meson_i2c_do_start(struct meson_i2c *i2c, struct i2c_msg *msg) |
| 154 | { |
| 155 | int token; |
| 156 | |
| 157 | token = (msg->flags & I2C_M_RD) ? TOKEN_SLAVE_ADDR_READ : |
| 158 | TOKEN_SLAVE_ADDR_WRITE; |
| 159 | |
| 160 | writel(msg->addr << 1, &i2c->regs->slave_addr); |
| 161 | meson_i2c_add_token(i2c, TOKEN_START); |
| 162 | meson_i2c_add_token(i2c, token); |
| 163 | } |
| 164 | |
| 165 | static int meson_i2c_xfer_msg(struct meson_i2c *i2c, struct i2c_msg *msg, |
| 166 | int last) |
| 167 | { |
| 168 | ulong start; |
| 169 | |
| 170 | debug("meson i2c: %s addr %u len %u\n", |
| 171 | (msg->flags & I2C_M_RD) ? "read" : "write", |
| 172 | msg->addr, msg->len); |
| 173 | |
| 174 | i2c->msg = msg; |
| 175 | i2c->last = last; |
| 176 | i2c->pos = 0; |
| 177 | i2c->count = 0; |
| 178 | |
| 179 | meson_i2c_reset_tokens(i2c); |
| 180 | meson_i2c_do_start(i2c, msg); |
| 181 | |
| 182 | do { |
| 183 | meson_i2c_prepare_xfer(i2c); |
| 184 | |
| 185 | /* start the transfer */ |
| 186 | setbits_le32(&i2c->regs->ctrl, REG_CTRL_START); |
| 187 | start = get_timer(0); |
| 188 | while (readl(&i2c->regs->ctrl) & REG_CTRL_STATUS) { |
| 189 | if (get_timer(start) > I2C_TIMEOUT_MS) { |
| 190 | clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START); |
| 191 | debug("meson i2c: timeout\n"); |
| 192 | return -ETIMEDOUT; |
| 193 | } |
| 194 | udelay(1); |
| 195 | } |
| 196 | meson_i2c_reset_tokens(i2c); |
| 197 | clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START); |
| 198 | |
| 199 | if (readl(&i2c->regs->ctrl) & REG_CTRL_ERROR) { |
| 200 | debug("meson i2c: error\n"); |
Beniamino Galvani | 04b9538 | 2017-11-26 17:40:56 +0100 | [diff] [blame] | 201 | return -EREMOTEIO; |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if ((msg->flags & I2C_M_RD) && i2c->count) { |
| 205 | meson_i2c_get_data(i2c, i2c->msg->buf + i2c->pos, |
| 206 | i2c->count); |
| 207 | } |
| 208 | i2c->pos += i2c->count; |
| 209 | } while (i2c->pos < msg->len); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int meson_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 215 | int nmsgs) |
| 216 | { |
| 217 | struct meson_i2c *i2c = dev_get_priv(bus); |
| 218 | int i, ret = 0; |
| 219 | |
| 220 | for (i = 0; i < nmsgs; i++) { |
| 221 | ret = meson_i2c_xfer_msg(i2c, msg + i, i == nmsgs - 1); |
| 222 | if (ret) |
Beniamino Galvani | 04b9538 | 2017-11-26 17:40:56 +0100 | [diff] [blame] | 223 | return ret; |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int meson_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 230 | { |
| 231 | struct meson_i2c *i2c = dev_get_priv(bus); |
Beniamino Galvani | 6c9c980 | 2018-06-14 13:43:40 +0200 | [diff] [blame] | 232 | ulong clk_rate; |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 233 | unsigned int div; |
| 234 | |
Beniamino Galvani | 6c9c980 | 2018-06-14 13:43:40 +0200 | [diff] [blame] | 235 | clk_rate = clk_get_rate(&i2c->clk); |
| 236 | if (IS_ERR_VALUE(clk_rate)) |
| 237 | return -EINVAL; |
| 238 | |
Guillaume La Roque | b83dac6 | 2019-03-22 14:49:32 +0100 | [diff] [blame] | 239 | div = DIV_ROUND_UP(clk_rate, speed * i2c->data->div_factor); |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 240 | |
| 241 | /* clock divider has 12 bits */ |
| 242 | if (div >= (1 << 12)) { |
| 243 | debug("meson i2c: requested bus frequency too low\n"); |
| 244 | div = (1 << 12) - 1; |
| 245 | } |
| 246 | |
| 247 | clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIV_MASK, |
| 248 | (div & GENMASK(9, 0)) << REG_CTRL_CLKDIV_SHIFT); |
| 249 | |
| 250 | clrsetbits_le32(&i2c->regs->ctrl, REG_CTRL_CLKDIVEXT_MASK, |
| 251 | (div >> 10) << REG_CTRL_CLKDIVEXT_SHIFT); |
| 252 | |
Beniamino Galvani | 6c9c980 | 2018-06-14 13:43:40 +0200 | [diff] [blame] | 253 | debug("meson i2c: set clk %u, src %lu, div %u\n", speed, clk_rate, div); |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int meson_i2c_probe(struct udevice *bus) |
| 259 | { |
| 260 | struct meson_i2c *i2c = dev_get_priv(bus); |
Beniamino Galvani | 6c9c980 | 2018-06-14 13:43:40 +0200 | [diff] [blame] | 261 | int ret; |
| 262 | |
Guillaume La Roque | b83dac6 | 2019-03-22 14:49:32 +0100 | [diff] [blame] | 263 | i2c->data = (const struct meson_i2c_data *)dev_get_driver_data(bus); |
| 264 | |
Beniamino Galvani | 6c9c980 | 2018-06-14 13:43:40 +0200 | [diff] [blame] | 265 | ret = clk_get_by_index(bus, 0, &i2c->clk); |
| 266 | if (ret < 0) |
| 267 | return ret; |
| 268 | |
| 269 | ret = clk_enable(&i2c->clk); |
| 270 | if (ret) |
| 271 | return ret; |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 272 | |
| 273 | i2c->regs = dev_read_addr_ptr(bus); |
| 274 | clrbits_le32(&i2c->regs->ctrl, REG_CTRL_START); |
| 275 | |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static const struct dm_i2c_ops meson_i2c_ops = { |
| 280 | .xfer = meson_i2c_xfer, |
| 281 | .set_bus_speed = meson_i2c_set_bus_speed, |
| 282 | }; |
| 283 | |
Guillaume La Roque | b83dac6 | 2019-03-22 14:49:32 +0100 | [diff] [blame] | 284 | static const struct meson_i2c_data i2c_meson6_data = { |
| 285 | .div_factor = 4, |
| 286 | }; |
| 287 | |
| 288 | static const struct meson_i2c_data i2c_gxbb_data = { |
| 289 | .div_factor = 4, |
| 290 | }; |
| 291 | |
| 292 | static const struct meson_i2c_data i2c_axg_data = { |
| 293 | .div_factor = 3, |
| 294 | }; |
| 295 | |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 296 | static const struct udevice_id meson_i2c_ids[] = { |
Guillaume La Roque | b83dac6 | 2019-03-22 14:49:32 +0100 | [diff] [blame] | 297 | {.compatible = "amlogic,meson6-i2c", .data = (ulong)&i2c_meson6_data}, |
| 298 | {.compatible = "amlogic,meson-gx-i2c", .data = (ulong)&i2c_gxbb_data}, |
| 299 | {.compatible = "amlogic,meson-gxbb-i2c", .data = (ulong)&i2c_gxbb_data}, |
| 300 | {.compatible = "amlogic,meson-axg-i2c", .data = (ulong)&i2c_axg_data}, |
| 301 | {} |
Beniamino Galvani | d5b199c | 2017-10-29 10:09:00 +0100 | [diff] [blame] | 302 | }; |
| 303 | |
| 304 | U_BOOT_DRIVER(i2c_meson) = { |
| 305 | .name = "i2c_meson", |
| 306 | .id = UCLASS_I2C, |
| 307 | .of_match = meson_i2c_ids, |
| 308 | .probe = meson_i2c_probe, |
| 309 | .priv_auto_alloc_size = sizeof(struct meson_i2c), |
| 310 | .ops = &meson_i2c_ops, |
| 311 | }; |