Home | History | Annotate | Line # | Download | only in s3c2xx0
      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.4   thorpej __KERNEL_RCSID(0, "$NetBSD: s3c2440.c,v 1.4 2021/08/07 16:18:45 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.3   thorpej 	config_search(self, NULL,
    200  1.4   thorpej 	    CFARGS(.search = s3c2440_search));
    201  1.1  nisimura 	return;
    202  1.1  nisimura 
    203  1.1  nisimura abort:
    204  1.1  nisimura 	panic("%s: unable to map %s registers",
    205  1.2       chs 	    device_xname(self), which_registers);
    206  1.1  nisimura 
    207  1.1  nisimura #undef FAIL
    208  1.1  nisimura }
    209  1.1  nisimura 
    210  1.1  nisimura int
    211  1.2       chs s3c2440_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    212  1.1  nisimura {
    213  1.1  nisimura 	struct s3c24x0_softc *sc = device_private(parent);
    214  1.1  nisimura 	struct s3c2xx0_attach_args aa;
    215  1.1  nisimura 
    216  1.1  nisimura 	aa.sa_sc = sc;
    217  1.1  nisimura 	aa.sa_iot = sc->sc_sx.sc_iot;
    218  1.1  nisimura 	aa.sa_addr = cf->cf_loc[SSIOCF_ADDR];
    219  1.1  nisimura 	aa.sa_size = cf->cf_loc[SSIOCF_SIZE];
    220  1.1  nisimura 	aa.sa_index = cf->cf_loc[SSIOCF_INDEX];
    221  1.1  nisimura 	aa.sa_intr = cf->cf_loc[SSIOCF_INTR];
    222  1.1  nisimura 
    223  1.1  nisimura 	aa.sa_dmat = sc->sc_sx.sc_dmat;
    224  1.1  nisimura 
    225  1.3   thorpej 	if (config_probe(parent, cf, &aa))
    226  1.4   thorpej 		config_attach(parent, cf, &aa, s3c2440_print, CFARGS_NONE);
    227  1.1  nisimura 
    228  1.1  nisimura 	return 0;
    229  1.1  nisimura }
    230  1.1  nisimura 
    231  1.1  nisimura /*
    232  1.1  nisimura  * fill sc_pclk, sc_hclk, sc_fclk from values of clock controller register.
    233  1.1  nisimura  *
    234  1.1  nisimura  * s3c24x0_clock_freq2() is meant to be called from kernel startup routines.
    235  1.1  nisimura  * s3c24x0_clock_freq() is for after kernel initialization is done.
    236  1.1  nisimura  */
    237  1.1  nisimura void
    238  1.1  nisimura s3c24x0_clock_freq2(vaddr_t clkman_base, int *fclk, int *hclk, int *pclk)
    239  1.1  nisimura {
    240  1.1  nisimura 	uint32_t pllcon, divn, camdivn;
    241  1.1  nisimura 	int mdiv, pdiv, sdiv;
    242  1.1  nisimura 	uint32_t f, h, p;
    243  1.1  nisimura 
    244  1.1  nisimura 	pllcon = *(volatile uint32_t *)(clkman_base + CLKMAN_MPLLCON);
    245  1.1  nisimura 	divn = *(volatile uint32_t *)(clkman_base + CLKMAN_CLKDIVN);
    246  1.1  nisimura         camdivn = *(volatile uint32_t *)(clkman_base + CLKMAN_CAMDIVN);
    247  1.1  nisimura 
    248  1.1  nisimura 	mdiv = (pllcon & PLLCON_MDIV_MASK) >> PLLCON_MDIV_SHIFT;
    249  1.1  nisimura 	pdiv = (pllcon & PLLCON_PDIV_MASK) >> PLLCON_PDIV_SHIFT;
    250  1.1  nisimura 	sdiv = (pllcon & PLLCON_SDIV_MASK) >> PLLCON_SDIV_SHIFT;
    251  1.1  nisimura 
    252  1.1  nisimura 	f = ((mdiv + 8) * S3C2XX0_XTAL_CLK) / ((pdiv + 2) * (1 << sdiv)) * 2;
    253  1.1  nisimura 	h = f;
    254  1.1  nisimura 
    255  1.1  nisimura         /* HDIVN of CLKDIVN can have 4 distinct values */
    256  1.1  nisimura         switch( (divn & CLKDIVN_HDIVN_MASK) >> CLKDIVN_HDIVN_SHIFT )
    257  1.1  nisimura         {
    258  1.1  nisimura           case 0:
    259  1.1  nisimura             /* 00b: HCLK = FCLK/1*/
    260  1.1  nisimura             break;
    261  1.1  nisimura           case 1:
    262  1.1  nisimura             /* 01b: HCLK = FCLK/2*/
    263  1.1  nisimura             h /= 2;
    264  1.1  nisimura             break;
    265  1.1  nisimura           case 2:
    266  1.1  nisimura             /* 10b: HCLK = FCLK/4 when CAMDIVN[9] (HCLK4_HALF) = 0
    267  1.1  nisimura              *      HCLK = FCLK/8 when CAMDIVN[9] (HCLK4_HALF) = 1 */
    268  1.1  nisimura             if( camdivn & CLKCAMDIVN_HCLK4_HALF )
    269  1.1  nisimura               h /= 8;
    270  1.1  nisimura             else
    271  1.1  nisimura               h /= 4;
    272  1.1  nisimura             break;
    273  1.1  nisimura           case 3:
    274  1.1  nisimura             /* 11b: HCLK = FCLK/3 when CAMDIVN[8] (HCLK3_HALF) = 0
    275  1.1  nisimura              *      HCLK = FCLK/6 when CAMDIVN[8] (HCLK3_HALF) = 1 */
    276  1.1  nisimura             if( camdivn & CLKCAMDIVN_HCLK3_HALF )
    277  1.1  nisimura               h /= 6;
    278  1.1  nisimura             else
    279  1.1  nisimura               h /= 3;
    280  1.1  nisimura             break;
    281  1.1  nisimura         }
    282  1.1  nisimura 
    283  1.1  nisimura 	p = h;
    284  1.1  nisimura 
    285  1.1  nisimura 	if (divn & CLKDIVN_PDIVN)
    286  1.1  nisimura 		p /= 2;
    287  1.1  nisimura 
    288  1.1  nisimura 	if (fclk) *fclk = f;
    289  1.1  nisimura 	if (hclk) *hclk = h;
    290  1.1  nisimura 	if (pclk) *pclk = p;
    291  1.1  nisimura 
    292  1.1  nisimura }
    293  1.1  nisimura 
    294  1.1  nisimura void
    295  1.1  nisimura s3c24x0_clock_freq(struct s3c2xx0_softc *sc)
    296  1.1  nisimura {
    297  1.1  nisimura 	s3c24x0_clock_freq2(
    298  1.1  nisimura 		(vaddr_t)bus_space_vaddr(sc->sc_iot, sc->sc_clkman_ioh),
    299  1.1  nisimura 		&sc->sc_fclk, &sc->sc_hclk, &sc->sc_pclk);
    300  1.1  nisimura }
    301  1.1  nisimura 
    302  1.1  nisimura /*
    303  1.1  nisimura  * Issue software reset command.
    304  1.1  nisimura  * called with MMU off.
    305  1.1  nisimura  *
    306  1.1  nisimura  * S3C2410 doesn't have sowtware reset bit like S3C2800.
    307  1.1  nisimura  * use watch dog timer and make it fire immediately.
    308  1.1  nisimura  */
    309  1.1  nisimura void
    310  1.1  nisimura s3c2440_softreset(void)
    311  1.1  nisimura {
    312  1.1  nisimura 	disable_interrupts(I32_bit|F32_bit);
    313  1.1  nisimura 
    314  1.1  nisimura 	*(volatile unsigned int *)(S3C2440_WDT_BASE + WDT_WTCON)
    315  1.1  nisimura 		= (0 << WTCON_PRESCALE_SHIFT) | WTCON_ENABLE |
    316  1.1  nisimura 		WTCON_CLKSEL_16 | WTCON_ENRST;
    317  1.1  nisimura }
    318  1.1  nisimura 
    319  1.1  nisimura 
    320