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