Skip to content
hn70ap_flash.c 5.18 KiB
Newer Older
/****************************************************************************
f4grx's avatar
f4grx committed
 * config/hn70ap/src/hn70ap_flash.c
 *
 *   Copyright (C) 2012, 2015-2016 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
f4grx's avatar
f4grx committed
 *   Copyright (C) 2018 Sebastien Lorquet. All rights reserved.
 *   Author: Sebastien Lorquet <sebastien@lorquet.fr>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <stdbool.h>
#include <stdio.h>
#include <syslog.h>
#include <errno.h>

#include <nuttx/board.h>
#include <nuttx/kmalloc.h>

f4grx's avatar
f4grx committed
#include <nuttx/mtd/mtd.h>
#include <nuttx/mtd/smart.h>
f4grx's avatar
f4grx committed
#include "driver_mtdchar.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/* Configuration ************************************************************/
f4grx's avatar
f4grx committed
#ifndef CONFIG_MTD_PARTITION
#error MTD partition support is required
#endif

/****************************************************************************
 * Public Functions
 ****************************************************************************/

f4grx's avatar
f4grx committed
struct mtd_partition_info_s
{
  const char *name;
  size_t      blocks;  //number of blocks (for sst26 this is 256 bytes)
};
f4grx's avatar
f4grx committed
/* Total space is 64 Mbit, 8 MB, 2048 sectors, 32768 blocks */
f4grx's avatar
f4grx committed
static const struct mtd_partition_info_s parts[] =
  {"firmware",  8192}, // 2MB
  {"storage" , 24576}  // 6MB
f4grx's avatar
f4grx committed
};
f4grx's avatar
f4grx committed
#define PARTCOUNT (sizeof(parts)/sizeof(parts[0]))

FAR struct mtd_dev_s *mtdparts[PARTCOUNT];
f4grx's avatar
f4grx committed
int hn70ap_flash_initialize(void)
f4grx's avatar
f4grx committed
{
  FAR struct spi_dev_s *spi2;
  FAR struct mtd_dev_s *mtd;
  struct mtd_geometry_s geo;
f4grx's avatar
f4grx committed
  int partno;
  int partoffset;
f4grx's avatar
f4grx committed
  spi2 = stm32_spibus_initialize(2);
  if (!spi2)
f4grx's avatar
f4grx committed
      _err("ERROR: Failed to initialize SPI port 2\n");
  /* Now bind the SPI interface to the SST25F064 SPI FLASH driver. */
f4grx's avatar
f4grx committed
  mtd = sst26_initialize_spi(spi2);
f4grx's avatar
f4grx committed
      _err("ERROR: Failed to bind SPI port 2 to the SPI FLASH driver\n");
f4grx's avatar
f4grx committed
      return -ENODEV;
f4grx's avatar
f4grx committed
  /* Get the geometry of the FLASH device */
f4grx's avatar
f4grx committed
  ret = mtd->ioctl(mtd, MTDIOC_GEOMETRY, (unsigned long)&geo);
f4grx's avatar
f4grx committed
  if (ret < 0)
    {
f4grx's avatar
f4grx committed
      _err("ERROR: mtd->ioctl failed: %d\n", ret);
f4grx's avatar
f4grx committed
      return ret;
    }
f4grx's avatar
f4grx committed
  syslog(
    LOG_INFO,
    "SST26: %d erase blocks each %d bytes (total %d kB), blocksize %d\n",
    geo.neraseblocks,
    geo.erasesize, 
    (geo.neraseblocks * geo.erasesize) >> 10,
    geo.blocksize
    );
f4grx's avatar
f4grx committed
  /* Now create partitions on the FLASH device */
f4grx's avatar
f4grx committed
  partoffset = 0;
  for(partno = 0; partno<PARTCOUNT; partno++)
    {
      mtdparts[partno] = mtd_partition(mtd, partoffset, parts[partno].blocks);
      partoffset  += parts[partno].blocks;
f4grx's avatar
f4grx committed
      if (mtdparts[partno] == NULL)
        {
f4grx's avatar
f4grx committed
          _err("ERROR: failed to create partition %s\n", parts[partno].name);
f4grx's avatar
f4grx committed
          continue;
        }
f4grx's avatar
f4grx committed
      /* Now initialize a SMART Flash block device and bind it
       * to the MTD device.
       */
f4grx's avatar
f4grx committed
      mtd_setpartitionname(mtdparts[partno], part[parno].name);
f4grx's avatar
f4grx committed
      syslog(LOG_INFO, "SST26: Created partition %s (%d blocks)\n", parts[partno].name, parts[partno].blocks);
f4grx's avatar
f4grx committed

f4grx's avatar
f4grx committed

  /* Create char device for firmware update partition */
  mtdchar_register(mtdparts[0], "/dev/firmware");
  _info("Registered /dev/firmware\n");

#if defined(CONFIG_MTD_SMART) && defined(CONFIG_FS_SMARTFS)
  smart_initialize(0, mtdparts[1], "p1");
#endif
f4grx's avatar
f4grx committed
  return OK;
}