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