# BL2 配置文件说明

BL2可以根据配置文件中的内容在启动阶段对系统做一些设置，包括bl2 feature配置（看门狗的配置、gpio的配置）、DDR的配置和eFUSE烧写相关的配置。

BL2配置文件路径为：
```
device/horizon/x5/board_cfg/soc/bl2_cfg/bl2_cfg.json
```
配置文件的打包程序路径为：
```
build/tools/bl2_cfg.py
```
bl2_cfg.py会解析bl2_cfg.json中的内容，打包为bin文件，最终和uboot通过fip包的形式打包在一起，制作uboot fip包的脚本路径为：
```
build/mk_uboot.sh
```

## bl2 feature配置

### bl2 看门狗配置
```
{
	"bl2_cfg": {
		"feature": {
			"wdt_en": "false",
			"wtd_timeout": 10,
			...
		},
	},
	...
}
```
在配置文件中可以设置bl2阶段看门狗的使能以及超时时间：
- wdt_en：bl2看门狗是否使能。"false"表示关闭，"true"表示开启
- wtd_timeout：超时时间，单位是秒

### bl2 gpio配置
bl2支持将pin配置成gpio功能，方向默认为"out"，可以配置高电平或低电平，可以配置单个pin或者连续的一组pin，最多支持40个pin的配置。
当前的pin分布在4个子系统，分别为"aon"、"hsio"、"lsio"和"dsp"。
每个子系统中pin可能会分为多个组，子系统"hsio"和"lsio"有组"0"和组"1"，其他子系统只有组"0"。
在每个组中，pin的序号以"0"为开始。

#### 单pin配置
```
{
	"bl2_cfg": {
		"feature": {
			...
			"gpio_cfg": {
				"lsio_0_1": {
					"value": 1
				}
			}
		}
	},
	...
}
```
配置时需要写明pin所属的系统、组和序号，以"lsio_0_1"为例，表明这个pin属于lsio子系统，第0组的序号为1的pin。
"value"为1表示将此pin配置为高电位， 0为低电位（只要配置为gpio，会将此pin默认配置为out）。

#### 连续pin配置
```
{
	"bl2_cfg": {
		"feature": {
			...
			"gpio_cfg": {
				"lsio_0_2-31": {
					"value": 1
				}
			}
		}
	},
	...
}
```
以"lsio_0_2-31"为例，表示lsio子系统的第0组的序号为2到31的pin，将此组pin都配置为高电位。

**注："gpio_cfg"下可以有多个单pin配置和连续pin配置，但是pin的总数不能超过40。**

## DDR配置
```
{
	...
    "ddr": {
        "detect": {
            "adc_channel": 2,
            "adc_map_rules": {
              "ddr_profile_10": [51, 140],
              "ddr_profile_11": [151, 240],
              "ddr_profile_2": [251, 340],
              "ddr_profile_3": [351, 440],
              "ddr_profile_1": [451, 540],
              "ddr_profile_7": [551, 640],
              "ddr_profile_17": [651, 740],
              "ddr_profile_18": [751, 840],
              "ddr_profile_14": [851, 940],
              "ddr_profile_15": [951, 1040],
              "ddr_profile_12": [1051, 1140],
              "ddr_profile_13": [1151, 1240],
              "reserved_5": [1251, 1340],
              "reserved_6": [1351, 1440]
            }
        },
        "force": {
            "force_enable":"false",
            "ddr_type": "lpddr4",
            "rank_type": "single",
            "die_dencity": 1,
            "alternative": 0
        },
        "ecc_enabled": "false",
        "freq": "default",
        "diag_test": "false"
    },
	...
}
```
这里主要配置DDR的信息，用于适配不同的DDR，需要配置的信息如下所示
- 识别DDR的ADC map规则
- DDR的类型，lpddr4还是lpddr4x
- DDR的rank值， single rank还是 dual rank
- DDR单die的容量，1: 1GB; 2: 2GB
- 频点信息
- 是否使能ecc

