Home | History | Annotate | Line # | Download | only in s3c2xx0
s3c2440.c revision 1.2.52.2
      1       1.1  nisimura /*-
      2       1.1  nisimura  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      3       1.1  nisimura  * All rights reserved.
      4       1.1  nisimura  *
      5       1.1  nisimura  * This code is derived from software contributed to The NetBSD Foundation
      6       1.1  nisimura  * by Paul Fleischer <paul (at) xpg.dk>
      7       1.1  nisimura  *
      8       1.1  nisimura  * Redistribution and use in source and binary forms, with or without
      9       1.1  nisimura  * modification, are permitted provided that the following conditions
     10       1.1  nisimura  * are met:
     11       1.1  nisimura  * 1. Redistributions of source code must retain the above copyright
     12       1.1  nisimura  *    notice, this list of conditions and the following disclaimer.
     13       1.1  nisimura  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  nisimura  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  nisimura  *    documentation and/or other materials provided with the distribution.
     16       1.1  nisimura  *
     17       1.1  nisimura  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18       1.1  nisimura  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19       1.1  nisimura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20       1.1  nisimura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21       1.1  nisimura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22       1.1  nisimura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23       1.1  nisimura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24       1.1  nisimura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25       1.1  nisimura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26       1.1  nisimura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27       1.1  nisimura  * POSSIBILITY OF SUCH DAMAGE.
     28       1.1  nisimura  */
     29       1.1  nisimura 
     30       1.1  nisimura /* Derived from s3c2410.c */
     31       1.1  nisimura /*
     32       1.1  nisimura  * Copyright (c) 2003, 2005  Genetec corporation.  All rights reserved.
     33       1.1  nisimura  * Written by Hiroyuki Bessho for Genetec corporation.
     34       1.1  nisimura  *
     35       1.1  nisimura  * Redistribution and use in source and binary forms, with or without
     36       1.1  nisimura  * modification, are permitted provided that the following conditions
     37       1.1  nisimura  * are met:
     38       1.1  nisimura  * 1. Redistributions of source code must retain the above copyright
     39       1.1  nisimura  *    notice, this list of conditions and the following disclaimer.
     40       1.1  nisimura  * 2. Redistributions in binary form must reproduce the above copyright
     41       1.1  nisimura  *    notice, this list of conditions and the following disclaimer in the
     42       1.1  nisimura  *    documentation and/or other materials provided with the distribution.
     43       1.1  nisimura  * 3. The name of Genetec corporation may not be used to endorse
     44       1.1  nisimura  *    or promote products derived from this software without specific prior
     45       1.1  nisimura  *    written permission.
     46       1.1  nisimura  *
     47       1.1  nisimura  * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
     48       1.1  nisimura  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     49       1.1  nisimura  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     50       1.1  nisimura  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORP.
     51       1.1  nisimura  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     52       1.1  nisimura  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     53       1.1  nisimura  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     54       1.1  nisimura  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     55       1.1  nisimura  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     56       1.1  nisimura  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     57       1.1  nisimura  * POSSIBILITY OF SUCH DAMAGE.
     58       1.1  nisimura  */
     59       1.1  nisimura 
     60       1.1  nisimura #include <sys/cdefs.h>
     61  1.2.52.2   thorpej __KERNEL_RCSID(0, "$NetBSD: s3c2440.c,v 1.2.52.2 2021/03/21 17:35:41 thorpej Exp $");
     62       1.1  nisimura 
     63       1.1  nisimura #include <sys/param.h>
     64       1.1  nisimura #include <sys/systm.h>
     65       1.1  nisimura #include <sys/device.h>
     66       1.1  nisimura #include <sys/kernel.h>
     67       1.1  nisimura #include <sys/reboot.h>
     68       1.1  nisimura 
     69       1.1  nisimura #include <machine/cpu.h>
     70       1.1  nisimura #include <sys/bus.h>
     71       1.1  nisimura 
     72       1.1  nisimura #include <arm/cpufunc.h>
     73       1.1  nisimura #include <arm/mainbus/mainbus.h>
     74       1.1  nisimura #include <arm/s3c2xx0/s3c2440reg.h>
     75       1.1  nisimura #include <arm/s3c2xx0/s3c2440var.h>
     76       1.1  nisimura #include <arm/s3c2xx0/s3c2440_dma.h>
     77       1.1  nisimura 
     78       1.1  nisimura #include "locators.h"
     79       1.1  nisimura #include "opt_cpuoptions.h"
     80       1.1  nisimura 
     81       1.1  nisimura /* prototypes */
     82       1.2       chs static int	s3c2440_match(device_t, cfdata_t, void *);
     83       1.2       chs static void	s3c2440_attach(device_t, device_t, void *);
     84       1.2       chs static int	s3c2440_search(device_t, cfdata_t, const int *, void *);
     85       1.1  nisimura 
     86       1.1  nisimura /* attach structures */
     87       1.1  nisimura CFATTACH_DECL_NEW(ssio, sizeof(struct s3c24x0_softc), s3c2440_match, s3c2440_attach,
     88       1.1  nisimura     NULL, NULL);
     89       1.1  nisimura 
     90       1.1  nisimura extern struct bus_space s3c2xx0_bs_tag;
     91       1.1  nisimura 
     92       1.1  nisimura struct s3c2xx0_softc *s3c2xx0_softc;
     93       1.1  nisimura 
     94       1.1  nisimura #ifdef DEBUG_PORTF
     95       1.1  nisimura volatile uint8_t *portf;	/* for debug */
     96       1.1  nisimura #endif
     97       1.1  nisimura 
     98       1.1  nisimura static int
     99       1.1  nisimura s3c2440_print(void *aux, const char *name)
    100       1.1  nisimura {
    101       1.1  nisimura 	struct s3c2xx0_attach_args *sa = (struct s3c2xx0_attach_args *) aux;
    102       1.1  nisimura 
    103       1.1  nisimura 	if (sa->sa_size)
    104       1.1  nisimura 		aprint_normal(" addr 0x%lx", sa->sa_addr);
    105       1.1  nisimura 	if (sa->sa_size > 1)
    106       1.1  nisimura 		aprint_normal("-0x%lx", sa->sa_addr + sa->sa_size - 1);
    107       1.1  nisimura 	if (sa->sa_intr != SSIOCF_INTR_DEFAULT)
    108       1.1  nisimura 		aprint_normal(" intr %d", sa->sa_intr);
    109       1.1  nisimura 	if (sa->sa_index != SSIOCF_INDEX_DEFAULT)
    110       1.1  nisimura 		aprint_normal(" unit %d", sa->sa_index);
    111       1.1  nisimura 
    112       1.1  nisimura 	return (UNCONF);
    113       1.1  nisimura }
    114       1.1  nisimura 
    115       1.1  nisimura int
    116       1.2       chs s3c2440_match(device_t parent, cfdata_t match, void *aux)
    117       1.1  nisimura {
    118       1.1  nisimura 	return 1;
    119       1.1  nisimura }
    120       1.1  nisimura 
    121       1.1  nisimura void
    122       1.2       chs s3c2440_attach(device_t parent, device_t self, void *aux)
    123       1.1  nisimura {
    124       1.1  nisimura 	struct s3c24x0_softc *sc = device_private(self);
    125       1.1  nisimura 	bus_space_tag_t iot;
    126       1.1  nisimura 	const char *which_registers;	/* for panic message */
    127       1.1  nisimura 
    128       1.1  nisimura #define FAIL(which)  do { \
    129       1.1  nisimura 	which_registers=(which); goto abort; }while(/*CONSTCOND*/0)
    130       1.1  nisimura 
    131       1.1  nisimura 	s3c2xx0_softc = &(sc->sc_sx);
    132       1.1  nisimura 	sc->sc_sx.sc_iot = iot = &s3c2xx0_bs_tag;
    133       1.1  nisimura 
    134       1.1  nisimura 	if (bus_space_map(iot,
    135       1.1  nisimura 		S3C2440_INTCTL_BASE, S3C2440_INTCTL_SIZE,
    136       1.1  nisimura 		BUS_SPACE_MAP_LINEAR, &sc->sc_sx.sc_intctl_ioh))
    137       1.1  nisimura 		FAIL("intc");
    138       1.1  nisimura 	/* tell register addresses to interrupt handler */
    139       1.1  nisimura 	s3c2440_intr_init(sc);
    140       1.1  nisimura 
    141       1.1  nisimura 	/* Map the GPIO registers */
    142       1.1  nisimura 	if (bus_space_map(iot, S3C2440_GPIO_BASE, S3C2440_GPIO_SIZE,
    143       1.1  nisimura 		0, &sc->sc_sx.sc_gpio_ioh))
    144       1.1  nisimura 		FAIL("GPIO");
    145       1.1  nisimura #ifdef DEBUG_PORTF
    146       1.1  nisimura 	{
    147       1.1  nisimura 		extern volatile uint8_t *portf;
    148       1.1  nisimura 		/* make all ports output */
    149       1.1  nisimura 		bus_space_write_2(iot, sc->sc_sx.sc_gpio_ioh, GPIO_PCONF, 0x5555);
    150       1.1  nisimura 		portf = (volatile uint8_t *)
    151       1.1  nisimura 			((char *)bus_space_vaddr(iot, sc->sc_sx.sc_gpio_ioh) + GPIO_PDATF);
    152       1.1  nisimura 	}
    153       1.1  nisimura #endif
    154       1.1  nisimura 
    155       1.1  nisimura #if 1
    156       1.1  nisimura 	/* Map the DMA controller registers */
    157       1.1  nisimura 	if (bus_space_map(iot, S3C2440_DMAC_BASE, S3C2440_DMAC_SIZE,
    158       1.1  nisimura 		0, &sc->sc_sx.sc_dmach))
    159       1.1  nisimura 		FAIL("DMAC");
    160       1.1  nisimura #endif
    161       1.1  nisimura 
    162       1.1  nisimura 	/* Memory controller */
    163       1.1  nisimura 	if (bus_space_map(iot, S3C2440_MEMCTL_BASE,
    164       1.1  nisimura 		S3C24X0_MEMCTL_SIZE, 0, &sc->sc_sx.sc_memctl_ioh))
    165       1.1  nisimura 		FAIL("MEMC");
    166       1.1  nisimura 	/* Clock manager */
    167       1.1  nisimura 	if (bus_space_map(iot, S3C2440_CLKMAN_BASE,
    168       1.1  nisimura 		S3C24X0_CLKMAN_SIZE, 0, &sc->sc_sx.sc_clkman_ioh))
    169       1.1  nisimura 		FAIL("CLK");
    170       1.1  nisimura 
    171       1.1  nisimura #if 0
    172       1.1  nisimura 	/* Real time clock */
    173       1.1  nisimura 	if (bus_space_map(iot, S3C2410_RTC_BASE,
    174       1.1  nisimura 		S3C24X0_RTC_SIZE, 0, &sc->sc_sx.sc_rtc_ioh))
    175       1.1  nisimura 		FAIL("RTC");
    176       1.1  nisimura #endif
    177       1.1  nisimura 
    178       1.1  nisimura 	if (bus_space_map(iot, S3C2440_TIMER_BASE,
    179       1.1  nisimura 		S3C24X0_TIMER_SIZE, 0, &sc->sc_timer_ioh)) {
    180       1.1  nisimura 		FAIL("TIMER");
    181       1.1  nisimura         }
    182       1.1  nisimura 
    183       1.1  nisimura 	/* calculate current clock frequency */
    184       1.1  nisimura 	s3c24x0_clock_freq(&sc->sc_sx);
    185       1.1  nisimura 	aprint_normal(": fclk %d MHz hclk %d MHz pclk %d MHz\n",
    186       1.1  nisimura 	       sc->sc_sx.sc_fclk / 1000000, sc->sc_sx.sc_hclk / 1000000,
    187       1.1  nisimura 	       sc->sc_sx.sc_pclk / 1000000);
    188       1.1  nisimura 
    189       1.1  nisimura 	aprint_naive("\n");
    190       1.1  nisimura 
    191       1.1  nisimura 	/* get busdma tag for the platform */
    192       1.1  nisimura 	sc->sc_sx.sc_dmat = s3c2xx0_bus_dma_init(&s3c2xx0_bus_dma);
    193       1.1  nisimura 
    194       1.1  nisimura 	s3c2440_dma_init();
    195       1.1  nisimura 
    196       1.1  nisimura 	/*
    197       1.1  nisimura 	 *  Attach devices.
    198       1.1  nisimura 	 */
    199  1.2.52.1   thorpej 	config_search(self, NULL,
    200  1.2.52.1   thorpej 	    CFARG_SUBMATCH, s3c2440_search,
    201  1.2.52.1   thorpej 	    CFARG_EOL);
    202       1.1  nisimura 	return;
    203       1.1  nisimura 
    204       1.1  nisimura abort:
    205       1.1  nisimura 	panic("%s: unable to map %s registers",
    206       1.2       chs 	    device_xname(self), which_registers);
    207       1.1  nisimura 
    208       1.1  nisimura #undef FAIL
    209       1.1  nisimura }
    210       1.1  nisimura 
    211       1.1  nisimura int
    212       1.2       chs s3c2440_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    213       1.1  nisimura {
    214       1.1  nisimura 	struct s3c24x0_softc *sc = device_private(parent);
    215       1.1  nisimura 	struct s3c2xx0_attach_args aa;
    216       1.1  nisimura 
    217       1.1  nisimura 	aa.sa_sc = sc;
    218       1.1  nisimura 	aa.sa_iot = sc->sc_sx.sc_iot;
    219       1.1  nisimura 	aa.sa_addr = cf->cf_loc[SSIOCF_ADDR];
    220       1.1  nisimura 	aa.sa_size = cf->cf_loc[SSIOCF_SIZE];
    221       1.1  nisimura 	aa.sa_index = cf->cf_loc[SSIOCF_INDEX];
    222       1.1  nisimura 	aa.sa_intr = cf->cf_loc[SSIOCF_INTR];
    223       1.1  nisimura 
    224       1.1  nisimura 	aa.sa_dmat = sc->sc_sx.sc_dmat;
    225       1.1  nisimura 
    226       1.1  nisimura 	if (config_match(parent, cf, &aa))
    227       1.1  nisimura 		config_attach(parent, cf, &aa, s3c2440_print);
    228       1.1  nisimura 
    229       1.1  nisimura 	return 0;
    230       1.1  nisimura }
    231       1.1  nisimura 
    232       1.1  nisimura /*
    233       1.1  nisimura  * fill sc_pclk, sc_hclk, sc_fclk from values of clock controller register.
    234       1.1  nisimura  *
    235       1.1  nisimura  * s3c24x0_clock_freq2() is meant to be called from kernel startup routines.
    236       1.1  nisimura  * s3c24x0_clock_freq() is for after kernel initialization is done.
    237       1.1  nisimura  */
    238       1.1  nisimura void
    239       1.1  nisimura s3c24x0_clock_freq2(vaddr_t clkman_base, int *fclk, int *hclk, int *pclk)
    240       1.1  nisimura {
    241       1.1  nisimura 	uint32_t pllcon, divn, camdivn;
    242       1.1  nisimura 	int mdiv, pdiv, sdiv;
    243       1.1  nisimura 	uint32_t f, h, p;
    244       1.1  nisimura 
    245       1.1  nisimura 	pllcon = *(volatile uint32_t *)(clkman_base + CLKMAN_MPLLCON);
    246       1.1  nisimura 	divn = *(volatile uint32_t *)(clkman_base + CLKMAN_CLKDIVN);
    247       1.1  nisimura         camdivn = *(volatile uint32_t *)(clkman_base + CLKMAN_CAMDIVN);
    248       1.1  nisimura 
    249       1.1  nisimura 	mdiv = (pllcon & PLLCON_MDIV_MASK) >> PLLCON_MDIV_SHIFT;
    250       1.1  nisimura 	pdiv = (pllcon & PLLCON_PDIV_MASK) >> PLLCON_PDIV_SHIFT;
    251       1.1  nisimura 	sdiv = (pllcon & PLLCON_SDIV_MASK) >> PLLCON_SDIV_SHIFT;
    252       1.1  nisimura 
    253       1.1  nisimura 	f = ((mdiv + 8) * S3C2XX0_XTAL_CLK) / ((pdiv + 2) * (1 << sdiv)) * 2;
    254       1.1  nisimura 	h = f;
    255       1.1  nisimura 
    256       1.1  nisimura         /* HDIVN of CLKDIVN can have 4 distinct values */
    257       1.1  nisimura         switch( (divn & CLKDIVN_HDIVN_MASK) >> CLKDIVN_HDIVN_SHIFT )
    258       1.1  nisimura         {
    259       1.1  nisimura           case 0:
    260       1.1  nisimura             /* 00b: HCLK = FCLK/1*/
    261       1.1  nisimura             break;
    262       1.1  nisimura           case 1:
    263       1.1  nisimura             /* 01b: HCLK = FCLK/2*/
    264       1.1  nisimura             h /= 2;
    265       1.1  nisimura             break;
    266       1.1  nisimura           case 2:
    267       1.1  nisimura             /* 10b: HCLK = FCLK/4 when CAMDIVN[9] (HCLK4_HALF) = 0
    268       1.1  nisimura              *      HCLK = FCLK/8 when CAMDIVN[9] (HCLK4_HALF) = 1 */
    269       1.1  nisimura             if( camdivn & CLKCAMDIVN_HCLK4_HALF )
    270       1.1  nisimura               h /= 8;
    271       1.1  nisimura             else
    272       1.1  nisimura               h /= 4;
    273       1.1  nisimura             break;
    274       1.1  nisimura           case 3:
    275       1.1  nisimura             /* 11b: HCLK = FCLK/3 when CAMDIVN[8] (HCLK3_HALF) = 0
    276       1.1  nisimura              *      HCLK = FCLK/6 when CAMDIVN[8] (HCLK3_HALF) = 1 */
    277       1.1  nisimura             if( camdivn & CLKCAMDIVN_HCLK3_HALF )
    278       1.1  nisimura               h /= 6;
    279       1.1  nisimura             else
    280       1.1  nisimura               h /= 3;
    281       1.1  nisimura             break;
    282       1.1  nisimura         }
    283       1.1  nisimura 
    284       1.1  nisimura 	p = h;
    285       1.1  nisimura 
    286       1.1  nisimura 	if (divn & CLKDIVN_PDIVN)
    287       1.1  nisimura 		p /= 2;
    288       1.1  nisimura 
    289       1.1  nisimura 	if (fclk) *fclk = f;
    290       1.1  nisimura 	if (hclk) *hclk = h;
    291       1.1  nisimura 	if (pclk) *pclk = p;
    292       1.1  nisimura 
    293       1.1  nisimura }
    294       1.1  nisimura 
    295       1.1  nisimura void
    296       1.1  nisimura s3c24x0_clock_freq(struct s3c2xx0_softc *sc)
    297       1.1  nisimura {
    298       1.1  nisimura 	s3c24x0_clock_freq2(
    299       1.1  nisimura 		(vaddr_t)bus_space_vaddr(sc->sc_iot, sc->sc_clkman_ioh),
    300       1.1  nisimura 		&sc->sc_fclk, &sc->sc_hclk, &sc->sc_pclk);
    301       1.1  nisimura }
    302       1.1  nisimura 
    303       1.1  nisimura /*
    304       1.1  nisimura  * Issue software reset command.
    305       1.1  nisimura  * called with MMU off.
    306       1.1  nisimura  *
    307       1.1  nisimura  * S3C2410 doesn't have sowtware reset bit like S3C2800.
    308       1.1  nisimura  * use watch dog timer and make it fire immediately.
    309       1.1  nisimura  */
    310       1.1  nisimura void
    311       1.1  nisimura s3c2440_softreset(void)
    312       1.1  nisimura {
    313       1.1  nisimura 	disable_interrupts(I32_bit|F32_bit);
    314       1.1  nisimura 
    315       1.1  nisimura 	*(volatile unsigned int *)(S3C2440_WDT_BASE + WDT_WTCON)
    316       1.1  nisimura 		= (0 << WTCON_PRESCALE_SHIFT) | WTCON_ENABLE |
    317       1.1  nisimura 		WTCON_CLKSEL_16 | WTCON_ENRST;
    318       1.1  nisimura }
    319       1.1  nisimura 
    320       1.1  nisimura 
    321