d05217c615f2bb40607945008e36c2500b82f65c
[openwrt.git] / target / linux / ar71xx / files / arch / mips / ar71xx / prom.c
1 /*
2  *  Atheros AR71xx SoC specific prom routines
3  *
4  *  Copyright (C) 2008 Gabor Juhos <juhosg@openwrt.org>
5  *  Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6  *
7  *  This program is free software; you can redistribute it and/or modify it
8  *  under the terms of the GNU General Public License version 2 as published
9  *  by the Free Software Foundation.
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/io.h>
15 #include <linux/serial_reg.h>
16
17 #include <asm/bootinfo.h>
18 #include <asm/addrspace.h>
19 #include <asm/fw/myloader/myloader.h>
20
21 #include <asm/mach-ar71xx/ar71xx.h>
22 #include <asm/mach-ar71xx/platform.h>
23
24 struct board_rec {
25         char            *name;
26         unsigned long   mach_type;
27 };
28
29 static int ar71xx_prom_argc __initdata;
30 static char **ar71xx_prom_argv __initdata;
31 static char **ar71xx_prom_envp __initdata;
32
33 static struct board_rec boards[] __initdata = {
34         {
35                 .name           = "411",
36                 .mach_type      = AR71XX_MACH_RB_411,
37         }, {
38                 .name           = "433",
39                 .mach_type      = AR71XX_MACH_RB_433,
40         }, {
41                 .name           = "450",
42                 .mach_type      = AR71XX_MACH_RB_450,
43         }, {
44                 .name           = "493",
45                 .mach_type      = AR71XX_MACH_RB_493,
46         }, {
47                 .name           = "AW-NR580",
48                 .mach_type      = AR71XX_MACH_AW_NR580,
49         }, {
50                 .name           = "AP83",
51                 .mach_type      = AR71XX_MACH_AP83,
52         }, {
53                 .name           = "TEW-632BRP",
54                 .mach_type      = AR71XX_MACH_TEW_632BRP,
55         }, {
56                 .name           = "UBNT-RS",
57                 .mach_type      = AR71XX_MACH_UBNT_RS,
58         }, {
59                 .name           = "UBNT-LSX",
60                 .mach_type      = AR71XX_MACH_UBNT_LSX,
61         }, {
62                 .name           = "WNR2000",
63                 .mach_type      = AR71XX_MACH_WNR2000,
64         }, {
65                 .name           = "PB42",
66                 .mach_type      = AR71XX_MACH_PB42,
67         }, {
68                 .name           = "MZK-W300NH",
69                 .mach_type      = AR71XX_MACH_MZK_W300NH,
70         }
71 };
72
73 static __init char *ar71xx_prom_getargv(const char *name)
74 {
75         int len = strlen(name);
76         int i;
77
78         if (!ar71xx_prom_argv)
79                 return NULL;
80
81         for (i = 0; i < ar71xx_prom_argc; i++) {
82                 char *argv = ar71xx_prom_argv[i];
83
84                 if (!argv)
85                         continue;
86
87                 if (strncmp(name, argv, len) == 0 && (argv)[len] == '=')
88                         return argv + len + 1;
89         }
90
91         return NULL;
92 }
93
94 static __init char *ar71xx_prom_getenv(const char *envname)
95 {
96         int len = strlen(envname);
97         char **env;
98
99         if (!ar71xx_prom_envp)
100                 return NULL;
101
102         for (env = ar71xx_prom_envp; *env != NULL; env++)
103                 if (strncmp(envname, *env, len) == 0 && (*env)[len] == '=')
104                         return *env + len + 1;
105
106         return NULL;
107 }
108
109 static __init unsigned long find_board_byname(char *name)
110 {
111         int i;
112
113         for (i = 0; i < ARRAY_SIZE(boards); i++)
114                 if (strcmp(name, boards[i].name) == 0)
115                         return boards[i].mach_type;
116
117         return AR71XX_MACH_GENERIC;
118 }
119
120 static int ar71xx_prom_init_myloader(void)
121 {
122         struct myloader_info *mylo;
123
124         mylo = myloader_get_info();
125         if (!mylo)
126                 return 0;
127
128         switch (mylo->did) {
129         case DEVID_COMPEX_WP543:
130                 ar71xx_mach_type = AR71XX_MACH_WP543;
131                 break;
132         default:
133                 printk(KERN_WARNING "prom: unknown device id: %x\n",
134                                 mylo->did);
135         }
136         ar71xx_set_mac_base(mylo->macs[0]);
137
138         return 1;
139 }
140
141 static void ar71xx_prom_init_generic(void)
142 {
143         char *p;
144
145         ar71xx_prom_argc = fw_arg0;
146         ar71xx_prom_argv = (char **)fw_arg1;
147         ar71xx_prom_envp = (char **)fw_arg2;
148
149         p = ar71xx_prom_getenv("board");
150         if (!p)
151                 p = ar71xx_prom_getargv("board");
152         if (p)
153                 ar71xx_mach_type = find_board_byname(p);
154
155         p = ar71xx_prom_getenv("ethaddr");
156         if (!p)
157                 p = ar71xx_prom_getargv("kmac");
158         if (p)
159                 ar71xx_parse_mac_addr(p);
160 }
161
162 void __init prom_init(void)
163 {
164         printk(KERN_DEBUG "prom: fw_arg0=%08x, fw_arg1=%08x, "
165                         "fw_arg2=%08x, fw_arg3=%08x\n",
166                         (unsigned int)fw_arg0, (unsigned int)fw_arg1,
167                         (unsigned int)fw_arg2, (unsigned int)fw_arg3);
168
169         ar71xx_mach_type = AR71XX_MACH_GENERIC;
170
171         if (ar71xx_prom_init_myloader())
172                 return;
173
174         ar71xx_prom_init_generic();
175 }
176
177 void __init prom_free_prom_memory(void)
178 {
179         /* We do not have to prom memory to free */
180 }
181
182 #define UART_READ(r) \
183         __raw_readl((void __iomem *)(KSEG1ADDR(AR71XX_UART_BASE) + 4 * (r)))
184
185 #define UART_WRITE(r, v) \
186         __raw_writel((v), (void __iomem *)(KSEG1ADDR(AR71XX_UART_BASE) + 4*(r)))
187
188 void prom_putchar(unsigned char ch)
189 {
190         while (((UART_READ(UART_LSR)) & UART_LSR_THRE) == 0);
191         UART_WRITE(UART_TX, ch);
192         while (((UART_READ(UART_LSR)) & UART_LSR_THRE) == 0);
193 }