smsc-ircc2.c 77.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*********************************************************************
 *
 * Description:   Driver for the SMC Infrared Communications Controller
 * Status:        Experimental.
 * Author:        Daniele Peri (peri@csai.unipa.it)
6 7 8 9
 * Created at:
 * Modified at:
 * Modified by:
 *
L
Linus Torvalds 已提交
10 11 12
 *     Copyright (c) 2002      Daniele Peri
 *     All Rights Reserved.
 *     Copyright (c) 2002      Jean Tourrilhes
13
 *     Copyright (c) 2006      Linus Walleij
L
Linus Torvalds 已提交
14 15 16 17 18 19
 *
 *
 * Based on smc-ircc.c:
 *
 *     Copyright (c) 2001      Stefani Seibold
 *     Copyright (c) 1999-2001 Dag Brattli
20
 *     Copyright (c) 1998-1999 Thomas Davis,
L
Linus Torvalds 已提交
21 22 23 24 25
 *
 *	and irport.c:
 *
 *     Copyright (c) 1997, 1998, 1999-2000 Dag Brattli, All Rights Reserved.
 *
26 27 28 29
 *
 *     This program is free software; you can redistribute it and/or
 *     modify it under the terms of the GNU General Public License as
 *     published by the Free Software Foundation; either version 2 of
L
Linus Torvalds 已提交
30
 *     the License, or (at your option) any later version.
31
 *
L
Linus Torvalds 已提交
32 33 34 35
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *     GNU General Public License for more details.
36 37 38 39
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program; if not, write to the Free Software
 *     Foundation, Inc., 59 Temple Place, Suite 330, Boston,
L
Linus Torvalds 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
 *     MA 02111-1307 USA
 *
 ********************************************************************/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/rtnetlink.h>
#include <linux/serial_reg.h>
#include <linux/dma-mapping.h>
56
#include <linux/pnp.h>
57
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
58 59 60 61 62 63 64

#include <asm/io.h>
#include <asm/dma.h>
#include <asm/byteorder.h>

#include <linux/spinlock.h>
#include <linux/pm.h>
65 66 67
#ifdef CONFIG_PCI
#include <linux/pci.h>
#endif
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75

#include <net/irda/wrapper.h>
#include <net/irda/irda.h>
#include <net/irda/irda_device.h>

#include "smsc-ircc2.h"
#include "smsc-sio.h"

76 77 78 79 80

MODULE_AUTHOR("Daniele Peri <peri@csai.unipa.it>");
MODULE_DESCRIPTION("SMC IrCC SIR/FIR controller driver");
MODULE_LICENSE("GPL");

81
static int smsc_nopnp = 1;
B
Bjorn Helgaas 已提交
82
module_param_named(nopnp, smsc_nopnp, bool, 0);
83
MODULE_PARM_DESC(nopnp, "Do not use PNP to detect controller settings, defaults to true");
B
Bjorn Helgaas 已提交
84

85 86
#define DMA_INVAL 255
static int ircc_dma = DMA_INVAL;
87 88 89
module_param(ircc_dma, int, 0);
MODULE_PARM_DESC(ircc_dma, "DMA channel");

90 91
#define IRQ_INVAL 255
static int ircc_irq = IRQ_INVAL;
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
module_param(ircc_irq, int, 0);
MODULE_PARM_DESC(ircc_irq, "IRQ line");

static int ircc_fir;
module_param(ircc_fir, int, 0);
MODULE_PARM_DESC(ircc_fir, "FIR Base Address");

static int ircc_sir;
module_param(ircc_sir, int, 0);
MODULE_PARM_DESC(ircc_sir, "SIR Base Address");

static int ircc_cfg;
module_param(ircc_cfg, int, 0);
MODULE_PARM_DESC(ircc_cfg, "Configuration register base address");

static int ircc_transceiver;
module_param(ircc_transceiver, int, 0);
MODULE_PARM_DESC(ircc_transceiver, "Transceiver type");

L
Linus Torvalds 已提交
111 112
/* Types */

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#ifdef CONFIG_PCI
struct smsc_ircc_subsystem_configuration {
	unsigned short vendor; /* PCI vendor ID */
	unsigned short device; /* PCI vendor ID */
	unsigned short subvendor; /* PCI subsystem vendor ID */
	unsigned short subdevice; /* PCI sybsystem device ID */
	unsigned short sir_io; /* I/O port for SIR */
	unsigned short fir_io; /* I/O port for FIR */
	unsigned char  fir_irq; /* FIR IRQ */
	unsigned char  fir_dma; /* FIR DMA */
	unsigned short cfg_base; /* I/O port for chip configuration */
	int (*preconfigure)(struct pci_dev *dev, struct smsc_ircc_subsystem_configuration *conf); /* Preconfig function */
	const char *name;	/* name shown as info */
};
#endif

L
Linus Torvalds 已提交
129 130
struct smsc_transceiver {
	char *name;
131
	void (*set_for_speed)(int fir_base, u32 speed);
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	int  (*probe)(int fir_base);
};

struct smsc_chip {
	char *name;
	#if 0
	u8	type;
	#endif
	u16 flags;
	u8 devid;
	u8 rev;
};

struct smsc_chip_address {
	unsigned int cfg_base;
	unsigned int type;
};

/* Private data for each instance */
struct smsc_ircc_cb {
	struct net_device *netdev;     /* Yes! we are some kind of netdevice */
	struct irlap_cb    *irlap; /* The link layer we are binded to */
154

L
Linus Torvalds 已提交
155 156 157 158 159 160 161 162 163
	chipio_t io;               /* IrDA controller information */
	iobuff_t tx_buff;          /* Transmit buffer */
	iobuff_t rx_buff;          /* Receive buffer */
	dma_addr_t tx_buff_dma;
	dma_addr_t rx_buff_dma;

	struct qos_info qos;       /* QoS capabilities for this device */

	spinlock_t lock;           /* For serializing operations */
164

L
Linus Torvalds 已提交
165 166 167 168 169 170 171
	__u32 new_speed;
	__u32 flags;               /* Interface flags */

	int tx_buff_offsets[10];   /* Offsets between frames in tx_buff */
	int tx_len;                /* Number of frames in tx_buff */

	int transceiver;
172
	struct platform_device *pldev;
L
Linus Torvalds 已提交
173 174 175 176
};

/* Constants */

177 178
#define SMSC_IRCC2_DRIVER_NAME			"smsc-ircc2"

L
Linus Torvalds 已提交
179 180
#define SMSC_IRCC2_C_IRDA_FALLBACK_SPEED	9600
#define SMSC_IRCC2_C_DEFAULT_TRANSCEIVER	1
181
#define SMSC_IRCC2_C_NET_TIMEOUT		0
L
Linus Torvalds 已提交
182 183
#define SMSC_IRCC2_C_SIR_STOP			0

184 185
static const char *driver_name = SMSC_IRCC2_DRIVER_NAME;

L
Linus Torvalds 已提交
186 187 188 189 190 191 192 193
/* Prototypes */

static int smsc_ircc_open(unsigned int firbase, unsigned int sirbase, u8 dma, u8 irq);
static int smsc_ircc_present(unsigned int fir_base, unsigned int sir_base);
static void smsc_ircc_setup_io(struct smsc_ircc_cb *self, unsigned int fir_base, unsigned int sir_base, u8 dma, u8 irq);
static void smsc_ircc_setup_qos(struct smsc_ircc_cb *self);
static void smsc_ircc_init_chip(struct smsc_ircc_cb *self);
static int __exit smsc_ircc_close(struct smsc_ircc_cb *self);
194 195
static int  smsc_ircc_dma_receive(struct smsc_ircc_cb *self);
static void smsc_ircc_dma_receive_complete(struct smsc_ircc_cb *self);
L
Linus Torvalds 已提交
196
static void smsc_ircc_sir_receive(struct smsc_ircc_cb *self);
S
Stephen Hemminger 已提交
197 198 199 200
static netdev_tx_t  smsc_ircc_hard_xmit_sir(struct sk_buff *skb,
						  struct net_device *dev);
static netdev_tx_t  smsc_ircc_hard_xmit_fir(struct sk_buff *skb,
						  struct net_device *dev);
201 202
static void smsc_ircc_dma_xmit(struct smsc_ircc_cb *self, int bofs);
static void smsc_ircc_dma_xmit_complete(struct smsc_ircc_cb *self);
203 204
static void smsc_ircc_change_speed(struct smsc_ircc_cb *self, u32 speed);
static void smsc_ircc_set_sir_speed(struct smsc_ircc_cb *self, u32 speed);
205
static irqreturn_t smsc_ircc_interrupt(int irq, void *dev_id);
L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
static irqreturn_t smsc_ircc_interrupt_sir(struct net_device *dev);
static void smsc_ircc_sir_start(struct smsc_ircc_cb *self);
#if SMSC_IRCC2_C_SIR_STOP
static void smsc_ircc_sir_stop(struct smsc_ircc_cb *self);
#endif
static void smsc_ircc_sir_write_wakeup(struct smsc_ircc_cb *self);
static int  smsc_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len);
static int  smsc_ircc_net_open(struct net_device *dev);
static int  smsc_ircc_net_close(struct net_device *dev);
static int  smsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
#if SMSC_IRCC2_C_NET_TIMEOUT
static void smsc_ircc_timeout(struct net_device *dev);
#endif
static int smsc_ircc_is_receiving(struct smsc_ircc_cb *self);
static void smsc_ircc_probe_transceiver(struct smsc_ircc_cb *self);
static void smsc_ircc_set_transceiver_for_speed(struct smsc_ircc_cb *self, u32 speed);
static void smsc_ircc_sir_wait_hw_transmitter_finish(struct smsc_ircc_cb *self);

/* Probing */
static int __init smsc_ircc_look_for_chips(void);
226 227 228
static const struct smsc_chip * __init smsc_ircc_probe(unsigned short cfg_base, u8 reg, const struct smsc_chip *chip, char *type);
static int __init smsc_superio_flat(const struct smsc_chip *chips, unsigned short cfg_base, char *type);
static int __init smsc_superio_paged(const struct smsc_chip *chips, unsigned short cfg_base, char *type);
L
Linus Torvalds 已提交
229 230
static int __init smsc_superio_fdc(unsigned short cfg_base);
static int __init smsc_superio_lpc(unsigned short cfg_base);
231 232 233
#ifdef CONFIG_PCI
static int __init preconfigure_smsc_chip(struct smsc_ircc_subsystem_configuration *conf);
static int __init preconfigure_through_82801(struct pci_dev *dev, struct smsc_ircc_subsystem_configuration *conf);
234 235
static void __init preconfigure_ali_port(struct pci_dev *dev,
					 unsigned short port);
236 237 238 239 240 241 242
static int __init preconfigure_through_ali(struct pci_dev *dev, struct smsc_ircc_subsystem_configuration *conf);
static int __init smsc_ircc_preconfigure_subsystems(unsigned short ircc_cfg,
						    unsigned short ircc_fir,
						    unsigned short ircc_sir,
						    unsigned char ircc_dma,
						    unsigned char ircc_irq);
#endif
L
Linus Torvalds 已提交
243 244 245 246 247 248 249 250 251 252 253 254

/* Transceivers specific functions */

static void smsc_ircc_set_transceiver_toshiba_sat1800(int fir_base, u32 speed);
static int  smsc_ircc_probe_transceiver_toshiba_sat1800(int fir_base);
static void smsc_ircc_set_transceiver_smsc_ircc_fast_pin_select(int fir_base, u32 speed);
static int  smsc_ircc_probe_transceiver_smsc_ircc_fast_pin_select(int fir_base);
static void smsc_ircc_set_transceiver_smsc_ircc_atc(int fir_base, u32 speed);
static int  smsc_ircc_probe_transceiver_smsc_ircc_atc(int fir_base);

/* Power Management */

255 256
static int smsc_ircc_suspend(struct platform_device *dev, pm_message_t state);
static int smsc_ircc_resume(struct platform_device *dev);
L
Linus Torvalds 已提交
257

258
static struct platform_driver smsc_ircc_driver = {
259 260
	.suspend	= smsc_ircc_suspend,
	.resume		= smsc_ircc_resume,
261 262 263
	.driver		= {
		.name	= SMSC_IRCC2_DRIVER_NAME,
	},
264
};
L
Linus Torvalds 已提交
265 266 267

/* Transceivers for SMSC-ircc */

268
static struct smsc_transceiver smsc_transceivers[] =
L
Linus Torvalds 已提交
269
{
270 271 272 273
	{ "Toshiba Satellite 1800 (GP data pin select)", smsc_ircc_set_transceiver_toshiba_sat1800, smsc_ircc_probe_transceiver_toshiba_sat1800 },
	{ "Fast pin select", smsc_ircc_set_transceiver_smsc_ircc_fast_pin_select, smsc_ircc_probe_transceiver_smsc_ircc_fast_pin_select },
	{ "ATC IRMode", smsc_ircc_set_transceiver_smsc_ircc_atc, smsc_ircc_probe_transceiver_smsc_ircc_atc },
	{ NULL, NULL }
L
Linus Torvalds 已提交
274
};
275
#define SMSC_IRCC2_C_NUMBER_OF_TRANSCEIVERS (ARRAY_SIZE(smsc_transceivers) - 1)
L
Linus Torvalds 已提交
276 277 278 279 280 281 282 283 284 285

/*  SMC SuperIO chipsets definitions */

#define	KEY55_1	0	/* SuperIO Configuration mode with Key <0x55> */
#define	KEY55_2	1	/* SuperIO Configuration mode with Key <0x55,0x55> */
#define	NoIRDA	2	/* SuperIO Chip has no IRDA Port */
#define	SIR	0	/* SuperIO Chip has only slow IRDA */
#define	FIR	4	/* SuperIO Chip has fast IRDA */
#define	SERx4	8	/* SuperIO Chip supports 115,2 KBaud * 4=460,8 KBaud */

286
static struct smsc_chip __initdata fdc_chips_flat[] =
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299
{
	/* Base address 0x3f0 or 0x370 */
	{ "37C44",	KEY55_1|NoIRDA,		0x00, 0x00 }, /* This chip cannot be detected */
	{ "37C665GT",	KEY55_2|NoIRDA,		0x65, 0x01 },
	{ "37C665GT",	KEY55_2|NoIRDA,		0x66, 0x01 },
	{ "37C669",	KEY55_2|SIR|SERx4,	0x03, 0x02 },
	{ "37C669",	KEY55_2|SIR|SERx4,	0x04, 0x02 }, /* ID? */
	{ "37C78",	KEY55_2|NoIRDA,		0x78, 0x00 },
	{ "37N769",	KEY55_1|FIR|SERx4,	0x28, 0x00 },
	{ "37N869",	KEY55_1|FIR|SERx4,	0x29, 0x00 },
	{ NULL }
};

300
static struct smsc_chip __initdata fdc_chips_paged[] =
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
{
	/* Base address 0x3f0 or 0x370 */
	{ "37B72X",	KEY55_1|SIR|SERx4,	0x4c, 0x00 },
	{ "37B77X",	KEY55_1|SIR|SERx4,	0x43, 0x00 },
	{ "37B78X",	KEY55_1|SIR|SERx4,	0x44, 0x00 },
	{ "37B80X",	KEY55_1|SIR|SERx4,	0x42, 0x00 },
	{ "37C67X",	KEY55_1|FIR|SERx4,	0x40, 0x00 },
	{ "37C93X",	KEY55_2|SIR|SERx4,	0x02, 0x01 },
	{ "37C93XAPM",	KEY55_1|SIR|SERx4,	0x30, 0x01 },
	{ "37C93XFR",	KEY55_2|FIR|SERx4,	0x03, 0x01 },
	{ "37M707",	KEY55_1|SIR|SERx4,	0x42, 0x00 },
	{ "37M81X",	KEY55_1|SIR|SERx4,	0x4d, 0x00 },
	{ "37N958FR",	KEY55_1|FIR|SERx4,	0x09, 0x04 },
	{ "37N971",	KEY55_1|FIR|SERx4,	0x0a, 0x00 },
	{ "37N972",	KEY55_1|FIR|SERx4,	0x0b, 0x00 },
	{ NULL }
};

319
static struct smsc_chip __initdata lpc_chips_flat[] =
L
Linus Torvalds 已提交
320 321 322
{
	/* Base address 0x2E or 0x4E */
	{ "47N227",	KEY55_1|FIR|SERx4,	0x5a, 0x00 },
323
	{ "47N227",	KEY55_1|FIR|SERx4,	0x7a, 0x00 },
L
Linus Torvalds 已提交
324 325 326 327
	{ "47N267",	KEY55_1|FIR|SERx4,	0x5e, 0x00 },
	{ NULL }
};

