~ubuntu-branches/debian/wheezy/linux-2.6/wheezy

« back to all changes in this revision

Viewing changes to drivers/mtd/nand/mxc_nand.c

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings, Ben Hutchings, Aurelien Jarno, Martin Michlmayr
  • Date: 2011-04-06 13:53:30 UTC
  • mfrom: (43.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110406135330-wjufxhd0tvn3zx4z
Tags: 2.6.38-3
[ Ben Hutchings ]
* [ppc64] Add to linux-tools package architectures (Closes: #620124)
* [amd64] Save cr4 to mmu_cr4_features at boot time (Closes: #620284)
* appletalk: Fix bugs introduced when removing use of BKL
* ALSA: Fix yet another race in disconnection
* cciss: Fix lost command issue
* ath9k: Fix kernel panic in AR2427
* ses: Avoid kernel panic when lun 0 is not mapped
* PCI/ACPI: Report ASPM support to BIOS if not disabled from command line

[ Aurelien Jarno ]
* rtlwifi: fix build when PCI is not enabled.

[ Martin Michlmayr ]
* rtlwifi: Eliminate udelay calls with too large values (Closes: #620204)

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include <linux/clk.h>
31
31
#include <linux/err.h>
32
32
#include <linux/io.h>
 
33
#include <linux/irq.h>
 
34
#include <linux/completion.h>
33
35
 
34
36
#include <asm/mach/flash.h>
35
37
#include <mach/mxc_nand.h>
 
38
#include <mach/hardware.h>
36
39
 
37
40
#define DRIVER_NAME "mxc_nand"
38
41
 
 
42
#define nfc_is_v21()            (cpu_is_mx25() || cpu_is_mx35())
 
43
#define nfc_is_v1()             (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
 
44
#define nfc_is_v3_2()           cpu_is_mx51()
 
45
#define nfc_is_v3()             nfc_is_v3_2()
 
46
 
39
47
/* Addresses for NFC registers */
40
 
#define NFC_BUF_SIZE            0xE00
41
 
#define NFC_BUF_ADDR            0xE04
42
 
#define NFC_FLASH_ADDR          0xE06
43
 
#define NFC_FLASH_CMD           0xE08
44
 
#define NFC_CONFIG              0xE0A
45
 
#define NFC_ECC_STATUS_RESULT   0xE0C
46
 
#define NFC_RSLTMAIN_AREA       0xE0E
47
 
#define NFC_RSLTSPARE_AREA      0xE10
48
 
#define NFC_WRPROT              0xE12
49
 
#define NFC_UNLOCKSTART_BLKADDR 0xE14
50
 
#define NFC_UNLOCKEND_BLKADDR   0xE16
51
 
#define NFC_NF_WRPRST           0xE18
52
 
#define NFC_CONFIG1             0xE1A
53
 
#define NFC_CONFIG2             0xE1C
54
 
 
55
 
/* Addresses for NFC RAM BUFFER Main area 0 */
56
 
#define MAIN_AREA0              0x000
57
 
#define MAIN_AREA1              0x200
58
 
#define MAIN_AREA2              0x400
59
 
#define MAIN_AREA3              0x600
60
 
 
61
 
/* Addresses for NFC SPARE BUFFER Spare area 0 */
62
 
#define SPARE_AREA0             0x800
63
 
#define SPARE_AREA1             0x810
64
 
#define SPARE_AREA2             0x820
65
 
#define SPARE_AREA3             0x830
66
 
 
67
 
/* Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register
68
 
 * for Command operation */
69
 
#define NFC_CMD            0x1
70
 
 
71
 
/* Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register
72
 
 * for Address operation */
73
 
#define NFC_ADDR           0x2
74
 
 
75
 
/* Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register
76
 
 * for Input operation */
77
 
#define NFC_INPUT          0x4
78
 
 
79
 
/* Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register
80
 
 * for Data Output operation */
81
 
#define NFC_OUTPUT         0x8
82
 
 
83
 
/* Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register
84
 
 * for Read ID operation */
85
 
#define NFC_ID             0x10
86
 
 
87
 
/* Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register
88
 
 * for Read Status operation */
89
 
#define NFC_STATUS         0x20
90
 
 
91
 
/* Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read
92
 
 * Status operation */
93
 
#define NFC_INT            0x8000
94
 
 
95
 
#define NFC_SP_EN           (1 << 2)
96
 
#define NFC_ECC_EN          (1 << 3)
97
 
#define NFC_INT_MSK         (1 << 4)
98
 
#define NFC_BIG             (1 << 5)
99
 
#define NFC_RST             (1 << 6)
100
 
#define NFC_CE              (1 << 7)
101
 
#define NFC_ONE_CYCLE       (1 << 8)
 
48
#define NFC_V1_V2_BUF_SIZE              (host->regs + 0x00)
 
49
#define NFC_V1_V2_BUF_ADDR              (host->regs + 0x04)
 
50
#define NFC_V1_V2_FLASH_ADDR            (host->regs + 0x06)
 
51
#define NFC_V1_V2_FLASH_CMD             (host->regs + 0x08)
 
52
#define NFC_V1_V2_CONFIG                (host->regs + 0x0a)
 
53
#define NFC_V1_V2_ECC_STATUS_RESULT     (host->regs + 0x0c)
 
54
#define NFC_V1_V2_RSLTMAIN_AREA         (host->regs + 0x0e)
 
55
#define NFC_V1_V2_RSLTSPARE_AREA        (host->regs + 0x10)
 
56
#define NFC_V1_V2_WRPROT                (host->regs + 0x12)
 
57
#define NFC_V1_UNLOCKSTART_BLKADDR      (host->regs + 0x14)
 
58
#define NFC_V1_UNLOCKEND_BLKADDR        (host->regs + 0x16)
 
59
#define NFC_V21_UNLOCKSTART_BLKADDR     (host->regs + 0x20)
 
60
#define NFC_V21_UNLOCKEND_BLKADDR       (host->regs + 0x22)
 
61
#define NFC_V1_V2_NF_WRPRST             (host->regs + 0x18)
 
62
#define NFC_V1_V2_CONFIG1               (host->regs + 0x1a)
 
63
#define NFC_V1_V2_CONFIG2               (host->regs + 0x1c)
 
64
 
 
65
#define NFC_V2_CONFIG1_ECC_MODE_4       (1 << 0)
 
66
#define NFC_V1_V2_CONFIG1_SP_EN         (1 << 2)
 
67
#define NFC_V1_V2_CONFIG1_ECC_EN        (1 << 3)
 
68
#define NFC_V1_V2_CONFIG1_INT_MSK       (1 << 4)
 
69
#define NFC_V1_V2_CONFIG1_BIG           (1 << 5)
 
70
#define NFC_V1_V2_CONFIG1_RST           (1 << 6)
 
71
#define NFC_V1_V2_CONFIG1_CE            (1 << 7)
 
72
#define NFC_V2_CONFIG1_ONE_CYCLE        (1 << 8)
 
73
#define NFC_V2_CONFIG1_PPB(x)           (((x) & 0x3) << 9)
 
74
#define NFC_V2_CONFIG1_FP_INT           (1 << 11)
 
75
 
 
76
#define NFC_V1_V2_CONFIG2_INT           (1 << 15)
 
77
 
 
78
/*
 
79
 * Operation modes for the NFC. Valid for v1, v2 and v3
 
80
 * type controllers.
 
81
 */
 
82
#define NFC_CMD                         (1 << 0)
 
83
#define NFC_ADDR                        (1 << 1)
 
84
#define NFC_INPUT                       (1 << 2)
 
85
#define NFC_OUTPUT                      (1 << 3)
 
86
#define NFC_ID                          (1 << 4)
 
87
#define NFC_STATUS                      (1 << 5)
 
88
 
 
89
#define NFC_V3_FLASH_CMD                (host->regs_axi + 0x00)
 
90
#define NFC_V3_FLASH_ADDR0              (host->regs_axi + 0x04)
 
91
 
 
92
#define NFC_V3_CONFIG1                  (host->regs_axi + 0x34)
 
93
#define NFC_V3_CONFIG1_SP_EN            (1 << 0)
 
94
#define NFC_V3_CONFIG1_RBA(x)           (((x) & 0x7 ) << 4)
 
95
 
 
96
#define NFC_V3_ECC_STATUS_RESULT        (host->regs_axi + 0x38)
 
97
 
 
98
#define NFC_V3_LAUNCH                   (host->regs_axi + 0x40)
 
99
 
 
100
#define NFC_V3_WRPROT                   (host->regs_ip + 0x0)
 
101
#define NFC_V3_WRPROT_LOCK_TIGHT        (1 << 0)
 
102
#define NFC_V3_WRPROT_LOCK              (1 << 1)
 
103
#define NFC_V3_WRPROT_UNLOCK            (1 << 2)
 
104
#define NFC_V3_WRPROT_BLS_UNLOCK        (2 << 6)
 
105
 
 
106
#define NFC_V3_WRPROT_UNLOCK_BLK_ADD0   (host->regs_ip + 0x04)
 
107
 
 
108
#define NFC_V3_CONFIG2                  (host->regs_ip + 0x24)
 
109
#define NFC_V3_CONFIG2_PS_512                   (0 << 0)
 
110
#define NFC_V3_CONFIG2_PS_2048                  (1 << 0)
 
111
#define NFC_V3_CONFIG2_PS_4096                  (2 << 0)
 
112
#define NFC_V3_CONFIG2_ONE_CYCLE                (1 << 2)
 
113
#define NFC_V3_CONFIG2_ECC_EN                   (1 << 3)
 
114
#define NFC_V3_CONFIG2_2CMD_PHASES              (1 << 4)
 
115
#define NFC_V3_CONFIG2_NUM_ADDR_PHASE0          (1 << 5)
 
116
#define NFC_V3_CONFIG2_ECC_MODE_8               (1 << 6)
 
117
#define NFC_V3_CONFIG2_PPB(x)                   (((x) & 0x3) << 7)
 
118
#define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x)       (((x) & 0x3) << 12)
 
119
#define NFC_V3_CONFIG2_INT_MSK                  (1 << 15)
 
120
#define NFC_V3_CONFIG2_ST_CMD(x)                (((x) & 0xff) << 24)
 
121
#define NFC_V3_CONFIG2_SPAS(x)                  (((x) & 0xff) << 16)
 
122
 
 
123
#define NFC_V3_CONFIG3                          (host->regs_ip + 0x28)
 
124
#define NFC_V3_CONFIG3_ADD_OP(x)                (((x) & 0x3) << 0)
 
125
#define NFC_V3_CONFIG3_FW8                      (1 << 3)
 
126
#define NFC_V3_CONFIG3_SBB(x)                   (((x) & 0x7) << 8)
 
127
#define NFC_V3_CONFIG3_NUM_OF_DEVICES(x)        (((x) & 0x7) << 12)
 
128
#define NFC_V3_CONFIG3_RBB_MODE                 (1 << 15)
 
129
#define NFC_V3_CONFIG3_NO_SDMA                  (1 << 20)
 
130
 
 
131
#define NFC_V3_IPC                      (host->regs_ip + 0x2C)
 
132
#define NFC_V3_IPC_CREQ                 (1 << 0)
 
133
#define NFC_V3_IPC_INT                  (1 << 31)
 
134
 
 
135
#define NFC_V3_DELAY_LINE               (host->regs_ip + 0x34)
102
136
 
103
137
struct mxc_nand_host {
104
138
        struct mtd_info         mtd;
106
140
        struct mtd_partition    *parts;
107
141
        struct device           *dev;
108
142
 
 
143
        void                    *spare0;
 
144
        void                    *main_area0;
 
145
 
 
146
        void __iomem            *base;
109
147
        void __iomem            *regs;
110
 
        int                     spare_only;
 
148
        void __iomem            *regs_axi;
 
149
        void __iomem            *regs_ip;
111
150
        int                     status_request;
112
 
        int                     pagesize_2k;
113
 
        uint16_t                col_addr;
114
151
        struct clk              *clk;
115
152
        int                     clk_act;
116
153
        int                     irq;
117
 
 
118
 
        wait_queue_head_t       irq_waitq;
 
154
        int                     eccsize;
 
155
 
 
156
        struct completion       op_completion;
 
157
 
 
158
        uint8_t                 *data_buf;
 
159
        unsigned int            buf_start;
 
160
        int                     spare_len;
 
161
 
 
162
        void                    (*preset)(struct mtd_info *);
 
163
        void                    (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
 
164
        void                    (*send_addr)(struct mxc_nand_host *, uint16_t, int);
 
165
        void                    (*send_page)(struct mtd_info *, unsigned int);
 
166
        void                    (*send_read_id)(struct mxc_nand_host *);
 
167
        uint16_t                (*get_dev_status)(struct mxc_nand_host *);
 
168
        int                     (*check_int)(struct mxc_nand_host *);
 
169
        void                    (*irq_control)(struct mxc_nand_host *, int);
119
170
};
120
171
 
121
 
/* Define delays in microsec for NAND device operations */
122
 
#define TROP_US_DELAY   2000
123
 
/* Macros to get byte and bit positions of ECC */
124
 
#define COLPOS(x)  ((x) >> 3)
125
 
#define BITPOS(x) ((x) & 0xf)
126
 
 
127
 
/* Define single bit Error positions in Main & Spare area */
128
 
#define MAIN_SINGLEBIT_ERROR 0x4
129
 
#define SPARE_SINGLEBIT_ERROR 0x1
130
 
 
131
172
/* OOB placement block for use with hardware ecc generation */
132
 
static struct nand_ecclayout nand_hw_eccoob_8 = {
133
 
        .eccbytes = 5,
134
 
        .eccpos = {6, 7, 8, 9, 10},
135
 
        .oobfree = {{0, 5}, {11, 5}, }
136
 
};
137
 
 
138
 
static struct nand_ecclayout nand_hw_eccoob_16 = {
139
 
        .eccbytes = 5,
140
 
        .eccpos = {6, 7, 8, 9, 10},
141
 
        .oobfree = {{0, 5}, {11, 5}, }
142
 
};
143
 
 
144
 
static struct nand_ecclayout nand_hw_eccoob_64 = {
 
173
static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
 
174
        .eccbytes = 5,
 
175
        .eccpos = {6, 7, 8, 9, 10},
 
176
        .oobfree = {{0, 5}, {12, 4}, }
 
177
};
 
178
 
 
179
static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
145
180
        .eccbytes = 20,
146
181
        .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
147
182
                   38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
148
183
        .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
149
184
};
150
185
 
 
186
/* OOB description for 512 byte pages with 16 byte OOB */
 
187
static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
 
188
        .eccbytes = 1 * 9,
 
189
        .eccpos = {
 
190
                 7,  8,  9, 10, 11, 12, 13, 14, 15
 
191
        },
 
192
        .oobfree = {
 
193
                {.offset = 0, .length = 5}
 
194
        }
 
195
};
 
196
 
 
197
/* OOB description for 2048 byte pages with 64 byte OOB */
 
198
static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
 
199
        .eccbytes = 4 * 9,
 
200
        .eccpos = {
 
201
                 7,  8,  9, 10, 11, 12, 13, 14, 15,
 
202
                23, 24, 25, 26, 27, 28, 29, 30, 31,
 
203
                39, 40, 41, 42, 43, 44, 45, 46, 47,
 
204
                55, 56, 57, 58, 59, 60, 61, 62, 63
 
205
        },
 
206
        .oobfree = {
 
207
                {.offset = 2, .length = 4},
 
208
                {.offset = 16, .length = 7},
 
209
                {.offset = 32, .length = 7},
 
210
                {.offset = 48, .length = 7}
 
211
        }
 
212
};
 
213
 
151
214
#ifdef CONFIG_MTD_PARTITIONS
152
215
static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
153
216
#endif
156
219
{
157
220
        struct mxc_nand_host *host = dev_id;
158
221
 
159
 
        uint16_t tmp;
160
 
 
161
 
        tmp = readw(host->regs + NFC_CONFIG1);
162
 
        tmp |= NFC_INT_MSK; /* Disable interrupt */
163
 
        writew(tmp, host->regs + NFC_CONFIG1);
164
 
 
165
 
        wake_up(&host->irq_waitq);
 
222
        if (!host->check_int(host))
 
223
                return IRQ_NONE;
 
224
 
 
225
        host->irq_control(host, 0);
 
226
 
 
227
        complete(&host->op_completion);
166
228
 
167
229
        return IRQ_HANDLED;
168
230
}
169
231
 
 
232
static int check_int_v3(struct mxc_nand_host *host)
 
233
{
 
234
        uint32_t tmp;
 
235
 
 
236
        tmp = readl(NFC_V3_IPC);
 
237
        if (!(tmp & NFC_V3_IPC_INT))
 
238
                return 0;
 
239
 
 
240
        tmp &= ~NFC_V3_IPC_INT;
 
241
        writel(tmp, NFC_V3_IPC);
 
242
 
 
243
        return 1;
 
244
}
 
245
 
 
246
static int check_int_v1_v2(struct mxc_nand_host *host)
 
247
{
 
248
        uint32_t tmp;
 
249
 
 
250
        tmp = readw(NFC_V1_V2_CONFIG2);
 
251
        if (!(tmp & NFC_V1_V2_CONFIG2_INT))
 
252
                return 0;
 
253
 
 
254
        if (!cpu_is_mx21())
 
255
                writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
 
256
 
 
257
        return 1;
 
258
}
 
259
 
 
260
/*
 
261
 * It has been observed that the i.MX21 cannot read the CONFIG2:INT bit
 
262
 * if interrupts are masked (CONFIG1:INT_MSK is set). To handle this, the
 
263
 * driver can enable/disable the irq line rather than simply masking the
 
264
 * interrupts.
 
265
 */
 
266
static void irq_control_mx21(struct mxc_nand_host *host, int activate)
 
267
{
 
268
        if (activate)
 
269
                enable_irq(host->irq);
 
270
        else
 
271
                disable_irq_nosync(host->irq);
 
272
}
 
273
 
 
274
static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
 
275
{
 
276
        uint16_t tmp;
 
277
 
 
278
        tmp = readw(NFC_V1_V2_CONFIG1);
 
279
 
 
280
        if (activate)
 
281
                tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
 
282
        else
 
283
                tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
 
284
 
 
285
        writew(tmp, NFC_V1_V2_CONFIG1);
 
286
}
 
287
 
 
288
static void irq_control_v3(struct mxc_nand_host *host, int activate)
 
289
{
 
290
        uint32_t tmp;
 
291
 
 
292
        tmp = readl(NFC_V3_CONFIG2);
 
293
 
 
294
        if (activate)
 
295
                tmp &= ~NFC_V3_CONFIG2_INT_MSK;
 
296
        else
 
297
                tmp |= NFC_V3_CONFIG2_INT_MSK;
 
298
 
 
299
        writel(tmp, NFC_V3_CONFIG2);
 
300
}
 
301
 
170
302
/* This function polls the NANDFC to wait for the basic operation to
171
303
 * complete by checking the INT bit of config2 register.
172
304
 */
173
 
static void wait_op_done(struct mxc_nand_host *host, int max_retries,
174
 
                                uint16_t param, int useirq)
 
305
static void wait_op_done(struct mxc_nand_host *host, int useirq)
175
306
{
176
 
        uint32_t tmp;
 
307
        int max_retries = 8000;
177
308
 
178
309
        if (useirq) {
179
 
                if ((readw(host->regs + NFC_CONFIG2) & NFC_INT) == 0) {
180
 
 
181
 
                        tmp = readw(host->regs + NFC_CONFIG1);
182
 
                        tmp  &= ~NFC_INT_MSK;   /* Enable interrupt */
183
 
                        writew(tmp, host->regs + NFC_CONFIG1);
184
 
 
185
 
                        wait_event(host->irq_waitq,
186
 
                                readw(host->regs + NFC_CONFIG2) & NFC_INT);
187
 
 
188
 
                        tmp = readw(host->regs + NFC_CONFIG2);
189
 
                        tmp  &= ~NFC_INT;
190
 
                        writew(tmp, host->regs + NFC_CONFIG2);
 
310
                if (!host->check_int(host)) {
 
311
                        INIT_COMPLETION(host->op_completion);
 
312
                        host->irq_control(host, 1);
 
313
                        wait_for_completion(&host->op_completion);
191
314
                }
192
315
        } else {
193
316
                while (max_retries-- > 0) {
194
 
                        if (readw(host->regs + NFC_CONFIG2) & NFC_INT) {
195
 
                                tmp = readw(host->regs + NFC_CONFIG2);
196
 
                                tmp  &= ~NFC_INT;
197
 
                                writew(tmp, host->regs + NFC_CONFIG2);
 
317
                        if (host->check_int(host))
198
318
                                break;
199
 
                        }
 
319
 
200
320
                        udelay(1);
201
321
                }
202
322
                if (max_retries < 0)
203
 
                        DEBUG(MTD_DEBUG_LEVEL0, "%s(%d): INT not set\n",
204
 
                              __func__, param);
 
323
                        DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
 
324
                              __func__);
205
325
        }
206
326
}
207
327
 
 
328
static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
 
329
{
 
330
        /* fill command */
 
331
        writel(cmd, NFC_V3_FLASH_CMD);
 
332
 
 
333
        /* send out command */
 
334
        writel(NFC_CMD, NFC_V3_LAUNCH);
 
335
 
 
336
        /* Wait for operation to complete */
 
337
        wait_op_done(host, useirq);
 
338
}
 
339
 
208
340
/* This function issues the specified command to the NAND device and
209
341
 * waits for completion. */
210
 
static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq)
 
342
static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
211
343
{
212
344
        DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
213
345
 
214
 
        writew(cmd, host->regs + NFC_FLASH_CMD);
215
 
        writew(NFC_CMD, host->regs + NFC_CONFIG2);
216
 
 
217
 
        /* Wait for operation to complete */
218
 
        wait_op_done(host, TROP_US_DELAY, cmd, useirq);
 
346
        writew(cmd, NFC_V1_V2_FLASH_CMD);
 
347
        writew(NFC_CMD, NFC_V1_V2_CONFIG2);
 
348
 
 
349
        if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) {
 
350
                int max_retries = 100;
 
351
                /* Reset completion is indicated by NFC_CONFIG2 */
 
352
                /* being set to 0 */
 
353
                while (max_retries-- > 0) {
 
354
                        if (readw(NFC_V1_V2_CONFIG2) == 0) {
 
355
                                break;
 
356
                        }
 
357
                        udelay(1);
 
358
                }
 
359
                if (max_retries < 0)
 
360
                        DEBUG(MTD_DEBUG_LEVEL0, "%s: RESET failed\n",
 
361
                              __func__);
 
362
        } else {
 
363
                /* Wait for operation to complete */
 
364
                wait_op_done(host, useirq);
 
365
        }
 
366
}
 
