ibmtr_cs.c 12.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/*======================================================================

    A PCMCIA token-ring driver for IBM-based cards

    This driver supports the IBM PCMCIA Token-Ring Card.
    Written by Steve Kipisz, kipisz@vnet.ibm.com or
                             bungy@ibm.net

    Written 1995,1996.

    This code is based on pcnet_cs.c from David Hinds.
    
    V2.2.0 February 1999 - Mike Phillips phillim@amtrak.com

    Linux V2.2.x presented significant changes to the underlying
    ibmtr.c code.  Mainly the code became a lot more organized and
    modular.

    This caused the old PCMCIA Token Ring driver to give up and go 
    home early. Instead of just patching the old code to make it 
    work, the PCMCIA code has been streamlined, updated and possibly
    improved.

    This code now only contains code required for the Card Services.
    All we do here is set the card up enough so that the real ibmtr.c
    driver can find it and work with it properly.

    i.e. We set up the io port, irq, mmio memory and shared ram
    memory.  This enables ibmtr_probe in ibmtr.c to find the card and
    configure it as though it was a normal ISA and/or PnP card.

    CHANGES

    v2.2.5 April 1999 Mike Phillips (phillim@amtrak.com)
    Obscure bug fix, required changed to ibmtr.c not ibmtr_cs.c
    
    v2.2.7 May 1999 Mike Phillips (phillim@amtrak.com)
    Updated to version 2.2.7 to match the first version of the kernel
    that the modification to ibmtr.c were incorporated into.
    
    v2.2.17 July 2000 Burt Silverman (burts@us.ibm.com)
    Address translation feature of PCMCIA controller is usable so
    memory windows can be placed in High memory (meaning above
    0xFFFFF.)

======================================================================*/

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/ptrace.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/timer.h>
#include <linux/module.h>
#include <linux/ethtool.h>
#include <linux/netdevice.h>
#include <linux/trdevice.h>
#include <linux/ibmtr.h>

#include <pcmcia/cs_types.h>
#include <pcmcia/cs.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/ds.h>

#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/system.h>

#define PCMCIA
#include "../tokenring/ibmtr.c"

#ifdef PCMCIA_DEBUG
static int pc_debug = PCMCIA_DEBUG;
module_param(pc_debug, int, 0);
#define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
static char *version =
"ibmtr_cs.c 1.10   1996/01/06 05:19:00 (Steve Kipisz)\n"
"           2.2.7  1999/05/03 12:00:00 (Mike Phillips)\n"
"           2.4.2  2001/30/28 Midnight (Burt Silverman)\n";
#else
#define DEBUG(n, args...)
#endif

/*====================================================================*/

/* Parameters that can be set with 'insmod' */

/* MMIO base address */
static u_long mmiobase = 0xce000;

/* SRAM base address */
static u_long srambase = 0xd0000;

/* SRAM size 8,16,32,64 */
static u_long sramsize = 64;

/* Ringspeed 4,16 */
static int ringspeed = 16;

module_param(mmiobase, ulong, 0);
module_param(srambase, ulong, 0);
module_param(sramsize, ulong, 0);
module_param(ringspeed, int, 0);
MODULE_LICENSE("GPL");

/*====================================================================*/

108
static int ibmtr_config(struct pcmcia_device *link);
L
Linus Torvalds 已提交
109
static void ibmtr_hw_setup(struct net_device *dev, u_int mmiobase);
110
static void ibmtr_release(struct pcmcia_device *link);
111
static void ibmtr_detach(struct pcmcia_device *p_dev);
L
Linus Torvalds 已提交
112 113 114 115

/*====================================================================*/

typedef struct ibmtr_dev_t {
116
	struct pcmcia_device	*p_dev;
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124 125 126 127 128
    struct net_device	*dev;
    dev_node_t          node;
    window_handle_t     sram_win_handle;
    struct tok_info	*ti;
} ibmtr_dev_t;

static void netdev_get_drvinfo(struct net_device *dev,
			       struct ethtool_drvinfo *info)
{
	strcpy(info->driver, "ibmtr_cs");
}

