blob: 84fecce078f6c5b75fe33fb60b6eae502ebd0e3e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Russell King15877e92012-01-08 12:04:05 +000018 * power_level:level - set the transmitter power level
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * tx_lpm:0|1 - set transmit low power mode
20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Russell Kingd052d1b2005-10-29 19:07:23 +010031#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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
38#include <asm/irq.h>
Russell Kingd281bc92008-12-01 23:01:19 +000039#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010040#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/mach/irda.h>
42
43static int power_level = 3;
44static int tx_lpm;
45static int max_rate = 4000000;
46
47struct sa1100_irda {
48 unsigned char hscr0;
49 unsigned char utcr4;
50 unsigned char power;
51 unsigned char open;
52
53 int speed;
54 int newspeed;
55
56 struct sk_buff *txskb;
57 struct sk_buff *rxskb;
58 dma_addr_t txbuf_dma;
59 dma_addr_t rxbuf_dma;
60 dma_regs_t *txdma;
61 dma_regs_t *rxdma;
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct device *dev;
64 struct irda_platform_data *pdata;
65 struct irlap_cb *irlap;
66 struct qos_info qos;
67
68 iobuff_t tx_buff;
69 iobuff_t rx_buff;
70};
71
72#define IS_FIR(si) ((si)->speed >= 4000000)
73
74#define HPSIR_MAX_RXLEN 2047
75
76/*
77 * Allocate and map the receive buffer, unless it is already allocated.
78 */
79static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
80{
81 if (si->rxskb)
82 return 0;
83
84 si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (!si->rxskb) {
86 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
87 return -ENOMEM;
88 }
89
90 /*
91 * Align any IP headers that may be contained
92 * within the frame.
93 */
94 skb_reserve(si->rxskb, 1);
95
96 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
97 HPSIR_MAX_RXLEN,
98 DMA_FROM_DEVICE);
Russell King22f0bf92012-01-08 13:55:23 +000099 if (dma_mapping_error(si->dev, si->rxbuf_dma)) {
100 dev_kfree_skb_any(si->rxskb);
101 return -ENOMEM;
102 }
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 return 0;
105}
106
107/*
108 * We want to get here as soon as possible, and get the receiver setup.
109 * We use the existing buffer.
110 */
111static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
112{
113 if (!si->rxskb) {
114 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
115 return;
116 }
117
118 /*
119 * First empty receive FIFO
120 */
121 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
122
123 /*
124 * Enable the DMA, receiver and receive interrupt.
125 */
126 sa1100_clear_dma(si->rxdma);
127 sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);
128 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
129}
130
131/*
132 * Set the IrDA communications speed.
133 */
134static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
135{
136 unsigned long flags;
137 int brd, ret = -EINVAL;
138
139 switch (speed) {
140 case 9600: case 19200: case 38400:
141 case 57600: case 115200:
142 brd = 3686400 / (16 * speed) - 1;
143
144 /*
145 * Stop the receive DMA.
146 */
147 if (IS_FIR(si))
148 sa1100_stop_dma(si->rxdma);
149
150 local_irq_save(flags);
151
152 Ser2UTCR3 = 0;
153 Ser2HSCR0 = HSCR0_UART;
154
155 Ser2UTCR1 = brd >> 8;
156 Ser2UTCR2 = brd;
157
158 /*
159 * Clear status register
160 */
161 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
162 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
163
164 if (si->pdata->set_speed)
165 si->pdata->set_speed(si->dev, speed);
166
167 si->speed = speed;
168
169 local_irq_restore(flags);
170 ret = 0;
171 break;
172
173 case 4000000:
174 local_irq_save(flags);
175
176 si->hscr0 = 0;
177
178 Ser2HSSR0 = 0xff;
179 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
180 Ser2UTCR3 = 0;
181
182 si->speed = speed;
183
184 if (si->pdata->set_speed)
185 si->pdata->set_speed(si->dev, speed);
186
187 sa1100_irda_rx_alloc(si);
188 sa1100_irda_rx_dma_start(si);
189
190 local_irq_restore(flags);
191
192 break;
193
194 default:
195 break;
196 }
197
198 return ret;
199}
200
201/*
202 * Control the power state of the IrDA transmitter.
203 * State:
204 * 0 - off
205 * 1 - short range, lowest power
206 * 2 - medium range, medium power
207 * 3 - maximum range, high power
208 *
209 * Currently, only assabet is known to support this.
210 */
211static int
212__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
213{
214 int ret = 0;
215 if (si->pdata->set_power)
216 ret = si->pdata->set_power(si->dev, state);
217 return ret;
218}
219
220static inline int
221sa1100_set_power(struct sa1100_irda *si, unsigned int state)
222{
223 int ret;
224
225 ret = __sa1100_irda_set_power(si, state);
226 if (ret == 0)
227 si->power = state;
228
229 return ret;
230}
231
232static int sa1100_irda_startup(struct sa1100_irda *si)
233{
234 int ret;
235
236 /*
237 * Ensure that the ports for this device are setup correctly.
238 */
Dmitry Artamonow91cd1752009-10-13 03:17:37 -0700239 if (si->pdata->startup) {
240 ret = si->pdata->startup(si->dev);
241 if (ret)
242 return ret;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /*
246 * Configure PPC for IRDA - we want to drive TXD2 low.
247 * We also want to drive this pin low during sleep.
248 */
249 PPSR &= ~PPC_TXD2;
250 PSDR &= ~PPC_TXD2;
251 PPDR |= PPC_TXD2;
252
253 /*
254 * Enable HP-SIR modulation, and ensure that the port is disabled.
255 */
256 Ser2UTCR3 = 0;
257 Ser2HSCR0 = HSCR0_UART;
258 Ser2UTCR4 = si->utcr4;
259 Ser2UTCR0 = UTCR0_8BitData;
260 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
261
262 /*
263 * Clear status register
264 */
265 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
266
267 ret = sa1100_irda_set_speed(si, si->speed = 9600);
268 if (ret) {
269 Ser2UTCR3 = 0;
270 Ser2HSCR0 = 0;
271
272 if (si->pdata->shutdown)
273 si->pdata->shutdown(si->dev);
274 }
275
276 return ret;
277}
278
279static void sa1100_irda_shutdown(struct sa1100_irda *si)
280{
281 /*
282 * Stop all DMA activity.
283 */
284 sa1100_stop_dma(si->rxdma);
285 sa1100_stop_dma(si->txdma);
286
287 /* Disable the port. */
288 Ser2UTCR3 = 0;
289 Ser2HSCR0 = 0;
290
291 if (si->pdata->shutdown)
292 si->pdata->shutdown(si->dev);
293}
294
295#ifdef CONFIG_PM
296/*
297 * Suspend the IrDA interface.
298 */
Russell King3ae5eae2005-11-09 22:32:44 +0000299static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Russell King3ae5eae2005-11-09 22:32:44 +0000301 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct sa1100_irda *si;
303
Russell King9480e302005-10-28 09:52:56 -0700304 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return 0;
306
Wang Chen4cf16532008-11-12 23:38:14 -0800307 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (si->open) {
309 /*
310 * Stop the transmit queue
311 */
312 netif_device_detach(dev);
313 disable_irq(dev->irq);
314 sa1100_irda_shutdown(si);
315 __sa1100_irda_set_power(si, 0);
316 }
317
318 return 0;
319}
320
321/*
322 * Resume the IrDA interface.
323 */
Russell King3ae5eae2005-11-09 22:32:44 +0000324static int sa1100_irda_resume(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Russell King3ae5eae2005-11-09 22:32:44 +0000326 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct sa1100_irda *si;
328
Russell King9480e302005-10-28 09:52:56 -0700329 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
331
Wang Chen4cf16532008-11-12 23:38:14 -0800332 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (si->open) {
334 /*
335 * If we missed a speed change, initialise at the new speed
336 * directly. It is debatable whether this is actually
337 * required, but in the interests of continuing from where
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800338 * we left off it is desirable. The converse argument is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * that we should re-negotiate at 9600 baud again.
340 */
341 if (si->newspeed) {
342 si->speed = si->newspeed;
343 si->newspeed = 0;
344 }
345
346 sa1100_irda_startup(si);
347 __sa1100_irda_set_power(si, si->power);
348 enable_irq(dev->irq);
349
350 /*
351 * This automatically wakes up the queue
352 */
353 netif_device_attach(dev);
354 }
355
356 return 0;
357}
358#else
359#define sa1100_irda_suspend NULL
360#define sa1100_irda_resume NULL
361#endif
362
363/*
364 * HP-SIR format interrupt service routines.
365 */
366static void sa1100_irda_hpsir_irq(struct net_device *dev)
367{
Wang Chen4cf16532008-11-12 23:38:14 -0800368 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int status;
370
371 status = Ser2UTSR0;
372
373 /*
374 * Deal with any receive errors first. The bytes in error may be
375 * the only bytes in the receive FIFO, so we do this first.
376 */
377 while (status & UTSR0_EIF) {
378 int stat, data;
379
380 stat = Ser2UTSR1;
381 data = Ser2UTDR;
382
383 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800384 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (stat & UTSR1_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800386 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (stat & UTSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800388 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 } else
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800390 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 status = Ser2UTSR0;
393 }
394
395 /*
396 * We must clear certain bits.
397 */
398 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
399
400 if (status & UTSR0_RFS) {
401 /*
402 * There are at least 4 bytes in the FIFO. Read 3 bytes
403 * and leave the rest to the block below.
404 */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800405 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
406 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
407 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409
410 if (status & (UTSR0_RFS | UTSR0_RID)) {
411 /*
412 * Fifo contains more than 1 character.
413 */
414 do {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800415 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 Ser2UTDR);
417 } while (Ser2UTSR1 & UTSR1_RNE);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
421 if (status & UTSR0_TFS && si->tx_buff.len) {
422 /*
423 * Transmitter FIFO is not full
424 */
425 do {
426 Ser2UTDR = *si->tx_buff.data++;
427 si->tx_buff.len -= 1;
428 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
429
430 if (si->tx_buff.len == 0) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800431 dev->stats.tx_packets++;
432 dev->stats.tx_bytes += si->tx_buff.data -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 si->tx_buff.head;
434
435 /*
436 * We need to ensure that the transmitter has
437 * finished.
438 */
439 do
440 rmb();
441 while (Ser2UTSR1 & UTSR1_TBY);
442
443 /*
444 * Ok, we've finished transmitting. Now enable
445 * the receiver. Sometimes we get a receive IRQ
446 * immediately after a transmit...
447 */
448 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
449 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
450
451 if (si->newspeed) {
452 sa1100_irda_set_speed(si, si->newspeed);
453 si->newspeed = 0;
454 }
455
456 /* I'm hungry! */
457 netif_wake_queue(dev);
458 }
459 }
460}
461
462static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
463{
464 struct sk_buff *skb = si->rxskb;
465 dma_addr_t dma_addr;
466 unsigned int len, stat, data;
467
468 if (!skb) {
469 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
470 return;
471 }
472
473 /*
474 * Get the current data position.
475 */
476 dma_addr = sa1100_get_dma_pos(si->rxdma);
477 len = dma_addr - si->rxbuf_dma;
478 if (len > HPSIR_MAX_RXLEN)
479 len = HPSIR_MAX_RXLEN;
480 dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);
481
482 do {
483 /*
484 * Read Status, and then Data.
485 */
486 stat = Ser2HSSR1;
487 rmb();
488 data = Ser2HSDR;
489
490 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800491 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (stat & HSSR1_CRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800493 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (stat & HSSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800495 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 } else
497 skb->data[len++] = data;
498
499 /*
500 * If we hit the end of frame, there's
501 * no point in continuing.
502 */
503 if (stat & HSSR1_EOF)
504 break;
505 } while (Ser2HSSR0 & HSSR0_EIF);
506
507 if (stat & HSSR1_EOF) {
508 si->rxskb = NULL;
509
510 skb_put(skb, len);
511 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700512 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 skb->protocol = htons(ETH_P_IRDA);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800514 dev->stats.rx_packets++;
515 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /*
518 * Before we pass the buffer up, allocate a new one.
519 */
520 sa1100_irda_rx_alloc(si);
521
522 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 } else {
524 /*
Russell King22f0bf92012-01-08 13:55:23 +0000525 * Remap the buffer - it was previously mapped, and we
526 * hope that this succeeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
529 HPSIR_MAX_RXLEN,
530 DMA_FROM_DEVICE);
531 }
532}
533
534/*
535 * FIR format interrupt service routine. We only have to
536 * handle RX events; transmit events go via the TX DMA handler.
537 *
538 * No matter what, we disable RX, process, and the restart RX.
539 */
540static void sa1100_irda_fir_irq(struct net_device *dev)
541{
Wang Chen4cf16532008-11-12 23:38:14 -0800542 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /*
545 * Stop RX DMA
546 */
547 sa1100_stop_dma(si->rxdma);
548
549 /*
550 * Framing error - we throw away the packet completely.
551 * Clearing RXE flushes the error conditions and data
552 * from the fifo.
553 */
554 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800555 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 if (Ser2HSSR0 & HSSR0_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800558 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /*
561 * Clear out the DMA...
562 */
563 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
564
565 /*
566 * Clear selected status bits now, so we
567 * don't miss them next time around.
568 */
569 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
570 }
571
572 /*
573 * Deal with any receive errors. The any of the lowest
574 * 8 bytes in the FIFO may contain an error. We must read
575 * them one by one. The "error" could even be the end of
576 * packet!
577 */
578 if (Ser2HSSR0 & HSSR0_EIF)
579 sa1100_irda_fir_error(si, dev);
580
581 /*
582 * No matter what happens, we must restart reception.
583 */
584 sa1100_irda_rx_dma_start(si);
585}
586
David Howells7d12e782006-10-05 14:55:46 +0100587static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800590 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 sa1100_irda_fir_irq(dev);
592 else
593 sa1100_irda_hpsir_irq(dev);
594 return IRQ_HANDLED;
595}
596
597/*
598 * TX DMA completion handler.
599 */
600static void sa1100_irda_txdma_irq(void *id)
601{
602 struct net_device *dev = id;
Wang Chen4cf16532008-11-12 23:38:14 -0800603 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 struct sk_buff *skb = si->txskb;
605
606 si->txskb = NULL;
607
608 /*
609 * Wait for the transmission to complete. Unfortunately,
610 * the hardware doesn't give us an interrupt to indicate
611 * "end of frame".
612 */
613 do
614 rmb();
615 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
616
617 /*
618 * Clear the transmit underrun bit.
619 */
620 Ser2HSSR0 = HSSR0_TUR;
621
622 /*
623 * Do we need to change speed? Note that we're lazy
624 * here - we don't free the old rxskb. We don't need
625 * to allocate a buffer either.
626 */
627 if (si->newspeed) {
628 sa1100_irda_set_speed(si, si->newspeed);
629 si->newspeed = 0;
630 }
631
632 /*
633 * Start reception. This disables the transmitter for
634 * us. This will be using the existing RX buffer.
635 */
636 sa1100_irda_rx_dma_start(si);
637
638 /*
639 * Account and free the packet.
640 */
641 if (skb) {
642 dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800643 dev->stats.tx_packets ++;
644 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 dev_kfree_skb_irq(skb);
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
655static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
656{
Wang Chen4cf16532008-11-12 23:38:14 -0800657 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 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);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000677 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
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 */
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200702 BUG_ON(si->txskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 netif_stop_queue(dev);
705
706 si->txskb = skb;
707 si->txbuf_dma = dma_map_single(si->dev, skb->data,
708 skb->len, DMA_TO_DEVICE);
Russell King22f0bf92012-01-08 13:55:23 +0000709 if (dma_mapping_error(si->dev, si->txbuf_dma)) {
710 si->txskb = NULL;
711 netif_wake_queue(dev);
712 dev->stats.tx_dropped++;
713 dev_kfree_skb(skb);
714 return NETDEV_TX_OK;
715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
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
Patrick McHardy6ed10652009-06-23 06:03:08 +0000730 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733static int
734sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
735{
736 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800737 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static int sa1100_irda_start(struct net_device *dev)
778{
Wang Chen4cf16532008-11-12 23:38:14 -0800779 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 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",
789 NULL, NULL, &si->rxdma);
790 if (err)
791 goto err_rx_dma;
792
793 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
794 sa1100_irda_txdma_irq, dev, &si->txdma);
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
827err_irlap:
828 si->open = 0;
829 sa1100_irda_shutdown(si);
830err_startup:
831 sa1100_free_dma(si->txdma);
832err_tx_dma:
833 sa1100_free_dma(si->rxdma);
834err_rx_dma:
835 free_irq(dev->irq, dev);
836err_irq:
837 return err;
838}
839
840static int sa1100_irda_stop(struct net_device *dev)
841{
Wang Chen4cf16532008-11-12 23:38:14 -0800842 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 disable_irq(dev->irq);
845 sa1100_irda_shutdown(si);
846
847 /*
848 * If we have been doing DMA receive, make sure we
849 * tidy that up cleanly.
850 */
851 if (si->rxskb) {
852 dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
853 DMA_FROM_DEVICE);
854 dev_kfree_skb(si->rxskb);
855 si->rxskb = NULL;
856 }
857
858 /* Stop IrLAP */
859 if (si->irlap) {
860 irlap_close(si->irlap);
861 si->irlap = NULL;
862 }
863
864 netif_stop_queue(dev);
865 si->open = 0;
866
867 /*
868 * Free resources
869 */
870 sa1100_free_dma(si->txdma);
871 sa1100_free_dma(si->rxdma);
872 free_irq(dev->irq, dev);
873
874 sa1100_set_power(si, 0);
875
876 return 0;
877}
878
879static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
880{
881 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
882 if (io->head != NULL) {
883 io->truesize = size;
884 io->in_frame = FALSE;
885 io->state = OUTSIDE_FRAME;
886 io->data = io->head;
887 }
888 return io->head ? 0 : -ENOMEM;
889}
890
Alexander Beregalova1de9662009-04-15 12:52:42 +0000891static const struct net_device_ops sa1100_irda_netdev_ops = {
892 .ndo_open = sa1100_irda_start,
893 .ndo_stop = sa1100_irda_stop,
894 .ndo_start_xmit = sa1100_irda_hard_xmit,
895 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000896};
897
Russell King3ae5eae2005-11-09 22:32:44 +0000898static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct net_device *dev;
901 struct sa1100_irda *si;
902 unsigned int baudrate_mask;
903 int err;
904
905 if (!pdev->dev.platform_data)
906 return -EINVAL;
907
908 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
909 if (err)
910 goto err_mem_1;
911 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
912 if (err)
913 goto err_mem_2;
914 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
915 if (err)
916 goto err_mem_3;
917
918 dev = alloc_irdadev(sizeof(struct sa1100_irda));
919 if (!dev)
920 goto err_mem_4;
921
Wang Chen4cf16532008-11-12 23:38:14 -0800922 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 si->dev = &pdev->dev;
924 si->pdata = pdev->dev.platform_data;
925
926 /*
927 * Initialise the HP-SIR buffers
928 */
929 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
930 if (err)
931 goto err_mem_5;
932 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
933 if (err)
934 goto err_mem_5;
935
Alexander Beregalova1de9662009-04-15 12:52:42 +0000936 dev->netdev_ops = &sa1100_irda_netdev_ops;
Alexander Beregalovbf98a822009-04-21 18:12:11 -0700937 dev->irq = IRQ_Ser2ICP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 irda_init_max_qos_capabilies(&si->qos);
940
941 /*
942 * We support original IRDA up to 115k2. (we don't currently
943 * support 4Mbps). Min Turn Time set to 1ms or greater.
944 */
945 baudrate_mask = IR_9600;
946
947 switch (max_rate) {
948 case 4000000: baudrate_mask |= IR_4000000 << 8;
949 case 115200: baudrate_mask |= IR_115200;
950 case 57600: baudrate_mask |= IR_57600;
951 case 38400: baudrate_mask |= IR_38400;
952 case 19200: baudrate_mask |= IR_19200;
953 }
954
955 si->qos.baud_rate.bits &= baudrate_mask;
956 si->qos.min_turn_time.bits = 7;
957
958 irda_qos_bits_to_value(&si->qos);
959
960 si->utcr4 = UTCR4_HPSIR;
961 if (tx_lpm)
962 si->utcr4 |= UTCR4_Z1_6us;
963
964 /*
965 * Initially enable HP-SIR modulation, and ensure that the port
966 * is disabled.
967 */
968 Ser2UTCR3 = 0;
969 Ser2UTCR4 = si->utcr4;
970 Ser2HSCR0 = HSCR0_UART;
971
972 err = register_netdev(dev);
973 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000974 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (err) {
977 err_mem_5:
978 kfree(si->tx_buff.head);
979 kfree(si->rx_buff.head);
980 free_netdev(dev);
981 err_mem_4:
982 release_mem_region(__PREG(Ser2HSCR2), 0x04);
983 err_mem_3:
984 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
985 err_mem_2:
986 release_mem_region(__PREG(Ser2UTCR0), 0x24);
987 }
988 err_mem_1:
989 return err;
990}
991
Russell King3ae5eae2005-11-09 22:32:44 +0000992static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Russell King3ae5eae2005-11-09 22:32:44 +0000994 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -0800997 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 unregister_netdev(dev);
999 kfree(si->tx_buff.head);
1000 kfree(si->rx_buff.head);
1001 free_netdev(dev);
1002 }
1003
1004 release_mem_region(__PREG(Ser2HSCR2), 0x04);
1005 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1006 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1007
1008 return 0;
1009}
1010
Russell King3ae5eae2005-11-09 22:32:44 +00001011static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 .probe = sa1100_irda_probe,
1013 .remove = sa1100_irda_remove,
1014 .suspend = sa1100_irda_suspend,
1015 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001016 .driver = {
1017 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001018 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001019 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020};
1021
1022static int __init sa1100_irda_init(void)
1023{
1024 /*
1025 * Limit power level a sensible range.
1026 */
1027 if (power_level < 1)
1028 power_level = 1;
1029 if (power_level > 3)
1030 power_level = 3;
1031
Russell King3ae5eae2005-11-09 22:32:44 +00001032 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035static void __exit sa1100_irda_exit(void)
1036{
Russell King3ae5eae2005-11-09 22:32:44 +00001037 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040module_init(sa1100_irda_init);
1041module_exit(sa1100_irda_exit);
1042module_param(power_level, int, 0);
1043module_param(tx_lpm, int, 0);
1044module_param(max_rate, int, 0);
1045
1046MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1047MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1048MODULE_LICENSE("GPL");
1049MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1050MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1051MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001052MODULE_ALIAS("platform:sa11x0-ir");