]>
git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blob - arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
5 * udbg serial input/output routines for the USB Gecko adapter.
6 * Copyright (C) 2008-2009 The GameCube Linux Team
7 * Copyright (C) 2008,2009 Albert Herranz
10 #include <mm/mmu_decl.h>
15 #include <asm/fixmap.h>
17 #include "usbgecko_udbg.h"
20 #define EXI_CLK_32MHZ 5
23 #define EXI_CSR_CLKMASK (0x7<<4)
24 #define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4)
25 #define EXI_CSR_CSMASK (0x7<<7)
26 #define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */
29 #define EXI_CR_TSTART (1<<0)
30 #define EXI_CR_WRITE (1<<2)
31 #define EXI_CR_READ_WRITE (2<<2)
32 #define EXI_CR_TLEN(len) (((len)-1)<<4)
36 #define UG_READ_ATTEMPTS 100
37 #define UG_WRITE_ATTEMPTS 100
40 static void __iomem
*ug_io_base
;
43 * Performs one input/output transaction between the exi host and the usbgecko.
45 static u32
ug_io_transaction(u32 in
)
47 u32 __iomem
*csr_reg
= ug_io_base
+ EXI_CSR
;
48 u32 __iomem
*data_reg
= ug_io_base
+ EXI_DATA
;
49 u32 __iomem
*cr_reg
= ug_io_base
+ EXI_CR
;
53 csr
= EXI_CSR_CLK_32MHZ
| EXI_CSR_CS_0
;
54 out_be32(csr_reg
, csr
);
58 out_be32(data_reg
, data
);
59 cr
= EXI_CR_TLEN(2) | EXI_CR_READ_WRITE
| EXI_CR_TSTART
;
62 while (in_be32(cr_reg
) & EXI_CR_TSTART
)
69 data
= in_be32(data_reg
);
75 * Returns true if an usbgecko adapter is found.
77 static int ug_is_adapter_present(void)
82 return ug_io_transaction(0x90000000) == 0x04700000;
86 * Returns true if the TX fifo is ready for transmission.
88 static int ug_is_txfifo_ready(void)
90 return ug_io_transaction(0xc0000000) & 0x04000000;
94 * Tries to transmit a character.
95 * If the TX fifo is not ready the result is undefined.
97 static void ug_raw_putc(char ch
)
99 ug_io_transaction(0xb0000000 | (ch
<< 20));
103 * Transmits a character.
104 * It silently fails if the TX fifo is not ready after a number of retries.
106 static void ug_putc(char ch
)
108 int count
= UG_WRITE_ATTEMPTS
;
116 while (!ug_is_txfifo_ready() && count
--)
123 * Returns true if the RX fifo is ready for transmission.
125 static int ug_is_rxfifo_ready(void)
127 return ug_io_transaction(0xd0000000) & 0x04000000;
131 * Tries to receive a character.
132 * If a character is unavailable the function returns -1.
134 static int ug_raw_getc(void)
136 u32 data
= ug_io_transaction(0xa0000000);
137 if (data
& 0x08000000)
138 return (data
>> 16) & 0xff;
144 * Receives a character.
145 * It fails if the RX fifo is not ready after a number of retries.
147 static int ug_getc(void)
149 int count
= UG_READ_ATTEMPTS
;
154 while (!ug_is_rxfifo_ready() && count
--)
156 return ug_raw_getc();
165 * Transmits a character.
167 static void ug_udbg_putc(char ch
)
173 * Receives a character. Waits until a character is available.
175 static int ug_udbg_getc(void)
179 while ((ch
= ug_getc()) == -1)
185 * Receives a character. If a character is not available, returns -1.
187 static int ug_udbg_getc_poll(void)
189 if (!ug_is_rxfifo_ready())
195 * Retrieves and prepares the virtual address needed to access the hardware.
197 static void __iomem
*ug_udbg_setup_exi_io_base(struct device_node
*np
)
199 void __iomem
*exi_io_base
= NULL
;
201 const unsigned int *reg
;
203 reg
= of_get_property(np
, "reg", NULL
);
205 paddr
= of_translate_address(np
, reg
);
207 exi_io_base
= ioremap(paddr
, reg
[1]);
213 * Checks if a USB Gecko adapter is inserted in any memory card slot.
215 static void __iomem
*ug_udbg_probe(void __iomem
*exi_io_base
)
219 /* look for a usbgecko on memcard slots A and B */
220 for (i
= 0; i
< 2; i
++) {
221 ug_io_base
= exi_io_base
+ 0x14 * i
;
222 if (ug_is_adapter_present())
232 * USB Gecko udbg support initialization.
234 void __init
ug_udbg_init(void)
236 struct device_node
*np
;
237 void __iomem
*exi_io_base
;
240 udbg_printf("%s: early -> final\n", __func__
);
242 np
= of_find_compatible_node(NULL
, NULL
, "nintendo,flipper-exi");
244 udbg_printf("%s: EXI node not found\n", __func__
);
248 exi_io_base
= ug_udbg_setup_exi_io_base(np
);
250 udbg_printf("%s: failed to setup EXI io base\n", __func__
);
254 if (!ug_udbg_probe(exi_io_base
)) {
255 udbg_printf("usbgecko_udbg: not found\n");
256 iounmap(exi_io_base
);
258 udbg_putc
= ug_udbg_putc
;
259 udbg_getc
= ug_udbg_getc
;
260 udbg_getc_poll
= ug_udbg_getc_poll
;
261 udbg_printf("usbgecko_udbg: ready\n");
270 #ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
272 static phys_addr_t __init
ug_early_grab_io_addr(void)
274 #if defined(CONFIG_GAMECUBE)
276 #elif defined(CONFIG_WII)
279 #error Invalid platform for USB Gecko based early debugging.
284 * USB Gecko early debug support initialization for udbg.
286 void __init
udbg_init_usbgecko(void)
288 void __iomem
*early_debug_area
;
289 void __iomem
*exi_io_base
;
292 * At this point we have a BAT already setup that enables I/O
293 * to the EXI hardware.
295 * The BAT uses a virtual address range reserved at the fixmap.
296 * This must match the virtual address configured in
297 * head_32.S:setup_usbgecko_bat().
299 early_debug_area
= (void __iomem
*)__fix_to_virt(FIX_EARLY_DEBUG_BASE
);
300 exi_io_base
= early_debug_area
+ 0x00006800;
302 /* try to detect a USB Gecko */
303 if (!ug_udbg_probe(exi_io_base
))
306 /* we found a USB Gecko, load udbg hooks */
307 udbg_putc
= ug_udbg_putc
;
308 udbg_getc
= ug_udbg_getc
;
309 udbg_getc_poll
= ug_udbg_getc_poll
;
312 * Prepare again the same BAT for MMU_init.
313 * This allows udbg I/O to continue working after the MMU is
314 * turned on for real.
315 * It is safe to continue using the same virtual address as it is
316 * a reserved fixmap area.
318 setbat(1, (unsigned long)early_debug_area
,
319 ug_early_grab_io_addr(), 128*1024, PAGE_KERNEL_NCG
);
322 #endif /* CONFIG_PPC_EARLY_DEBUG_USBGECKO */