blob: adb7fea78c2d63e964dcd79e0bdb3d925de5e99e [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
Russell King885767c2012-01-08 12:53:22 +000046struct sa1100_buf {
47 struct sk_buff *skb;
48 dma_addr_t dma;
49 dma_regs_t *regs;
50};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct 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
Russell King885767c2012-01-08 12:53:22 +000061 struct sa1100_buf dma_rx;
62 struct sa1100_buf dma_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 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 */
80static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
81{
Russell King885767c2012-01-08 12:53:22 +000082 if (si->dma_rx.skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return 0;
84
Russell King885767c2012-01-08 12:53:22 +000085 si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
86 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 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 */
Russell King885767c2012-01-08 12:53:22 +000095 skb_reserve(si->dma_rx.skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Russell King885767c2012-01-08 12:53:22 +000097 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 HPSIR_MAX_RXLEN,
99 DMA_FROM_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000100 if (dma_mapping_error(si->dev, si->dma_rx.dma)) {
101 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000102 return -ENOMEM;
103 }
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 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 */
112static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
113{
Russell King885767c2012-01-08 12:53:22 +0000114 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 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 */
Russell King885767c2012-01-08 12:53:22 +0000127 sa1100_clear_dma(si->dma_rx.regs);
128 sa1100_start_dma(si->dma_rx.regs, si->dma_rx.dma, HPSIR_MAX_RXLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
130}
131
132/*
133 * Set the IrDA communications speed.
134 */
135static 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))
Russell King885767c2012-01-08 12:53:22 +0000149 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
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 */
212static 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
221static inline int
222sa1100_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
233static 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 */
Dmitry Artamonow91cd1752009-10-13 03:17:37 -0700240 if (si->pdata->startup) {
241 ret = si->pdata->startup(si->dev);
242 if (ret)
243 return ret;
244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
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
280static void sa1100_irda_shutdown(struct sa1100_irda *si)
281{
282 /*
283 * Stop all DMA activity.
284 */
Russell King885767c2012-01-08 12:53:22 +0000285 sa1100_stop_dma(si->dma_rx.regs);
286 sa1100_stop_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
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 */
Russell King3ae5eae2005-11-09 22:32:44 +0000300static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Russell King3ae5eae2005-11-09 22:32:44 +0000302 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct sa1100_irda *si;
304
Russell King9480e302005-10-28 09:52:56 -0700305 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return 0;
307
Wang Chen4cf16532008-11-12 23:38:14 -0800308 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 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 */
Russell King3ae5eae2005-11-09 22:32:44 +0000325static int sa1100_irda_resume(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Russell King3ae5eae2005-11-09 22:32:44 +0000327 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 struct sa1100_irda *si;
329
Russell King9480e302005-10-28 09:52:56 -0700330 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
332
Wang Chen4cf16532008-11-12 23:38:14 -0800333 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 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
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800339 * we left off it is desirable. The converse argument is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * 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 */
367static void sa1100_irda_hpsir_irq(struct net_device *dev)
368{
Wang Chen4cf16532008-11-12 23:38:14 -0800369 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 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)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800385 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (stat & UTSR1_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800387 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (stat & UTSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800389 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 } else
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800391 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
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 */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800406 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
411 if (status & (UTSR0_RFS | UTSR0_RID)) {
412 /*
413 * Fifo contains more than 1 character.
414 */
415 do {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800416 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 Ser2UTDR);
418 } while (Ser2UTSR1 & UTSR1_RNE);
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
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) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800432 dev->stats.tx_packets++;
433 dev->stats.tx_bytes += si->tx_buff.data -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 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
463static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
464{
Russell King885767c2012-01-08 12:53:22 +0000465 struct sk_buff *skb = si->dma_rx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 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 */
Russell King885767c2012-01-08 12:53:22 +0000477 dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
478 len = dma_addr - si->dma_rx.dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (len > HPSIR_MAX_RXLEN)
480 len = HPSIR_MAX_RXLEN;
Russell King885767c2012-01-08 12:53:22 +0000481 dma_unmap_single(si->dev, si->dma_rx.dma, len, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
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)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800492 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (stat & HSSR1_CRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800494 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (stat & HSSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800496 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } 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) {
Russell King885767c2012-01-08 12:53:22 +0000509 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 skb_put(skb, len);
512 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700513 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 skb->protocol = htons(ETH_P_IRDA);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800515 dev->stats.rx_packets++;
516 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 /*
519 * Before we pass the buffer up, allocate a new one.
520 */
521 sa1100_irda_rx_alloc(si);
522
523 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 } else {
525 /*
Russell King22f0bf92012-01-08 13:55:23 +0000526 * Remap the buffer - it was previously mapped, and we
527 * hope that this succeeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Russell King885767c2012-01-08 12:53:22 +0000529 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 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 */
541static void sa1100_irda_fir_irq(struct net_device *dev)
542{
Wang Chen4cf16532008-11-12 23:38:14 -0800543 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 /*
546 * Stop RX DMA
547 */
Russell King885767c2012-01-08 12:53:22 +0000548 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
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)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800556 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (Ser2HSSR0 & HSSR0_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800559 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
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
David Howells7d12e782006-10-05 14:55:46 +0100588static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800591 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 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 */
601static void sa1100_irda_txdma_irq(void *id)
602{
603 struct net_device *dev = id;
Wang Chen4cf16532008-11-12 23:38:14 -0800604 struct sa1100_irda *si = netdev_priv(dev);
Russell King885767c2012-01-08 12:53:22 +0000605 struct sk_buff *skb = si->dma_tx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Russell King885767c2012-01-08 12:53:22 +0000607 si->dma_tx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /*
610 * Wait for the transmission to complete. Unfortunately,
611 * the hardware doesn't give us an interrupt to indicate
612 * "end of frame".
613 */
614 do
615 rmb();
616 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
617
618 /*
619 * Clear the transmit underrun bit.
620 */
621 Ser2HSSR0 = HSSR0_TUR;
622
623 /*
624 * Do we need to change speed? Note that we're lazy
Russell King885767c2012-01-08 12:53:22 +0000625 * here - we don't free the old dma_rx.skb. We don't need
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * to allocate a buffer either.
627 */
628 if (si->newspeed) {
629 sa1100_irda_set_speed(si, si->newspeed);
630 si->newspeed = 0;
631 }
632
633 /*
634 * Start reception. This disables the transmitter for
635 * us. This will be using the existing RX buffer.
636 */
637 sa1100_irda_rx_dma_start(si);
638
639 /*
640 * Account and free the packet.
641 */
642 if (skb) {
Russell King885767c2012-01-08 12:53:22 +0000643 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len, DMA_TO_DEVICE);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800644 dev->stats.tx_packets ++;
645 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 dev_kfree_skb_irq(skb);
647 }
648
649 /*
650 * Make sure that the TX queue is available for sending
651 * (for retries). TX has priority over RX at all times.
652 */
653 netif_wake_queue(dev);
654}
655
656static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
657{
Wang Chen4cf16532008-11-12 23:38:14 -0800658 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int speed = irda_get_next_speed(skb);
660
661 /*
662 * Does this packet contain a request to change the interface
663 * speed? If so, remember it until we complete the transmission
664 * of this frame.
665 */
666 if (speed != si->speed && speed != -1)
667 si->newspeed = speed;
668
669 /*
670 * If this is an empty frame, we can bypass a lot.
671 */
672 if (skb->len == 0) {
673 if (si->newspeed) {
674 si->newspeed = 0;
675 sa1100_irda_set_speed(si, speed);
676 }
677 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000678 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680
681 if (!IS_FIR(si)) {
682 netif_stop_queue(dev);
683
684 si->tx_buff.data = si->tx_buff.head;
685 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
686 si->tx_buff.truesize);
687
688 /*
689 * Set the transmit interrupt enable. This will fire
690 * off an interrupt immediately. Note that we disable
691 * the receiver so we won't get spurious characteres
692 * received.
693 */
694 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
695
696 dev_kfree_skb(skb);
697 } else {
698 int mtt = irda_get_mtt(skb);
699
700 /*
701 * We must not be transmitting...
702 */
Russell King885767c2012-01-08 12:53:22 +0000703 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 netif_stop_queue(dev);
706
Russell King885767c2012-01-08 12:53:22 +0000707 si->dma_tx.skb = skb;
708 si->dma_tx.dma = dma_map_single(si->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 skb->len, DMA_TO_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000710 if (dma_mapping_error(si->dev, si->dma_tx.dma)) {
711 si->dma_tx.skb = NULL;
Russell King22f0bf92012-01-08 13:55:23 +0000712 netif_wake_queue(dev);
713 dev->stats.tx_dropped++;
714 dev_kfree_skb(skb);
715 return NETDEV_TX_OK;
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Russell King885767c2012-01-08 12:53:22 +0000718 sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /*
721 * If we have a mean turn-around time, impose the specified
722 * specified delay. We could shorten this by timing from
723 * the point we received the packet.
724 */
725 if (mtt)
726 udelay(mtt);
727
728 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
729 }
730
Patrick McHardy6ed10652009-06-23 06:03:08 +0000731 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734static int
735sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
736{
737 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800738 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 int ret = -EOPNOTSUPP;
740
741 switch (cmd) {
742 case SIOCSBANDWIDTH:
743 if (capable(CAP_NET_ADMIN)) {
744 /*
745 * We are unable to set the speed if the
746 * device is not running.
747 */
748 if (si->open) {
749 ret = sa1100_irda_set_speed(si,
750 rq->ifr_baudrate);
751 } else {
752 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
753 ret = 0;
754 }
755 }
756 break;
757
758 case SIOCSMEDIABUSY:
759 ret = -EPERM;
760 if (capable(CAP_NET_ADMIN)) {
761 irda_device_set_media_busy(dev, TRUE);
762 ret = 0;
763 }
764 break;
765
766 case SIOCGRECEIVING:
767 rq->ifr_receiving = IS_FIR(si) ? 0
768 : si->rx_buff.state != OUTSIDE_FRAME;
769 break;
770
771 default:
772 break;
773 }
774
775 return ret;
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static int sa1100_irda_start(struct net_device *dev)
779{
Wang Chen4cf16532008-11-12 23:38:14 -0800780 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int err;
782
783 si->speed = 9600;
784
785 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
786 if (err)
787 goto err_irq;
788
789 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
Russell King885767c2012-01-08 12:53:22 +0000790 NULL, NULL, &si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (err)
792 goto err_rx_dma;
793
794 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
Russell King885767c2012-01-08 12:53:22 +0000795 sa1100_irda_txdma_irq, dev, &si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (err)
797 goto err_tx_dma;
798
799 /*
800 * The interrupt must remain disabled for now.
801 */
802 disable_irq(dev->irq);
803
804 /*
805 * Setup the serial port for the specified speed.
806 */
807 err = sa1100_irda_startup(si);
808 if (err)
809 goto err_startup;
810
811 /*
812 * Open a new IrLAP layer instance.
813 */
814 si->irlap = irlap_open(dev, &si->qos, "sa1100");
815 err = -ENOMEM;
816 if (!si->irlap)
817 goto err_irlap;
818
819 /*
820 * Now enable the interrupt and start the queue
821 */
822 si->open = 1;
823 sa1100_set_power(si, power_level); /* low power mode */
824 enable_irq(dev->irq);
825 netif_start_queue(dev);
826 return 0;
827
828err_irlap:
829 si->open = 0;
830 sa1100_irda_shutdown(si);
831err_startup:
Russell King885767c2012-01-08 12:53:22 +0000832 sa1100_free_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833err_tx_dma:
Russell King885767c2012-01-08 12:53:22 +0000834 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835err_rx_dma:
836 free_irq(dev->irq, dev);
837err_irq:
838 return err;
839}
840
841static int sa1100_irda_stop(struct net_device *dev)
842{
Wang Chen4cf16532008-11-12 23:38:14 -0800843 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 disable_irq(dev->irq);
846 sa1100_irda_shutdown(si);
847
848 /*
849 * If we have been doing DMA receive, make sure we
850 * tidy that up cleanly.
851 */
Russell King885767c2012-01-08 12:53:22 +0000852 if (si->dma_rx.skb) {
853 dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 DMA_FROM_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000855 dev_kfree_skb(si->dma_rx.skb);
856 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858
859 /* Stop IrLAP */
860 if (si->irlap) {
861 irlap_close(si->irlap);
862 si->irlap = NULL;
863 }
864
865 netif_stop_queue(dev);
866 si->open = 0;
867
868 /*
869 * Free resources
870 */
Russell King885767c2012-01-08 12:53:22 +0000871 sa1100_free_dma(si->dma_tx.regs);
872 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 free_irq(dev->irq, dev);
874
875 sa1100_set_power(si, 0);
876
877 return 0;
878}
879
880static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
881{
882 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
883 if (io->head != NULL) {
884 io->truesize = size;
885 io->in_frame = FALSE;
886 io->state = OUTSIDE_FRAME;
887 io->data = io->head;
888 }
889 return io->head ? 0 : -ENOMEM;
890}
891
Alexander Beregalova1de9662009-04-15 12:52:42 +0000892static const struct net_device_ops sa1100_irda_netdev_ops = {
893 .ndo_open = sa1100_irda_start,
894 .ndo_stop = sa1100_irda_stop,
895 .ndo_start_xmit = sa1100_irda_hard_xmit,
896 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000897};
898
Russell King3ae5eae2005-11-09 22:32:44 +0000899static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 struct net_device *dev;
902 struct sa1100_irda *si;
903 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000904 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 if (!pdev->dev.platform_data)
907 return -EINVAL;
908
Russell Kinge556fdb2012-01-08 12:02:17 +0000909 irq = platform_get_irq(pdev, 0);
910 if (irq <= 0)
911 return irq < 0 ? irq : -ENXIO;
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
914 if (err)
915 goto err_mem_1;
916 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
917 if (err)
918 goto err_mem_2;
919 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
920 if (err)
921 goto err_mem_3;
922
923 dev = alloc_irdadev(sizeof(struct sa1100_irda));
924 if (!dev)
925 goto err_mem_4;
926
Russell Kingd3238602012-01-08 12:07:24 +0000927 SET_NETDEV_DEV(dev, &pdev->dev);
928
Wang Chen4cf16532008-11-12 23:38:14 -0800929 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 si->dev = &pdev->dev;
931 si->pdata = pdev->dev.platform_data;
932
933 /*
934 * Initialise the HP-SIR buffers
935 */
936 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
937 if (err)
938 goto err_mem_5;
939 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
940 if (err)
941 goto err_mem_5;
942
Alexander Beregalova1de9662009-04-15 12:52:42 +0000943 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000944 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 irda_init_max_qos_capabilies(&si->qos);
947
948 /*
949 * We support original IRDA up to 115k2. (we don't currently
950 * support 4Mbps). Min Turn Time set to 1ms or greater.
951 */
952 baudrate_mask = IR_9600;
953
954 switch (max_rate) {
955 case 4000000: baudrate_mask |= IR_4000000 << 8;
956 case 115200: baudrate_mask |= IR_115200;
957 case 57600: baudrate_mask |= IR_57600;
958 case 38400: baudrate_mask |= IR_38400;
959 case 19200: baudrate_mask |= IR_19200;
960 }
961
962 si->qos.baud_rate.bits &= baudrate_mask;
963 si->qos.min_turn_time.bits = 7;
964
965 irda_qos_bits_to_value(&si->qos);
966
967 si->utcr4 = UTCR4_HPSIR;
968 if (tx_lpm)
969 si->utcr4 |= UTCR4_Z1_6us;
970
971 /*
972 * Initially enable HP-SIR modulation, and ensure that the port
973 * is disabled.
974 */
975 Ser2UTCR3 = 0;
976 Ser2UTCR4 = si->utcr4;
977 Ser2HSCR0 = HSCR0_UART;
978
979 err = register_netdev(dev);
980 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000981 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 if (err) {
984 err_mem_5:
985 kfree(si->tx_buff.head);
986 kfree(si->rx_buff.head);
987 free_netdev(dev);
988 err_mem_4:
989 release_mem_region(__PREG(Ser2HSCR2), 0x04);
990 err_mem_3:
991 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
992 err_mem_2:
993 release_mem_region(__PREG(Ser2UTCR0), 0x24);
994 }
995 err_mem_1:
996 return err;
997}
998
Russell King3ae5eae2005-11-09 22:32:44 +0000999static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Russell King3ae5eae2005-11-09 22:32:44 +00001001 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -08001004 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unregister_netdev(dev);
1006 kfree(si->tx_buff.head);
1007 kfree(si->rx_buff.head);
1008 free_netdev(dev);
1009 }
1010
1011 release_mem_region(__PREG(Ser2HSCR2), 0x04);
1012 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1013 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1014
1015 return 0;
1016}
1017
Russell King3ae5eae2005-11-09 22:32:44 +00001018static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 .probe = sa1100_irda_probe,
1020 .remove = sa1100_irda_remove,
1021 .suspend = sa1100_irda_suspend,
1022 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001023 .driver = {
1024 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001025 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001026 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027};
1028
1029static int __init sa1100_irda_init(void)
1030{
1031 /*
1032 * Limit power level a sensible range.
1033 */
1034 if (power_level < 1)
1035 power_level = 1;
1036 if (power_level > 3)
1037 power_level = 3;
1038
Russell King3ae5eae2005-11-09 22:32:44 +00001039 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042static void __exit sa1100_irda_exit(void)
1043{
Russell King3ae5eae2005-11-09 22:32:44 +00001044 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047module_init(sa1100_irda_init);
1048module_exit(sa1100_irda_exit);
1049module_param(power_level, int, 0);
1050module_param(tx_lpm, int, 0);
1051module_param(max_rate, int, 0);
1052
1053MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1054MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1055MODULE_LICENSE("GPL");
1056MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1057MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1058MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001059MODULE_ALIAS("platform:sa11x0-ir");