mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/4] mtd: fix erasesize math for non power-of-2 devices
@ 2013-09-03  2:22 Darren Garnier
  2013-09-03 18:04 ` Robert Jarzmik
  2013-09-03 18:16 ` Sascha Hauer
  0 siblings, 2 replies; 4+ messages in thread
From: Darren Garnier @ 2013-09-03  2:22 UTC (permalink / raw)
  To: barebox

Signed-off-by: Darren Garnier <dgarnier@reinrag.net>
---
drivers/mtd/core.c | 19 ++++++++++++-------
fs/devfs-core.c    |  4 ++--
2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index 70036aa..ed9658b 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -25,6 +25,7 @@
#include <ioctl.h>
#include <nand.h>
#include <errno.h>
+#include <linux/math64.h>

#include "mtd.h"

@@ -82,9 +83,6 @@ static ssize_t mtd_op_read(struct cdev *cdev, void* buf, size_t count,
	return retlen;
}

-#define NOTALIGNED(x) (x & (mtd->writesize - 1)) != 0
-#define MTDPGALG(x) ((x) & ~(mtd->writesize - 1))
-
#ifdef CONFIG_MTD_WRITE
static ssize_t mtd_op_write(struct cdev* cdev, const void *buf, size_t _count,
			  loff_t _offset, ulong flags)
@@ -112,15 +110,22 @@ static struct mtd_erase_region_info *mtd_find_erase_region(struct mtd_info *mtd,
	return NULL;
}

+#define ALIGNED_OFFSET(offset, bs) ((bs & (bs-1)) == 0) ? \
+									offset & ~(bs-1) : \
+									bs * div_u64(offset,bs)
+#define ALIGNED_COUNT(count, bs) ((bs & (bs-1)) == 0) ? \
+									ALIGN(count,bs) : \
+									bs * div_u64(count+(bs-1),bs)
+
static int mtd_erase_align(struct mtd_info *mtd, size_t *count, loff_t *offset)
{
	struct mtd_erase_region_info *e;
	loff_t ofs;

	if (mtd->numeraseregions == 0) {
-		ofs = *offset & ~(mtd->erasesize - 1);
+		ofs = ALIGNED_OFFSET(*offset, mtd->erasesize);
		*count += (*offset - ofs);
-		*count = ALIGN(*count, mtd->erasesize);
+		*count = ALIGNED_COUNT(*count, mtd->erasesize);
		*offset = ofs;
		return 0;
	}
@@ -129,14 +134,14 @@ static int mtd_erase_align(struct mtd_info *mtd, size_t *count, loff_t *offset)
	if (!e)
		return -EINVAL;

-	ofs = *offset & ~(e->erasesize - 1);
+	ofs = ALIGNED_OFFSET(*offset, mtd->erasesize);
	*count += (*offset - ofs);

	e = mtd_find_erase_region(mtd, *offset + *count);
	if (!e)
		return -EINVAL;

-	*count = ALIGN(*count, e->erasesize);
+	*count = ALIGNED_COUNT(*count, e->erasesize);
	*offset = ofs;

	return 0;
diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index a92d434..757c9b7 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -24,6 +24,7 @@
#include <ioctl.h>
#include <linux/err.h>
#include <linux/mtd/mtd.h>
+#include <linux/math64.h>

LIST_HEAD(cdev_list);

@@ -197,11 +198,10 @@ static int partition_ioctl(struct cdev *cdev, int request, void *buf)
	case MEMGETREGIONINFO:
		if (cdev->mtd) {
			struct region_info_user *reg = buf;
-			int erasesize_shift = ffs(cdev->mtd->erasesize) - 1;

			reg->offset = cdev->offset;
			reg->erasesize = cdev->mtd->erasesize;
-			reg->numblocks = cdev->size >> erasesize_shift;
+			reg->numblocks = div_u64(cdev->size, cdev->mtd->erasesize);
			reg->regionindex = cdev->mtd->index;
		}
	break;
-- 
1.8.3.1



_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/4] mtd: fix erasesize math for non power-of-2 devices
  2013-09-03  2:22 [PATCH 1/4] mtd: fix erasesize math for non power-of-2 devices Darren Garnier
