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