Skip to content

Commit 7c186e7

Browse files
committed
drivers/mtd: Remove bad block management in FTL for devices not needing it
Bad block management and the logical->physical mapping is only needed with raw NAND type memories, so we can compile that in conditionally, saving ~600 bytes of flash on a 32-bit arm when NAND is not used. Signed-off-by: Jukka Laitinen <jukka.laitinen@tii.ae>
1 parent 0916a88 commit 7c186e7

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

drivers/mtd/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ config FTL_READAHEAD
5050
default n
5151
depends on DRVR_READAHEAD
5252

53+
config FTL_BBM
54+
bool "Enable bad block management in the FTL layer"
55+
default y if MTD_NAND
56+
---help---
57+
Enable logical to physical erase-block mapping in the FTL layer for
58+
MTD devices that expose bad blocks via the MTD isbad/markbad methods.
59+
60+
This is needed for devices that require software bad block management.
61+
5362
config MTD_SECT512
5463
bool "512B sector conversion"
5564
default n
@@ -1442,6 +1451,7 @@ config MTD_GD5F
14421451
bool "SPI-based GD5F nand FLASH"
14431452
default n
14441453
select SPI
1454+
select FTL_BBM
14451455

14461456
if MTD_GD5F
14471457

drivers/mtd/ftl.c

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ struct ftl_struct_s
8080
FAR uint8_t *eblock; /* One, in-memory erase block */
8181
int oflags;
8282

83-
/* The nand block map between logic block and physical block */
83+
#ifdef CONFIG_FTL_BBM
84+
/* The block map between logic block and physical block */
8485

8586
FAR off_t *lptable;
8687
off_t lpcount;
88+
#endif
8789
};
8890

8991
/****************************************************************************
@@ -133,12 +135,14 @@ static const struct block_operations g_bops =
133135
* Private Functions
134136
****************************************************************************/
135137

138+
#ifdef CONFIG_FTL_BBM
139+
136140
/****************************************************************************
137141
* Name: ftl_init_map
138142
*
139143
* Description: Allocate logical block and physical block mapping table
140-
* space, and scan the entire nand flash device to establish
141-
* the mapping relationship between logical block and physical
144+
* space, and scan the entire MTD device to establish the
145+
* mapping relationship between logical block and physical
142146
* good block.
143147
*
144148
****************************************************************************/
@@ -207,6 +211,7 @@ static size_t ftl_get_cblock(FAR struct ftl_struct_s *dev, off_t start,
207211

208212
return count;
209213
}
214+
#endif
210215

