Title: [6561] trunk/drivers/mtd/maps/gpio-addr-flash.c: add kernel docs, optimize a bit, and touchup style based on lkml feedback
Revision
6561
Author
vapier
Date
2009-06-03 07:17:58 -0500 (Wed, 03 Jun 2009)

Log Message

add kernel docs, optimize a bit, and touchup style based on lkml feedback

Modified Paths

Diff

Modified: trunk/drivers/mtd/maps/gpio-addr-flash.c (6560 => 6561)


--- trunk/drivers/mtd/maps/gpio-addr-flash.c	2009-06-03 11:16:48 UTC (rev 6560)
+++ trunk/drivers/mtd/maps/gpio-addr-flash.c	2009-06-03 12:17:58 UTC (rev 6561)
@@ -6,7 +6,7 @@
  * to a 2meg memory range and use the GPIOs to select a particular range.
  *
  * Copyright 2000 Nicolas Pitre <[email protected]>
- * Copyright 2005-2008 Analog Devices Inc.
+ * Copyright 2005-2009 Analog Devices Inc.
  *
  * Enter bugs at http://blackfin.uclinux.org/
  *
@@ -29,7 +29,17 @@
 #define pr_devinit(fmt, args...) ({ static const __devinitconst char __fmt[] = fmt; printk(__fmt, ## args); })
 
 #define DRIVER_NAME "gpio-addr-flash"
+#define PFX DRIVER_NAME ": "
 
+/**
+ * struct async_state - keep GPIO flash state
+ *	@mtd:         MTD state for this mapping
+ *	@map:         MTD map state for this flash
+ *	@gpio_count:  number of GPIOs used to address
+ *	@gpio_addrs:  array of GPIOs to twiddle
+ *	@gpio_values: cached GPIO values
+ *	@win_size:    dedicated memory size (if no GPIOs)
+ */
 struct async_state {
 	struct mtd_info *mtd;
 	struct map_info map;
@@ -38,23 +48,40 @@
 	int *gpio_values;
 	unsigned long win_size;
 };
+#define gf_map_info_to_state(mi) ((struct async_state *)(mi)->map_priv_1)
 
+/**
+ * gf_set_gpios() - set GPIO address lines to access specified flash offset
+ *	@state: GPIO flash state
+ *	@ofs:   desired offset to access
+ *
+ * Rather than call the GPIO framework every time, cache the last-programmed
+ * value.  This speeds up sequential accesses (which are by far the most common
+ * type).  We rely on the GPIO framework to treat non-zero value as high so
+ * that we don't have to normalize the bits.
+ */
 static void gf_set_gpios(struct async_state *state, unsigned long ofs)
 {
-	size_t i;
+	size_t i = 0;
 	int value;
-	for (i = 0; i < state->gpio_count; ++i) {
-		value = !!((ofs / state->win_size) & (1 << i));
+	ofs /= state->win_size;
+	do {
+		value = ofs & (1 << i);
 		if (state->gpio_values[i] != value) {
 			gpio_set_value(state->gpio_addrs[i], value);
 			state->gpio_values[i] = value;
 		}
-	}
+	} while (++i < state->gpio_count);
 }
 
+/**
+ * gf_read() - read a word at the specified offset
+ *	@map: MTD map state
+ *	@ofs: desired offset to read
+ */
 static map_word gf_read(struct map_info *map, unsigned long ofs)
 {
-	struct async_state *state = (struct async_state *)map->map_priv_1;
+	struct async_state *state = gf_map_info_to_state(map);
 	u16 word;
 	map_word test;
 
@@ -65,22 +92,39 @@
 	return test;
 }
 
