1 /*
2  *  Copyright (C) 2018-2020  Anders Gavare.  All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions are met:
6  *
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. The name of the author may not be used to endorse or promote products
13  *     derived from this software without specific prior written permission.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  *  SUCH DAMAGE.
26  *
27  *
28  *  COMMENT: VoCore Palmbus
29  *
30  *  TODO
31  */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include "cpu.h"
38 #include "console.h"
39 #include "device.h"
40 #include "emul.h"
41 #include "machine.h"
42 #include "memory.h"
43 #include "misc.h"
44 
45 
46 #define	DEV_PALMBUS_LENGTH		0x1000
47 
48 struct palmbus_data {
49 	// struct interrupt irq;
50 	int console_handle;
51 };
52 
53 
DEVICE_ACCESS(palmbus)54 DEVICE_ACCESS(palmbus)
55 {
56 	struct palmbus_data *d = (struct palmbus_data *) extra;
57 	uint64_t idata = 0, odata = 0;
58 
59 	if (writeflag == MEM_WRITE)
60 		idata = memory_readmax64(cpu, data, len);
61 
62 	switch (relative_addr) {
63 
64 	case 0x0000:
65 		odata = 0x30335452;
66 		break;
67 
68 	case 0x0004:
69 		odata = 0x20203235;
70 		break;
71 
72 	case 0x000c:
73 		/*  Linux boot message says "SoC Type: Ralink RT5350 id:1 rev:3"  */
74 		odata = 0x00000103;
75 		break;
76 
77 	case 0x0c04:
78 		console_putchar(d->console_handle, idata);
79 		break;
80 
81 	case 0x0c1c:
82 		odata = 0x20;	// Serial ready (?)
83 		break;
84 
85 	default:
86 		if (writeflag == MEM_WRITE) {
87 			fatal("[ palmbus: unimplemented write to address 0x%x"
88 			    ", data=0x%02x ]\n",
89 			    (int)relative_addr, (int)idata);
90 		} else {
91 			fatal("[ palmbus: unimplemented read from address 0x%x "
92 			    "]\n", (int)relative_addr);
93 		}
94 		/*  exit(1);  */
95 	}
96 
97 	if (writeflag == MEM_READ)
98 		memory_writemax64(cpu, data, len, odata);
99 
100 	return 1;
101 }
102 
103 
DEVINIT(palmbus)104 DEVINIT(palmbus)
105 {
106 	struct palmbus_data *d;
107 
108 	CHECK_ALLOCATION(d = (struct palmbus_data *) malloc(sizeof(struct palmbus_data)));
109 	memset(d, 0, sizeof(struct palmbus_data));
110 
111 	// INTERRUPT_CONNECT(devinit->interrupt_path, d->irq);
112 
113 	d->console_handle = console_start_slave(devinit->machine, "uartlite", 1);
114 
115 	memory_device_register(devinit->machine->memory, devinit->name,
116 	    devinit->addr, DEV_PALMBUS_LENGTH,
117 	    dev_palmbus_access, (void *)d, DM_DEFAULT, NULL);
118 
119 	return 1;
120 }
121 
122