328
static struct smsc_chip __initdata lpc_chips_paged[] =
L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
{
	/* Base address 0x2E or 0x4E */
	{ "47B27X",	KEY55_1|SIR|SERx4,	0x51, 0x00 },
	{ "47B37X",	KEY55_1|SIR|SERx4,	0x52, 0x00 },
	{ "47M10X",	KEY55_1|SIR|SERx4,	0x59, 0x00 },
	{ "47M120",	KEY55_1|NoIRDA|SERx4,	0x5c, 0x00 },
	{ "47M13X",	KEY55_1|SIR|SERx4,	0x59, 0x00 },
	{ "47M14X",	KEY55_1|SIR|SERx4,	0x5f, 0x00 },
	{ "47N252",	KEY55_1|FIR|SERx4,	0x0e, 0x00 },
	{ "47S42X",	KEY55_1|SIR|SERx4,	0x57, 0x00 },
	{ NULL }
};

#define SMSCSIO_TYPE_FDC	1
#define SMSCSIO_TYPE_LPC	2
#define SMSCSIO_TYPE_FLAT	4
#define SMSCSIO_TYPE_PAGED	8

347
static struct smsc_chip_address __initdata possible_addresses[] =
L
Linus Torvalds 已提交
348
{
349 350 351 352 353 354
	{ 0x3f0, SMSCSIO_TYPE_FDC|SMSCSIO_TYPE_FLAT|SMSCSIO_TYPE_PAGED },
	{ 0x370, SMSCSIO_TYPE_FDC|SMSCSIO_TYPE_FLAT|SMSCSIO_TYPE_PAGED },
	{ 0xe0,  SMSCSIO_TYPE_FDC|SMSCSIO_TYPE_FLAT|SMSCSIO_TYPE_PAGED },
	{ 0x2e,  SMSCSIO_TYPE_LPC|SMSCSIO_TYPE_FLAT|SMSCSIO_TYPE_PAGED },
	{ 0x4e,  SMSCSIO_TYPE_LPC|SMSCSIO_TYPE_FLAT|SMSCSIO_TYPE_PAGED },
	{ 0, 0 }
L
Linus Torvalds 已提交
355 356 357 358
};

/* Globals */

359 360
static struct smsc_ircc_cb *dev_self[] = { NULL, NULL };
static unsigned short dev_count;
L
Linus Torvalds 已提交
361 362 363

static inline void register_bank(int iobase, int bank)
{
364 365
        outb(((inb(iobase + IRCC_MASTER) & 0xf0) | (bank & 0x07)),
               iobase + IRCC_MASTER);
L
Linus Torvalds 已提交
366 367
}

368 369 370 371 372 373 374
/* PNP hotplug support */
static const struct pnp_device_id smsc_ircc_pnp_table[] = {
	{ .id = "SMCf010", .driver_data = 0 },
	/* and presumably others */
	{ }
};
MODULE_DEVICE_TABLE(pnp, smsc_ircc_pnp_table);
B
Bjorn Helgaas 已提交
375 376 377

static int pnp_driver_registered;

378
#ifdef CONFIG_PNP
B
Bjorn Helgaas 已提交
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
static int __init smsc_ircc_pnp_probe(struct pnp_dev *dev,
				      const struct pnp_device_id *dev_id)
{
	unsigned int firbase, sirbase;
	u8 dma, irq;

	if (!(pnp_port_valid(dev, 0) && pnp_port_valid(dev, 1) &&
	      pnp_dma_valid(dev, 0) && pnp_irq_valid(dev, 0)))
		return -EINVAL;

	sirbase = pnp_port_start(dev, 0);
	firbase = pnp_port_start(dev, 1);
	dma = pnp_dma(dev, 0);
	irq = pnp_irq(dev, 0);

	if (smsc_ircc_open(firbase, sirbase, dma, irq))
		return -ENODEV;

	return 0;
}

static struct pnp_driver smsc_ircc_pnp_driver = {
	.name		= "smsc-ircc2",
	.id_table	= smsc_ircc_pnp_table,
	.probe		= smsc_ircc_pnp_probe,
};
405 406 407
#else /* CONFIG_PNP */
static struct pnp_driver smsc_ircc_pnp_driver;
#endif
L
Linus Torvalds 已提交
408 409 410 411 412 413 414 415 416

/*******************************************************************************
 *
 *
 * SMSC-ircc stuff
 *
 *
 *******************************************************************************/

B
Bjorn Helgaas 已提交
417 418 419 420
static int __init smsc_ircc_legacy_probe(void)
{
	int ret = 0;

421 422 423 424 425 426 427
#ifdef CONFIG_PCI
	if (smsc_ircc_preconfigure_subsystems(ircc_cfg, ircc_fir, ircc_sir, ircc_dma, ircc_irq) < 0) {
		/* Ignore errors from preconfiguration */
		IRDA_ERROR("%s, Preconfiguration failed !\n", driver_name);
	}
#endif

B
Bjorn Helgaas 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	if (ircc_fir > 0 && ircc_sir > 0) {
		IRDA_MESSAGE(" Overriding FIR address 0x%04x\n", ircc_fir);
		IRDA_MESSAGE(" Overriding SIR address 0x%04x\n", ircc_sir);

		if (smsc_ircc_open(ircc_fir, ircc_sir, ircc_dma, ircc_irq))
			ret = -ENODEV;
	} else {
		ret = -ENODEV;

		/* try user provided configuration register base address */
		if (ircc_cfg > 0) {
			IRDA_MESSAGE(" Overriding configuration address "
				     "0x%04x\n", ircc_cfg);
			if (!smsc_superio_fdc(ircc_cfg))
				ret = 0;
			if (!smsc_superio_lpc(ircc_cfg))
				ret = 0;
		}

		if (smsc_ircc_look_for_chips() > 0)
			ret = 0;
	}
	return ret;
}

L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460
/*
 * Function smsc_ircc_init ()
 *
 *    Initialize chip. Just try to find out how many chips we are dealing with
 *    and where they are
 */
static int __init smsc_ircc_init(void)
{
461
	int ret;
L
Linus Torvalds 已提交
462

463
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
464

465
	ret = platform_driver_register(&smsc_ircc_driver);
466 467 468 469 470
	if (ret) {
		IRDA_ERROR("%s, Can't register driver!\n", driver_name);
		return ret;
	}

471
	dev_count = 0;
472

B
Bjorn Helgaas 已提交
473 474 475 476
	if (smsc_nopnp || !pnp_platform_devices ||
	    ircc_cfg || ircc_fir || ircc_sir ||
	    ircc_dma != DMA_INVAL || ircc_irq != IRQ_INVAL) {
		ret = smsc_ircc_legacy_probe();
477
	} else {
B
Bjorn Helgaas 已提交
478 479
		if (pnp_register_driver(&smsc_ircc_pnp_driver) == 0)
			pnp_driver_registered = 1;
L
Linus Torvalds 已提交
480
	}
481

B
Bjorn Helgaas 已提交
482 483 484
	if (ret) {
		if (pnp_driver_registered)
			pnp_unregister_driver(&smsc_ircc_pnp_driver);
485
		platform_driver_unregister(&smsc_ircc_driver);
B
Bjorn Helgaas 已提交
486
	}
487

L
Linus Torvalds 已提交
488 489 490
	return ret;
}

S
Stephen Hemminger 已提交
491 492
static netdev_tx_t smsc_ircc_net_xmit(struct sk_buff *skb,
					    struct net_device *dev)
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
{
	struct smsc_ircc_cb *self = netdev_priv(dev);

	if (self->io.speed > 115200)
		return 	smsc_ircc_hard_xmit_fir(skb, dev);
	else
		return 	smsc_ircc_hard_xmit_sir(skb, dev);
}

static const struct net_device_ops smsc_ircc_netdev_ops = {
	.ndo_open       = smsc_ircc_net_open,
	.ndo_stop       = smsc_ircc_net_close,
	.ndo_do_ioctl   = smsc_ircc_net_ioctl,
	.ndo_start_xmit = smsc_ircc_net_xmit,
#if SMSC_IRCC2_C_NET_TIMEOUT
	.ndo_tx_timeout	= smsc_ircc_timeout,
#endif
};

L
Linus Torvalds 已提交
512 513 514 515 516 517 518 519 520 521 522
/*
 * Function smsc_ircc_open (firbase, sirbase, dma, irq)
 *
 *    Try to open driver instance
 *
 */
static int __init smsc_ircc_open(unsigned int fir_base, unsigned int sir_base, u8 dma, u8 irq)
{
	struct smsc_ircc_cb *self;
	struct net_device *dev;
	int err;
523

524
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
525 526

	err = smsc_ircc_present(fir_base, sir_base);
527
	if (err)
L
Linus Torvalds 已提交
528
		goto err_out;
529

L
Linus Torvalds 已提交
530
	err = -ENOMEM;
531
	if (dev_count >= ARRAY_SIZE(dev_self)) {
532
	        IRDA_WARNING("%s(), too many devices!\n", __func__);
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540
		goto err_out1;
	}

	/*
	 *  Allocate new instance of the driver
	 */
	dev = alloc_irdadev(sizeof(struct smsc_ircc_cb));
	if (!dev) {
541
		IRDA_WARNING("%s() can't allocate net device\n", __func__);
L
Linus Torvalds 已提交
542 543 544 545
		goto err_out1;
	}

#if SMSC_IRCC2_C_NET_TIMEOUT
546
	dev->watchdog_timeo  = HZ * 2;  /* Allow enough time for speed change */
L
Linus Torvalds 已提交
547
#endif
548
	dev->netdev_ops = &smsc_ircc_netdev_ops;
549

550
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
551 552 553 554 555 556 557
	self->netdev = dev;

	/* Make ifconfig display some details */
	dev->base_addr = self->io.fir_base = fir_base;
	dev->irq = self->io.irq = irq;

	/* Need to store self somewhere */
558
	dev_self[dev_count] = self;
L
Linus Torvalds 已提交
559 560
	spin_lock_init(&self->lock);

561
	self->rx_buff.truesize = SMSC_IRCC2_RX_BUFF_TRUESIZE;
L
Linus Torvalds 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
	self->tx_buff.truesize = SMSC_IRCC2_TX_BUFF_TRUESIZE;

	self->rx_buff.head =
		dma_alloc_coherent(NULL, self->rx_buff.truesize,
				   &self->rx_buff_dma, GFP_KERNEL);
	if (self->rx_buff.head == NULL) {
		IRDA_ERROR("%s, Can't allocate memory for receive buffer!\n",
			   driver_name);
		goto err_out2;
	}

	self->tx_buff.head =
		dma_alloc_coherent(NULL, self->tx_buff.truesize,
				   &self->tx_buff_dma, GFP_KERNEL);
	if (self->tx_buff.head == NULL) {
		IRDA_ERROR("%s, Can't allocate memory for transmit buffer!\n",
			   driver_name);
		goto err_out3;
	}

	memset(self->rx_buff.head, 0, self->rx_buff.truesize);
	memset(self->tx_buff.head, 0, self->tx_buff.truesize);

	self->rx_buff.in_frame = FALSE;
	self->rx_buff.state = OUTSIDE_FRAME;
	self->tx_buff.data = self->tx_buff.head;
	self->rx_buff.data = self->rx_buff.head;
589

L
Linus Torvalds 已提交
590 591 592
	smsc_ircc_setup_io(self, fir_base, sir_base, dma, irq);
	smsc_ircc_setup_qos(self);
	smsc_ircc_init_chip(self);
593

594 595
	if (ircc_transceiver > 0  &&
	    ircc_transceiver < SMSC_IRCC2_C_NUMBER_OF_TRANSCEIVERS)
L
Linus Torvalds 已提交
596 597 598 599 600
		self->transceiver = ircc_transceiver;
	else
		smsc_ircc_probe_transceiver(self);

	err = register_netdev(self->netdev);
601
	if (err) {
L
Linus Torvalds 已提交
602 603 604 605 606
		IRDA_ERROR("%s, Network device registration failed!\n",
			   driver_name);
		goto err_out4;
	}

607 608 609 610 611 612
	self->pldev = platform_device_register_simple(SMSC_IRCC2_DRIVER_NAME,
						      dev_count, NULL, 0);
	if (IS_ERR(self->pldev)) {
		err = PTR_ERR(self->pldev);
		goto err_out5;
	}
613
	platform_set_drvdata(self->pldev, self);
L
Linus Torvalds 已提交
614 615

	IRDA_MESSAGE("IrDA: Registered device %s\n", dev->name);
616
	dev_count++;
L
Linus Torvalds 已提交
617 618

	return 0;
619

620 621 622
 err_out5:
	unregister_netdev(self->netdev);

L
Linus Torvalds 已提交
623 624 625 626 627 628 629 630
 err_out4:
	dma_free_coherent(NULL, self->tx_buff.truesize,
			  self->tx_buff.head, self->tx_buff_dma);
 err_out3:
	dma_free_coherent(NULL, self->rx_buff.truesize,
			  self->rx_buff.head, self->rx_buff_dma);
 err_out2:
	free_netdev(self->netdev);
631
	dev_self[dev_count] = NULL;
L
Linus Torvalds 已提交
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
 err_out1:
	release_region(fir_base, SMSC_IRCC2_FIR_CHIP_IO_EXTENT);
	release_region(sir_base, SMSC_IRCC2_SIR_CHIP_IO_EXTENT);
 err_out:
	return err;
}

/*
 * Function smsc_ircc_present(fir_base, sir_base)
 *
 *    Check the smsc-ircc chip presence
 *
 */
static int smsc_ircc_present(unsigned int fir_base, unsigned int sir_base)
{
	unsigned char low, high, chip, config, dma, irq, version;

	if (!request_region(fir_base, SMSC_IRCC2_FIR_CHIP_IO_EXTENT,
			    driver_name)) {
		IRDA_WARNING("%s: can't get fir_base of 0x%03x\n",
652
			     __func__, fir_base);
L
Linus Torvalds 已提交
653 654 655 656 657 658
		goto out1;
	}

	if (!request_region(sir_base, SMSC_IRCC2_SIR_CHIP_IO_EXTENT,
			    driver_name)) {
		IRDA_WARNING("%s: can't get sir_base of 0x%03x\n",
659
			     __func__, sir_base);
L
Linus Torvalds 已提交
660 661 662 663 664
		goto out2;
	}

	register_bank(fir_base, 3);

665 666 667 668 669
	high    = inb(fir_base + IRCC_ID_HIGH);
	low     = inb(fir_base + IRCC_ID_LOW);
	chip    = inb(fir_base + IRCC_CHIP_ID);
	version = inb(fir_base + IRCC_VERSION);
	config  = inb(fir_base + IRCC_INTERFACE);
L
Linus Torvalds 已提交
670 671 672
	dma     = config & IRCC_INTERFACE_DMA_MASK;
	irq     = (config & IRCC_INTERFACE_IRQ_MASK) >> 4;

673
	if (high != 0x10 || low != 0xb8 || (chip != 0xf1 && chip != 0xf2)) {
674
		IRDA_WARNING("%s(), addr 0x%04x - no device found!\n",
675
			     __func__, fir_base);
L
Linus Torvalds 已提交
676 677 678 679 680 681 682
		goto out3;
	}
	IRDA_MESSAGE("SMsC IrDA Controller found\n IrCC version %d.%d, "
		     "firport 0x%03x, sirport 0x%03x dma=%d, irq=%d\n",
		     chip & 0x0f, version, fir_base, sir_base, dma, irq);

	return 0;
683

L
Linus Torvalds 已提交
684 685 686 687 688 689 690 691 692 693 694 695 696 697
 out3:
	release_region(sir_base, SMSC_IRCC2_SIR_CHIP_IO_EXTENT);
 out2:
	release_region(fir_base, SMSC_IRCC2_FIR_CHIP_IO_EXTENT);
 out1:
	return -ENODEV;
}

/*
 * Function smsc_ircc_setup_io(self, fir_base, sir_base, dma, irq)
 *
 *    Setup I/O
 *
 */
