Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 1 | /* |
| 2 | * MTD SPI driver for ST M25Pxx flash chips |
| 3 | * |
| 4 | * Author: Mike Lavender, mike@steroidmicros.com |
| 5 | * |
| 6 | * Copyright (c) 2005, Intec Automation Inc. |
| 7 | * Copyright (c) 2012, CradlePoint Technology, Inc. |
| 8 | * |
| 9 | * Some parts are based on lart.c by Abraham Van Der Merwe |
| 10 | * |
| 11 | * Cleaned up and generalized based on mtd_dataflash.c |
| 12 | * |
| 13 | * Ralink SPI updates and adjustments by CradlePoint |
| 14 | * |
| 15 | * This code is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License version 2 as |
| 17 | * published by the Free Software Foundation. |
| 18 | * |
| 19 | */ |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/types.h> |
| 22 | #include <linux/kernel.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/slab.h> |
| 26 | #include <linux/device.h> |
| 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/mtd/mtd.h> |
| 29 | #include <linux/mtd/map.h> |
| 30 | #include <linux/mtd/partitions.h> |
| 31 | #include <linux/mtd/physmap.h> |
| 32 | #include <linux/reboot.h> |
| 33 | #include <linux/semaphore.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/err.h> |
| 36 | |
| 37 | #include "../maps/ralink-flash.h" |
| 38 | |
| 39 | #if defined (CONFIG_RALINK_RT6855A) |
| 40 | #include "bbu_spiflash.h" |
| 41 | #else |
| 42 | #include "ralink_spi.h" |
| 43 | #endif |
| 44 | |
| 45 | /* Choose the SPI flash mode */ |
| 46 | #if defined (CONFIG_RALINK_RT6855A) |
| 47 | #define BBU_MODE // BBU SPI flash controller |
| 48 | #define MORE_BUF_MODE |
| 49 | #else |
| 50 | #define USER_MODE // SPI flash user mode support by default |
| 51 | //#define COMMAND_MODE // SPI flash command mode support |
| 52 | #endif |
| 53 | |
| 54 | #if !defined USER_MODE && !defined COMMAND_MODE && !defined BBU_MODE |
| 55 | #error "Please choose the correct mode of SPI flash controller" |
| 56 | #endif |
| 57 | |
| 58 | /****************************************************************************** |
| 59 | * SPI FLASH elementray definition and function |
| 60 | ******************************************************************************/ |
| 61 | |
| 62 | #define FLASH_PAGESIZE 256 |
| 63 | #define MX_4B_MODE /* MXIC 4 Byte Mode */ |
| 64 | |
| 65 | /* Flash opcodes. */ |
| 66 | #define OPCODE_WREN 6 /* Write enable */ |
| 67 | #define OPCODE_WRDI 4 /* Write disable */ |
| 68 | #define OPCODE_RDSR 5 /* Read status register */ |
| 69 | #define OPCODE_WRSR 1 /* Write status register */ |
| 70 | #define OPCODE_READ 3 /* Read data bytes */ |
| 71 | #define OPCODE_PP 2 /* Page program */ |
| 72 | #define OPCODE_SE 0xD8 /* Sector erase */ |
| 73 | #define OPCODE_RES 0xAB /* Read Electronic Signature */ |
| 74 | #define OPCODE_RDID 0x9F /* Read JEDEC ID */ |
| 75 | |
| 76 | #define OPCODE_FAST_READ 0x0B /* Fast Read */ |
| 77 | #define OPCODE_DOR 0x3B /* Dual Output Read */ |
| 78 | #define OPCODE_QOR 0x6B /* Quad Output Read */ |
| 79 | #define OPCODE_DIOR 0xBB /* Dual IO High Performance Read */ |
| 80 | #define OPCODE_QIOR 0xEB /* Quad IO High Performance Read */ |
| 81 | #define OPCODE_READ_ID 0x90 /* Read Manufacturer and Device ID */ |
| 82 | |
| 83 | #define OPCODE_P4E 0x20 /* 4KB Parameter Sectore Erase */ |
| 84 | #define OPCODE_P8E 0x40 /* 8KB Parameter Sectore Erase */ |
| 85 | #define OPCODE_BE 0x60 /* Bulk Erase */ |
| 86 | #define OPCODE_BE1 0xC7 /* Bulk Erase */ |
| 87 | #define OPCODE_QPP 0x32 /* Quad Page Programing */ |
| 88 | |
| 89 | /* Status Register bits. */ |
| 90 | #define SR_WIP 1 /* Write in progress */ |
| 91 | #define SR_WEL 2 /* Write enable latch */ |
| 92 | #define SR_BP0 4 /* Block protect 0 */ |
| 93 | #define SR_BP1 8 /* Block protect 1 */ |
| 94 | #define SR_BP2 0x10 /* Block protect 2 */ |
| 95 | #define SR_EPE 0x20 /* Erase/Program error */ |
| 96 | #define SR_SRWD 0x80 /* SR write protect */ |
| 97 | |
| 98 | /* Security Register bits. */ |
| 99 | #define READ_SCUR 0x2b /* Read Security register */ |
| 100 | #define SECR_OTP 0x01 |
| 101 | #define SECR_LDSO 0x02 |
| 102 | #define SECR_4BYTE 0x04 |
| 103 | #define SECR_CP 0x10 |
| 104 | #define SECR_PFAIL 0x20 |
| 105 | #define SECR_EFAIL 0x40 |
| 106 | #define SECR_WPSEL 0x80 |
| 107 | |
| 108 | |
| 109 | /* |
| 110 | The MTD partition map and number of partitions. |
| 111 | */ |
| 112 | static struct mtd_partition *rt2880_partitions; |
| 113 | static int nr_parts; |
| 114 | |
| 115 | //#define SPI_DEBUG |
| 116 | #if !defined (SPI_DEBUG) |
| 117 | |
| 118 | #define ra_inl(addr) (*(volatile unsigned int *)(addr)) |
| 119 | #define ra_outl(addr, value) (*(volatile unsigned int *)(addr) = (value)) |
| 120 | #define ra_dbg(args...) do {} while(0) |
| 121 | //#define ra_dbg(args...) do { printk(args); } while(0) |
| 122 | |
| 123 | #else |
| 124 | |
| 125 | #define ra_dbg(args...) do { printk(args); } while(0) |
| 126 | #define _ra_inl(addr) (*(volatile unsigned int *)(addr)) |
| 127 | #define _ra_outl(addr, value) (*(volatile unsigned int *)(addr) = (value)) |
| 128 | |
| 129 | u32 ra_inl(u32 addr) |
| 130 | { |
| 131 | u32 retval = _ra_inl(addr); |
| 132 | |
| 133 | printk("%s(%x) => %x \n", __func__, addr, retval); |
| 134 | |
| 135 | return retval; |
| 136 | } |
| 137 | |
| 138 | u32 ra_outl(u32 addr, u32 val) |
| 139 | { |
| 140 | _ra_outl(addr, val); |
| 141 | |
| 142 | printk("%s(%x, %x) \n", __func__, addr, val); |
| 143 | |
| 144 | return val; |
| 145 | } |
| 146 | |
| 147 | #endif // SPI_DEBUG // |
| 148 | |
| 149 | #define ra_aor(addr, a_mask, o_value) ra_outl(addr, (ra_inl(addr) & (a_mask)) | (o_value)) |
| 150 | #define ra_and(addr, a_mask) ra_aor(addr, a_mask, 0) |
| 151 | #define ra_or(addr, o_value) ra_aor(addr, -1, o_value) |
| 152 | |
| 153 | #define SPIC_READ_BYTES (1<<0) |
| 154 | #define SPIC_WRITE_BYTES (1<<1) |
| 155 | |
| 156 | #if 1 |
| 157 | void usleep(unsigned int usecs) |
| 158 | { |
| 159 | unsigned long timeout = usecs_to_jiffies(usecs); |
| 160 | |
| 161 | while (timeout) |
| 162 | timeout = schedule_timeout_interruptible(timeout); |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | |
| 167 | #if defined USER_MODE || defined COMMAND_MODE |
| 168 | static unsigned int spi_wait_nsec = 0; |
| 169 | static int spic_busy_wait(void) |
| 170 | { |
| 171 | do { |
| 172 | if ((ra_inl(RT2880_SPISTAT_REG) & 0x01) == 0) |
| 173 | return 0; |
| 174 | } while (spi_wait_nsec >> 1); |
| 175 | |
| 176 | printk("%s: fail \n", __func__); |
| 177 | return -1; |
| 178 | } |
| 179 | #elif defined BBU_MODE |
| 180 | static int bbu_spic_busy_wait(void) |
| 181 | { |
| 182 | int n = 100000; |
| 183 | do { |
| 184 | if ((ra_inl(SPI_REG_CTL) & SPI_CTL_BUSY) == 0) |
| 185 | return 0; |
| 186 | udelay(1); |
| 187 | } while (--n > 0); |
| 188 | |
| 189 | printk("%s: fail \n", __func__); |
| 190 | return -1; |
| 191 | } |
| 192 | #endif // USER_MODE || COMMAND_MODE // |
| 193 | |
| 194 | |
| 195 | #ifdef USER_MODE |
| 196 | /* |
| 197 | * @cmd: command and address |
| 198 | * @n_cmd: size of command, in bytes |
| 199 | * @buf: buffer into which data will be read/written |
| 200 | * @n_buf: size of buffer, in bytes |
| 201 | * @flag: tag as READ/WRITE |
| 202 | * |
| 203 | * @return: if write_onlu, -1 means write fail, or return writing counter. |
| 204 | * @return: if read, -1 means read fail, or return reading counter. |
| 205 | */ |
| 206 | static int spic_transfer(const u8 *cmd, int n_cmd, u8 *buf, int n_buf, int flag) |
| 207 | { |
| 208 | int retval = -1; |
| 209 | ra_dbg("cmd(%x): %x %x %x %x , buf:%x len:%x, flag:%s \n", |
| 210 | n_cmd, cmd[0], cmd[1], cmd[2], cmd[3], |
| 211 | (buf)? (*buf) : 0, n_buf, |
| 212 | (flag == SPIC_READ_BYTES)? "read" : "write"); |
| 213 | |
| 214 | // assert CS and we are already CLK normal high |
| 215 | ra_and(RT2880_SPICTL_REG, ~(SPICTL_SPIENA_HIGH)); |
| 216 | |
| 217 | // write command |
| 218 | for (retval = 0; retval < n_cmd; retval++) { |
| 219 | ra_outl(RT2880_SPIDATA_REG, cmd[retval]); |
| 220 | ra_or(RT2880_SPICTL_REG, SPICTL_STARTWR); |
| 221 | if (spic_busy_wait()) { |
| 222 | retval = -1; |
| 223 | goto end_trans; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | // read / write data |
| 228 | if (flag & SPIC_READ_BYTES) { |
| 229 | for (retval = 0; retval < n_buf; retval++) { |
| 230 | ra_or(RT2880_SPICTL_REG, SPICTL_STARTRD); |
| 231 | if (spic_busy_wait()) |
| 232 | goto end_trans; |
| 233 | buf[retval] = (u8) ra_inl(RT2880_SPIDATA_REG); |
| 234 | } |
| 235 | |
| 236 | } |
| 237 | else if (flag & SPIC_WRITE_BYTES) { |
| 238 | for (retval = 0; retval < n_buf; retval++) { |
| 239 | ra_outl(RT2880_SPIDATA_REG, buf[retval]); |
| 240 | ra_or(RT2880_SPICTL_REG, SPICTL_STARTWR); |
| 241 | if (spic_busy_wait()) |
| 242 | goto end_trans; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | end_trans: |
| 247 | // de-assert CS and |
| 248 | ra_or (RT2880_SPICTL_REG, (SPICTL_SPIENA_HIGH)); |
| 249 | |
| 250 | return retval; |
| 251 | } |
| 252 | |
| 253 | static int spic_read(const u8 *cmd, size_t n_cmd, u8 *rxbuf, size_t n_rx) |
| 254 | { |
| 255 | return spic_transfer(cmd, n_cmd, rxbuf, n_rx, SPIC_READ_BYTES); |
| 256 | } |
| 257 | |
| 258 | static int spic_write(const u8 *cmd, size_t n_cmd, const u8 *txbuf, size_t n_tx) |
| 259 | { |
| 260 | return spic_transfer(cmd, n_cmd, (u8 *)txbuf, n_tx, SPIC_WRITE_BYTES); |
| 261 | } |
| 262 | #endif // USER_MODE // |
| 263 | |
| 264 | void spic_init(void) |
| 265 | { |
| 266 | #if defined USER_MODE || defined COMMAND_MODE |
| 267 | |
| 268 | // use normal(SPI) mode instead of GPIO mode |
| 269 | //ra_and(RALINK_REG_GPIOMODE, ~(1 << 1)); |
| 270 | |
| 271 | // reset spi block |
| 272 | ra_or(RT2880_RSTCTRL_REG, RSTCTRL_SPI_RESET); |
| 273 | udelay(1); |
| 274 | ra_and(RT2880_RSTCTRL_REG, ~RSTCTRL_SPI_RESET); |
| 275 | |
| 276 | // FIXME, clk_div should depend on spi-flash. |
| 277 | // mode 0 (SPICLKPOL = 0) & (RXCLKEDGE_FALLING = 0) |
| 278 | // mode 3 (SPICLKPOL = 1) & (RXCLKEDGE_FALLING = 0) |
| 279 | ra_outl(RT2880_SPICFG_REG, SPICFG_MSBFIRST | SPICFG_TXCLKEDGE_FALLING | CFG_CLK_DIV | SPICFG_SPICLKPOL ); |
| 280 | |
| 281 | // set idle state |
| 282 | ra_outl(RT2880_SPICTL_REG, SPICTL_HIZSDO | SPICTL_SPIENA_HIGH); |
| 283 | |
| 284 | spi_wait_nsec = (8 * 1000 / (128 / CFG_CLK_DIV) ) >> 1 ; |
| 285 | //printk("spi_wait_nsec: %x \n", spi_wait_nsec); |
| 286 | #elif defined BBU_MODE |
| 287 | // enable SMC bank 0 alias addressing |
| 288 | ra_or(RALINK_SYSCTL_BASE + 0x38, 0x80000000); |
| 289 | #endif |
| 290 | } |
| 291 | |
| 292 | |
| 293 | struct chip_info { |
| 294 | char *name; |
| 295 | u8 id; |
| 296 | u32 jedec_id; |
| 297 | unsigned long sector_size; |
| 298 | unsigned int n_sectors; |
| 299 | char addr4b; |
| 300 | }; |
| 301 | |
| 302 | static struct chip_info chips_data [] = { |
| 303 | /* REVISIT: fill in JEDEC ids, for parts that have them */ |
| 304 | { "AT25DF321", 0x1f, 0x47000000, 64 * 1024, 64, 0 }, |
| 305 | { "AT26DF161", 0x1f, 0x46000000, 64 * 1024, 32, 0 }, |
| 306 | { "FL016AIF", 0x01, 0x02140000, 64 * 1024, 32, 0 }, |
| 307 | { "FL064AIF", 0x01, 0x02160000, 64 * 1024, 128, 0 }, |
| 308 | { "MX25L1605D", 0xc2, 0x2015c220, 64 * 1024, 32, 0 }, |
| 309 | { "MX25L3205D", 0xc2, 0x2016c220, 64 * 1024, 64, 0 }, |
| 310 | { "MX25L6405D", 0xc2, 0x2017c220, 64 * 1024, 128, 0 }, |
| 311 | { "MX25L12805D", 0xc2, 0x2018c220, 64 * 1024, 256, 0 }, |
| 312 | #ifdef MX_4B_MODE |
| 313 | { "MX25L25635E", 0xc2, 0x2019c220, 64 * 1024, 512, 1 }, |
| 314 | #endif |
| 315 | { "S25FL128P", 0x01, 0x20180301, 64 * 1024, 256, 0 }, |
| 316 | { "S25FL129P", 0x01, 0x20184D01, 64 * 1024, 256, 0 }, |
| 317 | { "S25FL032P", 0x01, 0x02154D00, 64 * 1024, 64, 0 }, |
| 318 | { "S25FL064P", 0x01, 0x02164D00, 64 * 1024, 128, 0 }, |
| 319 | { "EN25F16", 0x1c, 0x31151c31, 64 * 1024, 32, 0 }, |
| 320 | { "EN25F32", 0x1c, 0x31161c31, 64 * 1024, 64, 0 }, |
| 321 | { "EN25Q32", 0x1c, 0x30161c30, 64 * 1024, 64, 0 }, //EN25Q32B |
| 322 | { "EN25F64", 0x1c, 0x20171c20, 64 * 1024, 128, 0 }, //EN25P64 |
| 323 | { "EN25Q64", 0x1c, 0x30171c30, 64 * 1024, 128, 0 }, |
| 324 | { "W25Q32BV", 0xef, 0x30160000, 64 * 1024, 64, 0 }, |
| 325 | { "W25Q32BV", 0xef, 0x40160000, 64 * 1024, 64, 0 }, |
| 326 | { "W25Q64BV", 0xef, 0x40170000, 64 * 1024, 128, 0 }, //S25FL064K |
| 327 | { "W25Q128BV", 0xef, 0x40180000, 64 * 1024, 256, 0 }, |
| 328 | }; |
| 329 | |
| 330 | |
| 331 | struct flash_info { |
| 332 | struct semaphore lock; |
| 333 | struct mtd_info mtd; |
| 334 | struct chip_info *chip; |
| 335 | u8 command[5]; |
| 336 | }; |
| 337 | |
| 338 | struct flash_info *flash = NULL; |
| 339 | |
| 340 | |
| 341 | #ifdef BBU_MODE |
| 342 | #ifdef MORE_BUF_MODE |
| 343 | static int bbu_mb_spic_trans(const u8 code, const u32 addr, u8 *buf, const size_t n_tx, const size_t n_rx, int flag) |
| 344 | { |
| 345 | u32 reg; |
| 346 | int i, q, r; |
| 347 | int rc = -1; |
| 348 | |
| 349 | if (flag != SPIC_READ_BYTES && flag != SPIC_WRITE_BYTES) { |
| 350 | printk("we currently support more-byte-mode for reading and writing data only\n"); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | /* step 0. enable more byte mode */ |
| 355 | ra_or(SPI_REG_MASTER, (1 << 2)); |
| 356 | |
| 357 | bbu_spic_busy_wait(); |
| 358 | |
| 359 | /* step 1. set opcode & address, and fix cmd bit count to 32 (or 40) */ |
| 360 | #ifdef MX_4B_MODE |
| 361 | if (flash && flash->chip->addr4b) { |
| 362 | ra_and(SPI_REG_CTL, ~SPI_CTL_ADDREXT_MASK); |
| 363 | ra_or(SPI_REG_CTL, (code << 24) & SPI_CTL_ADDREXT_MASK); |
| 364 | ra_outl(SPI_REG_OPCODE, addr); |
| 365 | } |
| 366 | else |
| 367 | #endif |
| 368 | { |
| 369 | ra_outl(SPI_REG_OPCODE, (code << 24) & 0xff000000); |
| 370 | ra_or(SPI_REG_OPCODE, (addr & 0xffffff)); |
| 371 | } |
| 372 | ra_and(SPI_REG_MOREBUF, ~SPI_MBCTL_CMD_MASK); |
| 373 | #ifdef MX_4B_MODE |
| 374 | if (flash && flash->chip->addr4b) |
| 375 | ra_or(SPI_REG_MOREBUF, (40 << 24)); |
| 376 | else |
| 377 | #endif |
| 378 | ra_or(SPI_REG_MOREBUF, (32 << 24)); |
| 379 | |
| 380 | /* step 2. write DI/DO data #0 ~ #7 */ |
| 381 | if (flag & SPIC_WRITE_BYTES) { |
| 382 | if (buf == NULL) { |
| 383 | printk("%s: write null buf\n", __func__); |
| 384 | goto RET_MB_TRANS; |
| 385 | } |
| 386 | for (i = 0; i < n_tx; i++) { |
| 387 | q = i / 4; |
| 388 | r = i % 4; |
| 389 | if (r == 0) |
| 390 | ra_outl(SPI_REG_DATA(q), 0); |
| 391 | ra_or(SPI_REG_DATA(q), (*(buf + i) << (r * 8))); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /* step 3. set rx (miso_bit_cnt) and tx (mosi_bit_cnt) bit count */ |
| 396 | ra_and(SPI_REG_MOREBUF, ~SPI_MBCTL_TX_RX_CNT_MASK); |
| 397 | ra_or(SPI_REG_MOREBUF, (n_rx << 3 << 12)); |
| 398 | ra_or(SPI_REG_MOREBUF, n_tx << 3); |
| 399 | |
| 400 | /* step 4. kick */ |
| 401 | ra_or(SPI_REG_CTL, SPI_CTL_START); |
| 402 | |
| 403 | /* step 5. wait spi_master_busy */ |
| 404 | bbu_spic_busy_wait(); |
| 405 | if (flag & SPIC_WRITE_BYTES) { |
| 406 | rc = 0; |
| 407 | goto RET_MB_TRANS; |
| 408 | } |
| 409 | |
| 410 | /* step 6. read DI/DO data #0 */ |
| 411 | if (flag & SPIC_READ_BYTES) { |
| 412 | if (buf == NULL) { |
| 413 | printk("%s: read null buf\n", __func__); |
| 414 | return -1; |
| 415 | } |
| 416 | for (i = 0; i < n_rx; i++) { |
| 417 | q = i / 4; |
| 418 | r = i % 4; |
| 419 | reg = ra_inl(SPI_REG_DATA(q)); |
| 420 | *(buf + i) = (u8)(reg >> (r * 8)); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | rc = 0; |
| 425 | RET_MB_TRANS: |
| 426 | /* step #. disable more byte mode */ |
| 427 | ra_and(SPI_REG_MASTER, ~(1 << 2)); |
| 428 | return rc; |
| 429 | } |
| 430 | #endif // MORE_BUF_MODE // |
| 431 | |
| 432 | static int bbu_spic_trans(const u8 code, const u32 addr, u8 *buf, const size_t n_tx, const size_t n_rx, int flag) |
| 433 | { |
| 434 | u32 reg; |
| 435 | |
| 436 | bbu_spic_busy_wait(); |
| 437 | |
| 438 | /* step 1. set opcode & address */ |
| 439 | #ifdef MX_4B_MODE |
| 440 | if (flash && flash->chip->addr4b) { |
| 441 | ra_and(SPI_REG_CTL, ~SPI_CTL_ADDREXT_MASK); |
| 442 | ra_or(SPI_REG_CTL, addr & SPI_CTL_ADDREXT_MASK); |
| 443 | } |
| 444 | #endif |
| 445 | ra_outl(SPI_REG_OPCODE, ((addr & 0xffffff) << 8)); |
| 446 | ra_or(SPI_REG_OPCODE, code); |
| 447 | |
| 448 | /* step 2. write DI/DO data #0 */ |
| 449 | if (flag & SPIC_WRITE_BYTES) { |
| 450 | if (buf == NULL) { |
| 451 | printk("%s: write null buf\n", __func__); |
| 452 | return -1; |
| 453 | } |
| 454 | ra_outl(SPI_REG_DATA0, 0); |
| 455 | switch (n_tx) { |
| 456 | case 8: |
| 457 | ra_or(SPI_REG_DATA0, (*(buf+3) << 24)); |
| 458 | case 7: |
| 459 | ra_or(SPI_REG_DATA0, (*(buf+2) << 16)); |
| 460 | case 6: |
| 461 | ra_or(SPI_REG_DATA0, (*(buf+1) << 8)); |
| 462 | case 5: |
| 463 | case 2: |
| 464 | ra_or(SPI_REG_DATA0, *buf); |
| 465 | break; |
| 466 | default: |
| 467 | printk("%s: fixme, write of length %d\n", __func__, n_tx); |
| 468 | return -1; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /* step 3. set mosi_byte_cnt */ |
| 473 | ra_and(SPI_REG_CTL, ~SPI_CTL_TX_RX_CNT_MASK); |
| 474 | ra_or(SPI_REG_CTL, (n_rx << 4)); |
| 475 | #ifdef MX_4B_MODE |
| 476 | if (flash && flash->chip->addr4b && n_tx >= 4) |
| 477 | ra_or(SPI_REG_CTL, (n_tx + 1)); |
| 478 | else |
| 479 | #endif |
| 480 | ra_or(SPI_REG_CTL, n_tx); |
| 481 | |
| 482 | /* step 4. kick */ |
| 483 | ra_or(SPI_REG_CTL, SPI_CTL_START); |
| 484 | |
| 485 | /* step 5. wait spi_master_busy */ |
| 486 | bbu_spic_busy_wait(); |
| 487 | if (flag & SPIC_WRITE_BYTES) |
| 488 | return 0; |
| 489 | |
| 490 | /* step 6. read DI/DO data #0 */ |
| 491 | if (flag & SPIC_READ_BYTES) { |
| 492 | if (buf == NULL) { |
| 493 | printk("%s: read null buf\n", __func__); |
| 494 | return -1; |
| 495 | } |
| 496 | reg = ra_inl(SPI_REG_DATA0); |
| 497 | switch (n_rx) { |
| 498 | case 4: |
| 499 | *(buf+3) = (u8)(reg >> 24); |
| 500 | case 3: |
| 501 | *(buf+2) = (u8)(reg >> 16); |
| 502 | case 2: |
| 503 | *(buf+1) = (u8)(reg >> 8); |
| 504 | case 1: |
| 505 | *buf = (u8)reg; |
| 506 | break; |
| 507 | default: |
| 508 | printk("%s: fixme, read of length %d\n", __func__, n_rx); |
| 509 | return -1; |
| 510 | } |
| 511 | } |
| 512 | return 0; |
| 513 | } |
| 514 | #endif // BBU_MODE // |
| 515 | |
| 516 | /* |
| 517 | * read SPI flash device ID |
| 518 | */ |
| 519 | static int raspi_read_devid(u8 *rxbuf, int n_rx) |
| 520 | { |
| 521 | u8 code = OPCODE_RDID; |
| 522 | int retval; |
| 523 | |
| 524 | #ifdef USER_MODE |
| 525 | retval = spic_read(&code, 1, rxbuf, n_rx); |
| 526 | #elif defined BBU_MODE |
| 527 | retval = bbu_spic_trans(code, 0, rxbuf, 1, 3, SPIC_READ_BYTES); |
| 528 | if (!retval) |
| 529 | retval = n_rx; |
| 530 | #endif |
| 531 | if (retval != n_rx) { |
| 532 | printk("%s: ret: %x\n", __func__, retval); |
| 533 | return retval; |
| 534 | } |
| 535 | return retval; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Read the status register, returning its value in the location |
| 540 | */ |
| 541 | static int raspi_read_sr(u8 *val) |
| 542 | { |
| 543 | ssize_t retval; |
| 544 | u8 code = OPCODE_RDSR; |
| 545 | |
| 546 | #ifdef USER_MODE |
| 547 | retval = spic_read(&code, 1, val, 1); |
| 548 | #elif defined BBU_MODE |
| 549 | retval = bbu_spic_trans(code, 0, val, 1, 1, SPIC_READ_BYTES); |
| 550 | return retval; |
| 551 | #endif |
| 552 | if (retval != 1) { |
| 553 | printk("%s: ret: %x\n", __func__, retval); |
| 554 | return -EIO; |
| 555 | } |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * write status register |
| 561 | */ |
| 562 | static int raspi_write_sr(u8 *val) |
| 563 | { |
| 564 | ssize_t retval; |
| 565 | u8 code = OPCODE_WRSR; |
| 566 | |
| 567 | #ifdef USER_MODE |
| 568 | retval = spic_write(&code, 1, val, 1); |
| 569 | #elif defined BBU_MODE |
| 570 | retval = bbu_spic_trans(code, 0, val, 2, 0, SPIC_WRITE_BYTES); |
| 571 | #endif |
| 572 | if (retval != 1) { |
| 573 | printk("%s: ret: %x\n", __func__, retval); |
| 574 | return -EIO; |
| 575 | } |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | #ifdef MX_4B_MODE |
| 580 | static int raspi_4byte_mode(int enable) |
| 581 | { |
| 582 | ssize_t retval; |
| 583 | u8 code; |
| 584 | |
| 585 | code = enable? 0xB7 : 0xE9; /* B7: enter 4B, E9: exit 4B */ |
| 586 | |
| 587 | #ifdef USER_MODE |
| 588 | if (enable) |
| 589 | ra_or(RT2880_SPICFG_REG, SPICFG_ADDRMODE); |
| 590 | else |
| 591 | ra_and(RT2880_SPICFG_REG, ~(SPICFG_ADDRMODE)); |
| 592 | retval = spic_read(&code, 1, 0, 0); |
| 593 | #elif defined BBU_MODE |
| 594 | if (enable) { |
| 595 | ra_or(SPI_REG_CTL, 0x3 << 19); |
| 596 | ra_or(SPI_REG_Q_CTL, 0x3 << 8); |
| 597 | } |
| 598 | else { |
| 599 | ra_and(SPI_REG_CTL, ~SPI_CTL_SIZE_MASK); |
| 600 | ra_or(SPI_REG_CTL, 0x2 << 19); |
| 601 | ra_and(SPI_REG_Q_CTL, ~(0x3 << 8)); |
| 602 | ra_or(SPI_REG_Q_CTL, 0x2 << 8); |
| 603 | } |
| 604 | retval = bbu_spic_trans(code, 0, NULL, 1, 0, 0); |
| 605 | #endif |
| 606 | if (retval != 0) { |
| 607 | printk("%s: ret: %x\n", __func__, retval); |
| 608 | return -1; |
| 609 | } |
| 610 | return 0; |
| 611 | } |
| 612 | #endif // MX_4B_MODE // |
| 613 | |
| 614 | /* |
| 615 | * Set write enable latch with Write Enable command. |
| 616 | * Returns negative if error occurred. |
| 617 | */ |
| 618 | static inline int raspi_write_enable(void) |
| 619 | { |
| 620 | u8 code = OPCODE_WREN; |
| 621 | |
| 622 | #ifdef USER_MODE |
| 623 | return spic_write(&code, 1, NULL, 0); |
| 624 | #elif defined BBU_MODE |
| 625 | return bbu_spic_trans(code, 0, NULL, 1, 0, 0); |
| 626 | #endif |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Set all sectors (global) unprotected if they are protected. |
| 631 | * Returns negative if error occurred. |
| 632 | */ |
| 633 | static inline int raspi_unprotect(void) |
| 634 | { |
| 635 | u8 sr = 0; |
| 636 | |
| 637 | if (raspi_read_sr(&sr) < 0) { |
| 638 | printk("%s: read_sr fail: %x\n", __func__, sr); |
| 639 | return -1; |
| 640 | } |
| 641 | |
| 642 | if ((sr & (SR_BP0 | SR_BP1 | SR_BP2)) != 0) { |
| 643 | sr = 0; |
| 644 | raspi_write_sr(&sr); |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Service routine to read status register until ready, or timeout occurs. |
| 652 | * Returns non-zero if error. |
| 653 | */ |
| 654 | static int raspi_wait_ready(int sleep_ms) |
| 655 | { |
| 656 | int count; |
| 657 | int sr = 0; |
| 658 | |
| 659 | /*int timeout = sleep_ms * HZ / 1000; |
| 660 | while (timeout) |
| 661 | timeout = schedule_timeout (timeout);*/ |
| 662 | |
| 663 | /* one chip guarantees max 5 msec wait here after page writes, |
| 664 | * but potentially three seconds (!) after page erase. |
| 665 | */ |
| 666 | for (count = 0; count < ((sleep_ms+1) *1000 * 500); count++) { |
| 667 | if ((raspi_read_sr((u8 *)&sr)) < 0) |
| 668 | break; |
| 669 | else if (!(sr & (SR_WIP | SR_EPE))) { |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | udelay(1); |
| 674 | /* REVISIT sometimes sleeping would be best */ |
| 675 | } |
| 676 | |
| 677 | printk("%s: read_sr fail: %x\n", __func__, sr); |
| 678 | return -EIO; |
| 679 | } |
| 680 | |
| 681 | static int raspi_wait_sleep_ready(int sleep_ms) |
| 682 | { |
| 683 | int count; |
| 684 | int sr = 0; |
| 685 | |
| 686 | /*int timeout = sleep_ms * HZ / 1000; |
| 687 | while (timeout) |
| 688 | timeout = schedule_timeout (timeout);*/ |
| 689 | |
| 690 | /* one chip guarantees max 5 msec wait here after page writes, |
| 691 | * but potentially three seconds (!) after page erase. |
| 692 | */ |
| 693 | for (count = 0; count < ((sleep_ms+1) *1000); count++) { |
| 694 | if ((raspi_read_sr((u8 *)&sr)) < 0) |
| 695 | break; |
| 696 | else if (!(sr & (SR_WIP | SR_EPE))) { |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | usleep(1); |
| 701 | /* REVISIT sometimes sleeping would be best */ |
| 702 | } |
| 703 | |
| 704 | printk("%s: read_sr fail: %x\n", __func__, sr); |
| 705 | return -EIO; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * Erase one sector of flash memory at offset ``offset'' which is any |
| 710 | * address within the sector which should be erased. |
| 711 | * |
| 712 | * Returns 0 if successful, non-zero otherwise. |
| 713 | */ |
| 714 | static int raspi_erase_sector(u32 offset) |
| 715 | { |
| 716 | |
| 717 | u8 opcode = OPCODE_SE; |
| 718 | |
| 719 | // Here's an ugly magic number. For the partition at this address, |
| 720 | // I don't want to erase the entire partition. I only want to |
| 721 | // erase the first 4K block because there is important |
| 722 | // manufacturing data at a later block. I may play with erasesize |
| 723 | // at some time, but that requires app-layer changes also. |
| 724 | if (offset == 0x30000) { |
| 725 | opcode = OPCODE_P4E; |
| 726 | } |
| 727 | |
| 728 | /* Wait until finished previous write command. */ |
| 729 | if (raspi_wait_ready(3)) |
| 730 | return -EIO; |
| 731 | |
| 732 | /* Send write enable, then erase commands. */ |
| 733 | raspi_write_enable(); |
| 734 | raspi_unprotect(); |
| 735 | |
| 736 | #ifdef USER_MODE |
| 737 | #ifdef MX_4B_MODE |
| 738 | if (flash->chip->addr4b) { |
| 739 | raspi_4byte_mode(1); |
| 740 | flash->command[0] = opcode; |
| 741 | flash->command[1] = offset >> 24; |
| 742 | flash->command[2] = offset >> 16; |
| 743 | flash->command[3] = offset >> 8; |
| 744 | flash->command[4] = offset; |
| 745 | spic_write(flash->command, 5, 0 , 0); |
| 746 | raspi_wait_sleep_ready(950); |
| 747 | raspi_4byte_mode(0); |
| 748 | return 0; |
| 749 | } |
| 750 | #endif // MX_4B_MODE // |
| 751 | |
| 752 | /* Set up command buffer. */ |
| 753 | flash->command[0] = opcode; |
| 754 | flash->command[1] = offset >> 16; |
| 755 | flash->command[2] = offset >> 8; |
| 756 | flash->command[3] = offset; |
| 757 | |
| 758 | spic_write(flash->command, 4, 0 , 0); |
| 759 | raspi_wait_sleep_ready(950); |
| 760 | #elif defined BBU_MODE |
| 761 | #ifdef MX_4B_MODE |
| 762 | if (flash->chip->addr4b) |
| 763 | raspi_4byte_mode(1); |
| 764 | #endif |
| 765 | bbu_spic_trans(STM_OP_SECTOR_ERASE, offset, NULL, 4, 0, 0); |
| 766 | //raspi_wait_ready(950); |
| 767 | raspi_wait_sleep_ready(950); |
| 768 | #ifdef MX_4B_MODE |
| 769 | if (flash->chip->addr4b) |
| 770 | raspi_4byte_mode(0); |
| 771 | #endif |
| 772 | #endif |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | /* |
| 777 | * SPI device driver setup and teardown |
| 778 | */ |
| 779 | struct chip_info *chip_prob(void) |
| 780 | { |
| 781 | struct chip_info *info, *match; |
| 782 | u8 buf[5]; |
| 783 | u32 jedec, weight; |
| 784 | int i; |
| 785 | |
| 786 | raspi_read_devid(buf, 5); |
| 787 | jedec = (u32)((u32)(buf[1] << 24) | ((u32)buf[2] << 16) | ((u32)buf[3] <<8) | (u32)buf[4]); |
| 788 | |
| 789 | #ifdef BBU_MODE |
| 790 | printk("flash manufacture id: %x, device id %x %x\n", buf[0], buf[1], buf[2]); |
| 791 | #else |
| 792 | printk("device id : %x %x %x %x %x (%x)\n", buf[0], buf[1], buf[2], buf[3], buf[4], jedec); |
| 793 | #endif |
| 794 | |
| 795 | // FIXME, assign default as AT25D |
| 796 | weight = 0xffffffff; |
| 797 | match = &chips_data[0]; |
| 798 | for (i = 0; i < ARRAY_SIZE(chips_data); i++) { |
| 799 | info = &chips_data[i]; |
| 800 | if (info->id == buf[0]) { |
| 801 | #ifdef BBU_MODE |
| 802 | if ((u8)(info->jedec_id >> 24 & 0xff) == buf[1] && |
| 803 | (u8)(info->jedec_id >> 16 & 0xff) == buf[2]) |
| 804 | #else |
| 805 | if (info->jedec_id == jedec) |
| 806 | #endif |
| 807 | return info; |
| 808 | |
| 809 | if (weight > (info->jedec_id ^ jedec)) { |
| 810 | weight = info->jedec_id ^ jedec; |
| 811 | match = info; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | printk("Warning: un-recognized chip ID, please update SPI driver!\n"); |
| 816 | |
| 817 | return match; |
| 818 | } |
| 819 | |
| 820 | int raspi_set_lock (struct mtd_info *mtd, loff_t to, size_t len, int set) |
| 821 | { |
| 822 | u32 page_offset, page_size; |
| 823 | int retval; |
| 824 | |
| 825 | /* */ |
| 826 | while (len > 0) { |
| 827 | /* FIXME: 4b mode ? */ |
| 828 | /* write the next page to flash */ |
| 829 | flash->command[0] = (set == 0)? 0x39 : 0x36; |
| 830 | flash->command[1] = to >> 16; |
| 831 | flash->command[2] = to >> 8; |
| 832 | flash->command[3] = to; |
| 833 | |
| 834 | raspi_wait_ready(1); |
| 835 | |
| 836 | raspi_write_enable(); |
| 837 | |
| 838 | #ifdef USER_MODE |
| 839 | retval = spic_write(flash->command, 4, 0, 0); |
| 840 | #elif defined BBU_MODE |
| 841 | retval = 0; |
| 842 | //FIXME |
| 843 | #endif |
| 844 | |
| 845 | if (retval < 0) { |
| 846 | return -EIO; |
| 847 | } |
| 848 | |
| 849 | page_offset = (to & (mtd->erasesize-1)); |
| 850 | page_size = mtd->erasesize - page_offset; |
| 851 | |
| 852 | len -= mtd->erasesize; |
| 853 | to += mtd->erasesize; |
| 854 | } |
| 855 | |
| 856 | return 0; |
| 857 | |
| 858 | } |
| 859 | |
| 860 | |
| 861 | /* |
| 862 | * MTD implementation |
| 863 | */ |
| 864 | |
| 865 | /* |
| 866 | * Erase an address range on the flash chip. The address range may extend |
| 867 | * one or more erase sectors. Return an error is there is a problem erasing. |
| 868 | */ |
| 869 | static int ramtd_erase(struct mtd_info *mtd, struct erase_info *instr) |
| 870 | { |
| 871 | u32 addr,len; |
| 872 | |
| 873 | //printk("%s: addr:%x len:%x\n", __func__, instr->addr, instr->len); |
| 874 | /* sanity checks */ |
| 875 | if (instr->addr + instr->len > flash->mtd.size) |
| 876 | return -EINVAL; |
| 877 | |
| 878 | #if 0 //FIXME - undefined reference to `__umoddi3' |
| 879 | if ((instr->addr % mtd->erasesize) != 0 |
| 880 | || (instr->len % mtd->erasesize) != 0) { |
| 881 | return -EINVAL; |
| 882 | } |
| 883 | #endif |
| 884 | |
| 885 | addr = instr->addr; |
| 886 | len = instr->len; |
| 887 | |
| 888 | down(&flash->lock); |
| 889 | |
| 890 | /* now erase those sectors */ |
| 891 | while (len > 0) { |
| 892 | if (raspi_erase_sector(addr)) { |
| 893 | instr->state = MTD_ERASE_FAILED; |
| 894 | up(&flash->lock); |
| 895 | return -EIO; |
| 896 | } |
| 897 | |
| 898 | addr += mtd->erasesize; |
| 899 | len -= mtd->erasesize; |
| 900 | } |
| 901 | |
| 902 | up(&flash->lock); |
| 903 | |
| 904 | instr->state = MTD_ERASE_DONE; |
| 905 | mtd_erase_callback(instr); |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * Read an address range from the flash chip. The address range |
| 912 | * may be any size provided it is within the physical boundaries. |
| 913 | */ |
| 914 | static int ramtd_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 915 | size_t *retlen, u_char *buf) |
| 916 | { |
| 917 | size_t rdlen = 0; |
| 918 | |
| 919 | ra_dbg("%s: from:%x len:%x \n", __func__, (unsigned int)from, len); |
| 920 | |
| 921 | /* sanity checks */ |
| 922 | if ((len == 0) || (flash == NULL)) |
| 923 | return 0; |
| 924 | |
| 925 | if (from + len > flash->mtd.size) |
| 926 | return -EINVAL; |
| 927 | |
| 928 | /* Byte count starts at zero. */ |
| 929 | if (retlen) |
| 930 | *retlen = 0; |
| 931 | |
| 932 | down(&flash->lock); |
| 933 | |
| 934 | /* Wait till previous write/erase is done. */ |
| 935 | if (raspi_wait_ready(1)) { |
| 936 | /* REVISIT status return?? */ |
| 937 | up(&flash->lock); |
| 938 | return -EIO; |
| 939 | } |
| 940 | |
| 941 | #ifdef USER_MODE |
| 942 | |
| 943 | /* Set up the write data buffer. */ |
| 944 | #ifdef MX_4B_MODE |
| 945 | if (flash->chip->addr4b) { |
| 946 | raspi_4byte_mode(1); |
| 947 | flash->command[0] = OPCODE_READ; |
| 948 | flash->command[1] = from >> 24; |
| 949 | flash->command[2] = from >> 16; |
| 950 | flash->command[3] = from >> 8; |
| 951 | flash->command[4] = from; |
| 952 | rdlen = spic_read(flash->command, 5, buf, len); |
| 953 | raspi_4byte_mode(0); |
| 954 | } |
| 955 | else |
| 956 | #endif |
| 957 | { |
| 958 | flash->command[0] = OPCODE_FAST_READ; |
| 959 | flash->command[1] = from >> 16; |
| 960 | flash->command[2] = from >> 8; |
| 961 | flash->command[3] = from; |
| 962 | flash->command[4] = 0; |
| 963 | rdlen = spic_read(flash->command, 5, buf, len); |
| 964 | } |
| 965 | #elif defined BBU_MODE |
| 966 | #ifdef MX_4B_MODE |
| 967 | if (flash->chip->addr4b) |
| 968 | raspi_4byte_mode(1); |
| 969 | #endif |
| 970 | do { |
| 971 | int rc, more; |
| 972 | #ifdef MORE_BUF_MODE |
| 973 | more = 32; |
| 974 | #else |
| 975 | more = 4; |
| 976 | #endif |
| 977 | if (len - rdlen <= more) { |
| 978 | #ifdef MORE_BUF_MODE |
| 979 | rc = bbu_mb_spic_trans(STM_OP_RD_DATA, from, (buf+rdlen), 0, (len-rdlen), SPIC_READ_BYTES); |
| 980 | #else |
| 981 | rc = bbu_spic_trans(STM_OP_RD_DATA, from, (buf+rdlen), 4, (len-rdlen), SPIC_READ_BYTES); |
| 982 | #endif |
| 983 | if (rc != 0) { |
| 984 | printk("%s: failed\n", __func__); |
| 985 | break; |
| 986 | } |
| 987 | rdlen = len; |
| 988 | } |
| 989 | else { |
| 990 | #ifdef MORE_BUF_MODE |
| 991 | rc = bbu_mb_spic_trans(STM_OP_RD_DATA, from, (buf+rdlen), 0, more, SPIC_READ_BYTES); |
| 992 | #else |
| 993 | rc = bbu_spic_trans(STM_OP_RD_DATA, from, (buf+rdlen), 4, more, SPIC_READ_BYTES); |
| 994 | #endif |
| 995 | if (rc != 0) { |
| 996 | printk("%s: failed\n", __func__); |
| 997 | break; |
| 998 | } |
| 999 | rdlen += more; |
| 1000 | from += more; |
| 1001 | } |
| 1002 | } while (rdlen < len); |
| 1003 | #endif |
| 1004 | |
| 1005 | up(&flash->lock); |
| 1006 | |
| 1007 | if (retlen) |
| 1008 | *retlen = rdlen; |
| 1009 | |
| 1010 | if (rdlen != len) |
| 1011 | return -EIO; |
| 1012 | |
| 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | inline int ramtd_lock (struct mtd_info *mtd, loff_t to, uint64_t len) |
| 1017 | { |
| 1018 | //return raspi_set_lock(mtd, to, len, 1); |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | inline int ramtd_unlock (struct mtd_info *mtd, loff_t to, uint64_t len) |
| 1023 | { |
| 1024 | //return raspi_set_lock(mtd, to, len, 0); |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | /* |
| 1030 | * Write an address range to the flash chip. Data must be written in |
| 1031 | * FLASH_PAGESIZE chunks. The address range may be any size provided |
| 1032 | * it is within the physical boundaries. |
| 1033 | */ |
| 1034 | static int ramtd_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 1035 | size_t *retlen, const u_char *buf) |
| 1036 | { |
| 1037 | u32 page_offset, page_size; |
| 1038 | int rc = 0; |
| 1039 | #if defined BBU_MODE |
| 1040 | int wrto, wrlen, more; |
| 1041 | char *wrbuf; |
| 1042 | #endif |
| 1043 | int count = 0; |
| 1044 | |
| 1045 | ra_dbg("%s: to:%llx len:%x \n", __func__, to, len); |
| 1046 | if (retlen) |
| 1047 | *retlen = 0; |
| 1048 | |
| 1049 | /* sanity checks */ |
| 1050 | if (len == 0) |
| 1051 | return 0; |
| 1052 | if (to + len > flash->mtd.size) |
| 1053 | return -EINVAL; |
| 1054 | |
| 1055 | down(&flash->lock); |
| 1056 | |
| 1057 | /* Wait until finished previous write command. */ |
| 1058 | if (raspi_wait_ready(2)) { |
| 1059 | up(&flash->lock); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | |
| 1063 | #ifdef USER_MODE |
| 1064 | /* Set up the opcode in the write buffer. */ |
| 1065 | flash->command[0] = OPCODE_PP; |
| 1066 | #ifdef MX_4B_MODE |
| 1067 | if (flash->chip->addr4b) { |
| 1068 | flash->command[1] = to >> 24; |
| 1069 | flash->command[2] = to >> 16; |
| 1070 | flash->command[3] = to >> 8; |
| 1071 | flash->command[4] = to; |
| 1072 | } |
| 1073 | else |
| 1074 | #endif |
| 1075 | { |
| 1076 | flash->command[1] = to >> 16; |
| 1077 | flash->command[2] = to >> 8; |
| 1078 | flash->command[3] = to; |
| 1079 | } |
| 1080 | #endif |
| 1081 | |
| 1082 | /* what page do we start with? */ |
| 1083 | page_offset = to % FLASH_PAGESIZE; |
| 1084 | |
| 1085 | #ifdef MX_4B_MODE |
| 1086 | if (flash->chip->addr4b) |
| 1087 | raspi_4byte_mode(1); |
| 1088 | #endif |
| 1089 | |
| 1090 | /* write everything in PAGESIZE chunks */ |
| 1091 | while (len > 0) { |
| 1092 | page_size = min_t(size_t, len, FLASH_PAGESIZE-page_offset); |
| 1093 | page_offset = 0; |
| 1094 | |
| 1095 | /* write the next page to flash */ |
| 1096 | #ifdef USER_MODE |
| 1097 | #ifdef MX_4B_MODE |
| 1098 | if (flash->chip->addr4b) { |
| 1099 | flash->command[1] = to >> 24; |
| 1100 | flash->command[2] = to >> 16; |
| 1101 | flash->command[3] = to >> 8; |
| 1102 | flash->command[4] = to; |
| 1103 | } |
| 1104 | else |
| 1105 | #endif |
| 1106 | { |
| 1107 | flash->command[1] = to >> 16; |
| 1108 | flash->command[2] = to >> 8; |
| 1109 | flash->command[3] = to; |
| 1110 | } |
| 1111 | #endif |
| 1112 | |
| 1113 | raspi_wait_ready(3); |
| 1114 | raspi_write_enable(); |
| 1115 | raspi_unprotect(); |
| 1116 | |
| 1117 | #ifdef USER_MODE |
| 1118 | |
| 1119 | #ifdef MX_4B_MODE |
| 1120 | if (flash->chip->addr4b) |
| 1121 | rc = spic_write(flash->command, 5, buf, page_size); |
| 1122 | else |
| 1123 | #endif |
| 1124 | rc = spic_write(flash->command, 4, buf, page_size); |
| 1125 | |
| 1126 | #elif defined BBU_MODE |
| 1127 | wrto = to; |
| 1128 | wrlen = page_size; |
| 1129 | wrbuf = buf; |
| 1130 | rc = wrlen; |
| 1131 | do { |
| 1132 | #ifdef MORE_BUF_MODE |
| 1133 | more = 32; |
| 1134 | #else |
| 1135 | more = 4; |
| 1136 | #endif |
| 1137 | if (wrlen <= more) { |
| 1138 | #ifdef MORE_BUF_MODE |
| 1139 | bbu_mb_spic_trans(STM_OP_PAGE_PGRM, wrto, wrbuf, wrlen, 0, SPIC_WRITE_BYTES); |
| 1140 | #else |
| 1141 | bbu_spic_trans(STM_OP_PAGE_PGRM, wrto, wrbuf, wrlen+4, 0, SPIC_WRITE_BYTES); |
| 1142 | #endif |
| 1143 | wrlen = 0; |
| 1144 | } |
| 1145 | else { |
| 1146 | #ifdef MORE_BUF_MODE |
| 1147 | bbu_mb_spic_trans(STM_OP_PAGE_PGRM, wrto, wrbuf, more, 0, SPIC_WRITE_BYTES); |
| 1148 | #else |
| 1149 | bbu_spic_trans(STM_OP_PAGE_PGRM, wrto, wrbuf, more+4, 0, SPIC_WRITE_BYTES); |
| 1150 | #endif |
| 1151 | wrto += more; |
| 1152 | wrlen -= more; |
| 1153 | wrbuf += more; |
| 1154 | } |
| 1155 | if (wrlen > 0) { |
| 1156 | raspi_wait_ready(3); |
| 1157 | raspi_write_enable(); |
| 1158 | } |
| 1159 | } while (wrlen > 0); |
| 1160 | #endif |
| 1161 | //printk("%s : to:%llx page_size:%x ret:%x\n", __func__, to, page_size, rc); |
| 1162 | |
| 1163 | if (rc > 0) { |
| 1164 | if (retlen) |
| 1165 | *retlen += rc; |
| 1166 | if (rc < page_size) { |
| 1167 | #ifdef MX_4B_MODE |
| 1168 | if (flash->chip->addr4b) |
| 1169 | raspi_4byte_mode(0); |
| 1170 | #endif |
| 1171 | up(&flash->lock); |
| 1172 | printk("%s: rc:%x return:%x page_size:%x \n", |
| 1173 | __func__, rc, rc, page_size); |
| 1174 | return -EIO; |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | len -= page_size; |
| 1179 | to += page_size; |
| 1180 | buf += page_size; |
| 1181 | count++; |
| 1182 | if ((count & 0xf) == 0) |
| 1183 | raspi_wait_sleep_ready(1); |
| 1184 | |
| 1185 | } |
| 1186 | |
| 1187 | #ifdef MX_4B_MODE |
| 1188 | if (flash->chip->addr4b) |
| 1189 | raspi_4byte_mode(0); |
| 1190 | #endif |
| 1191 | |
| 1192 | up(&flash->lock); |
| 1193 | |
| 1194 | return 0; |
| 1195 | } |
| 1196 | |
| 1197 | /* |
| 1198 | * board specific setup should have ensured the SPI clock used here |
| 1199 | * matches what the READ command supports, at least until this driver |
| 1200 | * understands FAST_READ (for clocks over 25 MHz). |
| 1201 | */ |
| 1202 | static struct mtd_info *raspi_probe(struct map_info *map) |
| 1203 | { |
| 1204 | unsigned i; |
| 1205 | struct chip_info *chip; |
| 1206 | |
| 1207 | if (flash == NULL) { |
| 1208 | return NULL; |
| 1209 | } |
| 1210 | |
| 1211 | chip = flash->chip; |
| 1212 | |
| 1213 | //printk("%s\n", __FUNCTION__); |
| 1214 | |
| 1215 | printk("%s(%02x %04x) (%lld Kbytes)\n", |
| 1216 | chip->name, chip->id, chip->jedec_id, flash->mtd.size / 1024); |
| 1217 | |
| 1218 | printk("mtd .name = %s, .size = 0x%.8llx (%lluM) " |
| 1219 | ".erasesize = 0x%.8x (%uK) .numeraseregions = %d\n", |
| 1220 | flash->mtd.name, |
| 1221 | flash->mtd.size, flash->mtd.size / (1024*1024), |
| 1222 | flash->mtd.erasesize, flash->mtd.erasesize / 1024, |
| 1223 | flash->mtd.numeraseregions); |
| 1224 | |
| 1225 | if (flash->mtd.numeraseregions) |
| 1226 | for (i = 0; i < flash->mtd.numeraseregions; i++) |
| 1227 | printk("mtd.eraseregions[%d] = { .offset = 0x%.8llx, " |
| 1228 | ".erasesize = 0x%.8x (%uK), " |
| 1229 | ".numblocks = %d }\n", |
| 1230 | i, flash->mtd.eraseregions[i].offset, |
| 1231 | flash->mtd.eraseregions[i].erasesize, |
| 1232 | flash->mtd.eraseregions[i].erasesize / 1024, |
| 1233 | flash->mtd.eraseregions[i].numblocks); |
| 1234 | |
| 1235 | mtd_device_register(&flash->mtd, rt2880_partitions, nr_parts); |
| 1236 | |
| 1237 | return &flash->mtd; |
| 1238 | } |
| 1239 | |
| 1240 | |
| 1241 | static void raspi_destroy(struct mtd_info *mtd) |
| 1242 | { |
| 1243 | int status; |
| 1244 | |
| 1245 | /* Clean up MTD stuff. */ |
| 1246 | status = mtd_device_unregister(&flash->mtd); |
| 1247 | if (status == 0) { |
| 1248 | kfree(flash); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | static struct mtd_chip_driver raspi_chipdrv = { |
| 1253 | .probe = raspi_probe, |
| 1254 | .destroy = raspi_destroy, |
| 1255 | .name = "raspi_probe", |
| 1256 | .module = THIS_MODULE |
| 1257 | }; |
| 1258 | |
| 1259 | /* |
| 1260 | * Set the Intel flash back to read mode since some old boot |
| 1261 | * loaders don't. |
| 1262 | */ |
| 1263 | static int raspi_reboot_notifier(struct notifier_block *nb, unsigned long val, void *v) |
| 1264 | { |
| 1265 | |
| 1266 | #ifdef MX_4B_MODE |
| 1267 | int retval; |
| 1268 | u8 code, scur; |
| 1269 | int timeout = 10; |
| 1270 | struct mtd_info mtd; |
| 1271 | u_char buf[256]; |
| 1272 | int len; |
| 1273 | |
| 1274 | raspi_4byte_mode(0); |
| 1275 | |
| 1276 | /* |
| 1277 | This is an odd-looking hack. On some hardware (early rev |
| 1278 | Pearl for example) if the last flash activity was an |
| 1279 | erase/write cycle, the router will not reboot correctly |
| 1280 | without a power cycle. This read makes the system happier. |
| 1281 | */ |
| 1282 | mtd.size = 0x10000; |
| 1283 | ramtd_read(&mtd, 0x30000, 0x100, &len, &buf[0]); |
| 1284 | |
| 1285 | scur = SECR_4BYTE; |
| 1286 | while ((scur & SECR_4BYTE) && timeout) { |
| 1287 | timeout--; |
| 1288 | code = READ_SCUR; |
| 1289 | retval = spic_transfer(&code, 1, &scur, 1, SPIC_READ_BYTES); |
| 1290 | if (retval != 0) { |
| 1291 | printk("read scur failed\n"); |
| 1292 | scur = SECR_4BYTE; |
| 1293 | } else { |
| 1294 | //printf("scur 0x%x\n", scur); |
| 1295 | if (scur & SECR_4BYTE) { |
| 1296 | //printf("Still in 4 byte mode\n"); |
| 1297 | code = 0xE9; /* EX4B, exit 4-byte mode */ |
| 1298 | retval = spic_transfer(&code, 1, NULL, 0, SPIC_READ_BYTES); |
| 1299 | if (retval != 0) { |
| 1300 | printk("%s: ret: %x\n", __func__, retval); |
| 1301 | return(NOTIFY_OK); |
| 1302 | } |
| 1303 | usleep(500); |
| 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | #endif |
| 1309 | |
| 1310 | return(NOTIFY_OK); |
| 1311 | } |
| 1312 | |
| 1313 | |
| 1314 | static struct notifier_block raspi_notifier_block = { |
| 1315 | raspi_reboot_notifier, NULL, 0 |
| 1316 | }; |
| 1317 | |
| 1318 | static int __init raspi_init(void) |
| 1319 | { |
| 1320 | #if defined (CONFIG_RT6855A_FPGA) |
| 1321 | // only for FPGA: restore to registers since PCI initiation changes them. |
| 1322 | ra_outl(0xbfbc0028, 0x68880); |
| 1323 | ra_outl(0xbfbc0004, 0xe9); |
| 1324 | ra_outl(0xbfbc0008, 0xffffffff); |
| 1325 | ra_outl(0xbfbc0000, 0x160001); |
| 1326 | #endif |
| 1327 | |
| 1328 | register_mtd_chip_driver(&raspi_chipdrv); |
| 1329 | |
| 1330 | raspi_chipdrv.probe(NULL); |
| 1331 | |
| 1332 | register_reboot_notifier(&raspi_notifier_block); |
| 1333 | |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | static void __exit raspi_exit(void) |
| 1338 | { |
| 1339 | unregister_mtd_chip_driver(&raspi_chipdrv); |
| 1340 | unregister_reboot_notifier(&raspi_notifier_block); |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | module_init(raspi_init); |
| 1345 | module_exit(raspi_exit); |
| 1346 | |
| 1347 | /* |
| 1348 | Create dynamic MTD mappings. There are hints in |
| 1349 | the flash image for kernel and filesystem sizes. |
| 1350 | Use those for the mapping, and the Upgrade |
| 1351 | partition gets all free flash. |
| 1352 | */ |
| 1353 | void __init adjust_dynamic_maps(struct mtd_partition *parts, |
| 1354 | int number_partitions) |
| 1355 | { |
| 1356 | int i; |
| 1357 | u32 size; |
| 1358 | u32 aligned_size = 0; |
| 1359 | u32 magic; |
| 1360 | size_t retlen; |
| 1361 | u32 address; |
| 1362 | |
| 1363 | int kernel = 0, filesystem = 0, upgrade = 0; |
| 1364 | |
| 1365 | // Fake needed for the MTD Read function calls |
| 1366 | struct mtd_info mtd; |
| 1367 | |
| 1368 | #define KERNEL_MAGIC 0x27051956 |
| 1369 | #define FS_MAGIC 0x27051958 |
| 1370 | |
| 1371 | //printk("%s\n", __FUNCTION__); |
| 1372 | |
| 1373 | #if 0 |
| 1374 | printk("parts 0x%p, number_partitions %d\n", parts, number_partitions); |
| 1375 | if (0 == parts) return; |
| 1376 | for (i = 0; i < number_partitions; i++) { |
| 1377 | printk("name %s, size 0x%llx, offset 0x%llx, flags 0x%x \n", |
| 1378 | parts[i].name, parts[i].size, parts[i].offset, parts[i].mask_flags); |
| 1379 | } |
| 1380 | #endif |
| 1381 | |
| 1382 | /* Let's determine partitions based on names */ |
| 1383 | for (i = 0; i < number_partitions; i++) { |
| 1384 | if (strncmp(parts[i].name, "Kernel", strlen("Kernel")) == 0) { |
| 1385 | kernel = i; |
| 1386 | } |
| 1387 | if (strncmp(parts[i].name, "RootFS", strlen("RootFS")) == 0) { |
| 1388 | filesystem = i; |
| 1389 | } |
| 1390 | if (strncmp(parts[i].name, "Upgrade", strlen("Upgrade")) == 0) { |
| 1391 | upgrade = i; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | // SPI flash needs a read function to get a value |
| 1396 | |
| 1397 | // Find/set kernel size |
| 1398 | address = MTD_BOOT_PART_SIZE + MTD_CONFIG_PART_SIZE + MTD_FACTORY_PART_SIZE; |
| 1399 | ramtd_read(&mtd, address, sizeof(u32), &retlen, (char *)&magic); |
| 1400 | //printk("0x%x, %x\n", address, ntohl(magic)); |
| 1401 | |
| 1402 | if ((ntohl(magic) == KERNEL_MAGIC) && kernel) { |
| 1403 | ramtd_read(&mtd, address + 12, sizeof(u32), &retlen, (char *)&size); |
| 1404 | size = ntohl(size); |
| 1405 | //printk("%x\n", size); |
| 1406 | aligned_size = (size + 0x20000) & ~(0x20000 - 1); |
| 1407 | //printk("%x\n", aligned_size); |
| 1408 | parts[kernel].size = aligned_size; |
| 1409 | } |
| 1410 | |
| 1411 | // Find/Set Filesystem size |
| 1412 | address = address + aligned_size - 16; |
| 1413 | ramtd_read(&mtd, address, sizeof(u32), &retlen, (char *)&magic); |
| 1414 | //printk("0x%x %x\n", address, ntohl(magic)); |
| 1415 | |
| 1416 | if ((ntohl(magic) == FS_MAGIC) && filesystem) { |
| 1417 | ramtd_read(&mtd, address + 8, sizeof(u32), &retlen, (char *)&size); |
| 1418 | //printk("%x\n", size); |
| 1419 | //size = ntohl(size); // size is aligned correctly |
| 1420 | aligned_size = (size + 0x20000) & ~(0x20000 - 1); |
| 1421 | parts[filesystem].size = aligned_size; |
| 1422 | } |
| 1423 | |
| 1424 | // Upgrade partition has all the rest of the flash |
| 1425 | #ifdef CONFIG_MTD_TESTS_MODULE |
| 1426 | // If I'm using MTD tests, then I want a smaller |
| 1427 | // partition to play in. |
| 1428 | if (upgrade && (parts[upgrade].size == 0)) |
| 1429 | #endif |
| 1430 | { |
| 1431 | if (upgrade) { |
| 1432 | parts[upgrade].size = MTD_PART_SIZE; |
| 1433 | for (i = 0; i < number_partitions; i++) { |
| 1434 | if (i != upgrade) |
| 1435 | parts[upgrade].size -= parts[i].size; |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | #if 0 |
| 1441 | if (0 == parts) return; |
| 1442 | for (i = 0; i < number_partitions; i++) { |
| 1443 | printk("name %s, size 0x%x, offset 0x%x, flags 0x%x \n", |
| 1444 | parts[i].name, parts[i].size, parts[i].offset, parts[i].mask_flags); |
| 1445 | } |
| 1446 | #endif |
| 1447 | |
| 1448 | } |
| 1449 | |
| 1450 | static int physmap_flash_probe(struct platform_device *dev) |
| 1451 | { |
| 1452 | struct physmap_flash_data *physmap_data; |
| 1453 | struct chip_info *chip; |
| 1454 | |
| 1455 | //printk("%s init\n", __FUNCTION__); |
| 1456 | |
| 1457 | physmap_data = dev->dev.platform_data; |
| 1458 | |
| 1459 | if (physmap_data == NULL) |
| 1460 | return -ENODEV; |
| 1461 | |
| 1462 | // We do some chip driver setup here, rather than in |
| 1463 | // raspi_probe. We need to be able to read some |
| 1464 | // data from the flash part. |
| 1465 | |
| 1466 | spic_init(); |
| 1467 | |
| 1468 | chip = chip_prob(); |
| 1469 | |
| 1470 | flash = kzalloc(sizeof *flash, GFP_KERNEL); |
| 1471 | if (!flash) |
| 1472 | return -ENODEV; |
| 1473 | |
| 1474 | sema_init(&flash->lock, 1); |
| 1475 | |
| 1476 | flash->chip = chip; |
| 1477 | flash->mtd.name = "raspi"; |
| 1478 | |
| 1479 | flash->mtd.type = MTD_NORFLASH; |
| 1480 | flash->mtd.writesize = 1; |
| 1481 | flash->mtd.flags = MTD_CAP_NORFLASH; |
| 1482 | flash->mtd.size = chip->sector_size * chip->n_sectors; |
| 1483 | flash->mtd.erasesize = chip->sector_size; |
| 1484 | flash->mtd._erase = ramtd_erase; |
| 1485 | flash->mtd._read = ramtd_read; |
| 1486 | flash->mtd._write = ramtd_write; |
| 1487 | flash->mtd._lock = ramtd_lock; |
| 1488 | flash->mtd._unlock = ramtd_unlock; |
| 1489 | |
| 1490 | rt2880_partitions = physmap_data->parts; |
| 1491 | nr_parts = physmap_data->nr_parts; |
| 1492 | adjust_dynamic_maps(physmap_data->parts, physmap_data->nr_parts); |
| 1493 | |
| 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | static const struct of_device_id ralink_mtd_match[] = { |
| 1498 | { .compatible = "ralink,nor" }, |
| 1499 | {}, |
| 1500 | }; |
| 1501 | MODULE_DEVICE_TABLE(of, ralink_mtd_match); |
| 1502 | |
| 1503 | static struct platform_driver physmap_flash_driver = { |
| 1504 | .probe = physmap_flash_probe, |
| 1505 | .driver = { |
| 1506 | .name = "ralink-flash", |
| 1507 | .owner = THIS_MODULE, |
| 1508 | .of_match_table = ralink_mtd_match, |
| 1509 | }, |
| 1510 | }; |
| 1511 | |
| 1512 | static int __init ralink_physmap_init(void) |
| 1513 | { |
| 1514 | int err; |
| 1515 | |
| 1516 | //printk("*SPI* %s\n", __FUNCTION__); |
| 1517 | |
| 1518 | err = platform_driver_register(&physmap_flash_driver); |
| 1519 | return err; |
| 1520 | } |
| 1521 | |
| 1522 | static void __exit ralink_physmap_exit(void) |
| 1523 | { |
| 1524 | platform_driver_unregister(&physmap_flash_driver); |
| 1525 | } |
| 1526 | |
| 1527 | module_init(ralink_physmap_init); |
| 1528 | module_exit(ralink_physmap_exit); |
| 1529 | |
| 1530 | MODULE_LICENSE("GPL"); |
| 1531 | MODULE_AUTHOR("Steven Liu"); |
| 1532 | MODULE_DESCRIPTION("MTD SPI driver for Ralink flash chips"); |
| 1533 | |
| 1534 | int ra_mtd_write_nm(char *name, loff_t to, size_t len, const u_char *buf) |
| 1535 | { |
| 1536 | int ret = -1; |
| 1537 | size_t rdlen, wrlen; |
| 1538 | struct mtd_info *mtd; |
| 1539 | struct erase_info ei; |
| 1540 | u_char *bak = NULL; |
| 1541 | |
| 1542 | mtd = get_mtd_device_nm(name); |
| 1543 | if (IS_ERR(mtd)) |
| 1544 | return (int)mtd; |
| 1545 | if (len > mtd->erasesize) { |
| 1546 | put_mtd_device(mtd); |
| 1547 | return -E2BIG; |
| 1548 | } |
| 1549 | |
| 1550 | bak = kmalloc(mtd->erasesize, GFP_KERNEL); |
| 1551 | if (bak == NULL) { |
| 1552 | put_mtd_device(mtd); |
| 1553 | return -ENOMEM; |
| 1554 | } |
| 1555 | |
| 1556 | ret = mtd_read(mtd, 0, mtd->erasesize, &rdlen, bak); |
| 1557 | if (ret != 0) { |
| 1558 | put_mtd_device(mtd); |
| 1559 | kfree(bak); |
| 1560 | return ret; |
| 1561 | } |
| 1562 | if (rdlen != mtd->erasesize) |
| 1563 | printk("warning: ra_mtd_write: rdlen is not equal to erasesize\n"); |
| 1564 | |
| 1565 | memcpy(bak + to, buf, len); |
| 1566 | |
| 1567 | ei.mtd = mtd; |
| 1568 | ei.callback = NULL; |
| 1569 | ei.addr = 0; |
| 1570 | ei.len = mtd->erasesize; |
| 1571 | ei.priv = 0; |
| 1572 | ret = mtd_erase(mtd, &ei); |
| 1573 | if (ret != 0) { |
| 1574 | put_mtd_device(mtd); |
| 1575 | kfree(bak); |
| 1576 | return ret; |
| 1577 | } |
| 1578 | |
| 1579 | ret = mtd_write(mtd, 0, mtd->erasesize, &wrlen, bak); |
| 1580 | |
| 1581 | put_mtd_device(mtd); |
| 1582 | kfree(bak); |
| 1583 | return ret; |
| 1584 | } |
| 1585 | |
| 1586 | int ra_mtd_read_nm(char *name, loff_t from, size_t len, u_char *buf) |
| 1587 | { |
| 1588 | int ret; |
| 1589 | size_t rdlen; |
| 1590 | struct mtd_info *mtd; |
| 1591 | |
| 1592 | mtd = get_mtd_device_nm(name); |
| 1593 | if (IS_ERR(mtd)) |
| 1594 | return (int)mtd; |
| 1595 | |
| 1596 | ret = mtd_read(mtd, from, len, &rdlen, buf); |
| 1597 | if (rdlen != len) |
| 1598 | printk("warning: ra_mtd_read_nm: rdlen is not equal to len\n"); |
| 1599 | |
| 1600 | put_mtd_device(mtd); |
| 1601 | return ret; |
| 1602 | } |
| 1603 | |
| 1604 | EXPORT_SYMBOL(ra_mtd_write_nm); |
| 1605 | EXPORT_SYMBOL(ra_mtd_read_nm); |