26 #include <linux/module.h>
27 #include <linux/resource.h>
28 #include <linux/sched.h>
29 #include <linux/types.h>
37 #include <linux/slab.h>
44 .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52,
45 66, 67, 68, 82, 83, 84, 98, 99, 100, 114, 115, 116},
47 {.offset = 8, .length = 8},
48 {.offset = 24, .length = 8},
49 {.offset = 40, .length = 8},
50 {.offset = 56, .length = 8},
51 {.offset = 72, .length = 8},
52 {.offset = 88, .length = 8},
53 {.offset = 104, .length = 8},
54 {.offset = 120, .length = 8}
60 .eccpos = {2, 3, 4, 18, 19, 20, 34, 35, 36, 50, 51, 52},
62 {.offset = 8, .length = 8},
63 {.offset = 24, .length = 8},
64 {.offset = 40, .length = 8},
65 {.offset = 56, .length = 8},
73 {.offset = 8, .length = 8},
84 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
85 9, 10, 11, 12, 13, 14,
86 18, 19, 20, 21, 22, 23, 24,
87 25, 26, 27, 28, 29, 30,
88 34, 35, 36, 37, 38, 39, 40,
89 41, 42, 43, 44, 45, 46,
90 50, 51, 52, 53, 54, 55, 56,
91 57, 58, 59, 60, 61, 62,
92 66, 67, 68, 69, 70, 71, 72,
93 73, 74, 75, 76, 77, 78,
94 82, 83, 84, 85, 86, 87, 88,
95 89, 90, 91, 92, 93, 94,
96 98, 99, 100, 101, 102, 103, 104,
97 105, 106, 107, 108, 109, 110,
98 114, 115, 116, 117, 118, 119, 120,
99 121, 122, 123, 124, 125, 126,
100 130, 131, 132, 133, 134, 135, 136,
101 137, 138, 139, 140, 141, 142,
102 146, 147, 148, 149, 150, 151, 152,
103 153, 154, 155, 156, 157, 158,
104 162, 163, 164, 165, 166, 167, 168,
105 169, 170, 171, 172, 173, 174,
106 178, 179, 180, 181, 182, 183, 184,
107 185, 186, 187, 188, 189, 190,
108 194, 195, 196, 197, 198, 199, 200,
109 201, 202, 203, 204, 205, 206,
110 210, 211, 212, 213, 214, 215, 216,
111 217, 218, 219, 220, 221, 222,
112 226, 227, 228, 229, 230, 231, 232,
113 233, 234, 235, 236, 237, 238,
114 242, 243, 244, 245, 246, 247, 248,
115 249, 250, 251, 252, 253, 254
118 {.offset = 15, .length = 3},
119 {.offset = 31, .length = 3},
120 {.offset = 47, .length = 3},
121 {.offset = 63, .length = 3},
122 {.offset = 79, .length = 3},
123 {.offset = 95, .length = 3},
124 {.offset = 111, .length = 3},
125 {.offset = 127, .length = 3},
126 {.offset = 143, .length = 3},
127 {.offset = 159, .length = 3},
128 {.offset = 175, .length = 3},
129 {.offset = 191, .length = 3},
130 {.offset = 207, .length = 3},
131 {.offset = 223, .length = 3},
132 {.offset = 239, .length = 3},
133 {.offset = 255, .length = 1}
144 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
145 9, 10, 11, 12, 13, 14,
146 18, 19, 20, 21, 22, 23, 24,
147 25, 26, 27, 28, 29, 30,
148 34, 35, 36, 37, 38, 39, 40,
149 41, 42, 43, 44, 45, 46,
150 50, 51, 52, 53, 54, 55, 56,
151 57, 58, 59, 60, 61, 62,
152 66, 67, 68, 69, 70, 71, 72,
153 73, 74, 75, 76, 77, 78,
154 82, 83, 84, 85, 86, 87, 88,
155 89, 90, 91, 92, 93, 94,
156 98, 99, 100, 101, 102, 103, 104,
157 105, 106, 107, 108, 109, 110,
158 114, 115, 116, 117, 118, 119, 120,
159 121, 122, 123, 124, 125, 126
162 {.offset = 15, .length = 3},
163 {.offset = 31, .length = 3},
164 {.offset = 47, .length = 3},
165 {.offset = 63, .length = 3},
166 {.offset = 79, .length = 3},
167 {.offset = 95, .length = 3},
168 {.offset = 111, .length = 3},
169 {.offset = 127, .length = 97}
180 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
181 9, 10, 11, 12, 13, 14,
182 18, 19, 20, 21, 22, 23, 24,
183 25, 26, 27, 28, 29, 30,
184 34, 35, 36, 37, 38, 39, 40,
185 41, 42, 43, 44, 45, 46,
186 50, 51, 52, 53, 54, 55, 56,
187 57, 58, 59, 60, 61, 62,
188 66, 67, 68, 69, 70, 71, 72,
189 73, 74, 75, 76, 77, 78,
190 82, 83, 84, 85, 86, 87, 88,
191 89, 90, 91, 92, 93, 94,
192 98, 99, 100, 101, 102, 103, 104,
193 105, 106, 107, 108, 109, 110,
194 114, 115, 116, 117, 118, 119, 120,
195 121, 122, 123, 124, 125, 126
198 {.offset = 15, .length = 3},
199 {.offset = 31, .length = 3},
200 {.offset = 47, .length = 3},
201 {.offset = 63, .length = 3},
202 {.offset = 79, .length = 3},
203 {.offset = 95, .length = 3},
204 {.offset = 111, .length = 3},
205 {.offset = 127, .length = 1}
216 .eccpos = { 2, 3, 4, 5, 6, 7, 8,
217 9, 10, 11, 12, 13, 14,
218 18, 19, 20, 21, 22, 23, 24,
219 25, 26, 27, 28, 29, 30,
220 34, 35, 36, 37, 38, 39, 40,
221 41, 42, 43, 44, 45, 46,
222 50, 51, 52, 53, 54, 55, 56,
223 57, 58, 59, 60, 61, 62,
226 {.offset = 15, .length = 3},
227 {.offset = 31, .length = 3},
228 {.offset = 47, .length = 3},
229 {.offset = 63, .length = 1},
240 .eccpos = { 0, 1, 2, 3, 6, 7, 8,
241 9, 10, 11, 12, 13, 14
244 {.offset = 15, .length = 1},
259 {.offset = 2, .length = 13},
260 {.offset = 18, .length = 13},
261 {.offset = 34, .length = 13},
262 {.offset = 50, .length = 13},
263 {.offset = 66, .length = 13},
264 {.offset = 82, .length = 13},
265 {.offset = 98, .length = 13},
266 {.offset = 114, .length = 13}
272 {.offset = 0, .length = 4},
273 {.offset = 6, .length = 9}
330 static void fsmc_select_chip(
struct mtd_info *mtd,
int chipnr)
371 this->IO_ADDR_R = host->
cmd_va;
372 this->IO_ADDR_W = host->
cmd_va;
374 this->IO_ADDR_R = host->
addr_va;
375 this->IO_ADDR_W = host->
addr_va;
377 this->IO_ADDR_R = host->
data_va;
378 this->IO_ADDR_W = host->
data_va;
392 writeb(cmd, this->IO_ADDR_W);
405 uint32_t tclr, tar, thiz, thold, twait, tset;
419 tims = &default_timings;
442 static void fsmc_enable_hwecc(
struct mtd_info *mtd,
int mode)
480 dev_err(host->
dev,
"calculate ecc timed out\n");
485 ecc[0] = (
uint8_t) (ecc_tmp >> 0);
486 ecc[1] = (
uint8_t) (ecc_tmp >> 8);
487 ecc[2] = (
uint8_t) (ecc_tmp >> 16);
488 ecc[3] = (
uint8_t) (ecc_tmp >> 24);
491 ecc[4] = (
uint8_t) (ecc_tmp >> 0);
492 ecc[5] = (
uint8_t) (ecc_tmp >> 8);
493 ecc[6] = (
uint8_t) (ecc_tmp >> 16);
494 ecc[7] = (
uint8_t) (ecc_tmp >> 24);
497 ecc[8] = (
uint8_t) (ecc_tmp >> 0);
498 ecc[9] = (
uint8_t) (ecc_tmp >> 8);
499 ecc[10] = (
uint8_t) (ecc_tmp >> 16);
500 ecc[11] = (
uint8_t) (ecc_tmp >> 24);
503 ecc[12] = (
uint8_t) (ecc_tmp >> 16);
513 static int fsmc_read_hwecc_ecc1(
struct mtd_info *mtd,
const uint8_t *data,
523 ecc[0] = (
uint8_t) (ecc_tmp >> 0);
524 ecc[1] = (
uint8_t) (ecc_tmp >> 8);
525 ecc[2] = (
uint8_t) (ecc_tmp >> 16);
533 int k, written_bits = 0;
535 for (k = 0; k <
size; k++) {
537 if (written_bits > max_bits)
544 static void dma_complete(
void *
param)
586 dev_err(host->
dev,
"device_prep_dma_memcpy error\n");
597 dev_err(host->
dev,
"dma_submit_error %d\n", cookie);
601 dma_async_issue_pending(chan);
608 dev_err(host->
dev,
"wait_for_completion_timeout\n");
630 for (i = 0; i < len; i++)
633 for (i = 0; i < len; i++)
653 for (i = 0; i < len; i++)
656 for (i = 0; i < len; i++)
667 static void fsmc_read_buf_dma(
struct mtd_info *mtd,
uint8_t *buf,
int len)
681 static void fsmc_write_buf_dma(
struct mtd_info *mtd,
const uint8_t *buf,
711 int eccbytes = chip->
ecc.bytes;
712 int eccsteps = chip->
ecc.steps;
716 int off, len,
group = 0;
724 unsigned int max_bitflips = 0;
726 for (i = 0, s = 0; s < eccsteps; s++, i += eccbytes, p += eccsize) {
731 for (j = 0; j < eccbytes;) {
749 memcpy(&ecc_code[i], oob, chip->
ecc.bytes);
750 chip->
ecc.calculate(mtd, p, &ecc_calc[i]);
752 stat = chip->
ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
757 max_bitflips =
max_t(
unsigned int, max_bitflips, stat);
781 unsigned int bank = host->
bank;
808 int bits_ecc = count_written_bits(read_ecc, chip->
ecc.bytes, 8);
809 int bits_data = count_written_bits(dat, chip->
ecc.size, 8);
811 if ((bits_ecc + bits_data) <= 8) {
834 err_idx[0] = (ecc1 >> 0) & 0x1FFF;
835 err_idx[1] = (ecc1 >> 13) & 0x1FFF;
836 err_idx[2] = (((ecc2 >> 0) & 0x7F) << 6) | ((ecc1 >> 26) & 0x3F);
837 err_idx[3] = (ecc2 >> 7) & 0x1FFF;
838 err_idx[4] = (((ecc3 >> 0) & 0x1) << 12) | ((ecc2 >> 20) & 0xFFF);
839 err_idx[5] = (ecc3 >> 1) & 0x1FFF;
840 err_idx[6] = (ecc3 >> 14) & 0x1FFF;
841 err_idx[7] = (((ecc4 >> 16) & 0xFF) << 5) | ((ecc3 >> 27) & 0x1F);
848 if (err_idx[i] < chip->
ecc.size * 8) {
871 if (!of_property_read_u32(np,
"bank-width", &val)) {
874 }
else if (val != 1) {
875 dev_err(&pdev->
dev,
"invalid bank-width %u\n", val);
879 of_property_read_u32(np,
"st,ale-off", &pdata->
ale_off);
880 of_property_read_u32(np,
"st,cle-off", &pdata->
cle_off);
915 ret = fsmc_nand_probe_config_dt(pdev, np);
923 dev_err(&pdev->
dev,
"platform data is NULL\n");
930 dev_err(&pdev->
dev,
"failed to allocate device structure\n");
940 dev_err(&pdev->
dev,
"Failed to get memory data resourse\n");
953 resource_size(res), pdev->
name)) {
954 dev_err(&pdev->
dev,
"Failed to get memory ale resourse\n");
966 resource_size(res), pdev->
name)) {
967 dev_err(&pdev->
dev,
"Failed to get memory cle resourse\n");
984 dev_err(&pdev->
dev,
"Failed to get memory regs resourse\n");
996 if (IS_ERR(host->
clk)) {
997 dev_err(&pdev->
dev,
"failed to fetch block clock\n");
998 return PTR_ERR(host->
clk);
1001 ret = clk_prepare_enable(host->
clk);
1003 goto err_clk_prepare_enable;
1009 for (pid = 0, i = 0; i < 4; i++)
1010 pid |= (
readl(host->
regs_va + resource_size(res) - 0x20 + 4 * i) & 255) << (i * 8);
1012 dev_info(&pdev->
dev,
"FSMC device partno %03x, manufacturer %02x, "
1013 "revision %02x, config %02x\n",
1041 nand->
ecc.hwctl = fsmc_enable_hwecc;
1042 nand->
ecc.size = 512;
1050 switch (host->
mode) {
1057 dev_err(&pdev->
dev,
"Unable to get read dma channel\n");
1058 goto err_req_read_chnl;
1063 dev_err(&pdev->
dev,
"Unable to get write dma channel\n");
1064 goto err_req_write_chnl;
1066 nand->
read_buf = fsmc_read_buf_dma;
1082 nand->
ecc.read_page = fsmc_read_page_hwecc;
1083 nand->
ecc.calculate = fsmc_read_hwecc_ecc4;
1084 nand->
ecc.correct = fsmc_bch8_correct_data;
1085 nand->
ecc.bytes = 13;
1086 nand->
ecc.strength = 8;
1088 nand->
ecc.calculate = fsmc_read_hwecc_ecc1;
1090 nand->
ecc.bytes = 3;
1091 nand->
ecc.strength = 1;
1099 dev_err(&pdev->
dev,
"No NAND Device found!\n");
1100 goto err_scan_ident;
1104 switch (host->
mtd.oobsize) {
1106 nand->
ecc.layout = &fsmc_ecc4_16_layout;
1110 nand->
ecc.layout = &fsmc_ecc4_64_layout;
1114 nand->
ecc.layout = &fsmc_ecc4_128_layout;
1118 nand->
ecc.layout = &fsmc_ecc4_224_layout;
1122 nand->
ecc.layout = &fsmc_ecc4_256_layout;
1127 "oobsize %d\n", mtd->
oobsize);
1131 switch (host->
mtd.oobsize) {
1133 nand->
ecc.layout = &fsmc_ecc1_16_layout;
1136 nand->
ecc.layout = &fsmc_ecc1_64_layout;
1139 nand->
ecc.layout = &fsmc_ecc1_128_layout;
1143 "oobsize %d\n", mtd->
oobsize);
1164 host->
mtd.name =
"nand";
1171 platform_set_drvdata(pdev, host);
1172 dev_info(&pdev->
dev,
"FSMC NAND driver registration successful\n");
1183 clk_disable_unprepare(host->
clk);
1184 err_clk_prepare_enable:
1196 platform_set_drvdata(pdev,
NULL);
1205 clk_disable_unprepare(host->
clk);
1213 static int fsmc_nand_suspend(
struct device *
dev)
1217 clk_disable_unprepare(host->
clk);
1221 static int fsmc_nand_resume(
struct device *dev)
1225 clk_prepare_enable(host->
clk);
1233 static SIMPLE_DEV_PM_OPS(fsmc_nand_pm_ops, fsmc_nand_suspend, fsmc_nand_resume);
1237 static const struct of_device_id fsmc_nand_id_table[] = {
1245 .remove = fsmc_nand_remove,
1248 .name =
"fsmc-nand",
1251 .pm = &fsmc_nand_pm_ops,
1256 static int __init fsmc_nand_init(
void)
1263 static void __exit fsmc_nand_exit(
void)