1 |
/* |
/* |
2 |
* Copyright (C) 2003-2005 Anders Gavare. All rights reserved. |
* Copyright (C) 2003-2007 Anders Gavare. All rights reserved. |
3 |
* |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
5 |
* modification, are permitted provided that the following conditions are met: |
* modification, are permitted provided that the following conditions are met: |
25 |
* SUCH DAMAGE. |
* SUCH DAMAGE. |
26 |
* |
* |
27 |
* |
* |
28 |
* $Id: dev_asc.c,v 1.74 2005/07/27 06:57:34 debug Exp $ |
* $Id: dev_asc.c,v 1.86 2007/06/15 18:44:19 debug Exp $ |
29 |
* |
* |
30 |
* 'asc' SCSI controller for some DECstation/DECsystem models, and |
* COMMENT: NCR53C9X "ASC" SCSI controller |
31 |
* for PICA-61. |
* |
32 |
|
* This is the SCSI controller used in some DECstation/DECsystem models and |
33 |
|
* the PICA-61 machine. |
34 |
* |
* |
35 |
* Supposed to support SCSI-1 and SCSI-2. I've not yet found any docs |
* Supposed to support SCSI-1 and SCSI-2. I've not yet found any docs |
36 |
* on NCR53C9X, so I'll try to implement this device from LSI53CF92A docs |
* on NCR53C9X, so I'll try to implement this device from LSI53CF92A docs |
52 |
* TODO: This module needs a clean-up, and some testing to see that |
* TODO: This module needs a clean-up, and some testing to see that |
53 |
* it works will all OSes that might use it (NetBSD, OpenBSD, |
* it works will all OSes that might use it (NetBSD, OpenBSD, |
54 |
* Ultrix, Linux, Mach, OSF/1, Sprite, ...) |
* Ultrix, Linux, Mach, OSF/1, Sprite, ...) |
55 |
|
* |
56 |
|
* Running Linux/DECstation 2.4.26 with no scsi disks attached causes |
57 |
|
* a warning message to be printed by Linux. (Whether this is a bug, |
58 |
|
* is is the way it works on real hardware, I don't know.) |
59 |
*/ |
*/ |
60 |
|
|
61 |
#include <stdio.h> |
#include <stdio.h> |
104 |
int mode; |
int mode; |
105 |
|
|
106 |
void *turbochannel; |
void *turbochannel; |
107 |
int irq_nr; |
struct interrupt irq; |
108 |
int irq_caused_last_time; |
int irq_asserted; |
109 |
|
|
110 |
/* Current state and transfer: */ |
/* Current state and transfer: */ |
111 |
int cur_state; |
int cur_state; |
155 |
int to_id, int dmaflag, int n_messagebytes); |
int to_id, int dmaflag, int n_messagebytes); |
156 |
|
|
157 |
|
|
158 |
/* |
DEVICE_TICK(asc) |
|
* dev_asc_tick(): |
|
|
* |
|
|
* This function is called "every now and then" from the CPU |
|
|
* main loop. |
|
|
*/ |
|
|
void dev_asc_tick(struct cpu *cpu, void *extra) |
|
159 |
{ |
{ |
160 |
struct asc_data *d = extra; |
struct asc_data *d = extra; |
161 |
|
int new_assert = d->reg_ro[NCR_STAT] & NCRSTAT_INT; |
162 |
|
|
163 |
if (d->reg_ro[NCR_STAT] & NCRSTAT_INT) |
if (new_assert && !d->irq_asserted) |
164 |
cpu_interrupt(cpu, d->irq_nr); |
INTERRUPT_ASSERT(d->irq); |
165 |
|
|
166 |
|
d->irq_asserted = new_assert; |
167 |
} |
} |
168 |
|
|
169 |
|
|
312 |
fatal("no incoming DMA data?\n"); |
fatal("no incoming DMA data?\n"); |
313 |
res = 0; |
res = 0; |
314 |
} else { |
} else { |
315 |
int len = d->xferp->data_in_len; |
size_t len = d->xferp->data_in_len; |
316 |
int len2 = d->reg_wo[NCR_TCL] + |
size_t len2 = d->reg_wo[NCR_TCL] + |
317 |
d->reg_wo[NCR_TCM] * 256; |
d->reg_wo[NCR_TCM] * 256; |
318 |
if (len2 == 0) |
if (len2 == 0) |
319 |
len2 = 65536; |
len2 = 65536; |
375 |
(int)len, (int)len2); */ |
(int)len, (int)len2); */ |
376 |
|
|
377 |
d->xferp->data_in_len -= len2; |
d->xferp->data_in_len -= len2; |
378 |
n = malloc(d->xferp->data_in_len); |
CHECK_ALLOCATION(n = |
379 |
if (n == NULL) { |
malloc(d->xferp->data_in_len)); |
|
fprintf(stderr, "out of memory" |
|
|
" in dev_asc\n"); |
|
|
exit(1); |
|
|
} |
|
380 |
memcpy(n, d->xferp->data_in + len2, |
memcpy(n, d->xferp->data_in + len2, |
381 |
d->xferp->data_in_len); |
d->xferp->data_in_len); |
382 |
free(d->xferp->data_in); |
free(d->xferp->data_in); |
506 |
} |
} |
507 |
|
|
508 |
newlen = oldlen + d->n_bytes_in_fifo; |
newlen = oldlen + d->n_bytes_in_fifo; |
509 |
d->xferp->msg_out = realloc(d->xferp->msg_out, newlen); |
CHECK_ALLOCATION(d->xferp->msg_out = |
510 |
|
realloc(d->xferp->msg_out, newlen)); |
511 |
d->xferp->msg_out_len = newlen; |
d->xferp->msg_out_len = newlen; |
|
if (d->xferp->msg_out == NULL) { |
|
|
fprintf(stderr, "out of memory realloc'ing " |
|
|
"msg_out\n"); |
|
|
exit(1); |
|
|
} |
|
512 |
|
|
513 |
i = oldlen; |
i = oldlen; |
514 |
while (d->fifo_in != d->fifo_out) { |
while (d->fifo_in != d->fifo_out) { |
743 |
} |
} |
744 |
|
|
745 |
|
|
746 |
/* |
DEVICE_ACCESS(asc_address_reg) |
|
* dev_asc_address_reg_access(): |
|
|
*/ |
|
|
int dev_asc_address_reg_access(struct cpu *cpu, struct memory *mem, |
|
|
uint64_t relative_addr, unsigned char *data, size_t len, |
|
|
int writeflag, void *extra) |
|
747 |
{ |
{ |
748 |
struct asc_data *d = extra; |
struct asc_data *d = extra; |
749 |
|
|
760 |
} |
} |
761 |
|
|
762 |
|
|
763 |
/* |
DEVICE_ACCESS(asc_dma) |
|
* dev_asc_dma_access(): |
|
|
*/ |
|
|
int dev_asc_dma_access(struct cpu *cpu, struct memory *mem, |
|
|
uint64_t relative_addr, unsigned char *data, size_t len, |
|
|
int writeflag, void *extra) |
|
764 |
{ |
{ |
765 |
struct asc_data *d = extra; |
struct asc_data *d = extra; |
766 |
|
|
798 |
} |
} |
799 |
|
|
800 |
|
|
801 |
/* |
DEVICE_ACCESS(asc) |
|
* dev_asc_access(): |
|
|
*/ |
|
|
int dev_asc_access(struct cpu *cpu, struct memory *mem, |
|
|
uint64_t relative_addr, unsigned char *data, size_t len, |
|
|
int writeflag, void *extra) |
|
802 |
{ |
{ |
803 |
int regnr; |
int regnr; |
804 |
struct asc_data *d = extra; |
struct asc_data *d = extra; |
806 |
int n_messagebytes = 0; |
int n_messagebytes = 0; |
807 |
uint64_t idata = 0, odata = 0; |
uint64_t idata = 0, odata = 0; |
808 |
|
|
809 |
|
if (writeflag == MEM_WRITE) |
810 |
idata = memory_readmax64(cpu, data, len); |
idata = memory_readmax64(cpu, data, len); |
811 |
|
|
812 |
#if 0 |
#if 0 |
813 |
/* Debug stuff useful when trying to make dev_asc compatible |
/* Debug stuff useful when trying to make dev_asc compatible |
1194 |
d->reg_ro[NCR_STAT] = PHASE_COMMAND; |
d->reg_ro[NCR_STAT] = PHASE_COMMAND; |
1195 |
} |
} |
1196 |
|
|
1197 |
cpu_interrupt_ack(cpu, d->irq_nr); |
INTERRUPT_DEASSERT(d->irq); |
1198 |
|
d->irq_asserted = 0; |
1199 |
} |
} |
1200 |
|
|
1201 |
if (regnr == NCR_CFG1) { |
if (regnr == NCR_CFG1) { |
1225 |
* Register an 'asc' device. |
* Register an 'asc' device. |
1226 |
*/ |
*/ |
1227 |
void dev_asc_init(struct machine *machine, struct memory *mem, |
void dev_asc_init(struct machine *machine, struct memory *mem, |
1228 |
uint64_t baseaddr, int irq_nr, void *turbochannel, |
uint64_t baseaddr, char *irq_path, void *turbochannel, int mode, |
|
int mode, |
|
1229 |
size_t (*dma_controller)(void *dma_controller_data, |
size_t (*dma_controller)(void *dma_controller_data, |
1230 |
unsigned char *data, size_t len, int writeflag), |
unsigned char *data, size_t len, int writeflag), |
1231 |
void *dma_controller_data) |
void *dma_controller_data) |
1232 |
{ |
{ |
1233 |
struct asc_data *d; |
struct asc_data *d; |
1234 |
|
|
1235 |
d = malloc(sizeof(struct asc_data)); |
CHECK_ALLOCATION(d = malloc(sizeof(struct asc_data))); |
|
if (d == NULL) { |
|
|
fprintf(stderr, "out of memory\n"); |
|
|
exit(1); |
|
|
} |
|
1236 |
memset(d, 0, sizeof(struct asc_data)); |
memset(d, 0, sizeof(struct asc_data)); |
1237 |
d->irq_nr = irq_nr; |
|
1238 |
|
INTERRUPT_CONNECT(irq_path, d->irq); |
1239 |
d->turbochannel = turbochannel; |
d->turbochannel = turbochannel; |
1240 |
d->mode = mode; |
d->mode = mode; |
1241 |
|
|
1242 |
d->reg_ro[NCR_CFG3] = NCRF9XCFG3_CDB; |
d->reg_ro[NCR_CFG3] = NCRF9XCFG3_CDB; |
1243 |
|
|
1244 |
d->dma_address_reg_memory = malloc(machine->arch_pagesize); |
CHECK_ALLOCATION(d->dma_address_reg_memory = |
1245 |
d->dma = malloc(ASC_DMA_SIZE); |
malloc(machine->arch_pagesize)); |
|
if (d->dma == NULL || d->dma_address_reg_memory == NULL) { |
|
|
fprintf(stderr, "out of memory\n"); |
|
|
exit(1); |
|
|
} |
|
1246 |
memset(d->dma_address_reg_memory, 0, machine->arch_pagesize); |
memset(d->dma_address_reg_memory, 0, machine->arch_pagesize); |
1247 |
|
|
1248 |
|
CHECK_ALLOCATION(d->dma = malloc(ASC_DMA_SIZE)); |
1249 |
memset(d->dma, 0, ASC_DMA_SIZE); |
memset(d->dma, 0, ASC_DMA_SIZE); |
1250 |
|
|
1251 |
d->dma_controller = dma_controller; |
d->dma_controller = dma_controller; |
1252 |
d->dma_controller_data = dma_controller_data; |
d->dma_controller_data = dma_controller_data; |
1253 |
|
|
1254 |
memory_device_register(mem, "asc", baseaddr, |
memory_device_register(mem, "asc", baseaddr, |
1255 |
mode == DEV_ASC_PICA? |
mode == DEV_ASC_PICA? DEV_ASC_PICA_LENGTH : DEV_ASC_DEC_LENGTH, |
1256 |
DEV_ASC_PICA_LENGTH : DEV_ASC_DEC_LENGTH, |
dev_asc_access, d, DM_DEFAULT, NULL); |
|
dev_asc_access, d, MEM_DEFAULT, NULL); |
|
1257 |
|
|
1258 |
if (mode == DEV_ASC_DEC) { |
if (mode == DEV_ASC_DEC) { |
1259 |
memory_device_register(mem, "asc_dma_address_reg", |
memory_device_register(mem, "asc_dma_address_reg", |
1260 |
baseaddr + 0x40000, 4096, dev_asc_address_reg_access, d, |
baseaddr + 0x40000, 4096, dev_asc_address_reg_access, d, |
1261 |
MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK, |
DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK, |
1262 |
(unsigned char *)&d->dma_address_reg_memory[0]); |
(unsigned char *)&d->dma_address_reg_memory[0]); |
1263 |
memory_device_register(mem, "asc_dma", baseaddr + 0x80000, |
memory_device_register(mem, "asc_dma", baseaddr + 0x80000, |
1264 |
ASC_DMA_SIZE, dev_asc_dma_access, d, |
ASC_DMA_SIZE, dev_asc_dma_access, d, |
1265 |
MEM_DYNTRANS_OK | MEM_DYNTRANS_WRITE_OK, d->dma); |
DM_DYNTRANS_OK | DM_DYNTRANS_WRITE_OK, d->dma); |
1266 |
} |
} |
1267 |
|
|
1268 |
machine_add_tickfunction(machine, dev_asc_tick, d, ASC_TICK_SHIFT); |
machine_add_tickfunction(machine, dev_asc_tick, d, ASC_TICK_SHIFT); |