staging: rtl8192u: ieee80211: Do not export static function
[linux-2.6-block.git] / drivers / staging / mt29f_spinand / mt29f_spinand.c
CommitLineData
d974ce4f
KP
1/*
2 * Copyright (c) 2003-2013 Broadcom Corporation
3 *
4 * Copyright (c) 2009-2010 Micron Technology, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/module.h>
18#include <linux/delay.h>
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/partitions.h>
21#include <linux/mtd/nand.h>
22#include <linux/spi/spi.h>
23
24#include "mt29f_spinand.h"
25
26#define BUFSIZE (10 * 64 * 2048)
27#define CACHE_BUF 2112
28/*
29 * OOB area specification layout: Total 32 available free bytes.
30 */
d974ce4f
KP
31
32static inline struct spinand_state *mtd_to_state(struct mtd_info *mtd)
33{
34 struct nand_chip *chip = (struct nand_chip *)mtd->priv;
35 struct spinand_info *info = (struct spinand_info *)chip->priv;
36 struct spinand_state *state = (struct spinand_state *)info->priv;
37
38 return state;
39}
40
3685ebc4
RD
41#ifdef CONFIG_MTD_SPINAND_ONDIEECC
42static int enable_hw_ecc;
43static int enable_read_hw_ecc;
44
d974ce4f
KP
45static struct nand_ecclayout spinand_oob_64 = {
46 .eccbytes = 24,
47 .eccpos = {
48 1, 2, 3, 4, 5, 6,
49 17, 18, 19, 20, 21, 22,
50 33, 34, 35, 36, 37, 38,
51 49, 50, 51, 52, 53, 54, },
52 .oobavail = 32,
53 .oobfree = {
54 {.offset = 8,
55 .length = 8},
56 {.offset = 24,
57 .length = 8},
58 {.offset = 40,
59 .length = 8},
60 {.offset = 56,
61 .length = 8},
62 }
63};
64#endif
65
66/*
67 * spinand_cmd - to process a command to send to the SPI Nand
68 * Description:
69 * Set up the command buffer to send to the SPI controller.
70 * The command buffer has to initialized to 0.
71 */
72
73static int spinand_cmd(struct spi_device *spi, struct spinand_cmd *cmd)
74{
75 struct spi_message message;
76 struct spi_transfer x[4];
77 u8 dummy = 0xff;
78
79 spi_message_init(&message);
80 memset(x, 0, sizeof(x));
81
82 x[0].len = 1;
83 x[0].tx_buf = &cmd->cmd;
84 spi_message_add_tail(&x[0], &message);
85
86 if (cmd->n_addr) {
87 x[1].len = cmd->n_addr;
88 x[1].tx_buf = cmd->addr;
89 spi_message_add_tail(&x[1], &message);
90 }
91
92 if (cmd->n_dummy) {
93 x[2].len = cmd->n_dummy;
94 x[2].tx_buf = &dummy;
95 spi_message_add_tail(&x[2], &message);
96 }
97
98 if (cmd->n_tx) {
99 x[3].len = cmd->n_tx;
100 x[3].tx_buf = cmd->tx_buf;
101 spi_message_add_tail(&x[3], &message);
102 }
103
104 if (cmd->n_rx) {
105 x[3].len = cmd->n_rx;
106 x[3].rx_buf = cmd->rx_buf;
107 spi_message_add_tail(&x[3], &message);
108 }
109
110 return spi_sync(spi, &message);
111}
112
113/*
114 * spinand_read_id- Read SPI Nand ID
115 * Description:
116 * Read ID: read two ID bytes from the SPI Nand device
117 */
118static int spinand_read_id(struct spi_device *spi_nand, u8 *id)
119{
120 int retval;
121 u8 nand_id[3];
122 struct spinand_cmd cmd = {0};
123
124 cmd.cmd = CMD_READ_ID;
125 cmd.n_rx = 3;
126 cmd.rx_buf = &nand_id[0];
127
128 retval = spinand_cmd(spi_nand, &cmd);
129 if (retval < 0) {
130 dev_err(&spi_nand->dev, "error %d reading id\n", retval);
131 return retval;
132 }
133 id[0] = nand_id[1];
134 id[1] = nand_id[2];
135 return retval;
136}
137
138/*
139 * spinand_read_status- send command 0xf to the SPI Nand status register
140 * Description:
141 * After read, write, or erase, the Nand device is expected to set the
142 * busy status.
143 * This function is to allow reading the status of the command: read,
144 * write, and erase.
145 * Once the status turns to be ready, the other status bits also are
146 * valid status bits.
147 */
74f63bd6 148static int spinand_read_status(struct spi_device *spi_nand, u8 *status)
d974ce4f
KP
149{
150 struct spinand_cmd cmd = {0};
151 int ret;
152
153 cmd.cmd = CMD_READ_REG;
154 cmd.n_addr = 1;
155 cmd.addr[0] = REG_STATUS;
156 cmd.n_rx = 1;
157 cmd.rx_buf = status;
158
159 ret = spinand_cmd(spi_nand, &cmd);
160 if (ret < 0)
161 dev_err(&spi_nand->dev, "err: %d read status register\n", ret);
162
163 return ret;
164}
165
166#define MAX_WAIT_JIFFIES (40 * HZ)
167static int wait_till_ready(struct spi_device *spi_nand)
168{
169 unsigned long deadline;
170 int retval;
171 u8 stat = 0;
172
173 deadline = jiffies + MAX_WAIT_JIFFIES;
174 do {
175 retval = spinand_read_status(spi_nand, &stat);
176 if (retval < 0)
177 return -1;
178 else if (!(stat & 0x1))
179 break;
180
181 cond_resched();
182 } while (!time_after_eq(jiffies, deadline));
183
184 if ((stat & 0x1) == 0)
185 return 0;
186
187 return -1;
188}
522f0050 189
d974ce4f
KP
190/**
191 * spinand_get_otp- send command 0xf to read the SPI Nand OTP register
192 * Description:
193 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
194 * Enable chip internal ECC, set the bit to 1
195 * Disable chip internal ECC, clear the bit to 0
196 */
197static int spinand_get_otp(struct spi_device *spi_nand, u8 *otp)
198{
199 struct spinand_cmd cmd = {0};
200 int retval;
201
202 cmd.cmd = CMD_READ_REG;
203 cmd.n_addr = 1;
204 cmd.addr[0] = REG_OTP;
205 cmd.n_rx = 1;
206 cmd.rx_buf = otp;
207
208 retval = spinand_cmd(spi_nand, &cmd);
209 if (retval < 0)
210 dev_err(&spi_nand->dev, "error %d get otp\n", retval);
211 return retval;
212}
213
214/**
215 * spinand_set_otp- send command 0x1f to write the SPI Nand OTP register
216 * Description:
217 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
218 * Enable chip internal ECC, set the bit to 1
219 * Disable chip internal ECC, clear the bit to 0
220 */
221static int spinand_set_otp(struct spi_device *spi_nand, u8 *otp)
222{
223 int retval;
224 struct spinand_cmd cmd = {0};
225
226 cmd.cmd = CMD_WRITE_REG,
227 cmd.n_addr = 1,
228 cmd.addr[0] = REG_OTP,
229 cmd.n_tx = 1,
230 cmd.tx_buf = otp,
231
232 retval = spinand_cmd(spi_nand, &cmd);
233 if (retval < 0)
234 dev_err(&spi_nand->dev, "error %d set otp\n", retval);
235
236 return retval;
237}
238
239#ifdef CONFIG_MTD_SPINAND_ONDIEECC
240/**
241 * spinand_enable_ecc- send command 0x1f to write the SPI Nand OTP register
242 * Description:
243 * There is one bit( bit 0x10 ) to set or to clear the internal ECC.
244 * Enable chip internal ECC, set the bit to 1
245 * Disable chip internal ECC, clear the bit to 0
246 */
247static int spinand_enable_ecc(struct spi_device *spi_nand)
248{
249 int retval;
250 u8 otp = 0;
251
252 retval = spinand_get_otp(spi_nand, &otp);
253 if (retval < 0)
254 return retval;
255
72267c27 256 if ((otp & OTP_ECC_MASK) == OTP_ECC_MASK)
d974ce4f 257 return 0;
72267c27 258 otp |= OTP_ECC_MASK;
259 retval = spinand_set_otp(spi_nand, &otp);
260 if (retval < 0)
261 return retval;
262 return spinand_get_otp(spi_nand, &otp);
d974ce4f
KP
263}
264#endif
265
266static int spinand_disable_ecc(struct spi_device *spi_nand)
267{
268 int retval;
269 u8 otp = 0;
270
271 retval = spinand_get_otp(spi_nand, &otp);
272 if (retval < 0)
273 return retval;
274
275 if ((otp & OTP_ECC_MASK) == OTP_ECC_MASK) {
276 otp &= ~OTP_ECC_MASK;
277 retval = spinand_set_otp(spi_nand, &otp);
278 if (retval < 0)
279 return retval;
280 return spinand_get_otp(spi_nand, &otp);
72267c27 281 }
282 return 0;
d974ce4f
KP
283}
284
285/**
286 * spinand_write_enable- send command 0x06 to enable write or erase the
287 * Nand cells
288 * Description:
289 * Before write and erase the Nand cells, the write enable has to be set.
290 * After the write or erase, the write enable bit is automatically
291 * cleared (status register bit 2)
292 * Set the bit 2 of the status register has the same effect
293 */
294static int spinand_write_enable(struct spi_device *spi_nand)
295{
296 struct spinand_cmd cmd = {0};
297
298 cmd.cmd = CMD_WR_ENABLE;
299 return spinand_cmd(spi_nand, &cmd);
300}
301
302static int spinand_read_page_to_cache(struct spi_device *spi_nand, u16 page_id)
303{
304 struct spinand_cmd cmd = {0};
305 u16 row;
306
307 row = page_id;
308 cmd.cmd = CMD_READ;
309 cmd.n_addr = 3;
310 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
311 cmd.addr[2] = (u8)(row & 0x00ff);
312
313 return spinand_cmd(spi_nand, &cmd);
314}
315
316/*
317 * spinand_read_from_cache- send command 0x03 to read out the data from the
318 * cache register(2112 bytes max)
319 * Description:
320 * The read can specify 1 to 2112 bytes of data read at the corresponding
321 * locations.
322 * No tRd delay.
323 */
324static int spinand_read_from_cache(struct spi_device *spi_nand, u16 page_id,
325 u16 byte_id, u16 len, u8 *rbuf)
326{
327 struct spinand_cmd cmd = {0};
328 u16 column;
329
330 column = byte_id;
331 cmd.cmd = CMD_READ_RDM;
332 cmd.n_addr = 3;
333 cmd.addr[0] = (u8)((column & 0xff00) >> 8);
334 cmd.addr[0] |= (u8)(((page_id >> 6) & 0x1) << 4);
335 cmd.addr[1] = (u8)(column & 0x00ff);
336 cmd.addr[2] = (u8)(0xff);
337 cmd.n_dummy = 0;
338 cmd.n_rx = len;
339 cmd.rx_buf = rbuf;
340
341 return spinand_cmd(spi_nand, &cmd);
342}
343
344/*
345 * spinand_read_page-to read a page with:
346 * @page_id: the physical page number
347 * @offset: the location from 0 to 2111
348 * @len: number of bytes to read
349 * @rbuf: read buffer to hold @len bytes
350 *
351 * Description:
352 * The read includes two commands to the Nand: 0x13 and 0x03 commands
353 * Poll to read status to wait for tRD time.
354 */
355static int spinand_read_page(struct spi_device *spi_nand, u16 page_id,
356 u16 offset, u16 len, u8 *rbuf)
357{
358 int ret;
359 u8 status = 0;
360
361#ifdef CONFIG_MTD_SPINAND_ONDIEECC
362 if (enable_read_hw_ecc) {
363 if (spinand_enable_ecc(spi_nand) < 0)
364 dev_err(&spi_nand->dev, "enable HW ECC failed!");
365 }
366#endif
367 ret = spinand_read_page_to_cache(spi_nand, page_id);
368 if (ret < 0)
369 return ret;
370
371 if (wait_till_ready(spi_nand))
372 dev_err(&spi_nand->dev, "WAIT timedout!!!\n");
373
374 while (1) {
375 ret = spinand_read_status(spi_nand, &status);
376 if (ret < 0) {
377 dev_err(&spi_nand->dev,
378 "err %d read status register\n", ret);
379 return ret;
380 }
381
382 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
383 if ((status & STATUS_ECC_MASK) == STATUS_ECC_ERROR) {
384 dev_err(&spi_nand->dev, "ecc error, page=%d\n",
385 page_id);
386 return 0;
387 }
388 break;
389 }
390 }
391
392 ret = spinand_read_from_cache(spi_nand, page_id, offset, len, rbuf);
393 if (ret < 0) {
394 dev_err(&spi_nand->dev, "read from cache failed!!\n");
395 return ret;
396 }
397
398#ifdef CONFIG_MTD_SPINAND_ONDIEECC
399 if (enable_read_hw_ecc) {
400 ret = spinand_disable_ecc(spi_nand);
401 if (ret < 0) {
402 dev_err(&spi_nand->dev, "disable ecc failed!!\n");
403 return ret;
404 }
405 enable_read_hw_ecc = 0;
406 }
407#endif
408 return ret;
409}
410
411/*
412 * spinand_program_data_to_cache--to write a page to cache with:
413 * @byte_id: the location to write to the cache
414 * @len: number of bytes to write
415 * @rbuf: read buffer to hold @len bytes
416 *
417 * Description:
418 * The write command used here is 0x84--indicating that the cache is
419 * not cleared first.
420 * Since it is writing the data to cache, there is no tPROG time.
421 */
422static int spinand_program_data_to_cache(struct spi_device *spi_nand,
423 u16 page_id, u16 byte_id, u16 len, u8 *wbuf)
424{
425 struct spinand_cmd cmd = {0};
426 u16 column;
427
428 column = byte_id;
429 cmd.cmd = CMD_PROG_PAGE_CLRCACHE;
430 cmd.n_addr = 2;
431 cmd.addr[0] = (u8)((column & 0xff00) >> 8);
432 cmd.addr[0] |= (u8)(((page_id >> 6) & 0x1) << 4);
433 cmd.addr[1] = (u8)(column & 0x00ff);
434 cmd.n_tx = len;
435 cmd.tx_buf = wbuf;
436
437 return spinand_cmd(spi_nand, &cmd);
438}
439
440/**
441 * spinand_program_execute--to write a page from cache to the Nand array with
442 * @page_id: the physical page location to write the page.
443 *
444 * Description:
445 * The write command used here is 0x10--indicating the cache is writing to
446 * the Nand array.
447 * Need to wait for tPROG time to finish the transaction.
448 */
449static int spinand_program_execute(struct spi_device *spi_nand, u16 page_id)
450{
451 struct spinand_cmd cmd = {0};
452 u16 row;
453
454 row = page_id;
455 cmd.cmd = CMD_PROG_PAGE_EXC;
456 cmd.n_addr = 3;
457 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
458 cmd.addr[2] = (u8)(row & 0x00ff);
459
460 return spinand_cmd(spi_nand, &cmd);
461}
462
463/**
464 * spinand_program_page--to write a page with:
465 * @page_id: the physical page location to write the page.
466 * @offset: the location from the cache starting from 0 to 2111
467 * @len: the number of bytes to write
468 * @wbuf: the buffer to hold the number of bytes
469 *
470 * Description:
471 * The commands used here are 0x06, 0x84, and 0x10--indicating that
472 * the write enable is first sent, the write cache command, and the
473 * write execute command.
474 * Poll to wait for the tPROG time to finish the transaction.
475 */
476static int spinand_program_page(struct spi_device *spi_nand,
477 u16 page_id, u16 offset, u16 len, u8 *buf)
478{
479 int retval;
480 u8 status = 0;
74f63bd6 481 u8 *wbuf;
d974ce4f
KP
482#ifdef CONFIG_MTD_SPINAND_ONDIEECC
483 unsigned int i, j;
484
485 enable_read_hw_ecc = 0;
486 wbuf = devm_kzalloc(&spi_nand->dev, CACHE_BUF, GFP_KERNEL);
487 spinand_read_page(spi_nand, page_id, 0, CACHE_BUF, wbuf);
488
489 for (i = offset, j = 0; i < len; i++, j++)
490 wbuf[i] &= buf[j];
491
492 if (enable_hw_ecc) {
493 retval = spinand_enable_ecc(spi_nand);
494 if (retval < 0) {
495 dev_err(&spi_nand->dev, "enable ecc failed!!\n");
496 return retval;
497 }
498 }
499#else
500 wbuf = buf;
501#endif
502 retval = spinand_write_enable(spi_nand);
503 if (retval < 0) {
504 dev_err(&spi_nand->dev, "write enable failed!!\n");
505 return retval;
506 }
507 if (wait_till_ready(spi_nand))
508 dev_err(&spi_nand->dev, "wait timedout!!!\n");
509
510 retval = spinand_program_data_to_cache(spi_nand, page_id,
511 offset, len, wbuf);
512 if (retval < 0)
513 return retval;
514 retval = spinand_program_execute(spi_nand, page_id);
515 if (retval < 0)
516 return retval;
517 while (1) {
518 retval = spinand_read_status(spi_nand, &status);
519 if (retval < 0) {
520 dev_err(&spi_nand->dev,
521 "error %d reading status register\n",
522 retval);
523 return retval;
524 }
525
526 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
527 if ((status & STATUS_P_FAIL_MASK) == STATUS_P_FAIL) {
528 dev_err(&spi_nand->dev,
529 "program error, page %d\n", page_id);
530 return -1;
72267c27 531 }
532 break;
d974ce4f
KP
533 }
534 }
535#ifdef CONFIG_MTD_SPINAND_ONDIEECC
536 if (enable_hw_ecc) {
537 retval = spinand_disable_ecc(spi_nand);
538 if (retval < 0) {
539 dev_err(&spi_nand->dev, "disable ecc failed!!\n");
540 return retval;
541 }
542 enable_hw_ecc = 0;
543 }
544#endif
545
546 return 0;
547}
548
549/**
550 * spinand_erase_block_erase--to erase a page with:
551 * @block_id: the physical block location to erase.
552 *
553 * Description:
554 * The command used here is 0xd8--indicating an erase command to erase
555 * one block--64 pages
556 * Need to wait for tERS.
557 */
558static int spinand_erase_block_erase(struct spi_device *spi_nand, u16 block_id)
559{
560 struct spinand_cmd cmd = {0};
561 u16 row;
562
563 row = block_id;
564 cmd.cmd = CMD_ERASE_BLK;
565 cmd.n_addr = 3;
566 cmd.addr[1] = (u8)((row & 0xff00) >> 8);
567 cmd.addr[2] = (u8)(row & 0x00ff);
568
569 return spinand_cmd(spi_nand, &cmd);
570}
571
572/**
573 * spinand_erase_block--to erase a page with:
574 * @block_id: the physical block location to erase.
575 *
576 * Description:
577 * The commands used here are 0x06 and 0xd8--indicating an erase
578 * command to erase one block--64 pages
579 * It will first to enable the write enable bit (0x06 command),
580 * and then send the 0xd8 erase command
581 * Poll to wait for the tERS time to complete the tranaction.
582 */
583static int spinand_erase_block(struct spi_device *spi_nand, u16 block_id)
584{
585 int retval;
586 u8 status = 0;
587
588 retval = spinand_write_enable(spi_nand);
589 if (wait_till_ready(spi_nand))
590 dev_err(&spi_nand->dev, "wait timedout!!!\n");
591
592 retval = spinand_erase_block_erase(spi_nand, block_id);
593 while (1) {
594 retval = spinand_read_status(spi_nand, &status);
595 if (retval < 0) {
596 dev_err(&spi_nand->dev,
597 "error %d reading status register\n",
4cd1a64a 598 retval);
d974ce4f
KP
599 return retval;
600 }
601
602 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
603 if ((status & STATUS_E_FAIL_MASK) == STATUS_E_FAIL) {
604 dev_err(&spi_nand->dev,
605 "erase error, block %d\n", block_id);
606 return -1;
72267c27 607 }
608 break;
d974ce4f
KP
609 }
610 }
611 return 0;
612}
613
614#ifdef CONFIG_MTD_SPINAND_ONDIEECC
615static int spinand_write_page_hwecc(struct mtd_info *mtd,
74f63bd6 616 struct nand_chip *chip, const u8 *buf, int oob_required)
d974ce4f 617{
74f63bd6 618 const u8 *p = buf;
d974ce4f
KP
619 int eccsize = chip->ecc.size;
620 int eccsteps = chip->ecc.steps;
621
622 enable_hw_ecc = 1;
623 chip->write_buf(mtd, p, eccsize * eccsteps);
624 return 0;
625}
626
627static int spinand_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
74f63bd6 628 u8 *buf, int oob_required, int page)
d974ce4f 629{
1f5cf802
DN
630 int retval;
631 u8 status;
74f63bd6 632 u8 *p = buf;
d974ce4f
KP
633 int eccsize = chip->ecc.size;
634 int eccsteps = chip->ecc.steps;
635 struct spinand_info *info = (struct spinand_info *)chip->priv;
636
637 enable_read_hw_ecc = 1;
638
639 chip->read_buf(mtd, p, eccsize * eccsteps);
640 if (oob_required)
641 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
642
643 while (1) {
644 retval = spinand_read_status(info->spi, &status);
1f5cf802
DN
645 if (retval < 0) {
646 dev_err(&mtd->dev,
647 "error %d reading status register\n",
648 retval);
649 return retval;
650 }
651
d974ce4f
KP
652 if ((status & STATUS_OIP_MASK) == STATUS_READY) {
653 if ((status & STATUS_ECC_MASK) == STATUS_ECC_ERROR) {
654 pr_info("spinand: ECC error\n");
655 mtd->ecc_stats.failed++;
656 } else if ((status & STATUS_ECC_MASK) ==
657 STATUS_ECC_1BIT_CORRECTED)
658 mtd->ecc_stats.corrected++;
659 break;
660 }
661 }
662 return 0;
d974ce4f
KP
663}
664#endif
665
666static void spinand_select_chip(struct mtd_info *mtd, int dev)
667{
668}
669
74f63bd6 670static u8 spinand_read_byte(struct mtd_info *mtd)
d974ce4f
KP
671{
672 struct spinand_state *state = mtd_to_state(mtd);
673 u8 data;
674
675 data = state->buf[state->buf_ptr];
676 state->buf_ptr++;
677 return data;
678}
679
d974ce4f
KP
680static int spinand_wait(struct mtd_info *mtd, struct nand_chip *chip)
681{
682 struct spinand_info *info = (struct spinand_info *)chip->priv;
683
684 unsigned long timeo = jiffies;
685 int retval, state = chip->state;
686 u8 status;
687
688 if (state == FL_ERASING)
689 timeo += (HZ * 400) / 1000;
690 else
691 timeo += (HZ * 20) / 1000;
692
693 while (time_before(jiffies, timeo)) {
694 retval = spinand_read_status(info->spi, &status);
1f5cf802
DN
695 if (retval < 0) {
696 dev_err(&mtd->dev,
697 "error %d reading status register\n",
698 retval);
699 return retval;
700 }
701
d974ce4f
KP
702 if ((status & STATUS_OIP_MASK) == STATUS_READY)
703 return 0;
704
705 cond_resched();
706 }
707 return 0;
708}
709
74f63bd6 710static void spinand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
d974ce4f 711{
d974ce4f 712 struct spinand_state *state = mtd_to_state(mtd);
aeb49155 713
d974ce4f
KP
714 memcpy(state->buf + state->buf_ptr, buf, len);
715 state->buf_ptr += len;
716}
717
74f63bd6 718static void spinand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
d974ce4f
KP
719{
720 struct spinand_state *state = mtd_to_state(mtd);
aeb49155 721
d974ce4f
KP
722 memcpy(buf, state->buf + state->buf_ptr, len);
723 state->buf_ptr += len;
724}
725
726/*
727 * spinand_reset- send RESET command "0xff" to the Nand device.
728 */
729static void spinand_reset(struct spi_device *spi_nand)
730{
731 struct spinand_cmd cmd = {0};
732
733 cmd.cmd = CMD_RESET;
734
735 if (spinand_cmd(spi_nand, &cmd) < 0)
736 pr_info("spinand reset failed!\n");
737
738 /* elapse 1ms before issuing any other command */
c75e0577 739 usleep_range(1000, 2000);
d974ce4f
KP
740
741 if (wait_till_ready(spi_nand))
742 dev_err(&spi_nand->dev, "wait timedout!\n");
743}
744
745static void spinand_cmdfunc(struct mtd_info *mtd, unsigned int command,
746 int column, int page)
747{
748 struct nand_chip *chip = (struct nand_chip *)mtd->priv;
749 struct spinand_info *info = (struct spinand_info *)chip->priv;
750 struct spinand_state *state = (struct spinand_state *)info->priv;
751
752 switch (command) {
753 /*
754 * READ0 - read in first 0x800 bytes
755 */
756 case NAND_CMD_READ1:
757 case NAND_CMD_READ0:
758 state->buf_ptr = 0;
759 spinand_read_page(info->spi, page, 0x0, 0x840, state->buf);
760 break;
761 /* READOOB reads only the OOB because no ECC is performed. */
762 case NAND_CMD_READOOB:
763 state->buf_ptr = 0;
764 spinand_read_page(info->spi, page, 0x800, 0x40, state->buf);
765 break;
766 case NAND_CMD_RNDOUT:
767 state->buf_ptr = column;
768 break;
769 case NAND_CMD_READID:
770 state->buf_ptr = 0;
a511536f 771 spinand_read_id(info->spi, state->buf);
d974ce4f
KP
772 break;
773 case NAND_CMD_PARAM:
774 state->buf_ptr = 0;
775 break;
776 /* ERASE1 stores the block and page address */
777 case NAND_CMD_ERASE1:
778 spinand_erase_block(info->spi, page);
779 break;
780 /* ERASE2 uses the block and page address from ERASE1 */
781 case NAND_CMD_ERASE2:
782 break;
783 /* SEQIN sets up the addr buffer and all registers except the length */
784 case NAND_CMD_SEQIN:
785 state->col = column;
786 state->row = page;
787 state->buf_ptr = 0;
788 break;
789 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
790 case NAND_CMD_PAGEPROG:
791 spinand_program_page(info->spi, state->row, state->col,
792 state->buf_ptr, state->buf);
793 break;
794 case NAND_CMD_STATUS:
795 spinand_get_otp(info->spi, state->buf);
796 if (!(state->buf[0] & 0x80))
797 state->buf[0] = 0x80;
798 state->buf_ptr = 0;
799 break;
800 /* RESET command */
801 case NAND_CMD_RESET:
802 if (wait_till_ready(info->spi))
803 dev_err(&info->spi->dev, "WAIT timedout!!!\n");
804 /* a minimum of 250us must elapse before issuing RESET cmd*/
c75e0577 805 usleep_range(250, 1000);
d974ce4f
KP
806 spinand_reset(info->spi);
807 break;
808 default:
809 dev_err(&mtd->dev, "Unknown CMD: 0x%x\n", command);
810 }
811}
812
813/**
814 * spinand_lock_block- send write register 0x1f command to the Nand device
815 *
816 * Description:
817 * After power up, all the Nand blocks are locked. This function allows
818 * one to unlock the blocks, and so it can be written or erased.
819 */
820static int spinand_lock_block(struct spi_device *spi_nand, u8 lock)
821{
822 struct spinand_cmd cmd = {0};
823 int ret;
824 u8 otp = 0;
825
826 ret = spinand_get_otp(spi_nand, &otp);
827
828 cmd.cmd = CMD_WRITE_REG;
829 cmd.n_addr = 1;
830 cmd.addr[0] = REG_BLOCK_LOCK;
831 cmd.n_tx = 1;
832 cmd.tx_buf = &lock;
833
834 ret = spinand_cmd(spi_nand, &cmd);
835 if (ret < 0)
836 dev_err(&spi_nand->dev, "error %d lock block\n", ret);
837
838 return ret;
839}
522f0050 840
d974ce4f
KP
841/*
842 * spinand_probe - [spinand Interface]
843 * @spi_nand: registered device driver.
844 *
845 * Description:
846 * To set up the device driver parameters to make the device available.
847 */
848static int spinand_probe(struct spi_device *spi_nand)
849{
850 struct mtd_info *mtd;
851 struct nand_chip *chip;
852 struct spinand_info *info;
853 struct spinand_state *state;
854 struct mtd_part_parser_data ppdata;
855
856 info = devm_kzalloc(&spi_nand->dev, sizeof(struct spinand_info),
857 GFP_KERNEL);
858 if (!info)
859 return -ENOMEM;
860
861 info->spi = spi_nand;
862
863 spinand_lock_block(spi_nand, BL_ALL_UNLOCKED);
864
865 state = devm_kzalloc(&spi_nand->dev, sizeof(struct spinand_state),
866 GFP_KERNEL);
867 if (!state)
868 return -ENOMEM;
869
870 info->priv = state;
871 state->buf_ptr = 0;
872 state->buf = devm_kzalloc(&spi_nand->dev, BUFSIZE, GFP_KERNEL);
873 if (!state->buf)
874 return -ENOMEM;
875
876 chip = devm_kzalloc(&spi_nand->dev, sizeof(struct nand_chip),
877 GFP_KERNEL);
878 if (!chip)
879 return -ENOMEM;
880
881#ifdef CONFIG_MTD_SPINAND_ONDIEECC
882 chip->ecc.mode = NAND_ECC_HW;
883 chip->ecc.size = 0x200;
884 chip->ecc.bytes = 0x6;
885 chip->ecc.steps = 0x4;
886
887 chip->ecc.strength = 1;
888 chip->ecc.total = chip->ecc.steps * chip->ecc.bytes;
889 chip->ecc.layout = &spinand_oob_64;
890 chip->ecc.read_page = spinand_read_page_hwecc;
891 chip->ecc.write_page = spinand_write_page_hwecc;
892#else
893 chip->ecc.mode = NAND_ECC_SOFT;
894 if (spinand_disable_ecc(spi_nand) < 0)
895 pr_info("%s: disable ecc failed!\n", __func__);
896#endif
897
898 chip->priv = info;
899 chip->read_buf = spinand_read_buf;
900 chip->write_buf = spinand_write_buf;
901 chip->read_byte = spinand_read_byte;
902 chip->cmdfunc = spinand_cmdfunc;
903 chip->waitfunc = spinand_wait;
904 chip->options |= NAND_CACHEPRG;
905 chip->select_chip = spinand_select_chip;
906
907 mtd = devm_kzalloc(&spi_nand->dev, sizeof(struct mtd_info), GFP_KERNEL);
908 if (!mtd)
909 return -ENOMEM;
910
911 dev_set_drvdata(&spi_nand->dev, mtd);
912
913 mtd->priv = chip;
914 mtd->name = dev_name(&spi_nand->dev);
915 mtd->owner = THIS_MODULE;
916 mtd->oobsize = 64;
917
918 if (nand_scan(mtd, 1))
919 return -ENXIO;
920
921 ppdata.of_node = spi_nand->dev.of_node;
922 return mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
923}
924
925/*
926 * spinand_remove: Remove the device driver
927 * @spi: the spi device.
928 *
929 * Description:
930 * To remove the device driver parameters and free up allocated memories.
931 */
932static int spinand_remove(struct spi_device *spi)
933{
934 mtd_device_unregister(dev_get_drvdata(&spi->dev));
935
936 return 0;
937}
938
939static const struct of_device_id spinand_dt[] = {
940 { .compatible = "spinand,mt29f", },
ffd07de6 941 {}
d974ce4f 942};
1e498d4b 943MODULE_DEVICE_TABLE(of, spinand_dt);
d974ce4f
KP
944
945/*
946 * Device name structure description
947 */
948static struct spi_driver spinand_driver = {
949 .driver = {
950 .name = "mt29f",
d974ce4f
KP
951 .owner = THIS_MODULE,
952 .of_match_table = spinand_dt,
953 },
954 .probe = spinand_probe,
955 .remove = spinand_remove,
956};
957
4a54b615 958module_spi_driver(spinand_driver);
d974ce4f
KP
959
960MODULE_DESCRIPTION("SPI NAND driver for Micron");
961MODULE_AUTHOR("Henry Pan <hspan@micron.com>, Kamlakant Patel <kamlakant.patel@broadcom.com>");
962MODULE_LICENSE("GPL v2");