Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6] / drivers / net / irda / au1k_ir.c
1 /*
2  * Alchemy Semi Au1000 IrDA driver
3  *
4  * Copyright 2001 MontaVista Software Inc.
5  * Author: MontaVista Software, Inc.
6  *              ppopov@mvista.com or source@mvista.com
7  *
8  *  This program is free software; you can distribute it and/or modify it
9  *  under the terms of the GNU General Public License (Version 2) as
10  *  published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope it will be useful, but WITHOUT
13  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  *  for more details.
16  *
17  *  You should have received a copy of the GNU General Public License along
18  *  with this program; if not, write to the Free Software Foundation, Inc.,
19  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
20  */
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/init.h>
24 #include <linux/errno.h>
25 #include <linux/netdevice.h>
26 #include <linux/slab.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/interrupt.h>
29 #include <linux/pm.h>
30 #include <linux/bitops.h>
31
32 #include <asm/irq.h>
33 #include <asm/io.h>
34 #include <asm/au1000.h>
35 #if defined(CONFIG_MIPS_PB1000) || defined(CONFIG_MIPS_PB1100)
36 #include <asm/pb1000.h>
37 #elif defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
38 #include <asm/db1x00.h>
39 #else 
40 #error au1k_ir: unsupported board
41 #endif
42
43 #include <net/irda/irda.h>
44 #include <net/irda/irmod.h>
45 #include <net/irda/wrapper.h>
46 #include <net/irda/irda_device.h>
47 #include "au1000_ircc.h"
48
49 static int au1k_irda_net_init(struct net_device *);
50 static int au1k_irda_start(struct net_device *);
51 static int au1k_irda_stop(struct net_device *dev);
52 static int au1k_irda_hard_xmit(struct sk_buff *, struct net_device *);
53 static int au1k_irda_rx(struct net_device *);
54 static void au1k_irda_interrupt(int, void *);
55 static void au1k_tx_timeout(struct net_device *);
56 static int au1k_irda_ioctl(struct net_device *, struct ifreq *, int);
57 static int au1k_irda_set_speed(struct net_device *dev, int speed);
58
59 static void *dma_alloc(size_t, dma_addr_t *);
60 static void dma_free(void *, size_t);
61
62 static int qos_mtt_bits = 0x07;  /* 1 ms or more */
63 static struct net_device *ir_devs[NUM_IR_IFF];
64 static char version[] __devinitdata =
65     "au1k_ircc:1.2 ppopov@mvista.com\n";
66
67 #define RUN_AT(x) (jiffies + (x))
68
69 #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
70 static BCSR * const bcsr = (BCSR *)0xAE000000;
71 #endif
72
73 static DEFINE_SPINLOCK(ir_lock);
74
75 /*
76  * IrDA peripheral bug. You have to read the register
77  * twice to get the right value.
78  */
79 u32 read_ir_reg(u32 addr) 
80
81         readl(addr);
82         return readl(addr);
83 }
84
85
86 /*
87  * Buffer allocation/deallocation routines. The buffer descriptor returned
88  * has the virtual and dma address of a buffer suitable for 
89  * both, receive and transmit operations.
90  */
91 static db_dest_t *GetFreeDB(struct au1k_private *aup)
92 {
93         db_dest_t *pDB;
94         pDB = aup->pDBfree;
95
96         if (pDB) {
97                 aup->pDBfree = pDB->pnext;
98         }
99         return pDB;
100 }
101
102 static void ReleaseDB(struct au1k_private *aup, db_dest_t *pDB)
103 {
104         db_dest_t *pDBfree = aup->pDBfree;
105         if (pDBfree)
106                 pDBfree->pnext = pDB;
107         aup->pDBfree = pDB;
108 }
109
110
111 /*
112   DMA memory allocation, derived from pci_alloc_consistent.
113   However, the Au1000 data cache is coherent (when programmed
114   so), therefore we return KSEG0 address, not KSEG1.
115 */
116 static void *dma_alloc(size_t size, dma_addr_t * dma_handle)
117 {
118         void *ret;
119         int gfp = GFP_ATOMIC | GFP_DMA;
120
121         ret = (void *) __get_free_pages(gfp, get_order(size));
122
123         if (ret != NULL) {
124                 memset(ret, 0, size);
125                 *dma_handle = virt_to_bus(ret);
126                 ret = (void *)KSEG0ADDR(ret);
127         }
128         return ret;
129 }
130
131
132 static void dma_free(void *vaddr, size_t size)
133 {
134         vaddr = (void *)KSEG0ADDR(vaddr);
135         free_pages((unsigned long) vaddr, get_order(size));
136 }
137
138
139 static void 
140 setup_hw_rings(struct au1k_private *aup, u32 rx_base, u32 tx_base)
141 {
142         int i;
143         for (i=0; i<NUM_IR_DESC; i++) {
144                 aup->rx_ring[i] = (volatile ring_dest_t *) 
145                         (rx_base + sizeof(ring_dest_t)*i);
146         }
147         for (i=0; i<NUM_IR_DESC; i++) {
148                 aup->tx_ring[i] = (volatile ring_dest_t *) 
149                         (tx_base + sizeof(ring_dest_t)*i);
150         }
151 }
152
153 static int au1k_irda_init(void)
154 {
155         static unsigned version_printed = 0;
156         struct au1k_private *aup;
157         struct net_device *dev;
158         int err;
159
160         if (version_printed++ == 0) printk(version);
161
162         dev = alloc_irdadev(sizeof(struct au1k_private));
163         if (!dev)
164                 return -ENOMEM;
165
166         dev->irq = AU1000_IRDA_RX_INT; /* TX has its own interrupt */
167         err = au1k_irda_net_init(dev);
168         if (err)
169                 goto out;
170         err = register_netdev(dev);
171         if (err)
172                 goto out1;
173         ir_devs[0] = dev;
174         printk(KERN_INFO "IrDA: Registered device %s\n", dev->name);
175         return 0;
176
177 out1:
178         aup = netdev_priv(dev);
179         dma_free((void *)aup->db[0].vaddr,
180                 MAX_BUF_SIZE * 2*NUM_IR_DESC);
181         dma_free((void *)aup->rx_ring[0],
182                 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
183         kfree(aup->rx_buff.head);
184 out:
185         free_netdev(dev);
186         return err;
187 }
188
189 static int au1k_irda_init_iobuf(iobuff_t *io, int size)
190 {
191         io->head = kmalloc(size, GFP_KERNEL);
192         if (io->head != NULL) {
193                 io->truesize = size;
194                 io->in_frame = FALSE;
195                 io->state    = OUTSIDE_FRAME;
196                 io->data     = io->head;
197         }
198         return io->head ? 0 : -ENOMEM;
199 }
200
201 static const struct net_device_ops au1k_irda_netdev_ops = {
202         .ndo_open               = au1k_irda_start,
203         .ndo_stop               = au1k_irda_stop,
204         .ndo_start_xmit         = au1k_irda_hard_xmit,
205         .ndo_tx_timeout         = au1k_tx_timeout,
206         .ndo_do_ioctl           = au1k_irda_ioctl,
207         .ndo_change_mtu         = eth_change_mtu,
208         .ndo_validate_addr      = eth_validate_addr,
209         .ndo_set_mac_address    = eth_mac_addr,
210 };
211
212 static int au1k_irda_net_init(struct net_device *dev)
213 {
214         struct au1k_private *aup = netdev_priv(dev);
215         int i, retval = 0, err;
216         db_dest_t *pDB, *pDBfree;
217         dma_addr_t temp;
218
219         err = au1k_irda_init_iobuf(&aup->rx_buff, 14384);
220         if (err)
221                 goto out1;
222
223         dev->netdev_ops = &au1k_irda_netdev_ops;
224
225         irda_init_max_qos_capabilies(&aup->qos);
226
227         /* The only value we must override it the baudrate */
228         aup->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
229                 IR_115200|IR_576000 |(IR_4000000 << 8);
230         
231         aup->qos.min_turn_time.bits = qos_mtt_bits;
232         irda_qos_bits_to_value(&aup->qos);
233
234         retval = -ENOMEM;
235
236         /* Tx ring follows rx ring + 512 bytes */
237         /* we need a 1k aligned buffer */
238         aup->rx_ring[0] = (ring_dest_t *)
239                 dma_alloc(2*MAX_NUM_IR_DESC*(sizeof(ring_dest_t)), &temp);
240         if (!aup->rx_ring[0])
241                 goto out2;
242
243         /* allocate the data buffers */
244         aup->db[0].vaddr = 
245                 (void *)dma_alloc(MAX_BUF_SIZE * 2*NUM_IR_DESC, &temp);
246         if (!aup->db[0].vaddr)
247                 goto out3;
248
249         setup_hw_rings(aup, (u32)aup->rx_ring[0], (u32)aup->rx_ring[0] + 512);
250
251         pDBfree = NULL;
252         pDB = aup->db;
253         for (i=0; i<(2*NUM_IR_DESC); i++) {
254                 pDB->pnext = pDBfree;
255                 pDBfree = pDB;
256                 pDB->vaddr = 
257                         (u32 *)((unsigned)aup->db[0].vaddr + MAX_BUF_SIZE*i);
258                 pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
259                 pDB++;
260         }
261         aup->pDBfree = pDBfree;
262
263         /* attach a data buffer to each descriptor */
264         for (i=0; i<NUM_IR_DESC; i++) {
265                 pDB = GetFreeDB(aup);
266                 if (!pDB) goto out;
267                 aup->rx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
268                 aup->rx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
269                 aup->rx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
270                 aup->rx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
271                 aup->rx_db_inuse[i] = pDB;
272         }
273         for (i=0; i<NUM_IR_DESC; i++) {
274                 pDB = GetFreeDB(aup);
275                 if (!pDB) goto out;
276                 aup->tx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
277                 aup->tx_ring[i]->addr_1 = (u8)((pDB->dma_addr>>8) & 0xff);
278                 aup->tx_ring[i]->addr_2 = (u8)((pDB->dma_addr>>16) & 0xff);
279                 aup->tx_ring[i]->addr_3 = (u8)((pDB->dma_addr>>24) & 0xff);
280                 aup->tx_ring[i]->count_0 = 0;
281                 aup->tx_ring[i]->count_1 = 0;
282                 aup->tx_ring[i]->flags = 0;
283                 aup->tx_db_inuse[i] = pDB;
284         }
285
286 #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
287         /* power on */
288         bcsr->resets &= ~BCSR_RESETS_IRDA_MODE_MASK;
289         bcsr->resets |= BCSR_RESETS_IRDA_MODE_FULL;
290         au_sync();
291 #endif
292
293         return 0;
294
295 out3:
296         dma_free((void *)aup->rx_ring[0],
297                 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
298 out2:
299         kfree(aup->rx_buff.head);
300 out1:
301         printk(KERN_ERR "au1k_init_module failed.  Returns %d\n", retval);
302         return retval;
303 }
304
305
306 static int au1k_init(struct net_device *dev)
307 {
308         struct au1k_private *aup = netdev_priv(dev);
309         int i;
310         u32 control;
311         u32 ring_address;
312
313         /* bring the device out of reset */
314         control = 0xe; /* coherent, clock enable, one half system clock */
315                           
316 #ifndef CONFIG_CPU_LITTLE_ENDIAN
317         control |= 1;
318 #endif
319         aup->tx_head = 0;
320         aup->tx_tail = 0;
321         aup->rx_head = 0;
322
323         for (i=0; i<NUM_IR_DESC; i++) {
324                 aup->rx_ring[i]->flags = AU_OWN;
325         }
326
327         writel(control, IR_INTERFACE_CONFIG);
328         au_sync_delay(10);
329
330         writel(read_ir_reg(IR_ENABLE) & ~0x8000, IR_ENABLE); /* disable PHY */
331         au_sync_delay(1);
332
333         writel(MAX_BUF_SIZE, IR_MAX_PKT_LEN);
334
335         ring_address = (u32)virt_to_phys((void *)aup->rx_ring[0]);
336         writel(ring_address >> 26, IR_RING_BASE_ADDR_H);
337         writel((ring_address >> 10) & 0xffff, IR_RING_BASE_ADDR_L);
338
339         writel(RING_SIZE_64<<8 | RING_SIZE_64<<12, IR_RING_SIZE);
340
341         writel(1<<2 | IR_ONE_PIN, IR_CONFIG_2); /* 48MHz */
342         writel(0, IR_RING_ADDR_CMPR);
343
344         au1k_irda_set_speed(dev, 9600);
345         return 0;
346 }
347
348 static int au1k_irda_start(struct net_device *dev)
349 {
350         int retval;
351         char hwname[32];
352         struct au1k_private *aup = netdev_priv(dev);
353
354         if ((retval = au1k_init(dev))) {
355                 printk(KERN_ERR "%s: error in au1k_init\n", dev->name);
356                 return retval;
357         }
358
359         if ((retval = request_irq(AU1000_IRDA_TX_INT, &au1k_irda_interrupt, 
360                                         0, dev->name, dev))) {
361                 printk(KERN_ERR "%s: unable to get IRQ %d\n", 
362                                 dev->name, dev->irq);
363                 return retval;
364         }
365         if ((retval = request_irq(AU1000_IRDA_RX_INT, &au1k_irda_interrupt, 
366                                         0, dev->name, dev))) {
367                 free_irq(AU1000_IRDA_TX_INT, dev);
368                 printk(KERN_ERR "%s: unable to get IRQ %d\n", 
369                                 dev->name, dev->irq);
370                 return retval;
371         }
372
373         /* Give self a hardware name */
374         sprintf(hwname, "Au1000 SIR/FIR");
375         aup->irlap = irlap_open(dev, &aup->qos, hwname);
376         netif_start_queue(dev);
377
378         writel(read_ir_reg(IR_CONFIG_2) | 1<<8, IR_CONFIG_2); /* int enable */
379
380         aup->timer.expires = RUN_AT((3*HZ)); 
381         aup->timer.data = (unsigned long)dev;
382         return 0;
383 }
384
385 static int au1k_irda_stop(struct net_device *dev)
386 {
387         struct au1k_private *aup = netdev_priv(dev);
388
389         /* disable interrupts */
390         writel(read_ir_reg(IR_CONFIG_2) & ~(1<<8), IR_CONFIG_2);
391         writel(0, IR_CONFIG_1); 
392         writel(0, IR_INTERFACE_CONFIG); /* disable clock */
393         au_sync();
394
395         if (aup->irlap) {
396                 irlap_close(aup->irlap);
397                 aup->irlap = NULL;
398         }
399
400         netif_stop_queue(dev);
401         del_timer(&aup->timer);
402
403         /* disable the interrupt */
404         free_irq(AU1000_IRDA_TX_INT, dev);
405         free_irq(AU1000_IRDA_RX_INT, dev);
406         return 0;
407 }
408
409 static void __exit au1k_irda_exit(void)
410 {
411         struct net_device *dev = ir_devs[0];
412         struct au1k_private *aup = netdev_priv(dev);
413
414         unregister_netdev(dev);
415
416         dma_free((void *)aup->db[0].vaddr,
417                 MAX_BUF_SIZE * 2*NUM_IR_DESC);
418         dma_free((void *)aup->rx_ring[0],
419                 2 * MAX_NUM_IR_DESC*(sizeof(ring_dest_t)));
420         kfree(aup->rx_buff.head);
421         free_netdev(dev);
422 }
423
424
425 static inline void 
426 update_tx_stats(struct net_device *dev, u32 status, u32 pkt_len)
427 {
428         struct au1k_private *aup = netdev_priv(dev);
429         struct net_device_stats *ps = &aup->stats;
430
431         ps->tx_packets++;
432         ps->tx_bytes += pkt_len;
433
434         if (status & IR_TX_ERROR) {
435                 ps->tx_errors++;
436                 ps->tx_aborted_errors++;
437         }
438 }
439
440
441 static void au1k_tx_ack(struct net_device *dev)
442 {
443         struct au1k_private *aup = netdev_priv(dev);
444         volatile ring_dest_t *ptxd;
445
446         ptxd = aup->tx_ring[aup->tx_tail];
447         while (!(ptxd->flags & AU_OWN) && (aup->tx_tail != aup->tx_head)) {
448                 update_tx_stats(dev, ptxd->flags, 
449                                 ptxd->count_1<<8 | ptxd->count_0);
450                 ptxd->count_0 = 0;
451                 ptxd->count_1 = 0;
452                 au_sync();
453
454                 aup->tx_tail = (aup->tx_tail + 1) & (NUM_IR_DESC - 1);
455                 ptxd = aup->tx_ring[aup->tx_tail];
456
457                 if (aup->tx_full) {
458                         aup->tx_full = 0;
459                         netif_wake_queue(dev);
460                 }
461         }
462
463         if (aup->tx_tail == aup->tx_head) {
464                 if (aup->newspeed) {
465                         au1k_irda_set_speed(dev, aup->newspeed);
466                         aup->newspeed = 0;
467                 }
468                 else {
469                         writel(read_ir_reg(IR_CONFIG_1) & ~IR_TX_ENABLE, 
470                                         IR_CONFIG_1); 
471                         au_sync();
472                         writel(read_ir_reg(IR_CONFIG_1) | IR_RX_ENABLE, 
473                                         IR_CONFIG_1); 
474                         writel(0, IR_RING_PROMPT);
475                         au_sync();
476                 }
477         }
478 }
479
480
481 /*
482  * Au1000 transmit routine.
483  */
484 static int au1k_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
485 {
486         struct au1k_private *aup = netdev_priv(dev);
487         int speed = irda_get_next_speed(skb);
488         volatile ring_dest_t *ptxd;
489         u32 len;
490
491         u32 flags;
492         db_dest_t *pDB;
493
494         if (speed != aup->speed && speed != -1) {
495                 aup->newspeed = speed;
496         }
497
498         if ((skb->len == 0) && (aup->newspeed)) {
499                 if (aup->tx_tail == aup->tx_head) {
500                         au1k_irda_set_speed(dev, speed);
501                         aup->newspeed = 0;
502                 }
503                 dev_kfree_skb(skb);
504                 return 0;
505         }
506
507         ptxd = aup->tx_ring[aup->tx_head];
508         flags = ptxd->flags;
509
510         if (flags & AU_OWN) {
511                 printk(KERN_DEBUG "%s: tx_full\n", dev->name);
512                 netif_stop_queue(dev);
513                 aup->tx_full = 1;
514                 return 1;
515         }
516         else if (((aup->tx_head + 1) & (NUM_IR_DESC - 1)) == aup->tx_tail) {
517                 printk(KERN_DEBUG "%s: tx_full\n", dev->name);
518                 netif_stop_queue(dev);
519                 aup->tx_full = 1;
520                 return 1;
521         }
522
523         pDB = aup->tx_db_inuse[aup->tx_head];
524
525 #if 0
526         if (read_ir_reg(IR_RX_BYTE_CNT) != 0) {
527                 printk("tx warning: rx byte cnt %x\n", 
528                                 read_ir_reg(IR_RX_BYTE_CNT));
529         }
530 #endif
531         
532         if (aup->speed == 4000000) {
533                 /* FIR */
534                 skb_copy_from_linear_data(skb, pDB->vaddr, skb->len);
535                 ptxd->count_0 = skb->len & 0xff;
536                 ptxd->count_1 = (skb->len >> 8) & 0xff;
537
538         }
539         else {
540                 /* SIR */
541                 len = async_wrap_skb(skb, (u8 *)pDB->vaddr, MAX_BUF_SIZE);
542                 ptxd->count_0 = len & 0xff;
543                 ptxd->count_1 = (len >> 8) & 0xff;
544                 ptxd->flags |= IR_DIS_CRC;
545                 au_writel(au_readl(0xae00000c) & ~(1<<13), 0xae00000c);
546         }
547         ptxd->flags |= AU_OWN;
548         au_sync();
549
550         writel(read_ir_reg(IR_CONFIG_1) | IR_TX_ENABLE, IR_CONFIG_1); 
551         writel(0, IR_RING_PROMPT);
552         au_sync();
553
554         dev_kfree_skb(skb);
555         aup->tx_head = (aup->tx_head + 1) & (NUM_IR_DESC - 1);
556         dev->trans_start = jiffies;
557         return 0;
558 }
559
560
561 static inline void 
562 update_rx_stats(struct net_device *dev, u32 status, u32 count)
563 {
564         struct au1k_private *aup = netdev_priv(dev);
565         struct net_device_stats *ps = &aup->stats;
566
567         ps->rx_packets++;
568
569         if (status & IR_RX_ERROR) {
570                 ps->rx_errors++;
571                 if (status & (IR_PHY_ERROR|IR_FIFO_OVER))
572                         ps->rx_missed_errors++;
573                 if (status & IR_MAX_LEN)
574                         ps->rx_length_errors++;
575                 if (status & IR_CRC_ERROR)
576                         ps->rx_crc_errors++;
577         }
578         else 
579                 ps->rx_bytes += count;
580 }
581
582 /*
583  * Au1000 receive routine.
584  */
585 static int au1k_irda_rx(struct net_device *dev)
586 {
587         struct au1k_private *aup = netdev_priv(dev);
588         struct sk_buff *skb;
589         volatile ring_dest_t *prxd;
590         u32 flags, count;
591         db_dest_t *pDB;
592
593         prxd = aup->rx_ring[aup->rx_head];
594         flags = prxd->flags;
595
596         while (!(flags & AU_OWN))  {
597                 pDB = aup->rx_db_inuse[aup->rx_head];
598                 count = prxd->count_1<<8 | prxd->count_0;
599                 if (!(flags & IR_RX_ERROR))  {
600                         /* good frame */
601                         update_rx_stats(dev, flags, count);
602                         skb=alloc_skb(count+1,GFP_ATOMIC);
603                         if (skb == NULL) {
604                                 aup->netdev->stats.rx_dropped++;
605                                 continue;
606                         }
607                         skb_reserve(skb, 1);
608                         if (aup->speed == 4000000)
609                                 skb_put(skb, count);
610                         else
611                                 skb_put(skb, count-2);
612                         skb_copy_to_linear_data(skb, pDB->vaddr, count - 2);
613                         skb->dev = dev;
614                         skb_reset_mac_header(skb);
615                         skb->protocol = htons(ETH_P_IRDA);
616                         netif_rx(skb);
617                         prxd->count_0 = 0;
618                         prxd->count_1 = 0;
619                 }
620                 prxd->flags |= AU_OWN;
621                 aup->rx_head = (aup->rx_head + 1) & (NUM_IR_DESC - 1);
622                 writel(0, IR_RING_PROMPT);
623                 au_sync();
624
625                 /* next descriptor */
626                 prxd = aup->rx_ring[aup->rx_head];
627                 flags = prxd->flags;
628
629         }
630         return 0;
631 }
632
633
634 static irqreturn_t au1k_irda_interrupt(int dummy, void *dev_id)
635 {
636         struct net_device *dev = dev_id;
637
638         writel(0, IR_INT_CLEAR); /* ack irda interrupts */
639
640         au1k_irda_rx(dev);
641         au1k_tx_ack(dev);
642
643         return IRQ_HANDLED;
644 }
645
646
647 /*
648  * The Tx ring has been full longer than the watchdog timeout
649  * value. The transmitter must be hung?
650  */
651 static void au1k_tx_timeout(struct net_device *dev)
652 {
653         u32 speed;
654         struct au1k_private *aup = netdev_priv(dev);
655
656         printk(KERN_ERR "%s: tx timeout\n", dev->name);
657         speed = aup->speed;
658         aup->speed = 0;
659         au1k_irda_set_speed(dev, speed);
660         aup->tx_full = 0;
661         netif_wake_queue(dev);
662 }
663
664
665 /*
666  * Set the IrDA communications speed.
667  */
668 static int 
669 au1k_irda_set_speed(struct net_device *dev, int speed)
670 {
671         unsigned long flags;
672         struct au1k_private *aup = netdev_priv(dev);
673         u32 control;
674         int ret = 0, timeout = 10, i;
675         volatile ring_dest_t *ptxd;
676 #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
677         unsigned long irda_resets;
678 #endif
679
680         if (speed == aup->speed)
681                 return ret;
682
683         spin_lock_irqsave(&ir_lock, flags);
684
685         /* disable PHY first */
686         writel(read_ir_reg(IR_ENABLE) & ~0x8000, IR_ENABLE);
687
688         /* disable RX/TX */
689         writel(read_ir_reg(IR_CONFIG_1) & ~(IR_RX_ENABLE|IR_TX_ENABLE), 
690                         IR_CONFIG_1);
691         au_sync_delay(1);
692         while (read_ir_reg(IR_ENABLE) & (IR_RX_STATUS | IR_TX_STATUS)) {
693                 mdelay(1);
694                 if (!timeout--) {
695                         printk(KERN_ERR "%s: rx/tx disable timeout\n",
696                                         dev->name);
697                         break;
698                 }
699         }
700
701         /* disable DMA */
702         writel(read_ir_reg(IR_CONFIG_1) & ~IR_DMA_ENABLE, IR_CONFIG_1);
703         au_sync_delay(1);
704
705         /* 
706          *  After we disable tx/rx. the index pointers
707          * go back to zero.
708          */
709         aup->tx_head = aup->tx_tail = aup->rx_head = 0;
710         for (i=0; i<NUM_IR_DESC; i++) {
711                 ptxd = aup->tx_ring[i];
712                 ptxd->flags = 0;
713                 ptxd->count_0 = 0;
714                 ptxd->count_1 = 0;
715         }
716
717         for (i=0; i<NUM_IR_DESC; i++) {
718                 ptxd = aup->rx_ring[i];
719                 ptxd->count_0 = 0;
720                 ptxd->count_1 = 0;
721                 ptxd->flags = AU_OWN;
722         }
723
724         if (speed == 4000000) {
725 #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
726                 bcsr->resets |= BCSR_RESETS_FIR_SEL;
727 #else /* Pb1000 and Pb1100 */
728                 writel(1<<13, CPLD_AUX1);
729 #endif
730         }
731         else {
732 #if defined(CONFIG_MIPS_DB1000) || defined(CONFIG_MIPS_DB1100)
733                 bcsr->resets &= ~BCSR_RESETS_FIR_SEL;
734 #else /* Pb1000 and Pb1100 */
735                 writel(readl(CPLD_AUX1) & ~(1<<13), CPLD_AUX1);
736 #endif
737         }
738
739         switch (speed) {
740         case 9600:      
741                 writel(11<<10 | 12<<5, IR_WRITE_PHY_CONFIG); 
742                 writel(IR_SIR_MODE, IR_CONFIG_1); 
743                 break;
744         case 19200:     
745                 writel(5<<10 | 12<<5, IR_WRITE_PHY_CONFIG); 
746                 writel(IR_SIR_MODE, IR_CONFIG_1); 
747                 break;
748         case 38400:
749                 writel(2<<10 | 12<<5, IR_WRITE_PHY_CONFIG); 
750                 writel(IR_SIR_MODE, IR_CONFIG_1); 
751                 break;
752         case 57600:     
753                 writel(1<<10 | 12<<5, IR_WRITE_PHY_CONFIG); 
754                 writel(IR_SIR_MODE, IR_CONFIG_1); 
755                 break;
756         case 115200: 
757                 writel(12<<5, IR_WRITE_PHY_CONFIG); 
758                 writel(IR_SIR_MODE, IR_CONFIG_1); 
759                 break;
760         case 4000000:
761                 writel(0xF, IR_WRITE_PHY_CONFIG);
762                 writel(IR_FIR|IR_DMA_ENABLE|IR_RX_ENABLE, IR_CONFIG_1); 
763                 break;
764         default:
765                 printk(KERN_ERR "%s unsupported speed %x\n", dev->name, speed);
766                 ret = -EINVAL;
767                 break;
768         }
769
770         aup->speed = speed;
771         writel(read_ir_reg(IR_ENABLE) | 0x8000, IR_ENABLE);
772         au_sync();
773
774         control = read_ir_reg(IR_ENABLE);
775         writel(0, IR_RING_PROMPT);
776         au_sync();
777
778         if (control & (1<<14)) {
779                 printk(KERN_ERR "%s: configuration error\n", dev->name);
780         }
781         else {
782                 if (control & (1<<11))
783                         printk(KERN_DEBUG "%s Valid SIR config\n", dev->name);
784                 if (control & (1<<12))
785                         printk(KERN_DEBUG "%s Valid MIR config\n", dev->name);
786                 if (control & (1<<13))
787                         printk(KERN_DEBUG "%s Valid FIR config\n", dev->name);
788                 if (control & (1<<10))
789                         printk(KERN_DEBUG "%s TX enabled\n", dev->name);
790                 if (control & (1<<9))
791                         printk(KERN_DEBUG "%s RX enabled\n", dev->name);
792         }
793
794         spin_unlock_irqrestore(&ir_lock, flags);
795         return ret;
796 }
797
798 static int 
799 au1k_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
800 {
801         struct if_irda_req *rq = (struct if_irda_req *)ifreq;
802         struct au1k_private *aup = netdev_priv(dev);
803         int ret = -EOPNOTSUPP;
804
805         switch (cmd) {
806         case SIOCSBANDWIDTH:
807                 if (capable(CAP_NET_ADMIN)) {
808                         /*
809                          * We are unable to set the speed if the
810                          * device is not running.
811                          */
812                         if (aup->open)
813                                 ret = au1k_irda_set_speed(dev,
814                                                 rq->ifr_baudrate);
815                         else {
816                                 printk(KERN_ERR "%s ioctl: !netif_running\n",
817                                                 dev->name);
818                                 ret = 0;
819                         }
820                 }
821                 break;
822
823         case SIOCSMEDIABUSY:
824                 ret = -EPERM;
825                 if (capable(CAP_NET_ADMIN)) {
826                         irda_device_set_media_busy(dev, TRUE);
827                         ret = 0;
828                 }
829                 break;
830
831         case SIOCGRECEIVING:
832                 rq->ifr_receiving = 0;
833                 break;
834         default:
835                 break;
836         }
837         return ret;
838 }
839
840 MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
841 MODULE_DESCRIPTION("Au1000 IrDA Device Driver");
842
843 module_init(au1k_irda_init);
844 module_exit(au1k_irda_exit);