common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2000-2009
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. */
  6. #ifndef __COMMON_H_
  7. #define __COMMON_H_ 1
  8. #ifndef __ASSEMBLY__ /* put C only stuff in this section */
  9. typedef unsigned char uchar;
  10. typedef volatile unsigned long vu_long;
  11. typedef volatile unsigned short vu_short;
  12. typedef volatile unsigned char vu_char;
  13. /* Allow sharing constants with type modifiers between C and assembly. */
  14. #define _AC(X, Y) (X##Y)
  15. #include <config.h>
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <asm-offsets.h>
  19. #include <linux/bitops.h>
  20. #include <linux/bug.h>
  21. #include <linux/delay.h>
  22. #include <linux/types.h>
  23. #include <linux/printk.h>
  24. #include <linux/string.h>
  25. #include <linux/stringify.h>
  26. #include <asm/ptrace.h>
  27. #include <stdarg.h>
  28. #include <stdio.h>
  29. #include <linux/kernel.h>
  30. #include <part.h>
  31. #include <flash.h>
  32. #include <image.h>
  33. #ifdef __LP64__
  34. #define CONFIG_SYS_SUPPORT_64BIT_DATA
  35. #endif
  36. #include <log.h>
  37. typedef void (interrupt_handler_t)(void *);
  38. #include <asm/u-boot.h> /* boot information for Linux kernel */
  39. #include <asm/global_data.h> /* global data used for startup functions */
  40. #if defined(CONFIG_ENV_IS_EMBEDDED)
  41. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  42. #elif ( ((CONFIG_ENV_ADDR+CONFIG_ENV_SIZE) < CONFIG_SYS_MONITOR_BASE) || \
  43. (CONFIG_ENV_ADDR >= (CONFIG_SYS_MONITOR_BASE + CONFIG_SYS_MONITOR_LEN)) ) || \
  44. defined(CONFIG_ENV_IS_IN_NVRAM)
  45. #define TOTAL_MALLOC_LEN (CONFIG_SYS_MALLOC_LEN + CONFIG_ENV_SIZE)
  46. #else
  47. #define TOTAL_MALLOC_LEN CONFIG_SYS_MALLOC_LEN
  48. #endif
  49. /* startup functions, used in:
  50. * common/board_f.c
  51. * common/init/board_init.c
  52. * common/board_r.c
  53. * common/board_info.c
  54. */
  55. #include <init.h>
  56. /*
  57. * Function Prototypes
  58. */
  59. void hang (void) __attribute__ ((noreturn));
  60. int cpu_init(void);
  61. #include <display_options.h>
  62. /* common/main.c */
  63. void main_loop (void);
  64. int run_command(const char *cmd, int flag);
  65. int run_command_repeatable(const char *cmd, int flag);
  66. /**
  67. * Run a list of commands separated by ; or even \0
  68. *
  69. * Note that if 'len' is not -1, then the command does not need to be nul
  70. * terminated, Memory will be allocated for the command in that case.
  71. *
  72. * @param cmd List of commands to run, each separated bu semicolon
  73. * @param len Length of commands excluding terminator if known (-1 if not)
  74. * @param flag Execution flags (CMD_FLAG_...)
  75. * @return 0 on success, or != 0 on error.
  76. */
  77. int run_command_list(const char *cmd, int len, int flag);
  78. int checkflash(void);
  79. int checkdram(void);
  80. extern u8 __dtb_dt_begin[]; /* embedded device tree blob */
  81. extern u8 __dtb_dt_spl_begin[]; /* embedded device tree blob for SPL/TPL */
  82. int mdm_init(void);
  83. /**
  84. * Show the DRAM size in a board-specific way
  85. *
  86. * This is used by boards to display DRAM information in their own way.
  87. *
  88. * @param size Size of DRAM (which should be displayed along with other info)
  89. */
  90. void board_show_dram(phys_size_t size);
  91. /**
  92. * arch_fixup_fdt() - Write arch-specific information to fdt
  93. *
  94. * Defined in arch/$(ARCH)/lib/bootm-fdt.c
  95. *
  96. * @blob: FDT blob to write to
  97. * @return 0 if ok, or -ve FDT_ERR_... on failure
  98. */
  99. int arch_fixup_fdt(void *blob);
  100. /* common/flash.c */
  101. void flash_perror (int);
  102. /* common/cmd_source.c */
  103. int source (ulong addr, const char *fit_uname);
  104. extern ulong load_addr; /* Default Load Address */
  105. extern ulong save_addr; /* Default Save Address */
  106. extern ulong save_size; /* Default Save Size */
  107. /* common/cmd_net.c */
  108. int do_tftpb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  109. /* common/cmd_fat.c */
  110. int do_fat_fsload(cmd_tbl_t *, int, int, char * const []);
  111. /* common/cmd_ext2.c */
  112. int do_ext2load(cmd_tbl_t *, int, int, char * const []);
  113. /* common/cmd_nvedit.c */
  114. int env_init (void);
  115. void env_relocate (void);
  116. int envmatch (uchar *, int);
  117. /**
  118. * env_get() - Look up the value of an environment variable
  119. *
  120. * In U-Boot proper this can be called before relocation (which is when the
  121. * environment is loaded from storage, i.e. GD_FLG_ENV_READY is 0). In that
  122. * case this function calls env_get_f().
  123. *
  124. * @varname: Variable to look up
  125. * @return value of variable, or NULL if not found
  126. */
  127. char *env_get(const char *varname);
  128. /**
  129. * env_get_f() - Look up the value of an environment variable (early)
  130. *
  131. * This function is called from env_get() if the environment has not been
  132. * loaded yet (GD_FLG_ENV_READY flag is 0). Some environment locations will
  133. * support reading the value (slowly) and some will not.
  134. *
  135. * @varname: Variable to look up
  136. * @return value of variable, or NULL if not found
  137. */
  138. int env_get_f(const char *name, char *buf, unsigned len);
  139. /**
  140. * env_get_ulong() - Return an environment variable as an integer value
  141. *
  142. * Most U-Boot environment variables store hex values. For those which store
  143. * (e.g.) base-10 integers, this function can be used to read the value.
  144. *
  145. * @name: Variable to look up
  146. * @base: Base to use (e.g. 10 for base 10, 2 for binary)
  147. * @default_val: Default value to return if no value is found
  148. * @return the value found, or @default_val if none
  149. */
  150. ulong env_get_ulong(const char *name, int base, ulong default_val);
  151. /**
  152. * env_get_hex() - Return an environment variable as a hex value
  153. *
  154. * Decode an environment as a hex number (it may or may not have a 0x
  155. * prefix). If the environment variable cannot be found, or does not start
  156. * with hex digits, the default value is returned.
  157. *
  158. * @varname: Variable to decode
  159. * @default_val: Value to return on error
  160. */
  161. ulong env_get_hex(const char *varname, ulong default_val);
  162. /*
  163. * Read an environment variable as a boolean
  164. * Return -1 if variable does not exist (default to true)
  165. */
  166. int env_get_yesno(const char *var);
  167. /**
  168. * env_set() - set an environment variable
  169. *
  170. * This sets or deletes the value of an environment variable. For setting the
  171. * value the variable is created if it does not already exist.
  172. *
  173. * @varname: Variable to adjust
  174. * @value: Value to set for the variable, or NULL or "" to delete the variable
  175. * @return 0 if OK, 1 on error
  176. */
  177. int env_set(const char *varname, const char *value);
  178. /**
  179. * env_set_ulong() - set an environment variable to an integer
  180. *
  181. * @varname: Variable to adjust
  182. * @value: Value to set for the variable (will be converted to a string)
  183. * @return 0 if OK, 1 on error
  184. */
  185. int env_set_ulong(const char *varname, ulong value);
  186. /**
  187. * env_set_hex() - set an environment variable to a hex value
  188. *
  189. * @varname: Variable to adjust
  190. * @value: Value to set for the variable (will be converted to a hex string)
  191. * @return 0 if OK, 1 on error
  192. */
  193. int env_set_hex(const char *varname, ulong value);
  194. /**
  195. * env_set_addr - Set an environment variable to an address in hex
  196. *
  197. * @varname: Environment variable to set
  198. * @addr: Value to set it to
  199. * @return 0 if ok, 1 on error
  200. */
  201. static inline int env_set_addr(const char *varname, const void *addr)
  202. {
  203. return env_set_hex(varname, (ulong)addr);
  204. }
  205. #ifdef CONFIG_AUTO_COMPLETE
  206. int env_complete(char *var, int maxv, char *cmdv[], int maxsz, char *buf);
  207. #endif
  208. int get_env_id (void);
  209. void pci_init_board(void);
  210. /* common/exports.c */
  211. void jumptable_init(void);
  212. /* common/kallsysm.c */
  213. const char *symbol_lookup(unsigned long addr, unsigned long *caddr);
  214. /* common/memsize.c */
  215. long get_ram_size (long *, long);
  216. phys_size_t get_effective_memsize(void);
  217. /* $(BOARD)/$(BOARD).c */
  218. void reset_phy (void);
  219. void fdc_hw_init (void);
  220. /* $(BOARD)/eeprom.c */
  221. #ifdef CONFIG_CMD_EEPROM
  222. void eeprom_init (int bus);
  223. int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt);
  224. int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt);
  225. #else
  226. /*
  227. * Some EEPROM code is depecated because it used the legacy I2C interface. Add
  228. * some macros here so we don't have to touch every one of those uses
  229. */
  230. #define eeprom_init(bus)
  231. #define eeprom_read(dev_addr, offset, buffer, cnt) ((void)-ENOSYS)
  232. #define eeprom_write(dev_addr, offset, buffer, cnt) ((void)-ENOSYS)
  233. #endif
  234. #if !defined(CONFIG_ENV_EEPROM_IS_ON_I2C) && defined(CONFIG_SYS_I2C_EEPROM_ADDR)
  235. # define CONFIG_SYS_DEF_EEPROM_ADDR CONFIG_SYS_I2C_EEPROM_ADDR
  236. #endif
  237. #if defined(CONFIG_MPC8XX_SPI)
  238. extern void spi_init_f (void);
  239. extern void spi_init_r (void);
  240. extern ssize_t spi_read (uchar *, int, uchar *, int);
  241. extern ssize_t spi_write (uchar *, int, uchar *, int);
  242. #endif
  243. /* $(BOARD)/$(BOARD).c */
  244. int board_early_init_f (void);
  245. int board_fix_fdt (void *rw_fdt_blob); /* manipulate the U-Boot fdt before its relocation */
  246. int board_late_init (void);
  247. int board_postclk_init (void); /* after clocks/timebase, before env/serial */
  248. int board_early_init_r (void);
  249. #if defined(CONFIG_SYS_DRAM_TEST)
  250. int testdram(void);
  251. #endif /* CONFIG_SYS_DRAM_TEST */
  252. /* $(CPU)/start.S */
  253. int icache_status (void);
  254. void icache_enable (void);
  255. void icache_disable(void);
  256. int dcache_status (void);
  257. void dcache_enable (void);
  258. void dcache_disable(void);
  259. void mmu_disable(void);
  260. #if defined(CONFIG_ARM)
  261. void relocate_code(ulong);
  262. #else
  263. void relocate_code(ulong, gd_t *, ulong) __attribute__ ((noreturn));
  264. #endif
  265. ulong get_endaddr (void);
  266. void trap_init (ulong);
  267. /* $(CPU)/cpu.c */
  268. static inline int cpumask_next(int cpu, unsigned int mask)
  269. {
  270. for (cpu++; !((1 << cpu) & mask); cpu++)
  271. ;
  272. return cpu;
  273. }
  274. #define for_each_cpu(iter, cpu, num_cpus, mask) \
  275. for (iter = 0, cpu = cpumask_next(-1, mask); \
  276. iter < num_cpus; \
  277. iter++, cpu = cpumask_next(cpu, mask)) \
  278. int cpu_numcores (void);
  279. int cpu_num_dspcores(void);
  280. u32 cpu_mask (void);
  281. u32 cpu_dsp_mask(void);
  282. int is_core_valid (unsigned int);
  283. void s_init(void);
  284. int checkcpu (void);
  285. int checkicache (void);
  286. int checkdcache (void);
  287. void upmconfig (unsigned int, unsigned int *, unsigned int);
  288. ulong get_tbclk (void);
  289. void reset_misc (void);
  290. void reset_cpu (ulong addr);
  291. void ft_cpu_setup(void *blob, bd_t *bd);
  292. void ft_pci_setup(void *blob, bd_t *bd);
  293. void smp_set_core_boot_addr(unsigned long addr, int corenr);
  294. void smp_kick_all_cpus(void);
  295. /* $(CPU)/serial.c */
  296. int serial_init (void);
  297. void serial_setbrg (void);
  298. void serial_putc (const char);
  299. void serial_putc_raw(const char);
  300. void serial_puts (const char *);
  301. int serial_getc (void);
  302. int serial_tstc (void);
  303. int serial_setconfig(uint config);
  304. /* $(CPU)/speed.c */
  305. int get_clocks (void);
  306. ulong get_bus_freq (ulong);
  307. int get_serial_clock(void);
  308. /* $(CPU)/interrupts.c */
  309. int interrupt_init (void);
  310. void timer_interrupt (struct pt_regs *);
  311. void external_interrupt (struct pt_regs *);
  312. void irq_install_handler(int, interrupt_handler_t *, void *);
  313. void irq_free_handler (int);
  314. void reset_timer (void);
  315. /* Return value of monotonic microsecond timer */
  316. unsigned long timer_get_us(void);
  317. void enable_interrupts (void);
  318. int disable_interrupts (void);
  319. /* $(CPU)/.../commproc.c */
  320. int dpram_init (void);
  321. uint dpram_base(void);
  322. uint dpram_base_align(uint align);
  323. uint dpram_alloc(uint size);
  324. uint dpram_alloc_align(uint size,uint align);
  325. void bootcount_store (ulong);
  326. ulong bootcount_load (void);
  327. #define BOOTCOUNT_MAGIC 0xB001C041
  328. /* $(CPU)/.../<eth> */
  329. void mii_init (void);
  330. /* $(CPU)/.../lcd.c */
  331. ulong lcd_setmem (ulong);
  332. /* $(CPU)/.../video.c */
  333. ulong video_setmem (ulong);
  334. /* arch/$(ARCH)/lib/cache.c */
  335. void enable_caches(void);
  336. void flush_cache (unsigned long, unsigned long);
  337. void flush_dcache_all(void);
  338. void flush_dcache_range(unsigned long start, unsigned long stop);
  339. void invalidate_dcache_range(unsigned long start, unsigned long stop);
  340. void invalidate_dcache_all(void);
  341. void invalidate_icache_all(void);
  342. enum {
  343. /* Disable caches (else flush caches but leave them active) */
  344. CBL_DISABLE_CACHES = 1 << 0,
  345. CBL_SHOW_BOOTSTAGE_REPORT = 1 << 1,
  346. CBL_ALL = 3,
  347. };
  348. /**
  349. * Clean up ready for linux
  350. *
  351. * @param flags Flags to control what is done
  352. */
  353. int cleanup_before_linux_select(int flags);
  354. /* arch/$(ARCH)/lib/ticks.S */
  355. uint64_t get_ticks(void);
  356. void wait_ticks (unsigned long);
  357. /* arch/$(ARCH)/lib/time.c */
  358. ulong usec2ticks (unsigned long usec);
  359. ulong ticks2usec (unsigned long ticks);
  360. /* lib/gunzip.c */
  361. int gzip_parse_header(const unsigned char *src, unsigned long len);
  362. int gunzip(void *, int, unsigned char *, unsigned long *);
  363. int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
  364. int stoponerr, int offset);
  365. /**
  366. * gzwrite progress indicators: defined weak to allow board-specific
  367. * overrides:
  368. *
  369. * gzwrite_progress_init called on startup
  370. * gzwrite_progress called during decompress/write loop
  371. * gzwrite_progress_finish called at end of loop to
  372. * indicate success (retcode=0) or failure
  373. */
  374. void gzwrite_progress_init(u64 expected_size);
  375. void gzwrite_progress(int iteration,
  376. u64 bytes_written,
  377. u64 total_bytes);
  378. void gzwrite_progress_finish(int retcode,
  379. u64 totalwritten,
  380. u64 totalsize,
  381. u32 expected_crc,
  382. u32 calculated_crc);
  383. /**
  384. * decompress and write gzipped image from memory to block device
  385. *
  386. * @param src compressed image address
  387. * @param len compressed image length in bytes
  388. * @param dev block device descriptor
  389. * @param szwritebuf bytes per write (pad to erase size)
  390. * @param startoffs offset in bytes of first write
  391. * @param szexpected expected uncompressed length
  392. * may be zero to use gzip trailer
  393. * for files under 4GiB
  394. */
  395. int gzwrite(unsigned char *src, int len,
  396. struct blk_desc *dev,
  397. unsigned long szwritebuf,
  398. u64 startoffs,
  399. u64 szexpected);
  400. /* lib/lz4_wrapper.c */
  401. int ulz4fn(const void *src, size_t srcn, void *dst, size_t *dstn);
  402. /* lib/qsort.c */
  403. void qsort(void *base, size_t nmemb, size_t size,
  404. int(*compar)(const void *, const void *));
  405. int strcmp_compar(const void *, const void *);
  406. /* lib/uuid.c */
  407. #include <uuid.h>
  408. /* lib/vsprintf.c */
  409. #include <vsprintf.h>
  410. /* lib/strmhz.c */
  411. char * strmhz(char *buf, unsigned long hz);
  412. /* lib/crc32.c */
  413. #include <u-boot/crc.h>
  414. /* lib/rand.c */
  415. #define RAND_MAX -1U
  416. void srand(unsigned int seed);
  417. unsigned int rand(void);
  418. unsigned int rand_r(unsigned int *seedp);
  419. /*
  420. * STDIO based functions (can always be used)
  421. */
  422. /* serial stuff */
  423. int serial_printf (const char *fmt, ...)
  424. __attribute__ ((format (__printf__, 1, 2)));
  425. /* lib/gzip.c */
  426. int gzip(void *dst, unsigned long *lenp,
  427. unsigned char *src, unsigned long srclen);
  428. int zzip(void *dst, unsigned long *lenp, unsigned char *src,
  429. unsigned long srclen, int stoponerr,
  430. int (*func)(unsigned long, unsigned long));
  431. /* lib/net_utils.c */
  432. #include <net.h>
  433. static inline struct in_addr env_get_ip(char *var)
  434. {
  435. return string_to_ip(env_get(var));
  436. }
  437. int pcmcia_init (void);
  438. #ifdef CONFIG_LED_STATUS
  439. # include <status_led.h>
  440. #endif
  441. #include <bootstage.h>
  442. #ifdef CONFIG_SHOW_ACTIVITY
  443. void show_activity(int arg);
  444. #endif
  445. /* Multicore arch functions */
  446. #ifdef CONFIG_MP
  447. int cpu_status(u32 nr);
  448. int cpu_reset(u32 nr);
  449. int cpu_disable(u32 nr);
  450. int cpu_release(u32 nr, int argc, char * const argv[]);
  451. #endif
  452. #else /* __ASSEMBLY__ */
  453. /* Drop a C type modifier (like in 3UL) for constants used in assembly. */
  454. #define _AC(X, Y) X
  455. #endif /* __ASSEMBLY__ */
  456. /* Put only stuff here that the assembler can digest */
  457. /* Declare an unsigned long constant digestable both by C and an assembler. */
  458. #define UL(x) _AC(x, UL)
  459. #ifdef CONFIG_POST
  460. #define CONFIG_HAS_POST
  461. #ifndef CONFIG_POST_ALT_LIST
  462. #define CONFIG_POST_STD_LIST
  463. #endif
  464. #endif
  465. #ifdef CONFIG_INIT_CRITICAL
  466. #error CONFIG_INIT_CRITICAL is deprecated!
  467. #error Read section CONFIG_SKIP_LOWLEVEL_INIT in README.
  468. #endif
  469. #define ROUND(a,b) (((a) + (b) - 1) & ~((b) - 1))
  470. /*
  471. * check_member() - Check the offset of a structure member
  472. *
  473. * @structure: Name of structure (e.g. global_data)
  474. * @member: Name of member (e.g. baudrate)
  475. * @offset: Expected offset in bytes
  476. */
  477. #define check_member(structure, member, offset) _Static_assert( \
  478. offsetof(struct structure, member) == offset, \
  479. "`struct " #structure "` offset for `" #member "` is not " #offset)
  480. /* Avoid using CONFIG_EFI_STUB directly as we may boot from other loaders */
  481. #ifdef CONFIG_EFI_STUB
  482. #define ll_boot_init() false
  483. #else
  484. #define ll_boot_init() true
  485. #endif
  486. /* Pull in stuff for the build system */
  487. #ifdef DO_DEPS_ONLY
  488. # include <environment.h>
  489. #endif
  490. #endif /* __COMMON_H_ */