io.c revision 1.6 1 1.6 riastrad /* $NetBSD: io.c,v 1.6 2022/02/16 23:49:27 riastradh Exp $ */
2 1.1 nonaka
3 1.1 nonaka /*-
4 1.1 nonaka * Copyright (C) 1995-1997 Gary Thomas (gdt (at) linuxppc.org)
5 1.1 nonaka * All rights reserved.
6 1.1 nonaka *
7 1.1 nonaka * PCI/ISA I/O support
8 1.1 nonaka *
9 1.1 nonaka * Redistribution and use in source and binary forms, with or without
10 1.1 nonaka * modification, are permitted provided that the following conditions
11 1.1 nonaka * are met:
12 1.1 nonaka * 1. Redistributions of source code must retain the above copyright
13 1.1 nonaka * notice, this list of conditions and the following disclaimer.
14 1.1 nonaka * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 nonaka * notice, this list of conditions and the following disclaimer in the
16 1.1 nonaka * documentation and/or other materials provided with the distribution.
17 1.1 nonaka * 3. All advertising materials mentioning features or use of this software
18 1.1 nonaka * must display the following acknowledgement:
19 1.1 nonaka * This product includes software developed by Gary Thomas.
20 1.1 nonaka * 4. The name of the author may not be used to endorse or promote products
21 1.1 nonaka * derived from this software without specific prior written permission.
22 1.1 nonaka *
23 1.1 nonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1 nonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 nonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 nonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1 nonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1 nonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1 nonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 nonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1 nonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1 nonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 nonaka */
34 1.1 nonaka
35 1.1 nonaka #include <lib/libsa/stand.h>
36 1.3 garbled #include <sys/bswap.h>
37 1.1 nonaka #include "boot.h"
38 1.1 nonaka
39 1.3 garbled
40 1.5 kiyohara volatile u_char *PCI_mem = (u_char *)0xc0000000;
41 1.1 nonaka volatile u_char *ISA_io = (u_char *)0x80000000;
42 1.3 garbled volatile u_char *ISA_mem = (u_char *)0xc0000000;
43 1.3 garbled volatile char *videomem = (char *)0xc00b8000; /* + vram offset */
44 1.3 garbled
45 1.5 kiyohara static int dcache_line_size = 32;
46 1.5 kiyohara
47 1.1 nonaka
48 1.1 nonaka void
49 1.2 garbled outb(int port, char val)
50 1.1 nonaka {
51 1.1 nonaka
52 1.1 nonaka ISA_io[port] = val;
53 1.1 nonaka }
54 1.1 nonaka
55 1.3 garbled inline void
56 1.3 garbled outw(int port, u_int16_t val)
57 1.3 garbled {
58 1.3 garbled outb(port, val>>8);
59 1.3 garbled outb(port+1, val);
60 1.3 garbled }
61 1.3 garbled
62 1.1 nonaka u_char
63 1.2 garbled inb(int port)
64 1.1 nonaka {
65 1.1 nonaka
66 1.1 nonaka return (ISA_io[port]);
67 1.1 nonaka }
68 1.1 nonaka
69 1.5 kiyohara u_char
70 1.5 kiyohara readb(u_long addr)
71 1.5 kiyohara {
72 1.5 kiyohara
73 1.5 kiyohara return PCI_mem[addr];
74 1.5 kiyohara }
75 1.5 kiyohara
76 1.5 kiyohara u_short
77 1.5 kiyohara readw(u_long addr)
78 1.5 kiyohara {
79 1.5 kiyohara
80 1.5 kiyohara return le16toh(*((u_short *)&PCI_mem[addr]));
81 1.5 kiyohara }
82 1.5 kiyohara
83 1.1 nonaka u_long
84 1.5 kiyohara readl(u_long addr)
85 1.5 kiyohara {
86 1.5 kiyohara
87 1.5 kiyohara return le32toh(*((u_long *)&PCI_mem[addr]));
88 1.5 kiyohara }
89 1.5 kiyohara
90 1.5 kiyohara void
91 1.5 kiyohara writeb(u_long addr, u_char val)
92 1.5 kiyohara {
93 1.5 kiyohara
94 1.5 kiyohara PCI_mem[addr] = val;
95 1.5 kiyohara }
96 1.5 kiyohara
97 1.5 kiyohara void
98 1.5 kiyohara writel(u_long addr, u_long val)
99 1.5 kiyohara {
100 1.5 kiyohara
101 1.5 kiyohara *((u_long *)&PCI_mem[addr]) = htole32(val);
102 1.5 kiyohara }
103 1.5 kiyohara
104 1.5 kiyohara void
105 1.5 kiyohara _wbinv(uint32_t adr, uint32_t siz)
106 1.1 nonaka {
107 1.5 kiyohara uint32_t bnd;
108 1.1 nonaka
109 1.6 riastrad asm volatile("eieio" ::: "memory");
110 1.5 kiyohara for (bnd = adr + siz; adr < bnd; adr += dcache_line_size)
111 1.6 riastrad asm volatile("dcbf 0,%0" :: "r"(adr) : "memory");
112 1.6 riastrad asm volatile("sync" ::: "memory");
113 1.1 nonaka }
114 1.3 garbled
115 1.3 garbled void
116 1.5 kiyohara _inv(uint32_t adr, uint32_t siz)
117 1.3 garbled {
118 1.5 kiyohara uint32_t bnd, off;
119 1.3 garbled
120 1.5 kiyohara off = adr & (dcache_line_size - 1);
121 1.5 kiyohara adr -= off;
122 1.5 kiyohara siz += off;
123 1.6 riastrad asm volatile("eieio" ::: "memory");
124 1.5 kiyohara if (off != 0) {
125 1.5 kiyohara /* wbinv() leading unaligned dcache line */
126 1.6 riastrad asm volatile("dcbf 0,%0" :: "r"(adr) : "memory");
127 1.5 kiyohara if (siz < dcache_line_size)
128 1.5 kiyohara goto done;
129 1.5 kiyohara adr += dcache_line_size;
130 1.5 kiyohara siz -= dcache_line_size;
131 1.5 kiyohara }
132 1.5 kiyohara bnd = adr + siz;
133 1.5 kiyohara off = bnd & (dcache_line_size - 1);
134 1.5 kiyohara if (off != 0) {
135 1.5 kiyohara /* wbinv() trailing unaligned dcache line */
136 1.6 riastrad asm volatile("dcbf 0,%0" :: "r"(bnd) : "memory"); /* it's OK */
137 1.5 kiyohara if (siz < dcache_line_size)
138 1.5 kiyohara goto done;
139 1.5 kiyohara siz -= off;
140 1.3 garbled }
141 1.5 kiyohara for (bnd = adr + siz; adr < bnd; adr += dcache_line_size) {
142 1.5 kiyohara /* inv() intermediate dcache lines if ever */
143 1.6 riastrad asm volatile("dcbi 0,%0" :: "r"(adr) : "memory");
144 1.5 kiyohara }
145 1.5 kiyohara done:
146 1.6 riastrad asm volatile("sync" ::: "memory");
147 1.3 garbled }
148 1.3 garbled
149 1.5 kiyohara u_long
150 1.5 kiyohara local_to_PCI(u_long addr)
151 1.3 garbled {
152 1.3 garbled
153 1.5 kiyohara return ((addr & 0x7FFFFFFF) | 0x80000000);
154 1.3 garbled }
155