]> git.ipfire.org Git - people/ms/u-boot.git/blame - fs/yaffs2/yaffs_mtdif.c
mtd: nand: Rename nand.h into rawnand.h
[people/ms/u-boot.git] / fs / yaffs2 / yaffs_mtdif.c
CommitLineData
0e8cc8bd
WJ
1/*
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
90ef117b
WJ
14/* XXX U-BOOT XXX */
15#include <common.h>
16
0e8cc8bd
WJ
17#include "yportenv.h"
18
19
20#include "yaffs_mtdif.h"
21
b5bf5cb3
MY
22#include <linux/mtd/mtd.h>
23#include <linux/types.h>
24#include <linux/time.h>
6ae3900a 25#include <linux/mtd/rawnand.h>
0e8cc8bd 26
0e8cc8bd 27
753ac610 28static inline void translate_spare2oob(const struct yaffs_spare *spare, u8 *oob)
0e8cc8bd 29{
753ac610
CM
30 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;
0e8cc8bd
WJ
40}
41
753ac610 42static inline void translate_oob2spare(struct yaffs_spare *spare, u8 *oob)
0e8cc8bd 43{
753ac610
CM
44 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;
0e8cc8bd 53 spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
753ac610
CM
54 spare->tb6 = oob[6];
55 spare->tb7 = oob[7];
0e8cc8bd
WJ
56 spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
57
58 nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
59}
0e8cc8bd 60
753ac610
CM
61
62int nandmtd_WriteChunkToNAND(struct yaffs_dev *dev, int chunkInNAND,
63 const u8 *data, const struct yaffs_spare *spare)
0e8cc8bd 64{
753ac610 65 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
0e8cc8bd 66 struct mtd_oob_ops ops;
0e8cc8bd
WJ
67 size_t dummy;
68 int retval = 0;
753ac610
CM
69 loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
70 u8 spareAsBytes[8]; /* OOB */
0e8cc8bd
WJ
71
72 if (data && !spare)
dfe64e2c 73 retval = mtd_write(mtd, addr, dev->data_bytes_per_chunk,
0e8cc8bd
WJ
74 &dummy, data);
75 else if (spare) {
753ac610 76 if (dev->param.use_nand_ecc) {
0e8cc8bd 77 translate_spare2oob(spare, spareAsBytes);
dfe64e2c 78 ops.mode = MTD_OPS_AUTO_OOB;
0e8cc8bd
WJ
79 ops.ooblen = 8; /* temp hack */
80 } else {
dfe64e2c 81 ops.mode = MTD_OPS_RAW;
0e8cc8bd
WJ
82 ops.ooblen = YAFFS_BYTES_PER_SPARE;
83 }
753ac610 84 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
0e8cc8bd
WJ
85 ops.datbuf = (u8 *)data;
86 ops.ooboffs = 0;
87 ops.oobbuf = spareAsBytes;
dfe64e2c 88 retval = mtd_write_oob(mtd, addr, &ops);
0e8cc8bd 89 }
0e8cc8bd
WJ
90
91 if (retval == 0)
92 return YAFFS_OK;
93 else
94 return YAFFS_FAIL;
95}
96
753ac610
CM
97int nandmtd_ReadChunkFromNAND(struct yaffs_dev *dev, int chunkInNAND, u8 *data,
98 struct yaffs_spare *spare)
0e8cc8bd 99{
753ac610 100 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
0e8cc8bd 101 struct mtd_oob_ops ops;
0e8cc8bd
WJ
102 size_t dummy;
103 int retval = 0;
104
753ac610
CM
105 loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk;
106 u8 spareAsBytes[8]; /* OOB */
0e8cc8bd
WJ
107
108 if (data && !spare)
dfe64e2c 109 retval = mtd_read(mtd, addr, dev->data_bytes_per_chunk,
0e8cc8bd
WJ
110 &dummy, data);
111 else if (spare) {
753ac610 112 if (dev->param.use_nand_ecc) {
dfe64e2c 113 ops.mode = MTD_OPS_AUTO_OOB;
0e8cc8bd
WJ
114 ops.ooblen = 8; /* temp hack */
115 } else {
dfe64e2c 116 ops.mode = MTD_OPS_RAW;
0e8cc8bd
WJ
117 ops.ooblen = YAFFS_BYTES_PER_SPARE;
118 }
753ac610 119 ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen;
0e8cc8bd
WJ
120 ops.datbuf = data;
121 ops.ooboffs = 0;
122 ops.oobbuf = spareAsBytes;
dfe64e2c 123 retval = mtd_read_oob(mtd, addr, &ops);
753ac610 124 if (dev->param.use_nand_ecc)
0e8cc8bd
WJ
125 translate_oob2spare(spare, spareAsBytes);
126 }
0e8cc8bd
WJ
127
128 if (retval == 0)
129 return YAFFS_OK;
130 else
131 return YAFFS_FAIL;
132}
133
753ac610 134int nandmtd_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber)
0e8cc8bd 135{
753ac610 136 struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context);
0e8cc8bd 137 __u32 addr =
753ac610
CM
138 ((loff_t) blockNumber) * dev->data_bytes_per_chunk
139 * dev->param.chunks_per_block;
0e8cc8bd
WJ
140 struct erase_info ei;
141 int retval = 0;
142
143 ei.mtd = mtd;
144 ei.addr = addr;
753ac610 145 ei.len = dev->data_bytes_per_chunk * dev->param.chunks_per_block;
0e8cc8bd
WJ
146 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
0e8cc8bd 153
dfe64e2c 154 retval = mtd_erase(mtd, &ei);
0e8cc8bd
WJ
155
156 if (retval == 0)
157 return YAFFS_OK;
158 else
159 return YAFFS_FAIL;
160}
161
753ac610 162int nandmtd_InitialiseNAND(struct yaffs_dev *dev)
0e8cc8bd
WJ
163{
164 return YAFFS_OK;
165}