blob: 32ac4a44ce76bc890ce2dbab559e60175a6cbac5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/net/irda/sa1100_ir.c
3 *
4 * Copyright (C) 2000-2001 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Infra-red driver for the StrongARM SA1100 embedded microprocessor
11 *
12 * Note that we don't have to worry about the SA1111's DMA bugs in here,
13 * so we use the straight forward dma_map_* functions with a null pointer.
14 *
15 * This driver takes one kernel command line parameter, sa1100ir=, with
16 * the following options:
17 * max_rate:baudrate - set the maximum baud rate
Russell King15877e92012-01-08 12:04:05 +000018 * power_level:level - set the transmitter power level
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * tx_lpm:0|1 - set transmit low power mode
20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
22#include <linux/moduleparam.h>
23#include <linux/types.h>
24#include <linux/init.h>
25#include <linux/errno.h>
26#include <linux/netdevice.h>
27#include <linux/slab.h>
28#include <linux/rtnetlink.h>
29#include <linux/interrupt.h>
30#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010031#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/dma-mapping.h>
33
34#include <net/irda/irda.h>
35#include <net/irda/wrapper.h>
36#include <net/irda/irda_device.h>
37
Russell Kingd281bc92008-12-01 23:01:19 +000038#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010039#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/mach/irda.h>
41
42static int power_level = 3;
43static int tx_lpm;
44static int max_rate = 4000000;
45
Russell King885767c2012-01-08 12:53:22 +000046struct sa1100_buf {
47 struct sk_buff *skb;
48 dma_addr_t dma;
49 dma_regs_t *regs;
50};
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052struct sa1100_irda {
53 unsigned char hscr0;
54 unsigned char utcr4;
55 unsigned char power;
56 unsigned char open;
57
58 int speed;
59 int newspeed;
60
Russell King885767c2012-01-08 12:53:22 +000061 struct sa1100_buf dma_rx;
62 struct sa1100_buf dma_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct device *dev;
65 struct irda_platform_data *pdata;
66 struct irlap_cb *irlap;
67 struct qos_info qos;
68
69 iobuff_t tx_buff;
70 iobuff_t rx_buff;
71};
72
73#define IS_FIR(si) ((si)->speed >= 4000000)
74
75#define HPSIR_MAX_RXLEN 2047
76
77/*
78 * Allocate and map the receive buffer, unless it is already allocated.
79 */
80static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
81{
Russell King885767c2012-01-08 12:53:22 +000082 if (si->dma_rx.skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return 0;
84
Russell King885767c2012-01-08 12:53:22 +000085 si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
86 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
88 return -ENOMEM;
89 }
90
91 /*
92 * Align any IP headers that may be contained
93 * within the frame.
94 */
Russell King885767c2012-01-08 12:53:22 +000095 skb_reserve(si->dma_rx.skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Russell King885767c2012-01-08 12:53:22 +000097 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 HPSIR_MAX_RXLEN,
99 DMA_FROM_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000100 if (dma_mapping_error(si->dev, si->dma_rx.dma)) {
101 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000102 return -ENOMEM;
103 }
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106}
107
108/*
109 * We want to get here as soon as possible, and get the receiver setup.
110 * We use the existing buffer.
111 */
112static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
113{
Russell King885767c2012-01-08 12:53:22 +0000114 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
116 return;
117 }
118
119 /*
120 * First empty receive FIFO
121 */
122 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
123
124 /*
125 * Enable the DMA, receiver and receive interrupt.
126 */
Russell King885767c2012-01-08 12:53:22 +0000127 sa1100_clear_dma(si->dma_rx.regs);
128 sa1100_start_dma(si->dma_rx.regs, si->dma_rx.dma, HPSIR_MAX_RXLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
130}
131
132/*
133 * Set the IrDA communications speed.
134 */
135static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
136{
137 unsigned long flags;
138 int brd, ret = -EINVAL;
139
140 switch (speed) {
141 case 9600: case 19200: case 38400:
142 case 57600: case 115200:
143 brd = 3686400 / (16 * speed) - 1;
144
145 /*
146 * Stop the receive DMA.
147 */
148 if (IS_FIR(si))
Russell King885767c2012-01-08 12:53:22 +0000149 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 local_irq_save(flags);
152
153 Ser2UTCR3 = 0;
154 Ser2HSCR0 = HSCR0_UART;
155
156 Ser2UTCR1 = brd >> 8;
157 Ser2UTCR2 = brd;
158
159 /*
160 * Clear status register
161 */
162 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
163 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
164
165 if (si->pdata->set_speed)
166 si->pdata->set_speed(si->dev, speed);
167
168 si->speed = speed;
169
170 local_irq_restore(flags);
171 ret = 0;
172 break;
173
174 case 4000000:
175 local_irq_save(flags);
176
177 si->hscr0 = 0;
178
179 Ser2HSSR0 = 0xff;
180 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
181 Ser2UTCR3 = 0;
182
183 si->speed = speed;
184
185 if (si->pdata->set_speed)
186 si->pdata->set_speed(si->dev, speed);
187
188 sa1100_irda_rx_alloc(si);
189 sa1100_irda_rx_dma_start(si);
190
191 local_irq_restore(flags);
192
193 break;
194
195 default:
196 break;
197 }
198
199 return ret;
200}
201
202/*
203 * Control the power state of the IrDA transmitter.
204 * State:
205 * 0 - off
206 * 1 - short range, lowest power
207 * 2 - medium range, medium power
208 * 3 - maximum range, high power
209 *
210 * Currently, only assabet is known to support this.
211 */
212static int
213__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
214{
215 int ret = 0;
216 if (si->pdata->set_power)
217 ret = si->pdata->set_power(si->dev, state);
218 return ret;
219}
220
221static inline int
222sa1100_set_power(struct sa1100_irda *si, unsigned int state)
223{
224 int ret;
225
226 ret = __sa1100_irda_set_power(si, state);
227 if (ret == 0)
228 si->power = state;
229
230 return ret;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
234 * HP-SIR format interrupt service routines.
235 */
236static void sa1100_irda_hpsir_irq(struct net_device *dev)
237{
Wang Chen4cf16532008-11-12 23:38:14 -0800238 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int status;
240
241 status = Ser2UTSR0;
242
243 /*
244 * Deal with any receive errors first. The bytes in error may be
245 * the only bytes in the receive FIFO, so we do this first.
246 */
247 while (status & UTSR0_EIF) {
248 int stat, data;
249
250 stat = Ser2UTSR1;
251 data = Ser2UTDR;
252
253 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800254 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (stat & UTSR1_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800256 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (stat & UTSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800258 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 } else
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800260 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 status = Ser2UTSR0;
263 }
264
265 /*
266 * We must clear certain bits.
267 */
268 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
269
270 if (status & UTSR0_RFS) {
271 /*
272 * There are at least 4 bytes in the FIFO. Read 3 bytes
273 * and leave the rest to the block below.
274 */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800275 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
276 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
277 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
280 if (status & (UTSR0_RFS | UTSR0_RID)) {
281 /*
282 * Fifo contains more than 1 character.
283 */
284 do {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800285 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 Ser2UTDR);
287 } while (Ser2UTSR1 & UTSR1_RNE);
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 if (status & UTSR0_TFS && si->tx_buff.len) {
292 /*
293 * Transmitter FIFO is not full
294 */
295 do {
296 Ser2UTDR = *si->tx_buff.data++;
297 si->tx_buff.len -= 1;
298 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
299
300 if (si->tx_buff.len == 0) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800301 dev->stats.tx_packets++;
302 dev->stats.tx_bytes += si->tx_buff.data -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 si->tx_buff.head;
304
305 /*
306 * We need to ensure that the transmitter has
307 * finished.
308 */
309 do
310 rmb();
311 while (Ser2UTSR1 & UTSR1_TBY);
312
313 /*
314 * Ok, we've finished transmitting. Now enable
315 * the receiver. Sometimes we get a receive IRQ
316 * immediately after a transmit...
317 */
318 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
319 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
320
321 if (si->newspeed) {
322 sa1100_irda_set_speed(si, si->newspeed);
323 si->newspeed = 0;
324 }
325
326 /* I'm hungry! */
327 netif_wake_queue(dev);
328 }
329 }
330}
331
332static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
333{
Russell King885767c2012-01-08 12:53:22 +0000334 struct sk_buff *skb = si->dma_rx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 dma_addr_t dma_addr;
336 unsigned int len, stat, data;
337
338 if (!skb) {
339 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
340 return;
341 }
342
343 /*
344 * Get the current data position.
345 */
Russell King885767c2012-01-08 12:53:22 +0000346 dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
347 len = dma_addr - si->dma_rx.dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 if (len > HPSIR_MAX_RXLEN)
349 len = HPSIR_MAX_RXLEN;
Russell King885767c2012-01-08 12:53:22 +0000350 dma_unmap_single(si->dev, si->dma_rx.dma, len, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 do {
353 /*
354 * Read Status, and then Data.
355 */
356 stat = Ser2HSSR1;
357 rmb();
358 data = Ser2HSDR;
359
360 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800361 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (stat & HSSR1_CRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800363 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (stat & HSSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800365 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 } else
367 skb->data[len++] = data;
368
369 /*
370 * If we hit the end of frame, there's
371 * no point in continuing.
372 */
373 if (stat & HSSR1_EOF)
374 break;
375 } while (Ser2HSSR0 & HSSR0_EIF);
376
377 if (stat & HSSR1_EOF) {
Russell King885767c2012-01-08 12:53:22 +0000378 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 skb_put(skb, len);
381 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700382 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 skb->protocol = htons(ETH_P_IRDA);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800384 dev->stats.rx_packets++;
385 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 /*
388 * Before we pass the buffer up, allocate a new one.
389 */
390 sa1100_irda_rx_alloc(si);
391
392 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 } else {
394 /*
Russell King22f0bf92012-01-08 13:55:23 +0000395 * Remap the buffer - it was previously mapped, and we
396 * hope that this succeeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Russell King885767c2012-01-08 12:53:22 +0000398 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 HPSIR_MAX_RXLEN,
400 DMA_FROM_DEVICE);
401 }
402}
403
404/*
405 * FIR format interrupt service routine. We only have to
406 * handle RX events; transmit events go via the TX DMA handler.
407 *
408 * No matter what, we disable RX, process, and the restart RX.
409 */
410static void sa1100_irda_fir_irq(struct net_device *dev)
411{
Wang Chen4cf16532008-11-12 23:38:14 -0800412 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /*
415 * Stop RX DMA
416 */
Russell King885767c2012-01-08 12:53:22 +0000417 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 /*
420 * Framing error - we throw away the packet completely.
421 * Clearing RXE flushes the error conditions and data
422 * from the fifo.
423 */
424 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800425 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 if (Ser2HSSR0 & HSSR0_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800428 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /*
431 * Clear out the DMA...
432 */
433 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
434
435 /*
436 * Clear selected status bits now, so we
437 * don't miss them next time around.
438 */
439 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
440 }
441
442 /*
443 * Deal with any receive errors. The any of the lowest
444 * 8 bytes in the FIFO may contain an error. We must read
445 * them one by one. The "error" could even be the end of
446 * packet!
447 */
448 if (Ser2HSSR0 & HSSR0_EIF)
449 sa1100_irda_fir_error(si, dev);
450
451 /*
452 * No matter what happens, we must restart reception.
453 */
454 sa1100_irda_rx_dma_start(si);
455}
456
David Howells7d12e782006-10-05 14:55:46 +0100457static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800460 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 sa1100_irda_fir_irq(dev);
462 else
463 sa1100_irda_hpsir_irq(dev);
464 return IRQ_HANDLED;
465}
466
467/*
468 * TX DMA completion handler.
469 */
470static void sa1100_irda_txdma_irq(void *id)
471{
472 struct net_device *dev = id;
Wang Chen4cf16532008-11-12 23:38:14 -0800473 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000474 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 /*
477 * Wait for the transmission to complete. Unfortunately,
478 * the hardware doesn't give us an interrupt to indicate
479 * "end of frame".
480 */
481 do
482 rmb();
483 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
484
485 /*
486 * Clear the transmit underrun bit.
487 */
488 Ser2HSSR0 = HSSR0_TUR;
489
490 /*
491 * Do we need to change speed? Note that we're lazy
Russell King885767c2012-01-08 12:53:22 +0000492 * here - we don't free the old dma_rx.skb. We don't need
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * to allocate a buffer either.
494 */
495 if (si->newspeed) {
496 sa1100_irda_set_speed(si, si->newspeed);
497 si->newspeed = 0;
498 }
499
500 /*
501 * Start reception. This disables the transmitter for
502 * us. This will be using the existing RX buffer.
503 */
504 sa1100_irda_rx_dma_start(si);
505
Russell Kingba845252012-01-08 15:38:15 +0000506 /* Account and free the packet. */
507 skb = si->dma_tx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (skb) {
Russell Kingba845252012-01-08 15:38:15 +0000509 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
510 DMA_TO_DEVICE);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800511 dev->stats.tx_packets ++;
512 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 dev_kfree_skb_irq(skb);
Russell Kingba845252012-01-08 15:38:15 +0000514 si->dma_tx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
517 /*
518 * Make sure that the TX queue is available for sending
519 * (for retries). TX has priority over RX at all times.
520 */
521 netif_wake_queue(dev);
522}
523
524static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
525{
Wang Chen4cf16532008-11-12 23:38:14 -0800526 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int speed = irda_get_next_speed(skb);
528
529 /*
530 * Does this packet contain a request to change the interface
531 * speed? If so, remember it until we complete the transmission
532 * of this frame.
533 */
534 if (speed != si->speed && speed != -1)
535 si->newspeed = speed;
536
537 /*
538 * If this is an empty frame, we can bypass a lot.
539 */
540 if (skb->len == 0) {
541 if (si->newspeed) {
542 si->newspeed = 0;
543 sa1100_irda_set_speed(si, speed);
544 }
545 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000546 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 if (!IS_FIR(si)) {
550 netif_stop_queue(dev);
551
552 si->tx_buff.data = si->tx_buff.head;
553 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
554 si->tx_buff.truesize);
555
556 /*
557 * Set the transmit interrupt enable. This will fire
558 * off an interrupt immediately. Note that we disable
559 * the receiver so we won't get spurious characteres
560 * received.
561 */
562 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
563
564 dev_kfree_skb(skb);
565 } else {
566 int mtt = irda_get_mtt(skb);
567
568 /*
569 * We must not be transmitting...
570 */
Russell King885767c2012-01-08 12:53:22 +0000571 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 netif_stop_queue(dev);
574
Russell King885767c2012-01-08 12:53:22 +0000575 si->dma_tx.skb = skb;
576 si->dma_tx.dma = dma_map_single(si->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 skb->len, DMA_TO_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000578 if (dma_mapping_error(si->dev, si->dma_tx.dma)) {
579 si->dma_tx.skb = NULL;
Russell King22f0bf92012-01-08 13:55:23 +0000580 netif_wake_queue(dev);
581 dev->stats.tx_dropped++;
582 dev_kfree_skb(skb);
583 return NETDEV_TX_OK;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Russell King885767c2012-01-08 12:53:22 +0000586 sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /*
589 * If we have a mean turn-around time, impose the specified
590 * specified delay. We could shorten this by timing from
591 * the point we received the packet.
592 */
593 if (mtt)
594 udelay(mtt);
595
596 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
597 }
598
Patrick McHardy6ed10652009-06-23 06:03:08 +0000599 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602static int
603sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
604{
605 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800606 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int ret = -EOPNOTSUPP;
608
609 switch (cmd) {
610 case SIOCSBANDWIDTH:
611 if (capable(CAP_NET_ADMIN)) {
612 /*
613 * We are unable to set the speed if the
614 * device is not running.
615 */
616 if (si->open) {
617 ret = sa1100_irda_set_speed(si,
618 rq->ifr_baudrate);
619 } else {
620 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
621 ret = 0;
622 }
623 }
624 break;
625
626 case SIOCSMEDIABUSY:
627 ret = -EPERM;
628 if (capable(CAP_NET_ADMIN)) {
629 irda_device_set_media_busy(dev, TRUE);
630 ret = 0;
631 }
632 break;
633
634 case SIOCGRECEIVING:
635 rq->ifr_receiving = IS_FIR(si) ? 0
636 : si->rx_buff.state != OUTSIDE_FRAME;
637 break;
638
639 default:
640 break;
641 }
642
643 return ret;
644}
645
Russell Kingcbe1d242012-01-08 16:40:07 +0000646static int sa1100_irda_startup(struct sa1100_irda *si)
647{
648 int ret;
649
650 /*
651 * Ensure that the ports for this device are setup correctly.
652 */
653 if (si->pdata->startup) {
654 ret = si->pdata->startup(si->dev);
655 if (ret)
656 return ret;
657 }
658
659 /*
660 * Configure PPC for IRDA - we want to drive TXD2 low.
661 * We also want to drive this pin low during sleep.
662 */
663 PPSR &= ~PPC_TXD2;
664 PSDR &= ~PPC_TXD2;
665 PPDR |= PPC_TXD2;
666
667 /*
668 * Enable HP-SIR modulation, and ensure that the port is disabled.
669 */
670 Ser2UTCR3 = 0;
671 Ser2HSCR0 = HSCR0_UART;
672 Ser2UTCR4 = si->utcr4;
673 Ser2UTCR0 = UTCR0_8BitData;
674 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
675
676 /*
677 * Clear status register
678 */
679 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
680
681 ret = sa1100_irda_set_speed(si, si->speed = 9600);
682 if (ret) {
683 Ser2UTCR3 = 0;
684 Ser2HSCR0 = 0;
685
686 if (si->pdata->shutdown)
687 si->pdata->shutdown(si->dev);
688 }
689
690 return ret;
691}
692
693static void sa1100_irda_shutdown(struct sa1100_irda *si)
694{
695 /*
696 * Stop all DMA activity.
697 */
698 sa1100_stop_dma(si->dma_rx.regs);
699 sa1100_stop_dma(si->dma_tx.regs);
700
701 /* Disable the port. */
702 Ser2UTCR3 = 0;
703 Ser2HSCR0 = 0;
704
705 if (si->pdata->shutdown)
706 si->pdata->shutdown(si->dev);
707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709static int sa1100_irda_start(struct net_device *dev)
710{
Wang Chen4cf16532008-11-12 23:38:14 -0800711 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int err;
713
714 si->speed = 9600;
715
716 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
717 if (err)
718 goto err_irq;
719
720 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
Russell King885767c2012-01-08 12:53:22 +0000721 NULL, NULL, &si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 if (err)
723 goto err_rx_dma;
724
725 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
Russell King885767c2012-01-08 12:53:22 +0000726 sa1100_irda_txdma_irq, dev, &si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (err)
728 goto err_tx_dma;
729
730 /*
731 * The interrupt must remain disabled for now.
732 */
733 disable_irq(dev->irq);
734
735 /*
736 * Setup the serial port for the specified speed.
737 */
738 err = sa1100_irda_startup(si);
739 if (err)
740 goto err_startup;
741
742 /*
743 * Open a new IrLAP layer instance.
744 */
745 si->irlap = irlap_open(dev, &si->qos, "sa1100");
746 err = -ENOMEM;
747 if (!si->irlap)
748 goto err_irlap;
749
750 /*
751 * Now enable the interrupt and start the queue
752 */
753 si->open = 1;
754 sa1100_set_power(si, power_level); /* low power mode */
755 enable_irq(dev->irq);
756 netif_start_queue(dev);
757 return 0;
758
759err_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:
767 free_irq(dev->irq, dev);
768err_irq:
769 return err;
770}
771
772static int sa1100_irda_stop(struct net_device *dev)
773{
Wang Chen4cf16532008-11-12 23:38:14 -0800774 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000775 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 disable_irq(dev->irq);
778 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 King885767c2012-01-08 12:53:22 +0000786 dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 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) {
794 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
795 DMA_TO_DEVICE);
796 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
806 netif_stop_queue(dev);
807 si->open = 0;
808
809 /*
810 * Free resources
811 */
Russell King885767c2012-01-08 12:53:22 +0000812 sa1100_free_dma(si->dma_tx.regs);
813 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 free_irq(dev->irq, dev);
815
816 sa1100_set_power(si, 0);
817
818 return 0;
819}
820
821static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
822{
823 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
824 if (io->head != NULL) {
825 io->truesize = size;
826 io->in_frame = FALSE;
827 io->state = OUTSIDE_FRAME;
828 io->data = io->head;
829 }
830 return io->head ? 0 : -ENOMEM;
831}
832
Alexander Beregalova1de9662009-04-15 12:52:42 +0000833static const struct net_device_ops sa1100_irda_netdev_ops = {
834 .ndo_open = sa1100_irda_start,
835 .ndo_stop = sa1100_irda_stop,
836 .ndo_start_xmit = sa1100_irda_hard_xmit,
837 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000838};
839
Russell King3ae5eae2005-11-09 22:32:44 +0000840static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct net_device *dev;
843 struct sa1100_irda *si;
844 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000845 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 if (!pdev->dev.platform_data)
848 return -EINVAL;
849
Russell Kinge556fdb2012-01-08 12:02:17 +0000850 irq = platform_get_irq(pdev, 0);
851 if (irq <= 0)
852 return irq < 0 ? irq : -ENXIO;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
855 if (err)
856 goto err_mem_1;
857 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
858 if (err)
859 goto err_mem_2;
860 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
861 if (err)
862 goto err_mem_3;
863
864 dev = alloc_irdadev(sizeof(struct sa1100_irda));
865 if (!dev)
866 goto err_mem_4;
867
Russell Kingd3238602012-01-08 12:07:24 +0000868 SET_NETDEV_DEV(dev, &pdev->dev);
869
Wang Chen4cf16532008-11-12 23:38:14 -0800870 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 si->dev = &pdev->dev;
872 si->pdata = pdev->dev.platform_data;
873
874 /*
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");