2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "arch/alpha/ev5.hh"
35 #include "base/trace.hh"
36 #include "cpu/intr_control.hh"
37 #include "dev/ide_ctrl.hh"
38 #include "dev/ide_disk.hh"
39 #include "dev/pciconfigall.hh"
40 #include "dev/pcireg.h"
41 #include "dev/platform.hh"
42 #include "mem/bus/bus.hh"
43 #include "mem/bus/dma_interface.hh"
44 #include "mem/bus/pio_interface.hh"
45 #include "mem/bus/pio_interface_impl.hh"
46 #include "mem/functional/memory_control.hh"
47 #include "mem/functional/physical.hh"
48 #include "sim/builder.hh"
49 #include "sim/sim_object.hh"
52 using namespace TheISA
;
55 // Initialization and destruction
58 IdeController::IdeController(Params
*p
)
61 // initialize the PIO interface addresses
63 pri_cmd_size
= BARSize
[0];
66 pri_ctrl_size
= BARSize
[1];
69 sec_cmd_size
= BARSize
[2];
72 sec_ctrl_size
= BARSize
[3];
74 // initialize the bus master interface (BMI) address to be configured
77 bmi_size
= BARSize
[4];
79 // zero out all of the registers
80 memset(bmi_regs
.data
, 0, sizeof(bmi_regs
));
81 memset(config_regs
.data
, 0, sizeof(config_regs
.data
));
83 // setup initial values
84 // enable both channels
85 config_regs
.idetim0
= htole((uint16_t)IDETIM_DECODE_EN
);
86 config_regs
.idetim1
= htole((uint16_t)IDETIM_DECODE_EN
);
87 bmi_regs
.bmis0
= DMA1CAP
| DMA0CAP
;
88 bmi_regs
.bmis1
= DMA1CAP
| DMA0CAP
;
90 // reset all internal variables
93 memset(cmd_in_progress
, 0, sizeof(cmd_in_progress
));
97 // create the PIO and DMA interfaces
98 if (params()->pio_bus
) {
99 pioInterface
= newPioInterface(name() + ".pio", params()->hier
,
100 params()->pio_bus
, this,
101 &IdeController::cacheAccess
);
102 pioLatency
= params()->pio_latency
* params()->pio_bus
->clockRate
;
105 if (params()->dma_bus
) {
106 dmaInterface
= new DMAInterface
<Bus
>(name() + ".dma",
108 params()->dma_bus
, 1, true);
111 // setup the disks attached to controller
112 memset(disks
, 0, sizeof(disks
));
116 if (params()->disks
.size() > 3)
117 panic("IDE controllers support a maximum of 4 devices attached!\n");
119 for (int i
= 0; i
< params()->disks
.size(); i
++) {
120 disks
[i
] = params()->disks
[i
];
121 disks
[i
]->setController(this, dmaInterface
);
125 IdeController::~IdeController()
127 for (int i
= 0; i
< 4; i
++)
137 IdeController::parseAddr(const Addr
&addr
, Addr
&offset
, IdeChannel
&channel
,
138 IdeRegType
®_type
)
142 if (addr
>= pri_cmd_addr
&& addr
< (pri_cmd_addr
+ pri_cmd_size
)) {
143 offset
-= pri_cmd_addr
;
144 reg_type
= COMMAND_BLOCK
;
146 } else if (addr
>= pri_ctrl_addr
&&
147 addr
< (pri_ctrl_addr
+ pri_ctrl_size
)) {
148 offset
-= pri_ctrl_addr
;
149 reg_type
= CONTROL_BLOCK
;
151 } else if (addr
>= sec_cmd_addr
&&
152 addr
< (sec_cmd_addr
+ sec_cmd_size
)) {
153 offset
-= sec_cmd_addr
;
154 reg_type
= COMMAND_BLOCK
;
156 } else if (addr
>= sec_ctrl_addr
&&
157 addr
< (sec_ctrl_addr
+ sec_ctrl_size
)) {
158 offset
-= sec_ctrl_addr
;
159 reg_type
= CONTROL_BLOCK
;
161 } else if (addr
>= bmi_addr
&& addr
< (bmi_addr
+ bmi_size
)) {
163 reg_type
= BMI_BLOCK
;
164 channel
= (offset
< BMIC1
) ? PRIMARY
: SECONDARY
;
166 panic("IDE controller access to invalid address: %#x\n", addr
);
171 IdeController::getDisk(IdeChannel channel
)
174 uint8_t *devBit
= &dev
[0];
176 if (channel
== SECONDARY
) {
183 assert(*devBit
== 0 || *devBit
== 1);
189 IdeController::getDisk(IdeDisk
*diskPtr
)
191 for (int i
= 0; i
< 4; i
++) {
192 if ((long)diskPtr
== (long)disks
[i
])
199 IdeController::isDiskSelected(IdeDisk
*diskPtr
)
201 for (int i
= 0; i
< 4; i
++) {
202 if ((long)diskPtr
== (long)disks
[i
]) {
203 // is disk is on primary or secondary channel
205 // is disk the master or slave
208 return (dev
[channel
] == devID
);
211 panic("Unable to find disk by pointer!!\n");
215 // Command completion
219 IdeController::setDmaComplete(IdeDisk
*disk
)
221 int diskNum
= getDisk(disk
);
224 panic("Unable to find disk based on pointer %#x\n", disk
);
227 // clear the start/stop bit in the command register
228 bmi_regs
.bmic0
&= ~SSBM
;
229 // clear the bus master active bit in the status register
230 bmi_regs
.bmis0
&= ~BMIDEA
;
231 // set the interrupt bit
232 bmi_regs
.bmis0
|= IDEINTS
;
234 // clear the start/stop bit in the command register
235 bmi_regs
.bmic1
&= ~SSBM
;
236 // clear the bus master active bit in the status register
237 bmi_regs
.bmis1
&= ~BMIDEA
;
238 // set the interrupt bit
239 bmi_regs
.bmis1
|= IDEINTS
;
244 // Bus timing and bus access functions
248 IdeController::cacheAccess(MemReqPtr
&req
)
250 // @todo Add more accurate timing to cache access
251 return curTick
+ pioLatency
;
255 // Read and write handling
259 IdeController::readConfig(int offset
, int size
, uint8_t *data
)
263 if (offset
< PCI_DEVICE_SPECIFIC
) {
264 PciDev::readConfig(offset
, size
, data
);
265 } else if (offset
>= IDE_CTRL_CONF_START
&&
266 (offset
+ size
) <= IDE_CTRL_CONF_END
) {
268 config_offset
= offset
- IDE_CTRL_CONF_START
;
271 case sizeof(uint8_t):
272 *data
= config_regs
.data
[config_offset
];
274 case sizeof(uint16_t):
275 *(uint16_t*)data
= *(uint16_t*)&config_regs
.data
[config_offset
];
277 case sizeof(uint32_t):
278 *(uint32_t*)data
= *(uint32_t*)&config_regs
.data
[config_offset
];
281 panic("Invalid PCI configuration read size!\n");
284 DPRINTF(IdeCtrl
, "PCI read offset: %#x size: %#x data: %#x\n",
285 offset
, size
, *(uint32_t*)data
);
288 panic("Read of unimplemented PCI config. register: %x\n", offset
);
293 IdeController::writeConfig(int offset
, int size
, const uint8_t *data
)
297 if (offset
< PCI_DEVICE_SPECIFIC
) {
298 PciDev::writeConfig(offset
, size
, data
);
299 } else if (offset
>= IDE_CTRL_CONF_START
&&
300 (offset
+ size
) <= IDE_CTRL_CONF_END
) {
302 config_offset
= offset
- IDE_CTRL_CONF_START
;
305 case sizeof(uint8_t):
306 config_regs
.data
[config_offset
] = *data
;
308 case sizeof(uint16_t):
309 *(uint16_t*)&config_regs
.data
[config_offset
] = *(uint16_t*)data
;
311 case sizeof(uint32_t):
312 *(uint32_t*)&config_regs
.data
[config_offset
] = *(uint32_t*)data
;
315 panic("Invalid PCI configuration write size!\n");
318 panic("Write of unimplemented PCI config. register: %x\n", offset
);
321 DPRINTF(IdeCtrl
, "PCI write offset: %#x size: %#x data: %#x\n",
324 // Catch the writes to specific PCI registers that have side affects
325 // (like updating the PIO ranges)
328 if (letoh(config
.command
) & PCI_CMD_IOSE
)
333 if (letoh(config
.command
) & PCI_CMD_BME
)
339 case PCI0_BASE_ADDR0
:
340 if (BARAddrs
[0] != 0) {
341 pri_cmd_addr
= BARAddrs
[0];
343 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
,
346 pri_cmd_addr
&= EV5::PAddrUncachedMask
;
350 case PCI0_BASE_ADDR1
:
351 if (BARAddrs
[1] != 0) {
352 pri_ctrl_addr
= BARAddrs
[1];
354 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
,
357 pri_ctrl_addr
&= EV5::PAddrUncachedMask
;
361 case PCI0_BASE_ADDR2
:
362 if (BARAddrs
[2] != 0) {
363 sec_cmd_addr
= BARAddrs
[2];
365 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
,
368 sec_cmd_addr
&= EV5::PAddrUncachedMask
;
372 case PCI0_BASE_ADDR3
:
373 if (BARAddrs
[3] != 0) {
374 sec_ctrl_addr
= BARAddrs
[3];
376 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
,
379 sec_ctrl_addr
&= EV5::PAddrUncachedMask
;
383 case PCI0_BASE_ADDR4
:
384 if (BARAddrs
[4] != 0) {
385 bmi_addr
= BARAddrs
[4];
387 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
389 bmi_addr
&= EV5::PAddrUncachedMask
;
396 IdeController::read(MemReqPtr
&req
, uint8_t *data
)
403 parseAddr(req
->paddr
, offset
, channel
, reg_type
);
411 case sizeof(uint8_t):
412 *data
= bmi_regs
.data
[offset
];
414 case sizeof(uint16_t):
415 *(uint16_t*)data
= *(uint16_t*)&bmi_regs
.data
[offset
];
417 case sizeof(uint32_t):
418 *(uint32_t*)data
= *(uint32_t*)&bmi_regs
.data
[offset
];
421 panic("IDE read of BMI reg invalid size: %#x\n", req
->size
);
427 disk
= getDisk(channel
);
429 if (disks
[disk
] == NULL
)
435 case sizeof(uint16_t):
436 disks
[disk
]->read(offset
, reg_type
, data
);
439 case sizeof(uint32_t):
440 disks
[disk
]->read(offset
, reg_type
, data
);
441 disks
[disk
]->read(offset
, reg_type
, &data
[2]);
445 panic("IDE read of data reg invalid size: %#x\n", req
->size
);
449 if (req
->size
== sizeof(uint8_t)) {
450 disks
[disk
]->read(offset
, reg_type
, data
);
452 panic("IDE read of command reg of invalid size: %#x\n", req
->size
);
456 panic("IDE controller read of unknown register block type!\n");
459 DPRINTF(IdeCtrl
, "read from offset: %#x size: %#x data: %#x\n",
460 offset
, req
->size
, *(uint32_t*)data
);
466 IdeController::write(MemReqPtr
&req
, const uint8_t *data
)
472 uint8_t oldVal
, newVal
;
474 parseAddr(req
->paddr
, offset
, channel
, reg_type
);
485 // Bus master IDE command register
488 if (req
->size
!= sizeof(uint8_t))
489 panic("Invalid BMIC write size: %x\n", req
->size
);
491 // select the current disk based on DEV bit
492 disk
= getDisk(channel
);
494 oldVal
= bmi_regs
.chan
[channel
].bmic
;
497 // if a DMA transfer is in progress, R/W control cannot change
499 if ((oldVal
& RWCON
) ^ (newVal
& RWCON
)) {
500 (oldVal
& RWCON
) ? newVal
|= RWCON
: newVal
&= ~RWCON
;
504 // see if the start/stop bit is being changed
505 if ((oldVal
& SSBM
) ^ (newVal
& SSBM
)) {
507 // stopping DMA transfer
508 DPRINTF(IdeCtrl
, "Stopping DMA transfer\n");
510 // clear the BMIDEA bit
511 bmi_regs
.chan
[channel
].bmis
=
512 bmi_regs
.chan
[channel
].bmis
& ~BMIDEA
;
514 if (disks
[disk
] == NULL
)
515 panic("DMA stop for disk %d which does not exist\n",
518 // inform the disk of the DMA transfer abort
519 disks
[disk
]->abortDma();
521 // starting DMA transfer
522 DPRINTF(IdeCtrl
, "Starting DMA transfer\n");
524 // set the BMIDEA bit
525 bmi_regs
.chan
[channel
].bmis
=
526 bmi_regs
.chan
[channel
].bmis
| BMIDEA
;
528 if (disks
[disk
] == NULL
)
529 panic("DMA start for disk %d which does not exist\n",
532 // inform the disk of the DMA transfer start
533 disks
[disk
]->startDma(letoh(bmi_regs
.chan
[channel
].bmidtp
));
537 // update the register value
538 bmi_regs
.chan
[channel
].bmic
= newVal
;
541 // Bus master IDE status register
544 if (req
->size
!= sizeof(uint8_t))
545 panic("Invalid BMIS write size: %x\n", req
->size
);
547 oldVal
= bmi_regs
.chan
[channel
].bmis
;
550 // the BMIDEA bit is RO
551 newVal
|= (oldVal
& BMIDEA
);
553 // to reset (set 0) IDEINTS and IDEDMAE, write 1 to each
554 if ((oldVal
& IDEINTS
) && (newVal
& IDEINTS
))
555 newVal
&= ~IDEINTS
; // clear the interrupt?
557 (oldVal
& IDEINTS
) ? newVal
|= IDEINTS
: newVal
&= ~IDEINTS
;
559 if ((oldVal
& IDEDMAE
) && (newVal
& IDEDMAE
))
562 (oldVal
& IDEDMAE
) ? newVal
|= IDEDMAE
: newVal
&= ~IDEDMAE
;
564 bmi_regs
.chan
[channel
].bmis
= newVal
;
567 // Bus master IDE descriptor table pointer register
571 if (req
->size
!= sizeof(uint32_t))
572 panic("Invalid BMIDTP write size: %x\n", req
->size
);
574 uint32_t host_data
= letoh(*(uint32_t*)data
);
576 bmi_regs
.chan
[channel
].bmidtp
= htole(host_data
);
581 if (req
->size
!= sizeof(uint8_t) &&
582 req
->size
!= sizeof(uint16_t) &&
583 req
->size
!= sizeof(uint32_t))
584 panic("IDE controller write of invalid write size: %x\n",
587 // do a default copy of data into the registers
588 memcpy(&bmi_regs
.data
[offset
], data
, req
->size
);
592 if (offset
== IDE_SELECT_OFFSET
) {
593 uint8_t *devBit
= &dev
[channel
];
594 *devBit
= (letoh(*data
) & IDE_SELECT_DEV_BIT
) ? 1 : 0;
598 disk
= getDisk(channel
);
600 if (disks
[disk
] == NULL
)
606 case sizeof(uint16_t):
607 disks
[disk
]->write(offset
, reg_type
, data
);
610 case sizeof(uint32_t):
611 disks
[disk
]->write(offset
, reg_type
, data
);
612 disks
[disk
]->write(offset
, reg_type
, &data
[2]);
615 panic("IDE write of data reg invalid size: %#x\n", req
->size
);
619 if (req
->size
== sizeof(uint8_t)) {
620 disks
[disk
]->write(offset
, reg_type
, data
);
622 panic("IDE write of command reg of invalid size: %#x\n", req
->size
);
626 panic("IDE controller write of unknown register block type!\n");
629 DPRINTF(IdeCtrl
, "write to offset: %#x size: %#x data: %#x\n",
630 offset
, req
->size
, *(uint32_t*)data
);
640 IdeController::serialize(std::ostream
&os
)
642 // Serialize the PciDev base class
643 PciDev::serialize(os
);
645 // Serialize register addresses and sizes
646 SERIALIZE_SCALAR(pri_cmd_addr
);
647 SERIALIZE_SCALAR(pri_cmd_size
);
648 SERIALIZE_SCALAR(pri_ctrl_addr
);
649 SERIALIZE_SCALAR(pri_ctrl_size
);
650 SERIALIZE_SCALAR(sec_cmd_addr
);
651 SERIALIZE_SCALAR(sec_cmd_size
);
652 SERIALIZE_SCALAR(sec_ctrl_addr
);
653 SERIALIZE_SCALAR(sec_ctrl_size
);
654 SERIALIZE_SCALAR(bmi_addr
);
655 SERIALIZE_SCALAR(bmi_size
);
657 // Serialize registers
658 SERIALIZE_ARRAY(bmi_regs
.data
,
659 sizeof(bmi_regs
.data
) / sizeof(bmi_regs
.data
[0]));
660 SERIALIZE_ARRAY(dev
, sizeof(dev
) / sizeof(dev
[0]));
661 SERIALIZE_ARRAY(config_regs
.data
,
662 sizeof(config_regs
.data
) / sizeof(config_regs
.data
[0]));
664 // Serialize internal state
665 SERIALIZE_SCALAR(io_enabled
);
666 SERIALIZE_SCALAR(bm_enabled
);
667 SERIALIZE_ARRAY(cmd_in_progress
,
668 sizeof(cmd_in_progress
) / sizeof(cmd_in_progress
[0]));
672 IdeController::unserialize(Checkpoint
*cp
, const std::string
§ion
)
674 // Unserialize the PciDev base class
675 PciDev::unserialize(cp
, section
);
677 // Unserialize register addresses and sizes
678 UNSERIALIZE_SCALAR(pri_cmd_addr
);
679 UNSERIALIZE_SCALAR(pri_cmd_size
);
680 UNSERIALIZE_SCALAR(pri_ctrl_addr
);
681 UNSERIALIZE_SCALAR(pri_ctrl_size
);
682 UNSERIALIZE_SCALAR(sec_cmd_addr
);
683 UNSERIALIZE_SCALAR(sec_cmd_size
);
684 UNSERIALIZE_SCALAR(sec_ctrl_addr
);
685 UNSERIALIZE_SCALAR(sec_ctrl_size
);
686 UNSERIALIZE_SCALAR(bmi_addr
);
687 UNSERIALIZE_SCALAR(bmi_size
);
689 // Unserialize registers
690 UNSERIALIZE_ARRAY(bmi_regs
.data
,
691 sizeof(bmi_regs
.data
) / sizeof(bmi_regs
.data
[0]));
692 UNSERIALIZE_ARRAY(dev
, sizeof(dev
) / sizeof(dev
[0]));
693 UNSERIALIZE_ARRAY(config_regs
.data
,
694 sizeof(config_regs
.data
) / sizeof(config_regs
.data
[0]));
696 // Unserialize internal state
697 UNSERIALIZE_SCALAR(io_enabled
);
698 UNSERIALIZE_SCALAR(bm_enabled
);
699 UNSERIALIZE_ARRAY(cmd_in_progress
,
700 sizeof(cmd_in_progress
) / sizeof(cmd_in_progress
[0]));
703 pioInterface
->addAddrRange(RangeSize(pri_cmd_addr
, pri_cmd_size
));
704 pioInterface
->addAddrRange(RangeSize(pri_ctrl_addr
, pri_ctrl_size
));
705 pioInterface
->addAddrRange(RangeSize(sec_cmd_addr
, sec_cmd_size
));
706 pioInterface
->addAddrRange(RangeSize(sec_ctrl_addr
, sec_ctrl_size
));
707 pioInterface
->addAddrRange(RangeSize(bmi_addr
, bmi_size
));
711 #ifndef DOXYGEN_SHOULD_SKIP_THIS
713 BEGIN_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
716 SimObjectVectorParam
<IdeDisk
*> disks
;
717 SimObjectParam
<MemoryController
*> mmu
;
718 SimObjectParam
<PciConfigAll
*> configspace
;
719 SimObjectParam
<PciConfigData
*> configdata
;
720 SimObjectParam
<Platform
*> platform
;
721 Param
<uint32_t> pci_bus
;
722 Param
<uint32_t> pci_dev
;
723 Param
<uint32_t> pci_func
;
724 SimObjectParam
<Bus
*> pio_bus
;
725 SimObjectParam
<Bus
*> dma_bus
;
726 Param
<Tick
> pio_latency
;
727 SimObjectParam
<HierParams
*> hier
;
729 END_DECLARE_SIM_OBJECT_PARAMS(IdeController
)
731 BEGIN_INIT_SIM_OBJECT_PARAMS(IdeController
)
733 INIT_PARAM(addr
, "Device Address"),
734 INIT_PARAM(disks
, "IDE disks attached to this controller"),
735 INIT_PARAM(mmu
, "Memory controller"),
736 INIT_PARAM(configspace
, "PCI Configspace"),
737 INIT_PARAM(configdata
, "PCI Config data"),
738 INIT_PARAM(platform
, "Platform pointer"),
739 INIT_PARAM(pci_bus
, "PCI bus ID"),
740 INIT_PARAM(pci_dev
, "PCI device number"),
741 INIT_PARAM(pci_func
, "PCI function code"),
742 INIT_PARAM(pio_bus
, ""),
743 INIT_PARAM(dma_bus
, ""),
744 INIT_PARAM_DFLT(pio_latency
, "Programmed IO latency in bus cycles", 1),
745 INIT_PARAM_DFLT(hier
, "Hierarchy global variables", &defaultHierParams
)
747 END_INIT_SIM_OBJECT_PARAMS(IdeController
)
749 CREATE_SIM_OBJECT(IdeController
)
751 IdeController::Params
*params
= new IdeController::Params
;
752 params
->name
= getInstanceName();
754 params
->configSpace
= configspace
;
755 params
->configData
= configdata
;
756 params
->plat
= platform
;
757 params
->busNum
= pci_bus
;
758 params
->deviceNum
= pci_dev
;
759 params
->functionNum
= pci_func
;
761 params
->disks
= disks
;
762 params
->pio_bus
= pio_bus
;
763 params
->dma_bus
= dma_bus
;
764 params
->pio_latency
= pio_latency
;
766 return new IdeController(params
);
769 REGISTER_SIM_OBJECT("IdeController", IdeController
)
771 #endif //DOXYGEN_SHOULD_SKIP_THIS