1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, 1998, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #include "hw-config.h"
44 const struct hw_descriptor
*descriptor
;
45 hw_delete_callback
*to_delete
;
49 generic_hw_unit_decode (struct hw
*bus
,
53 memset (phys
, 0, sizeof (*phys
));
59 const int max_nr_cells
= hw_unit_nr_address_cells (bus
);
64 val
= strtoul (unit
, &end
, 0);
69 if (nr_cells
>= max_nr_cells
)
72 phys
->cells
[nr_cells
] = val
;
76 if (isspace (*unit
) || *unit
== '\0')
82 if (nr_cells
< max_nr_cells
) {
83 /* shift everything to correct position */
85 for (i
= 1; i
<= nr_cells
; i
++)
86 phys
->cells
[max_nr_cells
- i
] = phys
->cells
[nr_cells
- i
];
87 for (i
= 0; i
< (max_nr_cells
- nr_cells
); i
++)
90 phys
->nr_cells
= max_nr_cells
;
96 generic_hw_unit_encode (struct hw
*bus
,
104 /* skip leading zero's */
105 for (i
= 0; i
< phys
->nr_cells
; i
++)
107 if (phys
->cells
[i
] != 0)
110 /* don't output anything if empty */
111 if (phys
->nr_cells
== 0)
116 else if (i
== phys
->nr_cells
)
124 for (; i
< phys
->nr_cells
; i
++)
128 pos
= strchr(pos
, '\0');
130 if (phys
->cells
[i
] < 10)
131 sprintf (pos
, "%ld", (unsigned long)phys
->cells
[i
]);
133 sprintf (pos
, "0x%lx", (unsigned long)phys
->cells
[i
]);
134 pos
= strchr(pos
, '\0');
138 if (len
>= sizeof_buf
)
139 hw_abort (NULL
, "generic_unit_encode - buffer overflow\n");
144 generic_hw_unit_address_to_attach_address (struct hw
*me
,
145 const hw_unit
*address
,
147 unsigned_word
*attach_address
,
151 for (i
= 0; i
< address
->nr_cells
- 2; i
++)
153 if (address
->cells
[i
] != 0)
154 hw_abort (me
, "Only 32bit addresses supported");
156 if (address
->nr_cells
>= 2)
157 *attach_space
= address
->cells
[address
->nr_cells
- 2];
160 *attach_address
= address
->cells
[address
->nr_cells
- 1];
165 generic_hw_unit_size_to_attach_size (struct hw
*me
,
171 for (i
= 0; i
< size
->nr_cells
- 1; i
++)
173 if (size
->cells
[i
] != 0)
174 hw_abort (me
, "Only 32bit sizes supported");
176 *nr_bytes
= size
->cells
[0];
181 /* ignore/passthrough versions of each function */
184 passthrough_hw_attach_address (struct hw
*me
,
188 address_word nr_bytes
,
189 struct hw
*client
) /*callback/default*/
191 if (hw_parent (me
) == NULL
)
192 hw_abort (client
, "hw_attach_address: no parent attach method");
193 hw_attach_address (hw_parent (me
), level
,
194 space
, addr
, nr_bytes
,
199 passthrough_hw_detach_address (struct hw
*me
,
203 address_word nr_bytes
,
204 struct hw
*client
) /*callback/default*/
206 if (hw_parent (me
) == NULL
)
207 hw_abort (client
, "hw_attach_address: no parent attach method");
208 hw_detach_address (hw_parent (me
), level
,
209 space
, addr
, nr_bytes
,
214 panic_hw_io_read_buffer (struct hw
*me
,
220 hw_abort (me
, "no io-read method");
225 panic_hw_io_write_buffer (struct hw
*me
,
231 hw_abort (me
, "no io-write method");
236 passthrough_hw_dma_read_buffer (struct hw
*me
,
242 if (hw_parent (me
) == NULL
)
243 hw_abort (me
, "no parent dma-read method");
244 return hw_dma_read_buffer (hw_parent (me
), dest
,
245 space
, addr
, nr_bytes
);
249 passthrough_hw_dma_write_buffer (struct hw
*me
,
254 int violate_read_only_section
)
256 if (hw_parent (me
) == NULL
)
257 hw_abort (me
, "no parent dma-write method");
258 return hw_dma_write_buffer (hw_parent (me
), source
,
261 violate_read_only_section
);
265 ignore_hw_delete (struct hw
*me
)
274 full_name_of_hw (struct hw
*leaf
,
279 char full_name
[1024];
283 sizeof_buf
= sizeof (full_name
);
286 /* use head recursion to construct the path */
288 if (hw_parent (leaf
) == NULL
)
292 hw_abort (leaf
, "buffer overflow");
299 full_name_of_hw (hw_parent (leaf
), buf
, sizeof_buf
);
300 if (hw_unit_encode (hw_parent (leaf
),
301 hw_unit_address (leaf
),
308 if (strlen (buf
) + strlen ("/") + strlen (hw_name (leaf
)) + strlen (unit
)
310 hw_abort (leaf
, "buffer overflow");
312 strcat (buf
, hw_name (leaf
));
316 /* return it usefully */
317 if (buf
== full_name
)
318 buf
= hw_strdup (leaf
, full_name
);
323 hw_create (struct sim_state
*sd
,
330 /* NOTE: HW must be allocated using ZALLOC, others use HW_ZALLOC */
331 struct hw
*hw
= ZALLOC (struct hw
);
334 hw
->family_of_hw
= hw_strdup (hw
, family
);
335 hw
->name_of_hw
= hw_strdup (hw
, name
);
336 hw
->args_of_hw
= hw_strdup (hw
, args
);
338 /* a hook into the system */
340 hw
->system_of_hw
= sd
;
341 else if (parent
!= NULL
)
342 hw
->system_of_hw
= hw_system (parent
);
344 hw_abort (parent
, "No system found");
349 struct hw
**sibling
= &parent
->child_of_hw
;
350 while ((*sibling
) != NULL
)
351 sibling
= &(*sibling
)->sibling_of_hw
;
353 hw
->parent_of_hw
= parent
;
359 struct hw
*root
= parent
;
360 while (root
->parent_of_hw
!= NULL
)
361 root
= root
->parent_of_hw
;
362 hw
->root_of_hw
= root
;
365 /* a unique identifier for the device on the parents bus */
368 hw_unit_decode (parent
, unit
, &hw
->unit_address_of_hw
);
371 /* Determine our path */
373 hw
->path_of_hw
= full_name_of_hw (hw
, NULL
, 0);
375 hw
->path_of_hw
= "/";
377 /* create our base type */
378 hw
->base_of_hw
= HW_ZALLOC (hw
, struct hw_base_data
);
379 hw
->base_of_hw
->finished_p
= 0;
382 set_hw_io_read_buffer (hw
, panic_hw_io_read_buffer
);
383 set_hw_io_write_buffer (hw
, panic_hw_io_write_buffer
);
384 set_hw_dma_read_buffer (hw
, passthrough_hw_dma_read_buffer
);
385 set_hw_dma_write_buffer (hw
, passthrough_hw_dma_write_buffer
);
386 set_hw_unit_decode (hw
, generic_hw_unit_decode
);
387 set_hw_unit_encode (hw
, generic_hw_unit_encode
);
388 set_hw_unit_address_to_attach_address (hw
, generic_hw_unit_address_to_attach_address
);
389 set_hw_unit_size_to_attach_size (hw
, generic_hw_unit_size_to_attach_size
);
390 set_hw_attach_address (hw
, passthrough_hw_attach_address
);
391 set_hw_detach_address (hw
, passthrough_hw_detach_address
);
392 set_hw_delete (hw
, ignore_hw_delete
);
394 /* locate a descriptor */
396 const struct hw_descriptor
**table
;
397 for (table
= hw_descriptors
;
401 const struct hw_descriptor
*entry
;
403 entry
->family
!= NULL
;
406 if (strcmp (family
, entry
->family
) == 0)
408 hw
->base_of_hw
->descriptor
= entry
;
413 if (hw
->base_of_hw
->descriptor
== NULL
)
415 hw_abort (parent
, "Unknown device `%s'", family
);
419 /* Attach dummy ports */
420 create_hw_alloc_data (hw
);
421 create_hw_property_data (hw
);
422 create_hw_port_data (hw
);
423 create_hw_event_data (hw
);
430 hw_finished_p (struct hw
*me
)
432 return (me
->base_of_hw
->finished_p
);
436 hw_finish (struct hw
*me
)
438 if (hw_finished_p (me
))
439 hw_abort (me
, "Attempt to finish finished device");
441 /* Fill in the (hopefully) defined address/size cells values */
442 if (hw_find_property (me
, "#address-cells") != NULL
)
443 me
->nr_address_cells_of_hw_unit
=
444 hw_find_integer_property (me
, "#address-cells");
446 me
->nr_address_cells_of_hw_unit
= 2;
447 if (hw_find_property (me
, "#size-cells") != NULL
)
448 me
->nr_size_cells_of_hw_unit
=
449 hw_find_integer_property (me
, "#size-cells");
451 me
->nr_size_cells_of_hw_unit
= 1;
453 /* Fill in the (hopefully) defined trace variable */
454 if (hw_find_property (me
, "trace?") != NULL
)
455 me
->trace_of_hw_p
= hw_find_boolean_property (me
, "trace?");
456 /* allow global variable to define default tracing */
457 else if (! hw_trace_p (me
)
458 && hw_find_property (hw_root (me
), "global-trace?") != NULL
459 && hw_find_boolean_property (hw_root (me
), "global-trace?"))
460 me
->trace_of_hw_p
= 1;
463 /* Allow the real device to override any methods */
464 me
->base_of_hw
->descriptor
->to_finish (me
);
465 me
->base_of_hw
->finished_p
= 1;
470 hw_delete (struct hw
*me
)
472 /* give the object a chance to tidy up */
473 me
->base_of_hw
->to_delete (me
);
475 delete_hw_event_data (me
);
476 delete_hw_port_data (me
);
477 delete_hw_property_data (me
);
479 /* now unlink us from the tree */
482 struct hw
**sibling
= &hw_parent (me
)->child_of_hw
;
483 while (*sibling
!= NULL
)
487 *sibling
= me
->sibling_of_hw
;
488 me
->sibling_of_hw
= NULL
;
489 me
->parent_of_hw
= NULL
;
495 /* some sanity checks */
496 if (hw_child (me
) != NULL
)
498 hw_abort (me
, "attempt to delete device with children");
500 if (hw_sibling (me
) != NULL
)
502 hw_abort (me
, "attempt to delete device with siblings");
505 /* blow away all memory belonging to the device */
506 delete_hw_alloc_data (me
);
509 zfree (me
->base_of_hw
);
514 /* Go through the devices various reg properties for those that
515 specify attach addresses */
519 do_hw_attach_regs (struct hw
*hw
)
521 static const char *(reg_property_names
[]) = {
523 "assigned-addresses",
528 const char **reg_property_name
;
529 int nr_valid_reg_properties
= 0;
530 for (reg_property_name
= reg_property_names
;
531 *reg_property_name
!= NULL
;
534 if (hw_find_property (hw
, *reg_property_name
) != NULL
)
536 reg_property_spec reg
;
539 hw_find_reg_array_property (hw
, *reg_property_name
, reg_entry
,
543 unsigned_word attach_address
;
545 unsigned attach_size
;
546 if (!hw_unit_address_to_attach_address (hw_parent (hw
),
552 if (!hw_unit_size_to_attach_size (hw_parent (hw
),
556 hw_attach_address (hw_parent (hw
),
558 attach_space
, attach_address
, attach_size
,
560 nr_valid_reg_properties
++;
562 /* if first option matches don't try for any others */
563 if (reg_property_name
== reg_property_names
)