From 0fa08e17965956e436d698a5fa4d956810722c97 Mon Sep 17 00:00:00 2001
From: Felix Kaechele <felix@fetzig.org>
Date: Tue, 26 Mar 2013 17:24:16 +0100
Subject: [PATCH] Add support for Poray X5

---
 target/linux/ramips/base-files/etc/diag.sh         |   3 +
 .../etc/hotplug.d/firmware/10-rt2x00-eeprom        |   1 +
 .../ramips/base-files/etc/uci-defaults/02_network  |  10 ++
 .../ramips/base-files/lib/preinit/06_set_iface_mac |   5 +
 target/linux/ramips/base-files/lib/ramips.sh       |   3 +
 .../ramips/base-files/lib/upgrade/platform.sh      |   3 +-
 .../arch/mips/include/asm/mach-ralink/machine.h    |   1 +
 .../ramips/files/arch/mips/ralink/rt305x/Kconfig   |   5 +
 .../ramips/files/arch/mips/ralink/rt305x/Makefile  |   1 +
 .../ramips/files/arch/mips/ralink/rt305x/mach-x5.c | 118 +++++++++++++++++++++
 target/linux/ramips/image/Makefile                 |   5 +
 target/linux/ramips/rt305x/config-3.7              |   1 +
 target/linux/ramips/rt305x/config-3.8              |   1 +
 target/linux/ramips/rt305x/profiles/poray.mk       |  17 +++
 14 files changed, 173 insertions(+), 1 deletion(-)
 create mode 100644 target/linux/ramips/files/arch/mips/ralink/rt305x/mach-x5.c
 create mode 100644 target/linux/ramips/rt305x/profiles/poray.mk

diff --git a/target/linux/ramips/base-files/etc/diag.sh b/target/linux/ramips/base-files/etc/diag.sh
index 0c2ae83..22018d8 100755
--- a/target/linux/ramips/base-files/etc/diag.sh
+++ b/target/linux/ramips/base-files/etc/diag.sh
@@ -117,6 +117,9 @@ get_status_led() {
 	ur-336un)
 		status_led="ur336:green:wps"
 		;;
+	x5)
+		status_led="x5:blue:status"
+		;;
 	xdxrn502j)
 		status_led="xdxrn502j:green:power"
 		;;
diff --git a/target/linux/ramips/base-files/etc/hotplug.d/firmware/10-rt2x00-eeprom b/target/linux/ramips/base-files/etc/hotplug.d/firmware/10-rt2x00-eeprom
index 4078528..d29df11 100644
--- a/target/linux/ramips/base-files/etc/hotplug.d/firmware/10-rt2x00-eeprom
+++ b/target/linux/ramips/base-files/etc/hotplug.d/firmware/10-rt2x00-eeprom
@@ -95,6 +95,7 @@ case "$FIRMWARE" in
 	mzk-w300nh2 | \
 	ur-326n4g | \
 	ur-336un | \
+	x5 | \
 	xdxrn502j)
 		rt2x00_eeprom_extract "factory" 0 272
 		;;
diff --git a/target/linux/ramips/base-files/etc/uci-defaults/02_network b/target/linux/ramips/base-files/etc/uci-defaults/02_network
index d6ab75a..9a6ca1a 100755
--- a/target/linux/ramips/base-files/etc/uci-defaults/02_network
+++ b/target/linux/ramips/base-files/etc/uci-defaults/02_network
@@ -129,6 +129,11 @@ ramips_setup_interfaces()
 		ucidef_set_interface_lan "eth0"
 		;;
 
+	x5)
+		swconfig dev rt305x set reset 1
+		ucidef_set_interface_lan "eth0"
+		;;
+
 	*)
 		RT3X5X=`cat /proc/cpuinfo | egrep "(RT3.5|RT5350)"`
 		if [ -n "${RT3X5X}" ]; then
@@ -252,6 +257,11 @@ ramips_setup_macs()
 		lan_mac=$(mtd_get_mac_binary factory 4)
 		;;
 
+	x5)
+		lan_mac=$(mtd_get_mac_binary factory 4)
+		lan_mac=$(macaddr_add "$lan_mac" -1)
+		;;
+
 	esac
 
 	[ -n "$lan_mac" ] && ucidef_set_interface_macaddr lan $lan_mac
