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