fsi-core.c 23 KB

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