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
34 * On Sparc32, this is the ESP (NCR53C90) part of chip STP2000 (Master I/O),
35 * also produced as NCR89C100. See
36 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
38 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR53C9X.txt
44 typedef struct ESPState ESPState
;
47 uint8_t rregs
[ESP_REGS
];
48 uint8_t wregs
[ESP_REGS
];
52 uint32_t ti_rptr
, ti_wptr
;
55 uint8_t ti_buf
[TI_BUFSZ
];
57 SCSIDevice
*current_dev
;
58 SCSIRequest
*current_req
;
59 uint8_t cmdbuf
[TI_BUFSZ
];
63 /* The amount of data left in the current DMA transfer. */
65 /* The size of the current DMA transfer. Zero if no transfer is in
73 ESPDMAMemoryReadWriteFunc dma_memory_read
;
74 ESPDMAMemoryReadWriteFunc dma_memory_write
;
76 void (*dma_cb
)(ESPState
*s
);
84 #define ESP_WBUSID 0x4
88 #define ESP_WSYNTP 0x6
89 #define ESP_RFLAGS 0x7
106 #define CMD_FLUSH 0x01
107 #define CMD_RESET 0x02
108 #define CMD_BUSRESET 0x03
110 #define CMD_ICCS 0x11
111 #define CMD_MSGACC 0x12
113 #define CMD_SATN 0x1a
114 #define CMD_RSTATN 0x1b
116 #define CMD_SELATN 0x42
117 #define CMD_SELATNS 0x43
118 #define CMD_ENSEL 0x44
119 #define CMD_DISSEL 0x45
127 #define STAT_PIO_MASK 0x06
132 #define STAT_INT 0x80
134 #define BUSID_DID 0x07
139 #define INTR_RST 0x80
144 #define CFG1_RESREPT 0x40
146 #define TCHI_FAS100A 0x4
147 #define TCHI_AM53C974 0x12
149 static void esp_raise_irq(ESPState
*s
)
151 if (!(s
->rregs
[ESP_RSTAT
] & STAT_INT
)) {
152 s
->rregs
[ESP_RSTAT
] |= STAT_INT
;
153 qemu_irq_raise(s
->irq
);
154 trace_esp_raise_irq();
158 static void esp_lower_irq(ESPState
*s
)
160 if (s
->rregs
[ESP_RSTAT
] & STAT_INT
) {
161 s
->rregs
[ESP_RSTAT
] &= ~STAT_INT
;
162 qemu_irq_lower(s
->irq
);
163 trace_esp_lower_irq();
167 static void esp_dma_enable(ESPState
*s
, int irq
, int level
)
171 trace_esp_dma_enable();
177 trace_esp_dma_disable();
182 static void esp_request_cancelled(SCSIRequest
*req
)
184 ESPState
*s
= req
->hba_private
;
186 if (req
== s
->current_req
) {
187 scsi_req_unref(s
->current_req
);
188 s
->current_req
= NULL
;
189 s
->current_dev
= NULL
;
193 static uint32_t get_cmd(ESPState
*s
, uint8_t *buf
)
198 target
= s
->wregs
[ESP_WBUSID
] & BUSID_DID
;
200 dmalen
= s
->rregs
[ESP_TCLO
] | (s
->rregs
[ESP_TCMID
] << 8);
201 s
->dma_memory_read(s
->dma_opaque
, buf
, dmalen
);
204 memcpy(buf
, s
->ti_buf
, dmalen
);
205 buf
[0] = buf
[2] >> 5;
207 trace_esp_get_cmd(dmalen
, target
);
213 if (s
->current_req
) {
214 /* Started a new command before the old one finished. Cancel it. */
215 scsi_req_cancel(s
->current_req
);
219 s
->current_dev
= scsi_device_find(&s
->bus
, 0, target
, 0);
220 if (!s
->current_dev
) {
222 s
->rregs
[ESP_RSTAT
] = 0;
223 s
->rregs
[ESP_RINTR
] = INTR_DC
;
224 s
->rregs
[ESP_RSEQ
] = SEQ_0
;
231 static void do_busid_cmd(ESPState
*s
, uint8_t *buf
, uint8_t busid
)
235 SCSIDevice
*current_lun
;
237 trace_esp_do_busid_cmd(busid
);
239 current_lun
= scsi_device_find(&s
->bus
, 0, s
->current_dev
->id
, lun
);
240 s
->current_req
= scsi_req_new(current_lun
, 0, lun
, buf
, s
);
241 datalen
= scsi_req_enqueue(s
->current_req
);
242 s
->ti_size
= datalen
;
244 s
->rregs
[ESP_RSTAT
] = STAT_TC
;
248 s
->rregs
[ESP_RSTAT
] |= STAT_DI
;
250 s
->rregs
[ESP_RSTAT
] |= STAT_DO
;
252 scsi_req_continue(s
->current_req
);
254 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
255 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
259 static void do_cmd(ESPState
*s
, uint8_t *buf
)
261 uint8_t busid
= buf
[0];
263 do_busid_cmd(s
, &buf
[1], busid
);
266 static void handle_satn(ESPState
*s
)
271 if (s
->dma
&& !s
->dma_enabled
) {
272 s
->dma_cb
= handle_satn
;
275 len
= get_cmd(s
, buf
);
280 static void handle_s_without_atn(ESPState
*s
)
285 if (s
->dma
&& !s
->dma_enabled
) {
286 s
->dma_cb
= handle_s_without_atn
;
289 len
= get_cmd(s
, buf
);
291 do_busid_cmd(s
, buf
, 0);
295 static void handle_satn_stop(ESPState
*s
)
297 if (s
->dma
&& !s
->dma_enabled
) {
298 s
->dma_cb
= handle_satn_stop
;
301 s
->cmdlen
= get_cmd(s
, s
->cmdbuf
);
303 trace_esp_handle_satn_stop(s
->cmdlen
);
305 s
->rregs
[ESP_RSTAT
] = STAT_TC
| STAT_CD
;
306 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
307 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
312 static void write_response(ESPState
*s
)
314 trace_esp_write_response(s
->status
);
315 s
->ti_buf
[0] = s
->status
;
318 s
->dma_memory_write(s
->dma_opaque
, s
->ti_buf
, 2);
319 s
->rregs
[ESP_RSTAT
] = STAT_TC
| STAT_ST
;
320 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
321 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
326 s
->rregs
[ESP_RFLAGS
] = 2;
331 static void esp_dma_done(ESPState
*s
)
333 s
->rregs
[ESP_RSTAT
] |= STAT_TC
;
334 s
->rregs
[ESP_RINTR
] = INTR_BS
;
335 s
->rregs
[ESP_RSEQ
] = 0;
336 s
->rregs
[ESP_RFLAGS
] = 0;
337 s
->rregs
[ESP_TCLO
] = 0;
338 s
->rregs
[ESP_TCMID
] = 0;
342 static void esp_do_dma(ESPState
*s
)
347 to_device
= (s
->ti_size
< 0);
350 trace_esp_do_dma(s
->cmdlen
, len
);
351 s
->dma_memory_read(s
->dma_opaque
, &s
->cmdbuf
[s
->cmdlen
], len
);
355 do_cmd(s
, s
->cmdbuf
);
358 if (s
->async_len
== 0) {
359 /* Defer until data is available. */
362 if (len
> s
->async_len
) {
366 s
->dma_memory_read(s
->dma_opaque
, s
->async_buf
, len
);
368 s
->dma_memory_write(s
->dma_opaque
, s
->async_buf
, len
);
377 if (s
->async_len
== 0) {
378 scsi_req_continue(s
->current_req
);
379 /* If there is still data to be read from the device then
380 complete the DMA operation immediately. Otherwise defer
381 until the scsi layer has completed. */
382 if (to_device
|| s
->dma_left
!= 0 || s
->ti_size
== 0) {
387 /* Partially filled a scsi buffer. Complete immediately. */
391 static void esp_command_complete(SCSIRequest
*req
, uint32_t status
,
394 ESPState
*s
= req
->hba_private
;
396 trace_esp_command_complete();
397 if (s
->ti_size
!= 0) {
398 trace_esp_command_complete_unexpected();
404 trace_esp_command_complete_fail();
407 s
->rregs
[ESP_RSTAT
] = STAT_ST
;
409 if (s
->current_req
) {
410 scsi_req_unref(s
->current_req
);
411 s
->current_req
= NULL
;
412 s
->current_dev
= NULL
;
416 static void esp_transfer_data(SCSIRequest
*req
, uint32_t len
)
418 ESPState
*s
= req
->hba_private
;
420 trace_esp_transfer_data(s
->dma_left
, s
->ti_size
);
422 s
->async_buf
= scsi_req_get_buf(req
);
425 } else if (s
->dma_counter
!= 0 && s
->ti_size
<= 0) {
426 /* If this was the last part of a DMA transfer then the
427 completion interrupt is deferred to here. */
432 static void handle_ti(ESPState
*s
)
434 uint32_t dmalen
, minlen
;
436 if (s
->dma
&& !s
->dma_enabled
) {
437 s
->dma_cb
= handle_ti
;
441 dmalen
= s
->rregs
[ESP_TCLO
] | (s
->rregs
[ESP_TCMID
] << 8);
445 s
->dma_counter
= dmalen
;
448 minlen
= (dmalen
< 32) ? dmalen
: 32;
449 else if (s
->ti_size
< 0)
450 minlen
= (dmalen
< -s
->ti_size
) ? dmalen
: -s
->ti_size
;
452 minlen
= (dmalen
< s
->ti_size
) ? dmalen
: s
->ti_size
;
453 trace_esp_handle_ti(minlen
);
455 s
->dma_left
= minlen
;
456 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
458 } else if (s
->do_cmd
) {
459 trace_esp_handle_ti_cmd(s
->cmdlen
);
463 do_cmd(s
, s
->cmdbuf
);
468 static void esp_hard_reset(ESPState
*s
)
470 memset(s
->rregs
, 0, ESP_REGS
);
471 memset(s
->wregs
, 0, ESP_REGS
);
472 s
->rregs
[ESP_TCHI
] = s
->chip_id
;
480 s
->rregs
[ESP_CFG1
] = 7;
483 static void esp_soft_reset(ESPState
*s
)
485 qemu_irq_lower(s
->irq
);
489 static void parent_esp_reset(ESPState
*s
, int irq
, int level
)
496 static uint64_t esp_reg_read(ESPState
*s
, uint32_t saddr
)
500 trace_esp_mem_readb(saddr
, s
->rregs
[saddr
]);
503 if (s
->ti_size
> 0) {
505 if ((s
->rregs
[ESP_RSTAT
] & STAT_PIO_MASK
) == 0) {
507 qemu_log_mask(LOG_UNIMP
,
508 "esp: PIO data read not implemented\n");
509 s
->rregs
[ESP_FIFO
] = 0;
511 s
->rregs
[ESP_FIFO
] = s
->ti_buf
[s
->ti_rptr
++];
515 if (s
->ti_size
== 0) {
521 /* Clear sequence step, interrupt register and all status bits
523 old_val
= s
->rregs
[ESP_RINTR
];
524 s
->rregs
[ESP_RINTR
] = 0;
525 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
526 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
533 return s
->rregs
[saddr
];
536 static void esp_reg_write(ESPState
*s
, uint32_t saddr
, uint64_t val
)
538 trace_esp_mem_writeb(saddr
, s
->wregs
[saddr
], val
);
542 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
546 s
->cmdbuf
[s
->cmdlen
++] = val
& 0xff;
547 } else if (s
->ti_size
== TI_BUFSZ
- 1) {
548 trace_esp_error_fifo_overrun();
551 s
->ti_buf
[s
->ti_wptr
++] = val
& 0xff;
555 s
->rregs
[saddr
] = val
;
558 /* Reload DMA counter. */
559 s
->rregs
[ESP_TCLO
] = s
->wregs
[ESP_TCLO
];
560 s
->rregs
[ESP_TCMID
] = s
->wregs
[ESP_TCMID
];
564 switch(val
& CMD_CMD
) {
566 trace_esp_mem_writeb_cmd_nop(val
);
569 trace_esp_mem_writeb_cmd_flush(val
);
571 s
->rregs
[ESP_RINTR
] = INTR_FC
;
572 s
->rregs
[ESP_RSEQ
] = 0;
573 s
->rregs
[ESP_RFLAGS
] = 0;
576 trace_esp_mem_writeb_cmd_reset(val
);
580 trace_esp_mem_writeb_cmd_bus_reset(val
);
581 s
->rregs
[ESP_RINTR
] = INTR_RST
;
582 if (!(s
->wregs
[ESP_CFG1
] & CFG1_RESREPT
)) {
590 trace_esp_mem_writeb_cmd_iccs(val
);
592 s
->rregs
[ESP_RINTR
] = INTR_FC
;
593 s
->rregs
[ESP_RSTAT
] |= STAT_MI
;
596 trace_esp_mem_writeb_cmd_msgacc(val
);
597 s
->rregs
[ESP_RINTR
] = INTR_DC
;
598 s
->rregs
[ESP_RSEQ
] = 0;
599 s
->rregs
[ESP_RFLAGS
] = 0;
603 trace_esp_mem_writeb_cmd_pad(val
);
604 s
->rregs
[ESP_RSTAT
] = STAT_TC
;
605 s
->rregs
[ESP_RINTR
] = INTR_FC
;
606 s
->rregs
[ESP_RSEQ
] = 0;
609 trace_esp_mem_writeb_cmd_satn(val
);
612 trace_esp_mem_writeb_cmd_rstatn(val
);
615 trace_esp_mem_writeb_cmd_sel(val
);
616 handle_s_without_atn(s
);
619 trace_esp_mem_writeb_cmd_selatn(val
);
623 trace_esp_mem_writeb_cmd_selatns(val
);
627 trace_esp_mem_writeb_cmd_ensel(val
);
628 s
->rregs
[ESP_RINTR
] = 0;
631 trace_esp_mem_writeb_cmd_dissel(val
);
632 s
->rregs
[ESP_RINTR
] = 0;
636 trace_esp_error_unhandled_command(val
);
640 case ESP_WBUSID
... ESP_WSYNO
:
643 s
->rregs
[saddr
] = val
;
645 case ESP_WCCF
... ESP_WTEST
:
647 case ESP_CFG2
... ESP_RES4
:
648 s
->rregs
[saddr
] = val
;
651 trace_esp_error_invalid_write(val
, saddr
);
654 s
->wregs
[saddr
] = val
;
657 static bool esp_mem_accepts(void *opaque
, target_phys_addr_t addr
,
658 unsigned size
, bool is_write
)
660 return (size
== 1) || (is_write
&& size
== 4);
663 static const VMStateDescription vmstate_esp
= {
666 .minimum_version_id
= 3,
667 .minimum_version_id_old
= 3,
668 .fields
= (VMStateField
[]) {
669 VMSTATE_BUFFER(rregs
, ESPState
),
670 VMSTATE_BUFFER(wregs
, ESPState
),
671 VMSTATE_INT32(ti_size
, ESPState
),
672 VMSTATE_UINT32(ti_rptr
, ESPState
),
673 VMSTATE_UINT32(ti_wptr
, ESPState
),
674 VMSTATE_BUFFER(ti_buf
, ESPState
),
675 VMSTATE_UINT32(status
, ESPState
),
676 VMSTATE_UINT32(dma
, ESPState
),
677 VMSTATE_BUFFER(cmdbuf
, ESPState
),
678 VMSTATE_UINT32(cmdlen
, ESPState
),
679 VMSTATE_UINT32(do_cmd
, ESPState
),
680 VMSTATE_UINT32(dma_left
, ESPState
),
681 VMSTATE_END_OF_LIST()
692 static void sysbus_esp_mem_write(void *opaque
, target_phys_addr_t addr
,
693 uint64_t val
, unsigned int size
)
695 SysBusESPState
*sysbus
= opaque
;
698 saddr
= addr
>> sysbus
->it_shift
;
699 esp_reg_write(&sysbus
->esp
, saddr
, val
);
702 static uint64_t sysbus_esp_mem_read(void *opaque
, target_phys_addr_t addr
,
705 SysBusESPState
*sysbus
= opaque
;
708 saddr
= addr
>> sysbus
->it_shift
;
709 return esp_reg_read(&sysbus
->esp
, saddr
);
712 static const MemoryRegionOps sysbus_esp_mem_ops
= {
713 .read
= sysbus_esp_mem_read
,
714 .write
= sysbus_esp_mem_write
,
715 .endianness
= DEVICE_NATIVE_ENDIAN
,
716 .valid
.accepts
= esp_mem_accepts
,
719 void esp_init(target_phys_addr_t espaddr
, int it_shift
,
720 ESPDMAMemoryReadWriteFunc dma_memory_read
,
721 ESPDMAMemoryReadWriteFunc dma_memory_write
,
722 void *dma_opaque
, qemu_irq irq
, qemu_irq
*reset
,
723 qemu_irq
*dma_enable
)
727 SysBusESPState
*sysbus
;
730 dev
= qdev_create(NULL
, "esp");
731 sysbus
= DO_UPCAST(SysBusESPState
, busdev
.qdev
, dev
);
733 esp
->dma_memory_read
= dma_memory_read
;
734 esp
->dma_memory_write
= dma_memory_write
;
735 esp
->dma_opaque
= dma_opaque
;
736 sysbus
->it_shift
= it_shift
;
737 /* XXX for now until rc4030 has been changed to use DMA enable signal */
738 esp
->dma_enabled
= 1;
739 qdev_init_nofail(dev
);
740 s
= sysbus_from_qdev(dev
);
741 sysbus_connect_irq(s
, 0, irq
);
742 sysbus_mmio_map(s
, 0, espaddr
);
743 *reset
= qdev_get_gpio_in(dev
, 0);
744 *dma_enable
= qdev_get_gpio_in(dev
, 1);
747 static const struct SCSIBusInfo esp_scsi_info
= {
749 .max_target
= ESP_MAX_DEVS
,
752 .transfer_data
= esp_transfer_data
,
753 .complete
= esp_command_complete
,
754 .cancel
= esp_request_cancelled
757 static void sysbus_esp_gpio_demux(void *opaque
, int irq
, int level
)
759 DeviceState
*d
= opaque
;
760 SysBusESPState
*sysbus
= container_of(d
, SysBusESPState
, busdev
.qdev
);
761 ESPState
*s
= &sysbus
->esp
;
765 parent_esp_reset(s
, irq
, level
);
768 esp_dma_enable(opaque
, irq
, level
);
773 static int sysbus_esp_init(SysBusDevice
*dev
)
775 SysBusESPState
*sysbus
= FROM_SYSBUS(SysBusESPState
, dev
);
776 ESPState
*s
= &sysbus
->esp
;
778 sysbus_init_irq(dev
, &s
->irq
);
779 assert(sysbus
->it_shift
!= -1);
781 s
->chip_id
= TCHI_FAS100A
;
782 memory_region_init_io(&sysbus
->iomem
, &sysbus_esp_mem_ops
, sysbus
,
783 "esp", ESP_REGS
<< sysbus
->it_shift
);
784 sysbus_init_mmio(dev
, &sysbus
->iomem
);
786 qdev_init_gpio_in(&dev
->qdev
, sysbus_esp_gpio_demux
, 2);
788 scsi_bus_new(&s
->bus
, &dev
->qdev
, &esp_scsi_info
);
789 return scsi_bus_legacy_handle_cmdline(&s
->bus
);
792 static void sysbus_esp_hard_reset(DeviceState
*dev
)
794 SysBusESPState
*sysbus
= DO_UPCAST(SysBusESPState
, busdev
.qdev
, dev
);
795 esp_hard_reset(&sysbus
->esp
);
798 static const VMStateDescription vmstate_sysbus_esp_scsi
= {
799 .name
= "sysbusespscsi",
801 .minimum_version_id
= 0,
802 .minimum_version_id_old
= 0,
803 .fields
= (VMStateField
[]) {
804 VMSTATE_STRUCT(esp
, SysBusESPState
, 0, vmstate_esp
, ESPState
),
805 VMSTATE_END_OF_LIST()
809 static void sysbus_esp_class_init(ObjectClass
*klass
, void *data
)
811 DeviceClass
*dc
= DEVICE_CLASS(klass
);
812 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
814 k
->init
= sysbus_esp_init
;
815 dc
->reset
= sysbus_esp_hard_reset
;
816 dc
->vmsd
= &vmstate_sysbus_esp_scsi
;
819 static TypeInfo sysbus_esp_info
= {
821 .parent
= TYPE_SYS_BUS_DEVICE
,
822 .instance_size
= sizeof(SysBusESPState
),
823 .class_init
= sysbus_esp_class_init
,
832 #define DMA_SMDLA 0x6
835 #define DMA_CMD_MASK 0x03
836 #define DMA_CMD_DIAG 0x04
837 #define DMA_CMD_MDL 0x10
838 #define DMA_CMD_INTE_P 0x20
839 #define DMA_CMD_INTE_D 0x40
840 #define DMA_CMD_DIR 0x80
842 #define DMA_STAT_PWDN 0x01
843 #define DMA_STAT_ERROR 0x02
844 #define DMA_STAT_ABORT 0x04
845 #define DMA_STAT_DONE 0x08
846 #define DMA_STAT_SCSIINT 0x10
847 #define DMA_STAT_BCMBLT 0x20
849 #define SBAC_STATUS 0x1000
851 typedef struct PCIESPState
{
854 uint32_t dma_regs
[8];
859 static void esp_pci_handle_idle(PCIESPState
*pci
, uint32_t val
)
861 trace_esp_pci_dma_idle(val
);
862 esp_dma_enable(&pci
->esp
, 0, 0);
865 static void esp_pci_handle_blast(PCIESPState
*pci
, uint32_t val
)
867 trace_esp_pci_dma_blast(val
);
868 qemu_log_mask(LOG_UNIMP
, "am53c974: cmd BLAST not implemented\n");
871 static void esp_pci_handle_abort(PCIESPState
*pci
, uint32_t val
)
873 trace_esp_pci_dma_abort(val
);
874 if (pci
->esp
.current_req
) {
875 scsi_req_cancel(pci
->esp
.current_req
);
879 static void esp_pci_handle_start(PCIESPState
*pci
, uint32_t val
)
881 trace_esp_pci_dma_start(val
);
883 pci
->dma_regs
[DMA_WBC
] = pci
->dma_regs
[DMA_STC
];
884 pci
->dma_regs
[DMA_WAC
] = pci
->dma_regs
[DMA_SPA
];
885 pci
->dma_regs
[DMA_WMAC
] = pci
->dma_regs
[DMA_SMDLA
];
887 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
888 | DMA_STAT_DONE
| DMA_STAT_ABORT
889 | DMA_STAT_ERROR
| DMA_STAT_PWDN
);
891 esp_dma_enable(&pci
->esp
, 0, 1);
894 static void esp_pci_dma_write(PCIESPState
*pci
, uint32_t saddr
, uint32_t val
)
896 trace_esp_pci_dma_write(saddr
, pci
->dma_regs
[saddr
], val
);
899 pci
->dma_regs
[saddr
] = val
;
900 switch (val
& DMA_CMD_MASK
) {
902 esp_pci_handle_idle(pci
, val
);
904 case 0x1: /* BLAST */
905 esp_pci_handle_blast(pci
, val
);
907 case 0x2: /* ABORT */
908 esp_pci_handle_abort(pci
, val
);
910 case 0x3: /* START */
911 esp_pci_handle_start(pci
, val
);
913 default: /* can't happen */
920 pci
->dma_regs
[saddr
] = val
;
923 if (!(pci
->sbac
& SBAC_STATUS
)) {
924 /* clear some bits on write */
925 uint32_t mask
= DMA_STAT_ERROR
| DMA_STAT_ABORT
| DMA_STAT_DONE
;
926 pci
->dma_regs
[DMA_STAT
] &= ~(val
& mask
);
930 trace_esp_pci_error_invalid_write_dma(val
, saddr
);
935 static uint32_t esp_pci_dma_read(PCIESPState
*pci
, uint32_t saddr
)
939 val
= pci
->dma_regs
[saddr
];
940 if (saddr
== DMA_STAT
) {
941 if (pci
->esp
.rregs
[ESP_RSTAT
] & STAT_INT
) {
942 val
|= DMA_STAT_SCSIINT
;
944 if (pci
->sbac
& SBAC_STATUS
) {
945 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_ERROR
| DMA_STAT_ABORT
|
950 trace_esp_pci_dma_read(saddr
, val
);
954 static void esp_pci_io_write(void *opaque
, target_phys_addr_t addr
,
955 uint64_t val
, unsigned int size
)
957 PCIESPState
*pci
= opaque
;
959 if (size
< 4 || addr
& 3) {
960 /* need to upgrade request: we only support 4-bytes accesses */
961 uint32_t current
= 0, mask
;
965 current
= pci
->esp
.wregs
[addr
>> 2];
966 } else if (addr
< 0x60) {
967 current
= pci
->dma_regs
[(addr
- 0x40) >> 2];
968 } else if (addr
< 0x74) {
972 shift
= (4 - size
) * 8;
973 mask
= (~(uint32_t)0 << shift
) >> shift
;
975 shift
= ((4 - (addr
& 3)) & 3) * 8;
977 val
|= current
& ~(mask
<< shift
);
984 esp_reg_write(&pci
->esp
, addr
>> 2, val
);
985 } else if (addr
< 0x60) {
987 esp_pci_dma_write(pci
, (addr
- 0x40) >> 2, val
);
988 } else if (addr
== 0x70) {
989 /* DMA SCSI Bus and control */
990 trace_esp_pci_sbac_write(pci
->sbac
, val
);
993 trace_esp_pci_error_invalid_write((int)addr
);
997 static uint64_t esp_pci_io_read(void *opaque
, target_phys_addr_t addr
,
1000 PCIESPState
*pci
= opaque
;
1005 ret
= esp_reg_read(&pci
->esp
, addr
>> 2);
1006 } else if (addr
< 0x60) {
1008 ret
= esp_pci_dma_read(pci
, (addr
- 0x40) >> 2);
1009 } else if (addr
== 0x70) {
1010 /* DMA SCSI Bus and control */
1011 trace_esp_pci_sbac_read(pci
->sbac
);
1014 /* Invalid region */
1015 trace_esp_pci_error_invalid_read((int)addr
);
1019 /* give only requested data */
1020 ret
>>= (addr
& 3) * 8;
1021 ret
&= ~(~(uint64_t)0 << (8 * size
));
1026 static void esp_pci_dma_memory_rw(PCIESPState
*pci
, uint8_t *buf
, int len
,
1030 DMADirection expected_dir
;
1032 if (pci
->dma_regs
[DMA_CMD
] & DMA_CMD_DIR
) {
1033 expected_dir
= DMA_DIRECTION_FROM_DEVICE
;
1035 expected_dir
= DMA_DIRECTION_TO_DEVICE
;
1038 if (dir
!= expected_dir
) {
1039 trace_esp_pci_error_invalid_dma_direction();
1043 if (pci
->dma_regs
[DMA_STAT
] & DMA_CMD_MDL
) {
1044 qemu_log_mask(LOG_UNIMP
, "am53c974: MDL transfer not implemented\n");
1047 addr
= pci
->dma_regs
[DMA_SPA
];
1048 if (pci
->dma_regs
[DMA_WBC
] < len
) {
1049 len
= pci
->dma_regs
[DMA_WBC
];
1052 pci_dma_rw(&pci
->dev
, addr
, buf
, len
, dir
);
1054 /* update status registers */
1055 pci
->dma_regs
[DMA_WBC
] -= len
;
1056 pci
->dma_regs
[DMA_WAC
] += len
;
1059 static void esp_pci_dma_memory_read(void *opaque
, uint8_t *buf
, int len
)
1061 PCIESPState
*pci
= opaque
;
1062 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_TO_DEVICE
);
1065 static void esp_pci_dma_memory_write(void *opaque
, uint8_t *buf
, int len
)
1067 PCIESPState
*pci
= opaque
;
1068 esp_pci_dma_memory_rw(pci
, buf
, len
, DMA_DIRECTION_FROM_DEVICE
);
1071 static const MemoryRegionOps esp_pci_io_ops
= {
1072 .read
= esp_pci_io_read
,
1073 .write
= esp_pci_io_write
,
1074 .endianness
= DEVICE_LITTLE_ENDIAN
,
1076 .min_access_size
= 1,
1077 .max_access_size
= 4,
1081 static void esp_pci_hard_reset(DeviceState
*dev
)
1083 PCIESPState
*pci
= DO_UPCAST(PCIESPState
, dev
.qdev
, dev
);
1084 esp_hard_reset(&pci
->esp
);
1085 pci
->dma_regs
[DMA_CMD
] &= ~(DMA_CMD_DIR
| DMA_CMD_INTE_D
| DMA_CMD_INTE_P
1086 | DMA_CMD_MDL
| DMA_CMD_DIAG
| DMA_CMD_MASK
);
1087 pci
->dma_regs
[DMA_WBC
] &= ~0xffff;
1088 pci
->dma_regs
[DMA_WAC
] = 0xffffffff;
1089 pci
->dma_regs
[DMA_STAT
] &= ~(DMA_STAT_BCMBLT
| DMA_STAT_SCSIINT
1090 | DMA_STAT_DONE
| DMA_STAT_ABORT
1092 pci
->dma_regs
[DMA_WMAC
] = 0xfffffffd;
1095 static const VMStateDescription vmstate_esp_pci_scsi
= {
1096 .name
= "pciespscsi",
1098 .minimum_version_id
= 0,
1099 .minimum_version_id_old
= 0,
1100 .fields
= (VMStateField
[]) {
1101 VMSTATE_PCI_DEVICE(dev
, PCIESPState
),
1102 VMSTATE_BUFFER_UNSAFE(dma_regs
, PCIESPState
, 0, 8 * sizeof(uint32_t)),
1103 VMSTATE_STRUCT(esp
, PCIESPState
, 0, vmstate_esp
, ESPState
),
1104 VMSTATE_END_OF_LIST()
1108 static void esp_pci_command_complete(SCSIRequest
*req
, uint32_t status
,
1111 ESPState
*s
= req
->hba_private
;
1112 PCIESPState
*pci
= container_of(s
, PCIESPState
, esp
);
1114 esp_command_complete(req
, status
, resid
);
1115 pci
->dma_regs
[DMA_WBC
] = 0;
1116 pci
->dma_regs
[DMA_STAT
] |= DMA_STAT_DONE
;
1119 static const struct SCSIBusInfo esp_pci_scsi_info
= {
1121 .max_target
= ESP_MAX_DEVS
,
1124 .transfer_data
= esp_transfer_data
,
1125 .complete
= esp_pci_command_complete
,
1126 .cancel
= esp_request_cancelled
,
1129 static int esp_pci_scsi_init(PCIDevice
*dev
)
1131 PCIESPState
*pci
= DO_UPCAST(PCIESPState
, dev
, dev
);
1132 ESPState
*s
= &pci
->esp
;
1135 pci_conf
= pci
->dev
.config
;
1137 /* Interrupt pin A */
1138 pci_conf
[PCI_INTERRUPT_PIN
] = 0x01;
1140 s
->dma_memory_read
= esp_pci_dma_memory_read
;
1141 s
->dma_memory_write
= esp_pci_dma_memory_write
;
1142 s
->dma_opaque
= pci
;
1143 s
->chip_id
= TCHI_AM53C974
;
1144 memory_region_init_io(&pci
->io
, &esp_pci_io_ops
, pci
, "esp-io", 0x80);
1146 pci_register_bar(&pci
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->io
);
1147 s
->irq
= pci
->dev
.irq
[0];
1149 scsi_bus_new(&s
->bus
, &dev
->qdev
, &esp_pci_scsi_info
);
1150 if (!dev
->qdev
.hotplugged
) {
1151 return scsi_bus_legacy_handle_cmdline(&s
->bus
);
1156 static void esp_pci_scsi_uninit(PCIDevice
*d
)
1158 PCIESPState
*pci
= DO_UPCAST(PCIESPState
, dev
, d
);
1160 memory_region_destroy(&pci
->io
);
1163 static void esp_pci_class_init(ObjectClass
*klass
, void *data
)
1165 DeviceClass
*dc
= DEVICE_CLASS(klass
);
1166 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
1168 k
->init
= esp_pci_scsi_init
;
1169 k
->exit
= esp_pci_scsi_uninit
;
1170 k
->vendor_id
= PCI_VENDOR_ID_AMD
;
1171 k
->device_id
= PCI_DEVICE_ID_AMD_SCSI
;
1173 k
->class_id
= PCI_CLASS_STORAGE_SCSI
;
1174 dc
->desc
= "AMD Am53c974 PCscsi-PCI SCSI adapter";
1175 dc
->reset
= esp_pci_hard_reset
;
1176 dc
->vmsd
= &vmstate_esp_pci_scsi
;
1179 static TypeInfo esp_pci_info
= {
1181 .parent
= TYPE_PCI_DEVICE
,
1182 .instance_size
= sizeof(PCIESPState
),
1183 .class_init
= esp_pci_class_init
,
1186 static void esp_register_types(void)
1188 type_register_static(&sysbus_esp_info
);
1189 type_register_static(&esp_pci_info
);
1192 type_init(esp_register_types
)