2 * QEMU ESP/NCR53C9x emulation
4 * Copyright (c) 2005-2006 Fabrice Bellard
5 * Copyright (c) 2012 Herve Poussineau
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "hw/pci/pci.h"
27 #include "hw/nvram/eeprom93xx.h"
28 #include "hw/scsi/esp.h"
32 #define TYPE_AM53C974_DEVICE "am53c974"
34 #define PCI_ESP(obj) \
35 OBJECT_CHECK(PCIESPState, (obj), TYPE_AM53C974_DEVICE)
46 #define DMA_CMD_MASK 0x03
47 #define DMA_CMD_DIAG 0x04
48 #define DMA_CMD_MDL 0x10
49 #define DMA_CMD_INTE_P 0x20
50 #define DMA_CMD_INTE_D 0x40
51 #define DMA_CMD_DIR 0x80
53 #define DMA_STAT_PWDN 0x01
54 #define DMA_STAT_ERROR 0x02
55 #define DMA_STAT_ABORT 0x04
56 #define DMA_STAT_DONE 0x08
57 #define DMA_STAT_SCSIINT 0x10
58 #define DMA_STAT_BCMBLT 0x20
60 #define SBAC_STATUS 0x1000
62 typedef struct PCIESPState
{
73 static void esp_pci_handle_idle(PCIESPState
*pci
, uint32_t val
)
75 trace_esp_pci_dma_idle(val
);
76 esp_dma_enable(&pci
->esp
, 0, 0);
79 static void esp_pci_handle_blast(PCIESPState
*pci
, uint32_t val
)
81 trace_esp_pci_dma_blast(val
);
82 qemu_log_mask(LOG_UNIMP
, "am53c974: cmd BLAST not implemented\n");
85 static void esp_pci_handle_abort(PCIESPState
*pci
, uint32_t val
)
87 trace_esp_pci_dma_abort(val
);
88 if (pci
->esp
.current_req
) {
89 scsi_req_cancel(pci
->esp
.current_req
);
93 static void esp_pci_handle_start(PCIESPState
*pci
, uint32_t val
)
95 trace_esp_pci_dma_start(val
);
97 pci
->dma_regs
[DMA_WBC
] = pci
->dma_regs
[DMA_STC
];
98 pci
->dma_regs
[DMA_WAC
] = pci
->dma_regs
[DMA_SPA
];
99 pci
->dma_regs
[DMA_WMAC
] = pci
->dma_regs
[DMA_SMDLA
];
101 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
102 | DMA_STAT_DONE
| DMA_STAT_ABORT
103 | DMA_STAT_ERROR
| DMA_STAT_PWDN
);
105 esp_dma_enable(&pci
->esp
, 0, 1);
108 static void esp_pci_dma_write(PCIESPState
*pci
, uint32_t saddr
, uint32_t val
)
110 trace_esp_pci_dma_write(saddr
, pci
->dma_regs
[saddr
], val
);
113 pci
->dma_regs
[saddr
] = val
;
114 switch (val
& DMA_CMD_MASK
) {
116 esp_pci_handle_idle(pci
, val
);
118 case 0x1: /* BLAST */
119 esp_pci_handle_blast(pci
, val
);
121 case 0x2: /* ABORT */
122 esp_pci_handle_abort(pci
, val
);
124 case 0x3: /* START */
125 esp_pci_handle_start(pci
, val
);
127 default: /* can't happen */
134 pci
->dma_regs
[saddr
] = val
;
137 if (!(pci
->sbac
& SBAC_STATUS
)) {
138 /* clear some bits on write */
139 uint32_t mask
= DMA_STAT_ERROR
| DMA_STAT_ABORT
| DMA_STAT_DONE
;
140 pci
->dma_regs
[DMA_STAT
] &= ~(val
& mask
);
144 trace_esp_pci_error_invalid_write_dma(val
, saddr
);
149 static uint32_t esp_pci_dma_read(PCIESPState
*pci
, uint32_t saddr
)
153 val
= pci
->dma_regs
[saddr
];
154 if (saddr
== DMA_STAT
) {
155 if (pci
->esp
.rregs
[ESP_RSTAT
] & STAT_INT
) {
156 val
|= DMA_STAT_SCSIINT
;
158 if (pci
->sbac
& SBAC_STATUS
) {
159 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_ERROR
| DMA_STAT_ABORT
|
164 trace_esp_pci_dma_read(saddr
, val
);
168 static void esp_pci_io_write(void *opaque
, hwaddr addr
,
169 uint64_t val
, unsigned int size
)
171 PCIESPState
*pci
= opaque
;
173 if (size
< 4 || addr
& 3) {
174 /* need to upgrade request: we only support 4-bytes accesses */
175 uint32_t current
= 0, mask
;
179 current
= pci
->esp
.wregs
[addr
>> 2];
180 } else if (addr
< 0x60) {
181 current
= pci
->dma_regs
[(addr
- 0x40) >> 2];
182 } else if (addr
< 0x74) {
186 shift
= (4 - size
) * 8;
187 mask
= (~(uint32_t)0 << shift
) >> shift
;
189 shift
= ((4 - (addr
& 3)) & 3) * 8;
191 val
|= current
& ~(mask
<< shift
);
198 esp_reg_write(&pci
->esp
, addr
>> 2, val
);
199 } else if (addr
< 0x60) {
201 esp_pci_dma_write(pci
, (addr
- 0x40) >> 2, val
);
202 } else if (addr
== 0x70) {
203 /* DMA SCSI Bus and control */
204 trace_esp_pci_sbac_write(pci
->sbac
, val
);
207 trace_esp_pci_error_invalid_write((int)addr
);
211 static uint64_t esp_pci_io_read(void *opaque
, hwaddr addr
,
214 PCIESPState
*pci
= opaque
;
219 ret
= esp_reg_read(&pci
->esp
, addr
>> 2);
220 } else if (addr
< 0x60) {
222 ret
= esp_pci_dma_read(pci
, (addr
- 0x40) >> 2);
223 } else if (addr
== 0x70) {
224 /* DMA SCSI Bus and control */
225 trace_esp_pci_sbac_read(pci
->sbac
);
229 trace_esp_pci_error_invalid_read((int)addr
);
233 /* give only requested data */
234 ret
>>= (addr
& 3) * 8;
235 ret
&= ~(~(uint64_t)0 << (8 * size
));
240 static void esp_pci_dma_memory_rw(PCIESPState
*pci
, uint8_t *buf
, int len
,
244 DMADirection expected_dir
;
246 if (pci
->dma_regs
[DMA_CMD
] & DMA_CMD_DIR
) {
247 expected_dir
= DMA_DIRECTION_FROM_DEVICE
;
249 expected_dir
= DMA_DIRECTION_TO_DEVICE
;
252 if (dir
!= expected_dir
) {
253 trace_esp_pci_error_invalid_dma_direction();
257 if (pci
->dma_regs
[DMA_STAT
] & DMA_CMD_MDL
) {
258 qemu_log_mask(LOG_UNIMP
, "am53c974: MDL transfer not implemented\n");
261 addr
= pci
->dma_regs
[DMA_SPA
];
262 if (pci
->dma_regs
[DMA_WBC
] < len
) {
263 len
= pci
->dma_regs
[DMA_WBC
];
266 pci_dma_rw(PCI_DEVICE(pci
), addr
, buf
, len
, dir
);
268 /* update status registers */
269 pci
->dma_regs
[DMA_WBC
] -= len
;
270 pci
->dma_regs
[DMA_WAC
] += len
;
273 static void esp_pci_dma_memory_read(void *opaque
, uint8_t *buf
, int len
)
275 PCIESPState
*pci
= opaque
;
276 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
279 static void esp_pci_dma_memory_write(void *opaque
, uint8_t *buf
, int len
)
281 PCIESPState
*pci
= opaque
;
282 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_FROM_DEVICE
);
285 static const MemoryRegionOps esp_pci_io_ops
= {
286 .read
= esp_pci_io_read
,
287 .write
= esp_pci_io_write
,
288 .endianness
= DEVICE_LITTLE_ENDIAN
,
290 .min_access_size
= 1,
291 .max_access_size
= 4,
295 static void esp_pci_hard_reset(DeviceState
*dev
)
297 PCIESPState
*pci
= PCI_ESP(dev
);
298 esp_hard_reset(&pci
->esp
);
299 pci
->dma_regs
[DMA_CMD
] &= ~(DMA_CMD_DIR
| DMA_CMD_INTE_D
| DMA_CMD_INTE_P
300 | DMA_CMD_MDL
| DMA_CMD_DIAG
| DMA_CMD_MASK
);
301 pci
->dma_regs
[DMA_WBC
] &= ~0xffff;
302 pci
->dma_regs
[DMA_WAC
] = 0xffffffff;
303 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
304 | DMA_STAT_DONE
| DMA_STAT_ABORT
306 pci
->dma_regs
[DMA_WMAC
] = 0xfffffffd;
309 static const VMStateDescription vmstate_esp_pci_scsi
= {
310 .name
= "pciespscsi",
312 .minimum_version_id
= 0,
313 .minimum_version_id_old
= 0,
314 .fields
= (VMStateField
[]) {
315 VMSTATE_PCI_DEVICE(parent_obj
, PCIESPState
),
316 VMSTATE_BUFFER_UNSAFE(dma_regs
, PCIESPState
, 0, 8 * sizeof(uint32_t)),
317 VMSTATE_STRUCT(esp
, PCIESPState
, 0, vmstate_esp
, ESPState
),
318 VMSTATE_END_OF_LIST()
322 static void esp_pci_command_complete(SCSIRequest
*req
, uint32_t status
,
325 ESPState
*s
= req
->hba_private
;
326 PCIESPState
*pci
= container_of(s
, PCIESPState
, esp
);
328 esp_command_complete(req
, status
, resid
);
329 pci
->dma_regs
[DMA_WBC
] = 0;
330 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
333 static const struct SCSIBusInfo esp_pci_scsi_info
= {
335 .max_target
= ESP_MAX_DEVS
,
338 .transfer_data
= esp_transfer_data
,
339 .complete
= esp_pci_command_complete
,
340 .cancel
= esp_request_cancelled
,
343 static int esp_pci_scsi_init(PCIDevice
*dev
)
345 PCIESPState
*pci
= PCI_ESP(dev
);
346 DeviceState
*d
= DEVICE(dev
);
347 ESPState
*s
= &pci
->esp
;
351 pci_conf
= dev
->config
;
353 /* Interrupt pin A */
354 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
356 s
->dma_memory_read
= esp_pci_dma_memory_read
;
357 s
->dma_memory_write
= esp_pci_dma_memory_write
;
359 s
->chip_id
= TCHI_AM53C974
;
360 memory_region_init_io(&pci
->io
, OBJECT(pci
), &esp_pci_io_ops
, pci
,
363 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->io
);
364 s
->irq
= pci_allocate_irq(dev
);
366 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &esp_pci_scsi_info
, NULL
);
367 if (!d
->hotplugged
) {
368 scsi_bus_legacy_handle_cmdline(&s
->bus
, &err
);
377 static void esp_pci_scsi_uninit(PCIDevice
*d
)
379 PCIESPState
*pci
= PCI_ESP(d
);
381 qemu_free_irq(pci
->esp
.irq
);
382 memory_region_destroy(&pci
->io
);
385 static void esp_pci_class_init(ObjectClass
*klass
, void *data
)
387 DeviceClass
*dc
= DEVICE_CLASS(klass
);
388 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
390 k
->init
= esp_pci_scsi_init
;
391 k
->exit
= esp_pci_scsi_uninit
;
392 k
->vendor_id
= PCI_VENDOR_ID_AMD
;
393 k
->device_id
= PCI_DEVICE_ID_AMD_SCSI
;
395 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
396 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
397 dc
->desc
= "AMD Am53c974 PCscsi-PCI SCSI adapter";
398 dc
->reset
= esp_pci_hard_reset
;
399 dc
->vmsd
= &vmstate_esp_pci_scsi
;
402 static const TypeInfo esp_pci_info
= {
403 .name
= TYPE_AM53C974_DEVICE
,
404 .parent
= TYPE_PCI_DEVICE
,
405 .instance_size
= sizeof(PCIESPState
),
406 .class_init
= esp_pci_class_init
,
414 #define TYPE_DC390_DEVICE "dc390"
416 OBJECT_CHECK(DC390State, obj, TYPE_DC390_DEVICE)
418 #define EE_ADAPT_SCSI_ID 64
421 #define EE_TAG_CMD_NUM 67
422 #define EE_ADAPT_OPTIONS 68
423 #define EE_BOOT_SCSI_ID 69
424 #define EE_BOOT_SCSI_LUN 70
425 #define EE_CHKSUM1 126
426 #define EE_CHKSUM2 127
428 #define EE_ADAPT_OPTION_F6_F8_AT_BOOT 0x01
429 #define EE_ADAPT_OPTION_BOOT_FROM_CDROM 0x02
430 #define EE_ADAPT_OPTION_INT13 0x04
431 #define EE_ADAPT_OPTION_SCAM_SUPPORT 0x08
434 static uint32_t dc390_read_config(PCIDevice
*dev
, uint32_t addr
, int l
)
436 DC390State
*pci
= DC390(dev
);
439 val
= pci_default_read_config(dev
, addr
, l
);
441 if (addr
== 0x00 && l
== 1) {
442 /* First byte of address space is AND-ed with EEPROM DO line */
443 if (!eeprom93xx_read(pci
->eeprom
)) {
451 static void dc390_write_config(PCIDevice
*dev
,
452 uint32_t addr
, uint32_t val
, int l
)
454 DC390State
*pci
= DC390(dev
);
457 int eesk
= val
& 0x80 ? 1 : 0;
458 int eedi
= val
& 0x40 ? 1 : 0;
459 eeprom93xx_write(pci
->eeprom
, 1, eesk
, eedi
);
460 } else if (addr
== 0xc0) {
462 eeprom93xx_write(pci
->eeprom
, 0, 0, 0);
464 pci_default_write_config(dev
, addr
, val
, l
);
468 static int dc390_scsi_init(PCIDevice
*dev
)
470 DC390State
*pci
= DC390(dev
);
475 /* init base class */
476 ret
= esp_pci_scsi_init(dev
);
482 pci
->eeprom
= eeprom93xx_new(DEVICE(dev
), 64);
484 /* set default eeprom values */
485 contents
= (uint8_t *)eeprom93xx_data(pci
->eeprom
);
487 for (i
= 0; i
< 16; i
++) {
488 contents
[i
* 2] = 0x57;
489 contents
[i
* 2 + 1] = 0x00;
491 contents
[EE_ADAPT_SCSI_ID
] = 7;
492 contents
[EE_MODE2
] = 0x0f;
493 contents
[EE_TAG_CMD_NUM
] = 0x04;
494 contents
[EE_ADAPT_OPTIONS
] = EE_ADAPT_OPTION_F6_F8_AT_BOOT
495 | EE_ADAPT_OPTION_BOOT_FROM_CDROM
496 | EE_ADAPT_OPTION_INT13
;
498 /* update eeprom checksum */
499 for (i
= 0; i
< EE_CHKSUM1
; i
+= 2) {
500 chksum
+= contents
[i
] + (((uint16_t)contents
[i
+ 1]) << 8);
502 chksum
= 0x1234 - chksum
;
503 contents
[EE_CHKSUM1
] = chksum
& 0xff;
504 contents
[EE_CHKSUM2
] = chksum
>> 8;
509 static void dc390_class_init(ObjectClass
*klass
, void *data
)
511 DeviceClass
*dc
= DEVICE_CLASS(klass
);
512 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
514 k
->init
= dc390_scsi_init
;
515 k
->config_read
= dc390_read_config
;
516 k
->config_write
= dc390_write_config
;
517 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
518 dc
->desc
= "Tekram DC-390 SCSI adapter";
521 static const TypeInfo dc390_info
= {
523 .parent
= TYPE_AM53C974_DEVICE
,
524 .instance_size
= sizeof(DC390State
),
525 .class_init
= dc390_class_init
,
528 static void esp_pci_register_types(void)
530 type_register_static(&esp_pci_info
);
531 type_register_static(&dc390_info
);
534 type_init(esp_pci_register_types
)