blob: e651903040b937774e71abbc0d15a35dcaec0f93 [file] [log] [blame]
Anton Vorontsov6ab97e92008-01-14 23:09:32 +03001/*
2 * FSL UPM NAND driver
3 *
4 * Copyright (C) 2007 MontaVista Software, Inc.
5 * Anton Vorontsov <avorontsov@ru.mvista.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 */
12
13#include <config.h>
14
15#if defined(CONFIG_CMD_NAND) && defined(CONFIG_NAND_FSL_UPM)
16#include <common.h>
17#include <asm/io.h>
18#include <asm/errno.h>
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/fsl_upm.h>
21#include <nand.h>
22
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030023static void fsl_upm_start_pattern(struct fsl_upm *upm, u32 pat_offset)
24{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020025 clrsetbits_be32(upm->mxmr, MxMR_MAD_MSK, MxMR_OP_RUNP | pat_offset);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030026}
27
28static void fsl_upm_end_pattern(struct fsl_upm *upm)
29{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020030 clrbits_be32(upm->mxmr, MxMR_OP_RUNP);
31
32 while (in_be32(upm->mxmr) & MxMR_OP_RUNP)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030033 eieio();
34}
35
36static void fsl_upm_run_pattern(struct fsl_upm *upm, int width, u32 cmd)
37{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020038 out_be32(upm->mar, cmd << (32 - width));
39 switch (width) {
40 case 8:
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030041 out_8(upm->io_addr, 0x0);
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020042 break;
43 case 16:
44 out_be16(upm->io_addr, 0x0);
45 break;
46 case 32:
47 out_be32(upm->io_addr, 0x0);
48 break;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030049 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030050}
51
Anton Vorontsov67166032008-06-12 11:10:21 -050052static void fun_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030053{
54 struct nand_chip *chip = mtd->priv;
55 struct fsl_upm_nand *fun = chip->priv;
56
Anton Vorontsov67166032008-06-12 11:10:21 -050057 if (!(ctrl & fun->last_ctrl)) {
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +020058 fsl_upm_end_pattern(&fun->upm);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030059
Anton Vorontsov67166032008-06-12 11:10:21 -050060 if (cmd == NAND_CMD_NONE)
61 return;
62
63 fun->last_ctrl = ctrl & (NAND_ALE | NAND_CLE);
64 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030065
Anton Vorontsov67166032008-06-12 11:10:21 -050066 if (ctrl & NAND_CTRL_CHANGE) {
67 if (ctrl & NAND_ALE)
68 fsl_upm_start_pattern(&fun->upm, fun->upm_addr_offset);
69 else if (ctrl & NAND_CLE)
70 fsl_upm_start_pattern(&fun->upm, fun->upm_cmd_offset);
71 }
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030072
Anton Vorontsov67166032008-06-12 11:10:21 -050073 fsl_upm_run_pattern(&fun->upm, fun->width, cmd);
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030074
Anton Vorontsov67166032008-06-12 11:10:21 -050075 /*
76 * Some boards/chips needs this. At least on MPC8360E-RDK we
77 * need it. Probably weird chip, because I don't see any need
78 * for this on MPC8555E + Samsung K9F1G08U0A. Usually here are
79 * 0-2 unexpected busy states per block read.
80 */
81 if (fun->wait_pattern) {
82 while (!fun->dev_ready())
83 debug("unexpected busy state\n");
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030084 }
85}
86
Anton Vorontsov6ab97e92008-01-14 23:09:32 +030087static u8 nand_read_byte(struct mtd_info *mtd)
88{
89 struct nand_chip *chip = mtd->priv;
90
91 return in_8(chip->IO_ADDR_R);
92}
93
94static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
95{
96 int i;
97 struct nand_chip *chip = mtd->priv;
98
99 for (i = 0; i < len; i++)
100 out_8(chip->IO_ADDR_W, buf[i]);
101}
102
103static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
104{
105 int i;
106 struct nand_chip *chip = mtd->priv;
107
108 for (i = 0; i < len; i++)
109 buf[i] = in_8(chip->IO_ADDR_R);
110}
111
112static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
113{
114 int i;
115 struct nand_chip *chip = mtd->priv;
116
117 for (i = 0; i < len; i++) {
118 if (buf[i] != in_8(chip->IO_ADDR_R))
119 return -EFAULT;
120 }
121
122 return 0;
123}
124
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300125static int nand_dev_ready(struct mtd_info *mtd)
126{
127 struct nand_chip *chip = mtd->priv;
128 struct fsl_upm_nand *fun = chip->priv;
129
130 return fun->dev_ready();
131}
132
133int fsl_upm_nand_init(struct nand_chip *chip, struct fsl_upm_nand *fun)
134{
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +0200135 if (fun->width != 8 && fun->width != 16 && fun->width != 32)
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300136 return -ENOSYS;
137
Anton Vorontsov67166032008-06-12 11:10:21 -0500138 fun->last_ctrl = NAND_CLE;
139
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300140 chip->priv = fun;
141 chip->chip_delay = fun->chip_delay;
Anton Vorontsov67166032008-06-12 11:10:21 -0500142 chip->ecc.mode = NAND_ECC_SOFT;
143 chip->cmd_ctrl = fun_cmd_ctrl;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300144 chip->read_byte = nand_read_byte;
145 chip->read_buf = nand_read_buf;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300146 chip->write_buf = nand_write_buf;
147 chip->verify_buf = nand_verify_buf;
Wolfgang Grandegger0d3058a2008-06-05 13:02:29 +0200148 if (fun->dev_ready)
149 chip->dev_ready = nand_dev_ready;
Anton Vorontsov6ab97e92008-01-14 23:09:32 +0300150
151 return 0;
152}
153#endif /* CONFIG_CMD_NAND */