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.
26 /* LATER: #include "hwconfig.h" */
27 extern const struct hw_device_descriptor dv_core_descriptor[];
28 extern const struct hw_device_descriptor dv_pal_descriptor[];
29 const struct hw_device_descriptor *hw_descriptors[] = {
51 const struct hw_device_descriptor *descriptor;
52 hw_delete_callback *to_delete;
56 generic_hw_unit_decode (struct hw *bus,
60 memset (phys, 0, sizeof (*phys));
66 const int max_nr_cells = hw_unit_nr_address_cells (bus);
71 val = strtoul (unit, &end, 0);
76 if (nr_cells >= max_nr_cells)
79 phys->cells[nr_cells] = val;
83 if (isspace (*unit) || *unit == '\0')
89 if (nr_cells < max_nr_cells) {
90 /* shift everything to correct position */
92 for (i = 1; i <= nr_cells; i++)
93 phys->cells[max_nr_cells - i] = phys->cells[nr_cells - i];
94 for (i = 0; i < (max_nr_cells - nr_cells); i++)
97 phys->nr_cells = max_nr_cells;
103 generic_hw_unit_encode (struct hw *bus,
111 /* skip leading zero's */
112 for (i = 0; i < phys->nr_cells; i++)
114 if (phys->cells[i] != 0)
117 /* don't output anything if empty */
118 if (phys->nr_cells == 0)
123 else if (i == phys->nr_cells)
131 for (; i < phys->nr_cells; i++)
135 pos = strchr(pos, '\0');
137 if (phys->cells[i] < 10)
138 sprintf (pos, "%ld", (unsigned long)phys->cells[i]);
140 sprintf (pos, "0x%lx", (unsigned long)phys->cells[i]);
141 pos = strchr(pos, '\0');
145 if (len >= sizeof_buf)
146 hw_abort (NULL, "generic_unit_encode - buffer overflow\n");
151 generic_hw_unit_address_to_attach_address (struct hw *me,
152 const hw_unit *address,
154 unsigned_word *attach_address,
158 for (i = 0; i < address->nr_cells - 2; i++)
160 if (address->cells[i] != 0)
161 hw_abort (me, "Only 32bit addresses supported");
163 if (address->nr_cells >= 2)
164 *attach_space = address->cells[address->nr_cells - 2];
167 *attach_address = address->cells[address->nr_cells - 1];
172 generic_hw_unit_size_to_attach_size (struct hw *me,
178 for (i = 0; i < size->nr_cells - 1; i++)
180 if (size->cells[i] != 0)
181 hw_abort (me, "Only 32bit sizes supported");
183 *nr_bytes = size->cells[0];
188 /* ignore/passthrough versions of each function */
191 passthrough_hw_attach_address (struct hw *me,
195 address_word nr_bytes,
196 struct hw *client) /*callback/default*/
198 if (hw_parent (me) == NULL)
199 hw_abort (client, "hw_attach_address: no parent attach method");
200 hw_attach_address (hw_parent (me), level,
201 space, addr, nr_bytes,
206 passthrough_hw_detach_address (struct hw *me,
210 address_word nr_bytes,
211 struct hw *client) /*callback/default*/
213 if (hw_parent (me) == NULL)
214 hw_abort (client, "hw_attach_address: no parent attach method");
215 hw_detach_address (hw_parent (me), level,
216 space, addr, nr_bytes,
221 panic_hw_io_read_buffer (struct hw *me,
229 hw_abort (me, "no io-read method");
234 panic_hw_io_write_buffer (struct hw *me,
242 hw_abort (me, "no io-write method");
247 passthrough_hw_dma_read_buffer (struct hw *me,
253 if (hw_parent (me) == NULL)
254 hw_abort (me, "no parent dma-read method");
255 return hw_dma_read_buffer (hw_parent (me), dest,
256 space, addr, nr_bytes);
260 passthrough_hw_dma_write_buffer (struct hw *me,
265 int violate_read_only_section)
267 if (hw_parent (me) == NULL)
268 hw_abort (me, "no parent dma-write method");
269 return hw_dma_write_buffer (hw_parent (me), source,
272 violate_read_only_section);
275 const struct hw_port_descriptor empty_hw_ports[] = {
280 panic_hw_port_event (struct hw *me,
288 hw_abort (me, "no port method");
292 ignore_hw_delete (struct hw *me)
301 full_name_of_hw (struct hw *leaf,
306 char full_name[1024];
310 sizeof_buf = sizeof (full_name);
313 /* use head recursion to construct the path */
315 if (hw_parent (leaf) == NULL)
319 hw_abort (leaf, "buffer overflow");
326 full_name_of_hw (hw_parent (leaf), buf, sizeof_buf);
327 if (hw_unit_encode (hw_parent (leaf),
328 hw_unit_address (leaf),
335 if (strlen (buf) + strlen ("/") + strlen (hw_name (leaf)) + strlen (unit)
337 hw_abort (leaf, "buffer overflow");
339 strcat (buf, hw_name (leaf));
343 /* return it usefully */
344 if (buf == full_name)
345 buf = (char *) strdup (full_name);
350 hw_create (SIM_DESC sd,
357 /* NOTE: HW must be allocated using ZALLOC, others use HW_ZALLOC */
358 struct hw *hw = ZALLOC (struct hw);
361 hw->family_of_hw = family;
362 hw->name_of_hw = name;
363 hw->args_of_hw = args;
365 /* a hook into the system */
367 hw->system_of_hw = sd;
368 else if (parent != NULL)
369 hw->system_of_hw = hw_system (parent);
371 hw_abort (parent, "No system found");
376 struct hw **sibling = &parent->child_of_hw;
377 while ((*sibling) != NULL)
378 sibling = &(*sibling)->sibling_of_hw;
380 hw->parent_of_hw = parent;
386 struct hw *root = parent;
387 while (root->parent_of_hw != NULL)
388 root = root->parent_of_hw;
389 hw->root_of_hw = root;
392 /* a unique identifier for the device on the parents bus */
395 hw_unit_decode (parent, unit, &hw->unit_address_of_hw);
398 /* Determine our path */
400 hw->path_of_hw = full_name_of_hw (hw, NULL, 0);
402 hw->path_of_hw = "/";
404 /* create our base type */
405 hw->base_of_hw = HW_ZALLOC (hw, struct hw_base_data);
406 hw->base_of_hw->finished_p = 0;
409 set_hw_io_read_buffer (hw, panic_hw_io_read_buffer);
410 set_hw_io_write_buffer (hw, panic_hw_io_write_buffer);
411 set_hw_dma_read_buffer (hw, passthrough_hw_dma_read_buffer);
412 set_hw_dma_write_buffer (hw, passthrough_hw_dma_write_buffer);
413 set_hw_unit_decode (hw, generic_hw_unit_decode);
414 set_hw_unit_encode (hw, generic_hw_unit_encode);
415 set_hw_unit_address_to_attach_address (hw, generic_hw_unit_address_to_attach_address);
416 set_hw_unit_size_to_attach_size (hw, generic_hw_unit_size_to_attach_size);
417 set_hw_attach_address (hw, passthrough_hw_attach_address);
418 set_hw_detach_address (hw, passthrough_hw_detach_address);
419 set_hw_delete (hw, ignore_hw_delete);
421 /* locate a descriptor */
423 const struct hw_device_descriptor **table;
424 for (table = hw_descriptors;
428 const struct hw_device_descriptor *entry;
430 entry->family != NULL;
433 if (strcmp (family, entry->family) == 0)
435 hw->base_of_hw->descriptor = entry;
439 if (hw->base_of_hw->descriptor == NULL)
441 hw_abort (parent, "Unknown device `%s'", family);
445 /* Attach dummy ports */
446 set_hw_ports (hw, empty_hw_ports);
447 set_hw_port_event (hw, panic_hw_port_event);
454 hw_finished_p (struct hw *me)
456 return (me->base_of_hw->finished_p);
460 hw_finish (struct hw *me)
462 if (hw_finished_p (me))
463 hw_abort (me, "Attempt to finish finished device");
465 /* Fill in the (hopefully) defined address/size cells values */
466 if (hw_find_property (me, "#address-cells") != NULL)
467 me->nr_address_cells_of_hw_unit =
468 hw_find_integer_property (me, "#address-cells");
470 me->nr_address_cells_of_hw_unit = 2;
471 if (hw_find_property (me, "#size-cells") != NULL)
472 me->nr_size_cells_of_hw_unit =
473 hw_find_integer_property (me, "#size-cells");
475 me->nr_size_cells_of_hw_unit = 1;
477 /* Fill in the (hopefully) defined trace variable */
478 if (hw_find_property (hw, "trace?") != NULL)
479 hw->trace_of_hw_p = hw_find_boolean_property (hw, "trace?");
481 /* Allow the real device to override any methods */
482 me->base_of_hw->descriptor->to_finish (me);
483 me->base_of_hw->finished_p = 1;
488 hw_delete (struct hw *me)
490 /* give the object a chance to tidy up */
491 me->base_of_hw->to_delete (me);
493 /* now unlink us from the tree */
496 struct hw **sibling = &hw_parent (me)->child_of_hw;
497 while (*sibling != NULL)
501 *sibling = me->sibling_of_hw;
502 me->sibling_of_hw = NULL;
503 me->parent_of_hw = NULL;
509 /* some sanity checks */
510 if (hw_child (me) != NULL)
512 hw_abort (me, "attempt to delete device with children");
514 if (hw_sibling (me) != NULL)
516 hw_abort (me, "attempt to delete device with siblings");
519 /* blow away all memory belonging to the device */
523 zfree (me->base_of_hw);
528 /* Go through the devices various reg properties for those that
529 specify attach addresses */
533 do_hw_attach_regs (struct hw *hw)
535 static const char *(reg_property_names[]) = {
537 "assigned-addresses",
542 const char **reg_property_name;
543 int nr_valid_reg_properties = 0;
544 for (reg_property_name = reg_property_names;
545 *reg_property_name != NULL;
548 if (hw_find_property (hw, *reg_property_name) != NULL)
550 reg_property_spec reg;
553 hw_find_reg_array_property (hw, *reg_property_name, reg_entry,
557 unsigned_word attach_address;
559 unsigned attach_size;
560 if (!hw_unit_address_to_attach_address (hw_parent (hw),
566 if (!hw_unit_size_to_attach_size (hw_parent (hw),
570 hw_attach_address (hw_parent (hw),
572 attach_space, attach_address, attach_size,
574 nr_valid_reg_properties++;
576 /* if first option matches don't try for any others */
577 if (reg_property_name == reg_property_names)