Aneesh V | c0e8852 | 2011-07-21 09:10:12 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Linaro Limited |
| 3 | * Aneesh V <aneesh@ti.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Aneesh V | c0e8852 | 2011-07-21 09:10:12 -0400 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
Simon Glass | d9a766f | 2017-05-17 08:23:00 -0600 | [diff] [blame] | 8 | #include <asm/setup.h> |
Nishanth Menon | 92adeb6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 9 | #include <asm/arch/sys_proto.h> |
Aneesh V | c0e8852 | 2011-07-21 09:10:12 -0400 | [diff] [blame] | 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 | } |
Nishanth Menon | 92adeb6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 44 | |
Paul Kocialkowski | e0cfa45 | 2015-08-27 19:37:08 +0200 | [diff] [blame] | 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 | |
Paul Kocialkowski | 2edadee | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 50 | void omap_die_id_serial(void) |
Nishanth Menon | 92adeb6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 51 | { |
Paul Kocialkowski | 2edadee | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 52 | unsigned int die_id[4] = { 0 }; |
| 53 | char serial_string[17] = { 0 }; |
Nishanth Menon | 92adeb6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 54 | |
Paul Kocialkowski | 2edadee | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 55 | omap_die_id((unsigned int *)&die_id); |
Nishanth Menon | 92adeb6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 56 | |
Paul Kocialkowski | 2edadee | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 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); |
Nishanth Menon | 92adeb6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 62 | } |
| 63 | } |
Dileep Katta | 7354dfc | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 64 | |
Paul Kocialkowski | a7267d2 | 2015-08-27 19:37:14 +0200 | [diff] [blame] | 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 | |
Paul Kocialkowski | 2edadee | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 83 | void omap_die_id_usbethaddr(void) |
Dileep Katta | 7354dfc | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 84 | { |
Paul Kocialkowski | 2edadee | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 85 | unsigned int die_id[4] = { 0 }; |
| 86 | unsigned char mac[6] = { 0 }; |
Dileep Katta | 7354dfc | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 87 | |
Paul Kocialkowski | 2edadee | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 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); |
Dileep Katta | 7354dfc | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 103 | } |
| 104 | } |
Paul Kocialkowski | 6bc318e | 2015-08-27 19:37:13 +0200 | [diff] [blame] | 105 | |
| 106 | void omap_die_id_display(void) |
| 107 | { |
| 108 | unsigned int die_id[4] = { 0 }; |
| 109 | |
| 110 | omap_die_id(die_id); |
| 111 | |
Ladislav Michl | c84b952 | 2016-06-02 11:43:16 +0200 | [diff] [blame] | 112 | printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2], |
| 113 | die_id[1], die_id[0]); |
Paul Kocialkowski | 6bc318e | 2015-08-27 19:37:13 +0200 | [diff] [blame] | 114 | } |