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