117ae5bc4SMatthias Ringwald /*
217ae5bc4SMatthias Ringwald * Copyright (C) 2017 BlueKitchen GmbH
317ae5bc4SMatthias Ringwald *
417ae5bc4SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
517ae5bc4SMatthias Ringwald * modification, are permitted provided that the following conditions
617ae5bc4SMatthias Ringwald * are met:
717ae5bc4SMatthias Ringwald *
817ae5bc4SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
917ae5bc4SMatthias Ringwald * notice, this list of conditions and the following disclaimer.
1017ae5bc4SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
1117ae5bc4SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
1217ae5bc4SMatthias Ringwald * documentation and/or other materials provided with the distribution.
1317ae5bc4SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
1417ae5bc4SMatthias Ringwald * contributors may be used to endorse or promote products derived
1517ae5bc4SMatthias Ringwald * from this software without specific prior written permission.
1617ae5bc4SMatthias Ringwald *
17*2fca4dadSMilanka Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
1817ae5bc4SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1917ae5bc4SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20*2fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
21*2fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2217ae5bc4SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2317ae5bc4SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2417ae5bc4SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2517ae5bc4SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2617ae5bc4SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
2717ae5bc4SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2817ae5bc4SMatthias Ringwald * SUCH DAMAGE.
2917ae5bc4SMatthias Ringwald *
3017ae5bc4SMatthias Ringwald */
3117ae5bc4SMatthias Ringwald
3217ae5bc4SMatthias Ringwald /*
3317ae5bc4SMatthias Ringwald * hal_flash_bank_mxc.c
3417ae5bc4SMatthias Ringwald *
3517ae5bc4SMatthias Ringwald * HAL abstraction for Flash memory that can be written anywhere
3617ae5bc4SMatthias Ringwald * after being erased implemented with memory
3717ae5bc4SMatthias Ringwald */
3817ae5bc4SMatthias Ringwald
3917ae5bc4SMatthias Ringwald #include <stdint.h>
4017ae5bc4SMatthias Ringwald #include <string.h> // memcpy
4117ae5bc4SMatthias Ringwald
4217ae5bc4SMatthias Ringwald #include "hal_flash_bank_mxc.h"
4317ae5bc4SMatthias Ringwald
4417ae5bc4SMatthias Ringwald #include "flc.h" // Maxim Flash Controller
4517ae5bc4SMatthias Ringwald
hal_flash_bank_mxc_get_size(void * context)4617ae5bc4SMatthias Ringwald static uint32_t hal_flash_bank_mxc_get_size(void * context){
4717ae5bc4SMatthias Ringwald hal_flash_bank_mxc_t * self = (hal_flash_bank_mxc_t *) context;
4817ae5bc4SMatthias Ringwald return self->sector_size;
4917ae5bc4SMatthias Ringwald }
5017ae5bc4SMatthias Ringwald
hal_flash_bank_mxc_get_alignment(void * context)5117ae5bc4SMatthias Ringwald static uint32_t hal_flash_bank_mxc_get_alignment(void * context){
5217ae5bc4SMatthias Ringwald (void)(context);
5317ae5bc4SMatthias Ringwald return 4;
5417ae5bc4SMatthias Ringwald }
5517ae5bc4SMatthias Ringwald
hal_flash_bank_mxc_erase(void * context,int bank)5617ae5bc4SMatthias Ringwald static void hal_flash_bank_mxc_erase(void * context, int bank){
5717ae5bc4SMatthias Ringwald hal_flash_bank_mxc_t * self = (hal_flash_bank_mxc_t *) context;
5817ae5bc4SMatthias Ringwald
5917ae5bc4SMatthias Ringwald if (bank > 1) return;
6017ae5bc4SMatthias Ringwald
6117ae5bc4SMatthias Ringwald // Erase page
6217ae5bc4SMatthias Ringwald FLC_PageErase(self->banks[bank], MXC_V_FLC_ERASE_CODE_PAGE_ERASE, MXC_V_FLC_FLSH_UNLOCK_KEY);
6317ae5bc4SMatthias Ringwald }
6417ae5bc4SMatthias Ringwald
hal_flash_bank_mxc_read(void * context,int bank,uint32_t offset,uint8_t * buffer,uint32_t size)6517ae5bc4SMatthias Ringwald static void hal_flash_bank_mxc_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
6617ae5bc4SMatthias Ringwald hal_flash_bank_mxc_t * self = (hal_flash_bank_mxc_t *) context;
6717ae5bc4SMatthias Ringwald
6817ae5bc4SMatthias Ringwald if (bank > 1) return;
6917ae5bc4SMatthias Ringwald if (offset > self->sector_size) return;
7017ae5bc4SMatthias Ringwald if ((offset + size) > self->sector_size) return;
7117ae5bc4SMatthias Ringwald
7217ae5bc4SMatthias Ringwald memcpy(buffer, ((uint8_t *) self->banks[bank]) + offset, size);
7317ae5bc4SMatthias Ringwald }
7417ae5bc4SMatthias Ringwald
hal_flash_bank_mxc_write(void * context,int bank,uint32_t offset,const uint8_t * data,uint32_t size)7517ae5bc4SMatthias Ringwald static void hal_flash_bank_mxc_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
7617ae5bc4SMatthias Ringwald hal_flash_bank_mxc_t * self = (hal_flash_bank_mxc_t *) context;
7717ae5bc4SMatthias Ringwald
7817ae5bc4SMatthias Ringwald if (bank > 1) return;
7917ae5bc4SMatthias Ringwald if (offset > self->sector_size) return;
8017ae5bc4SMatthias Ringwald if ((offset + size) > self->sector_size) return;
8117ae5bc4SMatthias Ringwald
8217ae5bc4SMatthias Ringwald FLC_Write(self->banks[bank] + offset, data, size, MXC_V_FLC_FLSH_UNLOCK_KEY);
8317ae5bc4SMatthias Ringwald }
8417ae5bc4SMatthias Ringwald
8517ae5bc4SMatthias Ringwald static const hal_flash_bank_t hal_flash_bank_mxc_impl = {
8617ae5bc4SMatthias Ringwald /* uint32_t (*get_size)() */ &hal_flash_bank_mxc_get_size,
8717ae5bc4SMatthias Ringwald /* uint32_t (*get_alignment)(..); */ &hal_flash_bank_mxc_get_alignment,
8817ae5bc4SMatthias Ringwald /* void (*erase)(..); */ &hal_flash_bank_mxc_erase,
8917ae5bc4SMatthias Ringwald /* void (*read)(..); */ &hal_flash_bank_mxc_read,
9017ae5bc4SMatthias Ringwald /* void (*write)(..); */ &hal_flash_bank_mxc_write,
9117ae5bc4SMatthias Ringwald };
9217ae5bc4SMatthias Ringwald
hal_flash_bank_mxc_init_instance(hal_flash_bank_mxc_t * context,uint32_t sector_size,uintptr_t bank_0_addr,uintptr_t bank_1_addr)9317ae5bc4SMatthias Ringwald const hal_flash_bank_t * hal_flash_bank_mxc_init_instance(hal_flash_bank_mxc_t * context, uint32_t sector_size,
9417ae5bc4SMatthias Ringwald uintptr_t bank_0_addr, uintptr_t bank_1_addr){
9517ae5bc4SMatthias Ringwald context->sector_size = sector_size;
9617ae5bc4SMatthias Ringwald context->banks[0] = bank_0_addr;
9717ae5bc4SMatthias Ringwald context->banks[1] = bank_1_addr;
9817ae5bc4SMatthias Ringwald
9917ae5bc4SMatthias Ringwald // prepare FLC
10017ae5bc4SMatthias Ringwald FLC_Init();
10117ae5bc4SMatthias Ringwald
10217ae5bc4SMatthias Ringwald return &hal_flash_bank_mxc_impl;
10317ae5bc4SMatthias Ringwald }
10417ae5bc4SMatthias Ringwald
105