其中DDR的类型、DDR rank值和单die容量有两种配置方法
- 通过ADC探测获取
- 在该配置文件中强制指定

如果在该配置文件中强制指定DDR的类型、DDR rank值、单die容量和最高频率，则需要

- force_enable：设置为true，表示强制指定
- ddr_type：DDR类型。当前支持的有"lpddr4", "lpddr4x"
- rank_type：DDR rank数量。当前支持的有"single", "dual"
- die_dencity: DDR 单die容量。1: 1GB; 2: 2GB
- alternative: DDR Alternative值。表示在相同的ddr_type、rank_type和die_dencity下的其他配置，主要是针对相同规格客户的不同ODT配置。默认为0，表示使用默认的ODT配置。
- "freq": DDR最高频率。3200; 3733; 4266

如果通过ADC获取DDR的类型、DDR rank值和单die容量，则需要

- force_enable：设置为false，表示使用外部ADC自动探测的方式
- adc_channel：ADC探测DDR的通道
- adc_map_rules：ADC档位与DDR配置的映射关系。字典中的key表示的是DDR配置文件，即profile，value表示的是当前DDR配置的ADC识别范围，列表中的第一项表示当前DDR配置的ADC值的下限，第二项表示当前DDR配置的ADC值的上限，单位都是mv.

**注意：**
- 字典中的key的组成形式必须是ddr_profile_num或reserved_num, 其中最后的num必须是十进制数字

ADC的电压值与DDR配置的映射关系如下

| 颗粒类型 | rank数量 | density/CH | 最高频率 | Alternative | profile | ADC 档位 |
| -------- | -------- | ---------- | -------- | ---------- | -------- | --------- |
| LP4      | 1        | 1          |  3200   | 0          | profile_1 | 500mv |
| LP4      | 2        | 1          |  4266   | 0          | profile_2 | 300mv |
| LP4X     | 2        | 1          |  4266   | 0          | profile_3 | 400mv |
| LP4X     | 2        | 1          |  3733   | 0          | profile_7 | 600mv |
| LP4      | 1        | 1          |  4266   | 0          | profile_10| 100mv |
| LP4X     | 1        | 1          |  4266   | 0          | profile_11| 200mv |
| LP4      | 2        | 2          |  4266   | 0          | profile_12| 1100mv |
| LP4X     | 2        | 2          |  4266   | 0          | profile_13| 1200mv |
| LP4      | 1        | 2          |  4266   | 0          | profile_14| 900mv |
| LP4X     | 1        | 2          |  4266   | 0          | profile_15| 1000mv |
| LP4      | 1        | 1          |  3733   | 0          | profile_17| 700mv |
| LP4      | 1        | 2          |  4266   | 1          | profile_18| 800mv |

**替换profile**

例如地瓜提供的新的DDR配置文件profile_19，客户可以替换reserved_5为profile_19，profile_19将会启动1300这个ADC档位。即ADC识别到了1300档位，就会使用profile_19的配置进行对DDR的初始化。

```
...
              "ddr_profile_19": [1251, 1340],
...
```

参数"diag_test"用于配置是否开启DDR数字眼图功能
```
"diag_test": "false"   # 不开启眼图功能
"diag_test": "true"    # 开启眼图功能
```

**通过bl2_cfg开启DDR数字眼图后，想要退出只能重新烧录包含未开启眼图功能bl2_cfg的uboot镜像**

**DDR数字眼图功能开启除了可以在bl2_cfg中配置外，也可以在kernel下通过重启命令```reboot -m eye -f```进入，通过此命令进入DDR数字眼图模式后，芯片重启后可以退出眼图模式**

## eFUSE配置

参考 [BL2 烧录 eFuse](../../system_component_development/security_development/efuse/efuse_update.html#span-id-bl2-update-efuse-bl2-efuse) 章节。