698 699
static void smsc_ircc_setup_io(struct smsc_ircc_cb *self,
			       unsigned int fir_base, unsigned int sir_base,
L
Linus Torvalds 已提交
700 701 702 703 704
			       u8 dma, u8 irq)
{
	unsigned char config, chip_dma, chip_irq;

	register_bank(fir_base, 3);
705 706 707
	config = inb(fir_base + IRCC_INTERFACE);
	chip_dma = config & IRCC_INTERFACE_DMA_MASK;
	chip_irq = (config & IRCC_INTERFACE_IRQ_MASK) >> 4;
L
Linus Torvalds 已提交
708 709 710 711 712 713 714 715

	self->io.fir_base  = fir_base;
	self->io.sir_base  = sir_base;
	self->io.fir_ext   = SMSC_IRCC2_FIR_CHIP_IO_EXTENT;
	self->io.sir_ext   = SMSC_IRCC2_SIR_CHIP_IO_EXTENT;
	self->io.fifo_size = SMSC_IRCC2_FIFO_SIZE;
	self->io.speed = SMSC_IRCC2_C_IRDA_FALLBACK_SPEED;

716
	if (irq != IRQ_INVAL) {
L
Linus Torvalds 已提交
717 718 719 720
		if (irq != chip_irq)
			IRDA_MESSAGE("%s, Overriding IRQ - chip says %d, using %d\n",
				     driver_name, chip_irq, irq);
		self->io.irq = irq;
721
	} else
L
Linus Torvalds 已提交
722
		self->io.irq = chip_irq;
723

724
	if (dma != DMA_INVAL) {
L
Linus Torvalds 已提交
725 726 727 728
		if (dma != chip_dma)
			IRDA_MESSAGE("%s, Overriding DMA - chip says %d, using %d\n",
				     driver_name, chip_dma, dma);
		self->io.dma = dma;
729
	} else
L
Linus Torvalds 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743
		self->io.dma = chip_dma;

}

/*
 * Function smsc_ircc_setup_qos(self)
 *
 *    Setup qos
 *
 */
static void smsc_ircc_setup_qos(struct smsc_ircc_cb *self)
{
	/* Initialize QoS for this device */
	irda_init_max_qos_capabilies(&self->qos);
744

L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
	self->qos.baud_rate.bits = IR_9600|IR_19200|IR_38400|IR_57600|
		IR_115200|IR_576000|IR_1152000|(IR_4000000 << 8);

	self->qos.min_turn_time.bits = SMSC_IRCC2_MIN_TURN_TIME;
	self->qos.window_size.bits = SMSC_IRCC2_WINDOW_SIZE;
	irda_qos_bits_to_value(&self->qos);
}

/*
 * Function smsc_ircc_init_chip(self)
 *
 *    Init chip
 *
 */
static void smsc_ircc_init_chip(struct smsc_ircc_cb *self)
{
761
	int iobase = self->io.fir_base;
L
Linus Torvalds 已提交
762 763

	register_bank(iobase, 0);
764 765
	outb(IRCC_MASTER_RESET, iobase + IRCC_MASTER);
	outb(0x00, iobase + IRCC_MASTER);
L
Linus Torvalds 已提交
766 767

	register_bank(iobase, 1);
768
	outb(((inb(iobase + IRCC_SCE_CFGA) & 0x87) | IRCC_CFGA_IRDA_SIR_A),
769
	     iobase + IRCC_SCE_CFGA);
L
Linus Torvalds 已提交
770 771

#ifdef smsc_669 /* Uses pin 88/89 for Rx/Tx */
772 773
	outb(((inb(iobase + IRCC_SCE_CFGB) & 0x3f) | IRCC_CFGB_MUX_COM),
	     iobase + IRCC_SCE_CFGB);
774
#else
775 776
	outb(((inb(iobase + IRCC_SCE_CFGB) & 0x3f) | IRCC_CFGB_MUX_IR),
	     iobase + IRCC_SCE_CFGB);
777
#endif
778 779
	(void) inb(iobase + IRCC_FIFO_THRESHOLD);
	outb(SMSC_IRCC2_FIFO_THRESHOLD, iobase + IRCC_FIFO_THRESHOLD);
780

L
Linus Torvalds 已提交
781
	register_bank(iobase, 4);
782
	outb((inb(iobase + IRCC_CONTROL) & 0x30), iobase + IRCC_CONTROL);
783

L
Linus Torvalds 已提交
784
	register_bank(iobase, 0);
785
	outb(0, iobase + IRCC_LCR_A);
L
Linus Torvalds 已提交
786 787

	smsc_ircc_set_sir_speed(self, SMSC_IRCC2_C_IRDA_FALLBACK_SPEED);
788

L
Linus Torvalds 已提交
789
	/* Power on device */
790
	outb(0x00, iobase + IRCC_MASTER);
L
Linus Torvalds 已提交
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
}

/*
 * Function smsc_ircc_net_ioctl (dev, rq, cmd)
 *
 *    Process IOCTL commands for this device
 *
 */
static int smsc_ircc_net_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct if_irda_req *irq = (struct if_irda_req *) rq;
	struct smsc_ircc_cb *self;
	unsigned long flags;
	int ret = 0;

	IRDA_ASSERT(dev != NULL, return -1;);

808
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
809 810 811

	IRDA_ASSERT(self != NULL, return -1;);

812
	IRDA_DEBUG(2, "%s(), %s, (cmd=0x%X)\n", __func__, dev->name, cmd);
813

L
Linus Torvalds 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
	switch (cmd) {
	case SIOCSBANDWIDTH: /* Set bandwidth */
		if (!capable(CAP_NET_ADMIN))
			ret = -EPERM;
                else {
			/* Make sure we are the only one touching
			 * self->io.speed and the hardware - Jean II */
			spin_lock_irqsave(&self->lock, flags);
			smsc_ircc_change_speed(self, irq->ifr_baudrate);
			spin_unlock_irqrestore(&self->lock, flags);
		}
		break;
	case SIOCSMEDIABUSY: /* Set media busy */
		if (!capable(CAP_NET_ADMIN)) {
			ret = -EPERM;
			break;
		}

		irda_device_set_media_busy(self->netdev, TRUE);
		break;
	case SIOCGRECEIVING: /* Check if we are receiving right now */
		irq->ifr_receiving = smsc_ircc_is_receiving(self);
		break;
	#if 0
	case SIOCSDTRRTS:
		if (!capable(CAP_NET_ADMIN)) {
			ret = -EPERM;
			break;
		}
		smsc_ircc_sir_set_dtr_rts(dev, irq->ifr_dtr, irq->ifr_rts);
		break;
	#endif
	default:
		ret = -EOPNOTSUPP;
	}
849

L
Linus Torvalds 已提交
850 851 852 853 854 855 856 857 858 859 860 861 862
	return ret;
}

#if SMSC_IRCC2_C_NET_TIMEOUT
/*
 * Function smsc_ircc_timeout (struct net_device *dev)
 *
 *    The networking timeout management.
 *
 */

static void smsc_ircc_timeout(struct net_device *dev)
{
863
	struct smsc_ircc_cb *self = netdev_priv(dev);
L
Linus Torvalds 已提交
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
	unsigned long flags;

	IRDA_WARNING("%s: transmit timed out, changing speed to: %d\n",
		     dev->name, self->io.speed);
	spin_lock_irqsave(&self->lock, flags);
	smsc_ircc_sir_start(self);
	smsc_ircc_change_speed(self, self->io.speed);
	dev->trans_start = jiffies;
	netif_wake_queue(dev);
	spin_unlock_irqrestore(&self->lock, flags);
}
#endif

/*
 * Function smsc_ircc_hard_xmit_sir (struct sk_buff *skb, struct net_device *dev)
 *
 *    Transmits the current frame until FIFO is full, then
 *    waits until the next transmit interrupt, and continues until the
 *    frame is transmitted.
 */
S
Stephen Hemminger 已提交
884 885
static netdev_tx_t smsc_ircc_hard_xmit_sir(struct sk_buff *skb,
						 struct net_device *dev)
L
Linus Torvalds 已提交
886 887 888 889 890
{
	struct smsc_ircc_cb *self;
	unsigned long flags;
	s32 speed;

891
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
892

893
	IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
894

895
	self = netdev_priv(dev);
896
	IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
L
Linus Torvalds 已提交
897 898

	netif_stop_queue(dev);
899

L
Linus Torvalds 已提交
900 901 902 903 904
	/* Make sure test of self->io.speed & speed change are atomic */
	spin_lock_irqsave(&self->lock, flags);

	/* Check if we need to change the speed */
	speed = irda_get_next_speed(skb);
905
	if (speed != self->io.speed && speed != -1) {
L
Linus Torvalds 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
		/* Check for empty frame */
		if (!skb->len) {
			/*
			 * We send frames one by one in SIR mode (no
			 * pipelining), so at this point, if we were sending
			 * a previous frame, we just received the interrupt
			 * telling us it is finished (UART_IIR_THRI).
			 * Therefore, waiting for the transmitter to really
			 * finish draining the fifo won't take too long.
			 * And the interrupt handler is not expected to run.
			 * - Jean II */
			smsc_ircc_sir_wait_hw_transmitter_finish(self);
			smsc_ircc_change_speed(self, speed);
			spin_unlock_irqrestore(&self->lock, flags);
			dev_kfree_skb(skb);
921
			return NETDEV_TX_OK;
L
Linus Torvalds 已提交
922
		}
923
		self->new_speed = speed;
L
Linus Torvalds 已提交
924 925 926 927 928 929
	}

	/* Init tx buffer */
	self->tx_buff.data = self->tx_buff.head;

	/* Copy skb to tx_buff while wrapping, stuffing and making CRC */
930
	self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data,
L
Linus Torvalds 已提交
931
					   self->tx_buff.truesize);
932

933
	dev->stats.tx_bytes += self->tx_buff.len;
L
Linus Torvalds 已提交
934 935

	/* Turn on transmit finished interrupt. Will fire immediately!  */
936
	outb(UART_IER_THRI, self->io.sir_base + UART_IER);
L
Linus Torvalds 已提交
937 938 939 940

	spin_unlock_irqrestore(&self->lock, flags);

	dev_kfree_skb(skb);
941

942
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
}

/*
 * Function smsc_ircc_set_fir_speed (self, baud)
 *
 *    Change the speed of the device
 *
 */
static void smsc_ircc_set_fir_speed(struct smsc_ircc_cb *self, u32 speed)
{
	int fir_base, ir_mode, ctrl, fast;

	IRDA_ASSERT(self != NULL, return;);
	fir_base = self->io.fir_base;

	self->io.speed = speed;

960
	switch (speed) {
L
Linus Torvalds 已提交
961
	default:
962
	case 576000:
L
Linus Torvalds 已提交
963 964 965
		ir_mode = IRCC_CFGA_IRDA_HDLC;
		ctrl = IRCC_CRC;
		fast = 0;
966
		IRDA_DEBUG(0, "%s(), handling baud of 576000\n", __func__);
L
Linus Torvalds 已提交
967 968 969 970 971 972
		break;
	case 1152000:
		ir_mode = IRCC_CFGA_IRDA_HDLC;
		ctrl = IRCC_1152 | IRCC_CRC;
		fast = IRCC_LCR_A_FAST | IRCC_LCR_A_GP_DATA;
		IRDA_DEBUG(0, "%s(), handling baud of 1152000\n",
973
			   __func__);
L
Linus Torvalds 已提交
974 975 976 977 978 979
		break;
	case 4000000:
		ir_mode = IRCC_CFGA_IRDA_4PPM;
		ctrl = IRCC_CRC;
		fast = IRCC_LCR_A_FAST;
		IRDA_DEBUG(0, "%s(), handling baud of 4000000\n",
980
			   __func__);
L
Linus Torvalds 已提交
981 982 983 984 985 986
		break;
	}
	#if 0
	Now in tranceiver!
	/* This causes an interrupt */
	register_bank(fir_base, 0);
987
	outb((inb(fir_base + IRCC_LCR_A) &  0xbf) | fast, fir_base + IRCC_LCR_A);
L
Linus Torvalds 已提交
988
	#endif
989

L
Linus Torvalds 已提交
990
	register_bank(fir_base, 1);
991
	outb(((inb(fir_base + IRCC_SCE_CFGA) & IRCC_SCE_CFGA_BLOCK_CTRL_BITS_MASK) | ir_mode), fir_base + IRCC_SCE_CFGA);
992

L
Linus Torvalds 已提交
993
	register_bank(fir_base, 4);
994
	outb((inb(fir_base + IRCC_CONTROL) & 0x30) | ctrl, fir_base + IRCC_CONTROL);
L
Linus Torvalds 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
}

/*
 * Function smsc_ircc_fir_start(self)
 *
 *    Change the speed of the device
 *
 */
static void smsc_ircc_fir_start(struct smsc_ircc_cb *self)
{
	struct net_device *dev;
	int fir_base;

1008
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

	IRDA_ASSERT(self != NULL, return;);
	dev = self->netdev;
	IRDA_ASSERT(dev != NULL, return;);

	fir_base = self->io.fir_base;

	/* Reset everything */

	/* Clear FIFO */
1019
	outb(inb(fir_base + IRCC_LCR_A) | IRCC_LCR_A_FIFO_RESET, fir_base + IRCC_LCR_A);
L
Linus Torvalds 已提交
1020 1021

	/* Enable interrupt */
1022
	/*outb(IRCC_IER_ACTIVE_FRAME|IRCC_IER_EOM, fir_base + IRCC_IER);*/
L
Linus Torvalds 已提交
1023 1024 1025

	register_bank(fir_base, 1);

1026
	/* Select the TX/RX interface */
L
Linus Torvalds 已提交
1027
#ifdef SMSC_669 /* Uses pin 88/89 for Rx/Tx */
1028 1029
	outb(((inb(fir_base + IRCC_SCE_CFGB) & 0x3f) | IRCC_CFGB_MUX_COM),
	     fir_base + IRCC_SCE_CFGB);
1030
#else
1031 1032
	outb(((inb(fir_base + IRCC_SCE_CFGB) & 0x3f) | IRCC_CFGB_MUX_IR),
	     fir_base + IRCC_SCE_CFGB);
1033
#endif
1034
	(void) inb(fir_base + IRCC_FIFO_THRESHOLD);
L
Linus Torvalds 已提交
1035 1036

	/* Enable SCE interrupts */
1037
	outb(0, fir_base + IRCC_MASTER);
L
Linus Torvalds 已提交
1038
	register_bank(fir_base, 0);
1039 1040
	outb(IRCC_IER_ACTIVE_FRAME | IRCC_IER_EOM, fir_base + IRCC_IER);
	outb(IRCC_MASTER_INT_EN, fir_base + IRCC_MASTER);
L
Linus Torvalds 已提交
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
}

/*
 * Function smsc_ircc_fir_stop(self, baud)
 *
 *    Change the speed of the device
 *
 */
static void smsc_ircc_fir_stop(struct smsc_ircc_cb *self)
{
	int fir_base;

1053
	IRDA_DEBUG(1, "%s\n", __func__);
1054

L
Linus Torvalds 已提交
1055 1056 1057 1058
	IRDA_ASSERT(self != NULL, return;);

	fir_base = self->io.fir_base;
	register_bank(fir_base, 0);
1059 1060
	/*outb(IRCC_MASTER_RESET, fir_base + IRCC_MASTER);*/
	outb(inb(fir_base + IRCC_LCR_B) & IRCC_LCR_B_SIP_ENABLE, fir_base + IRCC_LCR_B);
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
}


/*
 * Function smsc_ircc_change_speed(self, baud)
 *
 *    Change the speed of the device
 *
 * This function *must* be called with spinlock held, because it may
 * be called from the irq handler. - Jean II
 */