129
static const struct ethtool_ops netdev_ethtool_ops = {
L
Linus Torvalds 已提交
130 131 132 133 134 135 136 137 138 139 140
	.get_drvinfo		= netdev_get_drvinfo,
};

/*======================================================================

    ibmtr_attach() creates an "instance" of the driver, allocating
    local data structures for one device.  The device is registered
    with Card Services.

======================================================================*/

141
static int __devinit ibmtr_attach(struct pcmcia_device *link)
L
Linus Torvalds 已提交
142 143 144
{
    ibmtr_dev_t *info;
    struct net_device *dev;
145

L
Linus Torvalds 已提交
146 147 148 149
    DEBUG(0, "ibmtr_attach()\n");

    /* Create new token-ring device */
    info = kmalloc(sizeof(*info), GFP_KERNEL); 
150
    if (!info) return -ENOMEM;
L
Linus Torvalds 已提交
151 152
    memset(info,0,sizeof(*info));
    dev = alloc_trdev(sizeof(struct tok_info));
153 154 155 156
    if (!dev) {
	kfree(info);
	return -ENOMEM;
    }
L
Linus Torvalds 已提交
157

158
    info->p_dev = link;
L
Linus Torvalds 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    link->priv = info;
    info->ti = netdev_priv(dev);

    link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
    link->io.NumPorts1 = 4;
    link->io.IOAddrLines = 16;
    link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
    link->irq.IRQInfo1 = IRQ_LEVEL_ID;
    link->irq.Handler = &tok_interrupt;
    link->conf.Attributes = CONF_ENABLE_IRQ;
    link->conf.IntType = INT_MEMORY_AND_IO;
    link->conf.Present = PRESENT_OPTION;

    link->irq.Instance = info->dev = dev;

174
    SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
L
Linus Torvalds 已提交
175

176
    return ibmtr_config(link);
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187
} /* ibmtr_attach */

/*======================================================================

    This deletes a driver "instance".  The device is de-registered
    with Card Services.  If it has been released, all local data
    structures are freed.  Otherwise, the structures will be freed
    when the device is released.

======================================================================*/

188
static void ibmtr_detach(struct pcmcia_device *link)
L
Linus Torvalds 已提交
189 190
{
    struct ibmtr_dev_t *info = link->priv;
191
    struct net_device *dev = info->dev;
192
     struct tok_info *ti = netdev_priv(dev);
L
Linus Torvalds 已提交
193 194

    DEBUG(0, "ibmtr_detach(0x%p)\n", link);
195 196 197 198 199 200
    
    /* 
     * When the card removal interrupt hits tok_interrupt(), 
     * bail out early, so we don't crash the machine 
     */
    ti->sram_phys |= 1;
L
Linus Torvalds 已提交
201

202
    if (link->dev_node)
L
Linus Torvalds 已提交
203
	unregister_netdev(dev);
204 205
    
    del_timer_sync(&(ti->tr_timer));
206 207

    ibmtr_release(link);
L
Linus Torvalds 已提交
208 209

    free_netdev(dev);
210
    kfree(info);
L
Linus Torvalds 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223
} /* ibmtr_detach */

/*======================================================================

    ibmtr_config() is scheduled to run after a CARD_INSERTION event
    is received, to configure the PCMCIA socket, and to make the
    token-ring device available to the system.

======================================================================*/

#define CS_CHECK(fn, ret) \
do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)

