Linux-libre 4.7.10-gnu
[librecmc/linux-libre.git] / drivers / net / can / softing / softing_cs.c
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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22
23 #include <pcmcia/cistpl.h>
24 #include <pcmcia/ds.h>
25
26 #include "softing_platform.h"
27
28 static int softingcs_index;
29 static DEFINE_SPINLOCK(softingcs_index_lock);
30
31 static int softingcs_reset(struct platform_device *pdev, int v);
32 static int softingcs_enable_irq(struct platform_device *pdev, int v);
33
34 /*
35  * platform_data descriptions
36  */
37 #define MHZ (1000*1000)
38 static const struct softing_platform_data softingcs_platform_data[] = {
39 {
40         .name = "CANcard",
41         .manf = 0x0168, .prod = 0x001,
42         .generation = 1,
43         .nbus = 2,
44         .freq = 16 * MHZ, .max_brp = 32, .max_sjw = 4,
45         .dpram_size = 0x0800,
46         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
47         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
48         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
49         .reset = softingcs_reset,
50         .enable_irq = softingcs_enable_irq,
51 }, {
52         .name = "CANcard-NEC",
53         .manf = 0x0168, .prod = 0x002,
54         .generation = 1,
55         .nbus = 2,
56         .freq = 16 * MHZ, .max_brp = 32, .max_sjw = 4,
57         .dpram_size = 0x0800,
58         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
59         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
60         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
61         .reset = softingcs_reset,
62         .enable_irq = softingcs_enable_irq,
63 }, {
64         .name = "CANcard-SJA",
65         .manf = 0x0168, .prod = 0x004,
66         .generation = 1,
67         .nbus = 2,
68         .freq = 20 * MHZ, .max_brp = 32, .max_sjw = 4,
69         .dpram_size = 0x0800,
70         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
71         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
72         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
73         .reset = softingcs_reset,
74         .enable_irq = softingcs_enable_irq,
75 }, {
76         .name = "CANcard-2",
77         .manf = 0x0168, .prod = 0x005,
78         .generation = 2,
79         .nbus = 2,
80         .freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
81         .dpram_size = 0x1000,
82         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
83         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
84         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
85         .reset = softingcs_reset,
86         .enable_irq = NULL,
87 }, {
88         .name = "Vector-CANcard",
89         .manf = 0x0168, .prod = 0x081,
90         .generation = 1,
91         .nbus = 2,
92         .freq = 16 * MHZ, .max_brp = 64, .max_sjw = 4,
93         .dpram_size = 0x0800,
94         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
95         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
96         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
97         .reset = softingcs_reset,
98         .enable_irq = softingcs_enable_irq,
99 }, {
100         .name = "Vector-CANcard-SJA",
101         .manf = 0x0168, .prod = 0x084,
102         .generation = 1,
103         .nbus = 2,
104         .freq = 20 * MHZ, .max_brp = 32, .max_sjw = 4,
105         .dpram_size = 0x0800,
106         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
107         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
108         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
109         .reset = softingcs_reset,
110         .enable_irq = softingcs_enable_irq,
111 }, {
112         .name = "Vector-CANcard-2",
113         .manf = 0x0168, .prod = 0x085,
114         .generation = 2,
115         .nbus = 2,
116         .freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
117         .dpram_size = 0x1000,
118         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
119         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
120         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
121         .reset = softingcs_reset,
122         .enable_irq = NULL,
123 }, {
124         .name = "EDICcard-NEC",
125         .manf = 0x0168, .prod = 0x102,
126         .generation = 1,
127         .nbus = 2,
128         .freq = 16 * MHZ, .max_brp = 64, .max_sjw = 4,
129         .dpram_size = 0x0800,
130         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
131         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
132         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
133         .reset = softingcs_reset,
134         .enable_irq = softingcs_enable_irq,
135 }, {
136         .name = "EDICcard-2",
137         .manf = 0x0168, .prod = 0x105,
138         .generation = 2,
139         .nbus = 2,
140         .freq = 24 * MHZ, .max_brp = 64, .max_sjw = 4,
141         .dpram_size = 0x1000,
142         .boot = {0x0000, 0x000000, fw_dir "/*(DEBLOBBED)*/",},
143         .load = {0x0120, 0x00f600, fw_dir "/*(DEBLOBBED)*/",},
144         .app = {0x0010, 0x0d0000, fw_dir "/*(DEBLOBBED)*/",},
145         .reset = softingcs_reset,
146         .enable_irq = NULL,
147 }, {
148         0, 0,
149 },
150 };
151
152 /*(DEBLOBBED)*/
153
154 static const struct softing_platform_data
155 *softingcs_find_platform_data(unsigned int manf, unsigned int prod)
156 {
157         const struct softing_platform_data *lp;
158
159         for (lp = softingcs_platform_data; lp->manf; ++lp) {
160                 if ((lp->manf == manf) && (lp->prod == prod))
161                         return lp;
162         }
163         return NULL;
164 }
165
166 /*
167  * platformdata callbacks
168  */
169 static int softingcs_reset(struct platform_device *pdev, int v)
170 {
171         struct pcmcia_device *pcmcia = to_pcmcia_dev(pdev->dev.parent);
172
173         dev_dbg(&pdev->dev, "pcmcia config [2] %02x\n", v ? 0 : 0x20);
174         return pcmcia_write_config_byte(pcmcia, 2, v ? 0 : 0x20);
175 }
176
177 static int softingcs_enable_irq(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 [0] %02x\n", v ? 0x60 : 0);
182         return pcmcia_write_config_byte(pcmcia, 0, v ? 0x60 : 0);
183 }
184
185 /*
186  * pcmcia check
187  */
188 static int softingcs_probe_config(struct pcmcia_device *pcmcia, void *priv_data)
189 {
190         struct softing_platform_data *pdat = priv_data;
191         struct resource *pres;
192         int memspeed = 0;
193
194         WARN_ON(!pdat);
195         pres = pcmcia->resource[PCMCIA_IOMEM_0];
196         if (resource_size(pres) < 0x1000)
197                 return -ERANGE;
198
199         pres->flags |= WIN_MEMORY_TYPE_CM | WIN_ENABLE;
200         if (pdat->generation < 2) {
201                 pres->flags |= WIN_USE_WAIT | WIN_DATA_WIDTH_8;
202                 memspeed = 3;
203         } else {
204                 pres->flags |= WIN_DATA_WIDTH_16;
205         }
206         return pcmcia_request_window(pcmcia, pres, memspeed);
207 }
208
209 static void softingcs_remove(struct pcmcia_device *pcmcia)
210 {
211         struct platform_device *pdev = pcmcia->priv;
212
213         /* free bits */
214         platform_device_unregister(pdev);
215         /* release pcmcia stuff */
216         pcmcia_disable_device(pcmcia);
217 }
218
219 /*
220  * platform_device wrapper
221  * pdev->resource has 2 entries: io & irq
222  */
223 static void softingcs_pdev_release(struct device *dev)
224 {
225         struct platform_device *pdev = to_platform_device(dev);
226         kfree(pdev);
227 }
228
229 static int softingcs_probe(struct pcmcia_device *pcmcia)
230 {
231         int ret;
232         struct platform_device *pdev;
233         const struct softing_platform_data *pdat;
234         struct resource *pres;
235         struct dev {
236                 struct platform_device pdev;
237                 struct resource res[2];
238         } *dev;
239
240         /* find matching platform_data */
241         pdat = softingcs_find_platform_data(pcmcia->manf_id, pcmcia->card_id);
242         if (!pdat)
243                 return -ENOTTY;
244
245         /* setup pcmcia device */
246         pcmcia->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IOMEM |
247                 CONF_AUTO_SET_VPP | CONF_AUTO_CHECK_VCC;
248         ret = pcmcia_loop_config(pcmcia, softingcs_probe_config, (void *)pdat);
249         if (ret)
250                 goto pcmcia_failed;
251
252         ret = pcmcia_enable_device(pcmcia);
253         if (ret < 0)
254                 goto pcmcia_failed;
255
256         pres = pcmcia->resource[PCMCIA_IOMEM_0];
257         if (!pres) {
258                 ret = -EBADF;
259                 goto pcmcia_bad;
260         }
261
262         /* create softing platform device */
263         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
264         if (!dev) {
265                 ret = -ENOMEM;
266                 goto mem_failed;
267         }
268         dev->pdev.resource = dev->res;
269         dev->pdev.num_resources = ARRAY_SIZE(dev->res);
270         dev->pdev.dev.release = softingcs_pdev_release;
271
272         pdev = &dev->pdev;
273         pdev->dev.platform_data = (void *)pdat;
274         pdev->dev.parent = &pcmcia->dev;
275         pcmcia->priv = pdev;
276
277         /* platform device resources */
278         pdev->resource[0].flags = IORESOURCE_MEM;
279         pdev->resource[0].start = pres->start;
280         pdev->resource[0].end = pres->end;
281
282         pdev->resource[1].flags = IORESOURCE_IRQ;
283         pdev->resource[1].start = pcmcia->irq;
284         pdev->resource[1].end = pdev->resource[1].start;
285
286         /* platform device setup */
287         spin_lock(&softingcs_index_lock);
288         pdev->id = softingcs_index++;
289         spin_unlock(&softingcs_index_lock);
290         pdev->name = "softing";
291         dev_set_name(&pdev->dev, "softingcs.%i", pdev->id);
292         ret = platform_device_register(pdev);
293         if (ret < 0)
294                 goto platform_failed;
295
296         dev_info(&pcmcia->dev, "created %s\n", dev_name(&pdev->dev));
297         return 0;
298
299 platform_failed:
300         kfree(dev);
301 mem_failed:
302 pcmcia_bad:
303 pcmcia_failed:
304         pcmcia_disable_device(pcmcia);
305         pcmcia->priv = NULL;
306         return ret ?: -ENODEV;
307 }
308
309 static const struct pcmcia_device_id softingcs_ids[] = {
310         /* softing */
311         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0001),
312         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0002),
313         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0004),
314         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0005),
315         /* vector, manufacturer? */
316         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0081),
317         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0084),
318         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0085),
319         /* EDIC */
320         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0102),
321         PCMCIA_DEVICE_MANF_CARD(0x0168, 0x0105),
322         PCMCIA_DEVICE_NULL,
323 };
324
325 MODULE_DEVICE_TABLE(pcmcia, softingcs_ids);
326
327 static struct pcmcia_driver softingcs_driver = {
328         .owner          = THIS_MODULE,
329         .name           = "softingcs",
330         .id_table       = softingcs_ids,
331         .probe          = softingcs_probe,
332         .remove         = softingcs_remove,
333 };
334
335 module_pcmcia_driver(softingcs_driver);
336
337 MODULE_DESCRIPTION("softing CANcard driver"
338                 ", links PCMCIA card to softing driver");
339 MODULE_LICENSE("GPL v2");