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        if (common_nfc_set_geometry(this))
 262                return !0;
 263
 264        block_count   = bch_geo->ecc_chunk_count - 1;
 265        block_size    = bch_geo->ecc_chunk_size;
 266        metadata_size = bch_geo->metadata_size;
 267        ecc_strength  = bch_geo->ecc_strength >> 1;
 268        page_size     = bch_geo->page_size;
 269        gf_len        = bch_geo->gf_len;
 270
 271        ret = gpmi_enable_clk(this);
 272        if (ret)
 273                return ret;
 274
 275        /*
 276        * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this
 277        * chip, otherwise it will lock up. So we skip resetting BCH on the MX23.
 278        * On the other hand, the MX28 needs the reset, because one case has been
 279        * seen where the BCH produced ECC errors constantly after 10000
 280        * consecutive reboots. The latter case has not been seen on the MX23
 281        * yet, still we don't know if it could happen there as well.
 282        */
 283        ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
 284        if (ret)
 285                goto err_out;
 286
 287        /* Configure layout 0. */
 288        writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count)
 289                        | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size)
 290                        | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this)
 291                        | BF_BCH_FLASH0LAYOUT0_GF(gf_len, this)
 292                        | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this),
 293                        r->bch_regs + HW_BCH_FLASH0LAYOUT0);
 294
 295        writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size)
 296                        | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this)
 297                        | BF_BCH_FLASH0LAYOUT1_GF(gf_len, this)
 298                        | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this),
 299                        r->bch_regs + HW_BCH_FLASH0LAYOUT1);
 300
 301        /* Set *all* chip selects to use layout 0. */
 302        writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
 303
 304        /* Enable interrupts. */
 305        writel(BM_BCH_CTRL_COMPLETE_IRQ_EN,
 306                                r->bch_regs + HW_BCH_CTRL_SET);
 307
 308        gpmi_disable_clk(this);
 309        return 0;
 310err_out:
 311        gpmi_disable_clk(this);
 312        return ret;
 313}
 314
 315/*
 316 * <1> Firstly, we should know what's the GPMI-clock means.
 317 *     The GPMI-clock is the internal clock in the gpmi nand controller.
 318 *     If you set 100MHz to gpmi nand controller, the GPMI-clock's period
 319 *     is 10ns. Mark the GPMI-clock's period as GPMI-clock-period.
 320 *
 321 * <2> Secondly, we should know what's the frequency on the nand chip pins.
 322 *     The frequency on the nand chip pins is derived from the GPMI-clock.
 323 *     We can get it from the following equation:
 324 *
 325 *         F = G / (DS + DH)
 326 *
 327 *         F  : the frequency on the nand chip pins.
 328 *         G  : the GPMI clock, such as 100MHz.
 329 *         DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP
 330 *         DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD
 331 *
 332 * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz,
 333 *     the nand EDO(extended Data Out) timing could be applied.
 334 *     The GPMI implements a feedback read strobe to sample the read data.
 335 *     The feedback read strobe can be delayed to support the nand EDO timing
 336 *     where the read strobe may deasserts before the read data is valid, and
 337 *     read data is valid for some time after read strobe.
 338 *
 339 *     The following figure illustrates some aspects of a NAND Flash read:
 340 *
 341 *                   |<---tREA---->|
 342 *                   |             |
 343 *                   |         |   |
 344 *                   |<--tRP-->|   |
 345 *                   |         |   |
 346 *                  __          ___|__________________________________
 347 *     RDN            \________/   |
 348 *                                 |
 349 *                                 /---------\
 350 *     Read Data    --------------<           >---------
 351 *                                 \---------/
 352 *                                |     |
 353 *                                |<-D->|
 354 *     FeedbackRDN  ________             ____________
 355 *                          \___________/
 356 *
 357 *          D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY.
 358 *
 359 *
 360 * <4> Now, we begin to describe how to compute the right RDN_DELAY.
 361 *
 362 *  4.1) From the aspect of the nand chip pins:
 363 *        Delay = (tREA + C - tRP)               {1}
 364 *
 365 *        tREA : the maximum read access time.
 366 *        C    : a constant to adjust the delay. default is 4000ps.
 367 *        tRP  : the read pulse width, which is exactly:
 368 *                   tRP = (GPMI-clock-period) * DATA_SETUP
 369 *
 370 *  4.2) From the aspect of the GPMI nand controller:
 371 *         Delay = RDN_DELAY * 0.125 * RP        {2}
 372 *
 373 *         RP   : the DLL reference period.
 374 *            if (GPMI-clock-period > DLL_THRETHOLD)
 375 *                   RP = GPMI-clock-period / 2;
 376 *            else
 377 *                   RP = GPMI-clock-period;
 378 *
 379 *            Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period
 380 *            is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD
 381 *            is 16000ps, but in mx6q, we use 12000ps.
 382 *
 383 *  4.3) since {1} equals {2}, we get:
 384 *
 385 *                     (tREA + 4000 - tRP) * 8
 386 *         RDN_DELAY = -----------------------     {3}
 387 *                           RP
 388 */
 389static void gpmi_nfc_compute_timings(struct gpmi_nand_data *this,
 390                                     const struct nand_sdr_timings *sdr)
 391{
 392        struct gpmi_nfc_hardware_timing *hw = &this->hw;
 393        unsigned int dll_threshold_ps = this->devdata->max_chain_delay;
 394        unsigned int period_ps, reference_period_ps;
 395        unsigned int data_setup_cycles, data_hold_cycles, addr_setup_cycles;
 396        unsigned int tRP_ps;
 397        bool use_half_period;
 398        int sample_delay_ps, sample_delay_factor;
 399        u16 busy_timeout_cycles;
 400        u8 wrn_dly_sel;
 401
 402        if (sdr->tRC_min >= 30000) {
 403                /* ONFI non-EDO modes [0-3] */
 404                hw->clk_rate = 22000000;
 405                wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS;
 406        } else if (sdr->tRC_min >= 25000) {
 407                /* ONFI EDO mode 4 */
 408                hw->clk_rate = 80000000;
 409                wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
 410        } else {
 411                /* ONFI EDO mode 5 */
 412                hw->clk_rate = 100000000;
 413                wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
 414        }
 415
 416        /* SDR core timings are given in picoseconds */
 417        period_ps = div_u64((u64)NSEC_PER_SEC * 1000, hw->clk_rate);
 418
 419        addr_setup_cycles = TO_CYCLES(sdr->tALS_min, period_ps);
 420        data_setup_cycles = TO_CYCLES(sdr->tDS_min, period_ps);
 421        data_hold_cycles = TO_CYCLES(sdr->tDH_min, period_ps);
 422        busy_timeout_cycles = TO_CYCLES(sdr->tWB_max + sdr->tR_max, period_ps);
 423
 424        hw->timing0 = BF_GPMI_TIMING0_ADDRESS_SETUP(addr_setup_cycles) |
 425                      BF_GPMI_TIMING0_DATA_HOLD(data_hold_cycles) |
 426                      BF_GPMI_TIMING0_DATA_SETUP(data_setup_cycles);
 427        hw->timing1 = BF_GPMI_TIMING1_BUSY_TIMEOUT(busy_timeout_cycles * 4096);
 428
 429        /*
 430         * Derive NFC ideal delay from {3}:
 431         *
 432         *                     (tREA + 4000 - tRP) * 8
 433         *         RDN_DELAY = -----------------------
 434         *                                RP
 435         */
 436        if (period_ps > dll_threshold_ps) {
 437                use_half_period = true;
 438                reference_period_ps = period_ps / 2;
 439        } else {
 440                use_half_period = false;
 441                reference_period_ps = period_ps;
 442        }
 443
 444        tRP_ps = data_setup_cycles * period_ps;
 445        sample_delay_ps = (sdr->tREA_max + 4000 - tRP_ps) * 8;
 446        if (sample_delay_ps > 0)
 447                sample_delay_factor = sample_delay_ps / reference_period_ps;
 448        else
 449                sample_delay_factor = 0;
 450
 451        hw->ctrl1n = BF_GPMI_CTRL1_WRN_DLY_SEL(wrn_dly_sel);
 452        if (sample_delay_factor)
 453                hw->ctrl1n |= BF_GPMI_CTRL1_RDN_DELAY(sample_delay_factor) |
 454                              BM_GPMI_CTRL1_DLL_ENABLE |
 455                              (use_half_period ? BM_GPMI_CTRL1_HALF_PERIOD : 0);
 456}
 457
 458void gpmi_nfc_apply_timings(struct gpmi_nand_data *this)
 459{
 460        struct gpmi_nfc_hardware_timing *hw = &this->hw;
 461        struct resources *r = &this->resources;
 462        void __iomem *gpmi_regs = r->gpmi_regs;
 463        unsigned int dll_wait_time_us;
 464
 465        clk_set_rate(r->clock[0], hw->clk_rate);
 466
 467        writel(hw->timing0, gpmi_regs + HW_GPMI_TIMING0);
 468        writel(hw->timing1, gpmi_regs + HW_GPMI_TIMING1);
 469
 470        /*
 471         * Clear several CTRL1 fields, DLL must be disabled when setting
 472         * RDN_DELAY or HALF_PERIOD.
 473         */
 474        writel(BM_GPMI_CTRL1_CLEAR_MASK, gpmi_regs + HW_GPMI_CTRL1_CLR);
 475        writel(hw->ctrl1n, gpmi_regs + HW_GPMI_CTRL1_SET);
 476
 477        /* Wait 64 clock cycles before using the GPMI after enabling the DLL */
 478        dll_wait_time_us = USEC_PER_SEC / hw->clk_rate * 64;
 479        if (!dll_wait_time_us)
 480                dll_wait_time_us = 1;
 481
 482        /* Wait for the DLL to settle. */
 483        udelay(dll_wait_time_us);
 484}
 485
 486int gpmi_setup_data_interface(struct mtd_info *mtd, int chipnr,
 487                              const struct nand_data_interface *conf)
 488{
 489        struct nand_chip *chip = mtd_to_nand(mtd);
 490        struct gpmi_nand_data *this = nand_get_controller_data(chip);
 491        const struct nand_sdr_timings *sdr;
 492
 493        /* Retrieve required NAND timings */
 494        sdr = nand_get_sdr_timings(conf);
 495        if (IS_ERR(sdr))
 496                return PTR_ERR(sdr);
 497
 498        /* Only MX6 GPMI controller can reach EDO timings */
 499        if (sdr->tRC_min <= 25000 && !GPMI_IS_MX6(this))
 500                return -ENOTSUPP;
 501
 502        /* Stop here if this call was just a check */
 503        if (chipnr < 0)
 504                return 0;
 505
 506        /* Do the actual derivation of the controller timings */
 507        gpmi_nfc_compute_timings(this, sdr);
 508
 509        this->hw.must_apply_timings = true;
 510
 511        return 0;
 512}
 513
 514/* Clears a BCH interrupt. */
 515void gpmi_clear_bch(struct gpmi_nand_data *this)
 516{
 517        struct resources *r = &this->resources;
 518        writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR);
 519}
 520
 521/* Returns the Ready/Busy status of the given chip. */
 522int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip)
 523{
 524        struct resources *r = &this->resources;
 525        uint32_t mask = 0;
 526        uint32_t reg = 0;
 527
 528        if (GPMI_IS_MX23(this)) {
 529                mask = MX23_BM_GPMI_DEBUG_READY0 << chip;
 530                reg = readl(r->gpmi_regs + HW_GPMI_DEBUG);
 531        } else if (GPMI_IS_MX28(this) || GPMI_IS_MX6(this)) {
 532                /*
 533                 * In the imx6, all the ready/busy pins are bound
 534                 * together. So we only need to check chip 0.
 535                 */
 536                if (GPMI_IS_MX6(this))
 537                        chip = 0;
 538
 539                /* MX28 shares the same R/B register as MX6Q. */
 540                mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip);
 541                reg = readl(r->gpmi_regs + HW_GPMI_STAT);
 542        } else
 543                dev_err(this->dev, "unknown arch.\n");
 544        return reg & mask;
 545}
 546
 547static inline void set_dma_type(struct gpmi_nand_data *this,
 548                                        enum dma_ops_type type)
 549{
 550        this->last_dma_type = this->dma_type;
 551        this->dma_type = type;
 552}
 553
 554int gpmi_send_command(struct gpmi_nand_data *this)
 555{
 556        struct dma_chan *channel = get_dma_chan(this);
 557        struct dma_async_tx_descriptor *desc;
 558        struct scatterlist *sgl;
 559        int chip = this->current_chip;
 560        u32 pio[3];
 561
 562        /* [1] send out the PIO words */
 563        pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE)
 564                | BM_GPMI_CTRL0_WORD_LENGTH
 565                | BF_GPMI_CTRL0_CS(chip, this)
 566                | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
 567                | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE)
 568                | BM_GPMI_CTRL0_ADDRESS_INCREMENT
 569                | BF_GPMI_CTRL0_XFER_COUNT(this->command_length);
 570        pio[1] = pio[2] = 0;
 571        desc = dmaengine_prep_slave_sg(channel,
 572                                        (struct scatterlist *)pio,
 573                                        ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
 574        if (!desc)
 575                return -EINVAL;
 576
 577        /* [2] send out the COMMAND + ADDRESS string stored in @buffer */
 578        sgl = &this->cmd_sgl;
 579
 580        sg_init_one(sgl, this->cmd_buffer, this->command_length);
 581        dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE);
 582        desc = dmaengine_prep_slave_sg(channel,
 583                                sgl, 1, DMA_MEM_TO_DEV,
 584                                DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 585        if (!desc)
 586                return -EINVAL;
 587
 588        /* [3] submit the DMA */
 589        set_dma_type(this, DMA_FOR_COMMAND);
 590        return start_dma_without_bch_irq(this, desc);
 591}
 592
 593int gpmi_send_data(struct gpmi_nand_data *this)
 594{
 595        struct dma_async_tx_descriptor *desc;
 596        struct dma_chan *channel = get_dma_chan(this);
 597        int chip = this->current_chip;
 598        uint32_t command_mode;
 599        uint32_t address;
 600        u32 pio[2];
 601
 602        /* [1] PIO */
 603        command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
 604        address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
 605
 606        pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
 607                | BM_GPMI_CTRL0_WORD_LENGTH
 608                | BF_GPMI_CTRL0_CS(chip, this)
 609                | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
 610                | BF_GPMI_CTRL0_ADDRESS(address)
 611                | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
 612        pio[1] = 0;
 613        desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio,
 614                                        ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
 615        if (!desc)
 616                return -EINVAL;
 617
 618        /* [2] send DMA request */
 619        prepare_data_dma(this, DMA_TO_DEVICE);
 620        desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
 621                                        1, DMA_MEM_TO_DEV,
 622                                        DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 623        if (!desc)
 624                return -EINVAL;
 625
 626        /* [3] submit the DMA */
 627        set_dma_type(this, DMA_FOR_WRITE_DATA);
 628        return start_dma_without_bch_irq(this, desc);
 629}
 630
 631int gpmi_read_data(struct gpmi_nand_data *this)
 632{
 633        struct dma_async_tx_descriptor *desc;
 634        struct dma_chan *channel = get_dma_chan(this);
 635        int chip = this->current_chip;
 636        u32 pio[2];
 637
 638        /* [1] : send PIO */
 639        pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ)
 640                | BM_GPMI_CTRL0_WORD_LENGTH
 641                | BF_GPMI_CTRL0_CS(chip, this)
 642                | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
 643                | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA)
 644                | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len);
 645        pio[1] = 0;
 646        desc = dmaengine_prep_slave_sg(channel,
 647                                        (struct scatterlist *)pio,
 648                                        ARRAY_SIZE(pio), DMA_TRANS_NONE, 0);
 649        if (!desc)
 650                return -EINVAL;
 651
 652        /* [2] : send DMA request */
 653        prepare_data_dma(this, DMA_FROM_DEVICE);
 654        desc = dmaengine_prep_slave_sg(channel, &this->data_sgl,
 655                                        1, DMA_DEV_TO_MEM,
 656                                        DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 657        if (!desc)
 658                return -EINVAL;
 659
 660        /* [3] : submit the DMA */
 661        set_dma_type(this, DMA_FOR_READ_DATA);
 662        return start_dma_without_bch_irq(this, desc);
 663}
 664
 665int gpmi_send_page(struct gpmi_nand_data *this,
 666                        dma_addr_t payload, dma_addr_t auxiliary)
 667{
 668        struct bch_geometry *geo = &this->bch_geometry;
 669        uint32_t command_mode;
 670        uint32_t address;
 671        uint32_t ecc_command;
 672        uint32_t buffer_mask;
 673        struct dma_async_tx_descriptor *desc;
 674        struct dma_chan *channel = get_dma_chan(this);
 675        int chip = this->current_chip;
 676        u32 pio[6];
 677
 678        /* A DMA descriptor that does an ECC page read. */
 679        command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE;
 680        address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
 681        ecc_command  = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE;
 682        buffer_mask  = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
 683                                BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
 684
 685        pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
 686                | BM_GPMI_CTRL0_WORD_LENGTH
 687                | BF_GPMI_CTRL0_CS(chip, this)
 688                | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
 689                | BF_GPMI_CTRL0_ADDRESS(address)
 690                | BF_GPMI_CTRL0_XFER_COUNT(0);
 691        pio[1] = 0;
 692        pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC
 693                | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
 694                | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
 695        pio[3] = geo->page_size;
 696        pio[4] = payload;
 697        pio[5] = auxiliary;
 698
 699        desc = dmaengine_prep_slave_sg(channel,
 700                                        (struct scatterlist *)pio,
 701                                        ARRAY_SIZE(pio), DMA_TRANS_NONE,
 702                                        DMA_CTRL_ACK);
 703        if (!desc)
 704                return -EINVAL;
 705
 706        set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE);
 707        return start_dma_with_bch_irq(this, desc);
 708}
 709
 710int gpmi_read_page(struct gpmi_nand_data *this,
 711                                dma_addr_t payload, dma_addr_t auxiliary)
 712{
 713        struct bch_geometry *geo = &this->bch_geometry;
 714        uint32_t command_mode;
 715        uint32_t address;
 716        uint32_t ecc_command;
 717        uint32_t buffer_mask;
 718        struct dma_async_tx_descriptor *desc;
 719        struct dma_chan *channel = get_dma_chan(this);
 720        int chip = this->current_chip;
 721        u32 pio[6];
 722
 723        /* [1] Wait for the chip to report ready. */
 724        command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
 725        address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
 726
 727        pio[0] =  BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
 728                | BM_GPMI_CTRL0_WORD_LENGTH
 729                | BF_GPMI_CTRL0_CS(chip, this)
 730                | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
 731                | BF_GPMI_CTRL0_ADDRESS(address)
 732                | BF_GPMI_CTRL0_XFER_COUNT(0);
 733        pio[1] = 0;
 734        desc = dmaengine_prep_slave_sg(channel,
 735                                (struct scatterlist *)pio, 2,
 736                                DMA_TRANS_NONE, 0);
 737        if (!desc)
 738                return -EINVAL;
 739
 740        /* [2] Enable the BCH block and read. */
 741        command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ;
 742        address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
 743        ecc_command  = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE;
 744        buffer_mask  = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE
 745                        | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY;
 746
 747        pio[0] =  BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
 748                | BM_GPMI_CTRL0_WORD_LENGTH
 749                | BF_GPMI_CTRL0_CS(chip, this)
 750                | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
 751                | BF_GPMI_CTRL0_ADDRESS(address)
 752                | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
 753
 754        pio[1] = 0;
 755        pio[2] =  BM_GPMI_ECCCTRL_ENABLE_ECC
 756                | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command)
 757                | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask);
 758        pio[3] = geo->page_size;
 759        pio[4] = payload;
 760        pio[5] = auxiliary;
 761        desc = dmaengine_prep_slave_sg(channel,
 762                                        (struct scatterlist *)pio,
 763                                        ARRAY_SIZE(pio), DMA_TRANS_NONE,
 764                                        DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 765        if (!desc)
 766                return -EINVAL;
 767
 768        /* [3] Disable the BCH block */
 769        command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY;
 770        address      = BV_GPMI_CTRL0_ADDRESS__NAND_DATA;
 771
 772        pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode)
 773                | BM_GPMI_CTRL0_WORD_LENGTH
 774                | BF_GPMI_CTRL0_CS(chip, this)
 775                | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this)
 776                | BF_GPMI_CTRL0_ADDRESS(address)
 777                | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size);
 778        pio[1] = 0;
 779        pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */
 780        desc = dmaengine_prep_slave_sg(channel,
 781                                (struct scatterlist *)pio, 3,
 782                                DMA_TRANS_NONE,
 783                                DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
 784        if (!desc)
 785                return -EINVAL;
 786
 787        /* [4] submit the DMA */
 788        set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
 789        return start_dma_with_bch_irq(this, desc);
 790}
 791
 792/**
 793 * gpmi_copy_bits - copy bits from one memory region to another
 794 * @dst: destination buffer
 795 * @dst_bit_off: bit offset we're starting to write at
 796 * @src: source buffer
 797 * @src_bit_off: bit offset we're starting to read from
 798 * @nbits: number of bits to copy
 799 *
 800 * This functions copies bits from one memory region to another, and is used by
 801 * the GPMI driver to copy ECC sections which are not guaranteed to be byte
 802 * aligned.
 803 *
 804 * src and dst should not overlap.
 805 *
 806 */
 807void gpmi_copy_bits(u8 *dst, size_t dst_bit_off,
 808                    const u8 *src, size_t src_bit_off,
 809                    size_t nbits)
 810{
 811        size_t i;
 812        size_t nbytes;
 813        u32 src_buffer = 0;
 814        size_t bits_in_src_buffer = 0;
 815
 816        if (!nbits)
 817                return;
 818
 819        /*
 820         * Move src and dst pointers to the closest byte pointer and store bit
 821         * offsets within a byte.
 822         */
 823        src += src_bit_off / 8;
 824        src_bit_off %= 8;
 825
 826        dst += dst_bit_off / 8;
 827        dst_bit_off %= 8;
 828
 829        /*
 830         * Initialize the src_buffer value with bits available in the first
 831         * byte of data so that we end up with a byte aligned src pointer.
 832         */
 833        if (src_bit_off) {
 834                src_buffer = src[0] >> src_bit_off;
 835                if (nbits >= (8 - src_bit_off)) {
 836                        bits_in_src_buffer += 8 - src_bit_off;
 837                } else {
 838                        src_buffer &= GENMASK(nbits - 1, 0);
 839                        bits_in_src_buffer += nbits;
 840                }
 841                nbits -= bits_in_src_buffer;
 842                src++;
 843        }
 844
 845        /* Calculate the number of bytes that can be copied from src to dst. */
 846        nbytes = nbits / 8;
 847
 848        /* Try to align dst to a byte boundary. */
 849        if (dst_bit_off) {
 850                if (bits_in_src_buffer < (8 - dst_bit_off) && nbytes) {
 851                        src_buffer |= src[0] << bits_in_src_buffer;
 852                        bits_in_src_buffer += 8;
 853                        src++;
 854                        nbytes--;
 855                }
 856
 857                if (bits_in_src_buffer >= (8 - dst_bit_off)) {
 858                        dst[0] &= GENMASK(dst_bit_off - 1, 0);
 859                        dst[0] |= src_buffer << dst_bit_off;
 860                        src_buffer >>= (8 - dst_bit_off);
 861                        bits_in_src_buffer -= (8 - dst_bit_off);
 862                        dst_bit_off = 0;
 863                        dst++;
 864                        if (bits_in_src_buffer > 7) {
 865                                bits_in_src_buffer -= 8;
 866                                dst[0] = src_buffer;
 867                                dst++;
 868                                src_buffer >>= 8;
 869                        }
 870                }
 871        }
 872
 873        if (!bits_in_src_buffer && !dst_bit_off) {
 874                /*
 875                 * Both src and dst pointers are byte aligned, thus we can
 876                 * just use the optimized memcpy function.
 877                 */
 878                if (nbytes)
 879                        memcpy(dst, src, nbytes);
 880        } else {
 881                /*
 882                 * src buffer is not byte aligned, hence we have to copy each
 883                 * src byte to the src_buffer variable before extracting a byte
 884                 * to store in dst.
 885                 */
 886                for (i = 0; i < nbytes; i++) {
 887                        src_buffer |= src[i] << bits_in_src_buffer;
 888                        dst[i] = src_buffer;
 889                        src_buffer >>= 8;
 890                }
 891        }
 892        /* Update dst and src pointers */
 893        dst += nbytes;
 894        src += nbytes;
 895
 896        /*
 897         * nbits is the number of remaining bits. It should not exceed 8 as
 898         * we've already copied as much bytes as possible.
 899         */
 900        nbits %= 8;
 901
 902        /*
 903         * If there's no more bits to copy to the destination and src buffer
 904         * was already byte aligned, then we're done.
 905         */
 906        if (!nbits && !bits_in_src_buffer)
 907                return;
 908
 909        /* Copy the remaining bits to src_buffer */
 910        if (nbits)
 911                src_buffer |= (*src & GENMASK(nbits - 1, 0)) <<
 912                              bits_in_src_buffer;
 913        bits_in_src_buffer += nbits;
 914
 915        /*
 916         * In case there were not enough bits to get a byte aligned dst buffer
 917         * prepare the src_buffer variable to match the dst organization (shift
 918         * src_buffer by dst_bit_off and retrieve the least significant bits
 919         * from dst).
 920         */
 921        if (dst_bit_off)
 922                src_buffer = (src_buffer << dst_bit_off) |
 923                             (*dst & GENMASK(dst_bit_off - 1, 0));
 924        bits_in_src_buffer += dst_bit_off;
 925
 926        /*
 927         * Keep most significant bits from dst if we end up with an unaligned
 928         * number of bits.
 929         */
 930        nbytes = bits_in_src_buffer / 8;
 931        if (bits_in_src_buffer % 8) {
 932                src_buffer |= (dst[nbytes] &
 933                               GENMASK(7, bits_in_src_buffer % 8)) <<
 934                              (nbytes * 8);
 935                nbytes++;
 936        }
 937
 938        /* Copy the remaining bytes to dst */
 939        for (i = 0; i < nbytes; i++) {
 940                dst[i] = src_buffer;
 941                src_buffer >>= 8;
 942        }
 943}
 944