blob: bfae34fa7a9ecb6ce262a02ee71b539990838498 [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
Russell King0e888ee2012-01-08 16:30:44 +000073static int sa1100_irda_set_speed(struct sa1100_irda *, int);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define IS_FIR(si) ((si)->speed >= 4000000)
76
77#define HPSIR_MAX_RXLEN 2047
78
79/*
80 * Allocate and map the receive buffer, unless it is already allocated.
81 */
82static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
83{
Russell King885767c2012-01-08 12:53:22 +000084 if (si->dma_rx.skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86
Russell King885767c2012-01-08 12:53:22 +000087 si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
88 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
90 return -ENOMEM;
91 }
92
93 /*
94 * Align any IP headers that may be contained
95 * within the frame.
96 */
Russell King885767c2012-01-08 12:53:22 +000097 skb_reserve(si->dma_rx.skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Russell King885767c2012-01-08 12:53:22 +000099 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 HPSIR_MAX_RXLEN,
101 DMA_FROM_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000102 if (dma_mapping_error(si->dev, si->dma_rx.dma)) {
103 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000104 return -ENOMEM;
105 }
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108}
109
110/*
111 * We want to get here as soon as possible, and get the receiver setup.
112 * We use the existing buffer.
113 */
114static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
115{
Russell King885767c2012-01-08 12:53:22 +0000116 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
118 return;
119 }
120
121 /*
122 * First empty receive FIFO
123 */
124 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
125
126 /*
127 * Enable the DMA, receiver and receive interrupt.
128 */
Russell King885767c2012-01-08 12:53:22 +0000129 sa1100_clear_dma(si->dma_rx.regs);
130 sa1100_start_dma(si->dma_rx.regs, si->dma_rx.dma, HPSIR_MAX_RXLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
132}
133
Russell King0e888ee2012-01-08 16:30:44 +0000134static void sa1100_irda_check_speed(struct sa1100_irda *si)
135{
136 if (si->newspeed) {
137 sa1100_irda_set_speed(si, si->newspeed);
138 si->newspeed = 0;
139 }
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/*
143 * Set the IrDA communications speed.
144 */
145static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
146{
147 unsigned long flags;
148 int brd, ret = -EINVAL;
149
150 switch (speed) {
151 case 9600: case 19200: case 38400:
152 case 57600: case 115200:
153 brd = 3686400 / (16 * speed) - 1;
154
155 /*
156 * Stop the receive DMA.
157 */
158 if (IS_FIR(si))
Russell King885767c2012-01-08 12:53:22 +0000159 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 local_irq_save(flags);
162
163 Ser2UTCR3 = 0;
164 Ser2HSCR0 = HSCR0_UART;
165
166 Ser2UTCR1 = brd >> 8;
167 Ser2UTCR2 = brd;
168
169 /*
170 * Clear status register
171 */
172 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
173 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
174
175 if (si->pdata->set_speed)
176 si->pdata->set_speed(si->dev, speed);
177
178 si->speed = speed;
179
180 local_irq_restore(flags);
181 ret = 0;
182 break;
183
184 case 4000000:
185 local_irq_save(flags);
186
187 si->hscr0 = 0;
188
189 Ser2HSSR0 = 0xff;
190 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
191 Ser2UTCR3 = 0;
192
193 si->speed = speed;
194
195 if (si->pdata->set_speed)
196 si->pdata->set_speed(si->dev, speed);
197
198 sa1100_irda_rx_alloc(si);
199 sa1100_irda_rx_dma_start(si);
200
201 local_irq_restore(flags);
202
203 break;
204
205 default:
206 break;
207 }
208
209 return ret;
210}
211
212/*
213 * Control the power state of the IrDA transmitter.
214 * State:
215 * 0 - off
216 * 1 - short range, lowest power
217 * 2 - medium range, medium power
218 * 3 - maximum range, high power
219 *
220 * Currently, only assabet is known to support this.
221 */
222static int
223__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
224{
225 int ret = 0;
226 if (si->pdata->set_power)
227 ret = si->pdata->set_power(si->dev, state);
228 return ret;
229}
230
231static inline int
232sa1100_set_power(struct sa1100_irda *si, unsigned int state)
233{
234 int ret;
235
236 ret = __sa1100_irda_set_power(si, state);
237 if (ret == 0)
238 si->power = state;
239
240 return ret;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
244 * HP-SIR format interrupt service routines.
245 */
246static void sa1100_irda_hpsir_irq(struct net_device *dev)
247{
Wang Chen4cf16532008-11-12 23:38:14 -0800248 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int status;
250
251 status = Ser2UTSR0;
252
253 /*
254 * Deal with any receive errors first. The bytes in error may be
255 * the only bytes in the receive FIFO, so we do this first.
256 */
257 while (status & UTSR0_EIF) {
258 int stat, data;
259
260 stat = Ser2UTSR1;
261 data = Ser2UTDR;
262
263 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800264 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (stat & UTSR1_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800266 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (stat & UTSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800268 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 } else
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800270 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 status = Ser2UTSR0;
273 }
274
275 /*
276 * We must clear certain bits.
277 */
278 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
279
280 if (status & UTSR0_RFS) {
281 /*
282 * There are at least 4 bytes in the FIFO. Read 3 bytes
283 * and leave the rest to the block below.
284 */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800285 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
286 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
287 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289
290 if (status & (UTSR0_RFS | UTSR0_RID)) {
291 /*
292 * Fifo contains more than 1 character.
293 */
294 do {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800295 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 Ser2UTDR);
297 } while (Ser2UTSR1 & UTSR1_RNE);
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 if (status & UTSR0_TFS && si->tx_buff.len) {
302 /*
303 * Transmitter FIFO is not full
304 */
305 do {
306 Ser2UTDR = *si->tx_buff.data++;
307 si->tx_buff.len -= 1;
308 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
309
310 if (si->tx_buff.len == 0) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800311 dev->stats.tx_packets++;
312 dev->stats.tx_bytes += si->tx_buff.data -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 si->tx_buff.head;
314
315 /*
316 * We need to ensure that the transmitter has
317 * finished.
318 */
319 do
320 rmb();
321 while (Ser2UTSR1 & UTSR1_TBY);
322
323 /*
324 * Ok, we've finished transmitting. Now enable
325 * the receiver. Sometimes we get a receive IRQ
326 * immediately after a transmit...
327 */
328 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
329 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
330
Russell King0e888ee2012-01-08 16:30:44 +0000331 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* I'm hungry! */
334 netif_wake_queue(dev);
335 }
336 }
337}
338
339static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
340{
Russell King885767c2012-01-08 12:53:22 +0000341 struct sk_buff *skb = si->dma_rx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 dma_addr_t dma_addr;
343 unsigned int len, stat, data;
344
345 if (!skb) {
346 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
347 return;
348 }
349
350 /*
351 * Get the current data position.
352 */
Russell King885767c2012-01-08 12:53:22 +0000353 dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
354 len = dma_addr - si->dma_rx.dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (len > HPSIR_MAX_RXLEN)
356 len = HPSIR_MAX_RXLEN;
Russell King885767c2012-01-08 12:53:22 +0000357 dma_unmap_single(si->dev, si->dma_rx.dma, len, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 do {
360 /*
361 * Read Status, and then Data.
362 */
363 stat = Ser2HSSR1;
364 rmb();
365 data = Ser2HSDR;
366
367 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800368 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (stat & HSSR1_CRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800370 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (stat & HSSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800372 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 } else
374 skb->data[len++] = data;
375
376 /*
377 * If we hit the end of frame, there's
378 * no point in continuing.
379 */
380 if (stat & HSSR1_EOF)
381 break;
382 } while (Ser2HSSR0 & HSSR0_EIF);
383
384 if (stat & HSSR1_EOF) {
Russell King885767c2012-01-08 12:53:22 +0000385 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 skb_put(skb, len);
388 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700389 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 skb->protocol = htons(ETH_P_IRDA);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800391 dev->stats.rx_packets++;
392 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /*
395 * Before we pass the buffer up, allocate a new one.
396 */
397 sa1100_irda_rx_alloc(si);
398
399 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 } else {
401 /*
Russell King22f0bf92012-01-08 13:55:23 +0000402 * Remap the buffer - it was previously mapped, and we
403 * hope that this succeeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
Russell King885767c2012-01-08 12:53:22 +0000405 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 HPSIR_MAX_RXLEN,
407 DMA_FROM_DEVICE);
408 }
409}
410
411/*
412 * FIR format interrupt service routine. We only have to
413 * handle RX events; transmit events go via the TX DMA handler.
414 *
415 * No matter what, we disable RX, process, and the restart RX.
416 */
417static void sa1100_irda_fir_irq(struct net_device *dev)
418{
Wang Chen4cf16532008-11-12 23:38:14 -0800419 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /*
422 * Stop RX DMA
423 */
Russell King885767c2012-01-08 12:53:22 +0000424 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /*
427 * Framing error - we throw away the packet completely.
428 * Clearing RXE flushes the error conditions and data
429 * from the fifo.
430 */
431 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800432 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 if (Ser2HSSR0 & HSSR0_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800435 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 /*
438 * Clear out the DMA...
439 */
440 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
441
442 /*
443 * Clear selected status bits now, so we
444 * don't miss them next time around.
445 */
446 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
447 }
448
449 /*
450 * Deal with any receive errors. The any of the lowest
451 * 8 bytes in the FIFO may contain an error. We must read
452 * them one by one. The "error" could even be the end of
453 * packet!
454 */
455 if (Ser2HSSR0 & HSSR0_EIF)
456 sa1100_irda_fir_error(si, dev);
457
458 /*
459 * No matter what happens, we must restart reception.
460 */
461 sa1100_irda_rx_dma_start(si);
462}
463
David Howells7d12e782006-10-05 14:55:46 +0100464static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct net_device *dev = dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800467 if (IS_FIR(((struct sa1100_irda *)netdev_priv(dev))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 sa1100_irda_fir_irq(dev);
469 else
470 sa1100_irda_hpsir_irq(dev);
471 return IRQ_HANDLED;
472}
473
474/*
475 * TX DMA completion handler.
476 */
477static void sa1100_irda_txdma_irq(void *id)
478{
479 struct net_device *dev = id;
Wang Chen4cf16532008-11-12 23:38:14 -0800480 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000481 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 /*
484 * Wait for the transmission to complete. Unfortunately,
485 * the hardware doesn't give us an interrupt to indicate
486 * "end of frame".
487 */
488 do
489 rmb();
490 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
491
492 /*
493 * Clear the transmit underrun bit.
494 */
495 Ser2HSSR0 = HSSR0_TUR;
496
497 /*
498 * Do we need to change speed? Note that we're lazy
Russell King885767c2012-01-08 12:53:22 +0000499 * here - we don't free the old dma_rx.skb. We don't need
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 * to allocate a buffer either.
501 */
Russell King0e888ee2012-01-08 16:30:44 +0000502 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 /*
505 * Start reception. This disables the transmitter for
506 * us. This will be using the existing RX buffer.
507 */
508 sa1100_irda_rx_dma_start(si);
509
Russell Kingba845252012-01-08 15:38:15 +0000510 /* Account and free the packet. */
511 skb = si->dma_tx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (skb) {
Russell Kingba845252012-01-08 15:38:15 +0000513 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
514 DMA_TO_DEVICE);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800515 dev->stats.tx_packets ++;
516 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 dev_kfree_skb_irq(skb);
Russell Kingba845252012-01-08 15:38:15 +0000518 si->dma_tx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 /*
522 * Make sure that the TX queue is available for sending
523 * (for retries). TX has priority over RX at all times.
524 */
525 netif_wake_queue(dev);
526}
527
528static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
529{
Wang Chen4cf16532008-11-12 23:38:14 -0800530 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int speed = irda_get_next_speed(skb);
532
533 /*
534 * Does this packet contain a request to change the interface
535 * speed? If so, remember it until we complete the transmission
536 * of this frame.
537 */
538 if (speed != si->speed && speed != -1)
539 si->newspeed = speed;
540
541 /*
542 * If this is an empty frame, we can bypass a lot.
543 */
544 if (skb->len == 0) {
Russell King0e888ee2012-01-08 16:30:44 +0000545 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000547 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 if (!IS_FIR(si)) {
551 netif_stop_queue(dev);
552
553 si->tx_buff.data = si->tx_buff.head;
554 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
555 si->tx_buff.truesize);
556
557 /*
558 * Set the transmit interrupt enable. This will fire
559 * off an interrupt immediately. Note that we disable
560 * the receiver so we won't get spurious characteres
561 * received.
562 */
563 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
564
565 dev_kfree_skb(skb);
566 } else {
567 int mtt = irda_get_mtt(skb);
568
569 /*
570 * We must not be transmitting...
571 */
Russell King885767c2012-01-08 12:53:22 +0000572 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 netif_stop_queue(dev);
575
Russell King885767c2012-01-08 12:53:22 +0000576 si->dma_tx.skb = skb;
577 si->dma_tx.dma = dma_map_single(si->dev, skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 skb->len, DMA_TO_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000579 if (dma_mapping_error(si->dev, si->dma_tx.dma)) {
580 si->dma_tx.skb = NULL;
Russell King22f0bf92012-01-08 13:55:23 +0000581 netif_wake_queue(dev);
582 dev->stats.tx_dropped++;
583 dev_kfree_skb(skb);
584 return NETDEV_TX_OK;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Russell King885767c2012-01-08 12:53:22 +0000587 sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /*
590 * If we have a mean turn-around time, impose the specified
591 * specified delay. We could shorten this by timing from
592 * the point we received the packet.
593 */
594 if (mtt)
595 udelay(mtt);
596
597 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
598 }
599
Patrick McHardy6ed10652009-06-23 06:03:08 +0000600 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603static int
604sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
605{
606 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800607 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int ret = -EOPNOTSUPP;
609
610 switch (cmd) {
611 case SIOCSBANDWIDTH:
612 if (capable(CAP_NET_ADMIN)) {
613 /*
614 * We are unable to set the speed if the
615 * device is not running.
616 */
617 if (si->open) {
618 ret = sa1100_irda_set_speed(si,
619 rq->ifr_baudrate);
620 } else {
621 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
622 ret = 0;
623 }
624 }
625 break;
626
627 case SIOCSMEDIABUSY:
628 ret = -EPERM;
629 if (capable(CAP_NET_ADMIN)) {
630 irda_device_set_media_busy(dev, TRUE);
631 ret = 0;
632 }
633 break;
634
635 case SIOCGRECEIVING:
636 rq->ifr_receiving = IS_FIR(si) ? 0
637 : si->rx_buff.state != OUTSIDE_FRAME;
638 break;
639
640 default:
641 break;
642 }
643
644 return ret;
645}
646
Russell Kingcbe1d242012-01-08 16:40:07 +0000647static int sa1100_irda_startup(struct sa1100_irda *si)
648{
649 int ret;
650
651 /*
652 * Ensure that the ports for this device are setup correctly.
653 */
654 if (si->pdata->startup) {
655 ret = si->pdata->startup(si->dev);
656 if (ret)
657 return ret;
658 }
659
660 /*
661 * Configure PPC for IRDA - we want to drive TXD2 low.
662 * We also want to drive this pin low during sleep.
663 */
664 PPSR &= ~PPC_TXD2;
665 PSDR &= ~PPC_TXD2;
666 PPDR |= PPC_TXD2;
667
668 /*
669 * Enable HP-SIR modulation, and ensure that the port is disabled.
670 */
671 Ser2UTCR3 = 0;
672 Ser2HSCR0 = HSCR0_UART;
673 Ser2UTCR4 = si->utcr4;
674 Ser2UTCR0 = UTCR0_8BitData;
675 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
676
677 /*
678 * Clear status register
679 */
680 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
681
682 ret = sa1100_irda_set_speed(si, si->speed = 9600);
683 if (ret) {
684 Ser2UTCR3 = 0;
685 Ser2HSCR0 = 0;
686
687 if (si->pdata->shutdown)
688 si->pdata->shutdown(si->dev);
689 }
690
691 return ret;
692}
693
694static void sa1100_irda_shutdown(struct sa1100_irda *si)
695{
696 /*
697 * Stop all DMA activity.
698 */
699 sa1100_stop_dma(si->dma_rx.regs);
700 sa1100_stop_dma(si->dma_tx.regs);
701
702 /* Disable the port. */
703 Ser2UTCR3 = 0;
704 Ser2HSCR0 = 0;
705
706 if (si->pdata->shutdown)
707 si->pdata->shutdown(si->dev);
708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710static int sa1100_irda_start(struct net_device *dev)
711{
Wang Chen4cf16532008-11-12 23:38:14 -0800712 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int err;
714
715 si->speed = 9600;
716
717 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
718 if (err)
719 goto err_irq;
720
721 err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive",
Russell King885767c2012-01-08 12:53:22 +0000722 NULL, NULL, &si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (err)
724 goto err_rx_dma;
725
726 err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit",
Russell King885767c2012-01-08 12:53:22 +0000727 sa1100_irda_txdma_irq, dev, &si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (err)
729 goto err_tx_dma;
730
731 /*
732 * The interrupt must remain disabled for now.
733 */
734 disable_irq(dev->irq);
735
736 /*
737 * Setup the serial port for the specified speed.
738 */
739 err = sa1100_irda_startup(si);
740 if (err)
741 goto err_startup;
742
743 /*
744 * Open a new IrLAP layer instance.
745 */
746 si->irlap = irlap_open(dev, &si->qos, "sa1100");
747 err = -ENOMEM;
748 if (!si->irlap)
749 goto err_irlap;
750
751 /*
752 * Now enable the interrupt and start the queue
753 */
754 si->open = 1;
755 sa1100_set_power(si, power_level); /* low power mode */
756 enable_irq(dev->irq);
757 netif_start_queue(dev);
758 return 0;
759
760err_irlap:
761 si->open = 0;
762 sa1100_irda_shutdown(si);
763err_startup:
Russell King885767c2012-01-08 12:53:22 +0000764 sa1100_free_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765err_tx_dma:
Russell King885767c2012-01-08 12:53:22 +0000766 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767err_rx_dma:
768 free_irq(dev->irq, dev);
769err_irq:
770 return err;
771}
772
773static int sa1100_irda_stop(struct net_device *dev)
774{
Wang Chen4cf16532008-11-12 23:38:14 -0800775 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000776 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 disable_irq(dev->irq);
779 sa1100_irda_shutdown(si);
780
781 /*
Russell Kingba845252012-01-08 15:38:15 +0000782 * If we have been doing any DMA activity, make sure we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 * tidy that up cleanly.
784 */
Russell Kingba845252012-01-08 15:38:15 +0000785 skb = si->dma_rx.skb;
786 if (skb) {
Russell King885767c2012-01-08 12:53:22 +0000787 dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 DMA_FROM_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000789 dev_kfree_skb(skb);
Russell King885767c2012-01-08 12:53:22 +0000790 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
Russell Kingba845252012-01-08 15:38:15 +0000793 skb = si->dma_tx.skb;
794 if (skb) {
795 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
796 DMA_TO_DEVICE);
797 dev_kfree_skb(skb);
798 si->dma_tx.skb = NULL;
799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /* Stop IrLAP */
802 if (si->irlap) {
803 irlap_close(si->irlap);
804 si->irlap = NULL;
805 }
806
807 netif_stop_queue(dev);
808 si->open = 0;
809
810 /*
811 * Free resources
812 */
Russell King885767c2012-01-08 12:53:22 +0000813 sa1100_free_dma(si->dma_tx.regs);
814 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 free_irq(dev->irq, dev);
816
817 sa1100_set_power(si, 0);
818
819 return 0;
820}
821
822static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
823{
824 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
825 if (io->head != NULL) {
826 io->truesize = size;
827 io->in_frame = FALSE;
828 io->state = OUTSIDE_FRAME;
829 io->data = io->head;
830 }
831 return io->head ? 0 : -ENOMEM;
832}
833
Alexander Beregalova1de9662009-04-15 12:52:42 +0000834static const struct net_device_ops sa1100_irda_netdev_ops = {
835 .ndo_open = sa1100_irda_start,
836 .ndo_stop = sa1100_irda_stop,
837 .ndo_start_xmit = sa1100_irda_hard_xmit,
838 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000839};
840
Russell King3ae5eae2005-11-09 22:32:44 +0000841static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct net_device *dev;
844 struct sa1100_irda *si;
845 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000846 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (!pdev->dev.platform_data)
849 return -EINVAL;
850
Russell Kinge556fdb2012-01-08 12:02:17 +0000851 irq = platform_get_irq(pdev, 0);
852 if (irq <= 0)
853 return irq < 0 ? irq : -ENXIO;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
856 if (err)
857 goto err_mem_1;
858 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
859 if (err)
860 goto err_mem_2;
861 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
862 if (err)
863 goto err_mem_3;
864
865 dev = alloc_irdadev(sizeof(struct sa1100_irda));
866 if (!dev)
867 goto err_mem_4;
868
Russell Kingd3238602012-01-08 12:07:24 +0000869 SET_NETDEV_DEV(dev, &pdev->dev);
870
Wang Chen4cf16532008-11-12 23:38:14 -0800871 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 si->dev = &pdev->dev;
873 si->pdata = pdev->dev.platform_data;
874
875 /*
876 * Initialise the HP-SIR buffers
877 */
878 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
879 if (err)
880 goto err_mem_5;
881 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
882 if (err)
883 goto err_mem_5;
884
Alexander Beregalova1de9662009-04-15 12:52:42 +0000885 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000886 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 irda_init_max_qos_capabilies(&si->qos);
889
890 /*
891 * We support original IRDA up to 115k2. (we don't currently
892 * support 4Mbps). Min Turn Time set to 1ms or greater.
893 */
894 baudrate_mask = IR_9600;
895
896 switch (max_rate) {
897 case 4000000: baudrate_mask |= IR_4000000 << 8;
898 case 115200: baudrate_mask |= IR_115200;
899 case 57600: baudrate_mask |= IR_57600;
900 case 38400: baudrate_mask |= IR_38400;
901 case 19200: baudrate_mask |= IR_19200;
902 }
903
904 si->qos.baud_rate.bits &= baudrate_mask;
905 si->qos.min_turn_time.bits = 7;
906
907 irda_qos_bits_to_value(&si->qos);
908
909 si->utcr4 = UTCR4_HPSIR;
910 if (tx_lpm)
911 si->utcr4 |= UTCR4_Z1_6us;
912
913 /*
914 * Initially enable HP-SIR modulation, and ensure that the port
915 * is disabled.
916 */
917 Ser2UTCR3 = 0;
918 Ser2UTCR4 = si->utcr4;
919 Ser2HSCR0 = HSCR0_UART;
920
921 err = register_netdev(dev);
922 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000923 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (err) {
926 err_mem_5:
927 kfree(si->tx_buff.head);
928 kfree(si->rx_buff.head);
929 free_netdev(dev);
930 err_mem_4:
931 release_mem_region(__PREG(Ser2HSCR2), 0x04);
932 err_mem_3:
933 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
934 err_mem_2:
935 release_mem_region(__PREG(Ser2UTCR0), 0x24);
936 }
937 err_mem_1:
938 return err;
939}
940
Russell King3ae5eae2005-11-09 22:32:44 +0000941static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Russell King3ae5eae2005-11-09 22:32:44 +0000943 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -0800946 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 unregister_netdev(dev);
948 kfree(si->tx_buff.head);
949 kfree(si->rx_buff.head);
950 free_netdev(dev);
951 }
952
953 release_mem_region(__PREG(Ser2HSCR2), 0x04);
954 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
955 release_mem_region(__PREG(Ser2UTCR0), 0x24);
956
957 return 0;
958}
959
Russell Kingcbe1d242012-01-08 16:40:07 +0000960#ifdef CONFIG_PM
961/*
962 * Suspend the IrDA interface.
963 */
964static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
965{
966 struct net_device *dev = platform_get_drvdata(pdev);
967 struct sa1100_irda *si;
968
969 if (!dev)
970 return 0;
971
972 si = netdev_priv(dev);
973 if (si->open) {
974 /*
975 * Stop the transmit queue
976 */
977 netif_device_detach(dev);
978 disable_irq(dev->irq);
979 sa1100_irda_shutdown(si);
980 __sa1100_irda_set_power(si, 0);
981 }
982
983 return 0;
984}
985
986/*
987 * Resume the IrDA interface.
988 */
989static int sa1100_irda_resume(struct platform_device *pdev)
990{
991 struct net_device *dev = platform_get_drvdata(pdev);
992 struct sa1100_irda *si;
993
994 if (!dev)
995 return 0;
996
997 si = netdev_priv(dev);
998 if (si->open) {
999 /*
1000 * If we missed a speed change, initialise at the new speed
1001 * directly. It is debatable whether this is actually
1002 * required, but in the interests of continuing from where
1003 * we left off it is desirable. The converse argument is
1004 * that we should re-negotiate at 9600 baud again.
1005 */
1006 if (si->newspeed) {
1007 si->speed = si->newspeed;
1008 si->newspeed = 0;
1009 }
1010
1011 sa1100_irda_startup(si);
1012 __sa1100_irda_set_power(si, si->power);
1013 enable_irq(dev->irq);
1014
1015 /*
1016 * This automatically wakes up the queue
1017 */
1018 netif_device_attach(dev);
1019 }
1020
1021 return 0;
1022}
1023#else
1024#define sa1100_irda_suspend NULL
1025#define sa1100_irda_resume NULL
1026#endif
1027
Russell King3ae5eae2005-11-09 22:32:44 +00001028static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 .probe = sa1100_irda_probe,
1030 .remove = sa1100_irda_remove,
1031 .suspend = sa1100_irda_suspend,
1032 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001033 .driver = {
1034 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001035 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001036 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037};
1038
1039static int __init sa1100_irda_init(void)
1040{
1041 /*
1042 * Limit power level a sensible range.
1043 */
1044 if (power_level < 1)
1045 power_level = 1;
1046 if (power_level > 3)
1047 power_level = 3;
1048
Russell King3ae5eae2005-11-09 22:32:44 +00001049 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052static void __exit sa1100_irda_exit(void)
1053{
Russell King3ae5eae2005-11-09 22:32:44 +00001054 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
1057module_init(sa1100_irda_init);
1058module_exit(sa1100_irda_exit);
1059module_param(power_level, int, 0);
1060module_param(tx_lpm, int, 0);
1061module_param(max_rate, int, 0);
1062
1063MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1064MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1065MODULE_LICENSE("GPL");
1066MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1067MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1068MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001069MODULE_ALIAS("platform:sa11x0-ir");