blob: 5fdde69464276bfd168d6aa207fa2fcaed529acf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux ARCnet driver - COM20020 PCMCIA support
Joe Perchescb334642015-05-05 10:05:47 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written 1994-1999 by Avery Pennarun,
5 * based on an ISA version by David Woodhouse.
6 * Derived from ibmtr_cs.c by Steve Kipisz (pcmcia-cs 3.1.4)
7 * which was derived from pcnet_cs.c by David Hinds.
8 * Some additional portions derived from skeleton.c by Donald Becker.
9 *
10 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
11 * for sponsoring the further development of this driver.
12 *
13 * **********************
14 *
15 * The original copyright of skeleton.c was as follows:
16 *
17 * skeleton.c Written 1993 by Donald Becker.
18 * Copyright 1993 United States Government as represented by the
19 * Director, National Security Agency. This software may only be used
20 * and distributed according to the terms of the GNU General Public License as
21 * modified by SRC, incorporated herein by reference.
Joe Perchescb334642015-05-05 10:05:47 -070022 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * **********************
24 * Changes:
25 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 08/08/2000
26 * - reorganize kmallocs in com20020_attach, checking all for failure
27 * and releasing the previous allocations if one fails
28 * **********************
Joe Perchescb334642015-05-05 10:05:47 -070029 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * For more details, see drivers/net/arcnet.c
31 *
32 * **********************
33 */
Joe Perches05a24b22015-05-05 10:05:56 -070034
35#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ptrace.h>
39#include <linux/slab.h>
40#include <linux/string.h>
41#include <linux/timer.h>
42#include <linux/delay.h>
43#include <linux/module.h>
44#include <linux/netdevice.h>
Joe Perches26c6d282015-05-05 10:06:03 -070045#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <pcmcia/cistpl.h>
47#include <pcmcia/ds.h>
48
Joe Perches26c6d282015-05-05 10:06:03 -070049#include "arcdevice.h"
50#include "com20020.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static void regdump(struct net_device *dev)
53{
Joe Perches636b8112010-08-12 12:22:51 +000054#ifdef DEBUG
Joe Perchescb334642015-05-05 10:05:47 -070055 int ioaddr = dev->base_addr;
56 int count;
57
58 netdev_dbg(dev, "register dump:\n");
Joe Perches7f5e7602015-05-05 10:05:49 -070059 for (count = ioaddr; count < ioaddr + 16; count++) {
Joe Perchescb334642015-05-05 10:05:47 -070060 if (!(count % 16))
61 pr_cont("%04X:", count);
62 pr_cont(" %02X", inb(count));
63 }
64 pr_cont("\n");
65
66 netdev_dbg(dev, "buffer0 dump:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 /* set up the address register */
Joe Perchescb334642015-05-05 10:05:47 -070068 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 outb((count >> 8) | RDDATAflag | AUTOINCflag, _ADDR_HI);
70 outb(count & 0xff, _ADDR_LO);
Joe Perchescb334642015-05-05 10:05:47 -070071
Joe Perches7f5e7602015-05-05 10:05:49 -070072 for (count = 0; count < 256 + 32; count++) {
Joe Perchescb334642015-05-05 10:05:47 -070073 if (!(count % 16))
74 pr_cont("%04X:", count);
75
76 /* copy the data */
77 pr_cont(" %02X", inb(_MEMDATA));
78 }
79 pr_cont("\n");
Joe Perches636b8112010-08-12 12:22:51 +000080#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*====================================================================*/
84
85/* Parameters that can be set with 'insmod' */
86
87static int node;
88static int timeout = 3;
89static int backplane;
90static int clockp;
91static int clockm;
92
93module_param(node, int, 0);
94module_param(timeout, int, 0);
95module_param(backplane, int, 0);
96module_param(clockp, int, 0);
97module_param(clockm, int, 0);
98
99MODULE_LICENSE("GPL");
100
101/*====================================================================*/
102
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200103static int com20020_config(struct pcmcia_device *link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200104static void com20020_release(struct pcmcia_device *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100106static void com20020_detach(struct pcmcia_device *p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108/*====================================================================*/
109
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200110static int com20020_probe(struct pcmcia_device *p_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Joe Perchescb334642015-05-05 10:05:47 -0700112 struct com20020_dev *info;
113 struct net_device *dev;
114 struct arcnet_local *lp;
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100115
Joe Perchescb334642015-05-05 10:05:47 -0700116 dev_dbg(&p_dev->dev, "com20020_attach()\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Joe Perchescb334642015-05-05 10:05:47 -0700118 /* Create new network device */
119 info = kzalloc(sizeof(*info), GFP_KERNEL);
120 if (!info)
121 goto fail_alloc_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Joe Perchescb334642015-05-05 10:05:47 -0700123 dev = alloc_arcdev("");
124 if (!dev)
125 goto fail_alloc_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Joe Perchescb334642015-05-05 10:05:47 -0700127 lp = netdev_priv(dev);
128 lp->timeout = timeout;
129 lp->backplane = backplane;
130 lp->clockp = clockp;
131 lp->clockm = clockm & 3;
132 lp->hw.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Joe Perchescb334642015-05-05 10:05:47 -0700134 /* fill in our module parameters as defaults */
135 dev->dev_addr[0] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Joe Perchescb334642015-05-05 10:05:47 -0700137 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
138 p_dev->resource[0]->end = 16;
139 p_dev->config_flags |= CONF_ENABLE_IRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Joe Perchescb334642015-05-05 10:05:47 -0700141 info->dev = dev;
142 p_dev->priv = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Joe Perchescb334642015-05-05 10:05:47 -0700144 return com20020_config(p_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146fail_alloc_dev:
Joe Perchescb334642015-05-05 10:05:47 -0700147 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148fail_alloc_info:
Joe Perchescb334642015-05-05 10:05:47 -0700149 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150} /* com20020_attach */
151
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200152static void com20020_detach(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Joe Perchescb334642015-05-05 10:05:47 -0700154 struct com20020_dev *info = link->priv;
155 struct net_device *dev = info->dev;
Dominik Brodowskib4635812005-11-14 21:25:35 +0100156
Joe Perchescb334642015-05-05 10:05:47 -0700157 dev_dbg(&link->dev, "detach...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Joe Perchescb334642015-05-05 10:05:47 -0700159 dev_dbg(&link->dev, "com20020_detach\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Joe Perchescb334642015-05-05 10:05:47 -0700161 dev_dbg(&link->dev, "unregister...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Joe Perchescb334642015-05-05 10:05:47 -0700163 unregister_netdev(dev);
Dominik Brodowskib4635812005-11-14 21:25:35 +0100164
Joe Perchesf2f0a162015-05-05 10:05:52 -0700165 /* this is necessary because we register our IRQ separately
Joe Perchescb334642015-05-05 10:05:47 -0700166 * from card services.
167 */
168 if (dev->irq)
169 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Joe Perchescb334642015-05-05 10:05:47 -0700171 com20020_release(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Joe Perchescb334642015-05-05 10:05:47 -0700173 /* Unlink device structure, free bits */
174 dev_dbg(&link->dev, "unlinking...\n");
Joe Perches7f5e7602015-05-05 10:05:49 -0700175 if (link->priv) {
Joe Perchescb334642015-05-05 10:05:47 -0700176 dev = info->dev;
Joe Perches7f5e7602015-05-05 10:05:49 -0700177 if (dev) {
Joe Perchescb334642015-05-05 10:05:47 -0700178 dev_dbg(&link->dev, "kfree...\n");
179 free_netdev(dev);
180 }
181 dev_dbg(&link->dev, "kfree2...\n");
182 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185} /* com20020_detach */
186
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200187static int com20020_config(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Joe Perchescb334642015-05-05 10:05:47 -0700189 struct arcnet_local *lp;
190 struct com20020_dev *info;
191 struct net_device *dev;
192 int i, ret;
193 int ioaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Joe Perchescb334642015-05-05 10:05:47 -0700195 info = link->priv;
196 dev = info->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Joe Perchescb334642015-05-05 10:05:47 -0700198 dev_dbg(&link->dev, "config...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Joe Perchescb334642015-05-05 10:05:47 -0700200 dev_dbg(&link->dev, "com20020_config\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Joe Perchescb334642015-05-05 10:05:47 -0700202 dev_dbg(&link->dev, "baseport1 is %Xh\n",
203 (unsigned int)link->resource[0]->start);
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200204
Joe Perchescb334642015-05-05 10:05:47 -0700205 i = -ENODEV;
206 link->io_lines = 16;
Dominik Brodowski90abdc32010-07-24 17:23:51 +0200207
Joe Perches7f5e7602015-05-05 10:05:49 -0700208 if (!link->resource[0]->start) {
209 for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x10) {
Joe Perchescb334642015-05-05 10:05:47 -0700210 link->resource[0]->start = ioaddr;
211 i = pcmcia_request_io(link);
212 if (i == 0)
213 break;
214 }
Joe Perches7f5e7602015-05-05 10:05:49 -0700215 } else {
Joe Perchescb334642015-05-05 10:05:47 -0700216 i = pcmcia_request_io(link);
Joe Perches7f5e7602015-05-05 10:05:49 -0700217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Joe Perches7f5e7602015-05-05 10:05:49 -0700219 if (i != 0) {
Joe Perchescb334642015-05-05 10:05:47 -0700220 dev_dbg(&link->dev, "requestIO failed totally!\n");
221 goto failed;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Joe Perchescb334642015-05-05 10:05:47 -0700224 ioaddr = dev->base_addr = link->resource[0]->start;
225 dev_dbg(&link->dev, "got ioaddr %Xh\n", ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Joe Perchescb334642015-05-05 10:05:47 -0700227 dev_dbg(&link->dev, "request IRQ %d\n",
228 link->irq);
Joe Perches7f5e7602015-05-05 10:05:49 -0700229 if (!link->irq) {
Joe Perchescb334642015-05-05 10:05:47 -0700230 dev_dbg(&link->dev, "requestIRQ failed totally!\n");
231 goto failed;
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Joe Perchescb334642015-05-05 10:05:47 -0700234 dev->irq = link->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Joe Perchescb334642015-05-05 10:05:47 -0700236 ret = pcmcia_enable_device(link);
237 if (ret)
238 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Joe Perches7f5e7602015-05-05 10:05:49 -0700240 if (com20020_check(dev)) {
Joe Perchescb334642015-05-05 10:05:47 -0700241 regdump(dev);
242 goto failed;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Joe Perchescb334642015-05-05 10:05:47 -0700245 lp = netdev_priv(dev);
246 lp->card_name = "PCMCIA COM20020";
247 lp->card_flags = ARC_CAN_10MBIT; /* pretend all of them can 10Mbit */
248
249 SET_NETDEV_DEV(dev, &link->dev);
250
251 i = com20020_found(dev, 0); /* calls register_netdev */
252
253 if (i != 0) {
254 dev_notice(&link->dev,
255 "com20020_found() failed\n");
256 goto failed;
257 }
258
259 netdev_dbg(dev, "port %#3lx, irq %d\n",
260 dev->base_addr, dev->irq);
261 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263failed:
Joe Perchescb334642015-05-05 10:05:47 -0700264 dev_dbg(&link->dev, "com20020_config failed...\n");
265 com20020_release(link);
266 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267} /* com20020_config */
268
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200269static void com20020_release(struct pcmcia_device *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Dominik Brodowskidd0fab52009-10-24 15:51:05 +0200271 dev_dbg(&link->dev, "com20020_release\n");
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200272 pcmcia_disable_device(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200275static int com20020_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100276{
Himangi Saraogi2dfd2532014-08-09 21:37:18 +0530277 struct com20020_dev *info = link->priv;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100278 struct net_device *dev = info->dev;
279
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100280 if (link->open)
Dominik Brodowski8661bb52006-03-02 00:02:33 +0100281 netif_device_detach(dev);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100282
283 return 0;
284}
285
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200286static int com20020_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100287{
Himangi Saraogi2dfd2532014-08-09 21:37:18 +0530288 struct com20020_dev *info = link->priv;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100289 struct net_device *dev = info->dev;
290
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100291 if (link->open) {
Dominik Brodowski8661bb52006-03-02 00:02:33 +0100292 int ioaddr = dev->base_addr;
Wang Chen4cf16532008-11-12 23:38:14 -0800293 struct arcnet_local *lp = netdev_priv(dev);
Joe Perches01a1d5a2015-05-05 10:05:48 -0700294
Dominik Brodowski8661bb52006-03-02 00:02:33 +0100295 ARCRESET;
296 }
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100297
298 return 0;
299}
300
Joe Perches25f8f542011-05-03 19:29:01 -0700301static const struct pcmcia_device_id com20020_ids[] = {
Marc Sowen6bb1c392006-06-25 01:56:24 -0700302 PCMCIA_DEVICE_PROD_ID12("Contemporary Control Systems, Inc.",
Joe Perchescb334642015-05-05 10:05:47 -0700303 "PCM20 Arcnet Adapter", 0x59991666, 0x95dfffaf),
Marc Sowen6bb1c392006-06-25 01:56:24 -0700304 PCMCIA_DEVICE_PROD_ID12("SoHard AG",
Joe Perchescb334642015-05-05 10:05:47 -0700305 "SH ARC PCMCIA", 0xf8991729, 0x69dff0c7),
Dominik Brodowski7fb22bb2005-06-27 16:28:38 -0700306 PCMCIA_DEVICE_NULL
307};
308MODULE_DEVICE_TABLE(pcmcia, com20020_ids);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310static struct pcmcia_driver com20020_cs_driver = {
311 .owner = THIS_MODULE,
Dominik Brodowski2e9b9812010-08-08 11:36:26 +0200312 .name = "com20020_cs",
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200313 .probe = com20020_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100314 .remove = com20020_detach,
Dominik Brodowski7fb22bb2005-06-27 16:28:38 -0700315 .id_table = com20020_ids,
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100316 .suspend = com20020_suspend,
317 .resume = com20020_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318};
H Hartley Sweetenfdd3f292013-03-06 11:27:43 -0700319module_pcmcia_driver(com20020_cs_driver);