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