2 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
4 * SPDX-License-Identifier: GPL-2.0+
10 #include <fdt_support.h>
15 DECLARE_GLOBAL_DATA_PTR;
18 * The QUADSPI_MEM_OP register is used to do memory protect and erase operations
20 #define QUADSPI_MEM_OP_BULK_ERASE 0x00000001
21 #define QUADSPI_MEM_OP_SECTOR_ERASE 0x00000002
22 #define QUADSPI_MEM_OP_SECTOR_PROTECT 0x00000003
25 * The QUADSPI_ISR register is used to determine whether an invalid write or
26 * erase operation trigerred an interrupt
28 #define QUADSPI_ISR_ILLEGAL_ERASE BIT(0)
29 #define QUADSPI_ISR_ILLEGAL_WRITE BIT(1)
31 struct altera_qspi_regs {
41 struct altera_qspi_platdata {
42 struct altera_qspi_regs *regs;
47 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* FLASH chips info */
49 void flash_print_info(flash_info_t *info)
51 printf("Altera QSPI flash Size: %ld MB in %d Sectors\n",
52 info->size >> 20, info->sector_count);
55 int flash_erase(flash_info_t *info, int s_first, int s_last)
57 struct mtd_info *mtd = info->mtd;
58 struct erase_info instr;
61 memset(&instr, 0, sizeof(instr));
62 instr.addr = mtd->erasesize * s_first;
63 instr.len = mtd->erasesize * (s_last + 1 - s_first);
64 ret = mtd_erase(mtd, &instr);
66 return ERR_NOT_ERASED;
71 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
73 struct mtd_info *mtd = info->mtd;
74 struct udevice *dev = mtd->dev;
75 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
76 ulong base = (ulong)pdata->base;
77 loff_t to = addr - base;
81 ret = mtd_write(mtd, to, cnt, &retlen, src);
83 return ERR_NOT_ERASED;
88 unsigned long flash_init(void)
92 /* probe every MTD device */
93 for (uclass_first_device(UCLASS_MTD, &dev);
95 uclass_next_device(&dev)) {
98 return flash_info[0].size;
101 static int altera_qspi_erase(struct mtd_info *mtd, struct erase_info *instr)
103 struct udevice *dev = mtd->dev;
104 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
105 struct altera_qspi_regs *regs = pdata->regs;
106 size_t addr = instr->addr;
107 size_t len = instr->len;
108 size_t end = addr + len;
112 instr->state = MTD_ERASING;
113 addr &= ~(mtd->erasesize - 1); /* get lower aligned address */
115 sect = addr / mtd->erasesize;
117 sect |= QUADSPI_MEM_OP_SECTOR_ERASE;
118 debug("erase %08x\n", sect);
119 writel(sect, ®s->mem_op);
120 stat = readl(®s->isr);
121 if (stat & QUADSPI_ISR_ILLEGAL_ERASE) {
122 /* erase failed, sector might be protected */
123 debug("erase %08x fail %x\n", sect, stat);
124 writel(stat, ®s->isr); /* clear isr */
125 instr->state = MTD_ERASE_FAILED;
128 addr += mtd->erasesize;
130 instr->state = MTD_ERASE_DONE;
131 mtd_erase_callback(instr);
136 static int altera_qspi_read(struct mtd_info *mtd, loff_t from, size_t len,
137 size_t *retlen, u_char *buf)
139 struct udevice *dev = mtd->dev;
140 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
142 memcpy_fromio(buf, pdata->base + from, len);
148 static int altera_qspi_write(struct mtd_info *mtd, loff_t to, size_t len,
149 size_t *retlen, const u_char *buf)
151 struct udevice *dev = mtd->dev;
152 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
153 struct altera_qspi_regs *regs = pdata->regs;
156 memcpy_toio(pdata->base + to, buf, len);
157 /* check whether write triggered a illegal write interrupt */
158 stat = readl(®s->isr);
159 if (stat & QUADSPI_ISR_ILLEGAL_WRITE) {
160 /* write failed, sector might be protected */
161 debug("write fail %x\n", stat);
162 writel(stat, ®s->isr); /* clear isr */
170 static void altera_qspi_sync(struct mtd_info *mtd)
174 static int altera_qspi_probe(struct udevice *dev)
176 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
177 struct altera_qspi_regs *regs = pdata->regs;
178 unsigned long base = (unsigned long)pdata->base;
179 struct mtd_info *mtd;
180 flash_info_t *flash = &flash_info[0];
184 rdid = readl(®s->rd_rdid);
185 debug("rdid %x\n", rdid);
187 mtd = dev_get_uclass_priv(dev);
190 mtd->type = MTD_NORFLASH;
191 mtd->flags = MTD_CAP_NORFLASH;
192 mtd->size = 1 << ((rdid & 0xff) - 6);
194 mtd->writebufsize = mtd->writesize;
195 mtd->_erase = altera_qspi_erase;
196 mtd->_read = altera_qspi_read;
197 mtd->_write = altera_qspi_write;
198 mtd->_sync = altera_qspi_sync;
199 mtd->numeraseregions = 0;
200 mtd->erasesize = 0x10000;
201 if (add_mtd_device(mtd))
205 flash->size = mtd->size;
206 flash->sector_count = mtd->size / mtd->erasesize;
207 flash->flash_id = rdid;
208 flash->start[0] = base;
209 for (i = 1; i < flash->sector_count; i++)
210 flash->start[i] = flash->start[i - 1] + mtd->erasesize;
211 gd->bd->bi_flashstart = base;
216 static int altera_qspi_ofdata_to_platdata(struct udevice *dev)
218 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
219 void *blob = (void *)gd->fdt_blob;
220 int node = dev->of_offset;
221 const char *list, *end;
224 unsigned long addr, size;
225 int parent, addrc, sizec;
229 * decode regs. there are multiple reg tuples, and they need to
230 * match with reg-names.
232 parent = fdt_parent_offset(blob, node);
233 of_bus_default_count_cells(blob, parent, &addrc, &sizec);
234 list = fdt_getprop(blob, node, "reg-names", &len);
238 cell = fdt_getprop(blob, node, "reg", &len);
243 addr = fdt_translate_address((void *)blob,
245 size = fdt_addr_to_cpu(cell[idx + addrc]);
246 base = map_physmem(addr, size, MAP_NOCACHE);
248 if (strcmp(list, "avl_csr") == 0) {
250 } else if (strcmp(list, "avl_mem") == 0) {
254 idx += addrc + sizec;
261 static const struct udevice_id altera_qspi_ids[] = {
262 { .compatible = "altr,quadspi-1.0" },
266 U_BOOT_DRIVER(altera_qspi) = {
267 .name = "altera_qspi",
269 .of_match = altera_qspi_ids,
270 .ofdata_to_platdata = altera_qspi_ofdata_to_platdata,
271 .platdata_auto_alloc_size = sizeof(struct altera_qspi_platdata),
272 .probe = altera_qspi_probe,