2 * QEMU ESP/NCR53C9x emulation
4 * Copyright (c) 2005-2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 * On Sparc32, this is the ESP (NCR53C90) part of chip STP2000 (Master I/O),
34 * also produced as NCR89C100. See
35 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
37 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR53C9X.txt
41 #define DPRINTF(fmt, ...) \
42 do { printf("ESP: " fmt , ## __VA_ARGS__); } while (0)
44 #define DPRINTF(fmt, ...) do {} while (0)
47 #define ESP_ERROR(fmt, ...) \
48 do { printf("ESP ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
53 typedef struct ESPState ESPState
;
59 uint8_t rregs
[ESP_REGS
];
60 uint8_t wregs
[ESP_REGS
];
62 uint32_t ti_rptr
, ti_wptr
;
63 uint8_t ti_buf
[TI_BUFSZ
];
67 SCSIDevice
*current_dev
;
68 uint8_t cmdbuf
[TI_BUFSZ
];
72 /* The amount of data left in the current DMA transfer. */
74 /* The size of the current DMA transfer. Zero if no transfer is in
80 ESPDMAMemoryReadWriteFunc dma_memory_read
;
81 ESPDMAMemoryReadWriteFunc dma_memory_write
;
90 #define ESP_WBUSID 0x4
94 #define ESP_WSYNTP 0x6
95 #define ESP_RFLAGS 0x7
100 #define ESP_RRES2 0xa
101 #define ESP_WTEST 0xa
112 #define CMD_FLUSH 0x01
113 #define CMD_RESET 0x02
114 #define CMD_BUSRESET 0x03
116 #define CMD_ICCS 0x11
117 #define CMD_MSGACC 0x12
119 #define CMD_SATN 0x1a
121 #define CMD_SELATN 0x42
122 #define CMD_SELATNS 0x43
123 #define CMD_ENSEL 0x44
131 #define STAT_PIO_MASK 0x06
136 #define STAT_INT 0x80
138 #define BUSID_DID 0x07
143 #define INTR_RST 0x80
148 #define CFG1_RESREPT 0x40
150 #define TCHI_FAS100A 0x4
152 static void esp_raise_irq(ESPState
*s
)
154 if (!(s
->rregs
[ESP_RSTAT
] & STAT_INT
)) {
155 s
->rregs
[ESP_RSTAT
] |= STAT_INT
;
156 qemu_irq_raise(s
->irq
);
157 DPRINTF("Raise IRQ\n");
161 static void esp_lower_irq(ESPState
*s
)
163 if (s
->rregs
[ESP_RSTAT
] & STAT_INT
) {
164 s
->rregs
[ESP_RSTAT
] &= ~STAT_INT
;
165 qemu_irq_lower(s
->irq
);
166 DPRINTF("Lower IRQ\n");
170 static uint32_t get_cmd(ESPState
*s
, uint8_t *buf
)
175 target
= s
->wregs
[ESP_WBUSID
] & BUSID_DID
;
177 dmalen
= s
->rregs
[ESP_TCLO
] | (s
->rregs
[ESP_TCMID
] << 8);
178 s
->dma_memory_read(s
->dma_opaque
, buf
, dmalen
);
181 memcpy(buf
, s
->ti_buf
, dmalen
);
184 DPRINTF("get_cmd: len %d target %d\n", dmalen
, target
);
190 if (s
->current_dev
) {
191 /* Started a new command before the old one finished. Cancel it. */
192 s
->current_dev
->info
->cancel_io(s
->current_dev
, 0);
196 if (target
>= ESP_MAX_DEVS
|| !s
->bus
.devs
[target
]) {
198 s
->rregs
[ESP_RSTAT
] = 0;
199 s
->rregs
[ESP_RINTR
] = INTR_DC
;
200 s
->rregs
[ESP_RSEQ
] = SEQ_0
;
204 s
->current_dev
= s
->bus
.devs
[target
];
208 static void do_busid_cmd(ESPState
*s
, uint8_t *buf
, uint8_t busid
)
213 DPRINTF("do_busid_cmd: busid 0x%x\n", busid
);
215 datalen
= s
->current_dev
->info
->send_command(s
->current_dev
, 0, buf
, lun
);
216 s
->ti_size
= datalen
;
218 s
->rregs
[ESP_RSTAT
] = STAT_TC
;
222 s
->rregs
[ESP_RSTAT
] |= STAT_DI
;
223 s
->current_dev
->info
->read_data(s
->current_dev
, 0);
225 s
->rregs
[ESP_RSTAT
] |= STAT_DO
;
226 s
->current_dev
->info
->write_data(s
->current_dev
, 0);
229 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
230 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
234 static void do_cmd(ESPState
*s
, uint8_t *buf
)
236 uint8_t busid
= buf
[0];
238 do_busid_cmd(s
, &buf
[1], busid
);
241 static void handle_satn(ESPState
*s
)
246 len
= get_cmd(s
, buf
);
251 static void handle_s_without_atn(ESPState
*s
)
256 len
= get_cmd(s
, buf
);
258 do_busid_cmd(s
, buf
, 0);
262 static void handle_satn_stop(ESPState
*s
)
264 s
->cmdlen
= get_cmd(s
, s
->cmdbuf
);
266 DPRINTF("Set ATN & Stop: cmdlen %d\n", s
->cmdlen
);
268 s
->rregs
[ESP_RSTAT
] = STAT_TC
| STAT_CD
;
269 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
270 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
275 static void write_response(ESPState
*s
)
277 DPRINTF("Transfer status (sense=%d)\n", s
->sense
);
278 s
->ti_buf
[0] = s
->sense
;
281 s
->dma_memory_write(s
->dma_opaque
, s
->ti_buf
, 2);
282 s
->rregs
[ESP_RSTAT
] = STAT_TC
| STAT_ST
;
283 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
284 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
289 s
->rregs
[ESP_RFLAGS
] = 2;
294 static void esp_dma_done(ESPState
*s
)
296 s
->rregs
[ESP_RSTAT
] |= STAT_TC
;
297 s
->rregs
[ESP_RINTR
] = INTR_BS
;
298 s
->rregs
[ESP_RSEQ
] = 0;
299 s
->rregs
[ESP_RFLAGS
] = 0;
300 s
->rregs
[ESP_TCLO
] = 0;
301 s
->rregs
[ESP_TCMID
] = 0;
305 static void esp_do_dma(ESPState
*s
)
310 to_device
= (s
->ti_size
< 0);
313 DPRINTF("command len %d + %d\n", s
->cmdlen
, len
);
314 s
->dma_memory_read(s
->dma_opaque
, &s
->cmdbuf
[s
->cmdlen
], len
);
318 do_cmd(s
, s
->cmdbuf
);
321 if (s
->async_len
== 0) {
322 /* Defer until data is available. */
325 if (len
> s
->async_len
) {
329 s
->dma_memory_read(s
->dma_opaque
, s
->async_buf
, len
);
331 s
->dma_memory_write(s
->dma_opaque
, s
->async_buf
, len
);
340 if (s
->async_len
== 0) {
342 // ti_size is negative
343 s
->current_dev
->info
->write_data(s
->current_dev
, 0);
345 s
->current_dev
->info
->read_data(s
->current_dev
, 0);
346 /* If there is still data to be read from the device then
347 complete the DMA operation immediately. Otherwise defer
348 until the scsi layer has completed. */
349 if (s
->dma_left
== 0 && s
->ti_size
> 0) {
354 /* Partially filled a scsi buffer. Complete immediately. */
359 static void esp_command_complete(SCSIBus
*bus
, int reason
, uint32_t tag
,
362 ESPState
*s
= DO_UPCAST(ESPState
, busdev
.qdev
, bus
->qbus
.parent
);
364 if (reason
== SCSI_REASON_DONE
) {
365 DPRINTF("SCSI Command complete\n");
367 DPRINTF("SCSI command completed unexpectedly\n");
372 DPRINTF("Command failed\n");
374 s
->rregs
[ESP_RSTAT
] = STAT_ST
;
376 s
->current_dev
= NULL
;
378 DPRINTF("transfer %d/%d\n", s
->dma_left
, s
->ti_size
);
380 s
->async_buf
= s
->current_dev
->info
->get_buf(s
->current_dev
, 0);
383 } else if (s
->dma_counter
!= 0 && s
->ti_size
<= 0) {
384 /* If this was the last part of a DMA transfer then the
385 completion interrupt is deferred to here. */
391 static void handle_ti(ESPState
*s
)
393 uint32_t dmalen
, minlen
;
395 dmalen
= s
->rregs
[ESP_TCLO
] | (s
->rregs
[ESP_TCMID
] << 8);
399 s
->dma_counter
= dmalen
;
402 minlen
= (dmalen
< 32) ? dmalen
: 32;
403 else if (s
->ti_size
< 0)
404 minlen
= (dmalen
< -s
->ti_size
) ? dmalen
: -s
->ti_size
;
406 minlen
= (dmalen
< s
->ti_size
) ? dmalen
: s
->ti_size
;
407 DPRINTF("Transfer Information len %d\n", minlen
);
409 s
->dma_left
= minlen
;
410 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
412 } else if (s
->do_cmd
) {
413 DPRINTF("command len %d\n", s
->cmdlen
);
417 do_cmd(s
, s
->cmdbuf
);
422 static void esp_hard_reset(DeviceState
*d
)
424 ESPState
*s
= container_of(d
, ESPState
, busdev
.qdev
);
426 memset(s
->rregs
, 0, ESP_REGS
);
427 memset(s
->wregs
, 0, ESP_REGS
);
428 s
->rregs
[ESP_TCHI
] = TCHI_FAS100A
; // Indicate fas100a
435 s
->rregs
[ESP_CFG1
] = 7;
438 static void esp_soft_reset(DeviceState
*d
)
440 ESPState
*s
= container_of(d
, ESPState
, busdev
.qdev
);
442 qemu_irq_lower(s
->irq
);
446 static void parent_esp_reset(void *opaque
, int irq
, int level
)
449 esp_soft_reset(opaque
);
453 static uint32_t esp_mem_readb(void *opaque
, target_phys_addr_t addr
)
455 ESPState
*s
= opaque
;
456 uint32_t saddr
, old_val
;
458 saddr
= addr
>> s
->it_shift
;
459 DPRINTF("read reg[%d]: 0x%2.2x\n", saddr
, s
->rregs
[saddr
]);
462 if (s
->ti_size
> 0) {
464 if ((s
->rregs
[ESP_RSTAT
] & STAT_PIO_MASK
) == 0) {
466 ESP_ERROR("PIO data read not implemented\n");
467 s
->rregs
[ESP_FIFO
] = 0;
469 s
->rregs
[ESP_FIFO
] = s
->ti_buf
[s
->ti_rptr
++];
473 if (s
->ti_size
== 0) {
479 /* Clear sequence step, interrupt register and all status bits
481 old_val
= s
->rregs
[ESP_RINTR
];
482 s
->rregs
[ESP_RINTR
] = 0;
483 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
484 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
491 return s
->rregs
[saddr
];
494 static void esp_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
496 ESPState
*s
= opaque
;
499 saddr
= addr
>> s
->it_shift
;
500 DPRINTF("write reg[%d]: 0x%2.2x -> 0x%2.2x\n", saddr
, s
->wregs
[saddr
],
505 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
509 s
->cmdbuf
[s
->cmdlen
++] = val
& 0xff;
510 } else if (s
->ti_size
== TI_BUFSZ
- 1) {
511 ESP_ERROR("fifo overrun\n");
514 s
->ti_buf
[s
->ti_wptr
++] = val
& 0xff;
518 s
->rregs
[saddr
] = val
;
521 /* Reload DMA counter. */
522 s
->rregs
[ESP_TCLO
] = s
->wregs
[ESP_TCLO
];
523 s
->rregs
[ESP_TCMID
] = s
->wregs
[ESP_TCMID
];
527 switch(val
& CMD_CMD
) {
529 DPRINTF("NOP (%2.2x)\n", val
);
532 DPRINTF("Flush FIFO (%2.2x)\n", val
);
534 s
->rregs
[ESP_RINTR
] = INTR_FC
;
535 s
->rregs
[ESP_RSEQ
] = 0;
536 s
->rregs
[ESP_RFLAGS
] = 0;
539 DPRINTF("Chip reset (%2.2x)\n", val
);
540 esp_soft_reset(&s
->busdev
.qdev
);
543 DPRINTF("Bus reset (%2.2x)\n", val
);
544 s
->rregs
[ESP_RINTR
] = INTR_RST
;
545 if (!(s
->wregs
[ESP_CFG1
] & CFG1_RESREPT
)) {
553 DPRINTF("Initiator Command Complete Sequence (%2.2x)\n", val
);
555 s
->rregs
[ESP_RINTR
] = INTR_FC
;
556 s
->rregs
[ESP_RSTAT
] |= STAT_MI
;
559 DPRINTF("Message Accepted (%2.2x)\n", val
);
560 s
->rregs
[ESP_RINTR
] = INTR_DC
;
561 s
->rregs
[ESP_RSEQ
] = 0;
562 s
->rregs
[ESP_RFLAGS
] = 0;
566 DPRINTF("Transfer padding (%2.2x)\n", val
);
567 s
->rregs
[ESP_RSTAT
] = STAT_TC
;
568 s
->rregs
[ESP_RINTR
] = INTR_FC
;
569 s
->rregs
[ESP_RSEQ
] = 0;
572 DPRINTF("Set ATN (%2.2x)\n", val
);
575 DPRINTF("Select without ATN (%2.2x)\n", val
);
576 handle_s_without_atn(s
);
579 DPRINTF("Select with ATN (%2.2x)\n", val
);
583 DPRINTF("Select with ATN & stop (%2.2x)\n", val
);
587 DPRINTF("Enable selection (%2.2x)\n", val
);
588 s
->rregs
[ESP_RINTR
] = 0;
591 ESP_ERROR("Unhandled ESP command (%2.2x)\n", val
);
595 case ESP_WBUSID
... ESP_WSYNO
:
598 s
->rregs
[saddr
] = val
;
600 case ESP_WCCF
... ESP_WTEST
:
602 case ESP_CFG2
... ESP_RES4
:
603 s
->rregs
[saddr
] = val
;
606 ESP_ERROR("invalid write of 0x%02x at [0x%x]\n", val
, saddr
);
609 s
->wregs
[saddr
] = val
;
612 static CPUReadMemoryFunc
* const esp_mem_read
[3] = {
618 static CPUWriteMemoryFunc
* const esp_mem_write
[3] = {
624 static const VMStateDescription vmstate_esp
= {
627 .minimum_version_id
= 3,
628 .minimum_version_id_old
= 3,
629 .fields
= (VMStateField
[]) {
630 VMSTATE_BUFFER(rregs
, ESPState
),
631 VMSTATE_BUFFER(wregs
, ESPState
),
632 VMSTATE_INT32(ti_size
, ESPState
),
633 VMSTATE_UINT32(ti_rptr
, ESPState
),
634 VMSTATE_UINT32(ti_wptr
, ESPState
),
635 VMSTATE_BUFFER(ti_buf
, ESPState
),
636 VMSTATE_UINT32(sense
, ESPState
),
637 VMSTATE_UINT32(dma
, ESPState
),
638 VMSTATE_BUFFER(cmdbuf
, ESPState
),
639 VMSTATE_UINT32(cmdlen
, ESPState
),
640 VMSTATE_UINT32(do_cmd
, ESPState
),
641 VMSTATE_UINT32(dma_left
, ESPState
),
642 VMSTATE_END_OF_LIST()
646 void esp_init(target_phys_addr_t espaddr
, int it_shift
,
647 ESPDMAMemoryReadWriteFunc dma_memory_read
,
648 ESPDMAMemoryReadWriteFunc dma_memory_write
,
649 void *dma_opaque
, qemu_irq irq
, qemu_irq
*reset
)
655 dev
= qdev_create(NULL
, "esp");
656 esp
= DO_UPCAST(ESPState
, busdev
.qdev
, dev
);
657 esp
->dma_memory_read
= dma_memory_read
;
658 esp
->dma_memory_write
= dma_memory_write
;
659 esp
->dma_opaque
= dma_opaque
;
660 esp
->it_shift
= it_shift
;
661 qdev_init_nofail(dev
);
662 s
= sysbus_from_qdev(dev
);
663 sysbus_connect_irq(s
, 0, irq
);
664 sysbus_mmio_map(s
, 0, espaddr
);
665 *reset
= qdev_get_gpio_in(dev
, 0);
668 static int esp_init1(SysBusDevice
*dev
)
670 ESPState
*s
= FROM_SYSBUS(ESPState
, dev
);
673 sysbus_init_irq(dev
, &s
->irq
);
674 assert(s
->it_shift
!= -1);
676 esp_io_memory
= cpu_register_io_memory(esp_mem_read
, esp_mem_write
, s
);
677 sysbus_init_mmio(dev
, ESP_REGS
<< s
->it_shift
, esp_io_memory
);
679 qdev_init_gpio_in(&dev
->qdev
, parent_esp_reset
, 1);
681 scsi_bus_new(&s
->bus
, &dev
->qdev
, 0, ESP_MAX_DEVS
, esp_command_complete
);
682 return scsi_bus_legacy_handle_cmdline(&s
->bus
);
685 static SysBusDeviceInfo esp_info
= {
688 .qdev
.size
= sizeof(ESPState
),
689 .qdev
.vmsd
= &vmstate_esp
,
690 .qdev
.reset
= esp_hard_reset
,
691 .qdev
.props
= (Property
[]) {
696 static void esp_register_devices(void)
698 sysbus_register_withprop(&esp_info
);
701 device_init(esp_register_devices
)