blob: 61b42d1265b4e2bee49101df7472a7202f72babf [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;
Russell King3d26db12012-01-08 16:16:39 +000071
72 int (*tx_start)(struct sk_buff *, struct net_device *, struct sa1100_irda *);
Russell King374f7732012-01-08 16:26:15 +000073 irqreturn_t (*irq)(struct net_device *, struct sa1100_irda *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Russell King0e888ee2012-01-08 16:30:44 +000076static int sa1100_irda_set_speed(struct sa1100_irda *, int);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define IS_FIR(si) ((si)->speed >= 4000000)
79
80#define HPSIR_MAX_RXLEN 2047
81
82/*
83 * Allocate and map the receive buffer, unless it is already allocated.
84 */
85static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
86{
Russell King885767c2012-01-08 12:53:22 +000087 if (si->dma_rx.skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
89
Russell King885767c2012-01-08 12:53:22 +000090 si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
91 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
93 return -ENOMEM;
94 }
95
96 /*
97 * Align any IP headers that may be contained
98 * within the frame.
99 */
Russell King885767c2012-01-08 12:53:22 +0000100 skb_reserve(si->dma_rx.skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Russell King885767c2012-01-08 12:53:22 +0000102 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 HPSIR_MAX_RXLEN,
104 DMA_FROM_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000105 if (dma_mapping_error(si->dev, si->dma_rx.dma)) {
106 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000107 return -ENOMEM;
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111}
112
113/*
114 * We want to get here as soon as possible, and get the receiver setup.
115 * We use the existing buffer.
116 */
117static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
118{
Russell King885767c2012-01-08 12:53:22 +0000119 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
121 return;
122 }
123
124 /*
125 * First empty receive FIFO
126 */
127 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
128
129 /*
130 * Enable the DMA, receiver and receive interrupt.
131 */
Russell King885767c2012-01-08 12:53:22 +0000132 sa1100_clear_dma(si->dma_rx.regs);
133 sa1100_start_dma(si->dma_rx.regs, si->dma_rx.dma, HPSIR_MAX_RXLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
135}
136
Russell King0e888ee2012-01-08 16:30:44 +0000137static void sa1100_irda_check_speed(struct sa1100_irda *si)
138{
139 if (si->newspeed) {
140 sa1100_irda_set_speed(si, si->newspeed);
141 si->newspeed = 0;
142 }
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
Russell King3d26db12012-01-08 16:16:39 +0000146 * HP-SIR format support.
147 */
148static int sa1100_irda_sir_tx_start(struct sk_buff *skb, struct net_device *dev,
149 struct sa1100_irda *si)
150{
151 si->tx_buff.data = si->tx_buff.head;
152 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
153 si->tx_buff.truesize);
154
155 /*
156 * Set the transmit interrupt enable. This will fire off an
157 * interrupt immediately. Note that we disable the receiver
158 * so we won't get spurious characters received.
159 */
160 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
161
162 dev_kfree_skb(skb);
163
164 return NETDEV_TX_OK;
165}
166
Russell Kinga6b2ea62012-01-08 17:10:01 +0000167static irqreturn_t sa1100_irda_sir_irq(struct net_device *dev, struct sa1100_irda *si)
168{
169 int status;
170
171 status = Ser2UTSR0;
172
173 /*
174 * Deal with any receive errors first. The bytes in error may be
175 * the only bytes in the receive FIFO, so we do this first.
176 */
177 while (status & UTSR0_EIF) {
178 int stat, data;
179
180 stat = Ser2UTSR1;
181 data = Ser2UTDR;
182
183 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
184 dev->stats.rx_errors++;
185 if (stat & UTSR1_FRE)
186 dev->stats.rx_frame_errors++;
187 if (stat & UTSR1_ROR)
188 dev->stats.rx_fifo_errors++;
189 } else
190 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
191
192 status = Ser2UTSR0;
193 }
194
195 /*
196 * We must clear certain bits.
197 */
198 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
199
200 if (status & UTSR0_RFS) {
201 /*
202 * There are at least 4 bytes in the FIFO. Read 3 bytes
203 * and leave the rest to the block below.
204 */
205 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
206 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
207 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
208 }
209
210 if (status & (UTSR0_RFS | UTSR0_RID)) {
211 /*
212 * Fifo contains more than 1 character.
213 */
214 do {
215 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
216 Ser2UTDR);
217 } while (Ser2UTSR1 & UTSR1_RNE);
218
219 }
220
221 if (status & UTSR0_TFS && si->tx_buff.len) {
222 /*
223 * Transmitter FIFO is not full
224 */
225 do {
226 Ser2UTDR = *si->tx_buff.data++;
227 si->tx_buff.len -= 1;
228 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
229
230 if (si->tx_buff.len == 0) {
231 dev->stats.tx_packets++;
232 dev->stats.tx_bytes += si->tx_buff.data -
233 si->tx_buff.head;
234
235 /*
236 * We need to ensure that the transmitter has
237 * finished.
238 */
239 do
240 rmb();
241 while (Ser2UTSR1 & UTSR1_TBY);
242
243 /*
244 * Ok, we've finished transmitting. Now enable
245 * the receiver. Sometimes we get a receive IRQ
246 * immediately after a transmit...
247 */
248 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
249 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
250
251 sa1100_irda_check_speed(si);
252
253 /* I'm hungry! */
254 netif_wake_queue(dev);
255 }
256 }
257
258 return IRQ_HANDLED;
259}
260
Russell King3d26db12012-01-08 16:16:39 +0000261/*
262 * FIR format support.
263 */
264static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
265 struct sa1100_irda *si)
266{
267 int mtt = irda_get_mtt(skb);
268
269 si->dma_tx.skb = skb;
270 si->dma_tx.dma = dma_map_single(si->dev, skb->data, skb->len,
271 DMA_TO_DEVICE);
272 if (dma_mapping_error(si->dev, si->dma_tx.dma)) {
273 si->dma_tx.skb = NULL;
274 netif_wake_queue(dev);
275 dev->stats.tx_dropped++;
276 dev_kfree_skb(skb);
277 return NETDEV_TX_OK;
278 }
279
280 sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len);
281
282 /*
283 * If we have a mean turn-around time, impose the specified
284 * specified delay. We could shorten this by timing from
285 * the point we received the packet.
286 */
287 if (mtt)
288 udelay(mtt);
289
290 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
291
292 return NETDEV_TX_OK;
293}
294
Russell Kinga6b2ea62012-01-08 17:10:01 +0000295static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
296{
297 struct sk_buff *skb = si->dma_rx.skb;
298 dma_addr_t dma_addr;
299 unsigned int len, stat, data;
300
301 if (!skb) {
302 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
303 return;
304 }
305
306 /*
307 * Get the current data position.
308 */
309 dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
310 len = dma_addr - si->dma_rx.dma;
311 if (len > HPSIR_MAX_RXLEN)
312 len = HPSIR_MAX_RXLEN;
313 dma_unmap_single(si->dev, si->dma_rx.dma, len, DMA_FROM_DEVICE);
314
315 do {
316 /*
317 * Read Status, and then Data.
318 */
319 stat = Ser2HSSR1;
320 rmb();
321 data = Ser2HSDR;
322
323 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
324 dev->stats.rx_errors++;
325 if (stat & HSSR1_CRE)
326 dev->stats.rx_crc_errors++;
327 if (stat & HSSR1_ROR)
328 dev->stats.rx_frame_errors++;
329 } else
330 skb->data[len++] = data;
331
332 /*
333 * If we hit the end of frame, there's
334 * no point in continuing.
335 */
336 if (stat & HSSR1_EOF)
337 break;
338 } while (Ser2HSSR0 & HSSR0_EIF);
339
340 if (stat & HSSR1_EOF) {
341 si->dma_rx.skb = NULL;
342
343 skb_put(skb, len);
344 skb->dev = dev;
345 skb_reset_mac_header(skb);
346 skb->protocol = htons(ETH_P_IRDA);
347 dev->stats.rx_packets++;
348 dev->stats.rx_bytes += len;
349
350 /*
351 * Before we pass the buffer up, allocate a new one.
352 */
353 sa1100_irda_rx_alloc(si);
354
355 netif_rx(skb);
356 } else {
357 /*
358 * Remap the buffer - it was previously mapped, and we
359 * hope that this succeeds.
360 */
361 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
362 HPSIR_MAX_RXLEN,
363 DMA_FROM_DEVICE);
364 }
365}
366
367/*
368 * We only have to handle RX events here; transmit events go via the TX
369 * DMA handler. We disable RX, process, and the restart RX.
370 */
371static irqreturn_t sa1100_irda_fir_irq(struct net_device *dev, struct sa1100_irda *si)
372{
373 /*
374 * Stop RX DMA
375 */
376 sa1100_stop_dma(si->dma_rx.regs);
377
378 /*
379 * Framing error - we throw away the packet completely.
380 * Clearing RXE flushes the error conditions and data
381 * from the fifo.
382 */
383 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
384 dev->stats.rx_errors++;
385
386 if (Ser2HSSR0 & HSSR0_FRE)
387 dev->stats.rx_frame_errors++;
388
389 /*
390 * Clear out the DMA...
391 */
392 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
393
394 /*
395 * Clear selected status bits now, so we
396 * don't miss them next time around.
397 */
398 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
399 }
400
401 /*
402 * Deal with any receive errors. The any of the lowest
403 * 8 bytes in the FIFO may contain an error. We must read
404 * them one by one. The "error" could even be the end of
405 * packet!
406 */
407 if (Ser2HSSR0 & HSSR0_EIF)
408 sa1100_irda_fir_error(si, dev);
409
410 /*
411 * No matter what happens, we must restart reception.
412 */
413 sa1100_irda_rx_dma_start(si);
414
415 return IRQ_HANDLED;
416}
Russell King3d26db12012-01-08 16:16:39 +0000417
418/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 * Set the IrDA communications speed.
420 */
421static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
422{
423 unsigned long flags;
424 int brd, ret = -EINVAL;
425
426 switch (speed) {
427 case 9600: case 19200: case 38400:
428 case 57600: case 115200:
429 brd = 3686400 / (16 * speed) - 1;
430
431 /*
432 * Stop the receive DMA.
433 */
434 if (IS_FIR(si))
Russell King885767c2012-01-08 12:53:22 +0000435 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 local_irq_save(flags);
438
439 Ser2UTCR3 = 0;
440 Ser2HSCR0 = HSCR0_UART;
441
442 Ser2UTCR1 = brd >> 8;
443 Ser2UTCR2 = brd;
444
445 /*
446 * Clear status register
447 */
448 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
449 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
450
451 if (si->pdata->set_speed)
452 si->pdata->set_speed(si->dev, speed);
453
454 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000455 si->tx_start = sa1100_irda_sir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000456 si->irq = sa1100_irda_sir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 local_irq_restore(flags);
459 ret = 0;
460 break;
461
462 case 4000000:
463 local_irq_save(flags);
464
465 si->hscr0 = 0;
466
467 Ser2HSSR0 = 0xff;
468 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
469 Ser2UTCR3 = 0;
470
471 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000472 si->tx_start = sa1100_irda_fir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000473 si->irq = sa1100_irda_fir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (si->pdata->set_speed)
476 si->pdata->set_speed(si->dev, speed);
477
478 sa1100_irda_rx_alloc(si);
479 sa1100_irda_rx_dma_start(si);
480
481 local_irq_restore(flags);
482
483 break;
484
485 default:
486 break;
487 }
488
489 return ret;
490}
491
492/*
493 * Control the power state of the IrDA transmitter.
494 * State:
495 * 0 - off
496 * 1 - short range, lowest power
497 * 2 - medium range, medium power
498 * 3 - maximum range, high power
499 *
500 * Currently, only assabet is known to support this.
501 */
502static int
503__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
504{
505 int ret = 0;
506 if (si->pdata->set_power)
507 ret = si->pdata->set_power(si->dev, state);
508 return ret;
509}
510
511static inline int
512sa1100_set_power(struct sa1100_irda *si, unsigned int state)
513{
514 int ret;
515
516 ret = __sa1100_irda_set_power(si, state);
517 if (ret == 0)
518 si->power = state;
519
520 return ret;
521}
522
David Howells7d12e782006-10-05 14:55:46 +0100523static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct net_device *dev = dev_id;
Russell King374f7732012-01-08 16:26:15 +0000526 struct sa1100_irda *si = netdev_priv(dev);
527
528 return si->irq(dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531/*
532 * TX DMA completion handler.
533 */
534static void sa1100_irda_txdma_irq(void *id)
535{
536 struct net_device *dev = id;
Wang Chen4cf16532008-11-12 23:38:14 -0800537 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000538 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /*
541 * Wait for the transmission to complete. Unfortunately,
542 * the hardware doesn't give us an interrupt to indicate
543 * "end of frame".
544 */
545 do
546 rmb();
547 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
548
549 /*
550 * Clear the transmit underrun bit.
551 */
552 Ser2HSSR0 = HSSR0_TUR;
553
554 /*
555 * Do we need to change speed? Note that we're lazy
Russell King885767c2012-01-08 12:53:22 +0000556 * here - we don't free the old dma_rx.skb. We don't need
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * to allocate a buffer either.
558 */
Russell King0e888ee2012-01-08 16:30:44 +0000559 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 /*
562 * Start reception. This disables the transmitter for
563 * us. This will be using the existing RX buffer.
564 */
565 sa1100_irda_rx_dma_start(si);
566
Russell Kingba845252012-01-08 15:38:15 +0000567 /* Account and free the packet. */
568 skb = si->dma_tx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (skb) {
Russell Kingba845252012-01-08 15:38:15 +0000570 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
571 DMA_TO_DEVICE);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800572 dev->stats.tx_packets ++;
573 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 dev_kfree_skb_irq(skb);
Russell Kingba845252012-01-08 15:38:15 +0000575 si->dma_tx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 /*
579 * Make sure that the TX queue is available for sending
580 * (for retries). TX has priority over RX at all times.
581 */
582 netif_wake_queue(dev);
583}
584
585static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
586{
Wang Chen4cf16532008-11-12 23:38:14 -0800587 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int speed = irda_get_next_speed(skb);
589
590 /*
591 * Does this packet contain a request to change the interface
592 * speed? If so, remember it until we complete the transmission
593 * of this frame.
594 */
595 if (speed != si->speed && speed != -1)
596 si->newspeed = speed;
597
Russell King3d26db12012-01-08 16:16:39 +0000598 /* If this is an empty frame, we can bypass a lot. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (skb->len == 0) {
Russell King0e888ee2012-01-08 16:30:44 +0000600 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000602 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604
Russell King3d26db12012-01-08 16:16:39 +0000605 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Russell King3d26db12012-01-08 16:16:39 +0000607 /* We must not already have a skb to transmit... */
608 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Russell King3d26db12012-01-08 16:16:39 +0000610 return si->tx_start(skb, dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613static int
614sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
615{
616 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800617 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int ret = -EOPNOTSUPP;
619
620 switch (cmd) {
621 case SIOCSBANDWIDTH:
622 if (capable(CAP_NET_ADMIN)) {
623 /*
624 * We are unable to set the speed if the
625 * device is not running.
626 */
627 if (si->open) {
628 ret = sa1100_irda_set_speed(si,
629 rq->ifr_baudrate);
630 } else {
631 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
632 ret = 0;
633 }
634 }
635 break;
636
637 case SIOCSMEDIABUSY:
638 ret = -EPERM;
639 if (capable(CAP_NET_ADMIN)) {
640 irda_device_set_media_busy(dev, TRUE);
641 ret = 0;
642 }
643 break;
644
645 case SIOCGRECEIVING:
646 rq->ifr_receiving = IS_FIR(si) ? 0
647 : si->rx_buff.state != OUTSIDE_FRAME;
648 break;
649
650 default:
651 break;
652 }
653
654 return ret;
655}
656
Russell Kingcbe1d242012-01-08 16:40:07 +0000657static int sa1100_irda_startup(struct sa1100_irda *si)
658{
659 int ret;
660
661 /*
662 * Ensure that the ports for this device are setup correctly.
663 */
664 if (si->pdata->startup) {
665 ret = si->pdata->startup(si->dev);
666 if (ret)
667 return ret;
668 }
669
670 /*
671 * Configure PPC for IRDA - we want to drive TXD2 low.
672 * We also want to drive this pin low during sleep.
673 */
674 PPSR &= ~PPC_TXD2;
675 PSDR &= ~PPC_TXD2;
676 PPDR |= PPC_TXD2;
677
678 /*
679 * Enable HP-SIR modulation, and ensure that the port is disabled.
680 */
681 Ser2UTCR3 = 0;
682 Ser2HSCR0 = HSCR0_UART;
683 Ser2UTCR4 = si->utcr4;
684 Ser2UTCR0 = UTCR0_8BitData;
685 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
686
687 /*
688 * Clear status register
689 */
690 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
691
692 ret = sa1100_irda_set_speed(si, si->speed = 9600);
693 if (ret) {
694 Ser2UTCR3 = 0;
695 Ser2HSCR0 = 0;
696
697 if (si->pdata->shutdown)
698 si->pdata->shutdown(si->dev);
699 }
700
701 return ret;
702}
703
704static void sa1100_irda_shutdown(struct sa1100_irda *si)
705{
706 /*
707 * Stop all DMA activity.
708 */
709 sa1100_stop_dma(si->dma_rx.regs);
710 sa1100_stop_dma(si->dma_tx.regs);
711
712 /* Disable the port. */
713 Ser2UTCR3 = 0;
714 Ser2HSCR0 = 0;
715
716 if (si->pdata->shutdown)
717 si->pdata->shutdown(si->dev);
718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720static int sa1100_irda_start(struct net_device *dev)
721{
Wang Chen4cf16532008-11-12 23:38:14 -0800722 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int err;
724
725 si->speed = 9600;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
Russell King885767c2012-01-08 12:53:22 +0000728 NULL, NULL, &si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (err)
730 goto err_rx_dma;
731
732 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
Russell King885767c2012-01-08 12:53:22 +0000733 sa1100_irda_txdma_irq, dev, &si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (err)
735 goto err_tx_dma;
736
737 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * Setup the serial port for the specified speed.
739 */
740 err = sa1100_irda_startup(si);
741 if (err)
742 goto err_startup;
743
744 /*
745 * Open a new IrLAP layer instance.
746 */
747 si->irlap = irlap_open(dev, &si->qos, "sa1100");
748 err = -ENOMEM;
749 if (!si->irlap)
750 goto err_irlap;
751
Russell King374f7732012-01-08 16:26:15 +0000752 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
753 if (err)
754 goto err_irq;
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * Now enable the interrupt and start the queue
758 */
759 si->open = 1;
760 sa1100_set_power(si, power_level); /* low power mode */
Russell King374f7732012-01-08 16:26:15 +0000761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 netif_start_queue(dev);
763 return 0;
764
Russell King374f7732012-01-08 16:26:15 +0000765err_irq:
766 irlap_close(si->irlap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767err_irlap:
768 si->open = 0;
769 sa1100_irda_shutdown(si);
770err_startup:
Russell King885767c2012-01-08 12:53:22 +0000771 sa1100_free_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772err_tx_dma:
Russell King885767c2012-01-08 12:53:22 +0000773 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774err_rx_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return err;
776}
777
778static int sa1100_irda_stop(struct net_device *dev)
779{
Wang Chen4cf16532008-11-12 23:38:14 -0800780 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000781 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Russell King374f7732012-01-08 16:26:15 +0000783 netif_stop_queue(dev);
784
785 si->open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 sa1100_irda_shutdown(si);
787
788 /*
Russell Kingba845252012-01-08 15:38:15 +0000789 * If we have been doing any DMA activity, make sure we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * tidy that up cleanly.
791 */
Russell Kingba845252012-01-08 15:38:15 +0000792 skb = si->dma_rx.skb;
793 if (skb) {
Russell King885767c2012-01-08 12:53:22 +0000794 dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 DMA_FROM_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000796 dev_kfree_skb(skb);
Russell King885767c2012-01-08 12:53:22 +0000797 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
Russell Kingba845252012-01-08 15:38:15 +0000800 skb = si->dma_tx.skb;
801 if (skb) {
802 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
803 DMA_TO_DEVICE);
804 dev_kfree_skb(skb);
805 si->dma_tx.skb = NULL;
806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 /* Stop IrLAP */
809 if (si->irlap) {
810 irlap_close(si->irlap);
811 si->irlap = NULL;
812 }
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
815 * Free resources
816 */
Russell King885767c2012-01-08 12:53:22 +0000817 sa1100_free_dma(si->dma_tx.regs);
818 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 free_irq(dev->irq, dev);
820
821 sa1100_set_power(si, 0);
822
823 return 0;
824}
825
826static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
827{
828 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
829 if (io->head != NULL) {
830 io->truesize = size;
831 io->in_frame = FALSE;
832 io->state = OUTSIDE_FRAME;
833 io->data = io->head;
834 }
835 return io->head ? 0 : -ENOMEM;
836}
837
Alexander Beregalova1de9662009-04-15 12:52:42 +0000838static const struct net_device_ops sa1100_irda_netdev_ops = {
839 .ndo_open = sa1100_irda_start,
840 .ndo_stop = sa1100_irda_stop,
841 .ndo_start_xmit = sa1100_irda_hard_xmit,
842 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000843};
844
Russell King3ae5eae2005-11-09 22:32:44 +0000845static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct net_device *dev;
848 struct sa1100_irda *si;
849 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000850 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (!pdev->dev.platform_data)
853 return -EINVAL;
854
Russell Kinge556fdb2012-01-08 12:02:17 +0000855 irq = platform_get_irq(pdev, 0);
856 if (irq <= 0)
857 return irq < 0 ? irq : -ENXIO;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
860 if (err)
861 goto err_mem_1;
862 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
863 if (err)
864 goto err_mem_2;
865 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
866 if (err)
867 goto err_mem_3;
868
869 dev = alloc_irdadev(sizeof(struct sa1100_irda));
870 if (!dev)
871 goto err_mem_4;
872
Russell Kingd3238602012-01-08 12:07:24 +0000873 SET_NETDEV_DEV(dev, &pdev->dev);
874
Wang Chen4cf16532008-11-12 23:38:14 -0800875 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 si->dev = &pdev->dev;
877 si->pdata = pdev->dev.platform_data;
878
879 /*
880 * Initialise the HP-SIR buffers
881 */
882 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
883 if (err)
884 goto err_mem_5;
885 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
886 if (err)
887 goto err_mem_5;
888
Alexander Beregalova1de9662009-04-15 12:52:42 +0000889 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000890 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 irda_init_max_qos_capabilies(&si->qos);
893
894 /*
895 * We support original IRDA up to 115k2. (we don't currently
896 * support 4Mbps). Min Turn Time set to 1ms or greater.
897 */
898 baudrate_mask = IR_9600;
899
900 switch (max_rate) {
901 case 4000000: baudrate_mask |= IR_4000000 << 8;
902 case 115200: baudrate_mask |= IR_115200;
903 case 57600: baudrate_mask |= IR_57600;
904 case 38400: baudrate_mask |= IR_38400;
905 case 19200: baudrate_mask |= IR_19200;
906 }
907
908 si->qos.baud_rate.bits &= baudrate_mask;
909 si->qos.min_turn_time.bits = 7;
910
911 irda_qos_bits_to_value(&si->qos);
912
913 si->utcr4 = UTCR4_HPSIR;
914 if (tx_lpm)
915 si->utcr4 |= UTCR4_Z1_6us;
916
917 /*
918 * Initially enable HP-SIR modulation, and ensure that the port
919 * is disabled.
920 */
921 Ser2UTCR3 = 0;
922 Ser2UTCR4 = si->utcr4;
923 Ser2HSCR0 = HSCR0_UART;
924
925 err = register_netdev(dev);
926 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000927 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (err) {
930 err_mem_5:
931 kfree(si->tx_buff.head);
932 kfree(si->rx_buff.head);
933 free_netdev(dev);
934 err_mem_4:
935 release_mem_region(__PREG(Ser2HSCR2), 0x04);
936 err_mem_3:
937 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
938 err_mem_2:
939 release_mem_region(__PREG(Ser2UTCR0), 0x24);
940 }
941 err_mem_1:
942 return err;
943}
944
Russell King3ae5eae2005-11-09 22:32:44 +0000945static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Russell King3ae5eae2005-11-09 22:32:44 +0000947 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -0800950 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 unregister_netdev(dev);
952 kfree(si->tx_buff.head);
953 kfree(si->rx_buff.head);
954 free_netdev(dev);
955 }
956
957 release_mem_region(__PREG(Ser2HSCR2), 0x04);
958 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
959 release_mem_region(__PREG(Ser2UTCR0), 0x24);
960
961 return 0;
962}
963
Russell Kingcbe1d242012-01-08 16:40:07 +0000964#ifdef CONFIG_PM
965/*
966 * Suspend the IrDA interface.
967 */
968static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
969{
970 struct net_device *dev = platform_get_drvdata(pdev);
971 struct sa1100_irda *si;
972
973 if (!dev)
974 return 0;
975
976 si = netdev_priv(dev);
977 if (si->open) {
978 /*
979 * Stop the transmit queue
980 */
981 netif_device_detach(dev);
982 disable_irq(dev->irq);
983 sa1100_irda_shutdown(si);
984 __sa1100_irda_set_power(si, 0);
985 }
986
987 return 0;
988}
989
990/*
991 * Resume the IrDA interface.
992 */
993static int sa1100_irda_resume(struct platform_device *pdev)
994{
995 struct net_device *dev = platform_get_drvdata(pdev);
996 struct sa1100_irda *si;
997
998 if (!dev)
999 return 0;
1000
1001 si = netdev_priv(dev);
1002 if (si->open) {
1003 /*
1004 * If we missed a speed change, initialise at the new speed
1005 * directly. It is debatable whether this is actually
1006 * required, but in the interests of continuing from where
1007 * we left off it is desirable. The converse argument is
1008 * that we should re-negotiate at 9600 baud again.
1009 */
1010 if (si->newspeed) {
1011 si->speed = si->newspeed;
1012 si->newspeed = 0;
1013 }
1014
1015 sa1100_irda_startup(si);
1016 __sa1100_irda_set_power(si, si->power);
1017 enable_irq(dev->irq);
1018
1019 /*
1020 * This automatically wakes up the queue
1021 */
1022 netif_device_attach(dev);
1023 }
1024
1025 return 0;
1026}
1027#else
1028#define sa1100_irda_suspend NULL
1029#define sa1100_irda_resume NULL
1030#endif
1031
Russell King3ae5eae2005-11-09 22:32:44 +00001032static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 .probe = sa1100_irda_probe,
1034 .remove = sa1100_irda_remove,
1035 .suspend = sa1100_irda_suspend,
1036 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001037 .driver = {
1038 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001039 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001040 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041};
1042
1043static int __init sa1100_irda_init(void)
1044{
1045 /*
1046 * Limit power level a sensible range.
1047 */
1048 if (power_level < 1)
1049 power_level = 1;
1050 if (power_level > 3)
1051 power_level = 3;
1052
Russell King3ae5eae2005-11-09 22:32:44 +00001053 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056static void __exit sa1100_irda_exit(void)
1057{
Russell King3ae5eae2005-11-09 22:32:44 +00001058 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061module_init(sa1100_irda_init);
1062module_exit(sa1100_irda_exit);
1063module_param(power_level, int, 0);
1064module_param(tx_lpm, int, 0);
1065module_param(max_rate, int, 0);
1066
1067MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1068MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1069MODULE_LICENSE("GPL");
1070MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1071MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1072MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001073MODULE_ALIAS("platform:sa11x0-ir");