fsi-core.c 34 KB

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