summaryrefslogtreecommitdiffstats
path: root/bsps/aarch64/xilinx-zynqmp/jffs2_xnandpsu.c
blob: 3fa70cc0c317c1471f3c4b37768675ea81fdfe5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (C) 2023 On-Line Applications Research Corporation (OAR)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This file contains an implementation of a basic JFFS2 filesystem adapter for
 * the NandPsu peripheral that uses the entirety of the available NAND chip(s)
 * for a JFFS2 filesystem or up to the maximum size possible. If an
 * implementation would prefer to only use a portion of the NAND flash chip,
 * this template would need rework to account for a reduced size and possibly a
 * start offset while also taking into account the driver's handling of bad
 * blocks and how that might affect the offset.
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>

#include <bsp/jffs2_xnandpsu.h>
#include <rtems/libio.h>
#include <rtems/libcsupport.h>
#include <rtems/malloc.h>
#include <rtems/thread.h>
#include <dev/nand/xnandpsu_bbm.h>

typedef struct {
  rtems_jffs2_flash_control super;
  XNandPsu *nandpsu;
  rtems_mutex access_lock;
} flash_control;

static flash_control *get_flash_control(rtems_jffs2_flash_control *super)
{
  return (flash_control *) super;
}

static int flash_read(
  rtems_jffs2_flash_control *super,
  uint32_t offset,
  unsigned char *buffer,
  size_t size_of_buffer
)
{
  XNandPsu *nandpsu = get_flash_control(super)->nandpsu;
  rtems_status_code sc;

  rtems_mutex_lock(&(get_flash_control(super)->access_lock));
  sc = XNandPsu_Read(nandpsu, offset, size_of_buffer, buffer);
  rtems_mutex_unlock(&(get_flash_control(super)->access_lock));
  if (sc) {
    return -EIO;
  }
  return 0;
}

static int flash_write(
  rtems_jffs2_flash_control *super,
  uint32_t offset,
  const unsigned char *buffer,
  size_t size_of_buffer
)
{
  XNandPsu *nandpsu = get_flash_control(super)->nandpsu;
  rtems_status_code sc;

  rtems_mutex_lock(&(get_flash_control(super)->access_lock));
  sc = XNandPsu_Write(nandpsu, offset, size_of_buffer, (void *)buffer);
  rtems_mutex_unlock(&(get_flash_control(super)->access_lock));
  if (sc) {
    return -EIO;
  }
  return 0;
}

static int flash_erase(
  rtems_jffs2_flash_control *super,
  uint32_t offset
)
{
  XNandPsu *nandpsu = get_flash_control(super)->nandpsu;
  rtems_status_code sc;
  uint64_t BlockSize = nandpsu->Geometry.BlockSize;

  if (offset > nandpsu->Geometry.DeviceSize) {
    return -EIO;
  }

  /* Perform erase operation. */
  rtems_mutex_lock(&(get_flash_control(super)->access_lock));
  sc = XNandPsu_Erase(nandpsu, RTEMS_ALIGN_DOWN(offset, BlockSize), BlockSize);
  rtems_mutex_unlock(&(get_flash_control(super)->access_lock));
  if (sc ) {
    return -EIO;
  }

  return 0;
}

static int flash_block_is_bad(
  rtems_jffs2_flash_control *super,
  uint32_t offset,
  bool *bad
)
{
  XNandPsu *nandpsu = get_flash_control(super)->nandpsu;
  uint32_t BlockIndex;
  uint8_t BlockData;
  uint8_t BlockShift;
  uint8_t BlockType;
  uint32_t BlockOffset;

  assert(bad);

  if (offset > nandpsu->Geometry.DeviceSize) {
    return -EIO;
  }

  *bad = true;

  BlockIndex = offset / nandpsu->Geometry.BlockSize;

  rtems_mutex_lock(&(get_flash_control(super)->access_lock));

  /* XNandPsu_IsBlockBad() is insufficient for this use case */
  BlockOffset = BlockIndex >> XNANDPSU_BBT_BLOCK_SHIFT;
  BlockShift = XNandPsu_BbtBlockShift(BlockIndex);
  BlockData = nandpsu->Bbt[BlockOffset];
  BlockType = (BlockData >> BlockShift) & XNANDPSU_BLOCK_TYPE_MASK;

  if (BlockType == XNANDPSU_BLOCK_GOOD) {
    *bad = false;
  }

  int TargetBlockIndex = BlockIndex % nandpsu->Geometry.NumTargetBlocks;
  /* The last 4 blocks of every device target are reserved for the BBT */
  if (nandpsu->Geometry.NumTargetBlocks - TargetBlockIndex <= 4) {
    *bad = true;
  }

  rtems_mutex_unlock(&(get_flash_control(super)->access_lock));
  return 0;
}

static int flash_block_mark_bad(
  rtems_jffs2_flash_control *super,
  uint32_t offset
)
{
  rtems_status_code sc;
  XNandPsu *nandpsu = get_flash_control(super)->nandpsu;
  uint32_t BlockIndex;

  if (offset > nandpsu->Geometry.DeviceSize) {
    return -EIO;
  }

  BlockIndex = offset / nandpsu->Geometry.BlockSize;

  rtems_mutex_lock(&(get_flash_control(super)->access_lock));
  sc = XNandPsu_MarkBlockBad(nandpsu, BlockIndex);
  rtems_mutex_unlock(&(get_flash_control(super)->access_lock));
  if ( sc != XST_SUCCESS ) {
    return -EIO;
  }
  return RTEMS_SUCCESSFUL;
}

