blob: f94a9fa60488ed8e23523f5f8c3665133f039dbb [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>
Roger Quadros3973c522014-11-14 17:40:13 +020038#include <linux/pinctrl/consumer.h>
Bhupesh Sharma881ff672011-02-13 22:51:44 -080039
40#include <linux/can.h>
41#include <linux/can/dev.h>
42#include <linux/can/error.h>
Fabio Baltieri5090f802012-12-18 18:51:01 +010043#include <linux/can/led.h>
Bhupesh Sharma881ff672011-02-13 22:51:44 -080044
45#include "c_can.h"
46
AnilKumar Ch33f81002012-05-29 11:13:15 +053047/* Number of interface registers */
48#define IF_ENUM_REG_LEN 11
49#define C_CAN_IFACE(reg, iface) (C_CAN_IF1_##reg + (iface) * IF_ENUM_REG_LEN)
50
AnilKumar Ch82120032012-09-21 15:29:01 +053051/* control extension register D_CAN specific */
52#define CONTROL_EX_PDR BIT(8)
53
Bhupesh Sharma881ff672011-02-13 22:51:44 -080054/* control register */
55#define CONTROL_TEST BIT(7)
56#define CONTROL_CCE BIT(6)
57#define CONTROL_DISABLE_AR BIT(5)
58#define CONTROL_ENABLE_AR (0 << 5)
59#define CONTROL_EIE BIT(3)
60#define CONTROL_SIE BIT(2)
61#define CONTROL_IE BIT(1)
62#define CONTROL_INIT BIT(0)
63
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +000064#define CONTROL_IRQMSK (CONTROL_EIE | CONTROL_IE | CONTROL_SIE)
65
Bhupesh Sharma881ff672011-02-13 22:51:44 -080066/* test register */
67#define TEST_RX BIT(7)
68#define TEST_TX1 BIT(6)
69#define TEST_TX2 BIT(5)
70#define TEST_LBACK BIT(4)
71#define TEST_SILENT BIT(3)
72#define TEST_BASIC BIT(2)
73
74/* status register */
AnilKumar Ch82120032012-09-21 15:29:01 +053075#define STATUS_PDA BIT(10)
Bhupesh Sharma881ff672011-02-13 22:51:44 -080076#define STATUS_BOFF BIT(7)
77#define STATUS_EWARN BIT(6)
78#define STATUS_EPASS BIT(5)
79#define STATUS_RXOK BIT(4)
80#define STATUS_TXOK BIT(3)
81
82/* error counter register */
83#define ERR_CNT_TEC_MASK 0xff
84#define ERR_CNT_TEC_SHIFT 0
85#define ERR_CNT_REC_SHIFT 8
86#define ERR_CNT_REC_MASK (0x7f << ERR_CNT_REC_SHIFT)
87#define ERR_CNT_RP_SHIFT 15
88#define ERR_CNT_RP_MASK (0x1 << ERR_CNT_RP_SHIFT)
89
90/* bit-timing register */
91#define BTR_BRP_MASK 0x3f
92#define BTR_BRP_SHIFT 0
93#define BTR_SJW_SHIFT 6
94#define BTR_SJW_MASK (0x3 << BTR_SJW_SHIFT)
95#define BTR_TSEG1_SHIFT 8
96#define BTR_TSEG1_MASK (0xf << BTR_TSEG1_SHIFT)
97#define BTR_TSEG2_SHIFT 12
98#define BTR_TSEG2_MASK (0x7 << BTR_TSEG2_SHIFT)
99
100/* brp extension register */
101#define BRP_EXT_BRPE_MASK 0x0f
102#define BRP_EXT_BRPE_SHIFT 0
103
104/* IFx command request */
105#define IF_COMR_BUSY BIT(15)
106
107/* IFx command mask */
108#define IF_COMM_WR BIT(7)
109#define IF_COMM_MASK BIT(6)
110#define IF_COMM_ARB BIT(5)
111#define IF_COMM_CONTROL BIT(4)
112#define IF_COMM_CLR_INT_PND BIT(3)
113#define IF_COMM_TXRQST BIT(2)
Thomas Gleixner6b48ff82014-04-11 08:13:14 +0000114#define IF_COMM_CLR_NEWDAT IF_COMM_TXRQST
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800115#define IF_COMM_DATAA BIT(1)
116#define IF_COMM_DATAB BIT(0)
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000117
118/* TX buffer setup */
119#define IF_COMM_TX (IF_COMM_ARB | IF_COMM_CONTROL | \
120 IF_COMM_TXRQST | \
121 IF_COMM_DATAA | IF_COMM_DATAB)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800122
Thomas Gleixnerc0a9f4d2014-03-18 17:19:13 +0000123/* For the low buffers we clear the interrupt bit, but keep newdat */
124#define IF_COMM_RCV_LOW (IF_COMM_MASK | IF_COMM_ARB | \
125 IF_COMM_CONTROL | IF_COMM_CLR_INT_PND | \
126 IF_COMM_DATAA | IF_COMM_DATAB)
127
128/* For the high buffers we clear the interrupt bit and newdat */
Thomas Gleixner6b48ff82014-04-11 08:13:14 +0000129#define IF_COMM_RCV_HIGH (IF_COMM_RCV_LOW | IF_COMM_CLR_NEWDAT)
Thomas Gleixnerc0a9f4d2014-03-18 17:19:13 +0000130
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000131
132/* Receive setup of message objects */
133#define IF_COMM_RCV_SETUP (IF_COMM_MASK | IF_COMM_ARB | IF_COMM_CONTROL)
134
Thomas Gleixnerb07faaa2014-04-11 08:13:19 +0000135/* Invalidation of message objects */
136#define IF_COMM_INVAL (IF_COMM_ARB | IF_COMM_CONTROL)
137
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800138/* IFx arbitration */
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000139#define IF_ARB_MSGVAL BIT(31)
140#define IF_ARB_MSGXTD BIT(30)
141#define IF_ARB_TRANSMIT BIT(29)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800142
143/* IFx message control */
144#define IF_MCONT_NEWDAT BIT(15)
145#define IF_MCONT_MSGLST BIT(14)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800146#define IF_MCONT_INTPND BIT(13)
147#define IF_MCONT_UMASK BIT(12)
148#define IF_MCONT_TXIE BIT(11)
149#define IF_MCONT_RXIE BIT(10)
150#define IF_MCONT_RMTEN BIT(9)
151#define IF_MCONT_TXRQST BIT(8)
152#define IF_MCONT_EOB BIT(7)
153#define IF_MCONT_DLC_MASK 0xf
154
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000155#define IF_MCONT_RCV (IF_MCONT_RXIE | IF_MCONT_UMASK)
156#define IF_MCONT_RCV_EOB (IF_MCONT_RCV | IF_MCONT_EOB)
157
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000158#define IF_MCONT_TX (IF_MCONT_TXIE | IF_MCONT_EOB)
159
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800160/*
Thomas Gleixner640916d2014-03-18 17:19:09 +0000161 * Use IF1 for RX and IF2 for TX
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800162 */
Thomas Gleixner640916d2014-03-18 17:19:09 +0000163#define IF_RX 0
164#define IF_TX 1
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800165
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800166/* minimum timeout for checking BUSY status */
167#define MIN_TIMEOUT_VALUE 6
168
AnilKumar Ch82120032012-09-21 15:29:01 +0530169/* Wait for ~1 sec for INIT bit */
170#define INIT_WAIT_MS 1000
171
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800172/* napi related */
173#define C_CAN_NAPI_WEIGHT C_CAN_MSG_OBJ_RX_NUM
174
175/* c_can lec values */
176enum c_can_lec_type {
177 LEC_NO_ERROR = 0,
178 LEC_STUFF_ERROR,
179 LEC_FORM_ERROR,
180 LEC_ACK_ERROR,
181 LEC_BIT1_ERROR,
182 LEC_BIT0_ERROR,
183 LEC_CRC_ERROR,
184 LEC_UNUSED,
Thomas Gleixner097aec12014-04-11 08:13:13 +0000185 LEC_MASK = LEC_UNUSED,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800186};
187
188/*
189 * c_can error types:
190 * Bus errors (BUS_OFF, ERROR_WARNING, ERROR_PASSIVE) are supported
191 */
192enum c_can_bus_error_types {
193 C_CAN_NO_ERROR = 0,
194 C_CAN_BUS_OFF,
195 C_CAN_ERROR_WARNING,
196 C_CAN_ERROR_PASSIVE,
197};
198
Marc Kleine-Budde194b9a42012-07-16 12:58:31 +0200199static const struct can_bittiming_const c_can_bittiming_const = {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800200 .name = KBUILD_MODNAME,
201 .tseg1_min = 2, /* Time segment 1 = prop_seg + phase_seg1 */
202 .tseg1_max = 16,
203 .tseg2_min = 1, /* Time segment 2 = phase_seg2 */
204 .tseg2_max = 8,
205 .sjw_max = 4,
206 .brp_min = 1,
207 .brp_max = 1024, /* 6-bit BRP field + 4-bit BRPE field*/
208 .brp_inc = 1,
209};
210
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +0530211static inline void c_can_pm_runtime_enable(const struct c_can_priv *priv)
212{
213 if (priv->device)
214 pm_runtime_enable(priv->device);
215}
216
217static inline void c_can_pm_runtime_disable(const struct c_can_priv *priv)
218{
219 if (priv->device)
220 pm_runtime_disable(priv->device);
221}
222
223static inline void c_can_pm_runtime_get_sync(const struct c_can_priv *priv)
224{
225 if (priv->device)
226 pm_runtime_get_sync(priv->device);
227}
228
229static inline void c_can_pm_runtime_put_sync(const struct c_can_priv *priv)
230{
231 if (priv->device)
232 pm_runtime_put_sync(priv->device);
233}
234
AnilKumar Ch52cde852012-11-21 11:14:10 +0530235static inline void c_can_reset_ram(const struct c_can_priv *priv, bool enable)
236{
237 if (priv->raminit)
238 priv->raminit(priv, enable);
239}
240
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000241static void c_can_irq_control(struct c_can_priv *priv, bool enable)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800242{
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000243 u32 ctrl = priv->read_reg(priv, C_CAN_CTRL_REG) & ~CONTROL_IRQMSK;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800244
245 if (enable)
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000246 ctrl |= CONTROL_IRQMSK;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800247
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000248 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800249}
250
Thomas Gleixner7af28632014-04-11 08:13:20 +0000251static void c_can_obj_update(struct net_device *dev, int iface, u32 cmd, u32 obj)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800252{
Thomas Gleixner7af28632014-04-11 08:13:20 +0000253 struct c_can_priv *priv = netdev_priv(dev);
254 int cnt, reg = C_CAN_IFACE(COMREQ_REG, iface);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800255
Pavel Machekccbc5352014-05-06 15:57:02 +0200256 priv->write_reg32(priv, reg, (cmd << 16) | obj);
Thomas Gleixner7af28632014-04-11 08:13:20 +0000257
258 for (cnt = MIN_TIMEOUT_VALUE; cnt; cnt--) {
259 if (!(priv->read_reg(priv, reg) & IF_COMR_BUSY))
260 return;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800261 udelay(1);
262 }
Thomas Gleixner7af28632014-04-11 08:13:20 +0000263 netdev_err(dev, "Updating object timed out\n");
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800264
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800265}
266
Thomas Gleixner7af28632014-04-11 08:13:20 +0000267static inline void c_can_object_get(struct net_device *dev, int iface,
268 u32 obj, u32 cmd)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800269{
Thomas Gleixner7af28632014-04-11 08:13:20 +0000270 c_can_obj_update(dev, iface, cmd, obj);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800271}
272
Thomas Gleixner7af28632014-04-11 08:13:20 +0000273static inline void c_can_object_put(struct net_device *dev, int iface,
274 u32 obj, u32 cmd)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800275{
Thomas Gleixner7af28632014-04-11 08:13:20 +0000276 c_can_obj_update(dev, iface, cmd | IF_COMM_WR, obj);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800277}
278
Thomas Gleixner93941592014-04-11 08:13:22 +0000279/*
280 * Note: According to documentation clearing TXIE while MSGVAL is set
281 * is not allowed, but works nicely on C/DCAN. And that lowers the I/O
282 * load significantly.
283 */
284static void c_can_inval_tx_object(struct net_device *dev, int iface, int obj)
285{
286 struct c_can_priv *priv = netdev_priv(dev);
287
288 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), 0);
289 c_can_object_put(dev, iface, obj, IF_COMM_INVAL);
290}
291
292static void c_can_inval_msg_object(struct net_device *dev, int iface, int obj)
293{
294 struct c_can_priv *priv = netdev_priv(dev);
295
296 priv->write_reg(priv, C_CAN_IFACE(ARB1_REG, iface), 0);
297 priv->write_reg(priv, C_CAN_IFACE(ARB2_REG, iface), 0);
298 c_can_inval_tx_object(dev, iface, obj);
299}
300
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000301static void c_can_setup_tx_object(struct net_device *dev, int iface,
Thomas Gleixner93941592014-04-11 08:13:22 +0000302 struct can_frame *frame, int idx)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800303{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800304 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000305 u16 ctrl = IF_MCONT_TX | frame->can_dlc;
Thomas Gleixner93941592014-04-11 08:13:22 +0000306 bool rtr = frame->can_id & CAN_RTR_FLAG;
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000307 u32 arb = IF_ARB_MSGVAL;
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000308 int i;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800309
310 if (frame->can_id & CAN_EFF_FLAG) {
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000311 arb |= frame->can_id & CAN_EFF_MASK;
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000312 arb |= IF_ARB_MSGXTD;
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000313 } else {
314 arb |= (frame->can_id & CAN_SFF_MASK) << 18;
315 }
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800316
Thomas Gleixner93941592014-04-11 08:13:22 +0000317 if (!rtr)
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000318 arb |= IF_ARB_TRANSMIT;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800319
Thomas Gleixner93941592014-04-11 08:13:22 +0000320 /*
321 * If we change the DIR bit, we need to invalidate the buffer
322 * first, i.e. clear the MSGVAL flag in the arbiter.
323 */
324 if (rtr != (bool)test_bit(idx, &priv->tx_dir)) {
325 u32 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
326
327 c_can_inval_msg_object(dev, iface, obj);
328 change_bit(idx, &priv->tx_dir);
329 }
330
Pavel Machekccbc5352014-05-06 15:57:02 +0200331 priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), arb);
Thomas Gleixner23ef0a82014-04-11 08:13:21 +0000332
333 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800334
335 for (i = 0; i < frame->can_dlc; i += 2) {
AnilKumar Ch33f81002012-05-29 11:13:15 +0530336 priv->write_reg(priv, C_CAN_IFACE(DATA1_REG, iface) + i / 2,
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800337 frame->data[i] | (frame->data[i + 1] << 8));
338 }
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800339}
340
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800341static inline void c_can_activate_all_lower_rx_msg_obj(struct net_device *dev,
Thomas Gleixner6b48ff82014-04-11 08:13:14 +0000342 int iface)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800343{
344 int i;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800345
Thomas Gleixner6b48ff82014-04-11 08:13:14 +0000346 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_MSG_RX_LOW_LAST; i++)
347 c_can_object_get(dev, iface, i, IF_COMM_CLR_NEWDAT);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800348}
349
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000350static int c_can_handle_lost_msg_obj(struct net_device *dev,
351 int iface, int objno, u32 ctrl)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800352{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800353 struct net_device_stats *stats = &dev->stats;
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000354 struct c_can_priv *priv = netdev_priv(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800355 struct can_frame *frame;
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000356 struct sk_buff *skb;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800357
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000358 ctrl &= ~(IF_MCONT_MSGLST | IF_MCONT_INTPND | IF_MCONT_NEWDAT);
359 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), ctrl);
Thomas Gleixner640916d2014-03-18 17:19:09 +0000360 c_can_object_put(dev, iface, objno, IF_COMM_CONTROL);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800361
Thomas Gleixner1da394d2014-04-11 08:13:13 +0000362 stats->rx_errors++;
363 stats->rx_over_errors++;
364
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800365 /* create an error msg */
366 skb = alloc_can_err_skb(dev, &frame);
367 if (unlikely(!skb))
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000368 return 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800369
370 frame->can_id |= CAN_ERR_CRTL;
371 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800372
373 netif_receive_skb(skb);
Thomas Gleixner07c7b6f2014-03-18 17:19:10 +0000374 return 1;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800375}
376
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000377static int c_can_read_msg_object(struct net_device *dev, int iface, u32 ctrl)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800378{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800379 struct net_device_stats *stats = &dev->stats;
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000380 struct c_can_priv *priv = netdev_priv(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800381 struct can_frame *frame;
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000382 struct sk_buff *skb;
383 u32 arb, data;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800384
385 skb = alloc_can_skb(dev, &frame);
386 if (!skb) {
387 stats->rx_dropped++;
388 return -ENOMEM;
389 }
390
391 frame->can_dlc = get_can_dlc(ctrl & 0x0F);
392
Pavel Machekccbc5352014-05-06 15:57:02 +0200393 arb = priv->read_reg32(priv, C_CAN_IFACE(ARB1_REG, iface));
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800394
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000395 if (arb & IF_ARB_MSGXTD)
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000396 frame->can_id = (arb & CAN_EFF_MASK) | CAN_EFF_FLAG;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800397 else
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000398 frame->can_id = (arb >> 18) & CAN_SFF_MASK;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800399
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000400 if (arb & IF_ARB_TRANSMIT) {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800401 frame->can_id |= CAN_RTR_FLAG;
Thomas Gleixner4fb6dcc2014-04-11 08:13:18 +0000402 } else {
403 int i, dreg = C_CAN_IFACE(DATA1_REG, iface);
404
405 for (i = 0; i < frame->can_dlc; i += 2, dreg ++) {
406 data = priv->read_reg(priv, dreg);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800407 frame->data[i] = data;
408 frame->data[i + 1] = data >> 8;
409 }
410 }
411
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800412 stats->rx_packets++;
413 stats->rx_bytes += frame->can_dlc;
Thomas Gleixner9c648632014-04-11 08:13:12 +0000414
415 netif_receive_skb(skb);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800416 return 0;
417}
418
419static void c_can_setup_receive_object(struct net_device *dev, int iface,
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000420 u32 obj, u32 mask, u32 id, u32 mcont)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800421{
422 struct c_can_priv *priv = netdev_priv(dev);
423
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000424 mask |= BIT(29);
Pavel Machekccbc5352014-05-06 15:57:02 +0200425 priv->write_reg32(priv, C_CAN_IFACE(MASK1_REG, iface), mask);
Alexander Stein2bd3bc42012-12-13 10:06:10 +0100426
Thomas Gleixnerd48071b2014-04-11 08:13:21 +0000427 id |= IF_ARB_MSGVAL;
Pavel Machekccbc5352014-05-06 15:57:02 +0200428 priv->write_reg32(priv, C_CAN_IFACE(ARB1_REG, iface), id);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800429
AnilKumar Ch33f81002012-05-29 11:13:15 +0530430 priv->write_reg(priv, C_CAN_IFACE(MSGCTRL_REG, iface), mcont);
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000431 c_can_object_put(dev, iface, obj, IF_COMM_RCV_SETUP);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800432}
433
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800434static netdev_tx_t c_can_start_xmit(struct sk_buff *skb,
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000435 struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800436{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800437 struct can_frame *frame = (struct can_frame *)skb->data;
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000438 struct c_can_priv *priv = netdev_priv(dev);
439 u32 idx, obj;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800440
441 if (can_dropped_invalid_skb(dev, skb))
442 return NETDEV_TX_OK;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800443 /*
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000444 * This is not a FIFO. C/D_CAN sends out the buffers
445 * prioritized. The lowest buffer number wins.
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800446 */
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000447 idx = fls(atomic_read(&priv->tx_active));
448 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
449
450 /* If this is the last buffer, stop the xmit queue */
451 if (idx == C_CAN_MSG_OBJ_TX_NUM - 1)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800452 netif_stop_queue(dev);
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000453 /*
454 * Store the message in the interface so we can call
455 * can_put_echo_skb(). We must do this before we enable
456 * transmit as we might race against do_tx().
457 */
Thomas Gleixner93941592014-04-11 08:13:22 +0000458 c_can_setup_tx_object(dev, IF_TX, frame, idx);
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000459 priv->dlc[idx] = frame->can_dlc;
460 can_put_echo_skb(skb, dev, idx);
461
462 /* Update the active bits */
463 atomic_add((1 << idx), &priv->tx_active);
464 /* Start transmission */
465 c_can_object_put(dev, IF_TX, obj, IF_COMM_TX);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800466
467 return NETDEV_TX_OK;
468}
469
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000470static int c_can_wait_for_ctrl_init(struct net_device *dev,
471 struct c_can_priv *priv, u32 init)
472{
473 int retry = 0;
474
475 while (init != (priv->read_reg(priv, C_CAN_CTRL_REG) & CONTROL_INIT)) {
476 udelay(10);
477 if (retry++ > 1000) {
478 netdev_err(dev, "CCTRL: set CONTROL_INIT failed\n");
479 return -EIO;
480 }
481 }
482 return 0;
483}
484
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800485static int c_can_set_bittiming(struct net_device *dev)
486{
487 unsigned int reg_btr, reg_brpe, ctrl_save;
488 u8 brp, brpe, sjw, tseg1, tseg2;
489 u32 ten_bit_brp;
490 struct c_can_priv *priv = netdev_priv(dev);
491 const struct can_bittiming *bt = &priv->can.bittiming;
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000492 int res;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800493
494 /* c_can provides a 6-bit brp and 4-bit brpe fields */
495 ten_bit_brp = bt->brp - 1;
496 brp = ten_bit_brp & BTR_BRP_MASK;
497 brpe = ten_bit_brp >> 6;
498
499 sjw = bt->sjw - 1;
500 tseg1 = bt->prop_seg + bt->phase_seg1 - 1;
501 tseg2 = bt->phase_seg2 - 1;
502 reg_btr = brp | (sjw << BTR_SJW_SHIFT) | (tseg1 << BTR_TSEG1_SHIFT) |
503 (tseg2 << BTR_TSEG2_SHIFT);
504 reg_brpe = brpe & BRP_EXT_BRPE_MASK;
505
506 netdev_info(dev,
507 "setting BTR=%04x BRPE=%04x\n", reg_btr, reg_brpe);
508
AnilKumar Ch33f81002012-05-29 11:13:15 +0530509 ctrl_save = priv->read_reg(priv, C_CAN_CTRL_REG);
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000510 ctrl_save &= ~CONTROL_INIT;
511 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_CCE | CONTROL_INIT);
512 res = c_can_wait_for_ctrl_init(dev, priv, CONTROL_INIT);
513 if (res)
514 return res;
515
AnilKumar Ch33f81002012-05-29 11:13:15 +0530516 priv->write_reg(priv, C_CAN_BTR_REG, reg_btr);
517 priv->write_reg(priv, C_CAN_BRPEXT_REG, reg_brpe);
518 priv->write_reg(priv, C_CAN_CTRL_REG, ctrl_save);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800519
Thomas Gleixner9fac1d12014-03-18 17:19:08 +0000520 return c_can_wait_for_ctrl_init(dev, priv, 0);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800521}
522
523/*
524 * Configure C_CAN message objects for Tx and Rx purposes:
525 * C_CAN provides a total of 32 message objects that can be configured
526 * either for Tx or Rx purposes. Here the first 16 message objects are used as
527 * a reception FIFO. The end of reception FIFO is signified by the EoB bit
528 * being SET. The remaining 16 message objects are kept aside for Tx purposes.
529 * See user guide document for further details on configuring message
530 * objects.
531 */
532static void c_can_configure_msg_objects(struct net_device *dev)
533{
534 int i;
535
536 /* first invalidate all message objects */
537 for (i = C_CAN_MSG_OBJ_RX_FIRST; i <= C_CAN_NO_OF_OBJECTS; i++)
Thomas Gleixner640916d2014-03-18 17:19:09 +0000538 c_can_inval_msg_object(dev, IF_RX, i);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800539
540 /* setup receive message objects */
541 for (i = C_CAN_MSG_OBJ_RX_FIRST; i < C_CAN_MSG_OBJ_RX_LAST; i++)
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000542 c_can_setup_receive_object(dev, IF_RX, i, 0, 0, IF_MCONT_RCV);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800543
Thomas Gleixner640916d2014-03-18 17:19:09 +0000544 c_can_setup_receive_object(dev, IF_RX, C_CAN_MSG_OBJ_RX_LAST, 0, 0,
Thomas Gleixner8ff2de02014-04-11 08:13:18 +0000545 IF_MCONT_RCV_EOB);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800546}
547
548/*
549 * Configure C_CAN chip:
550 * - enable/disable auto-retransmission
551 * - set operating mode
552 * - configure message objects
553 */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100554static int c_can_chip_config(struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800555{
556 struct c_can_priv *priv = netdev_priv(dev);
557
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +0000558 /* enable automatic retransmission */
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000559 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_ENABLE_AR);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800560
Dan Carpenterd9cb9bd2012-06-15 00:20:44 +0000561 if ((priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) &&
562 (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)) {
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800563 /* loopback + silent mode : useful for hot self-test */
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000564 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
565 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK | TEST_SILENT);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800566 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
567 /* loopback mode : useful for self-test function */
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000568 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530569 priv->write_reg(priv, C_CAN_TEST_REG, TEST_LBACK);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800570 } else if (priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY) {
571 /* silent mode : bus-monitoring mode */
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000572 priv->write_reg(priv, C_CAN_CTRL_REG, CONTROL_TEST);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530573 priv->write_reg(priv, C_CAN_TEST_REG, TEST_SILENT);
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000574 }
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800575
576 /* configure message objects */
577 c_can_configure_msg_objects(dev);
578
579 /* set a `lec` value so that we can check for updates later */
AnilKumar Ch33f81002012-05-29 11:13:15 +0530580 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800581
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000582 /* Clear all internal status */
583 atomic_set(&priv->tx_active, 0);
584 priv->rxmasked = 0;
Thomas Gleixner93941592014-04-11 08:13:22 +0000585 priv->tx_dir = 0;
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000586
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800587 /* set bittiming params */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100588 return c_can_set_bittiming(dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800589}
590
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100591static int c_can_start(struct net_device *dev)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800592{
593 struct c_can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100594 int err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800595
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800596 /* basic c_can configuration */
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100597 err = c_can_chip_config(dev);
598 if (err)
599 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800600
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000601 /* Setup the command for new messages */
602 priv->comm_rcv_high = priv->type != BOSCH_D_CAN ?
603 IF_COMM_RCV_LOW : IF_COMM_RCV_HIGH;
604
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800605 priv->can.state = CAN_STATE_ERROR_ACTIVE;
606
Roger Quadros3973c522014-11-14 17:40:13 +0200607 /* activate pins */
608 pinctrl_pm_select_default_state(dev->dev.parent);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100609 return 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800610}
611
612static void c_can_stop(struct net_device *dev)
613{
614 struct c_can_priv *priv = netdev_priv(dev);
615
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000616 c_can_irq_control(priv, false);
Roger Quadros3973c522014-11-14 17:40:13 +0200617
618 /* deactivate pins */
619 pinctrl_pm_select_sleep_state(dev->dev.parent);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800620 priv->can.state = CAN_STATE_STOPPED;
621}
622
623static int c_can_set_mode(struct net_device *dev, enum can_mode mode)
624{
Thomas Gleixnerbed11db2014-04-11 08:13:10 +0000625 struct c_can_priv *priv = netdev_priv(dev);
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100626 int err;
627
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800628 switch (mode) {
629 case CAN_MODE_START:
Marc Kleine-Budde130a5172014-03-18 19:06:01 +0100630 err = c_can_start(dev);
631 if (err)
632 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800633 netif_wake_queue(dev);
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +0000634 c_can_irq_control(priv, true);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800635 break;
636 default:
637 return -EOPNOTSUPP;
638 }
639
640 return 0;
641}
642
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100643static int __c_can_get_berr_counter(const struct net_device *dev,
644 struct can_berr_counter *bec)
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800645{
646 unsigned int reg_err_counter;
647 struct c_can_priv *priv = netdev_priv(dev);
648
AnilKumar Ch33f81002012-05-29 11:13:15 +0530649 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800650 bec->rxerr = (reg_err_counter & ERR_CNT_REC_MASK) >>
651 ERR_CNT_REC_SHIFT;
652 bec->txerr = reg_err_counter & ERR_CNT_TEC_MASK;
653
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100654 return 0;
655}
656
657static int c_can_get_berr_counter(const struct net_device *dev,
658 struct can_berr_counter *bec)
659{
660 struct c_can_priv *priv = netdev_priv(dev);
661 int err;
662
663 c_can_pm_runtime_get_sync(priv);
664 err = __c_can_get_berr_counter(dev, bec);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +0530665 c_can_pm_runtime_put_sync(priv);
666
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100667 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800668}
669
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800670static void c_can_do_tx(struct net_device *dev)
671{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800672 struct c_can_priv *priv = netdev_priv(dev);
673 struct net_device_stats *stats = &dev->stats;
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000674 u32 idx, obj, pkts = 0, bytes = 0, pend, clr;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800675
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000676 clr = pend = priv->read_reg(priv, C_CAN_INTPND2_REG);
Thomas Gleixnerbf88a2062014-03-18 17:19:12 +0000677
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000678 while ((idx = ffs(pend))) {
679 idx--;
680 pend &= ~(1 << idx);
681 obj = idx + C_CAN_MSG_OBJ_TX_FIRST;
Thomas Gleixner93941592014-04-11 08:13:22 +0000682 c_can_inval_tx_object(dev, IF_RX, obj);
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000683 can_get_echo_skb(dev, idx);
684 bytes += priv->dlc[idx];
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000685 pkts++;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800686 }
687
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000688 /* Clear the bits in the tx_active mask */
689 atomic_sub(clr, &priv->tx_active);
Thomas Gleixnerbf88a2062014-03-18 17:19:12 +0000690
Thomas Gleixner35bdafb52014-04-11 08:13:22 +0000691 if (clr & (1 << (C_CAN_MSG_OBJ_TX_NUM - 1)))
692 netif_wake_queue(dev);
Thomas Gleixner5a7513a2014-03-18 17:19:14 +0000693
694 if (pkts) {
695 stats->tx_bytes += bytes;
696 stats->tx_packets += pkts;
697 can_led_event(dev, CAN_LED_EVENT_TX);
698 }
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800699}
700
701/*
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000702 * If we have a gap in the pending bits, that means we either
703 * raced with the hardware or failed to readout all upper
704 * objects in the last run due to quota limit.
705 */
706static u32 c_can_adjust_pending(u32 pend)
707{
708 u32 weight, lasts;
709
710 if (pend == RECEIVE_OBJECT_BITS)
711 return pend;
712
713 /*
714 * If the last set bit is larger than the number of pending
715 * bits we have a gap.
716 */
717 weight = hweight32(pend);
718 lasts = fls(pend);
719
720 /* If the bits are linear, nothing to do */
721 if (lasts == weight)
722 return pend;
723
724 /*
725 * Find the first set bit after the gap. We walk backwards
726 * from the last set bit.
727 */
728 for (lasts--; pend & (1 << (lasts - 1)); lasts--);
729
730 return pend & ~((1 << lasts) - 1);
731}
732
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000733static inline void c_can_rx_object_get(struct net_device *dev,
734 struct c_can_priv *priv, u32 obj)
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000735{
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000736 c_can_object_get(dev, IF_RX, obj, priv->comm_rcv_high);
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000737}
738
739static inline void c_can_rx_finalize(struct net_device *dev,
740 struct c_can_priv *priv, u32 obj)
741{
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000742 if (priv->type != BOSCH_D_CAN)
743 c_can_object_get(dev, IF_RX, obj, IF_COMM_CLR_NEWDAT);
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000744}
745
Thomas Gleixner520f5702014-03-18 19:27:42 +0100746static int c_can_read_objects(struct net_device *dev, struct c_can_priv *priv,
747 u32 pend, int quota)
748{
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000749 u32 pkts = 0, ctrl, obj;
Thomas Gleixner520f5702014-03-18 19:27:42 +0100750
751 while ((obj = ffs(pend)) && quota > 0) {
752 pend &= ~BIT(obj - 1);
753
Thomas Gleixnerd61d09d2014-04-11 08:13:17 +0000754 c_can_rx_object_get(dev, priv, obj);
Thomas Gleixner520f5702014-03-18 19:27:42 +0100755 ctrl = priv->read_reg(priv, C_CAN_IFACE(MSGCTRL_REG, IF_RX));
756
757 if (ctrl & IF_MCONT_MSGLST) {
758 int n = c_can_handle_lost_msg_obj(dev, IF_RX, obj, ctrl);
759
760 pkts += n;
761 quota -= n;
762 continue;
763 }
764
765 /*
766 * This really should not happen, but this covers some
767 * odd HW behaviour. Do not remove that unless you
768 * want to brick your machine.
769 */
770 if (!(ctrl & IF_MCONT_NEWDAT))
771 continue;
772
773 /* read the data from the message object */
774 c_can_read_msg_object(dev, IF_RX, ctrl);
775
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000776 c_can_rx_finalize(dev, priv, obj);
Thomas Gleixner520f5702014-03-18 19:27:42 +0100777
778 pkts++;
779 quota--;
780 }
781
782 return pkts;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800783}
784
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000785static inline u32 c_can_get_pending(struct c_can_priv *priv)
786{
787 u32 pend = priv->read_reg(priv, C_CAN_NEWDAT1_REG);
788
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000789 return pend;
790}
791
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800792/*
793 * theory of operation:
794 *
795 * c_can core saves a received CAN message into the first free message
796 * object it finds free (starting with the lowest). Bits NEWDAT and
797 * INTPND are set for this message object indicating that a new message
798 * has arrived. To work-around this issue, we keep two groups of message
799 * objects whose partitioning is defined by C_CAN_MSG_OBJ_RX_SPLIT.
800 *
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000801 * We clear the newdat bit right away.
802 *
803 * This can result in packet reordering when the readout is slow.
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800804 */
805static int c_can_do_rx_poll(struct net_device *dev, int quota)
806{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800807 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixner520f5702014-03-18 19:27:42 +0100808 u32 pkts = 0, pend = 0, toread, n;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800809
Markus Pargmann4ce78a82013-11-01 10:36:36 +0100810 /*
811 * It is faster to read only one 16bit register. This is only possible
812 * for a maximum number of 16 objects.
813 */
814 BUILD_BUG_ON_MSG(C_CAN_MSG_OBJ_RX_LAST > 16,
815 "Implementation does not support more message objects than 16");
816
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000817 while (quota > 0) {
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000818 if (!pend) {
Thomas Gleixner2b9aecd2014-04-11 08:13:16 +0000819 pend = c_can_get_pending(priv);
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000820 if (!pend)
Thomas Gleixner520f5702014-03-18 19:27:42 +0100821 break;
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000822 /*
823 * If the pending field has a gap, handle the
824 * bits above the gap first.
825 */
Thomas Gleixner520f5702014-03-18 19:27:42 +0100826 toread = c_can_adjust_pending(pend);
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000827 } else {
Thomas Gleixner520f5702014-03-18 19:27:42 +0100828 toread = pend;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800829 }
Thomas Gleixner64f08f22014-03-18 17:19:10 +0000830 /* Remove the bits from pend */
Thomas Gleixner520f5702014-03-18 19:27:42 +0100831 pend &= ~toread;
832 /* Read the objects */
833 n = c_can_read_objects(dev, priv, toread, quota);
834 pkts += n;
835 quota -= n;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800836 }
837
Thomas Gleixnerb1d8e432014-03-18 17:19:15 +0000838 if (pkts)
839 can_led_event(dev, CAN_LED_EVENT_RX);
840
Thomas Gleixner520f5702014-03-18 19:27:42 +0100841 return pkts;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800842}
843
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800844static int c_can_handle_state_change(struct net_device *dev,
845 enum c_can_bus_error_types error_type)
846{
847 unsigned int reg_err_counter;
848 unsigned int rx_err_passive;
849 struct c_can_priv *priv = netdev_priv(dev);
850 struct net_device_stats *stats = &dev->stats;
851 struct can_frame *cf;
852 struct sk_buff *skb;
853 struct can_berr_counter bec;
854
Thomas Gleixnerf058d542014-04-11 08:13:12 +0000855 switch (error_type) {
856 case C_CAN_ERROR_WARNING:
857 /* error warning state */
858 priv->can.can_stats.error_warning++;
859 priv->can.state = CAN_STATE_ERROR_WARNING;
860 break;
861 case C_CAN_ERROR_PASSIVE:
862 /* error passive state */
863 priv->can.can_stats.error_passive++;
864 priv->can.state = CAN_STATE_ERROR_PASSIVE;
865 break;
866 case C_CAN_BUS_OFF:
867 /* bus-off state */
868 priv->can.state = CAN_STATE_BUS_OFF;
869 can_bus_off(dev);
870 break;
871 default:
872 break;
873 }
874
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300875 /* propagate the error condition to the CAN stack */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800876 skb = alloc_can_err_skb(dev, &cf);
877 if (unlikely(!skb))
878 return 0;
879
Marc Kleine-Buddee35d46a2013-11-24 23:31:24 +0100880 __c_can_get_berr_counter(dev, &bec);
AnilKumar Ch33f81002012-05-29 11:13:15 +0530881 reg_err_counter = priv->read_reg(priv, C_CAN_ERR_CNT_REG);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800882 rx_err_passive = (reg_err_counter & ERR_CNT_RP_MASK) >>
883 ERR_CNT_RP_SHIFT;
884
885 switch (error_type) {
886 case C_CAN_ERROR_WARNING:
887 /* error warning state */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800888 cf->can_id |= CAN_ERR_CRTL;
889 cf->data[1] = (bec.txerr > bec.rxerr) ?
890 CAN_ERR_CRTL_TX_WARNING :
891 CAN_ERR_CRTL_RX_WARNING;
892 cf->data[6] = bec.txerr;
893 cf->data[7] = bec.rxerr;
894
895 break;
896 case C_CAN_ERROR_PASSIVE:
897 /* error passive state */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800898 cf->can_id |= CAN_ERR_CRTL;
899 if (rx_err_passive)
900 cf->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
901 if (bec.txerr > 127)
902 cf->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
903
904 cf->data[6] = bec.txerr;
905 cf->data[7] = bec.rxerr;
906 break;
907 case C_CAN_BUS_OFF:
908 /* bus-off state */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800909 cf->can_id |= CAN_ERR_BUSOFF;
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800910 can_bus_off(dev);
911 break;
912 default:
913 break;
914 }
915
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800916 stats->rx_packets++;
917 stats->rx_bytes += cf->can_dlc;
Thomas Gleixner9c648632014-04-11 08:13:12 +0000918 netif_receive_skb(skb);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800919
920 return 1;
921}
922
923static int c_can_handle_bus_err(struct net_device *dev,
924 enum c_can_lec_type lec_type)
925{
926 struct c_can_priv *priv = netdev_priv(dev);
927 struct net_device_stats *stats = &dev->stats;
928 struct can_frame *cf;
929 struct sk_buff *skb;
930
931 /*
932 * early exit if no lec update or no error.
933 * no lec update means that no CAN bus event has been detected
934 * since CPU wrote 0x7 value to status reg.
935 */
936 if (lec_type == LEC_UNUSED || lec_type == LEC_NO_ERROR)
937 return 0;
938
Thomas Gleixner097aec12014-04-11 08:13:13 +0000939 if (!(priv->can.ctrlmode & CAN_CTRLMODE_BERR_REPORTING))
940 return 0;
941
Thomas Gleixner1da394d2014-04-11 08:13:13 +0000942 /* common for all type of bus errors */
943 priv->can.can_stats.bus_error++;
944 stats->rx_errors++;
945
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300946 /* propagate the error condition to the CAN stack */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800947 skb = alloc_can_err_skb(dev, &cf);
948 if (unlikely(!skb))
949 return 0;
950
951 /*
952 * check for 'last error code' which tells us the
953 * type of the last error to occur on the CAN bus
954 */
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800955 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
956 cf->data[2] |= CAN_ERR_PROT_UNSPEC;
957
958 switch (lec_type) {
959 case LEC_STUFF_ERROR:
960 netdev_dbg(dev, "stuff error\n");
961 cf->data[2] |= CAN_ERR_PROT_STUFF;
962 break;
963 case LEC_FORM_ERROR:
964 netdev_dbg(dev, "form error\n");
965 cf->data[2] |= CAN_ERR_PROT_FORM;
966 break;
967 case LEC_ACK_ERROR:
968 netdev_dbg(dev, "ack error\n");
Olivier Sobrie6ea45882013-01-18 09:32:39 +0100969 cf->data[3] |= (CAN_ERR_PROT_LOC_ACK |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800970 CAN_ERR_PROT_LOC_ACK_DEL);
971 break;
972 case LEC_BIT1_ERROR:
973 netdev_dbg(dev, "bit1 error\n");
974 cf->data[2] |= CAN_ERR_PROT_BIT1;
975 break;
976 case LEC_BIT0_ERROR:
977 netdev_dbg(dev, "bit0 error\n");
978 cf->data[2] |= CAN_ERR_PROT_BIT0;
979 break;
980 case LEC_CRC_ERROR:
981 netdev_dbg(dev, "CRC error\n");
Olivier Sobrie6ea45882013-01-18 09:32:39 +0100982 cf->data[3] |= (CAN_ERR_PROT_LOC_CRC_SEQ |
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800983 CAN_ERR_PROT_LOC_CRC_DEL);
984 break;
985 default:
986 break;
987 }
988
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800989 stats->rx_packets++;
990 stats->rx_bytes += cf->can_dlc;
Thomas Gleixner9c648632014-04-11 08:13:12 +0000991 netif_receive_skb(skb);
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800992 return 1;
993}
994
995static int c_can_poll(struct napi_struct *napi, int quota)
996{
Bhupesh Sharma881ff672011-02-13 22:51:44 -0800997 struct net_device *dev = napi->dev;
998 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixnerfa39b542014-04-11 08:13:15 +0000999 u16 curr, last = priv->last_status;
1000 int work_done = 0;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001001
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001002 priv->last_status = curr = priv->read_reg(priv, C_CAN_STS_REG);
1003 /* Ack status on C_CAN. D_CAN is self clearing */
1004 if (priv->type != BOSCH_D_CAN)
1005 priv->write_reg(priv, C_CAN_STS_REG, LEC_UNUSED);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001006
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001007 /* handle state changes */
1008 if ((curr & STATUS_EWARN) && (!(last & STATUS_EWARN))) {
1009 netdev_dbg(dev, "entered error warning state\n");
1010 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_WARNING);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001011 }
1012
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001013 if ((curr & STATUS_EPASS) && (!(last & STATUS_EPASS))) {
1014 netdev_dbg(dev, "entered error passive state\n");
1015 work_done += c_can_handle_state_change(dev, C_CAN_ERROR_PASSIVE);
1016 }
1017
1018 if ((curr & STATUS_BOFF) && (!(last & STATUS_BOFF))) {
1019 netdev_dbg(dev, "entered bus off state\n");
1020 work_done += c_can_handle_state_change(dev, C_CAN_BUS_OFF);
1021 goto end;
1022 }
1023
1024 /* handle bus recovery events */
1025 if ((!(curr & STATUS_BOFF)) && (last & STATUS_BOFF)) {
1026 netdev_dbg(dev, "left bus off state\n");
1027 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1028 }
1029 if ((!(curr & STATUS_EPASS)) && (last & STATUS_EPASS)) {
1030 netdev_dbg(dev, "left error passive state\n");
1031 priv->can.state = CAN_STATE_ERROR_ACTIVE;
1032 }
1033
1034 /* handle lec errors on the bus */
1035 work_done += c_can_handle_bus_err(dev, curr & LEC_MASK);
1036
1037 /* Handle Tx/Rx events. We do this unconditionally */
1038 work_done += c_can_do_rx_poll(dev, (quota - work_done));
1039 c_can_do_tx(dev);
1040
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001041end:
1042 if (work_done < quota) {
1043 napi_complete(napi);
Thomas Gleixneref1d2e22014-04-11 08:13:11 +00001044 /* enable all IRQs if we are not in bus off state */
1045 if (priv->can.state != CAN_STATE_BUS_OFF)
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +00001046 c_can_irq_control(priv, true);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001047 }
1048
1049 return work_done;
1050}
1051
1052static irqreturn_t c_can_isr(int irq, void *dev_id)
1053{
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001054 struct net_device *dev = (struct net_device *)dev_id;
1055 struct c_can_priv *priv = netdev_priv(dev);
1056
Thomas Gleixnerfa39b542014-04-11 08:13:15 +00001057 if (!priv->read_reg(priv, C_CAN_INT_REG))
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001058 return IRQ_NONE;
1059
1060 /* disable all interrupts and schedule the NAPI */
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +00001061 c_can_irq_control(priv, false);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001062 napi_schedule(&priv->napi);
1063
1064 return IRQ_HANDLED;
1065}
1066
1067static int c_can_open(struct net_device *dev)
1068{
1069 int err;
1070 struct c_can_priv *priv = netdev_priv(dev);
1071
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301072 c_can_pm_runtime_get_sync(priv);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301073 c_can_reset_ram(priv, true);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301074
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001075 /* open the can device */
1076 err = open_candev(dev);
1077 if (err) {
1078 netdev_err(dev, "failed to open can device\n");
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301079 goto exit_open_fail;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001080 }
1081
1082 /* register interrupt handler */
1083 err = request_irq(dev->irq, &c_can_isr, IRQF_SHARED, dev->name,
1084 dev);
1085 if (err < 0) {
1086 netdev_err(dev, "failed to request interrupt\n");
1087 goto exit_irq_fail;
1088 }
1089
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001090 /* start the c_can controller */
1091 err = c_can_start(dev);
1092 if (err)
1093 goto exit_start_fail;
AnilKumar Chf461f272012-05-23 17:45:11 +05301094
Fabio Baltieri5090f802012-12-18 18:51:01 +01001095 can_led_event(dev, CAN_LED_EVENT_OPEN);
1096
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001097 napi_enable(&priv->napi);
Thomas Gleixnerbed11db2014-04-11 08:13:10 +00001098 /* enable status change, error and module interrupts */
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +00001099 c_can_irq_control(priv, true);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001100 netif_start_queue(dev);
1101
1102 return 0;
1103
Marc Kleine-Budde130a5172014-03-18 19:06:01 +01001104exit_start_fail:
1105 free_irq(dev->irq, dev);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001106exit_irq_fail:
1107 close_candev(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301108exit_open_fail:
AnilKumar Ch52cde852012-11-21 11:14:10 +05301109 c_can_reset_ram(priv, false);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301110 c_can_pm_runtime_put_sync(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001111 return err;
1112}
1113
1114static int c_can_close(struct net_device *dev)
1115{
1116 struct c_can_priv *priv = netdev_priv(dev);
1117
1118 netif_stop_queue(dev);
1119 napi_disable(&priv->napi);
1120 c_can_stop(dev);
1121 free_irq(dev->irq, dev);
1122 close_candev(dev);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301123
1124 c_can_reset_ram(priv, false);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301125 c_can_pm_runtime_put_sync(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001126
Fabio Baltieri5090f802012-12-18 18:51:01 +01001127 can_led_event(dev, CAN_LED_EVENT_STOP);
1128
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001129 return 0;
1130}
1131
1132struct net_device *alloc_c_can_dev(void)
1133{
1134 struct net_device *dev;
1135 struct c_can_priv *priv;
1136
1137 dev = alloc_candev(sizeof(struct c_can_priv), C_CAN_MSG_OBJ_TX_NUM);
1138 if (!dev)
1139 return NULL;
1140
1141 priv = netdev_priv(dev);
1142 netif_napi_add(dev, &priv->napi, c_can_poll, C_CAN_NAPI_WEIGHT);
1143
1144 priv->dev = dev;
1145 priv->can.bittiming_const = &c_can_bittiming_const;
1146 priv->can.do_set_mode = c_can_set_mode;
1147 priv->can.do_get_berr_counter = c_can_get_berr_counter;
Marc Kleine-Buddeee6f0982011-03-24 02:34:32 +00001148 priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK |
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001149 CAN_CTRLMODE_LISTENONLY |
1150 CAN_CTRLMODE_BERR_REPORTING;
1151
1152 return dev;
1153}
1154EXPORT_SYMBOL_GPL(alloc_c_can_dev);
1155
AnilKumar Ch82120032012-09-21 15:29:01 +05301156#ifdef CONFIG_PM
1157int c_can_power_down(struct net_device *dev)
1158{
1159 u32 val;
1160 unsigned long time_out;
1161 struct c_can_priv *priv = netdev_priv(dev);
1162
1163 if (!(dev->flags & IFF_UP))
1164 return 0;
1165
1166 WARN_ON(priv->type != BOSCH_D_CAN);
1167
1168 /* set PDR value so the device goes to power down mode */
1169 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1170 val |= CONTROL_EX_PDR;
1171 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1172
1173 /* Wait for the PDA bit to get set */
1174 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1175 while (!(priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1176 time_after(time_out, jiffies))
1177 cpu_relax();
1178
1179 if (time_after(jiffies, time_out))
1180 return -ETIMEDOUT;
1181
1182 c_can_stop(dev);
1183
AnilKumar Ch52cde852012-11-21 11:14:10 +05301184 c_can_reset_ram(priv, false);
AnilKumar Ch82120032012-09-21 15:29:01 +05301185 c_can_pm_runtime_put_sync(priv);
1186
1187 return 0;
1188}
1189EXPORT_SYMBOL_GPL(c_can_power_down);
1190
1191int c_can_power_up(struct net_device *dev)
1192{
1193 u32 val;
1194 unsigned long time_out;
1195 struct c_can_priv *priv = netdev_priv(dev);
Thomas Gleixnerbed11db2014-04-11 08:13:10 +00001196 int ret;
AnilKumar Ch82120032012-09-21 15:29:01 +05301197
1198 if (!(dev->flags & IFF_UP))
1199 return 0;
1200
1201 WARN_ON(priv->type != BOSCH_D_CAN);
1202
1203 c_can_pm_runtime_get_sync(priv);
AnilKumar Ch52cde852012-11-21 11:14:10 +05301204 c_can_reset_ram(priv, true);
AnilKumar Ch82120032012-09-21 15:29:01 +05301205
1206 /* Clear PDR and INIT bits */
1207 val = priv->read_reg(priv, C_CAN_CTRL_EX_REG);
1208 val &= ~CONTROL_EX_PDR;
1209 priv->write_reg(priv, C_CAN_CTRL_EX_REG, val);
1210 val = priv->read_reg(priv, C_CAN_CTRL_REG);
1211 val &= ~CONTROL_INIT;
1212 priv->write_reg(priv, C_CAN_CTRL_REG, val);
1213
1214 /* Wait for the PDA bit to get clear */
1215 time_out = jiffies + msecs_to_jiffies(INIT_WAIT_MS);
1216 while ((priv->read_reg(priv, C_CAN_STS_REG) & STATUS_PDA) &&
1217 time_after(time_out, jiffies))
1218 cpu_relax();
1219
1220 if (time_after(jiffies, time_out))
1221 return -ETIMEDOUT;
1222
Thomas Gleixnerbed11db2014-04-11 08:13:10 +00001223 ret = c_can_start(dev);
1224 if (!ret)
Thomas Gleixner2d5f4f82014-04-11 08:13:17 +00001225 c_can_irq_control(priv, true);
Thomas Gleixnerbed11db2014-04-11 08:13:10 +00001226
1227 return ret;
AnilKumar Ch82120032012-09-21 15:29:01 +05301228}
1229EXPORT_SYMBOL_GPL(c_can_power_up);
1230#endif
1231
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001232void free_c_can_dev(struct net_device *dev)
1233{
Marc Kleine-Buddef29b4232014-03-18 19:13:59 +01001234 struct c_can_priv *priv = netdev_priv(dev);
1235
1236 netif_napi_del(&priv->napi);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001237 free_candev(dev);
1238}
1239EXPORT_SYMBOL_GPL(free_c_can_dev);
1240
1241static const struct net_device_ops c_can_netdev_ops = {
1242 .ndo_open = c_can_open,
1243 .ndo_stop = c_can_close,
1244 .ndo_start_xmit = c_can_start_xmit,
Oliver Hartkoppc971fa22014-03-07 09:23:41 +01001245 .ndo_change_mtu = can_change_mtu,
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001246};
1247
1248int register_c_can_dev(struct net_device *dev)
1249{
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301250 struct c_can_priv *priv = netdev_priv(dev);
1251 int err;
1252
Roger Quadros3973c522014-11-14 17:40:13 +02001253 /* Deactivate pins to prevent DRA7 DCAN IP from being
1254 * stuck in transition when module is disabled.
1255 * Pins are activated in c_can_start() and deactivated
1256 * in c_can_stop()
1257 */
1258 pinctrl_pm_select_sleep_state(dev->dev.parent);
1259
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301260 c_can_pm_runtime_enable(priv);
1261
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001262 dev->flags |= IFF_ECHO; /* we support local echo */
1263 dev->netdev_ops = &c_can_netdev_ops;
1264
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301265 err = register_candev(dev);
1266 if (err)
1267 c_can_pm_runtime_disable(priv);
Fabio Baltieri5090f802012-12-18 18:51:01 +01001268 else
1269 devm_can_led_init(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301270
1271 return err;
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001272}
1273EXPORT_SYMBOL_GPL(register_c_can_dev);
1274
1275void unregister_c_can_dev(struct net_device *dev)
1276{
1277 struct c_can_priv *priv = netdev_priv(dev);
1278
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001279 unregister_candev(dev);
AnilKumar Ch4cdd34b2012-08-20 16:50:54 +05301280
1281 c_can_pm_runtime_disable(priv);
Bhupesh Sharma881ff672011-02-13 22:51:44 -08001282}
1283EXPORT_SYMBOL_GPL(unregister_c_can_dev);
1284
1285MODULE_AUTHOR("Bhupesh Sharma <bhupesh.sharma@st.com>");
1286MODULE_LICENSE("GPL v2");
1287MODULE_DESCRIPTION("CAN bus driver for Bosch C_CAN controller");