mikrotik mikrotik
authorJohn Crispin <john@phrozen.org>
Thu, 11 Oct 2018 05:44:54 +0000 (07:44 +0200)
committerJohn Crispin <john@phrozen.org>
Thu, 11 Oct 2018 05:44:54 +0000 (07:44 +0200)
14 files changed:
include/image-commands.mk
target/linux/ath79/dts/ar7100.dtsi
target/linux/ath79/dts/ar7161_mikrotik_rb450g.dts [new file with mode: 0644]
target/linux/ath79/files/drivers/spi/spi-rb4xx-cpld.c [new file with mode: 0644]
target/linux/ath79/files/drivers/spi/spi-rb4xx.c [new file with mode: 0644]
target/linux/ath79/image/Makefile
target/linux/ath79/image/mikrotik.mk [new file with mode: 0644]
target/linux/ath79/image/nand-mikrotik.mk [new file with mode: 0644]
target/linux/ath79/nand/config-default
target/linux/ath79/nand/target.mk
target/linux/ath79/patches-4.14/409-mtd-rb4xx_nand_driver.patch [new file with mode: 0644]
target/linux/ath79/patches-4.14/432-spi-rb4xx-spi-driver.patch [new file with mode: 0644]
target/linux/ath79/patches-4.14/433-spi-rb4xx-cpld-driver.patch [new file with mode: 0644]
target/linux/ath79/patches-4.14/441-leds-rb750-led-driver.patch [new file with mode: 0644]

index ae01706b146bb47f2ae7c2fad947f336df3f96c9..baecced5879366ddc4923c45b52ab0ccfa969ebc 100644 (file)
@@ -162,7 +162,8 @@ endef
 
 define Build/lzma-no-dict
        $(STAGING_DIR_HOST)/bin/lzma e $@ $(1) $@.new
-       @mv $@.new $@
+       cp $@.new $@.new.lzma
+       mv $@.new $@
 endef
 
 define Build/gzip