367
 
 
368
static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
 
369
{
 
370
        /* fill address */
 
371
        writel(addr, NFC_V3_FLASH_ADDR0);
 
372
 
 
373
        /* send out address */
 
374
        writel(NFC_ADDR, NFC_V3_LAUNCH);
 
375
 
 
376
        wait_op_done(host, 0);
219
377
}
220
378
 
221
379
/* This function sends an address (or partial address) to the
222
380
 * NAND device. The address is used to select the source/destination for
223
381
 * a NAND command. */
224
 
static void send_addr(struct mxc_nand_host *host, uint16_t addr, int islast)
 
382
static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
225
383
{
226
384
        DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
227
385
 
228
 
        writew(addr, host->regs + NFC_FLASH_ADDR);
229
 
        writew(NFC_ADDR, host->regs + NFC_CONFIG2);
230
 
 
231
 
        /* Wait for operation to complete */
232
 
        wait_op_done(host, TROP_US_DELAY, addr, islast);
233
 
}
234
 
 
235
 
/* This function requests the NANDFC to initate the transfer
236
 
 * of data currently in the NANDFC RAM buffer to the NAND device. */
237
 
static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
238
 
                        int spare_only)
239
 
{
240
 
        DEBUG(MTD_DEBUG_LEVEL3, "send_prog_page (%d)\n", spare_only);
241
 
 
242
 
        /* NANDFC buffer 0 is used for page read/write */
243
 
        writew(buf_id, host->regs + NFC_BUF_ADDR);
244
 
 
245
 
        /* Configure spare or page+spare access */
246
 
        if (!host->pagesize_2k) {
247
 
                uint16_t config1 = readw(host->regs + NFC_CONFIG1);
248
 
                if (spare_only)
249
 
                        config1 |= NFC_SP_EN;
250
 
                else
251
 
                        config1 &= ~(NFC_SP_EN);
252
 
                writew(config1, host->regs + NFC_CONFIG1);
253
 
        }
254
 
 
255
 
        writew(NFC_INPUT, host->regs + NFC_CONFIG2);
256
 
 
257
 
        /* Wait for operation to complete */
258
 
        wait_op_done(host, TROP_US_DELAY, spare_only, true);
259
 
}
260
 
 
261
 
