2 #include "libopenbios/bindings.h"
3 #include "drivers/drivers.h"
4 #include "libc/byteorder.h"
5 #include "libc/vsprintf.h"
12 #define CUDA_DPRINTF(fmt, args...) \
13 do { printk("CUDA - %s: " fmt, __func__ , ##args); } while (0)
15 #define CUDA_DPRINTF(fmt, args...) do { } while (0)
18 #define IO_CUDA_OFFSET 0x00016000
19 #define IO_CUDA_SIZE 0x00002000
21 /* VIA registers - spaced 0x200 bytes apart */
22 #define RS 0x200 /* skip between registers */
23 #define B 0 /* B-side data */
24 #define A RS /* A-side data */
25 #define DIRB (2*RS) /* B-side direction (1=output) */
26 #define DIRA (3*RS) /* A-side direction (1=output) */
27 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
28 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
29 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
30 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
31 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
32 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
33 #define SR (10*RS) /* Shift register */
34 #define ACR (11*RS) /* Auxiliary control register */
35 #define PCR (12*RS) /* Peripheral control register */
36 #define IFR (13*RS) /* Interrupt flag register */
37 #define IER (14*RS) /* Interrupt enable register */
38 #define ANH (15*RS) /* A-side data, no handshake */
40 /* Bits in B data register: all active low */
41 #define TREQ 0x08 /* Transfer request (input) */
42 #define TACK 0x10 /* Transfer acknowledge (output) */
43 #define TIP 0x20 /* Transfer in progress (output) */
46 #define SR_CTRL 0x1c /* Shift register control bits */
47 #define SR_EXT 0x0c /* Shift on external clock */
48 #define SR_OUT 0x10 /* Shift out if 1 */
50 /* Bits in IFR and IER */
51 #define IER_SET 0x80 /* set bits in IER */
52 #define IER_CLR 0 /* clear bits in IER */
53 #define SR_INT 0x04 /* Shift register full/empty */
55 #define CUDA_BUF_SIZE 16
60 /* CUDA commands (2nd byte) */
61 #define CUDA_GET_TIME 0x03
62 #define CUDA_SET_TIME 0x09
63 #define CUDA_POWERDOWN 0x0a
64 #define CUDA_RESET_SYSTEM 0x11
66 static uint8_t cuda_readb (cuda_t *dev, int reg)
68 return *(volatile uint8_t *)(dev->base + reg);
71 static void cuda_writeb (cuda_t *dev, int reg, uint8_t val)
73 *(volatile uint8_t *)(dev->base + reg) = val;
76 static void cuda_wait_irq (cuda_t *dev)
80 // CUDA_DPRINTF("\n");
82 val = cuda_readb(dev, IFR);
83 cuda_writeb(dev, IFR, val & 0x7f);
91 static int cuda_request (cuda_t *dev, uint8_t pkt_type, const uint8_t *buf,
92 int buf_len, uint8_t *obuf)
96 cuda_writeb(dev, ACR, cuda_readb(dev, ACR) | SR_OUT);
97 cuda_writeb(dev, SR, pkt_type);
98 cuda_writeb(dev, B, cuda_readb(dev, B) & ~TIP);
100 //CUDA_DPRINTF("Send buf len: %d\n", buf_len);
102 for(i = 0; i < buf_len; i++) {
104 cuda_writeb(dev, SR, buf[i]);
105 cuda_writeb(dev, B, cuda_readb(dev, B) ^ TACK);
109 cuda_writeb(dev, ACR, cuda_readb(dev, ACR) & ~SR_OUT);
111 cuda_writeb(dev, B, cuda_readb(dev, B) | TIP | TACK);
117 cuda_writeb(dev, B, cuda_readb(dev, B) & ~TIP);
120 val = cuda_readb(dev, SR);
121 if (obuf_len < CUDA_BUF_SIZE)
122 obuf[obuf_len++] = val;
123 if (cuda_readb(dev, B) & TREQ)
125 cuda_writeb(dev, B, cuda_readb(dev, B) ^ TACK);
127 cuda_writeb(dev, B, cuda_readb(dev, B) | TIP | TACK);
132 // CUDA_DPRINTF("Got len: %d\n", obuf_len);
139 static int cuda_adb_req (void *host, const uint8_t *snd_buf, int len,
142 uint8_t buffer[CUDA_BUF_SIZE], *pos;
144 // CUDA_DPRINTF("len: %d %02x\n", len, snd_buf[0]);
145 len = cuda_request(host, ADB_PACKET, snd_buf, len, buffer);
146 if (len > 1 && buffer[0] == ADB_PACKET) {
147 /* We handle 2 types of ADB packet here:
148 Normal: <type> <status> <data> ...
149 Error : <type> <status> <cmd> (<data> ...)
150 Ideally we should use buffer[1] (status) to determine whether this
151 is a normal or error packet but this requires a corresponding fix
152 in QEMU <= 2.4. Hence we temporarily handle it this way to ease
154 if (len > 2 && buffer[2] == snd_buf[0]) {
167 memcpy(rcv_buf, pos, len);
173 DECLARE_UNNAMED_NODE(ob_cuda, INSTALL_OPEN, sizeof(int));
175 static cuda_t *main_cuda;
178 ppc32_reset_all(void)
180 uint8_t cmdbuf[2], obuf[64];
182 cmdbuf[0] = CUDA_RESET_SYSTEM;
183 cuda_request(main_cuda, CUDA_PACKET, cmdbuf, sizeof(cmdbuf), obuf);
189 uint8_t cmdbuf[2], obuf[64];
191 cmdbuf[0] = CUDA_POWERDOWN;
192 cuda_request(main_cuda, CUDA_PACKET, cmdbuf, sizeof(cmdbuf), obuf);
196 ob_cuda_initialize (int *idx)
198 phandle_t ph=get_cur_dev();
201 push_str("via-cuda");
202 fword("device-type");
204 set_int_property(ph, "#address-cells", 1);
205 set_int_property(ph, "#size-cells", 0);
207 set_property(ph, "compatible", "cuda", 5);
209 props[0] = __cpu_to_be32(IO_CUDA_OFFSET);
210 props[1] = __cpu_to_be32(IO_CUDA_SIZE);
212 set_property(ph, "reg", (char *)&props, sizeof(props));
214 /* on newworld machines the cuda is on interrupt 0x19 */
218 NEWWORLD(set_property(ph, "interrupts", (char *)props, sizeof(props)));
219 NEWWORLD(set_int_property(ph, "#interrupt-cells", 2));
221 /* we emulate an oldworld hardware, so we must use
222 * non-standard oldworld property (needed by linux 2.6.18)
225 OLDWORLD(set_int_property(ph, "AAPL,interrupts", 0x12));
227 bind_func("ppc32-reset-all", ppc32_reset_all);
228 push_str("' ppc32-reset-all to reset-all");
233 ob_cuda_open(int *idx)
239 ob_cuda_close(int *idx)
243 NODE_METHODS(ob_cuda) = {
244 { NULL, ob_cuda_initialize },
245 { "open", ob_cuda_open },
246 { "close", ob_cuda_close },
249 DECLARE_UNNAMED_NODE(rtc, INSTALL_OPEN, sizeof(int));
258 * get-time ( -- second minute hour day month year )
262 static const int days_month[12] =
263 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
264 static const int days_month_leap[12] =
265 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
267 static inline int is_leap(int year)
269 return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
273 rtc_get_time(int *idx)
275 uint8_t cmdbuf[2], obuf[64];
276 ucell second, minute, hour, day, month, year;
281 cmdbuf[0] = CUDA_GET_TIME;
282 cuda_request(main_cuda, CUDA_PACKET, cmdbuf, sizeof(cmdbuf), obuf);
284 /* seconds since 01/01/1904 */
286 now = (obuf[3] << 24) + (obuf[4] << 16) + (obuf[5] << 8) + obuf[6];
297 year = now * 100 / 36525;
298 now -= year * 36525 / 100;
301 days = is_leap(year) ? days_month_leap : days_month;
306 if (now <= current + days[month]) {
309 current += days[month];
325 * set-time ( second minute hour day month year -- )
330 rtc_set_time(int *idx)
332 uint8_t cmdbuf[5], obuf[3];
333 ucell second, minute, hour, day, month, year;
336 unsigned int nb_days;
346 days = is_leap(year) ? days_month_leap : days_month;
347 nb_days = (year - 1904) * 36525 / 100 + day;
348 for (i = 0; i < month - 1; i++)
351 now = (((nb_days * 24) + hour) * 60 + minute) * 60 + second;
353 cmdbuf[0] = CUDA_SET_TIME;
354 cmdbuf[1] = now >> 24;
355 cmdbuf[2] = now >> 16;
356 cmdbuf[3] = now >> 8;
359 cuda_request(main_cuda, CUDA_PACKET, cmdbuf, sizeof(cmdbuf), obuf);
362 NODE_METHODS(rtc) = {
363 { "open", rtc_open },
364 { "get-time", rtc_get_time },
365 { "set-time", rtc_set_time },
371 phandle_t ph, aliases;
374 snprintf(buf, sizeof(buf), "%s/rtc", path);
375 REGISTER_NAMED_NODE(rtc, buf);
378 set_property(ph, "device_type", "rtc", 4);
379 set_property(ph, "compatible", "rtc", 4);
381 aliases = find_dev("/aliases");
382 set_property(aliases, "rtc", buf, strlen(buf) + 1);
387 powermgt_init(char *path)
392 snprintf(buf, sizeof(buf), "%s/power-mgt", path);
393 REGISTER_NAMED_NODE(rtc, buf);
396 set_property(ph, "device_type", "power-mgt", 10);
397 set_property(ph, "mgt-kind", "min-consumption-pwm-led", strlen("min-consumption-pwm-led") + 1);
398 set_property(ph, "compatible", "cuda", strlen("cuda") + 1);
401 cuda_t *cuda_init (const char *path, phys_addr_t base)
407 base += IO_CUDA_OFFSET;
408 CUDA_DPRINTF(" base=" FMT_plx "\n", base);
409 cuda = malloc(sizeof(cuda_t));
413 snprintf(buf, sizeof(buf), "%s/via-cuda", path);
414 REGISTER_NAMED_NODE(ob_cuda, buf);
416 aliases = find_dev("/aliases");
417 set_property(aliases, "via-cuda", buf, strlen(buf) + 1);
420 cuda_writeb(cuda, B, cuda_readb(cuda, B) | TREQ | TIP);
421 #ifdef CONFIG_DRIVER_ADB
422 cuda->adb_bus = adb_bus_new(cuda, &cuda_adb_req);
423 if (cuda->adb_bus == NULL) {
427 adb_bus_init(buf, cuda->adb_bus);
436 bind_func("poweroff", ppc32_poweroff);