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,
67 /* Module parameters */
68 static int qos_mtt_bits = 0x07; /* 1 ms or more */
70 /* Use BIOS settions by default, but user may supply module parameters */
71 static unsigned int io[] = { ~0, ~0, ~0, ~0 };
72 static unsigned int irq[] = { 0, 0, 0, 0 };
73 static unsigned int dma[] = { 0, 0, 0, 0 };
75 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
76 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
77 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
79 /* These are the currently known ALi sourth-bridge chipsets, the only one difference
80 * is that M1543C doesn't support HP HDSL-3600
82 static ali_chip_t chips[] =
84 { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
85 { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
86 { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
90 /* Max 4 instances for now */
91 static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
94 static char *dongle_types[] = {
98 "No dongle connected",
101 /* Some prototypes */
102 static int ali_ircc_open(int i, chipio_t *info);
104 static int ali_ircc_close(struct ali_ircc_cb *self);
106 static int ali_ircc_setup(chipio_t *info);
107 static int ali_ircc_is_receiving(struct ali_ircc_cb *self);
108 static int ali_ircc_net_open(struct net_device *dev);
109 static int ali_ircc_net_close(struct net_device *dev);
110 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
111 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
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", __func__);
156 ret = platform_driver_register(&ali_ircc_driver);
158 IRDA_ERROR("%s, Can't register driver!\n",
159 ALI_IRCC_DRIVER_NAME);
165 /* Probe for all the ALi chipsets we know about */
166 for (chip= chips; chip->name; chip++, i++)
168 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __func__, chip->name);
170 /* Try all config registers for this chip */
171 for (cfg=0; cfg<2; cfg++)
173 cfg_base = chip->cfg[cfg];
177 memset(&info, 0, sizeof(chipio_t));
178 info.cfg_base = cfg_base;
179 info.fir_base = io[i];
184 /* Enter Configuration */
185 outb(chip->entr1, cfg_base);
186 outb(chip->entr2, cfg_base);
188 /* Select Logical Device 5 Registers (UART2) */
189 outb(0x07, cfg_base);
190 outb(0x05, cfg_base+1);
192 /* Read Chip Identification Register */
193 outb(chip->cid_index, cfg_base);
194 reg = inb(cfg_base+1);
196 if (reg == chip->cid_value)
198 IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __func__, cfg_base);
200 outb(0x1F, cfg_base);
201 revision = inb(cfg_base+1);
202 IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __func__,
203 chip->name, revision);
206 * If the user supplies the base address, then
207 * we init the chip, if not we probe the values
212 chip->init(chip, &info);
216 chip->probe(chip, &info);
219 if (ali_ircc_open(i, &info) == 0)
225 IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __func__, chip->name, cfg_base);
227 /* Exit configuration */
228 outb(0xbb, cfg_base);
232 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
235 platform_driver_unregister(&ali_ircc_driver);
241 * Function ali_ircc_cleanup ()
243 * Close all configured chips
246 static void __exit ali_ircc_cleanup(void)
250 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
252 for (i=0; i < ARRAY_SIZE(dev_self); i++) {
254 ali_ircc_close(dev_self[i]);
257 platform_driver_unregister(&ali_ircc_driver);
259 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
263 * Function ali_ircc_open (int i, chipio_t *inf)
265 * Open driver instance
268 static int ali_ircc_open(int i, chipio_t *info)
270 struct net_device *dev;
271 struct ali_ircc_cb *self;
275 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
277 if (i >= ARRAY_SIZE(dev_self)) {
278 IRDA_ERROR("%s(), maximum number of supported chips reached!\n",
283 /* Set FIR FIFO and DMA Threshold */
284 if ((ali_ircc_setup(info)) == -1)
287 dev = alloc_irdadev(sizeof(*self));
289 IRDA_ERROR("%s(), can't allocate memory for control block!\n",
294 self = netdev_priv(dev);
296 spin_lock_init(&self->lock);
298 /* Need to store self somewhere */
303 self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
304 self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
305 self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
306 self->io.irq = info->irq;
307 self->io.fir_ext = CHIP_IO_EXTENT;
308 self->io.dma = info->dma;
309 self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
311 /* Reserve the ioports that we need */
312 if (!request_region(self->io.fir_base, self->io.fir_ext,
313 ALI_IRCC_DRIVER_NAME)) {
314 IRDA_WARNING("%s(), can't get iobase of 0x%03x\n", __func__,
320 /* Initialize QoS for this device */
321 irda_init_max_qos_capabilies(&self->qos);
323 /* The only value we must override it the baudrate */
324 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
325 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
327 self->qos.min_turn_time.bits = qos_mtt_bits;
329 irda_qos_bits_to_value(&self->qos);
331 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
332 self->rx_buff.truesize = 14384;
333 self->tx_buff.truesize = 14384;
335 /* Allocate memory if needed */
337 dma_alloc_coherent(NULL, self->rx_buff.truesize,
338 &self->rx_buff_dma, GFP_KERNEL);
339 if (self->rx_buff.head == NULL) {
343 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
346 dma_alloc_coherent(NULL, self->tx_buff.truesize,
347 &self->tx_buff_dma, GFP_KERNEL);
348 if (self->tx_buff.head == NULL) {
352 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
354 self->rx_buff.in_frame = FALSE;
355 self->rx_buff.state = OUTSIDE_FRAME;
356 self->tx_buff.data = self->tx_buff.head;
357 self->rx_buff.data = self->rx_buff.head;
359 /* Reset Tx queue info */
360 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
361 self->tx_fifo.tail = self->tx_buff.head;
363 /* Override the network functions we need to use */
364 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
365 dev->open = ali_ircc_net_open;
366 dev->stop = ali_ircc_net_close;
367 dev->do_ioctl = ali_ircc_net_ioctl;
369 err = register_netdev(dev);
371 IRDA_ERROR("%s(), register_netdev() failed!\n", __func__);
374 IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
376 /* Check dongle id */
377 dongle_id = ali_ircc_read_dongle_id(i, info);
378 IRDA_MESSAGE("%s(), %s, Found dongle: %s\n", __func__,
379 ALI_IRCC_DRIVER_NAME, dongle_types[dongle_id]);
381 self->io.dongle_id = dongle_id;
383 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
388 dma_free_coherent(NULL, self->tx_buff.truesize,
389 self->tx_buff.head, self->tx_buff_dma);
391 dma_free_coherent(NULL, self->rx_buff.truesize,
392 self->rx_buff.head, self->rx_buff_dma);
394 release_region(self->io.fir_base, self->io.fir_ext);
403 * Function ali_ircc_close (self)
405 * Close driver instance
408 static int __exit ali_ircc_close(struct ali_ircc_cb *self)
412 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __func__);
414 IRDA_ASSERT(self != NULL, return -1;);
416 iobase = self->io.fir_base;
418 /* Remove netdevice */
419 unregister_netdev(self->netdev);
421 /* Release the PORT that this driver is using */
422 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __func__, self->io.fir_base);
423 release_region(self->io.fir_base, self->io.fir_ext);
425 if (self->tx_buff.head)
426 dma_free_coherent(NULL, self->tx_buff.truesize,
427 self->tx_buff.head, self->tx_buff_dma);
429 if (self->rx_buff.head)
430 dma_free_coherent(NULL, self->rx_buff.truesize,
431 self->rx_buff.head, self->rx_buff_dma);
433 dev_self[self->index] = NULL;
434 free_netdev(self->netdev);
436 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
442 * Function ali_ircc_init_43 (chip, info)
444 * Initialize the ALi M1543 chip.
446 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
448 /* All controller information like I/O address, DMA channel, IRQ
456 * Function ali_ircc_init_53 (chip, info)
458 * Initialize the ALi M1535 chip.
460 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
462 /* All controller information like I/O address, DMA channel, IRQ
470 * Function ali_ircc_probe_53 (chip, info)
472 * Probes for the ALi M1535D or M1535
474 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
476 int cfg_base = info->cfg_base;
479 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
481 /* Enter Configuration */
482 outb(chip->entr1, cfg_base);
483 outb(chip->entr2, cfg_base);
485 /* Select Logical Device 5 Registers (UART2) */
486 outb(0x07, cfg_base);
487 outb(0x05, cfg_base+1);
489 /* Read address control register */
490 outb(0x60, cfg_base);
491 hi = inb(cfg_base+1);
492 outb(0x61, cfg_base);
493 low = inb(cfg_base+1);
494 info->fir_base = (hi<<8) + low;
496 info->sir_base = info->fir_base;
498 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __func__, info->fir_base);
500 /* Read IRQ control register */
501 outb(0x70, cfg_base);
502 reg = inb(cfg_base+1);
503 info->irq = reg & 0x0f;
504 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __func__, info->irq);
506 /* Read DMA channel */
507 outb(0x74, cfg_base);
508 reg = inb(cfg_base+1);
509 info->dma = reg & 0x07;
511 if(info->dma == 0x04)
512 IRDA_WARNING("%s(), No DMA channel assigned !\n", __func__);
514 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __func__, info->dma);
516 /* Read Enabled Status */
517 outb(0x30, cfg_base);
518 reg = inb(cfg_base+1);
519 info->enabled = (reg & 0x80) && (reg & 0x01);
520 IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __func__, info->enabled);
522 /* Read Power Status */
523 outb(0x22, cfg_base);
524 reg = inb(cfg_base+1);
525 info->suspended = (reg & 0x20);
526 IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __func__, info->suspended);
528 /* Exit configuration */
529 outb(0xbb, cfg_base);
531 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
537 * Function ali_ircc_setup (info)
539 * Set FIR FIFO and DMA Threshold
540 * Returns non-negative on success.
543 static int ali_ircc_setup(chipio_t *info)
547 int iobase = info->fir_base;
549 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
551 /* Locking comments :
552 * Most operations here need to be protected. We are called before
553 * the device instance is created in ali_ircc_open(), therefore
554 * nobody can bother us - Jean II */
556 /* Switch to FIR space */
560 outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
562 /* Read FIR ID Version Register */
563 switch_bank(iobase, BANK3);
564 version = inb(iobase+FIR_ID_VR);
566 /* Should be 0x00 in the M1535/M1535D */
569 IRDA_ERROR("%s, Wrong chip version %02x\n",
570 ALI_IRCC_DRIVER_NAME, version);
574 /* Set FIR FIFO Threshold Register */
575 switch_bank(iobase, BANK1);
576 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
578 /* Set FIR DMA Threshold Register */
579 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
582 switch_bank(iobase, BANK2);
583 outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
585 /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
587 /* Switch to Bank 0 */
588 switch_bank(iobase, BANK0);
590 tmp = inb(iobase+FIR_LCR_B);
591 tmp &=~0x20; // disable SIP
592 tmp |= 0x80; // these two steps make RX mode
594 outb(tmp, iobase+FIR_LCR_B);
596 /* Disable Interrupt */
597 outb(0x00, iobase+FIR_IER);
600 /* Switch to SIR space */
603 IRDA_MESSAGE("%s, driver loaded (Benjamin Kong)\n",
604 ALI_IRCC_DRIVER_NAME);
606 /* Enable receive interrupts */
607 // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
608 // Turn on the interrupts in ali_ircc_net_open
610 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
616 * Function ali_ircc_read_dongle_id (int index, info)
618 * Try to read dongle indentification. This procedure needs to be executed
619 * once after power-on/reset. It also needs to be used whenever you suspect
620 * that the user may have plugged/unplugged the IrDA Dongle.
622 static int ali_ircc_read_dongle_id (int i, chipio_t *info)
625 int cfg_base = info->cfg_base;
627 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
629 /* Enter Configuration */
630 outb(chips[i].entr1, cfg_base);
631 outb(chips[i].entr2, cfg_base);
633 /* Select Logical Device 5 Registers (UART2) */
634 outb(0x07, cfg_base);
635 outb(0x05, cfg_base+1);
638 outb(0xf0, cfg_base);
639 reg = inb(cfg_base+1);
640 dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
641 IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __func__,
642 dongle_id, dongle_types[dongle_id]);
644 /* Exit configuration */
645 outb(0xbb, cfg_base);
647 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
653 * Function ali_ircc_interrupt (irq, dev_id, regs)
655 * An interrupt from the chip has arrived. Time to do some work
658 static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id)
660 struct net_device *dev = dev_id;
661 struct ali_ircc_cb *self;
664 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
666 self = netdev_priv(dev);
668 spin_lock(&self->lock);
670 /* Dispatch interrupt handler for the current speed */
671 if (self->io.speed > 115200)
672 ret = ali_ircc_fir_interrupt(self);
674 ret = ali_ircc_sir_interrupt(self);
676 spin_unlock(&self->lock);
678 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
682 * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
684 * Handle MIR/FIR interrupt
687 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
689 __u8 eir, OldMessageCount;
692 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__);
694 iobase = self->io.fir_base;
696 switch_bank(iobase, BANK0);
697 self->InterruptID = inb(iobase+FIR_IIR);
698 self->BusStatus = inb(iobase+FIR_BSR);
700 OldMessageCount = (self->LineStatus + 1) & 0x07;
701 self->LineStatus = inb(iobase+FIR_LSR);
702 //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
703 eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
705 IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __func__,self->InterruptID);
706 IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __func__,self->LineStatus);
707 IRDA_DEBUG(1, "%s(), self->ier = %x\n", __func__,self->ier);
708 IRDA_DEBUG(1, "%s(), eir = %x\n", __func__,eir);
710 /* Disable interrupts */
711 SetCOMInterrupts(self, FALSE);
713 /* Tx or Rx Interrupt */
717 if (self->io.direction == IO_XMIT) /* TX */
719 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __func__);
721 if(ali_ircc_dma_xmit_complete(self))
723 if (irda_device_txqueue_empty(self->netdev))
725 /* Prepare for receive */
726 ali_ircc_dma_receive(self);
738 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __func__);
740 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
742 self->rcvFramesOverflow = TRUE;
743 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******** \n", __func__);
746 if (ali_ircc_dma_receive_complete(self))
748 IRDA_DEBUG(1, "%s(), ******* receive complete ******** \n", __func__);
754 IRDA_DEBUG(1, "%s(), ******* Not receive complete ******** \n", __func__);
756 self->ier = IER_EOM | IER_TIMER;
761 /* Timer Interrupt */
762 else if (eir & IIR_TIMER)
764 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
766 self->rcvFramesOverflow = TRUE;
767 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******* \n", __func__);
770 switch_bank(iobase, BANK1);
771 tmp = inb(iobase+FIR_CR);
772 outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
774 /* Check if this is a Tx timer interrupt */
775 if (self->io.direction == IO_XMIT)
777 ali_ircc_dma_xmit(self);
779 /* Interrupt on EOM */
785 if(ali_ircc_dma_receive_complete(self))
791 self->ier = IER_EOM | IER_TIMER;
796 /* Restore Interrupt */
797 SetCOMInterrupts(self, TRUE);
799 IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __func__);
800 return IRQ_RETVAL(eir);
804 * Function ali_ircc_sir_interrupt (irq, self, eir)
806 * Handle SIR interrupt
809 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
814 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
816 iobase = self->io.sir_base;
818 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
820 /* Clear interrupt */
821 lsr = inb(iobase+UART_LSR);
823 IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __func__,
829 IRDA_DEBUG(2, "%s(), RLSI\n", __func__);
832 /* Receive interrupt */
833 ali_ircc_sir_receive(self);
836 if (lsr & UART_LSR_THRE)
838 /* Transmitter ready for data */
839 ali_ircc_sir_write_wakeup(self);
843 IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __func__, iir);
850 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
852 return IRQ_RETVAL(iir);
857 * Function ali_ircc_sir_receive (self)
859 * Receive one frame from the infrared port
862 static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
867 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
868 IRDA_ASSERT(self != NULL, return;);
870 iobase = self->io.sir_base;
873 * Receive all characters in Rx FIFO, unwrap and unstuff them.
874 * async_unwrap_char will deliver all found frames
877 async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
878 inb(iobase+UART_RX));
880 /* Make sure we don't stay here too long */
881 if (boguscount++ > 32) {
882 IRDA_DEBUG(2,"%s(), breaking!\n", __func__);
885 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
887 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
891 * Function ali_ircc_sir_write_wakeup (tty)
893 * Called by the driver when there's room for more data. If we have
894 * more packets to send, we send them here.
897 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
902 IRDA_ASSERT(self != NULL, return;);
904 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
906 iobase = self->io.sir_base;
908 /* Finished with frame? */
909 if (self->tx_buff.len > 0)
911 /* Write data left in transmit buffer */
912 actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
913 self->tx_buff.data, self->tx_buff.len);
914 self->tx_buff.data += actual;
915 self->tx_buff.len -= actual;
921 /* We must wait until all data are gone */
922 while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
923 IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __func__ );
925 IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __func__ , self->new_speed);
926 ali_ircc_change_speed(self, self->new_speed);
929 // benjamin 2000/11/10 06:32PM
930 if (self->io.speed > 115200)
932 IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT \n", __func__ );
935 // SetCOMInterrupts(self, TRUE);
941 netif_wake_queue(self->netdev);
944 self->netdev->stats.tx_packets++;
946 /* Turn on receive interrupts */
947 outb(UART_IER_RDI, iobase+UART_IER);
950 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
953 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
955 struct net_device *dev = self->netdev;
958 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
960 IRDA_DEBUG(2, "%s(), setting speed = %d \n", __func__ , baud);
962 /* This function *must* be called with irq off and spin-lock.
965 iobase = self->io.fir_base;
967 SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
969 /* Go to MIR, FIR Speed */
974 ali_ircc_fir_change_speed(self, baud);
976 /* Install FIR xmit handler*/
977 dev->hard_start_xmit = ali_ircc_fir_hard_xmit;
979 /* Enable Interuupt */
980 self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
982 /* Be ready for incomming frames */
983 ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
985 /* Go to SIR Speed */
988 ali_ircc_sir_change_speed(self, baud);
990 /* Install SIR xmit handler*/
991 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
995 SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
997 netif_wake_queue(self->netdev);
999 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
1002 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
1006 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1007 struct net_device *dev;
1009 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
1011 IRDA_ASSERT(self != NULL, return;);
1014 iobase = self->io.fir_base;
1016 IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __func__ ,self->io.speed,baud);
1018 /* Come from SIR speed */
1019 if(self->io.speed <=115200)
1024 /* Update accounting for new speed */
1025 self->io.speed = baud;
1027 // Set Dongle Speed mode
1028 ali_ircc_change_dongle_speed(self, baud);
1030 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1034 * Function ali_sir_change_speed (self, speed)
1036 * Set speed of IrDA port to specified baudrate
1039 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
1041 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1042 unsigned long flags;
1044 int fcr; /* FIFO control reg */
1045 int lcr; /* Line control reg */
1048 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
1050 IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __func__ , speed);
1052 IRDA_ASSERT(self != NULL, return;);
1054 iobase = self->io.sir_base;
1056 /* Come from MIR or FIR speed */
1057 if(self->io.speed >115200)
1059 // Set Dongle Speed mode first
1060 ali_ircc_change_dongle_speed(self, speed);
1065 // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
1067 inb(iobase+UART_LSR);
1068 inb(iobase+UART_SCR);
1070 /* Update accounting for new speed */
1071 self->io.speed = speed;
1073 spin_lock_irqsave(&self->lock, flags);
1075 divisor = 115200/speed;
1077 fcr = UART_FCR_ENABLE_FIFO;
1080 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
1081 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1082 * about this timeout since it will always be fast enough.
1084 if (self->io.speed < 38400)
1085 fcr |= UART_FCR_TRIGGER_1;
1087 fcr |= UART_FCR_TRIGGER_14;
1089 /* IrDA ports use 8N1 */
1090 lcr = UART_LCR_WLEN8;
1092 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
1093 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
1094 outb(divisor >> 8, iobase+UART_DLM);
1095 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
1096 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
1098 /* without this, the conection will be broken after come back from FIR speed,
1099 but with this, the SIR connection is harder to established */
1100 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
1102 spin_unlock_irqrestore(&self->lock, flags);
1104 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1107 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
1110 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1111 int iobase,dongle_id;
1114 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
1116 iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
1117 dongle_id = self->io.dongle_id;
1119 /* We are already locked, no need to do it again */
1121 IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __func__ , dongle_types[dongle_id], speed);
1123 switch_bank(iobase, BANK2);
1124 tmp = inb(iobase+FIR_IRDA_CR);
1126 /* IBM type dongle */
1129 if(speed == 4000000)
1132 // SD/MODE __| |__ __
1137 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1138 tmp |= IRDA_CR_CRC; // CRC=1
1140 switch_bank(iobase, BANK2);
1141 outb(tmp, iobase+FIR_IRDA_CR);
1143 // T1 -> SD/MODE:0 IRTX:0
1146 outb(tmp, iobase+FIR_IRDA_CR);
1149 // T2 -> SD/MODE:1 IRTX:0
1152 outb(tmp, iobase+FIR_IRDA_CR);
1155 // T3 -> SD/MODE:1 IRTX:1
1157 outb(tmp, iobase+FIR_IRDA_CR);
1160 // T4 -> SD/MODE:0 IRTX:1
1163 outb(tmp, iobase+FIR_IRDA_CR);
1166 // T5 -> SD/MODE:0 IRTX:0
1169 outb(tmp, iobase+FIR_IRDA_CR);
1172 // reset -> Normal TX output Signal
1173 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1175 else /* speed <=1152000 */
1183 /* MIR 115200, 57600 */
1186 tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
1190 tmp &=~0x80; //HDLC 0.576Mbps
1191 tmp |= 0x20; //HDLC=1,
1194 tmp |= IRDA_CR_CRC; // CRC=1
1196 switch_bank(iobase, BANK2);
1197 outb(tmp, iobase+FIR_IRDA_CR);
1199 /* MIR 115200, 57600 */
1201 //switch_bank(iobase, BANK2);
1202 // T1 -> SD/MODE:0 IRTX:0
1205 outb(tmp, iobase+FIR_IRDA_CR);
1208 // T2 -> SD/MODE:1 IRTX:0
1211 outb(tmp, iobase+FIR_IRDA_CR);
1213 // T3 -> SD/MODE:0 IRTX:0
1216 outb(tmp, iobase+FIR_IRDA_CR);
1219 // reset -> Normal TX output Signal
1220 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1223 else if (dongle_id == 1) /* HP HDSL-3600 */
1228 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1232 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1236 tmp &=~0x80; // HDLC 0.576Mbps
1237 tmp |= 0x20; // HDLC=1,
1241 tmp |= IRDA_CR_CRC; // CRC=1
1243 switch_bank(iobase, BANK2);
1244 outb(tmp, iobase+FIR_IRDA_CR);
1246 else /* HP HDSL-1100 */
1248 if(speed <= 115200) /* SIR */
1251 tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
1253 switch_bank(iobase, BANK2);
1254 outb(tmp, iobase+FIR_IRDA_CR);
1262 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1266 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1270 tmp &=~0x80; // HDLC 0.576Mbps
1271 tmp |= 0x20; // HDLC=1,
1275 tmp |= IRDA_CR_CRC; // CRC=1
1276 tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
1278 switch_bank(iobase, BANK2);
1279 outb(tmp, iobase+FIR_IRDA_CR);
1283 switch_bank(iobase, BANK0);
1285 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1289 * Function ali_ircc_sir_write (driver)
1291 * Fill Tx FIFO with transmit data
1294 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
1298 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
1300 /* Tx FIFO should be empty! */
1301 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1302 IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __func__ );
1306 /* Fill FIFO with current frame */
1307 while ((fifo_size-- > 0) && (actual < len)) {
1308 /* Transmit next byte */
1309 outb(buf[actual], iobase+UART_TX);
1314 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
1319 * Function ali_ircc_net_open (dev)
1324 static int ali_ircc_net_open(struct net_device *dev)
1326 struct ali_ircc_cb *self;
1330 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
1332 IRDA_ASSERT(dev != NULL, return -1;);
1334 self = netdev_priv(dev);
1336 IRDA_ASSERT(self != NULL, return 0;);
1338 iobase = self->io.fir_base;
1340 /* Request IRQ and install Interrupt Handler */
1341 if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev))
1343 IRDA_WARNING("%s, unable to allocate irq=%d\n",
1344 ALI_IRCC_DRIVER_NAME,
1350 * Always allocate the DMA channel after the IRQ, and clean up on
1353 if (request_dma(self->io.dma, dev->name)) {
1354 IRDA_WARNING("%s, unable to allocate dma=%d\n",
1355 ALI_IRCC_DRIVER_NAME,
1357 free_irq(self->io.irq, self);
1361 /* Turn on interrups */
1362 outb(UART_IER_RDI , iobase+UART_IER);
1364 /* Ready to play! */
1365 netif_start_queue(dev); //benjamin by irport
1367 /* Give self a hardware name */
1368 sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
1371 * Open new IrLAP layer instance, now that everything should be
1372 * initialized properly
1374 self->irlap = irlap_open(dev, &self->qos, hwname);
1376 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
1382 * Function ali_ircc_net_close (dev)
1387 static int ali_ircc_net_close(struct net_device *dev)
1390 struct ali_ircc_cb *self;
1393 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __func__ );
1395 IRDA_ASSERT(dev != NULL, return -1;);
1397 self = netdev_priv(dev);
1398 IRDA_ASSERT(self != NULL, return 0;);
1401 netif_stop_queue(dev);
1403 /* Stop and remove instance of IrLAP */
1405 irlap_close(self->irlap);
1408 disable_dma(self->io.dma);
1410 /* Disable interrupts */
1411 SetCOMInterrupts(self, FALSE);
1413 free_irq(self->io.irq, dev);
1414 free_dma(self->io.dma);
1416 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
1422 * Function ali_ircc_fir_hard_xmit (skb, dev)
1424 * Transmit the frame
1427 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1429 struct ali_ircc_cb *self;
1430 unsigned long flags;
1435 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1437 self = netdev_priv(dev);
1438 iobase = self->io.fir_base;
1440 netif_stop_queue(dev);
1442 /* Make sure tests *& speed change are atomic */
1443 spin_lock_irqsave(&self->lock, flags);
1445 /* Note : you should make sure that speed changes are not going
1446 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1447 * details - Jean II */
1449 /* Check if we need to change the speed */
1450 speed = irda_get_next_speed(skb);
1451 if ((speed != self->io.speed) && (speed != -1)) {
1452 /* Check for empty frame */
1454 ali_ircc_change_speed(self, speed);
1455 dev->trans_start = jiffies;
1456 spin_unlock_irqrestore(&self->lock, flags);
1460 self->new_speed = speed;
1463 /* Register and copy this frame to DMA memory */
1464 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1465 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1466 self->tx_fifo.tail += skb->len;
1468 dev->stats.tx_bytes += skb->len;
1470 skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
1472 self->tx_fifo.len++;
1473 self->tx_fifo.free++;
1475 /* Start transmit only if there is currently no transmit going on */
1476 if (self->tx_fifo.len == 1)
1478 /* Check if we must wait the min turn time or not */
1479 mtt = irda_get_mtt(skb);
1483 /* Check how much time we have used already */
1484 do_gettimeofday(&self->now);
1486 diff = self->now.tv_usec - self->stamp.tv_usec;
1487 /* self->stamp is set from ali_ircc_dma_receive_complete() */
1489 IRDA_DEBUG(1, "%s(), ******* diff = %d ******* \n", __func__ , diff);
1494 /* Check if the mtt is larger than the time we have
1495 * already used by all the protocol processing
1502 * Use timer if delay larger than 1000 us, and
1503 * use udelay for smaller values which should
1508 /* Adjust for timer resolution */
1509 mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
1511 IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __func__ , mtt);
1514 if (mtt == 1) /* 500 us */
1516 switch_bank(iobase, BANK1);
1517 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
1519 else if (mtt == 2) /* 1 ms */
1521 switch_bank(iobase, BANK1);
1522 outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
1524 else /* > 2ms -> 4ms */
1526 switch_bank(iobase, BANK1);
1527 outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
1532 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1533 self->io.direction = IO_XMIT;
1535 /* Enable timer interrupt */
1536 self->ier = IER_TIMER;
1537 SetCOMInterrupts(self, TRUE);
1539 /* Timer will take care of the rest */
1544 } // if (if (mtt > diff)
1547 /* Enable EOM interrupt */
1548 self->ier = IER_EOM;
1549 SetCOMInterrupts(self, TRUE);
1551 /* Transmit frame */
1552 ali_ircc_dma_xmit(self);
1553 } // if (self->tx_fifo.len == 1)
1557 /* Not busy transmitting anymore if window is not full */
1558 if (self->tx_fifo.free < MAX_TX_WINDOW)
1559 netif_wake_queue(self->netdev);
1561 /* Restore bank register */
1562 switch_bank(iobase, BANK0);
1564 dev->trans_start = jiffies;
1565 spin_unlock_irqrestore(&self->lock, flags);
1568 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1573 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
1576 unsigned char FIFO_OPTI, Hi, Lo;
1579 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1581 iobase = self->io.fir_base;
1583 /* FIFO threshold , this method comes from NDIS5 code */
1585 if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
1586 FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
1588 FIFO_OPTI = TX_FIFO_Threshold;
1591 switch_bank(iobase, BANK1);
1592 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1594 self->io.direction = IO_XMIT;
1596 irda_setup_dma(self->io.dma,
1597 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1598 self->tx_buff.head) + self->tx_buff_dma,
1599 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1603 switch_bank(iobase, BANK0);
1604 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1606 /* Set Tx FIFO threshold */
1607 if (self->fifo_opti_buf!=FIFO_OPTI)
1609 switch_bank(iobase, BANK1);
1610 outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
1611 self->fifo_opti_buf=FIFO_OPTI;
1614 /* Set Tx DMA threshold */
1615 switch_bank(iobase, BANK1);
1616 outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
1618 /* Set max Tx frame size */
1619 Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
1620 Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
1621 switch_bank(iobase, BANK2);
1622 outb(Hi, iobase+FIR_TX_DSR_HI);
1623 outb(Lo, iobase+FIR_TX_DSR_LO);
1625 /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
1626 switch_bank(iobase, BANK0);
1627 tmp = inb(iobase+FIR_LCR_B);
1628 tmp &= ~0x20; // Disable SIP
1629 outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
1630 IRDA_DEBUG(1, "%s(), ******* Change to TX mode: FIR_LCR_B = 0x%x ******* \n", __func__ , inb(iobase+FIR_LCR_B));
1632 outb(0, iobase+FIR_LSR);
1634 /* Enable DMA and Burst Mode */
1635 switch_bank(iobase, BANK1);
1636 outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1638 switch_bank(iobase, BANK0);
1640 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1643 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
1648 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1650 iobase = self->io.fir_base;
1653 switch_bank(iobase, BANK1);
1654 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1656 /* Check for underrun! */
1657 switch_bank(iobase, BANK0);
1658 if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
1661 IRDA_ERROR("%s(), ********* LSR_FRAME_ABORT *********\n", __func__);
1662 self->netdev->stats.tx_errors++;
1663 self->netdev->stats.tx_fifo_errors++;
1667 self->netdev->stats.tx_packets++;
1670 /* Check if we need to change the speed */
1671 if (self->new_speed)
1673 ali_ircc_change_speed(self, self->new_speed);
1674 self->new_speed = 0;
1677 /* Finished with this frame, so prepare for next */
1678 self->tx_fifo.ptr++;
1679 self->tx_fifo.len--;
1681 /* Any frames to be sent back-to-back? */
1682 if (self->tx_fifo.len)
1684 ali_ircc_dma_xmit(self);
1686 /* Not finished yet! */
1690 { /* Reset Tx FIFO info */
1691 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1692 self->tx_fifo.tail = self->tx_buff.head;
1695 /* Make sure we have room for more frames */
1696 if (self->tx_fifo.free < MAX_TX_WINDOW) {
1697 /* Not busy transmitting anymore */
1698 /* Tell the network layer, that we can accept more frames */
1699 netif_wake_queue(self->netdev);
1702 switch_bank(iobase, BANK0);
1704 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1709 * Function ali_ircc_dma_receive (self)
1711 * Get ready for receiving a frame. The device will initiate a DMA
1712 * if it starts to receive a frame.
1715 static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
1719 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1721 iobase = self->io.fir_base;
1723 /* Reset Tx FIFO info */
1724 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1725 self->tx_fifo.tail = self->tx_buff.head;
1728 switch_bank(iobase, BANK1);
1729 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1731 /* Reset Message Count */
1732 switch_bank(iobase, BANK0);
1733 outb(0x07, iobase+FIR_LSR);
1735 self->rcvFramesOverflow = FALSE;
1737 self->LineStatus = inb(iobase+FIR_LSR) ;
1739 /* Reset Rx FIFO info */
1740 self->io.direction = IO_RECV;
1741 self->rx_buff.data = self->rx_buff.head;
1744 // switch_bank(iobase, BANK0);
1745 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1747 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1748 self->st_fifo.tail = self->st_fifo.head = 0;
1750 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1753 /* Set Receive Mode,Brick Wall */
1754 //switch_bank(iobase, BANK0);
1755 tmp = inb(iobase+FIR_LCR_B);
1756 outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
1757 IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x *** \n", __func__ , inb(iobase+FIR_LCR_B));
1759 /* Set Rx Threshold */
1760 switch_bank(iobase, BANK1);
1761 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
1762 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
1764 /* Enable DMA and Burst Mode */
1765 // switch_bank(iobase, BANK1);
1766 outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1768 switch_bank(iobase, BANK0);
1769 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1773 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
1775 struct st_fifo *st_fifo;
1776 struct sk_buff *skb;
1777 __u8 status, MessageCount;
1778 int len, i, iobase, val;
1780 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
1782 st_fifo = &self->st_fifo;
1783 iobase = self->io.fir_base;
1785 switch_bank(iobase, BANK0);
1786 MessageCount = inb(iobase+ FIR_LSR)&0x07;
1788 if (MessageCount > 0)
1789 IRDA_DEBUG(0, "%s(), Messsage count = %d,\n", __func__ , MessageCount);
1791 for (i=0; i<=MessageCount; i++)
1794 switch_bank(iobase, BANK0);
1795 status = inb(iobase+FIR_LSR);
1797 switch_bank(iobase, BANK2);
1798 len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
1800 len |= inb(iobase+FIR_RX_DSR_LO);
1802 IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __func__ , len);
1803 IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __func__ , status);
1805 if (st_fifo->tail >= MAX_RX_WINDOW) {
1806 IRDA_DEBUG(0, "%s(), window is full!\n", __func__ );
1810 st_fifo->entries[st_fifo->tail].status = status;
1811 st_fifo->entries[st_fifo->tail].len = len;
1812 st_fifo->pending_bytes += len;
1817 for (i=0; i<=MessageCount; i++)
1819 /* Get first entry */
1820 status = st_fifo->entries[st_fifo->head].status;
1821 len = st_fifo->entries[st_fifo->head].len;
1822 st_fifo->pending_bytes -= len;
1826 /* Check for errors */
1827 if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
1829 IRDA_DEBUG(0,"%s(), ************* RX Errors ************ \n", __func__ );
1832 self->netdev->stats.rx_errors++;
1834 self->rx_buff.data += len;
1836 if (status & LSR_FIFO_UR)
1838 self->netdev->stats.rx_frame_errors++;
1839 IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************ \n", __func__ );
1841 if (status & LSR_FRAME_ERROR)
1843 self->netdev->stats.rx_frame_errors++;
1844 IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************ \n", __func__ );
1847 if (status & LSR_CRC_ERROR)
1849 self->netdev->stats.rx_crc_errors++;
1850 IRDA_DEBUG(0,"%s(), ************* CRC Errors ************ \n", __func__ );
1853 if(self->rcvFramesOverflow)
1855 self->netdev->stats.rx_frame_errors++;
1856 IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************ \n", __func__ );
1860 self->netdev->stats.rx_frame_errors++;
1861 IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 ********* \n", __func__ );
1867 if (st_fifo->pending_bytes < 32)
1869 switch_bank(iobase, BANK0);
1870 val = inb(iobase+FIR_BSR);
1871 if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
1873 IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************ \n", __func__ );
1875 /* Put this entry back in fifo */
1878 st_fifo->pending_bytes += len;
1879 st_fifo->entries[st_fifo->head].status = status;
1880 st_fifo->entries[st_fifo->head].len = len;
1883 * DMA not finished yet, so try again
1884 * later, set timer value, resolution
1888 switch_bank(iobase, BANK1);
1889 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
1892 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1894 return FALSE; /* I'll be back! */
1899 * Remember the time we received this frame, so we can
1900 * reduce the min turn time a bit since we will know
1901 * how much time we have used for protocol processing
1903 do_gettimeofday(&self->stamp);
1905 skb = dev_alloc_skb(len+1);
1908 IRDA_WARNING("%s(), memory squeeze, "
1909 "dropping frame.\n",
1911 self->netdev->stats.rx_dropped++;
1916 /* Make sure IP header gets aligned */
1917 skb_reserve(skb, 1);
1919 /* Copy frame without CRC, CRC is removed by hardware*/
1921 skb_copy_to_linear_data(skb, self->rx_buff.data, len);
1923 /* Move to next frame */
1924 self->rx_buff.data += len;
1925 self->netdev->stats.rx_bytes += len;
1926 self->netdev->stats.rx_packets++;
1928 skb->dev = self->netdev;
1929 skb_reset_mac_header(skb);
1930 skb->protocol = htons(ETH_P_IRDA);
1935 switch_bank(iobase, BANK0);
1937 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
1944 * Function ali_ircc_sir_hard_xmit (skb, dev)
1946 * Transmit the frame!
1949 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1951 struct ali_ircc_cb *self;
1952 unsigned long flags;
1956 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
1958 IRDA_ASSERT(dev != NULL, return 0;);
1960 self = netdev_priv(dev);
1961 IRDA_ASSERT(self != NULL, return 0;);
1963 iobase = self->io.sir_base;
1965 netif_stop_queue(dev);
1967 /* Make sure tests *& speed change are atomic */
1968 spin_lock_irqsave(&self->lock, flags);
1970 /* Note : you should make sure that speed changes are not going
1971 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1972 * details - Jean II */
1974 /* Check if we need to change the speed */
1975 speed = irda_get_next_speed(skb);
1976 if ((speed != self->io.speed) && (speed != -1)) {
1977 /* Check for empty frame */
1979 ali_ircc_change_speed(self, speed);
1980 dev->trans_start = jiffies;
1981 spin_unlock_irqrestore(&self->lock, flags);
1985 self->new_speed = speed;
1988 /* Init tx buffer */
1989 self->tx_buff.data = self->tx_buff.head;
1991 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
1992 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
1993 self->tx_buff.truesize);
1995 self->netdev->stats.tx_bytes += self->tx_buff.len;
1997 /* Turn on transmit finished interrupt. Will fire immediately! */
1998 outb(UART_IER_THRI, iobase+UART_IER);
2000 dev->trans_start = jiffies;
2001 spin_unlock_irqrestore(&self->lock, flags);
2005 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
2012 * Function ali_ircc_net_ioctl (dev, rq, cmd)
2014 * Process IOCTL commands for this device
2017 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2019 struct if_irda_req *irq = (struct if_irda_req *) rq;
2020 struct ali_ircc_cb *self;
2021 unsigned long flags;
2024 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
2026 IRDA_ASSERT(dev != NULL, return -1;);
2028 self = netdev_priv(dev);
2030 IRDA_ASSERT(self != NULL, return -1;);
2032 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
2035 case SIOCSBANDWIDTH: /* Set bandwidth */
2036 IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __func__ );
2038 * This function will also be used by IrLAP to change the
2039 * speed, so we still must allow for speed change within
2040 * interrupt context.
2042 if (!in_interrupt() && !capable(CAP_NET_ADMIN))
2045 spin_lock_irqsave(&self->lock, flags);
2046 ali_ircc_change_speed(self, irq->ifr_baudrate);
2047 spin_unlock_irqrestore(&self->lock, flags);
2049 case SIOCSMEDIABUSY: /* Set media busy */
2050 IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __func__ );
2051 if (!capable(CAP_NET_ADMIN))
2053 irda_device_set_media_busy(self->netdev, TRUE);
2055 case SIOCGRECEIVING: /* Check if we are receiving right now */
2056 IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __func__ );
2057 /* This is protected */
2058 irq->ifr_receiving = ali_ircc_is_receiving(self);
2064 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
2070 * Function ali_ircc_is_receiving (self)
2072 * Return TRUE is we are currently receiving a frame
2075 static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
2077 unsigned long flags;
2081 IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __func__ );
2083 IRDA_ASSERT(self != NULL, return FALSE;);
2085 spin_lock_irqsave(&self->lock, flags);
2087 if (self->io.speed > 115200)
2089 iobase = self->io.fir_base;
2091 switch_bank(iobase, BANK1);
2092 if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)
2094 /* We are receiving something */
2095 IRDA_DEBUG(1, "%s(), We are receiving something\n", __func__ );
2098 switch_bank(iobase, BANK0);
2102 status = (self->rx_buff.state != OUTSIDE_FRAME);
2105 spin_unlock_irqrestore(&self->lock, flags);
2107 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
2112 static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
2114 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2116 IRDA_MESSAGE("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
2118 if (self->io.suspended)
2121 ali_ircc_net_close(self->netdev);
2123 self->io.suspended = 1;
2128 static int ali_ircc_resume(struct platform_device *dev)
2130 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2132 if (!self->io.suspended)
2135 ali_ircc_net_open(self->netdev);
2137 IRDA_MESSAGE("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
2139 self->io.suspended = 0;
2144 /* ALi Chip Function */
2146 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
2149 unsigned char newMask;
2151 int iobase = self->io.fir_base; /* or sir_base */
2153 IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __func__ , enable);
2155 /* Enable the interrupt which we wish to */
2157 if (self->io.direction == IO_XMIT)
2159 if (self->io.speed > 115200) /* FIR, MIR */
2161 newMask = self->ier;
2165 newMask = UART_IER_THRI | UART_IER_RDI;
2169 if (self->io.speed > 115200) /* FIR, MIR */
2171 newMask = self->ier;
2175 newMask = UART_IER_RDI;
2179 else /* Disable all the interrupts */
2185 //SIR and FIR has different registers
2186 if (self->io.speed > 115200)
2188 switch_bank(iobase, BANK0);
2189 outb(newMask, iobase+FIR_IER);
2192 outb(newMask, iobase+UART_IER);
2194 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
2197 static void SIR2FIR(int iobase)
2199 //unsigned char tmp;
2201 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
2203 /* Already protected (change_speed() or setup()), no need to lock.
2206 outb(0x28, iobase+UART_MCR);
2207 outb(0x68, iobase+UART_MCR);
2208 outb(0x88, iobase+UART_MCR);
2210 outb(0x60, iobase+FIR_MCR); /* Master Reset */
2211 outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
2213 //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
2215 //outb(tmp, iobase+FIR_LCR_B);
2217 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
2220 static void FIR2SIR(int iobase)
2224 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
2226 /* Already protected (change_speed() or setup()), no need to lock.
2229 outb(0x20, iobase+FIR_MCR); /* IRQ to low */
2230 outb(0x00, iobase+UART_IER);
2232 outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
2233 outb(0x00, iobase+UART_FCR);
2234 outb(0x07, iobase+UART_FCR);
2236 val = inb(iobase+UART_RX);
2237 val = inb(iobase+UART_LSR);
2238 val = inb(iobase+UART_MSR);
2240 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
2243 MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
2244 MODULE_DESCRIPTION("ALi FIR Controller Driver");
2245 MODULE_LICENSE("GPL");
2246 MODULE_ALIAS("platform:" ALI_IRCC_DRIVER_NAME);
2249 module_param_array(io, int, NULL, 0);
2250 MODULE_PARM_DESC(io, "Base I/O addresses");
2251 module_param_array(irq, int, NULL, 0);
2252 MODULE_PARM_DESC(irq, "IRQ lines");
2253 module_param_array(dma, int, NULL, 0);
2254 MODULE_PARM_DESC(dma, "DMA channels");
2256 module_init(ali_ircc_init);
2257 module_exit(ali_ircc_cleanup);