kernel: update to version 4.4.14
[openwrt/staging/lynxis/omap.git] / target / linux / ipq806x / patches-4.4 / 162-mtd-nand-Qualcomm-NAND-controller-driver.patch
1 Content-Type: text/plain; charset="utf-8"
2 MIME-Version: 1.0
3 Content-Transfer-Encoding: 7bit
4 Subject: [v3,2/5] mtd: nand: Qualcomm NAND controller driver
5 From: Archit Taneja <architt@codeaurora.org>
6 X-Patchwork-Id: 6927101
7 Message-Id: <1438578498-32254-3-git-send-email-architt@codeaurora.org>
8 To: linux-mtd@lists.infradead.org, dehrenberg@google.com,
9 cernekee@gmail.com, computersforpeace@gmail.com
10 Cc: linux-arm-msm@vger.kernel.org, agross@codeaurora.org,
11 sboyd@codeaurora.org, linux-kernel@vger.kernel.org,
12 Archit Taneja <architt@codeaurora.org>
13 Date: Mon, 3 Aug 2015 10:38:15 +0530
14
15 The Qualcomm NAND controller is found in SoCs like IPQ806x, MSM7xx,
16 MDM9x15 series.
17
18 It exists as a sub block inside the IPs EBI2 (External Bus Interface 2)
19 and QPIC (Qualcomm Parallel Interface Controller). These IPs provide a
20 broader interface for external slow peripheral devices such as LCD and
21 NAND/NOR flash memory or SRAM like interfaces.
22
23 We add support for the NAND controller found within EBI2. For the SoCs
24 of our interest, we only use the NAND controller within EBI2. Therefore,
25 it's safe for us to assume that the NAND controller is a standalone block
26 within the SoC.
27
28 The controller supports 512B, 2kB, 4kB and 8kB page 8-bit and 16-bit NAND
29 flash devices. It contains a HW ECC block that supports BCH ECC (4, 8 and
30 16 bit correction/step) and RS ECC(4 bit correction/step) that covers main
31 and spare data. The controller contains an internal 512 byte page buffer
32 to which we read/write via DMA. The EBI2 type NAND controller uses ADM DMA
33 for register read/write and data transfers. The controller performs page
34 reads and writes at a codeword/step level of 512 bytes. It can support up
35 to 2 external chips of different configurations.
36
37 The driver prepares register read and write configuration descriptors for
38 each codeword, followed by data descriptors to read or write data from the
39 controller's internal buffer. It uses a single ADM DMA channel that we get
40 via dmaengine API. The controller requires 2 ADM CRCIs for command and
41 data flow control. These are passed via DT.
42
43 The ecc layout used by the controller is syndrome like, but we can't use
44 the standard syndrome ecc ops because of several reasons. First, the amount
45 of data bytes covered by ecc isn't same in each step. Second, writing to
46 free oob space requires us writing to the entire step in which the oob
47 lies. This forces us to create our own ecc ops.
48
49 One more difference is how the controller accesses the bad block marker.
50 The controller ignores reading the marker when ECC is enabled. ECC needs
51 to be explicity disabled to read or write to the bad block marker. For
52 this reason, we use the newly created flag NAND_BBT_ACCESS_BBM_RAW to
53 read the factory provided bad block markers.
54
55 v3:
56 - Refactor dma functions for maximum reuse
57 - Use dma_slave_confing on stack
58 - optimize and clean upempty_page_fixup using memchr_inv
59 - ensure portability with dma register reads using le32_* funcs
60 - use NAND_USE_BOUNCE_BUFFER instead of doing it ourselves
61 - fix handling of return values of dmaengine funcs
62 - constify wherever possible
63 - Remove dependency on ADM DMA in Kconfig
64 - Misc fixes and clean ups
65
66 v2:
67 - Use new BBT flag that allows us to read BBM in raw mode
68 - reduce memcpy-s in the driver
69 - some refactor and clean ups because of above changes
70
71 Reviewed-by: Andy Gross <agross@codeaurora.org>
72 Signed-off-by: Archit Taneja <architt@codeaurora.org>
73
74 ---
75 drivers/mtd/nand/Kconfig | 7 +
76 drivers/mtd/nand/Makefile | 1 +
77 drivers/mtd/nand/qcom_nandc.c | 1913 +++++++++++++++++++++++++++++++++++++++++
78 3 files changed, 1921 insertions(+)
79 create mode 100644 drivers/mtd/nand/qcom_nandc.c
80
81 --- a/drivers/mtd/nand/Kconfig
82 +++ b/drivers/mtd/nand/Kconfig
83 @@ -546,4 +546,11 @@ config MTD_NAND_HISI504
84 help
85 Enables support for NAND controller on Hisilicon SoC Hip04.
86
87 +config MTD_NAND_QCOM
88 + tristate "Support for NAND on QCOM SoCs"
89 + depends on ARCH_QCOM
90 + help
91 + Enables support for NAND flash chips on SoCs containing the EBI2 NAND
92 + controller. This controller is found on IPQ806x SoC.
93 +
94 endif # MTD_NAND
95 --- /dev/null
96 +++ b/drivers/mtd/nand/qcom_nandc.c
97 @@ -0,0 +1,1918 @@
98 +/*
99 + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
100 + *
101 + * This software is licensed under the terms of the GNU General Public
102 + * License version 2, as published by the Free Software Foundation, and
103 + * may be copied, distributed, and modified under those terms.
104 + *
105 + * This program is distributed in the hope that it will be useful,
106 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
107 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
108 + * GNU General Public License for more details.
109 + */
110 +
111 +#include <linux/clk.h>
112 +#include <linux/slab.h>
113 +#include <linux/bitops.h>
114 +#include <linux/dma-mapping.h>
115 +#include <linux/dmaengine.h>
116 +#include <linux/module.h>
117 +#include <linux/mtd/nand.h>
118 +#include <linux/mtd/partitions.h>
119 +#include <linux/of.h>
120 +#include <linux/of_device.h>
121 +#include <linux/of_mtd.h>
122 +#include <linux/delay.h>
123 +
124 +/* NANDc reg offsets */
125 +#define NAND_FLASH_CMD 0x00
126 +#define NAND_ADDR0 0x04
127 +#define NAND_ADDR1 0x08
128 +#define NAND_FLASH_CHIP_SELECT 0x0c
129 +#define NAND_EXEC_CMD 0x10
130 +#define NAND_FLASH_STATUS 0x14
131 +#define NAND_BUFFER_STATUS 0x18
132 +#define NAND_DEV0_CFG0 0x20
133 +#define NAND_DEV0_CFG1 0x24
134 +#define NAND_DEV0_ECC_CFG 0x28
135 +#define NAND_DEV1_ECC_CFG 0x2c
136 +#define NAND_DEV1_CFG0 0x30
137 +#define NAND_DEV1_CFG1 0x34
138 +#define NAND_READ_ID 0x40
139 +#define NAND_READ_STATUS 0x44
140 +#define NAND_DEV_CMD0 0xa0
141 +#define NAND_DEV_CMD1 0xa4
142 +#define NAND_DEV_CMD2 0xa8
143 +#define NAND_DEV_CMD_VLD 0xac
144 +#define SFLASHC_BURST_CFG 0xe0
145 +#define NAND_ERASED_CW_DETECT_CFG 0xe8
146 +#define NAND_ERASED_CW_DETECT_STATUS 0xec
147 +#define NAND_EBI2_ECC_BUF_CFG 0xf0
148 +#define FLASH_BUF_ACC 0x100
149 +
150 +#define NAND_CTRL 0xf00
151 +#define NAND_VERSION 0xf08
152 +#define NAND_READ_LOCATION_0 0xf20
153 +#define NAND_READ_LOCATION_1 0xf24
154 +
155 +/* dummy register offsets, used by write_reg_dma */
156 +#define NAND_DEV_CMD1_RESTORE 0xdead
157 +#define NAND_DEV_CMD_VLD_RESTORE 0xbeef
158 +
159 +/* NAND_FLASH_CMD bits */
160 +#define PAGE_ACC BIT(4)
161 +#define LAST_PAGE BIT(5)
162 +
163 +/* NAND_FLASH_CHIP_SELECT bits */
164 +#define NAND_DEV_SEL 0
165 +#define DM_EN BIT(2)
166 +
167 +/* NAND_FLASH_STATUS bits */
168 +#define FS_OP_ERR BIT(4)
169 +#define FS_READY_BSY_N BIT(5)
170 +#define FS_MPU_ERR BIT(8)
171 +#define FS_DEVICE_STS_ERR BIT(16)
172 +#define FS_DEVICE_WP BIT(23)
173 +
174 +/* NAND_BUFFER_STATUS bits */
175 +#define BS_UNCORRECTABLE_BIT BIT(8)
176 +#define BS_CORRECTABLE_ERR_MSK 0x1f
177 +
178 +/* NAND_DEVn_CFG0 bits */
179 +#define DISABLE_STATUS_AFTER_WRITE 4
180 +#define CW_PER_PAGE 6
181 +#define UD_SIZE_BYTES 9
182 +#define ECC_PARITY_SIZE_BYTES_RS 19
183 +#define SPARE_SIZE_BYTES 23
184 +#define NUM_ADDR_CYCLES 27
185 +#define STATUS_BFR_READ 30
186 +#define SET_RD_MODE_AFTER_STATUS 31
187 +
188 +/* NAND_DEVn_CFG0 bits */
189 +#define DEV0_CFG1_ECC_DISABLE 0
190 +#define WIDE_FLASH 1
191 +#define NAND_RECOVERY_CYCLES 2
192 +#define CS_ACTIVE_BSY 5
193 +#define BAD_BLOCK_BYTE_NUM 6
194 +#define BAD_BLOCK_IN_SPARE_AREA 16
195 +#define WR_RD_BSY_GAP 17
196 +#define ENABLE_BCH_ECC 27
197 +
198 +/* NAND_DEV0_ECC_CFG bits */
199 +#define ECC_CFG_ECC_DISABLE 0
200 +#define ECC_SW_RESET 1
201 +#define ECC_MODE 4
202 +#define ECC_PARITY_SIZE_BYTES_BCH 8
203 +#define ECC_NUM_DATA_BYTES 16
204 +#define ECC_FORCE_CLK_OPEN 30
205 +
206 +/* NAND_DEV_CMD1 bits */
207 +#define READ_ADDR 0
208 +
209 +/* NAND_DEV_CMD_VLD bits */
210 +#define READ_START_VLD 0
211 +
212 +/* NAND_EBI2_ECC_BUF_CFG bits */
213 +#define NUM_STEPS 0
214 +
215 +/* NAND_ERASED_CW_DETECT_CFG bits */
216 +#define ERASED_CW_ECC_MASK 1
217 +#define AUTO_DETECT_RES 0
218 +#define MASK_ECC (1 << ERASED_CW_ECC_MASK)
219 +#define RESET_ERASED_DET (1 << AUTO_DETECT_RES)
220 +#define ACTIVE_ERASED_DET (0 << AUTO_DETECT_RES)
221 +#define CLR_ERASED_PAGE_DET (RESET_ERASED_DET | MASK_ECC)
222 +#define SET_ERASED_PAGE_DET (ACTIVE_ERASED_DET | MASK_ECC)
223 +
224 +/* NAND_ERASED_CW_DETECT_STATUS bits */
225 +#define PAGE_ALL_ERASED BIT(7)
226 +#define CODEWORD_ALL_ERASED BIT(6)
227 +#define PAGE_ERASED BIT(5)
228 +#define CODEWORD_ERASED BIT(4)
229 +#define ERASED_PAGE (PAGE_ALL_ERASED | PAGE_ERASED)
230 +#define ERASED_CW (CODEWORD_ALL_ERASED | CODEWORD_ERASED)
231 +
232 +/* Version Mask */
233 +#define NAND_VERSION_MAJOR_MASK 0xf0000000
234 +#define NAND_VERSION_MAJOR_SHIFT 28
235 +#define NAND_VERSION_MINOR_MASK 0x0fff0000
236 +#define NAND_VERSION_MINOR_SHIFT 16
237 +
238 +/* NAND OP_CMDs */
239 +#define PAGE_READ 0x2
240 +#define PAGE_READ_WITH_ECC 0x3
241 +#define PAGE_READ_WITH_ECC_SPARE 0x4
242 +#define PROGRAM_PAGE 0x6
243 +#define PAGE_PROGRAM_WITH_ECC 0x7
244 +#define PROGRAM_PAGE_SPARE 0x9
245 +#define BLOCK_ERASE 0xa
246 +#define FETCH_ID 0xb
247 +#define RESET_DEVICE 0xd
248 +
249 +/*
250 + * the NAND controller performs reads/writes with ECC in 516 byte chunks.
251 + * the driver calls the chunks 'step' or 'codeword' interchangeably
252 + */
253 +#define NANDC_STEP_SIZE 512
254 +
255 +/*
256 + * the largest page size we support is 8K, this will have 16 steps/codewords
257 + * of 512 bytes each
258 + */
259 +#define MAX_NUM_STEPS (SZ_8K / NANDC_STEP_SIZE)
260 +
261 +/* we read at most 3 registers per codeword scan */
262 +#define MAX_REG_RD (3 * MAX_NUM_STEPS)
263 +
264 +/* ECC modes */
265 +#define ECC_NONE BIT(0)
266 +#define ECC_RS_4BIT BIT(1)
267 +#define ECC_BCH_4BIT BIT(2)
268 +#define ECC_BCH_8BIT BIT(3)
269 +
270 +struct desc_info {
271 + struct list_head list;
272 +
273 + enum dma_transfer_direction dir;
274 + struct scatterlist sgl;
275 + struct dma_async_tx_descriptor *dma_desc;
276 +};
277 +
278 +/*
279 + * holds the current register values that we want to write. acts as a contiguous
280 + * chunk of memory which we use to write the controller registers through DMA.
281 + */
282 +struct nandc_regs {
283 + u32 cmd;
284 + u32 addr0;
285 + u32 addr1;
286 + u32 chip_sel;
287 + u32 exec;
288 +
289 + u32 cfg0;
290 + u32 cfg1;
291 + u32 ecc_bch_cfg;
292 +
293 + u32 clrflashstatus;
294 + u32 clrreadstatus;
295 +
296 + u32 cmd1;
297 + u32 vld;
298 +
299 + u32 orig_cmd1;
300 + u32 orig_vld;
301 +
302 + u32 ecc_buf_cfg;
303 +};
304 +
305 +/*
306 + * @cmd_crci: ADM DMA CRCI for command flow control
307 + * @data_crci: ADM DMA CRCI for data flow control
308 + * @list: DMA descriptor list (list of desc_infos)
309 + * @dma_done: completion param to denote end of last
310 + * descriptor in the list
311 + * @data_buffer: our local DMA buffer for page read/writes,
312 + * used when we can't use the buffer provided
313 + * by upper layers directly
314 + * @buf_size/count/start: markers for chip->read_buf/write_buf functions
315 + * @reg_read_buf: buffer for reading register data via DMA
316 + * @reg_read_pos: marker for data read in reg_read_buf
317 + * @cfg0, cfg1, cfg0_raw..: NANDc register configurations needed for
318 + * ecc/non-ecc mode for the current nand flash
319 + * device
320 + * @regs: a contiguous chunk of memory for DMA register
321 + * writes
322 + * @ecc_strength: 4 bit or 8 bit ecc, received via DT
323 + * @bus_width: 8 bit or 16 bit NAND bus width, received via DT
324 + * @ecc_modes: supported ECC modes by the current controller,
325 + * initialized via DT match data
326 + * @cw_size: the number of bytes in a single step/codeword
327 + * of a page, consisting of all data, ecc, spare
328 + * and reserved bytes
329 + * @cw_data: the number of bytes within a codeword protected
330 + * by ECC
331 + * @bch_enabled: flag to tell whether BCH or RS ECC mode is used
332 + * @status: value to be returned if NAND_CMD_STATUS command
333 + * is executed
334 + */
335 +struct qcom_nandc_data {
336 + struct platform_device *pdev;
337 + struct device *dev;
338 +
339 + void __iomem *base;
340 + struct resource *res;
341 +
342 + struct clk *core_clk;
343 + struct clk *aon_clk;
344 +
345 + /* DMA stuff */
346 + struct dma_chan *chan;
347 + struct dma_slave_config slave_conf;
348 + unsigned int cmd_crci;
349 + unsigned int data_crci;
350 + struct list_head list;
351 + struct completion dma_done;
352 +
353 + /* MTD stuff */
354 + struct nand_chip chip;
355 + struct mtd_info mtd;
356 +
357 + /* local data buffer and markers */
358 + u8 *data_buffer;
359 + int buf_size;
360 + int buf_count;
361 + int buf_start;
362 +
363 + /* local buffer to read back registers */
364 + u32 *reg_read_buf;
365 + int reg_read_pos;
366 +
367 + /* required configs */
368 + u32 cfg0, cfg1;
369 + u32 cfg0_raw, cfg1_raw;
370 + u32 ecc_buf_cfg;
371 + u32 ecc_bch_cfg;
372 + u32 clrflashstatus;
373 + u32 clrreadstatus;
374 + u32 sflashc_burst_cfg;
375 + u32 cmd1, vld;
376 +
377 + /* register state */
378 + struct nandc_regs *regs;
379 +
380 + /* things we get from DT */
381 + int ecc_strength;
382 + int bus_width;
383 +
384 + u32 ecc_modes;
385 +
386 + /* misc params */
387 + int cw_size;
388 + int cw_data;
389 + bool use_ecc;
390 + bool bch_enabled;
391 + u8 status;
392 + int last_command;
393 +};
394 +
395 +static inline u32 nandc_read(struct qcom_nandc_data *this, int offset)
396 +{
397 + return ioread32(this->base + offset);
398 +}
399 +
400 +static inline void nandc_write(struct qcom_nandc_data *this, int offset,
401 + u32 val)
402 +{
403 + iowrite32(val, this->base + offset);
404 +}
405 +
406 +/* helper to configure address register values */
407 +static void set_address(struct qcom_nandc_data *this, u16 column, int page)
408 +{
409 + struct nand_chip *chip = &this->chip;
410 + struct nandc_regs *regs = this->regs;
411 +
412 + if (chip->options & NAND_BUSWIDTH_16)
413 + column >>= 1;
414 +
415 + regs->addr0 = page << 16 | column;
416 + regs->addr1 = page >> 16 & 0xff;
417 +}
418 +
419 +/*
420 + * update_rw_regs: set up read/write register values, these will be
421 + * written to the NAND controller registers via DMA
422 + *
423 + * @num_cw: number of steps for the read/write operation
424 + * @read: read or write operation
425 + */
426 +static void update_rw_regs(struct qcom_nandc_data *this, int num_cw, bool read)
427 +{
428 + struct nandc_regs *regs = this->regs;
429 +
430 + if (read) {
431 + if (this->use_ecc)
432 + regs->cmd = PAGE_READ_WITH_ECC | PAGE_ACC | LAST_PAGE;
433 + else
434 + regs->cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
435 + } else {
436 + regs->cmd = PROGRAM_PAGE | PAGE_ACC | LAST_PAGE;
437 + }
438 +
439 + if (this->use_ecc) {
440 + regs->cfg0 = (this->cfg0 & ~(7U << CW_PER_PAGE)) |
441 + (num_cw - 1) << CW_PER_PAGE;
442 +
443 + regs->cfg1 = this->cfg1;
444 + regs->ecc_bch_cfg = this->ecc_bch_cfg;
445 + } else {
446 + regs->cfg0 = (this->cfg0_raw & ~(7U << CW_PER_PAGE)) |
447 + (num_cw - 1) << CW_PER_PAGE;
448 +
449 + regs->cfg1 = this->cfg1_raw;
450 + regs->ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
451 + }
452 +
453 + regs->ecc_buf_cfg = this->ecc_buf_cfg;
454 + regs->clrflashstatus = this->clrflashstatus;
455 + regs->clrreadstatus = this->clrreadstatus;
456 + regs->exec = 1;
457 +}
458 +
459 +static int prep_dma_desc(struct qcom_nandc_data *this, bool read, int reg_off,
460 + const void *vaddr, int size, bool flow_control)
461 +{
462 + struct desc_info *desc;
463 + struct dma_async_tx_descriptor *dma_desc;
464 + struct scatterlist *sgl;
465 + struct dma_slave_config slave_conf;
466 + int r;
467 +
468 + desc = kzalloc(sizeof(*desc), GFP_KERNEL);
469 + if (!desc)
470 + return -ENOMEM;
471 +
472 + list_add_tail(&desc->list, &this->list);
473 +
474 + sgl = &desc->sgl;
475 +
476 + sg_init_one(sgl, vaddr, size);
477 +
478 + desc->dir = read ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV;
479 +
480 + r = dma_map_sg(this->dev, sgl, 1, desc->dir);
481 + if (r == 0) {
482 + r = -ENOMEM;
483 + goto err;
484 + }
485 +
486 + memset(&slave_conf, 0x00, sizeof(slave_conf));
487 +
488 + slave_conf.device_fc = flow_control;
489 + if (read) {
490 + slave_conf.src_maxburst = 16;
491 + slave_conf.src_addr = this->res->start + reg_off;
492 + slave_conf.slave_id = this->data_crci;
493 + } else {
494 + slave_conf.dst_maxburst = 16;
495 + slave_conf.dst_addr = this->res->start + reg_off;
496 + slave_conf.slave_id = this->cmd_crci;
497 + }
498 +
499 + r = dmaengine_slave_config(this->chan, &slave_conf);
500 + if (r) {
501 + dev_err(this->dev, "failed to configure dma channel\n");
502 + goto err;
503 + }
504 +
505 + dma_desc = dmaengine_prep_slave_sg(this->chan, sgl, 1, desc->dir, 0);
506 + if (!dma_desc) {
507 + dev_err(this->dev, "failed to prepare desc\n");
508 + r = -EINVAL;
509 + goto err;
510 + }
511 +
512 + desc->dma_desc = dma_desc;
513 +
514 + return 0;
515 +err:
516 + kfree(desc);
517 +
518 + return r;
519 +}
520 +
521 +/*
522 + * read_reg_dma: prepares a descriptor to read a given number of
523 + * contiguous registers to the reg_read_buf pointer
524 + *
525 + * @first: offset of the first register in the contiguous block
526 + * @num_regs: number of registers to read
527 + */
528 +static int read_reg_dma(struct qcom_nandc_data *this, int first, int num_regs)
529 +{
530 + bool flow_control = false;
531 + void *vaddr;
532 + int size;
533 +
534 + if (first == NAND_READ_ID || first == NAND_FLASH_STATUS)
535 + flow_control = true;
536 +
537 + size = num_regs * sizeof(u32);
538 + vaddr = this->reg_read_buf + this->reg_read_pos;
539 + this->reg_read_pos += num_regs;
540 +
541 + return prep_dma_desc(this, true, first, vaddr, size, flow_control);
542 +}
543 +
544 +/*
545 + * write_reg_dma: prepares a descriptor to write a given number of
546 + * contiguous registers
547 + *
548 + * @first: offset of the first register in the contiguous block
549 + * @num_regs: number of registers to write
550 + */
551 +static int write_reg_dma(struct qcom_nandc_data *this, int first, int num_regs)
552 +{
553 + bool flow_control = false;
554 + struct nandc_regs *regs = this->regs;
555 + void *vaddr;
556 + int size;
557 +
558 + switch (first) {
559 + case NAND_FLASH_CMD:
560 + vaddr = &regs->cmd;
561 + flow_control = true;
562 + break;
563 + case NAND_EXEC_CMD:
564 + vaddr = &regs->exec;
565 + break;
566 + case NAND_FLASH_STATUS:
567 + vaddr = &regs->clrflashstatus;
568 + break;
569 + case NAND_DEV0_CFG0:
570 + vaddr = &regs->cfg0;
571 + break;
572 + case NAND_READ_STATUS:
573 + vaddr = &regs->clrreadstatus;
574 + break;
575 + case NAND_DEV_CMD1:
576 + vaddr = &regs->cmd1;
577 + break;
578 + case NAND_DEV_CMD1_RESTORE:
579 + first = NAND_DEV_CMD1;
580 + vaddr = &regs->orig_cmd1;
581 + break;
582 + case NAND_DEV_CMD_VLD:
583 + vaddr = &regs->vld;
584 + break;
585 + case NAND_DEV_CMD_VLD_RESTORE:
586 + first = NAND_DEV_CMD_VLD;
587 + vaddr = &regs->orig_vld;
588 + break;
589 + case NAND_EBI2_ECC_BUF_CFG:
590 + vaddr = &regs->ecc_buf_cfg;
591 + break;
592 + default:
593 + dev_err(this->dev, "invalid starting register\n");
594 + return -EINVAL;
595 + }
596 +
597 + size = num_regs * sizeof(u32);
598 +
599 + return prep_dma_desc(this, false, first, vaddr, size, flow_control);
600 +}
601 +
602 +/*
603 + * read_data_dma: prepares a DMA descriptor to transfer data from the
604 + * controller's internal buffer to the buffer 'vaddr'
605 + *
606 + * @reg_off: offset within the controller's data buffer
607 + * @vaddr: virtual address of the buffer we want to write to
608 + * @size: DMA transaction size in bytes
609 + */
610 +static int read_data_dma(struct qcom_nandc_data *this, int reg_off,
611 + const u8 *vaddr, int size)
612 +{
613 + return prep_dma_desc(this, true, reg_off, vaddr, size, false);
614 +}
615 +
616 +/*
617 + * write_data_dma: prepares a DMA descriptor to transfer data from
618 + * 'vaddr' to the controller's internal buffer
619 + *
620 + * @reg_off: offset within the controller's data buffer
621 + * @vaddr: virtual address of the buffer we want to read from
622 + * @size: DMA transaction size in bytes
623 + */
624 +static int write_data_dma(struct qcom_nandc_data *this, int reg_off,
625 + const u8 *vaddr, int size)
626 +{
627 + return prep_dma_desc(this, false, reg_off, vaddr, size, false);
628 +}
629 +
630 +/*
631 + * helper to prepare dma descriptors to configure registers needed for reading a
632 + * codeword/step in a page
633 + */
634 +static void config_cw_read(struct qcom_nandc_data *this)
635 +{
636 + write_reg_dma(this, NAND_FLASH_CMD, 3);
637 + write_reg_dma(this, NAND_DEV0_CFG0, 3);
638 + write_reg_dma(this, NAND_EBI2_ECC_BUF_CFG, 1);
639 +
640 + write_reg_dma(this, NAND_EXEC_CMD, 1);
641 +
642 + read_reg_dma(this, NAND_FLASH_STATUS, 2);
643 + read_reg_dma(this, NAND_ERASED_CW_DETECT_STATUS, 1);
644 +}
645 +
646 +/*
647 + * helpers to prepare dma descriptors used to configure registers needed for
648 + * writing a codeword/step in a page
649 + */
650 +static void config_cw_write_pre(struct qcom_nandc_data *this)
651 +{
652 + write_reg_dma(this, NAND_FLASH_CMD, 3);
653 + write_reg_dma(this, NAND_DEV0_CFG0, 3);
654 + write_reg_dma(this, NAND_EBI2_ECC_BUF_CFG, 1);
655 +}
656 +
657 +static void config_cw_write_post(struct qcom_nandc_data *this)
658 +{
659 + write_reg_dma(this, NAND_EXEC_CMD, 1);
660 +
661 + read_reg_dma(this, NAND_FLASH_STATUS, 1);
662 +
663 + write_reg_dma(this, NAND_FLASH_STATUS, 1);
664 + write_reg_dma(this, NAND_READ_STATUS, 1);
665 +}
666 +
667 +/*
668 + * the following functions are used within chip->cmdfunc() to perform different
669 + * NAND_CMD_* commands
670 + */
671 +
672 +/* sets up descriptors for NAND_CMD_PARAM */
673 +static int nandc_param(struct qcom_nandc_data *this)
674 +{
675 + struct nandc_regs *regs = this->regs;
676 +
677 + /*
678 + * NAND_CMD_PARAM is called before we know much about the FLASH chip
679 + * in use. we configure the controller to perform a raw read of 512
680 + * bytes to read onfi params
681 + */
682 + regs->cmd = PAGE_READ | PAGE_ACC | LAST_PAGE;
683 + regs->addr0 = 0;
684 + regs->addr1 = 0;
685 + regs->cfg0 = 0 << CW_PER_PAGE
686 + | 512 << UD_SIZE_BYTES
687 + | 5 << NUM_ADDR_CYCLES
688 + | 0 << SPARE_SIZE_BYTES;
689 +
690 + regs->cfg1 = 7 << NAND_RECOVERY_CYCLES
691 + | 0 << CS_ACTIVE_BSY
692 + | 17 << BAD_BLOCK_BYTE_NUM
693 + | 1 << BAD_BLOCK_IN_SPARE_AREA
694 + | 2 << WR_RD_BSY_GAP
695 + | 0 << WIDE_FLASH
696 + | 1 << DEV0_CFG1_ECC_DISABLE;
697 +
698 + regs->ecc_bch_cfg = 1 << ECC_CFG_ECC_DISABLE;
699 +
700 + /* configure CMD1 and VLD for ONFI param probing */
701 + regs->vld = (this->vld & ~(1 << READ_START_VLD))
702 + | 0 << READ_START_VLD;
703 +
704 + regs->cmd1 = (this->cmd1 & ~(0xFF << READ_ADDR))
705 + | NAND_CMD_PARAM << READ_ADDR;
706 +
707 + regs->exec = 1;
708 +
709 + regs->orig_cmd1 = this->cmd1;
710 + regs->orig_vld = this->vld;
711 +
712 + write_reg_dma(this, NAND_DEV_CMD_VLD, 1);
713 + write_reg_dma(this, NAND_DEV_CMD1, 1);
714 +
715 + this->buf_count = 512;
716 + memset(this->data_buffer, 0xff, this->buf_count);
717 +
718 + config_cw_read(this);
719 +
720 + read_data_dma(this, FLASH_BUF_ACC, this->data_buffer, this->buf_count);
721 +
722 + /* restore CMD1 and VLD regs */
723 + write_reg_dma(this, NAND_DEV_CMD1_RESTORE, 1);
724 + write_reg_dma(this, NAND_DEV_CMD_VLD_RESTORE, 1);
725 +
726 + return 0;
727 +}
728 +
729 +/* sets up descriptors for NAND_CMD_ERASE1 */
730 +static int erase_block(struct qcom_nandc_data *this, int page_addr)
731 +{
732 + struct nandc_regs *regs = this->regs;
733 +
734 + regs->cmd = BLOCK_ERASE | PAGE_ACC | LAST_PAGE;
735 + regs->addr0 = page_addr;
736 + regs->addr1 = 0;
737 + regs->cfg0 = this->cfg0_raw & ~(7 << CW_PER_PAGE);
738 + regs->cfg1 = this->cfg1_raw;
739 + regs->exec = 1;
740 + regs->clrflashstatus = this->clrflashstatus;
741 + regs->clrreadstatus = this->clrreadstatus;
742 +
743 + write_reg_dma(this, NAND_FLASH_CMD, 3);
744 + write_reg_dma(this, NAND_DEV0_CFG0, 2);
745 + write_reg_dma(this, NAND_EXEC_CMD, 1);
746 +
747 + read_reg_dma(this, NAND_FLASH_STATUS, 1);
748 +
749 + write_reg_dma(this, NAND_FLASH_STATUS, 1);
750 + write_reg_dma(this, NAND_READ_STATUS, 1);
751 +
752 + return 0;
753 +}
754 +
755 +/* sets up descriptors for NAND_CMD_READID */
756 +static int read_id(struct qcom_nandc_data *this, int column)
757 +{
758 + struct nandc_regs *regs = this->regs;
759 +
760 + if (column == -1)
761 + return 0;
762 +
763 + regs->cmd = FETCH_ID;
764 + regs->addr0 = column;
765 + regs->addr1 = 0;
766 + regs->chip_sel = DM_EN;
767 + regs->exec = 1;
768 +
769 + write_reg_dma(this, NAND_FLASH_CMD, 4);
770 + write_reg_dma(this, NAND_EXEC_CMD, 1);
771 +
772 + read_reg_dma(this, NAND_READ_ID, 1);
773 +
774 + return 0;
775 +}
776 +
777 +/* sets up descriptors for NAND_CMD_RESET */
778 +static int reset(struct qcom_nandc_data *this)
779 +{
780 + struct nandc_regs *regs = this->regs;
781 +
782 + regs->cmd = RESET_DEVICE;
783 + regs->exec = 1;
784 +
785 + write_reg_dma(this, NAND_FLASH_CMD, 1);
786 + write_reg_dma(this, NAND_EXEC_CMD, 1);
787 +
788 + read_reg_dma(this, NAND_FLASH_STATUS, 1);
789 +
790 + return 0;
791 +}
792 +
793 +/* helpers to submit/free our list of dma descriptors */
794 +static void dma_callback(void *param)
795 +{
796 + struct qcom_nandc_data *this = param;
797 + struct completion *c = &this->dma_done;
798 +
799 + complete(c);
800 +}
801 +
802 +static int submit_descs(struct qcom_nandc_data *this)
803 +{
804 + struct completion *c = &this->dma_done;
805 + struct desc_info *desc;
806 + int r;
807 +
808 + init_completion(c);
809 +
810 + list_for_each_entry(desc, &this->list, list) {
811 + /*
812 + * we add a callback to the last descriptor in our list to
813 + * notify completion of command
814 + */
815 + if (list_is_last(&desc->list, &this->list)) {
816 + desc->dma_desc->callback = dma_callback;
817 + desc->dma_desc->callback_param = this;
818 + }
819 +
820 + dmaengine_submit(desc->dma_desc);
821 + }
822 +
823 + dma_async_issue_pending(this->chan);
824 +
825 + r = wait_for_completion_timeout(c, msecs_to_jiffies(500));
826 + if (!r)
827 + return -ETIMEDOUT;
828 +
829 + return 0;
830 +}
831 +
832 +static void free_descs(struct qcom_nandc_data *this)
833 +{
834 + struct desc_info *desc, *n;
835 +
836 + list_for_each_entry_safe(desc, n, &this->list, list) {
837 + list_del(&desc->list);
838 + dma_unmap_sg(this->dev, &desc->sgl, 1, desc->dir);
839 + kfree(desc);
840 + }
841 +}
842 +
843 +/* reset the register read buffer for next NAND operation */
844 +static void clear_read_regs(struct qcom_nandc_data *this)
845 +{
846 + this->reg_read_pos = 0;
847 + memset(this->reg_read_buf, 0, MAX_REG_RD * sizeof(*this->reg_read_buf));
848 +}
849 +
850 +static void pre_command(struct qcom_nandc_data *this, int command)
851 +{
852 + this->buf_count = 0;
853 + this->buf_start = 0;
854 + this->use_ecc = false;
855 + this->last_command = command;
856 +
857 + clear_read_regs(this);
858 +}
859 +
860 +/*
861 + * this is called after NAND_CMD_PAGEPROG and NAND_CMD_ERASE1 to set our
862 + * privately maintained status byte, this status byte can be read after
863 + * NAND_CMD_STATUS is called
864 + */
865 +static void parse_erase_write_errors(struct qcom_nandc_data *this, int command)
866 +{
867 + struct nand_chip *chip = &this->chip;
868 + struct nand_ecc_ctrl *ecc = &chip->ecc;
869 + int num_cw;
870 + int i;
871 +
872 + num_cw = command == NAND_CMD_PAGEPROG ? ecc->steps : 1;
873 +
874 + for (i = 0; i < num_cw; i++) {
875 + __le32 flash_status = le32_to_cpu(this->reg_read_buf[i]);
876 +
877 + if (flash_status & FS_MPU_ERR)
878 + this->status &= ~NAND_STATUS_WP;
879 +
880 + if (flash_status & FS_OP_ERR || (i == (num_cw - 1) &&
881 + (flash_status & FS_DEVICE_STS_ERR)))
882 + this->status |= NAND_STATUS_FAIL;
883 + }
884 +}
885 +
886 +static void post_command(struct qcom_nandc_data *this, int command)
887 +{
888 + switch (command) {
889 + case NAND_CMD_READID:
890 + memcpy(this->data_buffer, this->reg_read_buf, this->buf_count);
891 + break;
892 + case NAND_CMD_PAGEPROG:
893 + case NAND_CMD_ERASE1:
894 + parse_erase_write_errors(this, command);
895 + break;
896 + default:
897 + break;
898 + }
899 +}
900 +
901 +/*
902 + * Implements chip->cmdfunc. It's only used for a limited set of commands.
903 + * The rest of the commands wouldn't be called by upper layers. For example,
904 + * NAND_CMD_READOOB would never be called because we have our own versions
905 + * of read_oob ops for nand_ecc_ctrl.
906 + */
907 +static void qcom_nandc_command(struct mtd_info *mtd, unsigned int command,
908 + int column, int page_addr)
909 +{
910 + struct nand_chip *chip = mtd->priv;
911 + struct nand_ecc_ctrl *ecc = &chip->ecc;
912 + struct qcom_nandc_data *this = chip->priv;
913 + bool wait = false;
914 + int r = 0;
915 +
916 + pre_command(this, command);
917 +
918 + switch (command) {
919 + case NAND_CMD_RESET:
920 + r = reset(this);
921 + wait = true;
922 + break;
923 +
924 + case NAND_CMD_READID:
925 + this->buf_count = 4;
926 + r = read_id(this, column);
927 + wait = true;
928 + break;
929 +
930 + case NAND_CMD_PARAM:
931 + r = nandc_param(this);
932 + wait = true;
933 + break;
934 +
935 + case NAND_CMD_ERASE1:
936 + r = erase_block(this, page_addr);
937 + wait = true;
938 + break;
939 +
940 + case NAND_CMD_READ0:
941 + /* we read the entire page for now */
942 + WARN_ON(column != 0);
943 +
944 + this->use_ecc = true;
945 + set_address(this, 0, page_addr);
946 + update_rw_regs(this, ecc->steps, true);
947 + break;
948 +
949 + case NAND_CMD_SEQIN:
950 + WARN_ON(column != 0);
951 + set_address(this, 0, page_addr);
952 + break;
953 +
954 + case NAND_CMD_PAGEPROG:
955 + case NAND_CMD_STATUS:
956 + case NAND_CMD_NONE:
957 + default:
958 + break;
959 + }
960 +
961 + if (r) {
962 + dev_err(this->dev, "failure executing command %d\n",
963 + command);
964 + free_descs(this);
965 + return;
966 + }
967 +
968 + if (wait) {
969 + r = submit_descs(this);
970 + if (r)
971 + dev_err(this->dev,
972 + "failure submitting descs for command %d\n",
973 + command);
974 + }
975 +
976 + free_descs(this);
977 +
978 + post_command(this, command);
979 +}
980 +
981 +/*
982 + * when using RS ECC, the NAND controller flags an error when reading an
983 + * erased page. however, there are special characters at certain offsets when
984 + * we read the erased page. we check here if the page is really empty. if so,
985 + * we replace the magic characters with 0xffs
986 + */
987 +static bool empty_page_fixup(struct qcom_nandc_data *this, u8 *data_buf)
988 +{
989 + struct mtd_info *mtd = &this->mtd;
990 + struct nand_chip *chip = &this->chip;
991 + struct nand_ecc_ctrl *ecc = &chip->ecc;
992 + int cwperpage = ecc->steps;
993 + u8 orig1[MAX_NUM_STEPS], orig2[MAX_NUM_STEPS];
994 + int i, j;
995 +
996 + /* if BCH is enabled, HW will take care of detecting erased pages */
997 + if (this->bch_enabled || !this->use_ecc)
998 + return false;
999 +
1000 + for (i = 0; i < cwperpage; i++) {
1001 + u8 *empty1, *empty2;
1002 + __le32 flash_status = le32_to_cpu(this->reg_read_buf[3 * i]);
1003 +
1004 + /*
1005 + * an erased page flags an error in NAND_FLASH_STATUS, check if
1006 + * the page is erased by looking for 0x54s at offsets 3 and 175
1007 + * from the beginning of each codeword
1008 + */
1009 + if (!(flash_status & FS_OP_ERR))
1010 + break;
1011 +
1012 + empty1 = &data_buf[3 + i * this->cw_data];
1013 + empty2 = &data_buf[175 + i * this->cw_data];
1014 +
1015 + /*
1016 + * if the error wasn't because of an erased page, bail out and
1017 + * and let someone else do the error checking
1018 + */
1019 + if ((*empty1 == 0x54 && *empty2 == 0xff) ||
1020 + (*empty1 == 0xff && *empty2 == 0x54)) {
1021 + orig1[i] = *empty1;
1022 + orig2[i] = *empty2;
1023 +
1024 + *empty1 = 0xff;
1025 + *empty2 = 0xff;
1026 + } else {
1027 + break;
1028 + }
1029 + }
1030 +
1031 + if (i < cwperpage || memchr_inv(data_buf, 0xff, mtd->writesize))
1032 + goto not_empty;
1033 +
1034 + /*
1035 + * tell the caller that the page was empty and is fixed up, so that
1036 + * parse_read_errors() doesn't think it's an error
1037 + */
1038 + return true;
1039 +
1040 +not_empty:
1041 + /* restore original values if not empty*/
1042 + for (j = 0; j < i; j++) {
1043 + data_buf[3 + j * this->cw_data] = orig1[j];
1044 + data_buf[175 + j * this->cw_data] = orig2[j];
1045 + }
1046 +
1047 + return false;
1048 +}
1049 +
1050 +struct read_stats {
1051 + __le32 flash;
1052 + __le32 buffer;
1053 + __le32 erased_cw;
1054 +};
1055 +
1056 +/*
1057 + * reads back status registers set by the controller to notify page read
1058 + * errors. this is equivalent to what 'ecc->correct()' would do.
1059 + */
1060 +static int parse_read_errors(struct qcom_nandc_data *this, bool erased_page)
1061 +{
1062 + struct mtd_info *mtd = &this->mtd;
1063 + struct nand_chip *chip = &this->chip;
1064 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1065 + int cwperpage = ecc->steps;
1066 + unsigned int max_bitflips = 0;
1067 + int i;
1068 +
1069 + for (i = 0; i < cwperpage; i++) {
1070 + int stat;
1071 + struct read_stats *buf;
1072 +
1073 + buf = (struct read_stats *) (this->reg_read_buf + 3 * i);
1074 +
1075 + buf->flash = le32_to_cpu(buf->flash);
1076 + buf->buffer = le32_to_cpu(buf->buffer);
1077 + buf->erased_cw = le32_to_cpu(buf->erased_cw);
1078 +
1079 + if (buf->flash & (FS_OP_ERR | FS_MPU_ERR)) {
1080 +
1081 + /* ignore erased codeword errors */
1082 + if (this->bch_enabled) {
1083 + if ((buf->erased_cw & ERASED_CW) == ERASED_CW)
1084 + continue;
1085 + } else if (erased_page) {
1086 + continue;
1087 + }
1088 +
1089 + if (buf->buffer & BS_UNCORRECTABLE_BIT) {
1090 + mtd->ecc_stats.failed++;
1091 + continue;
1092 + }
1093 + }
1094 +
1095 + stat = buf->buffer & BS_CORRECTABLE_ERR_MSK;
1096 + mtd->ecc_stats.corrected += stat;
1097 +
1098 + max_bitflips = max_t(unsigned int, max_bitflips, stat);
1099 + }
1100 +
1101 + return max_bitflips;
1102 +}
1103 +
1104 +/*
1105 + * helper to perform the actual page read operation, used by ecc->read_page()
1106 + * and ecc->read_oob()
1107 + */
1108 +static int read_page_low(struct qcom_nandc_data *this, u8 *data_buf,
1109 + u8 *oob_buf)
1110 +{
1111 + struct nand_chip *chip = &this->chip;
1112 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1113 + int i, r;
1114 +
1115 + /* queue cmd descs for each codeword */
1116 + for (i = 0; i < ecc->steps; i++) {
1117 + int data_size, oob_size;
1118 +
1119 + if (i == (ecc->steps - 1)) {
1120 + data_size = ecc->size - ((ecc->steps - 1) << 2);
1121 + oob_size = (ecc->steps << 2) + ecc->bytes;
1122 + } else {
1123 + data_size = this->cw_data;
1124 + oob_size = ecc->bytes;
1125 + }
1126 +
1127 + config_cw_read(this);
1128 +
1129 + if (data_buf)
1130 + read_data_dma(this, FLASH_BUF_ACC, data_buf, data_size);
1131 +
1132 + if (oob_buf)
1133 + read_data_dma(this, FLASH_BUF_ACC + data_size, oob_buf,
1134 + oob_size);
1135 +
1136 + if (data_buf)
1137 + data_buf += data_size;
1138 + if (oob_buf)
1139 + oob_buf += oob_size;
1140 + }
1141 +
1142 + r = submit_descs(this);
1143 + if (r)
1144 + dev_err(this->dev, "failure to read page/oob\n");
1145 +
1146 + free_descs(this);
1147 +
1148 + return r;
1149 +}
1150 +
1151 +/*
1152 + * a helper that copies the last step/codeword of a page (containing free oob)
1153 + * into our local buffer
1154 + */
1155 +static int copy_last_cw(struct qcom_nandc_data *this, bool use_ecc, int page)
1156 +{
1157 + struct nand_chip *chip = &this->chip;
1158 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1159 + int size;
1160 + int r;
1161 +
1162 + clear_read_regs(this);
1163 +
1164 + size = use_ecc ? this->cw_data : this->cw_size;
1165 +
1166 + /* prepare a clean read buffer */
1167 + memset(this->data_buffer, 0xff, size);
1168 +
1169 + this->use_ecc = use_ecc;
1170 + set_address(this, this->cw_size * (ecc->steps - 1), page);
1171 + update_rw_regs(this, 1, true);
1172 +
1173 + config_cw_read(this);
1174 +
1175 + read_data_dma(this, FLASH_BUF_ACC, this->data_buffer, size);
1176 +
1177 + r = submit_descs(this);
1178 + if (r)
1179 + dev_err(this->dev, "failed to copy last codeword\n");
1180 +
1181 + free_descs(this);
1182 +
1183 + return r;
1184 +}
1185 +
1186 +/* implements ecc->read_page() */
1187 +static int qcom_nandc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1188 + uint8_t *buf, int oob_required, int page)
1189 +{
1190 + struct qcom_nandc_data *this = chip->priv;
1191 + u8 *data_buf, *oob_buf = NULL;
1192 + bool erased_page;
1193 + int r;
1194 +
1195 + data_buf = buf;
1196 + oob_buf = oob_required ? chip->oob_poi : NULL;
1197 +
1198 + r = read_page_low(this, data_buf, oob_buf);
1199 + if (r) {
1200 + dev_err(this->dev, "failure to read page\n");
1201 + return r;
1202 + }
1203 +
1204 + erased_page = empty_page_fixup(this, data_buf);
1205 +
1206 + return parse_read_errors(this, erased_page);
1207 +}
1208 +
1209 +/* implements ecc->read_oob() */
1210 +static int qcom_nandc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1211 + int page)
1212 +{
1213 + struct qcom_nandc_data *this = chip->priv;
1214 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1215 + int r;
1216 +
1217 + clear_read_regs(this);
1218 +
1219 + this->use_ecc = true;
1220 + set_address(this, 0, page);
1221 + update_rw_regs(this, ecc->steps, true);
1222 +
1223 + r = read_page_low(this, NULL, chip->oob_poi);
1224 + if (r)
1225 + dev_err(this->dev, "failure to read oob\n");
1226 +
1227 + return r;
1228 +}
1229 +
1230 +/* implements ecc->read_oob_raw(), used to read the bad block marker flag */
1231 +static int qcom_nandc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1232 + int page)
1233 +{
1234 + struct qcom_nandc_data *this = chip->priv;
1235 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1236 + uint8_t *oob = chip->oob_poi;
1237 + int start, length;
1238 + int r;
1239 +
1240 + /*
1241 + * configure registers for a raw page read, the address is set to the
1242 + * beginning of the last codeword, we don't care about reading ecc
1243 + * portion of oob, just the free stuff
1244 + */
1245 + r = copy_last_cw(this, false, page);
1246 + if (r)
1247 + return r;
1248 +
1249 + /*
1250 + * reading raw oob has 2 parts, first the bad block byte, then the
1251 + * actual free oob region. perform a memcpy in two steps
1252 + */
1253 + start = mtd->writesize - (this->cw_size * (ecc->steps - 1));
1254 + length = chip->options & NAND_BUSWIDTH_16 ? 2 : 1;
1255 +
1256 + memcpy(oob, this->data_buffer + start, length);
1257 +
1258 + oob += length;
1259 +
1260 + start = this->cw_data - (ecc->steps << 2) + 1;
1261 + length = ecc->steps << 2;
1262 +
1263 + memcpy(oob, this->data_buffer + start, length);
1264 +
1265 + return 0;
1266 +}
1267 +
1268 +/* implements ecc->write_page() */
1269 +static int qcom_nandc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1270 + const uint8_t *buf, int oob_required)
1271 +{
1272 + struct qcom_nandc_data *this = chip->priv;
1273 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1274 + u8 *data_buf, *oob_buf;
1275 + int i, r = 0;
1276 +
1277 + clear_read_regs(this);
1278 +
1279 + data_buf = (u8 *) buf;
1280 + oob_buf = chip->oob_poi;
1281 +
1282 + this->use_ecc = true;
1283 + update_rw_regs(this, ecc->steps, false);
1284 +
1285 + for (i = 0; i < ecc->steps; i++) {
1286 + int data_size, oob_size;
1287 +
1288 + if (i == (ecc->steps - 1)) {
1289 + data_size = ecc->size - ((ecc->steps - 1) << 2);
1290 + oob_size = (ecc->steps << 2) + ecc->bytes;
1291 + } else {
1292 + data_size = this->cw_data;
1293 + oob_size = ecc->bytes;
1294 + }
1295 +
1296 + config_cw_write_pre(this);
1297 + write_data_dma(this, FLASH_BUF_ACC, data_buf, data_size);
1298 +
1299 + /*
1300 + * we don't really need to write anything to oob for the
1301 + * first n - 1 codewords since these oob regions just
1302 + * contain ecc that's written by the controller itself
1303 + */
1304 + if (i == (ecc->steps - 1))
1305 + write_data_dma(this, FLASH_BUF_ACC + data_size,
1306 + oob_buf, oob_size);
1307 + config_cw_write_post(this);
1308 +
1309 + data_buf += data_size;
1310 + oob_buf += oob_size;
1311 + }
1312 +
1313 + r = submit_descs(this);
1314 + if (r)
1315 + dev_err(this->dev, "failure to write page\n");
1316 +
1317 + free_descs(this);
1318 +
1319 + return r;
1320 +}
1321 +
1322 +/*
1323 + * implements ecc->write_oob()
1324 + *
1325 + * the NAND controller cannot write only data or only oob within a codeword,
1326 + * since ecc is calculated for the combined codeword. we first copy the
1327 + * entire contents for the last codeword(data + oob), replace the old oob
1328 + * with the new one in chip->oob_poi, and then write the entire codeword.
1329 + * this read-copy-write operation results in a slight perormance loss.
1330 + */
1331 +static int qcom_nandc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
1332 + int page)
1333 +{
1334 + struct qcom_nandc_data *this = chip->priv;
1335 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1336 + uint8_t *oob = chip->oob_poi;
1337 + int free_boff;
1338 + int data_size, oob_size;
1339 + int r, status = 0;
1340 +
1341 + r = copy_last_cw(this, true, page);
1342 + if (r)
1343 + return r;
1344 +
1345 + clear_read_regs(this);
1346 +
1347 + /* calculate the data and oob size for the last codeword/step */
1348 + data_size = ecc->size - ((ecc->steps - 1) << 2);
1349 + oob_size = (ecc->steps << 2) + ecc->bytes;
1350 +
1351 + /*
1352 + * the location of spare data in the oob buffer, we could also use
1353 + * ecc->layout.oobfree here
1354 + */
1355 + free_boff = ecc->bytes * (ecc->steps - 1);
1356 +
1357 + /* override new oob content to last codeword */
1358 + memcpy(this->data_buffer + data_size, oob + free_boff, oob_size);
1359 +
1360 + this->use_ecc = true;
1361 + set_address(this, this->cw_size * (ecc->steps - 1), page);
1362 + update_rw_regs(this, 1, false);
1363 +
1364 + config_cw_write_pre(this);
1365 + write_data_dma(this, FLASH_BUF_ACC, this->data_buffer,
1366 + data_size + oob_size);
1367 + config_cw_write_post(this);
1368 +
1369 + r = submit_descs(this);
1370 +
1371 + free_descs(this);
1372 +
1373 + if (r) {
1374 + dev_err(this->dev, "failure to write oob\n");
1375 + return -EIO;
1376 + }
1377 +
1378 + chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1379 +
1380 + status = chip->waitfunc(mtd, chip);
1381 +
1382 + return status & NAND_STATUS_FAIL ? -EIO : 0;
1383 +}
1384 +
1385 +/* implements ecc->write_oob_raw(), used to write bad block marker flag */
1386 +static int qcom_nandc_write_oob_raw(struct mtd_info *mtd,
1387 + struct nand_chip *chip, int page)
1388 +{
1389 + struct qcom_nandc_data *this = chip->priv;
1390 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1391 + uint8_t *oob = chip->oob_poi;
1392 + int start, length;
1393 + int r, status = 0;
1394 +
1395 + r = copy_last_cw(this, false, page);
1396 + if (r)
1397 + return r;
1398 +
1399 + clear_read_regs(this);
1400 +
1401 + /*
1402 + * writing raw oob has 2 parts, first the bad block region, then the
1403 + * actual free region
1404 + */
1405 + start = mtd->writesize - (this->cw_size * (ecc->steps - 1));
1406 + length = chip->options & NAND_BUSWIDTH_16 ? 2 : 1;
1407 +
1408 + memcpy(this->data_buffer + start, oob, length);
1409 +
1410 + oob += length;
1411 +
1412 + start = this->cw_data - (ecc->steps << 2) + 1;
1413 + length = ecc->steps << 2;
1414 +
1415 + memcpy(this->data_buffer + start, oob, length);
1416 +
1417 + /* prepare write */
1418 + this->use_ecc = false;
1419 + set_address(this, this->cw_size * (ecc->steps - 1), page);
1420 + update_rw_regs(this, 1, false);
1421 +
1422 + config_cw_write_pre(this);
1423 + write_data_dma(this, FLASH_BUF_ACC, this->data_buffer, this->cw_size);
1424 + config_cw_write_post(this);
1425 +
1426 + r = submit_descs(this);
1427 +
1428 + free_descs(this);
1429 +
1430 + if (r) {
1431 + dev_err(this->dev, "failure to write updated oob\n");
1432 + return -EIO;
1433 + }
1434 +
1435 + chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1436 +
1437 + status = chip->waitfunc(mtd, chip);
1438 +
1439 + return status & NAND_STATUS_FAIL ? -EIO : 0;
1440 +}
1441 +
1442 +/*
1443 + * the three functions below implement chip->read_byte(), chip->read_buf()
1444 + * and chip->write_buf() respectively. these aren't used for
1445 + * reading/writing page data, they are used for smaller data like reading
1446 + * id, status etc
1447 + */
1448 +static uint8_t qcom_nandc_read_byte(struct mtd_info *mtd)
1449 +{
1450 + struct nand_chip *chip = mtd->priv;
1451 + struct qcom_nandc_data *this = chip->priv;
1452 + uint8_t *buf = this->data_buffer;
1453 + uint8_t ret = 0x0;
1454 +
1455 + if (this->last_command == NAND_CMD_STATUS) {
1456 + ret = this->status;
1457 +
1458 + this->status = NAND_STATUS_READY | NAND_STATUS_WP;
1459 +
1460 + return ret;
1461 + }
1462 +
1463 + if (this->buf_start < this->buf_count)
1464 + ret = buf[this->buf_start++];
1465 +
1466 + return ret;
1467 +}
1468 +
1469 +static void qcom_nandc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1470 +{
1471 + struct nand_chip *chip = mtd->priv;
1472 + struct qcom_nandc_data *this = chip->priv;
1473 + int real_len = min_t(size_t, len, this->buf_count - this->buf_start);
1474 +
1475 + memcpy(buf, this->data_buffer + this->buf_start, real_len);
1476 + this->buf_start += real_len;
1477 +}
1478 +
1479 +static void qcom_nandc_write_buf(struct mtd_info *mtd, const uint8_t *buf,
1480 + int len)
1481 +{
1482 + struct nand_chip *chip = mtd->priv;
1483 + struct qcom_nandc_data *this = chip->priv;
1484 + int real_len = min_t(size_t, len, this->buf_count - this->buf_start);
1485 +
1486 + memcpy(this->data_buffer + this->buf_start, buf, real_len);
1487 +
1488 + this->buf_start += real_len;
1489 +}
1490 +
1491 +/* we support only one external chip for now */
1492 +static void qcom_nandc_select_chip(struct mtd_info *mtd, int chipnr)
1493 +{
1494 + struct nand_chip *chip = mtd->priv;
1495 + struct qcom_nandc_data *this = chip->priv;
1496 +
1497 + if (chipnr <= 0)
1498 + return;
1499 +
1500 + dev_warn(this->dev, "invalid chip select\n");
1501 +}
1502 +
1503 +/*
1504 + * NAND controller page layout info
1505 + *
1506 + * |-----------------------| |---------------------------------|
1507 + * | xx.......xx| | *********xx.......xx|
1508 + * | DATA xx..ECC..xx| | DATA **SPARE**xx..ECC..xx|
1509 + * | (516) xx.......xx| | (516-n*4) **(n*4)**xx.......xx|
1510 + * | xx.......xx| | *********xx.......xx|
1511 + * |-----------------------| |---------------------------------|
1512 + * codeword 1,2..n-1 codeword n
1513 + * <---(528/532 Bytes)----> <-------(528/532 Bytes)---------->
1514 + *
1515 + * n = number of codewords in the page
1516 + * . = ECC bytes
1517 + * * = spare bytes
1518 + * x = unused/reserved bytes
1519 + *
1520 + * 2K page: n = 4, spare = 16 bytes
1521 + * 4K page: n = 8, spare = 32 bytes
1522 + * 8K page: n = 16, spare = 64 bytes
1523 + *
1524 + * the qcom nand controller operates at a sub page/codeword level. each
1525 + * codeword is 528 and 532 bytes for 4 bit and 8 bit ECC modes respectively.
1526 + * the number of ECC bytes vary based on the ECC strength and the bus width.
1527 + *
1528 + * the first n - 1 codewords contains 516 bytes of user data, the remaining
1529 + * 12/16 bytes consist of ECC and reserved data. The nth codeword contains
1530 + * both user data and spare(oobavail) bytes that sum up to 516 bytes.
1531 + *
1532 + * the layout described above is used by the controller when the ECC block is
1533 + * enabled. When we read a page with ECC enabled, the unused/reserved bytes are
1534 + * skipped and not copied to our internal buffer. therefore, the nand_ecclayout
1535 + * layouts defined below doesn't consider the positions occupied by the reserved
1536 + * bytes
1537 + *
1538 + * when the ECC block is disabled, one unused byte (or two for 16 bit bus width)
1539 + * in the last codeword is the position of bad block marker. the bad block
1540 + * marker cannot be accessed when ECC is enabled.
1541 + *
1542 + */
1543 +
1544 +/*
1545 + * Layouts for different page sizes and ecc modes. We skip the eccpos field
1546 + * since it isn't needed for this driver
1547 + */
1548 +
1549 +/* 2K page, 4 bit ECC */
1550 +static struct nand_ecclayout layout_oob_64 = {
1551 + .eccbytes = 40,
1552 + .oobfree = {
1553 + { 30, 16 },
1554 + },
1555 +};
1556 +
1557 +/* 4K page, 4 bit ECC, 8/16 bit bus width */
1558 +static struct nand_ecclayout layout_oob_128 = {
1559 + .eccbytes = 80,
1560 + .oobfree = {
1561 + { 70, 32 },
1562 + },
1563 +};
1564 +
1565 +/* 4K page, 8 bit ECC, 8 bit bus width */
1566 +static struct nand_ecclayout layout_oob_224_x8 = {
1567 + .eccbytes = 104,
1568 + .oobfree = {
1569 + { 91, 32 },
1570 + },
1571 +};
1572 +
1573 +/* 4K page, 8 bit ECC, 16 bit bus width */
1574 +static struct nand_ecclayout layout_oob_224_x16 = {
1575 + .eccbytes = 112,
1576 + .oobfree = {
1577 + { 98, 32 },
1578 + },
1579 +};
1580 +
1581 +/* 8K page, 4 bit ECC, 8/16 bit bus width */
1582 +static struct nand_ecclayout layout_oob_256 = {
1583 + .eccbytes = 160,
1584 + .oobfree = {
1585 + { 151, 64 },
1586 + },
1587 +};
1588 +
1589 +/*
1590 + * this is called before scan_ident, we do some minimal configurations so
1591 + * that reading ID and ONFI params work
1592 + */
1593 +static void qcom_nandc_pre_init(struct qcom_nandc_data *this)
1594 +{
1595 + /* kill onenand */
1596 + nandc_write(this, SFLASHC_BURST_CFG, 0);
1597 +
1598 + /* enable ADM DMA */
1599 + nandc_write(this, NAND_FLASH_CHIP_SELECT, DM_EN);
1600 +
1601 + /* save the original values of these registers */
1602 + this->cmd1 = nandc_read(this, NAND_DEV_CMD1);
1603 + this->vld = nandc_read(this, NAND_DEV_CMD_VLD);
1604 +
1605 + /* initial status value */
1606 + this->status = NAND_STATUS_READY | NAND_STATUS_WP;
1607 +}
1608 +
1609 +static int qcom_nandc_ecc_init(struct qcom_nandc_data *this)
1610 +{
1611 + struct mtd_info *mtd = &this->mtd;
1612 + struct nand_chip *chip = &this->chip;
1613 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1614 + int cwperpage;
1615 + bool wide_bus;
1616 +
1617 + /* the nand controller fetches codewords/chunks of 512 bytes */
1618 + cwperpage = mtd->writesize >> 9;
1619 +
1620 + ecc->strength = this->ecc_strength;
1621 +
1622 + wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
1623 +
1624 + if (ecc->strength >= 8) {
1625 + /* 8 bit ECC defaults to BCH ECC on all platforms */
1626 + ecc->bytes = wide_bus ? 14 : 13;
1627 + } else {
1628 + /*
1629 + * if the controller supports BCH for 4 bit ECC, the controller
1630 + * uses lesser bytes for ECC. If RS is used, the ECC bytes is
1631 + * always 10 bytes
1632 + */
1633 + if (this->ecc_modes & ECC_BCH_4BIT)
1634 + ecc->bytes = wide_bus ? 8 : 7;
1635 + else
1636 + ecc->bytes = 10;
1637 + }
1638 +
1639 + /* each step consists of 512 bytes of data */
1640 + ecc->size = NANDC_STEP_SIZE;
1641 +
1642 + ecc->read_page = qcom_nandc_read_page;
1643 + ecc->read_oob = qcom_nandc_read_oob;
1644 + ecc->write_page = qcom_nandc_write_page;
1645 + ecc->write_oob = qcom_nandc_write_oob;
1646 +
1647 + /*
1648 + * the bad block marker is readable only when we read the page with ECC
1649 + * disabled. all the ops above run with ECC enabled. We need raw read
1650 + * and write function for oob in order to access bad block marker.
1651 + */
1652 + ecc->read_oob_raw = qcom_nandc_read_oob_raw;
1653 + ecc->write_oob_raw = qcom_nandc_write_oob_raw;
1654 +
1655 + switch (mtd->oobsize) {
1656 + case 64:
1657 + ecc->layout = &layout_oob_64;
1658 + break;
1659 + case 128:
1660 + ecc->layout = &layout_oob_128;
1661 + break;
1662 + case 224:
1663 + if (wide_bus)
1664 + ecc->layout = &layout_oob_224_x16;
1665 + else
1666 + ecc->layout = &layout_oob_224_x8;
1667 + break;
1668 + case 256:
1669 + ecc->layout = &layout_oob_256;
1670 + break;
1671 + default:
1672 + dev_err(this->dev, "unsupported NAND device, oobsize %d\n",
1673 + mtd->oobsize);
1674 + return -ENODEV;
1675 + }
1676 +
1677 + ecc->mode = NAND_ECC_HW;
1678 +
1679 + /* enable ecc by default */
1680 + this->use_ecc = true;
1681 +
1682 + return 0;
1683 +}
1684 +
1685 +static void qcom_nandc_hw_post_init(struct qcom_nandc_data *this)
1686 +{
1687 + struct mtd_info *mtd = &this->mtd;
1688 + struct nand_chip *chip = &this->chip;
1689 + struct nand_ecc_ctrl *ecc = &chip->ecc;
1690 + int cwperpage = mtd->writesize / ecc->size;
1691 + int spare_bytes, bad_block_byte;
1692 + bool wide_bus;
1693 + int ecc_mode = 0;
1694 +
1695 + wide_bus = chip->options & NAND_BUSWIDTH_16 ? true : false;
1696 +
1697 + if (ecc->strength >= 8) {
1698 + this->cw_size = 532;
1699 +
1700 + spare_bytes = wide_bus ? 0 : 2;
1701 +
1702 + this->bch_enabled = true;
1703 + ecc_mode = 1;
1704 + } else {
1705 + this->cw_size = 528;
1706 +
1707 + if (this->ecc_modes & ECC_BCH_4BIT) {
1708 + spare_bytes = wide_bus ? 2 : 4;
1709 +
1710 + this->bch_enabled = true;
1711 + ecc_mode = 0;
1712 + } else {
1713 + spare_bytes = wide_bus ? 0 : 1;
1714 + }
1715 + }
1716 +
1717 + /*
1718 + * DATA_UD_BYTES varies based on whether the read/write command protects
1719 + * spare data with ECC too. We protect spare data by default, so we set
1720 + * it to main + spare data, which are 512 and 4 bytes respectively.
1721 + */
1722 + this->cw_data = 516;
1723 +
1724 + bad_block_byte = mtd->writesize - this->cw_size * (cwperpage - 1) + 1;
1725 +
1726 + this->cfg0 = (cwperpage - 1) << CW_PER_PAGE
1727 + | this->cw_data << UD_SIZE_BYTES
1728 + | 0 << DISABLE_STATUS_AFTER_WRITE
1729 + | 5 << NUM_ADDR_CYCLES
1730 + | ecc->bytes << ECC_PARITY_SIZE_BYTES_RS
1731 + | 0 << STATUS_BFR_READ
1732 + | 1 << SET_RD_MODE_AFTER_STATUS
1733 + | spare_bytes << SPARE_SIZE_BYTES;
1734 +
1735 + this->cfg1 = 7 << NAND_RECOVERY_CYCLES
1736 + | 0 << CS_ACTIVE_BSY
1737 + | bad_block_byte << BAD_BLOCK_BYTE_NUM
1738 + | 0 << BAD_BLOCK_IN_SPARE_AREA
1739 + | 2 << WR_RD_BSY_GAP
1740 + | wide_bus << WIDE_FLASH
1741 + | this->bch_enabled << ENABLE_BCH_ECC;
1742 +
1743 + this->cfg0_raw = (cwperpage - 1) << CW_PER_PAGE
1744 + | this->cw_size << UD_SIZE_BYTES
1745 + | 5 << NUM_ADDR_CYCLES
1746 + | 0 << SPARE_SIZE_BYTES;
1747 +
1748 + this->cfg1_raw = 7 << NAND_RECOVERY_CYCLES
1749 + | 0 << CS_ACTIVE_BSY
1750 + | 17 << BAD_BLOCK_BYTE_NUM
1751 + | 1 << BAD_BLOCK_IN_SPARE_AREA
1752 + | 2 << WR_RD_BSY_GAP
1753 + | wide_bus << WIDE_FLASH
1754 + | 1 << DEV0_CFG1_ECC_DISABLE;
1755 +
1756 + this->ecc_bch_cfg = this->bch_enabled << ECC_CFG_ECC_DISABLE
1757 + | 0 << ECC_SW_RESET
1758 + | this->cw_data << ECC_NUM_DATA_BYTES
1759 + | 1 << ECC_FORCE_CLK_OPEN
1760 + | ecc_mode << ECC_MODE
1761 + | ecc->bytes << ECC_PARITY_SIZE_BYTES_BCH;
1762 +
1763 + this->ecc_buf_cfg = 0x203 << NUM_STEPS;
1764 +
1765 + this->clrflashstatus = FS_READY_BSY_N;
1766 + this->clrreadstatus = 0xc0;
1767 +
1768 + dev_dbg(this->dev,
1769 + "cfg0 %x cfg1 %x ecc_buf_cfg %x ecc_bch cfg %x cw_size %d cw_data %d strength %d parity_bytes %d steps %d\n",
1770 + this->cfg0, this->cfg1, this->ecc_buf_cfg,
1771 + this->ecc_bch_cfg, this->cw_size, this->cw_data,
1772 + ecc->strength, ecc->bytes, cwperpage);
1773 +}
1774 +
1775 +static int qcom_nandc_alloc(struct qcom_nandc_data *this)
1776 +{
1777 + int r;
1778 +
1779 + r = dma_set_coherent_mask(this->dev, DMA_BIT_MASK(32));
1780 + if (r) {
1781 + dev_err(this->dev, "failed to set DMA mask\n");
1782 + return r;
1783 + }
1784 +
1785 + /*
1786 + * we use the internal buffer for reading ONFI params, reading small
1787 + * data like ID and status, and preforming read-copy-write operations
1788 + * when writing to a codeword partially. 532 is the maximum possible
1789 + * size of a codeword for our nand controller
1790 + */
1791 + this->buf_size = 532;
1792 +
1793 + this->data_buffer = devm_kzalloc(this->dev, this->buf_size, GFP_KERNEL);
1794 + if (!this->data_buffer)
1795 + return -ENOMEM;
1796 +
1797 + this->regs = devm_kzalloc(this->dev, sizeof(*this->regs), GFP_KERNEL);
1798 + if (!this->regs)
1799 + return -ENOMEM;
1800 +
1801 + this->reg_read_buf = devm_kzalloc(this->dev,
1802 + MAX_REG_RD * sizeof(*this->reg_read_buf),
1803 + GFP_KERNEL);
1804 + if (!this->reg_read_buf)
1805 + return -ENOMEM;
1806 +
1807 + INIT_LIST_HEAD(&this->list);
1808 +
1809 + this->chan = dma_request_slave_channel(this->dev, "rxtx");
1810 + if (!this->chan) {
1811 + dev_err(this->dev, "failed to request slave channel\n");
1812 + return -ENODEV;
1813 + }
1814 +
1815 + return 0;
1816 +}
1817 +
1818 +static void qcom_nandc_unalloc(struct qcom_nandc_data *this)
1819 +{
1820 + dma_release_channel(this->chan);
1821 +}
1822 +
1823 +static int qcom_nandc_init(struct qcom_nandc_data *this)
1824 +{
1825 + struct mtd_info *mtd = &this->mtd;
1826 + struct nand_chip *chip = &this->chip;
1827 + struct device_node *np = this->dev->of_node;
1828 + struct mtd_part_parser_data ppdata = { .of_node = np };
1829 + int r;
1830 +
1831 + mtd->priv = chip;
1832 + mtd->name = "qcom-nandc";
1833 + mtd->owner = THIS_MODULE;
1834 +
1835 + chip->priv = this;
1836 +
1837 + chip->cmdfunc = qcom_nandc_command;
1838 + chip->select_chip = qcom_nandc_select_chip;
1839 + chip->read_byte = qcom_nandc_read_byte;
1840 + chip->read_buf = qcom_nandc_read_buf;
1841 + chip->write_buf = qcom_nandc_write_buf;
1842 +
1843 + chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USE_BOUNCE_BUFFER;
1844 + if (this->bus_width == 16)
1845 + chip->options |= NAND_BUSWIDTH_16;
1846 +
1847 + chip->bbt_options = NAND_BBT_ACCESS_BBM_RAW;
1848 + if (of_get_nand_on_flash_bbt(np))
1849 + chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1850 +
1851 + qcom_nandc_pre_init(this);
1852 +
1853 + r = nand_scan_ident(mtd, 1, NULL);
1854 + if (r)
1855 + return r;
1856 +
1857 + r = qcom_nandc_ecc_init(this);
1858 + if (r)
1859 + return r;
1860 +
1861 + qcom_nandc_hw_post_init(this);
1862 +
1863 + r = nand_scan_tail(mtd);
1864 + if (r)
1865 + return r;
1866 +
1867 + return mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
1868 +}
1869 +
1870 +static int qcom_nandc_parse_dt(struct platform_device *pdev)
1871 +{
1872 + struct qcom_nandc_data *this = platform_get_drvdata(pdev);
1873 + struct device_node *np = this->dev->of_node;
1874 + int r;
1875 +
1876 + this->ecc_strength = of_get_nand_ecc_strength(np);
1877 + if (this->ecc_strength < 0) {
1878 + dev_warn(this->dev,
1879 + "incorrect ecc strength, setting to 4 bits/step\n");
1880 + this->ecc_strength = 4;
1881 + }
1882 +
1883 + this->bus_width = of_get_nand_bus_width(np);
1884 + if (this->bus_width < 0) {
1885 + dev_warn(this->dev, "incorrect bus width, setting to 8\n");
1886 + this->bus_width = 8;
1887 + }
1888 +
1889 + r = of_property_read_u32(np, "qcom,cmd-crci", &this->cmd_crci);
1890 + if (r) {
1891 + dev_err(this->dev, "command CRCI unspecified\n");
1892 + return r;
1893 + }
1894 +
1895 + r = of_property_read_u32(np, "qcom,data-crci", &this->data_crci);
1896 + if (r) {
1897 + dev_err(this->dev, "data CRCI unspecified\n");
1898 + return r;
1899 + }
1900 +
1901 + return 0;
1902 +}
1903 +
1904 +static int qcom_nandc_probe(struct platform_device *pdev)
1905 +{
1906 + struct qcom_nandc_data *this;
1907 + const struct of_device_id *match;
1908 + int r;
1909 +
1910 + this = devm_kzalloc(&pdev->dev, sizeof(*this), GFP_KERNEL);
1911 + if (!this)
1912 + return -ENOMEM;
1913 +
1914 + platform_set_drvdata(pdev, this);
1915 +
1916 + this->pdev = pdev;
1917 + this->dev = &pdev->dev;
1918 +
1919 + match = of_match_device(pdev->dev.driver->of_match_table, &pdev->dev);
1920 + if (!match) {
1921 + dev_err(&pdev->dev, "failed to match device\n");
1922 + return -ENODEV;
1923 + }
1924 +
1925 + if (!match->data) {
1926 + dev_err(&pdev->dev, "failed to get device data\n");
1927 + return -ENODEV;
1928 + }
1929 +
1930 + this->ecc_modes = (u32) match->data;
1931 +
1932 + this->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1933 + this->base = devm_ioremap_resource(&pdev->dev, this->res);
1934 + if (IS_ERR(this->base))
1935 + return PTR_ERR(this->base);
1936 +
1937 + this->core_clk = devm_clk_get(&pdev->dev, "core");
1938 + if (IS_ERR(this->core_clk))
1939 + return PTR_ERR(this->core_clk);
1940 +
1941 + this->aon_clk = devm_clk_get(&pdev->dev, "aon");
1942 + if (IS_ERR(this->aon_clk))
1943 + return PTR_ERR(this->aon_clk);
1944 +
1945 + r = qcom_nandc_parse_dt(pdev);
1946 + if (r)
1947 + return r;
1948 +
1949 + r = qcom_nandc_alloc(this);
1950 + if (r)
1951 + return r;
1952 +
1953 + r = clk_prepare_enable(this->core_clk);
1954 + if (r)
1955 + goto err_core_clk;
1956 +
1957 + r = clk_prepare_enable(this->aon_clk);
1958 + if (r)
1959 + goto err_aon_clk;
1960 +
1961 + r = qcom_nandc_init(this);
1962 + if (r)
1963 + goto err_init;
1964 +
1965 + return 0;
1966 +
1967 +err_init:
1968 + clk_disable_unprepare(this->aon_clk);
1969 +err_aon_clk:
1970 + clk_disable_unprepare(this->core_clk);
1971 +err_core_clk:
1972 + qcom_nandc_unalloc(this);
1973 +
1974 + return r;
1975 +}
1976 +
1977 +static int qcom_nandc_remove(struct platform_device *pdev)
1978 +{
1979 + struct qcom_nandc_data *this = platform_get_drvdata(pdev);
1980 +
1981 + qcom_nandc_unalloc(this);
1982 +
1983 + clk_disable_unprepare(this->aon_clk);
1984 + clk_disable_unprepare(this->core_clk);
1985 +
1986 + return 0;
1987 +}
1988 +
1989 +#define EBI2_NANDC_ECC_MODES (ECC_RS_4BIT | ECC_BCH_8BIT)
1990 +
1991 +/*
1992 + * data will hold a struct pointer containing more differences once we support
1993 + * more IPs
1994 + */
1995 +static const struct of_device_id qcom_nandc_of_match[] = {
1996 + { .compatible = "qcom,ebi2-nandc",
1997 + .data = (void *) EBI2_NANDC_ECC_MODES,
1998 + },
1999 + {}
2000 +};
2001 +MODULE_DEVICE_TABLE(of, qcom_nandc_of_match);
2002 +
2003 +static struct platform_driver qcom_nandc_driver = {
2004 + .driver = {
2005 + .name = "qcom-nandc",
2006 + .of_match_table = qcom_nandc_of_match,
2007 + },
2008 + .probe = qcom_nandc_probe,
2009 + .remove = qcom_nandc_remove,
2010 +};
2011 +module_platform_driver(qcom_nandc_driver);
2012 +
2013 +MODULE_AUTHOR("Archit Taneja <architt@codeaurora.org>");
2014 +MODULE_DESCRIPTION("Qualcomm NAND Controller driver");
2015 +MODULE_LICENSE("GPL v2");
2016 --- a/drivers/mtd/nand/Makefile
2017 +++ b/drivers/mtd/nand/Makefile
2018 @@ -55,5 +55,6 @@ obj-$(CONFIG_MTD_NAND_BCM47XXNFLASH) +=
2019 obj-$(CONFIG_MTD_NAND_SUNXI) += sunxi_nand.o
2020 obj-$(CONFIG_MTD_NAND_HISI504) += hisi504_nand.o
2021 obj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmnand/
2022 +obj-$(CONFIG_MTD_NAND_QCOM) += qcom_nandc.o
2023
2024 nand-objs := nand_base.o nand_bbt.o nand_timings.o