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