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