Home | History | Annotate | Line # | Download | only in dev
spd.c revision 1.14
      1  1.14  thorpej /*	$NetBSD: spd.c,v 1.14 2021/04/24 23:36:45 thorpej 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.14  thorpej __KERNEL_RCSID(0, "$NetBSD: spd.c,v 1.14 2021/04/24 23:36:45 thorpej Exp $");
     34   1.1      uch 
     35  1.12     maya #include <sys/param.h>
     36  1.11     maya #include <sys/device.h>
     37   1.1      uch #include <sys/systm.h>
     38   1.1      uch 
     39   1.1      uch #include <machine/bootinfo.h>
     40   1.1      uch 
     41   1.1      uch #include <playstation2/ee/eevar.h>
     42   1.1      uch #include <playstation2/dev/sbusvar.h>
     43   1.1      uch #include <playstation2/dev/spdvar.h>
     44   1.1      uch #include <playstation2/dev/spdreg.h>
     45   1.1      uch 
     46   1.1      uch #ifdef DEBUG
     47   1.1      uch #define STATIC
     48   1.1      uch #else
     49   1.1      uch #define STATIC static
     50   1.1      uch #endif
     51   1.1      uch 
     52  1.13     maya STATIC int spd_match(device_t, cfdata_t, void *);
     53  1.13     maya STATIC void spd_attach(device_t, device_t, void *);
     54   1.1      uch STATIC int spd_print(void *, const char *);
     55   1.1      uch STATIC int spd_intr(void *);
     56   1.1      uch STATIC void __spd_eeprom_out(u_int8_t *, int);
     57   1.1      uch STATIC int __spd_eeprom_in(u_int8_t *);
     58   1.1      uch 
     59   1.1      uch /* SPD device can't attach twice. because PS2 PC-Card slot is only one. */
     60   1.1      uch STATIC struct {
     61   1.1      uch 	int (*func)(void *);
     62   1.1      uch 	void *arg;
     63   1.1      uch 	const char *name;
     64   1.1      uch } __spd_table[2];
     65   1.1      uch 
     66  1.13     maya CFATTACH_DECL_NEW(spd, sizeof(struct device),
     67   1.3  thorpej     spd_match, spd_attach, NULL, NULL);
     68   1.1      uch 
     69   1.1      uch #ifdef DEBUG
     70   1.1      uch #define LEGAL_SLOT(slot)	((slot) >= 0 && (slot) < 2)
     71   1.1      uch #endif
     72   1.1      uch 
     73   1.1      uch int
     74  1.13     maya spd_match(device_t parent, cfdata_t cf, void *aux)
     75   1.1      uch {
     76   1.1      uch 
     77   1.1      uch 	return ((BOOTINFO_REF(BOOTINFO_DEVCONF) ==
     78   1.1      uch 	    BOOTINFO_DEVCONF_SPD_PRESENT));
     79   1.1      uch }
     80   1.1      uch 
     81   1.1      uch void
     82  1.13     maya spd_attach(device_t parent, device_t self, void *aux)
     83   1.1      uch {
     84   1.1      uch 	struct spd_attach_args spa;
     85   1.1      uch 
     86   1.1      uch 	printf(": PlayStation 2 HDD Unit\n");
     87   1.1      uch 
     88   1.1      uch 	switch (BOOTINFO_REF(BOOTINFO_PCMCIA_TYPE)) {
     89   1.1      uch 	default:
     90   1.1      uch 		__spd_table[0].name = "<unknown product>";
     91   1.1      uch 		break;
     92   1.1      uch 	case 0:
     93   1.1      uch 		/* FALLTHROUGH */
     94   1.1      uch 	case 1:
     95   1.1      uch 		/* FALLTHROUGH */
     96   1.1      uch 	case 2:
     97   1.1      uch 		__spd_table[SPD_HDD].name = "SCPH-20400";
     98   1.1      uch 		__spd_table[SPD_NIC].name = "SCPH-10190";
     99   1.1      uch 		break;
    100   1.1      uch 	case 3:
    101   1.1      uch 		__spd_table[SPD_HDD].name = "SCPH-10260";
    102   1.1      uch 		__spd_table[SPD_NIC].name = "SCPH-10260";
    103   1.1      uch 		break;
    104   1.1      uch 	}
    105   1.1      uch 
    106   1.1      uch 	/* disable all */
    107   1.1      uch 	_reg_write_2(SPD_INTR_ENABLE_REG16, 0);
    108   1.1      uch 	_reg_write_2(SPD_INTR_CLEAR_REG16, _reg_read_2(SPD_INTR_STATUS_REG16));
    109   1.1      uch 
    110   1.1      uch 	spa.spa_slot = SPD_HDD;
    111   1.1      uch 	spa.spa_product_name = __spd_table[SPD_HDD].name;
    112  1.14  thorpej 	config_found(self, &spa, spd_print, CFARG_EOL);
    113   1.1      uch 
    114   1.1      uch 	spa.spa_slot = SPD_NIC;
    115   1.1      uch 	spa.spa_product_name = __spd_table[SPD_NIC].name;
    116  1.14  thorpej 	config_found(self, &spa, spd_print, CFARG_EOL);
    117   1.1      uch 
    118   1.1      uch 	sbus_intr_establish(SBUS_IRQ_PCMCIA, spd_intr, 0);
    119   1.1      uch }
    120   1.1      uch 
    121   1.1      uch int
    122   1.1      uch spd_print(void *aux, const char *pnp)
    123   1.1      uch {
    124   1.1      uch 	struct spd_attach_args *spa = aux;
    125   1.1      uch 
    126   1.1      uch 	if (pnp)
    127   1.4  thorpej 		aprint_normal("%s at %s", __spd_table[spa->spa_slot].name, pnp);
    128   1.1      uch 
    129   1.1      uch 	return (UNCONF);
    130   1.1      uch }
    131   1.1      uch 
    132   1.1      uch int
    133   1.1      uch spd_intr(void *arg)
    134   1.1      uch {
    135   1.1      uch 	u_int16_t r;
    136   1.1      uch 
    137   1.1      uch 	r = _reg_read_2(SPD_INTR_STATUS_REG16);
    138   1.1      uch 
    139   1.1      uch  	/* HDD (SCPH-20400) */
    140   1.1      uch 	if ((r & SPD_INTR_HDD) != 0)
    141   1.1      uch 		if (__spd_table[SPD_HDD].func != NULL)
    142   1.1      uch 			(*__spd_table[SPD_HDD].func)(__spd_table[SPD_HDD].arg);
    143   1.1      uch 
    144   1.1      uch 	/* Network (SCPH-10190) */
    145   1.1      uch 	if ((r & (SPD_INTR_EMAC3 | SPD_INTR_RXEND | SPD_INTR_TXEND |
    146   1.1      uch 	    SPD_INTR_RXDNV | SPD_INTR_TXDNV)) != 0)
    147   1.1      uch 		if (__spd_table[SPD_NIC].func)
    148   1.1      uch 			(*__spd_table[SPD_NIC].func)(__spd_table[SPD_NIC].arg);
    149   1.1      uch 
    150   1.1      uch 	/* reinstall */
    151   1.1      uch 	r = _reg_read_2(SPD_INTR_ENABLE_REG16);
    152   1.1      uch 	_reg_write_2(SPD_INTR_ENABLE_REG16, 0);
    153   1.1      uch 	_reg_write_2(SPD_INTR_ENABLE_REG16, r);
    154   1.1      uch 
    155   1.1      uch 	return (1);
    156   1.1      uch }
    157   1.1      uch 
    158   1.1      uch void *
    159   1.1      uch spd_intr_establish(enum spd_slot slot, int (*func)(void *), void *arg)
    160   1.1      uch {
    161   1.1      uch 
    162   1.1      uch 	KDASSERT(LEGAL_SLOT(slot));
    163   1.1      uch 	KDASSERT(__spd_table[slot].func == 0);
    164   1.1      uch 
    165   1.1      uch 	__spd_table[slot].func = func;
    166   1.1      uch 	__spd_table[slot].arg = arg;
    167   1.1      uch 
    168   1.1      uch 	return ((void *)slot);
    169   1.1      uch }
    170   1.1      uch 
    171   1.1      uch void
    172   1.1      uch spd_intr_disestablish(void *handle)
    173   1.1      uch {
    174   1.1      uch 	int slot = (int)handle;
    175   1.1      uch 
    176   1.1      uch 	KDASSERT(LEGAL_SLOT(slot));
    177   1.1      uch 
    178   1.1      uch 	__spd_table[slot].func = 0;
    179   1.1      uch }
    180   1.1      uch 
    181   1.1      uch /*
    182   1.1      uch  * EEPROM access
    183   1.1      uch  */
    184   1.1      uch void
    185   1.1      uch spd_eeprom_read(int addr, u_int16_t *data, int n)
    186   1.1      uch {
    187   1.1      uch 	int i, j, s;
    188   1.1      uch 	u_int8_t r;
    189   1.1      uch 
    190   1.1      uch 	s = _intr_suspend();
    191   1.1      uch 
    192   1.1      uch 	/* set direction */
    193   1.1      uch 	_reg_write_1(SPD_IO_DIR_REG8, SPD_IO_CLK | SPD_IO_CS | SPD_IO_IN);
    194   1.1      uch 
    195   1.1      uch 	/* chip select high */
    196   1.1      uch 	r = 0;
    197   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    198   1.1      uch 	delay(1);
    199   1.1      uch 	r |= SPD_IO_CS;
    200   1.1      uch 	r &= ~(SPD_IO_IN | SPD_IO_CLK);
    201   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    202   1.1      uch 	delay(1);
    203   1.1      uch 
    204   1.1      uch 	/* put start bit */
    205   1.1      uch 	__spd_eeprom_out(&r, 1);
    206   1.1      uch 
    207   1.1      uch 	/* put op code (read) */
    208   1.1      uch 	__spd_eeprom_out(&r, 1);
    209   1.1      uch 	__spd_eeprom_out(&r, 0);
    210   1.1      uch 
    211   1.1      uch 	/* set address */
    212   1.1      uch 	for (i = 0; i < 6; i++, addr <<= 1)
    213   1.1      uch 		__spd_eeprom_out(&r, addr & 0x20);
    214   1.1      uch 
    215   1.1      uch 	/* get data */
    216   1.1      uch 	for (i = 0; i < n; i++, data++)
    217   1.1      uch 		for (*data = 0, j = 15; j >= 0; j--)
    218   1.1      uch 			*data |= (__spd_eeprom_in(&r) << j);
    219   1.1      uch 
    220   1.1      uch 	/* chip select low */
    221   1.1      uch 	r &= ~(SPD_IO_CS | SPD_IO_IN | SPD_IO_CLK);
    222   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    223   1.1      uch 	delay(2);
    224   1.1      uch 
    225   1.1      uch 	_intr_resume(s);
    226   1.1      uch }
    227   1.1      uch 
    228   1.1      uch void
    229   1.1      uch __spd_eeprom_out(u_int8_t *rp, int onoff)
    230   1.1      uch {
    231   1.1      uch 	u_int8_t r = *rp;
    232   1.1      uch 
    233   1.1      uch 	if (onoff)
    234   1.1      uch 		r |= SPD_IO_IN;
    235   1.1      uch 	else
    236   1.1      uch 		r &= ~SPD_IO_IN;
    237   1.1      uch 
    238   1.1      uch 	r &= ~SPD_IO_CLK;
    239   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    240   1.1      uch 	delay(1);
    241   1.1      uch 
    242   1.1      uch 	r |= SPD_IO_CLK;
    243   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    244   1.1      uch 	delay(1);
    245   1.1      uch 
    246   1.1      uch 	r &= ~SPD_IO_CLK;
    247   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    248   1.1      uch 	delay(1);
    249   1.1      uch 
    250   1.1      uch 	*rp = r;
    251   1.1      uch }
    252   1.1      uch 
    253   1.1      uch int
    254   1.1      uch __spd_eeprom_in(u_int8_t *rp)
    255   1.1      uch {
    256   1.1      uch 	int ret;
    257   1.1      uch 	u_int8_t r = *rp;
    258   1.1      uch 
    259   1.1      uch 	r &= ~(SPD_IO_IN | SPD_IO_CLK);
    260   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    261   1.1      uch 	delay(1);
    262   1.1      uch 
    263   1.1      uch 	r |= SPD_IO_CLK;
    264   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    265   1.1      uch 	delay(1);
    266   1.1      uch 	ret = (_reg_read_1(SPD_IO_DATA_REG8) >> 4) & 0x1;
    267   1.1      uch 
    268   1.1      uch 	r &= ~SPD_IO_CLK;
    269   1.1      uch 	_reg_write_1(SPD_IO_DATA_REG8, r);
    270   1.1      uch 	delay(1);
    271   1.1      uch 
    272   1.1      uch 	*rp = r;
    273   1.1      uch 
    274   1.1      uch 	return (ret);
    275   1.1      uch }
    276   1.1      uch 
    277