063281e3f7a8b24192925ad8aa832d90802d1353
[openwrt/openwrt.git] / target / linux / adm5120 / files-3.18 / arch / mips / adm5120 / prom / bootbase.c
1 /*
2 * ZyXEL's Bootbase specific prom routines
3 *
4 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 */
11
12 #include <linux/types.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16
17 #include <asm/bootinfo.h>
18 #include <asm/addrspace.h>
19 #include <asm/byteorder.h>
20
21 #include <asm/mach-adm5120/adm5120_defs.h>
22 #include <prom/zynos.h>
23 #include "prom_read.h"
24
25 #define ZYNOS_INFO_ADDR KSEG1ADDR(ADM5120_SRAM0_BASE+0x3F90)
26 #define ZYNOS_HDBG_ADDR KSEG1ADDR(ADM5120_SRAM0_BASE+0x4000)
27 #define BOOTEXT_ADDR_MIN KSEG1ADDR(ADM5120_SRAM0_BASE)
28 #define BOOTEXT_ADDR_MAX (BOOTEXT_ADDR_MIN + (2*1024*1024))
29
30 static int bootbase_found;
31 static struct zynos_board_info *board_info;
32
33 struct bootbase_info bootbase_info;
34
35 static inline int bootbase_dbgarea_present(u8 *data)
36 {
37 u32 t;
38
39 t = prom_read_be32(data+5);
40 if (t != ZYNOS_MAGIC_DBGAREA1)
41 return 0;
42
43 t = prom_read_be32(data+9);
44 if (t != ZYNOS_MAGIC_DBGAREA2)
45 return 0;
46
47 return 1;
48 }
49
50 static inline u32 bootbase_get_bootext_addr(void)
51 {
52 return prom_read_be32(&board_info->bootext_addr);
53 }
54
55 static inline void bootbase_get_mac(u8 *mac)
56 {
57 int i;
58
59 for (i = 0; i < 6; i++)
60 mac[i] = board_info->mac[i];
61 }
62
63 static inline u16 bootbase_get_vendor_id(void)
64 {
65 #define CHECK_VENDOR(n) (strnicmp(board_info->vendor, (n), strlen(n)) == 0)
66 unsigned char vendor[ZYNOS_NAME_LEN];
67 int i;
68
69 for (i = 0; i < ZYNOS_NAME_LEN; i++)
70 vendor[i] = board_info->vendor[i];
71
72 if CHECK_VENDOR(ZYNOS_VENDOR_ZYXEL)
73 return ZYNOS_VENDOR_ID_ZYXEL;
74
75 if CHECK_VENDOR(ZYNOS_VENDOR_DLINK)
76 return ZYNOS_VENDOR_ID_DLINK;
77
78 if CHECK_VENDOR(ZYNOS_VENDOR_LUCENT)
79 return ZYNOS_VENDOR_ID_LUCENT;
80
81 if CHECK_VENDOR(ZYNOS_VENDOR_NETGEAR)
82 return ZYNOS_VENDOR_ID_NETGEAR;
83
84 return ZYNOS_VENDOR_ID_OTHER;
85 }
86
87 static inline u16 bootbase_get_board_id(void)
88 {
89 return prom_read_be16(&board_info->board_id);
90 }
91
92 int __init bootbase_present(void)
93 {
94 u32 t;
95
96 if (bootbase_found)
97 goto out;
98
99 /* check presence of the dbgarea */
100 if (bootbase_dbgarea_present((u8 *)ZYNOS_HDBG_ADDR) == 0)
101 goto out;
102
103 board_info = (struct zynos_board_info *)(ZYNOS_INFO_ADDR);
104
105 /* check for a valid BootExt address */
106 t = bootbase_get_bootext_addr();
107 if ((t < BOOTEXT_ADDR_MIN) || (t > BOOTEXT_ADDR_MAX))
108 goto out;
109
110 bootbase_info.vendor_id = bootbase_get_vendor_id();
111 bootbase_info.board_id = bootbase_get_board_id();
112 bootbase_get_mac(bootbase_info.mac);
113
114 bootbase_found = 1;
115
116 out:
117 return bootbase_found;
118 }
119