/* Requests NANDFC to initated the transfer of data from the
262
 
 * NAND device into in the NANDFC ram buffer. */
263
 
static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
264
 
                int spare_only)
265
 
{
266
 
        DEBUG(MTD_DEBUG_LEVEL3, "send_read_page (%d)\n", spare_only);
267
 
 
268
 
        /* NANDFC buffer 0 is used for page read/write */
269
 
        writew(buf_id, host->regs + NFC_BUF_ADDR);
270
 
 
271
 
        /* Configure spare or page+spare access */
272
 
        if (!host->pagesize_2k) {
273
 
                uint32_t config1 = readw(host->regs + NFC_CONFIG1);
274
 
                if (spare_only)
275
 
                        config1 |= NFC_SP_EN;
276
 
                else
277
 
                        config1 &= ~NFC_SP_EN;
278
 
                writew(config1, host->regs + NFC_CONFIG1);
279
 
        }
280
 
 
281
 
        writew(NFC_OUTPUT, host->regs + NFC_CONFIG2);
282
 
 
283
 
        /* Wait for operation to complete */
284
 
        wait_op_done(host, TROP_US_DELAY, spare_only, true);
 
386
        writew(addr, NFC_V1_V2_FLASH_ADDR);
 
387
        writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
 
388
 
 
389
        /* Wait for operation to complete */
 
390
        wait_op_done(host, islast);
 
391
}
 
392
 
 
393
static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
 
394
{
 
395
        struct nand_chip *nand_chip = mtd->priv;
 
396
        struct mxc_nand_host *host = nand_chip->priv;
 
397
        uint32_t tmp;
 
398
 
 
399
        tmp = readl(NFC_V3_CONFIG1);
 
400
        tmp &= ~(7 << 4);
 
401
        writel(tmp, NFC_V3_CONFIG1);
 
402
 
 
403
        /* transfer data from NFC ram to nand */
 
404
        writel(ops, NFC_V3_LAUNCH);
 
405
 
 
406
        wait_op_done(host, false);
 
407
}
 
408
 
 
409
static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
 
410
{
 
411
        struct nand_chip *nand_chip = mtd->priv;
 
412
        struct mxc_nand_host *host = nand_chip->priv;
 
413
        int bufs, i;
 
414
 
 
415
        if (nfc_is_v1() && mtd->writesize > 512)
 
416
                bufs = 4;
 
417
        else
 
418
                bufs = 1;
 
419
 
 
420
        for (i = 0; i < bufs; i++) {
 
421
 
 
422
                /* NANDFC buffer 0 is used for page read/write */
 
423
                writew(i, NFC_V1_V2_BUF_ADDR);
 
424
 
 
425
                writew(ops, NFC_V1_V2_CONFIG2);
 
426
 
 
427
                /* Wait for operation to complete */
 
428
                wait_op_done(host, true);
 
429
        }
 
430
}
 
431
 
 
432
static void send_read_id_v3(struct mxc_nand_host *host)
 
433
{
 
434
        /* Read ID into main buffer */
 
435
        writel(NFC_ID, NFC_V3_LAUNCH);
 
436
 
 
437
        wait_op_done(host, true);
 
438
 
 
439
        memcpy(host->data_buf, host->main_area0, 16);
285
440
}
286
441
 
287
442
/* Request the NANDFC to perform a read of the NAND device ID. */
288
 
static void send_read_id(struct mxc_nand_host *host)
 
