From d018dcff2c2fd48287d7e6d501cf0256a26441e9 Mon Sep 17 00:00:00 2001
From: Felix Kaechele <felix@fetzig.org>
Date: Tue, 26 Mar 2013 00:39:07 +0100
Subject: [PATCH] Add support for Poray M3

---
 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      |  1 +
 .../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-m3.c | 94 ++++++++++++++++++++++
 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, 148 insertions(+)
 create mode 100644 target/linux/ramips/files/arch/mips/ralink/rt305x/mach-m3.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..885849b 100755
--- a/target/linux/ramips/base-files/etc/diag.sh
+++ b/target/linux/ramips/base-files/etc/diag.sh
@@ -46,6 +46,9 @@ get_status_led() {
 	hw550-3g)
 		status_led="hw550-3g:green:status"
 		;;
+	m3)
+		status_led="m3:blue:status"
+		;;
 	mofi3500-3gn)
 		status_led="mofi3500-3gn:green:status"
 		;;
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..cfb3e91 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
@@ -70,6 +70,7 @@ case "$FIRMWARE" in
 	rt-n13u | \
 	freestation5 | \
 	hw550-3g | \
+	m3 | \
 	mofi3500-3gn | \
 	pwh2004 | \
 	nbg-419n | \
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..04c5cc0 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"
 		;;
 
+	m3)
+		swconfig dev rt305x set reset 1
+		ucidef_set_interface_lan "eth0"
+		;;
+
 	*)
 		RT3X5X=`cat /proc/cpuinfo | egrep "(RT3.5|RT5350)"`
 		if [ -n "${RT3X5X}" ]; then
@@ -206,6 +211,11 @@ ramips_setup_macs()
 		wan_mac=$(macaddr_add "$lan_mac" 1)
 		;;
 
+	m3)
+		lan_mac=$(mtd_get_mac_binary factory 4)
+		lan_mac=$(macaddr_add "$lan_mac" -1)
+		;;
+
 	nbg-419n | \
 	wcr-150gn)
                 lan_mac=$(mtd_get_mac_binary factory 4)
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..75c0c3d 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
@@ -53,6 +53,11 @@ preinit_set_mac_address() {
 	fonera20n |\
 	rt-n13u |\
 	hw550-3g |\
+	m3)
+		mac=$(mtd_get_mac_binary factory 4)
+		mac=$(macaddr_add "$mac" -1)
+		ifconfig eth0 hw ether $mac 2>/dev/null
+		;;
 	nbg-419n |\
 	omni-emb |\
 	omni-emb-hpm |\
diff --git a/target/linux/ramips/base-files/lib/ramips.sh b/target/linux/ramips/base-files/lib/ramips.sh
index 6638b0a..9a08ae5 100755
--- a/target/linux/ramips/base-files/lib/ramips.sh
+++ b/target/linux/ramips/base-files/lib/ramips.sh
@@ -108,6 +108,9 @@ ramips_board_detect() {
 	*"Asus RT-N13U")
 		name="rt-n13u"
 		;;
+	*"Poray M3")
+		name="m3"
+		;;
 	*"MoFi Network MOFI3500-3GN")
 		name="mofi3500-3gn"
 		;;
diff --git a/target/linux/ramips/base-files/lib/upgrade/platform.sh b/target/linux/ramips/base-files/lib/upgrade/platform.sh
index 97cc382..0e43abf 100755
--- a/target/linux/ramips/base-files/lib/upgrade/platform.sh
+++ b/target/linux/ramips/base-files/lib/upgrade/platform.sh
@@ -35,6 +35,7 @@ platform_check_image() {
 	rt-n13u | \
 	freestation5 | \
 	hw550-3g | \
+	m3 | \
 	mofi3500-3gn | \
 	mzk-w300nh2 | \
 	nbg-419n | \
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..168e063 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_M3,			/* Poray M3 */
 };
