1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * tscan1.c: driver for Technologic Systems TS-CAN1 PC104 boards
4  *
5  * Copyright 2010 Andre B. Oliveira
6  */
7 
8 /*
9  * References:
10  * - Getting started with TS-CAN1, Technologic Systems, Jun 2009
11  *	http://www.embeddedarm.com/documentation/ts-can1-manual.pdf
12  */
13 
14 #include <linux/init.h>
15 #include <linux/io.h>
16 #include <linux/ioport.h>
17 #include <linux/isa.h>
18 #include <linux/module.h>
19 #include <linux/netdevice.h>
20 #include "sja1000.h"
21 
22 MODULE_DESCRIPTION("Driver for Technologic Systems TS-CAN1 PC104 boards");
23 MODULE_AUTHOR("Andre B. Oliveira <anbadeol@gmail.com>");
24 MODULE_LICENSE("GPL");
25 
26 /* Maximum number of boards (one in each JP1:JP2 setting of IO address) */
27 #define TSCAN1_MAXDEV 4
28 
29 /* PLD registers address offsets */
30 #define TSCAN1_ID1	0
31 #define TSCAN1_ID2	1
32 #define TSCAN1_VERSION	2
33 #define TSCAN1_LED	3
34 #define TSCAN1_PAGE	4
35 #define TSCAN1_MODE	5
36 #define TSCAN1_JUMPERS	6
37 
38 /* PLD board identifier registers magic values */
39 #define TSCAN1_ID1_VALUE 0xf6
40 #define TSCAN1_ID2_VALUE 0xb9
41 
42 /* PLD mode register SJA1000 IO enable bit */
43 #define TSCAN1_MODE_ENABLE 0x40
44 
45 /* PLD jumpers register bits */
46 #define TSCAN1_JP4 0x10
47 #define TSCAN1_JP5 0x20
48 
49 /* PLD IO base addresses start */
50 #define TSCAN1_PLD_ADDRESS 0x150
51 
52 /* PLD register space size */
53 #define TSCAN1_PLD_SIZE 8
54 
55 /* SJA1000 register space size */
56 #define TSCAN1_SJA1000_SIZE 32
57 
58 /* SJA1000 crystal frequency (16MHz) */
59 #define TSCAN1_SJA1000_XTAL 16000000
60 
61 /* SJA1000 IO base addresses */
62 static const unsigned short tscan1_sja1000_addresses[] = {
63 	0x100, 0x120, 0x180, 0x1a0, 0x200, 0x240, 0x280, 0x320
64 };
65 
66 /* Read SJA1000 register */
tscan1_read(const struct sja1000_priv * priv,int reg)67 static u8 tscan1_read(const struct sja1000_priv *priv, int reg)
68 {
69 	return inb((unsigned long)priv->reg_base + reg);
70 }
71 
72 /* Write SJA1000 register */
tscan1_write(const struct sja1000_priv * priv,int reg,u8 val)73 static void tscan1_write(const struct sja1000_priv *priv, int reg, u8 val)
74 {
75 	outb(val, (unsigned long)priv->reg_base + reg);
76 }
77 
78 /* Probe for a TS-CAN1 board with JP2:JP1 jumper setting ID */
tscan1_probe(struct device * dev,unsigned id)79 static int tscan1_probe(struct device *dev, unsigned id)
80 {
81 	struct net_device *netdev;
82 	struct sja1000_priv *priv;
83 	unsigned long pld_base, sja1000_base;
84 	int irq, i;
85 
86 	pld_base = TSCAN1_PLD_ADDRESS + id * TSCAN1_PLD_SIZE;
87 	if (!request_region(pld_base, TSCAN1_PLD_SIZE, dev_name(dev)))
88 		return -EBUSY;
89 
90 	if (inb(pld_base + TSCAN1_ID1) != TSCAN1_ID1_VALUE ||
91 	    inb(pld_base + TSCAN1_ID2) != TSCAN1_ID2_VALUE) {
92 		release_region(pld_base, TSCAN1_PLD_SIZE);
93 		return -ENODEV;
94 	}
95 
96 	switch (inb(pld_base + TSCAN1_JUMPERS) & (TSCAN1_JP4 | TSCAN1_JP5)) {
97 	case TSCAN1_JP4:
98 		irq = 6;
99 		break;
100 	case TSCAN1_JP5:
101 		irq = 7;
102 		break;
103 	case TSCAN1_JP4 | TSCAN1_JP5:
104 		irq = 5;
105 		break;
106 	default:
107 		dev_err(dev, "invalid JP4:JP5 setting (no IRQ)\n");
108 		release_region(pld_base, TSCAN1_PLD_SIZE);
109 		return -EINVAL;
110 	}
111 
112 	netdev = alloc_sja1000dev(0);
113 	if (!netdev) {
114 		release_region(pld_base, TSCAN1_PLD_SIZE);
115 		return -ENOMEM;
116 	}
117 
118 	dev_set_drvdata(dev, netdev);
119 	SET_NETDEV_DEV(netdev, dev);
120 
121 	netdev->base_addr = pld_base;
122 	netdev->irq = irq;
123 
124 	priv = netdev_priv(netdev);
125 	priv->read_reg = tscan1_read;
126 	priv->write_reg = tscan1_write;
127 	priv->can.clock.freq = TSCAN1_SJA1000_XTAL / 2;
128 	priv->cdr = CDR_CBP | CDR_CLK_OFF;
129 	priv->ocr = OCR_TX0_PUSHPULL;
130 
131 	/* Select the first SJA1000 IO address that is free and that works */
132 	for (i = 0; i < ARRAY_SIZE(tscan1_sja1000_addresses); i++) {
133 		sja1000_base = tscan1_sja1000_addresses[i];
134 		if (!request_region(sja1000_base, TSCAN1_SJA1000_SIZE,
135 								dev_name(dev)))
136 			continue;
137 
138 		/* Set SJA1000 IO base address and enable it */
139 		outb(TSCAN1_MODE_ENABLE | i, pld_base + TSCAN1_MODE);
140 
141 		priv->reg_base = (void __iomem *)sja1000_base;
142 		if (!register_sja1000dev(netdev)) {
143 			/* SJA1000 probe succeeded; turn LED off and return */
144 			outb(0, pld_base + TSCAN1_LED);
145 			netdev_info(netdev, "TS-CAN1 at 0x%lx 0x%lx irq %d\n",
146 						pld_base, sja1000_base, irq);
147 			return 0;
148 		}
149 
150 		/* SJA1000 probe failed; release and try next address */
151 		outb(0, pld_base + TSCAN1_MODE);
152 		release_region(sja1000_base, TSCAN1_SJA1000_SIZE);
153 	}
154 
155 	dev_err(dev, "failed to assign SJA1000 IO address\n");
156 	dev_set_drvdata(dev, NULL);
157 	free_sja1000dev(netdev);
158 	release_region(pld_base, TSCAN1_PLD_SIZE);
159 	return -ENXIO;
160 }
161 
tscan1_remove(struct device * dev,unsigned id)162 static void tscan1_remove(struct device *dev, unsigned id /*unused*/)
163 {
164 	struct net_device *netdev;
165 	struct sja1000_priv *priv;
166 	unsigned long pld_base, sja1000_base;
167 
168 	netdev = dev_get_drvdata(dev);
169 	unregister_sja1000dev(netdev);
170 	dev_set_drvdata(dev, NULL);
171 
172 	priv = netdev_priv(netdev);
173 	pld_base = netdev->base_addr;
174 	sja1000_base = (unsigned long)priv->reg_base;
175 
176 	outb(0, pld_base + TSCAN1_MODE);	/* disable SJA1000 IO space */
177 
178 	release_region(sja1000_base, TSCAN1_SJA1000_SIZE);
179 	release_region(pld_base, TSCAN1_PLD_SIZE);
180 
181 	free_sja1000dev(netdev);
182 }
183 
184 static struct isa_driver tscan1_isa_driver = {
185 	.probe = tscan1_probe,
186 	.remove = tscan1_remove,
187 	.driver = {
188 		.name = "tscan1",
189 	},
190 };
191 
192 module_isa_driver(tscan1_isa_driver, TSCAN1_MAXDEV);
193