1 /*
2  * cw-unix-pci.c - Unix specific PCI cw3 driver.
3  *
4  * Written by
5  *  Marco van den Heuvel <blackystardust68@yahoo.com>
6  *
7  * This file is part of VICE, the Versatile Commodore Emulator.
8  * See README for copyright notice.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23  *  02111-1307  USA.
24  *
25  */
26 
27 /* Tested and confirmed working on:
28 
29  - Linux 2.6 (/dev/port PCI I/O access)
30  - Linux 2.6 (permission based PCI I/O access)
31  */
32 
33 #include "vice.h"
34 
35 #ifdef UNIX_COMPILE
36 
37 #if defined(HAVE_CATWEASELMKIII) && defined(HAVE_CATWEASELMKIII_IO)
38 
39 #include <unistd.h>
40 
41 #include "catweaselmkiii.h"
42 #include "cw-unix.h"
43 #include "io-access.h"
44 #include "log.h"
45 #include "pci-unix-drv.h"
46 #include "types.h"
47 
48 #define CW_SID_DAT 0xd8
49 #define CW_SID_CMD 0xdc
50 
51 #define MAXSID 1
52 
53 static int sids_found = -1;
54 static int base = -1;
55 
56 /* input/output functions */
cw_outb(uint16_t port,uint8_t val)57 static void cw_outb(uint16_t port, uint8_t val)
58 {
59     io_access_store(port, val);
60 }
61 
cw_inb(uint16_t port)62 static uint8_t cw_inb(uint16_t port)
63 {
64     return io_access_read(port);
65 }
66 
cw_pci_read(uint16_t addr,int chipno)67 int cw_pci_read(uint16_t addr, int chipno)
68 {
69     unsigned char cmd;
70 
71     if (chipno < MAXSID && base != -1 && addr < 0x20) {
72         cmd = (addr & 0x1f) | 0x20;
73         if (catweaselmkiii_get_ntsc()) {
74             cmd |= 0x40;
75         }
76         cw_outb(base + CW_SID_CMD, cmd);
77         usleep(1);
78         return cw_inb(base + CW_SID_DAT);
79     }
80     return 0;
81 }
82 
cw_pci_store(uint16_t addr,uint8_t outval,int chipno)83 void cw_pci_store(uint16_t addr, uint8_t outval, int chipno)
84 {
85     unsigned char cmd;
86 
87     if (chipno < MAXSID && base != -1 && addr < 0x20) {
88         cmd = addr & 0x1f;
89         if (catweaselmkiii_get_ntsc()) {
90             cmd |= 0x40;
91         }
92         cw_outb(base + CW_SID_DAT, outval);
93         cw_outb(base + CW_SID_CMD, cmd);
94         usleep(1);
95     }
96 }
97 
detect_sid(void)98 static int detect_sid(void)
99 {
100     int i;
101 
102     for (i = 0x18; i >= 0; --i) {
103         cw_pci_store((uint16_t)i, 0, 0);
104     }
105 
106     cw_pci_store(0x12, 0xff, 0);
107 
108     for (i = 0; i < 100; ++i) {
109         if (cw_pci_read(0x1b, 0)) {
110             return 0;
111         }
112     }
113 
114     cw_pci_store(0x0e, 0xff, 0);
115     cw_pci_store(0x0f, 0xff, 0);
116     cw_pci_store(0x12, 0x20, 0);
117 
118     for (i = 0; i < 100; ++i) {
119         if (cw_pci_read(0x1b, 0)) {
120             return 1;
121         }
122     }
123     return 0;
124 }
125 
cw_pci_open(void)126 int cw_pci_open(void)
127 {
128     int i;
129     uint32_t b1 = 0;
130     uint32_t b2 = 0;
131 
132     if (!sids_found) {
133         return -1;
134     }
135 
136     if (sids_found > 0) {
137         return 0;
138     }
139 
140     sids_found = 0;
141 
142     log_message(LOG_DEFAULT, "Detecting PCI CatWeasel boards.");
143 
144     i = pci_get_base(0xe159, 0x0001, &b1, &b2);
145 
146     if (i < 0) {
147         log_message(LOG_DEFAULT, "No PCI CatWeasel found.");
148         return -1;
149     }
150 
151     base = b1 & 0xfffc;
152 
153     if (io_access_map(base, 256) < 0) {
154         log_message(LOG_DEFAULT, "Cannot get permission to access $%X.", base);
155         return -1;
156     }
157 
158     log_message(LOG_DEFAULT, "PCI CatWeasel board found at $%04X.", base);
159 
160     if (detect_sid()) {
161         sids_found++;
162     }
163 
164     if (!sids_found) {
165         log_message(LOG_DEFAULT, "No PCI CatWeasel found.");
166         io_access_unmap(base, 256);
167         return -1;
168     }
169 
170     log_message(LOG_DEFAULT, "PCI CatWeasel: opened, found %d SIDs.", sids_found);
171 
172     return 0;
173 }
174 
cw_pci_close(void)175 int cw_pci_close(void)
176 {
177     io_access_unmap(base, 256);
178 
179     base = -1;
180 
181     sids_found = -1;
182 
183     log_message(LOG_DEFAULT, "PCI CatWeasel: closed");
184 
185     return 0;
186 }
187 
cw_pci_available(void)188 int cw_pci_available(void)
189 {
190     return sids_found;
191 }
192 #endif
193 #endif
194