/* $NetBSD: machdep.c,v 1.58 2024/03/05 14:15:30 thorpej Exp $ */

/*-
* Copyright (c) 2006 Itronix Inc.
* All rights reserved.
*
* Portions written by Garrett D'Amore for Itronix Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. The name of Itronix Inc. may not be used to endorse
*    or promote products derived from this software without specific
*    prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1992, 1993
*      The Regents of the University of California.  All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department, The Mach Operating System project at
* Carnegie-Mellon University and Ralph Campbell.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
*    may be used to endorse or promote products derived from this software
*    without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*      @(#)machdep.c   8.3 (Berkeley) 1/12/94
*      from: Utah Hdr: machdep.c 1.63 91/04/24
*/

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.58 2024/03/05 14:15:30 thorpej Exp $");

#include "opt_ddb.h"
#include "opt_kgdb.h"

#include "opt_memsize.h"
#include "opt_modular.h"
#include "opt_ethaddr.h"

#include <sys/param.h>
#include <sys/boot_flag.h>
#include <sys/buf.h>
#include <sys/device.h>
#include <sys/kcore.h>
#include <sys/kernel.h>
#include <sys/ksyms.h>
#include <sys/mount.h>
#include <sys/reboot.h>
#include <sys/systm.h>
#include <sys/termios.h>
#include <sys/cpu.h>

#include <net/if.h>
#include <net/if_ether.h>

#include <uvm/uvm_extern.h>

#include <dev/cons.h>

#include "ksyms.h"

#if NKSYMS || defined(DDB) || defined(MODULAR)
#include <mips/db_machdep.h>
#include <ddb/db_extern.h>
#endif

#include <mips/cache.h>
#include <mips/locore.h>

#include <machine/yamon.h>

#include <evbmips/alchemy/board.h>

#include <mips/alchemy/dev/aupcivar.h>
#include <mips/alchemy/dev/aupcmciavar.h>
#include <mips/alchemy/dev/auspivar.h>
#include <mips/alchemy/include/aureg.h>
#include <mips/alchemy/include/auvar.h>
#include <mips/alchemy/include/aubusvar.h>

#include "com.h"
#if NCOM > 0

int     aucomcnrate = 0;
#endif /* NAUCOM > 0 */

#include "ohci.h"

/* Maps for VM objects. */
struct vm_map *phys_map = NULL;

int maxmem;                     /* max memory per process */

int mem_cluster_cnt;
phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];

yamon_env_var *yamon_envp;
struct mips_bus_space alchemy_cpuregt;

void    mach_init(int, char **, yamon_env_var *, u_long); /* XXX */

void
mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
{
       bus_space_handle_t sh;
       void *kernend;
       const char *cp;
       int freqok, howto, i;
       const struct alchemy_board *board;

       extern char edata[], end[];     /* XXX */

       board = board_info();
       KASSERT(board != NULL);

       /* clear the BSS segment */
       kernend = (void *)mips_round_page(end);
       memset(edata, 0, (char *)kernend - edata);

       /* set CPU model info for sysctl_hw */
       cpu_setmodel("%s", board->ab_name);

       /* save the yamon environment pointer */
       yamon_envp = envp;

       /* Use YAMON callbacks for early console I/O */
       cn_tab = &yamon_promcd;

       /*
        * Set up the exception vectors and CPU-specific function
        * vectors early on.  We need the wbflush() vector set up
        * before comcnattach() is called (or at least before the
        * first printf() after that is called).
        * Sets up mips_cpu_flags that may be queried by other
        * functions called during startup.
        * Also clears the I+D caches.
        */
       mips_vector_init(NULL, false);

       uvm_md_init();

       /*
        * Use YAMON's CPU frequency if available.
        */
       freqok = yamon_setcpufreq(1);

       /*
        * Initialize bus space tags.
        */
       au_cpureg_bus_mem_init(&alchemy_cpuregt, &alchemy_cpuregt);
       aubus_st = &alchemy_cpuregt;

       /*
        * Calibrate the timer if YAMON failed to tell us.
        */
       if (!freqok) {
               bus_space_map(aubus_st, PC_BASE, PC_SIZE, 0, &sh);
               au_cal_timers(aubus_st, sh);
               bus_space_unmap(aubus_st, sh, PC_SIZE);
       }

       /*
        * Perform board-specific initialization.
        */
       board->ab_init();

       /*
        * Bring up the console.
        */
#if NCOM > 0
#ifdef CONSPEED
       if (aucomcnrate == 0)
               aucomcnrate = CONSPEED;
#else /* !CONSPEED */
       /*
        * Learn default console speed.  We use the YAMON environment,
        * though we could probably also figure it out by checking the
        * aucom registers directly.
        */
       if ((aucomcnrate == 0) && ((cp = yamon_getenv("modetty0")) != NULL))
               aucomcnrate = strtoul(cp, NULL, 0);

       if (aucomcnrate == 0) {
               printf("FATAL: `modetty0' YAMON variable not set.  Set it\n");
               printf("       to the speed of the console and try again.\n");
               printf("       Or, build a kernel with the `CONSPEED' "
                   "option.\n");
               panic("mach_init");
       }
#endif /* CONSPEED */

       /*
        * Delay to allow firmware putchars to complete.
        * FIFO depth * character time.
        * character time = (1000000 / (defaultrate / 10))
        */
       delay(160000000 / aucomcnrate);
       if (com_aubus_cnattach(UART0_BASE, aucomcnrate) != 0)
               panic("mach_init: unable to initialize serial console");

#else /* NCOM > 0 */
       panic("mach_init: not configured to use serial console");
#endif /* NAUCOM > 0 */

       /*
        * Look at arguments passed to us and compute boothowto.
        */
       boothowto = RB_AUTOBOOT;
#ifdef KADB
       boothowto |= RB_KDB;
#endif
       for (i = 1; i < argc; i++) {
               for (cp = argv[i]; *cp; cp++) {
                       /* Ignore superfluous '-', if there is one */
                       if (*cp == '-')
                               continue;

                       howto = 0;
                       BOOT_FLAG(*cp, howto);
                       if (! howto)
                               printf("bootflag '%c' not recognised\n", *cp);
                       else
                               boothowto |= howto;
               }
       }

       /*
        * Determine the memory size.  Use the `memsize' PMON
        * variable.  If that's not available, panic.
        *
        * Note: Reserve the first page!  That's where the trap
        * vectors are located.
        */

#if defined(MEMSIZE)
       memsize = MEMSIZE;
#else
       if (memsize == 0) {
               if ((cp = yamon_getenv("memsize")) != NULL)
                       memsize = strtoul(cp, NULL, 0);
               else {
                       printf("FATAL: `memsize' YAMON variable not set.  Set it to\n");
                       printf("       the amount of memory (in MB) and try again.\n");
                       printf("       Or, build a kernel with the `MEMSIZE' "
                           "option.\n");
                       panic("mach_init");
               }
       }
#endif /* MEMSIZE */
       printf("Memory size: 0x%08lx\n", memsize);
       physmem = btoc(memsize);

       mem_clusters[mem_cluster_cnt].start = PAGE_SIZE;
       mem_clusters[mem_cluster_cnt].size =
           memsize - mem_clusters[mem_cluster_cnt].start;
       mem_cluster_cnt++;

       /*
        * Load the rest of the available pages into the VM system.
        */
       mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
           mem_clusters, mem_cluster_cnt, NULL, 0);

       /*
        * Initialize message buffer (at end of core).
        */
       mips_init_msgbuf();

       /*
        * Initialize the virtual memory system.
        */
       pmap_bootstrap();

       /*
        * Allocate uarea page for lwp0 and set it.
        */
       mips_init_lwp0_uarea();

       /*
        * Initialize debuggers, and break into them, if appropriate.
        */
