blob: cab86a08456bcd3c13bb3e2663ae2facd2221d85 [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
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020060
61/* Firmware commands */
62#define ZIIRAVE_CMD_DOWNLOAD_START 0x10
63#define ZIIRAVE_CMD_DOWNLOAD_END 0x11
64#define ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR 0x12
65#define ZIIRAVE_CMD_DOWNLOAD_READ_BYTE 0x13
66#define ZIIRAVE_CMD_RESET_PROCESSOR 0x0b
67#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER 0x0c
68#define ZIIRAVE_CMD_DOWNLOAD_PACKET 0x0e
69
Andrey Smirnov910d0f92019-08-12 13:09:00 -070070#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC 1
Andrey Smirnov0007cbd2019-08-12 13:09:02 -070071#define ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC 1
Andrey Smirnov910d0f92019-08-12 13:09:00 -070072
Andrey Smirnov42abc122019-08-12 13:08:49 -070073#define ZIIRAVE_FW_VERSION_FMT "02.%02u.%02u"
74#define ZIIRAVE_BL_VERSION_FMT "01.%02u.%02u"
75
Martyn Welch2a7b7532015-12-01 15:32:47 +000076struct ziirave_wdt_rev {
77 unsigned char major;
78 unsigned char minor;
79};
80
81struct ziirave_wdt_data {
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +020082 struct mutex sysfs_mutex;
Martyn Welch2a7b7532015-12-01 15:32:47 +000083 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
Andrey Smirnovfe051782019-08-12 13:09:03 -0700173static int ziirave_firm_read_ack(struct watchdog_device *wdd)
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200174{
175 struct i2c_client *client = to_i2c_client(wdd->parent);
176 int ret;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200177
Andrey Smirnovfe051782019-08-12 13:09:03 -0700178 ret = i2c_smbus_read_byte(client);
179 if (ret < 0) {
180 dev_err(&client->dev, "Failed to read status byte\n");
181 return ret;
182 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200183
184 return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO;
185}
186
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700187static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u32 addr)
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200188{
189 struct i2c_client *client = to_i2c_client(wdd->parent);
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700190 const u16 addr16 = (u16)addr / 2;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200191 u8 address[2];
192
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700193 put_unaligned_le16(addr16, address);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200194
195 return i2c_smbus_write_block_data(client,
196 ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR,
Andrey Smirnove6bd4482019-08-12 13:08:53 -0700197 sizeof(address), address);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200198}
199
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700200static bool ziirave_firm_addr_readonly(u32 addr)
201{
202 return addr < ZIIRAVE_FIRM_FLASH_MEMORY_START ||
203 addr > ZIIRAVE_FIRM_FLASH_MEMORY_END;
204}
205
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200206/*
207 * ziirave_firm_write_pkt() - Build and write a firmware packet
208 *
209 * A packet to send to the firmware is composed by following bytes:
210 * Length | Addr0 | Addr1 | Data0 .. Data15 | Checksum |
211 * Where,
212 * Length: A data byte containing the length of the data.
213 * Addr0: Low byte of the address.
214 * Addr1: High byte of the address.
215 * Data0 .. Data15: Array of 16 bytes of data.
216 * Checksum: Checksum byte to verify data integrity.
217 */
Andrey Smirnov5870f492019-08-12 13:08:50 -0700218static int __ziirave_firm_write_pkt(struct watchdog_device *wdd,
219 u32 addr, const u8 *data, u8 len)
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200220{
Andrey Smirnov5870f492019-08-12 13:08:50 -0700221 const u16 addr16 = (u16)addr / 2;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200222 struct i2c_client *client = to_i2c_client(wdd->parent);
223 u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE];
224 int ret;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200225
Andrey Smirnov08188e82019-08-12 13:08:51 -0700226 /* Check max data size */
227 if (len > ZIIRAVE_FIRM_PKT_DATA_SIZE) {
228 dev_err(&client->dev, "Firmware packet too long (%d)\n",
229 len);
230 return -EMSGSIZE;
231 }
232
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700233 /*
234 * Ignore packets that are targeting program memory outisde of
235 * app partition, since they will be ignored by the
236 * bootloader. At the same time, we need to make sure we'll
237 * allow zero length packet that will be sent as the last step
238 * of firmware update
239 */
240 if (len && ziirave_firm_addr_readonly(addr))
241 return 0;
242
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200243 /* Packet length */
Andrey Smirnov5870f492019-08-12 13:08:50 -0700244 packet[0] = len;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200245 /* Packet address */
Andrey Smirnov08f980a2019-08-12 13:08:55 -0700246 put_unaligned_le16(addr16, packet + 1);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200247
Andrey Smirnov5870f492019-08-12 13:08:50 -0700248 memcpy(packet + 3, data, len);
Andrey Smirnov10f98fe2019-08-12 13:08:54 -0700249 memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200250
251 /* Packet checksum */
Andrey Smirnovdc0dd282019-08-12 13:08:52 -0700252 for (i = 0; i < len + 3; i++)
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200253 checksum += packet[i];
254 packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum;
255
Andrey Smirnov08c913f2019-08-12 13:09:05 -0700256 ret = i2c_smbus_write_block_data(client, ZIIRAVE_CMD_DOWNLOAD_PACKET,
257 sizeof(packet), packet);
258 if (ret) {
259 dev_err(&client->dev,
260 "Failed to send DOWNLOAD_PACKET: %d\n", ret);
261 return ret;
262 }
263
264 ret = ziirave_firm_read_ack(wdd);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200265 if (ret)
266 dev_err(&client->dev,
267 "Failed to write firmware packet at address 0x%04x: %d\n",
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700268 addr, ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200269
270 return ret;
271}
272
Andrey Smirnov5870f492019-08-12 13:08:50 -0700273static int ziirave_firm_write_pkt(struct watchdog_device *wdd,
274 u32 addr, const u8 *data, u8 len)
275{
276 const u8 max_write_len = ZIIRAVE_FIRM_PAGE_SIZE -
277 (addr - ALIGN_DOWN(addr, ZIIRAVE_FIRM_PAGE_SIZE));
278 int ret;
279
280 if (len > max_write_len) {
281 /*
282 * If data crossed page boundary we need to split this
283 * write in two
284 */
285 ret = __ziirave_firm_write_pkt(wdd, addr, data, max_write_len);
286 if (ret)
287 return ret;
288
289 addr += max_write_len;
290 data += max_write_len;
291 len -= max_write_len;
292 }
293
294 return __ziirave_firm_write_pkt(wdd, addr, data, len);
295}
296
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200297static int ziirave_firm_verify(struct watchdog_device *wdd,
298 const struct firmware *fw)
299{
300 struct i2c_client *client = to_i2c_client(wdd->parent);
301 const struct ihex_binrec *rec;
302 int i, ret;
303 u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE];
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200304
305 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
Andrey Smirnovde880532019-08-12 13:08:57 -0700306 const u16 len = be16_to_cpu(rec->len);
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700307 const u32 addr = be32_to_cpu(rec->addr);
Andrey Smirnovde880532019-08-12 13:08:57 -0700308
Andrey Smirnovd2ddc4502019-08-12 13:08:58 -0700309 if (ziirave_firm_addr_readonly(addr))
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200310 continue;
311
312 ret = ziirave_firm_set_read_addr(wdd, addr);
313 if (ret) {
314 dev_err(&client->dev,
315 "Failed to send SET_READ_ADDR command: %d\n",
316 ret);
317 return ret;
318 }
319
Andrey Smirnovde880532019-08-12 13:08:57 -0700320 for (i = 0; i < len; i++) {
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200321 ret = i2c_smbus_read_byte_data(client,
322 ZIIRAVE_CMD_DOWNLOAD_READ_BYTE);
323 if (ret < 0) {
324 dev_err(&client->dev,
325 "Failed to READ DATA: %d\n", ret);
326 return ret;
327 }
328 data[i] = ret;
329 }
330
Andrey Smirnovde880532019-08-12 13:08:57 -0700331 if (memcmp(data, rec->data, len)) {
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200332 dev_err(&client->dev,
333 "Firmware mismatch at address 0x%04x\n", addr);
334 return -EINVAL;
335 }
336 }
337
338 return 0;
339}
340
341static int ziirave_firm_upload(struct watchdog_device *wdd,
342 const struct firmware *fw)
343{
344 struct i2c_client *client = to_i2c_client(wdd->parent);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200345 const struct ihex_binrec *rec;
Andrey Smirnov5870f492019-08-12 13:08:50 -0700346 int ret;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200347
Andrey Smirnov910d0f92019-08-12 13:09:00 -0700348 ret = i2c_smbus_write_byte_data(client,
349 ZIIRAVE_CMD_JUMP_TO_BOOTLOADER,
350 ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700351 if (ret) {
352 dev_err(&client->dev, "Failed to jump to bootloader\n");
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200353 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700354 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200355
356 msleep(500);
357
Andrey Smirnovfa0d2f42019-08-12 13:09:04 -0700358 ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_START);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700359 if (ret) {
360 dev_err(&client->dev, "Failed to start download\n");
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200361 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700362 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200363
Andrey Smirnovfa0d2f42019-08-12 13:09:04 -0700364 ret = ziirave_firm_read_ack(wdd);
365 if (ret) {
366 dev_err(&client->dev, "No ACK for start download\n");
367 return ret;
368 }
369
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200370 msleep(500);
371
372 for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
Andrey Smirnov5870f492019-08-12 13:08:50 -0700373 ret = ziirave_firm_write_pkt(wdd, be32_to_cpu(rec->addr),
374 rec->data, be16_to_cpu(rec->len));
375 if (ret)
376 return ret;
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200377 }
378
Andrey Smirnov5870f492019-08-12 13:08:50 -0700379 /*
380 * Finish firmware download process by sending a zero length
381 * payload
382 */
383 ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200384 if (ret) {
385 dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret);
386 return ret;
387 }
388
389 /* This sleep seems to be required */
390 msleep(20);
391
392 /* Start firmware verification */
393 ret = ziirave_firm_verify(wdd, fw);
394 if (ret) {
395 dev_err(&client->dev,
396 "Failed to verify firmware: %d\n", ret);
397 return ret;
398 }
399
400 /* End download operation */
Andrey Smirnovc47825f2019-08-12 13:09:01 -0700401 ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_END);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700402 if (ret) {
403 dev_err(&client->dev,
404 "Failed to end firmware download: %d\n", ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200405 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700406 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200407
408 /* Reset the processor */
Andrey Smirnov0007cbd2019-08-12 13:09:02 -0700409 ret = i2c_smbus_write_byte_data(client,
410 ZIIRAVE_CMD_RESET_PROCESSOR,
411 ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC);
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700412 if (ret) {
413 dev_err(&client->dev,
414 "Failed to reset the watchdog: %d\n", ret);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200415 return ret;
Andrey Smirnovb774fce2019-08-12 13:08:47 -0700416 }
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200417
418 msleep(500);
419
420 return 0;
421}
422
Martyn Welch2a7b7532015-12-01 15:32:47 +0000423static const struct watchdog_info ziirave_wdt_info = {
424 .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING,
Lucas Stache2ad9bc2020-03-13 11:11:38 +0100425 .identity = "RAVE Switch Watchdog",
Martyn Welch2a7b7532015-12-01 15:32:47 +0000426};
427
428static const struct watchdog_ops ziirave_wdt_ops = {
429 .owner = THIS_MODULE,
430 .start = ziirave_wdt_start,
431 .stop = ziirave_wdt_stop,
432 .ping = ziirave_wdt_ping,
433 .set_timeout = ziirave_wdt_set_timeout,
434 .get_timeleft = ziirave_wdt_get_timeleft,
435};
436
437static ssize_t ziirave_wdt_sysfs_show_firm(struct device *dev,
438 struct device_attribute *attr,
439 char *buf)
440{
441 struct i2c_client *client = to_i2c_client(dev->parent);
442 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200443 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000444
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200445 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
446 if (ret)
447 return ret;
448
Andrey Smirnov42abc122019-08-12 13:08:49 -0700449 ret = sprintf(buf, ZIIRAVE_FW_VERSION_FMT, w_priv->firmware_rev.major,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200450 w_priv->firmware_rev.minor);
451
452 mutex_unlock(&w_priv->sysfs_mutex);
453
454 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000455}
456
457static DEVICE_ATTR(firmware_version, S_IRUGO, ziirave_wdt_sysfs_show_firm,
458 NULL);
459
460static ssize_t ziirave_wdt_sysfs_show_boot(struct device *dev,
461 struct device_attribute *attr,
462 char *buf)
463{
464 struct i2c_client *client = to_i2c_client(dev->parent);
465 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200466 int ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000467
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200468 ret = mutex_lock_interruptible(&w_priv->sysfs_mutex);
469 if (ret)
470 return ret;
471
Andrey Smirnov42abc122019-08-12 13:08:49 -0700472 ret = sprintf(buf, ZIIRAVE_BL_VERSION_FMT, w_priv->bootloader_rev.major,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200473 w_priv->bootloader_rev.minor);
474
475 mutex_unlock(&w_priv->sysfs_mutex);
476
477 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000478}
479
480static DEVICE_ATTR(bootloader_version, S_IRUGO, ziirave_wdt_sysfs_show_boot,
481 NULL);
482
483static ssize_t ziirave_wdt_sysfs_show_reason(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
495 ret = sprintf(buf, "%s", ziirave_reasons[w_priv->reset_reason]);
496
497 mutex_unlock(&w_priv->sysfs_mutex);
498
499 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000500}
501
502static DEVICE_ATTR(reset_reason, S_IRUGO, ziirave_wdt_sysfs_show_reason,
503 NULL);
504
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200505static ssize_t ziirave_wdt_sysfs_store_firm(struct device *dev,
506 struct device_attribute *attr,
507 const char *buf, size_t count)
508{
509 struct i2c_client *client = to_i2c_client(dev->parent);
510 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
511 const struct firmware *fw;
512 int err;
513
514 err = request_ihex_firmware(&fw, ZIIRAVE_FW_NAME, dev);
515 if (err) {
516 dev_err(&client->dev, "Failed to request ihex firmware\n");
517 return err;
518 }
519
520 err = mutex_lock_interruptible(&w_priv->sysfs_mutex);
521 if (err)
522 goto release_firmware;
523
524 err = ziirave_firm_upload(&w_priv->wdd, fw);
525 if (err) {
526 dev_err(&client->dev, "The firmware update failed: %d\n", err);
527 goto unlock_mutex;
528 }
529
530 /* Update firmware version */
531 err = ziirave_wdt_revision(client, &w_priv->firmware_rev,
532 ZIIRAVE_WDT_FIRM_VER_MAJOR);
533 if (err) {
534 dev_err(&client->dev, "Failed to read firmware version: %d\n",
535 err);
536 goto unlock_mutex;
537 }
538
Andrey Smirnov42abc122019-08-12 13:08:49 -0700539 dev_info(&client->dev,
540 "Firmware updated to version " ZIIRAVE_FW_VERSION_FMT "\n",
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200541 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
542
543 /* Restore the watchdog timeout */
544 err = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
545 if (err)
546 dev_err(&client->dev, "Failed to set timeout: %d\n", err);
547
548unlock_mutex:
549 mutex_unlock(&w_priv->sysfs_mutex);
550
551release_firmware:
552 release_firmware(fw);
553
554 return err ? err : count;
555}
556
557static DEVICE_ATTR(update_firmware, S_IWUSR, NULL,
558 ziirave_wdt_sysfs_store_firm);
559
Martyn Welch2a7b7532015-12-01 15:32:47 +0000560static struct attribute *ziirave_wdt_attrs[] = {
561 &dev_attr_firmware_version.attr,
562 &dev_attr_bootloader_version.attr,
563 &dev_attr_reset_reason.attr,
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200564 &dev_attr_update_firmware.attr,
Martyn Welch2a7b7532015-12-01 15:32:47 +0000565 NULL
566};
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800567ATTRIBUTE_GROUPS(ziirave_wdt);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000568
569static int ziirave_wdt_init_duration(struct i2c_client *client)
570{
571 int ret;
572
573 if (!reset_duration) {
574 /* See if the reset pulse duration is provided in an of_node */
575 if (!client->dev.of_node)
576 ret = -ENODEV;
577 else
578 ret = of_property_read_u32(client->dev.of_node,
579 "reset-duration-ms",
580 &reset_duration);
581 if (ret) {
582 dev_info(&client->dev,
Andrey Smirnovd2c1d422019-08-12 13:08:59 -0700583 "No reset pulse duration specified, using default\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000584 return 0;
585 }
586 }
587
588 if (reset_duration < 1 || reset_duration > 255)
589 return -EINVAL;
590
591 dev_info(&client->dev, "Setting reset duration to %dms",
592 reset_duration);
593
594 return i2c_smbus_write_byte_data(client, ZIIRAVE_WDT_RESET_DURATION,
595 reset_duration);
596}
597
598static int ziirave_wdt_probe(struct i2c_client *client,
599 const struct i2c_device_id *id)
600{
601 int ret;
602 struct ziirave_wdt_data *w_priv;
603 int val;
604
Andrey Smirnovf676ac82019-08-12 13:09:06 -0700605 if (!i2c_check_functionality(client->adapter,
606 I2C_FUNC_SMBUS_BYTE |
607 I2C_FUNC_SMBUS_BYTE_DATA |
608 I2C_FUNC_SMBUS_WRITE_BLOCK_DATA))
Martyn Welch2a7b7532015-12-01 15:32:47 +0000609 return -ENODEV;
610
611 w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL);
612 if (!w_priv)
613 return -ENOMEM;
614
Enric Balletbo i Serra217209d2016-08-10 18:18:03 +0200615 mutex_init(&w_priv->sysfs_mutex);
616
Martyn Welch2a7b7532015-12-01 15:32:47 +0000617 w_priv->wdd.info = &ziirave_wdt_info;
618 w_priv->wdd.ops = &ziirave_wdt_ops;
619 w_priv->wdd.min_timeout = ZIIRAVE_TIMEOUT_MIN;
620 w_priv->wdd.max_timeout = ZIIRAVE_TIMEOUT_MAX;
621 w_priv->wdd.parent = &client->dev;
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800622 w_priv->wdd.groups = ziirave_wdt_groups;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000623
Wolfram Sang15451162019-04-19 20:16:01 +0200624 watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000625
626 /*
627 * The default value set in the watchdog should be perfectly valid, so
628 * pass that in if we haven't provided one via the module parameter or
629 * of property.
630 */
631 if (w_priv->wdd.timeout == 0) {
632 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700633 if (val < 0) {
634 dev_err(&client->dev, "Failed to read timeout\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000635 return val;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700636 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000637
Andrey Smirnov39d03872019-08-12 13:08:48 -0700638 if (val > ZIIRAVE_TIMEOUT_MAX ||
639 val < ZIIRAVE_TIMEOUT_MIN)
640 val = ZIIRAVE_TIMEOUT_DEFAULT;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000641
642 w_priv->wdd.timeout = val;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000643 }
644
Andrey Smirnov39d03872019-08-12 13:08:48 -0700645 ret = ziirave_wdt_set_timeout(&w_priv->wdd, w_priv->wdd.timeout);
646 if (ret) {
647 dev_err(&client->dev, "Failed to set timeout\n");
648 return ret;
649 }
650
651 dev_info(&client->dev, "Timeout set to %ds\n", w_priv->wdd.timeout);
652
Martyn Welch2a7b7532015-12-01 15:32:47 +0000653 watchdog_set_nowayout(&w_priv->wdd, nowayout);
654
655 i2c_set_clientdata(client, w_priv);
656
657 /* If in unconfigured state, set to stopped */
658 val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700659 if (val < 0) {
660 dev_err(&client->dev, "Failed to read state\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000661 return val;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700662 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000663
664 if (val == ZIIRAVE_STATE_INITIAL)
665 ziirave_wdt_stop(&w_priv->wdd);
666
667 ret = ziirave_wdt_init_duration(client);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700668 if (ret) {
669 dev_err(&client->dev, "Failed to init duration\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000670 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700671 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000672
673 ret = ziirave_wdt_revision(client, &w_priv->firmware_rev,
674 ZIIRAVE_WDT_FIRM_VER_MAJOR);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700675 if (ret) {
676 dev_err(&client->dev, "Failed to read firmware version\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000677 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700678 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000679
Andrey Smirnov42abc122019-08-12 13:08:49 -0700680 dev_info(&client->dev,
681 "Firmware version: " ZIIRAVE_FW_VERSION_FMT "\n",
682 w_priv->firmware_rev.major, w_priv->firmware_rev.minor);
683
Martyn Welch2a7b7532015-12-01 15:32:47 +0000684 ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev,
685 ZIIRAVE_WDT_BOOT_VER_MAJOR);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700686 if (ret) {
687 dev_err(&client->dev, "Failed to read bootloader version\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000688 return ret;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700689 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000690
Andrey Smirnov42abc122019-08-12 13:08:49 -0700691 dev_info(&client->dev,
692 "Bootloader version: " ZIIRAVE_BL_VERSION_FMT "\n",
693 w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor);
694
Martyn Welch2a7b7532015-12-01 15:32:47 +0000695 w_priv->reset_reason = i2c_smbus_read_byte_data(client,
696 ZIIRAVE_WDT_RESET_REASON);
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700697 if (w_priv->reset_reason < 0) {
698 dev_err(&client->dev, "Failed to read reset reason\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000699 return w_priv->reset_reason;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700700 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000701
702 if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) ||
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700703 !ziirave_reasons[w_priv->reset_reason]) {
704 dev_err(&client->dev, "Invalid reset reason\n");
Martyn Welch2a7b7532015-12-01 15:32:47 +0000705 return -ENODEV;
Andrey Smirnov4a9600c2019-08-12 13:08:46 -0700706 }
Martyn Welch2a7b7532015-12-01 15:32:47 +0000707
708 ret = watchdog_register_device(&w_priv->wdd);
Martyn Welch2a7b7532015-12-01 15:32:47 +0000709
Guenter Roeck2c2f3082016-01-03 15:11:57 -0800710 return ret;
Martyn Welch2a7b7532015-12-01 15:32:47 +0000711}
712
713static int ziirave_wdt_remove(struct i2c_client *client)
714{
715 struct ziirave_wdt_data *w_priv = i2c_get_clientdata(client);
716
Martyn Welch2a7b7532015-12-01 15:32:47 +0000717 watchdog_unregister_device(&w_priv->wdd);
718
719 return 0;
720}
721
Arvind Yadav68c82be2017-08-21 22:18:38 +0530722static const struct i2c_device_id ziirave_wdt_id[] = {
Enric Balletbo i Serra22daf7a2016-07-09 11:43:19 +0200723 { "rave-wdt", 0 },
Martyn Welch2a7b7532015-12-01 15:32:47 +0000724 { }
725};
726MODULE_DEVICE_TABLE(i2c, ziirave_wdt_id);
727
728static const struct of_device_id zrv_wdt_of_match[] = {
729 { .compatible = "zii,rave-wdt", },
730 { },
731};
732MODULE_DEVICE_TABLE(of, zrv_wdt_of_match);
733
734static struct i2c_driver ziirave_wdt_driver = {
735 .driver = {
736 .name = "ziirave_wdt",
737 .of_match_table = zrv_wdt_of_match,
738 },
739 .probe = ziirave_wdt_probe,
740 .remove = ziirave_wdt_remove,
741 .id_table = ziirave_wdt_id,
742};
743
744module_i2c_driver(ziirave_wdt_driver);
745
746MODULE_AUTHOR("Martyn Welch <martyn.welch@collabora.co.uk");
747MODULE_DESCRIPTION("Zodiac Aerospace RAVE Switch Watchdog Processor Driver");
748MODULE_LICENSE("GPL");