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