1 /* This file is part of the program psim.
3 Copyright (C) 1996, 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 3 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, see <http://www.gnu.org/licenses/>.
24 #include "device_table.h"
31 ide - Integrated Disk Electronics
37 This device models the primary/secondary <<ide>> controller
38 described in the [CHRPIO] document.
40 The controller has separate independant interrupt outputs for each
49 The <<reg>> property is described in the document [CHRPIO].
52 ready-delay = <integer> (optional)
54 If present, this specifies the time that the <<ide>> device takes
55 to complete an I/O operation.
58 disk@?/ide-byte-count = <integer> (optional)
60 disk@?/ide-sector-count = <integer> (optional)
62 disk@?/ide-head-count = <integer> (optional)
64 The <<ide>> device checks each child (disk device) node to see if
65 it has the above properties. If present, these values will be used
66 to compute the <<LBA>> address in <<CHS>> addressing mode.
77 Attach the <<ide>> device to the <<pci>> bus at slot one. Specify
80 | -o '/phb/ide@1/assigned-addresses \
86 | -o '/phb@0x80000000/ide@1/reg \
94 Note: the fouth and fifth reg entries specify that the register is
95 at an offset into the address specified by the base register
96 (<<assigned-addresses>>); Apart from restrictions placed by the
97 <<pci>> specification, no restrictions are placed on the number of
98 base registers specified by the <<assigned-addresses>> property.
100 Attach a <<disk>> to the primary and a <<cdrom>> to the secondary
103 | -o '/phb@0x80000000/ide@1/disk@0/file "zero' \
104 | -o '/phb@0x80000000/ide@1/cdrom@2/file "/dev/cdrom"' \
106 Connect the two interrupt outputs (a and b) to a <<glue>> device to
107 allow testing of the interrupt port. In a real simulation they
108 would be wired to the interrupt controller.
110 | -o '/phb@0x80000000/glue@2/reg 2 0 ni0,0,0,0 8' \
111 | -o '/phb@0x80000000/ide@1 > a 0 /phb@0x80000000/glue@2' \
112 | -o '/phb@0x80000000/ide@1 > b 1 /phb@0x80000000/glue@2'
118 While the DMA registers are present, DMA support has not yet been
121 The number of supported commands is very limited.
123 The standards documents appear to be vague on how to specify the
124 <<unit-address>> of disk devices devices being attached to the
125 <<ide>> controller. I've chosen to use integers with devices zero
126 and one going to the primary controller while two and three are
127 connected to the secondary controller.
133 [CHRPIO] PowerPC(tm) Microprocessor Common Hardware Reference
134 Platform: I/O Device Reference. http://chrp.apple.com/???.
136 [SCHMIDT] The SCSI Bus and IDE Interface - Protocols, Applications
137 and Programming. Friedhelm Schmidt (translated by Michael
138 Schultz). ISBN 0-201-42284-0. Addison-Wesley Publishing Company.
145 typedef enum _io_direction
{
152 nr_ide_controllers
= 2,
153 nr_ide_drives_per_controller
= 2,
154 nr_fifo_entries
= 8192,
158 /* command register block - read */
160 ide_error_reg
, /*ide_feature_reg*/
161 ide_sector_count_reg
,
162 ide_sector_number_reg
,
166 ide_status_reg
, /*ide_command_reg*/
167 /* command register block - write */
168 ide_feature_reg
, /*ide_error_reg*/
169 ide_command_reg
, /*ide_status_reg*/
170 /* control register block - read */
171 ide_alternate_status_reg
, /*ide_control_reg*/
172 ide_control_reg
, /*ide_alternate_status_reg*/
173 /* dma register block */
175 ide_dma_unused_1_reg
,
177 ide_dma_unused_3_reg
,
178 ide_dma_prd_table_address_reg0
,
179 ide_dma_prd_table_address_reg1
,
180 ide_dma_prd_table_address_reg2
,
181 ide_dma_prd_table_address_reg3
,
186 typedef enum _ide_states
{
197 ide_state_name(ide_states state
)
200 case idle_state
: return "idle";
201 case busy_loaded_state
: return "busy_loaded_state";
202 case busy_drained_state
: return "busy_drained_state";
203 case busy_dma_state
: return "busy_dma_state";
204 case busy_command_state
: return "busy_command_state";
205 case loading_state
: return "loading_state";
206 case draining_state
: return "draining_state";
207 default: return "illegal-state";
211 typedef struct _ide_geometry
{
217 typedef struct _ide_drive
{
220 ide_geometry geometry
;
221 ide_geometry default_geometry
;
224 typedef struct _ide_controller
{
227 unsigned8 reg
[nr_ide_registers
];
228 unsigned8 fifo
[nr_fifo_entries
];
231 ide_drive
*current_drive
;
233 int current_transfer
;
234 ide_drive drive
[nr_ide_drives_per_controller
];
236 event_entry_tag event_tag
;
238 signed64 ready_delay
;
244 set_interrupt(device
*me
,
245 ide_controller
*controller
)
247 if ((controller
->reg
[ide_control_reg
] & 0x2) == 0) {
248 DTRACE(ide
, ("controller %d - interrupt set\n", controller
->nr
));
249 device_interrupt_event(me
, controller
->nr
, 1, NULL
, 0);
250 controller
->is_interrupting
= 1;
256 clear_interrupt(device
*me
,
257 ide_controller
*controller
)
259 if (controller
->is_interrupting
) {
260 DTRACE(ide
, ("controller %d - interrupt clear\n", controller
->nr
));
261 device_interrupt_event(me
, controller
->nr
, 0, NULL
, 0);
262 controller
->is_interrupting
= 0;
270 ide_controller
*controller
= data
;
271 device
*me
= controller
->me
;
272 controller
->event_tag
= 0;
273 switch (controller
->state
) {
274 case busy_loaded_state
:
275 case busy_drained_state
:
276 if (controller
->current_transfer
> 0) {
277 controller
->state
= (controller
->state
== busy_loaded_state
278 ? loading_state
: draining_state
);
281 controller
->state
= idle_state
;
283 set_interrupt(me
, controller
);
286 device_error(me
, "controller %d - unexpected event", controller
->nr
);
293 schedule_ready_event(device
*me
,
294 ide_controller
*controller
)
296 if (controller
->event_tag
!= 0)
297 device_error(me
, "controller %d - attempting to schedule multiple events",
299 controller
->event_tag
=
300 device_event_queue_schedule(me
, controller
->ready_delay
,
301 do_event
, controller
);
306 do_fifo_read(device
*me
,
307 ide_controller
*controller
,
311 if (controller
->state
!= draining_state
)
312 device_error(me
, "controller %d - reading fifo when not ready (%s)",
314 ide_state_name(controller
->state
));
315 if (controller
->fifo_pos
+ nr_bytes
> controller
->fifo_size
)
316 device_error(me
, "controller %d - fifo underflow", controller
->nr
);
318 memcpy(dest
, &controller
->fifo
[controller
->fifo_pos
], nr_bytes
);
319 controller
->fifo_pos
+= nr_bytes
;
321 if (controller
->fifo_pos
== controller
->fifo_size
) {
322 controller
->current_transfer
-= 1;
323 if (controller
->current_transfer
> 0
324 && controller
->current_drive
!= NULL
) {
325 DTRACE(ide
, ("controller %d:%d - reading %d byte block at 0x%x\n",
327 controller
->current_drive
->nr
,
328 controller
->fifo_size
,
329 controller
->current_byte
));
330 if (device_io_read_buffer(controller
->current_drive
->device
,
332 0, controller
->current_byte
,
333 controller
->fifo_size
,
335 != controller
->fifo_size
)
336 device_error(me
, "controller %d - disk %s io read error",
338 device_path(controller
->current_drive
->device
));
340 controller
->state
= busy_drained_state
;
341 controller
->fifo_pos
= 0;
342 controller
->current_byte
+= controller
->fifo_size
;
343 schedule_ready_event(me
, controller
);
349 do_fifo_write(device
*me
,
350 ide_controller
*controller
,
354 if (controller
->state
!= loading_state
)
355 device_error(me
, "controller %d - writing fifo when not ready (%s)",
357 ide_state_name(controller
->state
));
358 if (controller
->fifo_pos
+ nr_bytes
> controller
->fifo_size
)
359 device_error(me
, "controller %d - fifo overflow", controller
->nr
);
361 memcpy(&controller
->fifo
[controller
->fifo_pos
], source
, nr_bytes
);
362 controller
->fifo_pos
+= nr_bytes
;
364 if (controller
->fifo_pos
== controller
->fifo_size
) {
365 if (controller
->current_transfer
> 0
366 && controller
->current_drive
!= NULL
) {
367 DTRACE(ide
, ("controller %d:%d - writing %d byte block at 0x%x\n",
369 controller
->current_drive
->nr
,
370 controller
->fifo_size
,
371 controller
->current_byte
));
372 if (device_io_write_buffer(controller
->current_drive
->device
,
374 0, controller
->current_byte
,
375 controller
->fifo_size
,
377 != controller
->fifo_size
)
378 device_error(me
, "controller %d - disk %s io write error",
380 device_path(controller
->current_drive
->device
));
382 controller
->current_transfer
-= 1;
383 controller
->fifo_pos
= 0;
384 controller
->current_byte
+= controller
->fifo_size
;
385 controller
->state
= busy_loaded_state
;
386 schedule_ready_event(me
, controller
);
392 setup_fifo(device
*me
,
393 ide_controller
*controller
,
396 io_direction direction
)
400 int drive_nr
= (controller
->reg
[ide_drive_head_reg
] & 0x10) != 0;
401 controller
->current_drive
= &controller
->drive
[drive_nr
];
404 controller
->current_drive
= NULL
;
407 /* number of transfers */
409 controller
->current_transfer
= 1;
411 int sector_count
= controller
->reg
[ide_sector_count_reg
];
412 if (sector_count
== 0)
413 controller
->current_transfer
= 256;
415 controller
->current_transfer
= sector_count
;
418 /* the transfer size */
419 if (controller
->current_drive
== NULL
)
420 controller
->fifo_size
= 512;
422 controller
->fifo_size
= controller
->current_drive
->geometry
.byte
;
425 controller
->fifo_pos
= 0;
427 /* the starting address */
428 if (controller
->current_drive
== NULL
)
429 controller
->current_byte
= 0;
430 else if (controller
->reg
[ide_drive_head_reg
] & 0x40) {
431 /* LBA addressing mode */
432 controller
->current_byte
= controller
->fifo_size
433 * (((controller
->reg
[ide_drive_head_reg
] & 0xf) << 24)
434 | (controller
->reg
[ide_cylinder_reg1
] << 16)
435 | (controller
->reg
[ide_cylinder_reg0
] << 8)
436 | (controller
->reg
[ide_sector_number_reg
]));
438 else if (controller
->current_drive
->geometry
.head
!= 0
439 && controller
->current_drive
->geometry
.sector
!= 0) {
440 /* CHS addressing mode */
441 int head_nr
= controller
->reg
[ide_drive_head_reg
] & 0xf;
442 int cylinder_nr
= ((controller
->reg
[ide_cylinder_reg1
] << 8)
443 | controller
->reg
[ide_cylinder_reg0
]);
444 int sector_nr
= controller
->reg
[ide_sector_number_reg
];
445 controller
->current_byte
= controller
->fifo_size
446 * ((cylinder_nr
* controller
->current_drive
->geometry
.head
+ head_nr
)
447 * controller
->current_drive
->geometry
.sector
+ sector_nr
- 1);
450 device_error(me
, "controller %d:%d - CHS addressing disabled",
451 controller
->nr
, controller
->current_drive
->nr
);
452 DTRACE(ide
, ("controller %ld:%ld - transfer (%s) %ld blocks of %ld bytes from 0x%lx\n",
453 (long)controller
->nr
,
454 controller
->current_drive
== NULL
? -1L : (long)controller
->current_drive
->nr
,
455 direction
== is_read
? "read" : "write",
456 (long)controller
->current_transfer
,
457 (long)controller
->fifo_size
,
458 (unsigned long)controller
->current_byte
));
461 /* force a primeing read */
462 controller
->current_transfer
+= 1;
463 controller
->state
= draining_state
;
464 controller
->fifo_pos
= controller
->fifo_size
;
465 do_fifo_read(me
, controller
, NULL
, 0);
468 controller
->state
= loading_state
;
475 do_command(device
*me
,
476 ide_controller
*controller
,
479 if (controller
->state
!= idle_state
)
480 device_error(me
, "controller %d - command when not idle", controller
->nr
);
482 case 0x20: case 0x21: /* read-sectors */
483 setup_fifo(me
, controller
, 0/*is_simple*/, 1/*is_with_disk*/, is_read
);
485 case 0x30: case 0x31: /* write */
486 setup_fifo(me
, controller
, 0/*is_simple*/, 1/*is_with_disk*/, is_write
);
492 get_status(device
*me
,
493 ide_controller
*controller
)
495 switch (controller
->state
) {
498 return 0x08; /* data req */
499 case busy_loaded_state
:
500 case busy_drained_state
:
501 return 0x80; /* busy */
503 return 0x40; /* drive ready */
505 device_error(me
, "internal error");
511 /* The address presented to the IDE controler is decoded and then
512 mapped onto a controller:reg pair */
515 nr_address_blocks
= 6,
518 typedef struct _address_block
{
520 unsigned_word base_addr
;
521 unsigned_word bound_addr
;
526 typedef struct _address_decoder
{
527 address_block block
[nr_address_blocks
];
531 decode_address(device
*me
,
532 address_decoder
*decoder
,
534 unsigned_word address
,
537 io_direction direction
)
540 for (i
= 0; i
< nr_address_blocks
; i
++) {
541 if (space
== decoder
->block
[i
].space
542 && address
>= decoder
->block
[i
].base_addr
543 && address
<= decoder
->block
[i
].bound_addr
) {
544 *controller
= decoder
->block
[i
].controller
;
546 - decoder
->block
[i
].base_addr
547 + decoder
->block
[i
].base_reg
);
548 if (direction
== is_write
) {
550 case ide_error_reg
: *reg
= ide_feature_reg
; break;
551 case ide_status_reg
: *reg
= ide_command_reg
; break;
552 case ide_alternate_status_reg
: *reg
= ide_control_reg
; break;
559 device_error(me
, "address %d:0x%lx invalid",
560 space
, (unsigned long)address
);
565 build_address_decoder(device
*me
,
566 address_decoder
*decoder
)
569 for (reg
= 1; reg
< 6; reg
++) {
570 reg_property_spec unit
;
572 unsigned_word address
;
574 /* find and decode the reg property */
575 if (!device_find_reg_array_property(me
, "reg", reg
, &unit
))
576 device_error(me
, "missing or invalid reg entry %d", reg
);
577 device_address_to_attach_address(device_parent(me
), &unit
.address
,
578 &space
, &address
, me
);
579 device_size_to_attach_size(device_parent(me
), &unit
.size
, &size
, me
);
580 /* insert it into the address decoder */
584 /* command register block */
586 device_error(me
, "reg entry %d must have a size of 8", reg
);
587 decoder
->block
[reg
-1].space
= space
;
588 decoder
->block
[reg
-1].base_addr
= address
;
589 decoder
->block
[reg
-1].bound_addr
= address
+ size
- 1;
590 decoder
->block
[reg
-1].controller
= (reg
+ 1) % nr_ide_controllers
;
591 decoder
->block
[reg
-1].base_reg
= ide_data_reg
;
592 DTRACE(ide
, ("controller %d command register block at %d:0x%lx..0x%lx\n",
593 decoder
->block
[reg
-1].controller
,
594 decoder
->block
[reg
-1].space
,
595 (unsigned long)decoder
->block
[reg
-1].base_addr
,
596 (unsigned long)decoder
->block
[reg
-1].bound_addr
));
600 /* control register block */
602 device_error(me
, "reg entry %d must have a size of 1", reg
);
603 decoder
->block
[reg
-1].space
= space
;
604 decoder
->block
[reg
-1].base_addr
= address
;
605 decoder
->block
[reg
-1].bound_addr
= address
+ size
- 1;
606 decoder
->block
[reg
-1].controller
= (reg
+ 1) % nr_ide_controllers
;
607 decoder
->block
[reg
-1].base_reg
= ide_alternate_status_reg
;
608 DTRACE(ide
, ("controller %d control register block at %d:0x%lx..0x%lx\n",
609 decoder
->block
[reg
-1].controller
,
610 decoder
->block
[reg
-1].space
,
611 (unsigned long)decoder
->block
[reg
-1].base_addr
,
612 (unsigned long)decoder
->block
[reg
-1].bound_addr
));
615 /* dma register block */
617 device_error(me
, "reg entry %d must have a size of 8", reg
);
618 decoder
->block
[reg
-1].space
= space
;
619 decoder
->block
[reg
-1].base_addr
= address
;
620 decoder
->block
[reg
-1].bound_addr
= address
+ 4 - 1;
621 decoder
->block
[reg
-1].base_reg
= ide_dma_command_reg
;
622 decoder
->block
[reg
-1].controller
= 0;
623 DTRACE(ide
, ("controller %d dma register block at %d:0x%lx..0x%lx\n",
624 decoder
->block
[reg
-1].controller
,
625 decoder
->block
[reg
-1].space
,
626 (unsigned long)decoder
->block
[reg
-1].base_addr
,
627 (unsigned long)decoder
->block
[reg
-1].bound_addr
));
628 decoder
->block
[reg
].space
= space
;
629 decoder
->block
[reg
].base_addr
= address
+ 4;
630 decoder
->block
[reg
].bound_addr
= address
+ 8 - 1;
631 decoder
->block
[reg
].controller
= 1;
632 decoder
->block
[reg
].base_reg
= ide_dma_command_reg
;
633 DTRACE(ide
, ("controller %d dma register block at %d:0x%lx..0x%lx\n",
634 decoder
->block
[reg
].controller
,
635 decoder
->block
[reg
-1].space
,
636 (unsigned long)decoder
->block
[reg
].base_addr
,
637 (unsigned long)decoder
->block
[reg
].bound_addr
));
640 device_error(me
, "internal error - bad switch");
648 typedef struct _hw_ide_device
{
649 ide_controller controller
[nr_ide_controllers
];
650 address_decoder decoder
;
655 hw_ide_init_address(device
*me
)
657 hw_ide_device
*ide
= device_data(me
);
661 /* zero some things */
662 for (controller
= 0; controller
< nr_ide_controllers
; controller
++) {
663 memset(&ide
->controller
[controller
], 0, sizeof(ide_controller
));
664 for (drive
= 0; drive
< nr_ide_drives_per_controller
; drive
++) {
665 ide
->controller
[controller
].drive
[drive
].nr
= drive
;
667 ide
->controller
[controller
].me
= me
;
668 if (device_find_property(me
, "ready-delay") != NULL
)
669 ide
->controller
[controller
].ready_delay
=
670 device_find_integer_property(me
, "ready-delay");
673 /* attach this device to its parent */
674 generic_device_init_address(me
);
676 /* determine our own address map */
677 build_address_decoder(me
, &ide
->decoder
);
683 hw_ide_attach_address(device
*me
,
689 device
*client
) /*callback/default*/
691 hw_ide_device
*ide
= (hw_ide_device
*)device_data(me
);
692 int controller_nr
= addr
/ nr_ide_drives_per_controller
;
693 int drive_nr
= addr
% nr_ide_drives_per_controller
;
694 ide_controller
*controller
;
696 if (controller_nr
>= nr_ide_controllers
)
697 device_error(me
, "no controller for disk %s",
698 device_path(client
));
700 controller
= &ide
->controller
[controller_nr
];
701 drive
= &controller
->drive
[drive_nr
];
702 drive
->device
= client
;
703 if (device_find_property(client
, "ide-byte-count") != NULL
)
704 drive
->geometry
.byte
= device_find_integer_property(client
, "ide-byte-count");
706 drive
->geometry
.byte
= 512;
707 if (device_find_property(client
, "ide-sector-count") != NULL
)
708 drive
->geometry
.sector
= device_find_integer_property(client
, "ide-sector-count");
709 if (device_find_property(client
, "ide-head-count") != NULL
)
710 drive
->geometry
.head
= device_find_integer_property(client
, "ide-head-count");
711 drive
->default_geometry
= drive
->geometry
;
712 DTRACE(ide
, ("controller %d:%d %s byte-count %d, sector-count %d, head-count %d\n",
716 drive
->geometry
.byte
,
717 drive
->geometry
.sector
,
718 drive
->geometry
.head
));
723 hw_ide_io_read_buffer(device
*me
,
731 hw_ide_device
*ide
= (hw_ide_device
*)device_data(me
);
734 ide_controller
*controller
;
736 /* find the interface */
737 decode_address(me
, &ide
->decoder
, space
, addr
, &control_nr
, ®
, is_read
);
738 controller
= & ide
->controller
[control_nr
];
740 /* process the transfer */
741 memset(dest
, 0, nr_bytes
);
744 do_fifo_read(me
, controller
, dest
, nr_bytes
);
747 *(unsigned8
*)dest
= get_status(me
, controller
);
748 clear_interrupt(me
, controller
);
750 case ide_alternate_status_reg
:
751 *(unsigned8
*)dest
= get_status(me
, controller
);
754 case ide_sector_count_reg
:
755 case ide_sector_number_reg
:
756 case ide_cylinder_reg0
:
757 case ide_cylinder_reg1
:
758 case ide_drive_head_reg
:
759 case ide_control_reg
:
760 case ide_dma_command_reg
:
761 case ide_dma_status_reg
:
762 case ide_dma_prd_table_address_reg0
:
763 case ide_dma_prd_table_address_reg1
:
764 case ide_dma_prd_table_address_reg2
:
765 case ide_dma_prd_table_address_reg3
:
766 *(unsigned8
*)dest
= controller
->reg
[reg
];
769 device_error(me
, "bus-error at address 0x%lx", addr
);
777 hw_ide_io_write_buffer(device
*me
,
785 hw_ide_device
*ide
= (hw_ide_device
*)device_data(me
);
788 ide_controller
*controller
;
790 /* find the interface */
791 decode_address(me
, &ide
->decoder
, space
, addr
, &control_nr
, ®
, is_write
);
792 controller
= &ide
->controller
[control_nr
];
794 /* process the access */
797 do_fifo_write(me
, controller
, source
, nr_bytes
);
799 case ide_command_reg
:
800 do_command(me
, controller
, *(unsigned8
*)source
);
802 case ide_control_reg
:
803 controller
->reg
[reg
] = *(unsigned8
*)source
;
804 /* possibly cancel interrupts */
805 if ((controller
->reg
[reg
] & 0x02) == 0x02)
806 clear_interrupt(me
, controller
);
808 case ide_feature_reg
:
809 case ide_sector_count_reg
:
810 case ide_sector_number_reg
:
811 case ide_cylinder_reg0
:
812 case ide_cylinder_reg1
:
813 case ide_drive_head_reg
:
814 case ide_dma_command_reg
:
815 case ide_dma_status_reg
:
816 case ide_dma_prd_table_address_reg0
:
817 case ide_dma_prd_table_address_reg1
:
818 case ide_dma_prd_table_address_reg2
:
819 case ide_dma_prd_table_address_reg3
:
820 controller
->reg
[reg
] = *(unsigned8
*)source
;
823 device_error(me
, "bus-error at 0x%lx", addr
);
830 static const device_interrupt_port_descriptor hw_ide_interrupt_ports
[] = {
840 static device_callbacks
const hw_ide_callbacks
= {
841 { hw_ide_init_address
, },
842 { hw_ide_attach_address
, }, /* attach */
843 { hw_ide_io_read_buffer
, hw_ide_io_write_buffer
, },
845 { NULL
, NULL
, hw_ide_interrupt_ports
}, /* interrupt */
846 { generic_device_unit_decode
,
847 generic_device_unit_encode
,
848 generic_device_address_to_attach_address
,
849 generic_device_size_to_attach_size
},
854 hw_ide_create(const char *name
,
855 const device_unit
*unit_address
,
858 hw_ide_device
*ide
= ZALLOC(hw_ide_device
);
863 const device_descriptor hw_ide_device_descriptor
[] = {
864 { "ide", hw_ide_create
, &hw_ide_callbacks
},
868 #endif /* _HW_IDE_ */