blob: 75c066602c0009c30c28f5ce06642aa3c3f5aa23 [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
Andrey Smirnov42abc122019-08-12 13:08:49 -070072#define ZIIRAVE_FW_VERSION_FMT "02.%02u.%02u"
73#define ZIIRAVE_BL_VERSION_FMT "01.%02u.%02u"
74
Martyn Welch2a7b7532015-12-01 15:32:47 +000075struct ziirave_wdt_rev {
76 unsigned char major;
77 unsigned char minor;
78};
79
80struct ziirave_wdt_data {
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020081 struct mutex sysfs_mutex;
Martyn Welch2a7b7532015-12-01 15:32:47 +000082 struct watchdog_device wdd;
83 struct ziirave_wdt_rev bootloader_rev;
84 struct ziirave_wdt_rev firmware_rev;
85 int reset_reason;
86};
87
88static int wdt_timeout;
89module_param(wdt_timeout, int, 0);
90MODULE_PARM_DESC(wdt_timeout, "Watchdog timeout in seconds");
91
92static int reset_duration;
93module_param(reset_duration, int, 0);
94MODULE_PARM_DESC(reset_duration,
95 "Watchdog reset pulse duration in milliseconds");
96
97static bool nowayout = WATCHDOG_NOWAYOUT;
98module_param(nowayout, bool, 0);
99MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started default="
100 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
101
102static int ziirave_wdt_revision(struct i2c_client *client,
103 struct ziirave_wdt_rev *rev, u8 command)
104{
105 int ret;
106
107 ret = i2c_smbus_read_byte_data(client, command);
108 if (ret < 0)
109 return ret;
110
111 rev->major = ret;
112
113 ret = i2c_smbus_read_byte_data(client, command + 1);
114 if (ret < 0)
115 return ret;
116
117 rev->minor = ret;
118
119 return 0;
120}
121
122static int ziirave_wdt_set_state(struct watchdog_device *wdd, int state)
123{
124 struct i2c_client *client = to_i2c_client(wdd->parent);
125
126 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_STATE, state);
127}
128
129static int ziirave_wdt_start(struct watchdog_device *wdd)
130{
131 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_ON);
132}
133
134static int ziirave_wdt_stop(struct watchdog_device *wdd)
135{
136 return ziirave_wdt_set_state(wdd, ZIIRAVE_STATE_OFF);
137}
138
139static int ziirave_wdt_ping(struct watchdog_device *wdd)
140{
141 struct i2c_client *client = to_i2c_client(wdd->parent);
142
143 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_PING,
144 ZIIRAVE_PING_VALUE);
145}
146
147static int ziirave_wdt_set_timeout(struct watchdog_device *wdd,
148 unsigned int timeout)
149{
150 struct i2c_client *client = to_i2c_client(wdd->parent);
151 int ret;
152
153 ret = i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_TIMEOUT, timeout);
154 if (!ret)
155 wdd->timeout = timeout;
156
157 return ret;
158}
159
160static unsigned int ziirave_wdt_get_timeleft(struct watchdog_device *wdd)
161{
162 struct i2c_client *client = to_i2c_client(wdd->parent);
163 int ret;
164
165 ret = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIME_LEFT);
166 if (ret < 0)
167 ret = 0;
168
169 return ret;
170}
171
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200172static int ziirave_firm_wait_for_ack(struct watchdog_device *wdd)
173{
174 struct i2c_client *client = to_i2c_client(wdd->parent);
175 int ret;
176 unsigned long timeout;
177
178 timeout = jiffies + msecs_to_jiffies(ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT);
179 do {
180 if (time_after(jiffies, timeout))
181 return -ETIMEDOUT;
182
183 usleep_range(5000, 10000);
184
185 ret = i2c_smbus_read_byte(client);
186 if (ret < 0) {
187 dev_err(&client->dev, "Failed to read byte\n");
188 return ret;
189 }
190 } while (ret == ZIIRAVE_FIRM_DOWNLOAD_BUSY);
191
192 return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
193}
194
195static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u16 addr)
196{
197 struct i2c_client *client = to_i2c_client(wdd->parent);
198 u8 address[2];
199
200 address[0] = addr & 0xff;
201 address[1] = (addr >> 8) & 0xff;
202
203 return i2c_smbus_write_block_data(client,
204 ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
205 ARRAY_SIZE(address), address);
206}
207
208static int ziirave_firm_write_block_data(struct watchdog_device *wdd,
209 u8 command, u8 length, const u8 *data,
210 bool wait_for_ack)
211{
212 struct i2c_client *client = to_i2c_client(wdd->parent);
213 int ret;
214
215 ret = i2c_smbus_write_block_data(client, command, length, data);
216 if (ret) {
217 dev_err(&client->dev,
218 "Failed to send command 0x%02x: %d\n", command, ret);
219 return ret;
220 }
221
222 if (wait_for_ack)
223 ret = ziirave_firm_wait_for_ack(wdd);
224
225 return ret;
226}
227
228static int ziirave_firm_write_byte(struct watchdog_device *wdd, u8 command,
229 u8 byte, bool wait_for_ack)
230{
231 return ziirave_firm_write_block_data(wdd, command, 1, &byte,
232 wait_for_ack);
233}
234
235/*
236 * ziirave_firm_write_pkt() - Build and write a firmware packet
237 *
238 * A packet to send to the firmware is composed by following bytes:
239 * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
240 * Where,
241 * Length: A data byte containing the length of the data.
242 * Addr0: Low byte of the address.
243 * Addr1: High byte of the address.
244 * Data0 .. Data15: Array of 16 bytes of data.
245 * Checksum: Checksum byte to verify data integrity.
246 */
247static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
248 const struct ihex_binrec *rec)
249{
250 struct i2c_client *client = to_i2c_client(wdd->parent);
251 u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
252 int ret;
253 u16 addr;
254
255 memset(packet, 0, ARRAY_SIZE(packet));
256
257 /* Packet length */
258 packet[0] = (u8)be16_to_cpu(rec->len);
259 /* Packet address */
260 addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
261 packet[1] = addr & 0xff;
262 packet[2] = (addr & 0xff00) >> 8;
263
264 /* Packet data */
265 if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE)
266 return -EMSGSIZE;
267 memcpy(packet + 3, rec->data, be16_to_cpu(rec->len));
268
269 /* Packet checksum */
270 for (i = 0; i < ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1; i++)
271 checksum += packet[i];
272 packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
273
274 ret = ziirave_firm_write_block_data(wdd, ZIIRAVE_CMD_DOWNLOAD_PACKET,
275 ARRAY_SIZE(packet), packet, true);
276 if (ret)
277 dev_err(&client->dev,
278 "Failed to write firmware packet at address 0x%04x: %d\n",
279 addr, ret);
280
281 return ret;
282}
283
284static int ziirave_firm_verify(struct watchdog_device *wdd,
285 const struct firmware *fw)
286{
287 struct i2c_client *client = to_i2c_client(wdd->parent);
288 const struct ihex_binrec *rec;
289 int i, ret;
290 u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
291 u16 addr;
292
293 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
294 /* Zero length marks end of records */
295 if (!be16_to_cpu(rec->len))
296 break;
297
298 addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1;
299 if (addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
300 addr > ZIIRAVE_FIRM_FLASH_MEMORY_END)
301 continue;
302
303 ret = ziirave_firm_set_read_addr(wdd, addr);
304 if (ret) {
305 dev_err(&client->dev,
306 "Failed to send SET_READ_ADDR command: %d\n",
307 ret);
308 return ret;
309 }
310
311 for (i = 0; i < ARRAY_SIZE(data); i++) {
312 ret = i2c_smbus_read_byte_data(client,
313 ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
314 if (ret < 0) {
315 dev_err(&client->dev,
316 "Failed to READ DATA: %d\n", ret);
317 return ret;
318 }
319 data[i] = ret;
320 }
321
322 if (memcmp(data, rec->data, be16_to_cpu(rec->len))) {
323 dev_err(&client->dev,
324 "Firmware mismatch at address 0x%04x\n", addr);
325 return -EINVAL;
326 }
327 }
328
329 return 0;
330}
331
332static int ziirave_firm_upload(struct watchdog_device *wdd,
333 const struct firmware *fw)
334{
335 struct i2c_client *client = to_i2c_client(wdd->parent);
336 int ret, words_till_page_break;
337 const struct ihex_binrec *rec;
338 struct ihex_binrec *rec_new;
339
340 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_JUMP_TO_BOOTLOADER, 1,
341 false);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700342 if (ret) {
343 dev_err(&client->dev, "Failed to jump to bootloader\n");
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200344 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700345 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200346
347 msleep(500);
348
349 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_START, 1, true);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700350 if (ret) {
351 dev_err(&client->dev, "Failed to start download\n");
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200352 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700353 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200354
355 msleep(500);
356
357 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
358 /* Zero length marks end of records */
359 if (!be16_to_cpu(rec->len))
360 break;
361
362 /* Check max data size */
363 if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
364 dev_err(&client->dev, "Firmware packet too long (%d)\n",
365 be16_to_cpu(rec->len));
366 return -EMSGSIZE;
367 }
368
369 /* Calculate words till page break */
370 words_till_page_break = (64 - ((be32_to_cpu(rec->addr) >> 1) &
371 0x3f));
372 if ((be16_to_cpu(rec->len) >> 1) > words_till_page_break) {
373 /*
374 * Data in passes page boundary, so we need to split in
375 * two blocks of data. Create a packet with the first
376 * block of data.
377 */
378 rec_new = kzalloc(sizeof(struct ihex_binrec) +
379 (words_till_page_break << 1),
380 GFP_KERNEL);
381 if (!rec_new)
382 return -ENOMEM;
383
384 rec_new->len = cpu_to_be16(words_till_page_break << 1);
385 rec_new->addr = rec->addr;
386 memcpy(rec_new->data, rec->data,
387 be16_to_cpu(rec_new->len));
388
389 ret = ziirave_firm_write_pkt(wdd, rec_new);
390 kfree(rec_new);
391 if (ret)
392 return ret;
393
394 /* Create a packet with the second block of data */
395 rec_new = kzalloc(sizeof(struct ihex_binrec) +
396 be16_to_cpu(rec->len) -
397 (words_till_page_break << 1),
398 GFP_KERNEL);
399 if (!rec_new)
400 return -ENOMEM;
401
402 /* Remaining bytes */
403 rec_new->len = rec->len -
404 cpu_to_be16(words_till_page_break << 1);
405
406 rec_new->addr = cpu_to_be32(be32_to_cpu(rec->addr) +
407 (words_till_page_break << 1));
408
409 memcpy(rec_new->data,
410 rec->data + (words_till_page_break << 1),
411 be16_to_cpu(rec_new->len));
412
413 ret = ziirave_firm_write_pkt(wdd, rec_new);
414 kfree(rec_new);
415 if (ret)
416 return ret;
417 } else {
418 ret = ziirave_firm_write_pkt(wdd, rec);
419 if (ret)
420 return ret;
421 }
422 }
423
424 /* For end of download, the length field will be set to 0 */
425 rec_new = kzalloc(sizeof(struct ihex_binrec) + 1, GFP_KERNEL);
426 if (!rec_new)
427 return -ENOMEM;
428
429 ret = ziirave_firm_write_pkt(wdd, rec_new);
430 kfree(rec_new);
431 if (ret) {
432 dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
433 return ret;
434 }
435
436 /* This sleep seems to be required */
437 msleep(20);
438
439 /* Start firmware verification */
440 ret = ziirave_firm_verify(wdd, fw);
441 if (ret) {
442 dev_err(&client->dev,
443 "Failed to verify firmware: %d\n", ret);
444 return ret;
445 }
446
447 /* End download operation */
448 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_END, 1, false);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700449 if (ret) {
450 dev_err(&client->dev,
451 "Failed to end firmware download: %d\n", ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200452 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700453 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200454
455 /* Reset the processor */
456 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_RESET_PROCESSOR, 1,
457 false);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700458 if (ret) {
459 dev_err(&client->dev,
460 "Failed to reset the watchdog: %d\n", ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200461 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700462 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200463
464 msleep(500);
465
466 return 0;
467}
468
Martyn Welch2a7b7532015-12-01 15:32:47 +0000469static const struct watchdog_info ziirave_wdt_info = {
470 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
471 .identity = "Zodiac RAVE Watchdog",
472};
473
474static const struct watchdog_ops ziirave_wdt_ops = {
475 .owner = THIS_MODULE,
476 .start = ziirave_wdt_start,
477 .stop = ziirave_wdt_stop,
478 .ping = ziirave_wdt_ping,
479 .set_timeout = ziirave_wdt_set_timeout,
480 .get_timeleft = ziirave_wdt_get_timeleft,
481};
482
483static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
484 struct device_attribute *attr,
485 char *buf)
486{
487 struct i2c_client *client = to_i2c_client(dev->parent);
488 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200489 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000490
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200491 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
492 if (ret)
493 return ret;
494
Andrey Smirnov42abc122019-08-12 13:08:49 -0700495 ret = sprintf(buf, ZIIRAVE_FW_VERSION_FMT, w_priv->firmware_rev.major,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200496 w_priv->firmware_rev.minor);
497
498 mutex_unlock(&w_priv->sysfs_mutex);
499
500 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000501}
502
503static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
504 NULL);
505
506static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
507 struct device_attribute *attr,
508 char *buf)
509{
510 struct i2c_client *client = to_i2c_client(dev->parent);
511 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200512 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000513
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200514 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
515 if (ret)
516 return ret;
517
Andrey Smirnov42abc122019-08-12 13:08:49 -0700518 ret = sprintf(buf, ZIIRAVE_BL_VERSION_FMT, w_priv->bootloader_rev.major,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200519 w_priv->bootloader_rev.minor);
520
521 mutex_unlock(&w_priv->sysfs_mutex);
522
523 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000524}
525
526static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
527 NULL);
528
529static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
530 struct device_attribute *attr,
531 char *buf)
532{
533 struct i2c_client *client = to_i2c_client(dev->parent);
534 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200535 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000536
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200537 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
538 if (ret)
539 return ret;
540
541 ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
542
543 mutex_unlock(&w_priv->sysfs_mutex);
544
545 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000546}
547
548static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
549 NULL);
550
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200551static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
552 struct device_attribute *attr,
553 const char *buf, size_t count)
554{
555 struct i2c_client *client = to_i2c_client(dev->parent);
556 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
557 const struct firmware *fw;
558 int err;
559
560 err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
561 if (err) {
562 dev_err(&client->dev, "Failed to request ihex firmware\n");
563 return err;
564 }
565
566 err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
567 if (err)
568 goto release_firmware;
569
570 err = ziirave_firm_upload(&w_priv->wdd, fw);
571 if (err) {
572 dev_err(&client->dev, "The firmware update failed: %d\n", err);
573 goto unlock_mutex;
574 }
575
576 /* Update firmware version */
577 err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
578 ZIIRAVE_WDT_FIRM_VER_MAJOR);
579 if (err) {
580 dev_err(&client->dev, "Failed to read firmware version: %d\n",
581 err);
582 goto unlock_mutex;
583 }
584
Andrey Smirnov42abc122019-08-12 13:08:49 -0700585 dev_info(&client->dev,
586 "Firmware updated to version " ZIIRAVE_FW_VERSION_FMT "\n",
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200587 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
588
589 /* Restore the watchdog timeout */
590 err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
591 if (err)
592 dev_err(&client->dev, "Failed to set timeout: %d\n", err);
593
594unlock_mutex:
595 mutex_unlock(&w_priv->sysfs_mutex);
596
597release_firmware:
598 release_firmware(fw);
599
600 return err ? err : count;
601}
602
603static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
604 ziirave_wdt_sysfs_store_firm);
605
Martyn Welch2a7b7532015-12-01 15:32:47 +0000606static struct attribute *ziirave_wdt_attrs[] = {
607 &dev_attr_firmware_version.attr,
608 &dev_attr_bootloader_version.attr,
609 &dev_attr_reset_reason.attr,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200610 &dev_attr_update_firmware.attr,
Martyn Welch2a7b7532015-12-01 15:32:47 +0000611 NULL
612};
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800613ATTRIBUTE_GROUPS(ziirave_wdt);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000614
615static int ziirave_wdt_init_duration(struct i2c_client *client)
616{
617 int ret;
618
619 if (!reset_duration) {
620 /* See if the reset pulse duration is provided in an of_node */
621 if (!client->dev.of_node)
622 ret = -ENODEV;
623 else
624 ret = of_property_read_u32(client->dev.of_node,
625 "reset-duration-ms",
626 &reset_duration);
627 if (ret) {
628 dev_info(&client->dev,
629 "Unable to set reset pulse duration, using default\n");
630 return 0;
631 }
632 }
633
634 if (reset_duration < 1 || reset_duration > 255)
635 return -EINVAL;
636
637 dev_info(&client->dev, "Setting reset duration to %dms",
638 reset_duration);
639
640 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
641 reset_duration);
642}
643
644static int ziirave_wdt_probe(struct i2c_client *client,
645 const struct i2c_device_id *id)
646{
647 int ret;
648 struct ziirave_wdt_data *w_priv;
649 int val;
650
651 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
652 return -ENODEV;
653
654 w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
655 if (!w_priv)
656 return -ENOMEM;
657
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200658 mutex_init(&w_priv->sysfs_mutex);
659
Martyn Welch2a7b7532015-12-01 15:32:47 +0000660 w_priv->wdd.info = &ziirave_wdt_info;
661 w_priv->wdd.ops = &ziirave_wdt_ops;
662 w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
663 w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
664 w_priv->wdd.parent = &client->dev;
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800665 w_priv->wdd.groups = ziirave_wdt_groups;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000666
Wolfram Sang15451162019-04-19 20:16:01 +0200667 watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000668
669 /*
670 * The default value set in the watchdog should be perfectly valid, so
671 * pass that in if we haven't provided one via the module parameter or
672 * of property.
673 */
674 if (w_priv->wdd.timeout == 0) {
675 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700676 if (val < 0) {
677 dev_err(&client->dev, "Failed to read timeout\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000678 return val;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700679 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000680
Andrey Smirnov39d03872019-08-12 13:08:48 -0700681 if (val > ZIIRAVE_TIMEOUT_MAX ||
682 val < ZIIRAVE_TIMEOUT_MIN)
683 val = ZIIRAVE_TIMEOUT_DEFAULT;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000684
685 w_priv->wdd.timeout = val;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000686 }
687
Andrey Smirnov39d03872019-08-12 13:08:48 -0700688 ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
689 if (ret) {
690 dev_err(&client->dev, "Failed to set timeout\n");
691 return ret;
692 }
693
694 dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
695
Martyn Welch2a7b7532015-12-01 15:32:47 +0000696 watchdog_set_nowayout(&w_priv->wdd, nowayout);
697
698 i2c_set_clientdata(client, w_priv);
699
700 /* If in unconfigured state, set to stopped */
701 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700702 if (val < 0) {
703 dev_err(&client->dev, "Failed to read state\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000704 return val;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700705 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000706
707 if (val == ZIIRAVE_STATE_INITIAL)
708 ziirave_wdt_stop(&w_priv->wdd);
709
710 ret = ziirave_wdt_init_duration(client);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700711 if (ret) {
712 dev_err(&client->dev, "Failed to init duration\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000713 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700714 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000715
716 ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
717 ZIIRAVE_WDT_FIRM_VER_MAJOR);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700718 if (ret) {
719 dev_err(&client->dev, "Failed to read firmware version\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000720 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700721 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000722
Andrey Smirnov42abc122019-08-12 13:08:49 -0700723 dev_info(&client->dev,
724 "Firmware version: " ZIIRAVE_FW_VERSION_FMT "\n",
725 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
726
Martyn Welch2a7b7532015-12-01 15:32:47 +0000727 ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
728 ZIIRAVE_WDT_BOOT_VER_MAJOR);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700729 if (ret) {
730 dev_err(&client->dev, "Failed to read bootloader version\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000731 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700732 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000733
Andrey Smirnov42abc122019-08-12 13:08:49 -0700734 dev_info(&client->dev,
735 "Bootloader version: " ZIIRAVE_BL_VERSION_FMT "\n",
736 w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
737
Martyn Welch2a7b7532015-12-01 15:32:47 +0000738 w_priv->reset_reason = i2c_smbus_read_byte_data(client,
739 ZIIRAVE_WDT_RESET_REASON);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700740 if (w_priv->reset_reason < 0) {
741 dev_err(&client->dev, "Failed to read reset reason\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000742 return w_priv->reset_reason;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700743 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000744
745 if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700746 !ziirave_reasons[w_priv->reset_reason]) {
747 dev_err(&client->dev, "Invalid reset reason\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000748 return -ENODEV;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700749 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000750
751 ret = watchdog_register_device(&w_priv->wdd);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000752
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800753 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000754}
755
756static int ziirave_wdt_remove(struct i2c_client *client)
757{
758 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
759
Martyn Welch2a7b7532015-12-01 15:32:47 +0000760 watchdog_unregister_device(&w_priv->wdd);
761
762 return 0;
763}
764
Arvind Yadav68c82be2017-08-21 22:18:38 +0530765static const struct i2c_device_id ziirave_wdt_id[] = {
Enric Balletbo i Serra22daf7a2016-07-09 11:43:19 +0200766 { "rave-wdt", 0 },
Martyn Welch2a7b7532015-12-01 15:32:47 +0000767 { }
768};
769MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
770
771static const struct of_device_id zrv_wdt_of_match[] = {
772 { .compatible = "zii,rave-wdt", },
773 { },
774};
775MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
776
777static struct i2c_driver ziirave_wdt_driver = {
778 .driver = {
779 .name = "ziirave_wdt",
780 .of_match_table = zrv_wdt_of_match,
781 },
782 .probe = ziirave_wdt_probe,
783 .remove = ziirave_wdt_remove,
784 .id_table = ziirave_wdt_id,
785};
786
787module_i2c_driver(ziirave_wdt_driver);
788
789MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
790MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
791MODULE_LICENSE("GPL");