onenand_sim.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /*
  2. * linux/drivers/mtd/onenand/onenand_sim.c
  3. *
  4. * The OneNAND simulator
  5. *
  6. * Copyright © 2005-2007 Samsung Electronics
  7. * Kyungmin Park <kyungmin.park@samsung.com>
  8. *
  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
  12. *
  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.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/partitions.h>
  23. #include <linux/mtd/onenand.h>
  24. #include <linux/io.h>
  25. #ifndef CONFIG_ONENAND_SIM_MANUFACTURER
  26. #define CONFIG_ONENAND_SIM_MANUFACTURER 0xec
  27. #endif
  28. #ifndef CONFIG_ONENAND_SIM_DEVICE_ID
  29. #define CONFIG_ONENAND_SIM_DEVICE_ID 0x04
  30. #endif
  31. #define CONFIG_FLEXONENAND ((CONFIG_ONENAND_SIM_DEVICE_ID >> 9) & 1)
  32. #ifndef CONFIG_ONENAND_SIM_VERSION_ID
  33. #define CONFIG_ONENAND_SIM_VERSION_ID 0x1e
  34. #endif
  35. #ifndef CONFIG_ONENAND_SIM_TECHNOLOGY_ID
  36. #define CONFIG_ONENAND_SIM_TECHNOLOGY_ID CONFIG_FLEXONENAND
  37. #endif
  38. /* Initial boundary values for Flex-OneNAND Simulator */
  39. #ifndef CONFIG_FLEXONENAND_SIM_DIE0_BOUNDARY
  40. #define CONFIG_FLEXONENAND_SIM_DIE0_BOUNDARY 0x01
  41. #endif
  42. #ifndef CONFIG_FLEXONENAND_SIM_DIE1_BOUNDARY
  43. #define CONFIG_FLEXONENAND_SIM_DIE1_BOUNDARY 0x01
  44. #endif
  45. static int manuf_id = CONFIG_ONENAND_SIM_MANUFACTURER;
  46. static int device_id = CONFIG_ONENAND_SIM_DEVICE_ID;
  47. static int version_id = CONFIG_ONENAND_SIM_VERSION_ID;
  48. static int technology_id = CONFIG_ONENAND_SIM_TECHNOLOGY_ID;
  49. static int boundary[] = {
  50. CONFIG_FLEXONENAND_SIM_DIE0_BOUNDARY,
  51. CONFIG_FLEXONENAND_SIM_DIE1_BOUNDARY,
  52. };
  53. struct onenand_flash {
  54. void __iomem *base;
  55. void __iomem *data;
  56. };
  57. #define ONENAND_CORE(flash) (flash->data)
  58. #define ONENAND_CORE_SPARE(flash, this, offset) \
  59. ((flash->data) + (this->chipsize) + (offset >> 5))
  60. #define ONENAND_MAIN_AREA(this, offset) \
  61. (this->base + ONENAND_DATARAM + offset)
  62. #define ONENAND_SPARE_AREA(this, offset) \
  63. (this->base + ONENAND_SPARERAM + offset)
  64. #define ONENAND_GET_WP_STATUS(this) \
  65. (readw(this->base + ONENAND_REG_WP_STATUS))
  66. #define ONENAND_SET_WP_STATUS(v, this) \
  67. (writew(v, this->base + ONENAND_REG_WP_STATUS))
  68. /* It has all 0xff chars */
  69. #define MAX_ONENAND_PAGESIZE (4096 + 128)
  70. static unsigned char *ffchars;
  71. #if CONFIG_FLEXONENAND
  72. #define PARTITION_NAME "Flex-OneNAND simulator partition"
  73. #else
  74. #define PARTITION_NAME "OneNAND simulator partition"
  75. #endif
  76. static struct mtd_partition os_partitions[] = {
  77. {
  78. .name = PARTITION_NAME,
  79. .offset = 0,
  80. .size = MTDPART_SIZ_FULL,
  81. },
  82. };
  83. /*
  84. * OneNAND simulator mtd
  85. */
  86. struct onenand_info {
  87. struct mtd_info mtd;
  88. struct mtd_partition *parts;
  89. struct onenand_chip onenand;
  90. struct onenand_flash flash;
  91. };
  92. static struct onenand_info *info;
  93. #define DPRINTK(format, args...) \
  94. do { \
  95. printk(KERN_DEBUG "%s[%d]: " format "\n", __func__, \
  96. __LINE__, ##args); \
  97. } while (0)
  98. /**
  99. * onenand_lock_handle - Handle Lock scheme
  100. * @this: OneNAND device structure
  101. * @cmd: The command to be sent
  102. *
  103. * Send lock command to OneNAND device.
  104. * The lock scheme depends on chip type.
  105. */
  106. static void onenand_lock_handle(struct onenand_chip *this, int cmd)
  107. {
  108. int block_lock_scheme;
  109. int status;
  110. status = ONENAND_GET_WP_STATUS(this);
  111. block_lock_scheme = !(this->options & ONENAND_HAS_CONT_LOCK);
  112. switch (cmd) {
  113. case ONENAND_CMD_UNLOCK:
  114. case ONENAND_CMD_UNLOCK_ALL:
  115. if (block_lock_scheme)
  116. ONENAND_SET_WP_STATUS(ONENAND_WP_US, this);
  117. else
  118. ONENAND_SET_WP_STATUS(status | ONENAND_WP_US, this);
  119. break;
  120. case ONENAND_CMD_LOCK:
  121. if (block_lock_scheme)
  122. ONENAND_SET_WP_STATUS(ONENAND_WP_LS, this);
  123. else
  124. ONENAND_SET_WP_STATUS(status | ONENAND_WP_LS, this);
  125. break;
  126. case ONENAND_CMD_LOCK_TIGHT:
  127. if (block_lock_scheme)
  128. ONENAND_SET_WP_STATUS(ONENAND_WP_LTS, this);
  129. else
  130. ONENAND_SET_WP_STATUS(status | ONENAND_WP_LTS, this);
  131. break;
  132. default:
  133. break;
  134. }
  135. }
  136. /**
  137. * onenand_bootram_handle - Handle BootRAM area
  138. * @this: OneNAND device structure
  139. * @cmd: The command to be sent
  140. *
  141. * Emulate BootRAM area. It is possible to do basic operation using BootRAM.
  142. */
  143. static void onenand_bootram_handle(struct onenand_chip *this, int cmd)
  144. {
  145. switch (cmd) {
  146. case ONENAND_CMD_READID:
  147. writew(manuf_id, this->base);
  148. writew(device_id, this->base + 2);
  149. writew(version_id, this->base + 4);
  150. break;
  151. default:
  152. /* REVIST: Handle other commands */
  153. break;
  154. }
  155. }
  156. /**
  157. * onenand_update_interrupt - Set interrupt register
  158. * @this: OneNAND device structure
  159. * @cmd: The command to be sent
  160. *
  161. * Update interrupt register. The status depends on command.
  162. */
  163. static void onenand_update_interrupt(struct onenand_chip *this, int cmd)
  164. {
  165. int interrupt = ONENAND_INT_MASTER;
  166. switch (cmd) {
  167. case ONENAND_CMD_READ:
  168. case ONENAND_CMD_READOOB:
  169. interrupt |= ONENAND_INT_READ;
  170. break;
  171. case ONENAND_CMD_PROG:
  172. case ONENAND_CMD_PROGOOB:
  173. interrupt |= ONENAND_INT_WRITE;
  174. break;
  175. case ONENAND_CMD_ERASE:
  176. interrupt |= ONENAND_INT_ERASE;
  177. break;
  178. case ONENAND_CMD_RESET:
  179. interrupt |= ONENAND_INT_RESET;
  180. break;
  181. default:
  182. break;
  183. }
  184. writew(interrupt, this->base + ONENAND_REG_INTERRUPT);
  185. }
  186. /**
  187. * onenand_check_overwrite - Check if over-write happened
  188. * @dest: The destination pointer
  189. * @src: The source pointer
  190. * @count: The length to be check
  191. *
  192. * Returns: 0 on same, otherwise 1
  193. *
  194. * Compare the source with destination
  195. */
  196. static int onenand_check_overwrite(void *dest, void *src, size_t count)
  197. {
  198. unsigned int *s = (unsigned int *) src;
  199. unsigned int *d = (unsigned int *) dest;
  200. int i;
  201. count >>= 2;
  202. for (i = 0; i < count; i++)
  203. if ((*s++ ^ *d++) != 0)
  204. return 1;
  205. return 0;
  206. }
  207. /**
  208. * onenand_data_handle - Handle OneNAND Core and DataRAM
  209. * @this: OneNAND device structure
  210. * @cmd: The command to be sent
  211. * @dataram: Which dataram used
  212. * @offset: The offset to OneNAND Core
  213. *
  214. * Copy data from OneNAND Core to DataRAM (read)
  215. * Copy data from DataRAM to OneNAND Core (write)
  216. * Erase the OneNAND Core (erase)
  217. */
  218. static void onenand_data_handle(struct onenand_chip *this, int cmd,
  219. int dataram, unsigned int offset)
  220. {
  221. struct mtd_info *mtd = &info->mtd;
  222. struct onenand_flash *flash = this->priv;
  223. int main_offset, spare_offset, die = 0;
  224. void __iomem *src;
  225. void __iomem *dest;
  226. unsigned int i;
  227. static int pi_operation;
  228. int erasesize, rgn;
  229. if (dataram) {
  230. main_offset = mtd->writesize;
  231. spare_offset = mtd->oobsize;
  232. } else {
  233. main_offset = 0;
  234. spare_offset = 0;
  235. }
  236. if (pi_operation) {
  237. die = readw(this->base + ONENAND_REG_START_ADDRESS2);
  238. die >>= ONENAND_DDP_SHIFT;
  239. }
  240. switch (cmd) {
  241. case FLEXONENAND_CMD_PI_ACCESS:
  242. pi_operation = 1;
  243. break;
  244. case ONENAND_CMD_RESET:
  245. pi_operation = 0;
  246. break;
  247. case ONENAND_CMD_READ:
  248. src = ONENAND_CORE(flash) + offset;
  249. dest = ONENAND_MAIN_AREA(this, main_offset);
  250. if (pi_operation) {
  251. writew(boundary[die], this->base + ONENAND_DATARAM);
  252. break;
  253. }
  254. memcpy(dest, src, mtd->writesize);
  255. /* Fall through */
  256. case ONENAND_CMD_READOOB:
  257. src = ONENAND_CORE_SPARE(flash, this, offset);
  258. dest = ONENAND_SPARE_AREA(this, spare_offset);
  259. memcpy(dest, src, mtd->oobsize);
  260. break;
  261. case ONENAND_CMD_PROG:
  262. src = ONENAND_MAIN_AREA(this, main_offset);
  263. dest = ONENAND_CORE(flash) + offset;
  264. if (pi_operation) {
  265. boundary[die] = readw(this->base + ONENAND_DATARAM);
  266. break;
  267. }
  268. /* To handle partial write */
  269. for (i = 0; i < (1 << mtd->subpage_sft); i++) {
  270. int off = i * this->subpagesize;
  271. if (!memcmp(src + off, ffchars, this->subpagesize))
  272. continue;
  273. if (memcmp(dest + off, ffchars, this->subpagesize) &&
  274. onenand_check_overwrite(dest + off, src + off, this->subpagesize))
  275. printk(KERN_ERR "over-write happend at 0x%08x\n", offset);
  276. memcpy(dest + off, src + off, this->subpagesize);
  277. }
  278. /* Fall through */
  279. case ONENAND_CMD_PROGOOB:
  280. src = ONENAND_SPARE_AREA(this, spare_offset);
  281. /* Check all data is 0xff chars */
  282. if (!memcmp(src, ffchars, mtd->oobsize))
  283. break;
  284. dest = ONENAND_CORE_SPARE(flash, this, offset);
  285. if (memcmp(dest, ffchars, mtd->oobsize) &&
  286. onenand_check_overwrite(dest, src, mtd->oobsize))
  287. printk(KERN_ERR "OOB: over-write happend at 0x%08x\n",
  288. offset);
  289. memcpy(dest, src, mtd->oobsize);
  290. break;
  291. case ONENAND_CMD_ERASE:
  292. if (pi_operation)
  293. break;
  294. if (FLEXONENAND(this)) {
  295. rgn = flexonenand_region(mtd, offset);
  296. erasesize = mtd->eraseregions[rgn].erasesize;
  297. } else
  298. erasesize = mtd->erasesize;
  299. memset(ONENAND_CORE(flash) + offset, 0xff, erasesize);
  300. memset(ONENAND_CORE_SPARE(flash, this, offset), 0xff,
  301. (erasesize >> 5));
  302. break;
  303. default:
  304. break;
  305. }
  306. }
  307. /**
  308. * onenand_command_handle - Handle command
  309. * @this: OneNAND device structure
  310. * @cmd: The command to be sent
  311. *
  312. * Emulate OneNAND command.
  313. */
  314. static void onenand_command_handle(struct onenand_chip *this, int cmd)
  315. {
  316. unsigned long offset = 0;
  317. int block = -1, page = -1, bufferram = -1;
  318. int dataram = 0;
  319. switch (cmd) {
  320. case ONENAND_CMD_UNLOCK:
  321. case ONENAND_CMD_LOCK:
  322. case ONENAND_CMD_LOCK_TIGHT:
  323. case ONENAND_CMD_UNLOCK_ALL:
  324. onenand_lock_handle(this, cmd);
  325. break;
  326. case ONENAND_CMD_BUFFERRAM:
  327. /* Do nothing */
  328. return;
  329. default:
  330. block = (int) readw(this->base + ONENAND_REG_START_ADDRESS1);
  331. if (block & (1 << ONENAND_DDP_SHIFT)) {
  332. block &= ~(1 << ONENAND_DDP_SHIFT);
  333. /* The half of chip block */
  334. block += this->chipsize >> (this->erase_shift + 1);
  335. }
  336. if (cmd == ONENAND_CMD_ERASE)
  337. break;
  338. page = (int) readw(this->base + ONENAND_REG_START_ADDRESS8);
  339. page = (page >> ONENAND_FPA_SHIFT);
  340. bufferram = (int) readw(this->base + ONENAND_REG_START_BUFFER);
  341. bufferram >>= ONENAND_BSA_SHIFT;
  342. bufferram &= ONENAND_BSA_DATARAM1;
  343. dataram = (bufferram == ONENAND_BSA_DATARAM1) ? 1 : 0;
  344. break;
  345. }
  346. if (block != -1)
  347. offset = onenand_addr(this, block);
  348. if (page != -1)
  349. offset += page << this->page_shift;
  350. onenand_data_handle(this, cmd, dataram, offset);
  351. onenand_update_interrupt(this, cmd);
  352. }
  353. /**
  354. * onenand_writew - [OneNAND Interface] Emulate write operation
  355. * @value: value to write
  356. * @addr: address to write
  357. *
  358. * Write OneNAND register with value
  359. */
  360. static void onenand_writew(unsigned short value, void __iomem * addr)
  361. {
  362. struct onenand_chip *this = info->mtd.priv;
  363. /* BootRAM handling */
  364. if (addr < this->base + ONENAND_DATARAM) {
  365. onenand_bootram_handle(this, value);
  366. return;
  367. }
  368. /* Command handling */
  369. if (addr == this->base + ONENAND_REG_COMMAND)
  370. onenand_command_handle(this, value);
  371. writew(value, addr);
  372. }
  373. /**
  374. * flash_init - Initialize OneNAND simulator
  375. * @flash: OneNAND simulator data strucutres
  376. *
  377. * Initialize OneNAND simulator.
  378. */
  379. static int __init flash_init(struct onenand_flash *flash)
  380. {
  381. int density, size;
  382. int buffer_size;
  383. flash->base = kzalloc(131072, GFP_KERNEL);
  384. if (!flash->base) {
  385. printk(KERN_ERR "Unable to allocate base address.\n");
  386. return -ENOMEM;
  387. }
  388. density = device_id >> ONENAND_DEVICE_DENSITY_SHIFT;
  389. density &= ONENAND_DEVICE_DENSITY_MASK;
  390. size = ((16 << 20) << density);
  391. ONENAND_CORE(flash) = vmalloc(size + (size >> 5));
  392. if (!ONENAND_CORE(flash)) {
  393. printk(KERN_ERR "Unable to allocate nand core address.\n");
  394. kfree(flash->base);
  395. return -ENOMEM;
  396. }
  397. memset(ONENAND_CORE(flash), 0xff, size + (size >> 5));
  398. /* Setup registers */
  399. writew(manuf_id, flash->base + ONENAND_REG_MANUFACTURER_ID);
  400. writew(device_id, flash->base + ONENAND_REG_DEVICE_ID);
  401. writew(version_id, flash->base + ONENAND_REG_VERSION_ID);
  402. writew(technology_id, flash->base + ONENAND_REG_TECHNOLOGY);
  403. if (density < 2 && (!CONFIG_FLEXONENAND))
  404. buffer_size = 0x0400; /* 1KiB page */
  405. else
  406. buffer_size = 0x0800; /* 2KiB page */
  407. writew(buffer_size, flash->base + ONENAND_REG_DATA_BUFFER_SIZE);
  408. return 0;
  409. }
  410. /**
  411. * flash_exit - Clean up OneNAND simulator
  412. * @flash: OneNAND simulator data structures
  413. *
  414. * Clean up OneNAND simulator.
  415. */
  416. static void flash_exit(struct onenand_flash *flash)
  417. {
  418. vfree(ONENAND_CORE(flash));
  419. kfree(flash->base);
  420. }
  421. static int __init onenand_sim_init(void)
  422. {
  423. /* Allocate all 0xff chars pointer */
  424. ffchars = kmalloc(MAX_ONENAND_PAGESIZE, GFP_KERNEL);
  425. if (!ffchars) {
  426. printk(KERN_ERR "Unable to allocate ff chars.\n");
  427. return -ENOMEM;
  428. }
  429. memset(ffchars, 0xff, MAX_ONENAND_PAGESIZE);
  430. /* Allocate OneNAND simulator mtd pointer */
  431. info = kzalloc(sizeof(struct onenand_info), GFP_KERNEL);
  432. if (!info) {
  433. printk(KERN_ERR "Unable to allocate core structures.\n");
  434. kfree(ffchars);
  435. return -ENOMEM;
  436. }
  437. /* Override write_word function */
  438. info->onenand.write_word = onenand_writew;
  439. if (flash_init(&info->flash)) {
  440. printk(KERN_ERR "Unable to allocate flash.\n");
  441. kfree(ffchars);
  442. kfree(info);
  443. return -ENOMEM;
  444. }
  445. info->parts = os_partitions;
  446. info->onenand.base = info->flash.base;
  447. info->onenand.priv = &info->flash;
  448. info->mtd.name = "OneNAND simulator";
  449. info->mtd.priv = &info->onenand;
  450. info->mtd.owner = THIS_MODULE;
  451. if (onenand_scan(&info->mtd, 1)) {
  452. flash_exit(&info->flash);
  453. kfree(ffchars);
  454. kfree(info);
  455. return -ENXIO;
  456. }
  457. add_mtd_partitions(&info->mtd, info->parts, ARRAY_SIZE(os_partitions));
  458. return 0;
  459. }
  460. static void __exit onenand_sim_exit(void)
  461. {
  462. struct onenand_chip *this = info->mtd.priv;
  463. struct onenand_flash *flash = this->priv;
  464. onenand_release(&info->mtd);
  465. flash_exit(flash);
  466. kfree(ffchars);
  467. kfree(info);
  468. }
  469. module_init(onenand_sim_init);
  470. module_exit(onenand_sim_exit);
  471. MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>");
  472. MODULE_DESCRIPTION("The OneNAND flash simulator");
  473. MODULE_LICENSE("GPL");