Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2015 Zodiac Inflight Innovations
  4 *
  5 * Author: Martyn Welch <martyn.welch@collabora.co.uk>
  6 *
  7 * Based on twl4030_wdt.c by Timo Kokkonen <timo.t.kokkonen at nokia.com>:
  8 *
  9 * Copyright (C) Nokia Corporation
 
 
 
 
 
 
 
 
 
 
 10 */
 11
 12#include <linux/delay.h>
 13#include <linux/i2c.h>
 14#include <linux/ihex.h>
 15#include <linux/firmware.h>
 16#include <linux/kernel.h>
 17#include <linux/module.h>
 18#include <linux/slab.h>
 19#include <linux/sysfs.h>
 20#include <linux/types.h>
 
 21#include <linux/watchdog.h>
 22
 23#include <linux/unaligned.h>
 24
 25#define ZIIRAVE_TIMEOUT_MIN	3
 26#define ZIIRAVE_TIMEOUT_MAX	255
 27#define ZIIRAVE_TIMEOUT_DEFAULT	30
 28
 29#define ZIIRAVE_PING_VALUE	0x0
 30
 31#define ZIIRAVE_STATE_INITIAL	0x0
 32#define ZIIRAVE_STATE_OFF	0x1
 33#define ZIIRAVE_STATE_ON	0x2
 34
 35#define ZIIRAVE_FW_NAME		"ziirave_wdt.fw"
 36
 37static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
 38				  "host request", NULL, "illegal configuration",
 39				  "illegal instruction", "illegal trap",
 40				  "unknown"};
 41
 42#define ZIIRAVE_WDT_FIRM_VER_MAJOR	0x1
 43#define ZIIRAVE_WDT_BOOT_VER_MAJOR	0x3
 44#define ZIIRAVE_WDT_RESET_REASON	0x5
 45#define ZIIRAVE_WDT_STATE		0x6
 46#define ZIIRAVE_WDT_TIMEOUT		0x7
 47#define ZIIRAVE_WDT_TIME_LEFT		0x8
 48#define ZIIRAVE_WDT_PING		0x9
 49#define ZIIRAVE_WDT_RESET_DURATION	0xa
 50
 51#define ZIIRAVE_FIRM_PKT_TOTAL_SIZE	20
 52#define ZIIRAVE_FIRM_PKT_DATA_SIZE	16
 53#define ZIIRAVE_FIRM_FLASH_MEMORY_START	(2 * 0x1600)
 54#define ZIIRAVE_FIRM_FLASH_MEMORY_END	(2 * 0x2bbf)
 55#define ZIIRAVE_FIRM_PAGE_SIZE		128
 56
 57/* Received and ready for next Download packet. */
 58#define ZIIRAVE_FIRM_DOWNLOAD_ACK	1
 
 
 
 
 
 59
 60/* Firmware commands */
 61#define ZIIRAVE_CMD_DOWNLOAD_START		0x10
 62#define ZIIRAVE_CMD_DOWNLOAD_END		0x11
 63#define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR	0x12
 64#define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE		0x13
 65#define ZIIRAVE_CMD_RESET_PROCESSOR		0x0b
 66#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER		0x0c
 67#define ZIIRAVE_CMD_DOWNLOAD_PACKET		0x0e
 68
 69#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC	1
 70#define ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC	1
 71
 72struct ziirave_wdt_rev {
 73	unsigned char major;
 74	unsigned char minor;
 75};
 76
 77struct ziirave_wdt_data {
 78	struct mutex sysfs_mutex;
 79	struct watchdog_device wdd;
 80	struct ziirave_wdt_rev bootloader_rev;
 81	struct ziirave_wdt_rev firmware_rev;
 82	int reset_reason;
 83};
 84
 85static int wdt_timeout;
 86module_param(wdt_timeout, int, 0);
 87MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
 88
 89static int reset_duration;
 90module_param(reset_duration, int, 0);
 91MODULE_PARM_DESC(reset_duration,
 92		 "Watchdog reset pulse duration in milliseconds");
 93
 94static bool nowayout = WATCHDOG_NOWAYOUT;
 95module_param(nowayout, bool, 0);
 96MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
 97		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 98
 99static int ziirave_wdt_revision(struct i2c_client *client,
100				struct ziirave_wdt_rev *rev, u8 command)
101{
102	int ret;
103
104	ret = i2c_smbus_read_byte_data(client, command);
105	if (ret < 0)
106		return ret;
107
108	rev->major = ret;
109
110	ret = i2c_smbus_read_byte_data(client, command + 1);
111	if (ret < 0)
112		return ret;
113
114	rev->minor = ret;
115
116	return 0;
117}
118
119static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
120{
121	struct i2c_client *client = to_i2c_client(wdd->parent);
122
123	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
124}
125
126static int ziirave_wdt_start(struct watchdog_device *wdd)
127{
128	return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
129}
130
131static int ziirave_wdt_stop(struct watchdog_device *wdd)
132{
133	return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
134}
135
136static int ziirave_wdt_ping(struct watchdog_device *wdd)
137{
138	struct i2c_client *client = to_i2c_client(wdd->parent);
139
140	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
141					 ZIIRAVE_PING_VALUE);
142}
143
144static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
145				   unsigned int timeout)
146{
147	struct i2c_client *client = to_i2c_client(wdd->parent);
148	int ret;
149
150	ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
151	if (!ret)
152		wdd->timeout = timeout;
153
154	return ret;
155}
156
157static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
158{
159	struct i2c_client *client = to_i2c_client(wdd->parent);
160	int ret;
161
162	ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
163	if (ret < 0)
164		ret = 0;
165
166	return ret;
167}
168
169static int ziirave_firm_read_ack(struct watchdog_device *wdd)
170{
171	struct i2c_client *client = to_i2c_client(wdd->parent);
172	int ret;
 
173
174	ret = i2c_smbus_read_byte(client);
175	if (ret < 0) {
176		dev_err(&client->dev, "Failed to read status byte\n");
177		return ret;
178	}
 
 
 
 
 
 
 
 
179
180	return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
181}
182
183static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u32 addr)
184{
185	struct i2c_client *client = to_i2c_client(wdd->parent);
186	const u16 addr16 = (u16)addr / 2;
187	u8 address[2];
188
189	put_unaligned_le16(addr16, address);
 
190
191	return i2c_smbus_write_block_data(client,
192					  ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
193					  sizeof(address), address);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194}
195
196static bool ziirave_firm_addr_readonly(u32 addr)
 
