blob: 57d9fbfabbffd7e22ed1145acd8898aa7344e480 [file] [log] [blame]
Vignesh Rffcc66e2019-02-05 17:31:24 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com
4 * Author: Peter Ujfalusi <peter.ujfalusi@ti.com>
5 */
6#define pr_fmt(fmt) "udma: " fmt
7
8#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060011#include <asm/cache.h>
Vignesh Rffcc66e2019-02-05 17:31:24 +053012#include <asm/io.h>
13#include <asm/bitops.h>
14#include <malloc.h>
Simon Glasscd93d622020-05-10 11:40:13 -060015#include <linux/bitops.h>
Masahiro Yamada9d86b892020-02-14 16:40:19 +090016#include <linux/dma-mapping.h>
Vignesh Rffcc66e2019-02-05 17:31:24 +053017#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070019#include <dm/devres.h>
Vignesh Rffcc66e2019-02-05 17:31:24 +053020#include <dm/read.h>
21#include <dm/of_access.h>
22#include <dma.h>
23#include <dma-uclass.h>
24#include <linux/delay.h>
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +053025#include <linux/bitmap.h>
Simon Glass61b29b82020-02-03 07:36:15 -070026#include <linux/err.h>
Vignesh Rffcc66e2019-02-05 17:31:24 +053027#include <linux/soc/ti/k3-navss-ringacc.h>
28#include <linux/soc/ti/cppi5.h>
29#include <linux/soc/ti/ti-udma.h>
30#include <linux/soc/ti/ti_sci_protocol.h>
31
32#include "k3-udma-hwdef.h"
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +053033#include "k3-psil-priv.h"
Vignesh Rffcc66e2019-02-05 17:31:24 +053034
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +053035#define K3_UDMA_MAX_RFLOWS 1024
36
Vignesh Rffcc66e2019-02-05 17:31:24 +053037struct udma_chan;
38
39enum udma_mmr {
40 MMR_GCFG = 0,
41 MMR_RCHANRT,
42 MMR_TCHANRT,
43 MMR_LAST,
44};
45
46static const char * const mmr_names[] = {
47 "gcfg", "rchanrt", "tchanrt"
48};
49
50struct udma_tchan {
51 void __iomem *reg_rt;
52
53 int id;
54 struct k3_nav_ring *t_ring; /* Transmit ring */
55 struct k3_nav_ring *tc_ring; /* Transmit Completion ring */
56};
57
58struct udma_rchan {
59 void __iomem *reg_rt;
60
61 int id;
Vignesh Rffcc66e2019-02-05 17:31:24 +053062};
63
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +053064#define UDMA_FLAG_PDMA_ACC32 BIT(0)
65#define UDMA_FLAG_PDMA_BURST BIT(1)
66#define UDMA_FLAG_TDTYPE BIT(2)
67
68struct udma_match_data {
69 u32 psil_base;
70 bool enable_memcpy_support;
71 u32 flags;
72 u32 statictr_z_mask;
73 u32 rchan_oes_offset;
74
75 u8 tpl_levels;
76 u32 level_start_idx[];
77};
78
Vignesh Rffcc66e2019-02-05 17:31:24 +053079struct udma_rflow {
80 int id;
Vignesh Raghavendra7be51212020-07-06 13:26:26 +053081
82 struct k3_nav_ring *fd_ring; /* Free Descriptor ring */
83 struct k3_nav_ring *r_ring; /* Receive ring*/
Vignesh Rffcc66e2019-02-05 17:31:24 +053084};
85
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +053086enum udma_rm_range {
87 RM_RANGE_TCHAN = 0,
88 RM_RANGE_RCHAN,
89 RM_RANGE_RFLOW,
90 RM_RANGE_LAST,
91};
92
93struct udma_tisci_rm {
94 const struct ti_sci_handle *tisci;
95 const struct ti_sci_rm_udmap_ops *tisci_udmap_ops;
96 u32 tisci_dev_id;
97
98 /* tisci information for PSI-L thread pairing/unpairing */
99 const struct ti_sci_rm_psil_ops *tisci_psil_ops;
100 u32 tisci_navss_dev_id;
101
102 struct ti_sci_resource *rm_ranges[RM_RANGE_LAST];
103};
104
Vignesh Rffcc66e2019-02-05 17:31:24 +0530105struct udma_dev {
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530106 struct udevice *dev;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530107 void __iomem *mmrs[MMR_LAST];
108
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530109 struct udma_tisci_rm tisci_rm;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530110 struct k3_nav_ringacc *ringacc;
111
112 u32 features;
113
114 int tchan_cnt;
115 int echan_cnt;
116 int rchan_cnt;
117 int rflow_cnt;
118 unsigned long *tchan_map;
119 unsigned long *rchan_map;
120 unsigned long *rflow_map;
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530121 unsigned long *rflow_map_reserved;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530122
123 struct udma_tchan *tchans;
124 struct udma_rchan *rchans;
125 struct udma_rflow *rflows;
126
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +0530127 struct udma_match_data *match_data;
128
Vignesh Rffcc66e2019-02-05 17:31:24 +0530129 struct udma_chan *channels;
130 u32 psil_base;
131
132 u32 ch_count;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530133};
134
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530135struct udma_chan_config {
136 u32 psd_size; /* size of Protocol Specific Data */
137 u32 metadata_size; /* (needs_epib ? 16:0) + psd_size */
138 u32 hdesc_size; /* Size of a packet descriptor in packet mode */
139 int remote_thread_id;
140 u32 atype;
141 u32 src_thread;
142 u32 dst_thread;
143 enum psil_endpoint_type ep_type;
144 enum udma_tp_level channel_tpl; /* Channel Throughput Level */
145
146 enum dma_direction dir;
147
148 unsigned int pkt_mode:1; /* TR or packet */
149 unsigned int needs_epib:1; /* EPIB is needed for the communication or not */
150 unsigned int enable_acc32:1;
151 unsigned int enable_burst:1;
152 unsigned int notdpkt:1; /* Suppress sending TDC packet */
153};
154
Vignesh Rffcc66e2019-02-05 17:31:24 +0530155struct udma_chan {
156 struct udma_dev *ud;
157 char name[20];
158
159 struct udma_tchan *tchan;
160 struct udma_rchan *rchan;
161 struct udma_rflow *rflow;
162
Vignesh Raghavendra5e6d9cc2019-12-04 22:17:21 +0530163 struct ti_udma_drv_chan_cfg_data cfg_data;
164
Vignesh Rffcc66e2019-02-05 17:31:24 +0530165 u32 bcnt; /* number of bytes completed since the start of the channel */
166
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530167 struct udma_chan_config config;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530168
169 u32 id;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530170
171 struct cppi5_host_desc_t *desc_tx;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530172 bool in_use;
173 void *desc_rx;
174 u32 num_rx_bufs;
175 u32 desc_rx_cur;
176
177};
178
179#define UDMA_CH_1000(ch) (ch * 0x1000)
180#define UDMA_CH_100(ch) (ch * 0x100)
181#define UDMA_CH_40(ch) (ch * 0x40)
182
183#ifdef PKTBUFSRX
184#define UDMA_RX_DESC_NUM PKTBUFSRX
185#else
186#define UDMA_RX_DESC_NUM 4
187#endif
188
189/* Generic register access functions */
190static inline u32 udma_read(void __iomem *base, int reg)
191{
192 u32 v;
193
194 v = __raw_readl(base + reg);
195 pr_debug("READL(32): v(%08X)<--reg(%p)\n", v, base + reg);
196 return v;
197}
198
199static inline void udma_write(void __iomem *base, int reg, u32 val)
200{
201 pr_debug("WRITEL(32): v(%08X)-->reg(%p)\n", val, base + reg);
202 __raw_writel(val, base + reg);
203}
204
205static inline void udma_update_bits(void __iomem *base, int reg,
206 u32 mask, u32 val)
207{
208 u32 tmp, orig;
209
210 orig = udma_read(base, reg);
211 tmp = orig & ~mask;
212 tmp |= (val & mask);
213
214 if (tmp != orig)
215 udma_write(base, reg, tmp);
216}
217
218/* TCHANRT */
219static inline u32 udma_tchanrt_read(struct udma_tchan *tchan, int reg)
220{
221 if (!tchan)
222 return 0;
223 return udma_read(tchan->reg_rt, reg);
224}
225
226static inline void udma_tchanrt_write(struct udma_tchan *tchan,
227 int reg, u32 val)
228{
229 if (!tchan)
230 return;
231 udma_write(tchan->reg_rt, reg, val);
232}
233
234/* RCHANRT */
235static inline u32 udma_rchanrt_read(struct udma_rchan *rchan, int reg)
236{
237 if (!rchan)
238 return 0;
239 return udma_read(rchan->reg_rt, reg);
240}
241
242static inline void udma_rchanrt_write(struct udma_rchan *rchan,
243 int reg, u32 val)
244{
245 if (!rchan)
246 return;
247 udma_write(rchan->reg_rt, reg, val);
248}
249
250static inline int udma_navss_psil_pair(struct udma_dev *ud, u32 src_thread,
251 u32 dst_thread)
252{
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530253 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
254
Vignesh Rffcc66e2019-02-05 17:31:24 +0530255 dst_thread |= UDMA_PSIL_DST_THREAD_ID_OFFSET;
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530256
257 return tisci_rm->tisci_psil_ops->pair(tisci_rm->tisci,
258 tisci_rm->tisci_navss_dev_id,
259 src_thread, dst_thread);
Vignesh Rffcc66e2019-02-05 17:31:24 +0530260}
261
262static inline int udma_navss_psil_unpair(struct udma_dev *ud, u32 src_thread,
263 u32 dst_thread)
264{
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530265 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
266
Vignesh Rffcc66e2019-02-05 17:31:24 +0530267 dst_thread |= UDMA_PSIL_DST_THREAD_ID_OFFSET;
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530268
269 return tisci_rm->tisci_psil_ops->unpair(tisci_rm->tisci,
270 tisci_rm->tisci_navss_dev_id,
271 src_thread, dst_thread);
Vignesh Rffcc66e2019-02-05 17:31:24 +0530272}
273
274static inline char *udma_get_dir_text(enum dma_direction dir)
275{
276 switch (dir) {
277 case DMA_DEV_TO_MEM:
278 return "DEV_TO_MEM";
279 case DMA_MEM_TO_DEV:
280 return "MEM_TO_DEV";
281 case DMA_MEM_TO_MEM:
282 return "MEM_TO_MEM";
283 case DMA_DEV_TO_DEV:
284 return "DEV_TO_DEV";
285 default:
286 break;
287 }
288
289 return "invalid";
290}
291
292static inline bool udma_is_chan_running(struct udma_chan *uc)
293{
294 u32 trt_ctl = 0;
295 u32 rrt_ctl = 0;
296
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530297 switch (uc->config.dir) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530298 case DMA_DEV_TO_MEM:
299 rrt_ctl = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_CTL_REG);
300 pr_debug("%s: rrt_ctl: 0x%08x (peer: 0x%08x)\n",
301 __func__, rrt_ctl,
302 udma_rchanrt_read(uc->rchan,
303 UDMA_RCHAN_RT_PEER_RT_EN_REG));
304 break;
305 case DMA_MEM_TO_DEV:
306 trt_ctl = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_CTL_REG);
307 pr_debug("%s: trt_ctl: 0x%08x (peer: 0x%08x)\n",
308 __func__, trt_ctl,
309 udma_tchanrt_read(uc->tchan,
310 UDMA_TCHAN_RT_PEER_RT_EN_REG));
311 break;
312 case DMA_MEM_TO_MEM:
313 trt_ctl = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_CTL_REG);
314 rrt_ctl = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_CTL_REG);
315 break;
316 default:
317 break;
318 }
319
320 if (trt_ctl & UDMA_CHAN_RT_CTL_EN || rrt_ctl & UDMA_CHAN_RT_CTL_EN)
321 return true;
322
323 return false;
324}
325
Vignesh Rffcc66e2019-02-05 17:31:24 +0530326static int udma_pop_from_ring(struct udma_chan *uc, dma_addr_t *addr)
327{
328 struct k3_nav_ring *ring = NULL;
329 int ret = -ENOENT;
330
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530331 switch (uc->config.dir) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530332 case DMA_DEV_TO_MEM:
Vignesh Raghavendra7be51212020-07-06 13:26:26 +0530333 ring = uc->rflow->r_ring;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530334 break;
335 case DMA_MEM_TO_DEV:
336 ring = uc->tchan->tc_ring;
337 break;
338 case DMA_MEM_TO_MEM:
339 ring = uc->tchan->tc_ring;
340 break;
341 default:
342 break;
343 }
344
345 if (ring && k3_nav_ringacc_ring_get_occ(ring))
346 ret = k3_nav_ringacc_ring_pop(ring, addr);
347
348 return ret;
349}
350
351static void udma_reset_rings(struct udma_chan *uc)
352{
353 struct k3_nav_ring *ring1 = NULL;
354 struct k3_nav_ring *ring2 = NULL;
355
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530356 switch (uc->config.dir) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530357 case DMA_DEV_TO_MEM:
Vignesh Raghavendra7be51212020-07-06 13:26:26 +0530358 ring1 = uc->rflow->fd_ring;
359 ring2 = uc->rflow->r_ring;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530360 break;
361 case DMA_MEM_TO_DEV:
362 ring1 = uc->tchan->t_ring;
363 ring2 = uc->tchan->tc_ring;
364 break;
365 case DMA_MEM_TO_MEM:
366 ring1 = uc->tchan->t_ring;
367 ring2 = uc->tchan->tc_ring;
368 break;
369 default:
370 break;
371 }
372
373 if (ring1)
374 k3_nav_ringacc_ring_reset_dma(ring1, 0);
375 if (ring2)
376 k3_nav_ringacc_ring_reset(ring2);
377}
378
379static void udma_reset_counters(struct udma_chan *uc)
380{
381 u32 val;
382
383 if (uc->tchan) {
384 val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_BCNT_REG);
385 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_BCNT_REG, val);
386
387 val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_SBCNT_REG);
388 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_SBCNT_REG, val);
389
390 val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_PCNT_REG);
391 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PCNT_REG, val);
392
393 val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_PEER_BCNT_REG);
394 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PEER_BCNT_REG, val);
395 }
396
397 if (uc->rchan) {
398 val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_BCNT_REG);
399 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_BCNT_REG, val);
400
401 val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_SBCNT_REG);
402 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_SBCNT_REG, val);
403
404 val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_PCNT_REG);
405 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PCNT_REG, val);
406
407 val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_PEER_BCNT_REG);
408 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PEER_BCNT_REG, val);
409 }
410
411 uc->bcnt = 0;
412}
413
414static inline int udma_stop_hard(struct udma_chan *uc)
415{
416 pr_debug("%s: ENTER (chan%d)\n", __func__, uc->id);
417
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530418 switch (uc->config.dir) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530419 case DMA_DEV_TO_MEM:
420 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PEER_RT_EN_REG, 0);
421 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG, 0);
422 break;
423 case DMA_MEM_TO_DEV:
424 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG, 0);
425 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PEER_RT_EN_REG, 0);
426 break;
427 case DMA_MEM_TO_MEM:
428 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG, 0);
429 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG, 0);
430 break;
431 default:
432 return -EINVAL;
433 }
434
435 return 0;
436}
437
438static int udma_start(struct udma_chan *uc)
439{
440 /* Channel is already running, no need to proceed further */
441 if (udma_is_chan_running(uc))
442 goto out;
443
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530444 pr_debug("%s: chan:%d dir:%s\n",
445 __func__, uc->id, udma_get_dir_text(uc->config.dir));
Vignesh Rffcc66e2019-02-05 17:31:24 +0530446
447 /* Make sure that we clear the teardown bit, if it is set */
448 udma_stop_hard(uc);
449
450 /* Reset all counters */
451 udma_reset_counters(uc);
452
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530453 switch (uc->config.dir) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530454 case DMA_DEV_TO_MEM:
455 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG,
456 UDMA_CHAN_RT_CTL_EN);
457
458 /* Enable remote */
459 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PEER_RT_EN_REG,
460 UDMA_PEER_RT_EN_ENABLE);
461
462 pr_debug("%s(rx): RT_CTL:0x%08x PEER RT_ENABLE:0x%08x\n",
463 __func__,
464 udma_rchanrt_read(uc->rchan,
465 UDMA_RCHAN_RT_CTL_REG),
466 udma_rchanrt_read(uc->rchan,
467 UDMA_RCHAN_RT_PEER_RT_EN_REG));
468 break;
469 case DMA_MEM_TO_DEV:
470 /* Enable remote */
471 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_PEER_RT_EN_REG,
472 UDMA_PEER_RT_EN_ENABLE);
473
474 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
475 UDMA_CHAN_RT_CTL_EN);
476
477 pr_debug("%s(tx): RT_CTL:0x%08x PEER RT_ENABLE:0x%08x\n",
478 __func__,
Vignesh Raghavendrac16cdd42019-12-09 10:25:36 +0530479 udma_tchanrt_read(uc->tchan,
Vignesh Rffcc66e2019-02-05 17:31:24 +0530480 UDMA_TCHAN_RT_CTL_REG),
Vignesh Raghavendrac16cdd42019-12-09 10:25:36 +0530481 udma_tchanrt_read(uc->tchan,
Vignesh Rffcc66e2019-02-05 17:31:24 +0530482 UDMA_TCHAN_RT_PEER_RT_EN_REG));
483 break;
484 case DMA_MEM_TO_MEM:
485 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG,
486 UDMA_CHAN_RT_CTL_EN);
487 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
488 UDMA_CHAN_RT_CTL_EN);
489
490 break;
491 default:
492 return -EINVAL;
493 }
494
495 pr_debug("%s: DONE chan:%d\n", __func__, uc->id);
496out:
497 return 0;
498}
499
500static inline void udma_stop_mem2dev(struct udma_chan *uc, bool sync)
501{
502 int i = 0;
503 u32 val;
504
505 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG,
506 UDMA_CHAN_RT_CTL_EN |
507 UDMA_CHAN_RT_CTL_TDOWN);
508
509 val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_CTL_REG);
510
511 while (sync && (val & UDMA_CHAN_RT_CTL_EN)) {
512 val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_CTL_REG);
513 udelay(1);
514 if (i > 1000) {
515 printf(" %s TIMEOUT !\n", __func__);
516 break;
517 }
518 i++;
519 }
520
521 val = udma_tchanrt_read(uc->tchan, UDMA_TCHAN_RT_PEER_RT_EN_REG);
522 if (val & UDMA_PEER_RT_EN_ENABLE)
523 printf("%s: peer not stopped TIMEOUT !\n", __func__);
524}
525
526static inline void udma_stop_dev2mem(struct udma_chan *uc, bool sync)
527{
528 int i = 0;
529 u32 val;
530
531 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_PEER_RT_EN_REG,
532 UDMA_PEER_RT_EN_ENABLE |
533 UDMA_PEER_RT_EN_TEARDOWN);
534
535 val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_CTL_REG);
536
537 while (sync && (val & UDMA_CHAN_RT_CTL_EN)) {
538 val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_CTL_REG);
539 udelay(1);
540 if (i > 1000) {
541 printf("%s TIMEOUT !\n", __func__);
542 break;
543 }
544 i++;
545 }
546
547 val = udma_rchanrt_read(uc->rchan, UDMA_RCHAN_RT_PEER_RT_EN_REG);
548 if (val & UDMA_PEER_RT_EN_ENABLE)
549 printf("%s: peer not stopped TIMEOUT !\n", __func__);
550}
551
552static inline int udma_stop(struct udma_chan *uc)
553{
554 pr_debug("%s: chan:%d dir:%s\n",
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530555 __func__, uc->id, udma_get_dir_text(uc->config.dir));
Vignesh Rffcc66e2019-02-05 17:31:24 +0530556
557 udma_reset_counters(uc);
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530558 switch (uc->config.dir) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530559 case DMA_DEV_TO_MEM:
560 udma_stop_dev2mem(uc, true);
561 break;
562 case DMA_MEM_TO_DEV:
563 udma_stop_mem2dev(uc, true);
564 break;
565 case DMA_MEM_TO_MEM:
566 udma_rchanrt_write(uc->rchan, UDMA_RCHAN_RT_CTL_REG, 0);
567 udma_tchanrt_write(uc->tchan, UDMA_TCHAN_RT_CTL_REG, 0);
568 break;
569 default:
570 return -EINVAL;
571 }
572
573 return 0;
574}
575
576static void udma_poll_completion(struct udma_chan *uc, dma_addr_t *paddr)
577{
578 int i = 1;
579
580 while (udma_pop_from_ring(uc, paddr)) {
581 udelay(1);
582 if (!(i % 1000000))
583 printf(".");
584 i++;
585 }
586}
587
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530588static struct udma_rflow *__udma_reserve_rflow(struct udma_dev *ud, int id)
589{
590 DECLARE_BITMAP(tmp, K3_UDMA_MAX_RFLOWS);
591
592 if (id >= 0) {
593 if (test_bit(id, ud->rflow_map)) {
594 dev_err(ud->dev, "rflow%d is in use\n", id);
595 return ERR_PTR(-ENOENT);
596 }
597 } else {
598 bitmap_or(tmp, ud->rflow_map, ud->rflow_map_reserved,
599 ud->rflow_cnt);
600
601 id = find_next_zero_bit(tmp, ud->rflow_cnt, ud->rchan_cnt);
602 if (id >= ud->rflow_cnt)
603 return ERR_PTR(-ENOENT);
604 }
605
606 __set_bit(id, ud->rflow_map);
607 return &ud->rflows[id];
608}
609
Vignesh Rffcc66e2019-02-05 17:31:24 +0530610#define UDMA_RESERVE_RESOURCE(res) \
611static struct udma_##res *__udma_reserve_##res(struct udma_dev *ud, \
612 int id) \
613{ \
614 if (id >= 0) { \
615 if (test_bit(id, ud->res##_map)) { \
616 dev_err(ud->dev, "res##%d is in use\n", id); \
617 return ERR_PTR(-ENOENT); \
618 } \
619 } else { \
620 id = find_first_zero_bit(ud->res##_map, ud->res##_cnt); \
621 if (id == ud->res##_cnt) { \
622 return ERR_PTR(-ENOENT); \
623 } \
624 } \
625 \
626 __set_bit(id, ud->res##_map); \
627 return &ud->res##s[id]; \
628}
629
630UDMA_RESERVE_RESOURCE(tchan);
631UDMA_RESERVE_RESOURCE(rchan);
Vignesh Rffcc66e2019-02-05 17:31:24 +0530632
633static int udma_get_tchan(struct udma_chan *uc)
634{
635 struct udma_dev *ud = uc->ud;
636
637 if (uc->tchan) {
638 dev_dbg(ud->dev, "chan%d: already have tchan%d allocated\n",
639 uc->id, uc->tchan->id);
640 return 0;
641 }
642
643 uc->tchan = __udma_reserve_tchan(ud, -1);
644 if (IS_ERR(uc->tchan))
645 return PTR_ERR(uc->tchan);
646
647 pr_debug("chan%d: got tchan%d\n", uc->id, uc->tchan->id);
648
Vignesh Rffcc66e2019-02-05 17:31:24 +0530649 return 0;
650}
651
652static int udma_get_rchan(struct udma_chan *uc)
653{
654 struct udma_dev *ud = uc->ud;
655
656 if (uc->rchan) {
657 dev_dbg(ud->dev, "chan%d: already have rchan%d allocated\n",
658 uc->id, uc->rchan->id);
659 return 0;
660 }
661
662 uc->rchan = __udma_reserve_rchan(ud, -1);
663 if (IS_ERR(uc->rchan))
664 return PTR_ERR(uc->rchan);
665
666 pr_debug("chan%d: got rchan%d\n", uc->id, uc->rchan->id);
667
Vignesh Rffcc66e2019-02-05 17:31:24 +0530668 return 0;
669}
670
671static int udma_get_chan_pair(struct udma_chan *uc)
672{
673 struct udma_dev *ud = uc->ud;
674 int chan_id, end;
675
676 if ((uc->tchan && uc->rchan) && uc->tchan->id == uc->rchan->id) {
677 dev_info(ud->dev, "chan%d: already have %d pair allocated\n",
678 uc->id, uc->tchan->id);
679 return 0;
680 }
681
682 if (uc->tchan) {
683 dev_err(ud->dev, "chan%d: already have tchan%d allocated\n",
684 uc->id, uc->tchan->id);
685 return -EBUSY;
686 } else if (uc->rchan) {
687 dev_err(ud->dev, "chan%d: already have rchan%d allocated\n",
688 uc->id, uc->rchan->id);
689 return -EBUSY;
690 }
691
692 /* Can be optimized, but let's have it like this for now */
693 end = min(ud->tchan_cnt, ud->rchan_cnt);
694 for (chan_id = 0; chan_id < end; chan_id++) {
695 if (!test_bit(chan_id, ud->tchan_map) &&
696 !test_bit(chan_id, ud->rchan_map))
697 break;
698 }
699
700 if (chan_id == end)
701 return -ENOENT;
702
703 __set_bit(chan_id, ud->tchan_map);
704 __set_bit(chan_id, ud->rchan_map);
705 uc->tchan = &ud->tchans[chan_id];
706 uc->rchan = &ud->rchans[chan_id];
707
708 pr_debug("chan%d: got t/rchan%d pair\n", uc->id, chan_id);
709
Vignesh Rffcc66e2019-02-05 17:31:24 +0530710 return 0;
711}
712
713static int udma_get_rflow(struct udma_chan *uc, int flow_id)
714{
715 struct udma_dev *ud = uc->ud;
716
717 if (uc->rflow) {
718 dev_dbg(ud->dev, "chan%d: already have rflow%d allocated\n",
719 uc->id, uc->rflow->id);
720 return 0;
721 }
722
723 if (!uc->rchan)
724 dev_warn(ud->dev, "chan%d: does not have rchan??\n", uc->id);
725
726 uc->rflow = __udma_reserve_rflow(ud, flow_id);
727 if (IS_ERR(uc->rflow))
728 return PTR_ERR(uc->rflow);
729
730 pr_debug("chan%d: got rflow%d\n", uc->id, uc->rflow->id);
731 return 0;
732}
733
734static void udma_put_rchan(struct udma_chan *uc)
735{
736 struct udma_dev *ud = uc->ud;
737
738 if (uc->rchan) {
739 dev_dbg(ud->dev, "chan%d: put rchan%d\n", uc->id,
740 uc->rchan->id);
741 __clear_bit(uc->rchan->id, ud->rchan_map);
742 uc->rchan = NULL;
743 }
744}
745
746static void udma_put_tchan(struct udma_chan *uc)
747{
748 struct udma_dev *ud = uc->ud;
749
750 if (uc->tchan) {
751 dev_dbg(ud->dev, "chan%d: put tchan%d\n", uc->id,
752 uc->tchan->id);
753 __clear_bit(uc->tchan->id, ud->tchan_map);
754 uc->tchan = NULL;
755 }
756}
757
758static void udma_put_rflow(struct udma_chan *uc)
759{
760 struct udma_dev *ud = uc->ud;
761
762 if (uc->rflow) {
763 dev_dbg(ud->dev, "chan%d: put rflow%d\n", uc->id,
764 uc->rflow->id);
765 __clear_bit(uc->rflow->id, ud->rflow_map);
766 uc->rflow = NULL;
767 }
768}
769
770static void udma_free_tx_resources(struct udma_chan *uc)
771{
772 if (!uc->tchan)
773 return;
774
775 k3_nav_ringacc_ring_free(uc->tchan->t_ring);
776 k3_nav_ringacc_ring_free(uc->tchan->tc_ring);
777 uc->tchan->t_ring = NULL;
778 uc->tchan->tc_ring = NULL;
779
780 udma_put_tchan(uc);
781}
782
783static int udma_alloc_tx_resources(struct udma_chan *uc)
784{
785 struct k3_nav_ring_cfg ring_cfg;
786 struct udma_dev *ud = uc->ud;
787 int ret;
788
789 ret = udma_get_tchan(uc);
790 if (ret)
791 return ret;
792
Vignesh Raghavendraddcf5312020-07-06 13:26:27 +0530793 ret = k3_nav_ringacc_request_rings_pair(ud->ringacc, uc->tchan->id, -1,
794 &uc->tchan->t_ring,
795 &uc->tchan->tc_ring);
796 if (ret) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530797 ret = -EBUSY;
798 goto err_tx_ring;
799 }
800
Vignesh Rffcc66e2019-02-05 17:31:24 +0530801 memset(&ring_cfg, 0, sizeof(ring_cfg));
802 ring_cfg.size = 16;
803 ring_cfg.elm_size = K3_NAV_RINGACC_RING_ELSIZE_8;
Vignesh Raghavendrace1a3072019-12-09 10:25:37 +0530804 ring_cfg.mode = K3_NAV_RINGACC_RING_MODE_RING;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530805
806 ret = k3_nav_ringacc_ring_cfg(uc->tchan->t_ring, &ring_cfg);
807 ret |= k3_nav_ringacc_ring_cfg(uc->tchan->tc_ring, &ring_cfg);
808
809 if (ret)
810 goto err_ringcfg;
811
812 return 0;
813
814err_ringcfg:
815 k3_nav_ringacc_ring_free(uc->tchan->tc_ring);
816 uc->tchan->tc_ring = NULL;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530817 k3_nav_ringacc_ring_free(uc->tchan->t_ring);
818 uc->tchan->t_ring = NULL;
819err_tx_ring:
820 udma_put_tchan(uc);
821
822 return ret;
823}
824
825static void udma_free_rx_resources(struct udma_chan *uc)
826{
827 if (!uc->rchan)
828 return;
829
Vignesh Raghavendra7be51212020-07-06 13:26:26 +0530830 if (uc->rflow) {
831 k3_nav_ringacc_ring_free(uc->rflow->fd_ring);
832 k3_nav_ringacc_ring_free(uc->rflow->r_ring);
833 uc->rflow->fd_ring = NULL;
834 uc->rflow->r_ring = NULL;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530835
Vignesh Raghavendra7be51212020-07-06 13:26:26 +0530836 udma_put_rflow(uc);
837 }
838
Vignesh Rffcc66e2019-02-05 17:31:24 +0530839 udma_put_rchan(uc);
840}
841
842static int udma_alloc_rx_resources(struct udma_chan *uc)
843{
844 struct k3_nav_ring_cfg ring_cfg;
845 struct udma_dev *ud = uc->ud;
Vignesh Raghavendraddcf5312020-07-06 13:26:27 +0530846 struct udma_rflow *rflow;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530847 int fd_ring_id;
848 int ret;
849
850 ret = udma_get_rchan(uc);
851 if (ret)
852 return ret;
853
854 /* For MEM_TO_MEM we don't need rflow or rings */
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530855 if (uc->config.dir == DMA_MEM_TO_MEM)
Vignesh Rffcc66e2019-02-05 17:31:24 +0530856 return 0;
857
858 ret = udma_get_rflow(uc, uc->rchan->id);
859 if (ret) {
860 ret = -EBUSY;
861 goto err_rflow;
862 }
863
864 fd_ring_id = ud->tchan_cnt + ud->echan_cnt + uc->rchan->id;
865
Vignesh Raghavendraddcf5312020-07-06 13:26:27 +0530866 rflow = uc->rflow;
867 ret = k3_nav_ringacc_request_rings_pair(ud->ringacc, fd_ring_id, -1,
868 &rflow->fd_ring, &rflow->r_ring);
869 if (ret) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530870 ret = -EBUSY;
871 goto err_rx_ring;
872 }
873
Vignesh Rffcc66e2019-02-05 17:31:24 +0530874 memset(&ring_cfg, 0, sizeof(ring_cfg));
875 ring_cfg.size = 16;
876 ring_cfg.elm_size = K3_NAV_RINGACC_RING_ELSIZE_8;
Vignesh Raghavendrace1a3072019-12-09 10:25:37 +0530877 ring_cfg.mode = K3_NAV_RINGACC_RING_MODE_RING;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530878
Vignesh Raghavendraddcf5312020-07-06 13:26:27 +0530879 ret = k3_nav_ringacc_ring_cfg(rflow->fd_ring, &ring_cfg);
880 ret |= k3_nav_ringacc_ring_cfg(rflow->r_ring, &ring_cfg);
Vignesh Rffcc66e2019-02-05 17:31:24 +0530881 if (ret)
882 goto err_ringcfg;
883
884 return 0;
885
886err_ringcfg:
Vignesh Raghavendraddcf5312020-07-06 13:26:27 +0530887 k3_nav_ringacc_ring_free(rflow->r_ring);
888 rflow->r_ring = NULL;
889 k3_nav_ringacc_ring_free(rflow->fd_ring);
890 rflow->fd_ring = NULL;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530891err_rx_ring:
892 udma_put_rflow(uc);
893err_rflow:
894 udma_put_rchan(uc);
895
896 return ret;
897}
898
899static int udma_alloc_tchan_sci_req(struct udma_chan *uc)
900{
901 struct udma_dev *ud = uc->ud;
902 int tc_ring = k3_nav_ringacc_get_ring_id(uc->tchan->tc_ring);
903 struct ti_sci_msg_rm_udmap_tx_ch_cfg req;
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530904 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530905 u32 mode;
906 int ret;
907
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530908 if (uc->config.pkt_mode)
Vignesh Rffcc66e2019-02-05 17:31:24 +0530909 mode = TI_SCI_RM_UDMAP_CHAN_TYPE_PKT_PBRR;
910 else
911 mode = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_BCOPY_PBRR;
912
913 req.valid_params = TI_SCI_MSG_VALUE_RM_UDMAP_CH_CHAN_TYPE_VALID |
914 TI_SCI_MSG_VALUE_RM_UDMAP_CH_FETCH_SIZE_VALID |
915 TI_SCI_MSG_VALUE_RM_UDMAP_CH_CQ_QNUM_VALID;
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530916 req.nav_id = tisci_rm->tisci_dev_id;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530917 req.index = uc->tchan->id;
918 req.tx_chan_type = mode;
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530919 if (uc->config.dir == DMA_MEM_TO_MEM)
Vignesh Rffcc66e2019-02-05 17:31:24 +0530920 req.tx_fetch_size = sizeof(struct cppi5_desc_hdr_t) >> 2;
921 else
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530922 req.tx_fetch_size = cppi5_hdesc_calc_size(uc->config.needs_epib,
923 uc->config.psd_size,
Vignesh Rffcc66e2019-02-05 17:31:24 +0530924 0) >> 2;
925 req.txcq_qnum = tc_ring;
926
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530927 ret = tisci_rm->tisci_udmap_ops->tx_ch_cfg(tisci_rm->tisci, &req);
Vignesh Rffcc66e2019-02-05 17:31:24 +0530928 if (ret)
929 dev_err(ud->dev, "tisci tx alloc failed %d\n", ret);
930
931 return ret;
932}
933
934static int udma_alloc_rchan_sci_req(struct udma_chan *uc)
935{
936 struct udma_dev *ud = uc->ud;
Vignesh Raghavendra7be51212020-07-06 13:26:26 +0530937 int fd_ring = k3_nav_ringacc_get_ring_id(uc->rflow->fd_ring);
938 int rx_ring = k3_nav_ringacc_get_ring_id(uc->rflow->r_ring);
Vignesh Rffcc66e2019-02-05 17:31:24 +0530939 int tc_ring = k3_nav_ringacc_get_ring_id(uc->tchan->tc_ring);
940 struct ti_sci_msg_rm_udmap_rx_ch_cfg req = { 0 };
941 struct ti_sci_msg_rm_udmap_flow_cfg flow_req = { 0 };
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530942 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530943 u32 mode;
944 int ret;
945
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530946 if (uc->config.pkt_mode)
Vignesh Rffcc66e2019-02-05 17:31:24 +0530947 mode = TI_SCI_RM_UDMAP_CHAN_TYPE_PKT_PBRR;
948 else
949 mode = TI_SCI_RM_UDMAP_CHAN_TYPE_3RDP_BCOPY_PBRR;
950
951 req.valid_params = TI_SCI_MSG_VALUE_RM_UDMAP_CH_FETCH_SIZE_VALID |
952 TI_SCI_MSG_VALUE_RM_UDMAP_CH_CQ_QNUM_VALID |
Lokesh Vutla57b27122020-02-28 17:56:20 +0530953 TI_SCI_MSG_VALUE_RM_UDMAP_CH_CHAN_TYPE_VALID |
954 TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_FLOWID_START_VALID |
955 TI_SCI_MSG_VALUE_RM_UDMAP_CH_RX_FLOWID_CNT_VALID;
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530956 req.nav_id = tisci_rm->tisci_dev_id;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530957 req.index = uc->rchan->id;
958 req.rx_chan_type = mode;
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530959 if (uc->config.dir == DMA_MEM_TO_MEM) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530960 req.rx_fetch_size = sizeof(struct cppi5_desc_hdr_t) >> 2;
961 req.rxcq_qnum = tc_ring;
962 } else {
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530963 req.rx_fetch_size = cppi5_hdesc_calc_size(uc->config.needs_epib,
964 uc->config.psd_size,
Vignesh Rffcc66e2019-02-05 17:31:24 +0530965 0) >> 2;
966 req.rxcq_qnum = rx_ring;
967 }
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530968 if (uc->rflow->id != uc->rchan->id && uc->config.dir != DMA_MEM_TO_MEM) {
Vignesh Rffcc66e2019-02-05 17:31:24 +0530969 req.flowid_start = uc->rflow->id;
970 req.flowid_cnt = 1;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530971 }
972
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530973 ret = tisci_rm->tisci_udmap_ops->rx_ch_cfg(tisci_rm->tisci, &req);
Vignesh Rffcc66e2019-02-05 17:31:24 +0530974 if (ret) {
975 dev_err(ud->dev, "tisci rx %u cfg failed %d\n",
976 uc->rchan->id, ret);
977 return ret;
978 }
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +0530979 if (uc->config.dir == DMA_MEM_TO_MEM)
Vignesh Rffcc66e2019-02-05 17:31:24 +0530980 return ret;
981
982 flow_req.valid_params =
983 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_EINFO_PRESENT_VALID |
984 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_PSINFO_PRESENT_VALID |
985 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_ERROR_HANDLING_VALID |
986 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DESC_TYPE_VALID |
987 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_QNUM_VALID |
988 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_SRC_TAG_HI_SEL_VALID |
989 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_SRC_TAG_LO_SEL_VALID |
990 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_TAG_HI_SEL_VALID |
991 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_DEST_TAG_LO_SEL_VALID |
992 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ0_SZ0_QNUM_VALID |
993 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ1_QNUM_VALID |
994 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ2_QNUM_VALID |
995 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_FDQ3_QNUM_VALID |
996 TI_SCI_MSG_VALUE_RM_UDMAP_FLOW_PS_LOCATION_VALID;
997
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +0530998 flow_req.nav_id = tisci_rm->tisci_dev_id;
Vignesh Rffcc66e2019-02-05 17:31:24 +0530999 flow_req.flow_index = uc->rflow->id;
1000
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301001 if (uc->config.needs_epib)
Vignesh Rffcc66e2019-02-05 17:31:24 +05301002 flow_req.rx_einfo_present = 1;
1003 else
1004 flow_req.rx_einfo_present = 0;
1005
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301006 if (uc->config.psd_size)
Vignesh Rffcc66e2019-02-05 17:31:24 +05301007 flow_req.rx_psinfo_present = 1;
1008 else
1009 flow_req.rx_psinfo_present = 0;
1010
1011 flow_req.rx_error_handling = 0;
1012 flow_req.rx_desc_type = 0;
1013 flow_req.rx_dest_qnum = rx_ring;
1014 flow_req.rx_src_tag_hi_sel = 2;
1015 flow_req.rx_src_tag_lo_sel = 4;
1016 flow_req.rx_dest_tag_hi_sel = 5;
1017 flow_req.rx_dest_tag_lo_sel = 4;
1018 flow_req.rx_fdq0_sz0_qnum = fd_ring;
1019 flow_req.rx_fdq1_qnum = fd_ring;
1020 flow_req.rx_fdq2_qnum = fd_ring;
1021 flow_req.rx_fdq3_qnum = fd_ring;
1022 flow_req.rx_ps_location = 0;
1023
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301024 ret = tisci_rm->tisci_udmap_ops->rx_flow_cfg(tisci_rm->tisci,
1025 &flow_req);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301026 if (ret)
1027 dev_err(ud->dev, "tisci rx %u flow %u cfg failed %d\n",
1028 uc->rchan->id, uc->rflow->id, ret);
1029
1030 return ret;
1031}
1032
1033static int udma_alloc_chan_resources(struct udma_chan *uc)
1034{
1035 struct udma_dev *ud = uc->ud;
1036 int ret;
1037
1038 pr_debug("%s: chan:%d as %s\n",
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301039 __func__, uc->id, udma_get_dir_text(uc->config.dir));
Vignesh Rffcc66e2019-02-05 17:31:24 +05301040
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301041 switch (uc->config.dir) {
Vignesh Rffcc66e2019-02-05 17:31:24 +05301042 case DMA_MEM_TO_MEM:
1043 /* Non synchronized - mem to mem type of transfer */
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301044 uc->config.pkt_mode = false;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301045 ret = udma_get_chan_pair(uc);
1046 if (ret)
1047 return ret;
1048
1049 ret = udma_alloc_tx_resources(uc);
1050 if (ret)
1051 goto err_free_res;
1052
1053 ret = udma_alloc_rx_resources(uc);
1054 if (ret)
1055 goto err_free_res;
1056
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301057 uc->config.src_thread = ud->psil_base + uc->tchan->id;
1058 uc->config.dst_thread = (ud->psil_base + uc->rchan->id) | 0x8000;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301059 break;
1060 case DMA_MEM_TO_DEV:
1061 /* Slave transfer synchronized - mem to dev (TX) trasnfer */
1062 ret = udma_alloc_tx_resources(uc);
1063 if (ret)
1064 goto err_free_res;
1065
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301066 uc->config.src_thread = ud->psil_base + uc->tchan->id;
1067 uc->config.dst_thread = uc->config.remote_thread_id;
1068 uc->config.dst_thread |= 0x8000;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301069
1070 break;
1071 case DMA_DEV_TO_MEM:
1072 /* Slave transfer synchronized - dev to mem (RX) trasnfer */
1073 ret = udma_alloc_rx_resources(uc);
1074 if (ret)
1075 goto err_free_res;
1076
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301077 uc->config.src_thread = uc->config.remote_thread_id;
1078 uc->config.dst_thread = (ud->psil_base + uc->rchan->id) | 0x8000;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301079
1080 break;
1081 default:
1082 /* Can not happen */
1083 pr_debug("%s: chan:%d invalid direction (%u)\n",
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301084 __func__, uc->id, uc->config.dir);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301085 return -EINVAL;
1086 }
1087
1088 /* We have channel indexes and rings */
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301089 if (uc->config.dir == DMA_MEM_TO_MEM) {
Vignesh Rffcc66e2019-02-05 17:31:24 +05301090 ret = udma_alloc_tchan_sci_req(uc);
1091 if (ret)
1092 goto err_free_res;
1093
1094 ret = udma_alloc_rchan_sci_req(uc);
1095 if (ret)
1096 goto err_free_res;
1097 } else {
1098 /* Slave transfer */
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301099 if (uc->config.dir == DMA_MEM_TO_DEV) {
Vignesh Rffcc66e2019-02-05 17:31:24 +05301100 ret = udma_alloc_tchan_sci_req(uc);
1101 if (ret)
1102 goto err_free_res;
1103 } else {
1104 ret = udma_alloc_rchan_sci_req(uc);
1105 if (ret)
1106 goto err_free_res;
1107 }
1108 }
1109
Peter Ujfalusi54877722019-04-25 12:08:15 +05301110 if (udma_is_chan_running(uc)) {
1111 dev_warn(ud->dev, "chan%d: is running!\n", uc->id);
1112 udma_stop(uc);
1113 if (udma_is_chan_running(uc)) {
1114 dev_err(ud->dev, "chan%d: won't stop!\n", uc->id);
1115 goto err_free_res;
1116 }
1117 }
1118
Vignesh Rffcc66e2019-02-05 17:31:24 +05301119 /* PSI-L pairing */
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301120 ret = udma_navss_psil_pair(ud, uc->config.src_thread, uc->config.dst_thread);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301121 if (ret) {
1122 dev_err(ud->dev, "k3_nav_psil_request_link fail\n");
1123 goto err_free_res;
1124 }
1125
1126 return 0;
1127
1128err_free_res:
1129 udma_free_tx_resources(uc);
1130 udma_free_rx_resources(uc);
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301131 uc->config.remote_thread_id = -1;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301132 return ret;
1133}
1134
1135static void udma_free_chan_resources(struct udma_chan *uc)
1136{
1137 /* Some configuration to UDMA-P channel: disable, reset, whatever */
1138
1139 /* Release PSI-L pairing */
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301140 udma_navss_psil_unpair(uc->ud, uc->config.src_thread, uc->config.dst_thread);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301141
1142 /* Reset the rings for a new start */
1143 udma_reset_rings(uc);
1144 udma_free_tx_resources(uc);
1145 udma_free_rx_resources(uc);
1146
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301147 uc->config.remote_thread_id = -1;
1148 uc->config.dir = DMA_MEM_TO_MEM;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301149}
1150
1151static int udma_get_mmrs(struct udevice *dev)
1152{
1153 struct udma_dev *ud = dev_get_priv(dev);
1154 int i;
1155
1156 for (i = 0; i < MMR_LAST; i++) {
1157 ud->mmrs[i] = (uint32_t *)devfdt_get_addr_name(dev,
1158 mmr_names[i]);
1159 if (!ud->mmrs[i])
1160 return -EINVAL;
1161 }
1162
1163 return 0;
1164}
1165
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301166static int udma_setup_resources(struct udma_dev *ud)
1167{
1168 struct udevice *dev = ud->dev;
1169 int ch_count, i;
1170 u32 cap2, cap3;
1171 struct ti_sci_resource_desc *rm_desc;
1172 struct ti_sci_resource *rm_res;
1173 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1174 static const char * const range_names[] = { "ti,sci-rm-range-tchan",
1175 "ti,sci-rm-range-rchan",
1176 "ti,sci-rm-range-rflow" };
Vignesh Rffcc66e2019-02-05 17:31:24 +05301177
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301178 cap2 = udma_read(ud->mmrs[MMR_GCFG], 0x28);
1179 cap3 = udma_read(ud->mmrs[MMR_GCFG], 0x2c);
1180
1181 ud->rflow_cnt = cap3 & 0x3fff;
1182 ud->tchan_cnt = cap2 & 0x1ff;
1183 ud->echan_cnt = (cap2 >> 9) & 0x1ff;
1184 ud->rchan_cnt = (cap2 >> 18) & 0x1ff;
1185 ch_count = ud->tchan_cnt + ud->rchan_cnt;
1186
1187 ud->tchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->tchan_cnt),
1188 sizeof(unsigned long), GFP_KERNEL);
1189 ud->tchans = devm_kcalloc(dev, ud->tchan_cnt, sizeof(*ud->tchans),
1190 GFP_KERNEL);
1191 ud->rchan_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->rchan_cnt),
1192 sizeof(unsigned long), GFP_KERNEL);
1193 ud->rchans = devm_kcalloc(dev, ud->rchan_cnt, sizeof(*ud->rchans),
1194 GFP_KERNEL);
1195 ud->rflow_map = devm_kmalloc_array(dev, BITS_TO_LONGS(ud->rflow_cnt),
1196 sizeof(unsigned long), GFP_KERNEL);
1197 ud->rflow_map_reserved = devm_kcalloc(dev, BITS_TO_LONGS(ud->rflow_cnt),
1198 sizeof(unsigned long),
1199 GFP_KERNEL);
1200 ud->rflows = devm_kcalloc(dev, ud->rflow_cnt, sizeof(*ud->rflows),
1201 GFP_KERNEL);
1202
1203 if (!ud->tchan_map || !ud->rchan_map || !ud->rflow_map ||
1204 !ud->rflow_map_reserved || !ud->tchans || !ud->rchans ||
1205 !ud->rflows)
1206 return -ENOMEM;
1207
1208 /*
1209 * RX flows with the same Ids as RX channels are reserved to be used
1210 * as default flows if remote HW can't generate flow_ids. Those
1211 * RX flows can be requested only explicitly by id.
1212 */
1213 bitmap_set(ud->rflow_map_reserved, 0, ud->rchan_cnt);
1214
1215 /* Get resource ranges from tisci */
1216 for (i = 0; i < RM_RANGE_LAST; i++)
1217 tisci_rm->rm_ranges[i] =
1218 devm_ti_sci_get_of_resource(tisci_rm->tisci, dev,
1219 tisci_rm->tisci_dev_id,
1220 (char *)range_names[i]);
1221
1222 /* tchan ranges */
1223 rm_res = tisci_rm->rm_ranges[RM_RANGE_TCHAN];
1224 if (IS_ERR(rm_res)) {
1225 bitmap_zero(ud->tchan_map, ud->tchan_cnt);
1226 } else {
1227 bitmap_fill(ud->tchan_map, ud->tchan_cnt);
1228 for (i = 0; i < rm_res->sets; i++) {
1229 rm_desc = &rm_res->desc[i];
1230 bitmap_clear(ud->tchan_map, rm_desc->start,
1231 rm_desc->num);
1232 }
1233 }
1234
1235 /* rchan and matching default flow ranges */
1236 rm_res = tisci_rm->rm_ranges[RM_RANGE_RCHAN];
1237 if (IS_ERR(rm_res)) {
1238 bitmap_zero(ud->rchan_map, ud->rchan_cnt);
1239 bitmap_zero(ud->rflow_map, ud->rchan_cnt);
1240 } else {
1241 bitmap_fill(ud->rchan_map, ud->rchan_cnt);
1242 bitmap_fill(ud->rflow_map, ud->rchan_cnt);
1243 for (i = 0; i < rm_res->sets; i++) {
1244 rm_desc = &rm_res->desc[i];
1245 bitmap_clear(ud->rchan_map, rm_desc->start,
1246 rm_desc->num);
1247 bitmap_clear(ud->rflow_map, rm_desc->start,
1248 rm_desc->num);
1249 }
1250 }
1251
1252 /* GP rflow ranges */
1253 rm_res = tisci_rm->rm_ranges[RM_RANGE_RFLOW];
1254 if (IS_ERR(rm_res)) {
1255 bitmap_clear(ud->rflow_map, ud->rchan_cnt,
1256 ud->rflow_cnt - ud->rchan_cnt);
1257 } else {
1258 bitmap_set(ud->rflow_map, ud->rchan_cnt,
1259 ud->rflow_cnt - ud->rchan_cnt);
1260 for (i = 0; i < rm_res->sets; i++) {
1261 rm_desc = &rm_res->desc[i];
1262 bitmap_clear(ud->rflow_map, rm_desc->start,
1263 rm_desc->num);
1264 }
1265 }
1266
1267 ch_count -= bitmap_weight(ud->tchan_map, ud->tchan_cnt);
1268 ch_count -= bitmap_weight(ud->rchan_map, ud->rchan_cnt);
1269 if (!ch_count)
1270 return -ENODEV;
1271
1272 ud->channels = devm_kcalloc(dev, ch_count, sizeof(*ud->channels),
1273 GFP_KERNEL);
1274 if (!ud->channels)
1275 return -ENOMEM;
1276
1277 dev_info(dev,
1278 "Channels: %d (tchan: %u, echan: %u, rchan: %u, rflow: %u)\n",
1279 ch_count, ud->tchan_cnt, ud->echan_cnt, ud->rchan_cnt,
1280 ud->rflow_cnt);
1281
1282 return ch_count;
1283}
Vignesh Rffcc66e2019-02-05 17:31:24 +05301284static int udma_probe(struct udevice *dev)
1285{
1286 struct dma_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1287 struct udma_dev *ud = dev_get_priv(dev);
1288 int i, ret;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301289 struct udevice *tmp;
1290 struct udevice *tisci_dev = NULL;
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301291 struct udma_tisci_rm *tisci_rm = &ud->tisci_rm;
1292 ofnode navss_ofnode = ofnode_get_parent(dev_ofnode(dev));
1293
Vignesh Rffcc66e2019-02-05 17:31:24 +05301294
1295 ret = udma_get_mmrs(dev);
1296 if (ret)
1297 return ret;
1298
1299 ret = uclass_get_device_by_phandle(UCLASS_MISC, dev,
1300 "ti,ringacc", &tmp);
1301 ud->ringacc = dev_get_priv(tmp);
1302 if (IS_ERR(ud->ringacc))
1303 return PTR_ERR(ud->ringacc);
1304
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +05301305 ud->match_data = (void *)dev_get_driver_data(dev);
1306 ud->psil_base = ud->match_data->psil_base;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301307
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301308 ret = uclass_get_device_by_phandle(UCLASS_FIRMWARE, dev,
1309 "ti,sci", &tisci_dev);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301310 if (ret) {
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301311 debug("Failed to get TISCI phandle (%d)\n", ret);
1312 tisci_rm->tisci = NULL;
1313 return -EINVAL;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301314 }
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301315 tisci_rm->tisci = (struct ti_sci_handle *)
1316 (ti_sci_get_handle_from_sysfw(tisci_dev));
Vignesh Rffcc66e2019-02-05 17:31:24 +05301317
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301318 tisci_rm->tisci_dev_id = -1;
1319 ret = dev_read_u32(dev, "ti,sci-dev-id", &tisci_rm->tisci_dev_id);
1320 if (ret) {
1321 dev_err(dev, "ti,sci-dev-id read failure %d\n", ret);
1322 return ret;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301323 }
1324
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301325 tisci_rm->tisci_navss_dev_id = -1;
1326 ret = ofnode_read_u32(navss_ofnode, "ti,sci-dev-id",
1327 &tisci_rm->tisci_navss_dev_id);
1328 if (ret) {
1329 dev_err(dev, "navss sci-dev-id read failure %d\n", ret);
1330 return ret;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301331 }
1332
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301333 tisci_rm->tisci_udmap_ops = &tisci_rm->tisci->ops.rm_udmap_ops;
1334 tisci_rm->tisci_psil_ops = &tisci_rm->tisci->ops.rm_psil_ops;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301335
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301336 ud->dev = dev;
1337 ud->ch_count = udma_setup_resources(ud);
1338 if (ud->ch_count <= 0)
1339 return ud->ch_count;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301340
1341 dev_info(dev,
1342 "Number of channels: %u (tchan: %u, echan: %u, rchan: %u dev-id %u)\n",
1343 ud->ch_count, ud->tchan_cnt, ud->echan_cnt, ud->rchan_cnt,
Vignesh Raghavendraa8837cf2019-12-09 10:25:32 +05301344 tisci_rm->tisci_dev_id);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301345 dev_info(dev, "Number of rflows: %u\n", ud->rflow_cnt);
1346
Vignesh Rffcc66e2019-02-05 17:31:24 +05301347 for (i = 0; i < ud->tchan_cnt; i++) {
1348 struct udma_tchan *tchan = &ud->tchans[i];
1349
1350 tchan->id = i;
1351 tchan->reg_rt = ud->mmrs[MMR_TCHANRT] + UDMA_CH_1000(i);
1352 }
1353
1354 for (i = 0; i < ud->rchan_cnt; i++) {
1355 struct udma_rchan *rchan = &ud->rchans[i];
1356
1357 rchan->id = i;
1358 rchan->reg_rt = ud->mmrs[MMR_RCHANRT] + UDMA_CH_1000(i);
1359 }
1360
1361 for (i = 0; i < ud->rflow_cnt; i++) {
1362 struct udma_rflow *rflow = &ud->rflows[i];
1363
1364 rflow->id = i;
1365 }
1366
1367 for (i = 0; i < ud->ch_count; i++) {
1368 struct udma_chan *uc = &ud->channels[i];
1369
1370 uc->ud = ud;
1371 uc->id = i;
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301372 uc->config.remote_thread_id = -1;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301373 uc->tchan = NULL;
1374 uc->rchan = NULL;
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301375 uc->config.dir = DMA_MEM_TO_MEM;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301376 sprintf(uc->name, "UDMA chan%d\n", i);
1377 if (!i)
1378 uc->in_use = true;
1379 }
1380
1381 pr_debug("UDMA(rev: 0x%08x) CAP0-3: 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
1382 udma_read(ud->mmrs[MMR_GCFG], 0),
1383 udma_read(ud->mmrs[MMR_GCFG], 0x20),
1384 udma_read(ud->mmrs[MMR_GCFG], 0x24),
1385 udma_read(ud->mmrs[MMR_GCFG], 0x28),
1386 udma_read(ud->mmrs[MMR_GCFG], 0x2c));
1387
1388 uc_priv->supported = DMA_SUPPORTS_MEM_TO_MEM | DMA_SUPPORTS_MEM_TO_DEV;
1389
1390 return ret;
1391}
1392
Vignesh Raghavendrab0ab0082019-12-09 10:25:38 +05301393static int udma_push_to_ring(struct k3_nav_ring *ring, void *elem)
1394{
1395 u64 addr = 0;
1396
1397 memcpy(&addr, &elem, sizeof(elem));
1398 return k3_nav_ringacc_ring_push(ring, &addr);
1399}
1400
Vignesh Rffcc66e2019-02-05 17:31:24 +05301401static int *udma_prep_dma_memcpy(struct udma_chan *uc, dma_addr_t dest,
1402 dma_addr_t src, size_t len)
1403{
1404 u32 tc_ring_id = k3_nav_ringacc_get_ring_id(uc->tchan->tc_ring);
1405 struct cppi5_tr_type15_t *tr_req;
1406 int num_tr;
1407 size_t tr_size = sizeof(struct cppi5_tr_type15_t);
1408 u16 tr0_cnt0, tr0_cnt1, tr1_cnt0;
1409 unsigned long dummy;
1410 void *tr_desc;
1411 size_t desc_size;
1412
1413 if (len < SZ_64K) {
1414 num_tr = 1;
1415 tr0_cnt0 = len;
1416 tr0_cnt1 = 1;
1417 } else {
1418 unsigned long align_to = __ffs(src | dest);
1419
1420 if (align_to > 3)
1421 align_to = 3;
1422 /*
1423 * Keep simple: tr0: SZ_64K-alignment blocks,
1424 * tr1: the remaining
1425 */
1426 num_tr = 2;
1427 tr0_cnt0 = (SZ_64K - BIT(align_to));
1428 if (len / tr0_cnt0 >= SZ_64K) {
1429 dev_err(uc->ud->dev, "size %zu is not supported\n",
1430 len);
1431 return NULL;
1432 }
1433
1434 tr0_cnt1 = len / tr0_cnt0;
1435 tr1_cnt0 = len % tr0_cnt0;
1436 }
1437
1438 desc_size = cppi5_trdesc_calc_size(num_tr, tr_size);
1439 tr_desc = dma_alloc_coherent(desc_size, &dummy);
1440 if (!tr_desc)
1441 return NULL;
1442 memset(tr_desc, 0, desc_size);
1443
1444 cppi5_trdesc_init(tr_desc, num_tr, tr_size, 0, 0);
1445 cppi5_desc_set_pktids(tr_desc, uc->id, 0x3fff);
1446 cppi5_desc_set_retpolicy(tr_desc, 0, tc_ring_id);
1447
1448 tr_req = tr_desc + tr_size;
1449
1450 cppi5_tr_init(&tr_req[0].flags, CPPI5_TR_TYPE15, false, true,
1451 CPPI5_TR_EVENT_SIZE_COMPLETION, 1);
1452 cppi5_tr_csf_set(&tr_req[0].flags, CPPI5_TR_CSF_SUPR_EVT);
1453
1454 tr_req[0].addr = src;
1455 tr_req[0].icnt0 = tr0_cnt0;
1456 tr_req[0].icnt1 = tr0_cnt1;
1457 tr_req[0].icnt2 = 1;
1458 tr_req[0].icnt3 = 1;
1459 tr_req[0].dim1 = tr0_cnt0;
1460
1461 tr_req[0].daddr = dest;
1462 tr_req[0].dicnt0 = tr0_cnt0;
1463 tr_req[0].dicnt1 = tr0_cnt1;
1464 tr_req[0].dicnt2 = 1;
1465 tr_req[0].dicnt3 = 1;
1466 tr_req[0].ddim1 = tr0_cnt0;
1467
1468 if (num_tr == 2) {
1469 cppi5_tr_init(&tr_req[1].flags, CPPI5_TR_TYPE15, false, true,
1470 CPPI5_TR_EVENT_SIZE_COMPLETION, 0);
1471 cppi5_tr_csf_set(&tr_req[1].flags, CPPI5_TR_CSF_SUPR_EVT);
1472
1473 tr_req[1].addr = src + tr0_cnt1 * tr0_cnt0;
1474 tr_req[1].icnt0 = tr1_cnt0;
1475 tr_req[1].icnt1 = 1;
1476 tr_req[1].icnt2 = 1;
1477 tr_req[1].icnt3 = 1;
1478
1479 tr_req[1].daddr = dest + tr0_cnt1 * tr0_cnt0;
1480 tr_req[1].dicnt0 = tr1_cnt0;
1481 tr_req[1].dicnt1 = 1;
1482 tr_req[1].dicnt2 = 1;
1483 tr_req[1].dicnt3 = 1;
1484 }
1485
1486 cppi5_tr_csf_set(&tr_req[num_tr - 1].flags, CPPI5_TR_CSF_EOP);
1487
Vignesh Raghavendraf03cb5c2019-12-09 10:25:39 +05301488 flush_dcache_range((unsigned long)tr_desc,
1489 ALIGN((unsigned long)tr_desc + desc_size,
Vignesh Raghavendrac0b94902019-12-09 10:25:35 +05301490 ARCH_DMA_MINALIGN));
Vignesh Rffcc66e2019-02-05 17:31:24 +05301491
Vignesh Raghavendrab0ab0082019-12-09 10:25:38 +05301492 udma_push_to_ring(uc->tchan->t_ring, tr_desc);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301493
1494 return 0;
1495}
1496
1497static int udma_transfer(struct udevice *dev, int direction,
1498 void *dst, void *src, size_t len)
1499{
1500 struct udma_dev *ud = dev_get_priv(dev);
1501 /* Channel0 is reserved for memcpy */
1502 struct udma_chan *uc = &ud->channels[0];
1503 dma_addr_t paddr = 0;
1504 int ret;
1505
1506 ret = udma_alloc_chan_resources(uc);
1507 if (ret)
1508 return ret;
1509
1510 udma_prep_dma_memcpy(uc, (dma_addr_t)dst, (dma_addr_t)src, len);
1511 udma_start(uc);
1512 udma_poll_completion(uc, &paddr);
1513 udma_stop(uc);
1514
1515 udma_free_chan_resources(uc);
1516 return 0;
1517}
1518
1519static int udma_request(struct dma *dma)
1520{
1521 struct udma_dev *ud = dev_get_priv(dma->dev);
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301522 struct udma_chan_config *ucc;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301523 struct udma_chan *uc;
1524 unsigned long dummy;
1525 int ret;
1526
1527 if (dma->id >= (ud->rchan_cnt + ud->tchan_cnt)) {
1528 dev_err(dma->dev, "invalid dma ch_id %lu\n", dma->id);
1529 return -EINVAL;
1530 }
1531
1532 uc = &ud->channels[dma->id];
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301533 ucc = &uc->config;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301534 ret = udma_alloc_chan_resources(uc);
1535 if (ret) {
1536 dev_err(dma->dev, "alloc dma res failed %d\n", ret);
1537 return -EINVAL;
1538 }
1539
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301540 if (uc->config.dir == DMA_MEM_TO_DEV) {
1541 uc->desc_tx = dma_alloc_coherent(ucc->hdesc_size, &dummy);
1542 memset(uc->desc_tx, 0, ucc->hdesc_size);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301543 } else {
1544 uc->desc_rx = dma_alloc_coherent(
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301545 ucc->hdesc_size * UDMA_RX_DESC_NUM, &dummy);
1546 memset(uc->desc_rx, 0, ucc->hdesc_size * UDMA_RX_DESC_NUM);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301547 }
1548
1549 uc->in_use = true;
1550 uc->desc_rx_cur = 0;
1551 uc->num_rx_bufs = 0;
1552
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301553 if (uc->config.dir == DMA_DEV_TO_MEM) {
Vignesh Raghavendra5e6d9cc2019-12-04 22:17:21 +05301554 uc->cfg_data.flow_id_base = uc->rflow->id;
1555 uc->cfg_data.flow_id_cnt = 1;
1556 }
1557
Vignesh Rffcc66e2019-02-05 17:31:24 +05301558 return 0;
1559}
1560
Simon Glassaae95882020-02-03 07:35:55 -07001561static int udma_rfree(struct dma *dma)
Vignesh Rffcc66e2019-02-05 17:31:24 +05301562{
1563 struct udma_dev *ud = dev_get_priv(dma->dev);
1564 struct udma_chan *uc;
1565
1566 if (dma->id >= (ud->rchan_cnt + ud->tchan_cnt)) {
1567 dev_err(dma->dev, "invalid dma ch_id %lu\n", dma->id);
1568 return -EINVAL;
1569 }
1570 uc = &ud->channels[dma->id];
1571
1572 if (udma_is_chan_running(uc))
1573 udma_stop(uc);
1574 udma_free_chan_resources(uc);
1575
1576 uc->in_use = false;
1577
1578 return 0;
1579}
1580
1581static int udma_enable(struct dma *dma)
1582{
1583 struct udma_dev *ud = dev_get_priv(dma->dev);
1584 struct udma_chan *uc;
1585 int ret;
1586
1587 if (dma->id >= (ud->rchan_cnt + ud->tchan_cnt)) {
1588 dev_err(dma->dev, "invalid dma ch_id %lu\n", dma->id);
1589 return -EINVAL;
1590 }
1591 uc = &ud->channels[dma->id];
1592
1593 ret = udma_start(uc);
1594
1595 return ret;
1596}
1597
1598static int udma_disable(struct dma *dma)
1599{
1600 struct udma_dev *ud = dev_get_priv(dma->dev);
1601 struct udma_chan *uc;
1602 int ret = 0;
1603
1604 if (dma->id >= (ud->rchan_cnt + ud->tchan_cnt)) {
1605 dev_err(dma->dev, "invalid dma ch_id %lu\n", dma->id);
1606 return -EINVAL;
1607 }
1608 uc = &ud->channels[dma->id];
1609
1610 if (udma_is_chan_running(uc))
1611 ret = udma_stop(uc);
1612 else
1613 dev_err(dma->dev, "%s not running\n", __func__);
1614
1615 return ret;
1616}
1617
1618static int udma_send(struct dma *dma, void *src, size_t len, void *metadata)
1619{
1620 struct udma_dev *ud = dev_get_priv(dma->dev);
1621 struct cppi5_host_desc_t *desc_tx;
1622 dma_addr_t dma_src = (dma_addr_t)src;
1623 struct ti_udma_drv_packet_data packet_data = { 0 };
1624 dma_addr_t paddr;
1625 struct udma_chan *uc;
1626 u32 tc_ring_id;
1627 int ret;
1628
Keerthya3f25b92019-04-24 16:33:54 +05301629 if (metadata)
Vignesh Rffcc66e2019-02-05 17:31:24 +05301630 packet_data = *((struct ti_udma_drv_packet_data *)metadata);
1631
1632 if (dma->id >= (ud->rchan_cnt + ud->tchan_cnt)) {
1633 dev_err(dma->dev, "invalid dma ch_id %lu\n", dma->id);
1634 return -EINVAL;
1635 }
1636 uc = &ud->channels[dma->id];
1637
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301638 if (uc->config.dir != DMA_MEM_TO_DEV)
Vignesh Rffcc66e2019-02-05 17:31:24 +05301639 return -EINVAL;
1640
1641 tc_ring_id = k3_nav_ringacc_get_ring_id(uc->tchan->tc_ring);
1642
1643 desc_tx = uc->desc_tx;
1644
1645 cppi5_hdesc_reset_hbdesc(desc_tx);
1646
1647 cppi5_hdesc_init(desc_tx,
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301648 uc->config.needs_epib ? CPPI5_INFO0_HDESC_EPIB_PRESENT : 0,
1649 uc->config.psd_size);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301650 cppi5_hdesc_set_pktlen(desc_tx, len);
1651 cppi5_hdesc_attach_buf(desc_tx, dma_src, len, dma_src, len);
1652 cppi5_desc_set_pktids(&desc_tx->hdr, uc->id, 0x3fff);
1653 cppi5_desc_set_retpolicy(&desc_tx->hdr, 0, tc_ring_id);
1654 /* pass below information from caller */
1655 cppi5_hdesc_set_pkttype(desc_tx, packet_data.pkt_type);
1656 cppi5_desc_set_tags_ids(&desc_tx->hdr, 0, packet_data.dest_tag);
1657
Vignesh Raghavendraf03cb5c2019-12-09 10:25:39 +05301658 flush_dcache_range((unsigned long)dma_src,
1659 ALIGN((unsigned long)dma_src + len,
Vignesh Raghavendrac0b94902019-12-09 10:25:35 +05301660 ARCH_DMA_MINALIGN));
Vignesh Raghavendraf03cb5c2019-12-09 10:25:39 +05301661 flush_dcache_range((unsigned long)desc_tx,
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301662 ALIGN((unsigned long)desc_tx + uc->config.hdesc_size,
Vignesh Raghavendrac0b94902019-12-09 10:25:35 +05301663 ARCH_DMA_MINALIGN));
Vignesh Rffcc66e2019-02-05 17:31:24 +05301664
Vignesh Raghavendrab0ab0082019-12-09 10:25:38 +05301665 ret = udma_push_to_ring(uc->tchan->t_ring, uc->desc_tx);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301666 if (ret) {
1667 dev_err(dma->dev, "TX dma push fail ch_id %lu %d\n",
1668 dma->id, ret);
1669 return ret;
1670 }
1671
1672 udma_poll_completion(uc, &paddr);
1673
1674 return 0;
1675}
1676
1677static int udma_receive(struct dma *dma, void **dst, void *metadata)
1678{
1679 struct udma_dev *ud = dev_get_priv(dma->dev);
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301680 struct udma_chan_config *ucc;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301681 struct cppi5_host_desc_t *desc_rx;
1682 dma_addr_t buf_dma;
1683 struct udma_chan *uc;
1684 u32 buf_dma_len, pkt_len;
1685 u32 port_id = 0;
1686 int ret;
1687
1688 if (dma->id >= (ud->rchan_cnt + ud->tchan_cnt)) {
1689 dev_err(dma->dev, "invalid dma ch_id %lu\n", dma->id);
1690 return -EINVAL;
1691 }
1692 uc = &ud->channels[dma->id];
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301693 ucc = &uc->config;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301694
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301695 if (uc->config.dir != DMA_DEV_TO_MEM)
Vignesh Rffcc66e2019-02-05 17:31:24 +05301696 return -EINVAL;
1697 if (!uc->num_rx_bufs)
1698 return -EINVAL;
1699
Vignesh Raghavendra7be51212020-07-06 13:26:26 +05301700 ret = k3_nav_ringacc_ring_pop(uc->rflow->r_ring, &desc_rx);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301701 if (ret && ret != -ENODATA) {
1702 dev_err(dma->dev, "rx dma fail ch_id:%lu %d\n", dma->id, ret);
1703 return ret;
1704 } else if (ret == -ENODATA) {
1705 return 0;
1706 }
1707
1708 /* invalidate cache data */
Vignesh Raghavendrac0b94902019-12-09 10:25:35 +05301709 invalidate_dcache_range((ulong)desc_rx,
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301710 (ulong)(desc_rx + ucc->hdesc_size));
Vignesh Rffcc66e2019-02-05 17:31:24 +05301711
1712 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len);
1713 pkt_len = cppi5_hdesc_get_pktlen(desc_rx);
1714
1715 /* invalidate cache data */
Vignesh Raghavendrac0b94902019-12-09 10:25:35 +05301716 invalidate_dcache_range((ulong)buf_dma,
1717 (ulong)(buf_dma + buf_dma_len));
Vignesh Rffcc66e2019-02-05 17:31:24 +05301718
1719 cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL);
1720
1721 *dst = (void *)buf_dma;
1722 uc->num_rx_bufs--;
1723
1724 return pkt_len;
1725}
1726
1727static int udma_of_xlate(struct dma *dma, struct ofnode_phandle_args *args)
1728{
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301729 struct udma_chan_config *ucc;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301730 struct udma_dev *ud = dev_get_priv(dma->dev);
1731 struct udma_chan *uc = &ud->channels[0];
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +05301732 struct psil_endpoint_config *ep_config;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301733 u32 val;
1734
1735 for (val = 0; val < ud->ch_count; val++) {
1736 uc = &ud->channels[val];
1737 if (!uc->in_use)
1738 break;
1739 }
1740
1741 if (val == ud->ch_count)
1742 return -EBUSY;
1743
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301744 ucc = &uc->config;
1745 ucc->remote_thread_id = args->args[0];
1746 if (ucc->remote_thread_id & K3_PSIL_DST_THREAD_ID_OFFSET)
1747 ucc->dir = DMA_MEM_TO_DEV;
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +05301748 else
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301749 ucc->dir = DMA_DEV_TO_MEM;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301750
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301751 ep_config = psil_get_ep_config(ucc->remote_thread_id);
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +05301752 if (IS_ERR(ep_config)) {
1753 dev_err(ud->dev, "No configuration for psi-l thread 0x%04x\n",
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301754 uc->config.remote_thread_id);
1755 ucc->dir = DMA_MEM_TO_MEM;
1756 ucc->remote_thread_id = -1;
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +05301757 return false;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301758 }
1759
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301760 ucc->pkt_mode = ep_config->pkt_mode;
1761 ucc->channel_tpl = ep_config->channel_tpl;
1762 ucc->notdpkt = ep_config->notdpkt;
1763 ucc->ep_type = ep_config->ep_type;
Vignesh Rffcc66e2019-02-05 17:31:24 +05301764
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301765 ucc->needs_epib = ep_config->needs_epib;
1766 ucc->psd_size = ep_config->psd_size;
1767 ucc->metadata_size = (ucc->needs_epib ? CPPI5_INFO0_HDESC_EPIB_SIZE : 0) + ucc->psd_size;
1768
1769 ucc->hdesc_size = cppi5_hdesc_calc_size(ucc->needs_epib,
1770 ucc->psd_size, 0);
1771 ucc->hdesc_size = ALIGN(ucc->hdesc_size, ARCH_DMA_MINALIGN);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301772
1773 dma->id = uc->id;
1774 pr_debug("Allocated dma chn:%lu epib:%d psdata:%u meta:%u thread_id:%x\n",
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301775 dma->id, ucc->needs_epib,
1776 ucc->psd_size, ucc->metadata_size,
1777 ucc->remote_thread_id);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301778
1779 return 0;
1780}
1781
1782int udma_prepare_rcv_buf(struct dma *dma, void *dst, size_t size)
1783{
1784 struct udma_dev *ud = dev_get_priv(dma->dev);
1785 struct cppi5_host_desc_t *desc_rx;
1786 dma_addr_t dma_dst;
1787 struct udma_chan *uc;
1788 u32 desc_num;
1789
1790 if (dma->id >= (ud->rchan_cnt + ud->tchan_cnt)) {
1791 dev_err(dma->dev, "invalid dma ch_id %lu\n", dma->id);
1792 return -EINVAL;
1793 }
1794 uc = &ud->channels[dma->id];
1795
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301796 if (uc->config.dir != DMA_DEV_TO_MEM)
Vignesh Rffcc66e2019-02-05 17:31:24 +05301797 return -EINVAL;
1798
1799 if (uc->num_rx_bufs >= UDMA_RX_DESC_NUM)
1800 return -EINVAL;
1801
1802 desc_num = uc->desc_rx_cur % UDMA_RX_DESC_NUM;
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301803 desc_rx = uc->desc_rx + (desc_num * uc->config.hdesc_size);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301804 dma_dst = (dma_addr_t)dst;
1805
1806 cppi5_hdesc_reset_hbdesc(desc_rx);
1807
1808 cppi5_hdesc_init(desc_rx,
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301809 uc->config.needs_epib ? CPPI5_INFO0_HDESC_EPIB_PRESENT : 0,
1810 uc->config.psd_size);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301811 cppi5_hdesc_set_pktlen(desc_rx, size);
1812 cppi5_hdesc_attach_buf(desc_rx, dma_dst, size, dma_dst, size);
1813
Vignesh Raghavendraf03cb5c2019-12-09 10:25:39 +05301814 flush_dcache_range((unsigned long)desc_rx,
Vignesh Raghavendraaf374c22020-07-06 13:26:25 +05301815 ALIGN((unsigned long)desc_rx + uc->config.hdesc_size,
Vignesh Raghavendrac0b94902019-12-09 10:25:35 +05301816 ARCH_DMA_MINALIGN));
Vignesh Rffcc66e2019-02-05 17:31:24 +05301817
Vignesh Raghavendra7be51212020-07-06 13:26:26 +05301818 udma_push_to_ring(uc->rflow->fd_ring, desc_rx);
Vignesh Rffcc66e2019-02-05 17:31:24 +05301819
1820 uc->num_rx_bufs++;
1821 uc->desc_rx_cur++;
1822
1823 return 0;
1824}
1825
Vignesh Raghavendra5e6d9cc2019-12-04 22:17:21 +05301826static int udma_get_cfg(struct dma *dma, u32 id, void **data)
1827{
1828 struct udma_dev *ud = dev_get_priv(dma->dev);
1829 struct udma_chan *uc;
1830
1831 if (dma->id >= (ud->rchan_cnt + ud->tchan_cnt)) {
1832 dev_err(dma->dev, "invalid dma ch_id %lu\n", dma->id);
1833 return -EINVAL;
1834 }
1835
1836 switch (id) {
1837 case TI_UDMA_CHAN_PRIV_INFO:
1838 uc = &ud->channels[dma->id];
1839 *data = &uc->cfg_data;
1840 return 0;
1841 }
1842
1843 return -EINVAL;
1844}
1845
Vignesh Rffcc66e2019-02-05 17:31:24 +05301846static const struct dma_ops udma_ops = {
1847 .transfer = udma_transfer,
1848 .of_xlate = udma_of_xlate,
1849 .request = udma_request,
Simon Glassaae95882020-02-03 07:35:55 -07001850 .rfree = udma_rfree,
Vignesh Rffcc66e2019-02-05 17:31:24 +05301851 .enable = udma_enable,
1852 .disable = udma_disable,
1853 .send = udma_send,
1854 .receive = udma_receive,
1855 .prepare_rcv_buf = udma_prepare_rcv_buf,
Vignesh Raghavendra5e6d9cc2019-12-04 22:17:21 +05301856 .get_cfg = udma_get_cfg,
Vignesh Rffcc66e2019-02-05 17:31:24 +05301857};
1858
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +05301859static struct udma_match_data am654_main_data = {
1860 .psil_base = 0x1000,
1861 .enable_memcpy_support = true,
1862 .statictr_z_mask = GENMASK(11, 0),
1863 .rchan_oes_offset = 0x200,
1864 .tpl_levels = 2,
1865 .level_start_idx = {
1866 [0] = 8, /* Normal channels */
1867 [1] = 0, /* High Throughput channels */
1868 },
1869};
1870
1871static struct udma_match_data am654_mcu_data = {
1872 .psil_base = 0x6000,
1873 .enable_memcpy_support = true,
1874 .statictr_z_mask = GENMASK(11, 0),
1875 .rchan_oes_offset = 0x200,
1876 .tpl_levels = 2,
1877 .level_start_idx = {
1878 [0] = 2, /* Normal channels */
1879 [1] = 0, /* High Throughput channels */
1880 },
1881};
1882
1883static struct udma_match_data j721e_main_data = {
1884 .psil_base = 0x1000,
1885 .enable_memcpy_support = true,
1886 .flags = UDMA_FLAG_PDMA_ACC32 | UDMA_FLAG_PDMA_BURST | UDMA_FLAG_TDTYPE,
1887 .statictr_z_mask = GENMASK(23, 0),
1888 .rchan_oes_offset = 0x400,
1889 .tpl_levels = 3,
1890 .level_start_idx = {
1891 [0] = 16, /* Normal channels */
1892 [1] = 4, /* High Throughput channels */
1893 [2] = 0, /* Ultra High Throughput channels */
1894 },
1895};
1896
1897static struct udma_match_data j721e_mcu_data = {
1898 .psil_base = 0x6000,
1899 .enable_memcpy_support = true,
1900 .flags = UDMA_FLAG_PDMA_ACC32 | UDMA_FLAG_PDMA_BURST | UDMA_FLAG_TDTYPE,
1901 .statictr_z_mask = GENMASK(23, 0),
1902 .rchan_oes_offset = 0x400,
1903 .tpl_levels = 2,
1904 .level_start_idx = {
1905 [0] = 2, /* Normal channels */
1906 [1] = 0, /* High Throughput channels */
1907 },
1908};
1909
Vignesh Rffcc66e2019-02-05 17:31:24 +05301910static const struct udevice_id udma_ids[] = {
Vignesh Raghavendra5c92fff2020-07-07 13:43:34 +05301911 {
1912 .compatible = "ti,am654-navss-main-udmap",
1913 .data = (ulong)&am654_main_data,
1914 },
1915 {
1916 .compatible = "ti,am654-navss-mcu-udmap",
1917 .data = (ulong)&am654_mcu_data,
1918 }, {
1919 .compatible = "ti,j721e-navss-main-udmap",
1920 .data = (ulong)&j721e_main_data,
1921 }, {
1922 .compatible = "ti,j721e-navss-mcu-udmap",
1923 .data = (ulong)&j721e_mcu_data,
1924 },
1925 { /* Sentinel */ },
Vignesh Rffcc66e2019-02-05 17:31:24 +05301926};
1927
1928U_BOOT_DRIVER(ti_edma3) = {
1929 .name = "ti-udma",
1930 .id = UCLASS_DMA,
1931 .of_match = udma_ids,
1932 .ops = &udma_ops,
1933 .probe = udma_probe,
1934 .priv_auto_alloc_size = sizeof(struct udma_dev),
1935};