Commit | Line | Data |
---|---|---|
d3465872 SIG |
1 | /* |
2 | * Industry-pack bus support functions. | |
3 | * | |
76859725 SIG |
4 | * Copyright (C) 2011-2012 CERN (www.cern.ch) |
5 | * Author: Samuel Iglesias Gonsalvez <siglesias@igalia.com> | |
d3465872 SIG |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the Free | |
416289b1 | 9 | * Software Foundation; version 2 of the License. |
d3465872 SIG |
10 | */ |
11 | ||
d3465872 | 12 | #include <linux/module.h> |
ec440335 | 13 | #include <linux/slab.h> |
3b86bb2e | 14 | #include <linux/idr.h> |
0a8f4a07 | 15 | #include <linux/io.h> |
7dbce021 | 16 | #include <linux/ipack.h> |
d3465872 SIG |
17 | |
18 | #define to_ipack_dev(device) container_of(device, struct ipack_device, dev) | |
19 | #define to_ipack_driver(drv) container_of(drv, struct ipack_driver, driver) | |
20 | ||
3b86bb2e | 21 | static DEFINE_IDA(ipack_ida); |
d3465872 | 22 | |
ec440335 SIG |
23 | static void ipack_device_release(struct device *dev) |
24 | { | |
25 | struct ipack_device *device = to_ipack_dev(dev); | |
187e4782 | 26 | kfree(device->id); |
1e91795c | 27 | device->release(device); |
ec440335 SIG |
28 | } |
29 | ||
4aa09d47 JT |
30 | static inline const struct ipack_device_id * |
31 | ipack_match_one_device(const struct ipack_device_id *id, | |
32 | const struct ipack_device *device) | |
d3465872 | 33 | { |
5948ae27 JT |
34 | if ((id->format == IPACK_ANY_FORMAT || |
35 | id->format == device->id_format) && | |
4aa09d47 JT |
36 | (id->vendor == IPACK_ANY_ID || id->vendor == device->id_vendor) && |
37 | (id->device == IPACK_ANY_ID || id->device == device->id_device)) | |
38 | return id; | |
39 | return NULL; | |
40 | } | |
d3465872 | 41 | |
4aa09d47 JT |
42 | static const struct ipack_device_id * |
43 | ipack_match_id(const struct ipack_device_id *ids, struct ipack_device *idev) | |
44 | { | |
45 | if (ids) { | |
46 | while (ids->vendor || ids->device) { | |
47 | if (ipack_match_one_device(ids, idev)) | |
48 | return ids; | |
49 | ids++; | |
50 | } | |
51 | } | |
52 | return NULL; | |
53 | } | |
54 | ||
55 | static int ipack_bus_match(struct device *dev, struct device_driver *drv) | |
56 | { | |
57 | struct ipack_device *idev = to_ipack_dev(dev); | |
58 | struct ipack_driver *idrv = to_ipack_driver(drv); | |
59 | const struct ipack_device_id *found_id; | |
d3465872 | 60 | |
4aa09d47 | 61 | found_id = ipack_match_id(idrv->id_table, idev); |
3bea7fcb | 62 | return found_id ? 1 : 0; |
d3465872 SIG |
63 | } |
64 | ||
65 | static int ipack_bus_probe(struct device *device) | |
66 | { | |
67 | struct ipack_device *dev = to_ipack_dev(device); | |
3bea7fcb | 68 | struct ipack_driver *drv = to_ipack_driver(device->driver); |
d3465872 | 69 | |
3bea7fcb | 70 | if (!drv->ops->probe) |
d3465872 SIG |
71 | return -EINVAL; |
72 | ||
3bea7fcb | 73 | return drv->ops->probe(dev); |
d3465872 SIG |
74 | } |
75 | ||
76 | static int ipack_bus_remove(struct device *device) | |
77 | { | |
78 | struct ipack_device *dev = to_ipack_dev(device); | |
3bea7fcb | 79 | struct ipack_driver *drv = to_ipack_driver(device->driver); |
d3465872 | 80 | |
3bea7fcb | 81 | if (!drv->ops->remove) |
d3465872 SIG |
82 | return -EINVAL; |
83 | ||
3bea7fcb | 84 | drv->ops->remove(dev); |
d3465872 SIG |
85 | return 0; |
86 | } | |
87 | ||
35eb97bb JT |
88 | static int ipack_uevent(struct device *dev, struct kobj_uevent_env *env) |
89 | { | |
90 | struct ipack_device *idev; | |
91 | ||
92 | if (!dev) | |
93 | return -ENODEV; | |
94 | ||
95 | idev = to_ipack_dev(dev); | |
96 | ||
97 | if (add_uevent_var(env, | |
98 | "MODALIAS=ipack:f%02Xv%08Xd%08X", idev->id_format, | |
99 | idev->id_vendor, idev->id_device)) | |
100 | return -ENOMEM; | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
e8ed3276 JT |
105 | #define ipack_device_attr(field, format_string) \ |
106 | static ssize_t \ | |
107 | field##_show(struct device *dev, struct device_attribute *attr, \ | |
108 | char *buf) \ | |
109 | { \ | |
110 | struct ipack_device *idev = to_ipack_dev(dev); \ | |
111 | return sprintf(buf, format_string, idev->field); \ | |
112 | } | |
113 | ||
5d72c848 JT |
114 | static ssize_t id_show(struct device *dev, |
115 | struct device_attribute *attr, char *buf) | |
116 | { | |
117 | unsigned int i, c, l, s; | |
118 | struct ipack_device *idev = to_ipack_dev(dev); | |
119 | ||
120 | ||
121 | switch (idev->id_format) { | |
122 | case IPACK_ID_VERSION_1: | |
123 | l = 0x7; s = 1; break; | |
124 | case IPACK_ID_VERSION_2: | |
125 | l = 0xf; s = 2; break; | |
126 | default: | |
127 | return -EIO; | |
128 | } | |
129 | c = 0; | |
130 | for (i = 0; i < idev->id_avail; i++) { | |
131 | if (i > 0) { | |
132 | if ((i & l) == 0) | |
133 | buf[c++] = '\n'; | |
134 | else if ((i & s) == 0) | |
135 | buf[c++] = ' '; | |
136 | } | |
137 | sprintf(&buf[c], "%02x", idev->id[i]); | |
138 | c += 2; | |
139 | } | |
140 | buf[c++] = '\n'; | |
141 | return c; | |
142 | } | |
143 | ||
e8ed3276 JT |
144 | static ssize_t |
145 | id_vendor_show(struct device *dev, struct device_attribute *attr, char *buf) | |
146 | { | |
147 | struct ipack_device *idev = to_ipack_dev(dev); | |
148 | switch (idev->id_format) { | |
149 | case IPACK_ID_VERSION_1: | |
150 | return sprintf(buf, "0x%02x\n", idev->id_vendor); | |
151 | case IPACK_ID_VERSION_2: | |
152 | return sprintf(buf, "0x%06x\n", idev->id_vendor); | |
153 | default: | |
154 | return -EIO; | |
155 | } | |
156 | } | |
157 | ||
158 | static ssize_t | |
159 | id_device_show(struct device *dev, struct device_attribute *attr, char *buf) | |
160 | { | |
161 | struct ipack_device *idev = to_ipack_dev(dev); | |
162 | switch (idev->id_format) { | |
163 | case IPACK_ID_VERSION_1: | |
164 | return sprintf(buf, "0x%02x\n", idev->id_device); | |
165 | case IPACK_ID_VERSION_2: | |
166 | return sprintf(buf, "0x%04x\n", idev->id_device); | |
167 | default: | |
168 | return -EIO; | |
169 | } | |
170 | } | |
171 | ||
35eb97bb JT |
172 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
173 | char *buf) | |
174 | { | |
175 | struct ipack_device *idev = to_ipack_dev(dev); | |
176 | ||
177 | return sprintf(buf, "ipac:f%02Xv%08Xd%08X", idev->id_format, | |
178 | idev->id_vendor, idev->id_device); | |
179 | } | |
180 | ||
e8ed3276 JT |
181 | ipack_device_attr(id_format, "0x%hhu\n"); |
182 | ||
5152a509 GKH |
183 | static DEVICE_ATTR_RO(id); |
184 | static DEVICE_ATTR_RO(id_device); | |
185 | static DEVICE_ATTR_RO(id_format); | |
186 | static DEVICE_ATTR_RO(id_vendor); | |
187 | static DEVICE_ATTR_RO(modalias); | |
188 | ||
189 | static struct attribute *ipack_attrs[] = { | |
190 | &dev_attr_id.attr, | |
191 | &dev_attr_id_device.attr, | |
192 | &dev_attr_id_format.attr, | |
193 | &dev_attr_id_vendor.attr, | |
194 | &dev_attr_modalias.attr, | |
195 | NULL, | |
e8ed3276 | 196 | }; |
5152a509 | 197 | ATTRIBUTE_GROUPS(ipack); |
e8ed3276 | 198 | |
d3465872 | 199 | static struct bus_type ipack_bus_type = { |
e8ed3276 JT |
200 | .name = "ipack", |
201 | .probe = ipack_bus_probe, | |
202 | .match = ipack_bus_match, | |
203 | .remove = ipack_bus_remove, | |
5152a509 | 204 | .dev_groups = ipack_groups, |
35eb97bb | 205 | .uevent = ipack_uevent, |
d3465872 SIG |
206 | }; |
207 | ||
ec440335 | 208 | struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots, |
36c53b3c FV |
209 | const struct ipack_bus_ops *ops, |
210 | struct module *owner) | |
d3465872 SIG |
211 | { |
212 | int bus_nr; | |
ec440335 SIG |
213 | struct ipack_bus_device *bus; |
214 | ||
215 | bus = kzalloc(sizeof(struct ipack_bus_device), GFP_KERNEL); | |
216 | if (!bus) | |
217 | return NULL; | |
d3465872 | 218 | |
3b86bb2e | 219 | bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL); |
ec440335 SIG |
220 | if (bus_nr < 0) { |
221 | kfree(bus); | |
222 | return NULL; | |
223 | } | |
d3465872 SIG |
224 | |
225 | bus->bus_nr = bus_nr; | |
ec440335 SIG |
226 | bus->parent = parent; |
227 | bus->slots = slots; | |
228 | bus->ops = ops; | |
36c53b3c | 229 | bus->owner = owner; |
ec440335 | 230 | return bus; |
d3465872 SIG |
231 | } |
232 | EXPORT_SYMBOL_GPL(ipack_bus_register); | |
233 | ||
bffe0fd0 SIG |
234 | static int ipack_unregister_bus_member(struct device *dev, void *data) |
235 | { | |
236 | struct ipack_device *idev = to_ipack_dev(dev); | |
237 | struct ipack_bus_device *bus = data; | |
238 | ||
f9e314d2 | 239 | if (idev->bus == bus) |
e926301b | 240 | ipack_device_del(idev); |
bffe0fd0 SIG |
241 | |
242 | return 1; | |
243 | } | |
244 | ||
d3465872 SIG |
245 | int ipack_bus_unregister(struct ipack_bus_device *bus) |
246 | { | |
0a8f4a07 JM |
247 | bus_for_each_dev(&ipack_bus_type, NULL, bus, |
248 | ipack_unregister_bus_member); | |
3b86bb2e | 249 | ida_simple_remove(&ipack_ida, bus->bus_nr); |
ec440335 | 250 | kfree(bus); |
d3465872 SIG |
251 | return 0; |
252 | } | |
253 | EXPORT_SYMBOL_GPL(ipack_bus_unregister); | |
254 | ||
ec440335 | 255 | int ipack_driver_register(struct ipack_driver *edrv, struct module *owner, |
9869a937 | 256 | const char *name) |
d3465872 | 257 | { |
ec440335 SIG |
258 | edrv->driver.owner = owner; |
259 | edrv->driver.name = name; | |
d3465872 SIG |
260 | edrv->driver.bus = &ipack_bus_type; |
261 | return driver_register(&edrv->driver); | |
262 | } | |
263 | EXPORT_SYMBOL_GPL(ipack_driver_register); | |
264 | ||
265 | void ipack_driver_unregister(struct ipack_driver *edrv) | |
266 | { | |
267 | driver_unregister(&edrv->driver); | |
268 | } | |
269 | EXPORT_SYMBOL_GPL(ipack_driver_unregister); | |
270 | ||
a92caeb8 JT |
271 | static u16 ipack_crc_byte(u16 crc, u8 c) |
272 | { | |
273 | int i; | |
274 | ||
275 | crc ^= c << 8; | |
276 | for (i = 0; i < 8; i++) | |
277 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0); | |
278 | return crc; | |
279 | } | |
280 | ||
281 | /* | |
282 | * The algorithm in lib/crc-ccitt.c does not seem to apply since it uses the | |
283 | * opposite bit ordering. | |
284 | */ | |
285 | static u8 ipack_calc_crc1(struct ipack_device *dev) | |
286 | { | |
287 | u8 c; | |
288 | u16 crc; | |
289 | unsigned int i; | |
290 | ||
291 | crc = 0xffff; | |
292 | for (i = 0; i < dev->id_avail; i++) { | |
293 | c = (i != 11) ? dev->id[i] : 0; | |
294 | crc = ipack_crc_byte(crc, c); | |
295 | } | |
296 | crc = ~crc; | |
297 | return crc & 0xff; | |
298 | } | |
299 | ||
300 | static u16 ipack_calc_crc2(struct ipack_device *dev) | |
301 | { | |
302 | u8 c; | |
303 | u16 crc; | |
304 | unsigned int i; | |
305 | ||
306 | crc = 0xffff; | |
307 | for (i = 0; i < dev->id_avail; i++) { | |
308 | c = ((i != 0x18) && (i != 0x19)) ? dev->id[i] : 0; | |
309 | crc = ipack_crc_byte(crc, c); | |
310 | } | |
311 | crc = ~crc; | |
312 | return crc; | |
313 | } | |
314 | ||
e8ed3276 JT |
315 | static void ipack_parse_id1(struct ipack_device *dev) |
316 | { | |
317 | u8 *id = dev->id; | |
a92caeb8 | 318 | u8 crc; |
e8ed3276 JT |
319 | |
320 | dev->id_vendor = id[4]; | |
321 | dev->id_device = id[5]; | |
0b0f3a1b JT |
322 | dev->speed_8mhz = 1; |
323 | dev->speed_32mhz = (id[7] == 'H'); | |
a92caeb8 JT |
324 | crc = ipack_calc_crc1(dev); |
325 | dev->id_crc_correct = (crc == id[11]); | |
326 | if (!dev->id_crc_correct) { | |
327 | dev_warn(&dev->dev, "ID CRC invalid found 0x%x, expected 0x%x.\n", | |
328 | id[11], crc); | |
329 | } | |
e8ed3276 JT |
330 | } |
331 | ||
332 | static void ipack_parse_id2(struct ipack_device *dev) | |
333 | { | |
334 | __be16 *id = (__be16 *) dev->id; | |
a92caeb8 | 335 | u16 flags, crc; |
e8ed3276 JT |
336 | |
337 | dev->id_vendor = ((be16_to_cpu(id[3]) & 0xff) << 16) | |
338 | + be16_to_cpu(id[4]); | |
339 | dev->id_device = be16_to_cpu(id[5]); | |
0b0f3a1b JT |
340 | flags = be16_to_cpu(id[10]); |
341 | dev->speed_8mhz = !!(flags & 2); | |
342 | dev->speed_32mhz = !!(flags & 4); | |
a92caeb8 JT |
343 | crc = ipack_calc_crc2(dev); |
344 | dev->id_crc_correct = (crc == be16_to_cpu(id[12])); | |
345 | if (!dev->id_crc_correct) { | |
346 | dev_warn(&dev->dev, "ID CRC invalid found 0x%x, expected 0x%x.\n", | |
347 | id[11], crc); | |
348 | } | |
e8ed3276 JT |
349 | } |
350 | ||
187e4782 JT |
351 | static int ipack_device_read_id(struct ipack_device *dev) |
352 | { | |
353 | u8 __iomem *idmem; | |
354 | int i; | |
355 | int ret = 0; | |
356 | ||
402228db JT |
357 | idmem = ioremap(dev->region[IPACK_ID_SPACE].start, |
358 | dev->region[IPACK_ID_SPACE].size); | |
359 | if (!idmem) { | |
187e4782 | 360 | dev_err(&dev->dev, "error mapping memory\n"); |
58b2c0ca | 361 | return -ENOMEM; |
187e4782 | 362 | } |
187e4782 JT |
363 | |
364 | /* Determine ID PROM Data Format. If we find the ids "IPAC" or "IPAH" | |
365 | * we are dealing with a IndustryPack format 1 device. If we detect | |
366 | * "VITA4 " (16 bit big endian formatted) we are dealing with a | |
367 | * IndustryPack format 2 device */ | |
368 | if ((ioread8(idmem + 1) == 'I') && | |
369 | (ioread8(idmem + 3) == 'P') && | |
370 | (ioread8(idmem + 5) == 'A') && | |
371 | ((ioread8(idmem + 7) == 'C') || | |
372 | (ioread8(idmem + 7) == 'H'))) { | |
373 | dev->id_format = IPACK_ID_VERSION_1; | |
374 | dev->id_avail = ioread8(idmem + 0x15); | |
375 | if ((dev->id_avail < 0x0c) || (dev->id_avail > 0x40)) { | |
376 | dev_warn(&dev->dev, "invalid id size"); | |
377 | dev->id_avail = 0x0c; | |
378 | } | |
379 | } else if ((ioread8(idmem + 0) == 'I') && | |
380 | (ioread8(idmem + 1) == 'V') && | |
381 | (ioread8(idmem + 2) == 'A') && | |
382 | (ioread8(idmem + 3) == 'T') && | |
383 | (ioread8(idmem + 4) == ' ') && | |
384 | (ioread8(idmem + 5) == '4')) { | |
385 | dev->id_format = IPACK_ID_VERSION_2; | |
386 | dev->id_avail = ioread16be(idmem + 0x16); | |
387 | if ((dev->id_avail < 0x1a) || (dev->id_avail > 0x40)) { | |
388 | dev_warn(&dev->dev, "invalid id size"); | |
389 | dev->id_avail = 0x1a; | |
390 | } | |
391 | } else { | |
392 | dev->id_format = IPACK_ID_VERSION_INVALID; | |
393 | dev->id_avail = 0; | |
394 | } | |
395 | ||
396 | if (!dev->id_avail) { | |
397 | ret = -ENODEV; | |
398 | goto out; | |
399 | } | |
400 | ||
401 | /* Obtain the amount of memory required to store a copy of the complete | |
402 | * ID ROM contents */ | |
403 | dev->id = kmalloc(dev->id_avail, GFP_KERNEL); | |
404 | if (!dev->id) { | |
405 | dev_err(&dev->dev, "dev->id alloc failed.\n"); | |
406 | ret = -ENOMEM; | |
407 | goto out; | |
408 | } | |
409 | for (i = 0; i < dev->id_avail; i++) { | |
410 | if (dev->id_format == IPACK_ID_VERSION_1) | |
411 | dev->id[i] = ioread8(idmem + (i << 1) + 1); | |
412 | else | |
413 | dev->id[i] = ioread8(idmem + i); | |
414 | } | |
415 | ||
e8ed3276 JT |
416 | /* now we can finally work with the copy */ |
417 | switch (dev->id_format) { | |
418 | case IPACK_ID_VERSION_1: | |
419 | ipack_parse_id1(dev); | |
420 | break; | |
421 | case IPACK_ID_VERSION_2: | |
422 | ipack_parse_id2(dev); | |
423 | break; | |
424 | } | |
425 | ||
187e4782 | 426 | out: |
402228db | 427 | iounmap(idmem); |
187e4782 JT |
428 | |
429 | return ret; | |
430 | } | |
431 | ||
e926301b | 432 | int ipack_device_init(struct ipack_device *dev) |
d3465872 SIG |
433 | { |
434 | int ret; | |
435 | ||
436 | dev->dev.bus = &ipack_bus_type; | |
437 | dev->dev.release = ipack_device_release; | |
1e91795c | 438 | dev->dev.parent = dev->bus->parent; |
d3465872 | 439 | dev_set_name(&dev->dev, |
f9e314d2 | 440 | "ipack-dev.%u.%u", dev->bus->bus_nr, dev->slot); |
e926301b | 441 | device_initialize(&dev->dev); |
d3465872 | 442 | |
1e91795c | 443 | if (dev->bus->ops->set_clockrate(dev, 8)) |
07766ab0 | 444 | dev_warn(&dev->dev, "failed to switch to 8 MHz operation for reading of device ID.\n"); |
1e91795c | 445 | if (dev->bus->ops->reset_timeout(dev)) |
8a3ae16e | 446 | dev_warn(&dev->dev, "failed to reset potential timeout."); |
07766ab0 | 447 | |
187e4782 JT |
448 | ret = ipack_device_read_id(dev); |
449 | if (ret < 0) { | |
450 | dev_err(&dev->dev, "error reading device id section.\n"); | |
1e91795c | 451 | return ret; |
187e4782 JT |
452 | } |
453 | ||
90cb6194 | 454 | /* if the device supports 32 MHz operation, use it. */ |
07766ab0 | 455 | if (dev->speed_32mhz) { |
1e91795c | 456 | ret = dev->bus->ops->set_clockrate(dev, 32); |
07766ab0 JT |
457 | if (ret < 0) |
458 | dev_err(&dev->dev, "failed to switch to 32 MHz operation.\n"); | |
459 | } | |
90cb6194 | 460 | |
e926301b SIG |
461 | return 0; |
462 | } | |
463 | EXPORT_SYMBOL_GPL(ipack_device_init); | |
d3465872 | 464 | |
e926301b SIG |
465 | int ipack_device_add(struct ipack_device *dev) |
466 | { | |
467 | return device_add(&dev->dev); | |
d3465872 | 468 | } |
e926301b | 469 | EXPORT_SYMBOL_GPL(ipack_device_add); |
d3465872 | 470 | |
e926301b | 471 | void ipack_device_del(struct ipack_device *dev) |
d3465872 | 472 | { |
e926301b SIG |
473 | device_del(&dev->dev); |
474 | ipack_put_device(dev); | |
d3465872 | 475 | } |
e926301b | 476 | EXPORT_SYMBOL_GPL(ipack_device_del); |
d3465872 | 477 | |
fa882867 SIG |
478 | void ipack_get_device(struct ipack_device *dev) |
479 | { | |
480 | get_device(&dev->dev); | |
481 | } | |
482 | EXPORT_SYMBOL_GPL(ipack_get_device); | |
483 | ||
484 | void ipack_put_device(struct ipack_device *dev) | |
485 | { | |
486 | put_device(&dev->dev); | |
487 | } | |
488 | EXPORT_SYMBOL_GPL(ipack_put_device); | |
489 | ||
d3465872 SIG |
490 | static int __init ipack_init(void) |
491 | { | |
3b86bb2e | 492 | ida_init(&ipack_ida); |
d3465872 SIG |
493 | return bus_register(&ipack_bus_type); |
494 | } | |
495 | ||
496 | static void __exit ipack_exit(void) | |
497 | { | |
498 | bus_unregister(&ipack_bus_type); | |
3b86bb2e | 499 | ida_destroy(&ipack_ida); |
d3465872 SIG |
500 | } |
501 | ||
502 | module_init(ipack_init); | |
503 | module_exit(ipack_exit); | |
504 | ||
505 | MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>"); | |
506 | MODULE_LICENSE("GPL"); | |
507 | MODULE_DESCRIPTION("Industry-pack bus core"); |