]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - kernel/2.6/drivers/net/can/at91_can.c
Add modifiers for sampling-point and sjw to can_if start/stop script.
[socketcan-devel.git] / kernel / 2.6 / drivers / net / can / at91_can.c
1 /*
2  * at91_can.c - CAN network driver for AT91 SoC CAN controller
3  *
4  * (C) 2007 by Hans J. Koch <hjk@linutronix.de>
5  * (C) 2008, 2009 by Marc Kleine-Budde <kernel@pengutronix.de>
6  *
7  * This software may be distributed under the terms of the GNU General
8  * Public License ("GPL") version 2 as distributed in the 'COPYING'
9  * file from the main directory of the linux kernel source.
10  *
11  * Send feedback to <socketcan-users@lists.berlios.de>
12  *
13  *
14  * Your platform definition file should specify something like:
15  *
16  * static struct at91_can_data ek_can_data = {
17  *      transceiver_switch = sam9263ek_transceiver_switch,
18  * };
19  *
20  * at91_add_device_can(&ek_can_data);
21  *
22  */
23
24 #include <linux/clk.h>
25 #include <linux/errno.h>
26 #include <linux/if_arp.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/netdevice.h>
32 #include <linux/platform_device.h>
33 #include <linux/skbuff.h>
34 #include <linux/spinlock.h>
35 #include <linux/string.h>
36 #include <linux/types.h>
37
38 #include <socketcan/can.h>
39 #include <socketcan/can/dev.h>
40 #include <socketcan/can/error.h>
41
42 #include <mach/board.h>
43
44 #define DRV_NAME                "at91_can"
45 #define AT91_NAPI_WEIGHT        12
46
47 /*
48  * RX/TX Mailbox split
49  * don't dare to touch
50  */
51 #define AT91_MB_RX_NUM          12
52 #define AT91_MB_TX_SHIFT        2
53
54 #define AT91_MB_RX_FIRST        0
55 #define AT91_MB_RX_LAST         (AT91_MB_RX_FIRST + AT91_MB_RX_NUM - 1)
56
57 #define AT91_MB_RX_MASK(i)      ((1 << (i)) - 1)
58 #define AT91_MB_RX_SPLIT        8
59 #define AT91_MB_RX_LOW_LAST     (AT91_MB_RX_SPLIT - 1)
60 #define AT91_MB_RX_LOW_MASK     (AT91_MB_RX_MASK(AT91_MB_RX_SPLIT))
61
62 #define AT91_MB_TX_NUM          (1 << AT91_MB_TX_SHIFT)
63 #define AT91_MB_TX_FIRST        (AT91_MB_RX_LAST + 1)
64 #define AT91_MB_TX_LAST         (AT91_MB_TX_FIRST + AT91_MB_TX_NUM - 1)
65
66 #define AT91_NEXT_PRIO_SHIFT    (AT91_MB_TX_SHIFT)
67 #define AT91_NEXT_PRIO_MASK     (0xf << AT91_MB_TX_SHIFT)
68 #define AT91_NEXT_MB_MASK       (AT91_MB_TX_NUM - 1)
69 #define AT91_NEXT_MASK          ((AT91_MB_TX_NUM - 1) | AT91_NEXT_PRIO_MASK)
70
71 /* Common registers */
72 enum at91_reg {
73         AT91_MR         = 0x000,
74         AT91_IER        = 0x004,
75         AT91_IDR        = 0x008,
76         AT91_IMR        = 0x00C,
77         AT91_SR         = 0x010,
78         AT91_BR         = 0x014,
79         AT91_TIM        = 0x018,
80         AT91_TIMESTP    = 0x01C,
81         AT91_ECR        = 0x020,
82         AT91_TCR        = 0x024,
83         AT91_ACR        = 0x028,
84 };
85
86 /* Mailbox registers (0 <= i <= 15) */
87 #define AT91_MMR(i)             (enum at91_reg)(0x200 + ((i) * 0x20))
88 #define AT91_MAM(i)             (enum at91_reg)(0x204 + ((i) * 0x20))
89 #define AT91_MID(i)             (enum at91_reg)(0x208 + ((i) * 0x20))
90 #define AT91_MFID(i)            (enum at91_reg)(0x20C + ((i) * 0x20))
91 #define AT91_MSR(i)             (enum at91_reg)(0x210 + ((i) * 0x20))
92 #define AT91_MDL(i)             (enum at91_reg)(0x214 + ((i) * 0x20))
93 #define AT91_MDH(i)             (enum at91_reg)(0x218 + ((i) * 0x20))
94 #define AT91_MCR(i)             (enum at91_reg)(0x21C + ((i) * 0x20))
95
96 /* Register bits */
97 #define AT91_MR_CANEN           BIT(0)
98 #define AT91_MR_LPM             BIT(1)
99 #define AT91_MR_ABM             BIT(2)
100 #define AT91_MR_OVL             BIT(3)
101 #define AT91_MR_TEOF            BIT(4)
102 #define AT91_MR_TTM             BIT(5)
103 #define AT91_MR_TIMFRZ          BIT(6)
104 #define AT91_MR_DRPT            BIT(7)
105
106 #define AT91_SR_RBSY            BIT(29)
107
108 #define AT91_MMR_PRIO_SHIFT     (16)
109
110 #define AT91_MID_MIDE           BIT(29)
111
112 #define AT91_MSR_MRTR           BIT(20)
113 #define AT91_MSR_MABT           BIT(22)
114 #define AT91_MSR_MRDY           BIT(23)
115 #define AT91_MSR_MMI            BIT(24)
116
117 #define AT91_MCR_MRTR           BIT(20)
118 #define AT91_MCR_MTCR           BIT(23)
119
120 /* Mailbox Modes */
121 enum at91_mb_mode {
122         AT91_MB_MODE_DISABLED   = 0,
123         AT91_MB_MODE_RX         = 1,
124         AT91_MB_MODE_RX_OVRWR   = 2,
125         AT91_MB_MODE_TX         = 3,
126         AT91_MB_MODE_CONSUMER   = 4,
127         AT91_MB_MODE_PRODUCER   = 5,
128 };
129
130 /* Interrupt mask bits */
131 #define AT91_IRQ_MB_RX          ((1 << (AT91_MB_RX_LAST + 1)) \
132                                  - (1 << AT91_MB_RX_FIRST))
133 #define AT91_IRQ_MB_TX          ((1 << (AT91_MB_TX_LAST + 1)) \
134                                  - (1 << AT91_MB_TX_FIRST))
135 #define AT91_IRQ_MB_ALL         (AT91_IRQ_MB_RX | AT91_IRQ_MB_TX)
136
137 #define AT91_IRQ_ERRA           (1 << 16)
138 #define AT91_IRQ_WARN           (1 << 17)
139 #define AT91_IRQ_ERRP           (1 << 18)
140 #define AT91_IRQ_BOFF           (1 << 19)
141 #define AT91_IRQ_SLEEP          (1 << 20)
142 #define AT91_IRQ_WAKEUP         (1 << 21)
143 #define AT91_IRQ_TOVF           (1 << 22)
144 #define AT91_IRQ_TSTP           (1 << 23)
145 #define AT91_IRQ_CERR           (1 << 24)
146 #define AT91_IRQ_SERR           (1 << 25)
147 #define AT91_IRQ_AERR           (1 << 26)
148 #define AT91_IRQ_FERR           (1 << 27)
149 #define AT91_IRQ_BERR           (1 << 28)
150
151 #define AT91_IRQ_ERR_ALL        (0x1fff0000)
152 #define AT91_IRQ_ERR_FRAME      (AT91_IRQ_CERR | AT91_IRQ_SERR | \
153                                  AT91_IRQ_AERR | AT91_IRQ_FERR | AT91_IRQ_BERR)
154 #define AT91_IRQ_ERR_LINE       (AT91_IRQ_ERRA | AT91_IRQ_WARN | \
155                                  AT91_IRQ_ERRP | AT91_IRQ_BOFF)
156
157 #define AT91_IRQ_ALL            (0x1fffffff)
158
159 struct at91_priv {
160         struct can_priv         can;       /* must be the first member! */
161         struct net_device       *dev;
162         struct napi_struct      napi;
163
164         void __iomem            *reg_base;
165
166         u32                     reg_sr;
167         unsigned int            tx_next;
168         unsigned int            tx_echo;
169         unsigned int            rx_next;
170
171         struct clk              *clk;
172         struct at91_can_data    *pdata;
173 };
174
175 static struct can_bittiming_const at91_bittiming_const = {
176         .tseg1_min      = 4,
177         .tseg1_max      = 16,
178         .tseg2_min      = 2,
179         .tseg2_max      = 8,
180         .sjw_max        = 4,
181         .brp_min        = 2,
182         .brp_max        = 128,
183         .brp_inc        = 1,
184 };
185
186 static inline int get_tx_next_mb(const struct at91_priv *priv)
187 {
188         return (priv->tx_next & AT91_NEXT_MB_MASK) + AT91_MB_TX_FIRST;
189 }
190
191 static inline int get_tx_next_prio(const struct at91_priv *priv)
192 {
193         return (priv->tx_next >> AT91_NEXT_PRIO_SHIFT) & 0xf;
194 }
195
196 static inline int get_tx_echo_mb(const struct at91_priv *priv)
197 {
198         return (priv->tx_echo & AT91_NEXT_MB_MASK) + AT91_MB_TX_FIRST;
199 }
200
201 static inline u32 at91_read(const struct at91_priv *priv, enum at91_reg reg)
202 {
203         return readl(priv->reg_base + reg);
204 }
205
206 static inline void at91_write(const struct at91_priv *priv, enum at91_reg reg,
207                 u32 value)
208 {
209         writel(value, priv->reg_base + reg);
210 }
211
212 static inline void set_mb_mode_prio(const struct at91_priv *priv,
213                 unsigned int mb, enum at91_mb_mode mode, int prio)
214 {
215         at91_write(priv, AT91_MMR(mb), (mode << 24) | (prio << 16));
216 }
217
218 static inline void set_mb_mode(const struct at91_priv *priv, unsigned int mb,
219                 enum at91_mb_mode mode)
220 {
221         set_mb_mode_prio(priv, mb, mode, 0);
222 }
223
224 /*
225  * Swtich transceiver on or off
226  */
227 static void at91_transceiver_switch(const struct at91_priv *priv, int on)
228 {
229         if (priv->pdata && priv->pdata->transceiver_switch)
230                 priv->pdata->transceiver_switch(on);
231 }
232
233 static void at91_setup_mailboxes(struct net_device *dev)
234 {
235         struct at91_priv *priv = netdev_priv(dev);
236         unsigned int i;
237
238         /*
239          * The first 12 mailboxes are used as a reception FIFO. The
240          * last mailbox is configured with overwrite option. The
241          * overwrite flag indicates a FIFO overflow.
242          */
243         for (i = AT91_MB_RX_FIRST; i < AT91_MB_RX_LAST; i++)
244                 set_mb_mode(priv, i, AT91_MB_MODE_RX);
245         set_mb_mode(priv, AT91_MB_RX_LAST, AT91_MB_MODE_RX_OVRWR);
246
247         /* The last 4 mailboxes are used for transmitting. */
248         for (i = AT91_MB_TX_FIRST; i <= AT91_MB_TX_LAST; i++)
249                 set_mb_mode_prio(priv, i, AT91_MB_MODE_TX, 0);
250
251         /* Reset tx and rx helper pointers */
252         priv->tx_next = priv->tx_echo = priv->rx_next = 0;
253 }
254
255 static int at91_set_bittiming(struct net_device *dev)
256 {
257         const struct at91_priv *priv = netdev_priv(dev);
258         const struct can_bittiming *bt = &priv->can.bittiming;
259         u32 reg_br;
260
261         reg_br = ((priv->can.ctrlmode & CAN_CTRLMODE_3_SAMPLES) << 24) |
262                 ((bt->brp - 1) << 16) | ((bt->sjw - 1) << 12) |
263                 ((bt->prop_seg - 1) << 8) | ((bt->phase_seg1 - 1) << 4) |
264                 ((bt->phase_seg2 - 1) << 0);
265
266         dev_info(ND2D(dev), "writing AT91_BR: 0x%08x\n", reg_br);
267
268         at91_write(priv, AT91_BR, reg_br);
269
270         return 0;
271 }
272
273 static void at91_chip_start(struct net_device *dev)
274 {
275         struct at91_priv *priv = netdev_priv(dev);
276         u32 reg_mr, reg_ier;
277
278         /* disable interrupts */
279         at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
280
281         /* disable chip */
282         reg_mr = at91_read(priv, AT91_MR);
283         at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN);
284
285         at91_setup_mailboxes(dev);
286         at91_transceiver_switch(priv, 1);
287
288         /* enable chip */
289         at91_write(priv, AT91_MR, AT91_MR_CANEN);
290
291         priv->can.state = CAN_STATE_ERROR_ACTIVE;
292
293         /* Enable interrupts */
294         reg_ier = AT91_IRQ_MB_RX | AT91_IRQ_ERRP | AT91_IRQ_ERR_FRAME;
295         at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
296         at91_write(priv, AT91_IER, reg_ier);
297 }
298
299 static void at91_chip_stop(struct net_device *dev, enum can_state state)
300 {
301         struct at91_priv *priv = netdev_priv(dev);
302         u32 reg_mr;
303
304         /* disable interrupts */
305         at91_write(priv, AT91_IDR, AT91_IRQ_ALL);
306
307         reg_mr = at91_read(priv, AT91_MR);
308         at91_write(priv, AT91_MR, reg_mr & ~AT91_MR_CANEN);
309
310         at91_transceiver_switch(priv, 0);
311         priv->can.state = state;
312 }
313
314 /*
315  * theory of operation:
316  *
317  * According to the datasheet priority 0 is the highest priority, 15
318  * is the lowest. If two mailboxes have the same priority level the
319  * message of the mailbox with the lowest number is sent first.
320  *
321  * We use the first TX mailbox (AT91_MB_TX_FIRST) with prio 0, then
322  * the next mailbox with prio 0, and so on, until all mailboxes are
323  * used. Then we start from the beginning with mailbox
324  * AT91_MB_TX_FIRST, but with prio 1, mailbox AT91_MB_TX_FIRST + 1
325  * prio 1. When we reach the last mailbox with prio 15, we have to
326  * stop sending, waiting for all messages to be delivered, then start
327  * again with mailbox AT91_MB_TX_FIRST prio 0.
328  *
329  * We use the priv->tx_next as counter for the next transmission
330  * mailbox, but without the offset AT91_MB_TX_FIRST. The lower bits
331  * encode the mailbox number, the upper 4 bits the mailbox priority:
332  *
333  * priv->tx_next = (prio << AT91_NEXT_PRIO_SHIFT) ||
334  *                 (mb - AT91_MB_TX_FIRST);
335  *
336  */
337 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
338 static int at91_start_xmit(struct sk_buff *skb, struct net_device *dev)
339 #else
340 static netdev_tx_t at91_start_xmit(struct sk_buff *skb, struct net_device *dev)
341 #endif
342 {
343         struct at91_priv *priv = netdev_priv(dev);
344         struct net_device_stats *stats = &dev->stats;
345         struct can_frame *cf = (struct can_frame *)skb->data;
346         unsigned int mb, prio;
347         u32 reg_mid, reg_mcr;
348
349         if (can_dropped_invalid_skb(dev, skb))
350                 return NETDEV_TX_OK;
351
352         mb = get_tx_next_mb(priv);
353         prio = get_tx_next_prio(priv);
354
355         if (unlikely(!(at91_read(priv, AT91_MSR(mb)) & AT91_MSR_MRDY))) {
356                 netif_stop_queue(dev);
357
358                 dev_err(ND2D(dev),
359                         "BUG! TX buffer full when queue awake!\n");
360                 return NETDEV_TX_BUSY;
361         }
362
363         if (cf->can_id & CAN_EFF_FLAG)
364                 reg_mid = (cf->can_id & CAN_EFF_MASK) | AT91_MID_MIDE;
365         else
366                 reg_mid = (cf->can_id & CAN_SFF_MASK) << 18;
367
368         reg_mcr = ((cf->can_id & CAN_RTR_FLAG) ? AT91_MCR_MRTR : 0) |
369                 (cf->can_dlc << 16) | AT91_MCR_MTCR;
370
371         /* disable MB while writing ID (see datasheet) */
372         set_mb_mode(priv, mb, AT91_MB_MODE_DISABLED);
373         at91_write(priv, AT91_MID(mb), reg_mid);
374         set_mb_mode_prio(priv, mb, AT91_MB_MODE_TX, prio);
375
376         at91_write(priv, AT91_MDL(mb), *(u32 *)(cf->data + 0));
377         at91_write(priv, AT91_MDH(mb), *(u32 *)(cf->data + 4));
378
379         /* This triggers transmission */
380         at91_write(priv, AT91_MCR(mb), reg_mcr);
381
382         stats->tx_bytes += cf->can_dlc;
383         dev->trans_start = jiffies;
384
385         /* _NOTE_: substract AT91_MB_TX_FIRST offset from mb! */
386         can_put_echo_skb(skb, dev, mb - AT91_MB_TX_FIRST);
387
388         /*
389          * we have to stop the queue and deliver all messages in case
390          * of a prio+mb counter wrap around. This is the case if
391          * tx_next buffer prio and mailbox equals 0.
392          *
393          * also stop the queue if next buffer is still in use
394          * (== not ready)
395          */
396         priv->tx_next++;
397         if (!(at91_read(priv, AT91_MSR(get_tx_next_mb(priv))) &
398               AT91_MSR_MRDY) ||
399             (priv->tx_next & AT91_NEXT_MASK) == 0)
400                 netif_stop_queue(dev);
401
402         /* Enable interrupt for this mailbox */
403         at91_write(priv, AT91_IER, 1 << mb);
404
405         return NETDEV_TX_OK;
406 }
407
408 /**
409  * at91_activate_rx_low - activate lower rx mailboxes
410  * @priv: a91 context
411  *
412  * Reenables the lower mailboxes for reception of new CAN messages
413  */
414 static inline void at91_activate_rx_low(const struct at91_priv *priv)
415 {
416         u32 mask = AT91_MB_RX_LOW_MASK;
417         at91_write(priv, AT91_TCR, mask);
418 }
419
420 /**
421  * at91_activate_rx_mb - reactive single rx mailbox
422  * @priv: a91 context
423  * @mb: mailbox to reactivate
424  *
425  * Reenables given mailbox for reception of new CAN messages
426  */
427 static inline void at91_activate_rx_mb(const struct at91_priv *priv,
428                 unsigned int mb)
429 {
430         u32 mask = 1 << mb;
431         at91_write(priv, AT91_TCR, mask);
432 }
433
434 /**
435  * at91_rx_overflow_err - send error frame due to rx overflow
436  * @dev: net device
437  */
438 static void at91_rx_overflow_err(struct net_device *dev)
439 {
440         struct net_device_stats *stats = &dev->stats;
441         struct sk_buff *skb;
442         struct can_frame *cf;
443
444         dev_dbg(ND2D(dev), "RX buffer overflow\n");
445         stats->rx_over_errors++;
446         stats->rx_errors++;
447
448         skb = alloc_can_err_skb(dev, &cf);
449         if (unlikely(!skb))
450                 return;
451
452         cf->can_id |= CAN_ERR_CRTL;
453         cf->data[1] = CAN_ERR_CRTL_RX_OVERFLOW;
454         netif_receive_skb(skb);
455
456 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
457         dev->last_rx = jiffies;
458 #endif
459         stats->rx_packets++;
460         stats->rx_bytes += cf->can_dlc;
461 }
462
463 /**
464  * at91_read_mb - read CAN msg from mailbox (lowlevel impl)
465  * @dev: net device
466  * @mb: mailbox number to read from
467  * @cf: can frame where to store message
468  *
469  * Reads a CAN message from the given mailbox and stores data into
470  * given can frame. "mb" and "cf" must be valid.
471  */
472 static void at91_read_mb(struct net_device *dev, unsigned int mb,
473                 struct can_frame *cf)
474 {
475         const struct at91_priv *priv = netdev_priv(dev);
476         u32 reg_msr, reg_mid;
477
478         reg_mid = at91_read(priv, AT91_MID(mb));
479         if (reg_mid & AT91_MID_MIDE)
480                 cf->can_id = ((reg_mid >> 0) & CAN_EFF_MASK) | CAN_EFF_FLAG;
481         else
482                 cf->can_id = (reg_mid >> 18) & CAN_SFF_MASK;
483
484         reg_msr = at91_read(priv, AT91_MSR(mb));
485         if (reg_msr & AT91_MSR_MRTR)
486                 cf->can_id |= CAN_RTR_FLAG;
487         cf->can_dlc = get_can_dlc((reg_msr >> 16) & 0xf);
488
489         *(u32 *)(cf->data + 0) = at91_read(priv, AT91_MDL(mb));
490         *(u32 *)(cf->data + 4) = at91_read(priv, AT91_MDH(mb));
491
492         if (unlikely(mb == AT91_MB_RX_LAST && reg_msr & AT91_MSR_MMI))
493                 at91_rx_overflow_err(dev);
494 }
495
496 /**
497  * at91_read_msg - read CAN message from mailbox
498  * @dev: net device
499  * @mb: mail box to read from
500  *
501  * Reads a CAN message from given mailbox, and put into linux network
502  * RX queue, does all housekeeping chores (stats, ...)
503  */
504 static void at91_read_msg(struct net_device *dev, unsigned int mb)
505 {
506         struct net_device_stats *stats = &dev->stats;
507         struct can_frame *cf;
508         struct sk_buff *skb;
509
510         skb = alloc_can_skb(dev, &cf);
511         if (unlikely(!skb)) {
512                 stats->rx_dropped++;
513                 return;
514         }
515
516         at91_read_mb(dev, mb, cf);
517         netif_receive_skb(skb);
518
519 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
520         dev->last_rx = jiffies;
521 #endif
522         stats->rx_packets++;
523         stats->rx_bytes += cf->can_dlc;
524 }
525
526 /**
527  * at91_poll_rx - read multiple CAN messages from mailboxes
528  * @dev: net device
529  * @quota: max number of pkgs we're allowed to receive
530  *
531  * Theory of Operation:
532  *
533  * 12 of the 16 mailboxes on the chip are reserved for RX. we split
534  * them into 2 groups. The lower group holds 8 and upper 4 mailboxes.
535  *
536  * Like it or not, but the chip always saves a received CAN message
537  * into the first free mailbox it finds (starting with the
538  * lowest). This makes it very difficult to read the messages in the
539  * right order from the chip. This is how we work around that problem:
540  *
541  * The first message goes into mb nr. 0 and issues an interrupt. All
542  * rx ints are disabled in the interrupt handler and a napi poll is
543  * scheduled. We read the mailbox, but do _not_ reenable the mb (to
544  * receive another message).
545  *
546  *    lower mbxs      upper
547  *   ______^______    __^__
548  *  /             \  /     \
549  * +-+-+-+-+-+-+-+-++-+-+-+-+
550  * |x|x|x|x|x|x|x|x|| | | | |
551  * +-+-+-+-+-+-+-+-++-+-+-+-+
552  *  0 0 0 0 0 0  0 0 0 0 1 1  \ mail
553  *  0 1 2 3 4 5  6 7 8 9 0 1  / box
554  *
555  * The variable priv->rx_next points to the next mailbox to read a
556  * message from. As long we're in the lower mailboxes we just read the
557  * mailbox but not reenable it.
558  *
559  * With completion of the last of the lower mailboxes, we reenable the
560  * whole first group, but continue to look for filled mailboxes in the
561  * upper mailboxes. Imagine the second group like overflow mailboxes,
562  * which takes CAN messages if the lower goup is full. While in the
563  * upper group we reenable the mailbox right after reading it. Giving
564  * the chip more room to store messages.
565  *
566  * After finishing we look again in the lower group if we've still
567  * quota.
568  *
569  */
570 static int at91_poll_rx(struct net_device *dev, int quota)
571 {
572         struct at91_priv *priv = netdev_priv(dev);
573         u32 reg_sr = at91_read(priv, AT91_SR);
574         const unsigned long *addr = (unsigned long *)&reg_sr;
575         unsigned int mb;
576         int received = 0;
577
578         if (priv->rx_next > AT91_MB_RX_LOW_LAST &&
579             reg_sr & AT91_MB_RX_LOW_MASK)
580                 dev_info(ND2D(dev),
581                          "order of incoming frames cannot be guaranteed\n");
582
583  again:
584         for (mb = find_next_bit(addr, AT91_MB_RX_NUM, priv->rx_next);
585              mb < AT91_MB_RX_NUM && quota > 0;
586              reg_sr = at91_read(priv, AT91_SR),
587              mb = find_next_bit(addr, AT91_MB_RX_NUM, ++priv->rx_next)) {
588                 at91_read_msg(dev, mb);
589
590                 /* reactivate mailboxes */
591                 if (mb == AT91_MB_RX_LOW_LAST)
592                         /* all lower mailboxed, if just finished it */
593                         at91_activate_rx_low(priv);
594                 else if (mb > AT91_MB_RX_LOW_LAST)
595                         /* only the mailbox we read */
596                         at91_activate_rx_mb(priv, mb);
597
598                 received++;
599                 quota--;
600         }
601
602         /* upper group completed, look again in lower */
603         if (priv->rx_next > AT91_MB_RX_LOW_LAST &&
604             quota > 0 && mb >= AT91_MB_RX_NUM) {
605                 priv->rx_next = 0;
606                 goto again;
607         }
608
609         return received;
610 }
611
612 static void at91_poll_err_frame(struct net_device *dev,
613                 struct can_frame *cf, u32 reg_sr)
614 {
615         struct at91_priv *priv = netdev_priv(dev);
616
617         /* CRC error */
618         if (reg_sr & AT91_IRQ_CERR) {
619                 dev_dbg(ND2D(dev), "CERR irq\n");
620                 dev->stats.rx_errors++;
621                 priv->can.can_stats.bus_error++;
622                 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
623         }
624
625         /* Stuffing Error */
626         if (reg_sr & AT91_IRQ_SERR) {
627                 dev_dbg(ND2D(dev), "SERR irq\n");
628                 dev->stats.rx_errors++;
629                 priv->can.can_stats.bus_error++;
630                 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
631                 cf->data[2] |= CAN_ERR_PROT_STUFF;
632         }
633
634         /* Acknowledgement Error */
635         if (reg_sr & AT91_IRQ_AERR) {
636                 dev_dbg(ND2D(dev), "AERR irq\n");
637                 dev->stats.tx_errors++;
638                 cf->can_id |= CAN_ERR_ACK;
639         }
640
641         /* Form error */
642         if (reg_sr & AT91_IRQ_FERR) {
643                 dev_dbg(ND2D(dev), "FERR irq\n");
644                 dev->stats.rx_errors++;
645                 priv->can.can_stats.bus_error++;
646                 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
647                 cf->data[2] |= CAN_ERR_PROT_FORM;
648         }
649
650         /* Bit Error */
651         if (reg_sr & AT91_IRQ_BERR) {
652                 dev_dbg(ND2D(dev), "BERR irq\n");
653                 dev->stats.tx_errors++;
654                 priv->can.can_stats.bus_error++;
655                 cf->can_id |= CAN_ERR_PROT | CAN_ERR_BUSERROR;
656                 cf->data[2] |= CAN_ERR_PROT_BIT;
657         }
658 }
659
660 static int at91_poll_err(struct net_device *dev, int quota, u32 reg_sr)
661 {
662         struct sk_buff *skb;
663         struct can_frame *cf;
664
665         if (quota == 0)
666                 return 0;
667
668         skb = alloc_can_err_skb(dev, &cf);
669         if (unlikely(!skb))
670                 return 0;
671
672         at91_poll_err_frame(dev, cf, reg_sr);
673         netif_receive_skb(skb);
674
675 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
676         dev->last_rx = jiffies;
677 #endif
678         dev->stats.rx_packets++;
679         dev->stats.rx_bytes += cf->can_dlc;
680
681         return 1;
682 }
683
684 static int at91_poll(struct napi_struct *napi, int quota)
685 {
686         struct net_device *dev = napi->dev;
687         const struct at91_priv *priv = netdev_priv(dev);
688         u32 reg_sr = at91_read(priv, AT91_SR);
689         int work_done = 0;
690
691         if (reg_sr & AT91_IRQ_MB_RX)
692                 work_done += at91_poll_rx(dev, quota - work_done);
693
694         /*
695          * The error bits are clear on read,
696          * so use saved value from irq handler.
697          */
698         reg_sr |= priv->reg_sr;
699         if (reg_sr & AT91_IRQ_ERR_FRAME)
700                 work_done += at91_poll_err(dev, quota - work_done, reg_sr);
701
702         if (work_done < quota) {
703                 /* enable IRQs for frame errors and all mailboxes >= rx_next */
704                 u32 reg_ier = AT91_IRQ_ERR_FRAME;
705                 reg_ier |= AT91_IRQ_MB_RX & ~AT91_MB_RX_MASK(priv->rx_next);
706
707                 napi_complete(napi);
708                 at91_write(priv, AT91_IER, reg_ier);
709         }
710
711         return work_done;
712 }
713
714 /*
715  * theory of operation:
716  *
717  * priv->tx_echo holds the number of the oldest can_frame put for
718  * transmission into the hardware, but not yet ACKed by the CAN tx
719  * complete IRQ.
720  *
721  * We iterate from priv->tx_echo to priv->tx_next and check if the
722  * packet has been transmitted, echo it back to the CAN framework. If
723  * we discover a not yet transmitted package, stop looking for more.
724  *
725  */
726 static void at91_irq_tx(struct net_device *dev, u32 reg_sr)
727 {
728         struct at91_priv *priv = netdev_priv(dev);
729         u32 reg_msr;
730         unsigned int mb;
731
732         /* masking of reg_sr not needed, already done by at91_irq */
733
734         for (/* nix */; (priv->tx_next - priv->tx_echo) > 0; priv->tx_echo++) {
735                 mb = get_tx_echo_mb(priv);
736
737                 /* no event in mailbox? */
738                 if (!(reg_sr & (1 << mb)))
739                         break;
740
741                 /* Disable irq for this TX mailbox */
742                 at91_write(priv, AT91_IDR, 1 << mb);
743
744                 /*
745                  * only echo if mailbox signals us a transfer
746                  * complete (MSR_MRDY). Otherwise it's a tansfer
747                  * abort. "can_bus_off()" takes care about the skbs
748                  * parked in the echo queue.
749                  */
750                 reg_msr = at91_read(priv, AT91_MSR(mb));
751                 if (likely(reg_msr & AT91_MSR_MRDY &&
752                            ~reg_msr & AT91_MSR_MABT)) {
753                         /* _NOTE_: substract AT91_MB_TX_FIRST offset from mb! */
754                         can_get_echo_skb(dev, mb - AT91_MB_TX_FIRST);
755                         dev->stats.tx_packets++;
756                 }
757         }
758
759         /*
760          * restart queue if we don't have a wrap around but restart if
761          * we get a TX int for the last can frame directly before a
762          * wrap around.
763          */
764         if ((priv->tx_next & AT91_NEXT_MASK) != 0 ||
765             (priv->tx_echo & AT91_NEXT_MASK) == 0)
766                 netif_wake_queue(dev);
767 }
768
769 static void at91_irq_err_state(struct net_device *dev,
770                 struct can_frame *cf, enum can_state new_state)
771 {
772         struct at91_priv *priv = netdev_priv(dev);
773         u32 reg_idr, reg_ier, reg_ecr;
774         u8 tec, rec;
775
776         reg_ecr = at91_read(priv, AT91_ECR);
777         rec = reg_ecr & 0xff;
778         tec = reg_ecr >> 16;
779
780         switch (priv->can.state) {
781         case CAN_STATE_ERROR_ACTIVE:
782                 /*
783                  * from: ERROR_ACTIVE
784                  * to  : ERROR_WARNING, ERROR_PASSIVE, BUS_OFF
785                  * =>  : there was a warning int
786                  */
787                 if (new_state >= CAN_STATE_ERROR_WARNING &&
788                     new_state <= CAN_STATE_BUS_OFF) {
789                         dev_dbg(ND2D(dev), "Error Warning IRQ\n");
790                         priv->can.can_stats.error_warning++;
791
792                         cf->can_id |= CAN_ERR_CRTL;
793                         cf->data[1] = (tec > rec) ?
794                                 CAN_ERR_CRTL_TX_WARNING :
795                                 CAN_ERR_CRTL_RX_WARNING;
796                 }
797         case CAN_STATE_ERROR_WARNING:   /* fallthrough */
798                 /*
799                  * from: ERROR_ACTIVE, ERROR_WARNING
800                  * to  : ERROR_PASSIVE, BUS_OFF
801                  * =>  : error passive int
802                  */
803                 if (new_state >= CAN_STATE_ERROR_PASSIVE &&
804                     new_state <= CAN_STATE_BUS_OFF) {
805                         dev_dbg(ND2D(dev), "Error Passive IRQ\n");
806                         priv->can.can_stats.error_passive++;
807
808                         cf->can_id |= CAN_ERR_CRTL;
809                         cf->data[1] = (tec > rec) ?
810                                 CAN_ERR_CRTL_TX_PASSIVE :
811                                 CAN_ERR_CRTL_RX_PASSIVE;
812                 }
813                 break;
814         case CAN_STATE_BUS_OFF:
815                 /*
816                  * from: BUS_OFF
817                  * to  : ERROR_ACTIVE, ERROR_WARNING, ERROR_PASSIVE
818                  */
819                 if (new_state <= CAN_STATE_ERROR_PASSIVE) {
820                         cf->can_id |= CAN_ERR_RESTARTED;
821
822                         dev_dbg(dev->dev.parent, "restarted\n");
823                         priv->can.can_stats.restarts++;
824
825                         netif_carrier_on(dev);
826                         netif_wake_queue(dev);
827                 }
828                 break;
829         default:
830                 break;
831         }
832
833
834         /* process state changes depending on the new state */
835         switch (new_state) {
836         case CAN_STATE_ERROR_ACTIVE:
837                 /*
838                  * actually we want to enable AT91_IRQ_WARN here, but
839                  * it screws up the system under certain
840                  * circumstances. so just enable AT91_IRQ_ERRP, thus
841                  * the "fallthrough"
842                  */
843                 dev_dbg(ND2D(dev), "Error Active\n");
844                 cf->can_id |= CAN_ERR_PROT;
845                 cf->data[2] = CAN_ERR_PROT_ACTIVE;
846         case CAN_STATE_ERROR_WARNING:   /* fallthrough */
847                 reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_BOFF;
848                 reg_ier = AT91_IRQ_ERRP;
849                 break;
850         case CAN_STATE_ERROR_PASSIVE:
851                 reg_idr = AT91_IRQ_ERRA | AT91_IRQ_WARN | AT91_IRQ_ERRP;
852                 reg_ier = AT91_IRQ_BOFF;
853                 break;
854         case CAN_STATE_BUS_OFF:
855                 reg_idr = AT91_IRQ_ERRA | AT91_IRQ_ERRP |
856                         AT91_IRQ_WARN | AT91_IRQ_BOFF;
857                 reg_ier = 0;
858
859                 cf->can_id |= CAN_ERR_BUSOFF;
860
861                 dev_dbg(ND2D(dev), "bus-off\n");
862                 netif_carrier_off(dev);
863                 priv->can.can_stats.bus_off++;
864
865                 /* turn off chip, if restart is disabled */
866                 if (!priv->can.restart_ms) {
867                         at91_chip_stop(dev, CAN_STATE_BUS_OFF);
868                         return;
869                 }
870                 break;
871         default:
872                 break;
873         }
874
875         at91_write(priv, AT91_IDR, reg_idr);
876         at91_write(priv, AT91_IER, reg_ier);
877 }
878
879 static void at91_irq_err(struct net_device *dev)
880 {
881         struct at91_priv *priv = netdev_priv(dev);
882         struct sk_buff *skb;
883         struct can_frame *cf;
884         enum can_state new_state;
885         u32 reg_sr;
886
887         reg_sr = at91_read(priv, AT91_SR);
888
889         /* we need to look at the unmasked reg_sr */
890         if (unlikely(reg_sr & AT91_IRQ_BOFF))
891                 new_state = CAN_STATE_BUS_OFF;
892         else if (unlikely(reg_sr & AT91_IRQ_ERRP))
893                 new_state = CAN_STATE_ERROR_PASSIVE;
894         else if (unlikely(reg_sr & AT91_IRQ_WARN))
895                 new_state = CAN_STATE_ERROR_WARNING;
896         else if (likely(reg_sr & AT91_IRQ_ERRA))
897                 new_state = CAN_STATE_ERROR_ACTIVE;
898         else {
899                 dev_err(ND2D(dev), "BUG! hardware in undefined state\n");
900                 return;
901         }
902
903         /* state hasn't changed */
904         if (likely(new_state == priv->can.state))
905                 return;
906
907         skb = alloc_can_err_skb(dev, &cf);
908         if (unlikely(!skb))
909                 return;
910
911         at91_irq_err_state(dev, cf, new_state);
912         netif_rx(skb);
913
914 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,32)
915         dev->last_rx = jiffies;
916 #endif
917         dev->stats.rx_packets++;
918         dev->stats.rx_bytes += cf->can_dlc;
919
920         priv->can.state = new_state;
921 }
922
923 /*
924  * interrupt handler
925  */
926 static irqreturn_t at91_irq(int irq, void *dev_id)
927 {
928         struct net_device *dev = dev_id;
929         struct at91_priv *priv = netdev_priv(dev);
930         irqreturn_t handled = IRQ_NONE;
931         u32 reg_sr, reg_imr;
932
933         reg_sr = at91_read(priv, AT91_SR);
934         reg_imr = at91_read(priv, AT91_IMR);
935
936         /* Ignore masked interrupts */
937         reg_sr &= reg_imr;
938         if (!reg_sr)
939                 goto exit;
940
941         handled = IRQ_HANDLED;
942
943         /* Receive or error interrupt? -> napi */
944         if (reg_sr & (AT91_IRQ_MB_RX | AT91_IRQ_ERR_FRAME)) {
945                 /*
946                  * The error bits are clear on read,
947                  * save for later use.
948                  */
949                 priv->reg_sr = reg_sr;
950                 at91_write(priv, AT91_IDR,
951                            AT91_IRQ_MB_RX | AT91_IRQ_ERR_FRAME);
952                 napi_schedule(&priv->napi);
953         }
954
955         /* Transmission complete interrupt */
956         if (reg_sr & AT91_IRQ_MB_TX)
957                 at91_irq_tx(dev, reg_sr);
958
959         at91_irq_err(dev);
960
961  exit:
962         return handled;
963 }
964
965 static int at91_open(struct net_device *dev)
966 {
967         struct at91_priv *priv = netdev_priv(dev);
968         int err;
969
970         clk_enable(priv->clk);
971
972         /* check or determine and set bittime */
973         err = open_candev(dev);
974         if (err)
975                 goto out;
976
977         /* register interrupt handler */
978         if (request_irq(dev->irq, at91_irq, IRQF_SHARED,
979                         dev->name, dev)) {
980                 err = -EAGAIN;
981                 goto out_close;
982         }
983
984         /* start chip and queuing */
985         at91_chip_start(dev);
986         napi_enable(&priv->napi);
987         netif_start_queue(dev);
988
989         return 0;
990
991  out_close:
992         close_candev(dev);
993  out:
994         clk_disable(priv->clk);
995
996         return err;
997 }
998
999 /*
1000  * stop CAN bus activity
1001  */
1002 static int at91_close(struct net_device *dev)
1003 {
1004         struct at91_priv *priv = netdev_priv(dev);
1005
1006         netif_stop_queue(dev);
1007         napi_disable(&priv->napi);
1008         at91_chip_stop(dev, CAN_STATE_STOPPED);
1009
1010         free_irq(dev->irq, dev);
1011         clk_disable(priv->clk);
1012
1013         close_candev(dev);
1014
1015         return 0;
1016 }
1017
1018 static int at91_set_mode(struct net_device *dev, enum can_mode mode)
1019 {
1020         switch (mode) {
1021         case CAN_MODE_START:
1022                 at91_chip_start(dev);
1023                 netif_wake_queue(dev);
1024                 break;
1025
1026         default:
1027                 return -EOPNOTSUPP;
1028         }
1029
1030         return 0;
1031 }
1032
1033 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
1034 static const struct net_device_ops at91_netdev_ops = {
1035         .ndo_open       = at91_open,
1036         .ndo_stop       = at91_close,
1037         .ndo_start_xmit = at91_start_xmit,
1038 };
1039 #endif
1040
1041 static int __init at91_can_probe(struct platform_device *pdev)
1042 {
1043         struct net_device *dev;
1044         struct at91_priv *priv;
1045         struct resource *res;
1046         struct clk *clk;
1047         void __iomem *addr;
1048         int err, irq;
1049
1050         clk = clk_get(&pdev->dev, "can_clk");
1051         if (IS_ERR(clk)) {
1052                 dev_err(&pdev->dev, "no clock defined\n");
1053                 err = -ENODEV;
1054                 goto exit;
1055         }
1056
1057         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1058         irq = platform_get_irq(pdev, 0);
1059         if (!res || irq <= 0) {
1060                 err = -ENODEV;
1061                 goto exit_put;
1062         }
1063
1064         if (!request_mem_region(res->start,
1065                                 resource_size(res),
1066                                 pdev->name)) {
1067                 err = -EBUSY;
1068                 goto exit_put;
1069         }
1070
1071         addr = ioremap_nocache(res->start, resource_size(res));
1072         if (!addr) {
1073                 err = -ENOMEM;
1074                 goto exit_release;
1075         }
1076
1077         dev = alloc_candev(sizeof(struct at91_priv), AT91_MB_TX_NUM);
1078         if (!dev) {
1079                 err = -ENOMEM;
1080                 goto exit_iounmap;
1081         }
1082
1083 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,28)
1084         dev->netdev_ops = &at91_netdev_ops;
1085 #else
1086         dev->open = at91_open;
1087         dev->stop = at91_close;
1088         dev->hard_start_xmit = at91_start_xmit;
1089 #endif
1090         dev->irq = irq;
1091         dev->flags |= IFF_ECHO;
1092
1093         priv = netdev_priv(dev);
1094         priv->can.clock.freq = clk_get_rate(clk);
1095         priv->can.bittiming_const = &at91_bittiming_const;
1096         priv->can.do_set_bittiming = at91_set_bittiming;
1097         priv->can.do_set_mode = at91_set_mode;
1098         priv->can.ctrlmode_supported = CAN_CTRLMODE_3_SAMPLES;
1099         priv->reg_base = addr;
1100         priv->dev = dev;
1101         priv->clk = clk;
1102         priv->pdata = pdev->dev.platform_data;
1103
1104         netif_napi_add(dev, &priv->napi, at91_poll, AT91_NAPI_WEIGHT);
1105
1106         dev_set_drvdata(&pdev->dev, dev);
1107         SET_NETDEV_DEV(dev, &pdev->dev);
1108
1109         err = register_candev(dev);
1110         if (err) {
1111                 dev_err(&pdev->dev, "registering netdev failed\n");
1112                 goto exit_free;
1113         }
1114
1115         dev_info(&pdev->dev, "device registered (reg_base=%p, irq=%d)\n",
1116                  priv->reg_base, dev->irq);
1117
1118         return 0;
1119
1120  exit_free:
1121         free_netdev(dev);
1122  exit_iounmap:
1123         iounmap(addr);
1124  exit_release:
1125         release_mem_region(res->start, resource_size(res));
1126  exit_put:
1127         clk_put(clk);
1128  exit:
1129         return err;
1130 }
1131
1132 static int __devexit at91_can_remove(struct platform_device *pdev)
1133 {
1134         struct net_device *dev = platform_get_drvdata(pdev);
1135         struct at91_priv *priv = netdev_priv(dev);
1136         struct resource *res;
1137
1138         unregister_netdev(dev);
1139
1140         platform_set_drvdata(pdev, NULL);
1141
1142         free_netdev(dev);
1143
1144         iounmap(priv->reg_base);
1145
1146         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1147         release_mem_region(res->start, resource_size(res));
1148
1149         clk_put(priv->clk);
1150
1151         return 0;
1152 }
1153
1154 static struct platform_driver at91_can_driver = {
1155         .probe          = at91_can_probe,
1156         .remove         = __devexit_p(at91_can_remove),
1157         .driver         = {
1158                 .name   = DRV_NAME,
1159                 .owner  = THIS_MODULE,
1160         },
1161 };
1162
1163 static int __init at91_can_module_init(void)
1164 {
1165         printk(KERN_INFO "%s netdevice driver\n", DRV_NAME);
1166         return platform_driver_register(&at91_can_driver);
1167 }
1168
1169 static void __exit at91_can_module_exit(void)
1170 {
1171         platform_driver_unregister(&at91_can_driver);
1172         printk(KERN_INFO "%s: driver removed\n", DRV_NAME);
1173 }
1174
1175 module_init(at91_can_module_init);
1176 module_exit(at91_can_module_exit);
1177
1178 MODULE_AUTHOR("Marc Kleine-Budde <mkl@pengutronix.de>");
1179 MODULE_LICENSE("GPL v2");
1180 MODULE_DESCRIPTION(DRV_NAME " CAN netdevice driver");