Patchwork [RFC,09/19] powerpc: gamecube/wii: udbg support for usbgecko

login
register
mail settings
Submitter Albert Herranz
Date Nov. 22, 2009, 10:01 p.m.
Message ID <1258927311-4340-10-git-send-email-albert_herranz@yahoo.es>
Download mbox | patch
Permalink /patch/39008/
State Changes Requested
Headers show

Comments

Albert Herranz - Nov. 22, 2009, 10:01 p.m.
Add support for using the USB Gecko adapter via the udbg facility on
the Nintendo GameCube and Wii video game consoles.
The USB Gecko is a 3rd party memory card interface adapter that provides
a EXI (External Interface) to USB serial converter.

Signed-off-by: Albert Herranz <albert_herranz@yahoo.es>
---
 arch/powerpc/platforms/embedded6xx/Kconfig         |   13 +
 arch/powerpc/platforms/embedded6xx/Makefile        |    1 +
 arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c |  285 ++++++++++++++++++++
 arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h |   30 ++
 4 files changed, 329 insertions(+), 0 deletions(-)
 create mode 100644 arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
 create mode 100644 arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
Grant Likely - Nov. 22, 2009, 11:22 p.m.
On Sun, Nov 22, 2009 at 3:01 PM, Albert Herranz <albert_herranz@yahoo.es> wrote:
> Add support for using the USB Gecko adapter via the udbg facility on
> the Nintendo GameCube and Wii video game consoles.
> The USB Gecko is a 3rd party memory card interface adapter that provides
> a EXI (External Interface) to USB serial converter.
>
> Signed-off-by: Albert Herranz <albert_herranz@yahoo.es>

Acked-by: Grant Likely <grant.likely@secretlab.ca>

