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