]>
Commit | Line | Data |
---|---|---|
0508ad1f JK |
1 | /* |
2 | * FSI core driver | |
3 | * | |
4 | * Copyright (C) IBM Corporation 2016 | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * 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 | ||
2b545cd8 | 16 | #include <linux/crc4.h> |
0508ad1f JK |
17 | #include <linux/device.h> |
18 | #include <linux/fsi.h> | |
09aecfab | 19 | #include <linux/idr.h> |
0508ad1f | 20 | #include <linux/module.h> |
2b545cd8 | 21 | #include <linux/slab.h> |
f7ade2a6 | 22 | #include <linux/bitops.h> |
0508ad1f | 23 | |
09aecfab JK |
24 | #include "fsi-master.h" |
25 | ||
f7ade2a6 JK |
26 | #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31) |
27 | #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16) | |
28 | #define FSI_SLAVE_CONF_SLOTS_SHIFT 16 | |
29 | #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12) | |
30 | #define FSI_SLAVE_CONF_VERSION_SHIFT 12 | |
31 | #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4) | |
32 | #define FSI_SLAVE_CONF_TYPE_SHIFT 4 | |
33 | #define FSI_SLAVE_CONF_CRC_SHIFT 4 | |
34 | #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0) | |
35 | #define FSI_SLAVE_CONF_DATA_BITS 28 | |
36 | ||
4efe37f4 JK |
37 | #define FSI_PEEK_BASE 0x410 |
38 | ||
f7ade2a6 JK |
39 | static const int engine_page_size = 0x400; |
40 | ||
2b37c3e2 CB |
41 | #define FSI_SLAVE_BASE 0x800 |
42 | ||
43 | /* | |
44 | * FSI slave engine control register offsets | |
45 | */ | |
46 | #define FSI_SMODE 0x0 /* R/W: Mode register */ | |
47 | ||
48 | /* | |
49 | * SMODE fields | |
50 | */ | |
51 | #define FSI_SMODE_WSC 0x80000000 /* Warm start done */ | |
52 | #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */ | |
53 | #define FSI_SMODE_SID_SHIFT 24 /* ID shift */ | |
54 | #define FSI_SMODE_SID_MASK 3 /* ID Mask */ | |
55 | #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */ | |
56 | #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */ | |
57 | #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */ | |
58 | #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */ | |
59 | #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */ | |
60 | #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */ | |
61 | ||
2b545cd8 JK |
62 | #define FSI_SLAVE_SIZE_23b 0x800000 |
63 | ||
09aecfab JK |
64 | static DEFINE_IDA(master_ida); |
65 | ||
faf0b116 JK |
66 | struct fsi_slave { |
67 | struct device dev; | |
68 | struct fsi_master *master; | |
69 | int id; | |
70 | int link; | |
71 | uint32_t size; /* size of slave address space */ | |
72 | }; | |
73 | ||
cd0fdb5c | 74 | #define to_fsi_master(d) container_of(d, struct fsi_master, dev) |
faf0b116 JK |
75 | #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev) |
76 | ||
014c2abc JK |
77 | static int fsi_master_read(struct fsi_master *master, int link, |
78 | uint8_t slave_id, uint32_t addr, void *val, size_t size); | |
79 | static int fsi_master_write(struct fsi_master *master, int link, | |
80 | uint8_t slave_id, uint32_t addr, const void *val, size_t size); | |
4efe37f4 JK |
81 | static int fsi_slave_read(struct fsi_slave *slave, uint32_t addr, |
82 | void *val, size_t size); | |
83 | static int fsi_slave_write(struct fsi_slave *slave, uint32_t addr, | |
84 | const void *val, size_t size); | |
85 | ||
86 | /* | |
87 | * fsi_device_read() / fsi_device_write() / fsi_device_peek() | |
88 | * | |
89 | * FSI endpoint-device support | |
90 | * | |
91 | * Read / write / peek accessors for a client | |
92 | * | |
93 | * Parameters: | |
94 | * dev: Structure passed to FSI client device drivers on probe(). | |
95 | * addr: FSI address of given device. Client should pass in its base address | |
96 | * plus desired offset to access its register space. | |
97 | * val: For read/peek this is the value read at the specified address. For | |
98 | * write this is value to write to the specified address. | |
99 | * The data in val must be FSI bus endian (big endian). | |
100 | * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes. | |
101 | * Addresses must be aligned on size boundaries or an error will result. | |
102 | */ | |
103 | int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val, | |
104 | size_t size) | |
105 | { | |
106 | if (addr > dev->size || size > dev->size || addr > dev->size - size) | |
107 | return -EINVAL; | |
108 | ||
109 | return fsi_slave_read(dev->slave, dev->addr + addr, val, size); | |
110 | } | |
111 | EXPORT_SYMBOL_GPL(fsi_device_read); | |
112 | ||
113 | int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val, | |
114 | size_t size) | |
115 | { | |
116 | if (addr > dev->size || size > dev->size || addr > dev->size - size) | |
117 | return -EINVAL; | |
118 | ||
119 | return fsi_slave_write(dev->slave, dev->addr + addr, val, size); | |
120 | } | |
121 | EXPORT_SYMBOL_GPL(fsi_device_write); | |
122 | ||
123 | int fsi_device_peek(struct fsi_device *dev, void *val) | |
124 | { | |
125 | uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t)); | |
014c2abc | 126 | |
4efe37f4 JK |
127 | return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t)); |
128 | } | |
f7ade2a6 JK |
129 | |
130 | static void fsi_device_release(struct device *_device) | |
131 | { | |
132 | struct fsi_device *device = to_fsi_dev(_device); | |
133 | ||
134 | kfree(device); | |
135 | } | |
136 | ||
137 | static struct fsi_device *fsi_create_device(struct fsi_slave *slave) | |
138 | { | |
139 | struct fsi_device *dev; | |
140 | ||
141 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
142 | if (!dev) | |
143 | return NULL; | |
144 | ||
145 | dev->dev.parent = &slave->dev; | |
146 | dev->dev.bus = &fsi_bus_type; | |
147 | dev->dev.release = fsi_device_release; | |
148 | ||
149 | return dev; | |
150 | } | |
151 | ||
414c1026 | 152 | /* FSI slave support */ |
014c2abc JK |
153 | static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp, |
154 | uint8_t *idp) | |
155 | { | |
156 | uint32_t addr = *addrp; | |
157 | uint8_t id = *idp; | |
158 | ||
159 | if (addr > slave->size) | |
160 | return -EINVAL; | |
161 | ||
162 | /* For 23 bit addressing, we encode the extra two bits in the slave | |
163 | * id (and the slave's actual ID needs to be 0). | |
164 | */ | |
165 | if (addr > 0x1fffff) { | |
166 | if (slave->id != 0) | |
167 | return -EINVAL; | |
168 | id = (addr >> 21) & 0x3; | |
169 | addr &= 0x1fffff; | |
170 | } | |
171 | ||
172 | *addrp = addr; | |
173 | *idp = id; | |
174 | return 0; | |
175 | } | |
176 | ||
177 | static int fsi_slave_read(struct fsi_slave *slave, uint32_t addr, | |
178 | void *val, size_t size) | |
179 | { | |
180 | uint8_t id = slave->id; | |
181 | int rc; | |
182 | ||
183 | rc = fsi_slave_calc_addr(slave, &addr, &id); | |
184 | if (rc) | |
185 | return rc; | |
186 | ||
187 | return fsi_master_read(slave->master, slave->link, id, | |
188 | addr, val, size); | |
189 | } | |
190 | ||
191 | static int fsi_slave_write(struct fsi_slave *slave, uint32_t addr, | |
192 | const void *val, size_t size) | |
193 | { | |
194 | uint8_t id = slave->id; | |
195 | int rc; | |
196 | ||
197 | rc = fsi_slave_calc_addr(slave, &addr, &id); | |
198 | if (rc) | |
199 | return rc; | |
200 | ||
201 | return fsi_master_write(slave->master, slave->link, id, | |
202 | addr, val, size); | |
203 | } | |
204 | ||
f7ade2a6 JK |
205 | static int fsi_slave_scan(struct fsi_slave *slave) |
206 | { | |
207 | uint32_t engine_addr; | |
208 | uint32_t conf; | |
209 | int rc, i; | |
210 | ||
211 | /* | |
212 | * scan engines | |
213 | * | |
214 | * We keep the peek mode and slave engines for the core; so start | |
215 | * at the third slot in the configuration table. We also need to | |
216 | * skip the chip ID entry at the start of the address space. | |
217 | */ | |
218 | engine_addr = engine_page_size * 3; | |
219 | for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) { | |
220 | uint8_t slots, version, type, crc; | |
221 | struct fsi_device *dev; | |
222 | ||
223 | rc = fsi_slave_read(slave, (i + 1) * sizeof(conf), | |
224 | &conf, sizeof(conf)); | |
225 | if (rc) { | |
226 | dev_warn(&slave->dev, | |
227 | "error reading slave registers\n"); | |
228 | return -1; | |
229 | } | |
230 | conf = be32_to_cpu(conf); | |
231 | ||
232 | crc = crc4(0, conf, 32); | |
233 | if (crc) { | |
234 | dev_warn(&slave->dev, | |
235 | "crc error in slave register at 0x%04x\n", | |
236 | i); | |
237 | return -1; | |
238 | } | |
239 | ||
240 | slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK) | |
241 | >> FSI_SLAVE_CONF_SLOTS_SHIFT; | |
242 | version = (conf & FSI_SLAVE_CONF_VERSION_MASK) | |
243 | >> FSI_SLAVE_CONF_VERSION_SHIFT; | |
244 | type = (conf & FSI_SLAVE_CONF_TYPE_MASK) | |
245 | >> FSI_SLAVE_CONF_TYPE_SHIFT; | |
246 | ||
247 | /* | |
248 | * Unused address areas are marked by a zero type value; this | |
249 | * skips the defined address areas | |
250 | */ | |
251 | if (type != 0 && slots != 0) { | |
252 | ||
253 | /* create device */ | |
254 | dev = fsi_create_device(slave); | |
255 | if (!dev) | |
256 | return -ENOMEM; | |
257 | ||
258 | dev->slave = slave; | |
259 | dev->engine_type = type; | |
260 | dev->version = version; | |
261 | dev->unit = i; | |
262 | dev->addr = engine_addr; | |
263 | dev->size = slots * engine_page_size; | |
264 | ||
265 | dev_dbg(&slave->dev, | |
266 | "engine[%i]: type %x, version %x, addr %x size %x\n", | |
267 | dev->unit, dev->engine_type, version, | |
268 | dev->addr, dev->size); | |
269 | ||
270 | dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x", | |
271 | slave->master->idx, slave->link, | |
272 | slave->id, i - 2); | |
273 | ||
274 | rc = device_register(&dev->dev); | |
275 | if (rc) { | |
276 | dev_warn(&slave->dev, "add failed: %d\n", rc); | |
277 | put_device(&dev->dev); | |
278 | } | |
279 | } | |
280 | ||
281 | engine_addr += slots * engine_page_size; | |
282 | ||
283 | if (!(conf & FSI_SLAVE_CONF_NEXT_MASK)) | |
284 | break; | |
285 | } | |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
125739cb JK |
290 | static ssize_t fsi_slave_sysfs_raw_read(struct file *file, |
291 | struct kobject *kobj, struct bin_attribute *attr, char *buf, | |
292 | loff_t off, size_t count) | |
293 | { | |
294 | struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); | |
295 | size_t total_len, read_len; | |
296 | int rc; | |
297 | ||
298 | if (off < 0) | |
299 | return -EINVAL; | |
300 | ||
301 | if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff) | |
302 | return -EINVAL; | |
303 | ||
304 | for (total_len = 0; total_len < count; total_len += read_len) { | |
305 | read_len = min_t(size_t, count, 4); | |
306 | read_len -= off & 0x3; | |
307 | ||
308 | rc = fsi_slave_read(slave, off, buf + total_len, read_len); | |
309 | if (rc) | |
310 | return rc; | |
311 | ||
312 | off += read_len; | |
313 | } | |
314 | ||
315 | return count; | |
316 | } | |
317 | ||
318 | static ssize_t fsi_slave_sysfs_raw_write(struct file *file, | |
319 | struct kobject *kobj, struct bin_attribute *attr, | |
320 | char *buf, loff_t off, size_t count) | |
321 | { | |
322 | struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); | |
323 | size_t total_len, write_len; | |
324 | int rc; | |
325 | ||
326 | if (off < 0) | |
327 | return -EINVAL; | |
328 | ||
329 | if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff) | |
330 | return -EINVAL; | |
331 | ||
332 | for (total_len = 0; total_len < count; total_len += write_len) { | |
333 | write_len = min_t(size_t, count, 4); | |
334 | write_len -= off & 0x3; | |
335 | ||
336 | rc = fsi_slave_write(slave, off, buf + total_len, write_len); | |
337 | if (rc) | |
338 | return rc; | |
339 | ||
340 | off += write_len; | |
341 | } | |
342 | ||
343 | return count; | |
344 | } | |
345 | ||
346 | static struct bin_attribute fsi_slave_raw_attr = { | |
347 | .attr = { | |
348 | .name = "raw", | |
349 | .mode = 0600, | |
350 | }, | |
351 | .size = 0, | |
352 | .read = fsi_slave_sysfs_raw_read, | |
353 | .write = fsi_slave_sysfs_raw_write, | |
354 | }; | |
355 | ||
356 | static ssize_t fsi_slave_sysfs_term_write(struct file *file, | |
357 | struct kobject *kobj, struct bin_attribute *attr, | |
358 | char *buf, loff_t off, size_t count) | |
359 | { | |
360 | struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj)); | |
361 | struct fsi_master *master = slave->master; | |
362 | ||
363 | if (!master->term) | |
364 | return -ENODEV; | |
365 | ||
366 | master->term(master, slave->link, slave->id); | |
367 | return count; | |
368 | } | |
369 | ||
370 | static struct bin_attribute fsi_slave_term_attr = { | |
371 | .attr = { | |
372 | .name = "term", | |
373 | .mode = 0200, | |
374 | }, | |
375 | .size = 0, | |
376 | .write = fsi_slave_sysfs_term_write, | |
377 | }; | |
378 | ||
2b37c3e2 CB |
379 | /* Encode slave local bus echo delay */ |
380 | static inline uint32_t fsi_smode_echodly(int x) | |
381 | { | |
382 | return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT; | |
383 | } | |
384 | ||
385 | /* Encode slave local bus send delay */ | |
386 | static inline uint32_t fsi_smode_senddly(int x) | |
387 | { | |
388 | return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT; | |
389 | } | |
390 | ||
391 | /* Encode slave local bus clock rate ratio */ | |
392 | static inline uint32_t fsi_smode_lbcrr(int x) | |
393 | { | |
394 | return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT; | |
395 | } | |
396 | ||
397 | /* Encode slave ID */ | |
398 | static inline uint32_t fsi_smode_sid(int x) | |
399 | { | |
400 | return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT; | |
401 | } | |
402 | ||
403 | static const uint32_t fsi_slave_smode(int id) | |
404 | { | |
405 | return FSI_SMODE_WSC | FSI_SMODE_ECRC | |
406 | | fsi_smode_sid(id) | |
407 | | fsi_smode_echodly(0xf) | fsi_smode_senddly(0xf) | |
408 | | fsi_smode_lbcrr(0x8); | |
409 | } | |
410 | ||
411 | static int fsi_slave_set_smode(struct fsi_master *master, int link, int id) | |
412 | { | |
413 | uint32_t smode; | |
414 | ||
415 | /* set our smode register with the slave ID field to 0; this enables | |
416 | * extended slave addressing | |
417 | */ | |
418 | smode = fsi_slave_smode(id); | |
419 | smode = cpu_to_be32(smode); | |
420 | ||
421 | return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SMODE, | |
422 | &smode, sizeof(smode)); | |
423 | } | |
424 | ||
2b545cd8 JK |
425 | static void fsi_slave_release(struct device *dev) |
426 | { | |
427 | struct fsi_slave *slave = to_fsi_slave(dev); | |
428 | ||
429 | kfree(slave); | |
430 | } | |
431 | ||
414c1026 JK |
432 | static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id) |
433 | { | |
2b545cd8 JK |
434 | struct fsi_slave *slave; |
435 | uint32_t chip_id; | |
436 | uint8_t crc; | |
437 | int rc; | |
438 | ||
439 | /* Currently, we only support single slaves on a link, and use the | |
440 | * full 23-bit address range | |
441 | */ | |
442 | if (id != 0) | |
443 | return -EINVAL; | |
444 | ||
445 | rc = fsi_master_read(master, link, id, 0, &chip_id, sizeof(chip_id)); | |
446 | if (rc) { | |
447 | dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n", | |
448 | link, id, rc); | |
449 | return -ENODEV; | |
450 | } | |
451 | chip_id = be32_to_cpu(chip_id); | |
452 | ||
453 | crc = crc4(0, chip_id, 32); | |
454 | if (crc) { | |
455 | dev_warn(&master->dev, "slave %02x:%02x invalid chip id CRC!\n", | |
456 | link, id); | |
457 | return -EIO; | |
458 | } | |
459 | ||
460 | dev_info(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n", | |
461 | chip_id, master->idx, link, id); | |
462 | ||
2b37c3e2 CB |
463 | rc = fsi_slave_set_smode(master, link, id); |
464 | if (rc) { | |
465 | dev_warn(&master->dev, | |
466 | "can't set smode on slave:%02x:%02x %d\n", | |
467 | link, id, rc); | |
468 | return -ENODEV; | |
469 | } | |
470 | ||
2b545cd8 JK |
471 | /* We can communicate with a slave; create the slave device and |
472 | * register. | |
473 | */ | |
474 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); | |
475 | if (!slave) | |
476 | return -ENOMEM; | |
477 | ||
478 | slave->master = master; | |
479 | slave->dev.parent = &master->dev; | |
480 | slave->dev.release = fsi_slave_release; | |
481 | slave->link = link; | |
482 | slave->id = id; | |
483 | slave->size = FSI_SLAVE_SIZE_23b; | |
484 | ||
485 | dev_set_name(&slave->dev, "slave@%02x:%02x", link, id); | |
486 | rc = device_register(&slave->dev); | |
487 | if (rc < 0) { | |
488 | dev_warn(&master->dev, "failed to create slave device: %d\n", | |
489 | rc); | |
490 | put_device(&slave->dev); | |
491 | return rc; | |
492 | } | |
493 | ||
125739cb JK |
494 | rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr); |
495 | if (rc) | |
496 | dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc); | |
497 | ||
498 | rc = device_create_bin_file(&slave->dev, &fsi_slave_term_attr); | |
499 | if (rc) | |
500 | dev_warn(&slave->dev, "failed to create term attr: %d\n", rc); | |
501 | ||
f7ade2a6 JK |
502 | rc = fsi_slave_scan(slave); |
503 | if (rc) | |
504 | dev_dbg(&master->dev, "failed during slave scan with: %d\n", | |
505 | rc); | |
414c1026 | 506 | |
2b545cd8 | 507 | return rc; |
414c1026 JK |
508 | } |
509 | ||
09aecfab | 510 | /* FSI master support */ |
014c2abc JK |
511 | static int fsi_check_access(uint32_t addr, size_t size) |
512 | { | |
513 | if (size != 1 && size != 2 && size != 4) | |
514 | return -EINVAL; | |
515 | ||
516 | if ((addr & 0x3) != (size & 0x3)) | |
517 | return -EINVAL; | |
518 | ||
519 | return 0; | |
520 | } | |
521 | ||
522 | static int fsi_master_read(struct fsi_master *master, int link, | |
523 | uint8_t slave_id, uint32_t addr, void *val, size_t size) | |
524 | { | |
525 | int rc; | |
526 | ||
527 | rc = fsi_check_access(addr, size); | |
528 | if (rc) | |
529 | return rc; | |
530 | ||
531 | return master->read(master, link, slave_id, addr, val, size); | |
532 | } | |
533 | ||
534 | static int fsi_master_write(struct fsi_master *master, int link, | |
535 | uint8_t slave_id, uint32_t addr, const void *val, size_t size) | |
536 | { | |
537 | int rc; | |
538 | ||
539 | rc = fsi_check_access(addr, size); | |
540 | if (rc) | |
541 | return rc; | |
542 | ||
543 | return master->write(master, link, slave_id, addr, val, size); | |
544 | } | |
545 | ||
26095282 CB |
546 | static int fsi_master_link_enable(struct fsi_master *master, int link) |
547 | { | |
548 | if (master->link_enable) | |
549 | return master->link_enable(master, link); | |
550 | ||
551 | return 0; | |
552 | } | |
553 | ||
554 | /* | |
555 | * Issue a break command on this link | |
556 | */ | |
557 | static int fsi_master_break(struct fsi_master *master, int link) | |
558 | { | |
559 | if (master->send_break) | |
560 | return master->send_break(master, link); | |
561 | ||
562 | return 0; | |
563 | } | |
564 | ||
414c1026 JK |
565 | static int fsi_master_scan(struct fsi_master *master) |
566 | { | |
26095282 CB |
567 | int link, rc; |
568 | ||
569 | for (link = 0; link < master->n_links; link++) { | |
570 | rc = fsi_master_link_enable(master, link); | |
571 | if (rc) { | |
572 | dev_dbg(&master->dev, | |
573 | "enable link %d failed: %d\n", link, rc); | |
574 | continue; | |
575 | } | |
576 | rc = fsi_master_break(master, link); | |
577 | if (rc) { | |
578 | dev_dbg(&master->dev, | |
579 | "break to link %d failed: %d\n", link, rc); | |
580 | continue; | |
581 | } | |
414c1026 | 582 | |
414c1026 | 583 | fsi_slave_init(master, link, 0); |
26095282 | 584 | } |
414c1026 JK |
585 | |
586 | return 0; | |
587 | } | |
588 | ||
cd0fdb5c CB |
589 | static int fsi_slave_remove_device(struct device *dev, void *arg) |
590 | { | |
591 | device_unregister(dev); | |
592 | return 0; | |
593 | } | |
594 | ||
595 | static int fsi_master_remove_slave(struct device *dev, void *arg) | |
596 | { | |
597 | device_for_each_child(dev, NULL, fsi_slave_remove_device); | |
598 | device_unregister(dev); | |
599 | return 0; | |
600 | } | |
601 | ||
602 | static void fsi_master_unscan(struct fsi_master *master) | |
603 | { | |
604 | device_for_each_child(&master->dev, NULL, fsi_master_remove_slave); | |
605 | } | |
606 | ||
607 | static ssize_t master_rescan_store(struct device *dev, | |
608 | struct device_attribute *attr, const char *buf, size_t count) | |
609 | { | |
610 | struct fsi_master *master = to_fsi_master(dev); | |
611 | int rc; | |
612 | ||
613 | fsi_master_unscan(master); | |
614 | rc = fsi_master_scan(master); | |
615 | if (rc < 0) | |
616 | return rc; | |
617 | ||
618 | return count; | |
619 | } | |
620 | ||
621 | static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store); | |
622 | ||
125739cb JK |
623 | static ssize_t master_break_store(struct device *dev, |
624 | struct device_attribute *attr, const char *buf, size_t count) | |
625 | { | |
626 | struct fsi_master *master = to_fsi_master(dev); | |
627 | ||
628 | fsi_master_break(master, 0); | |
629 | ||
630 | return count; | |
631 | } | |
632 | ||
633 | static DEVICE_ATTR(break, 0200, NULL, master_break_store); | |
634 | ||
09aecfab JK |
635 | int fsi_master_register(struct fsi_master *master) |
636 | { | |
637 | int rc; | |
638 | ||
639 | if (!master) | |
640 | return -EINVAL; | |
641 | ||
642 | master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL); | |
643 | dev_set_name(&master->dev, "fsi%d", master->idx); | |
644 | ||
645 | rc = device_register(&master->dev); | |
414c1026 | 646 | if (rc) { |
09aecfab | 647 | ida_simple_remove(&master_ida, master->idx); |
414c1026 JK |
648 | return rc; |
649 | } | |
09aecfab | 650 | |
cd0fdb5c CB |
651 | rc = device_create_file(&master->dev, &dev_attr_rescan); |
652 | if (rc) { | |
653 | device_unregister(&master->dev); | |
654 | ida_simple_remove(&master_ida, master->idx); | |
655 | return rc; | |
656 | } | |
657 | ||
125739cb JK |
658 | rc = device_create_file(&master->dev, &dev_attr_break); |
659 | if (rc) { | |
660 | device_unregister(&master->dev); | |
661 | ida_simple_remove(&master_ida, master->idx); | |
662 | return rc; | |
663 | } | |
664 | ||
414c1026 | 665 | fsi_master_scan(master); |
cd0fdb5c | 666 | |
414c1026 | 667 | return 0; |
09aecfab JK |
668 | } |
669 | EXPORT_SYMBOL_GPL(fsi_master_register); | |
670 | ||
671 | void fsi_master_unregister(struct fsi_master *master) | |
672 | { | |
673 | if (master->idx >= 0) { | |
674 | ida_simple_remove(&master_ida, master->idx); | |
675 | master->idx = -1; | |
676 | } | |
677 | ||
cd0fdb5c | 678 | fsi_master_unscan(master); |
09aecfab JK |
679 | device_unregister(&master->dev); |
680 | } | |
681 | EXPORT_SYMBOL_GPL(fsi_master_unregister); | |
682 | ||
0508ad1f JK |
683 | /* FSI core & Linux bus type definitions */ |
684 | ||
dd37eed7 JK |
685 | static int fsi_bus_match(struct device *dev, struct device_driver *drv) |
686 | { | |
687 | struct fsi_device *fsi_dev = to_fsi_dev(dev); | |
688 | struct fsi_driver *fsi_drv = to_fsi_drv(drv); | |
689 | const struct fsi_device_id *id; | |
690 | ||
691 | if (!fsi_drv->id_table) | |
692 | return 0; | |
693 | ||
694 | for (id = fsi_drv->id_table; id->engine_type; id++) { | |
695 | if (id->engine_type != fsi_dev->engine_type) | |
696 | continue; | |
697 | if (id->version == FSI_VERSION_ANY || | |
698 | id->version == fsi_dev->version) | |
699 | return 1; | |
700 | } | |
701 | ||
702 | return 0; | |
703 | } | |
704 | ||
356d8009 CB |
705 | int fsi_driver_register(struct fsi_driver *fsi_drv) |
706 | { | |
707 | if (!fsi_drv) | |
708 | return -EINVAL; | |
709 | if (!fsi_drv->id_table) | |
710 | return -EINVAL; | |
711 | ||
712 | return driver_register(&fsi_drv->drv); | |
713 | } | |
714 | EXPORT_SYMBOL_GPL(fsi_driver_register); | |
715 | ||
716 | void fsi_driver_unregister(struct fsi_driver *fsi_drv) | |
717 | { | |
718 | driver_unregister(&fsi_drv->drv); | |
719 | } | |
720 | EXPORT_SYMBOL_GPL(fsi_driver_unregister); | |
721 | ||
0508ad1f JK |
722 | struct bus_type fsi_bus_type = { |
723 | .name = "fsi", | |
dd37eed7 | 724 | .match = fsi_bus_match, |
0508ad1f JK |
725 | }; |
726 | EXPORT_SYMBOL_GPL(fsi_bus_type); | |
727 | ||
728 | static int fsi_init(void) | |
729 | { | |
730 | return bus_register(&fsi_bus_type); | |
731 | } | |
732 | ||
733 | static void fsi_exit(void) | |
734 | { | |
735 | bus_unregister(&fsi_bus_type); | |
736 | } | |
737 | ||
738 | module_init(fsi_init); | |
739 | module_exit(fsi_exit); |