summaryrefslogtreecommitdiffstats
path: root/target/linux/atheros-2.6/files/arch/mips/atheros/ar5315/board.c
blob: 8247d8548c5ee00ebabf545af1a725cee586b79a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2003 Atheros Communications, Inc.,  All Rights Reserved.
 * Copyright (C) 2006 FON Technology, SL.
 * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
 */

/*
 * Platform devices for Atheros SoCs
 */

#include <linux/autoconf.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/platform_device.h>
#include <linux/kernel.h>
#include <linux/reboot.h>
#include <asm/bootinfo.h>
#include <asm/reboot.h>
#include <asm/time.h>
#include <asm/irq.h>
#include <asm/io.h>
#include "../ar531x.h"

static int is_5315 = 0;

static struct resource ar5315_eth_res[] = {
	{
		.name = "eth0_membase",
		.flags = IORESOURCE_MEM,
		.start = AR5315_ENET0,
		.end = AR5315_ENET0 + 0x2000,
	},
	{
		.name = "eth0_irq",
		.flags = IORESOURCE_IRQ,
		.start = AR5315_IRQ_ENET0_INTRS,
		.end = AR5315_IRQ_ENET0_INTRS,
	},
};

static struct ar531x_eth ar5315_eth_data = {
	.phy = 1,
	.mac = 0,
	.reset_base = AR5315_RESET,
	.reset_mac = AR5315_RESET_ENET0,
	.reset_phy = AR5315_RESET_EPHY0,
	.phy_base = AR5315_ENET0
};

static struct platform_device ar5315_eth = {
	.id = 0,
	.name = "ar531x-eth",
	.dev.platform_data = &ar5315_eth_data,
	.resource = ar5315_eth_res,
	.num_resources = ARRAY_SIZE(ar5315_eth_res)
};

static struct platform_device ar5315_wmac = {
	.id = 0,
	.name = "ar531x-wmac",
	/* FIXME: add resources */
};

static struct resource ar5315_spiflash_res[] = {
	{
		.name = "flash_base",
		.flags = IORESOURCE_MEM,
		.start = KSEG1ADDR(AR5315_SPI_READ),
		.end = KSEG1ADDR(AR5315_SPI_READ) + 0x800000,
	},
	{
		.name = "flash_regs",
		.flags = IORESOURCE_MEM,
		.start = 0x11300000,
		.end = 0x11300012,
	},
};

static struct platform_device ar5315_spiflash = {
	.id = 0,
	.name = "spiflash",
	.resource = ar5315_spiflash_res,
	.num_resources = ARRAY_SIZE(ar5315_spiflash_res)
};

static __initdata struct platform_device *ar5315_devs[4];



static void *flash_regs;

static inline __u32 spiflash_regread32(int reg)
{
	volatile __u32 *data = (__u32 *)(flash_regs + reg);

	return (*data);
}

static inline void spiflash_regwrite32(int reg, __u32 data)
{
	volatile __u32 *addr = (__u32 *)(flash_regs + reg);

	*addr = data;
}

#define SPI_FLASH_CTL      0x00
#define SPI_FLASH_OPCODE   0x04
#define SPI_FLASH_DATA     0x08

static __u8 spiflash_probe(void)
{
	 __u32 reg;

	do {
		reg = spiflash_regread32(SPI_FLASH_CTL);
	} while (reg & SPI_CTL_BUSY);

	spiflash_regwrite32(SPI_FLASH_OPCODE, 0xab);

	reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | 4 |
        	(1 << 4) | SPI_CTL_START;

	spiflash_regwrite32(SPI_FLASH_CTL, reg);
 
	do {
  		reg = spiflash_regread32(SPI_FLASH_CTL);
	} while (reg & SPI_CTL_BUSY);

	reg = (__u32) spiflash_regread32(SPI_FLASH_DATA);
	reg &= 0xff;

	return (u8) reg;
}


