1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  BCM91280E-specific commands		File: ui_bcm91280e.c
5    *
6    *  A temporary sandbox for misc test routines and commands.
7    *
8    *  Author:  Mitch Lichtenberg
9    *
10    *********************************************************************
11    *
12    *  Copyright 2000,2001,2002,2003
13    *  Broadcom Corporation. All rights reserved.
14    *
15    *  This software is furnished under license and may be used and
16    *  copied only in accordance with the following terms and
17    *  conditions.  Subject to these conditions, you may download,
18    *  copy, install, use, modify and distribute modified or unmodified
19    *  copies of this software in source and/or binary form.  No title
20    *  or ownership is transferred hereby.
21    *
22    *  1) Any source code used, modified or distributed must reproduce
23    *     and retain this copyright notice and list of conditions
24    *     as they appear in the source file.
25    *
26    *  2) No right is granted to use any trade name, trademark, or
27    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
28    *     name may not be used to endorse or promote products derived
29    *     from this software without the prior written permission of
30    *     Broadcom Corporation.
31    *
32    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44    *     THE POSSIBILITY OF SUCH DAMAGE.
45    ********************************************************************* */
46
47
48#include "cfe.h"
49#include "ui_command.h"
50
51#if CFG_PCI
52#include "pcivar.h"
53#endif
54
55/*  *********************************************************************
56    *  prototypes
57    ********************************************************************* */
58
59int ui_init_bcm91280ecmds(void);
60
61
62/*  *********************************************************************
63    *  commands
64    ********************************************************************* */
65
66static int ui_cmd_home(ui_cmdline_t *cmd,int argc,char *argv[])
67{
68    printf("\033[H");
69    return 0;
70}
71
72static int ui_cmd_cls(ui_cmdline_t *cmd,int argc,char *argv[])
73{
74    printf("\033[H\033[2J");
75    return 0;
76}
77
78
79
80static int ui_cmd_uacwrite(ui_cmdline_t *cmd,int argc,char *argv[])
81{
82    uint32_t addr = 0;
83    hsaddr_t haddr;
84    uint64_t data0 = 0;
85    uint64_t data1 = 0;
86    uint64_t data2 = 0;
87    uint64_t data3 = 0;
88    int loops = 1;
89    int idx;
90    char *x;
91
92    if (((x = cmd_getarg(cmd,0)))) addr = lib_xtoi(x);
93    else ui_showusage(cmd);
94
95    haddr = (0xb800000000000000LL + (hsaddr_t)addr);
96
97    if (((x = cmd_getarg(cmd,1)))) data0 = lib_xtoq(x);
98    else ui_showusage(cmd);
99    if (((x = cmd_getarg(cmd,2)))) data1 = lib_xtoq(x);
100    else ui_showusage(cmd);
101    if (((x = cmd_getarg(cmd,3)))) data2 = lib_xtoq(x);
102    else ui_showusage(cmd);
103    if (((x = cmd_getarg(cmd,4)))) data3 = lib_xtoq(x);
104    else ui_showusage(cmd);
105
106    if (cmd_sw_value(cmd,"-loops",&x)) loops = atoi(x);
107
108    for (idx = 0; idx < loops; idx++) {
109	__asm __volatile ( " sd %0,0(%4) ; "
110			   " sd %1,8(%4) ; "
111			   " sd %2,16(%4) ; "
112			   " sd %3,24(%4) ; "
113			   " sync ; "
114			   " sync "
115			   : : "r"(data0),"r"(data1),"r"(data2),"r"(data3),"r"(haddr));
116	}
117
118    return 0;
119
120}
121
122static int ui_cmd_memmargin(ui_cmdline_t *cmd,int argc,char *argv[])
123{
124    hsaddr_t uacbase = 0xb800000000000500ULL;
125    hsaddr_t ucbase = 0x9000000000000500ULL;
126    int nblks=1024*1024;
127    uint64_t offset = 0;
128    uint64_t d0,d1,d2,d3;
129    uint64_t pattern;
130    int passnum = 0;
131    int idx;
132    int errors = 0;
133    int wo,ro;
134
135    ro = cmd_sw_isset(cmd,"-ro");
136    wo = cmd_sw_isset(cmd,"-wo");
137
138    if (ro && wo) return -1;
139
140    for (;;) {
141	errors = 0;
142	if (!ro) {
143	    offset = 0;
144	    pattern = 0x5555555555555555ULL;
145	    if (passnum&1) pattern = ~pattern;
146	    for (idx = 0; idx < nblks; idx++,offset += 32) {
147		__asm __volatile ( " sd %0,0(%1) ; "
148				   " sd %0,8(%1) ; "
149				   " sd %0,16(%1) ; "
150				   " sd %0,24(%1) ; "
151				   " sync ; "
152				   " sync "
153				   : : "r"(pattern),"r"(uacbase+offset));
154		pattern = ~pattern;
155		}
156	    }
157
158	if (!wo) {
159	    errors = 0;
160	    pattern = 0x5555555555555555ULL;
161	    offset = 0;
162	    if (passnum&1) pattern = ~pattern;
163	    for (idx = 0; idx < nblks; idx++,offset += 32) {
164		__asm __volatile ( " ld %1,0(%0) ; "
165				   " ld %2,8(%0) ; "
166				   " ld %3,16(%0) ; "
167				   " ld %4,24(%0) ; "
168				   " sync ; "
169				   " sync "
170				   : "=r"(d0),"=r"(d1),"=r"(d2),"=r"(d3) :"r"(ucbase+offset));
171		if ((d0 != pattern) || (d1 != pattern) || (d2 !=pattern) || (d3 != pattern)) errors++;
172		pattern = ~pattern;
173		}
174	    }
175	if (console_status()) break;
176	printf("%5d Errors: %d\n",passnum,errors);
177	errors = 0;
178	passnum++;
179	}
180
181    return 0;
182
183}
184
185
186#if CFG_PCI
187static uint64_t parse_hex(const char *num)
188{
189    uint64_t x = 0;
190    unsigned int digit;
191
192    if ((*num == '0') && (*(num+1) == 'x')) num += 2;
193
194    while (*num) {
195        if ((*num >= '0') && (*num <= '9')) {
196            digit = *num - '0';
197            }
198        else if ((*num >= 'A') && (*num <= 'F')) {
199            digit = 10 + *num - 'A';
200            }
201        else if ((*num >= 'a') && (*num <= 'f')) {
202            digit = 10 + *num - 'a';
203            }
204        else {
205            break;
206            }
207        x *= 16;
208        x += digit;
209        num++;
210        }
211
212    return x;
213}
214
215static int ui_cmd_map_pci(ui_cmdline_t *cmd,int argc,char *argv[])
216{
217    unsigned long offset, size;
218    uint64_t paddr;
219    int l2ca, endian;
220    int enable;
221    int result;
222
223    enable = !cmd_sw_isset(cmd, "-off");
224    if (enable) {
225	offset = parse_hex(cmd_getarg(cmd, 0));
226	size = parse_hex(cmd_getarg(cmd, 1));
227	paddr = parse_hex(cmd_getarg(cmd, 2));
228	l2ca = cmd_sw_isset(cmd,"-l2ca");
229	endian = cmd_sw_isset(cmd, "-matchbits");
230	result = pci_map_window(paddr, offset, size, l2ca, endian);
231	}
232    else {
233	offset = parse_hex(cmd_getarg(cmd, 0));
234	size = parse_hex(cmd_getarg(cmd, 1));
235	result = pci_unmap_window(offset, size);
236	}
237
238    return result;
239}
240#endif
241
242
243/*  *********************************************************************
244    *  ui_init_bcm91280ecmds()
245    *
246    *  Add BCM91280E-specific commands to the command table
247    *
248    *  Input parameters:
249    *  	   nothing
250    *
251    *  Return value:
252    *  	   0
253    ********************************************************************* */
254
255
256int ui_init_bcm91280ecmds(void)
257{
258    cmd_addcmd("home",
259	       ui_cmd_home,
260	       NULL,
261	       "Cursor to home position",
262	       "home",
263	       "");
264    cmd_addcmd("cls",
265	       ui_cmd_cls,
266	       NULL,
267	       "Clear screen",
268	       "home",
269	       "");
270    cmd_addcmd("uacwrite",
271	       ui_cmd_uacwrite,
272	       NULL,
273	       "Do an uncached write",
274	       "uacwrite addr data0 data1 data2 data3",
275	       "-loops=*;Number of iterations");
276
277    cmd_addcmd("memmargin",
278	       ui_cmd_memmargin,
279	       NULL,
280	       "Read/write memory continuously, count errors",
281	       "memmargin",
282	       "-ro;only do reads|"
283	       "-wo;only do writes");
284
285#if CFG_PCI
286    cmd_addcmd("map pci",
287	       ui_cmd_map_pci,
288	       NULL,
289	       "Define a BAR0 window available to PCI devices",
290	       "map pci offset size paddr [-off] [-l2ca] [-matchbits]\n\n"
291	       "Map the region of size bytes starting at paddr to appear\n"
292	       "at offset relative to BAR0\n",
293	       "-off;Remove the region|"
294	       "-l2ca;Make L2 cachable|"
295	       "-matchbits;Use match bits policy");
296#endif
297
298    return 0;
299}
300
301
302
303
304