diff --git a/target/linux/ramips/base-files/lib/preinit/06_set_iface_mac b/target/linux/ramips/base-files/lib/preinit/06_set_iface_mac
index fcfb86b..0f1ea6a 100644
--- a/target/linux/ramips/base-files/lib/preinit/06_set_iface_mac
+++ b/target/linux/ramips/base-files/lib/preinit/06_set_iface_mac
@@ -73,6 +73,11 @@ preinit_set_mac_address() {
 		mac=$(mtd_get_mac_binary devconf 4)
 		ifconfig eth0 hw ether $mac 2>/dev/null
 		;;
+	x5)
+		mac=$(mtd_get_mac_binary factory 4)
+		mac=$(macaddr_add "$mac" -1)
+		ifconfig eth0 hw ether $mac 2>/dev/null
+		;;
 	esac
 }
 
diff --git a/target/linux/ramips/base-files/lib/ramips.sh b/target/linux/ramips/base-files/lib/ramips.sh
index 6638b0a..ef45638 100755
--- a/target/linux/ramips/base-files/lib/ramips.sh
+++ b/target/linux/ramips/base-files/lib/ramips.sh
@@ -201,6 +201,9 @@ ramips_board_detect() {
 	*"AWB WR6202")
 		name="wr6202"
 		;;
+	*"Poray X5")
+		name="x5"
+		;;
 	*"XDX RN502J")
 		name="xdxrn502j"
 		;;