443
static void send_read_id_v1_v2(struct mxc_nand_host *host)
289
444
{
290
445
        struct nand_chip *this = &host->nand;
291
 
        uint16_t tmp;
292
446
 
293
447
        /* NANDFC buffer 0 is used for device ID output */
294
 
        writew(0x0, host->regs + NFC_BUF_ADDR);
295
 
 
296
 
        /* Read ID into main buffer */
297
 
        tmp = readw(host->regs + NFC_CONFIG1);
298
 
        tmp &= ~NFC_SP_EN;
299
 
        writew(tmp, host->regs + NFC_CONFIG1);
300
 
 
301
 
        writew(NFC_ID, host->regs + NFC_CONFIG2);
 
448
        writew(0x0, NFC_V1_V2_BUF_ADDR);
 
449
 
 
450
        writew(NFC_ID, NFC_V1_V2_CONFIG2);
302
451
 
303
452
        /* Wait for operation to complete */
304
 
        wait_op_done(host, TROP_US_DELAY, 0, true);
 
453
        wait_op_done(host, true);
 
454
 
 
455
        memcpy(host->data_buf, host->main_area0, 16);
305
456
 
306
457
        if (this->options & NAND_BUSWIDTH_16) {
307
 
                void __iomem *main_buf = host->regs + MAIN_AREA0;
308
458
                /* compress the ID info */
309
 
                writeb(readb(main_buf + 2), main_buf + 1);
310
 
                writeb(readb(main_buf + 4), main_buf + 2);
311
 
                writeb(readb(main_buf + 6), main_buf + 3);
312
 
                writeb(readb(main_buf + 8), main_buf + 4);
313
 
                writeb(readb(main_buf + 10), main_buf + 5);
 
459
                host->data_buf[1] = host->data_buf[2];
 
460
                host->data_buf[2] = host->data_buf[4];
 
461
                host->data_buf[3] = host->data_buf[6];
 
462
                host->data_buf[4] = host->data_buf[8];
 
463
                host->data_buf[5] = host->data_buf[10];
314
464
        }
315
465
}
316
466
 
 
467
static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
 
468
{
 
469
        writew(NFC_STATUS, NFC_V3_LAUNCH);
 
470
        wait_op_done(host, true);
 
471
 
 
472
        return readl(NFC_V3_CONFIG1) >> 16;
 
473
}
 
474
 
317
475
/* This function requests the NANDFC to perform a read of the
318
476
 * NAND device status and returns the current status. */
319
 
static uint16_t get_dev_status(struct mxc_nand_host *host)
 
477
static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
320
478
{
321
 
        void __iomem *main_buf = host->regs + MAIN_AREA1;
 
479
        void __iomem *main_buf = host->main_area0;
322
480
        uint32_t store;
323
 
        uint16_t ret, tmp;
324
 
        /* Issue status request to NAND device */
325
 
 
326
 
        /* store the main area1 first word, later do recovery */
 
481
        uint16_t ret;
 
482
 
 
483
        writew(0x0, NFC_V1_V2_BUF_ADDR);
 
484
 
 
485
        /*
 
486
         * The device status is stored in main_area0. To
 
487
         * prevent corruption of the buffer save the value
 
488
         * and restore it afterwards.
 
489
         */
327
490
        store = readl(main_buf);
328
 
        /* NANDFC buffer 1 is used for device status to prevent
329
 
         * corruption of read/write buffer on status requests. */
330
 
        writew(1, host->regs + NFC_BUF_ADDR);
331
 
 
332
 
        /* Read status into main buffer */
333
 
        tmp = readw(host->regs + NFC_CONFIG1);
334
 
        tmp &= ~NFC_SP_EN;
335
 
        writew(tmp, host->regs + NFC_CONFIG1);
336
 
 
337
 
        writew(NFC_STATUS, host->regs + NFC_CONFIG2);
338
 
 
339
 
        /* Wait for operation to complete */
340
 
        wait_op_done(host, TROP_US_DELAY, 0, true);
341
 
 
342
 
        /* Status is placed in first word of main buffer */
343
 
        /* get status, then recovery area 1 data */
 
491
 
 
492
        writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
 
493
        wait_op_done(host, true);
 
494
 
344
495
        ret = readw(main_buf);
 
496
 
345
497
        writel(store, main_buf);
346
498
 
347
499
        return ret;
365
517
         */
366
518
}
367
519
 
368
 
static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
 
520
static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
369
521
                                 u_char *read_ecc, u_char *calc_ecc)
370
522
{
371
523
        struct nand_chip *nand_chip = mtd->priv;
376
528
         * additional correction.  2-Bit errors cannot be corrected by
377
529
         * HW ECC, so we need to return failure
378
530
         */
379
 
        uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT);
 
531
        uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
380
532
 
381
533
        if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
382
534
                DEBUG(MTD_DEBUG_LEVEL0,
387
539
        return 0;
388
540
}
389
541
 
 
542
static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
 
543
                                 u_char *read_ecc, u_char *calc_ecc)
 
544
{
 
545
        struct nand_chip *nand_chip = mtd->priv;
 
546
        struct mxc_nand_host *host = nand_chip->priv;
 
547
        u32 ecc_stat, err;
 
548
        int no_subpages = 1;
 
549
        int ret = 0;
 
550
        u8 ecc_bit_mask, err_limit;
 
551
 
 
552
        ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
 
553
        err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
 
554
 
 
555
        no_subpages = mtd->writesize >> 9;
 
556
 
 
557
        if (nfc_is_v21())
 
558
                ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
 
559
        else
 
560
                ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
 
561
 
 
562
        do {
 
563
                err = ecc_stat & ecc_bit_mask;
 
564
                if (err > err_limit) {
 
565
                        printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
 
566
                        return -1;
 
567
                } else {
 
568
                        ret += err;
 
569
                }
 
570
                ecc_stat >>= 4;
 
571
        } while (--no_subpages);
 
572
 
 
573
        mtd->ecc_stats.corrected += ret;
 
574
        pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
 
575
 
 
576
        return ret;
 
577
}
 
578
 
390
579
static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
391
580
                                  u_char *ecc_code)
