fsi-core.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. * TODO:
  16. * - Rework topology
  17. * - s/chip_id/chip_loc
  18. * - s/cfam/chip (cfam_id -> chip_id etc...)
  19. */
  20. #include <linux/crc4.h>
  21. #include <linux/device.h>
  22. #include <linux/fsi.h>
  23. #include <linux/idr.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/slab.h>
  27. #include <linux/bitops.h>
  28. #include <linux/cdev.h>
  29. #include <linux/fs.h>
  30. #include <linux/uaccess.h>
  31. #include "fsi-master.h"
  32. #define CREATE_TRACE_POINTS
  33. #include <trace/events/fsi.h>
  34. #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31)
  35. #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16)
  36. #define FSI_SLAVE_CONF_SLOTS_SHIFT 16
  37. #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12)
  38. #define FSI_SLAVE_CONF_VERSION_SHIFT 12
  39. #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4)
  40. #define FSI_SLAVE_CONF_TYPE_SHIFT 4
  41. #define FSI_SLAVE_CONF_CRC_SHIFT 4
  42. #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0)
  43. #define FSI_SLAVE_CONF_DATA_BITS 28
  44. #define FSI_PEEK_BASE 0x410
  45. static const int engine_page_size = 0x400;
  46. #define FSI_SLAVE_BASE 0x800
  47. /*
  48. * FSI slave engine control register offsets
  49. */
  50. #define FSI_SMODE 0x0 /* R/W: Mode register */
  51. #define FSI_SISC 0x8 /* R/W: Interrupt condition */
  52. #define FSI_SSTAT 0x14 /* R : Slave status */
  53. #define FSI_LLMODE 0x100 /* R/W: Link layer mode register */
  54. /*
  55. * SMODE fields
  56. */
  57. #define FSI_SMODE_WSC 0x80000000 /* Warm start done */
  58. #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */
  59. #define FSI_SMODE_SID_SHIFT 24 /* ID shift */
  60. #define FSI_SMODE_SID_MASK 3 /* ID Mask */
  61. #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */
  62. #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */
  63. #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */
  64. #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */
  65. #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */
  66. #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */
  67. /*
  68. * LLMODE fields
  69. */
  70. #define FSI_LLMODE_ASYNC 0x1
  71. #define FSI_SLAVE_SIZE_23b 0x800000
  72. static DEFINE_IDA(master_ida);
  73. struct fsi_slave {
  74. struct device dev;
  75. struct fsi_master *master;
  76. struct cdev cdev;
  77. int cdev_idx;
  78. int id; /* FSI address */
  79. int link; /* FSI link# */
  80. u32 cfam_id;
  81. int chip_id;
  82. uint32_t size; /* size of slave address space */
  83. u8 t_send_delay;
  84. u8 t_echo_delay;
  85. };
  86. #define to_fsi_master(d) container_of(d, struct fsi_master, dev)
  87. #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)
  88. static const int slave_retries = 2;
  89. static int discard_errors;
  90. static dev_t fsi_base_dev;
  91. static DEFINE_IDA(fsi_minor_ida);
  92. #define FSI_CHAR_MAX_DEVICES 0x1000
  93. /* Legacy /dev numbering: 4 devices per chip, 16 chips */
  94. #define FSI_CHAR_LEGACY_TOP 64
  95. static int fsi_master_read(struct fsi_master *master, int link,
  96. uint8_t slave_id, uint32_t addr, void *val, size_t size);
  97. static int fsi_master_write(struct fsi_master *master, int link,
  98. uint8_t slave_id, uint32_t addr, const void *val, size_t size);
  99. static int fsi_master_break(struct fsi_master *master, int link);
  100. /*
  101. * fsi_device_read() / fsi_device_write() / fsi_device_peek()
  102. *
  103. * FSI endpoint-device support
  104. *
  105. * Read / write / peek accessors for a client
  106. *
  107. * Parameters:
  108. * dev: Structure passed to FSI client device drivers on probe().
  109. * addr: FSI address of given device. Client should pass in its base address
  110. * plus desired offset to access its register space.
  111. * val: For read/peek this is the value read at the specified address. For
  112. * write this is value to write to the specified address.
  113. * The data in val must be FSI bus endian (big endian).
  114. * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes.
  115. * Addresses must be aligned on size boundaries or an error will result.
  116. */
  117. int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val,
  118. size_t size)
  119. {
  120. if (addr > dev->size || size > dev->size || addr > dev->size - size)
  121. return -EINVAL;
  122. return fsi_slave_read(dev->slave, dev->addr + addr, val, size);
  123. }
  124. EXPORT_SYMBOL_GPL(fsi_device_read);
  125. int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val,
  126. size_t size)
  127. {
  128. if (addr > dev->size || size > dev->size || addr > dev->size - size)
  129. return -EINVAL;
  130. return fsi_slave_write(dev->slave, dev->addr + addr, val, size);
  131. }
  132. EXPORT_SYMBOL_GPL(fsi_device_write);
  133. int fsi_device_peek(struct fsi_device *dev, void *val)
  134. {
  135. uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t));
  136. return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t));
  137. }
  138. static void fsi_device_release(struct device *_device)
  139. {
  140. struct fsi_device *device = to_fsi_dev(_device);
  141. of_node_put(device->dev.of_node);
  142. kfree(device);
  143. }
  144. static struct fsi_device *fsi_create_device(struct fsi_slave *slave)
  145. {
  146. struct fsi_device *dev;
  147. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  148. if (!dev)
  149. return NULL;
  150. dev->dev.parent = &slave->dev;
  151. dev->dev.bus = &fsi_bus_type;
  152. dev->dev.release = fsi_device_release;
  153. return dev;
  154. }
  155. /* FSI slave support */
  156. static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp,
  157. uint8_t *idp)
  158. {
  159. uint32_t addr = *addrp;
  160. uint8_t id = *idp;
  161. if (addr > slave->size)
  162. return -EINVAL;
  163. /* For 23 bit addressing, we encode the extra two bits in the slave
  164. * id (and the slave's actual ID needs to be 0).
  165. */
  166. if (addr > 0x1fffff) {
  167. if (slave->id != 0)
  168. return -EINVAL;
  169. id = (addr >> 21) & 0x3;
  170. addr &= 0x1fffff;
  171. }
  172. *addrp = addr;
  173. *idp = id;
  174. return 0;
  175. }
  176. static int fsi_slave_report_and_clear_errors(struct fsi_slave *slave)
  177. {
  178. struct fsi_master *master = slave->master;
  179. __be32 irq, stat;
  180. int rc, link;
  181. uint8_t id;
  182. link = slave->link;
  183. id = slave->id;
  184. rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
  185. &irq, sizeof(irq));
  186. if (rc)
  187. return rc;
  188. rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT,
  189. &stat, sizeof(stat));
  190. if (rc)
  191. return rc;
  192. dev_dbg(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n",
  193. be32_to_cpu(stat), be32_to_cpu(irq));
  194. /* clear interrupts */
  195. return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
  196. &irq, sizeof(irq));
  197. }
  198. /* Encode slave local bus echo delay */
  199. static inline uint32_t fsi_smode_echodly(int x)
  200. {
  201. return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT;
  202. }
  203. /* Encode slave local bus send delay */
  204. static inline uint32_t fsi_smode_senddly(int x)
  205. {
  206. return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT;
  207. }
  208. /* Encode slave local bus clock rate ratio */
  209. static inline uint32_t fsi_smode_lbcrr(int x)
  210. {
  211. return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT;
  212. }
  213. /* Encode slave ID */
  214. static inline uint32_t fsi_smode_sid(int x)
  215. {
  216. return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT;
  217. }
  218. static uint32_t fsi_slave_smode(int id, u8 t_senddly, u8 t_echodly)
  219. {
  220. return FSI_SMODE_WSC | FSI_SMODE_ECRC
  221. | fsi_smode_sid(id)
  222. | fsi_smode_echodly(t_echodly - 1) | fsi_smode_senddly(t_senddly - 1)
  223. | fsi_smode_lbcrr(0x8);
  224. }
  225. static int fsi_slave_set_smode(struct fsi_slave *slave)
  226. {
  227. uint32_t smode;
  228. __be32 data;
  229. /* set our smode register with the slave ID field to 0; this enables
  230. * extended slave addressing
  231. */
  232. smode = fsi_slave_smode(slave->id, slave->t_send_delay, slave->t_echo_delay);
  233. data = cpu_to_be32(smode);
  234. return fsi_master_write(slave->master, slave->link, slave->id,
  235. FSI_SLAVE_BASE + FSI_SMODE,
  236. &data, sizeof(data));
  237. }
  238. static int fsi_slave_handle_error(struct fsi_slave *slave, bool write,
  239. uint32_t addr, size_t size)
  240. {
  241. struct fsi_master *master = slave->master;
  242. int rc, link;
  243. uint32_t reg;
  244. uint8_t id, send_delay, echo_delay;
  245. if (discard_errors)
  246. return -1;
  247. link = slave->link;
  248. id = slave->id;
  249. dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]",
  250. write ? "write" : "read", addr, size);
  251. /* try a simple clear of error conditions, which may fail if we've lost
  252. * communication with the slave
  253. */
  254. rc = fsi_slave_report_and_clear_errors(slave);
  255. if (!rc)
  256. return 0;
  257. /* send a TERM and retry */
  258. if (master->term) {
  259. rc = master->term(master, link, id);
  260. if (!rc) {
  261. rc = fsi_master_read(master, link, id, 0,
  262. &reg, sizeof(reg));
  263. if (!rc)
  264. rc = fsi_slave_report_and_clear_errors(slave);
  265. if (!rc)
  266. return 0;
  267. }
  268. }
  269. send_delay = slave->t_send_delay;
  270. echo_delay = slave->t_echo_delay;
  271. /* getting serious, reset the slave via BREAK */
  272. rc = fsi_master_break(master, link);
  273. if (rc)
  274. return rc;
  275. slave->t_send_delay = send_delay;
  276. slave->t_echo_delay = echo_delay;
  277. rc = fsi_slave_set_smode(slave);
  278. if (rc)
  279. return rc;
  280. if (master->link_config)
  281. master->link_config(master, link,
  282. slave->t_send_delay,
  283. slave->t_echo_delay);
  284. return fsi_slave_report_and_clear_errors(slave);
  285. }
  286. int fsi_slave_read(struct fsi_slave *slave, uint32_t addr,
  287. void *val, size_t size)
  288. {
  289. uint8_t id = slave->id;
  290. int rc, err_rc, i;
  291. rc = fsi_slave_calc_addr(slave, &addr, &id);
  292. if (rc)
  293. return rc;
  294. for (i = 0; i < slave_retries; i++) {
  295. rc = fsi_master_read(slave->master, slave->link,
  296. id, addr, val, size);
  297. if (!rc)
  298. break;
  299. err_rc = fsi_slave_handle_error(slave, false, addr, size);
  300. if (err_rc)
  301. break;
  302. }
  303. return rc;
  304. }
  305. EXPORT_SYMBOL_GPL(fsi_slave_read);
  306. int fsi_slave_write(struct fsi_slave *slave, uint32_t addr,
  307. const void *val, size_t size)
  308. {
  309. uint8_t id = slave->id;
  310. int rc, err_rc, i;
  311. rc = fsi_slave_calc_addr(slave, &addr, &id);
  312. if (rc)
  313. return rc;
  314. for (i = 0; i < slave_retries; i++) {
  315. rc = fsi_master_write(slave->master, slave->link,
  316. id, addr, val, size);
  317. if (!rc)
  318. break;
  319. err_rc = fsi_slave_handle_error(slave, true, addr, size);
  320. if (err_rc)
  321. break;
  322. }
  323. return rc;
  324. }
  325. EXPORT_SYMBOL_GPL(fsi_slave_write);
  326. extern int fsi_slave_claim_range(struct fsi_slave *slave,
  327. uint32_t addr, uint32_t size)
  328. {
  329. if (addr + size < addr)
  330. return -EINVAL;
  331. if (addr + size > slave->size)
  332. return -EINVAL;
  333. /* todo: check for overlapping claims */
  334. return 0;
  335. }
  336. EXPORT_SYMBOL_GPL(fsi_slave_claim_range);
  337. extern void fsi_slave_release_range(struct fsi_slave *slave,
  338. uint32_t addr, uint32_t size)
  339. {
  340. }
  341. EXPORT_SYMBOL_GPL(fsi_slave_release_range);
  342. static bool fsi_device_node_matches(struct device *dev, struct device_node *np,
  343. uint32_t addr, uint32_t size)
  344. {
  345. unsigned int len, na, ns;
  346. const __be32 *prop;
  347. uint32_t psize;
  348. na = of_n_addr_cells(np);
  349. ns = of_n_size_cells(np);
  350. if (na != 1 || ns != 1)
  351. return false;
  352. prop = of_get_property(np, "reg", &len);
  353. if (!prop || len != 8)
  354. return false;
  355. if (of_read_number(prop, 1) != addr)
  356. return false;
  357. psize = of_read_number(prop + 1, 1);
  358. if (psize != size) {
  359. dev_warn(dev,
  360. "node %s matches probed address, but not size (got 0x%x, expected 0x%x)",
  361. of_node_full_name(np), psize, size);
  362. }
  363. return true;
  364. }
  365. /* Find a matching node for the slave engine at @address, using @size bytes
  366. * of space. Returns NULL if not found, or a matching node with refcount
  367. * already incremented.
  368. */
  369. static struct device_node *fsi_device_find_of_node(struct fsi_device *dev)
  370. {
  371. struct device_node *parent, *np;
  372. parent = dev_of_node(&dev->slave->dev);
  373. if (!parent)
  374. return NULL;
  375. for_each_child_of_node(parent, np) {
  376. if (fsi_device_node_matches(&dev->dev, np,
  377. dev->addr, dev->size))
  378. return np;
  379. }
  380. return NULL;
  381. }
  382. static int fsi_slave_scan(struct fsi_slave *slave)
  383. {
  384. uint32_t engine_addr;
  385. int rc, i;
  386. /*
  387. * scan engines
  388. *
  389. * We keep the peek mode and slave engines for the core; so start
  390. * at the third slot in the configuration table. We also need to
  391. * skip the chip ID entry at the start of the address space.
  392. */
  393. engine_addr = engine_page_size * 3;
  394. for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) {
  395. uint8_t slots, version, type, crc;
  396. struct fsi_device *dev;
  397. uint32_t conf;
  398. __be32 data;
  399. rc = fsi_slave_read(slave, (i + 1) * sizeof(data),
  400. &data, sizeof(data));
  401. if (rc) {
  402. dev_warn(&slave->dev,
  403. "error reading slave registers\n");
  404. return -1;
  405. }
  406. conf = be32_to_cpu(data);
  407. crc = crc4(0, conf, 32);
  408. if (crc) {
  409. dev_warn(&slave->dev,
  410. "crc error in slave register at 0x%04x\n",
  411. i);
  412. return -1;
  413. }
  414. slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK)
  415. >> FSI_SLAVE_CONF_SLOTS_SHIFT;
  416. version = (conf & FSI_SLAVE_CONF_VERSION_MASK)
  417. >> FSI_SLAVE_CONF_VERSION_SHIFT;
  418. type = (conf & FSI_SLAVE_CONF_TYPE_MASK)
  419. >> FSI_SLAVE_CONF_TYPE_SHIFT;
  420. /*
  421. * Unused address areas are marked by a zero type value; this
  422. * skips the defined address areas
  423. */
  424. if (type != 0 && slots != 0) {
  425. /* create device */
  426. dev = fsi_create_device(slave);
  427. if (!dev)
  428. return -ENOMEM;
  429. dev->slave = slave;
  430. dev->engine_type = type;
  431. dev->version = version;
  432. dev->unit = i;
  433. dev->addr = engine_addr;
  434. dev->size = slots * engine_page_size;
  435. dev_dbg(&slave->dev,
  436. "engine[%i]: type %x, version %x, addr %x size %x\n",
  437. dev->unit, dev->engine_type, version,
  438. dev->addr, dev->size);
  439. dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x",
  440. slave->master->idx, slave->link,
  441. slave->id, i - 2);
  442. dev->dev.of_node = fsi_device_find_of_node(dev);
  443. rc = device_register(&dev->dev);
  444. if (rc) {
  445. dev_warn(&slave->dev, "add failed: %d\n", rc);
  446. put_device(&dev->dev);
  447. }
  448. }
  449. engine_addr += slots * engine_page_size;
  450. if (!(conf & FSI_SLAVE_CONF_NEXT_MASK))
  451. break;
  452. }
  453. return 0;
  454. }
  455. static unsigned long aligned_access_size(size_t offset, size_t count)
  456. {
  457. unsigned long offset_unit, count_unit;
  458. /* Criteria:
  459. *
  460. * 1. Access size must be less than or equal to the maximum access
  461. * width or the highest power-of-two factor of offset
  462. * 2. Access size must be less than or equal to the amount specified by
  463. * count
  464. *
  465. * The access width is optimal if we can calculate 1 to be strictly
  466. * equal while still satisfying 2.
  467. */
  468. /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */
  469. offset_unit = BIT(__builtin_ctzl(offset | 4));
  470. /* Find 2 by the top bit of count */
  471. count_unit = BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count));
  472. /* Constrain the maximum access width to the minimum of both criteria */
  473. return BIT(__builtin_ctzl(offset_unit | count_unit));
  474. }
  475. static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
  476. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  477. loff_t off, size_t count)
  478. {
  479. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  480. size_t total_len, read_len;
  481. int rc;
  482. if (off < 0)
  483. return -EINVAL;
  484. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  485. return -EINVAL;
  486. for (total_len = 0; total_len < count; total_len += read_len) {
  487. read_len = aligned_access_size(off, count - total_len);
  488. rc = fsi_slave_read(slave, off, buf + total_len, read_len);
  489. if (rc)
  490. return rc;
  491. off += read_len;
  492. }
  493. return count;
  494. }
  495. static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
  496. struct kobject *kobj, struct bin_attribute *attr,
  497. char *buf, loff_t off, size_t count)
  498. {
  499. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  500. size_t total_len, write_len;
  501. int rc;
  502. if (off < 0)
  503. return -EINVAL;
  504. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  505. return -EINVAL;
  506. for (total_len = 0; total_len < count; total_len += write_len) {
  507. write_len = aligned_access_size(off, count - total_len);
  508. rc = fsi_slave_write(slave, off, buf + total_len, write_len);
  509. if (rc)
  510. return rc;
  511. off += write_len;
  512. }
  513. return count;
  514. }
  515. static const struct bin_attribute fsi_slave_raw_attr = {
  516. .attr = {
  517. .name = "raw",
  518. .mode = 0600,
  519. },
  520. .size = 0,
  521. .read = fsi_slave_sysfs_raw_read,
  522. .write = fsi_slave_sysfs_raw_write,
  523. };
  524. static void fsi_slave_release(struct device *dev)
  525. {
  526. struct fsi_slave *slave = to_fsi_slave(dev);
  527. fsi_free_minor(slave->dev.devt);
  528. of_node_put(dev->of_node);
  529. kfree(slave);
  530. }
  531. static bool fsi_slave_node_matches(struct device_node *np,
  532. int link, uint8_t id)
  533. {
  534. unsigned int len, na, ns;
  535. const __be32 *prop;
  536. na = of_n_addr_cells(np);
  537. ns = of_n_size_cells(np);
  538. /* Ensure we have the correct format for addresses and sizes in
  539. * reg properties
  540. */
  541. if (na != 2 || ns != 0)
  542. return false;
  543. prop = of_get_property(np, "reg", &len);
  544. if (!prop || len != 8)
  545. return false;
  546. return (of_read_number(prop, 1) == link) &&
  547. (of_read_number(prop + 1, 1) == id);
  548. }
  549. /* Find a matching node for the slave at (link, id). Returns NULL if none
  550. * found, or a matching node with refcount already incremented.
  551. */
  552. static struct device_node *fsi_slave_find_of_node(struct fsi_master *master,
  553. int link, uint8_t id)
  554. {
  555. struct device_node *parent, *np;
  556. parent = dev_of_node(&master->dev);
  557. if (!parent)
  558. return NULL;
  559. for_each_child_of_node(parent, np) {
  560. if (fsi_slave_node_matches(np, link, id))
  561. return np;
  562. }
  563. return NULL;
  564. }
  565. static ssize_t cfam_read(struct file *filep, char __user *buf, size_t count,
  566. loff_t *offset)
  567. {
  568. struct fsi_slave *slave = filep->private_data;
  569. size_t total_len, read_len;
  570. loff_t off = *offset;
  571. ssize_t rc;
  572. if (off < 0)
  573. return -EINVAL;
  574. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  575. return -EINVAL;
  576. for (total_len = 0; total_len < count; total_len += read_len) {
  577. __be32 data;
  578. read_len = min_t(size_t, count, 4);
  579. read_len -= off & 0x3;
  580. rc = fsi_slave_read(slave, off, &data, read_len);
  581. if (rc)
  582. goto fail;
  583. rc = copy_to_user(buf + total_len, &data, read_len);
  584. if (rc) {
  585. rc = -EFAULT;
  586. goto fail;
  587. }
  588. off += read_len;
  589. }
  590. rc = count;
  591. fail:
  592. *offset = off;
  593. return count;
  594. }
  595. static ssize_t cfam_write(struct file *filep, const char __user *buf,
  596. size_t count, loff_t *offset)
  597. {
  598. struct fsi_slave *slave = filep->private_data;
  599. size_t total_len, write_len;
  600. loff_t off = *offset;
  601. ssize_t rc;
  602. if (off < 0)
  603. return -EINVAL;
  604. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  605. return -EINVAL;
  606. for (total_len = 0; total_len < count; total_len += write_len) {
  607. __be32 data;
  608. write_len = min_t(size_t, count, 4);
  609. write_len -= off & 0x3;
  610. rc = copy_from_user(&data, buf + total_len, write_len);
  611. if (rc) {
  612. rc = -EFAULT;
  613. goto fail;
  614. }
  615. rc = fsi_slave_write(slave, off, &data, write_len);
  616. if (rc)
  617. goto fail;
  618. off += write_len;
  619. }
  620. rc = count;
  621. fail:
  622. *offset = off;
  623. return count;
  624. }
  625. static loff_t cfam_llseek(struct file *file, loff_t offset, int whence)
  626. {
  627. switch (whence) {
  628. case SEEK_CUR:
  629. break;
  630. case SEEK_SET:
  631. file->f_pos = offset;
  632. break;
  633. default:
  634. return -EINVAL;
  635. }
  636. return offset;
  637. }
  638. static int cfam_open(struct inode *inode, struct file *file)
  639. {
  640. struct fsi_slave *slave = container_of(inode->i_cdev, struct fsi_slave, cdev);
  641. file->private_data = slave;
  642. return 0;
  643. }
  644. static const struct file_operations cfam_fops = {
  645. .owner = THIS_MODULE,
  646. .open = cfam_open,
  647. .llseek = cfam_llseek,
  648. .read = cfam_read,
  649. .write = cfam_write,
  650. };
  651. static ssize_t send_term_store(struct device *dev,
  652. struct device_attribute *attr,
  653. const char *buf, size_t count)
  654. {
  655. struct fsi_slave *slave = to_fsi_slave(dev);
  656. struct fsi_master *master = slave->master;
  657. if (!master->term)
  658. return -ENODEV;
  659. master->term(master, slave->link, slave->id);
  660. return count;
  661. }
  662. static DEVICE_ATTR_WO(send_term);
  663. static ssize_t slave_send_echo_show(struct device *dev,
  664. struct device_attribute *attr,
  665. char *buf)
  666. {
  667. struct fsi_slave *slave = to_fsi_slave(dev);
  668. return sprintf(buf, "%u\n", slave->t_send_delay);
  669. }
  670. static ssize_t slave_send_echo_store(struct device *dev,
  671. struct device_attribute *attr, const char *buf, size_t count)
  672. {
  673. struct fsi_slave *slave = to_fsi_slave(dev);
  674. struct fsi_master *master = slave->master;
  675. unsigned long val;
  676. int rc;
  677. if (kstrtoul(buf, 0, &val) < 0)
  678. return -EINVAL;
  679. if (val < 1 || val > 16)
  680. return -EINVAL;
  681. if (!master->link_config)
  682. return -ENXIO;
  683. /* Current HW mandates that send and echo delay are identical */
  684. slave->t_send_delay = val;
  685. slave->t_echo_delay = val;
  686. rc = fsi_slave_set_smode(slave);
  687. if (rc < 0)
  688. return rc;
  689. if (master->link_config)
  690. master->link_config(master, slave->link,
  691. slave->t_send_delay,
  692. slave->t_echo_delay);
  693. return count;
  694. }
  695. static DEVICE_ATTR(send_echo_delays, 0600,
  696. slave_send_echo_show, slave_send_echo_store);
  697. static ssize_t chip_id_show(struct device *dev,
  698. struct device_attribute *attr,
  699. char *buf)
  700. {
  701. struct fsi_slave *slave = to_fsi_slave(dev);
  702. return sprintf(buf, "%d\n", slave->chip_id);
  703. }
  704. static DEVICE_ATTR_RO(chip_id);
  705. static ssize_t cfam_id_show(struct device *dev,
  706. struct device_attribute *attr,
  707. char *buf)
  708. {
  709. struct fsi_slave *slave = to_fsi_slave(dev);
  710. return sprintf(buf, "0x%x\n", slave->cfam_id);
  711. }
  712. static DEVICE_ATTR_RO(cfam_id);
  713. static struct attribute *cfam_attr[] = {
  714. &dev_attr_send_echo_delays.attr,
  715. &dev_attr_chip_id.attr,
  716. &dev_attr_cfam_id.attr,
  717. &dev_attr_send_term.attr,
  718. NULL,
  719. };
  720. static const struct attribute_group cfam_attr_group = {
  721. .attrs = cfam_attr,
  722. };
  723. static const struct attribute_group *cfam_attr_groups[] = {
  724. &cfam_attr_group,
  725. NULL,
  726. };
  727. static char *cfam_devnode(struct device *dev, umode_t *mode,
  728. kuid_t *uid, kgid_t *gid)
  729. {
  730. struct fsi_slave *slave = to_fsi_slave(dev);
  731. #ifdef CONFIG_FSI_NEW_DEV_NODE
  732. return kasprintf(GFP_KERNEL, "fsi/cfam%d", slave->cdev_idx);
  733. #else
  734. return kasprintf(GFP_KERNEL, "cfam%d", slave->cdev_idx);
  735. #endif
  736. }
  737. static const struct device_type cfam_type = {
  738. .name = "cfam",
  739. .devnode = cfam_devnode,
  740. .groups = cfam_attr_groups
  741. };
  742. static char *fsi_cdev_devnode(struct device *dev, umode_t *mode,
  743. kuid_t *uid, kgid_t *gid)
  744. {
  745. #ifdef CONFIG_FSI_NEW_DEV_NODE
  746. return kasprintf(GFP_KERNEL, "fsi/%s", dev_name(dev));
  747. #else
  748. return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
  749. #endif
  750. }
  751. const struct device_type fsi_cdev_type = {
  752. .name = "fsi-cdev",
  753. .devnode = fsi_cdev_devnode,
  754. };
  755. EXPORT_SYMBOL_GPL(fsi_cdev_type);
  756. /* Backward compatible /dev/ numbering in "old style" mode */
  757. static int fsi_adjust_index(int index)
  758. {
  759. #ifdef CONFIG_FSI_NEW_DEV_NODE
  760. return index;
  761. #else
  762. return index + 1;
  763. #endif
  764. }
  765. static int __fsi_get_new_minor(struct fsi_slave *slave, enum fsi_dev_type type,
  766. dev_t *out_dev, int *out_index)
  767. {
  768. int cid = slave->chip_id;
  769. int id;
  770. /* Check if we qualify for legacy numbering */
  771. if (cid >= 0 && cid < 16 && type < 4) {
  772. /* Try reserving the legacy number */
  773. id = (cid << 4) | type;
  774. id = ida_simple_get(&fsi_minor_ida, id, id + 1, GFP_KERNEL);
  775. if (id >= 0) {
  776. *out_index = fsi_adjust_index(cid);
  777. *out_dev = fsi_base_dev + id;
  778. return 0;
  779. }
  780. /* Other failure */
  781. if (id != -ENOSPC)
  782. return id;
  783. /* Fallback to non-legacy allocation */
  784. }
  785. id = ida_simple_get(&fsi_minor_ida, FSI_CHAR_LEGACY_TOP,
  786. FSI_CHAR_MAX_DEVICES, GFP_KERNEL);
  787. if (id < 0)
  788. return id;
  789. *out_index = fsi_adjust_index(id);
  790. *out_dev = fsi_base_dev + id;
  791. return 0;
  792. }
  793. int fsi_get_new_minor(struct fsi_device *fdev, enum fsi_dev_type type,
  794. dev_t *out_dev, int *out_index)
  795. {
  796. return __fsi_get_new_minor(fdev->slave, type, out_dev, out_index);
  797. }
  798. EXPORT_SYMBOL_GPL(fsi_get_new_minor);
  799. void fsi_free_minor(dev_t dev)
  800. {
  801. ida_simple_remove(&fsi_minor_ida, MINOR(dev));
  802. }
  803. EXPORT_SYMBOL_GPL(fsi_free_minor);
  804. static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
  805. {
  806. uint32_t cfam_id;
  807. struct fsi_slave *slave;
  808. uint8_t crc;
  809. __be32 data, llmode;
  810. int rc;
  811. /* Currently, we only support single slaves on a link, and use the
  812. * full 23-bit address range
  813. */
  814. if (id != 0)
  815. return -EINVAL;
  816. rc = fsi_master_read(master, link, id, 0, &data, sizeof(data));
  817. if (rc) {
  818. dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
  819. link, id, rc);
  820. return -ENODEV;
  821. }
  822. cfam_id = be32_to_cpu(data);
  823. crc = crc4(0, cfam_id, 32);
  824. if (crc) {
  825. dev_warn(&master->dev, "slave %02x:%02x invalid cfam id CRC!\n",
  826. link, id);
  827. return -EIO;
  828. }
  829. dev_dbg(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
  830. cfam_id, master->idx, link, id);
  831. /* If we're behind a master that doesn't provide a self-running bus
  832. * clock, put the slave into async mode
  833. */
  834. if (master->flags & FSI_MASTER_FLAG_SWCLOCK) {
  835. llmode = cpu_to_be32(FSI_LLMODE_ASYNC);
  836. rc = fsi_master_write(master, link, id,
  837. FSI_SLAVE_BASE + FSI_LLMODE,
  838. &llmode, sizeof(llmode));
  839. if (rc)
  840. dev_warn(&master->dev,
  841. "can't set llmode on slave:%02x:%02x %d\n",
  842. link, id, rc);
  843. }
  844. /* We can communicate with a slave; create the slave device and
  845. * register.
  846. */
  847. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  848. if (!slave)
  849. return -ENOMEM;
  850. dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
  851. slave->dev.type = &cfam_type;
  852. slave->dev.parent = &master->dev;
  853. slave->dev.of_node = fsi_slave_find_of_node(master, link, id);
  854. slave->dev.release = fsi_slave_release;
  855. device_initialize(&slave->dev);
  856. slave->cfam_id = cfam_id;
  857. slave->master = master;
  858. slave->link = link;
  859. slave->id = id;
  860. slave->size = FSI_SLAVE_SIZE_23b;
  861. slave->t_send_delay = 16;
  862. slave->t_echo_delay = 16;
  863. /* Get chip ID if any */
  864. slave->chip_id = -1;
  865. if (slave->dev.of_node) {
  866. uint32_t prop;
  867. if (!of_property_read_u32(slave->dev.of_node, "chip-id", &prop))
  868. slave->chip_id = prop;
  869. }
  870. rc = fsi_slave_set_smode(slave);
  871. if (rc) {
  872. dev_warn(&master->dev,
  873. "can't set smode on slave:%02x:%02x %d\n",
  874. link, id, rc);
  875. goto err_free;
  876. }
  877. /* Allocate a minor in the FSI space */
  878. rc = __fsi_get_new_minor(slave, fsi_dev_cfam, &slave->dev.devt,
  879. &slave->cdev_idx);
  880. if (rc)
  881. goto err_free;
  882. /* Create chardev for userspace access */
  883. cdev_init(&slave->cdev, &cfam_fops);
  884. rc = cdev_device_add(&slave->cdev, &slave->dev);
  885. if (rc) {
  886. dev_err(&slave->dev, "Error %d creating slave device\n", rc);
  887. goto err_free_ida;
  888. }
  889. /* Now that we have the cdev registered with the core, any fatal
  890. * failures beyond this point will need to clean up through
  891. * cdev_device_del(). Fortunately though, nothing past here is fatal.
  892. */
  893. if (master->link_config)
  894. master->link_config(master, link,
  895. slave->t_send_delay,
  896. slave->t_echo_delay);
  897. /* Legacy raw file -> to be removed */
  898. rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
  899. if (rc)
  900. dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
  901. rc = fsi_slave_scan(slave);
  902. if (rc)
  903. dev_dbg(&master->dev, "failed during slave scan with: %d\n",
  904. rc);
  905. return 0;
  906. err_free_ida:
  907. fsi_free_minor(slave->dev.devt);
  908. err_free:
  909. of_node_put(slave->dev.of_node);
  910. kfree(slave);
  911. return rc;
  912. }
  913. /* FSI master support */
  914. static int fsi_check_access(uint32_t addr, size_t size)
  915. {
  916. if (size == 4) {
  917. if (addr & 0x3)
  918. return -EINVAL;
  919. } else if (size == 2) {
  920. if (addr & 0x1)
  921. return -EINVAL;
  922. } else if (size != 1)
  923. return -EINVAL;
  924. return 0;
  925. }
  926. static int fsi_master_read(struct fsi_master *master, int link,
  927. uint8_t slave_id, uint32_t addr, void *val, size_t size)
  928. {
  929. int rc;
  930. trace_fsi_master_read(master, link, slave_id, addr, size);
  931. rc = fsi_check_access(addr, size);
  932. if (!rc)
  933. rc = master->read(master, link, slave_id, addr, val, size);
  934. trace_fsi_master_rw_result(master, link, slave_id, addr, size,
  935. false, val, rc);
  936. return rc;
  937. }
  938. static int fsi_master_write(struct fsi_master *master, int link,
  939. uint8_t slave_id, uint32_t addr, const void *val, size_t size)
  940. {
  941. int rc;
  942. trace_fsi_master_write(master, link, slave_id, addr, size, val);
  943. rc = fsi_check_access(addr, size);
  944. if (!rc)
  945. rc = master->write(master, link, slave_id, addr, val, size);
  946. trace_fsi_master_rw_result(master, link, slave_id, addr, size,
  947. true, val, rc);
  948. return rc;
  949. }
  950. static int fsi_master_link_enable(struct fsi_master *master, int link)
  951. {
  952. if (master->link_enable)
  953. return master->link_enable(master, link);
  954. return 0;
  955. }
  956. /*
  957. * Issue a break command on this link
  958. */
  959. static int fsi_master_break(struct fsi_master *master, int link)
  960. {
  961. int rc = 0;
  962. trace_fsi_master_break(master, link);
  963. if (master->send_break)
  964. rc = master->send_break(master, link);
  965. if (master->link_config)
  966. master->link_config(master, link, 16, 16);
  967. return rc;
  968. }
  969. static int fsi_master_scan(struct fsi_master *master)
  970. {
  971. int link, rc;
  972. for (link = 0; link < master->n_links; link++) {
  973. rc = fsi_master_link_enable(master, link);
  974. if (rc) {
  975. dev_dbg(&master->dev,
  976. "enable link %d failed: %d\n", link, rc);
  977. continue;
  978. }
  979. rc = fsi_master_break(master, link);
  980. if (rc) {
  981. dev_dbg(&master->dev,
  982. "break to link %d failed: %d\n", link, rc);
  983. continue;
  984. }
  985. fsi_slave_init(master, link, 0);
  986. }
  987. return 0;
  988. }
  989. static int fsi_slave_remove_device(struct device *dev, void *arg)
  990. {
  991. device_unregister(dev);
  992. return 0;
  993. }
  994. static int fsi_master_remove_slave(struct device *dev, void *arg)
  995. {
  996. struct fsi_slave *slave = to_fsi_slave(dev);
  997. device_for_each_child(dev, NULL, fsi_slave_remove_device);
  998. cdev_device_del(&slave->cdev, &slave->dev);
  999. put_device(dev);
  1000. return 0;
  1001. }
  1002. static void fsi_master_unscan(struct fsi_master *master)
  1003. {
  1004. device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
  1005. }
  1006. int fsi_master_rescan(struct fsi_master *master)
  1007. {
  1008. int rc;
  1009. mutex_lock(&master->scan_lock);
  1010. fsi_master_unscan(master);
  1011. rc = fsi_master_scan(master);
  1012. mutex_unlock(&master->scan_lock);
  1013. return rc;
  1014. }
  1015. EXPORT_SYMBOL_GPL(fsi_master_rescan);
  1016. static ssize_t master_rescan_store(struct device *dev,
  1017. struct device_attribute *attr, const char *buf, size_t count)
  1018. {
  1019. struct fsi_master *master = to_fsi_master(dev);
  1020. int rc;
  1021. rc = fsi_master_rescan(master);
  1022. if (rc < 0)
  1023. return rc;
  1024. return count;
  1025. }
  1026. static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
  1027. static ssize_t master_break_store(struct device *dev,
  1028. struct device_attribute *attr, const char *buf, size_t count)
  1029. {
  1030. struct fsi_master *master = to_fsi_master(dev);
  1031. fsi_master_break(master, 0);
  1032. return count;
  1033. }
  1034. static DEVICE_ATTR(break, 0200, NULL, master_break_store);
  1035. int fsi_master_register(struct fsi_master *master)
  1036. {
  1037. int rc;
  1038. struct device_node *np;
  1039. mutex_init(&master->scan_lock);
  1040. master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
  1041. dev_set_name(&master->dev, "fsi%d", master->idx);
  1042. rc = device_register(&master->dev);
  1043. if (rc) {
  1044. ida_simple_remove(&master_ida, master->idx);
  1045. return rc;
  1046. }
  1047. rc = device_create_file(&master->dev, &dev_attr_rescan);
  1048. if (rc) {
  1049. device_del(&master->dev);
  1050. ida_simple_remove(&master_ida, master->idx);
  1051. return rc;
  1052. }
  1053. rc = device_create_file(&master->dev, &dev_attr_break);
  1054. if (rc) {
  1055. device_del(&master->dev);
  1056. ida_simple_remove(&master_ida, master->idx);
  1057. return rc;
  1058. }
  1059. np = dev_of_node(&master->dev);
  1060. if (!of_property_read_bool(np, "no-scan-on-init")) {
  1061. mutex_lock(&master->scan_lock);
  1062. fsi_master_scan(master);
  1063. mutex_unlock(&master->scan_lock);
  1064. }
  1065. return 0;
  1066. }
  1067. EXPORT_SYMBOL_GPL(fsi_master_register);
  1068. void fsi_master_unregister(struct fsi_master *master)
  1069. {
  1070. if (master->idx >= 0) {
  1071. ida_simple_remove(&master_ida, master->idx);
  1072. master->idx = -1;
  1073. }
  1074. mutex_lock(&master->scan_lock);
  1075. fsi_master_unscan(master);
  1076. mutex_unlock(&master->scan_lock);
  1077. device_unregister(&master->dev);
  1078. }
  1079. EXPORT_SYMBOL_GPL(fsi_master_unregister);
  1080. /* FSI core & Linux bus type definitions */
  1081. static int fsi_bus_match(struct device *dev, struct device_driver *drv)
  1082. {
  1083. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  1084. struct fsi_driver *fsi_drv = to_fsi_drv(drv);
  1085. const struct fsi_device_id *id;
  1086. if (!fsi_drv->id_table)
  1087. return 0;
  1088. for (id = fsi_drv->id_table; id->engine_type; id++) {
  1089. if (id->engine_type != fsi_dev->engine_type)
  1090. continue;
  1091. if (id->version == FSI_VERSION_ANY ||
  1092. id->version == fsi_dev->version)
  1093. return 1;
  1094. }
  1095. return 0;
  1096. }
  1097. int fsi_driver_register(struct fsi_driver *fsi_drv)
  1098. {
  1099. if (!fsi_drv)
  1100. return -EINVAL;
  1101. if (!fsi_drv->id_table)
  1102. return -EINVAL;
  1103. return driver_register(&fsi_drv->drv);
  1104. }
  1105. EXPORT_SYMBOL_GPL(fsi_driver_register);
  1106. void fsi_driver_unregister(struct fsi_driver *fsi_drv)
  1107. {
  1108. driver_unregister(&fsi_drv->drv);
  1109. }
  1110. EXPORT_SYMBOL_GPL(fsi_driver_unregister);
  1111. struct bus_type fsi_bus_type = {
  1112. .name = "fsi",
  1113. .match = fsi_bus_match,
  1114. };
  1115. EXPORT_SYMBOL_GPL(fsi_bus_type);
  1116. static int __init fsi_init(void)
  1117. {
  1118. int rc;
  1119. rc = alloc_chrdev_region(&fsi_base_dev, 0, FSI_CHAR_MAX_DEVICES, "fsi");
  1120. if (rc)
  1121. return rc;
  1122. rc = bus_register(&fsi_bus_type);
  1123. if (rc)
  1124. goto fail_bus;
  1125. return 0;
  1126. fail_bus:
  1127. unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
  1128. return rc;
  1129. }
  1130. postcore_initcall(fsi_init);
  1131. static void fsi_exit(void)
  1132. {
  1133. bus_unregister(&fsi_bus_type);
  1134. unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
  1135. ida_destroy(&fsi_minor_ida);
  1136. }
  1137. module_exit(fsi_exit);
  1138. module_param(discard_errors, int, 0664);
  1139. MODULE_LICENSE("GPL");
  1140. MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");