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