blob: a11b92383c5f31b60cdb88dc830a5b54b668cb11 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Martyn Welch2a7b7532015-12-01 15:32:47 +00002/*
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
Martyn Welch2a7b7532015-12-01 15:32:47 +000010 */
11
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020012#include <linux/delay.h>
Martyn Welch2a7b7532015-12-01 15:32:47 +000013#include <linux/i2c.h>
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020014#include <linux/ihex.h>
15#include <linux/firmware.h>
Martyn Welch2a7b7532015-12-01 15:32:47 +000016#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
Andrey Smirnov39d03872019-08-12 13:08:48 -070026#define ZIIRAVE_TIMEOUT_DEFAULT 30
Martyn Welch2a7b7532015-12-01 15:32:47 +000027
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
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020034#define ZIIRAVE_FW_NAME "ziirave_wdt.fw"
35
Martyn Welch0ce72f32016-02-26 16:05:12 +000036static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
Martyn Welch2a7b7532015-12-01 15:32:47 +000037 "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
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020050#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
54
55/* Received and ready for next Download packet. */
56#define ZIIRAVE_FIRM_DOWNLOAD_ACK 1
57/* Currently writing to flash. Retry Download status in a moment! */
58#define ZIIRAVE_FIRM_DOWNLOAD_BUSY 2
59
60/* Wait for ACK timeout in ms */
61#define ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT 50
62
63/* Firmware commands */
64#define ZIIRAVE_CMD_DOWNLOAD_START 0x10
65#define ZIIRAVE_CMD_DOWNLOAD_END 0x11
66#define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR 0x12
67#define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE 0x13
68#define ZIIRAVE_CMD_RESET_PROCESSOR 0x0b
69#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER 0x0c
70#define ZIIRAVE_CMD_DOWNLOAD_PACKET 0x0e
71
Martyn Welch2a7b7532015-12-01 15:32:47 +000072struct ziirave_wdt_rev {
73 unsigned char major;
74 unsigned char minor;
75};
76
77struct ziirave_wdt_data {
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020078 struct mutex sysfs_mutex;
Martyn Welch2a7b7532015-12-01 15:32:47 +000079 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
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200169static int ziirave_firm_wait_for_ack(struct watchdog_device *wdd)
170{
171 struct i2c_client *client = to_i2c_client(wdd->parent);
172 int ret;
173 unsigned long timeout;
174
175 timeout = jiffies + msecs_to_jiffies(ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT);
176 do {
177 if (time_after(jiffies, timeout))
178 return -ETIMEDOUT;
179
180 usleep_range(5000, 10000);
181
182 ret = i2c_smbus_read_byte(client);
183 if (ret < 0) {
184 dev_err(&client->dev, "Failed to read byte\n");
185 return ret;
186 }
187 } while (ret == ZIIRAVE_FIRM_DOWNLOAD_BUSY);
188
189 return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
190}
191
192static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u16 addr)
193{
194 struct i2c_client *client = to_i2c_client(wdd->parent);
195 u8 address[2];
196
197 address[0] = addr & 0xff;
198 address[1] = (addr >> 8) & 0xff;
199
200 return i2c_smbus_write_block_data(client,
201 ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
202 ARRAY_SIZE(address), address);
203}
204
205static int ziirave_firm_write_block_data(struct watchdog_device *wdd,
206 u8 command, u8 length, const u8 *data,
207 bool wait_for_ack)
208{
209 struct i2c_client *client = to_i2c_client(wdd->parent);
210 int ret;
211
212 ret = i2c_smbus_write_block_data(client, command, length, data);
213 if (ret) {
214 dev_err(&client->dev,
215 "Failed to send command 0x%02x: %d\n", command, ret);
216 return ret;
217 }
218
219 if (wait_for_ack)
220 ret = ziirave_firm_wait_for_ack(wdd);
221
222 return ret;
223}
224
225static int ziirave_firm_write_byte(struct watchdog_device *wdd, u8 command,
226 u8 byte, bool wait_for_ack)
227{
228 return ziirave_firm_write_block_data(wdd, command, 1, &byte,
229 wait_for_ack);
230}
231
232/*
233 * ziirave_firm_write_pkt() - Build and write a firmware packet
234 *
235 * A packet to send to the firmware is composed by following bytes:
236 * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
237 * Where,
238 * Length: A data byte containing the length of the data.
239 * Addr0: Low byte of the address.
240 * Addr1: High byte of the address.
241 * Data0 .. Data15: Array of 16 bytes of data.
242 * Checksum: Checksum byte to verify data integrity.
243 */
244static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
245 const struct ihex_binrec *rec)
246{
247 struct i2c_client *client = to_i2c_client(wdd->parent);
248 u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
249 int ret;
250 u16 addr;
251
252 memset(packet, 0, ARRAY_SIZE(packet));
253
254 /* Packet length */
255 packet[0] = (u8)be16_to_cpu(rec->len);
256 /* Packet address */
257 addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
258 packet[1] = addr & 0xff;
259 packet[2] = (addr & 0xff00) >> 8;
260
261 /* Packet data */
262 if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE)
263 return -EMSGSIZE;
264 memcpy(packet + 3, rec->data, be16_to_cpu(rec->len));
265
266 /* Packet checksum */
267 for (i = 0; i < ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1; i++)
268 checksum += packet[i];
269 packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
270
271 ret = ziirave_firm_write_block_data(wdd, ZIIRAVE_CMD_DOWNLOAD_PACKET,
272 ARRAY_SIZE(packet), packet, true);
273 if (ret)
274 dev_err(&client->dev,
275 "Failed to write firmware packet at address 0x%04x: %d\n",
276 addr, ret);
277
278 return ret;
279}
280
281static int ziirave_firm_verify(struct watchdog_device *wdd,
282 const struct firmware *fw)
283{
284 struct i2c_client *client = to_i2c_client(wdd->parent);
285 const struct ihex_binrec *rec;
286 int i, ret;
287 u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
288 u16 addr;
289
290 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
291 /* Zero length marks end of records */
292 if (!be16_to_cpu(rec->len))
293 break;
294
295 addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
296 if (addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
297 addr > ZIIRAVE_FIRM_FLASH_MEMORY_END)
298 continue;
299
300 ret = ziirave_firm_set_read_addr(wdd, addr);
301 if (ret) {
302 dev_err(&client->dev,
303 "Failed to send SET_READ_ADDR command: %d\n",
304 ret);
305 return ret;
306 }
307
308 for (i = 0; i < ARRAY_SIZE(data); i++) {
309 ret = i2c_smbus_read_byte_data(client,
310 ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
311 if (ret < 0) {
312 dev_err(&client->dev,
313 "Failed to READ DATA: %d\n", ret);
314 return ret;
315 }
316 data[i] = ret;
317 }
318
319 if (memcmp(data, rec->data, be16_to_cpu(rec->len))) {
320 dev_err(&client->dev,
321 "Firmware mismatch at address 0x%04x\n", addr);
322 return -EINVAL;
323 }
324 }
325
326 return 0;
327}
328
329static int ziirave_firm_upload(struct watchdog_device *wdd,
330 const struct firmware *fw)
331{
332 struct i2c_client *client = to_i2c_client(wdd->parent);
333 int ret, words_till_page_break;
334 const struct ihex_binrec *rec;
335 struct ihex_binrec *rec_new;
336
337 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_JUMP_TO_BOOTLOADER, 1,
338 false);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700339 if (ret) {
340 dev_err(&client->dev, "Failed to jump to bootloader\n");
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200341 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700342 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200343
344 msleep(500);
345
346 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_START, 1, true);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700347 if (ret) {
348 dev_err(&client->dev, "Failed to start download\n");
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200349 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700350 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200351
352 msleep(500);
353
354 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
355 /* Zero length marks end of records */
356 if (!be16_to_cpu(rec->len))
357 break;
358
359 /* Check max data size */
360 if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
361 dev_err(&client->dev, "Firmware packet too long (%d)\n",
362 be16_to_cpu(rec->len));
363 return -EMSGSIZE;
364 }
365
366 /* Calculate words till page break */
367 words_till_page_break = (64 - ((be32_to_cpu(rec->addr) >> 1) &
368 0x3f));
369 if ((be16_to_cpu(rec->len) >> 1) > words_till_page_break) {
370 /*
371 * Data in passes page boundary, so we need to split in
372 * two blocks of data. Create a packet with the first
373 * block of data.
374 */
375 rec_new = kzalloc(sizeof(struct ihex_binrec) +
376 (words_till_page_break << 1),
377 GFP_KERNEL);
378 if (!rec_new)
379 return -ENOMEM;
380
381 rec_new->len = cpu_to_be16(words_till_page_break << 1);
382 rec_new->addr = rec->addr;
383 memcpy(rec_new->data, rec->data,
384 be16_to_cpu(rec_new->len));
385
386 ret = ziirave_firm_write_pkt(wdd, rec_new);
387 kfree(rec_new);
388 if (ret)
389 return ret;
390
391 /* Create a packet with the second block of data */
392 rec_new = kzalloc(sizeof(struct ihex_binrec) +
393 be16_to_cpu(rec->len) -
394 (words_till_page_break << 1),
395 GFP_KERNEL);
396 if (!rec_new)
397 return -ENOMEM;
398
399 /* Remaining bytes */
400 rec_new->len = rec->len -
401 cpu_to_be16(words_till_page_break << 1);
402
403 rec_new->addr = cpu_to_be32(be32_to_cpu(rec->addr) +
404 (words_till_page_break << 1));
405
406 memcpy(rec_new->data,
407 rec->data + (words_till_page_break << 1),
408 be16_to_cpu(rec_new->len));
409
410 ret = ziirave_firm_write_pkt(wdd, rec_new);
411 kfree(rec_new);
412 if (ret)
413 return ret;
414 } else {
415 ret = ziirave_firm_write_pkt(wdd, rec);
416 if (ret)
417 return ret;
418 }
419 }
420
421 /* For end of download, the length field will be set to 0 */
422 rec_new = kzalloc(sizeof(struct ihex_binrec) + 1, GFP_KERNEL);
423 if (!rec_new)
424 return -ENOMEM;
425
426 ret = ziirave_firm_write_pkt(wdd, rec_new);
427 kfree(rec_new);
428 if (ret) {
429 dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
430 return ret;
431 }
432
433 /* This sleep seems to be required */
434 msleep(20);
435
436 /* Start firmware verification */
437 ret = ziirave_firm_verify(wdd, fw);
438 if (ret) {
439 dev_err(&client->dev,
440 "Failed to verify firmware: %d\n", ret);
441 return ret;
442 }
443
444 /* End download operation */
445 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_END, 1, false);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700446 if (ret) {
447 dev_err(&client->dev,
448 "Failed to end firmware download: %d\n", ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200449 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700450 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200451
452 /* Reset the processor */
453 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_RESET_PROCESSOR, 1,
454 false);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700455 if (ret) {
456 dev_err(&client->dev,
457 "Failed to reset the watchdog: %d\n", ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200458 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700459 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200460
461 msleep(500);
462
463 return 0;
464}
465
Martyn Welch2a7b7532015-12-01 15:32:47 +0000466static const struct watchdog_info ziirave_wdt_info = {
467 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
468 .identity = "Zodiac RAVE Watchdog",
469};
470
471static const struct watchdog_ops ziirave_wdt_ops = {
472 .owner = THIS_MODULE,
473 .start = ziirave_wdt_start,
474 .stop = ziirave_wdt_stop,
475 .ping = ziirave_wdt_ping,
476 .set_timeout = ziirave_wdt_set_timeout,
477 .get_timeleft = ziirave_wdt_get_timeleft,
478};
479
480static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
481 struct device_attribute *attr,
482 char *buf)
483{
484 struct i2c_client *client = to_i2c_client(dev->parent);
485 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200486 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000487
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200488 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
489 if (ret)
490 return ret;
491
492 ret = sprintf(buf, "02.%02u.%02u", w_priv->firmware_rev.major,
493 w_priv->firmware_rev.minor);
494
495 mutex_unlock(&w_priv->sysfs_mutex);
496
497 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000498}
499
500static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
501 NULL);
502
503static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
504 struct device_attribute *attr,
505 char *buf)
506{
507 struct i2c_client *client = to_i2c_client(dev->parent);
508 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200509 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000510
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200511 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
512 if (ret)
513 return ret;
514
515 ret = sprintf(buf, "01.%02u.%02u", w_priv->bootloader_rev.major,
516 w_priv->bootloader_rev.minor);
517
518 mutex_unlock(&w_priv->sysfs_mutex);
519
520 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000521}
522
523static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
524 NULL);
525
526static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
527 struct device_attribute *attr,
528 char *buf)
529{
530 struct i2c_client *client = to_i2c_client(dev->parent);
531 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200532 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000533
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200534 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
535 if (ret)
536 return ret;
537
538 ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
539
540 mutex_unlock(&w_priv->sysfs_mutex);
541
542 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000543}
544
545static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
546 NULL);
547
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200548static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
549 struct device_attribute *attr,
550 const char *buf, size_t count)
551{
552 struct i2c_client *client = to_i2c_client(dev->parent);
553 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
554 const struct firmware *fw;
555 int err;
556
557 err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
558 if (err) {
559 dev_err(&client->dev, "Failed to request ihex firmware\n");
560 return err;
561 }
562
563 err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
564 if (err)
565 goto release_firmware;
566
567 err = ziirave_firm_upload(&w_priv->wdd, fw);
568 if (err) {
569 dev_err(&client->dev, "The firmware update failed: %d\n", err);
570 goto unlock_mutex;
571 }
572
573 /* Update firmware version */
574 err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
575 ZIIRAVE_WDT_FIRM_VER_MAJOR);
576 if (err) {
577 dev_err(&client->dev, "Failed to read firmware version: %d\n",
578 err);
579 goto unlock_mutex;
580 }
581
582 dev_info(&client->dev, "Firmware updated to version 02.%02u.%02u\n",
583 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
584
585 /* Restore the watchdog timeout */
586 err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
587 if (err)
588 dev_err(&client->dev, "Failed to set timeout: %d\n", err);
589
590unlock_mutex:
591 mutex_unlock(&w_priv->sysfs_mutex);
592
593release_firmware:
594 release_firmware(fw);
595
596 return err ? err : count;
597}
598
599static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
600 ziirave_wdt_sysfs_store_firm);
601
Martyn Welch2a7b7532015-12-01 15:32:47 +0000602static struct attribute *ziirave_wdt_attrs[] = {
603 &dev_attr_firmware_version.attr,
604 &dev_attr_bootloader_version.attr,
605 &dev_attr_reset_reason.attr,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200606 &dev_attr_update_firmware.attr,
Martyn Welch2a7b7532015-12-01 15:32:47 +0000607 NULL
608};
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800609ATTRIBUTE_GROUPS(ziirave_wdt);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000610
611static int ziirave_wdt_init_duration(struct i2c_client *client)
612{
613 int ret;
614
615 if (!reset_duration) {
616 /* See if the reset pulse duration is provided in an of_node */
617 if (!client->dev.of_node)
618 ret = -ENODEV;
619 else
620 ret = of_property_read_u32(client->dev.of_node,
621 "reset-duration-ms",
622 &reset_duration);
623 if (ret) {
624 dev_info(&client->dev,
625 "Unable to set reset pulse duration, using default\n");
626 return 0;
627 }
628 }
629
630 if (reset_duration < 1 || reset_duration > 255)
631 return -EINVAL;
632
633 dev_info(&client->dev, "Setting reset duration to %dms",
634 reset_duration);
635
636 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
637 reset_duration);
638}
639
640static int ziirave_wdt_probe(struct i2c_client *client,
641 const struct i2c_device_id *id)
642{
643 int ret;
644 struct ziirave_wdt_data *w_priv;
645 int val;
646
647 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
648 return -ENODEV;
649
650 w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
651 if (!w_priv)
652 return -ENOMEM;
653
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200654 mutex_init(&w_priv->sysfs_mutex);
655
Martyn Welch2a7b7532015-12-01 15:32:47 +0000656 w_priv->wdd.info = &ziirave_wdt_info;
657 w_priv->wdd.ops = &ziirave_wdt_ops;
658 w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
659 w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
660 w_priv->wdd.parent = &client->dev;
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800661 w_priv->wdd.groups = ziirave_wdt_groups;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000662
Wolfram Sang15451162019-04-19 20:16:01 +0200663 watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000664
665 /*
666 * The default value set in the watchdog should be perfectly valid, so
667 * pass that in if we haven't provided one via the module parameter or
668 * of property.
669 */
670 if (w_priv->wdd.timeout == 0) {
671 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700672 if (val < 0) {
673 dev_err(&client->dev, "Failed to read timeout\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000674 return val;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700675 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000676
Andrey Smirnov39d03872019-08-12 13:08:48 -0700677 if (val > ZIIRAVE_TIMEOUT_MAX ||
678 val < ZIIRAVE_TIMEOUT_MIN)
679 val = ZIIRAVE_TIMEOUT_DEFAULT;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000680
681 w_priv->wdd.timeout = val;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000682 }
683
Andrey Smirnov39d03872019-08-12 13:08:48 -0700684 ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
685 if (ret) {
686 dev_err(&client->dev, "Failed to set timeout\n");
687 return ret;
688 }
689
690 dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
691
Martyn Welch2a7b7532015-12-01 15:32:47 +0000692 watchdog_set_nowayout(&w_priv->wdd, nowayout);
693
694 i2c_set_clientdata(client, w_priv);
695
696 /* If in unconfigured state, set to stopped */
697 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700698 if (val < 0) {
699 dev_err(&client->dev, "Failed to read state\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000700 return val;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700701 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000702
703 if (val == ZIIRAVE_STATE_INITIAL)
704 ziirave_wdt_stop(&w_priv->wdd);
705
706 ret = ziirave_wdt_init_duration(client);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700707 if (ret) {
708 dev_err(&client->dev, "Failed to init duration\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000709 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700710 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000711
712 ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
713 ZIIRAVE_WDT_FIRM_VER_MAJOR);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700714 if (ret) {
715 dev_err(&client->dev, "Failed to read firmware version\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000716 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700717 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000718
719 ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
720 ZIIRAVE_WDT_BOOT_VER_MAJOR);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700721 if (ret) {
722 dev_err(&client->dev, "Failed to read bootloader version\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000723 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700724 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000725
726 w_priv->reset_reason = i2c_smbus_read_byte_data(client,
727 ZIIRAVE_WDT_RESET_REASON);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700728 if (w_priv->reset_reason < 0) {
729 dev_err(&client->dev, "Failed to read reset reason\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000730 return w_priv->reset_reason;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700731 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000732
733 if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700734 !ziirave_reasons[w_priv->reset_reason]) {
735 dev_err(&client->dev, "Invalid reset reason\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000736 return -ENODEV;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700737 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000738
739 ret = watchdog_register_device(&w_priv->wdd);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000740
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800741 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000742}
743
744static int ziirave_wdt_remove(struct i2c_client *client)
745{
746 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
747
Martyn Welch2a7b7532015-12-01 15:32:47 +0000748 watchdog_unregister_device(&w_priv->wdd);
749
750 return 0;
751}
752
Arvind Yadav68c82be2017-08-21 22:18:38 +0530753static const struct i2c_device_id ziirave_wdt_id[] = {
Enric Balletbo i Serra22daf7a2016-07-09 11:43:19 +0200754 { "rave-wdt", 0 },
Martyn Welch2a7b7532015-12-01 15:32:47 +0000755 { }
756};
757MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
758
759static const struct of_device_id zrv_wdt_of_match[] = {
760 { .compatible = "zii,rave-wdt", },
761 { },
762};
763MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
764
765static struct i2c_driver ziirave_wdt_driver = {
766 .driver = {
767 .name = "ziirave_wdt",
768 .of_match_table = zrv_wdt_of_match,
769 },
770 .probe = ziirave_wdt_probe,
771 .remove = ziirave_wdt_remove,
772 .id_table = ziirave_wdt_id,
773};
774
775module_i2c_driver(ziirave_wdt_driver);
776
777MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
778MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
779MODULE_LICENSE("GPL");