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