12 #include <linux/module.h>
14 #include <linux/slab.h>
16 #include <linux/device.h>
40 #define OP_READ_CONTINUOUS 0xE8
41 #define OP_READ_PAGE 0xD2
44 #define OP_READ_STATUS 0xD7
47 #define OP_READ_BUFFER1 0xD4
48 #define OP_READ_BUFFER2 0xD6
49 #define OP_WRITE_BUFFER1 0x84
50 #define OP_WRITE_BUFFER2 0x87
53 #define OP_ERASE_PAGE 0x81
54 #define OP_ERASE_BLOCK 0x50
57 #define OP_TRANSFER_BUF1 0x53
58 #define OP_TRANSFER_BUF2 0x55
59 #define OP_MREAD_BUFFER1 0xD4
60 #define OP_MREAD_BUFFER2 0xD6
61 #define OP_MWERASE_BUFFER1 0x83
62 #define OP_MWERASE_BUFFER2 0x86
63 #define OP_MWRITE_BUFFER1 0x88
64 #define OP_MWRITE_BUFFER2 0x89
67 #define OP_PROGRAM_VIA_BUF1 0x82
68 #define OP_PROGRAM_VIA_BUF2 0x85
71 #define OP_COMPARE_BUF1 0x60
72 #define OP_COMPARE_BUF2 0x61
75 #define OP_REWRITE_VIA_BUF1 0x58
76 #define OP_REWRITE_VIA_BUF2 0x59
81 #define OP_READ_ID 0x9F
82 #define OP_READ_SECURITY 0x77
83 #define OP_WRITE_SECURITY_REVC 0x9A
84 #define OP_WRITE_SECURITY 0x9B
104 { .compatible =
"atmel,at45", },
105 { .compatible =
"atmel,dataflash", },
109 #define dataflash_dt_ids NULL
134 status = dataflash_status(spi);
137 dev_name(&spi->
dev), status);
141 if (status & (1 << 7))
159 unsigned blocksize = priv->
page_size << 3;
163 pr_debug(
"%s: erase addr=0x%llx len 0x%llx\n",
164 dev_name(&spi->
dev), (
long long)instr->
addr,
165 (
long long)instr->
len);
174 spi_message_init(&
msg);
178 spi_message_add_tail(&x, &
msg);
181 while (instr->
len > 0) {
182 unsigned int pageaddr;
190 do_block = (pageaddr & 0x7) == 0 && instr->
len >= blocksize;
194 command[1] = (
uint8_t)(pageaddr >> 16);
195 command[2] = (
uint8_t)(pageaddr >> 8);
198 pr_debug(
"ERASE %s: (%x) %x %x %x [%i]\n",
199 do_block ?
"block" :
"page",
200 command[0], command[1], command[2], command[3],
204 (
void) dataflash_waitready(spi);
208 dev_name(&spi->
dev), pageaddr, status);
216 instr->
addr += blocksize;
217 instr->
len -= blocksize;
239 static int dataflash_read(
struct mtd_info *mtd, loff_t
from,
size_t len,
249 pr_debug(
"%s: read 0x%x..0x%x\n", dev_name(&priv->
spi->dev),
250 (
unsigned)from, (
unsigned)(from + len));
259 command[0], command[1], command[2], command[3]);
261 spi_message_init(&
msg);
265 spi_message_add_tail(&x[0], &
msg);
269 spi_message_add_tail(&x[1], &
msg);
278 command[1] = (
uint8_t)(addr >> 16);
279 command[2] = (
uint8_t)(addr >> 8);
280 command[3] = (
uint8_t)(addr >> 0);
287 *retlen =
msg.actual_length - 8;
290 pr_debug(
"%s: read %x..%x --> %d\n",
291 dev_name(&priv->
spi->dev),
292 (
unsigned)from, (
unsigned)(from + len),
304 static int dataflash_write(
struct mtd_info *mtd, loff_t to,
size_t len,
305 size_t * retlen,
const u_char * buf)
311 unsigned int pageaddr,
addr,
offset, writelen;
312 size_t remaining = len;
318 dev_name(&spi->
dev), (
unsigned)to, (
unsigned)(to + len));
320 spi_message_init(&
msg);
324 spi_message_add_tail(&x[0], &
msg);
326 pageaddr = ((unsigned)to / priv->
page_size);
327 offset = ((unsigned)to % priv->
page_size);
334 while (remaining > 0) {
336 pageaddr, offset, writelen);
359 command[1] = (addr & 0x00FF0000) >> 16;
360 command[2] = (addr & 0x0000FF00) >> 8;
363 pr_debug(
"TRANSFER: (%x) %x %x %x\n",
364 command[0], command[1], command[2], command[3]);
369 dev_name(&spi->
dev), addr, status);
371 (
void) dataflash_waitready(priv->
spi);
377 command[1] = (addr & 0x00FF0000) >> 16;
378 command[2] = (addr & 0x0000FF00) >> 8;
379 command[3] = (addr & 0x000000FF);
381 pr_debug(
"PROGRAM: (%x) %x %x %x\n",
382 command[0], command[1], command[2], command[3]);
386 spi_message_add_tail(x + 1, &
msg);
388 spi_transfer_del(x + 1);
391 dev_name(&spi->
dev), addr, writelen, status);
393 (
void) dataflash_waitready(priv->
spi);
396 #ifdef CONFIG_MTD_DATAFLASH_WRITE_VERIFY
401 command[1] = (addr & 0x00FF0000) >> 16;
402 command[2] = (addr & 0x0000FF00) >> 8;
405 pr_debug(
"COMPARE: (%x) %x %x %x\n",
406 command[0], command[1], command[2], command[3]);
411 dev_name(&spi->
dev), addr, status);
413 status = dataflash_waitready(priv->
spi);
416 if (status & (1 << 6)) {
418 dev_name(&spi->
dev), pageaddr, status);
427 remaining = remaining - writelen;
430 writebuf += writelen;
436 writelen = remaining;
445 #ifdef CONFIG_MTD_DATAFLASH_OTP
447 static int dataflash_get_otp_info(
struct mtd_info *mtd,
457 return sizeof(*info);
461 uint8_t *buf, loff_t off,
size_t len)
472 if ((off + len) > 64)
475 spi_message_init(&
m);
477 l = 4 + base + off +
len;
491 spi_message_add_tail(&
t, &
m);
493 dataflash_waitready(spi);
497 memcpy(buf, scratch + 4 + base + off, len);
505 static int dataflash_read_fact_otp(
struct mtd_info *mtd,
506 loff_t from,
size_t len,
size_t *retlen,
u_char *buf)
513 status = otp_read(priv->
spi, 64, buf, from, len);
522 static int dataflash_read_user_otp(
struct mtd_info *mtd,
523 loff_t from,
size_t len,
size_t *retlen,
u_char *buf)
530 status = otp_read(priv->
spi, 0, buf, from, len);
539 static int dataflash_write_user_otp(
struct mtd_info *mtd,
540 loff_t from,
size_t len,
size_t *retlen,
u_char *buf)
543 const size_t l = 4 + 64;
555 if ((from + len) > 64)
565 memcpy(scratch + 4 + from, buf, len);
567 spi_message_init(&
m);
572 spi_message_add_tail(&
t, &
m);
578 dataflash_waitready(priv->
spi);
609 static char *otp_setup(
struct mtd_info *device,
char revision)
623 int nr_pages,
int pagesize,
int pageoffset,
char revision)
648 device->
size = nr_pages * pagesize;
654 device->
_erase = dataflash_erase;
655 device->
_read = dataflash_read;
656 device->
_write = dataflash_write;
659 device->
dev.parent = &spi->
dev;
662 otp_tag = otp_setup(device, revision);
664 dev_info(&spi->
dev,
"%s (%lld KBytes) pagesize %d bytes%s\n",
665 name, (
long long)((device->
size + 1023) >> 10),
683 add_dataflash(
struct spi_device *spi,
char *name,
684 int nr_pages,
int pagesize,
int pageoffset)
686 return add_dataflash_otp(spi, name, nr_pages, pagesize,
704 #define SUP_POW2PS 0x0002
705 #define IS_POW2PS 0x0001
719 {
"AT45DB011B", 0x1f2200, 512, 264, 9,
SUP_POW2PS},
722 {
"AT45DB021B", 0x1f2300, 1024, 264, 9,
SUP_POW2PS},
725 {
"AT45DB041x", 0x1f2400, 2048, 264, 9,
SUP_POW2PS},
728 {
"AT45DB081B", 0x1f2500, 4096, 264, 9,
SUP_POW2PS},
731 {
"AT45DB161x", 0x1f2600, 4096, 528, 10,
SUP_POW2PS},
734 {
"AT45DB321x", 0x1f2700, 8192, 528, 10, 0},
736 {
"AT45DB321x", 0x1f2701, 8192, 528, 10,
SUP_POW2PS},
739 {
"AT45DB642x", 0x1f2800, 8192, 1056, 11,
SUP_POW2PS},
762 pr_debug(
"%s: error %d reading JEDEC ID\n",
763 dev_name(&spi->
dev), tmp);
775 for (tmp = 0, info = dataflash_data;
779 pr_debug(
"%s: OTP, sector protect%s\n",
782 ?
", binary pagesize" :
""
785 status = dataflash_status(spi);
788 dev_name(&spi->
dev), status);
789 return ERR_PTR(status);
808 dev_warn(&spi->
dev,
"JEDEC id %06x not handled\n", jedec);
838 info = jedec_probe(spi);
840 return PTR_ERR(info);
842 return add_dataflash_otp(spi, info->
name, info->
nr_pages,
850 status = dataflash_status(spi);
851 if (status <= 0 || status == 0xff) {
853 dev_name(&spi->
dev), status);
854 if (status == 0 || status == 0xff)
863 switch (status & 0x3c) {
865 status = add_dataflash(spi,
"AT45DB011B", 512, 264, 9);
868 status = add_dataflash(spi,
"AT45DB021B", 1024, 264, 9);
871 status = add_dataflash(spi,
"AT45DB041x", 2048, 264, 9);
874 status = add_dataflash(spi,
"AT45DB081B", 4096, 264, 9);
877 status = add_dataflash(spi,
"AT45DB161x", 4096, 528, 10);
880 status = add_dataflash(spi,
"AT45DB321x", 8192, 528, 10);
884 status = add_dataflash(spi,
"AT45DB642x", 8192, 1056, 11);
888 pr_debug(
"%s: unsupported device (%x)\n", dev_name(&spi->
dev),
894 pr_debug(
"%s: add_dataflash --> %d\n", dev_name(&spi->
dev),
917 .name =
"mtd_dataflash",
922 .probe = dataflash_probe,