blob: d0a01a1a9cf35073fc0fc0a613bdfbfd2b453cd9 [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw.h"
25#include "block.h"
26#include "scsi-disk.h"
27#include "sun4m.h"
28/* FIXME: Only needed for MAX_DISKS, which is probably wrong. */
29#include "sysemu.h"
bellard6f7e9ae2005-03-13 09:43:36 +000030
31/* debug ESP card */
bellard2f275b82005-04-06 20:31:50 +000032//#define DEBUG_ESP
bellard6f7e9ae2005-03-13 09:43:36 +000033
bellard67e999b2006-09-03 16:09:07 +000034/*
35 * On Sparc32, this is the ESP (NCR53C90) part of chip STP2000 (Master I/O), also
36 * produced as NCR89C100. See
37 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C100.txt
38 * and
39 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR53C9X.txt
40 */
41
bellard6f7e9ae2005-03-13 09:43:36 +000042#ifdef DEBUG_ESP
43#define DPRINTF(fmt, args...) \
44do { printf("ESP: " fmt , ##args); } while (0)
45#else
46#define DPRINTF(fmt, args...)
47#endif
48
blueswir15aca8c32007-05-26 17:39:43 +000049#define ESP_MASK 0x3f
50#define ESP_REGS 16
51#define ESP_SIZE (ESP_REGS * 4)
pbrook2e5d83b2006-05-25 23:58:51 +000052#define TI_BUFSZ 32
thsfa1fb142006-12-24 17:12:43 +000053/* The HBA is ID 7, so for simplicitly limit to 7 devices. */
54#define ESP_MAX_DEVS 7
bellard67e999b2006-09-03 16:09:07 +000055
pbrook4e9aec72006-03-11 16:29:14 +000056typedef struct ESPState ESPState;
bellard6f7e9ae2005-03-13 09:43:36 +000057
pbrook4e9aec72006-03-11 16:29:14 +000058struct ESPState {
blueswir170c0de92007-05-27 16:36:10 +000059 qemu_irq irq;
bellard6f7e9ae2005-03-13 09:43:36 +000060 BlockDriverState **bd;
blueswir15aca8c32007-05-26 17:39:43 +000061 uint8_t rregs[ESP_REGS];
62 uint8_t wregs[ESP_REGS];
bellard67e999b2006-09-03 16:09:07 +000063 int32_t ti_size;
bellard4f6200f2005-10-30 17:24:05 +000064 uint32_t ti_rptr, ti_wptr;
bellard4f6200f2005-10-30 17:24:05 +000065 uint8_t ti_buf[TI_BUFSZ];
pbrook0fc5c152006-05-26 21:53:41 +000066 int sense;
bellard4f6200f2005-10-30 17:24:05 +000067 int dma;
pbrook2e5d83b2006-05-25 23:58:51 +000068 SCSIDevice *scsi_dev[MAX_DISKS];
69 SCSIDevice *current_dev;
pbrook9f149aa2006-06-03 14:19:19 +000070 uint8_t cmdbuf[TI_BUFSZ];
71 int cmdlen;
72 int do_cmd;
pbrook4d611c92006-08-12 01:04:27 +000073
pbrook6787f5f2006-09-17 03:20:58 +000074 /* The amount of data left in the current DMA transfer. */
pbrook4d611c92006-08-12 01:04:27 +000075 uint32_t dma_left;
pbrook6787f5f2006-09-17 03:20:58 +000076 /* The size of the current DMA transfer. Zero if no transfer is in
77 progress. */
78 uint32_t dma_counter;
pbrooka917d382006-08-29 04:52:16 +000079 uint8_t *async_buf;
pbrook4d611c92006-08-12 01:04:27 +000080 uint32_t async_len;
bellard67e999b2006-09-03 16:09:07 +000081 void *dma_opaque;
pbrook4e9aec72006-03-11 16:29:14 +000082};
bellard6f7e9ae2005-03-13 09:43:36 +000083
bellard2f275b82005-04-06 20:31:50 +000084#define STAT_DO 0x00
85#define STAT_DI 0x01
86#define STAT_CD 0x02
87#define STAT_ST 0x03
88#define STAT_MI 0x06
89#define STAT_MO 0x07
90
91#define STAT_TC 0x10
pbrook4d611c92006-08-12 01:04:27 +000092#define STAT_PE 0x20
93#define STAT_GE 0x40
bellard2f275b82005-04-06 20:31:50 +000094#define STAT_IN 0x80
95
96#define INTR_FC 0x08
97#define INTR_BS 0x10
98#define INTR_DC 0x20
bellard9e61bde2005-11-11 00:24:58 +000099#define INTR_RST 0x80
bellard2f275b82005-04-06 20:31:50 +0000100
101#define SEQ_0 0x0
102#define SEQ_CD 0x4
103
pbrook9f149aa2006-06-03 14:19:19 +0000104static int get_cmd(ESPState *s, uint8_t *buf)
bellard2f275b82005-04-06 20:31:50 +0000105{
pbrooka917d382006-08-29 04:52:16 +0000106 uint32_t dmalen;
bellard2f275b82005-04-06 20:31:50 +0000107 int target;
108
pbrook6787f5f2006-09-17 03:20:58 +0000109 dmalen = s->rregs[0] | (s->rregs[1] << 8);
bellard4f6200f2005-10-30 17:24:05 +0000110 target = s->wregs[4] & 7;
pbrook9f149aa2006-06-03 14:19:19 +0000111 DPRINTF("get_cmd: len %d target %d\n", dmalen, target);
bellard4f6200f2005-10-30 17:24:05 +0000112 if (s->dma) {
bellard67e999b2006-09-03 16:09:07 +0000113 espdma_memory_read(s->dma_opaque, buf, dmalen);
bellard4f6200f2005-10-30 17:24:05 +0000114 } else {
blueswir1f930d072007-10-06 11:28:21 +0000115 buf[0] = 0;
116 memcpy(&buf[1], s->ti_buf, dmalen);
117 dmalen++;
bellard4f6200f2005-10-30 17:24:05 +0000118 }
pbrook2e5d83b2006-05-25 23:58:51 +0000119
bellard2f275b82005-04-06 20:31:50 +0000120 s->ti_size = 0;
bellard4f6200f2005-10-30 17:24:05 +0000121 s->ti_rptr = 0;
122 s->ti_wptr = 0;
bellard2f275b82005-04-06 20:31:50 +0000123
pbrooka917d382006-08-29 04:52:16 +0000124 if (s->current_dev) {
125 /* Started a new command before the old one finished. Cancel it. */
126 scsi_cancel_io(s->current_dev, 0);
127 s->async_len = 0;
128 }
129
bellard67e999b2006-09-03 16:09:07 +0000130 if (target >= MAX_DISKS || !s->scsi_dev[target]) {
pbrook2e5d83b2006-05-25 23:58:51 +0000131 // No such drive
blueswir1f930d072007-10-06 11:28:21 +0000132 s->rregs[4] = STAT_IN;
133 s->rregs[5] = INTR_DC;
134 s->rregs[6] = SEQ_0;
135 qemu_irq_raise(s->irq);
136 return 0;
bellard2f275b82005-04-06 20:31:50 +0000137 }
pbrook2e5d83b2006-05-25 23:58:51 +0000138 s->current_dev = s->scsi_dev[target];
pbrook9f149aa2006-06-03 14:19:19 +0000139 return dmalen;
140}
141
142static void do_cmd(ESPState *s, uint8_t *buf)
143{
144 int32_t datalen;
145 int lun;
146
147 DPRINTF("do_cmd: busid 0x%x\n", buf[0]);
148 lun = buf[0] & 7;
pbrook0fc5c152006-05-26 21:53:41 +0000149 datalen = scsi_send_command(s->current_dev, 0, &buf[1], lun);
bellard67e999b2006-09-03 16:09:07 +0000150 s->ti_size = datalen;
151 if (datalen != 0) {
pbrook2e5d83b2006-05-25 23:58:51 +0000152 s->rregs[4] = STAT_IN | STAT_TC;
pbrooka917d382006-08-29 04:52:16 +0000153 s->dma_left = 0;
pbrook6787f5f2006-09-17 03:20:58 +0000154 s->dma_counter = 0;
pbrook2e5d83b2006-05-25 23:58:51 +0000155 if (datalen > 0) {
156 s->rregs[4] |= STAT_DI;
pbrooka917d382006-08-29 04:52:16 +0000157 scsi_read_data(s->current_dev, 0);
pbrook2e5d83b2006-05-25 23:58:51 +0000158 } else {
159 s->rregs[4] |= STAT_DO;
pbrooka917d382006-08-29 04:52:16 +0000160 scsi_write_data(s->current_dev, 0);
bellardb9788fc2005-12-05 20:30:36 +0000161 }
bellard2f275b82005-04-06 20:31:50 +0000162 }
bellard2f275b82005-04-06 20:31:50 +0000163 s->rregs[5] = INTR_BS | INTR_FC;
164 s->rregs[6] = SEQ_CD;
blueswir170c0de92007-05-27 16:36:10 +0000165 qemu_irq_raise(s->irq);
bellard2f275b82005-04-06 20:31:50 +0000166}
167
pbrook9f149aa2006-06-03 14:19:19 +0000168static void handle_satn(ESPState *s)
169{
170 uint8_t buf[32];
171 int len;
172
173 len = get_cmd(s, buf);
174 if (len)
175 do_cmd(s, buf);
176}
177
178static void handle_satn_stop(ESPState *s)
179{
180 s->cmdlen = get_cmd(s, s->cmdbuf);
181 if (s->cmdlen) {
182 DPRINTF("Set ATN & Stop: cmdlen %d\n", s->cmdlen);
183 s->do_cmd = 1;
pbrook9f149aa2006-06-03 14:19:19 +0000184 s->rregs[4] = STAT_IN | STAT_TC | STAT_CD;
185 s->rregs[5] = INTR_BS | INTR_FC;
186 s->rregs[6] = SEQ_CD;
blueswir170c0de92007-05-27 16:36:10 +0000187 qemu_irq_raise(s->irq);
pbrook9f149aa2006-06-03 14:19:19 +0000188 }
189}
190
pbrook0fc5c152006-05-26 21:53:41 +0000191static void write_response(ESPState *s)
bellard2f275b82005-04-06 20:31:50 +0000192{
pbrook0fc5c152006-05-26 21:53:41 +0000193 DPRINTF("Transfer status (sense=%d)\n", s->sense);
194 s->ti_buf[0] = s->sense;
195 s->ti_buf[1] = 0;
bellard4f6200f2005-10-30 17:24:05 +0000196 if (s->dma) {
bellard67e999b2006-09-03 16:09:07 +0000197 espdma_memory_write(s->dma_opaque, s->ti_buf, 2);
blueswir1f930d072007-10-06 11:28:21 +0000198 s->rregs[4] = STAT_IN | STAT_TC | STAT_ST;
199 s->rregs[5] = INTR_BS | INTR_FC;
200 s->rregs[6] = SEQ_CD;
bellard4f6200f2005-10-30 17:24:05 +0000201 } else {
blueswir1f930d072007-10-06 11:28:21 +0000202 s->ti_size = 2;
203 s->ti_rptr = 0;
204 s->ti_wptr = 0;
205 s->rregs[7] = 2;
bellard4f6200f2005-10-30 17:24:05 +0000206 }
blueswir170c0de92007-05-27 16:36:10 +0000207 qemu_irq_raise(s->irq);
bellard2f275b82005-04-06 20:31:50 +0000208}
bellard4f6200f2005-10-30 17:24:05 +0000209
pbrooka917d382006-08-29 04:52:16 +0000210static void esp_dma_done(ESPState *s)
211{
212 s->rregs[4] |= STAT_IN | STAT_TC;
213 s->rregs[5] = INTR_BS;
214 s->rregs[6] = 0;
215 s->rregs[7] = 0;
pbrook6787f5f2006-09-17 03:20:58 +0000216 s->rregs[0] = 0;
217 s->rregs[1] = 0;
blueswir170c0de92007-05-27 16:36:10 +0000218 qemu_irq_raise(s->irq);
pbrooka917d382006-08-29 04:52:16 +0000219}
220
pbrook4d611c92006-08-12 01:04:27 +0000221static void esp_do_dma(ESPState *s)
222{
bellard67e999b2006-09-03 16:09:07 +0000223 uint32_t len;
pbrook4d611c92006-08-12 01:04:27 +0000224 int to_device;
pbrooka917d382006-08-29 04:52:16 +0000225
bellard67e999b2006-09-03 16:09:07 +0000226 to_device = (s->ti_size < 0);
pbrooka917d382006-08-29 04:52:16 +0000227 len = s->dma_left;
pbrook4d611c92006-08-12 01:04:27 +0000228 if (s->do_cmd) {
pbrook4d611c92006-08-12 01:04:27 +0000229 DPRINTF("command len %d + %d\n", s->cmdlen, len);
bellard67e999b2006-09-03 16:09:07 +0000230 espdma_memory_read(s->dma_opaque, &s->cmdbuf[s->cmdlen], len);
pbrook4d611c92006-08-12 01:04:27 +0000231 s->ti_size = 0;
232 s->cmdlen = 0;
233 s->do_cmd = 0;
234 do_cmd(s, s->cmdbuf);
235 return;
pbrooka917d382006-08-29 04:52:16 +0000236 }
237 if (s->async_len == 0) {
238 /* Defer until data is available. */
239 return;
240 }
241 if (len > s->async_len) {
242 len = s->async_len;
243 }
244 if (to_device) {
bellard67e999b2006-09-03 16:09:07 +0000245 espdma_memory_read(s->dma_opaque, s->async_buf, len);
pbrook4d611c92006-08-12 01:04:27 +0000246 } else {
bellard67e999b2006-09-03 16:09:07 +0000247 espdma_memory_write(s->dma_opaque, s->async_buf, len);
pbrooka917d382006-08-29 04:52:16 +0000248 }
pbrooka917d382006-08-29 04:52:16 +0000249 s->dma_left -= len;
250 s->async_buf += len;
251 s->async_len -= len;
pbrook6787f5f2006-09-17 03:20:58 +0000252 if (to_device)
253 s->ti_size += len;
254 else
255 s->ti_size -= len;
pbrooka917d382006-08-29 04:52:16 +0000256 if (s->async_len == 0) {
pbrook4d611c92006-08-12 01:04:27 +0000257 if (to_device) {
bellard67e999b2006-09-03 16:09:07 +0000258 // ti_size is negative
pbrooka917d382006-08-29 04:52:16 +0000259 scsi_write_data(s->current_dev, 0);
pbrook4d611c92006-08-12 01:04:27 +0000260 } else {
pbrooka917d382006-08-29 04:52:16 +0000261 scsi_read_data(s->current_dev, 0);
pbrook6787f5f2006-09-17 03:20:58 +0000262 /* If there is still data to be read from the device then
263 complete the DMA operation immeriately. Otherwise defer
264 until the scsi layer has completed. */
265 if (s->dma_left == 0 && s->ti_size > 0) {
266 esp_dma_done(s);
267 }
pbrook4d611c92006-08-12 01:04:27 +0000268 }
pbrook6787f5f2006-09-17 03:20:58 +0000269 } else {
270 /* Partially filled a scsi buffer. Complete immediately. */
pbrooka917d382006-08-29 04:52:16 +0000271 esp_dma_done(s);
272 }
pbrook4d611c92006-08-12 01:04:27 +0000273}
274
pbrooka917d382006-08-29 04:52:16 +0000275static void esp_command_complete(void *opaque, int reason, uint32_t tag,
276 uint32_t arg)
pbrook2e5d83b2006-05-25 23:58:51 +0000277{
278 ESPState *s = (ESPState *)opaque;
279
pbrook4d611c92006-08-12 01:04:27 +0000280 if (reason == SCSI_REASON_DONE) {
281 DPRINTF("SCSI Command complete\n");
282 if (s->ti_size != 0)
283 DPRINTF("SCSI command completed unexpectedly\n");
284 s->ti_size = 0;
pbrooka917d382006-08-29 04:52:16 +0000285 s->dma_left = 0;
286 s->async_len = 0;
287 if (arg)
pbrook4d611c92006-08-12 01:04:27 +0000288 DPRINTF("Command failed\n");
pbrooka917d382006-08-29 04:52:16 +0000289 s->sense = arg;
290 s->rregs[4] = STAT_ST;
291 esp_dma_done(s);
292 s->current_dev = NULL;
pbrook4d611c92006-08-12 01:04:27 +0000293 } else {
294 DPRINTF("transfer %d/%d\n", s->dma_left, s->ti_size);
pbrooka917d382006-08-29 04:52:16 +0000295 s->async_len = arg;
296 s->async_buf = scsi_get_buf(s->current_dev, 0);
pbrook6787f5f2006-09-17 03:20:58 +0000297 if (s->dma_left) {
pbrooka917d382006-08-29 04:52:16 +0000298 esp_do_dma(s);
pbrook6787f5f2006-09-17 03:20:58 +0000299 } else if (s->dma_counter != 0 && s->ti_size <= 0) {
300 /* If this was the last part of a DMA transfer then the
301 completion interrupt is deferred to here. */
302 esp_dma_done(s);
303 }
pbrook4d611c92006-08-12 01:04:27 +0000304 }
pbrook2e5d83b2006-05-25 23:58:51 +0000305}
306
bellard2f275b82005-04-06 20:31:50 +0000307static void handle_ti(ESPState *s)
308{
pbrook4d611c92006-08-12 01:04:27 +0000309 uint32_t dmalen, minlen;
bellard2f275b82005-04-06 20:31:50 +0000310
pbrook6787f5f2006-09-17 03:20:58 +0000311 dmalen = s->rregs[0] | (s->rregs[1] << 8);
pbrookdb592032006-05-21 12:46:31 +0000312 if (dmalen==0) {
313 dmalen=0x10000;
314 }
pbrook6787f5f2006-09-17 03:20:58 +0000315 s->dma_counter = dmalen;
pbrookdb592032006-05-21 12:46:31 +0000316
pbrook9f149aa2006-06-03 14:19:19 +0000317 if (s->do_cmd)
318 minlen = (dmalen < 32) ? dmalen : 32;
bellard67e999b2006-09-03 16:09:07 +0000319 else if (s->ti_size < 0)
320 minlen = (dmalen < -s->ti_size) ? dmalen : -s->ti_size;
pbrook9f149aa2006-06-03 14:19:19 +0000321 else
322 minlen = (dmalen < s->ti_size) ? dmalen : s->ti_size;
pbrookdb592032006-05-21 12:46:31 +0000323 DPRINTF("Transfer Information len %d\n", minlen);
bellard4f6200f2005-10-30 17:24:05 +0000324 if (s->dma) {
pbrook4d611c92006-08-12 01:04:27 +0000325 s->dma_left = minlen;
326 s->rregs[4] &= ~STAT_TC;
327 esp_do_dma(s);
pbrook9f149aa2006-06-03 14:19:19 +0000328 } else if (s->do_cmd) {
329 DPRINTF("command len %d\n", s->cmdlen);
330 s->ti_size = 0;
331 s->cmdlen = 0;
332 s->do_cmd = 0;
333 do_cmd(s, s->cmdbuf);
334 return;
335 }
bellard2f275b82005-04-06 20:31:50 +0000336}
337
blueswir15aca8c32007-05-26 17:39:43 +0000338static void esp_reset(void *opaque)
bellard6f7e9ae2005-03-13 09:43:36 +0000339{
340 ESPState *s = opaque;
bellard67e999b2006-09-03 16:09:07 +0000341
blueswir15aca8c32007-05-26 17:39:43 +0000342 memset(s->rregs, 0, ESP_REGS);
343 memset(s->wregs, 0, ESP_REGS);
bellard2f275b82005-04-06 20:31:50 +0000344 s->rregs[0x0e] = 0x4; // Indicate fas100a
pbrook4e9aec72006-03-11 16:29:14 +0000345 s->ti_size = 0;
346 s->ti_rptr = 0;
347 s->ti_wptr = 0;
pbrook4e9aec72006-03-11 16:29:14 +0000348 s->dma = 0;
pbrook9f149aa2006-06-03 14:19:19 +0000349 s->do_cmd = 0;
bellard6f7e9ae2005-03-13 09:43:36 +0000350}
351
blueswir12d069ba2007-08-16 19:56:27 +0000352static void parent_esp_reset(void *opaque, int irq, int level)
353{
354 if (level)
355 esp_reset(opaque);
356}
357
bellard6f7e9ae2005-03-13 09:43:36 +0000358static uint32_t esp_mem_readb(void *opaque, target_phys_addr_t addr)
359{
360 ESPState *s = opaque;
361 uint32_t saddr;
362
blueswir15aca8c32007-05-26 17:39:43 +0000363 saddr = (addr & ESP_MASK) >> 2;
bellard9e61bde2005-11-11 00:24:58 +0000364 DPRINTF("read reg[%d]: 0x%2.2x\n", saddr, s->rregs[saddr]);
bellard6f7e9ae2005-03-13 09:43:36 +0000365 switch (saddr) {
bellard4f6200f2005-10-30 17:24:05 +0000366 case 2:
blueswir1f930d072007-10-06 11:28:21 +0000367 // FIFO
368 if (s->ti_size > 0) {
369 s->ti_size--;
pbrook2e5d83b2006-05-25 23:58:51 +0000370 if ((s->rregs[4] & 6) == 0) {
371 /* Data in/out. */
pbrooka917d382006-08-29 04:52:16 +0000372 fprintf(stderr, "esp: PIO data read not implemented\n");
373 s->rregs[2] = 0;
pbrook2e5d83b2006-05-25 23:58:51 +0000374 } else {
375 s->rregs[2] = s->ti_buf[s->ti_rptr++];
376 }
blueswir170c0de92007-05-27 16:36:10 +0000377 qemu_irq_raise(s->irq);
blueswir1f930d072007-10-06 11:28:21 +0000378 }
379 if (s->ti_size == 0) {
bellard4f6200f2005-10-30 17:24:05 +0000380 s->ti_rptr = 0;
381 s->ti_wptr = 0;
382 }
blueswir1f930d072007-10-06 11:28:21 +0000383 break;
bellard9e61bde2005-11-11 00:24:58 +0000384 case 5:
385 // interrupt
pbrook4d611c92006-08-12 01:04:27 +0000386 // Clear interrupt/error status bits
387 s->rregs[4] &= ~(STAT_IN | STAT_GE | STAT_PE);
blueswir1f930d072007-10-06 11:28:21 +0000388 qemu_irq_lower(s->irq);
bellard9e61bde2005-11-11 00:24:58 +0000389 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000390 default:
blueswir1f930d072007-10-06 11:28:21 +0000391 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000392 }
bellard2f275b82005-04-06 20:31:50 +0000393 return s->rregs[saddr];
bellard6f7e9ae2005-03-13 09:43:36 +0000394}
395
396static void esp_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
397{
398 ESPState *s = opaque;
399 uint32_t saddr;
400
blueswir15aca8c32007-05-26 17:39:43 +0000401 saddr = (addr & ESP_MASK) >> 2;
bellard2f275b82005-04-06 20:31:50 +0000402 DPRINTF("write reg[%d]: 0x%2.2x -> 0x%2.2x\n", saddr, s->wregs[saddr], val);
bellard6f7e9ae2005-03-13 09:43:36 +0000403 switch (saddr) {
bellard4f6200f2005-10-30 17:24:05 +0000404 case 0:
405 case 1:
pbrook4d611c92006-08-12 01:04:27 +0000406 s->rregs[4] &= ~STAT_TC;
bellard4f6200f2005-10-30 17:24:05 +0000407 break;
408 case 2:
blueswir1f930d072007-10-06 11:28:21 +0000409 // FIFO
pbrook9f149aa2006-06-03 14:19:19 +0000410 if (s->do_cmd) {
411 s->cmdbuf[s->cmdlen++] = val & 0xff;
412 } else if ((s->rregs[4] & 6) == 0) {
pbrook2e5d83b2006-05-25 23:58:51 +0000413 uint8_t buf;
414 buf = val & 0xff;
415 s->ti_size--;
pbrooka917d382006-08-29 04:52:16 +0000416 fprintf(stderr, "esp: PIO data write not implemented\n");
pbrook2e5d83b2006-05-25 23:58:51 +0000417 } else {
418 s->ti_size++;
419 s->ti_buf[s->ti_wptr++] = val & 0xff;
420 }
blueswir1f930d072007-10-06 11:28:21 +0000421 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000422 case 3:
bellard4f6200f2005-10-30 17:24:05 +0000423 s->rregs[saddr] = val;
blueswir1f930d072007-10-06 11:28:21 +0000424 // Command
425 if (val & 0x80) {
426 s->dma = 1;
pbrook6787f5f2006-09-17 03:20:58 +0000427 /* Reload DMA counter. */
428 s->rregs[0] = s->wregs[0];
429 s->rregs[1] = s->wregs[1];
blueswir1f930d072007-10-06 11:28:21 +0000430 } else {
431 s->dma = 0;
432 }
433 switch(val & 0x7f) {
434 case 0:
435 DPRINTF("NOP (%2.2x)\n", val);
436 break;
437 case 1:
438 DPRINTF("Flush FIFO (%2.2x)\n", val);
bellard9e61bde2005-11-11 00:24:58 +0000439 //s->ti_size = 0;
blueswir1f930d072007-10-06 11:28:21 +0000440 s->rregs[5] = INTR_FC;
441 s->rregs[6] = 0;
442 break;
443 case 2:
444 DPRINTF("Chip reset (%2.2x)\n", val);
445 esp_reset(s);
446 break;
447 case 3:
448 DPRINTF("Bus reset (%2.2x)\n", val);
449 s->rregs[5] = INTR_RST;
bellard9e61bde2005-11-11 00:24:58 +0000450 if (!(s->wregs[8] & 0x40)) {
blueswir170c0de92007-05-27 16:36:10 +0000451 qemu_irq_raise(s->irq);
bellard9e61bde2005-11-11 00:24:58 +0000452 }
blueswir1f930d072007-10-06 11:28:21 +0000453 break;
454 case 0x10:
455 handle_ti(s);
456 break;
457 case 0x11:
458 DPRINTF("Initiator Command Complete Sequence (%2.2x)\n", val);
459 write_response(s);
460 break;
461 case 0x12:
462 DPRINTF("Message Accepted (%2.2x)\n", val);
463 write_response(s);
464 s->rregs[5] = INTR_DC;
465 s->rregs[6] = 0;
466 break;
467 case 0x1a:
468 DPRINTF("Set ATN (%2.2x)\n", val);
469 break;
470 case 0x42:
471 DPRINTF("Set ATN (%2.2x)\n", val);
472 handle_satn(s);
473 break;
474 case 0x43:
475 DPRINTF("Set ATN & stop (%2.2x)\n", val);
476 handle_satn_stop(s);
477 break;
blueswir174ec6042007-08-11 07:58:41 +0000478 case 0x44:
479 DPRINTF("Enable selection (%2.2x)\n", val);
480 break;
blueswir1f930d072007-10-06 11:28:21 +0000481 default:
482 DPRINTF("Unhandled ESP command (%2.2x)\n", val);
483 break;
484 }
485 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000486 case 4 ... 7:
blueswir1f930d072007-10-06 11:28:21 +0000487 break;
bellard4f6200f2005-10-30 17:24:05 +0000488 case 8:
489 s->rregs[saddr] = val;
490 break;
491 case 9 ... 10:
492 break;
bellard9e61bde2005-11-11 00:24:58 +0000493 case 11:
494 s->rregs[saddr] = val & 0x15;
495 break;
496 case 12 ... 15:
bellard4f6200f2005-10-30 17:24:05 +0000497 s->rregs[saddr] = val;
498 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000499 default:
blueswir1f930d072007-10-06 11:28:21 +0000500 break;
bellard6f7e9ae2005-03-13 09:43:36 +0000501 }
bellard2f275b82005-04-06 20:31:50 +0000502 s->wregs[saddr] = val;
bellard6f7e9ae2005-03-13 09:43:36 +0000503}
504
505static CPUReadMemoryFunc *esp_mem_read[3] = {
506 esp_mem_readb,
507 esp_mem_readb,
508 esp_mem_readb,
509};
510
511static CPUWriteMemoryFunc *esp_mem_write[3] = {
512 esp_mem_writeb,
513 esp_mem_writeb,
514 esp_mem_writeb,
515};
516
bellard6f7e9ae2005-03-13 09:43:36 +0000517static void esp_save(QEMUFile *f, void *opaque)
518{
519 ESPState *s = opaque;
bellard2f275b82005-04-06 20:31:50 +0000520
blueswir15aca8c32007-05-26 17:39:43 +0000521 qemu_put_buffer(f, s->rregs, ESP_REGS);
522 qemu_put_buffer(f, s->wregs, ESP_REGS);
bellard4f6200f2005-10-30 17:24:05 +0000523 qemu_put_be32s(f, &s->ti_size);
524 qemu_put_be32s(f, &s->ti_rptr);
525 qemu_put_be32s(f, &s->ti_wptr);
bellard4f6200f2005-10-30 17:24:05 +0000526 qemu_put_buffer(f, s->ti_buf, TI_BUFSZ);
blueswir15425a212007-04-13 19:24:07 +0000527 qemu_put_be32s(f, &s->sense);
bellard4f6200f2005-10-30 17:24:05 +0000528 qemu_put_be32s(f, &s->dma);
blueswir15425a212007-04-13 19:24:07 +0000529 qemu_put_buffer(f, s->cmdbuf, TI_BUFSZ);
530 qemu_put_be32s(f, &s->cmdlen);
531 qemu_put_be32s(f, &s->do_cmd);
532 qemu_put_be32s(f, &s->dma_left);
533 // There should be no transfers in progress, so dma_counter is not saved
bellard6f7e9ae2005-03-13 09:43:36 +0000534}
535
536static int esp_load(QEMUFile *f, void *opaque, int version_id)
537{
538 ESPState *s = opaque;
ths3b46e622007-09-17 08:09:54 +0000539
blueswir15425a212007-04-13 19:24:07 +0000540 if (version_id != 3)
541 return -EINVAL; // Cannot emulate 2
bellard6f7e9ae2005-03-13 09:43:36 +0000542
blueswir15aca8c32007-05-26 17:39:43 +0000543 qemu_get_buffer(f, s->rregs, ESP_REGS);
544 qemu_get_buffer(f, s->wregs, ESP_REGS);
bellard4f6200f2005-10-30 17:24:05 +0000545 qemu_get_be32s(f, &s->ti_size);
546 qemu_get_be32s(f, &s->ti_rptr);
547 qemu_get_be32s(f, &s->ti_wptr);
bellard4f6200f2005-10-30 17:24:05 +0000548 qemu_get_buffer(f, s->ti_buf, TI_BUFSZ);
blueswir15425a212007-04-13 19:24:07 +0000549 qemu_get_be32s(f, &s->sense);
bellard4f6200f2005-10-30 17:24:05 +0000550 qemu_get_be32s(f, &s->dma);
blueswir15425a212007-04-13 19:24:07 +0000551 qemu_get_buffer(f, s->cmdbuf, TI_BUFSZ);
552 qemu_get_be32s(f, &s->cmdlen);
553 qemu_get_be32s(f, &s->do_cmd);
554 qemu_get_be32s(f, &s->dma_left);
bellard2f275b82005-04-06 20:31:50 +0000555
bellard6f7e9ae2005-03-13 09:43:36 +0000556 return 0;
557}
558
thsfa1fb142006-12-24 17:12:43 +0000559void esp_scsi_attach(void *opaque, BlockDriverState *bd, int id)
560{
561 ESPState *s = (ESPState *)opaque;
562
563 if (id < 0) {
564 for (id = 0; id < ESP_MAX_DEVS; id++) {
565 if (s->scsi_dev[id] == NULL)
566 break;
567 }
568 }
569 if (id >= ESP_MAX_DEVS) {
570 DPRINTF("Bad Device ID %d\n", id);
571 return;
572 }
573 if (s->scsi_dev[id]) {
574 DPRINTF("Destroying device %d\n", id);
575 scsi_disk_destroy(s->scsi_dev[id]);
576 }
577 DPRINTF("Attaching block device %d\n", id);
578 /* Command queueing is not implemented. */
579 s->scsi_dev[id] = scsi_disk_init(bd, 0, esp_command_complete, s);
580}
581
blueswir15dcb6b92007-05-19 12:58:30 +0000582void *esp_init(BlockDriverState **bd, target_phys_addr_t espaddr,
blueswir12d069ba2007-08-16 19:56:27 +0000583 void *dma_opaque, qemu_irq irq, qemu_irq *reset)
bellard6f7e9ae2005-03-13 09:43:36 +0000584{
585 ESPState *s;
bellard67e999b2006-09-03 16:09:07 +0000586 int esp_io_memory;
bellard6f7e9ae2005-03-13 09:43:36 +0000587
588 s = qemu_mallocz(sizeof(ESPState));
589 if (!s)
bellard67e999b2006-09-03 16:09:07 +0000590 return NULL;
bellard6f7e9ae2005-03-13 09:43:36 +0000591
592 s->bd = bd;
blueswir170c0de92007-05-27 16:36:10 +0000593 s->irq = irq;
bellard67e999b2006-09-03 16:09:07 +0000594 s->dma_opaque = dma_opaque;
bellard6f7e9ae2005-03-13 09:43:36 +0000595
596 esp_io_memory = cpu_register_io_memory(0, esp_mem_read, esp_mem_write, s);
blueswir15aca8c32007-05-26 17:39:43 +0000597 cpu_register_physical_memory(espaddr, ESP_SIZE, esp_io_memory);
bellard6f7e9ae2005-03-13 09:43:36 +0000598
bellard6f7e9ae2005-03-13 09:43:36 +0000599 esp_reset(s);
600
blueswir15425a212007-04-13 19:24:07 +0000601 register_savevm("esp", espaddr, 3, esp_save, esp_load, s);
bellard6f7e9ae2005-03-13 09:43:36 +0000602 qemu_register_reset(esp_reset, s);
bellard6f7e9ae2005-03-13 09:43:36 +0000603
blueswir12d069ba2007-08-16 19:56:27 +0000604 *reset = *qemu_allocate_irqs(parent_esp_reset, s, 1);
605
bellard67e999b2006-09-03 16:09:07 +0000606 return s;
607}