platform.c 13.8 KB
Newer Older
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
/*
 * Copyright (C) 2006,2007 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2006,2007 Eugene Konev <ejka@openwrt.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <linux/init.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/dma-mapping.h>
#include <linux/platform_device.h>
#include <linux/mtd/physmap.h>
#include <linux/serial.h>
#include <linux/serial_8250.h>
#include <linux/ioport.h>
#include <linux/io.h>
#include <linux/vlynq.h>
#include <linux/leds.h>
#include <linux/string.h>
#include <linux/etherdevice.h>
35 36
#include <linux/phy.h>
#include <linux/phy_fixed.h>
F
Florian Fainelli 已提交
37
#include <linux/gpio.h>
38
#include <linux/clk.h>
39 40 41 42 43 44

#include <asm/addrspace.h>
#include <asm/mach-ar7/ar7.h>
#include <asm/mach-ar7/gpio.h>
#include <asm/mach-ar7/prom.h>

45 46 47
/*****************************************************************************
 * VLYNQ Bus
 ****************************************************************************/
48 49 50 51 52 53 54 55
struct plat_vlynq_data {
	struct plat_vlynq_ops ops;
	int gpio_bit;
	int reset_bit;
};

static int vlynq_on(struct vlynq_device *dev)
{
56
	int ret;
57 58
	struct plat_vlynq_data *pdata = dev->dev.platform_data;

59 60
	ret = gpio_request(pdata->gpio_bit, "vlynq");
	if (ret)
61 62 63 64
		goto out;

	ar7_device_reset(pdata->reset_bit);

65 66
	ret = ar7_gpio_disable(pdata->gpio_bit);
	if (ret)
67 68
		goto out_enabled;

69 70
	ret = ar7_gpio_enable(pdata->gpio_bit);
	if (ret)
71 72
		goto out_enabled;

73 74
	ret = gpio_direction_output(pdata->gpio_bit, 0);
	if (ret)
75 76 77 78 79
		goto out_gpio_enabled;

	msleep(50);

	gpio_set_value(pdata->gpio_bit, 1);
80

81 82 83 84 85 86 87 88 89 90
	msleep(50);

	return 0;

out_gpio_enabled:
	ar7_gpio_disable(pdata->gpio_bit);
out_enabled:
	ar7_device_disable(pdata->reset_bit);
	gpio_free(pdata->gpio_bit);
out:
91
	return ret;
92 93 94 95 96
}

static void vlynq_off(struct vlynq_device *dev)
{
	struct plat_vlynq_data *pdata = dev->dev.platform_data;
97

98 99 100 101 102
	ar7_gpio_disable(pdata->gpio_bit);
	gpio_free(pdata->gpio_bit);
	ar7_device_disable(pdata->reset_bit);
}

103
static struct resource vlynq_low_res[] = {
104
	{
105 106 107 108
		.name	= "regs",
		.flags	= IORESOURCE_MEM,
		.start	= AR7_REGS_VLYNQ0,
		.end	= AR7_REGS_VLYNQ0 + 0xff,
109 110
	},
	{
111 112 113 114
		.name	= "irq",
		.flags	= IORESOURCE_IRQ,
		.start	= 29,
		.end	= 29,
115 116
	},
	{
117 118 119 120
		.name	= "mem",
		.flags	= IORESOURCE_MEM,
		.start	= 0x04000000,
		.end	= 0x04ffffff,
121 122
	},
	{
123 124 125 126
		.name	= "devirq",
		.flags	= IORESOURCE_IRQ,
		.start	= 80,
		.end	= 111,
127 128 129
	},
};

