150752790SStefan Roese /*
250752790SStefan Roese * (C) Copyright 2008 Stefan Roese <sr@denx.de>, DENX Software Engineering
350752790SStefan Roese *
41a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
550752790SStefan Roese */
650752790SStefan Roese
750752790SStefan Roese #include <common.h>
850752790SStefan Roese #include <asm/io.h>
950752790SStefan Roese #include <linux/mtd/mtd.h>
1050752790SStefan Roese #include <linux/mtd/onenand.h>
1150752790SStefan Roese #include "vct.h"
1250752790SStefan Roese
1350752790SStefan Roese #define BURST_SIZE_WORDS 4
1450752790SStefan Roese
ebi_nand_read_word(void __iomem * addr)1550752790SStefan Roese static u16 ebi_nand_read_word(void __iomem *addr)
1650752790SStefan Roese {
1750752790SStefan Roese reg_write(EBI_CPU_IO_ACCS(EBI_BASE), (EXT_DEVICE_CHANNEL_2 | (u32)addr));
1850752790SStefan Roese ebi_wait();
1950752790SStefan Roese
2050752790SStefan Roese return reg_read(EBI_IO_ACCS_DATA(EBI_BASE)) >> 16;
2150752790SStefan Roese }
2250752790SStefan Roese
ebi_nand_write_word(u16 data,void __iomem * addr)2350752790SStefan Roese static void ebi_nand_write_word(u16 data, void __iomem * addr)
2450752790SStefan Roese {
2550752790SStefan Roese ebi_wait();
2650752790SStefan Roese reg_write(EBI_IO_ACCS_DATA(EBI_BASE), (data << 16));
2750752790SStefan Roese reg_write(EBI_CPU_IO_ACCS(EBI_BASE),
2850752790SStefan Roese EXT_DEVICE_CHANNEL_2 | EBI_CPU_WRITE | (u32)addr);
2950752790SStefan Roese ebi_wait();
3050752790SStefan Roese }
3150752790SStefan Roese
3250752790SStefan Roese /*
3350752790SStefan Roese * EBI initialization for OneNAND FLASH access
3450752790SStefan Roese */
ebi_init_onenand(void)3550752790SStefan Roese int ebi_init_onenand(void)
3650752790SStefan Roese {
3750752790SStefan Roese reg_write(EBI_DEV1_CONFIG1(EBI_BASE), 0x83000);
3850752790SStefan Roese
3950752790SStefan Roese reg_write(EBI_DEV2_CONFIG1(EBI_BASE), 0x00403002);
4050752790SStefan Roese reg_write(EBI_DEV2_CONFIG2(EBI_BASE), 0x50);
4150752790SStefan Roese
4250752790SStefan Roese reg_write(EBI_DEV3_CONFIG1(EBI_BASE), 0x00403002);
4350752790SStefan Roese reg_write(EBI_DEV3_CONFIG2(EBI_BASE), 0x0); /* byte/word ordering */
4450752790SStefan Roese
4550752790SStefan Roese reg_write(EBI_DEV2_TIM1_RD1(EBI_BASE), 0x00504000);
4650752790SStefan Roese reg_write(EBI_DEV2_TIM1_RD2(EBI_BASE), 0x00001000);
4750752790SStefan Roese reg_write(EBI_DEV2_TIM1_WR1(EBI_BASE), 0x12002223);
4850752790SStefan Roese reg_write(EBI_DEV2_TIM1_WR2(EBI_BASE), 0x3FC02220);
4950752790SStefan Roese reg_write(EBI_DEV3_TIM1_RD1(EBI_BASE), 0x00504000);
5050752790SStefan Roese reg_write(EBI_DEV3_TIM1_RD2(EBI_BASE), 0x00001000);
5150752790SStefan Roese reg_write(EBI_DEV3_TIM1_WR1(EBI_BASE), 0x05001000);
5250752790SStefan Roese reg_write(EBI_DEV3_TIM1_WR2(EBI_BASE), 0x00010200);
5350752790SStefan Roese
5450752790SStefan Roese reg_write(EBI_DEV2_TIM_EXT(EBI_BASE), 0xFFF00000);
5550752790SStefan Roese reg_write(EBI_DEV2_EXT_ACC(EBI_BASE), 0x0FFFFFFF);
5650752790SStefan Roese
5750752790SStefan Roese reg_write(EBI_DEV3_TIM_EXT(EBI_BASE), 0xFFF00000);
5850752790SStefan Roese reg_write(EBI_DEV3_EXT_ACC(EBI_BASE), 0x0FFFFFFF);
5950752790SStefan Roese
6050752790SStefan Roese /* prepare DMA configuration for EBI */
6150752790SStefan Roese reg_write(EBI_DEV3_FIFO_CONFIG(EBI_BASE), 0x0101ff00);
6250752790SStefan Roese
6350752790SStefan Roese /* READ only no byte order change, TAG 1 used */
6450752790SStefan Roese reg_write(EBI_DEV3_DMA_CONFIG2(EBI_BASE), 0x00000004);
6550752790SStefan Roese
6650752790SStefan Roese reg_write(EBI_TAG1_SYS_ID(EBI_BASE), 0x0); /* SCC DMA channel 0 */
6750752790SStefan Roese reg_write(EBI_TAG2_SYS_ID(EBI_BASE), 0x1);
6850752790SStefan Roese reg_write(EBI_TAG3_SYS_ID(EBI_BASE), 0x2);
6950752790SStefan Roese reg_write(EBI_TAG4_SYS_ID(EBI_BASE), 0x3);
7050752790SStefan Roese
7150752790SStefan Roese return 0;
7250752790SStefan Roese }
7350752790SStefan Roese
memcpy_16_from_onenand(void * dst,const void * src,unsigned int len)7450752790SStefan Roese static void *memcpy_16_from_onenand(void *dst, const void *src, unsigned int len)
7550752790SStefan Roese {
7650752790SStefan Roese void *ret = dst;
7750752790SStefan Roese u16 *d = dst;
7850752790SStefan Roese u16 *s = (u16 *)src;
7950752790SStefan Roese
8050752790SStefan Roese len >>= 1;
8150752790SStefan Roese while (len-- > 0)
8250752790SStefan Roese *d++ = ebi_nand_read_word(s++);
8350752790SStefan Roese
8450752790SStefan Roese return ret;
8550752790SStefan Roese }
8650752790SStefan Roese
memcpy_32_from_onenand(void * dst,const void * src,unsigned int len)8750752790SStefan Roese static void *memcpy_32_from_onenand(void *dst, const void *src, unsigned int len)
8850752790SStefan Roese {
8950752790SStefan Roese void *ret = dst;
9050752790SStefan Roese u32 *d = (u32 *)dst;
9150752790SStefan Roese u32 s = (u32)src;
9250752790SStefan Roese u32 bytes_per_block = BURST_SIZE_WORDS * sizeof(int);
9350752790SStefan Roese u32 n_blocks = len / bytes_per_block;
9450752790SStefan Roese u32 block = 0;
9550752790SStefan Roese u32 burst_word;
9650752790SStefan Roese
9750752790SStefan Roese for (block = 0; block < n_blocks; block++) {
9850752790SStefan Roese /* Trigger read channel 3 */
9950752790SStefan Roese reg_write(EBI_CPU_IO_ACCS(EBI_BASE),
10050752790SStefan Roese (EXT_DEVICE_CHANNEL_3 | (s + (block * bytes_per_block))));
10150752790SStefan Roese /* Poll status to see whether read has finished */
10250752790SStefan Roese ebi_wait();
10350752790SStefan Roese
10450752790SStefan Roese /* Squirrel the data away in a safe place */
10550752790SStefan Roese for (burst_word = 0; burst_word < BURST_SIZE_WORDS; burst_word++)
10650752790SStefan Roese *d++ = reg_read(EBI_IO_ACCS_DATA(EBI_BASE));
10750752790SStefan Roese }
10850752790SStefan Roese
10950752790SStefan Roese return ret;
11050752790SStefan Roese }
11150752790SStefan Roese
memcpy_16_to_onenand(void * dst,const void * src,unsigned int len)11250752790SStefan Roese static void *memcpy_16_to_onenand(void *dst, const void *src, unsigned int len)
11350752790SStefan Roese {
11450752790SStefan Roese void *ret = dst;
11550752790SStefan Roese u16 *d = dst;
11650752790SStefan Roese u16 *s = (u16 *)src;
11750752790SStefan Roese
11850752790SStefan Roese len >>= 1;
11950752790SStefan Roese while (len-- > 0)
12050752790SStefan Roese ebi_nand_write_word(*s++, d++);
12150752790SStefan Roese
12250752790SStefan Roese return ret;
12350752790SStefan Roese }
12450752790SStefan Roese
onenand_bufferram_offset(struct mtd_info * mtd,int area)12550752790SStefan Roese static inline int onenand_bufferram_offset(struct mtd_info *mtd, int area)
12650752790SStefan Roese {
12750752790SStefan Roese struct onenand_chip *this = mtd->priv;
12850752790SStefan Roese
12950752790SStefan Roese if (ONENAND_CURRENT_BUFFERRAM(this)) {
13050752790SStefan Roese if (area == ONENAND_DATARAM)
13150752790SStefan Roese return mtd->writesize;
13250752790SStefan Roese if (area == ONENAND_SPARERAM)
13350752790SStefan Roese return mtd->oobsize;
13450752790SStefan Roese }
13550752790SStefan Roese
13650752790SStefan Roese return 0;
13750752790SStefan Roese }
13850752790SStefan Roese
ebi_read_bufferram(struct mtd_info * mtd,loff_t addr,int area,unsigned char * buffer,int offset,size_t count)13950752790SStefan Roese static int ebi_read_bufferram(struct mtd_info *mtd, loff_t addr, int area,
14050752790SStefan Roese unsigned char *buffer, int offset,
14150752790SStefan Roese size_t count)
14250752790SStefan Roese {
14350752790SStefan Roese struct onenand_chip *this = mtd->priv;
14450752790SStefan Roese void __iomem *bufferram;
14550752790SStefan Roese
14650752790SStefan Roese bufferram = this->base + area;
14750752790SStefan Roese bufferram += onenand_bufferram_offset(mtd, area);
14850752790SStefan Roese
14950752790SStefan Roese if (count < 4)
15050752790SStefan Roese memcpy_16_from_onenand(buffer, bufferram + offset, count);
15150752790SStefan Roese else
15250752790SStefan Roese memcpy_32_from_onenand(buffer, bufferram + offset, count);
15350752790SStefan Roese
15450752790SStefan Roese return 0;
15550752790SStefan Roese }
15650752790SStefan Roese
ebi_write_bufferram(struct mtd_info * mtd,loff_t addr,int area,const unsigned char * buffer,int offset,size_t count)15750752790SStefan Roese static int ebi_write_bufferram(struct mtd_info *mtd, loff_t addr, int area,
15850752790SStefan Roese const unsigned char *buffer, int offset,
15950752790SStefan Roese size_t count)
16050752790SStefan Roese {
16150752790SStefan Roese struct onenand_chip *this = mtd->priv;
16250752790SStefan Roese void __iomem *bufferram;
16350752790SStefan Roese
16450752790SStefan Roese bufferram = this->base + area;
16550752790SStefan Roese bufferram += onenand_bufferram_offset(mtd, area);
16650752790SStefan Roese
16750752790SStefan Roese memcpy_16_to_onenand(bufferram + offset, buffer, count);
16850752790SStefan Roese
16950752790SStefan Roese return 0;
17050752790SStefan Roese }
17150752790SStefan Roese
onenand_board_init(struct mtd_info * mtd)172*77b93e5eSLadislav Michl int onenand_board_init(struct mtd_info *mtd)
17350752790SStefan Roese {
17450752790SStefan Roese struct onenand_chip *chip = mtd->priv;
17550752790SStefan Roese
17650752790SStefan Roese /*
17750752790SStefan Roese * Insert board specific OneNAND access functions
17850752790SStefan Roese */
17950752790SStefan Roese chip->read_word = ebi_nand_read_word;
18050752790SStefan Roese chip->write_word = ebi_nand_write_word;
18150752790SStefan Roese
18250752790SStefan Roese chip->read_bufferram = ebi_read_bufferram;
18350752790SStefan Roese chip->write_bufferram = ebi_write_bufferram;
184*77b93e5eSLadislav Michl
185*77b93e5eSLadislav Michl return 0;
18650752790SStefan Roese }
187