Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, Network Appliance Inc. |
| 3 | * Author: Jason McMullan <mcmullan <at> netapp.com> |
| 4 | * Licensed under the GPL-2 or later. |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <malloc.h> |
| 9 | #include <spi_flash.h> |
| 10 | |
| 11 | #include "spi_flash_internal.h" |
| 12 | |
| 13 | /* M25Pxx-specific commands */ |
| 14 | #define CMD_W25_WREN 0x06 /* Write Enable */ |
| 15 | #define CMD_W25_WRDI 0x04 /* Write Disable */ |
| 16 | #define CMD_W25_RDSR 0x05 /* Read Status Register */ |
| 17 | #define CMD_W25_WRSR 0x01 /* Write Status Register */ |
| 18 | #define CMD_W25_READ 0x03 /* Read Data Bytes */ |
| 19 | #define CMD_W25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 20 | #define CMD_W25_PP 0x02 /* Page Program */ |
| 21 | #define CMD_W25_SE 0x20 /* Sector (4K) Erase */ |
| 22 | #define CMD_W25_BE 0xd8 /* Block (64K) Erase */ |
| 23 | #define CMD_W25_CE 0xc7 /* Chip Erase */ |
| 24 | #define CMD_W25_DP 0xb9 /* Deep Power-down */ |
| 25 | #define CMD_W25_RES 0xab /* Release from DP, and Read Signature */ |
| 26 | |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 27 | struct winbond_spi_flash_params { |
| 28 | uint16_t id; |
| 29 | /* Log2 of page size in power-of-two mode */ |
| 30 | uint8_t l2_page_size; |
| 31 | uint16_t pages_per_sector; |
| 32 | uint16_t sectors_per_block; |
Wojtek Skulski | b1637e5 | 2010-12-07 01:07:45 -0500 | [diff] [blame] | 33 | uint16_t nr_blocks; |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 34 | const char *name; |
| 35 | }; |
| 36 | |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 37 | static const struct winbond_spi_flash_params winbond_spi_flash_table[] = { |
| 38 | { |
James Le Cuirot | 7e921dc | 2011-07-15 06:12:51 +0000 | [diff] [blame] | 39 | .id = 0x3013, |
| 40 | .l2_page_size = 8, |
| 41 | .pages_per_sector = 16, |
| 42 | .sectors_per_block = 16, |
| 43 | .nr_blocks = 8, |
| 44 | .name = "W25X40", |
| 45 | }, |
| 46 | { |
Wojtek Skulski | b1637e5 | 2010-12-07 01:07:45 -0500 | [diff] [blame] | 47 | .id = 0x3015, |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 48 | .l2_page_size = 8, |
| 49 | .pages_per_sector = 16, |
| 50 | .sectors_per_block = 16, |
| 51 | .nr_blocks = 32, |
| 52 | .name = "W25X16", |
| 53 | }, |
| 54 | { |
Wojtek Skulski | b1637e5 | 2010-12-07 01:07:45 -0500 | [diff] [blame] | 55 | .id = 0x3016, |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 56 | .l2_page_size = 8, |
| 57 | .pages_per_sector = 16, |
| 58 | .sectors_per_block = 16, |
| 59 | .nr_blocks = 64, |
| 60 | .name = "W25X32", |
| 61 | }, |
| 62 | { |
Wojtek Skulski | b1637e5 | 2010-12-07 01:07:45 -0500 | [diff] [blame] | 63 | .id = 0x3017, |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 64 | .l2_page_size = 8, |
| 65 | .pages_per_sector = 16, |
| 66 | .sectors_per_block = 16, |
| 67 | .nr_blocks = 128, |
| 68 | .name = "W25X64", |
| 69 | }, |
Graeme Smecher | a448bce | 2010-07-29 09:00:02 -0400 | [diff] [blame] | 70 | { |
Wojtek Skulski | b1637e5 | 2010-12-07 01:07:45 -0500 | [diff] [blame] | 71 | .id = 0x4015, |
| 72 | .l2_page_size = 8, |
| 73 | .pages_per_sector = 16, |
| 74 | .sectors_per_block = 16, |
| 75 | .nr_blocks = 32, |
| 76 | .name = "W25Q16", |
| 77 | }, |
| 78 | { |
| 79 | .id = 0x4016, |
| 80 | .l2_page_size = 8, |
| 81 | .pages_per_sector = 16, |
| 82 | .sectors_per_block = 16, |
| 83 | .nr_blocks = 64, |
| 84 | .name = "W25Q32", |
| 85 | }, |
| 86 | { |
| 87 | .id = 0x4017, |
Graeme Smecher | a448bce | 2010-07-29 09:00:02 -0400 | [diff] [blame] | 88 | .l2_page_size = 8, |
| 89 | .pages_per_sector = 16, |
| 90 | .sectors_per_block = 16, |
| 91 | .nr_blocks = 128, |
| 92 | .name = "W25Q64", |
| 93 | }, |
Wojtek Skulski | b1637e5 | 2010-12-07 01:07:45 -0500 | [diff] [blame] | 94 | { |
| 95 | .id = 0x4018, |
| 96 | .l2_page_size = 8, |
| 97 | .pages_per_sector = 16, |
| 98 | .sectors_per_block = 16, |
| 99 | .nr_blocks = 256, |
| 100 | .name = "W25Q128", |
| 101 | }, |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 102 | }; |
| 103 | |
Mike Frysinger | aea4e17 | 2011-04-12 01:51:29 -0400 | [diff] [blame] | 104 | static int winbond_erase(struct spi_flash *flash, u32 offset, size_t len) |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 105 | { |
Richard Retanubun | b0148dc | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 106 | return spi_flash_cmd_erase(flash, CMD_W25_SE, offset, len); |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode) |
| 110 | { |
| 111 | const struct winbond_spi_flash_params *params; |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 112 | struct spi_flash *flash; |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 113 | unsigned int i; |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 114 | unsigned page_size; |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 115 | |
| 116 | for (i = 0; i < ARRAY_SIZE(winbond_spi_flash_table); i++) { |
| 117 | params = &winbond_spi_flash_table[i]; |
| 118 | if (params->id == ((idcode[1] << 8) | idcode[2])) |
| 119 | break; |
| 120 | } |
| 121 | |
| 122 | if (i == ARRAY_SIZE(winbond_spi_flash_table)) { |
| 123 | debug("SF: Unsupported Winbond ID %02x%02x\n", |
| 124 | idcode[1], idcode[2]); |
| 125 | return NULL; |
| 126 | } |
| 127 | |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 128 | flash = malloc(sizeof(*flash)); |
| 129 | if (!flash) { |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 130 | debug("SF: Failed to allocate memory\n"); |
| 131 | return NULL; |
| 132 | } |
| 133 | |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 134 | flash->spi = spi; |
| 135 | flash->name = params->name; |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 136 | |
| 137 | /* Assuming power-of-two page size initially. */ |
| 138 | page_size = 1 << params->l2_page_size; |
| 139 | |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 140 | flash->write = spi_flash_cmd_write_multi; |
| 141 | flash->erase = winbond_erase; |
| 142 | flash->read = spi_flash_cmd_read_fast; |
| 143 | flash->page_size = page_size; |
| 144 | flash->sector_size = page_size * params->pages_per_sector; |
| 145 | flash->size = page_size * params->pages_per_sector |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 146 | * params->sectors_per_block |
| 147 | * params->nr_blocks; |
| 148 | |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 149 | return flash; |
Jason McMullan | 64e5f3a | 2009-10-09 17:12:23 -0400 | [diff] [blame] | 150 | } |