197{
198	return addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
199	       addr > ZIIRAVE_FIRM_FLASH_MEMORY_END;
200}
201
202/*
203 * ziirave_firm_write_pkt() - Build and write a firmware packet
204 *
205 * A packet to send to the firmware is composed by following bytes:
206 *     Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
207 * Where,
208 *     Length: A data byte containing the length of the data.
209 *     Addr0: Low byte of the address.
210 *     Addr1: High byte of the address.
211 *     Data0 .. Data15: Array of 16 bytes of data.
212 *     Checksum: Checksum byte to verify data integrity.
213 */
214static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
215				    u32 addr, const u8 *data, u8 len)
216{
217	const u16 addr16 = (u16)addr / 2;
218	struct i2c_client *client = to_i2c_client(wdd->parent);
219	u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
220	int ret;
 
221
222	/* Check max data size */
223	if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
224		dev_err(&client->dev, "Firmware packet too long (%d)\n",
225			len);
226		return -EMSGSIZE;
227	}
228
229	/*
230	 * Ignore packets that are targeting program memory outisde of
231	 * app partition, since they will be ignored by the
232	 * bootloader. At the same time, we need to make sure we'll
233	 * allow zero length packet that will be sent as the last step
234	 * of firmware update
235	 */
236	if (len && ziirave_firm_addr_readonly(addr))
237		return 0;
238
239	/* Packet length */
240	packet[0] = len;
241	/* Packet address */
242	put_unaligned_le16(addr16, packet + 1);
 
 
243
244	memcpy(packet + 3, data, len);
245	memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len);
 
 
246
247	/* Packet checksum */
248	for (i = 0; i < len + 3; i++)
249		checksum += packet[i];
250	packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
251
252	ret = i2c_smbus_write_block_data(client, ZIIRAVE_CMD_DOWNLOAD_PACKET,
253					 sizeof(packet), packet);
254	if (ret) {
255		dev_err(&client->dev,
256			"Failed to send DOWNLOAD_PACKET: %d\n", ret);
257		return ret;
258	}
259
260	ret = ziirave_firm_read_ack(wdd);
261	if (ret)
262		dev_err(&client->dev,
263		      "Failed to write firmware packet at address 0x%04x: %d\n",
264		      addr, ret);
265
266	return ret;
267}
268
269static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
270				  u32 addr, const u8 *data, u8 len)
271{
272	const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
273		(addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
274	int ret;
275
276	if (len > max_write_len) {
277		/*
278		 * If data crossed page boundary we need to split this
279		 * write in two
280		 */
281		ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
282		if (ret)
283			return ret;
284
285		addr += max_write_len;
286		data += max_write_len;
287		len  -= max_write_len;
288	}
289
290	return __ziirave_firm_write_pkt(wdd, addr, data, len);
291}
292
293static int ziirave_firm_verify(struct watchdog_device *wdd,
294			       const struct firmware *fw)
295{
296	struct i2c_client *client = to_i2c_client(wdd->parent);
297	const struct ihex_binrec *rec;
298	int i, ret;
299	u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
 
300
301	for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
302		const u16 len = be16_to_cpu(rec->len);
303		const u32 addr = be32_to_cpu(rec->addr);
304
305		if (ziirave_firm_addr_readonly(addr))
 
 
 
306			continue;
307
308		ret = ziirave_firm_set_read_addr(wdd, addr);
309		if (ret) {
310			dev_err(&client->dev,
311				"Failed to send SET_READ_ADDR command: %d\n",
312				ret);
313			return ret;
314		}
315
316		for (i = 0; i < len; i++) {
317			ret = i2c_smbus_read_byte_data(client,
318						ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
319			if (ret < 0) {
320				dev_err(&client->dev,
321					"Failed to READ DATA: %d\n", ret);
322				return ret;
323			}
324			data[i] = ret;
325		}
326
327		if (memcmp(data, rec->data, len)) {
328			dev_err(&client->dev,
329				"Firmware mismatch at address 0x%04x\n", addr);
330			return -EINVAL;
331		}
332	}
333
334	return 0;
335}
336
337static int ziirave_firm_upload(struct watchdog_device *wdd,
338			       const struct firmware *fw)
339{
340	struct i2c_client *client = to_i2c_client(wdd->parent);
 
341	const struct ihex_binrec *rec;
342	int ret;
343
344	ret = i2c_smbus_write_byte_data(client,
345					ZIIRAVE_CMD_JUMP_TO_BOOTLOADER,
346					ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC);
347	if (ret) {
348		dev_err(&client->dev, "Failed to jump to bootloader\n");
349		return ret;
350	}
351
352	msleep(500);
353
354	ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_START);
355	if (ret) {
356		dev_err(&client->dev, "Failed to start download\n");
357		return ret;
358	}
359
360	ret = ziirave_firm_read_ack(wdd);
361	if (ret) {
362		dev_err(&client->dev, "No ACK for start download\n");
363		return ret;
364	}
365
366	msleep(500);
367
368	for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
369		ret = ziirave_firm_write_pkt(wdd, be32_to_cpu(rec->addr),
370					     rec->data, be16_to_cpu(rec->len));
371		if (ret)
372			return ret;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373	}
374
375	/*
376	 * Finish firmware download process by sending a zero length
377	 * payload
378	 */
379	ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
 
 
380	if (ret) {
381		dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
382		return ret;
383	}
384
385	/* This sleep seems to be required */
386	msleep(20);
387
388	/* Start firmware verification */
389	ret = ziirave_firm_verify(wdd, fw);
390	if (ret) {
391		dev_err(&client->dev,
392			"Failed to verify firmware: %d\n", ret);
393		return ret;
394	}
395
396	/* End download operation */
397	ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_END);
398	if (ret) {
399		dev_err(&client->dev,
400			"Failed to end firmware download: %d\n", ret);
401		return ret;
402	}
403
404	/* Reset the processor */
405	ret = i2c_smbus_write_byte_data(client,
406					ZIIRAVE_CMD_RESET_PROCESSOR,
407					ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC);
408	if (ret) {
409		dev_err(&client->dev,
410			"Failed to reset the watchdog: %d\n", ret);
411		return ret;
412	}
413
414	msleep(500);
415
416	return 0;
417}
418
419static const struct watchdog_info ziirave_wdt_info = {
420	.options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
421	.identity = "RAVE Switch Watchdog",
422};
423
424static const struct watchdog_ops ziirave_wdt_ops = {
425	.owner		= THIS_MODULE,
426	.start		= ziirave_wdt_start,
427	.stop		= ziirave_wdt_stop,
428	.ping		= ziirave_wdt_ping,
429	.set_timeout	= ziirave_wdt_set_timeout,
430	.get_timeleft	= ziirave_wdt_get_timeleft,
431};
432
433static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
434					   struct device_attribute *attr,
435					   char *buf)
436{
437	struct i2c_client *client = to_i2c_client(dev->parent);
438	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
439	int ret;
440
441	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
442	if (ret)
443		return ret;
444
445	ret = sysfs_emit(buf, "02.%02u.%02u\n",
446			 w_priv->firmware_rev.major,
447			 w_priv->firmware_rev.minor);
448
449	mutex_unlock(&w_priv->sysfs_mutex);
450
451	return ret;
452}
453
454static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
455		   NULL);
456
457static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
458					   struct device_attribute *attr,
459					   char *buf)
460{
461	struct i2c_client *client = to_i2c_client(dev->parent);
462	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
463	int ret;
464
465	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
466	if (ret)
467		return ret;
468
469	ret = sysfs_emit(buf, "01.%02u.%02u\n",
470			 w_priv->bootloader_rev.major,
471			 w_priv->bootloader_rev.minor);
472
473	mutex_unlock(&w_priv->sysfs_mutex);
474
475	return ret;
476}
477
478static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
479		   NULL);
480
481static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
482					     struct device_attribute *attr,
483					     char *buf)
484{
485	struct i2c_client *client = to_i2c_client(dev->parent);
486	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
487	int ret;
488
489	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
490	if (ret)
491		return ret;
492
493	ret = sysfs_emit(buf, "%s\n", ziirave_reasons[w_priv->reset_reason]);
494
495	mutex_unlock(&w_priv->sysfs_mutex);
496
497	return ret;
498}
499
500static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
501		   NULL);
502
503static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
504					    struct device_attribute *attr,
505					    const char *buf, size_t count)
506{
507	struct i2c_client *client = to_i2c_client(dev->parent);
508	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
509	const struct firmware *fw;
510	int err;
511
512	err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
513	if (err) {
514		dev_err(&client->dev, "Failed to request ihex firmware\n");
515		return err;
516	}
517
518	err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
519	if (err)
520		goto release_firmware;
521
522	err = ziirave_firm_upload(&w_priv->wdd, fw);
523	if (err) {
524		dev_err(&client->dev, "The firmware update failed: %d\n", err);
525		goto unlock_mutex;
526	}
527
528	/* Update firmware version */
529	err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
530				   ZIIRAVE_WDT_FIRM_VER_MAJOR);
531	if (err) {
532		dev_err(&client->dev, "Failed to read firmware version: %d\n",
533			err);
534		goto unlock_mutex;
535	}
536
537	dev_info(&client->dev,
538		 "Firmware updated to version 02.%02u.%02u\n",
539		 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
540
541	/* Restore the watchdog timeout */
542	err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
543	if (err)
544		dev_err(&client->dev, "Failed to set timeout: %d\n", err);
545
546unlock_mutex:
547	mutex_unlock(&w_priv->sysfs_mutex);
548
549release_firmware:
550	release_firmware(fw);
551
552	return err ? err : count;
553}
554
555static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
556		   ziirave_wdt_sysfs_store_firm);
557
558static struct attribute *ziirave_wdt_attrs[] = {
559	&dev_attr_firmware_version.attr,
560	&dev_attr_bootloader_version.attr,
561	&dev_attr_reset_reason.attr,
562	&dev_attr_update_firmware.attr,
563	NULL
564};
565ATTRIBUTE_GROUPS(ziirave_wdt);
566
567static int ziirave_wdt_init_duration(struct i2c_client *client)
568{
569	int ret;
570
571	if (!reset_duration) {
572		/* See if the reset pulse duration is provided in an of_node */
573		if (!client->dev.of_node)
574			ret = -ENODEV;
575		else
576			ret = of_property_read_u32(client->dev.of_node,
577						   "reset-duration-ms",
578						   &reset_duration);
579		if (ret) {
580			dev_info(&client->dev,
581			 "No reset pulse duration specified, using default\n");
582			return 0;
583		}
584	}
585
586	if (reset_duration < 1 || reset_duration > 255)
587		return -EINVAL;
588
589	dev_info(&client->dev, "Setting reset duration to %dms",
590		 reset_duration);
591
592	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
593					 reset_duration);
594}
595
596static int ziirave_wdt_probe(struct i2c_client *client)
 