130
static struct resource vlynq_high_res[] = {
131
	{
132 133 134 135
		.name	= "regs",
		.flags	= IORESOURCE_MEM,
		.start	= AR7_REGS_VLYNQ1,
		.end	= AR7_REGS_VLYNQ1 + 0xff,
136 137
	},
	{
138 139 140 141
		.name	= "irq",
		.flags	= IORESOURCE_IRQ,
		.start	= 33,
		.end	= 33,
142 143
	},
	{
144 145 146 147
		.name	= "mem",
		.flags	= IORESOURCE_MEM,
		.start	= 0x0c000000,
		.end	= 0x0cffffff,
148 149
	},
	{
150 151 152 153
		.name	= "devirq",
		.flags	= IORESOURCE_IRQ,
		.start	= 112,
		.end	= 143,
154 155 156
	},
};

157 158 159 160
static struct plat_vlynq_data vlynq_low_data = {
	.ops = {
		.on	= vlynq_on,
		.off	= vlynq_off,
161
	},
162 163 164 165 166 167 168 169
	.reset_bit	= 20,
	.gpio_bit	= 18,
};

static struct plat_vlynq_data vlynq_high_data = {
	.ops = {
		.on	= vlynq_on,
		.off	= vlynq_off,
170
	},
171
	.reset_bit	= 16,
172 173 174 175 176 177 178 179
	.gpio_bit	= 19,
};

static struct platform_device vlynq_low = {
	.id		= 0,
	.name		= "vlynq",
	.dev = {
		.platform_data	= &vlynq_low_data,
180
	},
181 182 183 184 185 186 187 188 189
	.resource	= vlynq_low_res,
	.num_resources	= ARRAY_SIZE(vlynq_low_res),
};

static struct platform_device vlynq_high = {
	.id		= 1,
	.name		= "vlynq",
	.dev = {
		.platform_data	= &vlynq_high_data,
190
	},
191 192
	.resource	= vlynq_high_res,
	.num_resources	= ARRAY_SIZE(vlynq_high_res),
193 194
};

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
/*****************************************************************************
 * Flash
 ****************************************************************************/
static struct resource physmap_flash_resource = {
	.name	= "mem",
	.flags	= IORESOURCE_MEM,
	.start	= 0x10000000,
	.end	= 0x107fffff,
};

static struct physmap_flash_data physmap_flash_data = {
	.width	= 2,
};

static struct platform_device physmap_flash = {
	.name		= "physmap-flash",
	.dev = {
		.platform_data	= &physmap_flash_data,
213
	},
214 215 216 217 218 219 220 221
	.resource	= &physmap_flash_resource,
	.num_resources	= 1,
};

/*****************************************************************************
 * Ethernet
 ****************************************************************************/
static struct resource cpmac_low_res[] = {
222
	{
223 224 225 226
		.name	= "regs",
		.flags	= IORESOURCE_MEM,
		.start	= AR7_REGS_MAC0,
		.end	= AR7_REGS_MAC0 + 0x7ff,
227 228
	},
	{
229 230 231 232
		.name	= "irq",
		.flags	= IORESOURCE_IRQ,
		.start	= 27,
		.end 	= 27,
233 234 235
	},
};

236 237 238 239 240 241 242 243 244 245 246 247 248
static struct resource cpmac_high_res[] = {
	{
		.name	= "regs",
		.flags	= IORESOURCE_MEM,
		.start	= AR7_REGS_MAC1,
		.end	= AR7_REGS_MAC1 + 0x7ff,
	},
	{
		.name	= "irq",
		.flags	= IORESOURCE_IRQ,
		.start	= 41,
		.end	= 41,
	},
249 250
};

251
static struct fixed_phy_status fixed_phy_status __initdata = {
252 253 254
	.link		= 1,
	.speed		= 100,
	.duplex		= 1,
255 256
};

257
static struct plat_cpmac_data cpmac_low_data = {
258 259 260
	.reset_bit	= 17,
	.power_bit	= 20,
	.phy_mask	= 0x80000000,
261 262 263
};

static struct plat_cpmac_data cpmac_high_data = {
264 265 266
	.reset_bit	= 21,
	.power_bit	= 22,
	.phy_mask	= 0x7fffffff,
267 268
};

269
static u64 cpmac_dma_mask = DMA_BIT_MASK(32);
270

