blob: 602d993e393a7725a3fd416078d9bb3dd5c04e60 [file] [log] [blame]
Aneesh V095aea22011-07-21 09:10:12 -04001/*
2 * Copyright 2011 Linaro Limited
3 * Aneesh V <aneesh@ti.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Aneesh V095aea22011-07-21 09:10:12 -04006 */
7#include <common.h>
Nishanth Menon8a0c6d62014-03-28 11:00:04 -05008#include <asm/arch/sys_proto.h>
Aneesh V095aea22011-07-21 09:10:12 -04009static 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 */
25void 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}
Nishanth Menon8a0c6d62014-03-28 11:00:04 -050043
Paul Kocialkowski72931b12015-08-27 19:37:08 +020044__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
Paul Kocialkowski07815eb2015-08-27 19:37:12 +020049void omap_die_id_serial(void)
Nishanth Menon8a0c6d62014-03-28 11:00:04 -050050{
Paul Kocialkowski07815eb2015-08-27 19:37:12 +020051 unsigned int die_id[4] = { 0 };
52 char serial_string[17] = { 0 };
Nishanth Menon8a0c6d62014-03-28 11:00:04 -050053
Paul Kocialkowski07815eb2015-08-27 19:37:12 +020054 omap_die_id((unsigned int *)&die_id);
Nishanth Menon8a0c6d62014-03-28 11:00:04 -050055
Paul Kocialkowski07815eb2015-08-27 19:37:12 +020056 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);
Nishanth Menon8a0c6d62014-03-28 11:00:04 -050061 }
62}
Dileep Kattaf12467d2015-03-25 04:04:51 +053063
Paul Kocialkowski2da87ab2015-08-27 19:37:14 +020064void 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
Paul Kocialkowski07815eb2015-08-27 19:37:12 +020082void omap_die_id_usbethaddr(void)
Dileep Kattaf12467d2015-03-25 04:04:51 +053083{
Paul Kocialkowski07815eb2015-08-27 19:37:12 +020084 unsigned int die_id[4] = { 0 };
85 unsigned char mac[6] = { 0 };
Dileep Kattaf12467d2015-03-25 04:04:51 +053086
Paul Kocialkowski07815eb2015-08-27 19:37:12 +020087 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);
Dileep Kattaf12467d2015-03-25 04:04:51 +0530102 }
103}
Paul Kocialkowski679f82c2015-08-27 19:37:13 +0200104
105void 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", die_id[0], die_id[1], die_id[2],
112 die_id[3]);
113}