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 $
27 * $DragonFly: src/lib/libio/Attic/bwx.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
30 #include <sys/param.h>
32 #include <sys/fcntl.h>
33 #include <sys/sysctl.h>
36 #include <machine/bwx.h>
37 #include <machine/sysarch.h>
41 #define mb() __asm__ __volatile__("mb" : : : "memory")
42 #define wmb() __asm__ __volatile__("wmb" : : : "memory")
44 static int mem_fd; /* file descriptor to /dev/mem */
45 static void *bwx_int1_ports; /* mapped int1 io ports */
46 static void *bwx_int2_ports; /* mapped int2 io ports */
47 static void *bwx_int4_ports; /* mapped int4 io ports */
48 static u_int64_t bwx_io_base; /* physical address of ports */
49 static u_int64_t bwx_mem_base; /* physical address of bwx mem */
54 size_t len = sizeof(u_int64_t);
57 mem_fd = open(_PATH_MEM, O_RDWR);
60 bwx_int1_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
61 bwx_int2_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
62 bwx_int4_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
64 if ((error = sysctlbyname("hw.chipset.ports", &bwx_io_base, &len,
66 err(1, "hw.chipset.ports");
67 if ((error = sysctlbyname("hw.chipset.memory", &bwx_mem_base, &len,
69 err(1, "hw.chipset.memory");
73 bwx_ioperm(u_int32_t from, u_int32_t num, int on)
81 return -1; /* XXX can't unmap yet */
83 start = trunc_page(from);
84 end = round_page(from + num);
86 munmap(bwx_int1_ports + start, end-start);
87 munmap(bwx_int2_ports + start, end-start);
88 munmap(bwx_int4_ports + start, end-start);
89 mmap(bwx_int1_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
90 mem_fd, bwx_io_base + BWX_EV56_INT1 + start);
91 mmap(bwx_int2_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
92 mem_fd, bwx_io_base + BWX_EV56_INT2 + start);
93 mmap(bwx_int4_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
94 mem_fd, bwx_io_base + BWX_EV56_INT4 + start);
99 bwx_inb(u_int32_t port)
102 return ldbu((vm_offset_t)bwx_int1_ports + port);
106 bwx_inw(u_int32_t port)
109 return ldwu((vm_offset_t)bwx_int2_ports + port);
113 bwx_inl(u_int32_t port)
116 return ldl((vm_offset_t)bwx_int4_ports + port);
120 bwx_outb(u_int32_t port, u_int8_t val)
122 stb((vm_offset_t)bwx_int1_ports + port, val);
127 bwx_outw(u_int32_t port, u_int16_t val)
129 stw((vm_offset_t)bwx_int2_ports + port, val);
134 bwx_outl(u_int32_t port, u_int32_t val)
136 stl((vm_offset_t)bwx_int4_ports + port, val);
140 struct bwx_mem_handle {
141 void *virt1; /* int1 address in user address-space */
142 void *virt2; /* int2 address in user address-space */
143 void *virt4; /* int4 address in user address-space */
147 bwx_map_memory(u_int32_t address, u_int32_t size)
149 struct bwx_mem_handle *h;
150 h = malloc(sizeof(struct bwx_mem_handle));
152 h->virt1 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
153 mem_fd, bwx_mem_base + BWX_EV56_INT1 + address);
154 if ((long) h->virt1 == -1) {
158 h->virt2 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
159 mem_fd, bwx_mem_base + BWX_EV56_INT2 + address);
160 if ((long) h->virt2 == -1) {
161 munmap(h->virt1, size);
165 h->virt4 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
166 mem_fd, bwx_mem_base + BWX_EV56_INT4 + address);
167 if ((long) h->virt4 == -1) {
168 munmap(h->virt1, size);
169 munmap(h->virt2, size);
177 bwx_unmap_memory(void *handle, u_int32_t size)
179 struct bwx_mem_handle *h = handle;
180 munmap(h->virt1, size);
181 munmap(h->virt2, size);
182 munmap(h->virt4, size);
187 bwx_readb(void *handle, u_int32_t offset)
189 struct bwx_mem_handle *h = handle;
190 return ldbu((vm_offset_t)h->virt1 + offset);
194 bwx_readw(void *handle, u_int32_t offset)
196 struct bwx_mem_handle *h = handle;
197 return ldwu((vm_offset_t)h->virt2 + offset);
201 bwx_readl(void *handle, u_int32_t offset)
203 struct bwx_mem_handle *h = handle;
204 return ldl((vm_offset_t)h->virt4 + offset);
208 bwx_writeb(void *handle, u_int32_t offset, u_int8_t val)
210 struct bwx_mem_handle *h = handle;
211 stb_nb((vm_offset_t)h->virt1 + offset, val);
215 bwx_writew(void *handle, u_int32_t offset, u_int16_t val)
217 struct bwx_mem_handle *h = handle;
218 stw_nb((vm_offset_t)h->virt2 + offset, val);
222 bwx_writel(void *handle, u_int32_t offset, u_int32_t val)
224 struct bwx_mem_handle *h = handle;
225 stl_nb((vm_offset_t)h->virt4 + offset, val);
228 struct io_ops bwx_io_ops = {