au1550nd.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2004 Embedded Edge, LLC
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/slab.h>
  7. #include <linux/module.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/mtd/mtd.h>
  10. #include <linux/mtd/rawnand.h>
  11. #include <linux/mtd/partitions.h>
  12. #include <linux/platform_device.h>
  13. #include <asm/io.h>
  14. #include <asm/mach-au1x00/au1000.h>
  15. #include <asm/mach-au1x00/au1550nd.h>
  16. struct au1550nd_ctx {
  17. struct nand_controller controller;
  18. struct nand_chip chip;
  19. int cs;
  20. void __iomem *base;
  21. };
  22. static struct au1550nd_ctx *chip_to_au_ctx(struct nand_chip *this)
  23. {
  24. return container_of(this, struct au1550nd_ctx, chip);
  25. }
  26. /**
  27. * au_write_buf - write buffer to chip
  28. * @this: NAND chip object
  29. * @buf: data buffer
  30. * @len: number of bytes to write
  31. *
  32. * write function for 8bit buswidth
  33. */
  34. static void au_write_buf(struct nand_chip *this, const void *buf,
  35. unsigned int len)
  36. {
  37. struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
  38. const u8 *p = buf;
  39. int i;
  40. for (i = 0; i < len; i++) {
  41. writeb(p[i], ctx->base + MEM_STNAND_DATA);
  42. wmb(); /* drain writebuffer */
  43. }
  44. }
  45. /**
  46. * au_read_buf - read chip data into buffer
  47. * @this: NAND chip object
  48. * @buf: buffer to store date
  49. * @len: number of bytes to read
  50. *
  51. * read function for 8bit buswidth
  52. */
  53. static void au_read_buf(struct nand_chip *this, void *buf,
  54. unsigned int len)
  55. {
  56. struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
  57. u8 *p = buf;
  58. int i;
  59. for (i = 0; i < len; i++) {
  60. p[i] = readb(ctx->base + MEM_STNAND_DATA);
  61. wmb(); /* drain writebuffer */
  62. }
  63. }
  64. /**
  65. * au_write_buf16 - write buffer to chip
  66. * @this: NAND chip object
  67. * @buf: data buffer
  68. * @len: number of bytes to write
  69. *
  70. * write function for 16bit buswidth
  71. */
  72. static void au_write_buf16(struct nand_chip *this, const void *buf,
  73. unsigned int len)
  74. {
  75. struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
  76. const u16 *p = buf;
  77. unsigned int i;
  78. len >>= 1;
  79. for (i = 0; i < len; i++) {
  80. writew(p[i], ctx->base + MEM_STNAND_DATA);
  81. wmb(); /* drain writebuffer */
  82. }
  83. }
  84. /**
  85. * au_read_buf16 - read chip data into buffer
  86. * @this: NAND chip object
  87. * @buf: buffer to store date
  88. * @len: number of bytes to read
  89. *
  90. * read function for 16bit buswidth
  91. */
  92. static void au_read_buf16(struct nand_chip *this, void *buf, unsigned int len)
  93. {
  94. struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
  95. unsigned int i;
  96. u16 *p = buf;
  97. len >>= 1;
  98. for (i = 0; i < len; i++) {
  99. p[i] = readw(ctx->base + MEM_STNAND_DATA);
  100. wmb(); /* drain writebuffer */
  101. }
  102. }
  103. static int find_nand_cs(unsigned long nand_base)
  104. {
  105. void __iomem *base =
  106. (void __iomem *)KSEG1ADDR(AU1000_STATIC_MEM_PHYS_ADDR);
  107. unsigned long addr, staddr, start, mask, end;
  108. int i;
  109. for (i = 0; i < 4; i++) {
  110. addr = 0x1000 + (i * 0x10); /* CSx */
  111. staddr = __raw_readl(base + addr + 0x08); /* STADDRx */
  112. /* figure out the decoded range of this CS */
  113. start = (staddr << 4) & 0xfffc0000;
  114. mask = (staddr << 18) & 0xfffc0000;
  115. end = (start | (start - 1)) & ~(start ^ mask);
  116. if ((nand_base >= start) && (nand_base < end))
  117. return i;
  118. }
  119. return -ENODEV;
  120. }
  121. static int au1550nd_waitrdy(struct nand_chip *this, unsigned int timeout_ms)
  122. {
  123. unsigned long timeout_jiffies = jiffies;
  124. timeout_jiffies += msecs_to_jiffies(timeout_ms) + 1;
  125. do {
  126. if (alchemy_rdsmem(AU1000_MEM_STSTAT) & 0x1)
  127. return 0;
  128. usleep_range(10, 100);
  129. } while (time_before(jiffies, timeout_jiffies));
  130. return -ETIMEDOUT;
  131. }
  132. static int au1550nd_exec_instr(struct nand_chip *this,
  133. const struct nand_op_instr *instr)
  134. {
  135. struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
  136. unsigned int i;
  137. int ret = 0;
  138. switch (instr->type) {
  139. case NAND_OP_CMD_INSTR:
  140. writeb(instr->ctx.cmd.opcode,
  141. ctx->base + MEM_STNAND_CMD);
  142. /* Drain the writebuffer */
  143. wmb();
  144. break;
  145. case NAND_OP_ADDR_INSTR:
  146. for (i = 0; i < instr->ctx.addr.naddrs; i++) {
  147. writeb(instr->ctx.addr.addrs[i],
  148. ctx->base + MEM_STNAND_ADDR);
  149. /* Drain the writebuffer */
  150. wmb();
  151. }
  152. break;
  153. case NAND_OP_DATA_IN_INSTR:
  154. if ((this->options & NAND_BUSWIDTH_16) &&
  155. !instr->ctx.data.force_8bit)
  156. au_read_buf16(this, instr->ctx.data.buf.in,
  157. instr->ctx.data.len);
  158. else
  159. au_read_buf(this, instr->ctx.data.buf.in,
  160. instr->ctx.data.len);
  161. break;
  162. case NAND_OP_DATA_OUT_INSTR:
  163. if ((this->options & NAND_BUSWIDTH_16) &&
  164. !instr->ctx.data.force_8bit)
  165. au_write_buf16(this, instr->ctx.data.buf.out,
  166. instr->ctx.data.len);
  167. else
  168. au_write_buf(this, instr->ctx.data.buf.out,
  169. instr->ctx.data.len);
  170. break;
  171. case NAND_OP_WAITRDY_INSTR:
  172. ret = au1550nd_waitrdy(this, instr->ctx.waitrdy.timeout_ms);
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. if (instr->delay_ns)
  178. ndelay(instr->delay_ns);
  179. return ret;
  180. }
  181. static int au1550nd_exec_op(struct nand_chip *this,
  182. const struct nand_operation *op,
  183. bool check_only)
  184. {
  185. struct au1550nd_ctx *ctx = chip_to_au_ctx(this);
  186. unsigned int i;
  187. int ret;
  188. if (check_only)
  189. return 0;
  190. /* assert (force assert) chip enable */
  191. alchemy_wrsmem((1 << (4 + ctx->cs)), AU1000_MEM_STNDCTL);
  192. /* Drain the writebuffer */
  193. wmb();
  194. for (i = 0; i < op->ninstrs; i++) {
  195. ret = au1550nd_exec_instr(this, &op->instrs[i]);
  196. if (ret)
  197. break;
  198. }
  199. /* deassert chip enable */
  200. alchemy_wrsmem(0, AU1000_MEM_STNDCTL);
  201. /* Drain the writebuffer */
  202. wmb();
  203. return ret;
  204. }
  205. static int au1550nd_attach_chip(struct nand_chip *chip)
  206. {
  207. if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT &&
  208. chip->ecc.algo == NAND_ECC_ALGO_UNKNOWN)
  209. chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
  210. return 0;
  211. }
  212. static const struct nand_controller_ops au1550nd_ops = {
  213. .exec_op = au1550nd_exec_op,
  214. .attach_chip = au1550nd_attach_chip,
  215. };
  216. static int au1550nd_probe(struct platform_device *pdev)
  217. {
  218. struct au1550nd_platdata *pd;
  219. struct au1550nd_ctx *ctx;
  220. struct nand_chip *this;
  221. struct mtd_info *mtd;
  222. struct resource *r;
  223. int ret, cs;
  224. pd = dev_get_platdata(&pdev->dev);
  225. if (!pd) {
  226. dev_err(&pdev->dev, "missing platform data\n");
  227. return -ENODEV;
  228. }
  229. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  230. if (!ctx)
  231. return -ENOMEM;
  232. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  233. if (!r) {
  234. dev_err(&pdev->dev, "no NAND memory resource\n");
  235. ret = -ENODEV;
  236. goto out1;
  237. }
  238. if (request_mem_region(r->start, resource_size(r), "au1550-nand")) {
  239. dev_err(&pdev->dev, "cannot claim NAND memory area\n");
  240. ret = -ENOMEM;
  241. goto out1;
  242. }
  243. ctx->base = ioremap(r->start, 0x1000);
  244. if (!ctx->base) {
  245. dev_err(&pdev->dev, "cannot remap NAND memory area\n");
  246. ret = -ENODEV;
  247. goto out2;
  248. }
  249. this = &ctx->chip;
  250. mtd = nand_to_mtd(this);
  251. mtd->dev.parent = &pdev->dev;
  252. /* figure out which CS# r->start belongs to */
  253. cs = find_nand_cs(r->start);
  254. if (cs < 0) {
  255. dev_err(&pdev->dev, "cannot detect NAND chipselect\n");
  256. ret = -ENODEV;
  257. goto out3;
  258. }
  259. ctx->cs = cs;
  260. nand_controller_init(&ctx->controller);
  261. ctx->controller.ops = &au1550nd_ops;
  262. this->controller = &ctx->controller;
  263. if (pd->devwidth)
  264. this->options |= NAND_BUSWIDTH_16;
  265. /*
  266. * This driver assumes that the default ECC engine should be TYPE_SOFT.
  267. * Set ->engine_type before registering the NAND devices in order to
  268. * provide a driver specific default value.
  269. */
  270. this->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
  271. ret = nand_scan(this, 1);
  272. if (ret) {
  273. dev_err(&pdev->dev, "NAND scan failed with %d\n", ret);
  274. goto out3;
  275. }
  276. mtd_device_register(mtd, pd->parts, pd->num_parts);
  277. platform_set_drvdata(pdev, ctx);
  278. return 0;
  279. out3:
  280. iounmap(ctx->base);
  281. out2:
  282. release_mem_region(r->start, resource_size(r));
  283. out1:
  284. kfree(ctx);
  285. return ret;
  286. }
  287. static int au1550nd_remove(struct platform_device *pdev)
  288. {
  289. struct au1550nd_ctx *ctx = platform_get_drvdata(pdev);
  290. struct resource *r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  291. struct nand_chip *chip = &ctx->chip;
  292. int ret;
  293. ret = mtd_device_unregister(nand_to_mtd(chip));
  294. WARN_ON(ret);
  295. nand_cleanup(chip);
  296. iounmap(ctx->base);
  297. release_mem_region(r->start, 0x1000);
  298. kfree(ctx);
  299. return 0;
  300. }
  301. static struct platform_driver au1550nd_driver = {
  302. .driver = {
  303. .name = "au1550-nand",
  304. },
  305. .probe = au1550nd_probe,
  306. .remove = au1550nd_remove,
  307. };
  308. module_platform_driver(au1550nd_driver);
  309. MODULE_LICENSE("GPL");
  310. MODULE_AUTHOR("Embedded Edge, LLC");
  311. MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");