fsl_upm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 <linux/slab.h>
  24. #include <asm/fsl_lbc.h>
  25. #define FSL_UPM_WAIT_RUN_PATTERN 0x1
  26. #define FSL_UPM_WAIT_WRITE_BYTE 0x2
  27. #define FSL_UPM_WAIT_WRITE_BUFFER 0x4
  28. struct fsl_upm_nand {
  29. struct device *dev;
  30. struct mtd_info mtd;
  31. struct nand_chip chip;
  32. int last_ctrl;
  33. #ifdef CONFIG_MTD_PARTITIONS
  34. struct mtd_partition *parts;
  35. #endif
  36. struct fsl_upm upm;
  37. uint8_t upm_addr_offset;
  38. uint8_t upm_cmd_offset;
  39. void __iomem *io_base;
  40. int rnb_gpio[NAND_MAX_CHIPS];
  41. uint32_t mchip_offsets[NAND_MAX_CHIPS];
  42. uint32_t mchip_count;
  43. uint32_t mchip_number;
  44. int chip_delay;
  45. uint32_t wait_flags;
  46. };
  47. #define to_fsl_upm_nand(mtd) container_of(mtd, struct fsl_upm_nand, mtd)
  48. static int fun_chip_ready(struct mtd_info *mtd)
  49. {
  50. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  51. if (gpio_get_value(fun->rnb_gpio[fun->mchip_number]))
  52. return 1;
  53. dev_vdbg(fun->dev, "busy\n");
  54. return 0;
  55. }
  56. static void fun_wait_rnb(struct fsl_upm_nand *fun)
  57. {
  58. if (fun->rnb_gpio[fun->mchip_number] >= 0) {
  59. int cnt = 1000000;
  60. while (--cnt && !fun_chip_ready(&fun->mtd))
  61. cpu_relax();
  62. if (!cnt)
  63. dev_err(fun->dev, "tired waiting for RNB\n");
  64. } else {
  65. ndelay(100);
  66. }
  67. }
  68. static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  69. {
  70. struct nand_chip *chip = mtd->priv;
  71. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  72. u32 mar;
  73. if (!(ctrl & fun->last_ctrl)) {
  74. fsl_upm_end_pattern(&fun->upm);
  75. if (cmd == NAND_CMD_NONE)
  76. return;
  77. fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
  78. }
  79. if (ctrl & NAND_CTRL_CHANGE) {
  80. if (ctrl & NAND_ALE)
  81. fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
  82. else if (ctrl & NAND_CLE)
  83. fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
  84. }
  85. mar = (cmd << (32 - fun->upm.width)) |
  86. fun->mchip_offsets[fun->mchip_number];
  87. fsl_upm_run_pattern(&fun->upm, chip->IO_ADDR_R, mar);
  88. if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
  89. fun_wait_rnb(fun);
  90. }
  91. static void fun_select_chip(struct mtd_info *mtd, int mchip_nr)
  92. {
  93. struct nand_chip *chip = mtd->priv;
  94. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  95. if (mchip_nr == -1) {
  96. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  97. } else if (mchip_nr >= 0 && mchip_nr < NAND_MAX_CHIPS) {
  98. fun->mchip_number = mchip_nr;
  99. chip->IO_ADDR_R = fun->io_base + fun->mchip_offsets[mchip_nr];
  100. chip->IO_ADDR_W = chip->IO_ADDR_R;
  101. } else {
  102. BUG();
  103. }
  104. }
  105. static uint8_t fun_read_byte(struct mtd_info *mtd)
  106. {
  107. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  108. return in_8(fun->chip.IO_ADDR_R);
  109. }
  110. static void fun_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  111. {
  112. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  113. int i;
  114. for (i = 0; i < len; i++)
  115. buf[i] = in_8(fun->chip.IO_ADDR_R);
  116. }
  117. static void fun_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  118. {
  119. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  120. int i;
  121. for (i = 0; i < len; i++) {
  122. out_8(fun->chip.IO_ADDR_W, buf[i]);
  123. if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
  124. fun_wait_rnb(fun);
  125. }
  126. if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
  127. fun_wait_rnb(fun);
  128. }
  129. static int __devinit fun_chip_init(struct fsl_upm_nand *fun,
  130. const struct device_node *upm_np,
  131. const struct resource *io_res)
  132. {
  133. int ret;
  134. struct device_node *flash_np;
  135. #ifdef CONFIG_MTD_PARTITIONS
  136. static const char *part_types[] = { "cmdlinepart", NULL, };
  137. #endif
  138. fun->chip.IO_ADDR_R = fun->io_base;
  139. fun->chip.IO_ADDR_W = fun->io_base;
  140. fun->chip.cmd_ctrl = fun_cmd_ctrl;
  141. fun->chip.chip_delay = fun->chip_delay;
  142. fun->chip.read_byte = fun_read_byte;
  143. fun->chip.read_buf = fun_read_buf;
  144. fun->chip.write_buf = fun_write_buf;
  145. fun->chip.ecc.mode = NAND_ECC_SOFT;
  146. if (fun->mchip_count > 1)
  147. fun->chip.select_chip = fun_select_chip;
  148. if (fun->rnb_gpio[0] >= 0)
  149. fun->chip.dev_ready = fun_chip_ready;
  150. fun->mtd.priv = &fun->chip;
  151. fun->mtd.owner = THIS_MODULE;
  152. flash_np = of_get_next_child(upm_np, NULL);
  153. if (!flash_np)
  154. return -ENODEV;
  155. fun->mtd.name = kasprintf(GFP_KERNEL, "%x.%s", io_res->start,
  156. flash_np->name);
  157. if (!fun->mtd.name) {
  158. ret = -ENOMEM;
  159. goto err;
  160. }
  161. ret = nand_scan(&fun->mtd, fun->mchip_count);
  162. if (ret)
  163. goto err;
  164. #ifdef CONFIG_MTD_PARTITIONS
  165. ret = parse_mtd_partitions(&fun->mtd, part_types, &fun->parts, 0);
  166. #ifdef CONFIG_MTD_OF_PARTS
  167. if (ret == 0) {
  168. ret = of_mtd_parse_partitions(fun->dev, flash_np, &fun->parts);
  169. if (ret < 0)
  170. goto err;
  171. }
  172. #endif
  173. if (ret > 0)
  174. ret = add_mtd_partitions(&fun->mtd, fun->parts, ret);
  175. else
  176. #endif
  177. ret = add_mtd_device(&fun->mtd);
  178. err:
  179. of_node_put(flash_np);
  180. return ret;
  181. }
  182. static int __devinit fun_probe(struct of_device *ofdev,
  183. const struct of_device_id *ofid)
  184. {
  185. struct fsl_upm_nand *fun;
  186. struct resource io_res;
  187. const uint32_t *prop;
  188. int rnb_gpio;
  189. int ret;
  190. int size;
  191. int i;
  192. fun = kzalloc(sizeof(*fun), GFP_KERNEL);
  193. if (!fun)
  194. return -ENOMEM;
  195. ret = of_address_to_resource(ofdev->node, 0, &io_res);
  196. if (ret) {
  197. dev_err(&ofdev->dev, "can't get IO base\n");
  198. goto err1;
  199. }
  200. ret = fsl_upm_find(io_res.start, &fun->upm);
  201. if (ret) {
  202. dev_err(&ofdev->dev, "can't find UPM\n");
  203. goto err1;
  204. }
  205. prop = of_get_property(ofdev->node, "fsl,upm-addr-offset", &size);
  206. if (!prop || size != sizeof(uint32_t)) {
  207. dev_err(&ofdev->dev, "can't get UPM address offset\n");
  208. ret = -EINVAL;
  209. goto err1;
  210. }
  211. fun->upm_addr_offset = *prop;
  212. prop = of_get_property(ofdev->node, "fsl,upm-cmd-offset", &size);
  213. if (!prop || size != sizeof(uint32_t)) {
  214. dev_err(&ofdev->dev, "can't get UPM command offset\n");
  215. ret = -EINVAL;
  216. goto err1;
  217. }
  218. fun->upm_cmd_offset = *prop;
  219. prop = of_get_property(ofdev->node,
  220. "fsl,upm-addr-line-cs-offsets", &size);
  221. if (prop && (size / sizeof(uint32_t)) > 0) {
  222. fun->mchip_count = size / sizeof(uint32_t);
  223. if (fun->mchip_count >= NAND_MAX_CHIPS) {
  224. dev_err(&ofdev->dev, "too much multiple chips\n");
  225. goto err1;
  226. }
  227. for (i = 0; i < fun->mchip_count; i++)
  228. fun->mchip_offsets[i] = prop[i];
  229. } else {
  230. fun->mchip_count = 1;
  231. }
  232. for (i = 0; i < fun->mchip_count; i++) {
  233. fun->rnb_gpio[i] = -1;
  234. rnb_gpio = of_get_gpio(ofdev->node, i);
  235. if (rnb_gpio >= 0) {
  236. ret = gpio_request(rnb_gpio, dev_name(&ofdev->dev));
  237. if (ret) {
  238. dev_err(&ofdev->dev,
  239. "can't request RNB gpio #%d\n", i);
  240. goto err2;
  241. }
  242. gpio_direction_input(rnb_gpio);
  243. fun->rnb_gpio[i] = rnb_gpio;
  244. } else if (rnb_gpio == -EINVAL) {
  245. dev_err(&ofdev->dev, "RNB gpio #%d is invalid\n", i);
  246. goto err2;
  247. }
  248. }
  249. prop = of_get_property(ofdev->node, "chip-delay", NULL);
  250. if (prop)
  251. fun->chip_delay = *prop;
  252. else
  253. fun->chip_delay = 50;
  254. prop = of_get_property(ofdev->node, "fsl,upm-wait-flags", &size);
  255. if (prop && size == sizeof(uint32_t))
  256. fun->wait_flags = *prop;
  257. else
  258. fun->wait_flags = FSL_UPM_WAIT_RUN_PATTERN |
  259. FSL_UPM_WAIT_WRITE_BYTE;
  260. fun->io_base = devm_ioremap_nocache(&ofdev->dev, io_res.start,
  261. io_res.end - io_res.start + 1);
  262. if (!fun->io_base) {
  263. ret = -ENOMEM;
  264. goto err2;
  265. }
  266. fun->dev = &ofdev->dev;
  267. fun->last_ctrl = NAND_CLE;
  268. ret = fun_chip_init(fun, ofdev->node, &io_res);
  269. if (ret)
  270. goto err2;
  271. dev_set_drvdata(&ofdev->dev, fun);
  272. return 0;
  273. err2:
  274. for (i = 0; i < fun->mchip_count; i++) {
  275. if (fun->rnb_gpio[i] < 0)
  276. break;
  277. gpio_free(fun->rnb_gpio[i]);
  278. }
  279. err1:
  280. kfree(fun);
  281. return ret;
  282. }
  283. static int __devexit fun_remove(struct of_device *ofdev)
  284. {
  285. struct fsl_upm_nand *fun = dev_get_drvdata(&ofdev->dev);
  286. int i;
  287. nand_release(&fun->mtd);
  288. kfree(fun->mtd.name);
  289. for (i = 0; i < fun->mchip_count; i++) {
  290. if (fun->rnb_gpio[i] < 0)
  291. break;
  292. gpio_free(fun->rnb_gpio[i]);
  293. }
  294. kfree(fun);
  295. return 0;
  296. }
  297. static struct of_device_id of_fun_match[] = {
  298. { .compatible = "fsl,upm-nand" },
  299. {},
  300. };
  301. MODULE_DEVICE_TABLE(of, of_fun_match);
  302. static struct of_platform_driver of_fun_driver = {
  303. .name = "fsl,upm-nand",
  304. .match_table = of_fun_match,
  305. .probe = fun_probe,
  306. .remove = __devexit_p(fun_remove),
  307. };
  308. static int __init fun_module_init(void)
  309. {
  310. return of_register_platform_driver(&of_fun_driver);
  311. }
  312. module_init(fun_module_init);
  313. static void __exit fun_module_exit(void)
  314. {
  315. of_unregister_platform_driver(&of_fun_driver);
  316. }
  317. module_exit(fun_module_exit);
  318. MODULE_LICENSE("GPL");
  319. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  320. MODULE_DESCRIPTION("Driver for NAND chips working through Freescale "
  321. "LocalBus User-Programmable Machine");