存储介质开发
存储介质开发是围绕nor/nand物料支持的功能开发,常见的功能有锁保护、OTP、UUID等。除此之外,还包括新物料的适配。
OTP:“One-Time-Programmable”一次性可编程,指的是一块特殊的存储区域,这块区域一旦被写入数据并锁定,就无法被擦除或修改。常用来存储加密密钥、设备校准参数等关键数据
UUID:是指芯片在出厂时,由制造商预先烧录并固化在只读区域的一个唯一序列号,是芯片的“身份证”,常用于云端注册、设备绑定、授权管理等
锁保护:一般spi nor flash都有支持,支持独立块保护、区域保护
1. SPI NOR
1.1 NOR Flash锁保护开发
独立块锁保护
宏CONFIG_DRIVERS_SPINOR_WRITE_LOCK 用于配置NOR Flash独立块锁保护,如该款物料支持独立块锁保护,请按如下步骤进行开发。
- 在芯片对应的struct nor_factory结构体,实现对应的lock和unlock功能函数即可。
struct nor_factory {
unsigned char factory;
unsigned int idt_cnt;
struct nor_info *idt;
/* ... */
int (*lock)(struct nor_flash *nor, unsigned int addr, unsigned int len);
int (*unlock)(struct nor_flash *nor, unsigned int addr, unsigned int len);
bool (*islock)(struct nor_flash *nor, unsigned int addr, unsigned int len);
struct nor_factory *next;
};
1.2 uuid
lichee/melis/source/ekernel/components/thirdparty/finsh_cli/commands/cmd_flash_uuid.c
lichee/melis/source/ekernel/drivers/drv/source/spinor/spinor_drv.c
lichee/melis/source/ekernel/drivers/include/drv/blkpart.h
lichee/rtos-hal/hal/source/spinor/core.c
lichee/rtos-hal/hal/source/spinor/factory_puya.c
lichee/rtos-hal/hal/source/spinor/hal_spinor.c
1.2.1 配置
- 1、打开 uuid 对应的测试命令配置,执行:make menuconfig
Kernel Setup --->
Components Support --->
Thirdparty Components Support --->
Command shell --->
Commands --->
[*] flash Command for uuid
-
2、确认当前款flash是否支持 uuid:以 py25q128ha 这款flash为例
- 查看当前 flash 的 datasheet,是否支持 uuid
- 这款flash对应的 id配置文件为 lichee/rtos-hal/hal/source/spinor/factory_puya.c,查看是否支持对应的 uuid 接口
-
3、烧录带有 uuid 命令的固件,正常启动后 执行命令 uuid ,则会打印出当前 flash 的 uuid
1.2.2 实现流程
- cmd_uuid() cmd_flash_uuid.c //执行命令 uuid 时,会进到这里
- hal_spinor_read_uuid() hal_spinor.c //通过设备节点 /dev/spinor 和 cmd BLOCK_DEVICE_CMD_GET_UUID ,通过 spinor_drv.c 的 ioctl 会进到这里
- nor_read_uuid() core.c //会根据当前flash的厂商型号去调用对应的 flash配置文件(如factory_puya.c)中的 uuid读取接口
- nor_puya_read_uuid() factory_puya.c //一般情况下,norflash的 uuid 长度为16个字节,读取uuid的指令为0x4B,访问的地址是 24bit长度的任意地址(可直接设置为全0x00)。但仍需按照实际进行配置,如 EN25QH128A 这款flash,uuid 长度为12个字节,读取uuid的指令为0x5A,访问的地址是 24bit长度的0x000080
1.2.3 测试
msh />uuid
the flash uuid is
0x00 0x41 0x50 0x33 0x59 0x32 0x33 0x33 0x01 0x00 0x2e 0x07 0x29 0x01 0x23 0x09
1.3 otp
lichee/melis/source/ekernel/components/thirdparty/finsh_cli/commands/cmd_flash_otp.c
lichee/melis/source/ekernel/drivers/drv/source/spinor/spinor_drv.c
lichee/melis/source/ekernel/drivers/include/drv/blkpart.h
lichee/rtos-hal/hal/source/spinor/core.c
lichee/rtos-hal/hal/source/spinor/factory_puya.c
lichee/rtos-hal/hal/source/spinor/hal_spinor.c
1.3.1 配置
- 1、打开 otp 对应的测试命令配置,执行:make menuconfig
Kernel Setup --->
Components Support --->
Thirdparty Components Support --->
Command shell --->
Commands --->
[*] flash Command for otp
-
2、确认当前款flash是否支持 otp:以 py25q128ha 这款flash为例
- 查看当前 flash 的 datasheet,是否支持 otp
- 这款flash对应的 id配置文件为 lichee/rtos-hal/hal/source/spinor/factory_puya.c,查看对应的具体flash型号是否有配置 otp的flag,如:
{
.model = "p25q128ha",
.id = {0x85, 0x20, 0x18},
.total_size = SZ_16M,
.flag = SUPPORT_GENERAL,
OTP_INFO(1024, 3, 0x1000, 0x1000)
},
-
这里的 otp 配置,表示:此款flash有 3块otp区域,每块otp为1024字节长度,第0块otp区域的起始偏移为 0x1000,每块otp区域的偏移为0x1000,如 datasheet 所示:

