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"
28 #include "hw/nvram/eeprom93xx.h"
29 #include "hw/scsi/esp.h"
31 #include "qapi/error.h"
34 #define TYPE_AM53C974_DEVICE "am53c974"
36 #define PCI_ESP(obj) \
37 OBJECT_CHECK(PCIESPState, (obj), TYPE_AM53C974_DEVICE)
48 #define DMA_CMD_MASK 0x03
49 #define DMA_CMD_DIAG 0x04
50 #define DMA_CMD_MDL 0x10
51 #define DMA_CMD_INTE_P 0x20
52 #define DMA_CMD_INTE_D 0x40
53 #define DMA_CMD_DIR 0x80
55 #define DMA_STAT_PWDN 0x01
56 #define DMA_STAT_ERROR 0x02
57 #define DMA_STAT_ABORT 0x04
58 #define DMA_STAT_DONE 0x08
59 #define DMA_STAT_SCSIINT 0x10
60 #define DMA_STAT_BCMBLT 0x20
62 #define SBAC_STATUS 0x1000
64 typedef struct PCIESPState
{
75 static void esp_pci_handle_idle(PCIESPState
*pci
, uint32_t val
)
77 trace_esp_pci_dma_idle(val
);
78 esp_dma_enable(&pci
->esp
, 0, 0);
81 static void esp_pci_handle_blast(PCIESPState
*pci
, uint32_t val
)
83 trace_esp_pci_dma_blast(val
);
84 qemu_log_mask(LOG_UNIMP
, "am53c974: cmd BLAST not implemented\n");
87 static void esp_pci_handle_abort(PCIESPState
*pci
, uint32_t val
)
89 trace_esp_pci_dma_abort(val
);
90 if (pci
->esp
.current_req
) {
91 scsi_req_cancel(pci
->esp
.current_req
);
95 static void esp_pci_handle_start(PCIESPState
*pci
, uint32_t val
)
97 trace_esp_pci_dma_start(val
);
99 pci
->dma_regs
[DMA_WBC
] = pci
->dma_regs
[DMA_STC
];
100 pci
->dma_regs
[DMA_WAC
] = pci
->dma_regs
[DMA_SPA
];
101 pci
->dma_regs
[DMA_WMAC
] = pci
->dma_regs
[DMA_SMDLA
];
103 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
104 | DMA_STAT_DONE
| DMA_STAT_ABORT
105 | DMA_STAT_ERROR
| DMA_STAT_PWDN
);
107 esp_dma_enable(&pci
->esp
, 0, 1);
110 static void esp_pci_dma_write(PCIESPState
*pci
, uint32_t saddr
, uint32_t val
)
112 trace_esp_pci_dma_write(saddr
, pci
->dma_regs
[saddr
], val
);
115 pci
->dma_regs
[saddr
] = val
;
116 switch (val
& DMA_CMD_MASK
) {
118 esp_pci_handle_idle(pci
, val
);
120 case 0x1: /* BLAST */
121 esp_pci_handle_blast(pci
, val
);
123 case 0x2: /* ABORT */
124 esp_pci_handle_abort(pci
, val
);
126 case 0x3: /* START */
127 esp_pci_handle_start(pci
, val
);
129 default: /* can't happen */
136 pci
->dma_regs
[saddr
] = val
;
139 if (!(pci
->sbac
& SBAC_STATUS
)) {
140 /* clear some bits on write */
141 uint32_t mask
= DMA_STAT_ERROR
| DMA_STAT_ABORT
| DMA_STAT_DONE
;
142 pci
->dma_regs
[DMA_STAT
] &= ~(val
& mask
);
146 trace_esp_pci_error_invalid_write_dma(val
, saddr
);
151 static uint32_t esp_pci_dma_read(PCIESPState
*pci
, uint32_t saddr
)
155 val
= pci
->dma_regs
[saddr
];
156 if (saddr
== DMA_STAT
) {
157 if (pci
->esp
.rregs
[ESP_RSTAT
] & STAT_INT
) {
158 val
|= DMA_STAT_SCSIINT
;
160 if (pci
->sbac
& SBAC_STATUS
) {
161 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_ERROR
| DMA_STAT_ABORT
|
166 trace_esp_pci_dma_read(saddr
, val
);
170 static void esp_pci_io_write(void *opaque
, hwaddr addr
,
171 uint64_t val
, unsigned int size
)
173 PCIESPState
*pci
= opaque
;
175 if (size
< 4 || addr
& 3) {
176 /* need to upgrade request: we only support 4-bytes accesses */
177 uint32_t current
= 0, mask
;
181 current
= pci
->esp
.wregs
[addr
>> 2];
182 } else if (addr
< 0x60) {
183 current
= pci
->dma_regs
[(addr
- 0x40) >> 2];
184 } else if (addr
< 0x74) {
188 shift
= (4 - size
) * 8;
189 mask
= (~(uint32_t)0 << shift
) >> shift
;
191 shift
= ((4 - (addr
& 3)) & 3) * 8;
193 val
|= current
& ~(mask
<< shift
);
200 esp_reg_write(&pci
->esp
, addr
>> 2, val
);
201 } else if (addr
< 0x60) {
203 esp_pci_dma_write(pci
, (addr
- 0x40) >> 2, val
);
204 } else if (addr
== 0x70) {
205 /* DMA SCSI Bus and control */
206 trace_esp_pci_sbac_write(pci
->sbac
, val
);
209 trace_esp_pci_error_invalid_write((int)addr
);
213 static uint64_t esp_pci_io_read(void *opaque
, hwaddr addr
,
216 PCIESPState
*pci
= opaque
;
221 ret
= esp_reg_read(&pci
->esp
, addr
>> 2);
222 } else if (addr
< 0x60) {
224 ret
= esp_pci_dma_read(pci
, (addr
- 0x40) >> 2);
225 } else if (addr
== 0x70) {
226 /* DMA SCSI Bus and control */
227 trace_esp_pci_sbac_read(pci
->sbac
);
231 trace_esp_pci_error_invalid_read((int)addr
);
235 /* give only requested data */
236 ret
>>= (addr
& 3) * 8;
237 ret
&= ~(~(uint64_t)0 << (8 * size
));
242 static void esp_pci_dma_memory_rw(PCIESPState
*pci
, uint8_t *buf
, int len
,
246 DMADirection expected_dir
;
248 if (pci
->dma_regs
[DMA_CMD
] & DMA_CMD_DIR
) {
249 expected_dir
= DMA_DIRECTION_FROM_DEVICE
;
251 expected_dir
= DMA_DIRECTION_TO_DEVICE
;
254 if (dir
!= expected_dir
) {
255 trace_esp_pci_error_invalid_dma_direction();
259 if (pci
->dma_regs
[DMA_STAT
] & DMA_CMD_MDL
) {
260 qemu_log_mask(LOG_UNIMP
, "am53c974: MDL transfer not implemented\n");
263 addr
= pci
->dma_regs
[DMA_SPA
];
264 if (pci
->dma_regs
[DMA_WBC
] < len
) {
265 len
= pci
->dma_regs
[DMA_WBC
];
268 pci_dma_rw(PCI_DEVICE(pci
), addr
, buf
, len
, dir
);
270 /* update status registers */
271 pci
->dma_regs
[DMA_WBC
] -= len
;
272 pci
->dma_regs
[DMA_WAC
] += len
;
273 if (pci
->dma_regs
[DMA_WBC
] == 0) {
274 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
278 static void esp_pci_dma_memory_read(void *opaque
, uint8_t *buf
, int len
)
280 PCIESPState
*pci
= opaque
;
281 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
284 static void esp_pci_dma_memory_write(void *opaque
, uint8_t *buf
, int len
)
286 PCIESPState
*pci
= opaque
;
287 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_FROM_DEVICE
);
290 static const MemoryRegionOps esp_pci_io_ops
= {
291 .read
= esp_pci_io_read
,
292 .write
= esp_pci_io_write
,
293 .endianness
= DEVICE_LITTLE_ENDIAN
,
295 .min_access_size
= 1,
296 .max_access_size
= 4,
300 static void esp_pci_hard_reset(DeviceState
*dev
)
302 PCIESPState
*pci
= PCI_ESP(dev
);
303 esp_hard_reset(&pci
->esp
);
304 pci
->dma_regs
[DMA_CMD
] &= ~(DMA_CMD_DIR
| DMA_CMD_INTE_D
| DMA_CMD_INTE_P
305 | DMA_CMD_MDL
| DMA_CMD_DIAG
| DMA_CMD_MASK
);
306 pci
->dma_regs
[DMA_WBC
] &= ~0xffff;
307 pci
->dma_regs
[DMA_WAC
] = 0xffffffff;
308 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
309 | DMA_STAT_DONE
| DMA_STAT_ABORT
311 pci
->dma_regs
[DMA_WMAC
] = 0xfffffffd;
314 static const VMStateDescription vmstate_esp_pci_scsi
= {
315 .name
= "pciespscsi",
317 .minimum_version_id
= 0,
318 .fields
= (VMStateField
[]) {
319 VMSTATE_PCI_DEVICE(parent_obj
, PCIESPState
),
320 VMSTATE_BUFFER_UNSAFE(dma_regs
, PCIESPState
, 0, 8 * sizeof(uint32_t)),
321 VMSTATE_STRUCT(esp
, PCIESPState
, 0, vmstate_esp
, ESPState
),
322 VMSTATE_END_OF_LIST()
326 static void esp_pci_command_complete(SCSIRequest
*req
, uint32_t status
,
329 ESPState
*s
= req
->hba_private
;
330 PCIESPState
*pci
= container_of(s
, PCIESPState
, esp
);
332 esp_command_complete(req
, status
, resid
);
333 pci
->dma_regs
[DMA_WBC
] = 0;
334 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
337 static const struct SCSIBusInfo esp_pci_scsi_info
= {
339 .max_target
= ESP_MAX_DEVS
,
342 .transfer_data
= esp_transfer_data
,
343 .complete
= esp_pci_command_complete
,
344 .cancel
= esp_request_cancelled
,
347 static void esp_pci_scsi_realize(PCIDevice
*dev
, Error
**errp
)
349 PCIESPState
*pci
= PCI_ESP(dev
);
350 DeviceState
*d
= DEVICE(dev
);
351 ESPState
*s
= &pci
->esp
;
354 pci_conf
= dev
->config
;
356 /* Interrupt pin A */
357 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
359 s
->dma_memory_read
= esp_pci_dma_memory_read
;
360 s
->dma_memory_write
= esp_pci_dma_memory_write
;
362 s
->chip_id
= TCHI_AM53C974
;
363 memory_region_init_io(&pci
->io
, OBJECT(pci
), &esp_pci_io_ops
, pci
,
366 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->io
);
367 s
->irq
= pci_allocate_irq(dev
);
369 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &esp_pci_scsi_info
, NULL
);
370 if (!d
->hotplugged
) {
371 scsi_bus_legacy_handle_cmdline(&s
->bus
, errp
);
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
,
411 #define TYPE_DC390_DEVICE "dc390"
413 OBJECT_CHECK(DC390State, obj, TYPE_DC390_DEVICE)
415 #define EE_ADAPT_SCSI_ID 64
418 #define EE_TAG_CMD_NUM 67
419 #define EE_ADAPT_OPTIONS 68
420 #define EE_BOOT_SCSI_ID 69
421 #define EE_BOOT_SCSI_LUN 70
422 #define EE_CHKSUM1 126
423 #define EE_CHKSUM2 127
425 #define EE_ADAPT_OPTION_F6_F8_AT_BOOT 0x01
426 #define EE_ADAPT_OPTION_BOOT_FROM_CDROM 0x02
427 #define EE_ADAPT_OPTION_INT13 0x04
428 #define EE_ADAPT_OPTION_SCAM_SUPPORT 0x08
431 static uint32_t dc390_read_config(PCIDevice
*dev
, uint32_t addr
, int l
)
433 DC390State
*pci
= DC390(dev
);
436 val
= pci_default_read_config(dev
, addr
, l
);
438 if (addr
== 0x00 && l
== 1) {
439 /* First byte of address space is AND-ed with EEPROM DO line */
440 if (!eeprom93xx_read(pci
->eeprom
)) {
448 static void dc390_write_config(PCIDevice
*dev
,
449 uint32_t addr
, uint32_t val
, int l
)
451 DC390State
*pci
= DC390(dev
);
454 int eesk
= val
& 0x80 ? 1 : 0;
455 int eedi
= val
& 0x40 ? 1 : 0;
456 eeprom93xx_write(pci
->eeprom
, 1, eesk
, eedi
);
457 } else if (addr
== 0xc0) {
459 eeprom93xx_write(pci
->eeprom
, 0, 0, 0);
461 pci_default_write_config(dev
, addr
, val
, l
);
465 static void dc390_scsi_realize(PCIDevice
*dev
, Error
**errp
)
467 DC390State
*pci
= DC390(dev
);
473 /* init base class */
474 esp_pci_scsi_realize(dev
, &err
);
476 error_propagate(errp
, err
);
481 pci
->eeprom
= eeprom93xx_new(DEVICE(dev
), 64);
483 /* set default eeprom values */
484 contents
= (uint8_t *)eeprom93xx_data(pci
->eeprom
);
486 for (i
= 0; i
< 16; i
++) {
487 contents
[i
* 2] = 0x57;
488 contents
[i
* 2 + 1] = 0x00;
490 contents
[EE_ADAPT_SCSI_ID
] = 7;
491 contents
[EE_MODE2
] = 0x0f;
492 contents
[EE_TAG_CMD_NUM
] = 0x04;
493 contents
[EE_ADAPT_OPTIONS
] = EE_ADAPT_OPTION_F6_F8_AT_BOOT
494 | EE_ADAPT_OPTION_BOOT_FROM_CDROM
495 | EE_ADAPT_OPTION_INT13
;
497 /* update eeprom checksum */
498 for (i
= 0; i
< EE_CHKSUM1
; i
+= 2) {
499 chksum
+= contents
[i
] + (((uint16_t)contents
[i
+ 1]) << 8);
501 chksum
= 0x1234 - chksum
;
502 contents
[EE_CHKSUM1
] = chksum
& 0xff;
503 contents
[EE_CHKSUM2
] = chksum
>> 8;
506 static void dc390_class_init(ObjectClass
*klass
, void *data
)
508 DeviceClass
*dc
= DEVICE_CLASS(klass
);
509 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
511 k
->realize
= dc390_scsi_realize
;
512 k
->config_read
= dc390_read_config
;
513 k
->config_write
= dc390_write_config
;
514 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
515 dc
->desc
= "Tekram DC-390 SCSI adapter";
518 static const TypeInfo dc390_info
= {
520 .parent
= TYPE_AM53C974_DEVICE
,
521 .instance_size
= sizeof(DC390State
),
522 .class_init
= dc390_class_init
,
525 static void esp_pci_register_types(void)
527 type_register_static(&esp_pci_info
);
528 type_register_static(&dc390_info
);
531 type_init(esp_pci_register_types
)