File: dev_zs.c

package info (click to toggle)
mips64emul 0.2.4-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,260 kB
  • ctags: 11,173
  • sloc: ansic: 43,056; sh: 535; asm: 326; makefile: 58
file content (207 lines) | stat: -rw-r--r-- 5,617 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 *  Copyright (C) 2004  Anders Gavare.  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright  
 *     notice, this list of conditions and the following disclaimer in the 
 *     documentation and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE   
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 *  SUCH DAMAGE.
 *   
 *
 *  $Id: dev_zs.c,v 1.16 2005/01/09 01:55:25 debug Exp $
 *  
 *  Zilog serial controller, used by (at least) the SGI emulation mode.
 *
 *  TODO:  Implement this correctly.  The values in here are too
 *  hardcoded, and the controller should be able to handle 2 serial lines.
 *
 *  Right now it only barely works with NetSBD/sgimips.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "memory.h"
#include "misc.h"
#include "console.h"
#include "devices.h"


#define	ZS_TICK_SHIFT		14

struct zs_data {
	int		irq_nr;
	int		addrmult;

	int		reg_select;

	int		tx_done;
};


/*  From NetBSD:  */
#define	ZSRR0_RX_READY		1
#define	ZSRR0_TX_READY		4


/*
 *  dev_zs_tick():
 */
void dev_zs_tick(struct cpu *cpu, void *extra)
{
	struct zs_data *d = (struct zs_data *) extra;

	if (console_charavail() || d->tx_done)
		cpu_interrupt(cpu, d->irq_nr);
	else
		cpu_interrupt_ack(cpu, d->irq_nr);
}


/*
 *  dev_zs_access():
 */
int dev_zs_access(struct cpu *cpu, struct memory *mem, uint64_t relative_addr,
	unsigned char *data, size_t len, int writeflag, void *extra)
{
	struct zs_data *d = extra;
	uint64_t idata = 0, odata = 0;
	int port_nr;

	idata = memory_readmax64(cpu, data, len);
	relative_addr /= d->addrmult;

	port_nr = relative_addr / 8;

/*  TODO:  The zs controller has 2 ports...  */
/*	relative_addr &= 7;  */

	switch (relative_addr) {
	case 3:
		if (writeflag==MEM_READ) {
			odata = ZSRR0_TX_READY;
			if (console_charavail())
				odata |= ZSRR0_RX_READY;
			/*  debug("[ zs: read from 0x%08lx: 0x%08x ]\n", (long)relative_addr, odata);  */
		} else {
			/*  Hm...  TODO  */
			if (d->reg_select == 0) {
				d->reg_select = idata;
			} else {
				switch (d->reg_select) {
				case 8:	console_putchar(idata & 255);
					break;
				default:
					debug("[ zs: write to (unimplemented) register 0x%02x: 0x%08x ]\n", d->reg_select, idata);
				}
				d->reg_select = 0;
			}
			/*  debug("[ zs: write to  0x%08lx: 0x%08x ]\n", (long)relative_addr, idata);  */
		}
		break;
	case 7:
		if (writeflag==MEM_READ) {
			if (console_charavail())
				odata = console_readchar();
			else
				odata = 0;
			/*  debug("[ zs: read from 0x%08lx: 0x%08x ]\n", (long)relative_addr, odata);  */
		} else {
			/*  debug("[ zs: write to  0x%08lx: 0x%08x ]\n", (long)relative_addr, idata);  */
			console_putchar(idata & 255);
			d->tx_done = 1;
		}
		break;

/*  hehe, perhaps 0xb and 0xf are the second channel :-)  */

	case 0xb:
		if (writeflag==MEM_READ) {
			odata = 0;
#if 0
	/*  TODO: Weird. Linux needs 4 here, NetBSD wants 0.  */
	odata = 4;
#endif
			if (d->tx_done)
				odata |= 2;
			if (console_charavail())
				odata |= 4;
			d->tx_done = 0;
			debug("[ zs: read from 0x%08lx: 0x%08x ]\n", (long)relative_addr, odata);
		} else {
			debug("[ zs: write to  0x%08lx: 0x%08x ]\n", (long)relative_addr, idata);
		}
		break;

	/*  0xf is used by Linux:  */
	case 0xf:
		if (writeflag==MEM_READ) {
			if (console_charavail())
				odata = console_readchar();
			else
				odata = 0;
			/*  debug("[ zs: read from 0x%08lx: 0x%08x ]\n", (long)relative_addr, odata);  */
		} else {
			/*  debug("[ zs: write to  0x%08lx: 0x%08x ]\n", (long)relative_addr, idata);  */
			console_putchar(idata & 255);
			d->tx_done = 1;
		}
		break;
	default:
		if (writeflag==MEM_READ) {
			debug("[ zs: read from 0x%08lx ]\n", (long)relative_addr);
		} else {
			debug("[ zs: write to  0x%08lx: 0x%08x ]\n", (long)relative_addr, idata);
		}
	}

	if (writeflag == MEM_READ)
		memory_writemax64(cpu, data, len, odata);

	dev_zs_tick(cpu, extra);

	return 1;
}


/*
 *  dev_zs_init():
 */
void dev_zs_init(struct cpu *cpu, struct memory *mem, uint64_t baseaddr,
	int irq_nr, int addrmult)
{
	struct zs_data *d;

	d = malloc(sizeof(struct zs_data));
	if (d == NULL) {
		fprintf(stderr, "out of memory\n");
		exit(1);
	}
	memset(d, 0, sizeof(struct zs_data));
	d->irq_nr   = irq_nr;
	d->addrmult = addrmult;

	memory_device_register(mem, "zs", baseaddr, DEV_ZS_LENGTH * addrmult,
	    dev_zs_access, d, MEM_DEFAULT, NULL);

	cpu_add_tickfunction(cpu, dev_zs_tick, d, ZS_TICK_SHIFT);
}