@ 2013-09-03 18:04 ` Robert Jarzmik
  2013-09-04  6:24   ` Sascha Hauer
  2013-09-03 18:16 ` Sascha Hauer
  1 sibling, 1 reply; 4+ messages in thread
From: Robert Jarzmik @ 2013-09-03 18:04 UTC (permalink / raw)
  To: Darren Garnier; +Cc: barebox

Darren Garnier <dgarnier@reinrag.net> writes:

> Signed-off-by: Darren Garnier <dgarnier@reinrag.net>

Hi Darren,

First of all, do any device exist where erasesize is non power-of-2, and if so
which one do you have in mind ?

Secondly, if such devices exist, I'd wish to have that explained in the commit
message, to know what motivated your patch.

> +#define ALIGNED_OFFSET(offset, bs) ((bs & (bs-1)) == 0) ? \
> +									offset & ~(bs-1) : \
> +									bs * div_u64(offset,bs)
> +#define ALIGNED_COUNT(count, bs) ((bs & (bs-1)) == 0) ? \
> +									ALIGN(count,bs) : \
> +									bs *
> div_u64(count+(bs-1),bs)
These lines look a bit over 80 caracters to me, or is it my editor ...
Moreover the (bs & (bs-1)) == 0 should be replaced by IS_ALIGNED(bs).

Cheers.

--
Robert

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/4] mtd: fix erasesize math for non power-of-2 devices
  2013-09-03  2:22 [PATCH 1/4] mtd: fix erasesize math for non power-of-2 devices Darren Garnier
  2013-09-03 18:04 ` Robert Jarzmik
@ 2013-09-03 18:16 ` Sascha Hauer
  1 sibling, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2013-09-03 18:16 UTC (permalink / raw)
  To: Darren Garnier; +Cc: barebox

Hi Darren,

On Mon, Sep 02, 2013 at 10:22:20PM -0400, Darren Garnier wrote:
> 
> +#define ALIGNED_OFFSET(offset, bs) ((bs & (bs-1)) == 0) ? \
> +									offset & ~(bs-1) : \
> +									bs * div_u64(offset,bs)
> +#define ALIGNED_COUNT(count, bs) ((bs & (bs-1)) == 0) ? \
> +									ALIGN(count,bs) : \
> +									bs * div_u64(count+(bs-1),bs)

Can you convert this into a function? It makes it easier and to read and
more obvious what's happening here. Please use the is_power_of_2 macro
and add whitespaces left and right to operators.

Otherwise looks good. I didn't know such flashes exist...

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/4] mtd: fix erasesize math for non power-of-2 devices
  2013-09-03 18:04 ` Robert Jarzmik
@ 2013-09-04  6:24   ` Sascha Hauer
  0 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2013-09-04  6:24 UTC (permalink / raw)
  To: Robert Jarzmik; +Cc: barebox

Hi Robert,

On Tue, Sep 03, 2013 at 08:04:56PM +0200, Robert Jarzmik wrote:
> Darren Garnier <dgarnier@reinrag.net> writes:
> 
> > Signed-off-by: Darren Garnier <dgarnier@reinrag.net>
> 
> Hi Darren,
> 
> First of all, do any device exist where erasesize is non power-of-2, and if so
> which one do you have in mind ?

Atmel DataFlashes seem to have non power-of-2 erasesizes, Darren
mentioned this is in the introduction message. But right, it should be
mentioned in the commit log.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2013-09-04  6:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-03  2:22 [PATCH 1/4] mtd: fix erasesize math for non power-of-2 devices Darren Garnier
2013-09-03 18:04 ` Robert Jarzmik
2013-09-04  6:24   ` Sascha Hauer
2013-09-03 18:16 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox