2016-10-21 16:58:53 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Linaro Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-12-18 01:56:56 +08:00
|
|
|
#include <logging/sys_log.h>
|
2016-12-05 04:59:37 +08:00
|
|
|
#include <kernel.h>
|
2016-10-21 16:58:53 +08:00
|
|
|
#include <device.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <flash.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <soc.h>
|
|
|
|
|
|
|
|
#include "fsl_common.h"
|
|
|
|
#include "fsl_flash.h"
|
|
|
|
|
|
|
|
struct flash_priv {
|
|
|
|
flash_config_t config;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interrupt vectors could be executed from flash hence the need for locking.
|
2017-01-03 04:31:32 +08:00
|
|
|
* The underlying MCUX driver takes care of copying the functions to SRAM.
|
2016-10-21 16:58:53 +08:00
|
|
|
*
|
|
|
|
* For more information, see the application note below on Read-While-Write
|
|
|
|
* http://cache.freescale.com/files/32bit/doc/app_note/AN4695.pdf
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2017-01-03 04:31:32 +08:00
|
|
|
static int flash_mcux_erase(struct device *dev, off_t offset, size_t len)
|
2016-10-21 16:58:53 +08:00
|
|
|
{
|
|
|
|
struct flash_priv *priv = dev->driver_data;
|
|
|
|
uint32_t addr;
|
|
|
|
status_t rc;
|
|
|
|
int key;
|
|
|
|
|
|
|
|
addr = offset + priv->config.PFlashBlockBase;
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
rc = FLASH_Erase(&priv->config, addr, len, kFLASH_apiEraseKey);
|
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
return (rc == kStatus_Success) ? 0 : -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-01-03 04:31:32 +08:00
|
|
|
static int flash_mcux_read(struct device *dev, off_t offset,
|
2016-10-21 16:58:53 +08:00
|
|
|
void *data, size_t len)
|
|
|
|
{
|
|
|
|
struct flash_priv *priv = dev->driver_data;
|
|
|
|
uint32_t addr;
|
|
|
|
|
|
|
|
/*
|
2017-01-03 04:31:32 +08:00
|
|
|
* The MCUX supports different flash chips whose valid ranges are
|
2016-10-21 16:58:53 +08:00
|
|
|
* hidden below the API: until the API export these ranges, we can not
|
|
|
|
* do any generic validation
|
|
|
|
*/
|
|
|
|
addr = offset + priv->config.PFlashBlockBase;
|
|
|
|
|
|
|
|
memcpy(data, (void *) addr, len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-03 04:31:32 +08:00
|
|
|
static int flash_mcux_write(struct device *dev, off_t offset,
|
2016-10-21 16:58:53 +08:00
|
|
|
const void *data, size_t len)
|
|
|
|
{
|
|
|
|
struct flash_priv *priv = dev->driver_data;
|
|
|
|
uint32_t addr;
|
|
|
|
status_t rc;
|
|
|
|
int key;
|
|
|
|
|
|
|
|
addr = offset + priv->config.PFlashBlockBase;
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
rc = FLASH_Program(&priv->config, addr, (uint32_t *) data, len);
|
|
|
|
irq_unlock(key);
|
|
|
|
|
|
|
|
return (rc == kStatus_Success) ? 0 : -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-01-03 04:31:32 +08:00
|
|
|
static int flash_mcux_write_protection(struct device *dev, bool enable)
|
2016-10-21 16:58:53 +08:00
|
|
|
{
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct flash_priv flash_data;
|
|
|
|
|
2017-01-03 04:31:32 +08:00
|
|
|
static const struct flash_driver_api flash_mcux_api = {
|
|
|
|
.write_protection = flash_mcux_write_protection,
|
|
|
|
.erase = flash_mcux_erase,
|
|
|
|
.write = flash_mcux_write,
|
|
|
|
.read = flash_mcux_read,
|
2016-10-21 16:58:53 +08:00
|
|
|
};
|
|
|
|
|
2017-01-03 04:31:32 +08:00
|
|
|
static int flash_mcux_init(struct device *dev)
|
2016-10-21 16:58:53 +08:00
|
|
|
{
|
|
|
|
struct flash_priv *priv = dev->driver_data;
|
|
|
|
status_t rc;
|
|
|
|
|
|
|
|
rc = FLASH_Init(&priv->config);
|
|
|
|
|
|
|
|
return (rc == kStatus_Success) ? 0 : -EIO;
|
|
|
|
}
|
|
|
|
|
2017-01-03 04:31:32 +08:00
|
|
|
DEVICE_AND_API_INIT(flash_mcux, CONFIG_SOC_FLASH_MCUX_DEV_NAME,
|
|
|
|
flash_mcux_init, &flash_data, NULL, POST_KERNEL,
|
|
|
|
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &flash_mcux_api);
|
2016-10-21 16:58:53 +08:00
|
|
|
|