+/**
+ * gf_copy_from() - copy a chunk of data from the flash
+ *	@map:  MTD map state
+ *	@to:   memory to copy to
+ *	@from: flash offset to copy from
+ *	@len:  how much to copy
+ *
+ * We rely on the MTD layer to chunk up copies such that a single request here
+ * will not cross a window size.  This allows us to only wiggle the GPIOs once
+ * before falling back to a normal memcpy.  Reading the higher layer code shows
+ * that this is indeed the case, but add a BUG_ON() to future proof.
+ */
 static void gf_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
 {
-	struct async_state *state = (struct async_state *)map->map_priv_1;
+	struct async_state *state = gf_map_info_to_state(map);
 
 	gf_set_gpios(state, from);
 
-	/* BUG if operation crosss the win_size */
+	/* BUG if operation crosses the win_size */
 	BUG_ON(!((from + len) % state->win_size <= (from + len)));
 
 	/* operation does not cross the win_size, so one shot it */
 	memcpy_fromio(to, map->virt + (from % state->win_size), len);
 }
 
+/**
+ * gf_write() - write a word at the specified offset
+ *	@map: MTD map state
+ *	@ofs: desired offset to write
+ */
 static void gf_write(struct map_info *map, map_word d1, unsigned long ofs)
 {
-	struct async_state *state = (struct async_state *)map->map_priv_1;
+	struct async_state *state = gf_map_info_to_state(map);
 	u16 d;
 
 	gf_set_gpios(state, ofs);
@@ -89,13 +133,22 @@
 	writew(d, map->virt + (ofs % state->win_size));
 }
 
