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
;
271 if (pci
->dma_regs
[DMA_WBC
] == 0) {
272 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
276 static void esp_pci_dma_memory_read(void *opaque
, uint8_t *buf
, int len
)
278 PCIESPState
*pci
= opaque
;
279 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
282 static void esp_pci_dma_memory_write(void *opaque
, uint8_t *buf
, int len
)
284 PCIESPState
*pci
= opaque
;
285 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_FROM_DEVICE
);
288 static const MemoryRegionOps esp_pci_io_ops
= {
289 .read
= esp_pci_io_read
,
290 .write
= esp_pci_io_write
,
291 .endianness
= DEVICE_LITTLE_ENDIAN
,
293 .min_access_size
= 1,
294 .max_access_size
= 4,
298 static void esp_pci_hard_reset(DeviceState
*dev
)
300 PCIESPState
*pci
= PCI_ESP(dev
);
301 esp_hard_reset(&pci
->esp
);
302 pci
->dma_regs
[DMA_CMD
] &= ~(DMA_CMD_DIR
| DMA_CMD_INTE_D
| DMA_CMD_INTE_P
303 | DMA_CMD_MDL
| DMA_CMD_DIAG
| DMA_CMD_MASK
);
304 pci
->dma_regs
[DMA_WBC
] &= ~0xffff;
305 pci
->dma_regs
[DMA_WAC
] = 0xffffffff;
306 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
307 | DMA_STAT_DONE
| DMA_STAT_ABORT
309 pci
->dma_regs
[DMA_WMAC
] = 0xfffffffd;
312 static const VMStateDescription vmstate_esp_pci_scsi
= {
313 .name
= "pciespscsi",
315 .minimum_version_id
= 0,
316 .fields
= (VMStateField
[]) {
317 VMSTATE_PCI_DEVICE(parent_obj
, PCIESPState
),
318 VMSTATE_BUFFER_UNSAFE(dma_regs
, PCIESPState
, 0, 8 * sizeof(uint32_t)),
319 VMSTATE_STRUCT(esp
, PCIESPState
, 0, vmstate_esp
, ESPState
),
320 VMSTATE_END_OF_LIST()
324 static void esp_pci_command_complete(SCSIRequest
*req
, uint32_t status
,
327 ESPState
*s
= req
->hba_private
;
328 PCIESPState
*pci
= container_of(s
, PCIESPState
, esp
);
330 esp_command_complete(req
, status
, resid
);
331 pci
->dma_regs
[DMA_WBC
] = 0;
332 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
335 static const struct SCSIBusInfo esp_pci_scsi_info
= {
337 .max_target
= ESP_MAX_DEVS
,
340 .transfer_data
= esp_transfer_data
,
341 .complete
= esp_pci_command_complete
,
342 .cancel
= esp_request_cancelled
,
345 static void esp_pci_scsi_realize(PCIDevice
*dev
, Error
**errp
)
347 PCIESPState
*pci
= PCI_ESP(dev
);
348 DeviceState
*d
= DEVICE(dev
);
349 ESPState
*s
= &pci
->esp
;
352 pci_conf
= dev
->config
;
354 /* Interrupt pin A */
355 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
357 s
->dma_memory_read
= esp_pci_dma_memory_read
;
358 s
->dma_memory_write
= esp_pci_dma_memory_write
;
360 s
->chip_id
= TCHI_AM53C974
;
361 memory_region_init_io(&pci
->io
, OBJECT(pci
), &esp_pci_io_ops
, pci
,
364 pci_register_bar(dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->io
);
365 s
->irq
= pci_allocate_irq(dev
);
367 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), d
, &esp_pci_scsi_info
, NULL
);
368 if (!d
->hotplugged
) {
369 scsi_bus_legacy_handle_cmdline(&s
->bus
, errp
);
373 static void esp_pci_scsi_uninit(PCIDevice
*d
)
375 PCIESPState
*pci
= PCI_ESP(d
);
377 qemu_free_irq(pci
->esp
.irq
);
380 static void esp_pci_class_init(ObjectClass
*klass
, void *data
)
382 DeviceClass
*dc
= DEVICE_CLASS(klass
);
383 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
385 k
->realize
= esp_pci_scsi_realize
;
386 k
->exit
= esp_pci_scsi_uninit
;
387 k
->vendor_id
= PCI_VENDOR_ID_AMD
;
388 k
->device_id
= PCI_DEVICE_ID_AMD_SCSI
;
390 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
391 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
392 dc
->desc
= "AMD Am53c974 PCscsi-PCI SCSI adapter";
393 dc
->reset
= esp_pci_hard_reset
;
394 dc
->vmsd
= &vmstate_esp_pci_scsi
;
397 static const TypeInfo esp_pci_info
= {
398 .name
= TYPE_AM53C974_DEVICE
,
399 .parent
= TYPE_PCI_DEVICE
,
400 .instance_size
= sizeof(PCIESPState
),
401 .class_init
= esp_pci_class_init
,
409 #define TYPE_DC390_DEVICE "dc390"
411 OBJECT_CHECK(DC390State, obj, TYPE_DC390_DEVICE)
413 #define EE_ADAPT_SCSI_ID 64
416 #define EE_TAG_CMD_NUM 67
417 #define EE_ADAPT_OPTIONS 68
418 #define EE_BOOT_SCSI_ID 69
419 #define EE_BOOT_SCSI_LUN 70
420 #define EE_CHKSUM1 126
421 #define EE_CHKSUM2 127
423 #define EE_ADAPT_OPTION_F6_F8_AT_BOOT 0x01
424 #define EE_ADAPT_OPTION_BOOT_FROM_CDROM 0x02
425 #define EE_ADAPT_OPTION_INT13 0x04
426 #define EE_ADAPT_OPTION_SCAM_SUPPORT 0x08
429 static uint32_t dc390_read_config(PCIDevice
*dev
, uint32_t addr
, int l
)
431 DC390State
*pci
= DC390(dev
);
434 val
= pci_default_read_config(dev
, addr
, l
);
436 if (addr
== 0x00 && l
== 1) {
437 /* First byte of address space is AND-ed with EEPROM DO line */
438 if (!eeprom93xx_read(pci
->eeprom
)) {
446 static void dc390_write_config(PCIDevice
*dev
,
447 uint32_t addr
, uint32_t val
, int l
)
449 DC390State
*pci
= DC390(dev
);
452 int eesk
= val
& 0x80 ? 1 : 0;
453 int eedi
= val
& 0x40 ? 1 : 0;
454 eeprom93xx_write(pci
->eeprom
, 1, eesk
, eedi
);
455 } else if (addr
== 0xc0) {
457 eeprom93xx_write(pci
->eeprom
, 0, 0, 0);
459 pci_default_write_config(dev
, addr
, val
, l
);
463 static void dc390_scsi_realize(PCIDevice
*dev
, Error
**errp
)
465 DC390State
*pci
= DC390(dev
);
471 /* init base class */
472 esp_pci_scsi_realize(dev
, &err
);
474 error_propagate(errp
, err
);
479 pci
->eeprom
= eeprom93xx_new(DEVICE(dev
), 64);
481 /* set default eeprom values */
482 contents
= (uint8_t *)eeprom93xx_data(pci
->eeprom
);
484 for (i
= 0; i
< 16; i
++) {
485 contents
[i
* 2] = 0x57;
486 contents
[i
* 2 + 1] = 0x00;
488 contents
[EE_ADAPT_SCSI_ID
] = 7;
489 contents
[EE_MODE2
] = 0x0f;
490 contents
[EE_TAG_CMD_NUM
] = 0x04;
491 contents
[EE_ADAPT_OPTIONS
] = EE_ADAPT_OPTION_F6_F8_AT_BOOT
492 | EE_ADAPT_OPTION_BOOT_FROM_CDROM
493 | EE_ADAPT_OPTION_INT13
;
495 /* update eeprom checksum */
496 for (i
= 0; i
< EE_CHKSUM1
; i
+= 2) {
497 chksum
+= contents
[i
] + (((uint16_t)contents
[i
+ 1]) << 8);
499 chksum
= 0x1234 - chksum
;
500 contents
[EE_CHKSUM1
] = chksum
& 0xff;
501 contents
[EE_CHKSUM2
] = chksum
>> 8;
504 static void dc390_class_init(ObjectClass
*klass
, void *data
)
506 DeviceClass
*dc
= DEVICE_CLASS(klass
);
507 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
509 k
->realize
= dc390_scsi_realize
;
510 k
->config_read
= dc390_read_config
;
511 k
->config_write
= dc390_write_config
;
512 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
513 dc
->desc
= "Tekram DC-390 SCSI adapter";
516 static const TypeInfo dc390_info
= {
518 .parent
= TYPE_AM53C974_DEVICE
,
519 .instance_size
= sizeof(DC390State
),
520 .class_init
= dc390_class_init
,
523 static void esp_pci_register_types(void)
525 type_register_static(&esp_pci_info
);
526 type_register_static(&dc390_info
);
529 type_init(esp_pci_register_types
)