Home | History | Annotate | Line # | Download | only in cortex
pl310.c revision 1.3
      1  1.3  matt /*	$NetBSD: pl310.c,v 1.3 2012/09/07 11:49:00 matt Exp $	*/
      2  1.1  matt 
      3  1.1  matt /*-
      4  1.1  matt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.1  matt  * All rights reserved.
      6  1.1  matt  *
      7  1.1  matt  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  matt  * by Matt Thomas
      9  1.1  matt  *
     10  1.1  matt  * Redistribution and use in source and binary forms, with or without
     11  1.1  matt  * modification, are permitted provided that the following conditions
     12  1.1  matt  * are met:
     13  1.1  matt  * 1. Redistributions of source code must retain the above copyright
     14  1.1  matt  *    notice, this list of conditions and the following disclaimer.
     15  1.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  matt  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  matt  *    documentation and/or other materials provided with the distribution.
     18  1.1  matt  *
     19  1.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  matt  */
     31  1.1  matt 
     32  1.1  matt #include <sys/cdefs.h>
     33  1.3  matt __KERNEL_RCSID(0, "$NetBSD: pl310.c,v 1.3 2012/09/07 11:49:00 matt Exp $");
     34  1.1  matt 
     35  1.1  matt #include <sys/param.h>
     36  1.1  matt #include <sys/bus.h>
     37  1.1  matt #include <sys/cpu.h>
     38  1.1  matt #include <sys/device.h>
     39  1.1  matt 
     40  1.1  matt #include <arm/cortex/mpcore_var.h>
     41  1.1  matt #include <arm/cortex/pl310_reg.h>
     42  1.3  matt #include <arm/cortex/pl310_var.h>
     43  1.1  matt 
     44  1.1  matt static int arml2cc_match(device_t, cfdata_t, void *);
     45  1.1  matt static void arml2cc_attach(device_t, device_t, void *);
     46  1.1  matt 
     47  1.1  matt #define	L2CC_BASE	0x2000
     48  1.1  matt #define	L2CC_SIZE	0x1000
     49  1.1  matt 
     50  1.1  matt struct arml2cc_softc {
     51  1.1  matt 	device_t sc_dev;
     52  1.1  matt 	bus_space_tag_t sc_memt;
     53  1.1  matt 	bus_space_handle_t sc_memh;
     54  1.1  matt };
     55  1.1  matt 
     56  1.1  matt CFATTACH_DECL_NEW(arml2cc, sizeof(struct arml2cc_softc),
     57  1.1  matt     arml2cc_match, arml2cc_attach, NULL, NULL);
     58  1.1  matt 
     59  1.3  matt static void arml2cc_disable(struct arml2cc_softc *);
     60  1.3  matt 
     61  1.1  matt static bool attached;
     62  1.1  matt 
     63  1.1  matt static inline uint32_t
     64  1.1  matt arml2cc_read_4(struct arml2cc_softc *sc, bus_size_t o)
     65  1.1  matt {
     66  1.1  matt 	return bus_space_read_4(sc->sc_memt, sc->sc_memh, o);
     67  1.1  matt }
     68  1.1  matt 
     69  1.1  matt static inline void
     70  1.1  matt arml2cc_write_4(struct arml2cc_softc *sc, bus_size_t o, uint32_t v)
     71  1.1  matt {
     72  1.1  matt 	bus_space_write_4(sc->sc_memt, sc->sc_memh, o, v);
     73  1.1  matt }
     74  1.1  matt 
     75  1.1  matt 
     76  1.1  matt /* ARGSUSED */
     77  1.1  matt static int
     78  1.1  matt arml2cc_match(device_t parent, cfdata_t cf, void *aux)
     79  1.1  matt {
     80  1.1  matt 	struct mpcore_attach_args * const mpcaa = aux;
     81  1.1  matt 
     82  1.1  matt 	if (attached)
     83  1.1  matt 		return 0;
     84  1.1  matt 
     85  1.1  matt 	if (!CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid))
     86  1.1  matt 		return 0;
     87  1.1  matt 
     88  1.1  matt 	if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0)
     89  1.1  matt 		return 0;
     90  1.1  matt 
     91  1.1  matt 	/*
     92  1.1  matt 	 * This isn't present on UP A9s (since CBAR isn't present).
     93  1.1  matt 	 */
     94  1.1  matt 	uint32_t mpidr = armreg_mpidr_read();
     95  1.1  matt 	if (mpidr == 0 || (mpidr & MPIDR_U))
     96  1.1  matt 		return 0;
     97  1.1  matt 
     98  1.1  matt 	return 1;
     99  1.1  matt }
    100  1.1  matt 
    101  1.1  matt static const struct {
    102  1.1  matt 	uint8_t rev;
    103  1.1  matt 	uint8_t str[7];
    104  1.1  matt } pl310_revs[] = {
    105  1.1  matt 	{ 0, " r0p0" },
    106  1.1  matt 	{ 2, " r1p0" },
    107  1.1  matt 	{ 4, " r2p0" },
    108  1.1  matt 	{ 5, " r3p0" },
    109  1.1  matt 	{ 6, " r3p1" },
    110  1.1  matt 	{ 8, " r3p2" },
    111  1.1  matt 	{ 9, " r3p3" },
    112  1.1  matt };
    113  1.1  matt 
    114  1.1  matt static void
    115  1.1  matt arml2cc_attach(device_t parent, device_t self, void *aux)
    116  1.1  matt {
    117  1.1  matt         struct arml2cc_softc * const sc = device_private(self);
    118  1.1  matt 	struct mpcore_attach_args * const mpcaa = aux;
    119  1.1  matt 
    120  1.1  matt 	sc->sc_dev = self;
    121  1.1  matt 	sc->sc_memt = mpcaa->mpcaa_memt;
    122  1.1  matt 
    123  1.1  matt 	bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh,
    124  1.1  matt 	    L2CC_BASE, L2CC_SIZE, &sc->sc_memh);
    125  1.1  matt 
    126  1.1  matt 	uint32_t id = arml2cc_read_4(sc, L2C_CACHE_ID);
    127  1.1  matt 	u_int rev = __SHIFTOUT(id, CACHE_ID_REV);
    128  1.1  matt 
    129  1.1  matt 	const char *revstr = "";
    130  1.1  matt 	for (size_t i = 0; i < __arraycount(pl310_revs); i++) {
    131  1.1  matt 		if (rev == pl310_revs[i].rev) {
    132  1.1  matt 			revstr = pl310_revs[i].str;
    133  1.1  matt 			break;
    134  1.1  matt 		}
    135  1.1  matt 	}
    136  1.1  matt 
    137  1.1  matt 	aprint_naive("\n");
    138  1.3  matt 	aprint_normal(": ARM PL310%s L2 Cache Controller%s\n",
    139  1.3  matt 	    revstr, arml2cc_read_4(sc, L2C_CTL) ? "" : " (disabled)");
    140  1.3  matt 
    141  1.3  matt 	arml2cc_disable(sc);
    142  1.3  matt 	aprint_normal_dev(self, "cache %s\n",
    143  1.3  matt 	    arml2cc_read_4(sc, L2C_CTL) ? "enabled" : "disabled");
    144  1.3  matt }
    145  1.3  matt 
    146  1.3  matt void
    147  1.3  matt arml2cc_disable(struct arml2cc_softc *sc)
    148  1.3  matt {
    149  1.3  matt 	uint32_t way_mask = __BIT(arm_scache.dcache_ways) - 1;
    150  1.3  matt 	register_t psw = cpsid(I32_bit);
    151  1.3  matt 
    152  1.3  matt 	arml2cc_write_4(sc, L2C_CLEAN_INV_WAY, way_mask);
    153  1.3  matt 	while (arml2cc_read_4(sc, L2C_CLEAN_INV_WAY) != 0) {
    154  1.3  matt 		/* spin */
    155  1.3  matt 	}
    156  1.3  matt 
    157  1.3  matt 	arml2cc_write_4(sc, L2C_CACHE_SYNC, 0);
    158  1.3  matt 	while (arml2cc_read_4(sc, L2C_CACHE_SYNC) & 1) {
    159  1.3  matt 		/* spin */
    160  1.3  matt 	}
    161  1.1  matt 
    162  1.3  matt 	arml2cc_write_4(sc, L2C_CTL, 0);	// turn it off */
    163  1.3  matt 	cpsie(psw);
    164  1.3  matt }
    165  1.3  matt 
    166  1.3  matt void
    167  1.3  matt arml2cc_init(bus_space_tag_t bst, bus_space_handle_t bsh, bus_size_t o)
    168  1.3  matt {
    169  1.3  matt 	struct arm_cache_info * const info = &arm_scache;
    170  1.3  matt 
    171  1.3  matt 	uint32_t cfg = bus_space_read_4(bst, bsh, o + L2C_CACHE_TYPE);
    172  1.3  matt 
    173  1.3  matt 	info->cache_type = __SHIFTOUT(cfg, CACHE_TYPE_CTYPE);
    174  1.3  matt 	info->cache_unified = __SHIFTOUT(cfg, CACHE_TYPE_HARVARD) == 0;
    175  1.1  matt 	u_int cfg_dsize = __SHIFTOUT(cfg, CACHE_TYPE_DSIZE);
    176  1.3  matt 
    177  1.1  matt 	u_int d_waysize = 8192 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xWAYSIZE);
    178  1.3  matt 	info->dcache_ways = 8 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xASSOC);
    179  1.3  matt 	info->dcache_line_size = 32 << __SHIFTOUT(cfg_dsize, CACHE_TYPE_xLINESIZE);
    180  1.3  matt 	info->dcache_size = info->dcache_ways * d_waysize;
    181  1.3  matt 
    182  1.3  matt 	if (info->cache_unified) {
    183  1.3  matt 		info->icache_ways = info->dcache_ways;
    184  1.3  matt 		info->icache_line_size = info->dcache_line_size;
    185  1.3  matt 		info->icache_size = info->dcache_size;
    186  1.3  matt 	} else {
    187  1.1  matt 		u_int cfg_isize = __SHIFTOUT(cfg, CACHE_TYPE_ISIZE);
    188  1.1  matt 		u_int i_waysize = 8192 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xWAYSIZE);
    189  1.3  matt 		info->icache_ways = 8 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xASSOC);
    190  1.3  matt 		info->icache_line_size = 32 << __SHIFTOUT(cfg_isize, CACHE_TYPE_xLINESIZE);
    191  1.3  matt 		info->icache_size = i_waysize * info->icache_ways;
    192  1.1  matt 	}
    193  1.1  matt }
    194