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