iPXE
Data Structures | Macros | Functions
devtree.h File Reference

Devicetree bus. More...

#include <ipxe/device.h>
#include <ipxe/dma.h>
#include <ipxe/fdt.h>

Go to the source code of this file.

Data Structures

struct  dt_device
 A devicetree device. More...
 
struct  dt_driver
 A devicetree driver. More...
 

Macros

#define DT_DRIVERS   __table ( struct dt_driver, "dt_drivers" )
 Devicetree driver table. More...
 
#define __dt_driver   __table_entry ( DT_DRIVERS, 01 )
 Declare a devicetree driver. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void dt_set_drvdata (struct dt_device *dt, void *priv)
 Set devicetree driver-private data. More...
 
static void * dt_get_drvdata (struct dt_device *dt)
 Get devicetree driver-private data. More...
 
static struct dt_devicedt_parent (struct dt_device *dt)
 Get devicetree parent device. More...
 
void * dt_ioremap (struct dt_device *dt, unsigned int offset, unsigned int index, size_t len)
 Map devicetree range. More...
 
int dt_probe_node (struct device *parent, unsigned int offset)
 Probe devicetree node. More...
 
void dt_remove_node (struct device *parent)
 Remove devicetree node. More...
 
int dt_probe_children (struct dt_device *parent, unsigned int offset)
 Probe devicetree children. More...
 
void dt_remove_children (struct dt_device *parent)
 Remove devicetree children. More...
 

Detailed Description

Devicetree bus.

Definition in file devtree.h.

Macro Definition Documentation

◆ DT_DRIVERS

#define DT_DRIVERS   __table ( struct dt_driver, "dt_drivers" )

Devicetree driver table.

Definition at line 55 of file devtree.h.

◆ __dt_driver

struct dt_driver dwgpio_driver __dt_driver   __table_entry ( DT_DRIVERS, 01 )

Declare a devicetree driver.

DesignWare GPIO port devicetree driver.

Generic node driver.

Definition at line 58 of file devtree.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ dt_set_drvdata()

static void dt_set_drvdata ( struct dt_device dt,
void *  priv 
)
inlinestatic

Set devicetree driver-private data.

Parameters
dtDevicetree device
privPrivate data

Definition at line 66 of file devtree.h.

66  {
67  dt->priv = priv;
68 }
void * priv
Driver-private data.
Definition: devtree.h:27
static struct tlan_private * priv
Definition: tlan.c:225

References dt_device::priv, and priv.

Referenced by cgem_probe(), dwgpio_group_probe(), dwgpio_probe(), dwmac_probe(), dwuart_probe(), and dwusb_probe().

◆ dt_get_drvdata()

static void* dt_get_drvdata ( struct dt_device dt)
inlinestatic

Get devicetree driver-private data.

Parameters
dtDevicetree device
Return values
privPrivate data

Definition at line 76 of file devtree.h.

76  {
77  return dt->priv;
78 }
void * priv
Driver-private data.
Definition: devtree.h:27

References dt_device::priv.

Referenced by cgem_remove(), dwgpio_group_remove(), dwgpio_probe(), dwgpio_remove(), dwmac_remove(), dwuart_remove(), and dwusb_remove().

◆ dt_parent()

static struct dt_device* dt_parent ( struct dt_device dt)
inlinestatic

Get devicetree parent device.

Parameters
dtDevicetree device
Return values
parentParent devicetree device

Definition at line 86 of file devtree.h.

86  {
87  return container_of ( dt->dev.parent, struct dt_device, dev );
88 }
struct device * parent
Bus device.
Definition: device.h:88
struct device dev
Generic device.
Definition: devtree.h:21
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
A devicetree device.
Definition: devtree.h:17

References container_of, dt_device::dev, and device::parent.

Referenced by dwgpio_probe().

◆ dt_ioremap()

void* dt_ioremap ( struct dt_device dt,
unsigned int  offset,
unsigned int  index,
size_t  len 
)

