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>
21 #include "usbgecko_udbg.h"
24 #define EXI_CLK_32MHZ 5
27 #define EXI_CSR_CLKMASK (0x7<<4)
28 #define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4)
29 #define EXI_CSR_CSMASK (0x7<<7)
30 #define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */
33 #define EXI_CR_TSTART (1<<0)
34 #define EXI_CR_WRITE (1<<2)
35 #define EXI_CR_READ_WRITE (2<<2)
36 #define EXI_CR_TLEN(len) (((len)-1)<<4)
40 #define UG_READ_ATTEMPTS 100
41 #define UG_WRITE_ATTEMPTS 100
44 static void __iomem *ug_io_base;
47 * Performs one input/output transaction between the exi host and the usbgecko.
49 static u32 ug_io_transaction(u32 in)
51 u32 __iomem *csr_reg = ug_io_base + EXI_CSR;
52 u32 __iomem *data_reg = ug_io_base + EXI_DATA;
53 u32 __iomem *cr_reg = ug_io_base + EXI_CR;
57 csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
58 out_be32(csr_reg, csr);
62 out_be32(data_reg, data);
63 cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
66 while (in_be32(cr_reg) & EXI_CR_TSTART)
73 data = in_be32(data_reg);
79 * Returns true if an usbgecko adapter is found.
81 static int ug_is_adapter_present(void)
86 return ug_io_transaction(0x90000000) == 0x04700000;
90 * Returns true if the TX fifo is ready for transmission.
92 static int ug_is_txfifo_ready(void)
94 return ug_io_transaction(0xc0000000) & 0x04000000;
98 * Tries to transmit a character.
99 * If the TX fifo is not ready the result is undefined.
101 static void ug_raw_putc(char ch)
103 ug_io_transaction(0xb0000000 | (ch << 20));
107 * Transmits a character.
108 * It silently fails if the TX fifo is not ready after a number of retries.
110 static void ug_putc(char ch)
112 int count = UG_WRITE_ATTEMPTS;
120 while (!ug_is_txfifo_ready() && count--)
127 * Returns true if the RX fifo is ready for transmission.
129 static int ug_is_rxfifo_ready(void)
131 return ug_io_transaction(0xd0000000) & 0x04000000;
135 * Tries to receive a character.
136 * If a character is unavailable the function returns -1.
138 static int ug_raw_getc(void)
140 u32 data = ug_io_transaction(0xa0000000);
141 if (data & 0x08000000)
142 return (data >> 16) & 0xff;
148 * Receives a character.
149 * It fails if the RX fifo is not ready after a number of retries.
151 static int ug_getc(void)
153 int count = UG_READ_ATTEMPTS;
158 while (!ug_is_rxfifo_ready() && count--)
160 return ug_raw_getc();
169 * Transmits a character.
171 void ug_udbg_putc(char ch)
177 * Receives a character. Waits until a character is available.
179 static int ug_udbg_getc(void)
183 while ((ch = ug_getc()) == -1)
189 * Receives a character. If a character is not available, returns -1.
191 static int ug_udbg_getc_poll(void)
193 if (!ug_is_rxfifo_ready())
199 * Retrieves and prepares the virtual address needed to access the hardware.
201 static void __iomem *ug_udbg_setup_exi_io_base(struct device_node *np)
203 void __iomem *exi_io_base = NULL;
205 const unsigned int *reg;
207 reg = of_get_property(np, "reg", NULL);
209 paddr = of_translate_address(np, reg);
211 exi_io_base = ioremap(paddr, reg[1]);
217 * Checks if a USB Gecko adapter is inserted in any memory card slot.
219 static void __iomem *ug_udbg_probe(void __iomem *exi_io_base)
223 /* look for a usbgecko on memcard slots A and B */
224 for (i = 0; i < 2; i++) {
225 ug_io_base = exi_io_base + 0x14 * i;
226 if (ug_is_adapter_present())
236 * USB Gecko udbg support initialization.
238 void __init ug_udbg_init(void)
240 struct device_node *np;
241 void __iomem *exi_io_base;
244 udbg_printf("%s: early -> final\n", __func__);
246 np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi");
248 udbg_printf("%s: EXI node not found\n", __func__);
252 exi_io_base = ug_udbg_setup_exi_io_base(np);
254 udbg_printf("%s: failed to setup EXI io base\n", __func__);
258 if (!ug_udbg_probe(exi_io_base)) {
259 udbg_printf("usbgecko_udbg: not found\n");
260 iounmap(exi_io_base);
262 udbg_putc = ug_udbg_putc;
263 udbg_getc = ug_udbg_getc;
264 udbg_getc_poll = ug_udbg_getc_poll;
265 udbg_printf("usbgecko_udbg: ready\n");