xref: /linux/drivers/net/arcnet/com20020-pci.c (revision 90e63d5354951d37fa2b3b91e6f17b95d2bf9bee)
1 /*
2  * Linux ARCnet driver - COM20020 PCI support
3  * Contemporary Controls PCI20 and SOHARD SH-ARC PCI
4  *
5  * Written 1994-1999 by Avery Pennarun,
6  *    based on an ISA version by David Woodhouse.
7  * Written 1999-2000 by Martin Mares <mj@ucw.cz>.
8  * 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.
22  *
23  * **********************
24  *
25  * For more details, see drivers/net/arcnet.c
26  *
27  * **********************
28  */
29 
30 #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
31 
32 #include <linux/module.h>
33 #include <linux/moduleparam.h>
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/ioport.h>
37 #include <linux/errno.h>
38 #include <linux/netdevice.h>
39 #include <linux/init.h>
40 #include <linux/interrupt.h>
41 #include <linux/pci.h>
42 #include <linux/list.h>
43 #include <linux/io.h>
44 #include <linux/leds.h>
45 
46 #include "arcdevice.h"
47 #include "com20020.h"
48 
49 /* Module parameters */
50 
51 static int node;
52 static char device[9];		/* use eg. device="arc1" to change name */
53 static int timeout = 3;
54 static int backplane;
55 static int clockp;
56 static int clockm;
57 
58 module_param(node, int, 0);
59 module_param_string(device, device, sizeof(device), 0);
60 module_param(timeout, int, 0);
61 module_param(backplane, int, 0);
62 module_param(clockp, int, 0);
63 module_param(clockm, int, 0);
64 MODULE_DESCRIPTION("ARCnet COM20020 chipset PCI driver");
65 MODULE_LICENSE("GPL");
66 
67 static void led_tx_set(struct led_classdev *led_cdev,
68 			     enum led_brightness value)
69 {
70 	struct com20020_dev *card;
71 	struct com20020_priv *priv;
72 	struct com20020_pci_card_info *ci;
73 
74 	card = container_of(led_cdev, struct com20020_dev, tx_led);
75 
76 	priv = card->pci_priv;
77 	ci = priv->ci;
78 
79 	outb(!!value, priv->misc + ci->leds[card->index].green);
80 }
81 
82 static void led_recon_set(struct led_classdev *led_cdev,
83 			     enum led_brightness value)
84 {
85 	struct com20020_dev *card;
86 	struct com20020_priv *priv;
87 	struct com20020_pci_card_info *ci;
88 
89 	card = container_of(led_cdev, struct com20020_dev, recon_led);
90 
91 	priv = card->pci_priv;
92 	ci = priv->ci;
93 
94 	outb(!!value, priv->misc + ci->leds[card->index].red);
95 }
96 
97 static ssize_t backplane_mode_show(struct device *dev,
98 				   struct device_attribute *attr,
99 				   char *buf)
100 {
101 	struct net_device *net_dev = to_net_dev(dev);
102 	struct arcnet_local *lp = netdev_priv(net_dev);
103 
104 	return sprintf(buf, "%s\n", lp->backplane ? "true" : "false");
105 }
106 static DEVICE_ATTR_RO(backplane_mode);
107 
108 static struct attribute *com20020_state_attrs[] = {
109 	&dev_attr_backplane_mode.attr,
110 	NULL,
111 };
112 
113 static const struct attribute_group com20020_state_group = {
114 	.name = NULL,
115 	.attrs = com20020_state_attrs,
116 };
117 
118 static struct com20020_pci_card_info card_info_2p5mbit;
119 
120 static void com20020pci_remove(struct pci_dev *pdev);
121 
122 static int com20020pci_probe(struct pci_dev *pdev,
123 			     const struct pci_device_id *id)
124 {
125 	struct com20020_pci_card_info *ci;
126 	struct com20020_pci_channel_map *mm;
127 	struct net_device *dev;
128 	struct arcnet_local *lp;
129 	struct com20020_priv *priv;
130 	int ioaddr, ret;
131 	u8 i;
132 	struct resource *r;
133 
134 	ret = 0;
135 
136 	if (pci_enable_device(pdev))
137 		return -EIO;
138 
139 	priv = devm_kzalloc(&pdev->dev, sizeof(struct com20020_priv),
140 			    GFP_KERNEL);
141 	if (!priv)
142 		return -ENOMEM;
143 
144 	ci = (struct com20020_pci_card_info *)id->driver_data;
145 	if (!ci)
146 		ci = &card_info_2p5mbit;
147 
148 	priv->ci = ci;
149 	mm = &ci->misc_map;
150 
151 	pci_set_drvdata(pdev, priv);
152 
153 	INIT_LIST_HEAD(&priv->list_dev);
154 
155 	if (mm->size) {
156 		ioaddr = pci_resource_start(pdev, mm->bar) + mm->offset;
157 		r = devm_request_region(&pdev->dev, ioaddr, mm->size,
158 					"com20020-pci");
159 		if (!r) {
160 			pr_err("IO region %xh-%xh already allocated.\n",
161 			       ioaddr, ioaddr + mm->size - 1);
162 			return -EBUSY;
163 		}
164 		priv->misc = ioaddr;
165 	}
166 
167 	for (i = 0; i < ci->devcount; i++) {
168 		struct com20020_pci_channel_map *cm = &ci->chan_map_tbl[i];
169 		struct com20020_dev *card;
170 		int dev_id_mask = 0xf;
171 
172 		dev = alloc_arcdev(device);
173 		if (!dev) {
174 			ret = -ENOMEM;
175 			break;
176 		}
177 		dev->dev_port = i;
178 
179 		dev->netdev_ops = &com20020_netdev_ops;
180 
181 		lp = netdev_priv(dev);
182 
183 		arc_printk(D_NORMAL, dev, "%s Controls\n", ci->name);
184 		ioaddr = pci_resource_start(pdev, cm->bar) + cm->offset;
185 
186 		r = devm_request_region(&pdev->dev, ioaddr, cm->size,
187 					"com20020-pci");
188 		if (!r) {
189 			pr_err("IO region %xh-%xh already allocated\n",
190 			       ioaddr, ioaddr + cm->size - 1);
191 			ret = -EBUSY;
192 			goto err_free_arcdev;
193 		}
194 
195 		/* Dummy access after Reset
196 		 * ARCNET controller needs
197 		 * this access to detect bustype
198 		 */
199 		outb(0x00, ioaddr + COM20020_REG_W_COMMAND);
200 		inb(ioaddr + COM20020_REG_R_DIAGSTAT);
201 
202 		SET_NETDEV_DEV(dev, &pdev->dev);
203 		dev->base_addr = ioaddr;
204 		arcnet_set_addr(dev, node);
205 		dev->sysfs_groups[0] = &com20020_state_group;
206 		dev->irq = pdev->irq;
207 		lp->card_name = "PCI COM20020";
208 		lp->card_flags = ci->flags;
209 		lp->backplane = backplane;
210 		lp->clockp = clockp & 7;
211 		lp->clockm = clockm & 3;
212 		lp->timeout = timeout;
213 		lp->hw.owner = THIS_MODULE;
214 
215 		lp->backplane = (inb(priv->misc) >> (2 + i)) & 0x1;
216 
217 		if (!strncmp(ci->name, "EAE PLX-PCI FB2", 15))
218 			lp->backplane = 1;
219 
220 		if (ci->flags & ARC_HAS_ROTARY) {
221 			/* Get the dev_id from the PLX rotary coder */
222 			if (!strncmp(ci->name, "EAE PLX-PCI MA1", 15))
223 				dev_id_mask = 0x3;
224 			dev->dev_id = (inb(priv->misc + ci->rotary) >> 4) & dev_id_mask;
225 			snprintf(dev->name, sizeof(dev->name), "arc%d-%d", dev->dev_id, i);
226 		}
227 
228 		if (inb(ioaddr + COM20020_REG_R_STATUS) == 0xFF) {
229 			pr_err("IO address %Xh is empty!\n", ioaddr);
230 			ret = -EIO;
231 			goto err_free_arcdev;
232 		}
233 		if (com20020_check(dev)) {
234 			ret = -EIO;
235 			goto err_free_arcdev;
236 		}
237 
238 		ret = com20020_found(dev, IRQF_SHARED);
239 		if (ret)
240 			goto err_free_arcdev;
241 
242 		card = devm_kzalloc(&pdev->dev, sizeof(struct com20020_dev),
243 				    GFP_KERNEL);
244 		if (!card) {
245 			ret = -ENOMEM;
246 			goto err_free_arcdev;
247 		}
248 
249 		card->index = i;
250 		card->pci_priv = priv;
251 
252 		if (ci->flags & ARC_HAS_LED) {
253 			card->tx_led.brightness_set = led_tx_set;
254 			card->tx_led.default_trigger = devm_kasprintf(&pdev->dev,
255 							GFP_KERNEL, "arc%d-%d-tx",
256 							dev->dev_id, i);
257 			if (!card->tx_led.default_trigger) {
258 				ret = -ENOMEM;
259 				goto err_free_arcdev;
260 			}
261 			card->tx_led.name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
262 							"pci:green:tx:%d-%d",
263 							dev->dev_id, i);
264 			if (!card->tx_led.name) {
265 				ret = -ENOMEM;
266 				goto err_free_arcdev;
267 			}
268 			card->tx_led.dev = &dev->dev;
269 			card->recon_led.brightness_set = led_recon_set;
270 			card->recon_led.default_trigger = devm_kasprintf(&pdev->dev,
271 							GFP_KERNEL, "arc%d-%d-recon",
272 							dev->dev_id, i);
273 			if (!card->recon_led.default_trigger) {
274 				ret = -ENOMEM;
275 				goto err_free_arcdev;
276 			}
277 			card->recon_led.name = devm_kasprintf(&pdev->dev, GFP_KERNEL,
278 							"pci:red:recon:%d-%d",
279 							dev->dev_id, i);
280 			if (!card->recon_led.name) {
281 				ret = -ENOMEM;
282 				goto err_free_arcdev;
283 			}
284 			card->recon_led.dev = &dev->dev;
285 
286 			ret = devm_led_classdev_register(&pdev->dev, &card->tx_led);
287 			if (ret)
288 				goto err_free_arcdev;
289 
290 			ret = devm_led_classdev_register(&pdev->dev, &card->recon_led);
291 			if (ret)
292 				goto err_free_arcdev;
293 
294 			dev_set_drvdata(&dev->dev, card);
295 			devm_arcnet_led_init(dev, dev->dev_id, i);
296 		}
297 
298 		card->dev = dev;
299 		list_add(&card->list, &priv->list_dev);
300 		continue;
301 
302 err_free_arcdev:
303 		free_arcdev(dev);
304 		break;
305 	}
306 	if (ret)
307 		com20020pci_remove(pdev);
308 	return ret;
309 }
310 
311 static void com20020pci_remove(struct pci_dev *pdev)
312 {
313 	struct com20020_dev *card, *tmpcard;
314 	struct com20020_priv *priv;
315 
316 	priv = pci_get_drvdata(pdev);
317 
318 	list_for_each_entry_safe(card, tmpcard, &priv->list_dev, list) {
319 		struct net_device *dev = card->dev;
320 
321 		unregister_netdev(dev);
322 		free_irq(dev->irq, dev);
323 		free_arcdev(dev);
324 	}
325 }
326 
327 static struct com20020_pci_card_info card_info_10mbit = {
328 	.name = "ARC-PCI",
329 	.devcount = 1,
330 	.chan_map_tbl = {
331 		{
332 			.bar = 2,
333 			.offset = 0x00,
334 			.size = 0x08,
335 		},
336 	},
337 	.flags = ARC_CAN_10MBIT,
338 };
339 
340 static struct com20020_pci_card_info card_info_5mbit = {
341 	.name = "ARC-PCI",
342 	.devcount = 1,
343 	.chan_map_tbl = {
344 		{
345 			.bar = 2,
346 			.offset = 0x00,
347 			.size = 0x08,
348 		},
349 	},
350 	.flags = ARC_IS_5MBIT,
351 };
352 
353 static struct com20020_pci_card_info card_info_2p5mbit = {
354 	.name = "ARC-PCI",
355 	.devcount = 1,
356 	.chan_map_tbl = {
357 		{
358 			.bar = 2,
359 			.offset = 0x00,
360 			.size = 0x08,
361 		},
362 	},
363 };
364 
365 static struct com20020_pci_card_info card_info_sohard = {
366 	.name = "SOHARD SH ARC-PCI",
367 	.devcount = 1,
368 	/* SOHARD needs PCI base addr 4 */
369 	.chan_map_tbl = {
370 		{
371 			.bar = 4,
372 			.offset = 0x00,
373 			.size = 0x08
374 		},
375 	},
376 	.flags = ARC_CAN_10MBIT,
377 };
378 
379 static struct com20020_pci_card_info card_info_eae_arc1 = {
380 	.name = "EAE PLX-PCI ARC1",
381 	.devcount = 1,
382 	.chan_map_tbl = {
383 		{
384 			.bar = 2,
385 			.offset = 0x00,
386 			.size = 0x08,
387 		},
388 	},
389 	.misc_map = {
390 		.bar = 2,
391 		.offset = 0x10,
392 		.size = 0x04,
393 	},
394 	.leds = {
395 		{
396 			.green = 0x0,
397 			.red = 0x1,
398 		},
399 	},
400 	.rotary = 0x0,
401 	.flags = ARC_HAS_ROTARY | ARC_HAS_LED | ARC_CAN_10MBIT,
402 };
403 
404 static struct com20020_pci_card_info card_info_eae_ma1 = {
405 	.name = "EAE PLX-PCI MA1",
406 	.devcount = 2,
407 	.chan_map_tbl = {
408 		{
409 			.bar = 2,
410 			.offset = 0x00,
411 			.size = 0x08,
412 		}, {
413 			.bar = 2,
414 			.offset = 0x08,
415 			.size = 0x08,
416 		}
417 	},
418 	.misc_map = {
419 		.bar = 2,
420 		.offset = 0x10,
421 		.size = 0x04,
422 	},
423 	.leds = {
424 		{
425 			.green = 0x0,
426 			.red = 0x1,
427 		}, {
428 			.green = 0x2,
429 			.red = 0x3,
430 		},
431 	},
432 	.rotary = 0x0,
433 	.flags = ARC_HAS_ROTARY | ARC_HAS_LED | ARC_CAN_10MBIT,
434 };
435 
436 static struct com20020_pci_card_info card_info_eae_fb2 = {
437 	.name = "EAE PLX-PCI FB2",
438 	.devcount = 1,
439 	.chan_map_tbl = {
440 		{
441 			.bar = 2,
442 			.offset = 0x00,
443 			.size = 0x08,
444 		},
445 	},
446 	.misc_map = {
447 		.bar = 2,
448 		.offset = 0x10,
449 		.size = 0x04,
450 	},
451 	.leds = {
452 		{
453 			.green = 0x0,
454 			.red = 0x1,
455 		},
456 	},
457 	.rotary = 0x0,
458 	.flags = ARC_HAS_ROTARY | ARC_HAS_LED | ARC_CAN_10MBIT,
459 };
460 
461 static const struct pci_device_id com20020pci_id_table[] = {
462 	{
463 		PCI_DEVICE(0x1571, 0xa001),
464 		.driver_data = 0,
465 	}, {
466 		PCI_DEVICE(0x1571, 0xa002),
467 		.driver_data = 0,
468 	}, {
469 		PCI_DEVICE(0x1571, 0xa003),
470 		.driver_data = 0,
471 	}, {
472 		PCI_DEVICE(0x1571, 0xa004),
473 		.driver_data = 0,
474 	}, {
475 		PCI_DEVICE(0x1571, 0xa005),
476 		.driver_data = 0,
477 	}, {
478 		PCI_DEVICE(0x1571, 0xa006),
479 		.driver_data = 0,
480 	}, {
481 		PCI_DEVICE(0x1571, 0xa007),
482 		.driver_data = 0,
483 	}, {
484 		PCI_DEVICE(0x1571, 0xa008),
485 		.driver_data = 0,
486 	}, {
487 		PCI_DEVICE(0x1571, 0xa009),
488 		.driver_data = (kernel_ulong_t)&card_info_5mbit,
489 	}, {
490 		PCI_DEVICE(0x1571, 0xa00a),
491 		.driver_data = (kernel_ulong_t)&card_info_5mbit,
492 	}, {
493 		PCI_DEVICE(0x1571, 0xa00b),
494 		.driver_data = (kernel_ulong_t)&card_info_5mbit,
495 	}, {
496 		PCI_DEVICE(0x1571, 0xa00c),
497 		.driver_data = (kernel_ulong_t)&card_info_5mbit,
498 	}, {
499 		PCI_DEVICE(0x1571, 0xa00d),
500 		.driver_data = (kernel_ulong_t)&card_info_5mbit,
501 	}, {
502 		PCI_DEVICE(0x1571, 0xa00e),
503 		.driver_data = (kernel_ulong_t)&card_info_5mbit,
504 	}, {
505 		PCI_DEVICE(0x1571, 0xa201),
506 		.driver_data = (kernel_ulong_t)&card_info_10mbit,
507 	}, {
508 		PCI_DEVICE(0x1571, 0xa202),
509 		.driver_data = (kernel_ulong_t)&card_info_10mbit,
510 	}, {
511 		PCI_DEVICE(0x1571, 0xa203),
512 		.driver_data = (kernel_ulong_t)&card_info_10mbit,
513 	}, {
514 		PCI_DEVICE(0x1571, 0xa204),
515 		.driver_data = (kernel_ulong_t)&card_info_10mbit,
516 	}, {
517 		PCI_DEVICE(0x1571, 0xa205),
518 		.driver_data = (kernel_ulong_t)&card_info_10mbit,
519 	}, {
520 		PCI_DEVICE(0x1571, 0xa206),
521 		.driver_data = (kernel_ulong_t)&card_info_10mbit,
522 	}, {
523 		PCI_DEVICE_SUB(0x10B5, 0x9030, 0x10B5, 0x2978),
524 		.driver_data = (kernel_ulong_t)&card_info_sohard,
525 	}, {
526 		PCI_DEVICE_SUB(0x10B5, 0x9050, 0x10B5, 0x2273),
527 		.driver_data = (kernel_ulong_t)&card_info_sohard,
528 	}, {
529 		PCI_DEVICE_SUB(0x10B5, 0x9050, 0x10B5, 0x3263),
530 		.driver_data = (kernel_ulong_t)&card_info_eae_arc1,
531 	}, {
532 		PCI_DEVICE_SUB(0x10B5, 0x9050, 0x10B5, 0x3292),
533 		.driver_data = (kernel_ulong_t)&card_info_eae_ma1,
534 	}, {
535 		PCI_DEVICE_SUB(0x10B5, 0x9050, 0x10B5, 0x3294),
536 		.driver_data = (kernel_ulong_t)&card_info_eae_fb2,
537 	}, {
538 		PCI_DEVICE(0x14BA, 0x6000),
539 		.driver_data = (kernel_ulong_t)&card_info_10mbit,
540 	}, {
541 		PCI_DEVICE(0x10B5, 0x2200),
542 		.driver_data = (kernel_ulong_t)&card_info_10mbit,
543 	},
544 	{ }
545 };
546 
547 MODULE_DEVICE_TABLE(pci, com20020pci_id_table);
548 
549 static struct pci_driver com20020pci_driver = {
550 	.name		= "com20020",
551 	.id_table	= com20020pci_id_table,
552 	.probe		= com20020pci_probe,
553 	.remove		= com20020pci_remove,
554 };
555 
556 module_pci_driver(com20020pci_driver);
557