blob: 8239c520f1ef2af494ddf42a2d78e31fc82a5358 [file] [log] [blame]
Nicolas Pitre6f475c02005-10-28 16:39:33 +01001/*
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 */
Nicolas Pitre6f475c02005-10-28 16:39:33 +010015#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>
Russell Kingd052d1b2005-10-29 19:07:23 +010024#include <linux/platform_device.h>
Nicolas Pitre6f475c02005-10-28 16:39:33 +010025#include <linux/pm.h>
Russell King82d553c2007-09-02 17:09:23 +010026#include <linux/clk.h>
Nicolas Pitre6f475c02005-10-28 16:39:33 +010027
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>
eric miaoa683b142008-03-03 09:44:25 +080039#include <asm/arch/pxa2xx-gpio.h>
Nicolas Pitre6f475c02005-10-28 16:39:33 +010040
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
70struct 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;
Russell King82d553c2007-09-02 17:09:23 +010092 struct clk *fir_clk;
93 struct clk *sir_clk;
94 struct clk *cur_clk;
Nicolas Pitre6f475c02005-10-28 16:39:33 +010095};
96
Russell King82d553c2007-09-02 17:09:23 +010097static 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
104static 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
110static 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
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100116
117#define IS_FIR(si) ((si)->speed >= 4000000)
118#define IRDA_FRAME_SIZE_LIMIT 2047
119
120inline 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
129inline 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 */
141static 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;
Russell King82d553c2007-09-02 17:09:23 +0100161 pxa_irda_disable_clk(si);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100162
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 */
Russell King82d553c2007-09-02 17:09:23 +0100171 pxa_irda_enable_sirclk(si);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100172 }
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;
Russell King82d553c2007-09-02 17:09:23 +0100196 pxa_irda_disable_clk(si);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100197
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 */
Russell King82d553c2007-09-02 17:09:23 +0100209 pxa_irda_enable_firclk(si);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100210
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. */
David Howells7d12e782006-10-05 14:55:46 +0100226static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100227{
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 */
David Howells7d12e782006-10-05 14:55:46 +0100308static void pxa_irda_fir_dma_rx_irq(int channel, void *data)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100309{
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 */
David Howells7d12e782006-10-05 14:55:46 +0100318static void pxa_irda_fir_dma_tx_irq(int channel, void *data)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100319{
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 {
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100348 int i = 64;
349
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100350 ICCR0 = 0;
351 pxa_irda_fir_dma_rx_start(si);
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100352 while ((ICSR1 & ICSR1_RNE) && i--)
353 (void)ICDR;
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100354 ICCR0 = ICCR0_ITR | ICCR0_RXE;
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100355
356 if (i < 0)
357 printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100358 }
359 netif_wake_queue(dev);
360}
361
362/* EIF(Error in FIFO/End in Frame) handler for FIR */
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100363static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev, int icsr0)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100364{
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");
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100384 si->stats.rx_over_errors++;
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100385 }
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. */
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100396 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);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100405 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);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300413 skb_copy_to_linear_data(skb, si->dma_rx_buff, len);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100414 skb_put(skb, len);
415
416 /* Feed it to IrLAP */
417 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700418 skb_reset_mac_header(skb);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100419 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 */
David Howells7d12e782006-10-05 14:55:46 +0100430static irqreturn_t pxa_irda_fir_irq(int irq, void *dev_id)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100431{
432 struct net_device *dev = dev_id;
433 struct pxa_irda *si = netdev_priv(dev);
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100434 int icsr0, i = 64;
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100435
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 */
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100454 pxa_irda_fir_irq_eif(si, dev, icsr0);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100455 }
456
457 ICCR0 = 0;
458 pxa_irda_fir_dma_rx_start(si);
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100459 while ((ICSR1 & ICSR1_RNE) && i--)
460 (void)ICDR;
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100461 ICCR0 = ICCR0_ITR | ICCR0_RXE;
462
Guennadi Liakhovetski9a4d93d2007-03-30 08:49:55 +0100463 if (i < 0)
464 printk(KERN_ERR "pxa_ir: cannot clear Rx FIFO!\n");
465
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100466 return IRQ_HANDLED;
467}
468
469/* hard_xmit interface of irda device */
470static 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;
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300511 skb_copy_from_linear_data(skb, si->dma_tx_buff, skb->len);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100512
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
530static 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
576static 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
582static 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
609static 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;
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100619
620 /* disable DMA */
621 DCSR(si->txdma) &= ~DCSR_RUN;
622 DCSR(si->rxdma) &= ~DCSR_RUN;
623 /* disable FICP */
624 ICCR0 = 0;
Russell King82d553c2007-09-02 17:09:23 +0100625
626 /* disable the STUART or FICP clocks */
627 pxa_irda_disable_clk(si);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100628
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
640static 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
703err_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);
706err_dma_tx_buff:
707 dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy);
708err_dma_rx_buff:
709 pxa_free_dma(si->txdma);
710err_tx_dma:
711 pxa_free_dma(si->rxdma);
712err_rx_dma:
713 free_irq(IRQ_ICP, dev);
714err_irq2:
715 free_irq(IRQ_STUART, dev);
716err_irq1:
717
718 return err;
719}
720
721static 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
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800750static int pxa_irda_suspend(struct platform_device *_dev, pm_message_t state)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100751{
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800752 struct net_device *dev = platform_get_drvdata(_dev);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100753 struct pxa_irda *si;
754
Richard Purdie91e1a512005-10-30 14:38:52 +0000755 if (dev && netif_running(dev)) {
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100756 si = netdev_priv(dev);
757 netif_device_detach(dev);
758 pxa_irda_shutdown(si);
759 }
760
761 return 0;
762}
763
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800764static int pxa_irda_resume(struct platform_device *_dev)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100765{
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800766 struct net_device *dev = platform_get_drvdata(_dev);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100767 struct pxa_irda *si;
768
Richard Purdie91e1a512005-10-30 14:38:52 +0000769 if (dev && netif_running(dev)) {
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100770 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
780static 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
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800792static int pxa_irda_probe(struct platform_device *pdev)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100793{
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100794 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
Russell King82d553c2007-09-02 17:09:23 +0100818 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
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100825 /*
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 dev->hard_start_xmit = pxa_irda_hard_xmit;
836 dev->open = pxa_irda_start;
837 dev->stop = pxa_irda_stop;
838 dev->do_ioctl = pxa_irda_ioctl;
839 dev->get_stats = pxa_irda_stats;
840
841 irda_init_max_qos_capabilies(&si->qos);
842
843 baudrate_mask = 0;
844 if (si->pdata->transceiver_cap & IR_SIRMODE)
845 baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200;
846 if (si->pdata->transceiver_cap & IR_FIRMODE)
847 baudrate_mask |= IR_4000000 << 8;
848
849 si->qos.baud_rate.bits &= baudrate_mask;
850 si->qos.min_turn_time.bits = 7; /* 1ms or more */
851
852 irda_qos_bits_to_value(&si->qos);
853
854 err = register_netdev(dev);
855
856 if (err == 0)
857 dev_set_drvdata(&pdev->dev, dev);
858
859 if (err) {
860 kfree(si->tx_buff.head);
861err_mem_5:
862 kfree(si->rx_buff.head);
863err_mem_4:
Russell King82d553c2007-09-02 17:09:23 +0100864 if (si->sir_clk && !IS_ERR(si->sir_clk))
865 clk_put(si->sir_clk);
866 if (si->fir_clk && !IS_ERR(si->fir_clk))
867 clk_put(si->fir_clk);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100868 free_netdev(dev);
869err_mem_3:
870 release_mem_region(__PREG(FICP), 0x1c);
871err_mem_2:
872 release_mem_region(__PREG(STUART), 0x24);
873 }
874err_mem_1:
875 return err;
876}
877
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800878static int pxa_irda_remove(struct platform_device *_dev)
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100879{
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800880 struct net_device *dev = platform_get_drvdata(_dev);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100881
882 if (dev) {
883 struct pxa_irda *si = netdev_priv(dev);
884 unregister_netdev(dev);
885 kfree(si->tx_buff.head);
886 kfree(si->rx_buff.head);
Russell King82d553c2007-09-02 17:09:23 +0100887 clk_put(si->fir_clk);
888 clk_put(si->sir_clk);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100889 free_netdev(dev);
890 }
891
892 release_mem_region(__PREG(STUART), 0x24);
893 release_mem_region(__PREG(FICP), 0x1c);
894
895 return 0;
896}
897
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800898static struct platform_driver pxa_ir_driver = {
899 .driver = {
900 .name = "pxa2xx-ir",
901 },
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100902 .probe = pxa_irda_probe,
903 .remove = pxa_irda_remove,
904 .suspend = pxa_irda_suspend,
905 .resume = pxa_irda_resume,
906};
907
908static int __init pxa_irda_init(void)
909{
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800910 return platform_driver_register(&pxa_ir_driver);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100911}
912
913static void __exit pxa_irda_exit(void)
914{
Paul Sokolovskyb259e7d2006-12-06 20:07:59 -0800915 platform_driver_unregister(&pxa_ir_driver);
Nicolas Pitre6f475c02005-10-28 16:39:33 +0100916}
917
918module_init(pxa_irda_init);
919module_exit(pxa_irda_exit);
920
921MODULE_LICENSE("GPL");