Home | History | Annotate | Line # | Download | only in dev
aupsc.c revision 1.2
      1 /* $NetBSD: aupsc.c,v 1.2 2006/03/06 17:15:03 shige Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Shigeyuki Fukushima.
      5  * All rights reserved.
      6  *
      7  * Written by Shigeyuki Fukushima.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above
     15  *    copyright notice, this list of conditions and the following
     16  *    disclaimer in the documentation and/or other materials provided
     17  *    with the distribution.
     18  * 3. The name of the author may not be used to endorse or promote
     19  *    products derived from this software without specific prior
     20  *    written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     28  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: aupsc.c,v 1.2 2006/03/06 17:15:03 shige Exp $");
     37 
     38 #include "locators.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/errno.h>
     44 
     45 #include <machine/bus.h>
     46 #include <machine/cpu.h>
     47 
     48 #include <mips/alchemy/include/aubusvar.h>
     49 #include <mips/alchemy/include/aureg.h>
     50 #include <mips/alchemy/dev/aupscreg.h>
     51 #include <mips/alchemy/dev/aupscvar.h>
     52 #include <mips/alchemy/dev/smbusreg.h>
     53 
     54 struct aupsc_softc {
     55 	struct device		sc_dev;
     56 	bus_space_tag_t		sc_bust;
     57 	bus_space_handle_t	sc_bush;
     58 	int			sc_pscsel;
     59 };
     60 
     61 const struct aupsc_proto {
     62 	const char *name;
     63 	int protocol;
     64 	int statreg;
     65 	int statbit;
     66 } aupsc_protos [] = {
     67 	{ "ausmbus", AUPSC_SEL_SMBUS, AUPSC_SMBSTAT, SMBUS_STAT_SR },
     68 #if 0
     69 	{ "auaudio" },
     70 	{ "aui2s" },
     71 	{ "ausmbus" },
     72 	{ "auspi" },
     73 #endif
     74 	{ NULL, AUPSC_SEL_DISABLE, 0, 0 }
     75 };
     76 
     77 static int	aupsc_match(struct device *, struct cfdata *, void *);
     78 static void	aupsc_attach(struct device *, struct device *, void *);
     79 static int	aupsc_submatch(struct device *, struct cfdata *, const int *,
     80 				void *);
     81 static int	aupsc_print(void *, const char *);
     82 
     83 static void	aupsc_enable(void *, int);
     84 static void	aupsc_disable(void *);
     85 static void	aupsc_suspend(void *);
     86 
     87 
     88 CFATTACH_DECL(aupsc, sizeof(struct aupsc_softc),
     89 	aupsc_match, aupsc_attach, NULL, NULL);
     90 
     91 static int
     92 aupsc_match(struct device *parent, struct cfdata *cf, void *aux)
     93 {
     94 	struct aubus_attach_args *aa = (struct aubus_attach_args *)aux;
     95 
     96 	if (strcmp(aa->aa_name, cf->cf_name) != 0)
     97 		return 0;
     98 
     99 	return 1;
    100 }
    101 
    102 static void
    103 aupsc_attach(struct device *parent, struct device *self, void *aux)
    104 {
    105 	int i;
    106 	uint32_t rv;
    107 	struct aupsc_softc *sc = (struct aupsc_softc *)self;
    108 	struct aubus_attach_args *aa = (struct aubus_attach_args *)aux;
    109 	struct aupsc_attach_args pa;
    110 	struct aupsc_controller ctrl;
    111 
    112 	sc->sc_bust = aa->aa_st;
    113 	if (bus_space_map(sc->sc_bust, aa->aa_addr,
    114 			AUPSC_SIZE, 0, &sc->sc_bush) != 0) {
    115 		aprint_normal(": unable to map device registers\n");
    116 		return;
    117 	}
    118 
    119 	/* Initialize PSC_SEL register */
    120 	sc->sc_pscsel = AUPSC_SEL_DISABLE;
    121 	rv = bus_space_read_4(sc->sc_bust, sc->sc_bush, AUPSC_SEL);
    122 	bus_space_write_4(sc->sc_bust, sc->sc_bush,
    123 		AUPSC_SEL, (rv & AUPSC_SEL_PS(AUPSC_SEL_DISABLE)));
    124 	bus_space_write_4(sc->sc_bust, sc->sc_bush,
    125 		AUPSC_CTRL, AUPSC_CTRL_ENA(AUPSC_CTRL_DISABLE));
    126 
    127 	aprint_normal(": Alchemy PSC\n");
    128 
    129 	ctrl.psc_bust = sc->sc_bust;
    130 	ctrl.psc_bush = sc->sc_bush;
    131 	ctrl.psc_sel = &(sc->sc_pscsel);
    132 	ctrl.psc_enable = aupsc_enable;
    133 	ctrl.psc_disable = aupsc_disable;
    134 	ctrl.psc_suspend = aupsc_suspend;
    135 	pa.aupsc_ctrl = ctrl;
    136 
    137 	for (i = 0 ; aupsc_protos[i].name != NULL ; i++) {
    138 		struct aupsc_protocol_device p;
    139 		uint32_t s;
    140 
    141 		pa.aupsc_name = aupsc_protos[i].name;
    142 
    143 		p.sc_dev = sc->sc_dev;
    144 		p.sc_ctrl = ctrl;
    145 
    146 		aupsc_enable(&p, aupsc_protos[i].protocol);
    147 		s = bus_space_read_4(sc->sc_bust, sc->sc_bush,
    148 			aupsc_protos[i].statreg);
    149 		aupsc_disable(&p);
    150 
    151 		if (s & aupsc_protos[i].statbit) {
    152 			(void) config_found_sm_loc(self, "aupsc", NULL,
    153 				&pa, aupsc_print, aupsc_submatch);
    154 		}
    155         }
    156 }
    157 
    158 static int
    159 aupsc_submatch(struct device *parent, struct cfdata *cf,
    160 	const int *ldesc, void *aux)
    161 {
    162 
    163 	return config_match(parent, cf, aux);
    164 }
    165 
    166 static int
    167 aupsc_print(void *aux, const char *pnp)
    168 {
    169 	struct aupsc_attach_args *pa = aux;
    170 
    171 	if (pnp)
    172 		aprint_normal("%s at %s", pa->aupsc_name, pnp);
    173 
    174 	return UNCONF;
    175 }
    176 
    177 static void
    178 aupsc_enable(void *arg, int proto)
    179 {
    180 	struct aupsc_protocol_device *sc = arg;
    181 
    182 	/* XXX: (TODO) setting clock AUPSC_SEL_CLK */
    183 	switch (proto) {
    184 	case AUPSC_SEL_SPI:
    185 	case AUPSC_SEL_I2S:
    186 	case AUPSC_SEL_AC97:
    187 	case AUPSC_SEL_SMBUS:
    188 		break;
    189 	case AUPSC_SEL_DISABLE:
    190 		aupsc_disable(arg);
    191 		break;
    192 	default:
    193 		printf("%s: aupsc_enable: unsupported protocol.\n",
    194 			sc->sc_dev.dv_xname);
    195 		return;
    196 	}
    197 
    198 	if (*(sc->sc_ctrl.psc_sel) != AUPSC_SEL_DISABLE) {
    199 		printf("%s: aupsc_enable: please disable first.\n",
    200 			sc->sc_dev.dv_xname);
    201 		return;
    202 	}
    203 
    204 	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
    205 			AUPSC_SEL, AUPSC_SEL_PS(proto));
    206 	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
    207 			AUPSC_CTRL, AUPSC_CTRL_ENA(AUPSC_CTRL_ENABLE));
    208 	delay(1);
    209 }
    210 
    211 static void
    212 aupsc_disable(void *arg)
    213 {
    214 	struct aupsc_protocol_device *sc = arg;
    215 
    216 	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
    217 			AUPSC_SEL, AUPSC_SEL_PS(AUPSC_SEL_DISABLE));
    218 	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
    219 			AUPSC_CTRL, AUPSC_CTRL_ENA(AUPSC_CTRL_DISABLE));
    220 	delay(1);
    221 }
    222 
    223 static void
    224 aupsc_suspend(void *arg)
    225 {
    226 	struct aupsc_protocol_device *sc = arg;
    227 
    228 	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
    229 			AUPSC_CTRL, AUPSC_CTRL_ENA(AUPSC_CTRL_SUSPEND));
    230 	delay(1);
    231 }
    232