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 "qemu/osdep.h"
27 #include "hw/pci/pci.h"
29 #include "hw/nvram/eeprom93xx.h"
30 #include "hw/scsi/esp.h"
31 #include "migration/vmstate.h"
33 #include "qapi/error.h"
35 #include "qemu/module.h"
36 #include "qom/object.h"
38 #define TYPE_AM53C974_DEVICE "am53c974"
40 typedef struct PCIESPState PCIESPState
;
41 DECLARE_INSTANCE_CHECKER(PCIESPState
, PCI_ESP
,
53 #define DMA_CMD_MASK 0x03
54 #define DMA_CMD_DIAG 0x04
55 #define DMA_CMD_MDL 0x10
56 #define DMA_CMD_INTE_P 0x20
57 #define DMA_CMD_INTE_D 0x40
58 #define DMA_CMD_DIR 0x80
60 #define DMA_STAT_PWDN 0x01
61 #define DMA_STAT_ERROR 0x02
62 #define DMA_STAT_ABORT 0x04
63 #define DMA_STAT_DONE 0x08
64 #define DMA_STAT_SCSIINT 0x10
65 #define DMA_STAT_BCMBLT 0x20
67 #define SBAC_STATUS (1 << 24)
80 static void esp_pci_handle_idle(PCIESPState
*pci
, uint32_t val
)
82 trace_esp_pci_dma_idle(val
);
83 esp_dma_enable(&pci
->esp
, 0, 0);
86 static void esp_pci_handle_blast(PCIESPState
*pci
, uint32_t val
)
88 trace_esp_pci_dma_blast(val
);
89 qemu_log_mask(LOG_UNIMP
, "am53c974: cmd BLAST not implemented\n");
92 static void esp_pci_handle_abort(PCIESPState
*pci
, uint32_t val
)
94 trace_esp_pci_dma_abort(val
);
95 if (pci
->esp
.current_req
) {
96 scsi_req_cancel(pci
->esp
.current_req
);
100 static void esp_pci_handle_start(PCIESPState
*pci
, uint32_t val
)
102 trace_esp_pci_dma_start(val
);
104 pci
->dma_regs
[DMA_WBC
] = pci
->dma_regs
[DMA_STC
];
105 pci
->dma_regs
[DMA_WAC
] = pci
->dma_regs
[DMA_SPA
];
106 pci
->dma_regs
[DMA_WMAC
] = pci
->dma_regs
[DMA_SMDLA
];
108 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
109 | DMA_STAT_DONE
| DMA_STAT_ABORT
110 | DMA_STAT_ERROR
| DMA_STAT_PWDN
);
112 esp_dma_enable(&pci
->esp
, 0, 1);
115 static void esp_pci_dma_write(PCIESPState
*pci
, uint32_t saddr
, uint32_t val
)
117 trace_esp_pci_dma_write(saddr
, pci
->dma_regs
[saddr
], val
);
120 pci
->dma_regs
[saddr
] = val
;
121 switch (val
& DMA_CMD_MASK
) {
123 esp_pci_handle_idle(pci
, val
);
125 case 0x1: /* BLAST */
126 esp_pci_handle_blast(pci
, val
);
128 case 0x2: /* ABORT */
129 esp_pci_handle_abort(pci
, val
);
131 case 0x3: /* START */
132 esp_pci_handle_start(pci
, val
);
134 default: /* can't happen */
141 pci
->dma_regs
[saddr
] = val
;
144 if (pci
->sbac
& SBAC_STATUS
) {
145 /* clear some bits on write */
146 uint32_t mask
= DMA_STAT_ERROR
| DMA_STAT_ABORT
| DMA_STAT_DONE
;
147 pci
->dma_regs
[DMA_STAT
] &= ~(val
& mask
);
151 trace_esp_pci_error_invalid_write_dma(val
, saddr
);
156 static uint32_t esp_pci_dma_read(PCIESPState
*pci
, uint32_t saddr
)
160 val
= pci
->dma_regs
[saddr
];
161 if (saddr
== DMA_STAT
) {
162 if (pci
->esp
.rregs
[ESP_RSTAT
] & STAT_INT
) {
163 val
|= DMA_STAT_SCSIINT
;
165 if (!(pci
->sbac
& SBAC_STATUS
)) {
166 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_ERROR
| DMA_STAT_ABORT
|
171 trace_esp_pci_dma_read(saddr
, val
);
175 static void esp_pci_io_write(void *opaque
, hwaddr addr
,
176 uint64_t val
, unsigned int size
)
178 PCIESPState
*pci
= opaque
;
180 if (size
< 4 || addr
& 3) {
181 /* need to upgrade request: we only support 4-bytes accesses */
182 uint32_t current
= 0, mask
;
186 current
= pci
->esp
.wregs
[addr
>> 2];
187 } else if (addr
< 0x60) {
188 current
= pci
->dma_regs
[(addr
- 0x40) >> 2];
189 } else if (addr
< 0x74) {
193 shift
= (4 - size
) * 8;
194 mask
= (~(uint32_t)0 << shift
) >> shift
;
196 shift
= ((4 - (addr
& 3)) & 3) * 8;
198 val
|= current
& ~(mask
<< shift
);
206 esp_reg_write(&pci
->esp
, addr
>> 2, val
);
207 } else if (addr
< 0x60) {
209 esp_pci_dma_write(pci
, (addr
- 0x40) >> 2, val
);
210 } else if (addr
== 0x70) {
211 /* DMA SCSI Bus and control */
212 trace_esp_pci_sbac_write(pci
->sbac
, val
);
215 trace_esp_pci_error_invalid_write((int)addr
);
219 static uint64_t esp_pci_io_read(void *opaque
, hwaddr addr
,
222 PCIESPState
*pci
= opaque
;
227 ret
= esp_reg_read(&pci
->esp
, addr
>> 2);
228 } else if (addr
< 0x60) {
230 ret
= esp_pci_dma_read(pci
, (addr
- 0x40) >> 2);
231 } else if (addr
== 0x70) {
232 /* DMA SCSI Bus and control */
233 trace_esp_pci_sbac_read(pci
->sbac
);
237 trace_esp_pci_error_invalid_read((int)addr
);
241 /* give only requested data */
242 ret
>>= (addr
& 3) * 8;
243 ret
&= ~(~(uint64_t)0 << (8 * size
));
248 static void esp_pci_dma_memory_rw(PCIESPState
*pci
, uint8_t *buf
, int len
,
252 DMADirection expected_dir
;
254 if (pci
->dma_regs
[DMA_CMD
] & DMA_CMD_DIR
) {
255 expected_dir
= DMA_DIRECTION_FROM_DEVICE
;
257 expected_dir
= DMA_DIRECTION_TO_DEVICE
;
260 if (dir
!= expected_dir
) {
261 trace_esp_pci_error_invalid_dma_direction();
265 if (pci
->dma_regs
[DMA_STAT
] & DMA_CMD_MDL
) {
266 qemu_log_mask(LOG_UNIMP
, "am53c974: MDL transfer not implemented\n");
269 addr
= pci
->dma_regs
[DMA_SPA
];
270 if (pci
->dma_regs
[DMA_WBC
] < len
) {
271 len
= pci
->dma_regs
[DMA_WBC
];
274 pci_dma_rw(PCI_DEVICE(pci
), addr
, buf
, len
, dir
);
276 /* update status registers */
277 pci
->dma_regs
[DMA_WBC
] -= len
;
278 pci
->dma_regs
[DMA_WAC
] += len
;
279 if (pci
->dma_regs
[DMA_WBC
] == 0) {
280 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
284 static void esp_pci_dma_memory_read(void *opaque
, uint8_t *buf
, int len
)
286 PCIESPState
*pci
= opaque
;
287 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
290 static void esp_pci_dma_memory_write(void *opaque
, uint8_t *buf
, int len
)
292 PCIESPState
*pci
= opaque
;
293 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_FROM_DEVICE
);
296 static const MemoryRegionOps esp_pci_io_ops
= {
297 .read
= esp_pci_io_read
,
298 .write
= esp_pci_io_write
,
299 .endianness
= DEVICE_LITTLE_ENDIAN
,
301 .min_access_size
= 1,
302 .max_access_size
= 4,
306 static void esp_pci_hard_reset(DeviceState
*dev
)
308 PCIESPState
*pci
= PCI_ESP(dev
);
309 esp_hard_reset(&pci
->esp
);
310 pci
->dma_regs
[DMA_CMD
] &= ~(DMA_CMD_DIR
| DMA_CMD_INTE_D
| DMA_CMD_INTE_P
311 | DMA_CMD_MDL
| DMA_CMD_DIAG
| DMA_CMD_MASK
);
312 pci
->dma_regs
[DMA_WBC
] &= ~0xffff;
313 pci
->dma_regs
[DMA_WAC
] = 0xffffffff;
314 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
315 | DMA_STAT_DONE
| DMA_STAT_ABORT
317 pci
->dma_regs
[DMA_WMAC
] = 0xfffffffd;
320 static const VMStateDescription vmstate_esp_pci_scsi
= {
321 .name
= "pciespscsi",
323 .minimum_version_id
= 1,
324 .fields
= (VMStateField
[]) {
325 VMSTATE_PCI_DEVICE(parent_obj
, PCIESPState
),
326 VMSTATE_BUFFER_UNSAFE(dma_regs
, PCIESPState
, 0, 8 * sizeof(uint32_t)),
327 VMSTATE_STRUCT(esp
, PCIESPState
, 0, vmstate_esp
, ESPState
),
328 VMSTATE_END_OF_LIST()
332 static void esp_pci_command_complete(SCSIRequest
*req
, uint32_t status
,
335 ESPState
*s
= req
->hba_private
;
336 PCIESPState
*pci
= container_of(s
, PCIESPState
, esp
);
338 esp_command_complete(req
, status
, resid
);
339 pci
->dma_regs
[DMA_WBC
] = 0;
340 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
343 static const struct SCSIBusInfo esp_pci_scsi_info
= {
345 .max_target
= ESP_MAX_DEVS
,
348 .transfer_data
= esp_transfer_data
,
349 .complete
= esp_pci_command_complete
,
350 .cancel
= esp_request_cancelled
,
353 static void esp_pci_scsi_realize(PCIDevice
*dev
, Error
**errp
)
355 PCIESPState
*pci
= PCI_ESP(dev
);
356 DeviceState
*d
= DEVICE(dev
);
357 ESPState
*s
= &pci
->esp
;
360 pci_conf
= dev
->config
;
362 /* Interrupt pin A */
363 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
365 s
->dma_memory_read
= esp_pci_dma_memory_read
;
366 s
->dma_memory_write
= esp_pci_dma_memory_write
;
368 s
->chip_id
= TCHI_AM53C974
;
369 memory_region_init_io(&pci
->io
, OBJECT(pci
), &esp_pci_io_ops
, pci
,
372 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->io
);
373 s
->irq
= pci_allocate_irq(dev
);
375 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &esp_pci_scsi_info
, NULL
);
378 static void esp_pci_scsi_uninit(PCIDevice
*d
)
380 PCIESPState
*pci
= PCI_ESP(d
);
382 qemu_free_irq(pci
->esp
.irq
);
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
->realize
= esp_pci_scsi_realize
;
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
,
407 .interfaces
= (InterfaceInfo
[]) {
408 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
417 typedef struct DC390State DC390State
;
419 #define TYPE_DC390_DEVICE "dc390"
420 DECLARE_INSTANCE_CHECKER(DC390State
, DC390
,
423 #define EE_ADAPT_SCSI_ID 64
426 #define EE_TAG_CMD_NUM 67
427 #define EE_ADAPT_OPTIONS 68
428 #define EE_BOOT_SCSI_ID 69
429 #define EE_BOOT_SCSI_LUN 70
430 #define EE_CHKSUM1 126
431 #define EE_CHKSUM2 127
433 #define EE_ADAPT_OPTION_F6_F8_AT_BOOT 0x01
434 #define EE_ADAPT_OPTION_BOOT_FROM_CDROM 0x02
435 #define EE_ADAPT_OPTION_INT13 0x04
436 #define EE_ADAPT_OPTION_SCAM_SUPPORT 0x08
439 static uint32_t dc390_read_config(PCIDevice
*dev
, uint32_t addr
, int l
)
441 DC390State
*pci
= DC390(dev
);
444 val
= pci_default_read_config(dev
, addr
, l
);
446 if (addr
== 0x00 && l
== 1) {
447 /* First byte of address space is AND-ed with EEPROM DO line */
448 if (!eeprom93xx_read(pci
->eeprom
)) {
456 static void dc390_write_config(PCIDevice
*dev
,
457 uint32_t addr
, uint32_t val
, int l
)
459 DC390State
*pci
= DC390(dev
);
462 int eesk
= val
& 0x80 ? 1 : 0;
463 int eedi
= val
& 0x40 ? 1 : 0;
464 eeprom93xx_write(pci
->eeprom
, 1, eesk
, eedi
);
465 } else if (addr
== 0xc0) {
467 eeprom93xx_write(pci
->eeprom
, 0, 0, 0);
469 pci_default_write_config(dev
, addr
, val
, l
);
473 static void dc390_scsi_realize(PCIDevice
*dev
, Error
**errp
)
475 DC390State
*pci
= DC390(dev
);
481 /* init base class */
482 esp_pci_scsi_realize(dev
, &err
);
484 error_propagate(errp
, err
);
489 pci
->eeprom
= eeprom93xx_new(DEVICE(dev
), 64);
491 /* set default eeprom values */
492 contents
= (uint8_t *)eeprom93xx_data(pci
->eeprom
);
494 for (i
= 0; i
< 16; i
++) {
495 contents
[i
* 2] = 0x57;
496 contents
[i
* 2 + 1] = 0x00;
498 contents
[EE_ADAPT_SCSI_ID
] = 7;
499 contents
[EE_MODE2
] = 0x0f;
500 contents
[EE_TAG_CMD_NUM
] = 0x04;
501 contents
[EE_ADAPT_OPTIONS
] = EE_ADAPT_OPTION_F6_F8_AT_BOOT
502 | EE_ADAPT_OPTION_BOOT_FROM_CDROM
503 | EE_ADAPT_OPTION_INT13
;
505 /* update eeprom checksum */
506 for (i
= 0; i
< EE_CHKSUM1
; i
+= 2) {
507 chksum
+= contents
[i
] + (((uint16_t)contents
[i
+ 1]) << 8);
509 chksum
= 0x1234 - chksum
;
510 contents
[EE_CHKSUM1
] = chksum
& 0xff;
511 contents
[EE_CHKSUM2
] = chksum
>> 8;
514 static void dc390_class_init(ObjectClass
*klass
, void *data
)
516 DeviceClass
*dc
= DEVICE_CLASS(klass
);
517 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
519 k
->realize
= dc390_scsi_realize
;
520 k
->config_read
= dc390_read_config
;
521 k
->config_write
= dc390_write_config
;
522 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
523 dc
->desc
= "Tekram DC-390 SCSI adapter";
526 static const TypeInfo dc390_info
= {
527 .name
= TYPE_DC390_DEVICE
,
528 .parent
= TYPE_AM53C974_DEVICE
,
529 .instance_size
= sizeof(DC390State
),
530 .class_init
= dc390_class_init
,
533 static void esp_pci_register_types(void)
535 type_register_static(&esp_pci_info
);
536 type_register_static(&dc390_info
);
539 type_init(esp_pci_register_types
)