Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | // SPDX-License-Identifier: GPL-2.0+ /* * U-Boot driver for the MediaTek MT7621 I2C controller. * * Derived from the Linux kernel driver: * drivers/i2c/busses/i2c-mt7621.c * * Copyright (C) 2013 Steven Liu <steven_liu@mediatek.com> * Copyright (C) 2014 Sittisak <sittisaks@hotmail.com> * Copyright (C) 2016 Michael Lee <igvtee@gmail.com> * Copyright (C) 2018 Jan Breuer <jan.breuer@jaybee.cz> * Copyright (C) 2025 Justin Swartz <justin.swartz@risingedge.co.za> */ #include <asm/io.h> #include <dm/device.h> #include <dm/device_compat.h> #include <linux/delay.h> #include <linux/errno.h> #include <linux/printk.h> #include <dm.h> #include <clk.h> #include <i2c.h> #include <log.h> #include <reset.h> #include <time.h> #define REG_SM0CFG2 0x28 #define REG_SM0CTL0 0x40 #define REG_SM0CTL1 0x44 #define REG_SM0D0 0x50 #define REG_SM0D1 0x54 #define SM0CFG2_MODE_MANUAL 0 #define SM0CTL0_ODRAIN BIT(31) #define SM0CTL0_CLK_DIV_MASK (0x7ff << 16) #define SM0CTL0_CLK_DIV_MAX 0x7ff #define SM0CTL0_EN BIT(1) #define SM0CTL0_SCL_STRETCH BIT(0) #define SM0CTL1_TRI BIT(0) #define SM0CTL1_TRI_IDLE 0 #define SM0CTL1_START (1 << 4) #define SM0CTL1_WRITE (2 << 4) #define SM0CTL1_STOP (3 << 4) #define SM0CTL1_READ_LAST (4 << 4) #define SM0CTL1_READ (5 << 4) #define SM0CTL1_PGLEN(x) ((((x) - 1) << 8) & SM0CTL1_PGLEN_MASK) #define SM0CTL1_PGLEN_MASK (0x7 << 8) #define SM0CTL1_ACK_MASK (0xff << 16) #define TIMEOUT_1SEC 1000 #define I2C_MAX_STD_MODE_FREQ 100000 struct mt7621_i2c_priv { void __iomem *base; uint speed; u32 clk_div; struct clk clk; struct reset_ctl reset_ctl; }; static int mt7621_i2c_wait_idle(struct udevice *dev) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); ulong start_time = get_timer(0); u32 value; while (get_timer(start_time) < TIMEOUT_1SEC) { value = readl(priv->base + REG_SM0CTL1); if ((value & SM0CTL1_TRI) == SM0CTL1_TRI_IDLE) return 0; udelay(10); } return -ETIMEDOUT; } static int mt7621_i2c_reset(struct udevice *dev) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); u32 value; reset_assert(&priv->reset_ctl); udelay(100); reset_deassert(&priv->reset_ctl); value = readl(priv->base + REG_SM0CTL0); value &= ~SM0CTL0_CLK_DIV_MASK; value |= (priv->clk_div << 16) & SM0CTL0_CLK_DIV_MASK; value |= SM0CTL0_EN | SM0CTL0_SCL_STRETCH; writel(value, priv->base + REG_SM0CTL0); writel(SM0CFG2_MODE_MANUAL, priv->base + REG_SM0CFG2); return 0; } static int mt7621_i2c_master_start(struct udevice *dev) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); writel(SM0CTL1_START | SM0CTL1_TRI, priv->base + REG_SM0CTL1); return mt7621_i2c_wait_idle(dev); } static int mt7621_i2c_master_stop(struct udevice *dev) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); writel(SM0CTL1_STOP | SM0CTL1_TRI, priv->base + REG_SM0CTL1); return mt7621_i2c_wait_idle(dev); } static int mt7621_i2c_master_cmd(struct udevice *dev, u32 cmd, int len) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); writel(cmd | SM0CTL1_TRI | SM0CTL1_PGLEN(len), priv->base + REG_SM0CTL1); return mt7621_i2c_wait_idle(dev); } static int mt7621_i2c_7bit_address(struct udevice *dev, struct i2c_msg *msg) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); u32 addr = msg->addr << 1; if (msg->flags & I2C_M_RD) addr |= 1; writel(addr, priv->base + REG_SM0D0); return mt7621_i2c_master_cmd(dev, SM0CTL1_WRITE, 1); } static int mt7621_i2c_10bit_address(struct udevice *dev, struct i2c_msg *msg) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); u16 addr = 0xf0 | ((msg->addr >> 7) & 0x06) | (msg->addr & 0xff) << 8; if (msg->flags & I2C_M_RD) addr |= 1; writel(addr, priv->base + REG_SM0D0); return mt7621_i2c_master_cmd(dev, SM0CTL1_WRITE, 2); } static int mt7621_i2c_address(struct udevice *dev, struct i2c_msg *msg) { int ret; if (msg->flags & I2C_M_TEN) { ret = mt7621_i2c_10bit_address(dev, msg); if (ret) return ret; } else { ret = mt7621_i2c_7bit_address(dev, msg); if (ret) return ret; } return 0; } static int mt7621_i2c_check_ack(struct udevice *dev, struct i2c_msg *msg, u32 length) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); u32 status = readl(priv->base + REG_SM0CTL1); u32 expected = GENMASK(length - 1, 0); u32 mask = (expected << 16) & SM0CTL1_ACK_MASK; if (msg->flags & I2C_M_IGNORE_NAK) return 0; if ((status & mask) != mask) return -ENXIO; return 0; } static int mt7621_i2c_master_read(struct udevice *dev, struct i2c_msg *msg) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); int offset, length, last, ret; u32 cmd; u32 data[2]; for (offset = 0; offset < msg->len; offset += 8) { if (msg->len - offset >= 8) length = 8; else length = msg->len - offset; last = msg->len - offset <= 8; cmd = last ? SM0CTL1_READ_LAST : SM0CTL1_READ; ret = mt7621_i2c_master_cmd(dev, cmd, length); if (ret) return ret; data[0] = readl(priv->base + REG_SM0D0); data[1] = readl(priv->base + REG_SM0D1); memcpy(&msg->buf[offset], data, length); } return 0; } static int mt7621_i2c_master_write(struct udevice *dev, struct i2c_msg *msg) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); int offset, length, ret; u32 data[2]; for (offset = 0; offset < msg->len; offset += 8) { if (msg->len - offset >= 8) length = 8; else length = msg->len - offset; memcpy(data, &msg->buf[offset], length); writel(data[0], priv->base + REG_SM0D0); writel(data[1], priv->base + REG_SM0D1); ret = mt7621_i2c_master_cmd(dev, SM0CTL1_WRITE, length); if (ret) return ret; ret = mt7621_i2c_check_ack(dev, msg, length); if (ret) return ret; } return 0; } static int mt7621_i2c_xfer(struct udevice *dev, struct i2c_msg *msgs, int count) { struct i2c_msg *msg; int index, ret; for (index = 0; index < count; index++) { msg = &msgs[index]; ret = mt7621_i2c_wait_idle(dev); if (ret) goto reset; ret = mt7621_i2c_master_start(dev); if (ret) goto reset; ret = mt7621_i2c_address(dev, msg); if (ret) goto reset; ret = mt7621_i2c_check_ack(dev, msg, 1); if (ret) goto stop; if (msg->flags & I2C_M_RD) { ret = mt7621_i2c_master_read(dev, msg); if (ret) goto reset; } else { ret = mt7621_i2c_master_write(dev, msg); if (ret) goto reset; } } ret = mt7621_i2c_wait_idle(dev); if (ret) goto reset; ret = mt7621_i2c_master_stop(dev); if (ret) goto reset; return 0; stop: ret = mt7621_i2c_master_stop(dev); if (ret) goto reset; return -ENXIO; reset: mt7621_i2c_reset(dev); return ret; } static int mt7621_i2c_set_speed(struct udevice *dev, uint speed) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); ulong clk_rate = clk_get_rate(&priv->clk); priv->speed = speed; priv->clk_div = clk_rate / priv->speed - 1; if (priv->clk_div < 99) priv->clk_div = 99; if (priv->clk_div > SM0CTL0_CLK_DIV_MAX) priv->clk_div = SM0CTL0_CLK_DIV_MAX; return 0; } static const struct dm_i2c_ops mt7621_i2c_ops = { .xfer = mt7621_i2c_xfer, .set_bus_speed = mt7621_i2c_set_speed, .deblock = mt7621_i2c_reset, }; static int mt7621_i2c_of_to_plat(struct udevice *dev) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); priv->base = dev_remap_addr(dev); return 0; } int mt7621_i2c_probe(struct udevice *dev) { struct mt7621_i2c_priv *priv = dev_get_priv(dev); int ret; priv->base = dev_remap_addr(dev); if (!priv->base) { dev_err(dev, "failed to get base address\n"); return -EINVAL; } ret = clk_get_by_name(dev, "sys_clock", &priv->clk); if (ret) { dev_err(dev, "failed to get clock source\n"); return ret; } ret = reset_get_by_name(dev, "i2c_reset", &priv->reset_ctl); if (ret) { dev_err(dev, "failed to get reset control\n"); return ret; } ret = clk_enable(&priv->clk); if (ret) { dev_err(dev, "failed to enable clock\n"); return ret; } mt7621_i2c_set_speed(dev, I2C_MAX_STD_MODE_FREQ); mt7621_i2c_reset(dev); return 0; } static const struct udevice_id mt7621_i2c_ids[] = { { .compatible = "mediatek,mt7621-i2c" }, { } }; U_BOOT_DRIVER(mt7621_i2c) = { .name = "mt7621_i2c", .id = UCLASS_I2C, .of_match = mt7621_i2c_ids, .of_to_plat = mt7621_i2c_of_to_plat, .probe = mt7621_i2c_probe, .priv_auto = sizeof(struct mt7621_i2c_priv), .ops = &mt7621_i2c_ops, }; |