fsi-core.c 33 KB

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