utils.c 4.7 KB

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