fsl_upm.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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_address.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/io.h>
  24. #include <linux/slab.h>
  25. #include <asm/fsl_lbc.h>
  26. #define FSL_UPM_WAIT_RUN_PATTERN 0x1
  27. #define FSL_UPM_WAIT_WRITE_BYTE 0x2
  28. #define FSL_UPM_WAIT_WRITE_BUFFER 0x4
  29. struct fsl_upm_nand {
  30. struct device *dev;
  31. struct mtd_info mtd;
  32. struct nand_chip chip;
  33. int last_ctrl;
  34. struct mtd_partition *parts;
  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. static inline struct fsl_upm_nand *to_fsl_upm_nand(struct mtd_info *mtdinfo)
  47. {
  48. return container_of(mtdinfo, struct fsl_upm_nand, mtd);
  49. }
  50. static int fun_chip_ready(struct mtd_info *mtd)
  51. {
  52. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  53. if (gpio_get_value(fun->rnb_gpio[fun->mchip_number]))
  54. return 1;
  55. dev_vdbg(fun->dev, "busy\n");
  56. return 0;
  57. }
  58. static void fun_wait_rnb(struct fsl_upm_nand *fun)
  59. {
  60. if (fun->rnb_gpio[fun->mchip_number] >= 0) {
  61. int cnt = 1000000;
  62. while (--cnt && !fun_chip_ready(&fun->mtd))
  63. cpu_relax();
  64. if (!cnt)
  65. dev_err(fun->dev, "tired waiting for RNB\n");
  66. } else {
  67. ndelay(100);
  68. }
  69. }
  70. static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  71. {
  72. struct nand_chip *chip = mtd->priv;
  73. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  74. u32 mar;
  75. if (!(ctrl & fun->last_ctrl)) {
  76. fsl_upm_end_pattern(&fun->upm);
  77. if (cmd == NAND_CMD_NONE)
  78. return;
  79. fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
  80. }
  81. if (ctrl & NAND_CTRL_CHANGE) {
  82. if (ctrl & NAND_ALE)
  83. fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
  84. else if (ctrl & NAND_CLE)
  85. fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
  86. }
  87. mar = (cmd << (32 - fun->upm.width)) |
  88. fun->mchip_offsets[fun->mchip_number];
  89. fsl_upm_run_pattern(&fun->upm, chip->IO_ADDR_R, mar);
  90. if (fun->wait_flags & FSL_UPM_WAIT_RUN_PATTERN)
  91. fun_wait_rnb(fun);
  92. }
  93. static void fun_select_chip(struct mtd_info *mtd, int mchip_nr)
  94. {
  95. struct nand_chip *chip = mtd->priv;
  96. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  97. if (mchip_nr == -1) {
  98. chip->cmd_ctrl(mtd, NAND_CMD_NONE, 0 | NAND_CTRL_CHANGE);
  99. } else if (mchip_nr >= 0 && mchip_nr < NAND_MAX_CHIPS) {
  100. fun->mchip_number = mchip_nr;
  101. chip->IO_ADDR_R = fun->io_base + fun->mchip_offsets[mchip_nr];
  102. chip->IO_ADDR_W = chip->IO_ADDR_R;
  103. } else {
  104. BUG();
  105. }
  106. }
  107. static uint8_t fun_read_byte(struct mtd_info *mtd)
  108. {
  109. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  110. return in_8(fun->chip.IO_ADDR_R);
  111. }
  112. static void fun_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  113. {
  114. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  115. int i;
  116. for (i = 0; i < len; i++)
  117. buf[i] = in_8(fun->chip.IO_ADDR_R);
  118. }
  119. static void fun_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  120. {
  121. struct fsl_upm_nand *fun = to_fsl_upm_nand(mtd);
  122. int i;
  123. for (i = 0; i < len; i++) {
  124. out_8(fun->chip.IO_ADDR_W, buf[i]);
  125. if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BYTE)
  126. fun_wait_rnb(fun);
  127. }
  128. if (fun->wait_flags & FSL_UPM_WAIT_WRITE_BUFFER)
  129. fun_wait_rnb(fun);
  130. }
  131. static int fun_chip_init(struct fsl_upm_nand *fun,
  132. const struct device_node *upm_np,
  133. const struct resource *io_res)
  134. {
  135. int ret;
  136. struct device_node *flash_np;
  137. struct mtd_part_parser_data ppdata;
  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.dev.parent = fun->dev;
  152. flash_np = of_get_next_child(upm_np, NULL);
  153. if (!flash_np)
  154. return -ENODEV;
  155. fun->mtd.name = kasprintf(GFP_KERNEL, "0x%llx.%s", (u64)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. ppdata.of_node = flash_np;
  165. ret = mtd_device_parse_register(&fun->mtd, NULL, &ppdata, NULL, 0);
  166. err:
  167. of_node_put(flash_np);
  168. if (ret)
  169. kfree(fun->mtd.name);
  170. return ret;
  171. }
  172. static int fun_probe(struct platform_device *ofdev)
  173. {
  174. struct fsl_upm_nand *fun;
  175. struct resource io_res;
  176. const __be32 *prop;
  177. int rnb_gpio;
  178. int ret;
  179. int size;
  180. int i;
  181. fun = kzalloc(sizeof(*fun), GFP_KERNEL);
  182. if (!fun)
  183. return -ENOMEM;
  184. ret = of_address_to_resource(ofdev->dev.of_node, 0, &io_res);
  185. if (ret) {
  186. dev_err(&ofdev->dev, "can't get IO base\n");
  187. goto err1;
  188. }
  189. ret = fsl_upm_find(io_res.start, &fun->upm);
  190. if (ret) {
  191. dev_err(&ofdev->dev, "can't find UPM\n");
  192. goto err1;
  193. }
  194. prop = of_get_property(ofdev->dev.of_node, "fsl,upm-addr-offset",
  195. &size);
  196. if (!prop || size != sizeof(uint32_t)) {
  197. dev_err(&ofdev->dev, "can't get UPM address offset\n");
  198. ret = -EINVAL;
  199. goto err1;
  200. }
  201. fun->upm_addr_offset = *prop;
  202. prop = of_get_property(ofdev->dev.of_node, "fsl,upm-cmd-offset", &size);
  203. if (!prop || size != sizeof(uint32_t)) {
  204. dev_err(&ofdev->dev, "can't get UPM command offset\n");
  205. ret = -EINVAL;
  206. goto err1;
  207. }
  208. fun->upm_cmd_offset = *prop;
  209. prop = of_get_property(ofdev->dev.of_node,
  210. "fsl,upm-addr-line-cs-offsets", &size);
  211. if (prop && (size / sizeof(uint32_t)) > 0) {
  212. fun->mchip_count = size / sizeof(uint32_t);
  213. if (fun->mchip_count >= NAND_MAX_CHIPS) {
  214. dev_err(&ofdev->dev, "too much multiple chips\n");
  215. goto err1;
  216. }
  217. for (i = 0; i < fun->mchip_count; i++)
  218. fun->mchip_offsets[i] = be32_to_cpu(prop[i]);
  219. } else {
  220. fun->mchip_count = 1;
  221. }
  222. for (i = 0; i < fun->mchip_count; i++) {
  223. fun->rnb_gpio[i] = -1;
  224. rnb_gpio = of_get_gpio(ofdev->dev.of_node, i);
  225. if (rnb_gpio >= 0) {
  226. ret = gpio_request(rnb_gpio, dev_name(&ofdev->dev));
  227. if (ret) {
  228. dev_err(&ofdev->dev,
  229. "can't request RNB gpio #%d\n", i);
  230. goto err2;
  231. }
  232. gpio_direction_input(rnb_gpio);
  233. fun->rnb_gpio[i] = rnb_gpio;
  234. } else if (rnb_gpio == -EINVAL) {
  235. dev_err(&ofdev->dev, "RNB gpio #%d is invalid\n", i);
  236. goto err2;
  237. }
  238. }
  239. prop = of_get_property(ofdev->dev.of_node, "chip-delay", NULL);
  240. if (prop)
  241. fun->chip_delay = be32_to_cpup(prop);
  242. else
  243. fun->chip_delay = 50;
  244. prop = of_get_property(ofdev->dev.of_node, "fsl,upm-wait-flags", &size);
  245. if (prop && size == sizeof(uint32_t))
  246. fun->wait_flags = be32_to_cpup(prop);
  247. else
  248. fun->wait_flags = FSL_UPM_WAIT_RUN_PATTERN |
  249. FSL_UPM_WAIT_WRITE_BYTE;
  250. fun->io_base = devm_ioremap_nocache(&ofdev->dev, io_res.start,
  251. resource_size(&io_res));
  252. if (!fun->io_base) {
  253. ret = -ENOMEM;
  254. goto err2;
  255. }
  256. fun->dev = &ofdev->dev;
  257. fun->last_ctrl = NAND_CLE;
  258. ret = fun_chip_init(fun, ofdev->dev.of_node, &io_res);
  259. if (ret)
  260. goto err2;
  261. dev_set_drvdata(&ofdev->dev, fun);
  262. return 0;
  263. err2:
  264. for (i = 0; i < fun->mchip_count; i++) {
  265. if (fun->rnb_gpio[i] < 0)
  266. break;
  267. gpio_free(fun->rnb_gpio[i]);
  268. }
  269. err1:
  270. kfree(fun);
  271. return ret;
  272. }
  273. static int fun_remove(struct platform_device *ofdev)
  274. {
  275. struct fsl_upm_nand *fun = dev_get_drvdata(&ofdev->dev);
  276. int i;
  277. nand_release(&fun->mtd);
  278. kfree(fun->mtd.name);
  279. for (i = 0; i < fun->mchip_count; i++) {
  280. if (fun->rnb_gpio[i] < 0)
  281. break;
  282. gpio_free(fun->rnb_gpio[i]);
  283. }
  284. kfree(fun);
  285. return 0;
  286. }
  287. static const struct of_device_id of_fun_match[] = {
  288. { .compatible = "fsl,upm-nand" },
  289. {},
  290. };
  291. MODULE_DEVICE_TABLE(of, of_fun_match);
  292. static struct platform_driver of_fun_driver = {
  293. .driver = {
  294. .name = "fsl,upm-nand",
  295. .of_match_table = of_fun_match,
  296. },
  297. .probe = fun_probe,
  298. .remove = fun_remove,
  299. };
  300. module_platform_driver(of_fun_driver);
  301. MODULE_LICENSE("GPL");
  302. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  303. MODULE_DESCRIPTION("Driver for NAND chips working through Freescale "
  304. "LocalBus User-Programmable Machine");