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/sysbus.h"
28 #include "hw/scsi/esp.h"
30 #include "qapi/error.h"
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
41 static void esp_raise_irq(ESPState
*s
)
43 if (!(s
->rregs
[ESP_RSTAT
] & STAT_INT
)) {
44 s
->rregs
[ESP_RSTAT
] |= STAT_INT
;
45 qemu_irq_raise(s
->irq
);
46 trace_esp_raise_irq();
50 static void esp_lower_irq(ESPState
*s
)
52 if (s
->rregs
[ESP_RSTAT
] & STAT_INT
) {
53 s
->rregs
[ESP_RSTAT
] &= ~STAT_INT
;
54 qemu_irq_lower(s
->irq
);
55 trace_esp_lower_irq();
59 void esp_dma_enable(ESPState
*s
, int irq
, int level
)
63 trace_esp_dma_enable();
69 trace_esp_dma_disable();
74 void esp_request_cancelled(SCSIRequest
*req
)
76 ESPState
*s
= req
->hba_private
;
78 if (req
== s
->current_req
) {
79 scsi_req_unref(s
->current_req
);
80 s
->current_req
= NULL
;
81 s
->current_dev
= NULL
;
85 static uint32_t get_cmd(ESPState
*s
, uint8_t *buf
, uint8_t buflen
)
90 target
= s
->wregs
[ESP_WBUSID
] & BUSID_DID
;
92 dmalen
= s
->rregs
[ESP_TCLO
];
93 dmalen
|= s
->rregs
[ESP_TCMID
] << 8;
94 dmalen
|= s
->rregs
[ESP_TCHI
] << 16;
95 if (dmalen
> buflen
) {
98 s
->dma_memory_read(s
->dma_opaque
, buf
, dmalen
);
101 memcpy(buf
, s
->ti_buf
, dmalen
);
102 buf
[0] = buf
[2] >> 5;
104 trace_esp_get_cmd(dmalen
, target
);
110 if (s
->current_req
) {
111 /* Started a new command before the old one finished. Cancel it. */
112 scsi_req_cancel(s
->current_req
);
116 s
->current_dev
= scsi_device_find(&s
->bus
, 0, target
, 0);
117 if (!s
->current_dev
) {
119 s
->rregs
[ESP_RSTAT
] = 0;
120 s
->rregs
[ESP_RINTR
] = INTR_DC
;
121 s
->rregs
[ESP_RSEQ
] = SEQ_0
;
128 static void do_busid_cmd(ESPState
*s
, uint8_t *buf
, uint8_t busid
)
132 SCSIDevice
*current_lun
;
134 trace_esp_do_busid_cmd(busid
);
136 current_lun
= scsi_device_find(&s
->bus
, 0, s
->current_dev
->id
, lun
);
137 s
->current_req
= scsi_req_new(current_lun
, 0, lun
, buf
, s
);
138 datalen
= scsi_req_enqueue(s
->current_req
);
139 s
->ti_size
= datalen
;
141 s
->rregs
[ESP_RSTAT
] = STAT_TC
;
145 s
->rregs
[ESP_RSTAT
] |= STAT_DI
;
147 s
->rregs
[ESP_RSTAT
] |= STAT_DO
;
149 scsi_req_continue(s
->current_req
);
151 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
152 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
156 static void do_cmd(ESPState
*s
, uint8_t *buf
)
158 uint8_t busid
= buf
[0];
160 do_busid_cmd(s
, &buf
[1], busid
);
163 static void handle_satn(ESPState
*s
)
168 if (s
->dma
&& !s
->dma_enabled
) {
169 s
->dma_cb
= handle_satn
;
172 len
= get_cmd(s
, buf
, sizeof(buf
));
177 static void handle_s_without_atn(ESPState
*s
)
182 if (s
->dma
&& !s
->dma_enabled
) {
183 s
->dma_cb
= handle_s_without_atn
;
186 len
= get_cmd(s
, buf
, sizeof(buf
));
188 do_busid_cmd(s
, buf
, 0);
192 static void handle_satn_stop(ESPState
*s
)
194 if (s
->dma
&& !s
->dma_enabled
) {
195 s
->dma_cb
= handle_satn_stop
;
198 s
->cmdlen
= get_cmd(s
, s
->cmdbuf
, sizeof(s
->cmdbuf
));
200 trace_esp_handle_satn_stop(s
->cmdlen
);
202 s
->rregs
[ESP_RSTAT
] = STAT_TC
| STAT_CD
;
203 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
204 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
209 static void write_response(ESPState
*s
)
211 trace_esp_write_response(s
->status
);
212 s
->ti_buf
[0] = s
->status
;
215 s
->dma_memory_write(s
->dma_opaque
, s
->ti_buf
, 2);
216 s
->rregs
[ESP_RSTAT
] = STAT_TC
| STAT_ST
;
217 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
218 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
223 s
->rregs
[ESP_RFLAGS
] = 2;
228 static void esp_dma_done(ESPState
*s
)
230 s
->rregs
[ESP_RSTAT
] |= STAT_TC
;
231 s
->rregs
[ESP_RINTR
] = INTR_BS
;
232 s
->rregs
[ESP_RSEQ
] = 0;
233 s
->rregs
[ESP_RFLAGS
] = 0;
234 s
->rregs
[ESP_TCLO
] = 0;
235 s
->rregs
[ESP_TCMID
] = 0;
236 s
->rregs
[ESP_TCHI
] = 0;
240 static void esp_do_dma(ESPState
*s
)
245 to_device
= (s
->ti_size
< 0);
248 trace_esp_do_dma(s
->cmdlen
, len
);
249 s
->dma_memory_read(s
->dma_opaque
, &s
->cmdbuf
[s
->cmdlen
], len
);
253 do_cmd(s
, s
->cmdbuf
);
256 if (s
->async_len
== 0) {
257 /* Defer until data is available. */
260 if (len
> s
->async_len
) {
264 s
->dma_memory_read(s
->dma_opaque
, s
->async_buf
, len
);
266 s
->dma_memory_write(s
->dma_opaque
, s
->async_buf
, len
);
275 if (s
->async_len
== 0) {
276 scsi_req_continue(s
->current_req
);
277 /* If there is still data to be read from the device then
278 complete the DMA operation immediately. Otherwise defer
279 until the scsi layer has completed. */
280 if (to_device
|| s
->dma_left
!= 0 || s
->ti_size
== 0) {
285 /* Partially filled a scsi buffer. Complete immediately. */
289 void esp_command_complete(SCSIRequest
*req
, uint32_t status
,
292 ESPState
*s
= req
->hba_private
;
294 trace_esp_command_complete();
295 if (s
->ti_size
!= 0) {
296 trace_esp_command_complete_unexpected();
302 trace_esp_command_complete_fail();
305 s
->rregs
[ESP_RSTAT
] = STAT_ST
;
307 if (s
->current_req
) {
308 scsi_req_unref(s
->current_req
);
309 s
->current_req
= NULL
;
310 s
->current_dev
= NULL
;
314 void esp_transfer_data(SCSIRequest
*req
, uint32_t len
)
316 ESPState
*s
= req
->hba_private
;
318 trace_esp_transfer_data(s
->dma_left
, s
->ti_size
);
320 s
->async_buf
= scsi_req_get_buf(req
);
323 } else if (s
->dma_counter
!= 0 && s
->ti_size
<= 0) {
324 /* If this was the last part of a DMA transfer then the
325 completion interrupt is deferred to here. */
330 static void handle_ti(ESPState
*s
)
332 uint32_t dmalen
, minlen
;
334 if (s
->dma
&& !s
->dma_enabled
) {
335 s
->dma_cb
= handle_ti
;
339 dmalen
= s
->rregs
[ESP_TCLO
];
340 dmalen
|= s
->rregs
[ESP_TCMID
] << 8;
341 dmalen
|= s
->rregs
[ESP_TCHI
] << 16;
345 s
->dma_counter
= dmalen
;
348 minlen
= (dmalen
< 32) ? dmalen
: 32;
349 else if (s
->ti_size
< 0)
350 minlen
= (dmalen
< -s
->ti_size
) ? dmalen
: -s
->ti_size
;
352 minlen
= (dmalen
< s
->ti_size
) ? dmalen
: s
->ti_size
;
353 trace_esp_handle_ti(minlen
);
355 s
->dma_left
= minlen
;
356 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
358 } else if (s
->do_cmd
) {
359 trace_esp_handle_ti_cmd(s
->cmdlen
);
363 do_cmd(s
, s
->cmdbuf
);
368 void esp_hard_reset(ESPState
*s
)
370 memset(s
->rregs
, 0, ESP_REGS
);
371 memset(s
->wregs
, 0, ESP_REGS
);
380 s
->rregs
[ESP_CFG1
] = 7;
383 static void esp_soft_reset(ESPState
*s
)
385 qemu_irq_lower(s
->irq
);
389 static void parent_esp_reset(ESPState
*s
, int irq
, int level
)
396 uint64_t esp_reg_read(ESPState
*s
, uint32_t saddr
)
400 trace_esp_mem_readb(saddr
, s
->rregs
[saddr
]);
403 if (s
->ti_size
> 0) {
405 if ((s
->rregs
[ESP_RSTAT
] & STAT_PIO_MASK
) == 0) {
407 qemu_log_mask(LOG_UNIMP
,
408 "esp: PIO data read not implemented\n");
409 s
->rregs
[ESP_FIFO
] = 0;
411 s
->rregs
[ESP_FIFO
] = s
->ti_buf
[s
->ti_rptr
++];
415 if (s
->ti_size
== 0) {
421 /* Clear sequence step, interrupt register and all status bits
423 old_val
= s
->rregs
[ESP_RINTR
];
424 s
->rregs
[ESP_RINTR
] = 0;
425 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
426 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
431 /* Return the unique id if the value has never been written */
432 if (!s
->tchi_written
) {
438 return s
->rregs
[saddr
];
441 void esp_reg_write(ESPState
*s
, uint32_t saddr
, uint64_t val
)
443 trace_esp_mem_writeb(saddr
, s
->wregs
[saddr
], val
);
446 s
->tchi_written
= true;
450 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
454 if (s
->cmdlen
< TI_BUFSZ
) {
455 s
->cmdbuf
[s
->cmdlen
++] = val
& 0xff;
457 trace_esp_error_fifo_overrun();
459 } else if (s
->ti_size
== TI_BUFSZ
- 1) {
460 trace_esp_error_fifo_overrun();
463 s
->ti_buf
[s
->ti_wptr
++] = val
& 0xff;
467 s
->rregs
[saddr
] = val
;
470 /* Reload DMA counter. */
471 s
->rregs
[ESP_TCLO
] = s
->wregs
[ESP_TCLO
];
472 s
->rregs
[ESP_TCMID
] = s
->wregs
[ESP_TCMID
];
473 s
->rregs
[ESP_TCHI
] = s
->wregs
[ESP_TCHI
];
477 switch(val
& CMD_CMD
) {
479 trace_esp_mem_writeb_cmd_nop(val
);
482 trace_esp_mem_writeb_cmd_flush(val
);
484 s
->rregs
[ESP_RINTR
] = INTR_FC
;
485 s
->rregs
[ESP_RSEQ
] = 0;
486 s
->rregs
[ESP_RFLAGS
] = 0;
489 trace_esp_mem_writeb_cmd_reset(val
);
493 trace_esp_mem_writeb_cmd_bus_reset(val
);
494 s
->rregs
[ESP_RINTR
] = INTR_RST
;
495 if (!(s
->wregs
[ESP_CFG1
] & CFG1_RESREPT
)) {
503 trace_esp_mem_writeb_cmd_iccs(val
);
505 s
->rregs
[ESP_RINTR
] = INTR_FC
;
506 s
->rregs
[ESP_RSTAT
] |= STAT_MI
;
509 trace_esp_mem_writeb_cmd_msgacc(val
);
510 s
->rregs
[ESP_RINTR
] = INTR_DC
;
511 s
->rregs
[ESP_RSEQ
] = 0;
512 s
->rregs
[ESP_RFLAGS
] = 0;
516 trace_esp_mem_writeb_cmd_pad(val
);
517 s
->rregs
[ESP_RSTAT
] = STAT_TC
;
518 s
->rregs
[ESP_RINTR
] = INTR_FC
;
519 s
->rregs
[ESP_RSEQ
] = 0;
522 trace_esp_mem_writeb_cmd_satn(val
);
525 trace_esp_mem_writeb_cmd_rstatn(val
);
528 trace_esp_mem_writeb_cmd_sel(val
);
529 handle_s_without_atn(s
);
532 trace_esp_mem_writeb_cmd_selatn(val
);
536 trace_esp_mem_writeb_cmd_selatns(val
);
540 trace_esp_mem_writeb_cmd_ensel(val
);
541 s
->rregs
[ESP_RINTR
] = 0;
544 trace_esp_mem_writeb_cmd_dissel(val
);
545 s
->rregs
[ESP_RINTR
] = 0;
549 trace_esp_error_unhandled_command(val
);
553 case ESP_WBUSID
... ESP_WSYNO
:
556 case ESP_CFG2
: case ESP_CFG3
:
557 case ESP_RES3
: case ESP_RES4
:
558 s
->rregs
[saddr
] = val
;
560 case ESP_WCCF
... ESP_WTEST
:
563 trace_esp_error_invalid_write(val
, saddr
);
566 s
->wregs
[saddr
] = val
;
569 static bool esp_mem_accepts(void *opaque
, hwaddr addr
,
570 unsigned size
, bool is_write
)
572 return (size
== 1) || (is_write
&& size
== 4);
575 const VMStateDescription vmstate_esp
= {
578 .minimum_version_id
= 3,
579 .fields
= (VMStateField
[]) {
580 VMSTATE_BUFFER(rregs
, ESPState
),
581 VMSTATE_BUFFER(wregs
, ESPState
),
582 VMSTATE_INT32(ti_size
, ESPState
),
583 VMSTATE_UINT32(ti_rptr
, ESPState
),
584 VMSTATE_UINT32(ti_wptr
, ESPState
),
585 VMSTATE_BUFFER(ti_buf
, ESPState
),
586 VMSTATE_UINT32(status
, ESPState
),
587 VMSTATE_UINT32(dma
, ESPState
),
588 VMSTATE_BUFFER(cmdbuf
, ESPState
),
589 VMSTATE_UINT32(cmdlen
, ESPState
),
590 VMSTATE_UINT32(do_cmd
, ESPState
),
591 VMSTATE_UINT32(dma_left
, ESPState
),
592 VMSTATE_END_OF_LIST()
596 #define TYPE_ESP "esp"
597 #define ESP(obj) OBJECT_CHECK(SysBusESPState, (obj), TYPE_ESP)
601 SysBusDevice parent_obj
;
609 static void sysbus_esp_mem_write(void *opaque
, hwaddr addr
,
610 uint64_t val
, unsigned int size
)
612 SysBusESPState
*sysbus
= opaque
;
615 saddr
= addr
>> sysbus
->it_shift
;
616 esp_reg_write(&sysbus
->esp
, saddr
, val
);
619 static uint64_t sysbus_esp_mem_read(void *opaque
, hwaddr addr
,
622 SysBusESPState
*sysbus
= opaque
;
625 saddr
= addr
>> sysbus
->it_shift
;
626 return esp_reg_read(&sysbus
->esp
, saddr
);
629 static const MemoryRegionOps sysbus_esp_mem_ops
= {
630 .read
= sysbus_esp_mem_read
,
631 .write
= sysbus_esp_mem_write
,
632 .endianness
= DEVICE_NATIVE_ENDIAN
,
633 .valid
.accepts
= esp_mem_accepts
,
636 void esp_init(hwaddr espaddr
, int it_shift
,
637 ESPDMAMemoryReadWriteFunc dma_memory_read
,
638 ESPDMAMemoryReadWriteFunc dma_memory_write
,
639 void *dma_opaque
, qemu_irq irq
, qemu_irq
*reset
,
640 qemu_irq
*dma_enable
)
644 SysBusESPState
*sysbus
;
647 dev
= qdev_create(NULL
, TYPE_ESP
);
650 esp
->dma_memory_read
= dma_memory_read
;
651 esp
->dma_memory_write
= dma_memory_write
;
652 esp
->dma_opaque
= dma_opaque
;
653 sysbus
->it_shift
= it_shift
;
654 /* XXX for now until rc4030 has been changed to use DMA enable signal */
655 esp
->dma_enabled
= 1;
656 qdev_init_nofail(dev
);
657 s
= SYS_BUS_DEVICE(dev
);
658 sysbus_connect_irq(s
, 0, irq
);
659 sysbus_mmio_map(s
, 0, espaddr
);
660 *reset
= qdev_get_gpio_in(dev
, 0);
661 *dma_enable
= qdev_get_gpio_in(dev
, 1);
664 static const struct SCSIBusInfo esp_scsi_info
= {
666 .max_target
= ESP_MAX_DEVS
,
669 .transfer_data
= esp_transfer_data
,
670 .complete
= esp_command_complete
,
671 .cancel
= esp_request_cancelled
674 static void sysbus_esp_gpio_demux(void *opaque
, int irq
, int level
)
676 SysBusESPState
*sysbus
= ESP(opaque
);
677 ESPState
*s
= &sysbus
->esp
;
681 parent_esp_reset(s
, irq
, level
);
684 esp_dma_enable(opaque
, irq
, level
);
689 static void sysbus_esp_realize(DeviceState
*dev
, Error
**errp
)
691 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
692 SysBusESPState
*sysbus
= ESP(dev
);
693 ESPState
*s
= &sysbus
->esp
;
696 sysbus_init_irq(sbd
, &s
->irq
);
697 assert(sysbus
->it_shift
!= -1);
699 s
->chip_id
= TCHI_FAS100A
;
700 memory_region_init_io(&sysbus
->iomem
, OBJECT(sysbus
), &sysbus_esp_mem_ops
,
701 sysbus
, "esp", ESP_REGS
<< sysbus
->it_shift
);
702 sysbus_init_mmio(sbd
, &sysbus
->iomem
);
704 qdev_init_gpio_in(dev
, sysbus_esp_gpio_demux
, 2);
706 scsi_bus_new(&s
->bus
, sizeof(s
->bus
), dev
, &esp_scsi_info
, NULL
);
707 scsi_bus_legacy_handle_cmdline(&s
->bus
, &err
);
709 error_propagate(errp
, err
);
714 static void sysbus_esp_hard_reset(DeviceState
*dev
)
716 SysBusESPState
*sysbus
= ESP(dev
);
717 esp_hard_reset(&sysbus
->esp
);
720 static const VMStateDescription vmstate_sysbus_esp_scsi
= {
721 .name
= "sysbusespscsi",
723 .minimum_version_id
= 0,
724 .fields
= (VMStateField
[]) {
725 VMSTATE_STRUCT(esp
, SysBusESPState
, 0, vmstate_esp
, ESPState
),
726 VMSTATE_END_OF_LIST()
730 static void sysbus_esp_class_init(ObjectClass
*klass
, void *data
)
732 DeviceClass
*dc
= DEVICE_CLASS(klass
);
734 dc
->realize
= sysbus_esp_realize
;
735 dc
->reset
= sysbus_esp_hard_reset
;
736 dc
->vmsd
= &vmstate_sysbus_esp_scsi
;
737 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
740 static const TypeInfo sysbus_esp_info
= {
742 .parent
= TYPE_SYS_BUS_DEVICE
,
743 .instance_size
= sizeof(SysBusESPState
),
744 .class_init
= sysbus_esp_class_init
,
747 static void esp_register_types(void)
749 type_register_static(&sysbus_esp_info
);
752 type_init(esp_register_types
)