utils.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2011 Linaro Limited
  4. * Aneesh V <aneesh@ti.com>
  5. */
  6. #include <common.h>
  7. #include <env.h>
  8. #include <asm/setup.h>
  9. #include <asm/arch/sys_proto.h>
  10. #include <asm/omap_common.h>
  11. static void do_cancel_out(u32 *num, u32 *den, u32 factor)
  12. {
  13. while (1) {
  14. if (((*num)/factor*factor == (*num)) &&
  15. ((*den)/factor*factor == (*den))) {
  16. (*num) /= factor;
  17. (*den) /= factor;
  18. } else
  19. break;
  20. }
  21. }
  22. #ifdef CONFIG_FASTBOOT_FLASH
  23. static void omap_set_fastboot_cpu(void)
  24. {
  25. char *cpu;
  26. u32 cpu_rev = omap_revision();
  27. switch (cpu_rev) {
  28. case DRA762_ES1_0:
  29. case DRA762_ABZ_ES1_0:
  30. case DRA762_ACD_ES1_0:
  31. cpu = "DRA762";
  32. break;
  33. case DRA752_ES1_0:
  34. case DRA752_ES1_1:
  35. case DRA752_ES2_0:
  36. cpu = "DRA752";
  37. break;
  38. case DRA722_ES1_0:
  39. case DRA722_ES2_0:
  40. case DRA722_ES2_1:
  41. cpu = "DRA722";
  42. break;
  43. default:
  44. cpu = NULL;
  45. printf("Warning: fastboot.cpu: unknown CPU rev: %u\n", cpu_rev);
  46. }
  47. env_set("fastboot.cpu", cpu);
  48. }
  49. static void omap_set_fastboot_secure(void)
  50. {
  51. const char *secure;
  52. u32 dev = get_device_type();
  53. switch (dev) {
  54. case EMU_DEVICE:
  55. secure = "EMU";
  56. break;
  57. case HS_DEVICE:
  58. secure = "HS";
  59. break;
  60. case GP_DEVICE:
  61. secure = "GP";
  62. break;
  63. default:
  64. secure = NULL;
  65. printf("Warning: fastboot.secure: unknown CPU sec: %u\n", dev);
  66. }
  67. env_set("fastboot.secure", secure);
  68. }
  69. static void omap_set_fastboot_board_rev(void)
  70. {
  71. const char *board_rev;
  72. board_rev = env_get("board_rev");
  73. if (board_rev == NULL)
  74. printf("Warning: fastboot.board_rev: unknown board revision\n");
  75. env_set("fastboot.board_rev", board_rev);
  76. }
  77. #ifdef CONFIG_FASTBOOT_FLASH_MMC
  78. static u32 omap_mmc_get_part_size(const char *part)
  79. {
  80. int res;
  81. struct blk_desc *dev_desc;
  82. disk_partition_t info;
  83. u64 sz = 0;
  84. dev_desc = blk_get_dev("mmc", CONFIG_FASTBOOT_FLASH_MMC_DEV);
  85. if (!dev_desc || dev_desc->type == DEV_TYPE_UNKNOWN) {
  86. pr_err("invalid mmc device\n");
  87. return 0;
  88. }
  89. /* Check only for EFI (GPT) partition table */
  90. res = part_get_info_by_name_type(dev_desc, part, &info, PART_TYPE_EFI);
  91. if (res < 0)
  92. return 0;
  93. /* Calculate size in bytes */
  94. sz = (info.size * (u64)info.blksz);
  95. /* to KiB */
  96. sz >>= 10;
  97. return (u32)sz;
  98. }
  99. static void omap_set_fastboot_userdata_size(void)
  100. {
  101. char buf[16];
  102. u32 sz_kb;
  103. sz_kb = omap_mmc_get_part_size("userdata");
  104. if (sz_kb == 0)
  105. return; /* probably it's not Android partition table */
  106. sprintf(buf, "%u", sz_kb);
  107. env_set("fastboot.userdata_size", buf);
  108. }
  109. #else
  110. static inline void omap_set_fastboot_userdata_size(void)
  111. {
  112. }
  113. #endif /* CONFIG_FASTBOOT_FLASH_MMC */
  114. static void omap_set_fastboot_product(void)
  115. {
  116. const char *board_name;
  117. board_name = env_get("board_name");
  118. if (board_name == NULL)
  119. printf("Warning: fastboot.product: unknown board\n");
  120. env_set("fastboot.product", board_name);
  121. }
  122. void omap_set_fastboot_vars(void)
  123. {
  124. omap_set_fastboot_cpu();
  125. omap_set_fastboot_secure();
  126. omap_set_fastboot_board_rev();
  127. omap_set_fastboot_userdata_size();
  128. omap_set_fastboot_product();
  129. }
  130. #endif /* CONFIG_FASTBOOT_FLASH */
  131. /*
  132. * Cancel out the denominator and numerator of a fraction
  133. * to get smaller numerator and denominator.
  134. */
  135. void cancel_out(u32 *num, u32 *den, u32 den_limit)
  136. {
  137. do_cancel_out(num, den, 2);
  138. do_cancel_out(num, den, 3);
  139. do_cancel_out(num, den, 5);
  140. do_cancel_out(num, den, 7);
  141. do_cancel_out(num, den, 11);
  142. do_cancel_out(num, den, 13);
  143. do_cancel_out(num, den, 17);
  144. while ((*den) > den_limit) {
  145. *num /= 2;
  146. /*
  147. * Round up the denominator so that the final fraction
  148. * (num/den) is always <= the desired value
  149. */
  150. *den = (*den + 1) / 2;
  151. }
  152. }
  153. __weak void omap_die_id(unsigned int *die_id)
  154. {
  155. die_id[0] = die_id[1] = die_id[2] = die_id[3] = 0;
  156. }
  157. void omap_die_id_serial(void)
  158. {
  159. unsigned int die_id[4] = { 0 };
  160. char serial_string[17] = { 0 };
  161. omap_die_id((unsigned int *)&die_id);
  162. if (!env_get("serial#")) {
  163. snprintf(serial_string, sizeof(serial_string),
  164. "%08x%08x", die_id[0], die_id[3]);
  165. env_set("serial#", serial_string);
  166. }
  167. }
  168. void omap_die_id_get_board_serial(struct tag_serialnr *serialnr)
  169. {
  170. char *serial_string;
  171. unsigned long long serial;
  172. serial_string = env_get("serial#");
  173. if (serial_string) {
  174. serial = simple_strtoull(serial_string, NULL, 16);
  175. serialnr->high = (unsigned int) (serial >> 32);
  176. serialnr->low = (unsigned int) (serial & 0xffffffff);
  177. } else {
  178. serialnr->high = 0;
  179. serialnr->low = 0;
  180. }
  181. }
  182. void omap_die_id_usbethaddr(void)
  183. {
  184. unsigned int die_id[4] = { 0 };
  185. unsigned char mac[6] = { 0 };
  186. omap_die_id((unsigned int *)&die_id);
  187. if (!env_get("usbethaddr")) {
  188. /*
  189. * Create a fake MAC address from the processor ID code.
  190. * First byte is 0x02 to signify locally administered.
  191. */
  192. mac[0] = 0x02;
  193. mac[1] = die_id[3] & 0xff;
  194. mac[2] = die_id[2] & 0xff;
  195. mac[3] = die_id[1] & 0xff;
  196. mac[4] = die_id[0] & 0xff;
  197. mac[5] = (die_id[0] >> 8) & 0xff;
  198. eth_env_set_enetaddr("usbethaddr", mac);
  199. if (!env_get("ethaddr"))
  200. eth_env_set_enetaddr("ethaddr", mac);
  201. }
  202. }
  203. void omap_die_id_display(void)
  204. {
  205. unsigned int die_id[4] = { 0 };
  206. omap_die_id(die_id);
  207. printf("OMAP die ID: %08x%08x%08x%08x\n", die_id[3], die_id[2],
  208. die_id[1], die_id[0]);
  209. }