1191289Sgonzo/*-
2191289Sgonzo * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3191289Sgonzo * All rights reserved.
4191289Sgonzo *
5191289Sgonzo * Redistribution and use in source and binary forms, with or without
6191289Sgonzo * modification, are permitted provided that the following conditions
7191289Sgonzo * are met:
8191289Sgonzo * 1. Redistributions of source code must retain the above copyright
9191289Sgonzo *    notice unmodified, this list of conditions, and the following
10191289Sgonzo *    disclaimer.
11191289Sgonzo * 2. Redistributions in binary form must reproduce the above copyright
12191289Sgonzo *    notice, this list of conditions and the following disclaimer in the
13191289Sgonzo *    documentation and/or other materials provided with the distribution.
14191289Sgonzo *
15191289Sgonzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16191289Sgonzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17191289Sgonzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18191289Sgonzo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19191289Sgonzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20191289Sgonzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21191289Sgonzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22191289Sgonzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23191289Sgonzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24191289Sgonzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25191289Sgonzo * SUCH DAMAGE.
26191289Sgonzo */
27191289Sgonzo#include <sys/cdefs.h>
28191289Sgonzo__FBSDID("$FreeBSD$");
29191289Sgonzo
30191289Sgonzo#include <sys/param.h>
31191289Sgonzo#include <sys/systm.h>
32191289Sgonzo#include <sys/bus.h>
33191289Sgonzo
34191289Sgonzo#include <machine/bus.h>
35191289Sgonzo#include <mips/atheros/ar71xx_bus_space_reversed.h>
36191289Sgonzo
37191289Sgonzostatic bs_r_1_proto(reversed);
38191289Sgonzostatic bs_r_2_proto(reversed);
39191289Sgonzostatic bs_w_1_proto(reversed);
40191289Sgonzostatic bs_w_2_proto(reversed);
41191289Sgonzo
42191289Sgonzo/*
43191289Sgonzo * Bus space that handles offsets in word for 1/2 bytes read/write access.
44191289Sgonzo * Byte order of values is handled by device drivers itself.
45191289Sgonzo */
46191289Sgonzostatic struct bus_space bus_space_reversed = {
47191289Sgonzo	/* cookie */
48191289Sgonzo	(void *) 0,
49191289Sgonzo
50191289Sgonzo	/* mapping/unmapping */
51191289Sgonzo	generic_bs_map,
52191289Sgonzo	generic_bs_unmap,
53191289Sgonzo	generic_bs_subregion,
54191289Sgonzo
55191289Sgonzo	/* allocation/deallocation */
56191289Sgonzo	NULL,
57191289Sgonzo	NULL,
58191289Sgonzo
59191289Sgonzo	/* barrier */
60191289Sgonzo	generic_bs_barrier,
61191289Sgonzo
62191289Sgonzo	/* read (single) */
63191289Sgonzo	reversed_bs_r_1,
64191289Sgonzo	reversed_bs_r_2,
65191289Sgonzo	generic_bs_r_4,
66191289Sgonzo	NULL,
67191289Sgonzo
68191289Sgonzo	/* read multiple */
69191289Sgonzo	generic_bs_rm_1,
70191289Sgonzo	generic_bs_rm_2,
71191289Sgonzo	generic_bs_rm_4,
72191289Sgonzo	NULL,
73191289Sgonzo
74191289Sgonzo	/* read region */
75191289Sgonzo	generic_bs_rr_1,
76191289Sgonzo	generic_bs_rr_2,
77191289Sgonzo	generic_bs_rr_4,
78191289Sgonzo	NULL,
79191289Sgonzo
80191289Sgonzo	/* write (single) */
81191289Sgonzo	reversed_bs_w_1,
82191289Sgonzo	reversed_bs_w_2,
83191289Sgonzo	generic_bs_w_4,
84191289Sgonzo	NULL,
85191289Sgonzo
86191289Sgonzo	/* write multiple */
87191289Sgonzo	generic_bs_wm_1,
88191289Sgonzo	generic_bs_wm_2,
89191289Sgonzo	generic_bs_wm_4,
90191289Sgonzo	NULL,
91191289Sgonzo
92191289Sgonzo	/* write region */
93191289Sgonzo	NULL,
94191289Sgonzo	generic_bs_wr_2,
95191289Sgonzo	generic_bs_wr_4,
96191289Sgonzo	NULL,
97191289Sgonzo
98191289Sgonzo	/* set multiple */
99191289Sgonzo	NULL,
100191289Sgonzo	NULL,
101191289Sgonzo	NULL,
102191289Sgonzo	NULL,
103191289Sgonzo
104191289Sgonzo	/* set region */
105191289Sgonzo	NULL,
106191289Sgonzo	generic_bs_sr_2,
107191289Sgonzo	generic_bs_sr_4,
108191289Sgonzo	NULL,
109191289Sgonzo
110191289Sgonzo	/* copy */
111191289Sgonzo	NULL,
112191289Sgonzo	generic_bs_c_2,
113191289Sgonzo	NULL,
114191289Sgonzo	NULL,
115191289Sgonzo
116191289Sgonzo	/* read (single) stream */
117191289Sgonzo	generic_bs_r_1,
118191289Sgonzo	generic_bs_r_2,
119191289Sgonzo	generic_bs_r_4,
120191289Sgonzo	NULL,
121191289Sgonzo
122191289Sgonzo	/* read multiple stream */
123191289Sgonzo	generic_bs_rm_1,
124191289Sgonzo	generic_bs_rm_2,
125191289Sgonzo	generic_bs_rm_4,
126191289Sgonzo	NULL,
127191289Sgonzo
128191289Sgonzo	/* read region stream */
129191289Sgonzo	generic_bs_rr_1,
130191289Sgonzo	generic_bs_rr_2,
131191289Sgonzo	generic_bs_rr_4,
132191289Sgonzo	NULL,
133191289Sgonzo
134191289Sgonzo	/* write (single) stream */
135191289Sgonzo	generic_bs_w_1,
136191289Sgonzo	generic_bs_w_2,
137191289Sgonzo	generic_bs_w_4,
138191289Sgonzo	NULL,
139191289Sgonzo
140191289Sgonzo	/* write multiple stream */
141191289Sgonzo	generic_bs_wm_1,
142191289Sgonzo	generic_bs_wm_2,
143191289Sgonzo	generic_bs_wm_4,
144191289Sgonzo	NULL,
145191289Sgonzo
146191289Sgonzo	/* write region stream */
147191289Sgonzo	NULL,
148191289Sgonzo	generic_bs_wr_2,
149191289Sgonzo	generic_bs_wr_4,
150191289Sgonzo	NULL,
151191289Sgonzo};
152191289Sgonzo
153191289Sgonzobus_space_tag_t ar71xx_bus_space_reversed = &bus_space_reversed;
154191289Sgonzo
155191289Sgonzostatic uint8_t
156191289Sgonzoreversed_bs_r_1(void *t, bus_space_handle_t h, bus_size_t o)
157191289Sgonzo{
158191289Sgonzo
159191289Sgonzo	return readb(h + (o &~ 3) + (3 - (o & 3)));
160191289Sgonzo}
161191289Sgonzo
162191289Sgonzostatic void
163191289Sgonzoreversed_bs_w_1(void *t, bus_space_handle_t h, bus_size_t o, u_int8_t v)
164191289Sgonzo{
165191289Sgonzo
166191289Sgonzo	writeb(h + (o &~ 3) + (3 - (o & 3)), v);
167191289Sgonzo}
168191289Sgonzo
169191289Sgonzostatic uint16_t
170191289Sgonzoreversed_bs_r_2(void *t, bus_space_handle_t h, bus_size_t o)
171191289Sgonzo{
172191289Sgonzo
173191289Sgonzo	return readw(h + (o &~ 3) + (2 - (o & 3)));
174191289Sgonzo}
175191289Sgonzo
176191289Sgonzostatic void
177191289Sgonzoreversed_bs_w_2(void *t, bus_space_handle_t h, bus_size_t o, uint16_t v)
178191289Sgonzo{
179191289Sgonzo
180191289Sgonzo	writew(h + (o &~ 3) + (2 - (o & 3)), v);
181191289Sgonzo}
182