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
26 #include "scsi-disk.h"
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, args...) \
42 do { printf("ESP: " fmt , ##args); } while (0)
44 #define DPRINTF(fmt, args...) do {} while (0)
47 #define ESP_ERROR(fmt, args...) \
48 do { printf("ESP ERROR: %s: " fmt, __func__ , ##args); } while (0)
53 typedef struct ESPState ESPState
;
58 uint8_t rregs
[ESP_REGS
];
59 uint8_t wregs
[ESP_REGS
];
61 uint32_t ti_rptr
, ti_wptr
;
62 uint8_t ti_buf
[TI_BUFSZ
];
65 SCSIDevice
*scsi_dev
[ESP_MAX_DEVS
];
66 SCSIDevice
*current_dev
;
67 uint8_t cmdbuf
[TI_BUFSZ
];
71 /* The amount of data left in the current DMA transfer. */
73 /* The size of the current DMA transfer. Zero if no transfer is in
79 espdma_memory_read_write dma_memory_read
;
80 espdma_memory_read_write dma_memory_write
;
89 #define ESP_WBUSID 0x4
93 #define ESP_WSYNTP 0x6
94 #define ESP_RFLAGS 0x7
100 #define ESP_WTEST 0xa
111 #define CMD_FLUSH 0x01
112 #define CMD_RESET 0x02
113 #define CMD_BUSRESET 0x03
115 #define CMD_ICCS 0x11
116 #define CMD_MSGACC 0x12
117 #define CMD_SATN 0x1a
118 #define CMD_SELATN 0x42
119 #define CMD_SELATNS 0x43
120 #define CMD_ENSEL 0x44
128 #define STAT_PIO_MASK 0x06
133 #define STAT_INT 0x80
135 #define BUSID_DID 0x07
140 #define INTR_RST 0x80
145 #define CFG1_RESREPT 0x40
147 #define TCHI_FAS100A 0x4
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
);
157 static void esp_lower_irq(ESPState
*s
)
159 if (s
->rregs
[ESP_RSTAT
] & STAT_INT
) {
160 s
->rregs
[ESP_RSTAT
] &= ~STAT_INT
;
161 qemu_irq_lower(s
->irq
);
165 static uint32_t get_cmd(ESPState
*s
, uint8_t *buf
)
170 target
= s
->wregs
[ESP_WBUSID
] & BUSID_DID
;
172 dmalen
= s
->rregs
[ESP_TCLO
] | (s
->rregs
[ESP_TCMID
] << 8);
173 s
->dma_memory_read(s
->dma_opaque
, buf
, dmalen
);
176 memcpy(buf
, s
->ti_buf
, dmalen
);
179 DPRINTF("get_cmd: len %d target %d\n", dmalen
, target
);
185 if (s
->current_dev
) {
186 /* Started a new command before the old one finished. Cancel it. */
187 s
->current_dev
->cancel_io(s
->current_dev
, 0);
191 if (target
>= ESP_MAX_DEVS
|| !s
->scsi_dev
[target
]) {
193 s
->rregs
[ESP_RSTAT
] = 0;
194 s
->rregs
[ESP_RINTR
] = INTR_DC
;
195 s
->rregs
[ESP_RSEQ
] = SEQ_0
;
199 s
->current_dev
= s
->scsi_dev
[target
];
203 static void do_cmd(ESPState
*s
, uint8_t *buf
)
208 DPRINTF("do_cmd: busid 0x%x\n", buf
[0]);
210 datalen
= s
->current_dev
->send_command(s
->current_dev
, 0, &buf
[1], lun
);
211 s
->ti_size
= datalen
;
213 s
->rregs
[ESP_RSTAT
] = STAT_TC
;
217 s
->rregs
[ESP_RSTAT
] |= STAT_DI
;
218 s
->current_dev
->read_data(s
->current_dev
, 0);
220 s
->rregs
[ESP_RSTAT
] |= STAT_DO
;
221 s
->current_dev
->write_data(s
->current_dev
, 0);
224 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
225 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
229 static void handle_satn(ESPState
*s
)
234 len
= get_cmd(s
, buf
);
239 static void handle_satn_stop(ESPState
*s
)
241 s
->cmdlen
= get_cmd(s
, s
->cmdbuf
);
243 DPRINTF("Set ATN & Stop: cmdlen %d\n", s
->cmdlen
);
245 s
->rregs
[ESP_RSTAT
] = STAT_TC
| STAT_CD
;
246 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
247 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
252 static void write_response(ESPState
*s
)
254 DPRINTF("Transfer status (sense=%d)\n", s
->sense
);
255 s
->ti_buf
[0] = s
->sense
;
258 s
->dma_memory_write(s
->dma_opaque
, s
->ti_buf
, 2);
259 s
->rregs
[ESP_RSTAT
] = STAT_TC
| STAT_ST
;
260 s
->rregs
[ESP_RINTR
] = INTR_BS
| INTR_FC
;
261 s
->rregs
[ESP_RSEQ
] = SEQ_CD
;
266 s
->rregs
[ESP_RFLAGS
] = 2;
271 static void esp_dma_done(ESPState
*s
)
273 s
->rregs
[ESP_RSTAT
] |= STAT_TC
;
274 s
->rregs
[ESP_RINTR
] = INTR_BS
;
275 s
->rregs
[ESP_RSEQ
] = 0;
276 s
->rregs
[ESP_RFLAGS
] = 0;
277 s
->rregs
[ESP_TCLO
] = 0;
278 s
->rregs
[ESP_TCMID
] = 0;
282 static void esp_do_dma(ESPState
*s
)
287 to_device
= (s
->ti_size
< 0);
290 DPRINTF("command len %d + %d\n", s
->cmdlen
, len
);
291 s
->dma_memory_read(s
->dma_opaque
, &s
->cmdbuf
[s
->cmdlen
], len
);
295 do_cmd(s
, s
->cmdbuf
);
298 if (s
->async_len
== 0) {
299 /* Defer until data is available. */
302 if (len
> s
->async_len
) {
306 s
->dma_memory_read(s
->dma_opaque
, s
->async_buf
, len
);
308 s
->dma_memory_write(s
->dma_opaque
, s
->async_buf
, len
);
317 if (s
->async_len
== 0) {
319 // ti_size is negative
320 s
->current_dev
->write_data(s
->current_dev
, 0);
322 s
->current_dev
->read_data(s
->current_dev
, 0);
323 /* If there is still data to be read from the device then
324 complete the DMA operation immediately. Otherwise defer
325 until the scsi layer has completed. */
326 if (s
->dma_left
== 0 && s
->ti_size
> 0) {
331 /* Partially filled a scsi buffer. Complete immediately. */
336 static void esp_command_complete(void *opaque
, int reason
, uint32_t tag
,
339 ESPState
*s
= (ESPState
*)opaque
;
341 if (reason
== SCSI_REASON_DONE
) {
342 DPRINTF("SCSI Command complete\n");
344 DPRINTF("SCSI command completed unexpectedly\n");
349 DPRINTF("Command failed\n");
351 s
->rregs
[ESP_RSTAT
] = STAT_ST
;
353 s
->current_dev
= NULL
;
355 DPRINTF("transfer %d/%d\n", s
->dma_left
, s
->ti_size
);
357 s
->async_buf
= s
->current_dev
->get_buf(s
->current_dev
, 0);
360 } else if (s
->dma_counter
!= 0 && s
->ti_size
<= 0) {
361 /* If this was the last part of a DMA transfer then the
362 completion interrupt is deferred to here. */
368 static void handle_ti(ESPState
*s
)
370 uint32_t dmalen
, minlen
;
372 dmalen
= s
->rregs
[ESP_TCLO
] | (s
->rregs
[ESP_TCMID
] << 8);
376 s
->dma_counter
= dmalen
;
379 minlen
= (dmalen
< 32) ? dmalen
: 32;
380 else if (s
->ti_size
< 0)
381 minlen
= (dmalen
< -s
->ti_size
) ? dmalen
: -s
->ti_size
;
383 minlen
= (dmalen
< s
->ti_size
) ? dmalen
: s
->ti_size
;
384 DPRINTF("Transfer Information len %d\n", minlen
);
386 s
->dma_left
= minlen
;
387 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
389 } else if (s
->do_cmd
) {
390 DPRINTF("command len %d\n", s
->cmdlen
);
394 do_cmd(s
, s
->cmdbuf
);
399 static void esp_reset(void *opaque
)
401 ESPState
*s
= opaque
;
405 memset(s
->rregs
, 0, ESP_REGS
);
406 memset(s
->wregs
, 0, ESP_REGS
);
407 s
->rregs
[ESP_TCHI
] = TCHI_FAS100A
; // Indicate fas100a
414 s
->rregs
[ESP_CFG1
] = 7;
417 static void parent_esp_reset(void *opaque
, int irq
, int level
)
423 static uint32_t esp_mem_readb(void *opaque
, target_phys_addr_t addr
)
425 ESPState
*s
= opaque
;
428 saddr
= addr
>> s
->it_shift
;
429 DPRINTF("read reg[%d]: 0x%2.2x\n", saddr
, s
->rregs
[saddr
]);
432 if (s
->ti_size
> 0) {
434 if ((s
->rregs
[ESP_RSTAT
] & STAT_PIO_MASK
) == 0) {
436 ESP_ERROR("PIO data read not implemented\n");
437 s
->rregs
[ESP_FIFO
] = 0;
439 s
->rregs
[ESP_FIFO
] = s
->ti_buf
[s
->ti_rptr
++];
443 if (s
->ti_size
== 0) {
449 // Clear interrupt/error status bits
450 s
->rregs
[ESP_RSTAT
] &= ~(STAT_GE
| STAT_PE
);
456 return s
->rregs
[saddr
];
459 static void esp_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
461 ESPState
*s
= opaque
;
464 saddr
= addr
>> s
->it_shift
;
465 DPRINTF("write reg[%d]: 0x%2.2x -> 0x%2.2x\n", saddr
, s
->wregs
[saddr
],
470 s
->rregs
[ESP_RSTAT
] &= ~STAT_TC
;
474 s
->cmdbuf
[s
->cmdlen
++] = val
& 0xff;
475 } else if (s
->ti_size
== TI_BUFSZ
- 1) {
476 ESP_ERROR("fifo overrun\n");
479 s
->ti_buf
[s
->ti_wptr
++] = val
& 0xff;
483 s
->rregs
[saddr
] = val
;
486 /* Reload DMA counter. */
487 s
->rregs
[ESP_TCLO
] = s
->wregs
[ESP_TCLO
];
488 s
->rregs
[ESP_TCMID
] = s
->wregs
[ESP_TCMID
];
492 switch(val
& CMD_CMD
) {
494 DPRINTF("NOP (%2.2x)\n", val
);
497 DPRINTF("Flush FIFO (%2.2x)\n", val
);
499 s
->rregs
[ESP_RINTR
] = INTR_FC
;
500 s
->rregs
[ESP_RSEQ
] = 0;
501 s
->rregs
[ESP_RFLAGS
] = 0;
504 DPRINTF("Chip reset (%2.2x)\n", val
);
508 DPRINTF("Bus reset (%2.2x)\n", val
);
509 s
->rregs
[ESP_RINTR
] = INTR_RST
;
510 if (!(s
->wregs
[ESP_CFG1
] & CFG1_RESREPT
)) {
518 DPRINTF("Initiator Command Complete Sequence (%2.2x)\n", val
);
520 s
->rregs
[ESP_RINTR
] = INTR_FC
;
521 s
->rregs
[ESP_RSTAT
] |= STAT_MI
;
524 DPRINTF("Message Accepted (%2.2x)\n", val
);
526 s
->rregs
[ESP_RINTR
] = INTR_DC
;
527 s
->rregs
[ESP_RSEQ
] = 0;
530 DPRINTF("Set ATN (%2.2x)\n", val
);
533 DPRINTF("Set ATN (%2.2x)\n", val
);
537 DPRINTF("Set ATN & stop (%2.2x)\n", val
);
541 DPRINTF("Enable selection (%2.2x)\n", val
);
542 s
->rregs
[ESP_RINTR
] = 0;
545 ESP_ERROR("Unhandled ESP command (%2.2x)\n", val
);
549 case ESP_WBUSID
... ESP_WSYNO
:
552 s
->rregs
[saddr
] = val
;
554 case ESP_WCCF
... ESP_WTEST
:
556 case ESP_CFG2
... ESP_RES4
:
557 s
->rregs
[saddr
] = val
;
560 ESP_ERROR("invalid write of 0x%02x at [0x%x]\n", val
, saddr
);
563 s
->wregs
[saddr
] = val
;
566 static CPUReadMemoryFunc
*esp_mem_read
[3] = {
572 static CPUWriteMemoryFunc
*esp_mem_write
[3] = {
578 static void esp_save(QEMUFile
*f
, void *opaque
)
580 ESPState
*s
= opaque
;
582 qemu_put_buffer(f
, s
->rregs
, ESP_REGS
);
583 qemu_put_buffer(f
, s
->wregs
, ESP_REGS
);
584 qemu_put_sbe32s(f
, &s
->ti_size
);
585 qemu_put_be32s(f
, &s
->ti_rptr
);
586 qemu_put_be32s(f
, &s
->ti_wptr
);
587 qemu_put_buffer(f
, s
->ti_buf
, TI_BUFSZ
);
588 qemu_put_be32s(f
, &s
->sense
);
589 qemu_put_be32s(f
, &s
->dma
);
590 qemu_put_buffer(f
, s
->cmdbuf
, TI_BUFSZ
);
591 qemu_put_be32s(f
, &s
->cmdlen
);
592 qemu_put_be32s(f
, &s
->do_cmd
);
593 qemu_put_be32s(f
, &s
->dma_left
);
594 // There should be no transfers in progress, so dma_counter is not saved
597 static int esp_load(QEMUFile
*f
, void *opaque
, int version_id
)
599 ESPState
*s
= opaque
;
602 return -EINVAL
; // Cannot emulate 2
604 qemu_get_buffer(f
, s
->rregs
, ESP_REGS
);
605 qemu_get_buffer(f
, s
->wregs
, ESP_REGS
);
606 qemu_get_sbe32s(f
, &s
->ti_size
);
607 qemu_get_be32s(f
, &s
->ti_rptr
);
608 qemu_get_be32s(f
, &s
->ti_wptr
);
609 qemu_get_buffer(f
, s
->ti_buf
, TI_BUFSZ
);
610 qemu_get_be32s(f
, &s
->sense
);
611 qemu_get_be32s(f
, &s
->dma
);
612 qemu_get_buffer(f
, s
->cmdbuf
, TI_BUFSZ
);
613 qemu_get_be32s(f
, &s
->cmdlen
);
614 qemu_get_be32s(f
, &s
->do_cmd
);
615 qemu_get_be32s(f
, &s
->dma_left
);
620 void esp_scsi_attach(void *opaque
, BlockDriverState
*bd
, int id
)
622 ESPState
*s
= (ESPState
*)opaque
;
625 for (id
= 0; id
< ESP_MAX_DEVS
; id
++) {
626 if (id
== (s
->rregs
[ESP_CFG1
] & 0x7))
628 if (s
->scsi_dev
[id
] == NULL
)
632 if (id
>= ESP_MAX_DEVS
) {
633 DPRINTF("Bad Device ID %d\n", id
);
636 if (s
->scsi_dev
[id
]) {
637 DPRINTF("Destroying device %d\n", id
);
638 s
->scsi_dev
[id
]->destroy(s
->scsi_dev
[id
]);
640 DPRINTF("Attaching block device %d\n", id
);
641 /* Command queueing is not implemented. */
642 s
->scsi_dev
[id
] = scsi_generic_init(bd
, 0, esp_command_complete
, s
);
643 if (s
->scsi_dev
[id
] == NULL
)
644 s
->scsi_dev
[id
] = scsi_disk_init(bd
, 0, esp_command_complete
, s
);
647 void *esp_init(target_phys_addr_t espaddr
, int it_shift
,
648 espdma_memory_read_write dma_memory_read
,
649 espdma_memory_read_write dma_memory_write
,
650 void *dma_opaque
, qemu_irq irq
, qemu_irq
*reset
)
655 s
= qemu_mallocz(sizeof(ESPState
));
658 s
->it_shift
= it_shift
;
659 s
->dma_memory_read
= dma_memory_read
;
660 s
->dma_memory_write
= dma_memory_write
;
661 s
->dma_opaque
= dma_opaque
;
663 esp_io_memory
= cpu_register_io_memory(0, esp_mem_read
, esp_mem_write
, s
);
664 cpu_register_physical_memory(espaddr
, ESP_REGS
<< it_shift
, esp_io_memory
);
668 register_savevm("esp", espaddr
, 3, esp_save
, esp_load
, s
);
669 qemu_register_reset(esp_reset
, s
);
671 *reset
= *qemu_allocate_irqs(parent_esp_reset
, s
, 1);