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