+/**
+ * gf_copy_to() - copy a chunk of data to the flash
+ *	@map:  MTD map state
+ *	@to:   flash offset to copy to
+ *	@from: memory to copy from
+ *	@len:  how much to copy
+ *
+ * See gf_copy_from() caveat.
+ */
 static void gf_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
 {
-	struct async_state *state = (struct async_state *)map->map_priv_1;
+	struct async_state *state = gf_map_info_to_state(map);
 
 	gf_set_gpios(state, to);
 
-	/* BUG if operation crosss the win_size */
+	/* BUG if operation crosses the win_size */
 	BUG_ON(!((to + len) % state->win_size <= (to + len)));
 
 	/* operation does not cross the win_size, so one shot it */
@@ -106,26 +159,60 @@
 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
 #endif
 
+/**
+ * gpio_flash_probe() - setup a mapping for a GPIO assisted flash
+ *	@pdev: platform device
+ *
+ * The platform resource layout expected looks something like:
+ * struct mtd_partition partitions[] = { ... };
+ * struct physmap_flash_data flash_data = { ... };
+ * unsigned flash_gpios[] = { GPIO_XX, GPIO_XX, ... };
+ * struct resource flash_resource[] = {
+ *	{
+ *		.name  = "cfi_probe",
+ *		.start = 0x20000000,
+ *		.end   = 0x201fffff,
+ *		.flags = IORESOURCE_MEM,
+ *	}, {
+ *		.start = (unsigned long)flash_gpios,
+ *		.end   = ARRAY_SIZE(flash_gpios),
+ *		.flags = IORESOURCE_IRQ,
+ *	}
+ * };
+ * struct platform_device flash_device = {
+ *	.name          = "gpio-addr-flash",
+ *	.dev           = { .platform_data = &flash_data, },
+ *	.num_resources = ARRAY_SIZE(flash_resource),
+ *	.resource      = flash_resource,
+ *	...
+ * };
+ */
 static int __devinit gpio_flash_probe(struct platform_device *pdev)
 {
 	int ret;
-	size_t i;
-	struct physmap_flash_data *pdata = pdev->dev.platform_data;
-	struct resource *memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	struct resource *gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+	size_t i, arr_size;
+	struct physmap_flash_data *pdata;
+	struct resource *memory;
+	struct resource *gpios;
 	struct async_state *state;
 
-	state = kzalloc(sizeof(*state) + (sizeof(int) * gpios->end * 2), GFP_KERNEL);
+	pdata = pdev->dev.platform_data;
+	memory = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	gpios = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
+
+	if (!memory || !gpios || !gpios->end)
+		return -EINVAL;
+
+	arr_size = sizeof(int) * gpios->end;
+	state = kzalloc(sizeof(*state) + arr_size, GFP_KERNEL);
 	if (!state)
 		return -ENOMEM;
 
 	state->gpio_count     = gpios->end;
-	state->gpio_addrs     = (void *)(state + 1);
-	state->gpio_values    = state->gpio_addrs + state->gpio_count;
+	state->gpio_addrs     = (void *)gpios->start;
+	state->gpio_values    = (void *)(state + 1);
 	state->win_size       = memory->end - memory->start + 1;
-	memcpy(state->gpio_addrs, (void *)gpios->start, sizeof(unsigned) * state->gpio_count);
-	for (i = 0; i < state->gpio_count; ++i)
-		state->gpio_values[i] = -1;
+	memset(state->gpio_values, 0xff, arr_size);
 
 	state->map.name       = DRIVER_NAME;
 	state->map.read       = gf_read;
@@ -140,18 +227,21 @@
 
 	platform_set_drvdata(pdev, state);
 
-	for (i = 0; i < state->gpio_count; ++i) {
+	i = 0;
+	do {
 		if (gpio_request(state->gpio_addrs[i], DRIVER_NAME)) {
-			pr_devinit(KERN_ERR DRIVER_NAME ": Failed to request gpio %d\n", state->gpio_addrs[i]);
+			pr_devinit(KERN_ERR PFX "failed to request gpio %d\n",
+				state->gpio_addrs[i]);
 			while (i--)
 				gpio_free(state->gpio_addrs[i]);
 			kfree(state);
 			return -EBUSY;
 		}
 		gpio_direction_output(state->gpio_addrs[i], 0);
-	}
+	} while (++i < state->gpio_count);
 
-	pr_devinit(KERN_NOTICE DRIVER_NAME ": probing %d-bit flash bus\n", state->map.bankwidth * 8);
+	pr_devinit(KERN_NOTICE PFX "probing %d-bit flash bus\n",
+		state->map.bankwidth * 8);
 	state->mtd = do_map_probe(memory->name, &state->map);
 	if (!state->mtd) {
 		for (i = 0; i < state->gpio_count; ++i)
@@ -163,18 +253,18 @@
 #ifdef CONFIG_MTD_PARTITIONS
 	ret = parse_mtd_partitions(state->mtd, part_probe_types, &pdata->parts, 0);
 	if (ret > 0) {
-		pr_devinit(KERN_NOTICE DRIVER_NAME ": Using commandline partition definition\n");
+		pr_devinit(KERN_NOTICE PFX "Using commandline partition definition\n");
 		add_mtd_partitions(state->mtd, pdata->parts, ret);
 		kfree(pdata->parts);
 
 	} else if (pdata->nr_parts) {
-		pr_devinit(KERN_NOTICE DRIVER_NAME ": Using board partition definition\n");
+		pr_devinit(KERN_NOTICE PFX "Using board partition definition\n");
 		add_mtd_partitions(state->mtd, pdata->parts, pdata->nr_parts);
 
 	} else
 #endif
 	{
-		pr_devinit(KERN_NOTICE DRIVER_NAME ": no partition info available, registering whole flash at once\n");
+		pr_devinit(KERN_NOTICE PFX "no partition info available, registering whole flash at once\n");
 		add_mtd_device(state->mtd);
 	}
 
@@ -184,9 +274,10 @@
 static int __devexit gpio_flash_remove(struct platform_device *pdev)
 {
 	struct async_state *state = platform_get_drvdata(pdev);
-	size_t i;
-	for (i = 0; i < state->gpio_count; ++i)
+	size_t i = 0;
+	do {
 		gpio_free(state->gpio_addrs[i]);
+	} while (++i < state->gpio_count);
 #ifdef CONFIG_MTD_PARTITIONS
 	del_mtd_partitions(state->mtd);
 #endif
_______________________________________________
Linux-kernel-commits mailing list
[email protected]
https://blackfin.uclinux.org/mailman/listinfo/linux-kernel-commits

Reply via email to