blob: 61fde414bb1fea245c9273fb36bef2d7c27311c0 [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
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +000063#define CONTROL_IRQMSK (CONTROL_EIE | CONTROL_IE | CONTROL_SIE)
64
Bhupesh Sharma881ff672011-02-13 22:51:44 -080065/* test register */
66#define TEST_RX BIT(7)
67#define TEST_TX1 BIT(6)
68#define TEST_TX2 BIT(5)
69#define TEST_LBACK BIT(4)
70#define TEST_SILENT BIT(3)
71#define TEST_BASIC BIT(2)
72
73/* status register */
AnilKumar Ch82120032012-09-21 15:29:01 +053074#define STATUS_PDA BIT(10)
Bhupesh Sharma881ff672011-02-13 22:51:44 -080075#define STATUS_BOFF BIT(7)
76#define STATUS_EWARN BIT(6)
77#define STATUS_EPASS BIT(5)
78#define STATUS_RXOK BIT(4)
79#define STATUS_TXOK BIT(3)
80
81/* error counter register */
82#define ERR_CNT_TEC_MASK 0xff
83#define ERR_CNT_TEC_SHIFT 0
84#define ERR_CNT_REC_SHIFT 8
85#define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
86#define ERR_CNT_RP_SHIFT 15
87#define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
88
89/* bit-timing register */
90#define BTR_BRP_MASK 0x3f
91#define BTR_BRP_SHIFT 0
92#define BTR_SJW_SHIFT 6
93#define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
94#define BTR_TSEG1_SHIFT 8
95#define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
96#define BTR_TSEG2_SHIFT 12
97#define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
98
99/* brp extension register */
100#define BRP_EXT_BRPE_MASK 0x0f
101#define BRP_EXT_BRPE_SHIFT 0
102
103/* IFx command request */
104#define IF_COMR_BUSY BIT(15)
105
106/* IFx command mask */
107#define IF_COMM_WR BIT(7)
108#define IF_COMM_MASK BIT(6)
109#define IF_COMM_ARB BIT(5)
110#define IF_COMM_CONTROL BIT(4)
111#define IF_COMM_CLR_INT_PND BIT(3)
112#define IF_COMM_TXRQST BIT(2)
Thomas Gleixner6b48ff82014-04-11 08:13:14 +0000113#define IF_COMM_CLR_NEWDAT IF_COMM_TXRQST
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800114#define IF_COMM_DATAA BIT(1)
115#define IF_COMM_DATAB BIT(0)
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000116
117/* TX buffer setup */
118#define IF_COMM_TX (IF_COMM_ARB | IF_COMM_CONTROL | \
119 IF_COMM_TXRQST | \
120 IF_COMM_DATAA | IF_COMM_DATAB)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800121
Thomas Gleixnerc0a9f4d2014-03-18 17:19:13 +0000122/* For the low buffers we clear the interrupt bit, but keep newdat */
123#define IF_COMM_RCV_LOW (IF_COMM_MASK | IF_COMM_ARB | \
124 IF_COMM_CONTROL | IF_COMM_CLR_INT_PND | \
125 IF_COMM_DATAA | IF_COMM_DATAB)
126
127/* For the high buffers we clear the interrupt bit and newdat */
Thomas Gleixner6b48ff82014-04-11 08:13:14 +0000128#define IF_COMM_RCV_HIGH (IF_COMM_RCV_LOW | IF_COMM_CLR_NEWDAT)
Thomas Gleixnerc0a9f4d2014-03-18 17:19:13 +0000129
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000130
131/* Receive setup of message objects */
132#define IF_COMM_RCV_SETUP (IF_COMM_MASK | IF_COMM_ARB | IF_COMM_CONTROL)
133
Thomas Gleixnerb07faaa2014-04-11 08:13:19 +0000134/* Invalidation of message objects */
135#define IF_COMM_INVAL (IF_COMM_ARB | IF_COMM_CONTROL)
136
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800137/* IFx arbitration */
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000138#define IF_ARB_MSGVAL BIT(31)
139#define IF_ARB_MSGXTD BIT(30)
140#define IF_ARB_TRANSMIT BIT(29)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800141
142/* IFx message control */
143#define IF_MCONT_NEWDAT BIT(15)
144#define IF_MCONT_MSGLST BIT(14)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800145#define IF_MCONT_INTPND BIT(13)
146#define IF_MCONT_UMASK BIT(12)
147#define IF_MCONT_TXIE BIT(11)
148#define IF_MCONT_RXIE BIT(10)
149#define IF_MCONT_RMTEN BIT(9)
150#define IF_MCONT_TXRQST BIT(8)
151#define IF_MCONT_EOB BIT(7)
152#define IF_MCONT_DLC_MASK 0xf
153
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000154#define IF_MCONT_RCV (IF_MCONT_RXIE | IF_MCONT_UMASK)
155#define IF_MCONT_RCV_EOB (IF_MCONT_RCV | IF_MCONT_EOB)
156
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000157#define IF_MCONT_TX (IF_MCONT_TXIE | IF_MCONT_EOB)
158
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800159/*
Thomas Gleixner640916d2014-03-18 17:19:09 +0000160 * Use IF1 for RX and IF2 for TX
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800161 */
Thomas Gleixner640916d2014-03-18 17:19:09 +0000162#define IF_RX 0
163#define IF_TX 1
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800164
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800165/* minimum timeout for checking BUSY status */
166#define MIN_TIMEOUT_VALUE 6
167
AnilKumar Ch82120032012-09-21 15:29:01 +0530168/* Wait for ~1 sec for INIT bit */
169#define INIT_WAIT_MS 1000
170
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800171/* napi related */
172#define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
173
174/* c_can lec values */
175enum c_can_lec_type {
176 LEC_NO_ERROR = 0,
177 LEC_STUFF_ERROR,
178 LEC_FORM_ERROR,
179 LEC_ACK_ERROR,
180 LEC_BIT1_ERROR,
181 LEC_BIT0_ERROR,
182 LEC_CRC_ERROR,
183 LEC_UNUSED,
Thomas Gleixner097aec12014-04-11 08:13:13 +0000184 LEC_MASK = LEC_UNUSED,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800185};
186
187/*
188 * c_can error types:
189 * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
190 */
191enum c_can_bus_error_types {
192 C_CAN_NO_ERROR = 0,
193 C_CAN_BUS_OFF,
194 C_CAN_ERROR_WARNING,
195 C_CAN_ERROR_PASSIVE,
196};
197
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200198static const struct can_bittiming_const c_can_bittiming_const = {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800199 .name = KBUILD_MODNAME,
200 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
201 .tseg1_max = 16,
202 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
203 .tseg2_max = 8,
204 .sjw_max = 4,
205 .brp_min = 1,
206 .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/
207 .brp_inc = 1,
208};
209
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +0530210static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
211{
212 if (priv->device)
213 pm_runtime_enable(priv->device);
214}
215
216static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
217{
218 if (priv->device)
219 pm_runtime_disable(priv->device);
220}
221
222static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
223{
224 if (priv->device)
225 pm_runtime_get_sync(priv->device);
226}
227
228static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
229{
230 if (priv->device)
231 pm_runtime_put_sync(priv->device);
232}
233
AnilKumar Ch52cde852012-11-21 11:14:10 +0530234static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
235{
236 if (priv->raminit)
237 priv->raminit(priv, enable);
238}
239
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800240static inline int get_tx_next_msg_obj(const struct c_can_priv *priv)
241{
242 return (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) +
243 C_CAN_MSG_OBJ_TX_FIRST;
244}
245
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000246static inline int get_tx_echo_msg_obj(int txecho)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800247{
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000248 return (txecho & C_CAN_NEXT_MSG_OBJ_MASK) + C_CAN_MSG_OBJ_TX_FIRST;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800249}
250
AnilKumar Ch33f81002012-05-29 11:13:15 +0530251static u32 c_can_read_reg32(struct c_can_priv *priv, enum reg index)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800252{
AnilKumar Ch33f81002012-05-29 11:13:15 +0530253 u32 val = priv->read_reg(priv, index);
254 val |= ((u32) priv->read_reg(priv, index + 1)) << 16;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800255 return val;
256}
257
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000258static void c_can_irq_control(struct c_can_priv *priv, bool enable)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800259{
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000260 u32 ctrl = priv->read_reg(priv, C_CAN_CTRL_REG) & ~CONTROL_IRQMSK;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800261
262 if (enable)
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000263 ctrl |= CONTROL_IRQMSK;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800264
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000265 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800266}
267
Thomas Gleixner7af28632014-04-11 08:13:20 +0000268static void c_can_obj_update(struct net_device *dev, int iface, u32 cmd, u32 obj)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800269{
Thomas Gleixner7af28632014-04-11 08:13:20 +0000270 struct c_can_priv *priv = netdev_priv(dev);
271 int cnt, reg = C_CAN_IFACE(COMREQ_REG, iface);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800272
Thomas Gleixner7af28632014-04-11 08:13:20 +0000273 priv->write_reg(priv, reg + 1, cmd);
274 priv->write_reg(priv, reg, obj);
275
276 for (cnt = MIN_TIMEOUT_VALUE; cnt; cnt--) {
277 if (!(priv->read_reg(priv, reg) & IF_COMR_BUSY))
278 return;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800279 udelay(1);
280 }
Thomas Gleixner7af28632014-04-11 08:13:20 +0000281 netdev_err(dev, "Updating object timed out\n");
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800282
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800283}
284
Thomas Gleixner7af28632014-04-11 08:13:20 +0000285static inline void c_can_object_get(struct net_device *dev, int iface,
286 u32 obj, u32 cmd)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800287{
Thomas Gleixner7af28632014-04-11 08:13:20 +0000288 c_can_obj_update(dev, iface, cmd, obj);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800289}
290
Thomas Gleixner7af28632014-04-11 08:13:20 +0000291static inline void c_can_object_put(struct net_device *dev, int iface,
292 u32 obj, u32 cmd)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800293{
Thomas Gleixner7af28632014-04-11 08:13:20 +0000294 c_can_obj_update(dev, iface, cmd | IF_COMM_WR, obj);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800295}
296
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000297static void c_can_write_msg_object(struct net_device *dev, int iface,
298 struct can_frame *frame, int obj)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800299{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800300 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000301 u16 ctrl = IF_MCONT_TX | frame->can_dlc;
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000302 u32 arb = IF_ARB_MSGVAL;
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000303 int i;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800304
305 if (frame->can_id & CAN_EFF_FLAG) {
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000306 arb |= frame->can_id & CAN_EFF_MASK;
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000307 arb |= IF_ARB_MSGXTD;
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000308 } else {
309 arb |= (frame->can_id & CAN_SFF_MASK) << 18;
310 }
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800311
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000312 if (!(frame->can_id & CAN_RTR_FLAG))
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000313 arb |= IF_ARB_TRANSMIT;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800314
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000315 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), arb);
316 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), arb >> 16);
317
318 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800319
320 for (i = 0; i < frame->can_dlc; i += 2) {
AnilKumar Ch33f81002012-05-29 11:13:15 +0530321 priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800322 frame->data[i] | (frame->data[i + 1] << 8));
323 }
324
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000325 c_can_object_put(dev, iface, obj, IF_COMM_TX);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800326}
327
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800328static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
Thomas Gleixner6b48ff82014-04-11 08:13:14 +0000329 int iface)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800330{
331 int i;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800332
Thomas Gleixner6b48ff82014-04-11 08:13:14 +0000333 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++)
334 c_can_object_get(dev, iface, i, IF_COMM_CLR_NEWDAT);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800335}
336
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000337static int c_can_handle_lost_msg_obj(struct net_device *dev,
338 int iface, int objno, u32 ctrl)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800339{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800340 struct net_device_stats *stats = &dev->stats;
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000341 struct c_can_priv *priv = netdev_priv(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800342 struct can_frame *frame;
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000343 struct sk_buff *skb;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800344
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000345 ctrl &= ~(IF_MCONT_MSGLST | IF_MCONT_INTPND | IF_MCONT_NEWDAT);
346 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
Thomas Gleixner640916d2014-03-18 17:19:09 +0000347 c_can_object_put(dev, iface, objno, IF_COMM_CONTROL);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800348
Thomas Gleixner1da394d2014-04-11 08:13:13 +0000349 stats->rx_errors++;
350 stats->rx_over_errors++;
351
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800352 /* create an error msg */
353 skb = alloc_can_err_skb(dev, &frame);
354 if (unlikely(!skb))
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000355 return 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800356
357 frame->can_id |= CAN_ERR_CRTL;
358 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800359
360 netif_receive_skb(skb);
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000361 return 1;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800362}
363
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000364static int c_can_read_msg_object(struct net_device *dev, int iface, u32 ctrl)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800365{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800366 struct net_device_stats *stats = &dev->stats;
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000367 struct c_can_priv *priv = netdev_priv(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800368 struct can_frame *frame;
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000369 struct sk_buff *skb;
370 u32 arb, data;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800371
372 skb = alloc_can_skb(dev, &frame);
373 if (!skb) {
374 stats->rx_dropped++;
375 return -ENOMEM;
376 }
377
378 frame->can_dlc = get_can_dlc(ctrl & 0x0F);
379
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000380 arb = priv->read_reg(priv, C_CAN_IFACE(ARB1_REG, iface));
381 arb |= priv->read_reg(priv, C_CAN_IFACE(ARB2_REG, iface)) << 16;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800382
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000383 if (arb & IF_ARB_MSGXTD)
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000384 frame->can_id = (arb & CAN_EFF_MASK) | CAN_EFF_FLAG;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800385 else
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000386 frame->can_id = (arb >> 18) & CAN_SFF_MASK;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800387
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000388 if (arb & IF_ARB_TRANSMIT) {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800389 frame->can_id |= CAN_RTR_FLAG;
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000390 } else {
391 int i, dreg = C_CAN_IFACE(DATA1_REG, iface);
392
393 for (i = 0; i < frame->can_dlc; i += 2, dreg ++) {
394 data = priv->read_reg(priv, dreg);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800395 frame->data[i] = data;
396 frame->data[i + 1] = data >> 8;
397 }
398 }
399
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800400 stats->rx_packets++;
401 stats->rx_bytes += frame->can_dlc;
Thomas Gleixner9c648632014-04-11 08:13:12 +0000402
403 netif_receive_skb(skb);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800404 return 0;
405}
406
407static void c_can_setup_receive_object(struct net_device *dev, int iface,
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000408 u32 obj, u32 mask, u32 id, u32 mcont)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800409{
410 struct c_can_priv *priv = netdev_priv(dev);
411
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000412 mask |= BIT(29);
413 priv->write_reg(priv, C_CAN_IFACE(MASK1_REG, iface), mask);
414 priv->write_reg(priv, C_CAN_IFACE(MASK2_REG, iface), mask >> 16);
Alexander Stein2bd3bc42012-12-13 10:06:10 +0100415
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000416 id |= IF_ARB_MSGVAL;
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000417 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), id);
418 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), id >> 16);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800419
AnilKumar Ch33f81002012-05-29 11:13:15 +0530420 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000421 c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800422}
423
Thomas Gleixnerb07faaa2014-04-11 08:13:19 +0000424static void c_can_inval_msg_object(struct net_device *dev, int iface, int obj)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800425{
426 struct c_can_priv *priv = netdev_priv(dev);
427
AnilKumar Ch33f81002012-05-29 11:13:15 +0530428 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
429 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
430 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800431
Thomas Gleixnerb07faaa2014-04-11 08:13:19 +0000432 c_can_object_put(dev, iface, obj, IF_COMM_INVAL);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800433}
434
435static inline int c_can_is_next_tx_obj_busy(struct c_can_priv *priv, int objno)
436{
AnilKumar Ch33f81002012-05-29 11:13:15 +0530437 int val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800438
439 /*
440 * as transmission request register's bit n-1 corresponds to
441 * message object n, we need to handle the same properly.
442 */
443 if (val & (1 << (objno - 1)))
444 return 1;
445
446 return 0;
447}
448
449static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
450 struct net_device *dev)
451{
452 u32 msg_obj_no;
453 struct c_can_priv *priv = netdev_priv(dev);
454 struct can_frame *frame = (struct can_frame *)skb->data;
455
456 if (can_dropped_invalid_skb(dev, skb))
457 return NETDEV_TX_OK;
458
Thomas Gleixnerbf88a2062014-03-18 17:19:12 +0000459 spin_lock_bh(&priv->xmit_lock);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800460 msg_obj_no = get_tx_next_msg_obj(priv);
461
462 /* prepare message object for transmission */
Thomas Gleixner640916d2014-03-18 17:19:09 +0000463 c_can_write_msg_object(dev, IF_TX, frame, msg_obj_no);
Thomas Gleixner90247002014-03-18 17:19:14 +0000464 priv->dlc[msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST] = frame->can_dlc;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800465 can_put_echo_skb(skb, dev, msg_obj_no - C_CAN_MSG_OBJ_TX_FIRST);
466
467 /*
468 * we have to stop the queue in case of a wrap around or
469 * if the next TX message object is still in use
470 */
471 priv->tx_next++;
472 if (c_can_is_next_tx_obj_busy(priv, get_tx_next_msg_obj(priv)) ||
473 (priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) == 0)
474 netif_stop_queue(dev);
Thomas Gleixnerbf88a2062014-03-18 17:19:12 +0000475 spin_unlock_bh(&priv->xmit_lock);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800476
477 return NETDEV_TX_OK;
478}
479
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000480static int c_can_wait_for_ctrl_init(struct net_device *dev,
481 struct c_can_priv *priv, u32 init)
482{
483 int retry = 0;
484
485 while (init != (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_INIT)) {
486 udelay(10);
487 if (retry++ > 1000) {
488 netdev_err(dev, "CCTRL: set CONTROL_INIT failed\n");
489 return -EIO;
490 }
491 }
492 return 0;
493}
494
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800495static int c_can_set_bittiming(struct net_device *dev)
496{
497 unsigned int reg_btr, reg_brpe, ctrl_save;
498 u8 brp, brpe, sjw, tseg1, tseg2;
499 u32 ten_bit_brp;
500 struct c_can_priv *priv = netdev_priv(dev);
501 const struct can_bittiming *bt = &priv->can.bittiming;
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000502 int res;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800503
504 /* c_can provides a 6-bit brp and 4-bit brpe fields */
505 ten_bit_brp = bt->brp - 1;
506 brp = ten_bit_brp & BTR_BRP_MASK;
507 brpe = ten_bit_brp >> 6;
508
509 sjw = bt->sjw - 1;
510 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
511 tseg2 = bt->phase_seg2 - 1;
512 reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
513 (tseg2 << BTR_TSEG2_SHIFT);
514 reg_brpe = brpe & BRP_EXT_BRPE_MASK;
515
516 netdev_info(dev,
517 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
518
AnilKumar Ch33f81002012-05-29 11:13:15 +0530519 ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000520 ctrl_save &= ~CONTROL_INIT;
521 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_CCE | CONTROL_INIT);
522 res = c_can_wait_for_ctrl_init(dev, priv, CONTROL_INIT);
523 if (res)
524 return res;
525
AnilKumar Ch33f81002012-05-29 11:13:15 +0530526 priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
527 priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
528 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800529
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000530 return c_can_wait_for_ctrl_init(dev, priv, 0);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800531}
532
533/*
534 * Configure C_CAN message objects for Tx and Rx purposes:
535 * C_CAN provides a total of 32 message objects that can be configured
536 * either for Tx or Rx purposes. Here the first 16 message objects are used as
537 * a reception FIFO. The end of reception FIFO is signified by the EoB bit
538 * being SET. The remaining 16 message objects are kept aside for Tx purposes.
539 * See user guide document for further details on configuring message
540 * objects.
541 */
542static void c_can_configure_msg_objects(struct net_device *dev)
543{
544 int i;
545
546 /* first invalidate all message objects */
547 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
Thomas Gleixner640916d2014-03-18 17:19:09 +0000548 c_can_inval_msg_object(dev, IF_RX, i);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800549
550 /* setup receive message objects */
551 for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000552 c_can_setup_receive_object(dev, IF_RX, i, 0, 0, IF_MCONT_RCV);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800553
Thomas Gleixner640916d2014-03-18 17:19:09 +0000554 c_can_setup_receive_object(dev, IF_RX, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000555 IF_MCONT_RCV_EOB);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800556}
557
558/*
559 * Configure C_CAN chip:
560 * - enable/disable auto-retransmission
561 * - set operating mode
562 * - configure message objects
563 */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100564static int c_can_chip_config(struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800565{
566 struct c_can_priv *priv = netdev_priv(dev);
567
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +0000568 /* enable automatic retransmission */
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000569 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_ENABLE_AR);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800570
Dan Carpenterd9cb9bd2012-06-15 00:20:44 +0000571 if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
572 (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800573 /* loopback + silent mode : useful for hot self-test */
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000574 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
575 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK | TEST_SILENT);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800576 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
577 /* loopback mode : useful for self-test function */
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000578 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530579 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800580 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
581 /* silent mode : bus-monitoring mode */
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000582 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530583 priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000584 }
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800585
586 /* configure message objects */
587 c_can_configure_msg_objects(dev);
588
589 /* set a `lec` value so that we can check for updates later */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530590 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800591
592 /* set bittiming params */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100593 return c_can_set_bittiming(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800594}
595
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100596static int c_can_start(struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800597{
598 struct c_can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100599 int err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800600
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800601 /* basic c_can configuration */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100602 err = c_can_chip_config(dev);
603 if (err)
604 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800605
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000606 /* Setup the command for new messages */
607 priv->comm_rcv_high = priv->type != BOSCH_D_CAN ?
608 IF_COMM_RCV_LOW : IF_COMM_RCV_HIGH;
609
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800610 priv->can.state = CAN_STATE_ERROR_ACTIVE;
611
Thomas Gleixnerfa39b542014-04-11 08:13:15 +0000612 /* reset tx helper pointers and the rx mask */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800613 priv->tx_next = priv->tx_echo = 0;
Thomas Gleixnerfa39b542014-04-11 08:13:15 +0000614 priv->rxmasked = 0;
Jan Altenberg4f2d56c2011-03-21 18:19:26 -0700615
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100616 return 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800617}
618
619static void c_can_stop(struct net_device *dev)
620{
621 struct c_can_priv *priv = netdev_priv(dev);
622
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000623 c_can_irq_control(priv, false);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800624 priv->can.state = CAN_STATE_STOPPED;
625}
626
627static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
628{
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000629 struct c_can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100630 int err;
631
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800632 switch (mode) {
633 case CAN_MODE_START:
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100634 err = c_can_start(dev);
635 if (err)
636 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800637 netif_wake_queue(dev);
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000638 c_can_irq_control(priv, true);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800639 break;
640 default:
641 return -EOPNOTSUPP;
642 }
643
644 return 0;
645}
646
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100647static int __c_can_get_berr_counter(const struct net_device *dev,
648 struct can_berr_counter *bec)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800649{
650 unsigned int reg_err_counter;
651 struct c_can_priv *priv = netdev_priv(dev);
652
AnilKumar Ch33f81002012-05-29 11:13:15 +0530653 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800654 bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
655 ERR_CNT_REC_SHIFT;
656 bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
657
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100658 return 0;
659}
660
661static int c_can_get_berr_counter(const struct net_device *dev,
662 struct can_berr_counter *bec)
663{
664 struct c_can_priv *priv = netdev_priv(dev);
665 int err;
666
667 c_can_pm_runtime_get_sync(priv);
668 err = __c_can_get_berr_counter(dev, bec);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +0530669 c_can_pm_runtime_put_sync(priv);
670
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100671 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800672}
673
674/*
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800675 * priv->tx_echo holds the number of the oldest can_frame put for
676 * transmission into the hardware, but not yet ACKed by the CAN tx
677 * complete IRQ.
678 *
679 * We iterate from priv->tx_echo to priv->tx_next and check if the
680 * packet has been transmitted, echo it back to the CAN framework.
AnilKumar Ch617cacc2012-05-23 17:45:09 +0530681 * If we discover a not yet transmitted packet, stop looking for more.
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800682 */
683static void c_can_do_tx(struct net_device *dev)
684{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800685 struct c_can_priv *priv = netdev_priv(dev);
686 struct net_device_stats *stats = &dev->stats;
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000687 u32 val, obj, pkts = 0, bytes = 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800688
Thomas Gleixnerbf88a2062014-03-18 17:19:12 +0000689 spin_lock_bh(&priv->xmit_lock);
690
691 for (; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000692 obj = get_tx_echo_msg_obj(priv->tx_echo);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530693 val = c_can_read_reg32(priv, C_CAN_TXRQST1_REG);
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000694
695 if (val & (1 << (obj - 1)))
AnilKumar Ch617cacc2012-05-23 17:45:09 +0530696 break;
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000697
698 can_get_echo_skb(dev, obj - C_CAN_MSG_OBJ_TX_FIRST);
699 bytes += priv->dlc[obj - C_CAN_MSG_OBJ_TX_FIRST];
700 pkts++;
701 c_can_inval_msg_object(dev, IF_TX, obj);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800702 }
703
704 /* restart queue if wrap-up or if queue stalled on last pkt */
705 if (((priv->tx_next & C_CAN_NEXT_MSG_OBJ_MASK) != 0) ||
706 ((priv->tx_echo & C_CAN_NEXT_MSG_OBJ_MASK) == 0))
707 netif_wake_queue(dev);
Thomas Gleixnerbf88a2062014-03-18 17:19:12 +0000708
709 spin_unlock_bh(&priv->xmit_lock);
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000710
711 if (pkts) {
712 stats->tx_bytes += bytes;
713 stats->tx_packets += pkts;
714 can_led_event(dev, CAN_LED_EVENT_TX);
715 }
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800716}
717
718/*
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000719 * If we have a gap in the pending bits, that means we either
720 * raced with the hardware or failed to readout all upper
721 * objects in the last run due to quota limit.
722 */
723static u32 c_can_adjust_pending(u32 pend)
724{
725 u32 weight, lasts;
726
727 if (pend == RECEIVE_OBJECT_BITS)
728 return pend;
729
730 /*
731 * If the last set bit is larger than the number of pending
732 * bits we have a gap.
733 */
734 weight = hweight32(pend);
735 lasts = fls(pend);
736
737 /* If the bits are linear, nothing to do */
738 if (lasts == weight)
739 return pend;
740
741 /*
742 * Find the first set bit after the gap. We walk backwards
743 * from the last set bit.
744 */
745 for (lasts--; pend & (1 << (lasts - 1)); lasts--);
746
747 return pend & ~((1 << lasts) - 1);
748}
749
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000750static inline void c_can_rx_object_get(struct net_device *dev,
751 struct c_can_priv *priv, u32 obj)
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000752{
753#ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
754 if (obj < C_CAN_MSG_RX_LOW_LAST)
755 c_can_object_get(dev, IF_RX, obj, IF_COMM_RCV_LOW);
756 else
757#endif
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000758 c_can_object_get(dev, IF_RX, obj, priv->comm_rcv_high);
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000759}
760
761static inline void c_can_rx_finalize(struct net_device *dev,
762 struct c_can_priv *priv, u32 obj)
763{
764#ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
765 if (obj < C_CAN_MSG_RX_LOW_LAST)
766 priv->rxmasked |= BIT(obj - 1);
767 else if (obj == C_CAN_MSG_RX_LOW_LAST) {
768 priv->rxmasked = 0;
769 /* activate all lower message objects */
770 c_can_activate_all_lower_rx_msg_obj(dev, IF_RX);
771 }
772#endif
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000773 if (priv->type != BOSCH_D_CAN)
774 c_can_object_get(dev, IF_RX, obj, IF_COMM_CLR_NEWDAT);
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000775}
776
Thomas Gleixner520f5702014-03-18 19:27:42 +0100777static int c_can_read_objects(struct net_device *dev, struct c_can_priv *priv,
778 u32 pend, int quota)
779{
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000780 u32 pkts = 0, ctrl, obj;
Thomas Gleixner520f5702014-03-18 19:27:42 +0100781
782 while ((obj = ffs(pend)) && quota > 0) {
783 pend &= ~BIT(obj - 1);
784
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000785 c_can_rx_object_get(dev, priv, obj);
Thomas Gleixner520f5702014-03-18 19:27:42 +0100786 ctrl = priv->read_reg(priv, C_CAN_IFACE(MSGCTRL_REG, IF_RX));
787
788 if (ctrl & IF_MCONT_MSGLST) {
789 int n = c_can_handle_lost_msg_obj(dev, IF_RX, obj, ctrl);
790
791 pkts += n;
792 quota -= n;
793 continue;
794 }
795
796 /*
797 * This really should not happen, but this covers some
798 * odd HW behaviour. Do not remove that unless you
799 * want to brick your machine.
800 */
801 if (!(ctrl & IF_MCONT_NEWDAT))
802 continue;
803
804 /* read the data from the message object */
805 c_can_read_msg_object(dev, IF_RX, ctrl);
806
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000807 c_can_rx_finalize(dev, priv, obj);
Thomas Gleixner520f5702014-03-18 19:27:42 +0100808
809 pkts++;
810 quota--;
811 }
812
813 return pkts;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800814}
815
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000816static inline u32 c_can_get_pending(struct c_can_priv *priv)
817{
818 u32 pend = priv->read_reg(priv, C_CAN_NEWDAT1_REG);
819
820#ifdef CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING
821 pend &= ~priv->rxmasked;
822#endif
823 return pend;
824}
825
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800826/*
827 * theory of operation:
828 *
829 * c_can core saves a received CAN message into the first free message
830 * object it finds free (starting with the lowest). Bits NEWDAT and
831 * INTPND are set for this message object indicating that a new message
832 * has arrived. To work-around this issue, we keep two groups of message
833 * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
834 *
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000835 * If CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING = y
836 *
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800837 * To ensure in-order frame reception we use the following
838 * approach while re-activating a message object to receive further
839 * frames:
840 * - if the current message object number is lower than
841 * C_CAN_MSG_RX_LOW_LAST, do not clear the NEWDAT bit while clearing
842 * the INTPND bit.
843 * - if the current message object number is equal to
844 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of all lower
845 * receive message objects.
846 * - if the current message object number is greater than
847 * C_CAN_MSG_RX_LOW_LAST then clear the NEWDAT bit of
848 * only this message object.
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000849 *
850 * This can cause packet loss!
851 *
852 * If CONFIG_CAN_C_CAN_STRICT_FRAME_ORDERING = n
853 *
854 * We clear the newdat bit right away.
855 *
856 * This can result in packet reordering when the readout is slow.
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800857 */
858static int c_can_do_rx_poll(struct net_device *dev, int quota)
859{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800860 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixner520f5702014-03-18 19:27:42 +0100861 u32 pkts = 0, pend = 0, toread, n;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800862
Markus Pargmann4ce78a82013-11-01 10:36:36 +0100863 /*
864 * It is faster to read only one 16bit register. This is only possible
865 * for a maximum number of 16 objects.
866 */
867 BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST > 16,
868 "Implementation does not support more message objects than 16");
869
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000870 while (quota > 0) {
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000871 if (!pend) {
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000872 pend = c_can_get_pending(priv);
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000873 if (!pend)
Thomas Gleixner520f5702014-03-18 19:27:42 +0100874 break;
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000875 /*
876 * If the pending field has a gap, handle the
877 * bits above the gap first.
878 */
Thomas Gleixner520f5702014-03-18 19:27:42 +0100879 toread = c_can_adjust_pending(pend);
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000880 } else {
Thomas Gleixner520f5702014-03-18 19:27:42 +0100881 toread = pend;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800882 }
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000883 /* Remove the bits from pend */
Thomas Gleixner520f5702014-03-18 19:27:42 +0100884 pend &= ~toread;
885 /* Read the objects */
886 n = c_can_read_objects(dev, priv, toread, quota);
887 pkts += n;
888 quota -= n;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800889 }
890
Thomas Gleixnerb1d8e432014-03-18 17:19:15 +0000891 if (pkts)
892 can_led_event(dev, CAN_LED_EVENT_RX);
893
Thomas Gleixner520f5702014-03-18 19:27:42 +0100894 return pkts;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800895}
896
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800897static int c_can_handle_state_change(struct net_device *dev,
898 enum c_can_bus_error_types error_type)
899{
900 unsigned int reg_err_counter;
901 unsigned int rx_err_passive;
902 struct c_can_priv *priv = netdev_priv(dev);
903 struct net_device_stats *stats = &dev->stats;
904 struct can_frame *cf;
905 struct sk_buff *skb;
906 struct can_berr_counter bec;
907
Thomas Gleixnerf058d542014-04-11 08:13:12 +0000908 switch (error_type) {
909 case C_CAN_ERROR_WARNING:
910 /* error warning state */
911 priv->can.can_stats.error_warning++;
912 priv->can.state = CAN_STATE_ERROR_WARNING;
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 break;
919 case C_CAN_BUS_OFF:
920 /* bus-off state */
921 priv->can.state = CAN_STATE_BUS_OFF;
922 can_bus_off(dev);
923 break;
924 default:
925 break;
926 }
927
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300928 /* propagate the error condition to the CAN stack */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800929 skb = alloc_can_err_skb(dev, &cf);
930 if (unlikely(!skb))
931 return 0;
932
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100933 __c_can_get_berr_counter(dev, &bec);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530934 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800935 rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
936 ERR_CNT_RP_SHIFT;
937
938 switch (error_type) {
939 case C_CAN_ERROR_WARNING:
940 /* error warning state */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800941 cf->can_id |= CAN_ERR_CRTL;
942 cf->data[1] = (bec.txerr > bec.rxerr) ?
943 CAN_ERR_CRTL_TX_WARNING :
944 CAN_ERR_CRTL_RX_WARNING;
945 cf->data[6] = bec.txerr;
946 cf->data[7] = bec.rxerr;
947
948 break;
949 case C_CAN_ERROR_PASSIVE:
950 /* error passive state */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800951 cf->can_id |= CAN_ERR_CRTL;
952 if (rx_err_passive)
953 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
954 if (bec.txerr > 127)
955 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
956
957 cf->data[6] = bec.txerr;
958 cf->data[7] = bec.rxerr;
959 break;
960 case C_CAN_BUS_OFF:
961 /* bus-off state */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800962 cf->can_id |= CAN_ERR_BUSOFF;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800963 can_bus_off(dev);
964 break;
965 default:
966 break;
967 }
968
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800969 stats->rx_packets++;
970 stats->rx_bytes += cf->can_dlc;
Thomas Gleixner9c648632014-04-11 08:13:12 +0000971 netif_receive_skb(skb);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800972
973 return 1;
974}
975
976static int c_can_handle_bus_err(struct net_device *dev,
977 enum c_can_lec_type lec_type)
978{
979 struct c_can_priv *priv = netdev_priv(dev);
980 struct net_device_stats *stats = &dev->stats;
981 struct can_frame *cf;
982 struct sk_buff *skb;
983
984 /*
985 * early exit if no lec update or no error.
986 * no lec update means that no CAN bus event has been detected
987 * since CPU wrote 0x7 value to status reg.
988 */
989 if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
990 return 0;
991
Thomas Gleixner097aec12014-04-11 08:13:13 +0000992 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
993 return 0;
994
Thomas Gleixner1da394d2014-04-11 08:13:13 +0000995 /* common for all type of bus errors */
996 priv->can.can_stats.bus_error++;
997 stats->rx_errors++;
998
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300999 /* propagate the error condition to the CAN stack */
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001000 skb = alloc_can_err_skb(dev, &cf);
1001 if (unlikely(!skb))
1002 return 0;
1003
1004 /*
1005 * check for 'last error code' which tells us the
1006 * type of the last error to occur on the CAN bus
1007 */
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001008 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
1009 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
1010
1011 switch (lec_type) {
1012 case LEC_STUFF_ERROR:
1013 netdev_dbg(dev, "stuff error\n");
1014 cf->data[2] |= CAN_ERR_PROT_STUFF;
1015 break;
1016 case LEC_FORM_ERROR:
1017 netdev_dbg(dev, "form error\n");
1018 cf->data[2] |= CAN_ERR_PROT_FORM;
1019 break;
1020 case LEC_ACK_ERROR:
1021 netdev_dbg(dev, "ack error\n");
Olivier Sobrie6ea45882013-01-18 09:32:39 +01001022 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001023 CAN_ERR_PROT_LOC_ACK_DEL);
1024 break;
1025 case LEC_BIT1_ERROR:
1026 netdev_dbg(dev, "bit1 error\n");
1027 cf->data[2] |= CAN_ERR_PROT_BIT1;
1028 break;
1029 case LEC_BIT0_ERROR:
1030 netdev_dbg(dev, "bit0 error\n");
1031 cf->data[2] |= CAN_ERR_PROT_BIT0;
1032 break;
1033 case LEC_CRC_ERROR:
1034 netdev_dbg(dev, "CRC error\n");
Olivier Sobrie6ea45882013-01-18 09:32:39 +01001035 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001036 CAN_ERR_PROT_LOC_CRC_DEL);
1037 break;
1038 default:
1039 break;
1040 }
1041
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001042 stats->rx_packets++;
1043 stats->rx_bytes += cf->can_dlc;
Thomas Gleixner9c648632014-04-11 08:13:12 +00001044 netif_receive_skb(skb);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001045 return 1;
1046}
1047
1048static int c_can_poll(struct napi_struct *napi, int quota)
1049{
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001050 struct net_device *dev = napi->dev;
1051 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001052 u16 curr, last = priv->last_status;
1053 int work_done = 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001054
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001055 priv->last_status = curr = priv->read_reg(priv, C_CAN_STS_REG);
1056 /* Ack status on C_CAN. D_CAN is self clearing */
1057 if (priv->type != BOSCH_D_CAN)
1058 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001059
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001060 /* handle state changes */
1061 if ((curr & STATUS_EWARN) && (!(last & STATUS_EWARN))) {
1062 netdev_dbg(dev, "entered error warning state\n");
1063 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_WARNING);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001064 }
1065
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001066 if ((curr & STATUS_EPASS) && (!(last & STATUS_EPASS))) {
1067 netdev_dbg(dev, "entered error passive state\n");
1068 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_PASSIVE);
1069 }
1070
1071 if ((curr & STATUS_BOFF) && (!(last & STATUS_BOFF))) {
1072 netdev_dbg(dev, "entered bus off state\n");
1073 work_done += c_can_handle_state_change(dev, C_CAN_BUS_OFF);
1074 goto end;
1075 }
1076
1077 /* handle bus recovery events */
1078 if ((!(curr & STATUS_BOFF)) && (last & STATUS_BOFF)) {
1079 netdev_dbg(dev, "left bus off state\n");
1080 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1081 }
1082 if ((!(curr & STATUS_EPASS)) && (last & STATUS_EPASS)) {
1083 netdev_dbg(dev, "left error passive state\n");
1084 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1085 }
1086
1087 /* handle lec errors on the bus */
1088 work_done += c_can_handle_bus_err(dev, curr & LEC_MASK);
1089
1090 /* Handle Tx/Rx events. We do this unconditionally */
1091 work_done += c_can_do_rx_poll(dev, (quota - work_done));
1092 c_can_do_tx(dev);
1093
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001094end:
1095 if (work_done < quota) {
1096 napi_complete(napi);
Thomas Gleixneref1d2e22014-04-11 08:13:11 +00001097 /* enable all IRQs if we are not in bus off state */
1098 if (priv->can.state != CAN_STATE_BUS_OFF)
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +00001099 c_can_irq_control(priv, true);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001100 }
1101
1102 return work_done;
1103}
1104
1105static irqreturn_t c_can_isr(int irq, void *dev_id)
1106{
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001107 struct net_device *dev = (struct net_device *)dev_id;
1108 struct c_can_priv *priv = netdev_priv(dev);
1109
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001110 if (!priv->read_reg(priv, C_CAN_INT_REG))
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001111 return IRQ_NONE;
1112
1113 /* disable all interrupts and schedule the NAPI */
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +00001114 c_can_irq_control(priv, false);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001115 napi_schedule(&priv->napi);
1116
1117 return IRQ_HANDLED;
1118}
1119
1120static int c_can_open(struct net_device *dev)
1121{
1122 int err;
1123 struct c_can_priv *priv = netdev_priv(dev);
1124
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301125 c_can_pm_runtime_get_sync(priv);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301126 c_can_reset_ram(priv, true);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301127
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001128 /* open the can device */
1129 err = open_candev(dev);
1130 if (err) {
1131 netdev_err(dev, "failed to open can device\n");
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301132 goto exit_open_fail;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001133 }
1134
1135 /* register interrupt handler */
1136 err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
1137 dev);
1138 if (err < 0) {
1139 netdev_err(dev, "failed to request interrupt\n");
1140 goto exit_irq_fail;
1141 }
1142
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001143 /* start the c_can controller */
1144 err = c_can_start(dev);
1145 if (err)
1146 goto exit_start_fail;
AnilKumar Chf461f272012-05-23 17:45:11 +05301147
Fabio Baltieri5090f802012-12-18 18:51:01 +01001148 can_led_event(dev, CAN_LED_EVENT_OPEN);
1149
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001150 napi_enable(&priv->napi);
Thomas Gleixnerbed11db2014-04-11 08:13:10 +00001151 /* enable status change, error and module interrupts */
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +00001152 c_can_irq_control(priv, true);
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);
Thomas Gleixnerbf88a2062014-03-18 17:19:12 +00001195 spin_lock_init(&priv->xmit_lock);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001196 netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
1197
1198 priv->dev = dev;
1199 priv->can.bittiming_const = &c_can_bittiming_const;
1200 priv->can.do_set_mode = c_can_set_mode;
1201 priv->can.do_get_berr_counter = c_can_get_berr_counter;
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +00001202 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001203 CAN_CTRLMODE_LISTENONLY |
1204 CAN_CTRLMODE_BERR_REPORTING;
1205
1206 return dev;
1207}
1208EXPORT_SYMBOL_GPL(alloc_c_can_dev);
1209
AnilKumar Ch82120032012-09-21 15:29:01 +05301210#ifdef CONFIG_PM
1211int c_can_power_down(struct net_device *dev)
1212{
1213 u32 val;
1214 unsigned long time_out;
1215 struct c_can_priv *priv = netdev_priv(dev);
1216
1217 if (!(dev->flags & IFF_UP))
1218 return 0;
1219
1220 WARN_ON(priv->type != BOSCH_D_CAN);
1221
1222 /* set PDR value so the device goes to power down mode */
1223 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1224 val |= CONTROL_EX_PDR;
1225 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1226
1227 /* Wait for the PDA bit to get set */
1228 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1229 while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1230 time_after(time_out, jiffies))
1231 cpu_relax();
1232
1233 if (time_after(jiffies, time_out))
1234 return -ETIMEDOUT;
1235
1236 c_can_stop(dev);
1237
AnilKumar Ch52cde852012-11-21 11:14:10 +05301238 c_can_reset_ram(priv, false);
AnilKumar Ch82120032012-09-21 15:29:01 +05301239 c_can_pm_runtime_put_sync(priv);
1240
1241 return 0;
1242}
1243EXPORT_SYMBOL_GPL(c_can_power_down);
1244
1245int c_can_power_up(struct net_device *dev)
1246{
1247 u32 val;
1248 unsigned long time_out;
1249 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixnerbed11db2014-04-11 08:13:10 +00001250 int ret;
AnilKumar Ch82120032012-09-21 15:29:01 +05301251
1252 if (!(dev->flags & IFF_UP))
1253 return 0;
1254
1255 WARN_ON(priv->type != BOSCH_D_CAN);
1256
1257 c_can_pm_runtime_get_sync(priv);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301258 c_can_reset_ram(priv, true);
AnilKumar Ch82120032012-09-21 15:29:01 +05301259
1260 /* Clear PDR and INIT bits */
1261 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1262 val &= ~CONTROL_EX_PDR;
1263 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1264 val = priv->read_reg(priv, C_CAN_CTRL_REG);
1265 val &= ~CONTROL_INIT;
1266 priv->write_reg(priv, C_CAN_CTRL_REG, val);
1267
1268 /* Wait for the PDA bit to get clear */
1269 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1270 while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1271 time_after(time_out, jiffies))
1272 cpu_relax();
1273
1274 if (time_after(jiffies, time_out))
1275 return -ETIMEDOUT;
1276
Thomas Gleixnerbed11db2014-04-11 08:13:10 +00001277 ret = c_can_start(dev);
1278 if (!ret)
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +00001279 c_can_irq_control(priv, true);
Thomas Gleixnerbed11db2014-04-11 08:13:10 +00001280
1281 return ret;
AnilKumar Ch82120032012-09-21 15:29:01 +05301282}
1283EXPORT_SYMBOL_GPL(c_can_power_up);
1284#endif
1285
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001286void free_c_can_dev(struct net_device *dev)
1287{
Marc Kleine-Buddef29b4232014-03-18 19:13:59 +01001288 struct c_can_priv *priv = netdev_priv(dev);
1289
1290 netif_napi_del(&priv->napi);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001291 free_candev(dev);
1292}
1293EXPORT_SYMBOL_GPL(free_c_can_dev);
1294
1295static const struct net_device_ops c_can_netdev_ops = {
1296 .ndo_open = c_can_open,
1297 .ndo_stop = c_can_close,
1298 .ndo_start_xmit = c_can_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001299 .ndo_change_mtu = can_change_mtu,
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001300};
1301
1302int register_c_can_dev(struct net_device *dev)
1303{
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301304 struct c_can_priv *priv = netdev_priv(dev);
1305 int err;
1306
1307 c_can_pm_runtime_enable(priv);
1308
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001309 dev->flags |= IFF_ECHO; /* we support local echo */
1310 dev->netdev_ops = &c_can_netdev_ops;
1311
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301312 err = register_candev(dev);
1313 if (err)
1314 c_can_pm_runtime_disable(priv);
Fabio Baltieri5090f802012-12-18 18:51:01 +01001315 else
1316 devm_can_led_init(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301317
1318 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001319}
1320EXPORT_SYMBOL_GPL(register_c_can_dev);
1321
1322void unregister_c_can_dev(struct net_device *dev)
1323{
1324 struct c_can_priv *priv = netdev_priv(dev);
1325
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001326 unregister_candev(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301327
1328 c_can_pm_runtime_disable(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001329}
1330EXPORT_SYMBOL_GPL(unregister_c_can_dev);
1331
1332MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1333MODULE_LICENSE("GPL v2");
1334MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");