|
| 1 | +/* |
| 2 | +DingusPPC - The Experimental PowerPC Macintosh emulator |
| 3 | +Copyright (C) 2018-24 divingkatae and maximum |
| 4 | + (theweirdo) spatium |
| 5 | +
|
| 6 | +(Contact divingkatae#1017 or powermax#2286 on Discord for more info) |
| 7 | +
|
| 8 | +This program is free software: you can redistribute it and/or modify |
| 9 | +it under the terms of the GNU General Public License as published by |
| 10 | +the Free Software Foundation, either version 3 of the License, or |
| 11 | +(at your option) any later version. |
| 12 | +
|
| 13 | +This program is distributed in the hope that it will be useful, |
| 14 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | +GNU General Public License for more details. |
| 17 | +
|
| 18 | +You should have received a copy of the GNU General Public License |
| 19 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | +*/ |
| 21 | + |
| 22 | +/** @file CMD646U2 PCI Ultra ATA controller emulation. */ |
| 23 | + |
| 24 | +#include <devices/common/ata/cmd646.h> |
| 25 | +#include <devices/deviceregistry.h> |
| 26 | +#include <loguru.hpp> |
| 27 | +#include <machines/machinebase.h> |
| 28 | + |
| 29 | +CmdIdeCtrl::CmdIdeCtrl() : PCIDevice("cmd-ide") { |
| 30 | + this->supports_types(HWCompType::PCI_DEV | HWCompType::IDE_HOST); |
| 31 | + |
| 32 | + // set up PCI configuration space header |
| 33 | + this->vendor_id = this->subsys_vndr = PCI_VENDOR_SILICON_IMAGE; |
| 34 | + this->device_id = this->subsys_id = DEV_ID_CMD646; |
| 35 | + this->class_rev = ((MY_DEV_CLASS | this->prog_if) << 8) | MY_REV_ID; |
| 36 | + this->max_lat = 4; |
| 37 | + this->min_gnt = 2; |
| 38 | + this->irq_pin = 1; |
| 39 | + |
| 40 | + this->bars_cfg[0] = 0xFFFFFFF9; // Command block I/O space, primary channel |
| 41 | + this->bars_cfg[1] = 0xFFFFFFFD; // Control block I/O space, primary channel |
| 42 | + this->bars_cfg[2] = 0xFFFFFFF9; // Command block I/O space, secondary channel |
| 43 | + this->bars_cfg[3] = 0xFFFFFFFD; // Control block I/O space, secondary channel |
| 44 | + this->bars_cfg[4] = 0xFFFFFFF1; // Bus master I/O space, both channels |
| 45 | + |
| 46 | + this->finish_config_bars(); |
| 47 | + |
| 48 | + this->pci_notify_bar_change = [this](int bar_num) { |
| 49 | + this->notify_bar_change(bar_num); |
| 50 | + }; |
| 51 | + |
| 52 | + gMachineObj->add_device("CmdAta0", std::unique_ptr<IdeChannel>(new IdeChannel("CmdAta0"))); |
| 53 | + gMachineObj->add_device("CmdAta1", std::unique_ptr<IdeChannel>(new IdeChannel("CmdAta1"))); |
| 54 | + |
| 55 | + this->ch0 = dynamic_cast<IdeChannel*>(gMachineObj->get_comp_by_name("CmdAta0")); |
| 56 | + this->ch1 = dynamic_cast<IdeChannel*>(gMachineObj->get_comp_by_name("CmdAta1")); |
| 57 | + |
| 58 | + this->ch0->set_irq_callback([this](const uint8_t intrq_state) { |
| 59 | + LOG_F(INFO, "CmdAta0 INTRQ updated to %d", intrq_state); |
| 60 | + this->update_irq(0, intrq_state); |
| 61 | + }); |
| 62 | + |
| 63 | + this->ch1->set_irq_callback([this](const uint8_t intrq_state) { |
| 64 | + LOG_F(INFO, "CmdAta1 INTRQ updated to %d", intrq_state); |
| 65 | + this->update_irq(1, intrq_state); |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +uint32_t CmdIdeCtrl::pci_cfg_read(uint32_t reg_offs, AccessDetails &details) { |
| 70 | + if (reg_offs < 64) |
| 71 | + return PCIDevice::pci_cfg_read(reg_offs, details); |
| 72 | + |
| 73 | + if (details.size != 1) |
| 74 | + ABORT_F("%s: non-byte read from PCI config reg 0x%X", this->name.c_str(), |
| 75 | + reg_offs + details.offset); |
| 76 | + |
| 77 | + if (reg_offs < 112) |
| 78 | + return this->read_config_reg(reg_offs + details.offset); |
| 79 | + |
| 80 | + LOG_F(WARNING, "%s: reading config reg at 0x%X", this->name.c_str(), reg_offs); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +void CmdIdeCtrl::pci_cfg_write(uint32_t reg_offs, uint32_t value, AccessDetails &details) { |
| 86 | + if (reg_offs < 64) { |
| 87 | + PCIDevice::pci_cfg_write(reg_offs, value, details); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + if (details.size != 1) |
| 92 | + ABORT_F("%s: non-byte write to PCI config reg 0x%X", this->name.c_str(), |
| 93 | + reg_offs + details.offset); |
| 94 | + |
| 95 | + if (reg_offs < 112) |
| 96 | + this->write_config_reg(reg_offs + details.offset, value); |
| 97 | + else |
| 98 | + LOG_F(WARNING, "%s: writing config reg at 0x%X", this->name.c_str(), reg_offs); |
| 99 | +} |
| 100 | + |
| 101 | +void CmdIdeCtrl::notify_bar_change(int bar_num) { |
| 102 | + if (bar_num >= 0 && bar_num <= 4) |
| 103 | + this->io_bases[bar_num] = this->bars[bar_num] & ~3; |
| 104 | +} |
| 105 | + |
| 106 | +bool CmdIdeCtrl::pci_io_read(uint32_t offset, uint32_t size, uint32_t* res) { |
| 107 | + *res = 0; |
| 108 | + |
| 109 | + if ((offset & ~7) == this->io_bases[0]) { |
| 110 | + *res = this->ch0->read(offset & 7, size); |
| 111 | + } else if ((offset & ~7) == this->io_bases[2]) { |
| 112 | + *res = this->ch1->read(offset & 7, size); |
| 113 | + } else if ((offset & ~3) == this->io_bases[1]) { |
| 114 | + *res = this->ch0->read((offset & 3) + DEV_CTRL_BLK_OFFSET, size); |
| 115 | + } else if ((offset & ~3) == this->io_bases[3]) { |
| 116 | + *res = this->ch1->read((offset & 3) + DEV_CTRL_BLK_OFFSET, size); |
| 117 | + } else if ((offset & ~0xF) == this->io_bases[4]) { |
| 118 | + *res = this->read_bus_master_reg(offset & 0xF); |
| 119 | + } else { |
| 120 | + *res = 0xFFFFFFFFUL; |
| 121 | + return false; |
| 122 | + } |
| 123 | + |
| 124 | + return true; |
| 125 | +} |
| 126 | + |
| 127 | +bool CmdIdeCtrl::pci_io_write(uint32_t offset, uint32_t value, uint32_t size) { |
| 128 | + if ((offset & ~7) == this->io_bases[0]) { |
| 129 | + this->ch0->write(offset & 7, value, size); |
| 130 | + } else if ((offset & ~7) == this->io_bases[2]) { |
| 131 | + this->ch1->write(offset & 7, value, size); |
| 132 | + } else if ((offset & ~3) == this->io_bases[1]) { |
| 133 | + this->ch0->write((offset & 3) + DEV_CTRL_BLK_OFFSET, value, size); |
| 134 | + } else if ((offset & ~3) == this->io_bases[3]) { |
| 135 | + this->ch1->write((offset & 3) + DEV_CTRL_BLK_OFFSET, value, size); |
| 136 | + } else if ((offset & ~0xF) == this->io_bases[4]) { |
| 137 | + this->write_bus_master_reg(offset & 0xF, value & 0xFFU); |
| 138 | + } else |
| 139 | + return false; |
| 140 | + |
| 141 | + return true; |
| 142 | +}; |
| 143 | + |
| 144 | +uint8_t CmdIdeCtrl::read_config_reg(uint32_t reg_offset) { |
| 145 | + switch(reg_offset) { |
| 146 | + case ARTTIM0: |
| 147 | + return this->addr_setup_time_0; |
| 148 | + case DRWTIM0: |
| 149 | + return this->data_rw_time_0; |
| 150 | + case ARTTIM1: |
| 151 | + return this->addr_setup_time_1; |
| 152 | + case DRWTIM1: |
| 153 | + return this->data_rw_time_1; |
| 154 | + default: |
| 155 | + LOG_F(ERROR, "%s: unimplemented config reg at 0x%X", this->name.c_str(), |
| 156 | + reg_offset); |
| 157 | + } |
| 158 | + |
| 159 | + return 0; |
| 160 | +} |
| 161 | + |
| 162 | +void CmdIdeCtrl::write_config_reg(uint32_t reg_offset, uint8_t val) { |
| 163 | + switch(reg_offset) { |
| 164 | + case ARTTIM0: |
| 165 | + this->addr_setup_time_0 = val; |
| 166 | + LOG_F(9, "%s: ARTTIM0 set to 0x%X", this->name.c_str(), val); |
| 167 | + break; |
| 168 | + case DRWTIM0: |
| 169 | + this->data_rw_time_0 = val; |
| 170 | + LOG_F(9, "%s: DRWTIM0 set to 0x%X", this->name.c_str(), val); |
| 171 | + break; |
| 172 | + case ARTTIM1: |
| 173 | + this->addr_setup_time_1 = val; |
| 174 | + LOG_F(9, "%s: ARTTIM1 set to 0x%X", this->name.c_str(), val); |
| 175 | + break; |
| 176 | + case DRWTIM1: |
| 177 | + this->data_rw_time_1 = val; |
| 178 | + LOG_F(9, "%s: DRWTIM1 set to 0x%X", this->name.c_str(), val); |
| 179 | + break; |
| 180 | + default: |
| 181 | + LOG_F(ERROR, "%s: unimplemented config reg at 0x%X", this->name.c_str(), |
| 182 | + reg_offset); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +uint8_t CmdIdeCtrl::read_bus_master_reg(const uint8_t reg_offset) { |
| 187 | + switch(reg_offset) { |
| 188 | + case MRDMODE: |
| 189 | + return this->mrdmode; |
| 190 | + default: |
| 191 | + LOG_F(ERROR, "%s: unimplemented bus master reg at 0x%X", this->name.c_str(), |
| 192 | + reg_offset); |
| 193 | + } |
| 194 | + |
| 195 | + return 0; |
| 196 | +} |
| 197 | + |
| 198 | +void CmdIdeCtrl::write_bus_master_reg(const uint8_t reg_offset, const uint8_t val) { |
| 199 | + switch(reg_offset) { |
| 200 | + case MRDMODE: |
| 201 | + if (val & BM_CH0_INT) |
| 202 | + this->mrdmode &= ~BM_CH0_INT; |
| 203 | + if (val & BM_CH1_INT) |
| 204 | + this->mrdmode &= ~BM_CH1_INT; |
| 205 | + this->mrdmode = (this->mrdmode & ~(BM_BLOCK_CH0_INT | BM_BLOCK_CH1_INT)) | |
| 206 | + (val & (BM_BLOCK_CH0_INT | BM_BLOCK_CH1_INT)); |
| 207 | + LOG_F(INFO, "%s: MRDMODE set to 0x%X", this->name.c_str(), val); |
| 208 | + if ((this->mrdmode & BM_CH0_INT) && !(this->mrdmode & BM_BLOCK_CH0_INT)) |
| 209 | + this->update_irq(0, 1); |
| 210 | + if ((this->mrdmode & BM_CH1_INT) && !(this->mrdmode & BM_BLOCK_CH1_INT)) |
| 211 | + this->update_irq(1, 1); |
| 212 | + break; |
| 213 | + case UDIDETCR0: |
| 214 | + this->udma_time_cr = val; |
| 215 | + break; |
| 216 | + default: |
| 217 | + LOG_F(ERROR, "%s: unimplemented bus master reg at 0x%X", this->name.c_str(), |
| 218 | + reg_offset); |
| 219 | + } |
| 220 | +} |
| 221 | + |
| 222 | +void CmdIdeCtrl::update_irq(const int ch_num, const uint8_t irq_level) { |
| 223 | + bool forward_irq = !(this->mrdmode & (ch_num ? BM_BLOCK_CH1_INT : BM_BLOCK_CH0_INT)); |
| 224 | + |
| 225 | + if (irq_level) |
| 226 | + this->mrdmode |= ch_num ? BM_CH1_INT : BM_CH0_INT; |
| 227 | + else |
| 228 | + this->mrdmode &= ~(ch_num ? BM_CH1_INT : BM_CH0_INT); |
| 229 | + |
| 230 | + if (!irq_level || forward_irq) |
| 231 | + this->irq_info.int_ctrl_obj->ack_int(this->irq_info.irq_id, irq_level); |
| 232 | +} |
| 233 | + |
| 234 | +static const DeviceDescription CmdIde_Descriptor = { |
| 235 | + CmdIdeCtrl::create, {}, {} |
| 236 | +}; |
| 237 | + |
| 238 | +REGISTER_DEVICE(CmdAta, CmdIde_Descriptor); |
0 commit comments