Home | History | Annotate | Line # | Download | only in atheros
ar5312.c revision 1.1
      1  1.1  gdamore /* $NetBSD: ar5312.c,v 1.1 2006/08/28 07:21:15 gdamore Exp $ */
      2  1.1  gdamore 
      3  1.1  gdamore /*
      4  1.1  gdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5  1.1  gdamore  * Copyright (c) 2006 Garrett D'Amore.
      6  1.1  gdamore  * All rights reserved.
      7  1.1  gdamore  *
      8  1.1  gdamore  * Portions of this code were written by Garrett D'Amore for the
      9  1.1  gdamore  * Champaign-Urbana Community Wireless Network Project.
     10  1.1  gdamore  *
     11  1.1  gdamore  * Redistribution and use in source and binary forms, with or
     12  1.1  gdamore  * without modification, are permitted provided that the following
     13  1.1  gdamore  * conditions are met:
     14  1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     15  1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     16  1.1  gdamore  * 2. Redistributions in binary form must reproduce the above
     17  1.1  gdamore  *    copyright notice, this list of conditions and the following
     18  1.1  gdamore  *    disclaimer in the documentation and/or other materials provided
     19  1.1  gdamore  *    with the distribution.
     20  1.1  gdamore  * 3. All advertising materials mentioning features or use of this
     21  1.1  gdamore  *    software must display the following acknowledgements:
     22  1.1  gdamore  *      This product includes software developed by the Urbana-Champaign
     23  1.1  gdamore  *      Independent Media Center.
     24  1.1  gdamore  *	This product includes software developed by Garrett D'Amore.
     25  1.1  gdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26  1.1  gdamore  *    D'Amore's name may not be used to endorse or promote products
     27  1.1  gdamore  *    derived from this software without specific prior written permission.
     28  1.1  gdamore  *
     29  1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30  1.1  gdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31  1.1  gdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32  1.1  gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  1.1  gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34  1.1  gdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35  1.1  gdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36  1.1  gdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37  1.1  gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38  1.1  gdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  1.1  gdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41  1.1  gdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42  1.1  gdamore  */
     43  1.1  gdamore 
     44  1.1  gdamore /*
     45  1.1  gdamore  * This file includes a bunch of implementation specific bits for
     46  1.1  gdamore  * AR5312, which differents these from other members of the AR5315
     47  1.1  gdamore  * family.
     48  1.1  gdamore  */
     49  1.1  gdamore #include "opt_ddb.h"
     50  1.1  gdamore #include "opt_kgdb.h"
     51  1.1  gdamore 
     52  1.1  gdamore #include "opt_memsize.h"
     53  1.1  gdamore #include <sys/param.h>
     54  1.1  gdamore #include <sys/systm.h>
     55  1.1  gdamore #include <sys/kernel.h>
     56  1.1  gdamore #include <sys/buf.h>
     57  1.1  gdamore 
     58  1.1  gdamore #include <mips/cache.h>
     59  1.1  gdamore #include <mips/locore.h>
     60  1.1  gdamore #include <mips/cpuregs.h>
     61  1.1  gdamore 
     62  1.1  gdamore #include <mips/atheros/include/ar5312reg.h>
     63  1.1  gdamore #include <mips/atheros/include/ar531xvar.h>
     64  1.1  gdamore #include <mips/atheros/include/arbusvar.h>
     65  1.1  gdamore #include "com.h"
     66  1.1  gdamore 
     67  1.1  gdamore uint32_t
     68  1.1  gdamore ar531x_memsize(void)
     69  1.1  gdamore {
     70  1.1  gdamore 	uint32_t memsize;
     71  1.1  gdamore 	uint32_t memcfg, bank0, bank1;
     72  1.1  gdamore 
     73  1.1  gdamore 	/*
     74  1.1  gdamore 	 * Determine the memory size.  Use the `memsize' PMON
     75  1.1  gdamore 	 * variable.  If that's not available, panic.
     76  1.1  gdamore 	 *
     77  1.1  gdamore 	 * NB: we allow compile time override
     78  1.1  gdamore 	 */
     79  1.1  gdamore #if defined(MEMSIZE)
     80  1.1  gdamore 	memsize = MEMSIZE;
     81  1.1  gdamore #else
     82  1.1  gdamore 	memcfg = GETSDRAMREG(AR5312_SDRAMCTL_MEM_CFG1);
     83  1.1  gdamore 	bank0 = (memcfg & AR5312_MEM_CFG1_BANK0_MASK) >>
     84  1.1  gdamore 	    AR5312_MEM_CFG1_BANK0_SHIFT;
     85  1.1  gdamore 	bank1 = (memcfg & AR5312_MEM_CFG1_BANK1_MASK) >>
     86  1.1  gdamore 	    AR5312_MEM_CFG1_BANK1_SHIFT;
     87  1.1  gdamore 
     88  1.1  gdamore 	memsize = (bank0 ? (1 << (bank0 + 1)) : 0) +
     89  1.1  gdamore 	    (bank1 ? (1 << (bank1 + 1)) : 0);
     90  1.1  gdamore 	memsize <<= 20;
     91  1.1  gdamore #endif
     92  1.1  gdamore 
     93  1.1  gdamore 	return (memsize);
     94  1.1  gdamore }
     95  1.1  gdamore 
     96  1.1  gdamore void
     97  1.1  gdamore ar531x_wdog(uint32_t period)
     98  1.1  gdamore {
     99  1.1  gdamore 
    100  1.1  gdamore 	if (period == 0) {
    101  1.1  gdamore 		PUTSYSREG(AR5312_SYSREG_WDOG_CTL, AR5312_WDOG_CTL_IGNORE);
    102  1.1  gdamore 		PUTSYSREG(AR5312_SYSREG_WDOG_TIMER, 0);
    103  1.1  gdamore 	} else {
    104  1.1  gdamore 		PUTSYSREG(AR5312_SYSREG_WDOG_TIMER, period);
    105  1.1  gdamore 		PUTSYSREG(AR5312_SYSREG_WDOG_CTL, AR5312_WDOG_CTL_RESET);
    106  1.1  gdamore 	}
    107  1.1  gdamore }
    108  1.1  gdamore 
    109  1.1  gdamore const char *
    110  1.1  gdamore ar531x_cpuname(void)
    111  1.1  gdamore {
    112  1.1  gdamore 	uint32_t	revision;
    113  1.1  gdamore 
    114  1.1  gdamore 	revision = GETSYSREG(AR5312_SYSREG_REVISION);
    115  1.1  gdamore 	switch (AR5312_REVISION_MAJOR(revision)) {
    116  1.1  gdamore 	case AR5312_REVISION_MAJ_AR5311:
    117  1.1  gdamore 		return ("Atheros AR5311");
    118  1.1  gdamore 	case AR5312_REVISION_MAJ_AR5312:
    119  1.1  gdamore 		return ("Atheros AR5312");
    120  1.1  gdamore 	case AR5312_REVISION_MAJ_AR2313:
    121  1.1  gdamore 		return ("Atheros AR2313");
    122  1.1  gdamore 	case AR5312_REVISION_MAJ_AR5315:
    123  1.1  gdamore 		return ("Atheros AR5315");
    124  1.1  gdamore 	default:
    125  1.1  gdamore 		return ("Atheros AR531X");
    126  1.1  gdamore 	}
    127  1.1  gdamore }
    128  1.1  gdamore 
    129  1.1  gdamore void
    130  1.1  gdamore ar531x_consinit(void)
    131  1.1  gdamore {
    132  1.1  gdamore 	/*
    133  1.1  gdamore 	 * Everything related to console initialization is done
    134  1.1  gdamore 	 * in mach_init().
    135  1.1  gdamore 	 */
    136  1.1  gdamore #if NCOM > 0
    137  1.1  gdamore 	/* Setup polled serial for early console I/O */
    138  1.1  gdamore 	/* XXX: pass in CONSPEED? */
    139  1.1  gdamore 	com_arbus_cnattach(AR5312_UART0_BASE);
    140  1.1  gdamore #else
    141  1.1  gdamore 	panic("Not configured to use serial console!\n");
    142  1.1  gdamore 	/* not going to see that message now, are we? */
    143  1.1  gdamore #endif
    144  1.1  gdamore }
    145  1.1  gdamore 
    146  1.1  gdamore void
    147  1.1  gdamore ar531x_businit(void)
    148  1.1  gdamore 
    149  1.1  gdamore {
    150  1.1  gdamore 	/*
    151  1.1  gdamore 	 * Clear previous AHB errors
    152  1.1  gdamore 	 */
    153  1.1  gdamore 	GETSYSREG(AR5312_SYSREG_AHBPERR);
    154  1.1  gdamore 	GETSYSREG(AR5312_SYSREG_AHBDMAE);
    155  1.1  gdamore }
    156  1.1  gdamore 
    157  1.1  gdamore uint32_t
    158  1.1  gdamore ar531x_cpu_freq(void)
    159  1.1  gdamore {
    160  1.1  gdamore 	static uint32_t	cpufreq;
    161  1.1  gdamore 	uint32_t	wisoc = GETSYSREG(AR5312_SYSREG_REVISION);
    162  1.1  gdamore 
    163  1.1  gdamore 	uint32_t	predivmask;
    164  1.1  gdamore 	uint32_t	predivshift;
    165  1.1  gdamore 	uint32_t	multmask;
    166  1.1  gdamore 	uint32_t	multshift;
    167  1.1  gdamore 	uint32_t	doublermask;
    168  1.1  gdamore 	uint32_t	divisor;
    169  1.1  gdamore 	uint32_t	multiplier;
    170  1.1  gdamore 	uint32_t	clockctl;
    171  1.1  gdamore 
    172  1.1  gdamore 	const int	predivide_table[4] = { 1, 2, 4, 5 };
    173  1.1  gdamore 
    174  1.1  gdamore 	/* XXX: in theory we might be able to get clock from bootrom */
    175  1.1  gdamore 
    176  1.1  gdamore 	/*
    177  1.1  gdamore 	 * This logic looks at the clock control register and
    178  1.1  gdamore 	 * determines the actual CPU frequency.  These parts lack any
    179  1.1  gdamore 	 * kind of real-time clock on them, but the cpu clocks should
    180  1.1  gdamore 	 * be very accurate -- WiFi requires usec resolution timers.
    181  1.1  gdamore 	 */
    182  1.1  gdamore 
    183  1.1  gdamore 	if (cpufreq) {
    184  1.1  gdamore 		return cpufreq;
    185  1.1  gdamore 	}
    186  1.1  gdamore 
    187  1.1  gdamore 	if (AR5312_REVISION_MAJOR(wisoc) == AR5312_REVISION_MAJ_AR2313) {
    188  1.1  gdamore 		predivmask = AR2313_CLOCKCTL_PREDIVIDE_MASK;
    189  1.1  gdamore 		predivshift = AR2313_CLOCKCTL_PREDIVIDE_SHIFT;
    190  1.1  gdamore 		multmask = AR2313_CLOCKCTL_MULTIPLIER_MASK;
    191  1.1  gdamore 		multshift = AR2313_CLOCKCTL_MULTIPLIER_SHIFT;
    192  1.1  gdamore 		doublermask = AR2313_CLOCKCTL_DOUBLER_MASK;
    193  1.1  gdamore 	} else {
    194  1.1  gdamore 		predivmask = AR5312_CLOCKCTL_PREDIVIDE_MASK;
    195  1.1  gdamore 		predivshift = AR5312_CLOCKCTL_PREDIVIDE_SHIFT;
    196  1.1  gdamore 		multmask = AR5312_CLOCKCTL_MULTIPLIER_MASK;
    197  1.1  gdamore 		multshift = AR5312_CLOCKCTL_MULTIPLIER_SHIFT;
    198  1.1  gdamore 		doublermask = AR5312_CLOCKCTL_DOUBLER_MASK;
    199  1.1  gdamore 	}
    200  1.1  gdamore 
    201  1.1  gdamore 	/*
    202  1.1  gdamore 	 * Note that the source clock involved here is a 40MHz.
    203  1.1  gdamore 	 */
    204  1.1  gdamore 
    205  1.1  gdamore 	clockctl = GETSYSREG(AR5312_SYSREG_CLOCKCTL);
    206  1.1  gdamore 	divisor = predivide_table[(clockctl & predivmask) >> predivshift];
    207  1.1  gdamore 	multiplier = (clockctl & multmask) >> multshift;
    208  1.1  gdamore 
    209  1.1  gdamore 	if (clockctl & doublermask)
    210  1.1  gdamore 		multiplier <<= 1;
    211  1.1  gdamore 
    212  1.1  gdamore 	cpufreq = (40000000 / divisor) * multiplier;
    213  1.1  gdamore 
    214  1.1  gdamore 	return (cpufreq);
    215  1.1  gdamore }
    216  1.1  gdamore 
    217  1.1  gdamore uint32_t
    218  1.1  gdamore ar531x_sys_freq(void)
    219  1.1  gdamore {
    220  1.1  gdamore 	return (ar531x_cpu_freq() / 4);
    221  1.1  gdamore }
    222