ramips: rt305x: fix CPU clock detection on RT3352
[openwrt.git] / target / linux / ramips / files / arch / mips / ralink / rt305x / rt305x.c
1 /*
2  * Ralink RT305x SoC specific setup
3  *
4  * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
5  * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6  *
7  * Parts of this file are based on Ralink's 2.6.21 BSP
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as published
11  * by the Free Software Foundation.
12  */
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17
18 #include <asm/mipsregs.h>
19
20 #include <asm/mach-ralink/common.h>
21 #include <asm/mach-ralink/ramips_gpio.h>
22 #include <asm/mach-ralink/rt305x.h>
23 #include <asm/mach-ralink/rt305x_regs.h>
24
25 void __iomem * rt305x_sysc_base;
26 void __iomem * rt305x_memc_base;
27 enum rt305x_soc_type rt305x_soc;
28
29 void __init ramips_soc_prom_init(void)
30 {
31         void __iomem *sysc = (void __iomem *) KSEG1ADDR(RT305X_SYSC_BASE);
32         const char *name = "unknown";
33         u32 n0;
34         u32 n1;
35         u32 id;
36
37         n0 = __raw_readl(sysc + SYSC_REG_CHIP_NAME0);
38         n1 = __raw_readl(sysc + SYSC_REG_CHIP_NAME1);
39
40         if (n0 == RT3052_CHIP_NAME0 && n1 == RT3052_CHIP_NAME1) {
41                 unsigned long icache_sets;
42
43                 icache_sets = (read_c0_config1() >> 22) & 7;
44                 if (icache_sets == 1) {
45                         rt305x_soc = RT305X_SOC_RT3050;
46                         name = "RT3050";
47                 } else {
48                         rt305x_soc = RT305X_SOC_RT3052;
49                         name = "RT3052";
50                 }
51         } else if (n0 == RT3350_CHIP_NAME0 && n1 == RT3350_CHIP_NAME1) {
52                 rt305x_soc = RT305X_SOC_RT3350;
53                 name = "RT3350";
54         } else if (n0 == RT3352_CHIP_NAME0 && n1 == RT3352_CHIP_NAME1) {
55                 rt305x_soc = RT305X_SOC_RT3352;
56                 name = "RT3352";
57         } else {
58                 panic("rt305x: unknown SoC, n0:%08x n1:%08x\n", n0, n1);
59         }
60
61         id = __raw_readl(sysc + SYSC_REG_CHIP_ID);
62
63         snprintf(ramips_sys_type, RAMIPS_SYS_TYPE_LEN,
64                 "Ralink %s id:%u rev:%u",
65                 name,
66                 (id >> CHIP_ID_ID_SHIFT) & CHIP_ID_ID_MASK,
67                 (id & CHIP_ID_REV_MASK));
68
69         ramips_mem_base = RT305X_SDRAM_BASE;
70
71         if (soc_is_rt305x() || soc_is_rt3350()) {
72                 ramips_mem_size_min = RT305X_MEM_SIZE_MIN;
73                 ramips_mem_size_max = RT305X_MEM_SIZE_MAX;
74         } else if (soc_is_rt3352()) {
75                 ramips_mem_size_min = RT3352_MEM_SIZE_MIN;
76                 ramips_mem_size_max = RT3352_MEM_SIZE_MAX;
77         } else {
78                 BUG();
79         }
80 }
81
82 static struct ramips_gpio_chip rt305x_gpio_chips[] = {
83         {
84                 .chip = {
85                         .label  = "RT305X-GPIO0",
86                         .base   = 0,
87                         .ngpio  = 24,
88                 },
89                 .regs = {
90                         [RAMIPS_GPIO_REG_INT]   = 0x00,
91                         [RAMIPS_GPIO_REG_EDGE]  = 0x04,
92                         [RAMIPS_GPIO_REG_RENA]  = 0x08,
93                         [RAMIPS_GPIO_REG_FENA]  = 0x0c,
94                         [RAMIPS_GPIO_REG_DATA]  = 0x20,
95                         [RAMIPS_GPIO_REG_DIR]   = 0x24,
96                         [RAMIPS_GPIO_REG_POL]   = 0x28,
97                         [RAMIPS_GPIO_REG_SET]   = 0x2c,
98                         [RAMIPS_GPIO_REG_RESET] = 0x30,
99                         [RAMIPS_GPIO_REG_TOGGLE] = 0x34,
100                 },
101                 .map_base = RT305X_PIO_BASE,
102                 .map_size = RT305X_PIO_SIZE,
103         },
104         {
105                 .chip = {
106                         .label  = "RT305X-GPIO1",
107                         .base   = 24,
108                         .ngpio  = 16,
109                 },
110                 .regs = {
111                         [RAMIPS_GPIO_REG_INT]   = 0x38,
112                         [RAMIPS_GPIO_REG_EDGE]  = 0x3c,
113                         [RAMIPS_GPIO_REG_RENA]  = 0x40,
114                         [RAMIPS_GPIO_REG_FENA]  = 0x44,
115                         [RAMIPS_GPIO_REG_DATA]  = 0x48,
116                         [RAMIPS_GPIO_REG_DIR]   = 0x4c,
117                         [RAMIPS_GPIO_REG_POL]   = 0x50,
118                         [RAMIPS_GPIO_REG_SET]   = 0x54,
119                         [RAMIPS_GPIO_REG_RESET] = 0x58,
120                         [RAMIPS_GPIO_REG_TOGGLE] = 0x5c,
121                 },
122                 .map_base = RT305X_PIO_BASE,
123                 .map_size = RT305X_PIO_SIZE,
124         },
125         {
126                 .chip = {
127                         .label  = "RT305X-GPIO2",
128                         .base   = 40,
129                         .ngpio  = 12,
130                 },
131                 .regs = {
132                         [RAMIPS_GPIO_REG_INT]   = 0x60,
133                         [RAMIPS_GPIO_REG_EDGE]  = 0x64,
134                         [RAMIPS_GPIO_REG_RENA]  = 0x68,
135                         [RAMIPS_GPIO_REG_FENA]  = 0x6c,
136                         [RAMIPS_GPIO_REG_DATA]  = 0x70,
137                         [RAMIPS_GPIO_REG_DIR]   = 0x74,
138                         [RAMIPS_GPIO_REG_POL]   = 0x78,
139                         [RAMIPS_GPIO_REG_SET]   = 0x7c,
140                         [RAMIPS_GPIO_REG_RESET] = 0x80,
141                         [RAMIPS_GPIO_REG_TOGGLE] = 0x84,
142                 },
143                 .map_base = RT305X_PIO_BASE,
144                 .map_size = RT305X_PIO_SIZE,
145         },
146 };
147
148 static struct ramips_gpio_data rt305x_gpio_data = {
149         .chips = rt305x_gpio_chips,
150         .num_chips = ARRAY_SIZE(rt305x_gpio_chips),
151 };
152
153 static void rt305x_gpio_reserve(int first, int last)
154 {
155         for (; first <= last; first++)
156                 gpio_request(first, "reserved");
157 }
158
159 void __init rt305x_gpio_init(u32 mode)
160 {
161         u32 t;
162
163         rt305x_sysc_wr(mode, SYSC_REG_GPIO_MODE);
164
165         ramips_gpio_init(&rt305x_gpio_data);
166         if ((mode & RT305X_GPIO_MODE_I2C) == 0)
167                 rt305x_gpio_reserve(RT305X_GPIO_I2C_SD, RT305X_GPIO_I2C_SCLK);
168
169         if ((mode & RT305X_GPIO_MODE_SPI) == 0)
170                 rt305x_gpio_reserve(RT305X_GPIO_SPI_EN, RT305X_GPIO_SPI_CLK);
171
172         t = mode >> RT305X_GPIO_MODE_UART0_SHIFT;
173         t &= RT305X_GPIO_MODE_UART0_MASK;
174         switch (t) {
175         case RT305X_GPIO_MODE_UARTF:
176         case RT305X_GPIO_MODE_PCM_UARTF:
177         case RT305X_GPIO_MODE_PCM_I2S:
178         case RT305X_GPIO_MODE_I2S_UARTF:
179                 rt305x_gpio_reserve(RT305X_GPIO_7, RT305X_GPIO_14);
180                 break;
181         case RT305X_GPIO_MODE_PCM_GPIO:
182                 rt305x_gpio_reserve(RT305X_GPIO_10, RT305X_GPIO_14);
183                 break;
184         case RT305X_GPIO_MODE_GPIO_UARTF:
185         case RT305X_GPIO_MODE_GPIO_I2S:
186                 rt305x_gpio_reserve(RT305X_GPIO_7, RT305X_GPIO_10);
187                 break;
188         }
189
190         if ((mode & RT305X_GPIO_MODE_UART1) == 0)
191                 rt305x_gpio_reserve(RT305X_GPIO_UART1_TXD,
192                                     RT305X_GPIO_UART1_RXD);
193
194         if ((mode & RT305X_GPIO_MODE_JTAG) == 0)
195                 rt305x_gpio_reserve(RT305X_GPIO_JTAG_TDO, RT305X_GPIO_JTAG_TDI);
196
197         if ((mode & RT305X_GPIO_MODE_MDIO) == 0)
198                 rt305x_gpio_reserve(RT305X_GPIO_MDIO_MDC,
199                                     RT305X_GPIO_MDIO_MDIO);
200
201         if ((mode & RT305X_GPIO_MODE_SDRAM) == 0)
202                 rt305x_gpio_reserve(RT305X_GPIO_SDRAM_MD16,
203                                     RT305X_GPIO_SDRAM_MD31);
204
205         if ((mode & RT305X_GPIO_MODE_RGMII) == 0)
206                 rt305x_gpio_reserve(RT305X_GPIO_GE0_TXD0,
207                                     RT305X_GPIO_GE0_RXCLK);
208 }