| 1 |
// SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause |
| 2 |
/* |
| 3 |
* UCSI driver for STMicroelectronics STM32G0 Type-C PD controller |
| 4 |
* |
| 5 |
* Copyright (C) 2022, STMicroelectronics - All Rights Reserved |
| 6 |
* Author: Fabrice Gasnier <[email protected]>. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/delay.h> |
| 10 |
#include <linux/firmware.h> |
| 11 |
#include <linux/i2c.h> |
| 12 |
#include <linux/interrupt.h> |
| 13 |
#include <linux/minmax.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/platform_device.h> |
| 16 |
#include <linux/unaligned.h> |
| 17 |
|
| 18 |
#include "ucsi.h" |
| 19 |
|
| 20 |
/* STM32G0 I2C bootloader addr: 0b1010001x (See AN2606) */ |
| 21 |
#define STM32G0_I2C_BL_ADDR (0xa2 >> 1) |
| 22 |
|
| 23 |
/* STM32G0 I2C bootloader max data size */ |
| 24 |
#define STM32G0_I2C_BL_SZ 256 |
| 25 |
|
| 26 |
/* STM32 I2C bootloader commands (See AN4221) */ |
| 27 |
#define STM32_CMD_GVR 0x01 /* Gets the bootloader version */ |
| 28 |
#define STM32_CMD_GVR_LEN 1 |
| 29 |
#define STM32_CMD_RM 0x11 /* Reag memory */ |
| 30 |
#define STM32_CMD_WM 0x31 /* Write memory */ |
| 31 |
#define STM32_CMD_ADDR_LEN 5 /* Address len for go, mem write... */ |
| 32 |
#define STM32_CMD_ERASE 0x44 /* Erase page, bank or all */ |
| 33 |
#define STM32_CMD_ERASE_SPECIAL_LEN 3 |
| 34 |
#define STM32_CMD_GLOBAL_MASS_ERASE 0xffff /* All-bank erase */ |
| 35 |
|
| 36 |
/* STM32 I2C bootloader answer status */ |
| 37 |
#define STM32G0_I2C_BL_ACK 0x79 |
| 38 |
#define STM32G0_I2C_BL_NACK 0x1f |
| 39 |
#define STM32G0_I2C_BL_BUSY 0x76 |
| 40 |
|
| 41 |
/* STM32G0 flash definitions */ |
| 42 |
#define STM32G0_USER_OPTION_BYTES 0x1fff7800 |
| 43 |
#define STM32G0_USER_OB_NBOOT0 BIT(26) |
| 44 |
#define STM32G0_USER_OB_NBOOT_SEL BIT(24) |
| 45 |
#define STM32G0_USER_OB_BOOT_MAIN (STM32G0_USER_OB_NBOOT0 | STM32G0_USER_OB_NBOOT_SEL) |
| 46 |
#define STM32G0_MAIN_MEM_ADDR 0x08000000 |
| 47 |
|
| 48 |
/* STM32 Firmware definitions: additional commands */ |
| 49 |
#define STM32G0_FW_GETVER 0x00 /* Gets the firmware version */ |
| 50 |
#define STM32G0_FW_GETVER_LEN 4 |
| 51 |
#define STM32G0_FW_RSTGOBL 0x21 /* Reset and go to bootloader */ |
| 52 |
#define STM32G0_FW_KEYWORD 0xa56959a6 |
| 53 |
|
| 54 |
/* ucsi_stm32g0_fw_info located at the end of the firmware */ |
| 55 |
struct ucsi_stm32g0_fw_info { |
| 56 |
u32 version; |
| 57 |
u32 keyword; |
| 58 |
}; |
| 59 |
|
| 60 |
struct ucsi_stm32g0 { |
| 61 |
struct i2c_client *client; |
| 62 |
struct i2c_client *i2c_bl; |
| 63 |
bool in_bootloader; |
| 64 |
u8 bl_version; |
| 65 |
struct device *dev; |
| 66 |
const char *fw_name; |
| 67 |
struct ucsi *ucsi; |
| 68 |
bool suspended; |
| 69 |
bool wakeup_event; |
| 70 |
}; |
| 71 |
|
| 72 |
/* |
| 73 |
* Bootloader commands helpers: |
| 74 |
* - send command (2 bytes) |
| 75 |
* - check ack |
| 76 |
* Then either: |
| 77 |
* - receive data |
| 78 |
* - receive data + check ack |
| 79 |
* - send data + check ack |
| 80 |
* These operations depends on the command and have various length. |
| 81 |
*/ |
| 82 |
static int ucsi_stm32g0_bl_check_ack(struct ucsi *ucsi) |
| 83 |
{ |
| 84 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 85 |
struct i2c_client *client = g0->i2c_bl; |
| 86 |
unsigned char ack; |
| 87 |
struct i2c_msg msg[] = { |
| 88 |
{ |
| 89 |
.addr = client->addr, |
| 90 |
.flags = I2C_M_RD, |
| 91 |
.len = 1, |
| 92 |
.buf = &ack, |
| 93 |
}, |
| 94 |
}; |
| 95 |
int ret; |
| 96 |
|
| 97 |
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 98 |
if (ret != ARRAY_SIZE(msg)) { |
| 99 |
dev_err(g0->dev, "i2c bl ack (%02x), error: %d\n", client->addr, ret); |
| 100 |
|
| 101 |
return ret < 0 ? ret : -EIO; |
| 102 |
} |
| 103 |
|
| 104 |
/* The 'ack' byte should contain bootloader answer: ack/nack/busy */ |
| 105 |
switch (ack) { |
| 106 |
case STM32G0_I2C_BL_ACK: |
| 107 |
return 0; |
| 108 |
case STM32G0_I2C_BL_NACK: |
| 109 |
return -ENOENT; |
| 110 |
case STM32G0_I2C_BL_BUSY: |
| 111 |
return -EBUSY; |
| 112 |
default: |
| 113 |
dev_err(g0->dev, "i2c bl ack (%02x), invalid byte: %02x\n", |
| 114 |
client->addr, ack); |
| 115 |
return -EINVAL; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
static int ucsi_stm32g0_bl_cmd_check_ack(struct ucsi *ucsi, unsigned int cmd, bool check_ack) |
| 120 |
{ |
| 121 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 122 |
struct i2c_client *client = g0->i2c_bl; |
| 123 |
unsigned char buf[2]; |
| 124 |
struct i2c_msg msg[] = { |
| 125 |
{ |
| 126 |
.addr = client->addr, |
| 127 |
.flags = 0, |
| 128 |
.len = sizeof(buf), |
| 129 |
.buf = buf, |
| 130 |
}, |
| 131 |
}; |
| 132 |
int ret; |
| 133 |
|
| 134 |
/* |
| 135 |
* Send STM32 bootloader command format is two bytes: |
| 136 |
* - command code |
| 137 |
* - XOR'ed command code |
| 138 |
*/ |
| 139 |
buf[0] = cmd; |
| 140 |
buf[1] = cmd ^ 0xff; |
| 141 |
|
| 142 |
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 143 |
if (ret != ARRAY_SIZE(msg)) { |
| 144 |
dev_dbg(g0->dev, "i2c bl cmd %d (%02x), error: %d\n", cmd, client->addr, ret); |
| 145 |
|
| 146 |
return ret < 0 ? ret : -EIO; |
| 147 |
} |
| 148 |
|
| 149 |
if (check_ack) |
| 150 |
return ucsi_stm32g0_bl_check_ack(ucsi); |
| 151 |
|
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
static int ucsi_stm32g0_bl_cmd(struct ucsi *ucsi, unsigned int cmd) |
| 156 |
{ |
| 157 |
return ucsi_stm32g0_bl_cmd_check_ack(ucsi, cmd, true); |
| 158 |
} |
| 159 |
|
| 160 |
static int ucsi_stm32g0_bl_rcv_check_ack(struct ucsi *ucsi, void *data, size_t len, bool check_ack) |
| 161 |
{ |
| 162 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 163 |
struct i2c_client *client = g0->i2c_bl; |
| 164 |
struct i2c_msg msg[] = { |
| 165 |
{ |
| 166 |
.addr = client->addr, |
| 167 |
.flags = I2C_M_RD, |
| 168 |
.len = len, |
| 169 |
.buf = data, |
| 170 |
}, |
| 171 |
}; |
| 172 |
int ret; |
| 173 |
|
| 174 |
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 175 |
if (ret != ARRAY_SIZE(msg)) { |
| 176 |
dev_err(g0->dev, "i2c bl rcv %02x, error: %d\n", client->addr, ret); |
| 177 |
|
| 178 |
return ret < 0 ? ret : -EIO; |
| 179 |
} |
| 180 |
|
| 181 |
if (check_ack) |
| 182 |
return ucsi_stm32g0_bl_check_ack(ucsi); |
| 183 |
|
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
static int ucsi_stm32g0_bl_rcv(struct ucsi *ucsi, void *data, size_t len) |
| 188 |
{ |
| 189 |
return ucsi_stm32g0_bl_rcv_check_ack(ucsi, data, len, true); |
| 190 |
} |
| 191 |
|
| 192 |
static int ucsi_stm32g0_bl_rcv_woack(struct ucsi *ucsi, void *data, size_t len) |
| 193 |
{ |
| 194 |
return ucsi_stm32g0_bl_rcv_check_ack(ucsi, data, len, false); |
| 195 |
} |
| 196 |
|
| 197 |
static int ucsi_stm32g0_bl_send(struct ucsi *ucsi, void *data, size_t len) |
| 198 |
{ |
| 199 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 200 |
struct i2c_client *client = g0->i2c_bl; |
| 201 |
struct i2c_msg msg[] = { |
| 202 |
{ |
| 203 |
.addr = client->addr, |
| 204 |
.flags = 0, |
| 205 |
.len = len, |
| 206 |
.buf = data, |
| 207 |
}, |
| 208 |
}; |
| 209 |
int ret; |
| 210 |
|
| 211 |
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 212 |
if (ret != ARRAY_SIZE(msg)) { |
| 213 |
dev_err(g0->dev, "i2c bl send %02x, error: %d\n", client->addr, ret); |
| 214 |
|
| 215 |
return ret < 0 ? ret : -EIO; |
| 216 |
} |
| 217 |
|
| 218 |
return ucsi_stm32g0_bl_check_ack(ucsi); |
| 219 |
} |
| 220 |
|
| 221 |
/* Bootloader commands */ |
| 222 |
static int ucsi_stm32g0_bl_get_version(struct ucsi *ucsi, u8 *bl_version) |
| 223 |
{ |
| 224 |
int ret; |
| 225 |
|
| 226 |
ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_GVR); |
| 227 |
if (ret) |
| 228 |
return ret; |
| 229 |
|
| 230 |
return ucsi_stm32g0_bl_rcv(ucsi, bl_version, STM32_CMD_GVR_LEN); |
| 231 |
} |
| 232 |
|
| 233 |
static int ucsi_stm32g0_bl_send_addr(struct ucsi *ucsi, u32 addr) |
| 234 |
{ |
| 235 |
u8 data8[STM32_CMD_ADDR_LEN]; |
| 236 |
|
| 237 |
/* Address format: 4 bytes addr (MSB first) + XOR'ed addr bytes */ |
| 238 |
put_unaligned_be32(addr, data8); |
| 239 |
data8[4] = data8[0] ^ data8[1] ^ data8[2] ^ data8[3]; |
| 240 |
|
| 241 |
return ucsi_stm32g0_bl_send(ucsi, data8, STM32_CMD_ADDR_LEN); |
| 242 |
} |
| 243 |
|
| 244 |
static int ucsi_stm32g0_bl_global_mass_erase(struct ucsi *ucsi) |
| 245 |
{ |
| 246 |
u8 data8[4]; |
| 247 |
u16 *data16 = (u16 *)&data8[0]; |
| 248 |
int ret; |
| 249 |
|
| 250 |
data16[0] = STM32_CMD_GLOBAL_MASS_ERASE; |
| 251 |
data8[2] = data8[0] ^ data8[1]; |
| 252 |
|
| 253 |
ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_ERASE); |
| 254 |
if (ret) |
| 255 |
return ret; |
| 256 |
|
| 257 |
return ucsi_stm32g0_bl_send(ucsi, data8, STM32_CMD_ERASE_SPECIAL_LEN); |
| 258 |
} |
| 259 |
|
| 260 |
static int ucsi_stm32g0_bl_write(struct ucsi *ucsi, u32 addr, const void *data, size_t len) |
| 261 |
{ |
| 262 |
u8 *data8; |
| 263 |
int i, ret; |
| 264 |
|
| 265 |
if (!len || len > STM32G0_I2C_BL_SZ) |
| 266 |
return -EINVAL; |
| 267 |
|
| 268 |
/* Write memory: len bytes -1, data up to 256 bytes + XOR'ed bytes */ |
| 269 |
data8 = kmalloc(STM32G0_I2C_BL_SZ + 2, GFP_KERNEL); |
| 270 |
if (!data8) |
| 271 |
return -ENOMEM; |
| 272 |
|
| 273 |
ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_WM); |
| 274 |
if (ret) |
| 275 |
goto free; |
| 276 |
|
| 277 |
ret = ucsi_stm32g0_bl_send_addr(ucsi, addr); |
| 278 |
if (ret) |
| 279 |
goto free; |
| 280 |
|
| 281 |
data8[0] = len - 1; |
| 282 |
memcpy(data8 + 1, data, len); |
| 283 |
data8[len + 1] = data8[0]; |
| 284 |
for (i = 1; i <= len; i++) |
| 285 |
data8[len + 1] ^= data8[i]; |
| 286 |
|
| 287 |
ret = ucsi_stm32g0_bl_send(ucsi, data8, len + 2); |
| 288 |
free: |
| 289 |
kfree(data8); |
| 290 |
|
| 291 |
return ret; |
| 292 |
} |
| 293 |
|
| 294 |
static int ucsi_stm32g0_bl_read(struct ucsi *ucsi, u32 addr, void *data, size_t len) |
| 295 |
{ |
| 296 |
int ret; |
| 297 |
|
| 298 |
if (!len || len > STM32G0_I2C_BL_SZ) |
| 299 |
return -EINVAL; |
| 300 |
|
| 301 |
ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_RM); |
| 302 |
if (ret) |
| 303 |
return ret; |
| 304 |
|
| 305 |
ret = ucsi_stm32g0_bl_send_addr(ucsi, addr); |
| 306 |
if (ret) |
| 307 |
return ret; |
| 308 |
|
| 309 |
ret = ucsi_stm32g0_bl_cmd(ucsi, len - 1); |
| 310 |
if (ret) |
| 311 |
return ret; |
| 312 |
|
| 313 |
return ucsi_stm32g0_bl_rcv_woack(ucsi, data, len); |
| 314 |
} |
| 315 |
|
| 316 |
/* Firmware commands (the same address as the bootloader) */ |
| 317 |
static int ucsi_stm32g0_fw_cmd(struct ucsi *ucsi, unsigned int cmd) |
| 318 |
{ |
| 319 |
return ucsi_stm32g0_bl_cmd_check_ack(ucsi, cmd, false); |
| 320 |
} |
| 321 |
|
| 322 |
static int ucsi_stm32g0_fw_rcv(struct ucsi *ucsi, void *data, size_t len) |
| 323 |
{ |
| 324 |
return ucsi_stm32g0_bl_rcv_woack(ucsi, data, len); |
| 325 |
} |
| 326 |
|
| 327 |
/* UCSI ops */ |
| 328 |
static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t len) |
| 329 |
{ |
| 330 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 331 |
struct i2c_client *client = g0->client; |
| 332 |
u8 reg = offset; |
| 333 |
struct i2c_msg msg[] = { |
| 334 |
{ |
| 335 |
.addr = client->addr, |
| 336 |
.flags = 0, |
| 337 |
.len = 1, |
| 338 |
.buf = ®, |
| 339 |
}, |
| 340 |
{ |
| 341 |
.addr = client->addr, |
| 342 |
.flags = I2C_M_RD, |
| 343 |
.len = len, |
| 344 |
.buf = val, |
| 345 |
}, |
| 346 |
}; |
| 347 |
int ret; |
| 348 |
|
| 349 |
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 350 |
if (ret != ARRAY_SIZE(msg)) { |
| 351 |
dev_err(g0->dev, "i2c read %02x, %02x error: %d\n", client->addr, reg, ret); |
| 352 |
|
| 353 |
return ret < 0 ? ret : -EIO; |
| 354 |
} |
| 355 |
|
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
static int ucsi_stm32g0_read_version(struct ucsi *ucsi, u16 *version) |
| 360 |
{ |
| 361 |
return ucsi_stm32g0_read(ucsi, UCSI_VERSION, version, sizeof(*version)); |
| 362 |
} |
| 363 |
|
| 364 |
static int ucsi_stm32g0_read_cci(struct ucsi *ucsi, u32 *cci) |
| 365 |
{ |
| 366 |
return ucsi_stm32g0_read(ucsi, UCSI_CCI, cci, sizeof(*cci)); |
| 367 |
} |
| 368 |
|
| 369 |
static int ucsi_stm32g0_read_message_in(struct ucsi *ucsi, void *val, size_t len) |
| 370 |
{ |
| 371 |
return ucsi_stm32g0_read(ucsi, UCSI_MESSAGE_IN, val, len); |
| 372 |
} |
| 373 |
|
| 374 |
static int ucsi_stm32g0_async_control(struct ucsi *ucsi, u64 command) |
| 375 |
{ |
| 376 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 377 |
struct i2c_client *client = g0->client; |
| 378 |
struct i2c_msg msg[] = { |
| 379 |
{ |
| 380 |
.addr = client->addr, |
| 381 |
.flags = 0, |
| 382 |
} |
| 383 |
}; |
| 384 |
unsigned char *buf; |
| 385 |
int ret; |
| 386 |
|
| 387 |
buf = kmalloc(sizeof(command) + 1, GFP_KERNEL); |
| 388 |
if (!buf) |
| 389 |
return -ENOMEM; |
| 390 |
|
| 391 |
buf[0] = UCSI_CONTROL; |
| 392 |
memcpy(&buf[1], &command, sizeof(command)); |
| 393 |
msg[0].len = sizeof(command) + 1; |
| 394 |
msg[0].buf = buf; |
| 395 |
|
| 396 |
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
| 397 |
kfree(buf); |
| 398 |
if (ret != ARRAY_SIZE(msg)) { |
| 399 |
dev_err(g0->dev, "i2c write %02x, %02x error: %d\n", client->addr, UCSI_CONTROL, ret); |
| 400 |
|
| 401 |
return ret < 0 ? ret : -EIO; |
| 402 |
} |
| 403 |
|
| 404 |
return 0; |
| 405 |
} |
| 406 |
|
| 407 |
static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data) |
| 408 |
{ |
| 409 |
struct ucsi_stm32g0 *g0 = data; |
| 410 |
u32 cci; |
| 411 |
int ret; |
| 412 |
|
| 413 |
if (g0->suspended) |
| 414 |
g0->wakeup_event = true; |
| 415 |
|
| 416 |
ret = ucsi_stm32g0_read(g0->ucsi, UCSI_CCI, &cci, sizeof(cci)); |
| 417 |
if (ret) |
| 418 |
return IRQ_NONE; |
| 419 |
|
| 420 |
ucsi_notify_common(g0->ucsi, cci); |
| 421 |
|
| 422 |
return IRQ_HANDLED; |
| 423 |
} |
| 424 |
|
| 425 |
static const struct ucsi_operations ucsi_stm32g0_ops = { |
| 426 |
.read_version = ucsi_stm32g0_read_version, |
| 427 |
.read_cci = ucsi_stm32g0_read_cci, |
| 428 |
.poll_cci = ucsi_stm32g0_read_cci, |
| 429 |
.read_message_in = ucsi_stm32g0_read_message_in, |
| 430 |
.sync_control = ucsi_sync_control_common, |
| 431 |
.async_control = ucsi_stm32g0_async_control, |
| 432 |
}; |
| 433 |
|
| 434 |
static int ucsi_stm32g0_register(struct ucsi *ucsi) |
| 435 |
{ |
| 436 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 437 |
struct i2c_client *client = g0->client; |
| 438 |
int ret; |
| 439 |
|
| 440 |
/* Request alert interrupt */ |
| 441 |
ret = request_threaded_irq(client->irq, NULL, ucsi_stm32g0_irq_handler, IRQF_ONESHOT, |
| 442 |
dev_name(g0->dev), g0); |
| 443 |
if (ret) { |
| 444 |
dev_err(g0->dev, "request IRQ failed: %d\n", ret); |
| 445 |
return ret; |
| 446 |
} |
| 447 |
|
| 448 |
ret = ucsi_register(ucsi); |
| 449 |
if (ret) { |
| 450 |
dev_err_probe(g0->dev, ret, "ucsi_register failed\n"); |
| 451 |
free_irq(client->irq, g0); |
| 452 |
return ret; |
| 453 |
} |
| 454 |
|
| 455 |
return 0; |
| 456 |
} |
| 457 |
|
| 458 |
static void ucsi_stm32g0_unregister(struct ucsi *ucsi) |
| 459 |
{ |
| 460 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 461 |
struct i2c_client *client = g0->client; |
| 462 |
|
| 463 |
ucsi_unregister(ucsi); |
| 464 |
free_irq(client->irq, g0); |
| 465 |
} |
| 466 |
|
| 467 |
static void ucsi_stm32g0_fw_cb(const struct firmware *fw, void *context) |
| 468 |
{ |
| 469 |
struct ucsi_stm32g0 *g0; |
| 470 |
const u8 *data, *end; |
| 471 |
const struct ucsi_stm32g0_fw_info *fw_info; |
| 472 |
u32 addr = STM32G0_MAIN_MEM_ADDR, ob, fw_version; |
| 473 |
int ret, size; |
| 474 |
|
| 475 |
if (!context) |
| 476 |
return; |
| 477 |
|
| 478 |
g0 = ucsi_get_drvdata(context); |
| 479 |
|
| 480 |
if (!fw) |
| 481 |
goto fw_release; |
| 482 |
|
| 483 |
fw_info = (struct ucsi_stm32g0_fw_info *)(fw->data + fw->size - sizeof(*fw_info)); |
| 484 |
|
| 485 |
if (!g0->in_bootloader) { |
| 486 |
/* Read running firmware version */ |
| 487 |
ret = ucsi_stm32g0_fw_cmd(g0->ucsi, STM32G0_FW_GETVER); |
| 488 |
if (ret) { |
| 489 |
dev_err(g0->dev, "Get version cmd failed %d\n", ret); |
| 490 |
goto fw_release; |
| 491 |
} |
| 492 |
ret = ucsi_stm32g0_fw_rcv(g0->ucsi, &fw_version, |
| 493 |
STM32G0_FW_GETVER_LEN); |
| 494 |
if (ret) { |
| 495 |
dev_err(g0->dev, "Get version failed %d\n", ret); |
| 496 |
goto fw_release; |
| 497 |
} |
| 498 |
|
| 499 |
/* Sanity check on keyword and firmware version */ |
| 500 |
if (fw_info->keyword != STM32G0_FW_KEYWORD || fw_info->version == fw_version) |
| 501 |
goto fw_release; |
| 502 |
|
| 503 |
dev_info(g0->dev, "Flashing FW: %08x (%08x cur)\n", fw_info->version, fw_version); |
| 504 |
|
| 505 |
/* Switch to bootloader mode */ |
| 506 |
ucsi_stm32g0_unregister(g0->ucsi); |
| 507 |
ret = ucsi_stm32g0_fw_cmd(g0->ucsi, STM32G0_FW_RSTGOBL); |
| 508 |
if (ret) { |
| 509 |
dev_err(g0->dev, "bootloader cmd failed %d\n", ret); |
| 510 |
goto fw_release; |
| 511 |
} |
| 512 |
g0->in_bootloader = true; |
| 513 |
|
| 514 |
/* STM32G0 reboot delay */ |
| 515 |
msleep(100); |
| 516 |
} |
| 517 |
|
| 518 |
ret = ucsi_stm32g0_bl_global_mass_erase(g0->ucsi); |
| 519 |
if (ret) { |
| 520 |
dev_err(g0->dev, "Erase failed %d\n", ret); |
| 521 |
goto fw_release; |
| 522 |
} |
| 523 |
|
| 524 |
data = fw->data; |
| 525 |
end = fw->data + fw->size; |
| 526 |
while (data < end) { |
| 527 |
size = min(end - data, STM32G0_I2C_BL_SZ); |
| 528 |
ret = ucsi_stm32g0_bl_write(g0->ucsi, addr, data, size); |
| 529 |
if (ret) { |
| 530 |
dev_err(g0->dev, "Write failed %d\n", ret); |
| 531 |
goto fw_release; |
| 532 |
} |
| 533 |
addr += size; |
| 534 |
data += size; |
| 535 |
} |
| 536 |
|
| 537 |
dev_dbg(g0->dev, "Configure to boot from main flash\n"); |
| 538 |
|
| 539 |
ret = ucsi_stm32g0_bl_read(g0->ucsi, STM32G0_USER_OPTION_BYTES, &ob, sizeof(ob)); |
| 540 |
if (ret) { |
| 541 |
dev_err(g0->dev, "read user option bytes failed %d\n", ret); |
| 542 |
goto fw_release; |
| 543 |
} |
| 544 |
|
| 545 |
dev_dbg(g0->dev, "STM32G0_USER_OPTION_BYTES 0x%08x\n", ob); |
| 546 |
|
| 547 |
/* Configure user option bytes to boot from main flash next time */ |
| 548 |
ob |= STM32G0_USER_OB_BOOT_MAIN; |
| 549 |
|
| 550 |
/* Writing option bytes will also reset G0 for updates to be loaded */ |
| 551 |
ret = ucsi_stm32g0_bl_write(g0->ucsi, STM32G0_USER_OPTION_BYTES, &ob, sizeof(ob)); |
| 552 |
if (ret) { |
| 553 |
dev_err(g0->dev, "write user option bytes failed %d\n", ret); |
| 554 |
goto fw_release; |
| 555 |
} |
| 556 |
|
| 557 |
dev_info(g0->dev, "Starting, option bytes:0x%08x\n", ob); |
| 558 |
|
| 559 |
/* STM32G0 FW boot delay */ |
| 560 |
msleep(500); |
| 561 |
|
| 562 |
/* Register UCSI interface */ |
| 563 |
if (!ucsi_stm32g0_register(g0->ucsi)) |
| 564 |
g0->in_bootloader = false; |
| 565 |
|
| 566 |
fw_release: |
| 567 |
release_firmware(fw); |
| 568 |
} |
| 569 |
|
| 570 |
static int ucsi_stm32g0_probe_bootloader(struct ucsi *ucsi) |
| 571 |
{ |
| 572 |
struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi); |
| 573 |
int ret; |
| 574 |
u16 ucsi_version; |
| 575 |
|
| 576 |
/* firmware-name is optional */ |
| 577 |
if (device_property_present(g0->dev, "firmware-name")) { |
| 578 |
ret = device_property_read_string(g0->dev, "firmware-name", &g0->fw_name); |
| 579 |
if (ret < 0) |
| 580 |
return dev_err_probe(g0->dev, ret, "Error reading firmware-name\n"); |
| 581 |
} |
| 582 |
|
| 583 |
if (g0->fw_name) { |
| 584 |
/* STM32G0 in bootloader mode communicates at reserved address 0x51 */ |
| 585 |
g0->i2c_bl = i2c_new_dummy_device(g0->client->adapter, STM32G0_I2C_BL_ADDR); |
| 586 |
if (IS_ERR(g0->i2c_bl)) { |
| 587 |
ret = dev_err_probe(g0->dev, PTR_ERR(g0->i2c_bl), |
| 588 |
"Failed to register bootloader I2C address\n"); |
| 589 |
return ret; |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
/* |
| 594 |
* Try to guess if the STM32G0 is running a UCSI firmware. First probe the UCSI FW at its |
| 595 |
* i2c address. Fallback to bootloader i2c address only if firmware-name is specified. |
| 596 |
*/ |
| 597 |
ret = ucsi_stm32g0_read(ucsi, UCSI_VERSION, &ucsi_version, sizeof(ucsi_version)); |
| 598 |
if (!ret || !g0->fw_name) |
| 599 |
return ret; |
| 600 |
|
| 601 |
/* Speculatively read the bootloader version that has a known length. */ |
| 602 |
ret = ucsi_stm32g0_bl_get_version(ucsi, &g0->bl_version); |
| 603 |
if (ret < 0) { |
| 604 |
i2c_unregister_device(g0->i2c_bl); |
| 605 |
return ret; |
| 606 |
} |
| 607 |
|
| 608 |
/* Device in bootloader mode */ |
| 609 |
g0->in_bootloader = true; |
| 610 |
dev_info(g0->dev, "Bootloader Version 0x%02x\n", g0->bl_version); |
| 611 |
|
| 612 |
return 0; |
| 613 |
} |
| 614 |
|
| 615 |
static int ucsi_stm32g0_probe(struct i2c_client *client) |
| 616 |
{ |
| 617 |
struct device *dev = &client->dev; |
| 618 |
struct ucsi_stm32g0 *g0; |
| 619 |
int ret; |
| 620 |
|
| 621 |
g0 = devm_kzalloc(dev, sizeof(*g0), GFP_KERNEL); |
| 622 |
if (!g0) |
| 623 |
return -ENOMEM; |
| 624 |
|
| 625 |
g0->dev = dev; |
| 626 |
g0->client = client; |
| 627 |
i2c_set_clientdata(client, g0); |
| 628 |
|
| 629 |
g0->ucsi = ucsi_create(dev, &ucsi_stm32g0_ops); |
| 630 |
if (IS_ERR(g0->ucsi)) |
| 631 |
return PTR_ERR(g0->ucsi); |
| 632 |
|
| 633 |
ucsi_set_drvdata(g0->ucsi, g0); |
| 634 |
|
| 635 |
ret = ucsi_stm32g0_probe_bootloader(g0->ucsi); |
| 636 |
if (ret < 0) |
| 637 |
goto destroy; |
| 638 |
|
| 639 |
/* |
| 640 |
* Don't register in bootloader mode: wait for the firmware to be loaded and started before |
| 641 |
* registering UCSI device. |
| 642 |
*/ |
| 643 |
if (!g0->in_bootloader) { |
| 644 |
ret = ucsi_stm32g0_register(g0->ucsi); |
| 645 |
if (ret < 0) |
| 646 |
goto freei2c; |
| 647 |
} |
| 648 |
|
| 649 |
if (g0->fw_name) { |
| 650 |
/* |
| 651 |
* Asynchronously flash (e.g. bootloader mode) or update the running firmware, |
| 652 |
* not to hang the boot process |
| 653 |
*/ |
| 654 |
ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT, g0->fw_name, g0->dev, |
| 655 |
GFP_KERNEL, g0->ucsi, ucsi_stm32g0_fw_cb); |
| 656 |
if (ret < 0) { |
| 657 |
dev_err_probe(dev, ret, "firmware request failed\n"); |
| 658 |
goto unregister; |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
return 0; |
| 663 |
|
| 664 |
unregister: |
| 665 |
if (!g0->in_bootloader) |
| 666 |
ucsi_stm32g0_unregister(g0->ucsi); |
| 667 |
freei2c: |
| 668 |
if (g0->fw_name) |
| 669 |
i2c_unregister_device(g0->i2c_bl); |
| 670 |
destroy: |
| 671 |
ucsi_destroy(g0->ucsi); |
| 672 |
|
| 673 |
return ret; |
| 674 |
} |
| 675 |
|
| 676 |
static void ucsi_stm32g0_remove(struct i2c_client *client) |
| 677 |
{ |
| 678 |
struct ucsi_stm32g0 *g0 = i2c_get_clientdata(client); |
| 679 |
|
| 680 |
if (!g0->in_bootloader) |
| 681 |
ucsi_stm32g0_unregister(g0->ucsi); |
| 682 |
if (g0->fw_name) |
| 683 |
i2c_unregister_device(g0->i2c_bl); |
| 684 |
ucsi_destroy(g0->ucsi); |
| 685 |
} |
| 686 |
|
| 687 |
static int ucsi_stm32g0_suspend(struct device *dev) |
| 688 |
{ |
| 689 |
struct ucsi_stm32g0 *g0 = dev_get_drvdata(dev); |
| 690 |
struct i2c_client *client = g0->client; |
| 691 |
|
| 692 |
if (g0->in_bootloader) |
| 693 |
return 0; |
| 694 |
|
| 695 |
/* Keep the interrupt disabled until the i2c bus has been resumed */ |
| 696 |
disable_irq(client->irq); |
| 697 |
|
| 698 |
g0->suspended = true; |
| 699 |
g0->wakeup_event = false; |
| 700 |
|
| 701 |
if (device_may_wakeup(dev) || device_wakeup_path(dev)) |
| 702 |
enable_irq_wake(client->irq); |
| 703 |
|
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
static int ucsi_stm32g0_resume(struct device *dev) |
| 708 |
{ |
| 709 |
struct ucsi_stm32g0 *g0 = dev_get_drvdata(dev); |
| 710 |
struct i2c_client *client = g0->client; |
| 711 |
|
| 712 |
if (g0->in_bootloader) |
| 713 |
return 0; |
| 714 |
|
| 715 |
if (device_may_wakeup(dev) || device_wakeup_path(dev)) |
| 716 |
disable_irq_wake(client->irq); |
| 717 |
|
| 718 |
enable_irq(client->irq); |
| 719 |
|
| 720 |
/* Enforce any pending handler gets called to signal a wakeup_event */ |
| 721 |
synchronize_irq(client->irq); |
| 722 |
|
| 723 |
if (g0->wakeup_event) |
| 724 |
pm_wakeup_event(g0->dev, 0); |
| 725 |
|
| 726 |
g0->suspended = false; |
| 727 |
|
| 728 |
return 0; |
| 729 |
} |
| 730 |
|
| 731 |
static DEFINE_SIMPLE_DEV_PM_OPS(ucsi_stm32g0_pm_ops, ucsi_stm32g0_suspend, ucsi_stm32g0_resume); |
| 732 |
|
| 733 |
static const struct of_device_id __maybe_unused ucsi_stm32g0_typec_of_match[] = { |
| 734 |
{ .compatible = "st,stm32g0-typec" }, |
| 735 |
{}, |
| 736 |
}; |
| 737 |
MODULE_DEVICE_TABLE(of, ucsi_stm32g0_typec_of_match); |
| 738 |
|
| 739 |
static const struct i2c_device_id ucsi_stm32g0_typec_i2c_devid[] = { |
| 740 |
{ "stm32g0-typec" }, |
| 741 |
{} |
| 742 |
}; |
| 743 |
MODULE_DEVICE_TABLE(i2c, ucsi_stm32g0_typec_i2c_devid); |
| 744 |
|
| 745 |
static struct i2c_driver ucsi_stm32g0_i2c_driver = { |
| 746 |
.driver = { |
| 747 |
.name = "ucsi-stm32g0-i2c", |
| 748 |
.of_match_table = of_match_ptr(ucsi_stm32g0_typec_of_match), |
| 749 |
.pm = pm_sleep_ptr(&ucsi_stm32g0_pm_ops), |
| 750 |
}, |
| 751 |
.probe = ucsi_stm32g0_probe, |
| 752 |
.remove = ucsi_stm32g0_remove, |
| 753 |
.id_table = ucsi_stm32g0_typec_i2c_devid |
| 754 |
}; |
| 755 |
module_i2c_driver(ucsi_stm32g0_i2c_driver); |
| 756 |
|
| 757 |
MODULE_AUTHOR("Fabrice Gasnier <[email protected]>"); |
| 758 |
MODULE_DESCRIPTION("STMicroelectronics STM32G0 Type-C controller"); |
| 759 |
MODULE_LICENSE("Dual BSD/GPL"); |
| 760 |
MODULE_ALIAS("platform:ucsi-stm32g0"); |