xref: /openbmc/u-boot/arch/arm/mach-omap2/utils.c (revision 33b78476)
1 /*
2  * Copyright 2011 Linaro Limited
3  * Aneesh V <aneesh@ti.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #include <common.h>
8 #include <asm/arch/sys_proto.h>
9 static void do_cancel_out(u32 *num, u32 *den, u32 factor)
10 {
11 	while (1) {
12 		if (((*num)/factor*factor == (*num)) &&
13 		   ((*den)/factor*factor == (*den))) {
14 			(*num) /= factor;
15 			(*den) /= factor;
16 		} else
17 			break;
18 	}
19 }
20 
21 /*
22  * Cancel out the denominator and numerator of a fraction
23  * to get smaller numerator and denominator.
24  */
25 void cancel_out(u32 *num, u32 *den, u32 den_limit)
26 {
27 	do_cancel_out(num, den, 2);
28 	do_cancel_out(num, den, 3);
29 	do_cancel_out(num, den, 5);
30 	do_cancel_out(num, den, 7);
31 	do_cancel_out(num, den, 11);
32 	do_cancel_out(num, den, 13);
33 	do_cancel_out(num, den, 17);
34 	while ((*den) > den_limit) {
35 		*num /= 2;
36 		/*
37 		 * Round up the denominator so that the final fraction
38 		 * (num/den) is always <= the desired value
39 		 */
40 		*den = (*den + 1) / 2;
41 	}
42 }
43 
44 __weak void omap_die_id(unsigned int *die_id)
45 {
46 	die_id[0] = die_id[1] = die_id[2] = die_id[3] = 0;
47 }
48 
49 void omap_die_id_serial(void)
50 {
51 	unsigned int die_id[4] = { 0 };
52 	char serial_string[17] = { 0 };
53 
54 	omap_die_id((unsigned int *)&die_id);
55 
56 	if (!getenv("serial#")) {
57 		snprintf(serial_string, sizeof(serial_string),
58 			"%08x%08x", die_id[0], die_id[3]);
59 
60 		setenv("serial#", serial_string);
61 	}
62 }
63 
64 void omap_die_id_get_board_serial(struct tag_serialnr *serialnr)
65 {
66 	char *serial_string;
67 	unsigned long long serial;
68 
69 	serial_string = getenv("serial#");
70 
71 	if (serial_string) {
72 		serial = simple_strtoull(serial_string, NULL, 16);
73 
74 		serialnr->high = (unsigned int) (serial >> 32);
75 		serialnr->low = (unsigned int) (serial & 0xffffffff);
76 	} else {
77 		serialnr->high = 0;
78 		serialnr->low = 0;
79 	}
80 }
81 
82 void omap_die_id_usbethaddr(void)
83 {
84 	unsigned int die_id[4] = { 0 };
85 	unsigned char mac[6] = { 0 };
86 
87 	omap_die_id((unsigned int *)&die_id);
88 
89 	if (!getenv("usbethaddr")) {
90 		/*
91 		 * Create a fake MAC address from the processor ID code.
92 		 * First byte is 0x02 to signify locally administered.
93 		 */
94 		mac[0] = 0x02;
95 		mac[1] = die_id[3] & 0xff;
96 		mac[2] = die_id[2] & 0xff;
97 		mac[3] = die_id[1] & 0xff;
98 		mac[4] = die_id[0] & 0xff;
99 		mac[5] = (die_id[0] >> 8) & 0xff;
100 
101 		eth_setenv_enetaddr("usbethaddr", mac);
102 	}
103 }
104 
105 void omap_die_id_display(void)
106 {
107 	unsigned int die_id[4] = { 0 };
108 
109 	omap_die_id(die_id);
110 
111 	printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2],
112 		die_id[1], die_id[0]);
113 }
114