Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/net/irda/sa1100_ir.c | |
3 | * | |
4 | * Copyright (C) 2000-2001 Russell King | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | * | |
10 | * Infra-red driver for the StrongARM SA1100 embedded microprocessor | |
11 | * | |
12 | * Note that we don't have to worry about the SA1111's DMA bugs in here, | |
13 | * so we use the straight forward dma_map_* functions with a null pointer. | |
14 | * | |
15 | * This driver takes one kernel command line parameter, sa1100ir=, with | |
16 | * the following options: | |
17 | * max_rate:baudrate - set the maximum baud rate | |
15877e9c | 18 | * power_level:level - set the transmitter power level |
1da177e4 LT |
19 | * tx_lpm:0|1 - set transmit low power mode |
20 | */ | |
1da177e4 LT |
21 | #include <linux/module.h> |
22 | #include <linux/moduleparam.h> | |
23 | #include <linux/types.h> | |
24 | #include <linux/init.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/netdevice.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/rtnetlink.h> | |
29 | #include <linux/interrupt.h> | |
30 | #include <linux/delay.h> | |
d052d1be | 31 | #include <linux/platform_device.h> |
1da177e4 LT |
32 | #include <linux/dma-mapping.h> |
33 | ||
34 | #include <net/irda/irda.h> | |
35 | #include <net/irda/wrapper.h> | |
36 | #include <net/irda/irda_device.h> | |
37 | ||
d281bc9d | 38 | #include <mach/dma.h> |
a09e64fb | 39 | #include <mach/hardware.h> |
1da177e4 LT |
40 | #include <asm/mach/irda.h> |
41 | ||
42 | static int power_level = 3; | |
43 | static int tx_lpm; | |
44 | static int max_rate = 4000000; | |
45 | ||
885767ca RK |
46 | struct sa1100_buf { |
47 | struct sk_buff *skb; | |
48 | dma_addr_t dma; | |
49 | dma_regs_t *regs; | |
50 | }; | |
51 | ||
1da177e4 LT |
52 | struct sa1100_irda { |
53 | unsigned char hscr0; | |
54 | unsigned char utcr4; | |
55 | unsigned char power; | |
56 | unsigned char open; | |
57 | ||
58 | int speed; | |
59 | int newspeed; | |
60 | ||
885767ca RK |
61 | struct sa1100_buf dma_rx; |
62 | struct sa1100_buf dma_tx; | |
1da177e4 | 63 | |
1da177e4 LT |
64 | struct device *dev; |
65 | struct irda_platform_data *pdata; | |
66 | struct irlap_cb *irlap; | |
67 | struct qos_info qos; | |
68 | ||
69 | iobuff_t tx_buff; | |
70 | iobuff_t rx_buff; | |
71 | }; | |
72 | ||
73 | #define IS_FIR(si) ((si)->speed >= 4000000) | |
74 | ||
75 | #define HPSIR_MAX_RXLEN 2047 | |
76 | ||
77 | /* | |
78 | * Allocate and map the receive buffer, unless it is already allocated. | |
79 | */ | |
80 | static int sa1100_irda_rx_alloc(struct sa1100_irda *si) | |
81 | { | |
885767ca | 82 | if (si->dma_rx.skb) |
1da177e4 LT |
83 | return 0; |
84 | ||
885767ca RK |
85 | si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC); |
86 | if (!si->dma_rx.skb) { | |
1da177e4 LT |
87 | printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n"); |
88 | return -ENOMEM; | |
89 | } | |
90 | ||
91 | /* | |
92 | * Align any IP headers that may be contained | |
93 | * within the frame. | |
94 | */ | |
885767ca | 95 | skb_reserve(si->dma_rx.skb, 1); |
1da177e4 | 96 | |
885767ca | 97 | si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data, |
1da177e4 LT |
98 | HPSIR_MAX_RXLEN, |
99 | DMA_FROM_DEVICE); | |
885767ca RK |
100 | if (dma_mapping_error(si->dev, si->dma_rx.dma)) { |
101 | dev_kfree_skb_any(si->dma_rx.skb); | |
22f0bf96 RK |
102 | return -ENOMEM; |
103 | } | |
104 | ||
1da177e4 LT |
105 | return 0; |
106 | } | |
107 | ||
108 | /* | |
109 | * We want to get here as soon as possible, and get the receiver setup. | |
110 | * We use the existing buffer. | |
111 | */ | |
112 | static void sa1100_irda_rx_dma_start(struct sa1100_irda *si) | |
113 | { | |
885767ca | 114 | if (!si->dma_rx.skb) { |
1da177e4 LT |
115 | printk(KERN_ERR "sa1100_ir: rx buffer went missing\n"); |
116 | return; | |
117 | } | |
118 | ||
119 | /* | |
120 | * First empty receive FIFO | |
121 | */ | |
122 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP; | |
123 | ||
124 | /* | |
125 | * Enable the DMA, receiver and receive interrupt. | |
126 | */ | |
885767ca RK |
127 | sa1100_clear_dma(si->dma_rx.regs); |
128 | sa1100_start_dma(si->dma_rx.regs, si->dma_rx.dma, HPSIR_MAX_RXLEN); | |
1da177e4 LT |
129 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE; |
130 | } | |
131 | ||
132 | /* | |
133 | * Set the IrDA communications speed. | |
134 | */ | |
135 | static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed) | |
136 | { | |
137 | unsigned long flags; | |
138 | int brd, ret = -EINVAL; | |
139 | ||
140 | switch (speed) { | |
141 | case 9600: case 19200: case 38400: | |
142 | case 57600: case 115200: | |
143 | brd = 3686400 / (16 * speed) - 1; | |
144 | ||
145 | /* | |
146 | * Stop the receive DMA. | |
147 | */ | |
148 | if (IS_FIR(si)) | |
885767ca | 149 | sa1100_stop_dma(si->dma_rx.regs); |
1da177e4 LT |
150 | |
151 | local_irq_save(flags); | |
152 | ||
153 | Ser2UTCR3 = 0; | |
154 | Ser2HSCR0 = HSCR0_UART; | |
155 | ||
156 | Ser2UTCR1 = brd >> 8; | |
157 | Ser2UTCR2 = brd; | |
158 | ||
159 | /* | |
160 | * Clear status register | |
161 | */ | |
162 | Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID; | |
163 | Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE; | |
164 | ||
165 | if (si->pdata->set_speed) | |
166 | si->pdata->set_speed(si->dev, speed); | |
167 | ||
168 | si->speed = speed; | |
169 | ||
170 | local_irq_restore(flags); | |
171 | ret = 0; | |
172 | break; | |
173 | ||
174 | case 4000000: | |
175 | local_irq_save(flags); | |
176 | ||
177 | si->hscr0 = 0; | |
178 | ||
179 | Ser2HSSR0 = 0xff; | |
180 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP; | |
181 | Ser2UTCR3 = 0; | |
182 | ||
183 | si->speed = speed; | |
184 | ||
185 | if (si->pdata->set_speed) | |
186 | si->pdata->set_speed(si->dev, speed); | |
187 | ||
188 | sa1100_irda_rx_alloc(si); | |
189 | sa1100_irda_rx_dma_start(si); | |
190 | ||
191 | local_irq_restore(flags); | |
192 | ||
193 | break; | |
194 | ||
195 | default: | |
196 | break; | |
197 | } | |
198 | ||
199 | return ret; | |
200 | } | |
201 | ||
202 | /* | |
203 | * Control the power state of the IrDA transmitter. | |
204 | * State: | |
205 | * 0 - off | |
206 | * 1 - short range, lowest power | |
207 | * 2 - medium range, medium power | |
208 | * 3 - maximum range, high power | |
209 | * | |
210 | * Currently, only assabet is known to support this. | |
211 | */ | |
212 | static int | |
213 | __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state) | |
214 | { | |
215 | int ret = 0; | |
216 | if (si->pdata->set_power) | |
217 | ret = si->pdata->set_power(si->dev, state); | |
218 | return ret; | |
219 | } | |
220 | ||
221 | static inline int | |
222 | sa1100_set_power(struct sa1100_irda *si, unsigned int state) | |
223 | { | |
224 | int ret; | |
225 | ||
226 | ret = __sa1100_irda_set_power(si, state); | |
227 | if (ret == 0) | |
228 | si->power = state; | |
229 | ||
230 | return ret; | |
231 | } | |
232 | ||
233 | static int sa1100_irda_startup(struct sa1100_irda *si) | |
234 | { | |
235 | int ret; | |
236 | ||
237 | /* | |
238 | * Ensure that the ports for this device are setup correctly. | |
239 | */ | |
91cd1756 DA |
240 | if (si->pdata->startup) { |
241 | ret = si->pdata->startup(si->dev); | |
242 | if (ret) | |
243 | return ret; | |
244 | } | |
1da177e4 LT |
245 | |
246 | /* | |
247 | * Configure PPC for IRDA - we want to drive TXD2 low. | |
248 | * We also want to drive this pin low during sleep. | |
249 | */ | |
250 | PPSR &= ~PPC_TXD2; | |
251 | PSDR &= ~PPC_TXD2; | |
252 | PPDR |= PPC_TXD2; | |
253 | ||
254 | /* | |
255 | * Enable HP-SIR modulation, and ensure that the port is disabled. | |
256 | */ | |
257 | Ser2UTCR3 = 0; | |
258 | Ser2HSCR0 = HSCR0_UART; | |
259 | Ser2UTCR4 = si->utcr4; | |
260 | Ser2UTCR0 = UTCR0_8BitData; | |
261 | Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL; | |
262 | ||
263 | /* | |
264 | * Clear status register | |
265 | */ | |
266 | Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID; | |
267 | ||
268 | ret = sa1100_irda_set_speed(si, si->speed = 9600); | |
269 | if (ret) { | |
270 | Ser2UTCR3 = 0; | |
271 | Ser2HSCR0 = 0; | |
272 | ||
273 | if (si->pdata->shutdown) | |
274 | si->pdata->shutdown(si->dev); | |
275 | } | |
276 | ||
277 | return ret; | |
278 | } | |
279 | ||
280 | static void sa1100_irda_shutdown(struct sa1100_irda *si) | |
281 | { | |
282 | /* | |
283 | * Stop all DMA activity. | |
284 | */ | |
885767ca RK |
285 | sa1100_stop_dma(si->dma_rx.regs); |
286 | sa1100_stop_dma(si->dma_tx.regs); | |
1da177e4 LT |
287 | |
288 | /* Disable the port. */ | |
289 | Ser2UTCR3 = 0; | |
290 | Ser2HSCR0 = 0; | |
291 | ||
292 | if (si->pdata->shutdown) | |
293 | si->pdata->shutdown(si->dev); | |
294 | } | |
295 | ||
296 | #ifdef CONFIG_PM | |
297 | /* | |
298 | * Suspend the IrDA interface. | |
299 | */ | |
3ae5eaec | 300 | static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state) |
1da177e4 | 301 | { |
3ae5eaec | 302 | struct net_device *dev = platform_get_drvdata(pdev); |
1da177e4 LT |
303 | struct sa1100_irda *si; |
304 | ||
9480e307 | 305 | if (!dev) |
1da177e4 LT |
306 | return 0; |
307 | ||
4cf1653a | 308 | si = netdev_priv(dev); |
1da177e4 LT |
309 | if (si->open) { |
310 | /* | |
311 | * Stop the transmit queue | |
312 | */ | |
313 | netif_device_detach(dev); | |
314 | disable_irq(dev->irq); | |
315 | sa1100_irda_shutdown(si); | |
316 | __sa1100_irda_set_power(si, 0); | |
317 | } | |
318 | ||
319 | return 0; | |
320 | } | |
321 | ||
322 | /* | |
323 | * Resume the IrDA interface. | |
324 | */ | |
3ae5eaec | 325 | static int sa1100_irda_resume(struct platform_device *pdev) |
1da177e4 | 326 | { |
3ae5eaec | 327 | struct net_device *dev = platform_get_drvdata(pdev); |
1da177e4 LT |
328 | struct sa1100_irda *si; |
329 | ||
9480e307 | 330 | if (!dev) |
1da177e4 LT |
331 | return 0; |
332 | ||
4cf1653a | 333 | si = netdev_priv(dev); |
1da177e4 LT |
334 | if (si->open) { |
335 | /* | |
336 | * If we missed a speed change, initialise at the new speed | |
337 | * directly. It is debatable whether this is actually | |
338 | * required, but in the interests of continuing from where | |
3ad2f3fb | 339 | * we left off it is desirable. The converse argument is |
1da177e4 LT |
340 | * that we should re-negotiate at 9600 baud again. |
341 | */ | |
342 | if (si->newspeed) { | |
343 | si->speed = si->newspeed; | |
344 | si->newspeed = 0; | |
345 | } | |
346 | ||
347 | sa1100_irda_startup(si); | |
348 | __sa1100_irda_set_power(si, si->power); | |
349 | enable_irq(dev->irq); | |
350 | ||
351 | /* | |
352 | * This automatically wakes up the queue | |
353 | */ | |
354 | netif_device_attach(dev); | |
355 | } | |
356 | ||
357 | return 0; | |
358 | } | |
359 | #else | |
360 | #define sa1100_irda_suspend NULL | |
361 | #define sa1100_irda_resume NULL | |
362 | #endif | |
363 | ||
364 | /* | |
365 | * HP-SIR format interrupt service routines. | |
366 | */ | |
367 | static void sa1100_irda_hpsir_irq(struct net_device *dev) | |
368 | { | |
4cf1653a | 369 | struct sa1100_irda *si = netdev_priv(dev); |
1da177e4 LT |
370 | int status; |
371 | ||
372 | status = Ser2UTSR0; | |
373 | ||
374 | /* | |
375 | * Deal with any receive errors first. The bytes in error may be | |
376 | * the only bytes in the receive FIFO, so we do this first. | |
377 | */ | |
378 | while (status & UTSR0_EIF) { | |
379 | int stat, data; | |
380 | ||
381 | stat = Ser2UTSR1; | |
382 | data = Ser2UTDR; | |
383 | ||
384 | if (stat & (UTSR1_FRE | UTSR1_ROR)) { | |
af049081 | 385 | dev->stats.rx_errors++; |
1da177e4 | 386 | if (stat & UTSR1_FRE) |
af049081 | 387 | dev->stats.rx_frame_errors++; |
1da177e4 | 388 | if (stat & UTSR1_ROR) |
af049081 | 389 | dev->stats.rx_fifo_errors++; |
1da177e4 | 390 | } else |
af049081 | 391 | async_unwrap_char(dev, &dev->stats, &si->rx_buff, data); |
1da177e4 LT |
392 | |
393 | status = Ser2UTSR0; | |
394 | } | |
395 | ||
396 | /* | |
397 | * We must clear certain bits. | |
398 | */ | |
399 | Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB); | |
400 | ||
401 | if (status & UTSR0_RFS) { | |
402 | /* | |
403 | * There are at least 4 bytes in the FIFO. Read 3 bytes | |
404 | * and leave the rest to the block below. | |
405 | */ | |
af049081 SH |
406 | async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR); |
407 | async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR); | |
408 | async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR); | |
1da177e4 LT |
409 | } |
410 | ||
411 | if (status & (UTSR0_RFS | UTSR0_RID)) { | |
412 | /* | |
413 | * Fifo contains more than 1 character. | |
414 | */ | |
415 | do { | |
af049081 | 416 | async_unwrap_char(dev, &dev->stats, &si->rx_buff, |
1da177e4 LT |
417 | Ser2UTDR); |
418 | } while (Ser2UTSR1 & UTSR1_RNE); | |
419 | ||
1da177e4 LT |
420 | } |
421 | ||
422 | if (status & UTSR0_TFS && si->tx_buff.len) { | |
423 | /* | |
424 | * Transmitter FIFO is not full | |
425 | */ | |
426 | do { | |
427 | Ser2UTDR = *si->tx_buff.data++; | |
428 | si->tx_buff.len -= 1; | |
429 | } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len); | |
430 | ||
431 | if (si->tx_buff.len == 0) { | |
af049081 SH |
432 | dev->stats.tx_packets++; |
433 | dev->stats.tx_bytes += si->tx_buff.data - | |
1da177e4 LT |
434 | si->tx_buff.head; |
435 | ||
436 | /* | |
437 | * We need to ensure that the transmitter has | |
438 | * finished. | |
439 | */ | |
440 | do | |
441 | rmb(); | |
442 | while (Ser2UTSR1 & UTSR1_TBY); | |
443 | ||
444 | /* | |
445 | * Ok, we've finished transmitting. Now enable | |
446 | * the receiver. Sometimes we get a receive IRQ | |
447 | * immediately after a transmit... | |
448 | */ | |
449 | Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID; | |
450 | Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE; | |
451 | ||
452 | if (si->newspeed) { | |
453 | sa1100_irda_set_speed(si, si->newspeed); | |
454 | si->newspeed = 0; | |
455 | } | |
456 | ||
457 | /* I'm hungry! */ | |
458 | netif_wake_queue(dev); | |
459 | } | |
460 | } | |
461 | } | |
462 | ||
463 | static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev) | |
464 | { | |
885767ca | 465 | struct sk_buff *skb = si->dma_rx.skb; |
1da177e4 LT |
466 | dma_addr_t dma_addr; |
467 | unsigned int len, stat, data; | |
468 | ||
469 | if (!skb) { | |
470 | printk(KERN_ERR "sa1100_ir: SKB is NULL!\n"); | |
471 | return; | |
472 | } | |
473 | ||
474 | /* | |
475 | * Get the current data position. | |
476 | */ | |
885767ca RK |
477 | dma_addr = sa1100_get_dma_pos(si->dma_rx.regs); |
478 | len = dma_addr - si->dma_rx.dma; | |
1da177e4 LT |
479 | if (len > HPSIR_MAX_RXLEN) |
480 | len = HPSIR_MAX_RXLEN; | |
885767ca | 481 | dma_unmap_single(si->dev, si->dma_rx.dma, len, DMA_FROM_DEVICE); |
1da177e4 LT |
482 | |
483 | do { | |
484 | /* | |
485 | * Read Status, and then Data. | |
486 | */ | |
487 | stat = Ser2HSSR1; | |
488 | rmb(); | |
489 | data = Ser2HSDR; | |
490 | ||
491 | if (stat & (HSSR1_CRE | HSSR1_ROR)) { | |
af049081 | 492 | dev->stats.rx_errors++; |
1da177e4 | 493 | if (stat & HSSR1_CRE) |
af049081 | 494 | dev->stats.rx_crc_errors++; |
1da177e4 | 495 | if (stat & HSSR1_ROR) |
af049081 | 496 | dev->stats.rx_frame_errors++; |
1da177e4 LT |
497 | } else |
498 | skb->data[len++] = data; | |
499 | ||
500 | /* | |
501 | * If we hit the end of frame, there's | |
502 | * no point in continuing. | |
503 | */ | |
504 | if (stat & HSSR1_EOF) | |
505 | break; | |
506 | } while (Ser2HSSR0 & HSSR0_EIF); | |
507 | ||
508 | if (stat & HSSR1_EOF) { | |
885767ca | 509 | si->dma_rx.skb = NULL; |
1da177e4 LT |
510 | |
511 | skb_put(skb, len); | |
512 | skb->dev = dev; | |
459a98ed | 513 | skb_reset_mac_header(skb); |
1da177e4 | 514 | skb->protocol = htons(ETH_P_IRDA); |
af049081 SH |
515 | dev->stats.rx_packets++; |
516 | dev->stats.rx_bytes += len; | |
1da177e4 LT |
517 | |
518 | /* | |
519 | * Before we pass the buffer up, allocate a new one. | |
520 | */ | |
521 | sa1100_irda_rx_alloc(si); | |
522 | ||
523 | netif_rx(skb); | |
1da177e4 LT |
524 | } else { |
525 | /* | |
22f0bf96 RK |
526 | * Remap the buffer - it was previously mapped, and we |
527 | * hope that this succeeds. | |
1da177e4 | 528 | */ |
885767ca | 529 | si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data, |
1da177e4 LT |
530 | HPSIR_MAX_RXLEN, |
531 | DMA_FROM_DEVICE); | |
532 | } | |
533 | } | |
534 | ||
535 | /* | |
536 | * FIR format interrupt service routine. We only have to | |
537 | * handle RX events; transmit events go via the TX DMA handler. | |
538 | * | |
539 | * No matter what, we disable RX, process, and the restart RX. | |
540 | */ | |
541 | static void sa1100_irda_fir_irq(struct net_device *dev) | |
542 | { | |
4cf1653a | 543 | struct sa1100_irda *si = netdev_priv(dev); |
1da177e4 LT |
544 | |
545 | /* | |
546 | * Stop RX DMA | |
547 | */ | |
885767ca | 548 | sa1100_stop_dma(si->dma_rx.regs); |
1da177e4 LT |
549 | |
550 | /* | |
551 | * Framing error - we throw away the packet completely. | |
552 | * Clearing RXE flushes the error conditions and data | |
553 | * from the fifo. | |
554 | */ | |
555 | if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) { | |
af049081 | 556 | dev->stats.rx_errors++; |
1da177e4 LT |
557 | |
558 | if (Ser2HSSR0 & HSSR0_FRE) | |
af049081 | 559 | dev->stats.rx_frame_errors++; |
1da177e4 LT |
560 | |
561 | /* | |
562 | * Clear out the DMA... | |
563 | */ | |
564 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP; | |
565 | ||
566 | /* | |
567 | * Clear selected status bits now, so we | |
568 | * don't miss them next time around. | |
569 | */ | |
570 | Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB; | |
571 | } | |
572 | ||
573 | /* | |
574 | * Deal with any receive errors. The any of the lowest | |
575 | * 8 bytes in the FIFO may contain an error. We must read | |
576 | * them one by one. The "error" could even be the end of | |
577 | * packet! | |
578 | */ | |
579 | if (Ser2HSSR0 & HSSR0_EIF) | |
580 | sa1100_irda_fir_error(si, dev); | |
581 | ||
582 | /* | |
583 | * No matter what happens, we must restart reception. | |
584 | */ | |
585 | sa1100_irda_rx_dma_start(si); | |
586 | } | |
587 | ||
7d12e780 | 588 | static irqreturn_t sa1100_irda_irq(int irq, void *dev_id) |
1da177e4 LT |
589 | { |
590 | struct net_device *dev = dev_id; | |
4cf1653a | 591 | if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev)))) |
1da177e4 LT |
592 | sa1100_irda_fir_irq(dev); |
593 | else | |
594 | sa1100_irda_hpsir_irq(dev); | |
595 | return IRQ_HANDLED; | |
596 | } | |
597 | ||
598 | /* | |
599 | * TX DMA completion handler. | |
600 | */ | |
601 | static void sa1100_irda_txdma_irq(void *id) | |
602 | { | |
603 | struct net_device *dev = id; | |
4cf1653a | 604 | struct sa1100_irda *si = netdev_priv(dev); |
ba84525b | 605 | struct sk_buff *skb; |
1da177e4 LT |
606 | |
607 | /* | |
608 | * Wait for the transmission to complete. Unfortunately, | |
609 | * the hardware doesn't give us an interrupt to indicate | |
610 | * "end of frame". | |
611 | */ | |
612 | do | |
613 | rmb(); | |
614 | while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY); | |
615 | ||
616 | /* | |
617 | * Clear the transmit underrun bit. | |
618 | */ | |
619 | Ser2HSSR0 = HSSR0_TUR; | |
620 | ||
621 | /* | |
622 | * Do we need to change speed? Note that we're lazy | |
885767ca | 623 | * here - we don't free the old dma_rx.skb. We don't need |
1da177e4 LT |
624 | * to allocate a buffer either. |
625 | */ | |
626 | if (si->newspeed) { | |
627 | sa1100_irda_set_speed(si, si->newspeed); | |
628 | si->newspeed = 0; | |
629 | } | |
630 | ||
631 | /* | |
632 | * Start reception. This disables the transmitter for | |
633 | * us. This will be using the existing RX buffer. | |
634 | */ | |
635 | sa1100_irda_rx_dma_start(si); | |
636 | ||
ba84525b RK |
637 | /* Account and free the packet. */ |
638 | skb = si->dma_tx.skb; | |
1da177e4 | 639 | if (skb) { |
ba84525b RK |
640 | dma_unmap_single(si->dev, si->dma_tx.dma, skb->len, |
641 | DMA_TO_DEVICE); | |
af049081 SH |
642 | dev->stats.tx_packets ++; |
643 | dev->stats.tx_bytes += skb->len; | |
1da177e4 | 644 | dev_kfree_skb_irq(skb); |
ba84525b | 645 | si->dma_tx.skb = NULL; |
1da177e4 LT |
646 | } |
647 | ||
648 | /* | |
649 | * Make sure that the TX queue is available for sending | |
650 | * (for retries). TX has priority over RX at all times. | |
651 | */ | |
652 | netif_wake_queue(dev); | |
653 | } | |
654 | ||
655 | static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev) | |
656 | { | |
4cf1653a | 657 | struct sa1100_irda *si = netdev_priv(dev); |
1da177e4 LT |
658 | int speed = irda_get_next_speed(skb); |
659 | ||
660 | /* | |
661 | * Does this packet contain a request to change the interface | |
662 | * speed? If so, remember it until we complete the transmission | |
663 | * of this frame. | |
664 | */ | |
665 | if (speed != si->speed && speed != -1) | |
666 | si->newspeed = speed; | |
667 | ||
668 | /* | |
669 | * If this is an empty frame, we can bypass a lot. | |
670 | */ | |
671 | if (skb->len == 0) { | |
672 | if (si->newspeed) { | |
673 | si->newspeed = 0; | |
674 | sa1100_irda_set_speed(si, speed); | |
675 | } | |
676 | dev_kfree_skb(skb); | |
6ed10654 | 677 | return NETDEV_TX_OK; |
1da177e4 LT |
678 | } |
679 | ||
680 | if (!IS_FIR(si)) { | |
681 | netif_stop_queue(dev); | |
682 | ||
683 | si->tx_buff.data = si->tx_buff.head; | |
684 | si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data, | |
685 | si->tx_buff.truesize); | |
686 | ||
687 | /* | |
688 | * Set the transmit interrupt enable. This will fire | |
689 | * off an interrupt immediately. Note that we disable | |
690 | * the receiver so we won't get spurious characteres | |
691 | * received. | |
692 | */ | |
693 | Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE; | |
694 | ||
695 | dev_kfree_skb(skb); | |
696 | } else { | |
697 | int mtt = irda_get_mtt(skb); | |
698 | ||
699 | /* | |
700 | * We must not be transmitting... | |
701 | */ | |
885767ca | 702 | BUG_ON(si->dma_tx.skb); |
1da177e4 LT |
703 | |
704 | netif_stop_queue(dev); | |
705 | ||
885767ca RK |
706 | si->dma_tx.skb = skb; |
707 | si->dma_tx.dma = dma_map_single(si->dev, skb->data, | |
1da177e4 | 708 | skb->len, DMA_TO_DEVICE); |
885767ca RK |
709 | if (dma_mapping_error(si->dev, si->dma_tx.dma)) { |
710 | si->dma_tx.skb = NULL; | |
22f0bf96 RK |
711 | netif_wake_queue(dev); |
712 | dev->stats.tx_dropped++; | |
713 | dev_kfree_skb(skb); | |
714 | return NETDEV_TX_OK; | |
715 | } | |
1da177e4 | 716 | |
885767ca | 717 | sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len); |
1da177e4 LT |
718 | |
719 | /* | |
720 | * If we have a mean turn-around time, impose the specified | |
721 | * specified delay. We could shorten this by timing from | |
722 | * the point we received the packet. | |
723 | */ | |
724 | if (mtt) | |
725 | udelay(mtt); | |
726 | ||
727 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE; | |
728 | } | |
729 | ||
6ed10654 | 730 | return NETDEV_TX_OK; |
1da177e4 LT |
731 | } |
732 | ||
733 | static int | |
734 | sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd) | |
735 | { | |
736 | struct if_irda_req *rq = (struct if_irda_req *)ifreq; | |
4cf1653a | 737 | struct sa1100_irda *si = netdev_priv(dev); |
1da177e4 LT |
738 | int ret = -EOPNOTSUPP; |
739 | ||
740 | switch (cmd) { | |
741 | case SIOCSBANDWIDTH: | |
742 | if (capable(CAP_NET_ADMIN)) { | |
743 | /* | |
744 | * We are unable to set the speed if the | |
745 | * device is not running. | |
746 | */ | |
747 | if (si->open) { | |
748 | ret = sa1100_irda_set_speed(si, | |
749 | rq->ifr_baudrate); | |
750 | } else { | |
751 | printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n"); | |
752 | ret = 0; | |
753 | } | |
754 | } | |
755 | break; | |
756 | ||
757 | case SIOCSMEDIABUSY: | |
758 | ret = -EPERM; | |
759 | if (capable(CAP_NET_ADMIN)) { | |
760 | irda_device_set_media_busy(dev, TRUE); | |
761 | ret = 0; | |
762 | } | |
763 | break; | |
764 | ||
765 | case SIOCGRECEIVING: | |
766 | rq->ifr_receiving = IS_FIR(si) ? 0 | |
767 | : si->rx_buff.state != OUTSIDE_FRAME; | |
768 | break; | |
769 | ||
770 | default: | |
771 | break; | |
772 | } | |
773 | ||
774 | return ret; | |
775 | } | |
776 | ||
1da177e4 LT |
777 | static int sa1100_irda_start(struct net_device *dev) |
778 | { | |
4cf1653a | 779 | struct sa1100_irda *si = netdev_priv(dev); |
1da177e4 LT |
780 | int err; |
781 | ||
782 | si->speed = 9600; | |
783 | ||
784 | err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev); | |
785 | if (err) | |
786 | goto err_irq; | |
787 | ||
788 | err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive", | |
885767ca | 789 | NULL, NULL, &si->dma_rx.regs); |
1da177e4 LT |
790 | if (err) |
791 | goto err_rx_dma; | |
792 | ||
793 | err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit", | |
885767ca | 794 | sa1100_irda_txdma_irq, dev, &si->dma_tx.regs); |
1da177e4 LT |
795 | if (err) |
796 | goto err_tx_dma; | |
797 | ||
798 | /* | |
799 | * The interrupt must remain disabled for now. | |
800 | */ | |
801 | disable_irq(dev->irq); | |
802 | ||
803 | /* | |
804 | * Setup the serial port for the specified speed. | |
805 | */ | |
806 | err = sa1100_irda_startup(si); | |
807 | if (err) | |
808 | goto err_startup; | |
809 | ||
810 | /* | |
811 | * Open a new IrLAP layer instance. | |
812 | */ | |
813 | si->irlap = irlap_open(dev, &si->qos, "sa1100"); | |
814 | err = -ENOMEM; | |
815 | if (!si->irlap) | |
816 | goto err_irlap; | |
817 | ||
818 | /* | |
819 | * Now enable the interrupt and start the queue | |
820 | */ | |
821 | si->open = 1; | |
822 | sa1100_set_power(si, power_level); /* low power mode */ | |
823 | enable_irq(dev->irq); | |
824 | netif_start_queue(dev); | |
825 | return 0; | |
826 | ||
827 | err_irlap: | |
828 | si->open = 0; | |
829 | sa1100_irda_shutdown(si); | |
830 | err_startup: | |
885767ca | 831 | sa1100_free_dma(si->dma_tx.regs); |
1da177e4 | 832 | err_tx_dma: |
885767ca | 833 | sa1100_free_dma(si->dma_rx.regs); |
1da177e4 LT |
834 | err_rx_dma: |
835 | free_irq(dev->irq, dev); | |
836 | err_irq: | |
837 | return err; | |
838 | } | |
839 | ||
840 | static int sa1100_irda_stop(struct net_device *dev) | |
841 | { | |
4cf1653a | 842 | struct sa1100_irda *si = netdev_priv(dev); |
ba84525b | 843 | struct sk_buff *skb; |
1da177e4 LT |
844 | |
845 | disable_irq(dev->irq); | |
846 | sa1100_irda_shutdown(si); | |
847 | ||
848 | /* | |
ba84525b | 849 | * If we have been doing any DMA activity, make sure we |
1da177e4 LT |
850 | * tidy that up cleanly. |
851 | */ | |
ba84525b RK |
852 | skb = si->dma_rx.skb; |
853 | if (skb) { | |
885767ca | 854 | dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN, |
1da177e4 | 855 | DMA_FROM_DEVICE); |
ba84525b | 856 | dev_kfree_skb(skb); |
885767ca | 857 | si->dma_rx.skb = NULL; |
1da177e4 LT |
858 | } |
859 | ||
ba84525b RK |
860 | skb = si->dma_tx.skb; |
861 | if (skb) { | |
862 | dma_unmap_single(si->dev, si->dma_tx.dma, skb->len, | |
863 | DMA_TO_DEVICE); | |
864 | dev_kfree_skb(skb); | |
865 | si->dma_tx.skb = NULL; | |
866 | } | |
867 | ||
1da177e4 LT |
868 | /* Stop IrLAP */ |
869 | if (si->irlap) { | |
870 | irlap_close(si->irlap); | |
871 | si->irlap = NULL; | |
872 | } | |
873 | ||
874 | netif_stop_queue(dev); | |
875 | si->open = 0; | |
876 | ||
877 | /* | |
878 | * Free resources | |
879 | */ | |
885767ca RK |
880 | sa1100_free_dma(si->dma_tx.regs); |
881 | sa1100_free_dma(si->dma_rx.regs); | |
1da177e4 LT |
882 | free_irq(dev->irq, dev); |
883 | ||
884 | sa1100_set_power(si, 0); | |
885 | ||
886 | return 0; | |
887 | } | |
888 | ||
889 | static int sa1100_irda_init_iobuf(iobuff_t *io, int size) | |
890 | { | |
891 | io->head = kmalloc(size, GFP_KERNEL | GFP_DMA); | |
892 | if (io->head != NULL) { | |
893 | io->truesize = size; | |
894 | io->in_frame = FALSE; | |
895 | io->state = OUTSIDE_FRAME; | |
896 | io->data = io->head; | |
897 | } | |
898 | return io->head ? 0 : -ENOMEM; | |
899 | } | |
900 | ||
a1de9666 AB |
901 | static const struct net_device_ops sa1100_irda_netdev_ops = { |
902 | .ndo_open = sa1100_irda_start, | |
903 | .ndo_stop = sa1100_irda_stop, | |
904 | .ndo_start_xmit = sa1100_irda_hard_xmit, | |
905 | .ndo_do_ioctl = sa1100_irda_ioctl, | |
a1de9666 AB |
906 | }; |
907 | ||
3ae5eaec | 908 | static int sa1100_irda_probe(struct platform_device *pdev) |
1da177e4 | 909 | { |
1da177e4 LT |
910 | struct net_device *dev; |
911 | struct sa1100_irda *si; | |
912 | unsigned int baudrate_mask; | |
e556fdbd | 913 | int err, irq; |
1da177e4 LT |
914 | |
915 | if (!pdev->dev.platform_data) | |
916 | return -EINVAL; | |
917 | ||
e556fdbd RK |
918 | irq = platform_get_irq(pdev, 0); |
919 | if (irq <= 0) | |
920 | return irq < 0 ? irq : -ENXIO; | |
921 | ||
1da177e4 LT |
922 | err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY; |
923 | if (err) | |
924 | goto err_mem_1; | |
925 | err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY; | |
926 | if (err) | |
927 | goto err_mem_2; | |
928 | err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY; | |
929 | if (err) | |
930 | goto err_mem_3; | |
931 | ||
932 | dev = alloc_irdadev(sizeof(struct sa1100_irda)); | |
933 | if (!dev) | |
934 | goto err_mem_4; | |
935 | ||
d3238608 RK |
936 | SET_NETDEV_DEV(dev, &pdev->dev); |
937 | ||
4cf1653a | 938 | si = netdev_priv(dev); |
1da177e4 LT |
939 | si->dev = &pdev->dev; |
940 | si->pdata = pdev->dev.platform_data; | |
941 | ||
942 | /* | |
943 | * Initialise the HP-SIR buffers | |
944 | */ | |
945 | err = sa1100_irda_init_iobuf(&si->rx_buff, 14384); | |
946 | if (err) | |
947 | goto err_mem_5; | |
948 | err = sa1100_irda_init_iobuf(&si->tx_buff, 4000); | |
949 | if (err) | |
950 | goto err_mem_5; | |
951 | ||
a1de9666 | 952 | dev->netdev_ops = &sa1100_irda_netdev_ops; |
e556fdbd | 953 | dev->irq = irq; |
1da177e4 LT |
954 | |
955 | irda_init_max_qos_capabilies(&si->qos); | |
956 | ||
957 | /* | |
958 | * We support original IRDA up to 115k2. (we don't currently | |
959 | * support 4Mbps). Min Turn Time set to 1ms or greater. | |
960 | */ | |
961 | baudrate_mask = IR_9600; | |
962 | ||
963 | switch (max_rate) { | |
964 | case 4000000: baudrate_mask |= IR_4000000 << 8; | |
965 | case 115200: baudrate_mask |= IR_115200; | |
966 | case 57600: baudrate_mask |= IR_57600; | |
967 | case 38400: baudrate_mask |= IR_38400; | |
968 | case 19200: baudrate_mask |= IR_19200; | |
969 | } | |
970 | ||
971 | si->qos.baud_rate.bits &= baudrate_mask; | |
972 | si->qos.min_turn_time.bits = 7; | |
973 | ||
974 | irda_qos_bits_to_value(&si->qos); | |
975 | ||
976 | si->utcr4 = UTCR4_HPSIR; | |
977 | if (tx_lpm) | |
978 | si->utcr4 |= UTCR4_Z1_6us; | |
979 | ||
980 | /* | |
981 | * Initially enable HP-SIR modulation, and ensure that the port | |
982 | * is disabled. | |
983 | */ | |
984 | Ser2UTCR3 = 0; | |
985 | Ser2UTCR4 = si->utcr4; | |
986 | Ser2HSCR0 = HSCR0_UART; | |
987 | ||
988 | err = register_netdev(dev); | |
989 | if (err == 0) | |
3ae5eaec | 990 | platform_set_drvdata(pdev, dev); |
1da177e4 LT |
991 | |
992 | if (err) { | |
993 | err_mem_5: | |
994 | kfree(si->tx_buff.head); | |
995 | kfree(si->rx_buff.head); | |
996 | free_netdev(dev); | |
997 | err_mem_4: | |
998 | release_mem_region(__PREG(Ser2HSCR2), 0x04); | |
999 | err_mem_3: | |
1000 | release_mem_region(__PREG(Ser2HSCR0), 0x1c); | |
1001 | err_mem_2: | |
1002 | release_mem_region(__PREG(Ser2UTCR0), 0x24); | |
1003 | } | |
1004 | err_mem_1: | |
1005 | return err; | |
1006 | } | |
1007 | ||
3ae5eaec | 1008 | static int sa1100_irda_remove(struct platform_device *pdev) |
1da177e4 | 1009 | { |
3ae5eaec | 1010 | struct net_device *dev = platform_get_drvdata(pdev); |
1da177e4 LT |
1011 | |
1012 | if (dev) { | |
4cf1653a | 1013 | struct sa1100_irda *si = netdev_priv(dev); |
1da177e4 LT |
1014 | unregister_netdev(dev); |
1015 | kfree(si->tx_buff.head); | |
1016 | kfree(si->rx_buff.head); | |
1017 | free_netdev(dev); | |
1018 | } | |
1019 | ||
1020 | release_mem_region(__PREG(Ser2HSCR2), 0x04); | |
1021 | release_mem_region(__PREG(Ser2HSCR0), 0x1c); | |
1022 | release_mem_region(__PREG(Ser2UTCR0), 0x24); | |
1023 | ||
1024 | return 0; | |
1025 | } | |
1026 | ||
3ae5eaec | 1027 | static struct platform_driver sa1100ir_driver = { |
1da177e4 LT |
1028 | .probe = sa1100_irda_probe, |
1029 | .remove = sa1100_irda_remove, | |
1030 | .suspend = sa1100_irda_suspend, | |
1031 | .resume = sa1100_irda_resume, | |
3ae5eaec RK |
1032 | .driver = { |
1033 | .name = "sa11x0-ir", | |
72abb461 | 1034 | .owner = THIS_MODULE, |
3ae5eaec | 1035 | }, |
1da177e4 LT |
1036 | }; |
1037 | ||
1038 | static int __init sa1100_irda_init(void) | |
1039 | { | |
1040 | /* | |
1041 | * Limit power level a sensible range. | |
1042 | */ | |
1043 | if (power_level < 1) | |
1044 | power_level = 1; | |
1045 | if (power_level > 3) | |
1046 | power_level = 3; | |
1047 | ||
3ae5eaec | 1048 | return platform_driver_register(&sa1100ir_driver); |
1da177e4 LT |
1049 | } |
1050 | ||
1051 | static void __exit sa1100_irda_exit(void) | |
1052 | { | |
3ae5eaec | 1053 | platform_driver_unregister(&sa1100ir_driver); |
1da177e4 LT |
1054 | } |
1055 | ||
1056 | module_init(sa1100_irda_init); | |
1057 | module_exit(sa1100_irda_exit); | |
1058 | module_param(power_level, int, 0); | |
1059 | module_param(tx_lpm, int, 0); | |
1060 | module_param(max_rate, int, 0); | |
1061 | ||
1062 | MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); | |
1063 | MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver"); | |
1064 | MODULE_LICENSE("GPL"); | |
1065 | MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)"); | |
1066 | MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode"); | |
1067 | MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)"); | |
72abb461 | 1068 | MODULE_ALIAS("platform:sa11x0-ir"); |