diff options
Diffstat (limited to 'drivers/net/irda/pxaficp_ir.c')
-rw-r--r-- | drivers/net/irda/pxaficp_ir.c | 866 |
1 files changed, 866 insertions, 0 deletions
diff --git a/drivers/net/irda/pxaficp_ir.c b/drivers/net/irda/pxaficp_ir.c new file mode 100644 index 000000000000..e1aa9910503b --- /dev/null +++ b/drivers/net/irda/pxaficp_ir.c | |||
@@ -0,0 +1,866 @@ | |||
1 | /* | ||
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 | */ | ||
15 | #include <linux/config.h> | ||
16 | #include <linux/module.h> | ||
17 | #include <linux/types.h> | ||
18 | #include <linux/init.h> | ||
19 | #include <linux/errno.h> | ||
20 | #include <linux/netdevice.h> | ||
21 | #include <linux/slab.h> | ||
22 | #include <linux/rtnetlink.h> | ||
23 | #include <linux/interrupt.h> | ||
24 | #include <linux/dma-mapping.h> | ||
25 | #include <linux/platform_device.h> | ||
26 | #include <linux/pm.h> | ||
27 | |||
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> | ||
39 | |||
40 | #ifdef CONFIG_MACH_MAINSTONE | ||
41 | #include <asm/arch/mainstone.h> | ||
42 | #endif | ||
43 | |||
44 | #define IrSR_RXPL_NEG_IS_ZERO (1<<4) | ||
45 | #define IrSR_RXPL_POS_IS_ZERO 0x0 | ||
46 | #define IrSR_TXPL_NEG_IS_ZERO (1<<3) | ||
47 | #define IrSR_TXPL_POS_IS_ZERO 0x0 | ||
48 | #define IrSR_XMODE_PULSE_1_6 (1<<2) | ||
49 | #define IrSR_XMODE_PULSE_3_16 0x0 | ||
50 | #define IrSR_RCVEIR_IR_MODE (1<<1) | ||
51 | #define IrSR_RCVEIR_UART_MODE 0x0 | ||
52 | #define IrSR_XMITIR_IR_MODE (1<<0) | ||
53 | #define IrSR_XMITIR_UART_MODE 0x0 | ||
54 | |||
55 | #define IrSR_IR_RECEIVE_ON (\ | ||
56 | IrSR_RXPL_NEG_IS_ZERO | \ | ||
57 | IrSR_TXPL_POS_IS_ZERO | \ | ||
58 | IrSR_XMODE_PULSE_3_16 | \ | ||
59 | IrSR_RCVEIR_IR_MODE | \ | ||
60 | IrSR_XMITIR_UART_MODE) | ||
61 | |||
62 | #define IrSR_IR_TRANSMIT_ON (\ | ||
63 | IrSR_RXPL_NEG_IS_ZERO | \ | ||
64 | IrSR_TXPL_POS_IS_ZERO | \ | ||
65 | IrSR_XMODE_PULSE_3_16 | \ | ||
66 | IrSR_RCVEIR_UART_MODE | \ | ||
67 | IrSR_XMITIR_IR_MODE) | ||
68 | |||
69 | struct pxa_irda { | ||
70 | int speed; | ||
71 | int newspeed; | ||
72 | unsigned long last_oscr; | ||
73 | |||
74 | unsigned char *dma_rx_buff; | ||
75 | unsigned char *dma_tx_buff; | ||
76 | dma_addr_t dma_rx_buff_phy; | ||
77 | dma_addr_t dma_tx_buff_phy; | ||
78 | unsigned int dma_tx_buff_len; | ||
79 | int txdma; | ||
80 | int rxdma; | ||
81 | |||
82 | struct net_device_stats stats; | ||
83 | struct irlap_cb *irlap; | ||
84 | struct qos_info qos; | ||
85 | |||
86 | iobuff_t tx_buff; | ||
87 | iobuff_t rx_buff; | ||
88 | |||
89 | struct device *dev; | ||
90 | struct pxaficp_platform_data *pdata; | ||
91 | }; | ||
92 | |||
93 | |||
94 | #define IS_FIR(si) ((si)->speed >= 4000000) | ||
95 | #define IRDA_FRAME_SIZE_LIMIT 2047 | ||
96 | |||
97 | inline static void pxa_irda_fir_dma_rx_start(struct pxa_irda *si) | ||
98 | { | ||
99 | DCSR(si->rxdma) = DCSR_NODESC; | ||
100 | DSADR(si->rxdma) = __PREG(ICDR); | ||
101 | DTADR(si->rxdma) = si->dma_rx_buff_phy; | ||
102 | DCMD(si->rxdma) = DCMD_INCTRGADDR | DCMD_FLOWSRC | DCMD_WIDTH1 | DCMD_BURST32 | IRDA_FRAME_SIZE_LIMIT; | ||
103 | DCSR(si->rxdma) |= DCSR_RUN; | ||
104 | } | ||
105 | |||
106 | inline static void pxa_irda_fir_dma_tx_start(struct pxa_irda *si) | ||
107 | { | ||
108 | DCSR(si->txdma) = DCSR_NODESC; | ||
109 | DSADR(si->txdma) = si->dma_tx_buff_phy; | ||
110 | DTADR(si->txdma) = __PREG(ICDR); | ||
111 | DCMD(si->txdma) = DCMD_INCSRCADDR | DCMD_FLOWTRG | DCMD_ENDIRQEN | DCMD_WIDTH1 | DCMD_BURST32 | si->dma_tx_buff_len; | ||
112 | DCSR(si->txdma) |= DCSR_RUN; | ||
113 | } | ||
114 | |||
115 | /* | ||
116 | * Set the IrDA communications speed. | ||
117 | */ | ||
118 | static int pxa_irda_set_speed(struct pxa_irda *si, int speed) | ||
119 | { | ||
120 | unsigned long flags; | ||
121 | unsigned int divisor; | ||
122 | |||
123 | switch (speed) { | ||
124 | case 9600: case 19200: case 38400: | ||
125 | case 57600: case 115200: | ||
126 | |||
127 | /* refer to PXA250/210 Developer's Manual 10-7 */ | ||
128 | /* BaudRate = 14.7456 MHz / (16*Divisor) */ | ||
129 | divisor = 14745600 / (16 * speed); | ||
130 | |||
131 | local_irq_save(flags); | ||
132 | |||
133 | if (IS_FIR(si)) { | ||
134 | /* stop RX DMA */ | ||
135 | DCSR(si->rxdma) &= ~DCSR_RUN; | ||
136 | /* disable FICP */ | ||
137 | ICCR0 = 0; | ||
138 | pxa_set_cken(CKEN13_FICP, 0); | ||
139 | |||
140 | /* set board transceiver to SIR mode */ | ||
141 | si->pdata->transceiver_mode(si->dev, IR_SIRMODE); | ||
142 | |||
143 | /* configure GPIO46/47 */ | ||
144 | pxa_gpio_mode(GPIO46_STRXD_MD); | ||
145 | pxa_gpio_mode(GPIO47_STTXD_MD); | ||
146 | |||
147 | /* enable the STUART clock */ | ||
148 | pxa_set_cken(CKEN5_STUART, 1); | ||
149 | } | ||
150 | |||
151 | /* disable STUART first */ | ||
152 | STIER = 0; | ||
153 | |||
154 | /* access DLL & DLH */ | ||
155 | STLCR |= LCR_DLAB; | ||
156 | STDLL = divisor & 0xff; | ||
157 | STDLH = divisor >> 8; | ||
158 | STLCR &= ~LCR_DLAB; | ||
159 | |||
160 | si->speed = speed; | ||
161 | STISR = IrSR_IR_RECEIVE_ON | IrSR_XMODE_PULSE_1_6; | ||
162 | STIER = IER_UUE | IER_RLSE | IER_RAVIE | IER_RTIOE; | ||
163 | |||
164 | local_irq_restore(flags); | ||
165 | break; | ||
166 | |||
167 | case 4000000: | ||
168 | local_irq_save(flags); | ||
169 | |||
170 | /* disable STUART */ | ||
171 | STIER = 0; | ||
172 | STISR = 0; | ||
173 | pxa_set_cken(CKEN5_STUART, 0); | ||
174 | |||
175 | /* disable FICP first */ | ||
176 | ICCR0 = 0; | ||
177 | |||
178 | /* set board transceiver to FIR mode */ | ||
179 | si->pdata->transceiver_mode(si->dev, IR_FIRMODE); | ||
180 | |||
181 | /* configure GPIO46/47 */ | ||
182 | pxa_gpio_mode(GPIO46_ICPRXD_MD); | ||
183 | pxa_gpio_mode(GPIO47_ICPTXD_MD); | ||
184 | |||
185 | /* enable the FICP clock */ | ||
186 | pxa_set_cken(CKEN13_FICP, 1); | ||
187 | |||
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. */ | ||
203 | static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id, struct pt_regs *regs) | ||
204 | { | ||
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 */ | ||
285 | static void pxa_irda_fir_dma_rx_irq(int channel, void *data, struct pt_regs *regs) | ||
286 | { | ||
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 */ | ||
295 | static void pxa_irda_fir_dma_tx_irq(int channel, void *data, struct pt_regs *regs) | ||
296 | { | ||
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 { | ||
325 | ICCR0 = 0; | ||
326 | pxa_irda_fir_dma_rx_start(si); | ||
327 | ICCR0 = ICCR0_ITR | ICCR0_RXE; | ||
328 | } | ||
329 | netif_wake_queue(dev); | ||
330 | } | ||
331 | |||
332 | /* EIF(Error in FIFO/End in Frame) handler for FIR */ | ||
333 | static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev) | ||
334 | { | ||
335 | unsigned int len, stat, data; | ||
336 | |||
337 | /* Get the current data position. */ | ||
338 | len = DTADR(si->rxdma) - si->dma_rx_buff_phy; | ||
339 | |||
340 | do { | ||
341 | /* Read Status, and then Data. */ | ||
342 | stat = ICSR1; | ||
343 | rmb(); | ||
344 | data = ICDR; | ||
345 | |||
346 | if (stat & (ICSR1_CRE | ICSR1_ROR)) { | ||
347 | si->stats.rx_errors++; | ||
348 | if (stat & ICSR1_CRE) { | ||
349 | printk(KERN_DEBUG "pxa_ir: fir receive CRC error\n"); | ||
350 | si->stats.rx_crc_errors++; | ||
351 | } | ||
352 | if (stat & ICSR1_ROR) { | ||
353 | printk(KERN_DEBUG "pxa_ir: fir receive overrun\n"); | ||
354 | si->stats.rx_frame_errors++; | ||
355 | } | ||
356 | } else { | ||
357 | si->dma_rx_buff[len++] = data; | ||
358 | } | ||
359 | /* If we hit the end of frame, there's no point in continuing. */ | ||
360 | if (stat & ICSR1_EOF) | ||
361 | break; | ||
362 | } while (ICSR0 & ICSR0_EIF); | ||
363 | |||
364 | if (stat & ICSR1_EOF) { | ||
365 | /* end of frame. */ | ||
366 | struct sk_buff *skb = alloc_skb(len+1,GFP_ATOMIC); | ||
367 | if (!skb) { | ||
368 | printk(KERN_ERR "pxa_ir: fir out of memory for receive skb\n"); | ||
369 | si->stats.rx_dropped++; | ||
370 | return; | ||
371 | } | ||
372 | |||
373 | /* Align IP header to 20 bytes */ | ||
374 | skb_reserve(skb, 1); | ||
375 | memcpy(skb->data, si->dma_rx_buff, len); | ||
376 | skb_put(skb, len); | ||
377 | |||
378 | /* Feed it to IrLAP */ | ||
379 | skb->dev = dev; | ||
380 | skb->mac.raw = skb->data; | ||
381 | skb->protocol = htons(ETH_P_IRDA); | ||
382 | netif_rx(skb); | ||
383 | |||
384 | si->stats.rx_packets++; | ||
385 | si->stats.rx_bytes += len; | ||
386 | |||
387 | dev->last_rx = jiffies; | ||
388 | } | ||
389 | } | ||
390 | |||
391 | /* FIR interrupt handler */ | ||
392 | static irqreturn_t pxa_irda_fir_irq(int irq, void *dev_id, struct pt_regs *regs) | ||
393 | { | ||
394 | struct net_device *dev = dev_id; | ||
395 | struct pxa_irda *si = netdev_priv(dev); | ||
396 | int icsr0; | ||
397 | |||
398 | /* stop RX DMA */ | ||
399 | DCSR(si->rxdma) &= ~DCSR_RUN; | ||
400 | si->last_oscr = OSCR; | ||
401 | icsr0 = ICSR0; | ||
402 | |||
403 | if (icsr0 & (ICSR0_FRE | ICSR0_RAB)) { | ||
404 | if (icsr0 & ICSR0_FRE) { | ||
405 | printk(KERN_DEBUG "pxa_ir: fir receive frame error\n"); | ||
406 | si->stats.rx_frame_errors++; | ||
407 | } else { | ||
408 | printk(KERN_DEBUG "pxa_ir: fir receive abort\n"); | ||
409 | si->stats.rx_errors++; | ||
410 | } | ||
411 | ICSR0 = icsr0 & (ICSR0_FRE | ICSR0_RAB); | ||
412 | } | ||
413 | |||
414 | if (icsr0 & ICSR0_EIF) { | ||
415 | /* An error in FIFO occured, or there is a end of frame */ | ||
416 | pxa_irda_fir_irq_eif(si, dev); | ||
417 | } | ||
418 | |||
419 | ICCR0 = 0; | ||
420 | pxa_irda_fir_dma_rx_start(si); | ||
421 | ICCR0 = ICCR0_ITR | ICCR0_RXE; | ||
422 | |||
423 | return IRQ_HANDLED; | ||
424 | } | ||
425 | |||
426 | /* hard_xmit interface of irda device */ | ||
427 | static int pxa_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev) | ||
428 | { | ||
429 | struct pxa_irda *si = netdev_priv(dev); | ||
430 | int speed = irda_get_next_speed(skb); | ||
431 | |||
432 | /* | ||
433 | * Does this packet contain a request to change the interface | ||
434 | * speed? If so, remember it until we complete the transmission | ||
435 | * of this frame. | ||
436 | */ | ||
437 | if (speed != si->speed && speed != -1) | ||
438 | si->newspeed = speed; | ||
439 | |||
440 | /* | ||
441 | * If this is an empty frame, we can bypass a lot. | ||
442 | */ | ||
443 | if (skb->len == 0) { | ||
444 | if (si->newspeed) { | ||
445 | si->newspeed = 0; | ||
446 | pxa_irda_set_speed(si, speed); | ||
447 | } | ||
448 | dev_kfree_skb(skb); | ||
449 | return 0; | ||
450 | } | ||
451 | |||
452 | netif_stop_queue(dev); | ||
453 | |||
454 | if (!IS_FIR(si)) { | ||
455 | si->tx_buff.data = si->tx_buff.head; | ||
456 | si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data, si->tx_buff.truesize); | ||
457 | |||
458 | /* Disable STUART interrupts and switch to transmit mode. */ | ||
459 | STIER = 0; | ||
460 | STISR = IrSR_IR_TRANSMIT_ON | IrSR_XMODE_PULSE_1_6; | ||
461 | |||
462 | /* enable STUART and transmit interrupts */ | ||
463 | STIER = IER_UUE | IER_TIE; | ||
464 | } else { | ||
465 | unsigned long mtt = irda_get_mtt(skb); | ||
466 | |||
467 | si->dma_tx_buff_len = skb->len; | ||
468 | memcpy(si->dma_tx_buff, skb->data, skb->len); | ||
469 | |||
470 | if (mtt) | ||
471 | while ((unsigned)(OSCR - si->last_oscr)/4 < mtt) | ||
472 | cpu_relax(); | ||
473 | |||
474 | /* stop RX DMA, disable FICP */ | ||
475 | DCSR(si->rxdma) &= ~DCSR_RUN; | ||
476 | ICCR0 = 0; | ||
477 | |||
478 | pxa_irda_fir_dma_tx_start(si); | ||
479 | ICCR0 = ICCR0_ITR | ICCR0_TXE; | ||
480 | } | ||
481 | |||
482 | dev_kfree_skb(skb); | ||
483 | dev->trans_start = jiffies; | ||
484 | return 0; | ||
485 | } | ||
486 | |||
487 | static int pxa_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd) | ||
488 | { | ||
489 | struct if_irda_req *rq = (struct if_irda_req *)ifreq; | ||
490 | struct pxa_irda *si = netdev_priv(dev); | ||
491 | int ret; | ||
492 | |||
493 | switch (cmd) { | ||
494 | case SIOCSBANDWIDTH: | ||
495 | ret = -EPERM; | ||
496 | if (capable(CAP_NET_ADMIN)) { | ||
497 | /* | ||
498 | * We are unable to set the speed if the | ||
499 | * device is not running. | ||
500 | */ | ||
501 | if (netif_running(dev)) { | ||
502 | ret = pxa_irda_set_speed(si, | ||
503 | rq->ifr_baudrate); | ||
504 | } else { | ||
505 | printk(KERN_INFO "pxa_ir: SIOCSBANDWIDTH: !netif_running\n"); | ||
506 | ret = 0; | ||
507 | } | ||
508 | } | ||
509 | break; | ||
510 | |||
511 | case SIOCSMEDIABUSY: | ||
512 | ret = -EPERM; | ||
513 | if (capable(CAP_NET_ADMIN)) { | ||
514 | irda_device_set_media_busy(dev, TRUE); | ||
515 | ret = 0; | ||
516 | } | ||
517 | break; | ||
518 | |||
519 | case SIOCGRECEIVING: | ||
520 | ret = 0; | ||
521 | rq->ifr_receiving = IS_FIR(si) ? 0 | ||
522 | : si->rx_buff.state != OUTSIDE_FRAME; | ||
523 | break; | ||
524 | |||
525 | default: | ||
526 | ret = -EOPNOTSUPP; | ||
527 | break; | ||
528 | } | ||
529 | |||
530 | return ret; | ||
531 | } | ||
532 | |||
533 | static struct net_device_stats *pxa_irda_stats(struct net_device *dev) | ||
534 | { | ||
535 | struct pxa_irda *si = netdev_priv(dev); | ||
536 | return &si->stats; | ||
537 | } | ||
538 | |||
539 | static void pxa_irda_startup(struct pxa_irda *si) | ||
540 | { | ||
541 | /* Disable STUART interrupts */ | ||
542 | STIER = 0; | ||
543 | /* enable STUART interrupt to the processor */ | ||
544 | STMCR = MCR_OUT2; | ||
545 | /* configure SIR frame format: StartBit - Data 7 ... Data 0 - Stop Bit */ | ||
546 | STLCR = LCR_WLS0 | LCR_WLS1; | ||
547 | /* enable FIFO, we use FIFO to improve performance */ | ||
548 | STFCR = FCR_TRFIFOE | FCR_ITL_32; | ||
549 | |||
550 | /* disable FICP */ | ||
551 | ICCR0 = 0; | ||
552 | /* configure FICP ICCR2 */ | ||
553 | ICCR2 = ICCR2_TXP | ICCR2_TRIG_32; | ||
554 | |||
555 | /* configure DMAC */ | ||
556 | DRCMR17 = si->rxdma | DRCMR_MAPVLD; | ||
557 | DRCMR18 = si->txdma | DRCMR_MAPVLD; | ||
558 | |||
559 | /* force SIR reinitialization */ | ||
560 | si->speed = 4000000; | ||
561 | pxa_irda_set_speed(si, 9600); | ||
562 | |||
563 | printk(KERN_DEBUG "pxa_ir: irda startup\n"); | ||
564 | } | ||
565 | |||
566 | static void pxa_irda_shutdown(struct pxa_irda *si) | ||
567 | { | ||
568 | unsigned long flags; | ||
569 | |||
570 | local_irq_save(flags); | ||
571 | |||
572 | /* disable STUART and interrupt */ | ||
573 | STIER = 0; | ||
574 | /* disable STUART SIR mode */ | ||
575 | STISR = 0; | ||
576 | /* disable the STUART clock */ | ||
577 | pxa_set_cken(CKEN5_STUART, 0); | ||
578 | |||
579 | /* disable DMA */ | ||
580 | DCSR(si->txdma) &= ~DCSR_RUN; | ||
581 | DCSR(si->rxdma) &= ~DCSR_RUN; | ||
582 | /* disable FICP */ | ||
583 | ICCR0 = 0; | ||
584 | /* disable the FICP clock */ | ||
585 | pxa_set_cken(CKEN13_FICP, 0); | ||
586 | |||
587 | DRCMR17 = 0; | ||
588 | DRCMR18 = 0; | ||
589 | |||
590 | local_irq_restore(flags); | ||
591 | |||
592 | /* power off board transceiver */ | ||
593 | si->pdata->transceiver_mode(si->dev, IR_OFF); | ||
594 | |||
595 | printk(KERN_DEBUG "pxa_ir: irda shutdown\n"); | ||
596 | } | ||
597 | |||
598 | static int pxa_irda_start(struct net_device *dev) | ||
599 | { | ||
600 | struct pxa_irda *si = netdev_priv(dev); | ||
601 | int err; | ||
602 | |||
603 | si->speed = 9600; | ||
604 | |||
605 | err = request_irq(IRQ_STUART, pxa_irda_sir_irq, 0, dev->name, dev); | ||
606 | if (err) | ||
607 | goto err_irq1; | ||
608 | |||
609 | err = request_irq(IRQ_ICP, pxa_irda_fir_irq, 0, dev->name, dev); | ||
610 | if (err) | ||
611 | goto err_irq2; | ||
612 | |||
613 | /* | ||
614 | * The interrupt must remain disabled for now. | ||
615 | */ | ||
616 | disable_irq(IRQ_STUART); | ||
617 | disable_irq(IRQ_ICP); | ||
618 | |||
619 | err = -EBUSY; | ||
620 | si->rxdma = pxa_request_dma("FICP_RX",DMA_PRIO_LOW, pxa_irda_fir_dma_rx_irq, dev); | ||
621 | if (si->rxdma < 0) | ||
622 | goto err_rx_dma; | ||
623 | |||
624 | si->txdma = pxa_request_dma("FICP_TX",DMA_PRIO_LOW, pxa_irda_fir_dma_tx_irq, dev); | ||
625 | if (si->txdma < 0) | ||
626 | goto err_tx_dma; | ||
627 | |||
628 | err = -ENOMEM; | ||
629 | si->dma_rx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, | ||
630 | &si->dma_rx_buff_phy, GFP_KERNEL ); | ||
631 | if (!si->dma_rx_buff) | ||
632 | goto err_dma_rx_buff; | ||
633 | |||
634 | si->dma_tx_buff = dma_alloc_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, | ||
635 | &si->dma_tx_buff_phy, GFP_KERNEL ); | ||
636 | if (!si->dma_tx_buff) | ||
637 | goto err_dma_tx_buff; | ||
638 | |||
639 | /* Setup the serial port for the initial speed. */ | ||
640 | pxa_irda_startup(si); | ||
641 | |||
642 | /* | ||
643 | * Open a new IrLAP layer instance. | ||
644 | */ | ||
645 | si->irlap = irlap_open(dev, &si->qos, "pxa"); | ||
646 | err = -ENOMEM; | ||
647 | if (!si->irlap) | ||
648 | goto err_irlap; | ||
649 | |||
650 | /* | ||
651 | * Now enable the interrupt and start the queue | ||
652 | */ | ||
653 | enable_irq(IRQ_STUART); | ||
654 | enable_irq(IRQ_ICP); | ||
655 | netif_start_queue(dev); | ||
656 | |||
657 | printk(KERN_DEBUG "pxa_ir: irda driver opened\n"); | ||
658 | |||
659 | return 0; | ||
660 | |||
661 | err_irlap: | ||
662 | pxa_irda_shutdown(si); | ||
663 | dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy); | ||
664 | err_dma_tx_buff: | ||
665 | dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy); | ||
666 | err_dma_rx_buff: | ||
667 | pxa_free_dma(si->txdma); | ||
668 | err_tx_dma: | ||
669 | pxa_free_dma(si->rxdma); | ||
670 | err_rx_dma: | ||
671 | free_irq(IRQ_ICP, dev); | ||
672 | err_irq2: | ||
673 | free_irq(IRQ_STUART, dev); | ||
674 | err_irq1: | ||
675 | |||
676 | return err; | ||
677 | } | ||
678 | |||
679 | static int pxa_irda_stop(struct net_device *dev) | ||
680 | { | ||
681 | struct pxa_irda *si = netdev_priv(dev); | ||
682 | |||
683 | netif_stop_queue(dev); | ||
684 | |||
685 | pxa_irda_shutdown(si); | ||
686 | |||
687 | /* Stop IrLAP */ | ||
688 | if (si->irlap) { | ||
689 | irlap_close(si->irlap); | ||
690 | si->irlap = NULL; | ||
691 | } | ||
692 | |||
693 | free_irq(IRQ_STUART, dev); | ||
694 | free_irq(IRQ_ICP, dev); | ||
695 | |||
696 | pxa_free_dma(si->rxdma); | ||
697 | pxa_free_dma(si->txdma); | ||
698 | |||
699 | if (si->dma_rx_buff) | ||
700 | dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_tx_buff, si->dma_tx_buff_phy); | ||
701 | if (si->dma_tx_buff) | ||
702 | dma_free_coherent(si->dev, IRDA_FRAME_SIZE_LIMIT, si->dma_rx_buff, si->dma_rx_buff_phy); | ||
703 | |||
704 | printk(KERN_DEBUG "pxa_ir: irda driver closed\n"); | ||
705 | return 0; | ||
706 | } | ||
707 | |||
708 | static int pxa_irda_suspend(struct device *_dev, pm_message_t state) | ||
709 | { | ||
710 | struct net_device *dev = dev_get_drvdata(_dev); | ||
711 | struct pxa_irda *si; | ||
712 | |||
713 | if (dev && netif_running(dev)) { | ||
714 | si = netdev_priv(dev); | ||
715 | netif_device_detach(dev); | ||
716 | pxa_irda_shutdown(si); | ||
717 | } | ||
718 | |||
719 | return 0; | ||
720 | } | ||
721 | |||
722 | static int pxa_irda_resume(struct device *_dev) | ||
723 | { | ||
724 | struct net_device *dev = dev_get_drvdata(_dev); | ||
725 | struct pxa_irda *si; | ||
726 | |||
727 | if (dev && netif_running(dev)) { | ||
728 | si = netdev_priv(dev); | ||
729 | pxa_irda_startup(si); | ||
730 | netif_device_attach(dev); | ||
731 | netif_wake_queue(dev); | ||
732 | } | ||
733 | |||
734 | return 0; | ||
735 | } | ||
736 | |||
737 | |||
738 | static int pxa_irda_init_iobuf(iobuff_t *io, int size) | ||
739 | { | ||
740 | io->head = kmalloc(size, GFP_KERNEL | GFP_DMA); | ||
741 | if (io->head != NULL) { | ||
742 | io->truesize = size; | ||
743 | io->in_frame = FALSE; | ||
744 | io->state = OUTSIDE_FRAME; | ||
745 | io->data = io->head; | ||
746 | } | ||
747 | return io->head ? 0 : -ENOMEM; | ||
748 | } | ||
749 | |||
750 | static int pxa_irda_probe(struct device *_dev) | ||
751 | { | ||
752 | struct platform_device *pdev = to_platform_device(_dev); | ||
753 | struct net_device *dev; | ||
754 | struct pxa_irda *si; | ||
755 | unsigned int baudrate_mask; | ||
756 | int err; | ||
757 | |||
758 | if (!pdev->dev.platform_data) | ||
759 | return -ENODEV; | ||
760 | |||
761 | err = request_mem_region(__PREG(STUART), 0x24, "IrDA") ? 0 : -EBUSY; | ||
762 | if (err) | ||
763 | goto err_mem_1; | ||
764 | |||
765 | err = request_mem_region(__PREG(FICP), 0x1c, "IrDA") ? 0 : -EBUSY; | ||
766 | if (err) | ||
767 | goto err_mem_2; | ||
768 | |||
769 | dev = alloc_irdadev(sizeof(struct pxa_irda)); | ||
770 | if (!dev) | ||
771 | goto err_mem_3; | ||
772 | |||
773 | si = netdev_priv(dev); | ||
774 | si->dev = &pdev->dev; | ||
775 | si->pdata = pdev->dev.platform_data; | ||
776 | |||
777 | /* | ||
778 | * Initialise the SIR buffers | ||
779 | */ | ||
780 | err = pxa_irda_init_iobuf(&si->rx_buff, 14384); | ||
781 | if (err) | ||
782 | goto err_mem_4; | ||
783 | err = pxa_irda_init_iobuf(&si->tx_buff, 4000); | ||
784 | if (err) | ||
785 | goto err_mem_5; | ||
786 | |||
787 | dev->hard_start_xmit = pxa_irda_hard_xmit; | ||
788 | dev->open = pxa_irda_start; | ||
789 | dev->stop = pxa_irda_stop; | ||
790 | dev->do_ioctl = pxa_irda_ioctl; | ||
791 | dev->get_stats = pxa_irda_stats; | ||
792 | |||
793 | irda_init_max_qos_capabilies(&si->qos); | ||
794 | |||
795 | baudrate_mask = 0; | ||
796 | if (si->pdata->transceiver_cap & IR_SIRMODE) | ||
797 | baudrate_mask |= IR_9600|IR_19200|IR_38400|IR_57600|IR_115200; | ||
798 | if (si->pdata->transceiver_cap & IR_FIRMODE) | ||
799 | baudrate_mask |= IR_4000000 << 8; | ||
800 | |||
801 | si->qos.baud_rate.bits &= baudrate_mask; | ||
802 | si->qos.min_turn_time.bits = 7; /* 1ms or more */ | ||
803 | |||
804 | irda_qos_bits_to_value(&si->qos); | ||
805 | |||
806 | err = register_netdev(dev); | ||
807 | |||
808 | if (err == 0) | ||
809 | dev_set_drvdata(&pdev->dev, dev); | ||
810 | |||
811 | if (err) { | ||
812 | kfree(si->tx_buff.head); | ||
813 | err_mem_5: | ||
814 | kfree(si->rx_buff.head); | ||
815 | err_mem_4: | ||
816 | free_netdev(dev); | ||
817 | err_mem_3: | ||
818 | release_mem_region(__PREG(FICP), 0x1c); | ||
819 | err_mem_2: | ||
820 | release_mem_region(__PREG(STUART), 0x24); | ||
821 | } | ||
822 | err_mem_1: | ||
823 | return err; | ||
824 | } | ||
825 | |||
826 | static int pxa_irda_remove(struct device *_dev) | ||
827 | { | ||
828 | struct net_device *dev = dev_get_drvdata(_dev); | ||
829 | |||
830 | if (dev) { | ||
831 | struct pxa_irda *si = netdev_priv(dev); | ||
832 | unregister_netdev(dev); | ||
833 | kfree(si->tx_buff.head); | ||
834 | kfree(si->rx_buff.head); | ||
835 | free_netdev(dev); | ||
836 | } | ||
837 | |||
838 | release_mem_region(__PREG(STUART), 0x24); | ||
839 | release_mem_region(__PREG(FICP), 0x1c); | ||
840 | |||
841 | return 0; | ||
842 | } | ||
843 | |||
844 | static struct device_driver pxa_ir_driver = { | ||
845 | .name = "pxa2xx-ir", | ||
846 | .bus = &platform_bus_type, | ||
847 | .probe = pxa_irda_probe, | ||
848 | .remove = pxa_irda_remove, | ||
849 | .suspend = pxa_irda_suspend, | ||
850 | .resume = pxa_irda_resume, | ||
851 | }; | ||
852 | |||
853 | static int __init pxa_irda_init(void) | ||
854 | { | ||
855 | return driver_register(&pxa_ir_driver); | ||
856 | } | ||
857 | |||
858 | static void __exit pxa_irda_exit(void) | ||
859 | { | ||
860 | driver_unregister(&pxa_ir_driver); | ||
861 | } | ||
862 | |||
863 | module_init(pxa_irda_init); | ||
864 | module_exit(pxa_irda_exit); | ||
865 | |||
866 | MODULE_LICENSE("GPL"); | ||