fsl_upm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 nand_chip chip;
  32. int last_ctrl;
  33. struct mtd_partition *parts;
  34. struct fsl_upm upm;
  35. uint8_t upm_addr_offset;
  36. uint8_t upm_cmd_offset;
  37. void __iomem *io_base;
  38. int rnb_gpio[NAND_MAX_CHIPS];
  39. uint32_t mchip_offsets[NAND_MAX_CHIPS];
  40. uint32_t mchip_count;
  41. uint32_t mchip_number;
  42. int chip_delay;
  43. uint32_t wait_flags;
  44. };
  45. static inline struct fsl_upm_nand *to_fsl_upm_nand(struct mtd_info *mtdinfo)
  46. {
  47. return container_of(mtd_to_nand(mtdinfo), struct fsl_upm_nand,
  48. chip);
  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. struct mtd_info *mtd = nand_to_mtd(&fun->chip);
  62. int cnt = 1000000;
  63. while (--cnt && !fun_chip_ready(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_to_nand(mtd);
  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_to_nand(mtd);
  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 fun_chip_init(struct fsl_upm_nand *fun,
  133. const struct device_node *upm_np,
  134. const struct resource *io_res)
  135. {
  136. struct mtd_info *mtd = nand_to_mtd(&fun->chip);
  137. int ret;
  138. struct device_node *flash_np;
  139. fun->chip.IO_ADDR_R = fun->io_base;
  140. fun->chip.IO_ADDR_W = fun->io_base;
  141. fun->chip.cmd_ctrl = fun_cmd_ctrl;
  142. fun->chip.chip_delay = fun->chip_delay;
  143. fun->chip.read_byte = fun_read_byte;
  144. fun->chip.read_buf = fun_read_buf;
  145. fun->chip.write_buf = fun_write_buf;
  146. fun->chip.ecc.mode = NAND_ECC_SOFT;
  147. fun->chip.ecc.algo = NAND_ECC_HAMMING;
  148. if (fun->mchip_count > 1)
  149. fun->chip.select_chip = fun_select_chip;
  150. if (fun->rnb_gpio[0] >= 0)
  151. fun->chip.dev_ready = fun_chip_ready;
  152. mtd->dev.parent = fun->dev;
  153. flash_np = of_get_next_child(upm_np, NULL);
  154. if (!flash_np)
  155. return -ENODEV;
  156. nand_set_flash_node(&fun->chip, flash_np);
  157. mtd->name = kasprintf(GFP_KERNEL, "0x%llx.%s", (u64)io_res->start,
  158. flash_np->name);
  159. if (!mtd->name) {
  160. ret = -ENOMEM;
  161. goto err;
  162. }
  163. ret = nand_scan(mtd, fun->mchip_count);
  164. if (ret)
  165. goto err;
  166. ret = mtd_device_register(mtd, NULL, 0);
  167. err:
  168. of_node_put(flash_np);
  169. if (ret)
  170. kfree(mtd->name);
  171. return ret;
  172. }
  173. static int fun_probe(struct platform_device *ofdev)
  174. {
  175. struct fsl_upm_nand *fun;
  176. struct resource io_res;
  177. const __be32 *prop;
  178. int rnb_gpio;
  179. int ret;
  180. int size;
  181. int i;
  182. fun = kzalloc(sizeof(*fun), GFP_KERNEL);
  183. if (!fun)
  184. return -ENOMEM;
  185. ret = of_address_to_resource(ofdev->dev.of_node, 0, &io_res);
  186. if (ret) {
  187. dev_err(&ofdev->dev, "can't get IO base\n");
  188. goto err1;
  189. }
  190. ret = fsl_upm_find(io_res.start, &fun->upm);
  191. if (ret) {
  192. dev_err(&ofdev->dev, "can't find UPM\n");
  193. goto err1;
  194. }
  195. prop = of_get_property(ofdev->dev.of_node, "fsl,upm-addr-offset",
  196. &size);
  197. if (!prop || size != sizeof(uint32_t)) {
  198. dev_err(&ofdev->dev, "can't get UPM address offset\n");
  199. ret = -EINVAL;
  200. goto err1;
  201. }
  202. fun->upm_addr_offset = *prop;
  203. prop = of_get_property(ofdev->dev.of_node, "fsl,upm-cmd-offset", &size);
  204. if (!prop || size != sizeof(uint32_t)) {
  205. dev_err(&ofdev->dev, "can't get UPM command offset\n");
  206. ret = -EINVAL;
  207. goto err1;
  208. }
  209. fun->upm_cmd_offset = *prop;
  210. prop = of_get_property(ofdev->dev.of_node,
  211. "fsl,upm-addr-line-cs-offsets", &size);
  212. if (prop && (size / sizeof(uint32_t)) > 0) {
  213. fun->mchip_count = size / sizeof(uint32_t);
  214. if (fun->mchip_count >= NAND_MAX_CHIPS) {
  215. dev_err(&ofdev->dev, "too much multiple chips\n");
  216. goto err1;
  217. }
  218. for (i = 0; i < fun->mchip_count; i++)
  219. fun->mchip_offsets[i] = be32_to_cpu(prop[i]);
  220. } else {
  221. fun->mchip_count = 1;
  222. }
  223. for (i = 0; i < fun->mchip_count; i++) {
  224. fun->rnb_gpio[i] = -1;
  225. rnb_gpio = of_get_gpio(ofdev->dev.of_node, i);
  226. if (rnb_gpio >= 0) {
  227. ret = gpio_request(rnb_gpio, dev_name(&ofdev->dev));
  228. if (ret) {
  229. dev_err(&ofdev->dev,
  230. "can't request RNB gpio #%d\n", i);
  231. goto err2;
  232. }
  233. gpio_direction_input(rnb_gpio);
  234. fun->rnb_gpio[i] = rnb_gpio;
  235. } else if (rnb_gpio == -EINVAL) {
  236. dev_err(&ofdev->dev, "RNB gpio #%d is invalid\n", i);
  237. goto err2;
  238. }
  239. }
  240. prop = of_get_property(ofdev->dev.of_node, "chip-delay", NULL);
  241. if (prop)
  242. fun->chip_delay = be32_to_cpup(prop);
  243. else
  244. fun->chip_delay = 50;
  245. prop = of_get_property(ofdev->dev.of_node, "fsl,upm-wait-flags", &size);
  246. if (prop && size == sizeof(uint32_t))
  247. fun->wait_flags = be32_to_cpup(prop);
  248. else
  249. fun->wait_flags = FSL_UPM_WAIT_RUN_PATTERN |
  250. FSL_UPM_WAIT_WRITE_BYTE;
  251. fun->io_base = devm_ioremap_nocache(&ofdev->dev, io_res.start,
  252. resource_size(&io_res));
  253. if (!fun->io_base) {
  254. ret = -ENOMEM;
  255. goto err2;
  256. }
  257. fun->dev = &ofdev->dev;
  258. fun->last_ctrl = NAND_CLE;
  259. ret = fun_chip_init(fun, ofdev->dev.of_node, &io_res);
  260. if (ret)
  261. goto err2;
  262. dev_set_drvdata(&ofdev->dev, fun);
  263. return 0;
  264. err2:
  265. for (i = 0; i < fun->mchip_count; i++) {
  266. if (fun->rnb_gpio[i] < 0)
  267. break;
  268. gpio_free(fun->rnb_gpio[i]);
  269. }
  270. err1:
  271. kfree(fun);
  272. return ret;
  273. }
  274. static int fun_remove(struct platform_device *ofdev)
  275. {
  276. struct fsl_upm_nand *fun = dev_get_drvdata(&ofdev->dev);
  277. struct mtd_info *mtd = nand_to_mtd(&fun->chip);
  278. int i;
  279. nand_release(mtd);
  280. kfree(mtd->name);
  281. for (i = 0; i < fun->mchip_count; i++) {
  282. if (fun->rnb_gpio[i] < 0)
  283. break;
  284. gpio_free(fun->rnb_gpio[i]);
  285. }
  286. kfree(fun);
  287. return 0;
  288. }
  289. static const struct of_device_id of_fun_match[] = {
  290. { .compatible = "fsl,upm-nand" },
  291. {},
  292. };
  293. MODULE_DEVICE_TABLE(of, of_fun_match);
  294. static struct platform_driver of_fun_driver = {
  295. .driver = {
  296. .name = "fsl,upm-nand",
  297. .of_match_table = of_fun_match,
  298. },
  299. .probe = fun_probe,
  300. .remove = fun_remove,
  301. };
  302. module_platform_driver(of_fun_driver);
  303. MODULE_LICENSE("GPL");
  304. MODULE_AUTHOR("Anton Vorontsov <avorontsov@ru.mvista.com>");
  305. MODULE_DESCRIPTION("Driver for NAND chips working through Freescale "
  306. "LocalBus User-Programmable Machine");