tools: edimax_fw_header: fix suspicious memset usage
[openwrt/openwrt.git] / target / linux / adm5120 / files / drivers / mtd / maps / adm5120-flash.c
index e07fb96d260a6287b3dbdf2e6537e52b757e9ad7..f6a86f4897b28afaed64f10376295e22c1b42867 100644 (file)
@@ -1,29 +1,15 @@
 /*
- *  $Id$
- *
  *  Platform driver for NOR flash devices on ADM5120 based boards
  *
- *  Copyright (C) 2007 OpenWrt.org
- *  Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
+ *  Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
  *
  *  This file was derived from: drivers/mtd/map/physmap.c
  *     Copyright (C) 2003 MontaVista Software Inc.
  *     Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  *
- *  This program is free software; you can redistribute it and/or
- *  modify it under the terms of the GNU General Public License
- *  as published by the Free Software Foundation; either version 2
- *  of the License, or (at your option) any later version.
- *
- *  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.
- *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the
- *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- *  Boston, MA  02110-1301, USA.
+ *  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/mtd/map.h>
 #include <linux/mtd/partitions.h>
 
-#include <adm5120_defs.h>
-#include <adm5120_switch.h>
-#include <adm5120_mpmc.h>
-#include <adm5120_platform.h>
+#include <asm/mach-adm5120/adm5120_defs.h>
+#include <asm/mach-adm5120/adm5120_switch.h>
+#include <asm/mach-adm5120/adm5120_mpmc.h>
+#include <asm/mach-adm5120/adm5120_platform.h>
 
 #define DRV_NAME       "adm5120-flash"
 #define DRV_DESC       "ADM5120 flash MAP driver"
@@ -59,7 +45,7 @@
 
 struct adm5120_map_info {
        struct map_info map;
-       void            (*switch_bank)(unsigned);
+       void            (*switch_bank)(unsigned);
        unsigned long   window_size;
 };
 
@@ -68,10 +54,6 @@ struct adm5120_flash_info {
        struct resource         *res;
        struct platform_device  *dev;
        struct adm5120_map_info amap;
-#ifdef CONFIG_MTD_PARTITIONS
-       int                     nr_parts;
-       struct mtd_partition    *parts[MAX_PARSED_PARTS];
-#endif
 };
 
 struct flash_desc {
@@ -103,15 +85,14 @@ static struct flash_desc flash_descs[2] = {
        }
 };
 
-static const char *probe_types[] = {
+static const char const *probe_types[] = {
        "cfi_probe",
        "jedec_probe",
        "map_rom",
        NULL
 };
 
-#ifdef CONFIG_MTD_PARTITIONS
-static const char *parse_types[] = {
+static const char const *parse_types[] = {
        "cmdlinepart",
 #ifdef CONFIG_MTD_REDBOOT_PARTS
        "RedBoot",
@@ -119,8 +100,8 @@ static const char *parse_types[] = {
 #ifdef CONFIG_MTD_MYLOADER_PARTS
        "MyLoader",
 #endif
+       NULL,
 };
-#endif
 
 #define BANK_SIZE      (2<<20)
 #define BANK_SIZE_MAX  (4<<20)
@@ -220,14 +201,15 @@ static int adm5120_flash_initres(struct adm5120_flash_info *info)
        struct map_info *map = &info->amap.map;
        int err = 0;
 
-       info->res = request_mem_region(map->phys, map->size, map->name);
+       info->res = request_mem_region(map->phys, info->amap.window_size,
+                       map->name);
        if (info->res == NULL) {
                MAP_ERR(map, "could not reserve memory region\n");
                err = -ENOMEM;
                goto out;
        }
 
-       map->virt = ioremap_nocache(map->phys, map->size);
+       map->virt = ioremap_nocache(map->phys, info->amap.window_size);
        if (map->virt == NULL) {
                MAP_ERR(map, "failed to ioremap flash region\n");
                err = -ENOMEM;
@@ -244,9 +226,9 @@ static int adm5120_flash_initinfo(struct adm5120_flash_info *info,
        struct map_info *map = &info->amap.map;
        struct adm5120_flash_platform_data *pdata = dev->dev.platform_data;
        struct flash_desc *fdesc;
-       u32 t;
+       u32 t = 0;
 
-       map->name = dev->dev.bus_id;
+       map->name = dev_name(&dev->dev);
 
        if (dev->id > 1) {
                MAP_ERR(map, "invalid flash id\n");
@@ -255,12 +237,18 @@ static int adm5120_flash_initinfo(struct adm5120_flash_info *info,
 
        fdesc = &flash_descs[dev->id];
 
-       /* get memory window size */
-       t = SW_READ_REG(MEMCTRL) >> fdesc->srs_shift;
-       t &= MEMCTRL_SRS_MASK;
-       info->amap.window_size = flash_sizes[t];
+       if (pdata)
+               info->amap.window_size = pdata->window_size;
+
        if (info->amap.window_size == 0) {
-               MAP_ERR(map, "invalid flash size detected\n");
+               /* get memory window size */
+               t = SW_READ_REG(SWITCH_REG_MEMCTRL) >> fdesc->srs_shift;
+               t &= MEMCTRL_SRS_MASK;
+               info->amap.window_size = flash_sizes[t];
+       }
+
+       if (info->amap.window_size == 0) {
+               MAP_ERR(map, "unable to determine window size\n");
                goto err_out;
        }
 
