blob: ef5f3b8099f4e0b042fedd7d74da2a0ac058cd64 [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)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800125#define IF_MCONT_INTPND BIT(13)
126#define IF_MCONT_UMASK BIT(12)
127#define IF_MCONT_TXIE BIT(11)
128#define IF_MCONT_RXIE BIT(10)
129#define IF_MCONT_RMTEN BIT(9)
130#define IF_MCONT_TXRQST BIT(8)
131#define IF_MCONT_EOB BIT(7)
132#define IF_MCONT_DLC_MASK 0xf
133
134/*
Thomas Gleixner640916d2014-03-18 17:19:09 +0000135 * Use IF1 for RX and IF2 for TX
136 */
137#define IF_RX 0
138#define IF_TX 1
139
140/*
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800141 * IFx register masks:
142 * allow easy operation on 16-bit registers when the
143 * argument is 32-bit instead
144 */
145#define IFX_WRITE_LOW_16BIT(x) ((x) & 0xFFFF)
146#define IFX_WRITE_HIGH_16BIT(x) (((x) & 0xFFFF0000) >> 16)
147
148/* message object split */
149#define C_CAN_NO_OF_OBJECTS 32
150#define C_CAN_MSG_OBJ_RX_NUM 16
151#define C_CAN_MSG_OBJ_TX_NUM 16
152
153#define C_CAN_MSG_OBJ_RX_FIRST 1
154#define C_CAN_MSG_OBJ_RX_LAST (C_CAN_MSG_OBJ_RX_FIRST + \
155 C_CAN_MSG_OBJ_RX_NUM - 1)
156
157#define C_CAN_MSG_OBJ_TX_FIRST (C_CAN_MSG_OBJ_RX_LAST + 1)
158#define C_CAN_MSG_OBJ_TX_LAST (C_CAN_MSG_OBJ_TX_FIRST + \
159 C_CAN_MSG_OBJ_TX_NUM - 1)
160
161#define C_CAN_MSG_OBJ_RX_SPLIT 9
162#define C_CAN_MSG_RX_LOW_LAST (C_CAN_MSG_OBJ_RX_SPLIT - 1)
163
164#define C_CAN_NEXT_MSG_OBJ_MASK (C_CAN_MSG_OBJ_TX_NUM - 1)
165#define RECEIVE_OBJECT_BITS 0x0000ffff
166
167/* status interrupt */
168#define STATUS_INTERRUPT 0x8000
169
170/* global interrupt masks */
171#define ENABLE_ALL_INTERRUPTS 1
172#define DISABLE_ALL_INTERRUPTS 0
173
174/* minimum timeout for checking BUSY status */
175#define MIN_TIMEOUT_VALUE 6
176
AnilKumar Ch82120032012-09-21 15:29:01 +0530177/* Wait for ~1 sec for INIT bit */
178#define INIT_WAIT_MS 1000
179
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800180/* napi related */
181#define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
182
183/* c_can lec values */
184enum c_can_lec_type {
185 LEC_NO_ERROR = 0,
186 LEC_STUFF_ERROR,
187 LEC_FORM_ERROR,
188 LEC_ACK_ERROR,
189 LEC_BIT1_ERROR,
190 LEC_BIT0_ERROR,
191 LEC_CRC_ERROR,
192 LEC_UNUSED,
193};
194
195/*
196 * c_can error types:
197 * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
198 */
199enum c_can_bus_error_types {
200 C_CAN_NO_ERROR = 0,
201 C_CAN_BUS_OFF,
202 C_CAN_ERROR_WARNING,
203 C_CAN_ERROR_PASSIVE,
204};
205
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200206static const struct can_bittiming_const c_can_bittiming_const = {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800207 .name = KBUILD_MODNAME,
208 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
209 .tseg1_max = 16,
210 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
211 .tseg2_max = 8,
212 .sjw_max = 4,
213 .brp_min = 1,
214 .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/
215 .brp_inc = 1,
216};
217
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +0530218static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
219{
220 if (priv->device)
221 pm_runtime_enable(priv->device);
222}
223
224static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
225{
226 if (priv->device)
227 pm_runtime_disable(priv->device);
228}
229
230static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
231{
232 if (priv->device)
233 pm_runtime_get_sync(priv->device);
234}
235
236static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
237{
238 if (priv->device)
239 pm_runtime_put_sync(priv->device);
240}
241
AnilKumar Ch52cde852012-11-21 11:14:10 +0530242static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
243{
244 if (priv->raminit)
245 priv->raminit(priv, enable);
246}
247
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800248static inline int get_tx_next_msg_obj(const struct c_can_priv *priv)
249{
250 return (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) +
251 C_CAN_MSG_OBJ_TX_FIRST;
252}
253
254static inline int get_tx_echo_msg_obj(const struct c_can_priv *priv)
255{
256 return (priv->tx_echo & C_CAN_NEXT_MSG_OBJ_MASK) +
257 C_CAN_MSG_OBJ_TX_FIRST;
258}
259
AnilKumar Ch33f81002012-05-29 11:13:15 +0530260static u32 c_can_read_reg32(struct c_can_priv *priv, enum reg index)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800261{
AnilKumar Ch33f81002012-05-29 11:13:15 +0530262 u32 val = priv->read_reg(priv, index);
263 val |= ((u32) priv->read_reg(priv, index + 1)) << 16;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800264 return val;
265}
266
267static void c_can_enable_all_interrupts(struct c_can_priv *priv,
268 int enable)
269{
270 unsigned int cntrl_save = priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530271 C_CAN_CTRL_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800272
273 if (enable)
274 cntrl_save |= (CONTROL_SIE | CONTROL_EIE | CONTROL_IE);
275 else
276 cntrl_save &= ~(CONTROL_EIE | CONTROL_IE | CONTROL_SIE);
277
AnilKumar Ch33f81002012-05-29 11:13:15 +0530278 priv->write_reg(priv, C_CAN_CTRL_REG, cntrl_save);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800279}
280
281static inline int c_can_msg_obj_is_busy(struct c_can_priv *priv, int iface)
282{
283 int count = MIN_TIMEOUT_VALUE;
284
285 while (count && priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530286 C_CAN_IFACE(COMREQ_REG, iface)) &
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800287 IF_COMR_BUSY) {
288 count--;
289 udelay(1);
290 }
291
292 if (!count)
293 return 1;
294
295 return 0;
296}
297
298static inline void c_can_object_get(struct net_device *dev,
299 int iface, int objno, int mask)
300{
301 struct c_can_priv *priv = netdev_priv(dev);
302
303 /*
304 * As per specs, after writting the message object number in the
305 * IF command request register the transfer b/w interface
306 * register and message RAM must be complete in 6 CAN-CLK
307 * period.
308 */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530309 priv->write_reg(priv, C_CAN_IFACE(COMMSK_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800310 IFX_WRITE_LOW_16BIT(mask));
AnilKumar Ch33f81002012-05-29 11:13:15 +0530311 priv->write_reg(priv, C_CAN_IFACE(COMREQ_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800312 IFX_WRITE_LOW_16BIT(objno));
313
314 if (c_can_msg_obj_is_busy(priv, iface))
315 netdev_err(dev, "timed out in object get\n");
316}
317
318static inline void c_can_object_put(struct net_device *dev,
319 int iface, int objno, int mask)
320{
321 struct c_can_priv *priv = netdev_priv(dev);
322
323 /*
324 * As per specs, after writting the message object number in the
325 * IF command request register the transfer b/w interface
326 * register and message RAM must be complete in 6 CAN-CLK
327 * period.
328 */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530329 priv->write_reg(priv, C_CAN_IFACE(COMMSK_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800330 (IF_COMM_WR | IFX_WRITE_LOW_16BIT(mask)));
AnilKumar Ch33f81002012-05-29 11:13:15 +0530331 priv->write_reg(priv, C_CAN_IFACE(COMREQ_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800332 IFX_WRITE_LOW_16BIT(objno));
333
334 if (c_can_msg_obj_is_busy(priv, iface))
335 netdev_err(dev, "timed out in object put\n");
336}
337
338static void c_can_write_msg_object(struct net_device *dev,
339 int iface, struct can_frame *frame, int objno)
340{
341 int i;
342 u16 flags = 0;
343 unsigned int id;
344 struct c_can_priv *priv = netdev_priv(dev);
345
346 if (!(frame->can_id & CAN_RTR_FLAG))
347 flags |= IF_ARB_TRANSMIT;
348
349 if (frame->can_id & CAN_EFF_FLAG) {
350 id = frame->can_id & CAN_EFF_MASK;
351 flags |= IF_ARB_MSGXTD;
352 } else
353 id = ((frame->can_id & CAN_SFF_MASK) << 18);
354
355 flags |= IF_ARB_MSGVAL;
356
AnilKumar Ch33f81002012-05-29 11:13:15 +0530357 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800358 IFX_WRITE_LOW_16BIT(id));
AnilKumar Ch33f81002012-05-29 11:13:15 +0530359 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), flags |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800360 IFX_WRITE_HIGH_16BIT(id));
361
362 for (i = 0; i < frame->can_dlc; i += 2) {
AnilKumar Ch33f81002012-05-29 11:13:15 +0530363 priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800364 frame->data[i] | (frame->data[i + 1] << 8));
365 }
366
367 /* enable interrupt for this message object */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530368 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800369 IF_MCONT_TXIE | IF_MCONT_TXRQST | IF_MCONT_EOB |
370 frame->can_dlc);
371 c_can_object_put(dev, iface, objno, IF_COMM_ALL);
372}
373
374static inline void c_can_mark_rx_msg_obj(struct net_device *dev,
375 int iface, int ctrl_mask,
376 int obj)
377{
378 struct c_can_priv *priv = netdev_priv(dev);
379
AnilKumar Ch33f81002012-05-29 11:13:15 +0530380 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800381 ctrl_mask & ~(IF_MCONT_MSGLST | IF_MCONT_INTPND));
382 c_can_object_put(dev, iface, obj, IF_COMM_CONTROL);
383
384}
385
386static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
387 int iface,
388 int ctrl_mask)
389{
390 int i;
391 struct c_can_priv *priv = netdev_priv(dev);
392
393 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++) {
AnilKumar Ch33f81002012-05-29 11:13:15 +0530394 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800395 ctrl_mask & ~(IF_MCONT_MSGLST |
396 IF_MCONT_INTPND | IF_MCONT_NEWDAT));
397 c_can_object_put(dev, iface, i, IF_COMM_CONTROL);
398 }
399}
400
401static inline void c_can_activate_rx_msg_obj(struct net_device *dev,
402 int iface, int ctrl_mask,
403 int obj)
404{
405 struct c_can_priv *priv = netdev_priv(dev);
406
AnilKumar Ch33f81002012-05-29 11:13:15 +0530407 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800408 ctrl_mask & ~(IF_MCONT_MSGLST |
409 IF_MCONT_INTPND | IF_MCONT_NEWDAT));
410 c_can_object_put(dev, iface, obj, IF_COMM_CONTROL);
411}
412
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000413static int c_can_handle_lost_msg_obj(struct net_device *dev,
414 int iface, int objno, u32 ctrl)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800415{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800416 struct net_device_stats *stats = &dev->stats;
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000417 struct c_can_priv *priv = netdev_priv(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800418 struct can_frame *frame;
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000419 struct sk_buff *skb;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800420
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000421 ctrl &= ~(IF_MCONT_MSGLST | IF_MCONT_INTPND | IF_MCONT_NEWDAT);
422 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
Thomas Gleixner640916d2014-03-18 17:19:09 +0000423 c_can_object_put(dev, iface, objno, IF_COMM_CONTROL);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800424
425 /* create an error msg */
426 skb = alloc_can_err_skb(dev, &frame);
427 if (unlikely(!skb))
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000428 return 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800429
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);
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000436 return 1;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800437}
438
439static int c_can_read_msg_object(struct net_device *dev, int iface, int ctrl)
440{
441 u16 flags, data;
442 int i;
443 unsigned int val;
444 struct c_can_priv *priv = netdev_priv(dev);
445 struct net_device_stats *stats = &dev->stats;
446 struct sk_buff *skb;
447 struct can_frame *frame;
448
449 skb = alloc_can_skb(dev, &frame);
450 if (!skb) {
451 stats->rx_dropped++;
452 return -ENOMEM;
453 }
454
455 frame->can_dlc = get_can_dlc(ctrl & 0x0F);
456
AnilKumar Ch33f81002012-05-29 11:13:15 +0530457 flags = priv->read_reg(priv, C_CAN_IFACE(ARB2_REG, iface));
458 val = priv->read_reg(priv, C_CAN_IFACE(ARB1_REG, iface)) |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800459 (flags << 16);
460
461 if (flags & IF_ARB_MSGXTD)
462 frame->can_id = (val & CAN_EFF_MASK) | CAN_EFF_FLAG;
463 else
464 frame->can_id = (val >> 18) & CAN_SFF_MASK;
465
466 if (flags & IF_ARB_TRANSMIT)
467 frame->can_id |= CAN_RTR_FLAG;
468 else {
469 for (i = 0; i < frame->can_dlc; i += 2) {
470 data = priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530471 C_CAN_IFACE(DATA1_REG, iface) + i / 2);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800472 frame->data[i] = data;
473 frame->data[i + 1] = data >> 8;
474 }
475 }
476
477 netif_receive_skb(skb);
478
479 stats->rx_packets++;
480 stats->rx_bytes += frame->can_dlc;
481
Fabio Baltieri5090f802012-12-18 18:51:01 +0100482 can_led_event(dev, CAN_LED_EVENT_RX);
483
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800484 return 0;
485}
486
487static void c_can_setup_receive_object(struct net_device *dev, int iface,
488 int objno, unsigned int mask,
489 unsigned int id, unsigned int mcont)
490{
491 struct c_can_priv *priv = netdev_priv(dev);
492
AnilKumar Ch33f81002012-05-29 11:13:15 +0530493 priv->write_reg(priv, C_CAN_IFACE(MASK1_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800494 IFX_WRITE_LOW_16BIT(mask));
Alexander Stein2bd3bc42012-12-13 10:06:10 +0100495
496 /* According to C_CAN documentation, the reserved bit
497 * in IFx_MASK2 register is fixed 1
498 */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530499 priv->write_reg(priv, C_CAN_IFACE(MASK2_REG, iface),
Alexander Stein2bd3bc42012-12-13 10:06:10 +0100500 IFX_WRITE_HIGH_16BIT(mask) | BIT(13));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800501
AnilKumar Ch33f81002012-05-29 11:13:15 +0530502 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800503 IFX_WRITE_LOW_16BIT(id));
AnilKumar Ch33f81002012-05-29 11:13:15 +0530504 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface),
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800505 (IF_ARB_MSGVAL | IFX_WRITE_HIGH_16BIT(id)));
506
AnilKumar Ch33f81002012-05-29 11:13:15 +0530507 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800508 c_can_object_put(dev, iface, objno, IF_COMM_ALL & ~IF_COMM_TXRQST);
509
510 netdev_dbg(dev, "obj no:%d, msgval:0x%08x\n", objno,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530511 c_can_read_reg32(priv, C_CAN_MSGVAL1_REG));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800512}
513
514static void c_can_inval_msg_object(struct net_device *dev, int iface, int objno)
515{
516 struct c_can_priv *priv = netdev_priv(dev);
517
AnilKumar Ch33f81002012-05-29 11:13:15 +0530518 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
519 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
520 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800521
522 c_can_object_put(dev, iface, objno, IF_COMM_ARB | IF_COMM_CONTROL);
523
524 netdev_dbg(dev, "obj no:%d, msgval:0x%08x\n", objno,
AnilKumar Ch33f81002012-05-29 11:13:15 +0530525 c_can_read_reg32(priv, C_CAN_MSGVAL1_REG));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800526}
527
528static inline int c_can_is_next_tx_obj_busy(struct c_can_priv *priv, int objno)
529{
AnilKumar Ch33f81002012-05-29 11:13:15 +0530530 int val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800531
532 /*
533 * as transmission request register's bit n-1 corresponds to
534 * message object n, we need to handle the same properly.
535 */
536 if (val & (1 << (objno - 1)))
537 return 1;
538
539 return 0;
540}
541
542static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
543 struct net_device *dev)
544{
545 u32 msg_obj_no;
546 struct c_can_priv *priv = netdev_priv(dev);
547 struct can_frame *frame = (struct can_frame *)skb->data;
548
549 if (can_dropped_invalid_skb(dev, skb))
550 return NETDEV_TX_OK;
551
552 msg_obj_no = get_tx_next_msg_obj(priv);
553
554 /* prepare message object for transmission */
Thomas Gleixner640916d2014-03-18 17:19:09 +0000555 c_can_write_msg_object(dev, IF_TX, frame, msg_obj_no);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800556 can_put_echo_skb(skb, dev, msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
557
558 /*
559 * we have to stop the queue in case of a wrap around or
560 * if the next TX message object is still in use
561 */
562 priv->tx_next++;
563 if (c_can_is_next_tx_obj_busy(priv, get_tx_next_msg_obj(priv)) ||
564 (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) == 0)
565 netif_stop_queue(dev);
566
567 return NETDEV_TX_OK;
568}
569
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000570static int c_can_wait_for_ctrl_init(struct net_device *dev,
571 struct c_can_priv *priv, u32 init)
572{
573 int retry = 0;
574
575 while (init != (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_INIT)) {
576 udelay(10);
577 if (retry++ > 1000) {
578 netdev_err(dev, "CCTRL: set CONTROL_INIT failed\n");
579 return -EIO;
580 }
581 }
582 return 0;
583}
584
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800585static int c_can_set_bittiming(struct net_device *dev)
586{
587 unsigned int reg_btr, reg_brpe, ctrl_save;
588 u8 brp, brpe, sjw, tseg1, tseg2;
589 u32 ten_bit_brp;
590 struct c_can_priv *priv = netdev_priv(dev);
591 const struct can_bittiming *bt = &priv->can.bittiming;
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000592 int res;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800593
594 /* c_can provides a 6-bit brp and 4-bit brpe fields */
595 ten_bit_brp = bt->brp - 1;
596 brp = ten_bit_brp & BTR_BRP_MASK;
597 brpe = ten_bit_brp >> 6;
598
599 sjw = bt->sjw - 1;
600 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
601 tseg2 = bt->phase_seg2 - 1;
602 reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
603 (tseg2 << BTR_TSEG2_SHIFT);
604 reg_brpe = brpe & BRP_EXT_BRPE_MASK;
605
606 netdev_info(dev,
607 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
608
AnilKumar Ch33f81002012-05-29 11:13:15 +0530609 ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000610 ctrl_save &= ~CONTROL_INIT;
611 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_CCE | CONTROL_INIT);
612 res = c_can_wait_for_ctrl_init(dev, priv, CONTROL_INIT);
613 if (res)
614 return res;
615
AnilKumar Ch33f81002012-05-29 11:13:15 +0530616 priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
617 priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
618 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800619
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000620 return c_can_wait_for_ctrl_init(dev, priv, 0);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800621}
622
623/*
624 * Configure C_CAN message objects for Tx and Rx purposes:
625 * C_CAN provides a total of 32 message objects that can be configured
626 * either for Tx or Rx purposes. Here the first 16 message objects are used as
627 * a reception FIFO. The end of reception FIFO is signified by the EoB bit
628 * being SET. The remaining 16 message objects are kept aside for Tx purposes.
629 * See user guide document for further details on configuring message
630 * objects.
631 */
632static void c_can_configure_msg_objects(struct net_device *dev)
633{
634 int i;
635
636 /* first invalidate all message objects */
637 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
Thomas Gleixner640916d2014-03-18 17:19:09 +0000638 c_can_inval_msg_object(dev, IF_RX, i);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800639
640 /* setup receive message objects */
641 for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
Thomas Gleixner640916d2014-03-18 17:19:09 +0000642 c_can_setup_receive_object(dev, IF_RX, i, 0, 0,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800643 (IF_MCONT_RXIE | IF_MCONT_UMASK) & ~IF_MCONT_EOB);
644
Thomas Gleixner640916d2014-03-18 17:19:09 +0000645 c_can_setup_receive_object(dev, IF_RX, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800646 IF_MCONT_EOB | IF_MCONT_RXIE | IF_MCONT_UMASK);
647}
648
649/*
650 * Configure C_CAN chip:
651 * - enable/disable auto-retransmission
652 * - set operating mode
653 * - configure message objects
654 */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100655static int c_can_chip_config(struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800656{
657 struct c_can_priv *priv = netdev_priv(dev);
658
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +0000659 /* enable automatic retransmission */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530660 priv->write_reg(priv, C_CAN_CTRL_REG,
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +0000661 CONTROL_ENABLE_AR);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800662
Dan Carpenterd9cb9bd2012-06-15 00:20:44 +0000663 if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
664 (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800665 /* loopback + silent mode : useful for hot self-test */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530666 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800667 CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530668 priv->write_reg(priv, C_CAN_TEST_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800669 TEST_LBACK | TEST_SILENT);
670 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
671 /* loopback mode : useful for self-test function */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530672 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800673 CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530674 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800675 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
676 /* silent mode : bus-monitoring mode */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530677 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_EIE |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800678 CONTROL_SIE | CONTROL_IE | CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530679 priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800680 } else
681 /* normal mode*/
AnilKumar Ch33f81002012-05-29 11:13:15 +0530682 priv->write_reg(priv, C_CAN_CTRL_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800683 CONTROL_EIE | CONTROL_SIE | CONTROL_IE);
684
685 /* configure message objects */
686 c_can_configure_msg_objects(dev);
687
688 /* set a `lec` value so that we can check for updates later */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530689 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800690
691 /* set bittiming params */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100692 return c_can_set_bittiming(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800693}
694
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100695static int c_can_start(struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800696{
697 struct c_can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100698 int err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800699
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800700 /* basic c_can configuration */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100701 err = c_can_chip_config(dev);
702 if (err)
703 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800704
705 priv->can.state = CAN_STATE_ERROR_ACTIVE;
706
707 /* reset tx helper pointers */
708 priv->tx_next = priv->tx_echo = 0;
Jan Altenberg4f2d56c2011-03-21 18:19:26 -0700709
710 /* enable status change, error and module interrupts */
711 c_can_enable_all_interrupts(priv, ENABLE_ALL_INTERRUPTS);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100712
713 return 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800714}
715
716static void c_can_stop(struct net_device *dev)
717{
718 struct c_can_priv *priv = netdev_priv(dev);
719
720 /* disable all interrupts */
721 c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
722
723 /* set the state as STOPPED */
724 priv->can.state = CAN_STATE_STOPPED;
725}
726
727static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
728{
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100729 int err;
730
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800731 switch (mode) {
732 case CAN_MODE_START:
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100733 err = c_can_start(dev);
734 if (err)
735 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800736 netif_wake_queue(dev);
737 break;
738 default:
739 return -EOPNOTSUPP;
740 }
741
742 return 0;
743}
744
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100745static int __c_can_get_berr_counter(const struct net_device *dev,
746 struct can_berr_counter *bec)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800747{
748 unsigned int reg_err_counter;
749 struct c_can_priv *priv = netdev_priv(dev);
750
AnilKumar Ch33f81002012-05-29 11:13:15 +0530751 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800752 bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
753 ERR_CNT_REC_SHIFT;
754 bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
755
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100756 return 0;
757}
758
759static int c_can_get_berr_counter(const struct net_device *dev,
760 struct can_berr_counter *bec)
761{
762 struct c_can_priv *priv = netdev_priv(dev);
763 int err;
764
765 c_can_pm_runtime_get_sync(priv);
766 err = __c_can_get_berr_counter(dev, bec);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +0530767 c_can_pm_runtime_put_sync(priv);
768
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100769 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800770}
771
772/*
773 * theory of operation:
774 *
775 * priv->tx_echo holds the number of the oldest can_frame put for
776 * transmission into the hardware, but not yet ACKed by the CAN tx
777 * complete IRQ.
778 *
779 * We iterate from priv->tx_echo to priv->tx_next and check if the
780 * packet has been transmitted, echo it back to the CAN framework.
AnilKumar Ch617cacc2012-05-23 17:45:09 +0530781 * If we discover a not yet transmitted packet, stop looking for more.
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800782 */
783static void c_can_do_tx(struct net_device *dev)
784{
785 u32 val;
786 u32 msg_obj_no;
787 struct c_can_priv *priv = netdev_priv(dev);
788 struct net_device_stats *stats = &dev->stats;
789
790 for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
791 msg_obj_no = get_tx_echo_msg_obj(priv);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530792 val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
AnilKumar Ch617cacc2012-05-23 17:45:09 +0530793 if (!(val & (1 << (msg_obj_no - 1)))) {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800794 can_get_echo_skb(dev,
795 msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
Thomas Gleixner640916d2014-03-18 17:19:09 +0000796 c_can_object_get(dev, IF_TX, msg_obj_no, IF_COMM_ALL);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800797 stats->tx_bytes += priv->read_reg(priv,
Thomas Gleixner640916d2014-03-18 17:19:09 +0000798 C_CAN_IFACE(MSGCTRL_REG, IF_TX))
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800799 & IF_MCONT_DLC_MASK;
800 stats->tx_packets++;
Fabio Baltieri5090f802012-12-18 18:51:01 +0100801 can_led_event(dev, CAN_LED_EVENT_TX);
Thomas Gleixner640916d2014-03-18 17:19:09 +0000802 c_can_inval_msg_object(dev, IF_TX, msg_obj_no);
AnilKumar Ch617cacc2012-05-23 17:45:09 +0530803 } else {
804 break;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800805 }
806 }
807
808 /* restart queue if wrap-up or if queue stalled on last pkt */
809 if (((priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) != 0) ||
810 ((priv->tx_echo & C_CAN_NEXT_MSG_OBJ_MASK) == 0))
811 netif_wake_queue(dev);
812}
813
814/*
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000815 * If we have a gap in the pending bits, that means we either
816 * raced with the hardware or failed to readout all upper
817 * objects in the last run due to quota limit.
818 */
819static u32 c_can_adjust_pending(u32 pend)
820{
821 u32 weight, lasts;
822
823 if (pend == RECEIVE_OBJECT_BITS)
824 return pend;
825
826 /*
827 * If the last set bit is larger than the number of pending
828 * bits we have a gap.
829 */
830 weight = hweight32(pend);
831 lasts = fls(pend);
832
833 /* If the bits are linear, nothing to do */
834 if (lasts == weight)
835 return pend;
836
837 /*
838 * Find the first set bit after the gap. We walk backwards
839 * from the last set bit.
840 */
841 for (lasts--; pend & (1 << (lasts - 1)); lasts--);
842
843 return pend & ~((1 << lasts) - 1);
844}
845
846/*
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800847 * theory of operation:
848 *
849 * c_can core saves a received CAN message into the first free message
850 * object it finds free (starting with the lowest). Bits NEWDAT and
851 * INTPND are set for this message object indicating that a new message
852 * has arrived. To work-around this issue, we keep two groups of message
853 * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
854 *
855 * To ensure in-order frame reception we use the following
856 * approach while re-activating a message object to receive further
857 * frames:
858 * - if the current message object number is lower than
859 * C_CAN_MSG_RX_LOW_LAST, do not clear the NEWDAT bit while clearing
860 * the INTPND bit.
861 * - if the current message object number is equal to
862 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of all lower
863 * receive message objects.
864 * - if the current message object number is greater than
865 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of
866 * only this message object.
867 */
868static int c_can_do_rx_poll(struct net_device *dev, int quota)
869{
870 u32 num_rx_pkts = 0;
871 unsigned int msg_obj, msg_ctrl_save;
872 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000873 u32 val, pend = 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800874
Markus Pargmann4ce78a82013-11-01 10:36:36 +0100875 /*
876 * It is faster to read only one 16bit register. This is only possible
877 * for a maximum number of 16 objects.
878 */
879 BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST > 16,
880 "Implementation does not support more message objects than 16");
881
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000882 while (quota > 0) {
883
884 if (!pend) {
885 pend = priv->read_reg(priv, C_CAN_INTPND1_REG);
886 if (!pend)
887 return num_rx_pkts;
888 /*
889 * If the pending field has a gap, handle the
890 * bits above the gap first.
891 */
892 val = c_can_adjust_pending(pend);
893 } else {
894 val = pend;
895 }
896 /* Remove the bits from pend */
897 pend &= ~val;
898
Markus Pargmann4ce78a82013-11-01 10:36:36 +0100899 while ((msg_obj = ffs(val)) && quota > 0) {
900 val &= ~BIT(msg_obj - 1);
901
Thomas Gleixner640916d2014-03-18 17:19:09 +0000902 c_can_object_get(dev, IF_RX, msg_obj, IF_COMM_ALL &
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800903 ~IF_COMM_TXRQST);
904 msg_ctrl_save = priv->read_reg(priv,
Thomas Gleixner640916d2014-03-18 17:19:09 +0000905 C_CAN_IFACE(MSGCTRL_REG, IF_RX));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800906
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800907 if (msg_ctrl_save & IF_MCONT_MSGLST) {
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000908 int n;
909
910 n = c_can_handle_lost_msg_obj(dev, IF_RX,
911 msg_obj,
912 msg_ctrl_save);
913 num_rx_pkts += n;
914 quota -=n;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800915 continue;
916 }
917
Markus Pargmann5d0f8012013-10-28 09:54:40 +0100918 if (msg_ctrl_save & IF_MCONT_EOB)
919 return num_rx_pkts;
920
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800921 if (!(msg_ctrl_save & IF_MCONT_NEWDAT))
922 continue;
923
924 /* read the data from the message object */
Thomas Gleixner640916d2014-03-18 17:19:09 +0000925 c_can_read_msg_object(dev, IF_RX, msg_ctrl_save);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800926
927 if (msg_obj < C_CAN_MSG_RX_LOW_LAST)
Thomas Gleixner640916d2014-03-18 17:19:09 +0000928 c_can_mark_rx_msg_obj(dev, IF_RX,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800929 msg_ctrl_save, msg_obj);
930 else if (msg_obj > C_CAN_MSG_RX_LOW_LAST)
931 /* activate this msg obj */
Thomas Gleixner640916d2014-03-18 17:19:09 +0000932 c_can_activate_rx_msg_obj(dev, IF_RX,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800933 msg_ctrl_save, msg_obj);
934 else if (msg_obj == C_CAN_MSG_RX_LOW_LAST)
935 /* activate all lower message objects */
936 c_can_activate_all_lower_rx_msg_obj(dev,
Thomas Gleixner640916d2014-03-18 17:19:09 +0000937 IF_RX, msg_ctrl_save);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800938
939 num_rx_pkts++;
940 quota--;
941 }
942 }
943
944 return num_rx_pkts;
945}
946
947static inline int c_can_has_and_handle_berr(struct c_can_priv *priv)
948{
949 return (priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING) &&
950 (priv->current_status & LEC_UNUSED);
951}
952
953static int c_can_handle_state_change(struct net_device *dev,
954 enum c_can_bus_error_types error_type)
955{
956 unsigned int reg_err_counter;
957 unsigned int rx_err_passive;
958 struct c_can_priv *priv = netdev_priv(dev);
959 struct net_device_stats *stats = &dev->stats;
960 struct can_frame *cf;
961 struct sk_buff *skb;
962 struct can_berr_counter bec;
963
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300964 /* propagate the error condition to the CAN stack */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800965 skb = alloc_can_err_skb(dev, &cf);
966 if (unlikely(!skb))
967 return 0;
968
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100969 __c_can_get_berr_counter(dev, &bec);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530970 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800971 rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
972 ERR_CNT_RP_SHIFT;
973
974 switch (error_type) {
975 case C_CAN_ERROR_WARNING:
976 /* error warning state */
977 priv->can.can_stats.error_warning++;
978 priv->can.state = CAN_STATE_ERROR_WARNING;
979 cf->can_id |= CAN_ERR_CRTL;
980 cf->data[1] = (bec.txerr > bec.rxerr) ?
981 CAN_ERR_CRTL_TX_WARNING :
982 CAN_ERR_CRTL_RX_WARNING;
983 cf->data[6] = bec.txerr;
984 cf->data[7] = bec.rxerr;
985
986 break;
987 case C_CAN_ERROR_PASSIVE:
988 /* error passive state */
989 priv->can.can_stats.error_passive++;
990 priv->can.state = CAN_STATE_ERROR_PASSIVE;
991 cf->can_id |= CAN_ERR_CRTL;
992 if (rx_err_passive)
993 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
994 if (bec.txerr > 127)
995 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
996
997 cf->data[6] = bec.txerr;
998 cf->data[7] = bec.rxerr;
999 break;
1000 case C_CAN_BUS_OFF:
1001 /* bus-off state */
1002 priv->can.state = CAN_STATE_BUS_OFF;
1003 cf->can_id |= CAN_ERR_BUSOFF;
1004 /*
1005 * disable all interrupts in bus-off mode to ensure that
1006 * the CPU is not hogged down
1007 */
1008 c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
1009 can_bus_off(dev);
1010 break;
1011 default:
1012 break;
1013 }
1014
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_handle_bus_err(struct net_device *dev,
1023 enum c_can_lec_type lec_type)
1024{
1025 struct c_can_priv *priv = netdev_priv(dev);
1026 struct net_device_stats *stats = &dev->stats;
1027 struct can_frame *cf;
1028 struct sk_buff *skb;
1029
1030 /*
1031 * early exit if no lec update or no error.
1032 * no lec update means that no CAN bus event has been detected
1033 * since CPU wrote 0x7 value to status reg.
1034 */
1035 if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
1036 return 0;
1037
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001038 /* propagate the error condition to the CAN stack */
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001039 skb = alloc_can_err_skb(dev, &cf);
1040 if (unlikely(!skb))
1041 return 0;
1042
1043 /*
1044 * check for 'last error code' which tells us the
1045 * type of the last error to occur on the CAN bus
1046 */
1047
1048 /* common for all type of bus errors */
1049 priv->can.can_stats.bus_error++;
1050 stats->rx_errors++;
1051 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
1052 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
1053
1054 switch (lec_type) {
1055 case LEC_STUFF_ERROR:
1056 netdev_dbg(dev, "stuff error\n");
1057 cf->data[2] |= CAN_ERR_PROT_STUFF;
1058 break;
1059 case LEC_FORM_ERROR:
1060 netdev_dbg(dev, "form error\n");
1061 cf->data[2] |= CAN_ERR_PROT_FORM;
1062 break;
1063 case LEC_ACK_ERROR:
1064 netdev_dbg(dev, "ack error\n");
Olivier Sobrie6ea45882013-01-18 09:32:39 +01001065 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001066 CAN_ERR_PROT_LOC_ACK_DEL);
1067 break;
1068 case LEC_BIT1_ERROR:
1069 netdev_dbg(dev, "bit1 error\n");
1070 cf->data[2] |= CAN_ERR_PROT_BIT1;
1071 break;
1072 case LEC_BIT0_ERROR:
1073 netdev_dbg(dev, "bit0 error\n");
1074 cf->data[2] |= CAN_ERR_PROT_BIT0;
1075 break;
1076 case LEC_CRC_ERROR:
1077 netdev_dbg(dev, "CRC error\n");
Olivier Sobrie6ea45882013-01-18 09:32:39 +01001078 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001079 CAN_ERR_PROT_LOC_CRC_DEL);
1080 break;
1081 default:
1082 break;
1083 }
1084
1085 /* set a `lec` value so that we can check for updates later */
AnilKumar Ch33f81002012-05-29 11:13:15 +05301086 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001087
1088 netif_receive_skb(skb);
1089 stats->rx_packets++;
1090 stats->rx_bytes += cf->can_dlc;
1091
1092 return 1;
1093}
1094
1095static int c_can_poll(struct napi_struct *napi, int quota)
1096{
1097 u16 irqstatus;
1098 int lec_type = 0;
1099 int work_done = 0;
1100 struct net_device *dev = napi->dev;
1101 struct c_can_priv *priv = netdev_priv(dev);
1102
AnilKumar Ch148c87c2012-05-23 17:45:10 +05301103 irqstatus = priv->irqstatus;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001104 if (!irqstatus)
1105 goto end;
1106
1107 /* status events have the highest priority */
1108 if (irqstatus == STATUS_INTERRUPT) {
1109 priv->current_status = priv->read_reg(priv,
AnilKumar Ch33f81002012-05-29 11:13:15 +05301110 C_CAN_STS_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001111
1112 /* handle Tx/Rx events */
1113 if (priv->current_status & STATUS_TXOK)
AnilKumar Ch33f81002012-05-29 11:13:15 +05301114 priv->write_reg(priv, C_CAN_STS_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001115 priv->current_status & ~STATUS_TXOK);
1116
1117 if (priv->current_status & STATUS_RXOK)
AnilKumar Ch33f81002012-05-29 11:13:15 +05301118 priv->write_reg(priv, C_CAN_STS_REG,
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001119 priv->current_status & ~STATUS_RXOK);
1120
1121 /* handle state changes */
1122 if ((priv->current_status & STATUS_EWARN) &&
1123 (!(priv->last_status & STATUS_EWARN))) {
1124 netdev_dbg(dev, "entered error warning state\n");
1125 work_done += c_can_handle_state_change(dev,
1126 C_CAN_ERROR_WARNING);
1127 }
1128 if ((priv->current_status & STATUS_EPASS) &&
1129 (!(priv->last_status & STATUS_EPASS))) {
1130 netdev_dbg(dev, "entered error passive state\n");
1131 work_done += c_can_handle_state_change(dev,
1132 C_CAN_ERROR_PASSIVE);
1133 }
1134 if ((priv->current_status & STATUS_BOFF) &&
1135 (!(priv->last_status & STATUS_BOFF))) {
1136 netdev_dbg(dev, "entered bus off state\n");
1137 work_done += c_can_handle_state_change(dev,
1138 C_CAN_BUS_OFF);
1139 }
1140
1141 /* handle bus recovery events */
1142 if ((!(priv->current_status & STATUS_BOFF)) &&
1143 (priv->last_status & STATUS_BOFF)) {
1144 netdev_dbg(dev, "left bus off state\n");
1145 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1146 }
1147 if ((!(priv->current_status & STATUS_EPASS)) &&
1148 (priv->last_status & STATUS_EPASS)) {
1149 netdev_dbg(dev, "left error passive state\n");
1150 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1151 }
1152
1153 priv->last_status = priv->current_status;
1154
1155 /* handle lec errors on the bus */
1156 lec_type = c_can_has_and_handle_berr(priv);
1157 if (lec_type)
1158 work_done += c_can_handle_bus_err(dev, lec_type);
1159 } else if ((irqstatus >= C_CAN_MSG_OBJ_RX_FIRST) &&
1160 (irqstatus <= C_CAN_MSG_OBJ_RX_LAST)) {
1161 /* handle events corresponding to receive message objects */
1162 work_done += c_can_do_rx_poll(dev, (quota - work_done));
1163 } else if ((irqstatus >= C_CAN_MSG_OBJ_TX_FIRST) &&
1164 (irqstatus <= C_CAN_MSG_OBJ_TX_LAST)) {
1165 /* handle events corresponding to transmit message objects */
1166 c_can_do_tx(dev);
1167 }
1168
1169end:
1170 if (work_done < quota) {
1171 napi_complete(napi);
1172 /* enable all IRQs */
1173 c_can_enable_all_interrupts(priv, ENABLE_ALL_INTERRUPTS);
1174 }
1175
1176 return work_done;
1177}
1178
1179static irqreturn_t c_can_isr(int irq, void *dev_id)
1180{
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001181 struct net_device *dev = (struct net_device *)dev_id;
1182 struct c_can_priv *priv = netdev_priv(dev);
1183
AnilKumar Ch33f81002012-05-29 11:13:15 +05301184 priv->irqstatus = priv->read_reg(priv, C_CAN_INT_REG);
AnilKumar Ch148c87c2012-05-23 17:45:10 +05301185 if (!priv->irqstatus)
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001186 return IRQ_NONE;
1187
1188 /* disable all interrupts and schedule the NAPI */
1189 c_can_enable_all_interrupts(priv, DISABLE_ALL_INTERRUPTS);
1190 napi_schedule(&priv->napi);
1191
1192 return IRQ_HANDLED;
1193}
1194
1195static int c_can_open(struct net_device *dev)
1196{
1197 int err;
1198 struct c_can_priv *priv = netdev_priv(dev);
1199
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301200 c_can_pm_runtime_get_sync(priv);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301201 c_can_reset_ram(priv, true);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301202
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001203 /* open the can device */
1204 err = open_candev(dev);
1205 if (err) {
1206 netdev_err(dev, "failed to open can device\n");
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301207 goto exit_open_fail;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001208 }
1209
1210 /* register interrupt handler */
1211 err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
1212 dev);
1213 if (err < 0) {
1214 netdev_err(dev, "failed to request interrupt\n");
1215 goto exit_irq_fail;
1216 }
1217
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001218 /* start the c_can controller */
1219 err = c_can_start(dev);
1220 if (err)
1221 goto exit_start_fail;
AnilKumar Chf461f272012-05-23 17:45:11 +05301222
Fabio Baltieri5090f802012-12-18 18:51:01 +01001223 can_led_event(dev, CAN_LED_EVENT_OPEN);
1224
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001225 napi_enable(&priv->napi);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001226 netif_start_queue(dev);
1227
1228 return 0;
1229
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001230exit_start_fail:
1231 free_irq(dev->irq, dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001232exit_irq_fail:
1233 close_candev(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301234exit_open_fail:
AnilKumar Ch52cde852012-11-21 11:14:10 +05301235 c_can_reset_ram(priv, false);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301236 c_can_pm_runtime_put_sync(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001237 return err;
1238}
1239
1240static int c_can_close(struct net_device *dev)
1241{
1242 struct c_can_priv *priv = netdev_priv(dev);
1243
1244 netif_stop_queue(dev);
1245 napi_disable(&priv->napi);
1246 c_can_stop(dev);
1247 free_irq(dev->irq, dev);
1248 close_candev(dev);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301249
1250 c_can_reset_ram(priv, false);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301251 c_can_pm_runtime_put_sync(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001252
Fabio Baltieri5090f802012-12-18 18:51:01 +01001253 can_led_event(dev, CAN_LED_EVENT_STOP);
1254
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001255 return 0;
1256}
1257
1258struct net_device *alloc_c_can_dev(void)
1259{
1260 struct net_device *dev;
1261 struct c_can_priv *priv;
1262
1263 dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM);
1264 if (!dev)
1265 return NULL;
1266
1267 priv = netdev_priv(dev);
1268 netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
1269
1270 priv->dev = dev;
1271 priv->can.bittiming_const = &c_can_bittiming_const;
1272 priv->can.do_set_mode = c_can_set_mode;
1273 priv->can.do_get_berr_counter = c_can_get_berr_counter;
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +00001274 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001275 CAN_CTRLMODE_LISTENONLY |
1276 CAN_CTRLMODE_BERR_REPORTING;
1277
1278 return dev;
1279}
1280EXPORT_SYMBOL_GPL(alloc_c_can_dev);
1281
AnilKumar Ch82120032012-09-21 15:29:01 +05301282#ifdef CONFIG_PM
1283int c_can_power_down(struct net_device *dev)
1284{
1285 u32 val;
1286 unsigned long time_out;
1287 struct c_can_priv *priv = netdev_priv(dev);
1288
1289 if (!(dev->flags & IFF_UP))
1290 return 0;
1291
1292 WARN_ON(priv->type != BOSCH_D_CAN);
1293
1294 /* set PDR value so the device goes to power down mode */
1295 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1296 val |= CONTROL_EX_PDR;
1297 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1298
1299 /* Wait for the PDA bit to get set */
1300 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1301 while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1302 time_after(time_out, jiffies))
1303 cpu_relax();
1304
1305 if (time_after(jiffies, time_out))
1306 return -ETIMEDOUT;
1307
1308 c_can_stop(dev);
1309
AnilKumar Ch52cde852012-11-21 11:14:10 +05301310 c_can_reset_ram(priv, false);
AnilKumar Ch82120032012-09-21 15:29:01 +05301311 c_can_pm_runtime_put_sync(priv);
1312
1313 return 0;
1314}
1315EXPORT_SYMBOL_GPL(c_can_power_down);
1316
1317int c_can_power_up(struct net_device *dev)
1318{
1319 u32 val;
1320 unsigned long time_out;
1321 struct c_can_priv *priv = netdev_priv(dev);
1322
1323 if (!(dev->flags & IFF_UP))
1324 return 0;
1325
1326 WARN_ON(priv->type != BOSCH_D_CAN);
1327
1328 c_can_pm_runtime_get_sync(priv);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301329 c_can_reset_ram(priv, true);
AnilKumar Ch82120032012-09-21 15:29:01 +05301330
1331 /* Clear PDR and INIT bits */
1332 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1333 val &= ~CONTROL_EX_PDR;
1334 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1335 val = priv->read_reg(priv, C_CAN_CTRL_REG);
1336 val &= ~CONTROL_INIT;
1337 priv->write_reg(priv, C_CAN_CTRL_REG, val);
1338
1339 /* Wait for the PDA bit to get clear */
1340 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1341 while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1342 time_after(time_out, jiffies))
1343 cpu_relax();
1344
1345 if (time_after(jiffies, time_out))
1346 return -ETIMEDOUT;
1347
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001348 return c_can_start(dev);
AnilKumar Ch82120032012-09-21 15:29:01 +05301349}
1350EXPORT_SYMBOL_GPL(c_can_power_up);
1351#endif
1352
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001353void free_c_can_dev(struct net_device *dev)
1354{
Marc Kleine-Buddef29b4232014-03-18 19:13:59 +01001355 struct c_can_priv *priv = netdev_priv(dev);
1356
1357 netif_napi_del(&priv->napi);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001358 free_candev(dev);
1359}
1360EXPORT_SYMBOL_GPL(free_c_can_dev);
1361
1362static const struct net_device_ops c_can_netdev_ops = {
1363 .ndo_open = c_can_open,
1364 .ndo_stop = c_can_close,
1365 .ndo_start_xmit = c_can_start_xmit,
1366};
1367
1368int register_c_can_dev(struct net_device *dev)
1369{
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301370 struct c_can_priv *priv = netdev_priv(dev);
1371 int err;
1372
1373 c_can_pm_runtime_enable(priv);
1374
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001375 dev->flags |= IFF_ECHO; /* we support local echo */
1376 dev->netdev_ops = &c_can_netdev_ops;
1377
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301378 err = register_candev(dev);
1379 if (err)
1380 c_can_pm_runtime_disable(priv);
Fabio Baltieri5090f802012-12-18 18:51:01 +01001381 else
1382 devm_can_led_init(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301383
1384 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001385}
1386EXPORT_SYMBOL_GPL(register_c_can_dev);
1387
1388void unregister_c_can_dev(struct net_device *dev)
1389{
1390 struct c_can_priv *priv = netdev_priv(dev);
1391
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001392 unregister_candev(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301393
1394 c_can_pm_runtime_disable(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001395}
1396EXPORT_SYMBOL_GPL(unregister_c_can_dev);
1397
1398MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1399MODULE_LICENSE("GPL v2");
1400MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");