Thomas Gleixner | 35e62ae | 2019-05-29 16:57:58 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2008-2010 |
| 4 | * |
| 5 | * - Kurt Van Dijck, EIA Electronics |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/firmware.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 9 | #include <linux/sched/signal.h> |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 10 | #include <asm/div64.h> |
Alexey Dobriyan | b7f080c | 2011-06-16 11:01:34 +0000 | [diff] [blame] | 11 | #include <asm/io.h> |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 12 | |
| 13 | #include "softing.h" |
| 14 | |
| 15 | /* |
| 16 | * low level DPRAM command. |
| 17 | * Make sure that card->dpram[DPRAM_FCT_HOST] is preset |
| 18 | */ |
| 19 | static int _softing_fct_cmd(struct softing *card, int16_t cmd, uint16_t vector, |
| 20 | const char *msg) |
| 21 | { |
| 22 | int ret; |
| 23 | unsigned long stamp; |
| 24 | |
| 25 | iowrite16(cmd, &card->dpram[DPRAM_FCT_PARAM]); |
| 26 | iowrite8(vector >> 8, &card->dpram[DPRAM_FCT_HOST + 1]); |
| 27 | iowrite8(vector, &card->dpram[DPRAM_FCT_HOST]); |
| 28 | /* be sure to flush this to the card */ |
| 29 | wmb(); |
| 30 | stamp = jiffies + 1 * HZ; |
| 31 | /* wait for card */ |
| 32 | do { |
| 33 | /* DPRAM_FCT_HOST is _not_ aligned */ |
| 34 | ret = ioread8(&card->dpram[DPRAM_FCT_HOST]) + |
| 35 | (ioread8(&card->dpram[DPRAM_FCT_HOST + 1]) << 8); |
| 36 | /* don't have any cached variables */ |
| 37 | rmb(); |
| 38 | if (ret == RES_OK) |
| 39 | /* read return-value now */ |
| 40 | return ioread16(&card->dpram[DPRAM_FCT_RESULT]); |
| 41 | |
| 42 | if ((ret != vector) || time_after(jiffies, stamp)) |
| 43 | break; |
| 44 | /* process context => relax */ |
| 45 | usleep_range(500, 10000); |
| 46 | } while (1); |
| 47 | |
| 48 | ret = (ret == RES_NONE) ? -ETIMEDOUT : -ECANCELED; |
| 49 | dev_alert(&card->pdev->dev, "firmware %s failed (%i)\n", msg, ret); |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | static int softing_fct_cmd(struct softing *card, int16_t cmd, const char *msg) |
| 54 | { |
| 55 | int ret; |
| 56 | |
| 57 | ret = _softing_fct_cmd(card, cmd, 0, msg); |
| 58 | if (ret > 0) { |
| 59 | dev_alert(&card->pdev->dev, "%s returned %u\n", msg, ret); |
| 60 | ret = -EIO; |
| 61 | } |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | int softing_bootloader_command(struct softing *card, int16_t cmd, |
| 66 | const char *msg) |
| 67 | { |
| 68 | int ret; |
| 69 | unsigned long stamp; |
| 70 | |
| 71 | iowrite16(RES_NONE, &card->dpram[DPRAM_RECEIPT]); |
| 72 | iowrite16(cmd, &card->dpram[DPRAM_COMMAND]); |
| 73 | /* be sure to flush this to the card */ |
| 74 | wmb(); |
| 75 | stamp = jiffies + 3 * HZ; |
| 76 | /* wait for card */ |
| 77 | do { |
| 78 | ret = ioread16(&card->dpram[DPRAM_RECEIPT]); |
| 79 | /* don't have any cached variables */ |
| 80 | rmb(); |
| 81 | if (ret == RES_OK) |
| 82 | return 0; |
| 83 | if (time_after(jiffies, stamp)) |
| 84 | break; |
| 85 | /* process context => relax */ |
| 86 | usleep_range(500, 10000); |
| 87 | } while (!signal_pending(current)); |
| 88 | |
| 89 | ret = (ret == RES_NONE) ? -ETIMEDOUT : -ECANCELED; |
| 90 | dev_alert(&card->pdev->dev, "bootloader %s failed (%i)\n", msg, ret); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static int fw_parse(const uint8_t **pmem, uint16_t *ptype, uint32_t *paddr, |
| 95 | uint16_t *plen, const uint8_t **pdat) |
| 96 | { |
| 97 | uint16_t checksum[2]; |
| 98 | const uint8_t *mem; |
| 99 | const uint8_t *end; |
| 100 | |
| 101 | /* |
| 102 | * firmware records are a binary, unaligned stream composed of: |
| 103 | * uint16_t type; |
| 104 | * uint32_t addr; |
| 105 | * uint16_t len; |
| 106 | * uint8_t dat[len]; |
| 107 | * uint16_t checksum; |
| 108 | * all values in little endian. |
| 109 | * We could define a struct for this, with __attribute__((packed)), |
| 110 | * but would that solve the alignment in _all_ cases (cfr. the |
| 111 | * struct itself may be an odd address)? |
| 112 | * |
| 113 | * I chose to use leXX_to_cpup() since this solves both |
| 114 | * endianness & alignment. |
| 115 | */ |
| 116 | mem = *pmem; |
| 117 | *ptype = le16_to_cpup((void *)&mem[0]); |
| 118 | *paddr = le32_to_cpup((void *)&mem[2]); |
| 119 | *plen = le16_to_cpup((void *)&mem[6]); |
| 120 | *pdat = &mem[8]; |
| 121 | /* verify checksum */ |
| 122 | end = &mem[8 + *plen]; |
| 123 | checksum[0] = le16_to_cpup((void *)end); |
| 124 | for (checksum[1] = 0; mem < end; ++mem) |
| 125 | checksum[1] += *mem; |
| 126 | if (checksum[0] != checksum[1]) |
| 127 | return -EINVAL; |
| 128 | /* increment */ |
| 129 | *pmem += 10 + *plen; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | int softing_load_fw(const char *file, struct softing *card, |
| 134 | __iomem uint8_t *dpram, unsigned int size, int offset) |
| 135 | { |
| 136 | const struct firmware *fw; |
| 137 | int ret; |
| 138 | const uint8_t *mem, *end, *dat; |
| 139 | uint16_t type, len; |
| 140 | uint32_t addr; |
Alexey Khoroshilov | ef813c4 | 2012-08-08 19:15:01 +0400 | [diff] [blame] | 141 | uint8_t *buf = NULL, *new_buf; |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 142 | int buflen = 0; |
| 143 | int8_t type_end = 0; |
| 144 | |
| 145 | ret = request_firmware(&fw, file, &card->pdev->dev); |
| 146 | if (ret < 0) |
| 147 | return ret; |
| 148 | dev_dbg(&card->pdev->dev, "%s, firmware(%s) got %u bytes" |
| 149 | ", offset %c0x%04x\n", |
| 150 | card->pdat->name, file, (unsigned int)fw->size, |
| 151 | (offset >= 0) ? '+' : '-', (unsigned int)abs(offset)); |
| 152 | /* parse the firmware */ |
| 153 | mem = fw->data; |
| 154 | end = &mem[fw->size]; |
| 155 | /* look for header record */ |
| 156 | ret = fw_parse(&mem, &type, &addr, &len, &dat); |
| 157 | if (ret < 0) |
| 158 | goto failed; |
| 159 | if (type != 0xffff) |
| 160 | goto failed; |
| 161 | if (strncmp("Structured Binary Format, Softing GmbH" , dat, len)) { |
| 162 | ret = -EINVAL; |
| 163 | goto failed; |
| 164 | } |
| 165 | /* ok, we had a header */ |
| 166 | while (mem < end) { |
| 167 | ret = fw_parse(&mem, &type, &addr, &len, &dat); |
| 168 | if (ret < 0) |
| 169 | goto failed; |
| 170 | if (type == 3) { |
| 171 | /* start address, not used here */ |
| 172 | continue; |
| 173 | } else if (type == 1) { |
| 174 | /* eof */ |
| 175 | type_end = 1; |
| 176 | break; |
| 177 | } else if (type != 0) { |
| 178 | ret = -EINVAL; |
| 179 | goto failed; |
| 180 | } |
| 181 | |
| 182 | if ((addr + len + offset) > size) |
| 183 | goto failed; |
| 184 | memcpy_toio(&dpram[addr + offset], dat, len); |
| 185 | /* be sure to flush caches from IO space */ |
| 186 | mb(); |
| 187 | if (len > buflen) { |
| 188 | /* align buflen */ |
| 189 | buflen = (len + (1024-1)) & ~(1024-1); |
Alexey Khoroshilov | ef813c4 | 2012-08-08 19:15:01 +0400 | [diff] [blame] | 190 | new_buf = krealloc(buf, buflen, GFP_KERNEL); |
| 191 | if (!new_buf) { |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 192 | ret = -ENOMEM; |
| 193 | goto failed; |
| 194 | } |
Alexey Khoroshilov | ef813c4 | 2012-08-08 19:15:01 +0400 | [diff] [blame] | 195 | buf = new_buf; |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 196 | } |
| 197 | /* verify record data */ |
| 198 | memcpy_fromio(buf, &dpram[addr + offset], len); |
| 199 | if (memcmp(buf, dat, len)) { |
| 200 | /* is not ok */ |
| 201 | dev_alert(&card->pdev->dev, "DPRAM readback failed\n"); |
| 202 | ret = -EIO; |
| 203 | goto failed; |
| 204 | } |
| 205 | } |
| 206 | if (!type_end) |
| 207 | /* no end record seen */ |
| 208 | goto failed; |
| 209 | ret = 0; |
| 210 | failed: |
| 211 | kfree(buf); |
| 212 | release_firmware(fw); |
| 213 | if (ret < 0) |
| 214 | dev_info(&card->pdev->dev, "firmware %s failed\n", file); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | int softing_load_app_fw(const char *file, struct softing *card) |
| 219 | { |
| 220 | const struct firmware *fw; |
| 221 | const uint8_t *mem, *end, *dat; |
| 222 | int ret, j; |
| 223 | uint16_t type, len; |
| 224 | uint32_t addr, start_addr = 0; |
| 225 | unsigned int sum, rx_sum; |
| 226 | int8_t type_end = 0, type_entrypoint = 0; |
| 227 | |
| 228 | ret = request_firmware(&fw, file, &card->pdev->dev); |
| 229 | if (ret) { |
| 230 | dev_alert(&card->pdev->dev, "request_firmware(%s) got %i\n", |
| 231 | file, ret); |
| 232 | return ret; |
| 233 | } |
| 234 | dev_dbg(&card->pdev->dev, "firmware(%s) got %lu bytes\n", |
| 235 | file, (unsigned long)fw->size); |
| 236 | /* parse the firmware */ |
| 237 | mem = fw->data; |
| 238 | end = &mem[fw->size]; |
| 239 | /* look for header record */ |
| 240 | ret = fw_parse(&mem, &type, &addr, &len, &dat); |
| 241 | if (ret) |
| 242 | goto failed; |
| 243 | ret = -EINVAL; |
| 244 | if (type != 0xffff) { |
| 245 | dev_alert(&card->pdev->dev, "firmware starts with type 0x%x\n", |
| 246 | type); |
| 247 | goto failed; |
| 248 | } |
| 249 | if (strncmp("Structured Binary Format, Softing GmbH", dat, len)) { |
| 250 | dev_alert(&card->pdev->dev, "firmware string '%.*s' fault\n", |
| 251 | len, dat); |
| 252 | goto failed; |
| 253 | } |
| 254 | /* ok, we had a header */ |
| 255 | while (mem < end) { |
| 256 | ret = fw_parse(&mem, &type, &addr, &len, &dat); |
| 257 | if (ret) |
| 258 | goto failed; |
| 259 | |
| 260 | if (type == 3) { |
| 261 | /* start address */ |
| 262 | start_addr = addr; |
| 263 | type_entrypoint = 1; |
| 264 | continue; |
| 265 | } else if (type == 1) { |
| 266 | /* eof */ |
| 267 | type_end = 1; |
| 268 | break; |
| 269 | } else if (type != 0) { |
| 270 | dev_alert(&card->pdev->dev, |
| 271 | "unknown record type 0x%04x\n", type); |
| 272 | ret = -EINVAL; |
| 273 | goto failed; |
| 274 | } |
| 275 | |
| 276 | /* regualar data */ |
| 277 | for (sum = 0, j = 0; j < len; ++j) |
| 278 | sum += dat[j]; |
| 279 | /* work in 16bit (target) */ |
| 280 | sum &= 0xffff; |
| 281 | |
| 282 | memcpy_toio(&card->dpram[card->pdat->app.offs], dat, len); |
| 283 | iowrite32(card->pdat->app.offs + card->pdat->app.addr, |
| 284 | &card->dpram[DPRAM_COMMAND + 2]); |
| 285 | iowrite32(addr, &card->dpram[DPRAM_COMMAND + 6]); |
| 286 | iowrite16(len, &card->dpram[DPRAM_COMMAND + 10]); |
| 287 | iowrite8(1, &card->dpram[DPRAM_COMMAND + 12]); |
| 288 | ret = softing_bootloader_command(card, 1, "loading app."); |
| 289 | if (ret < 0) |
| 290 | goto failed; |
| 291 | /* verify checksum */ |
| 292 | rx_sum = ioread16(&card->dpram[DPRAM_RECEIPT + 2]); |
| 293 | if (rx_sum != sum) { |
| 294 | dev_alert(&card->pdev->dev, "SRAM seems to be damaged" |
| 295 | ", wanted 0x%04x, got 0x%04x\n", sum, rx_sum); |
| 296 | ret = -EIO; |
| 297 | goto failed; |
| 298 | } |
| 299 | } |
| 300 | if (!type_end || !type_entrypoint) |
| 301 | goto failed; |
| 302 | /* start application in card */ |
| 303 | iowrite32(start_addr, &card->dpram[DPRAM_COMMAND + 2]); |
| 304 | iowrite8(1, &card->dpram[DPRAM_COMMAND + 6]); |
| 305 | ret = softing_bootloader_command(card, 3, "start app."); |
| 306 | if (ret < 0) |
| 307 | goto failed; |
| 308 | ret = 0; |
| 309 | failed: |
| 310 | release_firmware(fw); |
| 311 | if (ret < 0) |
| 312 | dev_info(&card->pdev->dev, "firmware %s failed\n", file); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static int softing_reset_chip(struct softing *card) |
| 317 | { |
| 318 | int ret; |
| 319 | |
| 320 | do { |
| 321 | /* reset chip */ |
| 322 | iowrite8(0, &card->dpram[DPRAM_RESET_RX_FIFO]); |
| 323 | iowrite8(0, &card->dpram[DPRAM_RESET_RX_FIFO+1]); |
| 324 | iowrite8(1, &card->dpram[DPRAM_RESET]); |
| 325 | iowrite8(0, &card->dpram[DPRAM_RESET+1]); |
| 326 | |
| 327 | ret = softing_fct_cmd(card, 0, "reset_can"); |
| 328 | if (!ret) |
| 329 | break; |
| 330 | if (signal_pending(current)) |
| 331 | /* don't wait any longer */ |
| 332 | break; |
| 333 | } while (1); |
| 334 | card->tx.pending = 0; |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | int softing_chip_poweron(struct softing *card) |
| 339 | { |
| 340 | int ret; |
| 341 | /* sync */ |
| 342 | ret = _softing_fct_cmd(card, 99, 0x55, "sync-a"); |
| 343 | if (ret < 0) |
| 344 | goto failed; |
| 345 | |
| 346 | ret = _softing_fct_cmd(card, 99, 0xaa, "sync-b"); |
| 347 | if (ret < 0) |
| 348 | goto failed; |
| 349 | |
| 350 | ret = softing_reset_chip(card); |
| 351 | if (ret < 0) |
| 352 | goto failed; |
| 353 | /* get_serial */ |
| 354 | ret = softing_fct_cmd(card, 43, "get_serial_number"); |
| 355 | if (ret < 0) |
| 356 | goto failed; |
| 357 | card->id.serial = ioread32(&card->dpram[DPRAM_FCT_PARAM]); |
| 358 | /* get_version */ |
| 359 | ret = softing_fct_cmd(card, 12, "get_version"); |
| 360 | if (ret < 0) |
| 361 | goto failed; |
| 362 | card->id.fw_version = ioread16(&card->dpram[DPRAM_FCT_PARAM + 2]); |
| 363 | card->id.hw_version = ioread16(&card->dpram[DPRAM_FCT_PARAM + 4]); |
| 364 | card->id.license = ioread16(&card->dpram[DPRAM_FCT_PARAM + 6]); |
| 365 | card->id.chip[0] = ioread16(&card->dpram[DPRAM_FCT_PARAM + 8]); |
| 366 | card->id.chip[1] = ioread16(&card->dpram[DPRAM_FCT_PARAM + 10]); |
| 367 | return 0; |
| 368 | failed: |
| 369 | return ret; |
| 370 | } |
| 371 | |
| 372 | static void softing_initialize_timestamp(struct softing *card) |
| 373 | { |
| 374 | uint64_t ovf; |
| 375 | |
| 376 | card->ts_ref = ktime_get(); |
| 377 | |
| 378 | /* 16MHz is the reference */ |
| 379 | ovf = 0x100000000ULL * 16; |
| 380 | do_div(ovf, card->pdat->freq ?: 16); |
| 381 | |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 382 | card->ts_overflow = ktime_add_us(0, ovf); |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | ktime_t softing_raw2ktime(struct softing *card, u32 raw) |
| 386 | { |
| 387 | uint64_t rawl; |
| 388 | ktime_t now, real_offset; |
| 389 | ktime_t target; |
| 390 | ktime_t tmp; |
| 391 | |
| 392 | now = ktime_get(); |
| 393 | real_offset = ktime_sub(ktime_get_real(), now); |
| 394 | |
| 395 | /* find nsec from card */ |
| 396 | rawl = raw * 16; |
| 397 | do_div(rawl, card->pdat->freq ?: 16); |
| 398 | target = ktime_add_us(card->ts_ref, rawl); |
| 399 | /* test for overflows */ |
| 400 | tmp = ktime_add(target, card->ts_overflow); |
| 401 | while (unlikely(ktime_to_ns(tmp) > ktime_to_ns(now))) { |
| 402 | card->ts_ref = ktime_add(card->ts_ref, card->ts_overflow); |
| 403 | target = tmp; |
| 404 | tmp = ktime_add(target, card->ts_overflow); |
| 405 | } |
| 406 | return ktime_add(target, real_offset); |
| 407 | } |
| 408 | |
| 409 | static inline int softing_error_reporting(struct net_device *netdev) |
| 410 | { |
| 411 | struct softing_priv *priv = netdev_priv(netdev); |
| 412 | |
| 413 | return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) |
| 414 | ? 1 : 0; |
| 415 | } |
| 416 | |
| 417 | int softing_startstop(struct net_device *dev, int up) |
| 418 | { |
| 419 | int ret; |
| 420 | struct softing *card; |
| 421 | struct softing_priv *priv; |
| 422 | struct net_device *netdev; |
| 423 | int bus_bitmask_start; |
| 424 | int j, error_reporting; |
| 425 | struct can_frame msg; |
| 426 | const struct can_bittiming *bt; |
| 427 | |
| 428 | priv = netdev_priv(dev); |
| 429 | card = priv->card; |
| 430 | |
| 431 | if (!card->fw.up) |
| 432 | return -EIO; |
| 433 | |
| 434 | ret = mutex_lock_interruptible(&card->fw.lock); |
| 435 | if (ret) |
| 436 | return ret; |
| 437 | |
| 438 | bus_bitmask_start = 0; |
| 439 | if (dev && up) |
| 440 | /* prepare to start this bus as well */ |
| 441 | bus_bitmask_start |= (1 << priv->index); |
| 442 | /* bring netdevs down */ |
| 443 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 444 | netdev = card->net[j]; |
| 445 | if (!netdev) |
| 446 | continue; |
| 447 | priv = netdev_priv(netdev); |
| 448 | |
| 449 | if (dev != netdev) |
| 450 | netif_stop_queue(netdev); |
| 451 | |
| 452 | if (netif_running(netdev)) { |
| 453 | if (dev != netdev) |
| 454 | bus_bitmask_start |= (1 << j); |
| 455 | priv->tx.pending = 0; |
| 456 | priv->tx.echo_put = 0; |
| 457 | priv->tx.echo_get = 0; |
| 458 | /* |
| 459 | * this bus' may just have called open_candev() |
| 460 | * which is rather stupid to call close_candev() |
| 461 | * already |
| 462 | * but we may come here from busoff recovery too |
| 463 | * in which case the echo_skb _needs_ flushing too. |
| 464 | * just be sure to call open_candev() again |
| 465 | */ |
| 466 | close_candev(netdev); |
| 467 | } |
| 468 | priv->can.state = CAN_STATE_STOPPED; |
| 469 | } |
| 470 | card->tx.pending = 0; |
| 471 | |
| 472 | softing_enable_irq(card, 0); |
| 473 | ret = softing_reset_chip(card); |
| 474 | if (ret) |
| 475 | goto failed; |
| 476 | if (!bus_bitmask_start) |
| 477 | /* no busses to be brought up */ |
| 478 | goto card_done; |
| 479 | |
| 480 | if ((bus_bitmask_start & 1) && (bus_bitmask_start & 2) |
| 481 | && (softing_error_reporting(card->net[0]) |
| 482 | != softing_error_reporting(card->net[1]))) { |
| 483 | dev_alert(&card->pdev->dev, |
| 484 | "err_reporting flag differs for busses\n"); |
| 485 | goto invalid; |
| 486 | } |
| 487 | error_reporting = 0; |
| 488 | if (bus_bitmask_start & 1) { |
| 489 | netdev = card->net[0]; |
| 490 | priv = netdev_priv(netdev); |
| 491 | error_reporting += softing_error_reporting(netdev); |
| 492 | /* init chip 1 */ |
| 493 | bt = &priv->can.bittiming; |
| 494 | iowrite16(bt->brp, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 495 | iowrite16(bt->sjw, &card->dpram[DPRAM_FCT_PARAM + 4]); |
| 496 | iowrite16(bt->phase_seg1 + bt->prop_seg, |
| 497 | &card->dpram[DPRAM_FCT_PARAM + 6]); |
| 498 | iowrite16(bt->phase_seg2, &card->dpram[DPRAM_FCT_PARAM + 8]); |
| 499 | iowrite16((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 1 : 0, |
| 500 | &card->dpram[DPRAM_FCT_PARAM + 10]); |
| 501 | ret = softing_fct_cmd(card, 1, "initialize_chip[0]"); |
| 502 | if (ret < 0) |
| 503 | goto failed; |
| 504 | /* set mode */ |
| 505 | iowrite16(0, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 506 | iowrite16(0, &card->dpram[DPRAM_FCT_PARAM + 4]); |
| 507 | ret = softing_fct_cmd(card, 3, "set_mode[0]"); |
| 508 | if (ret < 0) |
| 509 | goto failed; |
| 510 | /* set filter */ |
| 511 | /* 11bit id & mask */ |
| 512 | iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 513 | iowrite16(0x07ff, &card->dpram[DPRAM_FCT_PARAM + 4]); |
| 514 | /* 29bit id.lo & mask.lo & id.hi & mask.hi */ |
| 515 | iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 6]); |
| 516 | iowrite16(0xffff, &card->dpram[DPRAM_FCT_PARAM + 8]); |
| 517 | iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 10]); |
| 518 | iowrite16(0x1fff, &card->dpram[DPRAM_FCT_PARAM + 12]); |
| 519 | ret = softing_fct_cmd(card, 7, "set_filter[0]"); |
| 520 | if (ret < 0) |
| 521 | goto failed; |
| 522 | /* set output control */ |
| 523 | iowrite16(priv->output, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 524 | ret = softing_fct_cmd(card, 5, "set_output[0]"); |
| 525 | if (ret < 0) |
| 526 | goto failed; |
| 527 | } |
| 528 | if (bus_bitmask_start & 2) { |
| 529 | netdev = card->net[1]; |
| 530 | priv = netdev_priv(netdev); |
| 531 | error_reporting += softing_error_reporting(netdev); |
| 532 | /* init chip2 */ |
| 533 | bt = &priv->can.bittiming; |
| 534 | iowrite16(bt->brp, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 535 | iowrite16(bt->sjw, &card->dpram[DPRAM_FCT_PARAM + 4]); |
| 536 | iowrite16(bt->phase_seg1 + bt->prop_seg, |
| 537 | &card->dpram[DPRAM_FCT_PARAM + 6]); |
| 538 | iowrite16(bt->phase_seg2, &card->dpram[DPRAM_FCT_PARAM + 8]); |
| 539 | iowrite16((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) ? 1 : 0, |
| 540 | &card->dpram[DPRAM_FCT_PARAM + 10]); |
| 541 | ret = softing_fct_cmd(card, 2, "initialize_chip[1]"); |
| 542 | if (ret < 0) |
| 543 | goto failed; |
| 544 | /* set mode2 */ |
| 545 | iowrite16(0, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 546 | iowrite16(0, &card->dpram[DPRAM_FCT_PARAM + 4]); |
| 547 | ret = softing_fct_cmd(card, 4, "set_mode[1]"); |
| 548 | if (ret < 0) |
| 549 | goto failed; |
| 550 | /* set filter2 */ |
| 551 | /* 11bit id & mask */ |
| 552 | iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 553 | iowrite16(0x07ff, &card->dpram[DPRAM_FCT_PARAM + 4]); |
| 554 | /* 29bit id.lo & mask.lo & id.hi & mask.hi */ |
| 555 | iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 6]); |
| 556 | iowrite16(0xffff, &card->dpram[DPRAM_FCT_PARAM + 8]); |
| 557 | iowrite16(0x0000, &card->dpram[DPRAM_FCT_PARAM + 10]); |
| 558 | iowrite16(0x1fff, &card->dpram[DPRAM_FCT_PARAM + 12]); |
| 559 | ret = softing_fct_cmd(card, 8, "set_filter[1]"); |
| 560 | if (ret < 0) |
| 561 | goto failed; |
| 562 | /* set output control2 */ |
| 563 | iowrite16(priv->output, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 564 | ret = softing_fct_cmd(card, 6, "set_output[1]"); |
| 565 | if (ret < 0) |
| 566 | goto failed; |
| 567 | } |
| 568 | /* enable_error_frame */ |
| 569 | /* |
| 570 | * Error reporting is switched off at the moment since |
| 571 | * the receiving of them is not yet 100% verified |
| 572 | * This should be enabled sooner or later |
| 573 | * |
| 574 | if (error_reporting) { |
| 575 | ret = softing_fct_cmd(card, 51, "enable_error_frame"); |
| 576 | if (ret < 0) |
| 577 | goto failed; |
| 578 | } |
| 579 | */ |
| 580 | /* initialize interface */ |
| 581 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 2]); |
| 582 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 4]); |
| 583 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 6]); |
| 584 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 8]); |
| 585 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 10]); |
| 586 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 12]); |
| 587 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 14]); |
| 588 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 16]); |
| 589 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 18]); |
| 590 | iowrite16(1, &card->dpram[DPRAM_FCT_PARAM + 20]); |
| 591 | ret = softing_fct_cmd(card, 17, "initialize_interface"); |
| 592 | if (ret < 0) |
| 593 | goto failed; |
| 594 | /* enable_fifo */ |
| 595 | ret = softing_fct_cmd(card, 36, "enable_fifo"); |
| 596 | if (ret < 0) |
| 597 | goto failed; |
| 598 | /* enable fifo tx ack */ |
| 599 | ret = softing_fct_cmd(card, 13, "fifo_tx_ack[0]"); |
| 600 | if (ret < 0) |
| 601 | goto failed; |
| 602 | /* enable fifo tx ack2 */ |
| 603 | ret = softing_fct_cmd(card, 14, "fifo_tx_ack[1]"); |
| 604 | if (ret < 0) |
| 605 | goto failed; |
| 606 | /* start_chip */ |
| 607 | ret = softing_fct_cmd(card, 11, "start_chip"); |
| 608 | if (ret < 0) |
| 609 | goto failed; |
| 610 | iowrite8(0, &card->dpram[DPRAM_INFO_BUSSTATE]); |
| 611 | iowrite8(0, &card->dpram[DPRAM_INFO_BUSSTATE2]); |
| 612 | if (card->pdat->generation < 2) { |
| 613 | iowrite8(0, &card->dpram[DPRAM_V2_IRQ_TOHOST]); |
| 614 | /* flush the DPRAM caches */ |
| 615 | wmb(); |
| 616 | } |
| 617 | |
| 618 | softing_initialize_timestamp(card); |
| 619 | |
| 620 | /* |
| 621 | * do socketcan notifications/status changes |
| 622 | * from here, no errors should occur, or the failed: part |
| 623 | * must be reviewed |
| 624 | */ |
| 625 | memset(&msg, 0, sizeof(msg)); |
| 626 | msg.can_id = CAN_ERR_FLAG | CAN_ERR_RESTARTED; |
| 627 | msg.can_dlc = CAN_ERR_DLC; |
| 628 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 629 | if (!(bus_bitmask_start & (1 << j))) |
| 630 | continue; |
| 631 | netdev = card->net[j]; |
| 632 | if (!netdev) |
| 633 | continue; |
| 634 | priv = netdev_priv(netdev); |
| 635 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
| 636 | open_candev(netdev); |
| 637 | if (dev != netdev) { |
| 638 | /* notify other busses on the restart */ |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 639 | softing_netdev_rx(netdev, &msg, 0); |
Kurt Van Dijck | 03fd3cf | 2011-01-11 04:32:31 +0000 | [diff] [blame] | 640 | ++priv->can.can_stats.restarts; |
| 641 | } |
| 642 | netif_wake_queue(netdev); |
| 643 | } |
| 644 | |
| 645 | /* enable interrupts */ |
| 646 | ret = softing_enable_irq(card, 1); |
| 647 | if (ret) |
| 648 | goto failed; |
| 649 | card_done: |
| 650 | mutex_unlock(&card->fw.lock); |
| 651 | return 0; |
| 652 | invalid: |
| 653 | ret = -EINVAL; |
| 654 | failed: |
| 655 | softing_enable_irq(card, 0); |
| 656 | softing_reset_chip(card); |
| 657 | mutex_unlock(&card->fw.lock); |
| 658 | /* bring all other interfaces down */ |
| 659 | for (j = 0; j < ARRAY_SIZE(card->net); ++j) { |
| 660 | netdev = card->net[j]; |
| 661 | if (!netdev) |
| 662 | continue; |
| 663 | dev_close(netdev); |
| 664 | } |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | int softing_default_output(struct net_device *netdev) |
| 669 | { |
| 670 | struct softing_priv *priv = netdev_priv(netdev); |
| 671 | struct softing *card = priv->card; |
| 672 | |
| 673 | switch (priv->chip) { |
| 674 | case 1000: |
| 675 | return (card->pdat->generation < 2) ? 0xfb : 0xfa; |
| 676 | case 5: |
| 677 | return 0x60; |
| 678 | default: |
| 679 | return 0x40; |
| 680 | } |
| 681 | } |