1 /*********************************************************************
5 * Description: Driver for the ALI M1535D and M1543C FIR Controller
6 * Status: Experimental.
7 * Author: Benjamin Kong <benjamin_kong@ali.com.tw>
8 * Created at: 2000/10/16 03:46PM
9 * Modified at: 2001/1/3 02:55PM
10 * Modified by: Benjamin Kong <benjamin_kong@ali.com.tw>
11 * Modified at: 2003/11/6 and support for ALi south-bridge chipsets M1563
12 * Modified by: Clear Zhang <clear_zhang@ali.com.tw>
14 * Copyright (c) 2000 Benjamin Kong <benjamin_kong@ali.com.tw>
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 ********************************************************************/
24 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/types.h>
28 #include <linux/skbuff.h>
29 #include <linux/netdevice.h>
30 #include <linux/ioport.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/serial_reg.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/platform_device.h>
41 #include <asm/byteorder.h>
43 #include <net/irda/wrapper.h>
44 #include <net/irda/irda.h>
45 #include <net/irda/irda_device.h>
49 #define CHIP_IO_EXTENT 8
50 #define BROKEN_DONGLE_ID
52 #define ALI_IRCC_DRIVER_NAME "ali-ircc"
54 /* Power Management */
55 static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state);
56 static int ali_ircc_resume(struct platform_device *dev);
58 static struct platform_driver ali_ircc_driver = {
59 .suspend = ali_ircc_suspend,
60 .resume = ali_ircc_resume,
62 .name = ALI_IRCC_DRIVER_NAME,
66 /* Module parameters */
67 static int qos_mtt_bits = 0x07; /* 1 ms or more */
69 /* Use BIOS settions by default, but user may supply module parameters */
70 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
71 static unsigned int irq[] = { 0, 0, 0, 0 };
72 static unsigned int dma[] = { 0, 0, 0, 0 };
74 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
75 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
76 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
78 /* These are the currently known ALi sourth-bridge chipsets, the only one difference
79 * is that M1543C doesn't support HP HDSL-3600
81 static ali_chip_t chips[] =
83 { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
84 { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
85 { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
89 /* Max 4 instances for now */
90 static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
93 static char *dongle_types[] = {
97 "No dongle connected",
100 /* Some prototypes */
101 static int ali_ircc_open(int i, chipio_t *info);
103 static int ali_ircc_close(struct ali_ircc_cb *self);
105 static int ali_ircc_setup(chipio_t *info);
106 static int ali_ircc_is_receiving(struct ali_ircc_cb *self);
107 static int ali_ircc_net_open(struct net_device *dev);
108 static int ali_ircc_net_close(struct net_device *dev);
109 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
110 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
111 static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev);
114 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
115 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
116 static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
117 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
118 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
119 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
122 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
123 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
124 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
125 static int ali_ircc_dma_receive(struct ali_ircc_cb *self);
126 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
127 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
128 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
131 static int ali_ircc_read_dongle_id (int i, chipio_t *info);
132 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
134 /* ALi chip function */
135 static void SIR2FIR(int iobase);
136 static void FIR2SIR(int iobase);
137 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
140 * Function ali_ircc_init ()
142 * Initialize chip. Find out whay kinds of chips we are dealing with
143 * and their configuation registers address
145 static int __init ali_ircc_init(void)
154 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
156 ret = platform_driver_register(&ali_ircc_driver);
158 IRDA_ERROR("%s, Can't register driver!\n",
159 ALI_IRCC_DRIVER_NAME);
164 /* Probe for all the ALi chipsets we know about */
165 for (chip= chips; chip->name; chip++, i++)
167 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__, chip->name);
169 /* Try all config registers for this chip */
170 for (cfg=0; cfg<2; cfg++)
172 cfg_base = chip->cfg[cfg];
176 memset(&info, 0, sizeof(chipio_t));
177 info.cfg_base = cfg_base;
178 info.fir_base = io[i];
183 /* Enter Configuration */
184 outb(chip->entr1, cfg_base);
185 outb(chip->entr2, cfg_base);
187 /* Select Logical Device 5 Registers (UART2) */
188 outb(0x07, cfg_base);
189 outb(0x05, cfg_base+1);
191 /* Read Chip Identification Register */
192 outb(chip->cid_index, cfg_base);
193 reg = inb(cfg_base+1);
195 if (reg == chip->cid_value)
197 IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __FUNCTION__, cfg_base);
199 outb(0x1F, cfg_base);
200 revision = inb(cfg_base+1);
201 IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __FUNCTION__,
202 chip->name, revision);
205 * If the user supplies the base address, then
206 * we init the chip, if not we probe the values
211 chip->init(chip, &info);
215 chip->probe(chip, &info);
218 if (ali_ircc_open(i, &info) == 0)
224 IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __FUNCTION__, chip->name, cfg_base);
226 /* Exit configuration */
227 outb(0xbb, cfg_base);
231 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
234 platform_driver_unregister(&ali_ircc_driver);
240 * Function ali_ircc_cleanup ()
242 * Close all configured chips
245 static void __exit ali_ircc_cleanup(void)
249 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
251 for (i=0; i < 4; i++) {
253 ali_ircc_close(dev_self[i]);
256 platform_driver_unregister(&ali_ircc_driver);
258 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
262 * Function ali_ircc_open (int i, chipio_t *inf)
264 * Open driver instance
267 static int ali_ircc_open(int i, chipio_t *info)
269 struct net_device *dev;
270 struct ali_ircc_cb *self;
274 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
276 /* Set FIR FIFO and DMA Threshold */
277 if ((ali_ircc_setup(info)) == -1)
280 dev = alloc_irdadev(sizeof(*self));
282 IRDA_ERROR("%s(), can't allocate memory for control block!\n",
289 spin_lock_init(&self->lock);
291 /* Need to store self somewhere */
296 self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
297 self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
298 self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
299 self->io.irq = info->irq;
300 self->io.fir_ext = CHIP_IO_EXTENT;
301 self->io.dma = info->dma;
302 self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
304 /* Reserve the ioports that we need */
305 if (!request_region(self->io.fir_base, self->io.fir_ext,
306 ALI_IRCC_DRIVER_NAME)) {
307 IRDA_WARNING("%s(), can't get iobase of 0x%03x\n", __FUNCTION__,
313 /* Initialize QoS for this device */
314 irda_init_max_qos_capabilies(&self->qos);
316 /* The only value we must override it the baudrate */
317 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
318 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
320 self->qos.min_turn_time.bits = qos_mtt_bits;
322 irda_qos_bits_to_value(&self->qos);
324 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
325 self->rx_buff.truesize = 14384;
326 self->tx_buff.truesize = 14384;
328 /* Allocate memory if needed */
330 dma_alloc_coherent(NULL, self->rx_buff.truesize,
331 &self->rx_buff_dma, GFP_KERNEL);
332 if (self->rx_buff.head == NULL) {
336 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
339 dma_alloc_coherent(NULL, self->tx_buff.truesize,
340 &self->tx_buff_dma, GFP_KERNEL);
341 if (self->tx_buff.head == NULL) {
345 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
347 self->rx_buff.in_frame = FALSE;
348 self->rx_buff.state = OUTSIDE_FRAME;
349 self->tx_buff.data = self->tx_buff.head;
350 self->rx_buff.data = self->rx_buff.head;
352 /* Reset Tx queue info */
353 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
354 self->tx_fifo.tail = self->tx_buff.head;
357 /* Keep track of module usage */
358 SET_MODULE_OWNER(dev);
360 /* Override the network functions we need to use */
361 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
362 dev->open = ali_ircc_net_open;
363 dev->stop = ali_ircc_net_close;
364 dev->do_ioctl = ali_ircc_net_ioctl;
365 dev->get_stats = ali_ircc_net_get_stats;
367 err = register_netdev(dev);
369 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
372 IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
374 /* Check dongle id */
375 dongle_id = ali_ircc_read_dongle_id(i, info);
376 IRDA_MESSAGE("%s(), %s, Found dongle: %s\n", __FUNCTION__,
377 ALI_IRCC_DRIVER_NAME, dongle_types[dongle_id]);
379 self->io.dongle_id = dongle_id;
381 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
386 dma_free_coherent(NULL, self->tx_buff.truesize,
387 self->tx_buff.head, self->tx_buff_dma);
389 dma_free_coherent(NULL, self->rx_buff.truesize,
390 self->rx_buff.head, self->rx_buff_dma);
392 release_region(self->io.fir_base, self->io.fir_ext);
401 * Function ali_ircc_close (self)
403 * Close driver instance
406 static int __exit ali_ircc_close(struct ali_ircc_cb *self)
410 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
412 IRDA_ASSERT(self != NULL, return -1;);
414 iobase = self->io.fir_base;
416 /* Remove netdevice */
417 unregister_netdev(self->netdev);
419 /* Release the PORT that this driver is using */
420 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __FUNCTION__, self->io.fir_base);
421 release_region(self->io.fir_base, self->io.fir_ext);
423 if (self->tx_buff.head)
424 dma_free_coherent(NULL, self->tx_buff.truesize,
425 self->tx_buff.head, self->tx_buff_dma);
427 if (self->rx_buff.head)
428 dma_free_coherent(NULL, self->rx_buff.truesize,
429 self->rx_buff.head, self->rx_buff_dma);
431 dev_self[self->index] = NULL;
432 free_netdev(self->netdev);
434 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
440 * Function ali_ircc_init_43 (chip, info)
442 * Initialize the ALi M1543 chip.
444 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
446 /* All controller information like I/O address, DMA channel, IRQ
454 * Function ali_ircc_init_53 (chip, info)
456 * Initialize the ALi M1535 chip.
458 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
460 /* All controller information like I/O address, DMA channel, IRQ
468 * Function ali_ircc_probe_53 (chip, info)
470 * Probes for the ALi M1535D or M1535
472 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
474 int cfg_base = info->cfg_base;
477 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
479 /* Enter Configuration */
480 outb(chip->entr1, cfg_base);
481 outb(chip->entr2, cfg_base);
483 /* Select Logical Device 5 Registers (UART2) */
484 outb(0x07, cfg_base);
485 outb(0x05, cfg_base+1);
487 /* Read address control register */
488 outb(0x60, cfg_base);
489 hi = inb(cfg_base+1);
490 outb(0x61, cfg_base);
491 low = inb(cfg_base+1);
492 info->fir_base = (hi<<8) + low;
494 info->sir_base = info->fir_base;
496 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__, info->fir_base);
498 /* Read IRQ control register */
499 outb(0x70, cfg_base);
500 reg = inb(cfg_base+1);
501 info->irq = reg & 0x0f;
502 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
504 /* Read DMA channel */
505 outb(0x74, cfg_base);
506 reg = inb(cfg_base+1);
507 info->dma = reg & 0x07;
509 if(info->dma == 0x04)
510 IRDA_WARNING("%s(), No DMA channel assigned !\n", __FUNCTION__);
512 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
514 /* Read Enabled Status */
515 outb(0x30, cfg_base);
516 reg = inb(cfg_base+1);
517 info->enabled = (reg & 0x80) && (reg & 0x01);
518 IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __FUNCTION__, info->enabled);
520 /* Read Power Status */
521 outb(0x22, cfg_base);
522 reg = inb(cfg_base+1);
523 info->suspended = (reg & 0x20);
524 IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __FUNCTION__, info->suspended);
526 /* Exit configuration */
527 outb(0xbb, cfg_base);
529 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
535 * Function ali_ircc_setup (info)
537 * Set FIR FIFO and DMA Threshold
538 * Returns non-negative on success.
541 static int ali_ircc_setup(chipio_t *info)
545 int iobase = info->fir_base;
547 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
549 /* Locking comments :
550 * Most operations here need to be protected. We are called before
551 * the device instance is created in ali_ircc_open(), therefore
552 * nobody can bother us - Jean II */
554 /* Switch to FIR space */
558 outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
560 /* Read FIR ID Version Register */
561 switch_bank(iobase, BANK3);
562 version = inb(iobase+FIR_ID_VR);
564 /* Should be 0x00 in the M1535/M1535D */
567 IRDA_ERROR("%s, Wrong chip version %02x\n",
568 ALI_IRCC_DRIVER_NAME, version);
572 /* Set FIR FIFO Threshold Register */
573 switch_bank(iobase, BANK1);
574 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
576 /* Set FIR DMA Threshold Register */
577 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
580 switch_bank(iobase, BANK2);
581 outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
583 /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
585 /* Switch to Bank 0 */
586 switch_bank(iobase, BANK0);
588 tmp = inb(iobase+FIR_LCR_B);
589 tmp &=~0x20; // disable SIP
590 tmp |= 0x80; // these two steps make RX mode
592 outb(tmp, iobase+FIR_LCR_B);
594 /* Disable Interrupt */
595 outb(0x00, iobase+FIR_IER);
598 /* Switch to SIR space */
601 IRDA_MESSAGE("%s, driver loaded (Benjamin Kong)\n",
602 ALI_IRCC_DRIVER_NAME);
604 /* Enable receive interrupts */
605 // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
606 // Turn on the interrupts in ali_ircc_net_open
608 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
614 * Function ali_ircc_read_dongle_id (int index, info)
616 * Try to read dongle indentification. This procedure needs to be executed
617 * once after power-on/reset. It also needs to be used whenever you suspect
618 * that the user may have plugged/unplugged the IrDA Dongle.
620 static int ali_ircc_read_dongle_id (int i, chipio_t *info)
623 int cfg_base = info->cfg_base;
625 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
627 /* Enter Configuration */
628 outb(chips[i].entr1, cfg_base);
629 outb(chips[i].entr2, cfg_base);
631 /* Select Logical Device 5 Registers (UART2) */
632 outb(0x07, cfg_base);
633 outb(0x05, cfg_base+1);
636 outb(0xf0, cfg_base);
637 reg = inb(cfg_base+1);
638 dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
639 IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __FUNCTION__,
640 dongle_id, dongle_types[dongle_id]);
642 /* Exit configuration */
643 outb(0xbb, cfg_base);
645 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
651 * Function ali_ircc_interrupt (irq, dev_id, regs)
653 * An interrupt from the chip has arrived. Time to do some work
656 static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id,
657 struct pt_regs *regs)
659 struct net_device *dev = (struct net_device *) dev_id;
660 struct ali_ircc_cb *self;
663 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
666 IRDA_WARNING("%s: irq %d for unknown device.\n",
667 ALI_IRCC_DRIVER_NAME, irq);
671 self = (struct ali_ircc_cb *) dev->priv;
673 spin_lock(&self->lock);
675 /* Dispatch interrupt handler for the current speed */
676 if (self->io.speed > 115200)
677 ret = ali_ircc_fir_interrupt(self);
679 ret = ali_ircc_sir_interrupt(self);
681 spin_unlock(&self->lock);
683 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
687 * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
689 * Handle MIR/FIR interrupt
692 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
694 __u8 eir, OldMessageCount;
697 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
699 iobase = self->io.fir_base;
701 switch_bank(iobase, BANK0);
702 self->InterruptID = inb(iobase+FIR_IIR);
703 self->BusStatus = inb(iobase+FIR_BSR);
705 OldMessageCount = (self->LineStatus + 1) & 0x07;
706 self->LineStatus = inb(iobase+FIR_LSR);
707 //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
708 eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
710 IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __FUNCTION__,self->InterruptID);
711 IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __FUNCTION__,self->LineStatus);
712 IRDA_DEBUG(1, "%s(), self->ier = %x\n", __FUNCTION__,self->ier);
713 IRDA_DEBUG(1, "%s(), eir = %x\n", __FUNCTION__,eir);
715 /* Disable interrupts */
716 SetCOMInterrupts(self, FALSE);
718 /* Tx or Rx Interrupt */
722 if (self->io.direction == IO_XMIT) /* TX */
724 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __FUNCTION__);
726 if(ali_ircc_dma_xmit_complete(self))
728 if (irda_device_txqueue_empty(self->netdev))
730 /* Prepare for receive */
731 ali_ircc_dma_receive(self);
743 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __FUNCTION__);
745 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
747 self->rcvFramesOverflow = TRUE;
748 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******** \n", __FUNCTION__);
751 if (ali_ircc_dma_receive_complete(self))
753 IRDA_DEBUG(1, "%s(), ******* receive complete ******** \n", __FUNCTION__);
759 IRDA_DEBUG(1, "%s(), ******* Not receive complete ******** \n", __FUNCTION__);
761 self->ier = IER_EOM | IER_TIMER;
766 /* Timer Interrupt */
767 else if (eir & IIR_TIMER)
769 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
771 self->rcvFramesOverflow = TRUE;
772 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******* \n", __FUNCTION__);
775 switch_bank(iobase, BANK1);
776 tmp = inb(iobase+FIR_CR);
777 outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
779 /* Check if this is a Tx timer interrupt */
780 if (self->io.direction == IO_XMIT)
782 ali_ircc_dma_xmit(self);
784 /* Interrupt on EOM */
790 if(ali_ircc_dma_receive_complete(self))
796 self->ier = IER_EOM | IER_TIMER;
801 /* Restore Interrupt */
802 SetCOMInterrupts(self, TRUE);
804 IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __FUNCTION__);
805 return IRQ_RETVAL(eir);
809 * Function ali_ircc_sir_interrupt (irq, self, eir)
811 * Handle SIR interrupt
814 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
819 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
821 iobase = self->io.sir_base;
823 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
825 /* Clear interrupt */
826 lsr = inb(iobase+UART_LSR);
828 IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __FUNCTION__,
834 IRDA_DEBUG(2, "%s(), RLSI\n", __FUNCTION__);
837 /* Receive interrupt */
838 ali_ircc_sir_receive(self);
841 if (lsr & UART_LSR_THRE)
843 /* Transmitter ready for data */
844 ali_ircc_sir_write_wakeup(self);
848 IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __FUNCTION__, iir);
855 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
857 return IRQ_RETVAL(iir);
862 * Function ali_ircc_sir_receive (self)
864 * Receive one frame from the infrared port
867 static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
872 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
873 IRDA_ASSERT(self != NULL, return;);
875 iobase = self->io.sir_base;
878 * Receive all characters in Rx FIFO, unwrap and unstuff them.
879 * async_unwrap_char will deliver all found frames
882 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
883 inb(iobase+UART_RX));
885 /* Make sure we don't stay here too long */
886 if (boguscount++ > 32) {
887 IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
890 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
892 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
896 * Function ali_ircc_sir_write_wakeup (tty)
898 * Called by the driver when there's room for more data. If we have
899 * more packets to send, we send them here.
902 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
907 IRDA_ASSERT(self != NULL, return;);
909 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
911 iobase = self->io.sir_base;
913 /* Finished with frame? */
914 if (self->tx_buff.len > 0)
916 /* Write data left in transmit buffer */
917 actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
918 self->tx_buff.data, self->tx_buff.len);
919 self->tx_buff.data += actual;
920 self->tx_buff.len -= actual;
926 /* We must wait until all data are gone */
927 while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
928 IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __FUNCTION__ );
930 IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __FUNCTION__ , self->new_speed);
931 ali_ircc_change_speed(self, self->new_speed);
934 // benjamin 2000/11/10 06:32PM
935 if (self->io.speed > 115200)
937 IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT \n", __FUNCTION__ );
940 // SetCOMInterrupts(self, TRUE);
946 netif_wake_queue(self->netdev);
949 self->stats.tx_packets++;
951 /* Turn on receive interrupts */
952 outb(UART_IER_RDI, iobase+UART_IER);
955 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
958 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
960 struct net_device *dev = self->netdev;
963 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
965 IRDA_DEBUG(2, "%s(), setting speed = %d \n", __FUNCTION__ , baud);
967 /* This function *must* be called with irq off and spin-lock.
970 iobase = self->io.fir_base;
972 SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
974 /* Go to MIR, FIR Speed */
979 ali_ircc_fir_change_speed(self, baud);
981 /* Install FIR xmit handler*/
982 dev->hard_start_xmit = ali_ircc_fir_hard_xmit;
984 /* Enable Interuupt */
985 self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
987 /* Be ready for incomming frames */
988 ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
990 /* Go to SIR Speed */
993 ali_ircc_sir_change_speed(self, baud);
995 /* Install SIR xmit handler*/
996 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
1000 SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
1002 netif_wake_queue(self->netdev);
1004 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1007 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
1011 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1012 struct net_device *dev;
1014 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1016 IRDA_ASSERT(self != NULL, return;);
1019 iobase = self->io.fir_base;
1021 IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __FUNCTION__ ,self->io.speed,baud);
1023 /* Come from SIR speed */
1024 if(self->io.speed <=115200)
1029 /* Update accounting for new speed */
1030 self->io.speed = baud;
1032 // Set Dongle Speed mode
1033 ali_ircc_change_dongle_speed(self, baud);
1035 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1039 * Function ali_sir_change_speed (self, speed)
1041 * Set speed of IrDA port to specified baudrate
1044 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
1046 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1047 unsigned long flags;
1049 int fcr; /* FIFO control reg */
1050 int lcr; /* Line control reg */
1053 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1055 IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __FUNCTION__ , speed);
1057 IRDA_ASSERT(self != NULL, return;);
1059 iobase = self->io.sir_base;
1061 /* Come from MIR or FIR speed */
1062 if(self->io.speed >115200)
1064 // Set Dongle Speed mode first
1065 ali_ircc_change_dongle_speed(self, speed);
1070 // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
1072 inb(iobase+UART_LSR);
1073 inb(iobase+UART_SCR);
1075 /* Update accounting for new speed */
1076 self->io.speed = speed;
1078 spin_lock_irqsave(&self->lock, flags);
1080 divisor = 115200/speed;
1082 fcr = UART_FCR_ENABLE_FIFO;
1085 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
1086 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1087 * about this timeout since it will always be fast enough.
1089 if (self->io.speed < 38400)
1090 fcr |= UART_FCR_TRIGGER_1;
1092 fcr |= UART_FCR_TRIGGER_14;
1094 /* IrDA ports use 8N1 */
1095 lcr = UART_LCR_WLEN8;
1097 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
1098 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
1099 outb(divisor >> 8, iobase+UART_DLM);
1100 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
1101 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
1103 /* without this, the conection will be broken after come back from FIR speed,
1104 but with this, the SIR connection is harder to established */
1105 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
1107 spin_unlock_irqrestore(&self->lock, flags);
1109 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1112 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
1115 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1116 int iobase,dongle_id;
1119 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1121 iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
1122 dongle_id = self->io.dongle_id;
1124 /* We are already locked, no need to do it again */
1126 IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __FUNCTION__ , dongle_types[dongle_id], speed);
1128 switch_bank(iobase, BANK2);
1129 tmp = inb(iobase+FIR_IRDA_CR);
1131 /* IBM type dongle */
1134 if(speed == 4000000)
1137 // SD/MODE __| |__ __
1142 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1143 tmp |= IRDA_CR_CRC; // CRC=1
1145 switch_bank(iobase, BANK2);
1146 outb(tmp, iobase+FIR_IRDA_CR);
1148 // T1 -> SD/MODE:0 IRTX:0
1151 outb(tmp, iobase+FIR_IRDA_CR);
1154 // T2 -> SD/MODE:1 IRTX:0
1157 outb(tmp, iobase+FIR_IRDA_CR);
1160 // T3 -> SD/MODE:1 IRTX:1
1162 outb(tmp, iobase+FIR_IRDA_CR);
1165 // T4 -> SD/MODE:0 IRTX:1
1168 outb(tmp, iobase+FIR_IRDA_CR);
1171 // T5 -> SD/MODE:0 IRTX:0
1174 outb(tmp, iobase+FIR_IRDA_CR);
1177 // reset -> Normal TX output Signal
1178 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1180 else /* speed <=1152000 */
1188 /* MIR 115200, 57600 */
1191 tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
1195 tmp &=~0x80; //HDLC 0.576Mbps
1196 tmp |= 0x20; //HDLC=1,
1199 tmp |= IRDA_CR_CRC; // CRC=1
1201 switch_bank(iobase, BANK2);
1202 outb(tmp, iobase+FIR_IRDA_CR);
1204 /* MIR 115200, 57600 */
1206 //switch_bank(iobase, BANK2);
1207 // T1 -> SD/MODE:0 IRTX:0
1210 outb(tmp, iobase+FIR_IRDA_CR);
1213 // T2 -> SD/MODE:1 IRTX:0
1216 outb(tmp, iobase+FIR_IRDA_CR);
1218 // T3 -> SD/MODE:0 IRTX:0
1221 outb(tmp, iobase+FIR_IRDA_CR);
1224 // reset -> Normal TX output Signal
1225 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1228 else if (dongle_id == 1) /* HP HDSL-3600 */
1233 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1237 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1241 tmp &=~0x80; // HDLC 0.576Mbps
1242 tmp |= 0x20; // HDLC=1,
1246 tmp |= IRDA_CR_CRC; // CRC=1
1248 switch_bank(iobase, BANK2);
1249 outb(tmp, iobase+FIR_IRDA_CR);
1251 else /* HP HDSL-1100 */
1253 if(speed <= 115200) /* SIR */
1256 tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
1258 switch_bank(iobase, BANK2);
1259 outb(tmp, iobase+FIR_IRDA_CR);
1267 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1271 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1275 tmp &=~0x80; // HDLC 0.576Mbps
1276 tmp |= 0x20; // HDLC=1,
1280 tmp |= IRDA_CR_CRC; // CRC=1
1281 tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
1283 switch_bank(iobase, BANK2);
1284 outb(tmp, iobase+FIR_IRDA_CR);
1288 switch_bank(iobase, BANK0);
1290 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1294 * Function ali_ircc_sir_write (driver)
1296 * Fill Tx FIFO with transmit data
1299 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
1303 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1305 /* Tx FIFO should be empty! */
1306 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1307 IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __FUNCTION__ );
1311 /* Fill FIFO with current frame */
1312 while ((fifo_size-- > 0) && (actual < len)) {
1313 /* Transmit next byte */
1314 outb(buf[actual], iobase+UART_TX);
1319 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1324 * Function ali_ircc_net_open (dev)
1329 static int ali_ircc_net_open(struct net_device *dev)
1331 struct ali_ircc_cb *self;
1335 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1337 IRDA_ASSERT(dev != NULL, return -1;);
1339 self = (struct ali_ircc_cb *) dev->priv;
1341 IRDA_ASSERT(self != NULL, return 0;);
1343 iobase = self->io.fir_base;
1345 /* Request IRQ and install Interrupt Handler */
1346 if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev))
1348 IRDA_WARNING("%s, unable to allocate irq=%d\n",
1349 ALI_IRCC_DRIVER_NAME,
1355 * Always allocate the DMA channel after the IRQ, and clean up on
1358 if (request_dma(self->io.dma, dev->name)) {
1359 IRDA_WARNING("%s, unable to allocate dma=%d\n",
1360 ALI_IRCC_DRIVER_NAME,
1362 free_irq(self->io.irq, self);
1366 /* Turn on interrups */
1367 outb(UART_IER_RDI , iobase+UART_IER);
1369 /* Ready to play! */
1370 netif_start_queue(dev); //benjamin by irport
1372 /* Give self a hardware name */
1373 sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
1376 * Open new IrLAP layer instance, now that everything should be
1377 * initialized properly
1379 self->irlap = irlap_open(dev, &self->qos, hwname);
1381 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1387 * Function ali_ircc_net_close (dev)
1392 static int ali_ircc_net_close(struct net_device *dev)
1395 struct ali_ircc_cb *self;
1398 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1400 IRDA_ASSERT(dev != NULL, return -1;);
1402 self = (struct ali_ircc_cb *) dev->priv;
1403 IRDA_ASSERT(self != NULL, return 0;);
1406 netif_stop_queue(dev);
1408 /* Stop and remove instance of IrLAP */
1410 irlap_close(self->irlap);
1413 disable_dma(self->io.dma);
1415 /* Disable interrupts */
1416 SetCOMInterrupts(self, FALSE);
1418 free_irq(self->io.irq, dev);
1419 free_dma(self->io.dma);
1421 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1427 * Function ali_ircc_fir_hard_xmit (skb, dev)
1429 * Transmit the frame
1432 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1434 struct ali_ircc_cb *self;
1435 unsigned long flags;
1440 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1442 self = (struct ali_ircc_cb *) dev->priv;
1443 iobase = self->io.fir_base;
1445 netif_stop_queue(dev);
1447 /* Make sure tests *& speed change are atomic */
1448 spin_lock_irqsave(&self->lock, flags);
1450 /* Note : you should make sure that speed changes are not going
1451 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1452 * details - Jean II */
1454 /* Check if we need to change the speed */
1455 speed = irda_get_next_speed(skb);
1456 if ((speed != self->io.speed) && (speed != -1)) {
1457 /* Check for empty frame */
1459 ali_ircc_change_speed(self, speed);
1460 dev->trans_start = jiffies;
1461 spin_unlock_irqrestore(&self->lock, flags);
1465 self->new_speed = speed;
1468 /* Register and copy this frame to DMA memory */
1469 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1470 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1471 self->tx_fifo.tail += skb->len;
1473 self->stats.tx_bytes += skb->len;
1475 memcpy(self->tx_fifo.queue[self->tx_fifo.free].start, skb->data,
1478 self->tx_fifo.len++;
1479 self->tx_fifo.free++;
1481 /* Start transmit only if there is currently no transmit going on */
1482 if (self->tx_fifo.len == 1)
1484 /* Check if we must wait the min turn time or not */
1485 mtt = irda_get_mtt(skb);
1489 /* Check how much time we have used already */
1490 do_gettimeofday(&self->now);
1492 diff = self->now.tv_usec - self->stamp.tv_usec;
1493 /* self->stamp is set from ali_ircc_dma_receive_complete() */
1495 IRDA_DEBUG(1, "%s(), ******* diff = %d ******* \n", __FUNCTION__ , diff);
1500 /* Check if the mtt is larger than the time we have
1501 * already used by all the protocol processing
1508 * Use timer if delay larger than 1000 us, and
1509 * use udelay for smaller values which should
1514 /* Adjust for timer resolution */
1515 mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
1517 IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __FUNCTION__ , mtt);
1520 if (mtt == 1) /* 500 us */
1522 switch_bank(iobase, BANK1);
1523 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
1525 else if (mtt == 2) /* 1 ms */
1527 switch_bank(iobase, BANK1);
1528 outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
1530 else /* > 2ms -> 4ms */
1532 switch_bank(iobase, BANK1);
1533 outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
1538 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1539 self->io.direction = IO_XMIT;
1541 /* Enable timer interrupt */
1542 self->ier = IER_TIMER;
1543 SetCOMInterrupts(self, TRUE);
1545 /* Timer will take care of the rest */
1550 } // if (if (mtt > diff)
1553 /* Enable EOM interrupt */
1554 self->ier = IER_EOM;
1555 SetCOMInterrupts(self, TRUE);
1557 /* Transmit frame */
1558 ali_ircc_dma_xmit(self);
1559 } // if (self->tx_fifo.len == 1)
1563 /* Not busy transmitting anymore if window is not full */
1564 if (self->tx_fifo.free < MAX_TX_WINDOW)
1565 netif_wake_queue(self->netdev);
1567 /* Restore bank register */
1568 switch_bank(iobase, BANK0);
1570 dev->trans_start = jiffies;
1571 spin_unlock_irqrestore(&self->lock, flags);
1574 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1579 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
1582 unsigned char FIFO_OPTI, Hi, Lo;
1585 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1587 iobase = self->io.fir_base;
1589 /* FIFO threshold , this method comes from NDIS5 code */
1591 if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
1592 FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
1594 FIFO_OPTI = TX_FIFO_Threshold;
1597 switch_bank(iobase, BANK1);
1598 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1600 self->io.direction = IO_XMIT;
1602 irda_setup_dma(self->io.dma,
1603 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1604 self->tx_buff.head) + self->tx_buff_dma,
1605 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1609 switch_bank(iobase, BANK0);
1610 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1612 /* Set Tx FIFO threshold */
1613 if (self->fifo_opti_buf!=FIFO_OPTI)
1615 switch_bank(iobase, BANK1);
1616 outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
1617 self->fifo_opti_buf=FIFO_OPTI;
1620 /* Set Tx DMA threshold */
1621 switch_bank(iobase, BANK1);
1622 outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
1624 /* Set max Tx frame size */
1625 Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
1626 Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
1627 switch_bank(iobase, BANK2);
1628 outb(Hi, iobase+FIR_TX_DSR_HI);
1629 outb(Lo, iobase+FIR_TX_DSR_LO);
1631 /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
1632 switch_bank(iobase, BANK0);
1633 tmp = inb(iobase+FIR_LCR_B);
1634 tmp &= ~0x20; // Disable SIP
1635 outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
1636 IRDA_DEBUG(1, "%s(), ******* Change to TX mode: FIR_LCR_B = 0x%x ******* \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
1638 outb(0, iobase+FIR_LSR);
1640 /* Enable DMA and Burst Mode */
1641 switch_bank(iobase, BANK1);
1642 outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1644 switch_bank(iobase, BANK0);
1646 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1649 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
1654 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1656 iobase = self->io.fir_base;
1659 switch_bank(iobase, BANK1);
1660 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1662 /* Check for underrun! */
1663 switch_bank(iobase, BANK0);
1664 if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
1667 IRDA_ERROR("%s(), ********* LSR_FRAME_ABORT *********\n", __FUNCTION__);
1668 self->stats.tx_errors++;
1669 self->stats.tx_fifo_errors++;
1673 self->stats.tx_packets++;
1676 /* Check if we need to change the speed */
1677 if (self->new_speed)
1679 ali_ircc_change_speed(self, self->new_speed);
1680 self->new_speed = 0;
1683 /* Finished with this frame, so prepare for next */
1684 self->tx_fifo.ptr++;
1685 self->tx_fifo.len--;
1687 /* Any frames to be sent back-to-back? */
1688 if (self->tx_fifo.len)
1690 ali_ircc_dma_xmit(self);
1692 /* Not finished yet! */
1696 { /* Reset Tx FIFO info */
1697 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1698 self->tx_fifo.tail = self->tx_buff.head;
1701 /* Make sure we have room for more frames */
1702 if (self->tx_fifo.free < MAX_TX_WINDOW) {
1703 /* Not busy transmitting anymore */
1704 /* Tell the network layer, that we can accept more frames */
1705 netif_wake_queue(self->netdev);
1708 switch_bank(iobase, BANK0);
1710 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1715 * Function ali_ircc_dma_receive (self)
1717 * Get ready for receiving a frame. The device will initiate a DMA
1718 * if it starts to receive a frame.
1721 static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
1725 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1727 iobase = self->io.fir_base;
1729 /* Reset Tx FIFO info */
1730 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1731 self->tx_fifo.tail = self->tx_buff.head;
1734 switch_bank(iobase, BANK1);
1735 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1737 /* Reset Message Count */
1738 switch_bank(iobase, BANK0);
1739 outb(0x07, iobase+FIR_LSR);
1741 self->rcvFramesOverflow = FALSE;
1743 self->LineStatus = inb(iobase+FIR_LSR) ;
1745 /* Reset Rx FIFO info */
1746 self->io.direction = IO_RECV;
1747 self->rx_buff.data = self->rx_buff.head;
1750 // switch_bank(iobase, BANK0);
1751 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1753 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1754 self->st_fifo.tail = self->st_fifo.head = 0;
1756 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1759 /* Set Receive Mode,Brick Wall */
1760 //switch_bank(iobase, BANK0);
1761 tmp = inb(iobase+FIR_LCR_B);
1762 outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
1763 IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x *** \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
1765 /* Set Rx Threshold */
1766 switch_bank(iobase, BANK1);
1767 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
1768 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
1770 /* Enable DMA and Burst Mode */
1771 // switch_bank(iobase, BANK1);
1772 outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1774 switch_bank(iobase, BANK0);
1775 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1779 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
1781 struct st_fifo *st_fifo;
1782 struct sk_buff *skb;
1783 __u8 status, MessageCount;
1784 int len, i, iobase, val;
1786 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1788 st_fifo = &self->st_fifo;
1789 iobase = self->io.fir_base;
1791 switch_bank(iobase, BANK0);
1792 MessageCount = inb(iobase+ FIR_LSR)&0x07;
1794 if (MessageCount > 0)
1795 IRDA_DEBUG(0, "%s(), Messsage count = %d,\n", __FUNCTION__ , MessageCount);
1797 for (i=0; i<=MessageCount; i++)
1800 switch_bank(iobase, BANK0);
1801 status = inb(iobase+FIR_LSR);
1803 switch_bank(iobase, BANK2);
1804 len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
1806 len |= inb(iobase+FIR_RX_DSR_LO);
1808 IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __FUNCTION__ , len);
1809 IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __FUNCTION__ , status);
1811 if (st_fifo->tail >= MAX_RX_WINDOW) {
1812 IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__ );
1816 st_fifo->entries[st_fifo->tail].status = status;
1817 st_fifo->entries[st_fifo->tail].len = len;
1818 st_fifo->pending_bytes += len;
1823 for (i=0; i<=MessageCount; i++)
1825 /* Get first entry */
1826 status = st_fifo->entries[st_fifo->head].status;
1827 len = st_fifo->entries[st_fifo->head].len;
1828 st_fifo->pending_bytes -= len;
1832 /* Check for errors */
1833 if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
1835 IRDA_DEBUG(0,"%s(), ************* RX Errors ************ \n", __FUNCTION__ );
1838 self->stats.rx_errors++;
1840 self->rx_buff.data += len;
1842 if (status & LSR_FIFO_UR)
1844 self->stats.rx_frame_errors++;
1845 IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************ \n", __FUNCTION__ );
1847 if (status & LSR_FRAME_ERROR)
1849 self->stats.rx_frame_errors++;
1850 IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************ \n", __FUNCTION__ );
1853 if (status & LSR_CRC_ERROR)
1855 self->stats.rx_crc_errors++;
1856 IRDA_DEBUG(0,"%s(), ************* CRC Errors ************ \n", __FUNCTION__ );
1859 if(self->rcvFramesOverflow)
1861 self->stats.rx_frame_errors++;
1862 IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************ \n", __FUNCTION__ );
1866 self->stats.rx_frame_errors++;
1867 IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 ********* \n", __FUNCTION__ );
1873 if (st_fifo->pending_bytes < 32)
1875 switch_bank(iobase, BANK0);
1876 val = inb(iobase+FIR_BSR);
1877 if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
1879 IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************ \n", __FUNCTION__ );
1881 /* Put this entry back in fifo */
1884 st_fifo->pending_bytes += len;
1885 st_fifo->entries[st_fifo->head].status = status;
1886 st_fifo->entries[st_fifo->head].len = len;
1889 * DMA not finished yet, so try again
1890 * later, set timer value, resolution
1894 switch_bank(iobase, BANK1);
1895 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
1898 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1900 return FALSE; /* I'll be back! */
1905 * Remember the time we received this frame, so we can
1906 * reduce the min turn time a bit since we will know
1907 * how much time we have used for protocol processing
1909 do_gettimeofday(&self->stamp);
1911 skb = dev_alloc_skb(len+1);
1914 IRDA_WARNING("%s(), memory squeeze, "
1915 "dropping frame.\n",
1917 self->stats.rx_dropped++;
1922 /* Make sure IP header gets aligned */
1923 skb_reserve(skb, 1);
1925 /* Copy frame without CRC, CRC is removed by hardware*/
1927 memcpy(skb->data, self->rx_buff.data, len);
1929 /* Move to next frame */
1930 self->rx_buff.data += len;
1931 self->stats.rx_bytes += len;
1932 self->stats.rx_packets++;
1934 skb->dev = self->netdev;
1935 skb->mac.raw = skb->data;
1936 skb->protocol = htons(ETH_P_IRDA);
1938 self->netdev->last_rx = jiffies;
1942 switch_bank(iobase, BANK0);
1944 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1951 * Function ali_ircc_sir_hard_xmit (skb, dev)
1953 * Transmit the frame!
1956 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1958 struct ali_ircc_cb *self;
1959 unsigned long flags;
1963 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1965 IRDA_ASSERT(dev != NULL, return 0;);
1967 self = (struct ali_ircc_cb *) dev->priv;
1968 IRDA_ASSERT(self != NULL, return 0;);
1970 iobase = self->io.sir_base;
1972 netif_stop_queue(dev);
1974 /* Make sure tests *& speed change are atomic */
1975 spin_lock_irqsave(&self->lock, flags);
1977 /* Note : you should make sure that speed changes are not going
1978 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1979 * details - Jean II */
1981 /* Check if we need to change the speed */
1982 speed = irda_get_next_speed(skb);
1983 if ((speed != self->io.speed) && (speed != -1)) {
1984 /* Check for empty frame */
1986 ali_ircc_change_speed(self, speed);
1987 dev->trans_start = jiffies;
1988 spin_unlock_irqrestore(&self->lock, flags);
1992 self->new_speed = speed;
1995 /* Init tx buffer */
1996 self->tx_buff.data = self->tx_buff.head;
1998 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
1999 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
2000 self->tx_buff.truesize);
2002 self->stats.tx_bytes += self->tx_buff.len;
2004 /* Turn on transmit finished interrupt. Will fire immediately! */
2005 outb(UART_IER_THRI, iobase+UART_IER);
2007 dev->trans_start = jiffies;
2008 spin_unlock_irqrestore(&self->lock, flags);
2012 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2019 * Function ali_ircc_net_ioctl (dev, rq, cmd)
2021 * Process IOCTL commands for this device
2024 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2026 struct if_irda_req *irq = (struct if_irda_req *) rq;
2027 struct ali_ircc_cb *self;
2028 unsigned long flags;
2031 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2033 IRDA_ASSERT(dev != NULL, return -1;);
2037 IRDA_ASSERT(self != NULL, return -1;);
2039 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__ , dev->name, cmd);
2042 case SIOCSBANDWIDTH: /* Set bandwidth */
2043 IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __FUNCTION__ );
2045 * This function will also be used by IrLAP to change the
2046 * speed, so we still must allow for speed change within
2047 * interrupt context.
2049 if (!in_interrupt() && !capable(CAP_NET_ADMIN))
2052 spin_lock_irqsave(&self->lock, flags);
2053 ali_ircc_change_speed(self, irq->ifr_baudrate);
2054 spin_unlock_irqrestore(&self->lock, flags);
2056 case SIOCSMEDIABUSY: /* Set media busy */
2057 IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __FUNCTION__ );
2058 if (!capable(CAP_NET_ADMIN))
2060 irda_device_set_media_busy(self->netdev, TRUE);
2062 case SIOCGRECEIVING: /* Check if we are receiving right now */
2063 IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __FUNCTION__ );
2064 /* This is protected */
2065 irq->ifr_receiving = ali_ircc_is_receiving(self);
2071 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2077 * Function ali_ircc_is_receiving (self)
2079 * Return TRUE is we are currently receiving a frame
2082 static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
2084 unsigned long flags;
2088 IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
2090 IRDA_ASSERT(self != NULL, return FALSE;);
2092 spin_lock_irqsave(&self->lock, flags);
2094 if (self->io.speed > 115200)
2096 iobase = self->io.fir_base;
2098 switch_bank(iobase, BANK1);
2099 if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)
2101 /* We are receiving something */
2102 IRDA_DEBUG(1, "%s(), We are receiving something\n", __FUNCTION__ );
2105 switch_bank(iobase, BANK0);
2109 status = (self->rx_buff.state != OUTSIDE_FRAME);
2112 spin_unlock_irqrestore(&self->lock, flags);
2114 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2119 static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev)
2121 struct ali_ircc_cb *self = (struct ali_ircc_cb *) dev->priv;
2123 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2125 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2127 return &self->stats;
2130 static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
2132 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2134 IRDA_MESSAGE("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
2136 if (self->io.suspended)
2139 ali_ircc_net_close(self->netdev);
2141 self->io.suspended = 1;
2146 static int ali_ircc_resume(struct platform_device *dev)
2148 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2150 if (!self->io.suspended)
2153 ali_ircc_net_open(self->netdev);
2155 IRDA_MESSAGE("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
2157 self->io.suspended = 0;
2162 /* ALi Chip Function */
2164 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
2167 unsigned char newMask;
2169 int iobase = self->io.fir_base; /* or sir_base */
2171 IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __FUNCTION__ , enable);
2173 /* Enable the interrupt which we wish to */
2175 if (self->io.direction == IO_XMIT)
2177 if (self->io.speed > 115200) /* FIR, MIR */
2179 newMask = self->ier;
2183 newMask = UART_IER_THRI | UART_IER_RDI;
2187 if (self->io.speed > 115200) /* FIR, MIR */
2189 newMask = self->ier;
2193 newMask = UART_IER_RDI;
2197 else /* Disable all the interrupts */
2203 //SIR and FIR has different registers
2204 if (self->io.speed > 115200)
2206 switch_bank(iobase, BANK0);
2207 outb(newMask, iobase+FIR_IER);
2210 outb(newMask, iobase+UART_IER);
2212 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2215 static void SIR2FIR(int iobase)
2217 //unsigned char tmp;
2219 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2221 /* Already protected (change_speed() or setup()), no need to lock.
2224 outb(0x28, iobase+UART_MCR);
2225 outb(0x68, iobase+UART_MCR);
2226 outb(0x88, iobase+UART_MCR);
2228 outb(0x60, iobase+FIR_MCR); /* Master Reset */
2229 outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
2231 //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
2233 //outb(tmp, iobase+FIR_LCR_B);
2235 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2238 static void FIR2SIR(int iobase)
2242 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2244 /* Already protected (change_speed() or setup()), no need to lock.
2247 outb(0x20, iobase+FIR_MCR); /* IRQ to low */
2248 outb(0x00, iobase+UART_IER);
2250 outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
2251 outb(0x00, iobase+UART_FCR);
2252 outb(0x07, iobase+UART_FCR);
2254 val = inb(iobase+UART_RX);
2255 val = inb(iobase+UART_LSR);
2256 val = inb(iobase+UART_MSR);
2258 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2261 MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
2262 MODULE_DESCRIPTION("ALi FIR Controller Driver");
2263 MODULE_LICENSE("GPL");
2266 module_param_array(io, int, NULL, 0);
2267 MODULE_PARM_DESC(io, "Base I/O addresses");
2268 module_param_array(irq, int, NULL, 0);
2269 MODULE_PARM_DESC(irq, "IRQ lines");
2270 module_param_array(dma, int, NULL, 0);
2271 MODULE_PARM_DESC(dma, "DMA channels");
2273 module_init(ali_ircc_init);
2274 module_exit(ali_ircc_cleanup);