Vitaly Andrianov | 06c6ea7 | 2014-04-01 15:01:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Multicore Navigator driver for TI Keystone 2 devices. |
| 3 | * |
| 4 | * (C) Copyright 2012-2014 |
| 5 | * Texas Instruments Incorporated, <www.ti.com> |
| 6 | * |
| 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | #include <common.h> |
| 10 | #include <asm/io.h> |
| 11 | #include <asm/arch/keystone_nav.h> |
| 12 | |
| 13 | static int soc_type = |
| 14 | #ifdef CONFIG_SOC_K2HK |
| 15 | k2hk; |
| 16 | #endif |
| 17 | |
Khoronzhuk, Ivan | 29310e7 | 2014-09-05 19:02:45 +0300 | [diff] [blame^] | 18 | struct qm_config qm_memmap = { |
| 19 | .stat_cfg = KS2_QM_QUEUE_STATUS_BASE, |
| 20 | .queue = (void *)KS2_QM_MANAGER_QUEUES_BASE, |
| 21 | .mngr_vbusm = KS2_QM_BASE_ADDRESS, |
| 22 | .i_lram = KS2_QM_LINK_RAM_BASE, |
| 23 | .proxy = (void *)KS2_QM_MANAGER_Q_PROXY_BASE, |
| 24 | .status_ram = KS2_QM_STATUS_RAM_BASE, |
| 25 | .mngr_cfg = (void *)KS2_QM_CONF_BASE, |
| 26 | .intd_cfg = KS2_QM_INTD_CONF_BASE, |
| 27 | .desc_mem = (void *)KS2_QM_DESC_SETUP_BASE, |
| 28 | .region_num = KS2_QM_REGION_NUM, |
| 29 | .pdsp_cmd = KS2_QM_PDSP1_CMD_BASE, |
| 30 | .pdsp_ctl = KS2_QM_PDSP1_CTRL_BASE, |
| 31 | .pdsp_iram = KS2_QM_PDSP1_IRAM_BASE, |
| 32 | .qpool_num = KS2_QM_QPOOL_NUM, |
Vitaly Andrianov | 06c6ea7 | 2014-04-01 15:01:12 -0400 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * We are going to use only one type of descriptors - host packet |
| 37 | * descriptors. We staticaly allocate memory for them here |
| 38 | */ |
| 39 | struct qm_host_desc desc_pool[HDESC_NUM] __aligned(sizeof(struct qm_host_desc)); |
| 40 | |
| 41 | static struct qm_config *qm_cfg; |
| 42 | |
| 43 | inline int num_of_desc_to_reg(int num_descr) |
| 44 | { |
| 45 | int j, num; |
| 46 | |
| 47 | for (j = 0, num = 32; j < 15; j++, num *= 2) { |
| 48 | if (num_descr <= num) |
| 49 | return j; |
| 50 | } |
| 51 | |
| 52 | return 15; |
| 53 | } |
| 54 | |
Khoronzhuk, Ivan | 29310e7 | 2014-09-05 19:02:45 +0300 | [diff] [blame^] | 55 | int _qm_init(struct qm_config *cfg) |
Vitaly Andrianov | 06c6ea7 | 2014-04-01 15:01:12 -0400 | [diff] [blame] | 56 | { |
Khoronzhuk, Ivan | 29310e7 | 2014-09-05 19:02:45 +0300 | [diff] [blame^] | 57 | u32 j; |
Vitaly Andrianov | 06c6ea7 | 2014-04-01 15:01:12 -0400 | [diff] [blame] | 58 | |
| 59 | qm_cfg = cfg; |
| 60 | |
| 61 | qm_cfg->mngr_cfg->link_ram_base0 = qm_cfg->i_lram; |
| 62 | qm_cfg->mngr_cfg->link_ram_size0 = HDESC_NUM * 8; |
| 63 | qm_cfg->mngr_cfg->link_ram_base1 = 0; |
| 64 | qm_cfg->mngr_cfg->link_ram_size1 = 0; |
| 65 | qm_cfg->mngr_cfg->link_ram_base2 = 0; |
| 66 | |
| 67 | qm_cfg->desc_mem[0].base_addr = (u32)desc_pool; |
| 68 | qm_cfg->desc_mem[0].start_idx = 0; |
| 69 | qm_cfg->desc_mem[0].desc_reg_size = |
| 70 | (((sizeof(struct qm_host_desc) >> 4) - 1) << 16) | |
| 71 | num_of_desc_to_reg(HDESC_NUM); |
| 72 | |
| 73 | memset(desc_pool, 0, sizeof(desc_pool)); |
| 74 | for (j = 0; j < HDESC_NUM; j++) |
| 75 | qm_push(&desc_pool[j], qm_cfg->qpool_num); |
| 76 | |
| 77 | return QM_OK; |
| 78 | } |
| 79 | |
| 80 | int qm_init(void) |
| 81 | { |
Khoronzhuk, Ivan | 29310e7 | 2014-09-05 19:02:45 +0300 | [diff] [blame^] | 82 | return _qm_init(&qm_memmap); |
Vitaly Andrianov | 06c6ea7 | 2014-04-01 15:01:12 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void qm_close(void) |
| 86 | { |
| 87 | u32 j; |
| 88 | |
| 89 | if (qm_cfg == NULL) |
| 90 | return; |
| 91 | |
| 92 | queue_close(qm_cfg->qpool_num); |
| 93 | |
| 94 | qm_cfg->mngr_cfg->link_ram_base0 = 0; |
| 95 | qm_cfg->mngr_cfg->link_ram_size0 = 0; |
| 96 | qm_cfg->mngr_cfg->link_ram_base1 = 0; |
| 97 | qm_cfg->mngr_cfg->link_ram_size1 = 0; |
| 98 | qm_cfg->mngr_cfg->link_ram_base2 = 0; |
| 99 | |
| 100 | for (j = 0; j < qm_cfg->region_num; j++) { |
| 101 | qm_cfg->desc_mem[j].base_addr = 0; |
| 102 | qm_cfg->desc_mem[j].start_idx = 0; |
| 103 | qm_cfg->desc_mem[j].desc_reg_size = 0; |
| 104 | } |
| 105 | |
| 106 | qm_cfg = NULL; |
| 107 | } |
| 108 | |
| 109 | void qm_push(struct qm_host_desc *hd, u32 qnum) |
| 110 | { |
| 111 | u32 regd; |
| 112 | |
| 113 | if (!qm_cfg) |
| 114 | return; |
| 115 | |
| 116 | cpu_to_bus((u32 *)hd, sizeof(struct qm_host_desc)/4); |
| 117 | regd = (u32)hd | ((sizeof(struct qm_host_desc) >> 4) - 1); |
| 118 | writel(regd, &qm_cfg->queue[qnum].ptr_size_thresh); |
| 119 | } |
| 120 | |
| 121 | void qm_buff_push(struct qm_host_desc *hd, u32 qnum, |
| 122 | void *buff_ptr, u32 buff_len) |
| 123 | { |
| 124 | hd->orig_buff_len = buff_len; |
| 125 | hd->buff_len = buff_len; |
| 126 | hd->orig_buff_ptr = (u32)buff_ptr; |
| 127 | hd->buff_ptr = (u32)buff_ptr; |
| 128 | qm_push(hd, qnum); |
| 129 | } |
| 130 | |
| 131 | struct qm_host_desc *qm_pop(u32 qnum) |
| 132 | { |
| 133 | u32 uhd; |
| 134 | |
| 135 | if (!qm_cfg) |
| 136 | return NULL; |
| 137 | |
| 138 | uhd = readl(&qm_cfg->queue[qnum].ptr_size_thresh) & ~0xf; |
| 139 | if (uhd) |
| 140 | cpu_to_bus((u32 *)uhd, sizeof(struct qm_host_desc)/4); |
| 141 | |
| 142 | return (struct qm_host_desc *)uhd; |
| 143 | } |
| 144 | |
| 145 | struct qm_host_desc *qm_pop_from_free_pool(void) |
| 146 | { |
| 147 | if (!qm_cfg) |
| 148 | return NULL; |
| 149 | |
| 150 | return qm_pop(qm_cfg->qpool_num); |
| 151 | } |
| 152 | |
| 153 | void queue_close(u32 qnum) |
| 154 | { |
| 155 | struct qm_host_desc *hd; |
| 156 | |
| 157 | while ((hd = qm_pop(qnum))) |
| 158 | ; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * DMA API |
| 163 | */ |
| 164 | |
| 165 | struct pktdma_cfg k2hk_netcp_pktdma = { |
| 166 | .global = (struct global_ctl_regs *)0x02004000, |
| 167 | .tx_ch = (struct tx_chan_regs *)0x02004400, |
| 168 | .tx_ch_num = 9, |
| 169 | .rx_ch = (struct rx_chan_regs *)0x02004800, |
| 170 | .rx_ch_num = 26, |
| 171 | .tx_sched = (u32 *)0x02004c00, |
| 172 | .rx_flows = (struct rx_flow_regs *)0x02005000, |
| 173 | .rx_flow_num = 32, |
| 174 | .rx_free_q = 4001, |
| 175 | .rx_rcv_q = 4002, |
| 176 | .tx_snd_q = 648, |
| 177 | }; |
| 178 | |
| 179 | struct pktdma_cfg *netcp; |
| 180 | |
| 181 | static int netcp_rx_disable(void) |
| 182 | { |
| 183 | u32 j, v, k; |
| 184 | |
| 185 | for (j = 0; j < netcp->rx_ch_num; j++) { |
| 186 | v = readl(&netcp->rx_ch[j].cfg_a); |
| 187 | if (!(v & CPDMA_CHAN_A_ENABLE)) |
| 188 | continue; |
| 189 | |
| 190 | writel(v | CPDMA_CHAN_A_TDOWN, &netcp->rx_ch[j].cfg_a); |
| 191 | for (k = 0; k < TDOWN_TIMEOUT_COUNT; k++) { |
| 192 | udelay(100); |
| 193 | v = readl(&netcp->rx_ch[j].cfg_a); |
| 194 | if (!(v & CPDMA_CHAN_A_ENABLE)) |
| 195 | continue; |
| 196 | } |
| 197 | /* TODO: teardown error on if TDOWN_TIMEOUT_COUNT is reached */ |
| 198 | } |
| 199 | |
| 200 | /* Clear all of the flow registers */ |
| 201 | for (j = 0; j < netcp->rx_flow_num; j++) { |
| 202 | writel(0, &netcp->rx_flows[j].control); |
| 203 | writel(0, &netcp->rx_flows[j].tags); |
| 204 | writel(0, &netcp->rx_flows[j].tag_sel); |
| 205 | writel(0, &netcp->rx_flows[j].fdq_sel[0]); |
| 206 | writel(0, &netcp->rx_flows[j].fdq_sel[1]); |
| 207 | writel(0, &netcp->rx_flows[j].thresh[0]); |
| 208 | writel(0, &netcp->rx_flows[j].thresh[1]); |
| 209 | writel(0, &netcp->rx_flows[j].thresh[2]); |
| 210 | } |
| 211 | |
| 212 | return QM_OK; |
| 213 | } |
| 214 | |
| 215 | static int netcp_tx_disable(void) |
| 216 | { |
| 217 | u32 j, v, k; |
| 218 | |
| 219 | for (j = 0; j < netcp->tx_ch_num; j++) { |
| 220 | v = readl(&netcp->tx_ch[j].cfg_a); |
| 221 | if (!(v & CPDMA_CHAN_A_ENABLE)) |
| 222 | continue; |
| 223 | |
| 224 | writel(v | CPDMA_CHAN_A_TDOWN, &netcp->tx_ch[j].cfg_a); |
| 225 | for (k = 0; k < TDOWN_TIMEOUT_COUNT; k++) { |
| 226 | udelay(100); |
| 227 | v = readl(&netcp->tx_ch[j].cfg_a); |
| 228 | if (!(v & CPDMA_CHAN_A_ENABLE)) |
| 229 | continue; |
| 230 | } |
| 231 | /* TODO: teardown error on if TDOWN_TIMEOUT_COUNT is reached */ |
| 232 | } |
| 233 | |
| 234 | return QM_OK; |
| 235 | } |
| 236 | |
| 237 | static int _netcp_init(struct pktdma_cfg *netcp_cfg, |
| 238 | struct rx_buff_desc *rx_buffers) |
| 239 | { |
| 240 | u32 j, v; |
| 241 | struct qm_host_desc *hd; |
| 242 | u8 *rx_ptr; |
| 243 | |
| 244 | if (netcp_cfg == NULL || rx_buffers == NULL || |
| 245 | rx_buffers->buff_ptr == NULL || qm_cfg == NULL) |
| 246 | return QM_ERR; |
| 247 | |
| 248 | netcp = netcp_cfg; |
| 249 | netcp->rx_flow = rx_buffers->rx_flow; |
| 250 | |
| 251 | /* init rx queue */ |
| 252 | rx_ptr = rx_buffers->buff_ptr; |
| 253 | |
| 254 | for (j = 0; j < rx_buffers->num_buffs; j++) { |
| 255 | hd = qm_pop(qm_cfg->qpool_num); |
| 256 | if (hd == NULL) |
| 257 | return QM_ERR; |
| 258 | |
| 259 | qm_buff_push(hd, netcp->rx_free_q, |
| 260 | rx_ptr, rx_buffers->buff_len); |
| 261 | |
| 262 | rx_ptr += rx_buffers->buff_len; |
| 263 | } |
| 264 | |
| 265 | netcp_rx_disable(); |
| 266 | |
| 267 | /* configure rx channels */ |
| 268 | v = CPDMA_REG_VAL_MAKE_RX_FLOW_A(1, 1, 0, 0, 0, 0, 0, netcp->rx_rcv_q); |
| 269 | writel(v, &netcp->rx_flows[netcp->rx_flow].control); |
| 270 | writel(0, &netcp->rx_flows[netcp->rx_flow].tags); |
| 271 | writel(0, &netcp->rx_flows[netcp->rx_flow].tag_sel); |
| 272 | |
| 273 | v = CPDMA_REG_VAL_MAKE_RX_FLOW_D(0, netcp->rx_free_q, 0, |
| 274 | netcp->rx_free_q); |
| 275 | |
| 276 | writel(v, &netcp->rx_flows[netcp->rx_flow].fdq_sel[0]); |
| 277 | writel(v, &netcp->rx_flows[netcp->rx_flow].fdq_sel[1]); |
| 278 | writel(0, &netcp->rx_flows[netcp->rx_flow].thresh[0]); |
| 279 | writel(0, &netcp->rx_flows[netcp->rx_flow].thresh[1]); |
| 280 | writel(0, &netcp->rx_flows[netcp->rx_flow].thresh[2]); |
| 281 | |
| 282 | for (j = 0; j < netcp->rx_ch_num; j++) |
| 283 | writel(CPDMA_CHAN_A_ENABLE, &netcp->rx_ch[j].cfg_a); |
| 284 | |
| 285 | /* configure tx channels */ |
| 286 | /* Disable loopback in the tx direction */ |
| 287 | writel(0, &netcp->global->emulation_control); |
| 288 | |
Vitaly Andrianov | 06c6ea7 | 2014-04-01 15:01:12 -0400 | [diff] [blame] | 289 | /* Set QM base address, only for K2x devices */ |
Khoronzhuk, Ivan | 29310e7 | 2014-09-05 19:02:45 +0300 | [diff] [blame^] | 290 | writel(KS2_QM_BASE_ADDRESS, &netcp->global->qm_base_addr[0]); |
Vitaly Andrianov | 06c6ea7 | 2014-04-01 15:01:12 -0400 | [diff] [blame] | 291 | |
| 292 | /* Enable all channels. The current state isn't important */ |
| 293 | for (j = 0; j < netcp->tx_ch_num; j++) { |
| 294 | writel(0, &netcp->tx_ch[j].cfg_b); |
| 295 | writel(CPDMA_CHAN_A_ENABLE, &netcp->tx_ch[j].cfg_a); |
| 296 | } |
| 297 | |
| 298 | return QM_OK; |
| 299 | } |
| 300 | |
| 301 | int netcp_init(struct rx_buff_desc *rx_buffers) |
| 302 | { |
| 303 | switch (soc_type) { |
| 304 | case k2hk: |
| 305 | _netcp_init(&k2hk_netcp_pktdma, rx_buffers); |
| 306 | return QM_OK; |
| 307 | } |
| 308 | return QM_ERR; |
| 309 | } |
| 310 | |
| 311 | int netcp_close(void) |
| 312 | { |
| 313 | if (!netcp) |
| 314 | return QM_ERR; |
| 315 | |
| 316 | netcp_tx_disable(); |
| 317 | netcp_rx_disable(); |
| 318 | |
| 319 | queue_close(netcp->rx_free_q); |
| 320 | queue_close(netcp->rx_rcv_q); |
| 321 | queue_close(netcp->tx_snd_q); |
| 322 | |
| 323 | return QM_OK; |
| 324 | } |
| 325 | |
| 326 | int netcp_send(u32 *pkt, int num_bytes, u32 swinfo2) |
| 327 | { |
| 328 | struct qm_host_desc *hd; |
| 329 | |
| 330 | hd = qm_pop(qm_cfg->qpool_num); |
| 331 | if (hd == NULL) |
| 332 | return QM_ERR; |
| 333 | |
| 334 | hd->desc_info = num_bytes; |
| 335 | hd->swinfo[2] = swinfo2; |
| 336 | hd->packet_info = qm_cfg->qpool_num; |
| 337 | |
| 338 | qm_buff_push(hd, netcp->tx_snd_q, pkt, num_bytes); |
| 339 | |
| 340 | return QM_OK; |
| 341 | } |
| 342 | |
| 343 | void *netcp_recv(u32 **pkt, int *num_bytes) |
| 344 | { |
| 345 | struct qm_host_desc *hd; |
| 346 | |
| 347 | hd = qm_pop(netcp->rx_rcv_q); |
| 348 | if (!hd) |
| 349 | return NULL; |
| 350 | |
| 351 | *pkt = (u32 *)hd->buff_ptr; |
| 352 | *num_bytes = hd->desc_info & 0x3fffff; |
| 353 | |
| 354 | return hd; |
| 355 | } |
| 356 | |
| 357 | void netcp_release_rxhd(void *hd) |
| 358 | { |
| 359 | struct qm_host_desc *_hd = (struct qm_host_desc *)hd; |
| 360 | |
| 361 | _hd->buff_len = _hd->orig_buff_len; |
| 362 | _hd->buff_ptr = _hd->orig_buff_ptr; |
| 363 | |
| 364 | qm_push(_hd, netcp->rx_free_q); |
| 365 | } |