> ---
>  arch/powerpc/platforms/embedded6xx/Kconfig         |   13 +
>  arch/powerpc/platforms/embedded6xx/Makefile        |    1 +
>  arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c |  285 ++++++++++++++++++++
>  arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h |   30 ++
>  4 files changed, 329 insertions(+), 0 deletions(-)
>  create mode 100644 arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
>  create mode 100644 arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
>
> diff --git a/arch/powerpc/platforms/embedded6xx/Kconfig b/arch/powerpc/platforms/embedded6xx/Kconfig
> index 31487e4..bfd88be 100644
> --- a/arch/powerpc/platforms/embedded6xx/Kconfig
> +++ b/arch/powerpc/platforms/embedded6xx/Kconfig
> @@ -95,3 +95,16 @@ config GAMECUBE_COMMON
>        bool
>        select NOT_COHERENT_CACHE
>
> +config USBGECKO_UDBG
> +       bool "USB Gecko udbg console for the Nintendo GameCube/Wii"
> +       depends on GAMECUBE_COMMON
> +       help
> +         If you say yes to this option, support will be included for the
> +         USB Gecko adapter as an udbg console.
> +         The USB Gecko is a EXI to USB Serial converter that can be plugged
> +         into a memcard slot in the Nintendo GameCube/Wii.
> +
> +         This driver bypasses the EXI layer completely.
> +
> +         If in doubt, say N here.
> +
> diff --git a/arch/powerpc/platforms/embedded6xx/Makefile b/arch/powerpc/platforms/embedded6xx/Makefile
> index 0773c08..0ab7492 100644
> --- a/arch/powerpc/platforms/embedded6xx/Makefile
> +++ b/arch/powerpc/platforms/embedded6xx/Makefile
> @@ -7,3 +7,4 @@ obj-$(CONFIG_STORCENTER)        += storcenter.o
>  obj-$(CONFIG_PPC_HOLLY)                += holly.o
>  obj-$(CONFIG_PPC_PRPMC2800)    += prpmc2800.o
>  obj-$(CONFIG_PPC_C2K)          += c2k.o
> +obj-$(CONFIG_USBGECKO_UDBG)    += usbgecko_udbg.o
> diff --git a/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
> new file mode 100644
> index 0000000..49f86e8
> --- /dev/null
> +++ b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
> @@ -0,0 +1,285 @@
> +/*
> + * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
> + *
> + * udbg serial input/output routines for the USB Gecko adapter.
> + * Copyright (C) 2008-2009 The GameCube Linux Team
> + * Copyright (C) 2008,2009 Albert Herranz
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * as published by the Free Software Foundation; either version 2
> + * of the License, or (at your option) any later version.
> + *
> + */
> +
> +#include <asm/io.h>
> +#include <asm/prom.h>
> +#include <asm/udbg.h>
> +
> +#include <mm/mmu_decl.h>
> +
> +#include "usbgecko_udbg.h"
> +
> +
> +#define EXI_CLK_32MHZ           5
> +
> +#define EXI_CSR                 0x00
> +#define   EXI_CSR_CLKMASK       (0x7<<4)
> +#define     EXI_CSR_CLK_32MHZ   (EXI_CLK_32MHZ<<4)
> +#define   EXI_CSR_CSMASK        (0x7<<7)
> +#define     EXI_CSR_CS_0        (0x1<<7)  /* Chip Select 001 */
> +
> +#define EXI_CR                  0x0c
> +#define   EXI_CR_TSTART         (1<<0)
> +#define   EXI_CR_WRITE         (1<<2)
> +#define   EXI_CR_READ_WRITE     (2<<2)
> +#define   EXI_CR_TLEN(len)      (((len)-1)<<4)
> +
> +#define EXI_DATA                0x10
> +
> +#define UG_READ_ATTEMPTS       100
> +#define UG_WRITE_ATTEMPTS      100
> +
> +
> +static void __iomem *ug_io_base;
> +
> +/*
> + * Performs one input/output transaction between the exi host and the usbgecko.
> + */
> +static u32 ug_io_transaction(u32 in)
> +{
> +       u32 __iomem *csr_reg = ug_io_base + EXI_CSR;
> +       u32 __iomem *data_reg = ug_io_base + EXI_DATA;
> +       u32 __iomem *cr_reg = ug_io_base + EXI_CR;
> +       u32 csr, data, cr;
> +
> +       /* select */
> +       csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
> +       out_be32(csr_reg, csr);
> +
> +       /* read/write */
> +       data = in;
> +       out_be32(data_reg, data);
> +       cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
> +       out_be32(cr_reg, cr);
> +
> +       while (in_be32(cr_reg) & EXI_CR_TSTART)
> +               barrier();
> +
> +       /* deselect */
> +       out_be32(csr_reg, 0);
> +
> +       /* result */
> +       data = in_be32(data_reg);
> +
> +       return data;
> +}
> +
> +/*
> + * Returns true if an usbgecko adapter is found.
> + */
> +static int ug_is_adapter_present(void)
> +{
> +       if (!ug_io_base)
> +               return 0;
> +
> +       return ug_io_transaction(0x90000000) == 0x04700000;
> +}
> +
> +/*
> + * Returns true if the TX fifo is ready for transmission.
> + */
> +static int ug_is_txfifo_ready(void)
> +{
> +       return ug_io_transaction(0xc0000000) & 0x04000000;
> +}
> +
> +/*
> + * Tries to transmit a character.
> + * If the TX fifo is not ready the result is undefined.
> + */
> +static void ug_raw_putc(char ch)
> +{
> +       ug_io_transaction(0xb0000000 | (ch << 20));
> +}
> +
> +/*
> + * Transmits a character.
> + * It silently fails if the TX fifo is not ready after a number of retries.
> + */
> +static void ug_putc(char ch)
> +{
> +       int count = UG_WRITE_ATTEMPTS;
> +
> +       if (!ug_io_base)
> +               return;
> +
> +       if (ch == '\n')
> +               ug_putc('\r');
> +
> +       while (!ug_is_txfifo_ready() && count--)
> +               barrier();
> +       if (count)
> +               ug_raw_putc(ch);
> +}
> +
> +#if 0
> +/*
> + * Trasmits a null terminated character string.
> + */
> +static void ug_puts(char *s)
> +{
> +       while (*s)
> +               ug_putc(*s++);
> +}
> +#endif
> +
> +/*
> + * Returns true if the RX fifo is ready for transmission.
> + */
> +static int ug_is_rxfifo_ready(void)
> +{
> +       return ug_io_transaction(0xd0000000) & 0x04000000;
> +}
> +
> +/*
> + * Tries to receive a character.
> + * If a character is unavailable the function returns -1.
> + */
> +static int ug_raw_getc(void)
> +{
> +       u32 data = ug_io_transaction(0xa0000000);
> +       if (data & 0x08000000)
> +               return (data >> 16) & 0xff;
> +       else
> +               return -1;
> +}
> +
> +/*
> + * Receives a character.
> + * It fails if the RX fifo is not ready after a number of retries.
> + */
> +static int ug_getc(void)
> +{
> +       int count = UG_READ_ATTEMPTS;
> +
> +       if (!ug_io_base)
> +               return -1;
> +
> +       while (!ug_is_rxfifo_ready() && count--)
> +               barrier();
> +       return ug_raw_getc();
> +}
> +
> +/*
> + * udbg functions.
> + *
> + */
> +
> +/*
> + * Transmits a character.
> + */
> +void ug_udbg_putc(char ch)
> +{
> +       ug_putc(ch);
> +}
> +
> +/*
> + * Receives a character. Waits until a character is available.
> + */
> +static int ug_udbg_getc(void)
> +{
> +       int ch;
> +
> +       while ((ch = ug_getc()) == -1)
> +               barrier();
> +       return ch;
> +}
> +
> +/*
> + * Receives a character. If a character is not available, returns -1.
> + */
> +static int ug_udbg_getc_poll(void)
> +{
> +       if (!ug_is_rxfifo_ready())
> +               return -1;
> +       return ug_getc();
> +}
> +
> +/*
> + * Retrieves and prepares the virtual address needed to access the hardware.
> + */
> +static void __iomem *ug_udbg_setup_io_base(struct device_node *np)
> +{
> +       phys_addr_t paddr;
> +       const unsigned int *reg;
> +
> +       reg = of_get_property(np, "reg", NULL);
> +       if (reg) {
> +               paddr = of_translate_address(np, reg);
> +               if (paddr) {
> +                       ug_io_base = ioremap(paddr, reg[1]);
> +                       return ug_io_base;
> +               }
> +       }
> +       return NULL;
> +}
> +
> +/*
> + * USB Gecko udbg support initialization.
> + */
> +void __init ug_udbg_init(void)
> +{
> +       struct device_node *np = NULL;
> +       struct device_node *stdout;
> +       const char *path;
> +
> +       if (!of_chosen) {
> +               udbg_printf("%s: missing of_chosen\n", __func__);
> +               goto done;
> +       }
> +
> +       path = of_get_property(of_chosen, "linux,stdout-path", NULL);
> +       if (!path) {
> +               udbg_printf("%s: missing %s property", __func__,
> +                           "linux,stdout-path");
> +               goto done;
> +       }
> +
> +       stdout = of_find_node_by_path(path);
> +       if (!stdout) {
> +               udbg_printf("%s: missing path %s", __func__, path);
> +               goto done;
> +       }
> +
> +       for (np = NULL;
> +           (np = of_find_compatible_node(np, NULL, "usbgecko,usbgecko"));)
> +               if (np == stdout)
> +                       break;
> +
> +       of_node_put(stdout);
> +       if (!np) {
> +               udbg_printf("%s: stdout is not an usbgecko", __func__);
> +               goto done;
> +       }
> +
> +       if (!ug_udbg_setup_io_base(np)) {
> +               udbg_printf("%s: failed to setup io base", __func__);
> +               goto done;
> +       }
> +
> +       if (!ug_is_adapter_present()) {
> +               udbg_printf("usbgecko_udbg: not found\n");
> +               ug_io_base = NULL;
> +       } else {
> +               udbg_putc = ug_udbg_putc;
> +               udbg_getc = ug_udbg_getc;
> +               udbg_getc_poll = ug_udbg_getc_poll;
> +               udbg_printf("usbgecko_udbg: ready\n");
> +       }
> +
> +done:
> +       if (np)
> +               of_node_put(np);
> +       return;
> +}
> diff --git a/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
> new file mode 100644
> index 0000000..3929de3
> --- /dev/null
> +++ b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
> @@ -0,0 +1,30 @@
> +/*
> + * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
> + *
> + * udbg serial input/output routines for the USB Gecko adapter.
> + * Copyright (C) 2008-2009 The GameCube Linux Team
> + * Copyright (C) 2008,2009 Albert Herranz
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * as published by the Free Software Foundation; either version 2
> + * of the License, or (at your option) any later version.
> + *
> + */
> +
> +#ifndef __USBGECKO_UDBG_H
> +#define __USBGECKO_UDBG_H
> +
> +#ifdef CONFIG_USBGECKO_UDBG
> +
> +extern void __init ug_udbg_init(void);
> +
> +#else
> +
> +static inline void __init ug_udbg_init(void)
> +{
> +}
> +
> +#endif /* CONFIG_USBGECKO_UDBG */
> +
> +#endif /* __USBGECKO_UDBG_H */
> --
> 1.6.3.3
>
> _______________________________________________
> Linuxppc-dev mailing list
> Linuxppc-dev@lists.ozlabs.org
> https://lists.ozlabs.org/listinfo/linuxppc-dev
>
Segher Boessenkool - Nov. 24, 2009, 12:49 a.m.
> +	  If you say yes to this option, support will be included for the
> +	  USB Gecko adapter as an udbg console.
> +	  The USB Gecko is a EXI to USB Serial converter that can be plugged
> +	  into a memcard slot in the Nintendo GameCube/Wii.

