xref: /netbsd-src/sys/arch/hpcmips/vr/vrdmaau.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
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/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 	device_t		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(device_t, cfdata_t, void *);
53 void vrdmaau_attach(device_t, device_t, void *);
54 
55 CFATTACH_DECL_NEW(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(device_t parent, cfdata_t cf, void *aux)
65 {
66 	return 2; /* 1st attach group of vrip */
67 }
68 
69 void
70 vrdmaau_attach(device_t parent, device_t self, void *aux)
71 {
72 	struct vrip_attach_args *va = aux;
73 	struct vrdmaau_softc *sc = device_private(self);
74 
75 	sc->sc_dev = self;
76 	sc->sc_iot = va->va_iot;
77 	sc->sc_chipset.ac_sc = sc;
78 	sc->sc_chipset.ac_set_aiuin = vrdmaau_set_aiuin;
79 	sc->sc_chipset.ac_set_aiuout = vrdmaau_set_aiuout;
80 	sc->sc_chipset.ac_set_fir = vrdmaau_set_fir;
81 
82 	if (bus_space_map(sc->sc_iot, va->va_addr, va->va_size,
83 			  0 /* no flags */, &sc->sc_ioh)) {
84 		printf(": can't map i/o space\n");
85 		return;
86 	}
87 	printf("\n");
88 	vrip_register_dmaau(va->va_vc, &sc->sc_chipset);
89 }
90 
91 int
92 vrdmaau_set_aiuin(vrdmaau_chipset_tag_t ac, void *addr)
93 {
94 	struct vrdmaau_softc *sc = ac->ac_sc;
95 	u_int32_t phy;
96 	int err;
97 
98 	DPRINTFN(1, ("vrdmaau_set_aiuin: address %p\n", addr));
99 
100 	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
101 		return err;
102 
103 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUIBAH_REG_W, phy >> 16);
104 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUIBAL_REG_W, phy & 0xffff);
105 	return 0;
106 }
107 
108 int
109 vrdmaau_set_aiuout(vrdmaau_chipset_tag_t ac, void *addr)
110 {
111 	struct vrdmaau_softc *sc = ac->ac_sc;
112 	u_int32_t phy;
113 	int err;
114 
115 	DPRINTFN(1, ("vrdmaau_set_aiuout: address %p\n", addr));
116 
117 	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
118 		return err;
119 
120 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUOBAH_REG_W, phy >> 16);
121 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, AIUOBAL_REG_W, phy & 0xffff);
122 	return 0;
123 }
124 
125 int
126 vrdmaau_set_fir(vrdmaau_chipset_tag_t ac, void *addr)
127 {
128 	struct vrdmaau_softc *sc = ac->ac_sc;
129 	u_int32_t phy;
130 	int err;
131 
132 	DPRINTFN(1, ("vrdmaau_set_fir: address %p\n", addr));
133 
134 	if ((err = vrdmaau_phy_addr(sc, addr, &phy)))
135 		return err;
136 
137 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FIRBAH_REG_W, phy >> 16);
138 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, FIRBAL_REG_W, phy & 0xffff);
139 	return 0;
140 }
141 
142 static int
143 vrdmaau_phy_addr(struct vrdmaau_softc *sc, void *addr, u_int32_t *phy)
144 {
145 	DPRINTFN(1, ("vrdmaau_phy_addr\n"));
146 
147 	if (addr >= (void *)MIPS_KSEG0_START &&
148 	    addr < (void *)MIPS_KSEG1_START)
149 		*phy = MIPS_KSEG0_TO_PHYS(addr);
150 	else if (addr >= (void *)MIPS_KSEG1_START &&
151 		 addr < (void *)MIPS_KSEG2_START)
152 		*phy = MIPS_KSEG1_TO_PHYS(addr);
153 	else {
154 		DPRINTFN(0, ("vrdmaau_map_addr: invalid address %p\n", addr));
155 		return EFAULT;
156 	}
157 
158 #ifdef DIAGNOSTIC
159 	if ((*phy & (VRDMAAU_ALIGNMENT - 1)) ||
160 	    *phy >= VRDMAAU_BOUNCE_THRESHOLD ) {
161 		printf("%s: vrdmaau_phy_addr: invalid address %p\n",
162 		       device_xname(sc->sc_dev), addr);
163 		return EINVAL;
164 	}
165 #endif
166 	return 0;
167 }
168