summaryrefslogtreecommitdiff
path: root/sys/arch/playstation2/dev/spd.c
blob: 307c7e98cb5edc27bc744cfb4c1df481c16c5137 (plain)
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*	$NetBSD: spd.c,v 1.16 2022/02/11 17:30:48 riastradh Exp $	*/

/*-
 * Copyright (c) 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by UCHIYAMA Yasushi.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: spd.c,v 1.16 2022/02/11 17:30:48 riastradh Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>

#include <machine/bootinfo.h>

#include <playstation2/ee/eevar.h>
#include <playstation2/dev/sbusvar.h>
#include <playstation2/dev/spdvar.h>
#include <playstation2/dev/spdreg.h>

#ifdef DEBUG
#define STATIC
#else
#define STATIC static
#endif

STATIC int spd_match(device_t, cfdata_t, void *);
STATIC void spd_attach(device_t, device_t, void *);
STATIC int spd_print(void *, const char *);
STATIC int spd_intr(void *);
STATIC void __spd_eeprom_out(u_int8_t *, int);
STATIC int __spd_eeprom_in(u_int8_t *);

/* SPD device can't attach twice. because PS2 PC-Card slot is only one. */
STATIC struct {
	int (*func)(void *);
	void *arg;
	const char *name;
} __spd_table[2];

CFATTACH_DECL_NEW(spd, 0,
    spd_match, spd_attach, NULL, NULL);

#ifdef DEBUG
#define LEGAL_SLOT(slot)	((slot) >= 0 && (slot) < 2)
#endif

int
spd_match(device_t parent, cfdata_t cf, void *aux)
{
	
	return ((BOOTINFO_REF(BOOTINFO_DEVCONF) ==
	    BOOTINFO_DEVCONF_SPD_PRESENT));
}

void
spd_attach(device_t parent, device_t self, void *aux)
{
	struct spd_attach_args spa;

	printf(": PlayStation 2 HDD Unit\n");

	switch (BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE)) {
	default:
		__spd_table[0].name = "<unknown product>";
		break;
	case 0:
		/* FALLTHROUGH */
	case 1:
		/* FALLTHROUGH */
	case 2:
		__spd_table[SPD_HDD].name = "SCPH-20400";
		__spd_table[SPD_NIC].name = "SCPH-10190";
		break;
	case 3:
		__spd_table[SPD_HDD].name = "SCPH-10260";
		__spd_table[SPD_NIC].name = "SCPH-10260";
		break;
	}

	/* disable all */
	_reg_write_2(SPD_INTR_ENABLE_REG16, 0);
	_reg_write_2(SPD_INTR_CLEAR_REG16, _reg_read_2(SPD_INTR_STATUS_REG16));

	spa.spa_slot = SPD_HDD;
	spa.spa_product_name = __spd_table[SPD_HDD].name;
	config_found(self, &spa, spd_print, CFARGS_NONE);

	spa.spa_slot = SPD_NIC;
	spa.spa_product_name = __spd_table[SPD_NIC].name;
	config_found(self, &spa, spd_print, CFARGS_NONE);

	sbus_intr_establish(SBUS_IRQ_PCMCIA, spd_intr, 0);
}

int
spd_print(void *aux, const char *pnp)
{
	struct spd_attach_args *spa = aux;

	if (pnp)
		aprint_normal("%s at %s", __spd_table[spa->spa_slot].name, pnp);

	return (UNCONF);
}

