blob: e0e32123dcddee36b5ccedc48b42d6282501b944 [file] [log] [blame]
Aneesh Vc0e88522011-07-21 09:10:12 -04001/*
2 * Copyright 2011 Linaro Limited
3 * Aneesh V <aneesh@ti.com>
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Aneesh Vc0e88522011-07-21 09:10:12 -04006 */
7#include <common.h>
Simon Glassd9a766f2017-05-17 08:23:00 -06008#include <asm/setup.h>
Nishanth Menon92adeb62014-03-28 11:00:04 -05009#include <asm/arch/sys_proto.h>
Aneesh Vc0e88522011-07-21 09:10:12 -040010static 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 */
26void 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}
Nishanth Menon92adeb62014-03-28 11:00:04 -050044
Paul Kocialkowskie0cfa452015-08-27 19:37:08 +020045__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
Paul Kocialkowski2edadee2015-08-27 19:37:12 +020050void omap_die_id_serial(void)
Nishanth Menon92adeb62014-03-28 11:00:04 -050051{
Paul Kocialkowski2edadee2015-08-27 19:37:12 +020052 unsigned int die_id[4] = { 0 };
53 char serial_string[17] = { 0 };
Nishanth Menon92adeb62014-03-28 11:00:04 -050054
Paul Kocialkowski2edadee2015-08-27 19:37:12 +020055 omap_die_id((unsigned int *)&die_id);
Nishanth Menon92adeb62014-03-28 11:00:04 -050056
Paul Kocialkowski2edadee2015-08-27 19:37:12 +020057 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);
Nishanth Menon92adeb62014-03-28 11:00:04 -050062 }
63}
Dileep Katta7354dfc2015-03-25 04:04:51 +053064
Paul Kocialkowskia7267d22015-08-27 19:37:14 +020065void 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
Paul Kocialkowski2edadee2015-08-27 19:37:12 +020083void omap_die_id_usbethaddr(void)
Dileep Katta7354dfc2015-03-25 04:04:51 +053084{
Paul Kocialkowski2edadee2015-08-27 19:37:12 +020085 unsigned int die_id[4] = { 0 };
86 unsigned char mac[6] = { 0 };
Dileep Katta7354dfc2015-03-25 04:04:51 +053087
Paul Kocialkowski2edadee2015-08-27 19:37:12 +020088 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);
Dileep Katta7354dfc2015-03-25 04:04:51 +0530103 }
104}
Paul Kocialkowski6bc318e2015-08-27 19:37:13 +0200105
106void omap_die_id_display(void)
107{
108 unsigned int die_id[4] = { 0 };
109
110 omap_die_id(die_id);
111
Ladislav Michlc84b9522016-06-02 11:43:16 +0200112 printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2],
113 die_id[1], die_id[0]);
Paul Kocialkowski6bc318e2015-08-27 19:37:13 +0200114}