keystone_nav.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Multicore Navigator driver for TI Keystone 2 devices.
  4. *
  5. * (C) Copyright 2012-2014
  6. * Texas Instruments Incorporated, <www.ti.com>
  7. */
  8. #include <common.h>
  9. #include <asm/io.h>
  10. #include <asm/ti-common/keystone_nav.h>
  11. struct qm_config qm_memmap = {
  12. .stat_cfg = CONFIG_KSNAV_QM_QUEUE_STATUS_BASE,
  13. .queue = (void *)CONFIG_KSNAV_QM_MANAGER_QUEUES_BASE,
  14. .mngr_vbusm = CONFIG_KSNAV_QM_BASE_ADDRESS,
  15. .i_lram = CONFIG_KSNAV_QM_LINK_RAM_BASE,
  16. .proxy = (void *)CONFIG_KSNAV_QM_MANAGER_Q_PROXY_BASE,
  17. .status_ram = CONFIG_KSNAV_QM_STATUS_RAM_BASE,
  18. .mngr_cfg = (void *)CONFIG_KSNAV_QM_CONF_BASE,
  19. .intd_cfg = CONFIG_KSNAV_QM_INTD_CONF_BASE,
  20. .desc_mem = (void *)CONFIG_KSNAV_QM_DESC_SETUP_BASE,
  21. .region_num = CONFIG_KSNAV_QM_REGION_NUM,
  22. .pdsp_cmd = CONFIG_KSNAV_QM_PDSP1_CMD_BASE,
  23. .pdsp_ctl = CONFIG_KSNAV_QM_PDSP1_CTRL_BASE,
  24. .pdsp_iram = CONFIG_KSNAV_QM_PDSP1_IRAM_BASE,
  25. .qpool_num = CONFIG_KSNAV_QM_QPOOL_NUM,
  26. };
  27. /*
  28. * We are going to use only one type of descriptors - host packet
  29. * descriptors. We staticaly allocate memory for them here
  30. */
  31. struct qm_host_desc desc_pool[HDESC_NUM] __aligned(sizeof(struct qm_host_desc));
  32. static struct qm_config *qm_cfg;
  33. inline int num_of_desc_to_reg(int num_descr)
  34. {
  35. int j, num;
  36. for (j = 0, num = 32; j < 15; j++, num *= 2) {
  37. if (num_descr <= num)
  38. return j;
  39. }
  40. return 15;
  41. }
  42. int _qm_init(struct qm_config *cfg)
  43. {
  44. u32 j;
  45. qm_cfg = cfg;
  46. qm_cfg->mngr_cfg->link_ram_base0 = qm_cfg->i_lram;
  47. qm_cfg->mngr_cfg->link_ram_size0 = HDESC_NUM * 8 - 1;
  48. qm_cfg->mngr_cfg->link_ram_base1 = 0;
  49. qm_cfg->mngr_cfg->link_ram_size1 = 0;
  50. qm_cfg->mngr_cfg->link_ram_base2 = 0;
  51. qm_cfg->desc_mem[0].base_addr = (u32)desc_pool;
  52. qm_cfg->desc_mem[0].start_idx = 0;
  53. qm_cfg->desc_mem[0].desc_reg_size =
  54. (((sizeof(struct qm_host_desc) >> 4) - 1) << 16) |
  55. num_of_desc_to_reg(HDESC_NUM);
  56. memset(desc_pool, 0, sizeof(desc_pool));
  57. for (j = 0; j < HDESC_NUM; j++)
  58. qm_push(&desc_pool[j], qm_cfg->qpool_num);
  59. return QM_OK;
  60. }
  61. int qm_init(void)
  62. {
  63. return _qm_init(&qm_memmap);
  64. }
  65. void qm_close(void)
  66. {
  67. u32 j;
  68. queue_close(qm_cfg->qpool_num);
  69. qm_cfg->mngr_cfg->link_ram_base0 = 0;
  70. qm_cfg->mngr_cfg->link_ram_size0 = 0;
  71. qm_cfg->mngr_cfg->link_ram_base1 = 0;
  72. qm_cfg->mngr_cfg->link_ram_size1 = 0;
  73. qm_cfg->mngr_cfg->link_ram_base2 = 0;
  74. for (j = 0; j < qm_cfg->region_num; j++) {
  75. qm_cfg->desc_mem[j].base_addr = 0;
  76. qm_cfg->desc_mem[j].start_idx = 0;
  77. qm_cfg->desc_mem[j].desc_reg_size = 0;
  78. }
  79. qm_cfg = NULL;
  80. }
  81. void qm_push(struct qm_host_desc *hd, u32 qnum)
  82. {
  83. u32 regd;
  84. cpu_to_bus((u32 *)hd, sizeof(struct qm_host_desc)/4);
  85. regd = (u32)hd | ((sizeof(struct qm_host_desc) >> 4) - 1);
  86. writel(regd, &qm_cfg->queue[qnum].ptr_size_thresh);
  87. }
  88. void qm_buff_push(struct qm_host_desc *hd, u32 qnum,
  89. void *buff_ptr, u32 buff_len)
  90. {
  91. hd->orig_buff_len = buff_len;
  92. hd->buff_len = buff_len;
  93. hd->orig_buff_ptr = (u32)buff_ptr;
  94. hd->buff_ptr = (u32)buff_ptr;
  95. qm_push(hd, qnum);
  96. }
  97. struct qm_host_desc *qm_pop(u32 qnum)
  98. {
  99. u32 uhd;
  100. uhd = readl(&qm_cfg->queue[qnum].ptr_size_thresh) & ~0xf;
  101. if (uhd)
  102. cpu_to_bus((u32 *)uhd, sizeof(struct qm_host_desc)/4);
  103. return (struct qm_host_desc *)uhd;
  104. }
  105. struct qm_host_desc *qm_pop_from_free_pool(void)
  106. {
  107. return qm_pop(qm_cfg->qpool_num);
  108. }
  109. void queue_close(u32 qnum)
  110. {
  111. struct qm_host_desc *hd;
  112. while ((hd = qm_pop(qnum)))
  113. ;
  114. }
  115. /**
  116. * DMA API
  117. */
  118. static int ksnav_rx_disable(struct pktdma_cfg *pktdma)
  119. {
  120. u32 j, v, k;
  121. for (j = 0; j < pktdma->rx_ch_num; j++) {
  122. v = readl(&pktdma->rx_ch[j].cfg_a);
  123. if (!(v & CPDMA_CHAN_A_ENABLE))
  124. continue;
  125. writel(v | CPDMA_CHAN_A_TDOWN, &pktdma->rx_ch[j].cfg_a);
  126. for (k = 0; k < TDOWN_TIMEOUT_COUNT; k++) {
  127. udelay(100);
  128. v = readl(&pktdma->rx_ch[j].cfg_a);
  129. if (!(v & CPDMA_CHAN_A_ENABLE))
  130. continue;
  131. }
  132. /* TODO: teardown error on if TDOWN_TIMEOUT_COUNT is reached */
  133. }
  134. /* Clear all of the flow registers */
  135. for (j = 0; j < pktdma->rx_flow_num; j++) {
  136. writel(0, &pktdma->rx_flows[j].control);
  137. writel(0, &pktdma->rx_flows[j].tags);
  138. writel(0, &pktdma->rx_flows[j].tag_sel);
  139. writel(0, &pktdma->rx_flows[j].fdq_sel[0]);
  140. writel(0, &pktdma->rx_flows[j].fdq_sel[1]);
  141. writel(0, &pktdma->rx_flows[j].thresh[0]);
  142. writel(0, &pktdma->rx_flows[j].thresh[1]);
  143. writel(0, &pktdma->rx_flows[j].thresh[2]);
  144. }
  145. return QM_OK;
  146. }
  147. static int ksnav_tx_disable(struct pktdma_cfg *pktdma)
  148. {
  149. u32 j, v, k;
  150. for (j = 0; j < pktdma->tx_ch_num; j++) {
  151. v = readl(&pktdma->tx_ch[j].cfg_a);
  152. if (!(v & CPDMA_CHAN_A_ENABLE))
  153. continue;
  154. writel(v | CPDMA_CHAN_A_TDOWN, &pktdma->tx_ch[j].cfg_a);
  155. for (k = 0; k < TDOWN_TIMEOUT_COUNT; k++) {
  156. udelay(100);
  157. v = readl(&pktdma->tx_ch[j].cfg_a);
  158. if (!(v & CPDMA_CHAN_A_ENABLE))
  159. continue;
  160. }
  161. /* TODO: teardown error on if TDOWN_TIMEOUT_COUNT is reached */
  162. }
  163. return QM_OK;
  164. }
  165. int ksnav_init(struct pktdma_cfg *pktdma, struct rx_buff_desc *rx_buffers)
  166. {
  167. u32 j, v;
  168. struct qm_host_desc *hd;
  169. u8 *rx_ptr;
  170. if (pktdma == NULL || rx_buffers == NULL ||
  171. rx_buffers->buff_ptr == NULL || qm_cfg == NULL)
  172. return QM_ERR;
  173. pktdma->rx_flow = rx_buffers->rx_flow;
  174. /* init rx queue */
  175. rx_ptr = rx_buffers->buff_ptr;
  176. for (j = 0; j < rx_buffers->num_buffs; j++) {
  177. hd = qm_pop(qm_cfg->qpool_num);
  178. if (hd == NULL)
  179. return QM_ERR;
  180. qm_buff_push(hd, pktdma->rx_free_q,
  181. rx_ptr, rx_buffers->buff_len);
  182. rx_ptr += rx_buffers->buff_len;
  183. }
  184. ksnav_rx_disable(pktdma);
  185. /* configure rx channels */
  186. v = CPDMA_REG_VAL_MAKE_RX_FLOW_A(1, 1, 0, 0, 0, 0, 0, pktdma->rx_rcv_q);
  187. writel(v, &pktdma->rx_flows[pktdma->rx_flow].control);
  188. writel(0, &pktdma->rx_flows[pktdma->rx_flow].tags);
  189. writel(0, &pktdma->rx_flows[pktdma->rx_flow].tag_sel);
  190. v = CPDMA_REG_VAL_MAKE_RX_FLOW_D(0, pktdma->rx_free_q, 0,
  191. pktdma->rx_free_q);
  192. writel(v, &pktdma->rx_flows[pktdma->rx_flow].fdq_sel[0]);
  193. writel(v, &pktdma->rx_flows[pktdma->rx_flow].fdq_sel[1]);
  194. writel(0, &pktdma->rx_flows[pktdma->rx_flow].thresh[0]);
  195. writel(0, &pktdma->rx_flows[pktdma->rx_flow].thresh[1]);
  196. writel(0, &pktdma->rx_flows[pktdma->rx_flow].thresh[2]);
  197. for (j = 0; j < pktdma->rx_ch_num; j++)
  198. writel(CPDMA_CHAN_A_ENABLE, &pktdma->rx_ch[j].cfg_a);
  199. /* configure tx channels */
  200. /* Disable loopback in the tx direction */
  201. writel(0, &pktdma->global->emulation_control);
  202. /* Set QM base address, only for K2x devices */
  203. writel(CONFIG_KSNAV_QM_BASE_ADDRESS, &pktdma->global->qm_base_addr[0]);
  204. /* Enable all channels. The current state isn't important */
  205. for (j = 0; j < pktdma->tx_ch_num; j++) {
  206. writel(0, &pktdma->tx_ch[j].cfg_b);
  207. writel(CPDMA_CHAN_A_ENABLE, &pktdma->tx_ch[j].cfg_a);
  208. }
  209. return QM_OK;
  210. }
  211. int ksnav_close(struct pktdma_cfg *pktdma)
  212. {
  213. if (!pktdma)
  214. return QM_ERR;
  215. ksnav_tx_disable(pktdma);
  216. ksnav_rx_disable(pktdma);
  217. queue_close(pktdma->rx_free_q);
  218. queue_close(pktdma->rx_rcv_q);
  219. queue_close(pktdma->tx_snd_q);
  220. return QM_OK;
  221. }
  222. int ksnav_send(struct pktdma_cfg *pktdma, u32 *pkt, int num_bytes, u32 swinfo2)
  223. {
  224. struct qm_host_desc *hd;
  225. hd = qm_pop(qm_cfg->qpool_num);
  226. if (hd == NULL)
  227. return QM_ERR;
  228. hd->desc_info = num_bytes;
  229. hd->swinfo[2] = swinfo2;
  230. hd->packet_info = qm_cfg->qpool_num;
  231. qm_buff_push(hd, pktdma->tx_snd_q, pkt, num_bytes);
  232. return QM_OK;
  233. }
  234. void *ksnav_recv(struct pktdma_cfg *pktdma, u32 **pkt, int *num_bytes)
  235. {
  236. struct qm_host_desc *hd;
  237. hd = qm_pop(pktdma->rx_rcv_q);
  238. if (!hd)
  239. return NULL;
  240. *pkt = (u32 *)hd->buff_ptr;
  241. *num_bytes = hd->desc_info & 0x3fffff;
  242. return hd;
  243. }
  244. void ksnav_release_rxhd(struct pktdma_cfg *pktdma, void *hd)
  245. {
  246. struct qm_host_desc *_hd = (struct qm_host_desc *)hd;
  247. _hd->buff_len = _hd->orig_buff_len;
  248. _hd->buff_ptr = _hd->orig_buff_ptr;
  249. qm_push(_hd, pktdma->rx_free_q);
  250. }