Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mtd: rawnand: gpmi: Implement exec_op

The gpmi driver performance suffers from NAND operations being split
in multiple small DMA transfers. This has been forced by the NAND layer
in the former days, but now with exec_op we can use the controller as
intended.

With this patch gpmi_nfc_exec_op becomes the main entry point to NAND
operations. Here all instructions are collected and chained as separate
DMA transfers. In the end whole chain is fired and waited to be
finished. gpmi_nfc_exec_op only does the hardware operations, bad block
marker swapping and buffer scrambling is done by the callers. It's worth
noting that the nand_*_op functions always take the buffer lengths for
the data that the NAND chip actually transfers. When doing BCH we have
to calculate the net data size from the raw data size in some places.

This patch has been tested with 2048/64 and 2048/128 byte NAND on
i.MX6q. mtd_oobtest, mtd_subpagetest and mtd_speedtest run without
errors. nandbiterrs, nandpagetest and nandsubpagetest userspace tests
from mtdutils run without errors and UBIFS can successfully be mounted.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>

authored by

Sascha Hauer and committed by
Miquel Raynal
ef347c0c ceeeb99c

+444 -690
+3
drivers/dma/mxs-dma.c
··· 78 78 #define BM_CCW_COMMAND (3 << 0) 79 79 #define CCW_CHAIN (1 << 2) 80 80 #define CCW_IRQ (1 << 3) 81 + #define CCW_WAIT4RDY (1 << 5) 81 82 #define CCW_DEC_SEM (1 << 6) 82 83 #define CCW_WAIT4END (1 << 7) 83 84 #define CCW_HALT_ON_TERM (1 << 8) ··· 548 547 ccw->bits |= CCW_TERM_FLUSH; 549 548 ccw->bits |= BF_CCW(sg_len, PIO_NUM); 550 549 ccw->bits |= BF_CCW(MXS_DMA_CMD_NO_XFER, COMMAND); 550 + if (flags & MXS_DMA_CTRL_WAIT4RDY) 551 + ccw->bits |= CCW_WAIT4RDY; 551 552 } else { 552 553 for_each_sg(sgl, sg, sg_len, i) { 553 554 if (sg_dma_len(sg) > MAX_XFER_BYTES) {
+426 -679
drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.c
··· 528 528 static int bch_set_geometry(struct gpmi_nand_data *this) 529 529 { 530 530 struct resources *r = &this->resources; 531 - struct bch_geometry *bch_geo = &this->bch_geometry; 532 - unsigned int block_count; 533 - unsigned int block_size; 534 - unsigned int metadata_size; 535 - unsigned int ecc_strength; 536 - unsigned int page_size; 537 - unsigned int gf_len; 538 531 int ret; 539 532 540 533 ret = common_nfc_set_geometry(this); 541 534 if (ret) 542 535 return ret; 543 - 544 - block_count = bch_geo->ecc_chunk_count - 1; 545 - block_size = bch_geo->ecc_chunk_size; 546 - metadata_size = bch_geo->metadata_size; 547 - ecc_strength = bch_geo->ecc_strength >> 1; 548 - page_size = bch_geo->page_size; 549 - gf_len = bch_geo->gf_len; 550 536 551 537 ret = pm_runtime_get_sync(this->dev); 552 538 if (ret < 0) ··· 547 561 if (ret) 548 562 goto err_out; 549 563 550 - /* Configure layout 0. */ 551 - writel(BF_BCH_FLASH0LAYOUT0_NBLOCKS(block_count) 552 - | BF_BCH_FLASH0LAYOUT0_META_SIZE(metadata_size) 553 - | BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) 554 - | BF_BCH_FLASH0LAYOUT0_GF(gf_len, this) 555 - | BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this), 556 - r->bch_regs + HW_BCH_FLASH0LAYOUT0); 557 - 558 - writel(BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) 559 - | BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) 560 - | BF_BCH_FLASH0LAYOUT1_GF(gf_len, this) 561 - | BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this), 562 - r->bch_regs + HW_BCH_FLASH0LAYOUT1); 563 - 564 564 /* Set *all* chip selects to use layout 0. */ 565 565 writel(0, r->bch_regs + HW_BCH_LAYOUTSELECT); 566 - 567 - /* Enable interrupts. */ 568 - writel(BM_BCH_CTRL_COMPLETE_IRQ_EN, 569 - r->bch_regs + HW_BCH_CTRL_SET); 570 566 571 567 ret = 0; 572 568 err_out: ··· 763 795 writel(BM_BCH_CTRL_COMPLETE_IRQ, r->bch_regs + HW_BCH_CTRL_CLR); 764 796 } 765 797 766 - /* Returns the Ready/Busy status of the given chip. */ 767 - static int gpmi_is_ready(struct gpmi_nand_data *this, unsigned chip) 768 - { 769 - struct resources *r = &this->resources; 770 - uint32_t mask = 0; 771 - uint32_t reg = 0; 772 - 773 - if (GPMI_IS_MX23(this)) { 774 - mask = MX23_BM_GPMI_DEBUG_READY0 << chip; 775 - reg = readl(r->gpmi_regs + HW_GPMI_DEBUG); 776 - } else if (GPMI_IS_MX28(this) || GPMI_IS_MX6(this)) { 777 - /* 778 - * In the imx6, all the ready/busy pins are bound 779 - * together. So we only need to check chip 0. 780 - */ 781 - if (GPMI_IS_MX6(this)) 782 - chip = 0; 783 - 784 - /* MX28 shares the same R/B register as MX6Q. */ 785 - mask = MX28_BF_GPMI_STAT_READY_BUSY(1 << chip); 786 - reg = readl(r->gpmi_regs + HW_GPMI_STAT); 787 - } else 788 - dev_err(this->dev, "unknown arch.\n"); 789 - return reg & mask; 790 - } 791 - 792 798 static struct dma_chan *get_dma_chan(struct gpmi_nand_data *this) 793 799 { 794 800 /* We use the DMA channel 0 to access all the nand chips. */ ··· 778 836 complete(dma_c); 779 837 } 780 838 781 - static int start_dma_without_bch_irq(struct gpmi_nand_data *this, 782 - struct dma_async_tx_descriptor *desc) 783 - { 784 - struct completion *dma_c = &this->dma_done; 785 - unsigned long timeout; 786 - 787 - init_completion(dma_c); 788 - 789 - desc->callback = dma_irq_callback; 790 - desc->callback_param = this; 791 - dmaengine_submit(desc); 792 - dma_async_issue_pending(get_dma_chan(this)); 793 - 794 - /* Wait for the interrupt from the DMA block. */ 795 - timeout = wait_for_completion_timeout(dma_c, msecs_to_jiffies(1000)); 796 - if (!timeout) { 797 - dev_err(this->dev, "DMA timeout, last DMA\n"); 798 - gpmi_dump_info(this); 799 - return -ETIMEDOUT; 800 - } 801 - return 0; 802 - } 803 - 804 839 static irqreturn_t bch_irq(int irq, void *cookie) 805 840 { 806 841 struct gpmi_nand_data *this = cookie; ··· 787 868 return IRQ_HANDLED; 788 869 } 789 870 790 - /* 791 - * This function is used in BCH reading or BCH writing pages. 792 - * It will wait for the BCH interrupt as long as ONE second. 793 - * Actually, we must wait for two interrupts : 794 - * [1] firstly the DMA interrupt and 795 - * [2] secondly the BCH interrupt. 796 - */ 797 - static int start_dma_with_bch_irq(struct gpmi_nand_data *this, 798 - struct dma_async_tx_descriptor *desc) 871 + static int gpmi_raw_len_to_len(struct gpmi_nand_data *this, int raw_len) 799 872 { 800 - struct completion *bch_c = &this->bch_done; 801 - unsigned long timeout; 802 - 803 - /* Prepare to receive an interrupt from the BCH block. */ 804 - init_completion(bch_c); 805 - 806 - /* start the DMA */ 807 - start_dma_without_bch_irq(this, desc); 808 - 809 - /* Wait for the interrupt from the BCH block. */ 810 - timeout = wait_for_completion_timeout(bch_c, msecs_to_jiffies(1000)); 811 - if (!timeout) { 812 - dev_err(this->dev, "BCH timeout\n"); 813 - gpmi_dump_info(this); 814 - return -ETIMEDOUT; 815 - } 816 - return 0; 817 - } 818 - 819 - static int gpmi_send_command(struct gpmi_nand_data *this) 820 - { 821 - struct dma_chan *channel = get_dma_chan(this); 822 - struct dma_async_tx_descriptor *desc; 823 - struct scatterlist *sgl; 824 - int chip = this->current_chip; 825 - int ret; 826 - u32 pio[3]; 827 - 828 - /* [1] send out the PIO words */ 829 - pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE) 830 - | BM_GPMI_CTRL0_WORD_LENGTH 831 - | BF_GPMI_CTRL0_CS(chip, this) 832 - | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 833 - | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE) 834 - | BM_GPMI_CTRL0_ADDRESS_INCREMENT 835 - | BF_GPMI_CTRL0_XFER_COUNT(this->command_length); 836 - pio[1] = pio[2] = 0; 837 - desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 838 - DMA_TRANS_NONE, 0); 839 - if (!desc) 840 - return -EINVAL; 841 - 842 - /* [2] send out the COMMAND + ADDRESS string stored in @buffer */ 843 - sgl = &this->cmd_sgl; 844 - 845 - sg_init_one(sgl, this->cmd_buffer, this->command_length); 846 - dma_map_sg(this->dev, sgl, 1, DMA_TO_DEVICE); 847 - desc = dmaengine_prep_slave_sg(channel, 848 - sgl, 1, DMA_MEM_TO_DEV, 849 - MXS_DMA_CTRL_WAIT4END); 850 - if (!desc) 851 - return -EINVAL; 852 - 853 - /* [3] submit the DMA */ 854 - ret = start_dma_without_bch_irq(this, desc); 855 - 856 - dma_unmap_sg(this->dev, sgl, 1, DMA_TO_DEVICE); 857 - 858 - return ret; 873 + /* 874 + * raw_len is the length to read/write including bch data which 875 + * we are passed in exec_op. Calculate the data length from it. 876 + */ 877 + if (this->bch) 878 + return ALIGN_DOWN(raw_len, this->bch_geometry.ecc_chunk_size); 879 + else 880 + return raw_len; 859 881 } 860 882 861 883 /* Can we use the upper's buffer directly for DMA? */ 862 884 static bool prepare_data_dma(struct gpmi_nand_data *this, const void *buf, 863 - int len, enum dma_data_direction dr) 885 + int raw_len, struct scatterlist *sgl, 886 + enum dma_data_direction dr) 864 887 { 865 - struct scatterlist *sgl = &this->data_sgl; 866 888 int ret; 889 + int len = gpmi_raw_len_to_len(this, raw_len); 867 890 868 891 /* first try to map the upper buffer directly */ 869 892 if (virt_addr_valid(buf) && !object_is_on_stack(buf)) { ··· 821 960 /* We have to use our own DMA buffer. */ 822 961 sg_init_one(sgl, this->data_buffer_dma, len); 823 962 824 - if (dr == DMA_TO_DEVICE) 963 + if (dr == DMA_TO_DEVICE && buf != this->data_buffer_dma) 825 964 memcpy(this->data_buffer_dma, buf, len); 826 965 827 966 dma_map_sg(this->dev, sgl, 1, dr); 828 967 829 968 return false; 830 - } 831 - 832 - static int gpmi_send_data(struct gpmi_nand_data *this, const void *buf, int len) 833 - { 834 - struct dma_async_tx_descriptor *desc; 835 - struct dma_chan *channel = get_dma_chan(this); 836 - int chip = this->current_chip; 837 - int ret; 838 - uint32_t command_mode; 839 - uint32_t address; 840 - u32 pio[2]; 841 - 842 - /* [1] PIO */ 843 - command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE; 844 - address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; 845 - 846 - pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) 847 - | BM_GPMI_CTRL0_WORD_LENGTH 848 - | BF_GPMI_CTRL0_CS(chip, this) 849 - | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 850 - | BF_GPMI_CTRL0_ADDRESS(address) 851 - | BF_GPMI_CTRL0_XFER_COUNT(len); 852 - pio[1] = 0; 853 - desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 854 - DMA_TRANS_NONE, 0); 855 - if (!desc) 856 - return -EINVAL; 857 - 858 - /* [2] send DMA request */ 859 - prepare_data_dma(this, buf, len, DMA_TO_DEVICE); 860 - desc = dmaengine_prep_slave_sg(channel, &this->data_sgl, 861 - 1, DMA_MEM_TO_DEV, 862 - MXS_DMA_CTRL_WAIT4END); 863 - if (!desc) 864 - return -EINVAL; 865 - 866 - /* [3] submit the DMA */ 867 - ret = start_dma_without_bch_irq(this, desc); 868 - 869 - dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_TO_DEVICE); 870 - 871 - return ret; 872 - } 873 - 874 - static int gpmi_read_data(struct gpmi_nand_data *this, void *buf, int len) 875 - { 876 - struct dma_async_tx_descriptor *desc; 877 - struct dma_chan *channel = get_dma_chan(this); 878 - int chip = this->current_chip; 879 - int ret; 880 - u32 pio[2]; 881 - bool direct; 882 - 883 - /* [1] : send PIO */ 884 - pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ) 885 - | BM_GPMI_CTRL0_WORD_LENGTH 886 - | BF_GPMI_CTRL0_CS(chip, this) 887 - | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 888 - | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA) 889 - | BF_GPMI_CTRL0_XFER_COUNT(len); 890 - pio[1] = 0; 891 - desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 892 - DMA_TRANS_NONE, 0); 893 - if (!desc) 894 - return -EINVAL; 895 - 896 - /* [2] : send DMA request */ 897 - direct = prepare_data_dma(this, buf, len, DMA_FROM_DEVICE); 898 - desc = dmaengine_prep_slave_sg(channel, &this->data_sgl, 899 - 1, DMA_DEV_TO_MEM, 900 - MXS_DMA_CTRL_WAIT4END); 901 - if (!desc) 902 - return -EINVAL; 903 - 904 - /* [3] : submit the DMA */ 905 - 906 - ret = start_dma_without_bch_irq(this, desc); 907 - 908 - dma_unmap_sg(this->dev, &this->data_sgl, 1, DMA_FROM_DEVICE); 909 - if (!direct) 910 - memcpy(buf, this->data_buffer_dma, len); 911 - 912 - return ret; 913 - } 914 - 915 - static int gpmi_send_page(struct gpmi_nand_data *this, dma_addr_t payload, 916 - dma_addr_t auxiliary) 917 - { 918 - struct bch_geometry *geo = &this->bch_geometry; 919 - uint32_t command_mode; 920 - uint32_t address; 921 - uint32_t ecc_command; 922 - uint32_t buffer_mask; 923 - struct dma_async_tx_descriptor *desc; 924 - struct dma_chan *channel = get_dma_chan(this); 925 - int chip = this->current_chip; 926 - u32 pio[6]; 927 - 928 - /* A DMA descriptor that does an ECC page read. */ 929 - command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WRITE; 930 - address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; 931 - ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE; 932 - buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE | 933 - BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY; 934 - 935 - pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) 936 - | BM_GPMI_CTRL0_WORD_LENGTH 937 - | BF_GPMI_CTRL0_CS(chip, this) 938 - | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 939 - | BF_GPMI_CTRL0_ADDRESS(address) 940 - | BF_GPMI_CTRL0_XFER_COUNT(0); 941 - pio[1] = 0; 942 - pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC 943 - | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command) 944 - | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask); 945 - pio[3] = geo->page_size; 946 - pio[4] = payload; 947 - pio[5] = auxiliary; 948 - 949 - desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 950 - DMA_TRANS_NONE, 951 - MXS_DMA_CTRL_WAIT4END); 952 - if (!desc) 953 - return -EINVAL; 954 - 955 - return start_dma_with_bch_irq(this, desc); 956 - } 957 - 958 - static int gpmi_read_page(struct gpmi_nand_data *this, dma_addr_t payload, 959 - dma_addr_t auxiliary) 960 - { 961 - struct bch_geometry *geo = &this->bch_geometry; 962 - uint32_t command_mode; 963 - uint32_t address; 964 - uint32_t ecc_command; 965 - uint32_t buffer_mask; 966 - struct dma_async_tx_descriptor *desc; 967 - struct dma_chan *channel = get_dma_chan(this); 968 - int chip = this->current_chip; 969 - u32 pio[6]; 970 - 971 - /* [1] Wait for the chip to report ready. */ 972 - command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY; 973 - address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; 974 - 975 - pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) 976 - | BM_GPMI_CTRL0_WORD_LENGTH 977 - | BF_GPMI_CTRL0_CS(chip, this) 978 - | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 979 - | BF_GPMI_CTRL0_ADDRESS(address) 980 - | BF_GPMI_CTRL0_XFER_COUNT(0); 981 - pio[1] = 0; 982 - desc = mxs_dmaengine_prep_pio(channel, pio, 2, DMA_TRANS_NONE, 0); 983 - if (!desc) 984 - return -EINVAL; 985 - 986 - /* [2] Enable the BCH block and read. */ 987 - command_mode = BV_GPMI_CTRL0_COMMAND_MODE__READ; 988 - address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; 989 - ecc_command = BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE; 990 - buffer_mask = BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE 991 - | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY; 992 - 993 - pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) 994 - | BM_GPMI_CTRL0_WORD_LENGTH 995 - | BF_GPMI_CTRL0_CS(chip, this) 996 - | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 997 - | BF_GPMI_CTRL0_ADDRESS(address) 998 - | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size); 999 - 1000 - pio[1] = 0; 1001 - pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC 1002 - | BF_GPMI_ECCCTRL_ECC_CMD(ecc_command) 1003 - | BF_GPMI_ECCCTRL_BUFFER_MASK(buffer_mask); 1004 - pio[3] = geo->page_size; 1005 - pio[4] = payload; 1006 - pio[5] = auxiliary; 1007 - desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 1008 - DMA_TRANS_NONE, 1009 - MXS_DMA_CTRL_WAIT4END); 1010 - if (!desc) 1011 - return -EINVAL; 1012 - 1013 - /* [3] Disable the BCH block */ 1014 - command_mode = BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY; 1015 - address = BV_GPMI_CTRL0_ADDRESS__NAND_DATA; 1016 - 1017 - pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(command_mode) 1018 - | BM_GPMI_CTRL0_WORD_LENGTH 1019 - | BF_GPMI_CTRL0_CS(chip, this) 1020 - | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 1021 - | BF_GPMI_CTRL0_ADDRESS(address) 1022 - | BF_GPMI_CTRL0_XFER_COUNT(geo->page_size); 1023 - pio[1] = 0; 1024 - pio[2] = 0; /* clear GPMI_HW_GPMI_ECCCTRL, disable the BCH. */ 1025 - desc = mxs_dmaengine_prep_pio(channel, pio, 3, DMA_TRANS_NONE, 1026 - MXS_DMA_CTRL_WAIT4END); 1027 - if (!desc) 1028 - return -EINVAL; 1029 - 1030 - /* [4] submit the DMA */ 1031 - return start_dma_with_bch_irq(this, desc); 1032 969 } 1033 970 1034 971 /** ··· 1227 1568 release_dma_channels(this); 1228 1569 } 1229 1570 1230 - static int send_page_prepare(struct gpmi_nand_data *this, 1231 - const void *source, unsigned length, 1232 - void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, 1233 - const void **use_virt, dma_addr_t *use_phys) 1234 - { 1235 - struct device *dev = this->dev; 1236 - 1237 - if (virt_addr_valid(source)) { 1238 - dma_addr_t source_phys; 1239 - 1240 - source_phys = dma_map_single(dev, (void *)source, length, 1241 - DMA_TO_DEVICE); 1242 - if (dma_mapping_error(dev, source_phys)) { 1243 - if (alt_size < length) { 1244 - dev_err(dev, "Alternate buffer is too small\n"); 1245 - return -ENOMEM; 1246 - } 1247 - goto map_failed; 1248 - } 1249 - *use_virt = source; 1250 - *use_phys = source_phys; 1251 - return 0; 1252 - } 1253 - map_failed: 1254 - /* 1255 - * Copy the content of the source buffer into the alternate 1256 - * buffer and set up the return values accordingly. 1257 - */ 1258 - memcpy(alt_virt, source, length); 1259 - 1260 - *use_virt = alt_virt; 1261 - *use_phys = alt_phys; 1262 - return 0; 1263 - } 1264 - 1265 - static void send_page_end(struct gpmi_nand_data *this, 1266 - const void *source, unsigned length, 1267 - void *alt_virt, dma_addr_t alt_phys, unsigned alt_size, 1268 - const void *used_virt, dma_addr_t used_phys) 1269 - { 1270 - struct device *dev = this->dev; 1271 - if (used_virt == source) 1272 - dma_unmap_single(dev, used_phys, length, DMA_TO_DEVICE); 1273 - } 1274 - 1275 1571 static void gpmi_free_dma_buffer(struct gpmi_nand_data *this) 1276 1572 { 1277 1573 struct device *dev = this->dev; 1574 + struct bch_geometry *geo = &this->bch_geometry; 1278 1575 1279 - if (this->payload_virt && virt_addr_valid(this->payload_virt)) 1280 - dma_free_coherent(dev, this->page_buffer_size, 1281 - this->payload_virt, 1282 - this->payload_phys); 1283 - kfree(this->cmd_buffer); 1576 + if (this->auxiliary_virt && virt_addr_valid(this->auxiliary_virt)) 1577 + dma_free_coherent(dev, geo->auxiliary_size, 1578 + this->auxiliary_virt, 1579 + this->auxiliary_phys); 1284 1580 kfree(this->data_buffer_dma); 1285 1581 kfree(this->raw_buffer); 1286 1582 1287 - this->cmd_buffer = NULL; 1288 1583 this->data_buffer_dma = NULL; 1289 1584 this->raw_buffer = NULL; 1290 - this->page_buffer_size = 0; 1291 1585 } 1292 1586 1293 1587 /* Allocate the DMA buffers */ ··· 1249 1637 struct bch_geometry *geo = &this->bch_geometry; 1250 1638 struct device *dev = this->dev; 1251 1639 struct mtd_info *mtd = nand_to_mtd(&this->nand); 1252 - 1253 - /* [1] Allocate a command buffer. PAGE_SIZE is enough. */ 1254 - this->cmd_buffer = kzalloc(PAGE_SIZE, GFP_DMA | GFP_KERNEL); 1255 - if (this->cmd_buffer == NULL) 1256 - goto error_alloc; 1257 1640 1258 1641 /* 1259 1642 * [2] Allocate a read/write data buffer. ··· 1263 1656 if (this->data_buffer_dma == NULL) 1264 1657 goto error_alloc; 1265 1658 1266 - /* 1267 - * [3] Allocate the page buffer. 1268 - * 1269 - * Both the payload buffer and the auxiliary buffer must appear on 1270 - * 32-bit boundaries. We presume the size of the payload buffer is a 1271 - * power of two and is much larger than four, which guarantees the 1272 - * auxiliary buffer will appear on a 32-bit boundary. 1273 - */ 1274 - this->page_buffer_size = geo->payload_size + geo->auxiliary_size; 1275 - this->payload_virt = dma_alloc_coherent(dev, this->page_buffer_size, 1276 - &this->payload_phys, GFP_DMA); 1277 - if (!this->payload_virt) 1659 + this->auxiliary_virt = dma_alloc_coherent(dev, geo->auxiliary_size, 1660 + &this->auxiliary_phys, GFP_DMA); 1661 + if (!this->auxiliary_virt) 1278 1662 goto error_alloc; 1279 1663 1280 - this->raw_buffer = kzalloc(mtd->writesize + mtd->oobsize, GFP_KERNEL); 1664 + this->raw_buffer = kzalloc((mtd->writesize ?: PAGE_SIZE) + mtd->oobsize, GFP_KERNEL); 1281 1665 if (!this->raw_buffer) 1282 1666 goto error_alloc; 1283 1667 1284 - /* Slice up the page buffer. */ 1285 - this->auxiliary_virt = this->payload_virt + geo->payload_size; 1286 - this->auxiliary_phys = this->payload_phys + geo->payload_size; 1287 1668 return 0; 1288 1669 1289 1670 error_alloc: 1290 1671 gpmi_free_dma_buffer(this); 1291 1672 return -ENOMEM; 1292 - } 1293 - 1294 - static void gpmi_cmd_ctrl(struct nand_chip *chip, int data, unsigned int ctrl) 1295 - { 1296 - struct gpmi_nand_data *this = nand_get_controller_data(chip); 1297 - int ret; 1298 - 1299 - /* 1300 - * Every operation begins with a command byte and a series of zero or 1301 - * more address bytes. These are distinguished by either the Address 1302 - * Latch Enable (ALE) or Command Latch Enable (CLE) signals being 1303 - * asserted. When MTD is ready to execute the command, it will deassert 1304 - * both latch enables. 1305 - * 1306 - * Rather than run a separate DMA operation for every single byte, we 1307 - * queue them up and run a single DMA operation for the entire series 1308 - * of command and data bytes. NAND_CMD_NONE means the END of the queue. 1309 - */ 1310 - if ((ctrl & (NAND_ALE | NAND_CLE))) { 1311 - if (data != NAND_CMD_NONE) 1312 - this->cmd_buffer[this->command_length++] = data; 1313 - return; 1314 - } 1315 - 1316 - if (!this->command_length) 1317 - return; 1318 - 1319 - ret = gpmi_send_command(this); 1320 - if (ret) 1321 - dev_err(this->dev, "Chip: %u, Error %d\n", 1322 - this->current_chip, ret); 1323 - 1324 - this->command_length = 0; 1325 - } 1326 - 1327 - static int gpmi_dev_ready(struct nand_chip *chip) 1328 - { 1329 - struct gpmi_nand_data *this = nand_get_controller_data(chip); 1330 - 1331 - return gpmi_is_ready(this, this->current_chip); 1332 - } 1333 - 1334 - static void gpmi_select_chip(struct nand_chip *chip, int chipnr) 1335 - { 1336 - struct gpmi_nand_data *this = nand_get_controller_data(chip); 1337 - int ret; 1338 - 1339 - /* 1340 - * For power consumption matters, disable/enable the clock each time a 1341 - * die is selected/unselected. 1342 - */ 1343 - if (this->current_chip < 0 && chipnr >= 0) { 1344 - ret = pm_runtime_get_sync(this->dev); 1345 - if (ret < 0) 1346 - dev_err(this->dev, "Failed to enable the clock\n"); 1347 - } else if (this->current_chip >= 0 && chipnr < 0) { 1348 - pm_runtime_mark_last_busy(this->dev); 1349 - pm_runtime_put_autosuspend(this->dev); 1350 - } 1351 - 1352 - /* 1353 - * This driver currently supports only one NAND chip. Plus, dies share 1354 - * the same configuration. So once timings have been applied on the 1355 - * controller side, they will not change anymore. When the time will 1356 - * come, the check on must_apply_timings will have to be dropped. 1357 - */ 1358 - if (chipnr >= 0 && this->hw.must_apply_timings) { 1359 - this->hw.must_apply_timings = false; 1360 - gpmi_nfc_apply_timings(this); 1361 - } 1362 - 1363 - this->current_chip = chipnr; 1364 - } 1365 - 1366 - static void gpmi_read_buf(struct nand_chip *chip, uint8_t *buf, int len) 1367 - { 1368 - struct gpmi_nand_data *this = nand_get_controller_data(chip); 1369 - 1370 - dev_dbg(this->dev, "len is %d\n", len); 1371 - 1372 - gpmi_read_data(this, buf, len); 1373 - } 1374 - 1375 - static void gpmi_write_buf(struct nand_chip *chip, const uint8_t *buf, int len) 1376 - { 1377 - struct gpmi_nand_data *this = nand_get_controller_data(chip); 1378 - 1379 - dev_dbg(this->dev, "len is %d\n", len); 1380 - 1381 - gpmi_send_data(this, buf, len); 1382 - } 1383 - 1384 - static uint8_t gpmi_read_byte(struct nand_chip *chip) 1385 - { 1386 - struct gpmi_nand_data *this = nand_get_controller_data(chip); 1387 - uint8_t *buf = this->data_buffer_dma; 1388 - 1389 - gpmi_read_buf(chip, buf, 1); 1390 - return buf[0]; 1391 1673 } 1392 1674 1393 1675 /* ··· 1327 1831 p[1] = (p[1] & mask) | (from_oob >> (8 - bit)); 1328 1832 } 1329 1833 1330 - static int gpmi_ecc_read_page_data(struct nand_chip *chip, uint8_t *buf) 1834 + static int gpmi_count_bitflips(struct nand_chip *chip, void *buf, int first, 1835 + int last, int meta) 1331 1836 { 1332 1837 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1333 1838 struct bch_geometry *nfc_geo = &this->bch_geometry; 1334 1839 struct mtd_info *mtd = nand_to_mtd(chip); 1335 - dma_addr_t payload_phys; 1336 - unsigned int i; 1840 + int i; 1337 1841 unsigned char *status; 1338 - unsigned int max_bitflips = 0; 1339 - int ret; 1340 - bool direct = false; 1341 - 1342 - payload_phys = this->payload_phys; 1343 - 1344 - if (virt_addr_valid(buf)) { 1345 - dma_addr_t dest_phys; 1346 - 1347 - dest_phys = dma_map_single(this->dev, buf, nfc_geo->payload_size, 1348 - DMA_FROM_DEVICE); 1349 - if (!dma_mapping_error(this->dev, dest_phys)) { 1350 - payload_phys = dest_phys; 1351 - direct = true; 1352 - } 1353 - } 1354 - 1355 - /* go! */ 1356 - ret = gpmi_read_page(this, payload_phys, this->auxiliary_phys); 1357 - 1358 - if (direct) 1359 - dma_unmap_single(this->dev, payload_phys, nfc_geo->payload_size, 1360 - DMA_FROM_DEVICE); 1361 - 1362 - if (ret) { 1363 - dev_err(this->dev, "Error in ECC-based read: %d\n", ret); 1364 - return ret; 1365 - } 1842 + unsigned int max_bitflips = 0; 1366 1843 1367 1844 /* Loop over status bytes, accumulating ECC status. */ 1368 - status = this->auxiliary_virt + nfc_geo->auxiliary_status_offset; 1845 + status = this->auxiliary_virt + ALIGN(meta, 4); 1369 1846 1370 - if (!direct) 1371 - memcpy(buf, this->payload_virt, nfc_geo->payload_size); 1372 - 1373 - for (i = 0; i < nfc_geo->ecc_chunk_count; i++, status++) { 1847 + for (i = first; i < last; i++, status++) { 1374 1848 if ((*status == STATUS_GOOD) || (*status == STATUS_ERASED)) 1375 1849 continue; 1376 1850 ··· 1420 1954 max_bitflips = max_t(unsigned int, max_bitflips, *status); 1421 1955 } 1422 1956 1423 - /* handle the block mark swapping */ 1424 - block_mark_swapping(this, buf, this->auxiliary_virt); 1425 - 1426 1957 return max_bitflips; 1958 + } 1959 + 1960 + static void gpmi_bch_layout_std(struct gpmi_nand_data *this) 1961 + { 1962 + struct bch_geometry *geo = &this->bch_geometry; 1963 + unsigned int ecc_strength = geo->ecc_strength >> 1; 1964 + unsigned int gf_len = geo->gf_len; 1965 + unsigned int block_size = block_size = geo->ecc_chunk_size; 1966 + 1967 + this->bch_flashlayout0 = 1968 + BF_BCH_FLASH0LAYOUT0_NBLOCKS(geo->ecc_chunk_count - 1) | 1969 + BF_BCH_FLASH0LAYOUT0_META_SIZE(geo->metadata_size) | 1970 + BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) | 1971 + BF_BCH_FLASH0LAYOUT0_GF(gf_len, this) | 1972 + BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(block_size, this); 1973 + 1974 + this->bch_flashlayout1 = 1975 + BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(geo->page_size) | 1976 + BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) | 1977 + BF_BCH_FLASH0LAYOUT1_GF(gf_len, this) | 1978 + BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(block_size, this); 1427 1979 } 1428 1980 1429 1981 static int gpmi_ecc_read_page(struct nand_chip *chip, uint8_t *buf, ··· 1449 1965 { 1450 1966 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1451 1967 struct mtd_info *mtd = nand_to_mtd(chip); 1968 + struct bch_geometry *geo = &this->bch_geometry; 1969 + unsigned int max_bitflips; 1452 1970 int ret; 1453 1971 1454 - nand_read_page_op(chip, page, 0, NULL, 0); 1972 + gpmi_bch_layout_std(this); 1973 + this->bch = true; 1455 1974 1456 - ret = gpmi_ecc_read_page_data(chip, buf); 1457 - if (ret < 0) 1975 + ret = nand_read_page_op(chip, page, 0, buf, geo->page_size); 1976 + if (ret) 1458 1977 return ret; 1978 + 1979 + max_bitflips = gpmi_count_bitflips(chip, buf, 0, 1980 + geo->ecc_chunk_count, 1981 + geo->auxiliary_status_offset); 1982 + 1983 + /* handle the block mark swapping */ 1984 + block_mark_swapping(this, buf, this->auxiliary_virt); 1459 1985 1460 1986 if (oob_required) { 1461 1987 /* ··· 1482 1988 chip->oob_poi[0] = ((uint8_t *)this->auxiliary_virt)[0]; 1483 1989 } 1484 1990 1485 - return ret; 1991 + return max_bitflips; 1486 1992 } 1487 1993 1488 1994 /* Fake a virtual small page for the subpage read */ ··· 1490 1996 uint32_t len, uint8_t *buf, int page) 1491 1997 { 1492 1998 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1493 - void __iomem *bch_regs = this->resources.bch_regs; 1494 - struct bch_geometry old_geo = this->bch_geometry; 1495 1999 struct bch_geometry *geo = &this->bch_geometry; 1496 2000 int size = chip->ecc.size; /* ECC chunk size */ 1497 2001 int meta, n, page_size; 1498 - u32 r1_old, r2_old, r1_new, r2_new; 1499 2002 unsigned int max_bitflips; 2003 + unsigned int ecc_strength; 1500 2004 int first, last, marker_pos; 1501 2005 int ecc_parity_size; 1502 2006 int col = 0; 1503 - int old_swap_block_mark = this->swap_block_mark; 2007 + int ret; 1504 2008 1505 2009 /* The size of ECC parity */ 1506 2010 ecc_parity_size = geo->gf_len * geo->ecc_strength / 8; ··· 1531 2039 buf = buf + first * size; 1532 2040 } 1533 2041 1534 - nand_read_page_op(chip, page, col, NULL, 0); 2042 + ecc_parity_size = geo->gf_len * geo->ecc_strength / 8; 1535 2043 1536 - /* Save the old environment */ 1537 - r1_old = r1_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT0); 1538 - r2_old = r2_new = readl(bch_regs + HW_BCH_FLASH0LAYOUT1); 1539 - 1540 - /* change the BCH registers and bch_geometry{} */ 1541 2044 n = last - first + 1; 1542 2045 page_size = meta + (size + ecc_parity_size) * n; 2046 + ecc_strength = geo->ecc_strength >> 1; 1543 2047 1544 - r1_new &= ~(BM_BCH_FLASH0LAYOUT0_NBLOCKS | 1545 - BM_BCH_FLASH0LAYOUT0_META_SIZE); 1546 - r1_new |= BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1) 1547 - | BF_BCH_FLASH0LAYOUT0_META_SIZE(meta); 1548 - writel(r1_new, bch_regs + HW_BCH_FLASH0LAYOUT0); 2048 + this->bch_flashlayout0 = BF_BCH_FLASH0LAYOUT0_NBLOCKS(n - 1) | 2049 + BF_BCH_FLASH0LAYOUT0_META_SIZE(meta) | 2050 + BF_BCH_FLASH0LAYOUT0_ECC0(ecc_strength, this) | 2051 + BF_BCH_FLASH0LAYOUT0_GF(geo->gf_len, this) | 2052 + BF_BCH_FLASH0LAYOUT0_DATA0_SIZE(geo->ecc_chunk_size, this); 1549 2053 1550 - r2_new &= ~BM_BCH_FLASH0LAYOUT1_PAGE_SIZE; 1551 - r2_new |= BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size); 1552 - writel(r2_new, bch_regs + HW_BCH_FLASH0LAYOUT1); 2054 + this->bch_flashlayout1 = BF_BCH_FLASH0LAYOUT1_PAGE_SIZE(page_size) | 2055 + BF_BCH_FLASH0LAYOUT1_ECCN(ecc_strength, this) | 2056 + BF_BCH_FLASH0LAYOUT1_GF(geo->gf_len, this) | 2057 + BF_BCH_FLASH0LAYOUT1_DATAN_SIZE(geo->ecc_chunk_size, this); 1553 2058 1554 - geo->ecc_chunk_count = n; 1555 - geo->payload_size = n * size; 1556 - geo->page_size = page_size; 1557 - geo->auxiliary_status_offset = ALIGN(meta, 4); 2059 + this->bch = true; 2060 + 2061 + ret = nand_read_page_op(chip, page, col, buf, page_size); 2062 + if (ret) 2063 + return ret; 1558 2064 1559 2065 dev_dbg(this->dev, "page:%d(%d:%d)%d, chunk:(%d:%d), BCH PG size:%d\n", 1560 2066 page, offs, len, col, first, n, page_size); 1561 2067 1562 - /* Read the subpage now */ 1563 - this->swap_block_mark = false; 1564 - max_bitflips = gpmi_ecc_read_page_data(chip, buf); 1565 - 1566 - /* Restore */ 1567 - writel(r1_old, bch_regs + HW_BCH_FLASH0LAYOUT0); 1568 - writel(r2_old, bch_regs + HW_BCH_FLASH0LAYOUT1); 1569 - this->bch_geometry = old_geo; 1570 - this->swap_block_mark = old_swap_block_mark; 2068 + max_bitflips = gpmi_count_bitflips(chip, buf, first, last, meta); 1571 2069 1572 2070 return max_bitflips; 1573 2071 } ··· 1568 2086 struct mtd_info *mtd = nand_to_mtd(chip); 1569 2087 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1570 2088 struct bch_geometry *nfc_geo = &this->bch_geometry; 1571 - const void *payload_virt; 1572 - dma_addr_t payload_phys; 1573 - const void *auxiliary_virt; 1574 - dma_addr_t auxiliary_phys; 1575 - int ret; 2089 + int ret; 1576 2090 1577 2091 dev_dbg(this->dev, "ecc write page.\n"); 1578 2092 1579 - nand_prog_page_begin_op(chip, page, 0, NULL, 0); 2093 + gpmi_bch_layout_std(this); 2094 + this->bch = true; 2095 + 2096 + memcpy(this->auxiliary_virt, chip->oob_poi, nfc_geo->auxiliary_size); 1580 2097 1581 2098 if (this->swap_block_mark) { 1582 2099 /* 1583 - * If control arrives here, we're doing block mark swapping. 1584 - * Since we can't modify the caller's buffers, we must copy them 1585 - * into our own. 2100 + * When doing bad block marker swapping we must always copy the 2101 + * input buffer as we can't modify the const buffer. 1586 2102 */ 1587 - memcpy(this->payload_virt, buf, mtd->writesize); 1588 - payload_virt = this->payload_virt; 1589 - payload_phys = this->payload_phys; 1590 - 1591 - memcpy(this->auxiliary_virt, chip->oob_poi, 1592 - nfc_geo->auxiliary_size); 1593 - auxiliary_virt = this->auxiliary_virt; 1594 - auxiliary_phys = this->auxiliary_phys; 1595 - 1596 - /* Handle block mark swapping. */ 1597 - block_mark_swapping(this, 1598 - (void *)payload_virt, (void *)auxiliary_virt); 1599 - } else { 1600 - /* 1601 - * If control arrives here, we're not doing block mark swapping, 1602 - * so we can to try and use the caller's buffers. 1603 - */ 1604 - ret = send_page_prepare(this, 1605 - buf, mtd->writesize, 1606 - this->payload_virt, this->payload_phys, 1607 - nfc_geo->payload_size, 1608 - &payload_virt, &payload_phys); 1609 - if (ret) { 1610 - dev_err(this->dev, "Inadequate payload DMA buffer\n"); 1611 - return 0; 1612 - } 1613 - 1614 - ret = send_page_prepare(this, 1615 - chip->oob_poi, mtd->oobsize, 1616 - this->auxiliary_virt, this->auxiliary_phys, 1617 - nfc_geo->auxiliary_size, 1618 - &auxiliary_virt, &auxiliary_phys); 1619 - if (ret) { 1620 - dev_err(this->dev, "Inadequate auxiliary DMA buffer\n"); 1621 - goto exit_auxiliary; 1622 - } 2103 + memcpy(this->data_buffer_dma, buf, mtd->writesize); 2104 + buf = this->data_buffer_dma; 2105 + block_mark_swapping(this, this->data_buffer_dma, 2106 + this->auxiliary_virt); 1623 2107 } 1624 2108 1625 - /* Ask the NFC. */ 1626 - ret = gpmi_send_page(this, payload_phys, auxiliary_phys); 1627 - if (ret) 1628 - dev_err(this->dev, "Error in ECC-based write: %d\n", ret); 2109 + ret = nand_prog_page_op(chip, page, 0, buf, nfc_geo->page_size); 1629 2110 1630 - if (!this->swap_block_mark) { 1631 - send_page_end(this, chip->oob_poi, mtd->oobsize, 1632 - this->auxiliary_virt, this->auxiliary_phys, 1633 - nfc_geo->auxiliary_size, 1634 - auxiliary_virt, auxiliary_phys); 1635 - exit_auxiliary: 1636 - send_page_end(this, buf, mtd->writesize, 1637 - this->payload_virt, this->payload_phys, 1638 - nfc_geo->payload_size, 1639 - payload_virt, payload_phys); 1640 - } 1641 - 1642 - if (ret) 1643 - return ret; 1644 - 1645 - return nand_prog_page_end_op(chip); 2111 + return ret; 1646 2112 } 1647 2113 1648 2114 /* ··· 1659 2229 struct gpmi_nand_data *this = nand_get_controller_data(chip); 1660 2230 int ret; 1661 2231 1662 - dev_dbg(this->dev, "page number is %d\n", page); 1663 2232 /* clear the OOB buffer */ 1664 2233 memset(chip->oob_poi, ~0, mtd->oobsize); 1665 2234 ··· 1726 2297 size_t oob_byte_off; 1727 2298 uint8_t *oob = chip->oob_poi; 1728 2299 int step; 2300 + int ret; 1729 2301 1730 - nand_read_page_op(chip, page, 0, tmp_buf, 1731 - mtd->writesize + mtd->oobsize); 2302 + ret = nand_read_page_op(chip, page, 0, tmp_buf, 2303 + mtd->writesize + mtd->oobsize); 2304 + if (ret) 2305 + return ret; 1732 2306 1733 2307 /* 1734 2308 * If required, swap the bad block marker and the data stored in the ··· 2221 2789 return 0; 2222 2790 } 2223 2791 2792 + static struct gpmi_transfer *get_next_transfer(struct gpmi_nand_data *this) 2793 + { 2794 + struct gpmi_transfer *transfer = &this->transfers[this->ntransfers]; 2795 + 2796 + this->ntransfers++; 2797 + 2798 + if (this->ntransfers == GPMI_MAX_TRANSFERS) 2799 + return NULL; 2800 + 2801 + return transfer; 2802 + } 2803 + 2804 + static struct dma_async_tx_descriptor *gpmi_chain_command( 2805 + struct gpmi_nand_data *this, u8 cmd, const u8 *addr, int naddr) 2806 + { 2807 + struct dma_chan *channel = get_dma_chan(this); 2808 + struct dma_async_tx_descriptor *desc; 2809 + struct gpmi_transfer *transfer; 2810 + int chip = this->nand.cur_cs; 2811 + u32 pio[3]; 2812 + 2813 + /* [1] send out the PIO words */ 2814 + pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE) 2815 + | BM_GPMI_CTRL0_WORD_LENGTH 2816 + | BF_GPMI_CTRL0_CS(chip, this) 2817 + | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 2818 + | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_CLE) 2819 + | BM_GPMI_CTRL0_ADDRESS_INCREMENT 2820 + | BF_GPMI_CTRL0_XFER_COUNT(naddr + 1); 2821 + pio[1] = 0; 2822 + pio[2] = 0; 2823 + desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 2824 + DMA_TRANS_NONE, 0); 2825 + if (!desc) 2826 + return NULL; 2827 + 2828 + transfer = get_next_transfer(this); 2829 + if (!transfer) 2830 + return NULL; 2831 + 2832 + transfer->cmdbuf[0] = cmd; 2833 + if (naddr) 2834 + memcpy(&transfer->cmdbuf[1], addr, naddr); 2835 + 2836 + sg_init_one(&transfer->sgl, transfer->cmdbuf, naddr + 1); 2837 + dma_map_sg(this->dev, &transfer->sgl, 1, DMA_TO_DEVICE); 2838 + 2839 + transfer->direction = DMA_TO_DEVICE; 2840 + 2841 + desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, DMA_MEM_TO_DEV, 2842 + MXS_DMA_CTRL_WAIT4END); 2843 + return desc; 2844 + } 2845 + 2846 + static struct dma_async_tx_descriptor *gpmi_chain_wait_ready( 2847 + struct gpmi_nand_data *this) 2848 + { 2849 + struct dma_chan *channel = get_dma_chan(this); 2850 + u32 pio[2]; 2851 + 2852 + pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WAIT_FOR_READY) 2853 + | BM_GPMI_CTRL0_WORD_LENGTH 2854 + | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this) 2855 + | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 2856 + | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA) 2857 + | BF_GPMI_CTRL0_XFER_COUNT(0); 2858 + pio[1] = 0; 2859 + 2860 + return mxs_dmaengine_prep_pio(channel, pio, 2, DMA_TRANS_NONE, 2861 + MXS_DMA_CTRL_WAIT4END | MXS_DMA_CTRL_WAIT4RDY); 2862 + } 2863 + 2864 + static struct dma_async_tx_descriptor *gpmi_chain_data_read( 2865 + struct gpmi_nand_data *this, void *buf, int raw_len, bool *direct) 2866 + { 2867 + struct dma_async_tx_descriptor *desc; 2868 + struct dma_chan *channel = get_dma_chan(this); 2869 + struct gpmi_transfer *transfer; 2870 + u32 pio[6] = {}; 2871 + 2872 + transfer = get_next_transfer(this); 2873 + if (!transfer) 2874 + return NULL; 2875 + 2876 + transfer->direction = DMA_FROM_DEVICE; 2877 + 2878 + *direct = prepare_data_dma(this, buf, raw_len, &transfer->sgl, 2879 + DMA_FROM_DEVICE); 2880 + 2881 + pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__READ) 2882 + | BM_GPMI_CTRL0_WORD_LENGTH 2883 + | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this) 2884 + | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 2885 + | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA) 2886 + | BF_GPMI_CTRL0_XFER_COUNT(raw_len); 2887 + 2888 + if (this->bch) { 2889 + pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC 2890 + | BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_DECODE) 2891 + | BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE 2892 + | BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY); 2893 + pio[3] = raw_len; 2894 + pio[4] = transfer->sgl.dma_address; 2895 + pio[5] = this->auxiliary_phys; 2896 + } 2897 + 2898 + desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 2899 + DMA_TRANS_NONE, 0); 2900 + if (!desc) 2901 + return NULL; 2902 + 2903 + if (!this->bch) 2904 + desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, 2905 + DMA_DEV_TO_MEM, 2906 + MXS_DMA_CTRL_WAIT4END); 2907 + 2908 + return desc; 2909 + } 2910 + 2911 + static struct dma_async_tx_descriptor *gpmi_chain_data_write( 2912 + struct gpmi_nand_data *this, const void *buf, int raw_len) 2913 + { 2914 + struct dma_chan *channel = get_dma_chan(this); 2915 + struct dma_async_tx_descriptor *desc; 2916 + struct gpmi_transfer *transfer; 2917 + u32 pio[6] = {}; 2918 + 2919 + transfer = get_next_transfer(this); 2920 + if (!transfer) 2921 + return NULL; 2922 + 2923 + transfer->direction = DMA_TO_DEVICE; 2924 + 2925 + prepare_data_dma(this, buf, raw_len, &transfer->sgl, DMA_TO_DEVICE); 2926 + 2927 + pio[0] = BF_GPMI_CTRL0_COMMAND_MODE(BV_GPMI_CTRL0_COMMAND_MODE__WRITE) 2928 + | BM_GPMI_CTRL0_WORD_LENGTH 2929 + | BF_GPMI_CTRL0_CS(this->nand.cur_cs, this) 2930 + | BF_GPMI_CTRL0_LOCK_CS(LOCK_CS_ENABLE, this) 2931 + | BF_GPMI_CTRL0_ADDRESS(BV_GPMI_CTRL0_ADDRESS__NAND_DATA) 2932 + | BF_GPMI_CTRL0_XFER_COUNT(raw_len); 2933 + 2934 + if (this->bch) { 2935 + pio[2] = BM_GPMI_ECCCTRL_ENABLE_ECC 2936 + | BF_GPMI_ECCCTRL_ECC_CMD(BV_GPMI_ECCCTRL_ECC_CMD__BCH_ENCODE) 2937 + | BF_GPMI_ECCCTRL_BUFFER_MASK(BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_PAGE | 2938 + BV_GPMI_ECCCTRL_BUFFER_MASK__BCH_AUXONLY); 2939 + pio[3] = raw_len; 2940 + pio[4] = transfer->sgl.dma_address; 2941 + pio[5] = this->auxiliary_phys; 2942 + } 2943 + 2944 + desc = mxs_dmaengine_prep_pio(channel, pio, ARRAY_SIZE(pio), 2945 + DMA_TRANS_NONE, 2946 + (this->bch ? MXS_DMA_CTRL_WAIT4END : 0)); 2947 + if (!desc) 2948 + return NULL; 2949 + 2950 + if (!this->bch) 2951 + desc = dmaengine_prep_slave_sg(channel, &transfer->sgl, 1, 2952 + DMA_MEM_TO_DEV, 2953 + MXS_DMA_CTRL_WAIT4END); 2954 + 2955 + return desc; 2956 + } 2957 + 2958 + static int gpmi_nfc_exec_op(struct nand_chip *chip, 2959 + const struct nand_operation *op, 2960 + bool check_only) 2961 + { 2962 + const struct nand_op_instr *instr; 2963 + struct gpmi_nand_data *this = nand_get_controller_data(chip); 2964 + struct dma_async_tx_descriptor *desc = NULL; 2965 + int i, ret, buf_len = 0, nbufs = 0; 2966 + u8 cmd = 0; 2967 + void *buf_read = NULL; 2968 + const void *buf_write = NULL; 2969 + bool direct = false; 2970 + struct completion *completion; 2971 + unsigned long to; 2972 + 2973 + this->ntransfers = 0; 2974 + for (i = 0; i < GPMI_MAX_TRANSFERS; i++) 2975 + this->transfers[i].direction = DMA_NONE; 2976 + 2977 + ret = pm_runtime_get_sync(this->dev); 2978 + if (ret < 0) 2979 + return ret; 2980 + 2981 + /* 2982 + * This driver currently supports only one NAND chip. Plus, dies share 2983 + * the same configuration. So once timings have been applied on the 2984 + * controller side, they will not change anymore. When the time will 2985 + * come, the check on must_apply_timings will have to be dropped. 2986 + */ 2987 + if (this->hw.must_apply_timings) { 2988 + this->hw.must_apply_timings = false; 2989 + gpmi_nfc_apply_timings(this); 2990 + } 2991 + 2992 + dev_dbg(this->dev, "%s: %d instructions\n", __func__, op->ninstrs); 2993 + 2994 + for (i = 0; i < op->ninstrs; i++) { 2995 + instr = &op->instrs[i]; 2996 + 2997 + nand_op_trace(" ", instr); 2998 + 2999 + switch (instr->type) { 3000 + case NAND_OP_WAITRDY_INSTR: 3001 + desc = gpmi_chain_wait_ready(this); 3002 + break; 3003 + case NAND_OP_CMD_INSTR: 3004 + cmd = instr->ctx.cmd.opcode; 3005 + 3006 + /* 3007 + * When this command has an address cycle chain it 3008 + * together with the address cycle 3009 + */ 3010 + if (i + 1 != op->ninstrs && 3011 + op->instrs[i + 1].type == NAND_OP_ADDR_INSTR) 3012 + continue; 3013 + 3014 + desc = gpmi_chain_command(this, cmd, NULL, 0); 3015 + 3016 + break; 3017 + case NAND_OP_ADDR_INSTR: 3018 + desc = gpmi_chain_command(this, cmd, instr->ctx.addr.addrs, 3019 + instr->ctx.addr.naddrs); 3020 + break; 3021 + case NAND_OP_DATA_OUT_INSTR: 3022 + buf_write = instr->ctx.data.buf.out; 3023 + buf_len = instr->ctx.data.len; 3024 + nbufs++; 3025 + 3026 + desc = gpmi_chain_data_write(this, buf_write, buf_len); 3027 + 3028 + break; 3029 + case NAND_OP_DATA_IN_INSTR: 3030 + if (!instr->ctx.data.len) 3031 + break; 3032 + buf_read = instr->ctx.data.buf.in; 3033 + buf_len = instr->ctx.data.len; 3034 + nbufs++; 3035 + 3036 + desc = gpmi_chain_data_read(this, buf_read, buf_len, 3037 + &direct); 3038 + break; 3039 + } 3040 + 3041 + if (!desc) { 3042 + ret = -ENXIO; 3043 + goto unmap; 3044 + } 3045 + } 3046 + 3047 + dev_dbg(this->dev, "%s setup done\n", __func__); 3048 + 3049 + if (nbufs > 1) { 3050 + dev_err(this->dev, "Multiple data instructions not supported\n"); 3051 + ret = -EINVAL; 3052 + goto unmap; 3053 + } 3054 + 3055 + if (this->bch) { 3056 + writel(this->bch_flashlayout0, 3057 + this->resources.bch_regs + HW_BCH_FLASH0LAYOUT0); 3058 + writel(this->bch_flashlayout1, 3059 + this->resources.bch_regs + HW_BCH_FLASH0LAYOUT1); 3060 + } 3061 + 3062 + if (this->bch && buf_read) { 3063 + writel(BM_BCH_CTRL_COMPLETE_IRQ_EN, 3064 + this->resources.bch_regs + HW_BCH_CTRL_SET); 3065 + completion = &this->bch_done; 3066 + } else { 3067 + desc->callback = dma_irq_callback; 3068 + desc->callback_param = this; 3069 + completion = &this->dma_done; 3070 + } 3071 + 3072 + init_completion(completion); 3073 + 3074 + dmaengine_submit(desc); 3075 + dma_async_issue_pending(get_dma_chan(this)); 3076 + 3077 + to = wait_for_completion_timeout(completion, msecs_to_jiffies(1000)); 3078 + if (!to) { 3079 + dev_err(this->dev, "DMA timeout, last DMA\n"); 3080 + gpmi_dump_info(this); 3081 + ret = -ETIMEDOUT; 3082 + goto unmap; 3083 + } 3084 + 3085 + writel(BM_BCH_CTRL_COMPLETE_IRQ_EN, 3086 + this->resources.bch_regs + HW_BCH_CTRL_CLR); 3087 + gpmi_clear_bch(this); 3088 + 3089 + ret = 0; 3090 + 3091 + unmap: 3092 + for (i = 0; i < this->ntransfers; i++) { 3093 + struct gpmi_transfer *transfer = &this->transfers[i]; 3094 + 3095 + if (transfer->direction != DMA_NONE) 3096 + dma_unmap_sg(this->dev, &transfer->sgl, 1, 3097 + transfer->direction); 3098 + } 3099 + 3100 + if (!ret && buf_read && !direct) 3101 + memcpy(buf_read, this->data_buffer_dma, 3102 + gpmi_raw_len_to_len(this, buf_len)); 3103 + 3104 + this->bch = false; 3105 + 3106 + pm_runtime_mark_last_busy(this->dev); 3107 + pm_runtime_put_autosuspend(this->dev); 3108 + 3109 + return ret; 3110 + } 3111 + 2224 3112 static const struct nand_controller_ops gpmi_nand_controller_ops = { 2225 3113 .attach_chip = gpmi_nand_attach_chip, 2226 3114 .setup_data_interface = gpmi_setup_data_interface, 3115 + .exec_op = gpmi_nfc_exec_op, 2227 3116 }; 2228 3117 2229 3118 static int gpmi_nand_init(struct gpmi_nand_data *this) ··· 2553 2800 struct mtd_info *mtd = nand_to_mtd(chip); 2554 2801 int ret; 2555 2802 2556 - /* init current chip */ 2557 - this->current_chip = -1; 2558 - 2559 2803 /* init the MTD data structures */ 2560 2804 mtd->name = "gpmi-nand"; 2561 2805 mtd->dev.parent = this->dev; ··· 2560 2810 /* init the nand_chip{}, we don't support a 16-bit NAND Flash bus. */ 2561 2811 nand_set_controller_data(chip, this); 2562 2812 nand_set_flash_node(chip, this->pdev->dev.of_node); 2563 - chip->legacy.select_chip = gpmi_select_chip; 2564 - chip->legacy.cmd_ctrl = gpmi_cmd_ctrl; 2565 - chip->legacy.dev_ready = gpmi_dev_ready; 2566 - chip->legacy.read_byte = gpmi_read_byte; 2567 - chip->legacy.read_buf = gpmi_read_buf; 2568 - chip->legacy.write_buf = gpmi_write_buf; 2569 - chip->badblock_pattern = &gpmi_bbt_descr; 2570 2813 chip->legacy.block_markbad = gpmi_block_markbad; 2814 + chip->badblock_pattern = &gpmi_bbt_descr; 2571 2815 chip->options |= NAND_NO_SUBPAGE_WRITE; 2572 2816 2573 2817 /* Set up swap_block_mark, must be set before the gpmi_set_geometry() */ ··· 2577 2833 if (ret) 2578 2834 goto err_out; 2579 2835 2580 - chip->legacy.dummy_controller.ops = &gpmi_nand_controller_ops; 2836 + nand_controller_init(&this->base); 2837 + this->base.ops = &gpmi_nand_controller_ops; 2838 + chip->controller = &this->base; 2839 + 2581 2840 ret = nand_scan(chip, GPMI_IS_MX6(this) ? 2 : 1); 2582 2841 if (ret) 2583 2842 goto err_out;
+14 -11
drivers/mtd/nand/raw/gpmi-nand/gpmi-nand.h
··· 103 103 u32 ctrl1n; 104 104 }; 105 105 106 + #define GPMI_MAX_TRANSFERS 8 107 + 108 + struct gpmi_transfer { 109 + u8 cmdbuf[8]; 110 + struct scatterlist sgl; 111 + enum dma_data_direction direction; 112 + }; 113 + 106 114 struct gpmi_nand_data { 107 115 /* Devdata */ 108 116 const struct gpmi_devdata *devdata; ··· 134 126 struct boot_rom_geometry rom_geometry; 135 127 136 128 /* MTD / NAND */ 129 + struct nand_controller base; 137 130 struct nand_chip nand; 138 131 139 - /* General-use Variables */ 140 - int current_chip; 141 - unsigned int command_length; 132 + struct gpmi_transfer transfers[GPMI_MAX_TRANSFERS]; 133 + int ntransfers; 142 134 143 - struct scatterlist cmd_sgl; 144 - char *cmd_buffer; 135 + bool bch; 136 + uint32_t bch_flashlayout0; 137 + uint32_t bch_flashlayout1; 145 138 146 - struct scatterlist data_sgl; 147 139 char *data_buffer_dma; 148 - 149 - unsigned int page_buffer_size; 150 - 151 - void *payload_virt; 152 - dma_addr_t payload_phys; 153 140 154 141 void *auxiliary_virt; 155 142 dma_addr_t auxiliary_phys;
+1
include/linux/dma/mxs-dma.h
··· 5 5 #include <linux/dmaengine.h> 6 6 7 7 #define MXS_DMA_CTRL_WAIT4END BIT(31) 8 + #define MXS_DMA_CTRL_WAIT4RDY BIT(30) 8 9 9 10 /* 10 11 * The mxs dmaengine can do PIO transfers. We pass a pointer to the PIO words