blob: d338f9aa918f30b77c390bd763fc13cec1fe51a9 [file] [log] [blame]
William Juulc051bbe2007-11-15 11:13:05 +01001/*
2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3 *
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
6 *
7 * Created by Charles Manning <charles@aleph1.co.uk>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
William Juule24ebad2007-11-15 12:23:57 +010014/* XXX U-BOOT XXX */
15#include <common.h>
16
William Juulc051bbe2007-11-15 11:13:05 +010017#include "yportenv.h"
18
19
20#include "yaffs_mtdif.h"
21
Masahiro Yamadaadae2ec2016-09-21 11:28:53 +090022#include <linux/mtd/mtd.h>
23#include <linux/types.h>
24#include <linux/time.h>
Masahiro Yamada2b7a8732017-11-30 13:45:24 +090025#include <linux/mtd/rawnand.h>
William Juulc051bbe2007-11-15 11:13:05 +010026
William Juulc051bbe2007-11-15 11:13:05 +010027
Charles Manning3796e1f2012-05-09 16:55:17 +000028static inline void translate_spare2oob(const struct yaffs_spare *spare, u8 *oob)
William Juulc051bbe2007-11-15 11:13:05 +010029{
Charles Manning3796e1f2012-05-09 16:55:17 +000030 oob[0] = spare->tb0;
31 oob[1] = spare->tb1;
32 oob[2] = spare->tb2;
33 oob[3] = spare->tb3;
34 oob[4] = spare->tb4;
35 oob[5] = spare->tb5 & 0x3f;
36 oob[5] |= spare->block_status == 'Y' ? 0 : 0x80;
37 oob[5] |= spare->page_status == 0 ? 0 : 0x40;
38 oob[6] = spare->tb6;
39 oob[7] = spare->tb7;
William Juulc051bbe2007-11-15 11:13:05 +010040}
41
Charles Manning3796e1f2012-05-09 16:55:17 +000042static inline void translate_oob2spare(struct yaffs_spare *spare, u8 *oob)
William Juulc051bbe2007-11-15 11:13:05 +010043{
Charles Manning3796e1f2012-05-09 16:55:17 +000044 struct yaffs_nand_spare *nspare = (struct yaffs_nand_spare *)spare;
45 spare->tb0 = oob[0];
46 spare->tb1 = oob[1];
47 spare->tb2 = oob[2];
48 spare->tb3 = oob[3];
49 spare->tb4 = oob[4];
50 spare->tb5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
51 spare->block_status = oob[5] & 0x80 ? 0xff : 'Y';
52 spare->page_status = oob[5] & 0x40 ? 0xff : 0;
William Juulc051bbe2007-11-15 11:13:05 +010053 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
Charles Manning3796e1f2012-05-09 16:55:17 +000054 spare->tb6 = oob[6];
55 spare->tb7 = oob[7];
William Juulc051bbe2007-11-15 11:13:05 +010056 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
57
58 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
59}
William Juulc051bbe2007-11-15 11:13:05 +010060
Charles Manning3796e1f2012-05-09 16:55:17 +000061
62int nandmtd_WriteChunkToNAND(struct yaffs_dev *dev, int chunkInNAND,
63 const u8 *data, const struct yaffs_spare *spare)
William Juulc051bbe2007-11-15 11:13:05 +010064{
Charles Manning3796e1f2012-05-09 16:55:17 +000065 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
William Juulc051bbe2007-11-15 11:13:05 +010066 struct mtd_oob_ops ops;
William Juulc051bbe2007-11-15 11:13:05 +010067 size_t dummy;
68 int retval = 0;
Charles Manning3796e1f2012-05-09 16:55:17 +000069 loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
70 u8 spareAsBytes[8]; /* OOB */
William Juulc051bbe2007-11-15 11:13:05 +010071
72 if (data && !spare)
Sergey Lapin3a38a552013-01-14 03:46:50 +000073 retval = mtd_write(mtd, addr, dev->data_bytes_per_chunk,
William Juulc051bbe2007-11-15 11:13:05 +010074 &dummy, data);
75 else if (spare) {
Charles Manning3796e1f2012-05-09 16:55:17 +000076 if (dev->param.use_nand_ecc) {
William Juulc051bbe2007-11-15 11:13:05 +010077 translate_spare2oob(spare, spareAsBytes);
Sergey Lapin3a38a552013-01-14 03:46:50 +000078 ops.mode = MTD_OPS_AUTO_OOB;
William Juulc051bbe2007-11-15 11:13:05 +010079 ops.ooblen = 8; /* temp hack */
80 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +000081 ops.mode = MTD_OPS_RAW;
William Juulc051bbe2007-11-15 11:13:05 +010082 ops.ooblen = YAFFS_BYTES_PER_SPARE;
83 }
Charles Manning3796e1f2012-05-09 16:55:17 +000084 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
William Juulc051bbe2007-11-15 11:13:05 +010085 ops.datbuf = (u8 *)data;
86 ops.ooboffs = 0;
87 ops.oobbuf = spareAsBytes;
Sergey Lapin3a38a552013-01-14 03:46:50 +000088 retval = mtd_write_oob(mtd, addr, &ops);
William Juulc051bbe2007-11-15 11:13:05 +010089 }
William Juulc051bbe2007-11-15 11:13:05 +010090
91 if (retval == 0)
92 return YAFFS_OK;
93 else
94 return YAFFS_FAIL;
95}
96
Charles Manning3796e1f2012-05-09 16:55:17 +000097int nandmtd_ReadChunkFromNAND(struct yaffs_dev *dev, int chunkInNAND, u8 *data,
98 struct yaffs_spare *spare)
William Juulc051bbe2007-11-15 11:13:05 +010099{
Charles Manning3796e1f2012-05-09 16:55:17 +0000100 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
William Juulc051bbe2007-11-15 11:13:05 +0100101 struct mtd_oob_ops ops;
William Juulc051bbe2007-11-15 11:13:05 +0100102 size_t dummy;
103 int retval = 0;
104
Charles Manning3796e1f2012-05-09 16:55:17 +0000105 loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
106 u8 spareAsBytes[8]; /* OOB */
William Juulc051bbe2007-11-15 11:13:05 +0100107
108 if (data && !spare)
Sergey Lapin3a38a552013-01-14 03:46:50 +0000109 retval = mtd_read(mtd, addr, dev->data_bytes_per_chunk,
William Juulc051bbe2007-11-15 11:13:05 +0100110 &dummy, data);
111 else if (spare) {
Charles Manning3796e1f2012-05-09 16:55:17 +0000112 if (dev->param.use_nand_ecc) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000113 ops.mode = MTD_OPS_AUTO_OOB;
William Juulc051bbe2007-11-15 11:13:05 +0100114 ops.ooblen = 8; /* temp hack */
115 } else {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000116 ops.mode = MTD_OPS_RAW;
William Juulc051bbe2007-11-15 11:13:05 +0100117 ops.ooblen = YAFFS_BYTES_PER_SPARE;
118 }
Charles Manning3796e1f2012-05-09 16:55:17 +0000119 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
William Juulc051bbe2007-11-15 11:13:05 +0100120 ops.datbuf = data;
121 ops.ooboffs = 0;
122 ops.oobbuf = spareAsBytes;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000123 retval = mtd_read_oob(mtd, addr, &ops);
Charles Manning3796e1f2012-05-09 16:55:17 +0000124 if (dev->param.use_nand_ecc)
William Juulc051bbe2007-11-15 11:13:05 +0100125 translate_oob2spare(spare, spareAsBytes);
126 }
William Juulc051bbe2007-11-15 11:13:05 +0100127
128 if (retval == 0)
129 return YAFFS_OK;
130 else
131 return YAFFS_FAIL;
132}
133
Charles Manning3796e1f2012-05-09 16:55:17 +0000134int nandmtd_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
William Juulc051bbe2007-11-15 11:13:05 +0100135{
Charles Manning3796e1f2012-05-09 16:55:17 +0000136 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
William Juulc051bbe2007-11-15 11:13:05 +0100137 __u32 addr =
Charles Manning3796e1f2012-05-09 16:55:17 +0000138 ((loff_t) blockNumber) * dev->data_bytes_per_chunk
139 * dev->param.chunks_per_block;
William Juulc051bbe2007-11-15 11:13:05 +0100140 struct erase_info ei;
141 int retval = 0;
142
143 ei.mtd = mtd;
144 ei.addr = addr;
Charles Manning3796e1f2012-05-09 16:55:17 +0000145 ei.len = dev->data_bytes_per_chunk * dev->param.chunks_per_block;
William Juulc051bbe2007-11-15 11:13:05 +0100146 ei.time = 1000;
147 ei.retries = 2;
148 ei.callback = NULL;
149 ei.priv = (u_long) dev;
150
151 /* Todo finish off the ei if required */
152
William Juulc051bbe2007-11-15 11:13:05 +0100153
Sergey Lapin3a38a552013-01-14 03:46:50 +0000154 retval = mtd_erase(mtd, &ei);
William Juulc051bbe2007-11-15 11:13:05 +0100155
156 if (retval == 0)
157 return YAFFS_OK;
158 else
159 return YAFFS_FAIL;
160}
161
Charles Manning3796e1f2012-05-09 16:55:17 +0000162int nandmtd_InitialiseNAND(struct yaffs_dev *dev)
William Juulc051bbe2007-11-15 11:13:05 +0100163{
164 return YAFFS_OK;
165}