Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010, ucRobotics Inc. |
| 3 | * Author: Chong Huang <chuang@ucrobotics.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 | /* EN25Q128-specific commands */ |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 14 | #define CMD_EN25Q128_SE 0x20 /* Sector Erase */ |
| 15 | #define CMD_EN25Q128_BE 0xd8 /* Block Erase */ |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 16 | |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 17 | struct eon_spi_flash_params { |
| 18 | u8 idcode1; |
| 19 | u16 page_size; |
| 20 | u16 pages_per_sector; |
| 21 | u16 sectors_per_block; |
| 22 | u16 nr_sectors; |
| 23 | const char *name; |
| 24 | }; |
| 25 | |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 26 | static const struct eon_spi_flash_params eon_spi_flash_table[] = { |
| 27 | { |
Shaohui Xie | fc9d826 | 2011-09-27 19:21:34 +0800 | [diff] [blame] | 28 | .idcode1 = 0x16, |
| 29 | .page_size = 256, |
| 30 | .pages_per_sector = 16, |
| 31 | .sectors_per_block = 16, |
| 32 | .nr_sectors = 1024, |
| 33 | .name = "EN25Q32B", |
| 34 | }, |
| 35 | { |
Mike Frysinger | fc9d1fc | 2011-04-23 23:05:58 +0000 | [diff] [blame] | 36 | .idcode1 = 0x18, |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 37 | .page_size = 256, |
| 38 | .pages_per_sector = 16, |
| 39 | .sectors_per_block = 16, |
| 40 | .nr_sectors = 4096, |
| 41 | .name = "EN25Q128", |
| 42 | }, |
Shaohui Xie | 2d0dd6e | 2011-09-27 01:21:34 +0000 | [diff] [blame] | 43 | { |
| 44 | .idcode1 = 0x16, |
| 45 | .page_size = 256, |
| 46 | .pages_per_sector = 16, |
| 47 | .sectors_per_block = 16, |
| 48 | .nr_sectors = 1024, |
| 49 | .name = "EN25Q32B", |
| 50 | }, |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 51 | }; |
| 52 | |
Mike Frysinger | aea4e17 | 2011-04-12 01:51:29 -0400 | [diff] [blame] | 53 | static int eon_erase(struct spi_flash *flash, u32 offset, size_t len) |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 54 | { |
Richard Retanubun | b0148dc | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 55 | return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len); |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) |
| 59 | { |
| 60 | const struct eon_spi_flash_params *params; |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 61 | struct spi_flash *flash; |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 62 | unsigned int i; |
| 63 | |
| 64 | for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) { |
| 65 | params = &eon_spi_flash_table[i]; |
| 66 | if (params->idcode1 == idcode[2]) |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | if (i == ARRAY_SIZE(eon_spi_flash_table)) { |
| 71 | debug("SF: Unsupported EON ID %02x\n", idcode[1]); |
| 72 | return NULL; |
| 73 | } |
| 74 | |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 75 | flash = malloc(sizeof(*flash)); |
| 76 | if (!flash) { |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 77 | debug("SF: Failed to allocate memory\n"); |
| 78 | return NULL; |
| 79 | } |
| 80 | |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 81 | flash->spi = spi; |
| 82 | flash->name = params->name; |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 83 | |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 84 | flash->write = spi_flash_cmd_write_multi; |
| 85 | flash->erase = eon_erase; |
| 86 | flash->read = spi_flash_cmd_read_fast; |
| 87 | flash->page_size = params->page_size; |
| 88 | flash->sector_size = params->page_size * params->pages_per_sector |
Richard Retanubun | b0148dc | 2011-02-16 16:37:22 -0500 | [diff] [blame] | 89 | * params->sectors_per_block; |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 90 | flash->size = params->page_size * params->pages_per_sector |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 91 | * params->nr_sectors; |
| 92 | |
Mike Frysinger | ce7cc04 | 2011-06-28 07:38:10 +0000 | [diff] [blame] | 93 | return flash; |
Chong Huang | c71e6dc | 2010-11-30 03:33:25 -0500 | [diff] [blame] | 94 | } |