blob: 68839380086d2c376249a1abd9f72fc38ecf7c6f [file] [log] [blame]
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001/*
2 * CAN bus driver for Bosch C_CAN controller
3 *
4 * Copyright (C) 2010 ST Microelectronics
5 * Bhupesh Sharma <bhupesh.sharma@st.com>
6 *
7 * Borrowed heavily from the C_CAN driver originally written by:
8 * Copyright (C) 2007
9 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix <s.hauer@pengutronix.de>
10 * - Simon Kallweit, intefo AG <simon.kallweit@intefo.ch>
11 *
12 * TX and RX NAPI implementation has been borrowed from at91 CAN driver
13 * written by:
14 * Copyright
15 * (C) 2007 by Hans J. Koch <hjk@hansjkoch.de>
16 * (C) 2008, 2009 by Marc Kleine-Budde <kernel@pengutronix.de>
17 *
18 * Bosch C_CAN controller is compliant to CAN protocol version 2.0 part A and B.
19 * Bosch C_CAN user manual can be obtained from:
20 * http://www.semiconductors.bosch.de/media/en/pdf/ipmodules_1/c_can/
21 * users_manual_c_can.pdf
22 *
23 * This file is licensed under the terms of the GNU General Public
24 * License version 2. This program is licensed "as is" without any
25 * warranty of any kind, whether express or implied.
26 */
27
28#include <linux/kernel.h>
Bhupesh Sharma881ff672011-02-13 22:51:44 -080029#include <linux/module.h>
30#include <linux/interrupt.h>
31#include <linux/delay.h>
32#include <linux/netdevice.h>
33#include <linux/if_arp.h>
34#include <linux/if_ether.h>
35#include <linux/list.h>
Bhupesh Sharma881ff672011-02-13 22:51:44 -080036#include <linux/io.h>
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +053037#include <linux/pm_runtime.h>
Bhupesh Sharma881ff672011-02-13 22:51:44 -080038
39#include <linux/can.h>
40#include <linux/can/dev.h>
41#include <linux/can/error.h>
Fabio Baltieri5090f802012-12-18 18:51:01 +010042#include <linux/can/led.h>
Bhupesh Sharma881ff672011-02-13 22:51:44 -080043
44#include "c_can.h"
45
AnilKumar Ch33f81002012-05-29 11:13:15 +053046/* Number of interface registers */
47#define IF_ENUM_REG_LEN 11
48#define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN)
49
AnilKumar Ch82120032012-09-21 15:29:01 +053050/* control extension register D_CAN specific */
51#define CONTROL_EX_PDR BIT(8)
52
Bhupesh Sharma881ff672011-02-13 22:51:44 -080053/* control register */
54#define CONTROL_TEST BIT(7)
55#define CONTROL_CCE BIT(6)
56#define CONTROL_DISABLE_AR BIT(5)
57#define CONTROL_ENABLE_AR (0 << 5)
58#define CONTROL_EIE BIT(3)
59#define CONTROL_SIE BIT(2)
60#define CONTROL_IE BIT(1)
61#define CONTROL_INIT BIT(0)
62
63/* test register */
64#define TEST_RX BIT(7)
65#define TEST_TX1 BIT(6)
66#define TEST_TX2 BIT(5)
67#define TEST_LBACK BIT(4)
68#define TEST_SILENT BIT(3)
69#define TEST_BASIC BIT(2)
70
71/* status register */
AnilKumar Ch82120032012-09-21 15:29:01 +053072#define STATUS_PDA BIT(10)
Bhupesh Sharma881ff672011-02-13 22:51:44 -080073#define STATUS_BOFF BIT(7)
74#define STATUS_EWARN BIT(6)
75#define STATUS_EPASS BIT(5)
76#define STATUS_RXOK BIT(4)
77#define STATUS_TXOK BIT(3)
78
79/* error counter register */
80#define ERR_CNT_TEC_MASK 0xff
81#define ERR_CNT_TEC_SHIFT 0
82#define ERR_CNT_REC_SHIFT 8
83#define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
84#define ERR_CNT_RP_SHIFT 15
85#define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
86
87/* bit-timing register */
88#define BTR_BRP_MASK 0x3f
89#define BTR_BRP_SHIFT 0
90#define BTR_SJW_SHIFT 6
91#define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
92#define BTR_TSEG1_SHIFT 8
93#define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
94#define BTR_TSEG2_SHIFT 12
95#define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
96
97/* brp extension register */
98#define BRP_EXT_BRPE_MASK 0x0f
99#define BRP_EXT_BRPE_SHIFT 0
100
101/* IFx command request */
102#define IF_COMR_BUSY BIT(15)
103
104/* IFx command mask */
105#define IF_COMM_WR BIT(7)
106#define IF_COMM_MASK BIT(6)
107#define IF_COMM_ARB BIT(5)
108#define IF_COMM_CONTROL BIT(4)
109#define IF_COMM_CLR_INT_PND BIT(3)
110#define IF_COMM_TXRQST BIT(2)
111#define IF_COMM_DATAA BIT(1)
112#define IF_COMM_DATAB BIT(0)
113#define IF_COMM_ALL (IF_COMM_MASK | IF_COMM_ARB | \
114 IF_COMM_CONTROL | IF_COMM_TXRQST | \
115 IF_COMM_DATAA | IF_COMM_DATAB)
116
117/* IFx arbitration */
118#define IF_ARB_MSGVAL BIT(15)
119#define IF_ARB_MSGXTD BIT(14)
120#define IF_ARB_TRANSMIT BIT(13)
121
122/* IFx message control */
123#define IF_MCONT_NEWDAT BIT(15)
124#define IF_MCONT_MSGLST BIT(14)
125#define IF_MCONT_CLR_MSGLST (0 << 14)
126#define IF_MCONT_INTPND BIT(13)
127#define IF_MCONT_UMASK BIT(12)
128#define IF_MCONT_TXIE BIT(11)
129#define IF_MCONT_RXIE BIT(10)
130#define IF_MCONT_RMTEN BIT(9)
131#define IF_MCONT_TXRQST BIT(8)
132#define IF_MCONT_EOB BIT(7)
133#define IF_MCONT_DLC_MASK 0xf
134
135/*
136 * IFx register masks:
137 * allow easy operation on 16-bit registers when the
138 * argument is 32-bit instead
139 */
140#define IFX_WRITE_LOW_16BIT(x) ((x) & 0xFFFF)
141#define IFX_WRITE_HIGH_16BIT(x) (((x) & 0xFFFF0000) >> 16)
142
143/* message object split */
144#define C_CAN_NO_OF_OBJECTS 32
145#define C_CAN_MSG_OBJ_RX_NUM 16
146#define C_CAN_MSG_OBJ_TX_NUM 16
147
148#define C_CAN_MSG_OBJ_RX_FIRST 1
149#define C_CAN_MSG_OBJ_RX_LAST (C_CAN_MSG_OBJ_RX_FIRST + \
150 C_CAN_MSG_OBJ_RX_NUM - 1)
151
152#define C_CAN_MSG_OBJ_TX_FIRST (C_CAN_MSG_OBJ_RX_LAST + 1)
153#define C_CAN_MSG_OBJ_TX_LAST (C_CAN_MSG_OBJ_TX_FIRST + \
154 C_CAN_MSG_OBJ_TX_NUM - 1)
155
156#define C_CAN_MSG_OBJ_RX_SPLIT 9
157#define C_CAN_MSG_RX_LOW_LAST (C_CAN_MSG_OBJ_RX_SPLIT - 1)
158
159#define C_CAN_NEXT_MSG_OBJ_MASK (C_CAN_MSG_OBJ_TX_NUM - 1)
160#define RECEIVE_OBJECT_BITS 0x0000ffff
161
162/* status interrupt */
163#define STATUS_INTERRUPT 0x8000
164
165/* global interrupt masks */
166#define ENABLE_ALL_INTERRUPTS 1
167#define DISABLE_ALL_INTERRUPTS 0
168
169/* minimum timeout for checking BUSY status */
170#define MIN_TIMEOUT_VALUE 6
171
AnilKumar Ch82120032012-09-21 15:29:01 +0530172/* Wait for ~1 sec for INIT bit */
173#define INIT_WAIT_MS 1000
174
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800175/* napi related */
176#define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
177
178/* c_can lec values */
179enum c_can_lec_type {
180 LEC_NO_ERROR = 0,
181 LEC_STUFF_ERROR,
182 LEC_FORM_ERROR,
183 LEC_ACK_ERROR,
184 LEC_BIT1_ERROR,
185 LEC_BIT0_ERROR,
186 LEC_CRC_ERROR,
187 LEC_UNUSED,
188};
189
190/*
191 * c_can error types:
192 * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
193 */
194enum c_can_bus_error_types {
195 C_CAN_NO_ERROR = 0,
196 C_CAN_BUS_OFF,
197 C_CAN_ERROR_WARNING,
198 C_CAN_ERROR_PASSIVE,
199};
200
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200201static const struct can_bittiming_const c_can_bittiming_const = {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800202 .name = KBUILD_MODNAME,
203 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
204 .tseg1_max = 16,
205 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
206 .tseg2_max = 8,
207 .sjw_max = 4,
208 .brp_min = 1,
209 .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/
210 .brp_inc = 1,
211};
212
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +0530213static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
214{
215 if (priv->device)
216 pm_runtime_enable(priv->device);
217}
218
219static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
220{
221 if (priv->device)
222 pm_runtime_disable(priv->device);
223}
224
225static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
226{
227 if (priv->device)
228 pm_runtime_get_sync(priv->device);
229}
230
231static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
232{
233 if (priv->device)
234 pm_runtime_put_sync(priv->device);
235}
236
AnilKumar Ch52cde852012-11-21 11:14:10 +0530237static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
238{
239 if (priv->raminit)
240 priv->raminit(priv, enable);
241}
242
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800243static inline int get_tx_next_msg_obj(const struct c_can_priv *priv)
244{
245 return (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) +
246 C_CAN_MSG_OBJ_TX_FIRST;
247}
248
249static inline int get_tx_echo_msg_obj(const struct c_can_priv *priv)
250{
251 return (priv->tx_echo & C_CAN_NEXT_MSG_OBJ_MASK) +
252 C_CAN_MSG_OBJ_TX_FIRST;
253}
254
AnilKumar Ch33f81002012-05-29 11:13:15 +0530255static u32 c_can_read_reg32(struct c_can_priv *priv, enum reg index)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800256{
AnilKumar Ch33f81002012-05-29 11:13:15 +0530257 u32 val = priv->read_reg(priv, index);
258 val |= ((u32) priv->read_reg(priv, index + 1)) << 16;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800259 return val;
260}
261
262static void c_can_enable_all_interrupts(struct c_can_priv *priv,
263 int enable)
264{
265 unsigned int cntrl_save = priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530266 C_CAN_CTRL_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800267
268 if (enable)
269 cntrl_save |= (CONTROL_SIE | CONTROL_EIE | CONTROL_IE);
270 else
271 cntrl_save &= ~(CONTROL_EIE | CONTROL_IE | CONTROL_SIE);
272
AnilKumar Ch33f81002012-05-29 11:13:15 +0530273 priv->write_reg(priv, C_CAN_CTRL_REG, cntrl_save);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800274}
275
276static inline int c_can_msg_obj_is_busy(struct c_can_priv *priv, int iface)
277{
278 int count = MIN_TIMEOUT_VALUE;
279
280 while (count && priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530281 C_CAN_IFACE(COMREQ_REG, iface)) &
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800282 IF_COMR_BUSY) {
283 count--;
284 udelay(1);
285 }
286
287 if (!count)
288 return 1;
289
290 return 0;
291}
292
293static inline void c_can_object_get(struct net_device *dev,
294 int iface, int objno, int mask)
295{
296 struct c_can_priv *priv = netdev_priv(dev);
297
298 /*
299 * As per specs, after writting the message object number in the
300 * IF command request register the transfer b/w interface
301 * register and message RAM must be complete in 6 CAN-CLK
302 * period.
303 */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530304 priv->write_reg(priv, C_CAN_IFACE(COMMSK_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800305 IFX_WRITE_LOW_16BIT(mask));
AnilKumar Ch33f81002012-05-29 11:13:15 +0530306 priv->write_reg(priv, C_CAN_IFACE(COMREQ_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800307 IFX_WRITE_LOW_16BIT(objno));
308
309 if (c_can_msg_obj_is_busy(priv, iface))
310 netdev_err(dev, "timed out in object get\n");
311}
312
313static inline void c_can_object_put(struct net_device *dev,
314 int iface, int objno, int mask)
315{
316 struct c_can_priv *priv = netdev_priv(dev);
317
318 /*
319 * As per specs, after writting the message object number in the
320 * IF command request register the transfer b/w interface
321 * register and message RAM must be complete in 6 CAN-CLK
322 * period.
323 */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530324 priv->write_reg(priv, C_CAN_IFACE(COMMSK_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800325 (IF_COMM_WR | IFX_WRITE_LOW_16BIT(mask)));
AnilKumar Ch33f81002012-05-29 11:13:15 +0530326 priv->write_reg(priv, C_CAN_IFACE(COMREQ_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800327 IFX_WRITE_LOW_16BIT(objno));
328
329 if (c_can_msg_obj_is_busy(priv, iface))
330 netdev_err(dev, "timed out in object put\n");
331}
332
333static void c_can_write_msg_object(struct net_device *dev,
334 int iface, struct can_frame *frame, int objno)
335{
336 int i;
337 u16 flags = 0;
338 unsigned int id;
339 struct c_can_priv *priv = netdev_priv(dev);
340
341 if (!(frame->can_id & CAN_RTR_FLAG))
342 flags |= IF_ARB_TRANSMIT;
343
344 if (frame->can_id & CAN_EFF_FLAG) {
345 id = frame->can_id & CAN_EFF_MASK;
346 flags |= IF_ARB_MSGXTD;
347 } else
348 id = ((frame->can_id & CAN_SFF_MASK) << 18);
349
350 flags |= IF_ARB_MSGVAL;
351
AnilKumar Ch33f81002012-05-29 11:13:15 +0530352 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800353 IFX_WRITE_LOW_16BIT(id));
AnilKumar Ch33f81002012-05-29 11:13:15 +0530354 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), flags |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800355 IFX_WRITE_HIGH_16BIT(id));
356
357 for (i = 0; i < frame->can_dlc; i += 2) {
AnilKumar Ch33f81002012-05-29 11:13:15 +0530358 priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800359 frame->data[i] | (frame->data[i + 1] << 8));
360 }
361
362 /* enable interrupt for this message object */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530363 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800364 IF_MCONT_TXIE | IF_MCONT_TXRQST | IF_MCONT_EOB |
365 frame->can_dlc);
366 c_can_object_put(dev, iface, objno, IF_COMM_ALL);
367}
368
369static inline void c_can_mark_rx_msg_obj(struct net_device *dev,
370 int iface, int ctrl_mask,
371 int obj)
372{
373 struct c_can_priv *priv = netdev_priv(dev);
374
AnilKumar Ch33f81002012-05-29 11:13:15 +0530375 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800376 ctrl_mask & ~(IF_MCONT_MSGLST | IF_MCONT_INTPND));
377 c_can_object_put(dev, iface, obj, IF_COMM_CONTROL);
378
379}
380
381static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
382 int iface,
383 int ctrl_mask)
384{
385 int i;
386 struct c_can_priv *priv = netdev_priv(dev);
387
388 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++) {
AnilKumar Ch33f81002012-05-29 11:13:15 +0530389 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800390 ctrl_mask & ~(IF_MCONT_MSGLST |
391 IF_MCONT_INTPND | IF_MCONT_NEWDAT));
392 c_can_object_put(dev, iface, i, IF_COMM_CONTROL);
393 }
394}
395
396static inline void c_can_activate_rx_msg_obj(struct net_device *dev,
397 int iface, int ctrl_mask,
398 int obj)
399{
400 struct c_can_priv *priv = netdev_priv(dev);
401
AnilKumar Ch33f81002012-05-29 11:13:15 +0530402 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800403 ctrl_mask & ~(IF_MCONT_MSGLST |
404 IF_MCONT_INTPND | IF_MCONT_NEWDAT));
405 c_can_object_put(dev, iface, obj, IF_COMM_CONTROL);
406}
407
408static void c_can_handle_lost_msg_obj(struct net_device *dev,
409 int iface, int objno)
410{
411 struct c_can_priv *priv = netdev_priv(dev);
412 struct net_device_stats *stats = &dev->stats;
413 struct sk_buff *skb;
414 struct can_frame *frame;
415
416 netdev_err(dev, "msg lost in buffer %d\n", objno);
417
418 c_can_object_get(dev, iface, objno, IF_COMM_ALL & ~IF_COMM_TXRQST);
419
AnilKumar Ch33f81002012-05-29 11:13:15 +0530420 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800421 IF_MCONT_CLR_MSGLST);
422
423 c_can_object_put(dev, 0, objno, IF_COMM_CONTROL);
424
425 /* create an error msg */
426 skb = alloc_can_err_skb(dev, &frame);
427 if (unlikely(!skb))
428 return;
429
430 frame->can_id |= CAN_ERR_CRTL;
431 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
432 stats->rx_errors++;
433 stats->rx_over_errors++;
434
435 netif_receive_skb(skb);
436}
437
438static int c_can_read_msg_object(struct net_device *dev, int iface, int ctrl)
439{
440 u16 flags, data;
441 int i;
442 unsigned int val;
443 struct c_can_priv *priv = netdev_priv(dev);
444 struct net_device_stats *stats = &dev->stats;
445 struct sk_buff *skb;
446 struct can_frame *frame;
447
448 skb = alloc_can_skb(dev, &frame);
449 if (!skb) {
450 stats->rx_dropped++;
451 return -ENOMEM;
452 }
453
454 frame->can_dlc = get_can_dlc(ctrl & 0x0F);
455
AnilKumar Ch33f81002012-05-29 11:13:15 +0530456 flags = priv->read_reg(priv, C_CAN_IFACE(ARB2_REG, iface));
457 val = priv->read_reg(priv, C_CAN_IFACE(ARB1_REG, iface)) |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800458 (flags << 16);
459
460 if (flags & IF_ARB_MSGXTD)
461 frame->can_id = (val & CAN_EFF_MASK) | CAN_EFF_FLAG;
462 else
463 frame->can_id = (val >> 18) & CAN_SFF_MASK;
464
465 if (flags & IF_ARB_TRANSMIT)
466 frame->can_id |= CAN_RTR_FLAG;
467 else {
468 for (i = 0; i < frame->can_dlc; i += 2) {
469 data = priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530470 C_CAN_IFACE(DATA1_REG, iface) + i / 2);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800471 frame->data[i] = data;
472 frame->data[i + 1] = data >> 8;
473 }
474 }
475
476 netif_receive_skb(skb);
477
478 stats->rx_packets++;
479 stats->rx_bytes += frame->can_dlc;
480
Fabio Baltieri5090f802012-12-18 18:51:01 +0100481 can_led_event(dev, CAN_LED_EVENT_RX);
482
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800483 return 0;
484}
485
486static void c_can_setup_receive_object(struct net_device *dev, int iface,
487 int objno, unsigned int mask,
488 unsigned int id, unsigned int mcont)
489{
490 struct c_can_priv *priv = netdev_priv(dev);
491
AnilKumar Ch33f81002012-05-29 11:13:15 +0530492 priv->write_reg(priv, C_CAN_IFACE(MASK1_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800493 IFX_WRITE_LOW_16BIT(mask));
Alexander Stein2bd3bc42012-12-13 10:06:10 +0100494
495 /* According to C_CAN documentation, the reserved bit
496 * in IFx_MASK2 register is fixed 1
497 */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530498 priv->write_reg(priv, C_CAN_IFACE(MASK2_REG, iface),
Alexander Stein2bd3bc42012-12-13 10:06:10 +0100499 IFX_WRITE_HIGH_16BIT(mask) | BIT(13));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800500
AnilKumar Ch33f81002012-05-29 11:13:15 +0530501 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800502 IFX_WRITE_LOW_16BIT(id));
AnilKumar Ch33f81002012-05-29 11:13:15 +0530503 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800504 (IF_ARB_MSGVAL | IFX_WRITE_HIGH_16BIT(id)));
505
AnilKumar Ch33f81002012-05-29 11:13:15 +0530506 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800507 c_can_object_put(dev, iface, objno, IF_COMM_ALL & ~IF_COMM_TXRQST);
508
509 netdev_dbg(dev, "obj no:%d, msgval:0x%08x\n", objno,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530510 c_can_read_reg32(priv, C_CAN_MSGVAL1_REG));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800511}
512
513static void c_can_inval_msg_object(struct net_device *dev, int iface, int objno)
514{
515 struct c_can_priv *priv = netdev_priv(dev);
516
AnilKumar Ch33f81002012-05-29 11:13:15 +0530517 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
518 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
519 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800520
521 c_can_object_put(dev, iface, objno, IF_COMM_ARB | IF_COMM_CONTROL);
522
523 netdev_dbg(dev, "obj no:%d, msgval:0x%08x\n", objno,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530524 c_can_read_reg32(priv, C_CAN_MSGVAL1_REG));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800525}
526
527static inline int c_can_is_next_tx_obj_busy(struct c_can_priv *priv, int objno)
528{
AnilKumar Ch33f81002012-05-29 11:13:15 +0530529 int val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800530
531 /*
532 * as transmission request register's bit n-1 corresponds to
533 * message object n, we need to handle the same properly.
534 */
535 if (val & (1 << (objno - 1)))
536 return 1;
537
538 return 0;
539}
540
541static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
542 struct net_device *dev)
543{
544 u32 msg_obj_no;
545 struct c_can_priv *priv = netdev_priv(dev);
546 struct can_frame *frame = (struct can_frame *)skb->data;
547
548 if (can_dropped_invalid_skb(dev, skb))
549 return NETDEV_TX_OK;
550
551 msg_obj_no = get_tx_next_msg_obj(priv);
552
553 /* prepare message object for transmission */
554 c_can_write_msg_object(dev, 0, frame, msg_obj_no);
555 can_put_echo_skb(skb, dev, msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
556
557 /*
558 * we have to stop the queue in case of a wrap around or
559 * if the next TX message object is still in use
560 */
561 priv->tx_next++;
562 if (c_can_is_next_tx_obj_busy(priv, get_tx_next_msg_obj(priv)) ||
563 (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) == 0)
564 netif_stop_queue(dev);
565
566 return NETDEV_TX_OK;
567}
568
569static int c_can_set_bittiming(struct net_device *dev)
570{
571 unsigned int reg_btr, reg_brpe, ctrl_save;
572 u8 brp, brpe, sjw, tseg1, tseg2;
573 u32 ten_bit_brp;
574 struct c_can_priv *priv = netdev_priv(dev);
575 const struct can_bittiming *bt = &priv->can.bittiming;
576
577 /* c_can provides a 6-bit brp and 4-bit brpe fields */
578 ten_bit_brp = bt->brp - 1;
579 brp = ten_bit_brp & BTR_BRP_MASK;
580 brpe = ten_bit_brp >> 6;
581
582 sjw = bt->sjw - 1;
583 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
584 tseg2 = bt->phase_seg2 - 1;
585 reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
586 (tseg2 << BTR_TSEG2_SHIFT);
587 reg_brpe = brpe & BRP_EXT_BRPE_MASK;
588
589 netdev_info(dev,
590 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
591
AnilKumar Ch33f81002012-05-29 11:13:15 +0530592 ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
593 priv->write_reg(priv, C_CAN_CTRL_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800594 ctrl_save | CONTROL_CCE | CONTROL_INIT);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530595 priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
596 priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
597 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800598
599 return 0;
600}
601
602/*
603 * Configure C_CAN message objects for Tx and Rx purposes:
604 * C_CAN provides a total of 32 message objects that can be configured
605 * either for Tx or Rx purposes. Here the first 16 message objects are used as
606 * a reception FIFO. The end of reception FIFO is signified by the EoB bit
607 * being SET. The remaining 16 message objects are kept aside for Tx purposes.
608 * See user guide document for further details on configuring message
609 * objects.
610 */
611static void c_can_configure_msg_objects(struct net_device *dev)
612{
613 int i;
614
615 /* first invalidate all message objects */
616 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
617 c_can_inval_msg_object(dev, 0, i);
618
619 /* setup receive message objects */
620 for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
621 c_can_setup_receive_object(dev, 0, i, 0, 0,
622 (IF_MCONT_RXIE | IF_MCONT_UMASK) & ~IF_MCONT_EOB);
623
624 c_can_setup_receive_object(dev, 0, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
625 IF_MCONT_EOB | IF_MCONT_RXIE | IF_MCONT_UMASK);
626}
627
628/*
629 * Configure C_CAN chip:
630 * - enable/disable auto-retransmission
631 * - set operating mode
632 * - configure message objects
633 */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100634static int c_can_chip_config(struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800635{
636 struct c_can_priv *priv = netdev_priv(dev);
637
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +0000638 /* enable automatic retransmission */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530639 priv->write_reg(priv, C_CAN_CTRL_REG,
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +0000640 CONTROL_ENABLE_AR);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800641
Dan Carpenterd9cb9bd2012-06-15 00:20:44 +0000642 if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
643 (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800644 /* loopback + silent mode : useful for hot self-test */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530645 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800646 CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530647 priv->write_reg(priv, C_CAN_TEST_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800648 TEST_LBACK | TEST_SILENT);
649 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
650 /* loopback mode : useful for self-test function */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530651 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800652 CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530653 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800654 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
655 /* silent mode : bus-monitoring mode */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530656 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800657 CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530658 priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800659 } else
660 /* normal mode*/
AnilKumar Ch33f81002012-05-29 11:13:15 +0530661 priv->write_reg(priv, C_CAN_CTRL_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800662 CONTROL_EIE | CONTROL_SIE | CONTROL_IE);
663
664 /* configure message objects */
665 c_can_configure_msg_objects(dev);
666
667 /* set a `lec` value so that we can check for updates later */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530668 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800669
670 /* set bittiming params */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100671 return c_can_set_bittiming(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800672}
673
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100674static int c_can_start(struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800675{
676 struct c_can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100677 int err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800678
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800679 /* basic c_can configuration */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100680 err = c_can_chip_config(dev);
681 if (err)
682 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800683
684 priv->can.state = CAN_STATE_ERROR_ACTIVE;
685
686 /* reset tx helper pointers */
687 priv->tx_next = priv->tx_echo = 0;
Jan Altenberg4f2d56c2011-03-21 18:19:26 -0700688
689 /* enable status change, error and module interrupts */
690 c_can_enable_all_interrupts(priv, ENABLE_ALL_INTERRUPTS);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100691
692 return 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800693}
694
695static void c_can_stop(struct net_device *dev)
696{
697 struct c_can_priv *priv = netdev_priv(dev);
698
699 /* disable all interrupts */
700 c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
701
702 /* set the state as STOPPED */
703 priv->can.state = CAN_STATE_STOPPED;
704}
705
706static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
707{
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100708 int err;
709
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800710 switch (mode) {
711 case CAN_MODE_START:
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100712 err = c_can_start(dev);
713 if (err)
714 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800715 netif_wake_queue(dev);
716 break;
717 default:
718 return -EOPNOTSUPP;
719 }
720
721 return 0;
722}
723
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100724static int __c_can_get_berr_counter(const struct net_device *dev,
725 struct can_berr_counter *bec)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800726{
727 unsigned int reg_err_counter;
728 struct c_can_priv *priv = netdev_priv(dev);
729
AnilKumar Ch33f81002012-05-29 11:13:15 +0530730 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800731 bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
732 ERR_CNT_REC_SHIFT;
733 bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
734
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100735 return 0;
736}
737
738static int c_can_get_berr_counter(const struct net_device *dev,
739 struct can_berr_counter *bec)
740{
741 struct c_can_priv *priv = netdev_priv(dev);
742 int err;
743
744 c_can_pm_runtime_get_sync(priv);
745 err = __c_can_get_berr_counter(dev, bec);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +0530746 c_can_pm_runtime_put_sync(priv);
747
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100748 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800749}
750
751/*
752 * theory of operation:
753 *
754 * priv->tx_echo holds the number of the oldest can_frame put for
755 * transmission into the hardware, but not yet ACKed by the CAN tx
756 * complete IRQ.
757 *
758 * We iterate from priv->tx_echo to priv->tx_next and check if the
759 * packet has been transmitted, echo it back to the CAN framework.
AnilKumar Ch617cacc2012-05-23 17:45:09 +0530760 * If we discover a not yet transmitted packet, stop looking for more.
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800761 */
762static void c_can_do_tx(struct net_device *dev)
763{
764 u32 val;
765 u32 msg_obj_no;
766 struct c_can_priv *priv = netdev_priv(dev);
767 struct net_device_stats *stats = &dev->stats;
768
769 for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
770 msg_obj_no = get_tx_echo_msg_obj(priv);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530771 val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
AnilKumar Ch617cacc2012-05-23 17:45:09 +0530772 if (!(val & (1 << (msg_obj_no - 1)))) {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800773 can_get_echo_skb(dev,
774 msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
Holger Bechtold7ee330c2013-11-25 15:48:19 +0100775 c_can_object_get(dev, 0, msg_obj_no, IF_COMM_ALL);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800776 stats->tx_bytes += priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530777 C_CAN_IFACE(MSGCTRL_REG, 0))
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800778 & IF_MCONT_DLC_MASK;
779 stats->tx_packets++;
Fabio Baltieri5090f802012-12-18 18:51:01 +0100780 can_led_event(dev, CAN_LED_EVENT_TX);
Jan Altenbergdc760b32011-03-27 18:24:10 -0700781 c_can_inval_msg_object(dev, 0, msg_obj_no);
AnilKumar Ch617cacc2012-05-23 17:45:09 +0530782 } else {
783 break;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800784 }
785 }
786
787 /* restart queue if wrap-up or if queue stalled on last pkt */
788 if (((priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) != 0) ||
789 ((priv->tx_echo & C_CAN_NEXT_MSG_OBJ_MASK) == 0))
790 netif_wake_queue(dev);
791}
792
793/*
794 * theory of operation:
795 *
796 * c_can core saves a received CAN message into the first free message
797 * object it finds free (starting with the lowest). Bits NEWDAT and
798 * INTPND are set for this message object indicating that a new message
799 * has arrived. To work-around this issue, we keep two groups of message
800 * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
801 *
802 * To ensure in-order frame reception we use the following
803 * approach while re-activating a message object to receive further
804 * frames:
805 * - if the current message object number is lower than
806 * C_CAN_MSG_RX_LOW_LAST, do not clear the NEWDAT bit while clearing
807 * the INTPND bit.
808 * - if the current message object number is equal to
809 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of all lower
810 * receive message objects.
811 * - if the current message object number is greater than
812 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of
813 * only this message object.
814 */
815static int c_can_do_rx_poll(struct net_device *dev, int quota)
816{
817 u32 num_rx_pkts = 0;
818 unsigned int msg_obj, msg_ctrl_save;
819 struct c_can_priv *priv = netdev_priv(dev);
Markus Pargmann4ce78a82013-11-01 10:36:36 +0100820 u16 val;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800821
Markus Pargmann4ce78a82013-11-01 10:36:36 +0100822 /*
823 * It is faster to read only one 16bit register. This is only possible
824 * for a maximum number of 16 objects.
825 */
826 BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST > 16,
827 "Implementation does not support more message objects than 16");
828
829 while (quota > 0 && (val = priv->read_reg(priv, C_CAN_INTPND1_REG))) {
830 while ((msg_obj = ffs(val)) && quota > 0) {
831 val &= ~BIT(msg_obj - 1);
832
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800833 c_can_object_get(dev, 0, msg_obj, IF_COMM_ALL &
834 ~IF_COMM_TXRQST);
835 msg_ctrl_save = priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530836 C_CAN_IFACE(MSGCTRL_REG, 0));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800837
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800838 if (msg_ctrl_save & IF_MCONT_MSGLST) {
839 c_can_handle_lost_msg_obj(dev, 0, msg_obj);
840 num_rx_pkts++;
841 quota--;
842 continue;
843 }
844
Markus Pargmann5d0f8012013-10-28 09:54:40 +0100845 if (msg_ctrl_save & IF_MCONT_EOB)
846 return num_rx_pkts;
847
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800848 if (!(msg_ctrl_save & IF_MCONT_NEWDAT))
849 continue;
850
851 /* read the data from the message object */
852 c_can_read_msg_object(dev, 0, msg_ctrl_save);
853
854 if (msg_obj < C_CAN_MSG_RX_LOW_LAST)
855 c_can_mark_rx_msg_obj(dev, 0,
856 msg_ctrl_save, msg_obj);
857 else if (msg_obj > C_CAN_MSG_RX_LOW_LAST)
858 /* activate this msg obj */
859 c_can_activate_rx_msg_obj(dev, 0,
860 msg_ctrl_save, msg_obj);
861 else if (msg_obj == C_CAN_MSG_RX_LOW_LAST)
862 /* activate all lower message objects */
863 c_can_activate_all_lower_rx_msg_obj(dev,
864 0, msg_ctrl_save);
865
866 num_rx_pkts++;
867 quota--;
868 }
869 }
870
871 return num_rx_pkts;
872}
873
874static inline int c_can_has_and_handle_berr(struct c_can_priv *priv)
875{
876 return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
877 (priv->current_status & LEC_UNUSED);
878}
879
880static int c_can_handle_state_change(struct net_device *dev,
881 enum c_can_bus_error_types error_type)
882{
883 unsigned int reg_err_counter;
884 unsigned int rx_err_passive;
885 struct c_can_priv *priv = netdev_priv(dev);
886 struct net_device_stats *stats = &dev->stats;
887 struct can_frame *cf;
888 struct sk_buff *skb;
889 struct can_berr_counter bec;
890
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300891 /* propagate the error condition to the CAN stack */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800892 skb = alloc_can_err_skb(dev, &cf);
893 if (unlikely(!skb))
894 return 0;
895
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100896 __c_can_get_berr_counter(dev, &bec);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530897 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800898 rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
899 ERR_CNT_RP_SHIFT;
900
901 switch (error_type) {
902 case C_CAN_ERROR_WARNING:
903 /* error warning state */
904 priv->can.can_stats.error_warning++;
905 priv->can.state = CAN_STATE_ERROR_WARNING;
906 cf->can_id |= CAN_ERR_CRTL;
907 cf->data[1] = (bec.txerr > bec.rxerr) ?
908 CAN_ERR_CRTL_TX_WARNING :
909 CAN_ERR_CRTL_RX_WARNING;
910 cf->data[6] = bec.txerr;
911 cf->data[7] = bec.rxerr;
912
913 break;
914 case C_CAN_ERROR_PASSIVE:
915 /* error passive state */
916 priv->can.can_stats.error_passive++;
917 priv->can.state = CAN_STATE_ERROR_PASSIVE;
918 cf->can_id |= CAN_ERR_CRTL;
919 if (rx_err_passive)
920 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
921 if (bec.txerr > 127)
922 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
923
924 cf->data[6] = bec.txerr;
925 cf->data[7] = bec.rxerr;
926 break;
927 case C_CAN_BUS_OFF:
928 /* bus-off state */
929 priv->can.state = CAN_STATE_BUS_OFF;
930 cf->can_id |= CAN_ERR_BUSOFF;
931 /*
932 * disable all interrupts in bus-off mode to ensure that
933 * the CPU is not hogged down
934 */
935 c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
936 can_bus_off(dev);
937 break;
938 default:
939 break;
940 }
941
942 netif_receive_skb(skb);
943 stats->rx_packets++;
944 stats->rx_bytes += cf->can_dlc;
945
946 return 1;
947}
948
949static int c_can_handle_bus_err(struct net_device *dev,
950 enum c_can_lec_type lec_type)
951{
952 struct c_can_priv *priv = netdev_priv(dev);
953 struct net_device_stats *stats = &dev->stats;
954 struct can_frame *cf;
955 struct sk_buff *skb;
956
957 /*
958 * early exit if no lec update or no error.
959 * no lec update means that no CAN bus event has been detected
960 * since CPU wrote 0x7 value to status reg.
961 */
962 if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
963 return 0;
964
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300965 /* propagate the error condition to the CAN stack */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800966 skb = alloc_can_err_skb(dev, &cf);
967 if (unlikely(!skb))
968 return 0;
969
970 /*
971 * check for 'last error code' which tells us the
972 * type of the last error to occur on the CAN bus
973 */
974
975 /* common for all type of bus errors */
976 priv->can.can_stats.bus_error++;
977 stats->rx_errors++;
978 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
979 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
980
981 switch (lec_type) {
982 case LEC_STUFF_ERROR:
983 netdev_dbg(dev, "stuff error\n");
984 cf->data[2] |= CAN_ERR_PROT_STUFF;
985 break;
986 case LEC_FORM_ERROR:
987 netdev_dbg(dev, "form error\n");
988 cf->data[2] |= CAN_ERR_PROT_FORM;
989 break;
990 case LEC_ACK_ERROR:
991 netdev_dbg(dev, "ack error\n");
Olivier Sobrie6ea45882013-01-18 09:32:39 +0100992 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800993 CAN_ERR_PROT_LOC_ACK_DEL);
994 break;
995 case LEC_BIT1_ERROR:
996 netdev_dbg(dev, "bit1 error\n");
997 cf->data[2] |= CAN_ERR_PROT_BIT1;
998 break;
999 case LEC_BIT0_ERROR:
1000 netdev_dbg(dev, "bit0 error\n");
1001 cf->data[2] |= CAN_ERR_PROT_BIT0;
1002 break;
1003 case LEC_CRC_ERROR:
1004 netdev_dbg(dev, "CRC error\n");
Olivier Sobrie6ea45882013-01-18 09:32:39 +01001005 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001006 CAN_ERR_PROT_LOC_CRC_DEL);
1007 break;
1008 default:
1009 break;
1010 }
1011
1012 /* set a `lec` value so that we can check for updates later */
AnilKumar Ch33f81002012-05-29 11:13:15 +05301013 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001014
1015 netif_receive_skb(skb);
1016 stats->rx_packets++;
1017 stats->rx_bytes += cf->can_dlc;
1018
1019 return 1;
1020}
1021
1022static int c_can_poll(struct napi_struct *napi, int quota)
1023{
1024 u16 irqstatus;
1025 int lec_type = 0;
1026 int work_done = 0;
1027 struct net_device *dev = napi->dev;
1028 struct c_can_priv *priv = netdev_priv(dev);
1029
AnilKumar Ch148c87c2012-05-23 17:45:10 +05301030 irqstatus = priv->irqstatus;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001031 if (!irqstatus)
1032 goto end;
1033
1034 /* status events have the highest priority */
1035 if (irqstatus == STATUS_INTERRUPT) {
1036 priv->current_status = priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +05301037 C_CAN_STS_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001038
1039 /* handle Tx/Rx events */
1040 if (priv->current_status & STATUS_TXOK)
AnilKumar Ch33f81002012-05-29 11:13:15 +05301041 priv->write_reg(priv, C_CAN_STS_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001042 priv->current_status & ~STATUS_TXOK);
1043
1044 if (priv->current_status & STATUS_RXOK)
AnilKumar Ch33f81002012-05-29 11:13:15 +05301045 priv->write_reg(priv, C_CAN_STS_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001046 priv->current_status & ~STATUS_RXOK);
1047
1048 /* handle state changes */
1049 if ((priv->current_status & STATUS_EWARN) &&
1050 (!(priv->last_status & STATUS_EWARN))) {
1051 netdev_dbg(dev, "entered error warning state\n");
1052 work_done += c_can_handle_state_change(dev,
1053 C_CAN_ERROR_WARNING);
1054 }
1055 if ((priv->current_status & STATUS_EPASS) &&
1056 (!(priv->last_status & STATUS_EPASS))) {
1057 netdev_dbg(dev, "entered error passive state\n");
1058 work_done += c_can_handle_state_change(dev,
1059 C_CAN_ERROR_PASSIVE);
1060 }
1061 if ((priv->current_status & STATUS_BOFF) &&
1062 (!(priv->last_status & STATUS_BOFF))) {
1063 netdev_dbg(dev, "entered bus off state\n");
1064 work_done += c_can_handle_state_change(dev,
1065 C_CAN_BUS_OFF);
1066 }
1067
1068 /* handle bus recovery events */
1069 if ((!(priv->current_status & STATUS_BOFF)) &&
1070 (priv->last_status & STATUS_BOFF)) {
1071 netdev_dbg(dev, "left bus off state\n");
1072 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1073 }
1074 if ((!(priv->current_status & STATUS_EPASS)) &&
1075 (priv->last_status & STATUS_EPASS)) {
1076 netdev_dbg(dev, "left error passive state\n");
1077 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1078 }
1079
1080 priv->last_status = priv->current_status;
1081
1082 /* handle lec errors on the bus */
1083 lec_type = c_can_has_and_handle_berr(priv);
1084 if (lec_type)
1085 work_done += c_can_handle_bus_err(dev, lec_type);
1086 } else if ((irqstatus >= C_CAN_MSG_OBJ_RX_FIRST) &&
1087 (irqstatus <= C_CAN_MSG_OBJ_RX_LAST)) {
1088 /* handle events corresponding to receive message objects */
1089 work_done += c_can_do_rx_poll(dev, (quota - work_done));
1090 } else if ((irqstatus >= C_CAN_MSG_OBJ_TX_FIRST) &&
1091 (irqstatus <= C_CAN_MSG_OBJ_TX_LAST)) {
1092 /* handle events corresponding to transmit message objects */
1093 c_can_do_tx(dev);
1094 }
1095
1096end:
1097 if (work_done < quota) {
1098 napi_complete(napi);
1099 /* enable all IRQs */
1100 c_can_enable_all_interrupts(priv, ENABLE_ALL_INTERRUPTS);
1101 }
1102
1103 return work_done;
1104}
1105
1106static irqreturn_t c_can_isr(int irq, void *dev_id)
1107{
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001108 struct net_device *dev = (struct net_device *)dev_id;
1109 struct c_can_priv *priv = netdev_priv(dev);
1110
AnilKumar Ch33f81002012-05-29 11:13:15 +05301111 priv->irqstatus = priv->read_reg(priv, C_CAN_INT_REG);
AnilKumar Ch148c87c2012-05-23 17:45:10 +05301112 if (!priv->irqstatus)
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001113 return IRQ_NONE;
1114
1115 /* disable all interrupts and schedule the NAPI */
1116 c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
1117 napi_schedule(&priv->napi);
1118
1119 return IRQ_HANDLED;
1120}
1121
1122static int c_can_open(struct net_device *dev)
1123{
1124 int err;
1125 struct c_can_priv *priv = netdev_priv(dev);
1126
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301127 c_can_pm_runtime_get_sync(priv);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301128 c_can_reset_ram(priv, true);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301129
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001130 /* open the can device */
1131 err = open_candev(dev);
1132 if (err) {
1133 netdev_err(dev, "failed to open can device\n");
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301134 goto exit_open_fail;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001135 }
1136
1137 /* register interrupt handler */
1138 err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
1139 dev);
1140 if (err < 0) {
1141 netdev_err(dev, "failed to request interrupt\n");
1142 goto exit_irq_fail;
1143 }
1144
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001145 /* start the c_can controller */
1146 err = c_can_start(dev);
1147 if (err)
1148 goto exit_start_fail;
AnilKumar Chf461f272012-05-23 17:45:11 +05301149
Fabio Baltieri5090f802012-12-18 18:51:01 +01001150 can_led_event(dev, CAN_LED_EVENT_OPEN);
1151
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001152 napi_enable(&priv->napi);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001153 netif_start_queue(dev);
1154
1155 return 0;
1156
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001157exit_start_fail:
1158 free_irq(dev->irq, dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001159exit_irq_fail:
1160 close_candev(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301161exit_open_fail:
AnilKumar Ch52cde852012-11-21 11:14:10 +05301162 c_can_reset_ram(priv, false);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301163 c_can_pm_runtime_put_sync(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001164 return err;
1165}
1166
1167static int c_can_close(struct net_device *dev)
1168{
1169 struct c_can_priv *priv = netdev_priv(dev);
1170
1171 netif_stop_queue(dev);
1172 napi_disable(&priv->napi);
1173 c_can_stop(dev);
1174 free_irq(dev->irq, dev);
1175 close_candev(dev);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301176
1177 c_can_reset_ram(priv, false);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301178 c_can_pm_runtime_put_sync(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001179
Fabio Baltieri5090f802012-12-18 18:51:01 +01001180 can_led_event(dev, CAN_LED_EVENT_STOP);
1181
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001182 return 0;
1183}
1184
1185struct net_device *alloc_c_can_dev(void)
1186{
1187 struct net_device *dev;
1188 struct c_can_priv *priv;
1189
1190 dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM);
1191 if (!dev)
1192 return NULL;
1193
1194 priv = netdev_priv(dev);
1195 netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
1196
1197 priv->dev = dev;
1198 priv->can.bittiming_const = &c_can_bittiming_const;
1199 priv->can.do_set_mode = c_can_set_mode;
1200 priv->can.do_get_berr_counter = c_can_get_berr_counter;
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +00001201 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001202 CAN_CTRLMODE_LISTENONLY |
1203 CAN_CTRLMODE_BERR_REPORTING;
1204
1205 return dev;
1206}
1207EXPORT_SYMBOL_GPL(alloc_c_can_dev);
1208
AnilKumar Ch82120032012-09-21 15:29:01 +05301209#ifdef CONFIG_PM
1210int c_can_power_down(struct net_device *dev)
1211{
1212 u32 val;
1213 unsigned long time_out;
1214 struct c_can_priv *priv = netdev_priv(dev);
1215
1216 if (!(dev->flags & IFF_UP))
1217 return 0;
1218
1219 WARN_ON(priv->type != BOSCH_D_CAN);
1220
1221 /* set PDR value so the device goes to power down mode */
1222 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1223 val |= CONTROL_EX_PDR;
1224 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1225
1226 /* Wait for the PDA bit to get set */
1227 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1228 while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1229 time_after(time_out, jiffies))
1230 cpu_relax();
1231
1232 if (time_after(jiffies, time_out))
1233 return -ETIMEDOUT;
1234
1235 c_can_stop(dev);
1236
AnilKumar Ch52cde852012-11-21 11:14:10 +05301237 c_can_reset_ram(priv, false);
AnilKumar Ch82120032012-09-21 15:29:01 +05301238 c_can_pm_runtime_put_sync(priv);
1239
1240 return 0;
1241}
1242EXPORT_SYMBOL_GPL(c_can_power_down);
1243
1244int c_can_power_up(struct net_device *dev)
1245{
1246 u32 val;
1247 unsigned long time_out;
1248 struct c_can_priv *priv = netdev_priv(dev);
1249
1250 if (!(dev->flags & IFF_UP))
1251 return 0;
1252
1253 WARN_ON(priv->type != BOSCH_D_CAN);
1254
1255 c_can_pm_runtime_get_sync(priv);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301256 c_can_reset_ram(priv, true);
AnilKumar Ch82120032012-09-21 15:29:01 +05301257
1258 /* Clear PDR and INIT bits */
1259 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1260 val &= ~CONTROL_EX_PDR;
1261 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1262 val = priv->read_reg(priv, C_CAN_CTRL_REG);
1263 val &= ~CONTROL_INIT;
1264 priv->write_reg(priv, C_CAN_CTRL_REG, val);
1265
1266 /* Wait for the PDA bit to get clear */
1267 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1268 while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1269 time_after(time_out, jiffies))
1270 cpu_relax();
1271
1272 if (time_after(jiffies, time_out))
1273 return -ETIMEDOUT;
1274
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001275 return c_can_start(dev);
AnilKumar Ch82120032012-09-21 15:29:01 +05301276}
1277EXPORT_SYMBOL_GPL(c_can_power_up);
1278#endif
1279
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001280void free_c_can_dev(struct net_device *dev)
1281{
Marc Kleine-Buddef29b4232014-03-18 19:13:59 +01001282 struct c_can_priv *priv = netdev_priv(dev);
1283
1284 netif_napi_del(&priv->napi);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001285 free_candev(dev);
1286}
1287EXPORT_SYMBOL_GPL(free_c_can_dev);
1288
1289static const struct net_device_ops c_can_netdev_ops = {
1290 .ndo_open = c_can_open,
1291 .ndo_stop = c_can_close,
1292 .ndo_start_xmit = c_can_start_xmit,
1293};
1294
1295int register_c_can_dev(struct net_device *dev)
1296{
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301297 struct c_can_priv *priv = netdev_priv(dev);
1298 int err;
1299
1300 c_can_pm_runtime_enable(priv);
1301
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001302 dev->flags |= IFF_ECHO; /* we support local echo */
1303 dev->netdev_ops = &c_can_netdev_ops;
1304
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301305 err = register_candev(dev);
1306 if (err)
1307 c_can_pm_runtime_disable(priv);
Fabio Baltieri5090f802012-12-18 18:51:01 +01001308 else
1309 devm_can_led_init(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301310
1311 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001312}
1313EXPORT_SYMBOL_GPL(register_c_can_dev);
1314
1315void unregister_c_can_dev(struct net_device *dev)
1316{
1317 struct c_can_priv *priv = netdev_priv(dev);
1318
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001319 unregister_candev(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301320
1321 c_can_pm_runtime_disable(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001322}
1323EXPORT_SYMBOL_GPL(unregister_c_can_dev);
1324
1325MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1326MODULE_LICENSE("GPL v2");
1327MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");