spd.c revision 1.7 1 1.7 martin /* $NetBSD: spd.c,v 1.7 2008/04/28 20:23:31 martin Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch *
19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
30 1.1 uch */
31 1.5 lukem
32 1.5 lukem #include <sys/cdefs.h>
33 1.7 martin __KERNEL_RCSID(0, "$NetBSD: spd.c,v 1.7 2008/04/28 20:23:31 martin Exp $");
34 1.1 uch
35 1.1 uch #include <sys/param.h>
36 1.1 uch #include <sys/systm.h>
37 1.1 uch
38 1.1 uch #include <machine/bootinfo.h>
39 1.1 uch
40 1.1 uch #include <playstation2/ee/eevar.h>
41 1.1 uch #include <playstation2/dev/sbusvar.h>
42 1.1 uch #include <playstation2/dev/spdvar.h>
43 1.1 uch #include <playstation2/dev/spdreg.h>
44 1.1 uch
45 1.1 uch #ifdef DEBUG
46 1.1 uch #define STATIC
47 1.1 uch #else
48 1.1 uch #define STATIC static
49 1.1 uch #endif
50 1.1 uch
51 1.1 uch STATIC int spd_match(struct device *, struct cfdata *, void *);
52 1.1 uch STATIC void spd_attach(struct device *, struct device *, void *);
53 1.1 uch STATIC int spd_print(void *, const char *);
54 1.1 uch STATIC int spd_intr(void *);
55 1.1 uch STATIC void __spd_eeprom_out(u_int8_t *, int);
56 1.1 uch STATIC int __spd_eeprom_in(u_int8_t *);
57 1.1 uch
58 1.1 uch /* SPD device can't attach twice. because PS2 PC-Card slot is only one. */
59 1.1 uch STATIC struct {
60 1.1 uch int (*func)(void *);
61 1.1 uch void *arg;
62 1.1 uch const char *name;
63 1.1 uch } __spd_table[2];
64 1.1 uch
65 1.3 thorpej CFATTACH_DECL(spd, sizeof(struct device),
66 1.3 thorpej spd_match, spd_attach, NULL, NULL);
67 1.1 uch
68 1.1 uch #ifdef DEBUG
69 1.1 uch #define LEGAL_SLOT(slot) ((slot) >= 0 && (slot) < 2)
70 1.1 uch #endif
71 1.1 uch
72 1.1 uch int
73 1.1 uch spd_match(struct device *parent, struct cfdata *cf, void *aux)
74 1.1 uch {
75 1.1 uch
76 1.1 uch return ((BOOTINFO_REF(BOOTINFO_DEVCONF) ==
77 1.1 uch BOOTINFO_DEVCONF_SPD_PRESENT));
78 1.1 uch }
79 1.1 uch
80 1.1 uch void
81 1.1 uch spd_attach(struct device *parent, struct device *self, void *aux)
82 1.1 uch {
83 1.1 uch struct spd_attach_args spa;
84 1.1 uch
85 1.1 uch printf(": PlayStation 2 HDD Unit\n");
86 1.1 uch
87 1.1 uch switch (BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE)) {
88 1.1 uch default:
89 1.1 uch __spd_table[0].name = "<unknown product>";
90 1.1 uch break;
91 1.1 uch case 0:
92 1.1 uch /* FALLTHROUGH */
93 1.1 uch case 1:
94 1.1 uch /* FALLTHROUGH */
95 1.1 uch case 2:
96 1.1 uch __spd_table[SPD_HDD].name = "SCPH-20400";
97 1.1 uch __spd_table[SPD_NIC].name = "SCPH-10190";
98 1.1 uch break;
99 1.1 uch case 3:
100 1.1 uch __spd_table[SPD_HDD].name = "SCPH-10260";
101 1.1 uch __spd_table[SPD_NIC].name = "SCPH-10260";
102 1.1 uch break;
103 1.1 uch }
104 1.1 uch
105 1.1 uch /* disable all */
106 1.1 uch _reg_write_2(SPD_INTR_ENABLE_REG16, 0);
107 1.1 uch _reg_write_2(SPD_INTR_CLEAR_REG16, _reg_read_2(SPD_INTR_STATUS_REG16));
108 1.1 uch
109 1.1 uch spa.spa_slot = SPD_HDD;
110 1.1 uch spa.spa_product_name = __spd_table[SPD_HDD].name;
111 1.1 uch config_found(self, &spa, spd_print);
112 1.1 uch
113 1.1 uch spa.spa_slot = SPD_NIC;
114 1.1 uch spa.spa_product_name = __spd_table[SPD_NIC].name;
115 1.1 uch config_found(self, &spa, spd_print);
116 1.1 uch
117 1.1 uch sbus_intr_establish(SBUS_IRQ_PCMCIA, spd_intr, 0);
118 1.1 uch }
119 1.1 uch
120 1.1 uch int
121 1.1 uch spd_print(void *aux, const char *pnp)
122 1.1 uch {
123 1.1 uch struct spd_attach_args *spa = aux;
124 1.1 uch
125 1.1 uch if (pnp)
126 1.4 thorpej aprint_normal("%s at %s", __spd_table[spa->spa_slot].name, pnp);
127 1.1 uch
128 1.1 uch return (UNCONF);
129 1.1 uch }
130 1.1 uch
131 1.1 uch int
132 1.1 uch spd_intr(void *arg)
133 1.1 uch {
134 1.1 uch u_int16_t r;
135 1.1 uch
136 1.1 uch r = _reg_read_2(SPD_INTR_STATUS_REG16);
137 1.1 uch
138 1.1 uch /* HDD (SCPH-20400) */
139 1.1 uch if ((r & SPD_INTR_HDD) != 0)
140 1.1 uch if (__spd_table[SPD_HDD].func != NULL)
141 1.1 uch (*__spd_table[SPD_HDD].func)(__spd_table[SPD_HDD].arg);
142 1.1 uch
143 1.1 uch /* Network (SCPH-10190) */
144 1.1 uch if ((r & (SPD_INTR_EMAC3 | SPD_INTR_RXEND | SPD_INTR_TXEND |
145 1.1 uch SPD_INTR_RXDNV | SPD_INTR_TXDNV)) != 0)
146 1.1 uch if (__spd_table[SPD_NIC].func)
147 1.1 uch (*__spd_table[SPD_NIC].func)(__spd_table[SPD_NIC].arg);
148 1.1 uch
149 1.1 uch /* reinstall */
150 1.1 uch r = _reg_read_2(SPD_INTR_ENABLE_REG16);
151 1.1 uch _reg_write_2(SPD_INTR_ENABLE_REG16, 0);
152 1.1 uch _reg_write_2(SPD_INTR_ENABLE_REG16, r);
153 1.1 uch
154 1.1 uch return (1);
155 1.1 uch }
156 1.1 uch
157 1.1 uch void *
158 1.1 uch spd_intr_establish(enum spd_slot slot, int (*func)(void *), void *arg)
159 1.1 uch {
160 1.1 uch
161 1.1 uch KDASSERT(LEGAL_SLOT(slot));
162 1.1 uch KDASSERT(__spd_table[slot].func == 0);
163 1.1 uch
164 1.1 uch __spd_table[slot].func = func;
165 1.1 uch __spd_table[slot].arg = arg;
166 1.1 uch
167 1.1 uch return ((void *)slot);
168 1.1 uch }
169 1.1 uch
170 1.1 uch void
171 1.1 uch spd_intr_disestablish(void *handle)
172 1.1 uch {
173 1.1 uch int slot = (int)handle;
174 1.1 uch
175 1.1 uch KDASSERT(LEGAL_SLOT(slot));
176 1.1 uch
177 1.1 uch __spd_table[slot].func = 0;
178 1.1 uch }
179 1.1 uch
180 1.1 uch /*
181 1.1 uch * EEPROM access
182 1.1 uch */
183 1.1 uch void
184 1.1 uch spd_eeprom_read(int addr, u_int16_t *data, int n)
185 1.1 uch {
186 1.1 uch int i, j, s;
187 1.1 uch u_int8_t r;
188 1.1 uch
189 1.1 uch s = _intr_suspend();
190 1.1 uch
191 1.1 uch /* set direction */
192 1.1 uch _reg_write_1(SPD_IO_DIR_REG8, SPD_IO_CLK | SPD_IO_CS | SPD_IO_IN);
193 1.1 uch
194 1.1 uch /* chip select high */
195 1.1 uch r = 0;
196 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
197 1.1 uch delay(1);
198 1.1 uch r |= SPD_IO_CS;
199 1.1 uch r &= ~(SPD_IO_IN | SPD_IO_CLK);
200 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
201 1.1 uch delay(1);
202 1.1 uch
203 1.1 uch /* put start bit */
204 1.1 uch __spd_eeprom_out(&r, 1);
205 1.1 uch
206 1.1 uch /* put op code (read) */
207 1.1 uch __spd_eeprom_out(&r, 1);
208 1.1 uch __spd_eeprom_out(&r, 0);
209 1.1 uch
210 1.1 uch /* set address */
211 1.1 uch for (i = 0; i < 6; i++, addr <<= 1)
212 1.1 uch __spd_eeprom_out(&r, addr & 0x20);
213 1.1 uch
214 1.1 uch /* get data */
215 1.1 uch for (i = 0; i < n; i++, data++)
216 1.1 uch for (*data = 0, j = 15; j >= 0; j--)
217 1.1 uch *data |= (__spd_eeprom_in(&r) << j);
218 1.1 uch
219 1.1 uch /* chip select low */
220 1.1 uch r &= ~(SPD_IO_CS | SPD_IO_IN | SPD_IO_CLK);
221 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
222 1.1 uch delay(2);
223 1.1 uch
224 1.1 uch _intr_resume(s);
225 1.1 uch }
226 1.1 uch
227 1.1 uch void
228 1.1 uch __spd_eeprom_out(u_int8_t *rp, int onoff)
229 1.1 uch {
230 1.1 uch u_int8_t r = *rp;
231 1.1 uch
232 1.1 uch if (onoff)
233 1.1 uch r |= SPD_IO_IN;
234 1.1 uch else
235 1.1 uch r &= ~SPD_IO_IN;
236 1.1 uch
237 1.1 uch r &= ~SPD_IO_CLK;
238 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
239 1.1 uch delay(1);
240 1.1 uch
241 1.1 uch r |= SPD_IO_CLK;
242 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
243 1.1 uch delay(1);
244 1.1 uch
245 1.1 uch r &= ~SPD_IO_CLK;
246 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
247 1.1 uch delay(1);
248 1.1 uch
249 1.1 uch *rp = r;
250 1.1 uch }
251 1.1 uch
252 1.1 uch int
253 1.1 uch __spd_eeprom_in(u_int8_t *rp)
254 1.1 uch {
255 1.1 uch int ret;
256 1.1 uch u_int8_t r = *rp;
257 1.1 uch
258 1.1 uch r &= ~(SPD_IO_IN | SPD_IO_CLK);
259 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
260 1.1 uch delay(1);
261 1.1 uch
262 1.1 uch r |= SPD_IO_CLK;
263 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
264 1.1 uch delay(1);
265 1.1 uch ret = (_reg_read_1(SPD_IO_DATA_REG8) >> 4) & 0x1;
266 1.1 uch
267 1.1 uch r &= ~SPD_IO_CLK;
268 1.1 uch _reg_write_1(SPD_IO_DATA_REG8, r);
269 1.1 uch delay(1);
270 1.1 uch
271 1.1 uch *rp = r;
272 1.1 uch
273 1.1 uch return (ret);
274 1.1 uch }
275 1.1 uch
276