]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/irda/omap-ir.c
Merge branch 'for-tony-mailbox'
[linux-2.6-omap-h63xx.git] / drivers / net / irda / omap-ir.c
1 /*
2  * BRIEF MODULE DESCRIPTION
3  *
4  *      Infra-red driver for the OMAP1610-H2 and OMAP1710-H3 and H4 Platforms
5  *        (SIR/MIR/FIR modes)
6  *        (based on omap-sir.c)
7  *
8  * Copyright 2003 MontaVista Software Inc.
9  * Author: MontaVista Software, Inc.
10  *         source@mvista.com
11  *
12  * Copyright 2004 Texas Instruments.
13  *
14  *  This program is free software; you can redistribute  it and/or modify it
15  *  under  the terms of  the GNU General  Public License as published by the
16  *  Free Software Foundation;  either version 2 of the  License, or (at your
17  *  option) any later version.
18  *
19  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
20  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
21  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
22  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
23  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
25  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
27  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  *  You should have received a copy of the  GNU General Public License along
31  *  with this program; if not, write  to the Free Software Foundation, Inc.,
32  *  675 Mass Ave, Cambridge, MA 02139, USA.
33  *
34  Modifications:
35  Feb 2004, Texas Instruments
36  - Ported to 2.6 kernel (Feb 2004).
37  *
38  Apr 2004, Texas Instruments
39  - Added support for H3 (Apr 2004).
40  Nov 2004, Texas Instruments
41  - Added support for Power Management.
42  */
43
44 #include <linux/module.h>
45 #include <linux/types.h>
46 #include <linux/init.h>
47 #include <linux/errno.h>
48 #include <linux/netdevice.h>
49 #include <linux/slab.h>
50 #include <linux/rtnetlink.h>
51 #include <linux/interrupt.h>
52 #include <linux/delay.h>
53 #include <linux/ioport.h>
54 #include <linux/dma-mapping.h>
55 #include <linux/platform_device.h>
56 #include <linux/i2c.h>
57 #include <linux/workqueue.h>
58
59 #include <net/irda/irda.h>
60 #include <net/irda/irmod.h>
61 #include <net/irda/wrapper.h>
62 #include <net/irda/irda_device.h>
63
64 #include <asm/irq.h>
65 #include <asm/io.h>
66 #include <mach/hardware.h>
67 #include <asm/serial.h>
68 #include <asm/mach-types.h>
69 #include <asm/dma.h>
70 #include <mach/mux.h>
71 #include <mach/gpio.h>
72 #include <mach/irda.h>
73
74 #define UART3_EFR_EN                    (1 << 4)
75 #define UART3_MCR_EN_TCR_TLR            (1 << 6)
76
77 #define UART3_LCR_WL_8                  (3 << 0)
78 #define UART3_LCR_SP2                   (1 << 2)
79 #define UART3_LCR_DIVEN                 (1 << 7)
80
81 #define UART3_FCR_FIFO_EN               (1 << 0)
82 #define UART3_FCR_FIFO_RX               (1 << 1)
83 #define UART3_FCR_FIFO_TX               (1 << 2)
84 #define UART3_FCR_FIFO_DMA1             (1 << 3)
85 #define UART3_FCR_FIFO_TX_TRIG16        (1 << 4)
86 #define UART3_FCR_FIFO_RX_TRIG16        (1 << 6)
87 #define UART3_FCR_CONFIG        (\
88                 UART3_FCR_FIFO_EN | UART3_FCR_FIFO_RX   |\
89                 UART3_FCR_FIFO_TX | UART3_FCR_FIFO_DMA1 |\
90                 UART3_FCR_FIFO_TX_TRIG16                |\
91                 UART3_FCR_FIFO_RX_TRIG16)
92
93 #define UART3_SCR_TX_TRIG1              (1 << 6)
94 #define UART3_SCR_RX_TRIG1              (1 << 7)
95
96 #define UART3_MDR1_RESET                (0x07)
97 #define UART3_MDR1_SIR                  (1 << 0)
98 #define UART3_MDR1_MIR                  (4 << 0)
99 #define UART3_MDR1_FIR                  (5 << 0)
100 #define UART3_MDR1_SIP_AUTO             (1 << 6)
101
102 #define UART3_MDR2_TRIG1                (0 << 1)
103 #define UART3_MDR2_IRTX_UNDERRUN        (1 << 0)
104
105 #define UART3_ACERG_TX_UNDERRUN_DIS     (1 << 4)
106 #define UART3_ACERG_SD_MODE_LOW         (1 << 6)
107 #define UART3_ACERG_DIS_IR_RX           (1 << 5)
108
109 #define UART3_IER_EOF                   (1 << 5)
110 #define UART3_IER_CTS                   (1 << 7)
111
112 #define UART3_IIR_TX_STATUS             (1 << 5)
113 #define UART3_IIR_EOF                   (0x80)
114
115 #define IS_FIR(omap_ir)         ((omap_ir)->speed >= 4000000)
116
117 struct omap_irda {
118         unsigned char open;
119         int speed;              /* Current IrDA speed */
120         int newspeed;
121
122         struct net_device_stats stats;
123         struct irlap_cb *irlap;
124         struct qos_info qos;
125
126         int rx_dma_channel;
127         int tx_dma_channel;
128
129         dma_addr_t rx_buf_dma_phys;     /* Physical address of RX DMA buffer */
130         dma_addr_t tx_buf_dma_phys;     /* Physical address of TX DMA buffer */
131
132         void *rx_buf_dma_virt;          /* Virtual address of RX DMA buffer */
133         void *tx_buf_dma_virt;          /* Virtual address of TX DMA buffer */
134
135         struct device *dev;
136         struct omap_irda_config *pdata;
137 };
138
139 static void inline uart_reg_out(int idx, u8 val)
140 {
141         omap_writeb(val, idx);
142 }
143
144 static u8 inline uart_reg_in(int idx)
145 {
146         u8 b = omap_readb(idx);
147         return b;
148 }
149
150 /* forward declarations */
151 extern void omap_stop_dma(int lch);
152 static int omap_irda_set_speed(struct net_device *dev, int speed);
153
154 static void omap_irda_start_rx_dma(struct omap_irda *omap_ir)
155 {
156         /* Configure DMA */
157         omap_set_dma_src_params(omap_ir->rx_dma_channel, 0x3, 0x0,
158                                 omap_ir->pdata->src_start,
159                                 0, 0);
160
161         omap_enable_dma_irq(omap_ir->rx_dma_channel, 0x01);
162
163         omap_set_dma_dest_params(omap_ir->rx_dma_channel, 0x0, 0x1,
164                                 omap_ir->rx_buf_dma_phys,
165                                 0, 0);
166
167         omap_set_dma_transfer_params(omap_ir->rx_dma_channel, 0x0,
168                                 IRDA_SKB_MAX_MTU, 0x1,
169                                 0x0, omap_ir->pdata->rx_trigger, 0);
170
171         omap_start_dma(omap_ir->rx_dma_channel);
172 }
173
174 static void omap_start_tx_dma(struct omap_irda *omap_ir, int size)
175 {
176         /* Configure DMA */
177         omap_set_dma_dest_params(omap_ir->tx_dma_channel, 0x03, 0x0,
178                                 omap_ir->pdata->dest_start, 0, 0);
179
180         omap_enable_dma_irq(omap_ir->tx_dma_channel, 0x01);
181
182         omap_set_dma_src_params(omap_ir->tx_dma_channel, 0x0, 0x1,
183                                 omap_ir->tx_buf_dma_phys,
184                                 0, 0);
185
186         omap_set_dma_transfer_params(omap_ir->tx_dma_channel, 0x0, size, 0x1,
187                                 0x0, omap_ir->pdata->tx_trigger, 0);
188
189         /* Start DMA */
190         omap_start_dma(omap_ir->tx_dma_channel);
191 }
192
193 /* DMA RX callback - normally, we should not go here,
194  * it calls only if something is going wrong
195  */
196 static void omap_irda_rx_dma_callback(int lch, u16 ch_status, void *data)
197 {
198         struct net_device *dev = data;
199         struct omap_irda *omap_ir = netdev_priv(dev);
200
201         printk(KERN_ERR "RX Transfer error or very big frame\n");
202
203         /* Clear interrupts */
204         uart_reg_in(UART3_IIR);
205
206         omap_ir->stats.rx_frame_errors++;
207
208         uart_reg_in(UART3_RESUME);
209
210         /* Re-init RX DMA */
211         omap_irda_start_rx_dma(omap_ir);
212 }
213
214 /* DMA TX callback - calling when frame transfer has been finished */
215 static void omap_irda_tx_dma_callback(int lch, u16 ch_status, void *data)
216 {
217         struct net_device *dev = data;
218         struct omap_irda *omap_ir = netdev_priv(dev);
219
220         /*Stop DMA controller */
221         omap_stop_dma(omap_ir->tx_dma_channel);
222 }
223
224 /*
225  * Set the IrDA communications speed.
226  * Interrupt have to be disabled here.
227  */
228 static int omap_irda_startup(struct net_device *dev)
229 {
230         struct omap_irda *omap_ir = netdev_priv(dev);
231
232         /* FIXME: use clk_* apis for UART3 clock*/
233         /* Enable UART3 clock and set UART3 to IrDA mode */
234         if (machine_is_omap_h2() || machine_is_omap_h3())
235                 omap_writel(omap_readl(MOD_CONF_CTRL_0) | (1 << 31) | (1 << 15),
236                                 MOD_CONF_CTRL_0);
237
238         /* Only for H2?
239          */
240         if (omap_ir->pdata->transceiver_mode && machine_is_omap_h2()) {
241                 /* Is it select_irda on H2 ? */
242                 omap_writel(omap_readl(FUNC_MUX_CTRL_A) | 7,
243                                         FUNC_MUX_CTRL_A);
244                 omap_ir->pdata->transceiver_mode(omap_ir->dev, IR_SIRMODE);
245         }
246
247         uart_reg_out(UART3_MDR1, UART3_MDR1_RESET);     /* Reset mode */
248
249         /* Clear DLH and DLL */
250         uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
251
252         uart_reg_out(UART3_DLL, 0);
253         uart_reg_out(UART3_DLH, 0);
254         uart_reg_out(UART3_LCR, 0xbf);  /* FIXME: Add #define */
255
256         uart_reg_out(UART3_EFR, UART3_EFR_EN);
257         uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
258
259         /* Enable access to UART3_TLR and UART3_TCR registers */
260         uart_reg_out(UART3_MCR, UART3_MCR_EN_TCR_TLR);
261
262         uart_reg_out(UART3_SCR, 0);
263         /* Set Rx trigger to 1 and Tx trigger to 1 */
264         uart_reg_out(UART3_TLR, 0);
265
266         /* Set LCR to 8 bits and 1 stop bit */
267         uart_reg_out(UART3_LCR, 0x03);
268
269         /* Clear RX and TX FIFO and enable FIFO */
270         /* Use DMA Req for transfers */
271         uart_reg_out(UART3_FCR, UART3_FCR_CONFIG);
272
273         uart_reg_out(UART3_MCR, 0);
274
275         uart_reg_out(UART3_SCR, UART3_SCR_TX_TRIG1 |
276                         UART3_SCR_RX_TRIG1);
277
278         /* Enable UART3 SIR Mode,(Frame-length method to end frames) */
279         uart_reg_out(UART3_MDR1, UART3_MDR1_SIR);
280
281         /* Set Status FIFO trig to 1 */
282         uart_reg_out(UART3_MDR2, 0);
283
284         /* Enables RXIR input */
285         /* and disable TX underrun */
286         /* SEND_SIP pulse */
287         uart_reg_out(UART3_ACREG, UART3_ACERG_SD_MODE_LOW |
288                         UART3_ACERG_TX_UNDERRUN_DIS);
289
290         /* Enable EOF Interrupt only */
291         uart_reg_out(UART3_IER, UART3_IER_CTS | UART3_IER_EOF);
292
293         /* Set Maximum Received Frame size to 2048 bytes */
294         uart_reg_out(UART3_RXFLL, 0x00);
295         uart_reg_out(UART3_RXFLH, 0x08);
296
297         uart_reg_in(UART3_RESUME);
298
299         return 0;
300 }
301
302 static int omap_irda_shutdown(struct omap_irda *omap_ir)
303 {
304         unsigned long flags;
305
306         local_irq_save(flags);
307
308         /* Disable all UART3 Interrupts */
309         uart_reg_out(UART3_IER, 0);
310
311         /* Disable UART3 and disable baud rate generator */
312         uart_reg_out(UART3_MDR1, UART3_MDR1_RESET);
313
314         /* set SD_MODE pin to high and Disable RX IR */
315         uart_reg_out(UART3_ACREG, (UART3_ACERG_DIS_IR_RX |
316                         ~(UART3_ACERG_SD_MODE_LOW)));
317
318         /* Clear DLH and DLL */
319         uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
320         uart_reg_out(UART3_DLL, 0);
321         uart_reg_out(UART3_DLH, 0);
322
323         local_irq_restore(flags);
324
325         return 0;
326 }
327
328 static irqreturn_t
329 omap_irda_irq(int irq, void *dev_id)
330 {
331         struct net_device *dev = dev_id;
332         struct omap_irda *omap_ir = netdev_priv(dev);
333         struct sk_buff *skb;
334
335         u8 status;
336         int w = 0;
337
338         /* Clear EOF interrupt */
339         status = uart_reg_in(UART3_IIR);
340
341         if (status & UART3_IIR_TX_STATUS) {
342                 u8 mdr2 = uart_reg_in(UART3_MDR2);
343                 if (mdr2 & UART3_MDR2_IRTX_UNDERRUN)
344                         printk(KERN_ERR "IrDA Buffer underrun error\n");
345
346                 omap_ir->stats.tx_packets++;
347
348                 if (omap_ir->newspeed) {
349                         omap_irda_set_speed(dev, omap_ir->newspeed);
350                         omap_ir->newspeed = 0;
351                 }
352
353                 netif_wake_queue(dev);
354                 if (!(status & UART3_IIR_EOF))
355                         return IRQ_HANDLED;
356         }
357
358         /* Stop DMA and if there are no errors, send frame to upper layer */
359         omap_stop_dma(omap_ir->rx_dma_channel);
360
361         status = uart_reg_in(UART3_SFLSR);      /* Take a frame status */
362
363         if (status != 0) {      /* Bad frame? */
364                 omap_ir->stats.rx_frame_errors++;
365                 uart_reg_in(UART3_RESUME);
366         } else {
367                 /* We got a frame! */
368                 skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
369
370                 if (!skb) {
371                         printk(KERN_ERR "omap_sir: out of memory for RX SKB\n");
372                         return IRQ_HANDLED;
373                 }
374                 /*
375                  * Align any IP headers that may be contained
376                  * within the frame.
377                  */
378
379                 skb_reserve(skb, 1);
380
381                 w = omap_get_dma_dst_pos(omap_ir->rx_dma_channel) -
382                                                 omap_ir->rx_buf_dma_phys;
383
384                 if (!IS_FIR(omap_ir))
385                         /* Copy DMA buffer to skb */
386                         memcpy(skb_put(skb, w - 2), omap_ir->rx_buf_dma_virt,
387                                         w - 2);
388                 else
389                         /* Copy DMA buffer to skb */
390                         memcpy(skb_put(skb, w - 4), omap_ir->rx_buf_dma_virt,
391                                         w - 4);
392
393                 skb->dev = dev;
394                 skb_reset_mac_header(skb);
395                 skb->protocol = htons(ETH_P_IRDA);
396                 omap_ir->stats.rx_packets++;
397                 omap_ir->stats.rx_bytes += skb->len;
398                 netif_receive_skb(skb); /* Send data to upper level */
399         }
400
401         /* Re-init RX DMA */
402         omap_irda_start_rx_dma(omap_ir);
403
404         dev->last_rx = jiffies;
405
406         return IRQ_HANDLED;
407 }
408
409 static int omap_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
410 {
411         struct omap_irda *omap_ir = netdev_priv(dev);
412         int speed = irda_get_next_speed(skb);
413         int mtt = irda_get_mtt(skb);
414         int xbofs = irda_get_next_xbofs(skb);
415
416
417         /*
418          * Does this packet contain a request to change the interface
419          * speed?  If so, remember it until we complete the transmission
420          * of this frame.
421          */
422         if (speed != omap_ir->speed && speed != -1)
423                 omap_ir->newspeed = speed;
424
425         if (xbofs) /* Set number of addtional BOFS */
426                 uart_reg_out(UART3_EBLR, xbofs + 1);
427
428         /*
429          * If this is an empty frame, we can bypass a lot.
430          */
431         if (skb->len == 0) {
432                 if (omap_ir->newspeed) {
433                         omap_ir->newspeed = 0;
434                         omap_irda_set_speed(dev, speed);
435                 }
436                 dev_kfree_skb(skb);
437                 return 0;
438         }
439
440         netif_stop_queue(dev);
441
442         /* Copy skb data to DMA buffer */
443         skb_copy_from_linear_data(skb, omap_ir->tx_buf_dma_virt, skb->len);
444
445         /* Copy skb data to DMA buffer */
446         omap_ir->stats.tx_bytes += skb->len;
447
448         /* Set frame length */
449         uart_reg_out(UART3_TXFLL, (skb->len & 0xff));
450         uart_reg_out(UART3_TXFLH, (skb->len >> 8));
451
452         if (mtt > 1000)
453                 mdelay(mtt / 1000);
454         else
455                 udelay(mtt);
456
457         /* Start TX DMA transfer */
458         omap_start_tx_dma(omap_ir, skb->len);
459
460         /* We can free skb now because it's already in DMA buffer */
461         dev_kfree_skb(skb);
462
463         dev->trans_start = jiffies;
464
465         return 0;
466 }
467
468 static int
469 omap_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
470 {
471         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
472         struct omap_irda *omap_ir = netdev_priv(dev);
473         int ret = -EOPNOTSUPP;
474
475
476         switch (cmd) {
477         case SIOCSBANDWIDTH:
478                 if (capable(CAP_NET_ADMIN)) {
479                         /*
480                          * We are unable to set the speed if the
481                          * device is not running.
482                          */
483                         if (omap_ir->open)
484                                 ret = omap_irda_set_speed(dev,
485                                                 rq->ifr_baudrate);
486                         else {
487                                 printk(KERN_ERR "omap_ir: SIOCSBANDWIDTH:"
488                                                 " !netif_running\n");
489                                 ret = 0;
490                         }
491                 }
492                 break;
493
494         case SIOCSMEDIABUSY:
495                 ret = -EPERM;
496                 if (capable(CAP_NET_ADMIN)) {
497                         irda_device_set_media_busy(dev, TRUE);
498                         ret = 0;
499                 }
500                 break;
501
502         case SIOCGRECEIVING:
503                 rq->ifr_receiving = 0;
504                 break;
505
506         default:
507                 break;
508         }
509
510         return ret;
511 }
512
513 static struct net_device_stats *omap_irda_stats(struct net_device *dev)
514 {
515         struct omap_irda *omap_ir = netdev_priv(dev);
516         return &omap_ir->stats;
517 }
518
519 static int omap_irda_start(struct net_device *dev)
520 {
521         struct omap_irda *omap_ir = netdev_priv(dev);
522         int err;
523
524         omap_ir->speed = 9600;
525
526         err = request_irq(dev->irq, omap_irda_irq, 0, dev->name, dev);
527         if (err)
528                 goto err_irq;
529
530         /*
531          * The interrupt must remain disabled for now.
532          */
533         disable_irq(dev->irq);
534
535         /*  Request DMA channels for IrDA hardware */
536         if (omap_request_dma(omap_ir->pdata->rx_channel, "IrDA Rx DMA",
537                         (void *)omap_irda_rx_dma_callback,
538                         dev, &(omap_ir->rx_dma_channel))) {
539                 printk(KERN_ERR "Failed to request IrDA Rx DMA\n");
540                 goto err_irq;
541         }
542
543         if (omap_request_dma(omap_ir->pdata->tx_channel, "IrDA Tx DMA",
544                         (void *)omap_irda_tx_dma_callback,
545                         dev, &(omap_ir->tx_dma_channel))) {
546                 printk(KERN_ERR "Failed to request IrDA Tx DMA\n");
547                 goto err_irq;
548         }
549
550         /* Allocate TX and RX buffers for DMA channels */
551         omap_ir->rx_buf_dma_virt =
552                 dma_alloc_coherent(NULL, IRDA_SKB_MAX_MTU,
553                                 &(omap_ir->rx_buf_dma_phys),
554                                 GFP_KERNEL);
555
556         if (!omap_ir->rx_buf_dma_virt) {
557                 printk(KERN_ERR "Unable to allocate memory for rx_buf_dma\n");
558                 goto err_irq;
559         }
560
561         omap_ir->tx_buf_dma_virt =
562                 dma_alloc_coherent(NULL, IRDA_SIR_MAX_FRAME,
563                                 &(omap_ir->tx_buf_dma_phys),
564                                 GFP_KERNEL);
565
566         if (!omap_ir->tx_buf_dma_virt) {
567                 printk(KERN_ERR "Unable to allocate memory for tx_buf_dma\n");
568                 goto err_mem1;
569         }
570
571         /*
572          * Setup the serial port for the specified config.
573          */
574         if (omap_ir->pdata->select_irda)
575                 omap_ir->pdata->select_irda(omap_ir->dev, IR_SEL);
576
577         err = omap_irda_startup(dev);
578
579         if (err)
580                 goto err_startup;
581
582         omap_irda_set_speed(dev, omap_ir->speed = 9600);
583
584         /*
585          * Open a new IrLAP layer instance.
586          */
587         omap_ir->irlap = irlap_open(dev, &omap_ir->qos, "omap_sir");
588
589         err = -ENOMEM;
590         if (!omap_ir->irlap)
591                 goto err_irlap;
592
593         /* Now enable the interrupt and start the queue */
594         omap_ir->open = 1;
595
596         /* Start RX DMA */
597         omap_irda_start_rx_dma(omap_ir);
598
599         enable_irq(dev->irq);
600         netif_start_queue(dev);
601
602         return 0;
603
604 err_irlap:
605         omap_ir->open = 0;
606         omap_irda_shutdown(omap_ir);
607 err_startup:
608         dma_free_coherent(NULL, IRDA_SIR_MAX_FRAME,
609                         omap_ir->tx_buf_dma_virt, omap_ir->tx_buf_dma_phys);
610 err_mem1:
611         dma_free_coherent(NULL, IRDA_SKB_MAX_MTU,
612                         omap_ir->rx_buf_dma_virt, omap_ir->rx_buf_dma_phys);
613 err_irq:
614         free_irq(dev->irq, dev);
615         return err;
616 }
617
618 static int omap_irda_stop(struct net_device *dev)
619 {
620         struct omap_irda *omap_ir = netdev_priv(dev);
621
622         disable_irq(dev->irq);
623
624         netif_stop_queue(dev);
625
626         omap_free_dma(omap_ir->rx_dma_channel);
627         omap_free_dma(omap_ir->tx_dma_channel);
628
629         if (omap_ir->rx_buf_dma_virt)
630                 dma_free_coherent(NULL, IRDA_SKB_MAX_MTU,
631                                 omap_ir->rx_buf_dma_virt,
632                                 omap_ir->rx_buf_dma_phys);
633         if (omap_ir->tx_buf_dma_virt)
634                 dma_free_coherent(NULL, IRDA_SIR_MAX_FRAME,
635                                 omap_ir->tx_buf_dma_virt,
636                                 omap_ir->tx_buf_dma_phys);
637
638         omap_irda_shutdown(omap_ir);
639
640         /* Stop IrLAP */
641         if (omap_ir->irlap) {
642                 irlap_close(omap_ir->irlap);
643                 omap_ir->irlap = NULL;
644         }
645
646         omap_ir->open = 0;
647
648         /*
649          * Free resources
650          */
651         free_irq(dev->irq, dev);
652
653         return 0;
654 }
655
656 static int omap_irda_set_speed(struct net_device *dev, int speed)
657 {
658         struct omap_irda *omap_ir = netdev_priv(dev);
659         int divisor;
660         unsigned long flags;
661
662         /* Set IrDA speed */
663         if (speed <= 115200) {
664
665                 local_irq_save(flags);
666
667                 /* SIR mode */
668                 if (omap_ir->pdata->transceiver_mode)
669                         omap_ir->pdata->transceiver_mode(omap_ir->dev,
670                                                         IR_SIRMODE);
671
672                 /* Set SIR mode */
673                 uart_reg_out(UART3_MDR1, 1);
674                 uart_reg_out(UART3_EBLR, 1);
675
676                 divisor = 48000000 / (16 * speed);      /* Base clock 48 MHz */
677
678                 uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
679                 uart_reg_out(UART3_DLL, (divisor & 0xff));
680                 uart_reg_out(UART3_DLH, (divisor >> 8));
681                 uart_reg_out(UART3_LCR, 0x03);
682
683                 uart_reg_out(UART3_MCR, 0);
684
685                 local_irq_restore(flags);
686         } else if (speed <= 1152000) {
687
688                 local_irq_save(flags);
689
690                 /* Set MIR mode, auto SIP */
691                 uart_reg_out(UART3_MDR1, UART3_MDR1_MIR |
692                                 UART3_MDR1_SIP_AUTO);
693
694                 uart_reg_out(UART3_EBLR, 2);
695
696                 divisor = 48000000 / (41 * speed);      /* Base clock 48 MHz */
697
698                 uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
699                 uart_reg_out(UART3_DLL, (divisor & 0xff));
700                 uart_reg_out(UART3_DLH, (divisor >> 8));
701                 uart_reg_out(UART3_LCR, 0x03);
702
703                 if (omap_ir->pdata->transceiver_mode)
704                         omap_ir->pdata->transceiver_mode(omap_ir->dev,
705                                                         IR_MIRMODE);
706
707                 local_irq_restore(flags);
708         } else {
709                 local_irq_save(flags);
710
711                 /* FIR mode */
712                 uart_reg_out(UART3_MDR1, UART3_MDR1_FIR |
713                                 UART3_MDR1_SIP_AUTO);
714
715                 if (omap_ir->pdata->transceiver_mode)
716                         omap_ir->pdata->transceiver_mode(omap_ir->dev,
717                                                         IR_FIRMODE);
718
719                 local_irq_restore(flags);
720         }
721
722         omap_ir->speed = speed;
723
724         return 0;
725 }
726
727 #ifdef CONFIG_PM
728 /*
729  * Suspend the IrDA interface.
730  */
731 static int omap_irda_suspend(struct platform_device *pdev, pm_message_t state)
732 {
733         struct net_device *dev = platform_get_drvdata(pdev);
734         struct omap_irda *omap_ir = netdev_priv(dev);
735
736         if (!dev)
737                 return 0;
738
739         if (omap_ir->open) {
740                 /*
741                  * Stop the transmit queue
742                  */
743                 netif_device_detach(dev);
744                 disable_irq(dev->irq);
745                 omap_irda_shutdown(omap_ir);
746         }
747         return 0;
748 }
749
750 /*
751  * Resume the IrDA interface.
752  */
753 static int omap_irda_resume(struct platform_device *pdev)
754 {
755         struct net_device *dev = platform_get_drvdata(pdev);
756         struct omap_irda *omap_ir= netdev_priv(dev);
757
758         if (!dev)
759                 return 0;
760
761         if (omap_ir->open) {
762                 /*
763                  * If we missed a speed change, initialise at the new speed
764                  * directly.  It is debatable whether this is actually
765                  * required, but in the interests of continuing from where
766                  * we left off it is desireable.  The converse argument is
767                  * that we should re-negotiate at 9600 baud again.
768                  */
769                 if (omap_ir->newspeed) {
770                         omap_ir->speed = omap_ir->newspeed;
771                         omap_ir->newspeed = 0;
772                 }
773
774                 omap_irda_startup(dev);
775                 omap_irda_set_speed(dev, omap_ir->speed);
776                 enable_irq(dev->irq);
777
778                 /*
779                  * This automatically wakes up the queue
780                  */
781                 netif_device_attach(dev);
782         }
783
784         return 0;
785 }
786 #else
787 #define omap_irda_suspend       NULL
788 #define omap_irda_resume        NULL
789 #endif
790
791 static int omap_irda_probe(struct platform_device *pdev)
792 {
793         struct net_device *dev;
794         struct omap_irda *omap_ir;
795         struct omap_irda_config *pdata = pdev->dev.platform_data;
796         unsigned int baudrate_mask;
797         int err = 0;
798         int irq = NO_IRQ;
799
800         if (!pdata) {
801                 printk(KERN_ERR "IrDA Platform data not supplied\n");
802                 return -ENOENT;
803         }
804
805         if (!pdata->rx_channel || !pdata->tx_channel) {
806                 printk(KERN_ERR "IrDA invalid rx/tx channel value\n");
807                 return -ENOENT;
808         }
809
810         irq = platform_get_irq(pdev, 0);
811         if (irq <= 0) {
812                 printk(KERN_WARNING "no irq for IrDA\n");
813                 return -ENOENT;
814         }
815
816         dev = alloc_irdadev(sizeof(struct omap_irda));
817         if (!dev)
818                 goto err_mem_1;
819
820
821         omap_ir = netdev_priv(dev);
822         omap_ir->dev = &pdev->dev;
823         omap_ir->pdata = pdata;
824
825         dev->hard_start_xmit    = omap_irda_hard_xmit;
826         dev->open               = omap_irda_start;
827         dev->stop               = omap_irda_stop;
828         dev->do_ioctl           = omap_irda_ioctl;
829         dev->get_stats          = omap_irda_stats;
830         dev->irq                = irq;
831
832         irda_init_max_qos_capabilies(&omap_ir->qos);
833
834         baudrate_mask = 0;
835         if (omap_ir->pdata->transceiver_cap & IR_SIRMODE)
836                 baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
837         if (omap_ir->pdata->transceiver_cap & IR_MIRMODE)
838                 baudrate_mask |= IR_57600 | IR_1152000;
839         if (omap_ir->pdata->transceiver_cap & IR_FIRMODE)
840                 baudrate_mask |= IR_4000000 << 8;
841
842         omap_ir->qos.baud_rate.bits &= baudrate_mask;
843         omap_ir->qos.min_turn_time.bits = 7;
844
845         irda_qos_bits_to_value(&omap_ir->qos);
846
847         /* Any better way to avoid this? No. */
848         if (machine_is_omap_h3() || machine_is_omap_h4())
849                 INIT_DELAYED_WORK(&omap_ir->pdata->gpio_expa, NULL);
850
851         err = register_netdev(dev);
852         if (!err)
853                 platform_set_drvdata(pdev, dev);
854         else
855                 free_netdev(dev);
856
857 err_mem_1:
858         return err;
859 }
860
861 static int omap_irda_remove(struct platform_device *pdev)
862 {
863         struct net_device *dev = platform_get_drvdata(pdev);
864
865         if (pdev) {
866                 unregister_netdev(dev);
867                 free_netdev(dev);
868         }
869         return 0;
870 }
871
872 static struct platform_driver omapir_driver = {
873         .probe          = omap_irda_probe,
874         .remove         = omap_irda_remove,
875         .suspend        = omap_irda_suspend,
876         .resume         = omap_irda_resume,
877         .driver         = {
878                 .name   = "omapirda",
879         },
880 };
881
882 static char __initdata banner[] = KERN_INFO "OMAP IrDA driver initializing\n";
883
884 static int __init omap_irda_init(void)
885 {
886         printk(banner);
887         return platform_driver_register(&omapir_driver);
888 }
889
890 static void __exit omap_irda_exit(void)
891 {
892         platform_driver_unregister(&omapir_driver);
893 }
894
895 module_init(omap_irda_init);
896 module_exit(omap_irda_exit);
897
898 MODULE_AUTHOR("MontaVista");
899 MODULE_DESCRIPTION("OMAP IrDA Driver");
900 MODULE_LICENSE("GPL");
901