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