1/*	$NetBSD: sbdio.c,v 1.7 2021/08/07 16:18:53 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2004, 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: sbdio.c,v 1.7 2021/08/07 16:18:53 thorpej Exp $");
34
35#define _EWS4800MIPS_BUS_DMA_PRIVATE
36
37#include <sys/param.h>
38#include <sys/bus.h>
39#include <sys/device.h>
40#include <sys/systm.h>
41
42#include <mips/locore.h>
43
44#include <machine/autoconf.h>
45#include <machine/sbdvar.h>
46#include <machine/sbdiovar.h>
47
48#include "ioconf.h"
49
50struct sbdio_softc {
51	device_t sc_dev;
52	struct ews4800mips_bus_space sc_bus_tag;
53	struct ews4800mips_bus_dma_tag sc_dma_tag;
54};
55
56static int sbdio_match(device_t, cfdata_t, void *);
57static void sbdio_attach(device_t, device_t, void *);
58static int sbdio_print(void *, const char *);
59
60CFATTACH_DECL_NEW(sbdio, sizeof(struct sbdio_softc),
61    sbdio_match, sbdio_attach, NULL, NULL);
62
63static int sbdio_found;
64
65int
66sbdio_match(device_t parent, cfdata_t cf, void *aux)
67{
68	struct mainbus_attach_args *ma = aux;
69
70	if (sbdio_found != 0)
71		return 0;
72
73	if (strcmp(ma->ma_name, sbdio_cd.cd_name) != 0)
74		return 0;
75
76	if (platform.sbdiodevs == NULL)
77		return 0;
78
79	return 1;
80}
81
82void
83sbdio_attach(device_t parent, device_t self, void *aux)
84{
85	struct sbdio_softc *sc = device_private(self);
86	struct sbdio_attach_args sa;
87	const struct sbdiodevdesc *sd;
88
89	sbdio_found = 1;
90
91	sc->sc_dev = self;
92	aprint_normal("\n");
93
94	/* structure assignment */
95	sc->sc_dma_tag = ews4800mips_default_bus_dma_tag;
96
97	bus_space_create(&sc->sc_bus_tag, device_xname(sc->sc_dev),
98	    MIPS_KSEG1_START, MIPS_KSEG2_START - MIPS_KSEG1_START); /* XXX */
99
100	for (sd = platform.sbdiodevs; sd->sd_name != NULL; sd++) {
101		sa.sa_name  = sd->sd_name;
102		sa.sa_bust  = &sc->sc_bus_tag;
103		sa.sa_dmat  = &sc->sc_dma_tag;
104		sa.sa_addr1 = sd->sd_addr1;
105		sa.sa_addr2 = sd->sd_addr2;
106		sa.sa_irq   = sd->sd_irq;
107		sa.sa_flags = sd->sd_flags;
108		config_found(self, &sa, sbdio_print, CFARGS_NONE);
109	}
110}
111
112int
113sbdio_print(void *aux, const char *pnp)
114{
115	struct sbdio_attach_args *sa = aux;
116
117	if (sa->sa_addr1 != (paddr_t)-1) {
118		aprint_normal(" at %#"PRIxPADDR, sa->sa_addr1);
119		if (sa->sa_addr2 != (paddr_t)-1)
120			aprint_normal(", %#"PRIxPADDR, sa->sa_addr2);
121	}
122	if (sa->sa_irq != -1)
123		aprint_normal(" irq %d", sa->sa_irq);
124
125	return pnp ? QUIET : UNCONF;
126}
127