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