fsi-core.c 33 KB

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