1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup lpc32xx_nand_mlc |
---|
7 | * |
---|
8 | * @brief lpc32xx_mlc_write_blocks() implementation. |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * Copyright (C) 2010, 2011 embedded brains GmbH & Co. KG |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
33 | * POSSIBILITY OF SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #include <bsp/nand-mlc.h> |
---|
37 | |
---|
38 | #include <string.h> |
---|
39 | |
---|
40 | static const uint32_t ones_spare [MLC_LARGE_SPARE_WORD_COUNT] = { |
---|
41 | 0xffffffff, |
---|
42 | 0xffffffff, |
---|
43 | 0xffffffff, |
---|
44 | 0xffffffff, |
---|
45 | 0xffffffff, |
---|
46 | 0xffffffff, |
---|
47 | 0xffffffff, |
---|
48 | 0xffffffff, |
---|
49 | 0xffffffff, |
---|
50 | 0xffffffff, |
---|
51 | 0xffffffff, |
---|
52 | 0xffffffff, |
---|
53 | 0xffffffff, |
---|
54 | 0xffffffff, |
---|
55 | 0xffffffff, |
---|
56 | 0xffffffff |
---|
57 | }; |
---|
58 | |
---|
59 | rtems_status_code lpc32xx_mlc_write_blocks( |
---|
60 | uint32_t block_begin, |
---|
61 | uint32_t block_end, |
---|
62 | const void *src, |
---|
63 | size_t src_size, |
---|
64 | uint32_t *page_data_buffer |
---|
65 | ) |
---|
66 | { |
---|
67 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
68 | uint32_t pages_per_block = lpc32xx_mlc_pages_per_block(); |
---|
69 | uint32_t block_count = lpc32xx_mlc_block_count(); |
---|
70 | uint32_t page_size = lpc32xx_mlc_page_size(); |
---|
71 | uint32_t block = 0; |
---|
72 | const uint8_t *current = src; |
---|
73 | const uint8_t *last = current; |
---|
74 | const uint8_t *end = current + src_size; |
---|
75 | |
---|
76 | if (block_begin > block_end || block_end > block_count) { |
---|
77 | return RTEMS_INVALID_ID; |
---|
78 | } |
---|
79 | |
---|
80 | for (block = block_begin; block != block_end; ++block) { |
---|
81 | uint32_t page_begin = block * pages_per_block; |
---|
82 | uint32_t page_end = page_begin + pages_per_block; |
---|
83 | uint32_t page = 0; |
---|
84 | |
---|
85 | sc = lpc32xx_mlc_erase_block_safe_3(block, page_begin, page_end); |
---|
86 | if (sc != RTEMS_SUCCESSFUL) { |
---|
87 | continue; |
---|
88 | } |
---|
89 | |
---|
90 | for (page = page_begin; page < page_end; ++page) { |
---|
91 | uintptr_t remainder = (uintptr_t) end - (uintptr_t) current; |
---|
92 | size_t delta = remainder < page_size ? remainder : page_size; |
---|
93 | |
---|
94 | if (remainder > 0) { |
---|
95 | memcpy(page_data_buffer, current, delta); |
---|
96 | sc = lpc32xx_mlc_write_page_with_ecc( |
---|
97 | page, |
---|
98 | page_data_buffer, |
---|
99 | ones_spare |
---|
100 | ); |
---|
101 | if (sc != RTEMS_SUCCESSFUL) { |
---|
102 | lpc32xx_mlc_erase_block(block); |
---|
103 | lpc32xx_mlc_zero_pages(page_begin, page_end); |
---|
104 | current = last; |
---|
105 | continue; |
---|
106 | } |
---|
107 | |
---|
108 | current += delta; |
---|
109 | } else { |
---|
110 | goto done; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | last = current; |
---|
115 | } |
---|
116 | |
---|
117 | done: |
---|
118 | |
---|
119 | if (current != end) { |
---|
120 | return RTEMS_IO_ERROR; |
---|
121 | } |
---|
122 | |
---|
123 | return RTEMS_SUCCESSFUL; |
---|
124 | } |
---|