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