Home | History | Annotate | Line # | Download | only in imx
imx23_clkctrl.c revision 1.2.18.1
      1  1.2.18.1    martin /* $Id: imx23_clkctrl.c,v 1.2.18.1 2020/04/13 08:03:34 martin Exp $ */
      2       1.1      matt 
      3       1.1      matt /*
      4       1.1      matt * Copyright (c) 2013 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 Petri Laakso.
      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/param.h>
     33       1.1      matt #include <sys/types.h>
     34       1.1      matt #include <sys/bus.h>
     35       1.1      matt #include <sys/cdefs.h>
     36       1.1      matt #include <sys/device.h>
     37       1.1      matt #include <sys/errno.h>
     38       1.1      matt 
     39       1.1      matt #include <arm/imx/imx23_clkctrlreg.h>
     40       1.1      matt #include <arm/imx/imx23_clkctrlvar.h>
     41       1.1      matt #include <arm/imx/imx23var.h>
     42       1.1      matt 
     43       1.1      matt typedef struct clkctrl_softc {
     44       1.1      matt 	device_t sc_dev;
     45       1.1      matt 	bus_space_tag_t sc_iot;
     46       1.1      matt 	bus_space_handle_t sc_hdl;
     47       1.1      matt } *clkctrl_softc_t;
     48       1.1      matt 
     49       1.1      matt static int	clkctrl_match(device_t, cfdata_t, void *);
     50       1.1      matt static void	clkctrl_attach(device_t, device_t, void *);
     51       1.1      matt static int	clkctrl_activate(device_t, enum devact);
     52       1.1      matt 
     53       1.1      matt static void     clkctrl_init(struct clkctrl_softc *);
     54       1.1      matt 
     55       1.1      matt static clkctrl_softc_t _sc = NULL;
     56       1.1      matt 
     57       1.1      matt CFATTACH_DECL3_NEW(clkctrl,
     58       1.1      matt         sizeof(struct clkctrl_softc),
     59       1.1      matt         clkctrl_match,
     60       1.1      matt         clkctrl_attach,
     61       1.1      matt         NULL,
     62       1.1      matt         clkctrl_activate,
     63       1.1      matt         NULL,
     64       1.1      matt         NULL,
     65       1.1      matt         0
     66       1.1      matt );
     67       1.1      matt 
     68       1.1      matt #define CLKCTRL_RD(sc, reg)                                                 \
     69       1.1      matt         bus_space_read_4(sc->sc_iot, sc->sc_hdl, (reg))
     70       1.1      matt #define CLKCTRL_WR(sc, reg, val)                                            \
     71       1.1      matt         bus_space_write_4(sc->sc_iot, sc->sc_hdl, (reg), (val))
     72       1.1      matt 
     73       1.1      matt #define CLKCTRL_SOFT_RST_LOOP 455 /* At least 1 us ... */
     74       1.1      matt 
     75       1.1      matt static int
     76       1.1      matt clkctrl_match(device_t parent, cfdata_t match, void *aux)
     77       1.1      matt {
     78       1.1      matt 	struct apb_attach_args *aa = aux;
     79       1.1      matt 
     80       1.1      matt 	if ((aa->aa_addr == HW_CLKCTRL_BASE) &&
     81       1.1      matt 	    (aa->aa_size == HW_CLKCTRL_SIZE))
     82       1.1      matt 		return 1;
     83       1.1      matt 
     84       1.1      matt 	return 0;
     85       1.1      matt }
     86       1.1      matt 
     87       1.1      matt static void
     88       1.1      matt clkctrl_attach(device_t parent, device_t self, void *aux)
     89       1.1      matt {
     90       1.1      matt 	struct clkctrl_softc *sc = device_private(self);
     91       1.1      matt 	struct apb_attach_args *aa = aux;
     92       1.1      matt 	static int clkctrl_attached = 0;
     93       1.1      matt 
     94       1.1      matt 	sc->sc_dev = self;
     95       1.1      matt 	sc->sc_iot = aa->aa_iot;
     96       1.1      matt 
     97       1.1      matt 	if (clkctrl_attached) {
     98       1.1      matt 		aprint_error_dev(sc->sc_dev, "already attached\n");
     99       1.1      matt 		return;
    100       1.1      matt 	}
    101       1.1      matt 
    102       1.1      matt 	if (bus_space_map(sc->sc_iot, aa->aa_addr, aa->aa_size, 0,
    103       1.1      matt 	    &sc->sc_hdl))
    104       1.1      matt 	{
    105       1.1      matt 		aprint_error_dev(sc->sc_dev, "Unable to map bus space\n");
    106       1.1      matt 		return;
    107       1.1      matt 	}
    108       1.1      matt 
    109       1.1      matt 
    110       1.1      matt 	clkctrl_init(sc);
    111       1.1      matt 
    112       1.1      matt 	aprint_normal("\n");
    113       1.1      matt 
    114       1.1      matt 	clkctrl_attached = 1;
    115       1.1      matt 
    116       1.1      matt 	return;
    117       1.1      matt }
    118       1.1      matt 
    119       1.1      matt static int
    120       1.1      matt clkctrl_activate(device_t self, enum devact act)
    121       1.1      matt {
    122       1.1      matt 
    123       1.1      matt 	return EOPNOTSUPP;
    124       1.1      matt }
    125       1.1      matt 
    126       1.1      matt static void
    127       1.1      matt clkctrl_init(struct clkctrl_softc *sc)
    128       1.1      matt {
    129       1.1      matt 	_sc = sc;
    130       1.1      matt 	return;
    131       1.1      matt }
    132       1.1      matt 
    133       1.1      matt /*
    134       1.1      matt  * Power up 8-phase PLL outputs for USB PHY
    135       1.1      matt  *
    136       1.1      matt  */
    137       1.1      matt void
    138       1.1      matt clkctrl_en_usb(void)
    139       1.1      matt {
    140       1.1      matt 	struct clkctrl_softc *sc = _sc;
    141       1.1      matt 
    142       1.1      matt         if (sc == NULL) {
    143  1.2.18.1    martin                 aprint_error("clkctrl is not initialized");
    144       1.1      matt                 return;
    145       1.1      matt         }
    146       1.1      matt 
    147       1.1      matt 	CLKCTRL_WR(sc, HW_CLKCTRL_PLLCTRL0_SET,
    148       1.1      matt 	    HW_CLKCTRL_PLLCTRL0_EN_USB_CLKS);
    149       1.1      matt 
    150       1.1      matt 	return;
    151       1.1      matt }
    152       1.2  jmcneill 
    153       1.2  jmcneill /*
    154       1.2  jmcneill  * Enable 24MHz clock for the Digital Filter.
    155       1.2  jmcneill  *
    156       1.2  jmcneill  */
    157       1.2  jmcneill void
    158       1.2  jmcneill clkctrl_en_filtclk(void)
    159       1.2  jmcneill {
    160       1.2  jmcneill 	struct clkctrl_softc *sc = _sc;
    161       1.2  jmcneill 
    162       1.2  jmcneill 	if (sc == NULL) {
    163  1.2.18.1    martin 		aprint_error("clkctrl is not initialized");
    164       1.2  jmcneill 		return;
    165       1.2  jmcneill 	}
    166       1.2  jmcneill 
    167       1.2  jmcneill 	CLKCTRL_WR(sc, HW_CLKCTRL_XTAL_CLR, HW_CLKCTRL_XTAL_FILT_CLK24M_GATE);
    168       1.2  jmcneill 
    169       1.2  jmcneill 	return;
    170       1.2  jmcneill }
    171