1072
static void smsc_ircc_change_speed(struct smsc_ircc_cb *self, u32 speed)
L
Linus Torvalds 已提交
1073 1074 1075
{
	struct net_device *dev;
	int last_speed_was_sir;
1076

1077
	IRDA_DEBUG(0, "%s() changing speed to: %d\n", __func__, speed);
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

	IRDA_ASSERT(self != NULL, return;);
	dev = self->netdev;

	last_speed_was_sir = self->io.speed <= SMSC_IRCC2_MAX_SIR_SPEED;

	#if 0
	/* Temp Hack */
	speed= 1152000;
	self->io.speed = speed;
	last_speed_was_sir = 0;
1089
	smsc_ircc_fir_start(self);
L
Linus Torvalds 已提交
1090
	#endif
1091

1092
	if (self->io.speed == 0)
L
Linus Torvalds 已提交
1093 1094 1095
		smsc_ircc_sir_start(self);

	#if 0
1096
	if (!last_speed_was_sir) speed = self->io.speed;
L
Linus Torvalds 已提交
1097 1098
	#endif

1099 1100
	if (self->io.speed != speed)
		smsc_ircc_set_transceiver_for_speed(self, speed);
L
Linus Torvalds 已提交
1101 1102

	self->io.speed = speed;
1103

1104 1105
	if (speed <= SMSC_IRCC2_MAX_SIR_SPEED) {
		if (!last_speed_was_sir) {
L
Linus Torvalds 已提交
1106 1107 1108
			smsc_ircc_fir_stop(self);
			smsc_ircc_sir_start(self);
		}
1109
		smsc_ircc_set_sir_speed(self, speed);
1110 1111
	} else {
		if (last_speed_was_sir) {
1112
			#if SMSC_IRCC2_C_SIR_STOP
L
Linus Torvalds 已提交
1113 1114 1115 1116 1117 1118 1119 1120 1121
			smsc_ircc_sir_stop(self);
			#endif
			smsc_ircc_fir_start(self);
		}
		smsc_ircc_set_fir_speed(self, speed);

		#if 0
		self->tx_buff.len = 10;
		self->tx_buff.data = self->tx_buff.head;
1122

1123
		smsc_ircc_dma_xmit(self, 4000);
L
Linus Torvalds 已提交
1124 1125
		#endif
		/* Be ready for incoming frames */
1126
		smsc_ircc_dma_receive(self);
L
Linus Torvalds 已提交
1127
	}
1128

L
Linus Torvalds 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137
	netif_wake_queue(dev);
}

/*
 * Function smsc_ircc_set_sir_speed (self, speed)
 *
 *    Set speed of IrDA port to specified baudrate
 *
 */
1138
static void smsc_ircc_set_sir_speed(struct smsc_ircc_cb *self, __u32 speed)
L
Linus Torvalds 已提交
1139
{
1140
	int iobase;
L
Linus Torvalds 已提交
1141 1142 1143 1144
	int fcr;    /* FIFO control reg */
	int lcr;    /* Line control reg */
	int divisor;

1145
	IRDA_DEBUG(0, "%s(), Setting speed to: %d\n", __func__, speed);
L
Linus Torvalds 已提交
1146 1147 1148

	IRDA_ASSERT(self != NULL, return;);
	iobase = self->io.sir_base;
1149

L
Linus Torvalds 已提交
1150 1151 1152 1153
	/* Update accounting for new speed */
	self->io.speed = speed;

	/* Turn off interrupts */
1154
	outb(0, iobase + UART_IER);
L
Linus Torvalds 已提交
1155

1156
	divisor = SMSC_IRCC2_MAX_SIR_SPEED / speed;
1157

L
Linus Torvalds 已提交
1158 1159
	fcr = UART_FCR_ENABLE_FIFO;

1160
	/*
L
Linus Torvalds 已提交
1161 1162
	 * Use trigger level 1 to avoid 3 ms. timeout delay at 9600 bps, and
	 * almost 1,7 ms at 19200 bps. At speeds above that we can just forget
1163
	 * about this timeout since it will always be fast enough.
L
Linus Torvalds 已提交
1164
	 */
1165 1166
	fcr |= self->io.speed < 38400 ?
		UART_FCR_TRIGGER_1 : UART_FCR_TRIGGER_14;
1167

L
Linus Torvalds 已提交
1168 1169
	/* IrDA ports use 8N1 */
	lcr = UART_LCR_WLEN8;
1170

1171 1172 1173 1174 1175
	outb(UART_LCR_DLAB | lcr, iobase + UART_LCR); /* Set DLAB */
	outb(divisor & 0xff,      iobase + UART_DLL); /* Set speed */
	outb(divisor >> 8,	  iobase + UART_DLM);
	outb(lcr,		  iobase + UART_LCR); /* Set 8N1 */
	outb(fcr,		  iobase + UART_FCR); /* Enable FIFO's */
L
Linus Torvalds 已提交
1176 1177

	/* Turn on interrups */
1178
	outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
L
Linus Torvalds 已提交
1179

1180
	IRDA_DEBUG(2, "%s() speed changed to: %d\n", __func__, speed);
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189
}


/*
 * Function smsc_ircc_hard_xmit_fir (skb, dev)
 *
 *    Transmit the frame!
 *
 */
S
Stephen Hemminger 已提交
1190 1191
static netdev_tx_t smsc_ircc_hard_xmit_fir(struct sk_buff *skb,
						 struct net_device *dev)
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197
{
	struct smsc_ircc_cb *self;
	unsigned long flags;
	s32 speed;
	int mtt;

1198
	IRDA_ASSERT(dev != NULL, return NETDEV_TX_OK;);
1199
	self = netdev_priv(dev);
1200
	IRDA_ASSERT(self != NULL, return NETDEV_TX_OK;);
L
Linus Torvalds 已提交
1201 1202 1203 1204 1205 1206 1207 1208

	netif_stop_queue(dev);

	/* Make sure test of self->io.speed & speed change are atomic */
	spin_lock_irqsave(&self->lock, flags);

	/* Check if we need to change the speed after this frame */
	speed = irda_get_next_speed(skb);
1209
	if (speed != self->io.speed && speed != -1) {
L
Linus Torvalds 已提交
1210 1211 1212 1213 1214
		/* Check for empty frame */
		if (!skb->len) {
			/* Note : you should make sure that speed changes
			 * are not going to corrupt any outgoing frame.
			 * Look at nsc-ircc for the gory details - Jean II */
1215
			smsc_ircc_change_speed(self, speed);
L
Linus Torvalds 已提交
1216 1217
			spin_unlock_irqrestore(&self->lock, flags);
			dev_kfree_skb(skb);
1218
			return NETDEV_TX_OK;
1219 1220 1221
		}

		self->new_speed = speed;
L
Linus Torvalds 已提交
1222
	}
1223

1224
	skb_copy_from_linear_data(skb, self->tx_buff.head, skb->len);
L
Linus Torvalds 已提交
1225 1226 1227

	self->tx_buff.len = skb->len;
	self->tx_buff.data = self->tx_buff.head;
1228 1229

	mtt = irda_get_mtt(skb);
L
Linus Torvalds 已提交
1230 1231 1232
	if (mtt) {
		int bofs;

1233
		/*
L
Linus Torvalds 已提交
1234 1235 1236 1237 1238 1239 1240
		 * Compute how many BOFs (STA or PA's) we need to waste the
		 * min turn time given the speed of the link.
		 */
		bofs = mtt * (self->io.speed / 1000) / 8000;
		if (bofs > 4095)
			bofs = 4095;

1241
		smsc_ircc_dma_xmit(self, bofs);
L
Linus Torvalds 已提交
1242 1243
	} else {
		/* Transmit frame */
1244
		smsc_ircc_dma_xmit(self, 0);
L
Linus Torvalds 已提交
1245
	}
1246

L
Linus Torvalds 已提交
1247 1248 1249
	spin_unlock_irqrestore(&self->lock, flags);
	dev_kfree_skb(skb);

1250
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1251 1252 1253
}

/*
1254
 * Function smsc_ircc_dma_xmit (self, bofs)
L
Linus Torvalds 已提交
1255 1256 1257 1258
 *
 *    Transmit data using DMA
 *
 */
