Path: blob/master/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
10819 views
/*1* arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c2*3* udbg serial input/output routines for the USB Gecko adapter.4* Copyright (C) 2008-2009 The GameCube Linux Team5* Copyright (C) 2008,2009 Albert Herranz6*7* This program is free software; you can redistribute it and/or8* modify it under the terms of the GNU General Public License9* as published by the Free Software Foundation; either version 210* of the License, or (at your option) any later version.11*12*/1314#include <mm/mmu_decl.h>1516#include <asm/io.h>17#include <asm/prom.h>18#include <asm/udbg.h>19#include <asm/fixmap.h>2021#include "usbgecko_udbg.h"222324#define EXI_CLK_32MHZ 52526#define EXI_CSR 0x0027#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 */3132#define EXI_CR 0x0c33#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)3738#define EXI_DATA 0x103940#define UG_READ_ATTEMPTS 10041#define UG_WRITE_ATTEMPTS 100424344static void __iomem *ug_io_base;4546/*47* Performs one input/output transaction between the exi host and the usbgecko.48*/49static u32 ug_io_transaction(u32 in)50{51u32 __iomem *csr_reg = ug_io_base + EXI_CSR;52u32 __iomem *data_reg = ug_io_base + EXI_DATA;53u32 __iomem *cr_reg = ug_io_base + EXI_CR;54u32 csr, data, cr;5556/* select */57csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;58out_be32(csr_reg, csr);5960/* read/write */61data = in;62out_be32(data_reg, data);63cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;64out_be32(cr_reg, cr);6566while (in_be32(cr_reg) & EXI_CR_TSTART)67barrier();6869/* deselect */70out_be32(csr_reg, 0);7172/* result */73data = in_be32(data_reg);7475return data;76}7778/*79* Returns true if an usbgecko adapter is found.80*/81static int ug_is_adapter_present(void)82{83if (!ug_io_base)84return 0;8586return ug_io_transaction(0x90000000) == 0x04700000;87}8889/*90* Returns true if the TX fifo is ready for transmission.91*/92static int ug_is_txfifo_ready(void)93{94return ug_io_transaction(0xc0000000) & 0x04000000;95}9697/*98* Tries to transmit a character.99* If the TX fifo is not ready the result is undefined.100*/101static void ug_raw_putc(char ch)102{103ug_io_transaction(0xb0000000 | (ch << 20));104}105106/*107* Transmits a character.108* It silently fails if the TX fifo is not ready after a number of retries.109*/110static void ug_putc(char ch)111{112int count = UG_WRITE_ATTEMPTS;113114if (!ug_io_base)115return;116117if (ch == '\n')118ug_putc('\r');119120while (!ug_is_txfifo_ready() && count--)121barrier();122if (count >= 0)123ug_raw_putc(ch);124}125126/*127* Returns true if the RX fifo is ready for transmission.128*/129static int ug_is_rxfifo_ready(void)130{131return ug_io_transaction(0xd0000000) & 0x04000000;132}133134/*135* Tries to receive a character.136* If a character is unavailable the function returns -1.137*/138static int ug_raw_getc(void)139{140u32 data = ug_io_transaction(0xa0000000);141if (data & 0x08000000)142return (data >> 16) & 0xff;143else144return -1;145}146147/*148* Receives a character.149* It fails if the RX fifo is not ready after a number of retries.150*/151static int ug_getc(void)152{153int count = UG_READ_ATTEMPTS;154155if (!ug_io_base)156return -1;157158while (!ug_is_rxfifo_ready() && count--)159barrier();160return ug_raw_getc();161}162163/*164* udbg functions.165*166*/167168/*169* Transmits a character.170*/171void ug_udbg_putc(char ch)172{173ug_putc(ch);174}175176/*177* Receives a character. Waits until a character is available.178*/179static int ug_udbg_getc(void)180{181int ch;182183while ((ch = ug_getc()) == -1)184barrier();185return ch;186}187188/*189* Receives a character. If a character is not available, returns -1.190*/191static int ug_udbg_getc_poll(void)192{193if (!ug_is_rxfifo_ready())194return -1;195return ug_getc();196}197198/*199* Retrieves and prepares the virtual address needed to access the hardware.200*/201static void __iomem *ug_udbg_setup_exi_io_base(struct device_node *np)202{203void __iomem *exi_io_base = NULL;204phys_addr_t paddr;205const unsigned int *reg;206207reg = of_get_property(np, "reg", NULL);208if (reg) {209paddr = of_translate_address(np, reg);210if (paddr)211exi_io_base = ioremap(paddr, reg[1]);212}213return exi_io_base;214}215216/*217* Checks if a USB Gecko adapter is inserted in any memory card slot.218*/219static void __iomem *ug_udbg_probe(void __iomem *exi_io_base)220{221int i;222223/* look for a usbgecko on memcard slots A and B */224for (i = 0; i < 2; i++) {225ug_io_base = exi_io_base + 0x14 * i;226if (ug_is_adapter_present())227break;228}229if (i == 2)230ug_io_base = NULL;231return ug_io_base;232233}234235/*236* USB Gecko udbg support initialization.237*/238void __init ug_udbg_init(void)239{240struct device_node *np;241void __iomem *exi_io_base;242243if (ug_io_base)244udbg_printf("%s: early -> final\n", __func__);245246np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi");247if (!np) {248udbg_printf("%s: EXI node not found\n", __func__);249goto done;250}251252exi_io_base = ug_udbg_setup_exi_io_base(np);253if (!exi_io_base) {254udbg_printf("%s: failed to setup EXI io base\n", __func__);255goto done;256}257258if (!ug_udbg_probe(exi_io_base)) {259udbg_printf("usbgecko_udbg: not found\n");260iounmap(exi_io_base);261} else {262udbg_putc = ug_udbg_putc;263udbg_getc = ug_udbg_getc;264udbg_getc_poll = ug_udbg_getc_poll;265udbg_printf("usbgecko_udbg: ready\n");266}267268done:269if (np)270of_node_put(np);271return;272}273274#ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO275276static phys_addr_t __init ug_early_grab_io_addr(void)277{278#if defined(CONFIG_GAMECUBE)279return 0x0c000000;280#elif defined(CONFIG_WII)281return 0x0d000000;282#else283#error Invalid platform for USB Gecko based early debugging.284#endif285}286287/*288* USB Gecko early debug support initialization for udbg.289*/290void __init udbg_init_usbgecko(void)291{292void __iomem *early_debug_area;293void __iomem *exi_io_base;294295/*296* At this point we have a BAT already setup that enables I/O297* to the EXI hardware.298*299* The BAT uses a virtual address range reserved at the fixmap.300* This must match the virtual address configured in301* head_32.S:setup_usbgecko_bat().302*/303early_debug_area = (void __iomem *)__fix_to_virt(FIX_EARLY_DEBUG_BASE);304exi_io_base = early_debug_area + 0x00006800;305306/* try to detect a USB Gecko */307if (!ug_udbg_probe(exi_io_base))308return;309310/* we found a USB Gecko, load udbg hooks */311udbg_putc = ug_udbg_putc;312udbg_getc = ug_udbg_getc;313udbg_getc_poll = ug_udbg_getc_poll;314315/*316* Prepare again the same BAT for MMU_init.317* This allows udbg I/O to continue working after the MMU is318* turned on for real.319* It is safe to continue using the same virtual address as it is320* a reserved fixmap area.321*/322setbat(1, (unsigned long)early_debug_area,323ug_early_grab_io_addr(), 128*1024, PAGE_KERNEL_NCG);324}325326#endif /* CONFIG_PPC_EARLY_DEBUG_USBGECKO */327328329330