597{
598	int ret;
599	struct ziirave_wdt_data *w_priv;
600	int val;
601
602	if (!i2c_check_functionality(client->adapter,
603				     I2C_FUNC_SMBUS_BYTE |
604				     I2C_FUNC_SMBUS_BYTE_DATA |
605				     I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
606		return -ENODEV;
607
608	w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
609	if (!w_priv)
610		return -ENOMEM;
611
612	mutex_init(&w_priv->sysfs_mutex);
613
614	w_priv->wdd.info = &ziirave_wdt_info;
615	w_priv->wdd.ops = &ziirave_wdt_ops;
616	w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
617	w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
618	w_priv->wdd.parent = &client->dev;
619	w_priv->wdd.groups = ziirave_wdt_groups;
620
621	watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
 
 
 
 
622
623	/*
624	 * The default value set in the watchdog should be perfectly valid, so
625	 * pass that in if we haven't provided one via the module parameter or
626	 * of property.
627	 */
628	if (w_priv->wdd.timeout == 0) {
629		val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
630		if (val < 0) {
631			dev_err(&client->dev, "Failed to read timeout\n");
632			return val;
633		}
634
635		if (val > ZIIRAVE_TIMEOUT_MAX ||
636		    val < ZIIRAVE_TIMEOUT_MIN)
637			val = ZIIRAVE_TIMEOUT_DEFAULT;
638
639		w_priv->wdd.timeout = val;
640	}
 
 
 
 
641
642	ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
643	if (ret) {
644		dev_err(&client->dev, "Failed to set timeout\n");
645		return ret;
646	}
647
648	dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
649
650	watchdog_set_nowayout(&w_priv->wdd, nowayout);
651
652	i2c_set_clientdata(client, w_priv);
653
654	/* If in unconfigured state, set to stopped */
655	val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
656	if (val < 0) {
657		dev_err(&client->dev, "Failed to read state\n");
658		return val;
659	}
660
661	if (val == ZIIRAVE_STATE_INITIAL)
662		ziirave_wdt_stop(&w_priv->wdd);
663
664	ret = ziirave_wdt_init_duration(client);
665	if (ret) {
666		dev_err(&client->dev, "Failed to init duration\n");
667		return ret;
668	}
669
670	ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
671				   ZIIRAVE_WDT_FIRM_VER_MAJOR);
672	if (ret) {
673		dev_err(&client->dev, "Failed to read firmware version\n");
674		return ret;
675	}
676
677	dev_info(&client->dev,
678		 "Firmware version: 02.%02u.%02u\n",
679		 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
680
681	ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
682				   ZIIRAVE_WDT_BOOT_VER_MAJOR);
683	if (ret) {
684		dev_err(&client->dev, "Failed to read bootloader version\n");
685		return ret;
686	}
687
688	dev_info(&client->dev,
689		 "Bootloader version: 01.%02u.%02u\n",
690		 w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
691
692	w_priv->reset_reason = i2c_smbus_read_byte_data(client,
693						ZIIRAVE_WDT_RESET_REASON);
694	if (w_priv->reset_reason < 0) {
695		dev_err(&client->dev, "Failed to read reset reason\n");
696		return w_priv->reset_reason;
697	}
698
699	if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
700	    !ziirave_reasons[w_priv->reset_reason]) {
701		dev_err(&client->dev, "Invalid reset reason\n");
702		return -ENODEV;
703	}
704
705	ret = watchdog_register_device(&w_priv->wdd);
706
707	return ret;
708}
709
710static void ziirave_wdt_remove(struct i2c_client *client)
711{
712	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
713
714	watchdog_unregister_device(&w_priv->wdd);
 
 
715}
716
717static const struct i2c_device_id ziirave_wdt_id[] = {
718	{ "rave-wdt" },
719	{ }
720};
721MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
722
723static const struct of_device_id zrv_wdt_of_match[] = {
724	{ .compatible = "zii,rave-wdt", },
725	{ },
726};
727MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
728
729static struct i2c_driver ziirave_wdt_driver = {
730	.driver = {
731		.name = "ziirave_wdt",
732		.of_match_table = zrv_wdt_of_match,
733	},
734	.probe = ziirave_wdt_probe,
735	.remove = ziirave_wdt_remove,
736	.id_table = ziirave_wdt_id,
737};
738
739module_i2c_driver(ziirave_wdt_driver);
740
741MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
742MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
743MODULE_LICENSE("GPL");
v4.10.11
 
  1/*
  2 * Copyright (C) 2015 Zodiac Inflight Innovations
  3 *
  4 * Author: Martyn Welch <martyn.welch@collabora.co.uk>
  5 *
  6 * Based on twl4030_wdt.c by Timo Kokkonen <timo.t.kokkonen at nokia.com>:
  7 *
  8 * Copyright (C) Nokia Corporation
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 18 * GNU General Public License for more details.
 19 */
 20
 21#include <linux/delay.h>
 22#include <linux/i2c.h>
 23#include <linux/ihex.h>
 24#include <linux/firmware.h>
 25#include <linux/kernel.h>
 26#include <linux/module.h>
 27#include <linux/slab.h>
 28#include <linux/sysfs.h>
 29#include <linux/types.h>
 30#include <linux/version.h>
 31#include <linux/watchdog.h>
 32
 
 
 33#define ZIIRAVE_TIMEOUT_MIN	3
 34#define ZIIRAVE_TIMEOUT_MAX	255
 
 35
 36#define ZIIRAVE_PING_VALUE	0x0
 37
 38#define ZIIRAVE_STATE_INITIAL	0x0
 39#define ZIIRAVE_STATE_OFF	0x1
 40#define ZIIRAVE_STATE_ON	0x2
 41
 42#define ZIIRAVE_FW_NAME		"ziirave_wdt.fw"
 43
 44static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
 45				  "host request", NULL, "illegal configuration",
 46				  "illegal instruction", "illegal trap",
 47				  "unknown"};
 48
 49#define ZIIRAVE_WDT_FIRM_VER_MAJOR	0x1
 50#define ZIIRAVE_WDT_BOOT_VER_MAJOR	0x3
 51#define ZIIRAVE_WDT_RESET_REASON	0x5
 52#define ZIIRAVE_WDT_STATE		0x6
 53#define ZIIRAVE_WDT_TIMEOUT		0x7
 54#define ZIIRAVE_WDT_TIME_LEFT		0x8
 55#define ZIIRAVE_WDT_PING		0x9
 56#define ZIIRAVE_WDT_RESET_DURATION	0xa
 57
 58#define ZIIRAVE_FIRM_PKT_TOTAL_SIZE	20
 59#define ZIIRAVE_FIRM_PKT_DATA_SIZE	16
 60#define ZIIRAVE_FIRM_FLASH_MEMORY_START	0x1600
 61#define ZIIRAVE_FIRM_FLASH_MEMORY_END	0x2bbf
 
 62
 63/* Received and ready for next Download packet. */
 64#define ZIIRAVE_FIRM_DOWNLOAD_ACK	1
 65/* Currently writing to flash. Retry Download status in a moment! */
 66#define ZIIRAVE_FIRM_DOWNLOAD_BUSY	2
 67
 68/* Wait for ACK timeout in ms */
 69#define ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT	50
 70
 71/* Firmware commands */
 72#define ZIIRAVE_CMD_DOWNLOAD_START		0x10
 73#define ZIIRAVE_CMD_DOWNLOAD_END		0x11
 74#define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR	0x12
 75#define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE		0x13
 76#define ZIIRAVE_CMD_RESET_PROCESSOR		0x0b
 77#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER		0x0c
 78#define ZIIRAVE_CMD_DOWNLOAD_PACKET		0x0e
 79
 
 
 
 80struct ziirave_wdt_rev {
 81	unsigned char major;
 82	unsigned char minor;
 83};
 84
 85struct ziirave_wdt_data {
 86	struct mutex sysfs_mutex;
 87	struct watchdog_device wdd;
 88	struct ziirave_wdt_rev bootloader_rev;
 89	struct ziirave_wdt_rev firmware_rev;
 90	int reset_reason;
 91};
 92
 93static int wdt_timeout;
 94module_param(wdt_timeout, int, 0);
 95MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
 96
 97static int reset_duration;
 98module_param(reset_duration, int, 0);
 99MODULE_PARM_DESC(reset_duration,
100		 "Watchdog reset pulse duration in milliseconds");
101
102static bool nowayout = WATCHDOG_NOWAYOUT;
103module_param(nowayout, bool, 0);
104MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
105		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
106
107static int ziirave_wdt_revision(struct i2c_client *client,
108				struct ziirave_wdt_rev *rev, u8 command)
109{
110	int ret;
111
112	ret = i2c_smbus_read_byte_data(client, command);
113	if (ret < 0)
114		return ret;
115
116	rev->major = ret;
117
118	ret = i2c_smbus_read_byte_data(client, command + 1);
119	if (ret < 0)
120		return ret;
121
122	rev->minor = ret;
123
124	return 0;
125}
126
127static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
128{
129	struct i2c_client *client = to_i2c_client(wdd->parent);
130
131	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
132}
133
134static int ziirave_wdt_start(struct watchdog_device *wdd)
135{
136	return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
137}
138
139static int ziirave_wdt_stop(struct watchdog_device *wdd)
140{
141	return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
142}
143
144static int ziirave_wdt_ping(struct watchdog_device *wdd)
145{
146	struct i2c_client *client = to_i2c_client(wdd->parent);
147
148	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
149					 ZIIRAVE_PING_VALUE);
150}
151
152static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
153				   unsigned int timeout)
154{
155	struct i2c_client *client = to_i2c_client(wdd->parent);
156	int ret;
157
158	ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
159	if (!ret)
160		wdd->timeout = timeout;
161
162	return ret;
163}
164
165static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
166{
167	struct i2c_client *client = to_i2c_client(wdd->parent);
168	int ret;
169
170	ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
171	if (ret < 0)
172		ret = 0;
173
174	return ret;
175}
176
177static int ziirave_firm_wait_for_ack(struct watchdog_device *wdd)
178{
179	struct i2c_client *client = to_i2c_client(wdd->parent);
180	int ret;
181	unsigned long timeout;
182
183	timeout = jiffies + msecs_to_jiffies(ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT);
184	do {
185		if (time_after(jiffies, timeout))
186			return -ETIMEDOUT;
187
188		usleep_range(5000, 10000);
189
190		ret = i2c_smbus_read_byte(client);
191		if (ret < 0) {
192			dev_err(&client->dev, "Failed to read byte\n");
193			return ret;
194		}
195	} while (ret == ZIIRAVE_FIRM_DOWNLOAD_BUSY);
196
197	return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
198}
199
200static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u16 addr)
201{
202	struct i2c_client *client = to_i2c_client(wdd->parent);
 
203	u8 address[2];
204
205	address[0] = addr & 0xff;
206	address[1] = (addr >> 8) & 0xff;
207
208	return i2c_smbus_write_block_data(client,
209					  ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
210					  ARRAY_SIZE(address), address);
211}
212
213static int ziirave_firm_write_block_data(struct watchdog_device *wdd,
214					 u8 command, u8 length, const u8 *data,
215					 bool wait_for_ack)
216{
217	struct i2c_client *client = to_i2c_client(wdd->parent);
218	int ret;
219
220	ret = i2c_smbus_write_block_data(client, command, length, data);
221	if (ret) {
222		dev_err(&client->dev,
223			"Failed to send command 0x%02x: %d\n", command, ret);
224		return ret;
225	}
226
227	if (wait_for_ack)
228		ret = ziirave_firm_wait_for_ack(wdd);
229
230	return ret;
231}
232
233static int ziirave_firm_write_byte(struct watchdog_device *wdd, u8 command,
234				   u8 byte, bool wait_for_ack)
235{
236	return ziirave_firm_write_block_data(wdd, command, 1, &byte,
237					     wait_for_ack);
238}
239
240/*
241 * ziirave_firm_write_pkt() - Build and write a firmware packet
242 *
243 * A packet to send to the firmware is composed by following bytes:
244 *     Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
245 * Where,
246 *     Length: A data byte containing the length of the data.
247 *     Addr0: Low byte of the address.
248 *     Addr1: High byte of the address.
249 *     Data0 .. Data15: Array of 16 bytes of data.
250 *     Checksum: Checksum byte to verify data integrity.
251 */
252static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
253				  const struct ihex_binrec *rec)
254{
 
255	struct i2c_client *client = to_i2c_client(wdd->parent);
256	u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
257	int ret;
258	u16 addr;
259
260	memset(packet, 0, ARRAY_SIZE(packet));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
262	/* Packet length */
263	packet[0] = (u8)be16_to_cpu(rec->len);
264	/* Packet address */
265	addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
266	packet[1] = addr & 0xff;
267	packet[2] = (addr & 0xff00) >> 8;
268
269	/* Packet data */
270	if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE)
271		return -EMSGSIZE;
272	memcpy(packet + 3, rec->data, be16_to_cpu(rec->len));
273
274	/* Packet checksum */
275	for (i = 0; i < ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1; i++)
276		checksum += packet[i];
277	packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
278
279	ret = ziirave_firm_write_block_data(wdd, ZIIRAVE_CMD_DOWNLOAD_PACKET,
280					    ARRAY_SIZE(packet), packet, true);
 
 
 
 
 
 
 
