Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 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 | */ |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 21 | #include <linux/delay.h> |
| 22 | #include <linux/clk.h> |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 23 | |
| 24 | #include "gpmi-nand.h" |
| 25 | #include "gpmi-regs.h" |
| 26 | #include "bch-regs.h" |
| 27 | |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 28 | static struct timing_threshod timing_default_threshold = { |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 29 | .max_data_setup_cycles = (BM_GPMI_TIMING0_DATA_SETUP >> |
| 30 | BP_GPMI_TIMING0_DATA_SETUP), |
| 31 | .internal_data_setup_in_ns = 0, |
| 32 | .max_sample_delay_factor = (BM_GPMI_CTRL1_RDN_DELAY >> |
| 33 | BP_GPMI_CTRL1_RDN_DELAY), |
| 34 | .max_dll_clock_period_in_ns = 32, |
| 35 | .max_dll_delay_in_ns = 16, |
| 36 | }; |
| 37 | |
Huang Shijie | 4aa6ae3 | 2012-03-31 22:36:57 -0400 | [diff] [blame] | 38 | #define MXS_SET_ADDR 0x4 |
| 39 | #define MXS_CLR_ADDR 0x8 |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 40 | /* |
| 41 | * Clear the bit and poll it cleared. This is usually called with |
| 42 | * a reset address and mask being either SFTRST(bit 31) or CLKGATE |
| 43 | * (bit 30). |
| 44 | */ |
| 45 | static int clear_poll_bit(void __iomem *addr, u32 mask) |
| 46 | { |
| 47 | int timeout = 0x400; |
| 48 | |
| 49 | /* clear the bit */ |
Huang Shijie | 4aa6ae3 | 2012-03-31 22:36:57 -0400 | [diff] [blame] | 50 | writel(mask, addr + MXS_CLR_ADDR); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 51 | |
| 52 | /* |
| 53 | * SFTRST needs 3 GPMI clocks to settle, the reference manual |
| 54 | * recommends to wait 1us. |
| 55 | */ |
| 56 | udelay(1); |
| 57 | |
| 58 | /* poll the bit becoming clear */ |
| 59 | while ((readl(addr) & mask) && --timeout) |
| 60 | /* nothing */; |
| 61 | |
| 62 | return !timeout; |
| 63 | } |
| 64 | |
| 65 | #define MODULE_CLKGATE (1 << 30) |
| 66 | #define MODULE_SFTRST (1 << 31) |
| 67 | /* |
| 68 | * The current mxs_reset_block() will do two things: |
| 69 | * [1] enable the module. |
| 70 | * [2] reset the module. |
| 71 | * |
Huang Shijie | 9398d1c | 2012-01-04 11:18:46 +0800 | [diff] [blame] | 72 | * In most of the cases, it's ok. |
| 73 | * But in MX23, there is a hardware bug in the BCH block (see erratum #2847). |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 74 | * If you try to soft reset the BCH block, it becomes unusable until |
| 75 | * the next hard reset. This case occurs in the NAND boot mode. When the board |
| 76 | * boots by NAND, the ROM of the chip will initialize the BCH blocks itself. |
| 77 | * So If the driver tries to reset the BCH again, the BCH will not work anymore. |
Huang Shijie | 9398d1c | 2012-01-04 11:18:46 +0800 | [diff] [blame] | 78 | * You will see a DMA timeout in this case. The bug has been fixed |
| 79 | * in the following chips, such as MX28. |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 80 | * |
| 81 | * To avoid this bug, just add a new parameter `just_enable` for |
| 82 | * the mxs_reset_block(), and rewrite it here. |
| 83 | */ |
Huang Shijie | 9398d1c | 2012-01-04 11:18:46 +0800 | [diff] [blame] | 84 | static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable) |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 85 | { |
| 86 | int ret; |
| 87 | int timeout = 0x400; |
| 88 | |
| 89 | /* clear and poll SFTRST */ |
| 90 | ret = clear_poll_bit(reset_addr, MODULE_SFTRST); |
| 91 | if (unlikely(ret)) |
| 92 | goto error; |
| 93 | |
| 94 | /* clear CLKGATE */ |
Huang Shijie | 4aa6ae3 | 2012-03-31 22:36:57 -0400 | [diff] [blame] | 95 | writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 96 | |
| 97 | if (!just_enable) { |
| 98 | /* set SFTRST to reset the block */ |
Huang Shijie | 4aa6ae3 | 2012-03-31 22:36:57 -0400 | [diff] [blame] | 99 | writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 100 | udelay(1); |
| 101 | |
| 102 | /* poll CLKGATE becoming set */ |
| 103 | while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout) |
| 104 | /* nothing */; |
| 105 | if (unlikely(!timeout)) |
| 106 | goto error; |
| 107 | } |
| 108 | |
| 109 | /* clear and poll SFTRST */ |
| 110 | ret = clear_poll_bit(reset_addr, MODULE_SFTRST); |
| 111 | if (unlikely(ret)) |
| 112 | goto error; |
| 113 | |
| 114 | /* clear and poll CLKGATE */ |
| 115 | ret = clear_poll_bit(reset_addr, MODULE_CLKGATE); |
| 116 | if (unlikely(ret)) |
| 117 | goto error; |
| 118 | |
| 119 | return 0; |
| 120 | |
| 121 | error: |
| 122 | pr_err("%s(%p): module reset timeout\n", __func__, reset_addr); |
| 123 | return -ETIMEDOUT; |
| 124 | } |
| 125 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 126 | static int __gpmi_enable_clk(struct gpmi_nand_data *this, bool v) |
| 127 | { |
| 128 | struct clk *clk; |
| 129 | int ret; |
| 130 | int i; |
| 131 | |
| 132 | for (i = 0; i < GPMI_CLK_MAX; i++) { |
| 133 | clk = this->resources.clock[i]; |
| 134 | if (!clk) |
| 135 | break; |
| 136 | |
| 137 | if (v) { |
| 138 | ret = clk_prepare_enable(clk); |
| 139 | if (ret) |
| 140 | goto err_clk; |
| 141 | } else { |
| 142 | clk_disable_unprepare(clk); |
| 143 | } |
| 144 | } |
| 145 | return 0; |
| 146 | |
| 147 | err_clk: |
| 148 | for (; i > 0; i--) |
| 149 | clk_disable_unprepare(this->resources.clock[i - 1]); |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | #define gpmi_enable_clk(x) __gpmi_enable_clk(x, true) |
| 154 | #define gpmi_disable_clk(x) __gpmi_enable_clk(x, false) |
| 155 | |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 156 | int gpmi_init(struct gpmi_nand_data *this) |
| 157 | { |
| 158 | struct resources *r = &this->resources; |
| 159 | int ret; |
| 160 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 161 | ret = gpmi_enable_clk(this); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 162 | if (ret) |
| 163 | goto err_out; |
| 164 | ret = gpmi_reset_block(r->gpmi_regs, false); |
| 165 | if (ret) |
| 166 | goto err_out; |
| 167 | |
Wolfram Sang | 6f2a6a5 | 2012-12-05 21:46:02 +0100 | [diff] [blame] | 168 | /* |
| 169 | * Reset BCH here, too. We got failures otherwise :( |
| 170 | * See later BCH reset for explanation of MX23 handling |
| 171 | */ |
| 172 | ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this)); |
| 173 | if (ret) |
| 174 | goto err_out; |
| 175 | |
| 176 | |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 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 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 190 | gpmi_disable_clk(this); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 191 | return 0; |
| 192 | err_out: |
| 193 | return ret; |
| 194 | } |
| 195 | |
| 196 | /* This function is very useful. It is called only when the bug occur. */ |
| 197 | void gpmi_dump_info(struct gpmi_nand_data *this) |
| 198 | { |
| 199 | struct resources *r = &this->resources; |
| 200 | struct bch_geometry *geo = &this->bch_geometry; |
| 201 | u32 reg; |
| 202 | int i; |
| 203 | |
| 204 | pr_err("Show GPMI registers :\n"); |
| 205 | for (i = 0; i <= HW_GPMI_DEBUG / 0x10 + 1; i++) { |
| 206 | reg = readl(r->gpmi_regs + i * 0x10); |
| 207 | pr_err("offset 0x%.3x : 0x%.8x\n", i * 0x10, reg); |
| 208 | } |
| 209 | |
| 210 | /* start to print out the BCH info */ |
Huang Shijie | f722689 | 2013-01-14 15:19:29 +0800 | [diff] [blame^] | 211 | pr_err("Show BCH registers :\n"); |
| 212 | for (i = 0; i <= HW_BCH_VERSION / 0x10 + 1; i++) { |
| 213 | reg = readl(r->bch_regs + i * 0x10); |
| 214 | pr_err("offset 0x%.3x : 0x%.8x\n", i * 0x10, reg); |
| 215 | } |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 216 | pr_err("BCH Geometry :\n"); |
| 217 | pr_err("GF length : %u\n", geo->gf_len); |
| 218 | pr_err("ECC Strength : %u\n", geo->ecc_strength); |
| 219 | pr_err("Page Size in Bytes : %u\n", geo->page_size); |
| 220 | pr_err("Metadata Size in Bytes : %u\n", geo->metadata_size); |
| 221 | pr_err("ECC Chunk Size in Bytes: %u\n", geo->ecc_chunk_size); |
| 222 | pr_err("ECC Chunk Count : %u\n", geo->ecc_chunk_count); |
| 223 | pr_err("Payload Size in Bytes : %u\n", geo->payload_size); |
| 224 | pr_err("Auxiliary Size in Bytes: %u\n", geo->auxiliary_size); |
| 225 | pr_err("Auxiliary Status Offset: %u\n", geo->auxiliary_status_offset); |
| 226 | pr_err("Block Mark Byte Offset : %u\n", geo->block_mark_byte_offset); |
| 227 | pr_err("Block Mark Bit Offset : %u\n", geo->block_mark_bit_offset); |
| 228 | } |
| 229 | |
| 230 | /* Configures the geometry for BCH. */ |
| 231 | int bch_set_geometry(struct gpmi_nand_data *this) |
| 232 | { |
| 233 | struct resources *r = &this->resources; |
| 234 | struct bch_geometry *bch_geo = &this->bch_geometry; |
| 235 | unsigned int block_count; |
| 236 | unsigned int block_size; |
| 237 | unsigned int metadata_size; |
| 238 | unsigned int ecc_strength; |
| 239 | unsigned int page_size; |
| 240 | int ret; |
| 241 | |
| 242 | if (common_nfc_set_geometry(this)) |
| 243 | return !0; |
| 244 | |
| 245 | block_count = bch_geo->ecc_chunk_count - 1; |
| 246 | block_size = bch_geo->ecc_chunk_size; |
| 247 | metadata_size = bch_geo->metadata_size; |
| 248 | ecc_strength = bch_geo->ecc_strength >> 1; |
| 249 | page_size = bch_geo->page_size; |
| 250 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 251 | ret = gpmi_enable_clk(this); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 252 | if (ret) |
| 253 | goto err_out; |
| 254 | |
Huang Shijie | 9398d1c | 2012-01-04 11:18:46 +0800 | [diff] [blame] | 255 | /* |
| 256 | * Due to erratum #2847 of the MX23, the BCH cannot be soft reset on this |
| 257 | * chip, otherwise it will lock up. So we skip resetting BCH on the MX23. |
| 258 | * On the other hand, the MX28 needs the reset, because one case has been |
| 259 | * seen where the BCH produced ECC errors constantly after 10000 |
| 260 | * consecutive reboots. The latter case has not been seen on the MX23 yet, |
| 261 | * still we don't know if it could happen there as well. |
| 262 | */ |
| 263 | ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this)); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 264 | if (ret) |
| 265 | goto err_out; |
| 266 | |
| 267 | /* Configure layout 0. */ |
| 268 | writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count) |
| 269 | | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size) |
Huang Shijie | 9013bb4 | 2012-05-04 21:42:06 -0400 | [diff] [blame] | 270 | | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) |
| 271 | | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this), |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 272 | r->bch_regs + HW_BCH_FLASH0LAYOUT0); |
| 273 | |
| 274 | writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) |
Huang Shijie | 9013bb4 | 2012-05-04 21:42:06 -0400 | [diff] [blame] | 275 | | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) |
| 276 | | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this), |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 277 | r->bch_regs + HW_BCH_FLASH0LAYOUT1); |
| 278 | |
| 279 | /* Set *all* chip selects to use layout 0. */ |
| 280 | writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT); |
| 281 | |
| 282 | /* Enable interrupts. */ |
| 283 | writel(BM_BCH_CTRL_COMPLETE_IRQ_EN, |
| 284 | r->bch_regs + HW_BCH_CTRL_SET); |
| 285 | |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 286 | gpmi_disable_clk(this); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 287 | return 0; |
| 288 | err_out: |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | /* Converts time in nanoseconds to cycles. */ |
| 293 | static unsigned int ns_to_cycles(unsigned int time, |
| 294 | unsigned int period, unsigned int min) |
| 295 | { |
| 296 | unsigned int k; |
| 297 | |
| 298 | k = (time + period - 1) / period; |
| 299 | return max(k, min); |
| 300 | } |
| 301 | |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 302 | #define DEF_MIN_PROP_DELAY 5 |
| 303 | #define DEF_MAX_PROP_DELAY 9 |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 304 | /* Apply timing to current hardware conditions. */ |
| 305 | static int gpmi_nfc_compute_hardware_timing(struct gpmi_nand_data *this, |
| 306 | struct gpmi_nfc_hardware_timing *hw) |
| 307 | { |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 308 | struct timing_threshod *nfc = &timing_default_threshold; |
Huang Shijie | ae70ba2d | 2012-09-13 14:57:55 +0800 | [diff] [blame] | 309 | struct resources *r = &this->resources; |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 310 | struct nand_chip *nand = &this->nand; |
| 311 | struct nand_timing target = this->timing; |
| 312 | bool improved_timing_is_available; |
| 313 | unsigned long clock_frequency_in_hz; |
| 314 | unsigned int clock_period_in_ns; |
| 315 | bool dll_use_half_periods; |
| 316 | unsigned int dll_delay_shift; |
| 317 | unsigned int max_sample_delay_in_ns; |
| 318 | unsigned int address_setup_in_cycles; |
| 319 | unsigned int data_setup_in_ns; |
| 320 | unsigned int data_setup_in_cycles; |
| 321 | unsigned int data_hold_in_cycles; |
| 322 | int ideal_sample_delay_in_ns; |
| 323 | unsigned int sample_delay_factor; |
| 324 | int tEYE; |
Huang Shijie | e10db1f | 2012-05-04 21:42:05 -0400 | [diff] [blame] | 325 | unsigned int min_prop_delay_in_ns = DEF_MIN_PROP_DELAY; |
| 326 | unsigned int max_prop_delay_in_ns = DEF_MAX_PROP_DELAY; |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * If there are multiple chips, we need to relax the timings to allow |
| 330 | * for signal distortion due to higher capacitance. |
| 331 | */ |
| 332 | if (nand->numchips > 2) { |
| 333 | target.data_setup_in_ns += 10; |
| 334 | target.data_hold_in_ns += 10; |
| 335 | target.address_setup_in_ns += 10; |
| 336 | } else if (nand->numchips > 1) { |
| 337 | target.data_setup_in_ns += 5; |
| 338 | target.data_hold_in_ns += 5; |
| 339 | target.address_setup_in_ns += 5; |
| 340 | } |
| 341 | |
| 342 | /* Check if improved timing information is available. */ |
| 343 | improved_timing_is_available = |
| 344 | (target.tREA_in_ns >= 0) && |
| 345 | (target.tRLOH_in_ns >= 0) && |
| 346 | (target.tRHOH_in_ns >= 0) ; |
| 347 | |
| 348 | /* Inspect the clock. */ |
Huang Shijie | ae70ba2d | 2012-09-13 14:57:55 +0800 | [diff] [blame] | 349 | nfc->clock_frequency_in_hz = clk_get_rate(r->clock[0]); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 350 | clock_frequency_in_hz = nfc->clock_frequency_in_hz; |
Huang Shijie | ae70ba2d | 2012-09-13 14:57:55 +0800 | [diff] [blame] | 351 | clock_period_in_ns = NSEC_PER_SEC / clock_frequency_in_hz; |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 352 | |
| 353 | /* |
| 354 | * The NFC quantizes setup and hold parameters in terms of clock cycles. |
| 355 | * Here, we quantize the setup and hold timing parameters to the |
| 356 | * next-highest clock period to make sure we apply at least the |
| 357 | * specified times. |
| 358 | * |
| 359 | * For data setup and data hold, the hardware interprets a value of zero |
| 360 | * as the largest possible delay. This is not what's intended by a zero |
| 361 | * in the input parameter, so we impose a minimum of one cycle. |
| 362 | */ |
| 363 | data_setup_in_cycles = ns_to_cycles(target.data_setup_in_ns, |
| 364 | clock_period_in_ns, 1); |
| 365 | data_hold_in_cycles = ns_to_cycles(target.data_hold_in_ns, |
| 366 | clock_period_in_ns, 1); |
| 367 | address_setup_in_cycles = ns_to_cycles(target.address_setup_in_ns, |
| 368 | clock_period_in_ns, 0); |
| 369 | |
| 370 | /* |
| 371 | * The clock's period affects the sample delay in a number of ways: |
| 372 | * |
| 373 | * (1) The NFC HAL tells us the maximum clock period the sample delay |
| 374 | * DLL can tolerate. If the clock period is greater than half that |
| 375 | * maximum, we must configure the DLL to be driven by half periods. |
| 376 | * |
| 377 | * (2) We need to convert from an ideal sample delay, in ns, to a |
| 378 | * "sample delay factor," which the NFC uses. This factor depends on |
| 379 | * whether we're driving the DLL with full or half periods. |
| 380 | * Paraphrasing the reference manual: |
| 381 | * |
| 382 | * AD = SDF x 0.125 x RP |
| 383 | * |
| 384 | * where: |
| 385 | * |
| 386 | * AD is the applied delay, in ns. |
| 387 | * SDF is the sample delay factor, which is dimensionless. |
| 388 | * RP is the reference period, in ns, which is a full clock period |
| 389 | * if the DLL is being driven by full periods, or half that if |
| 390 | * the DLL is being driven by half periods. |
| 391 | * |
| 392 | * Let's re-arrange this in a way that's more useful to us: |
| 393 | * |
| 394 | * 8 |
| 395 | * SDF = AD x ---- |
| 396 | * RP |
| 397 | * |
| 398 | * The reference period is either the clock period or half that, so this |
| 399 | * is: |
| 400 | * |
| 401 | * 8 AD x DDF |
| 402 | * SDF = AD x ----- = -------- |
| 403 | * f x P P |
| 404 | * |
| 405 | * where: |
| 406 | * |
| 407 | * f is 1 or 1/2, depending on how we're driving the DLL. |
| 408 | * P is the clock period. |
| 409 | * DDF is the DLL Delay Factor, a dimensionless value that |
| 410 | * incorporates all the constants in the conversion. |
| 411 | * |
| 412 | * DDF will be either 8 or 16, both of which are powers of two. We can |
| 413 | * reduce the cost of this conversion by using bit shifts instead of |
| 414 | * multiplication or division. Thus: |
| 415 | * |
| 416 | * AD << DDS |
| 417 | * SDF = --------- |
| 418 | * P |
| 419 | * |
| 420 | * or |
| 421 | * |
| 422 | * AD = (SDF >> DDS) x P |
| 423 | * |
| 424 | * where: |
| 425 | * |
| 426 | * DDS is the DLL Delay Shift, the logarithm to base 2 of the DDF. |
| 427 | */ |
| 428 | if (clock_period_in_ns > (nfc->max_dll_clock_period_in_ns >> 1)) { |
| 429 | dll_use_half_periods = true; |
| 430 | dll_delay_shift = 3 + 1; |
| 431 | } else { |
| 432 | dll_use_half_periods = false; |
| 433 | dll_delay_shift = 3; |
| 434 | } |
| 435 | |
| 436 | /* |
| 437 | * Compute the maximum sample delay the NFC allows, under current |
| 438 | * conditions. If the clock is running too slowly, no sample delay is |
| 439 | * possible. |
| 440 | */ |
| 441 | if (clock_period_in_ns > nfc->max_dll_clock_period_in_ns) |
| 442 | max_sample_delay_in_ns = 0; |
| 443 | else { |
| 444 | /* |
| 445 | * Compute the delay implied by the largest sample delay factor |
| 446 | * the NFC allows. |
| 447 | */ |
| 448 | max_sample_delay_in_ns = |
| 449 | (nfc->max_sample_delay_factor * clock_period_in_ns) >> |
| 450 | dll_delay_shift; |
| 451 | |
| 452 | /* |
| 453 | * Check if the implied sample delay larger than the NFC |
| 454 | * actually allows. |
| 455 | */ |
| 456 | if (max_sample_delay_in_ns > nfc->max_dll_delay_in_ns) |
| 457 | max_sample_delay_in_ns = nfc->max_dll_delay_in_ns; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Check if improved timing information is available. If not, we have to |
| 462 | * use a less-sophisticated algorithm. |
| 463 | */ |
| 464 | if (!improved_timing_is_available) { |
| 465 | /* |
| 466 | * Fold the read setup time required by the NFC into the ideal |
| 467 | * sample delay. |
| 468 | */ |
| 469 | ideal_sample_delay_in_ns = target.gpmi_sample_delay_in_ns + |
| 470 | nfc->internal_data_setup_in_ns; |
| 471 | |
| 472 | /* |
| 473 | * The ideal sample delay may be greater than the maximum |
| 474 | * allowed by the NFC. If so, we can trade off sample delay time |
| 475 | * for more data setup time. |
| 476 | * |
| 477 | * In each iteration of the following loop, we add a cycle to |
| 478 | * the data setup time and subtract a corresponding amount from |
| 479 | * the sample delay until we've satisified the constraints or |
| 480 | * can't do any better. |
| 481 | */ |
| 482 | while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) && |
| 483 | (data_setup_in_cycles < nfc->max_data_setup_cycles)) { |
| 484 | |
| 485 | data_setup_in_cycles++; |
| 486 | ideal_sample_delay_in_ns -= clock_period_in_ns; |
| 487 | |
| 488 | if (ideal_sample_delay_in_ns < 0) |
| 489 | ideal_sample_delay_in_ns = 0; |
| 490 | |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Compute the sample delay factor that corresponds most closely |
| 495 | * to the ideal sample delay. If the result is too large for the |
| 496 | * NFC, use the maximum value. |
| 497 | * |
| 498 | * Notice that we use the ns_to_cycles function to compute the |
| 499 | * sample delay factor. We do this because the form of the |
| 500 | * computation is the same as that for calculating cycles. |
| 501 | */ |
| 502 | sample_delay_factor = |
| 503 | ns_to_cycles( |
| 504 | ideal_sample_delay_in_ns << dll_delay_shift, |
| 505 | clock_period_in_ns, 0); |
| 506 | |
| 507 | if (sample_delay_factor > nfc->max_sample_delay_factor) |
| 508 | sample_delay_factor = nfc->max_sample_delay_factor; |
| 509 | |
| 510 | /* Skip to the part where we return our results. */ |
| 511 | goto return_results; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * If control arrives here, we have more detailed timing information, |
| 516 | * so we can use a better algorithm. |
| 517 | */ |
| 518 | |
| 519 | /* |
| 520 | * Fold the read setup time required by the NFC into the maximum |
| 521 | * propagation delay. |
| 522 | */ |
| 523 | max_prop_delay_in_ns += nfc->internal_data_setup_in_ns; |
| 524 | |
| 525 | /* |
| 526 | * Earlier, we computed the number of clock cycles required to satisfy |
| 527 | * the data setup time. Now, we need to know the actual nanoseconds. |
| 528 | */ |
| 529 | data_setup_in_ns = clock_period_in_ns * data_setup_in_cycles; |
| 530 | |
| 531 | /* |
| 532 | * Compute tEYE, the width of the data eye when reading from the NAND |
| 533 | * Flash. The eye width is fundamentally determined by the data setup |
| 534 | * time, perturbed by propagation delays and some characteristics of the |
| 535 | * NAND Flash device. |
| 536 | * |
| 537 | * start of the eye = max_prop_delay + tREA |
| 538 | * end of the eye = min_prop_delay + tRHOH + data_setup |
| 539 | */ |
| 540 | tEYE = (int)min_prop_delay_in_ns + (int)target.tRHOH_in_ns + |
| 541 | (int)data_setup_in_ns; |
| 542 | |
| 543 | tEYE -= (int)max_prop_delay_in_ns + (int)target.tREA_in_ns; |
| 544 | |
| 545 | /* |
| 546 | * The eye must be open. If it's not, we can try to open it by |
| 547 | * increasing its main forcer, the data setup time. |
| 548 | * |
| 549 | * In each iteration of the following loop, we increase the data setup |
| 550 | * time by a single clock cycle. We do this until either the eye is |
| 551 | * open or we run into NFC limits. |
| 552 | */ |
| 553 | while ((tEYE <= 0) && |
| 554 | (data_setup_in_cycles < nfc->max_data_setup_cycles)) { |
| 555 | /* Give a cycle to data setup. */ |
| 556 | data_setup_in_cycles++; |
| 557 | /* Synchronize the data setup time with the cycles. */ |
| 558 | data_setup_in_ns += clock_period_in_ns; |
| 559 | /* Adjust tEYE accordingly. */ |
| 560 | tEYE += clock_period_in_ns; |
| 561 | } |
| 562 | |
| 563 | /* |
| 564 | * When control arrives here, the eye is open. The ideal time to sample |
| 565 | * the data is in the center of the eye: |
| 566 | * |
| 567 | * end of the eye + start of the eye |
| 568 | * --------------------------------- - data_setup |
| 569 | * 2 |
| 570 | * |
| 571 | * After some algebra, this simplifies to the code immediately below. |
| 572 | */ |
| 573 | ideal_sample_delay_in_ns = |
| 574 | ((int)max_prop_delay_in_ns + |
| 575 | (int)target.tREA_in_ns + |
| 576 | (int)min_prop_delay_in_ns + |
| 577 | (int)target.tRHOH_in_ns - |
| 578 | (int)data_setup_in_ns) >> 1; |
| 579 | |
| 580 | /* |
| 581 | * The following figure illustrates some aspects of a NAND Flash read: |
| 582 | * |
| 583 | * |
| 584 | * __ _____________________________________ |
| 585 | * RDN \_________________/ |
| 586 | * |
| 587 | * <---- tEYE -----> |
| 588 | * /-----------------\ |
| 589 | * Read Data ----------------------------< >--------- |
| 590 | * \-----------------/ |
| 591 | * ^ ^ ^ ^ |
| 592 | * | | | | |
| 593 | * |<--Data Setup -->|<--Delay Time -->| | |
| 594 | * | | | | |
| 595 | * | | | |
| 596 | * | |<-- Quantized Delay Time -->| |
| 597 | * | | | |
| 598 | * |
| 599 | * |
| 600 | * We have some issues we must now address: |
| 601 | * |
| 602 | * (1) The *ideal* sample delay time must not be negative. If it is, we |
| 603 | * jam it to zero. |
| 604 | * |
| 605 | * (2) The *ideal* sample delay time must not be greater than that |
| 606 | * allowed by the NFC. If it is, we can increase the data setup |
| 607 | * time, which will reduce the delay between the end of the data |
| 608 | * setup and the center of the eye. It will also make the eye |
| 609 | * larger, which might help with the next issue... |
| 610 | * |
| 611 | * (3) The *quantized* sample delay time must not fall either before the |
| 612 | * eye opens or after it closes (the latter is the problem |
| 613 | * illustrated in the above figure). |
| 614 | */ |
| 615 | |
| 616 | /* Jam a negative ideal sample delay to zero. */ |
| 617 | if (ideal_sample_delay_in_ns < 0) |
| 618 | ideal_sample_delay_in_ns = 0; |
| 619 | |
| 620 | /* |
| 621 | * Extend the data setup as needed to reduce the ideal sample delay |
| 622 | * below the maximum permitted by the NFC. |
| 623 | */ |
| 624 | while ((ideal_sample_delay_in_ns > max_sample_delay_in_ns) && |
| 625 | (data_setup_in_cycles < nfc->max_data_setup_cycles)) { |
| 626 | |
| 627 | /* Give a cycle to data setup. */ |
| 628 | data_setup_in_cycles++; |
| 629 | /* Synchronize the data setup time with the cycles. */ |
| 630 | data_setup_in_ns += clock_period_in_ns; |
| 631 | /* Adjust tEYE accordingly. */ |
| 632 | tEYE += clock_period_in_ns; |
| 633 | |
| 634 | /* |
| 635 | * Decrease the ideal sample delay by one half cycle, to keep it |
| 636 | * in the middle of the eye. |
| 637 | */ |
| 638 | ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1); |
| 639 | |
| 640 | /* Jam a negative ideal sample delay to zero. */ |
| 641 | if (ideal_sample_delay_in_ns < 0) |
| 642 | ideal_sample_delay_in_ns = 0; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Compute the sample delay factor that corresponds to the ideal sample |
| 647 | * delay. If the result is too large, then use the maximum allowed |
| 648 | * value. |
| 649 | * |
| 650 | * Notice that we use the ns_to_cycles function to compute the sample |
| 651 | * delay factor. We do this because the form of the computation is the |
| 652 | * same as that for calculating cycles. |
| 653 | */ |
| 654 | sample_delay_factor = |
| 655 | ns_to_cycles(ideal_sample_delay_in_ns << dll_delay_shift, |
| 656 | clock_period_in_ns, 0); |
| 657 | |
| 658 | if (sample_delay_factor > nfc->max_sample_delay_factor) |
| 659 | sample_delay_factor = nfc->max_sample_delay_factor; |
| 660 | |
| 661 | /* |
| 662 | * These macros conveniently encapsulate a computation we'll use to |
| 663 | * continuously evaluate whether or not the data sample delay is inside |
| 664 | * the eye. |
| 665 | */ |
| 666 | #define IDEAL_DELAY ((int) ideal_sample_delay_in_ns) |
| 667 | |
| 668 | #define QUANTIZED_DELAY \ |
| 669 | ((int) ((sample_delay_factor * clock_period_in_ns) >> \ |
| 670 | dll_delay_shift)) |
| 671 | |
| 672 | #define DELAY_ERROR (abs(QUANTIZED_DELAY - IDEAL_DELAY)) |
| 673 | |
| 674 | #define SAMPLE_IS_NOT_WITHIN_THE_EYE (DELAY_ERROR > (tEYE >> 1)) |
| 675 | |
| 676 | /* |
| 677 | * While the quantized sample time falls outside the eye, reduce the |
| 678 | * sample delay or extend the data setup to move the sampling point back |
| 679 | * toward the eye. Do not allow the number of data setup cycles to |
| 680 | * exceed the maximum allowed by the NFC. |
| 681 | */ |
| 682 | while (SAMPLE_IS_NOT_WITHIN_THE_EYE && |
| 683 | (data_setup_in_cycles < nfc->max_data_setup_cycles)) { |
| 684 | /* |
| 685 | * If control arrives here, the quantized sample delay falls |
| 686 | * outside the eye. Check if it's before the eye opens, or after |
| 687 | * the eye closes. |
| 688 | */ |
| 689 | if (QUANTIZED_DELAY > IDEAL_DELAY) { |
| 690 | /* |
| 691 | * If control arrives here, the quantized sample delay |
| 692 | * falls after the eye closes. Decrease the quantized |
| 693 | * delay time and then go back to re-evaluate. |
| 694 | */ |
| 695 | if (sample_delay_factor != 0) |
| 696 | sample_delay_factor--; |
| 697 | continue; |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * If control arrives here, the quantized sample delay falls |
| 702 | * before the eye opens. Shift the sample point by increasing |
| 703 | * data setup time. This will also make the eye larger. |
| 704 | */ |
| 705 | |
| 706 | /* Give a cycle to data setup. */ |
| 707 | data_setup_in_cycles++; |
| 708 | /* Synchronize the data setup time with the cycles. */ |
| 709 | data_setup_in_ns += clock_period_in_ns; |
| 710 | /* Adjust tEYE accordingly. */ |
| 711 | tEYE += clock_period_in_ns; |
| 712 | |
| 713 | /* |
| 714 | * Decrease the ideal sample delay by one half cycle, to keep it |
| 715 | * in the middle of the eye. |
| 716 | */ |
| 717 | ideal_sample_delay_in_ns -= (clock_period_in_ns >> 1); |
| 718 | |
| 719 | /* ...and one less period for the delay time. */ |
| 720 | ideal_sample_delay_in_ns -= clock_period_in_ns; |
| 721 | |
| 722 | /* Jam a negative ideal sample delay to zero. */ |
| 723 | if (ideal_sample_delay_in_ns < 0) |
| 724 | ideal_sample_delay_in_ns = 0; |
| 725 | |
| 726 | /* |
| 727 | * We have a new ideal sample delay, so re-compute the quantized |
| 728 | * delay. |
| 729 | */ |
| 730 | sample_delay_factor = |
| 731 | ns_to_cycles( |
| 732 | ideal_sample_delay_in_ns << dll_delay_shift, |
| 733 | clock_period_in_ns, 0); |
| 734 | |
| 735 | if (sample_delay_factor > nfc->max_sample_delay_factor) |
| 736 | sample_delay_factor = nfc->max_sample_delay_factor; |
| 737 | } |
| 738 | |
| 739 | /* Control arrives here when we're ready to return our results. */ |
| 740 | return_results: |
| 741 | hw->data_setup_in_cycles = data_setup_in_cycles; |
| 742 | hw->data_hold_in_cycles = data_hold_in_cycles; |
| 743 | hw->address_setup_in_cycles = address_setup_in_cycles; |
| 744 | hw->use_half_periods = dll_use_half_periods; |
| 745 | hw->sample_delay_factor = sample_delay_factor; |
Huang Shijie | ddab383 | 2012-09-13 14:57:54 +0800 | [diff] [blame] | 746 | hw->device_busy_timeout = GPMI_DEFAULT_BUSY_TIMEOUT; |
Huang Shijie | d37e02d | 2012-09-13 14:57:56 +0800 | [diff] [blame] | 747 | hw->wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_4_TO_8NS; |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 748 | |
| 749 | /* Return success. */ |
| 750 | return 0; |
| 751 | } |
| 752 | |
Huang Shijie | 995fbbf | 2012-09-13 14:57:59 +0800 | [diff] [blame] | 753 | /* |
| 754 | * <1> Firstly, we should know what's the GPMI-clock means. |
| 755 | * The GPMI-clock is the internal clock in the gpmi nand controller. |
| 756 | * If you set 100MHz to gpmi nand controller, the GPMI-clock's period |
| 757 | * is 10ns. Mark the GPMI-clock's period as GPMI-clock-period. |
| 758 | * |
| 759 | * <2> Secondly, we should know what's the frequency on the nand chip pins. |
| 760 | * The frequency on the nand chip pins is derived from the GPMI-clock. |
| 761 | * We can get it from the following equation: |
| 762 | * |
| 763 | * F = G / (DS + DH) |
| 764 | * |
| 765 | * F : the frequency on the nand chip pins. |
| 766 | * G : the GPMI clock, such as 100MHz. |
| 767 | * DS : GPMI_HW_GPMI_TIMING0:DATA_SETUP |
| 768 | * DH : GPMI_HW_GPMI_TIMING0:DATA_HOLD |
| 769 | * |
| 770 | * <3> Thirdly, when the frequency on the nand chip pins is above 33MHz, |
| 771 | * the nand EDO(extended Data Out) timing could be applied. |
| 772 | * The GPMI implements a feedback read strobe to sample the read data. |
| 773 | * The feedback read strobe can be delayed to support the nand EDO timing |
| 774 | * where the read strobe may deasserts before the read data is valid, and |
| 775 | * read data is valid for some time after read strobe. |
| 776 | * |
| 777 | * The following figure illustrates some aspects of a NAND Flash read: |
| 778 | * |
| 779 | * |<---tREA---->| |
| 780 | * | | |
| 781 | * | | | |
| 782 | * |<--tRP-->| | |
| 783 | * | | | |
| 784 | * __ ___|__________________________________ |
| 785 | * RDN \________/ | |
| 786 | * | |
| 787 | * /---------\ |
| 788 | * Read Data --------------< >--------- |
| 789 | * \---------/ |
| 790 | * | | |
| 791 | * |<-D->| |
| 792 | * FeedbackRDN ________ ____________ |
| 793 | * \___________/ |
| 794 | * |
| 795 | * D stands for delay, set in the HW_GPMI_CTRL1:RDN_DELAY. |
| 796 | * |
| 797 | * |
| 798 | * <4> Now, we begin to describe how to compute the right RDN_DELAY. |
| 799 | * |
| 800 | * 4.1) From the aspect of the nand chip pins: |
| 801 | * Delay = (tREA + C - tRP) {1} |
| 802 | * |
| 803 | * tREA : the maximum read access time. From the ONFI nand standards, |
| 804 | * we know that tREA is 16ns in mode 5, tREA is 20ns is mode 4. |
| 805 | * Please check it in : www.onfi.org |
| 806 | * C : a constant for adjust the delay. default is 4. |
| 807 | * tRP : the read pulse width. |
| 808 | * Specified by the HW_GPMI_TIMING0:DATA_SETUP: |
| 809 | * tRP = (GPMI-clock-period) * DATA_SETUP |
| 810 | * |
| 811 | * 4.2) From the aspect of the GPMI nand controller: |
| 812 | * Delay = RDN_DELAY * 0.125 * RP {2} |
| 813 | * |
| 814 | * RP : the DLL reference period. |
| 815 | * if (GPMI-clock-period > DLL_THRETHOLD) |
| 816 | * RP = GPMI-clock-period / 2; |
| 817 | * else |
| 818 | * RP = GPMI-clock-period; |
| 819 | * |
| 820 | * Set the HW_GPMI_CTRL1:HALF_PERIOD if GPMI-clock-period |
| 821 | * is greater DLL_THRETHOLD. In other SOCs, the DLL_THRETHOLD |
| 822 | * is 16ns, but in mx6q, we use 12ns. |
| 823 | * |
| 824 | * 4.3) since {1} equals {2}, we get: |
| 825 | * |
| 826 | * (tREA + 4 - tRP) * 8 |
| 827 | * RDN_DELAY = --------------------- {3} |
| 828 | * RP |
| 829 | * |
| 830 | * 4.4) We only support the fastest asynchronous mode of ONFI nand. |
| 831 | * For some ONFI nand, the mode 4 is the fastest mode; |
| 832 | * while for some ONFI nand, the mode 5 is the fastest mode. |
| 833 | * So we only support the mode 4 and mode 5. It is no need to |
| 834 | * support other modes. |
| 835 | */ |
| 836 | static void gpmi_compute_edo_timing(struct gpmi_nand_data *this, |
| 837 | struct gpmi_nfc_hardware_timing *hw) |
| 838 | { |
| 839 | struct resources *r = &this->resources; |
| 840 | unsigned long rate = clk_get_rate(r->clock[0]); |
| 841 | int mode = this->timing_mode; |
| 842 | int dll_threshold = 16; /* in ns */ |
| 843 | unsigned long delay; |
| 844 | unsigned long clk_period; |
| 845 | int t_rea; |
| 846 | int c = 4; |
| 847 | int t_rp; |
| 848 | int rp; |
| 849 | |
| 850 | /* |
| 851 | * [1] for GPMI_HW_GPMI_TIMING0: |
| 852 | * The async mode requires 40MHz for mode 4, 50MHz for mode 5. |
| 853 | * The GPMI can support 100MHz at most. So if we want to |
| 854 | * get the 40MHz or 50MHz, we have to set DS=1, DH=1. |
| 855 | * Set the ADDRESS_SETUP to 0 in mode 4. |
| 856 | */ |
| 857 | hw->data_setup_in_cycles = 1; |
| 858 | hw->data_hold_in_cycles = 1; |
| 859 | hw->address_setup_in_cycles = ((mode == 5) ? 1 : 0); |
| 860 | |
| 861 | /* [2] for GPMI_HW_GPMI_TIMING1 */ |
| 862 | hw->device_busy_timeout = 0x9000; |
| 863 | |
| 864 | /* [3] for GPMI_HW_GPMI_CTRL1 */ |
| 865 | hw->wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY; |
| 866 | |
| 867 | if (GPMI_IS_MX6Q(this)) |
| 868 | dll_threshold = 12; |
| 869 | |
| 870 | /* |
| 871 | * Enlarge 10 times for the numerator and denominator in {3}. |
| 872 | * This make us to get more accurate result. |
| 873 | */ |
| 874 | clk_period = NSEC_PER_SEC / (rate / 10); |
| 875 | dll_threshold *= 10; |
| 876 | t_rea = ((mode == 5) ? 16 : 20) * 10; |
| 877 | c *= 10; |
| 878 | |
| 879 | t_rp = clk_period * 1; /* DATA_SETUP is 1 */ |
| 880 | |
| 881 | if (clk_period > dll_threshold) { |
| 882 | hw->use_half_periods = 1; |
| 883 | rp = clk_period / 2; |
| 884 | } else { |
| 885 | hw->use_half_periods = 0; |
| 886 | rp = clk_period; |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * Multiply the numerator with 10, we could do a round off: |
| 891 | * 7.8 round up to 8; 7.4 round down to 7. |
| 892 | */ |
| 893 | delay = (((t_rea + c - t_rp) * 8) * 10) / rp; |
| 894 | delay = (delay + 5) / 10; |
| 895 | |
| 896 | hw->sample_delay_factor = delay; |
| 897 | } |
| 898 | |
| 899 | static int enable_edo_mode(struct gpmi_nand_data *this, int mode) |
| 900 | { |
| 901 | struct resources *r = &this->resources; |
| 902 | struct nand_chip *nand = &this->nand; |
| 903 | struct mtd_info *mtd = &this->mtd; |
| 904 | uint8_t feature[ONFI_SUBFEATURE_PARAM_LEN] = {}; |
| 905 | unsigned long rate; |
| 906 | int ret; |
| 907 | |
| 908 | nand->select_chip(mtd, 0); |
| 909 | |
| 910 | /* [1] send SET FEATURE commond to NAND */ |
| 911 | feature[0] = mode; |
| 912 | ret = nand->onfi_set_features(mtd, nand, |
| 913 | ONFI_FEATURE_ADDR_TIMING_MODE, feature); |
| 914 | if (ret) |
| 915 | goto err_out; |
| 916 | |
| 917 | /* [2] send GET FEATURE command to double-check the timing mode */ |
| 918 | memset(feature, 0, ONFI_SUBFEATURE_PARAM_LEN); |
| 919 | ret = nand->onfi_get_features(mtd, nand, |
| 920 | ONFI_FEATURE_ADDR_TIMING_MODE, feature); |
| 921 | if (ret || feature[0] != mode) |
| 922 | goto err_out; |
| 923 | |
| 924 | nand->select_chip(mtd, -1); |
| 925 | |
| 926 | /* [3] set the main IO clock, 100MHz for mode 5, 80MHz for mode 4. */ |
| 927 | rate = (mode == 5) ? 100000000 : 80000000; |
| 928 | clk_set_rate(r->clock[0], rate); |
| 929 | |
Huang Shijie | 9c95f11 | 2012-09-13 14:58:00 +0800 | [diff] [blame] | 930 | /* Let the gpmi_begin() re-compute the timing again. */ |
| 931 | this->flags &= ~GPMI_TIMING_INIT_OK; |
| 932 | |
Huang Shijie | 995fbbf | 2012-09-13 14:57:59 +0800 | [diff] [blame] | 933 | this->flags |= GPMI_ASYNC_EDO_ENABLED; |
| 934 | this->timing_mode = mode; |
| 935 | dev_info(this->dev, "enable the asynchronous EDO mode %d\n", mode); |
| 936 | return 0; |
| 937 | |
| 938 | err_out: |
| 939 | nand->select_chip(mtd, -1); |
| 940 | dev_err(this->dev, "mode:%d ,failed in set feature.\n", mode); |
| 941 | return -EINVAL; |
| 942 | } |
| 943 | |
| 944 | int gpmi_extra_init(struct gpmi_nand_data *this) |
| 945 | { |
| 946 | struct nand_chip *chip = &this->nand; |
| 947 | |
| 948 | /* Enable the asynchronous EDO feature. */ |
| 949 | if (GPMI_IS_MX6Q(this) && chip->onfi_version) { |
| 950 | int mode = onfi_get_async_timing_mode(chip); |
| 951 | |
| 952 | /* We only support the timing mode 4 and mode 5. */ |
| 953 | if (mode & ONFI_TIMING_MODE_5) |
| 954 | mode = 5; |
| 955 | else if (mode & ONFI_TIMING_MODE_4) |
| 956 | mode = 4; |
| 957 | else |
| 958 | return 0; |
| 959 | |
| 960 | return enable_edo_mode(this, mode); |
| 961 | } |
| 962 | return 0; |
| 963 | } |
| 964 | |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 965 | /* Begin the I/O */ |
| 966 | void gpmi_begin(struct gpmi_nand_data *this) |
| 967 | { |
| 968 | struct resources *r = &this->resources; |
Huang Shijie | 513d57e | 2012-07-17 14:14:02 +0800 | [diff] [blame] | 969 | void __iomem *gpmi_regs = r->gpmi_regs; |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 970 | unsigned int clock_period_in_ns; |
| 971 | uint32_t reg; |
| 972 | unsigned int dll_wait_time_in_us; |
| 973 | struct gpmi_nfc_hardware_timing hw; |
| 974 | int ret; |
| 975 | |
| 976 | /* Enable the clock. */ |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 977 | ret = gpmi_enable_clk(this); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 978 | if (ret) { |
| 979 | pr_err("We failed in enable the clk\n"); |
| 980 | goto err_out; |
| 981 | } |
| 982 | |
Huang Shijie | 9c95f11 | 2012-09-13 14:58:00 +0800 | [diff] [blame] | 983 | /* Only initialize the timing once */ |
| 984 | if (this->flags & GPMI_TIMING_INIT_OK) |
| 985 | return; |
| 986 | this->flags |= GPMI_TIMING_INIT_OK; |
| 987 | |
Huang Shijie | 995fbbf | 2012-09-13 14:57:59 +0800 | [diff] [blame] | 988 | if (this->flags & GPMI_ASYNC_EDO_ENABLED) |
| 989 | gpmi_compute_edo_timing(this, &hw); |
| 990 | else |
| 991 | gpmi_nfc_compute_hardware_timing(this, &hw); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 992 | |
Huang Shijie | ddab383 | 2012-09-13 14:57:54 +0800 | [diff] [blame] | 993 | /* [1] Set HW_GPMI_TIMING0 */ |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 994 | reg = BF_GPMI_TIMING0_ADDRESS_SETUP(hw.address_setup_in_cycles) | |
| 995 | BF_GPMI_TIMING0_DATA_HOLD(hw.data_hold_in_cycles) | |
| 996 | BF_GPMI_TIMING0_DATA_SETUP(hw.data_setup_in_cycles) ; |
| 997 | |
| 998 | writel(reg, gpmi_regs + HW_GPMI_TIMING0); |
| 999 | |
Huang Shijie | ddab383 | 2012-09-13 14:57:54 +0800 | [diff] [blame] | 1000 | /* [2] Set HW_GPMI_TIMING1 */ |
| 1001 | writel(BF_GPMI_TIMING1_BUSY_TIMEOUT(hw.device_busy_timeout), |
| 1002 | gpmi_regs + HW_GPMI_TIMING1); |
| 1003 | |
| 1004 | /* [3] The following code is to set the HW_GPMI_CTRL1. */ |
| 1005 | |
Huang Shijie | d37e02d | 2012-09-13 14:57:56 +0800 | [diff] [blame] | 1006 | /* Set the WRN_DLY_SEL */ |
| 1007 | writel(BM_GPMI_CTRL1_WRN_DLY_SEL, gpmi_regs + HW_GPMI_CTRL1_CLR); |
| 1008 | writel(BF_GPMI_CTRL1_WRN_DLY_SEL(hw.wrn_dly_sel), |
| 1009 | gpmi_regs + HW_GPMI_CTRL1_SET); |
| 1010 | |
Huang Shijie | ddab383 | 2012-09-13 14:57:54 +0800 | [diff] [blame] | 1011 | /* DLL_ENABLE must be set to 0 when setting RDN_DELAY or HALF_PERIOD. */ |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1012 | writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_CLR); |
| 1013 | |
| 1014 | /* Clear out the DLL control fields. */ |
Huang Shijie | c50d35a | 2012-09-13 14:57:57 +0800 | [diff] [blame] | 1015 | reg = BM_GPMI_CTRL1_RDN_DELAY | BM_GPMI_CTRL1_HALF_PERIOD; |
| 1016 | writel(reg, gpmi_regs + HW_GPMI_CTRL1_CLR); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1017 | |
| 1018 | /* If no sample delay is called for, return immediately. */ |
| 1019 | if (!hw.sample_delay_factor) |
| 1020 | return; |
| 1021 | |
Huang Shijie | c50d35a | 2012-09-13 14:57:57 +0800 | [diff] [blame] | 1022 | /* Set RDN_DELAY or HALF_PERIOD. */ |
| 1023 | reg = ((hw.use_half_periods) ? BM_GPMI_CTRL1_HALF_PERIOD : 0) |
| 1024 | | BF_GPMI_CTRL1_RDN_DELAY(hw.sample_delay_factor); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1025 | |
Huang Shijie | c50d35a | 2012-09-13 14:57:57 +0800 | [diff] [blame] | 1026 | writel(reg, gpmi_regs + HW_GPMI_CTRL1_SET); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1027 | |
Huang Shijie | c50d35a | 2012-09-13 14:57:57 +0800 | [diff] [blame] | 1028 | /* At last, we enable the DLL. */ |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1029 | writel(BM_GPMI_CTRL1_DLL_ENABLE, gpmi_regs + HW_GPMI_CTRL1_SET); |
| 1030 | |
| 1031 | /* |
| 1032 | * After we enable the GPMI DLL, we have to wait 64 clock cycles before |
Huang Shijie | c50d35a | 2012-09-13 14:57:57 +0800 | [diff] [blame] | 1033 | * we can use the GPMI. Calculate the amount of time we need to wait, |
| 1034 | * in microseconds. |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1035 | */ |
Huang Shijie | ae70ba2d | 2012-09-13 14:57:55 +0800 | [diff] [blame] | 1036 | clock_period_in_ns = NSEC_PER_SEC / clk_get_rate(r->clock[0]); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1037 | dll_wait_time_in_us = (clock_period_in_ns * 64) / 1000; |
| 1038 | |
| 1039 | if (!dll_wait_time_in_us) |
| 1040 | dll_wait_time_in_us = 1; |
| 1041 | |
| 1042 | /* Wait for the DLL to settle. */ |
| 1043 | udelay(dll_wait_time_in_us); |
| 1044 | |
| 1045 | err_out: |
| 1046 | return; |
| 1047 | } |
| 1048 | |
| 1049 | void gpmi_end(struct gpmi_nand_data *this) |
| 1050 | { |
Huang Shijie | ff50617 | 2012-07-02 21:39:32 -0400 | [diff] [blame] | 1051 | gpmi_disable_clk(this); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | /* Clears a BCH interrupt. */ |
| 1055 | void gpmi_clear_bch(struct gpmi_nand_data *this) |
| 1056 | { |
| 1057 | struct resources *r = &this->resources; |
| 1058 | writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR); |
| 1059 | } |
| 1060 | |
| 1061 | /* Returns the Ready/Busy status of the given chip. */ |
| 1062 | int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip) |
| 1063 | { |
| 1064 | struct resources *r = &this->resources; |
| 1065 | uint32_t mask = 0; |
| 1066 | uint32_t reg = 0; |
| 1067 | |
| 1068 | if (GPMI_IS_MX23(this)) { |
| 1069 | mask = MX23_BM_GPMI_DEBUG_READY0 << chip; |
| 1070 | reg = readl(r->gpmi_regs + HW_GPMI_DEBUG); |
Huang Shijie | 9013bb4 | 2012-05-04 21:42:06 -0400 | [diff] [blame] | 1071 | } else if (GPMI_IS_MX28(this) || GPMI_IS_MX6Q(this)) { |
| 1072 | /* MX28 shares the same R/B register as MX6Q. */ |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1073 | mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip); |
| 1074 | reg = readl(r->gpmi_regs + HW_GPMI_STAT); |
| 1075 | } else |
| 1076 | pr_err("unknow arch.\n"); |
| 1077 | return reg & mask; |
| 1078 | } |
| 1079 | |
| 1080 | static inline void set_dma_type(struct gpmi_nand_data *this, |
| 1081 | enum dma_ops_type type) |
| 1082 | { |
| 1083 | this->last_dma_type = this->dma_type; |
| 1084 | this->dma_type = type; |
| 1085 | } |
| 1086 | |
| 1087 | int gpmi_send_command(struct gpmi_nand_data *this) |
| 1088 | { |
| 1089 | struct dma_chan *channel = get_dma_chan(this); |
| 1090 | struct dma_async_tx_descriptor *desc; |
| 1091 | struct scatterlist *sgl; |
| 1092 | int chip = this->current_chip; |
| 1093 | u32 pio[3]; |
| 1094 | |
| 1095 | /* [1] send out the PIO words */ |
| 1096 | pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE) |
| 1097 | | BM_GPMI_CTRL0_WORD_LENGTH |
| 1098 | | BF_GPMI_CTRL0_CS(chip, this) |
| 1099 | | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) |
| 1100 | | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE) |
| 1101 | | BM_GPMI_CTRL0_ADDRESS_INCREMENT |
| 1102 | | BF_GPMI_CTRL0_XFER_COUNT(this->command_length); |
| 1103 | pio[1] = pio[2] = 0; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1104 | desc = dmaengine_prep_slave_sg(channel, |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1105 | (struct scatterlist *)pio, |
Shawn Guo | 0ef7e20 | 2011-12-13 23:48:06 +0800 | [diff] [blame] | 1106 | ARRAY_SIZE(pio), DMA_TRANS_NONE, 0); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1107 | if (!desc) { |
| 1108 | pr_err("step 1 error\n"); |
| 1109 | return -1; |
| 1110 | } |
| 1111 | |
| 1112 | /* [2] send out the COMMAND + ADDRESS string stored in @buffer */ |
| 1113 | sgl = &this->cmd_sgl; |
| 1114 | |
| 1115 | sg_init_one(sgl, this->cmd_buffer, this->command_length); |
| 1116 | dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE); |
Linus Torvalds | 623ff77 | 2012-03-30 17:31:56 -0700 | [diff] [blame] | 1117 | desc = dmaengine_prep_slave_sg(channel, |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 1118 | sgl, 1, DMA_MEM_TO_DEV, |
| 1119 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 1120 | |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1121 | if (!desc) { |
| 1122 | pr_err("step 2 error\n"); |
| 1123 | return -1; |
| 1124 | } |
| 1125 | |
| 1126 | /* [3] submit the DMA */ |
| 1127 | set_dma_type(this, DMA_FOR_COMMAND); |
| 1128 | return start_dma_without_bch_irq(this, desc); |
| 1129 | } |
| 1130 | |
| 1131 | int gpmi_send_data(struct gpmi_nand_data *this) |
| 1132 | { |
| 1133 | struct dma_async_tx_descriptor *desc; |
| 1134 | struct dma_chan *channel = get_dma_chan(this); |
| 1135 | int chip = this->current_chip; |
| 1136 | uint32_t command_mode; |
| 1137 | uint32_t address; |
| 1138 | u32 pio[2]; |
| 1139 | |
| 1140 | /* [1] PIO */ |
| 1141 | command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE; |
| 1142 | address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; |
| 1143 | |
| 1144 | pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) |
| 1145 | | BM_GPMI_CTRL0_WORD_LENGTH |
| 1146 | | BF_GPMI_CTRL0_CS(chip, this) |
| 1147 | | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) |
| 1148 | | BF_GPMI_CTRL0_ADDRESS(address) |
| 1149 | | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len); |
| 1150 | pio[1] = 0; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1151 | desc = dmaengine_prep_slave_sg(channel, (struct scatterlist *)pio, |
Shawn Guo | 0ef7e20 | 2011-12-13 23:48:06 +0800 | [diff] [blame] | 1152 | ARRAY_SIZE(pio), DMA_TRANS_NONE, 0); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1153 | if (!desc) { |
| 1154 | pr_err("step 1 error\n"); |
| 1155 | return -1; |
| 1156 | } |
| 1157 | |
| 1158 | /* [2] send DMA request */ |
| 1159 | prepare_data_dma(this, DMA_TO_DEVICE); |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1160 | desc = dmaengine_prep_slave_sg(channel, &this->data_sgl, |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 1161 | 1, DMA_MEM_TO_DEV, |
| 1162 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1163 | if (!desc) { |
| 1164 | pr_err("step 2 error\n"); |
| 1165 | return -1; |
| 1166 | } |
| 1167 | /* [3] submit the DMA */ |
| 1168 | set_dma_type(this, DMA_FOR_WRITE_DATA); |
| 1169 | return start_dma_without_bch_irq(this, desc); |
| 1170 | } |
| 1171 | |
| 1172 | int gpmi_read_data(struct gpmi_nand_data *this) |
| 1173 | { |
| 1174 | struct dma_async_tx_descriptor *desc; |
| 1175 | struct dma_chan *channel = get_dma_chan(this); |
| 1176 | int chip = this->current_chip; |
| 1177 | u32 pio[2]; |
| 1178 | |
| 1179 | /* [1] : send PIO */ |
| 1180 | pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ) |
| 1181 | | BM_GPMI_CTRL0_WORD_LENGTH |
| 1182 | | BF_GPMI_CTRL0_CS(chip, this) |
| 1183 | | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) |
| 1184 | | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA) |
| 1185 | | BF_GPMI_CTRL0_XFER_COUNT(this->upper_len); |
| 1186 | pio[1] = 0; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1187 | desc = dmaengine_prep_slave_sg(channel, |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1188 | (struct scatterlist *)pio, |
Shawn Guo | 0ef7e20 | 2011-12-13 23:48:06 +0800 | [diff] [blame] | 1189 | ARRAY_SIZE(pio), DMA_TRANS_NONE, 0); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1190 | if (!desc) { |
| 1191 | pr_err("step 1 error\n"); |
| 1192 | return -1; |
| 1193 | } |
| 1194 | |
| 1195 | /* [2] : send DMA request */ |
| 1196 | prepare_data_dma(this, DMA_FROM_DEVICE); |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1197 | desc = dmaengine_prep_slave_sg(channel, &this->data_sgl, |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 1198 | 1, DMA_DEV_TO_MEM, |
| 1199 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1200 | if (!desc) { |
| 1201 | pr_err("step 2 error\n"); |
| 1202 | return -1; |
| 1203 | } |
| 1204 | |
| 1205 | /* [3] : submit the DMA */ |
| 1206 | set_dma_type(this, DMA_FOR_READ_DATA); |
| 1207 | return start_dma_without_bch_irq(this, desc); |
| 1208 | } |
| 1209 | |
| 1210 | int gpmi_send_page(struct gpmi_nand_data *this, |
| 1211 | dma_addr_t payload, dma_addr_t auxiliary) |
| 1212 | { |
| 1213 | struct bch_geometry *geo = &this->bch_geometry; |
| 1214 | uint32_t command_mode; |
| 1215 | uint32_t address; |
| 1216 | uint32_t ecc_command; |
| 1217 | uint32_t buffer_mask; |
| 1218 | struct dma_async_tx_descriptor *desc; |
| 1219 | struct dma_chan *channel = get_dma_chan(this); |
| 1220 | int chip = this->current_chip; |
| 1221 | u32 pio[6]; |
| 1222 | |
| 1223 | /* A DMA descriptor that does an ECC page read. */ |
| 1224 | command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE; |
| 1225 | address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; |
| 1226 | ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE; |
| 1227 | buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE | |
| 1228 | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY; |
| 1229 | |
| 1230 | pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) |
| 1231 | | BM_GPMI_CTRL0_WORD_LENGTH |
| 1232 | | BF_GPMI_CTRL0_CS(chip, this) |
| 1233 | | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) |
| 1234 | | BF_GPMI_CTRL0_ADDRESS(address) |
| 1235 | | BF_GPMI_CTRL0_XFER_COUNT(0); |
| 1236 | pio[1] = 0; |
| 1237 | pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC |
| 1238 | | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command) |
| 1239 | | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask); |
| 1240 | pio[3] = geo->page_size; |
| 1241 | pio[4] = payload; |
| 1242 | pio[5] = auxiliary; |
| 1243 | |
Linus Torvalds | 623ff77 | 2012-03-30 17:31:56 -0700 | [diff] [blame] | 1244 | desc = dmaengine_prep_slave_sg(channel, |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1245 | (struct scatterlist *)pio, |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 1246 | ARRAY_SIZE(pio), DMA_TRANS_NONE, |
| 1247 | DMA_CTRL_ACK); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1248 | if (!desc) { |
| 1249 | pr_err("step 2 error\n"); |
| 1250 | return -1; |
| 1251 | } |
| 1252 | set_dma_type(this, DMA_FOR_WRITE_ECC_PAGE); |
| 1253 | return start_dma_with_bch_irq(this, desc); |
| 1254 | } |
| 1255 | |
| 1256 | int gpmi_read_page(struct gpmi_nand_data *this, |
| 1257 | dma_addr_t payload, dma_addr_t auxiliary) |
| 1258 | { |
| 1259 | struct bch_geometry *geo = &this->bch_geometry; |
| 1260 | uint32_t command_mode; |
| 1261 | uint32_t address; |
| 1262 | uint32_t ecc_command; |
| 1263 | uint32_t buffer_mask; |
| 1264 | struct dma_async_tx_descriptor *desc; |
| 1265 | struct dma_chan *channel = get_dma_chan(this); |
| 1266 | int chip = this->current_chip; |
| 1267 | u32 pio[6]; |
| 1268 | |
| 1269 | /* [1] Wait for the chip to report ready. */ |
| 1270 | command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY; |
| 1271 | address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; |
| 1272 | |
| 1273 | pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) |
| 1274 | | BM_GPMI_CTRL0_WORD_LENGTH |
| 1275 | | BF_GPMI_CTRL0_CS(chip, this) |
| 1276 | | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) |
| 1277 | | BF_GPMI_CTRL0_ADDRESS(address) |
| 1278 | | BF_GPMI_CTRL0_XFER_COUNT(0); |
| 1279 | pio[1] = 0; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1280 | desc = dmaengine_prep_slave_sg(channel, |
Shawn Guo | 0ef7e20 | 2011-12-13 23:48:06 +0800 | [diff] [blame] | 1281 | (struct scatterlist *)pio, 2, |
| 1282 | DMA_TRANS_NONE, 0); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1283 | if (!desc) { |
| 1284 | pr_err("step 1 error\n"); |
| 1285 | return -1; |
| 1286 | } |
| 1287 | |
| 1288 | /* [2] Enable the BCH block and read. */ |
| 1289 | command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ; |
| 1290 | address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; |
| 1291 | ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE; |
| 1292 | buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE |
| 1293 | | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY; |
| 1294 | |
| 1295 | pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) |
| 1296 | | BM_GPMI_CTRL0_WORD_LENGTH |
| 1297 | | BF_GPMI_CTRL0_CS(chip, this) |
| 1298 | | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) |
| 1299 | | BF_GPMI_CTRL0_ADDRESS(address) |
| 1300 | | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size); |
| 1301 | |
| 1302 | pio[1] = 0; |
| 1303 | pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC |
| 1304 | | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command) |
| 1305 | | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask); |
| 1306 | pio[3] = geo->page_size; |
| 1307 | pio[4] = payload; |
| 1308 | pio[5] = auxiliary; |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1309 | desc = dmaengine_prep_slave_sg(channel, |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1310 | (struct scatterlist *)pio, |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 1311 | ARRAY_SIZE(pio), DMA_TRANS_NONE, |
| 1312 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1313 | if (!desc) { |
| 1314 | pr_err("step 2 error\n"); |
| 1315 | return -1; |
| 1316 | } |
| 1317 | |
| 1318 | /* [3] Disable the BCH block */ |
| 1319 | command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY; |
| 1320 | address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; |
| 1321 | |
| 1322 | pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) |
| 1323 | | BM_GPMI_CTRL0_WORD_LENGTH |
| 1324 | | BF_GPMI_CTRL0_CS(chip, this) |
| 1325 | | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) |
| 1326 | | BF_GPMI_CTRL0_ADDRESS(address) |
| 1327 | | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size); |
| 1328 | pio[1] = 0; |
Huang Shijie | 09ef90d | 2012-03-12 10:22:18 +0800 | [diff] [blame] | 1329 | pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */ |
Alexandre Bounine | 1605282 | 2012-03-08 16:11:18 -0500 | [diff] [blame] | 1330 | desc = dmaengine_prep_slave_sg(channel, |
Huang Shijie | 09ef90d | 2012-03-12 10:22:18 +0800 | [diff] [blame] | 1331 | (struct scatterlist *)pio, 3, |
Huang Shijie | 921de86 | 2012-02-16 14:17:33 +0800 | [diff] [blame] | 1332 | DMA_TRANS_NONE, |
| 1333 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Huang Shijie | 45dfc1a | 2011-09-08 10:47:10 +0800 | [diff] [blame] | 1334 | if (!desc) { |
| 1335 | pr_err("step 3 error\n"); |
| 1336 | return -1; |
| 1337 | } |
| 1338 | |
| 1339 | /* [4] submit the DMA */ |
| 1340 | set_dma_type(this, DMA_FOR_READ_ECC_PAGE); |
| 1341 | return start_dma_with_bch_irq(this, desc); |
| 1342 | } |