| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (C) 2015 Zodiac Inflight Innovations |
|---|
| 3 | 4 | * |
|---|
| .. | .. |
|---|
| 6 | 7 | * Based on twl4030_wdt.c by Timo Kokkonen <timo.t.kokkonen at nokia.com>: |
|---|
| 7 | 8 | * |
|---|
| 8 | 9 | * Copyright (C) Nokia Corporation |
|---|
| 9 | | - * |
|---|
| 10 | | - * This program is free software; you can redistribute it and/or modify |
|---|
| 11 | | - * it under the terms of the GNU General Public License as published by |
|---|
| 12 | | - * the Free Software Foundation; either version 2 of the License, or |
|---|
| 13 | | - * (at your option) any later version. |
|---|
| 14 | | - * |
|---|
| 15 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 16 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | | - * GNU General Public License for more details. |
|---|
| 19 | 10 | */ |
|---|
| 20 | 11 | |
|---|
| 21 | 12 | #include <linux/delay.h> |
|---|
| .. | .. |
|---|
| 30 | 21 | #include <linux/version.h> |
|---|
| 31 | 22 | #include <linux/watchdog.h> |
|---|
| 32 | 23 | |
|---|
| 24 | +#include <asm/unaligned.h> |
|---|
| 25 | + |
|---|
| 33 | 26 | #define ZIIRAVE_TIMEOUT_MIN 3 |
|---|
| 34 | 27 | #define ZIIRAVE_TIMEOUT_MAX 255 |
|---|
| 28 | +#define ZIIRAVE_TIMEOUT_DEFAULT 30 |
|---|
| 35 | 29 | |
|---|
| 36 | 30 | #define ZIIRAVE_PING_VALUE 0x0 |
|---|
| 37 | 31 | |
|---|
| .. | .. |
|---|
| 57 | 51 | |
|---|
| 58 | 52 | #define ZIIRAVE_FIRM_PKT_TOTAL_SIZE 20 |
|---|
| 59 | 53 | #define ZIIRAVE_FIRM_PKT_DATA_SIZE 16 |
|---|
| 60 | | -#define ZIIRAVE_FIRM_FLASH_MEMORY_START 0x1600 |
|---|
| 61 | | -#define ZIIRAVE_FIRM_FLASH_MEMORY_END 0x2bbf |
|---|
| 54 | +#define ZIIRAVE_FIRM_FLASH_MEMORY_START (2 * 0x1600) |
|---|
| 55 | +#define ZIIRAVE_FIRM_FLASH_MEMORY_END (2 * 0x2bbf) |
|---|
| 56 | +#define ZIIRAVE_FIRM_PAGE_SIZE 128 |
|---|
| 62 | 57 | |
|---|
| 63 | 58 | /* Received and ready for next Download packet. */ |
|---|
| 64 | 59 | #define ZIIRAVE_FIRM_DOWNLOAD_ACK 1 |
|---|
| 65 | | -/* Currently writing to flash. Retry Download status in a moment! */ |
|---|
| 66 | | -#define ZIIRAVE_FIRM_DOWNLOAD_BUSY 2 |
|---|
| 67 | | - |
|---|
| 68 | | -/* Wait for ACK timeout in ms */ |
|---|
| 69 | | -#define ZIIRAVE_FIRM_WAIT_FOR_ACK_TIMEOUT 50 |
|---|
| 70 | 60 | |
|---|
| 71 | 61 | /* Firmware commands */ |
|---|
| 72 | 62 | #define ZIIRAVE_CMD_DOWNLOAD_START 0x10 |
|---|
| .. | .. |
|---|
| 76 | 66 | #define ZIIRAVE_CMD_RESET_PROCESSOR 0x0b |
|---|
| 77 | 67 | #define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER 0x0c |
|---|
| 78 | 68 | #define ZIIRAVE_CMD_DOWNLOAD_PACKET 0x0e |
|---|
| 69 | + |
|---|
| 70 | +#define ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC 1 |
|---|
| 71 | +#define ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC 1 |
|---|
| 72 | + |
|---|
| 73 | +#define ZIIRAVE_FW_VERSION_FMT "02.%02u.%02u" |
|---|
| 74 | +#define ZIIRAVE_BL_VERSION_FMT "01.%02u.%02u" |
|---|
| 79 | 75 | |
|---|
| 80 | 76 | struct ziirave_wdt_rev { |
|---|
| 81 | 77 | unsigned char major; |
|---|
| .. | .. |
|---|
| 174 | 170 | return ret; |
|---|
| 175 | 171 | } |
|---|
| 176 | 172 | |
|---|
| 177 | | -static int ziirave_firm_wait_for_ack(struct watchdog_device *wdd) |
|---|
| 173 | +static int ziirave_firm_read_ack(struct watchdog_device *wdd) |
|---|
| 178 | 174 | { |
|---|
| 179 | 175 | struct i2c_client *client = to_i2c_client(wdd->parent); |
|---|
| 180 | 176 | int ret; |
|---|
| 181 | | - unsigned long timeout; |
|---|
| 182 | 177 | |
|---|
| 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); |
|---|
| 178 | + 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 | + } |
|---|
| 196 | 183 | |
|---|
| 197 | 184 | return ret == ZIIRAVE_FIRM_DOWNLOAD_ACK ? 0 : -EIO; |
|---|
| 198 | 185 | } |
|---|
| 199 | 186 | |
|---|
| 200 | | -static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u16 addr) |
|---|
| 187 | +static int ziirave_firm_set_read_addr(struct watchdog_device *wdd, u32 addr) |
|---|
| 201 | 188 | { |
|---|
| 202 | 189 | struct i2c_client *client = to_i2c_client(wdd->parent); |
|---|
| 190 | + const u16 addr16 = (u16)addr / 2; |
|---|
| 203 | 191 | u8 address[2]; |
|---|
| 204 | 192 | |
|---|
| 205 | | - address[0] = addr & 0xff; |
|---|
| 206 | | - address[1] = (addr >> 8) & 0xff; |
|---|
| 193 | + put_unaligned_le16(addr16, address); |
|---|
| 207 | 194 | |
|---|
| 208 | 195 | return i2c_smbus_write_block_data(client, |
|---|
| 209 | 196 | ZIIRAVE_CMD_DOWNLOAD_SET_READ_ADDR, |
|---|
| 210 | | - ARRAY_SIZE(address), address); |
|---|
| 197 | + sizeof(address), address); |
|---|
| 211 | 198 | } |
|---|
| 212 | 199 | |
|---|
| 213 | | -static int ziirave_firm_write_block_data(struct watchdog_device *wdd, |
|---|
| 214 | | - u8 command, u8 length, const u8 *data, |
|---|
| 215 | | - bool wait_for_ack) |
|---|
| 200 | +static bool ziirave_firm_addr_readonly(u32 addr) |
|---|
| 216 | 201 | { |
|---|
| 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 | | - |
|---|
| 233 | | -static 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); |
|---|
| 202 | + return addr < ZIIRAVE_FIRM_FLASH_MEMORY_START || |
|---|
| 203 | + addr > ZIIRAVE_FIRM_FLASH_MEMORY_END; |
|---|
| 238 | 204 | } |
|---|
| 239 | 205 | |
|---|
| 240 | 206 | /* |
|---|
| .. | .. |
|---|
| 249 | 215 | * Data0 .. Data15: Array of 16 bytes of data. |
|---|
| 250 | 216 | * Checksum: Checksum byte to verify data integrity. |
|---|
| 251 | 217 | */ |
|---|
| 252 | | -static int ziirave_firm_write_pkt(struct watchdog_device *wdd, |
|---|
| 253 | | - const struct ihex_binrec *rec) |
|---|
| 218 | +static int __ziirave_firm_write_pkt(struct watchdog_device *wdd, |
|---|
| 219 | + u32 addr, const u8 *data, u8 len) |
|---|
| 254 | 220 | { |
|---|
| 221 | + const u16 addr16 = (u16)addr / 2; |
|---|
| 255 | 222 | struct i2c_client *client = to_i2c_client(wdd->parent); |
|---|
| 256 | 223 | u8 i, checksum = 0, packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE]; |
|---|
| 257 | 224 | int ret; |
|---|
| 258 | | - u16 addr; |
|---|
| 259 | 225 | |
|---|
| 260 | | - memset(packet, 0, ARRAY_SIZE(packet)); |
|---|
| 226 | + /* 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 | + |
|---|
| 233 | + /* |
|---|
| 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; |
|---|
| 261 | 242 | |
|---|
| 262 | 243 | /* Packet length */ |
|---|
| 263 | | - packet[0] = (u8)be16_to_cpu(rec->len); |
|---|
| 244 | + packet[0] = len; |
|---|
| 264 | 245 | /* Packet address */ |
|---|
| 265 | | - addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1; |
|---|
| 266 | | - packet[1] = addr & 0xff; |
|---|
| 267 | | - packet[2] = (addr & 0xff00) >> 8; |
|---|
| 246 | + put_unaligned_le16(addr16, packet + 1); |
|---|
| 268 | 247 | |
|---|
| 269 | | - /* Packet data */ |
|---|
| 270 | | - if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE) |
|---|
| 271 | | - return -EMSGSIZE; |
|---|
| 272 | | - memcpy(packet + 3, rec->data, be16_to_cpu(rec->len)); |
|---|
| 248 | + memcpy(packet + 3, data, len); |
|---|
| 249 | + memset(packet + 3 + len, 0, ZIIRAVE_FIRM_PKT_DATA_SIZE - len); |
|---|
| 273 | 250 | |
|---|
| 274 | 251 | /* Packet checksum */ |
|---|
| 275 | | - for (i = 0; i < ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1; i++) |
|---|
| 252 | + for (i = 0; i < len + 3; i++) |
|---|
| 276 | 253 | checksum += packet[i]; |
|---|
| 277 | 254 | packet[ZIIRAVE_FIRM_PKT_TOTAL_SIZE - 1] = checksum; |
|---|
| 278 | 255 | |
|---|
| 279 | | - ret = ziirave_firm_write_block_data(wdd, ZIIRAVE_CMD_DOWNLOAD_PACKET, |
|---|
| 280 | | - ARRAY_SIZE(packet), packet, true); |
|---|
| 256 | + 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); |
|---|
| 281 | 265 | if (ret) |
|---|
| 282 | 266 | dev_err(&client->dev, |
|---|
| 283 | 267 | "Failed to write firmware packet at address 0x%04x: %d\n", |
|---|
| 284 | 268 | addr, ret); |
|---|
| 285 | 269 | |
|---|
| 286 | 270 | return ret; |
|---|
| 271 | +} |
|---|
| 272 | + |
|---|
| 273 | +static 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); |
|---|
| 287 | 295 | } |
|---|
| 288 | 296 | |
|---|
| 289 | 297 | static int ziirave_firm_verify(struct watchdog_device *wdd, |
|---|
| .. | .. |
|---|
| 293 | 301 | const struct ihex_binrec *rec; |
|---|
| 294 | 302 | int i, ret; |
|---|
| 295 | 303 | u8 data[ZIIRAVE_FIRM_PKT_DATA_SIZE]; |
|---|
| 296 | | - u16 addr; |
|---|
| 297 | 304 | |
|---|
| 298 | 305 | for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) { |
|---|
| 299 | | - /* Zero length marks end of records */ |
|---|
| 300 | | - if (!be16_to_cpu(rec->len)) |
|---|
| 301 | | - break; |
|---|
| 306 | + const u16 len = be16_to_cpu(rec->len); |
|---|
| 307 | + const u32 addr = be32_to_cpu(rec->addr); |
|---|
| 302 | 308 | |
|---|
| 303 | | - addr = (be32_to_cpu(rec->addr) & 0xffff) >> 1; |
|---|
| 304 | | - if (addr < ZIIRAVE_FIRM_FLASH_MEMORY_START || |
|---|
| 305 | | - addr > ZIIRAVE_FIRM_FLASH_MEMORY_END) |
|---|
| 309 | + if (ziirave_firm_addr_readonly(addr)) |
|---|
| 306 | 310 | continue; |
|---|
| 307 | 311 | |
|---|
| 308 | 312 | ret = ziirave_firm_set_read_addr(wdd, addr); |
|---|
| .. | .. |
|---|
| 313 | 317 | return ret; |
|---|
| 314 | 318 | } |
|---|
| 315 | 319 | |
|---|
| 316 | | - for (i = 0; i < ARRAY_SIZE(data); i++) { |
|---|
| 320 | + for (i = 0; i < len; i++) { |
|---|
| 317 | 321 | ret = i2c_smbus_read_byte_data(client, |
|---|
| 318 | 322 | ZIIRAVE_CMD_DOWNLOAD_READ_BYTE); |
|---|
| 319 | 323 | if (ret < 0) { |
|---|
| .. | .. |
|---|
| 324 | 328 | data[i] = ret; |
|---|
| 325 | 329 | } |
|---|
| 326 | 330 | |
|---|
| 327 | | - if (memcmp(data, rec->data, be16_to_cpu(rec->len))) { |
|---|
| 331 | + if (memcmp(data, rec->data, len)) { |
|---|
| 328 | 332 | dev_err(&client->dev, |
|---|
| 329 | 333 | "Firmware mismatch at address 0x%04x\n", addr); |
|---|
| 330 | 334 | return -EINVAL; |
|---|
| .. | .. |
|---|
| 338 | 342 | const struct firmware *fw) |
|---|
| 339 | 343 | { |
|---|
| 340 | 344 | struct i2c_client *client = to_i2c_client(wdd->parent); |
|---|
| 341 | | - int ret, words_till_page_break; |
|---|
| 342 | 345 | const struct ihex_binrec *rec; |
|---|
| 343 | | - struct ihex_binrec *rec_new; |
|---|
| 346 | + int ret; |
|---|
| 344 | 347 | |
|---|
| 345 | | - ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_JUMP_TO_BOOTLOADER, 1, |
|---|
| 346 | | - false); |
|---|
| 347 | | - if (ret) |
|---|
| 348 | + ret = i2c_smbus_write_byte_data(client, |
|---|
| 349 | + ZIIRAVE_CMD_JUMP_TO_BOOTLOADER, |
|---|
| 350 | + ZIIRAVE_CMD_JUMP_TO_BOOTLOADER_MAGIC); |
|---|
| 351 | + if (ret) { |
|---|
| 352 | + dev_err(&client->dev, "Failed to jump to bootloader\n"); |
|---|
| 348 | 353 | return ret; |
|---|
| 354 | + } |
|---|
| 349 | 355 | |
|---|
| 350 | 356 | msleep(500); |
|---|
| 351 | 357 | |
|---|
| 352 | | - ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_START, 1, true); |
|---|
| 353 | | - if (ret) |
|---|
| 358 | + ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_START); |
|---|
| 359 | + if (ret) { |
|---|
| 360 | + dev_err(&client->dev, "Failed to start download\n"); |
|---|
| 354 | 361 | return ret; |
|---|
| 362 | + } |
|---|
| 363 | + |
|---|
| 364 | + ret = ziirave_firm_read_ack(wdd); |
|---|
| 365 | + if (ret) { |
|---|
| 366 | + dev_err(&client->dev, "No ACK for start download\n"); |
|---|
| 367 | + return ret; |
|---|
| 368 | + } |
|---|
| 355 | 369 | |
|---|
| 356 | 370 | msleep(500); |
|---|
| 357 | 371 | |
|---|
| 358 | 372 | for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) { |
|---|
| 359 | | - /* Zero length marks end of records */ |
|---|
| 360 | | - if (!be16_to_cpu(rec->len)) |
|---|
| 361 | | - break; |
|---|
| 362 | | - |
|---|
| 363 | | - /* Check max data size */ |
|---|
| 364 | | - if (be16_to_cpu(rec->len) > ZIIRAVE_FIRM_PKT_DATA_SIZE) { |
|---|
| 365 | | - dev_err(&client->dev, "Firmware packet too long (%d)\n", |
|---|
| 366 | | - be16_to_cpu(rec->len)); |
|---|
| 367 | | - return -EMSGSIZE; |
|---|
| 368 | | - } |
|---|
| 369 | | - |
|---|
| 370 | | - /* Calculate words till page break */ |
|---|
| 371 | | - words_till_page_break = (64 - ((be32_to_cpu(rec->addr) >> 1) & |
|---|
| 372 | | - 0x3f)); |
|---|
| 373 | | - if ((be16_to_cpu(rec->len) >> 1) > words_till_page_break) { |
|---|
| 374 | | - /* |
|---|
| 375 | | - * Data in passes page boundary, so we need to split in |
|---|
| 376 | | - * two blocks of data. Create a packet with the first |
|---|
| 377 | | - * block of data. |
|---|
| 378 | | - */ |
|---|
| 379 | | - rec_new = kzalloc(sizeof(struct ihex_binrec) + |
|---|
| 380 | | - (words_till_page_break << 1), |
|---|
| 381 | | - GFP_KERNEL); |
|---|
| 382 | | - if (!rec_new) |
|---|
| 383 | | - return -ENOMEM; |
|---|
| 384 | | - |
|---|
| 385 | | - rec_new->len = cpu_to_be16(words_till_page_break << 1); |
|---|
| 386 | | - rec_new->addr = rec->addr; |
|---|
| 387 | | - memcpy(rec_new->data, rec->data, |
|---|
| 388 | | - be16_to_cpu(rec_new->len)); |
|---|
| 389 | | - |
|---|
| 390 | | - ret = ziirave_firm_write_pkt(wdd, rec_new); |
|---|
| 391 | | - kfree(rec_new); |
|---|
| 392 | | - if (ret) |
|---|
| 393 | | - return ret; |
|---|
| 394 | | - |
|---|
| 395 | | - /* Create a packet with the second block of data */ |
|---|
| 396 | | - rec_new = kzalloc(sizeof(struct ihex_binrec) + |
|---|
| 397 | | - be16_to_cpu(rec->len) - |
|---|
| 398 | | - (words_till_page_break << 1), |
|---|
| 399 | | - GFP_KERNEL); |
|---|
| 400 | | - if (!rec_new) |
|---|
| 401 | | - return -ENOMEM; |
|---|
| 402 | | - |
|---|
| 403 | | - /* Remaining bytes */ |
|---|
| 404 | | - rec_new->len = rec->len - |
|---|
| 405 | | - cpu_to_be16(words_till_page_break << 1); |
|---|
| 406 | | - |
|---|
| 407 | | - rec_new->addr = cpu_to_be32(be32_to_cpu(rec->addr) + |
|---|
| 408 | | - (words_till_page_break << 1)); |
|---|
| 409 | | - |
|---|
| 410 | | - memcpy(rec_new->data, |
|---|
| 411 | | - rec->data + (words_till_page_break << 1), |
|---|
| 412 | | - be16_to_cpu(rec_new->len)); |
|---|
| 413 | | - |
|---|
| 414 | | - ret = ziirave_firm_write_pkt(wdd, rec_new); |
|---|
| 415 | | - kfree(rec_new); |
|---|
| 416 | | - if (ret) |
|---|
| 417 | | - return ret; |
|---|
| 418 | | - } else { |
|---|
| 419 | | - ret = ziirave_firm_write_pkt(wdd, rec); |
|---|
| 420 | | - if (ret) |
|---|
| 421 | | - return ret; |
|---|
| 422 | | - } |
|---|
| 373 | + 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; |
|---|
| 423 | 377 | } |
|---|
| 424 | 378 | |
|---|
| 425 | | - /* For end of download, the length field will be set to 0 */ |
|---|
| 426 | | - rec_new = kzalloc(sizeof(struct ihex_binrec) + 1, GFP_KERNEL); |
|---|
| 427 | | - if (!rec_new) |
|---|
| 428 | | - return -ENOMEM; |
|---|
| 429 | | - |
|---|
| 430 | | - ret = ziirave_firm_write_pkt(wdd, rec_new); |
|---|
| 431 | | - kfree(rec_new); |
|---|
| 379 | + /* |
|---|
| 380 | + * Finish firmware download process by sending a zero length |
|---|
| 381 | + * payload |
|---|
| 382 | + */ |
|---|
| 383 | + ret = ziirave_firm_write_pkt(wdd, 0, NULL, 0); |
|---|
| 432 | 384 | if (ret) { |
|---|
| 433 | 385 | dev_err(&client->dev, "Failed to send EMPTY packet: %d\n", ret); |
|---|
| 434 | 386 | return ret; |
|---|
| .. | .. |
|---|
| 446 | 398 | } |
|---|
| 447 | 399 | |
|---|
| 448 | 400 | /* End download operation */ |
|---|
| 449 | | - ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_DOWNLOAD_END, 1, false); |
|---|
| 450 | | - if (ret) |
|---|
| 401 | + ret = i2c_smbus_write_byte(client, ZIIRAVE_CMD_DOWNLOAD_END); |
|---|
| 402 | + if (ret) { |
|---|
| 403 | + dev_err(&client->dev, |
|---|
| 404 | + "Failed to end firmware download: %d\n", ret); |
|---|
| 451 | 405 | return ret; |
|---|
| 406 | + } |
|---|
| 452 | 407 | |
|---|
| 453 | 408 | /* Reset the processor */ |
|---|
| 454 | | - ret = ziirave_firm_write_byte(wdd, ZIIRAVE_CMD_RESET_PROCESSOR, 1, |
|---|
| 455 | | - false); |
|---|
| 456 | | - if (ret) |
|---|
| 409 | + ret = i2c_smbus_write_byte_data(client, |
|---|
| 410 | + ZIIRAVE_CMD_RESET_PROCESSOR, |
|---|
| 411 | + ZIIRAVE_CMD_RESET_PROCESSOR_MAGIC); |
|---|
| 412 | + if (ret) { |
|---|
| 413 | + dev_err(&client->dev, |
|---|
| 414 | + "Failed to reset the watchdog: %d\n", ret); |
|---|
| 457 | 415 | return ret; |
|---|
| 416 | + } |
|---|
| 458 | 417 | |
|---|
| 459 | 418 | msleep(500); |
|---|
| 460 | 419 | |
|---|
| .. | .. |
|---|
| 463 | 422 | |
|---|
| 464 | 423 | static const struct watchdog_info ziirave_wdt_info = { |
|---|
| 465 | 424 | .options = WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | WDIOF_KEEPALIVEPING, |
|---|
| 466 | | - .identity = "Zodiac RAVE Watchdog", |
|---|
| 425 | + .identity = "RAVE Switch Watchdog", |
|---|
| 467 | 426 | }; |
|---|
| 468 | 427 | |
|---|
| 469 | 428 | static const struct watchdog_ops ziirave_wdt_ops = { |
|---|
| .. | .. |
|---|
| 487 | 446 | if (ret) |
|---|
| 488 | 447 | return ret; |
|---|
| 489 | 448 | |
|---|
| 490 | | - ret = sprintf(buf, "02.%02u.%02u", w_priv->firmware_rev.major, |
|---|
| 449 | + ret = sprintf(buf, ZIIRAVE_FW_VERSION_FMT, w_priv->firmware_rev.major, |
|---|
| 491 | 450 | w_priv->firmware_rev.minor); |
|---|
| 492 | 451 | |
|---|
| 493 | 452 | mutex_unlock(&w_priv->sysfs_mutex); |
|---|
| .. | .. |
|---|
| 510 | 469 | if (ret) |
|---|
| 511 | 470 | return ret; |
|---|
| 512 | 471 | |
|---|
| 513 | | - ret = sprintf(buf, "01.%02u.%02u", w_priv->bootloader_rev.major, |
|---|
| 472 | + ret = sprintf(buf, ZIIRAVE_BL_VERSION_FMT, w_priv->bootloader_rev.major, |
|---|
| 514 | 473 | w_priv->bootloader_rev.minor); |
|---|
| 515 | 474 | |
|---|
| 516 | 475 | mutex_unlock(&w_priv->sysfs_mutex); |
|---|
| .. | .. |
|---|
| 577 | 536 | goto unlock_mutex; |
|---|
| 578 | 537 | } |
|---|
| 579 | 538 | |
|---|
| 580 | | - dev_info(&client->dev, "Firmware updated to version 02.%02u.%02u\n", |
|---|
| 539 | + dev_info(&client->dev, |
|---|
| 540 | + "Firmware updated to version " ZIIRAVE_FW_VERSION_FMT "\n", |
|---|
| 581 | 541 | w_priv->firmware_rev.major, w_priv->firmware_rev.minor); |
|---|
| 582 | 542 | |
|---|
| 583 | 543 | /* Restore the watchdog timeout */ |
|---|
| .. | .. |
|---|
| 620 | 580 | &reset_duration); |
|---|
| 621 | 581 | if (ret) { |
|---|
| 622 | 582 | dev_info(&client->dev, |
|---|
| 623 | | - "Unable to set reset pulse duration, using default\n"); |
|---|
| 583 | + "No reset pulse duration specified, using default\n"); |
|---|
| 624 | 584 | return 0; |
|---|
| 625 | 585 | } |
|---|
| 626 | 586 | } |
|---|
| .. | .. |
|---|
| 642 | 602 | struct ziirave_wdt_data *w_priv; |
|---|
| 643 | 603 | int val; |
|---|
| 644 | 604 | |
|---|
| 645 | | - if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
|---|
| 605 | + if (!i2c_check_functionality(client->adapter, |
|---|
| 606 | + I2C_FUNC_SMBUS_BYTE | |
|---|
| 607 | + I2C_FUNC_SMBUS_BYTE_DATA | |
|---|
| 608 | + I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)) |
|---|
| 646 | 609 | return -ENODEV; |
|---|
| 647 | 610 | |
|---|
| 648 | 611 | w_priv = devm_kzalloc(&client->dev, sizeof(*w_priv), GFP_KERNEL); |
|---|
| .. | .. |
|---|
| 658 | 621 | w_priv->wdd.parent = &client->dev; |
|---|
| 659 | 622 | w_priv->wdd.groups = ziirave_wdt_groups; |
|---|
| 660 | 623 | |
|---|
| 661 | | - ret = watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev); |
|---|
| 662 | | - if (ret) { |
|---|
| 663 | | - dev_info(&client->dev, |
|---|
| 664 | | - "Unable to select timeout value, using default\n"); |
|---|
| 665 | | - } |
|---|
| 624 | + watchdog_init_timeout(&w_priv->wdd, wdt_timeout, &client->dev); |
|---|
| 666 | 625 | |
|---|
| 667 | 626 | /* |
|---|
| 668 | 627 | * The default value set in the watchdog should be perfectly valid, so |
|---|
| .. | .. |
|---|
| 671 | 630 | */ |
|---|
| 672 | 631 | if (w_priv->wdd.timeout == 0) { |
|---|
| 673 | 632 | val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_TIMEOUT); |
|---|
| 674 | | - if (val < 0) |
|---|
| 633 | + if (val < 0) { |
|---|
| 634 | + dev_err(&client->dev, "Failed to read timeout\n"); |
|---|
| 675 | 635 | return val; |
|---|
| 636 | + } |
|---|
| 676 | 637 | |
|---|
| 677 | | - if (val < ZIIRAVE_TIMEOUT_MIN) |
|---|
| 678 | | - return -ENODEV; |
|---|
| 638 | + if (val > ZIIRAVE_TIMEOUT_MAX || |
|---|
| 639 | + val < ZIIRAVE_TIMEOUT_MIN) |
|---|
| 640 | + val = ZIIRAVE_TIMEOUT_DEFAULT; |
|---|
| 679 | 641 | |
|---|
| 680 | 642 | w_priv->wdd.timeout = val; |
|---|
| 681 | | - } else { |
|---|
| 682 | | - ret = ziirave_wdt_set_timeout(&w_priv->wdd, |
|---|
| 683 | | - w_priv->wdd.timeout); |
|---|
| 684 | | - if (ret) |
|---|
| 685 | | - return ret; |
|---|
| 686 | | - |
|---|
| 687 | | - dev_info(&client->dev, "Timeout set to %ds.", |
|---|
| 688 | | - w_priv->wdd.timeout); |
|---|
| 689 | 643 | } |
|---|
| 644 | + |
|---|
| 645 | + 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); |
|---|
| 690 | 652 | |
|---|
| 691 | 653 | watchdog_set_nowayout(&w_priv->wdd, nowayout); |
|---|
| 692 | 654 | |
|---|
| .. | .. |
|---|
| 694 | 656 | |
|---|
| 695 | 657 | /* If in unconfigured state, set to stopped */ |
|---|
| 696 | 658 | val = i2c_smbus_read_byte_data(client, ZIIRAVE_WDT_STATE); |
|---|
| 697 | | - if (val < 0) |
|---|
| 659 | + if (val < 0) { |
|---|
| 660 | + dev_err(&client->dev, "Failed to read state\n"); |
|---|
| 698 | 661 | return val; |
|---|
| 662 | + } |
|---|
| 699 | 663 | |
|---|
| 700 | 664 | if (val == ZIIRAVE_STATE_INITIAL) |
|---|
| 701 | 665 | ziirave_wdt_stop(&w_priv->wdd); |
|---|
| 702 | 666 | |
|---|
| 703 | 667 | ret = ziirave_wdt_init_duration(client); |
|---|
| 704 | | - if (ret) |
|---|
| 668 | + if (ret) { |
|---|
| 669 | + dev_err(&client->dev, "Failed to init duration\n"); |
|---|
| 705 | 670 | return ret; |
|---|
| 671 | + } |
|---|
| 706 | 672 | |
|---|
| 707 | 673 | ret = ziirave_wdt_revision(client, &w_priv->firmware_rev, |
|---|
| 708 | 674 | ZIIRAVE_WDT_FIRM_VER_MAJOR); |
|---|
| 709 | | - if (ret) |
|---|
| 675 | + if (ret) { |
|---|
| 676 | + dev_err(&client->dev, "Failed to read firmware version\n"); |
|---|
| 710 | 677 | return ret; |
|---|
| 678 | + } |
|---|
| 679 | + |
|---|
| 680 | + dev_info(&client->dev, |
|---|
| 681 | + "Firmware version: " ZIIRAVE_FW_VERSION_FMT "\n", |
|---|
| 682 | + w_priv->firmware_rev.major, w_priv->firmware_rev.minor); |
|---|
| 711 | 683 | |
|---|
| 712 | 684 | ret = ziirave_wdt_revision(client, &w_priv->bootloader_rev, |
|---|
| 713 | 685 | ZIIRAVE_WDT_BOOT_VER_MAJOR); |
|---|
| 714 | | - if (ret) |
|---|
| 686 | + if (ret) { |
|---|
| 687 | + dev_err(&client->dev, "Failed to read bootloader version\n"); |
|---|
| 715 | 688 | return ret; |
|---|
| 689 | + } |
|---|
| 690 | + |
|---|
| 691 | + dev_info(&client->dev, |
|---|
| 692 | + "Bootloader version: " ZIIRAVE_BL_VERSION_FMT "\n", |
|---|
| 693 | + w_priv->bootloader_rev.major, w_priv->bootloader_rev.minor); |
|---|
| 716 | 694 | |
|---|
| 717 | 695 | w_priv->reset_reason = i2c_smbus_read_byte_data(client, |
|---|
| 718 | 696 | ZIIRAVE_WDT_RESET_REASON); |
|---|
| 719 | | - if (w_priv->reset_reason < 0) |
|---|
| 697 | + if (w_priv->reset_reason < 0) { |
|---|
| 698 | + dev_err(&client->dev, "Failed to read reset reason\n"); |
|---|
| 720 | 699 | return w_priv->reset_reason; |
|---|
| 700 | + } |
|---|
| 721 | 701 | |
|---|
| 722 | 702 | if (w_priv->reset_reason >= ARRAY_SIZE(ziirave_reasons) || |
|---|
| 723 | | - !ziirave_reasons[w_priv->reset_reason]) |
|---|
| 703 | + !ziirave_reasons[w_priv->reset_reason]) { |
|---|
| 704 | + dev_err(&client->dev, "Invalid reset reason\n"); |
|---|
| 724 | 705 | return -ENODEV; |
|---|
| 706 | + } |
|---|
| 725 | 707 | |
|---|
| 726 | 708 | ret = watchdog_register_device(&w_priv->wdd); |
|---|
| 727 | 709 | |
|---|