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 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.
25 #include "device_table.h"
32 ide - Integrated Disk Electronics
38 This device models the primary/secondary <<ide>> controller
39 described in the [CHRPIO] document.
41 The controller has separate independant interrupt outputs for each
50 The <<reg>> property is described in the document [CHRPIO].
53 ready-delay = <integer> (optional)
55 If present, this specifies the time that the <<ide>> device takes
56 to complete an I/O operation.
59 disk@?/ide-byte-count = <integer> (optional)
61 disk@?/ide-sector-count = <integer> (optional)
63 disk@?/ide-head-count = <integer> (optional)
65 The <<ide>> device checks each child (disk device) node to see if
66 it has the above properties. If present, these values will be used
67 to compute the <<LBA>> address in <<CHS>> addressing mode.
78 Attach the <<ide>> device to the <<pci>> bus at slot one. Specify
81 | -o '/phb/ide@1/assigned-addresses \
87 | -o '/phb@0x80000000/ide@1/reg \
95 Note: the fouth and fifth reg entries specify that the register is
96 at an offset into the address specified by the base register
97 (<<assigned-addresses>>); Apart from restrictions placed by the
98 <<pci>> specification, no restrictions are placed on the number of
99 base registers specified by the <<assigned-addresses>> property.
101 Attach a <<disk>> to the primary and a <<cdrom>> to the secondary
104 | -o '/phb@0x80000000/ide@1/disk@0/file "zero' \
105 | -o '/phb@0x80000000/ide@1/cdrom@2/file "/dev/cdrom"' \
107 Connect the two interrupt outputs (a and b) to a <<glue>> device to
108 allow testing of the interrupt port. In a real simulation they
109 would be wired to the interrupt controller.
111 | -o '/phb@0x80000000/glue@2/reg 2 0 ni0,0,0,0 8' \
112 | -o '/phb@0x80000000/ide@1 > a 0 /phb@0x80000000/glue@2' \
113 | -o '/phb@0x80000000/ide@1 > b 1 /phb@0x80000000/glue@2'
119 While the DMA registers are present, DMA support has not yet been
122 The number of supported commands is very limited.
124 The standards documents appear to be vague on how to specify the
125 <<unit-address>> of disk devices devices being attached to the
126 <<ide>> controller. I've chosen to use integers with devices zero
127 and one going to the primary controller while two and three are
128 connected to the secondary controller.
134 [CHRPIO] PowerPC(tm) Microprocessor Common Hardware Reference
135 Platform: I/O Device Reference. http://chrp.apple.com/???.
137 [SCHMIDT] The SCSI Bus and IDE Interface - Protocols, Applications
138 and Programming. Friedhelm Schmidt (translated by Michael
139 Schultz). ISBN 0-201-42284-0. Addison-Wesley Publishing Company.
146 typedef enum _io_direction
{
153 nr_ide_controllers
= 2,
154 nr_ide_drives_per_controller
= 2,
155 nr_fifo_entries
= 8192,
159 /* command register block - read */
161 ide_error_reg
, /*ide_feature_reg*/
162 ide_sector_count_reg
,
163 ide_sector_number_reg
,
167 ide_status_reg
, /*ide_command_reg*/
168 /* command register block - write */
169 ide_feature_reg
, /*ide_error_reg*/
170 ide_command_reg
, /*ide_status_reg*/
171 /* control register block - read */
172 ide_alternate_status_reg
, /*ide_control_reg*/
173 ide_control_reg
, /*ide_alternate_status_reg*/
174 /* dma register block */
176 ide_dma_unused_1_reg
,
178 ide_dma_unused_3_reg
,
179 ide_dma_prd_table_address_reg0
,
180 ide_dma_prd_table_address_reg1
,
181 ide_dma_prd_table_address_reg2
,
182 ide_dma_prd_table_address_reg3
,
187 typedef enum _ide_states
{
198 ide_state_name(ide_states state
)
201 case idle_state
: return "idle";
202 case busy_loaded_state
: return "busy_loaded_state";
203 case busy_drained_state
: return "busy_drained_state";
204 case busy_dma_state
: return "busy_dma_state";
205 case busy_command_state
: return "busy_command_state";
206 case loading_state
: return "loading_state";
207 case draining_state
: return "draining_state";
208 default: return "illegal-state";
212 typedef struct _ide_geometry
{
218 typedef struct _ide_drive
{
221 ide_geometry geometry
;
222 ide_geometry default_geometry
;
225 typedef struct _ide_controller
{
228 unsigned8 reg
[nr_ide_registers
];
229 unsigned8 fifo
[nr_fifo_entries
];
232 ide_drive
*current_drive
;
234 int current_transfer
;
235 ide_drive drive
[nr_ide_drives_per_controller
];
237 event_entry_tag event_tag
;
239 signed64 ready_delay
;
245 set_interrupt(device
*me
,
246 ide_controller
*controller
)
248 if ((controller
->reg
[ide_control_reg
] & 0x2) == 0) {
249 DTRACE(ide
, ("controller %d - interrupt set\n", controller
->nr
));
250 device_interrupt_event(me
, controller
->nr
, 1, NULL
, 0);
251 controller
->is_interrupting
= 1;
257 clear_interrupt(device
*me
,
258 ide_controller
*controller
)
260 if (controller
->is_interrupting
) {
261 DTRACE(ide
, ("controller %d - interrupt clear\n", controller
->nr
));
262 device_interrupt_event(me
, controller
->nr
, 0, NULL
, 0);
263 controller
->is_interrupting
= 0;
271 ide_controller
*controller
= data
;
272 device
*me
= controller
->me
;
273 controller
->event_tag
= 0;
274 switch (controller
->state
) {
275 case busy_loaded_state
:
276 case busy_drained_state
:
277 if (controller
->current_transfer
> 0) {
278 controller
->state
= (controller
->state
== busy_loaded_state
279 ? loading_state
: draining_state
);
282 controller
->state
= idle_state
;
284 set_interrupt(me
, controller
);
287 device_error(me
, "controller %d - unexpected event", controller
->nr
);
294 schedule_ready_event(device
*me
,
295 ide_controller
*controller
)
297 if (controller
->event_tag
!= 0)
298 device_error(me
, "controller %d - attempting to schedule multiple events",
300 controller
->event_tag
=
301 device_event_queue_schedule(me
, controller
->ready_delay
,
302 do_event
, controller
);
307 do_fifo_read(device
*me
,
308 ide_controller
*controller
,
312 if (controller
->state
!= draining_state
)
313 device_error(me
, "controller %d - reading fifo when not ready (%s)",
315 ide_state_name(controller
->state
));
316 if (controller
->fifo_pos
+ nr_bytes
> controller
->fifo_size
)
317 device_error(me
, "controller %d - fifo underflow", controller
->nr
);
319 memcpy(dest
, &controller
->fifo
[controller
->fifo_pos
], nr_bytes
);
320 controller
->fifo_pos
+= nr_bytes
;
322 if (controller
->fifo_pos
== controller
->fifo_size
) {
323 controller
->current_transfer
-= 1;
324 if (controller
->current_transfer
> 0
325 && controller
->current_drive
!= NULL
) {
326 DTRACE(ide
, ("controller %d:%d - reading %d byte block at 0x%x\n",
328 controller
->current_drive
->nr
,
329 controller
->fifo_size
,
330 controller
->current_byte
));
331 if (device_io_read_buffer(controller
->current_drive
->device
,
333 0, controller
->current_byte
,
334 controller
->fifo_size
,
336 != controller
->fifo_size
)
337 device_error(me
, "controller %d - disk %s io read error",
339 device_path(controller
->current_drive
->device
));
341 controller
->state
= busy_drained_state
;
342 controller
->fifo_pos
= 0;
343 controller
->current_byte
+= controller
->fifo_size
;
344 schedule_ready_event(me
, controller
);
350 do_fifo_write(device
*me
,
351 ide_controller
*controller
,
355 if (controller
->state
!= loading_state
)
356 device_error(me
, "controller %d - writing fifo when not ready (%s)",
358 ide_state_name(controller
->state
));
359 if (controller
->fifo_pos
+ nr_bytes
> controller
->fifo_size
)
360 device_error(me
, "controller %d - fifo overflow", controller
->nr
);
362 memcpy(&controller
->fifo
[controller
->fifo_pos
], source
, nr_bytes
);
363 controller
->fifo_pos
+= nr_bytes
;
365 if (controller
->fifo_pos
== controller
->fifo_size
) {
366 if (controller
->current_transfer
> 0
367 && controller
->current_drive
!= NULL
) {
368 DTRACE(ide
, ("controller %d:%d - writing %d byte block at 0x%x\n",
370 controller
->current_drive
->nr
,
371 controller
->fifo_size
,
372 controller
->current_byte
));
373 if (device_io_write_buffer(controller
->current_drive
->device
,
375 0, controller
->current_byte
,
376 controller
->fifo_size
,
378 != controller
->fifo_size
)
379 device_error(me
, "controller %d - disk %s io write error",
381 device_path(controller
->current_drive
->device
));
383 controller
->current_transfer
-= 1;
384 controller
->fifo_pos
= 0;
385 controller
->current_byte
+= controller
->fifo_size
;
386 controller
->state
= busy_loaded_state
;
387 schedule_ready_event(me
, controller
);
393 setup_fifo(device
*me
,
394 ide_controller
*controller
,
397 io_direction direction
)
401 int drive_nr
= (controller
->reg
[ide_drive_head_reg
] & 0x10) != 0;
402 controller
->current_drive
= &controller
->drive
[drive_nr
];
405 controller
->current_drive
= NULL
;
408 /* number of transfers */
410 controller
->current_transfer
= 1;
412 int sector_count
= controller
->reg
[ide_sector_count_reg
];
413 if (sector_count
== 0)
414 controller
->current_transfer
= 256;
416 controller
->current_transfer
= sector_count
;
419 /* the transfer size */
420 if (controller
->current_drive
== NULL
)
421 controller
->fifo_size
= 512;
423 controller
->fifo_size
= controller
->current_drive
->geometry
.byte
;
426 controller
->fifo_pos
= 0;
428 /* the starting address */
429 if (controller
->current_drive
== NULL
)
430 controller
->current_byte
= 0;
431 else if (controller
->reg
[ide_drive_head_reg
] & 0x40) {
432 /* LBA addressing mode */
433 controller
->current_byte
= controller
->fifo_size
434 * (((controller
->reg
[ide_drive_head_reg
] & 0xf) << 24)
435 | (controller
->reg
[ide_cylinder_reg1
] << 16)
436 | (controller
->reg
[ide_cylinder_reg0
] << 8)
437 | (controller
->reg
[ide_sector_number_reg
]));
439 else if (controller
->current_drive
->geometry
.head
!= 0
440 && controller
->current_drive
->geometry
.sector
!= 0) {
441 /* CHS addressing mode */
442 int head_nr
= controller
->reg
[ide_drive_head_reg
] & 0xf;
443 int cylinder_nr
= ((controller
->reg
[ide_cylinder_reg1
] << 8)
444 | controller
->reg
[ide_cylinder_reg0
]);
445 int sector_nr
= controller
->reg
[ide_sector_number_reg
];
446 controller
->current_byte
= controller
->fifo_size
447 * ((cylinder_nr
* controller
->current_drive
->geometry
.head
+ head_nr
)
448 * controller
->current_drive
->geometry
.sector
+ sector_nr
- 1);
451 device_error(me
, "controller %d:%d - CHS addressing disabled",
452 controller
->nr
, controller
->current_drive
->nr
);
453 DTRACE(ide
, ("controller %ld:%ld - transfer (%s) %ld blocks of %ld bytes from 0x%lx\n",
454 (long)controller
->nr
,
455 controller
->current_drive
== NULL
? -1L : (long)controller
->current_drive
->nr
,
456 direction
== is_read
? "read" : "write",
457 (long)controller
->current_transfer
,
458 (long)controller
->fifo_size
,
459 (unsigned long)controller
->current_byte
));
462 /* force a primeing read */
463 controller
->current_transfer
+= 1;
464 controller
->state
= draining_state
;
465 controller
->fifo_pos
= controller
->fifo_size
;
466 do_fifo_read(me
, controller
, NULL
, 0);
469 controller
->state
= loading_state
;
476 do_command(device
*me
,
477 ide_controller
*controller
,
480 if (controller
->state
!= idle_state
)
481 device_error(me
, "controller %d - command when not idle", controller
->nr
);
483 case 0x20: case 0x21: /* read-sectors */
484 setup_fifo(me
, controller
, 0/*is_simple*/, 1/*is_with_disk*/, is_read
);
486 case 0x30: case 0x31: /* write */
487 setup_fifo(me
, controller
, 0/*is_simple*/, 1/*is_with_disk*/, is_write
);
493 get_status(device
*me
,
494 ide_controller
*controller
)
496 switch (controller
->state
) {
499 return 0x08; /* data req */
500 case busy_loaded_state
:
501 case busy_drained_state
:
502 return 0x80; /* busy */
504 return 0x40; /* drive ready */
506 device_error(me
, "internal error");
512 /* The address presented to the IDE controler is decoded and then
513 mapped onto a controller:reg pair */
516 nr_address_blocks
= 6,
519 typedef struct _address_block
{
521 unsigned_word base_addr
;
522 unsigned_word bound_addr
;
527 typedef struct _address_decoder
{
528 address_block block
[nr_address_blocks
];
532 decode_address(device
*me
,
533 address_decoder
*decoder
,
535 unsigned_word address
,
538 io_direction direction
)
541 for (i
= 0; i
< nr_address_blocks
; i
++) {
542 if (space
== decoder
->block
[i
].space
543 && address
>= decoder
->block
[i
].base_addr
544 && address
<= decoder
->block
[i
].bound_addr
) {
545 *controller
= decoder
->block
[i
].controller
;
547 - decoder
->block
[i
].base_addr
548 + decoder
->block
[i
].base_reg
);
549 if (direction
== is_write
) {
551 case ide_error_reg
: *reg
= ide_feature_reg
; break;
552 case ide_status_reg
: *reg
= ide_command_reg
; break;
553 case ide_alternate_status_reg
: *reg
= ide_control_reg
; break;
560 device_error(me
, "address %d:0x%lx invalid",
561 space
, (unsigned long)address
);
566 build_address_decoder(device
*me
,
567 address_decoder
*decoder
)
570 for (reg
= 1; reg
< 6; reg
++) {
571 reg_property_spec unit
;
573 unsigned_word address
;
575 /* find and decode the reg property */
576 if (!device_find_reg_array_property(me
, "reg", reg
, &unit
))
577 device_error(me
, "missing or invalid reg entry %d", reg
);
578 device_address_to_attach_address(device_parent(me
), &unit
.address
,
579 &space
, &address
, me
);
580 device_size_to_attach_size(device_parent(me
), &unit
.size
, &size
, me
);
581 /* insert it into the address decoder */
585 /* command register block */
587 device_error(me
, "reg entry %d must have a size of 8", reg
);
588 decoder
->block
[reg
-1].space
= space
;
589 decoder
->block
[reg
-1].base_addr
= address
;
590 decoder
->block
[reg
-1].bound_addr
= address
+ size
- 1;
591 decoder
->block
[reg
-1].controller
= (reg
+ 1) % nr_ide_controllers
;
592 decoder
->block
[reg
-1].base_reg
= ide_data_reg
;
593 DTRACE(ide
, ("controller %d command register block at %d:0x%lx..0x%lx\n",
594 decoder
->block
[reg
-1].controller
,
595 decoder
->block
[reg
-1].space
,
596 (unsigned long)decoder
->block
[reg
-1].base_addr
,
597 (unsigned long)decoder
->block
[reg
-1].bound_addr
));
601 /* control register block */
603 device_error(me
, "reg entry %d must have a size of 1", reg
);
604 decoder
->block
[reg
-1].space
= space
;
605 decoder
->block
[reg
-1].base_addr
= address
;
606 decoder
->block
[reg
-1].bound_addr
= address
+ size
- 1;
607 decoder
->block
[reg
-1].controller
= (reg
+ 1) % nr_ide_controllers
;
608 decoder
->block
[reg
-1].base_reg
= ide_alternate_status_reg
;
609 DTRACE(ide
, ("controller %d control register block at %d:0x%lx..0x%lx\n",
610 decoder
->block
[reg
-1].controller
,
611 decoder
->block
[reg
-1].space
,
612 (unsigned long)decoder
->block
[reg
-1].base_addr
,
613 (unsigned long)decoder
->block
[reg
-1].bound_addr
));
616 /* dma register block */
618 device_error(me
, "reg entry %d must have a size of 8", reg
);
619 decoder
->block
[reg
-1].space
= space
;
620 decoder
->block
[reg
-1].base_addr
= address
;
621 decoder
->block
[reg
-1].bound_addr
= address
+ 4 - 1;
622 decoder
->block
[reg
-1].base_reg
= ide_dma_command_reg
;
623 decoder
->block
[reg
-1].controller
= 0;
624 DTRACE(ide
, ("controller %d dma register block at %d:0x%lx..0x%lx\n",
625 decoder
->block
[reg
-1].controller
,
626 decoder
->block
[reg
-1].space
,
627 (unsigned long)decoder
->block
[reg
-1].base_addr
,
628 (unsigned long)decoder
->block
[reg
-1].bound_addr
));
629 decoder
->block
[reg
].space
= space
;
630 decoder
->block
[reg
].base_addr
= address
+ 4;
631 decoder
->block
[reg
].bound_addr
= address
+ 8 - 1;
632 decoder
->block
[reg
].controller
= 1;
633 decoder
->block
[reg
].base_reg
= ide_dma_command_reg
;
634 DTRACE(ide
, ("controller %d dma register block at %d:0x%lx..0x%lx\n",
635 decoder
->block
[reg
].controller
,
636 decoder
->block
[reg
-1].space
,
637 (unsigned long)decoder
->block
[reg
].base_addr
,
638 (unsigned long)decoder
->block
[reg
].bound_addr
));
641 device_error(me
, "internal error - bad switch");
649 typedef struct _hw_ide_device
{
650 ide_controller controller
[nr_ide_controllers
];
651 address_decoder decoder
;
656 hw_ide_init_address(device
*me
)
658 hw_ide_device
*ide
= device_data(me
);
662 /* zero some things */
663 for (controller
= 0; controller
< nr_ide_controllers
; controller
++) {
664 memset(&ide
->controller
[controller
], 0, sizeof(ide_controller
));
665 for (drive
= 0; drive
< nr_ide_drives_per_controller
; drive
++) {
666 ide
->controller
[controller
].drive
[drive
].nr
= drive
;
668 ide
->controller
[controller
].me
= me
;
669 if (device_find_property(me
, "ready-delay") != NULL
)
670 ide
->controller
[controller
].ready_delay
=
671 device_find_integer_property(me
, "ready-delay");
674 /* attach this device to its parent */
675 generic_device_init_address(me
);
677 /* determine our own address map */
678 build_address_decoder(me
, &ide
->decoder
);
684 hw_ide_attach_address(device
*me
,
690 device
*client
) /*callback/default*/
692 hw_ide_device
*ide
= (hw_ide_device
*)device_data(me
);
693 int controller_nr
= addr
/ nr_ide_drives_per_controller
;
694 int drive_nr
= addr
% nr_ide_drives_per_controller
;
695 ide_controller
*controller
;
697 if (controller_nr
>= nr_ide_controllers
)
698 device_error(me
, "no controller for disk %s",
699 device_path(client
));
701 controller
= &ide
->controller
[controller_nr
];
702 drive
= &controller
->drive
[drive_nr
];
703 drive
->device
= client
;
704 if (device_find_property(client
, "ide-byte-count") != NULL
)
705 drive
->geometry
.byte
= device_find_integer_property(client
, "ide-byte-count");
707 drive
->geometry
.byte
= 512;
708 if (device_find_property(client
, "ide-sector-count") != NULL
)
709 drive
->geometry
.sector
= device_find_integer_property(client
, "ide-sector-count");
710 if (device_find_property(client
, "ide-head-count") != NULL
)
711 drive
->geometry
.head
= device_find_integer_property(client
, "ide-head-count");
712 drive
->default_geometry
= drive
->geometry
;
713 DTRACE(ide
, ("controller %d:%d %s byte-count %d, sector-count %d, head-count %d\n",
717 drive
->geometry
.byte
,
718 drive
->geometry
.sector
,
719 drive
->geometry
.head
));
724 hw_ide_io_read_buffer(device
*me
,
732 hw_ide_device
*ide
= (hw_ide_device
*)device_data(me
);
735 ide_controller
*controller
;
737 /* find the interface */
738 decode_address(me
, &ide
->decoder
, space
, addr
, &control_nr
, ®
, is_read
);
739 controller
= & ide
->controller
[control_nr
];
741 /* process the transfer */
742 memset(dest
, 0, nr_bytes
);
745 do_fifo_read(me
, controller
, dest
, nr_bytes
);
748 *(unsigned8
*)dest
= get_status(me
, controller
);
749 clear_interrupt(me
, controller
);
751 case ide_alternate_status_reg
:
752 *(unsigned8
*)dest
= get_status(me
, controller
);
755 case ide_sector_count_reg
:
756 case ide_sector_number_reg
:
757 case ide_cylinder_reg0
:
758 case ide_cylinder_reg1
:
759 case ide_drive_head_reg
:
760 case ide_control_reg
:
761 case ide_dma_command_reg
:
762 case ide_dma_status_reg
:
763 case ide_dma_prd_table_address_reg0
:
764 case ide_dma_prd_table_address_reg1
:
765 case ide_dma_prd_table_address_reg2
:
766 case ide_dma_prd_table_address_reg3
:
767 *(unsigned8
*)dest
= controller
->reg
[reg
];
770 device_error(me
, "bus-error at address 0x%lx", addr
);
778 hw_ide_io_write_buffer(device
*me
,
786 hw_ide_device
*ide
= (hw_ide_device
*)device_data(me
);
789 ide_controller
*controller
;
791 /* find the interface */
792 decode_address(me
, &ide
->decoder
, space
, addr
, &control_nr
, ®
, is_write
);
793 controller
= &ide
->controller
[control_nr
];
795 /* process the access */
798 do_fifo_write(me
, controller
, source
, nr_bytes
);
800 case ide_command_reg
:
801 do_command(me
, controller
, *(unsigned8
*)source
);
803 case ide_control_reg
:
804 controller
->reg
[reg
] = *(unsigned8
*)source
;
805 /* possibly cancel interrupts */
806 if ((controller
->reg
[reg
] & 0x02) == 0x02)
807 clear_interrupt(me
, controller
);
809 case ide_feature_reg
:
810 case ide_sector_count_reg
:
811 case ide_sector_number_reg
:
812 case ide_cylinder_reg0
:
813 case ide_cylinder_reg1
:
814 case ide_drive_head_reg
:
815 case ide_dma_command_reg
:
816 case ide_dma_status_reg
:
817 case ide_dma_prd_table_address_reg0
:
818 case ide_dma_prd_table_address_reg1
:
819 case ide_dma_prd_table_address_reg2
:
820 case ide_dma_prd_table_address_reg3
:
821 controller
->reg
[reg
] = *(unsigned8
*)source
;
824 device_error(me
, "bus-error at 0x%lx", addr
);
831 static const device_interrupt_port_descriptor hw_ide_interrupt_ports
[] = {
841 static device_callbacks
const hw_ide_callbacks
= {
842 { hw_ide_init_address
, },
843 { hw_ide_attach_address
, }, /* attach */
844 { hw_ide_io_read_buffer
, hw_ide_io_write_buffer
, },
846 { NULL
, NULL
, hw_ide_interrupt_ports
}, /* interrupt */
847 { generic_device_unit_decode
,
848 generic_device_unit_encode
,
849 generic_device_address_to_attach_address
,
850 generic_device_size_to_attach_size
},
855 hw_ide_create(const char *name
,
856 const device_unit
*unit_address
,
859 hw_ide_device
*ide
= ZALLOC(hw_ide_device
);
864 const device_descriptor hw_ide_device_descriptor
[] = {
865 { "ide", hw_ide_create
, &hw_ide_callbacks
},
869 #endif /* _HW_IDE_ */