Map devicetree range.

Parameters
dtDevicetree device
offsetStarting node offset
indexRegion index
lenLength to map, or 0 to map whole region
Return values
io_addrI/O address, or NULL on error

Definition at line 52 of file devtree.c.

53  {
54  struct fdt_reg_cells regs;
56  uint64_t size;
57  void *io_addr;
58  int rc;
59 
60  /* Get parent region cell size specification */
61  if ( ( rc = fdt_parent_reg_cells ( &sysfdt, offset, &regs ) ) != 0 ) {
62  DBGC ( dt, "DT %s could not get region cell sizes: %s\n",
63  dt->name, strerror ( rc ) );
64  return NULL;
65  }
66 
67  /* Read address */
68  if ( ( rc = fdt_reg_address ( &sysfdt, offset, &regs, index,
69  &address ) ) != 0 ) {
70  DBGC ( dt, "DT %s could not read region %d address: %s\n",
71  dt->name, index, strerror ( rc ) );
72  return NULL;
73  }
74 
75  /* Read size (or assume sufficient, if tree specifies no sizes) */
76  size = len;
77  if ( regs.size_cells &&
78  ( ( rc = fdt_reg_size ( &sysfdt, offset, &regs, index,
79  &size ) ) != 0 ) ) {
80  DBGC ( dt, "DT %s could not read region %d size: %s\n",
81  dt->name, index, strerror ( rc ) );
82  return NULL;
83  }
84 
85  /* Use region size as length if not specified */
86  if ( ! len )
87  len = size;
88  DBGC ( dt, "DT %s region %d at %#08llx+%#04llx\n",
89  dt->name, index, ( ( unsigned long long ) address ),
90  ( ( unsigned long long ) size ) );
91 
92  /* Verify size */
93  if ( len > size ) {
94  DBGC ( dt, "DT %s region %d is too small (%#llx/%#zx bytes)\n",
95  dt->name, index, ( ( unsigned long long ) size ), len );
96  return NULL;
97  }
98 
99  /* Map region */
100  io_addr = ioremap ( address, len );
101  if ( ! io_addr ) {
102  DBGC ( dt, "DT %s could not map region %d\n",
103  dt->name, index );
104  return NULL;
105  }
106 
107  return io_addr;
108 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
uint64_t address
Base address.
Definition: ena.h:24
uint16_t size
Buffer size.
Definition: dwmac.h:14
#define DBGC(...)
Definition: compiler.h:505
A device tree region cell size specification.
Definition: fdt.h:136
long index
Definition: bigint.h:62
unsigned long long uint64_t
Definition: stdint.h:13
ring len
Length.
Definition: dwmac.h:231
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
int fdt_reg_address(struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs, unsigned int index, uint64_t *address)
Get region address.
Definition: fdt.c:792
struct i386_regs regs
Definition: registers.h:15
const char * name
Device name.
Definition: devtree.h:19
int fdt_parent_reg_cells(struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs)
Get parent region cell size specification.
Definition: fdt.c:743
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
void * ioremap(unsigned long bus_addr, size_t len)
Map bus address as an I/O address.
int fdt_reg_size(struct fdt *fdt, unsigned int offset, struct fdt_reg_cells *regs, unsigned int index, uint64_t *size)
Get region size.
Definition: fdt.c:817
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
struct fdt sysfdt
The system flattened device tree (if present)
Definition: fdt.c:44

References address, DBGC, fdt_parent_reg_cells(), fdt_reg_address(), fdt_reg_size(), index, ioremap(), len, dt_device::name, NULL, offset, rc, regs, size, strerror(), and sysfdt.

Referenced by cgem_probe(), dwgpio_group_probe(), dwmac_probe(), dwuart_probe(), and dwusb_probe().

◆ dt_probe_node()

int dt_probe_node ( struct device parent,
unsigned int  offset 
)

Probe devicetree node.

Parameters
parentParent generic device
offsetStarting node offset
Return values
rcReturn status code

Definition at line 196 of file devtree.c.

196  {
197  struct fdt_descriptor desc;
198  struct dt_device *dt;
199  const char *name;
200  int rc;
201 
202  /* Describe token */
203  if ( ( rc = fdt_describe ( &sysfdt, offset, &desc ) ) != 0 )
204  goto err_describe;
205 
206  /* Allocate and initialise device */
207  dt = zalloc ( sizeof ( *dt ) );
208  if ( ! dt ) {
209  rc = -ENOMEM;
210  goto err_alloc;
211  }
212  name = ( offset ? desc.name : "root node" );
213  dt->name = dt->dev.name;
214  snprintf ( dt->dev.name, sizeof ( dt->dev.name ), "%s", name );
217  dt->dev.parent = parent;
218  INIT_LIST_HEAD ( &dt->dev.children );
219  list_add_tail ( &dt->dev.siblings, &parent->children );
220 
221  /* Probe device */
222  if ( ( rc = dt_probe ( dt, offset ) ) != 0 )
223  goto err_probe;
224 
225  return 0;
226 
227  dt_remove ( dt );
228  err_probe:
229  list_del ( &dt->dev.siblings );
230  free ( dt );
231  err_alloc:
232  err_describe:
233  return rc;
234 }
static void dt_remove(struct dt_device *dt)
Remove devicetree device.
Definition: devtree.c:180
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int fdt_describe(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Describe device tree token.
Definition: fdt.c:89
char name[40]
Name.
Definition: device.h:78
struct device * parent
Bus device.
Definition: device.h:88
struct device dev
Generic device.
Definition: devtree.h:21
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
#define ENOMEM
Not enough space.
Definition: errno.h:534
uint32_t fdt_phandle(struct fdt *fdt, unsigned int offset)
Get package handle (phandle) property.
Definition: fdt.c:689
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
A devicetree device.
Definition: devtree.h:17
unsigned int location
Location.
Definition: device.h:29
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:661
struct list_head siblings
Devices on the same bus.
Definition: device.h:84
static int dt_probe(struct dt_device *dt, unsigned int offset)
Probe devicetree device.
Definition: devtree.c:154
#define BUS_TYPE_DT
Devicetree bus type.
Definition: device.h:73
const char * name
Device name.
Definition: devtree.h:19
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
unsigned int bus_type
Bus type.
Definition: device.h:24
struct list_head children
Devices attached to this device.
Definition: device.h:86
struct device_description desc
Device description.
Definition: device.h:82
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
struct fdt sysfdt
The system flattened device tree (if present)
Definition: fdt.c:44

References device_description::bus_type, BUS_TYPE_DT, device::children, desc, device::desc, dt_device::dev, dt_probe(), dt_remove(), ENOMEM, fdt_describe(), fdt_phandle(), free, INIT_LIST_HEAD, list_add_tail, list_del, device_description::location, dt_device::name, device::name, name, offset, device::parent, rc, device::siblings, snprintf(), sysfdt, and zalloc().

Referenced by dt_probe_all(), dt_probe_children(), and fdtcon_default().

◆ dt_remove_node()

void dt_remove_node ( struct device parent)

Remove devicetree node.

Parameters
parentParent generic device

Definition at line 241 of file devtree.c.

241  {
242  struct dt_device *dt;
243 
244  /* Identify most recently added child */
245  dt = list_last_entry ( &parent->children, struct dt_device,
246  dev.siblings );
247  assert ( dt != NULL );
248 
249  /* Remove driver */
250  dt_remove ( dt );
251 
252  /* Delete and free device */
253  list_del ( &dt->dev.siblings );
254  free ( dt );
255 }
static void dt_remove(struct dt_device *dt)
Remove devicetree device.
Definition: devtree.c:180
#define list_last_entry(list, type, member)
Get the container of the last entry in a list.
Definition: list.h:346
struct device dev
Generic device.
Definition: devtree.h:21
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
A devicetree device.
Definition: devtree.h:17
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
struct list_head siblings
Devices on the same bus.
Definition: device.h:84
struct list_head children
Devices attached to this device.
Definition: device.h:86
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321

References assert(), device::children, dt_device::dev, dt_remove(), free, list_del, list_last_entry, NULL, and device::siblings.

Referenced by dt_remove_all(), and dt_remove_children().

◆ dt_probe_children()

int dt_probe_children ( struct dt_device parent,
unsigned int  offset 
)

Probe devicetree children.

Parameters
parentParent device
offsetStarting node offset
Return values
rcReturn status code

Definition at line 264 of file devtree.c.

264  {
265  struct fdt_descriptor desc;
266  int depth;
267  int rc;
268 
269  /* Probe any immediate child nodes */
270  for ( depth = -1 ; ; depth += desc.depth, offset = desc.next ) {
271 
272  /* Describe token */
273  if ( ( rc = fdt_describe ( &sysfdt, offset, &desc ) ) != 0 ) {
274  DBGC ( &sysfdt, "DT %s has malformed node: %s\n",
275  parent->name, strerror ( rc ) );
276  goto err_describe;
277  }
278 
279  /* Terminate when we exit this node */
280  if ( ( depth == 0 ) && ( desc.depth < 0 ) )
281  break;
282 
283  /* Probe child node, if applicable */
284  if ( ( depth == 0 ) && desc.name && ( ! desc.data ) ) {
285  DBGC2 ( &sysfdt, "DT %s is child of %s\n",
286  desc.name, parent->name );
287  dt_probe_node ( &parent->dev, desc.offset );
288  }
289  }
290 
291  /* Fail if we have no children (so that this device will be freed) */
292  if ( list_empty ( &parent->dev.children ) ) {
293  rc = -ENODEV;
294  goto err_no_children;
295  }
296 
297  return 0;
298 
299  err_no_children:
300  err_describe:
301  dt_remove_children ( parent );
302  return rc;
303 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
int depth
Depth change.
Definition: fdt.h:132
void dt_remove_children(struct dt_device *parent)
Remove devicetree children.
Definition: devtree.c:310
int fdt_describe(struct fdt *fdt, unsigned int offset, struct fdt_descriptor *desc)
Describe device tree token.
Definition: fdt.c:89
#define DBGC(...)
Definition: compiler.h:505
struct device dev
Generic device.
Definition: devtree.h:21
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
#define ENODEV
No such device.
Definition: errno.h:509
int dt_probe_node(struct device *parent, unsigned int offset)
Probe devicetree node.
Definition: devtree.c:196
const char * name
Device name.
Definition: devtree.h:19
#define DBGC2(...)
Definition: compiler.h:522
struct list_head children
Devices attached to this device.
Definition: device.h:86
A device tree token descriptor.
Definition: fdt.h:120
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
struct fdt sysfdt
The system flattened device tree (if present)
Definition: fdt.c:44

References device::children, DBGC, DBGC2, fdt_descriptor::depth, desc, dt_device::dev, dt_probe_node(), dt_remove_children(), ENODEV, fdt_describe(), list_empty, dt_device::name, offset, rc, strerror(), and sysfdt.

Referenced by dwgpio_group_probe().

◆ dt_remove_children()

void dt_remove_children ( struct dt_device parent)

Remove devicetree children.

Parameters
parentParent device

Definition at line 310 of file devtree.c.

310  {
311 
312  /* Remove all child nodes */
313  while ( ! list_empty ( &parent->dev.children ) )
314  dt_remove_node ( &parent->dev );
315 }
struct device dev
Generic device.
Definition: devtree.h:21
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
void dt_remove_node(struct device *parent)
Remove devicetree node.
Definition: devtree.c:241
struct list_head children
Devices attached to this device.
Definition: device.h:86

References device::children, dt_device::dev, dt_remove_node(), and list_empty.

Referenced by dt_probe_children(), dwgpio_group_probe(), and dwgpio_group_remove().