onenand_sim.c 12 KB

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