1/*	$OpenBSD: lebuffer.c,v 1.11 2022/03/13 13:34:54 mpi Exp $	*/
2/*	$NetBSD: lebuffer.c,v 1.12 2002/03/11 16:00:57 pk Exp $ */
3
4/*-
5 * Copyright (c) 1998 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Paul Kranenburg.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/errno.h>
37#include <sys/device.h>
38#include <sys/malloc.h>
39
40#include <machine/bus.h>
41#include <machine/autoconf.h>
42#include <machine/cpu.h>
43
44#include <dev/sbus/sbusvar.h>
45#include <dev/sbus/lebuffervar.h>
46
47int	lebufprint(void *, const char *);
48int	lebufmatch(struct device *, void *, void *);
49void	lebufattach(struct device *, struct device *, void *);
50
51const struct cfattach lebuffer_ca = {
52	sizeof(struct lebuf_softc), lebufmatch, lebufattach
53};
54
55int
56lebufprint(void *aux, const char *busname)
57{
58	struct sbus_attach_args *sa = aux;
59	bus_space_tag_t t = sa->sa_bustag;
60	struct lebuf_softc *sc = t->cookie;
61
62	sa->sa_bustag = sc->sc_bustag;	/* XXX */
63	sbus_print(aux, busname);	/* XXX */
64	sa->sa_bustag = t;		/* XXX */
65	return (UNCONF);
66}
67
68int
69lebufmatch(struct device *parent, void *vcf, void *aux)
70{
71	struct sbus_attach_args *sa = aux;
72	struct cfdata *cf = vcf;
73
74	return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
75}
76
77/*
78 * Attach all the sub-devices we can find
79 */
80void
81lebufattach(struct device *parent, struct device *self, void *aux)
82{
83	struct sbus_attach_args *sa = aux;
84	struct lebuf_softc *sc = (void *)self;
85	int node;
86	int sbusburst;
87	struct sparc_bus_space_tag *sbt;
88	bus_space_handle_t bh;
89
90	sc->sc_bustag = sa->sa_bustag;
91	sc->sc_dmatag = sa->sa_dmatag;
92
93	if (sbus_bus_map(sa->sa_bustag,
94	    sa->sa_slot, sa->sa_offset, sa->sa_size,
95	    BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
96		printf("%s: attach: cannot map registers\n", self->dv_xname);
97		return;
98	}
99
100	/*
101	 * This device's "register space" is just a buffer where the
102	 * Lance ring-buffers can be stored. Note the buffer's location
103	 * and size, so the `le' driver can pick them up.
104	 */
105	sc->sc_buffer = (void *)bus_space_vaddr(sa->sa_bustag, bh);
106	sc->sc_bufsiz = sa->sa_size;
107
108	node = sc->sc_node = sa->sa_node;
109
110	/*
111	 * Get transfer burst size from PROM
112	 */
113	sbusburst = ((struct sbus_softc *)parent)->sc_burst;
114	if (sbusburst == 0)
115		sbusburst = SBUS_BURST_32 - 1; /* 1->16 */
116
117	sc->sc_burst = getpropint(node, "burst-sizes", -1);
118	if (sc->sc_burst == -1)
119		/* take SBus burst sizes */
120		sc->sc_burst = sbusburst;
121
122	/* Clamp at parent's burst sizes */
123	sc->sc_burst &= sbusburst;
124
125	/* Allocate a bus tag */
126	sbt = malloc(sizeof(*sbt), M_DEVBUF, M_NOWAIT | M_ZERO);
127	if (sbt == NULL) {
128		printf("%s: attach: out of memory\n", self->dv_xname);
129		return;
130	}
131
132	printf(": %dK memory\n", sc->sc_bufsiz / 1024);
133
134	sbt->cookie = sc;
135	sbt->parent = sc->sc_bustag;
136	sbt->asi = sbt->parent->asi;
137	sbt->sasi = sbt->parent->sasi;
138
139	/* search through children */
140	for (node = firstchild(node); node; node = nextsibling(node)) {
141		struct sbus_attach_args sa;
142		sbus_setup_attach_args((struct sbus_softc *)parent,
143		    sbt, sc->sc_dmatag, node, &sa);
144		(void)config_found(&sc->sc_dev, (void *)&sa, lebufprint);
145		sbus_destroy_attach_args(&sa);
146	}
147}
148