ibmtr_cs.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:17k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /*======================================================================
  2.     A PCMCIA token-ring driver for IBM-based cards
  3.     This driver supports the IBM PCMCIA Token-Ring Card.
  4.     Written by Steve Kipisz, kipisz@vnet.ibm.com or
  5.                              bungy@ibm.net
  6.     Written 1995,1996.
  7.     This code is based on pcnet_cs.c from David Hinds.
  8.     
  9.     V2.2.0 February 1999 - Mike Phillips phillim@amtrak.com
  10.     Linux V2.2.x presented significant changes to the underlying
  11.     ibmtr.c code.  Mainly the code became a lot more organized and
  12.     modular.
  13.     This caused the old PCMCIA Token Ring driver to give up and go 
  14.     home early. Instead of just patching the old code to make it 
  15.     work, the PCMCIA code has been streamlined, updated and possibly
  16.     improved.
  17.     This code now only contains code required for the Card Services.
  18.     All we do here is set the card up enough so that the real ibmtr.c
  19.     driver can find it and work with it properly.
  20.     i.e. We set up the io port, irq, mmio memory and shared ram
  21.     memory.  This enables ibmtr_probe in ibmtr.c to find the card and
  22.     configure it as though it was a normal ISA and/or PnP card.
  23.     CHANGES
  24.     v2.2.5 April 1999 Mike Phillips (phillim@amtrak.com)
  25.     Obscure bug fix, required changed to ibmtr.c not ibmtr_cs.c
  26.     
  27.     v2.2.7 May 1999 Mike Phillips (phillim@amtrak.com)
  28.     Updated to version 2.2.7 to match the first version of the kernel
  29.     that the modification to ibmtr.c were incorporated into.
  30.     
  31.     v2.2.17 July 2000 Burt Silverman (burts@us.ibm.com)
  32.     Address translation feature of PCMCIA controller is usable so
  33.     memory windows can be placed in High memory (meaning above
  34.     0xFFFFF.)
  35. ======================================================================*/
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/sched.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/slab.h>
  41. #include <linux/string.h>
  42. #include <linux/timer.h>
  43. #include <linux/module.h>
  44. #include <asm/io.h>
  45. #include <asm/system.h>
  46. #include <linux/netdevice.h>
  47. #include <linux/trdevice.h>
  48. #include <linux/ibmtr.h>
  49. #include <pcmcia/version.h>
  50. #include <pcmcia/cs_types.h>
  51. #include <pcmcia/cs.h>
  52. #include <pcmcia/cistpl.h>
  53. #include <pcmcia/ds.h>
  54. #define PCMCIA
  55. #include "../tokenring/ibmtr.c"
  56. #ifdef PCMCIA_DEBUG
  57. static int pc_debug = PCMCIA_DEBUG;
  58. MODULE_PARM(pc_debug, "i");
  59. #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
  60. static char *version =
  61. "ibmtr_cs.c 1.10   1996/01/06 05:19:00 (Steve Kipisz)n"
  62. "           2.2.7  1999/05/03 12:00:00 (Mike Phillips)n"
  63. "           2.4.2  2001/30/28 Midnight (Burt Silverman)n";
  64. #else
  65. #define DEBUG(n, args...)
  66. #endif
  67. /*====================================================================*/
  68. /* Parameters that can be set with 'insmod' */
  69. /* Bit map of interrupts to choose from */
  70. static u_int irq_mask = 0xdeb8;
  71. static int irq_list[4] = { -1 };
  72. /* MMIO base address */
  73. static u_long mmiobase = 0xce000;
  74. /* SRAM base address */
  75. static u_long srambase = 0xd0000;
  76. /* SRAM size 8,16,32,64 */
  77. static u_long sramsize = 64;
  78. /* Ringspeed 4,16 */
  79. static int ringspeed = 16;
  80. MODULE_PARM(irq_mask, "i");
  81. MODULE_PARM(irq_list, "1-4i");
  82. MODULE_PARM(mmiobase, "i");
  83. MODULE_PARM(srambase, "i");
  84. MODULE_PARM(sramsize, "i");
  85. MODULE_PARM(ringspeed, "i");
  86. MODULE_LICENSE("GPL");
  87. /*====================================================================*/
  88. static void ibmtr_config(dev_link_t *link);
  89. static void ibmtr_hw_setup(struct net_device *dev, u_int mmiobase);
  90. static void ibmtr_release(u_long arg);
  91. static int ibmtr_event(event_t event, int priority,
  92.                        event_callback_args_t *args);
  93. static dev_info_t dev_info = "ibmtr_cs";
  94. static dev_link_t *ibmtr_attach(void);
  95. static void ibmtr_detach(dev_link_t *);
  96. static dev_link_t *dev_list;
  97. extern int ibmtr_probe(struct net_device *dev);
  98. extern int trdev_init(struct net_device *dev);
  99. extern void tok_interrupt (int irq, void *dev_id, struct pt_regs *regs);
  100. /*====================================================================*/
  101. typedef struct ibmtr_dev_t {
  102.     dev_link_t link;
  103.     struct net_device *dev;
  104.     dev_node_t          node;
  105.     window_handle_t     sram_win_handle;
  106.     struct tok_info ti;
  107. } ibmtr_dev_t;
  108. /*======================================================================
  109.     This bit of code is used to avoid unregistering network devices
  110.     at inappropriate times.  2.2 and later kernels are fairly picky
  111.     about when this can happen.
  112.     
  113. ======================================================================*/
  114. static void flush_stale_links(void)
  115. {
  116.     dev_link_t *link, *next;
  117.     for (link = dev_list; link; link = next) {
  118. next = link->next;
  119. if (link->state & DEV_STALE_LINK)
  120.     ibmtr_detach(link);
  121.     }
  122. }
  123. /*====================================================================*/
  124. static void cs_error(client_handle_t handle, int func, int ret)
  125. {
  126.     error_info_t err = { func, ret };
  127.     CardServices(ReportError, handle, &err);
  128. }
  129. /*======================================================================
  130.     ibmtr_attach() creates an "instance" of the driver, allocating
  131.     local data structures for one device.  The device is registered
  132.     with Card Services.
  133. ======================================================================*/
  134. static dev_link_t *ibmtr_attach(void)
  135. {
  136.     ibmtr_dev_t *info;
  137.     dev_link_t *link;
  138.     struct net_device *dev;
  139.     client_reg_t client_reg;
  140.     int i, ret;
  141.     
  142.     DEBUG(0, "ibmtr_attach()n");
  143.     flush_stale_links();
  144.     /* Create new token-ring device */
  145.     info = kmalloc(sizeof(*info), GFP_KERNEL);
  146.     if (!info) return NULL;
  147.     memset(info, 0, sizeof(*info));
  148.     link = &info->link; link->priv = info;
  149.     link->release.function = &ibmtr_release;
  150.     link->release.data = (u_long)link;
  151.     link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  152.     link->io.NumPorts1 = 4;
  153.     link->io.IOAddrLines = 16;
  154.     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  155.     link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
  156.     if (irq_list[0] == -1)
  157. link->irq.IRQInfo2 = irq_mask;
  158.     else
  159. for (i = 0; i < 4; i++)
  160.     link->irq.IRQInfo2 |= 1 << irq_list[i];
  161.     link->irq.Handler = &tok_interrupt;
  162.     link->conf.Attributes = CONF_ENABLE_IRQ;
  163.     link->conf.Vcc = 50;
  164.     link->conf.IntType = INT_MEMORY_AND_IO;
  165.     link->conf.Present = PRESENT_OPTION;
  166.     dev = init_trdev(NULL,0);
  167.     if (dev == NULL) {
  168. ibmtr_detach(link);
  169.         return NULL;
  170.     }
  171.     dev->priv = &info->ti;
  172.     link->irq.Instance = info->dev = dev;
  173.     
  174.     dev->init = &ibmtr_probe;
  175.     /* Register with Card Services */
  176.     link->next = dev_list;
  177.     dev_list = link;
  178.     client_reg.dev_info = &dev_info;
  179.     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
  180.     client_reg.EventMask =
  181.         CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
  182.         CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
  183.         CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
  184.     client_reg.event_handler = &ibmtr_event;
  185.     client_reg.Version = 0x0210;
  186.     client_reg.event_callback_args.client_data = link;
  187.     ret = CardServices(RegisterClient, &link->handle, &client_reg);
  188.     if (ret != 0) {
  189.         cs_error(link->handle, RegisterClient, ret);
  190.         ibmtr_detach(link);
  191.         return NULL;
  192.     }
  193.     return link;
  194. } /* ibmtr_attach */
  195. /*======================================================================
  196.     This deletes a driver "instance".  The device is de-registered
  197.     with Card Services.  If it has been released, all local data
  198.     structures are freed.  Otherwise, the structures will be freed
  199.     when the device is released.
  200. ======================================================================*/
  201. static void ibmtr_detach(dev_link_t *link)
  202. {
  203.     struct ibmtr_dev_t *info = link->priv;
  204.     dev_link_t **linkp;
  205.     struct net_device *dev;
  206.     DEBUG(0, "ibmtr_detach(0x%p)n", link);
  207.     /* Locate device structure */
  208.     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  209.         if (*linkp == link) break;
  210.     if (*linkp == NULL)
  211.         return;
  212.     dev = info->dev;
  213.     {
  214. struct tok_info *ti = (struct tok_info *)dev->priv;
  215. del_timer(&(ti->tr_timer));
  216.     }
  217.     del_timer(&link->release);
  218.     if (link->state & DEV_CONFIG) {
  219.         ibmtr_release((u_long)link);
  220.         if (link->state & DEV_STALE_CONFIG) {
  221.             link->state |= DEV_STALE_LINK;
  222.             return;
  223.         }
  224.     }
  225.     if (link->handle)
  226.         CardServices(DeregisterClient, link->handle);
  227.     /* Unlink device structure, free bits */
  228.     *linkp = link->next;
  229.     if (info->dev) {
  230. unregister_trdev(info->dev);
  231. kfree(info->dev);
  232.     }
  233.     kfree(info);
  234. } /* ibmtr_detach */
  235. /*======================================================================
  236.     ibmtr_config() is scheduled to run after a CARD_INSERTION event
  237.     is received, to configure the PCMCIA socket, and to make the
  238.     token-ring device available to the system.
  239. ======================================================================*/
  240. #define CS_CHECK(fn, args...) 
  241. while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
  242. static void ibmtr_config(dev_link_t *link)
  243. {
  244.     client_handle_t handle = link->handle;
  245.     ibmtr_dev_t *info = link->priv;
  246.     struct net_device *dev = info->dev;
  247.     struct tok_info *ti = dev->priv;
  248.     tuple_t tuple;
  249.     cisparse_t parse;
  250.     win_req_t req;
  251.     memreq_t mem;
  252.     int i, last_ret, last_fn;
  253.     u_char buf[64];
  254.     DEBUG(0, "ibmtr_config(0x%p)n", link);
  255.     tuple.Attributes = 0;
  256.     tuple.TupleData = buf;
  257.     tuple.TupleDataMax = 64;
  258.     tuple.TupleOffset = 0;
  259.     tuple.DesiredTuple = CISTPL_CONFIG;
  260.     CS_CHECK(GetFirstTuple, handle, &tuple);
  261.     CS_CHECK(GetTupleData, handle, &tuple);
  262.     CS_CHECK(ParseTuple, handle, &tuple, &parse);
  263.     link->conf.ConfigBase = parse.config.base;
  264.     /* Configure card */
  265.     link->state |= DEV_CONFIG;
  266.     link->conf.ConfigIndex = 0x61;
  267.     /* Determine if this is PRIMARY or ALTERNATE. */
  268.     /* Try PRIMARY card at 0xA20-0xA23 */
  269.     link->io.BasePort1 = 0xA20;
  270.     i = CardServices(RequestIO, link->handle, &link->io);
  271.     if (i == CS_SUCCESS) {
  272. memcpy(info->node.dev_name, "tr0", 4);
  273.     } else {
  274. /* Couldn't get 0xA20-0xA23.  Try ALTERNATE at 0xA24-0xA27. */
  275. link->io.BasePort1 = 0xA24;
  276. CS_CHECK(RequestIO, link->handle, &link->io);
  277. memcpy(info->node.dev_name, "tr1", 4);
  278.     }
  279.     dev->base_addr = link->io.BasePort1;
  280.     CS_CHECK(RequestIRQ, link->handle, &link->irq);
  281.     dev->irq = link->irq.AssignedIRQ;
  282.     ti->irq = link->irq.AssignedIRQ;
  283.     ti->global_int_enable=GLOBAL_INT_ENABLE+((dev->irq==9) ? 2 : dev->irq);
  284.     /* Allocate the MMIO memory window */
  285.     req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
  286.     req.Attributes |= WIN_USE_WAIT;
  287.     req.Base = 0; 
  288.     req.Size = 0x2000;
  289.     req.AccessSpeed = 250;
  290.     link->win = (window_handle_t)link->handle;
  291.     CS_CHECK(RequestWindow, &link->win, &req);
  292.     mem.CardOffset = mmiobase;
  293.     mem.Page = 0;
  294.     CS_CHECK(MapMemPage, link->win, &mem);
  295.     ti->mmio = ioremap(req.Base, req.Size);
  296.     /* Allocate the SRAM memory window */
  297.     req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
  298.     req.Attributes |= WIN_USE_WAIT;
  299.     req.Base = 0;
  300.     req.Size = sramsize * 1024;
  301.     req.AccessSpeed = 250;
  302.     info->sram_win_handle = (window_handle_t)link->handle;
  303.     CS_CHECK(RequestWindow, &info->sram_win_handle, &req);
  304.     mem.CardOffset = srambase;
  305.     mem.Page = 0;
  306.     CS_CHECK(MapMemPage, info->sram_win_handle, &mem);
  307.     ti->sram_base = mem.CardOffset >> 12;
  308.     ti->sram_virt = (u_long)ioremap(req.Base, req.Size);
  309.     CS_CHECK(RequestConfiguration, link->handle, &link->conf);
  310.     /*  Set up the Token-Ring Controller Configuration Register and
  311.         turn on the card.  Check the "Local Area Network Credit Card
  312.         Adapters Technical Reference"  SC30-3585 for this info.  */
  313.     ibmtr_hw_setup(dev, mmiobase);
  314.     i = register_trdev(dev);
  315.     
  316.     if (i != 0) {
  317. printk(KERN_NOTICE "ibmtr_cs: register_trdev() failedn");
  318. goto failed;
  319.     }
  320.     link->dev = &info->node;
  321.     link->state &= ~DEV_CONFIG_PENDING;
  322.     printk(KERN_INFO "%s: port %#3lx, irq %d,",
  323.            dev->name, dev->base_addr, dev->irq);
  324.     printk (" mmio %#5lx,", (u_long)ti->mmio);
  325.     printk (" sram %#5lx,", (u_long)ti->sram_base << 12);
  326.     printk ("n" KERN_INFO "  hwaddr=");
  327.     for (i = 0; i < TR_ALEN; i++)
  328.         printk("%02X", dev->dev_addr[i]);
  329.     printk("n");
  330.     return;
  331. cs_failed:
  332.     cs_error(link->handle, last_fn, last_ret);
  333. failed:
  334.     ibmtr_release((u_long)link);
  335. } /* ibmtr_config */
  336. /*======================================================================
  337.     After a card is removed, ibmtr_release() will unregister the net
  338.     device, and release the PCMCIA configuration.  If the device is
  339.     still open, this will be postponed until it is closed.
  340. ======================================================================*/
  341. static void ibmtr_release(u_long arg)
  342. {
  343.     dev_link_t *link = (dev_link_t *)arg;
  344.     ibmtr_dev_t *info = link->priv;
  345.     struct net_device *dev = info->dev;
  346.     DEBUG(0, "ibmtr_release(0x%p)n", link);
  347.     if (link->open) {
  348. DEBUG(1, "ibmtr_cs: release postponed, '%s' "
  349.       "still openn", info->node.dev_name);
  350.         link->state |= DEV_STALE_CONFIG;
  351.         return;
  352.     }
  353.     CardServices(ReleaseConfiguration, link->handle);
  354.     CardServices(ReleaseIO, link->handle, &link->io);
  355.     CardServices(ReleaseIRQ, link->handle, &link->irq);
  356.     if (link->win) {
  357. struct tok_info *ti = dev->priv;
  358. iounmap((void *)ti->mmio);
  359. CardServices(ReleaseWindow, link->win);
  360. CardServices(ReleaseWindow, info->sram_win_handle);
  361.     }
  362.     link->state &= ~DEV_CONFIG;
  363. } /* ibmtr_release */
  364. /*======================================================================
  365.     The card status event handler.  Mostly, this schedules other
  366.     stuff to run after an event is received.  A CARD_REMOVAL event
  367.     also sets some flags to discourage the net drivers from trying
  368.     to talk to the card any more.
  369. ======================================================================*/
  370. static int ibmtr_event(event_t event, int priority,
  371.                        event_callback_args_t *args)
  372. {
  373.     dev_link_t *link = args->client_data;
  374.     ibmtr_dev_t *info = link->priv;
  375.     struct net_device *dev = info->dev;
  376.     DEBUG(1, "ibmtr_event(0x%06x)n", event);
  377.     switch (event) {
  378.     case CS_EVENT_CARD_REMOVAL:
  379.         link->state &= ~DEV_PRESENT;
  380.         if (link->state & DEV_CONFIG) {
  381.     /* set flag to bypass normal interrupt code */
  382.     ((struct tok_info *)dev->priv)->sram_virt |= 1;
  383.     netif_device_detach(dev);
  384.     mod_timer(&link->release, jiffies + HZ/20);
  385.         }
  386.         break;
  387.     case CS_EVENT_CARD_INSERTION:
  388.         link->state |= DEV_PRESENT;
  389. ibmtr_config(link);
  390. break;
  391.     case CS_EVENT_PM_SUSPEND:
  392.         link->state |= DEV_SUSPEND;
  393.         /* Fall through... */
  394.     case CS_EVENT_RESET_PHYSICAL:
  395.         if (link->state & DEV_CONFIG) {
  396.             if (link->open)
  397. netif_device_detach(dev);
  398.             CardServices(ReleaseConfiguration, link->handle);
  399.         }
  400.         break;
  401.     case CS_EVENT_PM_RESUME:
  402.         link->state &= ~DEV_SUSPEND;
  403.         /* Fall through... */
  404.     case CS_EVENT_CARD_RESET:
  405.         if (link->state & DEV_CONFIG) {
  406.             CardServices(RequestConfiguration, link->handle, &link->conf);
  407.             if (link->open) {
  408. (dev->init)(dev);
  409. netif_device_attach(dev);
  410.             }
  411.         }
  412.         break;
  413.     }
  414.     return 0;
  415. } /* ibmtr_event */
  416. /*====================================================================*/
  417. static void ibmtr_hw_setup(struct net_device *dev, u_int mmiobase)
  418. {
  419.     int i;
  420.     /* Bizarre IBM behavior, there are 16 bits of information we
  421.        need to set, but the card only allows us to send 4 bits at a 
  422.        time.  For each byte sent to base_addr, bits 7-4 tell the
  423.        card which part of the 16 bits we are setting, bits 3-0 contain 
  424.        the actual information */
  425.     /* First nibble provides 4 bits of mmio */
  426.     i = (mmiobase >> 16) & 0x0F;
  427.     outb(i, dev->base_addr);
  428.     /* Second nibble provides 3 bits of mmio */
  429.     i = 0x10 | ((mmiobase >> 12) & 0x0E);
  430.     outb(i, dev->base_addr);
  431.     /* Third nibble, hard-coded values */
  432.     i = 0x26;
  433.     outb(i, dev->base_addr);
  434.     /* Fourth nibble sets shared ram page size */
  435.     /* 8 = 00, 16 = 01, 32 = 10, 64 = 11 */          
  436.     i = (sramsize >> 4) & 0x07;
  437.     i = ((i == 4) ? 3 : i) << 2;
  438.     i |= 0x30;
  439.     if (ringspeed == 16)
  440. i |= 2;
  441.     if (dev->base_addr == 0xA24)
  442. i |= 1;
  443.     outb(i, dev->base_addr);
  444.     /* 0x40 will release the card for use */
  445.     outb(0x40, dev->base_addr);
  446.     return;
  447. }
  448. /*====================================================================*/
  449. static int __init init_ibmtr_cs(void)
  450. {
  451.     servinfo_t serv;
  452.     DEBUG(0, "%s", version);
  453.     CardServices(GetCardServicesInfo, &serv);
  454.     if (serv.Revision != CS_RELEASE_CODE) {
  455.         printk(KERN_NOTICE "ibmtr_cs: Card Services release "
  456.        "does not match!n");
  457.         return -1;
  458.     }
  459.     register_pccard_driver(&dev_info, &ibmtr_attach, &ibmtr_detach);
  460.     return 0;
  461. }
  462. static void __exit exit_ibmtr_cs(void)
  463. {
  464.     DEBUG(0, "ibmtr_cs: unloadingn");
  465.     unregister_pccard_driver(&dev_info);
  466.     while (dev_list != NULL)
  467.         ibmtr_detach(dev_list);
  468. }
  469. module_init(init_ibmtr_cs);
  470. module_exit(exit_ibmtr_cs);