diff --git a/target/linux/ramips/files/arch/mips/ralink/rt305x/Kconfig b/target/linux/ramips/files/arch/mips/ralink/rt305x/Kconfig
index 700a983..d660aba 100644
--- a/target/linux/ramips/files/arch/mips/ralink/rt305x/Kconfig
+++ b/target/linux/ramips/files/arch/mips/ralink/rt305x/Kconfig
@@ -124,6 +124,11 @@ config RT305X_MACH_RT_N13U
 config RT305X_MACH_FREESTATION5
 	bool "ARC FreeStation5"
 
+config RT305X_MACH_M3
+	bool "Poray M3 support"
+	select RALINK_DEV_GPIO_BUTTONS
+	select RALINK_DEV_GPIO_LEDS
+
 config RT305X_MACH_MOFI3500_3GN
 	bool "MoFi Network MOFI3500-3GN support"
 	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..254f675 100644
--- a/target/linux/ramips/files/arch/mips/ralink/rt305x/Makefile
+++ b/target/linux/ramips/files/arch/mips/ralink/rt305x/Makefile
@@ -30,6 +30,7 @@ obj-$(CONFIG_RT305X_MACH_FONERA20N)	+= mach-fonera20n.o
 obj-$(CONFIG_RT305X_MACH_RT_N13U)	+= mach-rt-n13u.o
 obj-$(CONFIG_RT305X_MACH_FREESTATION5)  += mach-freestation5.o
 obj-$(CONFIG_RT305X_MACH_HW550_3G)	+= mach-hw550-3g.o
+obj-$(CONFIG_RT305X_MACH_M3)		+= mach-m3.o
 obj-$(CONFIG_RT305X_MACH_MOFI3500_3GN)	+= mach-mofi3500-3gn.o
 obj-$(CONFIG_RT305X_MACH_NBG_419N)	+= mach-nbg-419n.o
 obj-$(CONFIG_RT305X_MACH_NW718)		+= mach-nw718.o