1259
static void smsc_ircc_dma_xmit(struct smsc_ircc_cb *self, int bofs)
L
Linus Torvalds 已提交
1260
{
1261
	int iobase = self->io.fir_base;
L
Linus Torvalds 已提交
1262 1263
	u8 ctrl;

1264
	IRDA_DEBUG(3, "%s\n", __func__);
L
Linus Torvalds 已提交
1265 1266 1267
#if 1
	/* Disable Rx */
	register_bank(iobase, 0);
1268
	outb(0x00, iobase + IRCC_LCR_B);
L
Linus Torvalds 已提交
1269 1270
#endif
	register_bank(iobase, 1);
1271 1272
	outb(inb(iobase + IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE,
	     iobase + IRCC_SCE_CFGB);
L
Linus Torvalds 已提交
1273 1274 1275 1276 1277

	self->io.direction = IO_XMIT;

	/* Set BOF additional count for generating the min turn time */
	register_bank(iobase, 4);
1278 1279 1280
	outb(bofs & 0xff, iobase + IRCC_BOF_COUNT_LO);
	ctrl = inb(iobase + IRCC_CONTROL) & 0xf0;
	outb(ctrl | ((bofs >> 8) & 0x0f), iobase + IRCC_BOF_COUNT_HI);
L
Linus Torvalds 已提交
1281 1282

	/* Set max Tx frame size */
1283 1284
	outb(self->tx_buff.len >> 8, iobase + IRCC_TX_SIZE_HI);
	outb(self->tx_buff.len & 0xff, iobase + IRCC_TX_SIZE_LO);
L
Linus Torvalds 已提交
1285 1286

	/*outb(UART_MCR_OUT2, self->io.sir_base + UART_MCR);*/
1287

L
Linus Torvalds 已提交
1288 1289
	/* Enable burst mode chip Tx DMA */
	register_bank(iobase, 1);
1290 1291
	outb(inb(iobase + IRCC_SCE_CFGB) | IRCC_CFGB_DMA_ENABLE |
	     IRCC_CFGB_DMA_BURST, iobase + IRCC_SCE_CFGB);
L
Linus Torvalds 已提交
1292 1293 1294 1295 1296 1297 1298 1299

	/* Setup DMA controller (must be done after enabling chip DMA) */
	irda_setup_dma(self->io.dma, self->tx_buff_dma, self->tx_buff.len,
		       DMA_TX_MODE);

	/* Enable interrupt */

	register_bank(iobase, 0);
1300 1301
	outb(IRCC_IER_ACTIVE_FRAME | IRCC_IER_EOM, iobase + IRCC_IER);
	outb(IRCC_MASTER_INT_EN, iobase + IRCC_MASTER);
1302

L
Linus Torvalds 已提交
1303
	/* Enable transmit */
1304
	outb(IRCC_LCR_B_SCE_TRANSMIT | IRCC_LCR_B_SIP_ENABLE, iobase + IRCC_LCR_B);
L
Linus Torvalds 已提交
1305 1306 1307 1308 1309
}

/*
 * Function smsc_ircc_dma_xmit_complete (self)
 *
1310
 *    The transfer of a frame in finished. This function will only be called
L
Linus Torvalds 已提交
1311 1312 1313
 *    by the interrupt handler
 *
 */
1314
static void smsc_ircc_dma_xmit_complete(struct smsc_ircc_cb *self)
L
Linus Torvalds 已提交
1315
{
1316 1317
	int iobase = self->io.fir_base;

1318
	IRDA_DEBUG(3, "%s\n", __func__);
L
Linus Torvalds 已提交
1319 1320 1321
#if 0
	/* Disable Tx */
	register_bank(iobase, 0);
1322
	outb(0x00, iobase + IRCC_LCR_B);
L
Linus Torvalds 已提交
1323
#endif
1324 1325 1326
	register_bank(iobase, 1);
	outb(inb(iobase + IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE,
	     iobase + IRCC_SCE_CFGB);
L
Linus Torvalds 已提交
1327 1328 1329

	/* Check for underrun! */
	register_bank(iobase, 0);
1330
	if (inb(iobase + IRCC_LSR) & IRCC_LSR_UNDERRUN) {
1331 1332
		self->netdev->stats.tx_errors++;
		self->netdev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
1333 1334 1335

		/* Reset error condition */
		register_bank(iobase, 0);
1336 1337
		outb(IRCC_MASTER_ERROR_RESET, iobase + IRCC_MASTER);
		outb(0x00, iobase + IRCC_MASTER);
L
Linus Torvalds 已提交
1338
	} else {
1339 1340
		self->netdev->stats.tx_packets++;
		self->netdev->stats.tx_bytes += self->tx_buff.len;
L
Linus Torvalds 已提交
1341 1342 1343 1344
	}

	/* Check if it's time to change the speed */
	if (self->new_speed) {
1345
		smsc_ircc_change_speed(self, self->new_speed);
L
Linus Torvalds 已提交
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
		self->new_speed = 0;
	}

	netif_wake_queue(self->netdev);
}

/*
 * Function smsc_ircc_dma_receive(self)
 *
 *    Get ready for receiving a frame. The device will initiate a DMA
 *    if it starts to receive a frame.
 *
 */
1359
static int smsc_ircc_dma_receive(struct smsc_ircc_cb *self)
L
Linus Torvalds 已提交
1360
{
1361
	int iobase = self->io.fir_base;
L
Linus Torvalds 已提交
1362 1363 1364
#if 0
	/* Turn off chip DMA */
	register_bank(iobase, 1);
1365 1366
	outb(inb(iobase + IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE,
	     iobase + IRCC_SCE_CFGB);
L
Linus Torvalds 已提交
1367
#endif
1368

L
Linus Torvalds 已提交
1369 1370
	/* Disable Tx */
	register_bank(iobase, 0);
1371
	outb(0x00, iobase + IRCC_LCR_B);
L
Linus Torvalds 已提交
1372 1373 1374

	/* Turn off chip DMA */
	register_bank(iobase, 1);
1375 1376
	outb(inb(iobase + IRCC_SCE_CFGB) & ~IRCC_CFGB_DMA_ENABLE,
	     iobase + IRCC_SCE_CFGB);
L
Linus Torvalds 已提交
1377 1378 1379 1380 1381 1382

	self->io.direction = IO_RECV;
	self->rx_buff.data = self->rx_buff.head;

	/* Set max Rx frame size */
	register_bank(iobase, 4);
1383 1384
	outb((2050 >> 8) & 0x0f, iobase + IRCC_RX_SIZE_HI);
	outb(2050 & 0xff, iobase + IRCC_RX_SIZE_LO);
L
Linus Torvalds 已提交
1385 1386 1387 1388 1389 1390 1391

	/* Setup DMA controller */
	irda_setup_dma(self->io.dma, self->rx_buff_dma, self->rx_buff.truesize,
		       DMA_RX_MODE);

	/* Enable burst mode chip Rx DMA */
	register_bank(iobase, 1);
1392 1393
	outb(inb(iobase + IRCC_SCE_CFGB) | IRCC_CFGB_DMA_ENABLE |
	     IRCC_CFGB_DMA_BURST, iobase + IRCC_SCE_CFGB);
L
Linus Torvalds 已提交
1394 1395 1396

	/* Enable interrupt */
	register_bank(iobase, 0);
1397 1398
	outb(IRCC_IER_ACTIVE_FRAME | IRCC_IER_EOM, iobase + IRCC_IER);
	outb(IRCC_MASTER_INT_EN, iobase + IRCC_MASTER);
L
Linus Torvalds 已提交
1399 1400 1401

	/* Enable receiver */
	register_bank(iobase, 0);
1402
	outb(IRCC_LCR_B_SCE_RECEIVE | IRCC_LCR_B_SIP_ENABLE,
1403
	     iobase + IRCC_LCR_B);
1404

L
Linus Torvalds 已提交
1405 1406 1407 1408
	return 0;
}

/*
1409
 * Function smsc_ircc_dma_receive_complete(self)
L
Linus Torvalds 已提交
1410 1411 1412 1413
 *
 *    Finished with receiving frames
 *
 */
1414
static void smsc_ircc_dma_receive_complete(struct smsc_ircc_cb *self)
L
Linus Torvalds 已提交
1415 1416 1417
{
	struct sk_buff *skb;
	int len, msgcnt, lsr;
1418
	int iobase = self->io.fir_base;
1419

L
Linus Torvalds 已提交
1420
	register_bank(iobase, 0);
1421

1422
	IRDA_DEBUG(3, "%s\n", __func__);
L
Linus Torvalds 已提交
1423 1424 1425
#if 0
	/* Disable Rx */
	register_bank(iobase, 0);
1426
	outb(0x00, iobase + IRCC_LCR_B);
L
Linus Torvalds 已提交
1427 1428
#endif
	register_bank(iobase, 0);
1429 1430 1431
	outb(inb(iobase + IRCC_LSAR) & ~IRCC_LSAR_ADDRESS_MASK, iobase + IRCC_LSAR);
	lsr= inb(iobase + IRCC_LSR);
	msgcnt = inb(iobase + IRCC_LCR_B) & 0x08;
L
Linus Torvalds 已提交
1432

1433
	IRDA_DEBUG(2, "%s: dma count = %d\n", __func__,
L
Linus Torvalds 已提交
1434 1435 1436 1437
		   get_dma_residue(self->io.dma));

	len = self->rx_buff.truesize - get_dma_residue(self->io.dma);

1438 1439
	/* Look for errors */
	if (lsr & (IRCC_LSR_FRAME_ERROR | IRCC_LSR_CRC_ERROR | IRCC_LSR_SIZE_ERROR)) {
1440
		self->netdev->stats.rx_errors++;
1441
		if (lsr & IRCC_LSR_FRAME_ERROR)
1442
			self->netdev->stats.rx_frame_errors++;
1443
		if (lsr & IRCC_LSR_CRC_ERROR)
1444
			self->netdev->stats.rx_crc_errors++;
1445
		if (lsr & IRCC_LSR_SIZE_ERROR)
1446
			self->netdev->stats.rx_length_errors++;
1447
		if (lsr & (IRCC_LSR_UNDERRUN | IRCC_LSR_OVERRUN))
1448
			self->netdev->stats.rx_length_errors++;
L
Linus Torvalds 已提交
1449 1450
		return;
	}
1451

L
Linus Torvalds 已提交
1452
	/* Remove CRC */
1453
	len -= self->io.speed < 4000000 ? 2 : 4;
L
Linus Torvalds 已提交
1454

1455
	if (len < 2 || len > 2050) {
1456
		IRDA_WARNING("%s(), bogus len=%d\n", __func__, len);
L
Linus Torvalds 已提交
1457 1458
		return;
	}
1459
	IRDA_DEBUG(2, "%s: msgcnt = %d, len=%d\n", __func__, msgcnt, len);
L
Linus Torvalds 已提交
1460

1461 1462
	skb = dev_alloc_skb(len + 1);
	if (!skb) {
L
Linus Torvalds 已提交
1463
		IRDA_WARNING("%s(), memory squeeze, dropping frame.\n",
1464
			     __func__);
L
Linus Torvalds 已提交
1465
		return;
1466
	}
L
Linus Torvalds 已提交
1467
	/* Make sure IP header gets aligned */
1468
	skb_reserve(skb, 1);
L
Linus Torvalds 已提交
1469 1470

	memcpy(skb_put(skb, len), self->rx_buff.data, len);
1471 1472
	self->netdev->stats.rx_packets++;
	self->netdev->stats.rx_bytes += len;
L
Linus Torvalds 已提交
1473 1474

	skb->dev = self->netdev;
1475
	skb_reset_mac_header(skb);
L
Linus Torvalds 已提交
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
	skb->protocol = htons(ETH_P_IRDA);
	netif_rx(skb);
}

/*
 * Function smsc_ircc_sir_receive (self)
 *
 *    Receive one frame from the infrared port
 *
 */
1486
static void smsc_ircc_sir_receive(struct smsc_ircc_cb *self)
L
Linus Torvalds 已提交
1487 1488 1489 1490 1491 1492 1493 1494
{
	int boguscount = 0;
	int iobase;

	IRDA_ASSERT(self != NULL, return;);

	iobase = self->io.sir_base;

1495 1496 1497
	/*
	 * Receive all characters in Rx FIFO, unwrap and unstuff them.
         * async_unwrap_char will deliver all found frames
L
Linus Torvalds 已提交
1498 1499
	 */
	do {
1500
		async_unwrap_char(self->netdev, &self->netdev->stats, &self->rx_buff,
1501
				  inb(iobase + UART_RX));
L
Linus Torvalds 已提交
1502 1503 1504

		/* Make sure we don't stay here to long */
		if (boguscount++ > 32) {
1505
			IRDA_DEBUG(2, "%s(), breaking!\n", __func__);
L
Linus Torvalds 已提交
1506 1507
			break;
		}
1508
	} while (inb(iobase + UART_LSR) & UART_LSR_DR);
L
Linus Torvalds 已提交
1509 1510 1511 1512 1513 1514 1515 1516 1517
}


/*
 * Function smsc_ircc_interrupt (irq, dev_id, regs)
 *
 *    An interrupt from the chip has arrived. Time to do some work
 *
 */
1518
static irqreturn_t smsc_ircc_interrupt(int dummy, void *dev_id)
L
Linus Torvalds 已提交
1519
{
1520 1521
	struct net_device *dev = dev_id;
	struct smsc_ircc_cb *self = netdev_priv(dev);
L
Linus Torvalds 已提交
1522 1523 1524 1525
	int iobase, iir, lcra, lsr;
	irqreturn_t ret = IRQ_NONE;

	/* Serialise the interrupt handler in various CPUs, stop Tx path */
1526
	spin_lock(&self->lock);
L
Linus Torvalds 已提交
1527 1528

	/* Check if we should use the SIR interrupt handler */
1529
	if (self->io.speed <= SMSC_IRCC2_MAX_SIR_SPEED) {
L
Linus Torvalds 已提交
1530 1531 1532 1533 1534 1535 1536
		ret = smsc_ircc_interrupt_sir(dev);
		goto irq_ret_unlock;
	}

	iobase = self->io.fir_base;

	register_bank(iobase, 0);
1537
	iir = inb(iobase + IRCC_IIR);
1538
	if (iir == 0)
L
Linus Torvalds 已提交
1539 1540 1541 1542
		goto irq_ret_unlock;
	ret = IRQ_HANDLED;

	/* Disable interrupts */
1543 1544 1545
	outb(0, iobase + IRCC_IER);
	lcra = inb(iobase + IRCC_LCR_A);
	lsr = inb(iobase + IRCC_LSR);
1546

1547
	IRDA_DEBUG(2, "%s(), iir = 0x%02x\n", __func__, iir);
L
Linus Torvalds 已提交
1548 1549 1550

	if (iir & IRCC_IIR_EOM) {
		if (self->io.direction == IO_RECV)
1551
			smsc_ircc_dma_receive_complete(self);
L
Linus Torvalds 已提交
1552
		else
1553
			smsc_ircc_dma_xmit_complete(self);
1554

1555
		smsc_ircc_dma_receive(self);
L
Linus Torvalds 已提交
1556 1557 1558
	}

	if (iir & IRCC_IIR_ACTIVE_FRAME) {
1559
		/*printk(KERN_WARNING "%s(): Active Frame\n", __func__);*/
L
Linus Torvalds 已提交
1560 1561 1562 1563 1564
	}

	/* Enable interrupts again */

	register_bank(iobase, 0);
1565
	outb(IRCC_IER_ACTIVE_FRAME | IRCC_IER_EOM, iobase + IRCC_IER);
L
Linus Torvalds 已提交
1566 1567 1568

 irq_ret_unlock:
	spin_unlock(&self->lock);
1569

L
Linus Torvalds 已提交
1570 1571 1572 1573
	return ret;
}

/*
1574
 * Function irport_interrupt_sir (irq, dev_id)
L
Linus Torvalds 已提交
1575 1576 1577 1578 1579
 *
 *    Interrupt handler for SIR modes
 */
static irqreturn_t smsc_ircc_interrupt_sir(struct net_device *dev)
{
1580
	struct smsc_ircc_cb *self = netdev_priv(dev);
L
Linus Torvalds 已提交
1581 1582 1583 1584 1585 1586 1587 1588 1589
	int boguscount = 0;
	int iobase;
	int iir, lsr;

	/* Already locked comming here in smsc_ircc_interrupt() */
	/*spin_lock(&self->lock);*/

	iobase = self->io.sir_base;

1590
	iir = inb(iobase + UART_IIR) & UART_IIR_ID;
L
Linus Torvalds 已提交
1591 1592 1593 1594
	if (iir == 0)
		return IRQ_NONE;
	while (iir) {
		/* Clear interrupt */
1595
		lsr = inb(iobase + UART_LSR);
L
Linus Torvalds 已提交
1596

1597
		IRDA_DEBUG(4, "%s(), iir=%02x, lsr=%02x, iobase=%#x\n",
1598
			    __func__, iir, lsr, iobase);
L
Linus Torvalds 已提交
1599 1600 1601

		switch (iir) {
		case UART_IIR_RLSI:
1602
			IRDA_DEBUG(2, "%s(), RLSI\n", __func__);
L
Linus Torvalds 已提交
1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
			break;
		case UART_IIR_RDI:
			/* Receive interrupt */
			smsc_ircc_sir_receive(self);
			break;
		case UART_IIR_THRI:
			if (lsr & UART_LSR_THRE)
				/* Transmitter ready for data */
				smsc_ircc_sir_write_wakeup(self);
			break;
		default:
			IRDA_DEBUG(0, "%s(), unhandled IIR=%#x\n",
1615
				   __func__, iir);
L
Linus Torvalds 已提交
1616
			break;
1617 1618
		}

L
Linus Torvalds 已提交
1619 1620 1621 1622
		/* Make sure we don't stay here to long */
		if (boguscount++ > 100)
			break;

1623
	        iir = inb(iobase + UART_IIR) & UART_IIR_ID;
L
Linus Torvalds 已提交
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
	}
	/*spin_unlock(&self->lock);*/
	return IRQ_HANDLED;
}


#if 0 /* unused */
/*
 * Function ircc_is_receiving (self)
 *
 *    Return TRUE is we are currently receiving a frame
 *
 */
static int ircc_is_receiving(struct smsc_ircc_cb *self)
{
	int status = FALSE;
	/* int iobase; */

1642
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
1643 1644 1645

	IRDA_ASSERT(self != NULL, return FALSE;);

1646
	IRDA_DEBUG(0, "%s: dma count = %d\n", __func__,
L
Linus Torvalds 已提交
1647 1648 1649
		   get_dma_residue(self->io.dma));

	status = (self->rx_buff.state != OUTSIDE_FRAME);
1650

L
Linus Torvalds 已提交
1651 1652 1653 1654
	return status;
}
#endif /* unused */

1655 1656 1657 1658 1659 1660 1661 1662
static int smsc_ircc_request_irq(struct smsc_ircc_cb *self)
{
	int error;

	error = request_irq(self->io.irq, smsc_ircc_interrupt, 0,
			    self->netdev->name, self->netdev);
	if (error)
		IRDA_DEBUG(0, "%s(), unable to allocate irq=%d, err=%d\n",
1663
			   __func__, self->io.irq, error);
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694

	return error;
}

static void smsc_ircc_start_interrupts(struct smsc_ircc_cb *self)
{
	unsigned long flags;

	spin_lock_irqsave(&self->lock, flags);

	self->io.speed = 0;
	smsc_ircc_change_speed(self, SMSC_IRCC2_C_IRDA_FALLBACK_SPEED);

	spin_unlock_irqrestore(&self->lock, flags);
}

static void smsc_ircc_stop_interrupts(struct smsc_ircc_cb *self)
{
	int iobase = self->io.fir_base;
	unsigned long flags;

	spin_lock_irqsave(&self->lock, flags);

	register_bank(iobase, 0);
	outb(0, iobase + IRCC_IER);
	outb(IRCC_MASTER_RESET, iobase + IRCC_MASTER);
	outb(0x00, iobase + IRCC_MASTER);

	spin_unlock_irqrestore(&self->lock, flags);
}

L
Linus Torvalds 已提交
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706

/*
 * Function smsc_ircc_net_open (dev)
 *
 *    Start the device
 *
 */
static int smsc_ircc_net_open(struct net_device *dev)
{
	struct smsc_ircc_cb *self;
	char hwname[16];

1707
	IRDA_DEBUG(1, "%s\n", __func__);
1708

L
Linus Torvalds 已提交
1709
	IRDA_ASSERT(dev != NULL, return -1;);
1710
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
1711
	IRDA_ASSERT(self != NULL, return 0;);
1712

1713
	if (self->io.suspended) {
1714
		IRDA_DEBUG(0, "%s(), device is suspended\n", __func__);
1715 1716 1717
		return -EAGAIN;
	}

1718
	if (request_irq(self->io.irq, smsc_ircc_interrupt, 0, dev->name,
L
Linus Torvalds 已提交
1719 1720
			(void *) dev)) {
		IRDA_DEBUG(0, "%s(), unable to allocate irq=%d\n",
1721
			   __func__, self->io.irq);
L
Linus Torvalds 已提交
1722 1723 1724
		return -EAGAIN;
	}

1725
	smsc_ircc_start_interrupts(self);
1726

L
Linus Torvalds 已提交
1727 1728 1729 1730
	/* Give self a hardware name */
	/* It would be cool to offer the chip revision here - Jean II */
	sprintf(hwname, "SMSC @ 0x%03x", self->io.fir_base);

1731
	/*
L
Linus Torvalds 已提交
1732
	 * Open new IrLAP layer instance, now that everything should be
1733
	 * initialized properly
L
Linus Torvalds 已提交
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744
	 */
	self->irlap = irlap_open(dev, &self->qos, hwname);

	/*
	 * Always allocate the DMA channel after the IRQ,
	 * and clean up on failure.
	 */
	if (request_dma(self->io.dma, dev->name)) {
		smsc_ircc_net_close(dev);

		IRDA_WARNING("%s(), unable to allocate DMA=%d\n",
1745
			     __func__, self->io.dma);
L
Linus Torvalds 已提交
1746 1747
		return -EAGAIN;
	}
1748

L
Linus Torvalds 已提交
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
	netif_start_queue(dev);

	return 0;
}

/*
 * Function smsc_ircc_net_close (dev)
 *
 *    Stop the device
 *
 */
static int smsc_ircc_net_close(struct net_device *dev)
{
	struct smsc_ircc_cb *self;

1764
	IRDA_DEBUG(1, "%s\n", __func__);
1765

L
Linus Torvalds 已提交
1766
	IRDA_ASSERT(dev != NULL, return -1;);
1767
	self = netdev_priv(dev);
L
Linus Torvalds 已提交
1768
	IRDA_ASSERT(self != NULL, return 0;);
1769

L
Linus Torvalds 已提交
1770 1771
	/* Stop device */
	netif_stop_queue(dev);
1772

L
Linus Torvalds 已提交
1773 1774 1775 1776 1777
	/* Stop and remove instance of IrLAP */
	if (self->irlap)
		irlap_close(self->irlap);
	self->irlap = NULL;

1778 1779 1780 1781 1782 1783
	smsc_ircc_stop_interrupts(self);

	/* if we are called from smsc_ircc_resume we don't have IRQ reserved */
	if (!self->io.suspended)
		free_irq(self->io.irq, dev);

L
Linus Torvalds 已提交
1784 1785 1786 1787 1788 1789
	disable_dma(self->io.dma);
	free_dma(self->io.dma);

	return 0;
}

1790
static int smsc_ircc_suspend(struct platform_device *dev, pm_message_t state)
L
Linus Torvalds 已提交
1791
{
1792
	struct smsc_ircc_cb *self = platform_get_drvdata(dev);
1793

1794
	if (!self->io.suspended) {
1795 1796 1797 1798 1799 1800 1801 1802 1803
		IRDA_DEBUG(1, "%s, Suspending\n", driver_name);

		rtnl_lock();
		if (netif_running(self->netdev)) {
			netif_device_detach(self->netdev);
			smsc_ircc_stop_interrupts(self);
			free_irq(self->io.irq, self->netdev);
			disable_dma(self->io.dma);
		}
1804
		self->io.suspended = 1;
1805
		rtnl_unlock();
1806
	}
1807 1808

	return 0;
L
Linus Torvalds 已提交
1809 1810
}

1811
static int smsc_ircc_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
1812
{
1813
	struct smsc_ircc_cb *self = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
1814

1815
	if (self->io.suspended) {
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
		IRDA_DEBUG(1, "%s, Waking up\n", driver_name);

		rtnl_lock();
		smsc_ircc_init_chip(self);
		if (netif_running(self->netdev)) {
			if (smsc_ircc_request_irq(self)) {
				/*
				 * Don't fail resume process, just kill this
				 * network interface
				 */
				unregister_netdevice(self->netdev);
			} else {
				enable_dma(self->io.dma);
				smsc_ircc_start_interrupts(self);
				netif_device_attach(self->netdev);
			}
		}
1833
		self->io.suspended = 0;
1834
		rtnl_unlock();
1835
	}
L
Linus Torvalds 已提交
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
	return 0;
}

/*
 * Function smsc_ircc_close (self)
 *
 *    Close driver instance
 *
 */
static int __exit smsc_ircc_close(struct smsc_ircc_cb *self)
{
1847
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
1848 1849 1850

	IRDA_ASSERT(self != NULL, return -1;);

1851
	platform_device_unregister(self->pldev);
L
Linus Torvalds 已提交
1852 1853 1854 1855

	/* Remove netdevice */
	unregister_netdev(self->netdev);

1856
	smsc_ircc_stop_interrupts(self);
L
Linus Torvalds 已提交
1857 1858

	/* Release the PORTS that this driver is using */
1859
	IRDA_DEBUG(0, "%s(), releasing 0x%03x\n",  __func__,
L
Linus Torvalds 已提交
1860 1861 1862 1863
		   self->io.fir_base);

	release_region(self->io.fir_base, self->io.fir_ext);

1864
	IRDA_DEBUG(0, "%s(), releasing 0x%03x\n", __func__,
L
Linus Torvalds 已提交
1865 1866 1867 1868 1869 1870 1871
		   self->io.sir_base);

	release_region(self->io.sir_base, self->io.sir_ext);

	if (self->tx_buff.head)
		dma_free_coherent(NULL, self->tx_buff.truesize,
				  self->tx_buff.head, self->tx_buff_dma);
1872

L
Linus Torvalds 已提交
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
	if (self->rx_buff.head)
		dma_free_coherent(NULL, self->rx_buff.truesize,
				  self->rx_buff.head, self->rx_buff_dma);

	free_netdev(self->netdev);

	return 0;
}

static void __exit smsc_ircc_cleanup(void)
{
	int i;

1886
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
1887

1888
	for (i = 0; i < 2; i++) {
L
Linus Torvalds 已提交
1889 1890 1891
		if (dev_self[i])
			smsc_ircc_close(dev_self[i]);
	}
1892

B
Bjorn Helgaas 已提交
1893 1894 1895
	if (pnp_driver_registered)
		pnp_unregister_driver(&smsc_ircc_pnp_driver);

1896
	platform_driver_unregister(&smsc_ircc_driver);
L
Linus Torvalds 已提交
1897 1898 1899 1900 1901 1902 1903 1904
}

/*
 *	Start SIR operations
 *
 * This function *must* be called with spinlock held, because it may
 * be called from the irq handler (via smsc_ircc_change_speed()). - Jean II
 */
1905
static void smsc_ircc_sir_start(struct smsc_ircc_cb *self)
L
Linus Torvalds 已提交
1906 1907 1908 1909
{
	struct net_device *dev;
	int fir_base, sir_base;

1910
	IRDA_DEBUG(3, "%s\n", __func__);
L
Linus Torvalds 已提交
1911

1912
	IRDA_ASSERT(self != NULL, return;);
1913
	dev = self->netdev;
1914
	IRDA_ASSERT(dev != NULL, return;);
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919

	fir_base = self->io.fir_base;
	sir_base = self->io.sir_base;

	/* Reset everything */
1920
	outb(IRCC_MASTER_RESET, fir_base + IRCC_MASTER);
L
Linus Torvalds 已提交
1921 1922 1923 1924 1925 1926

	#if SMSC_IRCC2_C_SIR_STOP
	/*smsc_ircc_sir_stop(self);*/
	#endif

	register_bank(fir_base, 1);
1927
	outb(((inb(fir_base + IRCC_SCE_CFGA) & IRCC_SCE_CFGA_BLOCK_CTRL_BITS_MASK) | IRCC_CFGA_IRDA_SIR_A), fir_base + IRCC_SCE_CFGA);
L
Linus Torvalds 已提交
1928 1929

	/* Initialize UART */
1930 1931
	outb(UART_LCR_WLEN8, sir_base + UART_LCR);  /* Reset DLAB */
	outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), sir_base + UART_MCR);
1932

L
Linus Torvalds 已提交
1933
	/* Turn on interrups */
1934
	outb(UART_IER_RLSI | UART_IER_RDI |UART_IER_THRI, sir_base + UART_IER);
L
Linus Torvalds 已提交
1935

1936
	IRDA_DEBUG(3, "%s() - exit\n", __func__);
L
Linus Torvalds 已提交
1937

1938
	outb(0x00, fir_base + IRCC_MASTER);
L
Linus Torvalds 已提交
1939 1940 1941 1942 1943 1944 1945
}

#if SMSC_IRCC2_C_SIR_STOP
void smsc_ircc_sir_stop(struct smsc_ircc_cb *self)
{
	int iobase;

1946
	IRDA_DEBUG(3, "%s\n", __func__);
L
Linus Torvalds 已提交
1947 1948 1949
	iobase = self->io.sir_base;

	/* Reset UART */
1950
	outb(0, iobase + UART_MCR);
1951

L
Linus Torvalds 已提交
1952
	/* Turn off interrupts */
1953
	outb(0, iobase + UART_IER);
L
Linus Torvalds 已提交
1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971
}
#endif

/*
 * Function smsc_sir_write_wakeup (self)
 *
 *    Called by the SIR interrupt handler when there's room for more data.
 *    If we have more packets to send, we send them here.
 *
 */
static void smsc_ircc_sir_write_wakeup(struct smsc_ircc_cb *self)
{
	int actual = 0;
	int iobase;
	int fcr;

	IRDA_ASSERT(self != NULL, return;);

1972
	IRDA_DEBUG(4, "%s\n", __func__);
L
Linus Torvalds 已提交
1973 1974 1975 1976 1977 1978

	iobase = self->io.sir_base;

	/* Finished with frame?  */
	if (self->tx_buff.len > 0)  {
		/* Write data left in transmit buffer */
1979
		actual = smsc_ircc_sir_write(iobase, self->io.fifo_size,
L
Linus Torvalds 已提交
1980 1981 1982 1983
				      self->tx_buff.data, self->tx_buff.len);
		self->tx_buff.data += actual;
		self->tx_buff.len  -= actual;
	} else {
1984

L
Linus Torvalds 已提交
1985
	/*if (self->tx_buff.len ==0)  {*/
1986 1987 1988

		/*
		 *  Now serial buffer is almost free & we can start
L
Linus Torvalds 已提交
1989 1990 1991 1992 1993
		 *  transmission of another packet. But first we must check
		 *  if we need to change the speed of the hardware
		 */
		if (self->new_speed) {
			IRDA_DEBUG(5, "%s(), Changing speed to %d.\n",
1994
				   __func__, self->new_speed);
L
Linus Torvalds 已提交
1995 1996 1997 1998 1999 2000 2001
			smsc_ircc_sir_wait_hw_transmitter_finish(self);
			smsc_ircc_change_speed(self, self->new_speed);
			self->new_speed = 0;
		} else {
			/* Tell network layer that we want more frames */
			netif_wake_queue(self->netdev);
		}
2002
		self->netdev->stats.tx_packets++;
L
Linus Torvalds 已提交
2003

2004 2005 2006 2007 2008 2009 2010 2011
		if (self->io.speed <= 115200) {
			/*
			 * Reset Rx FIFO to make sure that all reflected transmit data
			 * is discarded. This is needed for half duplex operation
			 */
			fcr = UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR;
			fcr |= self->io.speed < 38400 ?
					UART_FCR_TRIGGER_1 : UART_FCR_TRIGGER_14;
L
Linus Torvalds 已提交
2012

2013
			outb(fcr, iobase + UART_FCR);
L
Linus Torvalds 已提交
2014

2015 2016
			/* Turn on receive interrupts */
			outb(UART_IER_RDI, iobase + UART_IER);
L
Linus Torvalds 已提交
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
		}
	}
}

/*
 * Function smsc_ircc_sir_write (iobase, fifo_size, buf, len)
 *
 *    Fill Tx FIFO with transmit data
 *
 */
static int smsc_ircc_sir_write(int iobase, int fifo_size, __u8 *buf, int len)
{
	int actual = 0;
2030

L
Linus Torvalds 已提交
2031
	/* Tx FIFO should be empty! */
2032
	if (!(inb(iobase + UART_LSR) & UART_LSR_THRE)) {
2033
		IRDA_WARNING("%s(), failed, fifo not empty!\n", __func__);
L
Linus Torvalds 已提交
2034 2035
		return 0;
	}
2036

L
Linus Torvalds 已提交
2037
	/* Fill FIFO with current frame */
2038
	while (fifo_size-- > 0 && actual < len) {
L
Linus Torvalds 已提交
2039
		/* Transmit next byte */
2040
		outb(buf[actual], iobase + UART_TX);
L
Linus Torvalds 已提交
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
		actual++;
	}
	return actual;
}

/*
 * Function smsc_ircc_is_receiving (self)
 *
 *    Returns true is we are currently receiving data
 *
 */
static int smsc_ircc_is_receiving(struct smsc_ircc_cb *self)
{
	return (self->rx_buff.state != OUTSIDE_FRAME);
}


/*
 * Function smsc_ircc_probe_transceiver(self)
 *
 *    Tries to find the used Transceiver
 *
 */
static void smsc_ircc_probe_transceiver(struct smsc_ircc_cb *self)
{
	unsigned int	i;
2067

L
Linus Torvalds 已提交
2068
	IRDA_ASSERT(self != NULL, return;);
2069

2070 2071
	for (i = 0; smsc_transceivers[i].name != NULL; i++)
		if (smsc_transceivers[i].probe(self->io.fir_base)) {
L
Linus Torvalds 已提交
2072 2073
			IRDA_MESSAGE(" %s transceiver found\n",
				     smsc_transceivers[i].name);
2074
			self->transceiver= i + 1;
L
Linus Torvalds 已提交
2075 2076
			return;
		}
2077

L
Linus Torvalds 已提交
2078 2079
	IRDA_MESSAGE("No transceiver found. Defaulting to %s\n",
		     smsc_transceivers[SMSC_IRCC2_C_DEFAULT_TRANSCEIVER].name);
2080

2081
	self->transceiver = SMSC_IRCC2_C_DEFAULT_TRANSCEIVER;
L
Linus Torvalds 已提交
2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
}


/*
 * Function smsc_ircc_set_transceiver_for_speed(self, speed)
 *
 *    Set the transceiver according to the speed
 *
 */
static void smsc_ircc_set_transceiver_for_speed(struct smsc_ircc_cb *self, u32 speed)
{
	unsigned int trx;
2094

L
Linus Torvalds 已提交
2095
	trx = self->transceiver;
2096 2097
	if (trx > 0)
		smsc_transceivers[trx - 1].set_for_speed(self->io.fir_base, speed);
L
Linus Torvalds 已提交
2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124
}

/*
 * Function smsc_ircc_wait_hw_transmitter_finish ()
 *
 *    Wait for the real end of HW transmission
 *
 * The UART is a strict FIFO, and we get called only when we have finished
 * pushing data to the FIFO, so the maximum amount of time we must wait
 * is only for the FIFO to drain out.
 *
 * We use a simple calibrated loop. We may need to adjust the loop
 * delay (udelay) to balance I/O traffic and latency. And we also need to
 * adjust the maximum timeout.
 * It would probably be better to wait for the proper interrupt,
 * but it doesn't seem to be available.
 *
 * We can't use jiffies or kernel timers because :
 * 1) We are called from the interrupt handler, which disable softirqs,
 * so jiffies won't be increased
 * 2) Jiffies granularity is usually very coarse (10ms), and we don't
 * want to wait that long to detect stuck hardware.
 * Jean II
 */

static void smsc_ircc_sir_wait_hw_transmitter_finish(struct smsc_ircc_cb *self)
{
2125
	int iobase = self->io.sir_base;
L
Linus Torvalds 已提交
2126
	int count = SMSC_IRCC2_HW_TRANSMITTER_TIMEOUT_US;
2127

L
Linus Torvalds 已提交
2128
	/* Calibrated busy loop */
2129
	while (count-- > 0 && !(inb(iobase + UART_LSR) & UART_LSR_TEMT))
L
Linus Torvalds 已提交
2130 2131
		udelay(1);

R
Roel Kluin 已提交
2132
	if (count < 0)
2133
		IRDA_DEBUG(0, "%s(): stuck transmitter\n", __func__);
L
Linus Torvalds 已提交
2134 2135 2136 2137 2138
}


/* PROBING
 *
2139 2140
 * REVISIT we can be told about the device by PNP, and should use that info
 * instead of probing hardware and creating a platform_device ...
L
Linus Torvalds 已提交
2141 2142 2143 2144
 */

static int __init smsc_ircc_look_for_chips(void)
{
2145
	struct smsc_chip_address *address;
2146
	char *type;
L
Linus Torvalds 已提交
2147
	unsigned int cfg_base, found;
2148

L
Linus Torvalds 已提交
2149 2150
	found = 0;
	address = possible_addresses;
2151

2152
	while (address->cfg_base) {
L
Linus Torvalds 已提交
2153
		cfg_base = address->cfg_base;
2154

2155
		/*printk(KERN_WARNING "%s(): probing: 0x%02x for: 0x%02x\n", __func__, cfg_base, address->type);*/
2156

2157
		if (address->type & SMSCSIO_TYPE_FDC) {
L
Linus Torvalds 已提交
2158
			type = "FDC";
2159 2160 2161 2162 2163 2164 2165
			if (address->type & SMSCSIO_TYPE_FLAT)
				if (!smsc_superio_flat(fdc_chips_flat, cfg_base, type))
					found++;

			if (address->type & SMSCSIO_TYPE_PAGED)
				if (!smsc_superio_paged(fdc_chips_paged, cfg_base, type))
					found++;
L
Linus Torvalds 已提交
2166
		}
2167
		if (address->type & SMSCSIO_TYPE_LPC) {
L
Linus Torvalds 已提交
2168
			type = "LPC";
2169 2170 2171 2172 2173 2174 2175
			if (address->type & SMSCSIO_TYPE_FLAT)
				if (!smsc_superio_flat(lpc_chips_flat, cfg_base, type))
					found++;

			if (address->type & SMSCSIO_TYPE_PAGED)
				if (!smsc_superio_paged(lpc_chips_paged, cfg_base, type))
					found++;
L
Linus Torvalds 已提交
2176 2177 2178 2179
		}
		address++;
	}
	return found;
2180
}
L
Linus Torvalds 已提交
2181 2182 2183 2184 2185 2186 2187

/*
 * Function smsc_superio_flat (chip, base, type)
 *
 *    Try to get configuration of a smc SuperIO chip with flat register model
 *
 */
2188
static int __init smsc_superio_flat(const struct smsc_chip *chips, unsigned short cfgbase, char *type)
L
Linus Torvalds 已提交
2189 2190 2191 2192 2193
{
	unsigned short firbase, sirbase;
	u8 mode, dma, irq;
	int ret = -ENODEV;

2194
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
2195

2196
	if (smsc_ircc_probe(cfgbase, SMSCSIOFLAT_DEVICEID_REG, chips, type) == NULL)
L
Linus Torvalds 已提交
2197 2198 2199
		return ret;

	outb(SMSCSIOFLAT_UARTMODE0C_REG, cfgbase);
2200
	mode = inb(cfgbase + 1);
2201

2202
	/*printk(KERN_WARNING "%s(): mode: 0x%02x\n", __func__, mode);*/
2203

2204
	if (!(mode & SMSCSIOFLAT_UART2MODE_VAL_IRDA))
2205
		IRDA_WARNING("%s(): IrDA not enabled\n", __func__);
L
Linus Torvalds 已提交
2206 2207

	outb(SMSCSIOFLAT_UART2BASEADDR_REG, cfgbase);
2208
	sirbase = inb(cfgbase + 1) << 2;
L
Linus Torvalds 已提交
2209

2210
	/* FIR iobase */
L
Linus Torvalds 已提交
2211
	outb(SMSCSIOFLAT_FIRBASEADDR_REG, cfgbase);
2212
	firbase = inb(cfgbase + 1) << 3;
L
Linus Torvalds 已提交
2213 2214 2215

	/* DMA */
	outb(SMSCSIOFLAT_FIRDMASELECT_REG, cfgbase);
2216
	dma = inb(cfgbase + 1) & SMSCSIOFLAT_FIRDMASELECT_MASK;
2217

L
Linus Torvalds 已提交
2218 2219
	/* IRQ */
	outb(SMSCSIOFLAT_UARTIRQSELECT_REG, cfgbase);
2220
	irq = inb(cfgbase + 1) & SMSCSIOFLAT_UART2IRQSELECT_MASK;
L
Linus Torvalds 已提交
2221

2222
	IRDA_MESSAGE("%s(): fir: 0x%02x, sir: 0x%02x, dma: %02d, irq: %d, mode: 0x%02x\n", __func__, firbase, sirbase, dma, irq, mode);
L
Linus Torvalds 已提交
2223

2224 2225
	if (firbase && smsc_ircc_open(firbase, sirbase, dma, irq) == 0)
		ret = 0;
2226

L
Linus Torvalds 已提交
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238
	/* Exit configuration */
	outb(SMSCSIO_CFGEXITKEY, cfgbase);

	return ret;
}

/*
 * Function smsc_superio_paged (chip, base, type)
 *
 *    Try  to get configuration of a smc SuperIO chip with paged register model
 *
 */
2239
static int __init smsc_superio_paged(const struct smsc_chip *chips, unsigned short cfg_base, char *type)
L
Linus Torvalds 已提交
2240 2241 2242
{
	unsigned short fir_io, sir_io;
	int ret = -ENODEV;
2243

2244
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
2245

2246
	if (smsc_ircc_probe(cfg_base, 0x20, chips, type) == NULL)
L
Linus Torvalds 已提交
2247
		return ret;
2248

L
Linus Torvalds 已提交
2249 2250 2251
	/* Select logical device (UART2) */
	outb(0x07, cfg_base);
	outb(0x05, cfg_base + 1);
2252

L
Linus Torvalds 已提交
2253 2254
	/* SIR iobase */
	outb(0x60, cfg_base);
2255
	sir_io = inb(cfg_base + 1) << 8;
L
Linus Torvalds 已提交
2256 2257
	outb(0x61, cfg_base);
	sir_io |= inb(cfg_base + 1);
2258

L
Linus Torvalds 已提交
2259 2260 2261 2262 2263 2264 2265
	/* Read FIR base */
	outb(0x62, cfg_base);
	fir_io = inb(cfg_base + 1) << 8;
	outb(0x63, cfg_base);
	fir_io |= inb(cfg_base + 1);
	outb(0x2b, cfg_base); /* ??? */

2266 2267
	if (fir_io && smsc_ircc_open(fir_io, sir_io, ircc_dma, ircc_irq) == 0)
		ret = 0;
2268

L
Linus Torvalds 已提交
2269 2270 2271 2272 2273 2274 2275
	/* Exit configuration */
	outb(SMSCSIO_CFGEXITKEY, cfg_base);

	return ret;
}


2276
static int __init smsc_access(unsigned short cfg_base, unsigned char reg)
L
Linus Torvalds 已提交
2277
{
2278
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
2279 2280

	outb(reg, cfg_base);
2281
	return inb(cfg_base) != reg ? -1 : 0;
L
Linus Torvalds 已提交
2282 2283
}

2284
static const struct smsc_chip * __init smsc_ircc_probe(unsigned short cfg_base, u8 reg, const struct smsc_chip *chip, char *type)
L
Linus Torvalds 已提交
2285
{
2286
	u8 devid, xdevid, rev;
L
Linus Torvalds 已提交
2287

2288
	IRDA_DEBUG(1, "%s\n", __func__);
L
Linus Torvalds 已提交
2289 2290 2291 2292 2293 2294 2295 2296 2297 2298

	/* Leave configuration */

	outb(SMSCSIO_CFGEXITKEY, cfg_base);

	if (inb(cfg_base) == SMSCSIO_CFGEXITKEY)	/* not a smc superio chip */
		return NULL;

	outb(reg, cfg_base);

2299
	xdevid = inb(cfg_base + 1);
L
Linus Torvalds 已提交
2300 2301 2302 2303 2304 2305 2306 2307 2308

	/* Enter configuration */

	outb(SMSCSIO_CFGACCESSKEY, cfg_base);

	#if 0
	if (smsc_access(cfg_base,0x55))	/* send second key and check */
		return NULL;
	#endif
2309

L
Linus Torvalds 已提交
2310 2311
	/* probe device ID */

2312
	if (smsc_access(cfg_base, reg))
L
Linus Torvalds 已提交
2313 2314
		return NULL;

2315
	devid = inb(cfg_base + 1);
2316

2317
	if (devid == 0 || devid == 0xff)	/* typical values for unused port */
L
Linus Torvalds 已提交
2318 2319 2320 2321
		return NULL;

	/* probe revision ID */

2322
	if (smsc_access(cfg_base, reg + 1))
L
Linus Torvalds 已提交
2323 2324
		return NULL;

2325
	rev = inb(cfg_base + 1);
L
Linus Torvalds 已提交
2326

2327
	if (rev >= 128)			/* i think this will make no sense */
L
Linus Torvalds 已提交
2328 2329
		return NULL;

2330
	if (devid == xdevid)		/* protection against false positives */
L
Linus Torvalds 已提交
2331 2332 2333 2334
		return NULL;

	/* Check for expected device ID; are there others? */

2335
	while (chip->devid != devid) {
L
Linus Torvalds 已提交
2336 2337 2338

		chip++;

2339
		if (chip->name == NULL)
L
Linus Torvalds 已提交
2340 2341 2342
			return NULL;
	}

2343 2344
	IRDA_MESSAGE("found SMC SuperIO Chip (devid=0x%02x rev=%02X base=0x%04x): %s%s\n",
		     devid, rev, cfg_base, type, chip->name);
L
Linus Torvalds 已提交
2345

2346
	if (chip->rev > rev) {
2347
		IRDA_MESSAGE("Revision higher than expected\n");
L
Linus Torvalds 已提交
2348 2349
		return NULL;
	}
2350

2351
	if (chip->flags & NoIRDA)
L
Linus Torvalds 已提交
2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362
		IRDA_MESSAGE("chipset does not support IRDA\n");

	return chip;
}

static int __init smsc_superio_fdc(unsigned short cfg_base)
{
	int ret = -1;

	if (!request_region(cfg_base, 2, driver_name)) {
		IRDA_WARNING("%s: can't get cfg_base of 0x%03x\n",
2363
			     __func__, cfg_base);
L
Linus Torvalds 已提交
2364
	} else {
2365 2366
		if (!smsc_superio_flat(fdc_chips_flat, cfg_base, "FDC") ||
		    !smsc_superio_paged(fdc_chips_paged, cfg_base, "FDC"))
L
Linus Torvalds 已提交
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
			ret =  0;

		release_region(cfg_base, 2);
	}

	return ret;
}

static int __init smsc_superio_lpc(unsigned short cfg_base)
{
	int ret = -1;

	if (!request_region(cfg_base, 2, driver_name)) {
		IRDA_WARNING("%s: can't get cfg_base of 0x%03x\n",
2381
			     __func__, cfg_base);
L
Linus Torvalds 已提交
2382
	} else {
2383 2384
		if (!smsc_superio_flat(lpc_chips_flat, cfg_base, "LPC") ||
		    !smsc_superio_paged(lpc_chips_paged, cfg_base, "LPC"))
L
Linus Torvalds 已提交
2385
			ret = 0;
2386

L
Linus Torvalds 已提交
2387 2388 2389 2390 2391
		release_region(cfg_base, 2);
	}
	return ret;
}

2392 2393 2394 2395 2396
/*
 * Look for some specific subsystem setups that need
 * pre-configuration not properly done by the BIOS (especially laptops)
 * This code is based in part on smcinit.c, tosh1800-smcinit.c
 * and tosh2450-smcinit.c. The table lists the device entries
2397 2398 2399 2400 2401 2402 2403 2404
 * for ISA bridges with an LPC (Low Pin Count) controller which
 * handles the communication with the SMSC device. After the LPC
 * controller is initialized through PCI, the SMSC device is initialized
 * through a dedicated port in the ISA port-mapped I/O area, this latter
 * area is used to configure the SMSC device with default
 * SIR and FIR I/O ports, DMA and IRQ. Different vendors have
 * used different sets of parameters and different control port
 * addresses making a subsystem device table necessary.
2405 2406 2407 2408
 */
#ifdef CONFIG_PCI
#define PCIID_VENDOR_INTEL 0x8086
#define PCIID_VENDOR_ALI 0x10b9
2409
static struct smsc_ircc_subsystem_configuration subsystem_configurations[] __initdata = {
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
	/*
	 * Subsystems needing entries:
	 * 0x10b9:0x1533 0x103c:0x0850 HP nx9010 family
	 * 0x10b9:0x1533 0x0e11:0x005a Compaq nc4000 family
	 * 0x8086:0x24cc 0x0e11:0x002a HP nx9000 family
	 */
	{
		/* Guessed entry */
		.vendor = PCIID_VENDOR_INTEL, /* Intel 82801DBM LPC bridge */
		.device = 0x24cc,
		.subvendor = 0x103c,
		.subdevice = 0x08bc,
		.sir_io = 0x02f8,
		.fir_io = 0x0130,
		.fir_irq = 0x05,
		.fir_dma = 0x03,
		.cfg_base = 0x004e,
		.preconfigure = preconfigure_through_82801,
		.name = "HP nx5000 family",
	},
2430 2431 2432 2433 2434
	{
		.vendor = PCIID_VENDOR_INTEL, /* Intel 82801DBM LPC bridge */
		.device = 0x24cc,
		.subvendor = 0x103c,
		.subdevice = 0x088c,
2435 2436
		/* Quite certain these are the same for nc8000 as for nc6000 */
		.sir_io = 0x02f8,
2437
		.fir_io = 0x0130,
2438
		.fir_irq = 0x05,
2439 2440 2441
		.fir_dma = 0x03,
		.cfg_base = 0x004e,
		.preconfigure = preconfigure_through_82801,
2442
		.name = "HP nc8000 family",
2443 2444 2445 2446 2447 2448 2449 2450
	},
	{
		.vendor = PCIID_VENDOR_INTEL, /* Intel 82801DBM LPC bridge */
		.device = 0x24cc,
		.subvendor = 0x103c,
		.subdevice = 0x0890,
		.sir_io = 0x02f8,
		.fir_io = 0x0130,
2451
		.fir_irq = 0x05,
2452 2453 2454
		.fir_dma = 0x03,
		.cfg_base = 0x004e,
		.preconfigure = preconfigure_through_82801,
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469
		.name = "HP nc6000 family",
	},
	{
		.vendor = PCIID_VENDOR_INTEL, /* Intel 82801DBM LPC bridge */
		.device = 0x24cc,
		.subvendor = 0x0e11,
		.subdevice = 0x0860,
		/* I assume these are the same for x1000 as for the others */
		.sir_io = 0x02e8,
		.fir_io = 0x02f8,
		.fir_irq = 0x07,
		.fir_dma = 0x03,
		.cfg_base = 0x002e,
		.preconfigure = preconfigure_through_82801,
		.name = "Compaq x1000 family",
2470 2471
	},
	{
2472 2473
		/* Intel 82801DB/DBL (ICH4/ICH4-L) LPC Interface Bridge */
		.vendor = PCIID_VENDOR_INTEL,
2474 2475
		.device = 0x24c0,
		.subvendor = 0x1179,
2476
		.subdevice = 0xffff, /* 0xffff is "any" */
2477 2478 2479 2480 2481 2482
		.sir_io = 0x03f8,
		.fir_io = 0x0130,
		.fir_irq = 0x07,
		.fir_dma = 0x01,
		.cfg_base = 0x002e,
		.preconfigure = preconfigure_through_82801,
2483
		.name = "Toshiba laptop with Intel 82801DB/DBL LPC bridge",
2484 2485 2486
	},
	{
		.vendor = PCIID_VENDOR_INTEL, /* Intel 82801CAM ISA bridge */
2487
		.device = 0x248c,
2488
		.subvendor = 0x1179,
2489
		.subdevice = 0xffff, /* 0xffff is "any" */
2490 2491 2492 2493 2494 2495
		.sir_io = 0x03f8,
		.fir_io = 0x0130,
		.fir_irq = 0x03,
		.fir_dma = 0x03,
		.cfg_base = 0x002e,
		.preconfigure = preconfigure_through_82801,
2496
		.name = "Toshiba laptop with Intel 82801CAM ISA bridge",
2497 2498
	},
	{
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514
		/* 82801DBM (ICH4-M) LPC Interface Bridge */
		.vendor = PCIID_VENDOR_INTEL,
		.device = 0x24cc,
		.subvendor = 0x1179,
		.subdevice = 0xffff, /* 0xffff is "any" */
		.sir_io = 0x03f8,
		.fir_io = 0x0130,
		.fir_irq = 0x03,
		.fir_dma = 0x03,
		.cfg_base = 0x002e,
		.preconfigure = preconfigure_through_82801,
		.name = "Toshiba laptop with Intel 8281DBM LPC bridge",
	},
	{
		/* ALi M1533/M1535 PCI to ISA Bridge [Aladdin IV/V/V+] */
		.vendor = PCIID_VENDOR_ALI,
2515 2516
		.device = 0x1533,
		.subvendor = 0x1179,
2517
		.subdevice = 0xffff, /* 0xffff is "any" */
2518 2519 2520 2521 2522 2523
		.sir_io = 0x02e8,
		.fir_io = 0x02f8,
		.fir_irq = 0x07,
		.fir_dma = 0x03,
		.cfg_base = 0x002e,
		.preconfigure = preconfigure_through_ali,
2524
		.name = "Toshiba laptop with ALi ISA bridge",
2525 2526 2527 2528 2529 2530
	},
	{ } // Terminator
};


/*
2531 2532
 * This sets up the basic SMSC parameters
 * (FIR port, SIR port, FIR DMA, FIR IRQ)
2533 2534
 * through the chip configuration port.
 */
2535 2536 2537
static int __init preconfigure_smsc_chip(struct
					 smsc_ircc_subsystem_configuration
					 *conf)
2538 2539 2540 2541 2542 2543 2544
{
	unsigned short iobase = conf->cfg_base;
	unsigned char tmpbyte;

	outb(LPC47N227_CFGACCESSKEY, iobase); // enter configuration state
	outb(SMSCSIOFLAT_DEVICEID_REG, iobase); // set for device ID
	tmpbyte = inb(iobase +1); // Read device ID
2545 2546 2547
	IRDA_DEBUG(0,
		   "Detected Chip id: 0x%02x, setting up registers...\n",
		   tmpbyte);
2548 2549 2550 2551 2552 2553 2554 2555 2556

	/* Disable UART1 and set up SIR I/O port */
	outb(0x24, iobase);  // select CR24 - UART1 base addr
	outb(0x00, iobase + 1); // disable UART1
	outb(SMSCSIOFLAT_UART2BASEADDR_REG, iobase);  // select CR25 - UART2 base addr
	outb( (conf->sir_io >> 2), iobase + 1); // bits 2-9 of 0x3f8
	tmpbyte = inb(iobase + 1);
	if (tmpbyte != (conf->sir_io >> 2) ) {
		IRDA_WARNING("ERROR: could not configure SIR ioport.\n");
2557
		IRDA_WARNING("Try to supply ircc_cfg argument.\n");
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592
		return -ENXIO;
	}

	/* Set up FIR IRQ channel for UART2 */
	outb(SMSCSIOFLAT_UARTIRQSELECT_REG, iobase); // select CR28 - UART1,2 IRQ select
	tmpbyte = inb(iobase + 1);
	tmpbyte &= SMSCSIOFLAT_UART1IRQSELECT_MASK; // Do not touch the UART1 portion
	tmpbyte |= (conf->fir_irq & SMSCSIOFLAT_UART2IRQSELECT_MASK);
	outb(tmpbyte, iobase + 1);
	tmpbyte = inb(iobase + 1) & SMSCSIOFLAT_UART2IRQSELECT_MASK;
	if (tmpbyte != conf->fir_irq) {
		IRDA_WARNING("ERROR: could not configure FIR IRQ channel.\n");
		return -ENXIO;
	}

	/* Set up FIR I/O port */
	outb(SMSCSIOFLAT_FIRBASEADDR_REG, iobase);  // CR2B - SCE (FIR) base addr
	outb((conf->fir_io >> 3), iobase + 1);
	tmpbyte = inb(iobase + 1);
	if (tmpbyte != (conf->fir_io >> 3) ) {
		IRDA_WARNING("ERROR: could not configure FIR I/O port.\n");
		return -ENXIO;
	}

	/* Set up FIR DMA channel */
	outb(SMSCSIOFLAT_FIRDMASELECT_REG, iobase);  // CR2C - SCE (FIR) DMA select
	outb((conf->fir_dma & LPC47N227_FIRDMASELECT_MASK), iobase + 1); // DMA
	tmpbyte = inb(iobase + 1) & LPC47N227_FIRDMASELECT_MASK;
	if (tmpbyte != (conf->fir_dma & LPC47N227_FIRDMASELECT_MASK)) {
		IRDA_WARNING("ERROR: could not configure FIR DMA channel.\n");
		return -ENXIO;
	}

	outb(SMSCSIOFLAT_UARTMODE0C_REG, iobase);  // CR0C - UART mode
	tmpbyte = inb(iobase + 1);
2593 2594
	tmpbyte &= ~SMSCSIOFLAT_UART2MODE_MASK |
		SMSCSIOFLAT_UART2MODE_VAL_IRDA;
2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618
	outb(tmpbyte, iobase + 1); // enable IrDA (HPSIR) mode, high speed

	outb(LPC47N227_APMBOOTDRIVE_REG, iobase);  // CR07 - Auto Pwr Mgt/boot drive sel
	tmpbyte = inb(iobase + 1);
	outb(tmpbyte | LPC47N227_UART2AUTOPWRDOWN_MASK, iobase + 1); // enable UART2 autopower down

	/* This one was not part of tosh1800 */
	outb(0x0a, iobase);  // CR0a - ecp fifo / ir mux
	tmpbyte = inb(iobase + 1);
	outb(tmpbyte | 0x40, iobase + 1); // send active device to ir port

	outb(LPC47N227_UART12POWER_REG, iobase);  // CR02 - UART 1,2 power
	tmpbyte = inb(iobase + 1);
	outb(tmpbyte | LPC47N227_UART2POWERDOWN_MASK, iobase + 1); // UART2 power up mode, UART1 power down

	outb(LPC47N227_FDCPOWERVALIDCONF_REG, iobase);  // CR00 - FDC Power/valid config cycle
	tmpbyte = inb(iobase + 1);
	outb(tmpbyte | LPC47N227_VALID_MASK, iobase + 1); // valid config cycle done

	outb(LPC47N227_CFGEXITKEY, iobase);  // Exit configuration

	return 0;
}

2619
/* 82801CAM generic registers */
2620 2621
#define VID 0x00
#define DID 0x02
2622 2623 2624
#define PIRQ_A_D_ROUT 0x60
#define SIRQ_CNTL 0x64
#define PIRQ_E_H_ROUT 0x68
2625
#define PCI_DMA_C 0x90
2626
/* LPC-specific registers */
2627
#define COM_DEC 0xe0
2628
#define GEN1_DEC 0xe4
2629 2630 2631
#define LPC_EN 0xe6
#define GEN2_DEC 0xec
/*
2632 2633 2634
 * Sets up the I/O range using the 82801CAM ISA bridge, 82801DBM LPC bridge
 * or Intel 82801DB/DBL (ICH4/ICH4-L) LPC Interface Bridge.
 * They all work the same way!
2635 2636
 */
static int __init preconfigure_through_82801(struct pci_dev *dev,
2637 2638 2639
					     struct
					     smsc_ircc_subsystem_configuration
					     *conf)
2640 2641
{
	unsigned short tmpword;
2642
	unsigned char tmpbyte;
2643

2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694
	IRDA_MESSAGE("Setting up Intel 82801 controller and SMSC device\n");
	/*
	 * Select the range for the COMA COM port (SIR)
	 * Register COM_DEC:
	 * Bit 7: reserved
	 * Bit 6-4, COMB decode range
	 * Bit 3: reserved
	 * Bit 2-0, COMA decode range
	 *
	 * Decode ranges:
	 *   000 = 0x3f8-0x3ff (COM1)
	 *   001 = 0x2f8-0x2ff (COM2)
	 *   010 = 0x220-0x227
	 *   011 = 0x228-0x22f
	 *   100 = 0x238-0x23f
	 *   101 = 0x2e8-0x2ef (COM4)
	 *   110 = 0x338-0x33f
	 *   111 = 0x3e8-0x3ef (COM3)
	 */
	pci_read_config_byte(dev, COM_DEC, &tmpbyte);
	tmpbyte &= 0xf8; /* mask COMA bits */
	switch(conf->sir_io) {
	case 0x3f8:
		tmpbyte |= 0x00;
		break;
	case 0x2f8:
		tmpbyte |= 0x01;
		break;
	case 0x220:
		tmpbyte |= 0x02;
		break;
	case 0x228:
		tmpbyte |= 0x03;
		break;
	case 0x238:
		tmpbyte |= 0x04;
		break;
	case 0x2e8:
		tmpbyte |= 0x05;
		break;
	case 0x338:
		tmpbyte |= 0x06;
		break;
	case 0x3e8:
		tmpbyte |= 0x07;
		break;
	default:
		tmpbyte |= 0x01; /* COM2 default */
	}
	IRDA_DEBUG(1, "COM_DEC (write): 0x%02x\n", tmpbyte);
	pci_write_config_byte(dev, COM_DEC, tmpbyte);
2695

2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
	/* Enable Low Pin Count interface */
	pci_read_config_word(dev, LPC_EN, &tmpword);
	/* These seem to be set up at all times,
	 * just make sure it is properly set.
	 */
	switch(conf->cfg_base) {
	case 0x04e:
		tmpword |= 0x2000;
		break;
	case 0x02e:
		tmpword |= 0x1000;
		break;
	case 0x062:
		tmpword |= 0x0800;
		break;
	case 0x060:
		tmpword |= 0x0400;
		break;
	default:
		IRDA_WARNING("Uncommon I/O base address: 0x%04x\n",
			     conf->cfg_base);
		break;
	}
	tmpword &= 0xfffd; /* disable LPC COMB */
	tmpword |= 0x0001; /* set bit 0 : enable LPC COMA addr range (GEN2) */
	IRDA_DEBUG(1, "LPC_EN (write): 0x%04x\n", tmpword);
2722 2723
	pci_write_config_word(dev, LPC_EN, tmpword);

2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
	/*
	 * Configure LPC DMA channel
	 * PCI_DMA_C bits:
	 * Bit 15-14: DMA channel 7 select
	 * Bit 13-12: DMA channel 6 select
	 * Bit 11-10: DMA channel 5 select
	 * Bit 9-8:   Reserved
	 * Bit 7-6:   DMA channel 3 select
	 * Bit 5-4:   DMA channel 2 select
	 * Bit 3-2:   DMA channel 1 select
	 * Bit 1-0:   DMA channel 0 select
	 *  00 = Reserved value
	 *  01 = PC/PCI DMA
	 *  10 = Reserved value
	 *  11 = LPC I/F DMA
	 */
	pci_read_config_word(dev, PCI_DMA_C, &tmpword);
	switch(conf->fir_dma) {
	case 0x07:
		tmpword |= 0xc000;
		break;
	case 0x06:
		tmpword |= 0x3000;
		break;
	case 0x05:
		tmpword |= 0x0c00;
		break;
	case 0x03:
		tmpword |= 0x00c0;
		break;
	case 0x02:
		tmpword |= 0x0030;
		break;
	case 0x01:
		tmpword |= 0x000c;
		break;
	case 0x00:
		tmpword |= 0x0003;
		break;
	default:
		break; /* do not change settings */
	}
	IRDA_DEBUG(1, "PCI_DMA_C (write): 0x%04x\n", tmpword);
	pci_write_config_word(dev, PCI_DMA_C, tmpword);

	/*
	 * GEN2_DEC bits:
	 * Bit 15-4: Generic I/O range
	 * Bit 3-1: reserved (read as 0)
	 * Bit 0: enable GEN2 range on LPC I/F
	 */
	tmpword = conf->fir_io & 0xfff8;
	tmpword |= 0x0001;
	IRDA_DEBUG(1, "GEN2_DEC (write): 0x%04x\n", tmpword);
	pci_write_config_word(dev, GEN2_DEC, tmpword);
2779 2780

	/* Pre-configure chip */
2781 2782
	return preconfigure_smsc_chip(conf);
}
2783

2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825
/*
 * Pre-configure a certain port on the ALi 1533 bridge.
 * This is based on reverse-engineering since ALi does not
 * provide any data sheet for the 1533 chip.
 */
static void __init preconfigure_ali_port(struct pci_dev *dev,
					 unsigned short port)
{
	unsigned char reg;
	/* These bits obviously control the different ports */
	unsigned char mask;
	unsigned char tmpbyte;

	switch(port) {
	case 0x0130:
	case 0x0178:
		reg = 0xb0;
		mask = 0x80;
		break;
	case 0x03f8:
		reg = 0xb4;
		mask = 0x80;
		break;
	case 0x02f8:
		reg = 0xb4;
		mask = 0x30;
		break;
	case 0x02e8:
		reg = 0xb4;
		mask = 0x08;
		break;
	default:
		IRDA_ERROR("Failed to configure unsupported port on ALi 1533 bridge: 0x%04x\n", port);
		return;
	}

	pci_read_config_byte(dev, reg, &tmpbyte);
	/* Turn on the right bits */
	tmpbyte |= mask;
	pci_write_config_byte(dev, reg, tmpbyte);
	IRDA_MESSAGE("Activated ALi 1533 ISA bridge port 0x%04x.\n", port);
	return;
2826 2827 2828
}

static int __init preconfigure_through_ali(struct pci_dev *dev,
2829 2830 2831
					   struct
					   smsc_ircc_subsystem_configuration
					   *conf)
2832
{
2833 2834 2835 2836 2837 2838
	/* Configure the two ports on the ALi 1533 */
	preconfigure_ali_port(dev, conf->sir_io);
	preconfigure_ali_port(dev, conf->fir_io);

	/* Pre-configure chip */
	return preconfigure_smsc_chip(conf);
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857
}

static int __init smsc_ircc_preconfigure_subsystems(unsigned short ircc_cfg,
						    unsigned short ircc_fir,
						    unsigned short ircc_sir,
						    unsigned char ircc_dma,
						    unsigned char ircc_irq)
{
	struct pci_dev *dev = NULL;
	unsigned short ss_vendor = 0x0000;
	unsigned short ss_device = 0x0000;
	int ret = 0;

	dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);

	while (dev != NULL) {
		struct smsc_ircc_subsystem_configuration *conf;

		/*
2858 2859 2860 2861
		 * Cache the subsystem vendor/device:
		 * some manufacturers fail to set this for all components,
		 * so we save it in case there is just 0x0000 0x0000 on the
		 * device we want to check.
2862 2863 2864 2865 2866 2867 2868 2869 2870
		 */
		if (dev->subsystem_vendor != 0x0000U) {
			ss_vendor = dev->subsystem_vendor;
			ss_device = dev->subsystem_device;
		}
		conf = subsystem_configurations;
		for( ; conf->subvendor; conf++) {
			if(conf->vendor == dev->vendor &&
			   conf->device == dev->device &&
2871 2872 2873 2874 2875 2876
			   conf->subvendor == ss_vendor &&
			   /* Sometimes these are cached values */
			   (conf->subdevice == ss_device ||
			    conf->subdevice == 0xffff)) {
				struct smsc_ircc_subsystem_configuration
					tmpconf;
2877

2878 2879
				memcpy(&tmpconf, conf,
				       sizeof(struct smsc_ircc_subsystem_configuration));
2880

2881 2882 2883 2884
				/*
				 * Override the default values with anything
				 * passed in as parameter
				 */
2885 2886 2887 2888 2889 2890
				if (ircc_cfg != 0)
					tmpconf.cfg_base = ircc_cfg;
				if (ircc_fir != 0)
					tmpconf.fir_io = ircc_fir;
				if (ircc_sir != 0)
					tmpconf.sir_io = ircc_sir;
2891
				if (ircc_dma != DMA_INVAL)
2892
					tmpconf.fir_dma = ircc_dma;
2893
				if (ircc_irq != IRQ_INVAL)
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909
					tmpconf.fir_irq = ircc_irq;

				IRDA_MESSAGE("Detected unconfigured %s SMSC IrDA chip, pre-configuring device.\n", conf->name);
				if (conf->preconfigure)
					ret = conf->preconfigure(dev, &tmpconf);
				else
					ret = -ENODEV;
			}
		}
		dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev);
	}

	return ret;
}
#endif // CONFIG_PCI