static int flash_read_oob_locked(
  rtems_jffs2_flash_control *super,
  uint32_t offset,
  uint8_t *oobbuf,
  uint32_t ooblen
)
{
  uint8_t *spare_bytes;
  XNandPsu *nandpsu = get_flash_control(super)->nandpsu;
  uint32_t SpareBytesPerPage = nandpsu->Geometry.SpareBytesPerPage;

  if (offset > nandpsu->Geometry.DeviceSize) {
    return -EIO;
  }

  /* Can't request more spare bytes than exist */
  if (ooblen > SpareBytesPerPage * nandpsu->Geometry.PagesPerBlock) {
    return -EIO;
  }

  /* Get page index */
  uint32_t PageIndex = offset / nandpsu->Geometry.BytesPerPage;

  spare_bytes = rtems_malloc(SpareBytesPerPage);
  if (spare_bytes == NULL) {
    return -ENOMEM;
  }

  while (ooblen) {
    int rv = XNandPsu_ReadSpareBytes(nandpsu, PageIndex, spare_bytes);
    /* no guarantee oobbuf can hold all of spare bytes, so read and then copy */
    uint32_t readlen = SpareBytesPerPage;
    if (ooblen < readlen) {
	    readlen = ooblen;
    }

    if (rv) {
      free(spare_bytes);
      return -EIO;
    }

    memcpy(oobbuf, spare_bytes, readlen);

    PageIndex++;
    ooblen -= readlen;
    oobbuf += readlen;
  }
  free(spare_bytes);
  return RTEMS_SUCCESSFUL;
}

static int flash_read_oob(
  rtems_jffs2_flash_control *super,
  uint32_t offset,
  uint8_t *oobbuf,
  uint32_t ooblen
)
{
  rtems_mutex_lock(&(get_flash_control(super)->access_lock));
  int ret = flash_read_oob_locked(super, offset, oobbuf, ooblen);
  rtems_mutex_unlock(&(get_flash_control(super)->access_lock));
  return ret;
}

static int flash_write_oob(
  rtems_jffs2_flash_control *super,
  uint32_t offset,
  uint8_t *oobbuf,
  uint32_t ooblen
)
{
  rtems_status_code sc;
  uint8_t *spare_bytes;
  uint8_t *buffer = oobbuf;
  XNandPsu *nandpsu = get_flash_control(super)->nandpsu;
  uint32_t SpareBytesPerPage = nandpsu->Geometry.SpareBytesPerPage;

  if (offset > nandpsu->Geometry.DeviceSize) {
    return -EIO;
  }

  /* Writing a page spare area to large will result in ignored data.  */
  if (ooblen > SpareBytesPerPage) {
    return -EIO;
  }

  spare_bytes = rtems_malloc(SpareBytesPerPage);
  if (spare_bytes == NULL) {
    return -ENOMEM;
  }

  /* Writing a page spare area to small will result in invalid accesses */
  rtems_mutex_lock(&(get_flash_control(super)->access_lock));
  if (ooblen < SpareBytesPerPage) {
    int rv = flash_read_oob_locked(super, offset, spare_bytes, SpareBytesPerPage);
    if (rv) {
      free(spare_bytes);
      rtems_mutex_unlock(&(get_flash_control(super)->access_lock));
      return rv;
    }
    buffer = spare_bytes;
    memcpy(buffer, oobbuf, ooblen);
  }

  /* Get page index */
  uint32_t PageIndex = offset / nandpsu->Geometry.BytesPerPage;

  sc = XNandPsu_WriteSpareBytes(nandpsu, PageIndex, buffer);
  rtems_mutex_unlock(&(get_flash_control(super)->access_lock));
  free(spare_bytes);

  if ( sc != XST_SUCCESS ) {
    return -EIO;
  }
  return RTEMS_SUCCESSFUL;
}

static uint32_t flash_get_oob_size(
  rtems_jffs2_flash_control *super
)
{
  flash_control *self = get_flash_control(super);

  return self->nandpsu->Geometry.SpareBytesPerPage;
}

static flash_control flash_instance = {
  .super = {
    .read = flash_read,
    .write = flash_write,
    .erase = flash_erase,
    .block_is_bad = flash_block_is_bad,
    .block_mark_bad = flash_block_mark_bad,
    .oob_read = flash_read_oob,
    .oob_write = flash_write_oob,
    .get_oob_size = flash_get_oob_size,
  }
};

static rtems_jffs2_compressor_control compressor_instance = {
  .compress = rtems_jffs2_compressor_rtime_compress,
  .decompress = rtems_jffs2_compressor_rtime_decompress
};

static rtems_jffs2_mount_data mount_data;

int xilinx_zynqmp_nand_jffs2_initialize(
  const char *mount_dir,
  XNandPsu *NandInstPtr
)
{
  flash_instance.super.block_size = NandInstPtr->Geometry.BlockSize;

  uint64_t max_size = 0x100000000LU - flash_instance.super.block_size;

  /* JFFS2 maximum FS size is one block less than 4GB */
  if (NandInstPtr->Geometry.DeviceSize > max_size) {
    flash_instance.super.flash_size = max_size;
  } else {
    flash_instance.super.flash_size = NandInstPtr->Geometry.DeviceSize;
  }

  flash_instance.super.write_size = NandInstPtr->Geometry.BytesPerPage;
  flash_instance.nandpsu = NandInstPtr;
  rtems_mutex_init(&flash_instance.access_lock, "XNandPsu JFFS2 adapter lock");
  mount_data.flash_control = &flash_instance.super;
  mount_data.compressor_control = &compressor_instance;

  int rv = 0;
  rv = mount(
    NULL,
    mount_dir,
    RTEMS_FILESYSTEM_TYPE_JFFS2,
    RTEMS_FILESYSTEM_READ_WRITE,
    &mount_data
  );
  if ( rv != 0 ) {
    return rv;
  }

  return 0;
}