1f6220f1aSBin Meng /* 2bfa95c53SBin Meng * Copyright (C) 2014 Google, Inc 3ed800961SBin Meng * Copyright (C) 2015 Bin Meng <bmeng.cn@gmail.com> 4f6220f1aSBin Meng * 5f6220f1aSBin Meng * SPDX-License-Identifier: GPL-2.0+ 6f6220f1aSBin Meng */ 7f6220f1aSBin Meng 8bfa95c53SBin Meng #ifndef _ASM_MRCCACHE_H 9bfa95c53SBin Meng #define _ASM_MRCCACHE_H 10f6220f1aSBin Meng 11f6220f1aSBin Meng #define MRC_DATA_ALIGN 0x1000 12bfa95c53SBin Meng #define MRC_DATA_SIGNATURE (('M' << 0) | ('R' << 8) | \ 13bfa95c53SBin Meng ('C' << 16) | ('D'<<24)) 14f6220f1aSBin Meng 15ed800961SBin Meng #define MRC_DATA_HEADER_SIZE 32 16ed800961SBin Meng 17bfa95c53SBin Meng struct __packed mrc_data_container { 18f6220f1aSBin Meng u32 signature; /* "MRCD" */ 19f6220f1aSBin Meng u32 data_size; /* Size of the 'data' field */ 20f6220f1aSBin Meng u32 checksum; /* IP style checksum */ 21f6220f1aSBin Meng u32 reserved; /* For header alignment */ 22f6220f1aSBin Meng u8 data[0]; /* Variable size, platform/run time dependent */ 23f6220f1aSBin Meng }; 24f6220f1aSBin Meng 25*4b9f6a66SBin Meng struct mrc_region { 26*4b9f6a66SBin Meng u32 base; 27*4b9f6a66SBin Meng u32 offset; 28*4b9f6a66SBin Meng u32 length; 29*4b9f6a66SBin Meng }; 30*4b9f6a66SBin Meng 31f6220f1aSBin Meng struct udevice; 32f6220f1aSBin Meng 33f6220f1aSBin Meng /** 34f6220f1aSBin Meng * mrccache_find_current() - find the latest MRC cache record 35f6220f1aSBin Meng * 36f6220f1aSBin Meng * This searches the MRC cache region looking for the latest record to use 37f6220f1aSBin Meng * for setting up SDRAM 38f6220f1aSBin Meng * 39bfa95c53SBin Meng * @entry: Position and size of MRC cache in SPI flash 40f6220f1aSBin Meng * @return pointer to latest record, or NULL if none 41f6220f1aSBin Meng */ 42*4b9f6a66SBin Meng struct mrc_data_container *mrccache_find_current(struct mrc_region *entry); 43f6220f1aSBin Meng 44f6220f1aSBin Meng /** 45f6220f1aSBin Meng * mrccache_update() - update the MRC cache with a new record 46f6220f1aSBin Meng * 47bfa95c53SBin Meng * This writes a new record to the end of the MRC cache region. If the new 48bfa95c53SBin Meng * record is the same as the latest record then the write is skipped 49f6220f1aSBin Meng * 50f6220f1aSBin Meng * @sf: SPI flash to write to 51f6220f1aSBin Meng * @entry: Position and size of MRC cache in SPI flash 52f6220f1aSBin Meng * @cur: Record to write 53f6220f1aSBin Meng * @return 0 if updated, -EEXIST if the record is the same as the latest 542fe66dbcSBin Meng * record, -EINVAL if the record is not valid, other error if SPI write failed 55f6220f1aSBin Meng */ 56*4b9f6a66SBin Meng int mrccache_update(struct udevice *sf, struct mrc_region *entry, 57f6220f1aSBin Meng struct mrc_data_container *cur); 58f6220f1aSBin Meng 59ed800961SBin Meng /** 60ed800961SBin Meng * mrccache_reserve() - reserve MRC data on the stack 61ed800961SBin Meng * 62ed800961SBin Meng * This copies MRC data pointed by gd->arch.mrc_output to a new place on the 63ed800961SBin Meng * stack with length gd->arch.mrc_output_len, and updates gd->arch.mrc_output 64ed800961SBin Meng * to point to the new place once the migration is done. 65ed800961SBin Meng * 66ed800961SBin Meng * This routine should be called by reserve_arch() before U-Boot is relocated 67ed800961SBin Meng * when MRC cache is enabled. 68ed800961SBin Meng * 69ed800961SBin Meng * @return 0 always 70ed800961SBin Meng */ 71ed800961SBin Meng int mrccache_reserve(void); 72ed800961SBin Meng 73ed800961SBin Meng /** 74ed800961SBin Meng * mrccache_get_region() - get MRC region on the SPI flash 75ed800961SBin Meng * 76ed800961SBin Meng * This gets MRC region whose offset and size are described in the device tree 77ed800961SBin Meng * as a subnode to the SPI flash. If a non-NULL device pointer is supplied, 78ed800961SBin Meng * this also probes the SPI flash device and returns its device pointer for 79ed800961SBin Meng * the caller to use later. 80ed800961SBin Meng * 81ed800961SBin Meng * Be careful when calling this routine with a non-NULL device pointer: 82ed800961SBin Meng * - driver model initialization must be complete 83ed800961SBin Meng * - calling in the pre-relocation phase may bring some side effects during 84ed800961SBin Meng * the SPI flash device probe (eg: for SPI controllers on a PCI bus, it 85ed800961SBin Meng * triggers PCI bus enumeration during which insufficient memory issue 86ed800961SBin Meng * might be exposed and it causes subsequent SPI flash probe fails). 87ed800961SBin Meng * 88ed800961SBin Meng * @devp: Returns pointer to the SPI flash device 89ed800961SBin Meng * @entry: Position and size of MRC cache in SPI flash 90ed800961SBin Meng * @return 0 if success, -ENOENT if SPI flash node does not exist in the 91ed800961SBin Meng * device tree, -EPERM if MRC region subnode does not exist in the device 92ed800961SBin Meng * tree, -EINVAL if MRC region properties format is incorrect, other error 93ed800961SBin Meng * if SPI flash probe failed. 94ed800961SBin Meng */ 95*4b9f6a66SBin Meng int mrccache_get_region(struct udevice **devp, struct mrc_region *entry); 96ed800961SBin Meng 97ed800961SBin Meng /** 98ed800961SBin Meng * mrccache_save() - save MRC data to the SPI flash 99ed800961SBin Meng * 100ed800961SBin Meng * This saves MRC data stored previously by gd->arch.mrc_output to a proper 101ed800961SBin Meng * place within the MRC region on the SPI flash. 102ed800961SBin Meng * 103ed800961SBin Meng * @return 0 if saved to SPI flash successfully, other error if failed 104ed800961SBin Meng */ 105ed800961SBin Meng int mrccache_save(void); 106ed800961SBin Meng 107bfa95c53SBin Meng #endif /* _ASM_MRCCACHE_H */ 108