L
Linus Torvalds 已提交
2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926
/************************************************
 *
 * Transceivers specific functions
 *
 ************************************************/


/*
 * Function smsc_ircc_set_transceiver_smsc_ircc_atc(fir_base, speed)
 *
 *    Program transceiver through smsc-ircc ATC circuitry
 *
 */

static void smsc_ircc_set_transceiver_smsc_ircc_atc(int fir_base, u32 speed)
{
	unsigned long jiffies_now, jiffies_timeout;
2927
	u8 val;
2928

2929 2930
	jiffies_now = jiffies;
	jiffies_timeout = jiffies + SMSC_IRCC2_ATC_PROGRAMMING_TIMEOUT_JIFFIES;
2931

L
Linus Torvalds 已提交
2932 2933
	/* ATC */
	register_bank(fir_base, 4);
2934 2935 2936 2937 2938 2939 2940 2941
	outb((inb(fir_base + IRCC_ATC) & IRCC_ATC_MASK) | IRCC_ATC_nPROGREADY|IRCC_ATC_ENABLE,
	     fir_base + IRCC_ATC);

	while ((val = (inb(fir_base + IRCC_ATC) & IRCC_ATC_nPROGREADY)) &&
		!time_after(jiffies, jiffies_timeout))
		/* empty */;

	if (val)
2942
		IRDA_WARNING("%s(): ATC: 0x%02x\n", __func__,
2943
			     inb(fir_base + IRCC_ATC));
L
Linus Torvalds 已提交
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
}

