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> |
Simon Glass | 5d98285 | 2017-05-17 08:23:00 -0600 | [diff] [blame] | 8 | #include <asm/setup.h> |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 9 | #include <asm/arch/sys_proto.h> |
Aneesh V | 095aea2 | 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 | |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 22 | #ifdef CONFIG_FASTBOOT_FLASH |
| 23 | static void omap_set_fastboot_cpu(void) |
| 24 | { |
| 25 | char *cpu; |
| 26 | u32 cpu_rev = omap_revision(); |
| 27 | |
| 28 | switch (cpu_rev) { |
| 29 | case DRA752_ES1_0: |
| 30 | case DRA752_ES1_1: |
| 31 | case DRA752_ES2_0: |
| 32 | cpu = "DRA752"; |
| 33 | break; |
| 34 | case DRA722_ES1_0: |
| 35 | case DRA722_ES2_0: |
Vishal Mahaveer | b6df935 | 2017-10-12 22:47:04 -0500 | [diff] [blame^] | 36 | case DRA722_ES2_1: |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 37 | cpu = "DRA722"; |
| 38 | break; |
| 39 | default: |
| 40 | cpu = NULL; |
| 41 | printf("Warning: fastboot.cpu: unknown CPU rev: %u\n", cpu_rev); |
| 42 | } |
| 43 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 44 | env_set("fastboot.cpu", cpu); |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static void omap_set_fastboot_secure(void) |
| 48 | { |
| 49 | const char *secure; |
| 50 | u32 dev = get_device_type(); |
| 51 | |
| 52 | switch (dev) { |
| 53 | case EMU_DEVICE: |
| 54 | secure = "EMU"; |
| 55 | break; |
| 56 | case HS_DEVICE: |
| 57 | secure = "HS"; |
| 58 | break; |
| 59 | case GP_DEVICE: |
| 60 | secure = "GP"; |
| 61 | break; |
| 62 | default: |
| 63 | secure = NULL; |
| 64 | printf("Warning: fastboot.secure: unknown CPU sec: %u\n", dev); |
| 65 | } |
| 66 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 67 | env_set("fastboot.secure", secure); |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void omap_set_fastboot_board_rev(void) |
| 71 | { |
| 72 | const char *board_rev; |
| 73 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 74 | board_rev = env_get("board_rev"); |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 75 | if (board_rev == NULL) |
| 76 | printf("Warning: fastboot.board_rev: unknown board revision\n"); |
| 77 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 78 | env_set("fastboot.board_rev", board_rev); |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV |
| 82 | static u32 omap_mmc_get_part_size(const char *part) |
| 83 | { |
| 84 | int res; |
| 85 | struct blk_desc *dev_desc; |
| 86 | disk_partition_t info; |
| 87 | u64 sz = 0; |
| 88 | |
| 89 | dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV); |
| 90 | if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 91 | pr_err("invalid mmc device\n"); |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 92 | return 0; |
| 93 | } |
| 94 | |
Sam Protsenko | 2685776 | 2017-09-22 01:51:59 +0300 | [diff] [blame] | 95 | /* Check only for EFI (GPT) partition table */ |
| 96 | res = part_get_info_by_name_type(dev_desc, part, &info, PART_TYPE_EFI); |
Sam Protsenko | 19d141a | 2017-09-21 23:01:00 +0300 | [diff] [blame] | 97 | if (res < 0) |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 98 | return 0; |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 99 | |
| 100 | /* Calculate size in bytes */ |
| 101 | sz = (info.size * (u64)info.blksz); |
| 102 | /* to KiB */ |
| 103 | sz >>= 10; |
| 104 | |
| 105 | return (u32)sz; |
| 106 | } |
| 107 | |
| 108 | static void omap_set_fastboot_userdata_size(void) |
| 109 | { |
| 110 | char buf[16]; |
| 111 | u32 sz_kb; |
| 112 | |
| 113 | sz_kb = omap_mmc_get_part_size("userdata"); |
Sam Protsenko | 19d141a | 2017-09-21 23:01:00 +0300 | [diff] [blame] | 114 | if (sz_kb == 0) |
| 115 | return; /* probably it's not Android partition table */ |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 116 | |
Sam Protsenko | 19d141a | 2017-09-21 23:01:00 +0300 | [diff] [blame] | 117 | sprintf(buf, "%u", sz_kb); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 118 | env_set("fastboot.userdata_size", buf); |
Semen Protsenko | fa24eca | 2017-05-22 19:16:40 +0300 | [diff] [blame] | 119 | } |
| 120 | #else |
| 121 | static inline void omap_set_fastboot_userdata_size(void) |
| 122 | { |
| 123 | } |
| 124 | #endif /* CONFIG_FASTBOOT_FLASH_MMC_DEV */ |
| 125 | void omap_set_fastboot_vars(void) |
| 126 | { |
| 127 | omap_set_fastboot_cpu(); |
| 128 | omap_set_fastboot_secure(); |
| 129 | omap_set_fastboot_board_rev(); |
| 130 | omap_set_fastboot_userdata_size(); |
| 131 | } |
| 132 | #endif /* CONFIG_FASTBOOT_FLASH */ |
| 133 | |
Aneesh V | 095aea2 | 2011-07-21 09:10:12 -0400 | [diff] [blame] | 134 | /* |
| 135 | * Cancel out the denominator and numerator of a fraction |
| 136 | * to get smaller numerator and denominator. |
| 137 | */ |
| 138 | void cancel_out(u32 *num, u32 *den, u32 den_limit) |
| 139 | { |
| 140 | do_cancel_out(num, den, 2); |
| 141 | do_cancel_out(num, den, 3); |
| 142 | do_cancel_out(num, den, 5); |
| 143 | do_cancel_out(num, den, 7); |
| 144 | do_cancel_out(num, den, 11); |
| 145 | do_cancel_out(num, den, 13); |
| 146 | do_cancel_out(num, den, 17); |
| 147 | while ((*den) > den_limit) { |
| 148 | *num /= 2; |
| 149 | /* |
| 150 | * Round up the denominator so that the final fraction |
| 151 | * (num/den) is always <= the desired value |
| 152 | */ |
| 153 | *den = (*den + 1) / 2; |
| 154 | } |
| 155 | } |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 156 | |
Paul Kocialkowski | 72931b1 | 2015-08-27 19:37:08 +0200 | [diff] [blame] | 157 | __weak void omap_die_id(unsigned int *die_id) |
| 158 | { |
| 159 | die_id[0] = die_id[1] = die_id[2] = die_id[3] = 0; |
| 160 | } |
| 161 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 162 | void omap_die_id_serial(void) |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 163 | { |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 164 | unsigned int die_id[4] = { 0 }; |
| 165 | char serial_string[17] = { 0 }; |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 166 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 167 | omap_die_id((unsigned int *)&die_id); |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 168 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 169 | if (!env_get("serial#")) { |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 170 | snprintf(serial_string, sizeof(serial_string), |
| 171 | "%08x%08x", die_id[0], die_id[3]); |
| 172 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 173 | env_set("serial#", serial_string); |
Nishanth Menon | 8a0c6d6 | 2014-03-28 11:00:04 -0500 | [diff] [blame] | 174 | } |
| 175 | } |
Dileep Katta | f12467d | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 176 | |
Paul Kocialkowski | 2da87ab | 2015-08-27 19:37:14 +0200 | [diff] [blame] | 177 | void omap_die_id_get_board_serial(struct tag_serialnr *serialnr) |
| 178 | { |
| 179 | char *serial_string; |
| 180 | unsigned long long serial; |
| 181 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 182 | serial_string = env_get("serial#"); |
Paul Kocialkowski | 2da87ab | 2015-08-27 19:37:14 +0200 | [diff] [blame] | 183 | |
| 184 | if (serial_string) { |
| 185 | serial = simple_strtoull(serial_string, NULL, 16); |
| 186 | |
| 187 | serialnr->high = (unsigned int) (serial >> 32); |
| 188 | serialnr->low = (unsigned int) (serial & 0xffffffff); |
| 189 | } else { |
| 190 | serialnr->high = 0; |
| 191 | serialnr->low = 0; |
| 192 | } |
| 193 | } |
| 194 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 195 | void omap_die_id_usbethaddr(void) |
Dileep Katta | f12467d | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 196 | { |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 197 | unsigned int die_id[4] = { 0 }; |
| 198 | unsigned char mac[6] = { 0 }; |
Dileep Katta | f12467d | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 199 | |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 200 | omap_die_id((unsigned int *)&die_id); |
| 201 | |
Simon Glass | 00caae6 | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 202 | if (!env_get("usbethaddr")) { |
Paul Kocialkowski | 07815eb | 2015-08-27 19:37:12 +0200 | [diff] [blame] | 203 | /* |
| 204 | * Create a fake MAC address from the processor ID code. |
| 205 | * First byte is 0x02 to signify locally administered. |
| 206 | */ |
| 207 | mac[0] = 0x02; |
| 208 | mac[1] = die_id[3] & 0xff; |
| 209 | mac[2] = die_id[2] & 0xff; |
| 210 | mac[3] = die_id[1] & 0xff; |
| 211 | mac[4] = die_id[0] & 0xff; |
| 212 | mac[5] = (die_id[0] >> 8) & 0xff; |
| 213 | |
Simon Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 214 | eth_env_set_enetaddr("usbethaddr", mac); |
Dileep Katta | f12467d | 2015-03-25 04:04:51 +0530 | [diff] [blame] | 215 | } |
| 216 | } |
Paul Kocialkowski | 679f82c | 2015-08-27 19:37:13 +0200 | [diff] [blame] | 217 | |
| 218 | void omap_die_id_display(void) |
| 219 | { |
| 220 | unsigned int die_id[4] = { 0 }; |
| 221 | |
| 222 | omap_die_id(die_id); |
| 223 | |
Ladislav Michl | e22455f | 2016-06-02 11:43:16 +0200 | [diff] [blame] | 224 | printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2], |
| 225 | die_id[1], die_id[0]); |
Paul Kocialkowski | 679f82c | 2015-08-27 19:37:13 +0200 | [diff] [blame] | 226 | } |