NET: sa11x0-ir: move sa1100_irda_txdma_irq
[firefly-linux-kernel-4.4.55.git] / drivers / net / irda / sa1100_ir.c
1 /*
2  *  linux/drivers/net/irda/sa1100_ir.c
3  *
4  *  Copyright (C) 2000-2001 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  *  Infra-red driver for the StrongARM SA1100 embedded microprocessor
11  *
12  *  Note that we don't have to worry about the SA1111's DMA bugs in here,
13  *  so we use the straight forward dma_map_* functions with a null pointer.
14  *
15  *  This driver takes one kernel command line parameter, sa1100ir=, with
16  *  the following options:
17  *      max_rate:baudrate       - set the maximum baud rate
18  *      power_level:level       - set the transmitter power level
19  *      tx_lpm:0|1              - set transmit low power mode
20  */
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/errno.h>
26 #include <linux/netdevice.h>
27 #include <linux/slab.h>
28 #include <linux/rtnetlink.h>
29 #include <linux/interrupt.h>
30 #include <linux/delay.h>
31 #include <linux/platform_device.h>
32 #include <linux/dma-mapping.h>
33
34 #include <net/irda/irda.h>
35 #include <net/irda/wrapper.h>
36 #include <net/irda/irda_device.h>
37
38 #include <mach/dma.h>
39 #include <mach/hardware.h>
40 #include <asm/mach/irda.h>
41
42 static int power_level = 3;
43 static int tx_lpm;
44 static int max_rate = 4000000;
45
46 struct sa1100_buf {
47         struct sk_buff          *skb;
48         dma_addr_t              dma;
49         dma_regs_t              *regs;
50 };
51
52 struct sa1100_irda {
53         unsigned char           hscr0;
54         unsigned char           utcr4;
55         unsigned char           power;
56         unsigned char           open;
57
58         int                     speed;
59         int                     newspeed;
60
61         struct sa1100_buf       dma_rx;
62         struct sa1100_buf       dma_tx;
63
64         struct device           *dev;
65         struct irda_platform_data *pdata;
66         struct irlap_cb         *irlap;
67         struct qos_info         qos;
68
69         iobuff_t                tx_buff;
70         iobuff_t                rx_buff;
71
72         int (*tx_start)(struct sk_buff *, struct net_device *, struct sa1100_irda *);
73         irqreturn_t (*irq)(struct net_device *, struct sa1100_irda *);
74 };
75
76 static int sa1100_irda_set_speed(struct sa1100_irda *, int);
77
78 #define IS_FIR(si)              ((si)->speed >= 4000000)
79
80 #define HPSIR_MAX_RXLEN         2047
81
82 /*
83  * Allocate and map the receive buffer, unless it is already allocated.
84  */
85 static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
86 {
87         if (si->dma_rx.skb)
88                 return 0;
89
90         si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
91         if (!si->dma_rx.skb) {
92                 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
93                 return -ENOMEM;
94         }
95
96         /*
97          * Align any IP headers that may be contained
98          * within the frame.
99          */
100         skb_reserve(si->dma_rx.skb, 1);
101
102         si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
103                                         HPSIR_MAX_RXLEN,
104                                         DMA_FROM_DEVICE);
105         if (dma_mapping_error(si->dev, si->dma_rx.dma)) {
106                 dev_kfree_skb_any(si->dma_rx.skb);
107                 return -ENOMEM;
108         }
109
110         return 0;
111 }
112
113 /*
114  * We want to get here as soon as possible, and get the receiver setup.
115  * We use the existing buffer.
116  */
117 static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
118 {
119         if (!si->dma_rx.skb) {
120                 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
121                 return;
122         }
123
124         /*
125          * First empty receive FIFO
126          */
127         Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
128
129         /*
130          * Enable the DMA, receiver and receive interrupt.
131          */
132         sa1100_clear_dma(si->dma_rx.regs);
133         sa1100_start_dma(si->dma_rx.regs, si->dma_rx.dma, HPSIR_MAX_RXLEN);
134         Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
135 }
136
137 static void sa1100_irda_check_speed(struct sa1100_irda *si)
138 {
139         if (si->newspeed) {
140                 sa1100_irda_set_speed(si, si->newspeed);
141                 si->newspeed = 0;
142         }
143 }
144
145 /*
146  * HP-SIR format support.
147  */
148 static int sa1100_irda_sir_tx_start(struct sk_buff *skb, struct net_device *dev,
149         struct sa1100_irda *si)
150 {
151         si->tx_buff.data = si->tx_buff.head;
152         si->tx_buff.len  = async_wrap_skb(skb, si->tx_buff.data,
153                                           si->tx_buff.truesize);
154
155         /*
156          * Set the transmit interrupt enable.  This will fire off an
157          * interrupt immediately.  Note that we disable the receiver
158          * so we won't get spurious characters received.
159          */
160         Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
161
162         dev_kfree_skb(skb);
163
164         return NETDEV_TX_OK;
165 }
166
167 static irqreturn_t sa1100_irda_sir_irq(struct net_device *dev, struct sa1100_irda *si)
168 {
169         int status;
170
171         status = Ser2UTSR0;
172
173         /*
174          * Deal with any receive errors first.  The bytes in error may be
175          * the only bytes in the receive FIFO, so we do this first.
176          */
177         while (status & UTSR0_EIF) {
178                 int stat, data;
179
180                 stat = Ser2UTSR1;
181                 data = Ser2UTDR;
182
183                 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
184                         dev->stats.rx_errors++;
185                         if (stat & UTSR1_FRE)
186                                 dev->stats.rx_frame_errors++;
187                         if (stat & UTSR1_ROR)
188                                 dev->stats.rx_fifo_errors++;
189                 } else
190                         async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
191
192                 status = Ser2UTSR0;
193         }
194
195         /*
196          * We must clear certain bits.
197          */
198         Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
199
200         if (status & UTSR0_RFS) {
201                 /*
202                  * There are at least 4 bytes in the FIFO.  Read 3 bytes
203                  * and leave the rest to the block below.
204                  */
205                 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
206                 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
207                 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
208         }
209
210         if (status & (UTSR0_RFS | UTSR0_RID)) {
211                 /*
212                  * Fifo contains more than 1 character.
213                  */
214                 do {
215                         async_unwrap_char(dev, &dev->stats, &si->rx_buff,
216                                           Ser2UTDR);
217                 } while (Ser2UTSR1 & UTSR1_RNE);
218
219         }
220
221         if (status & UTSR0_TFS && si->tx_buff.len) {
222                 /*
223                  * Transmitter FIFO is not full
224                  */
225                 do {
226                         Ser2UTDR = *si->tx_buff.data++;
227                         si->tx_buff.len -= 1;
228                 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
229
230                 if (si->tx_buff.len == 0) {
231                         dev->stats.tx_packets++;
232                         dev->stats.tx_bytes += si->tx_buff.data -
233                                               si->tx_buff.head;
234
235                         /*
236                          * We need to ensure that the transmitter has
237                          * finished.
238                          */
239                         do
240                                 rmb();
241                         while (Ser2UTSR1 & UTSR1_TBY);
242
243                         /*
244                          * Ok, we've finished transmitting.  Now enable
245                          * the receiver.  Sometimes we get a receive IRQ
246                          * immediately after a transmit...
247                          */
248                         Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
249                         Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
250
251                         sa1100_irda_check_speed(si);
252
253                         /* I'm hungry! */
254                         netif_wake_queue(dev);
255                 }
256         }
257
258         return IRQ_HANDLED;
259 }
260
261 /*
262  * FIR format support.
263  */
264 static void sa1100_irda_firtxdma_irq(void *id)
265 {
266         struct net_device *dev = id;
267         struct sa1100_irda *si = netdev_priv(dev);
268         struct sk_buff *skb;
269
270         /*
271          * Wait for the transmission to complete.  Unfortunately,
272          * the hardware doesn't give us an interrupt to indicate
273          * "end of frame".
274          */
275         do
276                 rmb();
277         while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
278
279         /*
280          * Clear the transmit underrun bit.
281          */
282         Ser2HSSR0 = HSSR0_TUR;
283
284         /*
285          * Do we need to change speed?  Note that we're lazy
286          * here - we don't free the old dma_rx.skb.  We don't need
287          * to allocate a buffer either.
288          */
289         sa1100_irda_check_speed(si);
290
291         /*
292          * Start reception.  This disables the transmitter for
293          * us.  This will be using the existing RX buffer.
294          */
295         sa1100_irda_rx_dma_start(si);
296
297         /* Account and free the packet. */
298         skb = si->dma_tx.skb;
299         if (skb) {
300                 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
301                                  DMA_TO_DEVICE);
302                 dev->stats.tx_packets ++;
303                 dev->stats.tx_bytes += skb->len;
304                 dev_kfree_skb_irq(skb);
305                 si->dma_tx.skb = NULL;
306         }
307
308         /*
309          * Make sure that the TX queue is available for sending
310          * (for retries).  TX has priority over RX at all times.
311          */
312         netif_wake_queue(dev);
313 }
314
315 static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
316         struct sa1100_irda *si)
317 {
318         int mtt = irda_get_mtt(skb);
319
320         si->dma_tx.skb = skb;
321         si->dma_tx.dma = dma_map_single(si->dev, skb->data, skb->len,
322                                         DMA_TO_DEVICE);
323         if (dma_mapping_error(si->dev, si->dma_tx.dma)) {
324                 si->dma_tx.skb = NULL;
325                 netif_wake_queue(dev);
326                 dev->stats.tx_dropped++;
327                 dev_kfree_skb(skb);
328                 return NETDEV_TX_OK;
329         }
330
331         sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len);
332
333         /*
334          * If we have a mean turn-around time, impose the specified
335          * specified delay.  We could shorten this by timing from
336          * the point we received the packet.
337          */
338         if (mtt)
339                 udelay(mtt);
340
341         Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
342
343         return NETDEV_TX_OK;
344 }
345
346 static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
347 {
348         struct sk_buff *skb = si->dma_rx.skb;
349         dma_addr_t dma_addr;
350         unsigned int len, stat, data;
351
352         if (!skb) {
353                 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
354                 return;
355         }
356
357         /*
358          * Get the current data position.
359          */
360         dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
361         len = dma_addr - si->dma_rx.dma;
362         if (len > HPSIR_MAX_RXLEN)
363                 len = HPSIR_MAX_RXLEN;
364         dma_unmap_single(si->dev, si->dma_rx.dma, len, DMA_FROM_DEVICE);
365
366         do {
367                 /*
368                  * Read Status, and then Data.
369                  */
370                 stat = Ser2HSSR1;
371                 rmb();
372                 data = Ser2HSDR;
373
374                 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
375                         dev->stats.rx_errors++;
376                         if (stat & HSSR1_CRE)
377                                 dev->stats.rx_crc_errors++;
378                         if (stat & HSSR1_ROR)
379                                 dev->stats.rx_frame_errors++;
380                 } else
381                         skb->data[len++] = data;
382
383                 /*
384                  * If we hit the end of frame, there's
385                  * no point in continuing.
386                  */
387                 if (stat & HSSR1_EOF)
388                         break;
389         } while (Ser2HSSR0 & HSSR0_EIF);
390
391         if (stat & HSSR1_EOF) {
392                 si->dma_rx.skb = NULL;
393
394                 skb_put(skb, len);
395                 skb->dev = dev;
396                 skb_reset_mac_header(skb);
397                 skb->protocol = htons(ETH_P_IRDA);
398                 dev->stats.rx_packets++;
399                 dev->stats.rx_bytes += len;
400
401                 /*
402                  * Before we pass the buffer up, allocate a new one.
403                  */
404                 sa1100_irda_rx_alloc(si);
405
406                 netif_rx(skb);
407         } else {
408                 /*
409                  * Remap the buffer - it was previously mapped, and we
410                  * hope that this succeeds.
411                  */
412                 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
413                                                 HPSIR_MAX_RXLEN,
414                                                 DMA_FROM_DEVICE);
415         }
416 }
417
418 /*
419  * We only have to handle RX events here; transmit events go via the TX
420  * DMA handler. We disable RX, process, and the restart RX.
421  */
422 static irqreturn_t sa1100_irda_fir_irq(struct net_device *dev, struct sa1100_irda *si)
423 {
424         /*
425          * Stop RX DMA
426          */
427         sa1100_stop_dma(si->dma_rx.regs);
428
429         /*
430          * Framing error - we throw away the packet completely.
431          * Clearing RXE flushes the error conditions and data
432          * from the fifo.
433          */
434         if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
435                 dev->stats.rx_errors++;
436
437                 if (Ser2HSSR0 & HSSR0_FRE)
438                         dev->stats.rx_frame_errors++;
439
440                 /*
441                  * Clear out the DMA...
442                  */
443                 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
444
445                 /*
446                  * Clear selected status bits now, so we
447                  * don't miss them next time around.
448                  */
449                 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
450         }
451
452         /*
453          * Deal with any receive errors.  The any of the lowest
454          * 8 bytes in the FIFO may contain an error.  We must read
455          * them one by one.  The "error" could even be the end of
456          * packet!
457          */
458         if (Ser2HSSR0 & HSSR0_EIF)
459                 sa1100_irda_fir_error(si, dev);
460
461         /*
462          * No matter what happens, we must restart reception.
463          */
464         sa1100_irda_rx_dma_start(si);
465
466         return IRQ_HANDLED;
467 }
468
469 /*
470  * Set the IrDA communications speed.
471  */
472 static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
473 {
474         unsigned long flags;
475         int brd, ret = -EINVAL;
476
477         switch (speed) {
478         case 9600:      case 19200:     case 38400:
479         case 57600:     case 115200:
480                 brd = 3686400 / (16 * speed) - 1;
481
482                 /*
483                  * Stop the receive DMA.
484                  */
485                 if (IS_FIR(si))
486                         sa1100_stop_dma(si->dma_rx.regs);
487
488                 local_irq_save(flags);
489
490                 Ser2UTCR3 = 0;
491                 Ser2HSCR0 = HSCR0_UART;
492
493                 Ser2UTCR1 = brd >> 8;
494                 Ser2UTCR2 = brd;
495
496                 /*
497                  * Clear status register
498                  */
499                 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
500                 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
501
502                 if (si->pdata->set_speed)
503                         si->pdata->set_speed(si->dev, speed);
504
505                 si->speed = speed;
506                 si->tx_start = sa1100_irda_sir_tx_start;
507                 si->irq = sa1100_irda_sir_irq;
508
509                 local_irq_restore(flags);
510                 ret = 0;
511                 break;
512
513         case 4000000:
514                 local_irq_save(flags);
515
516                 si->hscr0 = 0;
517
518                 Ser2HSSR0 = 0xff;
519                 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
520                 Ser2UTCR3 = 0;
521
522                 si->speed = speed;
523                 si->tx_start = sa1100_irda_fir_tx_start;
524                 si->irq = sa1100_irda_fir_irq;
525
526                 if (si->pdata->set_speed)
527                         si->pdata->set_speed(si->dev, speed);
528
529                 sa1100_irda_rx_alloc(si);
530                 sa1100_irda_rx_dma_start(si);
531
532                 local_irq_restore(flags);
533
534                 break;
535
536         default:
537                 break;
538         }
539
540         return ret;
541 }
542
543 /*
544  * Control the power state of the IrDA transmitter.
545  * State:
546  *  0 - off
547  *  1 - short range, lowest power
548  *  2 - medium range, medium power
549  *  3 - maximum range, high power
550  *
551  * Currently, only assabet is known to support this.
552  */
553 static int
554 __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
555 {
556         int ret = 0;
557         if (si->pdata->set_power)
558                 ret = si->pdata->set_power(si->dev, state);
559         return ret;
560 }
561
562 static inline int
563 sa1100_set_power(struct sa1100_irda *si, unsigned int state)
564 {
565         int ret;
566
567         ret = __sa1100_irda_set_power(si, state);
568         if (ret == 0)
569                 si->power = state;
570
571         return ret;
572 }
573
574 static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
575 {
576         struct net_device *dev = dev_id;
577         struct sa1100_irda *si = netdev_priv(dev);
578
579         return si->irq(dev, si);
580 }
581
582 static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
583 {
584         struct sa1100_irda *si = netdev_priv(dev);
585         int speed = irda_get_next_speed(skb);
586
587         /*
588          * Does this packet contain a request to change the interface
589          * speed?  If so, remember it until we complete the transmission
590          * of this frame.
591          */
592         if (speed != si->speed && speed != -1)
593                 si->newspeed = speed;
594
595         /* If this is an empty frame, we can bypass a lot. */
596         if (skb->len == 0) {
597                 sa1100_irda_check_speed(si);
598                 dev_kfree_skb(skb);
599                 return NETDEV_TX_OK;
600         }
601
602         netif_stop_queue(dev);
603
604         /* We must not already have a skb to transmit... */
605         BUG_ON(si->dma_tx.skb);
606
607         return si->tx_start(skb, dev, si);
608 }
609
610 static int
611 sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
612 {
613         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
614         struct sa1100_irda *si = netdev_priv(dev);
615         int ret = -EOPNOTSUPP;
616
617         switch (cmd) {
618         case SIOCSBANDWIDTH:
619                 if (capable(CAP_NET_ADMIN)) {
620                         /*
621                          * We are unable to set the speed if the
622                          * device is not running.
623                          */
624                         if (si->open) {
625                                 ret = sa1100_irda_set_speed(si,
626                                                 rq->ifr_baudrate);
627                         } else {
628                                 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
629                                 ret = 0;
630                         }
631                 }
632                 break;
633
634         case SIOCSMEDIABUSY:
635                 ret = -EPERM;
636                 if (capable(CAP_NET_ADMIN)) {
637                         irda_device_set_media_busy(dev, TRUE);
638                         ret = 0;
639                 }
640                 break;
641
642         case SIOCGRECEIVING:
643                 rq->ifr_receiving = IS_FIR(si) ? 0
644                                         : si->rx_buff.state != OUTSIDE_FRAME;
645                 break;
646
647         default:
648                 break;
649         }
650                 
651         return ret;
652 }
653
654 static int sa1100_irda_startup(struct sa1100_irda *si)
655 {
656         int ret;
657
658         /*
659          * Ensure that the ports for this device are setup correctly.
660          */
661         if (si->pdata->startup) {
662                 ret = si->pdata->startup(si->dev);
663                 if (ret)
664                         return ret;
665         }
666
667         /*
668          * Configure PPC for IRDA - we want to drive TXD2 low.
669          * We also want to drive this pin low during sleep.
670          */
671         PPSR &= ~PPC_TXD2;
672         PSDR &= ~PPC_TXD2;
673         PPDR |= PPC_TXD2;
674
675         /*
676          * Enable HP-SIR modulation, and ensure that the port is disabled.
677          */
678         Ser2UTCR3 = 0;
679         Ser2HSCR0 = HSCR0_UART;
680         Ser2UTCR4 = si->utcr4;
681         Ser2UTCR0 = UTCR0_8BitData;
682         Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
683
684         /*
685          * Clear status register
686          */
687         Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
688
689         ret = sa1100_irda_set_speed(si, si->speed = 9600);
690         if (ret) {
691                 Ser2UTCR3 = 0;
692                 Ser2HSCR0 = 0;
693
694                 if (si->pdata->shutdown)
695                         si->pdata->shutdown(si->dev);
696         }
697
698         return ret;
699 }
700
701 static void sa1100_irda_shutdown(struct sa1100_irda *si)
702 {
703         /*
704          * Stop all DMA activity.
705          */
706         sa1100_stop_dma(si->dma_rx.regs);
707         sa1100_stop_dma(si->dma_tx.regs);
708
709         /* Disable the port. */
710         Ser2UTCR3 = 0;
711         Ser2HSCR0 = 0;
712
713         if (si->pdata->shutdown)
714                 si->pdata->shutdown(si->dev);
715 }
716
717 static int sa1100_irda_start(struct net_device *dev)
718 {
719         struct sa1100_irda *si = netdev_priv(dev);
720         int err;
721
722         si->speed = 9600;
723
724         err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
725                                  NULL, NULL, &si->dma_rx.regs);
726         if (err)
727                 goto err_rx_dma;
728
729         err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
730                                  sa1100_irda_firtxdma_irq, dev,
731                                  &si->dma_tx.regs);
732         if (err)
733                 goto err_tx_dma;
734
735         /*
736          * Setup the serial port for the specified speed.
737          */
738         err = sa1100_irda_startup(si);
739         if (err)
740                 goto err_startup;
741
742         /*
743          * Open a new IrLAP layer instance.
744          */
745         si->irlap = irlap_open(dev, &si->qos, "sa1100");
746         err = -ENOMEM;
747         if (!si->irlap)
748                 goto err_irlap;
749
750         err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
751         if (err)
752                 goto err_irq;
753
754         /*
755          * Now enable the interrupt and start the queue
756          */
757         si->open = 1;
758         sa1100_set_power(si, power_level); /* low power mode */
759
760         netif_start_queue(dev);
761         return 0;
762
763 err_irq:
764         irlap_close(si->irlap);
765 err_irlap:
766         si->open = 0;
767         sa1100_irda_shutdown(si);
768 err_startup:
769         sa1100_free_dma(si->dma_tx.regs);
770 err_tx_dma:
771         sa1100_free_dma(si->dma_rx.regs);
772 err_rx_dma:
773         return err;
774 }
775
776 static int sa1100_irda_stop(struct net_device *dev)
777 {
778         struct sa1100_irda *si = netdev_priv(dev);
779         struct sk_buff *skb;
780
781         netif_stop_queue(dev);
782
783         si->open = 0;
784         sa1100_irda_shutdown(si);
785
786         /*
787          * If we have been doing any DMA activity, make sure we
788          * tidy that up cleanly.
789          */
790         skb = si->dma_rx.skb;
791         if (skb) {
792                 dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN,
793                                  DMA_FROM_DEVICE);
794                 dev_kfree_skb(skb);
795                 si->dma_rx.skb = NULL;
796         }
797
798         skb = si->dma_tx.skb;
799         if (skb) {
800                 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
801                                  DMA_TO_DEVICE);
802                 dev_kfree_skb(skb);
803                 si->dma_tx.skb = NULL;
804         }
805
806         /* Stop IrLAP */
807         if (si->irlap) {
808                 irlap_close(si->irlap);
809                 si->irlap = NULL;
810         }
811
812         /*
813          * Free resources
814          */
815         sa1100_free_dma(si->dma_tx.regs);
816         sa1100_free_dma(si->dma_rx.regs);
817         free_irq(dev->irq, dev);
818
819         sa1100_set_power(si, 0);
820
821         return 0;
822 }
823
824 static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
825 {
826         io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
827         if (io->head != NULL) {
828                 io->truesize = size;
829                 io->in_frame = FALSE;
830                 io->state    = OUTSIDE_FRAME;
831                 io->data     = io->head;
832         }
833         return io->head ? 0 : -ENOMEM;
834 }
835
836 static const struct net_device_ops sa1100_irda_netdev_ops = {
837         .ndo_open               = sa1100_irda_start,
838         .ndo_stop               = sa1100_irda_stop,
839         .ndo_start_xmit         = sa1100_irda_hard_xmit,
840         .ndo_do_ioctl           = sa1100_irda_ioctl,
841 };
842
843 static int sa1100_irda_probe(struct platform_device *pdev)
844 {
845         struct net_device *dev;
846         struct sa1100_irda *si;
847         unsigned int baudrate_mask;
848         int err, irq;
849
850         if (!pdev->dev.platform_data)
851                 return -EINVAL;
852
853         irq = platform_get_irq(pdev, 0);
854         if (irq <= 0)
855                 return irq < 0 ? irq : -ENXIO;
856
857         err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
858         if (err)
859                 goto err_mem_1;
860         err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
861         if (err)
862                 goto err_mem_2;
863         err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
864         if (err)
865                 goto err_mem_3;
866
867         dev = alloc_irdadev(sizeof(struct sa1100_irda));
868         if (!dev)
869                 goto err_mem_4;
870
871         SET_NETDEV_DEV(dev, &pdev->dev);
872
873         si = netdev_priv(dev);
874         si->dev = &pdev->dev;
875         si->pdata = pdev->dev.platform_data;
876
877         /*
878          * Initialise the HP-SIR buffers
879          */
880         err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
881         if (err)
882                 goto err_mem_5;
883         err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
884         if (err)
885                 goto err_mem_5;
886
887         dev->netdev_ops = &sa1100_irda_netdev_ops;
888         dev->irq        = irq;
889
890         irda_init_max_qos_capabilies(&si->qos);
891
892         /*
893          * We support original IRDA up to 115k2. (we don't currently
894          * support 4Mbps).  Min Turn Time set to 1ms or greater.
895          */
896         baudrate_mask = IR_9600;
897
898         switch (max_rate) {
899         case 4000000:           baudrate_mask |= IR_4000000 << 8;
900         case 115200:            baudrate_mask |= IR_115200;
901         case 57600:             baudrate_mask |= IR_57600;
902         case 38400:             baudrate_mask |= IR_38400;
903         case 19200:             baudrate_mask |= IR_19200;
904         }
905                 
906         si->qos.baud_rate.bits &= baudrate_mask;
907         si->qos.min_turn_time.bits = 7;
908
909         irda_qos_bits_to_value(&si->qos);
910
911         si->utcr4 = UTCR4_HPSIR;
912         if (tx_lpm)
913                 si->utcr4 |= UTCR4_Z1_6us;
914
915         /*
916          * Initially enable HP-SIR modulation, and ensure that the port
917          * is disabled.
918          */
919         Ser2UTCR3 = 0;
920         Ser2UTCR4 = si->utcr4;
921         Ser2HSCR0 = HSCR0_UART;
922
923         err = register_netdev(dev);
924         if (err == 0)
925                 platform_set_drvdata(pdev, dev);
926
927         if (err) {
928  err_mem_5:
929                 kfree(si->tx_buff.head);
930                 kfree(si->rx_buff.head);
931                 free_netdev(dev);
932  err_mem_4:
933                 release_mem_region(__PREG(Ser2HSCR2), 0x04);
934  err_mem_3:
935                 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
936  err_mem_2:
937                 release_mem_region(__PREG(Ser2UTCR0), 0x24);
938         }
939  err_mem_1:
940         return err;
941 }
942
943 static int sa1100_irda_remove(struct platform_device *pdev)
944 {
945         struct net_device *dev = platform_get_drvdata(pdev);
946
947         if (dev) {
948                 struct sa1100_irda *si = netdev_priv(dev);
949                 unregister_netdev(dev);
950                 kfree(si->tx_buff.head);
951                 kfree(si->rx_buff.head);
952                 free_netdev(dev);
953         }
954
955         release_mem_region(__PREG(Ser2HSCR2), 0x04);
956         release_mem_region(__PREG(Ser2HSCR0), 0x1c);
957         release_mem_region(__PREG(Ser2UTCR0), 0x24);
958
959         return 0;
960 }
961
962 #ifdef CONFIG_PM
963 /*
964  * Suspend the IrDA interface.
965  */
966 static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
967 {
968         struct net_device *dev = platform_get_drvdata(pdev);
969         struct sa1100_irda *si;
970
971         if (!dev)
972                 return 0;
973
974         si = netdev_priv(dev);
975         if (si->open) {
976                 /*
977                  * Stop the transmit queue
978                  */
979                 netif_device_detach(dev);
980                 disable_irq(dev->irq);
981                 sa1100_irda_shutdown(si);
982                 __sa1100_irda_set_power(si, 0);
983         }
984
985         return 0;
986 }
987
988 /*
989  * Resume the IrDA interface.
990  */
991 static int sa1100_irda_resume(struct platform_device *pdev)
992 {
993         struct net_device *dev = platform_get_drvdata(pdev);
994         struct sa1100_irda *si;
995
996         if (!dev)
997                 return 0;
998
999         si = netdev_priv(dev);
1000         if (si->open) {
1001                 /*
1002                  * If we missed a speed change, initialise at the new speed
1003                  * directly.  It is debatable whether this is actually
1004                  * required, but in the interests of continuing from where
1005                  * we left off it is desirable.  The converse argument is
1006                  * that we should re-negotiate at 9600 baud again.
1007                  */
1008                 if (si->newspeed) {
1009                         si->speed = si->newspeed;
1010                         si->newspeed = 0;
1011                 }
1012
1013                 sa1100_irda_startup(si);
1014                 __sa1100_irda_set_power(si, si->power);
1015                 enable_irq(dev->irq);
1016
1017                 /*
1018                  * This automatically wakes up the queue
1019                  */
1020                 netif_device_attach(dev);
1021         }
1022
1023         return 0;
1024 }
1025 #else
1026 #define sa1100_irda_suspend     NULL
1027 #define sa1100_irda_resume      NULL
1028 #endif
1029
1030 static struct platform_driver sa1100ir_driver = {
1031         .probe          = sa1100_irda_probe,
1032         .remove         = sa1100_irda_remove,
1033         .suspend        = sa1100_irda_suspend,
1034         .resume         = sa1100_irda_resume,
1035         .driver         = {
1036                 .name   = "sa11x0-ir",
1037                 .owner  = THIS_MODULE,
1038         },
1039 };
1040
1041 static int __init sa1100_irda_init(void)
1042 {
1043         /*
1044          * Limit power level a sensible range.
1045          */
1046         if (power_level < 1)
1047                 power_level = 1;
1048         if (power_level > 3)
1049                 power_level = 3;
1050
1051         return platform_driver_register(&sa1100ir_driver);
1052 }
1053
1054 static void __exit sa1100_irda_exit(void)
1055 {
1056         platform_driver_unregister(&sa1100ir_driver);
1057 }
1058
1059 module_init(sa1100_irda_init);
1060 module_exit(sa1100_irda_exit);
1061 module_param(power_level, int, 0);
1062 module_param(tx_lpm, int, 0);
1063 module_param(max_rate, int, 0);
1064
1065 MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1066 MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1067 MODULE_LICENSE("GPL");
1068 MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1069 MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1070 MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
1071 MODULE_ALIAS("platform:sa11x0-ir");