Home | History | Annotate | Line # | Download | only in ep93xx
      1 /*	$NetBSD: epsoc.c,v 1.16 2021/11/21 08:25:26 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2004 Jesse Off
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: epsoc.c,v 1.16 2021/11/21 08:25:26 skrll Exp $");
     31 
     32 #include <sys/types.h>
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/kernel.h>
     36 #include <sys/time.h>
     37 #include <sys/device.h>
     38 
     39 #include <sys/bus.h>
     40 #include <machine/intr.h>
     41 
     42 #include <arm/cpufunc.h>
     43 
     44 #include <arm/ep93xx/ep93xxreg.h>
     45 #include <arm/ep93xx/ep93xxvar.h>
     46 #include <arm/ep93xx/epsocvar.h>
     47 
     48 #include "locators.h"
     49 
     50 static int	epsoc_match(device_t, cfdata_t, void *);
     51 static void	epsoc_attach(device_t, device_t, void *);
     52 static int	epsoc_search(device_t, cfdata_t, const int *, void *);
     53 static int	epsoc_print(void *, const char *);
     54 static int	epsoc_submatch(device_t, cfdata_t, const int *, void *);
     55 
     56 CFATTACH_DECL_NEW(epsoc, sizeof(struct epsoc_softc),
     57     epsoc_match, epsoc_attach, NULL, NULL);
     58 
     59 struct epsoc_softc *epsoc_sc = NULL;
     60 
     61 static int
     62 epsoc_match(device_t parent, cfdata_t match, void *aux)
     63 {
     64 	bus_space_handle_t	ioh;
     65 	uint32_t		id, ret = 0;
     66 
     67 	bus_space_map(&ep93xx_bs_tag, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
     68 		EP93XX_APB_SYSCON_SIZE, 0, &ioh);
     69 	id = bus_space_read_4(&ep93xx_bs_tag, ioh, EP93XX_SYSCON_ChipID);
     70 	if ((id & 0x01faffff) == 0x9213) ret = 1;
     71 	bus_space_unmap(&ep93xx_bs_tag, ioh, EP93XX_APB_SYSCON_SIZE);
     72 	return ret;
     73 }
     74 
     75 static void
     76 epsoc_attach(device_t parent, device_t self, void *aux)
     77 {
     78 	struct epsoc_softc	*sc;
     79 	uint64_t		fclk, pclk, hclk;
     80 	uint32_t		id, clkset1;
     81 	const char		*rev;
     82 	int			locs[EPSOCCF_NLOCS];
     83 	struct epsoc_attach_args sa;
     84 
     85 	sc = device_private(self);
     86 
     87 	if (epsoc_sc == NULL)
     88 		epsoc_sc = sc;
     89 
     90 	sc->sc_iot = &ep93xx_bs_tag;
     91 	bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
     92 		EP93XX_APB_SYSCON_SIZE, 0, &sc->sc_ioh);
     93 	id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, EP93XX_SYSCON_ChipID);
     94 	switch(id >> 28) {
     95 	case 0:
     96 		rev = "A";
     97 		break;
     98 	case 1:
     99 		rev = "B";
    100 		break;
    101 	case 2:
    102 		rev = "C";
    103 		break;
    104 	case 3:
    105 		rev = "D0";
    106 		break;
    107 	case 4:
    108 		rev = "D1";
    109 		break;
    110 	case 5:
    111 		rev = "E0";
    112 		break;
    113 	case 6:
    114 		rev = "E1";
    115 		break;
    116 	default:
    117 		rev = ">E1";
    118 		break;
    119 	}
    120 	printf(": Cirrus Logic EP93xx SoC rev %s\n", rev);
    121 
    122 	clkset1 = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    123 		EP93XX_SYSCON_ClkSet1);
    124 	{
    125 		int fclkdiv, hclkdiv, pclkdiv, pll1_ps;
    126 		int pll1x1fbd1, pll1x2fbd2, pll1x2ipd;
    127 		int hclkdivisors[] = {1, 2, 4, 5, 6, 8, 16, 32};
    128 
    129 		fclkdiv =    (clkset1 & 0x0e000000) >> 25;
    130 		hclkdiv =    (clkset1 & 0x00700000) >> 20;
    131 		pclkdiv =    (clkset1 & 0x000c0000) >> 18;
    132 		pll1_ps =    (clkset1 & 0x00030000) >> 16;
    133 		pll1x1fbd1 = (clkset1 & 0x0000f800) >> 11;
    134 		pll1x2fbd2 = (clkset1 & 0x000007e0) >> 5;
    135 		pll1x2ipd =  (clkset1 & 0x0000001f);
    136 		fclk = 14745600ULL * ((pll1x1fbd1 + 1) * (pll1x2fbd2 + 1)) /
    137 			((pll1x2ipd + 1) * (1 << pll1_ps));
    138 		hclk = fclk / hclkdivisors[hclkdiv];
    139 		pclk = hclk >> pclkdiv;
    140 		if (fclkdiv > 4) fclkdiv = 0;
    141 		if (clkset1 & 0x00800000)  /* nBYP1 bypasses PLL */
    142 			fclk = fclk >> fclkdiv;
    143 		else
    144 			fclk = 14745600ULL;
    145 	}
    146 	printf("%s: fclk %lld.%02lld MHz hclk %lld.%02lld MHz pclk %lld.%02lld MHz\n",
    147 		device_xname(self),
    148 		fclk / 1000000, (fclk % 1000000 + 5000) / 10000,
    149 		hclk / 1000000, (hclk % 1000000 + 5000) / 10000,
    150 		pclk / 1000000, (pclk % 1000000 + 5000) / 10000);
    151 
    152 	sc->sc_fclk = fclk;
    153 	sc->sc_hclk = hclk;
    154 	sc->sc_pclk = pclk;
    155 
    156         /* get busdma tag for the platform */
    157         sc->sc_dmat = ep93xx_bus_dma_init(&ep93xx_bus_dma);
    158 
    159 	/*
    160 	 *  Attach each devices
    161 	 *	some device is used by other system device. so attach first.
    162 	 */
    163 	sa.sa_iot = sc->sc_iot;
    164 	sa.sa_dmat = sc->sc_dmat;
    165 	sa.sa_hclk = sc->sc_hclk;
    166 	sa.sa_pclk = sc->sc_pclk;
    167 	locs[EPSOCCF_ADDR] = EP93XX_APB_HWBASE + EP93XX_APB_TIMERS;
    168 	config_found(self, &sa, epsoc_print,
    169 	    CFARGS(.submatch = epsoc_submatch,
    170 		   .locators = locs));
    171 
    172 	locs[EPSOCCF_ADDR] = EP93XX_APB_HWBASE + EP93XX_APB_GPIO;
    173 	sa.sa_gpio = device_private(
    174 	    config_found(self, &sa, epsoc_print,
    175 			 CFARGS(.submatch = epsoc_submatch,
    176 				.locators = locs)));
    177 
    178 	config_search(self, &sa,
    179 	    CFARGS(.search = epsoc_search));
    180 }
    181 
    182 int
    183 epsoc_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    184 {
    185 	struct epsoc_attach_args *sa = aux;
    186 
    187 	if (cf->cf_loc[EPSOCCF_ADDR] == ldesc[EPSOCCF_ADDR]) {
    188 		sa->sa_addr = cf->cf_loc[EPSOCCF_ADDR];
    189 		sa->sa_size = cf->cf_loc[EPSOCCF_SIZE];
    190 		sa->sa_intr = cf->cf_loc[EPSOCCF_INTR];
    191 		return (config_match(parent, cf, aux));
    192 	} else
    193 		return (0);
    194 }
    195 
    196 int
    197 epsoc_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    198 {
    199 	struct epsoc_attach_args *sa = aux;
    200 
    201 	sa->sa_addr = cf->cf_loc[EPSOCCF_ADDR];
    202 	sa->sa_size = cf->cf_loc[EPSOCCF_SIZE];
    203 	sa->sa_intr = cf->cf_loc[EPSOCCF_INTR];
    204 
    205 	if (config_probe(parent, cf, aux))
    206 		config_attach(parent, cf, aux, epsoc_print, CFARGS_NONE);
    207 
    208 	return (0);
    209 }
    210 
    211 static int
    212 epsoc_print(void *aux, const char *name)
    213 {
    214         struct epsoc_attach_args *sa = aux;
    215 
    216 	if (sa->sa_size)
    217 		aprint_normal(" addr 0x%lx", sa->sa_addr);
    218 	if (sa->sa_size > 1)
    219 		aprint_normal("-0x%lx", sa->sa_addr + sa->sa_size - 1);
    220 	if (sa->sa_intr > 1)
    221 		aprint_normal(" intr %d", sa->sa_intr);
    222 
    223 	return (UNCONF);
    224 }
    225