Not "a" memcard slot, only the first one, you have it hardcoded.

> +#if 0
> +/*
> + * Trasmits a null terminated character string.
> + */
> +static void ug_puts(char *s)
> +{
> +	while (*s)
> +		ug_putc(*s++);
> +}
> +#endif

Remove?

> +	stdout = of_find_node_by_path(path);
> +	if (!stdout) {
> +		udbg_printf("%s: missing path %s", __func__, path);
> +		goto done;
> +	}
> +
> +	for (np = NULL;
> +	    (np = of_find_compatible_node(np, NULL, "usbgecko,usbgecko"));)
> +		if (np == stdout)
> +			break;
> +
> +	of_node_put(stdout);
> +	if (!np) {
> +		udbg_printf("%s: stdout is not an usbgecko", __func__);
> +		goto done;
> +	}

Surely there is something called something like of_node_is_compatible()
you can use here?  You already have the node pointer, there is no need
to look at all other nodes.


Segher
Albert Herranz - Nov. 24, 2009, 6:08 p.m.
Segher Boessenkool wrote:
>> +      If you say yes to this option, support will be included for the
>> +      USB Gecko adapter as an udbg console.
>> +      The USB Gecko is a EXI to USB Serial converter that can be plugged
>> +      into a memcard slot in the Nintendo GameCube/Wii.
> 
> Not "a" memcard slot, only the first one, you have it hardcoded.
> 

No. It's not hardcoded in the code. It's specified in the device tree source.
The hardcoded one is just the early udbg, not the "normal" udbg.

>> +#if 0
>> +/*
>> + * Trasmits a null terminated character string.
>> + */
>> +static void ug_puts(char *s)
>> +{
>> +    while (*s)
>> +        ug_putc(*s++);
>> +}
>> +#endif
> 
> Remove?
> 

Ok :)

>> +    stdout = of_find_node_by_path(path);
>> +    if (!stdout) {
>> +        udbg_printf("%s: missing path %s", __func__, path);
>> +        goto done;
>> +    }
>> +
>> +    for (np = NULL;
>> +        (np = of_find_compatible_node(np, NULL, "usbgecko,usbgecko"));)
>> +        if (np == stdout)
>> +            break;
>> +
>> +    of_node_put(stdout);
>> +    if (!np) {
>> +        udbg_printf("%s: stdout is not an usbgecko", __func__);
>> +        goto done;
>> +    }
> 
> Surely there is something called something like of_node_is_compatible()
> you can use here?  You already have the node pointer, there is no need
> to look at all other nodes.
> 

I see the point.
I didn't find of_node_is_compatible() but I'll look what's available here.

