xref: /openbmc/linux/arch/powerpc/boot/ugecon.c (revision cfbb9be8)
1 /*
2  * arch/powerpc/boot/ugecon.c
3  *
4  * USB Gecko bootwrapper console.
5  * Copyright (C) 2008-2009 The GameCube Linux Team
6  * Copyright (C) 2008,2009 Albert Herranz
7  *
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.
12  *
13  */
14 
15 #include <stddef.h>
16 #include "stdio.h"
17 #include "types.h"
18 #include "io.h"
19 #include "ops.h"
20 
21 
22 #define EXI_CLK_32MHZ           5
23 
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 */
29 
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)
35 
36 #define EXI_DATA                0x10
37 
38 
39 /* virtual address base for input/output, retrieved from device tree */
40 static void *ug_io_base;
41 
42 
43 static u32 ug_io_transaction(u32 in)
44 {
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;
49 
50 	/* select */
51 	csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
52 	out_be32(csr_reg, csr);
53 
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);
59 
60 	while (in_be32(cr_reg) & EXI_CR_TSTART)
61 		barrier();
62 
63 	/* deselect */
64 	out_be32(csr_reg, 0);
65 
66 	data = in_be32(data_reg);
67 	return data;
68 }
69 
70 static int ug_is_txfifo_ready(void)
71 {
72 	return ug_io_transaction(0xc0000000) & 0x04000000;
73 }
74 
75 static void ug_raw_putc(char ch)
76 {
77 	ug_io_transaction(0xb0000000 | (ch << 20));
78 }
79 
80 static void ug_putc(char ch)
81 {
82 	int count = 16;
83 
84 	if (!ug_io_base)
85 		return;
86 
87 	while (!ug_is_txfifo_ready() && count--)
88 		barrier();
89 	if (count >= 0)
90 		ug_raw_putc(ch);
91 }
92 
93 void ug_console_write(const char *buf, int len)
94 {
95 	char *b = (char *)buf;
96 
97 	while (len--) {
98 		if (*b == '\n')
99 			ug_putc('\r');
100 		ug_putc(*b++);
101 	}
102 }
103 
104 static int ug_is_adapter_present(void)
105 {
106 	if (!ug_io_base)
107 		return 0;
108 	return ug_io_transaction(0x90000000) == 0x04700000;
109 }
110 
111 static void *ug_grab_exi_io_base(void)
112 {
113 	u32 v;
114 	void *devp;
115 
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;
121 
122 	return (void *)v;
123 
124 err_out:
125 	return NULL;
126 }
127 
128 void *ug_probe(void)
129 {
130 	void *exi_io_base;
131 	int i;
132 
133 	exi_io_base = ug_grab_exi_io_base();
134 	if (!exi_io_base)
135 		return NULL;
136 
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;
142 	}
143 	if (i == 2)
144 		ug_io_base = NULL;
145 	return ug_io_base;
146 }
147 
148