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 ESPState
*s
= ESP(&pci
->esp
);
84 trace_esp_pci_dma_idle(val
);
85 esp_dma_enable(s
, 0, 0);
88 static void esp_pci_handle_blast(PCIESPState
*pci
, uint32_t val
)
90 trace_esp_pci_dma_blast(val
);
91 qemu_log_mask(LOG_UNIMP
, "am53c974: cmd BLAST not implemented\n");
94 static void esp_pci_handle_abort(PCIESPState
*pci
, uint32_t val
)
96 ESPState
*s
= ESP(&pci
->esp
);
98 trace_esp_pci_dma_abort(val
);
100 scsi_req_cancel(s
->current_req
);
104 static void esp_pci_handle_start(PCIESPState
*pci
, uint32_t val
)
106 ESPState
*s
= ESP(&pci
->esp
);
108 trace_esp_pci_dma_start(val
);
110 pci
->dma_regs
[DMA_WBC
] = pci
->dma_regs
[DMA_STC
];
111 pci
->dma_regs
[DMA_WAC
] = pci
->dma_regs
[DMA_SPA
];
112 pci
->dma_regs
[DMA_WMAC
] = pci
->dma_regs
[DMA_SMDLA
];
114 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
115 | DMA_STAT_DONE
| DMA_STAT_ABORT
116 | DMA_STAT_ERROR
| DMA_STAT_PWDN
);
118 esp_dma_enable(s
, 0, 1);
121 static void esp_pci_dma_write(PCIESPState
*pci
, uint32_t saddr
, uint32_t val
)
123 trace_esp_pci_dma_write(saddr
, pci
->dma_regs
[saddr
], val
);
126 pci
->dma_regs
[saddr
] = val
;
127 switch (val
& DMA_CMD_MASK
) {
129 esp_pci_handle_idle(pci
, val
);
131 case 0x1: /* BLAST */
132 esp_pci_handle_blast(pci
, val
);
134 case 0x2: /* ABORT */
135 esp_pci_handle_abort(pci
, val
);
137 case 0x3: /* START */
138 esp_pci_handle_start(pci
, val
);
140 default: /* can't happen */
147 pci
->dma_regs
[saddr
] = val
;
150 if (pci
->sbac
& SBAC_STATUS
) {
151 /* clear some bits on write */
152 uint32_t mask
= DMA_STAT_ERROR
| DMA_STAT_ABORT
| DMA_STAT_DONE
;
153 pci
->dma_regs
[DMA_STAT
] &= ~(val
& mask
);
157 trace_esp_pci_error_invalid_write_dma(val
, saddr
);
162 static uint32_t esp_pci_dma_read(PCIESPState
*pci
, uint32_t saddr
)
164 ESPState
*s
= ESP(&pci
->esp
);
167 val
= pci
->dma_regs
[saddr
];
168 if (saddr
== DMA_STAT
) {
169 if (s
->rregs
[ESP_RSTAT
] & STAT_INT
) {
170 val
|= DMA_STAT_SCSIINT
;
172 if (!(pci
->sbac
& SBAC_STATUS
)) {
173 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_ERROR
| DMA_STAT_ABORT
|
178 trace_esp_pci_dma_read(saddr
, val
);
182 static void esp_pci_io_write(void *opaque
, hwaddr addr
,
183 uint64_t val
, unsigned int size
)
185 PCIESPState
*pci
= opaque
;
186 ESPState
*s
= ESP(&pci
->esp
);
188 if (size
< 4 || addr
& 3) {
189 /* need to upgrade request: we only support 4-bytes accesses */
190 uint32_t current
= 0, mask
;
194 current
= s
->wregs
[addr
>> 2];
195 } else if (addr
< 0x60) {
196 current
= pci
->dma_regs
[(addr
- 0x40) >> 2];
197 } else if (addr
< 0x74) {
201 shift
= (4 - size
) * 8;
202 mask
= (~(uint32_t)0 << shift
) >> shift
;
204 shift
= ((4 - (addr
& 3)) & 3) * 8;
206 val
|= current
& ~(mask
<< shift
);
214 esp_reg_write(s
, addr
>> 2, val
);
215 } else if (addr
< 0x60) {
217 esp_pci_dma_write(pci
, (addr
- 0x40) >> 2, val
);
218 } else if (addr
== 0x70) {
219 /* DMA SCSI Bus and control */
220 trace_esp_pci_sbac_write(pci
->sbac
, val
);
223 trace_esp_pci_error_invalid_write((int)addr
);
227 static uint64_t esp_pci_io_read(void *opaque
, hwaddr addr
,
230 PCIESPState
*pci
= opaque
;
231 ESPState
*s
= ESP(&pci
->esp
);
236 ret
= esp_reg_read(s
, addr
>> 2);
237 } else if (addr
< 0x60) {
239 ret
= esp_pci_dma_read(pci
, (addr
- 0x40) >> 2);
240 } else if (addr
== 0x70) {
241 /* DMA SCSI Bus and control */
242 trace_esp_pci_sbac_read(pci
->sbac
);
246 trace_esp_pci_error_invalid_read((int)addr
);
250 /* give only requested data */
251 ret
>>= (addr
& 3) * 8;
252 ret
&= ~(~(uint64_t)0 << (8 * size
));
257 static void esp_pci_dma_memory_rw(PCIESPState
*pci
, uint8_t *buf
, int len
,
261 DMADirection expected_dir
;
263 if (pci
->dma_regs
[DMA_CMD
] & DMA_CMD_DIR
) {
264 expected_dir
= DMA_DIRECTION_FROM_DEVICE
;
266 expected_dir
= DMA_DIRECTION_TO_DEVICE
;
269 if (dir
!= expected_dir
) {
270 trace_esp_pci_error_invalid_dma_direction();
274 if (pci
->dma_regs
[DMA_STAT
] & DMA_CMD_MDL
) {
275 qemu_log_mask(LOG_UNIMP
, "am53c974: MDL transfer not implemented\n");
278 addr
= pci
->dma_regs
[DMA_SPA
];
279 if (pci
->dma_regs
[DMA_WBC
] < len
) {
280 len
= pci
->dma_regs
[DMA_WBC
];
283 pci_dma_rw(PCI_DEVICE(pci
), addr
, buf
, len
, dir
);
285 /* update status registers */
286 pci
->dma_regs
[DMA_WBC
] -= len
;
287 pci
->dma_regs
[DMA_WAC
] += len
;
288 if (pci
->dma_regs
[DMA_WBC
] == 0) {
289 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
293 static void esp_pci_dma_memory_read(void *opaque
, uint8_t *buf
, int len
)
295 PCIESPState
*pci
= opaque
;
296 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
299 static void esp_pci_dma_memory_write(void *opaque
, uint8_t *buf
, int len
)
301 PCIESPState
*pci
= opaque
;
302 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_FROM_DEVICE
);
305 static const MemoryRegionOps esp_pci_io_ops
= {
306 .read
= esp_pci_io_read
,
307 .write
= esp_pci_io_write
,
308 .endianness
= DEVICE_LITTLE_ENDIAN
,
310 .min_access_size
= 1,
311 .max_access_size
= 4,
315 static void esp_pci_hard_reset(DeviceState
*dev
)
317 PCIESPState
*pci
= PCI_ESP(dev
);
318 ESPState
*s
= ESP(&pci
->esp
);
321 pci
->dma_regs
[DMA_CMD
] &= ~(DMA_CMD_DIR
| DMA_CMD_INTE_D
| DMA_CMD_INTE_P
322 | DMA_CMD_MDL
| DMA_CMD_DIAG
| DMA_CMD_MASK
);
323 pci
->dma_regs
[DMA_WBC
] &= ~0xffff;
324 pci
->dma_regs
[DMA_WAC
] = 0xffffffff;
325 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
326 | DMA_STAT_DONE
| DMA_STAT_ABORT
328 pci
->dma_regs
[DMA_WMAC
] = 0xfffffffd;
331 static const VMStateDescription vmstate_esp_pci_scsi
= {
332 .name
= "pciespscsi",
334 .minimum_version_id
= 1,
335 .fields
= (VMStateField
[]) {
336 VMSTATE_PCI_DEVICE(parent_obj
, PCIESPState
),
337 VMSTATE_BUFFER_UNSAFE(dma_regs
, PCIESPState
, 0, 8 * sizeof(uint32_t)),
338 VMSTATE_UINT8_V(esp
.mig_version_id
, PCIESPState
, 2),
339 VMSTATE_STRUCT(esp
, PCIESPState
, 0, vmstate_esp
, ESPState
),
340 VMSTATE_END_OF_LIST()
344 static void esp_pci_command_complete(SCSIRequest
*req
, size_t resid
)
346 ESPState
*s
= req
->hba_private
;
347 PCIESPState
*pci
= container_of(s
, PCIESPState
, esp
);
349 esp_command_complete(req
, resid
);
350 pci
->dma_regs
[DMA_WBC
] = 0;
351 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
354 static const struct SCSIBusInfo esp_pci_scsi_info
= {
356 .max_target
= ESP_MAX_DEVS
,
359 .transfer_data
= esp_transfer_data
,
360 .complete
= esp_pci_command_complete
,
361 .cancel
= esp_request_cancelled
,
364 static void esp_pci_scsi_realize(PCIDevice
*dev
, Error
**errp
)
366 PCIESPState
*pci
= PCI_ESP(dev
);
367 DeviceState
*d
= DEVICE(dev
);
368 ESPState
*s
= ESP(&pci
->esp
);
371 if (!qdev_realize(DEVICE(s
), NULL
, errp
)) {
375 pci_conf
= dev
->config
;
377 /* Interrupt pin A */
378 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
380 s
->dma_memory_read
= esp_pci_dma_memory_read
;
381 s
->dma_memory_write
= esp_pci_dma_memory_write
;
383 s
->chip_id
= TCHI_AM53C974
;
384 memory_region_init_io(&pci
->io
, OBJECT(pci
), &esp_pci_io_ops
, pci
,
387 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->io
);
388 s
->irq
= pci_allocate_irq(dev
);
390 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &esp_pci_scsi_info
, NULL
);
393 static void esp_pci_scsi_exit(PCIDevice
*d
)
395 PCIESPState
*pci
= PCI_ESP(d
);
396 ESPState
*s
= ESP(&pci
->esp
);
398 qemu_free_irq(s
->irq
);
401 static void esp_pci_init(Object
*obj
)
403 PCIESPState
*pci
= PCI_ESP(obj
);
405 object_initialize_child(obj
, "esp", &pci
->esp
, TYPE_ESP
);
408 static void esp_pci_class_init(ObjectClass
*klass
, void *data
)
410 DeviceClass
*dc
= DEVICE_CLASS(klass
);
411 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
413 k
->realize
= esp_pci_scsi_realize
;
414 k
->exit
= esp_pci_scsi_exit
;
415 k
->vendor_id
= PCI_VENDOR_ID_AMD
;
416 k
->device_id
= PCI_DEVICE_ID_AMD_SCSI
;
418 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
419 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
420 dc
->desc
= "AMD Am53c974 PCscsi-PCI SCSI adapter";
421 dc
->reset
= esp_pci_hard_reset
;
422 dc
->vmsd
= &vmstate_esp_pci_scsi
;
425 static const TypeInfo esp_pci_info
= {
426 .name
= TYPE_AM53C974_DEVICE
,
427 .parent
= TYPE_PCI_DEVICE
,
428 .instance_init
= esp_pci_init
,
429 .instance_size
= sizeof(PCIESPState
),
430 .class_init
= esp_pci_class_init
,
431 .interfaces
= (InterfaceInfo
[]) {
432 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
441 typedef struct DC390State DC390State
;
443 #define TYPE_DC390_DEVICE "dc390"
444 DECLARE_INSTANCE_CHECKER(DC390State
, DC390
,
447 #define EE_ADAPT_SCSI_ID 64
450 #define EE_TAG_CMD_NUM 67
451 #define EE_ADAPT_OPTIONS 68
452 #define EE_BOOT_SCSI_ID 69
453 #define EE_BOOT_SCSI_LUN 70
454 #define EE_CHKSUM1 126
455 #define EE_CHKSUM2 127
457 #define EE_ADAPT_OPTION_F6_F8_AT_BOOT 0x01
458 #define EE_ADAPT_OPTION_BOOT_FROM_CDROM 0x02
459 #define EE_ADAPT_OPTION_INT13 0x04
460 #define EE_ADAPT_OPTION_SCAM_SUPPORT 0x08
463 static uint32_t dc390_read_config(PCIDevice
*dev
, uint32_t addr
, int l
)
465 DC390State
*pci
= DC390(dev
);
468 val
= pci_default_read_config(dev
, addr
, l
);
470 if (addr
== 0x00 && l
== 1) {
471 /* First byte of address space is AND-ed with EEPROM DO line */
472 if (!eeprom93xx_read(pci
->eeprom
)) {
480 static void dc390_write_config(PCIDevice
*dev
,
481 uint32_t addr
, uint32_t val
, int l
)
483 DC390State
*pci
= DC390(dev
);
486 int eesk
= val
& 0x80 ? 1 : 0;
487 int eedi
= val
& 0x40 ? 1 : 0;
488 eeprom93xx_write(pci
->eeprom
, 1, eesk
, eedi
);
489 } else if (addr
== 0xc0) {
491 eeprom93xx_write(pci
->eeprom
, 0, 0, 0);
493 pci_default_write_config(dev
, addr
, val
, l
);
497 static void dc390_scsi_realize(PCIDevice
*dev
, Error
**errp
)
499 DC390State
*pci
= DC390(dev
);
505 /* init base class */
506 esp_pci_scsi_realize(dev
, &err
);
508 error_propagate(errp
, err
);
513 pci
->eeprom
= eeprom93xx_new(DEVICE(dev
), 64);
515 /* set default eeprom values */
516 contents
= (uint8_t *)eeprom93xx_data(pci
->eeprom
);
518 for (i
= 0; i
< 16; i
++) {
519 contents
[i
* 2] = 0x57;
520 contents
[i
* 2 + 1] = 0x00;
522 contents
[EE_ADAPT_SCSI_ID
] = 7;
523 contents
[EE_MODE2
] = 0x0f;
524 contents
[EE_TAG_CMD_NUM
] = 0x04;
525 contents
[EE_ADAPT_OPTIONS
] = EE_ADAPT_OPTION_F6_F8_AT_BOOT
526 | EE_ADAPT_OPTION_BOOT_FROM_CDROM
527 | EE_ADAPT_OPTION_INT13
;
529 /* update eeprom checksum */
530 for (i
= 0; i
< EE_CHKSUM1
; i
+= 2) {
531 chksum
+= contents
[i
] + (((uint16_t)contents
[i
+ 1]) << 8);
533 chksum
= 0x1234 - chksum
;
534 contents
[EE_CHKSUM1
] = chksum
& 0xff;
535 contents
[EE_CHKSUM2
] = chksum
>> 8;
538 static void dc390_class_init(ObjectClass
*klass
, void *data
)
540 DeviceClass
*dc
= DEVICE_CLASS(klass
);
541 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
543 k
->realize
= dc390_scsi_realize
;
544 k
->config_read
= dc390_read_config
;
545 k
->config_write
= dc390_write_config
;
546 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
547 dc
->desc
= "Tekram DC-390 SCSI adapter";
550 static const TypeInfo dc390_info
= {
551 .name
= TYPE_DC390_DEVICE
,
552 .parent
= TYPE_AM53C974_DEVICE
,
553 .instance_size
= sizeof(DC390State
),
554 .class_init
= dc390_class_init
,
557 static void esp_pci_register_types(void)
559 type_register_static(&esp_pci_info
);
560 type_register_static(&dc390_info
);
563 type_init(esp_pci_register_types
)