Thanks,
Albert
Segher Boessenkool - Nov. 24, 2009, 9:19 p.m.
>>> +      If you say yes to this option, support will be included  
>>> for the
>>> +      USB Gecko adapter as an udbg console.
>>> +      The USB Gecko is a EXI to USB Serial converter that can be  
>>> plugged
>>> +      into a memcard slot in the Nintendo GameCube/Wii.
>>
>> Not "a" memcard slot, only the first one, you have it hardcoded.
>>
>
> No. It's not hardcoded in the code. It's specified in the device  
> tree source.
> The hardcoded one is just the early udbg, not the "normal" udbg.

Ah I misread the code then.

>> Surely there is something called something like  
>> of_node_is_compatible()
>> you can use here?  You already have the node pointer, there is no  
>> need
>> to look at all other nodes.
>>
>
> I see the point.
> I didn't find of_node_is_compatible() but I'll look what's  
> available here.

int of_device_is_compatible(const struct device_node *device,
                 const char *compat)


Segher
Albert Herranz - Nov. 25, 2009, 5:07 p.m.
Segher Boessenkool wrote:
>>> Surely there is something called something like of_node_is_compatible()
>>> you can use here?  You already have the node pointer, there is no need
>>> to look at all other nodes.
>>>
>>
>> I see the point.
>> I didn't find of_node_is_compatible() but I'll look what's available
>> here.
> 
> int of_device_is_compatible(const struct device_node *device,
>                 const char *compat)
> 

Thanks. I'll pick that.

Cheers,
Albert
Benjamin Herrenschmidt - Nov. 26, 2009, 5:12 a.m.
On Sun, 2009-11-22 at 23:01 +0100, Albert Herranz wrote:
> Add support for using the USB Gecko adapter via the udbg facility on
> the Nintendo GameCube and Wii video game consoles.
> The USB Gecko is a 3rd party memory card interface adapter that provides
> a EXI (External Interface) to USB serial converter.

The main issue here is that the gecko is supposed to be something you
can plug or unplug no ? Is it hotswap or only at boot ? In the later
case, maybe the node should be created by the boot wrapper when
detecting the presence of the device. If it's hotplug, then we need
something smarter...

Cheers,
Ben.

