1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2012-2014 Daniel Schwierzeck, daniel.schwierzeck@gmail.com 4 */ 5 6 #include <common.h> 7 #include <malloc.h> 8 #include <linux/errno.h> 9 #include <linux/mtd/mtd.h> 10 #include <spi_flash.h> 11 12 static struct mtd_info sf_mtd_info; 13 static bool sf_mtd_registered; 14 static char sf_mtd_name[8]; 15 16 static int spi_flash_mtd_erase(struct mtd_info *mtd, struct erase_info *instr) 17 { 18 struct spi_flash *flash = mtd->priv; 19 int err; 20 21 if (!flash) 22 return -ENODEV; 23 24 instr->state = MTD_ERASING; 25 26 err = spi_flash_erase(flash, instr->addr, instr->len); 27 if (err) { 28 instr->state = MTD_ERASE_FAILED; 29 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 30 return -EIO; 31 } 32 33 instr->state = MTD_ERASE_DONE; 34 mtd_erase_callback(instr); 35 36 return 0; 37 } 38 39 static int spi_flash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, 40 size_t *retlen, u_char *buf) 41 { 42 struct spi_flash *flash = mtd->priv; 43 int err; 44 45 if (!flash) 46 return -ENODEV; 47 48 err = spi_flash_read(flash, from, len, buf); 49 if (!err) 50 *retlen = len; 51 52 return err; 53 } 54 55 static int spi_flash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, 56 size_t *retlen, const u_char *buf) 57 { 58 struct spi_flash *flash = mtd->priv; 59 int err; 60 61 if (!flash) 62 return -ENODEV; 63 64 err = spi_flash_write(flash, to, len, buf); 65 if (!err) 66 *retlen = len; 67 68 return err; 69 } 70 71 static void spi_flash_mtd_sync(struct mtd_info *mtd) 72 { 73 } 74 75 static int spi_flash_mtd_number(void) 76 { 77 #ifdef CONFIG_SYS_MAX_FLASH_BANKS 78 return CONFIG_SYS_MAX_FLASH_BANKS; 79 #else 80 return 0; 81 #endif 82 } 83 84 int spi_flash_mtd_register(struct spi_flash *flash) 85 { 86 int ret; 87 88 if (sf_mtd_registered) { 89 ret = del_mtd_device(&sf_mtd_info); 90 if (ret) 91 return ret; 92 93 sf_mtd_registered = false; 94 } 95 96 sf_mtd_registered = false; 97 memset(&sf_mtd_info, 0, sizeof(sf_mtd_info)); 98 sprintf(sf_mtd_name, "nor%d", spi_flash_mtd_number()); 99 100 sf_mtd_info.name = sf_mtd_name; 101 sf_mtd_info.type = MTD_NORFLASH; 102 sf_mtd_info.flags = MTD_CAP_NORFLASH; 103 sf_mtd_info.writesize = 1; 104 sf_mtd_info.writebufsize = flash->page_size; 105 106 sf_mtd_info._erase = spi_flash_mtd_erase; 107 sf_mtd_info._read = spi_flash_mtd_read; 108 sf_mtd_info._write = spi_flash_mtd_write; 109 sf_mtd_info._sync = spi_flash_mtd_sync; 110 111 sf_mtd_info.size = flash->size; 112 sf_mtd_info.priv = flash; 113 114 /* Only uniform flash devices for now */ 115 sf_mtd_info.numeraseregions = 0; 116 sf_mtd_info.erasesize = flash->sector_size; 117 118 ret = add_mtd_device(&sf_mtd_info); 119 if (!ret) 120 sf_mtd_registered = true; 121 122 return ret; 123 } 124 125 void spi_flash_mtd_unregister(void) 126 { 127 int ret; 128 129 if (!sf_mtd_registered) 130 return; 131 132 ret = del_mtd_device(&sf_mtd_info); 133 if (!ret) { 134 sf_mtd_registered = false; 135 return; 136 } 137 138 /* 139 * Setting mtd->priv to NULL is the best we can do. Thanks to that, 140 * the MTD layer can still call mtd hooks without risking a 141 * use-after-free bug. Still, things should be fixed to prevent the 142 * spi_flash object from being destroyed when del_mtd_device() fails. 143 */ 144 sf_mtd_info.priv = NULL; 145 printf("Failed to unregister MTD %s and the spi_flash object is going away: you're in deep trouble!", 146 sf_mtd_info.name); 147 } 148