[ARM] 5144/1: pxaficp_ir: cleanup includes
[linux-2.6] / drivers / net / irda / pxaficp_ir.c
1 /*
2  * linux/drivers/net/irda/pxaficp_ir.c
3  *
4  * Based on sa1100_ir.c by Russell King
5  *
6  * Changes copyright (C) 2003-2005 MontaVista Software, Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * Infra-red driver (SIR/FIR) for the PXA2xx embedded microprocessor
13  *
14  */
15 #include <linux/module.h>
16 #include <linux/netdevice.h>
17 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19
20 #include <net/irda/irda.h>
21 #include <net/irda/irmod.h>
22 #include <net/irda/wrapper.h>
23 #include <net/irda/irda_device.h>
24
25 #include <asm/dma.h>
26 #include <asm/arch/irda.h>
27 #include <asm/arch/pxa-regs.h>
28 #include <asm/arch/pxa2xx-gpio.h>
29
30 #define IrSR_RXPL_NEG_IS_ZERO (1<<4)
31 #define IrSR_RXPL_POS_IS_ZERO 0x0
32 #define IrSR_TXPL_NEG_IS_ZERO (1<<3)
33 #define IrSR_TXPL_POS_IS_ZERO 0x0
34 #define IrSR_XMODE_PULSE_1_6  (1<<2)
35 #define IrSR_XMODE_PULSE_3_16 0x0
36 #define IrSR_RCVEIR_IR_MODE   (1<<1)
37 #define IrSR_RCVEIR_UART_MODE 0x0
38 #define IrSR_XMITIR_IR_MODE   (1<<0)
39 #define IrSR_XMITIR_UART_MODE 0x0
40
41 #define IrSR_IR_RECEIVE_ON (\
42                 IrSR_RXPL_NEG_IS_ZERO | \
43                 IrSR_TXPL_POS_IS_ZERO | \
44                 IrSR_XMODE_PULSE_3_16 | \
45                 IrSR_RCVEIR_IR_MODE   | \
46                 IrSR_XMITIR_UART_MODE)
47
48 #define IrSR_IR_TRANSMIT_ON (\
49                 IrSR_RXPL_NEG_IS_ZERO | \
50                 IrSR_TXPL_POS_IS_ZERO | \
51                 IrSR_XMODE_PULSE_3_16 | \
52                 IrSR_RCVEIR_UART_MODE | \
53                 IrSR_XMITIR_IR_MODE)
54
55 struct pxa_irda {
56         int                     speed;
57         int                     newspeed;
58         unsigned long           last_oscr;
59
60         unsigned char           *dma_rx_buff;
61         unsigned char           *dma_tx_buff;
62         dma_addr_t              dma_rx_buff_phy;
63         dma_addr_t              dma_tx_buff_phy;
64         unsigned int            dma_tx_buff_len;
65         int                     txdma;
66         int                     rxdma;
67
68         struct net_device_stats stats;
69         struct irlap_cb         *irlap;
70         struct qos_info         qos;
71
72         iobuff_t                tx_buff;
73         iobuff_t                rx_buff;
74
75         struct device           *dev;
76         struct pxaficp_platform_data *pdata;
77         struct clk              *fir_clk;
78         struct clk              *sir_clk;
79         struct clk              *cur_clk;
80 };
81
82 static inline void pxa_irda_disable_clk(struct pxa_irda *si)
83 {
84         if (si->cur_clk)
85                 clk_disable(si->cur_clk);
86         si->cur_clk = NULL;
87 }
88
89 static inline void pxa_irda_enable_firclk(struct pxa_irda *si)
90 {
91         si->cur_clk = si->fir_clk;
92         clk_enable(si->fir_clk);
93 }
94
95 static inline void pxa_irda_enable_sirclk(struct pxa_irda *si)
96 {
97         si->cur_clk = si->sir_clk;
98         clk_enable(si->sir_clk);
99 }
100
101
102 #define IS_FIR(si)              ((si)->speed >= 4000000)
103 #define IRDA_FRAME_SIZE_LIMIT   2047
104
105 inline static void pxa_irda_fir_dma_rx_start(struct pxa_irda *si)
106 {
107         DCSR(si->rxdma)  = DCSR_NODESC;
108         DSADR(si->rxdma) = __PREG(ICDR);
109         DTADR(si->rxdma) = si->dma_rx_buff_phy;
110         DCMD(si->rxdma) = DCMD_INCTRGADDR | DCMD_FLOWSRC |  DCMD_WIDTH1 | DCMD_BURST32 | IRDA_FRAME_SIZE_LIMIT;
111         DCSR(si->rxdma) |= DCSR_RUN;
112 }
113
114 inline static void pxa_irda_fir_dma_tx_start(struct pxa_irda *si)
115 {
116         DCSR(si->txdma)  = DCSR_NODESC;
117         DSADR(si->txdma) = si->dma_tx_buff_phy;
118         DTADR(si->txdma) = __PREG(ICDR);
119         DCMD(si->txdma) = DCMD_INCSRCADDR | DCMD_FLOWTRG |  DCMD_ENDIRQEN | DCMD_WIDTH1 | DCMD_BURST32 | si->dma_tx_buff_len;
120         DCSR(si->txdma) |= DCSR_RUN;
121 }
122
123 /*
124  * Set the IrDA communications speed.
125  */
126 static int pxa_irda_set_speed(struct pxa_irda *si, int speed)
127 {
128         unsigned long flags;
129         unsigned int divisor;
130
131         switch (speed) {
132         case 9600:      case 19200:     case 38400:
133         case 57600:     case 115200:
134
135                 /* refer to PXA250/210 Developer's Manual 10-7 */
136                 /*  BaudRate = 14.7456 MHz / (16*Divisor) */
137                 divisor = 14745600 / (16 * speed);
138
139                 local_irq_save(flags);
140
141                 if (IS_FIR(si)) {
142                         /* stop RX DMA */
143                         DCSR(si->rxdma) &= ~DCSR_RUN;
144                         /* disable FICP */
145                         ICCR0 = 0;
146                         pxa_irda_disable_clk(si);
147
148                         /* set board transceiver to SIR mode */
149                         si->pdata->transceiver_mode(si->dev, IR_SIRMODE);
150
151                         /* configure GPIO46/47 */
152                         pxa_gpio_mode(GPIO46_STRXD_MD);
153                         pxa_gpio_mode(GPIO47_STTXD_MD);
154
155                         /* enable the STUART clock */
156                         pxa_irda_enable_sirclk(si);
157                 }
158
159                 /* disable STUART first */
160                 STIER = 0;
161
162                 /* access DLL & DLH */
163                 STLCR |= LCR_DLAB;
164                 STDLL = divisor & 0xff;
165                 STDLH = divisor >> 8;
166                 STLCR &= ~LCR_DLAB;
167
168                 si->speed = speed;
169                 STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
170                 STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
171
172                 local_irq_restore(flags);
173                 break;
174
175         case 4000000:
176                 local_irq_save(flags);
177
178                 /* disable STUART */
179                 STIER = 0;
180                 STISR = 0;
181                 pxa_irda_disable_clk(si);
182
183                 /* disable FICP first */
184                 ICCR0 = 0;
185
186                 /* set board transceiver to FIR mode */
187                 si->pdata->transceiver_mode(si->dev, IR_FIRMODE);
188
189                 /* configure GPIO46/47 */
190                 pxa_gpio_mode(GPIO46_ICPRXD_MD);
191                 pxa_gpio_mode(GPIO47_ICPTXD_MD);
192
193                 /* enable the FICP clock */
194                 pxa_irda_enable_firclk(si);
195
196                 si->speed = speed;
197                 pxa_irda_fir_dma_rx_start(si);
198                 ICCR0 = ICCR0_ITR | ICCR0_RXE;
199
200                 local_irq_restore(flags);
201                 break;
202
203         default:
204                 return -EINVAL;
205         }
206
207         return 0;
208 }
209
210 /* SIR interrupt service routine. */
211 static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id)
212 {
213         struct net_device *dev = dev_id;
214         struct pxa_irda *si = netdev_priv(dev);
215         int iir, lsr, data;
216
217         iir = STIIR;
218
219         switch  (iir & 0x0F) {
220         case 0x06: /* Receiver Line Status */
221                 lsr = STLSR;
222                 while (lsr & LSR_FIFOE) {
223                         data = STRBR;
224                         if (lsr & (LSR_OE | LSR_PE | LSR_FE | LSR_BI)) {
225                                 printk(KERN_DEBUG "pxa_ir: sir receiving error\n");
226                                 si->stats.rx_errors++;
227                                 if (lsr & LSR_FE)
228                                         si->stats.rx_frame_errors++;
229                                 if (lsr & LSR_OE)
230                                         si->stats.rx_fifo_errors++;
231                         } else {
232                                 si->stats.rx_bytes++;
233                                 async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
234                         }
235                         lsr = STLSR;
236                 }
237                 dev->last_rx = jiffies;
238                 si->last_oscr = OSCR;
239                 break;
240
241         case 0x04: /* Received Data Available */
242                    /* forth through */
243
244         case 0x0C: /* Character Timeout Indication */
245                 do  {
246                     si->stats.rx_bytes++;
247                     async_unwrap_char(dev, &si->stats, &si->rx_buff, STRBR);
248                 } while (STLSR & LSR_DR);
249                 dev->last_rx = jiffies;
250                 si->last_oscr = OSCR;
251                 break;
252
253         case 0x02: /* Transmit FIFO Data Request */
254                 while ((si->tx_buff.len) && (STLSR & LSR_TDRQ)) {
255                         STTHR = *si->tx_buff.data++;
256                         si->tx_buff.len -= 1;
257                 }
258
259                 if (si->tx_buff.len == 0) {
260                         si->stats.tx_packets++;
261                         si->stats.tx_bytes += si->tx_buff.data -
262                                               si->tx_buff.head;
263
264                         /* We need to ensure that the transmitter has finished. */
265                         while ((STLSR & LSR_TEMT) == 0)
266                                 cpu_relax();
267                         si->last_oscr = OSCR;
268
269                         /*
270                         * Ok, we've finished transmitting.  Now enable
271                         * the receiver.  Sometimes we get a receive IRQ
272                         * immediately after a transmit...
273                         */
274                         if (si->newspeed) {
275                                 pxa_irda_set_speed(si, si->newspeed);
276                                 si->newspeed = 0;
277                         } else {
278                                 /* enable IR Receiver, disable IR Transmitter */
279                                 STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6;
280                                 /* enable STUART and receive interrupts */
281                                 STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE;
282                         }
283                         /* I'm hungry! */
284                         netif_wake_queue(dev);
285                 }
286                 break;
287         }
288
289         return IRQ_HANDLED;
290 }
291
292 /* FIR Receive DMA interrupt handler */
293 static void pxa_irda_fir_dma_rx_irq(int channel, void *data)
294 {
295         int dcsr = DCSR(channel);
296
297         DCSR(channel) = dcsr & ~DCSR_RUN;
298
299         printk(KERN_DEBUG "pxa_ir: fir rx dma bus error %#x\n", dcsr);
300 }
301
302 /* FIR Transmit DMA interrupt handler */
303 static void pxa_irda_fir_dma_tx_irq(int channel, void *data)
304 {
305         struct net_device *dev = data;
306         struct pxa_irda *si = netdev_priv(dev);
307         int dcsr;
308
309         dcsr = DCSR(channel);
310         DCSR(channel) = dcsr & ~DCSR_RUN;
311
312         if (dcsr & DCSR_ENDINTR)  {
313                 si->stats.tx_packets++;
314                 si->stats.tx_bytes += si->dma_tx_buff_len;
315         } else {
316                 si->stats.tx_errors++;
317         }
318
319         while (ICSR1 & ICSR1_TBY)
320                 cpu_relax();
321         si->last_oscr = OSCR;
322
323         /*
324          * HACK: It looks like the TBY bit is dropped too soon.
325          * Without this delay things break.
326          */
327         udelay(120);
328
329         if (si->newspeed) {
330                 pxa_irda_set_speed(si, si->newspeed);
331                 si->newspeed = 0;
332         } else {
333                 int i = 64;
334
335                 ICCR0 = 0;
336                 pxa_irda_fir_dma_rx_start(si);
337                 while ((ICSR1 & ICSR1_RNE) && i--)
338                         (void)ICDR;
339                 ICCR0 = ICCR0_ITR | ICCR0_RXE;
340
341                 if (i < 0)
342                         printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
343         }
344         netif_wake_queue(dev);
345 }
346
347 /* EIF(Error in FIFO/End in Frame) handler for FIR */
348 static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev, int icsr0)
349 {
350         unsigned int len, stat, data;
351
352         /* Get the current data position. */
353         len = DTADR(si->rxdma) - si->dma_rx_buff_phy;
354
355         do {
356                 /* Read Status, and then Data.   */
357                 stat = ICSR1;
358                 rmb();
359                 data = ICDR;
360
361                 if (stat & (ICSR1_CRE | ICSR1_ROR)) {
362                         si->stats.rx_errors++;
363                         if (stat & ICSR1_CRE) {
364                                 printk(KERN_DEBUG "pxa_ir: fir receive CRC error\n");
365                                 si->stats.rx_crc_errors++;
366                         }
367                         if (stat & ICSR1_ROR) {
368                                 printk(KERN_DEBUG "pxa_ir: fir receive overrun\n");
369                                 si->stats.rx_over_errors++;
370                         }
371                 } else  {
372                         si->dma_rx_buff[len++] = data;
373                 }
374                 /* If we hit the end of frame, there's no point in continuing. */
375                 if (stat & ICSR1_EOF)
376                         break;
377         } while (ICSR0 & ICSR0_EIF);
378
379         if (stat & ICSR1_EOF) {
380                 /* end of frame. */
381                 struct sk_buff *skb;
382
383                 if (icsr0 & ICSR0_FRE) {
384                         printk(KERN_ERR "pxa_ir: dropping erroneous frame\n");
385                         si->stats.rx_dropped++;
386                         return;
387                 }
388
389                 skb = alloc_skb(len+1,GFP_ATOMIC);
390                 if (!skb)  {
391                         printk(KERN_ERR "pxa_ir: fir out of memory for receive skb\n");
392                         si->stats.rx_dropped++;
393                         return;
394                 }
395
396                 /* Align IP header to 20 bytes  */
397                 skb_reserve(skb, 1);
398                 skb_copy_to_linear_data(skb, si->dma_rx_buff, len);
399                 skb_put(skb, len);
400
401                 /* Feed it to IrLAP  */
402                 skb->dev = dev;
403                 skb_reset_mac_header(skb);
404                 skb->protocol = htons(ETH_P_IRDA);
405                 netif_rx(skb);
406
407                 si->stats.rx_packets++;
408                 si->stats.rx_bytes += len;
409
410                 dev->last_rx = jiffies;
411         }
412 }
413
414 /* FIR interrupt handler */
415 static irqreturn_t pxa_irda_fir_irq(int irq, void *dev_id)
416 {
417         struct net_device *dev = dev_id;
418         struct pxa_irda *si = netdev_priv(dev);
419         int icsr0, i = 64;
420
421         /* stop RX DMA */
422         DCSR(si->rxdma) &= ~DCSR_RUN;
423         si->last_oscr = OSCR;
424         icsr0 = ICSR0;
425
426         if (icsr0 & (ICSR0_FRE | ICSR0_RAB)) {
427                 if (icsr0 & ICSR0_FRE) {
428                         printk(KERN_DEBUG "pxa_ir: fir receive frame error\n");
429                         si->stats.rx_frame_errors++;
430                 } else {
431                         printk(KERN_DEBUG "pxa_ir: fir receive abort\n");
432                         si->stats.rx_errors++;
433                 }
434                 ICSR0 = icsr0 & (ICSR0_FRE | ICSR0_RAB);
435         }
436
437         if (icsr0 & ICSR0_EIF) {
438                 /* An error in FIFO occured, or there is a end of frame */
439                 pxa_irda_fir_irq_eif(si, dev, icsr0);
440         }
441
442         ICCR0 = 0;
443         pxa_irda_fir_dma_rx_start(si);
444         while ((ICSR1 & ICSR1_RNE) && i--)
445                 (void)ICDR;
446         ICCR0 = ICCR0_ITR | ICCR0_RXE;
447
448         if (i < 0)
449                 printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
450
451         return IRQ_HANDLED;
452 }
453
454 /* hard_xmit interface of irda device */
455 static int pxa_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
456 {
457         struct pxa_irda *si = netdev_priv(dev);
458         int speed = irda_get_next_speed(skb);
459
460         /*
461          * Does this packet contain a request to change the interface
462          * speed?  If so, remember it until we complete the transmission
463          * of this frame.
464          */
465         if (speed != si->speed && speed != -1)
466                 si->newspeed = speed;
467
468         /*
469          * If this is an empty frame, we can bypass a lot.
470          */
471         if (skb->len == 0) {
472                 if (si->newspeed) {
473                         si->newspeed = 0;
474                         pxa_irda_set_speed(si, speed);
475                 }
476                 dev_kfree_skb(skb);
477                 return 0;
478         }
479
480         netif_stop_queue(dev);
481
482         if (!IS_FIR(si)) {
483                 si->tx_buff.data = si->tx_buff.head;
484                 si->tx_buff.len  = async_wrap_skb(skb, si->tx_buff.data, si->tx_buff.truesize);
485
486                 /* Disable STUART interrupts and switch to transmit mode. */
487                 STIER = 0;
488                 STISR = IrSR_IR_TRANSMIT_ON | IrSR_XMODE_PULSE_1_6;
489
490                 /* enable STUART and transmit interrupts */
491                 STIER = IER_UUE | IER_TIE;
492         } else {
493                 unsigned long mtt = irda_get_mtt(skb);
494
495                 si->dma_tx_buff_len = skb->len;
496                 skb_copy_from_linear_data(skb, si->dma_tx_buff, skb->len);
497
498                 if (mtt)
499                         while ((unsigned)(OSCR - si->last_oscr)/4 < mtt)
500                                 cpu_relax();
501
502                 /* stop RX DMA,  disable FICP */
503                 DCSR(si->rxdma) &= ~DCSR_RUN;
504                 ICCR0 = 0;
505
506                 pxa_irda_fir_dma_tx_start(si);
507                 ICCR0 = ICCR0_ITR | ICCR0_TXE;
508         }
509
510         dev_kfree_skb(skb);
511         dev->trans_start = jiffies;
512         return 0;
513 }
514
515 static int pxa_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
516 {
517         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
518         struct pxa_irda *si = netdev_priv(dev);
519         int ret;
520
521         switch (cmd) {
522         case SIOCSBANDWIDTH:
523                 ret = -EPERM;
524                 if (capable(CAP_NET_ADMIN)) {
525                         /*
526                          * We are unable to set the speed if the
527                          * device is not running.
528                          */
529                         if (netif_running(dev)) {
530                                 ret = pxa_irda_set_speed(si,
531                                                 rq->ifr_baudrate);
532                         } else {
533                                 printk(KERN_INFO "pxa_ir: SIOCSBANDWIDTH: !netif_running\n");
534                                 ret = 0;
535                         }
536                 }
537                 break;
538
539         case SIOCSMEDIABUSY:
540                 ret = -EPERM;
541                 if (capable(CAP_NET_ADMIN)) {
542                         irda_device_set_media_busy(dev, TRUE);
543                         ret = 0;
544                 }
545                 break;
546
547         case SIOCGRECEIVING:
548                 ret = 0;
549                 rq->ifr_receiving = IS_FIR(si) ? 0
550                                         : si->rx_buff.state != OUTSIDE_FRAME;
551                 break;
552
553         default:
554                 ret = -EOPNOTSUPP;
555                 break;
556         }
557
558         return ret;
559 }
560
561 static struct net_device_stats *pxa_irda_stats(struct net_device *dev)
562 {
563         struct pxa_irda *si = netdev_priv(dev);
564         return &si->stats;
565 }
566
567 static void pxa_irda_startup(struct pxa_irda *si)
568 {
569         /* Disable STUART interrupts */
570         STIER = 0;
571         /* enable STUART interrupt to the processor */
572         STMCR = MCR_OUT2;
573         /* configure SIR frame format: StartBit - Data 7 ... Data 0 - Stop Bit */
574         STLCR = LCR_WLS0 | LCR_WLS1;
575         /* enable FIFO, we use FIFO to improve performance */
576         STFCR = FCR_TRFIFOE | FCR_ITL_32;
577
578         /* disable FICP */
579         ICCR0 = 0;
580         /* configure FICP ICCR2 */
581         ICCR2 = ICCR2_TXP | ICCR2_TRIG_32;
582
583         /* configure DMAC */
584         DRCMR17 = si->rxdma | DRCMR_MAPVLD;
585         DRCMR18 = si->txdma | DRCMR_MAPVLD;
586
587         /* force SIR reinitialization */
588         si->speed = 4000000;
589         pxa_irda_set_speed(si, 9600);
590
591         printk(KERN_DEBUG "pxa_ir: irda startup\n");
592 }
593
594 static void pxa_irda_shutdown(struct pxa_irda *si)
595 {
596         unsigned long flags;
597
598         local_irq_save(flags);
599
600         /* disable STUART and interrupt */
601         STIER = 0;
602         /* disable STUART SIR mode */
603         STISR = 0;
604
605         /* disable DMA */
606         DCSR(si->txdma) &= ~DCSR_RUN;
607         DCSR(si->rxdma) &= ~DCSR_RUN;
608         /* disable FICP */
609         ICCR0 = 0;
610
611         /* disable the STUART or FICP clocks */
612         pxa_irda_disable_clk(si);
613
614         DRCMR17 = 0;
615         DRCMR18 = 0;
616
617         local_irq_restore(flags);
618
619         /* power off board transceiver */
620         si->pdata->transceiver_mode(si->dev, IR_OFF);
621
622         printk(KERN_DEBUG "pxa_ir: irda shutdown\n");
623 }
624
625 static int pxa_irda_start(struct net_device *dev)
626 {
627         struct pxa_irda *si = netdev_priv(dev);
628         int err;
629
630         si->speed = 9600;
631
632         err = request_irq(IRQ_STUART, pxa_irda_sir_irq, 0, dev->name, dev);
633         if (err)
634                 goto err_irq1;
635
636         err = request_irq(IRQ_ICP, pxa_irda_fir_irq, 0, dev->name, dev);
637         if (err)
638                 goto err_irq2;
639
640         /*
641          * The interrupt must remain disabled for now.
642          */
643         disable_irq(IRQ_STUART);
644         disable_irq(IRQ_ICP);
645
646         err = -EBUSY;
647         si->rxdma = pxa_request_dma("FICP_RX",DMA_PRIO_LOW, pxa_irda_fir_dma_rx_irq, dev);
648         if (si->rxdma < 0)
649                 goto err_rx_dma;
650
651         si->txdma = pxa_request_dma("FICP_TX",DMA_PRIO_LOW, pxa_irda_fir_dma_tx_irq, dev);
652         if (si->txdma < 0)
653                 goto err_tx_dma;
654
655         err = -ENOMEM;
656         si->dma_rx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
657                                              &si->dma_rx_buff_phy, GFP_KERNEL );
658         if (!si->dma_rx_buff)
659                 goto err_dma_rx_buff;
660
661         si->dma_tx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT,
662                                              &si->dma_tx_buff_phy, GFP_KERNEL );
663         if (!si->dma_tx_buff)
664                 goto err_dma_tx_buff;
665
666         /* Setup the serial port for the initial speed. */
667         pxa_irda_startup(si);
668
669         /*
670          * Open a new IrLAP layer instance.
671          */
672         si->irlap = irlap_open(dev, &si->qos, "pxa");
673         err = -ENOMEM;
674         if (!si->irlap)
675                 goto err_irlap;
676
677         /*
678          * Now enable the interrupt and start the queue
679          */
680         enable_irq(IRQ_STUART);
681         enable_irq(IRQ_ICP);
682         netif_start_queue(dev);
683
684         printk(KERN_DEBUG "pxa_ir: irda driver opened\n");
685
686         return 0;
687
688 err_irlap:
689         pxa_irda_shutdown(si);
690         dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
691 err_dma_tx_buff:
692         dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
693 err_dma_rx_buff:
694         pxa_free_dma(si->txdma);
695 err_tx_dma:
696         pxa_free_dma(si->rxdma);
697 err_rx_dma:
698         free_irq(IRQ_ICP, dev);
699 err_irq2:
700         free_irq(IRQ_STUART, dev);
701 err_irq1:
702
703         return err;
704 }
705
706 static int pxa_irda_stop(struct net_device *dev)
707 {
708         struct pxa_irda *si = netdev_priv(dev);
709
710         netif_stop_queue(dev);
711
712         pxa_irda_shutdown(si);
713
714         /* Stop IrLAP */
715         if (si->irlap) {
716                 irlap_close(si->irlap);
717                 si->irlap = NULL;
718         }
719
720         free_irq(IRQ_STUART, dev);
721         free_irq(IRQ_ICP, dev);
722
723         pxa_free_dma(si->rxdma);
724         pxa_free_dma(si->txdma);
725
726         if (si->dma_rx_buff)
727                 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy);
728         if (si->dma_tx_buff)
729                 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
730
731         printk(KERN_DEBUG "pxa_ir: irda driver closed\n");
732         return 0;
733 }
734
735 static int pxa_irda_suspend(struct platform_device *_dev, pm_message_t state)
736 {
737         struct net_device *dev = platform_get_drvdata(_dev);
738         struct pxa_irda *si;
739
740         if (dev && netif_running(dev)) {
741                 si = netdev_priv(dev);
742                 netif_device_detach(dev);
743                 pxa_irda_shutdown(si);
744         }
745
746         return 0;
747 }
748
749 static int pxa_irda_resume(struct platform_device *_dev)
750 {
751         struct net_device *dev = platform_get_drvdata(_dev);
752         struct pxa_irda *si;
753
754         if (dev && netif_running(dev)) {
755                 si = netdev_priv(dev);
756                 pxa_irda_startup(si);
757                 netif_device_attach(dev);
758                 netif_wake_queue(dev);
759         }
760
761         return 0;
762 }
763
764
765 static int pxa_irda_init_iobuf(iobuff_t *io, int size)
766 {
767         io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
768         if (io->head != NULL) {
769                 io->truesize = size;
770                 io->in_frame = FALSE;
771                 io->state    = OUTSIDE_FRAME;
772                 io->data     = io->head;
773         }
774         return io->head ? 0 : -ENOMEM;
775 }
776
777 static int pxa_irda_probe(struct platform_device *pdev)
778 {
779         struct net_device *dev;
780         struct pxa_irda *si;
781         unsigned int baudrate_mask;
782         int err;
783
784         if (!pdev->dev.platform_data)
785                 return -ENODEV;
786
787         err = request_mem_region(__PREG(STUART), 0x24, "IrDA") ? 0 : -EBUSY;
788         if (err)
789                 goto err_mem_1;
790
791         err = request_mem_region(__PREG(FICP), 0x1c, "IrDA") ? 0 : -EBUSY;
792         if (err)
793                 goto err_mem_2;
794
795         dev = alloc_irdadev(sizeof(struct pxa_irda));
796         if (!dev)
797                 goto err_mem_3;
798
799         si = netdev_priv(dev);
800         si->dev = &pdev->dev;
801         si->pdata = pdev->dev.platform_data;
802
803         si->sir_clk = clk_get(&pdev->dev, "UARTCLK");
804         si->fir_clk = clk_get(&pdev->dev, "FICPCLK");
805         if (IS_ERR(si->sir_clk) || IS_ERR(si->fir_clk)) {
806                 err = PTR_ERR(IS_ERR(si->sir_clk) ? si->sir_clk : si->fir_clk);
807                 goto err_mem_4;
808         }
809
810         /*
811          * Initialise the SIR buffers
812          */
813         err = pxa_irda_init_iobuf(&si->rx_buff, 14384);
814         if (err)
815                 goto err_mem_4;
816         err = pxa_irda_init_iobuf(&si->tx_buff, 4000);
817         if (err)
818                 goto err_mem_5;
819
820         if (si->pdata->startup)
821                 err = si->pdata->startup(si->dev);
822         if (err)
823                 goto err_startup;
824
825         dev->hard_start_xmit    = pxa_irda_hard_xmit;
826         dev->open               = pxa_irda_start;
827         dev->stop               = pxa_irda_stop;
828         dev->do_ioctl           = pxa_irda_ioctl;
829         dev->get_stats          = pxa_irda_stats;
830
831         irda_init_max_qos_capabilies(&si->qos);
832
833         baudrate_mask = 0;
834         if (si->pdata->transceiver_cap & IR_SIRMODE)
835                 baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
836         if (si->pdata->transceiver_cap & IR_FIRMODE)
837                 baudrate_mask |= IR_4000000 << 8;
838
839         si->qos.baud_rate.bits &= baudrate_mask;
840         si->qos.min_turn_time.bits = 7;  /* 1ms or more */
841
842         irda_qos_bits_to_value(&si->qos);
843
844         err = register_netdev(dev);
845
846         if (err == 0)
847                 dev_set_drvdata(&pdev->dev, dev);
848
849         if (err) {
850                 if (si->pdata->shutdown)
851                         si->pdata->shutdown(si->dev);
852 err_startup:
853                 kfree(si->tx_buff.head);
854 err_mem_5:
855                 kfree(si->rx_buff.head);
856 err_mem_4:
857                 if (si->sir_clk && !IS_ERR(si->sir_clk))
858                         clk_put(si->sir_clk);
859                 if (si->fir_clk && !IS_ERR(si->fir_clk))
860                         clk_put(si->fir_clk);
861                 free_netdev(dev);
862 err_mem_3:
863                 release_mem_region(__PREG(FICP), 0x1c);
864 err_mem_2:
865                 release_mem_region(__PREG(STUART), 0x24);
866         }
867 err_mem_1:
868         return err;
869 }
870
871 static int pxa_irda_remove(struct platform_device *_dev)
872 {
873         struct net_device *dev = platform_get_drvdata(_dev);
874
875         if (dev) {
876                 struct pxa_irda *si = netdev_priv(dev);
877                 unregister_netdev(dev);
878                 if (si->pdata->shutdown)
879                         si->pdata->shutdown(si->dev);
880                 kfree(si->tx_buff.head);
881                 kfree(si->rx_buff.head);
882                 clk_put(si->fir_clk);
883                 clk_put(si->sir_clk);
884                 free_netdev(dev);
885         }
886
887         release_mem_region(__PREG(STUART), 0x24);
888         release_mem_region(__PREG(FICP), 0x1c);
889
890         return 0;
891 }
892
893 static struct platform_driver pxa_ir_driver = {
894         .driver         = {
895                 .name   = "pxa2xx-ir",
896                 .owner  = THIS_MODULE,
897         },
898         .probe          = pxa_irda_probe,
899         .remove         = pxa_irda_remove,
900         .suspend        = pxa_irda_suspend,
901         .resume         = pxa_irda_resume,
902 };
903
904 static int __init pxa_irda_init(void)
905 {
906         return platform_driver_register(&pxa_ir_driver);
907 }
908
909 static void __exit pxa_irda_exit(void)
910 {
911         platform_driver_unregister(&pxa_ir_driver);
912 }
913
914 module_init(pxa_irda_init);
915 module_exit(pxa_irda_exit);
916
917 MODULE_LICENSE("GPL");
918 MODULE_ALIAS("platform:pxa2xx-ir");