blob: 2f5bf0bc8458c48ad5a0423f5977f01d4e1da8f4 [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
Russell Kingd281bc92008-12-01 23:01:19 +000038#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/mach/irda.h>
41
42static int power_level = 3;
43static int tx_lpm;
44static int max_rate = 4000000;
45
46struct sa1100_irda {
47 unsigned char hscr0;
48 unsigned char utcr4;
49 unsigned char power;
50 unsigned char open;
51
52 int speed;
53 int newspeed;
54
55 struct sk_buff *txskb;
56 struct sk_buff *rxskb;
57 dma_addr_t txbuf_dma;
58 dma_addr_t rxbuf_dma;
59 dma_regs_t *txdma;
60 dma_regs_t *rxdma;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct device *dev;
63 struct irda_platform_data *pdata;
64 struct irlap_cb *irlap;
65 struct qos_info qos;
66
67 iobuff_t tx_buff;
68 iobuff_t rx_buff;
69};
70
71#define IS_FIR(si) ((si)->speed >= 4000000)
72
73#define HPSIR_MAX_RXLEN 2047
74
75/*
76 * Allocate and map the receive buffer, unless it is already allocated.
77 */
78static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
79{
80 if (si->rxskb)
81 return 0;
82
83 si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!si->rxskb) {
85 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
86 return -ENOMEM;
87 }
88
89 /*
90 * Align any IP headers that may be contained
91 * within the frame.
92 */
93 skb_reserve(si->rxskb, 1);
94
95 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
96 HPSIR_MAX_RXLEN,
97 DMA_FROM_DEVICE);
Russell King22f0bf92012-01-08 13:55:23 +000098 if (dma_mapping_error(si->dev, si->rxbuf_dma)) {
99 dev_kfree_skb_any(si->rxskb);
100 return -ENOMEM;
101 }
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104}
105
106/*
107 * We want to get here as soon as possible, and get the receiver setup.
108 * We use the existing buffer.
109 */
110static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
111{
112 if (!si->rxskb) {
113 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
114 return;
115 }
116
117 /*
118 * First empty receive FIFO
119 */
120 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
121
122 /*
123 * Enable the DMA, receiver and receive interrupt.
124 */
125 sa1100_clear_dma(si->rxdma);
126 sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);
127 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
128}
129
130/*
131 * Set the IrDA communications speed.
132 */
133static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
134{
135 unsigned long flags;
136 int brd, ret = -EINVAL;
137
138 switch (speed) {
139 case 9600: case 19200: case 38400:
140 case 57600: case 115200:
141 brd = 3686400 / (16 * speed) - 1;
142
143 /*
144 * Stop the receive DMA.
145 */
146 if (IS_FIR(si))
147 sa1100_stop_dma(si->rxdma);
148
149 local_irq_save(flags);
150
151 Ser2UTCR3 = 0;
152 Ser2HSCR0 = HSCR0_UART;
153
154 Ser2UTCR1 = brd >> 8;
155 Ser2UTCR2 = brd;
156
157 /*
158 * Clear status register
159 */
160 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
161 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
162
163 if (si->pdata->set_speed)
164 si->pdata->set_speed(si->dev, speed);
165
166 si->speed = speed;
167
168 local_irq_restore(flags);
169 ret = 0;
170 break;
171
172 case 4000000:
173 local_irq_save(flags);
174
175 si->hscr0 = 0;
176
177 Ser2HSSR0 = 0xff;
178 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
179 Ser2UTCR3 = 0;
180
181 si->speed = speed;
182
183 if (si->pdata->set_speed)
184 si->pdata->set_speed(si->dev, speed);
185
186 sa1100_irda_rx_alloc(si);
187 sa1100_irda_rx_dma_start(si);
188
189 local_irq_restore(flags);
190
191 break;
192
193 default:
194 break;
195 }
196
197 return ret;
198}
199
200/*
201 * Control the power state of the IrDA transmitter.
202 * State:
203 * 0 - off
204 * 1 - short range, lowest power
205 * 2 - medium range, medium power
206 * 3 - maximum range, high power
207 *
208 * Currently, only assabet is known to support this.
209 */
210static int
211__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
212{
213 int ret = 0;
214 if (si->pdata->set_power)
215 ret = si->pdata->set_power(si->dev, state);
216 return ret;
217}
218
219static inline int
220sa1100_set_power(struct sa1100_irda *si, unsigned int state)
221{
222 int ret;
223
224 ret = __sa1100_irda_set_power(si, state);
225 if (ret == 0)
226 si->power = state;
227
228 return ret;
229}
230
231static int sa1100_irda_startup(struct sa1100_irda *si)
232{
233 int ret;
234
235 /*
236 * Ensure that the ports for this device are setup correctly.
237 */
Dmitry Artamonow91cd1752009-10-13 03:17:37 -0700238 if (si->pdata->startup) {
239 ret = si->pdata->startup(si->dev);
240 if (ret)
241 return ret;
242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /*
245 * Configure PPC for IRDA - we want to drive TXD2 low.
246 * We also want to drive this pin low during sleep.
247 */
248 PPSR &= ~PPC_TXD2;
249 PSDR &= ~PPC_TXD2;
250 PPDR |= PPC_TXD2;
251
252 /*
253 * Enable HP-SIR modulation, and ensure that the port is disabled.
254 */
255 Ser2UTCR3 = 0;
256 Ser2HSCR0 = HSCR0_UART;
257 Ser2UTCR4 = si->utcr4;
258 Ser2UTCR0 = UTCR0_8BitData;
259 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
260
261 /*
262 * Clear status register
263 */
264 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
265
266 ret = sa1100_irda_set_speed(si, si->speed = 9600);
267 if (ret) {
268 Ser2UTCR3 = 0;
269 Ser2HSCR0 = 0;
270
271 if (si->pdata->shutdown)
272 si->pdata->shutdown(si->dev);
273 }
274
275 return ret;
276}
277
278static void sa1100_irda_shutdown(struct sa1100_irda *si)
279{
280 /*
281 * Stop all DMA activity.
282 */
283 sa1100_stop_dma(si->rxdma);
284 sa1100_stop_dma(si->txdma);
285
286 /* Disable the port. */
287 Ser2UTCR3 = 0;
288 Ser2HSCR0 = 0;
289
290 if (si->pdata->shutdown)
291 si->pdata->shutdown(si->dev);
292}
293
294#ifdef CONFIG_PM
295/*
296 * Suspend the IrDA interface.
297 */
Russell King3ae5eae2005-11-09 22:32:44 +0000298static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Russell King3ae5eae2005-11-09 22:32:44 +0000300 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 struct sa1100_irda *si;
302
Russell King9480e302005-10-28 09:52:56 -0700303 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305
Wang Chen4cf16532008-11-12 23:38:14 -0800306 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (si->open) {
308 /*
309 * Stop the transmit queue
310 */
311 netif_device_detach(dev);
312 disable_irq(dev->irq);
313 sa1100_irda_shutdown(si);
314 __sa1100_irda_set_power(si, 0);
315 }
316
317 return 0;
318}
319
320/*
321 * Resume the IrDA interface.
322 */
Russell King3ae5eae2005-11-09 22:32:44 +0000323static int sa1100_irda_resume(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Russell King3ae5eae2005-11-09 22:32:44 +0000325 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct sa1100_irda *si;
327
Russell King9480e302005-10-28 09:52:56 -0700328 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return 0;
330
Wang Chen4cf16532008-11-12 23:38:14 -0800331 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (si->open) {
333 /*
334 * If we missed a speed change, initialise at the new speed
335 * directly. It is debatable whether this is actually
336 * required, but in the interests of continuing from where
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800337 * we left off it is desirable. The converse argument is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * that we should re-negotiate at 9600 baud again.
339 */
340 if (si->newspeed) {
341 si->speed = si->newspeed;
342 si->newspeed = 0;
343 }
344
345 sa1100_irda_startup(si);
346 __sa1100_irda_set_power(si, si->power);
347 enable_irq(dev->irq);
348
349 /*
350 * This automatically wakes up the queue
351 */
352 netif_device_attach(dev);
353 }
354
355 return 0;
356}
357#else
358#define sa1100_irda_suspend NULL
359#define sa1100_irda_resume NULL
360#endif
361
362/*
363 * HP-SIR format interrupt service routines.
364 */
365static void sa1100_irda_hpsir_irq(struct net_device *dev)
366{
Wang Chen4cf16532008-11-12 23:38:14 -0800367 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 int status;
369
370 status = Ser2UTSR0;
371
372 /*
373 * Deal with any receive errors first. The bytes in error may be
374 * the only bytes in the receive FIFO, so we do this first.
375 */
376 while (status & UTSR0_EIF) {
377 int stat, data;
378
379 stat = Ser2UTSR1;
380 data = Ser2UTDR;
381
382 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800383 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (stat & UTSR1_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800385 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (stat & UTSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800387 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 } else
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800389 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 status = Ser2UTSR0;
392 }
393
394 /*
395 * We must clear certain bits.
396 */
397 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
398
399 if (status & UTSR0_RFS) {
400 /*
401 * There are at least 4 bytes in the FIFO. Read 3 bytes
402 * and leave the rest to the block below.
403 */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800404 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
405 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
406 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 }
408
409 if (status & (UTSR0_RFS | UTSR0_RID)) {
410 /*
411 * Fifo contains more than 1 character.
412 */
413 do {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800414 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 Ser2UTDR);
416 } while (Ser2UTSR1 & UTSR1_RNE);
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 if (status & UTSR0_TFS && si->tx_buff.len) {
421 /*
422 * Transmitter FIFO is not full
423 */
424 do {
425 Ser2UTDR = *si->tx_buff.data++;
426 si->tx_buff.len -= 1;
427 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
428
429 if (si->tx_buff.len == 0) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800430 dev->stats.tx_packets++;
431 dev->stats.tx_bytes += si->tx_buff.data -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 si->tx_buff.head;
433
434 /*
435 * We need to ensure that the transmitter has
436 * finished.
437 */
438 do
439 rmb();
440 while (Ser2UTSR1 & UTSR1_TBY);
441
442 /*
443 * Ok, we've finished transmitting. Now enable
444 * the receiver. Sometimes we get a receive IRQ
445 * immediately after a transmit...
446 */
447 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
448 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
449
450 if (si->newspeed) {
451 sa1100_irda_set_speed(si, si->newspeed);
452 si->newspeed = 0;
453 }
454
455 /* I'm hungry! */
456 netif_wake_queue(dev);
457 }
458 }
459}
460
461static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
462{
463 struct sk_buff *skb = si->rxskb;
464 dma_addr_t dma_addr;
465 unsigned int len, stat, data;
466
467 if (!skb) {
468 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
469 return;
470 }
471
472 /*
473 * Get the current data position.
474 */
475 dma_addr = sa1100_get_dma_pos(si->rxdma);
476 len = dma_addr - si->rxbuf_dma;
477 if (len > HPSIR_MAX_RXLEN)
478 len = HPSIR_MAX_RXLEN;
479 dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);
480
481 do {
482 /*
483 * Read Status, and then Data.
484 */
485 stat = Ser2HSSR1;
486 rmb();
487 data = Ser2HSDR;
488
489 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800490 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (stat & HSSR1_CRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800492 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (stat & HSSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800494 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 } else
496 skb->data[len++] = data;
497
498 /*
499 * If we hit the end of frame, there's
500 * no point in continuing.
501 */
502 if (stat & HSSR1_EOF)
503 break;
504 } while (Ser2HSSR0 & HSSR0_EIF);
505
506 if (stat & HSSR1_EOF) {
507 si->rxskb = NULL;
508
509 skb_put(skb, len);
510 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700511 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 skb->protocol = htons(ETH_P_IRDA);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800513 dev->stats.rx_packets++;
514 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /*
517 * Before we pass the buffer up, allocate a new one.
518 */
519 sa1100_irda_rx_alloc(si);
520
521 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 } else {
523 /*
Russell King22f0bf92012-01-08 13:55:23 +0000524 * Remap the buffer - it was previously mapped, and we
525 * hope that this succeeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
527 si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,
528 HPSIR_MAX_RXLEN,
529 DMA_FROM_DEVICE);
530 }
531}
532
533/*
534 * FIR format interrupt service routine. We only have to
535 * handle RX events; transmit events go via the TX DMA handler.
536 *
537 * No matter what, we disable RX, process, and the restart RX.
538 */
539static void sa1100_irda_fir_irq(struct net_device *dev)
540{
Wang Chen4cf16532008-11-12 23:38:14 -0800541 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /*
544 * Stop RX DMA
545 */
546 sa1100_stop_dma(si->rxdma);
547
548 /*
549 * Framing error - we throw away the packet completely.
550 * Clearing RXE flushes the error conditions and data
551 * from the fifo.
552 */
553 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800554 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 if (Ser2HSSR0 & HSSR0_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800557 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559 /*
560 * Clear out the DMA...
561 */
562 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
563
564 /*
565 * Clear selected status bits now, so we
566 * don't miss them next time around.
567 */
568 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
569 }
570
571 /*
572 * Deal with any receive errors. The any of the lowest
573 * 8 bytes in the FIFO may contain an error. We must read
574 * them one by one. The "error" could even be the end of
575 * packet!
576 */
577 if (Ser2HSSR0 & HSSR0_EIF)
578 sa1100_irda_fir_error(si, dev);
579
580 /*
581 * No matter what happens, we must restart reception.
582 */
583 sa1100_irda_rx_dma_start(si);
584}
585
David Howells7d12e782006-10-05 14:55:46 +0100586static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800589 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 sa1100_irda_fir_irq(dev);
591 else
592 sa1100_irda_hpsir_irq(dev);
593 return IRQ_HANDLED;
594}
595
596/*
597 * TX DMA completion handler.
598 */
599static void sa1100_irda_txdma_irq(void *id)
600{
601 struct net_device *dev = id;
Wang Chen4cf16532008-11-12 23:38:14 -0800602 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 struct sk_buff *skb = si->txskb;
604
605 si->txskb = NULL;
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
623 * here - we don't free the old rxskb. We don't need
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
637 /*
638 * Account and free the packet.
639 */
640 if (skb) {
641 dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800642 dev->stats.tx_packets ++;
643 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 dev_kfree_skb_irq(skb);
645 }
646
647 /*
648 * Make sure that the TX queue is available for sending
649 * (for retries). TX has priority over RX at all times.
650 */
651 netif_wake_queue(dev);
652}
653
654static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
655{
Wang Chen4cf16532008-11-12 23:38:14 -0800656 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int speed = irda_get_next_speed(skb);
658
659 /*
660 * Does this packet contain a request to change the interface
661 * speed? If so, remember it until we complete the transmission
662 * of this frame.
663 */
664 if (speed != si->speed && speed != -1)
665 si->newspeed = speed;
666
667 /*
668 * If this is an empty frame, we can bypass a lot.
669 */
670 if (skb->len == 0) {
671 if (si->newspeed) {
672 si->newspeed = 0;
673 sa1100_irda_set_speed(si, speed);
674 }
675 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000676 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 if (!IS_FIR(si)) {
680 netif_stop_queue(dev);
681
682 si->tx_buff.data = si->tx_buff.head;
683 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
684 si->tx_buff.truesize);
685
686 /*
687 * Set the transmit interrupt enable. This will fire
688 * off an interrupt immediately. Note that we disable
689 * the receiver so we won't get spurious characteres
690 * received.
691 */
692 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
693
694 dev_kfree_skb(skb);
695 } else {
696 int mtt = irda_get_mtt(skb);
697
698 /*
699 * We must not be transmitting...
700 */
Eric Sesterhenn5d9428d2006-04-02 13:52:48 +0200701 BUG_ON(si->txskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 netif_stop_queue(dev);
704
705 si->txskb = skb;
706 si->txbuf_dma = dma_map_single(si->dev, skb->data,
707 skb->len, DMA_TO_DEVICE);
Russell King22f0bf92012-01-08 13:55:23 +0000708 if (dma_mapping_error(si->dev, si->txbuf_dma)) {
709 si->txskb = NULL;
710 netif_wake_queue(dev);
711 dev->stats.tx_dropped++;
712 dev_kfree_skb(skb);
713 return NETDEV_TX_OK;
714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len);
717
718 /*
719 * If we have a mean turn-around time, impose the specified
720 * specified delay. We could shorten this by timing from
721 * the point we received the packet.
722 */
723 if (mtt)
724 udelay(mtt);
725
726 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
727 }
728
Patrick McHardy6ed10652009-06-23 06:03:08 +0000729 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
732static int
733sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
734{
735 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800736 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 int ret = -EOPNOTSUPP;
738
739 switch (cmd) {
740 case SIOCSBANDWIDTH:
741 if (capable(CAP_NET_ADMIN)) {
742 /*
743 * We are unable to set the speed if the
744 * device is not running.
745 */
746 if (si->open) {
747 ret = sa1100_irda_set_speed(si,
748 rq->ifr_baudrate);
749 } else {
750 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
751 ret = 0;
752 }
753 }
754 break;
755
756 case SIOCSMEDIABUSY:
757 ret = -EPERM;
758 if (capable(CAP_NET_ADMIN)) {
759 irda_device_set_media_busy(dev, TRUE);
760 ret = 0;
761 }
762 break;
763
764 case SIOCGRECEIVING:
765 rq->ifr_receiving = IS_FIR(si) ? 0
766 : si->rx_buff.state != OUTSIDE_FRAME;
767 break;
768
769 default:
770 break;
771 }
772
773 return ret;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static int sa1100_irda_start(struct net_device *dev)
777{
Wang Chen4cf16532008-11-12 23:38:14 -0800778 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 int err;
780
781 si->speed = 9600;
782
783 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
784 if (err)
785 goto err_irq;
786
787 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
788 NULL, NULL, &si->rxdma);
789 if (err)
790 goto err_rx_dma;
791
792 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
793 sa1100_irda_txdma_irq, dev, &si->txdma);
794 if (err)
795 goto err_tx_dma;
796
797 /*
798 * The interrupt must remain disabled for now.
799 */
800 disable_irq(dev->irq);
801
802 /*
803 * Setup the serial port for the specified speed.
804 */
805 err = sa1100_irda_startup(si);
806 if (err)
807 goto err_startup;
808
809 /*
810 * Open a new IrLAP layer instance.
811 */
812 si->irlap = irlap_open(dev, &si->qos, "sa1100");
813 err = -ENOMEM;
814 if (!si->irlap)
815 goto err_irlap;
816
817 /*
818 * Now enable the interrupt and start the queue
819 */
820 si->open = 1;
821 sa1100_set_power(si, power_level); /* low power mode */
822 enable_irq(dev->irq);
823 netif_start_queue(dev);
824 return 0;
825
826err_irlap:
827 si->open = 0;
828 sa1100_irda_shutdown(si);
829err_startup:
830 sa1100_free_dma(si->txdma);
831err_tx_dma:
832 sa1100_free_dma(si->rxdma);
833err_rx_dma:
834 free_irq(dev->irq, dev);
835err_irq:
836 return err;
837}
838
839static int sa1100_irda_stop(struct net_device *dev)
840{
Wang Chen4cf16532008-11-12 23:38:14 -0800841 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 disable_irq(dev->irq);
844 sa1100_irda_shutdown(si);
845
846 /*
847 * If we have been doing DMA receive, make sure we
848 * tidy that up cleanly.
849 */
850 if (si->rxskb) {
851 dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN,
852 DMA_FROM_DEVICE);
853 dev_kfree_skb(si->rxskb);
854 si->rxskb = NULL;
855 }
856
857 /* Stop IrLAP */
858 if (si->irlap) {
859 irlap_close(si->irlap);
860 si->irlap = NULL;
861 }
862
863 netif_stop_queue(dev);
864 si->open = 0;
865
866 /*
867 * Free resources
868 */
869 sa1100_free_dma(si->txdma);
870 sa1100_free_dma(si->rxdma);
871 free_irq(dev->irq, dev);
872
873 sa1100_set_power(si, 0);
874
875 return 0;
876}
877
878static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
879{
880 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
881 if (io->head != NULL) {
882 io->truesize = size;
883 io->in_frame = FALSE;
884 io->state = OUTSIDE_FRAME;
885 io->data = io->head;
886 }
887 return io->head ? 0 : -ENOMEM;
888}
889
Alexander Beregalova1de9662009-04-15 12:52:42 +0000890static const struct net_device_ops sa1100_irda_netdev_ops = {
891 .ndo_open = sa1100_irda_start,
892 .ndo_stop = sa1100_irda_stop,
893 .ndo_start_xmit = sa1100_irda_hard_xmit,
894 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000895};
896
Russell King3ae5eae2005-11-09 22:32:44 +0000897static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 struct net_device *dev;
900 struct sa1100_irda *si;
901 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000902 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (!pdev->dev.platform_data)
905 return -EINVAL;
906
Russell Kinge556fdb2012-01-08 12:02:17 +0000907 irq = platform_get_irq(pdev, 0);
908 if (irq <= 0)
909 return irq < 0 ? irq : -ENXIO;
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
912 if (err)
913 goto err_mem_1;
914 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
915 if (err)
916 goto err_mem_2;
917 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
918 if (err)
919 goto err_mem_3;
920
921 dev = alloc_irdadev(sizeof(struct sa1100_irda));
922 if (!dev)
923 goto err_mem_4;
924
Russell Kingd3238602012-01-08 12:07:24 +0000925 SET_NETDEV_DEV(dev, &pdev->dev);
926
Wang Chen4cf16532008-11-12 23:38:14 -0800927 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 si->dev = &pdev->dev;
929 si->pdata = pdev->dev.platform_data;
930
931 /*
932 * Initialise the HP-SIR buffers
933 */
934 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
935 if (err)
936 goto err_mem_5;
937 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
938 if (err)
939 goto err_mem_5;
940
Alexander Beregalova1de9662009-04-15 12:52:42 +0000941 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000942 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 irda_init_max_qos_capabilies(&si->qos);
945
946 /*
947 * We support original IRDA up to 115k2. (we don't currently
948 * support 4Mbps). Min Turn Time set to 1ms or greater.
949 */
950 baudrate_mask = IR_9600;
951
952 switch (max_rate) {
953 case 4000000: baudrate_mask |= IR_4000000 << 8;
954 case 115200: baudrate_mask |= IR_115200;
955 case 57600: baudrate_mask |= IR_57600;
956 case 38400: baudrate_mask |= IR_38400;
957 case 19200: baudrate_mask |= IR_19200;
958 }
959
960 si->qos.baud_rate.bits &= baudrate_mask;
961 si->qos.min_turn_time.bits = 7;
962
963 irda_qos_bits_to_value(&si->qos);
964
965 si->utcr4 = UTCR4_HPSIR;
966 if (tx_lpm)
967 si->utcr4 |= UTCR4_Z1_6us;
968
969 /*
970 * Initially enable HP-SIR modulation, and ensure that the port
971 * is disabled.
972 */
973 Ser2UTCR3 = 0;
974 Ser2UTCR4 = si->utcr4;
975 Ser2HSCR0 = HSCR0_UART;
976
977 err = register_netdev(dev);
978 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000979 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if (err) {
982 err_mem_5:
983 kfree(si->tx_buff.head);
984 kfree(si->rx_buff.head);
985 free_netdev(dev);
986 err_mem_4:
987 release_mem_region(__PREG(Ser2HSCR2), 0x04);
988 err_mem_3:
989 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
990 err_mem_2:
991 release_mem_region(__PREG(Ser2UTCR0), 0x24);
992 }
993 err_mem_1:
994 return err;
995}
996
Russell King3ae5eae2005-11-09 22:32:44 +0000997static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Russell King3ae5eae2005-11-09 22:32:44 +0000999 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -08001002 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 unregister_netdev(dev);
1004 kfree(si->tx_buff.head);
1005 kfree(si->rx_buff.head);
1006 free_netdev(dev);
1007 }
1008
1009 release_mem_region(__PREG(Ser2HSCR2), 0x04);
1010 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1011 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1012
1013 return 0;
1014}
1015
Russell King3ae5eae2005-11-09 22:32:44 +00001016static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 .probe = sa1100_irda_probe,
1018 .remove = sa1100_irda_remove,
1019 .suspend = sa1100_irda_suspend,
1020 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001021 .driver = {
1022 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001023 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001024 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025};
1026
1027static int __init sa1100_irda_init(void)
1028{
1029 /*
1030 * Limit power level a sensible range.
1031 */
1032 if (power_level < 1)
1033 power_level = 1;
1034 if (power_level > 3)
1035 power_level = 3;
1036
Russell King3ae5eae2005-11-09 22:32:44 +00001037 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040static void __exit sa1100_irda_exit(void)
1041{
Russell King3ae5eae2005-11-09 22:32:44 +00001042 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
1045module_init(sa1100_irda_init);
1046module_exit(sa1100_irda_exit);
1047module_param(power_level, int, 0);
1048module_param(tx_lpm, int, 0);
1049module_param(max_rate, int, 0);
1050
1051MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1052MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1053MODULE_LICENSE("GPL");
1054MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1055MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1056MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001057MODULE_ALIAS("platform:sa11x0-ir");