i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / mtd / onenand / onenand_sim.c
blob85399e3accda56205c07211853284cfe70317495
1 /*
2 * linux/drivers/mtd/onenand/onenand_sim.c
4 * The OneNAND simulator
6 * Copyright © 2005-2007 Samsung Electronics
7 * Kyungmin Park <kyungmin.park@samsung.com>
9 * Vishak G <vishak.g at samsung.com>, Rohit Hagargundgi <h.rohit at samsung.com>
10 * Flex-OneNAND simulator support
11 * Copyright (C) Samsung Electronics, 2008
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/vmalloc.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/partitions.h>
25 #include <linux/mtd/onenand.h>
27 #include <linux/io.h>
29 #ifndef CONFIG_ONENAND_SIM_MANUFACTURER
30 #define CONFIG_ONENAND_SIM_MANUFACTURER 0xec
31 #endif
33 #ifndef CONFIG_ONENAND_SIM_DEVICE_ID
34 #define CONFIG_ONENAND_SIM_DEVICE_ID 0x04
35 #endif
37 #define CONFIG_FLEXONENAND ((CONFIG_ONENAND_SIM_DEVICE_ID >> 9) & 1)
39 #ifndef CONFIG_ONENAND_SIM_VERSION_ID
40 #define CONFIG_ONENAND_SIM_VERSION_ID 0x1e
41 #endif
43 #ifndef CONFIG_ONENAND_SIM_TECHNOLOGY_ID
44 #define CONFIG_ONENAND_SIM_TECHNOLOGY_ID CONFIG_FLEXONENAND
45 #endif
47 /* Initial boundary values for Flex-OneNAND Simulator */
48 #ifndef CONFIG_FLEXONENAND_SIM_DIE0_BOUNDARY
49 #define CONFIG_FLEXONENAND_SIM_DIE0_BOUNDARY 0x01
50 #endif
52 #ifndef CONFIG_FLEXONENAND_SIM_DIE1_BOUNDARY
53 #define CONFIG_FLEXONENAND_SIM_DIE1_BOUNDARY 0x01
54 #endif
56 static int manuf_id = CONFIG_ONENAND_SIM_MANUFACTURER;
57 static int device_id = CONFIG_ONENAND_SIM_DEVICE_ID;
58 static int version_id = CONFIG_ONENAND_SIM_VERSION_ID;
59 static int technology_id = CONFIG_ONENAND_SIM_TECHNOLOGY_ID;
60 static int boundary[] = {
61 CONFIG_FLEXONENAND_SIM_DIE0_BOUNDARY,
62 CONFIG_FLEXONENAND_SIM_DIE1_BOUNDARY,
65 struct onenand_flash {
66 void __iomem *base;
67 void __iomem *data;
70 #define ONENAND_CORE(flash) (flash->data)
71 #define ONENAND_CORE_SPARE(flash, this, offset) \
72 ((flash->data) + (this->chipsize) + (offset >> 5))
74 #define ONENAND_MAIN_AREA(this, offset) \
75 (this->base + ONENAND_DATARAM + offset)
77 #define ONENAND_SPARE_AREA(this, offset) \
78 (this->base + ONENAND_SPARERAM + offset)
80 #define ONENAND_GET_WP_STATUS(this) \
81 (readw(this->base + ONENAND_REG_WP_STATUS))
83 #define ONENAND_SET_WP_STATUS(v, this) \
84 (writew(v, this->base + ONENAND_REG_WP_STATUS))
86 /* It has all 0xff chars */
87 #define MAX_ONENAND_PAGESIZE (4096 + 128)
88 static unsigned char *ffchars;
90 #if CONFIG_FLEXONENAND
91 #define PARTITION_NAME "Flex-OneNAND simulator partition"
92 #else
93 #define PARTITION_NAME "OneNAND simulator partition"
94 #endif
96 static struct mtd_partition os_partitions[] = {
98 .name = PARTITION_NAME,
99 .offset = 0,
100 .size = MTDPART_SIZ_FULL,
105 * OneNAND simulator mtd
107 struct onenand_info {
108 struct mtd_info mtd;
109 struct mtd_partition *parts;
110 struct onenand_chip onenand;
111 struct onenand_flash flash;
114 static struct onenand_info *info;
116 #define DPRINTK(format, args...) \
117 do { \
118 printk(KERN_DEBUG "%s[%d]: " format "\n", __func__, \
119 __LINE__, ##args); \
120 } while (0)
123 * onenand_lock_handle - Handle Lock scheme
124 * @this: OneNAND device structure
125 * @cmd: The command to be sent
127 * Send lock command to OneNAND device.
128 * The lock scheme depends on chip type.
130 static void onenand_lock_handle(struct onenand_chip *this, int cmd)
132 int block_lock_scheme;
133 int status;
135 status = ONENAND_GET_WP_STATUS(this);
136 block_lock_scheme = !(this->options & ONENAND_HAS_CONT_LOCK);
138 switch (cmd) {
139 case ONENAND_CMD_UNLOCK:
140 case ONENAND_CMD_UNLOCK_ALL:
141 if (block_lock_scheme)
142 ONENAND_SET_WP_STATUS(ONENAND_WP_US, this);
143 else
144 ONENAND_SET_WP_STATUS(status | ONENAND_WP_US, this);
145 break;
147 case ONENAND_CMD_LOCK:
148 if (block_lock_scheme)
149 ONENAND_SET_WP_STATUS(ONENAND_WP_LS, this);
150 else
151 ONENAND_SET_WP_STATUS(status | ONENAND_WP_LS, this);
152 break;
154 case ONENAND_CMD_LOCK_TIGHT:
155 if (block_lock_scheme)
156 ONENAND_SET_WP_STATUS(ONENAND_WP_LTS, this);
157 else
158 ONENAND_SET_WP_STATUS(status | ONENAND_WP_LTS, this);
159 break;
161 default:
162 break;
167 * onenand_bootram_handle - Handle BootRAM area
168 * @this: OneNAND device structure
169 * @cmd: The command to be sent
171 * Emulate BootRAM area. It is possible to do basic operation using BootRAM.
173 static void onenand_bootram_handle(struct onenand_chip *this, int cmd)
175 switch (cmd) {
176 case ONENAND_CMD_READID:
177 writew(manuf_id, this->base);
178 writew(device_id, this->base + 2);
179 writew(version_id, this->base + 4);
180 break;
182 default:
183 /* REVIST: Handle other commands */
184 break;
189 * onenand_update_interrupt - Set interrupt register
190 * @this: OneNAND device structure
191 * @cmd: The command to be sent
193 * Update interrupt register. The status depends on command.
195 static void onenand_update_interrupt(struct onenand_chip *this, int cmd)
197 int interrupt = ONENAND_INT_MASTER;
199 switch (cmd) {
200 case ONENAND_CMD_READ:
201 case ONENAND_CMD_READOOB:
202 interrupt |= ONENAND_INT_READ;
203 break;
205 case ONENAND_CMD_PROG:
206 case ONENAND_CMD_PROGOOB:
207 interrupt |= ONENAND_INT_WRITE;
208 break;
210 case ONENAND_CMD_ERASE:
211 interrupt |= ONENAND_INT_ERASE;
212 break;
214 case ONENAND_CMD_RESET:
215 interrupt |= ONENAND_INT_RESET;
216 break;
218 default:
219 break;
222 writew(interrupt, this->base + ONENAND_REG_INTERRUPT);
226 * onenand_check_overwrite - Check if over-write happened
227 * @dest: The destination pointer
228 * @src: The source pointer
229 * @count: The length to be check
231 * Returns: 0 on same, otherwise 1
233 * Compare the source with destination
235 static int onenand_check_overwrite(void *dest, void *src, size_t count)
237 unsigned int *s = (unsigned int *) src;
238 unsigned int *d = (unsigned int *) dest;
239 int i;
241 count >>= 2;
242 for (i = 0; i < count; i++)
243 if ((*s++ ^ *d++) != 0)
244 return 1;
246 return 0;
250 * onenand_data_handle - Handle OneNAND Core and DataRAM
251 * @this: OneNAND device structure
252 * @cmd: The command to be sent
253 * @dataram: Which dataram used
254 * @offset: The offset to OneNAND Core
256 * Copy data from OneNAND Core to DataRAM (read)
257 * Copy data from DataRAM to OneNAND Core (write)
258 * Erase the OneNAND Core (erase)
260 static void onenand_data_handle(struct onenand_chip *this, int cmd,
261 int dataram, unsigned int offset)
263 struct mtd_info *mtd = &info->mtd;
264 struct onenand_flash *flash = this->priv;
265 int main_offset, spare_offset, die = 0;
266 void __iomem *src;
267 void __iomem *dest;
268 unsigned int i;
269 static int pi_operation;
270 int erasesize, rgn;
272 if (dataram) {
273 main_offset = mtd->writesize;
274 spare_offset = mtd->oobsize;
275 } else {
276 main_offset = 0;
277 spare_offset = 0;
280 if (pi_operation) {
281 die = readw(this->base + ONENAND_REG_START_ADDRESS2);
282 die >>= ONENAND_DDP_SHIFT;
285 switch (cmd) {
286 case FLEXONENAND_CMD_PI_ACCESS:
287 pi_operation = 1;
288 break;
290 case ONENAND_CMD_RESET:
291 pi_operation = 0;
292 break;
294 case ONENAND_CMD_READ:
295 src = ONENAND_CORE(flash) + offset;
296 dest = ONENAND_MAIN_AREA(this, main_offset);
297 if (pi_operation) {
298 writew(boundary[die], this->base + ONENAND_DATARAM);
299 break;
301 memcpy(dest, src, mtd->writesize);
302 /* Fall through */
304 case ONENAND_CMD_READOOB:
305 src = ONENAND_CORE_SPARE(flash, this, offset);
306 dest = ONENAND_SPARE_AREA(this, spare_offset);
307 memcpy(dest, src, mtd->oobsize);
308 break;
310 case ONENAND_CMD_PROG:
311 src = ONENAND_MAIN_AREA(this, main_offset);
312 dest = ONENAND_CORE(flash) + offset;
313 if (pi_operation) {
314 boundary[die] = readw(this->base + ONENAND_DATARAM);
315 break;
317 /* To handle partial write */
318 for (i = 0; i < (1 << mtd->subpage_sft); i++) {
319 int off = i * this->subpagesize;
320 if (!memcmp(src + off, ffchars, this->subpagesize))
321 continue;
322 if (memcmp(dest + off, ffchars, this->subpagesize) &&
323 onenand_check_overwrite(dest + off, src + off, this->subpagesize))
324 printk(KERN_ERR "over-write happened at 0x%08x\n", offset);
325 memcpy(dest + off, src + off, this->subpagesize);
327 /* Fall through */
329 case ONENAND_CMD_PROGOOB:
330 src = ONENAND_SPARE_AREA(this, spare_offset);
331 /* Check all data is 0xff chars */
332 if (!memcmp(src, ffchars, mtd->oobsize))
333 break;
335 dest = ONENAND_CORE_SPARE(flash, this, offset);
336 if (memcmp(dest, ffchars, mtd->oobsize) &&
337 onenand_check_overwrite(dest, src, mtd->oobsize))
338 printk(KERN_ERR "OOB: over-write happened at 0x%08x\n",
339 offset);
340 memcpy(dest, src, mtd->oobsize);
341 break;
343 case ONENAND_CMD_ERASE:
344 if (pi_operation)
345 break;
347 if (FLEXONENAND(this)) {
348 rgn = flexonenand_region(mtd, offset);
349 erasesize = mtd->eraseregions[rgn].erasesize;
350 } else
351 erasesize = mtd->erasesize;
353 memset(ONENAND_CORE(flash) + offset, 0xff, erasesize);
354 memset(ONENAND_CORE_SPARE(flash, this, offset), 0xff,
355 (erasesize >> 5));
356 break;
358 default:
359 break;
364 * onenand_command_handle - Handle command
365 * @this: OneNAND device structure
366 * @cmd: The command to be sent
368 * Emulate OneNAND command.
370 static void onenand_command_handle(struct onenand_chip *this, int cmd)
372 unsigned long offset = 0;
373 int block = -1, page = -1, bufferram = -1;
374 int dataram = 0;
376 switch (cmd) {
377 case ONENAND_CMD_UNLOCK:
378 case ONENAND_CMD_LOCK:
379 case ONENAND_CMD_LOCK_TIGHT:
380 case ONENAND_CMD_UNLOCK_ALL:
381 onenand_lock_handle(this, cmd);
382 break;
384 case ONENAND_CMD_BUFFERRAM:
385 /* Do nothing */
386 return;
388 default:
389 block = (int) readw(this->base + ONENAND_REG_START_ADDRESS1);
390 if (block & (1 << ONENAND_DDP_SHIFT)) {
391 block &= ~(1 << ONENAND_DDP_SHIFT);
392 /* The half of chip block */
393 block += this->chipsize >> (this->erase_shift + 1);
395 if (cmd == ONENAND_CMD_ERASE)
396 break;
398 page = (int) readw(this->base + ONENAND_REG_START_ADDRESS8);
399 page = (page >> ONENAND_FPA_SHIFT);
400 bufferram = (int) readw(this->base + ONENAND_REG_START_BUFFER);
401 bufferram >>= ONENAND_BSA_SHIFT;
402 bufferram &= ONENAND_BSA_DATARAM1;
403 dataram = (bufferram == ONENAND_BSA_DATARAM1) ? 1 : 0;
404 break;
407 if (block != -1)
408 offset = onenand_addr(this, block);
410 if (page != -1)
411 offset += page << this->page_shift;
413 onenand_data_handle(this, cmd, dataram, offset);
415 onenand_update_interrupt(this, cmd);
419 * onenand_writew - [OneNAND Interface] Emulate write operation
420 * @value: value to write
421 * @addr: address to write
423 * Write OneNAND register with value
425 static void onenand_writew(unsigned short value, void __iomem * addr)
427 struct onenand_chip *this = info->mtd.priv;
429 /* BootRAM handling */
430 if (addr < this->base + ONENAND_DATARAM) {
431 onenand_bootram_handle(this, value);
432 return;
434 /* Command handling */
435 if (addr == this->base + ONENAND_REG_COMMAND)
436 onenand_command_handle(this, value);
438 writew(value, addr);
442 * flash_init - Initialize OneNAND simulator
443 * @flash: OneNAND simulator data strucutres
445 * Initialize OneNAND simulator.
447 static int __init flash_init(struct onenand_flash *flash)
449 int density, size;
450 int buffer_size;
452 flash->base = kzalloc(131072, GFP_KERNEL);
453 if (!flash->base) {
454 printk(KERN_ERR "Unable to allocate base address.\n");
455 return -ENOMEM;
458 density = device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
459 density &= ONENAND_DEVICE_DENSITY_MASK;
460 size = ((16 << 20) << density);
462 ONENAND_CORE(flash) = vmalloc(size + (size >> 5));
463 if (!ONENAND_CORE(flash)) {
464 printk(KERN_ERR "Unable to allocate nand core address.\n");
465 kfree(flash->base);
466 return -ENOMEM;
469 memset(ONENAND_CORE(flash), 0xff, size + (size >> 5));
471 /* Setup registers */
472 writew(manuf_id, flash->base + ONENAND_REG_MANUFACTURER_ID);
473 writew(device_id, flash->base + ONENAND_REG_DEVICE_ID);
474 writew(version_id, flash->base + ONENAND_REG_VERSION_ID);
475 writew(technology_id, flash->base + ONENAND_REG_TECHNOLOGY);
477 if (density < 2 && (!CONFIG_FLEXONENAND))
478 buffer_size = 0x0400; /* 1KiB page */
479 else
480 buffer_size = 0x0800; /* 2KiB page */
481 writew(buffer_size, flash->base + ONENAND_REG_DATA_BUFFER_SIZE);
483 return 0;
487 * flash_exit - Clean up OneNAND simulator
488 * @flash: OneNAND simulator data structures
490 * Clean up OneNAND simulator.
492 static void flash_exit(struct onenand_flash *flash)
494 vfree(ONENAND_CORE(flash));
495 kfree(flash->base);
498 static int __init onenand_sim_init(void)
500 /* Allocate all 0xff chars pointer */
501 ffchars = kmalloc(MAX_ONENAND_PAGESIZE, GFP_KERNEL);
502 if (!ffchars) {
503 printk(KERN_ERR "Unable to allocate ff chars.\n");
504 return -ENOMEM;
506 memset(ffchars, 0xff, MAX_ONENAND_PAGESIZE);
508 /* Allocate OneNAND simulator mtd pointer */
509 info = kzalloc(sizeof(struct onenand_info), GFP_KERNEL);
510 if (!info) {
511 printk(KERN_ERR "Unable to allocate core structures.\n");
512 kfree(ffchars);
513 return -ENOMEM;
516 /* Override write_word function */
517 info->onenand.write_word = onenand_writew;
519 if (flash_init(&info->flash)) {
520 printk(KERN_ERR "Unable to allocate flash.\n");
521 kfree(ffchars);
522 kfree(info);
523 return -ENOMEM;
526 info->parts = os_partitions;
528 info->onenand.base = info->flash.base;
529 info->onenand.priv = &info->flash;
531 info->mtd.name = "OneNAND simulator";
532 info->mtd.priv = &info->onenand;
533 info->mtd.owner = THIS_MODULE;
535 if (onenand_scan(&info->mtd, 1)) {
536 flash_exit(&info->flash);
537 kfree(ffchars);
538 kfree(info);
539 return -ENXIO;
542 mtd_device_register(&info->mtd, info->parts,
543 ARRAY_SIZE(os_partitions));
545 return 0;
548 static void __exit onenand_sim_exit(void)
550 struct onenand_chip *this = info->mtd.priv;
551 struct onenand_flash *flash = this->priv;
553 onenand_release(&info->mtd);
554 flash_exit(flash);
555 kfree(ffchars);
556 kfree(info);
559 module_init(onenand_sim_init);
560 module_exit(onenand_sim_exit);
562 MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
563 MODULE_DESCRIPTION("The OneNAND flash simulator");
564 MODULE_LICENSE("GPL");