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"
37 #define TYPE_AM53C974_DEVICE "am53c974"
39 #define PCI_ESP(obj) \
40 OBJECT_CHECK(PCIESPState, (obj), TYPE_AM53C974_DEVICE)
51 #define DMA_CMD_MASK 0x03
52 #define DMA_CMD_DIAG 0x04
53 #define DMA_CMD_MDL 0x10
54 #define DMA_CMD_INTE_P 0x20
55 #define DMA_CMD_INTE_D 0x40
56 #define DMA_CMD_DIR 0x80
58 #define DMA_STAT_PWDN 0x01
59 #define DMA_STAT_ERROR 0x02
60 #define DMA_STAT_ABORT 0x04
61 #define DMA_STAT_DONE 0x08
62 #define DMA_STAT_SCSIINT 0x10
63 #define DMA_STAT_BCMBLT 0x20
65 #define SBAC_STATUS (1 << 24)
67 typedef struct PCIESPState
{
78 static void esp_pci_handle_idle(PCIESPState
*pci
, uint32_t val
)
80 trace_esp_pci_dma_idle(val
);
81 esp_dma_enable(&pci
->esp
, 0, 0);
84 static void esp_pci_handle_blast(PCIESPState
*pci
, uint32_t val
)
86 trace_esp_pci_dma_blast(val
);
87 qemu_log_mask(LOG_UNIMP
, "am53c974: cmd BLAST not implemented\n");
90 static void esp_pci_handle_abort(PCIESPState
*pci
, uint32_t val
)
92 trace_esp_pci_dma_abort(val
);
93 if (pci
->esp
.current_req
) {
94 scsi_req_cancel(pci
->esp
.current_req
);
98 static void esp_pci_handle_start(PCIESPState
*pci
, uint32_t val
)
100 trace_esp_pci_dma_start(val
);
102 pci
->dma_regs
[DMA_WBC
] = pci
->dma_regs
[DMA_STC
];
103 pci
->dma_regs
[DMA_WAC
] = pci
->dma_regs
[DMA_SPA
];
104 pci
->dma_regs
[DMA_WMAC
] = pci
->dma_regs
[DMA_SMDLA
];
106 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
107 | DMA_STAT_DONE
| DMA_STAT_ABORT
108 | DMA_STAT_ERROR
| DMA_STAT_PWDN
);
110 esp_dma_enable(&pci
->esp
, 0, 1);
113 static void esp_pci_dma_write(PCIESPState
*pci
, uint32_t saddr
, uint32_t val
)
115 trace_esp_pci_dma_write(saddr
, pci
->dma_regs
[saddr
], val
);
118 pci
->dma_regs
[saddr
] = val
;
119 switch (val
& DMA_CMD_MASK
) {
121 esp_pci_handle_idle(pci
, val
);
123 case 0x1: /* BLAST */
124 esp_pci_handle_blast(pci
, val
);
126 case 0x2: /* ABORT */
127 esp_pci_handle_abort(pci
, val
);
129 case 0x3: /* START */
130 esp_pci_handle_start(pci
, val
);
132 default: /* can't happen */
139 pci
->dma_regs
[saddr
] = val
;
142 if (pci
->sbac
& SBAC_STATUS
) {
143 /* clear some bits on write */
144 uint32_t mask
= DMA_STAT_ERROR
| DMA_STAT_ABORT
| DMA_STAT_DONE
;
145 pci
->dma_regs
[DMA_STAT
] &= ~(val
& mask
);
149 trace_esp_pci_error_invalid_write_dma(val
, saddr
);
154 static uint32_t esp_pci_dma_read(PCIESPState
*pci
, uint32_t saddr
)
158 val
= pci
->dma_regs
[saddr
];
159 if (saddr
== DMA_STAT
) {
160 if (pci
->esp
.rregs
[ESP_RSTAT
] & STAT_INT
) {
161 val
|= DMA_STAT_SCSIINT
;
163 if (!(pci
->sbac
& SBAC_STATUS
)) {
164 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_ERROR
| DMA_STAT_ABORT
|
169 trace_esp_pci_dma_read(saddr
, val
);
173 static void esp_pci_io_write(void *opaque
, hwaddr addr
,
174 uint64_t val
, unsigned int size
)
176 PCIESPState
*pci
= opaque
;
178 if (size
< 4 || addr
& 3) {
179 /* need to upgrade request: we only support 4-bytes accesses */
180 uint32_t current
= 0, mask
;
184 current
= pci
->esp
.wregs
[addr
>> 2];
185 } else if (addr
< 0x60) {
186 current
= pci
->dma_regs
[(addr
- 0x40) >> 2];
187 } else if (addr
< 0x74) {
191 shift
= (4 - size
) * 8;
192 mask
= (~(uint32_t)0 << shift
) >> shift
;
194 shift
= ((4 - (addr
& 3)) & 3) * 8;
196 val
|= current
& ~(mask
<< shift
);
203 esp_reg_write(&pci
->esp
, addr
>> 2, val
);
204 } else if (addr
< 0x60) {
206 esp_pci_dma_write(pci
, (addr
- 0x40) >> 2, val
);
207 } else if (addr
== 0x70) {
208 /* DMA SCSI Bus and control */
209 trace_esp_pci_sbac_write(pci
->sbac
, val
);
212 trace_esp_pci_error_invalid_write((int)addr
);
216 static uint64_t esp_pci_io_read(void *opaque
, hwaddr addr
,
219 PCIESPState
*pci
= opaque
;
224 ret
= esp_reg_read(&pci
->esp
, addr
>> 2);
225 } else if (addr
< 0x60) {
227 ret
= esp_pci_dma_read(pci
, (addr
- 0x40) >> 2);
228 } else if (addr
== 0x70) {
229 /* DMA SCSI Bus and control */
230 trace_esp_pci_sbac_read(pci
->sbac
);
234 trace_esp_pci_error_invalid_read((int)addr
);
238 /* give only requested data */
239 ret
>>= (addr
& 3) * 8;
240 ret
&= ~(~(uint64_t)0 << (8 * size
));
245 static void esp_pci_dma_memory_rw(PCIESPState
*pci
, uint8_t *buf
, int len
,
249 DMADirection expected_dir
;
251 if (pci
->dma_regs
[DMA_CMD
] & DMA_CMD_DIR
) {
252 expected_dir
= DMA_DIRECTION_FROM_DEVICE
;
254 expected_dir
= DMA_DIRECTION_TO_DEVICE
;
257 if (dir
!= expected_dir
) {
258 trace_esp_pci_error_invalid_dma_direction();
262 if (pci
->dma_regs
[DMA_STAT
] & DMA_CMD_MDL
) {
263 qemu_log_mask(LOG_UNIMP
, "am53c974: MDL transfer not implemented\n");
266 addr
= pci
->dma_regs
[DMA_SPA
];
267 if (pci
->dma_regs
[DMA_WBC
] < len
) {
268 len
= pci
->dma_regs
[DMA_WBC
];
271 pci_dma_rw(PCI_DEVICE(pci
), addr
, buf
, len
, dir
);
273 /* update status registers */
274 pci
->dma_regs
[DMA_WBC
] -= len
;
275 pci
->dma_regs
[DMA_WAC
] += len
;
276 if (pci
->dma_regs
[DMA_WBC
] == 0) {
277 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
281 static void esp_pci_dma_memory_read(void *opaque
, uint8_t *buf
, int len
)
283 PCIESPState
*pci
= opaque
;
284 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
287 static void esp_pci_dma_memory_write(void *opaque
, uint8_t *buf
, int len
)
289 PCIESPState
*pci
= opaque
;
290 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_FROM_DEVICE
);
293 static const MemoryRegionOps esp_pci_io_ops
= {
294 .read
= esp_pci_io_read
,
295 .write
= esp_pci_io_write
,
296 .endianness
= DEVICE_LITTLE_ENDIAN
,
298 .min_access_size
= 1,
299 .max_access_size
= 4,
303 static void esp_pci_hard_reset(DeviceState
*dev
)
305 PCIESPState
*pci
= PCI_ESP(dev
);
306 esp_hard_reset(&pci
->esp
);
307 pci
->dma_regs
[DMA_CMD
] &= ~(DMA_CMD_DIR
| DMA_CMD_INTE_D
| DMA_CMD_INTE_P
308 | DMA_CMD_MDL
| DMA_CMD_DIAG
| DMA_CMD_MASK
);
309 pci
->dma_regs
[DMA_WBC
] &= ~0xffff;
310 pci
->dma_regs
[DMA_WAC
] = 0xffffffff;
311 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
312 | DMA_STAT_DONE
| DMA_STAT_ABORT
314 pci
->dma_regs
[DMA_WMAC
] = 0xfffffffd;
317 static const VMStateDescription vmstate_esp_pci_scsi
= {
318 .name
= "pciespscsi",
320 .minimum_version_id
= 1,
321 .fields
= (VMStateField
[]) {
322 VMSTATE_PCI_DEVICE(parent_obj
, PCIESPState
),
323 VMSTATE_BUFFER_UNSAFE(dma_regs
, PCIESPState
, 0, 8 * sizeof(uint32_t)),
324 VMSTATE_STRUCT(esp
, PCIESPState
, 0, vmstate_esp
, ESPState
),
325 VMSTATE_END_OF_LIST()
329 static void esp_pci_command_complete(SCSIRequest
*req
, uint32_t status
,
332 ESPState
*s
= req
->hba_private
;
333 PCIESPState
*pci
= container_of(s
, PCIESPState
, esp
);
335 esp_command_complete(req
, status
, resid
);
336 pci
->dma_regs
[DMA_WBC
] = 0;
337 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
340 static const struct SCSIBusInfo esp_pci_scsi_info
= {
342 .max_target
= ESP_MAX_DEVS
,
345 .transfer_data
= esp_transfer_data
,
346 .complete
= esp_pci_command_complete
,
347 .cancel
= esp_request_cancelled
,
350 static void esp_pci_scsi_realize(PCIDevice
*dev
, Error
**errp
)
352 PCIESPState
*pci
= PCI_ESP(dev
);
353 DeviceState
*d
= DEVICE(dev
);
354 ESPState
*s
= &pci
->esp
;
357 pci_conf
= dev
->config
;
359 /* Interrupt pin A */
360 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
362 s
->dma_memory_read
= esp_pci_dma_memory_read
;
363 s
->dma_memory_write
= esp_pci_dma_memory_write
;
365 s
->chip_id
= TCHI_AM53C974
;
366 memory_region_init_io(&pci
->io
, OBJECT(pci
), &esp_pci_io_ops
, pci
,
369 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->io
);
370 s
->irq
= pci_allocate_irq(dev
);
372 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &esp_pci_scsi_info
, NULL
);
375 static void esp_pci_scsi_uninit(PCIDevice
*d
)
377 PCIESPState
*pci
= PCI_ESP(d
);
379 qemu_free_irq(pci
->esp
.irq
);
382 static void esp_pci_class_init(ObjectClass
*klass
, void *data
)
384 DeviceClass
*dc
= DEVICE_CLASS(klass
);
385 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
387 k
->realize
= esp_pci_scsi_realize
;
388 k
->exit
= esp_pci_scsi_uninit
;
389 k
->vendor_id
= PCI_VENDOR_ID_AMD
;
390 k
->device_id
= PCI_DEVICE_ID_AMD_SCSI
;
392 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
393 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
394 dc
->desc
= "AMD Am53c974 PCscsi-PCI SCSI adapter";
395 dc
->reset
= esp_pci_hard_reset
;
396 dc
->vmsd
= &vmstate_esp_pci_scsi
;
399 static const TypeInfo esp_pci_info
= {
400 .name
= TYPE_AM53C974_DEVICE
,
401 .parent
= TYPE_PCI_DEVICE
,
402 .instance_size
= sizeof(PCIESPState
),
403 .class_init
= esp_pci_class_init
,
404 .interfaces
= (InterfaceInfo
[]) {
405 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
415 #define TYPE_DC390_DEVICE "dc390"
417 OBJECT_CHECK(DC390State, obj, TYPE_DC390_DEVICE)
419 #define EE_ADAPT_SCSI_ID 64
422 #define EE_TAG_CMD_NUM 67
423 #define EE_ADAPT_OPTIONS 68
424 #define EE_BOOT_SCSI_ID 69
425 #define EE_BOOT_SCSI_LUN 70
426 #define EE_CHKSUM1 126
427 #define EE_CHKSUM2 127
429 #define EE_ADAPT_OPTION_F6_F8_AT_BOOT 0x01
430 #define EE_ADAPT_OPTION_BOOT_FROM_CDROM 0x02
431 #define EE_ADAPT_OPTION_INT13 0x04
432 #define EE_ADAPT_OPTION_SCAM_SUPPORT 0x08
435 static uint32_t dc390_read_config(PCIDevice
*dev
, uint32_t addr
, int l
)
437 DC390State
*pci
= DC390(dev
);
440 val
= pci_default_read_config(dev
, addr
, l
);
442 if (addr
== 0x00 && l
== 1) {
443 /* First byte of address space is AND-ed with EEPROM DO line */
444 if (!eeprom93xx_read(pci
->eeprom
)) {
452 static void dc390_write_config(PCIDevice
*dev
,
453 uint32_t addr
, uint32_t val
, int l
)
455 DC390State
*pci
= DC390(dev
);
458 int eesk
= val
& 0x80 ? 1 : 0;
459 int eedi
= val
& 0x40 ? 1 : 0;
460 eeprom93xx_write(pci
->eeprom
, 1, eesk
, eedi
);
461 } else if (addr
== 0xc0) {
463 eeprom93xx_write(pci
->eeprom
, 0, 0, 0);
465 pci_default_write_config(dev
, addr
, val
, l
);
469 static void dc390_scsi_realize(PCIDevice
*dev
, Error
**errp
)
471 DC390State
*pci
= DC390(dev
);
477 /* init base class */
478 esp_pci_scsi_realize(dev
, &err
);
480 error_propagate(errp
, err
);
485 pci
->eeprom
= eeprom93xx_new(DEVICE(dev
), 64);
487 /* set default eeprom values */
488 contents
= (uint8_t *)eeprom93xx_data(pci
->eeprom
);
490 for (i
= 0; i
< 16; i
++) {
491 contents
[i
* 2] = 0x57;
492 contents
[i
* 2 + 1] = 0x00;
494 contents
[EE_ADAPT_SCSI_ID
] = 7;
495 contents
[EE_MODE2
] = 0x0f;
496 contents
[EE_TAG_CMD_NUM
] = 0x04;
497 contents
[EE_ADAPT_OPTIONS
] = EE_ADAPT_OPTION_F6_F8_AT_BOOT
498 | EE_ADAPT_OPTION_BOOT_FROM_CDROM
499 | EE_ADAPT_OPTION_INT13
;
501 /* update eeprom checksum */
502 for (i
= 0; i
< EE_CHKSUM1
; i
+= 2) {
503 chksum
+= contents
[i
] + (((uint16_t)contents
[i
+ 1]) << 8);
505 chksum
= 0x1234 - chksum
;
506 contents
[EE_CHKSUM1
] = chksum
& 0xff;
507 contents
[EE_CHKSUM2
] = chksum
>> 8;
510 static void dc390_class_init(ObjectClass
*klass
, void *data
)
512 DeviceClass
*dc
= DEVICE_CLASS(klass
);
513 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
515 k
->realize
= dc390_scsi_realize
;
516 k
->config_read
= dc390_read_config
;
517 k
->config_write
= dc390_write_config
;
518 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
519 dc
->desc
= "Tekram DC-390 SCSI adapter";
522 static const TypeInfo dc390_info
= {
524 .parent
= TYPE_AM53C974_DEVICE
,
525 .instance_size
= sizeof(DC390State
),
526 .class_init
= dc390_class_init
,
529 static void esp_pci_register_types(void)
531 type_register_static(&esp_pci_info
);
532 type_register_static(&dc390_info
);
535 type_init(esp_pci_register_types
)