David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #include <zephyr.h> |
| 21 | #include <misc/printk.h> |
| 22 | #include <flash.h> |
| 23 | |
| 24 | #include <flash_map/flash_map.h> |
| 25 | #include <hal/hal_flash.h> |
| 26 | #include <sysflash/sysflash.h> |
| 27 | |
| 28 | extern struct device *boot_flash_device; |
| 29 | |
| 30 | /* |
| 31 | * The flash area describes essentially the partition table of the |
| 32 | * flash. In this case, it starts with FLASH_AREA_IMAGE_0. |
| 33 | */ |
| 34 | static const struct flash_area part_map[] = { |
| 35 | { |
| 36 | .fa_id = FLASH_AREA_IMAGE_0, |
| 37 | .fa_off = 0x20000, |
| 38 | .fa_size = 0x20000, |
| 39 | }, |
| 40 | { |
| 41 | .fa_id = FLASH_AREA_IMAGE_1, |
| 42 | .fa_off = 0x40000, |
| 43 | .fa_size = 0x20000, |
| 44 | }, |
| 45 | { |
| 46 | .fa_id = FLASH_AREA_IMAGE_SCRATCH, |
| 47 | .fa_off = 0x60000, |
| 48 | .fa_size = 0x20000, |
| 49 | }, |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * The K64F has a simple 1MB of uniform 4KB sectors. Initially, we'll |
| 54 | * use the same partition layout as the Carbon board to make |
| 55 | * development easier. |
| 56 | */ |
| 57 | |
| 58 | /* |
| 59 | * `open` a flash area. The `area` in this case is not the individual |
| 60 | * sectors, but describes the particular flash area in question. |
| 61 | */ |
| 62 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 63 | { |
| 64 | int i; |
| 65 | printk("%s: area %d\n", __func__, id); |
| 66 | |
| 67 | for (i = 0; i < ARRAY_SIZE(part_map); i++) { |
| 68 | if (id == part_map[i].fa_id) |
| 69 | break; |
| 70 | } |
| 71 | if (i == ARRAY_SIZE(part_map)) |
| 72 | return -1; |
| 73 | |
| 74 | *area = &part_map[i]; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Nothing to do on close. |
| 80 | */ |
| 81 | void flash_area_close(const struct flash_area *area) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, |
| 86 | uint32_t len) |
| 87 | { |
| 88 | // printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len); |
| 89 | return flash_read(boot_flash_device, area->fa_off + off, dst, len); |
| 90 | } |
| 91 | |
| 92 | int flash_area_write(const struct flash_area *area, uint32_t off, const void *src, |
| 93 | uint32_t len) |
| 94 | { |
| 95 | printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len); |
| 96 | return flash_write(boot_flash_device, area->fa_off + off, src, len); |
| 97 | } |
| 98 | |
| 99 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 100 | { |
| 101 | printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len); |
| 102 | return flash_erase(boot_flash_device, area->fa_off + off, len); |
| 103 | } |
| 104 | |
| 105 | uint8_t flash_area_align(const struct flash_area *area) |
| 106 | { |
| 107 | return hal_flash_align(area->fa_id); |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * This depends on the mappings defined in sysflash.h, and assumes |
| 112 | * that slot 0, slot 1, and the scratch area area contiguous. |
| 113 | */ |
| 114 | int flash_area_id_from_image_slot(int slot) |
| 115 | { |
| 116 | return slot + FLASH_AREA_IMAGE_0; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Lookup the sector map for a given flash area. This should fill in |
| 121 | * `ret` with all of the sectors in the area. `*cnt` will be set to |
| 122 | * the storage at `ret` and should be set to the final number of |
| 123 | * sectors in this area. |
| 124 | */ |
| 125 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 126 | { |
| 127 | uint32_t off; |
| 128 | |
| 129 | printk("%s: lookup area %d\n", __func__, idx); |
| 130 | /* |
| 131 | * This simple layout has uniform slots, so just fill in the |
| 132 | * right one. |
| 133 | */ |
| 134 | if (idx < FLASH_AREA_IMAGE_0 || idx > FLASH_AREA_IMAGE_SCRATCH) |
| 135 | return -1; |
| 136 | |
| 137 | if (*cnt < 1) |
| 138 | return -1; |
| 139 | |
| 140 | off = (idx - FLASH_AREA_IMAGE_0 + 1) * 0x20000; |
| 141 | |
| 142 | ret->fa_id = idx; |
| 143 | ret->fa_device_id = 0; |
| 144 | ret->pad16 = 0; |
| 145 | ret->fa_off = off; |
| 146 | ret->fa_size = 0x20000; |
| 147 | |
| 148 | return 0; |
| 149 | } |