392
581
{
397
586
{
398
587
        struct nand_chip *nand_chip = mtd->priv;
399
588
        struct mxc_nand_host *host = nand_chip->priv;
400
 
        uint8_t ret = 0;
401
 
        uint16_t col, rd_word;
402
 
        uint16_t __iomem *main_buf = host->regs + MAIN_AREA0;
403
 
        uint16_t __iomem *spare_buf = host->regs + SPARE_AREA0;
 
589
        uint8_t ret;
404
590
 
405
591
        /* Check for status request */
406
592
        if (host->status_request)
407
 
                return get_dev_status(host) & 0xFF;
408
 
 
409
 
        /* Get column for 16-bit access */
410
 
        col = host->col_addr >> 1;
411
 
 
412
 
        /* If we are accessing the spare region */
413
 
        if (host->spare_only)
414
 
                rd_word = readw(&spare_buf[col]);
415
 
        else
416
 
                rd_word = readw(&main_buf[col]);
417
 
 
418
 
        /* Pick upper/lower byte of word from RAM buffer */
419
 
        if (host->col_addr & 0x1)
420
 
                ret = (rd_word >> 8) & 0xFF;
421
 
        else
422
 
                ret = rd_word & 0xFF;
423
 
 
424
 
        /* Update saved column address */
425
 
        host->col_addr++;
 
593
                return host->get_dev_status(host) & 0xFF;
 
594
 
 
595
        ret = *(uint8_t *)(host->data_buf + host->buf_start);
 
596
        host->buf_start++;
426
597
 
427
598
        return ret;
428
599
}
431
602
{
432
603
        struct nand_chip *nand_chip = mtd->priv;
433
604
        struct mxc_nand_host *host = nand_chip->priv;
434
 
        uint16_t col, rd_word, ret;
435
 
        uint16_t __iomem *p;
436
 
 
437
 
        DEBUG(MTD_DEBUG_LEVEL3,
438
 
              "mxc_nand_read_word(col = %d)\n", host->col_addr);
439
 
 
440
 
        col = host->col_addr;
441
 
        /* Adjust saved column address */
442
 
        if (col < mtd->writesize && host->spare_only)
443
 
                col += mtd->writesize;
444
 
 
445
 
        if (col < mtd->writesize)
446
 
                p = (host->regs + MAIN_AREA0) + (col >> 1);
447
 
        else
448
 
                p = (host->regs + SPARE_AREA0) + ((col - mtd->writesize) >> 1);
449
 
 
450
 
        if (col & 1) {
451
 
                rd_word = readw(p);
452
 
                ret = (rd_word >> 8) & 0xff;
453
 
                rd_word = readw(&p[1]);
454
 
                ret |= (rd_word << 8) & 0xff00;
455
 
 
456
 
        } else
457
 
                ret = readw(p);
458
 
 
459
 
        /* Update saved column address */
460
 
        host->col_addr = col + 2;
 
605
        uint16_t ret;
 
606
 
 
607
        ret = *(uint16_t *)(host->data_buf + host->buf_start);
 
608
        host->buf_start += 2;
461
609
 
462
610
        return ret;
463
611
}
470
618
{
471
619
        struct nand_chip *nand_chip = mtd->priv;
472
620
        struct mxc_nand_host *host = nand_chip->priv;
473
 
        int n, col, i = 0;
474
 
 
475
 
        DEBUG(MTD_DEBUG_LEVEL3,
476
 
              "mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
477
 
              len);
478
 
 
479
 
        col = host->col_addr;
480
 
 
481
 
        /* Adjust saved column address */
482
 
        if (col < mtd->writesize && host->spare_only)
483
 
                col += mtd->writesize;
484
 
 
485
 
        n = mtd->writesize + mtd->oobsize - col;
486
 
        n = min(len, n);
487
 
 
488
 
        DEBUG(MTD_DEBUG_LEVEL3,
489
 
              "%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
490
 
 
491
 
        while (n) {
492
 
                void __iomem *p;
493
 
 
494
 
                if (col < mtd->writesize)
495
 
                        p = host->regs + MAIN_AREA0 + (col & ~3);
496
 
                else
497
 
                        p = host->regs + SPARE_AREA0 -
498
 
                                                mtd->writesize + (col & ~3);
499
 
 
500
 
                DEBUG(MTD_DEBUG_LEVEL3, "%s:%d: p = %p\n", __func__,
501
 
                      __LINE__, p);
502
 
 
503
 
                if (((col | (int)&buf[i]) & 3) || n < 16) {
504
 
                        uint32_t data = 0;
505
 
 
506
 
                        if (col & 3 || n < 4)
507
 
                                data = readl(p);
508
 
 
509
 
                        switch (col & 3) {
510
 
                        case 0:
511
 
                                if (n) {
512
 
                                        data = (data & 0xffffff00) |
513
 
                                            (buf[i++] << 0);
514
 
                                        n--;
515
 
                                        col++;
516
 
                                }
517
 
                        case 1:
518
 
                                if (n) {
519
 
                                        data = (data & 0xffff00ff) |
520
 
                                            (buf[i++] << 8);
521
 
                                        n--;
522
 
                                        col++;
523
 
                                }
524
 
                        case 2:
525
 
                                if (n) {
526
 
                                        data = (data & 0xff00ffff) |
527
 
                                            (buf[i++] << 16);
528
 
                                        n--;
529
 
                                        col++;
530
 
                                }
531
 
                        case 3:
532
 
                                if (n) {
533
 
                                        data = (data & 0x00ffffff) |
534
 
                                            (buf[i++] << 24);
535
 
                                        n--;
536
 
                                        col++;
537
 
                                }
538
 
                        }
539
 
 
540
 
                        writel(data, p);
541
 
                } else {
542
 
                        int m = mtd->writesize - col;
543
 
 
544
 
                        if (col >= mtd->writesize)
545
 
                                m += mtd->oobsize;
546
 
 
547
 
                        m = min(n, m) & ~3;
548
 
 
549
 
                        DEBUG(MTD_DEBUG_LEVEL3,
550
 
                              "%s:%d: n = %d, m = %d, i = %d, col = %d\n",
551
 
                              __func__,  __LINE__, n, m, i, col);
552
 
 
553
 
                        memcpy(p, &buf[i], m);
554
 
                        col += m;
555
 
                        i += m;
556
 
                        n -= m;
557
 
                }
558
 
        }
559
 
        /* Update saved column address */
560
 
        host->col_addr = col;
 
621
        u16 col = host->buf_start;
 
622
        int n = mtd->oobsize + mtd->writesize - col;
 
623
 
 
624
        n = min(n, len);
 
625
 
 
626
        memcpy(host->data_buf + col, buf, n);
 
627
 
 
628
        host->buf_start += n;
561
629
}
562
630
 
563
631
/* Read the data buffer from the NAND Flash. To read the data from NAND
568
636
{
569
637
        struct nand_chip *nand_chip = mtd->priv;
570
638
        struct mxc_nand_host *host = nand_chip->priv;
571
 
        int n, col, i = 0;
572
 
 
573
 
        DEBUG(MTD_DEBUG_LEVEL3,
574
 
              "mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr, len);
575
 
 
576
 
        col = host->col_addr;
577
 
 
578
 
        /* Adjust saved column address */
579
 
        if (col < mtd->writesize && host->spare_only)
580
 
                col += mtd->writesize;
581
 
 
582
 
        n = mtd->writesize + mtd->oobsize - col;
583
 
        n = min(len, n);
584
 
 
585
 
        while (n) {
586
 
                void __iomem *p;
587
 
 
588
 
                if (col < mtd->writesize)
589
 
                        p = host->regs + MAIN_AREA0 + (col & ~3);
590
 
                else
591
 
                        p = host->regs + SPARE_AREA0 -
592
 
                                        mtd->writesize + (col & ~3);
593
 
 
594
 
                if (((col | (int)&buf[i]) & 3) || n < 16) {
595
 
                        uint32_t data;
596
 
 
597
 
                        data = readl(p);
598
 
                        switch (col & 3) {
599
 
                        case 0:
600
 
                                if (n) {
601
 
                                        buf[i++] = (uint8_t) (data);
602
 
                                        n--;
603
 
                                        col++;
604
 
                                }
605
 
                        case 1:
606
 
                                if (n) {
607
 
                                        buf[i++] = (uint8_t) (data >> 8);
608
 
                                        n--;
609
 
                                        col++;
610
 
                                }
611
 
                        case 2:
612
 
                                if (n) {
613
 
                                        buf[i++] = (uint8_t) (data >> 16);
614
 
                                        n--;
615
 
                                        col++;
616
 
                                }
617
 
                        case 3:
618
 
                                if (n) {
619
 
                                        buf[i++] = (uint8_t) (data >> 24);
620
 
                                        n--;
621
 
                                        col++;
622
 
                                }
623
 
                        }
624
 
                } else {
625
 
                        int m = mtd->writesize - col;
626
 
 
627
 
                        if (col >= mtd->writesize)
628
 
                                m += mtd->oobsize;
629
 
 
630
 
                        m = min(n, m) & ~3;
631
 
                        memcpy(&buf[i], p, m);
632
 
                        col += m;
633
 
                        i += m;
634
 
                        n -= m;
635
 
                }
636
 
        }
637
 
        /* Update saved column address */
638
 
        host->col_addr = col;
639
 
 
 
639
        u16 col = host->buf_start;
 
640
        int n = mtd->oobsize + mtd->writesize - col;
 
641
 
 
642
        n = min(n, len);
 
643
 
 
644
        memcpy(buf, host->data_buf + col, len);
 
645
 
 
646
        host->buf_start += len;
640
647
}
641
648
 
642
649
/* Used by the upper layer to verify the data in NAND Flash
654
661
        struct nand_chip *nand_chip = mtd->priv;
655
662
        struct mxc_nand_host *host = nand_chip->priv;
656
663
 
657
 
#ifdef CONFIG_MTD_NAND_MXC_FORCE_CE
658
 
        if (chip > 0) {
659
 
                DEBUG(MTD_DEBUG_LEVEL0,
660
 
                      "ERROR:  Illegal chip select (chip = %d)\n", chip);
661
 
                return;
662
 
        }
663
 
 
664
 
        if (chip == -1) {
665
 
                writew(readw(host->regs + NFC_CONFIG1) & ~NFC_CE,
666
 
                                host->regs + NFC_CONFIG1);
667
 
                return;
668
 
        }
669
 
 
670
 
        writew(readw(host->regs + NFC_CONFIG1) | NFC_CE,
671
 
                        host->regs + NFC_CONFIG1);
672
 
#endif
673
 
 
674
664
        switch (chip) {
675
665
        case -1:
676
666
                /* Disable the NFC clock */
692
682
        }
693
683
}
694
684
 
695
 
/* Used by the upper layer to write command to NAND Flash for
696
 
 * different operations to be carried out on NAND Flash */
697
 
static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
698
 
                                int column, int page_addr)
 
685
/*
 
686
 * Function to transfer data to/from spare area.
 
687
 */
 
688
static void copy_spare(struct mtd_info *mtd, bool bfrom)
 
689
{
 
690
        struct nand_chip *this = mtd->priv;
 
691
        struct mxc_nand_host *host = this->priv;
 
692
        u16 i, j;
 
693
        u16 n = mtd->writesize >> 9;
 
694
        u8 *d = host->data_buf + mtd->writesize;
 
695
        u8 *s = host->spare0;
 
696
        u16 t = host->spare_len;
 
697
 
 
698
        j = (mtd->oobsize / n >> 1) << 1;
 
699
 
 
700
        if (bfrom) {
 
701
                for (i = 0; i < n - 1; i++)
 
702
                        memcpy(d + i * j, s + i * t, j);
 
703
 
 
704
                /* the last section */
 
705
                memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
 
706
        } else {
 
707
                for (i = 0; i < n - 1; i++)
 
708
                        memcpy(&s[i * t], &d[i * j], j);
 
709
 
 
710
                /* the last section */
 
711
                memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
 
712
        }
 
713
}
 
