ar71xx: fix MAC address swapping on WNR2200 and fix the WLAN MAC address
[openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-arduino-yun.c
1 /*
2  *  Arduino Yun support
3  *
4  *  Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
5  *  Copyright (C) 2015 Hauke Mehrtens <hauke@hauke-m.de>
6  *
7  *  This program is free software; you can redistribute it and/or modify it
8  *  under the terms of the GNU General Public License version 2 as published
9  *  by the Free Software Foundation.
10  */
11
12 #include "dev-eth.h"
13 #include "dev-gpio-buttons.h"
14 #include "dev-leds-gpio.h"
15 #include "dev-m25p80.h"
16 #include "dev-spi.h"
17 #include "dev-usb.h"
18 #include "dev-wmac.h"
19 #include "machtypes.h"
20 #include <asm/mach-ath79/ar71xx_regs.h>
21 #include <asm/mach-ath79/ath79.h>
22 #include "common.h"
23 #include "gpio.h"
24 #include "linux/gpio.h"
25
26 // Uncomment to have reset on gpio18 instead of gipo7
27 #define DS2_B
28
29 #define DS_GPIO_LED_WLAN                0
30 #define DS_GPIO_LED_USB                 1
31
32 #define DS_GPIO_OE                      21
33 #define DS_GPIO_AVR_RESET               18
34
35 // Maintained to have the console in the previous version of DS2 working
36 #define DS_GPIO_AVR_RESET_DS2           7
37
38 #define DS_GPIO_OE2                     22
39 #define DS_GPIO_UART_ENA                23
40 #define DS_GPIO_CONF_BTN                20
41
42 #define DS_KEYS_POLL_INTERVAL           20      /* msecs */
43 #define DS_KEYS_DEBOUNCE_INTERVAL       (3 * DS_KEYS_POLL_INTERVAL)
44
45 #define DS_MAC0_OFFSET                  0x0000
46 #define DS_MAC1_OFFSET                  0x0006
47 #define DS_CALDATA_OFFSET               0x1000
48 #define DS_WMAC_MAC_OFFSET              0x1002
49
50
51 static struct gpio_led ds_leds_gpio[] __initdata = {
52         {
53                 .name           = "arduino:white:usb",
54                 .gpio           = DS_GPIO_LED_USB,
55                 .active_low     = 0,
56         },
57         {
58                 .name           = "arduino:blue:wlan",
59                 .gpio           = DS_GPIO_LED_WLAN,
60                 .active_low     = 0,
61         },
62 };
63
64 static void __init ds_common_setup(void)
65 {
66         static u8 mac[6];
67
68         u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);
69         ath79_register_m25p80(NULL);
70
71         if (ar93xx_wmac_read_mac_address(mac)) {
72                 ath79_register_wmac(NULL, NULL);
73         } else {
74                 ath79_register_wmac(art + DS_CALDATA_OFFSET,
75                                     art + DS_WMAC_MAC_OFFSET);
76                 memcpy(mac, art + DS_WMAC_MAC_OFFSET, sizeof(mac));
77         }
78
79         mac[3] |= 0x08;
80         ath79_init_mac(ath79_eth0_data.mac_addr, mac, 0);
81
82         mac[3] &= 0xF7;
83         ath79_init_mac(ath79_eth1_data.mac_addr, mac, 0);
84         ath79_register_mdio(0, 0x0);
85
86         /* LAN ports */
87         ath79_register_eth(1);
88
89         /* WAN port */
90         ath79_register_eth(0);
91 }
92
93 static void __init ds_setup(void)
94 {
95         u32 t;
96
97         ds_common_setup();
98
99         ath79_register_leds_gpio(-1, ARRAY_SIZE(ds_leds_gpio),
100                                  ds_leds_gpio);
101         ath79_register_usb();
102
103         //Disable the Function for some pins to have GPIO functionality active
104         // GPIO6-7-8 and GPIO11
105         ath79_gpio_function_setup(AR933X_GPIO_FUNC_JTAG_DISABLE | AR933X_GPIO_FUNC_I2S_MCK_EN, 0);
106
107         ath79_gpio_function2_setup(AR933X_GPIO_FUNC2_JUMPSTART_DISABLE, 0);
108
109         printk("Setting DogStick2 GPIO\n");
110
111         t = ath79_reset_rr(AR933X_RESET_REG_BOOTSTRAP);
112         t |= AR933X_BOOTSTRAP_MDIO_GPIO_EN;
113         ath79_reset_wr(AR933X_RESET_REG_BOOTSTRAP, t);
114
115         // Put the avr reset to high 
116         if (gpio_request_one(DS_GPIO_AVR_RESET_DS2,
117             GPIOF_OUT_INIT_LOW | GPIOF_EXPORT_DIR_FIXED, "OE-1") != 0)
118                 printk("Error setting GPIO OE\n");
119         gpio_unexport(DS_GPIO_AVR_RESET_DS2);
120         gpio_free(DS_GPIO_AVR_RESET_DS2);
121
122         // enable OE of level shifter
123         if (gpio_request_one(DS_GPIO_OE,
124             GPIOF_OUT_INIT_LOW | GPIOF_EXPORT_DIR_FIXED, "OE-1") != 0)
125                 printk("Error setting GPIO OE\n");
126
127         if (gpio_request_one(DS_GPIO_UART_ENA,
128             GPIOF_OUT_INIT_LOW | GPIOF_EXPORT_DIR_FIXED, "UART-ENA") != 0)
129                 printk("Error setting GPIO Uart Enable\n");
130
131         // enable OE of level shifter
132         if (gpio_request_one(DS_GPIO_OE2,
133             GPIOF_OUT_INIT_LOW | GPIOF_EXPORT_DIR_FIXED, "OE-2") != 0)
134                 printk("Error setting GPIO OE2\n");
135 }
136
137 MIPS_MACHINE(ATH79_MACH_ARDUINO_YUN, "Yun", "Arduino Yun", ds_setup);