blob: 86d296ed69a7e1d7609385d58c53bbc006be723a [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;
Russell King32273f52012-01-12 12:45:00 +000048 struct scatterlist sg;
Russell King885767c2012-01-08 12:53:22 +000049 dma_regs_t *regs;
50};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct sa1100_irda {
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 unsigned char utcr4;
54 unsigned char power;
55 unsigned char open;
56
57 int speed;
58 int newspeed;
59
Russell King885767c2012-01-08 12:53:22 +000060 struct sa1100_buf dma_rx;
61 struct sa1100_buf dma_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct device *dev;
64 struct irda_platform_data *pdata;
65 struct irlap_cb *irlap;
66 struct qos_info qos;
67
68 iobuff_t tx_buff;
69 iobuff_t rx_buff;
Russell King3d26db12012-01-08 16:16:39 +000070
71 int (*tx_start)(struct sk_buff *, struct net_device *, struct sa1100_irda *);
Russell King374f7732012-01-08 16:26:15 +000072 irqreturn_t (*irq)(struct net_device *, struct sa1100_irda *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Russell King0e888ee2012-01-08 16:30:44 +000075static int sa1100_irda_set_speed(struct sa1100_irda *, int);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define IS_FIR(si) ((si)->speed >= 4000000)
78
79#define HPSIR_MAX_RXLEN 2047
80
81/*
82 * Allocate and map the receive buffer, unless it is already allocated.
83 */
84static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
85{
Russell King885767c2012-01-08 12:53:22 +000086 if (si->dma_rx.skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88
Russell King885767c2012-01-08 12:53:22 +000089 si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
90 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
92 return -ENOMEM;
93 }
94
95 /*
96 * Align any IP headers that may be contained
97 * within the frame.
98 */
Russell King885767c2012-01-08 12:53:22 +000099 skb_reserve(si->dma_rx.skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Russell King32273f52012-01-12 12:45:00 +0000101 sg_set_buf(&si->dma_rx.sg, si->dma_rx.skb->data, HPSIR_MAX_RXLEN);
102 if (dma_map_sg(si->dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE) == 0) {
Russell King885767c2012-01-08 12:53:22 +0000103 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000104 return -ENOMEM;
105 }
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108}
109
110/*
111 * We want to get here as soon as possible, and get the receiver setup.
112 * We use the existing buffer.
113 */
114static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
115{
Russell King885767c2012-01-08 12:53:22 +0000116 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
118 return;
119 }
120
121 /*
122 * First empty receive FIFO
123 */
Russell King6a7f4912012-01-08 20:49:28 +0000124 Ser2HSCR0 = HSCR0_HSSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /*
127 * Enable the DMA, receiver and receive interrupt.
128 */
Russell King885767c2012-01-08 12:53:22 +0000129 sa1100_clear_dma(si->dma_rx.regs);
Russell King32273f52012-01-12 12:45:00 +0000130 sa1100_start_dma(si->dma_rx.regs, sg_dma_address(&si->dma_rx.sg),
131 sg_dma_len(&si->dma_rx.sg));
Russell King6a7f4912012-01-08 20:49:28 +0000132 Ser2HSCR0 = HSCR0_HSSP | HSCR0_RXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Russell King0e888ee2012-01-08 16:30:44 +0000135static void sa1100_irda_check_speed(struct sa1100_irda *si)
136{
137 if (si->newspeed) {
138 sa1100_irda_set_speed(si, si->newspeed);
139 si->newspeed = 0;
140 }
141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143/*
Russell King3d26db12012-01-08 16:16:39 +0000144 * HP-SIR format support.
145 */
146static int sa1100_irda_sir_tx_start(struct sk_buff *skb, struct net_device *dev,
147 struct sa1100_irda *si)
148{
149 si->tx_buff.data = si->tx_buff.head;
150 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
151 si->tx_buff.truesize);
152
153 /*
154 * Set the transmit interrupt enable. This will fire off an
155 * interrupt immediately. Note that we disable the receiver
156 * so we won't get spurious characters received.
157 */
158 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
159
160 dev_kfree_skb(skb);
161
162 return NETDEV_TX_OK;
163}
164
Russell Kinga6b2ea62012-01-08 17:10:01 +0000165static irqreturn_t sa1100_irda_sir_irq(struct net_device *dev, struct sa1100_irda *si)
166{
167 int status;
168
169 status = Ser2UTSR0;
170
171 /*
172 * Deal with any receive errors first. The bytes in error may be
173 * the only bytes in the receive FIFO, so we do this first.
174 */
175 while (status & UTSR0_EIF) {
176 int stat, data;
177
178 stat = Ser2UTSR1;
179 data = Ser2UTDR;
180
181 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
182 dev->stats.rx_errors++;
183 if (stat & UTSR1_FRE)
184 dev->stats.rx_frame_errors++;
185 if (stat & UTSR1_ROR)
186 dev->stats.rx_fifo_errors++;
187 } else
188 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
189
190 status = Ser2UTSR0;
191 }
192
193 /*
194 * We must clear certain bits.
195 */
196 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
197
198 if (status & UTSR0_RFS) {
199 /*
200 * There are at least 4 bytes in the FIFO. Read 3 bytes
201 * and leave the rest to the block below.
202 */
203 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
204 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
205 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
206 }
207
208 if (status & (UTSR0_RFS | UTSR0_RID)) {
209 /*
210 * Fifo contains more than 1 character.
211 */
212 do {
213 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
214 Ser2UTDR);
215 } while (Ser2UTSR1 & UTSR1_RNE);
216
217 }
218
219 if (status & UTSR0_TFS && si->tx_buff.len) {
220 /*
221 * Transmitter FIFO is not full
222 */
223 do {
224 Ser2UTDR = *si->tx_buff.data++;
225 si->tx_buff.len -= 1;
226 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
227
228 if (si->tx_buff.len == 0) {
229 dev->stats.tx_packets++;
230 dev->stats.tx_bytes += si->tx_buff.data -
231 si->tx_buff.head;
232
233 /*
234 * We need to ensure that the transmitter has
235 * finished.
236 */
237 do
238 rmb();
239 while (Ser2UTSR1 & UTSR1_TBY);
240
241 /*
242 * Ok, we've finished transmitting. Now enable
243 * the receiver. Sometimes we get a receive IRQ
244 * immediately after a transmit...
245 */
246 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
247 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
248
249 sa1100_irda_check_speed(si);
250
251 /* I'm hungry! */
252 netif_wake_queue(dev);
253 }
254 }
255
256 return IRQ_HANDLED;
257}
258
Russell King3d26db12012-01-08 16:16:39 +0000259/*
260 * FIR format support.
261 */
Russell King26f2bee2012-01-08 17:48:02 +0000262static void sa1100_irda_firtxdma_irq(void *id)
263{
264 struct net_device *dev = id;
265 struct sa1100_irda *si = netdev_priv(dev);
266 struct sk_buff *skb;
267
268 /*
269 * Wait for the transmission to complete. Unfortunately,
270 * the hardware doesn't give us an interrupt to indicate
271 * "end of frame".
272 */
273 do
274 rmb();
275 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
276
277 /*
278 * Clear the transmit underrun bit.
279 */
280 Ser2HSSR0 = HSSR0_TUR;
281
282 /*
283 * Do we need to change speed? Note that we're lazy
284 * here - we don't free the old dma_rx.skb. We don't need
285 * to allocate a buffer either.
286 */
287 sa1100_irda_check_speed(si);
288
289 /*
290 * Start reception. This disables the transmitter for
291 * us. This will be using the existing RX buffer.
292 */
293 sa1100_irda_rx_dma_start(si);
294
295 /* Account and free the packet. */
296 skb = si->dma_tx.skb;
297 if (skb) {
Russell King32273f52012-01-12 12:45:00 +0000298 dma_unmap_sg(si->dev, &si->dma_tx.sg, 1,
299 DMA_TO_DEVICE);
Russell King26f2bee2012-01-08 17:48:02 +0000300 dev->stats.tx_packets ++;
301 dev->stats.tx_bytes += skb->len;
302 dev_kfree_skb_irq(skb);
303 si->dma_tx.skb = NULL;
304 }
305
306 /*
307 * Make sure that the TX queue is available for sending
308 * (for retries). TX has priority over RX at all times.
309 */
310 netif_wake_queue(dev);
311}
312
Russell King3d26db12012-01-08 16:16:39 +0000313static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
314 struct sa1100_irda *si)
315{
316 int mtt = irda_get_mtt(skb);
317
318 si->dma_tx.skb = skb;
Russell King32273f52012-01-12 12:45:00 +0000319 sg_set_buf(&si->dma_tx.sg, skb->data, skb->len);
320 if (dma_map_sg(si->dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE) == 0) {
Russell King3d26db12012-01-08 16:16:39 +0000321 si->dma_tx.skb = NULL;
322 netif_wake_queue(dev);
323 dev->stats.tx_dropped++;
324 dev_kfree_skb(skb);
325 return NETDEV_TX_OK;
326 }
327
Russell King32273f52012-01-12 12:45:00 +0000328 sa1100_start_dma(si->dma_tx.regs, sg_dma_address(&si->dma_tx.sg),
329 sg_dma_len(&si->dma_tx.sg));
Russell King3d26db12012-01-08 16:16:39 +0000330
331 /*
332 * If we have a mean turn-around time, impose the specified
333 * specified delay. We could shorten this by timing from
334 * the point we received the packet.
335 */
336 if (mtt)
337 udelay(mtt);
338
Russell King6a7f4912012-01-08 20:49:28 +0000339 Ser2HSCR0 = HSCR0_HSSP | HSCR0_TXE;
Russell King3d26db12012-01-08 16:16:39 +0000340
341 return NETDEV_TX_OK;
342}
343
Russell Kinga6b2ea62012-01-08 17:10:01 +0000344static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
345{
346 struct sk_buff *skb = si->dma_rx.skb;
347 dma_addr_t dma_addr;
348 unsigned int len, stat, data;
349
350 if (!skb) {
351 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
352 return;
353 }
354
355 /*
356 * Get the current data position.
357 */
358 dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
Russell King32273f52012-01-12 12:45:00 +0000359 len = dma_addr - sg_dma_address(&si->dma_rx.sg);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000360 if (len > HPSIR_MAX_RXLEN)
361 len = HPSIR_MAX_RXLEN;
Russell King32273f52012-01-12 12:45:00 +0000362 dma_unmap_sg(si->dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000363
364 do {
365 /*
366 * Read Status, and then Data.
367 */
368 stat = Ser2HSSR1;
369 rmb();
370 data = Ser2HSDR;
371
372 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
373 dev->stats.rx_errors++;
374 if (stat & HSSR1_CRE)
375 dev->stats.rx_crc_errors++;
376 if (stat & HSSR1_ROR)
377 dev->stats.rx_frame_errors++;
378 } else
379 skb->data[len++] = data;
380
381 /*
382 * If we hit the end of frame, there's
383 * no point in continuing.
384 */
385 if (stat & HSSR1_EOF)
386 break;
387 } while (Ser2HSSR0 & HSSR0_EIF);
388
389 if (stat & HSSR1_EOF) {
390 si->dma_rx.skb = NULL;
391
392 skb_put(skb, len);
393 skb->dev = dev;
394 skb_reset_mac_header(skb);
395 skb->protocol = htons(ETH_P_IRDA);
396 dev->stats.rx_packets++;
397 dev->stats.rx_bytes += len;
398
399 /*
400 * Before we pass the buffer up, allocate a new one.
401 */
402 sa1100_irda_rx_alloc(si);
403
404 netif_rx(skb);
405 } else {
406 /*
407 * Remap the buffer - it was previously mapped, and we
408 * hope that this succeeds.
409 */
Russell King32273f52012-01-12 12:45:00 +0000410 dma_map_sg(si->dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000411 }
412}
413
414/*
415 * We only have to handle RX events here; transmit events go via the TX
416 * DMA handler. We disable RX, process, and the restart RX.
417 */
418static irqreturn_t sa1100_irda_fir_irq(struct net_device *dev, struct sa1100_irda *si)
419{
420 /*
421 * Stop RX DMA
422 */
423 sa1100_stop_dma(si->dma_rx.regs);
424
425 /*
426 * Framing error - we throw away the packet completely.
427 * Clearing RXE flushes the error conditions and data
428 * from the fifo.
429 */
430 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
431 dev->stats.rx_errors++;
432
433 if (Ser2HSSR0 & HSSR0_FRE)
434 dev->stats.rx_frame_errors++;
435
436 /*
437 * Clear out the DMA...
438 */
Russell King6a7f4912012-01-08 20:49:28 +0000439 Ser2HSCR0 = HSCR0_HSSP;
Russell Kinga6b2ea62012-01-08 17:10:01 +0000440
441 /*
442 * Clear selected status bits now, so we
443 * don't miss them next time around.
444 */
445 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
446 }
447
448 /*
449 * Deal with any receive errors. The any of the lowest
450 * 8 bytes in the FIFO may contain an error. We must read
451 * them one by one. The "error" could even be the end of
452 * packet!
453 */
454 if (Ser2HSSR0 & HSSR0_EIF)
455 sa1100_irda_fir_error(si, dev);
456
457 /*
458 * No matter what happens, we must restart reception.
459 */
460 sa1100_irda_rx_dma_start(si);
461
462 return IRQ_HANDLED;
463}
Russell King3d26db12012-01-08 16:16:39 +0000464
465/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * Set the IrDA communications speed.
467 */
468static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
469{
470 unsigned long flags;
471 int brd, ret = -EINVAL;
472
473 switch (speed) {
474 case 9600: case 19200: case 38400:
475 case 57600: case 115200:
476 brd = 3686400 / (16 * speed) - 1;
477
478 /*
479 * Stop the receive DMA.
480 */
481 if (IS_FIR(si))
Russell King885767c2012-01-08 12:53:22 +0000482 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 local_irq_save(flags);
485
486 Ser2UTCR3 = 0;
487 Ser2HSCR0 = HSCR0_UART;
488
489 Ser2UTCR1 = brd >> 8;
490 Ser2UTCR2 = brd;
491
492 /*
493 * Clear status register
494 */
495 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
496 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
497
498 if (si->pdata->set_speed)
499 si->pdata->set_speed(si->dev, speed);
500
501 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000502 si->tx_start = sa1100_irda_sir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000503 si->irq = sa1100_irda_sir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 local_irq_restore(flags);
506 ret = 0;
507 break;
508
509 case 4000000:
510 local_irq_save(flags);
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 Ser2HSSR0 = 0xff;
Russell King6a7f4912012-01-08 20:49:28 +0000513 Ser2HSCR0 = HSCR0_HSSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 Ser2UTCR3 = 0;
515
516 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000517 si->tx_start = sa1100_irda_fir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000518 si->irq = sa1100_irda_fir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 if (si->pdata->set_speed)
521 si->pdata->set_speed(si->dev, speed);
522
523 sa1100_irda_rx_alloc(si);
524 sa1100_irda_rx_dma_start(si);
525
526 local_irq_restore(flags);
527
528 break;
529
530 default:
531 break;
532 }
533
534 return ret;
535}
536
537/*
538 * Control the power state of the IrDA transmitter.
539 * State:
540 * 0 - off
541 * 1 - short range, lowest power
542 * 2 - medium range, medium power
543 * 3 - maximum range, high power
544 *
545 * Currently, only assabet is known to support this.
546 */
547static int
548__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
549{
550 int ret = 0;
551 if (si->pdata->set_power)
552 ret = si->pdata->set_power(si->dev, state);
553 return ret;
554}
555
556static inline int
557sa1100_set_power(struct sa1100_irda *si, unsigned int state)
558{
559 int ret;
560
561 ret = __sa1100_irda_set_power(si, state);
562 if (ret == 0)
563 si->power = state;
564
565 return ret;
566}
567
David Howells7d12e782006-10-05 14:55:46 +0100568static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct net_device *dev = dev_id;
Russell King374f7732012-01-08 16:26:15 +0000571 struct sa1100_irda *si = netdev_priv(dev);
572
573 return si->irq(dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
577{
Wang Chen4cf16532008-11-12 23:38:14 -0800578 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 int speed = irda_get_next_speed(skb);
580
581 /*
582 * Does this packet contain a request to change the interface
583 * speed? If so, remember it until we complete the transmission
584 * of this frame.
585 */
586 if (speed != si->speed && speed != -1)
587 si->newspeed = speed;
588
Russell King3d26db12012-01-08 16:16:39 +0000589 /* If this is an empty frame, we can bypass a lot. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (skb->len == 0) {
Russell King0e888ee2012-01-08 16:30:44 +0000591 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000593 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
Russell King3d26db12012-01-08 16:16:39 +0000596 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Russell King3d26db12012-01-08 16:16:39 +0000598 /* We must not already have a skb to transmit... */
599 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Russell King3d26db12012-01-08 16:16:39 +0000601 return si->tx_start(skb, dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604static int
605sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
606{
607 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800608 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 int ret = -EOPNOTSUPP;
610
611 switch (cmd) {
612 case SIOCSBANDWIDTH:
613 if (capable(CAP_NET_ADMIN)) {
614 /*
615 * We are unable to set the speed if the
616 * device is not running.
617 */
618 if (si->open) {
619 ret = sa1100_irda_set_speed(si,
620 rq->ifr_baudrate);
621 } else {
622 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
623 ret = 0;
624 }
625 }
626 break;
627
628 case SIOCSMEDIABUSY:
629 ret = -EPERM;
630 if (capable(CAP_NET_ADMIN)) {
631 irda_device_set_media_busy(dev, TRUE);
632 ret = 0;
633 }
634 break;
635
636 case SIOCGRECEIVING:
637 rq->ifr_receiving = IS_FIR(si) ? 0
638 : si->rx_buff.state != OUTSIDE_FRAME;
639 break;
640
641 default:
642 break;
643 }
644
645 return ret;
646}
647
Russell Kingcbe1d242012-01-08 16:40:07 +0000648static int sa1100_irda_startup(struct sa1100_irda *si)
649{
650 int ret;
651
652 /*
653 * Ensure that the ports for this device are setup correctly.
654 */
655 if (si->pdata->startup) {
656 ret = si->pdata->startup(si->dev);
657 if (ret)
658 return ret;
659 }
660
661 /*
662 * Configure PPC for IRDA - we want to drive TXD2 low.
663 * We also want to drive this pin low during sleep.
664 */
665 PPSR &= ~PPC_TXD2;
666 PSDR &= ~PPC_TXD2;
667 PPDR |= PPC_TXD2;
668
669 /*
670 * Enable HP-SIR modulation, and ensure that the port is disabled.
671 */
672 Ser2UTCR3 = 0;
673 Ser2HSCR0 = HSCR0_UART;
674 Ser2UTCR4 = si->utcr4;
675 Ser2UTCR0 = UTCR0_8BitData;
676 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
677
678 /*
679 * Clear status register
680 */
681 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
682
683 ret = sa1100_irda_set_speed(si, si->speed = 9600);
684 if (ret) {
685 Ser2UTCR3 = 0;
686 Ser2HSCR0 = 0;
687
688 if (si->pdata->shutdown)
689 si->pdata->shutdown(si->dev);
690 }
691
692 return ret;
693}
694
695static void sa1100_irda_shutdown(struct sa1100_irda *si)
696{
697 /*
698 * Stop all DMA activity.
699 */
700 sa1100_stop_dma(si->dma_rx.regs);
701 sa1100_stop_dma(si->dma_tx.regs);
702
703 /* Disable the port. */
704 Ser2UTCR3 = 0;
705 Ser2HSCR0 = 0;
706
707 if (si->pdata->shutdown)
708 si->pdata->shutdown(si->dev);
709}
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711static int sa1100_irda_start(struct net_device *dev)
712{
Wang Chen4cf16532008-11-12 23:38:14 -0800713 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 int err;
715
716 si->speed = 9600;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
Russell King885767c2012-01-08 12:53:22 +0000719 NULL, NULL, &si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (err)
721 goto err_rx_dma;
722
723 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
Russell King26f2bee2012-01-08 17:48:02 +0000724 sa1100_irda_firtxdma_irq, dev,
725 &si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (err)
727 goto err_tx_dma;
728
729 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 * Setup the serial port for the specified speed.
731 */
732 err = sa1100_irda_startup(si);
733 if (err)
734 goto err_startup;
735
736 /*
737 * Open a new IrLAP layer instance.
738 */
739 si->irlap = irlap_open(dev, &si->qos, "sa1100");
740 err = -ENOMEM;
741 if (!si->irlap)
742 goto err_irlap;
743
Russell King374f7732012-01-08 16:26:15 +0000744 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
745 if (err)
746 goto err_irq;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 /*
749 * Now enable the interrupt and start the queue
750 */
751 si->open = 1;
752 sa1100_set_power(si, power_level); /* low power mode */
Russell King374f7732012-01-08 16:26:15 +0000753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 netif_start_queue(dev);
755 return 0;
756
Russell King374f7732012-01-08 16:26:15 +0000757err_irq:
758 irlap_close(si->irlap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759err_irlap:
760 si->open = 0;
761 sa1100_irda_shutdown(si);
762err_startup:
Russell King885767c2012-01-08 12:53:22 +0000763 sa1100_free_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764err_tx_dma:
Russell King885767c2012-01-08 12:53:22 +0000765 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766err_rx_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return err;
768}
769
770static int sa1100_irda_stop(struct net_device *dev)
771{
Wang Chen4cf16532008-11-12 23:38:14 -0800772 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000773 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Russell King374f7732012-01-08 16:26:15 +0000775 netif_stop_queue(dev);
776
777 si->open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 sa1100_irda_shutdown(si);
779
780 /*
Russell Kingba845252012-01-08 15:38:15 +0000781 * If we have been doing any DMA activity, make sure we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * tidy that up cleanly.
783 */
Russell Kingba845252012-01-08 15:38:15 +0000784 skb = si->dma_rx.skb;
785 if (skb) {
Russell King32273f52012-01-12 12:45:00 +0000786 dma_unmap_sg(si->dev, &si->dma_rx.sg, 1,
787 DMA_FROM_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000788 dev_kfree_skb(skb);
Russell King885767c2012-01-08 12:53:22 +0000789 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Russell Kingba845252012-01-08 15:38:15 +0000792 skb = si->dma_tx.skb;
793 if (skb) {
Russell King32273f52012-01-12 12:45:00 +0000794 dma_unmap_sg(si->dev, &si->dma_tx.sg, 1,
795 DMA_TO_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000796 dev_kfree_skb(skb);
797 si->dma_tx.skb = NULL;
798 }
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* Stop IrLAP */
801 if (si->irlap) {
802 irlap_close(si->irlap);
803 si->irlap = NULL;
804 }
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
807 * Free resources
808 */
Russell King885767c2012-01-08 12:53:22 +0000809 sa1100_free_dma(si->dma_tx.regs);
810 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 free_irq(dev->irq, dev);
812
813 sa1100_set_power(si, 0);
814
815 return 0;
816}
817
818static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
819{
820 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
821 if (io->head != NULL) {
822 io->truesize = size;
823 io->in_frame = FALSE;
824 io->state = OUTSIDE_FRAME;
825 io->data = io->head;
826 }
827 return io->head ? 0 : -ENOMEM;
828}
829
Alexander Beregalova1de9662009-04-15 12:52:42 +0000830static const struct net_device_ops sa1100_irda_netdev_ops = {
831 .ndo_open = sa1100_irda_start,
832 .ndo_stop = sa1100_irda_stop,
833 .ndo_start_xmit = sa1100_irda_hard_xmit,
834 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000835};
836
Russell King3ae5eae2005-11-09 22:32:44 +0000837static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 struct net_device *dev;
840 struct sa1100_irda *si;
841 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000842 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 if (!pdev->dev.platform_data)
845 return -EINVAL;
846
Russell Kinge556fdb2012-01-08 12:02:17 +0000847 irq = platform_get_irq(pdev, 0);
848 if (irq <= 0)
849 return irq < 0 ? irq : -ENXIO;
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
852 if (err)
853 goto err_mem_1;
854 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
855 if (err)
856 goto err_mem_2;
857 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
858 if (err)
859 goto err_mem_3;
860
861 dev = alloc_irdadev(sizeof(struct sa1100_irda));
862 if (!dev)
863 goto err_mem_4;
864
Russell Kingd3238602012-01-08 12:07:24 +0000865 SET_NETDEV_DEV(dev, &pdev->dev);
866
Wang Chen4cf16532008-11-12 23:38:14 -0800867 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 si->dev = &pdev->dev;
869 si->pdata = pdev->dev.platform_data;
870
Russell King32273f52012-01-12 12:45:00 +0000871 sg_init_table(&si->dma_rx.sg, 1);
872 sg_init_table(&si->dma_tx.sg, 1);
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /*
875 * Initialise the HP-SIR buffers
876 */
877 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
878 if (err)
879 goto err_mem_5;
880 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
881 if (err)
882 goto err_mem_5;
883
Alexander Beregalova1de9662009-04-15 12:52:42 +0000884 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000885 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 irda_init_max_qos_capabilies(&si->qos);
888
889 /*
890 * We support original IRDA up to 115k2. (we don't currently
891 * support 4Mbps). Min Turn Time set to 1ms or greater.
892 */
893 baudrate_mask = IR_9600;
894
895 switch (max_rate) {
896 case 4000000: baudrate_mask |= IR_4000000 << 8;
897 case 115200: baudrate_mask |= IR_115200;
898 case 57600: baudrate_mask |= IR_57600;
899 case 38400: baudrate_mask |= IR_38400;
900 case 19200: baudrate_mask |= IR_19200;
901 }
902
903 si->qos.baud_rate.bits &= baudrate_mask;
904 si->qos.min_turn_time.bits = 7;
905
906 irda_qos_bits_to_value(&si->qos);
907
908 si->utcr4 = UTCR4_HPSIR;
909 if (tx_lpm)
910 si->utcr4 |= UTCR4_Z1_6us;
911
912 /*
913 * Initially enable HP-SIR modulation, and ensure that the port
914 * is disabled.
915 */
916 Ser2UTCR3 = 0;
917 Ser2UTCR4 = si->utcr4;
918 Ser2HSCR0 = HSCR0_UART;
919
920 err = register_netdev(dev);
921 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000922 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (err) {
925 err_mem_5:
926 kfree(si->tx_buff.head);
927 kfree(si->rx_buff.head);
928 free_netdev(dev);
929 err_mem_4:
930 release_mem_region(__PREG(Ser2HSCR2), 0x04);
931 err_mem_3:
932 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
933 err_mem_2:
934 release_mem_region(__PREG(Ser2UTCR0), 0x24);
935 }
936 err_mem_1:
937 return err;
938}
939
Russell King3ae5eae2005-11-09 22:32:44 +0000940static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Russell King3ae5eae2005-11-09 22:32:44 +0000942 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -0800945 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 unregister_netdev(dev);
947 kfree(si->tx_buff.head);
948 kfree(si->rx_buff.head);
949 free_netdev(dev);
950 }
951
952 release_mem_region(__PREG(Ser2HSCR2), 0x04);
953 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
954 release_mem_region(__PREG(Ser2UTCR0), 0x24);
955
956 return 0;
957}
958
Russell Kingcbe1d242012-01-08 16:40:07 +0000959#ifdef CONFIG_PM
960/*
961 * Suspend the IrDA interface.
962 */
963static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
964{
965 struct net_device *dev = platform_get_drvdata(pdev);
966 struct sa1100_irda *si;
967
968 if (!dev)
969 return 0;
970
971 si = netdev_priv(dev);
972 if (si->open) {
973 /*
974 * Stop the transmit queue
975 */
976 netif_device_detach(dev);
977 disable_irq(dev->irq);
978 sa1100_irda_shutdown(si);
979 __sa1100_irda_set_power(si, 0);
980 }
981
982 return 0;
983}
984
985/*
986 * Resume the IrDA interface.
987 */
988static int sa1100_irda_resume(struct platform_device *pdev)
989{
990 struct net_device *dev = platform_get_drvdata(pdev);
991 struct sa1100_irda *si;
992
993 if (!dev)
994 return 0;
995
996 si = netdev_priv(dev);
997 if (si->open) {
998 /*
999 * If we missed a speed change, initialise at the new speed
1000 * directly. It is debatable whether this is actually
1001 * required, but in the interests of continuing from where
1002 * we left off it is desirable. The converse argument is
1003 * that we should re-negotiate at 9600 baud again.
1004 */
1005 if (si->newspeed) {
1006 si->speed = si->newspeed;
1007 si->newspeed = 0;
1008 }
1009
1010 sa1100_irda_startup(si);
1011 __sa1100_irda_set_power(si, si->power);
1012 enable_irq(dev->irq);
1013
1014 /*
1015 * This automatically wakes up the queue
1016 */
1017 netif_device_attach(dev);
1018 }
1019
1020 return 0;
1021}
1022#else
1023#define sa1100_irda_suspend NULL
1024#define sa1100_irda_resume NULL
1025#endif
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");