1*7c073eb7SJohnny Huang /*
2*7c073eb7SJohnny Huang * This program is distributed in the hope that it will be useful,
3*7c073eb7SJohnny Huang * but WITHOUT ANY WARRANTY; without even the implied warranty of
4*7c073eb7SJohnny Huang * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5*7c073eb7SJohnny Huang * GNU General Public License for more details.
6*7c073eb7SJohnny Huang *
7*7c073eb7SJohnny Huang * You should have received a copy of the GNU General Public License
8*7c073eb7SJohnny Huang * along with this program; if not, write to the Free Software
9*7c073eb7SJohnny Huang * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
10*7c073eb7SJohnny Huang */
11*7c073eb7SJohnny Huang #include <stdlib.h>
12*7c073eb7SJohnny Huang #include <common.h>
13*7c073eb7SJohnny Huang #include <console.h>
14*7c073eb7SJohnny Huang #include <bootretry.h>
15*7c073eb7SJohnny Huang #include <cli.h>
16*7c073eb7SJohnny Huang #include <command.h>
17*7c073eb7SJohnny Huang #include <console.h>
18*7c073eb7SJohnny Huang #include <malloc.h>
19*7c073eb7SJohnny Huang #include <inttypes.h>
20*7c073eb7SJohnny Huang #include <mapmem.h>
21*7c073eb7SJohnny Huang #include <asm/io.h>
22*7c073eb7SJohnny Huang #include <linux/compiler.h>
23*7c073eb7SJohnny Huang
24*7c073eb7SJohnny Huang DECLARE_GLOBAL_DATA_PTR;
25*7c073eb7SJohnny Huang
26*7c073eb7SJohnny Huang #define AST_RNG_TYPE 0x1e6e2520
27*7c073eb7SJohnny Huang #define AST_RNG 0x1e6e2524
28*7c073eb7SJohnny Huang
29*7c073eb7SJohnny Huang #define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
30*7c073eb7SJohnny Huang #define PBWIDTH 60
31*7c073eb7SJohnny Huang
printProgress(int numerator,int denominator,char * format,...)32*7c073eb7SJohnny Huang void printProgress(int numerator, int denominator, char *format, ...)
33*7c073eb7SJohnny Huang {
34*7c073eb7SJohnny Huang int val = numerator * 100 / denominator;
35*7c073eb7SJohnny Huang int lpad = numerator * PBWIDTH / denominator;
36*7c073eb7SJohnny Huang int rpad = PBWIDTH - lpad;
37*7c073eb7SJohnny Huang char buffer[256];
38*7c073eb7SJohnny Huang va_list aptr;
39*7c073eb7SJohnny Huang
40*7c073eb7SJohnny Huang va_start(aptr, format);
41*7c073eb7SJohnny Huang vsprintf(buffer, format, aptr);
42*7c073eb7SJohnny Huang va_end(aptr);
43*7c073eb7SJohnny Huang
44*7c073eb7SJohnny Huang printf("\r%3d%% [%.*s%*s] %s", val, lpad, PBSTR, rpad, "", buffer);
45*7c073eb7SJohnny Huang if (numerator == denominator)
46*7c073eb7SJohnny Huang printf("\n");
47*7c073eb7SJohnny Huang }
48*7c073eb7SJohnny Huang
wait_new_rng(void)49*7c073eb7SJohnny Huang void wait_new_rng(void)
50*7c073eb7SJohnny Huang {
51*7c073eb7SJohnny Huang
52*7c073eb7SJohnny Huang int i;
53*7c073eb7SJohnny Huang // unsigned long long start, end;
54*7c073eb7SJohnny Huang // long long time;
55*7c073eb7SJohnny Huang
56*7c073eb7SJohnny Huang // start = get_ticks();
57*7c073eb7SJohnny Huang
58*7c073eb7SJohnny Huang // take 3456 kicks time, about 2.88us
59*7c073eb7SJohnny Huang i = 200;
60*7c073eb7SJohnny Huang while (i--) {
61*7c073eb7SJohnny Huang asm("");
62*7c073eb7SJohnny Huang }
63*7c073eb7SJohnny Huang
64*7c073eb7SJohnny Huang // end = get_ticks();
65*7c073eb7SJohnny Huang // time = end - start;
66*7c073eb7SJohnny Huang // printf("%llu %llu %lld\n", start, end, time);
67*7c073eb7SJohnny Huang }
68*7c073eb7SJohnny Huang
do_ast_rng(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])69*7c073eb7SJohnny Huang static int do_ast_rng(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
70*7c073eb7SJohnny Huang {
71*7c073eb7SJohnny Huang int i, j;
72*7c073eb7SJohnny Huang int type;
73*7c073eb7SJohnny Huang int len;
74*7c073eb7SJohnny Huang phys_addr_t addr;
75*7c073eb7SJohnny Huang char *buf;
76*7c073eb7SJohnny Huang
77*7c073eb7SJohnny Huang if (argc != 4)
78*7c073eb7SJohnny Huang return CMD_RET_USAGE;
79*7c073eb7SJohnny Huang type = simple_strtoul(argv[1], NULL, 16);
80*7c073eb7SJohnny Huang addr = simple_strtoul(argv[2], NULL, 16);
81*7c073eb7SJohnny Huang len = simple_strtoul(argv[3], NULL, 16);
82*7c073eb7SJohnny Huang buf = map_physmem(addr, len, MAP_WRBACK);
83*7c073eb7SJohnny Huang
84*7c073eb7SJohnny Huang if (type > 7)
85*7c073eb7SJohnny Huang return CMD_RET_USAGE;
86*7c073eb7SJohnny Huang writel((type & 0x7) << 1, AST_RNG_TYPE);
87*7c073eb7SJohnny Huang
88*7c073eb7SJohnny Huang wait_new_rng();
89*7c073eb7SJohnny Huang
90*7c073eb7SJohnny Huang for (i = 0; i < len; i++) {
91*7c073eb7SJohnny Huang if (i % 1000 == 0)
92*7c073eb7SJohnny Huang printProgress(i, len, "");
93*7c073eb7SJohnny Huang buf[i] = 0;
94*7c073eb7SJohnny Huang for (j = 0; j < 8; j++) {
95*7c073eb7SJohnny Huang buf[i] |= (readl(AST_RNG) & 0x1) << j;
96*7c073eb7SJohnny Huang wait_new_rng();
97*7c073eb7SJohnny Huang }
98*7c073eb7SJohnny Huang }
99*7c073eb7SJohnny Huang
100*7c073eb7SJohnny Huang printProgress(len, len, "");
101*7c073eb7SJohnny Huang
102*7c073eb7SJohnny Huang return CMD_RET_SUCCESS;
103*7c073eb7SJohnny Huang }
104*7c073eb7SJohnny Huang
105*7c073eb7SJohnny Huang U_BOOT_CMD(
106*7c073eb7SJohnny Huang rng, 4, 0, do_ast_rng,
107*7c073eb7SJohnny Huang "ASPEED true random number generator",
108*7c073eb7SJohnny Huang "rng <type> <addr> <len>\n"
109*7c073eb7SJohnny Huang " type: 0~7\n"
110*7c073eb7SJohnny Huang );
111