224
static int __devinit ibmtr_config(struct pcmcia_device *link)
L
Linus Torvalds 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
{
    ibmtr_dev_t *info = link->priv;
    struct net_device *dev = info->dev;
    struct tok_info *ti = netdev_priv(dev);
    win_req_t req;
    memreq_t mem;
    int i, last_ret, last_fn;

    DEBUG(0, "ibmtr_config(0x%p)\n", link);

    link->conf.ConfigIndex = 0x61;

    /* Determine if this is PRIMARY or ALTERNATE. */

    /* Try PRIMARY card at 0xA20-0xA23 */
    link->io.BasePort1 = 0xA20;
241
    i = pcmcia_request_io(link, &link->io);
L
Linus Torvalds 已提交
242 243 244
    if (i != CS_SUCCESS) {
	/* Couldn't get 0xA20-0xA23.  Try ALTERNATE at 0xA24-0xA27. */
	link->io.BasePort1 = 0xA24;
245
	CS_CHECK(RequestIO, pcmcia_request_io(link, &link->io));
L
Linus Torvalds 已提交
246 247 248
    }
    dev->base_addr = link->io.BasePort1;

249
    CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
L
Linus Torvalds 已提交
250 251 252 253 254 255 256 257 258 259
    dev->irq = link->irq.AssignedIRQ;
    ti->irq = link->irq.AssignedIRQ;
    ti->global_int_enable=GLOBAL_INT_ENABLE+((dev->irq==9) ? 2 : dev->irq);

    /* Allocate the MMIO memory window */
    req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
    req.Attributes |= WIN_USE_WAIT;
    req.Base = 0; 
    req.Size = 0x2000;
    req.AccessSpeed = 250;
260
    CS_CHECK(RequestWindow, pcmcia_request_window(&link, &req, &link->win));
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272

    mem.CardOffset = mmiobase;
    mem.Page = 0;
    CS_CHECK(MapMemPage, pcmcia_map_mem_page(link->win, &mem));
    ti->mmio = ioremap(req.Base, req.Size);

    /* Allocate the SRAM memory window */
    req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
    req.Attributes |= WIN_USE_WAIT;
    req.Base = 0;
    req.Size = sramsize * 1024;
    req.AccessSpeed = 250;
273
    CS_CHECK(RequestWindow, pcmcia_request_window(&link, &req, &info->sram_win_handle));
L
Linus Torvalds 已提交
274 275 276 277 278 279 280 281 282

    mem.CardOffset = srambase;
    mem.Page = 0;
    CS_CHECK(MapMemPage, pcmcia_map_mem_page(info->sram_win_handle, &mem));

    ti->sram_base = mem.CardOffset >> 12;
    ti->sram_virt = ioremap(req.Base, req.Size);
    ti->sram_phys = req.Base;

283
    CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link, &link->conf));
L
Linus Torvalds 已提交
284 285 286 287 288 289

    /*  Set up the Token-Ring Controller Configuration Register and
        turn on the card.  Check the "Local Area Network Credit Card
        Adapters Technical Reference"  SC30-3585 for this info.  */
    ibmtr_hw_setup(dev, mmiobase);

290
    link->dev_node = &info->node;
291
    SET_NETDEV_DEV(dev, &handle_to_dev(link));
L
Linus Torvalds 已提交
292 293 294 295

    i = ibmtr_probe_card(dev);
    if (i != 0) {
	printk(KERN_NOTICE "ibmtr_cs: register_netdev() failed\n");
296
	link->dev_node = NULL;
L
Linus Torvalds 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309
	goto failed;
    }

    strcpy(info->node.dev_name, dev->name);

    printk(KERN_INFO "%s: port %#3lx, irq %d,",
           dev->name, dev->base_addr, dev->irq);
    printk (" mmio %#5lx,", (u_long)ti->mmio);
    printk (" sram %#5lx,", (u_long)ti->sram_base << 12);
    printk ("\n" KERN_INFO "  hwaddr=");
    for (i = 0; i < TR_ALEN; i++)
        printk("%02X", dev->dev_addr[i]);
    printk("\n");
310
    return 0;
L
Linus Torvalds 已提交
311 312

cs_failed:
313
    cs_error(link, last_fn, last_ret);
L
Linus Torvalds 已提交
314 315
failed:
    ibmtr_release(link);
316
    return -ENODEV;
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326
} /* ibmtr_config */

/*======================================================================

    After a card is removed, ibmtr_release() will unregister the net
    device, and release the PCMCIA configuration.  If the device is
    still open, this will be postponed until it is closed.

======================================================================*/

