fsi-master-gpio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * A FSI master controller, using a simple GPIO bit-banging interface
  3. */
  4. #include <linux/crc4.h>
  5. #include <linux/delay.h>
  6. #include <linux/device.h>
  7. #include <linux/fsi.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/io.h>
  10. #include <linux/module.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/spinlock.h>
  14. #include "fsi-master.h"
  15. #define FSI_GPIO_STD_DLY 1 /* Standard pin delay in nS */
  16. #define FSI_ECHO_DELAY_CLOCKS 16 /* Number clocks for echo delay */
  17. #define FSI_PRE_BREAK_CLOCKS 50 /* Number clocks to prep for break */
  18. #define FSI_BREAK_CLOCKS 256 /* Number of clocks to issue break */
  19. #define FSI_POST_BREAK_CLOCKS 16000 /* Number clocks to set up cfam */
  20. #define FSI_INIT_CLOCKS 5000 /* Clock out any old data */
  21. #define FSI_GPIO_STD_DELAY 10 /* Standard GPIO delay in nS */
  22. /* todo: adjust down as low as */
  23. /* possible or eliminate */
  24. #define FSI_GPIO_CMD_DPOLL 0x2
  25. #define FSI_GPIO_CMD_TERM 0x3f
  26. #define FSI_GPIO_CMD_ABS_AR 0x4
  27. #define FSI_GPIO_DPOLL_CLOCKS 100 /* < 21 will cause slave to hang */
  28. /* Bus errors */
  29. #define FSI_GPIO_ERR_BUSY 1 /* Slave stuck in busy state */
  30. #define FSI_GPIO_RESP_ERRA 2 /* Any (misc) Error */
  31. #define FSI_GPIO_RESP_ERRC 3 /* Slave reports master CRC error */
  32. #define FSI_GPIO_MTOE 4 /* Master time out error */
  33. #define FSI_GPIO_CRC_INVAL 5 /* Master reports slave CRC error */
  34. /* Normal slave responses */
  35. #define FSI_GPIO_RESP_BUSY 1
  36. #define FSI_GPIO_RESP_ACK 0
  37. #define FSI_GPIO_RESP_ACKD 4
  38. #define FSI_GPIO_MAX_BUSY 100
  39. #define FSI_GPIO_MTOE_COUNT 1000
  40. #define FSI_GPIO_DRAIN_BITS 20
  41. #define FSI_GPIO_CRC_SIZE 4
  42. #define FSI_GPIO_MSG_ID_SIZE 2
  43. #define FSI_GPIO_MSG_RESPID_SIZE 2
  44. #define FSI_GPIO_PRIME_SLAVE_CLOCKS 100
  45. struct fsi_master_gpio {
  46. struct fsi_master master;
  47. struct device *dev;
  48. spinlock_t cmd_lock; /* Lock for commands */
  49. struct gpio_desc *gpio_clk;
  50. struct gpio_desc *gpio_data;
  51. struct gpio_desc *gpio_trans; /* Voltage translator */
  52. struct gpio_desc *gpio_enable; /* FSI enable */
  53. struct gpio_desc *gpio_mux; /* Mux control */
  54. };
  55. #define CREATE_TRACE_POINTS
  56. #include <trace/events/fsi_master_gpio.h>
  57. #define to_fsi_master_gpio(m) container_of(m, struct fsi_master_gpio, master)
  58. struct fsi_gpio_msg {
  59. uint64_t msg;
  60. uint8_t bits;
  61. };
  62. static void clock_toggle(struct fsi_master_gpio *master, int count)
  63. {
  64. int i;
  65. for (i = 0; i < count; i++) {
  66. ndelay(FSI_GPIO_STD_DLY);
  67. gpiod_set_value(master->gpio_clk, 0);
  68. ndelay(FSI_GPIO_STD_DLY);
  69. gpiod_set_value(master->gpio_clk, 1);
  70. }
  71. }
  72. static int sda_in(struct fsi_master_gpio *master)
  73. {
  74. int in;
  75. ndelay(FSI_GPIO_STD_DLY);
  76. in = gpiod_get_value(master->gpio_data);
  77. return in ? 1 : 0;
  78. }
  79. static void sda_out(struct fsi_master_gpio *master, int value)
  80. {
  81. gpiod_set_value(master->gpio_data, value);
  82. }
  83. static void set_sda_input(struct fsi_master_gpio *master)
  84. {
  85. gpiod_direction_input(master->gpio_data);
  86. gpiod_set_value(master->gpio_trans, 0);
  87. }
  88. static void set_sda_output(struct fsi_master_gpio *master, int value)
  89. {
  90. gpiod_set_value(master->gpio_trans, 1);
  91. gpiod_direction_output(master->gpio_data, value);
  92. }
  93. static void clock_zeros(struct fsi_master_gpio *master, int count)
  94. {
  95. set_sda_output(master, 1);
  96. clock_toggle(master, count);
  97. }
  98. static void serial_in(struct fsi_master_gpio *master, struct fsi_gpio_msg *msg,
  99. uint8_t num_bits)
  100. {
  101. uint8_t bit, in_bit;
  102. set_sda_input(master);
  103. for (bit = 0; bit < num_bits; bit++) {
  104. clock_toggle(master, 1);
  105. in_bit = sda_in(master);
  106. msg->msg <<= 1;
  107. msg->msg |= ~in_bit & 0x1; /* Data is active low */
  108. }
  109. msg->bits += num_bits;
  110. trace_fsi_master_gpio_in(master, num_bits, msg->msg);
  111. }
  112. static void serial_out(struct fsi_master_gpio *master,
  113. const struct fsi_gpio_msg *cmd)
  114. {
  115. uint8_t bit;
  116. uint64_t msg = ~cmd->msg; /* Data is active low */
  117. uint64_t sda_mask = 0x1ULL << (cmd->bits - 1);
  118. uint64_t last_bit = ~0;
  119. int next_bit;
  120. trace_fsi_master_gpio_out(master, cmd->bits, cmd->msg);
  121. if (!cmd->bits) {
  122. dev_warn(master->dev, "trying to output 0 bits\n");
  123. return;
  124. }
  125. set_sda_output(master, 0);
  126. /* Send the start bit */
  127. sda_out(master, 0);
  128. clock_toggle(master, 1);
  129. /* Send the message */
  130. for (bit = 0; bit < cmd->bits; bit++) {
  131. next_bit = (msg & sda_mask) >> (cmd->bits - 1);
  132. if (last_bit ^ next_bit) {
  133. sda_out(master, next_bit);
  134. last_bit = next_bit;
  135. }
  136. clock_toggle(master, 1);
  137. msg <<= 1;
  138. }
  139. }
  140. static void msg_push_bits(struct fsi_gpio_msg *msg, uint64_t data, int bits)
  141. {
  142. msg->msg <<= bits;
  143. msg->msg |= data & ((1ull << bits) - 1);
  144. msg->bits += bits;
  145. }
  146. static void msg_push_crc(struct fsi_gpio_msg *msg)
  147. {
  148. uint8_t crc;
  149. int top;
  150. top = msg->bits & 0x3;
  151. /* start bit, and any non-aligned top bits */
  152. crc = crc4(0, 1 << top | msg->msg >> (msg->bits - top), top + 1);
  153. /* aligned bits */
  154. crc = crc4(crc, msg->msg, msg->bits - top);
  155. msg_push_bits(msg, crc, 4);
  156. }
  157. /*
  158. * Encode an Absolute Address command
  159. */
  160. static void build_abs_ar_command(struct fsi_gpio_msg *cmd,
  161. uint8_t id, uint32_t addr, size_t size, const void *data)
  162. {
  163. bool write = !!data;
  164. uint8_t ds;
  165. int i;
  166. cmd->bits = 0;
  167. cmd->msg = 0;
  168. msg_push_bits(cmd, id, 2);
  169. msg_push_bits(cmd, FSI_GPIO_CMD_ABS_AR, 3);
  170. msg_push_bits(cmd, write ? 0 : 1, 1);
  171. /*
  172. * The read/write size is encoded in the lower bits of the address
  173. * (as it must be naturally-aligned), and the following ds bit.
  174. *
  175. * size addr:1 addr:0 ds
  176. * 1 x x 0
  177. * 2 x 0 1
  178. * 4 0 1 1
  179. *
  180. */
  181. ds = size > 1 ? 1 : 0;
  182. addr &= ~(size - 1);
  183. if (size == 4)
  184. addr |= 1;
  185. msg_push_bits(cmd, addr & ((1 << 21) - 1), 21);
  186. msg_push_bits(cmd, ds, 1);
  187. for (i = 0; write && i < size; i++)
  188. msg_push_bits(cmd, ((uint8_t *)data)[i], 8);
  189. msg_push_crc(cmd);
  190. }
  191. static void build_dpoll_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
  192. {
  193. cmd->bits = 0;
  194. cmd->msg = 0;
  195. msg_push_bits(cmd, slave_id, 2);
  196. msg_push_bits(cmd, FSI_GPIO_CMD_DPOLL, 3);
  197. msg_push_crc(cmd);
  198. }
  199. static void echo_delay(struct fsi_master_gpio *master)
  200. {
  201. set_sda_output(master, 1);
  202. clock_toggle(master, FSI_ECHO_DELAY_CLOCKS);
  203. }
  204. static void build_term_command(struct fsi_gpio_msg *cmd, uint8_t slave_id)
  205. {
  206. cmd->bits = 0;
  207. cmd->msg = 0;
  208. msg_push_bits(cmd, slave_id, 2);
  209. msg_push_bits(cmd, FSI_GPIO_CMD_TERM, 6);
  210. msg_push_crc(cmd);
  211. }
  212. /*
  213. * Store information on master errors so handler can detect and clean
  214. * up the bus
  215. */
  216. static void fsi_master_gpio_error(struct fsi_master_gpio *master, int error)
  217. {
  218. }
  219. static int read_one_response(struct fsi_master_gpio *master,
  220. uint8_t data_size, struct fsi_gpio_msg *msgp, uint8_t *tagp)
  221. {
  222. struct fsi_gpio_msg msg;
  223. uint8_t id, tag;
  224. uint32_t crc;
  225. int i;
  226. /* wait for the start bit */
  227. for (i = 0; i < FSI_GPIO_MTOE_COUNT; i++) {
  228. msg.bits = 0;
  229. msg.msg = 0;
  230. serial_in(master, &msg, 1);
  231. if (msg.msg)
  232. break;
  233. }
  234. if (i == FSI_GPIO_MTOE_COUNT) {
  235. dev_dbg(master->dev,
  236. "Master time out waiting for response\n");
  237. fsi_master_gpio_error(master, FSI_GPIO_MTOE);
  238. return -EIO;
  239. }
  240. msg.bits = 0;
  241. msg.msg = 0;
  242. /* Read slave ID & response tag */
  243. serial_in(master, &msg, 4);
  244. id = (msg.msg >> FSI_GPIO_MSG_RESPID_SIZE) & 0x3;
  245. tag = msg.msg & 0x3;
  246. /* If we have an ACK and we're expecting data, clock the data in too */
  247. if (tag == FSI_GPIO_RESP_ACK && data_size)
  248. serial_in(master, &msg, data_size * 8);
  249. /* read CRC */
  250. serial_in(master, &msg, FSI_GPIO_CRC_SIZE);
  251. /* we have a whole message now; check CRC */
  252. crc = crc4(0, 1, 1);
  253. crc = crc4(crc, msg.msg, msg.bits);
  254. if (crc) {
  255. dev_dbg(master->dev, "ERR response CRC\n");
  256. fsi_master_gpio_error(master, FSI_GPIO_CRC_INVAL);
  257. return -EIO;
  258. }
  259. if (msgp)
  260. *msgp = msg;
  261. if (tagp)
  262. *tagp = tag;
  263. return 0;
  264. }
  265. static int issue_term(struct fsi_master_gpio *master, uint8_t slave)
  266. {
  267. struct fsi_gpio_msg cmd;
  268. uint8_t tag;
  269. int rc;
  270. build_term_command(&cmd, slave);
  271. serial_out(master, &cmd);
  272. echo_delay(master);
  273. rc = read_one_response(master, 0, NULL, &tag);
  274. if (rc < 0) {
  275. dev_err(master->dev,
  276. "TERM failed; lost communication with slave\n");
  277. return -EIO;
  278. } else if (tag != FSI_GPIO_RESP_ACK) {
  279. dev_err(master->dev, "TERM failed; response %d\n", tag);
  280. return -EIO;
  281. }
  282. return 0;
  283. }
  284. static int poll_for_response(struct fsi_master_gpio *master,
  285. uint8_t slave, uint8_t size, void *data)
  286. {
  287. struct fsi_gpio_msg response, cmd;
  288. int busy_count = 0, rc, i;
  289. uint8_t tag;
  290. uint8_t *data_byte = data;
  291. retry:
  292. rc = read_one_response(master, size, &response, &tag);
  293. if (rc)
  294. return rc;
  295. switch (tag) {
  296. case FSI_GPIO_RESP_ACK:
  297. if (size && data) {
  298. uint64_t val = response.msg;
  299. /* clear crc & mask */
  300. val >>= 4;
  301. val &= (1ull << (size * 8)) - 1;
  302. for (i = 0; i < size; i++) {
  303. data_byte[size-i-1] = val;
  304. val >>= 8;
  305. }
  306. }
  307. break;
  308. case FSI_GPIO_RESP_BUSY:
  309. /*
  310. * Its necessary to clock slave before issuing
  311. * d-poll, not indicated in the hardware protocol
  312. * spec. < 20 clocks causes slave to hang, 21 ok.
  313. */
  314. clock_zeros(master, FSI_GPIO_DPOLL_CLOCKS);
  315. if (busy_count++ < FSI_GPIO_MAX_BUSY) {
  316. build_dpoll_command(&cmd, slave);
  317. serial_out(master, &cmd);
  318. echo_delay(master);
  319. goto retry;
  320. }
  321. dev_warn(master->dev,
  322. "ERR slave is stuck in busy state, issuing TERM\n");
  323. issue_term(master, slave);
  324. rc = -EIO;
  325. break;
  326. case FSI_GPIO_RESP_ERRA:
  327. case FSI_GPIO_RESP_ERRC:
  328. dev_dbg(master->dev, "ERR%c received: 0x%x\n",
  329. tag == FSI_GPIO_RESP_ERRA ? 'A' : 'C',
  330. (int)response.msg);
  331. fsi_master_gpio_error(master, response.msg);
  332. rc = -EIO;
  333. break;
  334. }
  335. /* Clock the slave enough to be ready for next operation */
  336. clock_zeros(master, FSI_GPIO_PRIME_SLAVE_CLOCKS);
  337. return rc;
  338. }
  339. static int fsi_master_gpio_xfer(struct fsi_master_gpio *master, uint8_t slave,
  340. struct fsi_gpio_msg *cmd, size_t resp_len, void *resp)
  341. {
  342. unsigned long flags;
  343. int rc;
  344. spin_lock_irqsave(&master->cmd_lock, flags);
  345. serial_out(master, cmd);
  346. echo_delay(master);
  347. rc = poll_for_response(master, slave, resp_len, resp);
  348. spin_unlock_irqrestore(&master->cmd_lock, flags);
  349. return rc;
  350. }
  351. static int fsi_master_gpio_read(struct fsi_master *_master, int link,
  352. uint8_t id, uint32_t addr, void *val, size_t size)
  353. {
  354. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  355. struct fsi_gpio_msg cmd;
  356. if (link != 0)
  357. return -ENODEV;
  358. build_abs_ar_command(&cmd, id, addr, size, NULL);
  359. return fsi_master_gpio_xfer(master, id, &cmd, size, val);
  360. }
  361. static int fsi_master_gpio_write(struct fsi_master *_master, int link,
  362. uint8_t id, uint32_t addr, const void *val, size_t size)
  363. {
  364. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  365. struct fsi_gpio_msg cmd;
  366. if (link != 0)
  367. return -ENODEV;
  368. build_abs_ar_command(&cmd, id, addr, size, val);
  369. return fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
  370. }
  371. static int fsi_master_gpio_term(struct fsi_master *_master,
  372. int link, uint8_t id)
  373. {
  374. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  375. struct fsi_gpio_msg cmd;
  376. if (link != 0)
  377. return -ENODEV;
  378. build_term_command(&cmd, id);
  379. return fsi_master_gpio_xfer(master, id, &cmd, 0, NULL);
  380. }
  381. static int fsi_master_gpio_break(struct fsi_master *_master, int link)
  382. {
  383. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  384. if (link != 0)
  385. return -ENODEV;
  386. trace_fsi_master_gpio_break(master);
  387. set_sda_output(master, 1);
  388. sda_out(master, 1);
  389. clock_toggle(master, FSI_PRE_BREAK_CLOCKS);
  390. sda_out(master, 0);
  391. clock_toggle(master, FSI_BREAK_CLOCKS);
  392. echo_delay(master);
  393. sda_out(master, 1);
  394. clock_toggle(master, FSI_POST_BREAK_CLOCKS);
  395. /* Wait for logic reset to take effect */
  396. udelay(200);
  397. return 0;
  398. }
  399. static void fsi_master_gpio_init(struct fsi_master_gpio *master)
  400. {
  401. gpiod_direction_output(master->gpio_mux, 1);
  402. gpiod_direction_output(master->gpio_trans, 1);
  403. gpiod_direction_output(master->gpio_enable, 1);
  404. gpiod_direction_output(master->gpio_clk, 1);
  405. gpiod_direction_output(master->gpio_data, 1);
  406. /* todo: evaluate if clocks can be reduced */
  407. clock_zeros(master, FSI_INIT_CLOCKS);
  408. }
  409. static int fsi_master_gpio_link_enable(struct fsi_master *_master, int link)
  410. {
  411. struct fsi_master_gpio *master = to_fsi_master_gpio(_master);
  412. if (link != 0)
  413. return -ENODEV;
  414. gpiod_set_value(master->gpio_enable, 1);
  415. return 0;
  416. }
  417. static int fsi_master_gpio_probe(struct platform_device *pdev)
  418. {
  419. struct fsi_master_gpio *master;
  420. struct gpio_desc *gpio;
  421. master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
  422. if (!master)
  423. return -ENOMEM;
  424. master->dev = &pdev->dev;
  425. master->master.dev.parent = master->dev;
  426. gpio = devm_gpiod_get(&pdev->dev, "clock", 0);
  427. if (IS_ERR(gpio)) {
  428. dev_err(&pdev->dev, "failed to get clock gpio\n");
  429. return PTR_ERR(gpio);
  430. }
  431. master->gpio_clk = gpio;
  432. gpio = devm_gpiod_get(&pdev->dev, "data", 0);
  433. if (IS_ERR(gpio)) {
  434. dev_err(&pdev->dev, "failed to get data gpio\n");
  435. return PTR_ERR(gpio);
  436. }
  437. master->gpio_data = gpio;
  438. /* Optional GPIOs */
  439. gpio = devm_gpiod_get_optional(&pdev->dev, "trans", 0);
  440. if (IS_ERR(gpio)) {
  441. dev_err(&pdev->dev, "failed to get trans gpio\n");
  442. return PTR_ERR(gpio);
  443. }
  444. master->gpio_trans = gpio;
  445. gpio = devm_gpiod_get_optional(&pdev->dev, "enable", 0);
  446. if (IS_ERR(gpio)) {
  447. dev_err(&pdev->dev, "failed to get enable gpio\n");
  448. return PTR_ERR(gpio);
  449. }
  450. master->gpio_enable = gpio;
  451. gpio = devm_gpiod_get_optional(&pdev->dev, "mux", 0);
  452. if (IS_ERR(gpio)) {
  453. dev_err(&pdev->dev, "failed to get mux gpio\n");
  454. return PTR_ERR(gpio);
  455. }
  456. master->gpio_mux = gpio;
  457. master->master.n_links = 1;
  458. master->master.flags = FSI_MASTER_FLAG_SWCLOCK;
  459. master->master.read = fsi_master_gpio_read;
  460. master->master.write = fsi_master_gpio_write;
  461. master->master.term = fsi_master_gpio_term;
  462. master->master.send_break = fsi_master_gpio_break;
  463. master->master.link_enable = fsi_master_gpio_link_enable;
  464. platform_set_drvdata(pdev, master);
  465. spin_lock_init(&master->cmd_lock);
  466. fsi_master_gpio_init(master);
  467. return fsi_master_register(&master->master);
  468. }
  469. static int fsi_master_gpio_remove(struct platform_device *pdev)
  470. {
  471. struct fsi_master_gpio *master = platform_get_drvdata(pdev);
  472. devm_gpiod_put(&pdev->dev, master->gpio_clk);
  473. devm_gpiod_put(&pdev->dev, master->gpio_data);
  474. if (master->gpio_trans)
  475. devm_gpiod_put(&pdev->dev, master->gpio_trans);
  476. if (master->gpio_enable)
  477. devm_gpiod_put(&pdev->dev, master->gpio_enable);
  478. if (master->gpio_mux)
  479. devm_gpiod_put(&pdev->dev, master->gpio_mux);
  480. fsi_master_unregister(&master->master);
  481. return 0;
  482. }
  483. static const struct of_device_id fsi_master_gpio_match[] = {
  484. { .compatible = "fsi-master-gpio" },
  485. { },
  486. };
  487. static struct platform_driver fsi_master_gpio_driver = {
  488. .driver = {
  489. .name = "fsi-master-gpio",
  490. .of_match_table = fsi_master_gpio_match,
  491. },
  492. .probe = fsi_master_gpio_probe,
  493. .remove = fsi_master_gpio_remove,
  494. };
  495. module_platform_driver(fsi_master_gpio_driver);
  496. MODULE_LICENSE("GPL");