/*
 * Function smsc_ircc_probe_transceiver_smsc_ircc_atc(fir_base)
 *
 *    Probe transceiver smsc-ircc ATC circuitry
 *
 */

static int smsc_ircc_probe_transceiver_smsc_ircc_atc(int fir_base)
{
	return 0;
}

/*
 * Function smsc_ircc_set_transceiver_smsc_ircc_fast_pin_select(self, speed)
 *
2961
 *    Set transceiver
L
Linus Torvalds 已提交
2962 2963 2964 2965 2966
 *
 */

static void smsc_ircc_set_transceiver_smsc_ircc_fast_pin_select(int fir_base, u32 speed)
{
2967
	u8 fast_mode;
2968

2969 2970 2971
	switch (speed) {
	default:
	case 576000 :
2972
		fast_mode = 0;
L
Linus Torvalds 已提交
2973
		break;
2974 2975
	case 1152000 :
	case 4000000 :
L
Linus Torvalds 已提交
2976 2977 2978 2979
		fast_mode = IRCC_LCR_A_FAST;
		break;
	}
	register_bank(fir_base, 0);
2980
	outb((inb(fir_base + IRCC_LCR_A) & 0xbf) | fast_mode, fir_base + IRCC_LCR_A);
L
Linus Torvalds 已提交
2981 2982 2983 2984 2985
}