> Signed-off-by: Albert Herranz <albert_herranz@yahoo.es>
> ---
>  arch/powerpc/platforms/embedded6xx/Kconfig         |   13 +
>  arch/powerpc/platforms/embedded6xx/Makefile        |    1 +
>  arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c |  285 ++++++++++++++++++++
>  arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h |   30 ++
>  4 files changed, 329 insertions(+), 0 deletions(-)
>  create mode 100644 arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
>  create mode 100644 arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
> 
> diff --git a/arch/powerpc/platforms/embedded6xx/Kconfig b/arch/powerpc/platforms/embedded6xx/Kconfig
> index 31487e4..bfd88be 100644
> --- a/arch/powerpc/platforms/embedded6xx/Kconfig
> +++ b/arch/powerpc/platforms/embedded6xx/Kconfig
> @@ -95,3 +95,16 @@ config GAMECUBE_COMMON
>  	bool
>  	select NOT_COHERENT_CACHE
>  
> +config USBGECKO_UDBG
> +	bool "USB Gecko udbg console for the Nintendo GameCube/Wii"
> +	depends on GAMECUBE_COMMON
> +	help
> +	  If you say yes to this option, support will be included for the
> +	  USB Gecko adapter as an udbg console.
> +	  The USB Gecko is a EXI to USB Serial converter that can be plugged
> +	  into a memcard slot in the Nintendo GameCube/Wii.
> +
> +	  This driver bypasses the EXI layer completely.
> +
> +	  If in doubt, say N here.
> +
> diff --git a/arch/powerpc/platforms/embedded6xx/Makefile b/arch/powerpc/platforms/embedded6xx/Makefile
> index 0773c08..0ab7492 100644
> --- a/arch/powerpc/platforms/embedded6xx/Makefile
> +++ b/arch/powerpc/platforms/embedded6xx/Makefile
> @@ -7,3 +7,4 @@ obj-$(CONFIG_STORCENTER)	+= storcenter.o
>  obj-$(CONFIG_PPC_HOLLY)		+= holly.o
>  obj-$(CONFIG_PPC_PRPMC2800)	+= prpmc2800.o
>  obj-$(CONFIG_PPC_C2K)		+= c2k.o
> +obj-$(CONFIG_USBGECKO_UDBG)	+= usbgecko_udbg.o
> diff --git a/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
> new file mode 100644
> index 0000000..49f86e8
> --- /dev/null
> +++ b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
> @@ -0,0 +1,285 @@
> +/*
> + * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
> + *
> + * udbg serial input/output routines for the USB Gecko adapter.
> + * Copyright (C) 2008-2009 The GameCube Linux Team
> + * Copyright (C) 2008,2009 Albert Herranz
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * as published by the Free Software Foundation; either version 2
> + * of the License, or (at your option) any later version.
> + *
> + */
> +
> +#include <asm/io.h>
> +#include <asm/prom.h>
> +#include <asm/udbg.h>
> +
> +#include <mm/mmu_decl.h>
> +
> +#include "usbgecko_udbg.h"
> +
> +
> +#define EXI_CLK_32MHZ           5
> +
> +#define EXI_CSR                 0x00
> +#define   EXI_CSR_CLKMASK       (0x7<<4)
> +#define     EXI_CSR_CLK_32MHZ   (EXI_CLK_32MHZ<<4)
> +#define   EXI_CSR_CSMASK        (0x7<<7)
> +#define     EXI_CSR_CS_0        (0x1<<7)  /* Chip Select 001 */
> +
> +#define EXI_CR                  0x0c
> +#define   EXI_CR_TSTART         (1<<0)
> +#define   EXI_CR_WRITE		(1<<2)
> +#define   EXI_CR_READ_WRITE     (2<<2)
> +#define   EXI_CR_TLEN(len)      (((len)-1)<<4)
> +
> +#define EXI_DATA                0x10
> +
> +#define UG_READ_ATTEMPTS	100
> +#define UG_WRITE_ATTEMPTS	100
> +
> +
> +static void __iomem *ug_io_base;
> +
> +/*
> + * Performs one input/output transaction between the exi host and the usbgecko.
> + */
> +static u32 ug_io_transaction(u32 in)
> +{
> +	u32 __iomem *csr_reg = ug_io_base + EXI_CSR;
> +	u32 __iomem *data_reg = ug_io_base + EXI_DATA;
> +	u32 __iomem *cr_reg = ug_io_base + EXI_CR;
> +	u32 csr, data, cr;
> +
> +	/* select */
> +	csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
> +	out_be32(csr_reg, csr);
> +
> +	/* read/write */
> +	data = in;
> +	out_be32(data_reg, data);
> +	cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
> +	out_be32(cr_reg, cr);
> +
> +	while (in_be32(cr_reg) & EXI_CR_TSTART)
> +		barrier();
> +
> +	/* deselect */
> +	out_be32(csr_reg, 0);
> +
> +	/* result */
> +	data = in_be32(data_reg);
> +
> +	return data;
> +}
> +
> +/*
> + * Returns true if an usbgecko adapter is found.
> + */
> +static int ug_is_adapter_present(void)
> +{
> +	if (!ug_io_base)
> +		return 0;
> +
> +	return ug_io_transaction(0x90000000) == 0x04700000;
> +}
> +
> +/*
> + * Returns true if the TX fifo is ready for transmission.
> + */
> +static int ug_is_txfifo_ready(void)
> +{
> +	return ug_io_transaction(0xc0000000) & 0x04000000;
> +}
> +
> +/*
> + * Tries to transmit a character.
> + * If the TX fifo is not ready the result is undefined.
> + */
> +static void ug_raw_putc(char ch)
> +{
> +	ug_io_transaction(0xb0000000 | (ch << 20));
> +}
> +
> +/*
> + * Transmits a character.
> + * It silently fails if the TX fifo is not ready after a number of retries.
> + */
> +static void ug_putc(char ch)
> +{
> +	int count = UG_WRITE_ATTEMPTS;
> +
> +	if (!ug_io_base)
> +		return;
> +
> +	if (ch == '\n')
> +		ug_putc('\r');
> +
> +	while (!ug_is_txfifo_ready() && count--)
> +		barrier();
> +	if (count)
> +		ug_raw_putc(ch);
> +}
> +
> +#if 0
> +/*
> + * Trasmits a null terminated character string.
> + */
> +static void ug_puts(char *s)
> +{
> +	while (*s)
> +		ug_putc(*s++);
> +}
> +#endif
> +
> +/*
> + * Returns true if the RX fifo is ready for transmission.
> + */
> +static int ug_is_rxfifo_ready(void)
> +{
> +	return ug_io_transaction(0xd0000000) & 0x04000000;
> +}
> +
> +/*
> + * Tries to receive a character.
> + * If a character is unavailable the function returns -1.
> + */
> +static int ug_raw_getc(void)
> +{
> +	u32 data = ug_io_transaction(0xa0000000);
> +	if (data & 0x08000000)
> +		return (data >> 16) & 0xff;
> +	else
> +		return -1;
> +}
> +
> +/*
> + * Receives a character.
> + * It fails if the RX fifo is not ready after a number of retries.
> + */
> +static int ug_getc(void)
> +{
> +	int count = UG_READ_ATTEMPTS;
> +
> +	if (!ug_io_base)
> +		return -1;
> +
> +	while (!ug_is_rxfifo_ready() && count--)
> +		barrier();
> +	return ug_raw_getc();
> +}
> +
> +/*
> + * udbg functions.
> + *
> + */
> +
> +/*
> + * Transmits a character.
> + */
> +void ug_udbg_putc(char ch)
> +{
> +	ug_putc(ch);
> +}
> +
> +/*
> + * Receives a character. Waits until a character is available.
> + */
> +static int ug_udbg_getc(void)
> +{
> +	int ch;
> +
> +	while ((ch = ug_getc()) == -1)
> +		barrier();
> +	return ch;
> +}
> +
> +/*
> + * Receives a character. If a character is not available, returns -1.
> + */
> +static int ug_udbg_getc_poll(void)
> +{
> +	if (!ug_is_rxfifo_ready())
> +		return -1;
> +	return ug_getc();
> +}
> +
> +/*
> + * Retrieves and prepares the virtual address needed to access the hardware.
> + */
> +static void __iomem *ug_udbg_setup_io_base(struct device_node *np)
> +{
> +	phys_addr_t paddr;
> +	const unsigned int *reg;
> +
> +	reg = of_get_property(np, "reg", NULL);
> +	if (reg) {
> +		paddr = of_translate_address(np, reg);
> +		if (paddr) {
> +			ug_io_base = ioremap(paddr, reg[1]);
> +			return ug_io_base;
> +		}
> +	}
> +	return NULL;
> +}
> +
> +/*
> + * USB Gecko udbg support initialization.
> + */
> +void __init ug_udbg_init(void)
> +{
> +	struct device_node *np = NULL;
> +	struct device_node *stdout;
> +	const char *path;
> +
> +	if (!of_chosen) {
> +		udbg_printf("%s: missing of_chosen\n", __func__);
> +		goto done;
> +	}
> +
> +	path = of_get_property(of_chosen, "linux,stdout-path", NULL);
> +	if (!path) {
> +		udbg_printf("%s: missing %s property", __func__,
> +			    "linux,stdout-path");
> +		goto done;
> +	}
> +
> +	stdout = of_find_node_by_path(path);
> +	if (!stdout) {
> +		udbg_printf("%s: missing path %s", __func__, path);
> +		goto done;
> +	}
> +
> +	for (np = NULL;
> +	    (np = of_find_compatible_node(np, NULL, "usbgecko,usbgecko"));)
> +		if (np == stdout)
> +			break;
> +
> +	of_node_put(stdout);
> +	if (!np) {
> +		udbg_printf("%s: stdout is not an usbgecko", __func__);
> +		goto done;
> +	}
> +
> +	if (!ug_udbg_setup_io_base(np)) {
> +		udbg_printf("%s: failed to setup io base", __func__);
> +		goto done;
> +	}
> +
> +	if (!ug_is_adapter_present()) {
> +		udbg_printf("usbgecko_udbg: not found\n");
> +		ug_io_base = NULL;
> +	} else {
> +		udbg_putc = ug_udbg_putc;
> +		udbg_getc = ug_udbg_getc;
> +		udbg_getc_poll = ug_udbg_getc_poll;
> +		udbg_printf("usbgecko_udbg: ready\n");
> +	}
> +
> +done:
> +	if (np)
> +		of_node_put(np);
> +	return;
> +}
> diff --git a/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
> new file mode 100644
> index 0000000..3929de3
> --- /dev/null
> +++ b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
> @@ -0,0 +1,30 @@
> +/*
> + * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
> + *
> + * udbg serial input/output routines for the USB Gecko adapter.
> + * Copyright (C) 2008-2009 The GameCube Linux Team
> + * Copyright (C) 2008,2009 Albert Herranz
> + *
> + * This program is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU General Public License
> + * as published by the Free Software Foundation; either version 2
> + * of the License, or (at your option) any later version.
> + *
> + */
> +
> +#ifndef __USBGECKO_UDBG_H
> +#define __USBGECKO_UDBG_H
> +
> +#ifdef CONFIG_USBGECKO_UDBG
> +
> +extern void __init ug_udbg_init(void);
> +
> +#else
> +
> +static inline void __init ug_udbg_init(void)
> +{
> +}
> +
> +#endif /* CONFIG_USBGECKO_UDBG */
> +
> +#endif /* __USBGECKO_UDBG_H */
Albert Herranz - Nov. 26, 2009, 3:28 p.m.
Benjamin Herrenschmidt wrote:
> On Sun, 2009-11-22 at 23:01 +0100, Albert Herranz wrote:
>> Add support for using the USB Gecko adapter via the udbg facility on
>> the Nintendo GameCube and Wii video game consoles.
>> The USB Gecko is a 3rd party memory card interface adapter that provides
>> a EXI (External Interface) to USB serial converter.
> 
> The main issue here is that the gecko is supposed to be something you
> can plug or unplug no ? Is it hotswap or only at boot ? In the later
> case, maybe the node should be created by the boot wrapper when
> detecting the presence of the device. If it's hotplug, then we need
> something smarter...
> 

