xref: /netbsd-src/sys/arch/hpcarm/dev/j720ssp.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: j720ssp.c,v 1.34 2021/08/07 16:18:53 thorpej Exp $	*/
2b57d496dStoshii 
3b57d496dStoshii /*-
4547e1f66Speter  * Copyright (c) 2001, 2006 The NetBSD Foundation, Inc.
5b57d496dStoshii  * All rights reserved.
6b57d496dStoshii  *
7b57d496dStoshii  * This code is derived from software contributed to The NetBSD Foundation
8547e1f66Speter  * by IWAMOTO Toshihiro.
9b57d496dStoshii  *
10b57d496dStoshii  * Redistribution and use in source and binary forms, with or without
11b57d496dStoshii  * modification, are permitted provided that the following conditions
12b57d496dStoshii  * are met:
13b57d496dStoshii  * 1. Redistributions of source code must retain the above copyright
14b57d496dStoshii  *    notice, this list of conditions and the following disclaimer.
15b57d496dStoshii  * 2. Redistributions in binary form must reproduce the above copyright
16b57d496dStoshii  *    notice, this list of conditions and the following disclaimer in the
17b57d496dStoshii  *    documentation and/or other materials provided with the distribution.
18b57d496dStoshii  *
19b57d496dStoshii  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b57d496dStoshii  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b57d496dStoshii  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b57d496dStoshii  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b57d496dStoshii  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b57d496dStoshii  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b57d496dStoshii  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b57d496dStoshii  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b57d496dStoshii  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b57d496dStoshii  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b57d496dStoshii  * POSSIBILITY OF SUCH DAMAGE.
30b57d496dStoshii  */
31b57d496dStoshii 
32547e1f66Speter /* Jornada 720 SSP port. */
33b57d496dStoshii 
3408716eaeSlukem #include <sys/cdefs.h>
35*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: j720ssp.c,v 1.34 2021/08/07 16:18:53 thorpej Exp $");
367f8a47f4Smanu 
37b57d496dStoshii #include <sys/param.h>
38b57d496dStoshii #include <sys/systm.h>
39b57d496dStoshii #include <sys/device.h>
40422658daSichiro 
41422658daSichiro #include <arm/sa11x0/sa11x0_var.h>
42422658daSichiro #include <arm/sa11x0/sa11x0_gpioreg.h>
43422658daSichiro #include <arm/sa11x0/sa11x0_ppcreg.h>
44422658daSichiro #include <arm/sa11x0/sa11x0_sspreg.h>
45b57d496dStoshii 
46547e1f66Speter #include <hpcarm/dev/j720sspvar.h>
47b57d496dStoshii 
48b57d496dStoshii #ifdef DEBUG
494c494f76Srjs #define DPRINTF(arg)	aprint_normal arg
50547e1f66Speter #else
51547e1f66Speter #define DPRINTF(arg)	/* nothing */
52b57d496dStoshii #endif
53b57d496dStoshii 
540c9a60ffSpeter #define BIT_INVERT(x)							\
550c9a60ffSpeter 	do {								\
560c9a60ffSpeter 		(x) = ((((x) & 0xf0) >> 4) | (((x) & 0x0f) << 4));	\
570c9a60ffSpeter 		(x) = ((((x) & 0xcc) >> 2) | (((x) & 0x33) << 2));	\
580c9a60ffSpeter 		(x) = ((((x) & 0xaa) >> 1) | (((x) & 0x55) << 1));	\
590c9a60ffSpeter 	} while (0)
600c9a60ffSpeter 
614c494f76Srjs static int	j720ssp_match(device_t, cfdata_t, void *);
624c494f76Srjs static void	j720ssp_attach(device_t, device_t, void *);
634c494f76Srjs static int	j720ssp_search(device_t, cfdata_t, const int *, void *);
64547e1f66Speter static int	j720ssp_print(void *, const char *);
65547e1f66Speter 
664c494f76Srjs CFATTACH_DECL_NEW(j720ssp, sizeof(struct j720ssp_softc),
67547e1f66Speter     j720ssp_match, j720ssp_attach, NULL, NULL);
68b57d496dStoshii 
697f8a47f4Smanu 
70547e1f66Speter static int
j720ssp_match(device_t parent,cfdata_t cf,void * aux)714c494f76Srjs j720ssp_match(device_t parent, cfdata_t cf, void *aux)
72b57d496dStoshii {
73547e1f66Speter 
74547e1f66Speter 	if (strcmp(cf->cf_name, "j720ssp") != 0)
75547e1f66Speter 		return 0;
76547e1f66Speter 
77547e1f66Speter 	return 1;
78b57d496dStoshii }
79b57d496dStoshii 
80547e1f66Speter static void
j720ssp_attach(device_t parent,device_t self,void * aux)814c494f76Srjs j720ssp_attach(device_t parent, device_t self, void *aux)
82b57d496dStoshii {
834c494f76Srjs 	struct j720ssp_softc *sc = device_private(self);
844c494f76Srjs 	struct sa11x0_softc *psc = device_private(parent);
85b57d496dStoshii 	struct sa11x0_attach_args *sa = aux;
8656b40cd7Stoshii 
874c494f76Srjs 	sc->sc_dev = self;
88b57d496dStoshii 	sc->sc_iot = psc->sc_iot;
89b57d496dStoshii 	sc->sc_gpioh = psc->sc_gpioh;
90547e1f66Speter 	sc->sc_parent = psc;
91547e1f66Speter 
92b57d496dStoshii 	if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
93b57d496dStoshii 	    &sc->sc_ssph)) {
944c494f76Srjs 		aprint_normal(": unable to map SSP registers\n");
95b57d496dStoshii 		return;
96b57d496dStoshii 	}
97b57d496dStoshii 
984c494f76Srjs 	aprint_normal("\n");
99b57d496dStoshii 
1002685996bSthorpej 	config_search(self, NULL,
101*c7fb772bSthorpej 	    CFARGS(.search = j720ssp_search));
102f8c1bea1Stoshii }
103f8c1bea1Stoshii 
104547e1f66Speter static int
j720ssp_search(device_t parent,cfdata_t cf,const int * ldesc,void * aux)1054c494f76Srjs j720ssp_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
106f72ad6b9Stoshii {
107f72ad6b9Stoshii 
1082685996bSthorpej 	if (config_probe(parent, cf, NULL))
109*c7fb772bSthorpej 		config_attach(parent, cf, NULL, j720ssp_print, CFARGS_NONE);
110f72ad6b9Stoshii 
11110087254Smanu 	return 0;
112f72ad6b9Stoshii }
113f72ad6b9Stoshii 
114f72ad6b9Stoshii static int
j720ssp_print(void * aux,const char * pnp)115547e1f66Speter j720ssp_print(void *aux, const char *pnp)
1167f8a47f4Smanu {
117f72ad6b9Stoshii 
118547e1f66Speter 	return pnp ? QUIET : UNCONF;
119f72ad6b9Stoshii }
120f72ad6b9Stoshii 
121b57d496dStoshii int
j720ssp_readwrite(struct j720ssp_softc * sc,int drainfifo,int in,int * out,int wait)122547e1f66Speter j720ssp_readwrite(struct j720ssp_softc *sc, int drainfifo, int in,
123547e1f66Speter     int *out, int wait)
124b57d496dStoshii {
125547e1f66Speter 	int timeout;
126b57d496dStoshii 
127547e1f66Speter 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) & SR_TNF))
128547e1f66Speter 		continue;
129b57d496dStoshii 
130547e1f66Speter 	timeout = 400000;
131b57d496dStoshii 	while (bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PLR) & 0x400)
132547e1f66Speter 		if (--timeout == 0) {
133547e1f66Speter 			DPRINTF(("j720ssp_readwrite: timeout 0\n"));
134b57d496dStoshii 			return -1;
135b57d496dStoshii 		}
136b57d496dStoshii 	if (drainfifo) {
137b57d496dStoshii 		while (bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) &
138b57d496dStoshii 		      SR_RNE)
139b57d496dStoshii 			bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
14010087254Smanu 		delay(wait);
141b57d496dStoshii 	}
142b57d496dStoshii 
1430c9a60ffSpeter 	BIT_INVERT(in);
1440c9a60ffSpeter 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_DR, in << 8);
145b57d496dStoshii 
14610087254Smanu 	delay(wait);
147547e1f66Speter 	timeout = 100000;
148b57d496dStoshii 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) & SR_RNE))
149547e1f66Speter 		if (--timeout == 0) {
150547e1f66Speter 			DPRINTF(("j720ssp_readwrite: timeout 1\n"));
151b57d496dStoshii 			return -1;
152b57d496dStoshii 		}
153b57d496dStoshii 
154b57d496dStoshii 	*out = bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
1550c9a60ffSpeter 	BIT_INVERT(*out);
156b57d496dStoshii 
157b57d496dStoshii 	return 0;
158b57d496dStoshii }
159