xref: /openbmc/linux/drivers/misc/gehc-achc.c (revision a3e16937)
10f920277SSebastian Reichel // SPDX-License-Identifier: GPL-2.0-only
20f920277SSebastian Reichel /*
30f920277SSebastian Reichel  * datasheet: https://www.nxp.com/docs/en/data-sheet/K20P144M120SF3.pdf
40f920277SSebastian Reichel  *
50f920277SSebastian Reichel  * Copyright (C) 2018-2021 Collabora
60f920277SSebastian Reichel  * Copyright (C) 2018-2021 GE Healthcare
70f920277SSebastian Reichel  */
80f920277SSebastian Reichel 
90f920277SSebastian Reichel #include <linux/delay.h>
100f920277SSebastian Reichel #include <linux/firmware.h>
110f920277SSebastian Reichel #include <linux/gpio/consumer.h>
120f920277SSebastian Reichel #include <linux/kernel.h>
130f920277SSebastian Reichel #include <linux/module.h>
140f920277SSebastian Reichel #include <linux/of.h>
150f920277SSebastian Reichel #include <linux/spi/spi.h>
160f920277SSebastian Reichel 
170f920277SSebastian Reichel #define ACHC_MAX_FREQ_HZ 300000
180f920277SSebastian Reichel #define ACHC_FAST_READ_FREQ_HZ 1000000
190f920277SSebastian Reichel 
200f920277SSebastian Reichel struct achc_data {
210f920277SSebastian Reichel 	struct spi_device *main;
220f920277SSebastian Reichel 	struct spi_device *ezport;
230f920277SSebastian Reichel 	struct gpio_desc *reset;
240f920277SSebastian Reichel 
250f920277SSebastian Reichel 	struct mutex device_lock; /* avoid concurrent device access */
260f920277SSebastian Reichel };
270f920277SSebastian Reichel 
280f920277SSebastian Reichel #define EZPORT_RESET_DELAY_MS	100
290f920277SSebastian Reichel #define EZPORT_STARTUP_DELAY_MS	200
300f920277SSebastian Reichel #define EZPORT_WRITE_WAIT_MS	10
310f920277SSebastian Reichel #define EZPORT_TRANSFER_SIZE	2048
320f920277SSebastian Reichel 
330f920277SSebastian Reichel #define EZPORT_CMD_SP		0x02 /* flash section program */
340f920277SSebastian Reichel #define EZPORT_CMD_RDSR		0x05 /* read status register */
350f920277SSebastian Reichel #define EZPORT_CMD_WREN		0x06 /* write enable */
360f920277SSebastian Reichel #define EZPORT_CMD_FAST_READ	0x0b /* flash read data at high speed */
370f920277SSebastian Reichel #define EZPORT_CMD_RESET	0xb9 /* reset chip */
380f920277SSebastian Reichel #define EZPORT_CMD_BE		0xc7 /* bulk erase */
390f920277SSebastian Reichel #define EZPORT_CMD_SE		0xd8 /* sector erase */
400f920277SSebastian Reichel 
410f920277SSebastian Reichel #define EZPORT_SECTOR_SIZE	4096
420f920277SSebastian Reichel #define EZPORT_SECTOR_MASK	(EZPORT_SECTOR_SIZE - 1)
430f920277SSebastian Reichel 
440f920277SSebastian Reichel #define EZPORT_STATUS_WIP	BIT(0) /* write in progress */
450f920277SSebastian Reichel #define EZPORT_STATUS_WEN	BIT(1) /* write enable */
460f920277SSebastian Reichel #define EZPORT_STATUS_BEDIS	BIT(2) /* bulk erase disable */
470f920277SSebastian Reichel #define EZPORT_STATUS_FLEXRAM	BIT(3) /* FlexRAM mode */
480f920277SSebastian Reichel #define EZPORT_STATUS_WEF	BIT(6) /* write error flag */
490f920277SSebastian Reichel #define EZPORT_STATUS_FS	BIT(7) /* flash security */
500f920277SSebastian Reichel 
ezport_reset(struct gpio_desc * reset)510f920277SSebastian Reichel static void ezport_reset(struct gpio_desc *reset)
520f920277SSebastian Reichel {
530f920277SSebastian Reichel 	gpiod_set_value(reset, 1);
540f920277SSebastian Reichel 	msleep(EZPORT_RESET_DELAY_MS);
550f920277SSebastian Reichel 	gpiod_set_value(reset, 0);
560f920277SSebastian Reichel 	msleep(EZPORT_STARTUP_DELAY_MS);
570f920277SSebastian Reichel }
580f920277SSebastian Reichel 
ezport_start_programming(struct spi_device * spi,struct gpio_desc * reset)590f920277SSebastian Reichel static int ezport_start_programming(struct spi_device *spi, struct gpio_desc *reset)
600f920277SSebastian Reichel {
610f920277SSebastian Reichel 	struct spi_message msg;
620f920277SSebastian Reichel 	struct spi_transfer assert_cs = {
630f920277SSebastian Reichel 		.cs_change   = 1,
640f920277SSebastian Reichel 	};
650f920277SSebastian Reichel 	struct spi_transfer release_cs = { };
660f920277SSebastian Reichel 	int ret;
670f920277SSebastian Reichel 
680f920277SSebastian Reichel 	spi_bus_lock(spi->master);
690f920277SSebastian Reichel 
700f920277SSebastian Reichel 	/* assert chip select */
710f920277SSebastian Reichel 	spi_message_init(&msg);
720f920277SSebastian Reichel 	spi_message_add_tail(&assert_cs, &msg);
730f920277SSebastian Reichel 	ret = spi_sync_locked(spi, &msg);
740f920277SSebastian Reichel 	if (ret)
750f920277SSebastian Reichel 		goto fail;
760f920277SSebastian Reichel 
770f920277SSebastian Reichel 	msleep(EZPORT_STARTUP_DELAY_MS);
780f920277SSebastian Reichel 
790f920277SSebastian Reichel 	/* reset with asserted chip select to switch into programming mode */
800f920277SSebastian Reichel 	ezport_reset(reset);
810f920277SSebastian Reichel 
820f920277SSebastian Reichel 	/* release chip select */
830f920277SSebastian Reichel 	spi_message_init(&msg);
840f920277SSebastian Reichel 	spi_message_add_tail(&release_cs, &msg);
850f920277SSebastian Reichel 	ret = spi_sync_locked(spi, &msg);
860f920277SSebastian Reichel 
870f920277SSebastian Reichel fail:
880f920277SSebastian Reichel 	spi_bus_unlock(spi->master);
890f920277SSebastian Reichel 	return ret;
900f920277SSebastian Reichel }
910f920277SSebastian Reichel 
ezport_stop_programming(struct spi_device * spi,struct gpio_desc * reset)920f920277SSebastian Reichel static void ezport_stop_programming(struct spi_device *spi, struct gpio_desc *reset)
930f920277SSebastian Reichel {
940f920277SSebastian Reichel 	/* reset without asserted chip select to return into normal mode */
950f920277SSebastian Reichel 	spi_bus_lock(spi->master);
960f920277SSebastian Reichel 	ezport_reset(reset);
970f920277SSebastian Reichel 	spi_bus_unlock(spi->master);
980f920277SSebastian Reichel }
990f920277SSebastian Reichel 
ezport_get_status_register(struct spi_device * spi)1000f920277SSebastian Reichel static int ezport_get_status_register(struct spi_device *spi)
1010f920277SSebastian Reichel {
1020f920277SSebastian Reichel 	int ret;
1030f920277SSebastian Reichel 
1040f920277SSebastian Reichel 	ret = spi_w8r8(spi, EZPORT_CMD_RDSR);
1050f920277SSebastian Reichel 	if (ret < 0)
1060f920277SSebastian Reichel 		return ret;
1070f920277SSebastian Reichel 	if (ret == 0xff) {
1080f920277SSebastian Reichel 		dev_err(&spi->dev, "Invalid EzPort status, EzPort is not functional!\n");
1090f920277SSebastian Reichel 		return -EINVAL;
1100f920277SSebastian Reichel 	}
1110f920277SSebastian Reichel 
1120f920277SSebastian Reichel 	return ret;
1130f920277SSebastian Reichel }
1140f920277SSebastian Reichel 
ezport_soft_reset(struct spi_device * spi)1150f920277SSebastian Reichel static int ezport_soft_reset(struct spi_device *spi)
1160f920277SSebastian Reichel {
1170f920277SSebastian Reichel 	u8 cmd = EZPORT_CMD_RESET;
1180f920277SSebastian Reichel 	int ret;
1190f920277SSebastian Reichel 
1200f920277SSebastian Reichel 	ret = spi_write(spi, &cmd, 1);
1210f920277SSebastian Reichel 	if (ret < 0)
1220f920277SSebastian Reichel 		return ret;
1230f920277SSebastian Reichel 
1240f920277SSebastian Reichel 	msleep(EZPORT_STARTUP_DELAY_MS);
1250f920277SSebastian Reichel 
1260f920277SSebastian Reichel 	return 0;
1270f920277SSebastian Reichel }
1280f920277SSebastian Reichel 
ezport_send_simple(struct spi_device * spi,u8 cmd)1290f920277SSebastian Reichel static int ezport_send_simple(struct spi_device *spi, u8 cmd)
1300f920277SSebastian Reichel {
1310f920277SSebastian Reichel 	int ret;
1320f920277SSebastian Reichel 
1330f920277SSebastian Reichel 	ret = spi_write(spi, &cmd, 1);
1340f920277SSebastian Reichel 	if (ret < 0)
1350f920277SSebastian Reichel 		return ret;
1360f920277SSebastian Reichel 
1370f920277SSebastian Reichel 	return ezport_get_status_register(spi);
1380f920277SSebastian Reichel }
1390f920277SSebastian Reichel 
ezport_wait_write(struct spi_device * spi,u32 retries)1400f920277SSebastian Reichel static int ezport_wait_write(struct spi_device *spi, u32 retries)
1410f920277SSebastian Reichel {
1420f920277SSebastian Reichel 	int ret;
1430f920277SSebastian Reichel 	u32 i;
1440f920277SSebastian Reichel 
1450f920277SSebastian Reichel 	for (i = 0; i < retries; i++) {
1460f920277SSebastian Reichel 		ret = ezport_get_status_register(spi);
1470f920277SSebastian Reichel 		if (ret >= 0 && !(ret & EZPORT_STATUS_WIP))
1480f920277SSebastian Reichel 			break;
1490f920277SSebastian Reichel 		msleep(EZPORT_WRITE_WAIT_MS);
1500f920277SSebastian Reichel 	}
1510f920277SSebastian Reichel 
1520f920277SSebastian Reichel 	return ret;
1530f920277SSebastian Reichel }
1540f920277SSebastian Reichel 
ezport_write_enable(struct spi_device * spi)1550f920277SSebastian Reichel static int ezport_write_enable(struct spi_device *spi)
1560f920277SSebastian Reichel {
1570f920277SSebastian Reichel 	int ret = 0, retries = 3;
1580f920277SSebastian Reichel 
1590f920277SSebastian Reichel 	for (retries = 0; retries < 3; retries++) {
1600f920277SSebastian Reichel 		ret = ezport_send_simple(spi, EZPORT_CMD_WREN);
1610f920277SSebastian Reichel 		if (ret > 0 && ret & EZPORT_STATUS_WEN)
1620f920277SSebastian Reichel 			break;
1630f920277SSebastian Reichel 	}
1640f920277SSebastian Reichel 
1650f920277SSebastian Reichel 	if (!(ret & EZPORT_STATUS_WEN)) {
1660f920277SSebastian Reichel 		dev_err(&spi->dev, "EzPort write enable timed out\n");
1670f920277SSebastian Reichel 		return -ETIMEDOUT;
1680f920277SSebastian Reichel 	}
1690f920277SSebastian Reichel 	return 0;
1700f920277SSebastian Reichel }
1710f920277SSebastian Reichel 
ezport_bulk_erase(struct spi_device * spi)1720f920277SSebastian Reichel static int ezport_bulk_erase(struct spi_device *spi)
1730f920277SSebastian Reichel {
1740f920277SSebastian Reichel 	int ret;
1750f920277SSebastian Reichel 	static const u8 cmd = EZPORT_CMD_BE;
1760f920277SSebastian Reichel 
1770f920277SSebastian Reichel 	dev_dbg(&spi->dev, "EzPort bulk erase...\n");
1780f920277SSebastian Reichel 
1790f920277SSebastian Reichel 	ret = ezport_write_enable(spi);
1800f920277SSebastian Reichel 	if (ret < 0)
1810f920277SSebastian Reichel 		return ret;
1820f920277SSebastian Reichel 
1830f920277SSebastian Reichel 	ret = spi_write(spi, &cmd, 1);
1840f920277SSebastian Reichel 	if (ret < 0)
1850f920277SSebastian Reichel 		return ret;
1860f920277SSebastian Reichel 
1870f920277SSebastian Reichel 	ret = ezport_wait_write(spi, 1000);
1880f920277SSebastian Reichel 	if (ret < 0)
1890f920277SSebastian Reichel 		return ret;
1900f920277SSebastian Reichel 
1910f920277SSebastian Reichel 	return 0;
1920f920277SSebastian Reichel }
1930f920277SSebastian Reichel 
ezport_section_erase(struct spi_device * spi,u32 address)1940f920277SSebastian Reichel static int ezport_section_erase(struct spi_device *spi, u32 address)
1950f920277SSebastian Reichel {
1960f920277SSebastian Reichel 	u8 query[] = {EZPORT_CMD_SE, (address >> 16) & 0xff, (address >> 8) & 0xff, address & 0xff};
1970f920277SSebastian Reichel 	int ret;
1980f920277SSebastian Reichel 
1990f920277SSebastian Reichel 	dev_dbg(&spi->dev, "Ezport section erase @ 0x%06x...\n", address);
2000f920277SSebastian Reichel 
2010f920277SSebastian Reichel 	if (address & EZPORT_SECTOR_MASK)
2020f920277SSebastian Reichel 		return -EINVAL;
2030f920277SSebastian Reichel 
2040f920277SSebastian Reichel 	ret = ezport_write_enable(spi);
2050f920277SSebastian Reichel 	if (ret < 0)
2060f920277SSebastian Reichel 		return ret;
2070f920277SSebastian Reichel 
2080f920277SSebastian Reichel 	ret = spi_write(spi, query, sizeof(query));
2090f920277SSebastian Reichel 	if (ret < 0)
2100f920277SSebastian Reichel 		return ret;
2110f920277SSebastian Reichel 
2120f920277SSebastian Reichel 	return ezport_wait_write(spi, 200);
2130f920277SSebastian Reichel }
2140f920277SSebastian Reichel 
ezport_flash_transfer(struct spi_device * spi,u32 address,const u8 * payload,size_t payload_size)2150f920277SSebastian Reichel static int ezport_flash_transfer(struct spi_device *spi, u32 address,
2160f920277SSebastian Reichel 				 const u8 *payload, size_t payload_size)
2170f920277SSebastian Reichel {
2180f920277SSebastian Reichel 	struct spi_transfer xfers[2] = {};
2190f920277SSebastian Reichel 	u8 *command;
2200f920277SSebastian Reichel 	int ret;
2210f920277SSebastian Reichel 
2220f920277SSebastian Reichel 	dev_dbg(&spi->dev, "EzPort write %zu bytes @ 0x%06x...\n", payload_size, address);
2230f920277SSebastian Reichel 
2240f920277SSebastian Reichel 	ret = ezport_write_enable(spi);
2250f920277SSebastian Reichel 	if (ret < 0)
2260f920277SSebastian Reichel 		return ret;
2270f920277SSebastian Reichel 
2280f920277SSebastian Reichel 	command = kmalloc(4, GFP_KERNEL | GFP_DMA);
2290f920277SSebastian Reichel 	if (!command)
2300f920277SSebastian Reichel 		return -ENOMEM;
2310f920277SSebastian Reichel 
2320f920277SSebastian Reichel 	command[0] = EZPORT_CMD_SP;
2330f920277SSebastian Reichel 	command[1] = address >> 16;
2340f920277SSebastian Reichel 	command[2] = address >> 8;
2350f920277SSebastian Reichel 	command[3] = address >> 0;
2360f920277SSebastian Reichel 
2370f920277SSebastian Reichel 	xfers[0].tx_buf = command;
2380f920277SSebastian Reichel 	xfers[0].len = 4;
2390f920277SSebastian Reichel 
2400f920277SSebastian Reichel 	xfers[1].tx_buf = payload;
2410f920277SSebastian Reichel 	xfers[1].len = payload_size;
2420f920277SSebastian Reichel 
2430f920277SSebastian Reichel 	ret = spi_sync_transfer(spi, xfers, 2);
2440f920277SSebastian Reichel 	kfree(command);
2450f920277SSebastian Reichel 	if (ret < 0)
2460f920277SSebastian Reichel 		return ret;
2470f920277SSebastian Reichel 
2480f920277SSebastian Reichel 	return ezport_wait_write(spi, 40);
2490f920277SSebastian Reichel }
2500f920277SSebastian Reichel 
ezport_flash_compare(struct spi_device * spi,u32 address,const u8 * payload,size_t payload_size)2510f920277SSebastian Reichel static int ezport_flash_compare(struct spi_device *spi, u32 address,
2520f920277SSebastian Reichel 				const u8 *payload, size_t payload_size)
2530f920277SSebastian Reichel {
2540f920277SSebastian Reichel 	struct spi_transfer xfers[2] = {};
2550f920277SSebastian Reichel 	u8 *buffer;
2560f920277SSebastian Reichel 	int ret;
2570f920277SSebastian Reichel 
2580f920277SSebastian Reichel 	buffer = kmalloc(payload_size + 5, GFP_KERNEL | GFP_DMA);
2590f920277SSebastian Reichel 	if (!buffer)
2600f920277SSebastian Reichel 		return -ENOMEM;
2610f920277SSebastian Reichel 
2620f920277SSebastian Reichel 	buffer[0] = EZPORT_CMD_FAST_READ;
2630f920277SSebastian Reichel 	buffer[1] = address >> 16;
2640f920277SSebastian Reichel 	buffer[2] = address >> 8;
2650f920277SSebastian Reichel 	buffer[3] = address >> 0;
2660f920277SSebastian Reichel 
2670f920277SSebastian Reichel 	xfers[0].tx_buf = buffer;
2680f920277SSebastian Reichel 	xfers[0].len = 4;
2690f920277SSebastian Reichel 	xfers[0].speed_hz = ACHC_FAST_READ_FREQ_HZ;
2700f920277SSebastian Reichel 
2710f920277SSebastian Reichel 	xfers[1].rx_buf = buffer + 4;
2720f920277SSebastian Reichel 	xfers[1].len = payload_size + 1;
2730f920277SSebastian Reichel 	xfers[1].speed_hz = ACHC_FAST_READ_FREQ_HZ;
2740f920277SSebastian Reichel 
2750f920277SSebastian Reichel 	ret = spi_sync_transfer(spi, xfers, 2);
2760f920277SSebastian Reichel 	if (ret)
2770f920277SSebastian Reichel 		goto err;
2780f920277SSebastian Reichel 
2790f920277SSebastian Reichel 	/* FAST_READ receives one dummy byte before the real data */
2800f920277SSebastian Reichel 	ret = memcmp(payload, buffer + 4 + 1, payload_size);
2810f920277SSebastian Reichel 	if (ret) {
2820f920277SSebastian Reichel 		ret = -EBADMSG;
28316af5357SColin Ian King 		dev_dbg(&spi->dev, "Verification failure @ %06x", address);
2840f920277SSebastian Reichel 		print_hex_dump_bytes("fw:  ", DUMP_PREFIX_OFFSET, payload, payload_size);
2850f920277SSebastian Reichel 		print_hex_dump_bytes("dev: ", DUMP_PREFIX_OFFSET, buffer + 4, payload_size);
2860f920277SSebastian Reichel 	}
2870f920277SSebastian Reichel 
2880f920277SSebastian Reichel err:
2890f920277SSebastian Reichel 	kfree(buffer);
2900f920277SSebastian Reichel 	return ret;
2910f920277SSebastian Reichel }
2920f920277SSebastian Reichel 
ezport_firmware_compare_data(struct spi_device * spi,const u8 * data,size_t size)2930f920277SSebastian Reichel static int ezport_firmware_compare_data(struct spi_device *spi,
2940f920277SSebastian Reichel 					const u8 *data, size_t size)
2950f920277SSebastian Reichel {
2960f920277SSebastian Reichel 	int ret;
2970f920277SSebastian Reichel 	size_t address = 0;
2980f920277SSebastian Reichel 	size_t transfer_size;
2990f920277SSebastian Reichel 
3000f920277SSebastian Reichel 	dev_dbg(&spi->dev, "EzPort compare data with %zu bytes...\n", size);
3010f920277SSebastian Reichel 
3020f920277SSebastian Reichel 	ret = ezport_get_status_register(spi);
3030f920277SSebastian Reichel 	if (ret < 0)
3040f920277SSebastian Reichel 		return ret;
3050f920277SSebastian Reichel 
3060f920277SSebastian Reichel 	if (ret & EZPORT_STATUS_FS) {
3070f920277SSebastian Reichel 		dev_info(&spi->dev, "Device is in secure mode (status=0x%02x)!\n", ret);
3080f920277SSebastian Reichel 		dev_info(&spi->dev, "FW verification is not possible\n");
3090f920277SSebastian Reichel 		return -EACCES;
3100f920277SSebastian Reichel 	}
3110f920277SSebastian Reichel 
3120f920277SSebastian Reichel 	while (size - address > 0) {
3130f920277SSebastian Reichel 		transfer_size = min((size_t) EZPORT_TRANSFER_SIZE, size - address);
3140f920277SSebastian Reichel 
3150f920277SSebastian Reichel 		ret = ezport_flash_compare(spi, address, data+address, transfer_size);
3160f920277SSebastian Reichel 		if (ret)
3170f920277SSebastian Reichel 			return ret;
3180f920277SSebastian Reichel 
3190f920277SSebastian Reichel 		address += transfer_size;
3200f920277SSebastian Reichel 	}
3210f920277SSebastian Reichel 
3220f920277SSebastian Reichel 	return 0;
3230f920277SSebastian Reichel }
3240f920277SSebastian Reichel 
ezport_firmware_flash_data(struct spi_device * spi,const u8 * data,size_t size)3250f920277SSebastian Reichel static int ezport_firmware_flash_data(struct spi_device *spi,
3260f920277SSebastian Reichel 				      const u8 *data, size_t size)
3270f920277SSebastian Reichel {
3280f920277SSebastian Reichel 	int ret;
3290f920277SSebastian Reichel 	size_t address = 0;
3300f920277SSebastian Reichel 	size_t transfer_size;
3310f920277SSebastian Reichel 
3320f920277SSebastian Reichel 	dev_dbg(&spi->dev, "EzPort flash data with %zu bytes...\n", size);
3330f920277SSebastian Reichel 
3340f920277SSebastian Reichel 	ret = ezport_get_status_register(spi);
3350f920277SSebastian Reichel 	if (ret < 0)
3360f920277SSebastian Reichel 		return ret;
3370f920277SSebastian Reichel 
3380f920277SSebastian Reichel 	if (ret & EZPORT_STATUS_FS) {
3390f920277SSebastian Reichel 		ret = ezport_bulk_erase(spi);
3400f920277SSebastian Reichel 		if (ret < 0)
3410f920277SSebastian Reichel 			return ret;
3420f920277SSebastian Reichel 		if (ret & EZPORT_STATUS_FS)
3430f920277SSebastian Reichel 			return -EINVAL;
3440f920277SSebastian Reichel 	}
3450f920277SSebastian Reichel 
3460f920277SSebastian Reichel 	while (size - address > 0) {
3470f920277SSebastian Reichel 		if (!(address & EZPORT_SECTOR_MASK)) {
3480f920277SSebastian Reichel 			ret = ezport_section_erase(spi, address);
3490f920277SSebastian Reichel 			if (ret < 0)
3500f920277SSebastian Reichel 				return ret;
3510f920277SSebastian Reichel 			if (ret & EZPORT_STATUS_WIP || ret & EZPORT_STATUS_WEF)
3520f920277SSebastian Reichel 				return -EIO;
3530f920277SSebastian Reichel 		}
3540f920277SSebastian Reichel 
3550f920277SSebastian Reichel 		transfer_size = min((size_t) EZPORT_TRANSFER_SIZE, size - address);
3560f920277SSebastian Reichel 
3570f920277SSebastian Reichel 		ret = ezport_flash_transfer(spi, address,
3580f920277SSebastian Reichel 					    data+address, transfer_size);
3590f920277SSebastian Reichel 		if (ret < 0)
3600f920277SSebastian Reichel 			return ret;
3610f920277SSebastian Reichel 		else if (ret & EZPORT_STATUS_WIP)
3620f920277SSebastian Reichel 			return -ETIMEDOUT;
3630f920277SSebastian Reichel 		else if (ret & EZPORT_STATUS_WEF)
3640f920277SSebastian Reichel 			return -EIO;
3650f920277SSebastian Reichel 
3660f920277SSebastian Reichel 		address += transfer_size;
3670f920277SSebastian Reichel 	}
3680f920277SSebastian Reichel 
3690f920277SSebastian Reichel 	dev_dbg(&spi->dev, "EzPort verify flashed data...\n");
3700f920277SSebastian Reichel 	ret = ezport_firmware_compare_data(spi, data, size);
3710f920277SSebastian Reichel 
3720f920277SSebastian Reichel 	/* allow missing FW verfication in secure mode */
3730f920277SSebastian Reichel 	if (ret == -EACCES)
3740f920277SSebastian Reichel 		ret = 0;
3750f920277SSebastian Reichel 
3760f920277SSebastian Reichel 	if (ret < 0)
3770f920277SSebastian Reichel 		dev_err(&spi->dev, "Failed to verify flashed data: %d\n", ret);
3780f920277SSebastian Reichel 
3790f920277SSebastian Reichel 	ret = ezport_soft_reset(spi);
3800f920277SSebastian Reichel 	if (ret < 0)
3810f920277SSebastian Reichel 		dev_warn(&spi->dev, "EzPort reset failed!\n");
3820f920277SSebastian Reichel 
3830f920277SSebastian Reichel 	return ret;
3840f920277SSebastian Reichel }
3850f920277SSebastian Reichel 
ezport_firmware_load(struct spi_device * spi,const char * fwname)3860f920277SSebastian Reichel static int ezport_firmware_load(struct spi_device *spi, const char *fwname)
3870f920277SSebastian Reichel {
3880f920277SSebastian Reichel 	const struct firmware *fw;
3890f920277SSebastian Reichel 	int ret;
3900f920277SSebastian Reichel 
3910f920277SSebastian Reichel 	ret = request_firmware(&fw, fwname, &spi->dev);
3920f920277SSebastian Reichel 	if (ret) {
3930f920277SSebastian Reichel 		dev_err(&spi->dev, "Could not get firmware: %d\n", ret);
3940f920277SSebastian Reichel 		return ret;
3950f920277SSebastian Reichel 	}
3960f920277SSebastian Reichel 
3970f920277SSebastian Reichel 	ret = ezport_firmware_flash_data(spi, fw->data, fw->size);
3980f920277SSebastian Reichel 
3990f920277SSebastian Reichel 	release_firmware(fw);
4000f920277SSebastian Reichel 
4010f920277SSebastian Reichel 	return ret;
4020f920277SSebastian Reichel }
4030f920277SSebastian Reichel 
4040f920277SSebastian Reichel /**
4050f920277SSebastian Reichel  * ezport_flash - flash device firmware
4060f920277SSebastian Reichel  * @spi: SPI device for NXP EzPort interface
4070f920277SSebastian Reichel  * @reset: the gpio connected to the device reset pin
4080f920277SSebastian Reichel  * @fwname: filename of the firmware that should be flashed
4090f920277SSebastian Reichel  *
4100f920277SSebastian Reichel  * Context: can sleep
4110f920277SSebastian Reichel  *
4120f920277SSebastian Reichel  * Return: 0 on success; negative errno on failure
4130f920277SSebastian Reichel  */
ezport_flash(struct spi_device * spi,struct gpio_desc * reset,const char * fwname)4140f920277SSebastian Reichel static int ezport_flash(struct spi_device *spi, struct gpio_desc *reset, const char *fwname)
4150f920277SSebastian Reichel {
4160f920277SSebastian Reichel 	int ret;
4170f920277SSebastian Reichel 
4180f920277SSebastian Reichel 	ret = ezport_start_programming(spi, reset);
4190f920277SSebastian Reichel 	if (ret)
4200f920277SSebastian Reichel 		return ret;
4210f920277SSebastian Reichel 
4220f920277SSebastian Reichel 	ret = ezport_firmware_load(spi, fwname);
4230f920277SSebastian Reichel 
4240f920277SSebastian Reichel 	ezport_stop_programming(spi, reset);
4250f920277SSebastian Reichel 
4260f920277SSebastian Reichel 	if (ret)
4270f920277SSebastian Reichel 		dev_err(&spi->dev, "Failed to flash firmware: %d\n", ret);
4280f920277SSebastian Reichel 	else
4290f920277SSebastian Reichel 		dev_dbg(&spi->dev, "Finished FW flashing!\n");
4300f920277SSebastian Reichel 
4310f920277SSebastian Reichel 	return ret;
4320f920277SSebastian Reichel }
4330f920277SSebastian Reichel 
update_firmware_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4340f920277SSebastian Reichel static ssize_t update_firmware_store(struct device *dev, struct device_attribute *attr,
4350f920277SSebastian Reichel 				     const char *buf, size_t count)
4360f920277SSebastian Reichel {
4370f920277SSebastian Reichel 	struct achc_data *achc = dev_get_drvdata(dev);
4380f920277SSebastian Reichel 	unsigned long value;
4390f920277SSebastian Reichel 	int ret;
4400f920277SSebastian Reichel 
4410f920277SSebastian Reichel 	ret = kstrtoul(buf, 0, &value);
4420f920277SSebastian Reichel 	if (ret < 0 || value != 1)
4430f920277SSebastian Reichel 		return -EINVAL;
4440f920277SSebastian Reichel 
4450f920277SSebastian Reichel 	mutex_lock(&achc->device_lock);
4460f920277SSebastian Reichel 	ret = ezport_flash(achc->ezport, achc->reset, "achc.bin");
4470f920277SSebastian Reichel 	mutex_unlock(&achc->device_lock);
4480f920277SSebastian Reichel 
4490f920277SSebastian Reichel 	if (ret < 0)
4500f920277SSebastian Reichel 		return ret;
4510f920277SSebastian Reichel 
4520f920277SSebastian Reichel 	return count;
4530f920277SSebastian Reichel }
4540f920277SSebastian Reichel static DEVICE_ATTR_WO(update_firmware);
4550f920277SSebastian Reichel 
reset_show(struct device * dev,struct device_attribute * attr,char * buf)4560f920277SSebastian Reichel static ssize_t reset_show(struct device *dev, struct device_attribute *attr, char *buf)
4570f920277SSebastian Reichel {
4580f920277SSebastian Reichel 	struct achc_data *achc = dev_get_drvdata(dev);
4590f920277SSebastian Reichel 	int ret;
4600f920277SSebastian Reichel 
4610f920277SSebastian Reichel 	mutex_lock(&achc->device_lock);
4620f920277SSebastian Reichel 	ret = gpiod_get_value(achc->reset);
4630f920277SSebastian Reichel 	mutex_unlock(&achc->device_lock);
4640f920277SSebastian Reichel 
4650f920277SSebastian Reichel 	if (ret < 0)
4660f920277SSebastian Reichel 		return ret;
4670f920277SSebastian Reichel 
4680f920277SSebastian Reichel 	return sysfs_emit(buf, "%d\n", ret);
4690f920277SSebastian Reichel }
4700f920277SSebastian Reichel 
reset_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4710f920277SSebastian Reichel static ssize_t reset_store(struct device *dev, struct device_attribute *attr,
4720f920277SSebastian Reichel 			   const char *buf, size_t count)
4730f920277SSebastian Reichel {
4740f920277SSebastian Reichel 	struct achc_data *achc = dev_get_drvdata(dev);
4750f920277SSebastian Reichel 	unsigned long value;
4760f920277SSebastian Reichel 	int ret;
4770f920277SSebastian Reichel 
4780f920277SSebastian Reichel 	ret = kstrtoul(buf, 0, &value);
4790f920277SSebastian Reichel 	if (ret < 0 || value > 1)
4800f920277SSebastian Reichel 		return -EINVAL;
4810f920277SSebastian Reichel 
4820f920277SSebastian Reichel 	mutex_lock(&achc->device_lock);
4830f920277SSebastian Reichel 	gpiod_set_value(achc->reset, value);
4840f920277SSebastian Reichel 	mutex_unlock(&achc->device_lock);
4850f920277SSebastian Reichel 
4860f920277SSebastian Reichel 	return count;
4870f920277SSebastian Reichel }
4880f920277SSebastian Reichel static DEVICE_ATTR_RW(reset);
4890f920277SSebastian Reichel 
4900f920277SSebastian Reichel static struct attribute *gehc_achc_attrs[] = {
4910f920277SSebastian Reichel 	&dev_attr_update_firmware.attr,
4920f920277SSebastian Reichel 	&dev_attr_reset.attr,
4930f920277SSebastian Reichel 	NULL,
4940f920277SSebastian Reichel };
4950f920277SSebastian Reichel ATTRIBUTE_GROUPS(gehc_achc);
4960f920277SSebastian Reichel 
unregister_ezport(void * data)4970f920277SSebastian Reichel static void unregister_ezport(void *data)
4980f920277SSebastian Reichel {
4990f920277SSebastian Reichel 	struct spi_device *ezport = data;
5000f920277SSebastian Reichel 
5010f920277SSebastian Reichel 	spi_unregister_device(ezport);
5020f920277SSebastian Reichel }
5030f920277SSebastian Reichel 
gehc_achc_probe(struct spi_device * spi)5040f920277SSebastian Reichel static int gehc_achc_probe(struct spi_device *spi)
5050f920277SSebastian Reichel {
5060f920277SSebastian Reichel 	struct achc_data *achc;
5070f920277SSebastian Reichel 	int ezport_reg, ret;
5080f920277SSebastian Reichel 
5090f920277SSebastian Reichel 	spi->max_speed_hz = ACHC_MAX_FREQ_HZ;
5100f920277SSebastian Reichel 	spi->bits_per_word = 8;
5110f920277SSebastian Reichel 	spi->mode = SPI_MODE_0;
5120f920277SSebastian Reichel 
5130f920277SSebastian Reichel 	achc = devm_kzalloc(&spi->dev, sizeof(*achc), GFP_KERNEL);
5140f920277SSebastian Reichel 	if (!achc)
5150f920277SSebastian Reichel 		return -ENOMEM;
5160f920277SSebastian Reichel 	spi_set_drvdata(spi, achc);
5170f920277SSebastian Reichel 	achc->main = spi;
5180f920277SSebastian Reichel 
5190f920277SSebastian Reichel 	mutex_init(&achc->device_lock);
5200f920277SSebastian Reichel 
5210f920277SSebastian Reichel 	ret = of_property_read_u32_index(spi->dev.of_node, "reg", 1, &ezport_reg);
5220f920277SSebastian Reichel 	if (ret)
5230f920277SSebastian Reichel 		return dev_err_probe(&spi->dev, ret, "missing second reg entry!\n");
5240f920277SSebastian Reichel 
5250f920277SSebastian Reichel 	achc->ezport = spi_new_ancillary_device(spi, ezport_reg);
5260f920277SSebastian Reichel 	if (IS_ERR(achc->ezport))
5270f920277SSebastian Reichel 		return PTR_ERR(achc->ezport);
5280f920277SSebastian Reichel 
5290f920277SSebastian Reichel 	ret = devm_add_action_or_reset(&spi->dev, unregister_ezport, achc->ezport);
5300f920277SSebastian Reichel 	if (ret)
5310f920277SSebastian Reichel 		return ret;
5320f920277SSebastian Reichel 
5330f920277SSebastian Reichel 	achc->reset = devm_gpiod_get(&spi->dev, "reset", GPIOD_OUT_LOW);
5340f920277SSebastian Reichel 	if (IS_ERR(achc->reset))
5350f920277SSebastian Reichel 		return dev_err_probe(&spi->dev, PTR_ERR(achc->reset), "Could not get reset gpio\n");
5360f920277SSebastian Reichel 
5370f920277SSebastian Reichel 	return 0;
5380f920277SSebastian Reichel }
5390f920277SSebastian Reichel 
5400f920277SSebastian Reichel static const struct spi_device_id gehc_achc_id[] = {
5410f920277SSebastian Reichel 	{ "ge,achc", 0 },
542*a3e16937SMark Brown 	{ "achc", 0 },
5430f920277SSebastian Reichel 	{ }
5440f920277SSebastian Reichel };
5450f920277SSebastian Reichel MODULE_DEVICE_TABLE(spi, gehc_achc_id);
5460f920277SSebastian Reichel 
5470f920277SSebastian Reichel static const struct of_device_id gehc_achc_of_match[] = {
5480f920277SSebastian Reichel 	{ .compatible = "ge,achc" },
5490f920277SSebastian Reichel 	{ /* sentinel */ }
5500f920277SSebastian Reichel };
5510f920277SSebastian Reichel MODULE_DEVICE_TABLE(of, gehc_achc_of_match);
5520f920277SSebastian Reichel 
5530f920277SSebastian Reichel static struct spi_driver gehc_achc_spi_driver = {
5540f920277SSebastian Reichel 	.driver = {
5550f920277SSebastian Reichel 		.name	= "gehc-achc",
5560f920277SSebastian Reichel 		.of_match_table = gehc_achc_of_match,
5570f920277SSebastian Reichel 		.dev_groups = gehc_achc_groups,
5580f920277SSebastian Reichel 	},
5590f920277SSebastian Reichel 	.probe		= gehc_achc_probe,
5600f920277SSebastian Reichel 	.id_table	= gehc_achc_id,
5610f920277SSebastian Reichel };
5620f920277SSebastian Reichel module_spi_driver(gehc_achc_spi_driver);
5630f920277SSebastian Reichel 
5640f920277SSebastian Reichel MODULE_DESCRIPTION("GEHC ACHC driver");
5650f920277SSebastian Reichel MODULE_AUTHOR("Sebastian Reichel <sebastian.reichel@collabora.com>");
5660f920277SSebastian Reichel MODULE_LICENSE("GPL");
567