Home | History | Annotate | Line # | Download | only in at91
at91pmc.c revision 1.1.2.1
      1  1.1.2.1  matt /*	$Id: at91pmc.c,v 1.1.2.1 2007/11/10 02:56:33 matt Exp $	*/
      2  1.1.2.1  matt /*	$NetBSD: at91pmc.c,v 1.1.2.1 2007/11/10 02:56:33 matt Exp $	*/
      3  1.1.2.1  matt 
      4  1.1.2.1  matt /*
      5  1.1.2.1  matt  * Copyright (c) 2007 Embedtronics Oy
      6  1.1.2.1  matt  * All rights reserved.
      7  1.1.2.1  matt  *
      8  1.1.2.1  matt  * Redistribution and use in source and binary forms, with or without
      9  1.1.2.1  matt  * modification, are permitted provided that the following conditions
     10  1.1.2.1  matt  * are met:
     11  1.1.2.1  matt  * 1. Redistributions of source code must retain the above copyright
     12  1.1.2.1  matt  *    notice, this list of conditions and the following disclaimer.
     13  1.1.2.1  matt  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1.2.1  matt  *    notice, this list of conditions and the following disclaimer in the
     15  1.1.2.1  matt  *    documentation and/or other materials provided with the distribution.
     16  1.1.2.1  matt  * 3. All advertising materials mentioning features or use of this software
     17  1.1.2.1  matt  *    must display the following acknowledgement:
     18  1.1.2.1  matt  *	This product includes software developed by the NetBSD
     19  1.1.2.1  matt  *	Foundation, Inc. and its contributors.
     20  1.1.2.1  matt  * 4. Neither the name of The NetBSD Foundation nor the names of its
     21  1.1.2.1  matt  *    contributors may be used to endorse or promote products derived
     22  1.1.2.1  matt  *    from this software without specific prior written permission.
     23  1.1.2.1  matt  *
     24  1.1.2.1  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     25  1.1.2.1  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  1.1.2.1  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     27  1.1.2.1  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     28  1.1.2.1  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29  1.1.2.1  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30  1.1.2.1  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31  1.1.2.1  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32  1.1.2.1  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33  1.1.2.1  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34  1.1.2.1  matt  * POSSIBILITY OF SUCH DAMAGE.
     35  1.1.2.1  matt  */
     36  1.1.2.1  matt 
     37  1.1.2.1  matt #include <sys/cdefs.h>
     38  1.1.2.1  matt __KERNEL_RCSID(0, "$NetBSD");
     39  1.1.2.1  matt 
     40  1.1.2.1  matt #include <sys/types.h>
     41  1.1.2.1  matt #include <sys/param.h>
     42  1.1.2.1  matt #include <sys/systm.h>
     43  1.1.2.1  matt #include <sys/kernel.h>
     44  1.1.2.1  matt #include <sys/time.h>
     45  1.1.2.1  matt #include <sys/device.h>
     46  1.1.2.1  matt 
     47  1.1.2.1  matt #include <machine/bus.h>
     48  1.1.2.1  matt #include <machine/intr.h>
     49  1.1.2.1  matt 
     50  1.1.2.1  matt #include <arm/cpufunc.h>
     51  1.1.2.1  matt 
     52  1.1.2.1  matt #include <arm/at91/at91reg.h>
     53  1.1.2.1  matt #include <arm/at91/at91var.h>
     54  1.1.2.1  matt #include <arm/at91/at91pmcreg.h>
     55  1.1.2.1  matt #include <arm/at91/at91pmcvar.h>
     56  1.1.2.1  matt 
     57  1.1.2.1  matt #define	SLOW_CLOCK	32768LU
     58  1.1.2.1  matt 
     59  1.1.2.1  matt void
     60  1.1.2.1  matt at91pmc_get_clocks(struct at91bus_clocks *clocks)
     61  1.1.2.1  matt {
     62  1.1.2.1  matt 	u_int64_t		mclk, pllaclk, pllbclk, pclk, mstclk;
     63  1.1.2.1  matt 	u_int32_t		reg;
     64  1.1.2.1  matt 
     65  1.1.2.1  matt 	if (!((reg = PMCREG(PMC_MOR)) & PMC_MOR_MOSCEN))
     66  1.1.2.1  matt 		panic("%s: main oscillator not enabled (MOR=0x%#X)", __FUNCTION__, reg);
     67  1.1.2.1  matt 
     68  1.1.2.1  matt 	if (!((reg = PMCREG(PMC_MCFR)) & PMC_MCFR_MAINRDY))
     69  1.1.2.1  matt 		panic("%s: main oscillator not ready (MCFR=0x%#X)", __FUNCTION__, reg);
     70  1.1.2.1  matt 
     71  1.1.2.1  matt 	mclk  = ((reg & PMC_MCFR_MAINF) * SLOW_CLOCK) / 16U;
     72  1.1.2.1  matt 
     73  1.1.2.1  matt 	// try to guess some nice MHz value
     74  1.1.2.1  matt 	if (((mclk / 1000) % 1000) >= 990) {
     75  1.1.2.1  matt 	  mclk += 1000000U - (mclk % 1000000U);
     76  1.1.2.1  matt 	} else if (((mclk / 1000) % 1000) <= 10) {
     77  1.1.2.1  matt 	  mclk -= (mclk % 1000000U);
     78  1.1.2.1  matt 	}
     79  1.1.2.1  matt 
     80  1.1.2.1  matt 	reg = PMCREG(PMC_PLLAR); pllaclk = 0;
     81  1.1.2.1  matt 	if (reg & PMC_PLL_DIV) {
     82  1.1.2.1  matt 		pllaclk = mclk * (((reg & PMC_PLL_MUL) >> PMC_PLL_MUL_SHIFT) + 1);
     83  1.1.2.1  matt 		pllaclk /= (reg & PMC_PLL_DIV) >> PMC_PLL_DIV_SHIFT;
     84  1.1.2.1  matt 	}
     85  1.1.2.1  matt 
     86  1.1.2.1  matt 	reg = PMCREG(PMC_PLLBR); pllbclk = 0;
     87  1.1.2.1  matt 	if (reg & PMC_PLL_DIV) {
     88  1.1.2.1  matt 		pllbclk = mclk * (((reg & PMC_PLL_MUL) >> PMC_PLL_MUL_SHIFT) + 1);
     89  1.1.2.1  matt 		pllbclk /= (reg & PMC_PLL_DIV) >> PMC_PLL_DIV_SHIFT;
     90  1.1.2.1  matt 		if (reg & PMC_PLLBR_USB_96M) {
     91  1.1.2.1  matt 			pllbclk /= 2;
     92  1.1.2.1  matt 		}
     93  1.1.2.1  matt 	}
     94  1.1.2.1  matt 
     95  1.1.2.1  matt 	reg = PMCREG(PMC_MCKR);
     96  1.1.2.1  matt 	switch ((reg & PMC_MCKR_CSS)) {
     97  1.1.2.1  matt 	case PMC_MCKR_CSS_SLOW_CLK:
     98  1.1.2.1  matt 	  pclk = SLOW_CLOCK;
     99  1.1.2.1  matt 	  break;
    100  1.1.2.1  matt 	default:
    101  1.1.2.1  matt 	case PMC_MCKR_CSS_MAIN_CLK:
    102  1.1.2.1  matt 	  pclk = mclk;
    103  1.1.2.1  matt 	  break;
    104  1.1.2.1  matt 	case PMC_MCKR_CSS_PLLA:
    105  1.1.2.1  matt 	  pclk = pllaclk;
    106  1.1.2.1  matt 	  break;
    107  1.1.2.1  matt 	case PMC_MCKR_CSS_PLLB:
    108  1.1.2.1  matt 	  pclk = pllbclk;
    109  1.1.2.1  matt 	  break;
    110  1.1.2.1  matt 	}
    111  1.1.2.1  matt 	pclk >>= (reg & PMC_MCKR_PRES) >> PMC_MCKR_PRES_SHIFT;
    112  1.1.2.1  matt 	mstclk = pclk / (((reg & PMC_MCKR_MDIV) >> PMC_MCKR_MDIV_SHIFT) + 1);
    113  1.1.2.1  matt 
    114  1.1.2.1  matt 	clocks->slow = SLOW_CLOCK;
    115  1.1.2.1  matt 	clocks->main = mclk;
    116  1.1.2.1  matt 	clocks->cpu = pclk;
    117  1.1.2.1  matt 	clocks->master = mstclk;
    118  1.1.2.1  matt 	clocks->plla = pllaclk;
    119  1.1.2.1  matt 	clocks->pllb = pllbclk;
    120  1.1.2.1  matt }
    121  1.1.2.1  matt 
    122  1.1.2.1  matt 
    123  1.1.2.1  matt #define	PID_COUNT	32
    124  1.1.2.1  matt static int pid_enable_count[PID_COUNT] = {0};
    125  1.1.2.1  matt 
    126  1.1.2.1  matt void
    127  1.1.2.1  matt at91pmc_peripheral_clock(int pid, int enable)
    128  1.1.2.1  matt {
    129  1.1.2.1  matt 	int s;
    130  1.1.2.1  matt 
    131  1.1.2.1  matt 	if (pid < 0 || pid >= PID_COUNT)
    132  1.1.2.1  matt 		panic("%s: pid %d out of range", __FUNCTION__, pid);
    133  1.1.2.1  matt 
    134  1.1.2.1  matt 	s = splhigh();
    135  1.1.2.1  matt 
    136  1.1.2.1  matt 	if (enable) {
    137  1.1.2.1  matt 		pid_enable_count[pid]++;
    138  1.1.2.1  matt 		PMCREG(PMC_PCER) = (1U << pid);
    139  1.1.2.1  matt 	} else {
    140  1.1.2.1  matt 		if (--pid_enable_count[pid] < 0)
    141  1.1.2.1  matt 			panic("%s: pid %d enable count got negative (%d)",
    142  1.1.2.1  matt 			      __FUNCTION__, pid, pid_enable_count[pid]);
    143  1.1.2.1  matt 		if (pid_enable_count[pid] == 0)
    144  1.1.2.1  matt 			PMCREG(PMC_PCDR) = (1U << pid);
    145  1.1.2.1  matt 	}
    146  1.1.2.1  matt 
    147  1.1.2.1  matt 	splx(s);
    148  1.1.2.1  matt 
    149  1.1.2.1  matt 	if (enable) {
    150  1.1.2.1  matt 		int c;
    151  1.1.2.1  matt 		for (c = 0; c < 10000; c++) {
    152  1.1.2.1  matt 		  __insn_barrier();
    153  1.1.2.1  matt 		}
    154  1.1.2.1  matt 	}
    155  1.1.2.1  matt }
    156