index 982ad9648467107aeac85a3ea51acb7b2758c5e8..7263455fcb0c5e2a3a011efcdc5f6ea12e93dc7c 100644 (file)
 
        spi: spi@1f000000 {
                compatible = "qca,ar7100-spi";
-               reg = <0x1f000000 0x10>;
+               reg = <0x1f000000 0x100>;
 
                clocks = <&pll ATH79_CLK_AHB>;
                clock-names = "ahb";
diff --git a/target/linux/ath79/dts/ar7161_mikrotik_rb450g.dts b/target/linux/ath79/dts/ar7161_mikrotik_rb450g.dts
new file mode 100644 (file)
index 0000000..80262a4
--- /dev/null
@@ -0,0 +1,126 @@
+// SPDX-License-Identifier: GPL-2.0-or-later OR MIT
+/dts-v1/;
+
+#include <dt-bindings/gpio/gpio.h>
+#include <dt-bindings/input/input.h>
+
+#include "ar7100.dtsi"
+
+/ {
+       compatible = "mikrotik,rb450g", "qca,ar7161";
+       model = "MikroTik RouterBOARD 450G";
+
+/*     aliases {
+               led-boot = &orange_power;
+               led-failsafe = &orange_power;
+               led-running = &blue_power;
+               led-upgrade = &orange_power;
+       };*/
+
+       chosen {
+               bootargs = "console=ttyS0,115200";
+       };
+
+       extosc: ref {
+               compatible = "fixed-clock";
+               #clock-cells = <0>;
+               clock-output-names = "ref";
+               clock-frequency = <40000000>;
+       };
+
+       leds {
+               compatible = "gpio-leds";
+               yellow_user {
+                       label = "rb4xx:yellow:user";
+                       gpios = <&gpio 4 GPIO_ACTIVE_LOW>;
+                       default-state = "off";
+               };
+       };
+
+       keys {
+               compatible = "gpio-keys-polled";
+               poll-interval = <20>;
+
+               reset {
+                       label = "reset";
+                       linux,code = <KEY_RESTART>;
+                       gpios = <&gpio 7 GPIO_ACTIVE_LOW>;
+                       debounce-interval = <60>;
+               };
+       };
+};
+
+&uart {
+       status = "okay";
+};
+
+&pll {
+       clocks = <&extosc>;
+};
+
+&spi {
+       compatible = "mikrotik,rb4xx-spi";
+       status = "okay";
+       num-cs = <1>;
+
+       flash@0 {
+               compatible = "jedec,spi-nor";
+               reg = <0>;
+               spi-max-frequency = <25000000>;
+
+               partitions {
+                       compatible = "fixed-partitions";
+                       #address-cells = <1>;
+                       #size-cells = <1>;
+
+                       partition@0 {
+                               label = "routerboot";
+                               reg = <0x000000 0x00b000>;
+                               read-only;
+                       };
+
+                       partition@b000 {
+                               label = "board_config";
+                               reg = <0x00b000 0x001000>;
+                               read-only;
+                       };
+
+                       partition@c000 {
+                               label = "bios";
+                               reg = <0x00d000 0x002000>;
+                       };
+
+                       partition@f000 {
+                               label = "soft_config";
+                               reg = <0x000f000 0x001000>;
+                               read-only;
+                       };
+               };
+       };
+};
+
+&eth0 {
+       status = "okay";
+       pll-data = <0x11110000 0x00001099 0x00991099>;
+
+       fixed-link {
+               speed = <1000>;
+               full-duplex;
+       };
+};
+
+&mdio0 {
+       status = "okay";
+
+       phy4: ethernet-phy@4 {
+               reg = <4>;
+               phy-mode = "rgmii";
+       };
+};
+
+&eth1 {
+       status = "okay";
+       pll-data = <0x11110000 0x00001099 0x00991099>;
+
+       phy-handle = <&phy4>;
+};
diff --git a/target/linux/ath79/files/drivers/spi/spi-rb4xx-cpld.c b/target/linux/ath79/files/drivers/spi/spi-rb4xx-cpld.c
new file mode 100644 (file)
index 0000000..18af83b
--- /dev/null
@@ -0,0 +1,347 @@
+/*
+ * SPI driver for the CPLD chip on the Mikrotik RB4xx boards
+ *
+ * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
+ *
+ * This file was based on the patches for Linux 2.6.27.39 published by
+ * MikroTik for their RouterBoard 4xx series devices.
+ *
+ * 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/types.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/bitops.h>
+#include <linux/spi/spi.h>
+#include <linux/gpio.h>
+#include <linux/slab.h>
+#include <linux/version.h>
+
+#include <asm/mach-ath79/rb4xx_cpld.h>
+
+#define DRV_NAME       "spi-rb4xx-cpld"
+#define DRV_DESC       "RB4xx CPLD driver"
+#define DRV_VERSION    "0.1.0"
+
+#define CPLD_CMD_WRITE_NAND    0x08 /* send cmd, n x send data, send indle */
+#define CPLD_CMD_WRITE_CFG     0x09 /* send cmd, n x send cfg */
+#define CPLD_CMD_READ_NAND     0x0a /* send cmd, send idle, n x read data */
+#define CPLD_CMD_READ_FAST     0x0b /* send cmd, 4 x idle, n x read data */
+#define CPLD_CMD_LED5_ON       0x0c /* send cmd */
+#define CPLD_CMD_LED5_OFF      0x0d /* send cmd */
+
+struct rb4xx_cpld {
+       struct spi_device       *spi;
+       struct mutex            lock;
+       struct gpio_chip        chip;
+       unsigned int            config;
+};
+
+static struct rb4xx_cpld *rb4xx_cpld;
+
+static inline struct rb4xx_cpld *gpio_to_cpld(struct gpio_chip *chip)
+{
+       return container_of(chip, struct rb4xx_cpld, chip);
+}
+
+static int rb4xx_cpld_write_cmd(struct rb4xx_cpld *cpld, unsigned char cmd)
+{
+       struct spi_transfer t[1];
+       struct spi_message m;
+       unsigned char tx_buf[1];
+       int err;
+
+       spi_message_init(&m);
+       memset(&t, 0, sizeof(t));
+
+       t[0].tx_buf = tx_buf;
+       t[0].len = sizeof(tx_buf);
+       spi_message_add_tail(&t[0], &m);
+
+       tx_buf[0] = cmd;
+
+       err = spi_sync(cpld->spi, &m);
+       return err;
+}
+
+static int rb4xx_cpld_write_cfg(struct rb4xx_cpld *cpld, unsigned char config)
+{
+       struct spi_transfer t[1];
+       struct spi_message m;
+       unsigned char cmd[2];
+       int err;
+
+       spi_message_init(&m);
+       memset(&t, 0, sizeof(t));
+
+       t[0].tx_buf = cmd;
+       t[0].len = sizeof(cmd);
+       spi_message_add_tail(&t[0], &m);
+
+       cmd[0] = CPLD_CMD_WRITE_CFG;
+       cmd[1] = config;
+
+       err = spi_sync(cpld->spi, &m);
+       return err;
+}
+
+static int __rb4xx_cpld_change_cfg(struct rb4xx_cpld *cpld, unsigned mask,
+                                  unsigned value)
+{
+       unsigned int config;
+       int err;
+
+       config = cpld->config & ~mask;
+       config |= value;
+
+       if ((cpld->config ^ config) & 0xff) {
+               err = rb4xx_cpld_write_cfg(cpld, config);
+               if (err)
+                       return err;
+       }
+
+       if ((cpld->config ^ config) & CPLD_CFG_nLED5) {
+               err = rb4xx_cpld_write_cmd(cpld, (value) ? CPLD_CMD_LED5_ON :
+                                                          CPLD_CMD_LED5_OFF);
+               if (err)
+                       return err;
+       }
+
+       cpld->config = config;
+       return 0;
+}
+
+int rb4xx_cpld_change_cfg(unsigned mask, unsigned value)
+{
+       int ret;
+
+       if (rb4xx_cpld == NULL)
+               return -ENODEV;
+
+       mutex_lock(&rb4xx_cpld->lock);
+       ret = __rb4xx_cpld_change_cfg(rb4xx_cpld, mask, value);
+       mutex_unlock(&rb4xx_cpld->lock);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(rb4xx_cpld_change_cfg);
+
+int rb4xx_cpld_read(unsigned char *rx_buf, unsigned count)
+{
+       static const unsigned char cmd[2] = { CPLD_CMD_READ_NAND, 0 };
+       struct spi_transfer t[2] = {
+               {
+                       .tx_buf = &cmd,
+                       .len = 2,
+               }, {
+                       .rx_buf = rx_buf,
+                       .len = count,
+               },
+       };
+       struct spi_message m;
+
+       if (rb4xx_cpld == NULL)
+               return -ENODEV;
+
+       spi_message_init(&m);
+       spi_message_add_tail(&t[0], &m);
+       spi_message_add_tail(&t[1], &m);
+       return spi_sync(rb4xx_cpld->spi, &m);
+}
+EXPORT_SYMBOL_GPL(rb4xx_cpld_read);
+
+int rb4xx_cpld_write(const unsigned char *buf, unsigned count)
+{
+       static const unsigned char cmd = CPLD_CMD_WRITE_NAND;
+       struct spi_transfer t[3] = {
+               {
+                       .tx_buf = &cmd,
+                       .len = 1,
+               }, {
+                       .tx_buf = buf,
+                       .len = count,
+                       .tx_nbits = SPI_NBITS_DUAL,
+               }, {
+                       .len = 1,
+                       .tx_nbits = SPI_NBITS_DUAL,
+               },
+       };
+       struct spi_message m;
+
+       if (rb4xx_cpld == NULL)
+               return -ENODEV;
+
+       spi_message_init(&m);
+       spi_message_add_tail(&t[0], &m);
+       spi_message_add_tail(&t[1], &m);
+       spi_message_add_tail(&t[2], &m);
+       return spi_sync(rb4xx_cpld->spi, &m);
+}
+EXPORT_SYMBOL_GPL(rb4xx_cpld_write);
+
+static int rb4xx_cpld_gpio_get(struct gpio_chip *chip, unsigned offset)
+{
+       struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
+       int ret;
+
+       mutex_lock(&cpld->lock);
+       ret = (cpld->config >> offset) & 1;
+       mutex_unlock(&cpld->lock);
+
+       return ret;
+}
+
+static void rb4xx_cpld_gpio_set(struct gpio_chip *chip, unsigned offset,
+                               int value)
+{
+       struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
+
+       mutex_lock(&cpld->lock);
+       __rb4xx_cpld_change_cfg(cpld, (1 << offset), !!value << offset);
+       mutex_unlock(&cpld->lock);
+}
+
+static int rb4xx_cpld_gpio_direction_input(struct gpio_chip *chip,
+                                          unsigned offset)
+{
+       return -EOPNOTSUPP;
+}
+
+static int rb4xx_cpld_gpio_direction_output(struct gpio_chip *chip,
+                                           unsigned offset,
+                                           int value)
+{
+       struct rb4xx_cpld *cpld = gpio_to_cpld(chip);
+       int ret;
+
+       mutex_lock(&cpld->lock);
+       ret = __rb4xx_cpld_change_cfg(cpld, (1 << offset), !!value << offset);
+       mutex_unlock(&cpld->lock);
+
+       return ret;
+}
+
+static int rb4xx_cpld_gpio_init(struct rb4xx_cpld *cpld, unsigned int base)
+{
+       int err;
+
+       /* init config */
+       cpld->config = CPLD_CFG_nLED1 | CPLD_CFG_nLED2 | CPLD_CFG_nLED3 |
+                      CPLD_CFG_nLED4 | CPLD_CFG_nCE;
+       rb4xx_cpld_write_cfg(cpld, cpld->config);
+
+       /* setup GPIO chip */
+       cpld->chip.label = DRV_NAME;
+
+       cpld->chip.get = rb4xx_cpld_gpio_get;
+       cpld->chip.set = rb4xx_cpld_gpio_set;
+       cpld->chip.direction_input = rb4xx_cpld_gpio_direction_input;
+       cpld->chip.direction_output = rb4xx_cpld_gpio_direction_output;
+
+       cpld->chip.base = base;
+       cpld->chip.ngpio = CPLD_NUM_GPIOS;
+       cpld->chip.can_sleep = 1;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4,5,0)
+       cpld->chip.dev = &cpld->spi->dev;
+#else
+       cpld->chip.parent = &cpld->spi->dev;
+#endif
+       cpld->chip.owner = THIS_MODULE;
+
+       err = gpiochip_add(&cpld->chip);
+       if (err)
+               dev_err(&cpld->spi->dev, "adding GPIO chip failed, err=%d\n",
+                       err);
+
+       return err;
+}
+
+static int rb4xx_cpld_probe(struct spi_device *spi)
+{
+       struct rb4xx_cpld *cpld;
+       struct rb4xx_cpld_platform_data *pdata;
+       int err;
+
+       pdata = spi->dev.platform_data;
+       if (!pdata) {
+               dev_dbg(&spi->dev, "no platform data\n");
+               return -EINVAL;
+       }
+
+       cpld = kzalloc(sizeof(*cpld), GFP_KERNEL);
+       if (!cpld) {
+               dev_err(&spi->dev, "no memory for private data\n");
+               return -ENOMEM;
+       }
+
+       mutex_init(&cpld->lock);
+       cpld->spi = spi_dev_get(spi);
+       dev_set_drvdata(&spi->dev, cpld);
+
+       spi->mode = SPI_MODE_0 | SPI_TX_DUAL;
+       spi->bits_per_word = 8;
+       err = spi_setup(spi);
+       if (err) {
+               dev_err(&spi->dev, "spi_setup failed, err=%d\n", err);
+               goto err_drvdata;
+       }
+
+       err = rb4xx_cpld_gpio_init(cpld, pdata->gpio_base);
+       if (err)
+               goto err_drvdata;
+
+       rb4xx_cpld = cpld;
+
+       return 0;
+
+err_drvdata:
+       dev_set_drvdata(&spi->dev, NULL);
+       kfree(cpld);
+
+       return err;
+}
+
+static int rb4xx_cpld_remove(struct spi_device *spi)
+{
+       struct rb4xx_cpld *cpld;
+
+       rb4xx_cpld = NULL;
+       cpld = dev_get_drvdata(&spi->dev);
+       dev_set_drvdata(&spi->dev, NULL);
+       kfree(cpld);
+
+       return 0;
+}
+
+static struct spi_driver rb4xx_cpld_driver = {
+       .driver = {
+               .name           = DRV_NAME,
+               .bus            = &spi_bus_type,
+               .owner          = THIS_MODULE,
+       },
+       .probe          = rb4xx_cpld_probe,
+       .remove         = rb4xx_cpld_remove,
+};
+
+static int __init rb4xx_cpld_init(void)
+{
+       return spi_register_driver(&rb4xx_cpld_driver);
+}
+module_init(rb4xx_cpld_init);
+
+static void __exit rb4xx_cpld_exit(void)
+{
+       spi_unregister_driver(&rb4xx_cpld_driver);
+}
+module_exit(rb4xx_cpld_exit);
+
+MODULE_DESCRIPTION(DRV_DESC);
+MODULE_VERSION(DRV_VERSION);
+MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
+MODULE_LICENSE("GPL v2");
diff --git a/target/linux/ath79/files/drivers/spi/spi-rb4xx.c b/target/linux/ath79/files/drivers/spi/spi-rb4xx.c
new file mode 100644 (file)
index 0000000..c5805ed
--- /dev/null
@@ -0,0 +1,442 @@
+/*
+ * SPI controller driver for the Mikrotik RB4xx boards
+ *
+ * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
+ *
+ * This file was based on the patches for Linux 2.6.27.39 published by
+ * MikroTik for their RouterBoard 4xx series devices.
+ *
+ * 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/clk.h>
+#include <linux/err.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/delay.h>
+#include <linux/spinlock.h>
+#include <linux/workqueue.h>
+#include <linux/platform_device.h>
+#include <linux/spi/spi.h>
+
+#include <asm/mach-ath79/ar71xx_regs.h>
+#include <asm/mach-ath79/ath79.h>
+
+#define DRV_NAME       "rb4xx-spi"
+#define DRV_DESC       "Mikrotik RB4xx SPI controller driver"
+#define DRV_VERSION    "0.1.0"
+
+#define SPI_CTRL_FASTEST       0x40
+#define SPI_FLASH_HZ           33333334
+#define SPI_CPLD_HZ            33333334
+
+#define CPLD_CMD_READ_FAST     0x0b
+
+#undef RB4XX_SPI_DEBUG
+
+struct rb4xx_spi {
+       void __iomem            *base;
+       struct spi_master       *master;
+
+       unsigned                spi_ctrl_flash;
+       unsigned                spi_ctrl_fread;
+
+       struct clk              *ahb_clk;
+       unsigned long           ahb_freq;
+
+       spinlock_t              lock;
+       struct list_head        queue;
+       int                     busy:1;
+       int                     cs_wait;
+};
+
+static unsigned spi_clk_low = AR71XX_SPI_IOC_CS1;
+
+#ifdef RB4XX_SPI_DEBUG
+static inline void do_spi_delay(void)
+{
+       ndelay(20000);
+}
+#else
+static inline void do_spi_delay(void) { }
+#endif
+
+static inline void do_spi_init(struct spi_device *spi)
+{
+       unsigned cs = AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1;
+
+       if (!(spi->mode & SPI_CS_HIGH))
+               cs ^= (spi->chip_select == 2) ? AR71XX_SPI_IOC_CS1 :
+                                               AR71XX_SPI_IOC_CS0;
+
+       spi_clk_low = cs;
+}
+
+static inline void do_spi_finish(void __iomem *base)
+{
+       do_spi_delay();
+       __raw_writel(AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1,
+                    base + AR71XX_SPI_REG_IOC);
+}
+
+static inline void do_spi_clk(void __iomem *base, int bit)
+{
+       unsigned bval = spi_clk_low | ((bit & 1) ? AR71XX_SPI_IOC_DO : 0);
+
+       do_spi_delay();
+       __raw_writel(bval, base + AR71XX_SPI_REG_IOC);
+       do_spi_delay();
+       __raw_writel(bval | AR71XX_SPI_IOC_CLK, base + AR71XX_SPI_REG_IOC);
+}
+
+static void do_spi_byte(void __iomem *base, unsigned char byte)
+{
+       do_spi_clk(base, byte >> 7);
+       do_spi_clk(base, byte >> 6);
+       do_spi_clk(base, byte >> 5);
+       do_spi_clk(base, byte >> 4);
+       do_spi_clk(base, byte >> 3);
+       do_spi_clk(base, byte >> 2);
+       do_spi_clk(base, byte >> 1);
+       do_spi_clk(base, byte);
+
+       pr_debug("spi_byte sent 0x%02x got 0x%02x\n",
+              (unsigned)byte,
+              (unsigned char)__raw_readl(base + AR71XX_SPI_REG_RDS));
+}
+
+static inline void do_spi_clk_fast(void __iomem *base, unsigned bit1,
+                                  unsigned bit2)
+{
+       unsigned bval = (spi_clk_low |
+                        ((bit1 & 1) ? AR71XX_SPI_IOC_DO : 0) |
+                        ((bit2 & 1) ? AR71XX_SPI_IOC_CS2 : 0));
+       do_spi_delay();
+       __raw_writel(bval, base + AR71XX_SPI_REG_IOC);
+       do_spi_delay();
+       __raw_writel(bval | AR71XX_SPI_IOC_CLK, base + AR71XX_SPI_REG_IOC);
+}
+
+static void do_spi_byte_fast(void __iomem *base, unsigned char byte)
+{
+       do_spi_clk_fast(base, byte >> 7, byte >> 6);
+       do_spi_clk_fast(base, byte >> 5, byte >> 4);
+       do_spi_clk_fast(base, byte >> 3, byte >> 2);
+       do_spi_clk_fast(base, byte >> 1, byte >> 0);
+
+       pr_debug("spi_byte_fast sent 0x%02x got 0x%02x\n",
+              (unsigned)byte,
+              (unsigned char) __raw_readl(base + AR71XX_SPI_REG_RDS));
+}
+
+static int rb4xx_spi_txrx(void __iomem *base, struct spi_transfer *t)
+{
+       const unsigned char *tx_ptr = t->tx_buf;
+       unsigned char *rx_ptr = t->rx_buf;
+       unsigned i;
+
+       pr_debug("spi_txrx len %u tx %u rx %u\n",
+              t->len,
+              (t->tx_buf ? 1 : 0),
+              (t->rx_buf ? 1 : 0));
+
+       for (i = 0; i < t->len; ++i) {
+               unsigned char sdata = tx_ptr ? tx_ptr[i] : 0;
+
+               if (t->tx_nbits == SPI_NBITS_DUAL)
+                       do_spi_byte_fast(base, sdata);
+               else
+                       do_spi_byte(base, sdata);
+
+               if (rx_ptr)
+                       rx_ptr[i] = __raw_readl(base + AR71XX_SPI_REG_RDS) & 0xff;
+       }
+
+       return i;
+}
+
+static int rb4xx_spi_msg(struct rb4xx_spi *rbspi, struct spi_message *m)
+{
+       struct spi_transfer *t = NULL;
+       void __iomem *base = rbspi->base;
+
+       m->status = 0;
+       if (list_empty(&m->transfers))
+               return -1;
+
+       __raw_writel(AR71XX_SPI_FS_GPIO, base + AR71XX_SPI_REG_FS);
+       __raw_writel(SPI_CTRL_FASTEST, base + AR71XX_SPI_REG_CTRL);
+       do_spi_init(m->spi);
+
+       list_for_each_entry(t, &m->transfers, transfer_list) {
+               int len;
+
+               len = rb4xx_spi_txrx(base, t);
+               if (len != t->len) {
+                       m->status = -EMSGSIZE;
+                       break;
+               }
+               m->actual_length += len;
+
+               if (t->cs_change) {
+                       if (list_is_last(&t->transfer_list, &m->transfers)) {
+                               /* wait for continuation */
+                               return m->spi->chip_select;
+                       }
+                       do_spi_finish(base);
+                       ndelay(100);
+               }
+       }
+
+       do_spi_finish(base);
+       __raw_writel(rbspi->spi_ctrl_flash, base + AR71XX_SPI_REG_CTRL);
+       __raw_writel(0, base + AR71XX_SPI_REG_FS);
+       return -1;
+}
+
+static void rb4xx_spi_process_queue_locked(struct rb4xx_spi *rbspi,
+                                          unsigned long *flags)
+{
+       int cs = rbspi->cs_wait;
+
+       rbspi->busy = 1;
+       while (!list_empty(&rbspi->queue)) {
+               struct spi_message *m;
+
+               list_for_each_entry(m, &rbspi->queue, queue)
+                       if (cs < 0 || cs == m->spi->chip_select)
+                               break;
+
+               if (&m->queue == &rbspi->queue)
+                       break;
+
+               list_del_init(&m->queue);
+               spin_unlock_irqrestore(&rbspi->lock, *flags);
+
+               cs = rb4xx_spi_msg(rbspi, m);
+               m->complete(m->context);
+
+               spin_lock_irqsave(&rbspi->lock, *flags);
+       }
+
+       rbspi->cs_wait = cs;
+       rbspi->busy = 0;
+
+       if (cs >= 0) {
+               /* TODO: add timer to unlock cs after 1s inactivity */
+       }
+}
+
+static int rb4xx_spi_transfer(struct spi_device *spi,
+                             struct spi_message *m)
+{
+       struct rb4xx_spi *rbspi = spi_master_get_devdata(spi->master);
+       unsigned long flags;
+
+       m->actual_length = 0;
+       m->status = -EINPROGRESS;
+
+       spin_lock_irqsave(&rbspi->lock, flags);
+       list_add_tail(&m->queue, &rbspi->queue);
+       if (rbspi->busy ||
+           (rbspi->cs_wait >= 0 && rbspi->cs_wait != m->spi->chip_select)) {
+               /* job will be done later */
+               spin_unlock_irqrestore(&rbspi->lock, flags);
+               return 0;
+       }
+
+       /* process job in current context */
+       rb4xx_spi_process_queue_locked(rbspi, &flags);
+       spin_unlock_irqrestore(&rbspi->lock, flags);
+
+       return 0;
+}
+
+static int rb4xx_spi_setup(struct spi_device *spi)
+{
+       struct rb4xx_spi *rbspi = spi_master_get_devdata(spi->master);
+       unsigned long flags;
+
+       if (spi->mode & ~(SPI_CS_HIGH | SPI_TX_DUAL)) {
+               dev_err(&spi->dev, "mode %x not supported\n",
+                       (unsigned) spi->mode);
+               return -EINVAL;
+       }
+
+       if (spi->bits_per_word != 8 && spi->bits_per_word != 0) {
+               dev_err(&spi->dev, "bits_per_word %u not supported\n",
+                       (unsigned) spi->bits_per_word);
+               return -EINVAL;
+       }
+
+       spin_lock_irqsave(&rbspi->lock, flags);
+       if (rbspi->cs_wait == spi->chip_select && !rbspi->busy) {
+               rbspi->cs_wait = -1;
+               rb4xx_spi_process_queue_locked(rbspi, &flags);
+       }
+       spin_unlock_irqrestore(&rbspi->lock, flags);
+
+       return 0;
+}
+
+static unsigned get_spi_ctrl(struct rb4xx_spi *rbspi, unsigned hz_max,
+                            const char *name)
+{
+       unsigned div;
+
+       div = (rbspi->ahb_freq - 1) / (2 * hz_max);
+
+       /*
+        * CPU has a bug at (div == 0) - first bit read is random
+        */
+       if (div == 0)
+               ++div;
+
+       if (name) {
+               unsigned ahb_khz = (rbspi->ahb_freq + 500) / 1000;
+               unsigned div_real = 2 * (div + 1);
+               pr_debug("rb4xx: %s SPI clock %u kHz (AHB %u kHz / %u)\n",
+                      name,
+                      ahb_khz / div_real,
+                      ahb_khz, div_real);
+       }
+
+       return SPI_CTRL_FASTEST + div;
+}
+
+static int rb4xx_spi_probe(struct platform_device *pdev)
+{
+       struct spi_master *master;
+       struct rb4xx_spi *rbspi;
+       struct resource *r;
+       int err = 0;
+
+       master = spi_alloc_master(&pdev->dev, sizeof(*rbspi));
+       if (master == NULL) {
+               dev_err(&pdev->dev, "no memory for spi_master\n");
+               err = -ENOMEM;
+               goto err_out;
+       }
+
+       master->bus_num = 0;
+       master->num_chipselect = 3;
+       master->mode_bits = SPI_TX_DUAL;
+       master->setup = rb4xx_spi_setup;
+       master->transfer = rb4xx_spi_transfer;
+
+       rbspi = spi_master_get_devdata(master);
+
+       rbspi->ahb_clk = clk_get(&pdev->dev, "ahb");
+       if (IS_ERR(rbspi->ahb_clk)) {
+               err = PTR_ERR(rbspi->ahb_clk);
+               printk("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
+               goto err_put_master;
+       }
+
+       printk("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
+       err = clk_prepare_enable(rbspi->ahb_clk);
+       if (err)
+               goto err_clk_put;
+
+       printk("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
+       rbspi->ahb_freq = clk_get_rate(rbspi->ahb_clk);
+       if (!rbspi->ahb_freq) {
+               err = -EINVAL;
+               goto err_clk_disable;
+       }
+
+       printk("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
+       platform_set_drvdata(pdev, rbspi);
+
+       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       if (r == NULL) {
+               err = -ENOENT;
+               goto err_clk_disable;
+       }
+printk("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
+
+       rbspi->base = ioremap(r->start, r->end - r->start + 1);
+       if (!rbspi->base) {
+               err = -ENXIO;
+               goto err_clk_disable;
+       }
+printk("%s:%s[%d]\n", __FILE__, __func__, __LINE__);
+
+       rbspi->master = master;
+       rbspi->spi_ctrl_flash = get_spi_ctrl(rbspi, SPI_FLASH_HZ, "FLASH");
+       rbspi->spi_ctrl_fread = get_spi_ctrl(rbspi, SPI_CPLD_HZ, "CPLD");
+       rbspi->cs_wait = -1;
+
+       spin_lock_init(&rbspi->lock);
+       INIT_LIST_HEAD(&rbspi->queue);
+
+       err = spi_register_master(master);
+       if (err) {
+               dev_err(&pdev->dev, "failed to register SPI master\n");
+               goto err_iounmap;
+       }
+
+       return 0;
+
+err_iounmap:
+       iounmap(rbspi->base);
+err_clk_disable:
+       clk_disable_unprepare(rbspi->ahb_clk);
+err_clk_put:
+       clk_put(rbspi->ahb_clk);
+err_put_master:
+       platform_set_drvdata(pdev, NULL);
+       spi_master_put(master);
+err_out:
+       return err;
+}
+
+static int rb4xx_spi_remove(struct platform_device *pdev)
+{
+       struct rb4xx_spi *rbspi = platform_get_drvdata(pdev);
+
+       iounmap(rbspi->base);
+       clk_disable_unprepare(rbspi->ahb_clk);
+       clk_put(rbspi->ahb_clk);
+       platform_set_drvdata(pdev, NULL);
+       spi_master_put(rbspi->master);
+
+       return 0;
+}
+
+static const struct of_device_id rb4xx_spi_match[] = {
+       { .compatible = "mikrotik,rb4xx-spi", },
+       {},
+};
+MODULE_DEVICE_TABLE(of, octeon_spi_match);
+
+static struct platform_driver rb4xx_spi_drv = {
+       .probe          = rb4xx_spi_probe,
+       .remove         = rb4xx_spi_remove,
+       .driver         = {
+               .name   = DRV_NAME,
+               .of_match_table = rb4xx_spi_match,
+       },
+};
+
+static int __init rb4xx_spi_init(void)
+{
+       return platform_driver_register(&rb4xx_spi_drv);
+}
+subsys_initcall(rb4xx_spi_init);
+
+static void __exit rb4xx_spi_exit(void)
+{
+       platform_driver_unregister(&rb4xx_spi_drv);
+}
+
+module_exit(rb4xx_spi_exit);
+
+MODULE_DESCRIPTION(DRV_DESC);
+MODULE_VERSION(DRV_VERSION);
+MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
+MODULE_LICENSE("GPL v2");
index 734f27e689c9d76abedad8c89f22a24006ba86ae..aa5f17d76983fe04527fbe63415bce0ea9f81d5e 100644 (file)
@@ -73,6 +73,7 @@ include ./generic-ubnt.mk
 endif
 ifeq ($(SUBTARGET),nand)
 include ./nand.mk
+include ./nand-mikrotik.mk
 endif
 ifeq ($(SUBTARGET),tiny)
 include ./tiny.mk
diff --git a/target/linux/ath79/image/mikrotik.mk b/target/linux/ath79/image/mikrotik.mk
new file mode 100644 (file)
index 0000000..0cf3b76
--- /dev/null
@@ -0,0 +1,64 @@
+define Device/mikrotik
+  DEVICE_PACKAGES := kmod-usb-core kmod-usb-ohci kmod-usb2 kmod-usb-ledtrig-usbport
+  LOADER_TYPE := elf
+#  KERNEL_NAME := vmlinux.bin.lzma
+  BOARD_NAME := routerboard
+  KERNEL := kernel-bin | loader-kernel
+  KERNEL_INITRAMFS := kernel-bin | append-dtb | lzma | loader-kernel
+#  KERNEL_INITRAMFS_NAME := vmlinux-initramfs.elf
+  MIKROTIK_CHUNKSIZE :=
+  IMAGE/sysupgrade.bin/squashfs :=
+endef
+DEVICE_VARS += MIKROTIK_CHUNKSIZE
+
+define Device/mikrotik-nand
+  $(Device/mikrotik)
+  IMAGE/sysupgrade.bin/squashfs = append-kernel | \
+       kernel2minor -s $$(MIKROTIK_CHUNKSIZE) -e -c | sysupgrade-tar kernel=$$$$@
+endef
+
+define Device/mikrotik-nand-64
+  $(Device/mikrotik-nand)
+  MIKROTIK_CHUNKSIZE := 512
+endef
+
+define Device/mikrotik-nand-large
+  $(Device/mikrotik-nand)
+  MIKROTIK_CHUNKSIZE := 2048
+endef
+
+define Device/mikrotik-nand-large-ac
+  $(Device/mikrotik-nand)
+  MIKROTIK_CHUNKSIZE := 2048
+  DEVICE_TITLE := MikroTik RouterBoard (>= 128 MB NAND, 802.11ac)
+  DEVICE_PACKAGES += kmod-ath10k-ct ath10k-firmware-qca988x-ct
+  SUPPORTED_DEVICES := rb-921gs-5hpacd-r2
+endef
+
+define Device/rb-nor-flash-16M
+  $(Device/mikrotik)
+  DEVICE_TITLE := MikroTik RouterBoard (16 MB SPI NOR)
+  DEVICE_PACKAGES := rbcfg rssileds -nand-utils kmod-ledtrig-gpio
+  IMAGE_SIZE := 16000k
+  KERNEL_INSTALL := 1
+  IMAGE/sysupgrade.bin := append-kernel | kernel2minor -s 1024 -e | pad-to $$$$(BLOCKSIZE) | \
+       append-rootfs | pad-rootfs | append-metadata | check-size $$$$(IMAGE_SIZE)
+endef
+
+define Device/rb-nor-flash-16M-ac
+  $(Device/rb-nor-flash-16M)
+  DEVICE_TITLE := MikroTik RouterBoard (16 MB SPI NOR, 802.11ac)
+  DEVICE_PACKAGES += kmod-ath10k-ct ath10k-firmware-qca988x-ct ath10k-firmware-qca9887-ct kmod-usb-ehci
+  SUPPORTED_DEVICES += rb-wapg-5hact2hnd
+endef
+
+define CompressLzma
+  $(STAGING_DIR_HOST)/bin/lzma e $(1) -lc1 -lp2 -pb2 $(3) $(2)
+endef
+
+define Image/1Prepare
+       $(call CompressLzma,$(KDIR)/vmlinux,$(KDIR)/vmlinux.bin.lzma)
+ifneq ($(CONFIG_TARGET_ROOTFS_INITRAMFS),)
+       $(call CompressLzma,$(KDIR)/vmlinux-initramfs,$(KDIR)/vmlinux-initramfs.bin.lzma)
+endif
+endef
diff --git a/target/linux/ath79/image/nand-mikrotik.mk b/target/linux/ath79/image/nand-mikrotik.mk
new file mode 100644 (file)
index 0000000..a0c9581
--- /dev/null
@@ -0,0 +1,9 @@
+include ./mikrotik.mk
+
+define Device/mikrotik_rb450g
+  ATH_SOC := ar7161
+  $(Device/mikrotik-nand-64)
+  DEVICE_TITLE := MikroTik RouterBoard RB450g
+  SUPPORTED_DEVICES := rb450g
+endef
+TARGET_DEVICES += mikrotik_rb450g
index 738c29c9b1e35eac27f622db884fbed1cfa6298e..789d20b69ed37a35382f9999b6e4140cc90c23d0 100644 (file)
@@ -3,6 +3,7 @@ CONFIG_MTD_NAND=y
 CONFIG_MTD_NAND_BCH=y
 CONFIG_MTD_NAND_ECC=y
 CONFIG_MTD_NAND_ECC_BCH=y
+CONFIG_MTD_NAND_RB4XX=y
 CONFIG_MTD_SPINAND_MT29F=y
 CONFIG_MTD_SPINAND_ONDIEECC=y
 CONFIG_MTD_UBI=y
@@ -11,4 +12,6 @@ CONFIG_MTD_UBI_BEB_LIMIT=20
 # CONFIG_MTD_UBI_FASTMAP is not set
 # CONFIG_MTD_UBI_GLUEBI is not set
 CONFIG_MTD_UBI_WL_THRESHOLD=4096
+CONFIG_SPI_RB4XX=y
+CONFIG_SPI_RB4XX_CPLD=y
 # CONFIG_UBIFS_FS is not set
index a53603d27b681822bc342c2fbc4428eec7988f42..f6dab48bddc3c669c3241c333f31ae1edba932d0 100644 (file)
@@ -1,5 +1,5 @@
 BOARDNAME := Generic devices with NAND flash
-FEATURES += squashfs nand rtc
+FEATURES += squashfs nand rtc minor
 
 define Target/Description
        Build firmware for Atheros AR71xx/AR913x based boards with
diff --git a/target/linux/ath79/patches-4.14/409-mtd-rb4xx_nand_driver.patch b/target/linux/ath79/patches-4.14/409-mtd-rb4xx_nand_driver.patch
new file mode 100644 (file)
index 0000000..c4b26ec
--- /dev/null
@@ -0,0 +1,21 @@
+--- a/drivers/mtd/nand/Kconfig
++++ b/drivers/mtd/nand/Kconfig
+@@ -563,4 +563,8 @@ config MTD_NAND_MTK
+         Enables support for NAND controller on MTK SoCs.
+         This controller is found on mt27xx, mt81xx, mt65xx SoCs.
++config MTD_NAND_RB4XX
++      tristate "NAND flash driver for RouterBoard 4xx series"
++      depends on MTD_NAND && ATH79_MACH_RB4XX
++
+ endif # MTD_NAND
+--- a/drivers/mtd/nand/Makefile
++++ b/drivers/mtd/nand/Makefile
+@@ -34,6 +34,7 @@ obj-$(CONFIG_MTD_NAND_CM_X270)               += cmx27
+ obj-$(CONFIG_MTD_NAND_PXA3xx)         += pxa3xx_nand.o
+ obj-$(CONFIG_MTD_NAND_TMIO)           += tmio_nand.o
+ obj-$(CONFIG_MTD_NAND_PLATFORM)               += plat_nand.o
++obj-$(CONFIG_MTD_NAND_RB4XX)          += rb4xx_nand.o
+ obj-$(CONFIG_MTD_NAND_PASEMI)         += pasemi_nand.o
+ obj-$(CONFIG_MTD_NAND_ORION)          += orion_nand.o
+ obj-$(CONFIG_MTD_NAND_OXNAS)          += oxnas_nand.o
diff --git a/target/linux/ath79/patches-4.14/432-spi-rb4xx-spi-driver.patch b/target/linux/ath79/patches-4.14/432-spi-rb4xx-spi-driver.patch
new file mode 100644 (file)
index 0000000..9b01489
--- /dev/null
@@ -0,0 +1,25 @@
+--- a/drivers/spi/Kconfig
++++ b/drivers/spi/Kconfig
+@@ -563,6 +563,12 @@ config SPI_QUP
+         This driver can also be built as a module.  If so, the module
+         will be called spi_qup.
++config SPI_RB4XX
++      tristate "Mikrotik RB4XX SPI master"
++      depends on SPI_MASTER && ATH79_MACH_RB4XX
++      help
++        SPI controller driver for the Mikrotik RB4xx series boards.
++
+ config SPI_S3C24XX
+       tristate "Samsung S3C24XX series SPI"
+       depends on ARCH_S3C24XX
+--- a/drivers/spi/Makefile
++++ b/drivers/spi/Makefile
+@@ -77,6 +77,7 @@ obj-$(CONFIG_SPI_PPC4xx)             += spi-ppc4xx.
+ spi-pxa2xx-platform-objs              := spi-pxa2xx.o spi-pxa2xx-dma.o
+ obj-$(CONFIG_SPI_PXA2XX)              += spi-pxa2xx-platform.o
+ obj-$(CONFIG_SPI_PXA2XX_PCI)          += spi-pxa2xx-pci.o
++obj-$(CONFIG_SPI_RB4XX)                       += spi-rb4xx.o
+ obj-$(CONFIG_SPI_QUP)                 += spi-qup.o
+ obj-$(CONFIG_SPI_ROCKCHIP)            += spi-rockchip.o
+ obj-$(CONFIG_SPI_RB4XX)                       += spi-rb4xx.o
diff --git a/target/linux/ath79/patches-4.14/433-spi-rb4xx-cpld-driver.patch b/target/linux/ath79/patches-4.14/433-spi-rb4xx-cpld-driver.patch
new file mode 100644 (file)
index 0000000..02c30c6
--- /dev/null
@@ -0,0 +1,26 @@
+--- a/drivers/spi/Kconfig
++++ b/drivers/spi/Kconfig
+@@ -801,6 +801,13 @@ config SPI_TLE62X0
+         sysfs interface, with each line presented as a kind of GPIO
+         exposing both switch control and diagnostic feedback.
++config SPI_RB4XX_CPLD
++      tristate "MikroTik RB4XX CPLD driver"
++      depends on ATH79_MACH_RB4XX
++      help
++        SPI driver for the Xilinx CPLD chip present on the
++        MikroTik RB4xx boards.
++
+ #
+ # Add new SPI protocol masters in alphabetical order above this line
+ #
+--- a/drivers/spi/Makefile
++++ b/drivers/spi/Makefile
+@@ -78,6 +78,7 @@ spi-pxa2xx-platform-objs             := spi-pxa2xx.
+ obj-$(CONFIG_SPI_PXA2XX)              += spi-pxa2xx-platform.o
+ obj-$(CONFIG_SPI_PXA2XX_PCI)          += spi-pxa2xx-pci.o
+ obj-$(CONFIG_SPI_RB4XX)                       += spi-rb4xx.o
++obj-$(CONFIG_SPI_RB4XX_CPLD)          += spi-rb4xx-cpld.o
+ obj-$(CONFIG_SPI_QUP)                 += spi-qup.o
+ obj-$(CONFIG_SPI_ROCKCHIP)            += spi-rockchip.o
+ obj-$(CONFIG_SPI_RB4XX)                       += spi-rb4xx.o
diff --git a/target/linux/ath79/patches-4.14/441-leds-rb750-led-driver.patch b/target/linux/ath79/patches-4.14/441-leds-rb750-led-driver.patch
new file mode 100644 (file)
index 0000000..012e6b4
--- /dev/null
@@ -0,0 +1,23 @@
+--- a/drivers/leds/Kconfig
++++ b/drivers/leds/Kconfig
+@@ -703,6 +703,10 @@ config LEDS_WNDR3700_USB
+         This option enables support for the USB LED found on the
+         NETGEAR WNDR3700 board.
++config LEDS_RB750
++      tristate "LED driver for the Mikrotik RouterBOARD 750"
++      depends on LEDS_CLASS && ATH79_MACH_RB750
++
+ comment "LED Triggers"
+ source "drivers/leds/trigger/Kconfig"
+--- a/drivers/leds/Makefile
++++ b/drivers/leds/Makefile
+@@ -57,6 +57,7 @@ obj-$(CONFIG_LEDS_INTEL_SS4200)              += leds
+ obj-$(CONFIG_LEDS_LT3593)             += leds-lt3593.o
+ obj-$(CONFIG_LEDS_ADP5520)            += leds-adp5520.o
+ obj-$(CONFIG_LEDS_MC13783)            += leds-mc13783.o
++obj-$(CONFIG_LEDS_RB750)              += leds-rb750.o
+ obj-$(CONFIG_LEDS_NS2)                        += leds-ns2.o
+ obj-$(CONFIG_LEDS_NETXBIG)            += leds-netxbig.o
+ obj-$(CONFIG_LEDS_ASIC3)              += leds-asic3.o