已合并
多芯片HAL层JTAG驱动实现 #17
多芯片HAL层JTAG驱动实现 #17
已合并
byosoft_zhoujiangkun创建于 16 天前
共 7 个文件变更+743-2
@@ -1,6 +1,10 @@
1# Changelog1# Changelog
2 2 
3All notable changes to this project will be documented in this file.3All notable changes to this project will be documented in this file.
4+## [1.0.9] - 2026-11-13
5+ 
6+- 支持jtag驱动接口
7+ 
4## [1.0.8] - 2026-11-108## [1.0.8] - 2026-11-10
5 9 
6- 支持wdt驱动接口10- 支持wdt驱动接口
@@ -0,0 +1,81 @@
1+/* SPDX-License-Identifier: GPL-2.0 */
2+/* Copyright (c) 2018 Mellanox Technologies. All rights reserved. */
3+/* Copyright (c) 2018 Oleksandr Shamray <oleksandrs@mellanox.com> */
4+/* Copyright (c) 2019 Intel Corporation */
5+ 
6+#ifndef __UAPI_LINUX_JTAG_H
7+#define __UAPI_LINUX_JTAG_H
8+ 
9+#include <linux/ioctl.h>
10+#include <linux/types.h>
11+ 
12+#define JTAG_XFER_MODE 0
13+#define JTAG_XFER_HW_MODE 1
14+#define JTAG_XFER_SW_MODE 0
atomgit-bot
atomgit-botatomgit-bot16 天前

🟡 Medium Priority

变更行:jtag.h:12-14 定义了 JTAG_XFER_MODE 0 / JTAG_XFER_HW_MODE 1 / JTAG_XFER_SW_MODE 0。

问题与证据链:

  1. 文件头注释声明「保留内核命名以便直接下发 ioctl」,但这里 JTAG_XFER_SW_MODE 与 JTAG_XFER_MODE 取同一个值 0。而 jtag.cpp:379 用 mode.feature = JTAG_XFER_MODE(feature 是「模式类别」哨兵),因此 mode 值与 feature 值撞在 0 上,二者不可能同时是正确的内核编码。
  2. jtag.cpp 中 JTAG_XFER_SW_MODE 从未被使用:handle_set_work_mode(374-380)把 bcal 侧 mode 原样透传给 JTAG_SIOCMODE,没有做 bcal mode → 内核 mode 的映射,只校验 mode > 1。
  3. 内核 jtag 框架的 mode 编码为 HW=1、SW=2(0 是 JTAG_XFER_MODE 这个 feature 标识,不是合法 mode)。若板上内核按此编码,则 bcal 传 mode=0(PR UC05 声明的「软件 bitbang」)会被驱动以 -EINVAL 拒绝,JTAG_SIOCMODE 返回 -1,软件 bitbang 能力实际不可用;而 HW(1) 恰好正确,掩盖了该问题。

失败模式:调用 control(CMD_JTAG_TAP_SET_WORK_MODE, mode=0) 时 ioctl 失败,SW bitbang 模式无法切换,UC05 的「0=软件 bitbang」不成立。

建议:确认内核实际 mode 编码后修正 JTAG_XFER_SW_MODE 的取值,并在 handle_set_work_mode 中用 JTAG_XFER_HW_MODE/JTAG_XFER_SW_MODE 做显式映射,避免把 bcal 的 0/1 原样下发给 JTAG_SIOCMODE。

likedislike
不准确?
byosoft_zhoujiangkun
byosoft_zhoujiangkun
16 天前 评论:
likedislike
15+ 
16+/* bcal 侧用 JTAG_TAP_* 名称,这里保留内核命名以便直接下发 ioctl */
17+enum jtag_tapstate {
18+ JTAG_STATE_TLRESET = 0,
19+ JTAG_STATE_IDLE,
20+ JTAG_STATE_SELECTDR,
21+ JTAG_STATE_CAPTUREDR,
22+ JTAG_STATE_SHIFTDR,
23+ JTAG_STATE_EXIT1DR,
24+ JTAG_STATE_PAUSEDR,
25+ JTAG_STATE_EXIT2DR,
26+ JTAG_STATE_UPDATEDR,
27+ JTAG_STATE_SELECTIR,
28+ JTAG_STATE_CAPTUREIR,
29+ JTAG_STATE_SHIFTIR,
30+ JTAG_STATE_EXIT1IR,
31+ JTAG_STATE_PAUSEIR,
32+ JTAG_STATE_EXIT2IR,
33+ JTAG_STATE_UPDATEIR,
34+ JTAG_STATE_CURRENT,
35+};
36+ 
37+enum jtag_reset {
38+ JTAG_NO_RESET = 0,
39+ JTAG_FORCE_RESET = 1,
40+};
41+ 
42+enum jtag_xfer_direction {
43+ JTAG_READ_XFER = 1,
44+ JTAG_WRITE_XFER = 2,
45+ JTAG_READ_WRITE_XFER = 3,
46+};
47+ 
48+struct jtag_tap_state {
49+ __u8 reset;
50+ __u8 from;
51+ __u8 endstate;
52+ __u32 tck;
53+};
atomgit-bot
atomgit-botatomgit-bot16 天前

