diff options
| author | Gary Guo <gary@garyguo.net> | 2026-09-01 17:50:35 +0100 |
|---|---|---|
| committer | Danilo Krummrich <dakr@kernel.org> | 2026-09-06 13:39:49 +0200 |
| commit | c09d3a26e1b810d43ee8a15c4f09ee752fee55a6 (patch) | |
| tree | 9148f1c715d304b66ec90352cfc788890539585f /rust/kernel | |
| parent | 4f620881808e9d3d951343e2edc9c8c41286391a (diff) | |
| download | linux-next-c09d3a26e1b810d43ee8a15c4f09ee752fee55a6.tar.gz linux-next-c09d3a26e1b810d43ee8a15c4f09ee752fee55a6.zip | |
rust: io: register: support fixed offset register without bitfield
Add a rule to allow creating `IoLoc` in `register!()` using an existing
type and not create a bitfield. Add an example to demonstrate this for FIFO
registers.
This rule is also going to be used to create subregions for registers; the
example of doing so will be added later when relative registers are
removed.
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Signed-off-by: Gary Guo <gary@garyguo.net>
Tested-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://patch.msgid.link/20260901-typed_register-v4-11-5552b1d59525@garyguo.net
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
Diffstat (limited to 'rust/kernel')
| -rw-r--r-- | rust/kernel/io/register.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/rust/kernel/io/register.rs b/rust/kernel/io/register.rs index 58dcf651dfd0..b9cb4fce8628 100644 --- a/rust/kernel/io/register.rs +++ b/rust/kernel/io/register.rs @@ -182,6 +182,24 @@ where } } +// Provides a `IoLoc` impl that for a fixed offset. +#[doc(hidden)] +pub struct OffsetLoc<Base: ?Sized, T>(usize, PhantomData<(T, Base)>); + +impl<Base: ?Sized, T> OffsetLoc<Base, T> { + #[inline] + pub const fn new(offset: usize) -> Self { + Self(offset, PhantomData) + } +} + +impl<Base: ?Sized, T> IoLoc<Base, T> for OffsetLoc<Base, T> { + #[inline(always)] + fn offset(self) -> usize { + self.0 + } +} + /// Trait providing a base address to be added to the offset of a relative register to obtain /// its actual offset. /// @@ -520,6 +538,19 @@ pub const fn element_alias_offset<Base: ?Sized, Alias: RegisterArray<Base = Base /// In this example, `SCRATCH_BOOT_STATUS` uses the same I/O address as `SCRATCH`, while providing /// its own `completed` field. /// +/// If you do not wish to have a bitfield defined, you can also create a register using an existing +/// type. +/// +/// ```no_run +/// # use kernel::io::*; +/// register! { +/// base: Region<0x1000>; +/// +/// /// UART RX register. +/// pub UART_RX: u8 @ 0x100; +/// } +/// ``` +/// /// ## Relative registers /// /// Relative registers can be instantiated several times at a relative offset of a group of bases. |
