xref: /netbsd/sys/arch/hpcmips/vr/vrdmaau.c (revision 6550d01e)
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.5 2005/12/11 12:17:34 christos Exp $");
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/device.h>
32 
33 #include <machine/bus.h>
34 
35 #include <hpcmips/vr/vripif.h>
36 #include <hpcmips/vr/dmaaureg.h>
37 
38 #ifdef VRDMAAU_DEBUG
39 int vrdmaau_debug = VRDMAAU_DEBUG;
40 #define DPRINTFN(n,x) if (vrdmaau_debug>(n)) printf x;
41 #else
42 #define DPRINTFN(n,x)
43 #endif
44 
45 struct vrdmaau_softc {
46 	struct device		sc_dev;
47 	bus_space_tag_t		sc_iot;
48 	bus_space_handle_t	sc_ioh;
49 	struct vrdmaau_chipset_tag	sc_chipset;
50 };
51 
52 int vrdmaau_match(struct device *, struct cfdata *, void *);
53 void vrdmaau_attach(struct device *, struct device *, void *);
54 
55 CFATTACH_DECL(vrdmaau, sizeof(struct vrdmaau_softc),
56     vrdmaau_match, vrdmaau_attach, NULL, NULL);
57 
58 int vrdmaau_set_aiuin(vrdmaau_chipset_tag_t, void *);
59 int vrdmaau_set_aiuout(vrdmaau_chipset_tag_t, void *);
60 int vrdmaau_set_fir(vrdmaau_chipset_tag_t, void *);
61 static int vrdmaau_phy_addr(struct vrdmaau_softc *, void *, u_int32_t *);
62 
63 int
64 vrdmaau_match(struct device *parent, struct cfdata *cf, void *aux)
65 {
66 	return 2; /* 1st attach group of vrip */
67 }
68 
69 void
70 vrdmaau_attach(struct device *parent, struct device *self, void *aux)
71 {
72 	struct vrip_attach_args *va = aux;
73 	struct vrdmaau_softc *sc = (void*)self;
74 
75 	sc->sc_iot = va->va_iot;
76 	sc->sc_chipset.ac_sc = sc;
77 	sc->sc_chipset.ac_set_aiuin = vrdmaau_set_aiuin;
78 	sc->sc_chipset.ac_set_aiuout = vrdmaau_set_aiuout;
79 	sc->sc_chipset.ac_set_fir = vrdmaau_set_fir;
80 
81 	if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
82 			  0 /* no flags */, &sc->sc_ioh)) {
83 		printf(": can't map i/o space\n");
84 		return;
85 	}
86 	printf("\n");
87 	vrip_register_dmaau(va->va_vc, &sc->sc_chipset);
88 }
89 
90 int
91 vrdmaau_set_aiuin(vrdmaau_chipset_tag_t ac, void *addr)
92 {
93 	struct vrdmaau_softc *sc = ac->ac_sc;
94 	u_int32_t phy;
95 	int err;
96 
97 	DPRINTFN(1, ("vrdmaau_set_aiuin: address %p\n", addr));
98 
99 	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
100 		return err;
101 
102 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUIBAH_REG_W, phy >> 16);
103 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUIBAL_REG_W, phy & 0xffff);
104 	return 0;
105 }
106 
107 int
108 vrdmaau_set_aiuout(vrdmaau_chipset_tag_t ac, void *addr)
109 {
110 	struct vrdmaau_softc *sc = ac->ac_sc;
111 	u_int32_t phy;
112 	int err;
113 
114 	DPRINTFN(1, ("vrdmaau_set_aiuout: address %p\n", addr));
115 
116 	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
117 		return err;
118 
119 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUOBAH_REG_W, phy >> 16);
120 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUOBAL_REG_W, phy & 0xffff);
121 	return 0;
122 }
123 
124 int
125 vrdmaau_set_fir(vrdmaau_chipset_tag_t ac, void *addr)
126 {
127 	struct vrdmaau_softc *sc = ac->ac_sc;
128 	u_int32_t phy;
129 	int err;
130 
131 	DPRINTFN(1, ("vrdmaau_set_fir: address %p\n", addr));
132 
133 	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
134 		return err;
135 
136 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FIRBAH_REG_W, phy >> 16);
137 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FIRBAL_REG_W, phy & 0xffff);
138 	return 0;
139 }
140 
141 static int
142 vrdmaau_phy_addr(struct vrdmaau_softc *sc, void *addr, u_int32_t *phy)
143 {
144 	DPRINTFN(1, ("vrdmaau_phy_addr\n"));
145 
146 	if (addr >= (void *)MIPS_KSEG0_START &&
147 	    addr < (void *)MIPS_KSEG1_START)
148 		*phy = MIPS_KSEG0_TO_PHYS(addr);
149 	else if (addr >= (void *)MIPS_KSEG1_START &&
150 		 addr < (void *)MIPS_KSEG2_START)
151 		*phy = MIPS_KSEG1_TO_PHYS(addr);
152 	else {
153 		DPRINTFN(0, ("vrdmaau_map_addr: invalid address %p\n", addr));
154 		return EFAULT;
155 	}
156 
157 #ifdef DIAGNOSTIC
158 	if ((*phy & (VRDMAAU_ALIGNMENT - 1)) ||
159 	    *phy >= VRDMAAU_BOUNCE_THRESHOLD ) {
160 		printf("%s: vrdmaau_phy_addr: invalid address %p\n",
161 		       sc->sc_dev.dv_xname, addr);
162 		return EINVAL;
163 	}
164 #endif
165 	return 0;
166 }
167