1152219Simp/*-
2152219Simp * Copyright (c) 2005 Rink Springer
3152219Simp * All rights reserved.
4152219Simp *
5152219Simp * Redistribution and use in source and binary forms, with or without
6152219Simp * modification, are permitted provided that the following conditions
7152219Simp * are met:
8152219Simp * 1. Redistributions of source code must retain the above copyright
9152219Simp *    notice, this list of conditions and the following disclaimer.
10152219Simp * 2. Redistributions in binary form must reproduce the above copyright
11152219Simp *    notice, this list of conditions and the following disclaimer in the
12152219Simp *    documentation and/or other materials provided with the distribution.
13152219Simp * 3. The name of the author may not be used to endorse or promote products
14152219Simp *    derived from this software without specific prior written permission
15152219Simp *
16152219Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17152219Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18152219Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19152219Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20152219Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21152219Simp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22152219Simp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23152219Simp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24152219Simp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25152219Simp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26152219Simp *
27152219Simp * $FreeBSD$
28152219Simp */
29152219Simp#include <sys/param.h>
30155288Srink#include <sys/systm.h>
31152219Simp#include <sys/kernel.h>
32152219Simp#include <sys/eventhandler.h>
33152219Simp#include <sys/reboot.h>
34152219Simp#include <machine/xbox.h>
35155288Srink#include <vm/vm.h>
36155288Srink#include <vm/pmap.h>
37152219Simp
38152219Simp#ifndef I686_CPU
39161340Spav#error You must have a I686_CPU in your kernel if you want to make an XBOX-compatible kernel
40152219Simp#endif
41152219Simp
42152219Simpstatic void
43152219Simpxbox_poweroff(void* junk, int howto)
44152219Simp{
45152219Simp	if (!(howto & RB_POWEROFF))
46152219Simp		return;
47152219Simp
48152219Simp	pic16l_poweroff();
49152219Simp}
50152219Simp
51152219Simpstatic void
52152219Simpxbox_init(void)
53152219Simp{
54155288Srink	char* ptr;
55155288Srink
56152219Simp	if (!arch_i386_is_xbox)
57152219Simp		return;
58152219Simp
59152219Simp	/* register our poweroff function */
60152219Simp	EVENTHANDLER_REGISTER (shutdown_final, xbox_poweroff, NULL,
61152219Simp	                       SHUTDOWN_PRI_LAST);
62155288Srink
63155288Srink	/*
64155288Srink	 * Some XBOX loaders, such as Cromwell, have a flaw which cause the
65155288Srink	 * nve(4) driver to fail attaching to the NIC.
66155288Srink	 *
67155288Srink	 * This is because they leave the NIC running; this will cause the
68155288Srink	 * Nvidia driver to fail as the NIC does not return any sensible
69155288Srink	 * values and thus fails attaching (using an error 0x5, this means
70155288Srink	 * it cannot find a valid PHY)
71155288Srink	 *
72155288Srink	 * We bluntly tell the NIC to stop whatever it's doing; this makes
73155288Srink	 * nve(4) attach correctly. As the NIC always resides at
74155288Srink	 * 0xfef00000-0xfef003ff on an XBOX, we simply hardcode this address.
75155288Srink	 */
76155288Srink	ptr = pmap_mapdev (0xfef00000, 0x400);
77155288Srink	*(uint32_t*)(ptr + 0x188) = 0; /* clear adapter control field */
78155288Srink	pmap_unmapdev ((vm_offset_t)ptr, 0x400);
79152219Simp}
80152219Simp
81155288Srink/*
82155288Srink * This must be called before the drivers, as the if_nve(4) driver will fail
83155288Srink * if we do not do this in advance.
84155288Srink */
85177253SrwatsonSYSINIT(xbox, SI_SUB_DRIVERS, SI_ORDER_FIRST, xbox_init, NULL);
86