Aneesh V | 095aea2 | 2011-07-21 09:10:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Linaro Limited |
| 3 | * Aneesh V <aneesh@ti.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Aneesh V | 095aea2 | 2011-07-21 09:10:12 -0400 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 8 | #include <asm/arch/sys_proto.h> |
Aneesh V | 095aea2 | 2011-07-21 09:10:12 -0400 | [diff] [blame] | 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 | } |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 43 | |
Paul Kocialkowski | 72931b1 | 2015-08-27 19:37:08 +0200 | [diff] [blame] | 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 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 49 | void omap_die_id_serial(void) |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 50 | { |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 51 | unsigned int die_id[4] = { 0 }; |
| 52 | char serial_string[17] = { 0 }; |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 53 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 54 | omap_die_id((unsigned int *)&die_id); |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 55 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 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); |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 61 | } |
| 62 | } |
Dileep Katta | f12467d | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 63 | |
Paul Kocialkowski | 2da87ab | 2015-08-27 19:37:14 +0200 | [diff] [blame] | 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 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 82 | void omap_die_id_usbethaddr(void) |
Dileep Katta | f12467d | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 83 | { |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 84 | unsigned int die_id[4] = { 0 }; |
| 85 | unsigned char mac[6] = { 0 }; |
Dileep Katta | f12467d | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 86 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 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); |
Dileep Katta | f12467d | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 102 | } |
| 103 | } |
Paul Kocialkowski | 679f82c | 2015-08-27 19:37:13 +0200 | [diff] [blame] | 104 | |
| 105 | void omap_die_id_display(void) |
| 106 | { |
| 107 | unsigned int die_id[4] = { 0 }; |
| 108 | |
| 109 | omap_die_id(die_id); |
| 110 | |
Ladislav Michl | e22455f | 2016-06-02 11:43:16 +0200 | [diff] [blame] | 111 | printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2], |
| 112 | die_id[1], die_id[0]); |
Paul Kocialkowski | 679f82c | 2015-08-27 19:37:13 +0200 | [diff] [blame] | 113 | } |