#define STM_8MBIT_SIGNATURE     0x13
#define STM_16MBIT_SIGNATURE    0x14
#define STM_32MBIT_SIGNATURE    0x15
#define STM_64MBIT_SIGNATURE    0x16
#define STM_128MBIT_SIGNATURE   0x17


static char __init *ar5315_flash_limit(void)
{
	u8 sig;
	u32 flash_size = 0;

	/* probe the flash chip size */
	flash_regs = ioremap_nocache(ar5315_spiflash_res[1].start, ar5315_spiflash_res[1].end - ar5315_spiflash_res[1].start);
	sig = spiflash_probe();
	iounmap(flash_regs);

	switch(sig) {
		case STM_8MBIT_SIGNATURE:
			flash_size = 0x00100000;
			break;
		case STM_16MBIT_SIGNATURE:
			flash_size = 0x00200000;
			break;
		case STM_32MBIT_SIGNATURE:
			flash_size = 0x00400000;
			break;
		case STM_64MBIT_SIGNATURE:
			flash_size = 0x00800000;
			break;
		case STM_128MBIT_SIGNATURE:
			flash_size = 0x01000000;
			break;
	}

	ar5315_spiflash_res[0].end = ar5315_spiflash_res[0].start + flash_size;
	return (char *) ar5315_spiflash_res[0].end;
}

int __init ar5315_init_devices(void)
{
	struct ar531x_config *config;
	struct ar531x_boarddata *bcfg;
	int dev = 0;

	if (!is_5315)
		return 0;

	/* Find board configuration */
	ar531x_find_config(ar5315_flash_limit());
	bcfg = (struct ar531x_boarddata *) board_config;

#if 0
	{
	/* Detect the hardware based on the device ID */
	u32 devid = sysRegRead(AR5315_SREV) & AR5315_REV_MAJ >> AR5315_REV_MAJ_S;
		switch(devid) {
		case 0x9:
			mips_machtype = MACH_ATHEROS_AR2317;
			break;
		/* FIXME: how can we detect AR2316? */
		case 0x8:
		default:
			mips_machtype = MACH_ATHEROS_AR2315;
			break;
		}
	}
#endif

	config = (struct ar531x_config *) kzalloc(sizeof(struct ar531x_config), GFP_KERNEL);
	config->board = board_config;
	config->radio = radio_config;
	config->unit = 0;
	config->tag = (u_int16_t) (sysRegRead(AR5315_SREV) & AR5315_REV_CHIP);
	
	ar5315_eth_data.board_config = board_config;
	ar5315_eth_data.macaddr = bcfg->enet0Mac;
	ar5315_wmac.dev.platform_data = config;
	
	ar5315_devs[dev++] = &ar5315_eth;
	ar5315_devs[dev++] = &ar5315_wmac;
	ar5315_devs[dev++] = &ar5315_spiflash;
			

	return platform_add_devices(ar5315_devs, dev);
}

static void ar5315_halt(void)
{
	 while (1);
}

static void ar5315_power_off(void)
{
	 ar5315_halt();
}


static void ar5315_restart(char *command)
{
	unsigned int reg;
	for(;;) {
		
		/* reset the system */
		sysRegWrite(AR5315_COLD_RESET,AR5317_RESET_SYSTEM);

		/* 
		 * Cold reset does not work on the AR2315/6, use the GPIO reset bits a workaround.
		 */

		reg = sysRegRead(AR5315_GPIO_DO);
		reg &= ~(1 << AR5315_RESET_GPIO);
		sysRegWrite(AR5315_GPIO_DO, reg);
		(void)sysRegRead(AR5315_GPIO_DO); /* flush write to hardware */
	}
}


/*
 * This table is indexed by bits 5..4 of the CLOCKCTL1 register
 * to determine the predevisor value.
 */
static int __initdata CLOCKCTL1_PREDIVIDE_TABLE[4] = {
    1,
    2,
    4,
    5
};