271
static struct platform_device cpmac_low = {
272 273
	.id		= 0,
	.name		= "cpmac",
274
	.dev = {
275 276 277
		.dma_mask		= &cpmac_dma_mask,
		.coherent_dma_mask	= DMA_BIT_MASK(32),
		.platform_data		= &cpmac_low_data,
278
	},
279 280
	.resource	= cpmac_low_res,
	.num_resources	= ARRAY_SIZE(cpmac_low_res),
281 282 283
};

static struct platform_device cpmac_high = {
284 285
	.id		= 1,
	.name		= "cpmac",
286
	.dev = {
287 288 289
		.dma_mask		= &cpmac_dma_mask,
		.coherent_dma_mask	= DMA_BIT_MASK(32),
		.platform_data		= &cpmac_high_data,
290
	},
291 292
	.resource	= cpmac_high_res,
	.num_resources	= ARRAY_SIZE(cpmac_high_res),
293 294
};

295 296 297 298 299 300 301 302 303 304 305 306 307 308
static inline unsigned char char2hex(char h)
{
	switch (h) {
	case '0': case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
		return h - '0';
	case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
		return h - 'A' + 10;
	case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
		return h - 'a' + 10;
	default:
		return 0;
	}
}
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
static void cpmac_get_mac(int instance, unsigned char *dev_addr)
{
	int i;
	char name[5], default_mac[ETH_ALEN], *mac;

	mac = NULL;
	sprintf(name, "mac%c", 'a' + instance);
	mac = prom_getenv(name);
	if (!mac) {
		sprintf(name, "mac%c", 'a');
		mac = prom_getenv(name);
	}
	if (!mac) {
		random_ether_addr(default_mac);
		mac = default_mac;
	}
	for (i = 0; i < 6; i++)
		dev_addr[i] = (char2hex(mac[i * 3]) << 4) +
			char2hex(mac[i * 3 + 1]);
}

/*****************************************************************************
 * USB
 ****************************************************************************/
static struct resource usb_res[] = {
	{
		.name	= "regs",
		.flags	= IORESOURCE_MEM,
		.start	= AR7_REGS_USB,
		.end	= AR7_REGS_USB + 0xff,
	},
	{
		.name	= "irq",
		.flags	= IORESOURCE_IRQ,
		.start	= 32,
		.end	= 32,
	},
	{
		.name	= "mem",
		.flags	= IORESOURCE_MEM,
		.start	= 0x03400000,
351
		.end	= 0x03401fff,
352
	},
353 354
};

355 356 357 358 359
static struct platform_device ar7_udc = {
	.name		= "ar7_udc",
	.resource	= usb_res,
	.num_resources	= ARRAY_SIZE(usb_res),
};
360

361 362 363
/*****************************************************************************
 * LEDs
 ****************************************************************************/
364 365
static struct gpio_led default_leds[] = {
	{
366 367 368
		.name			= "status",
		.gpio			= 8,
		.active_low		= 1,
369 370 371 372 373
	},
};

static struct gpio_led dsl502t_leds[] = {
	{
374 375 376
		.name			= "status",
		.gpio			= 9,
		.active_low		= 1,
377 378
	},
	{
379 380 381
		.name			= "ethernet",
		.gpio			= 7,
		.active_low		= 1,
382 383
	},
	{
384 385 386
		.name			= "usb",
		.gpio			= 12,
		.active_low		= 1,
387 388 389 390 391
	},
};

static struct gpio_led dg834g_leds[] = {
	{
392 393 394
		.name			= "ppp",
		.gpio			= 6,
		.active_low		= 1,
395 396
	},
	{
397 398 399
		.name			= "status",
		.gpio			= 7,
		.active_low		= 1,
400 401
	},
	{
402 403 404
		.name			= "adsl",
		.gpio			= 8,
		.active_low		= 1,
405 406
	},
	{
407 408 409
		.name			= "wifi",
		.gpio			= 12,
		.active_low		= 1,
410 411
	},
	{
412 413 414 415
		.name			= "power",
		.gpio			= 14,
		.active_low		= 1,
		.default_trigger	= "default-on",
416 417 418 419 420
	},
};

