KVM: PPC: Book3S HV: Fix endianness of instruction obtained from HEIR register
[linux-2.6/btrfs-unstable.git] / arch / powerpc / boot / ugecon.c
blob8f2a6b31153439c10cdb2c7fe212beeb689c7103
1 /*
2 * arch/powerpc/boot/ugecon.c
4 * USB Gecko bootwrapper console.
5 * Copyright (C) 2008-2009 The GameCube Linux Team
6 * Copyright (C) 2008,2009 Albert Herranz
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
15 #include <stddef.h>
16 #include "stdio.h"
17 #include "types.h"
18 #include "io.h"
19 #include "ops.h"
22 #define EXI_CLK_32MHZ 5
24 #define EXI_CSR 0x00
25 #define EXI_CSR_CLKMASK (0x7<<4)
26 #define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4)
27 #define EXI_CSR_CSMASK (0x7<<7)
28 #define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */
30 #define EXI_CR 0x0c
31 #define EXI_CR_TSTART (1<<0)
32 #define EXI_CR_WRITE (1<<2)
33 #define EXI_CR_READ_WRITE (2<<2)
34 #define EXI_CR_TLEN(len) (((len)-1)<<4)
36 #define EXI_DATA 0x10
39 /* virtual address base for input/output, retrieved from device tree */
40 static void *ug_io_base;
43 static u32 ug_io_transaction(u32 in)
45 u32 *csr_reg = ug_io_base + EXI_CSR;
46 u32 *data_reg = ug_io_base + EXI_DATA;
47 u32 *cr_reg = ug_io_base + EXI_CR;
48 u32 csr, data, cr;
50 /* select */
51 csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
52 out_be32(csr_reg, csr);
54 /* read/write */
55 data = in;
56 out_be32(data_reg, data);
57 cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
58 out_be32(cr_reg, cr);
60 while (in_be32(cr_reg) & EXI_CR_TSTART)
61 barrier();
63 /* deselect */
64 out_be32(csr_reg, 0);
66 data = in_be32(data_reg);
67 return data;
70 static int ug_is_txfifo_ready(void)
72 return ug_io_transaction(0xc0000000) & 0x04000000;
75 static void ug_raw_putc(char ch)
77 ug_io_transaction(0xb0000000 | (ch << 20));
80 static void ug_putc(char ch)
82 int count = 16;
84 if (!ug_io_base)
85 return;
87 while (!ug_is_txfifo_ready() && count--)
88 barrier();
89 if (count >= 0)
90 ug_raw_putc(ch);
93 void ug_console_write(const char *buf, int len)
95 char *b = (char *)buf;
97 while (len--) {
98 if (*b == '\n')
99 ug_putc('\r');
100 ug_putc(*b++);
104 static int ug_is_adapter_present(void)
106 if (!ug_io_base)
107 return 0;
108 return ug_io_transaction(0x90000000) == 0x04700000;
111 static void *ug_grab_exi_io_base(void)
113 u32 v;
114 void *devp;
116 devp = find_node_by_compatible(NULL, "nintendo,flipper-exi");
117 if (devp == NULL)
118 goto err_out;
119 if (getprop(devp, "virtual-reg", &v, sizeof(v)) != sizeof(v))
120 goto err_out;
122 return (void *)v;
124 err_out:
125 return NULL;
128 void *ug_probe(void)
130 void *exi_io_base;
131 int i;
133 exi_io_base = ug_grab_exi_io_base();
134 if (!exi_io_base)
135 return NULL;
137 /* look for a usbgecko on memcard slots A and B */
138 for (i = 0; i < 2; i++) {
139 ug_io_base = exi_io_base + 0x14 * i;
140 if (ug_is_adapter_present())
141 break;
143 if (i == 2)
144 ug_io_base = NULL;
145 return ug_io_base;