spi-fsi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. // Copyright (C) IBM Corporation 2020
  3. #include <linux/bitfield.h>
  4. #include <linux/bits.h>
  5. #include <linux/fsi.h>
  6. #include <linux/jiffies.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/spi/spi.h>
  11. #define FSI_ENGID_SPI 0x23
  12. #define FSI_MBOX_ROOT_CTRL_8 0x2860
  13. #define FSI_MBOX_ROOT_CTRL_8_SPI_MUX 0xf0000000
  14. #define FSI2SPI_DATA0 0x00
  15. #define FSI2SPI_DATA1 0x04
  16. #define FSI2SPI_CMD 0x08
  17. #define FSI2SPI_CMD_WRITE BIT(31)
  18. #define FSI2SPI_RESET 0x18
  19. #define FSI2SPI_STATUS 0x1c
  20. #define FSI2SPI_STATUS_ANY_ERROR BIT(31)
  21. #define FSI2SPI_IRQ 0x20
  22. #define SPI_FSI_BASE 0x70000
  23. #define SPI_FSI_TIMEOUT_MS 1000
  24. #define SPI_FSI_MAX_RX_SIZE 8
  25. #define SPI_FSI_MAX_TX_SIZE 40
  26. #define SPI_FSI_ERROR 0x0
  27. #define SPI_FSI_COUNTER_CFG 0x1
  28. #define SPI_FSI_CFG1 0x2
  29. #define SPI_FSI_CLOCK_CFG 0x3
  30. #define SPI_FSI_CLOCK_CFG_MM_ENABLE BIT_ULL(32)
  31. #define SPI_FSI_CLOCK_CFG_ECC_DISABLE (BIT_ULL(35) | BIT_ULL(33))
  32. #define SPI_FSI_CLOCK_CFG_RESET1 (BIT_ULL(36) | BIT_ULL(38))
  33. #define SPI_FSI_CLOCK_CFG_RESET2 (BIT_ULL(37) | BIT_ULL(39))
  34. #define SPI_FSI_CLOCK_CFG_MODE (BIT_ULL(41) | BIT_ULL(42))
  35. #define SPI_FSI_CLOCK_CFG_SCK_RECV_DEL GENMASK_ULL(51, 44)
  36. #define SPI_FSI_CLOCK_CFG_SCK_NO_DEL BIT_ULL(51)
  37. #define SPI_FSI_CLOCK_CFG_SCK_DIV GENMASK_ULL(63, 52)
  38. #define SPI_FSI_MMAP 0x4
  39. #define SPI_FSI_DATA_TX 0x5
  40. #define SPI_FSI_DATA_RX 0x6
  41. #define SPI_FSI_SEQUENCE 0x7
  42. #define SPI_FSI_SEQUENCE_STOP 0x00
  43. #define SPI_FSI_SEQUENCE_SEL_SLAVE(x) (0x10 | ((x) & 0xf))
  44. #define SPI_FSI_SEQUENCE_SHIFT_OUT(x) (0x30 | ((x) & 0xf))
  45. #define SPI_FSI_SEQUENCE_SHIFT_IN(x) (0x40 | ((x) & 0xf))
  46. #define SPI_FSI_SEQUENCE_COPY_DATA_TX 0xc0
  47. #define SPI_FSI_SEQUENCE_BRANCH(x) (0xe0 | ((x) & 0xf))
  48. #define SPI_FSI_STATUS 0x8
  49. #define SPI_FSI_STATUS_ERROR \
  50. (GENMASK_ULL(31, 21) | GENMASK_ULL(15, 12))
  51. #define SPI_FSI_STATUS_SEQ_STATE GENMASK_ULL(55, 48)
  52. #define SPI_FSI_STATUS_SEQ_STATE_IDLE BIT_ULL(48)
  53. #define SPI_FSI_STATUS_TDR_UNDERRUN BIT_ULL(57)
  54. #define SPI_FSI_STATUS_TDR_OVERRUN BIT_ULL(58)
  55. #define SPI_FSI_STATUS_TDR_FULL BIT_ULL(59)
  56. #define SPI_FSI_STATUS_RDR_UNDERRUN BIT_ULL(61)
  57. #define SPI_FSI_STATUS_RDR_OVERRUN BIT_ULL(62)
  58. #define SPI_FSI_STATUS_RDR_FULL BIT_ULL(63)
  59. #define SPI_FSI_STATUS_ANY_ERROR \
  60. (SPI_FSI_STATUS_ERROR | \
  61. SPI_FSI_STATUS_TDR_OVERRUN | SPI_FSI_STATUS_RDR_UNDERRUN | \
  62. SPI_FSI_STATUS_RDR_OVERRUN)
  63. #define SPI_FSI_PORT_CTRL 0x9
  64. struct fsi2spi {
  65. struct fsi_device *fsi; /* FSI2SPI CFAM engine device */
  66. struct mutex lock; /* lock access to the device */
  67. };
  68. struct fsi_spi {
  69. struct device *dev; /* SPI controller device */
  70. struct fsi2spi *bridge; /* FSI2SPI device */
  71. u32 base;
  72. };
  73. struct fsi_spi_sequence {
  74. int bit;
  75. u64 data;
  76. };
  77. static int fsi_spi_check_mux(struct fsi_device *fsi, struct device *dev)
  78. {
  79. int rc;
  80. u32 root_ctrl_8;
  81. __be32 root_ctrl_8_be;
  82. rc = fsi_slave_read(fsi->slave, FSI_MBOX_ROOT_CTRL_8, &root_ctrl_8_be,
  83. sizeof(root_ctrl_8_be));
  84. if (rc)
  85. return rc;
  86. root_ctrl_8 = be32_to_cpu(root_ctrl_8_be);
  87. dev_dbg(dev, "Root control register 8: %08x\n", root_ctrl_8);
  88. if ((root_ctrl_8 & FSI_MBOX_ROOT_CTRL_8_SPI_MUX) ==
  89. FSI_MBOX_ROOT_CTRL_8_SPI_MUX)
  90. return 0;
  91. return -ENOLINK;
  92. }
  93. static int fsi_spi_check_status(struct fsi_spi *ctx)
  94. {
  95. int rc;
  96. u32 sts;
  97. __be32 sts_be;
  98. rc = fsi_device_read(ctx->bridge->fsi, FSI2SPI_STATUS, &sts_be,
  99. sizeof(sts_be));
  100. if (rc)
  101. return rc;
  102. sts = be32_to_cpu(sts_be);
  103. if (sts & FSI2SPI_STATUS_ANY_ERROR) {
  104. dev_err(ctx->dev, "Error with FSI2SPI interface: %08x.\n", sts);
  105. return -EIO;
  106. }
  107. return 0;
  108. }
  109. static int fsi_spi_read_reg(struct fsi_spi *ctx, u32 offset, u64 *value)
  110. {
  111. int rc = 0;
  112. __be32 cmd_be;
  113. __be32 data_be;
  114. u32 cmd = offset + ctx->base;
  115. struct fsi2spi *bridge = ctx->bridge;
  116. *value = 0ULL;
  117. if (cmd & FSI2SPI_CMD_WRITE)
  118. return -EINVAL;
  119. rc = mutex_lock_interruptible(&bridge->lock);
  120. if (rc)
  121. return rc;
  122. cmd_be = cpu_to_be32(cmd);
  123. rc = fsi_device_write(bridge->fsi, FSI2SPI_CMD, &cmd_be,
  124. sizeof(cmd_be));
  125. if (rc)
  126. goto unlock;
  127. rc = fsi_spi_check_status(ctx);
  128. if (rc)
  129. goto unlock;
  130. rc = fsi_device_read(bridge->fsi, FSI2SPI_DATA0, &data_be,
  131. sizeof(data_be));
  132. if (rc)
  133. goto unlock;
  134. *value |= (u64)be32_to_cpu(data_be) << 32;
  135. rc = fsi_device_read(bridge->fsi, FSI2SPI_DATA1, &data_be,
  136. sizeof(data_be));
  137. if (rc)
  138. goto unlock;
  139. *value |= (u64)be32_to_cpu(data_be);
  140. dev_dbg(ctx->dev, "Read %02x[%016llx].\n", offset, *value);
  141. unlock:
  142. mutex_unlock(&bridge->lock);
  143. return rc;
  144. }
  145. static int fsi_spi_write_reg(struct fsi_spi *ctx, u32 offset, u64 value)
  146. {
  147. int rc = 0;
  148. __be32 cmd_be;
  149. __be32 data_be;
  150. u32 cmd = offset + ctx->base;
  151. struct fsi2spi *bridge = ctx->bridge;
  152. if (cmd & FSI2SPI_CMD_WRITE)
  153. return -EINVAL;
  154. rc = mutex_lock_interruptible(&bridge->lock);
  155. if (rc)
  156. return rc;
  157. dev_dbg(ctx->dev, "Write %02x[%016llx].\n", offset, value);
  158. data_be = cpu_to_be32(upper_32_bits(value));
  159. rc = fsi_device_write(bridge->fsi, FSI2SPI_DATA0, &data_be,
  160. sizeof(data_be));
  161. if (rc)
  162. goto unlock;
  163. data_be = cpu_to_be32(lower_32_bits(value));
  164. rc = fsi_device_write(bridge->fsi, FSI2SPI_DATA1, &data_be,
  165. sizeof(data_be));
  166. if (rc)
  167. goto unlock;
  168. cmd_be = cpu_to_be32(cmd | FSI2SPI_CMD_WRITE);
  169. rc = fsi_device_write(bridge->fsi, FSI2SPI_CMD, &cmd_be,
  170. sizeof(cmd_be));
  171. if (rc)
  172. goto unlock;
  173. rc = fsi_spi_check_status(ctx);
  174. unlock:
  175. mutex_unlock(&bridge->lock);
  176. return rc;
  177. }
  178. static int fsi_spi_data_in(u64 in, u8 *rx, int len)
  179. {
  180. int i;
  181. int num_bytes = min(len, 8);
  182. for (i = 0; i < num_bytes; ++i)
  183. rx[i] = (u8)(in >> (8 * ((num_bytes - 1) - i)));
  184. return num_bytes;
  185. }
  186. static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
  187. {
  188. int i;
  189. int num_bytes = min(len, 8);
  190. u8 *out_bytes = (u8 *)out;
  191. /* Unused bytes of the tx data should be 0. */
  192. *out = 0ULL;
  193. for (i = 0; i < num_bytes; ++i)
  194. out_bytes[8 - (i + 1)] = tx[i];
  195. return num_bytes;
  196. }
  197. static int fsi_spi_reset(struct fsi_spi *ctx)
  198. {
  199. int rc;
  200. dev_dbg(ctx->dev, "Resetting SPI controller.\n");
  201. rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
  202. SPI_FSI_CLOCK_CFG_RESET1);
  203. if (rc)
  204. return rc;
  205. rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
  206. SPI_FSI_CLOCK_CFG_RESET2);
  207. if (rc)
  208. return rc;
  209. return fsi_spi_write_reg(ctx, SPI_FSI_STATUS, 0ULL);
  210. }
  211. static int fsi_spi_status(struct fsi_spi *ctx, u64 *status, const char *dir)
  212. {
  213. int rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS, status);
  214. if (rc)
  215. return rc;
  216. if (*status & SPI_FSI_STATUS_ANY_ERROR) {
  217. dev_err(ctx->dev, "%s error: %016llx\n", dir, *status);
  218. rc = fsi_spi_reset(ctx);
  219. if (rc)
  220. return rc;
  221. return -EREMOTEIO;
  222. }
  223. return 0;
  224. }
  225. static void fsi_spi_sequence_add(struct fsi_spi_sequence *seq, u8 val)
  226. {
  227. /*
  228. * Add the next byte of instruction to the 8-byte sequence register.
  229. * Then decrement the counter so that the next instruction will go in
  230. * the right place. Return the index of the slot we just filled in the
  231. * sequence register.
  232. */
  233. seq->data |= (u64)val << seq->bit;
  234. seq->bit -= 8;
  235. }
  236. static void fsi_spi_sequence_init(struct fsi_spi_sequence *seq)
  237. {
  238. seq->bit = 56;
  239. seq->data = 0ULL;
  240. }
  241. static int fsi_spi_transfer_data(struct fsi_spi *ctx,
  242. struct spi_transfer *transfer)
  243. {
  244. int loops;
  245. int rc = 0;
  246. unsigned long end;
  247. u64 status = 0ULL;
  248. if (transfer->tx_buf) {
  249. int nb;
  250. int sent = 0;
  251. u64 out = 0ULL;
  252. const u8 *tx = transfer->tx_buf;
  253. while (transfer->len > sent) {
  254. nb = fsi_spi_data_out(&out, &tx[sent],
  255. (int)transfer->len - sent);
  256. rc = fsi_spi_write_reg(ctx, SPI_FSI_DATA_TX, out);
  257. if (rc)
  258. return rc;
  259. loops = 0;
  260. end = jiffies + msecs_to_jiffies(SPI_FSI_TIMEOUT_MS);
  261. do {
  262. if (loops++ && time_after(jiffies, end))
  263. return -ETIMEDOUT;
  264. rc = fsi_spi_status(ctx, &status, "TX");
  265. if (rc)
  266. return rc;
  267. } while (status & SPI_FSI_STATUS_TDR_FULL);
  268. sent += nb;
  269. }
  270. } else if (transfer->rx_buf) {
  271. int recv = 0;
  272. u64 in = 0ULL;
  273. u8 *rx = transfer->rx_buf;
  274. while (transfer->len > recv) {
  275. loops = 0;
  276. end = jiffies + msecs_to_jiffies(SPI_FSI_TIMEOUT_MS);
  277. do {
  278. if (loops++ && time_after(jiffies, end))
  279. return -ETIMEDOUT;
  280. rc = fsi_spi_status(ctx, &status, "RX");
  281. if (rc)
  282. return rc;
  283. } while (!(status & SPI_FSI_STATUS_RDR_FULL));
  284. rc = fsi_spi_read_reg(ctx, SPI_FSI_DATA_RX, &in);
  285. if (rc)
  286. return rc;
  287. recv += fsi_spi_data_in(in, &rx[recv],
  288. (int)transfer->len - recv);
  289. }
  290. }
  291. return 0;
  292. }
  293. static int fsi_spi_transfer_init(struct fsi_spi *ctx)
  294. {
  295. int loops = 0;
  296. int rc;
  297. bool reset = false;
  298. unsigned long end;
  299. u64 seq_state;
  300. u64 clock_cfg = 0ULL;
  301. u64 status = 0ULL;
  302. u64 wanted_clock_cfg = SPI_FSI_CLOCK_CFG_ECC_DISABLE |
  303. SPI_FSI_CLOCK_CFG_SCK_NO_DEL |
  304. FIELD_PREP(SPI_FSI_CLOCK_CFG_SCK_DIV, 19);
  305. end = jiffies + msecs_to_jiffies(SPI_FSI_TIMEOUT_MS);
  306. do {
  307. if (loops++ && time_after(jiffies, end))
  308. return -ETIMEDOUT;
  309. rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS, &status);
  310. if (rc)
  311. return rc;
  312. seq_state = status & SPI_FSI_STATUS_SEQ_STATE;
  313. if (status & (SPI_FSI_STATUS_ANY_ERROR |
  314. SPI_FSI_STATUS_TDR_FULL |
  315. SPI_FSI_STATUS_RDR_FULL)) {
  316. if (reset) {
  317. dev_err(ctx->dev,
  318. "Initialization error: %08llx\n",
  319. status);
  320. return -EIO;
  321. }
  322. rc = fsi_spi_reset(ctx);
  323. if (rc)
  324. return rc;
  325. reset = true;
  326. continue;
  327. }
  328. } while (seq_state && (seq_state != SPI_FSI_STATUS_SEQ_STATE_IDLE));
  329. rc = fsi_spi_write_reg(ctx, SPI_FSI_COUNTER_CFG, 0ULL);
  330. if (rc)
  331. return rc;
  332. rc = fsi_spi_read_reg(ctx, SPI_FSI_CLOCK_CFG, &clock_cfg);
  333. if (rc)
  334. return rc;
  335. if ((clock_cfg & (SPI_FSI_CLOCK_CFG_MM_ENABLE |
  336. SPI_FSI_CLOCK_CFG_ECC_DISABLE |
  337. SPI_FSI_CLOCK_CFG_MODE |
  338. SPI_FSI_CLOCK_CFG_SCK_RECV_DEL |
  339. SPI_FSI_CLOCK_CFG_SCK_DIV)) != wanted_clock_cfg)
  340. rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
  341. wanted_clock_cfg);
  342. return rc;
  343. }
  344. static int fsi_spi_transfer_one_message(struct spi_controller *ctlr,
  345. struct spi_message *mesg)
  346. {
  347. int rc;
  348. u8 seq_slave = SPI_FSI_SEQUENCE_SEL_SLAVE(mesg->spi->chip_select + 1);
  349. unsigned int len;
  350. struct spi_transfer *transfer;
  351. struct fsi_spi *ctx = spi_controller_get_devdata(ctlr);
  352. rc = fsi_spi_check_mux(ctx->bridge->fsi, ctx->dev);
  353. if (rc)
  354. goto error;
  355. list_for_each_entry(transfer, &mesg->transfers, transfer_list) {
  356. struct fsi_spi_sequence seq;
  357. struct spi_transfer *next = NULL;
  358. /* Sequencer must do shift out (tx) first. */
  359. if (!transfer->tx_buf || transfer->len > SPI_FSI_MAX_TX_SIZE) {
  360. rc = -EINVAL;
  361. goto error;
  362. }
  363. dev_dbg(ctx->dev, "Start tx of %d bytes.\n", transfer->len);
  364. rc = fsi_spi_transfer_init(ctx);
  365. if (rc < 0)
  366. goto error;
  367. fsi_spi_sequence_init(&seq);
  368. fsi_spi_sequence_add(&seq, seq_slave);
  369. len = transfer->len;
  370. while (len > 8) {
  371. fsi_spi_sequence_add(&seq,
  372. SPI_FSI_SEQUENCE_SHIFT_OUT(8));
  373. len -= 8;
  374. }
  375. fsi_spi_sequence_add(&seq, SPI_FSI_SEQUENCE_SHIFT_OUT(len));
  376. if (!list_is_last(&transfer->transfer_list,
  377. &mesg->transfers)) {
  378. next = list_next_entry(transfer, transfer_list);
  379. /* Sequencer can only do shift in (rx) after tx. */
  380. if (next->rx_buf) {
  381. u8 shift;
  382. if (next->len > SPI_FSI_MAX_RX_SIZE) {
  383. rc = -EINVAL;
  384. goto error;
  385. }
  386. dev_dbg(ctx->dev, "Sequence rx of %d bytes.\n",
  387. next->len);
  388. shift = SPI_FSI_SEQUENCE_SHIFT_IN(next->len);
  389. fsi_spi_sequence_add(&seq, shift);
  390. } else {
  391. next = NULL;
  392. }
  393. }
  394. fsi_spi_sequence_add(&seq, SPI_FSI_SEQUENCE_SEL_SLAVE(0));
  395. rc = fsi_spi_write_reg(ctx, SPI_FSI_SEQUENCE, seq.data);
  396. if (rc)
  397. goto error;
  398. rc = fsi_spi_transfer_data(ctx, transfer);
  399. if (rc)
  400. goto error;
  401. if (next) {
  402. rc = fsi_spi_transfer_data(ctx, next);
  403. if (rc)
  404. goto error;
  405. transfer = next;
  406. }
  407. }
  408. error:
  409. mesg->status = rc;
  410. spi_finalize_current_message(ctlr);
  411. return rc;
  412. }
  413. static size_t fsi_spi_max_transfer_size(struct spi_device *spi)
  414. {
  415. return SPI_FSI_MAX_RX_SIZE;
  416. }
  417. static int fsi_spi_probe(struct device *dev)
  418. {
  419. int rc;
  420. struct device_node *np;
  421. int num_controllers_registered = 0;
  422. struct fsi2spi *bridge;
  423. struct fsi_device *fsi = to_fsi_dev(dev);
  424. rc = fsi_spi_check_mux(fsi, dev);
  425. if (rc)
  426. return -ENODEV;
  427. bridge = devm_kzalloc(dev, sizeof(*bridge), GFP_KERNEL);
  428. if (!bridge)
  429. return -ENOMEM;
  430. bridge->fsi = fsi;
  431. mutex_init(&bridge->lock);
  432. for_each_available_child_of_node(dev->of_node, np) {
  433. u32 base;
  434. struct fsi_spi *ctx;
  435. struct spi_controller *ctlr;
  436. if (of_property_read_u32(np, "reg", &base))
  437. continue;
  438. ctlr = spi_alloc_master(dev, sizeof(*ctx));
  439. if (!ctlr) {
  440. of_node_put(np);
  441. break;
  442. }
  443. ctlr->dev.of_node = np;
  444. ctlr->num_chipselect = of_get_available_child_count(np) ?: 1;
  445. ctlr->flags = SPI_CONTROLLER_HALF_DUPLEX;
  446. ctlr->max_transfer_size = fsi_spi_max_transfer_size;
  447. ctlr->transfer_one_message = fsi_spi_transfer_one_message;
  448. ctx = spi_controller_get_devdata(ctlr);
  449. ctx->dev = &ctlr->dev;
  450. ctx->bridge = bridge;
  451. ctx->base = base + SPI_FSI_BASE;
  452. rc = devm_spi_register_controller(dev, ctlr);
  453. if (rc)
  454. spi_controller_put(ctlr);
  455. else
  456. num_controllers_registered++;
  457. }
  458. if (!num_controllers_registered)
  459. return -ENODEV;
  460. return 0;
  461. }
  462. static const struct fsi_device_id fsi_spi_ids[] = {
  463. { FSI_ENGID_SPI, FSI_VERSION_ANY },
  464. { }
  465. };
  466. MODULE_DEVICE_TABLE(fsi, fsi_spi_ids);
  467. static struct fsi_driver fsi_spi_driver = {
  468. .id_table = fsi_spi_ids,
  469. .drv = {
  470. .name = "spi-fsi",
  471. .bus = &fsi_bus_type,
  472. .probe = fsi_spi_probe,
  473. },
  474. };
  475. module_fsi_driver(fsi_spi_driver);
  476. MODULE_AUTHOR("Eddie James <[email protected]>");
  477. MODULE_DESCRIPTION("FSI attached SPI controller");
  478. MODULE_LICENSE("GPL");