@@ -325,89 +313,6 @@ static void adm5120_flash_initbanks(struct adm5120_flash_info *info)
        info->mtd->size = info->amap.window_size;
 }
 
-#ifdef CONFIG_MTD_PARTITIONS
-static int adm5120_flash_initparts(struct adm5120_flash_info *info)
-{
-       struct adm5120_flash_platform_data *pdata;
-       struct map_info *map = &info->amap.map;
-       int num_parsers;
-       const char *parser[2];
-       int err = 0;
-       int nr_parts;
-       int i;
-
-       info->nr_parts = 0;
-
-       pdata = info->dev->dev.platform_data;
-       if (pdata == NULL)
-               goto out;
-
-       if (pdata->nr_parts) {
-               MAP_INFO(map, "adding static partitions\n");
-               err = add_mtd_partitions(info->mtd, pdata->parts,
-                       pdata->nr_parts);
-               if (err == 0) {
-                       info->nr_parts += pdata->nr_parts;
-                       goto out;
-               }
-       }
-
-       num_parsers = ARRAY_SIZE(parse_types);
-       if (num_parsers > MAX_PARSED_PARTS)
-               num_parsers = MAX_PARSED_PARTS;
-
-       parser[1] = NULL;
-       for (i = 0; i < num_parsers; i++) {
-               parser[0] = parse_types[i];
-
-               MAP_INFO(map, "parsing \"%s\" partitions\n",
-                       parser[0]);
-               nr_parts = parse_mtd_partitions(info->mtd, parser,
-                       &info->parts[i], 0);
-
-               if (nr_parts <= 0)
-                       continue;
-
-               MAP_INFO(map, "adding \"%s\" partitions\n",
-                       parser[0]);
-
-               err = add_mtd_partitions(info->mtd, info->parts[i], nr_parts);
-               if (err)
-                       break;
-
-               info->nr_parts += nr_parts;
-       }
-out:
-       return err;
-}
-#else
-static int adm5120_flash_initparts(struct adm5120_flash_info *info)
-{
-       return 0;
-}
-#endif /* CONFIG_MTD_PARTITIONS */
-
-#ifdef CONFIG_MTD_PARTITIONS
-static void adm5120_flash_remove_mtd(struct adm5120_flash_info *info)
-{
-       int i;
-
-       if (info->nr_parts) {
-               del_mtd_partitions(info->mtd);
-               for (i = 0; i < MAX_PARSED_PARTS; i++)
-                       if (info->parts[i] != NULL)
-                               kfree(info->parts[i]);
-       } else {
-               del_mtd_device(info->mtd);
-       }
-}
-#else
-static void adm5120_flash_remove_mtd(struct adm5120_flash_info *info)
-{
-       del_mtd_device(info->mtd);
-}
-#endif
-
 static int adm5120_flash_remove(struct platform_device *dev)
 {
        struct adm5120_flash_info *info;
@@ -419,7 +324,7 @@ static int adm5120_flash_remove(struct platform_device *dev)
        platform_set_drvdata(dev, NULL);
 
        if (info->mtd != NULL) {
-               adm5120_flash_remove_mtd(info);
+               mtd_device_unregister(info->mtd);
                map_destroy(info->mtd);
        }
 
@@ -436,11 +341,18 @@ static int adm5120_flash_remove(struct platform_device *dev)
 
 static int adm5120_flash_probe(struct platform_device *dev)
 {
+       struct adm5120_flash_platform_data *pdata;
        struct adm5120_flash_info *info;
        struct map_info *map;
        const char **probe_type;
        int err;
 
+       pdata = dev->dev.platform_data;
+       if (!pdata) {
+               dev_err(&dev->dev, "no platform data\n");
+               return -EINVAL;
+       }
+
        info = kzalloc(sizeof(*info), GFP_KERNEL);
        if (info == NULL) {
                err = -ENOMEM;
@@ -491,16 +403,11 @@ static int adm5120_flash_probe(struct platform_device *dev)
 
        info->mtd->owner = THIS_MODULE;
 
-       err = adm5120_flash_initparts(info);
+       err = mtd_device_parse_register(info->mtd, parse_types, 0,
+                                       pdata->parts, pdata->nr_parts);
        if (err)
                goto err_out;
 
-       if (info->nr_parts == 0) {
-               MAP_INFO(map, "no partitions available, registering "
-                       "whole flash\n");
-               add_mtd_device(info->mtd);
-       }
-
        return 0;
 
 err_out:
@@ -570,6 +477,6 @@ static void __exit adm5120_flash_exit(void)
 module_init(adm5120_flash_init);
 module_exit(adm5120_flash_exit);
 
-MODULE_LICENSE("GPL");
-MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
 MODULE_DESCRIPTION(DRV_DESC);