The usbgecko is hotplugable and hotswappable.
But as this is mostly a developer feature, not normaly used by end users, I think that we can just let it be as it is: autodetect it on boot (now probing for it instead of using information from the device tree).
If you unplug it later it causes no errors, you just miss whatever data is sent to it.

> Cheers,
> Ben.
> 

Thanks,
Albert
Benjamin Herrenschmidt - Nov. 26, 2009, 9:02 p.m.
On Thu, 2009-11-26 at 16:28 +0100, Albert Herranz wrote:
> Benjamin Herrenschmidt wrote:
> > On Sun, 2009-11-22 at 23:01 +0100, Albert Herranz wrote:
> >> Add support for using the USB Gecko adapter via the udbg facility on
> >> the Nintendo GameCube and Wii video game consoles.
> >> The USB Gecko is a 3rd party memory card interface adapter that provides
> >> a EXI (External Interface) to USB serial converter.
> > 
> > The main issue here is that the gecko is supposed to be something you
> > can plug or unplug no ? Is it hotswap or only at boot ? In the later
> > case, maybe the node should be created by the boot wrapper when
> > detecting the presence of the device. If it's hotplug, then we need
> > something smarter...
> > 
> 
> The usbgecko is hotplugable and hotswappable.
> But as this is mostly a developer feature, not normaly used by end users,
> I think that we can just let it be as it is: autodetect it on boot (now probing
> for it instead of using information from the device tree).
> If you unplug it later it causes no errors, you just miss whatever data is sent to it.

Ack. We may still went ultimately to have some kind of nice hotplug
driver for that connector and we may even have a usbgecko driver for
that things "after boot" that creates areal tty etc... but for now, just
a hard probe will do just fine.

Of course a real OF implemention is welcome to create a device node for
it if it was indeed present at boot time.

Cheers,
Ben.

