sharpsl.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * drivers/mtd/nand/sharpsl.c
  3. *
  4. * Copyright (C) 2004 Richard Purdie
  5. * Copyright (C) 2008 Dmitry Baryshkov
  6. *
  7. * Based on Sharp's NAND driver sharp_sl.c
  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. */
  14. #include <linux/genhd.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/delay.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/nand.h>
  20. #include <linux/mtd/nand_ecc.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/mtd/sharpsl.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/platform_device.h>
  25. #include <asm/io.h>
  26. #include <mach/hardware.h>
  27. #include <asm/mach-types.h>
  28. struct sharpsl_nand {
  29. struct mtd_info mtd;
  30. struct nand_chip chip;
  31. void __iomem *io;
  32. };
  33. #define mtd_to_sharpsl(_mtd) container_of(_mtd, struct sharpsl_nand, mtd)
  34. /* register offset */
  35. #define ECCLPLB 0x00 /* line parity 7 - 0 bit */
  36. #define ECCLPUB 0x04 /* line parity 15 - 8 bit */
  37. #define ECCCP 0x08 /* column parity 5 - 0 bit */
  38. #define ECCCNTR 0x0C /* ECC byte counter */
  39. #define ECCCLRR 0x10 /* cleare ECC */
  40. #define FLASHIO 0x14 /* Flash I/O */
  41. #define FLASHCTL 0x18 /* Flash Control */
  42. /* Flash control bit */
  43. #define FLRYBY (1 << 5)
  44. #define FLCE1 (1 << 4)
  45. #define FLWP (1 << 3)
  46. #define FLALE (1 << 2)
  47. #define FLCLE (1 << 1)
  48. #define FLCE0 (1 << 0)
  49. /*
  50. * hardware specific access to control-lines
  51. * ctrl:
  52. * NAND_CNE: bit 0 -> ! bit 0 & 4
  53. * NAND_CLE: bit 1 -> bit 1
  54. * NAND_ALE: bit 2 -> bit 2
  55. *
  56. */
  57. static void sharpsl_nand_hwcontrol(struct mtd_info *mtd, int cmd,
  58. unsigned int ctrl)
  59. {
  60. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  61. struct nand_chip *chip = mtd->priv;
  62. if (ctrl & NAND_CTRL_CHANGE) {
  63. unsigned char bits = ctrl & 0x07;
  64. bits |= (ctrl & 0x01) << 4;
  65. bits ^= 0x11;
  66. writeb((readb(sharpsl->io + FLASHCTL) & ~0x17) | bits, sharpsl->io + FLASHCTL);
  67. }
  68. if (cmd != NAND_CMD_NONE)
  69. writeb(cmd, chip->IO_ADDR_W);
  70. }
  71. static int sharpsl_nand_dev_ready(struct mtd_info *mtd)
  72. {
  73. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  74. return !((readb(sharpsl->io + FLASHCTL) & FLRYBY) == 0);
  75. }
  76. static void sharpsl_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  77. {
  78. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  79. writeb(0, sharpsl->io + ECCCLRR);
  80. }
  81. static int sharpsl_nand_calculate_ecc(struct mtd_info *mtd, const u_char * dat, u_char * ecc_code)
  82. {
  83. struct sharpsl_nand *sharpsl = mtd_to_sharpsl(mtd);
  84. ecc_code[0] = ~readb(sharpsl->io + ECCLPUB);
  85. ecc_code[1] = ~readb(sharpsl->io + ECCLPLB);
  86. ecc_code[2] = (~readb(sharpsl->io + ECCCP) << 2) | 0x03;
  87. return readb(sharpsl->io + ECCCNTR) != 0;
  88. }
  89. #ifdef CONFIG_MTD_PARTITIONS
  90. static const char *part_probes[] = { "cmdlinepart", NULL };
  91. #endif
  92. /*
  93. * Main initialization routine
  94. */
  95. static int __devinit sharpsl_nand_probe(struct platform_device *pdev)
  96. {
  97. struct nand_chip *this;
  98. #ifdef CONFIG_MTD_PARTITIONS
  99. struct mtd_partition *sharpsl_partition_info;
  100. int nr_partitions;
  101. #endif
  102. struct resource *r;
  103. int err = 0;
  104. struct sharpsl_nand *sharpsl;
  105. struct sharpsl_nand_platform_data *data = pdev->dev.platform_data;
  106. if (!data) {
  107. dev_err(&pdev->dev, "no platform data!\n");
  108. return -EINVAL;
  109. }
  110. /* Allocate memory for MTD device structure and private data */
  111. sharpsl = kzalloc(sizeof(struct sharpsl_nand), GFP_KERNEL);
  112. if (!sharpsl) {
  113. printk("Unable to allocate SharpSL NAND MTD device structure.\n");
  114. return -ENOMEM;
  115. }
  116. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  117. if (!r) {
  118. dev_err(&pdev->dev, "no io memory resource defined!\n");
  119. err = -ENODEV;
  120. goto err_get_res;
  121. }
  122. /* map physical address */
  123. sharpsl->io = ioremap(r->start, resource_size(r));
  124. if (!sharpsl->io) {
  125. printk("ioremap to access Sharp SL NAND chip failed\n");
  126. err = -EIO;
  127. goto err_ioremap;
  128. }
  129. /* Get pointer to private data */
  130. this = (struct nand_chip *)(&sharpsl->chip);
  131. /* Link the private data with the MTD structure */
  132. sharpsl->mtd.priv = this;
  133. sharpsl->mtd.owner = THIS_MODULE;
  134. platform_set_drvdata(pdev, sharpsl);
  135. /*
  136. * PXA initialize
  137. */
  138. writeb(readb(sharpsl->io + FLASHCTL) | FLWP, sharpsl->io + FLASHCTL);
  139. /* Set address of NAND IO lines */
  140. this->IO_ADDR_R = sharpsl->io + FLASHIO;
  141. this->IO_ADDR_W = sharpsl->io + FLASHIO;
  142. /* Set address of hardware control function */
  143. this->cmd_ctrl = sharpsl_nand_hwcontrol;
  144. this->dev_ready = sharpsl_nand_dev_ready;
  145. /* 15 us command delay time */
  146. this->chip_delay = 15;
  147. /* set eccmode using hardware ECC */
  148. this->ecc.mode = NAND_ECC_HW;
  149. this->ecc.size = 256;
  150. this->ecc.bytes = 3;
  151. this->badblock_pattern = data->badblock_pattern;
  152. this->ecc.layout = data->ecc_layout;
  153. this->ecc.hwctl = sharpsl_nand_enable_hwecc;
  154. this->ecc.calculate = sharpsl_nand_calculate_ecc;
  155. this->ecc.correct = nand_correct_data;
  156. /* Scan to find existence of the device */
  157. err = nand_scan(&sharpsl->mtd, 1);
  158. if (err)
  159. goto err_scan;
  160. /* Register the partitions */
  161. sharpsl->mtd.name = "sharpsl-nand";
  162. #ifdef CONFIG_MTD_PARTITIONS
  163. nr_partitions = parse_mtd_partitions(&sharpsl->mtd, part_probes, &sharpsl_partition_info, 0);
  164. if (nr_partitions <= 0) {
  165. nr_partitions = data->nr_partitions;
  166. sharpsl_partition_info = data->partitions;
  167. }
  168. if (nr_partitions > 0)
  169. err = add_mtd_partitions(&sharpsl->mtd, sharpsl_partition_info, nr_partitions);
  170. else
  171. #endif
  172. err = add_mtd_device(&sharpsl->mtd);
  173. if (err)
  174. goto err_add;
  175. /* Return happy */
  176. return 0;
  177. err_add:
  178. nand_release(&sharpsl->mtd);
  179. err_scan:
  180. platform_set_drvdata(pdev, NULL);
  181. iounmap(sharpsl->io);
  182. err_ioremap:
  183. err_get_res:
  184. kfree(sharpsl);
  185. return err;
  186. }
  187. /*
  188. * Clean up routine
  189. */
  190. static int __devexit sharpsl_nand_remove(struct platform_device *pdev)
  191. {
  192. struct sharpsl_nand *sharpsl = platform_get_drvdata(pdev);
  193. /* Release resources, unregister device */
  194. nand_release(&sharpsl->mtd);
  195. platform_set_drvdata(pdev, NULL);
  196. iounmap(sharpsl->io);
  197. /* Free the MTD device structure */
  198. kfree(sharpsl);
  199. return 0;
  200. }
  201. static struct platform_driver sharpsl_nand_driver = {
  202. .driver = {
  203. .name = "sharpsl-nand",
  204. .owner = THIS_MODULE,
  205. },
  206. .probe = sharpsl_nand_probe,
  207. .remove = __devexit_p(sharpsl_nand_remove),
  208. };
  209. /*
  210. * Define partitions for flash device
  211. */
  212. static struct mtd_partition sharpsl_nand_partitions[] = {
  213. {
  214. .name = "System Area",
  215. .offset = 0,
  216. .size = 7 * 1024 * 1024,
  217. },
  218. {
  219. .name = "Root Filesystem",
  220. .offset = 7 * 1024 * 1024,
  221. .size = 30 * 1024 * 1024,
  222. },
  223. {
  224. .name = "Home Filesystem",
  225. .offset = MTDPART_OFS_APPEND,
  226. .size = MTDPART_SIZ_FULL,
  227. },
  228. };
  229. static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
  230. static struct nand_bbt_descr sharpsl_bbt = {
  231. .options = 0,
  232. .offs = 4,
  233. .len = 2,
  234. .pattern = scan_ff_pattern
  235. };
  236. static struct nand_bbt_descr sharpsl_akita_bbt = {
  237. .options = 0,
  238. .offs = 4,
  239. .len = 1,
  240. .pattern = scan_ff_pattern
  241. };
  242. static struct nand_ecclayout akita_oobinfo = {
  243. .eccbytes = 24,
  244. .eccpos = {
  245. 0x5, 0x1, 0x2, 0x3, 0x6, 0x7, 0x15, 0x11,
  246. 0x12, 0x13, 0x16, 0x17, 0x25, 0x21, 0x22, 0x23,
  247. 0x26, 0x27, 0x35, 0x31, 0x32, 0x33, 0x36, 0x37},
  248. .oobfree = {{0x08, 0x09}}
  249. };
  250. static struct sharpsl_nand_platform_data sharpsl_nand_platform_data = {
  251. .badblock_pattern = &sharpsl_bbt,
  252. .partitions = sharpsl_nand_partitions,
  253. .nr_partitions = ARRAY_SIZE(sharpsl_nand_partitions),
  254. };
  255. static struct resource sharpsl_nand_resources[] = {
  256. {
  257. .start = 0x0C000000,
  258. .end = 0x0C000FFF,
  259. .flags = IORESOURCE_MEM,
  260. },
  261. };
  262. static struct platform_device sharpsl_nand_device = {
  263. .name = "sharpsl-nand",
  264. .id = -1,
  265. .resource = sharpsl_nand_resources,
  266. .num_resources = ARRAY_SIZE(sharpsl_nand_resources),
  267. .dev.platform_data = &sharpsl_nand_platform_data,
  268. };
  269. static int __init sharpsl_nand_init(void)
  270. {
  271. if (machine_is_poodle()) {
  272. sharpsl_nand_partitions[1].size = 22 * 1024 * 1024;
  273. } else if (machine_is_corgi() || machine_is_shepherd()) {
  274. sharpsl_nand_partitions[1].size = 25 * 1024 * 1024;
  275. } else if (machine_is_husky()) {
  276. sharpsl_nand_partitions[1].size = 53 * 1024 * 1024;
  277. } else if (machine_is_spitz()) {
  278. sharpsl_nand_partitions[1].size = 5 * 1024 * 1024;
  279. } else if (machine_is_akita()) {
  280. sharpsl_nand_partitions[1].size = 58 * 1024 * 1024;
  281. sharpsl_nand_platform_data.badblock_pattern = &sharpsl_akita_bbt;
  282. sharpsl_nand_platform_data.ecc_layout = &akita_oobinfo;
  283. } else if (machine_is_borzoi()) {
  284. sharpsl_nand_partitions[1].size = 32 * 1024 * 1024;
  285. }
  286. platform_device_register(&sharpsl_nand_device);
  287. return platform_driver_register(&sharpsl_nand_driver);
  288. }
  289. module_init(sharpsl_nand_init);
  290. static void __exit sharpsl_nand_exit(void)
  291. {
  292. platform_driver_unregister(&sharpsl_nand_driver);
  293. platform_device_unregister(&sharpsl_nand_device);
  294. }
  295. module_exit(sharpsl_nand_exit);
  296. MODULE_LICENSE("GPL");
  297. MODULE_AUTHOR("Richard Purdie <rpurdie@rpsys.net>");
  298. MODULE_DESCRIPTION("Device specific logic for NAND flash on Sharp SL-C7xx Series");