summaryrefslogtreecommitdiffstats
path: root/bsps/include/ofw/ofw.h
blob: 411010be8994ce5c7a58c3b8cf5ef29a98bc5d2a (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/* SPDX-License-Identifier: BSD-2-Clause */

/**
 * @file
 *
 * @ingroup ofw
 *
 * RTEMS FDT implementation of OpenFirmWare Interface.
 *
 * RTEMS OFW is a FDT only implementation of the OpenFirmWare interface.
 * This API is created to be compatible with FreeBSD OpenFirmWare interface.
 * The main intention is to make porting of FreeBSD drivers to RTEMS easier.
 */

/*
 * Copyright (C) 2020 Niteesh Babu G S <niteesh.gs@gmail.com>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _OFW_H
#define _OFW_H

#ifdef __cplusplus
extern "C" {
#endif

#include <rtems.h>
#include <stdint.h>
#include <sys/types.h>

/**
 * Represents a node in the device tree. The offset from fdtp to node is used
 * instead of fdt offset to make sure this is compatible with OF interface in
 * FreeBSD.
 */
typedef uint32_t  phandle_t;
/**
 * Represents the effective phandle of the node.
 */
typedef uint32_t  ihandle_t;
/**
 * Represents the data type of the buffer.
 */
typedef uint32_t  pcell_t;

/**
 * @struct rtems_ofw_memory_area
 *
 * This is used to represent elements(FDT properties) that define
 * region of memory address.
 */
typedef struct {
  /** The start address of the memory region. */
  uint32_t start;
  /** The size of the memory region. */
  uint32_t size;
} rtems_ofw_memory_area;

/**
 * @struct rtems_ofw_ranges
 *
 * This is used to represent the ranges property in the device tree.
 */
typedef struct {
  /** The physical address within the child bus address space */
  uint32_t child_bus;
  /** The physical address within the parent bus address space */
  uint32_t parent_bus;
  /** The size of the range in the child’s address space */
  uint32_t size;
} rtems_ofw_ranges;

/**
 * @brief Gets the node that is next to @a node.
 *
 * @param[in] node Node offset
 *
 * @retval Peer node offset Successful operation.
 * @retval 0 No peer node or invalid node handle
 */
phandle_t rtems_ofw_peer(
  phandle_t   node
);

/**
 * @brief Gets the node that is the child of @a node.
 *
 * @param[in] node Node offset
 *
 * @retval child node offset Successful operation.
 * @retval 0 No child node or invalid node handle
 */
phandle_t rtems_ofw_child(
  phandle_t   node
);

/**
 * @brief Gets the node that is the parent of @a node.
 *
 * @param[in] node Node offset
 *
 * @retval child node offset Successful operation.
 * @retval 0 No child node or invalid node handle
 */
phandle_t rtems_ofw_parent(
  phandle_t   node
);

/**
 * @brief Gets the length of the property mentioned in @a propname.
 *
 * @param[in] node Node offset
 * @param[in] prop Property name
 *
 * @retval -1 Invalid node or property
 * @retval  Length of property on successful operation
 */
ssize_t rtems_ofw_get_prop_len(
  phandle_t   node,
  const char *propname
);

/**
 * @brief Gets the value of property mentioned in @a propname.
 *
 * @param[in] node Node offset
 * @param[in] prop Property name
 * @param[out] buf The property value gets stored in this buffer (Pre-allocated)
 * @param[in] len Length of the buffer
 
 * @retval -1 Invalid node or property
 * @retval  Length of property on successful operation
 */
ssize_t rtems_ofw_get_prop(
  phandle_t    node,
  const char  *propname,
  void        *buf,
  size_t       len
);

/**
 * @brief Gets the value of property mentioned in @a prop.
 * 
 * Gets the value of property of @a node and converts the value into the host's
 * endianness.
 *
 * @param[in] node Node offset
 * @param[in] prop Property name
 * @param[out] buf The property value gets stored in this buffer(Pre-allocated)
 * after converted to the host's endianness
 * @param[in] len Length of the buffer
 *
 * @retval -1 Invalid node or property
 * @retval  Length of property on successful operation
 */
ssize_t rtems_ofw_get_enc_prop(
  phandle_t    node,
  const char  *prop,
  pcell_t     *buf,
  size_t       len
);

/**
 * @brief Checks if the property @a propname is present in @a node.
 *
 * @param[in] node Node offset
 * @param[in] propname Property name
 *
 *  @retval  0 Property not present.
 *  @retval  1 Property is present.
 */
int rtems_ofw_has_prop(
  phandle_t    node,
  const char  *propname
);

/**
 * @brief Searches for property @a propname in @a node.
 * 
 * Searches the node and its parent recursively for the property and fills the
 * buffer with the first found value.
 *
 * @param[in] node Node offset
 * @param[in] propname Property name
 * @param[out] buf The property value gets stored in this buffer (Pre-allocated)
 * @param[in] len Length of the buffer
 *
 * @retval  Length of the property if property is found.
 * @retval  -1 Property is not found.
 */
ssize_t rtems_ofw_search_prop(
  phandle_t    node,
  const char  *propname,
  void        *buf,
  size_t       len
);

/**
 * @brief Searches for property @a propname in @a node.
 * 
 * Searches the node and its parent recursively for the property and fills the 
 * buffer with the first value found after converting it to the endianness of
 * the host.
 *
 * @param[in] node Node offset
 * @param[in] propname Property name
 * @param[out] buf The property value gets stored in this buffer(Pre-allocated)
 * after converted to the host's endianness
 * @param[in] len Length of the buffer
 *
 * @retval  Length of the property if property is found.
 * @retval  -1 Property is not found.
 */
ssize_t rtems_ofw_search_enc_prop(
  phandle_t    node,
  const char  *propname,
  pcell_t     *buf,
  size_t       len
);

/**
 * @brief Gets the value of property mentioned in @a propname.
 *
 * Same as rtems_ofw_getprop, but the @a buf is allocated in this routine and
 * the user is responsible for freeing it.
 * 
 * @param[in] node Node offset
 * @param[in] propname Property name
 * @param[out] buf The buffer is allocated in this routine and user is
 * responsible for freeing.
 *
 * @retval  -1 Property is not found.
 * @retval  Length of the property if property is found.
 */
ssize_t rtems_ofw_get_prop_alloc(
  phandle_t    node,
  const char  *propname,
  void       **buf
);

/**
 * @brief Gets multiple values of the property @a propname.
 * 
 * Same as rtems_ofw_getprop_alloc but it can read properties with multiple
 * values.
 * For eg: reg = <0x1000 0x10 0x2000 0x20>
 *
 * @param[in] node Node offset
 * @param[in] propname Property name
 * @param[in] elsz Size of the single value
 * @param[out] buf The buffer is allocated in this routine and user is
 * responsible for freeing.
 *
 * @retval  -1 Property is not found.
 * @retval   Number of values read.
 */
ssize_t rtems_ofw_get_prop_alloc_multi(
  phandle_t    node,
  const char  *propname,
  int          elsz,
  void       **buf
);

/**
 * @brief Gets the value of property mentioned in @a propname.
 * 
 * Same as rtems_ofw_getprop_alloc but the value stored in the buffer is
 * converted into the host's endianness.
 *
 * @param[in] node Node offset
 * @param[in] propname Property name
 * @param[out] buf The buffer is allocated in this routine and user is
 * responsible for freeing.
 *
 * @retval  -1 Property is not found.
 * @retval  Length of the property if property is found.
 */
ssize_t rtems_ofw_get_enc_prop_alloc(
  phandle_t    node,
  const char  *propname,
  void       **buf
);

/**
 * @brief Gets multiple values of the property @a propname.
 *
 * Same as rtems_ofw_getprop_alloc_multi but the values stored in the buffer
 * are converted to the host's endianness.
 * 
 * @param[in] node Node offset
 * @param[in] propname Property name
 * @param[in] elsz Size of the single value
 * @param[out] buf The buffer is allocated in this routine and user is
 * responsible for freeing.
 *
 * @retval  -1 Property is not found.
 * @retval   Number of values read.
 */
ssize_t rtems_ofw_get_enc_prop_alloc_multi(
  phandle_t     node,
  const char   *propname,
  int           elsz,
  void        **buf
);

/**
 * @brief Free's the buffers allocated by the rtems_ofw_*_alloc functions.
 *
 * @param[in] buf Buffer to be freed
 */
void rtems_ofw_free(
  void   *buf
);

/**
 * @brief Finds the next property of @a node.
 * 
 * Finds the next property of the node and when @a propname is NULL it returns
 * the value in the first property.
 *
 * @param[in] node Node offset
 * @param[in] previous Previous property name
 * @param[out] buf The value of the next property gets stored in this buffer
 * (Pre-allocated)
 * @param[in] len Length of the buffer
 *
 * @retval  -1 node or previous property does not exist
 * @retval   0 no more properties
 * @retval   1 success
 */
int rtems_ofw_next_prop(
  phandle_t    node,
  const char  *previous,
  char        *buf,
  size_t       len
);

/**
 * @brief Sets the property @name of @a node to @a buf.
 *
 * @param[in] node Node offset
 * @param[in] name Property name
 * @param[in] buf Buffer contains the value to be set.
 * @param[in] len Length of the buffer
 *
 * @retval  -1 node does not exist
 * @retval   0 on success
 * @retval   libFDT error codes on unsuccessful setting operation
 */
int rtems_ofw_set_prop(
  phandle_t    node,
  const char  *name,
  const void  *buf,
  size_t       len
);

/**
 * @brief Converts a device specifier to a fully qualified path name.
 *
 * @param[in] dev device specifier
 * @param[out] buf The path is filled into the buffer(Pre-allocated)
 * @param[in] length of the buffer
 *
 * @retval  -1 always. Unimplemented.
 */
ssize_t rtems_ofw_canon(
  const char  *dev,
  char        *buf,
  size_t       len
);

/**
 * @brief Finds the node at the given path
 *
 * @param[in] path to the node from root
 *
 * @retval  -1 node does not exist
 * @retval   node handle on success
 */
phandle_t rtems_ofw_find_device(
  const char  *path
);

/**
 * @brief This routine converts effective phandle @a xref to node offset.
 *
 * @param[in] xref Node phandle
 *
 * @retval Node offset on success
 * @retval Node phandle on failure
 */
phandle_t rtems_ofw_node_from_xref(
  phandle_t   xref
);

/**
 * @brief This routine converts node offset to effective phandle of @a node.
 *
 * @retval Node phandle on success
 * @retval Node offset on failure
 */
phandle_t rtems_ofw_xref_from_node(
  phandle_t   node
);

/*
 * instance handles(ihandle_t) as same as phandles in the FDT implementation
 * of OF interface.
 */

/**
 * @brief Converts @a instance handle to phandle.
 *
 * instance are same as node offsets in FDT.
 * 
 * @param[in] instance Node offset
 * 
 * @retval phandle of node on success.
 * @retval instance of node on failure.
 */
phandle_t rtems_ofw_instance_to_package(
  ihandle_t   instance
);

/**
 * @brief Find the node's path from phandle.
 * 
 * @param[in] node Node offset
 * @param[out] buf Path is filled into this buffer(Pre-allocated).
 * @param[in] len Length of the buffer.
 *
 * @retval -1 always. Unimplemented.
 */
ssize_t rtems_ofw_package_to_path(
  phandle_t   node,
  char       *buf,
  size_t      len
);

/**
 * @brief Find the node's path from ihandle.
 * 
 * @param[in] instance Node offset
 * @param[out] buf Path is filled into this buffer(Pre-allocated).
 * @param[in] len Length of the buffer.
 *
 * @retval -1 always. Unimplemented.
 */
ssize_t rtems_ofw_instance_to_path(
  ihandle_t  instance,
  char      *buf,
  size_t     len
);

/**
 * @brief Queries the node's reg value.
 *
 * This routine fills the buffer @a buf with the values in reg property of node
 * @a node. It reads all the values of the property and fills the buffer to max
 * size.
 * This routine is local to RTEMS OFW and does not have an corresponding
 * FreeBSD OFW pair.
 *
 * @param[in] node Node offset
 * @param[out] buf Register values are stored in this buffer(Pre-allocated).
 * @param[in] size Length of the buffer.
 *
 * @retval -1 If reg property is missing.
 * @retval  Length of the reg property in bytes.
 */
int rtems_ofw_get_reg(
  phandle_t              node,
  rtems_ofw_memory_area *buf,
  size_t                 size
);

/**
 * @brief Queries the node's interrupt value.
 *
 * This routine fills the buffer @a buf with the interrupt number. In case of
 * multiple numbers it fills the buffer to its full extent.
 * This routine is local to RTEMS OFW and does not have an corresponding
 * FreeBSD OFW pair.
 *
 * @param[in] node Node offset
 * @param[out] buf Interrupt values are stored in this buffer(Pre-allocated).
 * @param[in] size Length of the buffer.
 *
 * @retval -1 If interrupts property is missing.
 * @retval  The number of interrupt numbers read.
 */
int rtems_ofw_get_interrupts(
  phandle_t            node,
  rtems_vector_number *buf,
  size_t               size
);

/**
 * @brief Queries the node's status.
 *
 * This routine is local to RTEMS OFW and does not have an corresponding
 * FreeBSD OFW pair.
 *
 * @param[in] node Node offset
 *
 * @retval true  Status is OK or empty.
 * @retval false Status is not OK or missing.
 */
bool rtems_ofw_node_status( phandle_t node );

/**
 * @brief Gets node phandle from compatible property.
 *
 * This routine is local to RTEMS OFW and does not have an corresponding
 * FreeBSD OFW pair.
 *
 * @param[in] compat Compatible string
 *
 * @retval 0 Node with @a compat as compatible string not found
 * @retval Node phandle on success.
 */
phandle_t rtems_ofw_find_device_by_compat( const char *compat );

#ifdef __cplusplus
}
#endif

#endif /* _OFW_H */