]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/mtd/nand/raw/gpmi-nand/gpmi-lib.c
Merge tag 'omap-for-v5.0/fixes-rc7-signed' of git://git.kernel.org/pub/scm/linux...
[mirror_ubuntu-eoan-kernel.git] / drivers / mtd / nand / raw / gpmi-nand / gpmi-lib.c
CommitLineData
b04a3fe3 1// SPDX-License-Identifier: GPL-2.0+
45dfc1a0
HS
2/*
3 * Freescale GPMI NAND Flash Driver
4 *
5 * Copyright (C) 2008-2011 Freescale Semiconductor, Inc.
6 * Copyright (C) 2008 Embedded Alley Solutions, Inc.
45dfc1a0 7 */
45dfc1a0
HS
8#include <linux/delay.h>
9#include <linux/clk.h>
df877fb3 10#include <linux/slab.h>
45dfc1a0
HS
11
12#include "gpmi-nand.h"
13#include "gpmi-regs.h"
14#include "bch-regs.h"
15
b1206122
MR
16/* Converts time to clock cycles */
17#define TO_CYCLES(duration, period) DIV_ROUND_UP_ULL(duration, period)
45dfc1a0 18
4aa6ae3e
HS
19#define MXS_SET_ADDR 0x4
20#define MXS_CLR_ADDR 0x8
45dfc1a0
HS
21/*
22 * Clear the bit and poll it cleared. This is usually called with
23 * a reset address and mask being either SFTRST(bit 31) or CLKGATE
24 * (bit 30).
25 */
26static int clear_poll_bit(void __iomem *addr, u32 mask)
27{
28 int timeout = 0x400;
29
30 /* clear the bit */
4aa6ae3e 31 writel(mask, addr + MXS_CLR_ADDR);
45dfc1a0
HS
32
33 /*
34 * SFTRST needs 3 GPMI clocks to settle, the reference manual
35 * recommends to wait 1us.
36 */
37 udelay(1);
38
39 /* poll the bit becoming clear */
40 while ((readl(addr) & mask) && --timeout)
41 /* nothing */;
42
43 return !timeout;
44}
45
46#define MODULE_CLKGATE (1 << 30)
47#define MODULE_SFTRST (1 << 31)
48/*
49 * The current mxs_reset_block() will do two things:
50 * [1] enable the module.
51 * [2] reset the module.
52 *
9398d1ce
HS
53 * In most of the cases, it's ok.
54 * But in MX23, there is a hardware bug in the BCH block (see erratum #2847).
45dfc1a0
HS
55 * If you try to soft reset the BCH block, it becomes unusable until
56 * the next hard reset. This case occurs in the NAND boot mode. When the board
57 * boots by NAND, the ROM of the chip will initialize the BCH blocks itself.
58 * So If the driver tries to reset the BCH again, the BCH will not work anymore.
9398d1ce
HS
59 * You will see a DMA timeout in this case. The bug has been fixed
60 * in the following chips, such as MX28.
45dfc1a0
HS
61 *
62 * To avoid this bug, just add a new parameter `just_enable` for
63 * the mxs_reset_block(), and rewrite it here.
64 */
9398d1ce 65static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
45dfc1a0
HS
66{
67 int ret;
68 int timeout = 0x400;
69
70 /* clear and poll SFTRST */
71 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
72 if (unlikely(ret))
73 goto error;
74
75 /* clear CLKGATE */
4aa6ae3e 76 writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
45dfc1a0
HS
77
78 if (!just_enable) {
79 /* set SFTRST to reset the block */
4aa6ae3e 80 writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
45dfc1a0
HS
81 udelay(1);
82
83 /* poll CLKGATE becoming set */
84 while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
85 /* nothing */;
86 if (unlikely(!timeout))
87 goto error;
88 }
89
90 /* clear and poll SFTRST */
91 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
92 if (unlikely(ret))
93 goto error;
94
95 /* clear and poll CLKGATE */
96 ret = clear_poll_bit(reset_addr, MODULE_CLKGATE);
97 if (unlikely(ret))
98 goto error;
99
100 return 0;
101
102error:
103 pr_err("%s(%p): module reset timeout\n", __func__, reset_addr);
104 return -ETIMEDOUT;
105}
106
ff506172
HS
107static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v)
108{
109 struct clk *clk;
110 int ret;
111 int i;
112
113 for (i = 0; i < GPMI_CLK_MAX; i++) {
114 clk = this->resources.clock[i];
115 if (!clk)
116 break;
117
118 if (v) {
119 ret = clk_prepare_enable(clk);
120 if (ret)
121 goto err_clk;
122 } else {
123 clk_disable_unprepare(clk);
124 }
125 }
126 return 0;
127
128err_clk:
129 for (; i > 0; i--)
130 clk_disable_unprepare(this->resources.clock[i - 1]);
131 return ret;
132}
133
76e1a008
MR
134int gpmi_enable_clk(struct gpmi_nand_data *this)
135{
136 return __gpmi_enable_clk(this, true);
137}
138
139int gpmi_disable_clk(struct gpmi_nand_data *this)
140{
141 return __gpmi_enable_clk(this, false);
142}
ff506172 143
45dfc1a0
HS
144int gpmi_init(struct gpmi_nand_data *this)
145{
146 struct resources *r = &this->resources;
147 int ret;
148
ff506172 149 ret = gpmi_enable_clk(this);
45dfc1a0 150 if (ret)
ce93bedb 151 return ret;
45dfc1a0
HS
152 ret = gpmi_reset_block(r->gpmi_regs, false);
153 if (ret)
154 goto err_out;
155
6f2a6a52
WS
156 /*
157 * Reset BCH here, too. We got failures otherwise :(
d5d27fd9 158 * See later BCH reset for explanation of MX23 and MX28 handling
6f2a6a52 159 */
d5d27fd9
MK
160 ret = gpmi_reset_block(r->bch_regs,
161 GPMI_IS_MX23(this) || GPMI_IS_MX28(this));
6f2a6a52
WS
162 if (ret)
163 goto err_out;
164
45dfc1a0
HS
165 /* Choose NAND mode. */
166 writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
167
168 /* Set the IRQ polarity. */
169 writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
170 r->gpmi_regs + HW_GPMI_CTRL1_SET);
171
172 /* Disable Write-Protection. */
173 writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
174
175 /* Select BCH ECC. */
176 writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
177
d159d8b7
HS
178 /*
179 * Decouple the chip select from dma channel. We use dma0 for all
180 * the chips.
181 */
182 writel(BM_GPMI_CTRL1_DECOUPLE_CS, r->gpmi_regs + HW_GPMI_CTRL1_SET);
183
ff506172 184 gpmi_disable_clk(this);
45dfc1a0
HS
185 return 0;
186err_out:
ce93bedb 187 gpmi_disable_clk(this);
45dfc1a0
HS
188 return ret;
189}
190
191/* This function is very useful. It is called only when the bug occur. */
192void gpmi_dump_info(struct gpmi_nand_data *this)
193{
194 struct resources *r = &this->resources;
195 struct bch_geometry *geo = &this->bch_geometry;
196 u32 reg;
197 int i;
198
da40c16a 199 dev_err(this->dev, "Show GPMI registers :\n");
45dfc1a0
HS
200 for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) {
201 reg = readl(r->gpmi_regs + i * 0x10);
da40c16a 202 dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
45dfc1a0
HS
203 }
204
205 /* start to print out the BCH info */
da40c16a 206 dev_err(this->dev, "Show BCH registers :\n");
f7226893
HS
207 for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) {
208 reg = readl(r->bch_regs + i * 0x10);
da40c16a 209 dev_err(this->dev, "offset 0x%.3x : 0x%.8x\n", i * 0x10, reg);
f7226893 210 }
da40c16a
HS
211 dev_err(this->dev, "BCH Geometry :\n"
212 "GF length : %u\n"
213 "ECC Strength : %u\n"
214 "Page Size in Bytes : %u\n"
215 "Metadata Size in Bytes : %u\n"
216 "ECC Chunk Size in Bytes: %u\n"
217 "ECC Chunk Count : %u\n"
218 "Payload Size in Bytes : %u\n"
219 "Auxiliary Size in Bytes: %u\n"
220 "Auxiliary Status Offset: %u\n"
221 "Block Mark Byte Offset : %u\n"
222 "Block Mark Bit Offset : %u\n",
223 geo->gf_len,
224 geo->ecc_strength,
225 geo->page_size,
226 geo->metadata_size,
227 geo->ecc_chunk_size,
228 geo->ecc_chunk_count,
229 geo->payload_size,
230 geo->auxiliary_size,
231 geo->auxiliary_status_offset,
232 geo->block_mark_byte_offset,
233 geo->block_mark_bit_offset);
45dfc1a0
HS
234}
235
236/* Configures the geometry for BCH. */
237int bch_set_geometry(struct gpmi_nand_data *this)
238{
239 struct resources *r = &this->resources;
240 struct bch_geometry *bch_geo = &this->bch_geometry;
241 unsigned int block_count;
242 unsigned int block_size;
243 unsigned int metadata_size;
244 unsigned int ecc_strength;
245 unsigned int page_size;
9ff16f08 246 unsigned int gf_len;
45dfc1a0
HS
247 int ret;
248
e637f5fe
SH
249 ret = common_nfc_set_geometry(this);
250 if (ret)
251 return ret;
45dfc1a0
HS
252
253 block_count = bch_geo->ecc_chunk_count - 1;
254 block_size = bch_geo->ecc_chunk_size;
255 metadata_size = bch_geo->metadata_size;
256 ecc_strength = bch_geo->ecc_strength >> 1;
257 page_size = bch_geo->page_size;
9ff16f08 258 gf_len = bch_geo->gf_len;
45dfc1a0 259
ff506172 260 ret = gpmi_enable_clk(this);
45dfc1a0 261 if (ret)
ce93bedb 262 return ret;
45dfc1a0 263
9398d1ce
HS
264 /*
265 * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
266 * chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
d5d27fd9 267 * and MX28.
9398d1ce 268 */
d5d27fd9
MK
269 ret = gpmi_reset_block(r->bch_regs,
270 GPMI_IS_MX23(this) || GPMI_IS_MX28(this));
45dfc1a0
HS
271 if (ret)
272 goto err_out;
273
274 /* Configure layout 0. */
275 writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count)
276 | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size)
9013bb40 277 | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this)
9ff16f08 278 | BF_BCH_FLASH0LAYOUT0_GF(gf_len, this)
9013bb40 279 | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this),
45dfc1a0
HS
280 r->bch_regs + HW_BCH_FLASH0LAYOUT0);
281
282 writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size)
9013bb40 283 | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this)
9ff16f08 284 | BF_BCH_FLASH0LAYOUT1_GF(gf_len, this)
9013bb40 285 | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this),
45dfc1a0
HS
286 r->bch_regs + HW_BCH_FLASH0LAYOUT1);
287
288 /* Set *all* chip selects to use layout 0. */
289 writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
290
291 /* Enable interrupts. */
292 writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
293 r->bch_regs + HW_BCH_CTRL_SET);
294
ff506172 295 gpmi_disable_clk(this);
45dfc1a0
HS
296 return 0;
297err_out:
ce93bedb 298 gpmi_disable_clk(this);
45dfc1a0
HS
299 return ret;
300}
301
995fbbf5
HS
302/*
303 * <1> Firstly, we should know what's the GPMI-clock means.
304 * The GPMI-clock is the internal clock in the gpmi nand controller.
305 * If you set 100MHz to gpmi nand controller, the GPMI-clock's period
306 * is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
307 *
308 * <2> Secondly, we should know what's the frequency on the nand chip pins.
309 * The frequency on the nand chip pins is derived from the GPMI-clock.
310 * We can get it from the following equation:
311 *
312 * F = G / (DS + DH)
313 *
314 * F : the frequency on the nand chip pins.
315 * G : the GPMI clock, such as 100MHz.
316 * DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
317 * DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
318 *
319 * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
320 * the nand EDO(extended Data Out) timing could be applied.
321 * The GPMI implements a feedback read strobe to sample the read data.
322 * The feedback read strobe can be delayed to support the nand EDO timing
323 * where the read strobe may deasserts before the read data is valid, and
324 * read data is valid for some time after read strobe.
325 *
326 * The following figure illustrates some aspects of a NAND Flash read:
327 *
328 * |<---tREA---->|
329 * | |
330 * | | |
331 * |<--tRP-->| |
332 * | | |
333 * __ ___|__________________________________
334 * RDN \________/ |
335 * |
336 * /---------\
337 * Read Data --------------< >---------
338 * \---------/
339 * | |
340 * |<-D->|
341 * FeedbackRDN ________ ____________
342 * \___________/
343 *
344 * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
345 *
346 *
347 * <4> Now, we begin to describe how to compute the right RDN_DELAY.
348 *
349 * 4.1) From the aspect of the nand chip pins:
350 * Delay = (tREA + C - tRP) {1}
351 *
b1206122
MR
352 * tREA : the maximum read access time.
353 * C : a constant to adjust the delay. default is 4000ps.
354 * tRP : the read pulse width, which is exactly:
355 * tRP = (GPMI-clock-period) * DATA_SETUP
995fbbf5
HS
356 *
357 * 4.2) From the aspect of the GPMI nand controller:
358 * Delay = RDN_DELAY * 0.125 * RP {2}
359 *
360 * RP : the DLL reference period.
361 * if (GPMI-clock-period > DLL_THRETHOLD)
362 * RP = GPMI-clock-period / 2;
363 * else
364 * RP = GPMI-clock-period;
365 *
366 * Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
367 * is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
b1206122 368 * is 16000ps, but in mx6q, we use 12000ps.
995fbbf5
HS
369 *
370 * 4.3) since {1} equals {2}, we get:
371 *
b1206122
MR
372 * (tREA + 4000 - tRP) * 8
373 * RDN_DELAY = ----------------------- {3}
995fbbf5 374 * RP
995fbbf5 375 */
b1206122
MR
376static void gpmi_nfc_compute_timings(struct gpmi_nand_data *this,
377 const struct nand_sdr_timings *sdr)
995fbbf5 378{
76e1a008 379 struct gpmi_nfc_hardware_timing *hw = &this->hw;
b1206122
MR
380 unsigned int dll_threshold_ps = this->devdata->max_chain_delay;
381 unsigned int period_ps, reference_period_ps;
382 unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles;
383 unsigned int tRP_ps;
384 bool use_half_period;
385 int sample_delay_ps, sample_delay_factor;
386 u16 busy_timeout_cycles;
387 u8 wrn_dly_sel;
388
389 if (sdr->tRC_min >= 30000) {
390 /* ONFI non-EDO modes [0-3] */
391 hw->clk_rate = 22000000;
392 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
393 } else if (sdr->tRC_min >= 25000) {
394 /* ONFI EDO mode 4 */
395 hw->clk_rate = 80000000;
396 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
397 } else {
398 /* ONFI EDO mode 5 */
399 hw->clk_rate = 100000000;
400 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
401 }
995fbbf5 402
b1206122
MR
403 /* SDR core timings are given in picoseconds */
404 period_ps = div_u64((u64)NSEC_PER_SEC * 1000, hw->clk_rate);
995fbbf5 405
b1206122
MR
406 addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps);
407 data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps);
408 data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps);
409 busy_timeout_cycles = TO_CYCLES(sdr->tWB_max + sdr->tR_max, period_ps);
995fbbf5 410
b1206122
MR
411 hw->timing0 = BF_GPMI_TIMING0_ADDRESS_SETUP(addr_setup_cycles) |
412 BF_GPMI_TIMING0_DATA_HOLD(data_hold_cycles) |
413 BF_GPMI_TIMING0_DATA_SETUP(data_setup_cycles);
414 hw->timing1 = BF_GPMI_TIMING1_BUSY_TIMEOUT(busy_timeout_cycles * 4096);
995fbbf5
HS
415
416 /*
b1206122
MR
417 * Derive NFC ideal delay from {3}:
418 *
419 * (tREA + 4000 - tRP) * 8
420 * RDN_DELAY = -----------------------
421 * RP
995fbbf5 422 */
b1206122
MR
423 if (period_ps > dll_threshold_ps) {
424 use_half_period = true;
425 reference_period_ps = period_ps / 2;
995fbbf5 426 } else {
b1206122
MR
427 use_half_period = false;
428 reference_period_ps = period_ps;
995fbbf5
HS
429 }
430
b1206122
MR
431 tRP_ps = data_setup_cycles * period_ps;
432 sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8;
433 if (sample_delay_ps > 0)
434 sample_delay_factor = sample_delay_ps / reference_period_ps;
435 else
436 sample_delay_factor = 0;
995fbbf5 437
b1206122
MR
438 hw->ctrl1n = BF_GPMI_CTRL1_WRN_DLY_SEL(wrn_dly_sel);
439 if (sample_delay_factor)
440 hw->ctrl1n |= BF_GPMI_CTRL1_RDN_DELAY(sample_delay_factor) |
441 BM_GPMI_CTRL1_DLL_ENABLE |
442 (use_half_period ? BM_GPMI_CTRL1_HALF_PERIOD : 0);
995fbbf5
HS
443}
444
76e1a008 445void gpmi_nfc_apply_timings(struct gpmi_nand_data *this)
45dfc1a0 446{
76e1a008 447 struct gpmi_nfc_hardware_timing *hw = &this->hw;
45dfc1a0 448 struct resources *r = &this->resources;
513d57e1 449 void __iomem *gpmi_regs = r->gpmi_regs;
b1206122 450 unsigned int dll_wait_time_us;
45dfc1a0 451
76e1a008 452 clk_set_rate(r->clock[0], hw->clk_rate);
45dfc1a0 453
b1206122
MR
454 writel(hw->timing0, gpmi_regs + HW_GPMI_TIMING0);
455 writel(hw->timing1, gpmi_regs + HW_GPMI_TIMING1);
45dfc1a0
HS
456
457 /*
b1206122
MR
458 * Clear several CTRL1 fields, DLL must be disabled when setting
459 * RDN_DELAY or HALF_PERIOD.
45dfc1a0 460 */
b1206122
MR
461 writel(BM_GPMI_CTRL1_CLEAR_MASK, gpmi_regs + HW_GPMI_CTRL1_CLR);
462 writel(hw->ctrl1n, gpmi_regs + HW_GPMI_CTRL1_SET);
45dfc1a0 463
b1206122
MR
464 /* Wait 64 clock cycles before using the GPMI after enabling the DLL */
465 dll_wait_time_us = USEC_PER_SEC / hw->clk_rate * 64;
466 if (!dll_wait_time_us)
467 dll_wait_time_us = 1;
45dfc1a0
HS
468
469 /* Wait for the DLL to settle. */
b1206122 470 udelay(dll_wait_time_us);
45dfc1a0
HS
471}
472
858838b8 473int gpmi_setup_data_interface(struct nand_chip *chip, int chipnr,
76e1a008 474 const struct nand_data_interface *conf)
45dfc1a0 475{
76e1a008
MR
476 struct gpmi_nand_data *this = nand_get_controller_data(chip);
477 const struct nand_sdr_timings *sdr;
76e1a008
MR
478
479 /* Retrieve required NAND timings */
480 sdr = nand_get_sdr_timings(conf);
481 if (IS_ERR(sdr))
482 return PTR_ERR(sdr);
483
76e1a008 484 /* Only MX6 GPMI controller can reach EDO timings */
b1206122 485 if (sdr->tRC_min <= 25000 && !GPMI_IS_MX6(this))
76e1a008
MR
486 return -ENOTSUPP;
487
b1206122 488 /* Stop here if this call was just a check */
76e1a008
MR
489 if (chipnr < 0)
490 return 0;
491
b1206122
MR
492 /* Do the actual derivation of the controller timings */
493 gpmi_nfc_compute_timings(this, sdr);
76e1a008
MR
494
495 this->hw.must_apply_timings = true;
496
497 return 0;
45dfc1a0
HS
498}
499
500/* Clears a BCH interrupt. */
501void gpmi_clear_bch(struct gpmi_nand_data *this)
502{
503 struct resources *r = &this->resources;
504 writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
505}
506
507/* Returns the Ready/Busy status of the given chip. */
508int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
509{
510 struct resources *r = &this->resources;
511 uint32_t mask = 0;
512 uint32_t reg = 0;
513
514 if (GPMI_IS_MX23(this)) {
515 mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
516 reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
91f5498e 517 } else if (GPMI_IS_MX28(this) || GPMI_IS_MX6(this)) {
7caa4fd2
HS
518 /*
519 * In the imx6, all the ready/busy pins are bound
520 * together. So we only need to check chip 0.
521 */
91f5498e 522 if (GPMI_IS_MX6(this))
7caa4fd2
HS
523 chip = 0;
524
9013bb40 525 /* MX28 shares the same R/B register as MX6Q. */
45dfc1a0
HS
526 mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
527 reg = readl(r->gpmi_regs + HW_GPMI_STAT);
528 } else
f42cf8d6 529 dev_err(this->dev, "unknown arch.\n");
45dfc1a0
HS
530 return reg & mask;
531}
532
45dfc1a0
HS
533int gpmi_send_command(struct gpmi_nand_data *this)
534{
535 struct dma_chan *channel = get_dma_chan(this);
536 struct dma_async_tx_descriptor *desc;
537 struct scatterlist *sgl;
538 int chip = this->current_chip;
c3ee3f3d 539 int ret;
45dfc1a0
HS
540 u32 pio[3];
541
542 /* [1] send out the PIO words */
543 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
544 | BM_GPMI_CTRL0_WORD_LENGTH
545 | BF_GPMI_CTRL0_CS(chip, this)
546 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
547 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
548 | BM_GPMI_CTRL0_ADDRESS_INCREMENT
549 | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
550 pio[1] = pio[2] = 0;
16052827 551 desc = dmaengine_prep_slave_sg(channel,
45dfc1a0 552 (struct scatterlist *)pio,
0ef7e206 553 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
43a34b8b
HS
554 if (!desc)
555 return -EINVAL;
45dfc1a0
HS
556
557 /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
558 sgl = &this->cmd_sgl;
559
560 sg_init_one(sgl, this->cmd_buffer, this->command_length);
561 dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
623ff773 562 desc = dmaengine_prep_slave_sg(channel,
921de864
HS
563 sgl, 1, DMA_MEM_TO_DEV,
564 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
43a34b8b
HS
565 if (!desc)
566 return -EINVAL;
45dfc1a0
HS
567
568 /* [3] submit the DMA */
c3ee3f3d
SH
569 ret = start_dma_without_bch_irq(this, desc);
570
571 dma_unmap_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
572
573 return ret;
45dfc1a0
HS
574}
575
ba3900e6 576int gpmi_send_data(struct gpmi_nand_data *this, const void *buf, int len)
45dfc1a0
HS
577{
578 struct dma_async_tx_descriptor *desc;
579 struct dma_chan *channel = get_dma_chan(this);
580 int chip = this->current_chip;
c3ee3f3d 581 int ret;
45dfc1a0
HS
582 uint32_t command_mode;
583 uint32_t address;
584 u32 pio[2];
585
586 /* [1] PIO */
587 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
588 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
589
590 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
591 | BM_GPMI_CTRL0_WORD_LENGTH
592 | BF_GPMI_CTRL0_CS(chip, this)
593 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
594 | BF_GPMI_CTRL0_ADDRESS(address)
ba3900e6 595 | BF_GPMI_CTRL0_XFER_COUNT(len);
45dfc1a0 596 pio[1] = 0;
16052827 597 desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
0ef7e206 598 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
43a34b8b
HS
599 if (!desc)
600 return -EINVAL;
45dfc1a0
HS
601
602 /* [2] send DMA request */
ba3900e6 603 prepare_data_dma(this, buf, len, DMA_TO_DEVICE);
16052827 604 desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
921de864
HS
605 1, DMA_MEM_TO_DEV,
606 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
43a34b8b
HS
607 if (!desc)
608 return -EINVAL;
609
45dfc1a0 610 /* [3] submit the DMA */
c3ee3f3d
SH
611 ret = start_dma_without_bch_irq(this, desc);
612
613 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE);
614
615 return ret;
45dfc1a0
HS
616}
617
ba3900e6 618int gpmi_read_data(struct gpmi_nand_data *this, void *buf, int len)
45dfc1a0
HS
619{
620 struct dma_async_tx_descriptor *desc;
621 struct dma_chan *channel = get_dma_chan(this);
622 int chip = this->current_chip;
c3ee3f3d 623 int ret;
45dfc1a0 624 u32 pio[2];
111bfed4 625 bool direct;
45dfc1a0
HS
626
627 /* [1] : send PIO */
628 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
629 | BM_GPMI_CTRL0_WORD_LENGTH
630 | BF_GPMI_CTRL0_CS(chip, this)
631 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
632 | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
ba3900e6 633 | BF_GPMI_CTRL0_XFER_COUNT(len);
45dfc1a0 634 pio[1] = 0;
16052827 635 desc = dmaengine_prep_slave_sg(channel,
45dfc1a0 636 (struct scatterlist *)pio,
0ef7e206 637 ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
43a34b8b
HS
638 if (!desc)
639 return -EINVAL;
45dfc1a0
HS
640
641 /* [2] : send DMA request */
111bfed4 642 direct = prepare_data_dma(this, buf, len, DMA_FROM_DEVICE);
16052827 643 desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
921de864
HS
644 1, DMA_DEV_TO_MEM,
645 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
43a34b8b
HS
646 if (!desc)
647 return -EINVAL;
45dfc1a0
HS
648
649 /* [3] : submit the DMA */
c3ee3f3d
SH
650
651 ret = start_dma_without_bch_irq(this, desc);
652
653 dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE);
111bfed4 654 if (!direct)
ba3900e6 655 memcpy(buf, this->data_buffer_dma, len);
c3ee3f3d
SH
656
657 return ret;
45dfc1a0
HS
658}
659
660int gpmi_send_page(struct gpmi_nand_data *this,
661 dma_addr_t payload, dma_addr_t auxiliary)
662{
663 struct bch_geometry *geo = &this->bch_geometry;
664 uint32_t command_mode;
665 uint32_t address;
666 uint32_t ecc_command;
667 uint32_t buffer_mask;
668 struct dma_async_tx_descriptor *desc;
669 struct dma_chan *channel = get_dma_chan(this);
670 int chip = this->current_chip;
671 u32 pio[6];
672
673 /* A DMA descriptor that does an ECC page read. */
674 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
675 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
676 ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
677 buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
678 BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
679
680 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
681 | BM_GPMI_CTRL0_WORD_LENGTH
682 | BF_GPMI_CTRL0_CS(chip, this)
683 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
684 | BF_GPMI_CTRL0_ADDRESS(address)
685 | BF_GPMI_CTRL0_XFER_COUNT(0);
686 pio[1] = 0;
687 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
688 | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
689 | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
690 pio[3] = geo->page_size;
691 pio[4] = payload;
692 pio[5] = auxiliary;
693
623ff773 694 desc = dmaengine_prep_slave_sg(channel,
45dfc1a0 695 (struct scatterlist *)pio,
921de864
HS
696 ARRAY_SIZE(pio), DMA_TRANS_NONE,
697 DMA_CTRL_ACK);
43a34b8b
HS
698 if (!desc)
699 return -EINVAL;
700
45dfc1a0
HS
701 return start_dma_with_bch_irq(this, desc);
702}
703
704int gpmi_read_page(struct gpmi_nand_data *this,
705 dma_addr_t payload, dma_addr_t auxiliary)
706{
707 struct bch_geometry *geo = &this->bch_geometry;
708 uint32_t command_mode;
709 uint32_t address;
710 uint32_t ecc_command;
711 uint32_t buffer_mask;
712 struct dma_async_tx_descriptor *desc;
713 struct dma_chan *channel = get_dma_chan(this);
714 int chip = this->current_chip;
715 u32 pio[6];
716
717 /* [1] Wait for the chip to report ready. */
718 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
719 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
720
721 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
722 | BM_GPMI_CTRL0_WORD_LENGTH
723 | BF_GPMI_CTRL0_CS(chip, this)
724 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
725 | BF_GPMI_CTRL0_ADDRESS(address)
726 | BF_GPMI_CTRL0_XFER_COUNT(0);
727 pio[1] = 0;
16052827 728 desc = dmaengine_prep_slave_sg(channel,
0ef7e206
SG
729 (struct scatterlist *)pio, 2,
730 DMA_TRANS_NONE, 0);
43a34b8b
HS
731 if (!desc)
732 return -EINVAL;
45dfc1a0
HS
733
734 /* [2] Enable the BCH block and read. */
735 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
736 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
737 ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
738 buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
739 | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
740
741 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
742 | BM_GPMI_CTRL0_WORD_LENGTH
743 | BF_GPMI_CTRL0_CS(chip, this)
744 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
745 | BF_GPMI_CTRL0_ADDRESS(address)
746 | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
747
748 pio[1] = 0;
749 pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
750 | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
751 | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
752 pio[3] = geo->page_size;
753 pio[4] = payload;
754 pio[5] = auxiliary;
16052827 755 desc = dmaengine_prep_slave_sg(channel,
45dfc1a0 756 (struct scatterlist *)pio,
921de864
HS
757 ARRAY_SIZE(pio), DMA_TRANS_NONE,
758 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
43a34b8b
HS
759 if (!desc)
760 return -EINVAL;
45dfc1a0
HS
761
762 /* [3] Disable the BCH block */
763 command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
764 address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
765
766 pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
767 | BM_GPMI_CTRL0_WORD_LENGTH
768 | BF_GPMI_CTRL0_CS(chip, this)
769 | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
770 | BF_GPMI_CTRL0_ADDRESS(address)
771 | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
772 pio[1] = 0;
09ef90d9 773 pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
16052827 774 desc = dmaengine_prep_slave_sg(channel,
09ef90d9 775 (struct scatterlist *)pio, 3,
921de864
HS
776 DMA_TRANS_NONE,
777 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
43a34b8b
HS
778 if (!desc)
779 return -EINVAL;
45dfc1a0
HS
780
781 /* [4] submit the DMA */
45dfc1a0
HS
782 return start_dma_with_bch_irq(this, desc);
783}
66de54a7
BB
784
785/**
786 * gpmi_copy_bits - copy bits from one memory region to another
787 * @dst: destination buffer
788 * @dst_bit_off: bit offset we're starting to write at
789 * @src: source buffer
790 * @src_bit_off: bit offset we're starting to read from
791 * @nbits: number of bits to copy
792 *
793 * This functions copies bits from one memory region to another, and is used by
794 * the GPMI driver to copy ECC sections which are not guaranteed to be byte
795 * aligned.
796 *
797 * src and dst should not overlap.
798 *
799 */
800void gpmi_copy_bits(u8 *dst, size_t dst_bit_off,
801 const u8 *src, size_t src_bit_off,
802 size_t nbits)
803{
804 size_t i;
805 size_t nbytes;
806 u32 src_buffer = 0;
807 size_t bits_in_src_buffer = 0;
808
809 if (!nbits)
810 return;
811
812 /*
813 * Move src and dst pointers to the closest byte pointer and store bit
814 * offsets within a byte.
815 */
816 src += src_bit_off / 8;
817 src_bit_off %= 8;
818
819 dst += dst_bit_off / 8;
820 dst_bit_off %= 8;
821
822 /*
823 * Initialize the src_buffer value with bits available in the first
824 * byte of data so that we end up with a byte aligned src pointer.
825 */
826 if (src_bit_off) {
827 src_buffer = src[0] >> src_bit_off;
828 if (nbits >= (8 - src_bit_off)) {
829 bits_in_src_buffer += 8 - src_bit_off;
830 } else {
831 src_buffer &= GENMASK(nbits - 1, 0);
832 bits_in_src_buffer += nbits;
833 }
834 nbits -= bits_in_src_buffer;
835 src++;
836 }
837
838 /* Calculate the number of bytes that can be copied from src to dst. */
839 nbytes = nbits / 8;
840
841 /* Try to align dst to a byte boundary. */
842 if (dst_bit_off) {
843 if (bits_in_src_buffer < (8 - dst_bit_off) && nbytes) {
844 src_buffer |= src[0] << bits_in_src_buffer;
845 bits_in_src_buffer += 8;
846 src++;
847 nbytes--;
848 }
849
850 if (bits_in_src_buffer >= (8 - dst_bit_off)) {
851 dst[0] &= GENMASK(dst_bit_off - 1, 0);
852 dst[0] |= src_buffer << dst_bit_off;
853 src_buffer >>= (8 - dst_bit_off);
854 bits_in_src_buffer -= (8 - dst_bit_off);
855 dst_bit_off = 0;
856 dst++;
857 if (bits_in_src_buffer > 7) {
858 bits_in_src_buffer -= 8;
859 dst[0] = src_buffer;
860 dst++;
861 src_buffer >>= 8;
862 }
863 }
864 }
865
866 if (!bits_in_src_buffer && !dst_bit_off) {
867 /*
868 * Both src and dst pointers are byte aligned, thus we can
869 * just use the optimized memcpy function.
870 */
871 if (nbytes)
872 memcpy(dst, src, nbytes);
873 } else {
874 /*
875 * src buffer is not byte aligned, hence we have to copy each
876 * src byte to the src_buffer variable before extracting a byte
877 * to store in dst.
878 */
879 for (i = 0; i < nbytes; i++) {
880 src_buffer |= src[i] << bits_in_src_buffer;
881 dst[i] = src_buffer;
882 src_buffer >>= 8;
883 }
884 }
885 /* Update dst and src pointers */
886 dst += nbytes;
887 src += nbytes;
888
889 /*
890 * nbits is the number of remaining bits. It should not exceed 8 as
891 * we've already copied as much bytes as possible.
892 */
893 nbits %= 8;
894
895 /*
896 * If there's no more bits to copy to the destination and src buffer
897 * was already byte aligned, then we're done.
898 */
899 if (!nbits && !bits_in_src_buffer)
900 return;
901
902 /* Copy the remaining bits to src_buffer */
903 if (nbits)
904 src_buffer |= (*src & GENMASK(nbits - 1, 0)) <<
905 bits_in_src_buffer;
906 bits_in_src_buffer += nbits;
907
908 /*
909 * In case there were not enough bits to get a byte aligned dst buffer
910 * prepare the src_buffer variable to match the dst organization (shift
911 * src_buffer by dst_bit_off and retrieve the least significant bits
912 * from dst).
913 */
914 if (dst_bit_off)
915 src_buffer = (src_buffer << dst_bit_off) |
916 (*dst & GENMASK(dst_bit_off - 1, 0));
917 bits_in_src_buffer += dst_bit_off;
918
919 /*
920 * Keep most significant bits from dst if we end up with an unaligned
921 * number of bits.
922 */
923 nbytes = bits_in_src_buffer / 8;
924 if (bits_in_src_buffer % 8) {
925 src_buffer |= (dst[nbytes] &
926 GENMASK(7, bits_in_src_buffer % 8)) <<
927 (nbytes * 8);
928 nbytes++;
929 }
930
931 /* Copy the remaining bytes to dst */
932 for (i = 0; i < nbytes; i++) {
933 dst[i] = src_buffer;
934 src_buffer >>= 8;
935 }
936}