treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / arch / powerpc / boot / ugecon.c
blob938a38bd40ba9d225f5067611522a89faaa7e9ff
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * arch/powerpc/boot/ugecon.c
5 * USB Gecko bootwrapper console.
6 * Copyright (C) 2008-2009 The GameCube Linux Team
7 * Copyright (C) 2008,2009 Albert Herranz
8 */
10 #include <stddef.h>
11 #include "stdio.h"
12 #include "types.h"
13 #include "io.h"
14 #include "ops.h"
17 #define EXI_CLK_32MHZ 5
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 */
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)
31 #define EXI_DATA 0x10
34 /* virtual address base for input/output, retrieved from device tree */
35 static void *ug_io_base;
38 static u32 ug_io_transaction(u32 in)
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;
45 /* select */
46 csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
47 out_be32(csr_reg, csr);
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);
55 while (in_be32(cr_reg) & EXI_CR_TSTART)
56 barrier();
58 /* deselect */
59 out_be32(csr_reg, 0);
61 data = in_be32(data_reg);
62 return data;
65 static int ug_is_txfifo_ready(void)
67 return ug_io_transaction(0xc0000000) & 0x04000000;
70 static void ug_raw_putc(char ch)
72 ug_io_transaction(0xb0000000 | (ch << 20));
75 static void ug_putc(char ch)
77 int count = 16;
79 if (!ug_io_base)
80 return;
82 while (!ug_is_txfifo_ready() && count--)
83 barrier();
84 if (count >= 0)
85 ug_raw_putc(ch);
88 void ug_console_write(const char *buf, int len)
90 char *b = (char *)buf;
92 while (len--) {
93 if (*b == '\n')
94 ug_putc('\r');
95 ug_putc(*b++);
99 static int ug_is_adapter_present(void)
101 if (!ug_io_base)
102 return 0;
103 return ug_io_transaction(0x90000000) == 0x04700000;
106 static void *ug_grab_exi_io_base(void)
108 u32 v;
109 void *devp;
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;
117 return (void *)v;
119 err_out:
120 return NULL;
123 void *ug_probe(void)
125 void *exi_io_base;
126 int i;
128 exi_io_base = ug_grab_exi_io_base();
129 if (!exi_io_base)
130 return NULL;
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;
138 if (i == 2)
139 ug_io_base = NULL;
140 return ug_io_base;