1 // SPDX-License-Identifier: GPL-2.0-only
3 * datasheet: https://www.nxp.com/docs/en/data-sheet/K20P144M120SF3.pdf
5 * Copyright (C) 2018-2021 Collabora
6 * Copyright (C) 2018-2021 GE Healthcare
9 #include <linux/delay.h>
10 #include <linux/firmware.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
15 #include <linux/spi/spi.h>
17 #define ACHC_MAX_FREQ_HZ 300000
18 #define ACHC_FAST_READ_FREQ_HZ 1000000
21 struct spi_device *main;
22 struct spi_device *ezport;
23 struct gpio_desc *reset;
25 struct mutex device_lock; /* avoid concurrent device access */
28 #define EZPORT_RESET_DELAY_MS 100
29 #define EZPORT_STARTUP_DELAY_MS 200
30 #define EZPORT_WRITE_WAIT_MS 10
31 #define EZPORT_TRANSFER_SIZE 2048
33 #define EZPORT_CMD_SP 0x02 /* flash section program */
34 #define EZPORT_CMD_RDSR 0x05 /* read status register */
35 #define EZPORT_CMD_WREN 0x06 /* write enable */
36 #define EZPORT_CMD_FAST_READ 0x0b /* flash read data at high speed */
37 #define EZPORT_CMD_RESET 0xb9 /* reset chip */
38 #define EZPORT_CMD_BE 0xc7 /* bulk erase */
39 #define EZPORT_CMD_SE 0xd8 /* sector erase */
41 #define EZPORT_SECTOR_SIZE 4096
42 #define EZPORT_SECTOR_MASK (EZPORT_SECTOR_SIZE - 1)
44 #define EZPORT_STATUS_WIP BIT(0) /* write in progress */
45 #define EZPORT_STATUS_WEN BIT(1) /* write enable */
46 #define EZPORT_STATUS_BEDIS BIT(2) /* bulk erase disable */
47 #define EZPORT_STATUS_FLEXRAM BIT(3) /* FlexRAM mode */
48 #define EZPORT_STATUS_WEF BIT(6) /* write error flag */
49 #define EZPORT_STATUS_FS BIT(7) /* flash security */
51 static void ezport_reset(struct gpio_desc *reset)
53 gpiod_set_value(reset, 1);
54 msleep(EZPORT_RESET_DELAY_MS);
55 gpiod_set_value(reset, 0);
56 msleep(EZPORT_STARTUP_DELAY_MS);
59 static int ezport_start_programming(struct spi_device *spi, struct gpio_desc *reset)
61 struct spi_message msg;
62 struct spi_transfer assert_cs = {
65 struct spi_transfer release_cs = { };
68 spi_bus_lock(spi->master);
70 /* assert chip select */
71 spi_message_init(&msg);
72 spi_message_add_tail(&assert_cs, &msg);
73 ret = spi_sync_locked(spi, &msg);
77 msleep(EZPORT_STARTUP_DELAY_MS);
79 /* reset with asserted chip select to switch into programming mode */
82 /* release chip select */
83 spi_message_init(&msg);
84 spi_message_add_tail(&release_cs, &msg);
85 ret = spi_sync_locked(spi, &msg);
88 spi_bus_unlock(spi->master);
92 static void ezport_stop_programming(struct spi_device *spi, struct gpio_desc *reset)
94 /* reset without asserted chip select to return into normal mode */
95 spi_bus_lock(spi->master);
97 spi_bus_unlock(spi->master);
100 static int ezport_get_status_register(struct spi_device *spi)
104 ret = spi_w8r8(spi, EZPORT_CMD_RDSR);
108 dev_err(&spi->dev, "Invalid EzPort status, EzPort is not functional!\n");
115 static int ezport_soft_reset(struct spi_device *spi)
117 u8 cmd = EZPORT_CMD_RESET;
120 ret = spi_write(spi, &cmd, 1);
124 msleep(EZPORT_STARTUP_DELAY_MS);
129 static int ezport_send_simple(struct spi_device *spi, u8 cmd)
133 ret = spi_write(spi, &cmd, 1);
137 return ezport_get_status_register(spi);
140 static int ezport_wait_write(struct spi_device *spi, u32 retries)
145 for (i = 0; i < retries; i++) {
146 ret = ezport_get_status_register(spi);
147 if (ret >= 0 && !(ret & EZPORT_STATUS_WIP))
149 msleep(EZPORT_WRITE_WAIT_MS);
155 static int ezport_write_enable(struct spi_device *spi)
157 int ret = 0, retries = 3;
159 for (retries = 0; retries < 3; retries++) {
160 ret = ezport_send_simple(spi, EZPORT_CMD_WREN);
161 if (ret > 0 && ret & EZPORT_STATUS_WEN)
165 if (!(ret & EZPORT_STATUS_WEN)) {
166 dev_err(&spi->dev, "EzPort write enable timed out\n");
172 static int ezport_bulk_erase(struct spi_device *spi)
175 static const u8 cmd = EZPORT_CMD_BE;
177 dev_dbg(&spi->dev, "EzPort bulk erase...\n");
179 ret = ezport_write_enable(spi);
183 ret = spi_write(spi, &cmd, 1);
187 ret = ezport_wait_write(spi, 1000);
194 static int ezport_section_erase(struct spi_device *spi, u32 address)
196 u8 query[] = {EZPORT_CMD_SE, (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff};
199 dev_dbg(&spi->dev, "Ezport section erase @ 0x%06x...\n", address);
201 if (address & EZPORT_SECTOR_MASK)
204 ret = ezport_write_enable(spi);
208 ret = spi_write(spi, query, sizeof(query));
212 return ezport_wait_write(spi, 200);
215 static int ezport_flash_transfer(struct spi_device *spi, u32 address,
216 const u8 *payload, size_t payload_size)
218 struct spi_transfer xfers[2] = {};
222 dev_dbg(&spi->dev, "EzPort write %zu bytes @ 0x%06x...\n", payload_size, address);
224 ret = ezport_write_enable(spi);
228 command = kmalloc(4, GFP_KERNEL | GFP_DMA);
232 command[0] = EZPORT_CMD_SP;
233 command[1] = address >> 16;
234 command[2] = address >> 8;
235 command[3] = address >> 0;
237 xfers[0].tx_buf = command;
240 xfers[1].tx_buf = payload;
241 xfers[1].len = payload_size;
243 ret = spi_sync_transfer(spi, xfers, 2);
248 return ezport_wait_write(spi, 40);
251 static int ezport_flash_compare(struct spi_device *spi, u32 address,
252 const u8 *payload, size_t payload_size)
254 struct spi_transfer xfers[2] = {};
258 buffer = kmalloc(payload_size + 5, GFP_KERNEL | GFP_DMA);
262 buffer[0] = EZPORT_CMD_FAST_READ;
263 buffer[1] = address >> 16;
264 buffer[2] = address >> 8;
265 buffer[3] = address >> 0;
267 xfers[0].tx_buf = buffer;
269 xfers[0].speed_hz = ACHC_FAST_READ_FREQ_HZ;
271 xfers[1].rx_buf = buffer + 4;
272 xfers[1].len = payload_size + 1;
273 xfers[1].speed_hz = ACHC_FAST_READ_FREQ_HZ;
275 ret = spi_sync_transfer(spi, xfers, 2);
279 /* FAST_READ receives one dummy byte before the real data */
280 ret = memcmp(payload, buffer + 4 + 1, payload_size);
283 dev_dbg(&spi->dev, "Verification failure @ %06x", address);
284 print_hex_dump_bytes("fw: ", DUMP_PREFIX_OFFSET, payload, payload_size);
285 print_hex_dump_bytes("dev: ", DUMP_PREFIX_OFFSET, buffer + 4, payload_size);
293 static int ezport_firmware_compare_data(struct spi_device *spi,
294 const u8 *data, size_t size)
298 size_t transfer_size;
300 dev_dbg(&spi->dev, "EzPort compare data with %zu bytes...\n", size);
302 ret = ezport_get_status_register(spi);
306 if (ret & EZPORT_STATUS_FS) {
307 dev_info(&spi->dev, "Device is in secure mode (status=0x%02x)!\n", ret);
308 dev_info(&spi->dev, "FW verification is not possible\n");
312 while (size - address > 0) {
313 transfer_size = min((size_t) EZPORT_TRANSFER_SIZE, size - address);
315 ret = ezport_flash_compare(spi, address, data+address, transfer_size);
319 address += transfer_size;
325 static int ezport_firmware_flash_data(struct spi_device *spi,
326 const u8 *data, size_t size)
330 size_t transfer_size;
332 dev_dbg(&spi->dev, "EzPort flash data with %zu bytes...\n", size);
334 ret = ezport_get_status_register(spi);
338 if (ret & EZPORT_STATUS_FS) {
339 ret = ezport_bulk_erase(spi);
342 if (ret & EZPORT_STATUS_FS)
346 while (size - address > 0) {
347 if (!(address & EZPORT_SECTOR_MASK)) {
348 ret = ezport_section_erase(spi, address);
351 if (ret & EZPORT_STATUS_WIP || ret & EZPORT_STATUS_WEF)
355 transfer_size = min((size_t) EZPORT_TRANSFER_SIZE, size - address);
357 ret = ezport_flash_transfer(spi, address,
358 data+address, transfer_size);
361 else if (ret & EZPORT_STATUS_WIP)
363 else if (ret & EZPORT_STATUS_WEF)
366 address += transfer_size;
369 dev_dbg(&spi->dev, "EzPort verify flashed data...\n");
370 ret = ezport_firmware_compare_data(spi, data, size);
372 /* allow missing FW verfication in secure mode */
377 dev_err(&spi->dev, "Failed to verify flashed data: %d\n", ret);
379 ret = ezport_soft_reset(spi);
381 dev_warn(&spi->dev, "EzPort reset failed!\n");
386 static int ezport_firmware_load(struct spi_device *spi, const char *fwname)
388 const struct firmware *fw;
391 ret = request_firmware(&fw, fwname, &spi->dev);
393 dev_err(&spi->dev, "Could not get firmware: %d\n", ret);
397 ret = ezport_firmware_flash_data(spi, fw->data, fw->size);
399 release_firmware(fw);
405 * ezport_flash - flash device firmware
406 * @spi: SPI device for NXP EzPort interface
407 * @reset: the gpio connected to the device reset pin
408 * @fwname: filename of the firmware that should be flashed
412 * Return: 0 on success; negative errno on failure
414 static int ezport_flash(struct spi_device *spi, struct gpio_desc *reset, const char *fwname)
418 ret = ezport_start_programming(spi, reset);
422 ret = ezport_firmware_load(spi, fwname);
424 ezport_stop_programming(spi, reset);
427 dev_err(&spi->dev, "Failed to flash firmware: %d\n", ret);
429 dev_dbg(&spi->dev, "Finished FW flashing!\n");
434 static ssize_t update_firmware_store(struct device *dev, struct device_attribute *attr,
435 const char *buf, size_t count)
437 struct achc_data *achc = dev_get_drvdata(dev);
441 ret = kstrtoul(buf, 0, &value);
442 if (ret < 0 || value != 1)
445 mutex_lock(&achc->device_lock);
446 ret = ezport_flash(achc->ezport, achc->reset, "achc.bin");
447 mutex_unlock(&achc->device_lock);
454 static DEVICE_ATTR_WO(update_firmware);
456 static ssize_t reset_show(struct device *dev, struct device_attribute *attr, char *buf)
458 struct achc_data *achc = dev_get_drvdata(dev);
461 mutex_lock(&achc->device_lock);
462 ret = gpiod_get_value(achc->reset);
463 mutex_unlock(&achc->device_lock);
468 return sysfs_emit(buf, "%d\n", ret);
471 static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
472 const char *buf, size_t count)
474 struct achc_data *achc = dev_get_drvdata(dev);
478 ret = kstrtoul(buf, 0, &value);
479 if (ret < 0 || value > 1)
482 mutex_lock(&achc->device_lock);
483 gpiod_set_value(achc->reset, value);
484 mutex_unlock(&achc->device_lock);
488 static DEVICE_ATTR_RW(reset);
490 static struct attribute *gehc_achc_attrs[] = {
491 &dev_attr_update_firmware.attr,
492 &dev_attr_reset.attr,
495 ATTRIBUTE_GROUPS(gehc_achc);
497 static void unregister_ezport(void *data)
499 struct spi_device *ezport = data;
501 spi_unregister_device(ezport);
504 static int gehc_achc_probe(struct spi_device *spi)
506 struct achc_data *achc;
509 spi->max_speed_hz = ACHC_MAX_FREQ_HZ;
510 spi->bits_per_word = 8;
511 spi->mode = SPI_MODE_0;
513 achc = devm_kzalloc(&spi->dev, sizeof(*achc), GFP_KERNEL);
516 spi_set_drvdata(spi, achc);
519 mutex_init(&achc->device_lock);
521 ret = of_property_read_u32_index(spi->dev.of_node, "reg", 1, &ezport_reg);
523 return dev_err_probe(&spi->dev, ret, "missing second reg entry!\n");
525 achc->ezport = spi_new_ancillary_device(spi, ezport_reg);
526 if (IS_ERR(achc->ezport))
527 return PTR_ERR(achc->ezport);
529 ret = devm_add_action_or_reset(&spi->dev, unregister_ezport, achc->ezport);
533 achc->reset = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_LOW);
534 if (IS_ERR(achc->reset))
535 return dev_err_probe(&spi->dev, PTR_ERR(achc->reset), "Could not get reset gpio\n");
540 static const struct spi_device_id gehc_achc_id[] = {
545 MODULE_DEVICE_TABLE(spi, gehc_achc_id);
547 static const struct of_device_id gehc_achc_of_match[] = {
548 { .compatible = "ge,achc" },
551 MODULE_DEVICE_TABLE(of, gehc_achc_of_match);
553 static struct spi_driver gehc_achc_spi_driver = {
556 .of_match_table = gehc_achc_of_match,
557 .dev_groups = gehc_achc_groups,
559 .probe = gehc_achc_probe,
560 .id_table = gehc_achc_id,
562 module_spi_driver(gehc_achc_spi_driver);
564 MODULE_DESCRIPTION("GEHC ACHC driver");
565 MODULE_AUTHOR("Sebastian Reichel <sebastian.reichel@collabora.com>");
566 MODULE_LICENSE("GPL");