blob: 1696c2dbda6cda6a321ce503bbb4ad6008ba1155 [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>
Nishanth Menon92adeb62014-03-28 11:00:04 -05008#include <asm/arch/sys_proto.h>
Aneesh Vc0e88522011-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 Menon92adeb62014-03-28 11:00:04 -050043
44void __weak usb_fake_mac_from_die_id(u32 *id)
45{
46 uint8_t device_mac[6];
47
48 if (!getenv("usbethaddr")) {
49 /*
50 * create a fake MAC address from the processor ID code.
51 * first byte is 0x02 to signify locally administered.
52 */
53 device_mac[0] = 0x02;
54 device_mac[1] = id[3] & 0xff;
55 device_mac[2] = id[2] & 0xff;
56 device_mac[3] = id[1] & 0xff;
57 device_mac[4] = id[0] & 0xff;
58 device_mac[5] = (id[0] >> 8) & 0xff;
59
60 eth_setenv_enetaddr("usbethaddr", device_mac);
61 }
62}