2 * Micron SPI-ER NAND Flash Memory
3 * This code uses the built in Ubicom flash controller
5 * (C) Copyright 2009, Ubicom, Inc.
7 * This file is part of the Ubicom32 Linux Kernel Port.
9 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10 * it and/or modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation, either version 2 of the
12 * License, or (at your option) any later version.
14 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with the Ubicom32 Linux Kernel Port. If not,
21 * see <http://www.gnu.org/licenses/>.
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/device.h>
28 #include <linux/platform_device.h>
29 #include <linux/mutex.h>
30 #include <linux/err.h>
32 #include <linux/mtd/mtd.h>
33 #include <linux/mtd/partitions.h>
35 #define DRIVER_NAME "ubi32-nand-spi-er"
36 #define UBI32_NAND_SPI_ER_BLOCK_FROM_ROW(row) (row >> 6)
38 #define UBI32_NAND_SPI_ER_STATUS_P_FAIL (1 << 3)
39 #define UBI32_NAND_SPI_ER_STATUS_E_FAIL (1 << 2)
40 #define UBI32_NAND_SPI_ER_STATUS_OIP (1 << 0)
42 #define UBI32_NAND_SPI_ER_LAST_ROW_INVALID 0xFFFFFFFF
43 #define UBI32_NAND_SPI_ER_BAD_BLOCK_MARK_OFFSET 0x08
45 struct ubi32_nand_spi_er_device
{
51 unsigned int pages_per_block
;
52 unsigned int page_size
;
53 unsigned int write_size
;
54 unsigned int erase_size
;
57 struct ubi32_nand_spi_er
{
60 const struct ubi32_nand_spi_er_device
*device
;
63 struct platform_device
*pdev
;
67 unsigned int last_row
; /* the last row we fetched */
70 * Bad block table (MUST be last in strcuture)
77 * Chip supports a write_size of 512, but we cannot do partial
78 * page with command 0x84.
80 * We need to use command 0x84 because we cannot fill the FIFO fast
81 * enough to transfer the whole 512 bytes at a time. (maybe through
84 const struct ubi32_nand_spi_er_device ubi32_nand_spi_er_devices
[] = {
92 erase_size
: 64 * 2048,
101 erase_size
: 64 * 2048,
105 static int read_only
= 0;
106 module_param(read_only
, int, 0);
107 MODULE_PARM_DESC(read_only
, "Leave device locked");
110 * Ubicom32 FLASH Command Set
112 #define FLASH_PORT RA
114 #define FLASH_FC_INST_CMD 0x00 /* for SPI command only transaction */
115 #define FLASH_FC_INST_WR 0x01 /* for SPI write transaction */
116 #define FLASH_FC_INST_RD 0x02 /* for SPI read transaction */
118 #define FLASH_COMMAND_KICK_OFF(io) \
120 " bset "D(IO_INT_CLR)"(%0), #0, #%%bit("D(IO_XFL_INT_DONE)") \n\t" \
122 " bset "D(IO_INT_SET)"(%0), #0, #%%bit("D(IO_XFL_INT_START)") \n\t" \
128 #define FLASH_COMMAND_WAIT_FOR_COMPLETION(io) \
130 " btst "D(IO_INT_STATUS)"(%0), #%%bit("D(IO_XFL_INT_DONE)") \n\t" \
131 " jmpeq.f .-4 \n\t" \
137 #define FLASH_COMMAND_EXEC(io) \
138 FLASH_COMMAND_KICK_OFF(io) \
139 FLASH_COMMAND_WAIT_FOR_COMPLETION(io)
142 * ubi32_nand_spi_er_get_feature
143 * Get Feature register
145 static uint8_t ubi32_nand_spi_er_get_feature(uint32_t reg
)
147 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
150 * Note that this will produce the sequence:
151 * SI [0F][REG][00][00]
152 * SO ---------[SR][SR][SR]
153 * Since the flash controller can only output 24 bits of address, this is
154 * ok for this command since the data will just repeat as long as the CS
155 * is asserted and the clock is running.
157 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
158 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_RD
) | IO_XFL_CTL1_FC_DATA(1) |
160 io
->ctl2
= IO_XFL_CTL2_FC_CMD(0x0F) | IO_XFL_CTL2_FC_ADDR(reg
<< 16);
161 FLASH_COMMAND_EXEC(io
);
163 return io
->status1
& 0xFF;
167 * ubi32_nand_spi_er_write_buf
168 * writes a buffer to the bus
170 * Writes 511 + 1 bytes to the bus, we have to stuff one data byte into the address.
172 static void ubi32_nand_spi_er_write_buf(const uint8_t *buf
, uint32_t col
)
174 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
178 " bset "D(IO_INT_SET
)"(%[port]), #0, #%%bit("D(IO_PORTX_INT_FIFO_TX_RESET
)") \n\t"
181 : [port
] "a" (FLASH_PORT
)
186 * Write the data into the cache
188 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
189 #ifdef SUPPORT_512_FIFO
190 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_WR
) | IO_XFL_CTL1_FC_DATA(511) |
192 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_WR
) | IO_XFL_CTL1_FC_DATA(31) |
196 * Construct the address with the first byte of data
198 tmp
= (col
<< 8) | *buf
++;
199 io
->ctl2
= IO_XFL_CTL2_FC_CMD(0x84) | IO_XFL_CTL2_FC_ADDR(tmp
);
206 * The first word needs to be [11][22][33][33] to work around a flash
209 " move.2 %[tmp], (%[data])2++ \n\t"
210 " shmrg.1 %[tmp], (%[data]), %[tmp] \n\t"
211 " shmrg.1 %[tmp], (%[data])1++, %[tmp] \n\t"
212 " move.4 "D(IO_TX_FIFO
)"(%[port]), %[tmp] \n\t"
215 * We're aligned again!
218 " move.4 "D(IO_TX_FIFO
)"(%[port]), (%[data])4++ \n\t"
222 * Kick off the flash command
224 " bset "D(IO_INT_CLR
)"(%[port]), #0, #%%bit("D(IO_XFL_INT_DONE
)") \n\t"
226 " bset "D(IO_INT_SET
)"(%[port]), #0, #%%bit("D(IO_XFL_INT_START
)") \n\t"
228 #ifdef SUPPORT_512_FIFO
230 * Fill the remaining 120 words as space becomes available
233 " cmpi "D(IO_FIFO_LEVEL
)"(%[port]), #4 \n\t"
235 " move.4 "D(IO_TX_FIFO
)"(%[port]), (%[data])4++ \n\t"
236 " move.4 "D(IO_TX_FIFO
)"(%[port]), (%[data])4++ \n\t"
237 " move.4 "D(IO_TX_FIFO
)"(%[port]), (%[data])4++ \n\t"
238 " move.4 "D(IO_TX_FIFO
)"(%[port]), (%[data])4++ \n\t"
239 " add.4 %[cnt], #-4, %[cnt] \n\t"
243 * Wait for the transaction to finish
245 " btst "D(IO_INT_STATUS
)"(%[port]), #%%bit("D(IO_XFL_INT_DONE
)") \n\t"
250 : [column
] "d" (col
),
251 [port
] "a" (FLASH_PORT
),
252 [cnt
] "d" (120) // see above comment
258 * ubi32_nand_spi_er_send_rd_addr
259 * perform FC_RD: CMD + address
261 static void ubi32_nand_spi_er_send_rd_addr(uint8_t command
, uint32_t address
)
263 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
265 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
266 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_RD
) | IO_XFL_CTL1_FC_DATA(4) |
268 io
->ctl2
= IO_XFL_CTL2_FC_CMD(command
) | IO_XFL_CTL2_FC_ADDR(address
);
269 FLASH_COMMAND_EXEC(io
);
273 * ubi32_nand_spi_er_send_cmd_addr
274 * perform FC_(xxx): CMD + address
276 static void ubi32_nand_spi_er_send_cmd_addr(uint8_t command
, uint32_t address
)
278 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
280 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
281 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_CMD
) | IO_XFL_CTL1_FC_ADDR
;
282 io
->ctl2
= IO_XFL_CTL2_FC_CMD(command
) | IO_XFL_CTL2_FC_ADDR(address
);
283 FLASH_COMMAND_EXEC(io
);
287 * ubi32_nand_spi_er_write_disable
288 * clear the write enable bit
290 static void ubi32_nand_spi_er_write_disable(void)
292 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
294 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
295 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_CMD
);
296 io
->ctl2
= IO_XFL_CTL2_FC_CMD(0x04);
297 FLASH_COMMAND_EXEC(io
);
301 * ubi32_nand_spi_er_write_enable
302 * set the write enable bit
304 static void ubi32_nand_spi_er_write_enable(void)
306 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
308 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
309 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_CMD
);
310 io
->ctl2
= IO_XFL_CTL2_FC_CMD(0x06);
311 FLASH_COMMAND_EXEC(io
);
315 * ubi32_nand_spi_er_busywait
316 * Wait until the chip is not busy
318 static uint8_t ubi32_nand_spi_er_busywait(void)
324 * tRD is 100us, so don't delay too long, however, tERS is
325 * 10ms so you'd better loop enough.
327 for (i
= 0; i
< 200; i
++) {
328 data
= ubi32_nand_spi_er_get_feature(0xC0);
329 if (!(data
& UBI32_NAND_SPI_ER_STATUS_OIP
)) {
340 * ubi32_nand_spi_er_erase
341 * Erase a block, parameters must be block aligned
343 static int ubi32_nand_spi_er_erase(struct mtd_info
*mtd
, struct erase_info
*instr
)
345 struct ubi32_nand_spi_er
*chip
= mtd
->priv
;
348 DEBUG(MTD_DEBUG_LEVEL3
, "%s: erase addr:%x len:%x\n", chip
->name
, instr
->addr
, instr
->len
);
350 if ((instr
->addr
+ instr
->len
) > mtd
->size
) {
354 if (instr
->addr
& (chip
->device
->erase_size
- 1)) {
355 DEBUG(MTD_DEBUG_LEVEL1
, "%s: erase address is not aligned %x\n", chip
->name
, instr
->addr
);
359 if (instr
->len
& (chip
->device
->erase_size
- 1)) {
360 DEBUG(MTD_DEBUG_LEVEL1
, "%s: erase len is not aligned %x\n", chip
->name
, instr
->len
);
364 mutex_lock(&chip
->lock
);
365 chip
->last_row
= UBI32_NAND_SPI_ER_LAST_ROW_INVALID
;
368 uint32_t block
= instr
->addr
>> 17;
369 uint32_t row
= block
<< 6;
371 DEBUG(MTD_DEBUG_LEVEL3
, "%s: block erase row:%x block:%x addr:%x rem:%x\n", chip
->name
, row
, block
, instr
->addr
, instr
->len
);
376 if (test_bit(block
, chip
->bbt
)) {
377 instr
->fail_addr
= block
<< 17;
378 instr
->state
= MTD_ERASE_FAILED
;
383 ubi32_nand_spi_er_write_enable();
388 ubi32_nand_spi_er_send_cmd_addr(0xD8, row
);
393 stat
= ubi32_nand_spi_er_busywait();
394 if (stat
& UBI32_NAND_SPI_ER_STATUS_OIP
) {
395 instr
->fail_addr
= block
<< 17;
396 instr
->state
= MTD_ERASE_FAILED
;
397 DEBUG(MTD_DEBUG_LEVEL1
, "%s: chip is busy or nonresponsive stat=%02x\n", chip
->name
, stat
);
407 * Check the status register
409 if (stat
& UBI32_NAND_SPI_ER_STATUS_E_FAIL
) {
410 DEBUG(MTD_DEBUG_LEVEL1
, "%s: E_FAIL signalled (%02x)\n", chip
->name
, stat
);
411 instr
->fail_addr
= block
<< 17;
412 instr
->state
= MTD_ERASE_FAILED
;
420 instr
->len
-= chip
->device
->erase_size
;
421 instr
->addr
+= chip
->device
->erase_size
;
424 instr
->state
= MTD_ERASE_DONE
;
426 mutex_unlock(&chip
->lock
);
430 ubi32_nand_spi_er_write_disable();
432 mutex_unlock(&chip
->lock
);
434 mtd_erase_callback(instr
);
439 * ubi32_nand_spi_er_read
441 * return -EUCLEAN: ecc error recovered
442 * return -EBADMSG: ecc error not recovered
444 static int ubi32_nand_spi_er_read(struct mtd_info
*mtd
, loff_t from
, size_t len
,
445 size_t *retlen
, u_char
*buf
)
447 struct ubi32_nand_spi_er
*chip
= mtd
->priv
;
448 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
453 uint32_t *pbuf
= (uint32_t *)buf
;
456 DEBUG(MTD_DEBUG_LEVEL2
, "%s: read block from %llx len %d into %p\n", chip
->name
, from
, len
, buf
);
459 * buf should be aligned
461 if ((uint32_t)buf
& 0x03) {
466 * Zero length reads, nothing to do
473 * Reject reads which go over the end of the flash
475 if ((from
+ len
) > mtd
->size
) {
480 * Get the row and column address to start at
483 column
= from
& 0x7FF;
484 DEBUG(MTD_DEBUG_LEVEL3
, "%s: row=%x %d column=%x %d last_row=%x %d\n", chip
->name
, row
, row
, column
, column
, chip
->last_row
, chip
->last_row
);
487 * Read the data from the chip
489 mutex_lock(&chip
->lock
);
497 * Figure out how much to read
499 * If we are reading from the middle of a page then the most we
500 * can read is to the end of the page
503 if (toread
> (chip
->device
->page_size
- column
)) {
504 toread
= chip
->device
->page_size
- column
;
507 DEBUG(MTD_DEBUG_LEVEL3
, "%s: buf=%p toread=%x row=%x column=%x last_row=%x\n", chip
->name
, pbuf
, toread
, row
, column
, chip
->last_row
);
509 if (chip
->last_row
!= row
) {
511 * Check if the block is bad
513 if (test_bit(UBI32_NAND_SPI_ER_BLOCK_FROM_ROW(row
), chip
->bbt
)) {
514 mutex_unlock(&chip
->lock
);
519 * Load the appropriate page
521 ubi32_nand_spi_er_send_cmd_addr(0x13, row
);
526 stat
= ubi32_nand_spi_er_busywait();
527 if (stat
& UBI32_NAND_SPI_ER_STATUS_OIP
) {
528 DEBUG(MTD_DEBUG_LEVEL1
, "%s: chip is busy or nonresponsive stat=%02x\n", chip
->name
, stat
);
533 mutex_unlock(&chip
->lock
);
542 DEBUG(MTD_DEBUG_LEVEL1
, "%s: ECC recovered, row=%x\n", chip
->name
, row
);
546 DEBUG(MTD_DEBUG_LEVEL0
, "%s: failed ECC, row=%x\n", chip
->name
, row
);
547 chip
->last_row
= UBI32_NAND_SPI_ER_LAST_ROW_INVALID
;
548 mutex_unlock(&chip
->lock
);
554 chip
->last_row
= row
;
558 * We can always read a little too much since there is the
559 * OOB after byte addr 2047. The most we'll overread is 3 bytes.
561 if (((uint32_t)pbuf
& 0x03) == 0) {
565 tmp
= toread
& (~0x03);
566 for (i
= 0; i
< tmp
; i
+= 4) {
567 ubi32_nand_spi_er_send_rd_addr(0x03, column
<< 8);
568 *pbuf
++ = io
->status1
;
575 tmp
= toread
& (~0x03);
576 for (i
= 0; i
< tmp
; i
+= 4) {
577 ubi32_nand_spi_er_send_rd_addr(0x03, column
<< 8);
578 memcpy(pbuf
, &io
->status1
, 4);
584 * Fill in any single bytes
588 uint8_t *bbuf
= pbuf
;
590 ubi32_nand_spi_er_send_rd_addr(0x03, column
<< 8);
592 for (i
= 0; i
< tmp
; i
++) {
602 * For the next page, increment the row and always start at column 0
608 mutex_unlock(&chip
->lock
);
613 * ubi32_nand_spi_er_write
615 #define WRITE_NOT_ALIGNED(x) ((x & (device->write_size - 1)) != 0)
616 static int ubi32_nand_spi_er_write(struct mtd_info
*mtd
, loff_t to
, size_t len
,
617 size_t *retlen
, const u_char
*buf
)
619 struct ubi32_nand_spi_er
*chip
= mtd
->priv
;
620 const struct ubi32_nand_spi_er_device
*device
= chip
->device
;
621 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
627 DEBUG(MTD_DEBUG_LEVEL2
, "%s: write block to %llx len %d from %p\n", chip
->name
, to
, len
, buf
);
639 * Reject writes which go over the end of the flash
641 if ((to
+ len
) > mtd
->size
) {
646 * buf should be aligned to 16 bits
648 if ((uint32_t)buf
& 0x01) {
653 * Check to see if everything is page aligned
655 if (WRITE_NOT_ALIGNED(to
) || WRITE_NOT_ALIGNED(len
)) {
656 printk(KERN_NOTICE
"ubi32_nand_spi_er_write: Attempt to write non page aligned data\n");
660 mutex_lock(&chip
->lock
);
662 io
->ctl0
|= IO_XFL_CTL0_MCB_LOCK
;
664 chip
->last_row
= UBI32_NAND_SPI_ER_LAST_ROW_INVALID
;
667 * If the first write is a partial write then write at most the number of
668 * bytes to get us page aligned and then the remainder will be
669 * page aligned. The last bit may be a partial page as well.
671 col
= to
& (device
->page_size
- 1);
672 towrite
= device
->page_size
- col
;
685 DEBUG(MTD_DEBUG_LEVEL3
, "%s: write %p to row:%x col:%x len:%x rem:%x\n", chip
->name
, buf
, row
, col
, towrite
, len
);
687 ubi32_nand_spi_er_write_enable();
690 * Move the data into the cache
692 my_towrite
= towrite
;
694 uint32_t len
= my_towrite
;
699 ubi32_nand_spi_er_write_buf(buf
, col
);
708 ubi32_nand_spi_er_send_cmd_addr(0x10, row
);
713 stat
= ubi32_nand_spi_er_busywait();
714 if (stat
& UBI32_NAND_SPI_ER_STATUS_OIP
) {
715 DEBUG(MTD_DEBUG_LEVEL1
, "%s: chip is busy or nonresponsive stat=%02x\n", chip
->name
, stat
);
724 if (stat
& (1 << 3)) {
734 * At this point, we are always page aligned so start at column 0.
735 * Note we may not have a full page to write at the end, hence the
736 * check if towrite > len.
739 towrite
= device
->page_size
;
745 io
->ctl0
&= ~IO_XFL_CTL0_MCB_LOCK
;
747 mutex_unlock(&chip
->lock
);
751 ubi32_nand_spi_er_write_disable();
753 io
->ctl0
&= ~IO_XFL_CTL0_MCB_LOCK
;
755 mutex_unlock(&chip
->lock
);
761 * ubi32_nand_spi_er_isbad
763 static int ubi32_nand_spi_er_isbad(struct mtd_info
*mtd
, loff_t ofs
)
765 struct ubi32_nand_spi_er
*chip
= mtd
->priv
;
768 if (ofs
& (chip
->device
->erase_size
- 1)) {
769 DEBUG(MTD_DEBUG_LEVEL1
, "%s: address not aligned %llx\n", chip
->name
, ofs
);
775 return test_bit(block
, chip
->bbt
);
779 * ubi32_nand_spi_er_markbad
781 static int ubi32_nand_spi_er_markbad(struct mtd_info
*mtd
, loff_t ofs
)
783 struct ubi32_nand_spi_er
*chip
= mtd
->priv
;
784 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
790 if (ofs
& (chip
->device
->erase_size
- 1)) {
791 DEBUG(MTD_DEBUG_LEVEL1
, "%s: address not aligned %llx\n", chip
->name
, ofs
);
798 * If it's already marked bad, no need to mark it
800 if (test_bit(block
, chip
->bbt
)) {
805 * Mark it in our cache
807 __set_bit(block
, chip
->bbt
);
810 * Write the user bad block mark. If it fails, then we really
811 * can't do anything about it.
813 mutex_lock(&chip
->lock
);
814 chip
->last_row
= UBI32_NAND_SPI_ER_LAST_ROW_INVALID
;
816 ubi32_nand_spi_er_write_enable();
821 io
->ctl0
|= IO_XFL_CTL0_MCB_LOCK
;
822 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
823 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_WR
) | IO_XFL_CTL1_FC_DATA(6);
824 io
->ctl2
= IO_XFL_CTL2_FC_CMD(0x84);
827 " bset "D(IO_INT_SET
)"(%[port]), #0, #%%bit("D(IO_PORTX_INT_FIFO_TX_RESET
)") \n\t"
831 * Move the data into the FIFO
833 " move.4 "D(IO_TX_FIFO
)"(%[port]), %[word1] \n\t"
834 " move.4 "D(IO_TX_FIFO
)"(%[port]), %[word2] \n\t"
837 * Kick off the flash command
839 " bset "D(IO_INT_CLR
)"(%[port]), #0, #%%bit("D(IO_XFL_INT_DONE
)") \n\t"
841 " bset "D(IO_INT_SET
)"(%[port]), #0, #%%bit("D(IO_XFL_INT_START
)") \n\t"
844 * Wait for the transaction to finish
846 " btst "D(IO_INT_STATUS
)"(%[port]), #%%bit("D(IO_XFL_INT_DONE
)") \n\t"
850 : [word1
] "d" (0x0800dead | (UBI32_NAND_SPI_ER_BAD_BLOCK_MARK_OFFSET
<< 16)),
851 [word2
] "d" (0xbeef0000),
852 [port
] "a" (FLASH_PORT
)
856 io
->ctl0
&= ~IO_XFL_CTL0_MCB_LOCK
;
862 ubi32_nand_spi_er_send_cmd_addr(0x10, row
);
867 stat
= ubi32_nand_spi_er_busywait();
868 if (stat
& UBI32_NAND_SPI_ER_STATUS_OIP
) {
869 DEBUG(MTD_DEBUG_LEVEL1
, "%s: chip is busy or nonresponsive stat=%02x\n", chip
->name
, stat
);
878 if (stat
& (1 << 3)) {
883 ubi32_nand_spi_er_write_disable();
885 mutex_unlock(&chip
->lock
);
891 * ubi32_nand_spi_er_read_bbt
893 static int ubi32_nand_spi_er_read_bbt(struct ubi32_nand_spi_er
*chip
)
896 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
898 for (j
= 0; j
< chip
->device
->blocks
; j
++) {
899 unsigned short row
= j
<< 6;
905 ubi32_nand_spi_er_send_cmd_addr(0x13, row
);
910 stat
= ubi32_nand_spi_er_busywait();
911 if (stat
& UBI32_NAND_SPI_ER_STATUS_OIP
) {
912 DEBUG(MTD_DEBUG_LEVEL1
, "%s: chip is busy or nonresponsive stat=%02x\n", chip
->name
, stat
);
921 * Check factory bad block mark
923 ubi32_nand_spi_er_send_rd_addr(0x03, 0x080000);
925 if ((io
->status1
>> 24) != 0xFF) {
927 __set_bit(j
, chip
->bbt
);
931 ubi32_nand_spi_er_send_rd_addr(0x03, 0x080000 | (UBI32_NAND_SPI_ER_BAD_BLOCK_MARK_OFFSET
<< 8));
932 if (io
->status1
== 0xdeadbeef) {
934 __set_bit(j
, chip
->bbt
);
938 #if defined(CONFIG_MTD_DEBUG) && (MTD_DEBUG_LEVEL3 <= CONFIG_MTD_DEBUG_VERBOSE)
939 printk("%s: Bad Block Table:", chip
->name
);
940 for (j
= 0; j
< chip
->device
->blocks
; j
++) {
942 printk("\n%s: block %03x: ", chip
->name
, j
);
944 printk("%c", test_bit(j
, chip
->bbt
) ? 'X' : '.');
946 printk("\n%s: Bad Block Numbers: ", chip
->name
);
947 for (j
= 0; j
< chip
->device
->blocks
; j
++) {
948 if (test_bit(j
, chip
->bbt
)) {
960 * Called at boot time:
962 * ubi32_nand_spi_er=read_only
963 * if read_only specified then do not unlock device
965 static int __init
ubi32_nand_spi_er_setup(char *str
)
967 if (str
&& (strncasecmp(str
, "read_only", 9) == 0)) {
973 __setup("ubi32_nand_spi_er=", ubi32_nand_spi_er_setup
);
977 * ubi32_nand_spi_er_probe
978 * Detect and initialize ubi32_nand_spi_er device.
980 static int __devinit
ubi32_nand_spi_er_probe(struct platform_device
*pdev
)
986 struct ubi32_nand_spi_er
*chip
;
987 const struct ubi32_nand_spi_er_device
*device
;
988 struct ubicom32_io_port
*io
= (struct ubicom32_io_port
*)FLASH_PORT
;
993 for (i
= 0; i
< 2; i
++) {
994 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
995 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_CMD
);
996 io
->ctl2
= IO_XFL_CTL2_FC_CMD(0xFF);
997 FLASH_COMMAND_EXEC(io
);
1005 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
1006 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_RD
) | IO_XFL_CTL1_FC_DATA(2) |
1007 IO_XFL_CTL1_FC_ADDR
;
1008 io
->ctl2
= IO_XFL_CTL2_FC_CMD(0x9F);
1009 FLASH_COMMAND_EXEC(io
);
1011 id
= io
->status1
>> 16;
1012 device
= ubi32_nand_spi_er_devices
;
1013 for (i
= 0; i
< ARRAY_SIZE(ubi32_nand_spi_er_devices
); i
++) {
1014 if (device
->id
== id
) {
1019 if (i
== ARRAY_SIZE(ubi32_nand_spi_er_devices
)) {
1024 * Initialize our chip structure
1026 bbt_bytes
= DIV_ROUND_UP(device
->blocks
, BITS_PER_BYTE
);
1027 chip
= kzalloc(sizeof(struct ubi32_nand_spi_er
) + bbt_bytes
, GFP_KERNEL
);
1031 snprintf(chip
->name
, sizeof(chip
->name
), "%s", device
->name
);
1033 chip
->device
= device
;
1034 chip
->last_row
= UBI32_NAND_SPI_ER_LAST_ROW_INVALID
;
1036 mutex_init(&chip
->lock
);
1038 chip
->mtd
.type
= MTD_NANDFLASH
;
1039 chip
->mtd
.flags
= MTD_WRITEABLE
;
1042 * #blocks * block size * n blocks
1044 chip
->mtd
.size
= device
->blocks
* device
->pages_per_block
* device
->page_size
;
1045 chip
->mtd
.erasesize
= device
->erase_size
;
1048 * 1 page, optionally we can support partial write (512)
1050 chip
->mtd
.writesize
= device
->write_size
;
1051 chip
->mtd
.name
= device
->name
;
1052 chip
->mtd
.erase
= ubi32_nand_spi_er_erase
;
1053 chip
->mtd
.read
= ubi32_nand_spi_er_read
;
1054 chip
->mtd
.write
= ubi32_nand_spi_er_write
;
1055 chip
->mtd
.block_isbad
= ubi32_nand_spi_er_isbad
;
1056 chip
->mtd
.block_markbad
= ubi32_nand_spi_er_markbad
;
1057 chip
->mtd
.priv
= chip
;
1060 * Cache the bad block table
1062 res
= ubi32_nand_spi_er_read_bbt(chip
);
1071 io
->ctl0
|= IO_XFL_CTL0_MCB_LOCK
;
1072 io
->ctl1
&= ~IO_XFL_CTL1_MASK
;
1073 io
->ctl1
|= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_WR
) | IO_XFL_CTL1_FC_DATA(2);
1074 io
->ctl2
= IO_XFL_CTL2_FC_CMD(0x1F);
1082 " bset "D(IO_INT_SET
)"(%[port]), #0, #%%bit("D(IO_PORTX_INT_FIFO_TX_RESET
)") \n\t"
1083 " pipe_flush 0 \n\t"
1086 * Move the data into the FIFO
1088 " move.4 "D(IO_TX_FIFO
)"(%[port]), %[word1] \n\t"
1091 * Kick off the flash command
1093 " bset "D(IO_INT_CLR
)"(%[port]), #0, #%%bit("D(IO_XFL_INT_DONE
)") \n\t"
1095 " bset "D(IO_INT_SET
)"(%[port]), #0, #%%bit("D(IO_XFL_INT_START
)") \n\t"
1098 * Wait for the transaction to finish
1100 " btst "D(IO_INT_STATUS
)"(%[port]), #%%bit("D(IO_XFL_INT_DONE
)") \n\t"
1105 [port
] "a" (FLASH_PORT
)
1108 io
->ctl0
&= ~IO_XFL_CTL0_MCB_LOCK
;
1110 dev_set_drvdata(&pdev
->dev
, chip
);
1112 printk(KERN_INFO
"%s: added device size: %u KBytes %lu bad blocks %s\n", chip
->mtd
.name
, DIV_ROUND_UP(chip
->mtd
.size
, 1024), chip
->nbb
, read_only
? "[read only]" : "");
1113 return add_mtd_device(&chip
->mtd
);
1117 * ubi32_nand_spi_er_remove
1119 static int __devexit
ubi32_nand_spi_er_remove(struct platform_device
*pdev
)
1121 struct ubi32_nand_spi_er
*chip
= dev_get_drvdata(&pdev
->dev
);
1124 DEBUG(MTD_DEBUG_LEVEL1
, "%s: remove\n", chip
->name
);
1126 status
= del_mtd_device(&chip
->mtd
);
1131 dev_set_drvdata(&pdev
->dev
, NULL
);
1135 static struct platform_device
*ubi32_nand_spi_er_device
;
1137 static struct platform_driver ubi32_nand_spi_er_driver
= {
1139 .name
= DRIVER_NAME
,
1140 .owner
= THIS_MODULE
,
1143 .probe
= ubi32_nand_spi_er_probe
,
1144 .remove
= ubi32_nand_spi_er_remove
,
1148 * ubi32_nand_spi_er_init
1150 static int __init
ubi32_nand_spi_er_init(void)
1154 ret
= platform_driver_register(&ubi32_nand_spi_er_driver
);
1160 ubi32_nand_spi_er_device
= platform_device_alloc(DRIVER_NAME
, 0);
1161 if (!ubi32_nand_spi_er_device
) {
1165 ret
= platform_device_add(ubi32_nand_spi_er_device
);
1167 platform_device_put(ubi32_nand_spi_er_device
);
1168 platform_driver_unregister(&ubi32_nand_spi_er_driver
);
1173 module_init(ubi32_nand_spi_er_init
);
1176 * ubi32_nand_spi_er_exit
1178 static void __exit
ubi32_nand_spi_er_exit(void)
1180 platform_device_unregister(ubi32_nand_spi_er_device
);
1181 platform_driver_unregister(&ubi32_nand_spi_er_driver
);
1183 module_exit(ubi32_nand_spi_er_exit
);
1186 MODULE_LICENSE("GPL");
1187 MODULE_AUTHOR("Patrick Tjin");
1188 MODULE_DESCRIPTION("MTD ubi32_nand_spi_er driver for ubicom32 SPI flash controller.");