xref: /linux/drivers/net/can/softing/softing_cs.c (revision 0d456bad36d42d16022be045c8a53ddbb59ee478)
1 /*
2  * Copyright (C) 2008-2010
3  *
4  * - Kurt Van Dijck, EIA Electronics
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the version 2 of the GNU General Public License
8  * as published by the Free Software Foundation
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/slab.h>
23 
24 #include <pcmcia/cistpl.h>
25 #include <pcmcia/ds.h>
26 
27 #include "softing_platform.h"
28 
29 static int softingcs_index;
30 static spinlock_t softingcs_index_lock;
31 
32 static int softingcs_reset(struct platform_device *pdev, int v);
33 static int softingcs_enable_irq(struct platform_device *pdev, int v);
34 
35 /*
36  * platform_data descriptions
37  */
38 #define MHZ (1000*1000)
39 static const struct softing_platform_data softingcs_platform_data[] = {
40 {
41 	.name = "CANcard",
42 	.manf = 0x0168, .prod = 0x001,
43 	.generation = 1,
44 	.nbus = 2,
45 	.freq = 16 * MHZ, .max_brp = 32, .max_sjw = 4,
46 	.dpram_size = 0x0800,
47 	.boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
48 	.load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
49 	.app = {0x0010, 0x0d0000, fw_dir "cancard.bin",},
50 	.reset = softingcs_reset,
51 	.enable_irq = softingcs_enable_irq,
52 }, {
53 	.name = "CANcard-NEC",
54 	.manf = 0x0168, .prod = 0x002,
55 	.generation = 1,
56 	.nbus = 2,
57 	.freq = 16 * MHZ, .max_brp = 32, .max_sjw = 4,
58 	.dpram_size = 0x0800,
59 	.boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
60 	.load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
61 	.app = {0x0010, 0x0d0000, fw_dir "cancard.bin",},
62 	.reset = softingcs_reset,
63 	.enable_irq = softingcs_enable_irq,
64 }, {
65 	.name = "CANcard-SJA",
66 	.manf = 0x0168, .prod = 0x004,
67 	.generation = 1,
68 	.nbus = 2,
69 	.freq = 20 * MHZ, .max_brp = 32, .max_sjw = 4,
70 	.dpram_size = 0x0800,
71 	.boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
72 	.load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
73 	.app = {0x0010, 0x0d0000, fw_dir "cansja.bin",},
74 	.reset = softingcs_reset,
75 	.enable_irq = softingcs_enable_irq,
76 }, {
77 	.name = "CANcard-2",
78 	.manf = 0x0168, .prod = 0x005,
79 	.generation = 2,
80 	.nbus = 2,
81 	.freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
82 	.dpram_size = 0x1000,
83 	.boot = {0x0000, 0x000000, fw_dir "bcard2.bin",},
84 	.load = {0x0120, 0x00f600, fw_dir "ldcard2.bin",},
85 	.app = {0x0010, 0x0d0000, fw_dir "cancrd2.bin",},
86 	.reset = softingcs_reset,
87 	.enable_irq = NULL,
88 }, {
89 	.name = "Vector-CANcard",
90 	.manf = 0x0168, .prod = 0x081,
91 	.generation = 1,
92 	.nbus = 2,
93 	.freq = 16 * MHZ, .max_brp = 64, .max_sjw = 4,
94 	.dpram_size = 0x0800,
95 	.boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
96 	.load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
97 	.app = {0x0010, 0x0d0000, fw_dir "cancard.bin",},
98 	.reset = softingcs_reset,
99 	.enable_irq = softingcs_enable_irq,
100 }, {
101 	.name = "Vector-CANcard-SJA",
102 	.manf = 0x0168, .prod = 0x084,
103 	.generation = 1,
104 	.nbus = 2,
105 	.freq = 20 * MHZ, .max_brp = 32, .max_sjw = 4,
106 	.dpram_size = 0x0800,
107 	.boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
108 	.load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
109 	.app = {0x0010, 0x0d0000, fw_dir "cansja.bin",},
110 	.reset = softingcs_reset,
111 	.enable_irq = softingcs_enable_irq,
112 }, {
113 	.name = "Vector-CANcard-2",
114 	.manf = 0x0168, .prod = 0x085,
115 	.generation = 2,
116 	.nbus = 2,
117 	.freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
118 	.dpram_size = 0x1000,
119 	.boot = {0x0000, 0x000000, fw_dir "bcard2.bin",},
120 	.load = {0x0120, 0x00f600, fw_dir "ldcard2.bin",},
121 	.app = {0x0010, 0x0d0000, fw_dir "cancrd2.bin",},
122 	.reset = softingcs_reset,
123 	.enable_irq = NULL,
124 }, {
125 	.name = "EDICcard-NEC",
126 	.manf = 0x0168, .prod = 0x102,
127 	.generation = 1,
128 	.nbus = 2,
129 	.freq = 16 * MHZ, .max_brp = 64, .max_sjw = 4,
130 	.dpram_size = 0x0800,
131 	.boot = {0x0000, 0x000000, fw_dir "bcard.bin",},
132 	.load = {0x0120, 0x00f600, fw_dir "ldcard.bin",},
133 	.app = {0x0010, 0x0d0000, fw_dir "cancard.bin",},
134 	.reset = softingcs_reset,
135 	.enable_irq = softingcs_enable_irq,
136 }, {
137 	.name = "EDICcard-2",
138 	.manf = 0x0168, .prod = 0x105,
139 	.generation = 2,
140 	.nbus = 2,
141 	.freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
142 	.dpram_size = 0x1000,
143 	.boot = {0x0000, 0x000000, fw_dir "bcard2.bin",},
144 	.load = {0x0120, 0x00f600, fw_dir "ldcard2.bin",},
145 	.app = {0x0010, 0x0d0000, fw_dir "cancrd2.bin",},
146 	.reset = softingcs_reset,
147 	.enable_irq = NULL,
148 }, {
149 	0, 0,
150 },
151 };
152 
153 MODULE_FIRMWARE(fw_dir "bcard.bin");
154 MODULE_FIRMWARE(fw_dir "ldcard.bin");
155 MODULE_FIRMWARE(fw_dir "cancard.bin");
156 MODULE_FIRMWARE(fw_dir "cansja.bin");
157 
158 MODULE_FIRMWARE(fw_dir "bcard2.bin");
159 MODULE_FIRMWARE(fw_dir "ldcard2.bin");
160 MODULE_FIRMWARE(fw_dir "cancrd2.bin");
161 
162 static const struct softing_platform_data
163 *softingcs_find_platform_data(unsigned int manf, unsigned int prod)
164 {
165 	const struct softing_platform_data *lp;
166 
167 	for (lp = softingcs_platform_data; lp->manf; ++lp) {
168 		if ((lp->manf == manf) && (lp->prod == prod))
169 			return lp;
170 	}
171 	return NULL;
172 }
173 
174 /*
175  * platformdata callbacks
176  */
177 static int softingcs_reset(struct platform_device *pdev, int v)
178 {
179 	struct pcmcia_device *pcmcia = to_pcmcia_dev(pdev->dev.parent);
180 
181 	dev_dbg(&pdev->dev, "pcmcia config [2] %02x\n", v ? 0 : 0x20);
182 	return pcmcia_write_config_byte(pcmcia, 2, v ? 0 : 0x20);
183 }
184 
185 static int softingcs_enable_irq(struct platform_device *pdev, int v)
186 {
187 	struct pcmcia_device *pcmcia = to_pcmcia_dev(pdev->dev.parent);
188 
189 	dev_dbg(&pdev->dev, "pcmcia config [0] %02x\n", v ? 0x60 : 0);
190 	return pcmcia_write_config_byte(pcmcia, 0, v ? 0x60 : 0);
191 }
192 
193 /*
194  * pcmcia check
195  */
196 static int softingcs_probe_config(struct pcmcia_device *pcmcia, void *priv_data)
197 {
198 	struct softing_platform_data *pdat = priv_data;
199 	struct resource *pres;
200 	int memspeed = 0;
201 
202 	WARN_ON(!pdat);
203 	pres = pcmcia->resource[PCMCIA_IOMEM_0];
204 	if (resource_size(pres) < 0x1000)
205 		return -ERANGE;
206 
207 	pres->flags |= WIN_MEMORY_TYPE_CM | WIN_ENABLE;
208 	if (pdat->generation < 2) {
209 		pres->flags |= WIN_USE_WAIT | WIN_DATA_WIDTH_8;
210 		memspeed = 3;
211 	} else {
212 		pres->flags |= WIN_DATA_WIDTH_16;
213 	}
214 	return pcmcia_request_window(pcmcia, pres, memspeed);
215 }
216 
217 static void softingcs_remove(struct pcmcia_device *pcmcia)
218 {
219 	struct platform_device *pdev = pcmcia->priv;
220 
221 	/* free bits */
222 	platform_device_unregister(pdev);
223 	/* release pcmcia stuff */
224 	pcmcia_disable_device(pcmcia);
225 }
226 
227 /*
228  * platform_device wrapper
229  * pdev->resource has 2 entries: io & irq
230  */
231 static void softingcs_pdev_release(struct device *dev)
232 {
233 	struct platform_device *pdev = to_platform_device(dev);
234 	kfree(pdev);
235 }
236 
237 static int softingcs_probe(struct pcmcia_device *pcmcia)
238 {
239 	int ret;
240 	struct platform_device *pdev;
241 	const struct softing_platform_data *pdat;
242 	struct resource *pres;
243 	struct dev {
244 		struct platform_device pdev;
245 		struct resource res[2];
246 	} *dev;
247 
248 	/* find matching platform_data */
249 	pdat = softingcs_find_platform_data(pcmcia->manf_id, pcmcia->card_id);
250 	if (!pdat)
251 		return -ENOTTY;
252 
253 	/* setup pcmcia device */
254 	pcmcia->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IOMEM |
255 		CONF_AUTO_SET_VPP | CONF_AUTO_CHECK_VCC;
256 	ret = pcmcia_loop_config(pcmcia, softingcs_probe_config, (void *)pdat);
257 	if (ret)
258 		goto pcmcia_failed;
259 
260 	ret = pcmcia_enable_device(pcmcia);
261 	if (ret < 0)
262 		goto pcmcia_failed;
263 
264 	pres = pcmcia->resource[PCMCIA_IOMEM_0];
265 	if (!pres) {
266 		ret = -EBADF;
267 		goto pcmcia_bad;
268 	}
269 
270 	/* create softing platform device */
271 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
272 	if (!dev) {
273 		ret = -ENOMEM;
274 		goto mem_failed;
275 	}
276 	dev->pdev.resource = dev->res;
277 	dev->pdev.num_resources = ARRAY_SIZE(dev->res);
278 	dev->pdev.dev.release = softingcs_pdev_release;
279 
280 	pdev = &dev->pdev;
281 	pdev->dev.platform_data = (void *)pdat;
282 	pdev->dev.parent = &pcmcia->dev;
283 	pcmcia->priv = pdev;
284 
285 	/* platform device resources */
286 	pdev->resource[0].flags = IORESOURCE_MEM;
287 	pdev->resource[0].start = pres->start;
288 	pdev->resource[0].end = pres->end;
289 
290 	pdev->resource[1].flags = IORESOURCE_IRQ;
291 	pdev->resource[1].start = pcmcia->irq;
292 	pdev->resource[1].end = pdev->resource[1].start;
293 
294 	/* platform device setup */
295 	spin_lock(&softingcs_index_lock);
296 	pdev->id = softingcs_index++;
297 	spin_unlock(&softingcs_index_lock);
298 	pdev->name = "softing";
299 	dev_set_name(&pdev->dev, "softingcs.%i", pdev->id);
300 	ret = platform_device_register(pdev);
301 	if (ret < 0)
302 		goto platform_failed;
303 
304 	dev_info(&pcmcia->dev, "created %s\n", dev_name(&pdev->dev));
305 	return 0;
306 
307 platform_failed:
308 	kfree(dev);
309 mem_failed:
310 pcmcia_bad:
311 pcmcia_failed:
312 	pcmcia_disable_device(pcmcia);
313 	pcmcia->priv = NULL;
314 	return ret ?: -ENODEV;
315 }
316 
317 static const struct pcmcia_device_id softingcs_ids[] = {
318 	/* softing */
319 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0001),
320 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0002),
321 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0004),
322 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0005),
323 	/* vector, manufacturer? */
324 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0081),
325 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0084),
326 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0085),
327 	/* EDIC */
328 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0102),
329 	PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0105),
330 	PCMCIA_DEVICE_NULL,
331 };
332 
333 MODULE_DEVICE_TABLE(pcmcia, softingcs_ids);
334 
335 static struct pcmcia_driver softingcs_driver = {
336 	.owner		= THIS_MODULE,
337 	.name		= "softingcs",
338 	.id_table	= softingcs_ids,
339 	.probe		= softingcs_probe,
340 	.remove		= softingcs_remove,
341 };
342 
343 static int __init softingcs_start(void)
344 {
345 	spin_lock_init(&softingcs_index_lock);
346 	return pcmcia_register_driver(&softingcs_driver);
347 }
348 
349 static void __exit softingcs_stop(void)
350 {
351 	pcmcia_unregister_driver(&softingcs_driver);
352 }
353 
354 module_init(softingcs_start);
355 module_exit(softingcs_stop);
356 
357 MODULE_DESCRIPTION("softing CANcard driver"
358 		", links PCMCIA card to softing driver");
359 MODULE_LICENSE("GPL v2");
360