281	if (ret)
282		dev_err(&client->dev,
283		      "Failed to write firmware packet at address 0x%04x: %d\n",
284		      addr, ret);
285
286	return ret;
287}
288
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289static int ziirave_firm_verify(struct watchdog_device *wdd,
290			       const struct firmware *fw)
291{
292	struct i2c_client *client = to_i2c_client(wdd->parent);
293	const struct ihex_binrec *rec;
294	int i, ret;
295	u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
296	u16 addr;
297
298	for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
299		/* Zero length marks end of records */
300		if (!be16_to_cpu(rec->len))
301			break;
302
303		addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
304		if (addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
305		    addr > ZIIRAVE_FIRM_FLASH_MEMORY_END)
306			continue;
307
308		ret = ziirave_firm_set_read_addr(wdd, addr);
309		if (ret) {
310			dev_err(&client->dev,
311				"Failed to send SET_READ_ADDR command: %d\n",
312				ret);
313			return ret;
314		}
315
316		for (i = 0; i < ARRAY_SIZE(data); i++) {
317			ret = i2c_smbus_read_byte_data(client,
318						ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
319			if (ret < 0) {
320				dev_err(&client->dev,
321					"Failed to READ DATA: %d\n", ret);
322				return ret;
323			}
324			data[i] = ret;
325		}
326
327		if (memcmp(data, rec->data, be16_to_cpu(rec->len))) {
328			dev_err(&client->dev,
329				"Firmware mismatch at address 0x%04x\n", addr);
330			return -EINVAL;
331		}
332	}
333
334	return 0;
335}
336
337static int ziirave_firm_upload(struct watchdog_device *wdd,
338			       const struct firmware *fw)
339{
340	struct i2c_client *client = to_i2c_client(wdd->parent);
341	int ret, words_till_page_break;
342	const struct ihex_binrec *rec;
343	struct ihex_binrec *rec_new;
344
345	ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_JUMP_TO_BOOTLOADER, 1,
346				      false);
347	if (ret)
 
 
348		return ret;
 