714
 
 
715
static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
699
716
{
700
717
        struct nand_chip *nand_chip = mtd->priv;
701
718
        struct mxc_nand_host *host = nand_chip->priv;
702
 
        int useirq = true;
703
 
 
704
 
        DEBUG(MTD_DEBUG_LEVEL3,
705
 
              "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
706
 
              command, column, page_addr);
707
 
 
708
 
        /* Reset command state information */
709
 
        host->status_request = false;
710
 
 
711
 
        /* Command pre-processing step */
712
 
        switch (command) {
713
 
 
714
 
        case NAND_CMD_STATUS:
715
 
                host->col_addr = 0;
716
 
                host->status_request = true;
717
 
                break;
718
 
 
719
 
        case NAND_CMD_READ0:
720
 
                host->col_addr = column;
721
 
                host->spare_only = false;
722
 
                useirq = false;
723
 
                break;
724
 
 
725
 
        case NAND_CMD_READOOB:
726
 
                host->col_addr = column;
727
 
                host->spare_only = true;
728
 
                useirq = false;
729
 
                if (host->pagesize_2k)
730
 
                        command = NAND_CMD_READ0; /* only READ0 is valid */
731
 
                break;
732
 
 
733
 
        case NAND_CMD_SEQIN:
734
 
                if (column >= mtd->writesize) {
735
 
                        /*
736
 
                         * FIXME: before send SEQIN command for write OOB,
737
 
                         * We must read one page out.
738
 
                         * For K9F1GXX has no READ1 command to set current HW
739
 
                         * pointer to spare area, we must write the whole page
740
 
                         * including OOB together.
741
 
                         */
742
 
                        if (host->pagesize_2k)
743
 
                                /* call ourself to read a page */
744
 
                                mxc_nand_command(mtd, NAND_CMD_READ0, 0,
745
 
                                                page_addr);
746
 
 
747
 
                        host->col_addr = column - mtd->writesize;
748
 
                        host->spare_only = true;
749
 
 
750
 
                        /* Set program pointer to spare region */
751
 
                        if (!host->pagesize_2k)
752
 
                                send_cmd(host, NAND_CMD_READOOB, false);
753
 
                } else {
754
 
                        host->spare_only = false;
755
 
                        host->col_addr = column;
756
 
 
757
 
                        /* Set program pointer to page start */
758
 
                        if (!host->pagesize_2k)
759
 
                                send_cmd(host, NAND_CMD_READ0, false);
760
 
                }
761
 
                useirq = false;
762
 
                break;
763
 
 
764
 
        case NAND_CMD_PAGEPROG:
765
 
                send_prog_page(host, 0, host->spare_only);
766
 
 
767
 
                if (host->pagesize_2k) {
768
 
                        /* data in 4 areas datas */
769
 
                        send_prog_page(host, 1, host->spare_only);
770
 
                        send_prog_page(host, 2, host->spare_only);
771
 
                        send_prog_page(host, 3, host->spare_only);
772
 
                }
773
 
 
774
 
                break;
775
 
 
776
 
        case NAND_CMD_ERASE1:
777
 
                useirq = false;
778
 
                break;
779
 
        }
780
 
 
781
 
        /* Write out the command to the device. */
782
 
        send_cmd(host, command, useirq);
783
719
 
784
720
        /* Write out column address, if necessary */
785
721
        if (column != -1) {
787
723
                 * MXC NANDFC can only perform full page+spare or
788
724
                 * spare-only read/write.  When the upper layers
789
725
                 * layers perform a read/write buf operation,
790
 
                 * we will used the saved column adress to index into
 
726
                 * we will used the saved column address to index into
791
727
                 * the full page.
792
728
                 */
793
 
                send_addr(host, 0, page_addr == -1);
794
 
                if (host->pagesize_2k)
 
729
                host->send_addr(host, 0, page_addr == -1);
 
730
                if (mtd->writesize > 512)
795
731
                        /* another col addr cycle for 2k page */
796
 
                        send_addr(host, 0, false);
 
732
                        host->send_addr(host, 0, false);
797
733
        }
798
734
 
799
735
        /* Write out page address, if necessary */
800
736
        if (page_addr != -1) {
801
737
                /* paddr_0 - p_addr_7 */
802
 
                send_addr(host, (page_addr & 0xff), false);
 
738
                host->send_addr(host, (page_addr & 0xff), false);
803
739
 
804
 
                if (host->pagesize_2k) {
 
740
                if (mtd->writesize > 512) {
805
741
                        if (mtd->size >= 0x10000000) {
806
742
                                /* paddr_8 - paddr_15 */
807
 
                                send_addr(host, (page_addr >> 8) & 0xff, false);
808
 
                                send_addr(host, (page_addr >> 16) & 0xff, true);
 
743
                                host->send_addr(host, (page_addr >> 8) & 0xff, false);
 
744
                                host->send_addr(host, (page_addr >> 16) & 0xff, true);
809
745
                        } else
810
746
                                /* paddr_8 - paddr_15 */
811
 
                                send_addr(host, (page_addr >> 8) & 0xff, true);
 
747
                                host->send_addr(host, (page_addr >> 8) & 0xff, true);
812
748
                } else {
813
749
                        /* One more address cycle for higher density devices */
814
750
                        if (mtd->size >= 0x4000000) {
815
751
                                /* paddr_8 - paddr_15 */
816
 
                                send_addr(host, (page_addr >> 8) & 0xff, false);
817
 
                                send_addr(host, (page_addr >> 16) & 0xff, true);
 
752
                                host->send_addr(host, (page_addr >> 8) & 0xff, false);
 
753
                                host->send_addr(host, (page_addr >> 16) & 0xff, true);
818
754
                        } else
819
755
                                /* paddr_8 - paddr_15 */
820
 
                                send_addr(host, (page_addr >> 8) & 0xff, true);
 
756
                                host->send_addr(host, (page_addr >> 8) & 0xff, true);
821
757
                }
822
758
        }
823
 
 
824
 
        /* Command post-processing step */
 
759
}
 
760
 
 
761
/*
 
762
 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
 
763
 * on how much oob the nand chip has. For 8bit ecc we need at least
 
764
 * 26 bytes of oob data per 512 byte block.
 
765
 */
 
766
static int get_eccsize(struct mtd_info *mtd)
 
767
{
 
768
        int oobbytes_per_512 = 0;
 
769
 
 
770
        oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
 
771
 
 
772
        if (oobbytes_per_512 < 26)
 
773
                return 4;
 
774
        else
 
775
                return 8;
 
776
}
 
777
 
 
778
static void preset_v1_v2(struct mtd_info *mtd)
 
779
{
 
780
        struct nand_chip *nand_chip = mtd->priv;
 
781
        struct mxc_nand_host *host = nand_chip->priv;
 
782
        uint16_t config1 = 0;
 
783
 
 
784
        if (nand_chip->ecc.mode == NAND_ECC_HW)
 
785
                config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
 
786
 
 
787
        if (nfc_is_v21())
 
788
                config1 |= NFC_V2_CONFIG1_FP_INT;
 
789
 
 
790
        if (!cpu_is_mx21())
 
791
                config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
 
792
 
 
793
        if (nfc_is_v21() && mtd->writesize) {
 
794
                uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
 
795
 
 
796
                host->eccsize = get_eccsize(mtd);
 
797
                if (host->eccsize == 4)
 
798
                        config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
 
799
 
 
800
                config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
 
801
        } else {
 
802
                host->eccsize = 1;
 
803
        }
 
804
 
 
805
        writew(config1, NFC_V1_V2_CONFIG1);
 
806
        /* preset operation */
 
807
 
 
808
        /* Unlock the internal RAM Buffer */
 
809
        writew(0x2, NFC_V1_V2_CONFIG);
 
810
 
 
811
        /* Blocks to be unlocked */
 
812
        if (nfc_is_v21()) {
 
813
                writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR);
 
814
                writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR);
 
815
        } else if (nfc_is_v1()) {
 
816
                writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
 
817
                writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
 
818
        } else
 
819
                BUG();
 
820
 
 
821
        /* Unlock Block Command for given address range */
 
822
        writew(0x4, NFC_V1_V2_WRPROT);
 
823
}
 
824
 
 
825
static void preset_v3(struct mtd_info *mtd)
 
826
{
 
827
        struct nand_chip *chip = mtd->priv;
 
828
        struct mxc_nand_host *host = chip->priv;
 
829
        uint32_t config2, config3;
 
830
        int i, addr_phases;
 
831
 
 
832
        writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
 
833
        writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
 
834
 
 
835
        /* Unlock the internal RAM Buffer */
 
836
        writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
 
837
                        NFC_V3_WRPROT);
 
838
 
 
839
        /* Blocks to be unlocked */
 
840
        for (i = 0; i < NAND_MAX_CHIPS; i++)
 
841
                writel(0x0 |    (0xffff << 16),
 
842
                                NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
 
843
 
 
844
        writel(0, NFC_V3_IPC);
 
845
 
 
846
        config2 = NFC_V3_CONFIG2_ONE_CYCLE |
 
847
                NFC_V3_CONFIG2_2CMD_PHASES |
 
848
                NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
 
849
                NFC_V3_CONFIG2_ST_CMD(0x70) |
 
850
                NFC_V3_CONFIG2_INT_MSK |
 
851
                NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
 
852
 
 
853
        if (chip->ecc.mode == NAND_ECC_HW)
 
854
                config2 |= NFC_V3_CONFIG2_ECC_EN;
 
855
 
 
856
        addr_phases = fls(chip->pagemask) >> 3;
 
857
 
 
858
        if (mtd->writesize == 2048) {
 
859
                config2 |= NFC_V3_CONFIG2_PS_2048;
 
860
                config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
 
861
        } else if (mtd->writesize == 4096) {
 
862
                config2 |= NFC_V3_CONFIG2_PS_4096;
 
863
                config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
 
864
        } else {
 
865
                config2 |= NFC_V3_CONFIG2_PS_512;
 
866
                config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
 
867
        }
 
868
 
 
869
        if (mtd->writesize) {
 
870
                config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
 
871
                host->eccsize = get_eccsize(mtd);
 
872
                if (host->eccsize == 8)
 
873
                        config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
 
874
        }
 
875
 
 
876
        writel(config2, NFC_V3_CONFIG2);
 
877
 
 
878
        config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
 
879
                        NFC_V3_CONFIG3_NO_SDMA |
 
880
                        NFC_V3_CONFIG3_RBB_MODE |
 
881
                        NFC_V3_CONFIG3_SBB(6) | /* Reset default */
 
882
                        NFC_V3_CONFIG3_ADD_OP(0);
 
883
 
 
884
        if (!(chip->options & NAND_BUSWIDTH_16))
 
885
                config3 |= NFC_V3_CONFIG3_FW8;
 
886
 
 
887
        writel(config3, NFC_V3_CONFIG3);
 
888
 
 
889
        writel(0, NFC_V3_DELAY_LINE);
 
890
}
 
891
 
 
892
/* Used by the upper layer to write command to NAND Flash for
 
893
 * different operations to be carried out on NAND Flash */
 
894
static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
 
895
                                int column, int page_addr)
 
