blob: 7f23e5ff099e03d5d6720f9deb2f61788c17a2db [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*********************************************************************
2 *
3 * Filename: ali-ircc.h
4 * Version: 0.5
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>
13 *
14 * Copyright (c) 2000 Benjamin Kong <benjamin_kong@ali.com.tw>
15 * All Rights Reserved
16 *
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.
21 *
22 ********************************************************************/
23
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <linux/kernel.h>
28#include <linux/types.h>
29#include <linux/skbuff.h>
30#include <linux/netdevice.h>
31#include <linux/ioport.h>
32#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000034#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/rtnetlink.h>
36#include <linux/serial_reg.h>
37#include <linux/dma-mapping.h>
Samuel Ortiz898b1d12006-05-25 16:21:10 -070038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/io.h>
41#include <asm/dma.h>
42#include <asm/byteorder.h>
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <net/irda/wrapper.h>
45#include <net/irda/irda.h>
46#include <net/irda/irda_device.h>
47
48#include "ali-ircc.h"
49
50#define CHIP_IO_EXTENT 8
51#define BROKEN_DONGLE_ID
52
Samuel Ortiz898b1d12006-05-25 16:21:10 -070053#define ALI_IRCC_DRIVER_NAME "ali-ircc"
54
55/* Power Management */
56static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state);
57static int ali_ircc_resume(struct platform_device *dev);
58
59static struct platform_driver ali_ircc_driver = {
60 .suspend = ali_ircc_suspend,
61 .resume = ali_ircc_resume,
62 .driver = {
63 .name = ALI_IRCC_DRIVER_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -070064 .owner = THIS_MODULE,
Samuel Ortiz898b1d12006-05-25 16:21:10 -070065 },
66};
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* Module parameters */
69static int qos_mtt_bits = 0x07; /* 1 ms or more */
70
71/* Use BIOS settions by default, but user may supply module parameters */
72static unsigned int io[] = { ~0, ~0, ~0, ~0 };
73static unsigned int irq[] = { 0, 0, 0, 0 };
74static unsigned int dma[] = { 0, 0, 0, 0 };
75
76static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info);
77static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info);
78static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info);
79
Lucas De Marchi25985ed2011-03-30 22:57:33 -030080/* These are the currently known ALi south-bridge chipsets, the only one difference
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * is that M1543C doesn't support HP HDSL-3600
82 */
83static ali_chip_t chips[] =
84{
85 { "M1543", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x43, ali_ircc_probe_53, ali_ircc_init_43 },
86 { "M1535", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x53, ali_ircc_probe_53, ali_ircc_init_53 },
87 { "M1563", { 0x3f0, 0x370 }, 0x51, 0x23, 0x20, 0x63, ali_ircc_probe_53, ali_ircc_init_53 },
88 { NULL }
89};
90
91/* Max 4 instances for now */
92static struct ali_ircc_cb *dev_self[] = { NULL, NULL, NULL, NULL };
93
94/* Dongle Types */
95static char *dongle_types[] = {
96 "TFDS6000",
97 "HP HSDL-3600",
98 "HP HSDL-1100",
99 "No dongle connected",
100};
101
102/* Some prototypes */
103static int ali_ircc_open(int i, chipio_t *info);
104
105static int ali_ircc_close(struct ali_ircc_cb *self);
106
107static int ali_ircc_setup(chipio_t *info);
108static int ali_ircc_is_receiving(struct ali_ircc_cb *self);
109static int ali_ircc_net_open(struct net_device *dev);
110static int ali_ircc_net_close(struct net_device *dev);
111static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* SIR function */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +0000115static netdev_tx_t ali_ircc_sir_hard_xmit(struct sk_buff *skb,
116 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self);
118static void ali_ircc_sir_receive(struct ali_ircc_cb *self);
119static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self);
120static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
121static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
122
123/* FIR function */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +0000124static netdev_tx_t ali_ircc_fir_hard_xmit(struct sk_buff *skb,
125 struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 speed);
127static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self);
128static int ali_ircc_dma_receive(struct ali_ircc_cb *self);
129static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self);
130static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self);
131static void ali_ircc_dma_xmit(struct ali_ircc_cb *self);
132
133/* My Function */
134static int ali_ircc_read_dongle_id (int i, chipio_t *info);
135static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed);
136
137/* ALi chip function */
138static void SIR2FIR(int iobase);
139static void FIR2SIR(int iobase);
140static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable);
141
142/*
143 * Function ali_ircc_init ()
144 *
145 * Initialize chip. Find out whay kinds of chips we are dealing with
Thomas Weber0b1974d2010-09-23 11:46:48 +0200146 * and their configuration registers address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 */
148static int __init ali_ircc_init(void)
149{
150 ali_chip_t *chip;
151 chipio_t info;
Adrian Bunk9c6c6792006-07-08 13:33:28 -0700152 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 int cfg, cfg_base;
154 int reg, revision;
155 int i = 0;
156
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700157 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Samuel Ortiz898b1d12006-05-25 16:21:10 -0700158
159 ret = platform_driver_register(&ali_ircc_driver);
160 if (ret) {
Joe Perches6c910232014-11-11 13:37:30 -0800161 net_err_ratelimited("%s, Can't register driver!\n",
162 ALI_IRCC_DRIVER_NAME);
Samuel Ortiz898b1d12006-05-25 16:21:10 -0700163 return ret;
164 }
165
Adrian Bunk9c6c6792006-07-08 13:33:28 -0700166 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 /* Probe for all the ALi chipsets we know about */
169 for (chip= chips; chip->name; chip++, i++)
170 {
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700171 IRDA_DEBUG(2, "%s(), Probing for %s ...\n", __func__, chip->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 /* Try all config registers for this chip */
174 for (cfg=0; cfg<2; cfg++)
175 {
176 cfg_base = chip->cfg[cfg];
177 if (!cfg_base)
178 continue;
179
180 memset(&info, 0, sizeof(chipio_t));
181 info.cfg_base = cfg_base;
182 info.fir_base = io[i];
183 info.dma = dma[i];
184 info.irq = irq[i];
185
186
187 /* Enter Configuration */
188 outb(chip->entr1, cfg_base);
189 outb(chip->entr2, cfg_base);
190
191 /* Select Logical Device 5 Registers (UART2) */
192 outb(0x07, cfg_base);
193 outb(0x05, cfg_base+1);
194
195 /* Read Chip Identification Register */
196 outb(chip->cid_index, cfg_base);
197 reg = inb(cfg_base+1);
198
199 if (reg == chip->cid_value)
200 {
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700201 IRDA_DEBUG(2, "%s(), Chip found at 0x%03x\n", __func__, cfg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 outb(0x1F, cfg_base);
204 revision = inb(cfg_base+1);
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700205 IRDA_DEBUG(2, "%s(), Found %s chip, revision=%d\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 chip->name, revision);
207
208 /*
209 * If the user supplies the base address, then
210 * we init the chip, if not we probe the values
211 * set by the BIOS
212 */
213 if (io[i] < 2000)
214 {
215 chip->init(chip, &info);
216 }
217 else
218 {
219 chip->probe(chip, &info);
220 }
221
222 if (ali_ircc_open(i, &info) == 0)
223 ret = 0;
224 i++;
225 }
226 else
227 {
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700228 IRDA_DEBUG(2, "%s(), No %s chip at 0x%03x\n", __func__, chip->name, cfg_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230 /* Exit configuration */
231 outb(0xbb, cfg_base);
232 }
233 }
234
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700235 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
Samuel Ortiz898b1d12006-05-25 16:21:10 -0700236
237 if (ret)
238 platform_driver_unregister(&ali_ircc_driver);
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return ret;
241}
242
243/*
244 * Function ali_ircc_cleanup ()
245 *
246 * Close all configured chips
247 *
248 */
249static void __exit ali_ircc_cleanup(void)
250{
251 int i;
252
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700253 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Bjorn Helgaas9c3bd682006-08-15 00:05:38 -0700255 for (i=0; i < ARRAY_SIZE(dev_self); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (dev_self[i])
257 ali_ircc_close(dev_self[i]);
258 }
259
Samuel Ortiz898b1d12006-05-25 16:21:10 -0700260 platform_driver_unregister(&ali_ircc_driver);
261
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700262 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Stephen Hemminger2d44a222009-03-20 19:35:40 +0000265static const struct net_device_ops ali_ircc_sir_ops = {
266 .ndo_open = ali_ircc_net_open,
267 .ndo_stop = ali_ircc_net_close,
268 .ndo_start_xmit = ali_ircc_sir_hard_xmit,
269 .ndo_do_ioctl = ali_ircc_net_ioctl,
270};
271
272static const struct net_device_ops ali_ircc_fir_ops = {
273 .ndo_open = ali_ircc_net_open,
274 .ndo_stop = ali_ircc_net_close,
275 .ndo_start_xmit = ali_ircc_fir_hard_xmit,
276 .ndo_do_ioctl = ali_ircc_net_ioctl,
277};
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/*
280 * Function ali_ircc_open (int i, chipio_t *inf)
281 *
282 * Open driver instance
283 *
284 */
285static int ali_ircc_open(int i, chipio_t *info)
286{
287 struct net_device *dev;
288 struct ali_ircc_cb *self;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 int dongle_id;
290 int err;
291
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700292 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Bjorn Helgaas9c3bd682006-08-15 00:05:38 -0700293
294 if (i >= ARRAY_SIZE(dev_self)) {
Joe Perches6c910232014-11-11 13:37:30 -0800295 net_err_ratelimited("%s(), maximum number of supported chips reached!\n",
296 __func__);
Bjorn Helgaas9c3bd682006-08-15 00:05:38 -0700297 return -ENOMEM;
298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 /* Set FIR FIFO and DMA Threshold */
301 if ((ali_ircc_setup(info)) == -1)
302 return -1;
303
304 dev = alloc_irdadev(sizeof(*self));
305 if (dev == NULL) {
Joe Perches6c910232014-11-11 13:37:30 -0800306 net_err_ratelimited("%s(), can't allocate memory for control block!\n",
307 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -ENOMEM;
309 }
310
Wang Chen4cf16532008-11-12 23:38:14 -0800311 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 self->netdev = dev;
313 spin_lock_init(&self->lock);
314
315 /* Need to store self somewhere */
316 dev_self[i] = self;
317 self->index = i;
318
319 /* Initialize IO */
320 self->io.cfg_base = info->cfg_base; /* In ali_ircc_probe_53 assign */
321 self->io.fir_base = info->fir_base; /* info->sir_base = info->fir_base */
322 self->io.sir_base = info->sir_base; /* ALi SIR and FIR use the same address */
323 self->io.irq = info->irq;
324 self->io.fir_ext = CHIP_IO_EXTENT;
325 self->io.dma = info->dma;
326 self->io.fifo_size = 16; /* SIR: 16, FIR: 32 Benjamin 2000/11/1 */
327
328 /* Reserve the ioports that we need */
Samuel Ortiz898b1d12006-05-25 16:21:10 -0700329 if (!request_region(self->io.fir_base, self->io.fir_ext,
330 ALI_IRCC_DRIVER_NAME)) {
Joe Perches6c910232014-11-11 13:37:30 -0800331 net_warn_ratelimited("%s(), can't get iobase of 0x%03x\n",
332 __func__, self->io.fir_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 err = -ENODEV;
334 goto err_out1;
335 }
336
337 /* Initialize QoS for this device */
338 irda_init_max_qos_capabilies(&self->qos);
339
340 /* The only value we must override it the baudrate */
341 self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
342 IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8); // benjamin 2000/11/8 05:27PM
343
344 self->qos.min_turn_time.bits = qos_mtt_bits;
345
346 irda_qos_bits_to_value(&self->qos);
347
348 /* Max DMA buffer size needed = (data_size + 6) * (window_size) + 6; */
349 self->rx_buff.truesize = 14384;
350 self->tx_buff.truesize = 14384;
351
352 /* Allocate memory if needed */
353 self->rx_buff.head =
Joe Perchesede23fa2013-08-26 22:45:23 -0700354 dma_zalloc_coherent(NULL, self->rx_buff.truesize,
355 &self->rx_buff_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (self->rx_buff.head == NULL) {
357 err = -ENOMEM;
358 goto err_out2;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 self->tx_buff.head =
Joe Perchesede23fa2013-08-26 22:45:23 -0700362 dma_zalloc_coherent(NULL, self->tx_buff.truesize,
363 &self->tx_buff_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (self->tx_buff.head == NULL) {
365 err = -ENOMEM;
366 goto err_out3;
367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 self->rx_buff.in_frame = FALSE;
370 self->rx_buff.state = OUTSIDE_FRAME;
371 self->tx_buff.data = self->tx_buff.head;
372 self->rx_buff.data = self->rx_buff.head;
373
374 /* Reset Tx queue info */
375 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
376 self->tx_fifo.tail = self->tx_buff.head;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Override the network functions we need to use */
Stephen Hemminger2d44a222009-03-20 19:35:40 +0000379 dev->netdev_ops = &ali_ircc_sir_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 err = register_netdev(dev);
382 if (err) {
Joe Perches6c910232014-11-11 13:37:30 -0800383 net_err_ratelimited("%s(), register_netdev() failed!\n",
384 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto err_out4;
386 }
Joe Perches6c910232014-11-11 13:37:30 -0800387 net_info_ratelimited("IrDA: Registered device %s\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Check dongle id */
390 dongle_id = ali_ircc_read_dongle_id(i, info);
Joe Perches6c910232014-11-11 13:37:30 -0800391 net_info_ratelimited("%s(), %s, Found dongle: %s\n",
392 __func__, ALI_IRCC_DRIVER_NAME,
393 dongle_types[dongle_id]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 self->io.dongle_id = dongle_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700397 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400
401 err_out4:
402 dma_free_coherent(NULL, self->tx_buff.truesize,
403 self->tx_buff.head, self->tx_buff_dma);
404 err_out3:
405 dma_free_coherent(NULL, self->rx_buff.truesize,
406 self->rx_buff.head, self->rx_buff_dma);
407 err_out2:
408 release_region(self->io.fir_base, self->io.fir_ext);
409 err_out1:
410 dev_self[i] = NULL;
411 free_netdev(dev);
412 return err;
413}
414
415
416/*
417 * Function ali_ircc_close (self)
418 *
419 * Close driver instance
420 *
421 */
422static int __exit ali_ircc_close(struct ali_ircc_cb *self)
423{
424 int iobase;
425
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700426 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 IRDA_ASSERT(self != NULL, return -1;);
429
430 iobase = self->io.fir_base;
431
432 /* Remove netdevice */
433 unregister_netdev(self->netdev);
434
435 /* Release the PORT that this driver is using */
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700436 IRDA_DEBUG(4, "%s(), Releasing Region %03x\n", __func__, self->io.fir_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 release_region(self->io.fir_base, self->io.fir_ext);
438
439 if (self->tx_buff.head)
440 dma_free_coherent(NULL, self->tx_buff.truesize,
441 self->tx_buff.head, self->tx_buff_dma);
442
443 if (self->rx_buff.head)
444 dma_free_coherent(NULL, self->rx_buff.truesize,
445 self->rx_buff.head, self->rx_buff_dma);
446
447 dev_self[self->index] = NULL;
448 free_netdev(self->netdev);
449
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700450 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 return 0;
453}
454
455/*
456 * Function ali_ircc_init_43 (chip, info)
457 *
458 * Initialize the ALi M1543 chip.
459 */
460static int ali_ircc_init_43(ali_chip_t *chip, chipio_t *info)
461{
462 /* All controller information like I/O address, DMA channel, IRQ
463 * are set by BIOS
464 */
465
466 return 0;
467}
468
469/*
470 * Function ali_ircc_init_53 (chip, info)
471 *
472 * Initialize the ALi M1535 chip.
473 */
474static int ali_ircc_init_53(ali_chip_t *chip, chipio_t *info)
475{
476 /* All controller information like I/O address, DMA channel, IRQ
477 * are set by BIOS
478 */
479
480 return 0;
481}
482
483/*
484 * Function ali_ircc_probe_53 (chip, info)
485 *
486 * Probes for the ALi M1535D or M1535
487 */
488static int ali_ircc_probe_53(ali_chip_t *chip, chipio_t *info)
489{
490 int cfg_base = info->cfg_base;
491 int hi, low, reg;
492
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700493 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 /* Enter Configuration */
496 outb(chip->entr1, cfg_base);
497 outb(chip->entr2, cfg_base);
498
499 /* Select Logical Device 5 Registers (UART2) */
500 outb(0x07, cfg_base);
501 outb(0x05, cfg_base+1);
502
503 /* Read address control register */
504 outb(0x60, cfg_base);
505 hi = inb(cfg_base+1);
506 outb(0x61, cfg_base);
507 low = inb(cfg_base+1);
508 info->fir_base = (hi<<8) + low;
509
510 info->sir_base = info->fir_base;
511
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700512 IRDA_DEBUG(2, "%s(), probing fir_base=0x%03x\n", __func__, info->fir_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /* Read IRQ control register */
515 outb(0x70, cfg_base);
516 reg = inb(cfg_base+1);
517 info->irq = reg & 0x0f;
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700518 IRDA_DEBUG(2, "%s(), probing irq=%d\n", __func__, info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* Read DMA channel */
521 outb(0x74, cfg_base);
522 reg = inb(cfg_base+1);
523 info->dma = reg & 0x07;
524
525 if(info->dma == 0x04)
Joe Perches6c910232014-11-11 13:37:30 -0800526 net_warn_ratelimited("%s(), No DMA channel assigned !\n",
527 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 else
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700529 IRDA_DEBUG(2, "%s(), probing dma=%d\n", __func__, info->dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 /* Read Enabled Status */
532 outb(0x30, cfg_base);
533 reg = inb(cfg_base+1);
534 info->enabled = (reg & 0x80) && (reg & 0x01);
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700535 IRDA_DEBUG(2, "%s(), probing enabled=%d\n", __func__, info->enabled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 /* Read Power Status */
538 outb(0x22, cfg_base);
539 reg = inb(cfg_base+1);
540 info->suspended = (reg & 0x20);
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700541 IRDA_DEBUG(2, "%s(), probing suspended=%d\n", __func__, info->suspended);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 /* Exit configuration */
544 outb(0xbb, cfg_base);
545
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700546 IRDA_DEBUG(2, "%s(), ----------------- End -----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 return 0;
549}
550
551/*
552 * Function ali_ircc_setup (info)
553 *
554 * Set FIR FIFO and DMA Threshold
555 * Returns non-negative on success.
556 *
557 */
558static int ali_ircc_setup(chipio_t *info)
559{
560 unsigned char tmp;
561 int version;
562 int iobase = info->fir_base;
563
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700564 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* Locking comments :
567 * Most operations here need to be protected. We are called before
568 * the device instance is created in ali_ircc_open(), therefore
569 * nobody can bother us - Jean II */
570
571 /* Switch to FIR space */
572 SIR2FIR(iobase);
573
574 /* Master Reset */
575 outb(0x40, iobase+FIR_MCR); // benjamin 2000/11/30 11:45AM
576
577 /* Read FIR ID Version Register */
578 switch_bank(iobase, BANK3);
579 version = inb(iobase+FIR_ID_VR);
580
581 /* Should be 0x00 in the M1535/M1535D */
582 if(version != 0x00)
583 {
Joe Perches6c910232014-11-11 13:37:30 -0800584 net_err_ratelimited("%s, Wrong chip version %02x\n",
585 ALI_IRCC_DRIVER_NAME, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return -1;
587 }
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Set FIR FIFO Threshold Register */
590 switch_bank(iobase, BANK1);
591 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
592
593 /* Set FIR DMA Threshold Register */
594 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
595
596 /* CRC enable */
597 switch_bank(iobase, BANK2);
598 outb(inb(iobase+FIR_IRDA_CR) | IRDA_CR_CRC, iobase+FIR_IRDA_CR);
599
600 /* NDIS driver set TX Length here BANK2 Alias 3, Alias4*/
601
602 /* Switch to Bank 0 */
603 switch_bank(iobase, BANK0);
604
605 tmp = inb(iobase+FIR_LCR_B);
606 tmp &=~0x20; // disable SIP
607 tmp |= 0x80; // these two steps make RX mode
608 tmp &= 0xbf;
609 outb(tmp, iobase+FIR_LCR_B);
610
611 /* Disable Interrupt */
612 outb(0x00, iobase+FIR_IER);
613
614
615 /* Switch to SIR space */
616 FIR2SIR(iobase);
617
Joe Perches6c910232014-11-11 13:37:30 -0800618 net_info_ratelimited("%s, driver loaded (Benjamin Kong)\n",
619 ALI_IRCC_DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 /* Enable receive interrupts */
622 // outb(UART_IER_RDI, iobase+UART_IER); //benjamin 2000/11/23 01:25PM
623 // Turn on the interrupts in ali_ircc_net_open
624
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700625 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 return 0;
628}
629
630/*
631 * Function ali_ircc_read_dongle_id (int index, info)
632 *
Maxime Jayat3f794102013-10-12 01:29:46 +0200633 * Try to read dongle identification. This procedure needs to be executed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * once after power-on/reset. It also needs to be used whenever you suspect
635 * that the user may have plugged/unplugged the IrDA Dongle.
636 */
637static int ali_ircc_read_dongle_id (int i, chipio_t *info)
638{
639 int dongle_id, reg;
640 int cfg_base = info->cfg_base;
641
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700642 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Enter Configuration */
645 outb(chips[i].entr1, cfg_base);
646 outb(chips[i].entr2, cfg_base);
647
648 /* Select Logical Device 5 Registers (UART2) */
649 outb(0x07, cfg_base);
650 outb(0x05, cfg_base+1);
651
652 /* Read Dongle ID */
653 outb(0xf0, cfg_base);
654 reg = inb(cfg_base+1);
655 dongle_id = ((reg>>6)&0x02) | ((reg>>5)&0x01);
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700656 IRDA_DEBUG(2, "%s(), probing dongle_id=%d, dongle_types=%s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 dongle_id, dongle_types[dongle_id]);
658
659 /* Exit configuration */
660 outb(0xbb, cfg_base);
661
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700662 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 return dongle_id;
665}
666
667/*
668 * Function ali_ircc_interrupt (irq, dev_id, regs)
669 *
670 * An interrupt from the chip has arrived. Time to do some work
671 *
672 */
David Howells7d12e782006-10-05 14:55:46 +0100673static irqreturn_t ali_ircc_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Jeff Garzikc31f28e2006-10-06 14:56:04 -0400675 struct net_device *dev = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct ali_ircc_cb *self;
677 int ret;
678
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700679 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Wang Chen4cf16532008-11-12 23:38:14 -0800681 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 spin_lock(&self->lock);
684
685 /* Dispatch interrupt handler for the current speed */
686 if (self->io.speed > 115200)
687 ret = ali_ircc_fir_interrupt(self);
688 else
689 ret = ali_ircc_sir_interrupt(self);
690
691 spin_unlock(&self->lock);
692
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700693 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return ret;
695}
696/*
697 * Function ali_ircc_fir_interrupt(irq, struct ali_ircc_cb *self)
698 *
699 * Handle MIR/FIR interrupt
700 *
701 */
702static irqreturn_t ali_ircc_fir_interrupt(struct ali_ircc_cb *self)
703{
704 __u8 eir, OldMessageCount;
705 int iobase, tmp;
706
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700707 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 iobase = self->io.fir_base;
710
711 switch_bank(iobase, BANK0);
712 self->InterruptID = inb(iobase+FIR_IIR);
713 self->BusStatus = inb(iobase+FIR_BSR);
714
715 OldMessageCount = (self->LineStatus + 1) & 0x07;
716 self->LineStatus = inb(iobase+FIR_LSR);
717 //self->ier = inb(iobase+FIR_IER); 2000/12/1 04:32PM
718 eir = self->InterruptID & self->ier; /* Mask out the interesting ones */
719
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700720 IRDA_DEBUG(1, "%s(), self->InterruptID = %x\n", __func__,self->InterruptID);
721 IRDA_DEBUG(1, "%s(), self->LineStatus = %x\n", __func__,self->LineStatus);
722 IRDA_DEBUG(1, "%s(), self->ier = %x\n", __func__,self->ier);
723 IRDA_DEBUG(1, "%s(), eir = %x\n", __func__,eir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 /* Disable interrupts */
726 SetCOMInterrupts(self, FALSE);
727
728 /* Tx or Rx Interrupt */
729
730 if (eir & IIR_EOM)
731 {
732 if (self->io.direction == IO_XMIT) /* TX */
733 {
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700734 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Tx) *******\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if(ali_ircc_dma_xmit_complete(self))
737 {
738 if (irda_device_txqueue_empty(self->netdev))
739 {
740 /* Prepare for receive */
741 ali_ircc_dma_receive(self);
742 self->ier = IER_EOM;
743 }
744 }
745 else
746 {
747 self->ier = IER_EOM;
748 }
749
750 }
751 else /* RX */
752 {
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700753 IRDA_DEBUG(1, "%s(), ******* IIR_EOM (Rx) *******\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
756 {
757 self->rcvFramesOverflow = TRUE;
Frans Pop19299b342010-03-24 07:57:30 +0000758 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE ********\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 if (ali_ircc_dma_receive_complete(self))
762 {
Frans Pop19299b342010-03-24 07:57:30 +0000763 IRDA_DEBUG(1, "%s(), ******* receive complete ********\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 self->ier = IER_EOM;
766 }
767 else
768 {
Frans Pop19299b342010-03-24 07:57:30 +0000769 IRDA_DEBUG(1, "%s(), ******* Not receive complete ********\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 self->ier = IER_EOM | IER_TIMER;
772 }
773
774 }
775 }
776 /* Timer Interrupt */
777 else if (eir & IIR_TIMER)
778 {
779 if(OldMessageCount > ((self->LineStatus+1) & 0x07))
780 {
781 self->rcvFramesOverflow = TRUE;
Frans Pop19299b342010-03-24 07:57:30 +0000782 IRDA_DEBUG(1, "%s(), ******* self->rcvFramesOverflow = TRUE *******\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
784 /* Disable Timer */
785 switch_bank(iobase, BANK1);
786 tmp = inb(iobase+FIR_CR);
787 outb( tmp& ~CR_TIMER_EN, iobase+FIR_CR);
788
789 /* Check if this is a Tx timer interrupt */
790 if (self->io.direction == IO_XMIT)
791 {
792 ali_ircc_dma_xmit(self);
793
794 /* Interrupt on EOM */
795 self->ier = IER_EOM;
796
797 }
798 else /* Rx */
799 {
800 if(ali_ircc_dma_receive_complete(self))
801 {
802 self->ier = IER_EOM;
803 }
804 else
805 {
806 self->ier = IER_EOM | IER_TIMER;
807 }
808 }
809 }
810
811 /* Restore Interrupt */
812 SetCOMInterrupts(self, TRUE);
813
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700814 IRDA_DEBUG(1, "%s(), ----------------- End ---------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return IRQ_RETVAL(eir);
816}
817
818/*
819 * Function ali_ircc_sir_interrupt (irq, self, eir)
820 *
821 * Handle SIR interrupt
822 *
823 */
824static irqreturn_t ali_ircc_sir_interrupt(struct ali_ircc_cb *self)
825{
826 int iobase;
827 int iir, lsr;
828
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700829 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 iobase = self->io.sir_base;
832
833 iir = inb(iobase+UART_IIR) & UART_IIR_ID;
834 if (iir) {
835 /* Clear interrupt */
836 lsr = inb(iobase+UART_LSR);
837
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700838 IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 iir, lsr, iobase);
840
841 switch (iir)
842 {
843 case UART_IIR_RLSI:
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700844 IRDA_DEBUG(2, "%s(), RLSI\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 break;
846 case UART_IIR_RDI:
847 /* Receive interrupt */
848 ali_ircc_sir_receive(self);
849 break;
850 case UART_IIR_THRI:
851 if (lsr & UART_LSR_THRE)
852 {
853 /* Transmitter ready for data */
854 ali_ircc_sir_write_wakeup(self);
855 }
856 break;
857 default:
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700858 IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n", __func__, iir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 break;
860 }
861
862 }
863
864
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700865 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 return IRQ_RETVAL(iir);
868}
869
870
871/*
872 * Function ali_ircc_sir_receive (self)
873 *
874 * Receive one frame from the infrared port
875 *
876 */
877static void ali_ircc_sir_receive(struct ali_ircc_cb *self)
878{
879 int boguscount = 0;
880 int iobase;
881
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700882 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 IRDA_ASSERT(self != NULL, return;);
884
885 iobase = self->io.sir_base;
886
887 /*
888 * Receive all characters in Rx FIFO, unwrap and unstuff them.
889 * async_unwrap_char will deliver all found frames
890 */
891 do {
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800892 async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 inb(iobase+UART_RX));
894
895 /* Make sure we don't stay here too long */
896 if (boguscount++ > 32) {
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700897 IRDA_DEBUG(2,"%s(), breaking!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
899 }
900 } while (inb(iobase+UART_LSR) & UART_LSR_DR);
901
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700902 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
905/*
906 * Function ali_ircc_sir_write_wakeup (tty)
907 *
908 * Called by the driver when there's room for more data. If we have
909 * more packets to send, we send them here.
910 *
911 */
912static void ali_ircc_sir_write_wakeup(struct ali_ircc_cb *self)
913{
914 int actual = 0;
915 int iobase;
916
917 IRDA_ASSERT(self != NULL, return;);
918
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700919 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 iobase = self->io.sir_base;
922
923 /* Finished with frame? */
924 if (self->tx_buff.len > 0)
925 {
926 /* Write data left in transmit buffer */
927 actual = ali_ircc_sir_write(iobase, self->io.fifo_size,
928 self->tx_buff.data, self->tx_buff.len);
929 self->tx_buff.data += actual;
930 self->tx_buff.len -= actual;
931 }
932 else
933 {
934 if (self->new_speed)
935 {
936 /* We must wait until all data are gone */
937 while(!(inb(iobase+UART_LSR) & UART_LSR_TEMT))
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700938 IRDA_DEBUG(1, "%s(), UART_LSR_THRE\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700940 IRDA_DEBUG(1, "%s(), Changing speed! self->new_speed = %d\n", __func__ , self->new_speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 ali_ircc_change_speed(self, self->new_speed);
942 self->new_speed = 0;
943
944 // benjamin 2000/11/10 06:32PM
945 if (self->io.speed > 115200)
946 {
Frans Pop19299b342010-03-24 07:57:30 +0000947 IRDA_DEBUG(2, "%s(), ali_ircc_change_speed from UART_LSR_TEMT\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 self->ier = IER_EOM;
950 // SetCOMInterrupts(self, TRUE);
951 return;
952 }
953 }
954 else
955 {
956 netif_wake_queue(self->netdev);
957 }
958
Stephen Hemmingeraf049082009-01-06 10:40:43 -0800959 self->netdev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* Turn on receive interrupts */
962 outb(UART_IER_RDI, iobase+UART_IER);
963 }
964
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700965 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966}
967
968static void ali_ircc_change_speed(struct ali_ircc_cb *self, __u32 baud)
969{
970 struct net_device *dev = self->netdev;
971 int iobase;
972
Harvey Harrisona97a6f12008-07-30 17:20:18 -0700973 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Frans Pop19299b342010-03-24 07:57:30 +0000975 IRDA_DEBUG(2, "%s(), setting speed = %d\n", __func__ , baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* This function *must* be called with irq off and spin-lock.
978 * - Jean II */
979
980 iobase = self->io.fir_base;
981
982 SetCOMInterrupts(self, FALSE); // 2000/11/24 11:43AM
983
984 /* Go to MIR, FIR Speed */
985 if (baud > 115200)
986 {
987
988
989 ali_ircc_fir_change_speed(self, baud);
990
991 /* Install FIR xmit handler*/
Stephen Hemminger2d44a222009-03-20 19:35:40 +0000992 dev->netdev_ops = &ali_ircc_fir_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /* Enable Interuupt */
995 self->ier = IER_EOM; // benjamin 2000/11/20 07:24PM
996
Jorrit Schippersd82603c2012-12-27 17:33:02 +0100997 /* Be ready for incoming frames */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 ali_ircc_dma_receive(self); // benajmin 2000/11/8 07:46PM not complete
999 }
1000 /* Go to SIR Speed */
1001 else
1002 {
1003 ali_ircc_sir_change_speed(self, baud);
1004
1005 /* Install SIR xmit handler*/
Stephen Hemminger2d44a222009-03-20 19:35:40 +00001006 dev->netdev_ops = &ali_ircc_sir_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
1009
1010 SetCOMInterrupts(self, TRUE); // 2000/11/24 11:43AM
1011
1012 netif_wake_queue(self->netdev);
1013
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001014 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017static void ali_ircc_fir_change_speed(struct ali_ircc_cb *priv, __u32 baud)
1018{
1019
1020 int iobase;
Joe Perchesc2fd03a2012-06-04 12:44:18 +00001021 struct ali_ircc_cb *self = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct net_device *dev;
1023
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001024 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 IRDA_ASSERT(self != NULL, return;);
1027
1028 dev = self->netdev;
1029 iobase = self->io.fir_base;
1030
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001031 IRDA_DEBUG(1, "%s(), self->io.speed = %d, change to speed = %d\n", __func__ ,self->io.speed,baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 /* Come from SIR speed */
1034 if(self->io.speed <=115200)
1035 {
1036 SIR2FIR(iobase);
1037 }
1038
1039 /* Update accounting for new speed */
1040 self->io.speed = baud;
1041
1042 // Set Dongle Speed mode
1043 ali_ircc_change_dongle_speed(self, baud);
1044
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001045 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048/*
1049 * Function ali_sir_change_speed (self, speed)
1050 *
1051 * Set speed of IrDA port to specified baudrate
1052 *
1053 */
1054static void ali_ircc_sir_change_speed(struct ali_ircc_cb *priv, __u32 speed)
1055{
Joe Perchesc2fd03a2012-06-04 12:44:18 +00001056 struct ali_ircc_cb *self = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 unsigned long flags;
1058 int iobase;
1059 int fcr; /* FIFO control reg */
1060 int lcr; /* Line control reg */
1061 int divisor;
1062
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001063 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001065 IRDA_DEBUG(1, "%s(), Setting speed to: %d\n", __func__ , speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 IRDA_ASSERT(self != NULL, return;);
1068
1069 iobase = self->io.sir_base;
1070
1071 /* Come from MIR or FIR speed */
1072 if(self->io.speed >115200)
1073 {
1074 // Set Dongle Speed mode first
1075 ali_ircc_change_dongle_speed(self, speed);
1076
1077 FIR2SIR(iobase);
1078 }
1079
1080 // Clear Line and Auxiluary status registers 2000/11/24 11:47AM
1081
1082 inb(iobase+UART_LSR);
1083 inb(iobase+UART_SCR);
1084
1085 /* Update accounting for new speed */
1086 self->io.speed = speed;
1087
1088 spin_lock_irqsave(&self->lock, flags);
1089
1090 divisor = 115200/speed;
1091
1092 fcr = UART_FCR_ENABLE_FIFO;
1093
1094 /*
1095 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
1096 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1097 * about this timeout since it will always be fast enough.
1098 */
1099 if (self->io.speed < 38400)
1100 fcr |= UART_FCR_TRIGGER_1;
1101 else
1102 fcr |= UART_FCR_TRIGGER_14;
1103
1104 /* IrDA ports use 8N1 */
1105 lcr = UART_LCR_WLEN8;
1106
1107 outb(UART_LCR_DLAB | lcr, iobase+UART_LCR); /* Set DLAB */
1108 outb(divisor & 0xff, iobase+UART_DLL); /* Set speed */
1109 outb(divisor >> 8, iobase+UART_DLM);
1110 outb(lcr, iobase+UART_LCR); /* Set 8N1 */
1111 outb(fcr, iobase+UART_FCR); /* Enable FIFO's */
1112
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001113 /* without this, the connection will be broken after come back from FIR speed,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 but with this, the SIR connection is harder to established */
1115 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase+UART_MCR);
1116
1117 spin_unlock_irqrestore(&self->lock, flags);
1118
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001119 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122static void ali_ircc_change_dongle_speed(struct ali_ircc_cb *priv, int speed)
1123{
1124
Joe Perchesc2fd03a2012-06-04 12:44:18 +00001125 struct ali_ircc_cb *self = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int iobase,dongle_id;
1127 int tmp = 0;
1128
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001129 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 iobase = self->io.fir_base; /* or iobase = self->io.sir_base; */
1132 dongle_id = self->io.dongle_id;
1133
1134 /* We are already locked, no need to do it again */
1135
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001136 IRDA_DEBUG(1, "%s(), Set Speed for %s , Speed = %d\n", __func__ , dongle_types[dongle_id], speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 switch_bank(iobase, BANK2);
1139 tmp = inb(iobase+FIR_IRDA_CR);
1140
1141 /* IBM type dongle */
1142 if(dongle_id == 0)
1143 {
1144 if(speed == 4000000)
1145 {
1146 // __ __
1147 // SD/MODE __| |__ __
1148 // __ __
1149 // IRTX __ __| |__
1150 // T1 T2 T3 T4 T5
1151
1152 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1153 tmp |= IRDA_CR_CRC; // CRC=1
1154
1155 switch_bank(iobase, BANK2);
1156 outb(tmp, iobase+FIR_IRDA_CR);
1157
1158 // T1 -> SD/MODE:0 IRTX:0
1159 tmp &= ~0x09;
1160 tmp |= 0x02;
1161 outb(tmp, iobase+FIR_IRDA_CR);
1162 udelay(2);
1163
1164 // T2 -> SD/MODE:1 IRTX:0
1165 tmp &= ~0x01;
1166 tmp |= 0x0a;
1167 outb(tmp, iobase+FIR_IRDA_CR);
1168 udelay(2);
1169
1170 // T3 -> SD/MODE:1 IRTX:1
1171 tmp |= 0x0b;
1172 outb(tmp, iobase+FIR_IRDA_CR);
1173 udelay(2);
1174
1175 // T4 -> SD/MODE:0 IRTX:1
1176 tmp &= ~0x08;
1177 tmp |= 0x03;
1178 outb(tmp, iobase+FIR_IRDA_CR);
1179 udelay(2);
1180
1181 // T5 -> SD/MODE:0 IRTX:0
1182 tmp &= ~0x09;
1183 tmp |= 0x02;
1184 outb(tmp, iobase+FIR_IRDA_CR);
1185 udelay(2);
1186
1187 // reset -> Normal TX output Signal
1188 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1189 }
1190 else /* speed <=1152000 */
1191 {
1192 // __
1193 // SD/MODE __| |__
1194 //
1195 // IRTX ________
1196 // T1 T2 T3
1197
1198 /* MIR 115200, 57600 */
1199 if (speed==1152000)
1200 {
1201 tmp |= 0xA0; //HDLC=1, 1.152Mbps=1
1202 }
1203 else
1204 {
1205 tmp &=~0x80; //HDLC 0.576Mbps
1206 tmp |= 0x20; //HDLC=1,
1207 }
1208
1209 tmp |= IRDA_CR_CRC; // CRC=1
1210
1211 switch_bank(iobase, BANK2);
1212 outb(tmp, iobase+FIR_IRDA_CR);
1213
1214 /* MIR 115200, 57600 */
1215
1216 //switch_bank(iobase, BANK2);
1217 // T1 -> SD/MODE:0 IRTX:0
1218 tmp &= ~0x09;
1219 tmp |= 0x02;
1220 outb(tmp, iobase+FIR_IRDA_CR);
1221 udelay(2);
1222
1223 // T2 -> SD/MODE:1 IRTX:0
1224 tmp &= ~0x01;
1225 tmp |= 0x0a;
1226 outb(tmp, iobase+FIR_IRDA_CR);
1227
1228 // T3 -> SD/MODE:0 IRTX:0
1229 tmp &= ~0x09;
1230 tmp |= 0x02;
1231 outb(tmp, iobase+FIR_IRDA_CR);
1232 udelay(2);
1233
1234 // reset -> Normal TX output Signal
1235 outb(tmp & ~0x02, iobase+FIR_IRDA_CR);
1236 }
1237 }
1238 else if (dongle_id == 1) /* HP HDSL-3600 */
1239 {
1240 switch(speed)
1241 {
1242 case 4000000:
1243 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1244 break;
1245
1246 case 1152000:
1247 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1248 break;
1249
1250 case 576000:
1251 tmp &=~0x80; // HDLC 0.576Mbps
1252 tmp |= 0x20; // HDLC=1,
1253 break;
1254 }
1255
1256 tmp |= IRDA_CR_CRC; // CRC=1
1257
1258 switch_bank(iobase, BANK2);
1259 outb(tmp, iobase+FIR_IRDA_CR);
1260 }
1261 else /* HP HDSL-1100 */
1262 {
1263 if(speed <= 115200) /* SIR */
1264 {
1265
1266 tmp &= ~IRDA_CR_FIR_SIN; // HP sin select = 0
1267
1268 switch_bank(iobase, BANK2);
1269 outb(tmp, iobase+FIR_IRDA_CR);
1270 }
1271 else /* MIR FIR */
1272 {
1273
1274 switch(speed)
1275 {
1276 case 4000000:
1277 tmp &= ~IRDA_CR_HDLC; // HDLC=0
1278 break;
1279
1280 case 1152000:
1281 tmp |= 0xA0; // HDLC=1, 1.152Mbps=1
1282 break;
1283
1284 case 576000:
1285 tmp &=~0x80; // HDLC 0.576Mbps
1286 tmp |= 0x20; // HDLC=1,
1287 break;
1288 }
1289
1290 tmp |= IRDA_CR_CRC; // CRC=1
1291 tmp |= IRDA_CR_FIR_SIN; // HP sin select = 1
1292
1293 switch_bank(iobase, BANK2);
1294 outb(tmp, iobase+FIR_IRDA_CR);
1295 }
1296 }
1297
1298 switch_bank(iobase, BANK0);
1299
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001300 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303/*
1304 * Function ali_ircc_sir_write (driver)
1305 *
1306 * Fill Tx FIFO with transmit data
1307 *
1308 */
1309static int ali_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
1310{
1311 int actual = 0;
1312
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001313 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* Tx FIFO should be empty! */
1316 if (!(inb(iobase+UART_LSR) & UART_LSR_THRE)) {
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001317 IRDA_DEBUG(0, "%s(), failed, fifo not empty!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return 0;
1319 }
1320
1321 /* Fill FIFO with current frame */
1322 while ((fifo_size-- > 0) && (actual < len)) {
1323 /* Transmit next byte */
1324 outb(buf[actual], iobase+UART_TX);
1325
1326 actual++;
1327 }
1328
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001329 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return actual;
1331}
1332
1333/*
1334 * Function ali_ircc_net_open (dev)
1335 *
1336 * Start the device
1337 *
1338 */
1339static int ali_ircc_net_open(struct net_device *dev)
1340{
1341 struct ali_ircc_cb *self;
1342 int iobase;
1343 char hwname[32];
1344
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001345 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 IRDA_ASSERT(dev != NULL, return -1;);
1348
Wang Chen4cf16532008-11-12 23:38:14 -08001349 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 IRDA_ASSERT(self != NULL, return 0;);
1352
1353 iobase = self->io.fir_base;
1354
1355 /* Request IRQ and install Interrupt Handler */
1356 if (request_irq(self->io.irq, ali_ircc_interrupt, 0, dev->name, dev))
1357 {
Joe Perches6c910232014-11-11 13:37:30 -08001358 net_warn_ratelimited("%s, unable to allocate irq=%d\n",
1359 ALI_IRCC_DRIVER_NAME, self->io.irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EAGAIN;
1361 }
1362
1363 /*
1364 * Always allocate the DMA channel after the IRQ, and clean up on
1365 * failure.
1366 */
1367 if (request_dma(self->io.dma, dev->name)) {
Joe Perches6c910232014-11-11 13:37:30 -08001368 net_warn_ratelimited("%s, unable to allocate dma=%d\n",
1369 ALI_IRCC_DRIVER_NAME, self->io.dma);
Julia Lawalla997cbb2012-03-11 11:49:02 +00001370 free_irq(self->io.irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EAGAIN;
1372 }
1373
1374 /* Turn on interrups */
1375 outb(UART_IER_RDI , iobase+UART_IER);
1376
1377 /* Ready to play! */
1378 netif_start_queue(dev); //benjamin by irport
1379
1380 /* Give self a hardware name */
1381 sprintf(hwname, "ALI-FIR @ 0x%03x", self->io.fir_base);
1382
1383 /*
1384 * Open new IrLAP layer instance, now that everything should be
1385 * initialized properly
1386 */
1387 self->irlap = irlap_open(dev, &self->qos, hwname);
1388
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001389 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 return 0;
1392}
1393
1394/*
1395 * Function ali_ircc_net_close (dev)
1396 *
1397 * Stop the device
1398 *
1399 */
1400static int ali_ircc_net_close(struct net_device *dev)
1401{
1402
1403 struct ali_ircc_cb *self;
1404 //int iobase;
1405
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001406 IRDA_DEBUG(4, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 IRDA_ASSERT(dev != NULL, return -1;);
1409
Wang Chen4cf16532008-11-12 23:38:14 -08001410 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 IRDA_ASSERT(self != NULL, return 0;);
1412
1413 /* Stop device */
1414 netif_stop_queue(dev);
1415
1416 /* Stop and remove instance of IrLAP */
1417 if (self->irlap)
1418 irlap_close(self->irlap);
1419 self->irlap = NULL;
1420
1421 disable_dma(self->io.dma);
1422
1423 /* Disable interrupts */
1424 SetCOMInterrupts(self, FALSE);
1425
1426 free_irq(self->io.irq, dev);
1427 free_dma(self->io.dma);
1428
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001429 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 return 0;
1432}
1433
1434/*
1435 * Function ali_ircc_fir_hard_xmit (skb, dev)
1436 *
1437 * Transmit the frame
1438 *
1439 */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +00001440static netdev_tx_t ali_ircc_fir_hard_xmit(struct sk_buff *skb,
1441 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
1443 struct ali_ircc_cb *self;
1444 unsigned long flags;
1445 int iobase;
1446 __u32 speed;
1447 int mtt, diff;
1448
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001449 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Wang Chen4cf16532008-11-12 23:38:14 -08001451 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 iobase = self->io.fir_base;
1453
1454 netif_stop_queue(dev);
1455
1456 /* Make sure tests *& speed change are atomic */
1457 spin_lock_irqsave(&self->lock, flags);
1458
1459 /* Note : you should make sure that speed changes are not going
1460 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1461 * details - Jean II */
1462
1463 /* Check if we need to change the speed */
1464 speed = irda_get_next_speed(skb);
1465 if ((speed != self->io.speed) && (speed != -1)) {
1466 /* Check for empty frame */
1467 if (!skb->len) {
1468 ali_ircc_change_speed(self, speed);
1469 dev->trans_start = jiffies;
1470 spin_unlock_irqrestore(&self->lock, flags);
1471 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001472 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 } else
1474 self->new_speed = speed;
1475 }
1476
1477 /* Register and copy this frame to DMA memory */
1478 self->tx_fifo.queue[self->tx_fifo.free].start = self->tx_fifo.tail;
1479 self->tx_fifo.queue[self->tx_fifo.free].len = skb->len;
1480 self->tx_fifo.tail += skb->len;
1481
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001482 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -03001484 skb_copy_from_linear_data(skb, self->tx_fifo.queue[self->tx_fifo.free].start,
1485 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 self->tx_fifo.len++;
1487 self->tx_fifo.free++;
1488
1489 /* Start transmit only if there is currently no transmit going on */
1490 if (self->tx_fifo.len == 1)
1491 {
1492 /* Check if we must wait the min turn time or not */
1493 mtt = irda_get_mtt(skb);
1494
1495 if (mtt)
1496 {
1497 /* Check how much time we have used already */
1498 do_gettimeofday(&self->now);
1499
1500 diff = self->now.tv_usec - self->stamp.tv_usec;
1501 /* self->stamp is set from ali_ircc_dma_receive_complete() */
1502
Frans Pop19299b342010-03-24 07:57:30 +00001503 IRDA_DEBUG(1, "%s(), ******* diff = %d *******\n", __func__ , diff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 if (diff < 0)
1506 diff += 1000000;
1507
1508 /* Check if the mtt is larger than the time we have
1509 * already used by all the protocol processing
1510 */
1511 if (mtt > diff)
1512 {
1513 mtt -= diff;
1514
1515 /*
1516 * Use timer if delay larger than 1000 us, and
1517 * use udelay for smaller values which should
1518 * be acceptable
1519 */
1520 if (mtt > 500)
1521 {
1522 /* Adjust for timer resolution */
1523 mtt = (mtt+250) / 500; /* 4 discard, 5 get advanced, Let's round off */
1524
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001525 IRDA_DEBUG(1, "%s(), ************** mtt = %d ***********\n", __func__ , mtt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 /* Setup timer */
1528 if (mtt == 1) /* 500 us */
1529 {
1530 switch_bank(iobase, BANK1);
1531 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR);
1532 }
1533 else if (mtt == 2) /* 1 ms */
1534 {
1535 switch_bank(iobase, BANK1);
1536 outb(TIMER_IIR_1ms, iobase+FIR_TIMER_IIR);
1537 }
1538 else /* > 2ms -> 4ms */
1539 {
1540 switch_bank(iobase, BANK1);
1541 outb(TIMER_IIR_2ms, iobase+FIR_TIMER_IIR);
1542 }
1543
1544
1545 /* Start timer */
1546 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1547 self->io.direction = IO_XMIT;
1548
1549 /* Enable timer interrupt */
1550 self->ier = IER_TIMER;
1551 SetCOMInterrupts(self, TRUE);
1552
1553 /* Timer will take care of the rest */
1554 goto out;
1555 }
1556 else
1557 udelay(mtt);
1558 } // if (if (mtt > diff)
1559 }// if (mtt)
1560
1561 /* Enable EOM interrupt */
1562 self->ier = IER_EOM;
1563 SetCOMInterrupts(self, TRUE);
1564
1565 /* Transmit frame */
1566 ali_ircc_dma_xmit(self);
1567 } // if (self->tx_fifo.len == 1)
1568
1569 out:
1570
1571 /* Not busy transmitting anymore if window is not full */
1572 if (self->tx_fifo.free < MAX_TX_WINDOW)
1573 netif_wake_queue(self->netdev);
1574
1575 /* Restore bank register */
1576 switch_bank(iobase, BANK0);
1577
1578 dev->trans_start = jiffies;
1579 spin_unlock_irqrestore(&self->lock, flags);
1580 dev_kfree_skb(skb);
1581
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001582 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Patrick McHardy6ed10652009-06-23 06:03:08 +00001583 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
1586
1587static void ali_ircc_dma_xmit(struct ali_ircc_cb *self)
1588{
1589 int iobase, tmp;
1590 unsigned char FIFO_OPTI, Hi, Lo;
1591
1592
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001593 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 iobase = self->io.fir_base;
1596
1597 /* FIFO threshold , this method comes from NDIS5 code */
1598
1599 if(self->tx_fifo.queue[self->tx_fifo.ptr].len < TX_FIFO_Threshold)
1600 FIFO_OPTI = self->tx_fifo.queue[self->tx_fifo.ptr].len-1;
1601 else
1602 FIFO_OPTI = TX_FIFO_Threshold;
1603
1604 /* Disable DMA */
1605 switch_bank(iobase, BANK1);
1606 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1607
1608 self->io.direction = IO_XMIT;
1609
1610 irda_setup_dma(self->io.dma,
1611 ((u8 *)self->tx_fifo.queue[self->tx_fifo.ptr].start -
1612 self->tx_buff.head) + self->tx_buff_dma,
1613 self->tx_fifo.queue[self->tx_fifo.ptr].len,
1614 DMA_TX_MODE);
1615
1616 /* Reset Tx FIFO */
1617 switch_bank(iobase, BANK0);
1618 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1619
1620 /* Set Tx FIFO threshold */
1621 if (self->fifo_opti_buf!=FIFO_OPTI)
1622 {
1623 switch_bank(iobase, BANK1);
1624 outb(FIFO_OPTI, iobase+FIR_FIFO_TR) ;
1625 self->fifo_opti_buf=FIFO_OPTI;
1626 }
1627
1628 /* Set Tx DMA threshold */
1629 switch_bank(iobase, BANK1);
1630 outb(TX_DMA_Threshold, iobase+FIR_DMA_TR);
1631
1632 /* Set max Tx frame size */
1633 Hi = (self->tx_fifo.queue[self->tx_fifo.ptr].len >> 8) & 0x0f;
1634 Lo = self->tx_fifo.queue[self->tx_fifo.ptr].len & 0xff;
1635 switch_bank(iobase, BANK2);
1636 outb(Hi, iobase+FIR_TX_DSR_HI);
1637 outb(Lo, iobase+FIR_TX_DSR_LO);
1638
1639 /* Disable SIP , Disable Brick Wall (we don't support in TX mode), Change to TX mode */
1640 switch_bank(iobase, BANK0);
1641 tmp = inb(iobase+FIR_LCR_B);
1642 tmp &= ~0x20; // Disable SIP
1643 outb(((unsigned char)(tmp & 0x3f) | LCR_B_TX_MODE) & ~LCR_B_BW, iobase+FIR_LCR_B);
Frans Pop19299b342010-03-24 07:57:30 +00001644 IRDA_DEBUG(1, "%s(), *** Change to TX mode: FIR_LCR_B = 0x%x ***\n", __func__ , inb(iobase+FIR_LCR_B));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 outb(0, iobase+FIR_LSR);
1647
1648 /* Enable DMA and Burst Mode */
1649 switch_bank(iobase, BANK1);
1650 outb(inb(iobase+FIR_CR) | CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1651
1652 switch_bank(iobase, BANK0);
1653
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001654 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657static int ali_ircc_dma_xmit_complete(struct ali_ircc_cb *self)
1658{
1659 int iobase;
1660 int ret = TRUE;
1661
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001662 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 iobase = self->io.fir_base;
1665
1666 /* Disable DMA */
1667 switch_bank(iobase, BANK1);
1668 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1669
1670 /* Check for underrun! */
1671 switch_bank(iobase, BANK0);
1672 if((inb(iobase+FIR_LSR) & LSR_FRAME_ABORT) == LSR_FRAME_ABORT)
1673
1674 {
Joe Perches6c910232014-11-11 13:37:30 -08001675 net_err_ratelimited("%s(), ********* LSR_FRAME_ABORT *********\n",
1676 __func__);
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001677 self->netdev->stats.tx_errors++;
1678 self->netdev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
1680 else
1681 {
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001682 self->netdev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684
1685 /* Check if we need to change the speed */
1686 if (self->new_speed)
1687 {
1688 ali_ircc_change_speed(self, self->new_speed);
1689 self->new_speed = 0;
1690 }
1691
1692 /* Finished with this frame, so prepare for next */
1693 self->tx_fifo.ptr++;
1694 self->tx_fifo.len--;
1695
1696 /* Any frames to be sent back-to-back? */
1697 if (self->tx_fifo.len)
1698 {
1699 ali_ircc_dma_xmit(self);
1700
1701 /* Not finished yet! */
1702 ret = FALSE;
1703 }
1704 else
1705 { /* Reset Tx FIFO info */
1706 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1707 self->tx_fifo.tail = self->tx_buff.head;
1708 }
1709
1710 /* Make sure we have room for more frames */
1711 if (self->tx_fifo.free < MAX_TX_WINDOW) {
1712 /* Not busy transmitting anymore */
1713 /* Tell the network layer, that we can accept more frames */
1714 netif_wake_queue(self->netdev);
1715 }
1716
1717 switch_bank(iobase, BANK0);
1718
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001719 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return ret;
1721}
1722
1723/*
1724 * Function ali_ircc_dma_receive (self)
1725 *
1726 * Get ready for receiving a frame. The device will initiate a DMA
1727 * if it starts to receive a frame.
1728 *
1729 */
1730static int ali_ircc_dma_receive(struct ali_ircc_cb *self)
1731{
1732 int iobase, tmp;
1733
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001734 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 iobase = self->io.fir_base;
1737
1738 /* Reset Tx FIFO info */
1739 self->tx_fifo.len = self->tx_fifo.ptr = self->tx_fifo.free = 0;
1740 self->tx_fifo.tail = self->tx_buff.head;
1741
1742 /* Disable DMA */
1743 switch_bank(iobase, BANK1);
1744 outb(inb(iobase+FIR_CR) & ~CR_DMA_EN, iobase+FIR_CR);
1745
1746 /* Reset Message Count */
1747 switch_bank(iobase, BANK0);
1748 outb(0x07, iobase+FIR_LSR);
1749
1750 self->rcvFramesOverflow = FALSE;
1751
1752 self->LineStatus = inb(iobase+FIR_LSR) ;
1753
1754 /* Reset Rx FIFO info */
1755 self->io.direction = IO_RECV;
1756 self->rx_buff.data = self->rx_buff.head;
1757
1758 /* Reset Rx FIFO */
1759 // switch_bank(iobase, BANK0);
1760 outb(LCR_A_FIFO_RESET, iobase+FIR_LCR_A);
1761
1762 self->st_fifo.len = self->st_fifo.pending_bytes = 0;
1763 self->st_fifo.tail = self->st_fifo.head = 0;
1764
1765 irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
1766 DMA_RX_MODE);
1767
1768 /* Set Receive Mode,Brick Wall */
1769 //switch_bank(iobase, BANK0);
1770 tmp = inb(iobase+FIR_LCR_B);
1771 outb((unsigned char)(tmp &0x3f) | LCR_B_RX_MODE | LCR_B_BW , iobase + FIR_LCR_B); // 2000/12/1 05:16PM
Frans Pop19299b342010-03-24 07:57:30 +00001772 IRDA_DEBUG(1, "%s(), *** Change To RX mode: FIR_LCR_B = 0x%x ***\n", __func__ , inb(iobase+FIR_LCR_B));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 /* Set Rx Threshold */
1775 switch_bank(iobase, BANK1);
1776 outb(RX_FIFO_Threshold, iobase+FIR_FIFO_TR);
1777 outb(RX_DMA_Threshold, iobase+FIR_DMA_TR);
1778
1779 /* Enable DMA and Burst Mode */
1780 // switch_bank(iobase, BANK1);
1781 outb(CR_DMA_EN | CR_DMA_BURST, iobase+FIR_CR);
1782
1783 switch_bank(iobase, BANK0);
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001784 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return 0;
1786}
1787
1788static int ali_ircc_dma_receive_complete(struct ali_ircc_cb *self)
1789{
1790 struct st_fifo *st_fifo;
1791 struct sk_buff *skb;
1792 __u8 status, MessageCount;
1793 int len, i, iobase, val;
1794
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001795 IRDA_DEBUG(1, "%s(), ---------------- Start -----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 st_fifo = &self->st_fifo;
1798 iobase = self->io.fir_base;
1799
1800 switch_bank(iobase, BANK0);
1801 MessageCount = inb(iobase+ FIR_LSR)&0x07;
1802
1803 if (MessageCount > 0)
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02001804 IRDA_DEBUG(0, "%s(), Message count = %d,\n", __func__ , MessageCount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 for (i=0; i<=MessageCount; i++)
1807 {
1808 /* Bank 0 */
1809 switch_bank(iobase, BANK0);
1810 status = inb(iobase+FIR_LSR);
1811
1812 switch_bank(iobase, BANK2);
1813 len = inb(iobase+FIR_RX_DSR_HI) & 0x0f;
1814 len = len << 8;
1815 len |= inb(iobase+FIR_RX_DSR_LO);
1816
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001817 IRDA_DEBUG(1, "%s(), RX Length = 0x%.2x,\n", __func__ , len);
1818 IRDA_DEBUG(1, "%s(), RX Status = 0x%.2x,\n", __func__ , status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 if (st_fifo->tail >= MAX_RX_WINDOW) {
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001821 IRDA_DEBUG(0, "%s(), window is full!\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 continue;
1823 }
1824
1825 st_fifo->entries[st_fifo->tail].status = status;
1826 st_fifo->entries[st_fifo->tail].len = len;
1827 st_fifo->pending_bytes += len;
1828 st_fifo->tail++;
1829 st_fifo->len++;
1830 }
1831
1832 for (i=0; i<=MessageCount; i++)
1833 {
1834 /* Get first entry */
1835 status = st_fifo->entries[st_fifo->head].status;
1836 len = st_fifo->entries[st_fifo->head].len;
1837 st_fifo->pending_bytes -= len;
1838 st_fifo->head++;
1839 st_fifo->len--;
1840
1841 /* Check for errors */
1842 if ((status & 0xd8) || self->rcvFramesOverflow || (len==0))
1843 {
Frans Pop19299b342010-03-24 07:57:30 +00001844 IRDA_DEBUG(0,"%s(), ************* RX Errors ************\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846 /* Skip frame */
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001847 self->netdev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 self->rx_buff.data += len;
1850
1851 if (status & LSR_FIFO_UR)
1852 {
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001853 self->netdev->stats.rx_frame_errors++;
Frans Pop19299b342010-03-24 07:57:30 +00001854 IRDA_DEBUG(0,"%s(), ************* FIFO Errors ************\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856 if (status & LSR_FRAME_ERROR)
1857 {
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001858 self->netdev->stats.rx_frame_errors++;
Frans Pop19299b342010-03-24 07:57:30 +00001859 IRDA_DEBUG(0,"%s(), ************* FRAME Errors ************\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861
1862 if (status & LSR_CRC_ERROR)
1863 {
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001864 self->netdev->stats.rx_crc_errors++;
Frans Pop19299b342010-03-24 07:57:30 +00001865 IRDA_DEBUG(0,"%s(), ************* CRC Errors ************\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
1868 if(self->rcvFramesOverflow)
1869 {
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001870 self->netdev->stats.rx_frame_errors++;
Frans Pop19299b342010-03-24 07:57:30 +00001871 IRDA_DEBUG(0,"%s(), ************* Overran DMA buffer ************\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873 if(len == 0)
1874 {
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001875 self->netdev->stats.rx_frame_errors++;
Frans Pop19299b342010-03-24 07:57:30 +00001876 IRDA_DEBUG(0,"%s(), ********** Receive Frame Size = 0 *********\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878 }
1879 else
1880 {
1881
1882 if (st_fifo->pending_bytes < 32)
1883 {
1884 switch_bank(iobase, BANK0);
1885 val = inb(iobase+FIR_BSR);
1886 if ((val& BSR_FIFO_NOT_EMPTY)== 0x80)
1887 {
Frans Pop19299b342010-03-24 07:57:30 +00001888 IRDA_DEBUG(0, "%s(), ************* BSR_FIFO_NOT_EMPTY ************\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /* Put this entry back in fifo */
1891 st_fifo->head--;
1892 st_fifo->len++;
1893 st_fifo->pending_bytes += len;
1894 st_fifo->entries[st_fifo->head].status = status;
1895 st_fifo->entries[st_fifo->head].len = len;
1896
1897 /*
1898 * DMA not finished yet, so try again
1899 * later, set timer value, resolution
1900 * 500 us
1901 */
1902
1903 switch_bank(iobase, BANK1);
1904 outb(TIMER_IIR_500, iobase+FIR_TIMER_IIR); // 2001/1/2 05:07PM
1905
1906 /* Enable Timer */
1907 outb(inb(iobase+FIR_CR) | CR_TIMER_EN, iobase+FIR_CR);
1908
1909 return FALSE; /* I'll be back! */
1910 }
1911 }
1912
1913 /*
1914 * Remember the time we received this frame, so we can
1915 * reduce the min turn time a bit since we will know
1916 * how much time we have used for protocol processing
1917 */
1918 do_gettimeofday(&self->stamp);
1919
1920 skb = dev_alloc_skb(len+1);
1921 if (skb == NULL)
1922 {
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001923 self->netdev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 return FALSE;
1926 }
1927
1928 /* Make sure IP header gets aligned */
1929 skb_reserve(skb, 1);
1930
1931 /* Copy frame without CRC, CRC is removed by hardware*/
1932 skb_put(skb, len);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001933 skb_copy_to_linear_data(skb, self->rx_buff.data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* Move to next frame */
1936 self->rx_buff.data += len;
Stephen Hemmingeraf049082009-01-06 10:40:43 -08001937 self->netdev->stats.rx_bytes += len;
1938 self->netdev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 skb->dev = self->netdev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -07001941 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 skb->protocol = htons(ETH_P_IRDA);
1943 netif_rx(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945 }
1946
1947 switch_bank(iobase, BANK0);
1948
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001949 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return TRUE;
1951}
1952
1953
1954
1955/*
1956 * Function ali_ircc_sir_hard_xmit (skb, dev)
1957 *
1958 * Transmit the frame!
1959 *
1960 */
Stephen Hemminger6518bbb2009-08-31 19:50:50 +00001961static netdev_tx_t ali_ircc_sir_hard_xmit(struct sk_buff *skb,
1962 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 struct ali_ircc_cb *self;
1965 unsigned long flags;
1966 int iobase;
1967 __u32 speed;
1968
Harvey Harrisona97a6f12008-07-30 17:20:18 -07001969 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Patrick McHardyec634fe2009-07-05 19:23:38 -07001971 IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Wang Chen4cf16532008-11-12 23:38:14 -08001973 self = netdev_priv(dev);
Patrick McHardyec634fe2009-07-05 19:23:38 -07001974 IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
1976 iobase = self->io.sir_base;
1977
1978 netif_stop_queue(dev);
1979
1980 /* Make sure tests *& speed change are atomic */
1981 spin_lock_irqsave(&self->lock, flags);
1982
1983 /* Note : you should make sure that speed changes are not going
1984 * to corrupt any outgoing frame. Look at nsc-ircc for the gory
1985 * details - Jean II */
1986
1987 /* Check if we need to change the speed */
1988 speed = irda_get_next_speed(skb);
1989 if ((speed != self->io.speed) && (speed != -1)) {
1990 /* Check for empty frame */
1991 if (!skb->len) {
1992 ali_ircc_change_speed(self, speed);
1993 dev->trans_start = jiffies;
1994 spin_unlock_irqrestore(&self->lock, flags);
1995 dev_kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +00001996 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 } else
1998 self->new_speed = speed;
1999 }
2000
2001 /* Init tx buffer */
2002 self->tx_buff.data = self->tx_buff.head;
2003
2004 /* Copy skb to tx_buff while wrapping, stuffing and making CRC */
2005 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
2006 self->tx_buff.truesize);
2007
Stephen Hemmingeraf049082009-01-06 10:40:43 -08002008 self->netdev->stats.tx_bytes += self->tx_buff.len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 /* Turn on transmit finished interrupt. Will fire immediately! */
2011 outb(UART_IER_THRI, iobase+UART_IER);
2012
2013 dev->trans_start = jiffies;
2014 spin_unlock_irqrestore(&self->lock, flags);
2015
2016 dev_kfree_skb(skb);
2017
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002018 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Patrick McHardy6ed10652009-06-23 06:03:08 +00002020 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021}
2022
2023
2024/*
2025 * Function ali_ircc_net_ioctl (dev, rq, cmd)
2026 *
2027 * Process IOCTL commands for this device
2028 *
2029 */
2030static int ali_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2031{
2032 struct if_irda_req *irq = (struct if_irda_req *) rq;
2033 struct ali_ircc_cb *self;
2034 unsigned long flags;
2035 int ret = 0;
2036
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002037 IRDA_DEBUG(2, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 IRDA_ASSERT(dev != NULL, return -1;);
2040
Wang Chen4cf16532008-11-12 23:38:14 -08002041 self = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 IRDA_ASSERT(self != NULL, return -1;);
2044
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002045 IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __func__ , dev->name, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 switch (cmd) {
2048 case SIOCSBANDWIDTH: /* Set bandwidth */
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002049 IRDA_DEBUG(1, "%s(), SIOCSBANDWIDTH\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /*
2051 * This function will also be used by IrLAP to change the
2052 * speed, so we still must allow for speed change within
2053 * interrupt context.
2054 */
2055 if (!in_interrupt() && !capable(CAP_NET_ADMIN))
2056 return -EPERM;
2057
2058 spin_lock_irqsave(&self->lock, flags);
2059 ali_ircc_change_speed(self, irq->ifr_baudrate);
2060 spin_unlock_irqrestore(&self->lock, flags);
2061 break;
2062 case SIOCSMEDIABUSY: /* Set media busy */
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002063 IRDA_DEBUG(1, "%s(), SIOCSMEDIABUSY\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (!capable(CAP_NET_ADMIN))
2065 return -EPERM;
2066 irda_device_set_media_busy(self->netdev, TRUE);
2067 break;
2068 case SIOCGRECEIVING: /* Check if we are receiving right now */
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002069 IRDA_DEBUG(2, "%s(), SIOCGRECEIVING\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* This is protected */
2071 irq->ifr_receiving = ali_ircc_is_receiving(self);
2072 break;
2073 default:
2074 ret = -EOPNOTSUPP;
2075 }
2076
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002077 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 return ret;
2080}
2081
2082/*
2083 * Function ali_ircc_is_receiving (self)
2084 *
2085 * Return TRUE is we are currently receiving a frame
2086 *
2087 */
2088static int ali_ircc_is_receiving(struct ali_ircc_cb *self)
2089{
2090 unsigned long flags;
2091 int status = FALSE;
2092 int iobase;
2093
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002094 IRDA_DEBUG(2, "%s(), ---------------- Start -----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096 IRDA_ASSERT(self != NULL, return FALSE;);
2097
2098 spin_lock_irqsave(&self->lock, flags);
2099
2100 if (self->io.speed > 115200)
2101 {
2102 iobase = self->io.fir_base;
2103
2104 switch_bank(iobase, BANK1);
2105 if((inb(iobase+FIR_FIFO_FR) & 0x3f) != 0)
2106 {
2107 /* We are receiving something */
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002108 IRDA_DEBUG(1, "%s(), We are receiving something\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 status = TRUE;
2110 }
2111 switch_bank(iobase, BANK0);
2112 }
2113 else
2114 {
2115 status = (self->rx_buff.state != OUTSIDE_FRAME);
2116 }
2117
2118 spin_unlock_irqrestore(&self->lock, flags);
2119
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002120 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 return status;
2123}
2124
Samuel Ortiz898b1d12006-05-25 16:21:10 -07002125static int ali_ircc_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Samuel Ortiz898b1d12006-05-25 16:21:10 -07002127 struct ali_ircc_cb *self = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Joe Perches6c910232014-11-11 13:37:30 -08002129 net_info_ratelimited("%s, Suspending\n", ALI_IRCC_DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 if (self->io.suspended)
Samuel Ortiz898b1d12006-05-25 16:21:10 -07002132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 ali_ircc_net_close(self->netdev);
2135
2136 self->io.suspended = 1;
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 0;
2139}
2140
Samuel Ortiz898b1d12006-05-25 16:21:10 -07002141static int ali_ircc_resume(struct platform_device *dev)
2142{
2143 struct ali_ircc_cb *self = platform_get_drvdata(dev);
2144
2145 if (!self->io.suspended)
2146 return 0;
2147
2148 ali_ircc_net_open(self->netdev);
2149
Joe Perches6c910232014-11-11 13:37:30 -08002150 net_info_ratelimited("%s, Waking up\n", ALI_IRCC_DRIVER_NAME);
Samuel Ortiz898b1d12006-05-25 16:21:10 -07002151
2152 self->io.suspended = 0;
2153
2154 return 0;
2155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157/* ALi Chip Function */
2158
2159static void SetCOMInterrupts(struct ali_ircc_cb *self , unsigned char enable)
2160{
2161
2162 unsigned char newMask;
2163
2164 int iobase = self->io.fir_base; /* or sir_base */
2165
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002166 IRDA_DEBUG(2, "%s(), -------- Start -------- ( Enable = %d )\n", __func__ , enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /* Enable the interrupt which we wish to */
2169 if (enable){
2170 if (self->io.direction == IO_XMIT)
2171 {
2172 if (self->io.speed > 115200) /* FIR, MIR */
2173 {
2174 newMask = self->ier;
2175 }
2176 else /* SIR */
2177 {
2178 newMask = UART_IER_THRI | UART_IER_RDI;
2179 }
2180 }
2181 else {
2182 if (self->io.speed > 115200) /* FIR, MIR */
2183 {
2184 newMask = self->ier;
2185 }
2186 else /* SIR */
2187 {
2188 newMask = UART_IER_RDI;
2189 }
2190 }
2191 }
2192 else /* Disable all the interrupts */
2193 {
2194 newMask = 0x00;
2195
2196 }
2197
2198 //SIR and FIR has different registers
2199 if (self->io.speed > 115200)
2200 {
2201 switch_bank(iobase, BANK0);
2202 outb(newMask, iobase+FIR_IER);
2203 }
2204 else
2205 outb(newMask, iobase+UART_IER);
2206
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002207 IRDA_DEBUG(2, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
2210static void SIR2FIR(int iobase)
2211{
2212 //unsigned char tmp;
2213
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002214 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216 /* Already protected (change_speed() or setup()), no need to lock.
2217 * Jean II */
2218
2219 outb(0x28, iobase+UART_MCR);
2220 outb(0x68, iobase+UART_MCR);
2221 outb(0x88, iobase+UART_MCR);
2222
2223 outb(0x60, iobase+FIR_MCR); /* Master Reset */
2224 outb(0x20, iobase+FIR_MCR); /* Master Interrupt Enable */
2225
2226 //tmp = inb(iobase+FIR_LCR_B); /* SIP enable */
2227 //tmp |= 0x20;
2228 //outb(tmp, iobase+FIR_LCR_B);
2229
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002230 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231}
2232
2233static void FIR2SIR(int iobase)
2234{
2235 unsigned char val;
2236
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002237 IRDA_DEBUG(1, "%s(), ---------------- Start ----------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* Already protected (change_speed() or setup()), no need to lock.
2240 * Jean II */
2241
2242 outb(0x20, iobase+FIR_MCR); /* IRQ to low */
2243 outb(0x00, iobase+UART_IER);
2244
2245 outb(0xA0, iobase+FIR_MCR); /* Don't set master reset */
2246 outb(0x00, iobase+UART_FCR);
2247 outb(0x07, iobase+UART_FCR);
2248
2249 val = inb(iobase+UART_RX);
2250 val = inb(iobase+UART_LSR);
2251 val = inb(iobase+UART_MSR);
2252
Harvey Harrisona97a6f12008-07-30 17:20:18 -07002253 IRDA_DEBUG(1, "%s(), ----------------- End ------------------\n", __func__ );
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254}
2255
2256MODULE_AUTHOR("Benjamin Kong <benjamin_kong@ali.com.tw>");
2257MODULE_DESCRIPTION("ALi FIR Controller Driver");
2258MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07002259MODULE_ALIAS("platform:" ALI_IRCC_DRIVER_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261
2262module_param_array(io, int, NULL, 0);
2263MODULE_PARM_DESC(io, "Base I/O addresses");
2264module_param_array(irq, int, NULL, 0);
2265MODULE_PARM_DESC(irq, "IRQ lines");
2266module_param_array(dma, int, NULL, 0);
2267MODULE_PARM_DESC(dma, "DMA channels");
2268
2269module_init(ali_ircc_init);
2270module_exit(ali_ircc_cleanup);