1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * arch/powerpc/boot/ugecon.c 4 * 5 * USB Gecko bootwrapper console. 6 * Copyright (C) 2008-2009 The GameCube Linux Team 7 * Copyright (C) 2008,2009 Albert Herranz 8 */ 9 10 #include <stddef.h> 11 #include "stdio.h" 12 #include "types.h" 13 #include "io.h" 14 #include "ops.h" 15 16 17 #define EXI_CLK_32MHZ 5 18 19 #define EXI_CSR 0x00 20 #define EXI_CSR_CLKMASK (0x7<<4) 21 #define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4) 22 #define EXI_CSR_CSMASK (0x7<<7) 23 #define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */ 24 25 #define EXI_CR 0x0c 26 #define EXI_CR_TSTART (1<<0) 27 #define EXI_CR_WRITE (1<<2) 28 #define EXI_CR_READ_WRITE (2<<2) 29 #define EXI_CR_TLEN(len) (((len)-1)<<4) 30 31 #define EXI_DATA 0x10 32 33 34 /* virtual address base for input/output, retrieved from device tree */ 35 static void *ug_io_base; 36 37 38 static u32 ug_io_transaction(u32 in) 39 { 40 u32 *csr_reg = ug_io_base + EXI_CSR; 41 u32 *data_reg = ug_io_base + EXI_DATA; 42 u32 *cr_reg = ug_io_base + EXI_CR; 43 u32 csr, data, cr; 44 45 /* select */ 46 csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0; 47 out_be32(csr_reg, csr); 48 49 /* read/write */ 50 data = in; 51 out_be32(data_reg, data); 52 cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART; 53 out_be32(cr_reg, cr); 54 55 while (in_be32(cr_reg) & EXI_CR_TSTART) 56 barrier(); 57 58 /* deselect */ 59 out_be32(csr_reg, 0); 60 61 data = in_be32(data_reg); 62 return data; 63 } 64 65 static int ug_is_txfifo_ready(void) 66 { 67 return ug_io_transaction(0xc0000000) & 0x04000000; 68 } 69 70 static void ug_raw_putc(char ch) 71 { 72 ug_io_transaction(0xb0000000 | (ch << 20)); 73 } 74 75 static void ug_putc(char ch) 76 { 77 int count = 16; 78 79 if (!ug_io_base) 80 return; 81 82 while (!ug_is_txfifo_ready() && count--) 83 barrier(); 84 if (count >= 0) 85 ug_raw_putc(ch); 86 } 87 88 void ug_console_write(const char *buf, int len) 89 { 90 char *b = (char *)buf; 91 92 while (len--) { 93 if (*b == '\n') 94 ug_putc('\r'); 95 ug_putc(*b++); 96 } 97 } 98 99 static int ug_is_adapter_present(void) 100 { 101 if (!ug_io_base) 102 return 0; 103 return ug_io_transaction(0x90000000) == 0x04700000; 104 } 105 106 static void *ug_grab_exi_io_base(void) 107 { 108 u32 v; 109 void *devp; 110 111 devp = find_node_by_compatible(NULL, "nintendo,flipper-exi"); 112 if (devp == NULL) 113 goto err_out; 114 if (getprop(devp, "virtual-reg", &v, sizeof(v)) != sizeof(v)) 115 goto err_out; 116 117 return (void *)v; 118 119 err_out: 120 return NULL; 121 } 122 123 void *ug_probe(void) 124 { 125 void *exi_io_base; 126 int i; 127 128 exi_io_base = ug_grab_exi_io_base(); 129 if (!exi_io_base) 130 return NULL; 131 132 /* look for a usbgecko on memcard slots A and B */ 133 for (i = 0; i < 2; i++) { 134 ug_io_base = exi_io_base + 0x14 * i; 135 if (ug_is_adapter_present()) 136 break; 137 } 138 if (i == 2) 139 ug_io_base = NULL; 140 return ug_io_base; 141 } 142 143