1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2014 Google, Inc
4  * Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com>
5  */
6 
7 #ifndef _ASM_MRCCACHE_H
8 #define _ASM_MRCCACHE_H
9 
10 #include <compiler.h>
11 
12 #define MRC_DATA_ALIGN		0x100
13 #define MRC_DATA_SIGNATURE	(('M' << 0) | ('R' << 8) | \
14 				 ('C' << 16) | ('D'<<24))
15 
16 #define MRC_DATA_HEADER_SIZE	32
17 
18 struct __packed mrc_data_container {
19 	u32	signature;	/* "MRCD" */
20 	u32	data_size;	/* Size of the 'data' field */
21 	u32	checksum;	/* IP style checksum */
22 	u32	reserved;	/* For header alignment */
23 	u8	data[0];	/* Variable size, platform/run time dependent */
24 };
25 
26 struct mrc_region {
27 	u32	base;
28 	u32	offset;
29 	u32	length;
30 };
31 
32 /* Types of MRC data */
33 enum mrc_type_t {
34 	MRC_TYPE_NORMAL,
35 	MRC_TYPE_VAR,
36 
37 	MRC_TYPE_COUNT,
38 };
39 
40 struct udevice;
41 
42 /**
43  * mrccache_find_current() - find the latest MRC cache record
44  *
45  * This searches the MRC cache region looking for the latest record to use
46  * for setting up SDRAM
47  *
48  * @entry:	Position and size of MRC cache in SPI flash
49  * @return pointer to latest record, or NULL if none
50  */
51 struct mrc_data_container *mrccache_find_current(struct mrc_region *entry);
52 
53 /**
54  * mrccache_reserve() - reserve MRC data on the stack
55  *
56  * This copies MRC data pointed by gd->arch.mrc_output to a new place on the
57  * stack with length gd->arch.mrc_output_len, and updates gd->arch.mrc_output
58  * to point to the new place once the migration is done.
59  *
60  * This routine should be called by reserve_arch() before U-Boot is relocated
61  * when MRC cache is enabled.
62  *
63  * @return 0 always
64  */
65 int mrccache_reserve(void);
66 
67 /**
68  * mrccache_get_region() - get MRC region on the SPI flash
69  *
70  * This gets MRC region whose offset and size are described in the device tree
71  * as a subnode to the SPI flash. This tries to find the SPI flash device
72  * (without probing it), falling back to looking for the devicetree node if
73  * driver model is not inited or the SPI flash is not found.
74  *
75  * @type:	Type of MRC data to use
76  * @devp:	Returns pointer to the SPI flash device, if found
77  * @entry:	Position and size of MRC cache in SPI flash
78  * @return 0 if success, -ENOENT if SPI flash node does not exist in the
79  * device tree, -EPERM if MRC region subnode does not exist in the device
80  * tree, -EINVAL if MRC region properties format is incorrect, other error
81  * if SPI flash probe failed.
82  */
83 int mrccache_get_region(enum mrc_type_t type, struct udevice **devp,
84 			struct mrc_region *entry);
85 
86 /**
87  * mrccache_save() - save MRC data to the SPI flash
88  *
89  * This saves MRC data stored previously by gd->arch.mrc_output to a proper
90  * place within the MRC region on the SPI flash.
91  *
92  * @return 0 if saved to SPI flash successfully, other error if failed
93  */
94 int mrccache_save(void);
95 
96 /**
97  * mrccache_spl_save() - Save to the MRC region from SPL
98  *
99  * When SPL is used to set up the memory controller we want to save the MRC
100  * data in SPL to avoid needing to pass it up to U-Boot proper to save. This
101  * function handles that.
102  *
103  * @return 0 if saved to SPI flash successfully, other error if failed
104  */
105 int mrccache_spl_save(void);
106 
107 #endif /* _ASM_MRCCACHE_H */
108