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