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