Path: blob/master/arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
26489 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c3*4* udbg serial input/output routines for the USB Gecko adapter.5* Copyright (C) 2008-2009 The GameCube Linux Team6* Copyright (C) 2008,2009 Albert Herranz7*/89#include <linux/of_address.h>1011#include <mm/mmu_decl.h>1213#include <asm/io.h>14#include <asm/udbg.h>15#include <asm/fixmap.h>1617#include "usbgecko_udbg.h"181920#define EXI_CLK_32MHZ 52122#define EXI_CSR 0x0023#define EXI_CSR_CLKMASK (0x7<<4)24#define EXI_CSR_CLK_32MHZ (EXI_CLK_32MHZ<<4)25#define EXI_CSR_CSMASK (0x7<<7)26#define EXI_CSR_CS_0 (0x1<<7) /* Chip Select 001 */2728#define EXI_CR 0x0c29#define EXI_CR_TSTART (1<<0)30#define EXI_CR_WRITE (1<<2)31#define EXI_CR_READ_WRITE (2<<2)32#define EXI_CR_TLEN(len) (((len)-1)<<4)3334#define EXI_DATA 0x103536#define UG_READ_ATTEMPTS 10037#define UG_WRITE_ATTEMPTS 100383940static void __iomem *ug_io_base;4142/*43* Performs one input/output transaction between the exi host and the usbgecko.44*/45static u32 ug_io_transaction(u32 in)46{47u32 __iomem *csr_reg = ug_io_base + EXI_CSR;48u32 __iomem *data_reg = ug_io_base + EXI_DATA;49u32 __iomem *cr_reg = ug_io_base + EXI_CR;50u32 csr, data, cr;5152/* select */53csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;54out_be32(csr_reg, csr);5556/* read/write */57data = in;58out_be32(data_reg, data);59cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;60out_be32(cr_reg, cr);6162while (in_be32(cr_reg) & EXI_CR_TSTART)63barrier();6465/* deselect */66out_be32(csr_reg, 0);6768/* result */69data = in_be32(data_reg);7071return data;72}7374/*75* Returns true if an usbgecko adapter is found.76*/77static int ug_is_adapter_present(void)78{79if (!ug_io_base)80return 0;8182return ug_io_transaction(0x90000000) == 0x04700000;83}8485/*86* Returns true if the TX fifo is ready for transmission.87*/88static int ug_is_txfifo_ready(void)89{90return ug_io_transaction(0xc0000000) & 0x04000000;91}9293/*94* Tries to transmit a character.95* If the TX fifo is not ready the result is undefined.96*/97static void ug_raw_putc(char ch)98{99ug_io_transaction(0xb0000000 | (ch << 20));100}101102/*103* Transmits a character.104* It silently fails if the TX fifo is not ready after a number of retries.105*/106static void ug_putc(char ch)107{108int count = UG_WRITE_ATTEMPTS;109110if (!ug_io_base)111return;112113if (ch == '\n')114ug_putc('\r');115116while (!ug_is_txfifo_ready() && count--)117barrier();118if (count >= 0)119ug_raw_putc(ch);120}121122/*123* Returns true if the RX fifo is ready for transmission.124*/125static int ug_is_rxfifo_ready(void)126{127return ug_io_transaction(0xd0000000) & 0x04000000;128}129130/*131* Tries to receive a character.132* If a character is unavailable the function returns -1.133*/134static int ug_raw_getc(void)135{136u32 data = ug_io_transaction(0xa0000000);137if (data & 0x08000000)138return (data >> 16) & 0xff;139else140return -1;141}142143/*144* Receives a character.145* It fails if the RX fifo is not ready after a number of retries.146*/147static int ug_getc(void)148{149int count = UG_READ_ATTEMPTS;150151if (!ug_io_base)152return -1;153154while (!ug_is_rxfifo_ready() && count--)155barrier();156return ug_raw_getc();157}158159/*160* udbg functions.161*162*/163164/*165* Transmits a character.166*/167static void ug_udbg_putc(char ch)168{169ug_putc(ch);170}171172/*173* Receives a character. Waits until a character is available.174*/175static int ug_udbg_getc(void)176{177int ch;178179while ((ch = ug_getc()) == -1)180barrier();181return ch;182}183184/*185* Receives a character. If a character is not available, returns -1.186*/187static int ug_udbg_getc_poll(void)188{189if (!ug_is_rxfifo_ready())190return -1;191return ug_getc();192}193194/*195* Checks if a USB Gecko adapter is inserted in any memory card slot.196*/197static void __iomem *__init ug_udbg_probe(void __iomem *exi_io_base)198{199int i;200201/* look for a usbgecko on memcard slots A and B */202for (i = 0; i < 2; i++) {203ug_io_base = exi_io_base + 0x14 * i;204if (ug_is_adapter_present())205break;206}207if (i == 2)208ug_io_base = NULL;209return ug_io_base;210211}212213/*214* USB Gecko udbg support initialization.215*/216void __init ug_udbg_init(void)217{218struct device_node *np;219void __iomem *exi_io_base;220221if (ug_io_base)222udbg_printf("%s: early -> final\n", __func__);223224np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi");225if (!np) {226udbg_printf("%s: EXI node not found\n", __func__);227goto out;228}229230exi_io_base = of_iomap(np, 0);231if (!exi_io_base) {232udbg_printf("%s: failed to setup EXI io base\n", __func__);233goto done;234}235236if (!ug_udbg_probe(exi_io_base)) {237udbg_printf("usbgecko_udbg: not found\n");238iounmap(exi_io_base);239} else {240udbg_putc = ug_udbg_putc;241udbg_getc = ug_udbg_getc;242udbg_getc_poll = ug_udbg_getc_poll;243udbg_printf("usbgecko_udbg: ready\n");244}245246done:247of_node_put(np);248out:249return;250}251252#ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO253254static phys_addr_t __init ug_early_grab_io_addr(void)255{256#if defined(CONFIG_GAMECUBE)257return 0x0c000000;258#elif defined(CONFIG_WII)259return 0x0d000000;260#else261#error Invalid platform for USB Gecko based early debugging.262#endif263}264265/*266* USB Gecko early debug support initialization for udbg.267*/268void __init udbg_init_usbgecko(void)269{270void __iomem *early_debug_area;271void __iomem *exi_io_base;272273/*274* At this point we have a BAT already setup that enables I/O275* to the EXI hardware.276*277* The BAT uses a virtual address range reserved at the fixmap.278* This must match the virtual address configured in279* head_32.S:setup_usbgecko_bat().280*/281early_debug_area = (void __iomem *)__fix_to_virt(FIX_EARLY_DEBUG_BASE);282exi_io_base = early_debug_area + 0x00006800;283284/* try to detect a USB Gecko */285if (!ug_udbg_probe(exi_io_base))286return;287288/* we found a USB Gecko, load udbg hooks */289udbg_putc = ug_udbg_putc;290udbg_getc = ug_udbg_getc;291udbg_getc_poll = ug_udbg_getc_poll;292293/*294* Prepare again the same BAT for MMU_init.295* This allows udbg I/O to continue working after the MMU is296* turned on for real.297* It is safe to continue using the same virtual address as it is298* a reserved fixmap area.299*/300setbat(1, (unsigned long)early_debug_area,301ug_early_grab_io_addr(), 128*1024, PAGE_KERNEL_NCG);302}303304#endif /* CONFIG_PPC_EARLY_DEBUG_USBGECKO */305306307308