1/*
2 * Copyright (c) 2001 HAMAJIMA Katsuomi. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: vrdmaau.c,v 1.6 2012/10/27 17:17:56 chs Exp $");
28
29#include <sys/param.h>
30#include <sys/bus.h>
31#include <sys/device.h>
32#include <sys/systm.h>
33
34#include <mips/cpuregs.h>
35
36#include <hpcmips/vr/vripif.h>
37#include <hpcmips/vr/dmaaureg.h>
38
39#ifdef VRDMAAU_DEBUG
40int vrdmaau_debug = VRDMAAU_DEBUG;
41#define DPRINTFN(n,x) if (vrdmaau_debug>(n)) printf x;
42#else
43#define DPRINTFN(n,x)
44#endif
45
46struct vrdmaau_softc {
47	device_t		sc_dev;
48	bus_space_tag_t		sc_iot;
49	bus_space_handle_t	sc_ioh;
50	struct vrdmaau_chipset_tag	sc_chipset;
51};
52
53int vrdmaau_match(device_t, cfdata_t, void *);
54void vrdmaau_attach(device_t, device_t, void *);
55
56CFATTACH_DECL_NEW(vrdmaau, sizeof(struct vrdmaau_softc),
57    vrdmaau_match, vrdmaau_attach, NULL, NULL);
58
59int vrdmaau_set_aiuin(vrdmaau_chipset_tag_t, void *);
60int vrdmaau_set_aiuout(vrdmaau_chipset_tag_t, void *);
61int vrdmaau_set_fir(vrdmaau_chipset_tag_t, void *);
62static int vrdmaau_phy_addr(struct vrdmaau_softc *, void *, u_int32_t *);
63
64int
65vrdmaau_match(device_t parent, cfdata_t cf, void *aux)
66{
67	return 2; /* 1st attach group of vrip */
68}
69
70void
71vrdmaau_attach(device_t parent, device_t self, void *aux)
72{
73	struct vrip_attach_args *va = aux;
74	struct vrdmaau_softc *sc = device_private(self);
75
76	sc->sc_dev = self;
77	sc->sc_iot = va->va_iot;
78	sc->sc_chipset.ac_sc = sc;
79	sc->sc_chipset.ac_set_aiuin = vrdmaau_set_aiuin;
80	sc->sc_chipset.ac_set_aiuout = vrdmaau_set_aiuout;
81	sc->sc_chipset.ac_set_fir = vrdmaau_set_fir;
82
83	if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
84			  0 /* no flags */, &sc->sc_ioh)) {
85		printf(": can't map i/o space\n");
86		return;
87	}
88	printf("\n");
89	vrip_register_dmaau(va->va_vc, &sc->sc_chipset);
90}
91
92int
93vrdmaau_set_aiuin(vrdmaau_chipset_tag_t ac, void *addr)
94{
95	struct vrdmaau_softc *sc = ac->ac_sc;
96	u_int32_t phy;
97	int err;
98
99	DPRINTFN(1, ("vrdmaau_set_aiuin: address %p\n", addr));
100
101	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
102		return err;
103
104	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUIBAH_REG_W, phy >> 16);
105	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUIBAL_REG_W, phy & 0xffff);
106	return 0;
107}
108
109int
110vrdmaau_set_aiuout(vrdmaau_chipset_tag_t ac, void *addr)
111{
112	struct vrdmaau_softc *sc = ac->ac_sc;
113	u_int32_t phy;
114	int err;
115
116	DPRINTFN(1, ("vrdmaau_set_aiuout: address %p\n", addr));
117
118	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
119		return err;
120
121	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUOBAH_REG_W, phy >> 16);
122	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUOBAL_REG_W, phy & 0xffff);
123	return 0;
124}
125
126int
127vrdmaau_set_fir(vrdmaau_chipset_tag_t ac, void *addr)
128{
129	struct vrdmaau_softc *sc = ac->ac_sc;
130	u_int32_t phy;
131	int err;
132
133	DPRINTFN(1, ("vrdmaau_set_fir: address %p\n", addr));
134
135	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
136		return err;
137
138	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FIRBAH_REG_W, phy >> 16);
139	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FIRBAL_REG_W, phy & 0xffff);
140	return 0;
141}
142
143static int
144vrdmaau_phy_addr(struct vrdmaau_softc *sc, void *addr, u_int32_t *phy)
145{
146	DPRINTFN(1, ("vrdmaau_phy_addr\n"));
147
148	if (addr >= (void *)MIPS_KSEG0_START &&
149	    addr < (void *)MIPS_KSEG1_START)
150		*phy = MIPS_KSEG0_TO_PHYS(addr);
151	else if (addr >= (void *)MIPS_KSEG1_START &&
152		 addr < (void *)MIPS_KSEG2_START)
153		*phy = MIPS_KSEG1_TO_PHYS(addr);
154	else {
155		DPRINTFN(0, ("vrdmaau_map_addr: invalid address %p\n", addr));
156		return EFAULT;
157	}
158
159#ifdef DIAGNOSTIC
160	if ((*phy & (VRDMAAU_ALIGNMENT - 1)) ||
161	    *phy >= VRDMAAU_BOUNCE_THRESHOLD ) {
162		printf("%s: vrdmaau_phy_addr: invalid address %p\n",
163		       device_xname(sc->sc_dev), addr);
164		return EINVAL;
165	}
166#endif
167	return 0;
168}
169