-
3、确定好要对flash的哪块 otp区域 进行读写
- 注意:由于 otp区域是一次性可编程,可以多次读,但写完后 就无法再写,因此在写之前,务必确认好 写入的内容。可参考 cmd_flash_otp.c 测试用例
- otp_num:配置要对 哪块otp区域 进行读写
- otp_len:配置要对 某块otp区域 进行多长数据的读写
- 注意:由于 otp区域是一次性可编程,可以多次读,但写完后 就无法再写,因此在写之前,务必确认好 写入的内容。可参考 cmd_flash_otp.c 测试用例
-
4、烧录带有 otp 命令的固件,正常启动后 执行命令 otp ,则会按照 cmd_flash_otp.c 测试用例进行相关操作
1.3.2 实现流程
- cmd_otp() cmd_flash_otp.c //执行命令 otp 时,会进到这里
- hal_spinor_read_otp() hal_spinor.c //通过设备节点 /dev/spinor 和 cmd BLOCK_DEVICE_CMD_READ_OTP ,通过 spinor_drv.c 的 ioctl 会进到这里
- nor_otp_read() core.c
- nor_otp_read_write() //对指定的 otp区域写入或读取数据,且 单块otp区域数据可以多次读取,但只能写入一次,一旦写入数据 该块otp就会上锁
- hal_spinor_write_otp() hal_spinor.c //通过设备节点 /dev/spinor 和 cmd BLOCK_DEVICE_CMD_WRITE_OTP ,通过 spinor_drv.c 的 ioctl 会进到这里
- nor_otp_write() core.c
- nor_otp_read_write() //对指定的 otp区域写入或读取数据,且 单块otp区域数据可以多次读取,但只能写入一次,一旦写入数据 该块otp就会上锁
1.3.3 测试
//norflash:py25q128ha
//对单颗 norflash 进行第一次 otp操作
msh />otp
set OTP area 1 with 16 bytes default data: //设置buf的初值
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
read 16 bytes raw data from flash OTP area 1 success: //获取指定 otp 区域的数据,显示全为 0xff
0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff
set OTP area 1 with 16 bytes data: //将设定的数据写入指定 otp 区域,若写成功,则会将该指定 otp 区域进行上锁,后续无法再写入数据
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
write 16 bytes to OTP area 1 success
after write, re_read 16 bytes data from flash OTP area 1 success: //写入成功后重新读出指定 otp 区域的数据,显示和之前的 写入数据 相同
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
//对单颗 norflash 进行第二次 otp操作
msh />otp
set OTP area 1 with 16 bytes default data:
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
read 16 bytes raw data from flash OTP area 1 success: //读出指定 otp 区域的数据,显示和之前的 写入数据 相同
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
set OTP area 1 with 16 bytes data: //该指定 otp 区域由于第一次的 写操作 已被上锁,此时再进行 写操作 则会操作失败
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
[ERR]: [nor_otp_read_write:1467]: spinor: otp area 1 is already lock, not allow to write
/dev/spinor BLOCK_DEVICE_CMD_WRITE_OTP fail!
1.4 FAQ
USB烧录过程中GPT校验失败
如果出现下面的打印
GUID Partition Table Header signature is wrong: 0xFFFFFFFFFFFFFFFF != 0x5452415020494645

