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