2 * QEMU LSI53C895A SCSI Host Bus Adapter emulation
4 * Copyright (c) 2006 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licenced under the LGPL.
10 /* ??? Need to check if the {read,write}[wl] routines work properly on
11 big-endian targets. */
15 #include "scsi-disk.h"
16 #include "block_int.h"
19 //#define DEBUG_LSI_REG
22 #define DPRINTF(fmt, args...) \
23 do { printf("lsi_scsi: " fmt , ##args); } while (0)
24 #define BADF(fmt, args...) \
25 do { fprintf(stderr, "lsi_scsi: error: " fmt , ##args); exit(1);} while (0)
27 #define DPRINTF(fmt, args...) do {} while(0)
28 #define BADF(fmt, args...) \
29 do { fprintf(stderr, "lsi_scsi: error: " fmt , ##args);} while (0)
32 #define LSI_SCNTL0_TRG 0x01
33 #define LSI_SCNTL0_AAP 0x02
34 #define LSI_SCNTL0_EPC 0x08
35 #define LSI_SCNTL0_WATN 0x10
36 #define LSI_SCNTL0_START 0x20
38 #define LSI_SCNTL1_SST 0x01
39 #define LSI_SCNTL1_IARB 0x02
40 #define LSI_SCNTL1_AESP 0x04
41 #define LSI_SCNTL1_RST 0x08
42 #define LSI_SCNTL1_CON 0x10
43 #define LSI_SCNTL1_DHP 0x20
44 #define LSI_SCNTL1_ADB 0x40
45 #define LSI_SCNTL1_EXC 0x80
47 #define LSI_SCNTL2_WSR 0x01
48 #define LSI_SCNTL2_VUE0 0x02
49 #define LSI_SCNTL2_VUE1 0x04
50 #define LSI_SCNTL2_WSS 0x08
51 #define LSI_SCNTL2_SLPHBEN 0x10
52 #define LSI_SCNTL2_SLPMD 0x20
53 #define LSI_SCNTL2_CHM 0x40
54 #define LSI_SCNTL2_SDU 0x80
56 #define LSI_ISTAT0_DIP 0x01
57 #define LSI_ISTAT0_SIP 0x02
58 #define LSI_ISTAT0_INTF 0x04
59 #define LSI_ISTAT0_CON 0x08
60 #define LSI_ISTAT0_SEM 0x10
61 #define LSI_ISTAT0_SIGP 0x20
62 #define LSI_ISTAT0_SRST 0x40
63 #define LSI_ISTAT0_ABRT 0x80
65 #define LSI_ISTAT1_SI 0x01
66 #define LSI_ISTAT1_SRUN 0x02
67 #define LSI_ISTAT1_FLSH 0x04
69 #define LSI_SSTAT0_SDP0 0x01
70 #define LSI_SSTAT0_RST 0x02
71 #define LSI_SSTAT0_WOA 0x04
72 #define LSI_SSTAT0_LOA 0x08
73 #define LSI_SSTAT0_AIP 0x10
74 #define LSI_SSTAT0_OLF 0x20
75 #define LSI_SSTAT0_ORF 0x40
76 #define LSI_SSTAT0_ILF 0x80
78 #define LSI_SIST0_PAR 0x01
79 #define LSI_SIST0_RST 0x02
80 #define LSI_SIST0_UDC 0x04
81 #define LSI_SIST0_SGE 0x08
82 #define LSI_SIST0_RSL 0x10
83 #define LSI_SIST0_SEL 0x20
84 #define LSI_SIST0_CMP 0x40
85 #define LSI_SIST0_MA 0x80
87 #define LSI_SIST1_HTH 0x01
88 #define LSI_SIST1_GEN 0x02
89 #define LSI_SIST1_STO 0x04
90 #define LSI_SIST1_SBMC 0x10
92 #define LSI_SOCL_IO 0x01
93 #define LSI_SOCL_CD 0x02
94 #define LSI_SOCL_MSG 0x04
95 #define LSI_SOCL_ATN 0x08
96 #define LSI_SOCL_SEL 0x10
97 #define LSI_SOCL_BSY 0x20
98 #define LSI_SOCL_ACK 0x40
99 #define LSI_SOCL_REQ 0x80
101 #define LSI_DSTAT_IID 0x01
102 #define LSI_DSTAT_SIR 0x04
103 #define LSI_DSTAT_SSI 0x08
104 #define LSI_DSTAT_ABRT 0x10
105 #define LSI_DSTAT_BF 0x20
106 #define LSI_DSTAT_MDPE 0x40
107 #define LSI_DSTAT_DFE 0x80
109 #define LSI_DCNTL_COM 0x01
110 #define LSI_DCNTL_IRQD 0x02
111 #define LSI_DCNTL_STD 0x04
112 #define LSI_DCNTL_IRQM 0x08
113 #define LSI_DCNTL_SSM 0x10
114 #define LSI_DCNTL_PFEN 0x20
115 #define LSI_DCNTL_PFF 0x40
116 #define LSI_DCNTL_CLSE 0x80
118 #define LSI_DMODE_MAN 0x01
119 #define LSI_DMODE_BOF 0x02
120 #define LSI_DMODE_ERMP 0x04
121 #define LSI_DMODE_ERL 0x08
122 #define LSI_DMODE_DIOM 0x10
123 #define LSI_DMODE_SIOM 0x20
125 #define LSI_CTEST2_DACK 0x01
126 #define LSI_CTEST2_DREQ 0x02
127 #define LSI_CTEST2_TEOP 0x04
128 #define LSI_CTEST2_PCICIE 0x08
129 #define LSI_CTEST2_CM 0x10
130 #define LSI_CTEST2_CIO 0x20
131 #define LSI_CTEST2_SIGP 0x40
132 #define LSI_CTEST2_DDIR 0x80
134 #define LSI_CTEST5_BL2 0x04
135 #define LSI_CTEST5_DDIR 0x08
136 #define LSI_CTEST5_MASR 0x10
137 #define LSI_CTEST5_DFSN 0x20
138 #define LSI_CTEST5_BBCK 0x40
139 #define LSI_CTEST5_ADCK 0x80
141 #define LSI_CCNTL0_DILS 0x01
142 #define LSI_CCNTL0_DISFC 0x10
143 #define LSI_CCNTL0_ENNDJ 0x20
144 #define LSI_CCNTL0_PMJCTL 0x40
145 #define LSI_CCNTL0_ENPMJ 0x80
155 /* Maximum length of MSG IN data. */
156 #define LSI_MAX_MSGIN_LEN 8
158 /* Flag set if this is a tagged command. */
159 #define LSI_TAG_VALID (1 << 16)
171 uint32_t script_ram_base
;
173 int carry
; /* ??? Should this be an a visible register somewhere? */
175 /* Action to take at the end of a MSG IN phase.
176 0 = COMMAND, 1 = disconect, 2 = DATA OUT, 3 = DATA IN. */
179 uint8_t msg
[LSI_MAX_MSGIN_LEN
];
180 /* 0 if SCRIPTS are running or stopped.
181 * 1 if a Wait Reselect instruction has been issued.
182 * 2 if processing DMA from lsi_execute_script.
183 * 3 if a DMA operation is in progress. */
185 SCSIDevice
*scsi_dev
[LSI_MAX_DEVS
];
186 SCSIDevice
*current_dev
;
188 /* The tag is a combination of the device ID and the SCSI tag. */
189 uint32_t current_tag
;
190 uint32_t current_dma_len
;
191 int command_complete
;
255 uint32_t scratch
[18]; /* SCRATCHA-SCRATCHR */
257 /* Script ram is stored as 32-bit words in host byteorder. */
258 uint32_t script_ram
[2048];
261 static void lsi_soft_reset(LSIState
*s
)
271 memset(s
->scratch
, 0, sizeof(s
->scratch
));
325 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
);
326 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
);
327 static void lsi_execute_script(LSIState
*s
);
329 static inline uint32_t read_dword(LSIState
*s
, uint32_t addr
)
333 /* Optimize reading from SCRIPTS RAM. */
334 if ((addr
& 0xffffe000) == s
->script_ram_base
) {
335 return s
->script_ram
[(addr
& 0x1fff) >> 2];
337 cpu_physical_memory_read(addr
, (uint8_t *)&buf
, 4);
338 return cpu_to_le32(buf
);
341 static void lsi_stop_script(LSIState
*s
)
343 s
->istat1
&= ~LSI_ISTAT1_SRUN
;
346 static void lsi_update_irq(LSIState
*s
)
349 static int last_level
;
351 /* It's unclear whether the DIP/SIP bits should be cleared when the
352 Interrupt Status Registers are cleared or when istat0 is read.
353 We currently do the formwer, which seems to work. */
356 if (s
->dstat
& s
->dien
)
358 s
->istat0
|= LSI_ISTAT0_DIP
;
360 s
->istat0
&= ~LSI_ISTAT0_DIP
;
363 if (s
->sist0
|| s
->sist1
) {
364 if ((s
->sist0
& s
->sien0
) || (s
->sist1
& s
->sien1
))
366 s
->istat0
|= LSI_ISTAT0_SIP
;
368 s
->istat0
&= ~LSI_ISTAT0_SIP
;
370 if (s
->istat0
& LSI_ISTAT0_INTF
)
373 if (level
!= last_level
) {
374 DPRINTF("Update IRQ level %d dstat %02x sist %02x%02x\n",
375 level
, s
->dstat
, s
->sist1
, s
->sist0
);
378 qemu_set_irq(s
->pci_dev
.irq
[0], level
);
381 /* Stop SCRIPTS execution and raise a SCSI interrupt. */
382 static void lsi_script_scsi_interrupt(LSIState
*s
, int stat0
, int stat1
)
387 DPRINTF("SCSI Interrupt 0x%02x%02x prev 0x%02x%02x\n",
388 stat1
, stat0
, s
->sist1
, s
->sist0
);
391 /* Stop processor on fatal or unmasked interrupt. As a special hack
392 we don't stop processing when raising STO. Instead continue
393 execution and stop at the next insn that accesses the SCSI bus. */
394 mask0
= s
->sien0
| ~(LSI_SIST0_CMP
| LSI_SIST0_SEL
| LSI_SIST0_RSL
);
395 mask1
= s
->sien1
| ~(LSI_SIST1_GEN
| LSI_SIST1_HTH
);
396 mask1
&= ~LSI_SIST1_STO
;
397 if (s
->sist0
& mask0
|| s
->sist1
& mask1
) {
403 /* Stop SCRIPTS execution and raise a DMA interrupt. */
404 static void lsi_script_dma_interrupt(LSIState
*s
, int stat
)
406 DPRINTF("DMA Interrupt 0x%x prev 0x%x\n", stat
, s
->dstat
);
412 static inline void lsi_set_phase(LSIState
*s
, int phase
)
414 s
->sstat1
= (s
->sstat1
& ~PHASE_MASK
) | phase
;
417 static void lsi_bad_phase(LSIState
*s
, int out
, int new_phase
)
419 /* Trigger a phase mismatch. */
420 if (s
->ccntl0
& LSI_CCNTL0_ENPMJ
) {
421 if ((s
->ccntl0
& LSI_CCNTL0_PMJCTL
) || out
) {
426 DPRINTF("Data phase mismatch jump to %08x\n", s
->dsp
);
428 DPRINTF("Phase mismatch interrupt\n");
429 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
432 lsi_set_phase(s
, new_phase
);
436 /* Resume SCRIPTS execution after a DMA operation. */
437 static void lsi_resume_script(LSIState
*s
)
439 if (s
->waiting
!= 2) {
441 lsi_execute_script(s
);
447 /* Initiate a SCSI layer data transfer. */
448 static void lsi_do_dma(LSIState
*s
, int out
)
453 if (!s
->current_dma_len
) {
454 /* Wait until data is available. */
455 DPRINTF("DMA no data available\n");
460 if (count
> s
->current_dma_len
)
461 count
= s
->current_dma_len
;
462 DPRINTF("DMA addr=0x%08x len=%d\n", s
->dnad
, count
);
469 if (s
->dma_buf
== NULL
) {
470 s
->dma_buf
= s
->current_dev
->get_buf(s
->current_dev
,
474 /* ??? Set SFBR to first data byte. */
476 cpu_physical_memory_read(addr
, s
->dma_buf
, count
);
478 cpu_physical_memory_write(addr
, s
->dma_buf
, count
);
480 s
->current_dma_len
-= count
;
481 if (s
->current_dma_len
== 0) {
484 /* Write the data. */
485 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
487 /* Request any remaining data. */
488 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
492 lsi_resume_script(s
);
497 /* Add a command to the queue. */
498 static void lsi_queue_command(LSIState
*s
)
502 DPRINTF("Queueing tag=0x%x\n", s
->current_tag
);
503 if (s
->queue_len
== s
->active_commands
) {
505 s
->queue
= qemu_realloc(s
->queue
, s
->queue_len
* sizeof(lsi_queue
));
507 p
= &s
->queue
[s
->active_commands
++];
508 p
->tag
= s
->current_tag
;
510 p
->out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
513 /* Queue a byte for a MSG IN phase. */
514 static void lsi_add_msg_byte(LSIState
*s
, uint8_t data
)
516 if (s
->msg_len
>= LSI_MAX_MSGIN_LEN
) {
517 BADF("MSG IN data too long\n");
519 DPRINTF("MSG IN 0x%02x\n", data
);
520 s
->msg
[s
->msg_len
++] = data
;
524 /* Perform reselection to continue a command. */
525 static void lsi_reselect(LSIState
*s
, uint32_t tag
)
532 for (n
= 0; n
< s
->active_commands
; n
++) {
537 if (n
== s
->active_commands
) {
538 BADF("Reselected non-existant command tag=0x%x\n", tag
);
541 id
= (tag
>> 8) & 0xf;
543 DPRINTF("Reselected target %d\n", id
);
544 s
->current_dev
= s
->scsi_dev
[id
];
545 s
->current_tag
= tag
;
546 s
->scntl1
|= LSI_SCNTL1_CON
;
547 lsi_set_phase(s
, PHASE_MI
);
548 s
->msg_action
= p
->out
? 2 : 3;
549 s
->current_dma_len
= p
->pending
;
551 lsi_add_msg_byte(s
, 0x80);
552 if (s
->current_tag
& LSI_TAG_VALID
) {
553 lsi_add_msg_byte(s
, 0x20);
554 lsi_add_msg_byte(s
, tag
& 0xff);
557 s
->active_commands
--;
558 if (n
!= s
->active_commands
) {
559 s
->queue
[n
] = s
->queue
[s
->active_commands
];
563 /* Record that data is available for a queued command. Returns zero if
564 the device was reselected, nonzero if the IO is deferred. */
565 static int lsi_queue_tag(LSIState
*s
, uint32_t tag
, uint32_t arg
)
569 for (i
= 0; i
< s
->active_commands
; i
++) {
573 BADF("Multiple IO pending for tag %d\n", tag
);
576 if (s
->waiting
== 1) {
577 /* Reselect device. */
578 lsi_reselect(s
, tag
);
581 DPRINTF("Queueing IO tag=0x%x\n", tag
);
587 BADF("IO with unknown tag %d\n", tag
);
591 /* Callback to indicate that the SCSI layer has completed a transfer. */
592 static void lsi_command_complete(void *opaque
, int reason
, uint32_t tag
,
595 LSIState
*s
= (LSIState
*)opaque
;
598 out
= (s
->sstat1
& PHASE_MASK
) == PHASE_DO
;
599 if (reason
== SCSI_REASON_DONE
) {
600 DPRINTF("Command complete sense=%d\n", (int)arg
);
602 s
->command_complete
= 2;
603 if (s
->waiting
&& s
->dbc
!= 0) {
604 /* Raise phase mismatch for short transfers. */
605 lsi_bad_phase(s
, out
, PHASE_ST
);
607 lsi_set_phase(s
, PHASE_ST
);
609 lsi_resume_script(s
);
613 if (s
->waiting
== 1 || tag
!= s
->current_tag
) {
614 if (lsi_queue_tag(s
, tag
, arg
))
617 DPRINTF("Data ready tag=0x%x len=%d\n", tag
, arg
);
618 s
->current_dma_len
= arg
;
619 s
->command_complete
= 1;
622 if (s
->waiting
== 1 || s
->dbc
== 0) {
623 lsi_resume_script(s
);
629 static void lsi_do_command(LSIState
*s
)
634 DPRINTF("Send command len=%d\n", s
->dbc
);
637 cpu_physical_memory_read(s
->dnad
, buf
, s
->dbc
);
639 s
->command_complete
= 0;
640 n
= s
->current_dev
->send_command(s
->current_dev
, s
->current_tag
, buf
,
643 lsi_set_phase(s
, PHASE_DI
);
644 s
->current_dev
->read_data(s
->current_dev
, s
->current_tag
);
646 lsi_set_phase(s
, PHASE_DO
);
647 s
->current_dev
->write_data(s
->current_dev
, s
->current_tag
);
650 if (!s
->command_complete
) {
652 /* Command did not complete immediately so disconnect. */
653 lsi_add_msg_byte(s
, 2); /* SAVE DATA POINTER */
654 lsi_add_msg_byte(s
, 4); /* DISCONNECT */
656 lsi_set_phase(s
, PHASE_MI
);
658 lsi_queue_command(s
);
660 /* wait command complete */
661 lsi_set_phase(s
, PHASE_DI
);
666 static void lsi_do_status(LSIState
*s
)
669 DPRINTF("Get status len=%d sense=%d\n", s
->dbc
, s
->sense
);
671 BADF("Bad Status move\n");
675 cpu_physical_memory_write(s
->dnad
, &sense
, 1);
676 lsi_set_phase(s
, PHASE_MI
);
678 lsi_add_msg_byte(s
, 0); /* COMMAND COMPLETE */
681 static void lsi_disconnect(LSIState
*s
)
683 s
->scntl1
&= ~LSI_SCNTL1_CON
;
684 s
->sstat1
&= ~PHASE_MASK
;
687 static void lsi_do_msgin(LSIState
*s
)
690 DPRINTF("Message in len=%d/%d\n", s
->dbc
, s
->msg_len
);
695 cpu_physical_memory_write(s
->dnad
, s
->msg
, len
);
696 /* Linux drivers rely on the last byte being in the SIDL. */
697 s
->sidl
= s
->msg
[len
- 1];
700 memmove(s
->msg
, s
->msg
+ len
, s
->msg_len
);
702 /* ??? Check if ATN (not yet implemented) is asserted and maybe
703 switch to PHASE_MO. */
704 switch (s
->msg_action
) {
706 lsi_set_phase(s
, PHASE_CMD
);
712 lsi_set_phase(s
, PHASE_DO
);
715 lsi_set_phase(s
, PHASE_DI
);
723 /* Read the next byte during a MSGOUT phase. */
724 static uint8_t lsi_get_msgbyte(LSIState
*s
)
727 cpu_physical_memory_read(s
->dnad
, &data
, 1);
733 static void lsi_do_msgout(LSIState
*s
)
738 DPRINTF("MSG out len=%d\n", s
->dbc
);
740 msg
= lsi_get_msgbyte(s
);
745 DPRINTF("MSG: Disconnect\n");
749 DPRINTF("MSG: No Operation\n");
750 lsi_set_phase(s
, PHASE_CMD
);
753 len
= lsi_get_msgbyte(s
);
754 msg
= lsi_get_msgbyte(s
);
755 DPRINTF("Extended message 0x%x (len %d)\n", msg
, len
);
758 DPRINTF("SDTR (ignored)\n");
762 DPRINTF("WDTR (ignored)\n");
769 case 0x20: /* SIMPLE queue */
770 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
771 DPRINTF("SIMPLE queue tag=0x%x\n", s
->current_tag
& 0xff);
773 case 0x21: /* HEAD of queue */
774 BADF("HEAD queue not implemented\n");
775 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
777 case 0x22: /* ORDERED queue */
778 BADF("ORDERED queue not implemented\n");
779 s
->current_tag
|= lsi_get_msgbyte(s
) | LSI_TAG_VALID
;
782 if ((msg
& 0x80) == 0) {
785 s
->current_lun
= msg
& 7;
786 DPRINTF("Select LUN %d\n", s
->current_lun
);
787 lsi_set_phase(s
, PHASE_CMD
);
793 BADF("Unimplemented message 0x%02x\n", msg
);
794 lsi_set_phase(s
, PHASE_MI
);
795 lsi_add_msg_byte(s
, 7); /* MESSAGE REJECT */
799 /* Sign extend a 24-bit value. */
800 static inline int32_t sxt24(int32_t n
)
802 return (n
<< 8) >> 8;
805 static void lsi_memcpy(LSIState
*s
, uint32_t dest
, uint32_t src
, int count
)
808 uint8_t buf
[TARGET_PAGE_SIZE
];
810 DPRINTF("memcpy dest 0x%08x src 0x%08x count %d\n", dest
, src
, count
);
812 n
= (count
> TARGET_PAGE_SIZE
) ? TARGET_PAGE_SIZE
: count
;
813 cpu_physical_memory_read(src
, buf
, n
);
814 cpu_physical_memory_write(dest
, buf
, n
);
821 static void lsi_wait_reselect(LSIState
*s
)
824 DPRINTF("Wait Reselect\n");
825 if (s
->current_dma_len
)
826 BADF("Reselect with pending DMA\n");
827 for (i
= 0; i
< s
->active_commands
; i
++) {
828 if (s
->queue
[i
].pending
) {
829 lsi_reselect(s
, s
->queue
[i
].tag
);
833 if (s
->current_dma_len
== 0) {
838 static void lsi_execute_script(LSIState
*s
)
844 s
->istat1
|= LSI_ISTAT1_SRUN
;
846 insn
= read_dword(s
, s
->dsp
);
847 addr
= read_dword(s
, s
->dsp
+ 4);
848 DPRINTF("SCRIPTS dsp=%08x opcode %08x arg %08x\n", s
->dsp
, insn
, addr
);
850 s
->dcmd
= insn
>> 24;
852 switch (insn
>> 30) {
853 case 0: /* Block move. */
854 if (s
->sist1
& LSI_SIST1_STO
) {
855 DPRINTF("Delayed select timeout\n");
859 s
->dbc
= insn
& 0xffffff;
861 if (insn
& (1 << 29)) {
862 /* Indirect addressing. */
863 addr
= read_dword(s
, addr
);
864 } else if (insn
& (1 << 28)) {
867 /* Table indirect addressing. */
868 offset
= sxt24(addr
);
869 cpu_physical_memory_read(s
->dsa
+ offset
, (uint8_t *)buf
, 8);
870 s
->dbc
= cpu_to_le32(buf
[0]);
872 addr
= cpu_to_le32(buf
[1]);
874 if ((s
->sstat1
& PHASE_MASK
) != ((insn
>> 24) & 7)) {
875 DPRINTF("Wrong phase got %d expected %d\n",
876 s
->sstat1
& PHASE_MASK
, (insn
>> 24) & 7);
877 lsi_script_scsi_interrupt(s
, LSI_SIST0_MA
, 0);
883 switch (s
->sstat1
& 0x7) {
909 BADF("Unimplemented phase %d\n", s
->sstat1
& PHASE_MASK
);
912 s
->dfifo
= s
->dbc
& 0xff;
913 s
->ctest5
= (s
->ctest5
& 0xfc) | ((s
->dbc
>> 8) & 3);
916 s
->ua
= addr
+ s
->dbc
;
919 case 1: /* IO or Read/Write instruction. */
920 opcode
= (insn
>> 27) & 7;
924 if (insn
& (1 << 25)) {
925 id
= read_dword(s
, s
->dsa
+ sxt24(insn
));
929 id
= (id
>> 16) & 0xf;
930 if (insn
& (1 << 26)) {
931 addr
= s
->dsp
+ sxt24(addr
);
937 if (s
->current_dma_len
&& (s
->ssid
& 0xf) == id
) {
938 DPRINTF("Already reselected by target %d\n", id
);
941 s
->sstat0
|= LSI_SSTAT0_WOA
;
942 s
->scntl1
&= ~LSI_SCNTL1_IARB
;
943 if (id
>= LSI_MAX_DEVS
|| !s
->scsi_dev
[id
]) {
944 DPRINTF("Selected absent target %d\n", id
);
945 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_STO
);
949 DPRINTF("Selected target %d%s\n",
950 id
, insn
& (1 << 3) ? " ATN" : "");
951 /* ??? Linux drivers compain when this is set. Maybe
952 it only applies in low-level mode (unimplemented).
953 lsi_script_scsi_interrupt(s, LSI_SIST0_CMP, 0); */
954 s
->current_dev
= s
->scsi_dev
[id
];
955 s
->current_tag
= id
<< 8;
956 s
->scntl1
|= LSI_SCNTL1_CON
;
957 if (insn
& (1 << 3)) {
958 s
->socl
|= LSI_SOCL_ATN
;
960 lsi_set_phase(s
, PHASE_MO
);
962 case 1: /* Disconnect */
963 DPRINTF("Wait Disconect\n");
964 s
->scntl1
&= ~LSI_SCNTL1_CON
;
966 case 2: /* Wait Reselect */
967 lsi_wait_reselect(s
);
970 DPRINTF("Set%s%s%s%s\n",
971 insn
& (1 << 3) ? " ATN" : "",
972 insn
& (1 << 6) ? " ACK" : "",
973 insn
& (1 << 9) ? " TM" : "",
974 insn
& (1 << 10) ? " CC" : "");
975 if (insn
& (1 << 3)) {
976 s
->socl
|= LSI_SOCL_ATN
;
977 lsi_set_phase(s
, PHASE_MO
);
979 if (insn
& (1 << 9)) {
980 BADF("Target mode not implemented\n");
983 if (insn
& (1 << 10))
987 DPRINTF("Clear%s%s%s%s\n",
988 insn
& (1 << 3) ? " ATN" : "",
989 insn
& (1 << 6) ? " ACK" : "",
990 insn
& (1 << 9) ? " TM" : "",
991 insn
& (1 << 10) ? " CC" : "");
992 if (insn
& (1 << 3)) {
993 s
->socl
&= ~LSI_SOCL_ATN
;
995 if (insn
& (1 << 10))
1006 static const char *opcode_names
[3] =
1007 {"Write", "Read", "Read-Modify-Write"};
1008 static const char *operator_names
[8] =
1009 {"MOV", "SHL", "OR", "XOR", "AND", "SHR", "ADD", "ADC"};
1012 reg
= ((insn
>> 16) & 0x7f) | (insn
& 0x80);
1013 data8
= (insn
>> 8) & 0xff;
1014 opcode
= (insn
>> 27) & 7;
1015 operator = (insn
>> 24) & 7;
1016 DPRINTF("%s reg 0x%x %s data8=0x%02x sfbr=0x%02x%s\n",
1017 opcode_names
[opcode
- 5], reg
,
1018 operator_names
[operator], data8
, s
->sfbr
,
1019 (insn
& (1 << 23)) ? " SFBR" : "");
1022 case 5: /* From SFBR */
1026 case 6: /* To SFBR */
1028 op0
= lsi_reg_readb(s
, reg
);
1031 case 7: /* Read-modify-write */
1033 op0
= lsi_reg_readb(s
, reg
);
1034 if (insn
& (1 << 23)) {
1046 case 1: /* Shift left */
1048 op0
= (op0
<< 1) | s
->carry
;
1062 op0
= (op0
>> 1) | (s
->carry
<< 7);
1067 s
->carry
= op0
< op1
;
1070 op0
+= op1
+ s
->carry
;
1072 s
->carry
= op0
<= op1
;
1074 s
->carry
= op0
< op1
;
1079 case 5: /* From SFBR */
1080 case 7: /* Read-modify-write */
1081 lsi_reg_writeb(s
, reg
, op0
);
1083 case 6: /* To SFBR */
1090 case 2: /* Transfer Control. */
1095 if ((insn
& 0x002e0000) == 0) {
1099 if (s
->sist1
& LSI_SIST1_STO
) {
1100 DPRINTF("Delayed select timeout\n");
1104 cond
= jmp
= (insn
& (1 << 19)) != 0;
1105 if (cond
== jmp
&& (insn
& (1 << 21))) {
1106 DPRINTF("Compare carry %d\n", s
->carry
== jmp
);
1107 cond
= s
->carry
!= 0;
1109 if (cond
== jmp
&& (insn
& (1 << 17))) {
1110 DPRINTF("Compare phase %d %c= %d\n",
1111 (s
->sstat1
& PHASE_MASK
),
1113 ((insn
>> 24) & 7));
1114 cond
= (s
->sstat1
& PHASE_MASK
) == ((insn
>> 24) & 7);
1116 if (cond
== jmp
&& (insn
& (1 << 18))) {
1119 mask
= (~insn
>> 8) & 0xff;
1120 DPRINTF("Compare data 0x%x & 0x%x %c= 0x%x\n",
1121 s
->sfbr
, mask
, jmp
? '=' : '!', insn
& mask
);
1122 cond
= (s
->sfbr
& mask
) == (insn
& mask
);
1125 if (insn
& (1 << 23)) {
1126 /* Relative address. */
1127 addr
= s
->dsp
+ sxt24(addr
);
1129 switch ((insn
>> 27) & 7) {
1131 DPRINTF("Jump to 0x%08x\n", addr
);
1135 DPRINTF("Call 0x%08x\n", addr
);
1139 case 2: /* Return */
1140 DPRINTF("Return to 0x%08x\n", s
->temp
);
1143 case 3: /* Interrupt */
1144 DPRINTF("Interrupt 0x%08x\n", s
->dsps
);
1145 if ((insn
& (1 << 20)) != 0) {
1146 s
->istat0
|= LSI_ISTAT0_INTF
;
1149 lsi_script_dma_interrupt(s
, LSI_DSTAT_SIR
);
1153 DPRINTF("Illegal transfer control\n");
1154 lsi_script_dma_interrupt(s
, LSI_DSTAT_IID
);
1158 DPRINTF("Control condition failed\n");
1164 if ((insn
& (1 << 29)) == 0) {
1167 /* ??? The docs imply the destination address is loaded into
1168 the TEMP register. However the Linux drivers rely on
1169 the value being presrved. */
1170 dest
= read_dword(s
, s
->dsp
);
1172 lsi_memcpy(s
, dest
, addr
, insn
& 0xffffff);
1179 if (insn
& (1 << 28)) {
1180 addr
= s
->dsa
+ sxt24(addr
);
1183 reg
= (insn
>> 16) & 0xff;
1184 if (insn
& (1 << 24)) {
1185 cpu_physical_memory_read(addr
, data
, n
);
1186 DPRINTF("Load reg 0x%x size %d addr 0x%08x = %08x\n", reg
, n
,
1187 addr
, *(int *)data
);
1188 for (i
= 0; i
< n
; i
++) {
1189 lsi_reg_writeb(s
, reg
+ i
, data
[i
]);
1192 DPRINTF("Store reg 0x%x size %d addr 0x%08x\n", reg
, n
, addr
);
1193 for (i
= 0; i
< n
; i
++) {
1194 data
[i
] = lsi_reg_readb(s
, reg
+ i
);
1196 cpu_physical_memory_write(addr
, data
, n
);
1200 /* ??? Need to avoid infinite loops. */
1201 if (s
->istat1
& LSI_ISTAT1_SRUN
&& !s
->waiting
) {
1202 if (s
->dcntl
& LSI_DCNTL_SSM
) {
1203 lsi_script_dma_interrupt(s
, LSI_DSTAT_SSI
);
1208 DPRINTF("SCRIPTS execution stopped\n");
1211 static uint8_t lsi_reg_readb(LSIState
*s
, int offset
)
1214 #define CASE_GET_REG32(name, addr) \
1215 case addr: return s->name & 0xff; \
1216 case addr + 1: return (s->name >> 8) & 0xff; \
1217 case addr + 2: return (s->name >> 16) & 0xff; \
1218 case addr + 3: return (s->name >> 24) & 0xff;
1220 #ifdef DEBUG_LSI_REG
1221 DPRINTF("Read reg %x\n", offset
);
1224 case 0x00: /* SCNTL0 */
1226 case 0x01: /* SCNTL1 */
1228 case 0x02: /* SCNTL2 */
1230 case 0x03: /* SCNTL3 */
1232 case 0x04: /* SCID */
1234 case 0x05: /* SXFER */
1236 case 0x06: /* SDID */
1238 case 0x07: /* GPREG0 */
1240 case 0x08: /* Revision ID */
1242 case 0xa: /* SSID */
1244 case 0xb: /* SBCL */
1245 /* ??? This is not correct. However it's (hopefully) only
1246 used for diagnostics, so should be ok. */
1248 case 0xc: /* DSTAT */
1249 tmp
= s
->dstat
| 0x80;
1250 if ((s
->istat0
& LSI_ISTAT0_INTF
) == 0)
1254 case 0x0d: /* SSTAT0 */
1256 case 0x0e: /* SSTAT1 */
1258 case 0x0f: /* SSTAT2 */
1259 return s
->scntl1
& LSI_SCNTL1_CON
? 0 : 2;
1260 CASE_GET_REG32(dsa
, 0x10)
1261 case 0x14: /* ISTAT0 */
1263 case 0x16: /* MBOX0 */
1265 case 0x17: /* MBOX1 */
1267 case 0x18: /* CTEST0 */
1269 case 0x19: /* CTEST1 */
1271 case 0x1a: /* CTEST2 */
1272 tmp
= LSI_CTEST2_DACK
| LSI_CTEST2_CM
;
1273 if (s
->istat0
& LSI_ISTAT0_SIGP
) {
1274 s
->istat0
&= ~LSI_ISTAT0_SIGP
;
1275 tmp
|= LSI_CTEST2_SIGP
;
1278 case 0x1b: /* CTEST3 */
1280 CASE_GET_REG32(temp
, 0x1c)
1281 case 0x20: /* DFIFO */
1283 case 0x21: /* CTEST4 */
1285 case 0x22: /* CTEST5 */
1287 case 0x23: /* CTEST6 */
1289 case 0x24: /* DBC[0:7] */
1290 return s
->dbc
& 0xff;
1291 case 0x25: /* DBC[8:15] */
1292 return (s
->dbc
>> 8) & 0xff;
1293 case 0x26: /* DBC[16->23] */
1294 return (s
->dbc
>> 16) & 0xff;
1295 case 0x27: /* DCMD */
1297 CASE_GET_REG32(dsp
, 0x2c)
1298 CASE_GET_REG32(dsps
, 0x30)
1299 CASE_GET_REG32(scratch
[0], 0x34)
1300 case 0x38: /* DMODE */
1302 case 0x39: /* DIEN */
1304 case 0x3b: /* DCNTL */
1306 case 0x40: /* SIEN0 */
1308 case 0x41: /* SIEN1 */
1310 case 0x42: /* SIST0 */
1315 case 0x43: /* SIST1 */
1320 case 0x47: /* GPCNTL0 */
1322 case 0x48: /* STIME0 */
1324 case 0x4a: /* RESPID0 */
1326 case 0x4b: /* RESPID1 */
1328 case 0x4d: /* STEST1 */
1330 case 0x4e: /* STEST2 */
1332 case 0x4f: /* STEST3 */
1334 case 0x50: /* SIDL */
1335 /* This is needed by the linux drivers. We currently only update it
1336 during the MSG IN phase. */
1338 case 0x52: /* STEST4 */
1340 case 0x56: /* CCNTL0 */
1342 case 0x57: /* CCNTL1 */
1344 case 0x58: /* SBDL */
1345 /* Some drivers peek at the data bus during the MSG IN phase. */
1346 if ((s
->sstat1
& PHASE_MASK
) == PHASE_MI
)
1349 case 0x59: /* SBDL high */
1351 CASE_GET_REG32(mmrs
, 0xa0)
1352 CASE_GET_REG32(mmws
, 0xa4)
1353 CASE_GET_REG32(sfs
, 0xa8)
1354 CASE_GET_REG32(drs
, 0xac)
1355 CASE_GET_REG32(sbms
, 0xb0)
1356 CASE_GET_REG32(dmbs
, 0xb4)
1357 CASE_GET_REG32(dnad64
, 0xb8)
1358 CASE_GET_REG32(pmjad1
, 0xc0)
1359 CASE_GET_REG32(pmjad2
, 0xc4)
1360 CASE_GET_REG32(rbc
, 0xc8)
1361 CASE_GET_REG32(ua
, 0xcc)
1362 CASE_GET_REG32(ia
, 0xd4)
1363 CASE_GET_REG32(sbc
, 0xd8)
1364 CASE_GET_REG32(csbc
, 0xdc)
1366 if (offset
>= 0x5c && offset
< 0xa0) {
1369 n
= (offset
- 0x58) >> 2;
1370 shift
= (offset
& 3) * 8;
1371 return (s
->scratch
[n
] >> shift
) & 0xff;
1373 BADF("readb 0x%x\n", offset
);
1375 #undef CASE_GET_REG32
1378 static void lsi_reg_writeb(LSIState
*s
, int offset
, uint8_t val
)
1380 #define CASE_SET_REG32(name, addr) \
1381 case addr : s->name &= 0xffffff00; s->name |= val; break; \
1382 case addr + 1: s->name &= 0xffff00ff; s->name |= val << 8; break; \
1383 case addr + 2: s->name &= 0xff00ffff; s->name |= val << 16; break; \
1384 case addr + 3: s->name &= 0x00ffffff; s->name |= val << 24; break;
1386 #ifdef DEBUG_LSI_REG
1387 DPRINTF("Write reg %x = %02x\n", offset
, val
);
1390 case 0x00: /* SCNTL0 */
1392 if (val
& LSI_SCNTL0_START
) {
1393 BADF("Start sequence not implemented\n");
1396 case 0x01: /* SCNTL1 */
1397 s
->scntl1
= val
& ~LSI_SCNTL1_SST
;
1398 if (val
& LSI_SCNTL1_IARB
) {
1399 BADF("Immediate Arbritration not implemented\n");
1401 if (val
& LSI_SCNTL1_RST
) {
1402 s
->sstat0
|= LSI_SSTAT0_RST
;
1403 lsi_script_scsi_interrupt(s
, LSI_SIST0_RST
, 0);
1405 s
->sstat0
&= ~LSI_SSTAT0_RST
;
1408 case 0x02: /* SCNTL2 */
1409 val
&= ~(LSI_SCNTL2_WSR
| LSI_SCNTL2_WSS
);
1412 case 0x03: /* SCNTL3 */
1415 case 0x04: /* SCID */
1418 case 0x05: /* SXFER */
1421 case 0x06: /* SDID */
1422 if ((val
& 0xf) != (s
->ssid
& 0xf))
1423 BADF("Destination ID does not match SSID\n");
1424 s
->sdid
= val
& 0xf;
1426 case 0x07: /* GPREG0 */
1428 case 0x08: /* SFBR */
1429 /* The CPU is not allowed to write to this register. However the
1430 SCRIPTS register move instructions are. */
1433 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1434 /* Linux writes to these readonly registers on startup. */
1436 CASE_SET_REG32(dsa
, 0x10)
1437 case 0x14: /* ISTAT0 */
1438 s
->istat0
= (s
->istat0
& 0x0f) | (val
& 0xf0);
1439 if (val
& LSI_ISTAT0_ABRT
) {
1440 lsi_script_dma_interrupt(s
, LSI_DSTAT_ABRT
);
1442 if (val
& LSI_ISTAT0_INTF
) {
1443 s
->istat0
&= ~LSI_ISTAT0_INTF
;
1446 if (s
->waiting
== 1 && val
& LSI_ISTAT0_SIGP
) {
1447 DPRINTF("Woken by SIGP\n");
1450 lsi_execute_script(s
);
1452 if (val
& LSI_ISTAT0_SRST
) {
1456 case 0x16: /* MBOX0 */
1459 case 0x17: /* MBOX1 */
1462 case 0x1b: /* CTEST3 */
1463 s
->ctest3
= val
& 0x0f;
1465 CASE_SET_REG32(temp
, 0x1c)
1466 case 0x21: /* CTEST4 */
1468 BADF("Unimplemented CTEST4-FBL 0x%x\n", val
);
1472 case 0x22: /* CTEST5 */
1473 if (val
& (LSI_CTEST5_ADCK
| LSI_CTEST5_BBCK
)) {
1474 BADF("CTEST5 DMA increment not implemented\n");
1478 case 0x2c: /* DSP[0:7] */
1479 s
->dsp
&= 0xffffff00;
1482 case 0x2d: /* DSP[8:15] */
1483 s
->dsp
&= 0xffff00ff;
1486 case 0x2e: /* DSP[16:23] */
1487 s
->dsp
&= 0xff00ffff;
1488 s
->dsp
|= val
<< 16;
1490 case 0x2f: /* DSP[24:31] */
1491 s
->dsp
&= 0x00ffffff;
1492 s
->dsp
|= val
<< 24;
1493 if ((s
->dmode
& LSI_DMODE_MAN
) == 0
1494 && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1495 lsi_execute_script(s
);
1497 CASE_SET_REG32(dsps
, 0x30)
1498 CASE_SET_REG32(scratch
[0], 0x34)
1499 case 0x38: /* DMODE */
1500 if (val
& (LSI_DMODE_SIOM
| LSI_DMODE_DIOM
)) {
1501 BADF("IO mappings not implemented\n");
1505 case 0x39: /* DIEN */
1509 case 0x3b: /* DCNTL */
1510 s
->dcntl
= val
& ~(LSI_DCNTL_PFF
| LSI_DCNTL_STD
);
1511 if ((val
& LSI_DCNTL_STD
) && (s
->istat1
& LSI_ISTAT1_SRUN
) == 0)
1512 lsi_execute_script(s
);
1514 case 0x40: /* SIEN0 */
1518 case 0x41: /* SIEN1 */
1522 case 0x47: /* GPCNTL0 */
1524 case 0x48: /* STIME0 */
1527 case 0x49: /* STIME1 */
1529 DPRINTF("General purpose timer not implemented\n");
1530 /* ??? Raising the interrupt immediately seems to be sufficient
1531 to keep the FreeBSD driver happy. */
1532 lsi_script_scsi_interrupt(s
, 0, LSI_SIST1_GEN
);
1535 case 0x4a: /* RESPID0 */
1538 case 0x4b: /* RESPID1 */
1541 case 0x4d: /* STEST1 */
1544 case 0x4e: /* STEST2 */
1546 BADF("Low level mode not implemented\n");
1550 case 0x4f: /* STEST3 */
1552 BADF("SCSI FIFO test mode not implemented\n");
1556 case 0x56: /* CCNTL0 */
1559 case 0x57: /* CCNTL1 */
1562 CASE_SET_REG32(mmrs
, 0xa0)
1563 CASE_SET_REG32(mmws
, 0xa4)
1564 CASE_SET_REG32(sfs
, 0xa8)
1565 CASE_SET_REG32(drs
, 0xac)
1566 CASE_SET_REG32(sbms
, 0xb0)
1567 CASE_SET_REG32(dmbs
, 0xb4)
1568 CASE_SET_REG32(dnad64
, 0xb8)
1569 CASE_SET_REG32(pmjad1
, 0xc0)
1570 CASE_SET_REG32(pmjad2
, 0xc4)
1571 CASE_SET_REG32(rbc
, 0xc8)
1572 CASE_SET_REG32(ua
, 0xcc)
1573 CASE_SET_REG32(ia
, 0xd4)
1574 CASE_SET_REG32(sbc
, 0xd8)
1575 CASE_SET_REG32(csbc
, 0xdc)
1577 if (offset
>= 0x5c && offset
< 0xa0) {
1580 n
= (offset
- 0x58) >> 2;
1581 shift
= (offset
& 3) * 8;
1582 s
->scratch
[n
] &= ~(0xff << shift
);
1583 s
->scratch
[n
] |= (val
& 0xff) << shift
;
1585 BADF("Unhandled writeb 0x%x = 0x%x\n", offset
, val
);
1588 #undef CASE_SET_REG32
1591 static void lsi_mmio_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1593 LSIState
*s
= (LSIState
*)opaque
;
1595 lsi_reg_writeb(s
, addr
& 0xff, val
);
1598 static void lsi_mmio_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1600 LSIState
*s
= (LSIState
*)opaque
;
1603 lsi_reg_writeb(s
, addr
, val
& 0xff);
1604 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1607 static void lsi_mmio_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1609 LSIState
*s
= (LSIState
*)opaque
;
1612 lsi_reg_writeb(s
, addr
, val
& 0xff);
1613 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1614 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1615 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1618 static uint32_t lsi_mmio_readb(void *opaque
, target_phys_addr_t addr
)
1620 LSIState
*s
= (LSIState
*)opaque
;
1622 return lsi_reg_readb(s
, addr
& 0xff);
1625 static uint32_t lsi_mmio_readw(void *opaque
, target_phys_addr_t addr
)
1627 LSIState
*s
= (LSIState
*)opaque
;
1631 val
= lsi_reg_readb(s
, addr
);
1632 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1636 static uint32_t lsi_mmio_readl(void *opaque
, target_phys_addr_t addr
)
1638 LSIState
*s
= (LSIState
*)opaque
;
1641 val
= lsi_reg_readb(s
, addr
);
1642 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1643 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1644 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1648 static CPUReadMemoryFunc
*lsi_mmio_readfn
[3] = {
1654 static CPUWriteMemoryFunc
*lsi_mmio_writefn
[3] = {
1660 static void lsi_ram_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1662 LSIState
*s
= (LSIState
*)opaque
;
1667 newval
= s
->script_ram
[addr
>> 2];
1668 shift
= (addr
& 3) * 8;
1669 newval
&= ~(0xff << shift
);
1670 newval
|= val
<< shift
;
1671 s
->script_ram
[addr
>> 2] = newval
;
1674 static void lsi_ram_writew(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1676 LSIState
*s
= (LSIState
*)opaque
;
1680 newval
= s
->script_ram
[addr
>> 2];
1682 newval
= (newval
& 0xffff) | (val
<< 16);
1684 newval
= (newval
& 0xffff0000) | val
;
1686 s
->script_ram
[addr
>> 2] = newval
;
1690 static void lsi_ram_writel(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
1692 LSIState
*s
= (LSIState
*)opaque
;
1695 s
->script_ram
[addr
>> 2] = val
;
1698 static uint32_t lsi_ram_readb(void *opaque
, target_phys_addr_t addr
)
1700 LSIState
*s
= (LSIState
*)opaque
;
1704 val
= s
->script_ram
[addr
>> 2];
1705 val
>>= (addr
& 3) * 8;
1709 static uint32_t lsi_ram_readw(void *opaque
, target_phys_addr_t addr
)
1711 LSIState
*s
= (LSIState
*)opaque
;
1715 val
= s
->script_ram
[addr
>> 2];
1718 return le16_to_cpu(val
);
1721 static uint32_t lsi_ram_readl(void *opaque
, target_phys_addr_t addr
)
1723 LSIState
*s
= (LSIState
*)opaque
;
1726 return le32_to_cpu(s
->script_ram
[addr
>> 2]);
1729 static CPUReadMemoryFunc
*lsi_ram_readfn
[3] = {
1735 static CPUWriteMemoryFunc
*lsi_ram_writefn
[3] = {
1741 static uint32_t lsi_io_readb(void *opaque
, uint32_t addr
)
1743 LSIState
*s
= (LSIState
*)opaque
;
1744 return lsi_reg_readb(s
, addr
& 0xff);
1747 static uint32_t lsi_io_readw(void *opaque
, uint32_t addr
)
1749 LSIState
*s
= (LSIState
*)opaque
;
1752 val
= lsi_reg_readb(s
, addr
);
1753 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1757 static uint32_t lsi_io_readl(void *opaque
, uint32_t addr
)
1759 LSIState
*s
= (LSIState
*)opaque
;
1762 val
= lsi_reg_readb(s
, addr
);
1763 val
|= lsi_reg_readb(s
, addr
+ 1) << 8;
1764 val
|= lsi_reg_readb(s
, addr
+ 2) << 16;
1765 val
|= lsi_reg_readb(s
, addr
+ 3) << 24;
1769 static void lsi_io_writeb(void *opaque
, uint32_t addr
, uint32_t val
)
1771 LSIState
*s
= (LSIState
*)opaque
;
1772 lsi_reg_writeb(s
, addr
& 0xff, val
);
1775 static void lsi_io_writew(void *opaque
, uint32_t addr
, uint32_t val
)
1777 LSIState
*s
= (LSIState
*)opaque
;
1779 lsi_reg_writeb(s
, addr
, val
& 0xff);
1780 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1783 static void lsi_io_writel(void *opaque
, uint32_t addr
, uint32_t val
)
1785 LSIState
*s
= (LSIState
*)opaque
;
1787 lsi_reg_writeb(s
, addr
, val
& 0xff);
1788 lsi_reg_writeb(s
, addr
+ 1, (val
>> 8) & 0xff);
1789 lsi_reg_writeb(s
, addr
+ 2, (val
>> 16) & 0xff);
1790 lsi_reg_writeb(s
, addr
+ 3, (val
>> 24) & 0xff);
1793 static void lsi_io_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1794 uint32_t addr
, uint32_t size
, int type
)
1796 LSIState
*s
= (LSIState
*)pci_dev
;
1798 DPRINTF("Mapping IO at %08x\n", addr
);
1800 register_ioport_write(addr
, 256, 1, lsi_io_writeb
, s
);
1801 register_ioport_read(addr
, 256, 1, lsi_io_readb
, s
);
1802 register_ioport_write(addr
, 256, 2, lsi_io_writew
, s
);
1803 register_ioport_read(addr
, 256, 2, lsi_io_readw
, s
);
1804 register_ioport_write(addr
, 256, 4, lsi_io_writel
, s
);
1805 register_ioport_read(addr
, 256, 4, lsi_io_readl
, s
);
1808 static void lsi_ram_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1809 uint32_t addr
, uint32_t size
, int type
)
1811 LSIState
*s
= (LSIState
*)pci_dev
;
1813 DPRINTF("Mapping ram at %08x\n", addr
);
1814 s
->script_ram_base
= addr
;
1815 cpu_register_physical_memory(addr
+ 0, 0x2000, s
->ram_io_addr
);
1818 static void lsi_mmio_mapfunc(PCIDevice
*pci_dev
, int region_num
,
1819 uint32_t addr
, uint32_t size
, int type
)
1821 LSIState
*s
= (LSIState
*)pci_dev
;
1823 DPRINTF("Mapping registers at %08x\n", addr
);
1824 cpu_register_physical_memory(addr
+ 0, 0x400, s
->mmio_io_addr
);
1827 void lsi_scsi_attach(void *opaque
, BlockDriverState
*bd
, int id
)
1829 LSIState
*s
= (LSIState
*)opaque
;
1832 for (id
= 0; id
< LSI_MAX_DEVS
; id
++) {
1833 if (s
->scsi_dev
[id
] == NULL
)
1837 if (id
>= LSI_MAX_DEVS
) {
1838 BADF("Bad Device ID %d\n", id
);
1841 if (s
->scsi_dev
[id
]) {
1842 DPRINTF("Destroying device %d\n", id
);
1843 s
->scsi_dev
[id
]->destroy(s
->scsi_dev
[id
]);
1845 DPRINTF("Attaching block device %d\n", id
);
1846 s
->scsi_dev
[id
] = scsi_generic_init(bd
, 1, lsi_command_complete
, s
);
1847 if (s
->scsi_dev
[id
] == NULL
)
1848 s
->scsi_dev
[id
] = scsi_disk_init(bd
, 1, lsi_command_complete
, s
);
1849 bd
->devfn
= s
->pci_dev
.devfn
;
1852 int lsi_scsi_uninit(PCIDevice
*d
)
1854 LSIState
*s
= (LSIState
*) d
;
1856 cpu_unregister_io_memory(s
->mmio_io_addr
);
1857 cpu_unregister_io_memory(s
->ram_io_addr
);
1859 qemu_free(s
->queue
);
1864 void *lsi_scsi_init(PCIBus
*bus
, int devfn
)
1868 s
= (LSIState
*)pci_register_device(bus
, "LSI53C895A SCSI HBA",
1869 sizeof(*s
), devfn
, NULL
, NULL
);
1871 fprintf(stderr
, "lsi-scsi: Failed to register PCI device\n");
1875 s
->pci_dev
.config
[0x00] = 0x00;
1876 s
->pci_dev
.config
[0x01] = 0x10;
1877 s
->pci_dev
.config
[0x02] = 0x12;
1878 s
->pci_dev
.config
[0x03] = 0x00;
1879 s
->pci_dev
.config
[0x0b] = 0x01;
1880 s
->pci_dev
.config
[0x3d] = 0x01; /* interrupt pin 1 */
1882 s
->mmio_io_addr
= cpu_register_io_memory(0, lsi_mmio_readfn
,
1883 lsi_mmio_writefn
, s
);
1884 s
->ram_io_addr
= cpu_register_io_memory(0, lsi_ram_readfn
,
1885 lsi_ram_writefn
, s
);
1887 pci_register_io_region((struct PCIDevice
*)s
, 0, 256,
1888 PCI_ADDRESS_SPACE_IO
, lsi_io_mapfunc
);
1889 pci_register_io_region((struct PCIDevice
*)s
, 1, 0x400,
1890 PCI_ADDRESS_SPACE_MEM
, lsi_mmio_mapfunc
);
1891 pci_register_io_region((struct PCIDevice
*)s
, 2, 0x2000,
1892 PCI_ADDRESS_SPACE_MEM
, lsi_ram_mapfunc
);
1893 s
->queue
= qemu_malloc(sizeof(lsi_queue
));
1895 s
->active_commands
= 0;
1896 s
->pci_dev
.unregister
= lsi_scsi_uninit
;