1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
10 #include <fdt_support.h>
15 #include <linux/bitops.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 /* The STATUS register */
20 #define QUADSPI_SR_BP0 BIT(2)
21 #define QUADSPI_SR_BP1 BIT(3)
22 #define QUADSPI_SR_BP2 BIT(4)
23 #define QUADSPI_SR_BP2_0 GENMASK(4, 2)
24 #define QUADSPI_SR_BP3 BIT(6)
25 #define QUADSPI_SR_TB BIT(5)
28 * The QUADSPI_MEM_OP register is used to do memory protect and erase operations
30 #define QUADSPI_MEM_OP_BULK_ERASE 0x00000001
31 #define QUADSPI_MEM_OP_SECTOR_ERASE 0x00000002
32 #define QUADSPI_MEM_OP_SECTOR_PROTECT 0x00000003
35 * The QUADSPI_ISR register is used to determine whether an invalid write or
36 * erase operation trigerred an interrupt
38 #define QUADSPI_ISR_ILLEGAL_ERASE BIT(0)
39 #define QUADSPI_ISR_ILLEGAL_WRITE BIT(1)
41 struct altera_qspi_regs {
51 struct altera_qspi_platdata {
52 struct altera_qspi_regs *regs;
57 static uint flash_verbose;
58 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* FLASH chips info */
60 static void altera_qspi_get_locked_range(struct mtd_info *mtd, loff_t *ofs,
63 void flash_print_info(flash_info_t *info)
65 struct mtd_info *mtd = info->mtd;
69 printf("Altera QSPI flash Size: %ld MB in %d Sectors\n",
70 info->size >> 20, info->sector_count);
71 altera_qspi_get_locked_range(mtd, &ofs, &len);
72 printf(" %08lX +%lX", info->start[0], info->size);
74 printf(", protected %08llX +%llX",
75 info->start[0] + ofs, len);
80 void flash_set_verbose(uint v)
85 int flash_erase(flash_info_t *info, int s_first, int s_last)
87 struct mtd_info *mtd = info->mtd;
88 struct erase_info instr;
91 memset(&instr, 0, sizeof(instr));
93 instr.addr = mtd->erasesize * s_first;
94 instr.len = mtd->erasesize * (s_last + 1 - s_first);
96 ret = mtd_erase(mtd, &instr);
105 int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt)
107 struct mtd_info *mtd = info->mtd;
108 struct udevice *dev = mtd->dev;
109 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
110 ulong base = (ulong)pdata->base;
111 loff_t to = addr - base;
115 ret = mtd_write(mtd, to, cnt, &retlen, src);
117 return ERR_PROTECTED;
122 unsigned long flash_init(void)
126 /* probe every MTD device */
127 for (uclass_first_device(UCLASS_MTD, &dev);
129 uclass_next_device(&dev)) {
132 return flash_info[0].size;
135 static int altera_qspi_erase(struct mtd_info *mtd, struct erase_info *instr)
137 struct udevice *dev = mtd->dev;
138 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
139 struct altera_qspi_regs *regs = pdata->regs;
140 size_t addr = instr->addr;
141 size_t len = instr->len;
142 size_t end = addr + len;
147 instr->state = MTD_ERASING;
148 addr &= ~(mtd->erasesize - 1); /* get lower aligned address */
153 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
154 instr->state = MTD_ERASE_FAILED;
155 mtd_erase_callback(instr);
158 flash = pdata->base + addr;
159 last = pdata->base + addr + mtd->erasesize;
160 /* skip erase if sector is blank */
161 while (flash < last) {
162 if (readl(flash) != 0xffffffff)
167 sect = addr / mtd->erasesize;
169 sect |= QUADSPI_MEM_OP_SECTOR_ERASE;
170 debug("erase %08x\n", sect);
171 writel(sect, ®s->mem_op);
172 stat = readl(®s->isr);
173 if (stat & QUADSPI_ISR_ILLEGAL_ERASE) {
174 /* erase failed, sector might be protected */
175 debug("erase %08x fail %x\n", sect, stat);
176 writel(stat, ®s->isr); /* clear isr */
177 instr->fail_addr = addr;
178 instr->state = MTD_ERASE_FAILED;
179 mtd_erase_callback(instr);
188 addr += mtd->erasesize;
190 instr->state = MTD_ERASE_DONE;
191 mtd_erase_callback(instr);
196 static int altera_qspi_read(struct mtd_info *mtd, loff_t from, size_t len,
197 size_t *retlen, u_char *buf)
199 struct udevice *dev = mtd->dev;
200 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
202 memcpy_fromio(buf, pdata->base + from, len);
208 static int altera_qspi_write(struct mtd_info *mtd, loff_t to, size_t len,
209 size_t *retlen, const u_char *buf)
211 struct udevice *dev = mtd->dev;
212 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
213 struct altera_qspi_regs *regs = pdata->regs;
216 memcpy_toio(pdata->base + to, buf, len);
217 /* check whether write triggered a illegal write interrupt */
218 stat = readl(®s->isr);
219 if (stat & QUADSPI_ISR_ILLEGAL_WRITE) {
220 /* write failed, sector might be protected */
221 debug("write fail %x\n", stat);
222 writel(stat, ®s->isr); /* clear isr */
230 static void altera_qspi_sync(struct mtd_info *mtd)
234 static void altera_qspi_get_locked_range(struct mtd_info *mtd, loff_t *ofs,
237 struct udevice *dev = mtd->dev;
238 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
239 struct altera_qspi_regs *regs = pdata->regs;
240 int shift0 = ffs(QUADSPI_SR_BP2_0) - 1;
241 int shift3 = ffs(QUADSPI_SR_BP3) - 1 - 3;
242 u32 stat = readl(®s->rd_status);
243 unsigned pow = ((stat & QUADSPI_SR_BP2_0) >> shift0) |
244 ((stat & QUADSPI_SR_BP3) >> shift3);
249 *len = mtd->erasesize << (pow - 1);
250 if (*len > mtd->size)
252 if (!(stat & QUADSPI_SR_TB))
253 *ofs = mtd->size - *len;
257 static int altera_qspi_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
259 struct udevice *dev = mtd->dev;
260 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
261 struct altera_qspi_regs *regs = pdata->regs;
262 u32 sector_start, sector_end;
268 num_sectors = mtd->size / mtd->erasesize;
269 sector_start = ofs / mtd->erasesize;
270 sector_end = (ofs + len) / mtd->erasesize;
272 if (sector_start >= num_sectors / 2) {
273 sr_bp = fls(num_sectors - 1 - sector_start) + 1;
275 } else if (sector_end < num_sectors / 2) {
276 sr_bp = fls(sector_end) + 1;
283 mem_op = (sr_tb << 12) | (sr_bp << 8);
284 mem_op |= QUADSPI_MEM_OP_SECTOR_PROTECT;
285 debug("lock %08x\n", mem_op);
286 writel(mem_op, ®s->mem_op);
291 static int altera_qspi_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
293 struct udevice *dev = mtd->dev;
294 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
295 struct altera_qspi_regs *regs = pdata->regs;
298 mem_op = QUADSPI_MEM_OP_SECTOR_PROTECT;
299 debug("unlock %08x\n", mem_op);
300 writel(mem_op, ®s->mem_op);
305 static int altera_qspi_probe(struct udevice *dev)
307 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
308 struct altera_qspi_regs *regs = pdata->regs;
309 unsigned long base = (unsigned long)pdata->base;
310 struct mtd_info *mtd;
311 flash_info_t *flash = &flash_info[0];
315 rdid = readl(®s->rd_rdid);
316 debug("rdid %x\n", rdid);
318 mtd = dev_get_uclass_priv(dev);
321 mtd->type = MTD_NORFLASH;
322 mtd->flags = MTD_CAP_NORFLASH;
323 mtd->size = 1 << ((rdid & 0xff) - 6);
325 mtd->writebufsize = mtd->writesize;
326 mtd->_erase = altera_qspi_erase;
327 mtd->_read = altera_qspi_read;
328 mtd->_write = altera_qspi_write;
329 mtd->_sync = altera_qspi_sync;
330 mtd->_lock = altera_qspi_lock;
331 mtd->_unlock = altera_qspi_unlock;
332 mtd->numeraseregions = 0;
333 mtd->erasesize = 0x10000;
334 if (add_mtd_device(mtd))
338 flash->size = mtd->size;
339 flash->sector_count = mtd->size / mtd->erasesize;
340 flash->flash_id = rdid;
341 flash->start[0] = base;
342 for (i = 1; i < flash->sector_count; i++)
343 flash->start[i] = flash->start[i - 1] + mtd->erasesize;
344 gd->bd->bi_flashstart = base;
349 static int altera_qspi_ofdata_to_platdata(struct udevice *dev)
351 struct altera_qspi_platdata *pdata = dev_get_platdata(dev);
352 void *blob = (void *)gd->fdt_blob;
353 int node = dev_of_offset(dev);
354 const char *list, *end;
357 unsigned long addr, size;
358 int parent, addrc, sizec;
362 * decode regs. there are multiple reg tuples, and they need to
363 * match with reg-names.
365 parent = fdt_parent_offset(blob, node);
366 fdt_support_default_count_cells(blob, parent, &addrc, &sizec);
367 list = fdt_getprop(blob, node, "reg-names", &len);
371 cell = fdt_getprop(blob, node, "reg", &len);
376 addr = fdt_translate_address((void *)blob,
378 size = fdt_addr_to_cpu(cell[idx + addrc]);
379 base = map_physmem(addr, size, MAP_NOCACHE);
381 if (strcmp(list, "avl_csr") == 0) {
383 } else if (strcmp(list, "avl_mem") == 0) {
387 idx += addrc + sizec;
394 static const struct udevice_id altera_qspi_ids[] = {
395 { .compatible = "altr,quadspi-1.0" },
399 U_BOOT_DRIVER(altera_qspi) = {
400 .name = "altera_qspi",
402 .of_match = altera_qspi_ids,
403 .ofdata_to_platdata = altera_qspi_ofdata_to_platdata,
404 .platdata_auto = sizeof(struct altera_qspi_platdata),
405 .probe = altera_qspi_probe,