]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/irda/omap-ir.c
Merge current mainline tree into linux-omap tree
[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 <asm/hardware.h>
67 #include <asm/serial.h>
68 #include <asm/mach-types.h>
69 #include <asm/dma.h>
70 #include <asm/arch/mux.h>
71 #include <asm/arch/gpio.h>
72 #include <asm/arch/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_DMA_CDAC_REG(omap_ir->rx_dma_channel);
382
383                 if (cpu_is_omap16xx())
384                         w -= OMAP1_DMA_CDSA_L_REG(omap_ir->rx_dma_channel);
385                 if (cpu_is_omap24xx())
386                         w -= OMAP2_DMA_CDSA_REG(omap_ir->rx_dma_channel);
387
388                 if (!IS_FIR(omap_ir))
389                         /* Copy DMA buffer to skb */
390                         memcpy(skb_put(skb, w - 2), omap_ir->rx_buf_dma_virt,
391                                         w - 2);
392                 else
393                         /* Copy DMA buffer to skb */
394                         memcpy(skb_put(skb, w - 4), omap_ir->rx_buf_dma_virt,
395                                         w - 4);
396
397                 skb->dev = dev;
398                 skb_reset_mac_header(skb);
399                 skb->protocol = htons(ETH_P_IRDA);
400                 omap_ir->stats.rx_packets++;
401                 omap_ir->stats.rx_bytes += skb->len;
402                 netif_receive_skb(skb); /* Send data to upper level */
403         }
404
405         /* Re-init RX DMA */
406         omap_irda_start_rx_dma(omap_ir);
407
408         dev->last_rx = jiffies;
409
410         return IRQ_HANDLED;
411 }
412
413 static int omap_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
414 {
415         struct omap_irda *omap_ir = netdev_priv(dev);
416         int speed = irda_get_next_speed(skb);
417         int mtt = irda_get_mtt(skb);
418         int xbofs = irda_get_next_xbofs(skb);
419
420
421         /*
422          * Does this packet contain a request to change the interface
423          * speed?  If so, remember it until we complete the transmission
424          * of this frame.
425          */
426         if (speed != omap_ir->speed && speed != -1)
427                 omap_ir->newspeed = speed;
428
429         if (xbofs) /* Set number of addtional BOFS */
430                 uart_reg_out(UART3_EBLR, xbofs + 1);
431
432         /*
433          * If this is an empty frame, we can bypass a lot.
434          */
435         if (skb->len == 0) {
436                 if (omap_ir->newspeed) {
437                         omap_ir->newspeed = 0;
438                         omap_irda_set_speed(dev, speed);
439                 }
440                 dev_kfree_skb(skb);
441                 return 0;
442         }
443
444         netif_stop_queue(dev);
445
446         /* Copy skb data to DMA buffer */
447         skb_copy_from_linear_data(skb, omap_ir->tx_buf_dma_virt, skb->len);
448
449         /* Copy skb data to DMA buffer */
450         omap_ir->stats.tx_bytes += skb->len;
451
452         /* Set frame length */
453         uart_reg_out(UART3_TXFLL, (skb->len & 0xff));
454         uart_reg_out(UART3_TXFLH, (skb->len >> 8));
455
456         if (mtt > 1000)
457                 mdelay(mtt / 1000);
458         else
459                 udelay(mtt);
460
461         /* Start TX DMA transfer */
462         omap_start_tx_dma(omap_ir, skb->len);
463
464         /* We can free skb now because it's already in DMA buffer */
465         dev_kfree_skb(skb);
466
467         dev->trans_start = jiffies;
468
469         return 0;
470 }
471
472 static int
473 omap_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
474 {
475         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
476         struct omap_irda *omap_ir = netdev_priv(dev);
477         int ret = -EOPNOTSUPP;
478
479
480         switch (cmd) {
481         case SIOCSBANDWIDTH:
482                 if (capable(CAP_NET_ADMIN)) {
483                         /*
484                          * We are unable to set the speed if the
485                          * device is not running.
486                          */
487                         if (omap_ir->open)
488                                 ret = omap_irda_set_speed(dev,
489                                                 rq->ifr_baudrate);
490                         else {
491                                 printk(KERN_ERR "omap_ir: SIOCSBANDWIDTH:"
492                                                 " !netif_running\n");
493                                 ret = 0;
494                         }
495                 }
496                 break;
497
498         case SIOCSMEDIABUSY:
499                 ret = -EPERM;
500                 if (capable(CAP_NET_ADMIN)) {
501                         irda_device_set_media_busy(dev, TRUE);
502                         ret = 0;
503                 }
504                 break;
505
506         case SIOCGRECEIVING:
507                 rq->ifr_receiving = 0;
508                 break;
509
510         default:
511                 break;
512         }
513
514         return ret;
515 }
516
517 static struct net_device_stats *omap_irda_stats(struct net_device *dev)
518 {
519         struct omap_irda *omap_ir = netdev_priv(dev);
520         return &omap_ir->stats;
521 }
522
523 static int omap_irda_start(struct net_device *dev)
524 {
525         struct omap_irda *omap_ir = netdev_priv(dev);
526         int err;
527
528         omap_ir->speed = 9600;
529
530         err = request_irq(dev->irq, omap_irda_irq, 0, dev->name, dev);
531         if (err)
532                 goto err_irq;
533
534         /*
535          * The interrupt must remain disabled for now.
536          */
537         disable_irq(dev->irq);
538
539         /*  Request DMA channels for IrDA hardware */
540         if (omap_request_dma(omap_ir->pdata->rx_channel, "IrDA Rx DMA",
541                         (void *)omap_irda_rx_dma_callback,
542                         dev, &(omap_ir->rx_dma_channel))) {
543                 printk(KERN_ERR "Failed to request IrDA Rx DMA\n");
544                 goto err_irq;
545         }
546
547         if (omap_request_dma(omap_ir->pdata->tx_channel, "IrDA Tx DMA",
548                         (void *)omap_irda_tx_dma_callback,
549                         dev, &(omap_ir->tx_dma_channel))) {
550                 printk(KERN_ERR "Failed to request IrDA Tx DMA\n");
551                 goto err_irq;
552         }
553
554         /* Allocate TX and RX buffers for DMA channels */
555         omap_ir->rx_buf_dma_virt =
556                 dma_alloc_coherent(NULL, IRDA_SKB_MAX_MTU,
557                                 &(omap_ir->rx_buf_dma_phys),
558                                 GFP_KERNEL);
559
560         if (!omap_ir->rx_buf_dma_virt) {
561                 printk(KERN_ERR "Unable to allocate memory for rx_buf_dma\n");
562                 goto err_irq;
563         }
564
565         omap_ir->tx_buf_dma_virt =
566                 dma_alloc_coherent(NULL, IRDA_SIR_MAX_FRAME,
567                                 &(omap_ir->tx_buf_dma_phys),
568                                 GFP_KERNEL);
569
570         if (!omap_ir->tx_buf_dma_virt) {
571                 printk(KERN_ERR "Unable to allocate memory for tx_buf_dma\n");
572                 goto err_mem1;
573         }
574
575         /*
576          * Setup the serial port for the specified config.
577          */
578         if (omap_ir->pdata->select_irda)
579                 omap_ir->pdata->select_irda(omap_ir->dev, IR_SEL);
580
581         err = omap_irda_startup(dev);
582
583         if (err)
584                 goto err_startup;
585
586         omap_irda_set_speed(dev, omap_ir->speed = 9600);
587
588         /*
589          * Open a new IrLAP layer instance.
590          */
591         omap_ir->irlap = irlap_open(dev, &omap_ir->qos, "omap_sir");
592
593         err = -ENOMEM;
594         if (!omap_ir->irlap)
595                 goto err_irlap;
596
597         /* Now enable the interrupt and start the queue */
598         omap_ir->open = 1;
599
600         /* Start RX DMA */
601         omap_irda_start_rx_dma(omap_ir);
602
603         enable_irq(dev->irq);
604         netif_start_queue(dev);
605
606         return 0;
607
608 err_irlap:
609         omap_ir->open = 0;
610         omap_irda_shutdown(omap_ir);
611 err_startup:
612         dma_free_coherent(NULL, IRDA_SIR_MAX_FRAME,
613                         omap_ir->tx_buf_dma_virt, omap_ir->tx_buf_dma_phys);
614 err_mem1:
615         dma_free_coherent(NULL, IRDA_SKB_MAX_MTU,
616                         omap_ir->rx_buf_dma_virt, omap_ir->rx_buf_dma_phys);
617 err_irq:
618         free_irq(dev->irq, dev);
619         return err;
620 }
621
622 static int omap_irda_stop(struct net_device *dev)
623 {
624         struct omap_irda *omap_ir = netdev_priv(dev);
625
626         disable_irq(dev->irq);
627
628         netif_stop_queue(dev);
629
630         omap_free_dma(omap_ir->rx_dma_channel);
631         omap_free_dma(omap_ir->tx_dma_channel);
632
633         if (omap_ir->rx_buf_dma_virt)
634                 dma_free_coherent(NULL, IRDA_SKB_MAX_MTU,
635                                 omap_ir->rx_buf_dma_virt,
636                                 omap_ir->rx_buf_dma_phys);
637         if (omap_ir->tx_buf_dma_virt)
638                 dma_free_coherent(NULL, IRDA_SIR_MAX_FRAME,
639                                 omap_ir->tx_buf_dma_virt,
640                                 omap_ir->tx_buf_dma_phys);
641
642         omap_irda_shutdown(omap_ir);
643
644         /* Stop IrLAP */
645         if (omap_ir->irlap) {
646                 irlap_close(omap_ir->irlap);
647                 omap_ir->irlap = NULL;
648         }
649
650         omap_ir->open = 0;
651
652         /*
653          * Free resources
654          */
655         free_irq(dev->irq, dev);
656
657         return 0;
658 }
659
660 static int omap_irda_set_speed(struct net_device *dev, int speed)
661 {
662         struct omap_irda *omap_ir = netdev_priv(dev);
663         int divisor;
664         unsigned long flags;
665
666         /* Set IrDA speed */
667         if (speed <= 115200) {
668
669                 local_irq_save(flags);
670
671                 /* SIR mode */
672                 if (omap_ir->pdata->transceiver_mode)
673                         omap_ir->pdata->transceiver_mode(omap_ir->dev,
674                                                         IR_SIRMODE);
675
676                 /* Set SIR mode */
677                 uart_reg_out(UART3_MDR1, 1);
678                 uart_reg_out(UART3_EBLR, 1);
679
680                 divisor = 48000000 / (16 * speed);      /* Base clock 48 MHz */
681
682                 uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
683                 uart_reg_out(UART3_DLL, (divisor & 0xff));
684                 uart_reg_out(UART3_DLH, (divisor >> 8));
685                 uart_reg_out(UART3_LCR, 0x03);
686
687                 uart_reg_out(UART3_MCR, 0);
688
689                 local_irq_restore(flags);
690         } else if (speed <= 1152000) {
691
692                 local_irq_save(flags);
693
694                 /* Set MIR mode, auto SIP */
695                 uart_reg_out(UART3_MDR1, UART3_MDR1_MIR |
696                                 UART3_MDR1_SIP_AUTO);
697
698                 uart_reg_out(UART3_EBLR, 2);
699
700                 divisor = 48000000 / (41 * speed);      /* Base clock 48 MHz */
701
702                 uart_reg_out(UART3_LCR, UART3_LCR_DIVEN);
703                 uart_reg_out(UART3_DLL, (divisor & 0xff));
704                 uart_reg_out(UART3_DLH, (divisor >> 8));
705                 uart_reg_out(UART3_LCR, 0x03);
706
707                 if (omap_ir->pdata->transceiver_mode)
708                         omap_ir->pdata->transceiver_mode(omap_ir->dev,
709                                                         IR_MIRMODE);
710
711                 local_irq_restore(flags);
712         } else {
713                 local_irq_save(flags);
714
715                 /* FIR mode */
716                 uart_reg_out(UART3_MDR1, UART3_MDR1_FIR |
717                                 UART3_MDR1_SIP_AUTO);
718
719                 if (omap_ir->pdata->transceiver_mode)
720                         omap_ir->pdata->transceiver_mode(omap_ir->dev,
721                                                         IR_FIRMODE);
722
723                 local_irq_restore(flags);
724         }
725
726         omap_ir->speed = speed;
727
728         return 0;
729 }
730
731 #ifdef CONFIG_PM
732 /*
733  * Suspend the IrDA interface.
734  */
735 static int omap_irda_suspend(struct platform_device *pdev, pm_message_t state)
736 {
737         struct net_device *dev = platform_get_drvdata(pdev);
738         struct omap_irda *omap_ir = netdev_priv(dev);
739
740         if (!dev)
741                 return 0;
742
743         if (omap_ir->open) {
744                 /*
745                  * Stop the transmit queue
746                  */
747                 netif_device_detach(dev);
748                 disable_irq(dev->irq);
749                 omap_irda_shutdown(omap_ir);
750         }
751         return 0;
752 }
753
754 /*
755  * Resume the IrDA interface.
756  */
757 static int omap_irda_resume(struct platform_device *pdev)
758 {
759         struct net_device *dev = platform_get_drvdata(pdev);
760         struct omap_irda *omap_ir= netdev_priv(dev);
761
762         if (!dev)
763                 return 0;
764
765         if (omap_ir->open) {
766                 /*
767                  * If we missed a speed change, initialise at the new speed
768                  * directly.  It is debatable whether this is actually
769                  * required, but in the interests of continuing from where
770                  * we left off it is desireable.  The converse argument is
771                  * that we should re-negotiate at 9600 baud again.
772                  */
773                 if (omap_ir->newspeed) {
774                         omap_ir->speed = omap_ir->newspeed;
775                         omap_ir->newspeed = 0;
776                 }
777
778                 omap_irda_startup(dev);
779                 omap_irda_set_speed(dev, omap_ir->speed);
780                 enable_irq(dev->irq);
781
782                 /*
783                  * This automatically wakes up the queue
784                  */
785                 netif_device_attach(dev);
786         }
787
788         return 0;
789 }
790 #else
791 #define omap_irda_suspend       NULL
792 #define omap_irda_resume        NULL
793 #endif
794
795 static int omap_irda_probe(struct platform_device *pdev)
796 {
797         struct net_device *dev;
798         struct omap_irda *omap_ir;
799         struct omap_irda_config *pdata = pdev->dev.platform_data;
800         unsigned int baudrate_mask;
801         int err = 0;
802         int irq = NO_IRQ;
803
804         if (!pdata) {
805                 printk(KERN_ERR "IrDA Platform data not supplied\n");
806                 return -ENOENT;
807         }
808
809         if (!pdata->rx_channel || !pdata->tx_channel) {
810                 printk(KERN_ERR "IrDA invalid rx/tx channel value\n");
811                 return -ENOENT;
812         }
813
814         irq = platform_get_irq(pdev, 0);
815         if (irq <= 0) {
816                 printk(KERN_WARNING "no irq for IrDA\n");
817                 return -ENOENT;
818         }
819
820         dev = alloc_irdadev(sizeof(struct omap_irda));
821         if (!dev)
822                 goto err_mem_1;
823
824
825         omap_ir = netdev_priv(dev);
826         omap_ir->dev = &pdev->dev;
827         omap_ir->pdata = pdata;
828
829         dev->hard_start_xmit    = omap_irda_hard_xmit;
830         dev->open               = omap_irda_start;
831         dev->stop               = omap_irda_stop;
832         dev->do_ioctl           = omap_irda_ioctl;
833         dev->get_stats          = omap_irda_stats;
834         dev->irq                = irq;
835
836         irda_init_max_qos_capabilies(&omap_ir->qos);
837
838         baudrate_mask = 0;
839         if (omap_ir->pdata->transceiver_cap & IR_SIRMODE)
840                 baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
841         if (omap_ir->pdata->transceiver_cap & IR_MIRMODE)
842                 baudrate_mask |= IR_57600 | IR_1152000;
843         if (omap_ir->pdata->transceiver_cap & IR_FIRMODE)
844                 baudrate_mask |= IR_4000000 << 8;
845
846         omap_ir->qos.baud_rate.bits &= baudrate_mask;
847         omap_ir->qos.min_turn_time.bits = 7;
848
849         irda_qos_bits_to_value(&omap_ir->qos);
850
851         /* Any better way to avoid this? No. */
852         if (machine_is_omap_h3() || machine_is_omap_h4())
853                 INIT_DELAYED_WORK(&omap_ir->pdata->gpio_expa, NULL);
854
855         err = register_netdev(dev);
856         if (!err)
857                 platform_set_drvdata(pdev, dev);
858         else
859                 free_netdev(dev);
860
861 err_mem_1:
862         return err;
863 }
864
865 static int omap_irda_remove(struct platform_device *pdev)
866 {
867         struct net_device *dev = platform_get_drvdata(pdev);
868
869         if (pdev) {
870                 unregister_netdev(dev);
871                 free_netdev(dev);
872         }
873         return 0;
874 }
875
876 static struct platform_driver omapir_driver = {
877         .probe          = omap_irda_probe,
878         .remove         = omap_irda_remove,
879         .suspend        = omap_irda_suspend,
880         .resume         = omap_irda_resume,
881         .driver         = {
882                 .name   = "omapirda",
883         },
884 };
885
886 static char __initdata banner[] = KERN_INFO "OMAP IrDA driver initializing\n";
887
888 static int __init omap_irda_init(void)
889 {
890         printk(banner);
891         return platform_driver_register(&omapir_driver);
892 }
893
894 static void __exit omap_irda_exit(void)
895 {
896         platform_driver_unregister(&omapir_driver);
897 }
898
899 module_init(omap_irda_init);
900 module_exit(omap_irda_exit);
901
902 MODULE_AUTHOR("MontaVista");
903 MODULE_DESCRIPTION("OMAP IrDA Driver");
904 MODULE_LICENSE("GPL");
905