11 #include "../platforms/amiga/Gayle.h"
12 #include "../platforms/amiga/gayle-ide/ide.h"
16 volatile unsigned int *gpio;
17 volatile unsigned int *gpclk;
18 volatile unsigned int gpfsel0;
19 volatile unsigned int gpfsel1;
20 volatile unsigned int gpfsel2;
21 volatile unsigned int gpfsel0_o;
22 volatile unsigned int gpfsel1_o;
23 volatile unsigned int gpfsel2_o;
25 volatile uint16_t srdata;
26 volatile uint32_t srdata2;
27 volatile uint32_t srdata2_old;
29 extern int mem_fd, mouse_fd, keyboard_fd;
30 extern int mem_fd_gpclk;
39 inline void write16(uint32_t address, uint32_t data) {
40 // asm volatile ("dmb" ::: "memory");
43 *(gpio + 1) = gpfsel1_o;
44 *(gpio + 2) = gpfsel2_o;
46 *(gpio + 7) = ((address & 0x0000ffff) << 8);
47 *(gpio + 10) = ((~address & 0x0000ffff) << 8);
51 *(gpio + 7) = ((address >> 16) << 8);
52 *(gpio + 10) = ((~address >> 16) << 8);
57 *(gpio + 7) = ((data & 0x0000ffff) << 8);
58 *(gpio + 10) = ((~data & 0x0000ffff) << 8);
63 *(gpio + 1) = gpfsel1;
64 *(gpio + 2) = gpfsel2;
67 // asm volatile ("dmb" ::: "memory");
70 inline void write8(uint32_t address, uint32_t data) {
71 if ((address & 1) == 0)
72 data = data + (data << 8); // EVEN, A0=0,UDS
74 data = data & 0xff; // ODD , A0=1,LDS
76 // asm volatile ("dmb" ::: "memory");
79 *(gpio + 1) = gpfsel1_o;
80 *(gpio + 2) = gpfsel2_o;
82 *(gpio + 7) = ((address & 0x0000ffff) << 8);
83 *(gpio + 10) = ((~address & 0x0000ffff) << 8);
87 *(gpio + 7) = ((address >> 16) << 8);
88 *(gpio + 10) = ((~address >> 16) << 8);
93 *(gpio + 7) = ((data & 0x0000ffff) << 8);
94 *(gpio + 10) = ((~data & 0x0000ffff) << 8);
99 *(gpio + 1) = gpfsel1;
100 *(gpio + 2) = gpfsel2;
101 while ((GET_GPIO(0)))
103 // asm volatile ("dmb" ::: "memory");
106 inline uint32_t read32(uint32_t address) {
110 asm volatile ("dmb" ::: "memory");
113 *(gpio + 1) = gpfsel1_o;
114 *(gpio + 2) = gpfsel2_o;
116 *(gpio + 7) = ((address & 0x0000ffff) << 8);
117 *(gpio + 10) = ((~address & 0x0000ffff) << 8);
122 *(gpio + 7) = ((address >> 16) << 8);
123 *(gpio + 10) = ((~address >> 16) << 8);
129 *(gpio + 1) = gpfsel1;
130 *(gpio + 2) = gpfsel2;
132 while (!(GET_GPIO(0)))
138 // asm volatile ("dmb" ::: "memory");
139 a = (val >> 8) & 0xffff;
143 *(gpio + 1) = gpfsel1_o;
144 *(gpio + 2) = gpfsel2_o;
146 *(gpio + 7) = (((address+2) & 0x0000ffff) << 8);
147 *(gpio + 10) = ((~(address+2) & 0x0000ffff) << 8);
152 *(gpio + 7) = (((address+2) >> 16) << 8);
153 *(gpio + 10) = ((~(address+2) >> 16) << 8);
159 *(gpio + 1) = gpfsel1;
160 *(gpio + 2) = gpfsel2;
162 while (!(GET_GPIO(0)))
168 b = (val >> 8) & 0xffff;
169 asm volatile ("dmb" ::: "memory");
171 return (a << 16) | b;
174 inline uint32_t read16(uint32_t address) {
176 // asm volatile ("dmb" ::: "memory");
179 *(gpio + 1) = gpfsel1_o;
180 *(gpio + 2) = gpfsel2_o;
182 *(gpio + 7) = ((address & 0x0000ffff) << 8);
183 *(gpio + 10) = ((~address & 0x0000ffff) << 8);
188 *(gpio + 7) = ((address >> 16) << 8);
189 *(gpio + 10) = ((~address >> 16) << 8);
195 *(gpio + 1) = gpfsel1;
196 *(gpio + 2) = gpfsel2;
198 while (!(GET_GPIO(0)))
203 // asm volatile ("dmb" ::: "memory");
204 return (val >> 8) & 0xffff;
207 inline uint32_t read8(uint32_t address) {
209 // asm volatile ("dmb" ::: "memory");
212 *(gpio + 1) = gpfsel1_o;
213 *(gpio + 2) = gpfsel2_o;
215 *(gpio + 7) = ((address & 0x0000ffff) << 8);
216 *(gpio + 10) = ((~address & 0x0000ffff) << 8);
220 *(gpio + 7) = ((address >> 16) << 8);
221 *(gpio + 10) = ((~address >> 16) << 8);
227 *(gpio + 1) = gpfsel1;
228 *(gpio + 2) = gpfsel2;
231 while (!(GET_GPIO(0)))
236 // asm volatile ("dmb" ::: "memory");
238 val = (val >> 8) & 0xffff;
239 if ((address & 1) == 0)
240 return (val >> 8) & 0xff; // EVEN, A0=0,UDS
242 return val & 0xff; // ODD , A0=1,LDS
245 /******************************************************/
247 void write_reg(unsigned int value) {
250 *(gpio + 1) = gpfsel1_o;
251 *(gpio + 2) = gpfsel2_o;
252 *(gpio + 7) = (value & 0xffff) << 8;
253 *(gpio + 10) = (~value & 0xffff) << 8;
255 GPIO_CLR = 1 << 7; // delay
260 *(gpio + 1) = gpfsel1;
261 *(gpio + 2) = gpfsel2;
264 uint16_t read_reg(void) {
269 *(gpio + 1) = gpfsel1;
270 *(gpio + 2) = gpfsel2;
272 GPIO_CLR = 1 << 6; // delay
277 return (uint16_t)(val >> 8);
281 // Set up a memory regions to access GPIO
285 if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
286 printf("can't open /dev/mem \n");
292 NULL, // Any adddress in our space will do
293 BCM2708_PERI_SIZE, // Map length
294 PROT_READ | PROT_WRITE, // Enable reading & writting to mapped memory
295 MAP_SHARED, // Shared with other processes
296 mem_fd, // File to map
297 BCM2708_PERI_BASE // Offset to GPIO peripheral
300 close(mem_fd); // No need to keep mem_fd open after mmap
302 if (gpio_map == MAP_FAILED) {
303 printf("gpio mmap error %d\n", (int)gpio_map); // errno also set!
307 gpio = ((volatile unsigned *)gpio_map) + GPIO_ADDR / 4;
308 gpclk = ((volatile unsigned *)gpio_map) + GPCLK_ADDR / 4;
312 void gpio_enable_200mhz() {
313 *(gpclk + (CLK_GP0_CTL / 4)) = CLK_PASSWD | (1 << 5);
315 while ((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7))
318 *(gpclk + (CLK_GP0_DIV / 4)) =
319 CLK_PASSWD | (6 << 12); // divider , 6=200MHz on pi3
321 *(gpclk + (CLK_GP0_CTL / 4)) =
322 CLK_PASSWD | 5 | (1 << 4); // pll? 6=plld, 5=pllc
324 while (((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7)) == 0)
328 SET_GPIO_ALT(4, 0); // gpclk0
338 // set gpio0 (aux0) and gpio1 (aux1) to input
342 // Set GPIO pins 6,7 and 8-23 to output
343 for (g = 6; g <= 23; g++) {
347 printf("Precalculate GPIO8-23 as Output\n");
348 gpfsel0_o = *(gpio); // store gpio ddr
349 printf("gpfsel0: %#x\n", gpfsel0_o);
350 gpfsel1_o = *(gpio + 1); // store gpio ddr
351 printf("gpfsel1: %#x\n", gpfsel1_o);
352 gpfsel2_o = *(gpio + 2); // store gpio ddr
353 printf("gpfsel2: %#x\n", gpfsel2_o);
355 // Set GPIO pins 8-23 to input
356 for (g = 8; g <= 23; g++) {
359 printf("Precalculate GPIO8-23 as Input\n");
360 gpfsel0 = *(gpio); // store gpio ddr
361 printf("gpfsel0: %#x\n", gpfsel0);
362 gpfsel1 = *(gpio + 1); // store gpio ddr
363 printf("gpfsel1: %#x\n", gpfsel1);
364 gpfsel2 = *(gpio + 2); // store gpio ddr
365 printf("gpfsel2: %#x\n", gpfsel2);
375 inline void gpio_handle_irq() {
376 if (GET_GPIO(1) == 0) {
378 m68k_set_irq((srdata >> 13) & 0xff);
380 if ((gayle_int & 0x80) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
381 write16(0xdff09c, 0x8008);
389 inline int gpio_get_irq() {
390 return (GET_GPIO(1));
395 void *iplThread(void *args) {
396 printf("IPL thread running/n");
400 if (GET_GPIO(1) == 0) {
402 m68k_end_timeslice();
403 //printf("thread!/n");