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