static struct gpio_led fb_sl_leds[] = {
	{
421 422
		.name			= "1",
		.gpio			= 7,
423 424
	},
	{
425 426 427
		.name			= "2",
		.gpio			= 13,
		.active_low		= 1,
428 429
	},
	{
430 431 432
		.name			= "3",
		.gpio			= 10,
		.active_low		= 1,
433 434
	},
	{
435 436 437
		.name			= "4",
		.gpio			= 12,
		.active_low		= 1,
438 439
	},
	{
440 441 442
		.name			= "5",
		.gpio			= 9,
		.active_low		= 1,
443 444 445 446 447
	},
};

static struct gpio_led fb_fon_leds[] = {
	{
448 449
		.name			= "1",
		.gpio			= 8,
450 451
	},
	{
452 453 454
		.name			= "2",
		.gpio			= 3,
		.active_low		= 1,
455 456
	},
	{
457 458
		.name			= "3",
		.gpio			= 5,
459 460
	},
	{
461 462 463
		.name			= "4",
		.gpio			= 4,
		.active_low		= 1,
464 465
	},
	{
466 467 468
		.name			= "5",
		.gpio			= 11,
		.active_low		= 1,
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
	},
};

static struct gpio_led_platform_data ar7_led_data;

static struct platform_device ar7_gpio_leds = {
	.name = "leds-gpio",
	.dev = {
		.platform_data = &ar7_led_data,
	}
};

static void __init detect_leds(void)
{
	char *prid, *usb_prod;

	/* Default LEDs	*/
	ar7_led_data.num_leds = ARRAY_SIZE(default_leds);
	ar7_led_data.leds = default_leds;

	/* FIXME: the whole thing is unreliable */
	prid = prom_getenv("ProductID");
	usb_prod = prom_getenv("usb_prod");

	/* If we can't get the product id from PROM, use the default LEDs */
	if (!prid)
		return;

	if (strstr(prid, "Fritz_Box_FON")) {
		ar7_led_data.num_leds = ARRAY_SIZE(fb_fon_leds);
		ar7_led_data.leds = fb_fon_leds;
	} else if (strstr(prid, "Fritz_Box_")) {
		ar7_led_data.num_leds = ARRAY_SIZE(fb_sl_leds);
		ar7_led_data.leds = fb_sl_leds;
	} else if ((!strcmp(prid, "AR7RD") || !strcmp(prid, "AR7DB"))
		&& usb_prod != NULL && strstr(usb_prod, "DSL-502T")) {
		ar7_led_data.num_leds = ARRAY_SIZE(dsl502t_leds);
		ar7_led_data.leds = dsl502t_leds;
	} else if (strstr(prid, "DG834")) {
		ar7_led_data.num_leds = ARRAY_SIZE(dg834g_leds);
		ar7_led_data.leds = dg834g_leds;
	}
}

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
/*****************************************************************************
 * Watchdog
 ****************************************************************************/
static struct resource ar7_wdt_res = {
	.name		= "regs",
	.flags		= IORESOURCE_MEM,
	.start		= -1,	/* Filled at runtime */
	.end		= -1,	/* Filled at runtime */
};

static struct platform_device ar7_wdt = {
	.name		= "ar7_wdt",
	.resource	= &ar7_wdt_res,
	.num_resources	= 1,
};

/*****************************************************************************
 * Init
 ****************************************************************************/
