1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c 4 * 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 8 */ 9 10 #include <linux/of_address.h> 11 12 #include <mm/mmu_decl.h> 13 14 #include <asm/io.h> 15 #include <asm/udbg.h> 16 #include <asm/fixmap.h> 17 18 #include "usbgecko_udbg.h" 19 20 21 #define EXI_CLK_32MHZ 5 22 23 #define EXI_CSR 0x00 24 #define EXI_CSR_CLKMASK (0x7<<4) 25 #define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4) 26 #define EXI_CSR_CSMASK (0x7<<7) 27 #define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */ 28 29 #define EXI_CR 0x0c 30 #define EXI_CR_TSTART (1<<0) 31 #define EXI_CR_WRITE (1<<2) 32 #define EXI_CR_READ_WRITE (2<<2) 33 #define EXI_CR_TLEN(len) (((len)-1)<<4) 34 35 #define EXI_DATA 0x10 36 37 #define UG_READ_ATTEMPTS 100 38 #define UG_WRITE_ATTEMPTS 100 39 40 41 static void __iomem *ug_io_base; 42 43 /* 44 * Performs one input/output transaction between the exi host and the usbgecko. 45 */ 46 static u32 ug_io_transaction(u32 in) 47 { 48 u32 __iomem *csr_reg = ug_io_base + EXI_CSR; 49 u32 __iomem *data_reg = ug_io_base + EXI_DATA; 50 u32 __iomem *cr_reg = ug_io_base + EXI_CR; 51 u32 csr, data, cr; 52 53 /* select */ 54 csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0; 55 out_be32(csr_reg, csr); 56 57 /* read/write */ 58 data = in; 59 out_be32(data_reg, data); 60 cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART; 61 out_be32(cr_reg, cr); 62 63 while (in_be32(cr_reg) & EXI_CR_TSTART) 64 barrier(); 65 66 /* deselect */ 67 out_be32(csr_reg, 0); 68 69 /* result */ 70 data = in_be32(data_reg); 71 72 return data; 73 } 74 75 /* 76 * Returns true if an usbgecko adapter is found. 77 */ 78 static int ug_is_adapter_present(void) 79 { 80 if (!ug_io_base) 81 return 0; 82 83 return ug_io_transaction(0x90000000) == 0x04700000; 84 } 85 86 /* 87 * Returns true if the TX fifo is ready for transmission. 88 */ 89 static int ug_is_txfifo_ready(void) 90 { 91 return ug_io_transaction(0xc0000000) & 0x04000000; 92 } 93 94 /* 95 * Tries to transmit a character. 96 * If the TX fifo is not ready the result is undefined. 97 */ 98 static void ug_raw_putc(char ch) 99 { 100 ug_io_transaction(0xb0000000 | (ch << 20)); 101 } 102 103 /* 104 * Transmits a character. 105 * It silently fails if the TX fifo is not ready after a number of retries. 106 */ 107 static void ug_putc(char ch) 108 { 109 int count = UG_WRITE_ATTEMPTS; 110 111 if (!ug_io_base) 112 return; 113 114 if (ch == '\n') 115 ug_putc('\r'); 116 117 while (!ug_is_txfifo_ready() && count--) 118 barrier(); 119 if (count >= 0) 120 ug_raw_putc(ch); 121 } 122 123 /* 124 * Returns true if the RX fifo is ready for transmission. 125 */ 126 static int ug_is_rxfifo_ready(void) 127 { 128 return ug_io_transaction(0xd0000000) & 0x04000000; 129 } 130 131 /* 132 * Tries to receive a character. 133 * If a character is unavailable the function returns -1. 134 */ 135 static int ug_raw_getc(void) 136 { 137 u32 data = ug_io_transaction(0xa0000000); 138 if (data & 0x08000000) 139 return (data >> 16) & 0xff; 140 else 141 return -1; 142 } 143 144 /* 145 * Receives a character. 146 * It fails if the RX fifo is not ready after a number of retries. 147 */ 148 static int ug_getc(void) 149 { 150 int count = UG_READ_ATTEMPTS; 151 152 if (!ug_io_base) 153 return -1; 154 155 while (!ug_is_rxfifo_ready() && count--) 156 barrier(); 157 return ug_raw_getc(); 158 } 159 160 /* 161 * udbg functions. 162 * 163 */ 164 165 /* 166 * Transmits a character. 167 */ 168 static void ug_udbg_putc(char ch) 169 { 170 ug_putc(ch); 171 } 172 173 /* 174 * Receives a character. Waits until a character is available. 175 */ 176 static int ug_udbg_getc(void) 177 { 178 int ch; 179 180 while ((ch = ug_getc()) == -1) 181 barrier(); 182 return ch; 183 } 184 185 /* 186 * Receives a character. If a character is not available, returns -1. 187 */ 188 static int ug_udbg_getc_poll(void) 189 { 190 if (!ug_is_rxfifo_ready()) 191 return -1; 192 return ug_getc(); 193 } 194 195 /* 196 * Checks if a USB Gecko adapter is inserted in any memory card slot. 197 */ 198 static void __iomem *__init ug_udbg_probe(void __iomem *exi_io_base) 199 { 200 int i; 201 202 /* look for a usbgecko on memcard slots A and B */ 203 for (i = 0; i < 2; i++) { 204 ug_io_base = exi_io_base + 0x14 * i; 205 if (ug_is_adapter_present()) 206 break; 207 } 208 if (i == 2) 209 ug_io_base = NULL; 210 return ug_io_base; 211 212 } 213 214 /* 215 * USB Gecko udbg support initialization. 216 */ 217 void __init ug_udbg_init(void) 218 { 219 struct device_node *np; 220 void __iomem *exi_io_base; 221 222 if (ug_io_base) 223 udbg_printf("%s: early -> final\n", __func__); 224 225 np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi"); 226 if (!np) { 227 udbg_printf("%s: EXI node not found\n", __func__); 228 goto out; 229 } 230 231 exi_io_base = of_iomap(np, 0); 232 if (!exi_io_base) { 233 udbg_printf("%s: failed to setup EXI io base\n", __func__); 234 goto done; 235 } 236 237 if (!ug_udbg_probe(exi_io_base)) { 238 udbg_printf("usbgecko_udbg: not found\n"); 239 iounmap(exi_io_base); 240 } else { 241 udbg_putc = ug_udbg_putc; 242 udbg_getc = ug_udbg_getc; 243 udbg_getc_poll = ug_udbg_getc_poll; 244 udbg_printf("usbgecko_udbg: ready\n"); 245 } 246 247 done: 248 of_node_put(np); 249 out: 250 return; 251 } 252 253 #ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO 254 255 static phys_addr_t __init ug_early_grab_io_addr(void) 256 { 257 #if defined(CONFIG_GAMECUBE) 258 return 0x0c000000; 259 #elif defined(CONFIG_WII) 260 return 0x0d000000; 261 #else 262 #error Invalid platform for USB Gecko based early debugging. 263 #endif 264 } 265 266 /* 267 * USB Gecko early debug support initialization for udbg. 268 */ 269 void __init udbg_init_usbgecko(void) 270 { 271 void __iomem *early_debug_area; 272 void __iomem *exi_io_base; 273 274 /* 275 * At this point we have a BAT already setup that enables I/O 276 * to the EXI hardware. 277 * 278 * The BAT uses a virtual address range reserved at the fixmap. 279 * This must match the virtual address configured in 280 * head_32.S:setup_usbgecko_bat(). 281 */ 282 early_debug_area = (void __iomem *)__fix_to_virt(FIX_EARLY_DEBUG_BASE); 283 exi_io_base = early_debug_area + 0x00006800; 284 285 /* try to detect a USB Gecko */ 286 if (!ug_udbg_probe(exi_io_base)) 287 return; 288 289 /* we found a USB Gecko, load udbg hooks */ 290 udbg_putc = ug_udbg_putc; 291 udbg_getc = ug_udbg_getc; 292 udbg_getc_poll = ug_udbg_getc_poll; 293 294 /* 295 * Prepare again the same BAT for MMU_init. 296 * This allows udbg I/O to continue working after the MMU is 297 * turned on for real. 298 * It is safe to continue using the same virtual address as it is 299 * a reserved fixmap area. 300 */ 301 setbat(1, (unsigned long)early_debug_area, 302 ug_early_grab_io_addr(), 128*1024, PAGE_KERNEL_NCG); 303 } 304 305 #endif /* CONFIG_PPC_EARLY_DEBUG_USBGECKO */ 306 307