1 // SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
3 * UCSI driver for STMicroelectronics STM32G0 Type-C PD controller
5 * Copyright (C) 2022, STMicroelectronics - All Rights Reserved
6 * Author: Fabrice Gasnier <fabrice.gasnier@foss.st.com>.
9 #include <linux/delay.h>
10 #include <linux/firmware.h>
11 #include <linux/i2c.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <asm/unaligned.h>
19 /* STM32G0 I2C bootloader addr: 0b1010001x (See AN2606) */
20 #define STM32G0_I2C_BL_ADDR (0xa2 >> 1)
22 /* STM32G0 I2C bootloader max data size */
23 #define STM32G0_I2C_BL_SZ 256
25 /* STM32 I2C bootloader commands (See AN4221) */
26 #define STM32_CMD_GVR 0x01 /* Gets the bootloader version */
27 #define STM32_CMD_GVR_LEN 1
28 #define STM32_CMD_RM 0x11 /* Reag memory */
29 #define STM32_CMD_WM 0x31 /* Write memory */
30 #define STM32_CMD_ADDR_LEN 5 /* Address len for go, mem write... */
31 #define STM32_CMD_ERASE 0x44 /* Erase page, bank or all */
32 #define STM32_CMD_ERASE_SPECIAL_LEN 3
33 #define STM32_CMD_GLOBAL_MASS_ERASE 0xffff /* All-bank erase */
35 /* STM32 I2C bootloader answer status */
36 #define STM32G0_I2C_BL_ACK 0x79
37 #define STM32G0_I2C_BL_NACK 0x1f
38 #define STM32G0_I2C_BL_BUSY 0x76
40 /* STM32G0 flash definitions */
41 #define STM32G0_USER_OPTION_BYTES 0x1fff7800
42 #define STM32G0_USER_OB_NBOOT0 BIT(26)
43 #define STM32G0_USER_OB_NBOOT_SEL BIT(24)
44 #define STM32G0_USER_OB_BOOT_MAIN (STM32G0_USER_OB_NBOOT0 | STM32G0_USER_OB_NBOOT_SEL)
45 #define STM32G0_MAIN_MEM_ADDR 0x08000000
47 /* STM32 Firmware definitions: additional commands */
48 #define STM32G0_FW_GETVER 0x00 /* Gets the firmware version */
49 #define STM32G0_FW_GETVER_LEN 4
50 #define STM32G0_FW_RSTGOBL 0x21 /* Reset and go to bootloader */
51 #define STM32G0_FW_KEYWORD 0xa56959a6
53 /* ucsi_stm32g0_fw_info located at the end of the firmware */
54 struct ucsi_stm32g0_fw_info {
60 struct i2c_client *client;
61 struct i2c_client *i2c_bl;
64 struct completion complete;
74 * Bootloader commands helpers:
75 * - send command (2 bytes)
79 * - receive data + check ack
80 * - send data + check ack
81 * These operations depends on the command and have various length.
83 static int ucsi_stm32g0_bl_check_ack(struct ucsi *ucsi)
85 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
86 struct i2c_client *client = g0->i2c_bl;
88 struct i2c_msg msg[] = {
98 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
99 if (ret != ARRAY_SIZE(msg)) {
100 dev_err(g0->dev, "i2c bl ack (%02x), error: %d\n", client->addr, ret);
102 return ret < 0 ? ret : -EIO;
105 /* The 'ack' byte should contain bootloader answer: ack/nack/busy */
107 case STM32G0_I2C_BL_ACK:
109 case STM32G0_I2C_BL_NACK:
111 case STM32G0_I2C_BL_BUSY:
114 dev_err(g0->dev, "i2c bl ack (%02x), invalid byte: %02x\n",
120 static int ucsi_stm32g0_bl_cmd_check_ack(struct ucsi *ucsi, unsigned int cmd, bool check_ack)
122 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
123 struct i2c_client *client = g0->i2c_bl;
124 unsigned char buf[2];
125 struct i2c_msg msg[] = {
127 .addr = client->addr,
136 * Send STM32 bootloader command format is two bytes:
138 * - XOR'ed command code
143 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
144 if (ret != ARRAY_SIZE(msg)) {
145 dev_dbg(g0->dev, "i2c bl cmd %d (%02x), error: %d\n", cmd, client->addr, ret);
147 return ret < 0 ? ret : -EIO;
151 return ucsi_stm32g0_bl_check_ack(ucsi);
156 static int ucsi_stm32g0_bl_cmd(struct ucsi *ucsi, unsigned int cmd)
158 return ucsi_stm32g0_bl_cmd_check_ack(ucsi, cmd, true);
161 static int ucsi_stm32g0_bl_rcv_check_ack(struct ucsi *ucsi, void *data, size_t len, bool check_ack)
163 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
164 struct i2c_client *client = g0->i2c_bl;
165 struct i2c_msg msg[] = {
167 .addr = client->addr,
175 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
176 if (ret != ARRAY_SIZE(msg)) {
177 dev_err(g0->dev, "i2c bl rcv %02x, error: %d\n", client->addr, ret);
179 return ret < 0 ? ret : -EIO;
183 return ucsi_stm32g0_bl_check_ack(ucsi);
188 static int ucsi_stm32g0_bl_rcv(struct ucsi *ucsi, void *data, size_t len)
190 return ucsi_stm32g0_bl_rcv_check_ack(ucsi, data, len, true);
193 static int ucsi_stm32g0_bl_rcv_woack(struct ucsi *ucsi, void *data, size_t len)
195 return ucsi_stm32g0_bl_rcv_check_ack(ucsi, data, len, false);
198 static int ucsi_stm32g0_bl_send(struct ucsi *ucsi, void *data, size_t len)
200 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
201 struct i2c_client *client = g0->i2c_bl;
202 struct i2c_msg msg[] = {
204 .addr = client->addr,
212 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
213 if (ret != ARRAY_SIZE(msg)) {
214 dev_err(g0->dev, "i2c bl send %02x, error: %d\n", client->addr, ret);
216 return ret < 0 ? ret : -EIO;
219 return ucsi_stm32g0_bl_check_ack(ucsi);
222 /* Bootloader commands */
223 static int ucsi_stm32g0_bl_get_version(struct ucsi *ucsi, u8 *bl_version)
227 ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_GVR);
231 return ucsi_stm32g0_bl_rcv(ucsi, bl_version, STM32_CMD_GVR_LEN);
234 static int ucsi_stm32g0_bl_send_addr(struct ucsi *ucsi, u32 addr)
236 u8 data8[STM32_CMD_ADDR_LEN];
238 /* Address format: 4 bytes addr (MSB first) + XOR'ed addr bytes */
239 put_unaligned_be32(addr, data8);
240 data8[4] = data8[0] ^ data8[1] ^ data8[2] ^ data8[3];
242 return ucsi_stm32g0_bl_send(ucsi, data8, STM32_CMD_ADDR_LEN);
245 static int ucsi_stm32g0_bl_global_mass_erase(struct ucsi *ucsi)
248 u16 *data16 = (u16 *)&data8[0];
251 data16[0] = STM32_CMD_GLOBAL_MASS_ERASE;
252 data8[2] = data8[0] ^ data8[1];
254 ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_ERASE);
258 return ucsi_stm32g0_bl_send(ucsi, data8, STM32_CMD_ERASE_SPECIAL_LEN);
261 static int ucsi_stm32g0_bl_write(struct ucsi *ucsi, u32 addr, const void *data, size_t len)
266 if (!len || len > STM32G0_I2C_BL_SZ)
269 /* Write memory: len bytes -1, data up to 256 bytes + XOR'ed bytes */
270 data8 = kmalloc(STM32G0_I2C_BL_SZ + 2, GFP_KERNEL);
274 ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_WM);
278 ret = ucsi_stm32g0_bl_send_addr(ucsi, addr);
283 memcpy(data8 + 1, data, len);
284 data8[len + 1] = data8[0];
285 for (i = 1; i <= len; i++)
286 data8[len + 1] ^= data8[i];
288 ret = ucsi_stm32g0_bl_send(ucsi, data8, len + 2);
295 static int ucsi_stm32g0_bl_read(struct ucsi *ucsi, u32 addr, void *data, size_t len)
299 if (!len || len > STM32G0_I2C_BL_SZ)
302 ret = ucsi_stm32g0_bl_cmd(ucsi, STM32_CMD_RM);
306 ret = ucsi_stm32g0_bl_send_addr(ucsi, addr);
310 ret = ucsi_stm32g0_bl_cmd(ucsi, len - 1);
314 return ucsi_stm32g0_bl_rcv_woack(ucsi, data, len);
317 /* Firmware commands (the same address as the bootloader) */
318 static int ucsi_stm32g0_fw_cmd(struct ucsi *ucsi, unsigned int cmd)
320 return ucsi_stm32g0_bl_cmd_check_ack(ucsi, cmd, false);
323 static int ucsi_stm32g0_fw_rcv(struct ucsi *ucsi, void *data, size_t len)
325 return ucsi_stm32g0_bl_rcv_woack(ucsi, data, len);
329 static int ucsi_stm32g0_read(struct ucsi *ucsi, unsigned int offset, void *val, size_t len)
331 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
332 struct i2c_client *client = g0->client;
334 struct i2c_msg msg[] = {
336 .addr = client->addr,
342 .addr = client->addr,
350 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
351 if (ret != ARRAY_SIZE(msg)) {
352 dev_err(g0->dev, "i2c read %02x, %02x error: %d\n", client->addr, reg, ret);
354 return ret < 0 ? ret : -EIO;
360 static int ucsi_stm32g0_async_write(struct ucsi *ucsi, unsigned int offset, const void *val,
363 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
364 struct i2c_client *client = g0->client;
365 struct i2c_msg msg[] = {
367 .addr = client->addr,
374 buf = kmalloc(len + 1, GFP_KERNEL);
379 memcpy(&buf[1], val, len);
380 msg[0].len = len + 1;
383 ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
385 if (ret != ARRAY_SIZE(msg)) {
386 dev_err(g0->dev, "i2c write %02x, %02x error: %d\n", client->addr, offset, ret);
388 return ret < 0 ? ret : -EIO;
394 static int ucsi_stm32g0_sync_write(struct ucsi *ucsi, unsigned int offset, const void *val,
397 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
400 set_bit(COMMAND_PENDING, &g0->flags);
402 ret = ucsi_stm32g0_async_write(ucsi, offset, val, len);
406 if (!wait_for_completion_timeout(&g0->complete, msecs_to_jiffies(5000)))
410 clear_bit(COMMAND_PENDING, &g0->flags);
415 static irqreturn_t ucsi_stm32g0_irq_handler(int irq, void *data)
417 struct ucsi_stm32g0 *g0 = data;
422 g0->wakeup_event = true;
424 ret = ucsi_stm32g0_read(g0->ucsi, UCSI_CCI, &cci, sizeof(cci));
428 if (UCSI_CCI_CONNECTOR(cci))
429 ucsi_connector_change(g0->ucsi, UCSI_CCI_CONNECTOR(cci));
431 if (test_bit(COMMAND_PENDING, &g0->flags) &&
432 cci & (UCSI_CCI_ACK_COMPLETE | UCSI_CCI_COMMAND_COMPLETE))
433 complete(&g0->complete);
438 static const struct ucsi_operations ucsi_stm32g0_ops = {
439 .read = ucsi_stm32g0_read,
440 .sync_write = ucsi_stm32g0_sync_write,
441 .async_write = ucsi_stm32g0_async_write,
444 static int ucsi_stm32g0_register(struct ucsi *ucsi)
446 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
447 struct i2c_client *client = g0->client;
450 /* Request alert interrupt */
451 ret = request_threaded_irq(client->irq, NULL, ucsi_stm32g0_irq_handler, IRQF_ONESHOT,
452 dev_name(g0->dev), g0);
454 dev_err(g0->dev, "request IRQ failed: %d\n", ret);
458 ret = ucsi_register(ucsi);
460 dev_err_probe(g0->dev, ret, "ucsi_register failed\n");
461 free_irq(client->irq, g0);
468 static void ucsi_stm32g0_unregister(struct ucsi *ucsi)
470 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
471 struct i2c_client *client = g0->client;
473 ucsi_unregister(ucsi);
474 free_irq(client->irq, g0);
477 static void ucsi_stm32g0_fw_cb(const struct firmware *fw, void *context)
479 struct ucsi_stm32g0 *g0;
480 const u8 *data, *end;
481 const struct ucsi_stm32g0_fw_info *fw_info;
482 u32 addr = STM32G0_MAIN_MEM_ADDR, ob, fw_version;
488 g0 = ucsi_get_drvdata(context);
493 fw_info = (struct ucsi_stm32g0_fw_info *)(fw->data + fw->size - sizeof(*fw_info));
495 if (!g0->in_bootloader) {
496 /* Read running firmware version */
497 ret = ucsi_stm32g0_fw_cmd(g0->ucsi, STM32G0_FW_GETVER);
499 dev_err(g0->dev, "Get version cmd failed %d\n", ret);
502 ret = ucsi_stm32g0_fw_rcv(g0->ucsi, &fw_version,
503 STM32G0_FW_GETVER_LEN);
505 dev_err(g0->dev, "Get version failed %d\n", ret);
509 /* Sanity check on keyword and firmware version */
510 if (fw_info->keyword != STM32G0_FW_KEYWORD || fw_info->version == fw_version)
513 dev_info(g0->dev, "Flashing FW: %08x (%08x cur)\n", fw_info->version, fw_version);
515 /* Switch to bootloader mode */
516 ucsi_stm32g0_unregister(g0->ucsi);
517 ret = ucsi_stm32g0_fw_cmd(g0->ucsi, STM32G0_FW_RSTGOBL);
519 dev_err(g0->dev, "bootloader cmd failed %d\n", ret);
522 g0->in_bootloader = true;
524 /* STM32G0 reboot delay */
528 ret = ucsi_stm32g0_bl_global_mass_erase(g0->ucsi);
530 dev_err(g0->dev, "Erase failed %d\n", ret);
535 end = fw->data + fw->size;
537 if ((end - data) < STM32G0_I2C_BL_SZ)
540 size = STM32G0_I2C_BL_SZ;
542 ret = ucsi_stm32g0_bl_write(g0->ucsi, addr, data, size);
544 dev_err(g0->dev, "Write failed %d\n", ret);
551 dev_dbg(g0->dev, "Configure to boot from main flash\n");
553 ret = ucsi_stm32g0_bl_read(g0->ucsi, STM32G0_USER_OPTION_BYTES, &ob, sizeof(ob));
555 dev_err(g0->dev, "read user option bytes failed %d\n", ret);
559 dev_dbg(g0->dev, "STM32G0_USER_OPTION_BYTES 0x%08x\n", ob);
561 /* Configure user option bytes to boot from main flash next time */
562 ob |= STM32G0_USER_OB_BOOT_MAIN;
564 /* Writing option bytes will also reset G0 for updates to be loaded */
565 ret = ucsi_stm32g0_bl_write(g0->ucsi, STM32G0_USER_OPTION_BYTES, &ob, sizeof(ob));
567 dev_err(g0->dev, "write user option bytes failed %d\n", ret);
571 dev_info(g0->dev, "Starting, option bytes:0x%08x\n", ob);
573 /* STM32G0 FW boot delay */
576 /* Register UCSI interface */
577 if (!ucsi_stm32g0_register(g0->ucsi))
578 g0->in_bootloader = false;
581 release_firmware(fw);
584 static int ucsi_stm32g0_probe_bootloader(struct ucsi *ucsi)
586 struct ucsi_stm32g0 *g0 = ucsi_get_drvdata(ucsi);
590 /* firmware-name is optional */
591 if (device_property_present(g0->dev, "firmware-name")) {
592 ret = device_property_read_string(g0->dev, "firmware-name", &g0->fw_name);
594 return dev_err_probe(g0->dev, ret, "Error reading firmware-name\n");
598 /* STM32G0 in bootloader mode communicates at reserved address 0x51 */
599 g0->i2c_bl = i2c_new_dummy_device(g0->client->adapter, STM32G0_I2C_BL_ADDR);
600 if (IS_ERR(g0->i2c_bl)) {
601 ret = dev_err_probe(g0->dev, PTR_ERR(g0->i2c_bl),
602 "Failed to register bootloader I2C address\n");
608 * Try to guess if the STM32G0 is running a UCSI firmware. First probe the UCSI FW at its
609 * i2c address. Fallback to bootloader i2c address only if firmware-name is specified.
611 ret = ucsi_stm32g0_read(ucsi, UCSI_VERSION, &ucsi_version, sizeof(ucsi_version));
612 if (!ret || !g0->fw_name)
615 /* Speculatively read the bootloader version that has a known length. */
616 ret = ucsi_stm32g0_bl_get_version(ucsi, &g0->bl_version);
618 i2c_unregister_device(g0->i2c_bl);
622 /* Device in bootloader mode */
623 g0->in_bootloader = true;
624 dev_info(g0->dev, "Bootloader Version 0x%02x\n", g0->bl_version);
629 static int ucsi_stm32g0_probe(struct i2c_client *client)
631 struct device *dev = &client->dev;
632 struct ucsi_stm32g0 *g0;
635 g0 = devm_kzalloc(dev, sizeof(*g0), GFP_KERNEL);
641 init_completion(&g0->complete);
642 i2c_set_clientdata(client, g0);
644 g0->ucsi = ucsi_create(dev, &ucsi_stm32g0_ops);
645 if (IS_ERR(g0->ucsi))
646 return PTR_ERR(g0->ucsi);
648 ucsi_set_drvdata(g0->ucsi, g0);
650 ret = ucsi_stm32g0_probe_bootloader(g0->ucsi);
655 * Don't register in bootloader mode: wait for the firmware to be loaded and started before
656 * registering UCSI device.
658 if (!g0->in_bootloader) {
659 ret = ucsi_stm32g0_register(g0->ucsi);
666 * Asynchronously flash (e.g. bootloader mode) or update the running firmware,
667 * not to hang the boot process
669 ret = reject_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT, g0->fw_name, g0->dev,
670 GFP_KERNEL, g0->ucsi, ucsi_stm32g0_fw_cb);
672 dev_err_probe(dev, ret, "firmware request failed\n");
680 if (!g0->in_bootloader)
681 ucsi_stm32g0_unregister(g0->ucsi);
684 i2c_unregister_device(g0->i2c_bl);
686 ucsi_destroy(g0->ucsi);
691 static void ucsi_stm32g0_remove(struct i2c_client *client)
693 struct ucsi_stm32g0 *g0 = i2c_get_clientdata(client);
695 if (!g0->in_bootloader)
696 ucsi_stm32g0_unregister(g0->ucsi);
698 i2c_unregister_device(g0->i2c_bl);
699 ucsi_destroy(g0->ucsi);
702 static int ucsi_stm32g0_suspend(struct device *dev)
704 struct ucsi_stm32g0 *g0 = dev_get_drvdata(dev);
705 struct i2c_client *client = g0->client;
707 if (g0->in_bootloader)
710 /* Keep the interrupt disabled until the i2c bus has been resumed */
711 disable_irq(client->irq);
713 g0->suspended = true;
714 g0->wakeup_event = false;
716 if (device_may_wakeup(dev) || device_wakeup_path(dev))
717 enable_irq_wake(client->irq);
722 static int ucsi_stm32g0_resume(struct device *dev)
724 struct ucsi_stm32g0 *g0 = dev_get_drvdata(dev);
725 struct i2c_client *client = g0->client;
727 if (g0->in_bootloader)
730 if (device_may_wakeup(dev) || device_wakeup_path(dev))
731 disable_irq_wake(client->irq);
733 enable_irq(client->irq);
735 /* Enforce any pending handler gets called to signal a wakeup_event */
736 synchronize_irq(client->irq);
738 if (g0->wakeup_event)
739 pm_wakeup_event(g0->dev, 0);
741 g0->suspended = false;
746 static DEFINE_SIMPLE_DEV_PM_OPS(ucsi_stm32g0_pm_ops, ucsi_stm32g0_suspend, ucsi_stm32g0_resume);
748 static const struct of_device_id __maybe_unused ucsi_stm32g0_typec_of_match[] = {
749 { .compatible = "st,stm32g0-typec" },
752 MODULE_DEVICE_TABLE(of, ucsi_stm32g0_typec_of_match);
754 static const struct i2c_device_id ucsi_stm32g0_typec_i2c_devid[] = {
755 {"stm32g0-typec", 0},
758 MODULE_DEVICE_TABLE(i2c, ucsi_stm32g0_typec_i2c_devid);
760 static struct i2c_driver ucsi_stm32g0_i2c_driver = {
762 .name = "ucsi-stm32g0-i2c",
763 .of_match_table = of_match_ptr(ucsi_stm32g0_typec_of_match),
764 .pm = pm_sleep_ptr(&ucsi_stm32g0_pm_ops),
766 .probe = ucsi_stm32g0_probe,
767 .remove = ucsi_stm32g0_remove,
768 .id_table = ucsi_stm32g0_typec_i2c_devid
770 module_i2c_driver(ucsi_stm32g0_i2c_driver);
772 MODULE_AUTHOR("Fabrice Gasnier <fabrice.gasnier@foss.st.com>");
773 MODULE_DESCRIPTION("STMicroelectronics STM32G0 Type-C controller");
774 MODULE_LICENSE("Dual BSD/GPL");
775 MODULE_ALIAS("platform:ucsi-stm32g0");