327
static void ibmtr_release(struct pcmcia_device *link)
L
Linus Torvalds 已提交
328
{
329 330
	ibmtr_dev_t *info = link->priv;
	struct net_device *dev = info->dev;
L
Linus Torvalds 已提交
331

332
	DEBUG(0, "ibmtr_release(0x%p)\n", link);
L
Linus Torvalds 已提交
333

334 335 336 337 338
	if (link->win) {
		struct tok_info *ti = netdev_priv(dev);
		iounmap(ti->mmio);
		pcmcia_release_window(info->sram_win_handle);
	}
339
	pcmcia_disable_device(link);
L
Linus Torvalds 已提交
340 341
}

342
static int ibmtr_suspend(struct pcmcia_device *link)
343 344 345 346
{
	ibmtr_dev_t *info = link->priv;
	struct net_device *dev = info->dev;

347
	if (link->open)
348
		netif_device_detach(dev);
349 350 351 352

	return 0;
}

353
static int ibmtr_resume(struct pcmcia_device *link)
354 355 356 357
{
	ibmtr_dev_t *info = link->priv;
	struct net_device *dev = info->dev;

358
	if (link->open) {
359 360 361
		ibmtr_probe(dev);	/* really? */
		netif_device_attach(dev);
	}
362 363 364 365 366

	return 0;
}


L
Linus Torvalds 已提交
367 368 369 370 371 372 373 374 375 376 377 378 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 405 406 407 408 409
/*====================================================================*/

static void ibmtr_hw_setup(struct net_device *dev, u_int mmiobase)
{
    int i;

    /* Bizarre IBM behavior, there are 16 bits of information we
       need to set, but the card only allows us to send 4 bits at a 
       time.  For each byte sent to base_addr, bits 7-4 tell the
       card which part of the 16 bits we are setting, bits 3-0 contain 
       the actual information */

    /* First nibble provides 4 bits of mmio */
    i = (mmiobase >> 16) & 0x0F;
    outb(i, dev->base_addr);

    /* Second nibble provides 3 bits of mmio */
    i = 0x10 | ((mmiobase >> 12) & 0x0E);
    outb(i, dev->base_addr);

    /* Third nibble, hard-coded values */
    i = 0x26;
    outb(i, dev->base_addr);

    /* Fourth nibble sets shared ram page size */

    /* 8 = 00, 16 = 01, 32 = 10, 64 = 11 */          
    i = (sramsize >> 4) & 0x07;
    i = ((i == 4) ? 3 : i) << 2;
    i |= 0x30;

    if (ringspeed == 16)
	i |= 2;
    if (dev->base_addr == 0xA24)
	i |= 1;
    outb(i, dev->base_addr);

    /* 0x40 will release the card for use */
    outb(0x40, dev->base_addr);

    return;
}

410 411 412 413 414 415 416
static struct pcmcia_device_id ibmtr_ids[] = {
	PCMCIA_DEVICE_PROD_ID12("3Com", "TokenLink Velocity PC Card", 0x41240e5b, 0x82c3734e),
	PCMCIA_DEVICE_PROD_ID12("IBM", "TOKEN RING", 0xb569a6e5, 0xbf8eed47),
	PCMCIA_DEVICE_NULL,
};
MODULE_DEVICE_TABLE(pcmcia, ibmtr_ids);

L
Linus Torvalds 已提交
417 418 419 420 421
static struct pcmcia_driver ibmtr_cs_driver = {
	.owner		= THIS_MODULE,
	.drv		= {
		.name	= "ibmtr_cs",
	},
422
	.probe		= ibmtr_attach,
423
	.remove		= ibmtr_detach,
424
	.id_table       = ibmtr_ids,
425 426
	.suspend	= ibmtr_suspend,
	.resume		= ibmtr_resume,
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440
};

static int __init init_ibmtr_cs(void)
{
	return pcmcia_register_driver(&ibmtr_cs_driver);
}

static void __exit exit_ibmtr_cs(void)
{
	pcmcia_unregister_driver(&ibmtr_cs_driver);
}

module_init(init_ibmtr_cs);
module_exit(exit_ibmtr_cs);