11.5Sriastrad/*	$NetBSD: cpu.c,v 1.5 2022/03/03 06:27:40 riastradh Exp $	*/
21.1Smacallan
31.1Smacallan/*
41.1Smacallan * Copyright 2002 Wasabi Systems, Inc.
51.1Smacallan * All rights reserved.
61.1Smacallan *
71.1Smacallan * Written by Simon Burge for Wasabi Systems, Inc.
81.1Smacallan *
91.1Smacallan * Redistribution and use in source and binary forms, with or without
101.1Smacallan * modification, are permitted provided that the following conditions
111.1Smacallan * are met:
121.1Smacallan * 1. Redistributions of source code must retain the above copyright
131.1Smacallan *    notice, this list of conditions and the following disclaimer.
141.1Smacallan * 2. Redistributions in binary form must reproduce the above copyright
151.1Smacallan *    notice, this list of conditions and the following disclaimer in the
161.1Smacallan *    documentation and/or other materials provided with the distribution.
171.1Smacallan * 3. All advertising materials mentioning features or use of this software
181.1Smacallan *    must display the following acknowledgement:
191.1Smacallan *      This product includes software developed for the NetBSD Project by
201.1Smacallan *      Wasabi Systems, Inc.
211.1Smacallan * 4. The name of Wasabi Systems, Inc. may not be used to endorse
221.1Smacallan *    or promote products derived from this software without specific prior
231.1Smacallan *    written permission.
241.1Smacallan *
251.1Smacallan * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
261.1Smacallan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
271.1Smacallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
281.1Smacallan * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
291.1Smacallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
301.1Smacallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
311.1Smacallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
321.1Smacallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
331.1Smacallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
341.1Smacallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
351.1Smacallan * POSSIBILITY OF SUCH DAMAGE.
361.1Smacallan */
371.1Smacallan
381.1Smacallan#include <sys/cdefs.h>
391.5Sriastrad__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.5 2022/03/03 06:27:40 riastradh Exp $");
401.2Sskrll
411.2Sskrll#include "opt_ingenic.h"
421.2Sskrll#include "opt_multiprocessor.h"
431.1Smacallan
441.1Smacallan#include <sys/param.h>
451.1Smacallan#include <sys/device.h>
461.1Smacallan#include <sys/systm.h>
471.1Smacallan#include <sys/cpu.h>
481.1Smacallan
491.1Smacallan#include <mips/locore.h>
501.4Sskrll#include <mips/ingenic/ingenic_coreregs.h>
511.1Smacallan#include <mips/ingenic/ingenic_regs.h>
521.4Sskrll#include <mips/ingenic/ingenic_var.h>
531.1Smacallan
541.1Smacallanstatic int	cpu_match(device_t, cfdata_t, void *);
551.1Smacallanstatic void	cpu_attach(device_t, device_t, void *);
561.1Smacallan
571.1SmacallanCFATTACH_DECL_NEW(cpu, 0,
581.1Smacallan    cpu_match, cpu_attach, NULL, NULL);
591.1Smacallan
601.1Smacallanstruct cpu_info *startup_cpu_info;
611.1Smacallanextern void *ingenic_wakeup;
621.1Smacallan
631.1Smacallanstatic int
641.1Smacallancpu_match(device_t parent, cfdata_t match, void *aux)
651.1Smacallan{
661.1Smacallan	struct mainbusdev {
671.1Smacallan		const char *md_name;
681.1Smacallan	} *aa = aux;
691.1Smacallan	if (strcmp(aa->md_name, "cpu") == 0) return 1;
701.1Smacallan	return 0;
711.1Smacallan}
721.1Smacallan
731.1Smacallanstatic void
741.1Smacallancpu_attach(device_t parent, device_t self, void *aux)
751.1Smacallan{
761.1Smacallan	struct cpu_info *ci = curcpu();
771.1Smacallan	int unit;
781.1Smacallan
791.1Smacallan	if ((unit = device_unit(self)) > 0) {
801.1Smacallan#ifdef MULTIPROCESSOR
811.1Smacallan		uint32_t vec, reg;
821.1Smacallan		int bail = 10000;
831.3Sskrll
841.1Smacallan		startup_cpu_info = cpu_info_alloc(NULL, unit, 0, unit, 0);
851.1Smacallan		startup_cpu_info->ci_cpu_freq = ci->ci_cpu_freq;
861.1Smacallan		ci = startup_cpu_info;
871.1Smacallan		wbflush();
881.1Smacallan		vec = (uint32_t)&ingenic_wakeup;
891.4Sskrll		reg = mips_cp0_corereim_read();
901.1Smacallan		reg &= ~REIM_ENTRY_M;
911.1Smacallan		reg |= vec;
921.4Sskrll		mips_cp0_corereim_write(reg);
931.4Sskrll
941.4Sskrll		reg = mips_cp0_corectrl_read();
951.1Smacallan		reg |= CC_RPC1;		/* use our exception vector */
961.1Smacallan		reg &= ~CC_SW_RST1;	/* get core 1 out of reset */
971.4Sskrll		mips_cp0_corectrl_write(reg);
981.4Sskrll
991.1Smacallan		while ((!kcpuset_isset(cpus_hatched, cpu_index(startup_cpu_info))) && (bail > 0)) {
1001.1Smacallan			delay(1000);
1011.1Smacallan			bail--;
1021.1Smacallan		}
1031.1Smacallan		if (!kcpuset_isset(cpus_hatched, cpu_index(startup_cpu_info))) {
1041.1Smacallan			aprint_error_dev(self, "did not hatch\n");
1051.1Smacallan			return;
1061.1Smacallan		}
1071.1Smacallan#else
1081.1Smacallan		aprint_normal_dev(self,
1091.1Smacallan		    "processor off-line; "
1101.1Smacallan		    "multiprocessor support not present in kernel\n");
1111.1Smacallan		return;
1121.1Smacallan#endif
1131.1Smacallan
1141.1Smacallan	}
1151.1Smacallan	ci->ci_dev = self;
1161.5Sriastrad	device_set_private(self, ci);
1171.1Smacallan
1181.1Smacallan	aprint_normal(": %lu.%02luMHz (hz cycles = %lu, delay divisor = %lu)\n",
1191.1Smacallan	    ci->ci_cpu_freq / 1000000,
1201.1Smacallan	    (ci->ci_cpu_freq % 1000000) / 10000,
1211.1Smacallan	    ci->ci_cycles_per_hz, ci->ci_divisor_delay);
1221.1Smacallan
1231.1Smacallan	aprint_normal_dev(self, "");
1241.1Smacallan	cpu_identify(self);
1251.1Smacallan	cpu_attach_common(self, ci);
1261.1Smacallan}
127