> > Cheers,
> > Ben.
> > 
> 
> Thanks,
> Albert
>
Segher Boessenkool - Nov. 26, 2009, 11:12 p.m.
> The usbgecko is hotplugable and hotswappable.
> But as this is mostly a developer feature, not normaly used by end  
> users, I think that we can just let it be as it is: autodetect it  
> on boot (now probing for it instead of using information from the  
> device tree).
> If you unplug it later it causes no errors, you just miss whatever  
> data is sent to it.

You can get interrupts when an EXI device is plugged or unplugged; the
"normal" driver should use that.  But you haven't posted that yet.

The "early debug" driver can just assume the user doesn't like to hurt
himself, and provide pain when the user does, there's no problem with
that :-)


Segher

Patch

diff --git a/arch/powerpc/platforms/embedded6xx/Kconfig b/arch/powerpc/platforms/embedded6xx/Kconfig
index 31487e4..bfd88be 100644
--- a/arch/powerpc/platforms/embedded6xx/Kconfig
+++ b/arch/powerpc/platforms/embedded6xx/Kconfig
@@ -95,3 +95,16 @@  config GAMECUBE_COMMON
 	bool
 	select NOT_COHERENT_CACHE
 
+config USBGECKO_UDBG
+	bool "USB Gecko udbg console for the Nintendo GameCube/Wii"
+	depends on GAMECUBE_COMMON
+	help
+	  If you say yes to this option, support will be included for the
+	  USB Gecko adapter as an udbg console.
+	  The USB Gecko is a EXI to USB Serial converter that can be plugged
+	  into a memcard slot in the Nintendo GameCube/Wii.
+
+	  This driver bypasses the EXI layer completely.
+
+	  If in doubt, say N here.
+
diff --git a/arch/powerpc/platforms/embedded6xx/Makefile b/arch/powerpc/platforms/embedded6xx/Makefile
index 0773c08..0ab7492 100644
--- a/arch/powerpc/platforms/embedded6xx/Makefile
+++ b/arch/powerpc/platforms/embedded6xx/Makefile
@@ -7,3 +7,4 @@  obj-$(CONFIG_STORCENTER)	+= storcenter.o
 obj-$(CONFIG_PPC_HOLLY)		+= holly.o
 obj-$(CONFIG_PPC_PRPMC2800)	+= prpmc2800.o
 obj-$(CONFIG_PPC_C2K)		+= c2k.o
