Skip to content

Commit c42fd96

Browse files
committed
add new
1 parent 860f265 commit c42fd96

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

any-uart/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "any-uart"
3-
version = "0.2.1"
3+
version = "0.2.2"
44
edition = "2024"
55
authors = ["周睿 <[email protected]>"]
66
description = "Init early console from device tree, mostly for Arm"
@@ -18,3 +18,4 @@ alloc = []
1818
fdt-parser = "0.4"
1919
embedded-hal-nb = "1.0"
2020
bitflags = "2.8"
21+
link-boot = "0.2"

any-uart/src/lib.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ pub use embedded_hal_nb::nb::block;
1616
pub use embedded_hal_nb::serial::ErrorKind;
1717

1818
use aux_mini::AuxMini;
19-
use fdt_parser::{Chosen, Fdt, Node};
19+
pub use fdt_parser::Node;
20+
use fdt_parser::{Chosen, Fdt};
2021
use ns16550::Ns16550;
2122
use pl011::Pl011;
2223

@@ -35,7 +36,7 @@ pub struct Uart {
3536
}
3637

3738
impl Uart {
38-
fn new<C: Console>(data: UartData) -> Self {
39+
fn _new<C: Console>(data: UartData) -> Self {
3940
let op = C::to_op();
4041

4142
Self {
@@ -46,6 +47,33 @@ impl Uart {
4647
}
4748
}
4849

50+
pub fn new_by_fdt_node(node: &Node<'_>, f: FnPhysToVirt) -> Option<Self> {
51+
let reg = node.reg()?.next()?;
52+
53+
let io_kind = IoKind::Mmio32;
54+
55+
// TODO: support io kind detect
56+
57+
let uart = UartData::new(reg.address, io_kind, f);
58+
59+
for c in node.compatibles() {
60+
macro_rules! of_uart {
61+
($name:ty, $compatible:expr) => {
62+
for want in $compatible {
63+
if c.contains(want) {
64+
return Some(Uart::_new::<$name>(uart));
65+
}
66+
}
67+
};
68+
}
69+
70+
of_uart!(AuxMini, ["brcm,bcm2835-aux-uart"]);
71+
of_uart!(Pl011, ["arm,pl011", "arm,primecell"]);
72+
of_uart!(Ns16550, ["snps,dw-apb-uart"]);
73+
}
74+
None
75+
}
76+
4977
pub fn set_irq_enable(&mut self, enable: bool) {
5078
(self.op.set_irq_enable)(self.data, enable);
5179
}
@@ -226,14 +254,14 @@ pub fn init(fdt_addr: NonNull<u8>, fn_phys_to_virt: FnPhysToVirt) -> Option<Uart
226254
let uart = UartData::new(reg.address, io_kind, fn_phys_to_virt);
227255

228256
if is_8250 {
229-
return Some(Uart::new::<Ns16550>(uart));
257+
return Some(Uart::_new::<Ns16550>(uart));
230258
} else {
231259
for c in node.compatibles() {
232260
macro_rules! of_uart {
233261
($name:ty, $compatible:expr) => {
234262
for want in $compatible {
235263
if c.contains(want) {
236-
return Some(Uart::new::<$name>(uart));
264+
return Some(Uart::_new::<$name>(uart));
237265
}
238266
}
239267
};

0 commit comments

Comments
 (0)