From b960a4f0c50828c057af5bed2e43ca2625af9db7 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Wed, 29 Jul 2015 16:13:14 +0200 Subject: [PATCH] eeprom: move OF specific functions to of.c Signed-off-by: Felix Fietkau --- Makefile | 2 ++ eeprom.c | 93 +------------------------------------------------- of.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ of.h | 39 +++++++++++++++++++++ 4 files changed, 144 insertions(+), 92 deletions(-) create mode 100644 of.c create mode 100644 of.h diff --git a/Makefile b/Makefile index cccd558b..796b98a4 100644 --- a/Makefile +++ b/Makefile @@ -7,3 +7,5 @@ mt76pci-y := \ main.o init.o debugfs.o tx.o util.o \ core.o mac.o eeprom.o mcu.o phy.o \ trace.o + +mt76pci-$(CONFIG_OF) += of.o diff --git a/eeprom.c b/eeprom.c index ad570cda..c4b6999a 100644 --- a/eeprom.c +++ b/eeprom.c @@ -11,13 +11,11 @@ * GNU General Public License for more details. */ -#include -#include -#include #include #include #include "mt76.h" #include "eeprom.h" +#include "of.h" #define EE_FIELD(_name, _value) [MT_EE_##_name] = (_value) | 1 @@ -41,74 +39,6 @@ mt76_eeprom_get_macaddr(struct mt76_dev *dev) return 0; } -#ifdef CONFIG_OF -static int mt76_check_eeprom(struct mt76_dev *dev, const char *type) -{ - u16 val = get_unaligned_le16(dev->eeprom.data); - switch (val) { - case 0x7662: - case 0x7612: - return 0; - default: - printk("%s EEPROM data check failed: %04x\n", type, val); - return -EINVAL; - } -} -#endif - -static int mt76_get_of_eeprom(struct mt76_dev *dev, int len) -{ - int ret = -ENOENT; -#ifdef CONFIG_OF - struct device_node *np = dev->dev->of_node; - struct mtd_info *mtd; - const __be32 *list; - const char *part; - phandle phandle; - int offset = 0; - int size; - size_t retlen; - - if (!np) - return -ENOENT; - - list = of_get_property(np, "mediatek,mtd-eeprom", &size); - if (!list) - return -ENOENT; - - phandle = be32_to_cpup(list++); - if (!phandle) - return -ENOENT; - - np = of_find_node_by_phandle(phandle); - if (!np) - return -EINVAL; - - part = of_get_property(np, "label", NULL); - if (!part) - part = np->name; - - mtd = get_mtd_device_nm(part); - if (IS_ERR(mtd)) - return PTR_ERR(mtd); - - if (size <= sizeof(*list)) - return -EINVAL; - - offset = be32_to_cpup(list); - ret = mtd_read(mtd, offset, len, &retlen, dev->eeprom.data); - put_mtd_device(mtd); - if (ret) - return ret; - - if (retlen < len) - return -EINVAL; - - ret = mt76_check_eeprom(dev, "Flash"); -#endif - return ret; -} - static void mt76_eeprom_parse_hw_cap(struct mt76_dev *dev) { @@ -128,27 +58,6 @@ mt76_eeprom_parse_hw_cap(struct mt76_dev *dev) } } -static void -mt76_get_of_overrides(struct mt76_dev *dev) -{ -#ifdef CONFIG_OF - struct device_node *np = dev->dev->of_node; - const __be32 *val; - int size; - - if (!np) - return; - - val = of_get_property(np, "mediatek,2ghz", &size); - if (val) - dev->cap.has_2ghz = be32_to_cpup(val); - - val = of_get_property(np, "mediatek,5ghz", &size); - if (val) - dev->cap.has_5ghz = be32_to_cpup(val); -#endif -} - static int mt76_efuse_read(struct mt76_dev *dev, u16 addr, u8 *data) { diff --git a/of.c b/of.c new file mode 100644 index 00000000..a9b590ba --- /dev/null +++ b/of.c @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2014 Felix Fietkau + * + * 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 + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include "mt76.h" +#include "eeprom.h" +#include "of.h" + +static int mt76_check_eeprom(struct mt76_dev *dev, const char *type) +{ + u16 val = get_unaligned_le16(dev->eeprom.data); + switch (val) { + case 0x7662: + case 0x7612: + return 0; + default: + printk("%s EEPROM data check failed: %04x\n", type, val); + return -EINVAL; + } +} + +int +mt76_get_of_eeprom(struct mt76_dev *dev, int len) +{ + struct device_node *np = dev->dev->of_node; + struct mtd_info *mtd; + const __be32 *list; + const char *part; + phandle phandle; + int offset = 0; + int size; + size_t retlen; + int ret; + + if (!np) + return -ENOENT; + + list = of_get_property(np, "mediatek,mtd-eeprom", &size); + if (!list) + return -ENOENT; + + phandle = be32_to_cpup(list++); + if (!phandle) + return -ENOENT; + + np = of_find_node_by_phandle(phandle); + if (!np) + return -EINVAL; + + part = of_get_property(np, "label", NULL); + if (!part) + part = np->name; + + mtd = get_mtd_device_nm(part); + if (IS_ERR(mtd)) + return PTR_ERR(mtd); + + if (size <= sizeof(*list)) + return -EINVAL; + + offset = be32_to_cpup(list); + ret = mtd_read(mtd, offset, len, &retlen, dev->eeprom.data); + put_mtd_device(mtd); + if (ret) + return ret; + + if (retlen < len) + return -EINVAL; + + return mt76_check_eeprom(dev, "Flash"); +} + +void +mt76_get_of_overrides(struct mt76_dev *dev) +{ + struct device_node *np = dev->dev->of_node; + const __be32 *val; + int size; + + if (!np) + return; + + val = of_get_property(np, "mediatek,2ghz", &size); + if (val) + dev->cap.has_2ghz = be32_to_cpup(val); + + val = of_get_property(np, "mediatek,5ghz", &size); + if (val) + dev->cap.has_5ghz = be32_to_cpup(val); +} diff --git a/of.h b/of.h new file mode 100644 index 00000000..6f1d7c01 --- /dev/null +++ b/of.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2014 Felix Fietkau + * + * 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 + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef __MT76_OF_H +#define __MT76_OF_H + +#include "mt76.h" + +#ifdef CONFIG_OF + +int mt76_get_of_eeprom(struct mt76_dev *dev, int len); +void mt76_get_of_overrides(struct mt76_dev *dev); + +#else + +static inline int +mt76_get_of_eeprom(struct mt76_dev *dev, int len) +{ + return -ENOENT; +} + +static inline void +mt76_get_of_overrides(struct mt76_dev *dev) +{ +} + +#endif /* CONFIG_OF */ + +#endif