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