这里打印显示烧录完,把数据读出来校验分区表时,gpt头部数据不对,发现数据全是0xff,看上去是数据没有真正写进flash。要排查一下原因。 根据以往经验, 1.可能这flash不支持100M的频率,可以尝试把默认工作频率降低。参考下图修改

2.可能存在 flash 支持的功能配置有误,适当去掉一些不必要功能的配置。 3.flash被上INDIVIDUAL锁了,这个需要检查flash的寄存器确认,烧录的时候需要增加一下解锁操作。
2. SPI NAND
2.1 uuid
lichee/melis/source/ekernel/components/thirdparty/finsh_cli/commands/cmd_flash_uuid.c
lichee/melis/source/ekernel/drivers/drv/source/nand_flash/nandflash_drv.c
lichee/melis/source/ekernel/drivers/include/drv/blkpart.h
lichee/rtos-hal/hal/source/nand_flash/src/nand_phy.c
lichee/rtos-hal/hal/source/nand_flash/src/spic_op.c
2.1.1 配置
- 1、打开 uuid 对应的测试命令配置,执行:make menuconfig
Kernel Setup --->
Components Support --->
Thirdparty Components Support --->
Command shell --->
Commands --->
[*] flash Command for uuid
-
2、确认当前款flash是否支持 uuid:以 XT26G01CWSIGA 这款flash为例
- 查看当前 flash 的 datasheet,是否支持 uuid
- 这款flash对应的 uuid配置文件为 nand_phy.c,查看是否有支持上对应的 uuid接口
-
3、烧录带有 uuid 命令的固件,正常启动后 执行命令 uuid ,则会打印出当前 flash 的 uuid
2.1.2 实现流程
- cmd_uuid() cmd_flash_uuid.c //执行命令 uuid 时,会进到这里
- hal_nandflash_get_uuid() nandflash_drv.c //通过设备节点 /dev/nandflash 和 cmd BLOCK_DEVICE_CMD_GET_UUID ,通过 nandflash_drv.c 的 ioctl 会进到这里
- spi_nand_read_uuid() nand_phy.c
- spi_nand_read_uuid_do(0, 3, 16, uuid) spic_op.c //一般情况下,如 XT26G01CWSIGA 这款flash,nandflash的 uuid 长度为16个字节,读取uuid的指令为0x4B,访问的地址是 24bit长度的任意地址(可直接设置为全0x00)。但仍需按照实际进行配置,请查看flash的datasheet 进行确认
2.1.3 测试
msh />uuid
the flash uuid is
0x3e 0x53 0xe4 0x8b 0x7e 0x46 0x4f 0x0e 0x9a 0xde 0xb1 0x9f 0xb5 0x90 0x8b 0xfe
2.2 otp
lichee/melis/source/ekernel/components/thirdparty/finsh_cli/commands/cmd_flash_otp.c
lichee/melis/source/ekernel/drivers/drv/source/nand_flash/nandflash_drv.c
lichee/melis/source/ekernel/drivers/include/drv/blkpart.h
lichee/rtos-hal/hal/source/nand_flash/src/nand_phy.c
lichee/rtos-hal/hal/source/nand_flash/src/spic_op.c
2.2.1 配置
- 1、打开 otp 对应的测试命令配置,执行:make menuconfig
Kernel Setup --->
Components Support --->
Thirdparty Components Support --->
Command shell --->
Commands --->
[*] flash Command for otp
-
2、确认当前款flash是否支持 otp:以 XT26G01CWSIGA 这款flash为例
- 查看当前 flash 的 datasheet,是否支持 otp
- 这款flash对应的 otp配置文件为 nand_phy.c,查看是否有支持上对应的 otp接口
-
3、确定好要对flash的哪块 otp区域 进行读写
- 注意:由于 otp区域是一次性可编程,可以多次读,但写完后 就无法再写,因此在写之前,务必确认好 写入的内容。可参考 cmd_flash_otp.c 测试用例
- otp_num:配置要对 哪块otp区域 进行读写
- otp_len:配置要对 某块otp区域 进行多长数据的读写
- 注意:由于 otp区域是一次性可编程,可以多次读,但写完后 就无法再写,因此在写之前,务必确认好 写入的内容。可参考 cmd_flash_otp.c 测试用例
-
4、烧录带有 otp 命令的固件,正常启动后 执行命令 otp ,则会按照 cmd_flash_otp.c 测试用例进行相关操作
2.2.2 实现流程
- cmd_otp() cmd_flash_otp.c //执行命令 otp 时,会进到这里
- hal_nandflash_read_otp() nandflash_drv.c //通过设备节点 /dev/nandflash 和 cmd BLOCK_DEVICE_CMD_READ_OTP ,通过 nandflash_drv.c 的 ioctl 会进到这里
- spi_nand_read_otp_region() nand_phy.c
- spi_nand_read_otp_region_do(buf, len, 0, 3) spic_op.c //对指定的 otp区域读取数据,且 单块otp区域数据可以多次读取(otp_page:0~3)
- hal_nandflash_write_otp() nandflash_drv.c //通过设备节点 /dev/nandflash 和 cmd BLOCK_DEVICE_CMD_WRITE_OTP ,通过 nandflash_drv.c 的 ioctl 会进到这里
- spi_nand_write_otp_region() nand_phy.c
- spi_nand_write_otp_region_do(buf, len, 0, 3) //对指定的 otp区域读取数据,只能写入一次,一旦写入数据 该块otp就会上锁(otp_page:0~3)
2.2.3 测试
nandflash:XT26G01CWSIGA
//nandflash 的测试结果与 norflash 基本相同,仅在进行 第二次otp操作 时的log有些微差别,因此这里只附上对单颗 nandflash 进行第二次 otp操作 的测试log
msh />otp
set OTP area 1 with 16 bytes default data:
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
read 16 bytes raw data from flash OTP area 1 success:
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
set OTP area 1 with 16 bytes data: //该指定 otp 区域由于第一次的 写操作 已被上锁,此时再进行 写操作 则会操作失败
0x31 0x31 0x32 0x32 0x33 0x33 0x34 0x34 0x35 0x35 0x36 0x36 0x37 0x37 0x38 0x38
otp region is locked, cannot write!!
/dev/nandflash BLOCK_DEVICE_CMD_WRITE_OTP fail!
3. SDMMC Card
3.1 内核配置
执行make menuconfig,SDMMC Card使能配置如下:
# 选上SDMMC Card驱动
Kernel Setup --->
Drivers Setup --->
Melis Source Support --->
[*] Support SDMMC Driver # 使能SDMMC Card驱动
Kernel Setup --->
Drivers Setup --->
SoC HAL Drivers --->
SDMMC Devices --->
[*] enable SDMMC drivers
-*- enable SD
[*] enable SDIO
-*- enable mmc
[*] enable emmc
[*] enable detect card
[*] enable dma transmission
[*] enable sdio irq
[*] enable SD Card test case.
(64) SD Card Align DMA Buffer Size(Kbyte).
(0) sdc card detect pin present value
[*] support SDMMC filesystem
[ ] support optimize sdmmc rw speed
[ ] enable sdmmc debug
# SDMMC Card设备注册方式较为特殊,需打开以下配置
Kernel Setup --->
Drivers Setup --->
Melis Legacy Support --->
[*] Support Melis Legacy Driver Management #使能Melis以下系统的设备管理接口
[*] Support Melis Legacy DMS Device # 添加devfs虚拟设备
# 选上VFS支持,如需挂载fat或者ntfs等文件系统,请自行选上
Kernel Setup --->
File system support --->
[*] Support melis virtual file system #使能VFS
[*] Support devfs file system #使能对应的devfs
除此之外,提供了SDMMC Card的缓存功能,其实现了预读、读写相邻块合并、数据缓存等功能,配置项如下:
Kernel Setup --->
Drivers Setup --->
[*] support sdmmc cache # 使能SDMMC数据缓存
(1024) sdmmc cache size (KB) (NEW) #设置数据缓存大小
(14) sdmmc cache writeback thread priority (NEW)#缓存数据回刷任务的优先级
[ ] support sdmmc cache information command (NEW) # 数据缓存调试命令
3.2 物料适配
由于烧录时timing参数是uboot动态turning的,将timing参数保存到eMMC一个位置,启动时boot0、uboot会采用这组参数, kernel使用2x采样模式,硬件会自己来调整采样相位。 因此,eMMC物料适配一般不像nor/nand介质需要适配ID表,只需要确认sys_config文件sdc引脚、频率、mode配置正确。
以F102-MX-EVB1方案为例,eMMC接在sdc2,projects/f102-mx-cxx/evb1/configs/sys_config_card.fex内容如下 如果需要支持eMMC启动(sdc2),需要设置 used_card_no 为 0x04,如果也需要支持 sd卡,那就设置为 0x05
注:timing参数保存位置参考:mmc_write_info 函数(u-boot-2018/drivers/mmc/sunxi_mmc_tuning.c)
[sdcard_global]
used_card_no = 0x05
;used_card_no = 0x01, when use card0
;used_card_no = 0x02, when use card1
;used_card_no = 0x03, when use card0 & card1
internal_card = 0x04
;internal_card = 0x00, 无内置卡内置卡
;internal_card = 0x01, card0 做内置卡
;internal_card = 0x02, card1 做内置卡
[sdc0]
;sdc0_used = 1
bus-width = 4
sdc0_d1 = port:PF00<2><1><2><default>
sdc0_d0 = port:PF01<2><1><2><default>
sdc0_clk = port:PF02<2><1><2><default>
sdc0_cmd = port:PF03<2><1><2><default>
sdc0_d3 = port:PF04<2><1><2><default>
sdc0_d2 = port:PF05<2><1><2><default>
;cd-gpios = port:PF06<0><1><2><default>
;card-pwr-gpios = port:PL08<1><1><2><default>
;ctl-spec-caps = 0x4
;sunxi-power-save-mode =
;sunxi-dis-signal-vol-sw =
;vmmc="vcc-sdcv"
;vqmmc="vcc-sdcvq33"
;vdmmc="vcc-sdcvd"
;sd-uhs-sdr50 = 1
;sd-uhs-ddr50 = 1
;sd-uhs-sdr104 = 1
max-frequency = 50000000
[sdc2]
sdc2_used = 1
bus-width= 4
sdc2_clk = port:PE02<9><1><2><default>
sdc2_cmd = port:PE03<9><1><2><default>
sdc2_d0 = port:PE01<9><1><2><default>
sdc2_d1 = port:PE00<9><1><2><default>
sdc2_d2 = port:PE05<9><1><2><default>
sdc2_d3 = port:PE04<9><1><2><default>
;sunxi-power-save-mode =
;sd-uhs-sdr50=
;sd-uhs-ddr50=
;sd-uhs-sdr104=
;cap-sdio-irq1=
;keep-power-in-suspend=
;ignore-pm-notify=
;mmc-ddr-3_3v = 1
max-frequency = 50000000