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