static int __initdata PLLC_DIVIDE_TABLE[5] = {
    2,
    3,
    4,
    6,
    3
};

static unsigned int __init
ar5315_sys_clk(unsigned int clockCtl)
{
    unsigned int pllcCtrl,cpuDiv;
    unsigned int pllcOut,refdiv,fdiv,divby2;
	unsigned int clkDiv;

    pllcCtrl = sysRegRead(AR5315_PLLC_CTL);
    refdiv = (pllcCtrl & PLLC_REF_DIV_M) >> PLLC_REF_DIV_S;
    refdiv = CLOCKCTL1_PREDIVIDE_TABLE[refdiv];
    fdiv = (pllcCtrl & PLLC_FDBACK_DIV_M) >> PLLC_FDBACK_DIV_S;
    divby2 = (pllcCtrl & PLLC_ADD_FDBACK_DIV_M) >> PLLC_ADD_FDBACK_DIV_S;
    divby2 += 1;
    pllcOut = (40000000/refdiv)*(2*divby2)*fdiv;


    /* clkm input selected */
	switch(clockCtl & CPUCLK_CLK_SEL_M) {
		case 0:
		case 1:
			clkDiv = PLLC_DIVIDE_TABLE[(pllcCtrl & PLLC_CLKM_DIV_M) >> PLLC_CLKM_DIV_S];
			break;
		case 2:
			clkDiv = PLLC_DIVIDE_TABLE[(pllcCtrl & PLLC_CLKC_DIV_M) >> PLLC_CLKC_DIV_S];
			break;
		default:
			pllcOut = 40000000;
			clkDiv = 1;
			break;
	}
	cpuDiv = (clockCtl & CPUCLK_CLK_DIV_M) >> CPUCLK_CLK_DIV_S;  
	cpuDiv = cpuDiv * 2 ?: 1;
	return (pllcOut/(clkDiv * cpuDiv));
}
		
static inline unsigned int ar5315_cpu_frequency(void)
{
    return ar5315_sys_clk(sysRegRead(AR5315_CPUCLK));
}

static inline unsigned int ar5315_apb_frequency(void)
{
    return ar5315_sys_clk(sysRegRead(AR5315_AMBACLK));
}

static void __init ar5315_time_init(void)
{
	mips_hpt_frequency = ar5315_cpu_frequency() / 2;
}

void __init ar5315_prom_init(void)
{
	u32 memsize, memcfg;

	is_5315 = 1;
	memcfg = sysRegRead(AR5315_MEM_CFG);
	memsize   = 1 + ((memcfg & SDRAM_DATA_WIDTH_M) >> SDRAM_DATA_WIDTH_S);
	memsize <<= 1 + ((memcfg & SDRAM_COL_WIDTH_M) >> SDRAM_COL_WIDTH_S);
	memsize <<= 1 + ((memcfg & SDRAM_ROW_WIDTH_M) >> SDRAM_ROW_WIDTH_S);
	memsize <<= 3;
	add_memory_region(0, memsize, BOOT_MEM_RAM);

	/* Initialize it to AR2315 for now. Real detection will be done
	 * in ar5315_init_devices() */
	mips_machtype = MACH_ATHEROS_AR2315;
}

void __init ar5315_plat_setup(void)
{
	unsigned int config = read_c0_config();

	/* Clear any lingering AHB errors */
	write_c0_config(config & ~0x3);
	sysRegWrite(AR5315_AHB_ERR0,AHB_ERROR_DET);
	sysRegRead(AR5315_AHB_ERR1);
	sysRegWrite(AR5315_WDC, WDC_IGNORE_EXPIRATION);

	board_time_init = ar5315_time_init;

	_machine_restart = ar5315_restart;
	_machine_halt = ar5315_halt;
	pm_power_off = ar5315_power_off;

	serial_setup(KSEG1ADDR(AR5315_UART0), ar5315_apb_frequency());
}

arch_initcall(ar5315_init_devices);