#ifdef DDB
       if (boothowto & RB_KDB)
               Debugger();
#endif
}

void
consinit(void)
{

       /*
        * Everything related to console initialization is done
        * in mach_init().
        */
}

void
cpu_startup(void)
{
       cpu_startup_common();
}

void
cpu_reboot(int howto, char *bootstr)
{
       static int waittime = -1;
       const struct alchemy_board *board;

       /* Take a snapshot before clobbering any registers. */
       savectx(curpcb);

       board = board_info();
       KASSERT(board != NULL);

       /* If "always halt" was specified as a boot flag, obey. */
       if (boothowto & RB_HALT)
               howto |= RB_HALT;

       boothowto = howto;

       /* If system is cold, just halt. */
       if (cold) {
               boothowto |= RB_HALT;
               goto haltsys;
       }

       if ((boothowto & RB_NOSYNC) == 0 && waittime < 0) {
               waittime = 0;

               /*
                * Synchronize the disks....
                */
               vfs_shutdown();
       }

       /* Disable interrupts. */
       splhigh();

       if (boothowto & RB_DUMP)
               dumpsys();

haltsys:
       /* Run any shutdown hooks. */
       doshutdownhooks();

       pmf_system_shutdown(boothowto);

       if ((boothowto & RB_POWERDOWN) == RB_POWERDOWN)
               if (board && board->ab_poweroff)
                       board->ab_poweroff();

       /*
        * YAMON may autoboot (depending on settings), and we cannot pass
        * flags to it (at least I haven't figured out how to yet), so
        * we "pseudo-halt" now.
        */
       if (boothowto & RB_HALT) {
               printf("\n");
               printf("The operating system has halted.\n");
               printf("Please press any key to reboot.\n\n");
               cnpollc(1);     /* For proper keyboard command handling */
               cngetc();
               cnpollc(0);
       }

       printf("resetting board...\n\n");

       /*
        * Try to use board-specific reset logic, which might involve a better
        * hardware reset.
        */
       if (board->ab_reboot)
               board->ab_reboot();

#if 1
       /* XXX
        * For some reason we are leaving the ethernet MAC in a state where
        * YAMON isn't happy with it.  So just call the reset vector (grr,
        * Alchemy YAMON doesn't have a "reset" command).
        */
       mips_icache_sync_all();
       mips_dcache_wbinv_all();
       __asm volatile("jr      %0" :: "r"(MIPS_RESET_EXC_VEC));
#else
       printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
       yamon_exit(boothowto);
       printf("Oops, back from yamon_exit()\n\nSpinning...");
#endif
       for (;;)
               /* spin forever */ ;    /* XXX */
       /*NOTREACHED*/
}

/*
* Export our interrupt map function so aupci can find it.
*/
int
aupci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
{
       const struct alchemy_board *board;

       board = board_info();
       if (board->ab_pci_intr_map != NULL)
               return (board->ab_pci_intr_map(pa, ihp));
       return 1;
}

struct aupcmcia_machdep *
aupcmcia_machdep(void)
{
       const struct alchemy_board *board;

       board = board_info();
       return (board->ab_pcmcia);
}

const struct auspi_machdep *
auspi_machdep(bus_addr_t ba)
{
       const struct alchemy_board *board;

       board = board_info();
       if (board->ab_spi != NULL)
               return (board->ab_spi(ba));
       return NULL;
}