diff options
-rw-r--r-- | drivers/net/can/mcp251x.c | 421 |
1 files changed, 203 insertions, 218 deletions
diff --git a/drivers/net/can/mcp251x.c b/drivers/net/can/mcp251x.c index bbe186b5a0ed..f8cc168ec76c 100644 --- a/drivers/net/can/mcp251x.c +++ b/drivers/net/can/mcp251x.c | |||
@@ -180,6 +180,14 @@ | |||
180 | #define RXBEID0_OFF 4 | 180 | #define RXBEID0_OFF 4 |
181 | #define RXBDLC_OFF 5 | 181 | #define RXBDLC_OFF 5 |
182 | #define RXBDAT_OFF 6 | 182 | #define RXBDAT_OFF 6 |
183 | #define RXFSIDH(n) ((n) * 4) | ||
184 | #define RXFSIDL(n) ((n) * 4 + 1) | ||
185 | #define RXFEID8(n) ((n) * 4 + 2) | ||
186 | #define RXFEID0(n) ((n) * 4 + 3) | ||
187 | #define RXMSIDH(n) ((n) * 4 + 0x20) | ||
188 | #define RXMSIDL(n) ((n) * 4 + 0x21) | ||
189 | #define RXMEID8(n) ((n) * 4 + 0x22) | ||
190 | #define RXMEID0(n) ((n) * 4 + 0x23) | ||
183 | 191 | ||
184 | #define GET_BYTE(val, byte) \ | 192 | #define GET_BYTE(val, byte) \ |
185 | (((val) >> ((byte) * 8)) & 0xff) | 193 | (((val) >> ((byte) * 8)) & 0xff) |
@@ -219,7 +227,8 @@ struct mcp251x_priv { | |||
219 | struct net_device *net; | 227 | struct net_device *net; |
220 | struct spi_device *spi; | 228 | struct spi_device *spi; |
221 | 229 | ||
222 | struct mutex spi_lock; /* SPI buffer lock */ | 230 | struct mutex mcp_lock; /* SPI device lock */ |
231 | |||
223 | u8 *spi_tx_buf; | 232 | u8 *spi_tx_buf; |
224 | u8 *spi_rx_buf; | 233 | u8 *spi_rx_buf; |
225 | dma_addr_t spi_tx_dma; | 234 | dma_addr_t spi_tx_dma; |
@@ -227,11 +236,11 @@ struct mcp251x_priv { | |||
227 | 236 | ||
228 | struct sk_buff *tx_skb; | 237 | struct sk_buff *tx_skb; |
229 | int tx_len; | 238 | int tx_len; |
239 | |||
230 | struct workqueue_struct *wq; | 240 | struct workqueue_struct *wq; |
231 | struct work_struct tx_work; | 241 | struct work_struct tx_work; |
232 | struct work_struct irq_work; | 242 | struct work_struct restart_work; |
233 | struct completion awake; | 243 | |
234 | int wake; | ||
235 | int force_quit; | 244 | int force_quit; |
236 | int after_suspend; | 245 | int after_suspend; |
237 | #define AFTER_SUSPEND_UP 1 | 246 | #define AFTER_SUSPEND_UP 1 |
@@ -245,7 +254,8 @@ static void mcp251x_clean(struct net_device *net) | |||
245 | { | 254 | { |
246 | struct mcp251x_priv *priv = netdev_priv(net); | 255 | struct mcp251x_priv *priv = netdev_priv(net); |
247 | 256 | ||
248 | net->stats.tx_errors++; | 257 | if (priv->tx_skb || priv->tx_len) |
258 | net->stats.tx_errors++; | ||
249 | if (priv->tx_skb) | 259 | if (priv->tx_skb) |
250 | dev_kfree_skb(priv->tx_skb); | 260 | dev_kfree_skb(priv->tx_skb); |
251 | if (priv->tx_len) | 261 | if (priv->tx_len) |
@@ -300,16 +310,12 @@ static u8 mcp251x_read_reg(struct spi_device *spi, uint8_t reg) | |||
300 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); | 310 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); |
301 | u8 val = 0; | 311 | u8 val = 0; |
302 | 312 | ||
303 | mutex_lock(&priv->spi_lock); | ||
304 | |||
305 | priv->spi_tx_buf[0] = INSTRUCTION_READ; | 313 | priv->spi_tx_buf[0] = INSTRUCTION_READ; |
306 | priv->spi_tx_buf[1] = reg; | 314 | priv->spi_tx_buf[1] = reg; |
307 | 315 | ||
308 | mcp251x_spi_trans(spi, 3); | 316 | mcp251x_spi_trans(spi, 3); |
309 | val = priv->spi_rx_buf[2]; | 317 | val = priv->spi_rx_buf[2]; |
310 | 318 | ||
311 | mutex_unlock(&priv->spi_lock); | ||
312 | |||
313 | return val; | 319 | return val; |
314 | } | 320 | } |
315 | 321 | ||
@@ -317,15 +323,11 @@ static void mcp251x_write_reg(struct spi_device *spi, u8 reg, uint8_t val) | |||
317 | { | 323 | { |
318 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); | 324 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); |
319 | 325 | ||
320 | mutex_lock(&priv->spi_lock); | ||
321 | |||
322 | priv->spi_tx_buf[0] = INSTRUCTION_WRITE; | 326 | priv->spi_tx_buf[0] = INSTRUCTION_WRITE; |
323 | priv->spi_tx_buf[1] = reg; | 327 | priv->spi_tx_buf[1] = reg; |
324 | priv->spi_tx_buf[2] = val; | 328 | priv->spi_tx_buf[2] = val; |
325 | 329 | ||
326 | mcp251x_spi_trans(spi, 3); | 330 | mcp251x_spi_trans(spi, 3); |
327 | |||
328 | mutex_unlock(&priv->spi_lock); | ||
329 | } | 331 | } |
330 | 332 | ||
331 | static void mcp251x_write_bits(struct spi_device *spi, u8 reg, | 333 | static void mcp251x_write_bits(struct spi_device *spi, u8 reg, |
@@ -333,16 +335,12 @@ static void mcp251x_write_bits(struct spi_device *spi, u8 reg, | |||
333 | { | 335 | { |
334 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); | 336 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); |
335 | 337 | ||
336 | mutex_lock(&priv->spi_lock); | ||
337 | |||
338 | priv->spi_tx_buf[0] = INSTRUCTION_BIT_MODIFY; | 338 | priv->spi_tx_buf[0] = INSTRUCTION_BIT_MODIFY; |
339 | priv->spi_tx_buf[1] = reg; | 339 | priv->spi_tx_buf[1] = reg; |
340 | priv->spi_tx_buf[2] = mask; | 340 | priv->spi_tx_buf[2] = mask; |
341 | priv->spi_tx_buf[3] = val; | 341 | priv->spi_tx_buf[3] = val; |
342 | 342 | ||
343 | mcp251x_spi_trans(spi, 4); | 343 | mcp251x_spi_trans(spi, 4); |
344 | |||
345 | mutex_unlock(&priv->spi_lock); | ||
346 | } | 344 | } |
347 | 345 | ||
348 | static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf, | 346 | static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf, |
@@ -358,10 +356,8 @@ static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf, | |||
358 | mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + i, | 356 | mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + i, |
359 | buf[i]); | 357 | buf[i]); |
360 | } else { | 358 | } else { |
361 | mutex_lock(&priv->spi_lock); | ||
362 | memcpy(priv->spi_tx_buf, buf, TXBDAT_OFF + len); | 359 | memcpy(priv->spi_tx_buf, buf, TXBDAT_OFF + len); |
363 | mcp251x_spi_trans(spi, TXBDAT_OFF + len); | 360 | mcp251x_spi_trans(spi, TXBDAT_OFF + len); |
364 | mutex_unlock(&priv->spi_lock); | ||
365 | } | 361 | } |
366 | } | 362 | } |
367 | 363 | ||
@@ -408,13 +404,9 @@ static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf, | |||
408 | for (; i < (RXBDAT_OFF + len); i++) | 404 | for (; i < (RXBDAT_OFF + len); i++) |
409 | buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i); | 405 | buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i); |
410 | } else { | 406 | } else { |
411 | mutex_lock(&priv->spi_lock); | ||
412 | |||
413 | priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx); | 407 | priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx); |
414 | mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN); | 408 | mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN); |
415 | memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN); | 409 | memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN); |
416 | |||
417 | mutex_unlock(&priv->spi_lock); | ||
418 | } | 410 | } |
419 | } | 411 | } |
420 | 412 | ||
@@ -467,21 +459,6 @@ static void mcp251x_hw_sleep(struct spi_device *spi) | |||
467 | mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP); | 459 | mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP); |
468 | } | 460 | } |
469 | 461 | ||
470 | static void mcp251x_hw_wakeup(struct spi_device *spi) | ||
471 | { | ||
472 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); | ||
473 | |||
474 | priv->wake = 1; | ||
475 | |||
476 | /* Can only wake up by generating a wake-up interrupt. */ | ||
477 | mcp251x_write_bits(spi, CANINTE, CANINTE_WAKIE, CANINTE_WAKIE); | ||
478 | mcp251x_write_bits(spi, CANINTF, CANINTF_WAKIF, CANINTF_WAKIF); | ||
479 | |||
480 | /* Wait until the device is awake */ | ||
481 | if (!wait_for_completion_timeout(&priv->awake, HZ)) | ||
482 | dev_err(&spi->dev, "MCP251x didn't wake-up\n"); | ||
483 | } | ||
484 | |||
485 | static netdev_tx_t mcp251x_hard_start_xmit(struct sk_buff *skb, | 462 | static netdev_tx_t mcp251x_hard_start_xmit(struct sk_buff *skb, |
486 | struct net_device *net) | 463 | struct net_device *net) |
487 | { | 464 | { |
@@ -490,7 +467,6 @@ static netdev_tx_t mcp251x_hard_start_xmit(struct sk_buff *skb, | |||
490 | 467 | ||
491 | if (priv->tx_skb || priv->tx_len) { | 468 | if (priv->tx_skb || priv->tx_len) { |
492 | dev_warn(&spi->dev, "hard_xmit called while tx busy\n"); | 469 | dev_warn(&spi->dev, "hard_xmit called while tx busy\n"); |
493 | netif_stop_queue(net); | ||
494 | return NETDEV_TX_BUSY; | 470 | return NETDEV_TX_BUSY; |
495 | } | 471 | } |
496 | 472 | ||
@@ -511,12 +487,13 @@ static int mcp251x_do_set_mode(struct net_device *net, enum can_mode mode) | |||
511 | 487 | ||
512 | switch (mode) { | 488 | switch (mode) { |
513 | case CAN_MODE_START: | 489 | case CAN_MODE_START: |
490 | mcp251x_clean(net); | ||
514 | /* We have to delay work since SPI I/O may sleep */ | 491 | /* We have to delay work since SPI I/O may sleep */ |
515 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | 492 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
516 | priv->restart_tx = 1; | 493 | priv->restart_tx = 1; |
517 | if (priv->can.restart_ms == 0) | 494 | if (priv->can.restart_ms == 0) |
518 | priv->after_suspend = AFTER_SUSPEND_RESTART; | 495 | priv->after_suspend = AFTER_SUSPEND_RESTART; |
519 | queue_work(priv->wq, &priv->irq_work); | 496 | queue_work(priv->wq, &priv->restart_work); |
520 | break; | 497 | break; |
521 | default: | 498 | default: |
522 | return -EOPNOTSUPP; | 499 | return -EOPNOTSUPP; |
@@ -525,7 +502,7 @@ static int mcp251x_do_set_mode(struct net_device *net, enum can_mode mode) | |||
525 | return 0; | 502 | return 0; |
526 | } | 503 | } |
527 | 504 | ||
528 | static void mcp251x_set_normal_mode(struct spi_device *spi) | 505 | static int mcp251x_set_normal_mode(struct spi_device *spi) |
529 | { | 506 | { |
530 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); | 507 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); |
531 | unsigned long timeout; | 508 | unsigned long timeout; |
@@ -533,8 +510,7 @@ static void mcp251x_set_normal_mode(struct spi_device *spi) | |||
533 | /* Enable interrupts */ | 510 | /* Enable interrupts */ |
534 | mcp251x_write_reg(spi, CANINTE, | 511 | mcp251x_write_reg(spi, CANINTE, |
535 | CANINTE_ERRIE | CANINTE_TX2IE | CANINTE_TX1IE | | 512 | CANINTE_ERRIE | CANINTE_TX2IE | CANINTE_TX1IE | |
536 | CANINTE_TX0IE | CANINTE_RX1IE | CANINTE_RX0IE | | 513 | CANINTE_TX0IE | CANINTE_RX1IE | CANINTE_RX0IE); |
537 | CANINTF_MERRF); | ||
538 | 514 | ||
539 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) { | 515 | if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) { |
540 | /* Put device into loopback mode */ | 516 | /* Put device into loopback mode */ |
@@ -544,9 +520,7 @@ static void mcp251x_set_normal_mode(struct spi_device *spi) | |||
544 | mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LISTEN_ONLY); | 520 | mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LISTEN_ONLY); |
545 | } else { | 521 | } else { |
546 | /* Put device into normal mode */ | 522 | /* Put device into normal mode */ |
547 | mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL | | 523 | mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL); |
548 | (priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT ? | ||
549 | CANCTRL_OSM : 0)); | ||
550 | 524 | ||
551 | /* Wait for the device to enter normal mode */ | 525 | /* Wait for the device to enter normal mode */ |
552 | timeout = jiffies + HZ; | 526 | timeout = jiffies + HZ; |
@@ -555,11 +529,12 @@ static void mcp251x_set_normal_mode(struct spi_device *spi) | |||
555 | if (time_after(jiffies, timeout)) { | 529 | if (time_after(jiffies, timeout)) { |
556 | dev_err(&spi->dev, "MCP251x didn't" | 530 | dev_err(&spi->dev, "MCP251x didn't" |
557 | " enter in normal mode\n"); | 531 | " enter in normal mode\n"); |
558 | return; | 532 | return -EBUSY; |
559 | } | 533 | } |
560 | } | 534 | } |
561 | } | 535 | } |
562 | priv->can.state = CAN_STATE_ERROR_ACTIVE; | 536 | priv->can.state = CAN_STATE_ERROR_ACTIVE; |
537 | return 0; | ||
563 | } | 538 | } |
564 | 539 | ||
565 | static int mcp251x_do_set_bittiming(struct net_device *net) | 540 | static int mcp251x_do_set_bittiming(struct net_device *net) |
@@ -590,33 +565,39 @@ static int mcp251x_setup(struct net_device *net, struct mcp251x_priv *priv, | |||
590 | { | 565 | { |
591 | mcp251x_do_set_bittiming(net); | 566 | mcp251x_do_set_bittiming(net); |
592 | 567 | ||
593 | /* Enable RX0->RX1 buffer roll over and disable filters */ | 568 | mcp251x_write_reg(spi, RXBCTRL(0), |
594 | mcp251x_write_bits(spi, RXBCTRL(0), | 569 | RXBCTRL_BUKT | RXBCTRL_RXM0 | RXBCTRL_RXM1); |
595 | RXBCTRL_BUKT | RXBCTRL_RXM0 | RXBCTRL_RXM1, | 570 | mcp251x_write_reg(spi, RXBCTRL(1), |
596 | RXBCTRL_BUKT | RXBCTRL_RXM0 | RXBCTRL_RXM1); | 571 | RXBCTRL_RXM0 | RXBCTRL_RXM1); |
597 | mcp251x_write_bits(spi, RXBCTRL(1), | ||
598 | RXBCTRL_RXM0 | RXBCTRL_RXM1, | ||
599 | RXBCTRL_RXM0 | RXBCTRL_RXM1); | ||
600 | return 0; | 572 | return 0; |
601 | } | 573 | } |
602 | 574 | ||
603 | static void mcp251x_hw_reset(struct spi_device *spi) | 575 | static int mcp251x_hw_reset(struct spi_device *spi) |
604 | { | 576 | { |
605 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); | 577 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); |
606 | int ret; | 578 | int ret; |
607 | 579 | unsigned long timeout; | |
608 | mutex_lock(&priv->spi_lock); | ||
609 | 580 | ||
610 | priv->spi_tx_buf[0] = INSTRUCTION_RESET; | 581 | priv->spi_tx_buf[0] = INSTRUCTION_RESET; |
611 | |||
612 | ret = spi_write(spi, priv->spi_tx_buf, 1); | 582 | ret = spi_write(spi, priv->spi_tx_buf, 1); |
613 | 583 | if (ret) { | |
614 | mutex_unlock(&priv->spi_lock); | ||
615 | |||
616 | if (ret) | ||
617 | dev_err(&spi->dev, "reset failed: ret = %d\n", ret); | 584 | dev_err(&spi->dev, "reset failed: ret = %d\n", ret); |
585 | return -EIO; | ||
586 | } | ||
587 | |||
618 | /* Wait for reset to finish */ | 588 | /* Wait for reset to finish */ |
589 | timeout = jiffies + HZ; | ||
619 | mdelay(10); | 590 | mdelay(10); |
591 | while ((mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK) | ||
592 | != CANCTRL_REQOP_CONF) { | ||
593 | schedule(); | ||
594 | if (time_after(jiffies, timeout)) { | ||
595 | dev_err(&spi->dev, "MCP251x didn't" | ||
596 | " enter in conf mode after reset\n"); | ||
597 | return -EBUSY; | ||
598 | } | ||
599 | } | ||
600 | return 0; | ||
620 | } | 601 | } |
621 | 602 | ||
622 | static int mcp251x_hw_probe(struct spi_device *spi) | 603 | static int mcp251x_hw_probe(struct spi_device *spi) |
@@ -640,63 +621,17 @@ static int mcp251x_hw_probe(struct spi_device *spi) | |||
640 | return (st1 == 0x80 && st2 == 0x07) ? 1 : 0; | 621 | return (st1 == 0x80 && st2 == 0x07) ? 1 : 0; |
641 | } | 622 | } |
642 | 623 | ||
643 | static irqreturn_t mcp251x_can_isr(int irq, void *dev_id) | 624 | static void mcp251x_open_clean(struct net_device *net) |
644 | { | ||
645 | struct net_device *net = (struct net_device *)dev_id; | ||
646 | struct mcp251x_priv *priv = netdev_priv(net); | ||
647 | |||
648 | /* Schedule bottom half */ | ||
649 | if (!work_pending(&priv->irq_work)) | ||
650 | queue_work(priv->wq, &priv->irq_work); | ||
651 | |||
652 | return IRQ_HANDLED; | ||
653 | } | ||
654 | |||
655 | static int mcp251x_open(struct net_device *net) | ||
656 | { | 625 | { |
657 | struct mcp251x_priv *priv = netdev_priv(net); | 626 | struct mcp251x_priv *priv = netdev_priv(net); |
658 | struct spi_device *spi = priv->spi; | 627 | struct spi_device *spi = priv->spi; |
659 | struct mcp251x_platform_data *pdata = spi->dev.platform_data; | 628 | struct mcp251x_platform_data *pdata = spi->dev.platform_data; |
660 | int ret; | ||
661 | |||
662 | ret = open_candev(net); | ||
663 | if (ret) { | ||
664 | dev_err(&spi->dev, "unable to set initial baudrate!\n"); | ||
665 | return ret; | ||
666 | } | ||
667 | 629 | ||
630 | free_irq(spi->irq, priv); | ||
631 | mcp251x_hw_sleep(spi); | ||
668 | if (pdata->transceiver_enable) | 632 | if (pdata->transceiver_enable) |
669 | pdata->transceiver_enable(1); | 633 | pdata->transceiver_enable(0); |
670 | 634 | close_candev(net); | |
671 | priv->force_quit = 0; | ||
672 | priv->tx_skb = NULL; | ||
673 | priv->tx_len = 0; | ||
674 | |||
675 | ret = request_irq(spi->irq, mcp251x_can_isr, | ||
676 | IRQF_TRIGGER_FALLING, DEVICE_NAME, net); | ||
677 | if (ret) { | ||
678 | dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq); | ||
679 | if (pdata->transceiver_enable) | ||
680 | pdata->transceiver_enable(0); | ||
681 | close_candev(net); | ||
682 | return ret; | ||
683 | } | ||
684 | |||
685 | mcp251x_hw_wakeup(spi); | ||
686 | mcp251x_hw_reset(spi); | ||
687 | ret = mcp251x_setup(net, priv, spi); | ||
688 | if (ret) { | ||
689 | free_irq(spi->irq, net); | ||
690 | mcp251x_hw_sleep(spi); | ||
691 | if (pdata->transceiver_enable) | ||
692 | pdata->transceiver_enable(0); | ||
693 | close_candev(net); | ||
694 | return ret; | ||
695 | } | ||
696 | mcp251x_set_normal_mode(spi); | ||
697 | netif_wake_queue(net); | ||
698 | |||
699 | return 0; | ||
700 | } | 635 | } |
701 | 636 | ||
702 | static int mcp251x_stop(struct net_device *net) | 637 | static int mcp251x_stop(struct net_device *net) |
@@ -707,17 +642,19 @@ static int mcp251x_stop(struct net_device *net) | |||
707 | 642 | ||
708 | close_candev(net); | 643 | close_candev(net); |
709 | 644 | ||
645 | priv->force_quit = 1; | ||
646 | free_irq(spi->irq, priv); | ||
647 | destroy_workqueue(priv->wq); | ||
648 | priv->wq = NULL; | ||
649 | |||
650 | mutex_lock(&priv->mcp_lock); | ||
651 | |||
710 | /* Disable and clear pending interrupts */ | 652 | /* Disable and clear pending interrupts */ |
711 | mcp251x_write_reg(spi, CANINTE, 0x00); | 653 | mcp251x_write_reg(spi, CANINTE, 0x00); |
712 | mcp251x_write_reg(spi, CANINTF, 0x00); | 654 | mcp251x_write_reg(spi, CANINTF, 0x00); |
713 | 655 | ||
714 | priv->force_quit = 1; | ||
715 | free_irq(spi->irq, net); | ||
716 | flush_workqueue(priv->wq); | ||
717 | |||
718 | mcp251x_write_reg(spi, TXBCTRL(0), 0); | 656 | mcp251x_write_reg(spi, TXBCTRL(0), 0); |
719 | if (priv->tx_skb || priv->tx_len) | 657 | mcp251x_clean(net); |
720 | mcp251x_clean(net); | ||
721 | 658 | ||
722 | mcp251x_hw_sleep(spi); | 659 | mcp251x_hw_sleep(spi); |
723 | 660 | ||
@@ -726,9 +663,27 @@ static int mcp251x_stop(struct net_device *net) | |||
726 | 663 | ||
727 | priv->can.state = CAN_STATE_STOPPED; | 664 | priv->can.state = CAN_STATE_STOPPED; |
728 | 665 | ||
666 | mutex_unlock(&priv->mcp_lock); | ||
667 | |||
729 | return 0; | 668 | return 0; |
730 | } | 669 | } |
731 | 670 | ||
671 | static void mcp251x_error_skb(struct net_device *net, int can_id, int data1) | ||
672 | { | ||
673 | struct sk_buff *skb; | ||
674 | struct can_frame *frame; | ||
675 | |||
676 | skb = alloc_can_err_skb(net, &frame); | ||
677 | if (skb) { | ||
678 | frame->can_id = can_id; | ||
679 | frame->data[1] = data1; | ||
680 | netif_rx(skb); | ||
681 | } else { | ||
682 | dev_err(&net->dev, | ||
683 | "cannot allocate error skb\n"); | ||
684 | } | ||
685 | } | ||
686 | |||
732 | static void mcp251x_tx_work_handler(struct work_struct *ws) | 687 | static void mcp251x_tx_work_handler(struct work_struct *ws) |
733 | { | 688 | { |
734 | struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv, | 689 | struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv, |
@@ -737,33 +692,32 @@ static void mcp251x_tx_work_handler(struct work_struct *ws) | |||
737 | struct net_device *net = priv->net; | 692 | struct net_device *net = priv->net; |
738 | struct can_frame *frame; | 693 | struct can_frame *frame; |
739 | 694 | ||
695 | mutex_lock(&priv->mcp_lock); | ||
740 | if (priv->tx_skb) { | 696 | if (priv->tx_skb) { |
741 | frame = (struct can_frame *)priv->tx_skb->data; | ||
742 | |||
743 | if (priv->can.state == CAN_STATE_BUS_OFF) { | 697 | if (priv->can.state == CAN_STATE_BUS_OFF) { |
744 | mcp251x_clean(net); | 698 | mcp251x_clean(net); |
745 | netif_wake_queue(net); | 699 | } else { |
746 | return; | 700 | frame = (struct can_frame *)priv->tx_skb->data; |
701 | |||
702 | if (frame->can_dlc > CAN_FRAME_MAX_DATA_LEN) | ||
703 | frame->can_dlc = CAN_FRAME_MAX_DATA_LEN; | ||
704 | mcp251x_hw_tx(spi, frame, 0); | ||
705 | priv->tx_len = 1 + frame->can_dlc; | ||
706 | can_put_echo_skb(priv->tx_skb, net, 0); | ||
707 | priv->tx_skb = NULL; | ||
747 | } | 708 | } |
748 | if (frame->can_dlc > CAN_FRAME_MAX_DATA_LEN) | ||
749 | frame->can_dlc = CAN_FRAME_MAX_DATA_LEN; | ||
750 | mcp251x_hw_tx(spi, frame, 0); | ||
751 | priv->tx_len = 1 + frame->can_dlc; | ||
752 | can_put_echo_skb(priv->tx_skb, net, 0); | ||
753 | priv->tx_skb = NULL; | ||
754 | } | 709 | } |
710 | mutex_unlock(&priv->mcp_lock); | ||
755 | } | 711 | } |
756 | 712 | ||
757 | static void mcp251x_irq_work_handler(struct work_struct *ws) | 713 | static void mcp251x_restart_work_handler(struct work_struct *ws) |
758 | { | 714 | { |
759 | struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv, | 715 | struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv, |
760 | irq_work); | 716 | restart_work); |
761 | struct spi_device *spi = priv->spi; | 717 | struct spi_device *spi = priv->spi; |
762 | struct net_device *net = priv->net; | 718 | struct net_device *net = priv->net; |
763 | u8 txbnctrl; | ||
764 | u8 intf; | ||
765 | enum can_state new_state; | ||
766 | 719 | ||
720 | mutex_lock(&priv->mcp_lock); | ||
767 | if (priv->after_suspend) { | 721 | if (priv->after_suspend) { |
768 | mdelay(10); | 722 | mdelay(10); |
769 | mcp251x_hw_reset(spi); | 723 | mcp251x_hw_reset(spi); |
@@ -772,45 +726,54 @@ static void mcp251x_irq_work_handler(struct work_struct *ws) | |||
772 | mcp251x_set_normal_mode(spi); | 726 | mcp251x_set_normal_mode(spi); |
773 | } else if (priv->after_suspend & AFTER_SUSPEND_UP) { | 727 | } else if (priv->after_suspend & AFTER_SUSPEND_UP) { |
774 | netif_device_attach(net); | 728 | netif_device_attach(net); |
775 | /* Clean since we lost tx buffer */ | 729 | mcp251x_clean(net); |
776 | if (priv->tx_skb || priv->tx_len) { | ||
777 | mcp251x_clean(net); | ||
778 | netif_wake_queue(net); | ||
779 | } | ||
780 | mcp251x_set_normal_mode(spi); | 730 | mcp251x_set_normal_mode(spi); |
731 | netif_wake_queue(net); | ||
781 | } else { | 732 | } else { |
782 | mcp251x_hw_sleep(spi); | 733 | mcp251x_hw_sleep(spi); |
783 | } | 734 | } |
784 | priv->after_suspend = 0; | 735 | priv->after_suspend = 0; |
736 | priv->force_quit = 0; | ||
785 | } | 737 | } |
786 | 738 | ||
787 | if (priv->can.restart_ms == 0 && priv->can.state == CAN_STATE_BUS_OFF) | 739 | if (priv->restart_tx) { |
788 | return; | 740 | priv->restart_tx = 0; |
741 | mcp251x_write_reg(spi, TXBCTRL(0), 0); | ||
742 | mcp251x_clean(net); | ||
743 | netif_wake_queue(net); | ||
744 | mcp251x_error_skb(net, CAN_ERR_RESTARTED, 0); | ||
745 | } | ||
746 | mutex_unlock(&priv->mcp_lock); | ||
747 | } | ||
789 | 748 | ||
790 | while (!priv->force_quit && !freezing(current)) { | 749 | static irqreturn_t mcp251x_can_ist(int irq, void *dev_id) |
791 | u8 eflag = mcp251x_read_reg(spi, EFLG); | 750 | { |
792 | int can_id = 0, data1 = 0; | 751 | struct mcp251x_priv *priv = dev_id; |
752 | struct spi_device *spi = priv->spi; | ||
753 | struct net_device *net = priv->net; | ||
793 | 754 | ||
794 | mcp251x_write_reg(spi, EFLG, 0x00); | 755 | mutex_lock(&priv->mcp_lock); |
756 | while (!priv->force_quit) { | ||
757 | enum can_state new_state; | ||
758 | u8 intf = mcp251x_read_reg(spi, CANINTF); | ||
759 | u8 eflag; | ||
760 | int can_id = 0, data1 = 0; | ||
795 | 761 | ||
796 | if (priv->restart_tx) { | 762 | if (intf & CANINTF_RX0IF) { |
797 | priv->restart_tx = 0; | 763 | mcp251x_hw_rx(spi, 0); |
798 | mcp251x_write_reg(spi, TXBCTRL(0), 0); | 764 | /* Free one buffer ASAP */ |
799 | if (priv->tx_skb || priv->tx_len) | 765 | mcp251x_write_bits(spi, CANINTF, intf & CANINTF_RX0IF, |
800 | mcp251x_clean(net); | 766 | 0x00); |
801 | netif_wake_queue(net); | ||
802 | can_id |= CAN_ERR_RESTARTED; | ||
803 | } | 767 | } |
804 | 768 | ||
805 | if (priv->wake) { | 769 | if (intf & CANINTF_RX1IF) |
806 | /* Wait whilst the device wakes up */ | 770 | mcp251x_hw_rx(spi, 1); |
807 | mdelay(10); | ||
808 | priv->wake = 0; | ||
809 | } | ||
810 | 771 | ||
811 | intf = mcp251x_read_reg(spi, CANINTF); | ||
812 | mcp251x_write_bits(spi, CANINTF, intf, 0x00); | 772 | mcp251x_write_bits(spi, CANINTF, intf, 0x00); |
813 | 773 | ||
774 | eflag = mcp251x_read_reg(spi, EFLG); | ||
775 | mcp251x_write_reg(spi, EFLG, 0x00); | ||
776 | |||
814 | /* Update can state */ | 777 | /* Update can state */ |
815 | if (eflag & EFLG_TXBO) { | 778 | if (eflag & EFLG_TXBO) { |
816 | new_state = CAN_STATE_BUS_OFF; | 779 | new_state = CAN_STATE_BUS_OFF; |
@@ -851,59 +814,31 @@ static void mcp251x_irq_work_handler(struct work_struct *ws) | |||
851 | } | 814 | } |
852 | priv->can.state = new_state; | 815 | priv->can.state = new_state; |
853 | 816 | ||
854 | if ((intf & CANINTF_ERRIF) || (can_id & CAN_ERR_RESTARTED)) { | 817 | if (intf & CANINTF_ERRIF) { |
855 | struct sk_buff *skb; | 818 | /* Handle overflow counters */ |
856 | struct can_frame *frame; | 819 | if (eflag & (EFLG_RX0OVR | EFLG_RX1OVR)) { |
857 | 820 | if (eflag & EFLG_RX0OVR) | |
858 | /* Create error frame */ | 821 | net->stats.rx_over_errors++; |
859 | skb = alloc_can_err_skb(net, &frame); | 822 | if (eflag & EFLG_RX1OVR) |
860 | if (skb) { | 823 | net->stats.rx_over_errors++; |
861 | /* Set error frame flags based on bus state */ | 824 | can_id |= CAN_ERR_CRTL; |
862 | frame->can_id = can_id; | 825 | data1 |= CAN_ERR_CRTL_RX_OVERFLOW; |
863 | frame->data[1] = data1; | ||
864 | |||
865 | /* Update net stats for overflows */ | ||
866 | if (eflag & (EFLG_RX0OVR | EFLG_RX1OVR)) { | ||
867 | if (eflag & EFLG_RX0OVR) | ||
868 | net->stats.rx_over_errors++; | ||
869 | if (eflag & EFLG_RX1OVR) | ||
870 | net->stats.rx_over_errors++; | ||
871 | frame->can_id |= CAN_ERR_CRTL; | ||
872 | frame->data[1] |= | ||
873 | CAN_ERR_CRTL_RX_OVERFLOW; | ||
874 | } | ||
875 | |||
876 | netif_rx(skb); | ||
877 | } else { | ||
878 | dev_info(&spi->dev, | ||
879 | "cannot allocate error skb\n"); | ||
880 | } | 826 | } |
827 | mcp251x_error_skb(net, can_id, data1); | ||
881 | } | 828 | } |
882 | 829 | ||
883 | if (priv->can.state == CAN_STATE_BUS_OFF) { | 830 | if (priv->can.state == CAN_STATE_BUS_OFF) { |
884 | if (priv->can.restart_ms == 0) { | 831 | if (priv->can.restart_ms == 0) { |
832 | priv->force_quit = 1; | ||
885 | can_bus_off(net); | 833 | can_bus_off(net); |
886 | mcp251x_hw_sleep(spi); | 834 | mcp251x_hw_sleep(spi); |
887 | return; | 835 | break; |
888 | } | 836 | } |
889 | } | 837 | } |
890 | 838 | ||
891 | if (intf == 0) | 839 | if (intf == 0) |
892 | break; | 840 | break; |
893 | 841 | ||
894 | if (intf & CANINTF_WAKIF) | ||
895 | complete(&priv->awake); | ||
896 | |||
897 | if (intf & CANINTF_MERRF) { | ||
898 | /* If there are pending Tx buffers, restart queue */ | ||
899 | txbnctrl = mcp251x_read_reg(spi, TXBCTRL(0)); | ||
900 | if (!(txbnctrl & TXBCTRL_TXREQ)) { | ||
901 | if (priv->tx_skb || priv->tx_len) | ||
902 | mcp251x_clean(net); | ||
903 | netif_wake_queue(net); | ||
904 | } | ||
905 | } | ||
906 | |||
907 | if (intf & (CANINTF_TX2IF | CANINTF_TX1IF | CANINTF_TX0IF)) { | 842 | if (intf & (CANINTF_TX2IF | CANINTF_TX1IF | CANINTF_TX0IF)) { |
908 | net->stats.tx_packets++; | 843 | net->stats.tx_packets++; |
909 | net->stats.tx_bytes += priv->tx_len - 1; | 844 | net->stats.tx_bytes += priv->tx_len - 1; |
@@ -914,12 +849,66 @@ static void mcp251x_irq_work_handler(struct work_struct *ws) | |||
914 | netif_wake_queue(net); | 849 | netif_wake_queue(net); |
915 | } | 850 | } |
916 | 851 | ||
917 | if (intf & CANINTF_RX0IF) | 852 | } |
918 | mcp251x_hw_rx(spi, 0); | 853 | mutex_unlock(&priv->mcp_lock); |
854 | return IRQ_HANDLED; | ||
855 | } | ||
919 | 856 | ||
920 | if (intf & CANINTF_RX1IF) | 857 | static int mcp251x_open(struct net_device *net) |
921 | mcp251x_hw_rx(spi, 1); | 858 | { |
859 | struct mcp251x_priv *priv = netdev_priv(net); | ||
860 | struct spi_device *spi = priv->spi; | ||
861 | struct mcp251x_platform_data *pdata = spi->dev.platform_data; | ||
862 | int ret; | ||
863 | |||
864 | ret = open_candev(net); | ||
865 | if (ret) { | ||
866 | dev_err(&spi->dev, "unable to set initial baudrate!\n"); | ||
867 | return ret; | ||
868 | } | ||
869 | |||
870 | mutex_lock(&priv->mcp_lock); | ||
871 | if (pdata->transceiver_enable) | ||
872 | pdata->transceiver_enable(1); | ||
873 | |||
874 | priv->force_quit = 0; | ||
875 | priv->tx_skb = NULL; | ||
876 | priv->tx_len = 0; | ||
877 | |||
878 | ret = request_threaded_irq(spi->irq, NULL, mcp251x_can_ist, | ||
879 | IRQF_TRIGGER_FALLING, DEVICE_NAME, priv); | ||
880 | if (ret) { | ||
881 | dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq); | ||
882 | if (pdata->transceiver_enable) | ||
883 | pdata->transceiver_enable(0); | ||
884 | close_candev(net); | ||
885 | goto open_unlock; | ||
886 | } | ||
887 | |||
888 | priv->wq = create_freezeable_workqueue("mcp251x_wq"); | ||
889 | INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler); | ||
890 | INIT_WORK(&priv->restart_work, mcp251x_restart_work_handler); | ||
891 | |||
892 | ret = mcp251x_hw_reset(spi); | ||
893 | if (ret) { | ||
894 | mcp251x_open_clean(net); | ||
895 | goto open_unlock; | ||
896 | } | ||
897 | ret = mcp251x_setup(net, priv, spi); | ||
898 | if (ret) { | ||
899 | mcp251x_open_clean(net); | ||
900 | goto open_unlock; | ||
922 | } | 901 | } |
902 | ret = mcp251x_set_normal_mode(spi); | ||
903 | if (ret) { | ||
904 | mcp251x_open_clean(net); | ||
905 | goto open_unlock; | ||
906 | } | ||
907 | netif_wake_queue(net); | ||
908 | |||
909 | open_unlock: | ||
910 | mutex_unlock(&priv->mcp_lock); | ||
911 | return ret; | ||
923 | } | 912 | } |
924 | 913 | ||
925 | static const struct net_device_ops mcp251x_netdev_ops = { | 914 | static const struct net_device_ops mcp251x_netdev_ops = { |
@@ -955,13 +944,11 @@ static int __devinit mcp251x_can_probe(struct spi_device *spi) | |||
955 | priv->can.clock.freq = pdata->oscillator_frequency / 2; | 944 | priv->can.clock.freq = pdata->oscillator_frequency / 2; |
956 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | | 945 | priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES | |
957 | CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY; | 946 | CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY; |
958 | if (pdata->model == CAN_MCP251X_MCP2515) | ||
959 | priv->can.ctrlmode_supported |= CAN_CTRLMODE_ONE_SHOT; | ||
960 | priv->net = net; | 947 | priv->net = net; |
961 | dev_set_drvdata(&spi->dev, priv); | 948 | dev_set_drvdata(&spi->dev, priv); |
962 | 949 | ||
963 | priv->spi = spi; | 950 | priv->spi = spi; |
964 | mutex_init(&priv->spi_lock); | 951 | mutex_init(&priv->mcp_lock); |
965 | 952 | ||
966 | /* If requested, allocate DMA buffers */ | 953 | /* If requested, allocate DMA buffers */ |
967 | if (mcp251x_enable_dma) { | 954 | if (mcp251x_enable_dma) { |
@@ -1010,18 +997,12 @@ static int __devinit mcp251x_can_probe(struct spi_device *spi) | |||
1010 | 997 | ||
1011 | SET_NETDEV_DEV(net, &spi->dev); | 998 | SET_NETDEV_DEV(net, &spi->dev); |
1012 | 999 | ||
1013 | priv->wq = create_freezeable_workqueue("mcp251x_wq"); | ||
1014 | |||
1015 | INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler); | ||
1016 | INIT_WORK(&priv->irq_work, mcp251x_irq_work_handler); | ||
1017 | |||
1018 | init_completion(&priv->awake); | ||
1019 | |||
1020 | /* Configure the SPI bus */ | 1000 | /* Configure the SPI bus */ |
1021 | spi->mode = SPI_MODE_0; | 1001 | spi->mode = SPI_MODE_0; |
1022 | spi->bits_per_word = 8; | 1002 | spi->bits_per_word = 8; |
1023 | spi_setup(spi); | 1003 | spi_setup(spi); |
1024 | 1004 | ||
1005 | /* Here is OK to not lock the MCP, no one knows about it yet */ | ||
1025 | if (!mcp251x_hw_probe(spi)) { | 1006 | if (!mcp251x_hw_probe(spi)) { |
1026 | dev_info(&spi->dev, "Probe failed\n"); | 1007 | dev_info(&spi->dev, "Probe failed\n"); |
1027 | goto error_probe; | 1008 | goto error_probe; |
@@ -1064,10 +1045,6 @@ static int __devexit mcp251x_can_remove(struct spi_device *spi) | |||
1064 | unregister_candev(net); | 1045 | unregister_candev(net); |
1065 | free_candev(net); | 1046 | free_candev(net); |
1066 | 1047 | ||
1067 | priv->force_quit = 1; | ||
1068 | flush_workqueue(priv->wq); | ||
1069 | destroy_workqueue(priv->wq); | ||
1070 | |||
1071 | if (mcp251x_enable_dma) { | 1048 | if (mcp251x_enable_dma) { |
1072 | dma_free_coherent(&spi->dev, PAGE_SIZE, | 1049 | dma_free_coherent(&spi->dev, PAGE_SIZE, |
1073 | priv->spi_tx_buf, priv->spi_tx_dma); | 1050 | priv->spi_tx_buf, priv->spi_tx_dma); |
@@ -1089,6 +1066,12 @@ static int mcp251x_can_suspend(struct spi_device *spi, pm_message_t state) | |||
1089 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); | 1066 | struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); |
1090 | struct net_device *net = priv->net; | 1067 | struct net_device *net = priv->net; |
1091 | 1068 | ||
1069 | priv->force_quit = 1; | ||
1070 | disable_irq(spi->irq); | ||
1071 | /* | ||
1072 | * Note: at this point neither IST nor workqueues are running. | ||
1073 | * open/stop cannot be called anyway so locking is not needed | ||
1074 | */ | ||
1092 | if (netif_running(net)) { | 1075 | if (netif_running(net)) { |
1093 | netif_device_detach(net); | 1076 | netif_device_detach(net); |
1094 | 1077 | ||
@@ -1115,16 +1098,18 @@ static int mcp251x_can_resume(struct spi_device *spi) | |||
1115 | 1098 | ||
1116 | if (priv->after_suspend & AFTER_SUSPEND_POWER) { | 1099 | if (priv->after_suspend & AFTER_SUSPEND_POWER) { |
1117 | pdata->power_enable(1); | 1100 | pdata->power_enable(1); |
1118 | queue_work(priv->wq, &priv->irq_work); | 1101 | queue_work(priv->wq, &priv->restart_work); |
1119 | } else { | 1102 | } else { |
1120 | if (priv->after_suspend & AFTER_SUSPEND_UP) { | 1103 | if (priv->after_suspend & AFTER_SUSPEND_UP) { |
1121 | if (pdata->transceiver_enable) | 1104 | if (pdata->transceiver_enable) |
1122 | pdata->transceiver_enable(1); | 1105 | pdata->transceiver_enable(1); |
1123 | queue_work(priv->wq, &priv->irq_work); | 1106 | queue_work(priv->wq, &priv->restart_work); |
1124 | } else { | 1107 | } else { |
1125 | priv->after_suspend = 0; | 1108 | priv->after_suspend = 0; |
1126 | } | 1109 | } |
1127 | } | 1110 | } |
1111 | priv->force_quit = 0; | ||
1112 | enable_irq(spi->irq); | ||
1128 | return 0; | 1113 | return 0; |
1129 | } | 1114 | } |
1130 | #else | 1115 | #else |