Linux 4.6-rc6
[linux/fpc-iii.git] / arch / powerpc / platforms / embedded6xx / usbgecko_udbg.c
blob7feb325b636b87d97c238572e3fcf1f840eb5a2f
1 /*
2 * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
4 * udbg serial input/output routines for the USB Gecko adapter.
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 <mm/mmu_decl.h>
17 #include <asm/io.h>
18 #include <asm/prom.h>
19 #include <asm/udbg.h>
20 #include <asm/fixmap.h>
22 #include "usbgecko_udbg.h"
25 #define EXI_CLK_32MHZ 5
27 #define EXI_CSR 0x00
28 #define EXI_CSR_CLKMASK (0x7<<4)
29 #define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4)
30 #define EXI_CSR_CSMASK (0x7<<7)
31 #define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */
33 #define EXI_CR 0x0c
34 #define EXI_CR_TSTART (1<<0)
35 #define EXI_CR_WRITE (1<<2)
36 #define EXI_CR_READ_WRITE (2<<2)
37 #define EXI_CR_TLEN(len) (((len)-1)<<4)
39 #define EXI_DATA 0x10
41 #define UG_READ_ATTEMPTS 100
42 #define UG_WRITE_ATTEMPTS 100
45 static void __iomem *ug_io_base;
48 * Performs one input/output transaction between the exi host and the usbgecko.
50 static u32 ug_io_transaction(u32 in)
52 u32 __iomem *csr_reg = ug_io_base + EXI_CSR;
53 u32 __iomem *data_reg = ug_io_base + EXI_DATA;
54 u32 __iomem *cr_reg = ug_io_base + EXI_CR;
55 u32 csr, data, cr;
57 /* select */
58 csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
59 out_be32(csr_reg, csr);
61 /* read/write */
62 data = in;
63 out_be32(data_reg, data);
64 cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
65 out_be32(cr_reg, cr);
67 while (in_be32(cr_reg) & EXI_CR_TSTART)
68 barrier();
70 /* deselect */
71 out_be32(csr_reg, 0);
73 /* result */
74 data = in_be32(data_reg);
76 return data;
80 * Returns true if an usbgecko adapter is found.
82 static int ug_is_adapter_present(void)
84 if (!ug_io_base)
85 return 0;
87 return ug_io_transaction(0x90000000) == 0x04700000;
91 * Returns true if the TX fifo is ready for transmission.
93 static int ug_is_txfifo_ready(void)
95 return ug_io_transaction(0xc0000000) & 0x04000000;
99 * Tries to transmit a character.
100 * If the TX fifo is not ready the result is undefined.
102 static void ug_raw_putc(char ch)
104 ug_io_transaction(0xb0000000 | (ch << 20));
108 * Transmits a character.
109 * It silently fails if the TX fifo is not ready after a number of retries.
111 static void ug_putc(char ch)
113 int count = UG_WRITE_ATTEMPTS;
115 if (!ug_io_base)
116 return;
118 if (ch == '\n')
119 ug_putc('\r');
121 while (!ug_is_txfifo_ready() && count--)
122 barrier();
123 if (count >= 0)
124 ug_raw_putc(ch);
128 * Returns true if the RX fifo is ready for transmission.
130 static int ug_is_rxfifo_ready(void)
132 return ug_io_transaction(0xd0000000) & 0x04000000;
136 * Tries to receive a character.
137 * If a character is unavailable the function returns -1.
139 static int ug_raw_getc(void)
141 u32 data = ug_io_transaction(0xa0000000);
142 if (data & 0x08000000)
143 return (data >> 16) & 0xff;
144 else
145 return -1;
149 * Receives a character.
150 * It fails if the RX fifo is not ready after a number of retries.
152 static int ug_getc(void)
154 int count = UG_READ_ATTEMPTS;
156 if (!ug_io_base)
157 return -1;
159 while (!ug_is_rxfifo_ready() && count--)
160 barrier();
161 return ug_raw_getc();
165 * udbg functions.
170 * Transmits a character.
172 void ug_udbg_putc(char ch)
174 ug_putc(ch);
178 * Receives a character. Waits until a character is available.
180 static int ug_udbg_getc(void)
182 int ch;
184 while ((ch = ug_getc()) == -1)
185 barrier();
186 return ch;
190 * Receives a character. If a character is not available, returns -1.
192 static int ug_udbg_getc_poll(void)
194 if (!ug_is_rxfifo_ready())
195 return -1;
196 return ug_getc();
200 * Retrieves and prepares the virtual address needed to access the hardware.
202 static void __iomem *ug_udbg_setup_exi_io_base(struct device_node *np)
204 void __iomem *exi_io_base = NULL;
205 phys_addr_t paddr;
206 const unsigned int *reg;
208 reg = of_get_property(np, "reg", NULL);
209 if (reg) {
210 paddr = of_translate_address(np, reg);
211 if (paddr)
212 exi_io_base = ioremap(paddr, reg[1]);
214 return exi_io_base;
218 * Checks if a USB Gecko adapter is inserted in any memory card slot.
220 static void __iomem *ug_udbg_probe(void __iomem *exi_io_base)
222 int i;
224 /* look for a usbgecko on memcard slots A and B */
225 for (i = 0; i < 2; i++) {
226 ug_io_base = exi_io_base + 0x14 * i;
227 if (ug_is_adapter_present())
228 break;
230 if (i == 2)
231 ug_io_base = NULL;
232 return ug_io_base;
237 * USB Gecko udbg support initialization.
239 void __init ug_udbg_init(void)
241 struct device_node *np;
242 void __iomem *exi_io_base;
244 if (ug_io_base)
245 udbg_printf("%s: early -> final\n", __func__);
247 np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi");
248 if (!np) {
249 udbg_printf("%s: EXI node not found\n", __func__);
250 goto out;
253 exi_io_base = ug_udbg_setup_exi_io_base(np);
254 if (!exi_io_base) {
255 udbg_printf("%s: failed to setup EXI io base\n", __func__);
256 goto done;
259 if (!ug_udbg_probe(exi_io_base)) {
260 udbg_printf("usbgecko_udbg: not found\n");
261 iounmap(exi_io_base);
262 } else {
263 udbg_putc = ug_udbg_putc;
264 udbg_getc = ug_udbg_getc;
265 udbg_getc_poll = ug_udbg_getc_poll;
266 udbg_printf("usbgecko_udbg: ready\n");
269 done:
270 of_node_put(np);
271 out:
272 return;
275 #ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
277 static phys_addr_t __init ug_early_grab_io_addr(void)
279 #if defined(CONFIG_GAMECUBE)
280 return 0x0c000000;
281 #elif defined(CONFIG_WII)
282 return 0x0d000000;
283 #else
284 #error Invalid platform for USB Gecko based early debugging.
285 #endif
289 * USB Gecko early debug support initialization for udbg.
291 void __init udbg_init_usbgecko(void)
293 void __iomem *early_debug_area;
294 void __iomem *exi_io_base;
297 * At this point we have a BAT already setup that enables I/O
298 * to the EXI hardware.
300 * The BAT uses a virtual address range reserved at the fixmap.
301 * This must match the virtual address configured in
302 * head_32.S:setup_usbgecko_bat().
304 early_debug_area = (void __iomem *)__fix_to_virt(FIX_EARLY_DEBUG_BASE);
305 exi_io_base = early_debug_area + 0x00006800;
307 /* try to detect a USB Gecko */
308 if (!ug_udbg_probe(exi_io_base))
309 return;
311 /* we found a USB Gecko, load udbg hooks */
312 udbg_putc = ug_udbg_putc;
313 udbg_getc = ug_udbg_getc;
314 udbg_getc_poll = ug_udbg_getc_poll;
317 * Prepare again the same BAT for MMU_init.
318 * This allows udbg I/O to continue working after the MMU is
319 * turned on for real.
320 * It is safe to continue using the same virtual address as it is
321 * a reserved fixmap area.
323 setbat(1, (unsigned long)early_debug_area,
324 ug_early_grab_io_addr(), 128*1024, PAGE_KERNEL_NCG);
327 #endif /* CONFIG_PPC_EARLY_DEBUG_USBGECKO */