Home | History | Annotate | Line # | Download | only in cavium
      1  1.2  andvar /*	$NetBSD: octeon_misc.c,v 1.2 2022/02/06 20:20:19 andvar Exp $	*/
      2  1.1  simonb 
      3  1.1  simonb /*-
      4  1.1  simonb  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.1  simonb  * All rights reserved.
      6  1.1  simonb  *
      7  1.1  simonb  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  simonb  * by Simon Burge.
      9  1.1  simonb  *
     10  1.1  simonb  * Redistribution and use in source and binary forms, with or without
     11  1.1  simonb  * modification, are permitted provided that the following conditions
     12  1.1  simonb  * are met:
     13  1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     14  1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     15  1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  simonb  *    documentation and/or other materials provided with the distribution.
     18  1.1  simonb  *
     19  1.1  simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  simonb  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  simonb  */
     31  1.1  simonb 
     32  1.1  simonb /*
     33  1.1  simonb  * Copyright (c) 2009, 2010 Miodrag Vallat.
     34  1.1  simonb  * Copyright (c) 2019 Visa Hankala.
     35  1.1  simonb  *
     36  1.1  simonb  * Permission to use, copy, modify, and distribute this software for any
     37  1.1  simonb  * purpose with or without fee is hereby granted, provided that the above
     38  1.1  simonb  * copyright notice and this permission notice appear in all copies.
     39  1.1  simonb  *
     40  1.1  simonb  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     41  1.1  simonb  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     42  1.1  simonb  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     43  1.1  simonb  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     44  1.1  simonb  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     45  1.1  simonb  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     46  1.1  simonb  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     47  1.1  simonb  */
     48  1.1  simonb /*
     49  1.1  simonb  * Copyright (c) 2003-2004 Opsycon AB  (www.opsycon.se / www.opsycon.com)
     50  1.1  simonb  *
     51  1.1  simonb  * Redistribution and use in source and binary forms, with or without
     52  1.1  simonb  * modification, are permitted provided that the following conditions
     53  1.1  simonb  * are met:
     54  1.1  simonb  * 1. Redistributions of source code must retain the above copyright
     55  1.1  simonb  *    notice, this list of conditions and the following disclaimer.
     56  1.1  simonb  * 2. Redistributions in binary form must reproduce the above copyright
     57  1.1  simonb  *    notice, this list of conditions and the following disclaimer in the
     58  1.1  simonb  *    documentation and/or other materials provided with the distribution.
     59  1.1  simonb  *
     60  1.1  simonb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     61  1.1  simonb  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     62  1.1  simonb  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  1.1  simonb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     64  1.1  simonb  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  1.1  simonb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  1.1  simonb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  1.1  simonb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  1.1  simonb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  1.1  simonb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  1.1  simonb  * SUCH DAMAGE.
     71  1.1  simonb  *
     72  1.1  simonb  */
     73  1.1  simonb 
     74  1.1  simonb #include <sys/cdefs.h>
     75  1.2  andvar __KERNEL_RCSID(0, "$NetBSD: octeon_misc.c,v 1.2 2022/02/06 20:20:19 andvar Exp $");
     76  1.1  simonb 
     77  1.1  simonb #include <sys/param.h>
     78  1.1  simonb 
     79  1.1  simonb #include <mips/cavium/octeonreg.h>
     80  1.1  simonb #include <mips/cavium/octeonvar.h>
     81  1.1  simonb #include <mips/cavium/dev/octeon_ciureg.h>
     82  1.1  simonb 
     83  1.1  simonb int	octeon_core_ver;
     84  1.1  simonb 
     85  1.1  simonb /*
     86  1.1  simonb  * Return the name of the CPU model we are running on.
     87  1.1  simonb  * Side effect: sets the octeon_core_ver variable.
     88  1.1  simonb  */
     89  1.1  simonb const char *
     90  1.1  simonb octeon_cpu_model(mips_prid_t cpu_id)
     91  1.1  simonb {
     92  1.1  simonb 	const uint64_t fuse = octeon_xkphys_read_8(CIU_FUSE);
     93  1.1  simonb 	const int numcores = popcount64(fuse);
     94  1.1  simonb 	const int clock_mhz = curcpu()->ci_cpu_freq / 1000000;
     95  1.1  simonb 	const char *family;
     96  1.1  simonb 	const char *coremodel;
     97  1.1  simonb 	bool tested;
     98  1.1  simonb 	static char buf[sizeof("CNnnXX-NNNN (unverified)")] = {};
     99  1.1  simonb 
    100  1.1  simonb 	if (buf[0] != 0)
    101  1.1  simonb 		return buf;		/* we've been here before */
    102  1.1  simonb 
    103  1.1  simonb 	/*
    104  1.1  simonb 	 * Don't print "pass X.Y", but if needed:
    105  1.1  simonb 	 *	passhi = ((cpu_id >> 3) & 7) + '0';
    106  1.1  simonb 	 *	passlo = (cpu_id & 7) + '0';
    107  1.1  simonb 	 * Note some chips use different representation for the pass number.
    108  1.1  simonb 	 */
    109  1.1  simonb 
    110  1.1  simonb #ifdef OCTEON_DEBUG
    111  1.1  simonb 	printf("cpuid = 0x%x\n", cpu_id);
    112  1.1  simonb #endif
    113  1.1  simonb 
    114  1.1  simonb 	switch (numcores) {
    115  1.1  simonb 	case  1: coremodel = "10"; break;
    116  1.1  simonb 	case  2: coremodel = "20"; break;
    117  1.1  simonb 	case  3: coremodel = "25"; break;
    118  1.1  simonb 	case  4: coremodel = "30"; break;
    119  1.1  simonb 	case  5: coremodel = "32"; break;
    120  1.1  simonb 	case  6: coremodel = "34"; break;
    121  1.1  simonb 	case  7: coremodel = "38"; break;
    122  1.1  simonb 	case  8: coremodel = "40"; break;
    123  1.1  simonb 	case  9: coremodel = "42"; break;
    124  1.1  simonb 	case 10: coremodel = "45"; break;
    125  1.1  simonb 	case 11: coremodel = "48"; break;
    126  1.1  simonb 	case 12: coremodel = "50"; break;
    127  1.1  simonb 	case 13: coremodel = "52"; break;
    128  1.1  simonb 	case 14: coremodel = "55"; break;
    129  1.1  simonb 	case 15: coremodel = "58"; break;
    130  1.1  simonb 	case 16: coremodel = "60"; break;
    131  1.1  simonb 	case 24: coremodel = "70"; break;
    132  1.1  simonb 	case 32: coremodel = "80"; break;
    133  1.1  simonb 	case 40: coremodel = "85"; break;
    134  1.1  simonb 	case 44: coremodel = "88"; break;
    135  1.1  simonb 	case 48: coremodel = "90"; break;
    136  1.1  simonb 	default:
    137  1.1  simonb 		coremodel = "XX"; break;
    138  1.1  simonb 	}
    139  1.1  simonb 
    140  1.1  simonb 	/*
    141  1.1  simonb          * Assume all CPU families haven't been tested unless explicitly
    142  1.1  simonb          * noted.  Sources of extra information for determining actual
    143  1.1  simonb          * CPU models include chip documentation and U-Boot source code.
    144  1.1  simonb 	 */
    145  1.1  simonb 	tested = false;
    146  1.1  simonb 
    147  1.1  simonb 	switch (MIPS_PRID_IMPL(cpu_id)) {
    148  1.1  simonb 	/* the order of these cases is the numeric value of MIPS_CNnnXX */
    149  1.1  simonb 	case MIPS_CN38XX:
    150  1.1  simonb 		family = "38";	/* XXX may also be family "36" or "37" */
    151  1.1  simonb 		octeon_core_ver = OCTEON_1;
    152  1.1  simonb 		break;
    153  1.1  simonb 	case MIPS_CN31XX:
    154  1.1  simonb 		family = "31";	/* XXX may also be model "3020" */
    155  1.1  simonb 		octeon_core_ver = OCTEON_1;
    156  1.1  simonb 		break;
    157  1.1  simonb 	case MIPS_CN30XX:
    158  1.1  simonb 		family = "30";	/* XXX half cache model is "3005" */
    159  1.1  simonb 		octeon_core_ver = OCTEON_1;
    160  1.1  simonb 		break;
    161  1.1  simonb 	case MIPS_CN58XX:
    162  1.1  simonb 		family = "58";
    163  1.1  simonb 		octeon_core_ver = OCTEON_PLUS;
    164  1.1  simonb 		break;
    165  1.1  simonb 	case MIPS_CN56XX:
    166  1.1  simonb 		family = "56";	/* XXX may also be family "54", "55" or "57" */
    167  1.1  simonb 		octeon_core_ver = OCTEON_PLUS;
    168  1.1  simonb 		break;
    169  1.1  simonb 	case MIPS_CN50XX:
    170  1.1  simonb 		family = "50";
    171  1.1  simonb 		octeon_core_ver = OCTEON_PLUS;
    172  1.1  simonb 		tested = true;
    173  1.1  simonb 		break;
    174  1.1  simonb 	case MIPS_CN52XX:
    175  1.1  simonb 		family = "52";	/* XXX may also be family "51" */
    176  1.1  simonb 		octeon_core_ver = OCTEON_PLUS;
    177  1.1  simonb 		break;
    178  1.1  simonb 	case MIPS_CN63XX:
    179  1.1  simonb 		family = "63";	/* XXX may also be family "62" */
    180  1.1  simonb 		octeon_core_ver = OCTEON_2;
    181  1.1  simonb 		break;
    182  1.1  simonb 	case MIPS_CN68XX:
    183  1.1  simonb 		family = "68";
    184  1.1  simonb 		octeon_core_ver = OCTEON_2;
    185  1.1  simonb 		break;
    186  1.1  simonb 	case MIPS_CN66XX:
    187  1.1  simonb 		family = "66";
    188  1.1  simonb 		octeon_core_ver = OCTEON_2;
    189  1.1  simonb 		break;
    190  1.1  simonb 	case MIPS_CN61XX:
    191  1.1  simonb 		family = "61";	/* XXX may also be family "60" */
    192  1.1  simonb 		octeon_core_ver = OCTEON_2;
    193  1.1  simonb 		break;
    194  1.1  simonb 	case MIPS_CN78XX:
    195  1.1  simonb 		family = "78";	/* XXX may also be family "76" or "77" */
    196  1.1  simonb 		octeon_core_ver = OCTEON_3;
    197  1.1  simonb 		break;
    198  1.1  simonb 	case MIPS_CN70XX:
    199  1.1  simonb 		family = "70";
    200  1.1  simonb 		if (octeon_xkphys_read_8(MIO_FUS_PDF) & MIO_FUS_PDF_IS_71XX)
    201  1.1  simonb 			family = "71";
    202  1.1  simonb 		octeon_core_ver = OCTEON_3;
    203  1.1  simonb 		tested = true;
    204  1.1  simonb 		break;
    205  1.1  simonb 	case MIPS_CN73XX:
    206  1.1  simonb 		family = "73";	/* XXX may also be family "72" */
    207  1.1  simonb 		octeon_core_ver = OCTEON_3;
    208  1.1  simonb 		tested = true;
    209  1.1  simonb 		break;
    210  1.1  simonb 	default:
    211  1.2  andvar 		panic("IMPL 0x%02x not implemented", MIPS_PRID_IMPL(cpu_id));
    212  1.1  simonb 	}
    213  1.1  simonb 
    214  1.1  simonb 	snprintf(buf, sizeof(buf), "CN%s%s-%d%s", family, coremodel,
    215  1.1  simonb 	    clock_mhz, tested ? "" : " (unverified)");
    216  1.1  simonb 
    217  1.1  simonb 	if (!tested)
    218  1.1  simonb 		printf(">>> model %s\n", buf);
    219  1.1  simonb 
    220  1.1  simonb 	return buf;
    221  1.1  simonb }
    222  1.1  simonb 
    223  1.1  simonb /*
    224  1.1  simonb  * Return the coprocessor clock speed (IO clock speed).
    225  1.1  simonb  *
    226  1.1  simonb  * Octeon I and Octeon Plus use the CPU core clock speed.
    227  1.1  simonb  * Octeon II and III use a configurable multiplier against
    228  1.1  simonb  * the PLL reference clock speed (50MHz).
    229  1.1  simonb  */
    230  1.1  simonb int
    231  1.1  simonb octeon_ioclock_speed(void)
    232  1.1  simonb {
    233  1.1  simonb 	u_int64_t mio_rst_boot, rst_boot;
    234  1.1  simonb 
    235  1.1  simonb 	switch (octeon_core_ver) {
    236  1.1  simonb 	case OCTEON_1:
    237  1.1  simonb 	case OCTEON_PLUS:
    238  1.1  simonb 		return curcpu()->ci_cpu_freq;
    239  1.1  simonb 	case OCTEON_2:
    240  1.1  simonb 		mio_rst_boot = octeon_xkphys_read_8(MIO_RST_BOOT);
    241  1.1  simonb 		return OCTEON_PLL_REF_CLK *
    242  1.1  simonb 		    __SHIFTOUT(mio_rst_boot, MIO_RST_BOOT_PNR_MUL);
    243  1.1  simonb 	case OCTEON_3:
    244  1.1  simonb 		rst_boot = octeon_xkphys_read_8(RST_BOOT);
    245  1.1  simonb 		return OCTEON_PLL_REF_CLK *
    246  1.1  simonb 		    __SHIFTOUT(rst_boot, RST_BOOT_PNR_MUL);
    247  1.1  simonb 	default:
    248  1.1  simonb 		panic("%s: unknown Octeon core type %d", __func__,
    249  1.1  simonb 		    octeon_core_ver);
    250  1.1  simonb 	}
    251  1.1  simonb }
    252  1.1  simonb 
    253  1.1  simonb /*
    254  1.1  simonb  * Initiate chip soft-reset.
    255  1.1  simonb  */
    256  1.1  simonb void
    257  1.1  simonb octeon_soft_reset(void)
    258  1.1  simonb {
    259  1.1  simonb 
    260  1.1  simonb 	/* XXX should invalidate caches, tlb, watchdog? */
    261  1.1  simonb 	switch (octeon_core_ver) {
    262  1.1  simonb 	case OCTEON_1:
    263  1.1  simonb 	case OCTEON_PLUS:
    264  1.1  simonb 	case OCTEON_2:
    265  1.1  simonb 		octeon_xkphys_write_8(CIU_SOFT_BIST, CIU_SOFT_BIST_SOFT_BIST);
    266  1.1  simonb 		octeon_xkphys_write_8(CIU_SOFT_RST, CIU_SOFT_RST_SOFT_RST);
    267  1.1  simonb 	case OCTEON_3:
    268  1.1  simonb 		octeon_xkphys_write_8(RST_SOFT_RST, CIU_SOFT_RST_SOFT_RST);
    269  1.1  simonb 	default:
    270  1.1  simonb 		panic("%s: unknown Octeon core type %d", __func__,
    271  1.1  simonb 		    octeon_core_ver);
    272  1.1  simonb 	}
    273  1.1  simonb }
    274