int
spd_intr(void *arg)
{
	u_int16_t r;
	
	r = _reg_read_2(SPD_INTR_STATUS_REG16);

 	/* HDD (SCPH-20400) */
	if ((r & SPD_INTR_HDD) != 0)
		if (__spd_table[SPD_HDD].func != NULL)
			(*__spd_table[SPD_HDD].func)(__spd_table[SPD_HDD].arg);

	/* Network (SCPH-10190) */
	if ((r & (SPD_INTR_EMAC3 | SPD_INTR_RXEND | SPD_INTR_TXEND |
	    SPD_INTR_RXDNV | SPD_INTR_TXDNV)) != 0)
		if (__spd_table[SPD_NIC].func)
			(*__spd_table[SPD_NIC].func)(__spd_table[SPD_NIC].arg);

	/* reinstall */
	r = _reg_read_2(SPD_INTR_ENABLE_REG16);
	_reg_write_2(SPD_INTR_ENABLE_REG16, 0);
	_reg_write_2(SPD_INTR_ENABLE_REG16, r);

	return (1);
}

void *
spd_intr_establish(enum spd_slot slot, int (*func)(void *), void *arg)
{

	KDASSERT(LEGAL_SLOT(slot));
	KDASSERT(__spd_table[slot].func == 0);

	__spd_table[slot].func = func;
	__spd_table[slot].arg = arg;

	return ((void *)slot);
}

void
spd_intr_disestablish(void *handle)
{
	int slot = (int)handle;

	KDASSERT(LEGAL_SLOT(slot));

	__spd_table[slot].func = 0;
}

/*
 * EEPROM access
 */
void
spd_eeprom_read(int addr, u_int16_t *data, int n)
{
	int i, j, s;
	u_int8_t r;

	s = _intr_suspend();

	/* set direction */
	_reg_write_1(SPD_IO_DIR_REG8, SPD_IO_CLK | SPD_IO_CS | SPD_IO_IN);

	/* chip select high */
	r = 0;
	_reg_write_1(SPD_IO_DATA_REG8, r);
	delay(1);
	r |= SPD_IO_CS;
	r &= ~(SPD_IO_IN | SPD_IO_CLK);
	_reg_write_1(SPD_IO_DATA_REG8, r);	
	delay(1);

	/* put start bit */
	__spd_eeprom_out(&r, 1);

	/* put op code (read) */
	__spd_eeprom_out(&r, 1);
	__spd_eeprom_out(&r, 0);

	/* set address */
	for (i = 0; i < 6; i++, addr <<= 1)
		__spd_eeprom_out(&r, addr & 0x20);
	
	/* get data */
	for (i = 0; i < n; i++, data++)
		for (*data = 0, j = 15; j >= 0; j--)
			*data |= (__spd_eeprom_in(&r) << j);

	/* chip select low */
	r &= ~(SPD_IO_CS | SPD_IO_IN | SPD_IO_CLK);
	_reg_write_1(SPD_IO_DATA_REG8, r);
	delay(2);

	_intr_resume(s);
}

void
__spd_eeprom_out(u_int8_t *rp, int onoff)
{
	u_int8_t r = *rp;

	if (onoff)
		r |= SPD_IO_IN;
	else
		r &= ~SPD_IO_IN;

	r &= ~SPD_IO_CLK;
	_reg_write_1(SPD_IO_DATA_REG8, r);
	delay(1);
	
	r |= SPD_IO_CLK;
	_reg_write_1(SPD_IO_DATA_REG8, r);
	delay(1);

	r &= ~SPD_IO_CLK;
	_reg_write_1(SPD_IO_DATA_REG8, r);
	delay(1);

	*rp = r;
}

int
__spd_eeprom_in(u_int8_t *rp)
{
	int ret;
	u_int8_t r = *rp;

	r &= ~(SPD_IO_IN | SPD_IO_CLK);
	_reg_write_1(SPD_IO_DATA_REG8, r);
	delay(1);

	r |= SPD_IO_CLK;
	_reg_write_1(SPD_IO_DATA_REG8, r);
	delay(1);
	ret = (_reg_read_1(SPD_IO_DATA_REG8) >> 4) & 0x1;

	r &= ~SPD_IO_CLK;
	_reg_write_1(SPD_IO_DATA_REG8, r);
	delay(1);

	*rp = r;

	return (ret);
}