blob: 8b8cf4152604951d8a157963f7b742d4178be30a [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 *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
Russell King0e888ee2012-01-08 16:30:44 +000075static int sa1100_irda_set_speed(struct sa1100_irda *, int);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define IS_FIR(si) ((si)->speed >= 4000000)
78
79#define HPSIR_MAX_RXLEN 2047
80
81/*
82 * Allocate and map the receive buffer, unless it is already allocated.
83 */
84static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
85{
Russell King885767c2012-01-08 12:53:22 +000086 if (si->dma_rx.skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
88
Russell King885767c2012-01-08 12:53:22 +000089 si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
90 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
92 return -ENOMEM;
93 }
94
95 /*
96 * Align any IP headers that may be contained
97 * within the frame.
98 */
Russell King885767c2012-01-08 12:53:22 +000099 skb_reserve(si->dma_rx.skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Russell King885767c2012-01-08 12:53:22 +0000101 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 HPSIR_MAX_RXLEN,
103 DMA_FROM_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000104 if (dma_mapping_error(si->dev, si->dma_rx.dma)) {
105 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000106 return -ENOMEM;
107 }
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return 0;
110}
111
112/*
113 * We want to get here as soon as possible, and get the receiver setup.
114 * We use the existing buffer.
115 */
116static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
117{
Russell King885767c2012-01-08 12:53:22 +0000118 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
120 return;
121 }
122
123 /*
124 * First empty receive FIFO
125 */
126 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
127
128 /*
129 * Enable the DMA, receiver and receive interrupt.
130 */
Russell King885767c2012-01-08 12:53:22 +0000131 sa1100_clear_dma(si->dma_rx.regs);
132 sa1100_start_dma(si->dma_rx.regs, si->dma_rx.dma, HPSIR_MAX_RXLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
134}
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
166/*
167 * FIR format support.
168 */
169static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
170 struct sa1100_irda *si)
171{
172 int mtt = irda_get_mtt(skb);
173
174 si->dma_tx.skb = skb;
175 si->dma_tx.dma = dma_map_single(si->dev, skb->data, skb->len,
176 DMA_TO_DEVICE);
177 if (dma_mapping_error(si->dev, si->dma_tx.dma)) {
178 si->dma_tx.skb = NULL;
179 netif_wake_queue(dev);
180 dev->stats.tx_dropped++;
181 dev_kfree_skb(skb);
182 return NETDEV_TX_OK;
183 }
184
185 sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len);
186
187 /*
188 * If we have a mean turn-around time, impose the specified
189 * specified delay. We could shorten this by timing from
190 * the point we received the packet.
191 */
192 if (mtt)
193 udelay(mtt);
194
195 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
196
197 return NETDEV_TX_OK;
198}
199
200
201/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * Set the IrDA communications speed.
203 */
204static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
205{
206 unsigned long flags;
207 int brd, ret = -EINVAL;
208
209 switch (speed) {
210 case 9600: case 19200: case 38400:
211 case 57600: case 115200:
212 brd = 3686400 / (16 * speed) - 1;
213
214 /*
215 * Stop the receive DMA.
216 */
217 if (IS_FIR(si))
Russell King885767c2012-01-08 12:53:22 +0000218 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 local_irq_save(flags);
221
222 Ser2UTCR3 = 0;
223 Ser2HSCR0 = HSCR0_UART;
224
225 Ser2UTCR1 = brd >> 8;
226 Ser2UTCR2 = brd;
227
228 /*
229 * Clear status register
230 */
231 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
232 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
233
234 if (si->pdata->set_speed)
235 si->pdata->set_speed(si->dev, speed);
236
237 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000238 si->tx_start = sa1100_irda_sir_tx_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 local_irq_restore(flags);
241 ret = 0;
242 break;
243
244 case 4000000:
245 local_irq_save(flags);
246
247 si->hscr0 = 0;
248
249 Ser2HSSR0 = 0xff;
250 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
251 Ser2UTCR3 = 0;
252
253 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000254 si->tx_start = sa1100_irda_fir_tx_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (si->pdata->set_speed)
257 si->pdata->set_speed(si->dev, speed);
258
259 sa1100_irda_rx_alloc(si);
260 sa1100_irda_rx_dma_start(si);
261
262 local_irq_restore(flags);
263
264 break;
265
266 default:
267 break;
268 }
269
270 return ret;
271}
272
273/*
274 * Control the power state of the IrDA transmitter.
275 * State:
276 * 0 - off
277 * 1 - short range, lowest power
278 * 2 - medium range, medium power
279 * 3 - maximum range, high power
280 *
281 * Currently, only assabet is known to support this.
282 */
283static int
284__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
285{
286 int ret = 0;
287 if (si->pdata->set_power)
288 ret = si->pdata->set_power(si->dev, state);
289 return ret;
290}
291
292static inline int
293sa1100_set_power(struct sa1100_irda *si, unsigned int state)
294{
295 int ret;
296
297 ret = __sa1100_irda_set_power(si, state);
298 if (ret == 0)
299 si->power = state;
300
301 return ret;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/*
305 * HP-SIR format interrupt service routines.
306 */
307static void sa1100_irda_hpsir_irq(struct net_device *dev)
308{
Wang Chen4cf16532008-11-12 23:38:14 -0800309 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 int status;
311
312 status = Ser2UTSR0;
313
314 /*
315 * Deal with any receive errors first. The bytes in error may be
316 * the only bytes in the receive FIFO, so we do this first.
317 */
318 while (status & UTSR0_EIF) {
319 int stat, data;
320
321 stat = Ser2UTSR1;
322 data = Ser2UTDR;
323
324 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800325 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (stat & UTSR1_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800327 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (stat & UTSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800329 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 } else
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800331 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 status = Ser2UTSR0;
334 }
335
336 /*
337 * We must clear certain bits.
338 */
339 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
340
341 if (status & UTSR0_RFS) {
342 /*
343 * There are at least 4 bytes in the FIFO. Read 3 bytes
344 * and leave the rest to the block below.
345 */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800346 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
347 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
348 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 if (status & (UTSR0_RFS | UTSR0_RID)) {
352 /*
353 * Fifo contains more than 1 character.
354 */
355 do {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800356 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 Ser2UTDR);
358 } while (Ser2UTSR1 & UTSR1_RNE);
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361
362 if (status & UTSR0_TFS && si->tx_buff.len) {
363 /*
364 * Transmitter FIFO is not full
365 */
366 do {
367 Ser2UTDR = *si->tx_buff.data++;
368 si->tx_buff.len -= 1;
369 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
370
371 if (si->tx_buff.len == 0) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800372 dev->stats.tx_packets++;
373 dev->stats.tx_bytes += si->tx_buff.data -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 si->tx_buff.head;
375
376 /*
377 * We need to ensure that the transmitter has
378 * finished.
379 */
380 do
381 rmb();
382 while (Ser2UTSR1 & UTSR1_TBY);
383
384 /*
385 * Ok, we've finished transmitting. Now enable
386 * the receiver. Sometimes we get a receive IRQ
387 * immediately after a transmit...
388 */
389 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
390 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
391
Russell King0e888ee2012-01-08 16:30:44 +0000392 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /* I'm hungry! */
395 netif_wake_queue(dev);
396 }
397 }
398}
399
400static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
401{
Russell King885767c2012-01-08 12:53:22 +0000402 struct sk_buff *skb = si->dma_rx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 dma_addr_t dma_addr;
404 unsigned int len, stat, data;
405
406 if (!skb) {
407 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
408 return;
409 }
410
411 /*
412 * Get the current data position.
413 */
Russell King885767c2012-01-08 12:53:22 +0000414 dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
415 len = dma_addr - si->dma_rx.dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (len > HPSIR_MAX_RXLEN)
417 len = HPSIR_MAX_RXLEN;
Russell King885767c2012-01-08 12:53:22 +0000418 dma_unmap_single(si->dev, si->dma_rx.dma, len, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 do {
421 /*
422 * Read Status, and then Data.
423 */
424 stat = Ser2HSSR1;
425 rmb();
426 data = Ser2HSDR;
427
428 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800429 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (stat & HSSR1_CRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800431 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (stat & HSSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800433 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 } else
435 skb->data[len++] = data;
436
437 /*
438 * If we hit the end of frame, there's
439 * no point in continuing.
440 */
441 if (stat & HSSR1_EOF)
442 break;
443 } while (Ser2HSSR0 & HSSR0_EIF);
444
445 if (stat & HSSR1_EOF) {
Russell King885767c2012-01-08 12:53:22 +0000446 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 skb_put(skb, len);
449 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700450 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 skb->protocol = htons(ETH_P_IRDA);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800452 dev->stats.rx_packets++;
453 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /*
456 * Before we pass the buffer up, allocate a new one.
457 */
458 sa1100_irda_rx_alloc(si);
459
460 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 } else {
462 /*
Russell King22f0bf92012-01-08 13:55:23 +0000463 * Remap the buffer - it was previously mapped, and we
464 * hope that this succeeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 */
Russell King885767c2012-01-08 12:53:22 +0000466 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 HPSIR_MAX_RXLEN,
468 DMA_FROM_DEVICE);
469 }
470}
471
472/*
473 * FIR format interrupt service routine. We only have to
474 * handle RX events; transmit events go via the TX DMA handler.
475 *
476 * No matter what, we disable RX, process, and the restart RX.
477 */
478static void sa1100_irda_fir_irq(struct net_device *dev)
479{
Wang Chen4cf16532008-11-12 23:38:14 -0800480 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /*
483 * Stop RX DMA
484 */
Russell King885767c2012-01-08 12:53:22 +0000485 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /*
488 * Framing error - we throw away the packet completely.
489 * Clearing RXE flushes the error conditions and data
490 * from the fifo.
491 */
492 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800493 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (Ser2HSSR0 & HSSR0_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800496 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /*
499 * Clear out the DMA...
500 */
501 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
502
503 /*
504 * Clear selected status bits now, so we
505 * don't miss them next time around.
506 */
507 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
508 }
509
510 /*
511 * Deal with any receive errors. The any of the lowest
512 * 8 bytes in the FIFO may contain an error. We must read
513 * them one by one. The "error" could even be the end of
514 * packet!
515 */
516 if (Ser2HSSR0 & HSSR0_EIF)
517 sa1100_irda_fir_error(si, dev);
518
519 /*
520 * No matter what happens, we must restart reception.
521 */
522 sa1100_irda_rx_dma_start(si);
523}
524
David Howells7d12e782006-10-05 14:55:46 +0100525static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800528 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 sa1100_irda_fir_irq(dev);
530 else
531 sa1100_irda_hpsir_irq(dev);
532 return IRQ_HANDLED;
533}
534
535/*
536 * TX DMA completion handler.
537 */
538static void sa1100_irda_txdma_irq(void *id)
539{
540 struct net_device *dev = id;
Wang Chen4cf16532008-11-12 23:38:14 -0800541 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000542 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /*
545 * Wait for the transmission to complete. Unfortunately,
546 * the hardware doesn't give us an interrupt to indicate
547 * "end of frame".
548 */
549 do
550 rmb();
551 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
552
553 /*
554 * Clear the transmit underrun bit.
555 */
556 Ser2HSSR0 = HSSR0_TUR;
557
558 /*
559 * Do we need to change speed? Note that we're lazy
Russell King885767c2012-01-08 12:53:22 +0000560 * here - we don't free the old dma_rx.skb. We don't need
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 * to allocate a buffer either.
562 */
Russell King0e888ee2012-01-08 16:30:44 +0000563 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /*
566 * Start reception. This disables the transmitter for
567 * us. This will be using the existing RX buffer.
568 */
569 sa1100_irda_rx_dma_start(si);
570
Russell Kingba845252012-01-08 15:38:15 +0000571 /* Account and free the packet. */
572 skb = si->dma_tx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (skb) {
Russell Kingba845252012-01-08 15:38:15 +0000574 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
575 DMA_TO_DEVICE);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800576 dev->stats.tx_packets ++;
577 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 dev_kfree_skb_irq(skb);
Russell Kingba845252012-01-08 15:38:15 +0000579 si->dma_tx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 /*
583 * Make sure that the TX queue is available for sending
584 * (for retries). TX has priority over RX at all times.
585 */
586 netif_wake_queue(dev);
587}
588
589static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
590{
Wang Chen4cf16532008-11-12 23:38:14 -0800591 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 int speed = irda_get_next_speed(skb);
593
594 /*
595 * Does this packet contain a request to change the interface
596 * speed? If so, remember it until we complete the transmission
597 * of this frame.
598 */
599 if (speed != si->speed && speed != -1)
600 si->newspeed = speed;
601
Russell King3d26db12012-01-08 16:16:39 +0000602 /* If this is an empty frame, we can bypass a lot. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (skb->len == 0) {
Russell King0e888ee2012-01-08 16:30:44 +0000604 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000606 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
Russell King3d26db12012-01-08 16:16:39 +0000609 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Russell King3d26db12012-01-08 16:16:39 +0000611 /* We must not already have a skb to transmit... */
612 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Russell King3d26db12012-01-08 16:16:39 +0000614 return si->tx_start(skb, dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617static int
618sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
619{
620 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800621 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 int ret = -EOPNOTSUPP;
623
624 switch (cmd) {
625 case SIOCSBANDWIDTH:
626 if (capable(CAP_NET_ADMIN)) {
627 /*
628 * We are unable to set the speed if the
629 * device is not running.
630 */
631 if (si->open) {
632 ret = sa1100_irda_set_speed(si,
633 rq->ifr_baudrate);
634 } else {
635 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
636 ret = 0;
637 }
638 }
639 break;
640
641 case SIOCSMEDIABUSY:
642 ret = -EPERM;
643 if (capable(CAP_NET_ADMIN)) {
644 irda_device_set_media_busy(dev, TRUE);
645 ret = 0;
646 }
647 break;
648
649 case SIOCGRECEIVING:
650 rq->ifr_receiving = IS_FIR(si) ? 0
651 : si->rx_buff.state != OUTSIDE_FRAME;
652 break;
653
654 default:
655 break;
656 }
657
658 return ret;
659}
660
Russell Kingcbe1d242012-01-08 16:40:07 +0000661static int sa1100_irda_startup(struct sa1100_irda *si)
662{
663 int ret;
664
665 /*
666 * Ensure that the ports for this device are setup correctly.
667 */
668 if (si->pdata->startup) {
669 ret = si->pdata->startup(si->dev);
670 if (ret)
671 return ret;
672 }
673
674 /*
675 * Configure PPC for IRDA - we want to drive TXD2 low.
676 * We also want to drive this pin low during sleep.
677 */
678 PPSR &= ~PPC_TXD2;
679 PSDR &= ~PPC_TXD2;
680 PPDR |= PPC_TXD2;
681
682 /*
683 * Enable HP-SIR modulation, and ensure that the port is disabled.
684 */
685 Ser2UTCR3 = 0;
686 Ser2HSCR0 = HSCR0_UART;
687 Ser2UTCR4 = si->utcr4;
688 Ser2UTCR0 = UTCR0_8BitData;
689 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
690
691 /*
692 * Clear status register
693 */
694 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
695
696 ret = sa1100_irda_set_speed(si, si->speed = 9600);
697 if (ret) {
698 Ser2UTCR3 = 0;
699 Ser2HSCR0 = 0;
700
701 if (si->pdata->shutdown)
702 si->pdata->shutdown(si->dev);
703 }
704
705 return ret;
706}
707
708static void sa1100_irda_shutdown(struct sa1100_irda *si)
709{
710 /*
711 * Stop all DMA activity.
712 */
713 sa1100_stop_dma(si->dma_rx.regs);
714 sa1100_stop_dma(si->dma_tx.regs);
715
716 /* Disable the port. */
717 Ser2UTCR3 = 0;
718 Ser2HSCR0 = 0;
719
720 if (si->pdata->shutdown)
721 si->pdata->shutdown(si->dev);
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static int sa1100_irda_start(struct net_device *dev)
725{
Wang Chen4cf16532008-11-12 23:38:14 -0800726 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int err;
728
729 si->speed = 9600;
730
731 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
732 if (err)
733 goto err_irq;
734
735 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
Russell King885767c2012-01-08 12:53:22 +0000736 NULL, NULL, &si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (err)
738 goto err_rx_dma;
739
740 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
Russell King885767c2012-01-08 12:53:22 +0000741 sa1100_irda_txdma_irq, dev, &si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (err)
743 goto err_tx_dma;
744
745 /*
746 * The interrupt must remain disabled for now.
747 */
748 disable_irq(dev->irq);
749
750 /*
751 * Setup the serial port for the specified speed.
752 */
753 err = sa1100_irda_startup(si);
754 if (err)
755 goto err_startup;
756
757 /*
758 * Open a new IrLAP layer instance.
759 */
760 si->irlap = irlap_open(dev, &si->qos, "sa1100");
761 err = -ENOMEM;
762 if (!si->irlap)
763 goto err_irlap;
764
765 /*
766 * Now enable the interrupt and start the queue
767 */
768 si->open = 1;
769 sa1100_set_power(si, power_level); /* low power mode */
770 enable_irq(dev->irq);
771 netif_start_queue(dev);
772 return 0;
773
774err_irlap:
775 si->open = 0;
776 sa1100_irda_shutdown(si);
777err_startup:
Russell King885767c2012-01-08 12:53:22 +0000778 sa1100_free_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779err_tx_dma:
Russell King885767c2012-01-08 12:53:22 +0000780 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781err_rx_dma:
782 free_irq(dev->irq, dev);
783err_irq:
784 return err;
785}
786
787static int sa1100_irda_stop(struct net_device *dev)
788{
Wang Chen4cf16532008-11-12 23:38:14 -0800789 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000790 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 disable_irq(dev->irq);
793 sa1100_irda_shutdown(si);
794
795 /*
Russell Kingba845252012-01-08 15:38:15 +0000796 * If we have been doing any DMA activity, make sure we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 * tidy that up cleanly.
798 */
Russell Kingba845252012-01-08 15:38:15 +0000799 skb = si->dma_rx.skb;
800 if (skb) {
Russell King885767c2012-01-08 12:53:22 +0000801 dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 DMA_FROM_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000803 dev_kfree_skb(skb);
Russell King885767c2012-01-08 12:53:22 +0000804 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
Russell Kingba845252012-01-08 15:38:15 +0000807 skb = si->dma_tx.skb;
808 if (skb) {
809 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
810 DMA_TO_DEVICE);
811 dev_kfree_skb(skb);
812 si->dma_tx.skb = NULL;
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* Stop IrLAP */
816 if (si->irlap) {
817 irlap_close(si->irlap);
818 si->irlap = NULL;
819 }
820
821 netif_stop_queue(dev);
822 si->open = 0;
823
824 /*
825 * Free resources
826 */
Russell King885767c2012-01-08 12:53:22 +0000827 sa1100_free_dma(si->dma_tx.regs);
828 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 free_irq(dev->irq, dev);
830
831 sa1100_set_power(si, 0);
832
833 return 0;
834}
835
836static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
837{
838 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
839 if (io->head != NULL) {
840 io->truesize = size;
841 io->in_frame = FALSE;
842 io->state = OUTSIDE_FRAME;
843 io->data = io->head;
844 }
845 return io->head ? 0 : -ENOMEM;
846}
847
Alexander Beregalova1de9662009-04-15 12:52:42 +0000848static const struct net_device_ops sa1100_irda_netdev_ops = {
849 .ndo_open = sa1100_irda_start,
850 .ndo_stop = sa1100_irda_stop,
851 .ndo_start_xmit = sa1100_irda_hard_xmit,
852 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000853};
854
Russell King3ae5eae2005-11-09 22:32:44 +0000855static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 struct net_device *dev;
858 struct sa1100_irda *si;
859 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000860 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 if (!pdev->dev.platform_data)
863 return -EINVAL;
864
Russell Kinge556fdb2012-01-08 12:02:17 +0000865 irq = platform_get_irq(pdev, 0);
866 if (irq <= 0)
867 return irq < 0 ? irq : -ENXIO;
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
870 if (err)
871 goto err_mem_1;
872 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
873 if (err)
874 goto err_mem_2;
875 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
876 if (err)
877 goto err_mem_3;
878
879 dev = alloc_irdadev(sizeof(struct sa1100_irda));
880 if (!dev)
881 goto err_mem_4;
882
Russell Kingd3238602012-01-08 12:07:24 +0000883 SET_NETDEV_DEV(dev, &pdev->dev);
884
Wang Chen4cf16532008-11-12 23:38:14 -0800885 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 si->dev = &pdev->dev;
887 si->pdata = pdev->dev.platform_data;
888
889 /*
890 * Initialise the HP-SIR buffers
891 */
892 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
893 if (err)
894 goto err_mem_5;
895 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
896 if (err)
897 goto err_mem_5;
898
Alexander Beregalova1de9662009-04-15 12:52:42 +0000899 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000900 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 irda_init_max_qos_capabilies(&si->qos);
903
904 /*
905 * We support original IRDA up to 115k2. (we don't currently
906 * support 4Mbps). Min Turn Time set to 1ms or greater.
907 */
908 baudrate_mask = IR_9600;
909
910 switch (max_rate) {
911 case 4000000: baudrate_mask |= IR_4000000 << 8;
912 case 115200: baudrate_mask |= IR_115200;
913 case 57600: baudrate_mask |= IR_57600;
914 case 38400: baudrate_mask |= IR_38400;
915 case 19200: baudrate_mask |= IR_19200;
916 }
917
918 si->qos.baud_rate.bits &= baudrate_mask;
919 si->qos.min_turn_time.bits = 7;
920
921 irda_qos_bits_to_value(&si->qos);
922
923 si->utcr4 = UTCR4_HPSIR;
924 if (tx_lpm)
925 si->utcr4 |= UTCR4_Z1_6us;
926
927 /*
928 * Initially enable HP-SIR modulation, and ensure that the port
929 * is disabled.
930 */
931 Ser2UTCR3 = 0;
932 Ser2UTCR4 = si->utcr4;
933 Ser2HSCR0 = HSCR0_UART;
934
935 err = register_netdev(dev);
936 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000937 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 if (err) {
940 err_mem_5:
941 kfree(si->tx_buff.head);
942 kfree(si->rx_buff.head);
943 free_netdev(dev);
944 err_mem_4:
945 release_mem_region(__PREG(Ser2HSCR2), 0x04);
946 err_mem_3:
947 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
948 err_mem_2:
949 release_mem_region(__PREG(Ser2UTCR0), 0x24);
950 }
951 err_mem_1:
952 return err;
953}
954
Russell King3ae5eae2005-11-09 22:32:44 +0000955static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Russell King3ae5eae2005-11-09 22:32:44 +0000957 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -0800960 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 unregister_netdev(dev);
962 kfree(si->tx_buff.head);
963 kfree(si->rx_buff.head);
964 free_netdev(dev);
965 }
966
967 release_mem_region(__PREG(Ser2HSCR2), 0x04);
968 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
969 release_mem_region(__PREG(Ser2UTCR0), 0x24);
970
971 return 0;
972}
973
Russell Kingcbe1d242012-01-08 16:40:07 +0000974#ifdef CONFIG_PM
975/*
976 * Suspend the IrDA interface.
977 */
978static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
979{
980 struct net_device *dev = platform_get_drvdata(pdev);
981 struct sa1100_irda *si;
982
983 if (!dev)
984 return 0;
985
986 si = netdev_priv(dev);
987 if (si->open) {
988 /*
989 * Stop the transmit queue
990 */
991 netif_device_detach(dev);
992 disable_irq(dev->irq);
993 sa1100_irda_shutdown(si);
994 __sa1100_irda_set_power(si, 0);
995 }
996
997 return 0;
998}
999
1000/*
1001 * Resume the IrDA interface.
1002 */
1003static int sa1100_irda_resume(struct platform_device *pdev)
1004{
1005 struct net_device *dev = platform_get_drvdata(pdev);
1006 struct sa1100_irda *si;
1007
1008 if (!dev)
1009 return 0;
1010
1011 si = netdev_priv(dev);
1012 if (si->open) {
1013 /*
1014 * If we missed a speed change, initialise at the new speed
1015 * directly. It is debatable whether this is actually
1016 * required, but in the interests of continuing from where
1017 * we left off it is desirable. The converse argument is
1018 * that we should re-negotiate at 9600 baud again.
1019 */
1020 if (si->newspeed) {
1021 si->speed = si->newspeed;
1022 si->newspeed = 0;
1023 }
1024
1025 sa1100_irda_startup(si);
1026 __sa1100_irda_set_power(si, si->power);
1027 enable_irq(dev->irq);
1028
1029 /*
1030 * This automatically wakes up the queue
1031 */
1032 netif_device_attach(dev);
1033 }
1034
1035 return 0;
1036}
1037#else
1038#define sa1100_irda_suspend NULL
1039#define sa1100_irda_resume NULL
1040#endif
1041
Russell King3ae5eae2005-11-09 22:32:44 +00001042static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 .probe = sa1100_irda_probe,
1044 .remove = sa1100_irda_remove,
1045 .suspend = sa1100_irda_suspend,
1046 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001047 .driver = {
1048 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001049 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001050 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051};
1052
1053static int __init sa1100_irda_init(void)
1054{
1055 /*
1056 * Limit power level a sensible range.
1057 */
1058 if (power_level < 1)
1059 power_level = 1;
1060 if (power_level > 3)
1061 power_level = 3;
1062
Russell King3ae5eae2005-11-09 22:32:44 +00001063 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066static void __exit sa1100_irda_exit(void)
1067{
Russell King3ae5eae2005-11-09 22:32:44 +00001068 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071module_init(sa1100_irda_init);
1072module_exit(sa1100_irda_exit);
1073module_param(power_level, int, 0);
1074module_param(tx_lpm, int, 0);
1075module_param(max_rate, int, 0);
1076
1077MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1078MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1079MODULE_LICENSE("GPL");
1080MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1081MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1082MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001083MODULE_ALIAS("platform:sa11x0-ir");