utils.c 4.6 KB

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