2023-08-13 21:23:58 +03:00
|
|
|
//! Simple "passthrough" bus device
|
|
|
|
|
2024-12-13 23:35:17 +02:00
|
|
|
// use device_tree::{device_tree_driver, dt::DevTreeIndexNodeExt};
|
|
|
|
// use libk::device::manager::DEVICE_REGISTRY;
|
|
|
|
|
2024-12-16 00:23:23 +02:00
|
|
|
use core::ops::Range;
|
2024-12-13 23:35:17 +02:00
|
|
|
|
|
|
|
use abi::error::Error;
|
|
|
|
use alloc::{sync::Arc, vec::Vec};
|
2025-02-06 12:24:03 +02:00
|
|
|
use device_api::{
|
|
|
|
bus::Bus,
|
|
|
|
device::{Device, DeviceInitContext},
|
|
|
|
};
|
2024-12-13 23:35:17 +02:00
|
|
|
use device_tree::{
|
2024-12-16 00:23:23 +02:00
|
|
|
driver::{device_tree_driver, Node, ProbeContext},
|
2024-12-16 12:56:05 +02:00
|
|
|
DeviceTreePropertyRead,
|
2024-12-13 23:35:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SimpleBus {
|
|
|
|
ranges: Vec<(Range<u64>, u64)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Device for SimpleBus {
|
2025-02-06 12:24:03 +02:00
|
|
|
unsafe fn init(self: Arc<Self>, _cx: DeviceInitContext) -> Result<(), Error> {
|
2024-12-13 23:35:17 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-12-16 00:23:23 +02:00
|
|
|
fn as_bus(self: Arc<Self>) -> Option<Arc<dyn Bus>> {
|
|
|
|
Some(self)
|
|
|
|
}
|
|
|
|
|
2024-12-13 23:35:17 +02:00
|
|
|
fn display_name(&self) -> &str {
|
|
|
|
"simple-bus"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bus for SimpleBus {
|
2024-12-16 00:23:23 +02:00
|
|
|
fn map_range(&self, bus_range: Range<u64>) -> Option<Range<u64>> {
|
2025-01-19 15:16:26 +02:00
|
|
|
if self.ranges.is_empty() {
|
|
|
|
return Some(bus_range);
|
|
|
|
}
|
|
|
|
|
2024-12-16 00:23:23 +02:00
|
|
|
let start = bus_range.start;
|
|
|
|
let end = bus_range.end;
|
|
|
|
|
2024-12-13 23:35:17 +02:00
|
|
|
for (range, offset) in self.ranges.iter() {
|
2024-12-16 00:23:23 +02:00
|
|
|
if range.contains(&start) && range.contains(&end) {
|
|
|
|
let start = start - range.start + *offset;
|
|
|
|
let end = end - range.start + *offset;
|
|
|
|
return Some(start..end);
|
2024-12-13 23:35:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-16 00:23:23 +02:00
|
|
|
None
|
2024-12-13 23:35:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-13 21:23:58 +03:00
|
|
|
device_tree_driver! {
|
|
|
|
compatible: ["simple-bus"],
|
2024-12-16 00:23:23 +02:00
|
|
|
driver: {
|
|
|
|
fn probe(&self, node: &Arc<Node>, _context: &ProbeContext) -> Option<Arc<dyn Device>> {
|
|
|
|
// Format per DT spec: (child-bus-address, parent-bus-address, length)
|
|
|
|
// Where:
|
|
|
|
// child-bus-address: #address-cells of this node
|
|
|
|
// parent-bus-address: #address-cells of parent bus
|
|
|
|
// length: #size-cells of this node
|
|
|
|
let ranges = node.property("ranges")?;
|
|
|
|
|
|
|
|
let parent_address_cells = node.bus_address_cells();
|
|
|
|
let child_address_cells = node.self_address_cells()?;
|
|
|
|
let child_size_cells = node.self_size_cells()?;
|
|
|
|
|
|
|
|
let cell_sizes = (child_address_cells, parent_address_cells, child_size_cells);
|
|
|
|
|
|
|
|
let mut items = Vec::new();
|
2025-01-19 15:16:26 +02:00
|
|
|
|
2024-12-16 00:23:23 +02:00
|
|
|
for (child_address, parent_address, length) in ranges.iter_cells(cell_sizes) {
|
|
|
|
let child_range = child_address..child_address + length;
|
|
|
|
items.push((child_range, parent_address));
|
|
|
|
}
|
2024-12-13 23:35:17 +02:00
|
|
|
|
2024-12-16 00:23:23 +02:00
|
|
|
Some(Arc::new(SimpleBus {
|
|
|
|
ranges: items,
|
|
|
|
}))
|
2024-12-13 23:35:17 +02:00
|
|
|
}
|
2023-08-13 21:23:58 +03:00
|
|
|
}
|
|
|
|
}
|