blob: 9c748f38b9d587925bb68caeb98850b205662db6 [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 {
Russell King3c500a32012-01-12 13:56:28 +000047 struct device *dev;
Russell King885767c2012-01-08 12:53:22 +000048 struct sk_buff *skb;
Russell King32273f52012-01-12 12:45:00 +000049 struct scatterlist sg;
Russell King885767c2012-01-08 12:53:22 +000050 dma_regs_t *regs;
51};
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053struct sa1100_irda {
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 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 King32273f52012-01-12 12:45:00 +0000102 sg_set_buf(&si->dma_rx.sg, si->dma_rx.skb->data, HPSIR_MAX_RXLEN);
Russell King3c500a32012-01-12 13:56:28 +0000103 if (dma_map_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE) == 0) {
Russell King885767c2012-01-08 12:53:22 +0000104 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000105 return -ENOMEM;
106 }
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
111/*
112 * We want to get here as soon as possible, and get the receiver setup.
113 * We use the existing buffer.
114 */
115static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
116{
Russell King885767c2012-01-08 12:53:22 +0000117 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
119 return;
120 }
121
122 /*
123 * First empty receive FIFO
124 */
Russell King6a7f4912012-01-08 20:49:28 +0000125 Ser2HSCR0 = HSCR0_HSSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 /*
128 * Enable the DMA, receiver and receive interrupt.
129 */
Russell King885767c2012-01-08 12:53:22 +0000130 sa1100_clear_dma(si->dma_rx.regs);
Russell King32273f52012-01-12 12:45:00 +0000131 sa1100_start_dma(si->dma_rx.regs, sg_dma_address(&si->dma_rx.sg),
132 sg_dma_len(&si->dma_rx.sg));
Russell King6a7f4912012-01-08 20:49:28 +0000133 Ser2HSCR0 = HSCR0_HSSP | HSCR0_RXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Russell King0e888ee2012-01-08 16:30:44 +0000136static void sa1100_irda_check_speed(struct sa1100_irda *si)
137{
138 if (si->newspeed) {
139 sa1100_irda_set_speed(si, si->newspeed);
140 si->newspeed = 0;
141 }
142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
Russell King3d26db12012-01-08 16:16:39 +0000145 * HP-SIR format support.
146 */
147static int sa1100_irda_sir_tx_start(struct sk_buff *skb, struct net_device *dev,
148 struct sa1100_irda *si)
149{
150 si->tx_buff.data = si->tx_buff.head;
151 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
152 si->tx_buff.truesize);
153
154 /*
155 * Set the transmit interrupt enable. This will fire off an
156 * interrupt immediately. Note that we disable the receiver
157 * so we won't get spurious characters received.
158 */
159 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
160
161 dev_kfree_skb(skb);
162
163 return NETDEV_TX_OK;
164}
165
Russell Kinga6b2ea62012-01-08 17:10:01 +0000166static irqreturn_t sa1100_irda_sir_irq(struct net_device *dev, struct sa1100_irda *si)
167{
168 int status;
169
170 status = Ser2UTSR0;
171
172 /*
173 * Deal with any receive errors first. The bytes in error may be
174 * the only bytes in the receive FIFO, so we do this first.
175 */
176 while (status & UTSR0_EIF) {
177 int stat, data;
178
179 stat = Ser2UTSR1;
180 data = Ser2UTDR;
181
182 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
183 dev->stats.rx_errors++;
184 if (stat & UTSR1_FRE)
185 dev->stats.rx_frame_errors++;
186 if (stat & UTSR1_ROR)
187 dev->stats.rx_fifo_errors++;
188 } else
189 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
190
191 status = Ser2UTSR0;
192 }
193
194 /*
195 * We must clear certain bits.
196 */
197 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
198
199 if (status & UTSR0_RFS) {
200 /*
201 * There are at least 4 bytes in the FIFO. Read 3 bytes
202 * and leave the rest to the block below.
203 */
204 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
205 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
206 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
207 }
208
209 if (status & (UTSR0_RFS | UTSR0_RID)) {
210 /*
211 * Fifo contains more than 1 character.
212 */
213 do {
214 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
215 Ser2UTDR);
216 } while (Ser2UTSR1 & UTSR1_RNE);
217
218 }
219
220 if (status & UTSR0_TFS && si->tx_buff.len) {
221 /*
222 * Transmitter FIFO is not full
223 */
224 do {
225 Ser2UTDR = *si->tx_buff.data++;
226 si->tx_buff.len -= 1;
227 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
228
229 if (si->tx_buff.len == 0) {
230 dev->stats.tx_packets++;
231 dev->stats.tx_bytes += si->tx_buff.data -
232 si->tx_buff.head;
233
234 /*
235 * We need to ensure that the transmitter has
236 * finished.
237 */
238 do
239 rmb();
240 while (Ser2UTSR1 & UTSR1_TBY);
241
242 /*
243 * Ok, we've finished transmitting. Now enable
244 * the receiver. Sometimes we get a receive IRQ
245 * immediately after a transmit...
246 */
247 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
248 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
249
250 sa1100_irda_check_speed(si);
251
252 /* I'm hungry! */
253 netif_wake_queue(dev);
254 }
255 }
256
257 return IRQ_HANDLED;
258}
259
Russell King3d26db12012-01-08 16:16:39 +0000260/*
261 * FIR format support.
262 */
Russell King26f2bee2012-01-08 17:48:02 +0000263static void sa1100_irda_firtxdma_irq(void *id)
264{
265 struct net_device *dev = id;
266 struct sa1100_irda *si = netdev_priv(dev);
267 struct sk_buff *skb;
268
269 /*
270 * Wait for the transmission to complete. Unfortunately,
271 * the hardware doesn't give us an interrupt to indicate
272 * "end of frame".
273 */
274 do
275 rmb();
276 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
277
278 /*
279 * Clear the transmit underrun bit.
280 */
281 Ser2HSSR0 = HSSR0_TUR;
282
283 /*
284 * Do we need to change speed? Note that we're lazy
285 * here - we don't free the old dma_rx.skb. We don't need
286 * to allocate a buffer either.
287 */
288 sa1100_irda_check_speed(si);
289
290 /*
291 * Start reception. This disables the transmitter for
292 * us. This will be using the existing RX buffer.
293 */
294 sa1100_irda_rx_dma_start(si);
295
296 /* Account and free the packet. */
297 skb = si->dma_tx.skb;
298 if (skb) {
Russell King3c500a32012-01-12 13:56:28 +0000299 dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1,
Russell King32273f52012-01-12 12:45:00 +0000300 DMA_TO_DEVICE);
Russell King26f2bee2012-01-08 17:48:02 +0000301 dev->stats.tx_packets ++;
302 dev->stats.tx_bytes += skb->len;
303 dev_kfree_skb_irq(skb);
304 si->dma_tx.skb = NULL;
305 }
306
307 /*
308 * Make sure that the TX queue is available for sending
309 * (for retries). TX has priority over RX at all times.
310 */
311 netif_wake_queue(dev);
312}
313
Russell King3d26db12012-01-08 16:16:39 +0000314static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
315 struct sa1100_irda *si)
316{
317 int mtt = irda_get_mtt(skb);
318
319 si->dma_tx.skb = skb;
Russell King32273f52012-01-12 12:45:00 +0000320 sg_set_buf(&si->dma_tx.sg, skb->data, skb->len);
Russell King3c500a32012-01-12 13:56:28 +0000321 if (dma_map_sg(si->dma_tx.dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE) == 0) {
Russell King3d26db12012-01-08 16:16:39 +0000322 si->dma_tx.skb = NULL;
323 netif_wake_queue(dev);
324 dev->stats.tx_dropped++;
325 dev_kfree_skb(skb);
326 return NETDEV_TX_OK;
327 }
328
Russell King32273f52012-01-12 12:45:00 +0000329 sa1100_start_dma(si->dma_tx.regs, sg_dma_address(&si->dma_tx.sg),
330 sg_dma_len(&si->dma_tx.sg));
Russell King3d26db12012-01-08 16:16:39 +0000331
332 /*
333 * If we have a mean turn-around time, impose the specified
334 * specified delay. We could shorten this by timing from
335 * the point we received the packet.
336 */
337 if (mtt)
338 udelay(mtt);
339
Russell King6a7f4912012-01-08 20:49:28 +0000340 Ser2HSCR0 = HSCR0_HSSP | HSCR0_TXE;
Russell King3d26db12012-01-08 16:16:39 +0000341
342 return NETDEV_TX_OK;
343}
344
Russell Kinga6b2ea62012-01-08 17:10:01 +0000345static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
346{
347 struct sk_buff *skb = si->dma_rx.skb;
348 dma_addr_t dma_addr;
349 unsigned int len, stat, data;
350
351 if (!skb) {
352 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
353 return;
354 }
355
356 /*
357 * Get the current data position.
358 */
359 dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
Russell King32273f52012-01-12 12:45:00 +0000360 len = dma_addr - sg_dma_address(&si->dma_rx.sg);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000361 if (len > HPSIR_MAX_RXLEN)
362 len = HPSIR_MAX_RXLEN;
Russell King3c500a32012-01-12 13:56:28 +0000363 dma_unmap_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000364
365 do {
366 /*
367 * Read Status, and then Data.
368 */
369 stat = Ser2HSSR1;
370 rmb();
371 data = Ser2HSDR;
372
373 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
374 dev->stats.rx_errors++;
375 if (stat & HSSR1_CRE)
376 dev->stats.rx_crc_errors++;
377 if (stat & HSSR1_ROR)
378 dev->stats.rx_frame_errors++;
379 } else
380 skb->data[len++] = data;
381
382 /*
383 * If we hit the end of frame, there's
384 * no point in continuing.
385 */
386 if (stat & HSSR1_EOF)
387 break;
388 } while (Ser2HSSR0 & HSSR0_EIF);
389
390 if (stat & HSSR1_EOF) {
391 si->dma_rx.skb = NULL;
392
393 skb_put(skb, len);
394 skb->dev = dev;
395 skb_reset_mac_header(skb);
396 skb->protocol = htons(ETH_P_IRDA);
397 dev->stats.rx_packets++;
398 dev->stats.rx_bytes += len;
399
400 /*
401 * Before we pass the buffer up, allocate a new one.
402 */
403 sa1100_irda_rx_alloc(si);
404
405 netif_rx(skb);
406 } else {
407 /*
408 * Remap the buffer - it was previously mapped, and we
409 * hope that this succeeds.
410 */
Russell King3c500a32012-01-12 13:56:28 +0000411 dma_map_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000412 }
413}
414
415/*
416 * We only have to handle RX events here; transmit events go via the TX
417 * DMA handler. We disable RX, process, and the restart RX.
418 */
419static irqreturn_t sa1100_irda_fir_irq(struct net_device *dev, struct sa1100_irda *si)
420{
421 /*
422 * Stop RX DMA
423 */
424 sa1100_stop_dma(si->dma_rx.regs);
425
426 /*
427 * Framing error - we throw away the packet completely.
428 * Clearing RXE flushes the error conditions and data
429 * from the fifo.
430 */
431 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
432 dev->stats.rx_errors++;
433
434 if (Ser2HSSR0 & HSSR0_FRE)
435 dev->stats.rx_frame_errors++;
436
437 /*
438 * Clear out the DMA...
439 */
Russell King6a7f4912012-01-08 20:49:28 +0000440 Ser2HSCR0 = HSCR0_HSSP;
Russell Kinga6b2ea62012-01-08 17:10:01 +0000441
442 /*
443 * Clear selected status bits now, so we
444 * don't miss them next time around.
445 */
446 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
447 }
448
449 /*
450 * Deal with any receive errors. The any of the lowest
451 * 8 bytes in the FIFO may contain an error. We must read
452 * them one by one. The "error" could even be the end of
453 * packet!
454 */
455 if (Ser2HSSR0 & HSSR0_EIF)
456 sa1100_irda_fir_error(si, dev);
457
458 /*
459 * No matter what happens, we must restart reception.
460 */
461 sa1100_irda_rx_dma_start(si);
462
463 return IRQ_HANDLED;
464}
Russell King3d26db12012-01-08 16:16:39 +0000465
466/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * Set the IrDA communications speed.
468 */
469static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
470{
471 unsigned long flags;
472 int brd, ret = -EINVAL;
473
474 switch (speed) {
475 case 9600: case 19200: case 38400:
476 case 57600: case 115200:
477 brd = 3686400 / (16 * speed) - 1;
478
479 /*
480 * Stop the receive DMA.
481 */
482 if (IS_FIR(si))
Russell King885767c2012-01-08 12:53:22 +0000483 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 local_irq_save(flags);
486
487 Ser2UTCR3 = 0;
488 Ser2HSCR0 = HSCR0_UART;
489
490 Ser2UTCR1 = brd >> 8;
491 Ser2UTCR2 = brd;
492
493 /*
494 * Clear status register
495 */
496 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
497 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
498
499 if (si->pdata->set_speed)
500 si->pdata->set_speed(si->dev, speed);
501
502 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000503 si->tx_start = sa1100_irda_sir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000504 si->irq = sa1100_irda_sir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 local_irq_restore(flags);
507 ret = 0;
508 break;
509
510 case 4000000:
511 local_irq_save(flags);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 Ser2HSSR0 = 0xff;
Russell King6a7f4912012-01-08 20:49:28 +0000514 Ser2HSCR0 = HSCR0_HSSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 Ser2UTCR3 = 0;
516
517 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000518 si->tx_start = sa1100_irda_fir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000519 si->irq = sa1100_irda_fir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 if (si->pdata->set_speed)
522 si->pdata->set_speed(si->dev, speed);
523
524 sa1100_irda_rx_alloc(si);
525 sa1100_irda_rx_dma_start(si);
526
527 local_irq_restore(flags);
528
529 break;
530
531 default:
532 break;
533 }
534
535 return ret;
536}
537
538/*
539 * Control the power state of the IrDA transmitter.
540 * State:
541 * 0 - off
542 * 1 - short range, lowest power
543 * 2 - medium range, medium power
544 * 3 - maximum range, high power
545 *
546 * Currently, only assabet is known to support this.
547 */
548static int
549__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
550{
551 int ret = 0;
552 if (si->pdata->set_power)
553 ret = si->pdata->set_power(si->dev, state);
554 return ret;
555}
556
557static inline int
558sa1100_set_power(struct sa1100_irda *si, unsigned int state)
559{
560 int ret;
561
562 ret = __sa1100_irda_set_power(si, state);
563 if (ret == 0)
564 si->power = state;
565
566 return ret;
567}
568
David Howells7d12e782006-10-05 14:55:46 +0100569static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 struct net_device *dev = dev_id;
Russell King374f7732012-01-08 16:26:15 +0000572 struct sa1100_irda *si = netdev_priv(dev);
573
574 return si->irq(dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
578{
Wang Chen4cf16532008-11-12 23:38:14 -0800579 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int speed = irda_get_next_speed(skb);
581
582 /*
583 * Does this packet contain a request to change the interface
584 * speed? If so, remember it until we complete the transmission
585 * of this frame.
586 */
587 if (speed != si->speed && speed != -1)
588 si->newspeed = speed;
589
Russell King3d26db12012-01-08 16:16:39 +0000590 /* If this is an empty frame, we can bypass a lot. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (skb->len == 0) {
Russell King0e888ee2012-01-08 16:30:44 +0000592 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000594 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
Russell King3d26db12012-01-08 16:16:39 +0000597 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Russell King3d26db12012-01-08 16:16:39 +0000599 /* We must not already have a skb to transmit... */
600 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Russell King3d26db12012-01-08 16:16:39 +0000602 return si->tx_start(skb, dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605static int
606sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
607{
608 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800609 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 int ret = -EOPNOTSUPP;
611
612 switch (cmd) {
613 case SIOCSBANDWIDTH:
614 if (capable(CAP_NET_ADMIN)) {
615 /*
616 * We are unable to set the speed if the
617 * device is not running.
618 */
619 if (si->open) {
620 ret = sa1100_irda_set_speed(si,
621 rq->ifr_baudrate);
622 } else {
623 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
624 ret = 0;
625 }
626 }
627 break;
628
629 case SIOCSMEDIABUSY:
630 ret = -EPERM;
631 if (capable(CAP_NET_ADMIN)) {
632 irda_device_set_media_busy(dev, TRUE);
633 ret = 0;
634 }
635 break;
636
637 case SIOCGRECEIVING:
638 rq->ifr_receiving = IS_FIR(si) ? 0
639 : si->rx_buff.state != OUTSIDE_FRAME;
640 break;
641
642 default:
643 break;
644 }
645
646 return ret;
647}
648
Russell Kingcbe1d242012-01-08 16:40:07 +0000649static int sa1100_irda_startup(struct sa1100_irda *si)
650{
651 int ret;
652
653 /*
654 * Ensure that the ports for this device are setup correctly.
655 */
656 if (si->pdata->startup) {
657 ret = si->pdata->startup(si->dev);
658 if (ret)
659 return ret;
660 }
661
662 /*
663 * Configure PPC for IRDA - we want to drive TXD2 low.
664 * We also want to drive this pin low during sleep.
665 */
666 PPSR &= ~PPC_TXD2;
667 PSDR &= ~PPC_TXD2;
668 PPDR |= PPC_TXD2;
669
670 /*
671 * Enable HP-SIR modulation, and ensure that the port is disabled.
672 */
673 Ser2UTCR3 = 0;
674 Ser2HSCR0 = HSCR0_UART;
675 Ser2UTCR4 = si->utcr4;
676 Ser2UTCR0 = UTCR0_8BitData;
677 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
678
679 /*
680 * Clear status register
681 */
682 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
683
684 ret = sa1100_irda_set_speed(si, si->speed = 9600);
685 if (ret) {
686 Ser2UTCR3 = 0;
687 Ser2HSCR0 = 0;
688
689 if (si->pdata->shutdown)
690 si->pdata->shutdown(si->dev);
691 }
692
693 return ret;
694}
695
696static void sa1100_irda_shutdown(struct sa1100_irda *si)
697{
698 /*
699 * Stop all DMA activity.
700 */
701 sa1100_stop_dma(si->dma_rx.regs);
702 sa1100_stop_dma(si->dma_tx.regs);
703
704 /* Disable the port. */
705 Ser2UTCR3 = 0;
706 Ser2HSCR0 = 0;
707
708 if (si->pdata->shutdown)
709 si->pdata->shutdown(si->dev);
710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712static int sa1100_irda_start(struct net_device *dev)
713{
Wang Chen4cf16532008-11-12 23:38:14 -0800714 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 int err;
716
717 si->speed = 9600;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
Russell King885767c2012-01-08 12:53:22 +0000720 NULL, NULL, &si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (err)
722 goto err_rx_dma;
723
724 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
Russell King26f2bee2012-01-08 17:48:02 +0000725 sa1100_irda_firtxdma_irq, dev,
726 &si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (err)
728 goto err_tx_dma;
729
Russell King3c500a32012-01-12 13:56:28 +0000730 si->dma_rx.dev = si->dev;
731 si->dma_tx.dev = si->dev;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 * Setup the serial port for the specified speed.
735 */
736 err = sa1100_irda_startup(si);
737 if (err)
738 goto err_startup;
739
740 /*
741 * Open a new IrLAP layer instance.
742 */
743 si->irlap = irlap_open(dev, &si->qos, "sa1100");
744 err = -ENOMEM;
745 if (!si->irlap)
746 goto err_irlap;
747
Russell King374f7732012-01-08 16:26:15 +0000748 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
749 if (err)
750 goto err_irq;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /*
753 * Now enable the interrupt and start the queue
754 */
755 si->open = 1;
756 sa1100_set_power(si, power_level); /* low power mode */
Russell King374f7732012-01-08 16:26:15 +0000757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 netif_start_queue(dev);
759 return 0;
760
Russell King374f7732012-01-08 16:26:15 +0000761err_irq:
762 irlap_close(si->irlap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763err_irlap:
764 si->open = 0;
765 sa1100_irda_shutdown(si);
766err_startup:
Russell King885767c2012-01-08 12:53:22 +0000767 sa1100_free_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768err_tx_dma:
Russell King885767c2012-01-08 12:53:22 +0000769 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770err_rx_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return err;
772}
773
774static int sa1100_irda_stop(struct net_device *dev)
775{
Wang Chen4cf16532008-11-12 23:38:14 -0800776 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000777 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Russell King374f7732012-01-08 16:26:15 +0000779 netif_stop_queue(dev);
780
781 si->open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 sa1100_irda_shutdown(si);
783
784 /*
Russell Kingba845252012-01-08 15:38:15 +0000785 * If we have been doing any DMA activity, make sure we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 * tidy that up cleanly.
787 */
Russell Kingba845252012-01-08 15:38:15 +0000788 skb = si->dma_rx.skb;
789 if (skb) {
Russell King3c500a32012-01-12 13:56:28 +0000790 dma_unmap_sg(si->dma_rx.dev, &si->dma_rx.sg, 1,
Russell King32273f52012-01-12 12:45:00 +0000791 DMA_FROM_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000792 dev_kfree_skb(skb);
Russell King885767c2012-01-08 12:53:22 +0000793 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
Russell Kingba845252012-01-08 15:38:15 +0000796 skb = si->dma_tx.skb;
797 if (skb) {
Russell King3c500a32012-01-12 13:56:28 +0000798 dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1,
Russell King32273f52012-01-12 12:45:00 +0000799 DMA_TO_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000800 dev_kfree_skb(skb);
801 si->dma_tx.skb = NULL;
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /* Stop IrLAP */
805 if (si->irlap) {
806 irlap_close(si->irlap);
807 si->irlap = NULL;
808 }
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 /*
811 * Free resources
812 */
Russell King885767c2012-01-08 12:53:22 +0000813 sa1100_free_dma(si->dma_tx.regs);
814 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 free_irq(dev->irq, dev);
816
817 sa1100_set_power(si, 0);
818
819 return 0;
820}
821
822static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
823{
824 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
825 if (io->head != NULL) {
826 io->truesize = size;
827 io->in_frame = FALSE;
828 io->state = OUTSIDE_FRAME;
829 io->data = io->head;
830 }
831 return io->head ? 0 : -ENOMEM;
832}
833
Alexander Beregalova1de9662009-04-15 12:52:42 +0000834static const struct net_device_ops sa1100_irda_netdev_ops = {
835 .ndo_open = sa1100_irda_start,
836 .ndo_stop = sa1100_irda_stop,
837 .ndo_start_xmit = sa1100_irda_hard_xmit,
838 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000839};
840
Russell King3ae5eae2005-11-09 22:32:44 +0000841static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct net_device *dev;
844 struct sa1100_irda *si;
845 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000846 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (!pdev->dev.platform_data)
849 return -EINVAL;
850
Russell Kinge556fdb2012-01-08 12:02:17 +0000851 irq = platform_get_irq(pdev, 0);
852 if (irq <= 0)
853 return irq < 0 ? irq : -ENXIO;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
856 if (err)
857 goto err_mem_1;
858 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
859 if (err)
860 goto err_mem_2;
861 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
862 if (err)
863 goto err_mem_3;
864
865 dev = alloc_irdadev(sizeof(struct sa1100_irda));
866 if (!dev)
867 goto err_mem_4;
868
Russell Kingd3238602012-01-08 12:07:24 +0000869 SET_NETDEV_DEV(dev, &pdev->dev);
870
Wang Chen4cf16532008-11-12 23:38:14 -0800871 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 si->dev = &pdev->dev;
873 si->pdata = pdev->dev.platform_data;
874
Russell King32273f52012-01-12 12:45:00 +0000875 sg_init_table(&si->dma_rx.sg, 1);
876 sg_init_table(&si->dma_tx.sg, 1);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 /*
879 * Initialise the HP-SIR buffers
880 */
881 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
882 if (err)
883 goto err_mem_5;
Russell King04b7fc42012-01-12 13:51:10 +0000884 err = sa1100_irda_init_iobuf(&si->tx_buff, IRDA_SIR_MAX_FRAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (err)
886 goto err_mem_5;
887
Alexander Beregalova1de9662009-04-15 12:52:42 +0000888 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000889 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 irda_init_max_qos_capabilies(&si->qos);
892
893 /*
894 * We support original IRDA up to 115k2. (we don't currently
895 * support 4Mbps). Min Turn Time set to 1ms or greater.
896 */
897 baudrate_mask = IR_9600;
898
899 switch (max_rate) {
900 case 4000000: baudrate_mask |= IR_4000000 << 8;
901 case 115200: baudrate_mask |= IR_115200;
902 case 57600: baudrate_mask |= IR_57600;
903 case 38400: baudrate_mask |= IR_38400;
904 case 19200: baudrate_mask |= IR_19200;
905 }
906
907 si->qos.baud_rate.bits &= baudrate_mask;
908 si->qos.min_turn_time.bits = 7;
909
910 irda_qos_bits_to_value(&si->qos);
911
912 si->utcr4 = UTCR4_HPSIR;
913 if (tx_lpm)
914 si->utcr4 |= UTCR4_Z1_6us;
915
916 /*
917 * Initially enable HP-SIR modulation, and ensure that the port
918 * is disabled.
919 */
920 Ser2UTCR3 = 0;
921 Ser2UTCR4 = si->utcr4;
922 Ser2HSCR0 = HSCR0_UART;
923
924 err = register_netdev(dev);
925 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000926 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (err) {
929 err_mem_5:
930 kfree(si->tx_buff.head);
931 kfree(si->rx_buff.head);
932 free_netdev(dev);
933 err_mem_4:
934 release_mem_region(__PREG(Ser2HSCR2), 0x04);
935 err_mem_3:
936 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
937 err_mem_2:
938 release_mem_region(__PREG(Ser2UTCR0), 0x24);
939 }
940 err_mem_1:
941 return err;
942}
943
Russell King3ae5eae2005-11-09 22:32:44 +0000944static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Russell King3ae5eae2005-11-09 22:32:44 +0000946 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -0800949 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 unregister_netdev(dev);
951 kfree(si->tx_buff.head);
952 kfree(si->rx_buff.head);
953 free_netdev(dev);
954 }
955
956 release_mem_region(__PREG(Ser2HSCR2), 0x04);
957 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
958 release_mem_region(__PREG(Ser2UTCR0), 0x24);
959
960 return 0;
961}
962
Russell Kingcbe1d242012-01-08 16:40:07 +0000963#ifdef CONFIG_PM
964/*
965 * Suspend the IrDA interface.
966 */
967static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
968{
969 struct net_device *dev = platform_get_drvdata(pdev);
970 struct sa1100_irda *si;
971
972 if (!dev)
973 return 0;
974
975 si = netdev_priv(dev);
976 if (si->open) {
977 /*
978 * Stop the transmit queue
979 */
980 netif_device_detach(dev);
981 disable_irq(dev->irq);
982 sa1100_irda_shutdown(si);
983 __sa1100_irda_set_power(si, 0);
984 }
985
986 return 0;
987}
988
989/*
990 * Resume the IrDA interface.
991 */
992static int sa1100_irda_resume(struct platform_device *pdev)
993{
994 struct net_device *dev = platform_get_drvdata(pdev);
995 struct sa1100_irda *si;
996
997 if (!dev)
998 return 0;
999
1000 si = netdev_priv(dev);
1001 if (si->open) {
1002 /*
1003 * If we missed a speed change, initialise at the new speed
1004 * directly. It is debatable whether this is actually
1005 * required, but in the interests of continuing from where
1006 * we left off it is desirable. The converse argument is
1007 * that we should re-negotiate at 9600 baud again.
1008 */
1009 if (si->newspeed) {
1010 si->speed = si->newspeed;
1011 si->newspeed = 0;
1012 }
1013
1014 sa1100_irda_startup(si);
1015 __sa1100_irda_set_power(si, si->power);
1016 enable_irq(dev->irq);
1017
1018 /*
1019 * This automatically wakes up the queue
1020 */
1021 netif_device_attach(dev);
1022 }
1023
1024 return 0;
1025}
1026#else
1027#define sa1100_irda_suspend NULL
1028#define sa1100_irda_resume NULL
1029#endif
1030
Russell King3ae5eae2005-11-09 22:32:44 +00001031static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 .probe = sa1100_irda_probe,
1033 .remove = sa1100_irda_remove,
1034 .suspend = sa1100_irda_suspend,
1035 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001036 .driver = {
1037 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001038 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001039 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040};
1041
1042static int __init sa1100_irda_init(void)
1043{
1044 /*
1045 * Limit power level a sensible range.
1046 */
1047 if (power_level < 1)
1048 power_level = 1;
1049 if (power_level > 3)
1050 power_level = 3;
1051
Russell King3ae5eae2005-11-09 22:32:44 +00001052 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054
1055static void __exit sa1100_irda_exit(void)
1056{
Russell King3ae5eae2005-11-09 22:32:44 +00001057 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060module_init(sa1100_irda_init);
1061module_exit(sa1100_irda_exit);
1062module_param(power_level, int, 0);
1063module_param(tx_lpm, int, 0);
1064module_param(max_rate, int, 0);
1065
1066MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1067MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1068MODULE_LICENSE("GPL");
1069MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1070MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1071MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001072MODULE_ALIAS("platform:sa11x0-ir");