2 * Copyright (c) 1998 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/lib/libio/bwx.c,v 1.4.2.1 2000/12/11 01:03:20 obrien Exp $
29 #include <sys/param.h>
31 #include <sys/fcntl.h>
32 #include <sys/sysctl.h>
35 #include <machine/bwx.h>
36 #include <machine/sysarch.h>
40 #define mb() __asm__ __volatile__("mb" : : : "memory")
41 #define wmb() __asm__ __volatile__("wmb" : : : "memory")
43 static int mem_fd; /* file descriptor to /dev/mem */
44 static void *bwx_int1_ports; /* mapped int1 io ports */
45 static void *bwx_int2_ports; /* mapped int2 io ports */
46 static void *bwx_int4_ports; /* mapped int4 io ports */
47 static u_int64_t bwx_io_base; /* physical address of ports */
48 static u_int64_t bwx_mem_base; /* physical address of bwx mem */
53 size_t len = sizeof(u_int64_t);
56 mem_fd = open(_PATH_MEM, O_RDWR);
59 bwx_int1_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
60 bwx_int2_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
61 bwx_int4_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
63 if ((error = sysctlbyname("hw.chipset.ports", &bwx_io_base, &len,
65 err(1, "hw.chipset.ports");
66 if ((error = sysctlbyname("hw.chipset.memory", &bwx_mem_base, &len,
68 err(1, "hw.chipset.memory");
72 bwx_ioperm(u_int32_t from, u_int32_t num, int on)
80 return -1; /* XXX can't unmap yet */
82 start = trunc_page(from);
83 end = round_page(from + num);
85 munmap(bwx_int1_ports + start, end-start);
86 munmap(bwx_int2_ports + start, end-start);
87 munmap(bwx_int4_ports + start, end-start);
88 mmap(bwx_int1_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
89 mem_fd, bwx_io_base + BWX_EV56_INT1 + start);
90 mmap(bwx_int2_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
91 mem_fd, bwx_io_base + BWX_EV56_INT2 + start);
92 mmap(bwx_int4_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
93 mem_fd, bwx_io_base + BWX_EV56_INT4 + start);
98 bwx_inb(u_int32_t port)
101 return ldbu((vm_offset_t)bwx_int1_ports + port);
105 bwx_inw(u_int32_t port)
108 return ldwu((vm_offset_t)bwx_int2_ports + port);
112 bwx_inl(u_int32_t port)
115 return ldl((vm_offset_t)bwx_int4_ports + port);
119 bwx_outb(u_int32_t port, u_int8_t val)
121 stb((vm_offset_t)bwx_int1_ports + port, val);
126 bwx_outw(u_int32_t port, u_int16_t val)
128 stw((vm_offset_t)bwx_int2_ports + port, val);
133 bwx_outl(u_int32_t port, u_int32_t val)
135 stl((vm_offset_t)bwx_int4_ports + port, val);
139 struct bwx_mem_handle {
140 void *virt1; /* int1 address in user address-space */
141 void *virt2; /* int2 address in user address-space */
142 void *virt4; /* int4 address in user address-space */
146 bwx_map_memory(u_int32_t address, u_int32_t size)
148 struct bwx_mem_handle *h;
149 h = malloc(sizeof(struct bwx_mem_handle));
151 h->virt1 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
152 mem_fd, bwx_mem_base + BWX_EV56_INT1 + address);
153 if ((long) h->virt1 == -1) {
157 h->virt2 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
158 mem_fd, bwx_mem_base + BWX_EV56_INT2 + address);
159 if ((long) h->virt2 == -1) {
160 munmap(h->virt1, size);
164 h->virt4 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
165 mem_fd, bwx_mem_base + BWX_EV56_INT4 + address);
166 if ((long) h->virt4 == -1) {
167 munmap(h->virt1, size);
168 munmap(h->virt2, size);
176 bwx_unmap_memory(void *handle, u_int32_t size)
178 struct bwx_mem_handle *h = handle;
179 munmap(h->virt1, size);
180 munmap(h->virt2, size);
181 munmap(h->virt4, size);
186 bwx_readb(void *handle, u_int32_t offset)
188 struct bwx_mem_handle *h = handle;
189 return ldbu((vm_offset_t)h->virt1 + offset);
193 bwx_readw(void *handle, u_int32_t offset)
195 struct bwx_mem_handle *h = handle;
196 return ldwu((vm_offset_t)h->virt2 + offset);
200 bwx_readl(void *handle, u_int32_t offset)
202 struct bwx_mem_handle *h = handle;
203 return ldl((vm_offset_t)h->virt4 + offset);
207 bwx_writeb(void *handle, u_int32_t offset, u_int8_t val)
209 struct bwx_mem_handle *h = handle;
210 stb_nb((vm_offset_t)h->virt1 + offset, val);
214 bwx_writew(void *handle, u_int32_t offset, u_int16_t val)
216 struct bwx_mem_handle *h = handle;
217 stw_nb((vm_offset_t)h->virt2 + offset, val);
221 bwx_writel(void *handle, u_int32_t offset, u_int32_t val)
223 struct bwx_mem_handle *h = handle;
224 stl_nb((vm_offset_t)h->virt4 + offset, val);
227 struct io_ops bwx_io_ops = {