blob: 88d3877b62770702419ffba079b118b8124459b7 [file] [log] [blame]
Christian Pellegrine0000162009-11-02 23:07:00 +00001/*
2 * CAN bus driver for Microchip 251x CAN Controller with SPI Interface
3 *
4 * MCP2510 support and bug fixes by Christian Pellegrin
5 * <chripell@evolware.org>
6 *
7 * Copyright 2009 Christian Pellegrin EVOL S.r.l.
8 *
9 * Copyright 2007 Raymarine UK, Ltd. All Rights Reserved.
10 * Written under contract by:
11 * Chris Elston, Katalix Systems, Ltd.
12 *
13 * Based on Microchip MCP251x CAN controller driver written by
14 * David Vrabel, Copyright 2006 Arcom Control Systems Ltd.
15 *
16 * Based on CAN bus driver for the CCAN controller written by
17 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix
18 * - Simon Kallweit, intefo AG
19 * Copyright 2007
20 *
21 * This program is free software; you can redistribute it and/or modify
22 * it under the terms of the version 2 of the GNU General Public License
23 * as published by the Free Software Foundation
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
Jeff Kirsher05780d92013-12-06 06:28:45 -080031 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Christian Pellegrine0000162009-11-02 23:07:00 +000032 *
33 *
34 *
35 * Your platform definition file should specify something like:
36 *
37 * static struct mcp251x_platform_data mcp251x_info = {
38 * .oscillator_frequency = 8000000,
Christian Pellegrine0000162009-11-02 23:07:00 +000039 * };
40 *
41 * static struct spi_board_info spi_board_info[] = {
42 * {
Marc Kleine-Buddef1f8c6c2010-10-18 15:00:18 +020043 * .modalias = "mcp2510",
44 * // or "mcp2515" depending on your controller
Christian Pellegrine0000162009-11-02 23:07:00 +000045 * .platform_data = &mcp251x_info,
46 * .irq = IRQ_EINT13,
47 * .max_speed_hz = 2*1000*1000,
48 * .chip_select = 2,
49 * },
50 * };
51 *
52 * Please see mcp251x.h for a description of the fields in
53 * struct mcp251x_platform_data.
54 *
55 */
56
Christian Pellegrine0000162009-11-02 23:07:00 +000057#include <linux/can/core.h>
58#include <linux/can/dev.h>
Fabio Baltierieb072a92012-12-18 18:51:02 +010059#include <linux/can/led.h>
Christian Pellegrine0000162009-11-02 23:07:00 +000060#include <linux/can/platform/mcp251x.h>
61#include <linux/completion.h>
62#include <linux/delay.h>
63#include <linux/device.h>
64#include <linux/dma-mapping.h>
65#include <linux/freezer.h>
66#include <linux/interrupt.h>
67#include <linux/io.h>
68#include <linux/kernel.h>
69#include <linux/module.h>
70#include <linux/netdevice.h>
71#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090072#include <linux/slab.h>
Christian Pellegrine0000162009-11-02 23:07:00 +000073#include <linux/spi/spi.h>
74#include <linux/uaccess.h>
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +040075#include <linux/regulator/consumer.h>
Christian Pellegrine0000162009-11-02 23:07:00 +000076
77/* SPI interface instruction set */
78#define INSTRUCTION_WRITE 0x02
79#define INSTRUCTION_READ 0x03
80#define INSTRUCTION_BIT_MODIFY 0x05
81#define INSTRUCTION_LOAD_TXB(n) (0x40 + 2 * (n))
82#define INSTRUCTION_READ_RXB(n) (((n) == 0) ? 0x90 : 0x94)
83#define INSTRUCTION_RESET 0xC0
BenoƮt Lochercab32f32012-08-27 15:02:45 +020084#define RTS_TXB0 0x01
85#define RTS_TXB1 0x02
86#define RTS_TXB2 0x04
87#define INSTRUCTION_RTS(n) (0x80 | ((n) & 0x07))
88
Christian Pellegrine0000162009-11-02 23:07:00 +000089
90/* MPC251x registers */
91#define CANSTAT 0x0e
92#define CANCTRL 0x0f
93# define CANCTRL_REQOP_MASK 0xe0
94# define CANCTRL_REQOP_CONF 0x80
95# define CANCTRL_REQOP_LISTEN_ONLY 0x60
96# define CANCTRL_REQOP_LOOPBACK 0x40
97# define CANCTRL_REQOP_SLEEP 0x20
98# define CANCTRL_REQOP_NORMAL 0x00
99# define CANCTRL_OSM 0x08
100# define CANCTRL_ABAT 0x10
101#define TEC 0x1c
102#define REC 0x1d
103#define CNF1 0x2a
104# define CNF1_SJW_SHIFT 6
105#define CNF2 0x29
106# define CNF2_BTLMODE 0x80
107# define CNF2_SAM 0x40
108# define CNF2_PS1_SHIFT 3
109#define CNF3 0x28
110# define CNF3_SOF 0x08
111# define CNF3_WAKFIL 0x04
112# define CNF3_PHSEG2_MASK 0x07
113#define CANINTE 0x2b
114# define CANINTE_MERRE 0x80
115# define CANINTE_WAKIE 0x40
116# define CANINTE_ERRIE 0x20
117# define CANINTE_TX2IE 0x10
118# define CANINTE_TX1IE 0x08
119# define CANINTE_TX0IE 0x04
120# define CANINTE_RX1IE 0x02
121# define CANINTE_RX0IE 0x01
122#define CANINTF 0x2c
123# define CANINTF_MERRF 0x80
124# define CANINTF_WAKIF 0x40
125# define CANINTF_ERRIF 0x20
126# define CANINTF_TX2IF 0x10
127# define CANINTF_TX1IF 0x08
128# define CANINTF_TX0IF 0x04
129# define CANINTF_RX1IF 0x02
130# define CANINTF_RX0IF 0x01
Marc Kleine-Budde5601b2d2010-10-20 00:02:25 +0000131# define CANINTF_RX (CANINTF_RX0IF | CANINTF_RX1IF)
132# define CANINTF_TX (CANINTF_TX2IF | CANINTF_TX1IF | CANINTF_TX0IF)
133# define CANINTF_ERR (CANINTF_ERRIF)
Christian Pellegrine0000162009-11-02 23:07:00 +0000134#define EFLG 0x2d
135# define EFLG_EWARN 0x01
136# define EFLG_RXWAR 0x02
137# define EFLG_TXWAR 0x04
138# define EFLG_RXEP 0x08
139# define EFLG_TXEP 0x10
140# define EFLG_TXBO 0x20
141# define EFLG_RX0OVR 0x40
142# define EFLG_RX1OVR 0x80
143#define TXBCTRL(n) (((n) * 0x10) + 0x30 + TXBCTRL_OFF)
144# define TXBCTRL_ABTF 0x40
145# define TXBCTRL_MLOA 0x20
146# define TXBCTRL_TXERR 0x10
147# define TXBCTRL_TXREQ 0x08
148#define TXBSIDH(n) (((n) * 0x10) + 0x30 + TXBSIDH_OFF)
149# define SIDH_SHIFT 3
150#define TXBSIDL(n) (((n) * 0x10) + 0x30 + TXBSIDL_OFF)
151# define SIDL_SID_MASK 7
152# define SIDL_SID_SHIFT 5
153# define SIDL_EXIDE_SHIFT 3
154# define SIDL_EID_SHIFT 16
155# define SIDL_EID_MASK 3
156#define TXBEID8(n) (((n) * 0x10) + 0x30 + TXBEID8_OFF)
157#define TXBEID0(n) (((n) * 0x10) + 0x30 + TXBEID0_OFF)
158#define TXBDLC(n) (((n) * 0x10) + 0x30 + TXBDLC_OFF)
159# define DLC_RTR_SHIFT 6
160#define TXBCTRL_OFF 0
161#define TXBSIDH_OFF 1
162#define TXBSIDL_OFF 2
163#define TXBEID8_OFF 3
164#define TXBEID0_OFF 4
165#define TXBDLC_OFF 5
166#define TXBDAT_OFF 6
167#define RXBCTRL(n) (((n) * 0x10) + 0x60 + RXBCTRL_OFF)
168# define RXBCTRL_BUKT 0x04
169# define RXBCTRL_RXM0 0x20
170# define RXBCTRL_RXM1 0x40
171#define RXBSIDH(n) (((n) * 0x10) + 0x60 + RXBSIDH_OFF)
172# define RXBSIDH_SHIFT 3
173#define RXBSIDL(n) (((n) * 0x10) + 0x60 + RXBSIDL_OFF)
174# define RXBSIDL_IDE 0x08
Marc Kleine-Buddeb9958a92010-10-21 06:37:10 +0000175# define RXBSIDL_SRR 0x10
Christian Pellegrine0000162009-11-02 23:07:00 +0000176# define RXBSIDL_EID 3
177# define RXBSIDL_SHIFT 5
178#define RXBEID8(n) (((n) * 0x10) + 0x60 + RXBEID8_OFF)
179#define RXBEID0(n) (((n) * 0x10) + 0x60 + RXBEID0_OFF)
180#define RXBDLC(n) (((n) * 0x10) + 0x60 + RXBDLC_OFF)
181# define RXBDLC_LEN_MASK 0x0f
182# define RXBDLC_RTR 0x40
183#define RXBCTRL_OFF 0
184#define RXBSIDH_OFF 1
185#define RXBSIDL_OFF 2
186#define RXBEID8_OFF 3
187#define RXBEID0_OFF 4
188#define RXBDLC_OFF 5
189#define RXBDAT_OFF 6
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000190#define RXFSIDH(n) ((n) * 4)
191#define RXFSIDL(n) ((n) * 4 + 1)
192#define RXFEID8(n) ((n) * 4 + 2)
193#define RXFEID0(n) ((n) * 4 + 3)
194#define RXMSIDH(n) ((n) * 4 + 0x20)
195#define RXMSIDL(n) ((n) * 4 + 0x21)
196#define RXMEID8(n) ((n) * 4 + 0x22)
197#define RXMEID0(n) ((n) * 4 + 0x23)
Christian Pellegrine0000162009-11-02 23:07:00 +0000198
199#define GET_BYTE(val, byte) \
200 (((val) >> ((byte) * 8)) & 0xff)
201#define SET_BYTE(val, byte) \
202 (((val) & 0xff) << ((byte) * 8))
203
204/*
205 * Buffer size required for the largest SPI transfer (i.e., reading a
206 * frame)
207 */
208#define CAN_FRAME_MAX_DATA_LEN 8
209#define SPI_TRANSFER_BUF_LEN (6 + CAN_FRAME_MAX_DATA_LEN)
210#define CAN_FRAME_MAX_BITS 128
211
212#define TX_ECHO_SKB_MAX 1
213
214#define DEVICE_NAME "mcp251x"
215
216static int mcp251x_enable_dma; /* Enable SPI DMA. Default: 0 (Off) */
217module_param(mcp251x_enable_dma, int, S_IRUGO);
218MODULE_PARM_DESC(mcp251x_enable_dma, "Enable SPI DMA. Default: 0 (Off)");
219
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200220static const struct can_bittiming_const mcp251x_bittiming_const = {
Christian Pellegrine0000162009-11-02 23:07:00 +0000221 .name = DEVICE_NAME,
222 .tseg1_min = 3,
223 .tseg1_max = 16,
224 .tseg2_min = 2,
225 .tseg2_max = 8,
226 .sjw_max = 4,
227 .brp_min = 1,
228 .brp_max = 64,
229 .brp_inc = 1,
230};
231
Marc Kleine-Buddef1f8c6c2010-10-18 15:00:18 +0200232enum mcp251x_model {
233 CAN_MCP251X_MCP2510 = 0x2510,
234 CAN_MCP251X_MCP2515 = 0x2515,
235};
236
Christian Pellegrine0000162009-11-02 23:07:00 +0000237struct mcp251x_priv {
238 struct can_priv can;
239 struct net_device *net;
240 struct spi_device *spi;
Marc Kleine-Buddef1f8c6c2010-10-18 15:00:18 +0200241 enum mcp251x_model model;
Christian Pellegrine0000162009-11-02 23:07:00 +0000242
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000243 struct mutex mcp_lock; /* SPI device lock */
244
Christian Pellegrine0000162009-11-02 23:07:00 +0000245 u8 *spi_tx_buf;
246 u8 *spi_rx_buf;
247 dma_addr_t spi_tx_dma;
248 dma_addr_t spi_rx_dma;
249
250 struct sk_buff *tx_skb;
251 int tx_len;
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000252
Christian Pellegrine0000162009-11-02 23:07:00 +0000253 struct workqueue_struct *wq;
254 struct work_struct tx_work;
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000255 struct work_struct restart_work;
256
Christian Pellegrine0000162009-11-02 23:07:00 +0000257 int force_quit;
258 int after_suspend;
259#define AFTER_SUSPEND_UP 1
260#define AFTER_SUSPEND_DOWN 2
261#define AFTER_SUSPEND_POWER 4
262#define AFTER_SUSPEND_RESTART 8
263 int restart_tx;
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +0400264 struct regulator *power;
265 struct regulator *transceiver;
Christian Pellegrine0000162009-11-02 23:07:00 +0000266};
267
Marc Kleine-Buddebeab6752010-09-23 21:34:28 +0200268#define MCP251X_IS(_model) \
269static inline int mcp251x_is_##_model(struct spi_device *spi) \
270{ \
Jingoo Hanfce5c292013-04-05 20:35:14 +0000271 struct mcp251x_priv *priv = spi_get_drvdata(spi); \
Marc Kleine-Buddebeab6752010-09-23 21:34:28 +0200272 return priv->model == CAN_MCP251X_MCP##_model; \
273}
274
275MCP251X_IS(2510);
276MCP251X_IS(2515);
277
Christian Pellegrine0000162009-11-02 23:07:00 +0000278static void mcp251x_clean(struct net_device *net)
279{
280 struct mcp251x_priv *priv = netdev_priv(net);
281
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000282 if (priv->tx_skb || priv->tx_len)
283 net->stats.tx_errors++;
Christian Pellegrine0000162009-11-02 23:07:00 +0000284 if (priv->tx_skb)
285 dev_kfree_skb(priv->tx_skb);
286 if (priv->tx_len)
287 can_free_echo_skb(priv->net, 0);
288 priv->tx_skb = NULL;
289 priv->tx_len = 0;
290}
291
292/*
293 * Note about handling of error return of mcp251x_spi_trans: accessing
294 * registers via SPI is not really different conceptually than using
295 * normal I/O assembler instructions, although it's much more
296 * complicated from a practical POV. So it's not advisable to always
297 * check the return value of this function. Imagine that every
298 * read{b,l}, write{b,l} and friends would be bracketed in "if ( < 0)
299 * error();", it would be a great mess (well there are some situation
300 * when exception handling C++ like could be useful after all). So we
301 * just check that transfers are OK at the beginning of our
302 * conversation with the chip and to avoid doing really nasty things
303 * (like injecting bogus packets in the network stack).
304 */
305static int mcp251x_spi_trans(struct spi_device *spi, int len)
306{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000307 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000308 struct spi_transfer t = {
309 .tx_buf = priv->spi_tx_buf,
310 .rx_buf = priv->spi_rx_buf,
311 .len = len,
312 .cs_change = 0,
313 };
314 struct spi_message m;
315 int ret;
316
317 spi_message_init(&m);
318
319 if (mcp251x_enable_dma) {
320 t.tx_dma = priv->spi_tx_dma;
321 t.rx_dma = priv->spi_rx_dma;
322 m.is_dma_mapped = 1;
323 }
324
325 spi_message_add_tail(&t, &m);
326
327 ret = spi_sync(spi, &m);
328 if (ret)
329 dev_err(&spi->dev, "spi transfer failed: ret = %d\n", ret);
330 return ret;
331}
332
333static u8 mcp251x_read_reg(struct spi_device *spi, uint8_t reg)
334{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000335 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000336 u8 val = 0;
337
Christian Pellegrine0000162009-11-02 23:07:00 +0000338 priv->spi_tx_buf[0] = INSTRUCTION_READ;
339 priv->spi_tx_buf[1] = reg;
340
341 mcp251x_spi_trans(spi, 3);
342 val = priv->spi_rx_buf[2];
343
Christian Pellegrine0000162009-11-02 23:07:00 +0000344 return val;
345}
346
Sascha Hauerf3a3ed32010-09-28 09:53:35 +0200347static void mcp251x_read_2regs(struct spi_device *spi, uint8_t reg,
348 uint8_t *v1, uint8_t *v2)
349{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000350 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Sascha Hauerf3a3ed32010-09-28 09:53:35 +0200351
352 priv->spi_tx_buf[0] = INSTRUCTION_READ;
353 priv->spi_tx_buf[1] = reg;
354
355 mcp251x_spi_trans(spi, 4);
356
357 *v1 = priv->spi_rx_buf[2];
358 *v2 = priv->spi_rx_buf[3];
359}
360
Christian Pellegrine0000162009-11-02 23:07:00 +0000361static void mcp251x_write_reg(struct spi_device *spi, u8 reg, uint8_t val)
362{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000363 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000364
Christian Pellegrine0000162009-11-02 23:07:00 +0000365 priv->spi_tx_buf[0] = INSTRUCTION_WRITE;
366 priv->spi_tx_buf[1] = reg;
367 priv->spi_tx_buf[2] = val;
368
369 mcp251x_spi_trans(spi, 3);
Christian Pellegrine0000162009-11-02 23:07:00 +0000370}
371
372static void mcp251x_write_bits(struct spi_device *spi, u8 reg,
373 u8 mask, uint8_t val)
374{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000375 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000376
Christian Pellegrine0000162009-11-02 23:07:00 +0000377 priv->spi_tx_buf[0] = INSTRUCTION_BIT_MODIFY;
378 priv->spi_tx_buf[1] = reg;
379 priv->spi_tx_buf[2] = mask;
380 priv->spi_tx_buf[3] = val;
381
382 mcp251x_spi_trans(spi, 4);
Christian Pellegrine0000162009-11-02 23:07:00 +0000383}
384
385static void mcp251x_hw_tx_frame(struct spi_device *spi, u8 *buf,
386 int len, int tx_buf_idx)
387{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000388 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000389
Marc Kleine-Buddebeab6752010-09-23 21:34:28 +0200390 if (mcp251x_is_2510(spi)) {
Christian Pellegrine0000162009-11-02 23:07:00 +0000391 int i;
392
393 for (i = 1; i < TXBDAT_OFF + len; i++)
394 mcp251x_write_reg(spi, TXBCTRL(tx_buf_idx) + i,
395 buf[i]);
396 } else {
Christian Pellegrine0000162009-11-02 23:07:00 +0000397 memcpy(priv->spi_tx_buf, buf, TXBDAT_OFF + len);
398 mcp251x_spi_trans(spi, TXBDAT_OFF + len);
Christian Pellegrine0000162009-11-02 23:07:00 +0000399 }
400}
401
402static void mcp251x_hw_tx(struct spi_device *spi, struct can_frame *frame,
403 int tx_buf_idx)
404{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000405 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000406 u32 sid, eid, exide, rtr;
407 u8 buf[SPI_TRANSFER_BUF_LEN];
408
409 exide = (frame->can_id & CAN_EFF_FLAG) ? 1 : 0; /* Extended ID Enable */
410 if (exide)
411 sid = (frame->can_id & CAN_EFF_MASK) >> 18;
412 else
413 sid = frame->can_id & CAN_SFF_MASK; /* Standard ID */
414 eid = frame->can_id & CAN_EFF_MASK; /* Extended ID */
415 rtr = (frame->can_id & CAN_RTR_FLAG) ? 1 : 0; /* Remote transmission */
416
417 buf[TXBCTRL_OFF] = INSTRUCTION_LOAD_TXB(tx_buf_idx);
418 buf[TXBSIDH_OFF] = sid >> SIDH_SHIFT;
419 buf[TXBSIDL_OFF] = ((sid & SIDL_SID_MASK) << SIDL_SID_SHIFT) |
420 (exide << SIDL_EXIDE_SHIFT) |
421 ((eid >> SIDL_EID_SHIFT) & SIDL_EID_MASK);
422 buf[TXBEID8_OFF] = GET_BYTE(eid, 1);
423 buf[TXBEID0_OFF] = GET_BYTE(eid, 0);
424 buf[TXBDLC_OFF] = (rtr << DLC_RTR_SHIFT) | frame->can_dlc;
425 memcpy(buf + TXBDAT_OFF, frame->data, frame->can_dlc);
426 mcp251x_hw_tx_frame(spi, buf, frame->can_dlc, tx_buf_idx);
BenoƮt Lochercab32f32012-08-27 15:02:45 +0200427
428 /* use INSTRUCTION_RTS, to avoid "repeated frame problem" */
429 priv->spi_tx_buf[0] = INSTRUCTION_RTS(1 << tx_buf_idx);
430 mcp251x_spi_trans(priv->spi, 1);
Christian Pellegrine0000162009-11-02 23:07:00 +0000431}
432
433static void mcp251x_hw_rx_frame(struct spi_device *spi, u8 *buf,
434 int buf_idx)
435{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000436 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000437
Marc Kleine-Buddebeab6752010-09-23 21:34:28 +0200438 if (mcp251x_is_2510(spi)) {
Christian Pellegrine0000162009-11-02 23:07:00 +0000439 int i, len;
440
441 for (i = 1; i < RXBDAT_OFF; i++)
442 buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i);
Oliver Hartkoppc7cd6062009-12-12 04:13:21 +0000443
444 len = get_can_dlc(buf[RXBDLC_OFF] & RXBDLC_LEN_MASK);
Christian Pellegrine0000162009-11-02 23:07:00 +0000445 for (; i < (RXBDAT_OFF + len); i++)
446 buf[i] = mcp251x_read_reg(spi, RXBCTRL(buf_idx) + i);
447 } else {
Christian Pellegrine0000162009-11-02 23:07:00 +0000448 priv->spi_tx_buf[RXBCTRL_OFF] = INSTRUCTION_READ_RXB(buf_idx);
449 mcp251x_spi_trans(spi, SPI_TRANSFER_BUF_LEN);
450 memcpy(buf, priv->spi_rx_buf, SPI_TRANSFER_BUF_LEN);
Christian Pellegrine0000162009-11-02 23:07:00 +0000451 }
452}
453
454static void mcp251x_hw_rx(struct spi_device *spi, int buf_idx)
455{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000456 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000457 struct sk_buff *skb;
458 struct can_frame *frame;
459 u8 buf[SPI_TRANSFER_BUF_LEN];
460
461 skb = alloc_can_skb(priv->net, &frame);
462 if (!skb) {
463 dev_err(&spi->dev, "cannot allocate RX skb\n");
464 priv->net->stats.rx_dropped++;
465 return;
466 }
467
468 mcp251x_hw_rx_frame(spi, buf, buf_idx);
469 if (buf[RXBSIDL_OFF] & RXBSIDL_IDE) {
470 /* Extended ID format */
471 frame->can_id = CAN_EFF_FLAG;
472 frame->can_id |=
473 /* Extended ID part */
474 SET_BYTE(buf[RXBSIDL_OFF] & RXBSIDL_EID, 2) |
475 SET_BYTE(buf[RXBEID8_OFF], 1) |
476 SET_BYTE(buf[RXBEID0_OFF], 0) |
477 /* Standard ID part */
478 (((buf[RXBSIDH_OFF] << RXBSIDH_SHIFT) |
479 (buf[RXBSIDL_OFF] >> RXBSIDL_SHIFT)) << 18);
480 /* Remote transmission request */
481 if (buf[RXBDLC_OFF] & RXBDLC_RTR)
482 frame->can_id |= CAN_RTR_FLAG;
483 } else {
484 /* Standard ID format */
485 frame->can_id =
486 (buf[RXBSIDH_OFF] << RXBSIDH_SHIFT) |
487 (buf[RXBSIDL_OFF] >> RXBSIDL_SHIFT);
Marc Kleine-Buddeb9958a92010-10-21 06:37:10 +0000488 if (buf[RXBSIDL_OFF] & RXBSIDL_SRR)
489 frame->can_id |= CAN_RTR_FLAG;
Christian Pellegrine0000162009-11-02 23:07:00 +0000490 }
491 /* Data length */
Oliver Hartkoppc7cd6062009-12-12 04:13:21 +0000492 frame->can_dlc = get_can_dlc(buf[RXBDLC_OFF] & RXBDLC_LEN_MASK);
Christian Pellegrine0000162009-11-02 23:07:00 +0000493 memcpy(frame->data, buf + RXBDAT_OFF, frame->can_dlc);
494
495 priv->net->stats.rx_packets++;
496 priv->net->stats.rx_bytes += frame->can_dlc;
Fabio Baltierieb072a92012-12-18 18:51:02 +0100497
498 can_led_event(priv->net, CAN_LED_EVENT_RX);
499
Marc Kleine-Budde57d3c7b2010-10-04 10:50:51 +0200500 netif_rx_ni(skb);
Christian Pellegrine0000162009-11-02 23:07:00 +0000501}
502
503static void mcp251x_hw_sleep(struct spi_device *spi)
504{
505 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_SLEEP);
506}
507
Christian Pellegrine0000162009-11-02 23:07:00 +0000508static netdev_tx_t mcp251x_hard_start_xmit(struct sk_buff *skb,
509 struct net_device *net)
510{
511 struct mcp251x_priv *priv = netdev_priv(net);
512 struct spi_device *spi = priv->spi;
513
514 if (priv->tx_skb || priv->tx_len) {
515 dev_warn(&spi->dev, "hard_xmit called while tx busy\n");
Christian Pellegrine0000162009-11-02 23:07:00 +0000516 return NETDEV_TX_BUSY;
517 }
518
Oliver Hartkopp3ccd4c62010-01-12 02:00:46 -0800519 if (can_dropped_invalid_skb(net, skb))
Christian Pellegrine0000162009-11-02 23:07:00 +0000520 return NETDEV_TX_OK;
Christian Pellegrine0000162009-11-02 23:07:00 +0000521
522 netif_stop_queue(net);
523 priv->tx_skb = skb;
Christian Pellegrine0000162009-11-02 23:07:00 +0000524 queue_work(priv->wq, &priv->tx_work);
525
526 return NETDEV_TX_OK;
527}
528
529static int mcp251x_do_set_mode(struct net_device *net, enum can_mode mode)
530{
531 struct mcp251x_priv *priv = netdev_priv(net);
532
533 switch (mode) {
534 case CAN_MODE_START:
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000535 mcp251x_clean(net);
Christian Pellegrine0000162009-11-02 23:07:00 +0000536 /* We have to delay work since SPI I/O may sleep */
537 priv->can.state = CAN_STATE_ERROR_ACTIVE;
538 priv->restart_tx = 1;
539 if (priv->can.restart_ms == 0)
540 priv->after_suspend = AFTER_SUSPEND_RESTART;
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000541 queue_work(priv->wq, &priv->restart_work);
Christian Pellegrine0000162009-11-02 23:07:00 +0000542 break;
543 default:
544 return -EOPNOTSUPP;
545 }
546
547 return 0;
548}
549
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000550static int mcp251x_set_normal_mode(struct spi_device *spi)
Christian Pellegrine0000162009-11-02 23:07:00 +0000551{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000552 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000553 unsigned long timeout;
554
555 /* Enable interrupts */
556 mcp251x_write_reg(spi, CANINTE,
557 CANINTE_ERRIE | CANINTE_TX2IE | CANINTE_TX1IE |
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000558 CANINTE_TX0IE | CANINTE_RX1IE | CANINTE_RX0IE);
Christian Pellegrine0000162009-11-02 23:07:00 +0000559
560 if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
561 /* Put device into loopback mode */
562 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LOOPBACK);
Christian Pellegrinad72c342010-01-14 07:08:34 +0000563 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
564 /* Put device into listen-only mode */
565 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_LISTEN_ONLY);
Christian Pellegrine0000162009-11-02 23:07:00 +0000566 } else {
567 /* Put device into normal mode */
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000568 mcp251x_write_reg(spi, CANCTRL, CANCTRL_REQOP_NORMAL);
Christian Pellegrine0000162009-11-02 23:07:00 +0000569
570 /* Wait for the device to enter normal mode */
571 timeout = jiffies + HZ;
572 while (mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK) {
573 schedule();
574 if (time_after(jiffies, timeout)) {
575 dev_err(&spi->dev, "MCP251x didn't"
576 " enter in normal mode\n");
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000577 return -EBUSY;
Christian Pellegrine0000162009-11-02 23:07:00 +0000578 }
579 }
580 }
581 priv->can.state = CAN_STATE_ERROR_ACTIVE;
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000582 return 0;
Christian Pellegrine0000162009-11-02 23:07:00 +0000583}
584
585static int mcp251x_do_set_bittiming(struct net_device *net)
586{
587 struct mcp251x_priv *priv = netdev_priv(net);
588 struct can_bittiming *bt = &priv->can.bittiming;
589 struct spi_device *spi = priv->spi;
590
591 mcp251x_write_reg(spi, CNF1, ((bt->sjw - 1) << CNF1_SJW_SHIFT) |
592 (bt->brp - 1));
593 mcp251x_write_reg(spi, CNF2, CNF2_BTLMODE |
594 (priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES ?
595 CNF2_SAM : 0) |
596 ((bt->phase_seg1 - 1) << CNF2_PS1_SHIFT) |
597 (bt->prop_seg - 1));
598 mcp251x_write_bits(spi, CNF3, CNF3_PHSEG2_MASK,
599 (bt->phase_seg2 - 1));
600 dev_info(&spi->dev, "CNF: 0x%02x 0x%02x 0x%02x\n",
601 mcp251x_read_reg(spi, CNF1),
602 mcp251x_read_reg(spi, CNF2),
603 mcp251x_read_reg(spi, CNF3));
604
605 return 0;
606}
607
608static int mcp251x_setup(struct net_device *net, struct mcp251x_priv *priv,
609 struct spi_device *spi)
610{
Christian Pellegrin615534b2009-11-17 06:20:44 +0000611 mcp251x_do_set_bittiming(net);
Christian Pellegrine0000162009-11-02 23:07:00 +0000612
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000613 mcp251x_write_reg(spi, RXBCTRL(0),
614 RXBCTRL_BUKT | RXBCTRL_RXM0 | RXBCTRL_RXM1);
615 mcp251x_write_reg(spi, RXBCTRL(1),
616 RXBCTRL_RXM0 | RXBCTRL_RXM1);
Christian Pellegrine0000162009-11-02 23:07:00 +0000617 return 0;
618}
619
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000620static int mcp251x_hw_reset(struct spi_device *spi)
Christian Pellegrine0000162009-11-02 23:07:00 +0000621{
Jingoo Hanfce5c292013-04-05 20:35:14 +0000622 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +0000623 int ret;
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000624 unsigned long timeout;
Christian Pellegrine0000162009-11-02 23:07:00 +0000625
626 priv->spi_tx_buf[0] = INSTRUCTION_RESET;
Christian Pellegrine0000162009-11-02 23:07:00 +0000627 ret = spi_write(spi, priv->spi_tx_buf, 1);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000628 if (ret) {
Christian Pellegrine0000162009-11-02 23:07:00 +0000629 dev_err(&spi->dev, "reset failed: ret = %d\n", ret);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000630 return -EIO;
631 }
632
Christian Pellegrine0000162009-11-02 23:07:00 +0000633 /* Wait for reset to finish */
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000634 timeout = jiffies + HZ;
Christian Pellegrine0000162009-11-02 23:07:00 +0000635 mdelay(10);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000636 while ((mcp251x_read_reg(spi, CANSTAT) & CANCTRL_REQOP_MASK)
637 != CANCTRL_REQOP_CONF) {
638 schedule();
639 if (time_after(jiffies, timeout)) {
640 dev_err(&spi->dev, "MCP251x didn't"
641 " enter in conf mode after reset\n");
642 return -EBUSY;
643 }
644 }
645 return 0;
Christian Pellegrine0000162009-11-02 23:07:00 +0000646}
647
648static int mcp251x_hw_probe(struct spi_device *spi)
649{
650 int st1, st2;
651
652 mcp251x_hw_reset(spi);
653
654 /*
655 * Please note that these are "magic values" based on after
656 * reset defaults taken from data sheet which allows us to see
657 * if we really have a chip on the bus (we avoid common all
658 * zeroes or all ones situations)
659 */
660 st1 = mcp251x_read_reg(spi, CANSTAT) & 0xEE;
661 st2 = mcp251x_read_reg(spi, CANCTRL) & 0x17;
662
663 dev_dbg(&spi->dev, "CANSTAT 0x%02x CANCTRL 0x%02x\n", st1, st2);
664
665 /* Check for power up default values */
666 return (st1 == 0x80 && st2 == 0x07) ? 1 : 0;
667}
668
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +0400669static int mcp251x_power_enable(struct regulator *reg, int enable)
670{
671 if (IS_ERR(reg))
672 return 0;
673
674 if (enable)
675 return regulator_enable(reg);
676 else
677 return regulator_disable(reg);
678}
679
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000680static void mcp251x_open_clean(struct net_device *net)
Christian Pellegrine0000162009-11-02 23:07:00 +0000681{
682 struct mcp251x_priv *priv = netdev_priv(net);
683 struct spi_device *spi = priv->spi;
Christian Pellegrine0000162009-11-02 23:07:00 +0000684
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000685 free_irq(spi->irq, priv);
686 mcp251x_hw_sleep(spi);
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +0400687 mcp251x_power_enable(priv->transceiver, 0);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000688 close_candev(net);
Christian Pellegrine0000162009-11-02 23:07:00 +0000689}
690
691static int mcp251x_stop(struct net_device *net)
692{
693 struct mcp251x_priv *priv = netdev_priv(net);
694 struct spi_device *spi = priv->spi;
Christian Pellegrine0000162009-11-02 23:07:00 +0000695
696 close_candev(net);
697
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000698 priv->force_quit = 1;
699 free_irq(spi->irq, priv);
700 destroy_workqueue(priv->wq);
701 priv->wq = NULL;
702
703 mutex_lock(&priv->mcp_lock);
704
Christian Pellegrine0000162009-11-02 23:07:00 +0000705 /* Disable and clear pending interrupts */
706 mcp251x_write_reg(spi, CANINTE, 0x00);
707 mcp251x_write_reg(spi, CANINTF, 0x00);
708
Christian Pellegrine0000162009-11-02 23:07:00 +0000709 mcp251x_write_reg(spi, TXBCTRL(0), 0);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000710 mcp251x_clean(net);
Christian Pellegrine0000162009-11-02 23:07:00 +0000711
712 mcp251x_hw_sleep(spi);
713
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +0400714 mcp251x_power_enable(priv->transceiver, 0);
Christian Pellegrine0000162009-11-02 23:07:00 +0000715
716 priv->can.state = CAN_STATE_STOPPED;
717
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000718 mutex_unlock(&priv->mcp_lock);
719
Fabio Baltierieb072a92012-12-18 18:51:02 +0100720 can_led_event(net, CAN_LED_EVENT_STOP);
721
Christian Pellegrine0000162009-11-02 23:07:00 +0000722 return 0;
723}
724
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000725static void mcp251x_error_skb(struct net_device *net, int can_id, int data1)
726{
727 struct sk_buff *skb;
728 struct can_frame *frame;
729
730 skb = alloc_can_err_skb(net, &frame);
731 if (skb) {
Marc Kleine-Budde612eef42010-10-20 00:02:26 +0000732 frame->can_id |= can_id;
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000733 frame->data[1] = data1;
Marc Kleine-Budde57d3c7b2010-10-04 10:50:51 +0200734 netif_rx_ni(skb);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000735 } else {
Wolfgang Grandeggeraabdfd62012-02-01 11:02:05 +0100736 netdev_err(net, "cannot allocate error skb\n");
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000737 }
738}
739
Christian Pellegrine0000162009-11-02 23:07:00 +0000740static void mcp251x_tx_work_handler(struct work_struct *ws)
741{
742 struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv,
743 tx_work);
744 struct spi_device *spi = priv->spi;
745 struct net_device *net = priv->net;
746 struct can_frame *frame;
747
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000748 mutex_lock(&priv->mcp_lock);
Christian Pellegrine0000162009-11-02 23:07:00 +0000749 if (priv->tx_skb) {
Christian Pellegrine0000162009-11-02 23:07:00 +0000750 if (priv->can.state == CAN_STATE_BUS_OFF) {
751 mcp251x_clean(net);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000752 } else {
753 frame = (struct can_frame *)priv->tx_skb->data;
754
755 if (frame->can_dlc > CAN_FRAME_MAX_DATA_LEN)
756 frame->can_dlc = CAN_FRAME_MAX_DATA_LEN;
757 mcp251x_hw_tx(spi, frame, 0);
758 priv->tx_len = 1 + frame->can_dlc;
759 can_put_echo_skb(priv->tx_skb, net, 0);
760 priv->tx_skb = NULL;
Christian Pellegrine0000162009-11-02 23:07:00 +0000761 }
Christian Pellegrine0000162009-11-02 23:07:00 +0000762 }
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000763 mutex_unlock(&priv->mcp_lock);
Christian Pellegrine0000162009-11-02 23:07:00 +0000764}
765
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000766static void mcp251x_restart_work_handler(struct work_struct *ws)
Christian Pellegrine0000162009-11-02 23:07:00 +0000767{
768 struct mcp251x_priv *priv = container_of(ws, struct mcp251x_priv,
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000769 restart_work);
Christian Pellegrine0000162009-11-02 23:07:00 +0000770 struct spi_device *spi = priv->spi;
771 struct net_device *net = priv->net;
Christian Pellegrine0000162009-11-02 23:07:00 +0000772
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000773 mutex_lock(&priv->mcp_lock);
Christian Pellegrine0000162009-11-02 23:07:00 +0000774 if (priv->after_suspend) {
775 mdelay(10);
776 mcp251x_hw_reset(spi);
777 mcp251x_setup(net, priv, spi);
778 if (priv->after_suspend & AFTER_SUSPEND_RESTART) {
779 mcp251x_set_normal_mode(spi);
780 } else if (priv->after_suspend & AFTER_SUSPEND_UP) {
781 netif_device_attach(net);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000782 mcp251x_clean(net);
Christian Pellegrine0000162009-11-02 23:07:00 +0000783 mcp251x_set_normal_mode(spi);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000784 netif_wake_queue(net);
Christian Pellegrine0000162009-11-02 23:07:00 +0000785 } else {
786 mcp251x_hw_sleep(spi);
787 }
788 priv->after_suspend = 0;
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000789 priv->force_quit = 0;
Christian Pellegrine0000162009-11-02 23:07:00 +0000790 }
791
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000792 if (priv->restart_tx) {
793 priv->restart_tx = 0;
794 mcp251x_write_reg(spi, TXBCTRL(0), 0);
795 mcp251x_clean(net);
796 netif_wake_queue(net);
797 mcp251x_error_skb(net, CAN_ERR_RESTARTED, 0);
798 }
799 mutex_unlock(&priv->mcp_lock);
800}
Christian Pellegrine0000162009-11-02 23:07:00 +0000801
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000802static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
803{
804 struct mcp251x_priv *priv = dev_id;
805 struct spi_device *spi = priv->spi;
806 struct net_device *net = priv->net;
807
808 mutex_lock(&priv->mcp_lock);
809 while (!priv->force_quit) {
810 enum can_state new_state;
Sascha Hauerf3a3ed32010-09-28 09:53:35 +0200811 u8 intf, eflag;
Marc Kleine-Budded3cd1562010-09-28 10:18:34 +0200812 u8 clear_intf = 0;
Christian Pellegrine0000162009-11-02 23:07:00 +0000813 int can_id = 0, data1 = 0;
814
Sascha Hauerf3a3ed32010-09-28 09:53:35 +0200815 mcp251x_read_2regs(spi, CANINTF, &intf, &eflag);
816
Marc Kleine-Budde5601b2d2010-10-20 00:02:25 +0000817 /* mask out flags we don't care about */
818 intf &= CANINTF_RX | CANINTF_TX | CANINTF_ERR;
819
Marc Kleine-Budded3cd1562010-09-28 10:18:34 +0200820 /* receive buffer 0 */
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000821 if (intf & CANINTF_RX0IF) {
822 mcp251x_hw_rx(spi, 0);
Marc Kleine-Budde9c473fc2010-10-04 12:09:31 +0200823 /*
824 * Free one buffer ASAP
825 * (The MCP2515 does this automatically.)
826 */
827 if (mcp251x_is_2510(spi))
828 mcp251x_write_bits(spi, CANINTF, CANINTF_RX0IF, 0x00);
Christian Pellegrine0000162009-11-02 23:07:00 +0000829 }
830
Marc Kleine-Budded3cd1562010-09-28 10:18:34 +0200831 /* receive buffer 1 */
832 if (intf & CANINTF_RX1IF) {
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000833 mcp251x_hw_rx(spi, 1);
Marc Kleine-Budde9c473fc2010-10-04 12:09:31 +0200834 /* the MCP2515 does this automatically */
835 if (mcp251x_is_2510(spi))
836 clear_intf |= CANINTF_RX1IF;
Marc Kleine-Budded3cd1562010-09-28 10:18:34 +0200837 }
Christian Pellegrine0000162009-11-02 23:07:00 +0000838
Marc Kleine-Budded3cd1562010-09-28 10:18:34 +0200839 /* any error or tx interrupt we need to clear? */
Marc Kleine-Budde5601b2d2010-10-20 00:02:25 +0000840 if (intf & (CANINTF_ERR | CANINTF_TX))
841 clear_intf |= intf & (CANINTF_ERR | CANINTF_TX);
Marc Kleine-Budded3cd1562010-09-28 10:18:34 +0200842 if (clear_intf)
843 mcp251x_write_bits(spi, CANINTF, clear_intf, 0x00);
Christian Pellegrine0000162009-11-02 23:07:00 +0000844
Sascha Hauer7e15de32010-09-28 10:00:47 +0200845 if (eflag)
846 mcp251x_write_bits(spi, EFLG, eflag, 0x00);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000847
Christian Pellegrine0000162009-11-02 23:07:00 +0000848 /* Update can state */
849 if (eflag & EFLG_TXBO) {
850 new_state = CAN_STATE_BUS_OFF;
851 can_id |= CAN_ERR_BUSOFF;
852 } else if (eflag & EFLG_TXEP) {
853 new_state = CAN_STATE_ERROR_PASSIVE;
854 can_id |= CAN_ERR_CRTL;
855 data1 |= CAN_ERR_CRTL_TX_PASSIVE;
856 } else if (eflag & EFLG_RXEP) {
857 new_state = CAN_STATE_ERROR_PASSIVE;
858 can_id |= CAN_ERR_CRTL;
859 data1 |= CAN_ERR_CRTL_RX_PASSIVE;
860 } else if (eflag & EFLG_TXWAR) {
861 new_state = CAN_STATE_ERROR_WARNING;
862 can_id |= CAN_ERR_CRTL;
863 data1 |= CAN_ERR_CRTL_TX_WARNING;
864 } else if (eflag & EFLG_RXWAR) {
865 new_state = CAN_STATE_ERROR_WARNING;
866 can_id |= CAN_ERR_CRTL;
867 data1 |= CAN_ERR_CRTL_RX_WARNING;
868 } else {
869 new_state = CAN_STATE_ERROR_ACTIVE;
870 }
871
872 /* Update can state statistics */
873 switch (priv->can.state) {
874 case CAN_STATE_ERROR_ACTIVE:
875 if (new_state >= CAN_STATE_ERROR_WARNING &&
876 new_state <= CAN_STATE_BUS_OFF)
877 priv->can.can_stats.error_warning++;
878 case CAN_STATE_ERROR_WARNING: /* fallthrough */
879 if (new_state >= CAN_STATE_ERROR_PASSIVE &&
880 new_state <= CAN_STATE_BUS_OFF)
881 priv->can.can_stats.error_passive++;
882 break;
883 default:
884 break;
885 }
886 priv->can.state = new_state;
887
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000888 if (intf & CANINTF_ERRIF) {
889 /* Handle overflow counters */
890 if (eflag & (EFLG_RX0OVR | EFLG_RX1OVR)) {
Sascha Hauer711e4d62010-09-30 09:46:00 +0200891 if (eflag & EFLG_RX0OVR) {
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000892 net->stats.rx_over_errors++;
Sascha Hauer711e4d62010-09-30 09:46:00 +0200893 net->stats.rx_errors++;
894 }
895 if (eflag & EFLG_RX1OVR) {
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000896 net->stats.rx_over_errors++;
Sascha Hauer711e4d62010-09-30 09:46:00 +0200897 net->stats.rx_errors++;
898 }
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000899 can_id |= CAN_ERR_CRTL;
900 data1 |= CAN_ERR_CRTL_RX_OVERFLOW;
Christian Pellegrine0000162009-11-02 23:07:00 +0000901 }
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000902 mcp251x_error_skb(net, can_id, data1);
Christian Pellegrine0000162009-11-02 23:07:00 +0000903 }
904
905 if (priv->can.state == CAN_STATE_BUS_OFF) {
906 if (priv->can.restart_ms == 0) {
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000907 priv->force_quit = 1;
Christian Pellegrine0000162009-11-02 23:07:00 +0000908 can_bus_off(net);
909 mcp251x_hw_sleep(spi);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000910 break;
Christian Pellegrine0000162009-11-02 23:07:00 +0000911 }
912 }
913
914 if (intf == 0)
915 break;
916
Marc Kleine-Budde5601b2d2010-10-20 00:02:25 +0000917 if (intf & CANINTF_TX) {
Christian Pellegrine0000162009-11-02 23:07:00 +0000918 net->stats.tx_packets++;
919 net->stats.tx_bytes += priv->tx_len - 1;
Fabio Baltierieb072a92012-12-18 18:51:02 +0100920 can_led_event(net, CAN_LED_EVENT_TX);
Christian Pellegrine0000162009-11-02 23:07:00 +0000921 if (priv->tx_len) {
922 can_get_echo_skb(net, 0);
923 priv->tx_len = 0;
924 }
925 netif_wake_queue(net);
926 }
927
Christian Pellegrine0000162009-11-02 23:07:00 +0000928 }
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000929 mutex_unlock(&priv->mcp_lock);
930 return IRQ_HANDLED;
931}
932
933static int mcp251x_open(struct net_device *net)
934{
935 struct mcp251x_priv *priv = netdev_priv(net);
936 struct spi_device *spi = priv->spi;
Alexander Shiyanae5d5892013-08-19 15:39:20 +0400937 unsigned long flags = IRQF_ONESHOT | IRQF_TRIGGER_FALLING;
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000938 int ret;
939
940 ret = open_candev(net);
941 if (ret) {
942 dev_err(&spi->dev, "unable to set initial baudrate!\n");
943 return ret;
944 }
945
946 mutex_lock(&priv->mcp_lock);
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +0400947 mcp251x_power_enable(priv->transceiver, 1);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000948
949 priv->force_quit = 0;
950 priv->tx_skb = NULL;
951 priv->tx_len = 0;
952
953 ret = request_threaded_irq(spi->irq, NULL, mcp251x_can_ist,
Marc Kleine-Buddedb388d62013-04-11 10:08:27 +0200954 flags, DEVICE_NAME, priv);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000955 if (ret) {
956 dev_err(&spi->dev, "failed to acquire irq %d\n", spi->irq);
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +0400957 mcp251x_power_enable(priv->transceiver, 0);
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000958 close_candev(net);
959 goto open_unlock;
960 }
961
Tejun Heo58a69cb2011-02-16 09:25:31 +0100962 priv->wq = create_freezable_workqueue("mcp251x_wq");
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000963 INIT_WORK(&priv->tx_work, mcp251x_tx_work_handler);
964 INIT_WORK(&priv->restart_work, mcp251x_restart_work_handler);
965
966 ret = mcp251x_hw_reset(spi);
967 if (ret) {
968 mcp251x_open_clean(net);
969 goto open_unlock;
970 }
971 ret = mcp251x_setup(net, priv, spi);
972 if (ret) {
973 mcp251x_open_clean(net);
974 goto open_unlock;
975 }
976 ret = mcp251x_set_normal_mode(spi);
977 if (ret) {
978 mcp251x_open_clean(net);
979 goto open_unlock;
980 }
Fabio Baltierieb072a92012-12-18 18:51:02 +0100981
982 can_led_event(net, CAN_LED_EVENT_OPEN);
983
Christian Pellegrinbf66f372010-02-03 07:39:54 +0000984 netif_wake_queue(net);
985
986open_unlock:
987 mutex_unlock(&priv->mcp_lock);
988 return ret;
Christian Pellegrine0000162009-11-02 23:07:00 +0000989}
990
991static const struct net_device_ops mcp251x_netdev_ops = {
992 .ndo_open = mcp251x_open,
993 .ndo_stop = mcp251x_stop,
994 .ndo_start_xmit = mcp251x_hard_start_xmit,
995};
996
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -0500997static int mcp251x_can_probe(struct spi_device *spi)
Christian Pellegrine0000162009-11-02 23:07:00 +0000998{
999 struct net_device *net;
1000 struct mcp251x_priv *priv;
Jingoo Han369566e2013-09-10 17:42:19 +09001001 struct mcp251x_platform_data *pdata = dev_get_platdata(&spi->dev);
Christian Pellegrine0000162009-11-02 23:07:00 +00001002 int ret = -ENODEV;
1003
1004 if (!pdata)
1005 /* Platform data is required for osc freq */
1006 goto error_out;
1007
1008 /* Allocate can/net device */
1009 net = alloc_candev(sizeof(struct mcp251x_priv), TX_ECHO_SKB_MAX);
1010 if (!net) {
1011 ret = -ENOMEM;
1012 goto error_alloc;
1013 }
1014
1015 net->netdev_ops = &mcp251x_netdev_ops;
1016 net->flags |= IFF_ECHO;
1017
1018 priv = netdev_priv(net);
1019 priv->can.bittiming_const = &mcp251x_bittiming_const;
1020 priv->can.do_set_mode = mcp251x_do_set_mode;
1021 priv->can.clock.freq = pdata->oscillator_frequency / 2;
Christian Pellegrinad72c342010-01-14 07:08:34 +00001022 priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES |
1023 CAN_CTRLMODE_LOOPBACK | CAN_CTRLMODE_LISTENONLY;
Marc Kleine-Buddef1f8c6c2010-10-18 15:00:18 +02001024 priv->model = spi_get_device_id(spi)->driver_data;
Christian Pellegrine0000162009-11-02 23:07:00 +00001025 priv->net = net;
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +04001026
1027 priv->power = devm_regulator_get(&spi->dev, "vdd");
1028 priv->transceiver = devm_regulator_get(&spi->dev, "xceiver");
1029 if ((PTR_ERR(priv->power) == -EPROBE_DEFER) ||
1030 (PTR_ERR(priv->transceiver) == -EPROBE_DEFER)) {
1031 ret = -EPROBE_DEFER;
1032 goto error_power;
1033 }
1034
1035 ret = mcp251x_power_enable(priv->power, 1);
1036 if (ret)
1037 goto error_power;
1038
Jingoo Hanfce5c292013-04-05 20:35:14 +00001039 spi_set_drvdata(spi, priv);
Christian Pellegrine0000162009-11-02 23:07:00 +00001040
1041 priv->spi = spi;
Christian Pellegrinbf66f372010-02-03 07:39:54 +00001042 mutex_init(&priv->mcp_lock);
Christian Pellegrine0000162009-11-02 23:07:00 +00001043
1044 /* If requested, allocate DMA buffers */
1045 if (mcp251x_enable_dma) {
1046 spi->dev.coherent_dma_mask = ~0;
1047
1048 /*
1049 * Minimum coherent DMA allocation is PAGE_SIZE, so allocate
1050 * that much and share it between Tx and Rx DMA buffers.
1051 */
1052 priv->spi_tx_buf = dma_alloc_coherent(&spi->dev,
1053 PAGE_SIZE,
1054 &priv->spi_tx_dma,
1055 GFP_DMA);
1056
1057 if (priv->spi_tx_buf) {
Joe Perchesc2fd03a2012-06-04 12:44:18 +00001058 priv->spi_rx_buf = (priv->spi_tx_buf + (PAGE_SIZE / 2));
Christian Pellegrine0000162009-11-02 23:07:00 +00001059 priv->spi_rx_dma = (dma_addr_t)(priv->spi_tx_dma +
1060 (PAGE_SIZE / 2));
1061 } else {
1062 /* Fall back to non-DMA */
1063 mcp251x_enable_dma = 0;
1064 }
1065 }
1066
1067 /* Allocate non-DMA buffers */
1068 if (!mcp251x_enable_dma) {
Alexander Shiyan21629e12013-12-15 18:16:00 +04001069 priv->spi_tx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN,
1070 GFP_KERNEL);
Christian Pellegrine0000162009-11-02 23:07:00 +00001071 if (!priv->spi_tx_buf) {
1072 ret = -ENOMEM;
Alexander Shiyan21629e12013-12-15 18:16:00 +04001073 goto error_probe;
Christian Pellegrine0000162009-11-02 23:07:00 +00001074 }
Alexander Shiyan21629e12013-12-15 18:16:00 +04001075 priv->spi_rx_buf = devm_kzalloc(&spi->dev, SPI_TRANSFER_BUF_LEN,
1076 GFP_KERNEL);
Julia Lawallce739b42009-12-27 11:27:44 +00001077 if (!priv->spi_rx_buf) {
Christian Pellegrine0000162009-11-02 23:07:00 +00001078 ret = -ENOMEM;
Alexander Shiyan21629e12013-12-15 18:16:00 +04001079 goto error_probe;
Christian Pellegrine0000162009-11-02 23:07:00 +00001080 }
1081 }
1082
Christian Pellegrine0000162009-11-02 23:07:00 +00001083 SET_NETDEV_DEV(net, &spi->dev);
1084
Christian Pellegrine0000162009-11-02 23:07:00 +00001085 /* Configure the SPI bus */
Alexander Shiyanb1ef05a2013-08-19 15:39:21 +04001086 spi->mode = spi->mode ? : SPI_MODE_0;
1087 if (mcp251x_is_2510(spi))
1088 spi->max_speed_hz = spi->max_speed_hz ? : 5 * 1000 * 1000;
1089 else
1090 spi->max_speed_hz = spi->max_speed_hz ? : 10 * 1000 * 1000;
Christian Pellegrine0000162009-11-02 23:07:00 +00001091 spi->bits_per_word = 8;
1092 spi_setup(spi);
1093
Christian Pellegrinbf66f372010-02-03 07:39:54 +00001094 /* Here is OK to not lock the MCP, no one knows about it yet */
Christian Pellegrine0000162009-11-02 23:07:00 +00001095 if (!mcp251x_hw_probe(spi)) {
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +04001096 ret = -ENODEV;
Christian Pellegrine0000162009-11-02 23:07:00 +00001097 goto error_probe;
1098 }
1099 mcp251x_hw_sleep(spi);
1100
Christian Pellegrine0000162009-11-02 23:07:00 +00001101 ret = register_candev(net);
Fabio Baltierieb072a92012-12-18 18:51:02 +01001102 if (ret)
1103 goto error_probe;
1104
1105 devm_can_led_init(net);
1106
1107 dev_info(&spi->dev, "probed\n");
1108
1109 return ret;
1110
Christian Pellegrine0000162009-11-02 23:07:00 +00001111error_probe:
Christian Pellegrine0000162009-11-02 23:07:00 +00001112 if (mcp251x_enable_dma)
1113 dma_free_coherent(&spi->dev, PAGE_SIZE,
1114 priv->spi_tx_buf, priv->spi_tx_dma);
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +04001115 mcp251x_power_enable(priv->power, 0);
1116error_power:
1117 free_candev(net);
Christian Pellegrine0000162009-11-02 23:07:00 +00001118error_alloc:
Christian Pellegrine0000162009-11-02 23:07:00 +00001119 dev_err(&spi->dev, "probe failed\n");
1120error_out:
1121 return ret;
1122}
1123
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001124static int mcp251x_can_remove(struct spi_device *spi)
Christian Pellegrine0000162009-11-02 23:07:00 +00001125{
Jingoo Hanfce5c292013-04-05 20:35:14 +00001126 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +00001127 struct net_device *net = priv->net;
1128
1129 unregister_candev(net);
Christian Pellegrine0000162009-11-02 23:07:00 +00001130
Christian Pellegrine0000162009-11-02 23:07:00 +00001131 if (mcp251x_enable_dma) {
1132 dma_free_coherent(&spi->dev, PAGE_SIZE,
1133 priv->spi_tx_buf, priv->spi_tx_dma);
Christian Pellegrine0000162009-11-02 23:07:00 +00001134 }
1135
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +04001136 mcp251x_power_enable(priv->power, 0);
1137
1138 free_candev(net);
Christian Pellegrine0000162009-11-02 23:07:00 +00001139
1140 return 0;
1141}
1142
Lars-Peter Clausen612b2a92013-03-12 13:13:53 +01001143#ifdef CONFIG_PM_SLEEP
1144
1145static int mcp251x_can_suspend(struct device *dev)
Christian Pellegrine0000162009-11-02 23:07:00 +00001146{
Lars-Peter Clausen612b2a92013-03-12 13:13:53 +01001147 struct spi_device *spi = to_spi_device(dev);
Jingoo Hanfce5c292013-04-05 20:35:14 +00001148 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +00001149 struct net_device *net = priv->net;
1150
Christian Pellegrinbf66f372010-02-03 07:39:54 +00001151 priv->force_quit = 1;
1152 disable_irq(spi->irq);
1153 /*
1154 * Note: at this point neither IST nor workqueues are running.
1155 * open/stop cannot be called anyway so locking is not needed
1156 */
Christian Pellegrine0000162009-11-02 23:07:00 +00001157 if (netif_running(net)) {
1158 netif_device_detach(net);
1159
1160 mcp251x_hw_sleep(spi);
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +04001161 mcp251x_power_enable(priv->transceiver, 0);
Christian Pellegrine0000162009-11-02 23:07:00 +00001162 priv->after_suspend = AFTER_SUSPEND_UP;
1163 } else {
1164 priv->after_suspend = AFTER_SUSPEND_DOWN;
1165 }
1166
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +04001167 if (!IS_ERR(priv->power)) {
1168 regulator_disable(priv->power);
Christian Pellegrine0000162009-11-02 23:07:00 +00001169 priv->after_suspend |= AFTER_SUSPEND_POWER;
1170 }
1171
1172 return 0;
1173}
1174
Lars-Peter Clausen612b2a92013-03-12 13:13:53 +01001175static int mcp251x_can_resume(struct device *dev)
Christian Pellegrine0000162009-11-02 23:07:00 +00001176{
Lars-Peter Clausen612b2a92013-03-12 13:13:53 +01001177 struct spi_device *spi = to_spi_device(dev);
Jingoo Hanfce5c292013-04-05 20:35:14 +00001178 struct mcp251x_priv *priv = spi_get_drvdata(spi);
Christian Pellegrine0000162009-11-02 23:07:00 +00001179
1180 if (priv->after_suspend & AFTER_SUSPEND_POWER) {
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +04001181 mcp251x_power_enable(priv->power, 1);
Christian Pellegrinbf66f372010-02-03 07:39:54 +00001182 queue_work(priv->wq, &priv->restart_work);
Christian Pellegrine0000162009-11-02 23:07:00 +00001183 } else {
1184 if (priv->after_suspend & AFTER_SUSPEND_UP) {
Alexander Shiyan1ddff7d2013-08-19 15:39:19 +04001185 mcp251x_power_enable(priv->transceiver, 1);
Christian Pellegrinbf66f372010-02-03 07:39:54 +00001186 queue_work(priv->wq, &priv->restart_work);
Christian Pellegrine0000162009-11-02 23:07:00 +00001187 } else {
1188 priv->after_suspend = 0;
1189 }
1190 }
Christian Pellegrinbf66f372010-02-03 07:39:54 +00001191 priv->force_quit = 0;
1192 enable_irq(spi->irq);
Christian Pellegrine0000162009-11-02 23:07:00 +00001193 return 0;
1194}
Fabio Estevam4fcc9992013-04-16 09:28:27 +00001195#endif
Lars-Peter Clausen612b2a92013-03-12 13:13:53 +01001196
1197static SIMPLE_DEV_PM_OPS(mcp251x_can_pm_ops, mcp251x_can_suspend,
1198 mcp251x_can_resume);
Christian Pellegrine0000162009-11-02 23:07:00 +00001199
Marc Kleine-Buddef1f8c6c2010-10-18 15:00:18 +02001200static const struct spi_device_id mcp251x_id_table[] = {
Marc Zyngiere4466302010-03-29 08:57:56 +00001201 { "mcp2510", CAN_MCP251X_MCP2510 },
1202 { "mcp2515", CAN_MCP251X_MCP2515 },
1203 { },
1204};
1205
1206MODULE_DEVICE_TABLE(spi, mcp251x_id_table);
1207
Christian Pellegrine0000162009-11-02 23:07:00 +00001208static struct spi_driver mcp251x_can_driver = {
1209 .driver = {
1210 .name = DEVICE_NAME,
Christian Pellegrine0000162009-11-02 23:07:00 +00001211 .owner = THIS_MODULE,
Fabio Estevam4fcc9992013-04-16 09:28:27 +00001212 .pm = &mcp251x_can_pm_ops,
Christian Pellegrine0000162009-11-02 23:07:00 +00001213 },
1214
Marc Zyngiere4466302010-03-29 08:57:56 +00001215 .id_table = mcp251x_id_table,
Christian Pellegrine0000162009-11-02 23:07:00 +00001216 .probe = mcp251x_can_probe,
Bill Pemberton3c8ac0f2012-12-03 09:22:44 -05001217 .remove = mcp251x_can_remove,
Christian Pellegrine0000162009-11-02 23:07:00 +00001218};
Lars-Peter Clausen01b88072013-03-12 13:13:52 +01001219module_spi_driver(mcp251x_can_driver);
Christian Pellegrine0000162009-11-02 23:07:00 +00001220
1221MODULE_AUTHOR("Chris Elston <celston@katalix.com>, "
1222 "Christian Pellegrin <chripell@evolware.org>");
1223MODULE_DESCRIPTION("Microchip 251x CAN driver");
1224MODULE_LICENSE("GPL v2");