fsl_upm.c 9.0 KB

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