211216
/****************************************************************************
212217
* Name: ftl_open
@@ -273,11 +278,15 @@ static int ftl_close(FAR struct inode *inode)
273278
static ssize_t ftl_mtd_bread(FAR struct ftl_struct_s *dev, off_t startblock,
274279
size_t nblocks, FAR uint8_t *buffer)
275280
{
281+
#ifdef CONFIG_FTL_BBM
276282
off_t mask = dev->blkper - 1;
277283
size_t nread = nblocks;
284+
#endif
278285
ssize_t ret = OK;
279286

287+
#ifdef CONFIG_FTL_BBM
280288
if (dev->lptable == NULL)
289+
#endif
281290
{
282291
ret = MTD_BREAD(dev->mtd, startblock, nblocks, buffer);
283292
if (ret != nblocks)
@@ -289,6 +298,7 @@ static ssize_t ftl_mtd_bread(FAR struct ftl_struct_s *dev, off_t startblock,
289298
return ret;
290299
}
291300

301+
#ifdef CONFIG_FTL_BBM
292302
while (nblocks > 0)
293303
{
294304
off_t startphysicalblock;
@@ -324,6 +334,7 @@ static ssize_t ftl_mtd_bread(FAR struct ftl_struct_s *dev, off_t startblock,
324334
}
325335

326336
return nblocks != nread ? nread - nblocks : ret;
337+
#endif
327338
}
328339

329340
/****************************************************************************
@@ -339,10 +350,14 @@ static ssize_t ftl_mtd_bread(FAR struct ftl_struct_s *dev, off_t startblock,
339350
static ssize_t ftl_mtd_bwrite(FAR struct ftl_struct_s *dev, off_t startblock,
340351
FAR const uint8_t *buffer)
341352
{
353+
#ifdef CONFIG_FTL_BBM
342354
off_t starteraseblock;
355+
#endif
343356
ssize_t ret;
344357

358+
#ifdef CONFIG_FTL_BBM
345359
if (dev->lptable == NULL)
360+
#endif
346361
{
347362
ret = MTD_BWRITE(dev->mtd, startblock, dev->blkper, buffer);
348363
if (ret != dev->blkper)
@@ -354,6 +369,7 @@ static ssize_t ftl_mtd_bwrite(FAR struct ftl_struct_s *dev, off_t startblock,
354369
return ret;
355370
}
356371

372+
#ifdef CONFIG_FTL_BBM
357373
starteraseblock = startblock / dev->blkper;
358374
while (1)
359375
{
@@ -372,6 +388,7 @@ static ssize_t ftl_mtd_bwrite(FAR struct ftl_struct_s *dev, off_t startblock,
372388
MTD_MARKBAD(dev->mtd, dev->lptable[starteraseblock]);
373389
ftl_update_map(dev, starteraseblock);
374390
}
391+
#endif
375392
}
376393

377394
/****************************************************************************
@@ -389,7 +406,9 @@ static ssize_t ftl_mtd_erase(FAR struct ftl_struct_s *dev, off_t startblock)
389406
{
390407
ssize_t ret;
391408

409+
#ifdef CONFIG_FTL_BBM
392410
if (dev->lptable == NULL)
411+
#endif
393412
{
394413
ret = MTD_ERASE(dev->mtd, startblock, 1);
395414
if (ret < 0 && ret != -ENOSYS)
@@ -402,6 +421,7 @@ static ssize_t ftl_mtd_erase(FAR struct ftl_struct_s *dev, off_t startblock)
402421
return OK;
403422
}
404423

424+
#ifdef CONFIG_FTL_BBM
405425
while (1)
406426
{
407427
if (startblock >= dev->lpcount)
@@ -418,6 +438,7 @@ static ssize_t ftl_mtd_erase(FAR struct ftl_struct_s *dev, off_t startblock)
418438
MTD_MARKBAD(dev->mtd, dev->lptable[startblock]);
419439
ftl_update_map(dev, startblock);
420440
}
441+
#endif
421442
}
422443

423444
/****************************************************************************
@@ -517,7 +538,9 @@ static ssize_t ftl_flush_direct(FAR struct ftl_struct_s *dev,
517538
}
518539
}
519540

541+
#ifdef CONFIG_FTL_BBM
520542
if (dev->lptable == NULL)
543+
#endif
521544
{
522545
ret = MTD_BWRITE(dev->mtd, startblock, count, buffer);
523546
if (ret != count)
@@ -527,6 +550,7 @@ static ssize_t ftl_flush_direct(FAR struct ftl_struct_s *dev,
527550
return ret;
528551
}
529552
}
553+
#ifdef CONFIG_FTL_BBM
530554
else
531555
{
532556
if (starteraseblock >= dev->lpcount)
@@ -544,6 +568,7 @@ static ssize_t ftl_flush_direct(FAR struct ftl_struct_s *dev,
544568
continue;
545569
}
546570
}
571+
#endif
547572

548573
nblocks -= count;
549574
startblock += count;
@@ -567,7 +592,11 @@ static ssize_t ftl_flush(FAR void *priv, FAR const uint8_t *buffer,
567592
int nbytes;
568593
int ret;
569594

595+
#ifdef CONFIG_FTL_BBM
570596
if (dev->mtd->erase == NULL && dev->lptable == NULL)
597+
#else
598+
if (dev->mtd->erase == NULL)
599+
#endif
571600
{
572601
ret = MTD_BWRITE(dev->mtd, startblock, nblocks, buffer);
573602
if (ret != nblocks)
@@ -978,6 +1007,7 @@ int ftl_initialize_by_path(FAR const char *path, FAR struct mtd_dev_s *mtd,
9781007
}
9791008
#endif
9801009

1010+
#ifdef CONFIG_FTL_BBM
9811011
if (MTD_ISBAD(dev->mtd, 0) != -ENOSYS)
9821012
{
9831013
ret = ftl_init_map(dev);
@@ -986,15 +1016,18 @@ int ftl_initialize_by_path(FAR const char *path, FAR struct mtd_dev_s *mtd,
9861016
goto out;
9871017
}
9881018
}
1019+
#endif
9891020

9901021
/* Inode private data is a reference to the FTL device structure */
9911022

9921023
ret = register_blockdriver(path, &g_bops, 0, dev);
9931024
if (ret < 0)
9941025
{
9951026
ferr("ERROR: register_blockdriver failed: %d\n", -ret);
1027+
#ifdef CONFIG_FTL_BBM
9961028
kmm_free(dev->lptable);
9971029
out:
1030+
#endif
9981031
#ifdef FTL_HAVE_RWBUFFER
9991032
rwb_uninitialize(&dev->rwb);
10001033
#endif

0 commit comments

Comments
 (0)