fsl_upm.c 8.8 KB

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