utils.c 5.1 KB

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