blob: 32dee3322679f5a725ab6a723ee16811b7c10816 [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 *);
Russell King374f7732012-01-08 16:26:15 +000073 irqreturn_t (*irq)(struct net_device *, struct sa1100_irda *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
Russell King0e888ee2012-01-08 16:30:44 +000076static int sa1100_irda_set_speed(struct sa1100_irda *, int);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define IS_FIR(si) ((si)->speed >= 4000000)
79
80#define HPSIR_MAX_RXLEN 2047
81
82/*
83 * Allocate and map the receive buffer, unless it is already allocated.
84 */
85static int sa1100_irda_rx_alloc(struct sa1100_irda *si)
86{
Russell King885767c2012-01-08 12:53:22 +000087 if (si->dma_rx.skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
89
Russell King885767c2012-01-08 12:53:22 +000090 si->dma_rx.skb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);
91 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");
93 return -ENOMEM;
94 }
95
96 /*
97 * Align any IP headers that may be contained
98 * within the frame.
99 */
Russell King885767c2012-01-08 12:53:22 +0000100 skb_reserve(si->dma_rx.skb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Russell King885767c2012-01-08 12:53:22 +0000102 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 HPSIR_MAX_RXLEN,
104 DMA_FROM_DEVICE);
Russell King885767c2012-01-08 12:53:22 +0000105 if (dma_mapping_error(si->dev, si->dma_rx.dma)) {
106 dev_kfree_skb_any(si->dma_rx.skb);
Russell King22f0bf92012-01-08 13:55:23 +0000107 return -ENOMEM;
108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111}
112
113/*
114 * We want to get here as soon as possible, and get the receiver setup.
115 * We use the existing buffer.
116 */
117static void sa1100_irda_rx_dma_start(struct sa1100_irda *si)
118{
Russell King885767c2012-01-08 12:53:22 +0000119 if (!si->dma_rx.skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");
121 return;
122 }
123
124 /*
125 * First empty receive FIFO
126 */
127 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
128
129 /*
130 * Enable the DMA, receiver and receive interrupt.
131 */
Russell King885767c2012-01-08 12:53:22 +0000132 sa1100_clear_dma(si->dma_rx.regs);
133 sa1100_start_dma(si->dma_rx.regs, si->dma_rx.dma, HPSIR_MAX_RXLEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;
135}
136
Russell King0e888ee2012-01-08 16:30:44 +0000137static void sa1100_irda_check_speed(struct sa1100_irda *si)
138{
139 if (si->newspeed) {
140 sa1100_irda_set_speed(si, si->newspeed);
141 si->newspeed = 0;
142 }
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
Russell King3d26db12012-01-08 16:16:39 +0000146 * HP-SIR format support.
147 */
148static int sa1100_irda_sir_tx_start(struct sk_buff *skb, struct net_device *dev,
149 struct sa1100_irda *si)
150{
151 si->tx_buff.data = si->tx_buff.head;
152 si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data,
153 si->tx_buff.truesize);
154
155 /*
156 * Set the transmit interrupt enable. This will fire off an
157 * interrupt immediately. Note that we disable the receiver
158 * so we won't get spurious characters received.
159 */
160 Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE;
161
162 dev_kfree_skb(skb);
163
164 return NETDEV_TX_OK;
165}
166
167/*
168 * FIR format support.
169 */
170static int sa1100_irda_fir_tx_start(struct sk_buff *skb, struct net_device *dev,
171 struct sa1100_irda *si)
172{
173 int mtt = irda_get_mtt(skb);
174
175 si->dma_tx.skb = skb;
176 si->dma_tx.dma = dma_map_single(si->dev, skb->data, skb->len,
177 DMA_TO_DEVICE);
178 if (dma_mapping_error(si->dev, si->dma_tx.dma)) {
179 si->dma_tx.skb = NULL;
180 netif_wake_queue(dev);
181 dev->stats.tx_dropped++;
182 dev_kfree_skb(skb);
183 return NETDEV_TX_OK;
184 }
185
186 sa1100_start_dma(si->dma_tx.regs, si->dma_tx.dma, skb->len);
187
188 /*
189 * If we have a mean turn-around time, impose the specified
190 * specified delay. We could shorten this by timing from
191 * the point we received the packet.
192 */
193 if (mtt)
194 udelay(mtt);
195
196 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE;
197
198 return NETDEV_TX_OK;
199}
200
Russell King374f7732012-01-08 16:26:15 +0000201static irqreturn_t sa1100_irda_sir_irq(struct net_device *, struct sa1100_irda *);
202static irqreturn_t sa1100_irda_fir_irq(struct net_device *, struct sa1100_irda *);
Russell King3d26db12012-01-08 16:16:39 +0000203
204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * Set the IrDA communications speed.
206 */
207static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed)
208{
209 unsigned long flags;
210 int brd, ret = -EINVAL;
211
212 switch (speed) {
213 case 9600: case 19200: case 38400:
214 case 57600: case 115200:
215 brd = 3686400 / (16 * speed) - 1;
216
217 /*
218 * Stop the receive DMA.
219 */
220 if (IS_FIR(si))
Russell King885767c2012-01-08 12:53:22 +0000221 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 local_irq_save(flags);
224
225 Ser2UTCR3 = 0;
226 Ser2HSCR0 = HSCR0_UART;
227
228 Ser2UTCR1 = brd >> 8;
229 Ser2UTCR2 = brd;
230
231 /*
232 * Clear status register
233 */
234 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
235 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
236
237 if (si->pdata->set_speed)
238 si->pdata->set_speed(si->dev, speed);
239
240 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000241 si->tx_start = sa1100_irda_sir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000242 si->irq = sa1100_irda_sir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 local_irq_restore(flags);
245 ret = 0;
246 break;
247
248 case 4000000:
249 local_irq_save(flags);
250
251 si->hscr0 = 0;
252
253 Ser2HSSR0 = 0xff;
254 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
255 Ser2UTCR3 = 0;
256
257 si->speed = speed;
Russell King3d26db12012-01-08 16:16:39 +0000258 si->tx_start = sa1100_irda_fir_tx_start;
Russell King374f7732012-01-08 16:26:15 +0000259 si->irq = sa1100_irda_fir_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 if (si->pdata->set_speed)
262 si->pdata->set_speed(si->dev, speed);
263
264 sa1100_irda_rx_alloc(si);
265 sa1100_irda_rx_dma_start(si);
266
267 local_irq_restore(flags);
268
269 break;
270
271 default:
272 break;
273 }
274
275 return ret;
276}
277
278/*
279 * Control the power state of the IrDA transmitter.
280 * State:
281 * 0 - off
282 * 1 - short range, lowest power
283 * 2 - medium range, medium power
284 * 3 - maximum range, high power
285 *
286 * Currently, only assabet is known to support this.
287 */
288static int
289__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state)
290{
291 int ret = 0;
292 if (si->pdata->set_power)
293 ret = si->pdata->set_power(si->dev, state);
294 return ret;
295}
296
297static inline int
298sa1100_set_power(struct sa1100_irda *si, unsigned int state)
299{
300 int ret;
301
302 ret = __sa1100_irda_set_power(si, state);
303 if (ret == 0)
304 si->power = state;
305
306 return ret;
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
310 * HP-SIR format interrupt service routines.
311 */
Russell King374f7732012-01-08 16:26:15 +0000312static irqreturn_t sa1100_irda_sir_irq(struct net_device *dev, struct sa1100_irda *si)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int status;
315
316 status = Ser2UTSR0;
317
318 /*
319 * Deal with any receive errors first. The bytes in error may be
320 * the only bytes in the receive FIFO, so we do this first.
321 */
322 while (status & UTSR0_EIF) {
323 int stat, data;
324
325 stat = Ser2UTSR1;
326 data = Ser2UTDR;
327
328 if (stat & (UTSR1_FRE | UTSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800329 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (stat & UTSR1_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800331 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (stat & UTSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800333 dev->stats.rx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } else
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800335 async_unwrap_char(dev, &dev->stats, &si->rx_buff, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 status = Ser2UTSR0;
338 }
339
340 /*
341 * We must clear certain bits.
342 */
343 Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
344
345 if (status & UTSR0_RFS) {
346 /*
347 * There are at least 4 bytes in the FIFO. Read 3 bytes
348 * and leave the rest to the block below.
349 */
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800350 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
351 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
352 async_unwrap_char(dev, &dev->stats, &si->rx_buff, Ser2UTDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
355 if (status & (UTSR0_RFS | UTSR0_RID)) {
356 /*
357 * Fifo contains more than 1 character.
358 */
359 do {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800360 async_unwrap_char(dev, &dev->stats, &si->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 Ser2UTDR);
362 } while (Ser2UTSR1 & UTSR1_RNE);
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 if (status & UTSR0_TFS && si->tx_buff.len) {
367 /*
368 * Transmitter FIFO is not full
369 */
370 do {
371 Ser2UTDR = *si->tx_buff.data++;
372 si->tx_buff.len -= 1;
373 } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);
374
375 if (si->tx_buff.len == 0) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800376 dev->stats.tx_packets++;
377 dev->stats.tx_bytes += si->tx_buff.data -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 si->tx_buff.head;
379
380 /*
381 * We need to ensure that the transmitter has
382 * finished.
383 */
384 do
385 rmb();
386 while (Ser2UTSR1 & UTSR1_TBY);
387
388 /*
389 * Ok, we've finished transmitting. Now enable
390 * the receiver. Sometimes we get a receive IRQ
391 * immediately after a transmit...
392 */
393 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
394 Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;
395
Russell King0e888ee2012-01-08 16:30:44 +0000396 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /* I'm hungry! */
399 netif_wake_queue(dev);
400 }
401 }
Russell King374f7732012-01-08 16:26:15 +0000402
403 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev)
407{
Russell King885767c2012-01-08 12:53:22 +0000408 struct sk_buff *skb = si->dma_rx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 dma_addr_t dma_addr;
410 unsigned int len, stat, data;
411
412 if (!skb) {
413 printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");
414 return;
415 }
416
417 /*
418 * Get the current data position.
419 */
Russell King885767c2012-01-08 12:53:22 +0000420 dma_addr = sa1100_get_dma_pos(si->dma_rx.regs);
421 len = dma_addr - si->dma_rx.dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (len > HPSIR_MAX_RXLEN)
423 len = HPSIR_MAX_RXLEN;
Russell King885767c2012-01-08 12:53:22 +0000424 dma_unmap_single(si->dev, si->dma_rx.dma, len, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 do {
427 /*
428 * Read Status, and then Data.
429 */
430 stat = Ser2HSSR1;
431 rmb();
432 data = Ser2HSDR;
433
434 if (stat & (HSSR1_CRE | HSSR1_ROR)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800435 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (stat & HSSR1_CRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800437 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (stat & HSSR1_ROR)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800439 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 } else
441 skb->data[len++] = data;
442
443 /*
444 * If we hit the end of frame, there's
445 * no point in continuing.
446 */
447 if (stat & HSSR1_EOF)
448 break;
449 } while (Ser2HSSR0 & HSSR0_EIF);
450
451 if (stat & HSSR1_EOF) {
Russell King885767c2012-01-08 12:53:22 +0000452 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 skb_put(skb, len);
455 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700456 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 skb->protocol = htons(ETH_P_IRDA);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800458 dev->stats.rx_packets++;
459 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 /*
462 * Before we pass the buffer up, allocate a new one.
463 */
464 sa1100_irda_rx_alloc(si);
465
466 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 } else {
468 /*
Russell King22f0bf92012-01-08 13:55:23 +0000469 * Remap the buffer - it was previously mapped, and we
470 * hope that this succeeds.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
Russell King885767c2012-01-08 12:53:22 +0000472 si->dma_rx.dma = dma_map_single(si->dev, si->dma_rx.skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 HPSIR_MAX_RXLEN,
474 DMA_FROM_DEVICE);
475 }
476}
477
478/*
479 * FIR format interrupt service routine. We only have to
480 * handle RX events; transmit events go via the TX DMA handler.
481 *
482 * No matter what, we disable RX, process, and the restart RX.
483 */
Russell King374f7732012-01-08 16:26:15 +0000484static irqreturn_t sa1100_irda_fir_irq(struct net_device *dev, struct sa1100_irda *si)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /*
487 * Stop RX DMA
488 */
Russell King885767c2012-01-08 12:53:22 +0000489 sa1100_stop_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /*
492 * Framing error - we throw away the packet completely.
493 * Clearing RXE flushes the error conditions and data
494 * from the fifo.
495 */
496 if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800497 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (Ser2HSSR0 & HSSR0_FRE)
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800500 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /*
503 * Clear out the DMA...
504 */
505 Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;
506
507 /*
508 * Clear selected status bits now, so we
509 * don't miss them next time around.
510 */
511 Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB;
512 }
513
514 /*
515 * Deal with any receive errors. The any of the lowest
516 * 8 bytes in the FIFO may contain an error. We must read
517 * them one by one. The "error" could even be the end of
518 * packet!
519 */
520 if (Ser2HSSR0 & HSSR0_EIF)
521 sa1100_irda_fir_error(si, dev);
522
523 /*
524 * No matter what happens, we must restart reception.
525 */
526 sa1100_irda_rx_dma_start(si);
Russell King374f7732012-01-08 16:26:15 +0000527
528 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
David Howells7d12e782006-10-05 14:55:46 +0100531static irqreturn_t sa1100_irda_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct net_device *dev = dev_id;
Russell King374f7732012-01-08 16:26:15 +0000534 struct sa1100_irda *si = netdev_priv(dev);
535
536 return si->irq(dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
539/*
540 * TX DMA completion handler.
541 */
542static void sa1100_irda_txdma_irq(void *id)
543{
544 struct net_device *dev = id;
Wang Chen4cf16532008-11-12 23:38:14 -0800545 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000546 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /*
549 * Wait for the transmission to complete. Unfortunately,
550 * the hardware doesn't give us an interrupt to indicate
551 * "end of frame".
552 */
553 do
554 rmb();
555 while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY);
556
557 /*
558 * Clear the transmit underrun bit.
559 */
560 Ser2HSSR0 = HSSR0_TUR;
561
562 /*
563 * Do we need to change speed? Note that we're lazy
Russell King885767c2012-01-08 12:53:22 +0000564 * here - we don't free the old dma_rx.skb. We don't need
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * to allocate a buffer either.
566 */
Russell King0e888ee2012-01-08 16:30:44 +0000567 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 /*
570 * Start reception. This disables the transmitter for
571 * us. This will be using the existing RX buffer.
572 */
573 sa1100_irda_rx_dma_start(si);
574
Russell Kingba845252012-01-08 15:38:15 +0000575 /* Account and free the packet. */
576 skb = si->dma_tx.skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (skb) {
Russell Kingba845252012-01-08 15:38:15 +0000578 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
579 DMA_TO_DEVICE);
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800580 dev->stats.tx_packets ++;
581 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 dev_kfree_skb_irq(skb);
Russell Kingba845252012-01-08 15:38:15 +0000583 si->dma_tx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585
586 /*
587 * Make sure that the TX queue is available for sending
588 * (for retries). TX has priority over RX at all times.
589 */
590 netif_wake_queue(dev);
591}
592
593static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
594{
Wang Chen4cf16532008-11-12 23:38:14 -0800595 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 int speed = irda_get_next_speed(skb);
597
598 /*
599 * Does this packet contain a request to change the interface
600 * speed? If so, remember it until we complete the transmission
601 * of this frame.
602 */
603 if (speed != si->speed && speed != -1)
604 si->newspeed = speed;
605
Russell King3d26db12012-01-08 16:16:39 +0000606 /* If this is an empty frame, we can bypass a lot. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (skb->len == 0) {
Russell King0e888ee2012-01-08 16:30:44 +0000608 sa1100_irda_check_speed(si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000610 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Russell King3d26db12012-01-08 16:16:39 +0000613 netif_stop_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Russell King3d26db12012-01-08 16:16:39 +0000615 /* We must not already have a skb to transmit... */
616 BUG_ON(si->dma_tx.skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Russell King3d26db12012-01-08 16:16:39 +0000618 return si->tx_start(skb, dev, si);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621static int
622sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
623{
624 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
Wang Chen4cf16532008-11-12 23:38:14 -0800625 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 int ret = -EOPNOTSUPP;
627
628 switch (cmd) {
629 case SIOCSBANDWIDTH:
630 if (capable(CAP_NET_ADMIN)) {
631 /*
632 * We are unable to set the speed if the
633 * device is not running.
634 */
635 if (si->open) {
636 ret = sa1100_irda_set_speed(si,
637 rq->ifr_baudrate);
638 } else {
639 printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n");
640 ret = 0;
641 }
642 }
643 break;
644
645 case SIOCSMEDIABUSY:
646 ret = -EPERM;
647 if (capable(CAP_NET_ADMIN)) {
648 irda_device_set_media_busy(dev, TRUE);
649 ret = 0;
650 }
651 break;
652
653 case SIOCGRECEIVING:
654 rq->ifr_receiving = IS_FIR(si) ? 0
655 : si->rx_buff.state != OUTSIDE_FRAME;
656 break;
657
658 default:
659 break;
660 }
661
662 return ret;
663}
664
Russell Kingcbe1d242012-01-08 16:40:07 +0000665static int sa1100_irda_startup(struct sa1100_irda *si)
666{
667 int ret;
668
669 /*
670 * Ensure that the ports for this device are setup correctly.
671 */
672 if (si->pdata->startup) {
673 ret = si->pdata->startup(si->dev);
674 if (ret)
675 return ret;
676 }
677
678 /*
679 * Configure PPC for IRDA - we want to drive TXD2 low.
680 * We also want to drive this pin low during sleep.
681 */
682 PPSR &= ~PPC_TXD2;
683 PSDR &= ~PPC_TXD2;
684 PPDR |= PPC_TXD2;
685
686 /*
687 * Enable HP-SIR modulation, and ensure that the port is disabled.
688 */
689 Ser2UTCR3 = 0;
690 Ser2HSCR0 = HSCR0_UART;
691 Ser2UTCR4 = si->utcr4;
692 Ser2UTCR0 = UTCR0_8BitData;
693 Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;
694
695 /*
696 * Clear status register
697 */
698 Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;
699
700 ret = sa1100_irda_set_speed(si, si->speed = 9600);
701 if (ret) {
702 Ser2UTCR3 = 0;
703 Ser2HSCR0 = 0;
704
705 if (si->pdata->shutdown)
706 si->pdata->shutdown(si->dev);
707 }
708
709 return ret;
710}
711
712static void sa1100_irda_shutdown(struct sa1100_irda *si)
713{
714 /*
715 * Stop all DMA activity.
716 */
717 sa1100_stop_dma(si->dma_rx.regs);
718 sa1100_stop_dma(si->dma_tx.regs);
719
720 /* Disable the port. */
721 Ser2UTCR3 = 0;
722 Ser2HSCR0 = 0;
723
724 if (si->pdata->shutdown)
725 si->pdata->shutdown(si->dev);
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728static int sa1100_irda_start(struct net_device *dev)
729{
Wang Chen4cf16532008-11-12 23:38:14 -0800730 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 int err;
732
733 si->speed = 9600;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 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 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * Setup the serial port for the specified speed.
747 */
748 err = sa1100_irda_startup(si);
749 if (err)
750 goto err_startup;
751
752 /*
753 * Open a new IrLAP layer instance.
754 */
755 si->irlap = irlap_open(dev, &si->qos, "sa1100");
756 err = -ENOMEM;
757 if (!si->irlap)
758 goto err_irlap;
759
Russell King374f7732012-01-08 16:26:15 +0000760 err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev);
761 if (err)
762 goto err_irq;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /*
765 * Now enable the interrupt and start the queue
766 */
767 si->open = 1;
768 sa1100_set_power(si, power_level); /* low power mode */
Russell King374f7732012-01-08 16:26:15 +0000769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 netif_start_queue(dev);
771 return 0;
772
Russell King374f7732012-01-08 16:26:15 +0000773err_irq:
774 irlap_close(si->irlap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775err_irlap:
776 si->open = 0;
777 sa1100_irda_shutdown(si);
778err_startup:
Russell King885767c2012-01-08 12:53:22 +0000779 sa1100_free_dma(si->dma_tx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780err_tx_dma:
Russell King885767c2012-01-08 12:53:22 +0000781 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782err_rx_dma:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return err;
784}
785
786static int sa1100_irda_stop(struct net_device *dev)
787{
Wang Chen4cf16532008-11-12 23:38:14 -0800788 struct sa1100_irda *si = netdev_priv(dev);
Russell Kingba845252012-01-08 15:38:15 +0000789 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Russell King374f7732012-01-08 16:26:15 +0000791 netif_stop_queue(dev);
792
793 si->open = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 sa1100_irda_shutdown(si);
795
796 /*
Russell Kingba845252012-01-08 15:38:15 +0000797 * If we have been doing any DMA activity, make sure we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 * tidy that up cleanly.
799 */
Russell Kingba845252012-01-08 15:38:15 +0000800 skb = si->dma_rx.skb;
801 if (skb) {
Russell King885767c2012-01-08 12:53:22 +0000802 dma_unmap_single(si->dev, si->dma_rx.dma, HPSIR_MAX_RXLEN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 DMA_FROM_DEVICE);
Russell Kingba845252012-01-08 15:38:15 +0000804 dev_kfree_skb(skb);
Russell King885767c2012-01-08 12:53:22 +0000805 si->dma_rx.skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
Russell Kingba845252012-01-08 15:38:15 +0000808 skb = si->dma_tx.skb;
809 if (skb) {
810 dma_unmap_single(si->dev, si->dma_tx.dma, skb->len,
811 DMA_TO_DEVICE);
812 dev_kfree_skb(skb);
813 si->dma_tx.skb = NULL;
814 }
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* Stop IrLAP */
817 if (si->irlap) {
818 irlap_close(si->irlap);
819 si->irlap = NULL;
820 }
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /*
823 * Free resources
824 */
Russell King885767c2012-01-08 12:53:22 +0000825 sa1100_free_dma(si->dma_tx.regs);
826 sa1100_free_dma(si->dma_rx.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 free_irq(dev->irq, dev);
828
829 sa1100_set_power(si, 0);
830
831 return 0;
832}
833
834static int sa1100_irda_init_iobuf(iobuff_t *io, int size)
835{
836 io->head = kmalloc(size, GFP_KERNEL | GFP_DMA);
837 if (io->head != NULL) {
838 io->truesize = size;
839 io->in_frame = FALSE;
840 io->state = OUTSIDE_FRAME;
841 io->data = io->head;
842 }
843 return io->head ? 0 : -ENOMEM;
844}
845
Alexander Beregalova1de9662009-04-15 12:52:42 +0000846static const struct net_device_ops sa1100_irda_netdev_ops = {
847 .ndo_open = sa1100_irda_start,
848 .ndo_stop = sa1100_irda_stop,
849 .ndo_start_xmit = sa1100_irda_hard_xmit,
850 .ndo_do_ioctl = sa1100_irda_ioctl,
Alexander Beregalova1de9662009-04-15 12:52:42 +0000851};
852
Russell King3ae5eae2005-11-09 22:32:44 +0000853static int sa1100_irda_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct net_device *dev;
856 struct sa1100_irda *si;
857 unsigned int baudrate_mask;
Russell Kinge556fdb2012-01-08 12:02:17 +0000858 int err, irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (!pdev->dev.platform_data)
861 return -EINVAL;
862
Russell Kinge556fdb2012-01-08 12:02:17 +0000863 irq = platform_get_irq(pdev, 0);
864 if (irq <= 0)
865 return irq < 0 ? irq : -ENXIO;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY;
868 if (err)
869 goto err_mem_1;
870 err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY;
871 if (err)
872 goto err_mem_2;
873 err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY;
874 if (err)
875 goto err_mem_3;
876
877 dev = alloc_irdadev(sizeof(struct sa1100_irda));
878 if (!dev)
879 goto err_mem_4;
880
Russell Kingd3238602012-01-08 12:07:24 +0000881 SET_NETDEV_DEV(dev, &pdev->dev);
882
Wang Chen4cf16532008-11-12 23:38:14 -0800883 si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 si->dev = &pdev->dev;
885 si->pdata = pdev->dev.platform_data;
886
887 /*
888 * Initialise the HP-SIR buffers
889 */
890 err = sa1100_irda_init_iobuf(&si->rx_buff, 14384);
891 if (err)
892 goto err_mem_5;
893 err = sa1100_irda_init_iobuf(&si->tx_buff, 4000);
894 if (err)
895 goto err_mem_5;
896
Alexander Beregalova1de9662009-04-15 12:52:42 +0000897 dev->netdev_ops = &sa1100_irda_netdev_ops;
Russell Kinge556fdb2012-01-08 12:02:17 +0000898 dev->irq = irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 irda_init_max_qos_capabilies(&si->qos);
901
902 /*
903 * We support original IRDA up to 115k2. (we don't currently
904 * support 4Mbps). Min Turn Time set to 1ms or greater.
905 */
906 baudrate_mask = IR_9600;
907
908 switch (max_rate) {
909 case 4000000: baudrate_mask |= IR_4000000 << 8;
910 case 115200: baudrate_mask |= IR_115200;
911 case 57600: baudrate_mask |= IR_57600;
912 case 38400: baudrate_mask |= IR_38400;
913 case 19200: baudrate_mask |= IR_19200;
914 }
915
916 si->qos.baud_rate.bits &= baudrate_mask;
917 si->qos.min_turn_time.bits = 7;
918
919 irda_qos_bits_to_value(&si->qos);
920
921 si->utcr4 = UTCR4_HPSIR;
922 if (tx_lpm)
923 si->utcr4 |= UTCR4_Z1_6us;
924
925 /*
926 * Initially enable HP-SIR modulation, and ensure that the port
927 * is disabled.
928 */
929 Ser2UTCR3 = 0;
930 Ser2UTCR4 = si->utcr4;
931 Ser2HSCR0 = HSCR0_UART;
932
933 err = register_netdev(dev);
934 if (err == 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000935 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (err) {
938 err_mem_5:
939 kfree(si->tx_buff.head);
940 kfree(si->rx_buff.head);
941 free_netdev(dev);
942 err_mem_4:
943 release_mem_region(__PREG(Ser2HSCR2), 0x04);
944 err_mem_3:
945 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
946 err_mem_2:
947 release_mem_region(__PREG(Ser2UTCR0), 0x24);
948 }
949 err_mem_1:
950 return err;
951}
952
Russell King3ae5eae2005-11-09 22:32:44 +0000953static int sa1100_irda_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
Russell King3ae5eae2005-11-09 22:32:44 +0000955 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (dev) {
Wang Chen4cf16532008-11-12 23:38:14 -0800958 struct sa1100_irda *si = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 unregister_netdev(dev);
960 kfree(si->tx_buff.head);
961 kfree(si->rx_buff.head);
962 free_netdev(dev);
963 }
964
965 release_mem_region(__PREG(Ser2HSCR2), 0x04);
966 release_mem_region(__PREG(Ser2HSCR0), 0x1c);
967 release_mem_region(__PREG(Ser2UTCR0), 0x24);
968
969 return 0;
970}
971
Russell Kingcbe1d242012-01-08 16:40:07 +0000972#ifdef CONFIG_PM
973/*
974 * Suspend the IrDA interface.
975 */
976static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state)
977{
978 struct net_device *dev = platform_get_drvdata(pdev);
979 struct sa1100_irda *si;
980
981 if (!dev)
982 return 0;
983
984 si = netdev_priv(dev);
985 if (si->open) {
986 /*
987 * Stop the transmit queue
988 */
989 netif_device_detach(dev);
990 disable_irq(dev->irq);
991 sa1100_irda_shutdown(si);
992 __sa1100_irda_set_power(si, 0);
993 }
994
995 return 0;
996}
997
998/*
999 * Resume the IrDA interface.
1000 */
1001static int sa1100_irda_resume(struct platform_device *pdev)
1002{
1003 struct net_device *dev = platform_get_drvdata(pdev);
1004 struct sa1100_irda *si;
1005
1006 if (!dev)
1007 return 0;
1008
1009 si = netdev_priv(dev);
1010 if (si->open) {
1011 /*
1012 * If we missed a speed change, initialise at the new speed
1013 * directly. It is debatable whether this is actually
1014 * required, but in the interests of continuing from where
1015 * we left off it is desirable. The converse argument is
1016 * that we should re-negotiate at 9600 baud again.
1017 */
1018 if (si->newspeed) {
1019 si->speed = si->newspeed;
1020 si->newspeed = 0;
1021 }
1022
1023 sa1100_irda_startup(si);
1024 __sa1100_irda_set_power(si, si->power);
1025 enable_irq(dev->irq);
1026
1027 /*
1028 * This automatically wakes up the queue
1029 */
1030 netif_device_attach(dev);
1031 }
1032
1033 return 0;
1034}
1035#else
1036#define sa1100_irda_suspend NULL
1037#define sa1100_irda_resume NULL
1038#endif
1039
Russell King3ae5eae2005-11-09 22:32:44 +00001040static struct platform_driver sa1100ir_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 .probe = sa1100_irda_probe,
1042 .remove = sa1100_irda_remove,
1043 .suspend = sa1100_irda_suspend,
1044 .resume = sa1100_irda_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001045 .driver = {
1046 .name = "sa11x0-ir",
Kay Sievers72abb462008-04-18 13:50:44 -07001047 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001048 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049};
1050
1051static int __init sa1100_irda_init(void)
1052{
1053 /*
1054 * Limit power level a sensible range.
1055 */
1056 if (power_level < 1)
1057 power_level = 1;
1058 if (power_level > 3)
1059 power_level = 3;
1060
Russell King3ae5eae2005-11-09 22:32:44 +00001061 return platform_driver_register(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064static void __exit sa1100_irda_exit(void)
1065{
Russell King3ae5eae2005-11-09 22:32:44 +00001066 platform_driver_unregister(&sa1100ir_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069module_init(sa1100_irda_init);
1070module_exit(sa1100_irda_exit);
1071module_param(power_level, int, 0);
1072module_param(tx_lpm, int, 0);
1073module_param(max_rate, int, 0);
1074
1075MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
1076MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver");
1077MODULE_LICENSE("GPL");
1078MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)");
1079MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode");
1080MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)");
Kay Sievers72abb462008-04-18 13:50:44 -07001081MODULE_ALIAS("platform:sa11x0-ir");