blob: 620a48d00e2bf0fef1cd403eec3b6aaafec53a4e [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>
Russell Kingbf951542012-01-13 11:48:13 +000033#include <linux/dmaengine.h>
34#include <linux/sa11x0-dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <net/irda/irda.h>
37#include <net/irda/wrapper.h>
38#include <net/irda/irda_device.h>
39
Russell Kinga09e64f2008-08-05 16:14:15 +010040#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/mach/irda.h>
42
43static int power_level = 3;
44static int tx_lpm;
45static int max_rate = 4000000;
46
Russell King885767c2012-01-08 12:53:22 +000047struct sa1100_buf {
Russell King3c500a32012-01-12 13:56:28 +000048 struct device *dev;
Russell King885767c2012-01-08 12:53:22 +000049 struct sk_buff *skb;
Russell King32273f52012-01-12 12:45:00 +000050 struct scatterlist sg;
Russell Kingbf951542012-01-13 11:48:13 +000051 struct dma_chan *chan;
52 dma_cookie_t cookie;
Russell King885767c2012-01-08 12:53:22 +000053};
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct sa1100_irda {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 unsigned char utcr4;
57 unsigned char power;
58 unsigned char open;
59
60 int speed;
61 int newspeed;
62
Russell King885767c2012-01-08 12:53:22 +000063 struct sa1100_buf dma_rx;
64 struct sa1100_buf dma_tx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 struct device *dev;
67 struct irda_platform_data *pdata;
68 struct irlap_cb *irlap;
69 struct qos_info qos;
70
71 iobuff_t tx_buff;
72 iobuff_t rx_buff;
Russell King3d26db12012-01-08 16:16:39 +000073
74 int (*tx_start)(struct sk_buff *, struct net_device *, struct sa1100_irda *);
Russell King374f7732012-01-08 16:26:15 +000075 irqreturn_t (*irq)(struct net_device *, struct sa1100_irda *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Russell King0e888ee2012-01-08 16:30:44 +000078static int sa1100_irda_set_speed(struct sa1100_irda *, int);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define IS_FIR(si) ((si)->speed >= 4000000)
81
82#define HPSIR_MAX_RXLEN 2047
83
Russell Kingbf951542012-01-13 11:48:13 +000084static struct dma_slave_config sa1100_irda_fir_rx = {
85 .direction = DMA_FROM_DEVICE,
86 .src_addr = __PREG(Ser2HSDR),
87 .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
88 .src_maxburst = 8,
89};
90
91static struct dma_slave_config sa1100_irda_fir_tx = {
92 .direction = DMA_TO_DEVICE,
93 .dst_addr = __PREG(Ser2HSDR),
94 .dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
95 .dst_maxburst = 8,
96};
97
98static unsigned sa1100_irda_dma_xferred(struct sa1100_buf *buf)
99{
100 struct dma_chan *chan = buf->chan;
101 struct dma_tx_state state;
102 enum dma_status status;
103
104 status = chan->device->device_tx_status(chan, buf->cookie, &state);
105 if (status != DMA_PAUSED)
106 return 0;
107
108 return sg_dma_len(&buf->sg) - state.residue;
109}
110
111static int sa1100_irda_dma_request(struct device *dev, struct sa1100_buf *buf,
112 const char *name, struct dma_slave_config *cfg)
113{
114 dma_cap_mask_t m;
115 int ret;
116
117 dma_cap_zero(m);
118 dma_cap_set(DMA_SLAVE, m);
119
120 buf->chan = dma_request_channel(m, sa11x0_dma_filter_fn, (void *)name);
121 if (!buf->chan) {
122 dev_err(dev, "unable to request DMA channel for %s\n",
123 name);
124 return -ENOENT;
125 }
126
127 ret = dmaengine_slave_config(buf->chan, cfg);
128 if (ret)
129 dev_warn(dev, "DMA slave_config for %s returned %d\n",
130 name, ret);
131
132 buf->dev = buf->chan->device->dev;
133
134 return 0;
135}
136
137static void sa1100_irda_dma_start(struct sa1100_buf *buf,
138 enum dma_transfer_direction dir, dma_async_tx_callback cb, void *cb_p)
139{
140 struct dma_async_tx_descriptor *desc;
141 struct dma_chan *chan = buf->chan;
142
143 desc = chan->device->device_prep_slave_sg(chan, &buf->sg, 1, dir,
144 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
145 if (desc) {
146 desc->callback = cb;
147 desc->callback_param = cb_p;
148 buf->cookie = dmaengine_submit(desc);
149 dma_async_issue_pending(chan);
150 }
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 * Allocate and map the receive buffer, unless it is already allocated.
155 */
156static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
157{
Russell King885767c2012-01-08 12:53:22 +0000158 if (si->dma_rx.skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 0;
160
Russell King885767c2012-01-08 12:53:22 +0000161 si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
162 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
164 return -ENOMEM;
165 }
166
167 /*
168 * Align any IP headers that may be contained
169 * within the frame.
170 */
Russell King885767c2012-01-08 12:53:22 +0000171 skb_reserve(si->dma_rx.skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Russell King32273f52012-01-12 12:45:00 +0000173 sg_set_buf(&si->dma_rx.sg, si->dma_rx.skb->data, HPSIR_MAX_RXLEN);
Russell King3c500a32012-01-12 13:56:28 +0000174 if (dma_map_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE) == 0) {
Russell King885767c2012-01-08 12:53:22 +0000175 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000176 return -ENOMEM;
177 }
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return 0;
180}
181
182/*
183 * We want to get here as soon as possible, and get the receiver setup.
184 * We use the existing buffer.
185 */
186static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
187{
Russell King885767c2012-01-08 12:53:22 +0000188 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
190 return;
191 }
192
193 /*
194 * First empty receive FIFO
195 */
Russell King6a7f4912012-01-08 20:49:28 +0000196 Ser2HSCR0 = HSCR0_HSSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 /*
199 * Enable the DMA, receiver and receive interrupt.
200 */
Russell Kingbf951542012-01-13 11:48:13 +0000201 dmaengine_terminate_all(si->dma_rx.chan);
202 sa1100_irda_dma_start(&si->dma_rx, DMA_DEV_TO_MEM, NULL, NULL);
203
Russell King6a7f4912012-01-08 20:49:28 +0000204 Ser2HSCR0 = HSCR0_HSSP | HSCR0_RXE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Russell King0e888ee2012-01-08 16:30:44 +0000207static void sa1100_irda_check_speed(struct sa1100_irda *si)
208{
209 if (si->newspeed) {
210 sa1100_irda_set_speed(si, si->newspeed);
211 si->newspeed = 0;
212 }
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
Russell King3d26db12012-01-08 16:16:39 +0000216 * HP-SIR format support.
217 */
218static int sa1100_irda_sir_tx_start(struct sk_buff *skb, struct net_device *dev,
219 struct sa1100_irda *si)
220{
221 si->tx_buff.data = si->tx_buff.head;
222 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
223 si->tx_buff.truesize);
224
225 /*
226 * Set the transmit interrupt enable. This will fire off an
227 * interrupt immediately. Note that we disable the receiver
228 * so we won't get spurious characters received.
229 */
230 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
231
232 dev_kfree_skb(skb);
233
234 return NETDEV_TX_OK;
235}
236
Russell Kinga6b2ea62012-01-08 17:10:01 +0000237static irqreturn_t sa1100_irda_sir_irq(struct net_device *dev, struct sa1100_irda *si)
238{
239 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)) {
254 dev->stats.rx_errors++;
255 if (stat & UTSR1_FRE)
256 dev->stats.rx_frame_errors++;
257 if (stat & UTSR1_ROR)
258 dev->stats.rx_fifo_errors++;
259 } else
260 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
261
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 */
275 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);
278 }
279
280 if (status & (UTSR0_RFS | UTSR0_RID)) {
281 /*
282 * Fifo contains more than 1 character.
283 */
284 do {
285 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
286 Ser2UTDR);
287 } while (Ser2UTSR1 & UTSR1_RNE);
288
289 }
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) {
301 dev->stats.tx_packets++;
302 dev->stats.tx_bytes += si->tx_buff.data -
303 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 sa1100_irda_check_speed(si);
322
323 /* I'm hungry! */
324 netif_wake_queue(dev);
325 }
326 }
327
328 return IRQ_HANDLED;
329}
330
Russell King3d26db12012-01-08 16:16:39 +0000331/*
332 * FIR format support.
333 */
Russell King26f2bee2012-01-08 17:48:02 +0000334static void sa1100_irda_firtxdma_irq(void *id)
335{
336 struct net_device *dev = id;
337 struct sa1100_irda *si = netdev_priv(dev);
338 struct sk_buff *skb;
339
340 /*
341 * Wait for the transmission to complete. Unfortunately,
342 * the hardware doesn't give us an interrupt to indicate
343 * "end of frame".
344 */
345 do
346 rmb();
347 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
348
349 /*
350 * Clear the transmit underrun bit.
351 */
352 Ser2HSSR0 = HSSR0_TUR;
353
354 /*
355 * Do we need to change speed? Note that we're lazy
356 * here - we don't free the old dma_rx.skb. We don't need
357 * to allocate a buffer either.
358 */
359 sa1100_irda_check_speed(si);
360
361 /*
362 * Start reception. This disables the transmitter for
363 * us. This will be using the existing RX buffer.
364 */
365 sa1100_irda_rx_dma_start(si);
366
367 /* Account and free the packet. */
368 skb = si->dma_tx.skb;
369 if (skb) {
Russell King3c500a32012-01-12 13:56:28 +0000370 dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1,
Russell King32273f52012-01-12 12:45:00 +0000371 DMA_TO_DEVICE);
Russell King26f2bee2012-01-08 17:48:02 +0000372 dev->stats.tx_packets ++;
373 dev->stats.tx_bytes += skb->len;
374 dev_kfree_skb_irq(skb);
375 si->dma_tx.skb = NULL;
376 }
377
378 /*
379 * Make sure that the TX queue is available for sending
380 * (for retries). TX has priority over RX at all times.
381 */
382 netif_wake_queue(dev);
383}
384
Russell King3d26db12012-01-08 16:16:39 +0000385static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
386 struct sa1100_irda *si)
387{
388 int mtt = irda_get_mtt(skb);
389
390 si->dma_tx.skb = skb;
Russell King32273f52012-01-12 12:45:00 +0000391 sg_set_buf(&si->dma_tx.sg, skb->data, skb->len);
Russell King3c500a32012-01-12 13:56:28 +0000392 if (dma_map_sg(si->dma_tx.dev, &si->dma_tx.sg, 1, DMA_TO_DEVICE) == 0) {
Russell King3d26db12012-01-08 16:16:39 +0000393 si->dma_tx.skb = NULL;
394 netif_wake_queue(dev);
395 dev->stats.tx_dropped++;
396 dev_kfree_skb(skb);
397 return NETDEV_TX_OK;
398 }
399
Russell Kingbf951542012-01-13 11:48:13 +0000400 sa1100_irda_dma_start(&si->dma_tx, DMA_MEM_TO_DEV, sa1100_irda_firtxdma_irq, dev);
Russell King3d26db12012-01-08 16:16:39 +0000401
402 /*
403 * If we have a mean turn-around time, impose the specified
404 * specified delay. We could shorten this by timing from
405 * the point we received the packet.
406 */
407 if (mtt)
408 udelay(mtt);
409
Russell King6a7f4912012-01-08 20:49:28 +0000410 Ser2HSCR0 = HSCR0_HSSP | HSCR0_TXE;
Russell King3d26db12012-01-08 16:16:39 +0000411
412 return NETDEV_TX_OK;
413}
414
Russell Kinga6b2ea62012-01-08 17:10:01 +0000415static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
416{
417 struct sk_buff *skb = si->dma_rx.skb;
Russell Kinga6b2ea62012-01-08 17:10:01 +0000418 unsigned int len, stat, data;
419
420 if (!skb) {
421 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
422 return;
423 }
424
425 /*
426 * Get the current data position.
427 */
Russell Kingbf951542012-01-13 11:48:13 +0000428 len = sa1100_irda_dma_xferred(&si->dma_rx);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000429 if (len > HPSIR_MAX_RXLEN)
430 len = HPSIR_MAX_RXLEN;
Russell King3c500a32012-01-12 13:56:28 +0000431 dma_unmap_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000432
433 do {
434 /*
435 * Read Status, and then Data.
436 */
437 stat = Ser2HSSR1;
438 rmb();
439 data = Ser2HSDR;
440
441 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
442 dev->stats.rx_errors++;
443 if (stat & HSSR1_CRE)
444 dev->stats.rx_crc_errors++;
445 if (stat & HSSR1_ROR)
446 dev->stats.rx_frame_errors++;
447 } else
448 skb->data[len++] = data;
449
450 /*
451 * If we hit the end of frame, there's
452 * no point in continuing.
453 */
454 if (stat & HSSR1_EOF)
455 break;
456 } while (Ser2HSSR0 & HSSR0_EIF);
457
458 if (stat & HSSR1_EOF) {
459 si->dma_rx.skb = NULL;
460
461 skb_put(skb, len);
462 skb->dev = dev;
463 skb_reset_mac_header(skb);
464 skb->protocol = htons(ETH_P_IRDA);
465 dev->stats.rx_packets++;
466 dev->stats.rx_bytes += len;
467
468 /*
469 * Before we pass the buffer up, allocate a new one.
470 */
471 sa1100_irda_rx_alloc(si);
472
473 netif_rx(skb);
474 } else {
475 /*
476 * Remap the buffer - it was previously mapped, and we
477 * hope that this succeeds.
478 */
Russell King3c500a32012-01-12 13:56:28 +0000479 dma_map_sg(si->dma_rx.dev, &si->dma_rx.sg, 1, DMA_FROM_DEVICE);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000480 }
481}
482
483/*
484 * We only have to handle RX events here; transmit events go via the TX
485 * DMA handler. We disable RX, process, and the restart RX.
486 */
487static irqreturn_t sa1100_irda_fir_irq(struct net_device *dev, struct sa1100_irda *si)
488{
489 /*
490 * Stop RX DMA
491 */
Russell Kingbf951542012-01-13 11:48:13 +0000492 dmaengine_pause(si->dma_rx.chan);
Russell Kinga6b2ea62012-01-08 17:10:01 +0000493
494 /*
495 * Framing error - we throw away the packet completely.
496 * Clearing RXE flushes the error conditions and data
497 * from the fifo.
498 */
499 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
500 dev->stats.rx_errors++;
501
502 if (Ser2HSSR0 & HSSR0_FRE)
503 dev->stats.rx_frame_errors++;
504
505 /*
506 * Clear out the DMA...
507 */
Russell King6a7f4912012-01-08 20:49:28 +0000508 Ser2HSCR0 = HSCR0_HSSP;
Russell Kinga6b2ea62012-01-08 17:10:01 +0000509
510 /*
511 * Clear selected status bits now, so we
512 * don't miss them next time around.
513 */
514 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
515 }
516
517 /*
518 * Deal with any receive errors. The any of the lowest
519 * 8 bytes in the FIFO may contain an error. We must read
520 * them one by one. The "error" could even be the end of
521 * packet!
522 */
523 if (Ser2HSSR0 & HSSR0_EIF)
524 sa1100_irda_fir_error(si, dev);
525
526 /*
527 * No matter what happens, we must restart reception.
528 */
529 sa1100_irda_rx_dma_start(si);
530
531 return IRQ_HANDLED;
532}
Russell King3d26db12012-01-08 16:16:39 +0000533
534/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * Set the IrDA communications speed.
536 */
537static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
538{
539 unsigned long flags;
540 int brd, ret = -EINVAL;
541
542 switch (speed) {
543 case 9600: case 19200: case 38400:
544 case 57600: case 115200:
545 brd = 3686400 / (16 * speed) - 1;
546
Russell Kingbf951542012-01-13 11:48:13 +0000547 /* Stop the receive DMA, and configure transmit. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (IS_FIR(si))
Russell Kingbf951542012-01-13 11:48:13 +0000549 dmaengine_terminate_all(si->dma_rx.chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 local_irq_save(flags);
552
553 Ser2UTCR3 = 0;
554 Ser2HSCR0 = HSCR0_UART;
555
556 Ser2UTCR1 = brd >> 8;
557 Ser2UTCR2 = brd;
558
559 /*
560 * Clear status register
561 */
562 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
563 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
564
565 if (si->pdata->set_speed)
566 si->pdata->set_speed(si->dev, speed);
567
568 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000569 si->tx_start = sa1100_irda_sir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000570 si->irq = sa1100_irda_sir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 local_irq_restore(flags);
573 ret = 0;
574 break;
575
576 case 4000000:
577 local_irq_save(flags);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 Ser2HSSR0 = 0xff;
Russell King6a7f4912012-01-08 20:49:28 +0000580 Ser2HSCR0 = HSCR0_HSSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 Ser2UTCR3 = 0;
582
583 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000584 si->tx_start = sa1100_irda_fir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000585 si->irq = sa1100_irda_fir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (si->pdata->set_speed)
588 si->pdata->set_speed(si->dev, speed);
589
590 sa1100_irda_rx_alloc(si);
591 sa1100_irda_rx_dma_start(si);
592
593 local_irq_restore(flags);
594
595 break;
596
597 default:
598 break;
599 }
600
601 return ret;
602}
603
604/*
605 * Control the power state of the IrDA transmitter.
606 * State:
607 * 0 - off
608 * 1 - short range, lowest power
609 * 2 - medium range, medium power
610 * 3 - maximum range, high power
611 *
612 * Currently, only assabet is known to support this.
613 */
614static int
615__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
616{
617 int ret = 0;
618 if (si->pdata->set_power)
619 ret = si->pdata->set_power(si->dev, state);
620 return ret;
621}
622
623static inline int
624sa1100_set_power(struct sa1100_irda *si, unsigned int state)
625{
626 int ret;
627
628 ret = __sa1100_irda_set_power(si, state);
629 if (ret == 0)
630 si->power = state;
631
632 return ret;
633}
634
David Howells7d12e782006-10-05 14:55:46 +0100635static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
637 struct net_device *dev = dev_id;
Russell King374f7732012-01-08 16:26:15 +0000638 struct sa1100_irda *si = netdev_priv(dev);
639
640 return si->irq(dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
644{
Wang Chen4cf16532008-11-12 23:38:14 -0800645 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int speed = irda_get_next_speed(skb);
647
648 /*
649 * Does this packet contain a request to change the interface
650 * speed? If so, remember it until we complete the transmission
651 * of this frame.
652 */
653 if (speed != si->speed && speed != -1)
654 si->newspeed = speed;
655
Russell King3d26db12012-01-08 16:16:39 +0000656 /* If this is an empty frame, we can bypass a lot. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (skb->len == 0) {
Russell King0e888ee2012-01-08 16:30:44 +0000658 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000660 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Russell King3d26db12012-01-08 16:16:39 +0000663 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Russell King3d26db12012-01-08 16:16:39 +0000665 /* We must not already have a skb to transmit... */
666 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Russell King3d26db12012-01-08 16:16:39 +0000668 return si->tx_start(skb, dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671static int
672sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
673{
674 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800675 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 int ret = -EOPNOTSUPP;
677
678 switch (cmd) {
679 case SIOCSBANDWIDTH:
680 if (capable(CAP_NET_ADMIN)) {
681 /*
682 * We are unable to set the speed if the
683 * device is not running.
684 */
685 if (si->open) {
686 ret = sa1100_irda_set_speed(si,
687 rq->ifr_baudrate);
688 } else {
689 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
690 ret = 0;
691 }
692 }
693 break;
694
695 case SIOCSMEDIABUSY:
696 ret = -EPERM;
697 if (capable(CAP_NET_ADMIN)) {
698 irda_device_set_media_busy(dev, TRUE);
699 ret = 0;
700 }
701 break;
702
703 case SIOCGRECEIVING:
704 rq->ifr_receiving = IS_FIR(si) ? 0
705 : si->rx_buff.state != OUTSIDE_FRAME;
706 break;
707
708 default:
709 break;
710 }
711
712 return ret;
713}
714
Russell Kingcbe1d242012-01-08 16:40:07 +0000715static int sa1100_irda_startup(struct sa1100_irda *si)
716{
717 int ret;
718
719 /*
720 * Ensure that the ports for this device are setup correctly.
721 */
722 if (si->pdata->startup) {
723 ret = si->pdata->startup(si->dev);
724 if (ret)
725 return ret;
726 }
727
728 /*
729 * Configure PPC for IRDA - we want to drive TXD2 low.
730 * We also want to drive this pin low during sleep.
731 */
732 PPSR &= ~PPC_TXD2;
733 PSDR &= ~PPC_TXD2;
734 PPDR |= PPC_TXD2;
735
736 /*
737 * Enable HP-SIR modulation, and ensure that the port is disabled.
738 */
739 Ser2UTCR3 = 0;
740 Ser2HSCR0 = HSCR0_UART;
741 Ser2UTCR4 = si->utcr4;
742 Ser2UTCR0 = UTCR0_8BitData;
743 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
744
745 /*
746 * Clear status register
747 */
748 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
749
750 ret = sa1100_irda_set_speed(si, si->speed = 9600);
751 if (ret) {
752 Ser2UTCR3 = 0;
753 Ser2HSCR0 = 0;
754
755 if (si->pdata->shutdown)
756 si->pdata->shutdown(si->dev);
757 }
758
759 return ret;
760}
761
762static void sa1100_irda_shutdown(struct sa1100_irda *si)
763{
764 /*
765 * Stop all DMA activity.
766 */
Russell Kingbf951542012-01-13 11:48:13 +0000767 dmaengine_terminate_all(si->dma_rx.chan);
768 dmaengine_terminate_all(si->dma_tx.chan);
Russell Kingcbe1d242012-01-08 16:40:07 +0000769
770 /* Disable the port. */
771 Ser2UTCR3 = 0;
772 Ser2HSCR0 = 0;
773
774 if (si->pdata->shutdown)
775 si->pdata->shutdown(si->dev);
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static int sa1100_irda_start(struct net_device *dev)
779{
Wang Chen4cf16532008-11-12 23:38:14 -0800780 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int err;
782
783 si->speed = 9600;
784
Russell Kingbf951542012-01-13 11:48:13 +0000785 err = sa1100_irda_dma_request(si->dev, &si->dma_rx, "Ser2ICPRc",
786 &sa1100_irda_fir_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (err)
788 goto err_rx_dma;
789
Russell Kingbf951542012-01-13 11:48:13 +0000790 err = sa1100_irda_dma_request(si->dev, &si->dma_tx, "Ser2ICPTr",
791 &sa1100_irda_sir_tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (err)
793 goto err_tx_dma;
794
795 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * Setup the serial port for the specified speed.
797 */
798 err = sa1100_irda_startup(si);
799 if (err)
800 goto err_startup;
801
802 /*
803 * Open a new IrLAP layer instance.
804 */
805 si->irlap = irlap_open(dev, &si->qos, "sa1100");
806 err = -ENOMEM;
807 if (!si->irlap)
808 goto err_irlap;
809
Russell King374f7732012-01-08 16:26:15 +0000810 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
811 if (err)
812 goto err_irq;
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
815 * Now enable the interrupt and start the queue
816 */
817 si->open = 1;
818 sa1100_set_power(si, power_level); /* low power mode */
Russell King374f7732012-01-08 16:26:15 +0000819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 netif_start_queue(dev);
821 return 0;
822
Russell King374f7732012-01-08 16:26:15 +0000823err_irq:
824 irlap_close(si->irlap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825err_irlap:
826 si->open = 0;
827 sa1100_irda_shutdown(si);
828err_startup:
Russell Kingbf951542012-01-13 11:48:13 +0000829 dma_release_channel(si->dma_tx.chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830err_tx_dma:
Russell Kingbf951542012-01-13 11:48:13 +0000831 dma_release_channel(si->dma_rx.chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832err_rx_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return err;
834}
835
836static int sa1100_irda_stop(struct net_device *dev)
837{
Wang Chen4cf16532008-11-12 23:38:14 -0800838 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000839 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Russell King374f7732012-01-08 16:26:15 +0000841 netif_stop_queue(dev);
842
843 si->open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 sa1100_irda_shutdown(si);
845
846 /*
Russell Kingba845252012-01-08 15:38:15 +0000847 * If we have been doing any DMA activity, make sure we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * tidy that up cleanly.
849 */
Russell Kingba845252012-01-08 15:38:15 +0000850 skb = si->dma_rx.skb;
851 if (skb) {
Russell King3c500a32012-01-12 13:56:28 +0000852 dma_unmap_sg(si->dma_rx.dev, &si->dma_rx.sg, 1,
Russell King32273f52012-01-12 12:45:00 +0000853 DMA_FROM_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000854 dev_kfree_skb(skb);
Russell King885767c2012-01-08 12:53:22 +0000855 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
857
Russell Kingba845252012-01-08 15:38:15 +0000858 skb = si->dma_tx.skb;
859 if (skb) {
Russell King3c500a32012-01-12 13:56:28 +0000860 dma_unmap_sg(si->dma_tx.dev, &si->dma_tx.sg, 1,
Russell King32273f52012-01-12 12:45:00 +0000861 DMA_TO_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000862 dev_kfree_skb(skb);
863 si->dma_tx.skb = NULL;
864 }
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 /* Stop IrLAP */
867 if (si->irlap) {
868 irlap_close(si->irlap);
869 si->irlap = NULL;
870 }
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 /*
873 * Free resources
874 */
Russell Kingbf951542012-01-13 11:48:13 +0000875 dma_release_channel(si->dma_tx.chan);
876 dma_release_channel(si->dma_rx.chan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 free_irq(dev->irq, dev);
878
879 sa1100_set_power(si, 0);
880
881 return 0;
882}
883
884static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
885{
886 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
887 if (io->head != NULL) {
888 io->truesize = size;
889 io->in_frame = FALSE;
890 io->state = OUTSIDE_FRAME;
891 io->data = io->head;
892 }
893 return io->head ? 0 : -ENOMEM;
894}
895
Alexander Beregalova1de9662009-04-15 12:52:42 +0000896static const struct net_device_ops sa1100_irda_netdev_ops = {
897 .ndo_open = sa1100_irda_start,
898 .ndo_stop = sa1100_irda_stop,
899 .ndo_start_xmit = sa1100_irda_hard_xmit,
900 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000901};
902
Russell King3ae5eae2005-11-09 22:32:44 +0000903static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 struct net_device *dev;
906 struct sa1100_irda *si;
907 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000908 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 if (!pdev->dev.platform_data)
911 return -EINVAL;
912
Russell Kinge556fdb2012-01-08 12:02:17 +0000913 irq = platform_get_irq(pdev, 0);
914 if (irq <= 0)
915 return irq < 0 ? irq : -ENXIO;
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
918 if (err)
919 goto err_mem_1;
920 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
921 if (err)
922 goto err_mem_2;
923 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
924 if (err)
925 goto err_mem_3;
926
927 dev = alloc_irdadev(sizeof(struct sa1100_irda));
928 if (!dev)
929 goto err_mem_4;
930
Russell Kingd3238602012-01-08 12:07:24 +0000931 SET_NETDEV_DEV(dev, &pdev->dev);
932
Wang Chen4cf16532008-11-12 23:38:14 -0800933 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 si->dev = &pdev->dev;
935 si->pdata = pdev->dev.platform_data;
936
Russell King32273f52012-01-12 12:45:00 +0000937 sg_init_table(&si->dma_rx.sg, 1);
938 sg_init_table(&si->dma_tx.sg, 1);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /*
941 * Initialise the HP-SIR buffers
942 */
943 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
944 if (err)
945 goto err_mem_5;
Russell King04b7fc42012-01-12 13:51:10 +0000946 err = sa1100_irda_init_iobuf(&si->tx_buff, IRDA_SIR_MAX_FRAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (err)
948 goto err_mem_5;
949
Alexander Beregalova1de9662009-04-15 12:52:42 +0000950 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000951 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 irda_init_max_qos_capabilies(&si->qos);
954
955 /*
956 * We support original IRDA up to 115k2. (we don't currently
957 * support 4Mbps). Min Turn Time set to 1ms or greater.
958 */
959 baudrate_mask = IR_9600;
960
961 switch (max_rate) {
962 case 4000000: baudrate_mask |= IR_4000000 << 8;
963 case 115200: baudrate_mask |= IR_115200;
964 case 57600: baudrate_mask |= IR_57600;
965 case 38400: baudrate_mask |= IR_38400;
966 case 19200: baudrate_mask |= IR_19200;
967 }
968
969 si->qos.baud_rate.bits &= baudrate_mask;
970 si->qos.min_turn_time.bits = 7;
971
972 irda_qos_bits_to_value(&si->qos);
973
974 si->utcr4 = UTCR4_HPSIR;
975 if (tx_lpm)
976 si->utcr4 |= UTCR4_Z1_6us;
977
978 /*
979 * Initially enable HP-SIR modulation, and ensure that the port
980 * is disabled.
981 */
982 Ser2UTCR3 = 0;
983 Ser2UTCR4 = si->utcr4;
984 Ser2HSCR0 = HSCR0_UART;
985
986 err = register_netdev(dev);
987 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000988 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 if (err) {
991 err_mem_5:
992 kfree(si->tx_buff.head);
993 kfree(si->rx_buff.head);
994 free_netdev(dev);
995 err_mem_4:
996 release_mem_region(__PREG(Ser2HSCR2), 0x04);
997 err_mem_3:
998 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
999 err_mem_2:
1000 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1001 }
1002 err_mem_1:
1003 return err;
1004}
1005
Russell King3ae5eae2005-11-09 22:32:44 +00001006static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Russell King3ae5eae2005-11-09 22:32:44 +00001008 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -08001011 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 unregister_netdev(dev);
1013 kfree(si->tx_buff.head);
1014 kfree(si->rx_buff.head);
1015 free_netdev(dev);
1016 }
1017
1018 release_mem_region(__PREG(Ser2HSCR2), 0x04);
1019 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
1020 release_mem_region(__PREG(Ser2UTCR0), 0x24);
1021
1022 return 0;
1023}
1024
Russell Kingcbe1d242012-01-08 16:40:07 +00001025#ifdef CONFIG_PM
1026/*
1027 * Suspend the IrDA interface.
1028 */
1029static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
1030{
1031 struct net_device *dev = platform_get_drvdata(pdev);
1032 struct sa1100_irda *si;
1033
1034 if (!dev)
1035 return 0;
1036
1037 si = netdev_priv(dev);
1038 if (si->open) {
1039 /*
1040 * Stop the transmit queue
1041 */
1042 netif_device_detach(dev);
1043 disable_irq(dev->irq);
1044 sa1100_irda_shutdown(si);
1045 __sa1100_irda_set_power(si, 0);
1046 }
1047
1048 return 0;
1049}
1050
1051/*
1052 * Resume the IrDA interface.
1053 */
1054static int sa1100_irda_resume(struct platform_device *pdev)
1055{
1056 struct net_device *dev = platform_get_drvdata(pdev);
1057 struct sa1100_irda *si;
1058
1059 if (!dev)
1060 return 0;
1061
1062 si = netdev_priv(dev);
1063 if (si->open) {
1064 /*
1065 * If we missed a speed change, initialise at the new speed
1066 * directly. It is debatable whether this is actually
1067 * required, but in the interests of continuing from where
1068 * we left off it is desirable. The converse argument is
1069 * that we should re-negotiate at 9600 baud again.
1070 */
1071 if (si->newspeed) {
1072 si->speed = si->newspeed;
1073 si->newspeed = 0;
1074 }
1075
1076 sa1100_irda_startup(si);
1077 __sa1100_irda_set_power(si, si->power);
1078 enable_irq(dev->irq);
1079
1080 /*
1081 * This automatically wakes up the queue
1082 */
1083 netif_device_attach(dev);
1084 }
1085
1086 return 0;
1087}
1088#else
1089#define sa1100_irda_suspend NULL
1090#define sa1100_irda_resume NULL
1091#endif
1092
Russell King3ae5eae2005-11-09 22:32:44 +00001093static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 .probe = sa1100_irda_probe,
1095 .remove = sa1100_irda_remove,
1096 .suspend = sa1100_irda_suspend,
1097 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001098 .driver = {
1099 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001100 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001101 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102};
1103
1104static int __init sa1100_irda_init(void)
1105{
1106 /*
1107 * Limit power level a sensible range.
1108 */
1109 if (power_level < 1)
1110 power_level = 1;
1111 if (power_level > 3)
1112 power_level = 3;
1113
Russell King3ae5eae2005-11-09 22:32:44 +00001114 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117static void __exit sa1100_irda_exit(void)
1118{
Russell King3ae5eae2005-11-09 22:32:44 +00001119 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122module_init(sa1100_irda_init);
1123module_exit(sa1100_irda_exit);
1124module_param(power_level, int, 0);
1125module_param(tx_lpm, int, 0);
1126module_param(max_rate, int, 0);
1127
1128MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1129MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1130MODULE_LICENSE("GPL");
1131MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1132MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1133MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001134MODULE_ALIAS("platform:sa11x0-ir");