+obj-$(CONFIG_USBGECKO_UDBG)	+= usbgecko_udbg.o
diff --git a/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
new file mode 100644
index 0000000..49f86e8
--- /dev/null
+++ b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
@@ -0,0 +1,285 @@ 
+/*
+ * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
+ *
+ * udbg serial input/output routines for the USB Gecko adapter.
+ * Copyright (C) 2008-2009 The GameCube Linux Team
+ * Copyright (C) 2008,2009 Albert Herranz
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ */
+
+#include <asm/io.h>
+#include <asm/prom.h>
+#include <asm/udbg.h>
+
+#include <mm/mmu_decl.h>
+
+#include "usbgecko_udbg.h"
+
+
+#define EXI_CLK_32MHZ           5
+
+#define EXI_CSR                 0x00
+#define   EXI_CSR_CLKMASK       (0x7<<4)
+#define     EXI_CSR_CLK_32MHZ   (EXI_CLK_32MHZ<<4)
+#define   EXI_CSR_CSMASK        (0x7<<7)
+#define     EXI_CSR_CS_0        (0x1<<7)  /* Chip Select 001 */
+
+#define EXI_CR                  0x0c
+#define   EXI_CR_TSTART         (1<<0)
+#define   EXI_CR_WRITE		(1<<2)
+#define   EXI_CR_READ_WRITE     (2<<2)
+#define   EXI_CR_TLEN(len)      (((len)-1)<<4)
+
+#define EXI_DATA                0x10
+
+#define UG_READ_ATTEMPTS	100
+#define UG_WRITE_ATTEMPTS	100
+
+
+static void __iomem *ug_io_base;
+
+/*
+ * Performs one input/output transaction between the exi host and the usbgecko.
+ */
+static u32 ug_io_transaction(u32 in)
+{
+	u32 __iomem *csr_reg = ug_io_base + EXI_CSR;
+	u32 __iomem *data_reg = ug_io_base + EXI_DATA;
+	u32 __iomem *cr_reg = ug_io_base + EXI_CR;
+	u32 csr, data, cr;
+
+	/* select */
+	csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
+	out_be32(csr_reg, csr);
+
+	/* read/write */
+	data = in;
+	out_be32(data_reg, data);
+	cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
+	out_be32(cr_reg, cr);
+
+	while (in_be32(cr_reg) & EXI_CR_TSTART)
+		barrier();
+
+	/* deselect */
+	out_be32(csr_reg, 0);
+
+	/* result */
+	data = in_be32(data_reg);
+
+	return data;
+}
+
+/*
+ * Returns true if an usbgecko adapter is found.
+ */
+static int ug_is_adapter_present(void)
+{
+	if (!ug_io_base)
+		return 0;
+
+	return ug_io_transaction(0x90000000) == 0x04700000;
+}
+
+/*
+ * Returns true if the TX fifo is ready for transmission.
+ */
+static int ug_is_txfifo_ready(void)
+{
+	return ug_io_transaction(0xc0000000) & 0x04000000;
+}
+
+/*
+ * Tries to transmit a character.
+ * If the TX fifo is not ready the result is undefined.
+ */
+static void ug_raw_putc(char ch)
+{
+	ug_io_transaction(0xb0000000 | (ch << 20));
+}
+
+/*
+ * Transmits a character.
+ * It silently fails if the TX fifo is not ready after a number of retries.
+ */
+static void ug_putc(char ch)
+{
+	int count = UG_WRITE_ATTEMPTS;
+
+	if (!ug_io_base)
+		return;
+
+	if (ch == '\n')
+		ug_putc('\r');
+
+	while (!ug_is_txfifo_ready() && count--)
+		barrier();
+	if (count)
+		ug_raw_putc(ch);
+}
+
+#if 0
+/*
+ * Trasmits a null terminated character string.
+ */
+static void ug_puts(char *s)
+{
+	while (*s)
+		ug_putc(*s++);
+}
+#endif
+
+/*
+ * Returns true if the RX fifo is ready for transmission.
+ */
+static int ug_is_rxfifo_ready(void)
+{
+	return ug_io_transaction(0xd0000000) & 0x04000000;
+}
+
+/*
+ * Tries to receive a character.
+ * If a character is unavailable the function returns -1.
+ */
+static int ug_raw_getc(void)
+{
+	u32 data = ug_io_transaction(0xa0000000);
+	if (data & 0x08000000)
+		return (data >> 16) & 0xff;
+	else
+		return -1;
+}
+
+/*
+ * Receives a character.
+ * It fails if the RX fifo is not ready after a number of retries.
+ */
+static int ug_getc(void)
+{
+	int count = UG_READ_ATTEMPTS;
+
+	if (!ug_io_base)
+		return -1;
+
+	while (!ug_is_rxfifo_ready() && count--)
+		barrier();
+	return ug_raw_getc();
+}
+
+/*
+ * udbg functions.
+ *
+ */
+
+/*
+ * Transmits a character.
+ */
+void ug_udbg_putc(char ch)
+{
+	ug_putc(ch);
+}
+
+/*
+ * Receives a character. Waits until a character is available.
+ */
+static int ug_udbg_getc(void)
+{
+	int ch;
+
+	while ((ch = ug_getc()) == -1)
+		barrier();
+	return ch;
+}
+
+/*
+ * Receives a character. If a character is not available, returns -1.
+ */
+static int ug_udbg_getc_poll(void)
+{
+	if (!ug_is_rxfifo_ready())
+		return -1;
+	return ug_getc();
+}
+
+/*
+ * Retrieves and prepares the virtual address needed to access the hardware.
+ */
+static void __iomem *ug_udbg_setup_io_base(struct device_node *np)
+{
+	phys_addr_t paddr;
+	const unsigned int *reg;
+
+	reg = of_get_property(np, "reg", NULL);
+	if (reg) {
+		paddr = of_translate_address(np, reg);
+		if (paddr) {
+			ug_io_base = ioremap(paddr, reg[1]);
+			return ug_io_base;
+		}
+	}
+	return NULL;
+}
+
+/*
+ * USB Gecko udbg support initialization.
+ */
+void __init ug_udbg_init(void)
+{
+	struct device_node *np = NULL;
+	struct device_node *stdout;
+	const char *path;
+
+	if (!of_chosen) {
+		udbg_printf("%s: missing of_chosen\n", __func__);
+		goto done;
+	}
+
+	path = of_get_property(of_chosen, "linux,stdout-path", NULL);
+	if (!path) {
+		udbg_printf("%s: missing %s property", __func__,
+			    "linux,stdout-path");
+		goto done;
+	}
+
+	stdout = of_find_node_by_path(path);
+	if (!stdout) {
+		udbg_printf("%s: missing path %s", __func__, path);
+		goto done;
+	}
+
+	for (np = NULL;
+	    (np = of_find_compatible_node(np, NULL, "usbgecko,usbgecko"));)
+		if (np == stdout)
+			break;
+
+	of_node_put(stdout);
+	if (!np) {
+		udbg_printf("%s: stdout is not an usbgecko", __func__);
+		goto done;
+	}
+
+	if (!ug_udbg_setup_io_base(np)) {
+		udbg_printf("%s: failed to setup io base", __func__);
+		goto done;
+	}
+
+	if (!ug_is_adapter_present()) {
+		udbg_printf("usbgecko_udbg: not found\n");
+		ug_io_base = NULL;
+	} else {
+		udbg_putc = ug_udbg_putc;
+		udbg_getc = ug_udbg_getc;
+		udbg_getc_poll = ug_udbg_getc_poll;
+		udbg_printf("usbgecko_udbg: ready\n");
+	}
+
+done:
+	if (np)
+		of_node_put(np);
+	return;
+}
diff --git a/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
new file mode 100644
index 0000000..3929de3
--- /dev/null
+++ b/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
@@ -0,0 +1,30 @@ 
+/*
+ * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.h
+ *
+ * udbg serial input/output routines for the USB Gecko adapter.
+ * Copyright (C) 2008-2009 The GameCube Linux Team
+ * Copyright (C) 2008,2009 Albert Herranz
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ */
+
+#ifndef __USBGECKO_UDBG_H
+#define __USBGECKO_UDBG_H
+
+#ifdef CONFIG_USBGECKO_UDBG
+
+extern void __init ug_udbg_init(void);
+
+#else
+
+static inline void __init ug_udbg_init(void)
+{
+}
+
+#endif /* CONFIG_USBGECKO_UDBG */
+
+#endif /* __USBGECKO_UDBG_H */