blob: 0185b9175cc0b039c5a4c9ea7e856ab5173bfd8e [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
Andrey Smirnov08f980a2019-08-12 13:08:55 -070024#include <asm/unaligned.h>
25
Martyn Welch2a7b7532015-12-01 15:32:47 +000026#define ZIIRAVE_TIMEOUT_MIN 3
27#define ZIIRAVE_TIMEOUT_MAX 255
Andrey Smirnov39d03872019-08-12 13:08:48 -070028#define ZIIRAVE_TIMEOUT_DEFAULT 30
Martyn Welch2a7b7532015-12-01 15:32:47 +000029
30#define ZIIRAVE_PING_VALUE 0x0
31
32#define ZIIRAVE_STATE_INITIAL 0x0
33#define ZIIRAVE_STATE_OFF 0x1
34#define ZIIRAVE_STATE_ON 0x2
35
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020036#define ZIIRAVE_FW_NAME "ziirave_wdt.fw"
37
Martyn Welch0ce72f32016-02-26 16:05:12 +000038static char *ziirave_reasons[] = {"power cycle", "hw watchdog", NULL, NULL,
Martyn Welch2a7b7532015-12-01 15:32:47 +000039 "host request", NULL, "illegal configuration",
40 "illegal instruction", "illegal trap",
41 "unknown"};
42
43#define ZIIRAVE_WDT_FIRM_VER_MAJOR 0x1
44#define ZIIRAVE_WDT_BOOT_VER_MAJOR 0x3
45#define ZIIRAVE_WDT_RESET_REASON 0x5
46#define ZIIRAVE_WDT_STATE 0x6
47#define ZIIRAVE_WDT_TIMEOUT 0x7
48#define ZIIRAVE_WDT_TIME_LEFT 0x8
49#define ZIIRAVE_WDT_PING 0x9
50#define ZIIRAVE_WDT_RESET_DURATION 0xa
51
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020052#define ZIIRAVE_FIRM_PKT_TOTAL_SIZE 20
53#define ZIIRAVE_FIRM_PKT_DATA_SIZE 16
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -070054#define ZIIRAVE_FIRM_FLASH_MEMORY_START (2 * 0x1600)
55#define ZIIRAVE_FIRM_FLASH_MEMORY_END (2 * 0x2bbf)
Andrey Smirnov5870f492019-08-12 13:08:50 -070056#define ZIIRAVE_FIRM_PAGE_SIZE 128
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020057
58/* Received and ready for next Download packet. */
59#define ZIIRAVE_FIRM_DOWNLOAD_ACK 1
60/* Currently writing to flash. Retry Download status in a moment! */
61#define ZIIRAVE_FIRM_DOWNLOAD_BUSY 2
62
63/* Wait for ACK timeout in ms */
64#define ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT 50
65
66/* Firmware commands */
67#define ZIIRAVE_CMD_DOWNLOAD_START 0x10
68#define ZIIRAVE_CMD_DOWNLOAD_END 0x11
69#define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR 0x12
70#define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE 0x13
71#define ZIIRAVE_CMD_RESET_PROCESSOR 0x0b
72#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER 0x0c
73#define ZIIRAVE_CMD_DOWNLOAD_PACKET 0x0e
74
Andrey Smirnov910d0f92019-08-12 13:09:00 -070075#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC 1
76
Andrey Smirnov42abc122019-08-12 13:08:49 -070077#define ZIIRAVE_FW_VERSION_FMT "02.%02u.%02u"
78#define ZIIRAVE_BL_VERSION_FMT "01.%02u.%02u"
79
Martyn Welch2a7b7532015-12-01 15:32:47 +000080struct ziirave_wdt_rev {
81 unsigned char major;
82 unsigned char minor;
83};
84
85struct ziirave_wdt_data {
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020086 struct mutex sysfs_mutex;
Martyn Welch2a7b7532015-12-01 15:32:47 +000087 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
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200177static 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
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700200static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u32 addr)
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200201{
202 struct i2c_client *client = to_i2c_client(wdd->parent);
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700203 const u16 addr16 = (u16)addr / 2;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200204 u8 address[2];
205
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700206 put_unaligned_le16(addr16, address);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200207
208 return i2c_smbus_write_block_data(client,
209 ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
Andrey Smirnove6bd4482019-08-12 13:08:53 -0700210 sizeof(address), address);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200211}
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
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700240static bool ziirave_firm_addr_readonly(u32 addr)
241{
242 return addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
243 addr > ZIIRAVE_FIRM_FLASH_MEMORY_END;
244}
245
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200246/*
247 * ziirave_firm_write_pkt() - Build and write a firmware packet
248 *
249 * A packet to send to the firmware is composed by following bytes:
250 * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
251 * Where,
252 * Length: A data byte containing the length of the data.
253 * Addr0: Low byte of the address.
254 * Addr1: High byte of the address.
255 * Data0 .. Data15: Array of 16 bytes of data.
256 * Checksum: Checksum byte to verify data integrity.
257 */
Andrey Smirnov5870f492019-08-12 13:08:50 -0700258static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
259 u32 addr, const u8 *data, u8 len)
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200260{
Andrey Smirnov5870f492019-08-12 13:08:50 -0700261 const u16 addr16 = (u16)addr / 2;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200262 struct i2c_client *client = to_i2c_client(wdd->parent);
263 u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
264 int ret;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200265
Andrey Smirnov08188e82019-08-12 13:08:51 -0700266 /* Check max data size */
267 if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
268 dev_err(&client->dev, "Firmware packet too long (%d)\n",
269 len);
270 return -EMSGSIZE;
271 }
272
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700273 /*
274 * Ignore packets that are targeting program memory outisde of
275 * app partition, since they will be ignored by the
276 * bootloader. At the same time, we need to make sure we'll
277 * allow zero length packet that will be sent as the last step
278 * of firmware update
279 */
280 if (len && ziirave_firm_addr_readonly(addr))
281 return 0;
282
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200283 /* Packet length */
Andrey Smirnov5870f492019-08-12 13:08:50 -0700284 packet[0] = len;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200285 /* Packet address */
Andrey Smirnov08f980a2019-08-12 13:08:55 -0700286 put_unaligned_le16(addr16, packet + 1);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200287
Andrey Smirnov5870f492019-08-12 13:08:50 -0700288 memcpy(packet + 3, data, len);
Andrey Smirnov10f98fe2019-08-12 13:08:54 -0700289 memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200290
291 /* Packet checksum */
Andrey Smirnovdc0dd282019-08-12 13:08:52 -0700292 for (i = 0; i < len + 3; i++)
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200293 checksum += packet[i];
294 packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
295
296 ret = ziirave_firm_write_block_data(wdd, ZIIRAVE_CMD_DOWNLOAD_PACKET,
Andrey Smirnove6bd4482019-08-12 13:08:53 -0700297 sizeof(packet), packet, true);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200298 if (ret)
299 dev_err(&client->dev,
300 "Failed to write firmware packet at address 0x%04x: %d\n",
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700301 addr, ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200302
303 return ret;
304}
305
Andrey Smirnov5870f492019-08-12 13:08:50 -0700306static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
307 u32 addr, const u8 *data, u8 len)
308{
309 const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
310 (addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
311 int ret;
312
313 if (len > max_write_len) {
314 /*
315 * If data crossed page boundary we need to split this
316 * write in two
317 */
318 ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
319 if (ret)
320 return ret;
321
322 addr += max_write_len;
323 data += max_write_len;
324 len -= max_write_len;
325 }
326
327 return __ziirave_firm_write_pkt(wdd, addr, data, len);
328}
329
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200330static int ziirave_firm_verify(struct watchdog_device *wdd,
331 const struct firmware *fw)
332{
333 struct i2c_client *client = to_i2c_client(wdd->parent);
334 const struct ihex_binrec *rec;
335 int i, ret;
336 u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200337
338 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
Andrey Smirnovde880532019-08-12 13:08:57 -0700339 const u16 len = be16_to_cpu(rec->len);
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700340 const u32 addr = be32_to_cpu(rec->addr);
Andrey Smirnovde880532019-08-12 13:08:57 -0700341
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700342 if (ziirave_firm_addr_readonly(addr))
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200343 continue;
344
345 ret = ziirave_firm_set_read_addr(wdd, addr);
346 if (ret) {
347 dev_err(&client->dev,
348 "Failed to send SET_READ_ADDR command: %d\n",
349 ret);
350 return ret;
351 }
352
Andrey Smirnovde880532019-08-12 13:08:57 -0700353 for (i = 0; i < len; i++) {
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200354 ret = i2c_smbus_read_byte_data(client,
355 ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
356 if (ret < 0) {
357 dev_err(&client->dev,
358 "Failed to READ DATA: %d\n", ret);
359 return ret;
360 }
361 data[i] = ret;
362 }
363
Andrey Smirnovde880532019-08-12 13:08:57 -0700364 if (memcmp(data, rec->data, len)) {
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200365 dev_err(&client->dev,
366 "Firmware mismatch at address 0x%04x\n", addr);
367 return -EINVAL;
368 }
369 }
370
371 return 0;
372}
373
374static int ziirave_firm_upload(struct watchdog_device *wdd,
375 const struct firmware *fw)
376{
377 struct i2c_client *client = to_i2c_client(wdd->parent);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200378 const struct ihex_binrec *rec;
Andrey Smirnov5870f492019-08-12 13:08:50 -0700379 int ret;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200380
Andrey Smirnov910d0f92019-08-12 13:09:00 -0700381 ret = i2c_smbus_write_byte_data(client,
382 ZIIRAVE_CMD_JUMP_TO_BOOTLOADER,
383 ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700384 if (ret) {
385 dev_err(&client->dev, "Failed to jump to bootloader\n");
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200386 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700387 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200388
389 msleep(500);
390
391 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_START, 1, true);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700392 if (ret) {
393 dev_err(&client->dev, "Failed to start download\n");
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200394 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700395 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200396
397 msleep(500);
398
399 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
Andrey Smirnov5870f492019-08-12 13:08:50 -0700400 ret = ziirave_firm_write_pkt(wdd, be32_to_cpu(rec->addr),
401 rec->data, be16_to_cpu(rec->len));
402 if (ret)
403 return ret;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200404 }
405
Andrey Smirnov5870f492019-08-12 13:08:50 -0700406 /*
407 * Finish firmware download process by sending a zero length
408 * payload
409 */
410 ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200411 if (ret) {
412 dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
413 return ret;
414 }
415
416 /* This sleep seems to be required */
417 msleep(20);
418
419 /* Start firmware verification */
420 ret = ziirave_firm_verify(wdd, fw);
421 if (ret) {
422 dev_err(&client->dev,
423 "Failed to verify firmware: %d\n", ret);
424 return ret;
425 }
426
427 /* End download operation */
428 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_END, 1, false);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700429 if (ret) {
430 dev_err(&client->dev,
431 "Failed to end firmware download: %d\n", ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200432 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700433 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200434
435 /* Reset the processor */
436 ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_RESET_PROCESSOR, 1,
437 false);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700438 if (ret) {
439 dev_err(&client->dev,
440 "Failed to reset the watchdog: %d\n", ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200441 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700442 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200443
444 msleep(500);
445
446 return 0;
447}
448
Martyn Welch2a7b7532015-12-01 15:32:47 +0000449static const struct watchdog_info ziirave_wdt_info = {
450 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
451 .identity = "Zodiac RAVE Watchdog",
452};
453
454static const struct watchdog_ops ziirave_wdt_ops = {
455 .owner = THIS_MODULE,
456 .start = ziirave_wdt_start,
457 .stop = ziirave_wdt_stop,
458 .ping = ziirave_wdt_ping,
459 .set_timeout = ziirave_wdt_set_timeout,
460 .get_timeleft = ziirave_wdt_get_timeleft,
461};
462
463static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
464 struct device_attribute *attr,
465 char *buf)
466{
467 struct i2c_client *client = to_i2c_client(dev->parent);
468 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200469 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000470
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200471 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
472 if (ret)
473 return ret;
474
Andrey Smirnov42abc122019-08-12 13:08:49 -0700475 ret = sprintf(buf, ZIIRAVE_FW_VERSION_FMT, w_priv->firmware_rev.major,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200476 w_priv->firmware_rev.minor);
477
478 mutex_unlock(&w_priv->sysfs_mutex);
479
480 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000481}
482
483static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
484 NULL);
485
486static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
487 struct device_attribute *attr,
488 char *buf)
489{
490 struct i2c_client *client = to_i2c_client(dev->parent);
491 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200492 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000493
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200494 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
495 if (ret)
496 return ret;
497
Andrey Smirnov42abc122019-08-12 13:08:49 -0700498 ret = sprintf(buf, ZIIRAVE_BL_VERSION_FMT, w_priv->bootloader_rev.major,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200499 w_priv->bootloader_rev.minor);
500
501 mutex_unlock(&w_priv->sysfs_mutex);
502
503 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000504}
505
506static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
507 NULL);
508
509static ssize_t ziirave_wdt_sysfs_show_reason(struct device *dev,
510 struct device_attribute *attr,
511 char *buf)
512{
513 struct i2c_client *client = to_i2c_client(dev->parent);
514 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200515 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000516
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200517 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
518 if (ret)
519 return ret;
520
521 ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
522
523 mutex_unlock(&w_priv->sysfs_mutex);
524
525 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000526}
527
528static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
529 NULL);
530
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200531static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
532 struct device_attribute *attr,
533 const char *buf, size_t count)
534{
535 struct i2c_client *client = to_i2c_client(dev->parent);
536 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
537 const struct firmware *fw;
538 int err;
539
540 err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
541 if (err) {
542 dev_err(&client->dev, "Failed to request ihex firmware\n");
543 return err;
544 }
545
546 err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
547 if (err)
548 goto release_firmware;
549
550 err = ziirave_firm_upload(&w_priv->wdd, fw);
551 if (err) {
552 dev_err(&client->dev, "The firmware update failed: %d\n", err);
553 goto unlock_mutex;
554 }
555
556 /* Update firmware version */
557 err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
558 ZIIRAVE_WDT_FIRM_VER_MAJOR);
559 if (err) {
560 dev_err(&client->dev, "Failed to read firmware version: %d\n",
561 err);
562 goto unlock_mutex;
563 }
564
Andrey Smirnov42abc122019-08-12 13:08:49 -0700565 dev_info(&client->dev,
566 "Firmware updated to version " ZIIRAVE_FW_VERSION_FMT "\n",
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200567 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
568
569 /* Restore the watchdog timeout */
570 err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
571 if (err)
572 dev_err(&client->dev, "Failed to set timeout: %d\n", err);
573
574unlock_mutex:
575 mutex_unlock(&w_priv->sysfs_mutex);
576
577release_firmware:
578 release_firmware(fw);
579
580 return err ? err : count;
581}
582
583static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
584 ziirave_wdt_sysfs_store_firm);
585
Martyn Welch2a7b7532015-12-01 15:32:47 +0000586static struct attribute *ziirave_wdt_attrs[] = {
587 &dev_attr_firmware_version.attr,
588 &dev_attr_bootloader_version.attr,
589 &dev_attr_reset_reason.attr,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200590 &dev_attr_update_firmware.attr,
Martyn Welch2a7b7532015-12-01 15:32:47 +0000591 NULL
592};
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800593ATTRIBUTE_GROUPS(ziirave_wdt);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000594
595static int ziirave_wdt_init_duration(struct i2c_client *client)
596{
597 int ret;
598
599 if (!reset_duration) {
600 /* See if the reset pulse duration is provided in an of_node */
601 if (!client->dev.of_node)
602 ret = -ENODEV;
603 else
604 ret = of_property_read_u32(client->dev.of_node,
605 "reset-duration-ms",
606 &reset_duration);
607 if (ret) {
608 dev_info(&client->dev,
Andrey Smirnovd2c1d422019-08-12 13:08:59 -0700609 "No reset pulse duration specified, using default\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000610 return 0;
611 }
612 }
613
614 if (reset_duration < 1 || reset_duration > 255)
615 return -EINVAL;
616
617 dev_info(&client->dev, "Setting reset duration to %dms",
618 reset_duration);
619
620 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
621 reset_duration);
622}
623
624static int ziirave_wdt_probe(struct i2c_client *client,
625 const struct i2c_device_id *id)
626{
627 int ret;
628 struct ziirave_wdt_data *w_priv;
629 int val;
630
631 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
632 return -ENODEV;
633
634 w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
635 if (!w_priv)
636 return -ENOMEM;
637
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200638 mutex_init(&w_priv->sysfs_mutex);
639
Martyn Welch2a7b7532015-12-01 15:32:47 +0000640 w_priv->wdd.info = &ziirave_wdt_info;
641 w_priv->wdd.ops = &ziirave_wdt_ops;
642 w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
643 w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
644 w_priv->wdd.parent = &client->dev;
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800645 w_priv->wdd.groups = ziirave_wdt_groups;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000646
Wolfram Sang15451162019-04-19 20:16:01 +0200647 watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000648
649 /*
650 * The default value set in the watchdog should be perfectly valid, so
651 * pass that in if we haven't provided one via the module parameter or
652 * of property.
653 */
654 if (w_priv->wdd.timeout == 0) {
655 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700656 if (val < 0) {
657 dev_err(&client->dev, "Failed to read timeout\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000658 return val;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700659 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000660
Andrey Smirnov39d03872019-08-12 13:08:48 -0700661 if (val > ZIIRAVE_TIMEOUT_MAX ||
662 val < ZIIRAVE_TIMEOUT_MIN)
663 val = ZIIRAVE_TIMEOUT_DEFAULT;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000664
665 w_priv->wdd.timeout = val;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000666 }
667
Andrey Smirnov39d03872019-08-12 13:08:48 -0700668 ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
669 if (ret) {
670 dev_err(&client->dev, "Failed to set timeout\n");
671 return ret;
672 }
673
674 dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
675
Martyn Welch2a7b7532015-12-01 15:32:47 +0000676 watchdog_set_nowayout(&w_priv->wdd, nowayout);
677
678 i2c_set_clientdata(client, w_priv);
679
680 /* If in unconfigured state, set to stopped */
681 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700682 if (val < 0) {
683 dev_err(&client->dev, "Failed to read state\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000684 return val;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700685 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000686
687 if (val == ZIIRAVE_STATE_INITIAL)
688 ziirave_wdt_stop(&w_priv->wdd);
689
690 ret = ziirave_wdt_init_duration(client);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700691 if (ret) {
692 dev_err(&client->dev, "Failed to init duration\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000693 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700694 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000695
696 ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
697 ZIIRAVE_WDT_FIRM_VER_MAJOR);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700698 if (ret) {
699 dev_err(&client->dev, "Failed to read firmware version\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000700 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700701 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000702
Andrey Smirnov42abc122019-08-12 13:08:49 -0700703 dev_info(&client->dev,
704 "Firmware version: " ZIIRAVE_FW_VERSION_FMT "\n",
705 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
706
Martyn Welch2a7b7532015-12-01 15:32:47 +0000707 ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
708 ZIIRAVE_WDT_BOOT_VER_MAJOR);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700709 if (ret) {
710 dev_err(&client->dev, "Failed to read bootloader version\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000711 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700712 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000713
Andrey Smirnov42abc122019-08-12 13:08:49 -0700714 dev_info(&client->dev,
715 "Bootloader version: " ZIIRAVE_BL_VERSION_FMT "\n",
716 w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
717
Martyn Welch2a7b7532015-12-01 15:32:47 +0000718 w_priv->reset_reason = i2c_smbus_read_byte_data(client,
719 ZIIRAVE_WDT_RESET_REASON);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700720 if (w_priv->reset_reason < 0) {
721 dev_err(&client->dev, "Failed to read reset reason\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000722 return w_priv->reset_reason;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700723 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000724
725 if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700726 !ziirave_reasons[w_priv->reset_reason]) {
727 dev_err(&client->dev, "Invalid reset reason\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000728 return -ENODEV;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700729 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000730
731 ret = watchdog_register_device(&w_priv->wdd);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000732
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800733 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000734}
735
736static int ziirave_wdt_remove(struct i2c_client *client)
737{
738 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
739
Martyn Welch2a7b7532015-12-01 15:32:47 +0000740 watchdog_unregister_device(&w_priv->wdd);
741
742 return 0;
743}
744
Arvind Yadav68c82be2017-08-21 22:18:38 +0530745static const struct i2c_device_id ziirave_wdt_id[] = {
Enric Balletbo i Serra22daf7a2016-07-09 11:43:19 +0200746 { "rave-wdt", 0 },
Martyn Welch2a7b7532015-12-01 15:32:47 +0000747 { }
748};
749MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
750
751static const struct of_device_id zrv_wdt_of_match[] = {
752 { .compatible = "zii,rave-wdt", },
753 { },
754};
755MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
756
757static struct i2c_driver ziirave_wdt_driver = {
758 .driver = {
759 .name = "ziirave_wdt",
760 .of_match_table = zrv_wdt_of_match,
761 },
762 .probe = ziirave_wdt_probe,
763 .remove = ziirave_wdt_remove,
764 .id_table = ziirave_wdt_id,
765};
766
767module_i2c_driver(ziirave_wdt_driver);
768
769MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
770MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
771MODULE_LICENSE("GPL");