William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 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 | |
William Juul | e24ebad | 2007-11-15 12:23:57 +0100 | [diff] [blame] | 14 | /* XXX U-BOOT XXX */ |
William Juul | e24ebad | 2007-11-15 12:23:57 +0100 | [diff] [blame] | 15 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 16 | #include "yportenv.h" |
| 17 | |
| 18 | |
| 19 | #include "yaffs_mtdif.h" |
| 20 | |
Masahiro Yamada | adae2ec | 2016-09-21 11:28:53 +0900 | [diff] [blame] | 21 | #include <linux/mtd/mtd.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/time.h> |
Masahiro Yamada | 2b7a873 | 2017-11-30 13:45:24 +0900 | [diff] [blame] | 24 | #include <linux/mtd/rawnand.h> |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 25 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 26 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 27 | static inline void translate_spare2oob(const struct yaffs_spare *spare, u8 *oob) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 28 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 29 | oob[0] = spare->tb0; |
| 30 | oob[1] = spare->tb1; |
| 31 | oob[2] = spare->tb2; |
| 32 | oob[3] = spare->tb3; |
| 33 | oob[4] = spare->tb4; |
| 34 | oob[5] = spare->tb5 & 0x3f; |
| 35 | oob[5] |= spare->block_status == 'Y' ? 0 : 0x80; |
| 36 | oob[5] |= spare->page_status == 0 ? 0 : 0x40; |
| 37 | oob[6] = spare->tb6; |
| 38 | oob[7] = spare->tb7; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 41 | static inline void translate_oob2spare(struct yaffs_spare *spare, u8 *oob) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 42 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 43 | struct yaffs_nand_spare *nspare = (struct yaffs_nand_spare *)spare; |
| 44 | spare->tb0 = oob[0]; |
| 45 | spare->tb1 = oob[1]; |
| 46 | spare->tb2 = oob[2]; |
| 47 | spare->tb3 = oob[3]; |
| 48 | spare->tb4 = oob[4]; |
| 49 | spare->tb5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f; |
| 50 | spare->block_status = oob[5] & 0x80 ? 0xff : 'Y'; |
| 51 | spare->page_status = oob[5] & 0x40 ? 0xff : 0; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 52 | spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 53 | spare->tb6 = oob[6]; |
| 54 | spare->tb7 = oob[7]; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 55 | spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff; |
| 56 | |
| 57 | nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */ |
| 58 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 59 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 60 | |
| 61 | int nandmtd_WriteChunkToNAND(struct yaffs_dev *dev, int chunkInNAND, |
| 62 | const u8 *data, const struct yaffs_spare *spare) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 63 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 64 | struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 65 | struct mtd_oob_ops ops; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 66 | size_t dummy; |
| 67 | int retval = 0; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 68 | loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk; |
| 69 | u8 spareAsBytes[8]; /* OOB */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 70 | |
| 71 | if (data && !spare) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 72 | retval = mtd_write(mtd, addr, dev->data_bytes_per_chunk, |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 73 | &dummy, data); |
| 74 | else if (spare) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 75 | if (dev->param.use_nand_ecc) { |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 76 | translate_spare2oob(spare, spareAsBytes); |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 77 | ops.mode = MTD_OPS_AUTO_OOB; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 78 | ops.ooblen = 8; /* temp hack */ |
| 79 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 80 | ops.mode = MTD_OPS_RAW; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 81 | ops.ooblen = YAFFS_BYTES_PER_SPARE; |
| 82 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 83 | ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 84 | ops.datbuf = (u8 *)data; |
| 85 | ops.ooboffs = 0; |
| 86 | ops.oobbuf = spareAsBytes; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 87 | retval = mtd_write_oob(mtd, addr, &ops); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 88 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 89 | |
| 90 | if (retval == 0) |
| 91 | return YAFFS_OK; |
| 92 | else |
| 93 | return YAFFS_FAIL; |
| 94 | } |
| 95 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 96 | int nandmtd_ReadChunkFromNAND(struct yaffs_dev *dev, int chunkInNAND, u8 *data, |
| 97 | struct yaffs_spare *spare) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 98 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 99 | struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 100 | struct mtd_oob_ops ops; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 101 | size_t dummy; |
| 102 | int retval = 0; |
| 103 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 104 | loff_t addr = ((loff_t) chunkInNAND) * dev->data_bytes_per_chunk; |
| 105 | u8 spareAsBytes[8]; /* OOB */ |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 106 | |
| 107 | if (data && !spare) |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 108 | retval = mtd_read(mtd, addr, dev->data_bytes_per_chunk, |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 109 | &dummy, data); |
| 110 | else if (spare) { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 111 | if (dev->param.use_nand_ecc) { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 112 | ops.mode = MTD_OPS_AUTO_OOB; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 113 | ops.ooblen = 8; /* temp hack */ |
| 114 | } else { |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 115 | ops.mode = MTD_OPS_RAW; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 116 | ops.ooblen = YAFFS_BYTES_PER_SPARE; |
| 117 | } |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 118 | ops.len = data ? dev->data_bytes_per_chunk : ops.ooblen; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 119 | ops.datbuf = data; |
| 120 | ops.ooboffs = 0; |
| 121 | ops.oobbuf = spareAsBytes; |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 122 | retval = mtd_read_oob(mtd, addr, &ops); |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 123 | if (dev->param.use_nand_ecc) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 124 | translate_oob2spare(spare, spareAsBytes); |
| 125 | } |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 126 | |
| 127 | if (retval == 0) |
| 128 | return YAFFS_OK; |
| 129 | else |
| 130 | return YAFFS_FAIL; |
| 131 | } |
| 132 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 133 | int nandmtd_EraseBlockInNAND(struct yaffs_dev *dev, int blockNumber) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 134 | { |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 135 | struct mtd_info *mtd = (struct mtd_info *)(dev->driver_context); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 136 | __u32 addr = |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 137 | ((loff_t) blockNumber) * dev->data_bytes_per_chunk |
| 138 | * dev->param.chunks_per_block; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 139 | struct erase_info ei; |
| 140 | int retval = 0; |
| 141 | |
| 142 | ei.mtd = mtd; |
| 143 | ei.addr = addr; |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 144 | ei.len = dev->data_bytes_per_chunk * dev->param.chunks_per_block; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 145 | ei.time = 1000; |
| 146 | ei.retries = 2; |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 147 | ei.priv = (u_long) dev; |
| 148 | |
| 149 | /* Todo finish off the ei if required */ |
| 150 | |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 151 | |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 152 | retval = mtd_erase(mtd, &ei); |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 153 | |
| 154 | if (retval == 0) |
| 155 | return YAFFS_OK; |
| 156 | else |
| 157 | return YAFFS_FAIL; |
| 158 | } |
| 159 | |
Charles Manning | 3796e1f | 2012-05-09 16:55:17 +0000 | [diff] [blame] | 160 | int nandmtd_InitialiseNAND(struct yaffs_dev *dev) |
William Juul | c051bbe | 2007-11-15 11:13:05 +0100 | [diff] [blame] | 161 | { |
| 162 | return YAFFS_OK; |
| 163 | } |