Home | History | Annotate | Line # | Download | only in dev
nbppcon.c revision 1.4
      1  1.4   thorpej /*	$NetBSD: nbppcon.c,v 1.4 2021/04/24 23:36:37 thorpej Exp $ */
      2  1.1  kiyohara /*
      3  1.1  kiyohara  * Copyright (c) 2011 KIYOHARA Takashi
      4  1.1  kiyohara  * All rights reserved.
      5  1.1  kiyohara  *
      6  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7  1.1  kiyohara  * modification, are permitted provided that the following conditions
      8  1.1  kiyohara  * are met:
      9  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14  1.1  kiyohara  *
     15  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26  1.1  kiyohara  */
     27  1.1  kiyohara #include <sys/cdefs.h>
     28  1.4   thorpej __KERNEL_RCSID(0, "$NetBSD: nbppcon.c,v 1.4 2021/04/24 23:36:37 thorpej Exp $");
     29  1.1  kiyohara 
     30  1.1  kiyohara #include <sys/param.h>
     31  1.1  kiyohara #include <sys/device.h>
     32  1.1  kiyohara #include <sys/errno.h>
     33  1.1  kiyohara 
     34  1.1  kiyohara #include <machine/platid.h>
     35  1.1  kiyohara #include <machine/platid_mask.h>
     36  1.1  kiyohara 
     37  1.1  kiyohara #include <arm/xscale/pxa2x0_i2c.h>
     38  1.1  kiyohara 
     39  1.1  kiyohara #include <hpcarm/dev/nbppconvar.h>
     40  1.1  kiyohara 
     41  1.1  kiyohara #include <dev/i2c/i2cvar.h>
     42  1.1  kiyohara #include <dev/i2c/at24cxxvar.h>
     43  1.1  kiyohara 
     44  1.1  kiyohara #include "locators.h"
     45  1.1  kiyohara 
     46  1.1  kiyohara #define NBPPCON_ADDR	0x13
     47  1.1  kiyohara 
     48  1.1  kiyohara 
     49  1.1  kiyohara struct nbppcon_softc {
     50  1.1  kiyohara 	device_t sc_dev;
     51  1.1  kiyohara 	i2c_tag_t sc_tag;
     52  1.1  kiyohara 	int sc_address;
     53  1.1  kiyohara 
     54  1.1  kiyohara 	struct callback {
     55  1.1  kiyohara 		int cb_tag;
     56  1.1  kiyohara 		int (*cb_func)(void *, int, char *);
     57  1.1  kiyohara 		void *cb_arg;
     58  1.1  kiyohara 	} sc_cbs[2];
     59  1.1  kiyohara };
     60  1.1  kiyohara 
     61  1.1  kiyohara static int nbppcon_match(device_t, cfdata_t, void *);
     62  1.1  kiyohara static void nbppcon_attach(device_t, device_t, void *);
     63  1.1  kiyohara 
     64  1.1  kiyohara static int nbppcon_search(device_t, cfdata_t, const int *, void *);
     65  1.1  kiyohara static int nbppcon_print(void *aux, const char *pnp);
     66  1.1  kiyohara 
     67  1.1  kiyohara CFATTACH_DECL_NEW(nbppcon, sizeof(struct nbppcon_softc),
     68  1.1  kiyohara     nbppcon_match, nbppcon_attach, NULL, NULL);
     69  1.1  kiyohara 
     70  1.1  kiyohara 
     71  1.1  kiyohara /* ARGSUSED */
     72  1.1  kiyohara static int
     73  1.1  kiyohara nbppcon_match(device_t parent, cfdata_t match, void *aux)
     74  1.1  kiyohara {
     75  1.1  kiyohara 	struct i2c_attach_args *ia = aux;
     76  1.1  kiyohara 
     77  1.1  kiyohara 	if (ia->ia_addr != NBPPCON_ADDR ||
     78  1.1  kiyohara 	    !platid_match(&platid, &platid_mask_MACH_PSIONTEKLOGIX_NETBOOK_PRO))
     79  1.1  kiyohara 		 return 0;
     80  1.1  kiyohara 
     81  1.3   thorpej 	return I2C_MATCH_ADDRESS_AND_PROBE;
     82  1.1  kiyohara }
     83  1.1  kiyohara 
     84  1.1  kiyohara /* ARGSUSED */
     85  1.1  kiyohara static void
     86  1.1  kiyohara nbppcon_attach(device_t parent, device_t self, void *aux)
     87  1.1  kiyohara {
     88  1.1  kiyohara 	struct nbppcon_softc *sc = device_private(self);
     89  1.1  kiyohara 	struct i2c_attach_args *ia = aux;
     90  1.1  kiyohara 	int rv;
     91  1.1  kiyohara 	char buf[128];
     92  1.1  kiyohara 
     93  1.1  kiyohara 	sc->sc_dev = self;
     94  1.1  kiyohara 	sc->sc_tag = ia->ia_tag;
     95  1.1  kiyohara 	sc->sc_address = ia->ia_addr;
     96  1.1  kiyohara 
     97  1.1  kiyohara 	aprint_naive("\n");
     98  1.1  kiyohara 	aprint_normal(": NETBOOK PRO PCon\n");
     99  1.1  kiyohara 
    100  1.1  kiyohara #define NVRAM_ADDR	0x53
    101  1.1  kiyohara #define NVRAM_DATA_REV	0x14
    102  1.1  kiyohara 	rv = seeprom_bootstrap_read(sc->sc_tag, NVRAM_ADDR, 0x00, 128,
    103  1.1  kiyohara 	    buf, sizeof(buf));
    104  1.1  kiyohara 	if (rv == 0)
    105  1.2  kiyohara 		aprint_normal_dev(self, "NETBOOK PRO Rev.%c\n",
    106  1.1  kiyohara 		    buf[NVRAM_DATA_REV] - '0' + '@');
    107  1.1  kiyohara 	else
    108  1.1  kiyohara 		aprint_error_dev(self, "NVRAM read failed\n");
    109  1.1  kiyohara 
    110  1.4   thorpej 	config_search(self, NULL,
    111  1.4   thorpej 	    CFARG_SEARCH, nbppcon_search,
    112  1.4   thorpej 	    CFARG_EOL);
    113  1.1  kiyohara }
    114  1.1  kiyohara 
    115  1.1  kiyohara /* ARGSUSED */
    116  1.1  kiyohara static int
    117  1.1  kiyohara nbppcon_search(device_t self, cfdata_t cf, const int *ldesc, void *aux)
    118  1.1  kiyohara {
    119  1.1  kiyohara 	struct nbppcon_attach_args pcon;
    120  1.1  kiyohara 
    121  1.1  kiyohara 	pcon.aa_name = cf->cf_name;
    122  1.1  kiyohara 	pcon.aa_tag = cf->cf_loc[NBPPCONCF_TAG];
    123  1.1  kiyohara 
    124  1.4   thorpej 	if (config_probe(self, cf, &pcon))
    125  1.4   thorpej 		config_attach(self, cf, &pcon, nbppcon_print, CFARG_EOL);
    126  1.1  kiyohara 
    127  1.1  kiyohara 	return 0;
    128  1.1  kiyohara }
    129  1.1  kiyohara 
    130  1.1  kiyohara /* ARGSUSED */
    131  1.1  kiyohara static int
    132  1.1  kiyohara nbppcon_print(void *aux, const char *pnp)
    133  1.1  kiyohara {
    134  1.1  kiyohara 
    135  1.1  kiyohara 	if (pnp != NULL)
    136  1.1  kiyohara 		aprint_normal("%s", pnp);
    137  1.1  kiyohara 	return UNCONF;
    138  1.1  kiyohara }
    139  1.1  kiyohara 
    140  1.1  kiyohara 
    141  1.1  kiyohara void *
    142  1.1  kiyohara nbppcon_regist_callback(device_t self,
    143  1.1  kiyohara 			int tag, int (*func)(void *, int, char *), void *arg)
    144  1.1  kiyohara {
    145  1.1  kiyohara 	struct nbppcon_softc *sc = device_private(self);
    146  1.1  kiyohara 	int i;
    147  1.1  kiyohara 
    148  1.1  kiyohara 	for (i = 0; i < __arraycount(sc->sc_cbs); i++)
    149  1.1  kiyohara 		if (sc->sc_cbs[i].cb_func == NULL) {
    150  1.1  kiyohara 			sc->sc_cbs[i].cb_tag = tag;
    151  1.1  kiyohara 			sc->sc_cbs[i].cb_func = func;
    152  1.1  kiyohara 			sc->sc_cbs[i].cb_arg = arg;
    153  1.1  kiyohara 			return func;
    154  1.1  kiyohara 		}
    155  1.1  kiyohara 
    156  1.1  kiyohara 	return NULL;
    157  1.1  kiyohara }
    158  1.1  kiyohara 
    159  1.1  kiyohara int
    160  1.1  kiyohara nbppcon_intr(device_t self, int buflen, char *buf)
    161  1.1  kiyohara {
    162  1.1  kiyohara 	struct nbppcon_softc *sc = device_private(self);
    163  1.1  kiyohara 	struct callback *cb;
    164  1.1  kiyohara 	int i;
    165  1.1  kiyohara 
    166  1.1  kiyohara 	for (i = 0; i < __arraycount(sc->sc_cbs); i++) {
    167  1.1  kiyohara 		cb = &sc->sc_cbs[i];
    168  1.1  kiyohara 		if (cb->cb_func != NULL &&
    169  1.1  kiyohara 		    cb->cb_tag == buf[0])
    170  1.1  kiyohara 			return cb->cb_func(cb->cb_arg, buflen, buf);
    171  1.1  kiyohara 	}
    172  1.1  kiyohara 	aprint_error_dev(sc->sc_dev, "unknown tag received: 0x%02x\n", buf[0]);
    173  1.1  kiyohara 	return 0;
    174  1.1  kiyohara }
    175  1.1  kiyohara 
    176  1.1  kiyohara int
    177  1.1  kiyohara nbppcon_poll(device_t self, int tag, int buflen, char *buf)
    178  1.1  kiyohara {
    179  1.1  kiyohara 	struct nbppcon_softc *sc = device_private(self);
    180  1.1  kiyohara 	int rv;
    181  1.1  kiyohara 
    182  1.1  kiyohara 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    183  1.1  kiyohara 		aprint_error_dev(sc->sc_dev,
    184  1.1  kiyohara 		    "%s: acquire bus failed\n", __func__);
    185  1.1  kiyohara 		return -1;
    186  1.1  kiyohara 	}
    187  1.1  kiyohara 
    188  1.1  kiyohara 	do {
    189  1.1  kiyohara 		extern int nbpiic_poll(void *, int, char *);
    190  1.1  kiyohara 
    191  1.1  kiyohara 		rv = nbpiic_poll(sc->sc_tag->ic_cookie, buflen, buf);
    192  1.1  kiyohara 		if (rv > 0 && buf[0] == tag)
    193  1.1  kiyohara 			break;
    194  1.1  kiyohara 	} while (rv == 0 || buf[0] != tag);
    195  1.1  kiyohara 
    196  1.1  kiyohara 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    197  1.1  kiyohara 
    198  1.1  kiyohara 	return rv;
    199  1.1  kiyohara }
    200  1.1  kiyohara 
    201  1.1  kiyohara int
    202  1.1  kiyohara nbppcon_kbd_ready(device_t self)
    203  1.1  kiyohara {
    204  1.1  kiyohara 	struct nbppcon_softc *sc = device_private(self);
    205  1.1  kiyohara 	int rv;
    206  1.1  kiyohara 	char cmd[1], data[1];
    207  1.1  kiyohara 
    208  1.1  kiyohara 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    209  1.1  kiyohara 		aprint_error_dev(sc->sc_dev,
    210  1.1  kiyohara 		    "%s: acquire bus failed\n", __func__);
    211  1.1  kiyohara 		return -1;
    212  1.1  kiyohara 	}
    213  1.1  kiyohara 
    214  1.1  kiyohara 	cmd[0] = 0x00;		/* Keyboard */
    215  1.1  kiyohara 	data[0] = 0x00;		/* 0x00:Ready, 0x01:Busy? */
    216  1.1  kiyohara 	rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    217  1.1  kiyohara 	    cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
    218  1.1  kiyohara 
    219  1.1  kiyohara 	cpu_dcache_wbinv_all();		/* XXXX: Why? */
    220  1.1  kiyohara 
    221  1.1  kiyohara 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    222  1.1  kiyohara 
    223  1.1  kiyohara 	return rv;
    224  1.1  kiyohara }
    225  1.1  kiyohara 
    226  1.1  kiyohara int
    227  1.1  kiyohara nbppcon_pwr_hwreset(device_t self)
    228  1.1  kiyohara {
    229  1.1  kiyohara 	struct nbppcon_softc *sc = device_private(self);
    230  1.1  kiyohara 	int rv;
    231  1.1  kiyohara 	char cmd[1], data[1];
    232  1.1  kiyohara 
    233  1.1  kiyohara 	if (iic_acquire_bus(sc->sc_tag, I2C_F_POLL)) {
    234  1.1  kiyohara 		aprint_error_dev(sc->sc_dev,
    235  1.1  kiyohara 		    "%s: acquire bus failed\n", __func__);
    236  1.1  kiyohara 		return -1;
    237  1.1  kiyohara 	}
    238  1.1  kiyohara 
    239  1.1  kiyohara 	cmd[0] = 0x05;		/* POWER */
    240  1.1  kiyohara 	data[0] = 0x00;		/* HWReset */
    241  1.1  kiyohara #if 0
    242  1.1  kiyohara 	rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address,
    243  1.1  kiyohara 	    cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
    244  1.1  kiyohara #else	/* XXXX: Oops, ensure HWReset, ignore cmd and data. */
    245  1.1  kiyohara 	rv = iic_exec(sc->sc_tag, I2C_OP_WRITE_WITH_STOP, sc->sc_address + 1,
    246  1.1  kiyohara 	    cmd, sizeof(cmd), data, sizeof(data), I2C_F_POLL);
    247  1.1  kiyohara #endif
    248  1.1  kiyohara 
    249  1.1  kiyohara 	iic_release_bus(sc->sc_tag, I2C_F_POLL);
    250  1.1  kiyohara 
    251  1.1  kiyohara 	return rv;
    252  1.1  kiyohara }
    253