fsl_upm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/delay.h>
  16. #include <linux/mtd/nand.h>
  17. #include <linux/mtd/nand_ecc.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/io.h>
  23. #include <asm/fsl_lbc.h>
  24. struct fsl_upm_nand {
  25. struct device *dev;
  26. struct mtd_info mtd;
  27. struct nand_chip chip;
  28. int last_ctrl;
  29. #ifdef CONFIG_MTD_PARTITIONS
  30. struct mtd_partition *parts;
  31. #endif
  32. struct fsl_upm upm;
  33. uint8_t upm_addr_offset;
  34. uint8_t upm_cmd_offset;
  35. void __iomem *io_base;
  36. int rnb_gpio;
  37. int chip_delay;
  38. };
  39. #define to_fsl_upm_nand(mtd) container_of(mtd, struct fsl_upm_nand, mtd)
  40. static int fun_chip_ready(struct mtd_info *mtd)
  41. {
  42. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  43. if (gpio_get_value(fun->rnb_gpio))
  44. return 1;
  45. dev_vdbg(fun->dev, "busy\n");
  46. return 0;
  47. }
  48. static void fun_wait_rnb(struct fsl_upm_nand *fun)
  49. {
  50. int cnt = 1000000;
  51. if (fun->rnb_gpio >= 0) {
  52. while (--cnt && !fun_chip_ready(&fun->mtd))
  53. cpu_relax();
  54. if (!cnt)
  55. dev_err(fun->dev, "tired waiting for RNB\n");
  56. } else {
  57. ndelay(100);
  58. }
  59. }
  60. static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  61. {
  62. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  63. if (!(ctrl & fun->last_ctrl)) {
  64. fsl_upm_end_pattern(&fun->upm);
  65. if (cmd == NAND_CMD_NONE)
  66. return;
  67. fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
  68. }
  69. if (ctrl & NAND_CTRL_CHANGE) {
  70. if (ctrl & NAND_ALE)
  71. fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
  72. else if (ctrl & NAND_CLE)
  73. fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
  74. }
  75. fsl_upm_run_pattern(&fun->upm, fun->io_base, cmd);
  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. fun_wait_rnb(fun);
  97. }
  98. }
  99. static int __devinit fun_chip_init(struct fsl_upm_nand *fun,
  100. const struct device_node *upm_np,
  101. const struct resource *io_res)
  102. {
  103. int ret;
  104. struct device_node *flash_np;
  105. #ifdef CONFIG_MTD_PARTITIONS
  106. static const char *part_types[] = { "cmdlinepart", NULL, };
  107. #endif
  108. fun->chip.IO_ADDR_R = fun->io_base;
  109. fun->chip.IO_ADDR_W = fun->io_base;
  110. fun->chip.cmd_ctrl = fun_cmd_ctrl;
  111. fun->chip.chip_delay = fun->chip_delay;
  112. fun->chip.read_byte = fun_read_byte;
  113. fun->chip.read_buf = fun_read_buf;
  114. fun->chip.write_buf = fun_write_buf;
  115. fun->chip.ecc.mode = NAND_ECC_SOFT;
  116. if (fun->rnb_gpio >= 0)
  117. fun->chip.dev_ready = fun_chip_ready;
  118. fun->mtd.priv = &fun->chip;
  119. fun->mtd.owner = THIS_MODULE;
  120. flash_np = of_get_next_child(upm_np, NULL);
  121. if (!flash_np)
  122. return -ENODEV;
  123. fun->mtd.name = kasprintf(GFP_KERNEL, "%x.%s", io_res->start,
  124. flash_np->name);
  125. if (!fun->mtd.name) {
  126. ret = -ENOMEM;
  127. goto err;
  128. }
  129. ret = nand_scan(&fun->mtd, 1);
  130. if (ret)
  131. goto err;
  132. #ifdef CONFIG_MTD_PARTITIONS
  133. ret = parse_mtd_partitions(&fun->mtd, part_types, &fun->parts, 0);
  134. #ifdef CONFIG_MTD_OF_PARTS
  135. if (ret == 0) {
  136. ret = of_mtd_parse_partitions(fun->dev, flash_np, &fun->parts);
  137. if (ret < 0)
  138. goto err;
  139. }
  140. #endif
  141. if (ret > 0)
  142. ret = add_mtd_partitions(&fun->mtd, fun->parts, ret);
  143. else
  144. #endif
  145. ret = add_mtd_device(&fun->mtd);
  146. err:
  147. of_node_put(flash_np);
  148. return ret;
  149. }
  150. static int __devinit fun_probe(struct of_device *ofdev,
  151. const struct of_device_id *ofid)
  152. {
  153. struct fsl_upm_nand *fun;
  154. struct resource io_res;
  155. const uint32_t *prop;
  156. int ret;
  157. int size;
  158. fun = kzalloc(sizeof(*fun), GFP_KERNEL);
  159. if (!fun)
  160. return -ENOMEM;
  161. ret = of_address_to_resource(ofdev->node, 0, &io_res);
  162. if (ret) {
  163. dev_err(&ofdev->dev, "can't get IO base\n");
  164. goto err1;
  165. }
  166. ret = fsl_upm_find(io_res.start, &fun->upm);
  167. if (ret) {
  168. dev_err(&ofdev->dev, "can't find UPM\n");
  169. goto err1;
  170. }
  171. prop = of_get_property(ofdev->node, "fsl,upm-addr-offset", &size);
  172. if (!prop || size != sizeof(uint32_t)) {
  173. dev_err(&ofdev->dev, "can't get UPM address offset\n");
  174. ret = -EINVAL;
  175. goto err2;
  176. }
  177. fun->upm_addr_offset = *prop;
  178. prop = of_get_property(ofdev->node, "fsl,upm-cmd-offset", &size);
  179. if (!prop || size != sizeof(uint32_t)) {
  180. dev_err(&ofdev->dev, "can't get UPM command offset\n");
  181. ret = -EINVAL;
  182. goto err2;
  183. }
  184. fun->upm_cmd_offset = *prop;
  185. fun->rnb_gpio = of_get_gpio(ofdev->node, 0);
  186. if (fun->rnb_gpio >= 0) {
  187. ret = gpio_request(fun->rnb_gpio, ofdev->dev.bus_id);
  188. if (ret) {
  189. dev_err(&ofdev->dev, "can't request RNB gpio\n");
  190. goto err2;
  191. }
  192. gpio_direction_input(fun->rnb_gpio);
  193. } else if (fun->rnb_gpio == -EINVAL) {
  194. dev_err(&ofdev->dev, "specified RNB gpio is invalid\n");
  195. goto err2;
  196. }
  197. prop = of_get_property(ofdev->node, "chip-delay", NULL);
  198. if (prop)
  199. fun->chip_delay = *prop;
  200. else
  201. fun->chip_delay = 50;
  202. fun->io_base = devm_ioremap_nocache(&ofdev->dev, io_res.start,
  203. io_res.end - io_res.start + 1);
  204. if (!fun->io_base) {
  205. ret = -ENOMEM;
  206. goto err2;
  207. }
  208. fun->dev = &ofdev->dev;
  209. fun->last_ctrl = NAND_CLE;
  210. ret = fun_chip_init(fun, ofdev->node, &io_res);
  211. if (ret)
  212. goto err2;
  213. dev_set_drvdata(&ofdev->dev, fun);
  214. return 0;
  215. err2:
  216. if (fun->rnb_gpio >= 0)
  217. gpio_free(fun->rnb_gpio);
  218. err1:
  219. kfree(fun);
  220. return ret;
  221. }
  222. static int __devexit fun_remove(struct of_device *ofdev)
  223. {
  224. struct fsl_upm_nand *fun = dev_get_drvdata(&ofdev->dev);
  225. nand_release(&fun->mtd);
  226. kfree(fun->mtd.name);
  227. if (fun->rnb_gpio >= 0)
  228. gpio_free(fun->rnb_gpio);
  229. kfree(fun);
  230. return 0;
  231. }
  232. static struct of_device_id of_fun_match[] = {
  233. { .compatible = "fsl,upm-nand" },
  234. {},
  235. };
  236. MODULE_DEVICE_TABLE(of, of_fun_match);
  237. static struct of_platform_driver of_fun_driver = {
  238. .name = "fsl,upm-nand",
  239. .match_table = of_fun_match,
  240. .probe = fun_probe,
  241. .remove = __devexit_p(fun_remove),
  242. };
  243. static int __init fun_module_init(void)
  244. {
  245. return of_register_platform_driver(&of_fun_driver);
  246. }
  247. module_init(fun_module_init);
  248. static void __exit fun_module_exit(void)
  249. {
  250. of_unregister_platform_driver(&of_fun_driver);
  251. }
  252. module_exit(fun_module_exit);
  253. MODULE_LICENSE("GPL");
  254. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  255. MODULE_DESCRIPTION("Driver for NAND chips working through Freescale "
  256. "LocalBus User-Programmable Machine");