diff --git a/target/linux/ramips/files/arch/mips/ralink/rt305x/mach-m3.c b/target/linux/ramips/files/arch/mips/ralink/rt305x/mach-m3.c
new file mode 100644
index 0000000..71a79ea
--- /dev/null
+++ b/target/linux/ramips/files/arch/mips/ralink/rt305x/mach-m3.c
@@ -0,0 +1,94 @@
+/*
+ *  Poray M3 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 M3_GPIO_LED_STATUS	9
+#define M3_GPIO_BUTTON_RESET	10
+#define M3_GPIO_BUTTON_SWITCH	17
+
+#define M3_KEYS_POLL_INTERVAL	20
+#define M3_KEYS_DEBOUNCE_INTERVAL	(3 * M3_KEYS_POLL_INTERVAL)
+
+const struct flash_platform_data m3_flash = {
+	.type				= "w25q32",
+};
+
+struct spi_board_info m3_spi_slave_info[] __initdata = {
+	{
+		.modalias		= "m25p80",
+		.platform_data	= &m3_flash,
+		.irq			= -1,
+		.max_speed_hz	= 10000000,
+		.bus_num		= 0,
+		.chip_select	= 0,
+		}
+};
+
+static struct gpio_led m3_leds_gpio[] __initdata = {
+	{
+		.name		= "m3:blue:status",
+		.gpio		= M3_GPIO_LED_STATUS,
+		.active_low	= 1,
+	}
+};
+
+static struct gpio_keys_button m3_gpio_buttons[] __initdata = {
+	{
+		.desc		= "Reset Button",
+		.type		= EV_KEY,
+		.code		= KEY_RESTART,
+		.debounce_interval = M3_KEYS_DEBOUNCE_INTERVAL,
+		.gpio		= M3_GPIO_BUTTON_RESET,
+		.active_low	= 1,
+	}, {
+		.desc		= "DHCP/3G Switch", /* 0 = DHCP, 1 = 3G */
+		.type		= EV_SW,
+		.code		= BTN_0,
+		.debounce_interval = M3_KEYS_DEBOUNCE_INTERVAL,
+		.gpio		= M3_GPIO_BUTTON_SWITCH,
+	}
+};
+
+static void __init m3_init(void)
+{
+	rt305x_gpio_init(RT305X_GPIO_MODE_GPIO << RT305X_GPIO_MODE_UART0_SHIFT);
+
+	rt305x_register_spi(m3_spi_slave_info,
+				ARRAY_SIZE(m3_spi_slave_info));
+
+	ramips_register_gpio_leds(-1, ARRAY_SIZE(m3_leds_gpio),
+				  m3_leds_gpio);
+
+	ramips_register_gpio_buttons(-1, M3_KEYS_POLL_INTERVAL,
+				     ARRAY_SIZE(m3_gpio_buttons),
+				     m3_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_M3, "M3", "Poray M3",
+	     m3_init);
diff --git a/target/linux/ramips/image/Makefile b/target/linux/ramips/image/Makefile
index 6ff1bb1..8c46d97 100644
--- a/target/linux/ramips/image/Makefile
+++ b/target/linux/ramips/image/Makefile
@@ -551,6 +551,10 @@ define Image/Build/Profile/FREESTATION5
 	$(call Image/Build/Template/$(fs_squash)/$(1),GENERIC_8M,freestation5,FREESTATION5,ttyS1,115200,phys)
 endef
 
+define Image/Build/Profile/M3
+	$(call Image/Build/Template/$(fs_squash)/$(1),GENERIC_4M,m3,M3,ttyS1,57600,spi)
+endef
+
 define Image/Build/Profile/MOFI35003GN
 	$(call Image/Build/Template/$(fs_squash)/$(1),GENERIC_8M,mofi3500-3gn,MOFI3500-3GN,ttyS1,57600,phys)
 endef
@@ -671,6 +675,7 @@ define Image/Build/Profile/Default
 	$(call Image/Build/Profile/RT-N13U,$(1))
 	$(call Image/Build/Profile/FREESTATION5,$(1))
 	$(call Image/Build/Profile/HW5503G,$(1))
+	$(call Image/Build/Profile/M3,$(1))
 	$(call Image/Build/Profile/MOFI35003GN,$(1))
 	$(call Image/Build/Profile/MZKW3000NH2,$(1))
 	$(call Image/Build/Profile/NBG419N,$(1))
diff --git a/target/linux/ramips/rt305x/config-3.7 b/target/linux/ramips/rt305x/config-3.7
index 2c2ca00..9e24a04 100644
--- a/target/linux/ramips/rt305x/config-3.7
+++ b/target/linux/ramips/rt305x/config-3.7
@@ -120,6 +120,7 @@ CONFIG_RT305X_MACH_F5D8235_V2=y
 CONFIG_RT305X_MACH_FONERA20N=y
 CONFIG_RT305X_MACH_FREESTATION5=y
 CONFIG_RT305X_MACH_HW550_3G=y
+CONFIG_RT305X_MACH_M3=y
 CONFIG_RT305X_MACH_MOFI3500_3GN=y
 CONFIG_RT305X_MACH_MZKW300NH2=y
 CONFIG_RT305X_MACH_NBG_419N=y
diff --git a/target/linux/ramips/rt305x/config-3.8 b/target/linux/ramips/rt305x/config-3.8
index f0a6338..e3a669b 100644
--- a/target/linux/ramips/rt305x/config-3.8
+++ b/target/linux/ramips/rt305x/config-3.8
@@ -119,6 +119,7 @@ CONFIG_RT305X_MACH_F5D8235_V2=y
 CONFIG_RT305X_MACH_FONERA20N=y
 CONFIG_RT305X_MACH_FREESTATION5=y
 CONFIG_RT305X_MACH_HW550_3G=y
+CONFIG_RT305X_MACH_M3=y
 CONFIG_RT305X_MACH_MOFI3500_3GN=y
 CONFIG_RT305X_MACH_MZKW300NH2=y
 CONFIG_RT305X_MACH_NBG_419N=y
diff --git a/target/linux/ramips/rt305x/profiles/poray.mk b/target/linux/ramips/rt305x/profiles/poray.mk
new file mode 100644
index 0000000..9c76c78
--- /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/M3
+	NAME:=Poray M3
+	PACKAGES:=kmod-usb-core kmod-usb2 kmod-ledtrig-netdev kmod-ledtrig-timer
+endef
+
+define Profile/M3/Description
+	Package set for Poray M3 board
+endef
+
+$(eval $(call Profile,M3))
-- 
1.8.1.4