896
{
 
897
        struct nand_chip *nand_chip = mtd->priv;
 
898
        struct mxc_nand_host *host = nand_chip->priv;
 
899
 
 
900
        DEBUG(MTD_DEBUG_LEVEL3,
 
901
              "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
 
902
              command, column, page_addr);
 
903
 
 
904
        /* Reset command state information */
 
905
        host->status_request = false;
 
906
 
 
907
        /* Command pre-processing step */
825
908
        switch (command) {
826
 
 
827
909
        case NAND_CMD_RESET:
828
 
                break;
829
 
 
 
910
                host->preset(mtd);
 
911
                host->send_cmd(host, command, false);
 
912
                break;
 
913
 
 
914
        case NAND_CMD_STATUS:
 
915
                host->buf_start = 0;
 
916
                host->status_request = true;
 
917
 
 
918
                host->send_cmd(host, command, true);
 
919
                mxc_do_addr_cycle(mtd, column, page_addr);
 
920
                break;
 
921
 
 
922
        case NAND_CMD_READ0:
830
923
        case NAND_CMD_READOOB:
831
 
        case NAND_CMD_READ0:
832
 
                if (host->pagesize_2k) {
833
 
                        /* send read confirm command */
834
 
                        send_cmd(host, NAND_CMD_READSTART, true);
835
 
                        /* read for each AREA */
836
 
                        send_read_page(host, 0, host->spare_only);
837
 
                        send_read_page(host, 1, host->spare_only);
838
 
                        send_read_page(host, 2, host->spare_only);
839
 
                        send_read_page(host, 3, host->spare_only);
840
 
                } else
841
 
                        send_read_page(host, 0, host->spare_only);
 
924
                if (command == NAND_CMD_READ0)
 
925
                        host->buf_start = column;
 
926
                else
 
927
                        host->buf_start = column + mtd->writesize;
 
928
 
 
929
                command = NAND_CMD_READ0; /* only READ0 is valid */
 
930
 
 
931
                host->send_cmd(host, command, false);
 
932
                mxc_do_addr_cycle(mtd, column, page_addr);
 
933
 
 
934
                if (mtd->writesize > 512)
 
935
                        host->send_cmd(host, NAND_CMD_READSTART, true);
 
936
 
 
937
                host->send_page(mtd, NFC_OUTPUT);
 
938
 
 
939
                memcpy(host->data_buf, host->main_area0, mtd->writesize);
 
940
                copy_spare(mtd, true);
 
941
                break;
 
942
 
 
943
        case NAND_CMD_SEQIN:
 
944
                if (column >= mtd->writesize)
 
945
                        /* call ourself to read a page */
 
946
                        mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
 
947
 
 
948
                host->buf_start = column;
 
949
 
 
950
                host->send_cmd(host, command, false);
 
951
                mxc_do_addr_cycle(mtd, column, page_addr);
 
952
                break;
 
953
 
 
954
        case NAND_CMD_PAGEPROG:
 
955
                memcpy(host->main_area0, host->data_buf, mtd->writesize);
 
956
                copy_spare(mtd, false);
 
957
                host->send_page(mtd, NFC_INPUT);
 
958
                host->send_cmd(host, command, true);
 
959
                mxc_do_addr_cycle(mtd, column, page_addr);
842
960
                break;
843
961
 
844
962
        case NAND_CMD_READID:
845
 
                host->col_addr = 0;
846
 
                send_read_id(host);
847
 
                break;
848
 
 
849
 
        case NAND_CMD_PAGEPROG:
850
 
                break;
851
 
 
852
 
        case NAND_CMD_STATUS:
853
 
                break;
854
 
 
 
963
                host->send_cmd(host, command, true);
 
964
                mxc_do_addr_cycle(mtd, column, page_addr);
 
965
                host->send_read_id(host);
 
966
                host->buf_start = column;
 
967
                break;
 
968
 
 
969
        case NAND_CMD_ERASE1:
855
970
        case NAND_CMD_ERASE2:
 
971
                host->send_cmd(host, command, false);
 
972
                mxc_do_addr_cycle(mtd, column, page_addr);
 
973
 
856
974
                break;
857
975
        }
858
976
}
859
977
 
860
 
/* Define some generic bad / good block scan pattern which are used
861
 
 * while scanning a device for factory marked good / bad blocks. */
862
 
static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
863
 
 
864
 
static struct nand_bbt_descr smallpage_memorybased = {
865
 
        .options = NAND_BBT_SCAN2NDPAGE,
866
 
        .offs = 5,
867
 
        .len = 1,
868
 
        .pattern = scan_ff_pattern
 
978
/*
 
979
 * The generic flash bbt decriptors overlap with our ecc
 
980
 * hardware, so define some i.MX specific ones.
 
981
 */
 
982
static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
 
983
static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
 
984
 
 
985
static struct nand_bbt_descr bbt_main_descr = {
 
986
        .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
 
987
            | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
 
988
        .offs = 0,
 
989
        .len = 4,
 
990
        .veroffs = 4,
 
991
        .maxblocks = 4,
 
992
        .pattern = bbt_pattern,
 
993
};
 
994
 
 
995
static struct nand_bbt_descr bbt_mirror_descr = {
 
996
        .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
 
997
            | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
 
998
        .offs = 0,
 
999
        .len = 4,
 
1000
        .veroffs = 4,
 
1001
        .maxblocks = 4,
 
1002
        .pattern = mirror_pattern,
869
1003
};
870
1004
 
871
1005
static int __init mxcnd_probe(struct platform_device *pdev)
875
1009
        struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
876
1010
        struct mxc_nand_host *host;
877
1011
        struct resource *res;
878
 
        uint16_t tmp;
879
 
        int err = 0, nr_parts = 0;
 
1012
        int err = 0, __maybe_unused nr_parts = 0;
 
1013
        struct nand_ecclayout *oob_smallpage, *oob_largepage;
880
1014
 
881
1015
        /* Allocate memory for MTD device structure and private data */
882
 
        host = kzalloc(sizeof(struct mxc_nand_host), GFP_KERNEL);
 
1016
        host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
 
1017
                        NAND_MAX_OOBSIZE, GFP_KERNEL);
883
1018
        if (!host)
884
1019
                return -ENOMEM;
885
1020
 
 
1021
        host->data_buf = (uint8_t *)(host + 1);
 
1022
 
886
1023
        host->dev = &pdev->dev;
887
1024
        /* structures must be linked */
888
1025
        this = &host->nand;
890
1027
        mtd->priv = this;
891
1028
        mtd->owner = THIS_MODULE;
892
1029
        mtd->dev.parent = &pdev->dev;
893
 
        mtd->name = "mxc_nand";
 
1030
        mtd->name = DRIVER_NAME;
894
1031
 
895
1032
        /* 50 us command delay time */
896
1033
        this->chip_delay = 5;
920
1057
                goto eres;
921
1058
        }
922
1059
 
923
 
        host->regs = ioremap(res->start, res->end - res->start + 1);