349
350	msleep(500);
351
352	ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_START, 1, true);
353	if (ret)
 
 
 
 
 
 
 
354		return ret;
 
355
356	msleep(500);
357
358	for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
359		/* Zero length marks end of records */
360		if (!be16_to_cpu(rec->len))
361			break;
362
363		/* Check max data size */
364		if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
365			dev_err(&client->dev, "Firmware packet too long (%d)\n",
366				be16_to_cpu(rec->len));
367			return -EMSGSIZE;
368		}
369
370		/* Calculate words till page break */
371		words_till_page_break = (64 - ((be32_to_cpu(rec->addr) >> 1) &
372					 0x3f));
373		if ((be16_to_cpu(rec->len) >> 1) > words_till_page_break) {
374			/*
375			 * Data in passes page boundary, so we need to split in
376			 * two blocks of data. Create a packet with the first
377			 * block of data.
378			 */
379			rec_new = kzalloc(sizeof(struct ihex_binrec) +
380					  (words_till_page_break << 1),
381					  GFP_KERNEL);
382			if (!rec_new)
383				return -ENOMEM;
384
385			rec_new->len = cpu_to_be16(words_till_page_break << 1);
386			rec_new->addr = rec->addr;
387			memcpy(rec_new->data, rec->data,
388			       be16_to_cpu(rec_new->len));
389
390			ret = ziirave_firm_write_pkt(wdd, rec_new);
391			kfree(rec_new);
392			if (ret)
393				return ret;
394
395			/* Create a packet with the second block of data */
396			rec_new = kzalloc(sizeof(struct ihex_binrec) +
397					  be16_to_cpu(rec->len) -
398					  (words_till_page_break << 1),
399					  GFP_KERNEL);
400			if (!rec_new)
401				return -ENOMEM;
402
403			/* Remaining bytes */
404			rec_new->len = rec->len -
405				       cpu_to_be16(words_till_page_break << 1);
406
407			rec_new->addr = cpu_to_be32(be32_to_cpu(rec->addr) +
408					(words_till_page_break << 1));
409
410			memcpy(rec_new->data,
411			       rec->data + (words_till_page_break << 1),
412			       be16_to_cpu(rec_new->len));
413
414			ret = ziirave_firm_write_pkt(wdd, rec_new);
415			kfree(rec_new);
416			if (ret)
417				return ret;
418		} else {
419			ret = ziirave_firm_write_pkt(wdd, rec);
420			if (ret)
421				return ret;
422		}
423	}
424
425	/* For end of download, the length field will be set to 0 */
426	rec_new = kzalloc(sizeof(struct ihex_binrec) + 1, GFP_KERNEL);
427	if (!rec_new)
428		return -ENOMEM;
429
430	ret = ziirave_firm_write_pkt(wdd, rec_new);
431	kfree(rec_new);
432	if (ret) {
433		dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
434		return ret;
435	}
436
437	/* This sleep seems to be required */
438	msleep(20);
439
440	/* Start firmware verification */
441	ret = ziirave_firm_verify(wdd, fw);
442	if (ret) {
443		dev_err(&client->dev,
444			"Failed to verify firmware: %d\n", ret);
445		return ret;
446	}
447
448	/* End download operation */
449	ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_END, 1, false);
450	if (ret)
 
 
451		return ret;
 
