1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2014 Freescale Semiconductor
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun /* qbman_sys_decl.h and qbman_sys.h are the two platform-specific files in the
8*4882a593Smuzhiyun * driver. They are only included via qbman_private.h, which is itself a
9*4882a593Smuzhiyun * platform-independent file and is included by all the other driver source.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * qbman_sys_decl.h is included prior to all other declarations and logic, and
12*4882a593Smuzhiyun * it exists to provide compatibility with any linux interfaces our
13*4882a593Smuzhiyun * single-source driver code is dependent on (eg. kmalloc). Ie. this file
14*4882a593Smuzhiyun * provides linux compatibility.
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * This qbman_sys.h header, on the other hand, is included *after* any common
17*4882a593Smuzhiyun * and platform-neutral declarations and logic in qbman_private.h, and exists to
18*4882a593Smuzhiyun * implement any platform-specific logic of the qbman driver itself. Ie. it is
19*4882a593Smuzhiyun * *not* to provide linux compatibility.
20*4882a593Smuzhiyun */
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /* Trace the 3 different classes of read/write access to QBMan. #undef as
23*4882a593Smuzhiyun * required. */
24*4882a593Smuzhiyun #undef QBMAN_CCSR_TRACE
25*4882a593Smuzhiyun #undef QBMAN_CINH_TRACE
26*4882a593Smuzhiyun #undef QBMAN_CENA_TRACE
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* Temporarily define this to get around the fact that cache enabled mapping is
29*4882a593Smuzhiyun * not working right now. Will remove this after uboot could map the cache
30*4882a593Smuzhiyun * enabled portal memory.
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun #define QBMAN_CINH_ONLY
33*4882a593Smuzhiyun
word_copy(void * d,const void * s,unsigned int cnt)34*4882a593Smuzhiyun static inline void word_copy(void *d, const void *s, unsigned int cnt)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun uint32_t *dd = d;
37*4882a593Smuzhiyun const uint32_t *ss = s;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun while (cnt--)
40*4882a593Smuzhiyun *(dd++) = *(ss++);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* Currently, the CENA support code expects each 32-bit word to be written in
44*4882a593Smuzhiyun * host order, and these are converted to hardware (little-endian) order on
45*4882a593Smuzhiyun * command submission. However, 64-bit quantities are must be written (and read)
46*4882a593Smuzhiyun * as two 32-bit words with the least-significant word first, irrespective of
47*4882a593Smuzhiyun * host endianness. */
u64_to_le32_copy(void * d,const uint64_t * s,unsigned int cnt)48*4882a593Smuzhiyun static inline void u64_to_le32_copy(void *d, const uint64_t *s,
49*4882a593Smuzhiyun unsigned int cnt)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun uint32_t *dd = d;
52*4882a593Smuzhiyun const uint32_t *ss = (const uint32_t *)s;
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun while (cnt--) {
55*4882a593Smuzhiyun /* TBD: the toolchain was choking on the use of 64-bit types up
56*4882a593Smuzhiyun * until recently so this works entirely with 32-bit variables.
57*4882a593Smuzhiyun * When 64-bit types become usable again, investigate better
58*4882a593Smuzhiyun * ways of doing this. */
59*4882a593Smuzhiyun #if defined(__BIG_ENDIAN)
60*4882a593Smuzhiyun *(dd++) = ss[1];
61*4882a593Smuzhiyun *(dd++) = ss[0];
62*4882a593Smuzhiyun ss += 2;
63*4882a593Smuzhiyun #else
64*4882a593Smuzhiyun *(dd++) = *(ss++);
65*4882a593Smuzhiyun *(dd++) = *(ss++);
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun }
u64_from_le32_copy(uint64_t * d,const void * s,unsigned int cnt)69*4882a593Smuzhiyun static inline void u64_from_le32_copy(uint64_t *d, const void *s,
70*4882a593Smuzhiyun unsigned int cnt)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun const uint32_t *ss = s;
73*4882a593Smuzhiyun uint32_t *dd = (uint32_t *)d;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun while (cnt--) {
76*4882a593Smuzhiyun #if defined(__BIG_ENDIAN)
77*4882a593Smuzhiyun dd[1] = *(ss++);
78*4882a593Smuzhiyun dd[0] = *(ss++);
79*4882a593Smuzhiyun dd += 2;
80*4882a593Smuzhiyun #else
81*4882a593Smuzhiyun *(dd++) = *(ss++);
82*4882a593Smuzhiyun *(dd++) = *(ss++);
83*4882a593Smuzhiyun #endif
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Convert a host-native 32bit value into little endian */
88*4882a593Smuzhiyun #if defined(__BIG_ENDIAN)
make_le32(uint32_t val)89*4882a593Smuzhiyun static inline uint32_t make_le32(uint32_t val)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
92*4882a593Smuzhiyun ((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun #else
95*4882a593Smuzhiyun #define make_le32(val) (val)
96*4882a593Smuzhiyun #endif
make_le32_n(uint32_t * val,unsigned int num)97*4882a593Smuzhiyun static inline void make_le32_n(uint32_t *val, unsigned int num)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun while (num--) {
100*4882a593Smuzhiyun *val = make_le32(*val);
101*4882a593Smuzhiyun val++;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /******************/
106*4882a593Smuzhiyun /* Portal access */
107*4882a593Smuzhiyun /******************/
108*4882a593Smuzhiyun struct qbman_swp_sys {
109*4882a593Smuzhiyun /* On GPP, the sys support for qbman_swp is here. The CENA region isi
110*4882a593Smuzhiyun * not an mmap() of the real portal registers, but an allocated
111*4882a593Smuzhiyun * place-holder, because the actual writes/reads to/from the portal are
112*4882a593Smuzhiyun * marshalled from these allocated areas using QBMan's "MC access
113*4882a593Smuzhiyun * registers". CINH accesses are atomic so there's no need for a
114*4882a593Smuzhiyun * place-holder. */
115*4882a593Smuzhiyun void *cena;
116*4882a593Smuzhiyun void __iomem *addr_cena;
117*4882a593Smuzhiyun void __iomem *addr_cinh;
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun /* P_OFFSET is (ACCESS_CMD,0,12) - offset within the portal
121*4882a593Smuzhiyun * C is (ACCESS_CMD,12,1) - is inhibited? (0==CENA, 1==CINH)
122*4882a593Smuzhiyun * SWP_IDX is (ACCESS_CMD,16,10) - Software portal index
123*4882a593Smuzhiyun * P is (ACCESS_CMD,28,1) - (0==special portal, 1==any portal)
124*4882a593Smuzhiyun * T is (ACCESS_CMD,29,1) - Command type (0==READ, 1==WRITE)
125*4882a593Smuzhiyun * E is (ACCESS_CMD,31,1) - Command execute (1 to issue, poll for 0==complete)
126*4882a593Smuzhiyun */
127*4882a593Smuzhiyun
qbman_cinh_write(struct qbman_swp_sys * s,uint32_t offset,uint32_t val)128*4882a593Smuzhiyun static inline void qbman_cinh_write(struct qbman_swp_sys *s, uint32_t offset,
129*4882a593Smuzhiyun uint32_t val)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun __raw_writel(val, s->addr_cinh + offset);
132*4882a593Smuzhiyun #ifdef QBMAN_CINH_TRACE
133*4882a593Smuzhiyun pr_info("qbman_cinh_write(%p:0x%03x) 0x%08x\n",
134*4882a593Smuzhiyun s->addr_cinh, offset, val);
135*4882a593Smuzhiyun #endif
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
qbman_cinh_read(struct qbman_swp_sys * s,uint32_t offset)138*4882a593Smuzhiyun static inline uint32_t qbman_cinh_read(struct qbman_swp_sys *s, uint32_t offset)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun uint32_t reg = __raw_readl(s->addr_cinh + offset);
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun #ifdef QBMAN_CINH_TRACE
143*4882a593Smuzhiyun pr_info("qbman_cinh_read(%p:0x%03x) 0x%08x\n",
144*4882a593Smuzhiyun s->addr_cinh, offset, reg);
145*4882a593Smuzhiyun #endif
146*4882a593Smuzhiyun return reg;
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
qbman_cena_write_start(struct qbman_swp_sys * s,uint32_t offset)149*4882a593Smuzhiyun static inline void *qbman_cena_write_start(struct qbman_swp_sys *s,
150*4882a593Smuzhiyun uint32_t offset)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun void *shadow = s->cena + offset;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun #ifdef QBMAN_CENA_TRACE
155*4882a593Smuzhiyun pr_info("qbman_cena_write_start(%p:0x%03x) %p\n",
156*4882a593Smuzhiyun s->addr_cena, offset, shadow);
157*4882a593Smuzhiyun #endif
158*4882a593Smuzhiyun BUG_ON(offset & 63);
159*4882a593Smuzhiyun dcbz(shadow);
160*4882a593Smuzhiyun return shadow;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun
qbman_cena_write_complete(struct qbman_swp_sys * s,uint32_t offset,void * cmd)163*4882a593Smuzhiyun static inline void qbman_cena_write_complete(struct qbman_swp_sys *s,
164*4882a593Smuzhiyun uint32_t offset, void *cmd)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun const uint32_t *shadow = cmd;
167*4882a593Smuzhiyun int loop;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun #ifdef QBMAN_CENA_TRACE
170*4882a593Smuzhiyun pr_info("qbman_cena_write_complete(%p:0x%03x) %p\n",
171*4882a593Smuzhiyun s->addr_cena, offset, shadow);
172*4882a593Smuzhiyun hexdump(cmd, 64);
173*4882a593Smuzhiyun #endif
174*4882a593Smuzhiyun for (loop = 15; loop >= 0; loop--)
175*4882a593Smuzhiyun #ifdef QBMAN_CINH_ONLY
176*4882a593Smuzhiyun __raw_writel(shadow[loop], s->addr_cinh +
177*4882a593Smuzhiyun offset + loop * 4);
178*4882a593Smuzhiyun #else
179*4882a593Smuzhiyun __raw_writel(shadow[loop], s->addr_cena +
180*4882a593Smuzhiyun offset + loop * 4);
181*4882a593Smuzhiyun #endif
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
qbman_cena_read(struct qbman_swp_sys * s,uint32_t offset)184*4882a593Smuzhiyun static inline void *qbman_cena_read(struct qbman_swp_sys *s, uint32_t offset)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun uint32_t *shadow = s->cena + offset;
187*4882a593Smuzhiyun unsigned int loop;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun #ifdef QBMAN_CENA_TRACE
190*4882a593Smuzhiyun pr_info("qbman_cena_read(%p:0x%03x) %p\n",
191*4882a593Smuzhiyun s->addr_cena, offset, shadow);
192*4882a593Smuzhiyun #endif
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun for (loop = 0; loop < 16; loop++)
195*4882a593Smuzhiyun #ifdef QBMAN_CINH_ONLY
196*4882a593Smuzhiyun shadow[loop] = __raw_readl(s->addr_cinh + offset
197*4882a593Smuzhiyun + loop * 4);
198*4882a593Smuzhiyun #else
199*4882a593Smuzhiyun shadow[loop] = __raw_readl(s->addr_cena + offset
200*4882a593Smuzhiyun + loop * 4);
201*4882a593Smuzhiyun #endif
202*4882a593Smuzhiyun #ifdef QBMAN_CENA_TRACE
203*4882a593Smuzhiyun hexdump(shadow, 64);
204*4882a593Smuzhiyun #endif
205*4882a593Smuzhiyun return shadow;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
qbman_cena_invalidate_prefetch(struct qbman_swp_sys * s,uint32_t offset)208*4882a593Smuzhiyun static inline void qbman_cena_invalidate_prefetch(struct qbman_swp_sys *s,
209*4882a593Smuzhiyun uint32_t offset)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun }
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun /******************/
214*4882a593Smuzhiyun /* Portal support */
215*4882a593Smuzhiyun /******************/
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* The SWP_CFG portal register is special, in that it is used by the
218*4882a593Smuzhiyun * platform-specific code rather than the platform-independent code in
219*4882a593Smuzhiyun * qbman_portal.c. So use of it is declared locally here. */
220*4882a593Smuzhiyun #define QBMAN_CINH_SWP_CFG 0xd00
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* For MC portal use, we always configure with
223*4882a593Smuzhiyun * DQRR_MF is (SWP_CFG,20,3) - DQRR max fill (<- 0x4)
224*4882a593Smuzhiyun * EST is (SWP_CFG,16,3) - EQCR_CI stashing threshold (<- 0x0)
225*4882a593Smuzhiyun * RPM is (SWP_CFG,12,2) - RCR production notification mode (<- 0x3)
226*4882a593Smuzhiyun * DCM is (SWP_CFG,10,2) - DQRR consumption notification mode (<- 0x2)
227*4882a593Smuzhiyun * EPM is (SWP_CFG,8,2) - EQCR production notification mode (<- 0x3)
228*4882a593Smuzhiyun * SD is (SWP_CFG,5,1) - memory stashing drop enable (<- FALSE)
229*4882a593Smuzhiyun * SP is (SWP_CFG,4,1) - memory stashing priority (<- TRUE)
230*4882a593Smuzhiyun * SE is (SWP_CFG,3,1) - memory stashing enable (<- 0x0)
231*4882a593Smuzhiyun * DP is (SWP_CFG,2,1) - dequeue stashing priority (<- TRUE)
232*4882a593Smuzhiyun * DE is (SWP_CFG,1,1) - dequeue stashing enable (<- 0x0)
233*4882a593Smuzhiyun * EP is (SWP_CFG,0,1) - EQCR_CI stashing priority (<- FALSE)
234*4882a593Smuzhiyun */
qbman_set_swp_cfg(uint8_t max_fill,uint8_t wn,uint8_t est,uint8_t rpm,uint8_t dcm,uint8_t epm,int sd,int sp,int se,int dp,int de,int ep)235*4882a593Smuzhiyun static inline uint32_t qbman_set_swp_cfg(uint8_t max_fill, uint8_t wn,
236*4882a593Smuzhiyun uint8_t est, uint8_t rpm, uint8_t dcm,
237*4882a593Smuzhiyun uint8_t epm, int sd, int sp, int se,
238*4882a593Smuzhiyun int dp, int de, int ep)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun uint32_t reg;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun reg = e32_uint8_t(20, (uint32_t)(3 + (max_fill >> 3)), max_fill) |
243*4882a593Smuzhiyun e32_uint8_t(16, 3, est) | e32_uint8_t(12, 2, rpm) |
244*4882a593Smuzhiyun e32_uint8_t(10, 2, dcm) | e32_uint8_t(8, 2, epm) |
245*4882a593Smuzhiyun e32_int(5, 1, sd) | e32_int(4, 1, sp) | e32_int(3, 1, se) |
246*4882a593Smuzhiyun e32_int(2, 1, dp) | e32_int(1, 1, de) | e32_int(0, 1, ep) |
247*4882a593Smuzhiyun e32_uint8_t(14, 1, wn);
248*4882a593Smuzhiyun return reg;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
qbman_swp_sys_init(struct qbman_swp_sys * s,const struct qbman_swp_desc * d,uint8_t dqrr_size)251*4882a593Smuzhiyun static inline int qbman_swp_sys_init(struct qbman_swp_sys *s,
252*4882a593Smuzhiyun const struct qbman_swp_desc *d,
253*4882a593Smuzhiyun uint8_t dqrr_size)
254*4882a593Smuzhiyun {
255*4882a593Smuzhiyun uint32_t reg;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun s->addr_cena = d->cena_bar;
258*4882a593Smuzhiyun s->addr_cinh = d->cinh_bar;
259*4882a593Smuzhiyun s->cena = (void *)valloc(CONFIG_SYS_PAGE_SIZE);
260*4882a593Smuzhiyun if (!s->cena) {
261*4882a593Smuzhiyun printf("Could not allocate page for cena shadow\n");
262*4882a593Smuzhiyun return -1;
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun memset((void *)s->cena, 0x00, CONFIG_SYS_PAGE_SIZE);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun #ifdef QBMAN_CHECKING
267*4882a593Smuzhiyun /* We should never be asked to initialise for a portal that isn't in
268*4882a593Smuzhiyun * the power-on state. (Ie. don't forget to reset portals when they are
269*4882a593Smuzhiyun * decommissioned!)
270*4882a593Smuzhiyun */
271*4882a593Smuzhiyun reg = qbman_cinh_read(s, QBMAN_CINH_SWP_CFG);
272*4882a593Smuzhiyun BUG_ON(reg);
273*4882a593Smuzhiyun #endif
274*4882a593Smuzhiyun #ifdef QBMAN_CINH_ONLY
275*4882a593Smuzhiyun reg = qbman_set_swp_cfg(dqrr_size, 1, 0, 3, 2, 3, 0, 1, 0, 1, 0, 0);
276*4882a593Smuzhiyun #else
277*4882a593Smuzhiyun reg = qbman_set_swp_cfg(dqrr_size, 0, 0, 3, 2, 3, 0, 1, 0, 1, 0, 0);
278*4882a593Smuzhiyun #endif
279*4882a593Smuzhiyun qbman_cinh_write(s, QBMAN_CINH_SWP_CFG, reg);
280*4882a593Smuzhiyun reg = qbman_cinh_read(s, QBMAN_CINH_SWP_CFG);
281*4882a593Smuzhiyun if (!reg) {
282*4882a593Smuzhiyun printf("The portal is not enabled!\n");
283*4882a593Smuzhiyun free(s->cena);
284*4882a593Smuzhiyun return -1;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun return 0;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun
qbman_swp_sys_finish(struct qbman_swp_sys * s)289*4882a593Smuzhiyun static inline void qbman_swp_sys_finish(struct qbman_swp_sys *s)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun free((void *)s->cena);
292*4882a593Smuzhiyun }
293