diff --git a/target/linux/ramips/base-files/lib/upgrade/platform.sh b/target/linux/ramips/base-files/lib/upgrade/platform.sh
index 97cc382..8b46ed7 100755
--- a/target/linux/ramips/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ramips/base-files/lib/upgrade/platform.sh
@@ -60,7 +60,8 @@ platform_check_image() {
 	whr-g300n |\
 	ur-326n4g |\
 	ur-336un |\
-	wr512-3gn)
+	wr512-3gn |\
+	x5)
 		[ "$magic" != "27051956" ] && {
 			echo "Invalid image type."
 			return 1
diff --git a/target/linux/ramips/files/arch/mips/include/asm/mach-ralink/machine.h b/target/linux/ramips/files/arch/mips/include/asm/mach-ralink/machine.h
index 200f737..ec39f7c 100644
--- a/target/linux/ramips/files/arch/mips/include/asm/mach-ralink/machine.h
+++ b/target/linux/ramips/files/arch/mips/include/asm/mach-ralink/machine.h
@@ -85,4 +85,5 @@ enum ramips_mach_type {
 
 	/* RT5350 based machines */
 	RAMIPS_MACH_AIR3GII,		/* AirLive Air3GII */
+	RAMIPS_MACH_X5,			/* Poray X5 */
 };
diff --git a/target/linux/ramips/files/arch/mips/ralink/rt305x/Kconfig b/target/linux/ramips/files/arch/mips/ralink/rt305x/Kconfig
index 700a983..464055d 100644
--- a/target/linux/ramips/files/arch/mips/ralink/rt305x/Kconfig
+++ b/target/linux/ramips/files/arch/mips/ralink/rt305x/Kconfig
@@ -209,6 +209,11 @@ config RT305X_MACH_MZKW300NH2
 	select RALINK_DEV_GPIO_BUTTONS
 	select RALINK_DEV_GPIO_LEDS
 
+config RT305X_MACH_X5
+	bool "Poray X5 support"
+	select RALINK_DEV_GPIO_BUTTONS
+	select RALINK_DEV_GPIO_LEDS
+
 config RT305X_MACH_XDX_RN502J
 	bool "Unknown board XDX-RN502J"
 	select RALINK_DEV_GPIO_BUTTONS
diff --git a/target/linux/ramips/files/arch/mips/ralink/rt305x/Makefile b/target/linux/ramips/files/arch/mips/ralink/rt305x/Makefile
index 47e9ce5..e7f3e18 100644
--- a/target/linux/ramips/files/arch/mips/ralink/rt305x/Makefile
+++ b/target/linux/ramips/files/arch/mips/ralink/rt305x/Makefile
@@ -53,4 +53,5 @@ obj-$(CONFIG_RT305X_MACH_WL341V3)	+= mach-wl341v3.o
 obj-$(CONFIG_RT305X_MACH_WL351)		+= mach-wl351.o
 obj-$(CONFIG_RT305X_MACH_WR6202)	+= mach-wr6202.o
 obj-$(CONFIG_RT305X_MACH_MZKW300NH2) 	+= mach-mzk-w300nh2.o
+obj-$(CONFIG_RT305X_MACH_X5)		+= mach-x5.o
 obj-$(CONFIG_RT305X_MACH_XDX_RN502J)    += mach-xdx-rn502j.o
diff --git a/target/linux/ramips/files/arch/mips/ralink/rt305x/mach-x5.c b/target/linux/ramips/files/arch/mips/ralink/rt305x/mach-x5.c
new file mode 100644
index 0000000..2d8612c
--- /dev/null
+++ b/target/linux/ramips/files/arch/mips/ralink/rt305x/mach-x5.c
@@ -0,0 +1,118 @@
+/*
+ *  Poray X5 board support by Felix Kaechele <felix@fetzig.org>
+ * 
+ *
+ *  This program is free software; you can redistribute it and/or modify it
+ *  under the terms of the GNU General Public License version 2 as published
+ *  by the Free Software Foundation.
+ */
+
+#include <linux/init.h>
+#include <linux/platform_device.h>
+#include <linux/spi/spi.h>
+#include <linux/spi/flash.h>
+#include <linux/gpio.h>
+
+#include <asm/mach-ralink/machine.h>
+#include <asm/mach-ralink/dev-gpio-buttons.h>
+#include <asm/mach-ralink/dev-gpio-leds.h>
+#include <asm/mach-ralink/rt305x.h>
+#include <asm/mach-ralink/rt305x_regs.h>
+
+#include "devices.h"
+
+#define X5_GPIO_LED_BATT_STATUS_80	7 /* active low */
+#define X5_GPIO_LED_BATT_STATUS_50	11 /* active low */
+#define X5_GPIO_LED_BATT_STATUS_20	12 /* active low */
+#define X5_GPIO_LED_STATUS_BLUE	13 /* active low */
+
+#define X5_GPIO_BUTTON_POWER	 9 /* active low */
+#define X5_GPIO_BUTTON_RESET	10 /* active low */
+#define X5_GPIO_BUTTON_SWITCH	14
+
+#define X5_KEYS_POLL_INTERVAL	20
+#define X5_KEYS_DEBOUNCE_INTERVAL	(3 * X5_KEYS_POLL_INTERVAL)
+
+const struct flash_platform_data x5_flash = {
+	.type			= "gd25q64b",
+};
+
+struct spi_board_info x5_spi_slave_info[] __initdata = {
+	{
+		.modalias	= "m25p80",
+		.platform_data	= &x5_flash,
+		.irq		= -1,
+		.max_speed_hz	= 10000000,
+		.bus_num	= 0,
+		.chip_select	= 0,
+		}
+};
+
+static struct gpio_led x5_leds_gpio[] __initdata = {
+	{
+		.name		= "x5:green:battstatus:20",
+		.gpio		= X5_GPIO_LED_BATT_STATUS_20,
+		.active_low	= 1,
+	}, {
+		.name		= "x5:green:battstatus:50",
+		.gpio		= X5_GPIO_LED_BATT_STATUS_50,
+		.active_low	= 1,
+	}, {
+		.name		= "x5:green:battstatus:80",
+		.gpio		= X5_GPIO_LED_BATT_STATUS_80,
+		.active_low	= 1,
+	}, {
+		.name		= "x5:blue:status",
+		.gpio		= X5_GPIO_LED_STATUS_BLUE,
+		.active_low	= 1,
+	}
+};
+
+static struct gpio_keys_button x5_gpio_buttons[] __initdata = {
+	{
+		.desc		= "Power Button",
+		.type		= EV_KEY,
+		.code		= KEY_POWER,
+		.debounce_interval = X5_KEYS_DEBOUNCE_INTERVAL,
+		.gpio		= X5_GPIO_BUTTON_POWER,
+		.active_low	= 1,
+	}, {
+		.desc		= "Reset Button",
+		.type		= EV_KEY,
+		.code		= KEY_RESTART,
+		.debounce_interval = X5_KEYS_DEBOUNCE_INTERVAL,
+		.gpio		= X5_GPIO_BUTTON_RESET,
+		.active_low	= 1,
+	}, {
+		.desc		= "DHCP/3G Switch", /* 0 = DHCP, 1 = 3G */
+		.type		= EV_SW,
+		.code		= BTN_0,
+		.debounce_interval = X5_KEYS_DEBOUNCE_INTERVAL,
+		.gpio		= X5_GPIO_BUTTON_SWITCH,
+	}
+};
+
+static void __init x5_init(void)
+{
+	rt305x_gpio_init(RT305X_GPIO_MODE_GPIO << RT305X_GPIO_MODE_UART0_SHIFT);
+
+	rt305x_register_spi(x5_spi_slave_info,
+				ARRAY_SIZE(x5_spi_slave_info));
+
+	ramips_register_gpio_leds(-1, ARRAY_SIZE(x5_leds_gpio),
+				  x5_leds_gpio);
+
+	ramips_register_gpio_buttons(-1, X5_KEYS_POLL_INTERVAL,
+				     ARRAY_SIZE(x5_gpio_buttons),
+				     x5_gpio_buttons);
+
+	rt305x_esw_data.vlan_config = RT305X_ESW_VLAN_CONFIG_NONE;
+	rt305x_register_ethernet();
+
+	rt305x_register_wifi();
+	rt305x_register_wdt();
+	rt305x_register_usb();
+}
+
+MIPS_MACHINE(RAMIPS_MACH_X5, "X5", "Poray X5",
+	     x5_init);
diff --git a/target/linux/ramips/image/Makefile b/target/linux/ramips/image/Makefile
index 6ff1bb1..aa1f7ca 100644
--- a/target/linux/ramips/image/Makefile
+++ b/target/linux/ramips/image/Makefile
@@ -645,6 +645,10 @@ define Image/Build/Profile/WR6202
 	$(call Image/Build/Template/$(fs_squash)/$(1),GENERIC_8M,wr6202,WR6202,ttyS1,115200,phys)
 endef
 
+define Image/Build/Profile/X5
+	$(call Image/Build/Template/$(fs_squash)/$(1),GENERIC_8M,x5,X5,ttyS1,57600,spi)
+endef
+
 define Image/Build/Profile/XDXRN502J
 	$(call Image/Build/Template/$(fs_squash)/$(1),GENERIC_4M,xdxrn502j,XDXRN502J,ttyS1,57600,phys)
 endef
@@ -692,6 +696,7 @@ define Image/Build/Profile/Default
 	$(call Image/Build/Profile/WL351,$(1))
 	$(call Image/Build/Profile/WR5123GN,$(1))
 	$(call Image/Build/Profile/WR6202,$(1))
+	$(call Image/Build/Profile/X5,$(1))
 	$(call Image/Build/Profile/XDXRN502J,$(1))
 endef
 endif
diff --git a/target/linux/ramips/rt305x/config-3.7 b/target/linux/ramips/rt305x/config-3.7
index 2c2ca00..4e5fe5a 100644
--- a/target/linux/ramips/rt305x/config-3.7
+++ b/target/linux/ramips/rt305x/config-3.7
@@ -144,6 +144,7 @@ CONFIG_RT305X_MACH_WL_330N=y
 CONFIG_RT305X_MACH_WL_330N3G=y
 CONFIG_RT305X_MACH_WR512_3GN=y
 CONFIG_RT305X_MACH_WR6202=y
+CONFIG_RT305X_MACH_X5=y
 CONFIG_RT305X_MACH_XDX_RN502J=y
 # CONFIG_SCSI_DMA is not set
 CONFIG_SERIAL_8250_NR_UARTS=4
diff --git a/target/linux/ramips/rt305x/config-3.8 b/target/linux/ramips/rt305x/config-3.8
index f0a6338..08d5943 100644
--- a/target/linux/ramips/rt305x/config-3.8
+++ b/target/linux/ramips/rt305x/config-3.8
@@ -143,6 +143,7 @@ CONFIG_RT305X_MACH_WL_330N=y
 CONFIG_RT305X_MACH_WL_330N3G=y
 CONFIG_RT305X_MACH_WR512_3GN=y
 CONFIG_RT305X_MACH_WR6202=y
+CONFIG_RT305X_MACH_X5=y
 CONFIG_RT305X_MACH_XDX_RN502J=y
 # CONFIG_SCSI_DMA is not set
 CONFIG_SERIAL_8250_NR_UARTS=4
diff --git a/target/linux/ramips/rt305x/profiles/poray.mk b/target/linux/ramips/rt305x/profiles/poray.mk
new file mode 100644
index 0000000..6692d81
--- /dev/null
+++ b/target/linux/ramips/rt305x/profiles/poray.mk
@@ -0,0 +1,17 @@
+#
+# Copyright (C) 2012 Felix Kaechele <felix@fetzig.org>
+#
+# This is free software, licensed under the GNU General Public License v2.
+# See /LICENSE for more information.
+#
+
+define Profile/X5
+	NAME:=Poray X5
+	PACKAGES:=kmod-usb-core kmod-usb2 kmod-ledtrig-netdev kmod-ledtrig-timer
+endef
+
+define Profile/X5/Description
+	Package set for Poray X5 board
+endef
+
+$(eval $(call Profile,X5))
-- 
1.8.1.4

