Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
| 10 | #include <fdt_support.h> |
| 11 | #include <flash.h> |
| 12 | #include <mtd.h> |
| 13 | #include <asm/io.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Thomas Chou | c92ff91 | 2015-12-01 17:00:22 +0800 | [diff] [blame] | 17 | /* The STATUS register */ |
| 18 | #define QUADSPI_SR_BP0 BIT(2) |
| 19 | #define QUADSPI_SR_BP1 BIT(3) |
| 20 | #define QUADSPI_SR_BP2 BIT(4) |
| 21 | #define QUADSPI_SR_BP2_0 GENMASK(4, 2) |
| 22 | #define QUADSPI_SR_BP3 BIT(6) |
| 23 | #define QUADSPI_SR_TB BIT(5) |
| 24 | |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 25 | /* |
| 26 | * The QUADSPI_MEM_OP register is used to do memory protect and erase operations |
| 27 | */ |
| 28 | #define QUADSPI_MEM_OP_BULK_ERASE 0x00000001 |
| 29 | #define QUADSPI_MEM_OP_SECTOR_ERASE 0x00000002 |
| 30 | #define QUADSPI_MEM_OP_SECTOR_PROTECT 0x00000003 |
| 31 | |
| 32 | /* |
| 33 | * The QUADSPI_ISR register is used to determine whether an invalid write or |
| 34 | * erase operation trigerred an interrupt |
| 35 | */ |
| 36 | #define QUADSPI_ISR_ILLEGAL_ERASE BIT(0) |
| 37 | #define QUADSPI_ISR_ILLEGAL_WRITE BIT(1) |
| 38 | |
| 39 | struct altera_qspi_regs { |
| 40 | u32 rd_status; |
| 41 | u32 rd_sid; |
| 42 | u32 rd_rdid; |
| 43 | u32 mem_op; |
| 44 | u32 isr; |
| 45 | u32 imr; |
| 46 | u32 chip_select; |
| 47 | }; |
| 48 | |
| 49 | struct altera_qspi_platdata { |
| 50 | struct altera_qspi_regs *regs; |
| 51 | void *base; |
| 52 | unsigned long size; |
| 53 | }; |
| 54 | |
| 55 | flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS]; /* FLASH chips info */ |
| 56 | |
Thomas Chou | c92ff91 | 2015-12-01 17:00:22 +0800 | [diff] [blame] | 57 | static void altera_qspi_get_locked_range(struct mtd_info *mtd, loff_t *ofs, |
| 58 | uint64_t *len); |
| 59 | |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 60 | void flash_print_info(flash_info_t *info) |
| 61 | { |
Thomas Chou | c92ff91 | 2015-12-01 17:00:22 +0800 | [diff] [blame] | 62 | struct mtd_info *mtd = info->mtd; |
| 63 | loff_t ofs; |
| 64 | u64 len; |
| 65 | |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 66 | printf("Altera QSPI flash Size: %ld MB in %d Sectors\n", |
| 67 | info->size >> 20, info->sector_count); |
Thomas Chou | c92ff91 | 2015-12-01 17:00:22 +0800 | [diff] [blame] | 68 | altera_qspi_get_locked_range(mtd, &ofs, &len); |
| 69 | printf(" %08lX +%lX", info->start[0], info->size); |
| 70 | if (len) { |
| 71 | printf(", protected %08llX +%llX", |
| 72 | info->start[0] + ofs, len); |
| 73 | } |
| 74 | putc('\n'); |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int flash_erase(flash_info_t *info, int s_first, int s_last) |
| 78 | { |
| 79 | struct mtd_info *mtd = info->mtd; |
| 80 | struct erase_info instr; |
| 81 | int ret; |
| 82 | |
| 83 | memset(&instr, 0, sizeof(instr)); |
Thomas Chou | c9830b5 | 2015-12-18 21:35:08 +0800 | [diff] [blame] | 84 | instr.mtd = mtd; |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 85 | instr.addr = mtd->erasesize * s_first; |
| 86 | instr.len = mtd->erasesize * (s_last + 1 - s_first); |
| 87 | ret = mtd_erase(mtd, &instr); |
| 88 | if (ret) |
Thomas Chou | 176e7d2 | 2015-12-01 16:18:20 +0800 | [diff] [blame] | 89 | return ERR_PROTECTED; |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int write_buff(flash_info_t *info, uchar *src, ulong addr, ulong cnt) |
| 95 | { |
| 96 | struct mtd_info *mtd = info->mtd; |
| 97 | struct udevice *dev = mtd->dev; |
| 98 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 99 | ulong base = (ulong)pdata->base; |
| 100 | loff_t to = addr - base; |
| 101 | size_t retlen; |
| 102 | int ret; |
| 103 | |
| 104 | ret = mtd_write(mtd, to, cnt, &retlen, src); |
| 105 | if (ret) |
Thomas Chou | 176e7d2 | 2015-12-01 16:18:20 +0800 | [diff] [blame] | 106 | return ERR_PROTECTED; |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | unsigned long flash_init(void) |
| 112 | { |
| 113 | struct udevice *dev; |
| 114 | |
| 115 | /* probe every MTD device */ |
| 116 | for (uclass_first_device(UCLASS_MTD, &dev); |
| 117 | dev; |
| 118 | uclass_next_device(&dev)) { |
| 119 | } |
| 120 | |
| 121 | return flash_info[0].size; |
| 122 | } |
| 123 | |
| 124 | static int altera_qspi_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 125 | { |
| 126 | struct udevice *dev = mtd->dev; |
| 127 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 128 | struct altera_qspi_regs *regs = pdata->regs; |
| 129 | size_t addr = instr->addr; |
| 130 | size_t len = instr->len; |
| 131 | size_t end = addr + len; |
| 132 | u32 sect; |
| 133 | u32 stat; |
| 134 | |
| 135 | instr->state = MTD_ERASING; |
| 136 | addr &= ~(mtd->erasesize - 1); /* get lower aligned address */ |
| 137 | while (addr < end) { |
| 138 | sect = addr / mtd->erasesize; |
| 139 | sect <<= 8; |
| 140 | sect |= QUADSPI_MEM_OP_SECTOR_ERASE; |
| 141 | debug("erase %08x\n", sect); |
| 142 | writel(sect, ®s->mem_op); |
| 143 | stat = readl(®s->isr); |
| 144 | if (stat & QUADSPI_ISR_ILLEGAL_ERASE) { |
| 145 | /* erase failed, sector might be protected */ |
| 146 | debug("erase %08x fail %x\n", sect, stat); |
| 147 | writel(stat, ®s->isr); /* clear isr */ |
| 148 | instr->state = MTD_ERASE_FAILED; |
| 149 | return -EIO; |
| 150 | } |
| 151 | addr += mtd->erasesize; |
| 152 | } |
| 153 | instr->state = MTD_ERASE_DONE; |
| 154 | mtd_erase_callback(instr); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | static int altera_qspi_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 160 | size_t *retlen, u_char *buf) |
| 161 | { |
| 162 | struct udevice *dev = mtd->dev; |
| 163 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 164 | |
| 165 | memcpy_fromio(buf, pdata->base + from, len); |
| 166 | *retlen = len; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static int altera_qspi_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 172 | size_t *retlen, const u_char *buf) |
| 173 | { |
| 174 | struct udevice *dev = mtd->dev; |
| 175 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 176 | struct altera_qspi_regs *regs = pdata->regs; |
| 177 | u32 stat; |
| 178 | |
| 179 | memcpy_toio(pdata->base + to, buf, len); |
| 180 | /* check whether write triggered a illegal write interrupt */ |
| 181 | stat = readl(®s->isr); |
| 182 | if (stat & QUADSPI_ISR_ILLEGAL_WRITE) { |
| 183 | /* write failed, sector might be protected */ |
| 184 | debug("write fail %x\n", stat); |
| 185 | writel(stat, ®s->isr); /* clear isr */ |
| 186 | return -EIO; |
| 187 | } |
| 188 | *retlen = len; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static void altera_qspi_sync(struct mtd_info *mtd) |
| 194 | { |
Thomas Chou | c92ff91 | 2015-12-01 17:00:22 +0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static void altera_qspi_get_locked_range(struct mtd_info *mtd, loff_t *ofs, |
| 198 | uint64_t *len) |
| 199 | { |
| 200 | struct udevice *dev = mtd->dev; |
| 201 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 202 | struct altera_qspi_regs *regs = pdata->regs; |
| 203 | int shift0 = ffs(QUADSPI_SR_BP2_0) - 1; |
| 204 | int shift3 = ffs(QUADSPI_SR_BP3) - 1 - 3; |
| 205 | u32 stat = readl(®s->rd_status); |
| 206 | unsigned pow = ((stat & QUADSPI_SR_BP2_0) >> shift0) | |
| 207 | ((stat & QUADSPI_SR_BP3) >> shift3); |
| 208 | |
| 209 | *ofs = 0; |
| 210 | *len = 0; |
| 211 | if (pow) { |
| 212 | *len = mtd->erasesize << (pow - 1); |
| 213 | if (*len > mtd->size) |
| 214 | *len = mtd->size; |
| 215 | if (!(stat & QUADSPI_SR_TB)) |
| 216 | *ofs = mtd->size - *len; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | static int altera_qspi_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 221 | { |
| 222 | struct udevice *dev = mtd->dev; |
| 223 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 224 | struct altera_qspi_regs *regs = pdata->regs; |
| 225 | u32 sector_start, sector_end; |
| 226 | u32 num_sectors; |
| 227 | u32 mem_op; |
| 228 | u32 sr_bp; |
| 229 | u32 sr_tb; |
| 230 | |
| 231 | num_sectors = mtd->size / mtd->erasesize; |
| 232 | sector_start = ofs / mtd->erasesize; |
| 233 | sector_end = (ofs + len) / mtd->erasesize; |
| 234 | |
| 235 | if (sector_start >= num_sectors / 2) { |
| 236 | sr_bp = fls(num_sectors - 1 - sector_start) + 1; |
| 237 | sr_tb = 0; |
| 238 | } else if (sector_end < num_sectors / 2) { |
| 239 | sr_bp = fls(sector_end) + 1; |
| 240 | sr_tb = 1; |
| 241 | } else { |
| 242 | sr_bp = 15; |
| 243 | sr_tb = 0; |
| 244 | } |
| 245 | |
| 246 | mem_op = (sr_tb << 12) | (sr_bp << 8); |
| 247 | mem_op |= QUADSPI_MEM_OP_SECTOR_PROTECT; |
| 248 | debug("lock %08x\n", mem_op); |
| 249 | writel(mem_op, ®s->mem_op); |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int altera_qspi_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 255 | { |
| 256 | struct udevice *dev = mtd->dev; |
| 257 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 258 | struct altera_qspi_regs *regs = pdata->regs; |
| 259 | u32 mem_op; |
| 260 | |
| 261 | mem_op = QUADSPI_MEM_OP_SECTOR_PROTECT; |
| 262 | debug("unlock %08x\n", mem_op); |
| 263 | writel(mem_op, ®s->mem_op); |
| 264 | |
| 265 | return 0; |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static int altera_qspi_probe(struct udevice *dev) |
| 269 | { |
| 270 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 271 | struct altera_qspi_regs *regs = pdata->regs; |
| 272 | unsigned long base = (unsigned long)pdata->base; |
| 273 | struct mtd_info *mtd; |
| 274 | flash_info_t *flash = &flash_info[0]; |
| 275 | u32 rdid; |
| 276 | int i; |
| 277 | |
| 278 | rdid = readl(®s->rd_rdid); |
| 279 | debug("rdid %x\n", rdid); |
| 280 | |
| 281 | mtd = dev_get_uclass_priv(dev); |
| 282 | mtd->dev = dev; |
| 283 | mtd->name = "nor0"; |
| 284 | mtd->type = MTD_NORFLASH; |
| 285 | mtd->flags = MTD_CAP_NORFLASH; |
| 286 | mtd->size = 1 << ((rdid & 0xff) - 6); |
| 287 | mtd->writesize = 1; |
| 288 | mtd->writebufsize = mtd->writesize; |
| 289 | mtd->_erase = altera_qspi_erase; |
| 290 | mtd->_read = altera_qspi_read; |
| 291 | mtd->_write = altera_qspi_write; |
| 292 | mtd->_sync = altera_qspi_sync; |
Thomas Chou | c92ff91 | 2015-12-01 17:00:22 +0800 | [diff] [blame] | 293 | mtd->_lock = altera_qspi_lock; |
| 294 | mtd->_unlock = altera_qspi_unlock; |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 295 | mtd->numeraseregions = 0; |
| 296 | mtd->erasesize = 0x10000; |
| 297 | if (add_mtd_device(mtd)) |
| 298 | return -ENOMEM; |
| 299 | |
| 300 | flash->mtd = mtd; |
| 301 | flash->size = mtd->size; |
| 302 | flash->sector_count = mtd->size / mtd->erasesize; |
| 303 | flash->flash_id = rdid; |
| 304 | flash->start[0] = base; |
| 305 | for (i = 1; i < flash->sector_count; i++) |
| 306 | flash->start[i] = flash->start[i - 1] + mtd->erasesize; |
| 307 | gd->bd->bi_flashstart = base; |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int altera_qspi_ofdata_to_platdata(struct udevice *dev) |
| 313 | { |
| 314 | struct altera_qspi_platdata *pdata = dev_get_platdata(dev); |
| 315 | void *blob = (void *)gd->fdt_blob; |
| 316 | int node = dev->of_offset; |
| 317 | const char *list, *end; |
| 318 | const fdt32_t *cell; |
| 319 | void *base; |
| 320 | unsigned long addr, size; |
| 321 | int parent, addrc, sizec; |
| 322 | int len, idx; |
| 323 | |
| 324 | /* |
| 325 | * decode regs. there are multiple reg tuples, and they need to |
| 326 | * match with reg-names. |
| 327 | */ |
| 328 | parent = fdt_parent_offset(blob, node); |
| 329 | of_bus_default_count_cells(blob, parent, &addrc, &sizec); |
| 330 | list = fdt_getprop(blob, node, "reg-names", &len); |
| 331 | if (!list) |
| 332 | return -ENOENT; |
| 333 | end = list + len; |
| 334 | cell = fdt_getprop(blob, node, "reg", &len); |
| 335 | if (!cell) |
| 336 | return -ENOENT; |
| 337 | idx = 0; |
| 338 | while (list < end) { |
| 339 | addr = fdt_translate_address((void *)blob, |
| 340 | node, cell + idx); |
| 341 | size = fdt_addr_to_cpu(cell[idx + addrc]); |
Thomas Chou | 24a5baf | 2015-11-14 11:22:50 +0800 | [diff] [blame] | 342 | base = map_physmem(addr, size, MAP_NOCACHE); |
Thomas Chou | cdc1152 | 2015-11-09 14:56:02 +0800 | [diff] [blame] | 343 | len = strlen(list); |
| 344 | if (strcmp(list, "avl_csr") == 0) { |
| 345 | pdata->regs = base; |
| 346 | } else if (strcmp(list, "avl_mem") == 0) { |
| 347 | pdata->base = base; |
| 348 | pdata->size = size; |
| 349 | } |
| 350 | idx += addrc + sizec; |
| 351 | list += (len + 1); |
| 352 | } |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | static const struct udevice_id altera_qspi_ids[] = { |
| 358 | { .compatible = "altr,quadspi-1.0" }, |
| 359 | {} |
| 360 | }; |
| 361 | |
| 362 | U_BOOT_DRIVER(altera_qspi) = { |
| 363 | .name = "altera_qspi", |
| 364 | .id = UCLASS_MTD, |
| 365 | .of_match = altera_qspi_ids, |
| 366 | .ofdata_to_platdata = altera_qspi_ofdata_to_platdata, |
| 367 | .platdata_auto_alloc_size = sizeof(struct altera_qspi_platdata), |
| 368 | .probe = altera_qspi_probe, |
| 369 | }; |