blob: 302e897f5db8b0ab676e0b691624dc0a62e2d6ca [file] [log] [blame]
bellard6f7e9ae2005-03-13 09:43:36 +00001/*
bellard67e999b2006-09-03 16:09:07 +00002 * QEMU ESP/NCR53C9x emulation
ths5fafdf22007-09-16 21:08:06 +00003 *
pbrook4e9aec72006-03-11 16:29:14 +00004 * Copyright (c) 2005-2006 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard6f7e9ae2005-03-13 09:43:36 +00006 * 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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
blueswir15d20fa62008-04-09 16:32:48 +000024
Paul Brookcfb9de92009-05-14 22:35:07 +010025#include "sysbus.h"
pbrook87ecb682007-11-17 17:14:51 +000026#include "scsi-disk.h"
blueswir18b17de82008-03-02 08:48:47 +000027#include "scsi.h"
bellard6f7e9ae2005-03-13 09:43:36 +000028
29/* debug ESP card */
bellard2f275b82005-04-06 20:31:50 +000030//#define DEBUG_ESP
bellard6f7e9ae2005-03-13 09:43:36 +000031
bellard67e999b2006-09-03 16:09:07 +000032/*
blueswir15ad6bb92007-12-01 14:51:23 +000033 * On Sparc32, this is the ESP (NCR53C90) part of chip STP2000 (Master I/O),
34 * also produced as NCR89C100. See
bellard67e999b2006-09-03 16:09:07 +000035 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
36 * and
37 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR53C9X.txt
38 */
39
bellard6f7e9ae2005-03-13 09:43:36 +000040#ifdef DEBUG_ESP
Blue Swirl001faf32009-05-13 17:53:17 +000041#define DPRINTF(fmt, ...) \
42 do { printf("ESP: " fmt , ## __VA_ARGS__); } while (0)
bellard6f7e9ae2005-03-13 09:43:36 +000043#else
Blue Swirl001faf32009-05-13 17:53:17 +000044#define DPRINTF(fmt, ...) do {} while (0)
bellard6f7e9ae2005-03-13 09:43:36 +000045#endif
46
Blue Swirl001faf32009-05-13 17:53:17 +000047#define ESP_ERROR(fmt, ...) \
48 do { printf("ESP ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
blueswir18dea1dd2008-11-29 16:45:28 +000049
blueswir15aca8c32007-05-26 17:39:43 +000050#define ESP_REGS 16
blueswir18dea1dd2008-11-29 16:45:28 +000051#define TI_BUFSZ 16
bellard67e999b2006-09-03 16:09:07 +000052
pbrook4e9aec72006-03-11 16:29:14 +000053typedef struct ESPState ESPState;
bellard6f7e9ae2005-03-13 09:43:36 +000054
pbrook4e9aec72006-03-11 16:29:14 +000055struct ESPState {
Paul Brookcfb9de92009-05-14 22:35:07 +010056 SysBusDevice busdev;
blueswir15d20fa62008-04-09 16:32:48 +000057 uint32_t it_shift;
blueswir170c0de92007-05-27 16:36:10 +000058 qemu_irq irq;
blueswir15aca8c32007-05-26 17:39:43 +000059 uint8_t rregs[ESP_REGS];
60 uint8_t wregs[ESP_REGS];
bellard67e999b2006-09-03 16:09:07 +000061 int32_t ti_size;
bellard4f6200f2005-10-30 17:24:05 +000062 uint32_t ti_rptr, ti_wptr;
bellard4f6200f2005-10-30 17:24:05 +000063 uint8_t ti_buf[TI_BUFSZ];
blueswir122548762008-05-10 10:12:00 +000064 uint32_t sense;
65 uint32_t dma;
thse4bcb142007-12-02 04:51:10 +000066 SCSIDevice *scsi_dev[ESP_MAX_DEVS];
pbrook2e5d83b2006-05-25 23:58:51 +000067 SCSIDevice *current_dev;
pbrook9f149aa2006-06-03 14:19:19 +000068 uint8_t cmdbuf[TI_BUFSZ];
blueswir122548762008-05-10 10:12:00 +000069 uint32_t cmdlen;
70 uint32_t do_cmd;
pbrook4d611c92006-08-12 01:04:27 +000071
pbrook6787f5f2006-09-17 03:20:58 +000072 /* The amount of data left in the current DMA transfer. */
pbrook4d611c92006-08-12 01:04:27 +000073 uint32_t dma_left;
pbrook6787f5f2006-09-17 03:20:58 +000074 /* The size of the current DMA transfer. Zero if no transfer is in
75 progress. */
76 uint32_t dma_counter;
pbrooka917d382006-08-29 04:52:16 +000077 uint8_t *async_buf;
pbrook4d611c92006-08-12 01:04:27 +000078 uint32_t async_len;
blueswir18b17de82008-03-02 08:48:47 +000079
80 espdma_memory_read_write dma_memory_read;
81 espdma_memory_read_write dma_memory_write;
bellard67e999b2006-09-03 16:09:07 +000082 void *dma_opaque;
pbrook4e9aec72006-03-11 16:29:14 +000083};
bellard6f7e9ae2005-03-13 09:43:36 +000084
blueswir15ad6bb92007-12-01 14:51:23 +000085#define ESP_TCLO 0x0
86#define ESP_TCMID 0x1
87#define ESP_FIFO 0x2
88#define ESP_CMD 0x3
89#define ESP_RSTAT 0x4
90#define ESP_WBUSID 0x4
91#define ESP_RINTR 0x5
92#define ESP_WSEL 0x5
93#define ESP_RSEQ 0x6
94#define ESP_WSYNTP 0x6
95#define ESP_RFLAGS 0x7
96#define ESP_WSYNO 0x7
97#define ESP_CFG1 0x8
98#define ESP_RRES1 0x9
99#define ESP_WCCF 0x9
100#define ESP_RRES2 0xa
101#define ESP_WTEST 0xa
102#define ESP_CFG2 0xb
103#define ESP_CFG3 0xc
104#define ESP_RES3 0xd
105#define ESP_TCHI 0xe
106#define ESP_RES4 0xf
107
108#define CMD_DMA 0x80
109#define CMD_CMD 0x7f
110
111#define CMD_NOP 0x00
112#define CMD_FLUSH 0x01
113#define CMD_RESET 0x02
114#define CMD_BUSRESET 0x03
115#define CMD_TI 0x10
116#define CMD_ICCS 0x11
117#define CMD_MSGACC 0x12
118#define CMD_SATN 0x1a
119#define CMD_SELATN 0x42
120#define CMD_SELATNS 0x43
121#define CMD_ENSEL 0x44
122
bellard2f275b82005-04-06 20:31:50 +0000123#define STAT_DO 0x00
124#define STAT_DI 0x01
125#define STAT_CD 0x02
126#define STAT_ST 0x03
blueswir18dea1dd2008-11-29 16:45:28 +0000127#define STAT_MO 0x06
128#define STAT_MI 0x07
blueswir15ad6bb92007-12-01 14:51:23 +0000129#define STAT_PIO_MASK 0x06
bellard2f275b82005-04-06 20:31:50 +0000130
131#define STAT_TC 0x10
pbrook4d611c92006-08-12 01:04:27 +0000132#define STAT_PE 0x20
133#define STAT_GE 0x40
blueswir1c73f96f2008-04-24 17:20:25 +0000134#define STAT_INT 0x80
bellard2f275b82005-04-06 20:31:50 +0000135
blueswir18dea1dd2008-11-29 16:45:28 +0000136#define BUSID_DID 0x07
137
bellard2f275b82005-04-06 20:31:50 +0000138#define INTR_FC 0x08
139#define INTR_BS 0x10
140#define INTR_DC 0x20
bellard9e61bde2005-11-11 00:24:58 +0000141#define INTR_RST 0x80
bellard2f275b82005-04-06 20:31:50 +0000142
143#define SEQ_0 0x0
144#define SEQ_CD 0x4
145
blueswir15ad6bb92007-12-01 14:51:23 +0000146#define CFG1_RESREPT 0x40
147
blueswir15ad6bb92007-12-01 14:51:23 +0000148#define TCHI_FAS100A 0x4
149
blueswir1c73f96f2008-04-24 17:20:25 +0000150static void esp_raise_irq(ESPState *s)
151{
152 if (!(s->rregs[ESP_RSTAT] & STAT_INT)) {
153 s->rregs[ESP_RSTAT] |= STAT_INT;
154 qemu_irq_raise(s->irq);
155 }
156}
157
158static void esp_lower_irq(ESPState *s)
159{
160 if (s->rregs[ESP_RSTAT] & STAT_INT) {
161 s->rregs[ESP_RSTAT] &= ~STAT_INT;
162 qemu_irq_lower(s->irq);
163 }
164}
165
blueswir122548762008-05-10 10:12:00 +0000166static uint32_t get_cmd(ESPState *s, uint8_t *buf)
bellard2f275b82005-04-06 20:31:50 +0000167{
pbrooka917d382006-08-29 04:52:16 +0000168 uint32_t dmalen;
bellard2f275b82005-04-06 20:31:50 +0000169 int target;
170
blueswir18dea1dd2008-11-29 16:45:28 +0000171 target = s->wregs[ESP_WBUSID] & BUSID_DID;
bellard4f6200f2005-10-30 17:24:05 +0000172 if (s->dma) {
blueswir1fc4d65d2008-11-29 16:51:02 +0000173 dmalen = s->rregs[ESP_TCLO] | (s->rregs[ESP_TCMID] << 8);
blueswir18b17de82008-03-02 08:48:47 +0000174 s->dma_memory_read(s->dma_opaque, buf, dmalen);
bellard4f6200f2005-10-30 17:24:05 +0000175 } else {
blueswir1fc4d65d2008-11-29 16:51:02 +0000176 dmalen = s->ti_size;
177 memcpy(buf, s->ti_buf, dmalen);
blueswir1f930d072007-10-06 11:28:21 +0000178 buf[0] = 0;
bellard4f6200f2005-10-30 17:24:05 +0000179 }
blueswir1fc4d65d2008-11-29 16:51:02 +0000180 DPRINTF("get_cmd: len %d target %d\n", dmalen, target);
pbrook2e5d83b2006-05-25 23:58:51 +0000181
bellard2f275b82005-04-06 20:31:50 +0000182 s->ti_size = 0;
bellard4f6200f2005-10-30 17:24:05 +0000183 s->ti_rptr = 0;
184 s->ti_wptr = 0;
bellard2f275b82005-04-06 20:31:50 +0000185
pbrooka917d382006-08-29 04:52:16 +0000186 if (s->current_dev) {
187 /* Started a new command before the old one finished. Cancel it. */
ths8ccc2ac2007-12-10 02:58:34 +0000188 s->current_dev->cancel_io(s->current_dev, 0);
pbrooka917d382006-08-29 04:52:16 +0000189 s->async_len = 0;
190 }
191
thse4bcb142007-12-02 04:51:10 +0000192 if (target >= ESP_MAX_DEVS || !s->scsi_dev[target]) {
pbrook2e5d83b2006-05-25 23:58:51 +0000193 // No such drive
blueswir1c73f96f2008-04-24 17:20:25 +0000194 s->rregs[ESP_RSTAT] = 0;
blueswir15ad6bb92007-12-01 14:51:23 +0000195 s->rregs[ESP_RINTR] = INTR_DC;
196 s->rregs[ESP_RSEQ] = SEQ_0;
blueswir1c73f96f2008-04-24 17:20:25 +0000197 esp_raise_irq(s);
blueswir1f930d072007-10-06 11:28:21 +0000198 return 0;
bellard2f275b82005-04-06 20:31:50 +0000199 }
pbrook2e5d83b2006-05-25 23:58:51 +0000200 s->current_dev = s->scsi_dev[target];
pbrook9f149aa2006-06-03 14:19:19 +0000201 return dmalen;
202}
203
204static void do_cmd(ESPState *s, uint8_t *buf)
205{
206 int32_t datalen;
207 int lun;
208
209 DPRINTF("do_cmd: busid 0x%x\n", buf[0]);
210 lun = buf[0] & 7;
ths8ccc2ac2007-12-10 02:58:34 +0000211 datalen = s->current_dev->send_command(s->current_dev, 0, &buf[1], lun);
bellard67e999b2006-09-03 16:09:07 +0000212 s->ti_size = datalen;
213 if (datalen != 0) {
blueswir1c73f96f2008-04-24 17:20:25 +0000214 s->rregs[ESP_RSTAT] = STAT_TC;
pbrooka917d382006-08-29 04:52:16 +0000215 s->dma_left = 0;
pbrook6787f5f2006-09-17 03:20:58 +0000216 s->dma_counter = 0;
pbrook2e5d83b2006-05-25 23:58:51 +0000217 if (datalen > 0) {
blueswir15ad6bb92007-12-01 14:51:23 +0000218 s->rregs[ESP_RSTAT] |= STAT_DI;
ths8ccc2ac2007-12-10 02:58:34 +0000219 s->current_dev->read_data(s->current_dev, 0);
pbrook2e5d83b2006-05-25 23:58:51 +0000220 } else {
blueswir15ad6bb92007-12-01 14:51:23 +0000221 s->rregs[ESP_RSTAT] |= STAT_DO;
ths8ccc2ac2007-12-10 02:58:34 +0000222 s->current_dev->write_data(s->current_dev, 0);
bellardb9788fc2005-12-05 20:30:36 +0000223 }
bellard2f275b82005-04-06 20:31:50 +0000224 }
blueswir15ad6bb92007-12-01 14:51:23 +0000225 s->rregs[ESP_RINTR] = INTR_BS | INTR_FC;
226 s->rregs[ESP_RSEQ] = SEQ_CD;
blueswir1c73f96f2008-04-24 17:20:25 +0000227 esp_raise_irq(s);
bellard2f275b82005-04-06 20:31:50 +0000228}
229
pbrook9f149aa2006-06-03 14:19:19 +0000230static void handle_satn(ESPState *s)
231{
232 uint8_t buf[32];
233 int len;
234
235 len = get_cmd(s, buf);
236 if (len)
237 do_cmd(s, buf);
238}
239
240static void handle_satn_stop(ESPState *s)
241{
242 s->cmdlen = get_cmd(s, s->cmdbuf);
243 if (s->cmdlen) {
244 DPRINTF("Set ATN & Stop: cmdlen %d\n", s->cmdlen);
245 s->do_cmd = 1;
blueswir1c73f96f2008-04-24 17:20:25 +0000246 s->rregs[ESP_RSTAT] = STAT_TC | STAT_CD;
blueswir15ad6bb92007-12-01 14:51:23 +0000247 s->rregs[ESP_RINTR] = INTR_BS | INTR_FC;
248 s->rregs[ESP_RSEQ] = SEQ_CD;
blueswir1c73f96f2008-04-24 17:20:25 +0000249 esp_raise_irq(s);
pbrook9f149aa2006-06-03 14:19:19 +0000250 }
251}
252
pbrook0fc5c152006-05-26 21:53:41 +0000253static void write_response(ESPState *s)
bellard2f275b82005-04-06 20:31:50 +0000254{
pbrook0fc5c152006-05-26 21:53:41 +0000255 DPRINTF("Transfer status (sense=%d)\n", s->sense);
256 s->ti_buf[0] = s->sense;
257 s->ti_buf[1] = 0;
bellard4f6200f2005-10-30 17:24:05 +0000258 if (s->dma) {
blueswir18b17de82008-03-02 08:48:47 +0000259 s->dma_memory_write(s->dma_opaque, s->ti_buf, 2);
blueswir1c73f96f2008-04-24 17:20:25 +0000260 s->rregs[ESP_RSTAT] = STAT_TC | STAT_ST;
blueswir15ad6bb92007-12-01 14:51:23 +0000261 s->rregs[ESP_RINTR] = INTR_BS | INTR_FC;
262 s->rregs[ESP_RSEQ] = SEQ_CD;
bellard4f6200f2005-10-30 17:24:05 +0000263 } else {
blueswir1f930d072007-10-06 11:28:21 +0000264 s->ti_size = 2;
265 s->ti_rptr = 0;
266 s->ti_wptr = 0;
blueswir15ad6bb92007-12-01 14:51:23 +0000267 s->rregs[ESP_RFLAGS] = 2;
bellard4f6200f2005-10-30 17:24:05 +0000268 }
blueswir1c73f96f2008-04-24 17:20:25 +0000269 esp_raise_irq(s);
bellard2f275b82005-04-06 20:31:50 +0000270}
bellard4f6200f2005-10-30 17:24:05 +0000271
pbrooka917d382006-08-29 04:52:16 +0000272static void esp_dma_done(ESPState *s)
273{
blueswir1c73f96f2008-04-24 17:20:25 +0000274 s->rregs[ESP_RSTAT] |= STAT_TC;
blueswir15ad6bb92007-12-01 14:51:23 +0000275 s->rregs[ESP_RINTR] = INTR_BS;
276 s->rregs[ESP_RSEQ] = 0;
277 s->rregs[ESP_RFLAGS] = 0;
278 s->rregs[ESP_TCLO] = 0;
279 s->rregs[ESP_TCMID] = 0;
blueswir1c73f96f2008-04-24 17:20:25 +0000280 esp_raise_irq(s);
pbrooka917d382006-08-29 04:52:16 +0000281}
282
pbrook4d611c92006-08-12 01:04:27 +0000283static void esp_do_dma(ESPState *s)
284{
bellard67e999b2006-09-03 16:09:07 +0000285 uint32_t len;
pbrook4d611c92006-08-12 01:04:27 +0000286 int to_device;
pbrooka917d382006-08-29 04:52:16 +0000287
bellard67e999b2006-09-03 16:09:07 +0000288 to_device = (s->ti_size < 0);
pbrooka917d382006-08-29 04:52:16 +0000289 len = s->dma_left;
pbrook4d611c92006-08-12 01:04:27 +0000290 if (s->do_cmd) {
pbrook4d611c92006-08-12 01:04:27 +0000291 DPRINTF("command len %d + %d\n", s->cmdlen, len);
blueswir18b17de82008-03-02 08:48:47 +0000292 s->dma_memory_read(s->dma_opaque, &s->cmdbuf[s->cmdlen], len);
pbrook4d611c92006-08-12 01:04:27 +0000293 s->ti_size = 0;
294 s->cmdlen = 0;
295 s->do_cmd = 0;
296 do_cmd(s, s->cmdbuf);
297 return;
pbrooka917d382006-08-29 04:52:16 +0000298 }
299 if (s->async_len == 0) {
300 /* Defer until data is available. */
301 return;
302 }
303 if (len > s->async_len) {
304 len = s->async_len;
305 }
306 if (to_device) {
blueswir18b17de82008-03-02 08:48:47 +0000307 s->dma_memory_read(s->dma_opaque, s->async_buf, len);
pbrook4d611c92006-08-12 01:04:27 +0000308 } else {
blueswir18b17de82008-03-02 08:48:47 +0000309 s->dma_memory_write(s->dma_opaque, s->async_buf, len);
pbrooka917d382006-08-29 04:52:16 +0000310 }
pbrooka917d382006-08-29 04:52:16 +0000311 s->dma_left -= len;
312 s->async_buf += len;
313 s->async_len -= len;
pbrook6787f5f2006-09-17 03:20:58 +0000314 if (to_device)
315 s->ti_size += len;
316 else
317 s->ti_size -= len;
pbrooka917d382006-08-29 04:52:16 +0000318 if (s->async_len == 0) {
pbrook4d611c92006-08-12 01:04:27 +0000319 if (to_device) {
bellard67e999b2006-09-03 16:09:07 +0000320 // ti_size is negative
ths8ccc2ac2007-12-10 02:58:34 +0000321 s->current_dev->write_data(s->current_dev, 0);
pbrook4d611c92006-08-12 01:04:27 +0000322 } else {
ths8ccc2ac2007-12-10 02:58:34 +0000323 s->current_dev->read_data(s->current_dev, 0);
pbrook6787f5f2006-09-17 03:20:58 +0000324 /* If there is still data to be read from the device then
blueswir18dea1dd2008-11-29 16:45:28 +0000325 complete the DMA operation immediately. Otherwise defer
pbrook6787f5f2006-09-17 03:20:58 +0000326 until the scsi layer has completed. */
327 if (s->dma_left == 0 && s->ti_size > 0) {
328 esp_dma_done(s);
329 }
pbrook4d611c92006-08-12 01:04:27 +0000330 }
pbrook6787f5f2006-09-17 03:20:58 +0000331 } else {
332 /* Partially filled a scsi buffer. Complete immediately. */
pbrooka917d382006-08-29 04:52:16 +0000333 esp_dma_done(s);
334 }
pbrook4d611c92006-08-12 01:04:27 +0000335}
336
pbrooka917d382006-08-29 04:52:16 +0000337static void esp_command_complete(void *opaque, int reason, uint32_t tag,
338 uint32_t arg)
pbrook2e5d83b2006-05-25 23:58:51 +0000339{
340 ESPState *s = (ESPState *)opaque;
341
pbrook4d611c92006-08-12 01:04:27 +0000342 if (reason == SCSI_REASON_DONE) {
343 DPRINTF("SCSI Command complete\n");
344 if (s->ti_size != 0)
345 DPRINTF("SCSI command completed unexpectedly\n");
346 s->ti_size = 0;
pbrooka917d382006-08-29 04:52:16 +0000347 s->dma_left = 0;
348 s->async_len = 0;
349 if (arg)
pbrook4d611c92006-08-12 01:04:27 +0000350 DPRINTF("Command failed\n");
pbrooka917d382006-08-29 04:52:16 +0000351 s->sense = arg;
blueswir15ad6bb92007-12-01 14:51:23 +0000352 s->rregs[ESP_RSTAT] = STAT_ST;
pbrooka917d382006-08-29 04:52:16 +0000353 esp_dma_done(s);
354 s->current_dev = NULL;
pbrook4d611c92006-08-12 01:04:27 +0000355 } else {
356 DPRINTF("transfer %d/%d\n", s->dma_left, s->ti_size);
pbrooka917d382006-08-29 04:52:16 +0000357 s->async_len = arg;
ths8ccc2ac2007-12-10 02:58:34 +0000358 s->async_buf = s->current_dev->get_buf(s->current_dev, 0);
pbrook6787f5f2006-09-17 03:20:58 +0000359 if (s->dma_left) {
pbrooka917d382006-08-29 04:52:16 +0000360 esp_do_dma(s);
pbrook6787f5f2006-09-17 03:20:58 +0000361 } else if (s->dma_counter != 0 && s->ti_size <= 0) {
362 /* If this was the last part of a DMA transfer then the
363 completion interrupt is deferred to here. */
364 esp_dma_done(s);
365 }
pbrook4d611c92006-08-12 01:04:27 +0000366 }
pbrook2e5d83b2006-05-25 23:58:51 +0000367}
368
bellard2f275b82005-04-06 20:31:50 +0000369static void handle_ti(ESPState *s)
370{
pbrook4d611c92006-08-12 01:04:27 +0000371 uint32_t dmalen, minlen;
bellard2f275b82005-04-06 20:31:50 +0000372
blueswir15ad6bb92007-12-01 14:51:23 +0000373 dmalen = s->rregs[ESP_TCLO] | (s->rregs[ESP_TCMID] << 8);
pbrookdb592032006-05-21 12:46:31 +0000374 if (dmalen==0) {
375 dmalen=0x10000;
376 }
pbrook6787f5f2006-09-17 03:20:58 +0000377 s->dma_counter = dmalen;
pbrookdb592032006-05-21 12:46:31 +0000378
pbrook9f149aa2006-06-03 14:19:19 +0000379 if (s->do_cmd)
380 minlen = (dmalen < 32) ? dmalen : 32;
bellard67e999b2006-09-03 16:09:07 +0000381 else if (s->ti_size < 0)
382 minlen = (dmalen < -s->ti_size) ? dmalen : -s->ti_size;
pbrook9f149aa2006-06-03 14:19:19 +0000383 else
384 minlen = (dmalen < s->ti_size) ? dmalen : s->ti_size;
pbrookdb592032006-05-21 12:46:31 +0000385 DPRINTF("Transfer Information len %d\n", minlen);
bellard4f6200f2005-10-30 17:24:05 +0000386 if (s->dma) {
pbrook4d611c92006-08-12 01:04:27 +0000387 s->dma_left = minlen;
blueswir15ad6bb92007-12-01 14:51:23 +0000388 s->rregs[ESP_RSTAT] &= ~STAT_TC;
pbrook4d611c92006-08-12 01:04:27 +0000389 esp_do_dma(s);
pbrook9f149aa2006-06-03 14:19:19 +0000390 } else if (s->do_cmd) {
391 DPRINTF("command len %d\n", s->cmdlen);
392 s->ti_size = 0;
393 s->cmdlen = 0;
394 s->do_cmd = 0;
395 do_cmd(s, s->cmdbuf);
396 return;
397 }
bellard2f275b82005-04-06 20:31:50 +0000398}
399
blueswir15aca8c32007-05-26 17:39:43 +0000400static void esp_reset(void *opaque)
bellard6f7e9ae2005-03-13 09:43:36 +0000401{
402 ESPState *s = opaque;
bellard67e999b2006-09-03 16:09:07 +0000403
blueswir1c73f96f2008-04-24 17:20:25 +0000404 esp_lower_irq(s);
405
blueswir15aca8c32007-05-26 17:39:43 +0000406 memset(s->rregs, 0, ESP_REGS);
407 memset(s->wregs, 0, ESP_REGS);
blueswir15ad6bb92007-12-01 14:51:23 +0000408 s->rregs[ESP_TCHI] = TCHI_FAS100A; // Indicate fas100a
pbrook4e9aec72006-03-11 16:29:14 +0000409 s->ti_size = 0;
410 s->ti_rptr = 0;
411 s->ti_wptr = 0;
pbrook4e9aec72006-03-11 16:29:14 +0000412 s->dma = 0;
pbrook9f149aa2006-06-03 14:19:19 +0000413 s->do_cmd = 0;
blueswir18dea1dd2008-11-29 16:45:28 +0000414
415 s->rregs[ESP_CFG1] = 7;
bellard6f7e9ae2005-03-13 09:43:36 +0000416}
417
blueswir12d069ba2007-08-16 19:56:27 +0000418static void parent_esp_reset(void *opaque, int irq, int level)
419{
420 if (level)
421 esp_reset(opaque);
422}
423
bellard6f7e9ae2005-03-13 09:43:36 +0000424static uint32_t esp_mem_readb(void *opaque, target_phys_addr_t addr)
425{
426 ESPState *s = opaque;
427 uint32_t saddr;
428
blueswir1e64d7d52008-12-02 17:47:02 +0000429 saddr = addr >> s->it_shift;
bellard9e61bde2005-11-11 00:24:58 +0000430 DPRINTF("read reg[%d]: 0x%2.2x\n", saddr, s->rregs[saddr]);
bellard6f7e9ae2005-03-13 09:43:36 +0000431 switch (saddr) {
blueswir15ad6bb92007-12-01 14:51:23 +0000432 case ESP_FIFO:
blueswir1f930d072007-10-06 11:28:21 +0000433 if (s->ti_size > 0) {
434 s->ti_size--;
blueswir15ad6bb92007-12-01 14:51:23 +0000435 if ((s->rregs[ESP_RSTAT] & STAT_PIO_MASK) == 0) {
blueswir18dea1dd2008-11-29 16:45:28 +0000436 /* Data out. */
437 ESP_ERROR("PIO data read not implemented\n");
blueswir15ad6bb92007-12-01 14:51:23 +0000438 s->rregs[ESP_FIFO] = 0;
pbrook2e5d83b2006-05-25 23:58:51 +0000439 } else {
blueswir15ad6bb92007-12-01 14:51:23 +0000440 s->rregs[ESP_FIFO] = s->ti_buf[s->ti_rptr++];
pbrook2e5d83b2006-05-25 23:58:51 +0000441 }
blueswir1c73f96f2008-04-24 17:20:25 +0000442 esp_raise_irq(s);
blueswir1f930d072007-10-06 11:28:21 +0000443 }
444 if (s->ti_size == 0) {
bellard4f6200f2005-10-30 17:24:05 +0000445 s->ti_rptr = 0;
446 s->ti_wptr = 0;
447 }
blueswir1f930d072007-10-06 11:28:21 +0000448 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000449 case ESP_RINTR:
pbrook4d611c92006-08-12 01:04:27 +0000450 // Clear interrupt/error status bits
blueswir1c73f96f2008-04-24 17:20:25 +0000451 s->rregs[ESP_RSTAT] &= ~(STAT_GE | STAT_PE);
452 esp_lower_irq(s);
bellard9e61bde2005-11-11 00:24:58 +0000453 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000454 default:
blueswir1f930d072007-10-06 11:28:21 +0000455 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000456 }
bellard2f275b82005-04-06 20:31:50 +0000457 return s->rregs[saddr];
bellard6f7e9ae2005-03-13 09:43:36 +0000458}
459
460static void esp_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
461{
462 ESPState *s = opaque;
463 uint32_t saddr;
464
blueswir1e64d7d52008-12-02 17:47:02 +0000465 saddr = addr >> s->it_shift;
blueswir15ad6bb92007-12-01 14:51:23 +0000466 DPRINTF("write reg[%d]: 0x%2.2x -> 0x%2.2x\n", saddr, s->wregs[saddr],
467 val);
bellard6f7e9ae2005-03-13 09:43:36 +0000468 switch (saddr) {
blueswir15ad6bb92007-12-01 14:51:23 +0000469 case ESP_TCLO:
470 case ESP_TCMID:
471 s->rregs[ESP_RSTAT] &= ~STAT_TC;
bellard4f6200f2005-10-30 17:24:05 +0000472 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000473 case ESP_FIFO:
pbrook9f149aa2006-06-03 14:19:19 +0000474 if (s->do_cmd) {
475 s->cmdbuf[s->cmdlen++] = val & 0xff;
blueswir18dea1dd2008-11-29 16:45:28 +0000476 } else if (s->ti_size == TI_BUFSZ - 1) {
477 ESP_ERROR("fifo overrun\n");
pbrook2e5d83b2006-05-25 23:58:51 +0000478 } else {
479 s->ti_size++;
480 s->ti_buf[s->ti_wptr++] = val & 0xff;
481 }
blueswir1f930d072007-10-06 11:28:21 +0000482 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000483 case ESP_CMD:
bellard4f6200f2005-10-30 17:24:05 +0000484 s->rregs[saddr] = val;
blueswir15ad6bb92007-12-01 14:51:23 +0000485 if (val & CMD_DMA) {
blueswir1f930d072007-10-06 11:28:21 +0000486 s->dma = 1;
pbrook6787f5f2006-09-17 03:20:58 +0000487 /* Reload DMA counter. */
blueswir15ad6bb92007-12-01 14:51:23 +0000488 s->rregs[ESP_TCLO] = s->wregs[ESP_TCLO];
489 s->rregs[ESP_TCMID] = s->wregs[ESP_TCMID];
blueswir1f930d072007-10-06 11:28:21 +0000490 } else {
491 s->dma = 0;
492 }
blueswir15ad6bb92007-12-01 14:51:23 +0000493 switch(val & CMD_CMD) {
494 case CMD_NOP:
blueswir1f930d072007-10-06 11:28:21 +0000495 DPRINTF("NOP (%2.2x)\n", val);
496 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000497 case CMD_FLUSH:
blueswir1f930d072007-10-06 11:28:21 +0000498 DPRINTF("Flush FIFO (%2.2x)\n", val);
bellard9e61bde2005-11-11 00:24:58 +0000499 //s->ti_size = 0;
blueswir15ad6bb92007-12-01 14:51:23 +0000500 s->rregs[ESP_RINTR] = INTR_FC;
501 s->rregs[ESP_RSEQ] = 0;
blueswir1a214c592008-06-25 19:59:53 +0000502 s->rregs[ESP_RFLAGS] = 0;
blueswir1f930d072007-10-06 11:28:21 +0000503 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000504 case CMD_RESET:
blueswir1f930d072007-10-06 11:28:21 +0000505 DPRINTF("Chip reset (%2.2x)\n", val);
506 esp_reset(s);
507 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000508 case CMD_BUSRESET:
blueswir1f930d072007-10-06 11:28:21 +0000509 DPRINTF("Bus reset (%2.2x)\n", val);
blueswir15ad6bb92007-12-01 14:51:23 +0000510 s->rregs[ESP_RINTR] = INTR_RST;
511 if (!(s->wregs[ESP_CFG1] & CFG1_RESREPT)) {
blueswir1c73f96f2008-04-24 17:20:25 +0000512 esp_raise_irq(s);
bellard9e61bde2005-11-11 00:24:58 +0000513 }
blueswir1f930d072007-10-06 11:28:21 +0000514 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000515 case CMD_TI:
blueswir1f930d072007-10-06 11:28:21 +0000516 handle_ti(s);
517 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000518 case CMD_ICCS:
blueswir1f930d072007-10-06 11:28:21 +0000519 DPRINTF("Initiator Command Complete Sequence (%2.2x)\n", val);
520 write_response(s);
blueswir14bf58012008-11-30 10:24:13 +0000521 s->rregs[ESP_RINTR] = INTR_FC;
522 s->rregs[ESP_RSTAT] |= STAT_MI;
blueswir1f930d072007-10-06 11:28:21 +0000523 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000524 case CMD_MSGACC:
blueswir1f930d072007-10-06 11:28:21 +0000525 DPRINTF("Message Accepted (%2.2x)\n", val);
526 write_response(s);
blueswir15ad6bb92007-12-01 14:51:23 +0000527 s->rregs[ESP_RINTR] = INTR_DC;
528 s->rregs[ESP_RSEQ] = 0;
blueswir1f930d072007-10-06 11:28:21 +0000529 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000530 case CMD_SATN:
blueswir1f930d072007-10-06 11:28:21 +0000531 DPRINTF("Set ATN (%2.2x)\n", val);
532 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000533 case CMD_SELATN:
blueswir1f930d072007-10-06 11:28:21 +0000534 DPRINTF("Set ATN (%2.2x)\n", val);
535 handle_satn(s);
536 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000537 case CMD_SELATNS:
blueswir1f930d072007-10-06 11:28:21 +0000538 DPRINTF("Set ATN & stop (%2.2x)\n", val);
539 handle_satn_stop(s);
540 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000541 case CMD_ENSEL:
blueswir174ec6042007-08-11 07:58:41 +0000542 DPRINTF("Enable selection (%2.2x)\n", val);
blueswir1e3926832008-11-29 16:51:42 +0000543 s->rregs[ESP_RINTR] = 0;
blueswir174ec6042007-08-11 07:58:41 +0000544 break;
blueswir1f930d072007-10-06 11:28:21 +0000545 default:
blueswir18dea1dd2008-11-29 16:45:28 +0000546 ESP_ERROR("Unhandled ESP command (%2.2x)\n", val);
blueswir1f930d072007-10-06 11:28:21 +0000547 break;
548 }
549 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000550 case ESP_WBUSID ... ESP_WSYNO:
blueswir1f930d072007-10-06 11:28:21 +0000551 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000552 case ESP_CFG1:
bellard4f6200f2005-10-30 17:24:05 +0000553 s->rregs[saddr] = val;
554 break;
blueswir15ad6bb92007-12-01 14:51:23 +0000555 case ESP_WCCF ... ESP_WTEST:
bellard4f6200f2005-10-30 17:24:05 +0000556 break;
blueswir1b44c08f2008-11-29 16:48:29 +0000557 case ESP_CFG2 ... ESP_RES4:
bellard4f6200f2005-10-30 17:24:05 +0000558 s->rregs[saddr] = val;
559 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000560 default:
blueswir18dea1dd2008-11-29 16:45:28 +0000561 ESP_ERROR("invalid write of 0x%02x at [0x%x]\n", val, saddr);
562 return;
bellard6f7e9ae2005-03-13 09:43:36 +0000563 }
bellard2f275b82005-04-06 20:31:50 +0000564 s->wregs[saddr] = val;
bellard6f7e9ae2005-03-13 09:43:36 +0000565}
566
567static CPUReadMemoryFunc *esp_mem_read[3] = {
568 esp_mem_readb,
blueswir17c560452008-01-01 17:06:38 +0000569 NULL,
570 NULL,
bellard6f7e9ae2005-03-13 09:43:36 +0000571};
572
573static CPUWriteMemoryFunc *esp_mem_write[3] = {
574 esp_mem_writeb,
blueswir17c560452008-01-01 17:06:38 +0000575 NULL,
blueswir1daa41b02008-10-02 18:07:56 +0000576 esp_mem_writeb,
bellard6f7e9ae2005-03-13 09:43:36 +0000577};
578
bellard6f7e9ae2005-03-13 09:43:36 +0000579static void esp_save(QEMUFile *f, void *opaque)
580{
581 ESPState *s = opaque;
bellard2f275b82005-04-06 20:31:50 +0000582
blueswir15aca8c32007-05-26 17:39:43 +0000583 qemu_put_buffer(f, s->rregs, ESP_REGS);
584 qemu_put_buffer(f, s->wregs, ESP_REGS);
blueswir1b6c4f712008-10-02 19:14:17 +0000585 qemu_put_sbe32s(f, &s->ti_size);
bellard4f6200f2005-10-30 17:24:05 +0000586 qemu_put_be32s(f, &s->ti_rptr);
587 qemu_put_be32s(f, &s->ti_wptr);
bellard4f6200f2005-10-30 17:24:05 +0000588 qemu_put_buffer(f, s->ti_buf, TI_BUFSZ);
blueswir15425a212007-04-13 19:24:07 +0000589 qemu_put_be32s(f, &s->sense);
bellard4f6200f2005-10-30 17:24:05 +0000590 qemu_put_be32s(f, &s->dma);
blueswir15425a212007-04-13 19:24:07 +0000591 qemu_put_buffer(f, s->cmdbuf, TI_BUFSZ);
592 qemu_put_be32s(f, &s->cmdlen);
593 qemu_put_be32s(f, &s->do_cmd);
594 qemu_put_be32s(f, &s->dma_left);
595 // There should be no transfers in progress, so dma_counter is not saved
bellard6f7e9ae2005-03-13 09:43:36 +0000596}
597
598static int esp_load(QEMUFile *f, void *opaque, int version_id)
599{
600 ESPState *s = opaque;
ths3b46e622007-09-17 08:09:54 +0000601
blueswir15425a212007-04-13 19:24:07 +0000602 if (version_id != 3)
603 return -EINVAL; // Cannot emulate 2
bellard6f7e9ae2005-03-13 09:43:36 +0000604
blueswir15aca8c32007-05-26 17:39:43 +0000605 qemu_get_buffer(f, s->rregs, ESP_REGS);
606 qemu_get_buffer(f, s->wregs, ESP_REGS);
blueswir1b6c4f712008-10-02 19:14:17 +0000607 qemu_get_sbe32s(f, &s->ti_size);
bellard4f6200f2005-10-30 17:24:05 +0000608 qemu_get_be32s(f, &s->ti_rptr);
609 qemu_get_be32s(f, &s->ti_wptr);
bellard4f6200f2005-10-30 17:24:05 +0000610 qemu_get_buffer(f, s->ti_buf, TI_BUFSZ);
blueswir15425a212007-04-13 19:24:07 +0000611 qemu_get_be32s(f, &s->sense);
bellard4f6200f2005-10-30 17:24:05 +0000612 qemu_get_be32s(f, &s->dma);
blueswir15425a212007-04-13 19:24:07 +0000613 qemu_get_buffer(f, s->cmdbuf, TI_BUFSZ);
614 qemu_get_be32s(f, &s->cmdlen);
615 qemu_get_be32s(f, &s->do_cmd);
616 qemu_get_be32s(f, &s->dma_left);
bellard2f275b82005-04-06 20:31:50 +0000617
bellard6f7e9ae2005-03-13 09:43:36 +0000618 return 0;
619}
620
Paul Brookcfb9de92009-05-14 22:35:07 +0100621static void esp_scsi_attach(DeviceState *host, BlockDriverState *bd, int id)
thsfa1fb142006-12-24 17:12:43 +0000622{
Paul Brookcfb9de92009-05-14 22:35:07 +0100623 ESPState *s = FROM_SYSBUS(ESPState, sysbus_from_qdev(host));
thsfa1fb142006-12-24 17:12:43 +0000624
625 if (id < 0) {
626 for (id = 0; id < ESP_MAX_DEVS; id++) {
blueswir18dea1dd2008-11-29 16:45:28 +0000627 if (id == (s->rregs[ESP_CFG1] & 0x7))
628 continue;
thsfa1fb142006-12-24 17:12:43 +0000629 if (s->scsi_dev[id] == NULL)
630 break;
631 }
632 }
633 if (id >= ESP_MAX_DEVS) {
634 DPRINTF("Bad Device ID %d\n", id);
635 return;
636 }
637 if (s->scsi_dev[id]) {
638 DPRINTF("Destroying device %d\n", id);
ths8ccc2ac2007-12-10 02:58:34 +0000639 s->scsi_dev[id]->destroy(s->scsi_dev[id]);
thsfa1fb142006-12-24 17:12:43 +0000640 }
641 DPRINTF("Attaching block device %d\n", id);
642 /* Command queueing is not implemented. */
ths985a03b2007-12-24 16:10:43 +0000643 s->scsi_dev[id] = scsi_generic_init(bd, 0, esp_command_complete, s);
644 if (s->scsi_dev[id] == NULL)
645 s->scsi_dev[id] = scsi_disk_init(bd, 0, esp_command_complete, s);
thsfa1fb142006-12-24 17:12:43 +0000646}
647
Paul Brookcfb9de92009-05-14 22:35:07 +0100648void esp_init(target_phys_addr_t espaddr, int it_shift,
649 espdma_memory_read_write dma_memory_read,
650 espdma_memory_read_write dma_memory_write,
651 void *dma_opaque, qemu_irq irq, qemu_irq *reset)
bellard6f7e9ae2005-03-13 09:43:36 +0000652{
Paul Brookcfb9de92009-05-14 22:35:07 +0100653 DeviceState *dev;
654 SysBusDevice *s;
655
656 dev = qdev_create(NULL, "esp");
657 qdev_set_prop_ptr(dev, "dma_memory_read", dma_memory_read);
658 qdev_set_prop_ptr(dev, "dma_memory_write", dma_memory_write);
659 qdev_set_prop_ptr(dev, "dma_opaque", dma_opaque);
660 qdev_set_prop_int(dev, "it_shift", it_shift);
661 qdev_init(dev);
662 s = sysbus_from_qdev(dev);
663 sysbus_connect_irq(s, 0, irq);
664 sysbus_mmio_map(s, 0, espaddr);
665}
666
667static void esp_init1(SysBusDevice *dev)
668{
669 ESPState *s = FROM_SYSBUS(ESPState, dev);
bellard67e999b2006-09-03 16:09:07 +0000670 int esp_io_memory;
bellard6f7e9ae2005-03-13 09:43:36 +0000671
Paul Brookcfb9de92009-05-14 22:35:07 +0100672 sysbus_init_irq(dev, &s->irq);
673 s->it_shift = qdev_get_prop_int(&dev->qdev, "it_shift", -1);
674 assert(s->it_shift != -1);
675 s->dma_memory_read = qdev_get_prop_ptr(&dev->qdev, "dma_memory_read");
676 s->dma_memory_write = qdev_get_prop_ptr(&dev->qdev, "dma_memory_write");
677 s->dma_opaque = qdev_get_prop_ptr(&dev->qdev, "dma_opaque");
bellard6f7e9ae2005-03-13 09:43:36 +0000678
679 esp_io_memory = cpu_register_io_memory(0, esp_mem_read, esp_mem_write, s);
Paul Brookcfb9de92009-05-14 22:35:07 +0100680 sysbus_init_mmio(dev, ESP_REGS << s->it_shift, esp_io_memory);
bellard6f7e9ae2005-03-13 09:43:36 +0000681
bellard6f7e9ae2005-03-13 09:43:36 +0000682 esp_reset(s);
683
Paul Brookcfb9de92009-05-14 22:35:07 +0100684 register_savevm("esp", -1, 3, esp_save, esp_load, s);
bellard6f7e9ae2005-03-13 09:43:36 +0000685 qemu_register_reset(esp_reset, s);
bellard6f7e9ae2005-03-13 09:43:36 +0000686
Paul Brookcfb9de92009-05-14 22:35:07 +0100687 qdev_init_irq_sink(&dev->qdev, parent_esp_reset, 1);
blueswir12d069ba2007-08-16 19:56:27 +0000688
Paul Brookcfb9de92009-05-14 22:35:07 +0100689 scsi_bus_new(&dev->qdev, esp_scsi_attach);
bellard67e999b2006-09-03 16:09:07 +0000690}
Paul Brookcfb9de92009-05-14 22:35:07 +0100691
692static void esp_register_devices(void)
693{
694 sysbus_register_dev("esp", sizeof(ESPState), esp_init1);
695}
696
697device_init(esp_register_devices)