fsi-core.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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 FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31)
  25. #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16)
  26. #define FSI_SLAVE_CONF_SLOTS_SHIFT 16
  27. #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12)
  28. #define FSI_SLAVE_CONF_VERSION_SHIFT 12
  29. #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4)
  30. #define FSI_SLAVE_CONF_TYPE_SHIFT 4
  31. #define FSI_SLAVE_CONF_CRC_SHIFT 4
  32. #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0)
  33. #define FSI_SLAVE_CONF_DATA_BITS 28
  34. #define FSI_PEEK_BASE 0x410
  35. static const int engine_page_size = 0x400;
  36. #define FSI_SLAVE_BASE 0x800
  37. /*
  38. * FSI slave engine control register offsets
  39. */
  40. #define FSI_SMODE 0x0 /* R/W: Mode register */
  41. #define FSI_SISC 0x8 /* R/W: Interrupt condition */
  42. #define FSI_SSTAT 0x14 /* R : Slave status */
  43. #define FSI_SLBUS 0x30 /* W : LBUS Ownership */
  44. #define FSI_LLMODE 0x100 /* R/W: Link layer mode register */
  45. /*
  46. * SMODE fields
  47. */
  48. #define FSI_SMODE_WSC 0x80000000 /* Warm start done */
  49. #define FSI_SMODE_ECRC 0x20000000 /* Hw CRC check */
  50. #define FSI_SMODE_SID_SHIFT 24 /* ID shift */
  51. #define FSI_SMODE_SID_MASK 3 /* ID Mask */
  52. #define FSI_SMODE_ED_SHIFT 20 /* Echo delay shift */
  53. #define FSI_SMODE_ED_MASK 0xf /* Echo delay mask */
  54. #define FSI_SMODE_SD_SHIFT 16 /* Send delay shift */
  55. #define FSI_SMODE_SD_MASK 0xf /* Send delay mask */
  56. #define FSI_SMODE_LBCRR_SHIFT 8 /* Clk ratio shift */
  57. #define FSI_SMODE_LBCRR_MASK 0xf /* Clk ratio mask */
  58. /*
  59. * SLBUS fields
  60. */
  61. #define FSI_SLBUS_FORCE 0x80000000 /* Force LBUS ownership */
  62. /*
  63. * LLMODE fields
  64. */
  65. #define FSI_LLMODE_ASYNC 0x1
  66. #define FSI_SLAVE_SIZE_23b 0x800000
  67. static DEFINE_IDA(master_ida);
  68. struct fsi_slave {
  69. struct device dev;
  70. struct fsi_master *master;
  71. struct cdev cdev;
  72. int cdev_idx;
  73. int id; /* FSI address */
  74. int link; /* FSI link# */
  75. u32 cfam_id;
  76. int chip_id;
  77. uint32_t size; /* size of slave address space */
  78. u8 t_send_delay;
  79. u8 t_echo_delay;
  80. };
  81. #define CREATE_TRACE_POINTS
  82. #include <trace/events/fsi.h>
  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. 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. 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. trace_fsi_dev_init(dev);
  433. dev_dbg(&slave->dev,
  434. "engine[%i]: type %x, version %x, addr %x size %x\n",
  435. dev->unit, dev->engine_type, version,
  436. dev->addr, dev->size);
  437. dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x",
  438. slave->master->idx, slave->link,
  439. slave->id, i - 2);
  440. dev->dev.of_node = fsi_device_find_of_node(dev);
  441. rc = device_register(&dev->dev);
  442. if (rc) {
  443. dev_warn(&slave->dev, "add failed: %d\n", rc);
  444. put_device(&dev->dev);
  445. }
  446. }
  447. engine_addr += slots * engine_page_size;
  448. if (!(conf & FSI_SLAVE_CONF_NEXT_MASK))
  449. break;
  450. }
  451. return 0;
  452. }
  453. static unsigned long aligned_access_size(size_t offset, size_t count)
  454. {
  455. unsigned long offset_unit, count_unit;
  456. /* Criteria:
  457. *
  458. * 1. Access size must be less than or equal to the maximum access
  459. * width or the highest power-of-two factor of offset
  460. * 2. Access size must be less than or equal to the amount specified by
  461. * count
  462. *
  463. * The access width is optimal if we can calculate 1 to be strictly
  464. * equal while still satisfying 2.
  465. */
  466. /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */
  467. offset_unit = BIT(__builtin_ctzl(offset | 4));
  468. /* Find 2 by the top bit of count */
  469. count_unit = BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count));
  470. /* Constrain the maximum access width to the minimum of both criteria */
  471. return BIT(__builtin_ctzl(offset_unit | count_unit));
  472. }
  473. static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
  474. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  475. loff_t off, size_t count)
  476. {
  477. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  478. size_t total_len, read_len;
  479. int rc;
  480. if (off < 0)
  481. return -EINVAL;
  482. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  483. return -EINVAL;
  484. for (total_len = 0; total_len < count; total_len += read_len) {
  485. read_len = aligned_access_size(off, count - total_len);
  486. rc = fsi_slave_read(slave, off, buf + total_len, read_len);
  487. if (rc)
  488. return rc;
  489. off += read_len;
  490. }
  491. return count;
  492. }
  493. static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
  494. struct kobject *kobj, struct bin_attribute *attr,
  495. char *buf, loff_t off, size_t count)
  496. {
  497. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  498. size_t total_len, write_len;
  499. int rc;
  500. if (off < 0)
  501. return -EINVAL;
  502. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  503. return -EINVAL;
  504. for (total_len = 0; total_len < count; total_len += write_len) {
  505. write_len = aligned_access_size(off, count - total_len);
  506. rc = fsi_slave_write(slave, off, buf + total_len, write_len);
  507. if (rc)
  508. return rc;
  509. off += write_len;
  510. }
  511. return count;
  512. }
  513. static const struct bin_attribute fsi_slave_raw_attr = {
  514. .attr = {
  515. .name = "raw",
  516. .mode = 0600,
  517. },
  518. .size = 0,
  519. .read = fsi_slave_sysfs_raw_read,
  520. .write = fsi_slave_sysfs_raw_write,
  521. };
  522. static void fsi_slave_release(struct device *dev)
  523. {
  524. struct fsi_slave *slave = to_fsi_slave(dev);
  525. fsi_free_minor(slave->dev.devt);
  526. of_node_put(dev->of_node);
  527. kfree(slave);
  528. }
  529. static bool fsi_slave_node_matches(struct device_node *np,
  530. int link, uint8_t id)
  531. {
  532. unsigned int len, na, ns;
  533. const __be32 *prop;
  534. na = of_n_addr_cells(np);
  535. ns = of_n_size_cells(np);
  536. /* Ensure we have the correct format for addresses and sizes in
  537. * reg properties
  538. */
  539. if (na != 2 || ns != 0)
  540. return false;
  541. prop = of_get_property(np, "reg", &len);
  542. if (!prop || len != 8)
  543. return false;
  544. return (of_read_number(prop, 1) == link) &&
  545. (of_read_number(prop + 1, 1) == id);
  546. }
  547. /* Find a matching node for the slave at (link, id). Returns NULL if none
  548. * found, or a matching node with refcount already incremented.
  549. */
  550. static struct device_node *fsi_slave_find_of_node(struct fsi_master *master,
  551. int link, uint8_t id)
  552. {
  553. struct device_node *parent, *np;
  554. parent = dev_of_node(&master->dev);
  555. if (!parent)
  556. return NULL;
  557. for_each_child_of_node(parent, np) {
  558. if (fsi_slave_node_matches(np, link, id))
  559. return np;
  560. }
  561. return NULL;
  562. }
  563. static ssize_t cfam_read(struct file *filep, char __user *buf, size_t count,
  564. loff_t *offset)
  565. {
  566. struct fsi_slave *slave = filep->private_data;
  567. size_t total_len, read_len;
  568. loff_t off = *offset;
  569. ssize_t rc;
  570. if (off < 0)
  571. return -EINVAL;
  572. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  573. return -EINVAL;
  574. for (total_len = 0; total_len < count; total_len += read_len) {
  575. __be32 data;
  576. read_len = min_t(size_t, count, 4);
  577. read_len -= off & 0x3;
  578. rc = fsi_slave_read(slave, off, &data, read_len);
  579. if (rc)
  580. goto fail;
  581. rc = copy_to_user(buf + total_len, &data, read_len);
  582. if (rc) {
  583. rc = -EFAULT;
  584. goto fail;
  585. }
  586. off += read_len;
  587. }
  588. rc = count;
  589. fail:
  590. *offset = off;
  591. return rc;
  592. }
  593. static ssize_t cfam_write(struct file *filep, const char __user *buf,
  594. size_t count, loff_t *offset)
  595. {
  596. struct fsi_slave *slave = filep->private_data;
  597. size_t total_len, write_len;
  598. loff_t off = *offset;
  599. ssize_t rc;
  600. if (off < 0)
  601. return -EINVAL;
  602. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  603. return -EINVAL;
  604. for (total_len = 0; total_len < count; total_len += write_len) {
  605. __be32 data;
  606. write_len = min_t(size_t, count, 4);
  607. write_len -= off & 0x3;
  608. rc = copy_from_user(&data, buf + total_len, write_len);
  609. if (rc) {
  610. rc = -EFAULT;
  611. goto fail;
  612. }
  613. rc = fsi_slave_write(slave, off, &data, write_len);
  614. if (rc)
  615. goto fail;
  616. off += write_len;
  617. }
  618. rc = count;
  619. fail:
  620. *offset = off;
  621. return rc;
  622. }
  623. static loff_t cfam_llseek(struct file *file, loff_t offset, int whence)
  624. {
  625. switch (whence) {
  626. case SEEK_CUR:
  627. break;
  628. case SEEK_SET:
  629. file->f_pos = offset;
  630. break;
  631. default:
  632. return -EINVAL;
  633. }
  634. return offset;
  635. }
  636. static int cfam_open(struct inode *inode, struct file *file)
  637. {
  638. struct fsi_slave *slave = container_of(inode->i_cdev, struct fsi_slave, cdev);
  639. file->private_data = slave;
  640. return 0;
  641. }
  642. static const struct file_operations cfam_fops = {
  643. .owner = THIS_MODULE,
  644. .open = cfam_open,
  645. .llseek = cfam_llseek,
  646. .read = cfam_read,
  647. .write = cfam_write,
  648. };
  649. static ssize_t send_term_store(struct device *dev,
  650. struct device_attribute *attr,
  651. const char *buf, size_t count)
  652. {
  653. struct fsi_slave *slave = to_fsi_slave(dev);
  654. struct fsi_master *master = slave->master;
  655. if (!master->term)
  656. return -ENODEV;
  657. master->term(master, slave->link, slave->id);
  658. return count;
  659. }
  660. static DEVICE_ATTR_WO(send_term);
  661. static ssize_t slave_send_echo_show(struct device *dev,
  662. struct device_attribute *attr,
  663. char *buf)
  664. {
  665. struct fsi_slave *slave = to_fsi_slave(dev);
  666. return sprintf(buf, "%u\n", slave->t_send_delay);
  667. }
  668. static ssize_t slave_send_echo_store(struct device *dev,
  669. struct device_attribute *attr, const char *buf, size_t count)
  670. {
  671. struct fsi_slave *slave = to_fsi_slave(dev);
  672. struct fsi_master *master = slave->master;
  673. unsigned long val;
  674. int rc;
  675. if (kstrtoul(buf, 0, &val) < 0)
  676. return -EINVAL;
  677. if (val < 1 || val > 16)
  678. return -EINVAL;
  679. if (!master->link_config)
  680. return -ENXIO;
  681. /* Current HW mandates that send and echo delay are identical */
  682. slave->t_send_delay = val;
  683. slave->t_echo_delay = val;
  684. rc = fsi_slave_set_smode(slave);
  685. if (rc < 0)
  686. return rc;
  687. if (master->link_config)
  688. master->link_config(master, slave->link,
  689. slave->t_send_delay,
  690. slave->t_echo_delay);
  691. return count;
  692. }
  693. static DEVICE_ATTR(send_echo_delays, 0600,
  694. slave_send_echo_show, slave_send_echo_store);
  695. static ssize_t chip_id_show(struct device *dev,
  696. struct device_attribute *attr,
  697. char *buf)
  698. {
  699. struct fsi_slave *slave = to_fsi_slave(dev);
  700. return sprintf(buf, "%d\n", slave->chip_id);
  701. }
  702. static DEVICE_ATTR_RO(chip_id);
  703. static ssize_t cfam_id_show(struct device *dev,
  704. struct device_attribute *attr,
  705. char *buf)
  706. {
  707. struct fsi_slave *slave = to_fsi_slave(dev);
  708. return sprintf(buf, "0x%x\n", slave->cfam_id);
  709. }
  710. static DEVICE_ATTR_RO(cfam_id);
  711. static struct attribute *cfam_attr[] = {
  712. &dev_attr_send_echo_delays.attr,
  713. &dev_attr_chip_id.attr,
  714. &dev_attr_cfam_id.attr,
  715. &dev_attr_send_term.attr,
  716. NULL,
  717. };
  718. static const struct attribute_group cfam_attr_group = {
  719. .attrs = cfam_attr,
  720. };
  721. static const struct attribute_group *cfam_attr_groups[] = {
  722. &cfam_attr_group,
  723. NULL,
  724. };
  725. static char *cfam_devnode(struct device *dev, umode_t *mode,
  726. kuid_t *uid, kgid_t *gid)
  727. {
  728. struct fsi_slave *slave = to_fsi_slave(dev);
  729. #ifdef CONFIG_FSI_NEW_DEV_NODE
  730. return kasprintf(GFP_KERNEL, "fsi/cfam%d", slave->cdev_idx);
  731. #else
  732. return kasprintf(GFP_KERNEL, "cfam%d", slave->cdev_idx);
  733. #endif
  734. }
  735. static const struct device_type cfam_type = {
  736. .name = "cfam",
  737. .devnode = cfam_devnode,
  738. .groups = cfam_attr_groups
  739. };
  740. static char *fsi_cdev_devnode(struct device *dev, umode_t *mode,
  741. kuid_t *uid, kgid_t *gid)
  742. {
  743. #ifdef CONFIG_FSI_NEW_DEV_NODE
  744. return kasprintf(GFP_KERNEL, "fsi/%s", dev_name(dev));
  745. #else
  746. return kasprintf(GFP_KERNEL, "%s", dev_name(dev));
  747. #endif
  748. }
  749. const struct device_type fsi_cdev_type = {
  750. .name = "fsi-cdev",
  751. .devnode = fsi_cdev_devnode,
  752. };
  753. EXPORT_SYMBOL_GPL(fsi_cdev_type);
  754. /* Backward compatible /dev/ numbering in "old style" mode */
  755. static int fsi_adjust_index(int index)
  756. {
  757. #ifdef CONFIG_FSI_NEW_DEV_NODE
  758. return index;
  759. #else
  760. return index + 1;
  761. #endif
  762. }
  763. static int __fsi_get_new_minor(struct fsi_slave *slave, enum fsi_dev_type type,
  764. dev_t *out_dev, int *out_index)
  765. {
  766. int cid = slave->chip_id;
  767. int id;
  768. /* Check if we qualify for legacy numbering */
  769. if (cid >= 0 && cid < 16 && type < 4) {
  770. /* Try reserving the legacy number */
  771. id = (cid << 4) | type;
  772. id = ida_simple_get(&fsi_minor_ida, id, id + 1, GFP_KERNEL);
  773. if (id >= 0) {
  774. *out_index = fsi_adjust_index(cid);
  775. *out_dev = fsi_base_dev + id;
  776. return 0;
  777. }
  778. /* Other failure */
  779. if (id != -ENOSPC)
  780. return id;
  781. /* Fallback to non-legacy allocation */
  782. }
  783. id = ida_simple_get(&fsi_minor_ida, FSI_CHAR_LEGACY_TOP,
  784. FSI_CHAR_MAX_DEVICES, GFP_KERNEL);
  785. if (id < 0)
  786. return id;
  787. *out_index = fsi_adjust_index(id);
  788. *out_dev = fsi_base_dev + id;
  789. return 0;
  790. }
  791. int fsi_get_new_minor(struct fsi_device *fdev, enum fsi_dev_type type,
  792. dev_t *out_dev, int *out_index)
  793. {
  794. return __fsi_get_new_minor(fdev->slave, type, out_dev, out_index);
  795. }
  796. EXPORT_SYMBOL_GPL(fsi_get_new_minor);
  797. void fsi_free_minor(dev_t dev)
  798. {
  799. ida_simple_remove(&fsi_minor_ida, MINOR(dev));
  800. }
  801. EXPORT_SYMBOL_GPL(fsi_free_minor);
  802. static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
  803. {
  804. uint32_t cfam_id;
  805. struct fsi_slave *slave;
  806. uint8_t crc;
  807. __be32 data, llmode, slbus;
  808. int rc;
  809. /* Currently, we only support single slaves on a link, and use the
  810. * full 23-bit address range
  811. */
  812. if (id != 0)
  813. return -EINVAL;
  814. rc = fsi_master_read(master, link, id, 0, &data, sizeof(data));
  815. if (rc) {
  816. dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
  817. link, id, rc);
  818. return -ENODEV;
  819. }
  820. cfam_id = be32_to_cpu(data);
  821. crc = crc4(0, cfam_id, 32);
  822. if (crc) {
  823. trace_fsi_slave_invalid_cfam(master, link, cfam_id);
  824. dev_warn(&master->dev, "slave %02x:%02x invalid cfam id CRC!\n",
  825. link, id);
  826. return -EIO;
  827. }
  828. dev_dbg(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
  829. cfam_id, master->idx, link, id);
  830. /* If we're behind a master that doesn't provide a self-running bus
  831. * clock, put the slave into async mode
  832. */
  833. if (master->flags & FSI_MASTER_FLAG_SWCLOCK) {
  834. llmode = cpu_to_be32(FSI_LLMODE_ASYNC);
  835. rc = fsi_master_write(master, link, id,
  836. FSI_SLAVE_BASE + FSI_LLMODE,
  837. &llmode, sizeof(llmode));
  838. if (rc)
  839. dev_warn(&master->dev,
  840. "can't set llmode on slave:%02x:%02x %d\n",
  841. link, id, rc);
  842. }
  843. /* We can communicate with a slave; create the slave device and
  844. * register.
  845. */
  846. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  847. if (!slave)
  848. return -ENOMEM;
  849. dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
  850. slave->dev.type = &cfam_type;
  851. slave->dev.parent = &master->dev;
  852. slave->dev.of_node = fsi_slave_find_of_node(master, link, id);
  853. slave->dev.release = fsi_slave_release;
  854. device_initialize(&slave->dev);
  855. slave->cfam_id = cfam_id;
  856. slave->master = master;
  857. slave->link = link;
  858. slave->id = id;
  859. slave->size = FSI_SLAVE_SIZE_23b;
  860. slave->t_send_delay = 16;
  861. slave->t_echo_delay = 16;
  862. /* Get chip ID if any */
  863. slave->chip_id = -1;
  864. if (slave->dev.of_node) {
  865. uint32_t prop;
  866. if (!of_property_read_u32(slave->dev.of_node, "chip-id", &prop))
  867. slave->chip_id = prop;
  868. }
  869. slbus = cpu_to_be32(FSI_SLBUS_FORCE);
  870. rc = fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SLBUS,
  871. &slbus, sizeof(slbus));
  872. if (rc)
  873. dev_warn(&master->dev,
  874. "can't set slbus on slave:%02x:%02x %d\n", link, id,
  875. rc);
  876. rc = fsi_slave_set_smode(slave);
  877. if (rc) {
  878. dev_warn(&master->dev,
  879. "can't set smode on slave:%02x:%02x %d\n",
  880. link, id, rc);
  881. goto err_free;
  882. }
  883. /* Allocate a minor in the FSI space */
  884. rc = __fsi_get_new_minor(slave, fsi_dev_cfam, &slave->dev.devt,
  885. &slave->cdev_idx);
  886. if (rc)
  887. goto err_free;
  888. trace_fsi_slave_init(slave);
  889. /* Create chardev for userspace access */
  890. cdev_init(&slave->cdev, &cfam_fops);
  891. rc = cdev_device_add(&slave->cdev, &slave->dev);
  892. if (rc) {
  893. dev_err(&slave->dev, "Error %d creating slave device\n", rc);
  894. goto err_free_ida;
  895. }
  896. /* Now that we have the cdev registered with the core, any fatal
  897. * failures beyond this point will need to clean up through
  898. * cdev_device_del(). Fortunately though, nothing past here is fatal.
  899. */
  900. if (master->link_config)
  901. master->link_config(master, link,
  902. slave->t_send_delay,
  903. slave->t_echo_delay);
  904. /* Legacy raw file -> to be removed */
  905. rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
  906. if (rc)
  907. dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
  908. rc = fsi_slave_scan(slave);
  909. if (rc)
  910. dev_dbg(&master->dev, "failed during slave scan with: %d\n",
  911. rc);
  912. return 0;
  913. err_free_ida:
  914. fsi_free_minor(slave->dev.devt);
  915. err_free:
  916. of_node_put(slave->dev.of_node);
  917. kfree(slave);
  918. return rc;
  919. }
  920. /* FSI master support */
  921. static int fsi_check_access(uint32_t addr, size_t size)
  922. {
  923. if (size == 4) {
  924. if (addr & 0x3)
  925. return -EINVAL;
  926. } else if (size == 2) {
  927. if (addr & 0x1)
  928. return -EINVAL;
  929. } else if (size != 1)
  930. return -EINVAL;
  931. return 0;
  932. }
  933. static int fsi_master_read(struct fsi_master *master, int link,
  934. uint8_t slave_id, uint32_t addr, void *val, size_t size)
  935. {
  936. int rc;
  937. trace_fsi_master_read(master, link, slave_id, addr, size);
  938. rc = fsi_check_access(addr, size);
  939. if (!rc)
  940. rc = master->read(master, link, slave_id, addr, val, size);
  941. trace_fsi_master_rw_result(master, link, slave_id, addr, size,
  942. false, val, rc);
  943. return rc;
  944. }
  945. static int fsi_master_write(struct fsi_master *master, int link,
  946. uint8_t slave_id, uint32_t addr, const void *val, size_t size)
  947. {
  948. int rc;
  949. trace_fsi_master_write(master, link, slave_id, addr, size, val);
  950. rc = fsi_check_access(addr, size);
  951. if (!rc)
  952. rc = master->write(master, link, slave_id, addr, val, size);
  953. trace_fsi_master_rw_result(master, link, slave_id, addr, size,
  954. true, val, rc);
  955. return rc;
  956. }
  957. static int fsi_master_link_disable(struct fsi_master *master, int link)
  958. {
  959. if (master->link_enable)
  960. return master->link_enable(master, link, false);
  961. return 0;
  962. }
  963. static int fsi_master_link_enable(struct fsi_master *master, int link)
  964. {
  965. if (master->link_enable)
  966. return master->link_enable(master, link, true);
  967. return 0;
  968. }
  969. /*
  970. * Issue a break command on this link
  971. */
  972. static int fsi_master_break(struct fsi_master *master, int link)
  973. {
  974. int rc = 0;
  975. trace_fsi_master_break(master, link);
  976. if (master->send_break)
  977. rc = master->send_break(master, link);
  978. if (master->link_config)
  979. master->link_config(master, link, 16, 16);
  980. return rc;
  981. }
  982. static int fsi_master_scan(struct fsi_master *master)
  983. {
  984. int link, rc;
  985. for (link = 0; link < master->n_links; link++) {
  986. rc = fsi_master_link_enable(master, link);
  987. if (rc) {
  988. dev_dbg(&master->dev,
  989. "enable link %d failed: %d\n", link, rc);
  990. continue;
  991. }
  992. rc = fsi_master_break(master, link);
  993. if (rc) {
  994. fsi_master_link_disable(master, link);
  995. dev_dbg(&master->dev,
  996. "break to link %d failed: %d\n", link, rc);
  997. continue;
  998. }
  999. rc = fsi_slave_init(master, link, 0);
  1000. if (rc)
  1001. fsi_master_link_disable(master, link);
  1002. }
  1003. return 0;
  1004. }
  1005. static int fsi_slave_remove_device(struct device *dev, void *arg)
  1006. {
  1007. device_unregister(dev);
  1008. return 0;
  1009. }
  1010. static int fsi_master_remove_slave(struct device *dev, void *arg)
  1011. {
  1012. struct fsi_slave *slave = to_fsi_slave(dev);
  1013. device_for_each_child(dev, NULL, fsi_slave_remove_device);
  1014. cdev_device_del(&slave->cdev, &slave->dev);
  1015. put_device(dev);
  1016. return 0;
  1017. }
  1018. static void fsi_master_unscan(struct fsi_master *master)
  1019. {
  1020. device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
  1021. }
  1022. int fsi_master_rescan(struct fsi_master *master)
  1023. {
  1024. int rc;
  1025. mutex_lock(&master->scan_lock);
  1026. fsi_master_unscan(master);
  1027. rc = fsi_master_scan(master);
  1028. mutex_unlock(&master->scan_lock);
  1029. return rc;
  1030. }
  1031. EXPORT_SYMBOL_GPL(fsi_master_rescan);
  1032. static ssize_t master_rescan_store(struct device *dev,
  1033. struct device_attribute *attr, const char *buf, size_t count)
  1034. {
  1035. struct fsi_master *master = to_fsi_master(dev);
  1036. int rc;
  1037. rc = fsi_master_rescan(master);
  1038. if (rc < 0)
  1039. return rc;
  1040. return count;
  1041. }
  1042. static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
  1043. static ssize_t master_break_store(struct device *dev,
  1044. struct device_attribute *attr, const char *buf, size_t count)
  1045. {
  1046. struct fsi_master *master = to_fsi_master(dev);
  1047. fsi_master_break(master, 0);
  1048. return count;
  1049. }
  1050. static DEVICE_ATTR(break, 0200, NULL, master_break_store);
  1051. static struct attribute *master_attrs[] = {
  1052. &dev_attr_break.attr,
  1053. &dev_attr_rescan.attr,
  1054. NULL
  1055. };
  1056. ATTRIBUTE_GROUPS(master);
  1057. static struct class fsi_master_class = {
  1058. .name = "fsi-master",
  1059. .dev_groups = master_groups,
  1060. };
  1061. int fsi_master_register(struct fsi_master *master)
  1062. {
  1063. int rc;
  1064. struct device_node *np;
  1065. mutex_init(&master->scan_lock);
  1066. master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
  1067. if (master->idx < 0)
  1068. return master->idx;
  1069. dev_set_name(&master->dev, "fsi%d", master->idx);
  1070. master->dev.class = &fsi_master_class;
  1071. rc = device_register(&master->dev);
  1072. if (rc) {
  1073. ida_simple_remove(&master_ida, master->idx);
  1074. return rc;
  1075. }
  1076. np = dev_of_node(&master->dev);
  1077. if (!of_property_read_bool(np, "no-scan-on-init")) {
  1078. mutex_lock(&master->scan_lock);
  1079. fsi_master_scan(master);
  1080. mutex_unlock(&master->scan_lock);
  1081. }
  1082. return 0;
  1083. }
  1084. EXPORT_SYMBOL_GPL(fsi_master_register);
  1085. void fsi_master_unregister(struct fsi_master *master)
  1086. {
  1087. if (master->idx >= 0) {
  1088. ida_simple_remove(&master_ida, master->idx);
  1089. master->idx = -1;
  1090. }
  1091. mutex_lock(&master->scan_lock);
  1092. fsi_master_unscan(master);
  1093. mutex_unlock(&master->scan_lock);
  1094. device_unregister(&master->dev);
  1095. }
  1096. EXPORT_SYMBOL_GPL(fsi_master_unregister);
  1097. /* FSI core & Linux bus type definitions */
  1098. static int fsi_bus_match(struct device *dev, struct device_driver *drv)
  1099. {
  1100. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  1101. struct fsi_driver *fsi_drv = to_fsi_drv(drv);
  1102. const struct fsi_device_id *id;
  1103. if (!fsi_drv->id_table)
  1104. return 0;
  1105. for (id = fsi_drv->id_table; id->engine_type; id++) {
  1106. if (id->engine_type != fsi_dev->engine_type)
  1107. continue;
  1108. if (id->version == FSI_VERSION_ANY ||
  1109. id->version == fsi_dev->version)
  1110. return 1;
  1111. }
  1112. return 0;
  1113. }
  1114. int fsi_driver_register(struct fsi_driver *fsi_drv)
  1115. {
  1116. if (!fsi_drv)
  1117. return -EINVAL;
  1118. if (!fsi_drv->id_table)
  1119. return -EINVAL;
  1120. return driver_register(&fsi_drv->drv);
  1121. }
  1122. EXPORT_SYMBOL_GPL(fsi_driver_register);
  1123. void fsi_driver_unregister(struct fsi_driver *fsi_drv)
  1124. {
  1125. driver_unregister(&fsi_drv->drv);
  1126. }
  1127. EXPORT_SYMBOL_GPL(fsi_driver_unregister);
  1128. struct bus_type fsi_bus_type = {
  1129. .name = "fsi",
  1130. .match = fsi_bus_match,
  1131. };
  1132. EXPORT_SYMBOL_GPL(fsi_bus_type);
  1133. static int __init fsi_init(void)
  1134. {
  1135. int rc;
  1136. rc = alloc_chrdev_region(&fsi_base_dev, 0, FSI_CHAR_MAX_DEVICES, "fsi");
  1137. if (rc)
  1138. return rc;
  1139. rc = bus_register(&fsi_bus_type);
  1140. if (rc)
  1141. goto fail_bus;
  1142. rc = class_register(&fsi_master_class);
  1143. if (rc)
  1144. goto fail_class;
  1145. return 0;
  1146. fail_class:
  1147. bus_unregister(&fsi_bus_type);
  1148. fail_bus:
  1149. unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
  1150. return rc;
  1151. }
  1152. postcore_initcall(fsi_init);
  1153. static void fsi_exit(void)
  1154. {
  1155. class_unregister(&fsi_master_class);
  1156. bus_unregister(&fsi_bus_type);
  1157. unregister_chrdev_region(fsi_base_dev, FSI_CHAR_MAX_DEVICES);
  1158. ida_destroy(&fsi_minor_ida);
  1159. }
  1160. module_exit(fsi_exit);
  1161. module_param(discard_errors, int, 0664);
  1162. MODULE_LICENSE("GPL");
  1163. MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");