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