cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

usbgecko_udbg.c (6675B)


      1// SPDX-License-Identifier: GPL-2.0-or-later
      2/*
      3 * arch/powerpc/platforms/embedded6xx/usbgecko_udbg.c
      4 *
      5 * udbg serial input/output routines for the USB Gecko adapter.
      6 * Copyright (C) 2008-2009 The GameCube Linux Team
      7 * Copyright (C) 2008,2009 Albert Herranz
      8 */
      9
     10#include <linux/of_address.h>
     11
     12#include <mm/mmu_decl.h>
     13
     14#include <asm/io.h>
     15#include <asm/udbg.h>
     16#include <asm/fixmap.h>
     17
     18#include "usbgecko_udbg.h"
     19
     20
     21#define EXI_CLK_32MHZ           5
     22
     23#define EXI_CSR                 0x00
     24#define   EXI_CSR_CLKMASK       (0x7<<4)
     25#define     EXI_CSR_CLK_32MHZ   (EXI_CLK_32MHZ<<4)
     26#define   EXI_CSR_CSMASK        (0x7<<7)
     27#define     EXI_CSR_CS_0        (0x1<<7)  /* Chip Select 001 */
     28
     29#define EXI_CR                  0x0c
     30#define   EXI_CR_TSTART         (1<<0)
     31#define   EXI_CR_WRITE		(1<<2)
     32#define   EXI_CR_READ_WRITE     (2<<2)
     33#define   EXI_CR_TLEN(len)      (((len)-1)<<4)
     34
     35#define EXI_DATA                0x10
     36
     37#define UG_READ_ATTEMPTS	100
     38#define UG_WRITE_ATTEMPTS	100
     39
     40
     41static void __iomem *ug_io_base;
     42
     43/*
     44 * Performs one input/output transaction between the exi host and the usbgecko.
     45 */
     46static u32 ug_io_transaction(u32 in)
     47{
     48	u32 __iomem *csr_reg = ug_io_base + EXI_CSR;
     49	u32 __iomem *data_reg = ug_io_base + EXI_DATA;
     50	u32 __iomem *cr_reg = ug_io_base + EXI_CR;
     51	u32 csr, data, cr;
     52
     53	/* select */
     54	csr = EXI_CSR_CLK_32MHZ | EXI_CSR_CS_0;
     55	out_be32(csr_reg, csr);
     56
     57	/* read/write */
     58	data = in;
     59	out_be32(data_reg, data);
     60	cr = EXI_CR_TLEN(2) | EXI_CR_READ_WRITE | EXI_CR_TSTART;
     61	out_be32(cr_reg, cr);
     62
     63	while (in_be32(cr_reg) & EXI_CR_TSTART)
     64		barrier();
     65
     66	/* deselect */
     67	out_be32(csr_reg, 0);
     68
     69	/* result */
     70	data = in_be32(data_reg);
     71
     72	return data;
     73}
     74
     75/*
     76 * Returns true if an usbgecko adapter is found.
     77 */
     78static int ug_is_adapter_present(void)
     79{
     80	if (!ug_io_base)
     81		return 0;
     82
     83	return ug_io_transaction(0x90000000) == 0x04700000;
     84}
     85
     86/*
     87 * Returns true if the TX fifo is ready for transmission.
     88 */
     89static int ug_is_txfifo_ready(void)
     90{
     91	return ug_io_transaction(0xc0000000) & 0x04000000;
     92}
     93
     94/*
     95 * Tries to transmit a character.
     96 * If the TX fifo is not ready the result is undefined.
     97 */
     98static void ug_raw_putc(char ch)
     99{
    100	ug_io_transaction(0xb0000000 | (ch << 20));
    101}
    102
    103/*
    104 * Transmits a character.
    105 * It silently fails if the TX fifo is not ready after a number of retries.
    106 */
    107static void ug_putc(char ch)
    108{
    109	int count = UG_WRITE_ATTEMPTS;
    110
    111	if (!ug_io_base)
    112		return;
    113
    114	if (ch == '\n')
    115		ug_putc('\r');
    116
    117	while (!ug_is_txfifo_ready() && count--)
    118		barrier();
    119	if (count >= 0)
    120		ug_raw_putc(ch);
    121}
    122
    123/*
    124 * Returns true if the RX fifo is ready for transmission.
    125 */
    126static int ug_is_rxfifo_ready(void)
    127{
    128	return ug_io_transaction(0xd0000000) & 0x04000000;
    129}
    130
    131/*
    132 * Tries to receive a character.
    133 * If a character is unavailable the function returns -1.
    134 */
    135static int ug_raw_getc(void)
    136{
    137	u32 data = ug_io_transaction(0xa0000000);
    138	if (data & 0x08000000)
    139		return (data >> 16) & 0xff;
    140	else
    141		return -1;
    142}
    143
    144/*
    145 * Receives a character.
    146 * It fails if the RX fifo is not ready after a number of retries.
    147 */
    148static int ug_getc(void)
    149{
    150	int count = UG_READ_ATTEMPTS;
    151
    152	if (!ug_io_base)
    153		return -1;
    154
    155	while (!ug_is_rxfifo_ready() && count--)
    156		barrier();
    157	return ug_raw_getc();
    158}
    159
    160/*
    161 * udbg functions.
    162 *
    163 */
    164
    165/*
    166 * Transmits a character.
    167 */
    168static void ug_udbg_putc(char ch)
    169{
    170	ug_putc(ch);
    171}
    172
    173/*
    174 * Receives a character. Waits until a character is available.
    175 */
    176static int ug_udbg_getc(void)
    177{
    178	int ch;
    179
    180	while ((ch = ug_getc()) == -1)
    181		barrier();
    182	return ch;
    183}
    184
    185/*
    186 * Receives a character. If a character is not available, returns -1.
    187 */
    188static int ug_udbg_getc_poll(void)
    189{
    190	if (!ug_is_rxfifo_ready())
    191		return -1;
    192	return ug_getc();
    193}
    194
    195/*
    196 * Retrieves and prepares the virtual address needed to access the hardware.
    197 */
    198static void __iomem *__init ug_udbg_setup_exi_io_base(struct device_node *np)
    199{
    200	void __iomem *exi_io_base = NULL;
    201	phys_addr_t paddr;
    202	const unsigned int *reg;
    203
    204	reg = of_get_property(np, "reg", NULL);
    205	if (reg) {
    206		paddr = of_translate_address(np, reg);
    207		if (paddr)
    208			exi_io_base = ioremap(paddr, reg[1]);
    209	}
    210	return exi_io_base;
    211}
    212
    213/*
    214 * Checks if a USB Gecko adapter is inserted in any memory card slot.
    215 */
    216static void __iomem *__init ug_udbg_probe(void __iomem *exi_io_base)
    217{
    218	int i;
    219
    220	/* look for a usbgecko on memcard slots A and B */
    221	for (i = 0; i < 2; i++) {
    222		ug_io_base = exi_io_base + 0x14 * i;
    223		if (ug_is_adapter_present())
    224			break;
    225	}
    226	if (i == 2)
    227		ug_io_base = NULL;
    228	return ug_io_base;
    229
    230}
    231
    232/*
    233 * USB Gecko udbg support initialization.
    234 */
    235void __init ug_udbg_init(void)
    236{
    237	struct device_node *np;
    238	void __iomem *exi_io_base;
    239
    240	if (ug_io_base)
    241		udbg_printf("%s: early -> final\n", __func__);
    242
    243	np = of_find_compatible_node(NULL, NULL, "nintendo,flipper-exi");
    244	if (!np) {
    245		udbg_printf("%s: EXI node not found\n", __func__);
    246		goto out;
    247	}
    248
    249	exi_io_base = ug_udbg_setup_exi_io_base(np);
    250	if (!exi_io_base) {
    251		udbg_printf("%s: failed to setup EXI io base\n", __func__);
    252		goto done;
    253	}
    254
    255	if (!ug_udbg_probe(exi_io_base)) {
    256		udbg_printf("usbgecko_udbg: not found\n");
    257		iounmap(exi_io_base);
    258	} else {
    259		udbg_putc = ug_udbg_putc;
    260		udbg_getc = ug_udbg_getc;
    261		udbg_getc_poll = ug_udbg_getc_poll;
    262		udbg_printf("usbgecko_udbg: ready\n");
    263	}
    264
    265done:
    266	of_node_put(np);
    267out:
    268	return;
    269}
    270
    271#ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
    272
    273static phys_addr_t __init ug_early_grab_io_addr(void)
    274{
    275#if defined(CONFIG_GAMECUBE)
    276	return 0x0c000000;
    277#elif defined(CONFIG_WII)
    278	return 0x0d000000;
    279#else
    280#error Invalid platform for USB Gecko based early debugging.
    281#endif
    282}
    283
    284/*
    285 * USB Gecko early debug support initialization for udbg.
    286 */
    287void __init udbg_init_usbgecko(void)
    288{
    289	void __iomem *early_debug_area;
    290	void __iomem *exi_io_base;
    291
    292	/*
    293	 * At this point we have a BAT already setup that enables I/O
    294	 * to the EXI hardware.
    295	 *
    296	 * The BAT uses a virtual address range reserved at the fixmap.
    297	 * This must match the virtual address configured in
    298	 * head_32.S:setup_usbgecko_bat().
    299	 */
    300	early_debug_area = (void __iomem *)__fix_to_virt(FIX_EARLY_DEBUG_BASE);
    301	exi_io_base = early_debug_area + 0x00006800;
    302
    303	/* try to detect a USB Gecko */
    304	if (!ug_udbg_probe(exi_io_base))
    305		return;
    306
    307	/* we found a USB Gecko, load udbg hooks */
    308	udbg_putc = ug_udbg_putc;
    309	udbg_getc = ug_udbg_getc;
    310	udbg_getc_poll = ug_udbg_getc_poll;
    311
    312	/*
    313	 * Prepare again the same BAT for MMU_init.
    314	 * This allows udbg I/O to continue working after the MMU is
    315	 * turned on for real.
    316	 * It is safe to continue using the same virtual address as it is
    317	 * a reserved fixmap area.
    318	 */
    319	setbat(1, (unsigned long)early_debug_area,
    320	       ug_early_grab_io_addr(), 128*1024, PAGE_KERNEL_NCG);
    321}
    322
    323#endif /* CONFIG_PPC_EARLY_DEBUG_USBGECKO */
    324