xref: /openbmc/u-boot/drivers/mtd/altera_qspi.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
238a0f36eSThomas Chou /*
338a0f36eSThomas Chou  * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
438a0f36eSThomas Chou  */
538a0f36eSThomas Chou 
638a0f36eSThomas Chou #include <common.h>
78e8106dcSThomas Chou #include <console.h>
838a0f36eSThomas Chou #include <dm.h>
938a0f36eSThomas Chou #include <errno.h>
1038a0f36eSThomas Chou #include <fdt_support.h>
1138a0f36eSThomas Chou #include <flash.h>
1238a0f36eSThomas Chou #include <mtd.h>
1338a0f36eSThomas Chou #include <asm/io.h>
1438a0f36eSThomas Chou 
1538a0f36eSThomas Chou DECLARE_GLOBAL_DATA_PTR;
1638a0f36eSThomas Chou 
17421f306fSThomas Chou /* The STATUS register */
18421f306fSThomas Chou #define QUADSPI_SR_BP0				BIT(2)
19421f306fSThomas Chou #define QUADSPI_SR_BP1				BIT(3)
20421f306fSThomas Chou #define QUADSPI_SR_BP2				BIT(4)
21421f306fSThomas Chou #define QUADSPI_SR_BP2_0			GENMASK(4, 2)
22421f306fSThomas Chou #define QUADSPI_SR_BP3				BIT(6)
23421f306fSThomas Chou #define QUADSPI_SR_TB				BIT(5)
24421f306fSThomas Chou 
2538a0f36eSThomas Chou /*
2638a0f36eSThomas Chou  * The QUADSPI_MEM_OP register is used to do memory protect and erase operations
2738a0f36eSThomas Chou  */
2838a0f36eSThomas Chou #define QUADSPI_MEM_OP_BULK_ERASE		0x00000001
2938a0f36eSThomas Chou #define QUADSPI_MEM_OP_SECTOR_ERASE		0x00000002
3038a0f36eSThomas Chou #define QUADSPI_MEM_OP_SECTOR_PROTECT		0x00000003
3138a0f36eSThomas Chou 
3238a0f36eSThomas Chou /*
3338a0f36eSThomas Chou  * The QUADSPI_ISR register is used to determine whether an invalid write or
3438a0f36eSThomas Chou  * erase operation trigerred an interrupt
3538a0f36eSThomas Chou  */
3638a0f36eSThomas Chou #define QUADSPI_ISR_ILLEGAL_ERASE		BIT(0)
3738a0f36eSThomas Chou #define QUADSPI_ISR_ILLEGAL_WRITE		BIT(1)
3838a0f36eSThomas Chou 
3938a0f36eSThomas Chou struct altera_qspi_regs {
4038a0f36eSThomas Chou 	u32	rd_status;
4138a0f36eSThomas Chou 	u32	rd_sid;
4238a0f36eSThomas Chou 	u32	rd_rdid;
4338a0f36eSThomas Chou 	u32	mem_op;
4438a0f36eSThomas Chou 	u32	isr;
4538a0f36eSThomas Chou 	u32	imr;
4638a0f36eSThomas Chou 	u32	chip_select;
4738a0f36eSThomas Chou };
4838a0f36eSThomas Chou 
4938a0f36eSThomas Chou struct altera_qspi_platdata {
5038a0f36eSThomas Chou 	struct altera_qspi_regs *regs;
5138a0f36eSThomas Chou 	void *base;
5238a0f36eSThomas Chou 	unsigned long size;
5338a0f36eSThomas Chou };
5438a0f36eSThomas Chou 
55d579d38fSThomas Chou static uint flash_verbose;
5638a0f36eSThomas Chou flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];	/* FLASH chips info */
5738a0f36eSThomas Chou 
58421f306fSThomas Chou static void altera_qspi_get_locked_range(struct mtd_info *mtd, loff_t *ofs,
59421f306fSThomas Chou 					 uint64_t *len);
60421f306fSThomas Chou 
flash_print_info(flash_info_t * info)6138a0f36eSThomas Chou void flash_print_info(flash_info_t *info)
6238a0f36eSThomas Chou {
63421f306fSThomas Chou 	struct mtd_info *mtd = info->mtd;
64421f306fSThomas Chou 	loff_t ofs;
65421f306fSThomas Chou 	u64 len;
66421f306fSThomas Chou 
6738a0f36eSThomas Chou 	printf("Altera QSPI flash  Size: %ld MB in %d Sectors\n",
6838a0f36eSThomas Chou 	       info->size >> 20, info->sector_count);
69421f306fSThomas Chou 	altera_qspi_get_locked_range(mtd, &ofs, &len);
70421f306fSThomas Chou 	printf("  %08lX +%lX", info->start[0], info->size);
71421f306fSThomas Chou 	if (len) {
72421f306fSThomas Chou 		printf(", protected %08llX +%llX",
73421f306fSThomas Chou 		       info->start[0] + ofs, len);
74421f306fSThomas Chou 	}
75421f306fSThomas Chou 	putc('\n');
7638a0f36eSThomas Chou }
7738a0f36eSThomas Chou 
flash_set_verbose(uint v)78d579d38fSThomas Chou void flash_set_verbose(uint v)
79d579d38fSThomas Chou {
80d579d38fSThomas Chou 	flash_verbose = v;
81d579d38fSThomas Chou }
82d579d38fSThomas Chou 
flash_erase(flash_info_t * info,int s_first,int s_last)8338a0f36eSThomas Chou int flash_erase(flash_info_t *info, int s_first, int s_last)
8438a0f36eSThomas Chou {
8538a0f36eSThomas Chou 	struct mtd_info *mtd = info->mtd;
8638a0f36eSThomas Chou 	struct erase_info instr;
8738a0f36eSThomas Chou 	int ret;
8838a0f36eSThomas Chou 
8938a0f36eSThomas Chou 	memset(&instr, 0, sizeof(instr));
901c0e84caSThomas Chou 	instr.mtd = mtd;
9138a0f36eSThomas Chou 	instr.addr = mtd->erasesize * s_first;
9238a0f36eSThomas Chou 	instr.len = mtd->erasesize * (s_last + 1 - s_first);
93d579d38fSThomas Chou 	flash_set_verbose(1);
9438a0f36eSThomas Chou 	ret = mtd_erase(mtd, &instr);
95d579d38fSThomas Chou 	flash_set_verbose(0);
9638a0f36eSThomas Chou 	if (ret)
97f118fe5cSThomas Chou 		return ERR_PROTECTED;
9838a0f36eSThomas Chou 
99d579d38fSThomas Chou 	puts(" done\n");
10038a0f36eSThomas Chou 	return 0;
10138a0f36eSThomas Chou }
10238a0f36eSThomas Chou 
write_buff(flash_info_t * info,uchar * src,ulong addr,ulong cnt)10338a0f36eSThomas Chou int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
10438a0f36eSThomas Chou {
10538a0f36eSThomas Chou 	struct mtd_info *mtd = info->mtd;
10638a0f36eSThomas Chou 	struct udevice *dev = mtd->dev;
10738a0f36eSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
10838a0f36eSThomas Chou 	ulong base = (ulong)pdata->base;
10938a0f36eSThomas Chou 	loff_t to = addr - base;
11038a0f36eSThomas Chou 	size_t retlen;
11138a0f36eSThomas Chou 	int ret;
11238a0f36eSThomas Chou 
11338a0f36eSThomas Chou 	ret = mtd_write(mtd, to, cnt, &retlen, src);
11438a0f36eSThomas Chou 	if (ret)
115f118fe5cSThomas Chou 		return ERR_PROTECTED;
11638a0f36eSThomas Chou 
11738a0f36eSThomas Chou 	return 0;
11838a0f36eSThomas Chou }
11938a0f36eSThomas Chou 
flash_init(void)12038a0f36eSThomas Chou unsigned long flash_init(void)
12138a0f36eSThomas Chou {
12238a0f36eSThomas Chou 	struct udevice *dev;
12338a0f36eSThomas Chou 
12438a0f36eSThomas Chou 	/* probe every MTD device */
12538a0f36eSThomas Chou 	for (uclass_first_device(UCLASS_MTD, &dev);
12638a0f36eSThomas Chou 	     dev;
12738a0f36eSThomas Chou 	     uclass_next_device(&dev)) {
12838a0f36eSThomas Chou 	}
12938a0f36eSThomas Chou 
13038a0f36eSThomas Chou 	return flash_info[0].size;
13138a0f36eSThomas Chou }
13238a0f36eSThomas Chou 
altera_qspi_erase(struct mtd_info * mtd,struct erase_info * instr)13338a0f36eSThomas Chou static int altera_qspi_erase(struct mtd_info *mtd, struct erase_info *instr)
13438a0f36eSThomas Chou {
13538a0f36eSThomas Chou 	struct udevice *dev = mtd->dev;
13638a0f36eSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
13738a0f36eSThomas Chou 	struct altera_qspi_regs *regs = pdata->regs;
13838a0f36eSThomas Chou 	size_t addr = instr->addr;
13938a0f36eSThomas Chou 	size_t len = instr->len;
14038a0f36eSThomas Chou 	size_t end = addr + len;
14138a0f36eSThomas Chou 	u32 sect;
14238a0f36eSThomas Chou 	u32 stat;
143f81a673eSThomas Chou 	u32 *flash, *last;
14438a0f36eSThomas Chou 
14538a0f36eSThomas Chou 	instr->state = MTD_ERASING;
14638a0f36eSThomas Chou 	addr &= ~(mtd->erasesize - 1); /* get lower aligned address */
14738a0f36eSThomas Chou 	while (addr < end) {
1488e8106dcSThomas Chou 		if (ctrlc()) {
1498e8106dcSThomas Chou 			if (flash_verbose)
1508e8106dcSThomas Chou 				putc('\n');
1518e8106dcSThomas Chou 			instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
1528e8106dcSThomas Chou 			instr->state = MTD_ERASE_FAILED;
1538e8106dcSThomas Chou 			mtd_erase_callback(instr);
1548e8106dcSThomas Chou 			return -EIO;
1558e8106dcSThomas Chou 		}
156f81a673eSThomas Chou 		flash = pdata->base + addr;
157f81a673eSThomas Chou 		last = pdata->base + addr + mtd->erasesize;
158f81a673eSThomas Chou 		/* skip erase if sector is blank */
159f81a673eSThomas Chou 		while (flash < last) {
160f81a673eSThomas Chou 			if (readl(flash) != 0xffffffff)
161f81a673eSThomas Chou 				break;
162f81a673eSThomas Chou 			flash++;
163f81a673eSThomas Chou 		}
164f81a673eSThomas Chou 		if (flash < last) {
16538a0f36eSThomas Chou 			sect = addr / mtd->erasesize;
16638a0f36eSThomas Chou 			sect <<= 8;
16738a0f36eSThomas Chou 			sect |= QUADSPI_MEM_OP_SECTOR_ERASE;
16838a0f36eSThomas Chou 			debug("erase %08x\n", sect);
16938a0f36eSThomas Chou 			writel(sect, &regs->mem_op);
17038a0f36eSThomas Chou 			stat = readl(&regs->isr);
17138a0f36eSThomas Chou 			if (stat & QUADSPI_ISR_ILLEGAL_ERASE) {
17238a0f36eSThomas Chou 				/* erase failed, sector might be protected */
17338a0f36eSThomas Chou 				debug("erase %08x fail %x\n", sect, stat);
17438a0f36eSThomas Chou 				writel(stat, &regs->isr); /* clear isr */
175a1b1d7ecSThomas Chou 				instr->fail_addr = addr;
17638a0f36eSThomas Chou 				instr->state = MTD_ERASE_FAILED;
1779e957aa4SThomas Chou 				mtd_erase_callback(instr);
17838a0f36eSThomas Chou 				return -EIO;
17938a0f36eSThomas Chou 			}
180d579d38fSThomas Chou 			if (flash_verbose)
181d579d38fSThomas Chou 				putc('.');
182d579d38fSThomas Chou 		} else {
183d579d38fSThomas Chou 			if (flash_verbose)
184d579d38fSThomas Chou 				putc(',');
185f81a673eSThomas Chou 		}
18638a0f36eSThomas Chou 		addr += mtd->erasesize;
18738a0f36eSThomas Chou 	}
18838a0f36eSThomas Chou 	instr->state = MTD_ERASE_DONE;
18938a0f36eSThomas Chou 	mtd_erase_callback(instr);
19038a0f36eSThomas Chou 
19138a0f36eSThomas Chou 	return 0;
19238a0f36eSThomas Chou }
19338a0f36eSThomas Chou 
altera_qspi_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)19438a0f36eSThomas Chou static int altera_qspi_read(struct mtd_info *mtd, loff_t from, size_t len,
19538a0f36eSThomas Chou 			    size_t *retlen, u_char *buf)
19638a0f36eSThomas Chou {
19738a0f36eSThomas Chou 	struct udevice *dev = mtd->dev;
19838a0f36eSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
19938a0f36eSThomas Chou 
20038a0f36eSThomas Chou 	memcpy_fromio(buf, pdata->base + from, len);
20138a0f36eSThomas Chou 	*retlen = len;
20238a0f36eSThomas Chou 
20338a0f36eSThomas Chou 	return 0;
20438a0f36eSThomas Chou }
20538a0f36eSThomas Chou 
altera_qspi_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)20638a0f36eSThomas Chou static int altera_qspi_write(struct mtd_info *mtd, loff_t to, size_t len,
20738a0f36eSThomas Chou 			     size_t *retlen, const u_char *buf)
20838a0f36eSThomas Chou {
20938a0f36eSThomas Chou 	struct udevice *dev = mtd->dev;
21038a0f36eSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
21138a0f36eSThomas Chou 	struct altera_qspi_regs *regs = pdata->regs;
21238a0f36eSThomas Chou 	u32 stat;
21338a0f36eSThomas Chou 
21438a0f36eSThomas Chou 	memcpy_toio(pdata->base + to, buf, len);
21538a0f36eSThomas Chou 	/* check whether write triggered a illegal write interrupt */
21638a0f36eSThomas Chou 	stat = readl(&regs->isr);
21738a0f36eSThomas Chou 	if (stat & QUADSPI_ISR_ILLEGAL_WRITE) {
21838a0f36eSThomas Chou 		/* write failed, sector might be protected */
21938a0f36eSThomas Chou 		debug("write fail %x\n", stat);
22038a0f36eSThomas Chou 		writel(stat, &regs->isr); /* clear isr */
22138a0f36eSThomas Chou 		return -EIO;
22238a0f36eSThomas Chou 	}
22338a0f36eSThomas Chou 	*retlen = len;
22438a0f36eSThomas Chou 
22538a0f36eSThomas Chou 	return 0;
22638a0f36eSThomas Chou }
22738a0f36eSThomas Chou 
altera_qspi_sync(struct mtd_info * mtd)22838a0f36eSThomas Chou static void altera_qspi_sync(struct mtd_info *mtd)
22938a0f36eSThomas Chou {
23038a0f36eSThomas Chou }
23138a0f36eSThomas Chou 
altera_qspi_get_locked_range(struct mtd_info * mtd,loff_t * ofs,uint64_t * len)232421f306fSThomas Chou static void altera_qspi_get_locked_range(struct mtd_info *mtd, loff_t *ofs,
233421f306fSThomas Chou 					 uint64_t *len)
234421f306fSThomas Chou {
235421f306fSThomas Chou 	struct udevice *dev = mtd->dev;
236421f306fSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
237421f306fSThomas Chou 	struct altera_qspi_regs *regs = pdata->regs;
238421f306fSThomas Chou 	int shift0 = ffs(QUADSPI_SR_BP2_0) - 1;
239421f306fSThomas Chou 	int shift3 = ffs(QUADSPI_SR_BP3) - 1 - 3;
240421f306fSThomas Chou 	u32 stat = readl(&regs->rd_status);
241421f306fSThomas Chou 	unsigned pow = ((stat & QUADSPI_SR_BP2_0) >> shift0) |
242421f306fSThomas Chou 		((stat & QUADSPI_SR_BP3) >> shift3);
243421f306fSThomas Chou 
244421f306fSThomas Chou 	*ofs = 0;
245421f306fSThomas Chou 	*len = 0;
246421f306fSThomas Chou 	if (pow) {
247421f306fSThomas Chou 		*len = mtd->erasesize << (pow - 1);
248421f306fSThomas Chou 		if (*len > mtd->size)
249421f306fSThomas Chou 			*len = mtd->size;
250421f306fSThomas Chou 		if (!(stat & QUADSPI_SR_TB))
251421f306fSThomas Chou 			*ofs = mtd->size - *len;
252421f306fSThomas Chou 	}
253421f306fSThomas Chou }
254421f306fSThomas Chou 
altera_qspi_lock(struct mtd_info * mtd,loff_t ofs,uint64_t len)255421f306fSThomas Chou static int altera_qspi_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
256421f306fSThomas Chou {
257421f306fSThomas Chou 	struct udevice *dev = mtd->dev;
258421f306fSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
259421f306fSThomas Chou 	struct altera_qspi_regs *regs = pdata->regs;
260421f306fSThomas Chou 	u32 sector_start, sector_end;
261421f306fSThomas Chou 	u32 num_sectors;
262421f306fSThomas Chou 	u32 mem_op;
263421f306fSThomas Chou 	u32 sr_bp;
264421f306fSThomas Chou 	u32 sr_tb;
265421f306fSThomas Chou 
266421f306fSThomas Chou 	num_sectors = mtd->size / mtd->erasesize;
267421f306fSThomas Chou 	sector_start = ofs / mtd->erasesize;
268421f306fSThomas Chou 	sector_end = (ofs + len) / mtd->erasesize;
269421f306fSThomas Chou 
270421f306fSThomas Chou 	if (sector_start >= num_sectors / 2) {
271421f306fSThomas Chou 		sr_bp = fls(num_sectors - 1 - sector_start) + 1;
272421f306fSThomas Chou 		sr_tb = 0;
273421f306fSThomas Chou 	} else if (sector_end < num_sectors / 2) {
274421f306fSThomas Chou 		sr_bp = fls(sector_end) + 1;
275421f306fSThomas Chou 		sr_tb = 1;
276421f306fSThomas Chou 	} else {
277421f306fSThomas Chou 		sr_bp = 15;
278421f306fSThomas Chou 		sr_tb = 0;
279421f306fSThomas Chou 	}
280421f306fSThomas Chou 
281421f306fSThomas Chou 	mem_op = (sr_tb << 12) | (sr_bp << 8);
282421f306fSThomas Chou 	mem_op |= QUADSPI_MEM_OP_SECTOR_PROTECT;
283421f306fSThomas Chou 	debug("lock %08x\n", mem_op);
284421f306fSThomas Chou 	writel(mem_op, &regs->mem_op);
285421f306fSThomas Chou 
286421f306fSThomas Chou 	return 0;
287421f306fSThomas Chou }
288421f306fSThomas Chou 
altera_qspi_unlock(struct mtd_info * mtd,loff_t ofs,uint64_t len)289421f306fSThomas Chou static int altera_qspi_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
290421f306fSThomas Chou {
291421f306fSThomas Chou 	struct udevice *dev = mtd->dev;
292421f306fSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
293421f306fSThomas Chou 	struct altera_qspi_regs *regs = pdata->regs;
294421f306fSThomas Chou 	u32 mem_op;
295421f306fSThomas Chou 
296421f306fSThomas Chou 	mem_op = QUADSPI_MEM_OP_SECTOR_PROTECT;
297421f306fSThomas Chou 	debug("unlock %08x\n", mem_op);
298421f306fSThomas Chou 	writel(mem_op, &regs->mem_op);
299421f306fSThomas Chou 
300421f306fSThomas Chou 	return 0;
301421f306fSThomas Chou }
302421f306fSThomas Chou 
altera_qspi_probe(struct udevice * dev)30338a0f36eSThomas Chou static int altera_qspi_probe(struct udevice *dev)
30438a0f36eSThomas Chou {
30538a0f36eSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
30638a0f36eSThomas Chou 	struct altera_qspi_regs *regs = pdata->regs;
30738a0f36eSThomas Chou 	unsigned long base = (unsigned long)pdata->base;
30838a0f36eSThomas Chou 	struct mtd_info *mtd;
30938a0f36eSThomas Chou 	flash_info_t *flash = &flash_info[0];
31038a0f36eSThomas Chou 	u32 rdid;
31138a0f36eSThomas Chou 	int i;
31238a0f36eSThomas Chou 
31338a0f36eSThomas Chou 	rdid = readl(&regs->rd_rdid);
31438a0f36eSThomas Chou 	debug("rdid %x\n", rdid);
31538a0f36eSThomas Chou 
31638a0f36eSThomas Chou 	mtd = dev_get_uclass_priv(dev);
31738a0f36eSThomas Chou 	mtd->dev = dev;
31838a0f36eSThomas Chou 	mtd->name		= "nor0";
31938a0f36eSThomas Chou 	mtd->type		= MTD_NORFLASH;
32038a0f36eSThomas Chou 	mtd->flags		= MTD_CAP_NORFLASH;
32138a0f36eSThomas Chou 	mtd->size		= 1 << ((rdid & 0xff) - 6);
32238a0f36eSThomas Chou 	mtd->writesize		= 1;
32338a0f36eSThomas Chou 	mtd->writebufsize	= mtd->writesize;
32438a0f36eSThomas Chou 	mtd->_erase		= altera_qspi_erase;
32538a0f36eSThomas Chou 	mtd->_read		= altera_qspi_read;
32638a0f36eSThomas Chou 	mtd->_write		= altera_qspi_write;
32738a0f36eSThomas Chou 	mtd->_sync		= altera_qspi_sync;
328421f306fSThomas Chou 	mtd->_lock		= altera_qspi_lock;
329421f306fSThomas Chou 	mtd->_unlock		= altera_qspi_unlock;
33038a0f36eSThomas Chou 	mtd->numeraseregions = 0;
33138a0f36eSThomas Chou 	mtd->erasesize = 0x10000;
33238a0f36eSThomas Chou 	if (add_mtd_device(mtd))
33338a0f36eSThomas Chou 		return -ENOMEM;
33438a0f36eSThomas Chou 
33538a0f36eSThomas Chou 	flash->mtd = mtd;
33638a0f36eSThomas Chou 	flash->size = mtd->size;
33738a0f36eSThomas Chou 	flash->sector_count = mtd->size / mtd->erasesize;
33838a0f36eSThomas Chou 	flash->flash_id = rdid;
33938a0f36eSThomas Chou 	flash->start[0] = base;
34038a0f36eSThomas Chou 	for (i = 1; i < flash->sector_count; i++)
34138a0f36eSThomas Chou 		flash->start[i] = flash->start[i - 1] + mtd->erasesize;
34238a0f36eSThomas Chou 	gd->bd->bi_flashstart = base;
34338a0f36eSThomas Chou 
34438a0f36eSThomas Chou 	return 0;
34538a0f36eSThomas Chou }
34638a0f36eSThomas Chou 
altera_qspi_ofdata_to_platdata(struct udevice * dev)34738a0f36eSThomas Chou static int altera_qspi_ofdata_to_platdata(struct udevice *dev)
34838a0f36eSThomas Chou {
34938a0f36eSThomas Chou 	struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
35038a0f36eSThomas Chou 	void *blob = (void *)gd->fdt_blob;
351e160f7d4SSimon Glass 	int node = dev_of_offset(dev);
35238a0f36eSThomas Chou 	const char *list, *end;
35338a0f36eSThomas Chou 	const fdt32_t *cell;
35438a0f36eSThomas Chou 	void *base;
35538a0f36eSThomas Chou 	unsigned long addr, size;
35638a0f36eSThomas Chou 	int parent, addrc, sizec;
35738a0f36eSThomas Chou 	int len, idx;
35838a0f36eSThomas Chou 
35938a0f36eSThomas Chou 	/*
36038a0f36eSThomas Chou 	 * decode regs. there are multiple reg tuples, and they need to
36138a0f36eSThomas Chou 	 * match with reg-names.
36238a0f36eSThomas Chou 	 */
36338a0f36eSThomas Chou 	parent = fdt_parent_offset(blob, node);
364eed36609SSimon Glass 	fdt_support_default_count_cells(blob, parent, &addrc, &sizec);
36538a0f36eSThomas Chou 	list = fdt_getprop(blob, node, "reg-names", &len);
36638a0f36eSThomas Chou 	if (!list)
36738a0f36eSThomas Chou 		return -ENOENT;
36838a0f36eSThomas Chou 	end = list + len;
36938a0f36eSThomas Chou 	cell = fdt_getprop(blob, node, "reg", &len);
37038a0f36eSThomas Chou 	if (!cell)
37138a0f36eSThomas Chou 		return -ENOENT;
37238a0f36eSThomas Chou 	idx = 0;
37338a0f36eSThomas Chou 	while (list < end) {
37438a0f36eSThomas Chou 		addr = fdt_translate_address((void *)blob,
37538a0f36eSThomas Chou 					     node, cell + idx);
37638a0f36eSThomas Chou 		size = fdt_addr_to_cpu(cell[idx + addrc]);
3778ed38fa5SThomas Chou 		base = map_physmem(addr, size, MAP_NOCACHE);
37838a0f36eSThomas Chou 		len = strlen(list);
37938a0f36eSThomas Chou 		if (strcmp(list, "avl_csr") == 0) {
38038a0f36eSThomas Chou 			pdata->regs = base;
38138a0f36eSThomas Chou 		} else if (strcmp(list, "avl_mem") == 0) {
38238a0f36eSThomas Chou 			pdata->base = base;
38338a0f36eSThomas Chou 			pdata->size = size;
38438a0f36eSThomas Chou 		}
38538a0f36eSThomas Chou 		idx += addrc + sizec;
38638a0f36eSThomas Chou 		list += (len + 1);
38738a0f36eSThomas Chou 	}
38838a0f36eSThomas Chou 
38938a0f36eSThomas Chou 	return 0;
39038a0f36eSThomas Chou }
39138a0f36eSThomas Chou 
39238a0f36eSThomas Chou static const struct udevice_id altera_qspi_ids[] = {
39338a0f36eSThomas Chou 	{ .compatible = "altr,quadspi-1.0" },
39438a0f36eSThomas Chou 	{}
39538a0f36eSThomas Chou };
39638a0f36eSThomas Chou 
39738a0f36eSThomas Chou U_BOOT_DRIVER(altera_qspi) = {
39838a0f36eSThomas Chou 	.name	= "altera_qspi",
39938a0f36eSThomas Chou 	.id	= UCLASS_MTD,
40038a0f36eSThomas Chou 	.of_match = altera_qspi_ids,
40138a0f36eSThomas Chou 	.ofdata_to_platdata = altera_qspi_ofdata_to_platdata,
40238a0f36eSThomas Chou 	.platdata_auto_alloc_size = sizeof(struct altera_qspi_platdata),
40338a0f36eSThomas Chou 	.probe	= altera_qspi_probe,
40438a0f36eSThomas Chou };
405