🔵 Low Priority

变更行:jtag.h:48-53 的 struct jtag_tap_state 用 __u8 reset/from/endstate 加 __u32 tck,混用宽度后 sizeof 变为 8(4 个 u8 + 4 字节 padding),而 JTAG_SIOCSTATE(73 行)是 _IOW(..., 0, struct jtag_tap_state)——sizeof 会被编码进 ioctl 请求号。

问题:同一份头文件的 jtag_xfer 有 static_assert(sizeof(struct jtag_xfer) == 24, ...)(jtag.cpp:71)守护,而 jtag_tap_state 没有任何断言。若内核 uapi 中 tck 是 __u8(与相邻三个字段同宽,结构体为 4 字节),则本文件算出的请求号(size=8)与内核不一致,JTAG_SIOCSTATE 会被判为未知命令返回 ENOTTY,直接导致 UC08(RUN_TEST_IDLE/迁移 TAP 状态)和 UC12(reset)全部失败,且失败原因只是 "run test idle failed: Inappropriate ioctl for device",很难定位。

失败模式:结构体布局/宽度与板上内核 uapi 不一致时,所有 SIOCSTATE 调用在运行期失败;由于是运行期而非编译期暴露,工具自验证(PR 的自验证报告为空)很难发现。

建议:与板上内核 uapi 逐字段核对 jtag_tap_state(尤其是 tck 的宽度),并补 static_assert(sizeof(struct jtag_tap_state) == 期望字节数) 守护 ioctl 请求号,避免布局漂移只在运行期以 ENOTTY 形式暴露。

