]> Git Repo - qemu.git/blame - hw/esp.c
SMI enable bit support
[qemu.git] / hw / esp.c
CommitLineData
6f7e9aec 1/*
67e999be 2 * QEMU ESP/NCR53C9x emulation
6f7e9aec 3 *
4e9aec74 4 * Copyright (c) 2005-2006 Fabrice Bellard
6f7e9aec
FB
5 *
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:
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 */
24#include "vl.h"
25
26/* debug ESP card */
2f275b8f 27//#define DEBUG_ESP
6f7e9aec 28
67e999be
FB
29/*
30 * On Sparc32, this is the ESP (NCR53C90) part of chip STP2000 (Master I/O), also
31 * produced as NCR89C100. See
32 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
33 * and
34 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR53C9X.txt
35 */
36
6f7e9aec
FB
37#ifdef DEBUG_ESP
38#define DPRINTF(fmt, args...) \
39do { printf("ESP: " fmt , ##args); } while (0)
40#else
41#define DPRINTF(fmt, args...)
42#endif
43
6f7e9aec 44#define ESP_MAXREG 0x3f
2e5d83bb 45#define TI_BUFSZ 32
67e999be 46
4e9aec74 47typedef struct ESPState ESPState;
6f7e9aec 48
4e9aec74 49struct ESPState {
6f7e9aec 50 BlockDriverState **bd;
2f275b8f
FB
51 uint8_t rregs[ESP_MAXREG];
52 uint8_t wregs[ESP_MAXREG];
67e999be 53 int32_t ti_size;
4f6200f0 54 uint32_t ti_rptr, ti_wptr;
4f6200f0 55 uint8_t ti_buf[TI_BUFSZ];
0fc5c15a 56 int sense;
4f6200f0 57 int dma;
2e5d83bb
PB
58 SCSIDevice *scsi_dev[MAX_DISKS];
59 SCSIDevice *current_dev;
9f149aa9
PB
60 uint8_t cmdbuf[TI_BUFSZ];
61 int cmdlen;
62 int do_cmd;
4d611c9a 63
6787f5fa 64 /* The amount of data left in the current DMA transfer. */
4d611c9a 65 uint32_t dma_left;
6787f5fa
PB
66 /* The size of the current DMA transfer. Zero if no transfer is in
67 progress. */
68 uint32_t dma_counter;
a917d384 69 uint8_t *async_buf;
4d611c9a 70 uint32_t async_len;
67e999be 71 void *dma_opaque;
4e9aec74 72};
6f7e9aec 73
2f275b8f
FB
74#define STAT_DO 0x00
75#define STAT_DI 0x01
76#define STAT_CD 0x02
77#define STAT_ST 0x03
78#define STAT_MI 0x06
79#define STAT_MO 0x07
80
81#define STAT_TC 0x10
4d611c9a
PB
82#define STAT_PE 0x20
83#define STAT_GE 0x40
2f275b8f
FB
84#define STAT_IN 0x80
85
86#define INTR_FC 0x08
87#define INTR_BS 0x10
88#define INTR_DC 0x20
9e61bde5 89#define INTR_RST 0x80
2f275b8f
FB
90
91#define SEQ_0 0x0
92#define SEQ_CD 0x4
93
9f149aa9 94static int get_cmd(ESPState *s, uint8_t *buf)
2f275b8f 95{
a917d384 96 uint32_t dmalen;
2f275b8f
FB
97 int target;
98
6787f5fa 99 dmalen = s->rregs[0] | (s->rregs[1] << 8);
4f6200f0 100 target = s->wregs[4] & 7;
9f149aa9 101 DPRINTF("get_cmd: len %d target %d\n", dmalen, target);
4f6200f0 102 if (s->dma) {
67e999be 103 espdma_memory_read(s->dma_opaque, buf, dmalen);
4f6200f0
FB
104 } else {
105 buf[0] = 0;
106 memcpy(&buf[1], s->ti_buf, dmalen);
107 dmalen++;
108 }
2e5d83bb 109
2f275b8f 110 s->ti_size = 0;
4f6200f0
FB
111 s->ti_rptr = 0;
112 s->ti_wptr = 0;
2f275b8f 113
a917d384
PB
114 if (s->current_dev) {
115 /* Started a new command before the old one finished. Cancel it. */
116 scsi_cancel_io(s->current_dev, 0);
117 s->async_len = 0;
118 }
119
67e999be 120 if (target >= MAX_DISKS || !s->scsi_dev[target]) {
2e5d83bb 121 // No such drive
2f275b8f
FB
122 s->rregs[4] = STAT_IN;
123 s->rregs[5] = INTR_DC;
124 s->rregs[6] = SEQ_0;
67e999be 125 espdma_raise_irq(s->dma_opaque);
9f149aa9 126 return 0;
2f275b8f 127 }
2e5d83bb 128 s->current_dev = s->scsi_dev[target];
9f149aa9
PB
129 return dmalen;
130}
131
132static void do_cmd(ESPState *s, uint8_t *buf)
133{
134 int32_t datalen;
135 int lun;
136
137 DPRINTF("do_cmd: busid 0x%x\n", buf[0]);
138 lun = buf[0] & 7;
0fc5c15a 139 datalen = scsi_send_command(s->current_dev, 0, &buf[1], lun);
67e999be
FB
140 s->ti_size = datalen;
141 if (datalen != 0) {
2e5d83bb 142 s->rregs[4] = STAT_IN | STAT_TC;
a917d384 143 s->dma_left = 0;
6787f5fa 144 s->dma_counter = 0;
2e5d83bb
PB
145 if (datalen > 0) {
146 s->rregs[4] |= STAT_DI;
a917d384 147 scsi_read_data(s->current_dev, 0);
2e5d83bb
PB
148 } else {
149 s->rregs[4] |= STAT_DO;
a917d384 150 scsi_write_data(s->current_dev, 0);
b9788fc4 151 }
2f275b8f 152 }
2f275b8f
FB
153 s->rregs[5] = INTR_BS | INTR_FC;
154 s->rregs[6] = SEQ_CD;
67e999be 155 espdma_raise_irq(s->dma_opaque);
2f275b8f
FB
156}
157
9f149aa9
PB
158static void handle_satn(ESPState *s)
159{
160 uint8_t buf[32];
161 int len;
162
163 len = get_cmd(s, buf);
164 if (len)
165 do_cmd(s, buf);
166}
167
168static void handle_satn_stop(ESPState *s)
169{
170 s->cmdlen = get_cmd(s, s->cmdbuf);
171 if (s->cmdlen) {
172 DPRINTF("Set ATN & Stop: cmdlen %d\n", s->cmdlen);
173 s->do_cmd = 1;
9f149aa9
PB
174 s->rregs[4] = STAT_IN | STAT_TC | STAT_CD;
175 s->rregs[5] = INTR_BS | INTR_FC;
176 s->rregs[6] = SEQ_CD;
67e999be 177 espdma_raise_irq(s->dma_opaque);
9f149aa9
PB
178 }
179}
180
0fc5c15a 181static void write_response(ESPState *s)
2f275b8f 182{
0fc5c15a
PB
183 DPRINTF("Transfer status (sense=%d)\n", s->sense);
184 s->ti_buf[0] = s->sense;
185 s->ti_buf[1] = 0;
4f6200f0 186 if (s->dma) {
67e999be 187 espdma_memory_write(s->dma_opaque, s->ti_buf, 2);
4f6200f0
FB
188 s->rregs[4] = STAT_IN | STAT_TC | STAT_ST;
189 s->rregs[5] = INTR_BS | INTR_FC;
190 s->rregs[6] = SEQ_CD;
4f6200f0 191 } else {
0fc5c15a 192 s->ti_size = 2;
4f6200f0
FB
193 s->ti_rptr = 0;
194 s->ti_wptr = 0;
0fc5c15a 195 s->rregs[7] = 2;
4f6200f0 196 }
67e999be 197 espdma_raise_irq(s->dma_opaque);
2f275b8f 198}
4f6200f0 199
a917d384
PB
200static void esp_dma_done(ESPState *s)
201{
202 s->rregs[4] |= STAT_IN | STAT_TC;
203 s->rregs[5] = INTR_BS;
204 s->rregs[6] = 0;
205 s->rregs[7] = 0;
6787f5fa
PB
206 s->rregs[0] = 0;
207 s->rregs[1] = 0;
67e999be 208 espdma_raise_irq(s->dma_opaque);
a917d384
PB
209}
210
4d611c9a
PB
211static void esp_do_dma(ESPState *s)
212{
67e999be 213 uint32_t len;
4d611c9a 214 int to_device;
a917d384 215
67e999be 216 to_device = (s->ti_size < 0);
a917d384 217 len = s->dma_left;
4d611c9a 218 if (s->do_cmd) {
4d611c9a 219 DPRINTF("command len %d + %d\n", s->cmdlen, len);
67e999be 220 espdma_memory_read(s->dma_opaque, &s->cmdbuf[s->cmdlen], len);
4d611c9a
PB
221 s->ti_size = 0;
222 s->cmdlen = 0;
223 s->do_cmd = 0;
224 do_cmd(s, s->cmdbuf);
225 return;
a917d384
PB
226 }
227 if (s->async_len == 0) {
228 /* Defer until data is available. */
229 return;
230 }
231 if (len > s->async_len) {
232 len = s->async_len;
233 }
234 if (to_device) {
67e999be 235 espdma_memory_read(s->dma_opaque, s->async_buf, len);
4d611c9a 236 } else {
67e999be 237 espdma_memory_write(s->dma_opaque, s->async_buf, len);
a917d384 238 }
a917d384
PB
239 s->dma_left -= len;
240 s->async_buf += len;
241 s->async_len -= len;
6787f5fa
PB
242 if (to_device)
243 s->ti_size += len;
244 else
245 s->ti_size -= len;
a917d384 246 if (s->async_len == 0) {
4d611c9a 247 if (to_device) {
67e999be 248 // ti_size is negative
a917d384 249 scsi_write_data(s->current_dev, 0);
4d611c9a 250 } else {
a917d384 251 scsi_read_data(s->current_dev, 0);
6787f5fa
PB
252 /* If there is still data to be read from the device then
253 complete the DMA operation immeriately. Otherwise defer
254 until the scsi layer has completed. */
255 if (s->dma_left == 0 && s->ti_size > 0) {
256 esp_dma_done(s);
257 }
4d611c9a 258 }
6787f5fa
PB
259 } else {
260 /* Partially filled a scsi buffer. Complete immediately. */
a917d384
PB
261 esp_dma_done(s);
262 }
4d611c9a
PB
263}
264
a917d384
PB
265static void esp_command_complete(void *opaque, int reason, uint32_t tag,
266 uint32_t arg)
2e5d83bb
PB
267{
268 ESPState *s = (ESPState *)opaque;
269
4d611c9a
PB
270 if (reason == SCSI_REASON_DONE) {
271 DPRINTF("SCSI Command complete\n");
272 if (s->ti_size != 0)
273 DPRINTF("SCSI command completed unexpectedly\n");
274 s->ti_size = 0;
a917d384
PB
275 s->dma_left = 0;
276 s->async_len = 0;
277 if (arg)
4d611c9a 278 DPRINTF("Command failed\n");
a917d384
PB
279 s->sense = arg;
280 s->rregs[4] = STAT_ST;
281 esp_dma_done(s);
282 s->current_dev = NULL;
4d611c9a
PB
283 } else {
284 DPRINTF("transfer %d/%d\n", s->dma_left, s->ti_size);
a917d384
PB
285 s->async_len = arg;
286 s->async_buf = scsi_get_buf(s->current_dev, 0);
6787f5fa 287 if (s->dma_left) {
a917d384 288 esp_do_dma(s);
6787f5fa
PB
289 } else if (s->dma_counter != 0 && s->ti_size <= 0) {
290 /* If this was the last part of a DMA transfer then the
291 completion interrupt is deferred to here. */
292 esp_dma_done(s);
293 }
4d611c9a 294 }
2e5d83bb
PB
295}
296
2f275b8f
FB
297static void handle_ti(ESPState *s)
298{
4d611c9a 299 uint32_t dmalen, minlen;
2f275b8f 300
6787f5fa 301 dmalen = s->rregs[0] | (s->rregs[1] << 8);
db59203d
PB
302 if (dmalen==0) {
303 dmalen=0x10000;
304 }
6787f5fa 305 s->dma_counter = dmalen;
db59203d 306
9f149aa9
PB
307 if (s->do_cmd)
308 minlen = (dmalen < 32) ? dmalen : 32;
67e999be
FB
309 else if (s->ti_size < 0)
310 minlen = (dmalen < -s->ti_size) ? dmalen : -s->ti_size;
9f149aa9
PB
311 else
312 minlen = (dmalen < s->ti_size) ? dmalen : s->ti_size;
db59203d 313 DPRINTF("Transfer Information len %d\n", minlen);
4f6200f0 314 if (s->dma) {
4d611c9a
PB
315 s->dma_left = minlen;
316 s->rregs[4] &= ~STAT_TC;
317 esp_do_dma(s);
9f149aa9
PB
318 } else if (s->do_cmd) {
319 DPRINTF("command len %d\n", s->cmdlen);
320 s->ti_size = 0;
321 s->cmdlen = 0;
322 s->do_cmd = 0;
323 do_cmd(s, s->cmdbuf);
324 return;
325 }
2f275b8f
FB
326}
327
67e999be 328void esp_reset(void *opaque)
6f7e9aec
FB
329{
330 ESPState *s = opaque;
67e999be 331
2f275b8f 332 memset(s->rregs, 0, ESP_MAXREG);
4e9aec74 333 memset(s->wregs, 0, ESP_MAXREG);
2f275b8f 334 s->rregs[0x0e] = 0x4; // Indicate fas100a
4e9aec74
PB
335 s->ti_size = 0;
336 s->ti_rptr = 0;
337 s->ti_wptr = 0;
4e9aec74 338 s->dma = 0;
9f149aa9 339 s->do_cmd = 0;
6f7e9aec
FB
340}
341
342static uint32_t esp_mem_readb(void *opaque, target_phys_addr_t addr)
343{
344 ESPState *s = opaque;
345 uint32_t saddr;
346
347 saddr = (addr & ESP_MAXREG) >> 2;
9e61bde5 348 DPRINTF("read reg[%d]: 0x%2.2x\n", saddr, s->rregs[saddr]);
6f7e9aec 349 switch (saddr) {
4f6200f0
FB
350 case 2:
351 // FIFO
352 if (s->ti_size > 0) {
353 s->ti_size--;
2e5d83bb
PB
354 if ((s->rregs[4] & 6) == 0) {
355 /* Data in/out. */
a917d384
PB
356 fprintf(stderr, "esp: PIO data read not implemented\n");
357 s->rregs[2] = 0;
2e5d83bb
PB
358 } else {
359 s->rregs[2] = s->ti_buf[s->ti_rptr++];
360 }
67e999be 361 espdma_raise_irq(s->dma_opaque);
4f6200f0
FB
362 }
363 if (s->ti_size == 0) {
364 s->ti_rptr = 0;
365 s->ti_wptr = 0;
366 }
367 break;
9e61bde5
FB
368 case 5:
369 // interrupt
4d611c9a
PB
370 // Clear interrupt/error status bits
371 s->rregs[4] &= ~(STAT_IN | STAT_GE | STAT_PE);
67e999be 372 espdma_clear_irq(s->dma_opaque);
9e61bde5 373 break;
6f7e9aec
FB
374 default:
375 break;
376 }
2f275b8f 377 return s->rregs[saddr];
6f7e9aec
FB
378}
379
380static void esp_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
381{
382 ESPState *s = opaque;
383 uint32_t saddr;
384
385 saddr = (addr & ESP_MAXREG) >> 2;
2f275b8f 386 DPRINTF("write reg[%d]: 0x%2.2x -> 0x%2.2x\n", saddr, s->wregs[saddr], val);
6f7e9aec 387 switch (saddr) {
4f6200f0
FB
388 case 0:
389 case 1:
4d611c9a 390 s->rregs[4] &= ~STAT_TC;
4f6200f0
FB
391 break;
392 case 2:
393 // FIFO
9f149aa9
PB
394 if (s->do_cmd) {
395 s->cmdbuf[s->cmdlen++] = val & 0xff;
396 } else if ((s->rregs[4] & 6) == 0) {
2e5d83bb
PB
397 uint8_t buf;
398 buf = val & 0xff;
399 s->ti_size--;
a917d384 400 fprintf(stderr, "esp: PIO data write not implemented\n");
2e5d83bb
PB
401 } else {
402 s->ti_size++;
403 s->ti_buf[s->ti_wptr++] = val & 0xff;
404 }
4f6200f0 405 break;
6f7e9aec 406 case 3:
4f6200f0 407 s->rregs[saddr] = val;
6f7e9aec 408 // Command
4f6200f0
FB
409 if (val & 0x80) {
410 s->dma = 1;
6787f5fa
PB
411 /* Reload DMA counter. */
412 s->rregs[0] = s->wregs[0];
413 s->rregs[1] = s->wregs[1];
4f6200f0
FB
414 } else {
415 s->dma = 0;
416 }
6f7e9aec
FB
417 switch(val & 0x7f) {
418 case 0:
2f275b8f
FB
419 DPRINTF("NOP (%2.2x)\n", val);
420 break;
421 case 1:
422 DPRINTF("Flush FIFO (%2.2x)\n", val);
9e61bde5 423 //s->ti_size = 0;
2f275b8f 424 s->rregs[5] = INTR_FC;
9e61bde5 425 s->rregs[6] = 0;
6f7e9aec
FB
426 break;
427 case 2:
2f275b8f 428 DPRINTF("Chip reset (%2.2x)\n", val);
6f7e9aec
FB
429 esp_reset(s);
430 break;
431 case 3:
2f275b8f 432 DPRINTF("Bus reset (%2.2x)\n", val);
9e61bde5
FB
433 s->rregs[5] = INTR_RST;
434 if (!(s->wregs[8] & 0x40)) {
67e999be 435 espdma_raise_irq(s->dma_opaque);
9e61bde5 436 }
2f275b8f
FB
437 break;
438 case 0x10:
439 handle_ti(s);
440 break;
441 case 0x11:
442 DPRINTF("Initiator Command Complete Sequence (%2.2x)\n", val);
0fc5c15a 443 write_response(s);
2f275b8f
FB
444 break;
445 case 0x12:
446 DPRINTF("Message Accepted (%2.2x)\n", val);
0fc5c15a 447 write_response(s);
2f275b8f
FB
448 s->rregs[5] = INTR_DC;
449 s->rregs[6] = 0;
6f7e9aec
FB
450 break;
451 case 0x1a:
2f275b8f 452 DPRINTF("Set ATN (%2.2x)\n", val);
6f7e9aec
FB
453 break;
454 case 0x42:
9f149aa9 455 DPRINTF("Set ATN (%2.2x)\n", val);
2f275b8f
FB
456 handle_satn(s);
457 break;
458 case 0x43:
459 DPRINTF("Set ATN & stop (%2.2x)\n", val);
9f149aa9 460 handle_satn_stop(s);
2f275b8f
FB
461 break;
462 default:
4f6200f0 463 DPRINTF("Unhandled ESP command (%2.2x)\n", val);
6f7e9aec
FB
464 break;
465 }
466 break;
467 case 4 ... 7:
6f7e9aec 468 break;
4f6200f0
FB
469 case 8:
470 s->rregs[saddr] = val;
471 break;
472 case 9 ... 10:
473 break;
9e61bde5
FB
474 case 11:
475 s->rregs[saddr] = val & 0x15;
476 break;
477 case 12 ... 15:
4f6200f0
FB
478 s->rregs[saddr] = val;
479 break;
6f7e9aec 480 default:
6f7e9aec
FB
481 break;
482 }
2f275b8f 483 s->wregs[saddr] = val;
6f7e9aec
FB
484}
485
486static CPUReadMemoryFunc *esp_mem_read[3] = {
487 esp_mem_readb,
488 esp_mem_readb,
489 esp_mem_readb,
490};
491
492static CPUWriteMemoryFunc *esp_mem_write[3] = {
493 esp_mem_writeb,
494 esp_mem_writeb,
495 esp_mem_writeb,
496};
497
6f7e9aec
FB
498static void esp_save(QEMUFile *f, void *opaque)
499{
500 ESPState *s = opaque;
2f275b8f
FB
501
502 qemu_put_buffer(f, s->rregs, ESP_MAXREG);
503 qemu_put_buffer(f, s->wregs, ESP_MAXREG);
4f6200f0
FB
504 qemu_put_be32s(f, &s->ti_size);
505 qemu_put_be32s(f, &s->ti_rptr);
506 qemu_put_be32s(f, &s->ti_wptr);
4f6200f0
FB
507 qemu_put_buffer(f, s->ti_buf, TI_BUFSZ);
508 qemu_put_be32s(f, &s->dma);
6f7e9aec
FB
509}
510
511static int esp_load(QEMUFile *f, void *opaque, int version_id)
512{
513 ESPState *s = opaque;
514
67e999be
FB
515 if (version_id != 2)
516 return -EINVAL; // Cannot emulate 1
6f7e9aec 517
2f275b8f
FB
518 qemu_get_buffer(f, s->rregs, ESP_MAXREG);
519 qemu_get_buffer(f, s->wregs, ESP_MAXREG);
4f6200f0
FB
520 qemu_get_be32s(f, &s->ti_size);
521 qemu_get_be32s(f, &s->ti_rptr);
522 qemu_get_be32s(f, &s->ti_wptr);
4f6200f0
FB
523 qemu_get_buffer(f, s->ti_buf, TI_BUFSZ);
524 qemu_get_be32s(f, &s->dma);
2f275b8f 525
6f7e9aec
FB
526 return 0;
527}
528
67e999be 529void *esp_init(BlockDriverState **bd, uint32_t espaddr, void *dma_opaque)
6f7e9aec
FB
530{
531 ESPState *s;
67e999be 532 int esp_io_memory;
2e5d83bb 533 int i;
6f7e9aec
FB
534
535 s = qemu_mallocz(sizeof(ESPState));
536 if (!s)
67e999be 537 return NULL;
6f7e9aec
FB
538
539 s->bd = bd;
67e999be 540 s->dma_opaque = dma_opaque;
6f7e9aec
FB
541
542 esp_io_memory = cpu_register_io_memory(0, esp_mem_read, esp_mem_write, s);
543 cpu_register_physical_memory(espaddr, ESP_MAXREG*4, esp_io_memory);
544
6f7e9aec
FB
545 esp_reset(s);
546
67e999be 547 register_savevm("esp", espaddr, 2, esp_save, esp_load, s);
6f7e9aec 548 qemu_register_reset(esp_reset, s);
2e5d83bb
PB
549 for (i = 0; i < MAX_DISKS; i++) {
550 if (bs_table[i]) {
a917d384 551 /* Command queueing is not implemented. */
2e5d83bb 552 s->scsi_dev[i] =
a917d384 553 scsi_disk_init(bs_table[i], 0, esp_command_complete, s);
2e5d83bb
PB
554 }
555 }
6f7e9aec 556
67e999be
FB
557 return s;
558}
This page took 0.169285 seconds and 4 git commands to generate.