fsi-core.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 ssize_t fsi_slave_sysfs_raw_read(struct file *file,
  346. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  347. loff_t off, size_t count)
  348. {
  349. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  350. size_t total_len, read_len;
  351. int rc;
  352. if (off < 0)
  353. return -EINVAL;
  354. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  355. return -EINVAL;
  356. for (total_len = 0; total_len < count; total_len += read_len) {
  357. read_len = min_t(size_t, count, 4);
  358. read_len -= off & 0x3;
  359. rc = fsi_slave_read(slave, off, buf + total_len, read_len);
  360. if (rc)
  361. return rc;
  362. off += read_len;
  363. }
  364. return count;
  365. }
  366. static ssize_t fsi_slave_sysfs_raw_write(struct file *file,
  367. struct kobject *kobj, struct bin_attribute *attr,
  368. char *buf, loff_t off, size_t count)
  369. {
  370. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  371. size_t total_len, write_len;
  372. int rc;
  373. if (off < 0)
  374. return -EINVAL;
  375. if (off > 0xffffffff || count > 0xffffffff || off + count > 0xffffffff)
  376. return -EINVAL;
  377. for (total_len = 0; total_len < count; total_len += write_len) {
  378. write_len = min_t(size_t, count, 4);
  379. write_len -= off & 0x3;
  380. rc = fsi_slave_write(slave, off, buf + total_len, write_len);
  381. if (rc)
  382. return rc;
  383. off += write_len;
  384. }
  385. return count;
  386. }
  387. static const struct bin_attribute fsi_slave_raw_attr = {
  388. .attr = {
  389. .name = "raw",
  390. .mode = 0600,
  391. },
  392. .size = 0,
  393. .read = fsi_slave_sysfs_raw_read,
  394. .write = fsi_slave_sysfs_raw_write,
  395. };
  396. static ssize_t fsi_slave_sysfs_term_write(struct file *file,
  397. struct kobject *kobj, struct bin_attribute *attr,
  398. char *buf, loff_t off, size_t count)
  399. {
  400. struct fsi_slave *slave = to_fsi_slave(kobj_to_dev(kobj));
  401. struct fsi_master *master = slave->master;
  402. if (!master->term)
  403. return -ENODEV;
  404. master->term(master, slave->link, slave->id);
  405. return count;
  406. }
  407. static const struct bin_attribute fsi_slave_term_attr = {
  408. .attr = {
  409. .name = "term",
  410. .mode = 0200,
  411. },
  412. .size = 0,
  413. .write = fsi_slave_sysfs_term_write,
  414. };
  415. /* Encode slave local bus echo delay */
  416. static inline uint32_t fsi_smode_echodly(int x)
  417. {
  418. return (x & FSI_SMODE_ED_MASK) << FSI_SMODE_ED_SHIFT;
  419. }
  420. /* Encode slave local bus send delay */
  421. static inline uint32_t fsi_smode_senddly(int x)
  422. {
  423. return (x & FSI_SMODE_SD_MASK) << FSI_SMODE_SD_SHIFT;
  424. }
  425. /* Encode slave local bus clock rate ratio */
  426. static inline uint32_t fsi_smode_lbcrr(int x)
  427. {
  428. return (x & FSI_SMODE_LBCRR_MASK) << FSI_SMODE_LBCRR_SHIFT;
  429. }
  430. /* Encode slave ID */
  431. static inline uint32_t fsi_smode_sid(int x)
  432. {
  433. return (x & FSI_SMODE_SID_MASK) << FSI_SMODE_SID_SHIFT;
  434. }
  435. static uint32_t fsi_slave_smode(int id)
  436. {
  437. return FSI_SMODE_WSC | FSI_SMODE_ECRC
  438. | fsi_smode_sid(id)
  439. | fsi_smode_echodly(0xf) | fsi_smode_senddly(0xf)
  440. | fsi_smode_lbcrr(0x8);
  441. }
  442. static int fsi_slave_set_smode(struct fsi_master *master, int link, int id)
  443. {
  444. uint32_t smode;
  445. /* set our smode register with the slave ID field to 0; this enables
  446. * extended slave addressing
  447. */
  448. smode = fsi_slave_smode(id);
  449. smode = cpu_to_be32(smode);
  450. return fsi_master_write(master, link, id, FSI_SLAVE_BASE + FSI_SMODE,
  451. &smode, sizeof(smode));
  452. }
  453. static void fsi_slave_release(struct device *dev)
  454. {
  455. struct fsi_slave *slave = to_fsi_slave(dev);
  456. kfree(slave);
  457. }
  458. static int fsi_slave_init(struct fsi_master *master, int link, uint8_t id)
  459. {
  460. uint32_t chip_id, llmode;
  461. struct fsi_slave *slave;
  462. uint8_t crc;
  463. int rc;
  464. /* Currently, we only support single slaves on a link, and use the
  465. * full 23-bit address range
  466. */
  467. if (id != 0)
  468. return -EINVAL;
  469. rc = fsi_master_read(master, link, id, 0, &chip_id, sizeof(chip_id));
  470. if (rc) {
  471. dev_dbg(&master->dev, "can't read slave %02x:%02x %d\n",
  472. link, id, rc);
  473. return -ENODEV;
  474. }
  475. chip_id = be32_to_cpu(chip_id);
  476. crc = crc4(0, chip_id, 32);
  477. if (crc) {
  478. dev_warn(&master->dev, "slave %02x:%02x invalid chip id CRC!\n",
  479. link, id);
  480. return -EIO;
  481. }
  482. dev_info(&master->dev, "fsi: found chip %08x at %02x:%02x:%02x\n",
  483. chip_id, master->idx, link, id);
  484. rc = fsi_slave_set_smode(master, link, id);
  485. if (rc) {
  486. dev_warn(&master->dev,
  487. "can't set smode on slave:%02x:%02x %d\n",
  488. link, id, rc);
  489. return -ENODEV;
  490. }
  491. /* If we're behind a master that doesn't provide a self-running bus
  492. * clock, put the slave into async mode
  493. */
  494. if (master->flags & FSI_MASTER_FLAG_SWCLOCK) {
  495. llmode = cpu_to_be32(FSI_LLMODE_ASYNC);
  496. rc = fsi_master_write(master, link, id,
  497. FSI_SLAVE_BASE + FSI_LLMODE,
  498. &llmode, sizeof(llmode));
  499. if (rc)
  500. dev_warn(&master->dev,
  501. "can't set llmode on slave:%02x:%02x %d\n",
  502. link, id, rc);
  503. }
  504. /* We can communicate with a slave; create the slave device and
  505. * register.
  506. */
  507. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  508. if (!slave)
  509. return -ENOMEM;
  510. slave->master = master;
  511. slave->dev.parent = &master->dev;
  512. slave->dev.release = fsi_slave_release;
  513. slave->link = link;
  514. slave->id = id;
  515. slave->size = FSI_SLAVE_SIZE_23b;
  516. dev_set_name(&slave->dev, "slave@%02x:%02x", link, id);
  517. rc = device_register(&slave->dev);
  518. if (rc < 0) {
  519. dev_warn(&master->dev, "failed to create slave device: %d\n",
  520. rc);
  521. put_device(&slave->dev);
  522. return rc;
  523. }
  524. rc = device_create_bin_file(&slave->dev, &fsi_slave_raw_attr);
  525. if (rc)
  526. dev_warn(&slave->dev, "failed to create raw attr: %d\n", rc);
  527. rc = device_create_bin_file(&slave->dev, &fsi_slave_term_attr);
  528. if (rc)
  529. dev_warn(&slave->dev, "failed to create term attr: %d\n", rc);
  530. rc = fsi_slave_scan(slave);
  531. if (rc)
  532. dev_dbg(&master->dev, "failed during slave scan with: %d\n",
  533. rc);
  534. return rc;
  535. }
  536. /* FSI master support */
  537. static int fsi_check_access(uint32_t addr, size_t size)
  538. {
  539. if (size != 1 && size != 2 && size != 4)
  540. return -EINVAL;
  541. if ((addr & 0x3) != (size & 0x3))
  542. return -EINVAL;
  543. return 0;
  544. }
  545. static int fsi_master_read(struct fsi_master *master, int link,
  546. uint8_t slave_id, uint32_t addr, void *val, size_t size)
  547. {
  548. int rc;
  549. trace_fsi_master_read(master, link, slave_id, addr, size);
  550. rc = fsi_check_access(addr, size);
  551. if (!rc)
  552. rc = master->read(master, link, slave_id, addr, val, size);
  553. trace_fsi_master_rw_result(master, link, slave_id, addr, size,
  554. false, val, rc);
  555. return rc;
  556. }
  557. static int fsi_master_write(struct fsi_master *master, int link,
  558. uint8_t slave_id, uint32_t addr, const void *val, size_t size)
  559. {
  560. int rc;
  561. trace_fsi_master_write(master, link, slave_id, addr, size, val);
  562. rc = fsi_check_access(addr, size);
  563. if (!rc)
  564. rc = master->write(master, link, slave_id, addr, val, size);
  565. trace_fsi_master_rw_result(master, link, slave_id, addr, size,
  566. true, val, rc);
  567. return rc;
  568. }
  569. static int fsi_master_link_enable(struct fsi_master *master, int link)
  570. {
  571. if (master->link_enable)
  572. return master->link_enable(master, link);
  573. return 0;
  574. }
  575. /*
  576. * Issue a break command on this link
  577. */
  578. static int fsi_master_break(struct fsi_master *master, int link)
  579. {
  580. trace_fsi_master_break(master, link);
  581. if (master->send_break)
  582. return master->send_break(master, link);
  583. return 0;
  584. }
  585. static int fsi_master_scan(struct fsi_master *master)
  586. {
  587. int link, rc;
  588. for (link = 0; link < master->n_links; link++) {
  589. rc = fsi_master_link_enable(master, link);
  590. if (rc) {
  591. dev_dbg(&master->dev,
  592. "enable link %d failed: %d\n", link, rc);
  593. continue;
  594. }
  595. rc = fsi_master_break(master, link);
  596. if (rc) {
  597. dev_dbg(&master->dev,
  598. "break to link %d failed: %d\n", link, rc);
  599. continue;
  600. }
  601. fsi_slave_init(master, link, 0);
  602. }
  603. return 0;
  604. }
  605. static int fsi_slave_remove_device(struct device *dev, void *arg)
  606. {
  607. device_unregister(dev);
  608. return 0;
  609. }
  610. static int fsi_master_remove_slave(struct device *dev, void *arg)
  611. {
  612. device_for_each_child(dev, NULL, fsi_slave_remove_device);
  613. device_unregister(dev);
  614. return 0;
  615. }
  616. static void fsi_master_unscan(struct fsi_master *master)
  617. {
  618. device_for_each_child(&master->dev, NULL, fsi_master_remove_slave);
  619. }
  620. static ssize_t master_rescan_store(struct device *dev,
  621. struct device_attribute *attr, const char *buf, size_t count)
  622. {
  623. struct fsi_master *master = to_fsi_master(dev);
  624. int rc;
  625. fsi_master_unscan(master);
  626. rc = fsi_master_scan(master);
  627. if (rc < 0)
  628. return rc;
  629. return count;
  630. }
  631. static DEVICE_ATTR(rescan, 0200, NULL, master_rescan_store);
  632. static ssize_t master_break_store(struct device *dev,
  633. struct device_attribute *attr, const char *buf, size_t count)
  634. {
  635. struct fsi_master *master = to_fsi_master(dev);
  636. fsi_master_break(master, 0);
  637. return count;
  638. }
  639. static DEVICE_ATTR(break, 0200, NULL, master_break_store);
  640. int fsi_master_register(struct fsi_master *master)
  641. {
  642. int rc;
  643. if (!master)
  644. return -EINVAL;
  645. master->idx = ida_simple_get(&master_ida, 0, INT_MAX, GFP_KERNEL);
  646. dev_set_name(&master->dev, "fsi%d", master->idx);
  647. rc = device_register(&master->dev);
  648. if (rc) {
  649. ida_simple_remove(&master_ida, master->idx);
  650. return rc;
  651. }
  652. rc = device_create_file(&master->dev, &dev_attr_rescan);
  653. if (rc) {
  654. device_unregister(&master->dev);
  655. ida_simple_remove(&master_ida, master->idx);
  656. return rc;
  657. }
  658. rc = device_create_file(&master->dev, &dev_attr_break);
  659. if (rc) {
  660. device_unregister(&master->dev);
  661. ida_simple_remove(&master_ida, master->idx);
  662. return rc;
  663. }
  664. fsi_master_scan(master);
  665. return 0;
  666. }
  667. EXPORT_SYMBOL_GPL(fsi_master_register);
  668. void fsi_master_unregister(struct fsi_master *master)
  669. {
  670. if (master->idx >= 0) {
  671. ida_simple_remove(&master_ida, master->idx);
  672. master->idx = -1;
  673. }
  674. fsi_master_unscan(master);
  675. device_unregister(&master->dev);
  676. }
  677. EXPORT_SYMBOL_GPL(fsi_master_unregister);
  678. /* FSI core & Linux bus type definitions */
  679. static int fsi_bus_match(struct device *dev, struct device_driver *drv)
  680. {
  681. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  682. struct fsi_driver *fsi_drv = to_fsi_drv(drv);
  683. const struct fsi_device_id *id;
  684. if (!fsi_drv->id_table)
  685. return 0;
  686. for (id = fsi_drv->id_table; id->engine_type; id++) {
  687. if (id->engine_type != fsi_dev->engine_type)
  688. continue;
  689. if (id->version == FSI_VERSION_ANY ||
  690. id->version == fsi_dev->version)
  691. return 1;
  692. }
  693. return 0;
  694. }
  695. int fsi_driver_register(struct fsi_driver *fsi_drv)
  696. {
  697. if (!fsi_drv)
  698. return -EINVAL;
  699. if (!fsi_drv->id_table)
  700. return -EINVAL;
  701. return driver_register(&fsi_drv->drv);
  702. }
  703. EXPORT_SYMBOL_GPL(fsi_driver_register);
  704. void fsi_driver_unregister(struct fsi_driver *fsi_drv)
  705. {
  706. driver_unregister(&fsi_drv->drv);
  707. }
  708. EXPORT_SYMBOL_GPL(fsi_driver_unregister);
  709. struct bus_type fsi_bus_type = {
  710. .name = "fsi",
  711. .match = fsi_bus_match,
  712. };
  713. EXPORT_SYMBOL_GPL(fsi_bus_type);
  714. static int __init fsi_init(void)
  715. {
  716. return bus_register(&fsi_bus_type);
  717. }
  718. postcore_initcall(fsi_init);
  719. static void fsi_exit(void)
  720. {
  721. bus_unregister(&fsi_bus_type);
  722. }
  723. module_exit(fsi_exit);
  724. module_param(discard_errors, int, 0664);
  725. MODULE_LICENSE("GPL");
  726. MODULE_PARM_DESC(discard_errors, "Don't invoke error handling on bus accesses");