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