utils.c 4.6 KB

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