]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/mscan/mscan.c
Fix common misspellings
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / mscan / mscan.c
1 /*
2  * CAN bus driver for the alone generic (as possible as) MSCAN controller.
3  *
4  * Copyright (C) 2005-2006 Andrey Volkov <avolkov@varma-el.com>,
5  *                         Varma Electronics Oy
6  * Copyright (C) 2008-2009 Wolfgang Grandegger <wg@grandegger.com>
7  * Copyright (C) 2008-2009 Pengutronix <kernel@pengutronix.de>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the version 2 of the GNU General Public License
11  * as published by the Free Software Foundation
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_ether.h>
30 #include <linux/list.h>
31 #include <socketcan/can.h>
32 #include <socketcan/can/dev.h>
33 #include <socketcan/can/error.h>
34 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,16)
35 #include <linux/io.h>
36 #else
37 #include <asm/io.h>
38 #endif
39
40 #include "mscan.h"
41
42 #include <socketcan/can/version.h>      /* for RCSID. Removed by mkpatch script */
43 RCSID("$Id$");
44
45 static struct can_bittiming_const mscan_bittiming_const = {
46         .name = "mscan",
47         .tseg1_min = 4,
48         .tseg1_max = 16,
49         .tseg2_min = 2,
50         .tseg2_max = 8,
51         .sjw_max = 4,
52         .brp_min = 1,
53         .brp_max = 64,
54         .brp_inc = 1,
55 };
56
57 struct mscan_state {
58         u8 mode;
59         u8 canrier;
60         u8 cantier;
61 };
62
63 static enum can_state state_map[] = {
64         CAN_STATE_ERROR_ACTIVE,
65         CAN_STATE_ERROR_WARNING,
66         CAN_STATE_ERROR_PASSIVE,
67         CAN_STATE_BUS_OFF
68 };
69
70 static int mscan_set_mode(struct net_device *dev, u8 mode)
71 {
72         struct mscan_priv *priv = netdev_priv(dev);
73         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
74         int ret = 0;
75         int i;
76         u8 canctl1;
77
78         if (mode != MSCAN_NORMAL_MODE) {
79                 if (priv->tx_active) {
80                         /* Abort transfers before going to sleep */#
81                         out_8(&regs->cantarq, priv->tx_active);
82                         /* Suppress TX done interrupts */
83                         out_8(&regs->cantier, 0);
84                 }
85
86                 canctl1 = in_8(&regs->canctl1);
87                 if ((mode & MSCAN_SLPRQ) && !(canctl1 & MSCAN_SLPAK)) {
88                         setbits8(&regs->canctl0, MSCAN_SLPRQ);
89                         for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
90                                 if (in_8(&regs->canctl1) & MSCAN_SLPAK)
91                                         break;
92                                 udelay(100);
93                         }
94                         /*
95                          * The mscan controller will fail to enter sleep mode,
96                          * while there are irregular activities on bus, like
97                          * somebody keeps retransmitting. This behavior is
98                          * undocumented and seems to differ between mscan built
99                          * in mpc5200b and mpc5200. We proceed in that case,
100                          * since otherwise the slprq will be kept set and the
101                          * controller will get stuck. NOTE: INITRQ or CSWAI
102                          * will abort all active transmit actions, if still
103                          * any, at once.
104                          */
105                         if (i >= MSCAN_SET_MODE_RETRIES)
106                                 dev_dbg(ND2D(dev),
107                                         "device failed to enter sleep mode. "
108                                         "We proceed anyhow.\n");
109                         else
110                                 priv->can.state = CAN_STATE_SLEEPING;
111                 }
112
113                 if ((mode & MSCAN_INITRQ) && !(canctl1 & MSCAN_INITAK)) {
114                         setbits8(&regs->canctl0, MSCAN_INITRQ);
115                         for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
116                                 if (in_8(&regs->canctl1) & MSCAN_INITAK)
117                                         break;
118                         }
119                         if (i >= MSCAN_SET_MODE_RETRIES)
120                                 ret = -ENODEV;
121                 }
122                 if (!ret)
123                         priv->can.state = CAN_STATE_STOPPED;
124
125                 if (mode & MSCAN_CSWAI)
126                         setbits8(&regs->canctl0, MSCAN_CSWAI);
127
128         } else {
129                 canctl1 = in_8(&regs->canctl1);
130                 if (canctl1 & (MSCAN_SLPAK | MSCAN_INITAK)) {
131                         clrbits8(&regs->canctl0, MSCAN_SLPRQ | MSCAN_INITRQ);
132                         for (i = 0; i < MSCAN_SET_MODE_RETRIES; i++) {
133                                 canctl1 = in_8(&regs->canctl1);
134                                 if (!(canctl1 & (MSCAN_INITAK | MSCAN_SLPAK)))
135                                         break;
136                         }
137                         if (i >= MSCAN_SET_MODE_RETRIES)
138                                 ret = -ENODEV;
139                         else
140                                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
141                 }
142         }
143         return ret;
144 }
145
146 static int mscan_start(struct net_device *dev)
147 {
148         struct mscan_priv *priv = netdev_priv(dev);
149         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
150         u8 canrflg;
151         int err;
152
153         out_8(&regs->canrier, 0);
154
155         INIT_LIST_HEAD(&priv->tx_head);
156         priv->prev_buf_id = 0;
157         priv->cur_pri = 0;
158         priv->tx_active = 0;
159         priv->shadow_canrier = 0;
160         priv->flags = 0;
161
162         if (priv->type == MSCAN_TYPE_MPC5121) {
163                 /* Clear pending bus-off condition */
164                 if (in_8(&regs->canmisc) & MSCAN_BOHOLD)
165                         out_8(&regs->canmisc, MSCAN_BOHOLD);
166         }
167
168         err = mscan_set_mode(dev, MSCAN_NORMAL_MODE);
169         if (err)
170                 return err;
171
172         canrflg = in_8(&regs->canrflg);
173         priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
174         priv->can.state = state_map[max(MSCAN_STATE_RX(canrflg),
175                                     MSCAN_STATE_TX(canrflg))];
176         out_8(&regs->cantier, 0);
177
178         /* Enable receive interrupts. */
179         out_8(&regs->canrier, MSCAN_RX_INTS_ENABLE);
180
181         return 0;
182 }
183
184 static int mscan_restart(struct net_device *dev)
185 {
186         struct mscan_priv *priv = netdev_priv(dev);
187
188         if (priv->type == MSCAN_TYPE_MPC5121) {
189                 struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
190
191                 priv->can.state = CAN_STATE_ERROR_ACTIVE;
192                 WARN(!(in_8(&regs->canmisc) & MSCAN_BOHOLD),
193                      "bus-off state expected");
194                 out_8(&regs->canmisc, MSCAN_BOHOLD);
195                 /* Re-enable receive interrupts. */
196                 out_8(&regs->canrier, MSCAN_RX_INTS_ENABLE);
197         } else {
198                 if (priv->can.state <= CAN_STATE_BUS_OFF)
199                         mscan_set_mode(dev, MSCAN_INIT_MODE);
200                 return mscan_start(dev);
201         }
202
203         return 0;
204 }
205
206 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
207 static int mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
208 #else
209 static netdev_tx_t mscan_start_xmit(struct sk_buff *skb, struct net_device *dev)
210 #endif
211 {
212         struct can_frame *frame = (struct can_frame *)skb->data;
213         struct mscan_priv *priv = netdev_priv(dev);
214         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
215         int i, rtr, buf_id;
216         u32 can_id;
217
218         if (can_dropped_invalid_skb(dev, skb))
219                 return NETDEV_TX_OK;
220
221         out_8(&regs->cantier, 0);
222
223         i = ~priv->tx_active & MSCAN_TXE;
224         buf_id = ffs(i) - 1;
225         switch (hweight8(i)) {
226         case 0:
227                 netif_stop_queue(dev);
228                 dev_err(ND2D(dev), "Tx Ring full when queue awake!\n");
229                 return NETDEV_TX_BUSY;
230         case 1:
231                 /*
232                  * if buf_id < 3, then current frame will be send out of order,
233                  * since buffer with lower id have higher priority (hell..)
234                  */
235                 netif_stop_queue(dev);
236         case 2:
237                 if (buf_id < priv->prev_buf_id) {
238                         priv->cur_pri++;
239                         if (priv->cur_pri == 0xff) {
240                                 set_bit(F_TX_WAIT_ALL, &priv->flags);
241                                 netif_stop_queue(dev);
242                         }
243                 }
244                 set_bit(F_TX_PROGRESS, &priv->flags);
245                 break;
246         }
247         priv->prev_buf_id = buf_id;
248         out_8(&regs->cantbsel, i);
249
250         rtr = frame->can_id & CAN_RTR_FLAG;
251
252         /* RTR is always the lowest bit of interest, then IDs follow */
253         if (frame->can_id & CAN_EFF_FLAG) {
254                 can_id = (frame->can_id & CAN_EFF_MASK)
255                          << (MSCAN_EFF_RTR_SHIFT + 1);
256                 if (rtr)
257                         can_id |= 1 << MSCAN_EFF_RTR_SHIFT;
258                 out_be16(&regs->tx.idr3_2, can_id);
259
260                 can_id >>= 16;
261                 /* EFF_FLAGS are between the IDs :( */
262                 can_id = (can_id & 0x7) | ((can_id << 2) & 0xffe0)
263                          | MSCAN_EFF_FLAGS;
264         } else {
265                 can_id = (frame->can_id & CAN_SFF_MASK)
266                          << (MSCAN_SFF_RTR_SHIFT + 1);
267                 if (rtr)
268                         can_id |= 1 << MSCAN_SFF_RTR_SHIFT;
269         }
270         out_be16(&regs->tx.idr1_0, can_id);
271
272         if (!rtr) {
273                 void __iomem *data = &regs->tx.dsr1_0;
274                 u16 *payload = (u16 *)frame->data;
275
276                 /* It is safe to write into dsr[dlc+1] */
277                 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
278                         out_be16(data, *payload++);
279                         data += 2 + _MSCAN_RESERVED_DSR_SIZE;
280                 }
281         }
282
283         out_8(&regs->tx.dlr, frame->can_dlc);
284         out_8(&regs->tx.tbpr, priv->cur_pri);
285
286         /* Start transmission. */
287         out_8(&regs->cantflg, 1 << buf_id);
288
289         if (!test_bit(F_TX_PROGRESS, &priv->flags))
290                 dev->trans_start = jiffies;
291
292         list_add_tail(&priv->tx_queue[buf_id].list, &priv->tx_head);
293
294         can_put_echo_skb(skb, dev, buf_id);
295
296         /* Enable interrupt. */
297         priv->tx_active |= 1 << buf_id;
298         out_8(&regs->cantier, priv->tx_active);
299
300         return NETDEV_TX_OK;
301 }
302
303 /* This function returns the old state to see where we came from */
304 static enum can_state check_set_state(struct net_device *dev, u8 canrflg)
305 {
306         struct mscan_priv *priv = netdev_priv(dev);
307         enum can_state state, old_state = priv->can.state;
308
309         if (canrflg & MSCAN_CSCIF && old_state <= CAN_STATE_BUS_OFF) {
310                 state = state_map[max(MSCAN_STATE_RX(canrflg),
311                                       MSCAN_STATE_TX(canrflg))];
312                 priv->can.state = state;
313         }
314         return old_state;
315 }
316
317 static void mscan_get_rx_frame(struct net_device *dev, struct can_frame *frame)
318 {
319         struct mscan_priv *priv = netdev_priv(dev);
320         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
321         u32 can_id;
322         int i;
323
324         can_id = in_be16(&regs->rx.idr1_0);
325         if (can_id & (1 << 3)) {
326                 frame->can_id = CAN_EFF_FLAG;
327                 can_id = ((can_id << 16) | in_be16(&regs->rx.idr3_2));
328                 can_id = ((can_id & 0xffe00000) |
329                           ((can_id & 0x7ffff) << 2)) >> 2;
330         } else {
331                 can_id >>= 4;
332                 frame->can_id = 0;
333         }
334
335         frame->can_id |= can_id >> 1;
336         if (can_id & 1)
337                 frame->can_id |= CAN_RTR_FLAG;
338
339         frame->can_dlc = get_can_dlc(in_8(&regs->rx.dlr) & 0xf);
340
341         if (!(frame->can_id & CAN_RTR_FLAG)) {
342                 void __iomem *data = &regs->rx.dsr1_0;
343                 u16 *payload = (u16 *)frame->data;
344
345                 for (i = 0; i < (frame->can_dlc + 1) / 2; i++) {
346                         *payload++ = in_be16(data);
347                         data += 2 + _MSCAN_RESERVED_DSR_SIZE;
348                 }
349         }
350
351         out_8(&regs->canrflg, MSCAN_RXF);
352 }
353
354 static void mscan_get_err_frame(struct net_device *dev, struct can_frame *frame,
355                                 u8 canrflg)
356 {
357         struct mscan_priv *priv = netdev_priv(dev);
358         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
359 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
360         struct net_device_stats *stats = can_get_stats(dev);
361 #else
362         struct net_device_stats *stats = &dev->stats;
363 #endif
364         enum can_state old_state;
365
366         dev_dbg(ND2D(dev), "error interrupt (canrflg=%#x)\n", canrflg);
367         frame->can_id = CAN_ERR_FLAG;
368
369         if (canrflg & MSCAN_OVRIF) {
370                 frame->can_id |= CAN_ERR_CRTL;
371                 frame->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
372                 stats->rx_over_errors++;
373                 stats->rx_errors++;
374         } else {
375                 frame->data[1] = 0;
376         }
377
378         old_state = check_set_state(dev, canrflg);
379         /* State changed */
380         if (old_state != priv->can.state) {
381                 switch (priv->can.state) {
382                 case CAN_STATE_ERROR_WARNING:
383                         frame->can_id |= CAN_ERR_CRTL;
384                         priv->can.can_stats.error_warning++;
385                         if ((priv->shadow_statflg & MSCAN_RSTAT_MSK) <
386                             (canrflg & MSCAN_RSTAT_MSK))
387                                 frame->data[1] |= CAN_ERR_CRTL_RX_WARNING;
388                         if ((priv->shadow_statflg & MSCAN_TSTAT_MSK) <
389                             (canrflg & MSCAN_TSTAT_MSK))
390                                 frame->data[1] |= CAN_ERR_CRTL_TX_WARNING;
391                         break;
392                 case CAN_STATE_ERROR_PASSIVE:
393                         frame->can_id |= CAN_ERR_CRTL;
394                         priv->can.can_stats.error_passive++;
395                         frame->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
396                         break;
397                 case CAN_STATE_BUS_OFF:
398                         frame->can_id |= CAN_ERR_BUSOFF;
399                         /*
400                          * The MSCAN on the MPC5200 does recover from bus-off
401                          * automatically. To avoid that we stop the chip doing
402                          * a light-weight stop (we are in irq-context).
403                          */
404                         if (priv->type != MSCAN_TYPE_MPC5121) {
405                                 out_8(&regs->cantier, 0);
406                                 out_8(&regs->canrier, 0);
407                                 setbits8(&regs->canctl0,
408                                          MSCAN_SLPRQ | MSCAN_INITRQ);
409                         }
410                         can_bus_off(dev);
411                         break;
412                 default:
413                         break;
414                 }
415         }
416         priv->shadow_statflg = canrflg & MSCAN_STAT_MSK;
417         frame->can_dlc = CAN_ERR_DLC;
418         out_8(&regs->canrflg, MSCAN_ERR_IF);
419 }
420
421 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
422 static int mscan_rx_poll(struct napi_struct *napi, int quota)
423 #else
424 static int mscan_rx_poll(struct net_device *dev, int *budget)
425 #endif
426 {
427 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
428         struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
429         struct net_device *dev = napi->dev;
430 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
431         struct mscan_priv *priv = container_of(napi, struct mscan_priv, napi);
432         struct net_device *dev = priv->dev;
433 #else
434         struct mscan_priv *priv = netdev_priv(dev);
435         int quota = min(dev->quota, *budget);
436 #endif
437         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
438 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
439         struct net_device_stats *stats = can_get_stats(dev);
440 #else
441         struct net_device_stats *stats = &dev->stats;
442 #endif
443         int npackets = 0;
444         int ret = 1;
445         struct sk_buff *skb;
446         struct can_frame *frame;
447         u8 canrflg;
448
449         while (npackets < quota) {
450                 canrflg = in_8(&regs->canrflg);
451                 if (!(canrflg & (MSCAN_RXF | MSCAN_ERR_IF)))
452                         break;
453
454                 skb = alloc_can_skb(dev, &frame);
455                 if (!skb) {
456                         if (printk_ratelimit())
457                                 dev_notice(ND2D(dev), "packet dropped\n");
458                         stats->rx_dropped++;
459                         out_8(&regs->canrflg, canrflg);
460                         continue;
461                 }
462
463                 if (canrflg & MSCAN_RXF)
464                         mscan_get_rx_frame(dev, frame);
465                 else if (canrflg & MSCAN_ERR_IF)
466                         mscan_get_err_frame(dev, frame, canrflg);
467
468 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
469                 dev->last_rx = jiffies;
470 #endif
471                 stats->rx_packets++;
472                 stats->rx_bytes += frame->can_dlc;
473                 npackets++;
474                 netif_receive_skb(skb);
475         }
476
477 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
478         *budget -= npackets;
479         dev->quota -= npackets;
480 #endif
481
482         if (!(in_8(&regs->canrflg) & (MSCAN_RXF | MSCAN_ERR_IF))) {
483 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
484                 napi_complete(&priv->napi);
485 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
486                 netif_rx_complete(dev, &priv->napi);
487 #else
488                 netif_rx_complete(dev);
489 #endif
490                 clear_bit(F_RX_PROGRESS, &priv->flags);
491                 if (priv->can.state < CAN_STATE_BUS_OFF)
492                         out_8(&regs->canrier, priv->shadow_canrier);
493                 ret = 0;
494         }
495         return ret;
496 }
497
498 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
499 static irqreturn_t mscan_isr(int irq, void *dev_id, struct pt_regs *r)
500 #else
501 static irqreturn_t mscan_isr(int irq, void *dev_id)
502 #endif
503 {
504         struct net_device *dev = (struct net_device *)dev_id;
505         struct mscan_priv *priv = netdev_priv(dev);
506         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
507 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
508         struct net_device_stats *stats = can_get_stats(dev);
509 #else
510         struct net_device_stats *stats = &dev->stats;
511 #endif
512         u8 cantier, cantflg, canrflg;
513         irqreturn_t ret = IRQ_NONE;
514
515         cantier = in_8(&regs->cantier) & MSCAN_TXE;
516         cantflg = in_8(&regs->cantflg) & cantier;
517
518         if (cantier && cantflg) {
519                 struct list_head *tmp, *pos;
520
521                 list_for_each_safe(pos, tmp, &priv->tx_head) {
522                         struct tx_queue_entry *entry =
523                             list_entry(pos, struct tx_queue_entry, list);
524                         u8 mask = entry->mask;
525
526                         if (!(cantflg & mask))
527                                 continue;
528
529                         out_8(&regs->cantbsel, mask);
530                         stats->tx_bytes += in_8(&regs->tx.dlr);
531                         stats->tx_packets++;
532                         can_get_echo_skb(dev, entry->id);
533                         priv->tx_active &= ~mask;
534                         list_del(pos);
535                 }
536
537                 if (list_empty(&priv->tx_head)) {
538                         clear_bit(F_TX_WAIT_ALL, &priv->flags);
539                         clear_bit(F_TX_PROGRESS, &priv->flags);
540                         priv->cur_pri = 0;
541                 } else {
542                         dev->trans_start = jiffies;
543                 }
544
545                 if (!test_bit(F_TX_WAIT_ALL, &priv->flags))
546                         netif_wake_queue(dev);
547
548                 out_8(&regs->cantier, priv->tx_active);
549                 ret = IRQ_HANDLED;
550         }
551
552         canrflg = in_8(&regs->canrflg);
553         if ((canrflg & ~MSCAN_STAT_MSK) &&
554             !test_and_set_bit(F_RX_PROGRESS, &priv->flags)) {
555                 if (canrflg & ~MSCAN_STAT_MSK) {
556                         priv->shadow_canrier = in_8(&regs->canrier);
557                         out_8(&regs->canrier, 0);
558 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
559                         napi_schedule(&priv->napi);
560 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
561                         netif_rx_schedule(dev, &priv->napi);
562 #else
563                         netif_rx_schedule(dev);
564 #endif
565                         ret = IRQ_HANDLED;
566                 } else {
567                         clear_bit(F_RX_PROGRESS, &priv->flags);
568                 }
569         }
570         return ret;
571 }
572
573 static int mscan_do_set_mode(struct net_device *dev, enum can_mode mode)
574 {
575         struct mscan_priv *priv = netdev_priv(dev);
576         int ret = 0;
577
578         if (!priv->open_time)
579                 return -EINVAL;
580
581         switch (mode) {
582         case CAN_MODE_START:
583                 ret = mscan_restart(dev);
584                 if (ret)
585                         break;
586                 if (netif_queue_stopped(dev))
587                         netif_wake_queue(dev);
588                 break;
589
590         default:
591                 ret = -EOPNOTSUPP;
592                 break;
593         }
594         return ret;
595 }
596
597 static int mscan_do_set_bittiming(struct net_device *dev)
598 {
599         struct mscan_priv *priv = netdev_priv(dev);
600         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
601         struct can_bittiming *bt = &priv->can.bittiming;
602         u8 btr0, btr1;
603
604         btr0 = BTR0_SET_BRP(bt->brp) | BTR0_SET_SJW(bt->sjw);
605         btr1 = (BTR1_SET_TSEG1(bt->prop_seg + bt->phase_seg1) |
606                 BTR1_SET_TSEG2(bt->phase_seg2) |
607                 BTR1_SET_SAM(priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES));
608
609         dev_info(ND2D(dev), "setting BTR0=0x%02x BTR1=0x%02x\n",
610                 btr0, btr1);
611
612         out_8(&regs->canbtr0, btr0);
613         out_8(&regs->canbtr1, btr1);
614
615         return 0;
616 }
617
618 static int mscan_open(struct net_device *dev)
619 {
620         int ret;
621         struct mscan_priv *priv = netdev_priv(dev);
622         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
623
624         /* common open */
625         ret = open_candev(dev);
626         if (ret)
627                 return ret;
628
629 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
630         napi_enable(&priv->napi);
631 #endif
632
633         ret = request_irq(dev->irq, mscan_isr, 0, dev->name, dev);
634         if (ret < 0) {
635                 dev_err(ND2D(dev), "failed to attach interrupt\n");
636                 goto exit_napi_disable;
637         }
638
639         priv->open_time = jiffies;
640
641         clrbits8(&regs->canctl1, MSCAN_LISTEN);
642
643         ret = mscan_start(dev);
644         if (ret)
645                 goto exit_free_irq;
646
647         netif_start_queue(dev);
648
649         return 0;
650
651 exit_free_irq:
652         priv->open_time = 0;
653         free_irq(dev->irq, dev);
654 exit_napi_disable:
655 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
656         napi_disable(&priv->napi);
657 #endif
658         close_candev(dev);
659         return ret;
660 }
661
662 static int mscan_close(struct net_device *dev)
663 {
664         struct mscan_priv *priv = netdev_priv(dev);
665         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
666
667         netif_stop_queue(dev);
668 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
669         napi_disable(&priv->napi);
670 #endif
671
672         out_8(&regs->cantier, 0);
673         out_8(&regs->canrier, 0);
674         mscan_set_mode(dev, MSCAN_INIT_MODE);
675         close_candev(dev);
676         free_irq(dev->irq, dev);
677         priv->open_time = 0;
678
679         return 0;
680 }
681
682 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
683 static const struct net_device_ops mscan_netdev_ops = {
684        .ndo_open               = mscan_open,
685        .ndo_stop               = mscan_close,
686        .ndo_start_xmit         = mscan_start_xmit,
687 };
688 #endif
689
690 int register_mscandev(struct net_device *dev, int mscan_clksrc)
691 {
692         struct mscan_priv *priv = netdev_priv(dev);
693         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
694         u8 ctl1;
695
696         ctl1 = in_8(&regs->canctl1);
697         if (mscan_clksrc)
698                 ctl1 |= MSCAN_CLKSRC;
699         else
700                 ctl1 &= ~MSCAN_CLKSRC;
701
702         if (priv->type == MSCAN_TYPE_MPC5121)
703                 ctl1 |= MSCAN_BORM; /* bus-off recovery upon request */
704
705         ctl1 |= MSCAN_CANE;
706         out_8(&regs->canctl1, ctl1);
707         udelay(100);
708
709         /* acceptance mask/acceptance code (accept everything) */
710         out_be16(&regs->canidar1_0, 0);
711         out_be16(&regs->canidar3_2, 0);
712         out_be16(&regs->canidar5_4, 0);
713         out_be16(&regs->canidar7_6, 0);
714
715         out_be16(&regs->canidmr1_0, 0xffff);
716         out_be16(&regs->canidmr3_2, 0xffff);
717         out_be16(&regs->canidmr5_4, 0xffff);
718         out_be16(&regs->canidmr7_6, 0xffff);
719         /* Two 32 bit Acceptance Filters */
720         out_8(&regs->canidac, MSCAN_AF_32BIT);
721
722         mscan_set_mode(dev, MSCAN_INIT_MODE);
723
724         return register_candev(dev);
725 }
726
727 void unregister_mscandev(struct net_device *dev)
728 {
729         struct mscan_priv *priv = netdev_priv(dev);
730         struct mscan_regs *regs = (struct mscan_regs *)priv->reg_base;
731         mscan_set_mode(dev, MSCAN_INIT_MODE);
732         clrbits8(&regs->canctl1, MSCAN_CANE);
733         unregister_candev(dev);
734 }
735
736 struct net_device *alloc_mscandev(void)
737 {
738         struct net_device *dev;
739         struct mscan_priv *priv;
740         int i;
741
742         dev = alloc_candev(sizeof(struct mscan_priv), MSCAN_ECHO_SKB_MAX);
743         if (!dev)
744                 return NULL;
745         priv = netdev_priv(dev);
746
747 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
748         dev->netdev_ops = &mscan_netdev_ops;
749 #else
750         dev->open = mscan_open;
751         dev->stop = mscan_close;
752         dev->hard_start_xmit = mscan_start_xmit;
753 #endif
754
755         dev->flags |= IFF_ECHO; /* we support local echo */
756
757 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
758         netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
759 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
760         priv->dev = dev;
761         netif_napi_add(dev, &priv->napi, mscan_rx_poll, 8);
762 #else
763         dev->poll = mscan_rx_poll;
764         dev->weight = 8;
765 #endif
766
767         priv->can.bittiming_const = &mscan_bittiming_const;
768         priv->can.do_set_bittiming = mscan_do_set_bittiming;
769         priv->can.do_set_mode = mscan_do_set_mode;
770         priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
771
772         for (i = 0; i < TX_QUEUE_SIZE; i++) {
773                 priv->tx_queue[i].id = i;
774                 priv->tx_queue[i].mask = 1 << i;
775         }
776
777         return dev;
778 }
779
780 MODULE_AUTHOR("Andrey Volkov <avolkov@varma-el.com>");
781 MODULE_LICENSE("GPL v2");
782 MODULE_DESCRIPTION("CAN port driver for a MSCAN based chips");