likedislike
不准确?
byosoft_zhoujiangkun
byosoft_zhoujiangkun
16 天前 评论:
likedislike
54+ 
55+struct jtag_xfer {
56+ __u8 type;
57+ __u8 direction;
58+ __u8 from;
59+ __u8 endstate;
60+ __u32 padding;
61+ __u32 length;
62+ __u64 tdio;
63+};
64+ 
65+struct jtag_mode {
66+ __u32 feature;
67+ __u32 mode;
68+};
69+ 
70+/* ioctl interface */
71+#define __JTAG_IOCTL_MAGIC 0xb2
72+ 
73+#define JTAG_SIOCSTATE _IOW(__JTAG_IOCTL_MAGIC, 0, struct jtag_tap_state)
74+#define JTAG_SIOCFREQ _IOW(__JTAG_IOCTL_MAGIC, 1, unsigned int)
75+#define JTAG_GIOCFREQ _IOR(__JTAG_IOCTL_MAGIC, 2, unsigned int)
76+#define JTAG_IOCXFER _IOWR(__JTAG_IOCTL_MAGIC, 3, struct jtag_xfer)
77+#define JTAG_GIOCSTATUS _IOWR(__JTAG_IOCTL_MAGIC, 4, enum jtag_tapstate)
78+#define JTAG_SIOCMODE _IOW(__JTAG_IOCTL_MAGIC, 5, unsigned int)
79+#define JTAG_SIOCTRST _IOW(__JTAG_IOCTL_MAGIC, 7, unsigned int)
80+ 
81+#endif /* __UAPI_LINUX_JTAG_H */
@@ -0,0 +1,639 @@
1+/*
2+ * Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
3+ *
4+ * Licensed under Mulan PSL v2.
5+ * You can use this software according to the terms and conditions of the Mulan PSL v2.
6+ * You may obtain a copy of Mulan PSL v2 at:
7+ * http://license.coscl.org.cn/MulanPSL2
8+ *
9+ * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
10+ * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
11+ * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
12+ * See the Mulan PSL v2 for more details.
13+ */
14+ 
15+ 
16+#include <bcal/jtag.h>
17+ 
18+#include <algorithm>
19+#include <cerrno>
20+#include <cstdint>
21+#include <cstdio>
22+#include <cstring>
23+#include <dirent.h>
24+#include <fcntl.h>
25+#include <linux/jtag.h>
26+#include <memory>
27+#include <mutex>
28+#include <stdexcept>
29+#include <string>
30+#include <sys/ioctl.h>
31+#include <unistd.h>
32+#include <vector>
33+ 
34+namespace BCAL_JTAG_ASPEED {
35+using namespace bcal;
36+using namespace BCAL_DRIVER_JTAG;
37+ 
38+namespace {
39+ 
40+constexpr const char* DRIVER_VERSION = "bcal_jtag_aspeed v1.0.0 (/dev/jtagN)";
41+constexpr const char* DEV_PREFIX = "/dev/";
42+ 
43+constexpr uint32_t BITS_PER_BYTE = 8;
44+constexpr uint32_t TAP_RESET_TCK = 10; /* Test-Logic-Reset 后额外打 10 拍 */
45+ 
46+constexpr int32_t JTAG_MAX_CONTROLLER_NUM = 2; /* ast2600: jtag0/jtag1 */
47+ 
48+struct JtagModel {
49+ const char* compatible;
50+ uint32_t base_addr;
51+ uint32_t addr_stride;
52+ uint32_t tck_min_khz;
53+ uint32_t tck_max_khz;
54+};
55+ 
56+/* 各 SoC 的 JTAG 控制器基址/步长/TCK 频率范围;新增 SoC 时追加一项。
57+ * ast2600 JTAG pclk 实测 200 MHz,分频字段 12 bit:
58+ * max = pclk = 200 MHz;min ≈ ceil(200 MHz / 4096) = 48.83 kHz。
59+ * 这里取保守范围 50 kHz..200 MHz,避免分频值回绕/被内核静默截断。 */
60+constexpr JtagModel JTAG_MODELS[] = {
61+ {"aspeed,ast2600-jtag", 0x1e6e4000, 0x100, 50, 200000},
62+};
63+ 
64+constexpr const char* JTAG_CLASS_ROOT = "/sys/class/misc";
65+constexpr const char* JTAG_CLASS_PREFIX = "jtag";
66+ 
67+/* DT 属性理论上都很小,异常属性不允许无限增长 */
68+constexpr size_t DT_PROPERTY_MAX_SIZE = 4096;
69+ 
70+static_assert(sizeof(struct jtag_xfer) == 24, "jtag_xfer layout must match the kernel uapi");
71+static_assert(sizeof(struct jtag_tap_state) == 8, "jtag_tap_state layout must match kernel UAPI");
72+/* JTAG_SIOCMODE 的请求号仍按内核 UAPI 的 unsigned int 编码,
73+ * jtag_mode 的断言只守护结构体布局。 */
74+static_assert(sizeof(struct jtag_mode) == 8, "jtag_mode layout must match kernel UAPI");
75+ 
76+bool starts_with(const string_t& text, const char* prefix)
77+{
78+ return text.compare(0, std::strlen(prefix), prefix) == 0;
79+}
80+ 
81+std::vector<string_t> list_dir(const char* root)
82+{
83+ const auto close_dir = [](DIR* directory) {
84+ if (directory != nullptr) {
85+ ::closedir(directory);
86+ }
87+ };
88+ std::unique_ptr<DIR, decltype(close_dir)> directory(opendir(root), close_dir);
89+ if (directory == nullptr) {
90+ throw std::runtime_error("opendir " + string_t(root) + " failed: " + std::strerror(errno) +
91+ "; check that /sys is mounted and readable");
92+ }
93+ 
94+ std::vector<string_t> names;
95+ errno = 0;
96+ while (const dirent* entry = readdir(directory.get())) {
97+ names.emplace_back(entry->d_name);
98+ }
99+ if (errno != 0) {
100+ throw std::runtime_error("readdir " + string_t(root) + " failed: " + std::strerror(errno));
@@ -0,0 +1,15 @@
1+driver_name = 'jtag'
2+ 
3+sources = files('jtag.cpp')
4+ 
5+jtag_inc = include_directories('include')
6+ 
7+jtag_lib = shared_library(driver_name,
8+ sources,
9+ include_directories: [jtag_inc],
10+ dependencies: [bcal_dep],
11+ # 默认隐藏,create_driver/destroy_driver 靠 visibility("default") 显式导出
12+ cpp_args: ['-fvisibility=hidden', '-fvisibility-inlines-hidden'],
13+ install: true,
14+ install_dir: driver_install_dir
15+)
@@ -5,3 +5,4 @@ subdir('h2b')
5subdir('adc')5subdir('adc')
6subdir('pwm')6subdir('pwm')
7subdir('wdt')7subdir('wdt')
8+subdir('jtag')
@@ -2,14 +2,14 @@
2 "name": "libsoc_adapter_aspeed",2 "name": "libsoc_adapter_aspeed",
3 "type": "application",3 "type": "application",
4 "deployConfig": "",4 "deployConfig": "",
5- "version": "1.0.8",5+ "version": "1.0.9",
6 "license": "Mulan PSL v2",6 "license": "Mulan PSL v2",
7 "description": "BMC chip adapter for Aspeed.",7 "description": "BMC chip adapter for Aspeed.",
8 "dependencies": {8 "dependencies": {
9 "test": [],9 "test": [],
10 "build": [10 "build": [
11 {11 {
12- "conan": "bcal/[>=0.0.8]"12+ "conan": "bcal/[>=0.0.10]"
13 }13 }
14 ]14 ]
15 },15 },
@@ -13,3 +13,4 @@ opt/bmc/bcal/libh2b.so f 550 0 0
13opt/bmc/bcal/libadc.so f 550 0 013opt/bmc/bcal/libadc.so f 550 0 0
14opt/bmc/bcal/libpwm.so f 550 0 014opt/bmc/bcal/libpwm.so f 550 0 0
15opt/bmc/bcal/libwdt.so f 550 0 015opt/bmc/bcal/libwdt.so f 550 0 0
16+opt/bmc/bcal/libjtag.so f 550 0 0