fsi-core.c 31 KB

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