452
453	/* Reset the processor */
454	ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_RESET_PROCESSOR, 1,
455				      false);
456	if (ret)
 
 
 
457		return ret;
 
458
459	msleep(500);
460
461	return 0;
462}
463
464static const struct watchdog_info ziirave_wdt_info = {
465	.options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
466	.identity = "Zodiac RAVE Watchdog",
467};
468
469static const struct watchdog_ops ziirave_wdt_ops = {
470	.owner		= THIS_MODULE,
471	.start		= ziirave_wdt_start,
472	.stop		= ziirave_wdt_stop,
473	.ping		= ziirave_wdt_ping,
474	.set_timeout	= ziirave_wdt_set_timeout,
475	.get_timeleft	= ziirave_wdt_get_timeleft,
476};
477
478static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
479					   struct device_attribute *attr,
480					   char *buf)
481{
482	struct i2c_client *client = to_i2c_client(dev->parent);
483	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
484	int ret;
485
486	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
487	if (ret)
488		return ret;
489
490	ret = sprintf(buf, "02.%02u.%02u", w_priv->firmware_rev.major,
491		      w_priv->firmware_rev.minor);
 
492
493	mutex_unlock(&w_priv->sysfs_mutex);
494
495	return ret;
496}
497
498static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
499		   NULL);
500
501static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
502					   struct device_attribute *attr,
503					   char *buf)
504{
505	struct i2c_client *client = to_i2c_client(dev->parent);
506	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
507	int ret;
508
509	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
510	if (ret)
511		return ret;
512
513	ret = sprintf(buf, "01.%02u.%02u", w_priv->bootloader_rev.major,
514		      w_priv->bootloader_rev.minor);
 
515
516	mutex_unlock(&w_priv->sysfs_mutex);
517
518	return ret;
519}
520
521static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
522		   NULL);
523
524static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
525					     struct device_attribute *attr,
526					     char *buf)
527{
528	struct i2c_client *client = to_i2c_client(dev->parent);
529	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
530	int ret;
531
532	ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
533	if (ret)
534		return ret;
535
536	ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
537
538	mutex_unlock(&w_priv->sysfs_mutex);
539
540	return ret;
541}
542
543static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
544		   NULL);
545
546static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
547					    struct device_attribute *attr,
548					    const char *buf, size_t count)
549{
550	struct i2c_client *client = to_i2c_client(dev->parent);
551	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
552	const struct firmware *fw;
553	int err;
554
555	err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
556	if (err) {
557		dev_err(&client->dev, "Failed to request ihex firmware\n");
558		return err;
559	}
560
561	err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
562	if (err)
563		goto release_firmware;
564
565	err = ziirave_firm_upload(&w_priv->wdd, fw);
566	if (err) {
567		dev_err(&client->dev, "The firmware update failed: %d\n", err);
568		goto unlock_mutex;
569	}
570
571	/* Update firmware version */
572	err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
573				   ZIIRAVE_WDT_FIRM_VER_MAJOR);
574	if (err) {
575		dev_err(&client->dev, "Failed to read firmware version: %d\n",
576			err);
577		goto unlock_mutex;
578	}
579
580	dev_info(&client->dev, "Firmware updated to version 02.%02u.%02u\n",
 
581		 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
582
583	/* Restore the watchdog timeout */
584	err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
585	if (err)
586		dev_err(&client->dev, "Failed to set timeout: %d\n", err);
587
588unlock_mutex:
589	mutex_unlock(&w_priv->sysfs_mutex);
590
591release_firmware:
592	release_firmware(fw);
593
594	return err ? err : count;
595}
596
597static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
598		   ziirave_wdt_sysfs_store_firm);
599
600static struct attribute *ziirave_wdt_attrs[] = {
601	&dev_attr_firmware_version.attr,
602	&dev_attr_bootloader_version.attr,
603	&dev_attr_reset_reason.attr,
604	&dev_attr_update_firmware.attr,
605	NULL
606};
607ATTRIBUTE_GROUPS(ziirave_wdt);
608
609static int ziirave_wdt_init_duration(struct i2c_client *client)
610{
611	int ret;
612
613	if (!reset_duration) {
614		/* See if the reset pulse duration is provided in an of_node */
615		if (!client->dev.of_node)
616			ret = -ENODEV;
617		else
618			ret = of_property_read_u32(client->dev.of_node,
619						   "reset-duration-ms",
620						   &reset_duration);
621		if (ret) {
622			dev_info(&client->dev,
623				 "Unable to set reset pulse duration, using default\n");
624			return 0;
625		}
626	}
627
628	if (reset_duration < 1 || reset_duration > 255)
629		return -EINVAL;
630
631	dev_info(&client->dev, "Setting reset duration to %dms",
632		 reset_duration);
633
634	return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
635					 reset_duration);
636}
637
638static int ziirave_wdt_probe(struct i2c_client *client,
639			     const struct i2c_device_id *id)
640{
641	int ret;
642	struct ziirave_wdt_data *w_priv;
643	int val;
644
645	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
 
 
 
646		return -ENODEV;
647
648	w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
649	if (!w_priv)
650		return -ENOMEM;
651
652	mutex_init(&w_priv->sysfs_mutex);
653
654	w_priv->wdd.info = &ziirave_wdt_info;
655	w_priv->wdd.ops = &ziirave_wdt_ops;
656	w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
657	w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
658	w_priv->wdd.parent = &client->dev;
659	w_priv->wdd.groups = ziirave_wdt_groups;
660
661	ret = watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
662	if (ret) {
663		dev_info(&client->dev,
664			 "Unable to select timeout value, using default\n");
665	}
666
667	/*
668	 * The default value set in the watchdog should be perfectly valid, so
669	 * pass that in if we haven't provided one via the module parameter or
670	 * of property.
671	 */
672	if (w_priv->wdd.timeout == 0) {
673		val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
674		if (val < 0)
 
675			return val;
 
676
677		if (val < ZIIRAVE_TIMEOUT_MIN)
678			return -ENODEV;
 
679
680		w_priv->wdd.timeout = val;
681	} else {
682		ret = ziirave_wdt_set_timeout(&w_priv->wdd,
683					      w_priv->wdd.timeout);
684		if (ret)
685			return ret;
686
687		dev_info(&client->dev, "Timeout set to %ds.",
688			 w_priv->wdd.timeout);
 
 
689	}
690
 
 
691	watchdog_set_nowayout(&w_priv->wdd, nowayout);
692
693	i2c_set_clientdata(client, w_priv);
694
695	/* If in unconfigured state, set to stopped */
696	val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
697	if (val < 0)
 
698		return val;
 
699
700	if (val == ZIIRAVE_STATE_INITIAL)
701		ziirave_wdt_stop(&w_priv->wdd);
702
703	ret = ziirave_wdt_init_duration(client);
704	if (ret)
 
705		return ret;
 
706
707	ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
708				   ZIIRAVE_WDT_FIRM_VER_MAJOR);
709	if (ret)
 
