fsi-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /*
  2. * FSI core driver
  3. *
  4. * Copyright (C) IBM Corporation 2016
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/crc4.h>
  16. #include <linux/device.h>
  17. #include <linux/fsi.h>
  18. #include <linux/idr.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/bitops.h>
  22. #include "fsi-master.h"
  23. #define CREATE_TRACE_POINTS
  24. #include <trace/events/fsi.h>
  25. #define FSI_SLAVE_CONF_NEXT_MASK GENMASK(31, 31)
  26. #define FSI_SLAVE_CONF_SLOTS_MASK GENMASK(23, 16)
  27. #define FSI_SLAVE_CONF_SLOTS_SHIFT 16
  28. #define FSI_SLAVE_CONF_VERSION_MASK GENMASK(15, 12)
  29. #define FSI_SLAVE_CONF_VERSION_SHIFT 12
  30. #define FSI_SLAVE_CONF_TYPE_MASK GENMASK(11, 4)
  31. #define FSI_SLAVE_CONF_TYPE_SHIFT 4
  32. #define FSI_SLAVE_CONF_CRC_SHIFT 4
  33. #define FSI_SLAVE_CONF_CRC_MASK GENMASK(3, 0)
  34. #define FSI_SLAVE_CONF_DATA_BITS 28
  35. #define FSI_PEEK_BASE 0x410
  36. static const int engine_page_size = 0x400;
  37. #define FSI_SLAVE_BASE 0x800
  38. /*
  39. * FSI slave engine control register offsets
  40. */
  41. #define FSI_SMODE 0x0 /* R/W: Mode register */
  42. #define FSI_SISC 0x8 /* R/W: Interrupt condition */
  43. #define FSI_SSTAT 0x14 /* R : Slave status */
  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. * LLMODE fields
  60. */
  61. #define FSI_LLMODE_ASYNC 0x1
  62. #define FSI_SLAVE_SIZE_23b 0x800000
  63. static DEFINE_IDA(master_ida);
  64. struct fsi_slave {
  65. struct device dev;
  66. struct fsi_master *master;
  67. int id;
  68. int link;
  69. uint32_t size; /* size of slave address space */
  70. };
  71. #define to_fsi_master(d) container_of(d, struct fsi_master, dev)
  72. #define to_fsi_slave(d) container_of(d, struct fsi_slave, dev)
  73. static const int slave_retries = 2;
  74. static int discard_errors;
  75. static int fsi_master_read(struct fsi_master *master, int link,
  76. uint8_t slave_id, uint32_t addr, void *val, size_t size);
  77. static int fsi_master_write(struct fsi_master *master, int link,
  78. uint8_t slave_id, uint32_t addr, const void *val, size_t size);
  79. static int fsi_master_break(struct fsi_master *master, int link);
  80. /*
  81. * fsi_device_read() / fsi_device_write() / fsi_device_peek()
  82. *
  83. * FSI endpoint-device support
  84. *
  85. * Read / write / peek accessors for a client
  86. *
  87. * Parameters:
  88. * dev: Structure passed to FSI client device drivers on probe().
  89. * addr: FSI address of given device. Client should pass in its base address
  90. * plus desired offset to access its register space.
  91. * val: For read/peek this is the value read at the specified address. For
  92. * write this is value to write to the specified address.
  93. * The data in val must be FSI bus endian (big endian).
  94. * size: Size in bytes of the operation. Sizes supported are 1, 2 and 4 bytes.
  95. * Addresses must be aligned on size boundaries or an error will result.
  96. */
  97. int fsi_device_read(struct fsi_device *dev, uint32_t addr, void *val,
  98. size_t size)
  99. {
  100. if (addr > dev->size || size > dev->size || addr > dev->size - size)
  101. return -EINVAL;
  102. return fsi_slave_read(dev->slave, dev->addr + addr, val, size);
  103. }
  104. EXPORT_SYMBOL_GPL(fsi_device_read);
  105. int fsi_device_write(struct fsi_device *dev, uint32_t addr, const void *val,
  106. size_t size)
  107. {
  108. if (addr > dev->size || size > dev->size || addr > dev->size - size)
  109. return -EINVAL;
  110. return fsi_slave_write(dev->slave, dev->addr + addr, val, size);
  111. }
  112. EXPORT_SYMBOL_GPL(fsi_device_write);
  113. int fsi_device_peek(struct fsi_device *dev, void *val)
  114. {
  115. uint32_t addr = FSI_PEEK_BASE + ((dev->unit - 2) * sizeof(uint32_t));
  116. return fsi_slave_read(dev->slave, addr, val, sizeof(uint32_t));
  117. }
  118. static void fsi_device_release(struct device *_device)
  119. {
  120. struct fsi_device *device = to_fsi_dev(_device);
  121. kfree(device);
  122. }
  123. static struct fsi_device *fsi_create_device(struct fsi_slave *slave)
  124. {
  125. struct fsi_device *dev;
  126. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  127. if (!dev)
  128. return NULL;
  129. dev->dev.parent = &slave->dev;
  130. dev->dev.bus = &fsi_bus_type;
  131. dev->dev.release = fsi_device_release;
  132. return dev;
  133. }
  134. /* FSI slave support */
  135. static int fsi_slave_calc_addr(struct fsi_slave *slave, uint32_t *addrp,
  136. uint8_t *idp)
  137. {
  138. uint32_t addr = *addrp;
  139. uint8_t id = *idp;
  140. if (addr > slave->size)
  141. return -EINVAL;
  142. /* For 23 bit addressing, we encode the extra two bits in the slave
  143. * id (and the slave's actual ID needs to be 0).
  144. */
  145. if (addr > 0x1fffff) {
  146. if (slave->id != 0)
  147. return -EINVAL;
  148. id = (addr >> 21) & 0x3;
  149. addr &= 0x1fffff;
  150. }
  151. *addrp = addr;
  152. *idp = id;
  153. return 0;
  154. }
  155. int fsi_slave_report_and_clear_errors(struct fsi_slave *slave)
  156. {
  157. struct fsi_master *master = slave->master;
  158. uint32_t irq, stat;
  159. int rc, link;
  160. uint8_t id;
  161. link = slave->link;
  162. id = slave->id;
  163. rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
  164. &irq, sizeof(irq));
  165. if (rc)
  166. return rc;
  167. rc = fsi_master_read(master, link, id, FSI_SLAVE_BASE + FSI_SSTAT,
  168. &stat, sizeof(stat));
  169. if (rc)
  170. return rc;
  171. dev_info(&slave->dev, "status: 0x%08x, sisc: 0x%08x\n",
  172. be32_to_cpu(stat), be32_to_cpu(irq));
  173. /* clear interrupts */
  174. return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SISC,
  175. &irq, sizeof(irq));
  176. }
  177. static int fsi_slave_set_smode(struct fsi_master *master, int link, int id);
  178. int fsi_slave_handle_error(struct fsi_slave *slave, bool write, uint32_t addr,
  179. size_t size)
  180. {
  181. struct fsi_master *master = slave->master;
  182. int rc, link;
  183. uint32_t reg;
  184. uint8_t id;
  185. if (discard_errors)
  186. return -1;
  187. link = slave->link;
  188. id = slave->id;
  189. dev_dbg(&slave->dev, "handling error on %s to 0x%08x[%zd]",
  190. write ? "write" : "read", addr, size);
  191. /* try a simple clear of error conditions, which may fail if we've lost
  192. * communication with the slave
  193. */
  194. rc = fsi_slave_report_and_clear_errors(slave);
  195. if (!rc)
  196. return 0;
  197. /* send a TERM and retry */
  198. if (master->term) {
  199. rc = master->term(master, link, id);
  200. if (!rc) {
  201. rc = fsi_master_read(master, link, id, 0,
  202. &reg, sizeof(reg));
  203. if (!rc)
  204. rc = fsi_slave_report_and_clear_errors(slave);
  205. if (!rc)
  206. return 0;
  207. }
  208. }
  209. /* getting serious, reset the slave via BREAK */
  210. rc = fsi_master_break(master, link);
  211. if (rc)
  212. return rc;
  213. rc = fsi_slave_set_smode(master, link, id);
  214. if (rc)
  215. return rc;
  216. return fsi_slave_report_and_clear_errors(slave);
  217. }
  218. int fsi_slave_read(struct fsi_slave *slave, uint32_t addr,
  219. void *val, size_t size)
  220. {
  221. uint8_t id = slave->id;
  222. int rc, err_rc, i;
  223. rc = fsi_slave_calc_addr(slave, &addr, &id);
  224. if (rc)
  225. return rc;
  226. for (i = 0; i < slave_retries; i++) {
  227. rc = fsi_master_read(slave->master, slave->link,
  228. id, addr, val, size);
  229. if (!rc)
  230. break;
  231. err_rc = fsi_slave_handle_error(slave, false, addr, size);
  232. if (err_rc)
  233. break;
  234. }
  235. return rc;
  236. }
  237. EXPORT_SYMBOL_GPL(fsi_slave_read);
  238. int fsi_slave_write(struct fsi_slave *slave, uint32_t addr,
  239. const void *val, size_t size)
  240. {
  241. uint8_t id = slave->id;
  242. int rc, err_rc, i;
  243. rc = fsi_slave_calc_addr(slave, &addr, &id);
  244. if (rc)
  245. return rc;
  246. for (i = 0; i < slave_retries; i++) {
  247. rc = fsi_master_write(slave->master, slave->link,
  248. id, addr, val, size);
  249. if (!rc)
  250. break;
  251. err_rc = fsi_slave_handle_error(slave, true, addr, size);
  252. if (err_rc)
  253. break;
  254. }
  255. return rc;
  256. }
  257. EXPORT_SYMBOL_GPL(fsi_slave_write);
  258. extern int fsi_slave_claim_range(struct fsi_slave *slave,
  259. uint32_t addr, uint32_t size)
  260. {
  261. if (addr + size < addr)
  262. return -EINVAL;
  263. if (addr + size > slave->size)
  264. return -EINVAL;
  265. /* todo: check for overlapping claims */
  266. return 0;
  267. }
  268. EXPORT_SYMBOL_GPL(fsi_slave_claim_range);
  269. extern void fsi_slave_release_range(struct fsi_slave *slave,
  270. uint32_t addr, uint32_t size)
  271. {
  272. }
  273. EXPORT_SYMBOL_GPL(fsi_slave_release_range);
  274. static int fsi_slave_scan(struct fsi_slave *slave)
  275. {
  276. uint32_t engine_addr;
  277. uint32_t conf;
  278. int rc, i;
  279. /*
  280. * scan engines
  281. *
  282. * We keep the peek mode and slave engines for the core; so start
  283. * at the third slot in the configuration table. We also need to
  284. * skip the chip ID entry at the start of the address space.
  285. */
  286. engine_addr = engine_page_size * 3;
  287. for (i = 2; i < engine_page_size / sizeof(uint32_t); i++) {
  288. uint8_t slots, version, type, crc;
  289. struct fsi_device *dev;
  290. rc = fsi_slave_read(slave, (i + 1) * sizeof(conf),
  291. &conf, sizeof(conf));
  292. if (rc) {
  293. dev_warn(&slave->dev,
  294. "error reading slave registers\n");
  295. return -1;
  296. }
  297. conf = be32_to_cpu(conf);
  298. crc = crc4(0, conf, 32);
  299. if (crc) {
  300. dev_warn(&slave->dev,
  301. "crc error in slave register at 0x%04x\n",
  302. i);
  303. return -1;
  304. }
  305. slots = (conf & FSI_SLAVE_CONF_SLOTS_MASK)
  306. >> FSI_SLAVE_CONF_SLOTS_SHIFT;
  307. version = (conf & FSI_SLAVE_CONF_VERSION_MASK)
  308. >> FSI_SLAVE_CONF_VERSION_SHIFT;
  309. type = (conf & FSI_SLAVE_CONF_TYPE_MASK)
  310. >> FSI_SLAVE_CONF_TYPE_SHIFT;
  311. /*
  312. * Unused address areas are marked by a zero type value; this
  313. * skips the defined address areas
  314. */
  315. if (type != 0 && slots != 0) {
  316. /* create device */
  317. dev = fsi_create_device(slave);
  318. if (!dev)
  319. return -ENOMEM;
  320. dev->slave = slave;
  321. dev->engine_type = type;
  322. dev->version = version;
  323. dev->unit = i;
  324. dev->addr = engine_addr;
  325. dev->size = slots * engine_page_size;
  326. dev_dbg(&slave->dev,
  327. "engine[%i]: type %x, version %x, addr %x size %x\n",
  328. dev->unit, dev->engine_type, version,
  329. dev->addr, dev->size);
  330. dev_set_name(&dev->dev, "%02x:%02x:%02x:%02x",
  331. slave->master->idx, slave->link,
  332. slave->id, i - 2);
  333. rc = device_register(&dev->dev);
  334. if (rc) {
  335. dev_warn(&slave->dev, "add failed: %d\n", rc);
  336. put_device(&dev->dev);
  337. }
  338. }
  339. engine_addr += slots * engine_page_size;
  340. if (!(conf & FSI_SLAVE_CONF_NEXT_MASK))
  341. break;
  342. }
  343. return 0;
  344. }
  345. static unsigned long aligned_access_size(size_t offset, size_t count)
  346. {
  347. unsigned long offset_unit, count_unit;
  348. /* Criteria:
  349. *
  350. * 1. Access size must be less than or equal to the maximum access
  351. * width or the highest power-of-two factor of offset
  352. * 2. Access size must be less than or equal to the amount specified by
  353. * count
  354. *
  355. * The access width is optimal if we can calculate 1 to be strictly
  356. * equal while still satisfying 2.
  357. */
  358. /* Find 1 by the bottom bit of offset (with a 4 byte access cap) */
  359. offset_unit = BIT(__builtin_ctzl(offset | 4));
  360. /* Find 2 by the top bit of count */
  361. count_unit = BIT(8 * sizeof(unsigned long) - 1 - __builtin_clzl(count));
  362. /* Constrain the maximum access width to the minimum of both criteria */
  363. return BIT(__builtin_ctzl(offset_unit | count_unit));
  364. }
  365. static ssize_t fsi_slave_sysfs_raw_read(struct file *file,
  366. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  367. loff_t off, size_t count)
  368. {
  369. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  370. size_t total_len, read_len;
  371. int rc;
  372. if (off < 0)
  373. return -EINVAL;
  374. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  375. return -EINVAL;
  376. for (total_len = 0; total_len < count; total_len += read_len) {
  377. read_len = aligned_access_size(off, count - total_len);
  378. rc = fsi_slave_read(slave, off, buf + total_len, read_len);
  379. if (rc)
  380. return rc;
  381. off += read_len;
  382. }
  383. return count;
  384. }
  385. static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
  386. struct kobject *kobj, struct bin_attribute *attr,
  387. char *buf, loff_t off, size_t count)
  388. {
  389. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  390. size_t total_len, write_len;
  391. int rc;
  392. if (off < 0)
  393. return -EINVAL;
  394. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  395. return -EINVAL;
  396. for (total_len = 0; total_len < count; total_len += write_len) {
  397. write_len = aligned_access_size(off, count - total_len);
  398. rc = fsi_slave_write(slave, off, buf + total_len, write_len);
  399. if (rc)
  400. return rc;
  401. off += write_len;
  402. }
  403. return count;
  404. }
  405. static const struct bin_attribute fsi_slave_raw_attr = {
  406. .attr = {
  407. .name = "raw",
  408. .mode = 0600,
  409. },
  410. .size = 0,
  411. .read = fsi_slave_sysfs_raw_read,
  412. .write = fsi_slave_sysfs_raw_write,
  413. };
  414. static ssize_t fsi_slave_sysfs_term_write(struct file *file,
  415. struct kobject *kobj, struct bin_attribute *attr,
  416. char *buf, loff_t off, size_t count)
  417. {
  418. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  419. struct fsi_master *master = slave->master;
  420. if (!master->term)
  421. return -ENODEV;
  422. master->term(master, slave->link, slave->id);
  423. return count;
  424. }
  425. static const struct bin_attribute fsi_slave_term_attr = {
  426. .attr = {
  427. .name = "term",
  428. .mode = 0200,
  429. },
  430. .size = 0,
  431. .write = fsi_slave_sysfs_term_write,
  432. };
  433. /* Encode slave local bus echo delay */
  434. static inline uint32_t fsi_smode_echodly(int x)
  435. {
  436. return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT;
  437. }
  438. /* Encode slave local bus send delay */
  439. static inline uint32_t fsi_smode_senddly(int x)
  440. {
  441. return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT;
  442. }
  443. /* Encode slave local bus clock rate ratio */
  444. static inline uint32_t fsi_smode_lbcrr(int x)
  445. {
  446. return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT;
  447. }
  448. /* Encode slave ID */
  449. static inline uint32_t fsi_smode_sid(int x)
  450. {
  451. return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT;
  452. }
  453. static uint32_t fsi_slave_smode(int id)
  454. {
  455. return FSI_SMODE_WSC | FSI_SMODE_ECRC
  456. | fsi_smode_sid(id)
  457. | fsi_smode_echodly(0xf) | fsi_smode_senddly(0xf)
  458. | fsi_smode_lbcrr(0x8);
  459. }
  460. static int fsi_slave_set_smode(struct fsi_master *master, int link, int id)
  461. {
  462. uint32_t smode;
  463. /* set our smode register with the slave ID field to 0; this enables
  464. * extended slave addressing
  465. */
  466. smode = fsi_slave_smode(id);
  467. smode = cpu_to_be32(smode);
  468. return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SMODE,
  469. &smode, sizeof(smode));
  470. }
  471. static void fsi_slave_release(struct device *dev)
  472. {
  473. struct fsi_slave *slave = to_fsi_slave(dev);
  474. kfree(slave);
  475. }
  476. static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
  477. {
  478. uint32_t chip_id, llmode;
  479. struct fsi_slave *slave;
  480. uint8_t crc;
  481. int rc;
  482. /* Currently, we only support single slaves on a link, and use the
  483. * full 23-bit address range
  484. */
  485. if (id != 0)
  486. return -EINVAL;
  487. rc = fsi_master_read(master, link, id, 0, &chip_id, sizeof(chip_id));
  488. if (rc) {
  489. dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
  490. link, id, rc);
  491. return -ENODEV;
  492. }
  493. chip_id = be32_to_cpu(chip_id);
  494. crc = crc4(0, chip_id, 32);
  495. if (crc) {
  496. dev_warn(&master->dev, "slave %02x:%02x invalid chip id CRC!\n",
  497. link, id);
  498. return -EIO;
  499. }
  500. dev_info(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
  501. chip_id, master->idx, link, id);
  502. rc = fsi_slave_set_smode(master, link, id);
  503. if (rc) {
  504. dev_warn(&master->dev,
  505. "can't set smode on slave:%02x:%02x %d\n",
  506. link, id, rc);
  507. return -ENODEV;
  508. }
  509. /* If we're behind a master that doesn't provide a self-running bus
  510. * clock, put the slave into async mode
  511. */
  512. if (master->flags & FSI_MASTER_FLAG_SWCLOCK) {
  513. llmode = cpu_to_be32(FSI_LLMODE_ASYNC);
  514. rc = fsi_master_write(master, link, id,
  515. FSI_SLAVE_BASE + FSI_LLMODE,
  516. &llmode, sizeof(llmode));
  517. if (rc)
  518. dev_warn(&master->dev,
  519. "can't set llmode on slave:%02x:%02x %d\n",
  520. link, id, rc);
  521. }
  522. /* We can communicate with a slave; create the slave device and
  523. * register.
  524. */
  525. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  526. if (!slave)
  527. return -ENOMEM;
  528. slave->master = master;
  529. slave->dev.parent = &master->dev;
  530. slave->dev.release = fsi_slave_release;
  531. slave->link = link;
  532. slave->id = id;
  533. slave->size = FSI_SLAVE_SIZE_23b;
  534. dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
  535. rc = device_register(&slave->dev);
  536. if (rc < 0) {
  537. dev_warn(&master->dev, "failed to create slave device: %d\n",
  538. rc);
  539. put_device(&slave->dev);
  540. return rc;
  541. }
  542. rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
  543. if (rc)
  544. dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
  545. rc = device_create_bin_file(&slave->dev, &fsi_slave_term_attr);
  546. if (rc)
  547. dev_warn(&slave->dev, "failed to create term attr: %d\n", rc);
  548. rc = fsi_slave_scan(slave);
  549. if (rc)
  550. dev_dbg(&master->dev, "failed during slave scan with: %d\n",
  551. rc);
  552. return rc;
  553. }
  554. /* FSI master support */
  555. static int fsi_check_access(uint32_t addr, size_t size)
  556. {
  557. if (size != 1 && size != 2 && size != 4)
  558. return -EINVAL;
  559. if ((addr & 0x3) != (size & 0x3))
  560. return -EINVAL;
  561. return 0;
  562. }
  563. static int fsi_master_read(struct fsi_master *master, int link,
  564. uint8_t slave_id, uint32_t addr, void *val, size_t size)
  565. {
  566. int rc;
  567. trace_fsi_master_read(master, link, slave_id, addr, size);
  568. rc = fsi_check_access(addr, size);
  569. if (!rc)
  570. rc = master->read(master, link, slave_id, addr, val, size);
  571. trace_fsi_master_rw_result(master, link, slave_id, addr, size,
  572. false, val, rc);
  573. return rc;
  574. }
  575. static int fsi_master_write(struct fsi_master *master, int link,
  576. uint8_t slave_id, uint32_t addr, const void *val, size_t size)
  577. {
  578. int rc;
  579. trace_fsi_master_write(master, link, slave_id, addr, size, val);
  580. rc = fsi_check_access(addr, size);
  581. if (!rc)
  582. rc = master->write(master, link, slave_id, addr, val, size);
  583. trace_fsi_master_rw_result(master, link, slave_id, addr, size,
  584. true, val, rc);
  585. return rc;
  586. }
  587. static int fsi_master_link_enable(struct fsi_master *master, int link)
  588. {
  589. if (master->link_enable)
  590. return master->link_enable(master, link);
  591. return 0;
  592. }
  593. /*
  594. * Issue a break command on this link
  595. */
  596. static int fsi_master_break(struct fsi_master *master, int link)
  597. {
  598. trace_fsi_master_break(master, link);
  599. if (master->send_break)
  600. return master->send_break(master, link);
  601. return 0;
  602. }
  603. static int fsi_master_scan(struct fsi_master *master)
  604. {
  605. int link, rc;
  606. for (link = 0; link < master->n_links; link++) {
  607. rc = fsi_master_link_enable(master, link);
  608. if (rc) {
  609. dev_dbg(&master->dev,
  610. "enable link %d failed: %d\n", link, rc);
  611. continue;
  612. }
  613. rc = fsi_master_break(master, link);
  614. if (rc) {
  615. dev_dbg(&master->dev,
  616. "break to link %d failed: %d\n", link, rc);
  617. continue;
  618. }
  619. fsi_slave_init(master, link, 0);
  620. }
  621. return 0;
  622. }
  623. static int fsi_slave_remove_device(struct device *dev, void *arg)
  624. {
  625. device_unregister(dev);
  626. return 0;
  627. }
  628. static int fsi_master_remove_slave(struct device *dev, void *arg)
  629. {
  630. device_for_each_child(dev, NULL, fsi_slave_remove_device);
  631. device_unregister(dev);
  632. return 0;
  633. }
  634. static void fsi_master_unscan(struct fsi_master *master)
  635. {
  636. device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
  637. }
  638. static ssize_t master_rescan_store(struct device *dev,
  639. struct device_attribute *attr, const char *buf, size_t count)
  640. {
  641. struct fsi_master *master = to_fsi_master(dev);
  642. int rc;
  643. fsi_master_unscan(master);
  644. rc = fsi_master_scan(master);
  645. if (rc < 0)
  646. return rc;
  647. return count;
  648. }
  649. static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
  650. static ssize_t master_break_store(struct device *dev,
  651. struct device_attribute *attr, const char *buf, size_t count)
  652. {
  653. struct fsi_master *master = to_fsi_master(dev);
  654. fsi_master_break(master, 0);
  655. return count;
  656. }
  657. static DEVICE_ATTR(break, 0200, NULL, master_break_store);
  658. int fsi_master_register(struct fsi_master *master)
  659. {
  660. int rc;
  661. if (!master)
  662. return -EINVAL;
  663. master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
  664. dev_set_name(&master->dev, "fsi%d", master->idx);
  665. rc = device_register(&master->dev);
  666. if (rc) {
  667. ida_simple_remove(&master_ida, master->idx);
  668. return rc;
  669. }
  670. rc = device_create_file(&master->dev, &dev_attr_rescan);
  671. if (rc) {
  672. device_unregister(&master->dev);
  673. ida_simple_remove(&master_ida, master->idx);
  674. return rc;
  675. }
  676. rc = device_create_file(&master->dev, &dev_attr_break);
  677. if (rc) {
  678. device_unregister(&master->dev);
  679. ida_simple_remove(&master_ida, master->idx);
  680. return rc;
  681. }
  682. fsi_master_scan(master);
  683. return 0;
  684. }
  685. EXPORT_SYMBOL_GPL(fsi_master_register);
  686. void fsi_master_unregister(struct fsi_master *master)
  687. {
  688. if (master->idx >= 0) {
  689. ida_simple_remove(&master_ida, master->idx);
  690. master->idx = -1;
  691. }
  692. fsi_master_unscan(master);
  693. device_unregister(&master->dev);
  694. }
  695. EXPORT_SYMBOL_GPL(fsi_master_unregister);
  696. /* FSI core & Linux bus type definitions */
  697. static int fsi_bus_match(struct device *dev, struct device_driver *drv)
  698. {
  699. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  700. struct fsi_driver *fsi_drv = to_fsi_drv(drv);
  701. const struct fsi_device_id *id;
  702. if (!fsi_drv->id_table)
  703. return 0;
  704. for (id = fsi_drv->id_table; id->engine_type; id++) {
  705. if (id->engine_type != fsi_dev->engine_type)
  706. continue;
  707. if (id->version == FSI_VERSION_ANY ||
  708. id->version == fsi_dev->version)
  709. return 1;
  710. }
  711. return 0;
  712. }
  713. int fsi_driver_register(struct fsi_driver *fsi_drv)
  714. {
  715. if (!fsi_drv)
  716. return -EINVAL;
  717. if (!fsi_drv->id_table)
  718. return -EINVAL;
  719. return driver_register(&fsi_drv->drv);
  720. }
  721. EXPORT_SYMBOL_GPL(fsi_driver_register);
  722. void fsi_driver_unregister(struct fsi_driver *fsi_drv)
  723. {
  724. driver_unregister(&fsi_drv->drv);
  725. }
  726. EXPORT_SYMBOL_GPL(fsi_driver_unregister);
  727. struct bus_type fsi_bus_type = {
  728. .name = "fsi",
  729. .match = fsi_bus_match,
  730. };
  731. EXPORT_SYMBOL_GPL(fsi_bus_type);
  732. static int __init fsi_init(void)
  733. {
  734. return bus_register(&fsi_bus_type);
  735. }
  736. postcore_initcall(fsi_init);
  737. static void fsi_exit(void)
  738. {
  739. bus_unregister(&fsi_bus_type);
  740. }
  741. module_exit(fsi_exit);
  742. module_param(discard_errors, int, 0664);
  743. MODULE_LICENSE("GPL");
  744. MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");