#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: adb_direct.c,v 1.47 2025/09/07 21:31:20 andvar Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/device.h>
#include <machine/cpu.h>
#include <machine/autoconf.h>
#include <machine/adbsys.h>
#include <machine/pio.h>
#include <macppc/dev/viareg.h>
#include <macppc/dev/adbvar.h>
#include <macppc/dev/pm_direct.h>
#define printf_intr printf
#ifdef DEBUG
#ifndef ADB_DEBUG
#define ADB_DEBUG
#endif
#endif
#define vPB 0x0000
#define vPB3 0x08
#define vPB4 0x10
#define vPB5 0x20
#define vSR_INT 0x04
#define vSR_OUT 0x10
#define ADB_ACTION_NOTREADY 0x1
#define ADB_ACTION_IDLE 0x2
#define ADB_ACTION_OUT 0x3
#define ADB_ACTION_IN 0x4
#define ADB_ACTION_POLLING 0x5
#define ADB_BUS_UNKNOWN 0x1
#define ADB_BUS_IDLE 0x2
#define ADB_BUS_CMD 0x3
#define ADB_BUS_ODD 0x4
#define ADB_BUS_EVEN 0x5
#define ADB_BUS_ACTIVE 0x6
#define ADB_BUS_ACK 0x7
#define ADB_SET_STATE_IDLE_CUDA() via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
#define ADB_SET_STATE_TIP() via_reg_and(VIA1, vBufB, ~vPB5)
#define ADB_CLR_STATE_TIP() via_reg_or(VIA1, vBufB, vPB5)
#define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4)
#define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4)
#define ADB_SET_SR_INPUT() via_reg_and(VIA1, vACR, ~vSR_OUT)
#define ADB_SET_SR_OUTPUT() via_reg_or(VIA1, vACR, vSR_OUT)
#define ADB_SR() read_via_reg(VIA1, vSR)
#define ADB_VIA_INTR_ENABLE() write_via_reg(VIA1, vIER, 0x84)
#define ADB_VIA_INTR_DISABLE() write_via_reg(VIA1, vIER, 0x04)
#define ADB_INTR_IS_OFF (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3))
#define ADB_INTR_IS_ON (0 == (read_via_reg(VIA1, vBufB) & vPB3))
#define ADB_SR_INTR_IS_OFF (0 == (read_via_reg(VIA1, vIFR) & vSR_INT))
#define ADB_SR_INTR_IS_ON (vSR_INT == (read_via_reg(VIA1, \
vIFR) & vSR_INT))
#define ADB_DELAY 150
#define ADB_MAX_MSG_LENGTH 16
#define ADB_MAX_HDR_LENGTH 8
#define ADB_QUEUE 32
#define ADB_TICKLE_TICKS 4
struct ADBDevEntry {
void (*ServiceRtPtr)(void);
void *DataAreaAddr;
int devType;
int origAddr;
int currentAddr;
};
struct adbCmdHoldEntry {
u_char outBuf[ADB_MAX_MSG_LENGTH];
u_char *saveBuf;
adbComp *compRout;
int *data;
};
struct adbCommand {
u_char header[ADB_MAX_HDR_LENGTH];
u_char data[ADB_MAX_MSG_LENGTH];
u_char *saveBuf;
adbComp *compRout;
volatile int *compData;
u_int cmd;
u_int unsol;
u_int ack_only;
};
int adbHardware = ADB_HW_UNKNOWN;
int adbActionState = ADB_ACTION_NOTREADY;
int adbWaiting = 0;
int adbWriteDelay = 0;
int adbWaitingCmd = 0;
u_char *adbBuffer = (long)0;
adbComp *adbCompRout = NULL;
volatile int *adbCompData = NULL;
int adbStarting = 1;
u_char adbInputBuffer[ADB_MAX_MSG_LENGTH];
u_char adbOutputBuffer[ADB_MAX_MSG_LENGTH];
int adbSentChars = 0;
struct ADBDevEntry ADBDevTable[16];
int ADBNumDevices;
struct adbCommand adbInbound[ADB_QUEUE];
int adbInCount = 0;
int adbInHead = 0;
int adbInTail = 0;
struct adbCommand adbOutbound[ADB_QUEUE];
int adbOutCount = 0;
int adbOutHead = 0;
int adbOutTail = 0;
int tickle_count = 0;
int tickle_serial = 0;
int adb_cuda_serial = 0;
struct callout adb_cuda_tickle_ch;
struct callout adb_soft_intr_ch;
volatile uint8_t *Via1Base;
extern int adb_polling;
void pm_setup_adb(void);
void pm_check_adb_devices(int);
int pm_adb_op(u_char *, void *, volatile void *, int);
void pm_init_adb_device(void);
#ifdef ADB_DEBUG
void print_single(u_char *);
#endif
void adb_soft_intr(void);
int send_adb_cuda(u_char *, u_char *, adbComp *, volatile void *, int);
void adb_intr_cuda_test(void);
void adb_cuda_tickle(void);
void adb_pass_up(struct adbCommand *);
void adb_op_comprout(void *, volatile int *, int);
void adb_reinit(void);
int count_adbs(void);
int get_ind_adb_info(ADBDataBlock *, int);
int get_adb_info(ADBDataBlock *, int);
int set_adb_info(ADBSetInfoBlock *, int);
void adb_setup_hw_type(void);
int adb_op (Ptr, adbComp *, volatile void *, short);
int adb_op_sync(Ptr, adbComp *, Ptr, short);
void adb_hw_setup(void);
int adb_cmd_result(u_char *);
int adb_cmd_extra(u_char *);
int send_adb(u_char *, void *, void *);
int setsoftadb(void);
#ifdef ADB_DEBUG
void
print_single(u_char *str)
{
int x;
if (str == 0) {
printf_intr("no data - null pointer\n");
return;
}
if (*str == 0) {
printf_intr("nothing returned\n");
return;
}
if (*str > 20) {
printf_intr("ADB: ACK > 20 no way!\n");
*str = 20;
}
printf_intr("(length=0x%x):", *str);
for (x = 1; x <= *str; x++)
printf_intr(" 0x%02x", str[x]);
printf_intr("\n");
}
#endif
void
adb_cuda_tickle(void)
{
volatile int s;
if (adbActionState == ADB_ACTION_IN) {
if (tickle_serial == adb_cuda_serial) {
if (++tickle_count > 0) {
s = splhigh();
adbActionState = ADB_ACTION_IDLE;
adbInputBuffer[0] = 0;
ADB_SET_STATE_IDLE_CUDA();
splx(s);
}
} else {
tickle_serial = adb_cuda_serial;
tickle_count = 0;
}
} else {
tickle_serial = adb_cuda_serial;
tickle_count = 0;
}
callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
(void *)adb_cuda_tickle, NULL);
}
int
adb_intr_cuda(void *arg)
{
volatile int i, ending;
volatile unsigned int s;
struct adbCommand packet;
uint8_t reg;
s = splhigh();
reg = read_via_reg(VIA1, vIFR);
if ((reg & 0x80) == 0) {
splx(s);
return 0;
}
write_via_reg(VIA1, vIFR, reg & 0x7f);
ADB_VIA_INTR_DISABLE();
switch_start:
switch (adbActionState) {
case ADB_ACTION_IDLE:
adbInputBuffer[1] = ADB_SR();
adb_cuda_serial++;
if (ADB_INTR_IS_OFF)
break;
ADB_SET_SR_INPUT();
ADB_SET_STATE_TIP();
adbInputBuffer[0] = 1;
adbActionState = ADB_ACTION_IN;
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("idle 0x%02x ", adbInputBuffer[1]);
#endif
break;
case ADB_ACTION_IN:
adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
if (ADB_INTR_IS_OFF)
ending = 1;
else
ending = 0;
if (1 == ending) {
#ifdef ADB_DEBUG
if (adb_debug) {
printf_intr("in end 0x%02x ",
adbInputBuffer[adbInputBuffer[0]]);
print_single(adbInputBuffer);
}
#endif
memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
if ((adbWaiting == 1) &&
(adbInputBuffer[4] == adbWaitingCmd) &&
((adbInputBuffer[2] == 0x00) ||
(adbInputBuffer[2] == 0x01))) {
packet.saveBuf = adbBuffer;
packet.compRout = adbCompRout;
packet.compData = adbCompData;
packet.unsol = 0;
packet.ack_only = 0;
adb_pass_up(&packet);
adbWaitingCmd = 0;
adbWaiting = 0;
adbBuffer = (long)0;
adbCompRout = (long)0;
adbCompData = (long)0;
} else {
packet.unsol = 1;
packet.ack_only = 0;
adb_pass_up(&packet);
}
adbActionState = ADB_ACTION_IDLE;
adbInputBuffer[0] = 0;
ADB_SET_STATE_IDLE_CUDA();
if (adbWriteDelay == 1) {
delay(ADB_DELAY);
adbSentChars = 0;
adbActionState = ADB_ACTION_OUT;
if (ADB_INTR_IS_ON) {
ADB_SET_SR_INPUT();
ADB_SET_STATE_IDLE_CUDA();
adbSentChars = 0;
adbActionState = ADB_ACTION_IDLE;
adbInputBuffer[0] = 0;
break;
}
ADB_SET_STATE_TIP();
ADB_SET_SR_OUTPUT();
write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
}
} else {
ADB_TOGGLE_STATE_ACK_CUDA();
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("in 0x%02x ",
adbInputBuffer[adbInputBuffer[0]]);
#endif
}
break;
case ADB_ACTION_OUT:
i = ADB_SR();
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("intr out 0x%02x ", i);
#endif
adbSentChars++;
if (ADB_INTR_IS_ON) {
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("intr was on ");
#endif
ADB_SET_SR_INPUT();
ADB_SET_STATE_IDLE_CUDA();
adbSentChars = 0;
adbActionState = ADB_ACTION_IDLE;
adbInputBuffer[0] = 0;
adbWriteDelay = 1;
delay(ADB_DELAY);
goto switch_start;
break;
}
if (adbOutputBuffer[0] == adbSentChars) {
if (0 == adb_cmd_result(adbOutputBuffer)) {
adbWaiting = 1;
adbWaitingCmd = adbOutputBuffer[2];
} else {
memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
packet.saveBuf = adbBuffer;
packet.compRout = adbCompRout;
packet.compData = adbCompData;
packet.cmd = adbWaitingCmd;
packet.unsol = 0;
packet.ack_only = 1;
adb_pass_up(&packet);
adbWaitingCmd = 0;
adbBuffer = (long)0;
adbCompRout = NULL;
adbCompData = NULL;
}
adbWriteDelay = 0;
adbActionState = ADB_ACTION_IDLE;
ADB_SET_SR_INPUT();
ADB_SET_STATE_IDLE_CUDA();
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("write done ");
#endif
} else {
write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
ADB_TOGGLE_STATE_ACK_CUDA();
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("toggle ");
#endif
}
break;
case ADB_ACTION_NOTREADY:
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("adb: not yet initialized\n");
#endif
break;
default:
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("intr: unknown ADB state\n");
#endif
break;
}
ADB_VIA_INTR_ENABLE();
splx(s);
return 1;
}
int
send_adb_cuda(u_char * in, u_char * buffer, adbComp *compRout,
volatile void *data, int command)
{
int s, len;
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("SEND\n");
#endif
if (adbActionState == ADB_ACTION_NOTREADY)
return 1;
s = splhigh();
if ((adbActionState == ADB_ACTION_IDLE) &&
(ADB_INTR_IS_OFF)) {
} else
if (adbWriteDelay == 0)
adbWriteDelay = 1;
else {
splx(s);
return 1;
}
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("QUEUE\n");
#endif
if ((long)in == (long)0) {
if ((command & 0x0c) == 0x08)
len = buffer[0];
else
len = 0;
adbOutputBuffer[0] = 2 + len;
adbOutputBuffer[1] = 0x00;
adbOutputBuffer[2] = (u_char)command;
memcpy(adbOutputBuffer + 3, buffer + 1, len);
} else
memcpy(adbOutputBuffer, in, in[0] + 2);
adbSentChars = 0;
adbBuffer = buffer;
adbCompRout = compRout;
adbCompData = data;
adbWaitingCmd = adbOutputBuffer[2];
if (adbWriteDelay != 1) {
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("out start NOW");
#endif
delay(ADB_DELAY);
adbActionState = ADB_ACTION_OUT;
ADB_SET_SR_OUTPUT();
write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
ADB_SET_STATE_ACKOFF_CUDA();
ADB_SET_STATE_TIP();
}
adbWriteDelay = 1;
splx(s);
if ((s & (1 << 18)) || adb_polling)
while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
|| (adbWaiting == 1))
if (ADB_SR_INTR_IS_ON) {
adb_intr_cuda(NULL);
adb_soft_intr();
}
return 0;
}
int
adb_intr(void *arg)
{
switch (adbHardware) {
case ADB_HW_PMU:
return pm_intr(arg);
break;
case ADB_HW_CUDA:
return adb_intr_cuda(arg);
break;
case ADB_HW_UNKNOWN:
break;
}
return 0;
}
void
adb_pass_up(struct adbCommand *in)
{
int start = 0, len = 0, cmd = 0;
ADBDataBlock block;
if (adbInCount >= ADB_QUEUE) {
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("adb: ring buffer overflow\n");
#endif
return;
}
if (in->ack_only) {
len = in->data[0];
cmd = in->cmd;
start = 0;
} else {
switch (adbHardware) {
case ADB_HW_CUDA:
if (in->unsol)
if (0 != in->data[2])
return;
cmd = in->data[4];
if (in->data[0] < 5)
len = 0;
else
len = in->data[0]-4;
start = 4;
break;
case ADB_HW_PMU:
cmd = in->data[1];
if (in->data[0] < 2)
len = 0;
else
len = in->data[0]-1;
start = 1;
break;
case ADB_HW_UNKNOWN:
return;
}
if (in->unsol) {
if (adbStarting)
return;
if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
return;
}
}
if (in->unsol) {
adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
} else {
adbInbound[adbInTail].compRout = in->compRout;
adbInbound[adbInTail].compData = in->compData;
adbInbound[adbInTail].saveBuf = in->saveBuf;
}
#ifdef ADB_DEBUG
if (adb_debug && in->data[1] == 2)
printf_intr("adb: caught error\n");
#endif
memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
adbInbound[adbInTail].data[0] = len;
adbInbound[adbInTail].cmd = cmd;
adbInCount++;
if (++adbInTail >= ADB_QUEUE)
adbInTail = 0;
if (adb_polling)
adb_soft_intr();
else
setsoftadb();
return;
}
void
adb_soft_intr(void)
{
int s;
int cmd = 0;
u_char *buffer = 0;
adbComp *comprout = NULL;
volatile int *compdata = 0;
#if 0
s = splhigh();
printf_intr("sr: %x\n", (s & 0x0700));
splx(s);
#endif
while (adbInCount) {
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("%x %x %x ",
adbInCount, adbInHead, adbInTail);
#endif
buffer = adbInbound[adbInHead].saveBuf;
comprout = adbInbound[adbInHead].compRout;
compdata = adbInbound[adbInHead].compData;
cmd = adbInbound[adbInHead].cmd;
if (buffer)
memcpy(buffer, adbInbound[adbInHead].data,
adbInbound[adbInHead].data[0] + 1);
#ifdef ADB_DEBUG
if (adb_debug & 0x80) {
printf_intr("%p %p %p %x ",
buffer, comprout, compdata, (short)cmd);
printf_intr("buf: ");
print_single(adbInbound[adbInHead].data);
}
#endif
s = splhigh();
adbInCount--;
if (++adbInHead >= ADB_QUEUE)
adbInHead = 0;
splx(s);
if (comprout)
(*comprout)(buffer, compdata, cmd);
}
return;
}
int
adb_op(Ptr buffer, adbComp *compRout, volatile void *data, short command)
{
int result;
switch (adbHardware) {
case ADB_HW_PMU:
result = pm_adb_op((u_char *)buffer, compRout,
data, (int)command);
if (result == 0)
return 0;
else
return -1;
break;
case ADB_HW_CUDA:
result = send_adb_cuda((u_char *)0, (u_char *)buffer,
compRout, data, (int)command);
if (result == 0)
return 0;
else
return -1;
break;
case ADB_HW_UNKNOWN:
default:
return -1;
}
}
void
adb_hw_setup(void)
{
volatile int i;
switch (adbHardware) {
case ADB_HW_PMU:
write_via_reg(VIA1, vIFR, 0x90);
break;
case ADB_HW_CUDA:
via_reg_or(VIA1, vDirB, 0x30);
via_reg_and(VIA1, vDirB, 0xf7);
via_reg_and(VIA1, vACR, ~vSR_OUT);
write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
adbActionState = ADB_ACTION_IDLE;
write_via_reg(VIA1, vIER, 0x84);
ADB_SET_STATE_IDLE_CUDA();
i = ADB_SR();
ADB_VIA_INTR_DISABLE();
ADB_SET_STATE_IDLE_CUDA();
delay(ADB_DELAY);
ADB_SET_STATE_TIP();
delay(ADB_DELAY);
ADB_TOGGLE_STATE_ACK_CUDA();
delay(ADB_DELAY);
ADB_CLR_STATE_TIP();
delay(ADB_DELAY);
ADB_SET_STATE_IDLE_CUDA();
i = ADB_SR();
ADB_VIA_INTR_ENABLE();
break;
case ADB_HW_UNKNOWN:
default:
write_via_reg(VIA1, vIER, 0x04);
return;
break;
}
}
void
adb_reinit(void)
{
u_char send_string[ADB_MAX_MSG_LENGTH];
ADBDataBlock data;
volatile int i, x;
int s = 0;
int command;
int result;
int saveptr;
int device;
int nonewtimes;
static bool callo;
if (!callo) {
callo = true;
callout_init(&adb_cuda_tickle_ch, 0);
callout_init(&adb_soft_intr_ch, 0);
}
if (adbHardware != ADB_HW_PMU)
s = splhigh();
ADBNumDevices = 0;
adbStarting = 1;
for (i = 0; i < 16; i++)
ADBDevTable[i].devType = 0;
adb_setup_hw_type();
adb_hw_setup();
delay(1000);
result = adb_op_sync((Ptr)0, NULL, (Ptr)0, (short)0x00);
delay(200000);
#ifdef ADB_DEBUG
if (result && adb_debug) {
printf_intr("adb_reinit: failed to reset, result = %d\n",result);
}
#endif
for (i = 1; i < 16; i++) {
send_string[0] = 0;
command = ADBTALK(i, 3);
result = adb_op_sync((Ptr)send_string, NULL,
(Ptr)0, (short)command);
#ifdef ADB_DEBUG
if (result && adb_debug) {
printf_intr("adb_reinit: scan of device %d, result = %d, str = 0x%x\n",
i,result,send_string[0]);
}
#endif
if (send_string[0] != 0) {
switch (send_string[2]) {
case 0:
case 0xfd:
case 0xfe:
case 0xff:
continue;
}
++ADBNumDevices;
KASSERT(ADBNumDevices < 16);
ADBDevTable[ADBNumDevices].devType =
(int)send_string[2];
ADBDevTable[ADBNumDevices].origAddr = i;
ADBDevTable[ADBNumDevices].currentAddr = i;
ADBDevTable[ADBNumDevices].DataAreaAddr =
(long)0;
ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
pm_check_adb_devices(i);
}
}
for (saveptr = 15; saveptr > 0; saveptr--)
if (-1 == get_adb_info(&data, saveptr))
break;
#ifdef ADB_DEBUG
if (adb_debug & 0x80) {
printf_intr("first free is: 0x%02x\n", saveptr);
printf_intr("devices: %i\n", ADBNumDevices);
}
#endif
nonewtimes = 0;
while (saveptr > 0 && nonewtimes++ < 11) {
for (i = 1; i <= ADBNumDevices; i++) {
device = ADBDevTable[i].currentAddr;
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("moving device 0x%02x to 0x%02x "
"(index 0x%02x) ", device, saveptr, i);
#endif
command = ADBTALK(device, 3);
adb_op_sync((Ptr)send_string, NULL,
(Ptr)0, (short)command);
command = ADBLISTEN(device, 3);
send_string[0] = 2;
send_string[1] = (u_char)(saveptr | 0x60);
send_string[2] = 0xfe;
adb_op_sync((Ptr)send_string, NULL,
(Ptr)0, (short)command);
delay(500);
command = ADBTALK(saveptr, 3);
adb_op_sync((Ptr)send_string, NULL,
(Ptr)0, (short)command);
delay(500);
if (send_string[0] == 0) {
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("failed, continuing\n");
#endif
continue;
}
command = ADBTALK(device, 3);
result = adb_op_sync((Ptr)send_string, NULL,
(Ptr)0, (short)command);
if (send_string[0] != 0) {
switch (send_string[2]) {
case 0:
case 0xfd:
case 0xfe:
case 0xff:
continue;
}
ADBDevTable[i].currentAddr = saveptr;
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("old device at index %i\n",i);
#endif
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("new device found\n");
#endif
if (saveptr > ADBNumDevices) {
++ADBNumDevices;
KASSERT(ADBNumDevices < 16);
}
ADBDevTable[ADBNumDevices].devType =
(int)send_string[2];
ADBDevTable[ADBNumDevices].origAddr = device;
ADBDevTable[ADBNumDevices].currentAddr = device;
ADBDevTable[ADBNumDevices].DataAreaAddr =
(long)0;
ADBDevTable[ADBNumDevices].ServiceRtPtr =
(void *)0;
for (x = saveptr; x > 0; x--) {
if (-1 == get_adb_info(&data, x)) {
saveptr = x;
break;
}
}
if (x == 0)
saveptr = 0;
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("new free is 0x%02x\n",
saveptr);
#endif
nonewtimes = 0;
pm_check_adb_devices(device);
} else {
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("moving back...\n");
#endif
command = ADBLISTEN(saveptr, 3);
send_string[0] = 2;
send_string[1] = (u_char)(device | 0x60);
send_string[2] = 0xfe;
adb_op_sync((Ptr)send_string, NULL,
(Ptr)0, (short)command);
delay(1000);
}
}
}
#ifdef ADB_DEBUG
if (adb_debug) {
for (i = 1; i <= ADBNumDevices; i++) {
x = get_ind_adb_info(&data, i);
if (x != -1)
printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
i, x, data.devType);
}
}
#endif
#ifdef ADB_DEBUG
if (adb_debug) {
if (0 == ADBNumDevices)
printf_intr("adb: no devices found\n");
}
#endif
adbStarting = 0;
#ifdef ADB_DEBUG
if (adb_debug)
printf_intr("adb: ADBReInit complete\n");
#endif
if (adbHardware == ADB_HW_CUDA)
callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
(void *)adb_cuda_tickle, NULL);
if (adbHardware != ADB_HW_PMU)
splx(s);
}
int
adb_cmd_result(u_char *in)
{
switch (adbHardware) {
case ADB_HW_CUDA:
if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
return 0;
if ((in[1] == 0x01) && (in[2] == 0x03))
return 0;
return 1;
case ADB_HW_PMU:
return 1;
case ADB_HW_UNKNOWN:
default:
return 1;
}
}
int
adb_cmd_extra(u_char *in)
{
switch (adbHardware) {
case ADB_HW_CUDA:
if ((in[2] & 0x0c) == 0x08)
return 0;
return 1;
case ADB_HW_PMU:
return 1;
case ADB_HW_UNKNOWN:
default:
return 1;
}
}
int
adb_op_sync(Ptr buffer, adbComp *compRout, Ptr data, short command)
{
int tmout;
int result;
volatile int flag = 0;
result = adb_op(buffer, adb_op_comprout,
&flag, command);
if (result == 0) {
for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
delay(10);
if (!flag && tmout > 0)
delay(tmout);
if (!flag)
result = -2;
}
return result;
}
void
adb_op_comprout(void *buffer, volatile int *compdata, int cmd)
{
volatile int *p = compdata;
*p = 1;
}
void
adb_setup_hw_type(void)
{
switch (adbHardware) {
case ADB_HW_CUDA:
return;
case ADB_HW_PMU:
pm_setup_adb();
return;
default:
panic("unknown adb hardware");
}
}
int
count_adbs(void)
{
int i;
int found;
found = 0;
for (i = 1; i < 16; i++)
if (0 != ADBDevTable[i].devType)
found++;
return found;
}
int
get_ind_adb_info(ADBDataBlock * info, int index)
{
if ((index < 1) || (index > 15))
return (-1);
#ifdef ADB_DEBUG
if (adb_debug & 0x80)
printf_intr("index 0x%x devType is: 0x%x\n", index,
ADBDevTable[index].devType);
#endif
if (0 == ADBDevTable[index].devType)
return (-1);
info->devType = ADBDevTable[index].devType;
info->origADBAddr = ADBDevTable[index].origAddr;
info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
return (ADBDevTable[index].currentAddr);
}
int
get_adb_info(ADBDataBlock * info, int adbAddr)
{
int i;
if ((adbAddr < 1) || (adbAddr > 15))
return (-1);
for (i = 1; i < 15; i++)
if (ADBDevTable[i].currentAddr == adbAddr) {
info->devType = ADBDevTable[i].devType;
info->origADBAddr = ADBDevTable[i].origAddr;
info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
return 0;
}
return (-1);
}
int
set_adb_info(ADBSetInfoBlock * info, int adbAddr)
{
int i;
if ((adbAddr < 1) || (adbAddr > 15))
return (-1);
for (i = 1; i < 15; i++)
if (ADBDevTable[i].currentAddr == adbAddr) {
ADBDevTable[i].ServiceRtPtr =
(void *)(info->siServiceRtPtr);
ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
return 0;
}
return (-1);
}
#ifndef MRG_ADB
int
adb_read_date_time(unsigned long *t)
{
u_char output[ADB_MAX_MSG_LENGTH];
int result;
volatile int flag = 0;
switch (adbHardware) {
case ADB_HW_PMU:
pm_read_date_time(t);
return 0;
case ADB_HW_CUDA:
output[0] = 0x02;
output[1] = 0x01;
output[2] = 0x03;
result = send_adb_cuda((u_char *)output, (u_char *)output,
adb_op_comprout, &flag, (int)0);
if (result != 0)
return -1;
while (0 == flag)
;
memcpy(t, output + 1, 4);
return 0;
case ADB_HW_UNKNOWN:
default:
return -1;
}
}
int
adb_set_date_time(unsigned long t)
{
u_char output[ADB_MAX_MSG_LENGTH];
int result;
volatile int flag = 0;
switch (adbHardware) {
case ADB_HW_CUDA:
output[0] = 0x06;
output[1] = 0x01;
output[2] = 0x09;
output[3] = (u_char)(t >> 24);
output[4] = (u_char)(t >> 16);
output[5] = (u_char)(t >> 8);
output[6] = (u_char)(t);
result = send_adb_cuda((u_char *)output, (u_char *)0,
adb_op_comprout, &flag, (int)0);
if (result != 0)
return -1;
while (0 == flag)
;
return 0;
case ADB_HW_PMU:
pm_set_date_time(t);
return 0;
case ADB_HW_UNKNOWN:
default:
return -1;
}
}
int
adb_poweroff(void)
{
u_char output[ADB_MAX_MSG_LENGTH];
int result;
adb_polling = 1;
switch (adbHardware) {
case ADB_HW_PMU:
pm_adb_poweroff();
for (;;);
return 0;
case ADB_HW_CUDA:
output[0] = 0x02;
output[1] = 0x01;
output[2] = 0x0a;
result = send_adb_cuda((u_char *)output, (u_char *)0,
(void *)0, (void *)0, (int)0);
if (result != 0)
return -1;
for (;;);
return 0;
case ADB_HW_UNKNOWN:
default:
return -1;
}
}
int
CountADBs(void)
{
return (count_adbs());
}
void
ADBReInit(void)
{
adb_reinit();
}
int
GetIndADB(ADBDataBlock * info, int index)
{
return (get_ind_adb_info(info, index));
}
int
GetADBInfo(ADBDataBlock * info, int adbAddr)
{
return (get_adb_info(info, adbAddr));
}
int
SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
{
return (set_adb_info(info, adbAddr));
}
int
ADBOp(Ptr buffer, adbComp *compRout, Ptr data, short commandNum)
{
return (adb_op(buffer, compRout, data, commandNum));
}
#endif
int
setsoftadb(void)
{
callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
return 0;
}
void
adb_cuda_autopoll(void)
{
volatile int flag = 0;
int result;
u_char output[16];
output[0] = 0x03;
output[1] = 0x01;
output[2] = 0x01;
output[3] = 0x01;
result = send_adb_cuda(output, output, adb_op_comprout,
&flag, 0);
if (result != 0)
return;
while (flag == 0);
}
void
adb_restart(void)
{
int result;
u_char output[16];
adb_polling = 1;
switch (adbHardware) {
case ADB_HW_CUDA:
output[0] = 0x02;
output[1] = 0x01;
output[2] = 0x11;
result = send_adb_cuda(output, NULL, NULL, NULL, 0);
if (result != 0)
return;
while (1);
case ADB_HW_PMU:
pm_adb_restart();
while (1);
}
}