fdt_parser/
clocks.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use crate::{node::Node, read::FdtReader, Fdt, Phandle};

pub struct ClocksIter<'a> {
    pub fdt: &'a Fdt<'a>,
    pub prop: Option<FdtReader<'a>>,
}

impl<'a> ClocksIter<'a> {
    pub fn new(node: &'a Node<'a>) -> Self {
        let fdt = node.fdt;
        let prop = node.find_property("clocks");

        Self {
            fdt,
            prop: prop.map(|p| p.data),
        }
    }
}

impl<'a> Iterator for ClocksIter<'a> {
    type Item = ClockRef<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        let p = self.prop.as_mut()?;
        let phandle = Phandle::from(p.take_u32()?);

        let node = self.fdt.get_node_by_phandle(phandle)?;
        let mut select = 0;

        let cell_size = node
            .find_property("#clock-cells")
            .expect("#clock-cells not found")
            .u32();

        if cell_size > 0 {
            select = p.take_u32().expect("invalid clock cells");
            for _ in 0..cell_size - 1 {
                p.take_u32();
            }
        }

        Some(ClockRef {
            node,
            select: select as _,
        })
    }
}

pub struct ClockRef<'a> {
    pub node: Node<'a>,
    /// second cell of one of `clocks`.
    pub select: usize,
}