710		return ret;
 
 
 
 
 
711
712	ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
713				   ZIIRAVE_WDT_BOOT_VER_MAJOR);
714	if (ret)
 
715		return ret;
 
 
 
 
 
716
717	w_priv->reset_reason = i2c_smbus_read_byte_data(client,
718						ZIIRAVE_WDT_RESET_REASON);
719	if (w_priv->reset_reason < 0)
 
720		return w_priv->reset_reason;
 
721
722	if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
723	    !ziirave_reasons[w_priv->reset_reason])
 
724		return -ENODEV;
 
725
726	ret = watchdog_register_device(&w_priv->wdd);
727
728	return ret;
729}
730
731static int ziirave_wdt_remove(struct i2c_client *client)
732{
733	struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
734
735	watchdog_unregister_device(&w_priv->wdd);
736
737	return 0;
738}
739
740static struct i2c_device_id ziirave_wdt_id[] = {
741	{ "rave-wdt", 0 },
742	{ }
743};
744MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
745
746static const struct of_device_id zrv_wdt_of_match[] = {
747	{ .compatible = "zii,rave-wdt", },
748	{ },
749};
750MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
751
752static struct i2c_driver ziirave_wdt_driver = {
753	.driver = {
754		.name = "ziirave_wdt",
755		.of_match_table = zrv_wdt_of_match,
756	},
757	.probe = ziirave_wdt_probe,
758	.remove = ziirave_wdt_remove,
759	.id_table = ziirave_wdt_id,
760};
761
762module_i2c_driver(ziirave_wdt_driver);
763
764MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
765MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
766MODULE_LICENSE("GPL");