1 /*
2  * Copyright (c) 2015 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #include <lk/err.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <lk/trace.h>
12 #include <lk/pow2.h>
13 #include <arch/ops.h>
14 #include <lib/bio.h>
15 #include <platform/stm32.h>
16 
17 #define LOCAL_TRACE 0
18 
19 /* device parameters */
20 #define MAX_GEOMETRY_COUNT 3
21 #define PROGRAM_SIZE 4
22 
23 #define _32K  (32 * 1024U)
24 #define _128K (128 * 1024U)
25 #define _256K (256 * 1024U)
26 
27 #define ERASE_RANGE0_START  (0)
28 #define ERASE_RANGE0_END    (_32K * 4)
29 #define ERASE_RANGE1_START  ERASE_RANGE0_END
30 #define ERASE_RANGE1_END    (ERASE_RANGE1_START + _128K)
31 #define ERASE_RANGE2_START  ERASE_RANGE1_END
32 #define ERASE_RANGE2_END    (flash.size)
33 
34 struct stm32_flash {
35     bdev_t bdev;
36     off_t size;
37 
38     bio_erase_geometry_info_t geometry[MAX_GEOMETRY_COUNT];
39 } flash;
40 
41 static ssize_t stm32_flash_bdev_read(struct bdev *, void *buf, off_t offset, size_t len);
42 static ssize_t stm32_flash_bdev_read_block(struct bdev *, void *buf, bnum_t block, uint count);
43 static ssize_t stm32_flash_bdev_write(struct bdev *bdev, const void *buf, off_t offset, size_t len);
44 static ssize_t stm32_flash_bdev_write_block(struct bdev *, const void *buf, bnum_t block, uint count);
45 static ssize_t stm32_flash_bdev_erase(struct bdev *, off_t offset, size_t len);
46 static int stm32_flash_ioctl(struct bdev *, int request, void *argp);
47 
stm32_flash_early_init(void)48 void stm32_flash_early_init(void) {
49     /* Enable FLASH clock  */
50     __HAL_RCC_ETH_CLK_ENABLE();
51 }
52 
stm32_flash_init(void)53 void stm32_flash_init(void) {
54     // XXX detect here
55     flash.size = 1024*1024;
56 
57     flash.geometry[0].start = ERASE_RANGE0_START;
58     flash.geometry[0].size = ERASE_RANGE0_END - ERASE_RANGE0_START;
59     flash.geometry[0].erase_size = _32K;
60     flash.geometry[0].erase_size = log2_uint(_32K);
61 
62     flash.geometry[1].start = ERASE_RANGE1_START;
63     flash.geometry[1].size = ERASE_RANGE1_END - ERASE_RANGE1_START;
64     flash.geometry[1].erase_size = _128K;
65     flash.geometry[1].erase_size = log2_uint(_128K);
66 
67     flash.geometry[2].start = ERASE_RANGE2_START;
68     flash.geometry[2].size = ERASE_RANGE2_END - ERASE_RANGE2_START;
69     flash.geometry[2].erase_size = _256K;
70     flash.geometry[2].erase_size = log2_uint(_256K);
71 
72     /* construct the block device */
73     bio_initialize_bdev(&flash.bdev, "flash0",
74                         PROGRAM_SIZE, flash.size / PROGRAM_SIZE,
75                         3, flash.geometry, BIO_FLAGS_NONE);
76 
77     /* we erase to 0xff */
78     flash.bdev.erase_byte = 0xff;
79 
80     /* override our block device hooks */
81     flash.bdev.read = &stm32_flash_bdev_read;
82     flash.bdev.read_block = &stm32_flash_bdev_read_block;
83     //flash.bdev.write = &stm32_flash_bdev_write;
84     flash.bdev.write_block = &stm32_flash_bdev_write_block;
85     flash.bdev.erase = &stm32_flash_bdev_erase;
86     flash.bdev.ioctl = &stm32_flash_ioctl;
87 
88     bio_register_device(&flash.bdev);
89 }
90 
stm32_flash_bdev_read(struct bdev * bdev,void * buf,off_t offset,size_t len)91 static ssize_t stm32_flash_bdev_read(struct bdev *bdev, void *buf, off_t offset, size_t len) {
92     LTRACEF("dev %p, buf %p, offset 0x%llx, len 0x%zx\n", bdev, buf, offset, len);
93 
94     memcpy(buf, (uint8_t *)FLASHAXI_BASE + offset, len);
95 
96     return len;
97 }
98 
stm32_flash_bdev_read_block(struct bdev * bdev,void * buf,bnum_t block,uint count)99 static ssize_t stm32_flash_bdev_read_block(struct bdev *bdev, void *buf, bnum_t block, uint count) {
100     LTRACEF("dev %p, buf %p, block 0x%x, count %u\n", bdev, buf, block, count);
101 
102     memcpy(buf, (uint8_t *)FLASHAXI_BASE + block * bdev->block_size, count * bdev->block_size);
103 
104     return count * bdev->block_size;
105 }
106 
stm32_flash_bdev_write_block(struct bdev * bdev,const void * buf,bnum_t block,uint count)107 static ssize_t stm32_flash_bdev_write_block(struct bdev *bdev, const void *buf, bnum_t block, uint count) {
108     LTRACEF("dev %p, buf %p, block 0x%x, count %u\n", bdev, buf, block, count);
109 
110     HAL_FLASH_Unlock();
111 
112     ssize_t written_bytes = count * bdev->block_size;
113     const uint32_t *buf32 = (const uint32_t *)buf;
114     while (count > 0) {
115         if (HAL_FLASH_Program(TYPEPROGRAM_WORD, FLASHAXI_BASE + block * bdev->block_size, *buf32) != HAL_OK) {
116             written_bytes = ERR_IO;
117             break;
118         }
119 
120         buf32++;
121         block++;
122         count--;
123     }
124 
125     HAL_FLASH_Lock();
126 
127     return written_bytes;
128 }
129 
offset_to_sector(off_t offset,uint32_t * sector,off_t * sector_offset,off_t * next_offset)130 static status_t offset_to_sector(off_t offset, uint32_t *sector, off_t *sector_offset, off_t *next_offset) {
131     if (offset < 0) {
132         return -1;
133     } else if (offset < ERASE_RANGE0_END) {
134         *sector = (offset - ERASE_RANGE0_START) / _32K;
135         *sector_offset = ROUNDDOWN(offset - ERASE_RANGE0_START, _32K) + ERASE_RANGE0_START;
136         *next_offset = *sector_offset + _32K;
137     } else if (offset < ERASE_RANGE1_END) {
138         *sector = (offset - ERASE_RANGE1_START) / _128K + 4;
139         *sector_offset = ROUNDDOWN(offset - ERASE_RANGE1_START, _128K) + ERASE_RANGE1_START;
140         *next_offset = *sector_offset + _128K;
141     } else if (offset < ERASE_RANGE2_END) {
142         *sector = (offset - ERASE_RANGE2_START) / _256K + 5;
143         *sector_offset = ROUNDDOWN(offset - ERASE_RANGE2_START, _256K) + ERASE_RANGE2_START;
144         *next_offset = *sector_offset + _256K;
145     } else {
146         return -1;
147     }
148 
149     DEBUG_ASSERT(*sector < FLASH_SECTOR_TOTAL);
150 
151     LTRACEF("offset 0x%llx, sector %u, sector_offset 0x%llx, next_offset 0x%llx\n", offset, *sector, *sector_offset, *next_offset);
152 
153     return NO_ERROR;
154 }
155 
stm32_flash_bdev_erase(struct bdev * bdev,off_t offset,size_t len)156 static ssize_t stm32_flash_bdev_erase(struct bdev *bdev, off_t offset, size_t len) {
157     LTRACEF("dev %p, offset 0x%llx, len 0x%zx\n", bdev, offset, len);
158 
159     ssize_t total_erased = 0;
160 
161     HAL_FLASH_Unlock();
162 
163     while (len > 0) {
164         uint32_t sector = 0;
165         off_t sector_offset = 0;
166         off_t next_offset = 0;
167 
168         if (offset_to_sector(offset, &sector, &sector_offset, &next_offset) < 0)
169             return ERR_INVALID_ARGS;
170 
171         FLASH_EraseInitTypeDef erase;
172         erase.TypeErase = FLASH_TYPEERASE_SECTORS;
173         erase.Sector = sector;
174         erase.NbSectors = 1;
175         erase.VoltageRange = FLASH_VOLTAGE_RANGE_3; // XXX
176 
177         LTRACEF("erase params: sector %u, num_sectors %u, next_offset 0x%llx\n", erase.Sector, erase.NbSectors, next_offset);
178 
179         if (1) {
180             uint32_t sector_error;
181             HAL_StatusTypeDef err = HAL_FLASHEx_Erase(&erase, &sector_error);
182             if (err != HAL_OK) {
183                 TRACEF("error starting erase operation, sector error %u\n", sector_error);
184                 total_erased = ERR_IO;
185                 break;
186             }
187 
188             err = FLASH_WaitForLastOperation(HAL_MAX_DELAY);
189             if (err != HAL_OK) {
190                 TRACEF("error waiting for erase operation to end, hal error %u\n", HAL_FLASH_GetError());
191                 total_erased = ERR_IO;
192                 break;
193             }
194 
195             // invalidate the cache on this region
196             arch_invalidate_cache_range(FLASHAXI_BASE + sector_offset, next_offset - sector_offset);
197         }
198 
199         // move to the next erase boundary
200         total_erased += next_offset - sector_offset;
201         off_t erased_bytes = next_offset - offset;
202         if (erased_bytes >= len)
203             break;
204         len -= erased_bytes;
205         offset = next_offset;
206     }
207 
208     HAL_FLASH_Lock();
209 
210     return total_erased;
211 }
212 
stm32_flash_ioctl(struct bdev * bdev,int request,void * argp)213 static int stm32_flash_ioctl(struct bdev *bdev, int request, void *argp) {
214     LTRACEF("dev %p, request %d, argp %p\n", bdev, request, argp);
215 
216     int ret = ERR_NOT_SUPPORTED;
217     switch (request) {
218         case BIO_IOCTL_GET_MAP_ADDR:
219         case BIO_IOCTL_GET_MEM_MAP:
220             /* we're already mapped */
221             if (argp)
222                 *(void **)argp = (void *)FLASHAXI_BASE;
223             break;
224         case BIO_IOCTL_PUT_MEM_MAP:
225             break;
226     }
227 
228     return ret;
229 }
230 
231