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>
20 #include <asm/fixmap.h>
22 #include "usbgecko_udbg.h"
25 #define EXI_CLK_32MHZ 5
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 */
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)
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;
58 csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
59 out_be32(csr_reg, csr);
63 out_be32(data_reg, data);
64 cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
67 while (in_be32(cr_reg) & EXI_CR_TSTART)
74 data = in_be32(data_reg);
80 * Returns true if an usbgecko adapter is found.
82 static int ug_is_adapter_present(void)
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;
121 while (!ug_is_txfifo_ready() && count--)
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;
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;
159 while (!ug_is_rxfifo_ready() && count--)
161 return ug_raw_getc();
170 * Transmits a character.
172 void ug_udbg_putc(char ch)
178 * Receives a character. Waits until a character is available.
180 static int ug_udbg_getc(void)
184 while ((ch = ug_getc()) == -1)
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())
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;
206 const unsigned int *reg;
208 reg = of_get_property(np, "reg", NULL);
210 paddr = of_translate_address(np, reg);
212 exi_io_base = ioremap(paddr, reg[1]);
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)
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())
237 * USB Gecko udbg support initialization.
239 void __init ug_udbg_init(void)
241 struct device_node *np;
242 void __iomem *exi_io_base;
245 udbg_printf("%s: early -> final\n", __func__);
247 np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi");
249 udbg_printf("%s: EXI node not found\n", __func__);
253 exi_io_base = ug_udbg_setup_exi_io_base(np);
255 udbg_printf("%s: failed to setup EXI io base\n", __func__);
259 if (!ug_udbg_probe(exi_io_base)) {
260 udbg_printf("usbgecko_udbg: not found\n");
261 iounmap(exi_io_base);
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");
275 #ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
277 static phys_addr_t __init ug_early_grab_io_addr(void)
279 #if defined(CONFIG_GAMECUBE)
281 #elif defined(CONFIG_WII)
284 #error Invalid platform for USB Gecko based early debugging.
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))
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 */