| .. | .. |
|---|
| 5 | 5 | * specs for the same is available at <http://www.ti.com> |
|---|
| 6 | 6 | * |
|---|
| 7 | 7 | * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/ |
|---|
| 8 | + * Copyright (C) 2019 Jeroen Hofstee <jhofstee@victronenergy.com> |
|---|
| 8 | 9 | * |
|---|
| 9 | 10 | * This program is free software; you can redistribute it and/or |
|---|
| 10 | 11 | * modify it under the terms of the GNU General Public License as |
|---|
| .. | .. |
|---|
| 34 | 35 | #include <linux/can/dev.h> |
|---|
| 35 | 36 | #include <linux/can/error.h> |
|---|
| 36 | 37 | #include <linux/can/led.h> |
|---|
| 38 | +#include <linux/can/rx-offload.h> |
|---|
| 37 | 39 | |
|---|
| 38 | 40 | #define DRV_NAME "ti_hecc" |
|---|
| 39 | 41 | #define HECC_MODULE_VERSION "0.7" |
|---|
| .. | .. |
|---|
| 44 | 46 | #define HECC_MAX_MAILBOXES 32 /* hardware mailboxes - do not change */ |
|---|
| 45 | 47 | #define MAX_TX_PRIO 0x3F /* hardware value - do not change */ |
|---|
| 46 | 48 | |
|---|
| 47 | | -/* |
|---|
| 48 | | - * Important Note: TX mailbox configuration |
|---|
| 49 | +/* Important Note: TX mailbox configuration |
|---|
| 49 | 50 | * TX mailboxes should be restricted to the number of SKB buffers to avoid |
|---|
| 50 | 51 | * maintaining SKB buffers separately. TX mailboxes should be a power of 2 |
|---|
| 51 | 52 | * for the mailbox logic to work. Top mailbox numbers are reserved for RX |
|---|
| .. | .. |
|---|
| 63 | 64 | #define HECC_TX_PRIO_MASK (MAX_TX_PRIO << HECC_MB_TX_SHIFT) |
|---|
| 64 | 65 | #define HECC_TX_MB_MASK (HECC_MAX_TX_MBOX - 1) |
|---|
| 65 | 66 | #define HECC_TX_MASK ((HECC_MAX_TX_MBOX - 1) | HECC_TX_PRIO_MASK) |
|---|
| 66 | | -#define HECC_TX_MBOX_MASK (~(BIT(HECC_MAX_TX_MBOX) - 1)) |
|---|
| 67 | | -#define HECC_DEF_NAPI_WEIGHT HECC_MAX_RX_MBOX |
|---|
| 68 | 67 | |
|---|
| 69 | | -/* |
|---|
| 70 | | - * Important Note: RX mailbox configuration |
|---|
| 71 | | - * RX mailboxes are further logically split into two - main and buffer |
|---|
| 72 | | - * mailboxes. The goal is to get all packets into main mailboxes as |
|---|
| 73 | | - * driven by mailbox number and receive priority (higher to lower) and |
|---|
| 74 | | - * buffer mailboxes are used to receive pkts while main mailboxes are being |
|---|
| 75 | | - * processed. This ensures in-order packet reception. |
|---|
| 68 | +/* RX mailbox configuration |
|---|
| 76 | 69 | * |
|---|
| 77 | | - * Here are the recommended values for buffer mailbox. Note that RX mailboxes |
|---|
| 78 | | - * start after TX mailboxes: |
|---|
| 79 | | - * |
|---|
| 80 | | - * HECC_MAX_RX_MBOX HECC_RX_BUFFER_MBOX No of buffer mailboxes |
|---|
| 81 | | - * 28 12 8 |
|---|
| 82 | | - * 16 20 4 |
|---|
| 70 | + * The remaining mailboxes are used for reception and are delivered |
|---|
| 71 | + * based on their timestamp, to avoid a hardware race when CANME is |
|---|
| 72 | + * changed while CAN-bus traffic is being received. |
|---|
| 83 | 73 | */ |
|---|
| 84 | | - |
|---|
| 85 | 74 | #define HECC_MAX_RX_MBOX (HECC_MAX_MAILBOXES - HECC_MAX_TX_MBOX) |
|---|
| 86 | | -#define HECC_RX_BUFFER_MBOX 12 /* as per table above */ |
|---|
| 87 | 75 | #define HECC_RX_FIRST_MBOX (HECC_MAX_MAILBOXES - 1) |
|---|
| 88 | | -#define HECC_RX_HIGH_MBOX_MASK (~(BIT(HECC_RX_BUFFER_MBOX) - 1)) |
|---|
| 76 | +#define HECC_RX_LAST_MBOX (HECC_MAX_TX_MBOX) |
|---|
| 89 | 77 | |
|---|
| 90 | 78 | /* TI HECC module registers */ |
|---|
| 91 | 79 | #define HECC_CANME 0x0 /* Mailbox enable */ |
|---|
| .. | .. |
|---|
| 95 | 83 | #define HECC_CANTA 0x10 /* Transmission acknowledge */ |
|---|
| 96 | 84 | #define HECC_CANAA 0x14 /* Abort acknowledge */ |
|---|
| 97 | 85 | #define HECC_CANRMP 0x18 /* Receive message pending */ |
|---|
| 98 | | -#define HECC_CANRML 0x1C /* Remote message lost */ |
|---|
| 86 | +#define HECC_CANRML 0x1C /* Receive message lost */ |
|---|
| 99 | 87 | #define HECC_CANRFP 0x20 /* Remote frame pending */ |
|---|
| 100 | 88 | #define HECC_CANGAM 0x24 /* SECC only:Global acceptance mask */ |
|---|
| 101 | 89 | #define HECC_CANMC 0x28 /* Master control */ |
|---|
| .. | .. |
|---|
| 116 | 104 | #define HECC_CANTOS 0x64 /* HECC only: Time-out status */ |
|---|
| 117 | 105 | #define HECC_CANTIOCE 0x68 /* SCC only:Enhanced TX I/O control */ |
|---|
| 118 | 106 | #define HECC_CANRIOCE 0x6C /* SCC only:Enhanced RX I/O control */ |
|---|
| 107 | + |
|---|
| 108 | +/* TI HECC RAM registers */ |
|---|
| 109 | +#define HECC_CANMOTS 0x80 /* Message object time stamp */ |
|---|
| 119 | 110 | |
|---|
| 120 | 111 | /* Mailbox registers */ |
|---|
| 121 | 112 | #define HECC_CANMID 0x0 |
|---|
| .. | .. |
|---|
| 159 | 150 | #define HECC_BUS_ERROR (HECC_CANES_FE | HECC_CANES_BE |\ |
|---|
| 160 | 151 | HECC_CANES_CRCE | HECC_CANES_SE |\ |
|---|
| 161 | 152 | HECC_CANES_ACKE) |
|---|
| 153 | +#define HECC_CANES_FLAGS (HECC_BUS_ERROR | HECC_CANES_BO |\ |
|---|
| 154 | + HECC_CANES_EP | HECC_CANES_EW) |
|---|
| 162 | 155 | |
|---|
| 163 | 156 | #define HECC_CANMCF_RTR BIT(4) /* Remote transmit request */ |
|---|
| 164 | 157 | |
|---|
| .. | .. |
|---|
| 193 | 186 | |
|---|
| 194 | 187 | struct ti_hecc_priv { |
|---|
| 195 | 188 | struct can_priv can; /* MUST be first member/field */ |
|---|
| 196 | | - struct napi_struct napi; |
|---|
| 189 | + struct can_rx_offload offload; |
|---|
| 197 | 190 | struct net_device *ndev; |
|---|
| 198 | 191 | struct clk *clk; |
|---|
| 199 | 192 | void __iomem *base; |
|---|
| .. | .. |
|---|
| 203 | 196 | spinlock_t mbx_lock; /* CANME register needs protection */ |
|---|
| 204 | 197 | u32 tx_head; |
|---|
| 205 | 198 | u32 tx_tail; |
|---|
| 206 | | - u32 rx_next; |
|---|
| 207 | 199 | struct regulator *reg_xceiver; |
|---|
| 208 | 200 | }; |
|---|
| 209 | 201 | |
|---|
| .. | .. |
|---|
| 227 | 219 | __raw_writel(val, priv->hecc_ram + mbxno * 4); |
|---|
| 228 | 220 | } |
|---|
| 229 | 221 | |
|---|
| 222 | +static inline u32 hecc_read_stamp(struct ti_hecc_priv *priv, u32 mbxno) |
|---|
| 223 | +{ |
|---|
| 224 | + return __raw_readl(priv->hecc_ram + HECC_CANMOTS + mbxno * 4); |
|---|
| 225 | +} |
|---|
| 226 | + |
|---|
| 230 | 227 | static inline void hecc_write_mbx(struct ti_hecc_priv *priv, u32 mbxno, |
|---|
| 231 | | - u32 reg, u32 val) |
|---|
| 228 | + u32 reg, u32 val) |
|---|
| 232 | 229 | { |
|---|
| 233 | 230 | __raw_writel(val, priv->mbx + mbxno * 0x10 + reg); |
|---|
| 234 | 231 | } |
|---|
| .. | .. |
|---|
| 249 | 246 | } |
|---|
| 250 | 247 | |
|---|
| 251 | 248 | static inline void hecc_set_bit(struct ti_hecc_priv *priv, int reg, |
|---|
| 252 | | - u32 bit_mask) |
|---|
| 249 | + u32 bit_mask) |
|---|
| 253 | 250 | { |
|---|
| 254 | 251 | hecc_write(priv, reg, hecc_read(priv, reg) | bit_mask); |
|---|
| 255 | 252 | } |
|---|
| 256 | 253 | |
|---|
| 257 | 254 | static inline void hecc_clear_bit(struct ti_hecc_priv *priv, int reg, |
|---|
| 258 | | - u32 bit_mask) |
|---|
| 255 | + u32 bit_mask) |
|---|
| 259 | 256 | { |
|---|
| 260 | 257 | hecc_write(priv, reg, hecc_read(priv, reg) & ~bit_mask); |
|---|
| 261 | 258 | } |
|---|
| .. | .. |
|---|
| 277 | 274 | if (bit_timing->brp > 4) |
|---|
| 278 | 275 | can_btc |= HECC_CANBTC_SAM; |
|---|
| 279 | 276 | else |
|---|
| 280 | | - netdev_warn(priv->ndev, "WARN: Triple" |
|---|
| 281 | | - "sampling not set due to h/w limitations"); |
|---|
| 277 | + netdev_warn(priv->ndev, |
|---|
| 278 | + "WARN: Triple sampling not set due to h/w limitations"); |
|---|
| 282 | 279 | } |
|---|
| 283 | 280 | can_btc |= ((bit_timing->sjw - 1) & 0x3) << 8; |
|---|
| 284 | 281 | can_btc |= ((bit_timing->brp - 1) & 0xFF) << 16; |
|---|
| .. | .. |
|---|
| 314 | 311 | /* Set change control request and wait till enabled */ |
|---|
| 315 | 312 | hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR); |
|---|
| 316 | 313 | |
|---|
| 317 | | - /* |
|---|
| 318 | | - * INFO: It has been observed that at times CCE bit may not be |
|---|
| 314 | + /* INFO: It has been observed that at times CCE bit may not be |
|---|
| 319 | 315 | * set and hw seems to be ok even if this bit is not set so |
|---|
| 320 | 316 | * timing out with a timing of 1ms to respect the specs |
|---|
| 321 | 317 | */ |
|---|
| .. | .. |
|---|
| 325 | 321 | udelay(10); |
|---|
| 326 | 322 | } |
|---|
| 327 | 323 | |
|---|
| 328 | | - /* |
|---|
| 329 | | - * Note: On HECC, BTC can be programmed only in initialization mode, so |
|---|
| 324 | + /* Note: On HECC, BTC can be programmed only in initialization mode, so |
|---|
| 330 | 325 | * it is expected that the can bittiming parameters are set via ip |
|---|
| 331 | 326 | * utility before the device is opened |
|---|
| 332 | 327 | */ |
|---|
| .. | .. |
|---|
| 335 | 330 | /* Clear CCR (and CANMC register) and wait for CCE = 0 enable */ |
|---|
| 336 | 331 | hecc_write(priv, HECC_CANMC, 0); |
|---|
| 337 | 332 | |
|---|
| 338 | | - /* |
|---|
| 339 | | - * INFO: CAN net stack handles bus off and hence disabling auto-bus-on |
|---|
| 333 | + /* INFO: CAN net stack handles bus off and hence disabling auto-bus-on |
|---|
| 340 | 334 | * hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_ABO); |
|---|
| 341 | 335 | */ |
|---|
| 342 | 336 | |
|---|
| 343 | | - /* |
|---|
| 344 | | - * INFO: It has been observed that at times CCE bit may not be |
|---|
| 337 | + /* INFO: It has been observed that at times CCE bit may not be |
|---|
| 345 | 338 | * set and hw seems to be ok even if this bit is not set so |
|---|
| 346 | 339 | */ |
|---|
| 347 | 340 | cnt = HECC_CCE_WAIT_COUNT; |
|---|
| .. | .. |
|---|
| 374 | 367 | /* put HECC in initialization mode and set btc */ |
|---|
| 375 | 368 | ti_hecc_reset(ndev); |
|---|
| 376 | 369 | |
|---|
| 377 | | - priv->tx_head = priv->tx_tail = HECC_TX_MASK; |
|---|
| 378 | | - priv->rx_next = HECC_RX_FIRST_MBOX; |
|---|
| 370 | + priv->tx_head = HECC_TX_MASK; |
|---|
| 371 | + priv->tx_tail = HECC_TX_MASK; |
|---|
| 379 | 372 | |
|---|
| 380 | 373 | /* Enable local and global acceptance mask registers */ |
|---|
| 381 | 374 | hecc_write(priv, HECC_CANGAM, HECC_SET_REG); |
|---|
| .. | .. |
|---|
| 392 | 385 | hecc_set_bit(priv, HECC_CANMIM, mbx_mask); |
|---|
| 393 | 386 | } |
|---|
| 394 | 387 | |
|---|
| 395 | | - /* Prevent message over-write & Enable interrupts */ |
|---|
| 396 | | - hecc_write(priv, HECC_CANOPC, HECC_SET_REG); |
|---|
| 388 | + /* Enable tx interrupts */ |
|---|
| 389 | + hecc_set_bit(priv, HECC_CANMIM, BIT(HECC_MAX_TX_MBOX) - 1); |
|---|
| 390 | + |
|---|
| 391 | + /* Prevent message over-write to create a rx fifo, but not for |
|---|
| 392 | + * the lowest priority mailbox, since that allows detecting |
|---|
| 393 | + * overflows instead of the hardware silently dropping the |
|---|
| 394 | + * messages. |
|---|
| 395 | + */ |
|---|
| 396 | + mbx_mask = ~BIT(HECC_RX_LAST_MBOX); |
|---|
| 397 | + hecc_write(priv, HECC_CANOPC, mbx_mask); |
|---|
| 398 | + |
|---|
| 399 | + /* Enable interrupts */ |
|---|
| 397 | 400 | if (priv->use_hecc1int) { |
|---|
| 398 | 401 | hecc_write(priv, HECC_CANMIL, HECC_SET_REG); |
|---|
| 399 | 402 | hecc_write(priv, HECC_CANGIM, HECC_CANGIM_DEF_MASK | |
|---|
| .. | .. |
|---|
| 401 | 404 | } else { |
|---|
| 402 | 405 | hecc_write(priv, HECC_CANMIL, 0); |
|---|
| 403 | 406 | hecc_write(priv, HECC_CANGIM, |
|---|
| 404 | | - HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN); |
|---|
| 407 | + HECC_CANGIM_DEF_MASK | HECC_CANGIM_I0EN); |
|---|
| 405 | 408 | } |
|---|
| 406 | 409 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
|---|
| 407 | 410 | } |
|---|
| .. | .. |
|---|
| 409 | 412 | static void ti_hecc_stop(struct net_device *ndev) |
|---|
| 410 | 413 | { |
|---|
| 411 | 414 | struct ti_hecc_priv *priv = netdev_priv(ndev); |
|---|
| 415 | + |
|---|
| 416 | + /* Disable the CPK; stop sending, erroring and acking */ |
|---|
| 417 | + hecc_set_bit(priv, HECC_CANMC, HECC_CANMC_CCR); |
|---|
| 412 | 418 | |
|---|
| 413 | 419 | /* Disable interrupts and disable mailboxes */ |
|---|
| 414 | 420 | hecc_write(priv, HECC_CANGIM, 0); |
|---|
| .. | .. |
|---|
| 435 | 441 | } |
|---|
| 436 | 442 | |
|---|
| 437 | 443 | static int ti_hecc_get_berr_counter(const struct net_device *ndev, |
|---|
| 438 | | - struct can_berr_counter *bec) |
|---|
| 444 | + struct can_berr_counter *bec) |
|---|
| 439 | 445 | { |
|---|
| 440 | 446 | struct ti_hecc_priv *priv = netdev_priv(ndev); |
|---|
| 441 | 447 | |
|---|
| .. | .. |
|---|
| 445 | 451 | return 0; |
|---|
| 446 | 452 | } |
|---|
| 447 | 453 | |
|---|
| 448 | | -/* |
|---|
| 449 | | - * ti_hecc_xmit: HECC Transmit |
|---|
| 454 | +/* ti_hecc_xmit: HECC Transmit |
|---|
| 450 | 455 | * |
|---|
| 451 | 456 | * The transmit mailboxes start from 0 to HECC_MAX_TX_MBOX. In HECC the |
|---|
| 452 | | - * priority of the mailbox for tranmission is dependent upon priority setting |
|---|
| 457 | + * priority of the mailbox for transmission is dependent upon priority setting |
|---|
| 453 | 458 | * field in mailbox registers. The mailbox with highest value in priority field |
|---|
| 454 | 459 | * is transmitted first. Only when two mailboxes have the same value in |
|---|
| 455 | 460 | * priority field the highest numbered mailbox is transmitted first. |
|---|
| .. | .. |
|---|
| 484 | 489 | spin_unlock_irqrestore(&priv->mbx_lock, flags); |
|---|
| 485 | 490 | netif_stop_queue(ndev); |
|---|
| 486 | 491 | netdev_err(priv->ndev, |
|---|
| 487 | | - "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n", |
|---|
| 488 | | - priv->tx_head, priv->tx_tail); |
|---|
| 492 | + "BUG: TX mbx not ready tx_head=%08X, tx_tail=%08X\n", |
|---|
| 493 | + priv->tx_head, priv->tx_tail); |
|---|
| 489 | 494 | return NETDEV_TX_BUSY; |
|---|
| 490 | 495 | } |
|---|
| 491 | 496 | spin_unlock_irqrestore(&priv->mbx_lock, flags); |
|---|
| .. | .. |
|---|
| 502 | 507 | data = (cf->can_id & CAN_SFF_MASK) << 18; |
|---|
| 503 | 508 | hecc_write_mbx(priv, mbxno, HECC_CANMID, data); |
|---|
| 504 | 509 | hecc_write_mbx(priv, mbxno, HECC_CANMDL, |
|---|
| 505 | | - be32_to_cpu(*(__be32 *)(cf->data))); |
|---|
| 510 | + be32_to_cpu(*(__be32 *)(cf->data))); |
|---|
| 506 | 511 | if (cf->can_dlc > 4) |
|---|
| 507 | 512 | hecc_write_mbx(priv, mbxno, HECC_CANMDH, |
|---|
| 508 | | - be32_to_cpu(*(__be32 *)(cf->data + 4))); |
|---|
| 513 | + be32_to_cpu(*(__be32 *)(cf->data + 4))); |
|---|
| 509 | 514 | else |
|---|
| 510 | 515 | *(u32 *)(cf->data + 4) = 0; |
|---|
| 511 | 516 | can_put_echo_skb(skb, ndev, mbxno); |
|---|
| .. | .. |
|---|
| 513 | 518 | spin_lock_irqsave(&priv->mbx_lock, flags); |
|---|
| 514 | 519 | --priv->tx_head; |
|---|
| 515 | 520 | if ((hecc_read(priv, HECC_CANME) & BIT(get_tx_head_mb(priv))) || |
|---|
| 516 | | - (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) { |
|---|
| 521 | + (priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK) { |
|---|
| 517 | 522 | netif_stop_queue(ndev); |
|---|
| 518 | 523 | } |
|---|
| 519 | 524 | hecc_set_bit(priv, HECC_CANME, mbx_mask); |
|---|
| 520 | 525 | spin_unlock_irqrestore(&priv->mbx_lock, flags); |
|---|
| 521 | 526 | |
|---|
| 522 | | - hecc_clear_bit(priv, HECC_CANMD, mbx_mask); |
|---|
| 523 | | - hecc_set_bit(priv, HECC_CANMIM, mbx_mask); |
|---|
| 524 | 527 | hecc_write(priv, HECC_CANTRS, mbx_mask); |
|---|
| 525 | 528 | |
|---|
| 526 | 529 | return NETDEV_TX_OK; |
|---|
| 527 | 530 | } |
|---|
| 528 | 531 | |
|---|
| 529 | | -static int ti_hecc_rx_pkt(struct ti_hecc_priv *priv, int mbxno) |
|---|
| 532 | +static inline |
|---|
| 533 | +struct ti_hecc_priv *rx_offload_to_priv(struct can_rx_offload *offload) |
|---|
| 530 | 534 | { |
|---|
| 531 | | - struct net_device_stats *stats = &priv->ndev->stats; |
|---|
| 532 | | - struct can_frame *cf; |
|---|
| 533 | | - struct sk_buff *skb; |
|---|
| 534 | | - u32 data, mbx_mask; |
|---|
| 535 | | - unsigned long flags; |
|---|
| 535 | + return container_of(offload, struct ti_hecc_priv, offload); |
|---|
| 536 | +} |
|---|
| 536 | 537 | |
|---|
| 537 | | - skb = alloc_can_skb(priv->ndev, &cf); |
|---|
| 538 | | - if (!skb) { |
|---|
| 539 | | - if (printk_ratelimit()) |
|---|
| 540 | | - netdev_err(priv->ndev, |
|---|
| 541 | | - "ti_hecc_rx_pkt: alloc_can_skb() failed\n"); |
|---|
| 542 | | - return -ENOMEM; |
|---|
| 543 | | - } |
|---|
| 538 | +static struct sk_buff *ti_hecc_mailbox_read(struct can_rx_offload *offload, |
|---|
| 539 | + unsigned int mbxno, u32 *timestamp, |
|---|
| 540 | + bool drop) |
|---|
| 541 | +{ |
|---|
| 542 | + struct ti_hecc_priv *priv = rx_offload_to_priv(offload); |
|---|
| 543 | + struct sk_buff *skb; |
|---|
| 544 | + struct can_frame *cf; |
|---|
| 545 | + u32 data, mbx_mask; |
|---|
| 544 | 546 | |
|---|
| 545 | 547 | mbx_mask = BIT(mbxno); |
|---|
| 548 | + |
|---|
| 549 | + if (unlikely(drop)) { |
|---|
| 550 | + skb = ERR_PTR(-ENOBUFS); |
|---|
| 551 | + goto mark_as_read; |
|---|
| 552 | + } |
|---|
| 553 | + |
|---|
| 554 | + skb = alloc_can_skb(offload->dev, &cf); |
|---|
| 555 | + if (unlikely(!skb)) { |
|---|
| 556 | + skb = ERR_PTR(-ENOMEM); |
|---|
| 557 | + goto mark_as_read; |
|---|
| 558 | + } |
|---|
| 559 | + |
|---|
| 546 | 560 | data = hecc_read_mbx(priv, mbxno, HECC_CANMID); |
|---|
| 547 | 561 | if (data & HECC_CANMID_IDE) |
|---|
| 548 | 562 | cf->can_id = (data & CAN_EFF_MASK) | CAN_EFF_FLAG; |
|---|
| 549 | 563 | else |
|---|
| 550 | 564 | cf->can_id = (data >> 18) & CAN_SFF_MASK; |
|---|
| 565 | + |
|---|
| 551 | 566 | data = hecc_read_mbx(priv, mbxno, HECC_CANMCF); |
|---|
| 552 | 567 | if (data & HECC_CANMCF_RTR) |
|---|
| 553 | 568 | cf->can_id |= CAN_RTR_FLAG; |
|---|
| 554 | 569 | cf->can_dlc = get_can_dlc(data & 0xF); |
|---|
| 570 | + |
|---|
| 555 | 571 | data = hecc_read_mbx(priv, mbxno, HECC_CANMDL); |
|---|
| 556 | 572 | *(__be32 *)(cf->data) = cpu_to_be32(data); |
|---|
| 557 | 573 | if (cf->can_dlc > 4) { |
|---|
| 558 | 574 | data = hecc_read_mbx(priv, mbxno, HECC_CANMDH); |
|---|
| 559 | 575 | *(__be32 *)(cf->data + 4) = cpu_to_be32(data); |
|---|
| 560 | 576 | } |
|---|
| 561 | | - spin_lock_irqsave(&priv->mbx_lock, flags); |
|---|
| 562 | | - hecc_clear_bit(priv, HECC_CANME, mbx_mask); |
|---|
| 577 | + |
|---|
| 578 | + *timestamp = hecc_read_stamp(priv, mbxno); |
|---|
| 579 | + |
|---|
| 580 | + /* Check for FIFO overrun. |
|---|
| 581 | + * |
|---|
| 582 | + * All but the last RX mailbox have activated overwrite |
|---|
| 583 | + * protection. So skip check for overrun, if we're not |
|---|
| 584 | + * handling the last RX mailbox. |
|---|
| 585 | + * |
|---|
| 586 | + * As the overwrite protection for the last RX mailbox is |
|---|
| 587 | + * disabled, the CAN core might update while we're reading |
|---|
| 588 | + * it. This means the skb might be inconsistent. |
|---|
| 589 | + * |
|---|
| 590 | + * Return an error to let rx-offload discard this CAN frame. |
|---|
| 591 | + */ |
|---|
| 592 | + if (unlikely(mbxno == HECC_RX_LAST_MBOX && |
|---|
| 593 | + hecc_read(priv, HECC_CANRML) & mbx_mask)) |
|---|
| 594 | + skb = ERR_PTR(-ENOBUFS); |
|---|
| 595 | + |
|---|
| 596 | + mark_as_read: |
|---|
| 563 | 597 | hecc_write(priv, HECC_CANRMP, mbx_mask); |
|---|
| 564 | | - /* enable mailbox only if it is part of rx buffer mailboxes */ |
|---|
| 565 | | - if (priv->rx_next < HECC_RX_BUFFER_MBOX) |
|---|
| 566 | | - hecc_set_bit(priv, HECC_CANME, mbx_mask); |
|---|
| 567 | | - spin_unlock_irqrestore(&priv->mbx_lock, flags); |
|---|
| 568 | 598 | |
|---|
| 569 | | - stats->rx_bytes += cf->can_dlc; |
|---|
| 570 | | - can_led_event(priv->ndev, CAN_LED_EVENT_RX); |
|---|
| 571 | | - netif_receive_skb(skb); |
|---|
| 572 | | - stats->rx_packets++; |
|---|
| 573 | | - |
|---|
| 574 | | - return 0; |
|---|
| 575 | | -} |
|---|
| 576 | | - |
|---|
| 577 | | -/* |
|---|
| 578 | | - * ti_hecc_rx_poll - HECC receive pkts |
|---|
| 579 | | - * |
|---|
| 580 | | - * The receive mailboxes start from highest numbered mailbox till last xmit |
|---|
| 581 | | - * mailbox. On CAN frame reception the hardware places the data into highest |
|---|
| 582 | | - * numbered mailbox that matches the CAN ID filter. Since all receive mailboxes |
|---|
| 583 | | - * have same filtering (ALL CAN frames) packets will arrive in the highest |
|---|
| 584 | | - * available RX mailbox and we need to ensure in-order packet reception. |
|---|
| 585 | | - * |
|---|
| 586 | | - * To ensure the packets are received in the right order we logically divide |
|---|
| 587 | | - * the RX mailboxes into main and buffer mailboxes. Packets are received as per |
|---|
| 588 | | - * mailbox priotity (higher to lower) in the main bank and once it is full we |
|---|
| 589 | | - * disable further reception into main mailboxes. While the main mailboxes are |
|---|
| 590 | | - * processed in NAPI, further packets are received in buffer mailboxes. |
|---|
| 591 | | - * |
|---|
| 592 | | - * We maintain a RX next mailbox counter to process packets and once all main |
|---|
| 593 | | - * mailboxe packets are passed to the upper stack we enable all of them but |
|---|
| 594 | | - * continue to process packets received in buffer mailboxes. With each packet |
|---|
| 595 | | - * received from buffer mailbox we enable it immediately so as to handle the |
|---|
| 596 | | - * overflow from higher mailboxes. |
|---|
| 597 | | - */ |
|---|
| 598 | | -static int ti_hecc_rx_poll(struct napi_struct *napi, int quota) |
|---|
| 599 | | -{ |
|---|
| 600 | | - struct net_device *ndev = napi->dev; |
|---|
| 601 | | - struct ti_hecc_priv *priv = netdev_priv(ndev); |
|---|
| 602 | | - u32 num_pkts = 0; |
|---|
| 603 | | - u32 mbx_mask; |
|---|
| 604 | | - unsigned long pending_pkts, flags; |
|---|
| 605 | | - |
|---|
| 606 | | - if (!netif_running(ndev)) |
|---|
| 607 | | - return 0; |
|---|
| 608 | | - |
|---|
| 609 | | - while ((pending_pkts = hecc_read(priv, HECC_CANRMP)) && |
|---|
| 610 | | - num_pkts < quota) { |
|---|
| 611 | | - mbx_mask = BIT(priv->rx_next); /* next rx mailbox to process */ |
|---|
| 612 | | - if (mbx_mask & pending_pkts) { |
|---|
| 613 | | - if (ti_hecc_rx_pkt(priv, priv->rx_next) < 0) |
|---|
| 614 | | - return num_pkts; |
|---|
| 615 | | - ++num_pkts; |
|---|
| 616 | | - } else if (priv->rx_next > HECC_RX_BUFFER_MBOX) { |
|---|
| 617 | | - break; /* pkt not received yet */ |
|---|
| 618 | | - } |
|---|
| 619 | | - --priv->rx_next; |
|---|
| 620 | | - if (priv->rx_next == HECC_RX_BUFFER_MBOX) { |
|---|
| 621 | | - /* enable high bank mailboxes */ |
|---|
| 622 | | - spin_lock_irqsave(&priv->mbx_lock, flags); |
|---|
| 623 | | - mbx_mask = hecc_read(priv, HECC_CANME); |
|---|
| 624 | | - mbx_mask |= HECC_RX_HIGH_MBOX_MASK; |
|---|
| 625 | | - hecc_write(priv, HECC_CANME, mbx_mask); |
|---|
| 626 | | - spin_unlock_irqrestore(&priv->mbx_lock, flags); |
|---|
| 627 | | - } else if (priv->rx_next == HECC_MAX_TX_MBOX - 1) { |
|---|
| 628 | | - priv->rx_next = HECC_RX_FIRST_MBOX; |
|---|
| 629 | | - break; |
|---|
| 630 | | - } |
|---|
| 631 | | - } |
|---|
| 632 | | - |
|---|
| 633 | | - /* Enable packet interrupt if all pkts are handled */ |
|---|
| 634 | | - if (hecc_read(priv, HECC_CANRMP) == 0) { |
|---|
| 635 | | - napi_complete(napi); |
|---|
| 636 | | - /* Re-enable RX mailbox interrupts */ |
|---|
| 637 | | - mbx_mask = hecc_read(priv, HECC_CANMIM); |
|---|
| 638 | | - mbx_mask |= HECC_TX_MBOX_MASK; |
|---|
| 639 | | - hecc_write(priv, HECC_CANMIM, mbx_mask); |
|---|
| 640 | | - } else { |
|---|
| 641 | | - /* repoll is done only if whole budget is used */ |
|---|
| 642 | | - num_pkts = quota; |
|---|
| 643 | | - } |
|---|
| 644 | | - |
|---|
| 645 | | - return num_pkts; |
|---|
| 599 | + return skb; |
|---|
| 646 | 600 | } |
|---|
| 647 | 601 | |
|---|
| 648 | 602 | static int ti_hecc_error(struct net_device *ndev, int int_status, |
|---|
| 649 | | - int err_status) |
|---|
| 603 | + int err_status) |
|---|
| 650 | 604 | { |
|---|
| 651 | 605 | struct ti_hecc_priv *priv = netdev_priv(ndev); |
|---|
| 652 | | - struct net_device_stats *stats = &ndev->stats; |
|---|
| 653 | 606 | struct can_frame *cf; |
|---|
| 654 | 607 | struct sk_buff *skb; |
|---|
| 655 | | - |
|---|
| 656 | | - /* propagate the error condition to the can stack */ |
|---|
| 657 | | - skb = alloc_can_err_skb(ndev, &cf); |
|---|
| 658 | | - if (!skb) { |
|---|
| 659 | | - if (printk_ratelimit()) |
|---|
| 660 | | - netdev_err(priv->ndev, |
|---|
| 661 | | - "ti_hecc_error: alloc_can_err_skb() failed\n"); |
|---|
| 662 | | - return -ENOMEM; |
|---|
| 663 | | - } |
|---|
| 664 | | - |
|---|
| 665 | | - if (int_status & HECC_CANGIF_WLIF) { /* warning level int */ |
|---|
| 666 | | - if ((int_status & HECC_CANGIF_BOIF) == 0) { |
|---|
| 667 | | - priv->can.state = CAN_STATE_ERROR_WARNING; |
|---|
| 668 | | - ++priv->can.can_stats.error_warning; |
|---|
| 669 | | - cf->can_id |= CAN_ERR_CRTL; |
|---|
| 670 | | - if (hecc_read(priv, HECC_CANTEC) > 96) |
|---|
| 671 | | - cf->data[1] |= CAN_ERR_CRTL_TX_WARNING; |
|---|
| 672 | | - if (hecc_read(priv, HECC_CANREC) > 96) |
|---|
| 673 | | - cf->data[1] |= CAN_ERR_CRTL_RX_WARNING; |
|---|
| 674 | | - } |
|---|
| 675 | | - hecc_set_bit(priv, HECC_CANES, HECC_CANES_EW); |
|---|
| 676 | | - netdev_dbg(priv->ndev, "Error Warning interrupt\n"); |
|---|
| 677 | | - hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR); |
|---|
| 678 | | - } |
|---|
| 679 | | - |
|---|
| 680 | | - if (int_status & HECC_CANGIF_EPIF) { /* error passive int */ |
|---|
| 681 | | - if ((int_status & HECC_CANGIF_BOIF) == 0) { |
|---|
| 682 | | - priv->can.state = CAN_STATE_ERROR_PASSIVE; |
|---|
| 683 | | - ++priv->can.can_stats.error_passive; |
|---|
| 684 | | - cf->can_id |= CAN_ERR_CRTL; |
|---|
| 685 | | - if (hecc_read(priv, HECC_CANTEC) > 127) |
|---|
| 686 | | - cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE; |
|---|
| 687 | | - if (hecc_read(priv, HECC_CANREC) > 127) |
|---|
| 688 | | - cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE; |
|---|
| 689 | | - } |
|---|
| 690 | | - hecc_set_bit(priv, HECC_CANES, HECC_CANES_EP); |
|---|
| 691 | | - netdev_dbg(priv->ndev, "Error passive interrupt\n"); |
|---|
| 692 | | - hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR); |
|---|
| 693 | | - } |
|---|
| 694 | | - |
|---|
| 695 | | - /* |
|---|
| 696 | | - * Need to check busoff condition in error status register too to |
|---|
| 697 | | - * ensure warning interrupts don't hog the system |
|---|
| 698 | | - */ |
|---|
| 699 | | - if ((int_status & HECC_CANGIF_BOIF) || (err_status & HECC_CANES_BO)) { |
|---|
| 700 | | - priv->can.state = CAN_STATE_BUS_OFF; |
|---|
| 701 | | - cf->can_id |= CAN_ERR_BUSOFF; |
|---|
| 702 | | - hecc_set_bit(priv, HECC_CANES, HECC_CANES_BO); |
|---|
| 703 | | - hecc_clear_bit(priv, HECC_CANMC, HECC_CANMC_CCR); |
|---|
| 704 | | - /* Disable all interrupts in bus-off to avoid int hog */ |
|---|
| 705 | | - hecc_write(priv, HECC_CANGIM, 0); |
|---|
| 706 | | - ++priv->can.can_stats.bus_off; |
|---|
| 707 | | - can_bus_off(ndev); |
|---|
| 708 | | - } |
|---|
| 608 | + u32 timestamp; |
|---|
| 609 | + int err; |
|---|
| 709 | 610 | |
|---|
| 710 | 611 | if (err_status & HECC_BUS_ERROR) { |
|---|
| 612 | + /* propagate the error condition to the can stack */ |
|---|
| 613 | + skb = alloc_can_err_skb(ndev, &cf); |
|---|
| 614 | + if (!skb) { |
|---|
| 615 | + if (net_ratelimit()) |
|---|
| 616 | + netdev_err(priv->ndev, |
|---|
| 617 | + "%s: alloc_can_err_skb() failed\n", |
|---|
| 618 | + __func__); |
|---|
| 619 | + return -ENOMEM; |
|---|
| 620 | + } |
|---|
| 621 | + |
|---|
| 711 | 622 | ++priv->can.can_stats.bus_error; |
|---|
| 712 | 623 | cf->can_id |= CAN_ERR_BUSERROR | CAN_ERR_PROT; |
|---|
| 713 | | - if (err_status & HECC_CANES_FE) { |
|---|
| 714 | | - hecc_set_bit(priv, HECC_CANES, HECC_CANES_FE); |
|---|
| 624 | + if (err_status & HECC_CANES_FE) |
|---|
| 715 | 625 | cf->data[2] |= CAN_ERR_PROT_FORM; |
|---|
| 716 | | - } |
|---|
| 717 | | - if (err_status & HECC_CANES_BE) { |
|---|
| 718 | | - hecc_set_bit(priv, HECC_CANES, HECC_CANES_BE); |
|---|
| 626 | + if (err_status & HECC_CANES_BE) |
|---|
| 719 | 627 | cf->data[2] |= CAN_ERR_PROT_BIT; |
|---|
| 720 | | - } |
|---|
| 721 | | - if (err_status & HECC_CANES_SE) { |
|---|
| 722 | | - hecc_set_bit(priv, HECC_CANES, HECC_CANES_SE); |
|---|
| 628 | + if (err_status & HECC_CANES_SE) |
|---|
| 723 | 629 | cf->data[2] |= CAN_ERR_PROT_STUFF; |
|---|
| 724 | | - } |
|---|
| 725 | | - if (err_status & HECC_CANES_CRCE) { |
|---|
| 726 | | - hecc_set_bit(priv, HECC_CANES, HECC_CANES_CRCE); |
|---|
| 630 | + if (err_status & HECC_CANES_CRCE) |
|---|
| 727 | 631 | cf->data[3] = CAN_ERR_PROT_LOC_CRC_SEQ; |
|---|
| 728 | | - } |
|---|
| 729 | | - if (err_status & HECC_CANES_ACKE) { |
|---|
| 730 | | - hecc_set_bit(priv, HECC_CANES, HECC_CANES_ACKE); |
|---|
| 632 | + if (err_status & HECC_CANES_ACKE) |
|---|
| 731 | 633 | cf->data[3] = CAN_ERR_PROT_LOC_ACK; |
|---|
| 732 | | - } |
|---|
| 634 | + |
|---|
| 635 | + timestamp = hecc_read(priv, HECC_CANLNT); |
|---|
| 636 | + err = can_rx_offload_queue_sorted(&priv->offload, skb, |
|---|
| 637 | + timestamp); |
|---|
| 638 | + if (err) |
|---|
| 639 | + ndev->stats.rx_fifo_errors++; |
|---|
| 733 | 640 | } |
|---|
| 734 | 641 | |
|---|
| 735 | | - stats->rx_packets++; |
|---|
| 736 | | - stats->rx_bytes += cf->can_dlc; |
|---|
| 737 | | - netif_rx(skb); |
|---|
| 642 | + hecc_write(priv, HECC_CANES, HECC_CANES_FLAGS); |
|---|
| 738 | 643 | |
|---|
| 739 | 644 | return 0; |
|---|
| 645 | +} |
|---|
| 646 | + |
|---|
| 647 | +static void ti_hecc_change_state(struct net_device *ndev, |
|---|
| 648 | + enum can_state rx_state, |
|---|
| 649 | + enum can_state tx_state) |
|---|
| 650 | +{ |
|---|
| 651 | + struct ti_hecc_priv *priv = netdev_priv(ndev); |
|---|
| 652 | + struct can_frame *cf; |
|---|
| 653 | + struct sk_buff *skb; |
|---|
| 654 | + u32 timestamp; |
|---|
| 655 | + int err; |
|---|
| 656 | + |
|---|
| 657 | + skb = alloc_can_err_skb(priv->ndev, &cf); |
|---|
| 658 | + if (unlikely(!skb)) { |
|---|
| 659 | + priv->can.state = max(tx_state, rx_state); |
|---|
| 660 | + return; |
|---|
| 661 | + } |
|---|
| 662 | + |
|---|
| 663 | + can_change_state(priv->ndev, cf, tx_state, rx_state); |
|---|
| 664 | + |
|---|
| 665 | + if (max(tx_state, rx_state) != CAN_STATE_BUS_OFF) { |
|---|
| 666 | + cf->data[6] = hecc_read(priv, HECC_CANTEC); |
|---|
| 667 | + cf->data[7] = hecc_read(priv, HECC_CANREC); |
|---|
| 668 | + } |
|---|
| 669 | + |
|---|
| 670 | + timestamp = hecc_read(priv, HECC_CANLNT); |
|---|
| 671 | + err = can_rx_offload_queue_sorted(&priv->offload, skb, timestamp); |
|---|
| 672 | + if (err) |
|---|
| 673 | + ndev->stats.rx_fifo_errors++; |
|---|
| 740 | 674 | } |
|---|
| 741 | 675 | |
|---|
| 742 | 676 | static irqreturn_t ti_hecc_interrupt(int irq, void *dev_id) |
|---|
| .. | .. |
|---|
| 744 | 678 | struct net_device *ndev = (struct net_device *)dev_id; |
|---|
| 745 | 679 | struct ti_hecc_priv *priv = netdev_priv(ndev); |
|---|
| 746 | 680 | struct net_device_stats *stats = &ndev->stats; |
|---|
| 747 | | - u32 mbxno, mbx_mask, int_status, err_status; |
|---|
| 748 | | - unsigned long ack, flags; |
|---|
| 681 | + u32 mbxno, mbx_mask, int_status, err_status, stamp; |
|---|
| 682 | + unsigned long flags, rx_pending; |
|---|
| 683 | + u32 handled = 0; |
|---|
| 749 | 684 | |
|---|
| 750 | 685 | int_status = hecc_read(priv, |
|---|
| 751 | | - (priv->use_hecc1int) ? HECC_CANGIF1 : HECC_CANGIF0); |
|---|
| 686 | + priv->use_hecc1int ? |
|---|
| 687 | + HECC_CANGIF1 : HECC_CANGIF0); |
|---|
| 752 | 688 | |
|---|
| 753 | 689 | if (!int_status) |
|---|
| 754 | 690 | return IRQ_NONE; |
|---|
| 755 | 691 | |
|---|
| 756 | 692 | err_status = hecc_read(priv, HECC_CANES); |
|---|
| 757 | | - if (err_status & (HECC_BUS_ERROR | HECC_CANES_BO | |
|---|
| 758 | | - HECC_CANES_EP | HECC_CANES_EW)) |
|---|
| 759 | | - ti_hecc_error(ndev, int_status, err_status); |
|---|
| 693 | + if (unlikely(err_status & HECC_CANES_FLAGS)) |
|---|
| 694 | + ti_hecc_error(ndev, int_status, err_status); |
|---|
| 695 | + |
|---|
| 696 | + if (unlikely(int_status & HECC_CANGIM_DEF_MASK)) { |
|---|
| 697 | + enum can_state rx_state, tx_state; |
|---|
| 698 | + u32 rec = hecc_read(priv, HECC_CANREC); |
|---|
| 699 | + u32 tec = hecc_read(priv, HECC_CANTEC); |
|---|
| 700 | + |
|---|
| 701 | + if (int_status & HECC_CANGIF_WLIF) { |
|---|
| 702 | + handled |= HECC_CANGIF_WLIF; |
|---|
| 703 | + rx_state = rec >= tec ? CAN_STATE_ERROR_WARNING : 0; |
|---|
| 704 | + tx_state = rec <= tec ? CAN_STATE_ERROR_WARNING : 0; |
|---|
| 705 | + netdev_dbg(priv->ndev, "Error Warning interrupt\n"); |
|---|
| 706 | + ti_hecc_change_state(ndev, rx_state, tx_state); |
|---|
| 707 | + } |
|---|
| 708 | + |
|---|
| 709 | + if (int_status & HECC_CANGIF_EPIF) { |
|---|
| 710 | + handled |= HECC_CANGIF_EPIF; |
|---|
| 711 | + rx_state = rec >= tec ? CAN_STATE_ERROR_PASSIVE : 0; |
|---|
| 712 | + tx_state = rec <= tec ? CAN_STATE_ERROR_PASSIVE : 0; |
|---|
| 713 | + netdev_dbg(priv->ndev, "Error passive interrupt\n"); |
|---|
| 714 | + ti_hecc_change_state(ndev, rx_state, tx_state); |
|---|
| 715 | + } |
|---|
| 716 | + |
|---|
| 717 | + if (int_status & HECC_CANGIF_BOIF) { |
|---|
| 718 | + handled |= HECC_CANGIF_BOIF; |
|---|
| 719 | + rx_state = CAN_STATE_BUS_OFF; |
|---|
| 720 | + tx_state = CAN_STATE_BUS_OFF; |
|---|
| 721 | + netdev_dbg(priv->ndev, "Bus off interrupt\n"); |
|---|
| 722 | + |
|---|
| 723 | + /* Disable all interrupts */ |
|---|
| 724 | + hecc_write(priv, HECC_CANGIM, 0); |
|---|
| 725 | + can_bus_off(ndev); |
|---|
| 726 | + ti_hecc_change_state(ndev, rx_state, tx_state); |
|---|
| 727 | + } |
|---|
| 728 | + } else if (unlikely(priv->can.state != CAN_STATE_ERROR_ACTIVE)) { |
|---|
| 729 | + enum can_state new_state, tx_state, rx_state; |
|---|
| 730 | + u32 rec = hecc_read(priv, HECC_CANREC); |
|---|
| 731 | + u32 tec = hecc_read(priv, HECC_CANTEC); |
|---|
| 732 | + |
|---|
| 733 | + if (rec >= 128 || tec >= 128) |
|---|
| 734 | + new_state = CAN_STATE_ERROR_PASSIVE; |
|---|
| 735 | + else if (rec >= 96 || tec >= 96) |
|---|
| 736 | + new_state = CAN_STATE_ERROR_WARNING; |
|---|
| 737 | + else |
|---|
| 738 | + new_state = CAN_STATE_ERROR_ACTIVE; |
|---|
| 739 | + |
|---|
| 740 | + if (new_state < priv->can.state) { |
|---|
| 741 | + rx_state = rec >= tec ? new_state : 0; |
|---|
| 742 | + tx_state = rec <= tec ? new_state : 0; |
|---|
| 743 | + ti_hecc_change_state(ndev, rx_state, tx_state); |
|---|
| 744 | + } |
|---|
| 745 | + } |
|---|
| 760 | 746 | |
|---|
| 761 | 747 | if (int_status & HECC_CANGIF_GMIF) { |
|---|
| 762 | 748 | while (priv->tx_tail - priv->tx_head > 0) { |
|---|
| .. | .. |
|---|
| 764 | 750 | mbx_mask = BIT(mbxno); |
|---|
| 765 | 751 | if (!(mbx_mask & hecc_read(priv, HECC_CANTA))) |
|---|
| 766 | 752 | break; |
|---|
| 767 | | - hecc_clear_bit(priv, HECC_CANMIM, mbx_mask); |
|---|
| 768 | 753 | hecc_write(priv, HECC_CANTA, mbx_mask); |
|---|
| 769 | 754 | spin_lock_irqsave(&priv->mbx_lock, flags); |
|---|
| 770 | 755 | hecc_clear_bit(priv, HECC_CANME, mbx_mask); |
|---|
| 771 | 756 | spin_unlock_irqrestore(&priv->mbx_lock, flags); |
|---|
| 772 | | - stats->tx_bytes += hecc_read_mbx(priv, mbxno, |
|---|
| 773 | | - HECC_CANMCF) & 0xF; |
|---|
| 757 | + stamp = hecc_read_stamp(priv, mbxno); |
|---|
| 758 | + stats->tx_bytes += |
|---|
| 759 | + can_rx_offload_get_echo_skb(&priv->offload, |
|---|
| 760 | + mbxno, stamp); |
|---|
| 774 | 761 | stats->tx_packets++; |
|---|
| 775 | 762 | can_led_event(ndev, CAN_LED_EVENT_TX); |
|---|
| 776 | | - can_get_echo_skb(ndev, mbxno); |
|---|
| 777 | 763 | --priv->tx_tail; |
|---|
| 778 | 764 | } |
|---|
| 779 | 765 | |
|---|
| 780 | 766 | /* restart queue if wrap-up or if queue stalled on last pkt */ |
|---|
| 781 | | - if (((priv->tx_head == priv->tx_tail) && |
|---|
| 782 | | - ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) || |
|---|
| 783 | | - (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) && |
|---|
| 784 | | - ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK))) |
|---|
| 767 | + if ((priv->tx_head == priv->tx_tail && |
|---|
| 768 | + ((priv->tx_head & HECC_TX_MASK) != HECC_TX_MASK)) || |
|---|
| 769 | + (((priv->tx_tail & HECC_TX_MASK) == HECC_TX_MASK) && |
|---|
| 770 | + ((priv->tx_head & HECC_TX_MASK) == HECC_TX_MASK))) |
|---|
| 785 | 771 | netif_wake_queue(ndev); |
|---|
| 786 | 772 | |
|---|
| 787 | | - /* Disable RX mailbox interrupts and let NAPI reenable them */ |
|---|
| 788 | | - if (hecc_read(priv, HECC_CANRMP)) { |
|---|
| 789 | | - ack = hecc_read(priv, HECC_CANMIM); |
|---|
| 790 | | - ack &= BIT(HECC_MAX_TX_MBOX) - 1; |
|---|
| 791 | | - hecc_write(priv, HECC_CANMIM, ack); |
|---|
| 792 | | - napi_schedule(&priv->napi); |
|---|
| 773 | + /* offload RX mailboxes and let NAPI deliver them */ |
|---|
| 774 | + while ((rx_pending = hecc_read(priv, HECC_CANRMP))) { |
|---|
| 775 | + can_rx_offload_irq_offload_timestamp(&priv->offload, |
|---|
| 776 | + rx_pending); |
|---|
| 793 | 777 | } |
|---|
| 794 | 778 | } |
|---|
| 795 | 779 | |
|---|
| 796 | 780 | /* clear all interrupt conditions - read back to avoid spurious ints */ |
|---|
| 797 | 781 | if (priv->use_hecc1int) { |
|---|
| 798 | | - hecc_write(priv, HECC_CANGIF1, HECC_SET_REG); |
|---|
| 782 | + hecc_write(priv, HECC_CANGIF1, handled); |
|---|
| 799 | 783 | int_status = hecc_read(priv, HECC_CANGIF1); |
|---|
| 800 | 784 | } else { |
|---|
| 801 | | - hecc_write(priv, HECC_CANGIF0, HECC_SET_REG); |
|---|
| 785 | + hecc_write(priv, HECC_CANGIF0, handled); |
|---|
| 802 | 786 | int_status = hecc_read(priv, HECC_CANGIF0); |
|---|
| 803 | 787 | } |
|---|
| 804 | 788 | |
|---|
| .. | .. |
|---|
| 811 | 795 | int err; |
|---|
| 812 | 796 | |
|---|
| 813 | 797 | err = request_irq(ndev->irq, ti_hecc_interrupt, IRQF_SHARED, |
|---|
| 814 | | - ndev->name, ndev); |
|---|
| 798 | + ndev->name, ndev); |
|---|
| 815 | 799 | if (err) { |
|---|
| 816 | 800 | netdev_err(ndev, "error requesting interrupt\n"); |
|---|
| 817 | 801 | return err; |
|---|
| .. | .. |
|---|
| 831 | 815 | can_led_event(ndev, CAN_LED_EVENT_OPEN); |
|---|
| 832 | 816 | |
|---|
| 833 | 817 | ti_hecc_start(ndev); |
|---|
| 834 | | - napi_enable(&priv->napi); |
|---|
| 818 | + can_rx_offload_enable(&priv->offload); |
|---|
| 835 | 819 | netif_start_queue(ndev); |
|---|
| 836 | 820 | |
|---|
| 837 | 821 | return 0; |
|---|
| .. | .. |
|---|
| 842 | 826 | struct ti_hecc_priv *priv = netdev_priv(ndev); |
|---|
| 843 | 827 | |
|---|
| 844 | 828 | netif_stop_queue(ndev); |
|---|
| 845 | | - napi_disable(&priv->napi); |
|---|
| 829 | + can_rx_offload_disable(&priv->offload); |
|---|
| 846 | 830 | ti_hecc_stop(ndev); |
|---|
| 847 | 831 | free_irq(ndev->irq, ndev); |
|---|
| 848 | 832 | close_candev(ndev); |
|---|
| .. | .. |
|---|
| 873 | 857 | struct net_device *ndev = (struct net_device *)0; |
|---|
| 874 | 858 | struct ti_hecc_priv *priv; |
|---|
| 875 | 859 | struct device_node *np = pdev->dev.of_node; |
|---|
| 876 | | - struct resource *res, *irq; |
|---|
| 860 | + struct resource *irq; |
|---|
| 877 | 861 | struct regulator *reg_xceiver; |
|---|
| 878 | 862 | int err = -ENODEV; |
|---|
| 879 | 863 | |
|---|
| .. | .. |
|---|
| 894 | 878 | priv = netdev_priv(ndev); |
|---|
| 895 | 879 | |
|---|
| 896 | 880 | /* handle hecc memory */ |
|---|
| 897 | | - res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hecc"); |
|---|
| 898 | | - if (!res) { |
|---|
| 899 | | - dev_err(&pdev->dev, "can't get IORESOURCE_MEM hecc\n"); |
|---|
| 900 | | - return -EINVAL; |
|---|
| 901 | | - } |
|---|
| 902 | | - |
|---|
| 903 | | - priv->base = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 881 | + priv->base = devm_platform_ioremap_resource_byname(pdev, "hecc"); |
|---|
| 904 | 882 | if (IS_ERR(priv->base)) { |
|---|
| 905 | 883 | dev_err(&pdev->dev, "hecc ioremap failed\n"); |
|---|
| 906 | 884 | err = PTR_ERR(priv->base); |
|---|
| .. | .. |
|---|
| 908 | 886 | } |
|---|
| 909 | 887 | |
|---|
| 910 | 888 | /* handle hecc-ram memory */ |
|---|
| 911 | | - res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "hecc-ram"); |
|---|
| 912 | | - if (!res) { |
|---|
| 913 | | - dev_err(&pdev->dev, "can't get IORESOURCE_MEM hecc-ram\n"); |
|---|
| 914 | | - return -EINVAL; |
|---|
| 915 | | - } |
|---|
| 916 | | - |
|---|
| 917 | | - priv->hecc_ram = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 889 | + priv->hecc_ram = devm_platform_ioremap_resource_byname(pdev, |
|---|
| 890 | + "hecc-ram"); |
|---|
| 918 | 891 | if (IS_ERR(priv->hecc_ram)) { |
|---|
| 919 | 892 | dev_err(&pdev->dev, "hecc-ram ioremap failed\n"); |
|---|
| 920 | 893 | err = PTR_ERR(priv->hecc_ram); |
|---|
| .. | .. |
|---|
| 922 | 895 | } |
|---|
| 923 | 896 | |
|---|
| 924 | 897 | /* handle mbx memory */ |
|---|
| 925 | | - res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mbx"); |
|---|
| 926 | | - if (!res) { |
|---|
| 927 | | - dev_err(&pdev->dev, "can't get IORESOURCE_MEM mbx\n"); |
|---|
| 928 | | - return -EINVAL; |
|---|
| 929 | | - } |
|---|
| 930 | | - |
|---|
| 931 | | - priv->mbx = devm_ioremap_resource(&pdev->dev, res); |
|---|
| 898 | + priv->mbx = devm_platform_ioremap_resource_byname(pdev, "mbx"); |
|---|
| 932 | 899 | if (IS_ERR(priv->mbx)) { |
|---|
| 933 | 900 | dev_err(&pdev->dev, "mbx ioremap failed\n"); |
|---|
| 934 | 901 | err = PTR_ERR(priv->mbx); |
|---|
| .. | .. |
|---|
| 965 | 932 | goto probe_exit_candev; |
|---|
| 966 | 933 | } |
|---|
| 967 | 934 | priv->can.clock.freq = clk_get_rate(priv->clk); |
|---|
| 968 | | - netif_napi_add(ndev, &priv->napi, ti_hecc_rx_poll, |
|---|
| 969 | | - HECC_DEF_NAPI_WEIGHT); |
|---|
| 970 | 935 | |
|---|
| 971 | 936 | err = clk_prepare_enable(priv->clk); |
|---|
| 972 | 937 | if (err) { |
|---|
| 973 | 938 | dev_err(&pdev->dev, "clk_prepare_enable() failed\n"); |
|---|
| 974 | | - goto probe_exit_clk; |
|---|
| 939 | + goto probe_exit_release_clk; |
|---|
| 940 | + } |
|---|
| 941 | + |
|---|
| 942 | + priv->offload.mailbox_read = ti_hecc_mailbox_read; |
|---|
| 943 | + priv->offload.mb_first = HECC_RX_FIRST_MBOX; |
|---|
| 944 | + priv->offload.mb_last = HECC_RX_LAST_MBOX; |
|---|
| 945 | + err = can_rx_offload_add_timestamp(ndev, &priv->offload); |
|---|
| 946 | + if (err) { |
|---|
| 947 | + dev_err(&pdev->dev, "can_rx_offload_add_timestamp() failed\n"); |
|---|
| 948 | + goto probe_exit_disable_clk; |
|---|
| 975 | 949 | } |
|---|
| 976 | 950 | |
|---|
| 977 | 951 | err = register_candev(ndev); |
|---|
| 978 | 952 | if (err) { |
|---|
| 979 | 953 | dev_err(&pdev->dev, "register_candev() failed\n"); |
|---|
| 980 | | - goto probe_exit_clk; |
|---|
| 954 | + goto probe_exit_offload; |
|---|
| 981 | 955 | } |
|---|
| 982 | 956 | |
|---|
| 983 | 957 | devm_can_led_init(ndev); |
|---|
| 984 | 958 | |
|---|
| 985 | 959 | dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%u)\n", |
|---|
| 986 | | - priv->base, (u32) ndev->irq); |
|---|
| 960 | + priv->base, (u32)ndev->irq); |
|---|
| 987 | 961 | |
|---|
| 988 | 962 | return 0; |
|---|
| 989 | 963 | |
|---|
| 990 | | -probe_exit_clk: |
|---|
| 964 | +probe_exit_offload: |
|---|
| 965 | + can_rx_offload_del(&priv->offload); |
|---|
| 966 | +probe_exit_disable_clk: |
|---|
| 967 | + clk_disable_unprepare(priv->clk); |
|---|
| 968 | +probe_exit_release_clk: |
|---|
| 991 | 969 | clk_put(priv->clk); |
|---|
| 992 | 970 | probe_exit_candev: |
|---|
| 993 | 971 | free_candev(ndev); |
|---|
| .. | .. |
|---|
| 1003 | 981 | unregister_candev(ndev); |
|---|
| 1004 | 982 | clk_disable_unprepare(priv->clk); |
|---|
| 1005 | 983 | clk_put(priv->clk); |
|---|
| 984 | + can_rx_offload_del(&priv->offload); |
|---|
| 1006 | 985 | free_candev(ndev); |
|---|
| 1007 | 986 | |
|---|
| 1008 | 987 | return 0; |
|---|