xref: /netbsd-src/sys/arch/mips/adm5120/adm5120_obio.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /* $NetBSD: adm5120_obio.c,v 1.6 2021/08/07 16:18:58 thorpej Exp $ */
2320845ddSdyoung 
3320845ddSdyoung /*-
4320845ddSdyoung  * Copyright (c) 2007 Ruslan Ermilov and Vsevolod Lobko.
5320845ddSdyoung  * All rights reserved.
6320845ddSdyoung  *
7320845ddSdyoung  * Redistribution and use in source and binary forms, with or
8320845ddSdyoung  * without modification, are permitted provided that the following
9320845ddSdyoung  * conditions are met:
10320845ddSdyoung  * 1. Redistributions of source code must retain the above copyright
11320845ddSdyoung  *    notice, this list of conditions and the following disclaimer.
12320845ddSdyoung  * 2. Redistributions in binary form must reproduce the above
13320845ddSdyoung  *    copyright notice, this list of conditions and the following
14320845ddSdyoung  *    disclaimer in the documentation and/or other materials provided
15320845ddSdyoung  *    with the distribution.
16320845ddSdyoung  * 3. The names of the authors may not be used to endorse or promote
17320845ddSdyoung  *    products derived from this software without specific prior
18320845ddSdyoung  *    written permission.
19320845ddSdyoung  *
20320845ddSdyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY
21320845ddSdyoung  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22320845ddSdyoung  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23320845ddSdyoung  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS
24320845ddSdyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25320845ddSdyoung  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26320845ddSdyoung  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
27320845ddSdyoung  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28320845ddSdyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29320845ddSdyoung  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30320845ddSdyoung  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31320845ddSdyoung  * OF SUCH DAMAGE.
32320845ddSdyoung  */
33320845ddSdyoung /*
34320845ddSdyoung  * Copyright 2002 Wasabi Systems, Inc.
35320845ddSdyoung  * All rights reserved.
36320845ddSdyoung  *
37320845ddSdyoung  * Written by Simon Burge for Wasabi Systems, Inc.
38320845ddSdyoung  *
39320845ddSdyoung  * Redistribution and use in source and binary forms, with or without
40320845ddSdyoung  * modification, are permitted provided that the following conditions
41320845ddSdyoung  * are met:
42320845ddSdyoung  * 1. Redistributions of source code must retain the above copyright
43320845ddSdyoung  *    notice, this list of conditions and the following disclaimer.
44320845ddSdyoung  * 2. Redistributions in binary form must reproduce the above copyright
45320845ddSdyoung  *    notice, this list of conditions and the following disclaimer in the
46320845ddSdyoung  *    documentation and/or other materials provided with the distribution.
47320845ddSdyoung  * 3. All advertising materials mentioning features or use of this software
48320845ddSdyoung  *    must display the following acknowledgement:
49320845ddSdyoung  *      This product includes software developed for the NetBSD Project by
50320845ddSdyoung  *      Wasabi Systems, Inc.
51320845ddSdyoung  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
52320845ddSdyoung  *    or promote products derived from this software without specific prior
53320845ddSdyoung  *    written permission.
54320845ddSdyoung  *
55320845ddSdyoung  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
56320845ddSdyoung  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
57320845ddSdyoung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58320845ddSdyoung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
59320845ddSdyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60320845ddSdyoung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61320845ddSdyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
62320845ddSdyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63320845ddSdyoung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
64320845ddSdyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
65320845ddSdyoung  * POSSIBILITY OF SUCH DAMAGE.
66320845ddSdyoung  */
67320845ddSdyoung 
68320845ddSdyoung #include <sys/cdefs.h>
69*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: adm5120_obio.c,v 1.6 2021/08/07 16:18:58 thorpej Exp $");
70320845ddSdyoung 
71320845ddSdyoung #include <sys/param.h>
72320845ddSdyoung #include <sys/systm.h>
73320845ddSdyoung #include <sys/device.h>
74320845ddSdyoung 
75e265f67bSdyoung #include <sys/bus.h>
76320845ddSdyoung 
77320845ddSdyoung #include <mips/cache.h>
78320845ddSdyoung #include <mips/cpuregs.h>
79320845ddSdyoung 
80320845ddSdyoung #include <mips/adm5120/include/adm5120reg.h>
81320845ddSdyoung #include <mips/adm5120/include/adm5120var.h>
82320845ddSdyoung #include <mips/adm5120/include/adm5120_mainbusvar.h>
83320845ddSdyoung #include <mips/adm5120/include/adm5120_obiovar.h>
84320845ddSdyoung 
85320845ddSdyoung #include "locators.h"
86320845ddSdyoung 
87320845ddSdyoung #ifdef OBIO_DEBUG
88320845ddSdyoung int obio_debug = 1;
89320845ddSdyoung #define	OBIO_DPRINTF(__fmt, ...)		\
90320845ddSdyoung do {						\
91320845ddSdyoung 	if (obio_debug)			\
92320845ddSdyoung 		printf((__fmt), __VA_ARGS__);	\
93320845ddSdyoung } while (/*CONSTCOND*/0)
94320845ddSdyoung #else /* !OBIO_DEBUG */
95320845ddSdyoung #define	OBIO_DPRINTF(__fmt, ...)	do { } while (/*CONSTCOND*/0)
96320845ddSdyoung #endif /* OBIO_DEBUG */
97320845ddSdyoung 
983e67b512Smatt static int	obio_match(device_t, cfdata_t, void *);
993e67b512Smatt static void	obio_attach(device_t, device_t, void *);
1003e67b512Smatt static int	obio_submatch(device_t, cfdata_t, const int *, void *);
101320845ddSdyoung static int	obio_print(void *, const char *);
102320845ddSdyoung 
1033e67b512Smatt CFATTACH_DECL_NEW(obio, 0, obio_match, obio_attach, NULL, NULL);
104320845ddSdyoung 
105320845ddSdyoung /* There can be only one. */
106320845ddSdyoung int	obio_found;
107320845ddSdyoung 
108320845ddSdyoung struct obiodev {
109320845ddSdyoung 	const char	*od_name;
110320845ddSdyoung 	bus_addr_t	od_addr;
111320845ddSdyoung 	int		od_irq;
112320845ddSdyoung 	uint32_t	od_gpio_mask;
113320845ddSdyoung };
114320845ddSdyoung 
1153e67b512Smatt const struct obiodev obiodevs[] = {
116320845ddSdyoung 	{"uart",	ADM5120_BASE_UART0,	1, 0x0},
117320845ddSdyoung 	{"uart",	ADM5120_BASE_UART1,	2, 0x0},
118320845ddSdyoung 	{"admsw",	ADM5120_BASE_SWITCH,	9, 0x0},
119320845ddSdyoung 	{"ahci",	ADM5120_BASE_USB,	3, 0x0},
120320845ddSdyoung 	{"admflash",	ADM5120_BASE_SRAM0,	0, 0x0},
121320845ddSdyoung 	{NULL,		0,			0, 0x0},
122320845ddSdyoung };
123320845ddSdyoung 
124320845ddSdyoung static int
obio_match(device_t parent,cfdata_t match,void * aux)1253e67b512Smatt obio_match(device_t parent, cfdata_t match, void *aux)
126320845ddSdyoung {
127320845ddSdyoung 	return !obio_found;
128320845ddSdyoung }
129320845ddSdyoung 
130320845ddSdyoung static void
obio_attach_args_create(struct obio_attach_args * oa,const struct obiodev * od,void * gpio,bus_dma_tag_t dmat,bus_space_tag_t st)1313e67b512Smatt obio_attach_args_create(struct obio_attach_args *oa, const struct obiodev *od,
132320845ddSdyoung     void *gpio, bus_dma_tag_t dmat, bus_space_tag_t st)
133320845ddSdyoung {
134320845ddSdyoung 	oa->oba_name = od->od_name;
135320845ddSdyoung 	oa->oba_addr = od->od_addr;
136320845ddSdyoung 	oa->oba_irq = od->od_irq;
137320845ddSdyoung 	oa->oba_dt = dmat;
138320845ddSdyoung 	oa->oba_st = st;
139320845ddSdyoung 	oa->oba_gpio = gpio;
140320845ddSdyoung 	oa->oba_gpio_mask = od->od_gpio_mask;
141320845ddSdyoung }
142320845ddSdyoung 
143320845ddSdyoung static void
obio_attach(device_t parent,device_t self,void * aux)1443e67b512Smatt obio_attach(device_t parent, device_t self, void *aux)
145320845ddSdyoung {
146320845ddSdyoung 	struct mainbus_attach_args *ma = (struct mainbus_attach_args *)aux;
147320845ddSdyoung 	struct obio_attach_args oa;
1483e67b512Smatt 	const struct obiodev *od;
149320845ddSdyoung 
150320845ddSdyoung 	obio_found = 1;
151320845ddSdyoung 	printf("\n");
152320845ddSdyoung 
153320845ddSdyoung 	OBIO_DPRINTF("%s: %d\n", __func__, __LINE__);
154320845ddSdyoung 
155320845ddSdyoung 	OBIO_DPRINTF("%s: %d\n", __func__, __LINE__);
156320845ddSdyoung 	for (od = obiodevs; od->od_name != NULL; od++) {
157320845ddSdyoung 		OBIO_DPRINTF("%s: %d\n", __func__, __LINE__);
158320845ddSdyoung 		obio_attach_args_create(&oa, od, ma->ma_gpio, ma->ma_dmat,
159320845ddSdyoung 		    ma->ma_obiot);
160320845ddSdyoung 		OBIO_DPRINTF("%s: %d\n", __func__, __LINE__);
1612685996bSthorpej 		config_found(self, &oa, obio_print,
162*c7fb772bSthorpej 		    CFARGS(.submatch = obio_submatch,
163*c7fb772bSthorpej 			   .iattr = "obio"));
164320845ddSdyoung 	}
165320845ddSdyoung 	OBIO_DPRINTF("%s: %d\n", __func__, __LINE__);
166320845ddSdyoung }
167320845ddSdyoung 
168320845ddSdyoung static int
obio_submatch(device_t parent,cfdata_t cf,const int * ldesc,void * aux)1693e67b512Smatt obio_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
170320845ddSdyoung {
171320845ddSdyoung 	struct obio_attach_args *oa = aux;
172320845ddSdyoung 
173320845ddSdyoung 	if (cf->cf_loc[OBIOCF_ADDR] != OBIOCF_ADDR_DEFAULT &&
174320845ddSdyoung 	    cf->cf_loc[OBIOCF_ADDR] != oa->oba_addr)
175320845ddSdyoung 		return 0;
176320845ddSdyoung 
177320845ddSdyoung 	return config_match(parent, cf, aux);
178320845ddSdyoung }
179320845ddSdyoung 
180320845ddSdyoung static int
obio_print(void * aux,const char * pnp)181320845ddSdyoung obio_print(void *aux, const char *pnp)
182320845ddSdyoung {
183320845ddSdyoung 	struct obio_attach_args *oa = aux;
184320845ddSdyoung 
185320845ddSdyoung 	if (pnp != NULL)
186320845ddSdyoung 		aprint_normal("%s at %s", oa->oba_name, pnp);
187320845ddSdyoung 	if (oa->oba_addr != OBIOCF_ADDR_DEFAULT)
188047e93c4Smatt 		aprint_normal(" addr 0x%"PRIxBUSADDR, oa->oba_addr);
189320845ddSdyoung 	if (oa->oba_gpio_mask != OBIOCF_GPIO_MASK_DEFAULT)
190320845ddSdyoung 		aprint_normal(" gpio_mask 0x%02x", oa->oba_gpio_mask);
191320845ddSdyoung 
192320845ddSdyoung 	return UNCONF;
193320845ddSdyoung }
194