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