pio.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * Copyright (c) 2020, MIPI Alliance, Inc.
  4. *
  5. * Author: Nicolas Pitre <[email protected]>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/device.h>
  9. #include <linux/errno.h>
  10. #include <linux/i3c/master.h>
  11. #include <linux/io.h>
  12. #include "hci.h"
  13. #include "cmd.h"
  14. #include "ibi.h"
  15. /*
  16. * PIO Access Area
  17. */
  18. #define pio_reg_read(r) readl(hci->PIO_regs + (PIO_##r))
  19. #define pio_reg_write(r, v) writel(v, hci->PIO_regs + (PIO_##r))
  20. #define PIO_COMMAND_QUEUE_PORT 0x00
  21. #define PIO_RESPONSE_QUEUE_PORT 0x04
  22. #define PIO_XFER_DATA_PORT 0x08
  23. #define PIO_IBI_PORT 0x0c
  24. #define PIO_QUEUE_THLD_CTRL 0x10
  25. #define QUEUE_IBI_STATUS_THLD GENMASK(31, 24)
  26. #define QUEUE_IBI_DATA_THLD GENMASK(23, 16)
  27. #define QUEUE_RESP_BUF_THLD GENMASK(15, 8)
  28. #define QUEUE_CMD_EMPTY_BUF_THLD GENMASK(7, 0)
  29. #define PIO_DATA_BUFFER_THLD_CTRL 0x14
  30. #define DATA_RX_START_THLD GENMASK(26, 24)
  31. #define DATA_TX_START_THLD GENMASK(18, 16)
  32. #define DATA_RX_BUF_THLD GENMASK(10, 8)
  33. #define DATA_TX_BUF_THLD GENMASK(2, 0)
  34. #define PIO_QUEUE_SIZE 0x18
  35. #define TX_DATA_BUFFER_SIZE GENMASK(31, 24)
  36. #define RX_DATA_BUFFER_SIZE GENMASK(23, 16)
  37. #define IBI_STATUS_SIZE GENMASK(15, 8)
  38. #define CR_QUEUE_SIZE GENMASK(7, 0)
  39. #define PIO_INTR_STATUS 0x20
  40. #define PIO_INTR_STATUS_ENABLE 0x24
  41. #define PIO_INTR_SIGNAL_ENABLE 0x28
  42. #define PIO_INTR_FORCE 0x2c
  43. #define STAT_TRANSFER_BLOCKED BIT(25)
  44. #define STAT_PERR_RESP_UFLOW BIT(24)
  45. #define STAT_PERR_CMD_OFLOW BIT(23)
  46. #define STAT_PERR_IBI_UFLOW BIT(22)
  47. #define STAT_PERR_RX_UFLOW BIT(21)
  48. #define STAT_PERR_TX_OFLOW BIT(20)
  49. #define STAT_ERR_RESP_QUEUE_FULL BIT(19)
  50. #define STAT_WARN_RESP_QUEUE_FULL BIT(18)
  51. #define STAT_ERR_IBI_QUEUE_FULL BIT(17)
  52. #define STAT_WARN_IBI_QUEUE_FULL BIT(16)
  53. #define STAT_ERR_RX_DATA_FULL BIT(15)
  54. #define STAT_WARN_RX_DATA_FULL BIT(14)
  55. #define STAT_ERR_TX_DATA_EMPTY BIT(13)
  56. #define STAT_WARN_TX_DATA_EMPTY BIT(12)
  57. #define STAT_TRANSFER_ERR BIT(9)
  58. #define STAT_WARN_INS_STOP_MODE BIT(7)
  59. #define STAT_TRANSFER_ABORT BIT(5)
  60. #define STAT_RESP_READY BIT(4)
  61. #define STAT_CMD_QUEUE_READY BIT(3)
  62. #define STAT_IBI_STATUS_THLD BIT(2)
  63. #define STAT_RX_THLD BIT(1)
  64. #define STAT_TX_THLD BIT(0)
  65. #define PIO_QUEUE_CUR_STATUS 0x38
  66. #define CUR_IBI_Q_LEVEL GENMASK(28, 20)
  67. #define CUR_RESP_Q_LEVEL GENMASK(18, 10)
  68. #define CUR_CMD_Q_EMPTY_LEVEL GENMASK(8, 0)
  69. #define PIO_DATA_BUFFER_CUR_STATUS 0x3c
  70. #define CUR_RX_BUF_LVL GENMASK(26, 16)
  71. #define CUR_TX_BUF_LVL GENMASK(10, 0)
  72. /*
  73. * Handy status bit combinations
  74. */
  75. #define STAT_LATENCY_WARNINGS (STAT_WARN_RESP_QUEUE_FULL | \
  76. STAT_WARN_IBI_QUEUE_FULL | \
  77. STAT_WARN_RX_DATA_FULL | \
  78. STAT_WARN_TX_DATA_EMPTY | \
  79. STAT_WARN_INS_STOP_MODE)
  80. #define STAT_LATENCY_ERRORS (STAT_ERR_RESP_QUEUE_FULL | \
  81. STAT_ERR_IBI_QUEUE_FULL | \
  82. STAT_ERR_RX_DATA_FULL | \
  83. STAT_ERR_TX_DATA_EMPTY)
  84. #define STAT_PROG_ERRORS (STAT_TRANSFER_BLOCKED | \
  85. STAT_PERR_RESP_UFLOW | \
  86. STAT_PERR_CMD_OFLOW | \
  87. STAT_PERR_IBI_UFLOW | \
  88. STAT_PERR_RX_UFLOW | \
  89. STAT_PERR_TX_OFLOW)
  90. #define STAT_ALL_ERRORS (STAT_TRANSFER_ABORT | \
  91. STAT_TRANSFER_ERR | \
  92. STAT_LATENCY_ERRORS | \
  93. STAT_PROG_ERRORS)
  94. struct hci_pio_dev_ibi_data {
  95. struct i3c_generic_ibi_pool *pool;
  96. unsigned int max_len;
  97. };
  98. struct hci_pio_ibi_data {
  99. struct i3c_ibi_slot *slot;
  100. void *data_ptr;
  101. unsigned int addr;
  102. unsigned int seg_len, seg_cnt;
  103. unsigned int max_len;
  104. bool last_seg;
  105. };
  106. struct hci_pio_data {
  107. spinlock_t lock;
  108. struct hci_xfer *curr_xfer, *xfer_queue;
  109. struct hci_xfer *curr_rx, *rx_queue;
  110. struct hci_xfer *curr_tx, *tx_queue;
  111. struct hci_xfer *curr_resp, *resp_queue;
  112. struct hci_pio_ibi_data ibi;
  113. unsigned int rx_thresh_size, tx_thresh_size;
  114. unsigned int max_ibi_thresh;
  115. u32 reg_queue_thresh;
  116. u32 enabled_irqs;
  117. };
  118. static int hci_pio_init(struct i3c_hci *hci)
  119. {
  120. struct hci_pio_data *pio;
  121. u32 val, size_val, rx_thresh, tx_thresh, ibi_val;
  122. pio = kzalloc(sizeof(*pio), GFP_KERNEL);
  123. if (!pio)
  124. return -ENOMEM;
  125. hci->io_data = pio;
  126. spin_lock_init(&pio->lock);
  127. size_val = pio_reg_read(QUEUE_SIZE);
  128. dev_info(&hci->master.dev, "CMD/RESP FIFO = %ld entries\n",
  129. FIELD_GET(CR_QUEUE_SIZE, size_val));
  130. dev_info(&hci->master.dev, "IBI FIFO = %ld bytes\n",
  131. 4 * FIELD_GET(IBI_STATUS_SIZE, size_val));
  132. dev_info(&hci->master.dev, "RX data FIFO = %d bytes\n",
  133. 4 * (2 << FIELD_GET(RX_DATA_BUFFER_SIZE, size_val)));
  134. dev_info(&hci->master.dev, "TX data FIFO = %d bytes\n",
  135. 4 * (2 << FIELD_GET(TX_DATA_BUFFER_SIZE, size_val)));
  136. /*
  137. * Let's initialize data thresholds to half of the actual FIFO size.
  138. * The start thresholds aren't used (set to 0) as the FIFO is always
  139. * serviced before the corresponding command is queued.
  140. */
  141. rx_thresh = FIELD_GET(RX_DATA_BUFFER_SIZE, size_val);
  142. tx_thresh = FIELD_GET(TX_DATA_BUFFER_SIZE, size_val);
  143. if (hci->version_major == 1) {
  144. /* those are expressed as 2^[n+1), so just sub 1 if not 0 */
  145. if (rx_thresh)
  146. rx_thresh -= 1;
  147. if (tx_thresh)
  148. tx_thresh -= 1;
  149. pio->rx_thresh_size = 2 << rx_thresh;
  150. pio->tx_thresh_size = 2 << tx_thresh;
  151. } else {
  152. /* size is 2^(n+1) and threshold is 2^n i.e. already halved */
  153. pio->rx_thresh_size = 1 << rx_thresh;
  154. pio->tx_thresh_size = 1 << tx_thresh;
  155. }
  156. val = FIELD_PREP(DATA_RX_BUF_THLD, rx_thresh) |
  157. FIELD_PREP(DATA_TX_BUF_THLD, tx_thresh);
  158. pio_reg_write(DATA_BUFFER_THLD_CTRL, val);
  159. /*
  160. * Let's raise an interrupt as soon as there is one free cmd slot
  161. * or one available response or IBI. For IBI data let's use half the
  162. * IBI queue size within allowed bounds.
  163. */
  164. ibi_val = FIELD_GET(IBI_STATUS_SIZE, size_val);
  165. pio->max_ibi_thresh = clamp_val(ibi_val/2, 1, 63);
  166. val = FIELD_PREP(QUEUE_IBI_STATUS_THLD, 1) |
  167. FIELD_PREP(QUEUE_IBI_DATA_THLD, pio->max_ibi_thresh) |
  168. FIELD_PREP(QUEUE_RESP_BUF_THLD, 1) |
  169. FIELD_PREP(QUEUE_CMD_EMPTY_BUF_THLD, 1);
  170. pio_reg_write(QUEUE_THLD_CTRL, val);
  171. pio->reg_queue_thresh = val;
  172. /* Disable all IRQs but allow all status bits */
  173. pio_reg_write(INTR_SIGNAL_ENABLE, 0x0);
  174. pio_reg_write(INTR_STATUS_ENABLE, 0xffffffff);
  175. /* Always accept error interrupts (will be activated on first xfer) */
  176. pio->enabled_irqs = STAT_ALL_ERRORS;
  177. return 0;
  178. }
  179. static void hci_pio_cleanup(struct i3c_hci *hci)
  180. {
  181. struct hci_pio_data *pio = hci->io_data;
  182. pio_reg_write(INTR_SIGNAL_ENABLE, 0x0);
  183. if (pio) {
  184. DBG("status = %#x/%#x",
  185. pio_reg_read(INTR_STATUS), pio_reg_read(INTR_SIGNAL_ENABLE));
  186. BUG_ON(pio->curr_xfer);
  187. BUG_ON(pio->curr_rx);
  188. BUG_ON(pio->curr_tx);
  189. BUG_ON(pio->curr_resp);
  190. kfree(pio);
  191. hci->io_data = NULL;
  192. }
  193. }
  194. static void hci_pio_write_cmd(struct i3c_hci *hci, struct hci_xfer *xfer)
  195. {
  196. DBG("cmd_desc[%d] = 0x%08x", 0, xfer->cmd_desc[0]);
  197. DBG("cmd_desc[%d] = 0x%08x", 1, xfer->cmd_desc[1]);
  198. pio_reg_write(COMMAND_QUEUE_PORT, xfer->cmd_desc[0]);
  199. pio_reg_write(COMMAND_QUEUE_PORT, xfer->cmd_desc[1]);
  200. if (hci->cmd == &mipi_i3c_hci_cmd_v2) {
  201. DBG("cmd_desc[%d] = 0x%08x", 2, xfer->cmd_desc[2]);
  202. DBG("cmd_desc[%d] = 0x%08x", 3, xfer->cmd_desc[3]);
  203. pio_reg_write(COMMAND_QUEUE_PORT, xfer->cmd_desc[2]);
  204. pio_reg_write(COMMAND_QUEUE_PORT, xfer->cmd_desc[3]);
  205. }
  206. }
  207. static bool hci_pio_do_rx(struct i3c_hci *hci, struct hci_pio_data *pio)
  208. {
  209. struct hci_xfer *xfer = pio->curr_rx;
  210. unsigned int nr_words;
  211. u32 *p;
  212. p = xfer->data;
  213. p += (xfer->data_len - xfer->data_left) / 4;
  214. while (xfer->data_left >= 4) {
  215. /* bail out if FIFO hasn't reached the threshold value yet */
  216. if (!(pio_reg_read(INTR_STATUS) & STAT_RX_THLD))
  217. return false;
  218. nr_words = min(xfer->data_left / 4, pio->rx_thresh_size);
  219. /* extract data from FIFO */
  220. xfer->data_left -= nr_words * 4;
  221. DBG("now %d left %d", nr_words * 4, xfer->data_left);
  222. while (nr_words--)
  223. *p++ = pio_reg_read(XFER_DATA_PORT);
  224. }
  225. /* trailing data is retrieved upon response reception */
  226. return !xfer->data_left;
  227. }
  228. static void hci_pio_do_trailing_rx(struct i3c_hci *hci,
  229. struct hci_pio_data *pio, unsigned int count)
  230. {
  231. struct hci_xfer *xfer = pio->curr_rx;
  232. u32 *p;
  233. DBG("%d remaining", count);
  234. p = xfer->data;
  235. p += (xfer->data_len - xfer->data_left) / 4;
  236. if (count >= 4) {
  237. unsigned int nr_words = count / 4;
  238. /* extract data from FIFO */
  239. xfer->data_left -= nr_words * 4;
  240. DBG("now %d left %d", nr_words * 4, xfer->data_left);
  241. while (nr_words--)
  242. *p++ = pio_reg_read(XFER_DATA_PORT);
  243. }
  244. count &= 3;
  245. if (count) {
  246. /*
  247. * There are trailing bytes in the last word.
  248. * Fetch it and extract bytes in an endian independent way.
  249. * Unlike the TX case, we must not write memory past the
  250. * end of the destination buffer.
  251. */
  252. u8 *p_byte = (u8 *)p;
  253. u32 data = pio_reg_read(XFER_DATA_PORT);
  254. xfer->data_word_before_partial = data;
  255. xfer->data_left -= count;
  256. data = (__force u32) cpu_to_le32(data);
  257. while (count--) {
  258. *p_byte++ = data;
  259. data >>= 8;
  260. }
  261. }
  262. }
  263. static bool hci_pio_do_tx(struct i3c_hci *hci, struct hci_pio_data *pio)
  264. {
  265. struct hci_xfer *xfer = pio->curr_tx;
  266. unsigned int nr_words;
  267. u32 *p;
  268. p = xfer->data;
  269. p += (xfer->data_len - xfer->data_left) / 4;
  270. while (xfer->data_left >= 4) {
  271. /* bail out if FIFO free space is below set threshold */
  272. if (!(pio_reg_read(INTR_STATUS) & STAT_TX_THLD))
  273. return false;
  274. /* we can fill up to that TX threshold */
  275. nr_words = min(xfer->data_left / 4, pio->tx_thresh_size);
  276. /* push data into the FIFO */
  277. xfer->data_left -= nr_words * 4;
  278. DBG("now %d left %d", nr_words * 4, xfer->data_left);
  279. while (nr_words--)
  280. pio_reg_write(XFER_DATA_PORT, *p++);
  281. }
  282. if (xfer->data_left) {
  283. /*
  284. * There are trailing bytes to send. We can simply load
  285. * them from memory as a word which will keep those bytes
  286. * in their proper place even on a BE system. This will
  287. * also get some bytes past the actual buffer but no one
  288. * should care as they won't be sent out.
  289. */
  290. if (!(pio_reg_read(INTR_STATUS) & STAT_TX_THLD))
  291. return false;
  292. DBG("trailing %d", xfer->data_left);
  293. pio_reg_write(XFER_DATA_PORT, *p);
  294. xfer->data_left = 0;
  295. }
  296. return true;
  297. }
  298. static bool hci_pio_process_rx(struct i3c_hci *hci, struct hci_pio_data *pio)
  299. {
  300. while (pio->curr_rx && hci_pio_do_rx(hci, pio))
  301. pio->curr_rx = pio->curr_rx->next_data;
  302. return !pio->curr_rx;
  303. }
  304. static bool hci_pio_process_tx(struct i3c_hci *hci, struct hci_pio_data *pio)
  305. {
  306. while (pio->curr_tx && hci_pio_do_tx(hci, pio))
  307. pio->curr_tx = pio->curr_tx->next_data;
  308. return !pio->curr_tx;
  309. }
  310. static void hci_pio_queue_data(struct i3c_hci *hci, struct hci_pio_data *pio)
  311. {
  312. struct hci_xfer *xfer = pio->curr_xfer;
  313. struct hci_xfer *prev_queue_tail;
  314. if (!xfer->data) {
  315. xfer->data_len = xfer->data_left = 0;
  316. return;
  317. }
  318. if (xfer->rnw) {
  319. prev_queue_tail = pio->rx_queue;
  320. pio->rx_queue = xfer;
  321. if (pio->curr_rx) {
  322. prev_queue_tail->next_data = xfer;
  323. } else {
  324. pio->curr_rx = xfer;
  325. if (!hci_pio_process_rx(hci, pio))
  326. pio->enabled_irqs |= STAT_RX_THLD;
  327. }
  328. } else {
  329. prev_queue_tail = pio->tx_queue;
  330. pio->tx_queue = xfer;
  331. if (pio->curr_tx) {
  332. prev_queue_tail->next_data = xfer;
  333. } else {
  334. pio->curr_tx = xfer;
  335. if (!hci_pio_process_tx(hci, pio))
  336. pio->enabled_irqs |= STAT_TX_THLD;
  337. }
  338. }
  339. }
  340. static void hci_pio_push_to_next_rx(struct i3c_hci *hci, struct hci_xfer *xfer,
  341. unsigned int words_to_keep)
  342. {
  343. u32 *from = xfer->data;
  344. u32 from_last;
  345. unsigned int received, count;
  346. received = (xfer->data_len - xfer->data_left) / 4;
  347. if ((xfer->data_len - xfer->data_left) & 3) {
  348. from_last = xfer->data_word_before_partial;
  349. received += 1;
  350. } else {
  351. from_last = from[received];
  352. }
  353. from += words_to_keep;
  354. count = received - words_to_keep;
  355. while (count) {
  356. unsigned int room, left, chunk, bytes_to_move;
  357. u32 last_word;
  358. xfer = xfer->next_data;
  359. if (!xfer) {
  360. dev_err(&hci->master.dev, "pushing RX data to unexistent xfer\n");
  361. return;
  362. }
  363. room = DIV_ROUND_UP(xfer->data_len, 4);
  364. left = DIV_ROUND_UP(xfer->data_left, 4);
  365. chunk = min(count, room);
  366. if (chunk > left) {
  367. hci_pio_push_to_next_rx(hci, xfer, chunk - left);
  368. left = chunk;
  369. xfer->data_left = left * 4;
  370. }
  371. bytes_to_move = xfer->data_len - xfer->data_left;
  372. if (bytes_to_move & 3) {
  373. /* preserve word to become partial */
  374. u32 *p = xfer->data;
  375. xfer->data_word_before_partial = p[bytes_to_move / 4];
  376. }
  377. memmove(xfer->data + chunk, xfer->data, bytes_to_move);
  378. /* treat last word specially because of partial word issues */
  379. chunk -= 1;
  380. memcpy(xfer->data, from, chunk * 4);
  381. xfer->data_left -= chunk * 4;
  382. from += chunk;
  383. count -= chunk;
  384. last_word = (count == 1) ? from_last : *from++;
  385. if (xfer->data_left < 4) {
  386. /*
  387. * Like in hci_pio_do_trailing_rx(), preserve original
  388. * word to be stored partially then store bytes it
  389. * in an endian independent way.
  390. */
  391. u8 *p_byte = xfer->data;
  392. p_byte += chunk * 4;
  393. xfer->data_word_before_partial = last_word;
  394. last_word = (__force u32) cpu_to_le32(last_word);
  395. while (xfer->data_left--) {
  396. *p_byte++ = last_word;
  397. last_word >>= 8;
  398. }
  399. } else {
  400. u32 *p = xfer->data;
  401. p[chunk] = last_word;
  402. xfer->data_left -= 4;
  403. }
  404. count--;
  405. }
  406. }
  407. static void hci_pio_err(struct i3c_hci *hci, struct hci_pio_data *pio,
  408. u32 status);
  409. static bool hci_pio_process_resp(struct i3c_hci *hci, struct hci_pio_data *pio)
  410. {
  411. while (pio->curr_resp &&
  412. (pio_reg_read(INTR_STATUS) & STAT_RESP_READY)) {
  413. struct hci_xfer *xfer = pio->curr_resp;
  414. u32 resp = pio_reg_read(RESPONSE_QUEUE_PORT);
  415. unsigned int tid = RESP_TID(resp);
  416. DBG("resp = 0x%08x", resp);
  417. if (tid != xfer->cmd_tid) {
  418. dev_err(&hci->master.dev,
  419. "response tid=%d when expecting %d\n",
  420. tid, xfer->cmd_tid);
  421. /* let's pretend it is a prog error... any of them */
  422. hci_pio_err(hci, pio, STAT_PROG_ERRORS);
  423. return false;
  424. }
  425. xfer->response = resp;
  426. if (pio->curr_rx == xfer) {
  427. /*
  428. * Response availability implies RX completion.
  429. * Retrieve trailing RX data if any.
  430. * Note that short reads are possible.
  431. */
  432. unsigned int received, expected, to_keep;
  433. received = xfer->data_len - xfer->data_left;
  434. expected = RESP_DATA_LENGTH(xfer->response);
  435. if (expected > received) {
  436. hci_pio_do_trailing_rx(hci, pio,
  437. expected - received);
  438. } else if (received > expected) {
  439. /* we consumed data meant for next xfer */
  440. to_keep = DIV_ROUND_UP(expected, 4);
  441. hci_pio_push_to_next_rx(hci, xfer, to_keep);
  442. }
  443. /* then process the RX list pointer */
  444. if (hci_pio_process_rx(hci, pio))
  445. pio->enabled_irqs &= ~STAT_RX_THLD;
  446. }
  447. /*
  448. * We're about to give back ownership of the xfer structure
  449. * to the waiting instance. Make sure no reference to it
  450. * still exists.
  451. */
  452. if (pio->curr_rx == xfer) {
  453. DBG("short RX ?");
  454. pio->curr_rx = pio->curr_rx->next_data;
  455. } else if (pio->curr_tx == xfer) {
  456. DBG("short TX ?");
  457. pio->curr_tx = pio->curr_tx->next_data;
  458. } else if (xfer->data_left) {
  459. DBG("PIO xfer count = %d after response",
  460. xfer->data_left);
  461. }
  462. pio->curr_resp = xfer->next_resp;
  463. if (xfer->completion)
  464. complete(xfer->completion);
  465. }
  466. return !pio->curr_resp;
  467. }
  468. static void hci_pio_queue_resp(struct i3c_hci *hci, struct hci_pio_data *pio)
  469. {
  470. struct hci_xfer *xfer = pio->curr_xfer;
  471. struct hci_xfer *prev_queue_tail;
  472. if (!(xfer->cmd_desc[0] & CMD_0_ROC))
  473. return;
  474. prev_queue_tail = pio->resp_queue;
  475. pio->resp_queue = xfer;
  476. if (pio->curr_resp) {
  477. prev_queue_tail->next_resp = xfer;
  478. } else {
  479. pio->curr_resp = xfer;
  480. if (!hci_pio_process_resp(hci, pio))
  481. pio->enabled_irqs |= STAT_RESP_READY;
  482. }
  483. }
  484. static bool hci_pio_process_cmd(struct i3c_hci *hci, struct hci_pio_data *pio)
  485. {
  486. while (pio->curr_xfer &&
  487. (pio_reg_read(INTR_STATUS) & STAT_CMD_QUEUE_READY)) {
  488. /*
  489. * Always process the data FIFO before sending the command
  490. * so needed TX data or RX space is available upfront.
  491. */
  492. hci_pio_queue_data(hci, pio);
  493. /*
  494. * Then queue our response request. This will also process
  495. * the response FIFO in case it got suddenly filled up
  496. * with results from previous commands.
  497. */
  498. hci_pio_queue_resp(hci, pio);
  499. /*
  500. * Finally send the command.
  501. */
  502. hci_pio_write_cmd(hci, pio->curr_xfer);
  503. /*
  504. * And move on.
  505. */
  506. pio->curr_xfer = pio->curr_xfer->next_xfer;
  507. }
  508. return !pio->curr_xfer;
  509. }
  510. static int hci_pio_queue_xfer(struct i3c_hci *hci, struct hci_xfer *xfer, int n)
  511. {
  512. struct hci_pio_data *pio = hci->io_data;
  513. struct hci_xfer *prev_queue_tail;
  514. int i;
  515. DBG("n = %d", n);
  516. /* link xfer instances together and initialize data count */
  517. for (i = 0; i < n; i++) {
  518. xfer[i].next_xfer = (i + 1 < n) ? &xfer[i + 1] : NULL;
  519. xfer[i].next_data = NULL;
  520. xfer[i].next_resp = NULL;
  521. xfer[i].data_left = xfer[i].data_len;
  522. }
  523. spin_lock_irq(&pio->lock);
  524. prev_queue_tail = pio->xfer_queue;
  525. pio->xfer_queue = &xfer[n - 1];
  526. if (pio->curr_xfer) {
  527. prev_queue_tail->next_xfer = xfer;
  528. } else {
  529. pio->curr_xfer = xfer;
  530. if (!hci_pio_process_cmd(hci, pio))
  531. pio->enabled_irqs |= STAT_CMD_QUEUE_READY;
  532. pio_reg_write(INTR_SIGNAL_ENABLE, pio->enabled_irqs);
  533. DBG("status = %#x/%#x",
  534. pio_reg_read(INTR_STATUS), pio_reg_read(INTR_SIGNAL_ENABLE));
  535. }
  536. spin_unlock_irq(&pio->lock);
  537. return 0;
  538. }
  539. static bool hci_pio_dequeue_xfer_common(struct i3c_hci *hci,
  540. struct hci_pio_data *pio,
  541. struct hci_xfer *xfer, int n)
  542. {
  543. struct hci_xfer *p, **p_prev_next;
  544. int i;
  545. /*
  546. * To safely dequeue a transfer request, it must be either entirely
  547. * processed, or not yet processed at all. If our request tail is
  548. * reachable from either the data or resp list that means the command
  549. * was submitted and not yet completed.
  550. */
  551. for (p = pio->curr_resp; p; p = p->next_resp)
  552. for (i = 0; i < n; i++)
  553. if (p == &xfer[i])
  554. goto pio_screwed;
  555. for (p = pio->curr_rx; p; p = p->next_data)
  556. for (i = 0; i < n; i++)
  557. if (p == &xfer[i])
  558. goto pio_screwed;
  559. for (p = pio->curr_tx; p; p = p->next_data)
  560. for (i = 0; i < n; i++)
  561. if (p == &xfer[i])
  562. goto pio_screwed;
  563. /*
  564. * The command was completed, or wasn't yet submitted.
  565. * Unlink it from the que if the later.
  566. */
  567. p_prev_next = &pio->curr_xfer;
  568. for (p = pio->curr_xfer; p; p = p->next_xfer) {
  569. if (p == &xfer[0]) {
  570. *p_prev_next = xfer[n - 1].next_xfer;
  571. break;
  572. }
  573. p_prev_next = &p->next_xfer;
  574. }
  575. /* return true if we actually unqueued something */
  576. return !!p;
  577. pio_screwed:
  578. /*
  579. * Life is tough. We must invalidate the hardware state and
  580. * discard everything that is still queued.
  581. */
  582. for (p = pio->curr_resp; p; p = p->next_resp) {
  583. p->response = FIELD_PREP(RESP_ERR_FIELD, RESP_ERR_HC_TERMINATED);
  584. if (p->completion)
  585. complete(p->completion);
  586. }
  587. for (p = pio->curr_xfer; p; p = p->next_xfer) {
  588. p->response = FIELD_PREP(RESP_ERR_FIELD, RESP_ERR_HC_TERMINATED);
  589. if (p->completion)
  590. complete(p->completion);
  591. }
  592. pio->curr_xfer = pio->curr_rx = pio->curr_tx = pio->curr_resp = NULL;
  593. return true;
  594. }
  595. static bool hci_pio_dequeue_xfer(struct i3c_hci *hci, struct hci_xfer *xfer, int n)
  596. {
  597. struct hci_pio_data *pio = hci->io_data;
  598. int ret;
  599. spin_lock_irq(&pio->lock);
  600. DBG("n=%d status=%#x/%#x", n,
  601. pio_reg_read(INTR_STATUS), pio_reg_read(INTR_SIGNAL_ENABLE));
  602. DBG("main_status = %#x/%#x",
  603. readl(hci->base_regs + 0x20), readl(hci->base_regs + 0x28));
  604. ret = hci_pio_dequeue_xfer_common(hci, pio, xfer, n);
  605. spin_unlock_irq(&pio->lock);
  606. return ret;
  607. }
  608. static void hci_pio_err(struct i3c_hci *hci, struct hci_pio_data *pio,
  609. u32 status)
  610. {
  611. /* TODO: this ought to be more sophisticated eventually */
  612. if (pio_reg_read(INTR_STATUS) & STAT_RESP_READY) {
  613. /* this may happen when an error is signaled with ROC unset */
  614. u32 resp = pio_reg_read(RESPONSE_QUEUE_PORT);
  615. dev_err(&hci->master.dev,
  616. "orphan response (%#x) on error\n", resp);
  617. }
  618. /* dump states on programming errors */
  619. if (status & STAT_PROG_ERRORS) {
  620. u32 queue = pio_reg_read(QUEUE_CUR_STATUS);
  621. u32 data = pio_reg_read(DATA_BUFFER_CUR_STATUS);
  622. dev_err(&hci->master.dev,
  623. "prog error %#lx (C/R/I = %ld/%ld/%ld, TX/RX = %ld/%ld)\n",
  624. status & STAT_PROG_ERRORS,
  625. FIELD_GET(CUR_CMD_Q_EMPTY_LEVEL, queue),
  626. FIELD_GET(CUR_RESP_Q_LEVEL, queue),
  627. FIELD_GET(CUR_IBI_Q_LEVEL, queue),
  628. FIELD_GET(CUR_TX_BUF_LVL, data),
  629. FIELD_GET(CUR_RX_BUF_LVL, data));
  630. }
  631. /* just bust out everything with pending responses for now */
  632. hci_pio_dequeue_xfer_common(hci, pio, pio->curr_resp, 1);
  633. /* ... and half-way TX transfers if any */
  634. if (pio->curr_tx && pio->curr_tx->data_left != pio->curr_tx->data_len)
  635. hci_pio_dequeue_xfer_common(hci, pio, pio->curr_tx, 1);
  636. /* then reset the hardware */
  637. mipi_i3c_hci_pio_reset(hci);
  638. mipi_i3c_hci_resume(hci);
  639. DBG("status=%#x/%#x",
  640. pio_reg_read(INTR_STATUS), pio_reg_read(INTR_SIGNAL_ENABLE));
  641. }
  642. static void hci_pio_set_ibi_thresh(struct i3c_hci *hci,
  643. struct hci_pio_data *pio,
  644. unsigned int thresh_val)
  645. {
  646. u32 regval = pio->reg_queue_thresh;
  647. regval &= ~QUEUE_IBI_STATUS_THLD;
  648. regval |= FIELD_PREP(QUEUE_IBI_STATUS_THLD, thresh_val);
  649. /* write the threshold reg only if it changes */
  650. if (regval != pio->reg_queue_thresh) {
  651. pio_reg_write(QUEUE_THLD_CTRL, regval);
  652. pio->reg_queue_thresh = regval;
  653. DBG("%d", thresh_val);
  654. }
  655. }
  656. static bool hci_pio_get_ibi_segment(struct i3c_hci *hci,
  657. struct hci_pio_data *pio)
  658. {
  659. struct hci_pio_ibi_data *ibi = &pio->ibi;
  660. unsigned int nr_words, thresh_val;
  661. u32 *p;
  662. p = ibi->data_ptr;
  663. p += (ibi->seg_len - ibi->seg_cnt) / 4;
  664. while ((nr_words = ibi->seg_cnt/4)) {
  665. /* determine our IBI queue threshold value */
  666. thresh_val = min(nr_words, pio->max_ibi_thresh);
  667. hci_pio_set_ibi_thresh(hci, pio, thresh_val);
  668. /* bail out if we don't have that amount of data ready */
  669. if (!(pio_reg_read(INTR_STATUS) & STAT_IBI_STATUS_THLD))
  670. return false;
  671. /* extract the data from the IBI port */
  672. nr_words = thresh_val;
  673. ibi->seg_cnt -= nr_words * 4;
  674. DBG("now %d left %d", nr_words * 4, ibi->seg_cnt);
  675. while (nr_words--)
  676. *p++ = pio_reg_read(IBI_PORT);
  677. }
  678. if (ibi->seg_cnt) {
  679. /*
  680. * There are trailing bytes in the last word.
  681. * Fetch it and extract bytes in an endian independent way.
  682. * Unlike the TX case, we must not write past the end of
  683. * the destination buffer.
  684. */
  685. u32 data;
  686. u8 *p_byte = (u8 *)p;
  687. hci_pio_set_ibi_thresh(hci, pio, 1);
  688. if (!(pio_reg_read(INTR_STATUS) & STAT_IBI_STATUS_THLD))
  689. return false;
  690. DBG("trailing %d", ibi->seg_cnt);
  691. data = pio_reg_read(IBI_PORT);
  692. data = (__force u32) cpu_to_le32(data);
  693. while (ibi->seg_cnt--) {
  694. *p_byte++ = data;
  695. data >>= 8;
  696. }
  697. }
  698. return true;
  699. }
  700. static bool hci_pio_prep_new_ibi(struct i3c_hci *hci, struct hci_pio_data *pio)
  701. {
  702. struct hci_pio_ibi_data *ibi = &pio->ibi;
  703. struct i3c_dev_desc *dev;
  704. struct i3c_hci_dev_data *dev_data;
  705. struct hci_pio_dev_ibi_data *dev_ibi;
  706. u32 ibi_status;
  707. /*
  708. * We have a new IBI. Try to set up its payload retrieval.
  709. * When returning true, the IBI data has to be consumed whether
  710. * or not we are set up to capture it. If we return true with
  711. * ibi->slot == NULL that means the data payload has to be
  712. * drained out of the IBI port and dropped.
  713. */
  714. ibi_status = pio_reg_read(IBI_PORT);
  715. DBG("status = %#x", ibi_status);
  716. ibi->addr = FIELD_GET(IBI_TARGET_ADDR, ibi_status);
  717. if (ibi_status & IBI_ERROR) {
  718. dev_err(&hci->master.dev, "IBI error from %#x\n", ibi->addr);
  719. return false;
  720. }
  721. ibi->last_seg = ibi_status & IBI_LAST_STATUS;
  722. ibi->seg_len = FIELD_GET(IBI_DATA_LENGTH, ibi_status);
  723. ibi->seg_cnt = ibi->seg_len;
  724. dev = i3c_hci_addr_to_dev(hci, ibi->addr);
  725. if (!dev) {
  726. dev_err(&hci->master.dev,
  727. "IBI for unknown device %#x\n", ibi->addr);
  728. return true;
  729. }
  730. dev_data = i3c_dev_get_master_data(dev);
  731. dev_ibi = dev_data->ibi_data;
  732. ibi->max_len = dev_ibi->max_len;
  733. if (ibi->seg_len > ibi->max_len) {
  734. dev_err(&hci->master.dev, "IBI payload too big (%d > %d)\n",
  735. ibi->seg_len, ibi->max_len);
  736. return true;
  737. }
  738. ibi->slot = i3c_generic_ibi_get_free_slot(dev_ibi->pool);
  739. if (!ibi->slot) {
  740. dev_err(&hci->master.dev, "no free slot for IBI\n");
  741. } else {
  742. ibi->slot->len = 0;
  743. ibi->data_ptr = ibi->slot->data;
  744. }
  745. return true;
  746. }
  747. static void hci_pio_free_ibi_slot(struct i3c_hci *hci, struct hci_pio_data *pio)
  748. {
  749. struct hci_pio_ibi_data *ibi = &pio->ibi;
  750. struct hci_pio_dev_ibi_data *dev_ibi;
  751. if (ibi->slot) {
  752. dev_ibi = ibi->slot->dev->common.master_priv;
  753. i3c_generic_ibi_recycle_slot(dev_ibi->pool, ibi->slot);
  754. ibi->slot = NULL;
  755. }
  756. }
  757. static bool hci_pio_process_ibi(struct i3c_hci *hci, struct hci_pio_data *pio)
  758. {
  759. struct hci_pio_ibi_data *ibi = &pio->ibi;
  760. if (!ibi->slot && !ibi->seg_cnt && ibi->last_seg)
  761. if (!hci_pio_prep_new_ibi(hci, pio))
  762. return false;
  763. for (;;) {
  764. u32 ibi_status;
  765. unsigned int ibi_addr;
  766. if (ibi->slot) {
  767. if (!hci_pio_get_ibi_segment(hci, pio))
  768. return false;
  769. ibi->slot->len += ibi->seg_len;
  770. ibi->data_ptr += ibi->seg_len;
  771. if (ibi->last_seg) {
  772. /* was the last segment: submit it and leave */
  773. i3c_master_queue_ibi(ibi->slot->dev, ibi->slot);
  774. ibi->slot = NULL;
  775. hci_pio_set_ibi_thresh(hci, pio, 1);
  776. return true;
  777. }
  778. } else if (ibi->seg_cnt) {
  779. /*
  780. * No slot but a non-zero count. This is the result
  781. * of some error and the payload must be drained.
  782. * This normally does not happen therefore no need
  783. * to be extra optimized here.
  784. */
  785. hci_pio_set_ibi_thresh(hci, pio, 1);
  786. do {
  787. if (!(pio_reg_read(INTR_STATUS) & STAT_IBI_STATUS_THLD))
  788. return false;
  789. pio_reg_read(IBI_PORT);
  790. } while (--ibi->seg_cnt);
  791. if (ibi->last_seg)
  792. return true;
  793. }
  794. /* try to move to the next segment right away */
  795. hci_pio_set_ibi_thresh(hci, pio, 1);
  796. if (!(pio_reg_read(INTR_STATUS) & STAT_IBI_STATUS_THLD))
  797. return false;
  798. ibi_status = pio_reg_read(IBI_PORT);
  799. ibi_addr = FIELD_GET(IBI_TARGET_ADDR, ibi_status);
  800. if (ibi->addr != ibi_addr) {
  801. /* target address changed before last segment */
  802. dev_err(&hci->master.dev,
  803. "unexp IBI address changed from %d to %d\n",
  804. ibi->addr, ibi_addr);
  805. hci_pio_free_ibi_slot(hci, pio);
  806. }
  807. ibi->last_seg = ibi_status & IBI_LAST_STATUS;
  808. ibi->seg_len = FIELD_GET(IBI_DATA_LENGTH, ibi_status);
  809. ibi->seg_cnt = ibi->seg_len;
  810. if (ibi->slot && ibi->slot->len + ibi->seg_len > ibi->max_len) {
  811. dev_err(&hci->master.dev,
  812. "IBI payload too big (%d > %d)\n",
  813. ibi->slot->len + ibi->seg_len, ibi->max_len);
  814. hci_pio_free_ibi_slot(hci, pio);
  815. }
  816. }
  817. return false;
  818. }
  819. static int hci_pio_request_ibi(struct i3c_hci *hci, struct i3c_dev_desc *dev,
  820. const struct i3c_ibi_setup *req)
  821. {
  822. struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
  823. struct i3c_generic_ibi_pool *pool;
  824. struct hci_pio_dev_ibi_data *dev_ibi;
  825. dev_ibi = kmalloc(sizeof(*dev_ibi), GFP_KERNEL);
  826. if (!dev_ibi)
  827. return -ENOMEM;
  828. pool = i3c_generic_ibi_alloc_pool(dev, req);
  829. if (IS_ERR(pool)) {
  830. kfree(dev_ibi);
  831. return PTR_ERR(pool);
  832. }
  833. dev_ibi->pool = pool;
  834. dev_ibi->max_len = req->max_payload_len;
  835. dev_data->ibi_data = dev_ibi;
  836. return 0;
  837. }
  838. static void hci_pio_free_ibi(struct i3c_hci *hci, struct i3c_dev_desc *dev)
  839. {
  840. struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
  841. struct hci_pio_dev_ibi_data *dev_ibi = dev_data->ibi_data;
  842. dev_data->ibi_data = NULL;
  843. i3c_generic_ibi_free_pool(dev_ibi->pool);
  844. kfree(dev_ibi);
  845. }
  846. static void hci_pio_recycle_ibi_slot(struct i3c_hci *hci,
  847. struct i3c_dev_desc *dev,
  848. struct i3c_ibi_slot *slot)
  849. {
  850. struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
  851. struct hci_pio_dev_ibi_data *dev_ibi = dev_data->ibi_data;
  852. i3c_generic_ibi_recycle_slot(dev_ibi->pool, slot);
  853. }
  854. static bool hci_pio_irq_handler(struct i3c_hci *hci, unsigned int unused)
  855. {
  856. struct hci_pio_data *pio = hci->io_data;
  857. u32 status;
  858. spin_lock(&pio->lock);
  859. status = pio_reg_read(INTR_STATUS);
  860. DBG("(in) status: %#x/%#x", status, pio->enabled_irqs);
  861. status &= pio->enabled_irqs | STAT_LATENCY_WARNINGS;
  862. if (!status) {
  863. spin_unlock(&pio->lock);
  864. return false;
  865. }
  866. if (status & STAT_IBI_STATUS_THLD)
  867. hci_pio_process_ibi(hci, pio);
  868. if (status & STAT_RX_THLD)
  869. if (hci_pio_process_rx(hci, pio))
  870. pio->enabled_irqs &= ~STAT_RX_THLD;
  871. if (status & STAT_TX_THLD)
  872. if (hci_pio_process_tx(hci, pio))
  873. pio->enabled_irqs &= ~STAT_TX_THLD;
  874. if (status & STAT_RESP_READY)
  875. if (hci_pio_process_resp(hci, pio))
  876. pio->enabled_irqs &= ~STAT_RESP_READY;
  877. if (unlikely(status & STAT_LATENCY_WARNINGS)) {
  878. pio_reg_write(INTR_STATUS, status & STAT_LATENCY_WARNINGS);
  879. dev_warn_ratelimited(&hci->master.dev,
  880. "encountered warning condition %#lx\n",
  881. status & STAT_LATENCY_WARNINGS);
  882. }
  883. if (unlikely(status & STAT_ALL_ERRORS)) {
  884. pio_reg_write(INTR_STATUS, status & STAT_ALL_ERRORS);
  885. hci_pio_err(hci, pio, status & STAT_ALL_ERRORS);
  886. }
  887. if (status & STAT_CMD_QUEUE_READY)
  888. if (hci_pio_process_cmd(hci, pio))
  889. pio->enabled_irqs &= ~STAT_CMD_QUEUE_READY;
  890. pio_reg_write(INTR_SIGNAL_ENABLE, pio->enabled_irqs);
  891. DBG("(out) status: %#x/%#x",
  892. pio_reg_read(INTR_STATUS), pio_reg_read(INTR_SIGNAL_ENABLE));
  893. spin_unlock(&pio->lock);
  894. return true;
  895. }
  896. const struct hci_io_ops mipi_i3c_hci_pio = {
  897. .init = hci_pio_init,
  898. .cleanup = hci_pio_cleanup,
  899. .queue_xfer = hci_pio_queue_xfer,
  900. .dequeue_xfer = hci_pio_dequeue_xfer,
  901. .irq_handler = hci_pio_irq_handler,
  902. .request_ibi = hci_pio_request_ibi,
  903. .free_ibi = hci_pio_free_ibi,
  904. .recycle_ibi_slot = hci_pio_recycle_ibi_slot,
  905. };