fsl_upm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Freescale UPM NAND driver.
  3. *
  4. * Copyright © 2007-2008 MontaVista Software, Inc.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mtd/nand.h>
  16. #include <linux/mtd/nand_ecc.h>
  17. #include <linux/mtd/partitions.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/io.h>
  22. #include <asm/fsl_lbc.h>
  23. struct fsl_upm_nand {
  24. struct device *dev;
  25. struct mtd_info mtd;
  26. struct nand_chip chip;
  27. int last_ctrl;
  28. #ifdef CONFIG_MTD_PARTITIONS
  29. struct mtd_partition *parts;
  30. #endif
  31. struct fsl_upm upm;
  32. uint8_t upm_addr_offset;
  33. uint8_t upm_cmd_offset;
  34. void __iomem *io_base;
  35. int rnb_gpio;
  36. const uint32_t *wait_pattern;
  37. const uint32_t *wait_write;
  38. int chip_delay;
  39. };
  40. #define to_fsl_upm_nand(mtd) container_of(mtd, struct fsl_upm_nand, mtd)
  41. static int fun_chip_ready(struct mtd_info *mtd)
  42. {
  43. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  44. if (gpio_get_value(fun->rnb_gpio))
  45. return 1;
  46. dev_vdbg(fun->dev, "busy\n");
  47. return 0;
  48. }
  49. static void fun_wait_rnb(struct fsl_upm_nand *fun)
  50. {
  51. int cnt = 1000000;
  52. if (fun->rnb_gpio >= 0) {
  53. while (--cnt && !fun_chip_ready(&fun->mtd))
  54. cpu_relax();
  55. }
  56. if (!cnt)
  57. dev_err(fun->dev, "tired waiting for RNB\n");
  58. }
  59. static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  60. {
  61. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  62. if (!(ctrl & fun->last_ctrl)) {
  63. fsl_upm_end_pattern(&fun->upm);
  64. if (cmd == NAND_CMD_NONE)
  65. return;
  66. fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
  67. }
  68. if (ctrl & NAND_CTRL_CHANGE) {
  69. if (ctrl & NAND_ALE)
  70. fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
  71. else if (ctrl & NAND_CLE)
  72. fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
  73. }
  74. fsl_upm_run_pattern(&fun->upm, fun->io_base, cmd);
  75. if (fun->wait_pattern)
  76. fun_wait_rnb(fun);
  77. }
  78. static uint8_t fun_read_byte(struct mtd_info *mtd)
  79. {
  80. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  81. return in_8(fun->chip.IO_ADDR_R);
  82. }
  83. static void fun_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  84. {
  85. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  86. int i;
  87. for (i = 0; i < len; i++)
  88. buf[i] = in_8(fun->chip.IO_ADDR_R);
  89. }
  90. static void fun_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  91. {
  92. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  93. int i;
  94. for (i = 0; i < len; i++) {
  95. out_8(fun->chip.IO_ADDR_W, buf[i]);
  96. if (fun->wait_write)
  97. fun_wait_rnb(fun);
  98. }
  99. }
  100. static int __devinit fun_chip_init(struct fsl_upm_nand *fun)
  101. {
  102. int ret;
  103. #ifdef CONFIG_MTD_PARTITIONS
  104. static const char *part_types[] = { "cmdlinepart", NULL, };
  105. #endif
  106. fun->chip.IO_ADDR_R = fun->io_base;
  107. fun->chip.IO_ADDR_W = fun->io_base;
  108. fun->chip.cmd_ctrl = fun_cmd_ctrl;
  109. fun->chip.chip_delay = fun->chip_delay;
  110. fun->chip.read_byte = fun_read_byte;
  111. fun->chip.read_buf = fun_read_buf;
  112. fun->chip.write_buf = fun_write_buf;
  113. fun->chip.ecc.mode = NAND_ECC_SOFT;
  114. if (fun->rnb_gpio >= 0)
  115. fun->chip.dev_ready = fun_chip_ready;
  116. fun->mtd.priv = &fun->chip;
  117. fun->mtd.owner = THIS_MODULE;
  118. ret = nand_scan(&fun->mtd, 1);
  119. if (ret)
  120. return ret;
  121. fun->mtd.name = fun->dev->bus_id;
  122. #ifdef CONFIG_MTD_PARTITIONS
  123. ret = parse_mtd_partitions(&fun->mtd, part_types, &fun->parts, 0);
  124. if (ret > 0)
  125. return add_mtd_partitions(&fun->mtd, fun->parts, ret);
  126. #endif
  127. return add_mtd_device(&fun->mtd);
  128. }
  129. static int __devinit fun_probe(struct of_device *ofdev,
  130. const struct of_device_id *ofid)
  131. {
  132. struct fsl_upm_nand *fun;
  133. struct resource io_res;
  134. const uint32_t *prop;
  135. int ret;
  136. int size;
  137. fun = kzalloc(sizeof(*fun), GFP_KERNEL);
  138. if (!fun)
  139. return -ENOMEM;
  140. ret = of_address_to_resource(ofdev->node, 0, &io_res);
  141. if (ret) {
  142. dev_err(&ofdev->dev, "can't get IO base\n");
  143. goto err1;
  144. }
  145. ret = fsl_upm_find(io_res.start, &fun->upm);
  146. if (ret) {
  147. dev_err(&ofdev->dev, "can't find UPM\n");
  148. goto err1;
  149. }
  150. prop = of_get_property(ofdev->node, "fsl,upm-addr-offset", &size);
  151. if (!prop || size != sizeof(uint32_t)) {
  152. dev_err(&ofdev->dev, "can't get UPM address offset\n");
  153. ret = -EINVAL;
  154. goto err2;
  155. }
  156. fun->upm_addr_offset = *prop;
  157. prop = of_get_property(ofdev->node, "fsl,upm-cmd-offset", &size);
  158. if (!prop || size != sizeof(uint32_t)) {
  159. dev_err(&ofdev->dev, "can't get UPM command offset\n");
  160. ret = -EINVAL;
  161. goto err2;
  162. }
  163. fun->upm_cmd_offset = *prop;
  164. fun->rnb_gpio = of_get_gpio(ofdev->node, 0);
  165. if (fun->rnb_gpio >= 0) {
  166. ret = gpio_request(fun->rnb_gpio, ofdev->dev.bus_id);
  167. if (ret) {
  168. dev_err(&ofdev->dev, "can't request RNB gpio\n");
  169. goto err2;
  170. }
  171. gpio_direction_input(fun->rnb_gpio);
  172. } else if (fun->rnb_gpio == -EINVAL) {
  173. dev_err(&ofdev->dev, "specified RNB gpio is invalid\n");
  174. goto err2;
  175. }
  176. fun->io_base = devm_ioremap_nocache(&ofdev->dev, io_res.start,
  177. io_res.end - io_res.start + 1);
  178. if (!fun->io_base) {
  179. ret = -ENOMEM;
  180. goto err2;
  181. }
  182. fun->dev = &ofdev->dev;
  183. fun->last_ctrl = NAND_CLE;
  184. fun->wait_pattern = of_get_property(ofdev->node, "fsl,wait-pattern",
  185. NULL);
  186. fun->wait_write = of_get_property(ofdev->node, "fsl,wait-write", NULL);
  187. prop = of_get_property(ofdev->node, "chip-delay", NULL);
  188. if (prop)
  189. fun->chip_delay = *prop;
  190. else
  191. fun->chip_delay = 50;
  192. ret = fun_chip_init(fun);
  193. if (ret)
  194. goto err2;
  195. dev_set_drvdata(&ofdev->dev, fun);
  196. return 0;
  197. err2:
  198. if (fun->rnb_gpio >= 0)
  199. gpio_free(fun->rnb_gpio);
  200. err1:
  201. kfree(fun);
  202. return ret;
  203. }
  204. static int __devexit fun_remove(struct of_device *ofdev)
  205. {
  206. struct fsl_upm_nand *fun = dev_get_drvdata(&ofdev->dev);
  207. nand_release(&fun->mtd);
  208. if (fun->rnb_gpio >= 0)
  209. gpio_free(fun->rnb_gpio);
  210. kfree(fun);
  211. return 0;
  212. }
  213. static struct of_device_id of_fun_match[] = {
  214. { .compatible = "fsl,upm-nand" },
  215. {},
  216. };
  217. MODULE_DEVICE_TABLE(of, of_fun_match);
  218. static struct of_platform_driver of_fun_driver = {
  219. .name = "fsl,upm-nand",
  220. .match_table = of_fun_match,
  221. .probe = fun_probe,
  222. .remove = __devexit_p(fun_remove),
  223. };
  224. static int __init fun_module_init(void)
  225. {
  226. return of_register_platform_driver(&of_fun_driver);
  227. }
  228. module_init(fun_module_init);
  229. static void __exit fun_module_exit(void)
  230. {
  231. of_unregister_platform_driver(&of_fun_driver);
  232. }
  233. module_exit(fun_module_exit);
  234. MODULE_LICENSE("GPL");
  235. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  236. MODULE_DESCRIPTION("Driver for NAND chips working through Freescale "
  237. "LocalBus User-Programmable Machine");