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);
112 static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev);
115 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
116 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
117 static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
118 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
119 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
120 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
123 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev);
124 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
125 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
126 static int ali_ircc_dma_receive(struct ali_ircc_cb *self);
127 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
128 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
129 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
132 static int ali_ircc_read_dongle_id (int i, chipio_t *info);
133 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
135 /* ALi chip function */
136 static void SIR2FIR(int iobase);
137 static void FIR2SIR(int iobase);
138 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
141 * Function ali_ircc_init ()
143 * Initialize chip. Find out whay kinds of chips we are dealing with
144 * and their configuation registers address
146 static int __init ali_ircc_init(void)
155 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
157 ret = platform_driver_register(&ali_ircc_driver);
159 IRDA_ERROR("%s, Can't register driver!\n",
160 ALI_IRCC_DRIVER_NAME);
166 /* Probe for all the ALi chipsets we know about */
167 for (chip= chips; chip->name; chip++, i++)
169 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __FUNCTION__, chip->name);
171 /* Try all config registers for this chip */
172 for (cfg=0; cfg<2; cfg++)
174 cfg_base = chip->cfg[cfg];
178 memset(&info, 0, sizeof(chipio_t));
179 info.cfg_base = cfg_base;
180 info.fir_base = io[i];
185 /* Enter Configuration */
186 outb(chip->entr1, cfg_base);
187 outb(chip->entr2, cfg_base);
189 /* Select Logical Device 5 Registers (UART2) */
190 outb(0x07, cfg_base);
191 outb(0x05, cfg_base+1);
193 /* Read Chip Identification Register */
194 outb(chip->cid_index, cfg_base);
195 reg = inb(cfg_base+1);
197 if (reg == chip->cid_value)
199 IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __FUNCTION__, cfg_base);
201 outb(0x1F, cfg_base);
202 revision = inb(cfg_base+1);
203 IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __FUNCTION__,
204 chip->name, revision);
207 * If the user supplies the base address, then
208 * we init the chip, if not we probe the values
213 chip->init(chip, &info);
217 chip->probe(chip, &info);
220 if (ali_ircc_open(i, &info) == 0)
226 IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __FUNCTION__, chip->name, cfg_base);
228 /* Exit configuration */
229 outb(0xbb, cfg_base);
233 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
236 platform_driver_unregister(&ali_ircc_driver);
242 * Function ali_ircc_cleanup ()
244 * Close all configured chips
247 static void __exit ali_ircc_cleanup(void)
251 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
253 for (i=0; i < ARRAY_SIZE(dev_self); i++) {
255 ali_ircc_close(dev_self[i]);
258 platform_driver_unregister(&ali_ircc_driver);
260 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
264 * Function ali_ircc_open (int i, chipio_t *inf)
266 * Open driver instance
269 static int ali_ircc_open(int i, chipio_t *info)
271 struct net_device *dev;
272 struct ali_ircc_cb *self;
276 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
278 if (i >= ARRAY_SIZE(dev_self)) {
279 IRDA_ERROR("%s(), maximum number of supported chips reached!\n",
284 /* Set FIR FIFO and DMA Threshold */
285 if ((ali_ircc_setup(info)) == -1)
288 dev = alloc_irdadev(sizeof(*self));
290 IRDA_ERROR("%s(), can't allocate memory for control block!\n",
297 spin_lock_init(&self->lock);
299 /* Need to store self somewhere */
304 self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
305 self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
306 self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
307 self->io.irq = info->irq;
308 self->io.fir_ext = CHIP_IO_EXTENT;
309 self->io.dma = info->dma;
310 self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
312 /* Reserve the ioports that we need */
313 if (!request_region(self->io.fir_base, self->io.fir_ext,
314 ALI_IRCC_DRIVER_NAME)) {
315 IRDA_WARNING("%s(), can't get iobase of 0x%03x\n", __FUNCTION__,
321 /* Initialize QoS for this device */
322 irda_init_max_qos_capabilies(&self->qos);
324 /* The only value we must override it the baudrate */
325 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
326 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
328 self->qos.min_turn_time.bits = qos_mtt_bits;
330 irda_qos_bits_to_value(&self->qos);
332 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
333 self->rx_buff.truesize = 14384;
334 self->tx_buff.truesize = 14384;
336 /* Allocate memory if needed */
338 dma_alloc_coherent(NULL, self->rx_buff.truesize,
339 &self->rx_buff_dma, GFP_KERNEL);
340 if (self->rx_buff.head == NULL) {
344 memset(self->rx_buff.head, 0, self->rx_buff.truesize);
347 dma_alloc_coherent(NULL, self->tx_buff.truesize,
348 &self->tx_buff_dma, GFP_KERNEL);
349 if (self->tx_buff.head == NULL) {
353 memset(self->tx_buff.head, 0, self->tx_buff.truesize);
355 self->rx_buff.in_frame = FALSE;
356 self->rx_buff.state = OUTSIDE_FRAME;
357 self->tx_buff.data = self->tx_buff.head;
358 self->rx_buff.data = self->rx_buff.head;
360 /* Reset Tx queue info */
361 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
362 self->tx_fifo.tail = self->tx_buff.head;
364 /* Override the network functions we need to use */
365 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
366 dev->open = ali_ircc_net_open;
367 dev->stop = ali_ircc_net_close;
368 dev->do_ioctl = ali_ircc_net_ioctl;
369 dev->get_stats = ali_ircc_net_get_stats;
371 err = register_netdev(dev);
373 IRDA_ERROR("%s(), register_netdev() failed!\n", __FUNCTION__);
376 IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
378 /* Check dongle id */
379 dongle_id = ali_ircc_read_dongle_id(i, info);
380 IRDA_MESSAGE("%s(), %s, Found dongle: %s\n", __FUNCTION__,
381 ALI_IRCC_DRIVER_NAME, dongle_types[dongle_id]);
383 self->io.dongle_id = dongle_id;
385 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
390 dma_free_coherent(NULL, self->tx_buff.truesize,
391 self->tx_buff.head, self->tx_buff_dma);
393 dma_free_coherent(NULL, self->rx_buff.truesize,
394 self->rx_buff.head, self->rx_buff_dma);
396 release_region(self->io.fir_base, self->io.fir_ext);
405 * Function ali_ircc_close (self)
407 * Close driver instance
410 static int __exit ali_ircc_close(struct ali_ircc_cb *self)
414 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
416 IRDA_ASSERT(self != NULL, return -1;);
418 iobase = self->io.fir_base;
420 /* Remove netdevice */
421 unregister_netdev(self->netdev);
423 /* Release the PORT that this driver is using */
424 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __FUNCTION__, self->io.fir_base);
425 release_region(self->io.fir_base, self->io.fir_ext);
427 if (self->tx_buff.head)
428 dma_free_coherent(NULL, self->tx_buff.truesize,
429 self->tx_buff.head, self->tx_buff_dma);
431 if (self->rx_buff.head)
432 dma_free_coherent(NULL, self->rx_buff.truesize,
433 self->rx_buff.head, self->rx_buff_dma);
435 dev_self[self->index] = NULL;
436 free_netdev(self->netdev);
438 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
444 * Function ali_ircc_init_43 (chip, info)
446 * Initialize the ALi M1543 chip.
448 static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
450 /* All controller information like I/O address, DMA channel, IRQ
458 * Function ali_ircc_init_53 (chip, info)
460 * Initialize the ALi M1535 chip.
462 static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
464 /* All controller information like I/O address, DMA channel, IRQ
472 * Function ali_ircc_probe_53 (chip, info)
474 * Probes for the ALi M1535D or M1535
476 static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
478 int cfg_base = info->cfg_base;
481 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
483 /* Enter Configuration */
484 outb(chip->entr1, cfg_base);
485 outb(chip->entr2, cfg_base);
487 /* Select Logical Device 5 Registers (UART2) */
488 outb(0x07, cfg_base);
489 outb(0x05, cfg_base+1);
491 /* Read address control register */
492 outb(0x60, cfg_base);
493 hi = inb(cfg_base+1);
494 outb(0x61, cfg_base);
495 low = inb(cfg_base+1);
496 info->fir_base = (hi<<8) + low;
498 info->sir_base = info->fir_base;
500 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __FUNCTION__, info->fir_base);
502 /* Read IRQ control register */
503 outb(0x70, cfg_base);
504 reg = inb(cfg_base+1);
505 info->irq = reg & 0x0f;
506 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __FUNCTION__, info->irq);
508 /* Read DMA channel */
509 outb(0x74, cfg_base);
510 reg = inb(cfg_base+1);
511 info->dma = reg & 0x07;
513 if(info->dma == 0x04)
514 IRDA_WARNING("%s(), No DMA channel assigned !\n", __FUNCTION__);
516 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __FUNCTION__, info->dma);
518 /* Read Enabled Status */
519 outb(0x30, cfg_base);
520 reg = inb(cfg_base+1);
521 info->enabled = (reg & 0x80) && (reg & 0x01);
522 IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __FUNCTION__, info->enabled);
524 /* Read Power Status */
525 outb(0x22, cfg_base);
526 reg = inb(cfg_base+1);
527 info->suspended = (reg & 0x20);
528 IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __FUNCTION__, info->suspended);
530 /* Exit configuration */
531 outb(0xbb, cfg_base);
533 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __FUNCTION__);
539 * Function ali_ircc_setup (info)
541 * Set FIR FIFO and DMA Threshold
542 * Returns non-negative on success.
545 static int ali_ircc_setup(chipio_t *info)
549 int iobase = info->fir_base;
551 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
553 /* Locking comments :
554 * Most operations here need to be protected. We are called before
555 * the device instance is created in ali_ircc_open(), therefore
556 * nobody can bother us - Jean II */
558 /* Switch to FIR space */
562 outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
564 /* Read FIR ID Version Register */
565 switch_bank(iobase, BANK3);
566 version = inb(iobase+FIR_ID_VR);
568 /* Should be 0x00 in the M1535/M1535D */
571 IRDA_ERROR("%s, Wrong chip version %02x\n",
572 ALI_IRCC_DRIVER_NAME, version);
576 /* Set FIR FIFO Threshold Register */
577 switch_bank(iobase, BANK1);
578 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
580 /* Set FIR DMA Threshold Register */
581 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
584 switch_bank(iobase, BANK2);
585 outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
587 /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
589 /* Switch to Bank 0 */
590 switch_bank(iobase, BANK0);
592 tmp = inb(iobase+FIR_LCR_B);
593 tmp &=~0x20; // disable SIP
594 tmp |= 0x80; // these two steps make RX mode
596 outb(tmp, iobase+FIR_LCR_B);
598 /* Disable Interrupt */
599 outb(0x00, iobase+FIR_IER);
602 /* Switch to SIR space */
605 IRDA_MESSAGE("%s, driver loaded (Benjamin Kong)\n",
606 ALI_IRCC_DRIVER_NAME);
608 /* Enable receive interrupts */
609 // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
610 // Turn on the interrupts in ali_ircc_net_open
612 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
618 * Function ali_ircc_read_dongle_id (int index, info)
620 * Try to read dongle indentification. This procedure needs to be executed
621 * once after power-on/reset. It also needs to be used whenever you suspect
622 * that the user may have plugged/unplugged the IrDA Dongle.
624 static int ali_ircc_read_dongle_id (int i, chipio_t *info)
627 int cfg_base = info->cfg_base;
629 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
631 /* Enter Configuration */
632 outb(chips[i].entr1, cfg_base);
633 outb(chips[i].entr2, cfg_base);
635 /* Select Logical Device 5 Registers (UART2) */
636 outb(0x07, cfg_base);
637 outb(0x05, cfg_base+1);
640 outb(0xf0, cfg_base);
641 reg = inb(cfg_base+1);
642 dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
643 IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __FUNCTION__,
644 dongle_id, dongle_types[dongle_id]);
646 /* Exit configuration */
647 outb(0xbb, cfg_base);
649 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
655 * Function ali_ircc_interrupt (irq, dev_id, regs)
657 * An interrupt from the chip has arrived. Time to do some work
660 static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id)
662 struct net_device *dev = dev_id;
663 struct ali_ircc_cb *self;
666 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
670 spin_lock(&self->lock);
672 /* Dispatch interrupt handler for the current speed */
673 if (self->io.speed > 115200)
674 ret = ali_ircc_fir_interrupt(self);
676 ret = ali_ircc_sir_interrupt(self);
678 spin_unlock(&self->lock);
680 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
684 * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
686 * Handle MIR/FIR interrupt
689 static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
691 __u8 eir, OldMessageCount;
694 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
696 iobase = self->io.fir_base;
698 switch_bank(iobase, BANK0);
699 self->InterruptID = inb(iobase+FIR_IIR);
700 self->BusStatus = inb(iobase+FIR_BSR);
702 OldMessageCount = (self->LineStatus + 1) & 0x07;
703 self->LineStatus = inb(iobase+FIR_LSR);
704 //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
705 eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
707 IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __FUNCTION__,self->InterruptID);
708 IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __FUNCTION__,self->LineStatus);
709 IRDA_DEBUG(1, "%s(), self->ier = %x\n", __FUNCTION__,self->ier);
710 IRDA_DEBUG(1, "%s(), eir = %x\n", __FUNCTION__,eir);
712 /* Disable interrupts */
713 SetCOMInterrupts(self, FALSE);
715 /* Tx or Rx Interrupt */
719 if (self->io.direction == IO_XMIT) /* TX */
721 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __FUNCTION__);
723 if(ali_ircc_dma_xmit_complete(self))
725 if (irda_device_txqueue_empty(self->netdev))
727 /* Prepare for receive */
728 ali_ircc_dma_receive(self);
740 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __FUNCTION__);
742 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
744 self->rcvFramesOverflow = TRUE;
745 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******** \n", __FUNCTION__);
748 if (ali_ircc_dma_receive_complete(self))
750 IRDA_DEBUG(1, "%s(), ******* receive complete ******** \n", __FUNCTION__);
756 IRDA_DEBUG(1, "%s(), ******* Not receive complete ******** \n", __FUNCTION__);
758 self->ier = IER_EOM | IER_TIMER;
763 /* Timer Interrupt */
764 else if (eir & IIR_TIMER)
766 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
768 self->rcvFramesOverflow = TRUE;
769 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ******* \n", __FUNCTION__);
772 switch_bank(iobase, BANK1);
773 tmp = inb(iobase+FIR_CR);
774 outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
776 /* Check if this is a Tx timer interrupt */
777 if (self->io.direction == IO_XMIT)
779 ali_ircc_dma_xmit(self);
781 /* Interrupt on EOM */
787 if(ali_ircc_dma_receive_complete(self))
793 self->ier = IER_EOM | IER_TIMER;
798 /* Restore Interrupt */
799 SetCOMInterrupts(self, TRUE);
801 IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __FUNCTION__);
802 return IRQ_RETVAL(eir);
806 * Function ali_ircc_sir_interrupt (irq, self, eir)
808 * Handle SIR interrupt
811 static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
816 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
818 iobase = self->io.sir_base;
820 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
822 /* Clear interrupt */
823 lsr = inb(iobase+UART_LSR);
825 IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __FUNCTION__,
831 IRDA_DEBUG(2, "%s(), RLSI\n", __FUNCTION__);
834 /* Receive interrupt */
835 ali_ircc_sir_receive(self);
838 if (lsr & UART_LSR_THRE)
840 /* Transmitter ready for data */
841 ali_ircc_sir_write_wakeup(self);
845 IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __FUNCTION__, iir);
852 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__);
854 return IRQ_RETVAL(iir);
859 * Function ali_ircc_sir_receive (self)
861 * Receive one frame from the infrared port
864 static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
869 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__);
870 IRDA_ASSERT(self != NULL, return;);
872 iobase = self->io.sir_base;
875 * Receive all characters in Rx FIFO, unwrap and unstuff them.
876 * async_unwrap_char will deliver all found frames
879 async_unwrap_char(self->netdev, &self->stats, &self->rx_buff,
880 inb(iobase+UART_RX));
882 /* Make sure we don't stay here too long */
883 if (boguscount++ > 32) {
884 IRDA_DEBUG(2,"%s(), breaking!\n", __FUNCTION__);
887 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
889 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
893 * Function ali_ircc_sir_write_wakeup (tty)
895 * Called by the driver when there's room for more data. If we have
896 * more packets to send, we send them here.
899 static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
904 IRDA_ASSERT(self != NULL, return;);
906 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
908 iobase = self->io.sir_base;
910 /* Finished with frame? */
911 if (self->tx_buff.len > 0)
913 /* Write data left in transmit buffer */
914 actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
915 self->tx_buff.data, self->tx_buff.len);
916 self->tx_buff.data += actual;
917 self->tx_buff.len -= actual;
923 /* We must wait until all data are gone */
924 while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
925 IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __FUNCTION__ );
927 IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __FUNCTION__ , self->new_speed);
928 ali_ircc_change_speed(self, self->new_speed);
931 // benjamin 2000/11/10 06:32PM
932 if (self->io.speed > 115200)
934 IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT \n", __FUNCTION__ );
937 // SetCOMInterrupts(self, TRUE);
943 netif_wake_queue(self->netdev);
946 self->stats.tx_packets++;
948 /* Turn on receive interrupts */
949 outb(UART_IER_RDI, iobase+UART_IER);
952 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
955 static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
957 struct net_device *dev = self->netdev;
960 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
962 IRDA_DEBUG(2, "%s(), setting speed = %d \n", __FUNCTION__ , baud);
964 /* This function *must* be called with irq off and spin-lock.
967 iobase = self->io.fir_base;
969 SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
971 /* Go to MIR, FIR Speed */
976 ali_ircc_fir_change_speed(self, baud);
978 /* Install FIR xmit handler*/
979 dev->hard_start_xmit = ali_ircc_fir_hard_xmit;
981 /* Enable Interuupt */
982 self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
984 /* Be ready for incomming frames */
985 ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
987 /* Go to SIR Speed */
990 ali_ircc_sir_change_speed(self, baud);
992 /* Install SIR xmit handler*/
993 dev->hard_start_xmit = ali_ircc_sir_hard_xmit;
997 SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
999 netif_wake_queue(self->netdev);
1001 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1004 static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
1008 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1009 struct net_device *dev;
1011 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1013 IRDA_ASSERT(self != NULL, return;);
1016 iobase = self->io.fir_base;
1018 IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __FUNCTION__ ,self->io.speed,baud);
1020 /* Come from SIR speed */
1021 if(self->io.speed <=115200)
1026 /* Update accounting for new speed */
1027 self->io.speed = baud;
1029 // Set Dongle Speed mode
1030 ali_ircc_change_dongle_speed(self, baud);
1032 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1036 * Function ali_sir_change_speed (self, speed)
1038 * Set speed of IrDA port to specified baudrate
1041 static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
1043 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1044 unsigned long flags;
1046 int fcr; /* FIFO control reg */
1047 int lcr; /* Line control reg */
1050 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1052 IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __FUNCTION__ , speed);
1054 IRDA_ASSERT(self != NULL, return;);
1056 iobase = self->io.sir_base;
1058 /* Come from MIR or FIR speed */
1059 if(self->io.speed >115200)
1061 // Set Dongle Speed mode first
1062 ali_ircc_change_dongle_speed(self, speed);
1067 // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
1069 inb(iobase+UART_LSR);
1070 inb(iobase+UART_SCR);
1072 /* Update accounting for new speed */
1073 self->io.speed = speed;
1075 spin_lock_irqsave(&self->lock, flags);
1077 divisor = 115200/speed;
1079 fcr = UART_FCR_ENABLE_FIFO;
1082 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
1083 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1084 * about this timeout since it will always be fast enough.
1086 if (self->io.speed < 38400)
1087 fcr |= UART_FCR_TRIGGER_1;
1089 fcr |= UART_FCR_TRIGGER_14;
1091 /* IrDA ports use 8N1 */
1092 lcr = UART_LCR_WLEN8;
1094 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
1095 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
1096 outb(divisor >> 8, iobase+UART_DLM);
1097 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
1098 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
1100 /* without this, the conection will be broken after come back from FIR speed,
1101 but with this, the SIR connection is harder to established */
1102 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
1104 spin_unlock_irqrestore(&self->lock, flags);
1106 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1109 static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
1112 struct ali_ircc_cb *self = (struct ali_ircc_cb *) priv;
1113 int iobase,dongle_id;
1116 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1118 iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
1119 dongle_id = self->io.dongle_id;
1121 /* We are already locked, no need to do it again */
1123 IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __FUNCTION__ , dongle_types[dongle_id], speed);
1125 switch_bank(iobase, BANK2);
1126 tmp = inb(iobase+FIR_IRDA_CR);
1128 /* IBM type dongle */
1131 if(speed == 4000000)
1134 // SD/MODE __| |__ __
1139 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1140 tmp |= IRDA_CR_CRC; // CRC=1
1142 switch_bank(iobase, BANK2);
1143 outb(tmp, iobase+FIR_IRDA_CR);
1145 // T1 -> SD/MODE:0 IRTX:0
1148 outb(tmp, iobase+FIR_IRDA_CR);
1151 // T2 -> SD/MODE:1 IRTX:0
1154 outb(tmp, iobase+FIR_IRDA_CR);
1157 // T3 -> SD/MODE:1 IRTX:1
1159 outb(tmp, iobase+FIR_IRDA_CR);
1162 // T4 -> SD/MODE:0 IRTX:1
1165 outb(tmp, iobase+FIR_IRDA_CR);
1168 // T5 -> SD/MODE:0 IRTX:0
1171 outb(tmp, iobase+FIR_IRDA_CR);
1174 // reset -> Normal TX output Signal
1175 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1177 else /* speed <=1152000 */
1185 /* MIR 115200, 57600 */
1188 tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
1192 tmp &=~0x80; //HDLC 0.576Mbps
1193 tmp |= 0x20; //HDLC=1,
1196 tmp |= IRDA_CR_CRC; // CRC=1
1198 switch_bank(iobase, BANK2);
1199 outb(tmp, iobase+FIR_IRDA_CR);
1201 /* MIR 115200, 57600 */
1203 //switch_bank(iobase, BANK2);
1204 // T1 -> SD/MODE:0 IRTX:0
1207 outb(tmp, iobase+FIR_IRDA_CR);
1210 // T2 -> SD/MODE:1 IRTX:0
1213 outb(tmp, iobase+FIR_IRDA_CR);
1215 // T3 -> SD/MODE:0 IRTX:0
1218 outb(tmp, iobase+FIR_IRDA_CR);
1221 // reset -> Normal TX output Signal
1222 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1225 else if (dongle_id == 1) /* HP HDSL-3600 */
1230 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1234 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1238 tmp &=~0x80; // HDLC 0.576Mbps
1239 tmp |= 0x20; // HDLC=1,
1243 tmp |= IRDA_CR_CRC; // CRC=1
1245 switch_bank(iobase, BANK2);
1246 outb(tmp, iobase+FIR_IRDA_CR);
1248 else /* HP HDSL-1100 */
1250 if(speed <= 115200) /* SIR */
1253 tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
1255 switch_bank(iobase, BANK2);
1256 outb(tmp, iobase+FIR_IRDA_CR);
1264 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1268 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1272 tmp &=~0x80; // HDLC 0.576Mbps
1273 tmp |= 0x20; // HDLC=1,
1277 tmp |= IRDA_CR_CRC; // CRC=1
1278 tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
1280 switch_bank(iobase, BANK2);
1281 outb(tmp, iobase+FIR_IRDA_CR);
1285 switch_bank(iobase, BANK0);
1287 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1291 * Function ali_ircc_sir_write (driver)
1293 * Fill Tx FIFO with transmit data
1296 static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
1300 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1302 /* Tx FIFO should be empty! */
1303 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
1304 IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __FUNCTION__ );
1308 /* Fill FIFO with current frame */
1309 while ((fifo_size-- > 0) && (actual < len)) {
1310 /* Transmit next byte */
1311 outb(buf[actual], iobase+UART_TX);
1316 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1321 * Function ali_ircc_net_open (dev)
1326 static int ali_ircc_net_open(struct net_device *dev)
1328 struct ali_ircc_cb *self;
1332 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1334 IRDA_ASSERT(dev != NULL, return -1;);
1336 self = (struct ali_ircc_cb *) dev->priv;
1338 IRDA_ASSERT(self != NULL, return 0;);
1340 iobase = self->io.fir_base;
1342 /* Request IRQ and install Interrupt Handler */
1343 if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev))
1345 IRDA_WARNING("%s, unable to allocate irq=%d\n",
1346 ALI_IRCC_DRIVER_NAME,
1352 * Always allocate the DMA channel after the IRQ, and clean up on
1355 if (request_dma(self->io.dma, dev->name)) {
1356 IRDA_WARNING("%s, unable to allocate dma=%d\n",
1357 ALI_IRCC_DRIVER_NAME,
1359 free_irq(self->io.irq, self);
1363 /* Turn on interrups */
1364 outb(UART_IER_RDI , iobase+UART_IER);
1366 /* Ready to play! */
1367 netif_start_queue(dev); //benjamin by irport
1369 /* Give self a hardware name */
1370 sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
1373 * Open new IrLAP layer instance, now that everything should be
1374 * initialized properly
1376 self->irlap = irlap_open(dev, &self->qos, hwname);
1378 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1384 * Function ali_ircc_net_close (dev)
1389 static int ali_ircc_net_close(struct net_device *dev)
1392 struct ali_ircc_cb *self;
1395 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1397 IRDA_ASSERT(dev != NULL, return -1;);
1399 self = (struct ali_ircc_cb *) dev->priv;
1400 IRDA_ASSERT(self != NULL, return 0;);
1403 netif_stop_queue(dev);
1405 /* Stop and remove instance of IrLAP */
1407 irlap_close(self->irlap);
1410 disable_dma(self->io.dma);
1412 /* Disable interrupts */
1413 SetCOMInterrupts(self, FALSE);
1415 free_irq(self->io.irq, dev);
1416 free_dma(self->io.dma);
1418 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1424 * Function ali_ircc_fir_hard_xmit (skb, dev)
1426 * Transmit the frame
1429 static int ali_ircc_fir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1431 struct ali_ircc_cb *self;
1432 unsigned long flags;
1437 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1439 self = (struct ali_ircc_cb *) dev->priv;
1440 iobase = self->io.fir_base;
1442 netif_stop_queue(dev);
1444 /* Make sure tests *& speed change are atomic */
1445 spin_lock_irqsave(&self->lock, flags);
1447 /* Note : you should make sure that speed changes are not going
1448 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1449 * details - Jean II */
1451 /* Check if we need to change the speed */
1452 speed = irda_get_next_speed(skb);
1453 if ((speed != self->io.speed) && (speed != -1)) {
1454 /* Check for empty frame */
1456 ali_ircc_change_speed(self, speed);
1457 dev->trans_start = jiffies;
1458 spin_unlock_irqrestore(&self->lock, flags);
1462 self->new_speed = speed;
1465 /* Register and copy this frame to DMA memory */
1466 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1467 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1468 self->tx_fifo.tail += skb->len;
1470 self->stats.tx_bytes += skb->len;
1472 skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
1474 self->tx_fifo.len++;
1475 self->tx_fifo.free++;
1477 /* Start transmit only if there is currently no transmit going on */
1478 if (self->tx_fifo.len == 1)
1480 /* Check if we must wait the min turn time or not */
1481 mtt = irda_get_mtt(skb);
1485 /* Check how much time we have used already */
1486 do_gettimeofday(&self->now);
1488 diff = self->now.tv_usec - self->stamp.tv_usec;
1489 /* self->stamp is set from ali_ircc_dma_receive_complete() */
1491 IRDA_DEBUG(1, "%s(), ******* diff = %d ******* \n", __FUNCTION__ , diff);
1496 /* Check if the mtt is larger than the time we have
1497 * already used by all the protocol processing
1504 * Use timer if delay larger than 1000 us, and
1505 * use udelay for smaller values which should
1510 /* Adjust for timer resolution */
1511 mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
1513 IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __FUNCTION__ , mtt);
1516 if (mtt == 1) /* 500 us */
1518 switch_bank(iobase, BANK1);
1519 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
1521 else if (mtt == 2) /* 1 ms */
1523 switch_bank(iobase, BANK1);
1524 outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
1526 else /* > 2ms -> 4ms */
1528 switch_bank(iobase, BANK1);
1529 outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
1534 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1535 self->io.direction = IO_XMIT;
1537 /* Enable timer interrupt */
1538 self->ier = IER_TIMER;
1539 SetCOMInterrupts(self, TRUE);
1541 /* Timer will take care of the rest */
1546 } // if (if (mtt > diff)
1549 /* Enable EOM interrupt */
1550 self->ier = IER_EOM;
1551 SetCOMInterrupts(self, TRUE);
1553 /* Transmit frame */
1554 ali_ircc_dma_xmit(self);
1555 } // if (self->tx_fifo.len == 1)
1559 /* Not busy transmitting anymore if window is not full */
1560 if (self->tx_fifo.free < MAX_TX_WINDOW)
1561 netif_wake_queue(self->netdev);
1563 /* Restore bank register */
1564 switch_bank(iobase, BANK0);
1566 dev->trans_start = jiffies;
1567 spin_unlock_irqrestore(&self->lock, flags);
1570 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1575 static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
1578 unsigned char FIFO_OPTI, Hi, Lo;
1581 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1583 iobase = self->io.fir_base;
1585 /* FIFO threshold , this method comes from NDIS5 code */
1587 if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
1588 FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
1590 FIFO_OPTI = TX_FIFO_Threshold;
1593 switch_bank(iobase, BANK1);
1594 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1596 self->io.direction = IO_XMIT;
1598 irda_setup_dma(self->io.dma,
1599 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1600 self->tx_buff.head) + self->tx_buff_dma,
1601 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1605 switch_bank(iobase, BANK0);
1606 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1608 /* Set Tx FIFO threshold */
1609 if (self->fifo_opti_buf!=FIFO_OPTI)
1611 switch_bank(iobase, BANK1);
1612 outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
1613 self->fifo_opti_buf=FIFO_OPTI;
1616 /* Set Tx DMA threshold */
1617 switch_bank(iobase, BANK1);
1618 outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
1620 /* Set max Tx frame size */
1621 Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
1622 Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
1623 switch_bank(iobase, BANK2);
1624 outb(Hi, iobase+FIR_TX_DSR_HI);
1625 outb(Lo, iobase+FIR_TX_DSR_LO);
1627 /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
1628 switch_bank(iobase, BANK0);
1629 tmp = inb(iobase+FIR_LCR_B);
1630 tmp &= ~0x20; // Disable SIP
1631 outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
1632 IRDA_DEBUG(1, "%s(), ******* Change to TX mode: FIR_LCR_B = 0x%x ******* \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
1634 outb(0, iobase+FIR_LSR);
1636 /* Enable DMA and Burst Mode */
1637 switch_bank(iobase, BANK1);
1638 outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1640 switch_bank(iobase, BANK0);
1642 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1645 static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
1650 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1652 iobase = self->io.fir_base;
1655 switch_bank(iobase, BANK1);
1656 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1658 /* Check for underrun! */
1659 switch_bank(iobase, BANK0);
1660 if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
1663 IRDA_ERROR("%s(), ********* LSR_FRAME_ABORT *********\n", __FUNCTION__);
1664 self->stats.tx_errors++;
1665 self->stats.tx_fifo_errors++;
1669 self->stats.tx_packets++;
1672 /* Check if we need to change the speed */
1673 if (self->new_speed)
1675 ali_ircc_change_speed(self, self->new_speed);
1676 self->new_speed = 0;
1679 /* Finished with this frame, so prepare for next */
1680 self->tx_fifo.ptr++;
1681 self->tx_fifo.len--;
1683 /* Any frames to be sent back-to-back? */
1684 if (self->tx_fifo.len)
1686 ali_ircc_dma_xmit(self);
1688 /* Not finished yet! */
1692 { /* Reset Tx FIFO info */
1693 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1694 self->tx_fifo.tail = self->tx_buff.head;
1697 /* Make sure we have room for more frames */
1698 if (self->tx_fifo.free < MAX_TX_WINDOW) {
1699 /* Not busy transmitting anymore */
1700 /* Tell the network layer, that we can accept more frames */
1701 netif_wake_queue(self->netdev);
1704 switch_bank(iobase, BANK0);
1706 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1711 * Function ali_ircc_dma_receive (self)
1713 * Get ready for receiving a frame. The device will initiate a DMA
1714 * if it starts to receive a frame.
1717 static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
1721 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1723 iobase = self->io.fir_base;
1725 /* Reset Tx FIFO info */
1726 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1727 self->tx_fifo.tail = self->tx_buff.head;
1730 switch_bank(iobase, BANK1);
1731 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1733 /* Reset Message Count */
1734 switch_bank(iobase, BANK0);
1735 outb(0x07, iobase+FIR_LSR);
1737 self->rcvFramesOverflow = FALSE;
1739 self->LineStatus = inb(iobase+FIR_LSR) ;
1741 /* Reset Rx FIFO info */
1742 self->io.direction = IO_RECV;
1743 self->rx_buff.data = self->rx_buff.head;
1746 // switch_bank(iobase, BANK0);
1747 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1749 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1750 self->st_fifo.tail = self->st_fifo.head = 0;
1752 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1755 /* Set Receive Mode,Brick Wall */
1756 //switch_bank(iobase, BANK0);
1757 tmp = inb(iobase+FIR_LCR_B);
1758 outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
1759 IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x *** \n", __FUNCTION__ , inb(iobase+FIR_LCR_B));
1761 /* Set Rx Threshold */
1762 switch_bank(iobase, BANK1);
1763 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
1764 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
1766 /* Enable DMA and Burst Mode */
1767 // switch_bank(iobase, BANK1);
1768 outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1770 switch_bank(iobase, BANK0);
1771 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1775 static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
1777 struct st_fifo *st_fifo;
1778 struct sk_buff *skb;
1779 __u8 status, MessageCount;
1780 int len, i, iobase, val;
1782 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
1784 st_fifo = &self->st_fifo;
1785 iobase = self->io.fir_base;
1787 switch_bank(iobase, BANK0);
1788 MessageCount = inb(iobase+ FIR_LSR)&0x07;
1790 if (MessageCount > 0)
1791 IRDA_DEBUG(0, "%s(), Messsage count = %d,\n", __FUNCTION__ , MessageCount);
1793 for (i=0; i<=MessageCount; i++)
1796 switch_bank(iobase, BANK0);
1797 status = inb(iobase+FIR_LSR);
1799 switch_bank(iobase, BANK2);
1800 len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
1802 len |= inb(iobase+FIR_RX_DSR_LO);
1804 IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __FUNCTION__ , len);
1805 IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __FUNCTION__ , status);
1807 if (st_fifo->tail >= MAX_RX_WINDOW) {
1808 IRDA_DEBUG(0, "%s(), window is full!\n", __FUNCTION__ );
1812 st_fifo->entries[st_fifo->tail].status = status;
1813 st_fifo->entries[st_fifo->tail].len = len;
1814 st_fifo->pending_bytes += len;
1819 for (i=0; i<=MessageCount; i++)
1821 /* Get first entry */
1822 status = st_fifo->entries[st_fifo->head].status;
1823 len = st_fifo->entries[st_fifo->head].len;
1824 st_fifo->pending_bytes -= len;
1828 /* Check for errors */
1829 if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
1831 IRDA_DEBUG(0,"%s(), ************* RX Errors ************ \n", __FUNCTION__ );
1834 self->stats.rx_errors++;
1836 self->rx_buff.data += len;
1838 if (status & LSR_FIFO_UR)
1840 self->stats.rx_frame_errors++;
1841 IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************ \n", __FUNCTION__ );
1843 if (status & LSR_FRAME_ERROR)
1845 self->stats.rx_frame_errors++;
1846 IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************ \n", __FUNCTION__ );
1849 if (status & LSR_CRC_ERROR)
1851 self->stats.rx_crc_errors++;
1852 IRDA_DEBUG(0,"%s(), ************* CRC Errors ************ \n", __FUNCTION__ );
1855 if(self->rcvFramesOverflow)
1857 self->stats.rx_frame_errors++;
1858 IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************ \n", __FUNCTION__ );
1862 self->stats.rx_frame_errors++;
1863 IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 ********* \n", __FUNCTION__ );
1869 if (st_fifo->pending_bytes < 32)
1871 switch_bank(iobase, BANK0);
1872 val = inb(iobase+FIR_BSR);
1873 if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
1875 IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************ \n", __FUNCTION__ );
1877 /* Put this entry back in fifo */
1880 st_fifo->pending_bytes += len;
1881 st_fifo->entries[st_fifo->head].status = status;
1882 st_fifo->entries[st_fifo->head].len = len;
1885 * DMA not finished yet, so try again
1886 * later, set timer value, resolution
1890 switch_bank(iobase, BANK1);
1891 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
1894 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1896 return FALSE; /* I'll be back! */
1901 * Remember the time we received this frame, so we can
1902 * reduce the min turn time a bit since we will know
1903 * how much time we have used for protocol processing
1905 do_gettimeofday(&self->stamp);
1907 skb = dev_alloc_skb(len+1);
1910 IRDA_WARNING("%s(), memory squeeze, "
1911 "dropping frame.\n",
1913 self->stats.rx_dropped++;
1918 /* Make sure IP header gets aligned */
1919 skb_reserve(skb, 1);
1921 /* Copy frame without CRC, CRC is removed by hardware*/
1923 skb_copy_to_linear_data(skb, self->rx_buff.data, len);
1925 /* Move to next frame */
1926 self->rx_buff.data += len;
1927 self->stats.rx_bytes += len;
1928 self->stats.rx_packets++;
1930 skb->dev = self->netdev;
1931 skb_reset_mac_header(skb);
1932 skb->protocol = htons(ETH_P_IRDA);
1934 self->netdev->last_rx = jiffies;
1938 switch_bank(iobase, BANK0);
1940 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
1947 * Function ali_ircc_sir_hard_xmit (skb, dev)
1949 * Transmit the frame!
1952 static int ali_ircc_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
1954 struct ali_ircc_cb *self;
1955 unsigned long flags;
1959 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
1961 IRDA_ASSERT(dev != NULL, return 0;);
1963 self = (struct ali_ircc_cb *) dev->priv;
1964 IRDA_ASSERT(self != NULL, return 0;);
1966 iobase = self->io.sir_base;
1968 netif_stop_queue(dev);
1970 /* Make sure tests *& speed change are atomic */
1971 spin_lock_irqsave(&self->lock, flags);
1973 /* Note : you should make sure that speed changes are not going
1974 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1975 * details - Jean II */
1977 /* Check if we need to change the speed */
1978 speed = irda_get_next_speed(skb);
1979 if ((speed != self->io.speed) && (speed != -1)) {
1980 /* Check for empty frame */
1982 ali_ircc_change_speed(self, speed);
1983 dev->trans_start = jiffies;
1984 spin_unlock_irqrestore(&self->lock, flags);
1988 self->new_speed = speed;
1991 /* Init tx buffer */
1992 self->tx_buff.data = self->tx_buff.head;
1994 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
1995 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
1996 self->tx_buff.truesize);
1998 self->stats.tx_bytes += self->tx_buff.len;
2000 /* Turn on transmit finished interrupt. Will fire immediately! */
2001 outb(UART_IER_THRI, iobase+UART_IER);
2003 dev->trans_start = jiffies;
2004 spin_unlock_irqrestore(&self->lock, flags);
2008 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2015 * Function ali_ircc_net_ioctl (dev, rq, cmd)
2017 * Process IOCTL commands for this device
2020 static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2022 struct if_irda_req *irq = (struct if_irda_req *) rq;
2023 struct ali_ircc_cb *self;
2024 unsigned long flags;
2027 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2029 IRDA_ASSERT(dev != NULL, return -1;);
2033 IRDA_ASSERT(self != NULL, return -1;);
2035 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __FUNCTION__ , dev->name, cmd);
2038 case SIOCSBANDWIDTH: /* Set bandwidth */
2039 IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __FUNCTION__ );
2041 * This function will also be used by IrLAP to change the
2042 * speed, so we still must allow for speed change within
2043 * interrupt context.
2045 if (!in_interrupt() && !capable(CAP_NET_ADMIN))
2048 spin_lock_irqsave(&self->lock, flags);
2049 ali_ircc_change_speed(self, irq->ifr_baudrate);
2050 spin_unlock_irqrestore(&self->lock, flags);
2052 case SIOCSMEDIABUSY: /* Set media busy */
2053 IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __FUNCTION__ );
2054 if (!capable(CAP_NET_ADMIN))
2056 irda_device_set_media_busy(self->netdev, TRUE);
2058 case SIOCGRECEIVING: /* Check if we are receiving right now */
2059 IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __FUNCTION__ );
2060 /* This is protected */
2061 irq->ifr_receiving = ali_ircc_is_receiving(self);
2067 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2073 * Function ali_ircc_is_receiving (self)
2075 * Return TRUE is we are currently receiving a frame
2078 static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
2080 unsigned long flags;
2084 IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __FUNCTION__ );
2086 IRDA_ASSERT(self != NULL, return FALSE;);
2088 spin_lock_irqsave(&self->lock, flags);
2090 if (self->io.speed > 115200)
2092 iobase = self->io.fir_base;
2094 switch_bank(iobase, BANK1);
2095 if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)
2097 /* We are receiving something */
2098 IRDA_DEBUG(1, "%s(), We are receiving something\n", __FUNCTION__ );
2101 switch_bank(iobase, BANK0);
2105 status = (self->rx_buff.state != OUTSIDE_FRAME);
2108 spin_unlock_irqrestore(&self->lock, flags);
2110 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2115 static struct net_device_stats *ali_ircc_net_get_stats(struct net_device *dev)
2117 struct ali_ircc_cb *self = (struct ali_ircc_cb *) dev->priv;
2119 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2121 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2123 return &self->stats;
2126 static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
2128 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2130 IRDA_MESSAGE("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
2132 if (self->io.suspended)
2135 ali_ircc_net_close(self->netdev);
2137 self->io.suspended = 1;
2142 static int ali_ircc_resume(struct platform_device *dev)
2144 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2146 if (!self->io.suspended)
2149 ali_ircc_net_open(self->netdev);
2151 IRDA_MESSAGE("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
2153 self->io.suspended = 0;
2158 /* ALi Chip Function */
2160 static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
2163 unsigned char newMask;
2165 int iobase = self->io.fir_base; /* or sir_base */
2167 IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __FUNCTION__ , enable);
2169 /* Enable the interrupt which we wish to */
2171 if (self->io.direction == IO_XMIT)
2173 if (self->io.speed > 115200) /* FIR, MIR */
2175 newMask = self->ier;
2179 newMask = UART_IER_THRI | UART_IER_RDI;
2183 if (self->io.speed > 115200) /* FIR, MIR */
2185 newMask = self->ier;
2189 newMask = UART_IER_RDI;
2193 else /* Disable all the interrupts */
2199 //SIR and FIR has different registers
2200 if (self->io.speed > 115200)
2202 switch_bank(iobase, BANK0);
2203 outb(newMask, iobase+FIR_IER);
2206 outb(newMask, iobase+UART_IER);
2208 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2211 static void SIR2FIR(int iobase)
2213 //unsigned char tmp;
2215 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2217 /* Already protected (change_speed() or setup()), no need to lock.
2220 outb(0x28, iobase+UART_MCR);
2221 outb(0x68, iobase+UART_MCR);
2222 outb(0x88, iobase+UART_MCR);
2224 outb(0x60, iobase+FIR_MCR); /* Master Reset */
2225 outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
2227 //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
2229 //outb(tmp, iobase+FIR_LCR_B);
2231 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2234 static void FIR2SIR(int iobase)
2238 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __FUNCTION__ );
2240 /* Already protected (change_speed() or setup()), no need to lock.
2243 outb(0x20, iobase+FIR_MCR); /* IRQ to low */
2244 outb(0x00, iobase+UART_IER);
2246 outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
2247 outb(0x00, iobase+UART_FCR);
2248 outb(0x07, iobase+UART_FCR);
2250 val = inb(iobase+UART_RX);
2251 val = inb(iobase+UART_LSR);
2252 val = inb(iobase+UART_MSR);
2254 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __FUNCTION__ );
2257 MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
2258 MODULE_DESCRIPTION("ALi FIR Controller Driver");
2259 MODULE_LICENSE("GPL");
2260 MODULE_ALIAS("platform:" ALI_IRCC_DRIVER_NAME);
2263 module_param_array(io, int, NULL, 0);
2264 MODULE_PARM_DESC(io, "Base I/O addresses");
2265 module_param_array(irq, int, NULL, 0);
2266 MODULE_PARM_DESC(irq, "IRQ lines");
2267 module_param_array(dma, int, NULL, 0);
2268 MODULE_PARM_DESC(dma, "DMA channels");
2270 module_init(ali_ircc_init);
2271 module_exit(ali_ircc_cleanup);