blob: 9dc564830c9ddabe44b9c2e52de7286d671fce10 [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 Kingba845252012-01-08 15:38:15 +0000605 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
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
Russell King885767c2012-01-08 12:53:22 +0000623 * here - we don't free the old dma_rx.skb. We don't need
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 * 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
Russell Kingba845252012-01-08 15:38:15 +0000637 /* Account and free the packet. */
638 skb = si->dma_tx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (skb) {
Russell Kingba845252012-01-08 15:38:15 +0000640 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
641 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);
Russell Kingba845252012-01-08 15:38:15 +0000645 si->dma_tx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
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 */
Russell King885767c2012-01-08 12:53:22 +0000702 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 netif_stop_queue(dev);
705
Russell King885767c2012-01-08 12:53:22 +0000706 si->dma_tx.skb = skb;
707 si->dma_tx.dma = dma_map_single(si->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 skb->len, DMA_TO_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000709 if (dma_mapping_error(si->dev, si->dma_tx.dma)) {
710 si->dma_tx.skb = NULL;
Russell King22f0bf92012-01-08 13:55:23 +0000711 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
Russell King885767c2012-01-08 12:53:22 +0000717 sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
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",
Russell King885767c2012-01-08 12:53:22 +0000789 NULL, NULL, &si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (err)
791 goto err_rx_dma;
792
793 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
Russell King885767c2012-01-08 12:53:22 +0000794 sa1100_irda_txdma_irq, dev, &si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 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:
Russell King885767c2012-01-08 12:53:22 +0000831 sa1100_free_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832err_tx_dma:
Russell King885767c2012-01-08 12:53:22 +0000833 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834err_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);
Russell Kingba845252012-01-08 15:38:15 +0000843 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 disable_irq(dev->irq);
846 sa1100_irda_shutdown(si);
847
848 /*
Russell Kingba845252012-01-08 15:38:15 +0000849 * If we have been doing any DMA activity, make sure we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * tidy that up cleanly.
851 */
Russell Kingba845252012-01-08 15:38:15 +0000852 skb = si->dma_rx.skb;
853 if (skb) {
Russell King885767c2012-01-08 12:53:22 +0000854 dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 DMA_FROM_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000856 dev_kfree_skb(skb);
Russell King885767c2012-01-08 12:53:22 +0000857 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859
Russell Kingba845252012-01-08 15:38:15 +0000860 skb = si->dma_tx.skb;
861 if (skb) {
862 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
863 DMA_TO_DEVICE);
864 dev_kfree_skb(skb);
865 si->dma_tx.skb = NULL;
866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 /* Stop IrLAP */
869 if (si->irlap) {
870 irlap_close(si->irlap);
871 si->irlap = NULL;
872 }
873
874 netif_stop_queue(dev);
875 si->open = 0;
876
877 /*
878 * Free resources
879 */
Russell King885767c2012-01-08 12:53:22 +0000880 sa1100_free_dma(si->dma_tx.regs);
881 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 free_irq(dev->irq, dev);
883
884 sa1100_set_power(si, 0);
885
886 return 0;
887}
888
889static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
890{
891 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
892 if (io->head != NULL) {
893 io->truesize = size;
894 io->in_frame = FALSE;
895 io->state = OUTSIDE_FRAME;
896 io->data = io->head;
897 }
898 return io->head ? 0 : -ENOMEM;
899}
900
Alexander Beregalova1de9662009-04-15 12:52:42 +0000901static const struct net_device_ops sa1100_irda_netdev_ops = {
902 .ndo_open = sa1100_irda_start,
903 .ndo_stop = sa1100_irda_stop,
904 .ndo_start_xmit = sa1100_irda_hard_xmit,
905 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000906};
907
Russell King3ae5eae2005-11-09 22:32:44 +0000908static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct net_device *dev;
911 struct sa1100_irda *si;
912 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000913 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 if (!pdev->dev.platform_data)
916 return -EINVAL;
917
Russell Kinge556fdb2012-01-08 12:02:17 +0000918 irq = platform_get_irq(pdev, 0);
919 if (irq <= 0)
920 return irq < 0 ? irq : -ENXIO;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
923 if (err)
924 goto err_mem_1;
925 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
926 if (err)
927 goto err_mem_2;
928 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
929 if (err)
930 goto err_mem_3;
931
932 dev = alloc_irdadev(sizeof(struct sa1100_irda));
933 if (!dev)
934 goto err_mem_4;
935
Russell Kingd3238602012-01-08 12:07:24 +0000936 SET_NETDEV_DEV(dev, &pdev->dev);
937
Wang Chen4cf16532008-11-12 23:38:14 -0800938 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 si->dev = &pdev->dev;
940 si->pdata = pdev->dev.platform_data;
941
942 /*
943 * Initialise the HP-SIR buffers
944 */
945 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
946 if (err)
947 goto err_mem_5;
948 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
949 if (err)
950 goto err_mem_5;
951
Alexander Beregalova1de9662009-04-15 12:52:42 +0000952 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000953 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 irda_init_max_qos_capabilies(&si->qos);
956
957 /*
958 * We support original IRDA up to 115k2. (we don't currently
959 * support 4Mbps). Min Turn Time set to 1ms or greater.
960 */
961 baudrate_mask = IR_9600;
962
963 switch (max_rate) {
964 case 4000000: baudrate_mask |= IR_4000000 << 8;
965 case 115200: baudrate_mask |= IR_115200;
966 case 57600: baudrate_mask |= IR_57600;
967 case 38400: baudrate_mask |= IR_38400;
968 case 19200: baudrate_mask |= IR_19200;
969 }
970
971 si->qos.baud_rate.bits &= baudrate_mask;
972 si->qos.min_turn_time.bits = 7;
973
974 irda_qos_bits_to_value(&si->qos);
975
976 si->utcr4 = UTCR4_HPSIR;
977 if (tx_lpm)
978 si->utcr4 |= UTCR4_Z1_6us;
979
980 /*
981 * Initially enable HP-SIR modulation, and ensure that the port
982 * is disabled.
983 */
984 Ser2UTCR3 = 0;
985 Ser2UTCR4 = si->utcr4;
986 Ser2HSCR0 = HSCR0_UART;
987
988 err = register_netdev(dev);
989 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000990 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (err) {
993 err_mem_5:
994 kfree(si->tx_buff.head);
995 kfree(si->rx_buff.head);
996 free_netdev(dev);
997 err_mem_4:
998 release_mem_region(__PREG(Ser2HSCR2), 0x04);
999 err_mem_3:
1000 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1001 err_mem_2:
1002 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1003 }
1004 err_mem_1:
1005 return err;
1006}
1007
Russell King3ae5eae2005-11-09 22:32:44 +00001008static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Russell King3ae5eae2005-11-09 22:32:44 +00001010 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -08001013 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 unregister_netdev(dev);
1015 kfree(si->tx_buff.head);
1016 kfree(si->rx_buff.head);
1017 free_netdev(dev);
1018 }
1019
1020 release_mem_region(__PREG(Ser2HSCR2), 0x04);
1021 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1022 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1023
1024 return 0;
1025}
1026
Russell King3ae5eae2005-11-09 22:32:44 +00001027static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 .probe = sa1100_irda_probe,
1029 .remove = sa1100_irda_remove,
1030 .suspend = sa1100_irda_suspend,
1031 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001032 .driver = {
1033 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001034 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001035 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036};
1037
1038static int __init sa1100_irda_init(void)
1039{
1040 /*
1041 * Limit power level a sensible range.
1042 */
1043 if (power_level < 1)
1044 power_level = 1;
1045 if (power_level > 3)
1046 power_level = 3;
1047
Russell King3ae5eae2005-11-09 22:32:44 +00001048 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051static void __exit sa1100_irda_exit(void)
1052{
Russell King3ae5eae2005-11-09 22:32:44 +00001053 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056module_init(sa1100_irda_init);
1057module_exit(sa1100_irda_exit);
1058module_param(power_level, int, 0);
1059module_param(tx_lpm, int, 0);
1060module_param(max_rate, int, 0);
1061
1062MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1063MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1064MODULE_LICENSE("GPL");
1065MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1066MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1067MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001068MODULE_ALIAS("platform:sa11x0-ir");