3 * CAN bus driver for Microchip 251x CAN Controller with SPI Interface
5 * MCP2510 support and bug fixes by Christian Pellegrin
6 * <chripell@evolware.org>
8 * Copyright 2007 Raymarine UK, Ltd. All Rights Reserved.
9 * Written under contract by:
10 * Chris Elston, Katalix Systems, Ltd.
12 * Based on Microchip MCP251x CAN controller driver written by
13 * David Vrabel, Copyright 2006 Arcom Control Systems Ltd.
15 * Based on CAN bus driver for the CCAN controller written by
16 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix
17 * - Simon Kallweit, intefo AG
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the version 2 of the GNU General Public License
22 * as published by the Free Software Foundation
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 * Your platform definition file should specify something like:
37 * static struct mcp251x_platform_data mcp251x_info = {
38 * .oscillator_frequency = 8000000,
39 * .board_specific_setup = &mcp251x_setup,
40 * .model = CAN_MCP251X_MCP2510,
41 * .power_enable = mcp251x_power_enable,
42 * .transceiver_enable = NULL,
45 * static struct spi_board_info spi_board_info[] = {
47 * .modalias = "mcp251x",
48 * .platform_data = &mcp251x_info,
50 * .max_speed_hz = 2*1000*1000,
55 * Please see mcp251x.h for a description of the fields in
56 * struct mcp251x_platform_data.
60 #include <linux/device.h>
61 #include <linux/kernel.h>
62 #include <linux/module.h>
63 #include <linux/interrupt.h>
64 #include <linux/platform_device.h>
65 #include <linux/netdevice.h>
66 #include <linux/can.h>
67 #include <linux/spi/spi.h>
68 #include <linux/can/dev.h>
69 #include <linux/can/core.h>
70 #include <linux/if_arp.h>
71 #include <linux/dma-mapping.h>
72 #include <linux/delay.h>
73 #include <linux/completion.h>
74 #include <linux/freezer.h>
75 #include <linux/uaccess.h>
77 #include <linux/can/platform/mcp251x.h>
79 /* SPI interface instruction set */
80 #define INSTRUCTION_WRITE 0x02
81 #define INSTRUCTION_READ 0x03
82 #define INSTRUCTION_BIT_MODIFY 0x05
83 #define INSTRUCTION_LOAD_TXB(n) (0x40 + 2 * (n))
84 #define INSTRUCTION_READ_RXB(n) (((n) == 0) ? 0x90 : 0x94)
85 #define INSTRUCTION_RESET 0xC0
87 /* MPC251x registers */
90 # define CANCTRL_REQOP_MASK 0xe0
91 # define CANCTRL_REQOP_CONF 0x80
92 # define CANCTRL_REQOP_LISTEN_ONLY 0x60
93 # define CANCTRL_REQOP_LOOPBACK 0x40
94 # define CANCTRL_REQOP_SLEEP 0x20
95 # define CANCTRL_REQOP_NORMAL 0x00
96 # define CANCTRL_OSM 0x08
97 # define CANCTRL_ABAT 0x10
102 # define CNF2_BTLMODE 0x80
104 # define CNF3_SOF 0x08
105 # define CNF3_WAKFIL 0x04
106 # define CNF3_PHSEG2_MASK 0x07
108 # define CANINTE_MERRE 0x80
109 # define CANINTE_WAKIE 0x40
110 # define CANINTE_ERRIE 0x20
111 # define CANINTE_TX2IE 0x10
112 # define CANINTE_TX1IE 0x08
113 # define CANINTE_TX0IE 0x04
114 # define CANINTE_RX1IE 0x02
115 # define CANINTE_RX0IE 0x01
117 # define CANINTF_MERRF 0x80
118 # define CANINTF_WAKIF 0x40
119 # define CANINTF_ERRIF 0x20
120 # define CANINTF_TX2IF 0x10
121 # define CANINTF_TX1IF 0x08
122 # define CANINTF_TX0IF 0x04
123 # define CANINTF_RX1IF 0x02
124 # define CANINTF_RX0IF 0x01
126 # define EFLG_EWARN 0x01
127 # define EFLG_RXWAR 0x02
128 # define EFLG_TXWAR 0x04
129 # define EFLG_RXEP 0x08
130 # define EFLG_TXEP 0x10
131 # define EFLG_TXBO 0x20
132 # define EFLG_RX0OVR 0x40
133 # define EFLG_RX1OVR 0x80
134 #define TXBCTRL(n) ((n * 0x10) + 0x30)
135 # define TXBCTRL_ABTF 0x40
136 # define TXBCTRL_MLOA 0x20
137 # define TXBCTRL_TXERR 0x10
138 # define TXBCTRL_TXREQ 0x08
139 #define RXBCTRL(n) ((n * 0x10) + 0x60)
140 # define RXBCTRL_BUKT 0x04
141 # define RXBCTRL_RXM0 0x20
142 # define RXBCTRL_RXM1 0x40
144 /* Buffer size required for the largest SPI transfer (i.e., reading a
146 #define CAN_FRAME_MAX_DATA_LEN 8
147 #define SPI_TRANSFER_BUF_LEN (2*(6 + CAN_FRAME_MAX_DATA_LEN))
148 #define CAN_FRAME_MAX_BITS 128
150 #define DEVICE_NAME "mcp251x"
152 static int mcp251x_enable_dma; /* Enable SPI DMA. Default: 0 (Off) */
153 module_param(mcp251x_enable_dma, int, S_IRUGO);
154 MODULE_PARM_DESC(mcp251x_enable_dma, "Enable SPI DMA. Default: 0 (Off)");
156 static struct can_bittiming_const mcp251x_bittiming_const = {
167 struct mcp251x_priv {
169 struct net_device *net;
170 struct spi_device *spi;
172 struct mutex spi_lock; /* SPI buffer lock */
175 dma_addr_t spi_tx_dma;
176 dma_addr_t spi_rx_dma;
178 struct sk_buff *tx_skb;
179 struct workqueue_struct *wq;
180 struct work_struct tx_work;
181 struct work_struct irq_work;
182 struct completion awake;
186 #define AFTER_SUSPEND_UP 1
187 #define AFTER_SUSPEND_DOWN 2
188 #define AFTER_SUSPEND_POWER 4
192 static u8 mcp251x_read_reg(struct spi_device *spi, uint8_t reg)
194 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
195 struct spi_transfer t = {
196 .tx_buf = priv->spi_tx_buf,
197 .rx_buf = priv->spi_rx_buf,
201 struct spi_message m;
205 mutex_lock(&priv->spi_lock);
207 priv->spi_tx_buf[0] = INSTRUCTION_READ;
208 priv->spi_tx_buf[1] = reg;
210 spi_message_init(&m);
212 if (mcp251x_enable_dma) {
213 t.tx_dma = priv->spi_tx_dma;
214 t.rx_dma = priv->spi_rx_dma;
218 spi_message_add_tail(&t, &m);
220 ret = spi_sync(spi, &m);
222 dev_dbg(&spi->dev, "%s: failed: ret = %d\n", __func__, ret);
224 val = priv->spi_rx_buf[2];
226 mutex_unlock(&priv->spi_lock);
228 dev_dbg(&spi->dev, "%s: read %02x = %02x\n", __func__, reg, val);
232 static void mcp251x_write_reg(struct spi_device *spi, u8 reg, uint8_t val)
234 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
235 struct spi_transfer t = {
236 .tx_buf = priv->spi_tx_buf,
237 .rx_buf = priv->spi_rx_buf,
241 struct spi_message m;
244 mutex_lock(&priv->spi_lock);
246 priv->spi_tx_buf[0] = INSTRUCTION_WRITE;
247 priv->spi_tx_buf[1] = reg;
248 priv->spi_tx_buf[2] = val;
250 spi_message_init(&m);
252 if (mcp251x_enable_dma) {
253 t.tx_dma = priv->spi_tx_dma;
254 t.rx_dma = priv->spi_rx_dma;
258 spi_message_add_tail(&t, &m);
260 ret = spi_sync(spi, &m);
262 mutex_unlock(&priv->spi_lock);
265 dev_dbg(&spi->dev, "%s: failed\n", __func__);
268 static void mcp251x_write_bits(struct spi_device *spi, u8 reg,
269 u8 mask, uint8_t val)
271 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
272 struct spi_transfer t = {
273 .tx_buf = priv->spi_tx_buf,
274 .rx_buf = priv->spi_rx_buf,
278 struct spi_message m;
281 mutex_lock(&priv->spi_lock);
283 priv->spi_tx_buf[0] = INSTRUCTION_BIT_MODIFY;
284 priv->spi_tx_buf[1] = reg;
285 priv->spi_tx_buf[2] = mask;
286 priv->spi_tx_buf[3] = val;
288 spi_message_init(&m);
290 if (mcp251x_enable_dma) {
291 t.tx_dma = priv->spi_tx_dma;
292 t.rx_dma = priv->spi_rx_dma;
296 spi_message_add_tail(&t, &m);
298 ret = spi_sync(spi, &m);
300 mutex_unlock(&priv->spi_lock);
303 dev_dbg(&spi->dev, "%s: failed\n", __func__);
306 static int mcp251x_hw_tx(struct spi_device *spi, struct can_frame *frame,
309 struct mcp251x_platform_data *pdata = spi->dev.platform_data;
310 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
311 u32 sid, eid, exide, rtr;
313 dev_dbg(&spi->dev, "%s\n", __func__);
315 exide = (frame->can_id & CAN_EFF_FLAG) ? 1 : 0; /* Extended ID Enable */
317 sid = (frame->can_id & CAN_EFF_MASK) >> 18;
319 sid = frame->can_id & CAN_SFF_MASK; /* Standard ID */
320 eid = frame->can_id & CAN_EFF_MASK; /* Extended ID */
321 rtr = (frame->can_id & CAN_RTR_FLAG) ? 1 : 0; /* Remote transmission */
323 if (pdata->model == CAN_MCP251X_MCP2510) {
326 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + 1, sid >> 3);
327 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + 2,
328 ((sid & 7) << 5) | (exide << 3) |
330 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + 3,
332 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + 4, eid & 0xff);
333 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + 5,
334 (rtr << 6) | frame->can_dlc);
336 for (i = 0; i < frame->can_dlc ; i++) {
337 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + 6 + i,
341 struct spi_transfer t = {
342 .tx_buf = priv->spi_tx_buf,
343 .rx_buf = priv->spi_rx_buf,
345 .len = 6 + CAN_FRAME_MAX_DATA_LEN,
347 struct spi_message m;
349 u8 *tx_buf = priv->spi_tx_buf;
351 mutex_lock(&priv->spi_lock);
353 tx_buf[0] = INSTRUCTION_LOAD_TXB(tx_buf_idx);
354 tx_buf[1] = sid >> 3;
355 tx_buf[2] = ((sid & 7) << 5) | (exide << 3) |
357 tx_buf[3] = (eid >> 8) & 0xff;
358 tx_buf[4] = eid & 0xff;
359 tx_buf[5] = (rtr << 6) | frame->can_dlc;
361 memcpy(tx_buf + 6, frame->data, frame->can_dlc);
363 spi_message_init(&m);
365 if (mcp251x_enable_dma) {
366 t.tx_dma = priv->spi_tx_dma;
367 t.rx_dma = priv->spi_rx_dma;
371 spi_message_add_tail(&t, &m);
373 ret = spi_sync(spi, &m);
375 mutex_unlock(&priv->spi_lock);
378 dev_dbg(&spi->dev, "%s: failed: ret = %d\n", __func__,
383 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx), TXBCTRL_TXREQ);
387 static void mcp251x_hw_rx(struct spi_device *spi, int buf_idx)
389 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
390 struct mcp251x_platform_data *pdata = spi->dev.platform_data;
392 struct can_frame *frame;
394 dev_dbg(&spi->dev, "%s\n", __func__);
396 skb = dev_alloc_skb(sizeof(struct can_frame));
398 dev_dbg(&spi->dev, "%s: out of memory for Rx'd frame\n",
400 priv->net->stats.rx_dropped++;
403 skb->dev = priv->net;
404 frame = (struct can_frame *)skb_put(skb, sizeof(struct can_frame));
406 if (pdata->model == CAN_MCP251X_MCP2510) {
410 rx_buf[1] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + 1);
411 rx_buf[2] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + 2);
412 rx_buf[3] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + 3);
413 rx_buf[4] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + 4);
414 rx_buf[5] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + 5);
416 if ((rx_buf[2] >> 3) & 0x1) {
417 /* Extended ID format */
418 frame->can_id = CAN_EFF_FLAG;
419 frame->can_id |= ((rx_buf[2] & 3) << 16) |
420 (rx_buf[3] << 8) | rx_buf[4] |
421 (((rx_buf[1] << 3) | (rx_buf[2] >> 5)) << 18);
423 /* Standard ID format */
424 frame->can_id = (rx_buf[1] << 3) | (rx_buf[2] >> 5);
427 if ((rx_buf[5] >> 6) & 0x1) {
428 /* Remote transmission request */
429 frame->can_id |= CAN_RTR_FLAG;
433 frame->can_dlc = rx_buf[5] & 0x0f;
434 if (frame->can_dlc > 8) {
435 dev_warn(&spi->dev, "invalid frame recevied\n");
436 priv->net->stats.rx_errors++;
441 for (i = 0; i < frame->can_dlc; i++) {
442 frame->data[i] = mcp251x_read_reg(spi,
447 struct spi_transfer t = {
448 .tx_buf = priv->spi_tx_buf,
449 .rx_buf = priv->spi_rx_buf,
451 .len = 14, /* RX buffer: RXBnCTRL to RXBnD7 */
453 struct spi_message m;
455 u8 *tx_buf = priv->spi_tx_buf;
456 u8 *rx_buf = priv->spi_rx_buf;
458 mutex_lock(&priv->spi_lock);
460 tx_buf[0] = INSTRUCTION_READ_RXB(buf_idx);
462 spi_message_init(&m);
464 if (mcp251x_enable_dma) {
465 t.tx_dma = priv->spi_tx_dma;
466 t.rx_dma = priv->spi_rx_dma;
470 spi_message_add_tail(&t, &m);
472 ret = spi_sync(spi, &m);
475 dev_dbg(&spi->dev, "%s: failed: ret = %d\n",
477 priv->net->stats.rx_errors++;
478 mutex_unlock(&priv->spi_lock);
482 if ((rx_buf[2] >> 3) & 0x1) {
483 /* Extended ID format */
484 frame->can_id = CAN_EFF_FLAG;
485 frame->can_id |= ((rx_buf[2] & 3) << 16) |
486 (rx_buf[3] << 8) | rx_buf[4] |
487 (((rx_buf[1] << 3) | (rx_buf[2] >> 5)) << 18);
489 /* Standard ID format */
490 frame->can_id = (rx_buf[1] << 3) | (rx_buf[2] >> 5);
493 if ((rx_buf[5] >> 6) & 0x1) {
494 /* Remote transmission request */
495 frame->can_id |= CAN_RTR_FLAG;
499 frame->can_dlc = rx_buf[5] & 0x0f;
500 if (frame->can_dlc > 8) {
501 dev_warn(&spi->dev, "invalid frame recevied\n");
502 priv->net->stats.rx_errors++;
504 mutex_unlock(&priv->spi_lock);
508 memcpy(frame->data, rx_buf + 6, CAN_FRAME_MAX_DATA_LEN);
510 mutex_unlock(&priv->spi_lock);
513 priv->net->stats.rx_packets++;
514 priv->net->stats.rx_bytes += frame->can_dlc;
516 skb->protocol = __constant_htons(ETH_P_CAN);
517 skb->pkt_type = PACKET_BROADCAST;
518 skb->ip_summed = CHECKSUM_UNNECESSARY;
522 static void mcp251x_hw_sleep(struct spi_device *spi)
524 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP);
527 static void mcp251x_hw_wakeup(struct spi_device *spi)
529 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
533 /* Can only wake up by generating a wake-up interrupt. */
534 mcp251x_write_bits(spi, CANINTE, CANINTE_WAKIE, CANINTE_WAKIE);
535 mcp251x_write_bits(spi, CANINTF, CANINTF_WAKIF, CANINTF_WAKIF);
537 /* Wait until the device is awake */
538 if (!wait_for_completion_timeout(&priv->awake, HZ))
539 dev_err(&spi->dev, "MCP251x didn't wake-up\n");
542 static int mcp251x_hard_start_xmit(struct sk_buff *skb, struct net_device *net)
544 struct mcp251x_priv *priv = netdev_priv(net);
545 struct spi_device *spi = priv->spi;
547 dev_dbg(&spi->dev, "%s\n", __func__);
550 dev_warn(&spi->dev, "hard_xmit called with not null tx_skb\n");
551 return NETDEV_TX_BUSY;
554 if (skb->len != sizeof(struct can_frame)) {
555 dev_dbg(&spi->dev, "dropping packet - bad length\n");
557 net->stats.tx_dropped++;
561 netif_stop_queue(net);
563 net->trans_start = jiffies;
564 queue_work(priv->wq, &priv->tx_work);
569 static int mcp251x_do_set_mode(struct net_device *net, enum can_mode mode)
571 struct mcp251x_priv *priv = netdev_priv(net);
572 struct spi_device *spi = priv->spi;
574 dev_dbg(&spi->dev, "%s (unimplemented)\n", __func__);
584 static void mcp251x_set_normal_mode(struct spi_device *spi)
586 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
587 unsigned long timeout;
589 /* Enable interrupts */
590 mcp251x_write_reg(spi, CANINTE,
591 CANINTE_ERRIE | CANINTE_TX2IE | CANINTE_TX1IE |
592 CANINTE_TX0IE | CANINTE_RX1IE | CANINTE_RX0IE);
594 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
595 /* Put device into loopback mode */
596 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LOOPBACK);
598 /* Put device into normal mode */
599 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL);
601 /* Wait for the device to enter normal mode */
602 timeout = jiffies + HZ;
603 while (mcp251x_read_reg(spi, CANSTAT) & 0xE0) {
605 if (time_after(jiffies, timeout)) {
606 dev_err(&spi->dev, "MCP251x didn't"
607 " enter in normal mode\n");
614 static int mcp251x_do_set_bittiming(struct net_device *net)
616 struct mcp251x_priv *priv = netdev_priv(net);
617 struct can_bittiming *bt = &priv->can.bittiming;
618 struct spi_device *spi = priv->spi;
621 dev_dbg(&spi->dev, "%s: BRP = %d, PropSeg = %d, PS1 = %d,"
622 " PS2 = %d, SJW = %d\n", __func__, bt->brp,
623 bt->prop_seg, bt->phase_seg1, bt->phase_seg2,
626 /* Store original mode and set mode to config */
627 state = mcp251x_read_reg(spi, CANCTRL);
628 state = mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK;
629 mcp251x_write_bits(spi, CANCTRL, CANCTRL_REQOP_MASK,
632 mcp251x_write_reg(spi, CNF1, ((bt->sjw - 1) << 6) | (bt->brp - 1));
633 mcp251x_write_reg(spi, CNF2, CNF2_BTLMODE |
634 ((bt->phase_seg1 - 1) << 3) |
636 mcp251x_write_bits(spi, CNF3, CNF3_PHSEG2_MASK,
637 (bt->phase_seg2 - 1));
639 /* Restore original state */
640 mcp251x_write_bits(spi, CANCTRL, CANCTRL_REQOP_MASK, state);
645 static void mcp251x_setup(struct net_device *net, struct mcp251x_priv *priv,
646 struct spi_device *spi)
650 /* Set initial baudrate. Make sure that registers are updated
651 always by explicitly calling mcp251x_do_set_bittiming */
652 ret = can_set_bittiming(net);
654 dev_err(&spi->dev, "unable to set initial baudrate!\n");
656 mcp251x_do_set_bittiming(net);
658 /* Enable RX0->RX1 buffer roll over and disable filters */
659 mcp251x_write_bits(spi, RXBCTRL(0),
660 RXBCTRL_BUKT | RXBCTRL_RXM0 | RXBCTRL_RXM1,
661 RXBCTRL_BUKT | RXBCTRL_RXM0 | RXBCTRL_RXM1);
662 mcp251x_write_bits(spi, RXBCTRL(1),
663 RXBCTRL_RXM0 | RXBCTRL_RXM1,
664 RXBCTRL_RXM0 | RXBCTRL_RXM1);
666 dev_dbg(&spi->dev, "%s RXBCTL 0 and 1: %02x %02x\n", __func__,
667 mcp251x_read_reg(spi, RXBCTRL(0)),
668 mcp251x_read_reg(spi, RXBCTRL(1)));
671 static void mcp251x_hw_reset(struct spi_device *spi)
673 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
676 mutex_lock(&priv->spi_lock);
678 priv->spi_tx_buf[0] = INSTRUCTION_RESET;
680 ret = spi_write(spi, priv->spi_tx_buf, 1);
682 mutex_unlock(&priv->spi_lock);
685 dev_dbg(&spi->dev, "%s: failed: ret = %d\n", __func__, ret);
686 /* wait for reset to finish */
690 static int mcp251x_hw_probe(struct spi_device *spi)
694 mcp251x_hw_reset(spi);
696 st1 = mcp251x_read_reg(spi, CANSTAT) & 0xEE;
697 st2 = mcp251x_read_reg(spi, CANCTRL) & 0x17;
699 dev_dbg(&spi->dev, "%s: 0x%02x - 0x%02x\n", __func__,
702 /* check for power up default values */
703 return (st1 == 0x80 && st2 == 0x07) ? 1 : 0;
706 static int mcp251x_open(struct net_device *net)
708 struct mcp251x_priv *priv = netdev_priv(net);
709 struct spi_device *spi = priv->spi;
710 struct mcp251x_platform_data *pdata = spi->dev.platform_data;
712 dev_dbg(&spi->dev, "%s\n", __func__);
714 if (pdata->transceiver_enable)
715 pdata->transceiver_enable(1);
717 priv->force_quit = 0;
719 enable_irq(spi->irq);
720 mcp251x_hw_wakeup(spi);
721 mcp251x_hw_reset(spi);
722 mcp251x_setup(net, priv, spi);
723 mcp251x_set_normal_mode(spi);
724 netif_wake_queue(net);
729 static int mcp251x_stop(struct net_device *net)
731 struct mcp251x_priv *priv = netdev_priv(net);
732 struct spi_device *spi = priv->spi;
733 struct mcp251x_platform_data *pdata = spi->dev.platform_data;
735 dev_dbg(&spi->dev, "%s\n", __func__);
737 /* Disable and clear pending interrupts */
738 mcp251x_write_reg(spi, CANINTE, 0x00);
739 mcp251x_write_reg(spi, CANINTF, 0x00);
741 priv->force_quit = 1;
742 disable_irq(spi->irq);
743 flush_workqueue(priv->wq);
745 mcp251x_write_reg(spi, TXBCTRL(0), 0);
747 net->stats.tx_errors++;
748 dev_kfree_skb(priv->tx_skb);
752 mcp251x_hw_sleep(spi);
754 if (pdata->transceiver_enable)
755 pdata->transceiver_enable(0);
760 static int mcp251x_do_get_state(struct net_device *net, enum can_state *state)
762 struct mcp251x_priv *priv = netdev_priv(net);
763 struct spi_device *spi = priv->spi;
766 eflag = mcp251x_read_reg(spi, EFLG);
768 if (eflag & EFLG_TXBO)
769 *state = CAN_STATE_BUS_OFF;
770 else if (eflag & (EFLG_RXEP | EFLG_TXEP))
771 *state = CAN_STATE_BUS_PASSIVE;
772 else if (eflag & EFLG_EWARN)
773 *state = CAN_STATE_BUS_WARNING;
775 *state = CAN_STATE_ACTIVE;
780 static void mcp251x_tx_work_handler(struct work_struct *ws)
782 struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv,
784 struct spi_device *spi = priv->spi;
785 struct can_frame *frame;
787 dev_dbg(&spi->dev, "%s\n", __func__);
790 frame = (struct can_frame *)priv->tx_skb->data;
791 if (frame->can_dlc > CAN_FRAME_MAX_DATA_LEN)
792 frame->can_dlc = CAN_FRAME_MAX_DATA_LEN;
793 mcp251x_hw_tx(spi, frame, 0);
797 static void mcp251x_irq_work_handler(struct work_struct *ws)
799 struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv,
801 struct spi_device *spi = priv->spi;
802 struct net_device *net = priv->net;
806 if (priv->after_suspend) {
807 /* Wait whilst the device wakes up */
809 mcp251x_hw_reset(spi);
810 mcp251x_setup(net, priv, spi);
811 if (priv->after_suspend & AFTER_SUSPEND_UP) {
812 netif_device_attach(net);
813 /* clear since we lost tx buffer */
815 net->stats.tx_errors++;
816 dev_kfree_skb(priv->tx_skb);
818 netif_wake_queue(net);
820 mcp251x_set_normal_mode(spi);
822 mcp251x_hw_sleep(spi);
823 priv->after_suspend = 0;
827 while (!priv->force_quit && !freezing(current)) {
828 if (priv->restart_tx) {
829 priv->restart_tx = 0;
831 "timeout in txing a packet, restarting\n");
832 mcp251x_write_reg(spi, TXBCTRL(0), 0);
834 net->stats.tx_errors++;
835 dev_kfree_skb(priv->tx_skb);
838 netif_wake_queue(net);
842 /* Wait whilst the device wakes up */
847 intf = mcp251x_read_reg(spi, CANINTF);
850 mcp251x_write_bits(spi, CANINTF, intf, 0x00);
852 dev_dbg(&spi->dev, "interrupt:%s%s%s%s%s%s%s%s\n",
853 (intf & CANINTF_MERRF) ? " MERR" : "",
854 (intf & CANINTF_WAKIF) ? " WAK" : "",
855 (intf & CANINTF_ERRIF) ? " ERR" : "",
856 (intf & CANINTF_TX2IF) ? " TX2" : "",
857 (intf & CANINTF_TX1IF) ? " TX1" : "",
858 (intf & CANINTF_TX0IF) ? " TX0" : "",
859 (intf & CANINTF_RX1IF) ? " RX1" : "",
860 (intf & CANINTF_RX0IF) ? " RX0" : "");
862 if (intf & CANINTF_WAKIF)
863 complete(&priv->awake);
865 if (intf & CANINTF_MERRF) {
866 /* if there are no pending Tx buffers, restart queue */
867 txbnctrl = mcp251x_read_reg(spi, TXBCTRL(0));
868 if (!(txbnctrl & TXBCTRL_TXREQ)) {
870 net->stats.tx_errors++;
871 dev_kfree_skb(priv->tx_skb);
874 netif_wake_queue(net);
878 if (intf & CANINTF_ERRIF) {
880 struct can_frame *frame = NULL;
881 u8 eflag = mcp251x_read_reg(spi, EFLG);
883 dev_dbg(&spi->dev, "EFLG = 0x%02x\n", eflag);
885 /* Create error frame */
886 skb = dev_alloc_skb(sizeof(struct can_frame));
888 frame = (struct can_frame *)
889 skb_put(skb, sizeof(struct can_frame));
890 *(unsigned long long *)&frame->data = 0ULL;
891 frame->can_id = CAN_ERR_FLAG;
892 frame->can_dlc = CAN_ERR_DLC;
895 skb->protocol = __constant_htons(ETH_P_CAN);
896 skb->pkt_type = PACKET_BROADCAST;
897 skb->ip_summed = CHECKSUM_UNNECESSARY;
899 /* Set error frame flags based on bus state */
900 if (eflag & EFLG_TXBO) {
901 frame->can_id |= CAN_ERR_BUSOFF;
902 } else if (eflag & EFLG_TXEP) {
903 frame->can_id |= CAN_ERR_CRTL;
905 CAN_ERR_CRTL_TX_PASSIVE;
906 } else if (eflag & EFLG_RXEP) {
907 frame->can_id |= CAN_ERR_CRTL;
909 CAN_ERR_CRTL_RX_PASSIVE;
910 } else if (eflag & EFLG_TXWAR) {
911 frame->can_id |= CAN_ERR_CRTL;
913 CAN_ERR_CRTL_TX_WARNING;
914 } else if (eflag & EFLG_RXWAR) {
915 frame->can_id |= CAN_ERR_CRTL;
917 CAN_ERR_CRTL_RX_WARNING;
921 if (eflag & (EFLG_RX0OVR | EFLG_RX1OVR)) {
922 if (eflag & EFLG_RX0OVR)
923 net->stats.rx_over_errors++;
924 if (eflag & EFLG_RX1OVR)
925 net->stats.rx_over_errors++;
927 frame->can_id |= CAN_ERR_CRTL;
929 CAN_ERR_CRTL_RX_OVERFLOW;
932 mcp251x_write_reg(spi, EFLG, 0x00);
938 if (intf & (CANINTF_TX2IF | CANINTF_TX1IF | CANINTF_TX0IF)) {
940 net->stats.tx_packets++;
941 net->stats.tx_bytes +=
942 ((struct can_frame *)
943 (priv->tx_skb->data))->can_dlc;
944 dev_kfree_skb(priv->tx_skb);
947 netif_wake_queue(net);
950 if (intf & CANINTF_RX0IF)
951 mcp251x_hw_rx(spi, 0);
953 if (intf & CANINTF_RX1IF)
954 mcp251x_hw_rx(spi, 1);
958 mcp251x_read_reg(spi, CANSTAT);
960 dev_dbg(&spi->dev, "interrupt ended\n");
963 static irqreturn_t mcp251x_can_isr(int irq, void *dev_id)
965 struct net_device *net = (struct net_device *)dev_id;
966 struct mcp251x_priv *priv = netdev_priv(net);
968 dev_dbg(&priv->spi->dev, "%s: irq\n", __func__);
969 /* Schedule bottom half */
970 if (!work_pending(&priv->irq_work))
971 queue_work(priv->wq, &priv->irq_work);
976 static void mcp251x_tx_timeout(struct net_device *net)
978 struct mcp251x_priv *priv = netdev_priv(net);
980 priv->restart_tx = 1;
981 queue_work(priv->wq, &priv->irq_work);
984 static struct net_device *alloc_mcp251x_netdev(int sizeof_priv)
986 struct net_device *net;
987 struct mcp251x_priv *priv;
989 net = alloc_candev(sizeof_priv);
993 priv = netdev_priv(net);
995 net->open = mcp251x_open;
996 net->stop = mcp251x_stop;
997 net->hard_start_xmit = mcp251x_hard_start_xmit;
998 net->tx_timeout = mcp251x_tx_timeout;
999 net->watchdog_timeo = HZ;
1001 priv->can.bittiming_const = &mcp251x_bittiming_const;
1002 priv->can.do_get_state = mcp251x_do_get_state;
1003 priv->can.do_set_mode = mcp251x_do_set_mode;
1010 static int __devinit mcp251x_can_probe(struct spi_device *spi)
1012 struct net_device *net;
1013 struct mcp251x_priv *priv;
1014 struct mcp251x_platform_data *pdata = spi->dev.platform_data;
1018 /* Platform data is required for osc freq */
1022 /* Allocate can/net device */
1023 net = alloc_mcp251x_netdev(sizeof(struct mcp251x_priv));
1029 priv = netdev_priv(net);
1030 dev_set_drvdata(&spi->dev, priv);
1033 mutex_init(&priv->spi_lock);
1035 priv->can.bittiming.clock = pdata->oscillator_frequency / 2;
1037 /* If requested, allocate DMA buffers */
1038 if (mcp251x_enable_dma) {
1039 spi->dev.coherent_dma_mask = DMA_32BIT_MASK;
1041 /* Minimum coherent DMA allocation is PAGE_SIZE, so allocate
1042 that much and share it between Tx and Rx DMA buffers. */
1043 priv->spi_tx_buf = dma_alloc_coherent(&spi->dev,
1044 PAGE_SIZE, &priv->spi_tx_dma, GFP_DMA);
1046 if (priv->spi_tx_buf) {
1047 priv->spi_rx_buf = (u8 *)(priv->spi_tx_buf +
1049 priv->spi_rx_dma = (dma_addr_t)(priv->spi_tx_dma +
1052 /* Fall back to non-DMA */
1053 mcp251x_enable_dma = 0;
1057 /* Allocate non-DMA buffers */
1058 if (!mcp251x_enable_dma) {
1059 priv->spi_tx_buf = kmalloc(SPI_TRANSFER_BUF_LEN, GFP_KERNEL);
1060 if (!priv->spi_tx_buf) {
1064 priv->spi_rx_buf = kmalloc(SPI_TRANSFER_BUF_LEN, GFP_KERNEL);
1065 if (!priv->spi_tx_buf) {
1071 if (pdata->power_enable)
1072 pdata->power_enable(1);
1074 /* Call out to platform specific setup */
1075 if (pdata->board_specific_setup)
1076 pdata->board_specific_setup(spi);
1078 SET_NETDEV_DEV(net, &spi->dev);
1080 priv->wq = create_freezeable_workqueue("mcp251x_wq");
1082 INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler);
1083 INIT_WORK(&priv->irq_work, mcp251x_irq_work_handler);
1085 init_completion(&priv->awake);
1087 /* Configure the SPI bus */
1088 spi->mode = SPI_MODE_0;
1089 spi->bits_per_word = 8;
1093 if (request_irq(spi->irq, mcp251x_can_isr,
1094 IRQF_TRIGGER_FALLING, DEVICE_NAME, net) < 0) {
1095 dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq);
1098 disable_irq(spi->irq);
1100 if (!mcp251x_hw_probe(spi)) {
1101 dev_info(&spi->dev, "Probe failed\n");
1104 mcp251x_hw_sleep(spi);
1106 if (pdata->transceiver_enable)
1107 pdata->transceiver_enable(0);
1109 ret = register_candev(net);
1111 dev_info(&spi->dev, "probed\n");
1115 free_irq(spi->irq, net);
1117 if (!mcp251x_enable_dma)
1118 kfree(priv->spi_rx_buf);
1120 if (!mcp251x_enable_dma)
1121 kfree(priv->spi_tx_buf);
1124 if (mcp251x_enable_dma) {
1125 dma_free_coherent(&spi->dev, PAGE_SIZE,
1126 priv->spi_tx_buf, priv->spi_tx_dma);
1129 dev_err(&spi->dev, "probe failed\n");
1134 static int __devexit mcp251x_can_remove(struct spi_device *spi)
1136 struct mcp251x_platform_data *pdata = spi->dev.platform_data;
1137 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
1138 struct net_device *net = priv->net;
1140 unregister_candev(net);
1143 free_irq(spi->irq, net);
1144 priv->force_quit = 1;
1145 flush_workqueue(priv->wq);
1146 destroy_workqueue(priv->wq);
1148 if (mcp251x_enable_dma) {
1149 dma_free_coherent(&spi->dev, PAGE_SIZE,
1150 priv->spi_tx_buf, priv->spi_tx_dma);
1152 kfree(priv->spi_tx_buf);
1153 kfree(priv->spi_rx_buf);
1156 if (pdata->power_enable)
1157 pdata->power_enable(0);
1163 static int mcp251x_can_suspend(struct spi_device *spi, pm_message_t state)
1165 struct mcp251x_platform_data *pdata = spi->dev.platform_data;
1166 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
1167 struct net_device *net = priv->net;
1169 if (netif_running(net)) {
1170 netif_device_detach(net);
1172 mcp251x_hw_sleep(spi);
1173 if (pdata->transceiver_enable)
1174 pdata->transceiver_enable(0);
1175 priv->after_suspend = AFTER_SUSPEND_UP;
1177 priv->after_suspend = AFTER_SUSPEND_DOWN;
1179 if (pdata->power_enable) {
1180 pdata->power_enable(0);
1181 priv->after_suspend |= AFTER_SUSPEND_POWER;
1187 static int mcp251x_can_resume(struct spi_device *spi)
1189 struct mcp251x_platform_data *pdata = spi->dev.platform_data;
1190 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev);
1192 if (priv->after_suspend & AFTER_SUSPEND_POWER) {
1193 pdata->power_enable(1);
1194 queue_work(priv->wq, &priv->irq_work);
1196 if (priv->after_suspend & AFTER_SUSPEND_UP) {
1197 if (pdata->transceiver_enable)
1198 pdata->transceiver_enable(1);
1199 queue_work(priv->wq, &priv->irq_work);
1201 priv->after_suspend = 0;
1206 #define mcp251x_can_suspend NULL
1207 #define mcp251x_can_resume NULL
1210 static struct spi_driver mcp251x_can_driver = {
1212 .name = DEVICE_NAME,
1213 .bus = &spi_bus_type,
1214 .owner = THIS_MODULE,
1217 .probe = mcp251x_can_probe,
1218 .remove = __devexit_p(mcp251x_can_remove),
1219 .suspend = mcp251x_can_suspend,
1220 .resume = mcp251x_can_resume,
1223 static int __init mcp251x_can_init(void)
1225 return spi_register_driver(&mcp251x_can_driver);
1228 static void __exit mcp251x_can_exit(void)
1230 spi_unregister_driver(&mcp251x_can_driver);
1233 module_init(mcp251x_can_init);
1234 module_exit(mcp251x_can_exit);
1236 MODULE_AUTHOR("Chris Elston <celston@katalix.com>, "
1237 "Christian Pellegrin <chripell@evolware.org>");
1238 MODULE_DESCRIPTION("Microchip 251x CAN driver");
1239 MODULE_LICENSE("GPL v2");