532
static int __init ar7_register_uarts(void)
533
{
534
#ifdef CONFIG_SERIAL_8250
535
	static struct uart_port uart_port __initdata;
536
	struct clk *bus_clk;
537
	int res;
538

539
	memset(&uart_port, 0, sizeof(struct uart_port));
540

541 542 543 544
	bus_clk = clk_get(NULL, "bus");
	if (IS_ERR(bus_clk))
		panic("unable to get bus clk\n");

545 546 547 548 549 550 551 552 553 554 555
	uart_port.type		= PORT_16550A;
	uart_port.uartclk	= clk_get_rate(bus_clk) / 2;
	uart_port.iotype	= UPIO_MEM32;
	uart_port.regshift	= 2;

	uart_port.line		= 0;
	uart_port.irq		= AR7_IRQ_UART0;
	uart_port.mapbase	= AR7_REGS_UART0;
	uart_port.membase	= ioremap(uart_port.mapbase, 256);

	res = early_serial_setup(&uart_port);
556 557 558 559 560
	if (res)
		return res;

	/* Only TNETD73xx have a second serial port */
	if (ar7_has_second_uart()) {
561 562 563 564 565 566
		uart_port.line		= 1;
		uart_port.irq		= AR7_IRQ_UART1;
		uart_port.mapbase	= UR8_REGS_UART1;
		uart_port.membase	= ioremap(uart_port.mapbase, 256);

		res = early_serial_setup(&uart_port);
567 568 569
		if (res)
			return res;
	}
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
#endif

	return 0;
}

static int __init ar7_register_devices(void)
{
	void __iomem *bootcr;
	u32 val;
	int res;

	res = ar7_register_uarts();
	if (res)
		pr_err("unable to setup uart(s): %d\n", res);

585 586
	res = platform_device_register(&physmap_flash);
	if (res)
587
		pr_warning("unable to register physmap-flash: %d\n", res);
588 589 590 591

	ar7_device_disable(vlynq_low_data.reset_bit);
	res = platform_device_register(&vlynq_low);
	if (res)
592
		pr_warning("unable to register vlynq-low: %d\n", res);
593 594 595 596 597

	if (ar7_has_high_vlynq()) {
		ar7_device_disable(vlynq_high_data.reset_bit);
		res = platform_device_register(&vlynq_high);
		if (res)
598
			pr_warning("unable to register vlynq-high: %d\n", res);
599 600 601
	}

	if (ar7_has_high_cpmac()) {
602
		res = fixed_phy_add(PHY_POLL, cpmac_high.id, &fixed_phy_status);
603 604 605 606 607 608 609 610 611
		if (!res) {
			cpmac_get_mac(1, cpmac_high_data.dev_addr);

			res = platform_device_register(&cpmac_high);
			if (res)
				pr_warning("unable to register cpmac-high: %d\n", res);
		} else
			pr_warning("unable to add cpmac-high phy: %d\n", res);
	} else
612 613
		cpmac_low_data.phy_mask = 0xffffffff;

614
	res = fixed_phy_add(PHY_POLL, cpmac_low.id, &fixed_phy_status);
615 616 617 618 619 620 621
	if (!res) {
		cpmac_get_mac(0, cpmac_low_data.dev_addr);
		res = platform_device_register(&cpmac_low);
		if (res)
			pr_warning("unable to register cpmac-low: %d\n", res);
	} else
		pr_warning("unable to add cpmac-low phy: %d\n", res);
622 623 624 625

	detect_leds();
	res = platform_device_register(&ar7_gpio_leds);
	if (res)
626
		pr_warning("unable to register leds: %d\n", res);
627 628

	res = platform_device_register(&ar7_udc);
629 630
	if (res)
		pr_warning("unable to register usb slave: %d\n", res);
631 632

	/* Register watchdog only if enabled in hardware */
633 634 635 636
	bootcr = ioremap_nocache(AR7_REGS_DCL, 4);
	val = readl(bootcr);
	iounmap(bootcr);
	if (val & AR7_WDT_HW_ENA) {
637
		if (ar7_has_high_vlynq())
638
			ar7_wdt_res.start = UR8_REGS_WDT;
639 640
		else
			ar7_wdt_res.start = AR7_REGS_WDT;
641 642

		ar7_wdt_res.end = ar7_wdt_res.start + 0x20;
643
		res = platform_device_register(&ar7_wdt);
644 645 646
		if (res)
			pr_warning("unable to register watchdog: %d\n", res);
	}
647

648
	return 0;
649 650
}
arch_initcall(ar7_register_devices);