/*
 *  sysmocom devices
 *
 *  Copyright (C) 2015 Alexander Couzens <lynxis@fe80.eu>
 *
 *  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 <asm/mach-ath79/ath79.h>
#include <asm/mach-ath79/ar71xx_regs.h>
#include "common.h"
#include "dev-eth.h"
#include "dev-gpio-buttons.h"
#include "dev-leds-gpio.h"
#include "dev-m25p80.h"
#include "dev-spi.h"
#include "dev-usb.h"
#include "dev-wmac.h"
#include "machtypes.h"

#define SYSMOCOM_GPIO_LED_WLAN		0
#define SYSMOCOM_GPIO_LED_ETH0		13
#define SYSMOCOM_GPIO_LED_PSE		15

#define SYSMOCOM_MAC0_OFFSET			0x0000
#define SYSMOCOM_MAC1_OFFSET			0x0006
#define SYSMOCOM_CALDATA_OFFSET			0x1000
#define SYSMOCOM_WMAC_MAC_OFFSET		0x1002

static struct gpio_led sysmocom_sob_ap1_leds_gpio[] __initdata = {
	{
		.name		= "sysmocom:orange:wlan",
		.gpio		= SYSMOCOM_GPIO_LED_WLAN,
		.active_low	= 0,
	}, {
		.name		= "sysmocom:orange:eth0",
		.gpio		= SYSMOCOM_GPIO_LED_ETH0,
		.active_low	= 0,
	}, {
		.name		= "sysmocom:red:pse",
		.gpio		= SYSMOCOM_GPIO_LED_PSE,
		.active_low	= 0,
	}
};

static struct gpio_led sysmocom_sob_ap2_leds_gpio[] __initdata = {
	{
		.name		= "sysmocom:red:wlan",
		.gpio		= SYSMOCOM_GPIO_LED_WLAN,
		.active_low	= 0,
	}
};

static struct gpio_led sysmocom_sob_jb02_leds_gpio[] __initdata = {
	{
		.name		= "sysmocom:orange:wlan",
		.gpio		= SYSMOCOM_GPIO_LED_WLAN,
		.active_low	= 0,
	}, {
		.name		= "sysmocom:orange:eth0",
		.gpio		= SYSMOCOM_GPIO_LED_ETH0,
		.active_low	= 0,
	}
};


static void __init sysmocom_common_setup(void)
{
	ath79_register_m25p80(NULL);
}

static void __init sysmocom_sob_ap1_setup(void)
{
	u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);

	sysmocom_common_setup();
	ath79_register_wmac(art + SYSMOCOM_CALDATA_OFFSET,
			    art + SYSMOCOM_WMAC_MAC_OFFSET);

	ath79_setup_ar933x_phy4_switch(false, false);
	ath79_init_mac(ath79_eth0_data.mac_addr, art + SYSMOCOM_MAC0_OFFSET, 0);
	ath79_register_mdio(0, 0x0);
	/* ath79_register_eth(0) is unused because it using the internal switch */
	ath79_register_eth(1);

	ath79_gpio_function_disable(AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN);

	ath79_register_leds_gpio(-1, ARRAY_SIZE(sysmocom_sob_ap1_leds_gpio),
				 sysmocom_sob_ap1_leds_gpio);
	ath79_register_usb();
}

static void __init sysmocom_sob_ap2_setup(void)
{
	u8 *art = (u8 *) KSEG1ADDR(0x1f7f0000);

	sysmocom_common_setup();
	ath79_register_wmac(art + SYSMOCOM_CALDATA_OFFSET,
			    art + SYSMOCOM_WMAC_MAC_OFFSET);

	/* RJ45 port is separate WAN port / MAC, not switched with other ports */
	ath79_setup_ar933x_phy4_switch(false, false);
	ath79_init_mac(ath79_eth0_data.mac_addr, art + SYSMOCOM_MAC0_OFFSET, 0);
	ath79_init_mac(ath79_eth1_data.mac_addr, art + SYSMOCOM_MAC1_OFFSET, 0);
	ath79_register_mdio(0, 0x0);
	ath79_register_eth(1);
	ath79_register_eth(0);

	ath79_gpio_function_enable(AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN);

	ath79_register_leds_gpio(-1, ARRAY_SIZE(sysmocom_sob_ap2_leds_gpio),
				 sysmocom_sob_ap2_leds_gpio);
	ath79_register_usb();

	/* Enable GPIO26 and GPIO27 */
	ath79_reset_wr(AR933X_RESET_REG_BOOTSTRAP,
		       ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP) |
		       AR933X_BOOTSTRAP_MDIO_GPIO_EN);

}

static void __init sysmocom_sob_jb02_setup(void)
{
	u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);

	sysmocom_common_setup();
	ath79_register_wmac(art + SYSMOCOM_CALDATA_OFFSET,
			    art + SYSMOCOM_WMAC_MAC_OFFSET);

	ath79_setup_ar933x_phy4_switch(false, false);
	ath79_init_mac(ath79_eth0_data.mac_addr, art + SYSMOCOM_MAC0_OFFSET, 0);
	ath79_register_mdio(0, 0x0);
	/* ath79_register_eth(0) is unused because it using the internal switch */
	ath79_register_eth(1);

	ath79_gpio_function_disable(AR724X_GPIO_FUNC_ETH_SWITCH_LED0_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED1_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED2_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED3_EN |
				AR724X_GPIO_FUNC_ETH_SWITCH_LED4_EN);

	ath79_register_leds_gpio(-1, ARRAY_SIZE(sysmocom_sob_jb02_leds_gpio),
				 sysmocom_sob_jb02_leds_gpio);
	ath79_register_usb();
}

MIPS_MACHINE(ATH79_MACH_SYSMOCOM_SOB_AP1, "SYSMOSOBAP1", "sysmocom SOB-AP1",
		sysmocom_sob_ap1_setup);

MIPS_MACHINE(ATH79_MACH_SYSMOCOM_SOB_AP2, "SYSMOSOBAP2", "sysmocom SOB-AP2",
		sysmocom_sob_ap2_setup);

MIPS_MACHINE(ATH79_MACH_SYSMOCOM_SOB_JB02, "SYSMOSOBJB02", "sysmocom SOB-JB02",
		sysmocom_sob_jb02_setup);