/*
 * Function smsc_ircc_probe_transceiver_smsc_ircc_fast_pin_select(fir_base)
 *
2986
 *    Probe transceiver
L
Linus Torvalds 已提交
2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997
 *
 */

static int smsc_ircc_probe_transceiver_smsc_ircc_fast_pin_select(int fir_base)
{
	return 0;
}

/*
 * Function smsc_ircc_set_transceiver_toshiba_sat1800(fir_base, speed)
 *
2998
 *    Set transceiver
L
Linus Torvalds 已提交
2999 3000 3001 3002 3003
 *
 */

static void smsc_ircc_set_transceiver_toshiba_sat1800(int fir_base, u32 speed)
{
3004
	u8 fast_mode;
3005

3006 3007 3008
	switch (speed) {
	default:
	case 576000 :
3009
		fast_mode = 0;
L
Linus Torvalds 已提交
3010
		break;
3011 3012
	case 1152000 :
	case 4000000 :
L
Linus Torvalds 已提交
3013 3014
		fast_mode = /*IRCC_LCR_A_FAST |*/ IRCC_LCR_A_GP_DATA;
		break;
3015

L
Linus Torvalds 已提交
3016 3017 3018
	}
	/* This causes an interrupt */
	register_bank(fir_base, 0);
3019
	outb((inb(fir_base + IRCC_LCR_A) &  0xbf) | fast_mode, fir_base + IRCC_LCR_A);
L
Linus Torvalds 已提交
3020 3021 3022 3023 3024
}

/*
 * Function smsc_ircc_probe_transceiver_toshiba_sat1800(fir_base)
 *
3025
 *    Probe transceiver
L
Linus Torvalds 已提交
3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036
 *
 */

static int smsc_ircc_probe_transceiver_toshiba_sat1800(int fir_base)
{
	return 0;
}


module_init(smsc_ircc_init);
module_exit(smsc_ircc_cleanup);