Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2010, ucRobotics Inc. |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014, Sage Electronic Engineering, LLC |
| 4 | * . |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 5 | * Author: Chong Huang <chuang@ucrobotics.com> |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <spi_flash.h> |
Edward O'Callaghan | c4561e2 | 2014-06-26 15:02:40 +1000 | [diff] [blame] | 11 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 12 | #include "spi_flash_internal.h" |
| 13 | |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 14 | /* EN25*-specific commands */ |
| 15 | #define CMD_EN25_WREN 0x06 /* Write Enable */ |
| 16 | #define CMD_EN25_WRDI 0x04 /* Write Disable */ |
| 17 | #define CMD_EN25_RDSR 0x05 /* Read Status Register */ |
| 18 | #define CMD_EN25_WRSR 0x01 /* Write Status Register */ |
| 19 | #define CMD_EN25_READ 0x03 /* Read Data Bytes */ |
| 20 | #define CMD_EN25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */ |
| 21 | #define CMD_EN25_PP 0x02 /* Page Program */ |
| 22 | #define CMD_EN25_SE 0x20 /* Sector Erase */ |
| 23 | #define CMD_EN25_BE 0xd8 /* Block Erase */ |
| 24 | #define CMD_EN25_DP 0xb9 /* Deep Power-down */ |
| 25 | #define CMD_EN25_RES 0xab /* Release from DP, and Read Signature */ |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 26 | |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 27 | #define EON_ID_EN25Q128 0x3018 |
| 28 | #define EON_ID_EN25Q64 0x3017 |
Marc Jones | 0658d23 | 2014-05-01 14:54:11 -0600 | [diff] [blame^] | 29 | #define EON_ID_EN25S64 0x3817 |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 30 | |
| 31 | struct eon_spi_flash_params { |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 32 | u16 id; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 33 | u16 page_size; |
| 34 | u16 pages_per_sector; |
| 35 | u16 sectors_per_block; |
| 36 | u16 nr_sectors; |
| 37 | const char *name; |
| 38 | }; |
| 39 | |
| 40 | /* spi_flash needs to be first so upper layers can free() it */ |
| 41 | struct eon_spi_flash { |
| 42 | struct spi_flash flash; |
| 43 | const struct eon_spi_flash_params *params; |
| 44 | }; |
| 45 | |
| 46 | static inline struct eon_spi_flash *to_eon_spi_flash(struct spi_flash *flash) |
| 47 | { |
| 48 | return container_of(flash, struct eon_spi_flash, flash); |
| 49 | } |
| 50 | |
| 51 | static const struct eon_spi_flash_params eon_spi_flash_table[] = { |
| 52 | { |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 53 | .id = EON_ID_EN25Q128, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 54 | .page_size = 256, |
| 55 | .pages_per_sector = 16, |
| 56 | .sectors_per_block = 16, |
| 57 | .nr_sectors = 4096, |
| 58 | .name = "EN25Q128", |
| 59 | }, |
Vladimir Serbinenko | 066dcec | 2014-01-05 06:46:19 +0100 | [diff] [blame] | 60 | { |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 61 | .id = EON_ID_EN25Q64, |
Vladimir Serbinenko | 066dcec | 2014-01-05 06:46:19 +0100 | [diff] [blame] | 62 | .page_size = 256, |
| 63 | .pages_per_sector = 16, |
| 64 | .sectors_per_block = 16, |
| 65 | .nr_sectors = 2048, |
| 66 | .name = "EN25Q64", |
| 67 | }, |
Marc Jones | 0658d23 | 2014-05-01 14:54:11 -0600 | [diff] [blame^] | 68 | { |
| 69 | .id = EON_ID_EN25S64, |
| 70 | .page_size = 256, |
| 71 | .pages_per_sector = 16, |
| 72 | .sectors_per_block = 16, |
| 73 | .nr_sectors = 2048, |
| 74 | .name = "EN25S64", |
| 75 | }, |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | static int eon_write(struct spi_flash *flash, |
| 79 | u32 offset, size_t len, const void *buf) |
| 80 | { |
| 81 | struct eon_spi_flash *eon = to_eon_spi_flash(flash); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 82 | unsigned long byte_addr; |
| 83 | unsigned long page_size; |
| 84 | size_t chunk_len; |
| 85 | size_t actual; |
David Hendricks | 032c843 | 2014-04-11 19:48:55 -0700 | [diff] [blame] | 86 | int ret = 0; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 87 | u8 cmd[4]; |
| 88 | |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 89 | page_size = 1 << eon->params->page_size; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 90 | byte_addr = offset % page_size; |
| 91 | |
Martin Roth | 3316cf2 | 2012-12-05 16:22:54 -0700 | [diff] [blame] | 92 | flash->spi->rw = SPI_WRITE_FLAG; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 93 | |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 94 | for (actual = 0; actual < len; actual += chunk_len) { |
| 95 | chunk_len = min(len - actual, page_size - byte_addr); |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 96 | chunk_len = spi_crop_chunk(sizeof(cmd), chunk_len); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 97 | |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 98 | ret = spi_flash_cmd(flash->spi, CMD_EN25_WREN, NULL, 0); |
| 99 | if (ret < 0) { |
| 100 | printk(BIOS_WARNING, "SF: Enabling Write failed\n"); |
| 101 | goto out; |
| 102 | } |
| 103 | |
| 104 | cmd[0] = CMD_EN25_PP; |
| 105 | cmd[1] = (offset >> 16) & 0xff; |
| 106 | cmd[2] = (offset >> 8) & 0xff; |
| 107 | cmd[3] = offset & 0xff; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 108 | |
Stefan Reinauer | 5649b08 | 2012-05-23 11:03:29 -0700 | [diff] [blame] | 109 | #if CONFIG_DEBUG_SPI_FLASH |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 110 | printk(BIOS_SPEW, |
Stefan Reinauer | 8ea5a34 | 2012-05-14 13:52:32 -0700 | [diff] [blame] | 111 | "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n", |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 112 | buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); |
Stefan Reinauer | 5649b08 | 2012-05-23 11:03:29 -0700 | [diff] [blame] | 113 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 114 | |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 115 | ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 116 | buf + actual, chunk_len); |
| 117 | if (ret < 0) { |
| 118 | printk(BIOS_WARNING, "SF: EON Page Program failed\n"); |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 119 | goto out; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 123 | if (ret) { |
| 124 | printk(BIOS_WARNING, "SF: EON Page Program timeout\n"); |
| 125 | goto out; |
| 126 | } |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 127 | |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 128 | offset += chunk_len; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 129 | byte_addr = 0; |
| 130 | } |
| 131 | |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 132 | #if CONFIG_DEBUG_SPI_FLASH |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 133 | printk(BIOS_SPEW, "SF: EON: Successfully programmed %zu bytes @ %#x\n", |
| 134 | len, (unsigned int)(offset - len)); |
Marc Jones | 747127d | 2012-12-03 22:16:29 -0700 | [diff] [blame] | 135 | #endif |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 136 | |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 137 | out: |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static int eon_erase(struct spi_flash *flash, u32 offset, size_t len) |
| 142 | { |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 143 | return spi_flash_cmd_erase(flash, CMD_EN25_SE, offset, len); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) |
| 147 | { |
| 148 | const struct eon_spi_flash_params *params; |
| 149 | struct eon_spi_flash *eon; |
| 150 | unsigned int i; |
| 151 | |
| 152 | for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) { |
| 153 | params = &eon_spi_flash_table[i]; |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 154 | if (params->id == ((idcode[1] << 8) | idcode[2])) |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 155 | break; |
| 156 | } |
| 157 | |
| 158 | if (i == ARRAY_SIZE(eon_spi_flash_table)) { |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 159 | printk(BIOS_WARNING, "SF: Unsupported EON ID %#02x%02x\n", |
| 160 | idcode[1], idcode[2]); |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
| 163 | |
| 164 | eon = malloc(sizeof(*eon)); |
| 165 | if (!eon) { |
| 166 | printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | eon->params = params; |
| 171 | eon->flash.spi = spi; |
| 172 | eon->flash.name = params->name; |
| 173 | |
| 174 | eon->flash.write = eon_write; |
| 175 | eon->flash.erase = eon_erase; |
| 176 | eon->flash.read = spi_flash_cmd_read_fast; |
Marc Jones | 79f60a5 | 2014-05-01 14:53:24 -0600 | [diff] [blame] | 177 | eon->flash.sector_size = params->page_size * params->pages_per_sector; |
Stefan Reinauer | 1c56d9b | 2012-05-10 11:27:32 -0700 | [diff] [blame] | 178 | eon->flash.size = params->page_size * params->pages_per_sector |
| 179 | * params->nr_sectors; |
| 180 | |
| 181 | return &eon->flash; |
| 182 | } |