924
 
        if (!host->regs) {
 
1060
        host->base = ioremap(res->start, resource_size(res));
 
1061
        if (!host->base) {
925
1062
                err = -ENOMEM;
926
1063
                goto eres;
927
1064
        }
928
1065
 
929
 
        tmp = readw(host->regs + NFC_CONFIG1);
930
 
        tmp |= NFC_INT_MSK;
931
 
        writew(tmp, host->regs + NFC_CONFIG1);
932
 
 
933
 
        init_waitqueue_head(&host->irq_waitq);
934
 
 
935
 
        host->irq = platform_get_irq(pdev, 0);
936
 
 
937
 
        err = request_irq(host->irq, mxc_nfc_irq, 0, "mxc_nd", host);
938
 
        if (err)
939
 
                goto eirq;
 
1066
        host->main_area0 = host->base;
 
1067
 
 
1068
        if (nfc_is_v1() || nfc_is_v21()) {
 
1069
                host->preset = preset_v1_v2;
 
1070
                host->send_cmd = send_cmd_v1_v2;
 
1071
                host->send_addr = send_addr_v1_v2;
 
1072
                host->send_page = send_page_v1_v2;
 
1073
                host->send_read_id = send_read_id_v1_v2;
 
1074
                host->get_dev_status = get_dev_status_v1_v2;
 
1075
                host->check_int = check_int_v1_v2;
 
1076
                if (cpu_is_mx21())
 
1077
                        host->irq_control = irq_control_mx21;
 
1078
                else
 
1079
                        host->irq_control = irq_control_v1_v2;
 
1080
        }
 
1081
 
 
1082
        if (nfc_is_v21()) {
 
1083
                host->regs = host->base + 0x1e00;
 
1084
                host->spare0 = host->base + 0x1000;
 
1085
                host->spare_len = 64;
 
1086
                oob_smallpage = &nandv2_hw_eccoob_smallpage;
 
1087
                oob_largepage = &nandv2_hw_eccoob_largepage;
 
1088
                this->ecc.bytes = 9;
 
1089
        } else if (nfc_is_v1()) {
 
1090
                host->regs = host->base + 0xe00;
 
1091
                host->spare0 = host->base + 0x800;
 
1092
                host->spare_len = 16;
 
1093
                oob_smallpage = &nandv1_hw_eccoob_smallpage;
 
1094
                oob_largepage = &nandv1_hw_eccoob_largepage;
 
1095
                this->ecc.bytes = 3;
 
1096
                host->eccsize = 1;
 
1097
        } else if (nfc_is_v3_2()) {
 
1098
                res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
 
1099
                if (!res) {
 
1100
                        err = -ENODEV;
 
1101
                        goto eirq;
 
1102
                }
 
1103
                host->regs_ip = ioremap(res->start, resource_size(res));
 
1104
                if (!host->regs_ip) {
 
1105
                        err = -ENOMEM;
 
1106
                        goto eirq;
 
1107
                }
 
1108
                host->regs_axi = host->base + 0x1e00;
 
1109
                host->spare0 = host->base + 0x1000;
 
1110
                host->spare_len = 64;
 
1111
                host->preset = preset_v3;
 
1112
                host->send_cmd = send_cmd_v3;
 
1113
                host->send_addr = send_addr_v3;
 
1114
                host->send_page = send_page_v3;
 
1115
                host->send_read_id = send_read_id_v3;
 
1116
                host->check_int = check_int_v3;
 
1117
                host->get_dev_status = get_dev_status_v3;
 
1118
                host->irq_control = irq_control_v3;
 
1119
                oob_smallpage = &nandv2_hw_eccoob_smallpage;
 
1120
                oob_largepage = &nandv2_hw_eccoob_largepage;
 
1121
        } else
 
1122
                BUG();
 
1123
 
 
1124
        this->ecc.size = 512;
 
1125
        this->ecc.layout = oob_smallpage;
940
1126
 
941
1127
        if (pdata->hw_ecc) {
942
1128
                this->ecc.calculate = mxc_nand_calculate_ecc;
943
1129
                this->ecc.hwctl = mxc_nand_enable_hwecc;
944
 
                this->ecc.correct = mxc_nand_correct_data;
 
1130
                if (nfc_is_v1())
 
1131
                        this->ecc.correct = mxc_nand_correct_data_v1;
 
1132
                else
 
1133
                        this->ecc.correct = mxc_nand_correct_data_v2_v3;
945
1134
                this->ecc.mode = NAND_ECC_HW;
946
 
                this->ecc.size = 512;
947
 
                this->ecc.bytes = 3;
948
 
                tmp = readw(host->regs + NFC_CONFIG1);
949
 
                tmp |= NFC_ECC_EN;
950
 
                writew(tmp, host->regs + NFC_CONFIG1);
951
1135
        } else {
952
 
                this->ecc.size = 512;
953
 
                this->ecc.bytes = 3;
954
 
                this->ecc.layout = &nand_hw_eccoob_8;
955
1136
                this->ecc.mode = NAND_ECC_SOFT;
956
 
                tmp = readw(host->regs + NFC_CONFIG1);
957
 
                tmp &= ~NFC_ECC_EN;
958
 
                writew(tmp, host->regs + NFC_CONFIG1);
959
1137
        }
960
1138
 
961
 
        /* Reset NAND */
962
 
        this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
963
 
 
964
 
        /* preset operation */
965
 
        /* Unlock the internal RAM Buffer */
966
 
        writew(0x2, host->regs + NFC_CONFIG);
967
 
 
968
 
        /* Blocks to be unlocked */
969
 
        writew(0x0, host->regs + NFC_UNLOCKSTART_BLKADDR);
970
 
        writew(0x4000, host->regs + NFC_UNLOCKEND_BLKADDR);
971
 
 
972
 
        /* Unlock Block Command for given address range */
973
 
        writew(0x4, host->regs + NFC_WRPROT);
974
 
 
975
1139
        /* NAND bus width determines access funtions used by upper layer */
976
 
        if (pdata->width == 2) {
 
1140
        if (pdata->width == 2)
977
1141
                this->options |= NAND_BUSWIDTH_16;
978
 
                this->ecc.layout = &nand_hw_eccoob_16;
 
1142
 
 
1143
        if (pdata->flash_bbt) {
 
1144
                this->bbt_td = &bbt_main_descr;
 
1145
                this->bbt_md = &bbt_mirror_descr;
 
1146
                /* update flash based bbt */
 
1147
                this->options |= NAND_USE_FLASH_BBT;
979
1148
        }
980
1149
 
 
1150
        init_completion(&host->op_completion);
 
1151
 
 
1152
        host->irq = platform_get_irq(pdev, 0);
 
1153
 
 
1154
        /*
 
1155
         * mask the interrupt. For i.MX21 explicitely call
 
1156
         * irq_control_v1_v2 to use the mask bit. We can't call
 
1157
         * disable_irq_nosync() for an interrupt we do not own yet.
 
1158
         */
 
1159
        if (cpu_is_mx21())
 
1160
                irq_control_v1_v2(host, 0);
 
1161
        else
 
1162
                host->irq_control(host, 0);
 
1163
 
 
1164
        err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host);
 
1165
        if (err)
 
1166
                goto eirq;
 
1167
 
 
1168
        host->irq_control(host, 0);
 
1169
 
 
1170
        /*
 
1171
         * Now that the interrupt is disabled make sure the interrupt
 
1172
         * mask bit is cleared on i.MX21. Otherwise we can't read
 
1173
         * the interrupt status bit on this machine.
 
1174
         */
 
1175
        if (cpu_is_mx21())
 
1176
                irq_control_v1_v2(host, 1);
 
1177
 
981
1178
        /* first scan to find the device and get the page size */
982
 
        if (nand_scan_ident(mtd, 1)) {
 
1179
        if (nand_scan_ident(mtd, 1, NULL)) {
983
1180
                err = -ENXIO;
984
1181
                goto escan;
985
1182
        }
986
1183
 
987
 
        if (mtd->writesize == 2048) {
988
 
                host->pagesize_2k = 1;
989
 
                this->badblock_pattern = &smallpage_memorybased;
990
 
        }
 
1184
        /* Call preset again, with correct writesize this time */
 
1185
        host->preset(mtd);
991
1186
 
992
 
        if (this->ecc.mode == NAND_ECC_HW) {
993
 
                switch (mtd->oobsize) {
994
 
                case 8:
995
 
                        this->ecc.layout = &nand_hw_eccoob_8;
996
 
                        break;
997
 
                case 16:
998
 
                        this->ecc.layout = &nand_hw_eccoob_16;
999
 
                        break;
1000
 
                case 64:
1001
 
                        this->ecc.layout = &nand_hw_eccoob_64;
1002
 
                        break;
1003
 
                default:
1004
 
                        /* page size not handled by HW ECC */
1005
 
                        /* switching back to soft ECC */
1006
 
                        this->ecc.size = 512;
1007
 
                        this->ecc.bytes = 3;
1008
 
                        this->ecc.layout = &nand_hw_eccoob_8;
1009
 
                        this->ecc.mode = NAND_ECC_SOFT;
1010
 
                        this->ecc.calculate = NULL;
1011
 
                        this->ecc.correct = NULL;
1012
 
                        this->ecc.hwctl = NULL;
1013
 
                        tmp = readw(host->regs + NFC_CONFIG1);
1014
 
                        tmp &= ~NFC_ECC_EN;
1015
 
                        writew(tmp, host->regs + NFC_CONFIG1);
1016
 
                        break;
1017
 
                }
1018
 
        }
 
1187
        if (mtd->writesize == 2048)
 
1188
                this->ecc.layout = oob_largepage;
1019
1189
 
1020
1190
        /* second phase scan */
1021
1191
        if (nand_scan_tail(mtd)) {
1029
1199
            parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
1030
1200
        if (nr_parts > 0)
1031
1201
                add_mtd_partitions(mtd, host->parts, nr_parts);
 
1202
        else if (pdata->parts)
 
1203
                add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
1032
1204
        else
1033
1205
#endif
1034
1206
        {
1043
1215
escan:
1044
1216
        free_irq(host->irq, host);
1045
1217
eirq:
1046
 
        iounmap(host->regs);
 
1218
        if (host->regs_ip)
 
1219
                iounmap(host->regs_ip);
 
1220
        iounmap(host->base);
1047
1221
eres:
1048
1222
        clk_put(host->clk);
1049
1223
eclk:
1062
1236
 
1063
1237
        nand_release(&host->mtd);
1064
1238
        free_irq(host->irq, host);
1065
 
        iounmap(host->regs);
 
1239
        if (host->regs_ip)
 
1240
                iounmap(host->regs_ip);
 
1241
        iounmap(host->base);
1066
1242
        kfree(host);
1067
1243
 
1068
1244
        return 0;
1069
1245
}
1070
1246
 
1071
 
#ifdef CONFIG_PM
1072
 
static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
1073
 
{
1074
 
        struct mtd_info *mtd = platform_get_drvdata(pdev);
1075
 
        struct nand_chip *nand_chip = mtd->priv;
1076
 
        struct mxc_nand_host *host = nand_chip->priv;
1077
 
        int ret = 0;
1078
 
 
1079
 
        DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
1080
 
        if (mtd) {
1081
 
                ret = mtd->suspend(mtd);
1082
 
                /* Disable the NFC clock */
1083
 
                clk_disable(host->clk);
1084
 
        }
1085
 
 
1086
 
        return ret;
1087
 
}
1088
 
 
1089
 
static int mxcnd_resume(struct platform_device *pdev)
1090
 
{
1091
 
        struct mtd_info *mtd = platform_get_drvdata(pdev);
1092
 
        struct nand_chip *nand_chip = mtd->priv;
1093
 
        struct mxc_nand_host *host = nand_chip->priv;
1094
 
        int ret = 0;
1095
 
 
1096
 
        DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
1097
 
 
1098
 
        if (mtd) {
1099
 
                /* Enable the NFC clock */
1100
 
                clk_enable(host->clk);
1101
 
                mtd->resume(mtd);
1102
 
        }
1103
 
 
1104
 
        return ret;
1105
 
}
1106
 
 
1107
 
#else
1108
 
# define mxcnd_suspend   NULL
1109
 
# define mxcnd_resume    NULL
1110
 
#endif                          /* CONFIG_PM */
1111
 
 
1112
1247
static struct platform_driver mxcnd_driver = {
1113
1248
        .driver = {
1114
1249
                   .name = DRIVER_NAME,
1115
 
                   },
1116
 
        .remove = __exit_p(mxcnd_remove),
1117
 
        .suspend = mxcnd_suspend,
1118
 
        .resume = mxcnd_resume,
 
1250
        },
 
1251
        .remove = __devexit_p(mxcnd_remove),
1119
1252
};
1120
1253
 
1121
1254
static int __init mxc_nd_init(void)