2 * drivers/mtd/devices/ubi32-m25p80.c
3 * NOR flash driver, Ubicom processor internal SPI flash interface.
5 * This code instantiates the serial flash that contains the
6 * original bootcode. The serial flash start at address 0x60000000
7 * in both Ubicom32V3 and Ubicom32V4 ISAs.
9 * This piece of flash is made to appear as a Memory Technology
10 * Device (MTD) with this driver to allow Read/Write/Erase operations.
12 * (C) Copyright 2009, Ubicom, Inc.
14 * This file is part of the Ubicom32 Linux Kernel Port.
16 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
17 * it and/or modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation, either version 2 of the
19 * License, or (at your option) any later version.
21 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
22 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
23 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
24 * the GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with the Ubicom32 Linux Kernel Port. If not,
28 * see <http://www.gnu.org/licenses/>.
30 * Ubicom32 implementation derived from (with many thanks):
35 #include <linux/types.h>
36 #include <linux/device.h>
37 #include <linux/platform_device.h>
38 #include <linux/mtd/mtd.h>
39 #include <linux/mtd/partitions.h>
40 #include <linux/mtd/physmap.h>
41 #include <linux/spi/spi.h>
42 #include <linux/spi/flash.h>
44 #include <linux/init.h>
45 #include <linux/module.h>
46 #include <linux/interrupt.h>
47 #include <linux/mutex.h>
49 #include <asm/ip5000.h>
50 #include <asm/devtree.h>
52 #define UBICOM32_FLASH_BASE 0x60000000
53 #define UBICOM32_FLASH_MAX_SIZE 0x01000000
54 #define UBICOM32_FLASH_START 0x00000000
55 #define UBICOM32_KERNEL_OFFSET 0x00010000 /* The kernel starts after Ubicom
56 * .protect section. */
58 static struct mtd_partition ubicom32_flash_partitions[] = {
60 .name = "Bootloader", /* Protected Section
63 .offset = UBICOM32_FLASH_START,
64 // .mask_flags = MTD_WRITEABLE /* Mark Read-only */
67 .name = "Kernel", /* Kernel Partition. */
68 .size = 0, /* this will be set up during
69 * probe stage. At that time we
70 * will know end of linux image
72 .offset = MTDPART_OFS_APPEND, /* Starts right after Protected
74 // .mask_flags = MTD_WRITEABLE /* Mark Read-only */
77 .name = "Rest", /* Rest of the flash. */
78 .size = 0x200000, /* Use up what remains in the
80 .offset = MTDPART_OFS_NXTBLK, /* Starts right after Protected
85 static struct flash_platform_data ubicom32_flash_data = {
86 .name = "ubicom32_boot_flash",
87 .parts = ubicom32_flash_partitions,
88 .nr_parts = ARRAY_SIZE(ubicom32_flash_partitions),
91 static struct resource ubicom32_flash_resource[] = {
93 .start = UBICOM32_FLASH_BASE,
94 .end = UBICOM32_FLASH_BASE +
95 UBICOM32_FLASH_MAX_SIZE - 1,
96 .flags = IORESOURCE_MEM,
100 static struct platform_device ubicom32_flash_device = {
101 .name = "ubicom32flashdriver",
102 .id = 0, /* Bus number */
103 .num_resources = ARRAY_SIZE(ubicom32_flash_resource),
104 .resource = ubicom32_flash_resource,
106 .platform_data = &ubicom32_flash_data,
110 static struct platform_device *ubicom32_flash_devices[] = {
111 &ubicom32_flash_device,
114 static int __init ubicom32_flash_init(void)
116 printk(KERN_INFO "%s(): registering device resources\n",
118 platform_add_devices(ubicom32_flash_devices,
119 ARRAY_SIZE(ubicom32_flash_devices));
123 arch_initcall(ubicom32_flash_init);
126 * MTD SPI driver for ST M25Pxx (and similar) serial flash chips through
127 * Ubicom32 SPI controller.
129 * Author: Mike Lavender, mike@steroidmicros.com
131 * Copyright (c) 2005, Intec Automation Inc.
133 * Some parts are based on lart.c by Abraham Van Der Merwe
135 * Cleaned up and generalized based on mtd_dataflash.c
137 * This code is free software; you can redistribute it and/or modify
138 * it under the terms of the GNU General Public License version 2 as
139 * published by the Free Software Foundation.
143 #define FLASH_PAGESIZE 256
146 #define OPCODE_WREN 0x06 /* Write enable */
147 #define OPCODE_RDSR 0x05 /* Read status register */
148 #define OPCODE_READ 0x03 /* Read data bytes (low frequency) */
149 #define OPCODE_FAST_READ 0x0b /* Read data bytes (high frequency) */
150 #define OPCODE_PP 0x02 /* Page program (up to 256 bytes) */
151 #define OPCODE_BE_4K 0x20 /* Erase 4KiB block */
152 #define OPCODE_BE_32K 0x52 /* Erase 32KiB block */
153 #define OPCODE_SE 0xd8 /* Sector erase (usually 64KiB) */
154 #define OPCODE_RDID 0x9f /* Read JEDEC ID */
156 /* Status Register bits. */
157 #define SR_WIP 1 /* Write in progress */
158 #define SR_WEL 2 /* Write enable latch */
159 /* meaning of other SR_* bits may differ between vendors */
160 #define SR_BP0 4 /* Block protect 0 */
161 #define SR_BP1 8 /* Block protect 1 */
162 #define SR_BP2 0x10 /* Block protect 2 */
163 #define SR_SRWD 0x80 /* SR write protect */
165 /* Define max times to check status register before we give up. */
166 #define MAX_READY_WAIT_COUNT 100000
169 #ifdef CONFIG_MTD_PARTITIONS
170 #define mtd_has_partitions() (1)
172 #define mtd_has_partitions() (0)
176 * Ubicom32 FLASH Command Set
178 #define FLASH_FC_INST_CMD 0x00 /* for SPI command only transaction */
179 #define FLASH_FC_INST_WR 0x01 /* for SPI write transaction */
180 #define FLASH_FC_INST_RD 0x02 /* for SPI read transaction */
182 #define ALIGN_DOWN(v, a) ((v) & ~((a) - 1))
183 #define ALIGN_UP(v, a) (((v) + ((a) - 1)) & ~((a) - 1))
185 #define FLASH_COMMAND_KICK_OFF(io) \
187 " bset "D(IO_INT_CLR)"(%0), #0, #%%bit("D(IO_XFL_INT_DONE)") \n\t" \
189 " bset "D(IO_INT_SET)"(%0), #0, #%%bit("D(IO_XFL_INT_START)") \n\t" \
195 #define FLASH_COMMAND_WAIT_FOR_COMPLETION(io) \
197 " btst "D(IO_INT_STATUS)"(%0), #%%bit("D(IO_XFL_INT_DONE)") \n\t" \
198 " jmpeq.f .-4 \n\t" \
204 #define FLASH_COMMAND_EXEC(io) \
205 FLASH_COMMAND_KICK_OFF(io) \
206 FLASH_COMMAND_WAIT_FOR_COMPLETION(io)
209 #define OSC1_FREQ 12000000
210 #define TEN_MICRO_SECONDS (OSC1_FREQ * 10 / 1000000)
213 * We will have to eventually replace this null definition with the real thing.
215 #define WATCHDOG_RESET()
217 #define EXTFLASH_WRITE_FIFO_SIZE 32
218 #define EXTFLASH_WRITE_BLOCK_SIZE EXTFLASH_WRITE_FIFO_SIZE /* limit the size to
223 #define JFFS2_FILESYSTEM_SIZE 0x100000
225 /****************************************************************************/
228 struct platform_device *plt_dev;
231 unsigned partitioned:1;
236 static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
238 return container_of(mtd, struct m25p, mtd);
241 /****************************************************************************/
244 * Internal helper functions
248 * Read the status register, returning its value in the location
249 * Return the status register value.
250 * Returns negative if error occurred.
252 static int read_sr(struct m25p *flash)
254 struct ubicom32_io_port *io = (struct ubicom32_io_port *)RA;
256 io->ctl1 &= ~IO_XFL_CTL1_MASK;
257 io->ctl1 |= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_RD) |
258 IO_XFL_CTL1_FC_DATA(1);
259 io->ctl2 = IO_XFL_CTL2_FC_CMD(OPCODE_RDSR);
260 FLASH_COMMAND_EXEC(io);
262 return io->status1 & 0xff;
266 * mem_flash_io_read_u32()
268 static u32 mem_flash_io_read_u32(u32 addr)
270 struct ubicom32_io_port *io = (struct ubicom32_io_port *)RA;
271 io->ctl1 &= ~IO_XFL_CTL1_MASK;
272 io->ctl1 |= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_RD) |
273 IO_XFL_CTL1_FC_DATA(4) | IO_XFL_CTL1_FC_DUMMY(1) |
275 io->ctl2 = IO_XFL_CTL2_FC_CMD(OPCODE_FAST_READ) |
276 IO_XFL_CTL2_FC_ADDR(addr);
277 FLASH_COMMAND_EXEC(io);
282 * mem_flash_read_u8()
284 static u8 mem_flash_read_u8(u32 addr)
286 u32 tmp_addr = ALIGN_DOWN(addr, 4);
287 u32 tmp_data = mem_flash_io_read_u32(tmp_addr);
288 u8 *ptr = (u8 *)&tmp_data;
289 return ptr[addr & 0x3];
294 * No need to lock as read is implemented with ireads (same as normal flash
297 static void mem_flash_read(u32 addr, void *dst, size_t length)
303 * Fix source alignment.
305 while (addr & 0x03) {
309 *((u8 *)dst) = mem_flash_read_u8(addr++);
314 while (length >= 4) {
315 u32 tmp_data = mem_flash_io_read_u32(addr);
320 * Send the data to the destination.
322 memcpy((void *)dst, (void *)&tmp_data, 4);
327 *((u8 *)dst) = mem_flash_read_u8(addr++);
333 * mem_flash_wait_until_complete()
335 static void mem_flash_wait_until_complete(void)
337 struct ubicom32_io_port *io = (struct ubicom32_io_port *)RA;
341 * Put a delay here to deal with flash programming problem.
343 u32 mptval = UBICOM32_IO_TIMER->mptval + TEN_MICRO_SECONDS;
344 while (UBICOM32_IO_TIMER->mptval < mptval)
347 io->ctl1 &= ~IO_XFL_CTL1_MASK;
348 io->ctl1 |= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_RD) |
349 IO_XFL_CTL1_FC_DATA(1);
350 io->ctl2 = IO_XFL_CTL2_FC_CMD(OPCODE_RDSR);
351 FLASH_COMMAND_EXEC(io);
352 } while (io->status1 & SR_WIP);
356 * mem_flash_write_next()
358 static size_t mem_flash_write_next(u32 addr, u8 *buf, size_t length)
360 struct ubicom32_io_port *io = (struct ubicom32_io_port *)RA;
361 u32 data_start = addr;
362 u32 data_end = addr + length;
369 u32 block_start = ALIGN_DOWN(data_start, 4);
370 u32 block_end = block_start + EXTFLASH_WRITE_BLOCK_SIZE;
373 u8 byte[EXTFLASH_WRITE_BLOCK_SIZE];
374 u32 word[EXTFLASH_WRITE_BLOCK_SIZE / 4];
377 u32 *flash_addr = (u32 *)block_start;
380 * The write block must be limited by FLASH internal buffer.
382 u32 block_end_align = ALIGN_DOWN(block_end, 256);
385 block_end = (block_end_align > block_start)
386 ? block_end_align : block_end;
387 data_end = (data_end <= block_end) ? data_end : block_end;
388 block_end = ALIGN_UP(data_end, 4);
389 count = data_end - data_start;
392 * Transfer data to a buffer.
394 for (i = 0; i < (block_end - block_start) / 4; i++) {
396 * The FLASH read can hold D-cache for a long time.
397 * Use I/O operation to read FLASH to avoid starving other
398 * threads, especially HRT. (Do this for application only)
400 write_buf.word[i] = mem_flash_io_read_u32(
401 (u32)(&flash_addr[i]));
404 write_needed = false;
405 for (i = 0, j = (data_start - block_start);
406 i < (data_end - data_start); i++, j++) {
407 write_needed = write_needed || (write_buf.byte[j] != buf[i]);
408 write_buf.byte[j] &= buf[i];
413 * If the data in FLASH is identical to what to be written. Then skip
420 void *tmp __attribute__((unused));
424 " move.4 %0, %2 \n\t"
425 " bset "D(IO_INT_SET)"(%1), #0, #%%bit("D(IO_PORTX_INT_FIFO_TX_RESET)") \n\t"
427 " .rept "D(EXTFLASH_WRITE_FIFO_SIZE / 4)" \n\t"
428 " move.4 "D(IO_TX_FIFO)"(%1), (%0)4++ \n\t"
431 : "a" (io), "r" (&write_buf.word[0])
435 /* Lock FLASH for write access. */
436 io->ctl0 |= IO_XFL_CTL0_MCB_LOCK;
439 io->ctl1 &= ~IO_XFL_CTL1_MASK;
440 io->ctl1 |= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_CMD);
441 io->ctl2 = IO_XFL_CTL2_FC_CMD(OPCODE_WREN);
442 FLASH_COMMAND_EXEC(io);
444 /* Command: BYTE PROGRAM */
445 io->ctl1 &= ~IO_XFL_CTL1_MASK;
446 io->ctl1 |= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_WR) |
447 IO_XFL_CTL1_FC_DATA(block_end - block_start) |
449 io->ctl2 = IO_XFL_CTL2_FC_CMD(OPCODE_PP) |
450 IO_XFL_CTL2_FC_ADDR(block_start);
451 FLASH_COMMAND_KICK_OFF(io);
453 extra_words = (s32)(block_end - block_start -
454 EXTFLASH_WRITE_FIFO_SIZE) / 4;
455 if (extra_words > 0) {
457 " move.4 %0, %3 \n\t"
458 "1: cmpi "D(IO_FIFO_LEVEL)"(%1), #4 \n\t"
460 " move.4 "D(IO_TX_FIFO)"(%1), (%0)4++ \n\t"
461 " add.4 %2, #-1, %2 \n\t"
464 : "a" (io), "d" (extra_words),
465 "r" (&write_buf.word[EXTFLASH_WRITE_FIFO_SIZE / 4])
469 FLASH_COMMAND_WAIT_FOR_COMPLETION(io);
471 mem_flash_wait_until_complete();
474 /* Unlock FLASH for cache access. */
475 io->ctl0 &= ~IO_XFL_CTL0_MCB_LOCK;
487 static void mem_flash_write(u32 addr, const void *src, size_t length)
492 u8_t *ptr = (u8_t *)src;
494 size_t count = mem_flash_write_next(addr, ptr, length);
502 * Service routine to read status register until ready, or timeout occurs.
503 * Returns non-zero if error.
505 static int wait_till_ready(struct m25p *flash)
510 /* one chip guarantees max 5 msec wait here after page writes,
511 * but potentially three seconds (!) after page erase.
513 for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
518 else if (!(sr & SR_WIP))
522 * Put a 10us delay here to deal with flash programming problem.
524 mptval = UBICOM32_IO_TIMER->mptval + TEN_MICRO_SECONDS;
525 while ((s32)(mptval - UBICOM32_IO_TIMER->mptval) > 0) {
528 /* REVISIT sometimes sleeping would be best */
535 * mem_flash_erase_page()
537 static void mem_flash_erase_page(u32 addr)
539 struct ubicom32_io_port *io = (struct ubicom32_io_port *)RA;
541 /* Lock FLASH for write access. */
542 io->ctl0 |= IO_XFL_CTL0_MCB_LOCK;
545 io->ctl1 &= ~IO_XFL_CTL1_MASK;
546 io->ctl1 |= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_CMD);
547 io->ctl2 = IO_XFL_CTL2_FC_CMD(OPCODE_WREN);
548 FLASH_COMMAND_EXEC(io);
551 io->ctl1 &= ~IO_XFL_CTL1_MASK;
552 io->ctl1 |= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_CMD) |
554 io->ctl2 = IO_XFL_CTL2_FC_CMD(OPCODE_SE) |
555 IO_XFL_CTL2_FC_ADDR(addr);
556 FLASH_COMMAND_EXEC(io);
558 mem_flash_wait_until_complete();
560 /* Unlock FLASH for cache access. */
561 io->ctl0 &= ~IO_XFL_CTL0_MCB_LOCK;
567 static u32 mem_flash_erase(u32 addr, u32 length)
570 * Calculate the endaddress to be the first address of the page
571 * just beyond this erase section of pages.
573 u32 endaddr = addr + length;
578 while (addr < endaddr) {
579 u32 test_addr = addr;
580 mem_flash_erase_page(addr);
583 * Test how much was erased as actual flash page at this address
584 * may be smaller than the expected page size.
586 while (test_addr < endaddr) {
588 * The FLASH read can hold D-cache for a long time. Use
589 * I/O operation to read FLASH to avoid starving other
590 * threads, especially HRT. (Do this for application
593 if (mem_flash_io_read_u32(test_addr) != 0xFFFFFFFF) {
598 if (test_addr == addr) {
599 printk("erase failed at address 0x%x, skipping",
610 /****************************************************************************/
617 * Erase an address range on the flash chip. The address range may extend
618 * one or more erase sectors. Return an error is there is a problem erasing.
620 static int ubicom32_flash_driver_erase(struct mtd_info *mtd,
621 struct erase_info *instr)
623 struct m25p *flash = mtd_to_m25p(mtd);
626 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %lld\n",
627 dev_name(&flash->plt_dev->dev), __FUNCTION__, "at",
628 (u32)instr->addr, instr->len);
631 if (instr->addr + instr->len > flash->mtd.size)
633 if ((instr->addr % mtd->erasesize) != 0
634 || (instr->len % mtd->erasesize) != 0) {
638 addr = instr->addr + UBICOM32_FLASH_BASE;
641 mutex_lock(&flash->lock);
643 /* REVISIT in some cases we could speed up erasing large regions
644 * by using OPCODE_SE instead of OPCODE_BE_4K
647 /* now erase those sectors */
648 if (mem_flash_erase(addr, len)) {
649 instr->state = MTD_ERASE_FAILED;
650 mutex_unlock(&flash->lock);
654 mutex_unlock(&flash->lock);
655 instr->state = MTD_ERASE_DONE;
656 mtd_erase_callback(instr);
661 * Read an address range from the flash chip. The address range
662 * may be any size provided it is within the physical boundaries.
664 static int ubicom32_flash_driver_read(struct mtd_info *mtd, loff_t from,
665 size_t len, size_t *retlen, u_char *buf)
667 struct m25p *flash = mtd_to_m25p(mtd);
668 u32 base_addr = UBICOM32_FLASH_BASE + from;
670 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %d\n",
671 dev_name(&flash->plt_dev->dev), __FUNCTION__, "from",
678 if (from + len > flash->mtd.size)
681 /* Byte count starts at zero. */
685 mutex_lock(&flash->lock);
687 /* Wait till previous write/erase is done. */
688 if (wait_till_ready(flash)) {
689 /* REVISIT status return?? */
690 mutex_unlock(&flash->lock);
694 mem_flash_read(base_addr, (void *)buf, len);
699 mutex_unlock(&flash->lock);
705 * Write an address range to the flash chip. Data must be written in
706 * FLASH_PAGESIZE chunks. The address range may be any size provided
707 * it is within the physical boundaries.
709 static int ubicom32_flash_driver_write(struct mtd_info *mtd, loff_t to,
710 size_t len, size_t *retlen,
713 struct m25p *flash = mtd_to_m25p(mtd);
714 u32 base_addr = UBICOM32_FLASH_BASE + to;
715 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %d\n",
716 dev_name(&flash->plt_dev->dev), __FUNCTION__, "to",
726 if (to + len > flash->mtd.size)
729 mutex_lock(&flash->lock);
731 mem_flash_write(base_addr, (void *) buf, len);
733 /* Wait until finished previous write command. */
734 if (wait_till_ready(flash)) {
735 mutex_unlock(&flash->lock);
742 mutex_unlock(&flash->lock);
747 /****************************************************************************/
750 * SPI device driver setup and teardown
756 /* JEDEC id zero means "no ID" (most older chips); otherwise it has
757 * a high byte of zero plus three data bytes: the manufacturer id,
758 * then a two byte device id.
762 /* The size listed here is what works with OPCODE_SE, which isn't
763 * necessarily called a "sector" by the vendor.
765 unsigned sector_size;
769 #define SECT_4K 0x01 /* OPCODE_BE_4K works uniformly */
773 /* NOTE: double check command sets and memory organization when you add
774 * more flash chips. This current list focusses on newer chips, which
775 * have been converging on command sets which including JEDEC ID.
777 static struct flash_info __devinitdata m25p_data[] = {
779 /* Atmel -- some are (confusingly) marketed as "DataFlash" */
780 { "at25fs010", 0x1f6601, 32 * 1024, 4, SECT_4K, },
781 { "at25fs040", 0x1f6604, 64 * 1024, 8, SECT_4K, },
783 { "at25df041a", 0x1f4401, 64 * 1024, 8, SECT_4K, },
785 { "at26f004", 0x1f0400, 64 * 1024, 8, SECT_4K, },
786 { "at26df081a", 0x1f4501, 64 * 1024, 16, SECT_4K, },
787 { "at26df161a", 0x1f4601, 64 * 1024, 32, SECT_4K, },
788 { "at26df321", 0x1f4701, 64 * 1024, 64, SECT_4K, },
790 /* Spansion -- single (large) sector size only, at least
791 * for the chips listed here (without boot sectors).
793 { "s25sl004a", 0x010212, 64 * 1024, 8, },
794 { "s25sl008a", 0x010213, 64 * 1024, 16, },
795 { "s25sl016a", 0x010214, 64 * 1024, 32, },
796 { "s25sl032a", 0x010215, 64 * 1024, 64, },
797 { "s25sl064a", 0x010216, 64 * 1024, 128, },
799 /* SST -- large erase sizes are "overlays", "sectors" are 4K */
800 { "sst25vf040b", 0xbf258d, 64 * 1024, 8, SECT_4K, },
801 { "sst25vf080b", 0xbf258e, 64 * 1024, 16, SECT_4K, },
802 { "sst25vf016b", 0xbf2541, 64 * 1024, 32, SECT_4K, },
803 { "sst25vf032b", 0xbf254a, 64 * 1024, 64, SECT_4K, },
805 /* ST Microelectronics -- newer production may have feature updates */
806 { "m25p05", 0x202010, 32 * 1024, 2, },
807 { "m25p10", 0x202011, 32 * 1024, 4, },
808 { "m25p20", 0x202012, 64 * 1024, 4, },
809 { "m25p40", 0x202013, 64 * 1024, 8, },
810 { "m25p80", 0, 64 * 1024, 16, },
811 { "m25p16", 0x202015, 64 * 1024, 32, },
812 { "m25p32", 0x202016, 64 * 1024, 64, },
813 { "m25p64", 0x202017, 64 * 1024, 128, },
814 { "m25p128", 0x202018, 256 * 1024, 64, },
816 { "m45pe80", 0x204014, 64 * 1024, 16, },
817 { "m45pe16", 0x204015, 64 * 1024, 32, },
819 { "m25pe80", 0x208014, 64 * 1024, 16, },
820 { "m25pe16", 0x208015, 64 * 1024, 32, SECT_4K, },
822 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
823 { "w25x10", 0xef3011, 64 * 1024, 2, SECT_4K, },
824 { "w25x20", 0xef3012, 64 * 1024, 4, SECT_4K, },
825 { "w25x40", 0xef3013, 64 * 1024, 8, SECT_4K, },
826 { "w25x80", 0xef3014, 64 * 1024, 16, SECT_4K, },
827 { "w25x16", 0xef3015, 64 * 1024, 32, SECT_4K, },
828 { "w25x32", 0xef3016, 64 * 1024, 64, SECT_4K, },
829 { "w25x64", 0xef3017, 64 * 1024, 128, SECT_4K, },
831 /* Macronix -- mx25lxxx */
832 { "mx25l32", 0xc22016, 64 * 1024, 64, },
833 { "mx25l64", 0xc22017, 64 * 1024, 128, },
834 { "mx25l128", 0xc22018, 64 * 1024, 256, },
838 struct flash_info *__devinit jedec_probe(struct platform_device *spi)
842 struct flash_info *info;
843 struct ubicom32_io_port *io = (struct ubicom32_io_port *)RA;
846 * Setup and run RDID command on the flash.
848 io->ctl1 &= ~IO_XFL_CTL1_MASK;
849 io->ctl1 |= IO_XFL_CTL1_FC_INST(FLASH_FC_INST_RD) |
850 IO_XFL_CTL1_FC_DATA(3);
851 io->ctl2 = IO_XFL_CTL2_FC_CMD(OPCODE_RDID);
852 FLASH_COMMAND_EXEC(io);
854 jedec = io->status1 & 0x00ffffff;
856 for (tmp = 0, info = m25p_data;
857 tmp < ARRAY_SIZE(m25p_data);
859 if (info->jedec_id == jedec)
862 dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
868 * board specific setup should have ensured the SPI clock used here
869 * matches what the READ command supports, at least until this driver
870 * understands FAST_READ (for clocks over 25 MHz).
872 static int __devinit ubicom32_flash_probe(struct platform_device *spi)
874 struct flash_platform_data *data;
876 struct flash_info *info;
879 /* Platform data helps sort out which chip type we have, as
880 * well as how this board partitions it. If we don't have
881 * a chip ID, try the JEDEC id commands; they'll work for most
882 * newer chips, even if we don't recognize the particular chip.
884 data = spi->dev.platform_data;
885 if (data && data->type) {
886 for (i = 0, info = m25p_data;
887 i < ARRAY_SIZE(m25p_data);
889 if (strcmp(data->type, info->name) == 0)
893 /* unrecognized chip? */
894 if (i == ARRAY_SIZE(m25p_data)) {
895 DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
896 dev_name(&spi->dev), data->type);
899 /* recognized; is that chip really what's there? */
900 } else if (info->jedec_id) {
901 struct flash_info *chip = jedec_probe(spi);
903 if (!chip || chip != info) {
904 dev_warn(&spi->dev, "found %s, expected %s\n",
905 chip ? chip->name : "UNKNOWN",
911 info = jedec_probe(spi);
916 flash = kzalloc(sizeof *flash, GFP_KERNEL);
920 flash->plt_dev = spi;
921 mutex_init(&flash->lock);
922 dev_set_drvdata(&spi->dev, flash);
924 if (data && data->name)
925 flash->mtd.name = data->name;
927 flash->mtd.name = dev_name(&spi->dev);
929 flash->mtd.type = MTD_NORFLASH;
930 flash->mtd.writesize = 1;
931 flash->mtd.flags = MTD_CAP_NORFLASH;
932 flash->mtd.size = info->sector_size * info->n_sectors;
933 flash->mtd.erase = ubicom32_flash_driver_erase;
934 flash->mtd.read = ubicom32_flash_driver_read;
935 flash->mtd.write = ubicom32_flash_driver_write;
937 /* prefer "small sector" erase if possible */
939 * The Ubicom erase code does not use the opcode for smaller sectors,
940 * so disable that functionality and keep erasesize == sector_size
941 * so that the test in ubicom32_flash_driver_erase works properly.
943 * This was: `if (info->flags & SECT_4K) {' instead of `if (0) {'
946 flash->erase_opcode = OPCODE_BE_4K;
947 flash->mtd.erasesize = 4096;
949 flash->erase_opcode = OPCODE_SE;
950 flash->mtd.erasesize = info->sector_size;
953 dev_info(&spi->dev, "%s (%lld Kbytes)\n", info->name,
954 flash->mtd.size / 1024);
956 DEBUG(MTD_DEBUG_LEVEL2,
957 "mtd .name = %s, .size = 0x%.8llx (%lluMiB) "
958 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
960 flash->mtd.size, flash->mtd.size / (1024*1024),
961 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
962 flash->mtd.numeraseregions);
964 if (flash->mtd.numeraseregions)
965 for (i = 0; i < flash->mtd.numeraseregions; i++)
966 DEBUG(MTD_DEBUG_LEVEL2,
967 "mtd.eraseregions[%d] = { .offset = 0x%.8llx, "
968 ".erasesize = 0x%.8x (%uKiB), "
969 ".numblocks = %d }\n",
970 i, flash->mtd.eraseregions[i].offset,
971 flash->mtd.eraseregions[i].erasesize,
972 flash->mtd.eraseregions[i].erasesize / 1024,
973 flash->mtd.eraseregions[i].numblocks);
976 /* partitions should match sector boundaries; and it may be good to
977 * use readonly partitions for writeprotected sectors (BP2..BP0).
979 if (mtd_has_partitions()) {
980 struct mtd_partition *parts = NULL;
983 #ifdef CONFIG_MTD_CMDLINE_PARTS
984 static const char *part_probes[] = { "cmdlinepart", NULL, };
986 nr_parts = parse_mtd_partitions(&flash->mtd,
987 part_probes, &parts, 0);
990 if (nr_parts <= 0 && data && data->parts) {
992 nr_parts = data->nr_parts;
995 * Set last partition size to be 1M.
997 parts[1].size = flash->mtd.size -
998 parts[0].size - JFFS2_FILESYSTEM_SIZE;
999 parts[2].size = JFFS2_FILESYSTEM_SIZE;
1004 for (i = 0; i < nr_parts; i++) {
1005 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
1006 "{.name = %s, .offset = 0x%.8llx, "
1007 ".size = 0x%.8llx (%lluKiB) }\n",
1011 parts[i].size / 1024);
1013 flash->partitioned = 1;
1014 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
1016 } else if (data->nr_parts)
1017 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
1018 data->nr_parts, data->name);
1020 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
1024 static int __devexit ubicom32_flash_remove(struct spi_device *spi)
1026 struct m25p *flash = dev_get_drvdata(&spi->dev);
1029 /* Clean up MTD stuff. */
1030 if (mtd_has_partitions() && flash->partitioned)
1031 status = del_mtd_partitions(&flash->mtd);
1033 status = del_mtd_device(&flash->mtd);
1039 static struct platform_driver ubicom32_flash_driver = {
1041 .name = "ubicom32flashdriver",
1042 .bus = &platform_bus_type,
1043 .owner = THIS_MODULE,
1045 .probe = ubicom32_flash_probe,
1049 static int ubicom32_flash_driver_init(void)
1051 return platform_driver_register(&ubicom32_flash_driver);
1055 static void ubicom32_flash_driver_exit(void)
1057 platform_driver_unregister(&ubicom32_flash_driver);
1061 module_init(ubicom32_flash_driver_init);
1062 module_exit(ubicom32_flash_driver_exit);
1064 MODULE_LICENSE("GPL");
1065 MODULE_AUTHOR("Mike Lavender");
1066 MODULE_DESCRIPTION("Ubicom32 MTD SPI driver for ST M25Pxx flash chips");