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