From 5febf432df1cfa5b25d99b54c32103fafbdd0eb9 Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Sun, 16 Aug 2026 15:32:33 +0200 Subject: kbuild: rust: keep Rust objects out of Clang LTO with inline helpers Under `CONFIG_LTO_CLANG` + `CONFIG_RUST_INLINE_HELPERS`, one may hit `objtool` errors such as: vmlinux.o: error: objtool: _R..._3Gsp4boot+0xd6a: can't find jump dest instruction at .text._R..._3Gsp4boot+0x1dfd The reason is that in such builds, the Clang invocation that compiles the combined Rust plus helpers bitcode emits LLVM bitcode (again) -- the final code generation happens in the linker's LTO step, which the `-mllvm` trap options passed to Clang do not reach. This, in turn, means that unreachable traps are missing, and the impossible paths do not merely fallthrough to the next symbol, but past the end of their own section, since LTO builds place each function in its own section. Thus filter `CC_FLAGS_LTO` out of the Clang invocation, so that it always emits machine code directly, with the traps in place. Assisted-by: LLM Cc: Gary Guo Cc: Boqun Feng Cc: Alice Ryhl Cc: Matthew Maurer Cc: Josh Poimboeuf Cc: Peter Zijlstra Cc: stable@vger.kernel.org Fixes: 3a2486cc1da5 ("kbuild: rust: provide an option to inline C helpers into Rust") Acked-by: Gary Guo Link: https://patch.msgid.link/20260816133233.197500-2-ojeda@kernel.org Signed-off-by: Miguel Ojeda --- drivers/rtc/rtc-ds2404.c | 225 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 drivers/rtc/rtc-ds2404.c (limited to 'drivers/rtc/rtc-ds2404.c') diff --git a/drivers/rtc/rtc-ds2404.c b/drivers/rtc/rtc-ds2404.c new file mode 100644 index 000000000..217694eca --- /dev/null +++ b/drivers/rtc/rtc-ds2404.c @@ -0,0 +1,225 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2012 Sven Schnelle + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#define DS2404_STATUS_REG 0x200 +#define DS2404_CONTROL_REG 0x201 +#define DS2404_RTC_REG 0x202 + +#define DS2404_WRITE_SCRATCHPAD_CMD 0x0f +#define DS2404_READ_SCRATCHPAD_CMD 0xaa +#define DS2404_COPY_SCRATCHPAD_CMD 0x55 +#define DS2404_READ_MEMORY_CMD 0xf0 + +#define DS2404_RST 0 +#define DS2404_CLK 1 +#define DS2404_DQ 2 + +struct ds2404 { + struct device *dev; + struct gpio_desc *rst_gpiod; + struct gpio_desc *clk_gpiod; + struct gpio_desc *dq_gpiod; +}; + +static int ds2404_gpio_map(struct ds2404 *chip, struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + + /* This will de-assert RESET, declare this GPIO as GPIOD_ACTIVE_LOW */ + chip->rst_gpiod = devm_gpiod_get(dev, "rst", GPIOD_OUT_LOW); + if (IS_ERR(chip->rst_gpiod)) + return PTR_ERR(chip->rst_gpiod); + + chip->clk_gpiod = devm_gpiod_get(dev, "clk", GPIOD_OUT_HIGH); + if (IS_ERR(chip->clk_gpiod)) + return PTR_ERR(chip->clk_gpiod); + + chip->dq_gpiod = devm_gpiod_get(dev, "dq", GPIOD_ASIS); + if (IS_ERR(chip->dq_gpiod)) + return PTR_ERR(chip->dq_gpiod); + + return 0; +} + +static void ds2404_reset(struct ds2404 *chip) +{ + gpiod_set_value(chip->rst_gpiod, 1); + udelay(1000); + gpiod_set_value(chip->rst_gpiod, 0); + gpiod_set_value(chip->clk_gpiod, 0); + gpiod_direction_output(chip->dq_gpiod, 0); + udelay(10); +} + +static void ds2404_write_byte(struct ds2404 *chip, u8 byte) +{ + int i; + + gpiod_direction_output(chip->dq_gpiod, 1); + for (i = 0; i < 8; i++) { + gpiod_set_value(chip->dq_gpiod, byte & (1 << i)); + udelay(10); + gpiod_set_value(chip->clk_gpiod, 1); + udelay(10); + gpiod_set_value(chip->clk_gpiod, 0); + udelay(10); + } +} + +static u8 ds2404_read_byte(struct ds2404 *chip) +{ + int i; + u8 ret = 0; + + gpiod_direction_input(chip->dq_gpiod); + + for (i = 0; i < 8; i++) { + gpiod_set_value(chip->clk_gpiod, 0); + udelay(10); + if (gpiod_get_value(chip->dq_gpiod)) + ret |= 1 << i; + gpiod_set_value(chip->clk_gpiod, 1); + udelay(10); + } + return ret; +} + +static void ds2404_read_memory(struct ds2404 *chip, u16 offset, + int length, u8 *out) +{ + ds2404_reset(chip); + ds2404_write_byte(chip, DS2404_READ_MEMORY_CMD); + ds2404_write_byte(chip, offset & 0xff); + ds2404_write_byte(chip, (offset >> 8) & 0xff); + while (length--) + *out++ = ds2404_read_byte(chip); +} + +static void ds2404_write_memory(struct ds2404 *chip, u16 offset, + int length, u8 *out) +{ + int i; + u8 ta01, ta02, es; + + ds2404_reset(chip); + ds2404_write_byte(chip, DS2404_WRITE_SCRATCHPAD_CMD); + ds2404_write_byte(chip, offset & 0xff); + ds2404_write_byte(chip, (offset >> 8) & 0xff); + + for (i = 0; i < length; i++) + ds2404_write_byte(chip, out[i]); + + ds2404_reset(chip); + ds2404_write_byte(chip, DS2404_READ_SCRATCHPAD_CMD); + + ta01 = ds2404_read_byte(chip); + ta02 = ds2404_read_byte(chip); + es = ds2404_read_byte(chip); + + for (i = 0; i < length; i++) { + if (out[i] != ds2404_read_byte(chip)) { + dev_err(chip->dev, "read invalid data\n"); + return; + } + } + + ds2404_reset(chip); + ds2404_write_byte(chip, DS2404_COPY_SCRATCHPAD_CMD); + ds2404_write_byte(chip, ta01); + ds2404_write_byte(chip, ta02); + ds2404_write_byte(chip, es); + + while (gpiod_get_value(chip->dq_gpiod)) + ; +} + +static void ds2404_enable_osc(struct ds2404 *chip) +{ + u8 in[1] = { 0x10 }; /* enable oscillator */ + + ds2404_write_memory(chip, 0x201, 1, in); +} + +static int ds2404_read_time(struct device *dev, struct rtc_time *dt) +{ + struct ds2404 *chip = dev_get_drvdata(dev); + unsigned long time = 0; + __le32 hw_time = 0; + + ds2404_read_memory(chip, 0x203, 4, (u8 *)&hw_time); + time = le32_to_cpu(hw_time); + + rtc_time64_to_tm(time, dt); + return 0; +} + +static int ds2404_set_time(struct device *dev, struct rtc_time *dt) +{ + struct ds2404 *chip = dev_get_drvdata(dev); + u32 time = cpu_to_le32(rtc_tm_to_time64(dt)); + ds2404_write_memory(chip, 0x203, 4, (u8 *)&time); + return 0; +} + +static const struct rtc_class_ops ds2404_rtc_ops = { + .read_time = ds2404_read_time, + .set_time = ds2404_set_time, +}; + +static int rtc_probe(struct platform_device *pdev) +{ + struct ds2404 *chip; + struct rtc_device *rtc; + int retval = -EBUSY; + + chip = devm_kzalloc(&pdev->dev, sizeof(struct ds2404), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + chip->dev = &pdev->dev; + + rtc = devm_rtc_allocate_device(&pdev->dev); + if (IS_ERR(rtc)) + return PTR_ERR(rtc); + + retval = ds2404_gpio_map(chip, pdev); + if (retval) + return retval; + + platform_set_drvdata(pdev, chip); + + rtc->ops = &ds2404_rtc_ops; + rtc->range_max = U32_MAX; + + retval = devm_rtc_register_device(rtc); + if (retval) + return retval; + + ds2404_enable_osc(chip); + return 0; +} + +static struct platform_driver rtc_device_driver = { + .probe = rtc_probe, + .driver = { + .name = "ds2404", + }, +}; +module_platform_driver(rtc_device_driver); + +MODULE_DESCRIPTION("DS2404 RTC"); +MODULE_AUTHOR("Sven Schnelle"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:ds2404"); -- cgit v1.3.1