Home | History | Annotate | Line # | Download | only in atheros
      1 /* $NetBSD: ar5312_board.c,v 1.5 2015/06/09 22:50:50 matt Exp $ */
      2 /*
      3  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      4  * Copyright (c) 2006 Garrett D'Amore.
      5  * All rights reserved.
      6  *
      7  * This code was written by Garrett D'Amore for the Champaign-Urbana
      8  * Community Wireless Network Project.
      9  *
     10  * Redistribution and use in source and binary forms, with or
     11  * without modification, are permitted provided that the following
     12  * conditions are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above
     16  *    copyright notice, this list of conditions and the following
     17  *    disclaimer in the documentation and/or other materials provided
     18  *    with the distribution.
     19  * 3. All advertising materials mentioning features or use of this
     20  *    software must display the following acknowledgements:
     21  *      This product includes software developed by the Urbana-Champaign
     22  *      Independent Media Center.
     23  *	This product includes software developed by Garrett D'Amore.
     24  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     25  *    D'Amore's name may not be used to endorse or promote products
     26  *    derived from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     29  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     30  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     31  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     33  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     35  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     36  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     37  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     38  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     40  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41  */
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: ar5312_board.c,v 1.5 2015/06/09 22:50:50 matt Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/bus.h>
     47 #include <sys/device.h>
     48 #include <sys/systm.h>
     49 
     50 #include <mips/cpuregs.h>
     51 
     52 #include <mips/atheros/include/ar5312reg.h>
     53 #include <mips/atheros/include/platform.h>
     54 
     55 #include <ah_soc.h>
     56 
     57 extern const char *ether_sprintf(const uint8_t *);
     58 
     59 /*
     60  * Locate the Board Configuration data using heuristics.
     61  * Search backward from the (aliased) end of flash looking
     62  * for the signature string that marks the start of the data.
     63  * We search at most 500KB.
     64  */
     65 static const struct ar531x_boarddata *
     66 ar5312_get_board_info(void)
     67 {
     68 	static const struct ar531x_boarddata *board = NULL;
     69 	const uint8_t *ptr, *end;
     70 	uint32_t fctl;
     71 
     72 	if (board == NULL) {
     73 		/* configure flash bank 0 */
     74 		fctl = REGVAL(AR5312_FLASHCTL_BASE + AR5312_FLASHCTL_0) &
     75 		    AR5312_FLASHCTL_MW;
     76 
     77 		fctl |=
     78 		    AR5312_FLASHCTL_E |
     79 		    AR5312_FLASHCTL_RBLE |
     80 		    AR5312_FLASHCTL_AC_8M |
     81 		    __SHIFTIN(1, AR5312_FLASHCTL_IDCY) |
     82 		    __SHIFTIN(7, AR5312_FLASHCTL_WST1) |
     83 		    __SHIFTIN(7, AR5312_FLASHCTL_WST2);
     84 
     85 		REGVAL(AR5312_FLASHCTL_BASE + AR5312_FLASHCTL_0) = fctl;
     86 
     87 		REGVAL(AR5312_FLASHCTL_BASE + AR5312_FLASHCTL_1) &=
     88 		    ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC);
     89 
     90 		REGVAL(AR5312_FLASHCTL_BASE + AR5312_FLASHCTL_2) &=
     91 		    ~(AR5312_FLASHCTL_E | AR5312_FLASHCTL_AC);
     92 
     93 		/* search backward in the flash looking for the signature */
     94 		ptr = (const uint8_t *) MIPS_PHYS_TO_KSEG1(AR5312_FLASH_END - 0x1000);
     95 		end = ptr - (500 * 1024);	/* NB: max 500KB window */
     96 		/* XXX validate end */
     97 		for (; ptr > end; ptr -= 0x1000)
     98 			if (*(const uint32_t *)ptr == AR531X_BD_MAGIC) {
     99 				board = (const struct ar531x_boarddata *) ptr;
    100 				break;
    101 			}
    102 	}
    103 	return board;
    104 }
    105 
    106 /*
    107  * Locate the radio configuration data; it is located relative
    108  * to the board configuration data.
    109  */
    110 static const void *
    111 ar5312_get_radio_info(void)
    112 {
    113 	static const void *radio = NULL;
    114 	const struct ar531x_boarddata *board;
    115 	const uint8_t *baddr, *ptr, *end;
    116 
    117 	if (radio == NULL) {
    118 		board = ar5312_get_board_info();
    119 		if (board == NULL)
    120 			return NULL;
    121 		baddr = (const uint8_t *) board;
    122 		ptr = baddr + 0x1000;
    123 		end = (const uint8_t *)
    124 		    MIPS_PHYS_TO_KSEG1(AR5312_FLASH_END-0x1000);
    125 	again:
    126 		for (; ptr < end; ptr += 0x1000)
    127 			if (*(const uint32_t *)ptr != 0xffffffff) {
    128 				radio = ptr;
    129 				goto done;
    130 			}
    131 		/* sort of an Algol-style for loop ... */
    132 		if (end == (uint8_t *) MIPS_PHYS_TO_KSEG1(AR5312_FLASH_END)) {
    133 			/* NB: AR2316 has radio data in a different location */
    134 			ptr = baddr + 0xf8;
    135 			end = (const uint8_t *)
    136 			    MIPS_PHYS_TO_KSEG1(AR5312_FLASH_END-0x1000 + 0xf8);
    137 			goto again;
    138 		}
    139 	}
    140 done:
    141 	return radio;
    142 }
    143 
    144 const struct atheros_boardsw ar5312_boardsw = {
    145 	.absw_get_board_info = ar5312_get_board_info,
    146 	.absw_get_radio_info = ar5312_get_radio_info,
    147 };
    148