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> |
| 7 | #include <malloc.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 8 | #include <linux/errno.h> |
Daniel Schwierzeck | 9fe6d87 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 9 | #include <linux/mtd/mtd.h> |
| 10 | #include <spi_flash.h> |
| 11 | |
| 12 | static struct mtd_info sf_mtd_info; |
Boris Brezillon | 492151b | 2018-12-02 10:54:25 +0100 | [diff] [blame] | 13 | static bool sf_mtd_registered; |
Daniel Schwierzeck | 9fe6d87 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 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 | |
Boris Brezillon | 08898e8 | 2018-12-02 10:54:32 +0100 | [diff] [blame] | 21 | if (!flash) |
| 22 | return -ENODEV; |
| 23 | |
Daniel Schwierzeck | 9fe6d87 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 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 | |
Boris Brezillon | 08898e8 | 2018-12-02 10:54:32 +0100 | [diff] [blame] | 45 | if (!flash) |
| 46 | return -ENODEV; |
| 47 | |
Daniel Schwierzeck | 9fe6d87 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 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 | |
Boris Brezillon | 08898e8 | 2018-12-02 10:54:32 +0100 | [diff] [blame] | 61 | if (!flash) |
| 62 | return -ENODEV; |
| 63 | |
Daniel Schwierzeck | 9fe6d87 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 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 | { |
Boris Brezillon | 492151b | 2018-12-02 10:54:25 +0100 | [diff] [blame] | 86 | int ret; |
| 87 | |
Boris Brezillon | 08898e8 | 2018-12-02 10:54:32 +0100 | [diff] [blame] | 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 | } |
Boris Brezillon | 492151b | 2018-12-02 10:54:25 +0100 | [diff] [blame] | 95 | |
| 96 | sf_mtd_registered = false; |
Daniel Schwierzeck | 9fe6d87 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 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 | |
Boris Brezillon | 492151b | 2018-12-02 10:54:25 +0100 | [diff] [blame] | 118 | ret = add_mtd_device(&sf_mtd_info); |
| 119 | if (!ret) |
| 120 | sf_mtd_registered = true; |
| 121 | |
| 122 | return ret; |
Daniel Schwierzeck | 9fe6d87 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void spi_flash_mtd_unregister(void) |
| 126 | { |
Boris Brezillon | 08898e8 | 2018-12-02 10:54:32 +0100 | [diff] [blame] | 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); |
Daniel Schwierzeck | 9fe6d87 | 2015-04-27 07:42:04 +0200 | [diff] [blame] | 147 | } |