66 #include <linux/device.h>
71 #include <linux/kernel.h>
72 #include <linux/module.h>
73 #include <linux/netdevice.h>
75 #include <linux/slab.h>
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
89 #define INSTRUCTION_RTS(n) (0x80 | ((n) & 0x07))
95 # define CANCTRL_REQOP_MASK 0xe0
96 # define CANCTRL_REQOP_CONF 0x80
97 # define CANCTRL_REQOP_LISTEN_ONLY 0x60
98 # define CANCTRL_REQOP_LOOPBACK 0x40
99 # define CANCTRL_REQOP_SLEEP 0x20
100 # define CANCTRL_REQOP_NORMAL 0x00
101 # define CANCTRL_OSM 0x08
102 # define CANCTRL_ABAT 0x10
106 # define CNF1_SJW_SHIFT 6
108 # define CNF2_BTLMODE 0x80
109 # define CNF2_SAM 0x40
110 # define CNF2_PS1_SHIFT 3
112 # define CNF3_SOF 0x08
113 # define CNF3_WAKFIL 0x04
114 # define CNF3_PHSEG2_MASK 0x07
116 # define CANINTE_MERRE 0x80
117 # define CANINTE_WAKIE 0x40
118 # define CANINTE_ERRIE 0x20
119 # define CANINTE_TX2IE 0x10
120 # define CANINTE_TX1IE 0x08
121 # define CANINTE_TX0IE 0x04
122 # define CANINTE_RX1IE 0x02
123 # define CANINTE_RX0IE 0x01
125 # define CANINTF_MERRF 0x80
126 # define CANINTF_WAKIF 0x40
127 # define CANINTF_ERRIF 0x20
128 # define CANINTF_TX2IF 0x10
129 # define CANINTF_TX1IF 0x08
130 # define CANINTF_TX0IF 0x04
131 # define CANINTF_RX1IF 0x02
132 # define CANINTF_RX0IF 0x01
133 # define CANINTF_RX (CANINTF_RX0IF | CANINTF_RX1IF)
134 # define CANINTF_TX (CANINTF_TX2IF | CANINTF_TX1IF | CANINTF_TX0IF)
135 # define CANINTF_ERR (CANINTF_ERRIF)
137 # define EFLG_EWARN 0x01
138 # define EFLG_RXWAR 0x02
139 # define EFLG_TXWAR 0x04
140 # define EFLG_RXEP 0x08
141 # define EFLG_TXEP 0x10
142 # define EFLG_TXBO 0x20
143 # define EFLG_RX0OVR 0x40
144 # define EFLG_RX1OVR 0x80
145 #define TXBCTRL(n) (((n) * 0x10) + 0x30 + TXBCTRL_OFF)
146 # define TXBCTRL_ABTF 0x40
147 # define TXBCTRL_MLOA 0x20
148 # define TXBCTRL_TXERR 0x10
149 # define TXBCTRL_TXREQ 0x08
150 #define TXBSIDH(n) (((n) * 0x10) + 0x30 + TXBSIDH_OFF)
151 # define SIDH_SHIFT 3
152 #define TXBSIDL(n) (((n) * 0x10) + 0x30 + TXBSIDL_OFF)
153 # define SIDL_SID_MASK 7
154 # define SIDL_SID_SHIFT 5
155 # define SIDL_EXIDE_SHIFT 3
156 # define SIDL_EID_SHIFT 16
157 # define SIDL_EID_MASK 3
158 #define TXBEID8(n) (((n) * 0x10) + 0x30 + TXBEID8_OFF)
159 #define TXBEID0(n) (((n) * 0x10) + 0x30 + TXBEID0_OFF)
160 #define TXBDLC(n) (((n) * 0x10) + 0x30 + TXBDLC_OFF)
161 # define DLC_RTR_SHIFT 6
162 #define TXBCTRL_OFF 0
163 #define TXBSIDH_OFF 1
164 #define TXBSIDL_OFF 2
165 #define TXBEID8_OFF 3
166 #define TXBEID0_OFF 4
169 #define RXBCTRL(n) (((n) * 0x10) + 0x60 + RXBCTRL_OFF)
170 # define RXBCTRL_BUKT 0x04
171 # define RXBCTRL_RXM0 0x20
172 # define RXBCTRL_RXM1 0x40
173 #define RXBSIDH(n) (((n) * 0x10) + 0x60 + RXBSIDH_OFF)
174 # define RXBSIDH_SHIFT 3
175 #define RXBSIDL(n) (((n) * 0x10) + 0x60 + RXBSIDL_OFF)
176 # define RXBSIDL_IDE 0x08
177 # define RXBSIDL_SRR 0x10
178 # define RXBSIDL_EID 3
179 # define RXBSIDL_SHIFT 5
180 #define RXBEID8(n) (((n) * 0x10) + 0x60 + RXBEID8_OFF)
181 #define RXBEID0(n) (((n) * 0x10) + 0x60 + RXBEID0_OFF)
182 #define RXBDLC(n) (((n) * 0x10) + 0x60 + RXBDLC_OFF)
183 # define RXBDLC_LEN_MASK 0x0f
184 # define RXBDLC_RTR 0x40
185 #define RXBCTRL_OFF 0
186 #define RXBSIDH_OFF 1
187 #define RXBSIDL_OFF 2
188 #define RXBEID8_OFF 3
189 #define RXBEID0_OFF 4
192 #define RXFSIDH(n) ((n) * 4)
193 #define RXFSIDL(n) ((n) * 4 + 1)
194 #define RXFEID8(n) ((n) * 4 + 2)
195 #define RXFEID0(n) ((n) * 4 + 3)
196 #define RXMSIDH(n) ((n) * 4 + 0x20)
197 #define RXMSIDL(n) ((n) * 4 + 0x21)
198 #define RXMEID8(n) ((n) * 4 + 0x22)
199 #define RXMEID0(n) ((n) * 4 + 0x23)
201 #define GET_BYTE(val, byte) \
202 (((val) >> ((byte) * 8)) & 0xff)
203 #define SET_BYTE(val, byte) \
204 (((val) & 0xff) << ((byte) * 8))
210 #define CAN_FRAME_MAX_DATA_LEN 8
211 #define SPI_TRANSFER_BUF_LEN (6 + CAN_FRAME_MAX_DATA_LEN)
212 #define CAN_FRAME_MAX_BITS 128
214 #define TX_ECHO_SKB_MAX 1
216 #define DEVICE_NAME "mcp251x"
218 static int mcp251x_enable_dma;
261 #define AFTER_SUSPEND_UP 1
262 #define AFTER_SUSPEND_DOWN 2
263 #define AFTER_SUSPEND_POWER 4
264 #define AFTER_SUSPEND_RESTART 8
268 #define MCP251X_IS(_model) \
269 static inline int mcp251x_is_##_model(struct spi_device *spi) \
271 struct mcp251x_priv *priv = dev_get_drvdata(&spi->dev); \
272 return priv->model == CAN_MCP251X_MCP##_model; \
283 net->
stats.tx_errors++;
285 dev_kfree_skb(priv->
tx_skb);
305 static int mcp251x_spi_trans(
struct spi_device *
spi,
int len)
317 spi_message_init(&
m);
319 if (mcp251x_enable_dma) {
325 spi_message_add_tail(&t, &
m);
329 dev_err(&spi->
dev,
"spi transfer failed: ret = %d\n", ret);
341 mcp251x_spi_trans(spi, 3);
355 mcp251x_spi_trans(spi, 4);
369 mcp251x_spi_trans(spi, 3);
382 mcp251x_spi_trans(spi, 4);
386 int len,
int tx_buf_idx)
390 if (mcp251x_is_2510(spi)) {
394 mcp251x_write_reg(spi,
TXBCTRL(tx_buf_idx) + i,
426 mcp251x_hw_tx_frame(spi, buf, frame->
can_dlc, tx_buf_idx);
430 mcp251x_spi_trans(priv->
spi, 1);
433 static void mcp251x_hw_rx_frame(
struct spi_device *spi,
u8 *buf,
438 if (mcp251x_is_2510(spi)) {
442 buf[i] = mcp251x_read_reg(spi,
RXBCTRL(buf_idx) + i);
445 for (; i < (RXBDAT_OFF + len); i++)
446 buf[i] = mcp251x_read_reg(spi,
RXBCTRL(buf_idx) +
i);
454 static void mcp251x_hw_rx(
struct spi_device *spi,
int buf_idx)
463 dev_err(&spi->
dev,
"cannot allocate RX skb\n");
464 priv->
net->stats.rx_dropped++;
468 mcp251x_hw_rx_frame(spi, buf, buf_idx);
495 priv->
net->stats.rx_packets++;
500 static void mcp251x_hw_sleep(
struct spi_device *spi)
512 dev_warn(&spi->
dev,
"hard_xmit called while tx busy\n");
516 if (can_dropped_invalid_skb(net, skb))
519 netif_stop_queue(net);
536 if (priv->
can.restart_ms == 0)
547 static int mcp251x_set_normal_mode(
struct spi_device *spi)
553 mcp251x_write_reg(spi,
CANINTE,
573 " enter in normal mode\n");
582 static int mcp251x_do_set_bittiming(
struct net_device *net)
598 mcp251x_read_reg(spi,
CNF1),
599 mcp251x_read_reg(spi,
CNF2),
600 mcp251x_read_reg(spi,
CNF3));
608 mcp251x_do_set_bittiming(net);
610 mcp251x_write_reg(spi,
RXBCTRL(0),
612 mcp251x_write_reg(spi,
RXBCTRL(1),
617 static int mcp251x_hw_reset(
struct spi_device *spi)
621 unsigned long timeout;
626 dev_err(&spi->
dev,
"reset failed: ret = %d\n", ret);
633 while ((mcp251x_read_reg(spi,
CANSTAT) & CANCTRL_REQOP_MASK)
638 " enter in conf mode after reset\n");
645 static int mcp251x_hw_probe(
struct spi_device *spi)
649 mcp251x_hw_reset(spi);
657 st1 = mcp251x_read_reg(spi,
CANSTAT) & 0xEE;
658 st2 = mcp251x_read_reg(spi,
CANCTRL) & 0x17;
660 dev_dbg(&spi->
dev,
"CANSTAT 0x%02x CANCTRL 0x%02x\n", st1, st2);
663 return (st1 == 0x80 && st2 == 0x07) ? 1 : 0;
666 static void mcp251x_open_clean(
struct net_device *net)
673 mcp251x_hw_sleep(spi);
679 static int mcp251x_stop(
struct net_device *net)
695 mcp251x_write_reg(spi,
CANINTE, 0x00);
696 mcp251x_write_reg(spi,
CANINTF, 0x00);
698 mcp251x_write_reg(spi,
TXBCTRL(0), 0);
701 mcp251x_hw_sleep(spi);
713 static void mcp251x_error_skb(
struct net_device *net,
int can_id,
int data1)
721 frame->data[1] =
data1;
724 netdev_err(net,
"cannot allocate error skb\n");
745 mcp251x_hw_tx(spi, frame, 0);
754 static void mcp251x_restart_work_handler(
struct work_struct *ws)
764 mcp251x_hw_reset(spi);
765 mcp251x_setup(net, priv, spi);
767 mcp251x_set_normal_mode(spi);
771 mcp251x_set_normal_mode(spi);
772 netif_wake_queue(net);
774 mcp251x_hw_sleep(spi);
782 mcp251x_write_reg(spi,
TXBCTRL(0), 0);
784 netif_wake_queue(net);
801 int can_id = 0, data1 = 0;
803 mcp251x_read_2regs(spi,
CANINTF, &intf, &eflag);
810 mcp251x_hw_rx(spi, 0);
815 if (mcp251x_is_2510(spi))
816 mcp251x_write_bits(spi,
CANINTF, CANINTF_RX0IF, 0x00);
821 mcp251x_hw_rx(spi, 1);
823 if (mcp251x_is_2510(spi))
831 mcp251x_write_bits(spi,
CANINTF, clear_intf, 0x00);
834 mcp251x_write_bits(spi,
EFLG, eflag, 0x00);
861 switch (priv->
can.state) {
865 priv->
can.can_stats.error_warning++;
869 priv->
can.can_stats.error_passive++;
880 net->
stats.rx_over_errors++;
881 net->
stats.rx_errors++;
884 net->
stats.rx_over_errors++;
885 net->
stats.rx_errors++;
890 mcp251x_error_skb(net, can_id, data1);
894 if (priv->
can.restart_ms == 0) {
897 mcp251x_hw_sleep(spi);
906 net->
stats.tx_packets++;
912 netif_wake_queue(net);
920 static int mcp251x_open(
struct net_device *net)
929 dev_err(&spi->
dev,
"unable to set initial baudrate!\n");
956 ret = mcp251x_hw_reset(spi);
958 mcp251x_open_clean(net);
961 ret = mcp251x_setup(net, priv, spi);
963 mcp251x_open_clean(net);
966 ret = mcp251x_set_normal_mode(spi);
968 mcp251x_open_clean(net);
971 netif_wake_queue(net);
979 .ndo_open = mcp251x_open,
980 .ndo_stop = mcp251x_stop,
981 .ndo_start_xmit = mcp251x_hard_start_xmit,
1005 priv = netdev_priv(net);
1006 priv->
can.bittiming_const = &mcp251x_bittiming_const;
1007 priv->
can.do_set_mode = mcp251x_do_set_mode;
1019 if (mcp251x_enable_dma) {
1020 spi->
dev.coherent_dma_mask = ~0;
1037 mcp251x_enable_dma = 0;
1042 if (!mcp251x_enable_dma) {
1070 if (!mcp251x_hw_probe(spi)) {
1074 mcp251x_hw_sleep(spi);
1085 if (!mcp251x_enable_dma)
1088 if (!mcp251x_enable_dma)
1092 if (mcp251x_enable_dma)
1112 if (mcp251x_enable_dma) {
1139 if (netif_running(net)) {
1142 mcp251x_hw_sleep(spi);
1180 #define mcp251x_can_suspend NULL
1181 #define mcp251x_can_resume NULL
1192 static struct spi_driver mcp251x_can_driver = {
1199 .id_table = mcp251x_id_table,
1200 .probe = mcp251x_can_probe,
1206 static int __init mcp251x_can_init(
void)
1211 static void __exit mcp251x_can_exit(
void)
1213 spi_unregister_driver(&mcp251x_can_driver);