ar71xx: add initial support for the Mikrotik RB911G/RB912UAG boards
[openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / mach-rb91x.c
1 /*
2  *  MikroTik RouterBOARD 91X support
3  *
4  *  Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  */
10
11 #define pr_fmt(fmt) "rb91x: " fmt
12
13 #include <linux/phy.h>
14 #include <linux/delay.h>
15 #include <linux/platform_device.h>
16 #include <linux/ath9k_platform.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/nand.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/74x164.h>
22 #include <linux/spi/flash.h>
23 #include <linux/routerboot.h>
24 #include <linux/gpio.h>
25
26 #include <asm/prom.h>
27 #include <asm/mach-ath79/ath79.h>
28 #include <asm/mach-ath79/ath79_spi_platform.h>
29 #include <asm/mach-ath79/ar71xx_regs.h>
30
31 #include "common.h"
32 #include "dev-eth.h"
33 #include "dev-leds-gpio.h"
34 #include "dev-m25p80.h"
35 #include "dev-nfc.h"
36 #include "dev-usb.h"
37 #include "dev-wmac.h"
38 #include "machtypes.h"
39 #include "pci.h"
40 #include "routerboot.h"
41
42 #define RB_ROUTERBOOT_OFFSET    0x0000
43 #define RB_ROUTERBOOT_MIN_SIZE  0xb000
44 #define RB_HARD_CFG_SIZE        0x1000
45 #define RB_BIOS_OFFSET          0xd000
46 #define RB_BIOS_SIZE            0x1000
47 #define RB_SOFT_CFG_OFFSET      0xf000
48 #define RB_SOFT_CFG_SIZE        0x1000
49
50 #define RB91X_FLAG_USB          BIT(0)
51 #define RB91X_FLAG_PCIE         BIT(1)
52
53 struct rb_board_info {
54         const char *name;
55         u32 flags;
56 };
57
58 static struct mtd_partition rb711gr100_spi_partitions[] = {
59         {
60                 .name           = "routerboot",
61                 .offset         = RB_ROUTERBOOT_OFFSET,
62                 .mask_flags     = MTD_WRITEABLE,
63         }, {
64                 .name           = "hard_config",
65                 .size           = RB_HARD_CFG_SIZE,
66                 .mask_flags     = MTD_WRITEABLE,
67         }, {
68                 .name           = "bios",
69                 .offset         = RB_BIOS_OFFSET,
70                 .size           = RB_BIOS_SIZE,
71                 .mask_flags     = MTD_WRITEABLE,
72         }, {
73                 .name           = "soft_config",
74                 .size           = RB_SOFT_CFG_SIZE,
75         }
76 };
77
78 static struct flash_platform_data rb711gr100_spi_flash_data = {
79         .parts          = rb711gr100_spi_partitions,
80         .nr_parts       = ARRAY_SIZE(rb711gr100_spi_partitions),
81 };
82
83 static void __init rb711gr100_init_partitions(const struct rb_info *info)
84 {
85         rb711gr100_spi_partitions[0].size = info->hard_cfg_offs;
86         rb711gr100_spi_partitions[1].offset = info->hard_cfg_offs;
87
88         rb711gr100_spi_partitions[3].offset = info->soft_cfg_offs;
89 }
90
91 void __init rb711gr100_wlan_init(void)
92 {
93         char *caldata;
94         u8 wlan_mac[ETH_ALEN];
95
96         caldata = rb_get_wlan_data();
97         if (caldata == NULL)
98                 return;
99
100         ath79_init_mac(wlan_mac, ath79_mac_base, 1);
101         ath79_register_wmac(caldata + 0x1000, wlan_mac);
102
103         kfree(caldata);
104 }
105
106 #define RB_BOARD_INFO(_name, _flags)    \
107         {                               \
108                 .name = (_name),        \
109                 .flags = (_flags),      \
110         }
111
112 static const struct rb_board_info rb711gr100_boards[] __initconst = {
113         RB_BOARD_INFO("911G-2HPnD", 0),
114         RB_BOARD_INFO("911G-5HPnD", 0),
115         RB_BOARD_INFO("912UAG-2HPnD", RB91X_FLAG_USB | RB91X_FLAG_PCIE),
116         RB_BOARD_INFO("912UAG-5HPnD", RB91X_FLAG_USB | RB91X_FLAG_PCIE),
117 };
118
119 static u32 rb711gr100_get_flags(const struct rb_info *info)
120 {
121         int i;
122
123         for (i = 0; i < ARRAY_SIZE(rb711gr100_boards); i++) {
124                 const struct rb_board_info *bi;
125
126                 bi = &rb711gr100_boards[i];
127                 if (strcmp(info->board_name, bi->name) == 0)
128                         return bi->flags;
129         }
130
131         return 0;
132 }
133
134 static void __init rb711gr100_setup(void)
135 {
136         const struct rb_info *info;
137         char buf[64];
138         u32 flags;
139
140         info = rb_init_info((void *) KSEG1ADDR(0x1f000000), 0x10000);
141         if (!info)
142                 return;
143
144         scnprintf(buf, sizeof(buf), "Mikrotik RouterBOARD %s",
145                   (info->board_name) ? info->board_name : "");
146         mips_set_machine_name(buf);
147
148         rb711gr100_init_partitions(info);
149         ath79_register_m25p80(&rb711gr100_spi_flash_data);
150
151         ath79_setup_ar934x_eth_cfg(AR934X_ETH_CFG_RGMII_GMAC0 |
152                                    AR934X_ETH_CFG_SW_ONLY_MODE);
153
154         ath79_register_mdio(0, 0x0);
155
156         ath79_init_mac(ath79_eth0_data.mac_addr, ath79_mac_base, 0);
157         ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_RGMII;
158         ath79_eth0_data.phy_mask = BIT(0);
159
160         ath79_register_eth(0);
161
162         rb711gr100_wlan_init();
163
164         platform_device_register_simple("rb91x-nand", -1, NULL, 0);
165
166         flags = rb711gr100_get_flags(info);
167
168         if (flags & RB91X_FLAG_USB)
169                 ath79_register_usb();
170
171         if (flags & RB91X_FLAG_PCIE)
172                 ath79_register_pci();
173
174 }
175
176 MIPS_MACHINE_NONAME(ATH79_MACH_RB_711GR100, "711Gr100", rb711gr100_setup);