1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
36
37
38
39static int clear_poll_bit(void __iomem *addr, u32 mask)
40{
41 int timeout = 0x400;
42
43
44 writel(mask, addr + MXS_CLR_ADDR);
45
46
47
48
49
50 udelay(1);
51
52
53 while ((readl(addr) & mask) && --timeout)
54 ;
55
56 return !timeout;
57}
58
59#define MODULE_CLKGATE (1 << 30)
60#define MODULE_SFTRST (1 << 31)
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78static int gpmi_reset_block(void __iomem *reset_addr, bool just_enable)
79{
80 int ret;
81 int timeout = 0x400;
82
83
84 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
85 if (unlikely(ret))
86 goto error;
87
88
89 writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
90
91 if (!just_enable) {
92
93 writel(MODULE_SFTRST, reset_addr + MXS_SET_ADDR);
94 udelay(1);
95
96
97 while ((!(readl(reset_addr) & MODULE_CLKGATE)) && --timeout)
98 ;
99 if (unlikely(!timeout))
100 goto error;
101 }
102
103
104 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
105 if (unlikely(ret))
106 goto error;
107
108
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
171
172
173 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
174 if (ret)
175 goto err_out;
176
177
178 writel(BM_GPMI_CTRL1_GPMI_MODE, r->gpmi_regs + HW_GPMI_CTRL1_CLR);
179
180
181 writel(BM_GPMI_CTRL1_ATA_IRQRDY_POLARITY,
182 r->gpmi_regs + HW_GPMI_CTRL1_SET);
183
184
185 writel(BM_GPMI_CTRL1_DEV_RESET, r->gpmi_regs + HW_GPMI_CTRL1_SET);
186
187
188 writel(BM_GPMI_CTRL1_BCH_MODE, r->gpmi_regs + HW_GPMI_CTRL1_SET);
189
190
191
192
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
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
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
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
277
278
279
280
281
282
283 ret = gpmi_reset_block(r->bch_regs, GPMI_IS_MX23(this));
284 if (ret)
285 goto err_out;
286
287
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
302 writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT);
303
304
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
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
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
408 hw->clk_rate = 80000000;
409 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
410 } else {
411
412 hw->clk_rate = 100000000;
413 wrn_dly_sel = BV_GPMI_CTRL1_WRN_DLY_SEL_NO_DELAY;
414 }
415
416
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
431
432
433
434
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
472
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
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
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
494 sdr = nand_get_sdr_timings(conf);
495 if (IS_ERR(sdr))
496 return PTR_ERR(sdr);
497
498
499 if (sdr->tRC_min <= 25000 && !GPMI_IS_MX6(this))
500 return -ENOTSUPP;
501
502
503 if (chipnr < 0)
504 return 0;
505
506
507 gpmi_nfc_compute_timings(this, sdr);
508
509 this->hw.must_apply_timings = true;
510
511 return 0;
512}
513
514
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
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
534
535
536 if (GPMI_IS_MX6(this))
537 chip = 0;
538
539
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
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
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
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
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
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
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
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
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
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
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
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
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
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;
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
788 set_dma_type(this, DMA_FOR_READ_ECC_PAGE);
789 return start_dma_with_bch_irq(this, desc);
790}
791
792
793
794
795
796
797
798
799
800
801
802
803
804
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
821
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
831
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
846 nbytes = nbits / 8;
847
848
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
876
877
878 if (nbytes)
879 memcpy(dst, src, nbytes);
880 } else {
881
882
883
884
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
893 dst += nbytes;
894 src += nbytes;
895
896
897
898
899
900 nbits %= 8;
901
902
903
904
905
906 if (!nbits && !bits_in_src_buffer)
907 return;
908
909
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
917
918
919
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
928
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
939 for (i = 0; i < nbytes; i++) {
940 dst[i] = src_buffer;
941 src_buffer >>= 8;
942 }
943}
944