utils.c 4.8 KB

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