spiflash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * MTD driver for the SPI Flash Memory support.
  3. *
  4. * Copyright (c) 2005-2006 Atheros Communications Inc.
  5. * Copyright (C) 2006-2007 FON Technology, SL.
  6. * Copyright (C) 2006-2007 Imre Kaloz <[email protected]>
  7. * Copyright (C) 2006-2007 Felix Fietkau <[email protected]>
  8. *
  9. * This code is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. /*===========================================================================
  15. ** !!!! VERY IMPORTANT NOTICE !!!! FLASH DATA STORED IN LITTLE ENDIAN FORMAT
  16. **
  17. ** This module contains the Serial Flash access routines for the Atheros SOC.
  18. ** The Atheros SOC integrates a SPI flash controller that is used to access
  19. ** serial flash parts. The SPI flash controller executes in "Little Endian"
  20. ** mode. THEREFORE, all WRITES and READS from the MIPS CPU must be
  21. ** BYTESWAPPED! The SPI Flash controller hardware by default performs READ
  22. ** ONLY byteswapping when accessed via the SPI Flash Alias memory region
  23. ** (Physical Address 0x0800_0000 - 0x0fff_ffff). The data stored in the
  24. ** flash sectors is stored in "Little Endian" format.
  25. **
  26. ** The spiflash_write() routine performs byteswapping on all write
  27. ** operations.
  28. **===========================================================================*/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/types.h>
  32. #include <linux/version.h>
  33. #include <linux/errno.h>
  34. #include <linux/slab.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/mtd/partitions.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/sched.h>
  39. #include <linux/squashfs_fs.h>
  40. #include <linux/root_dev.h>
  41. #include <linux/delay.h>
  42. #include <asm/delay.h>
  43. #include <asm/io.h>
  44. #include "spiflash.h"
  45. #ifndef __BIG_ENDIAN
  46. #error This driver currently only works with big endian CPU.
  47. #endif
  48. #define MAX_PARTS 32
  49. #define SPIFLASH "spiflash: "
  50. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  51. #define busy_wait(condition, wait) \
  52. do { \
  53. while (condition) { \
  54. spin_unlock_bh(&spidata->mutex); \
  55. if (wait > 1) \
  56. msleep(wait); \
  57. else if ((wait == 1) && need_resched()) \
  58. schedule(); \
  59. else \
  60. udelay(1); \
  61. spin_lock_bh(&spidata->mutex); \
  62. } \
  63. } while (0)
  64. static __u32 spiflash_regread32(int reg);
  65. static void spiflash_regwrite32(int reg, __u32 data);
  66. static __u32 spiflash_sendcmd (int op, u32 addr);
  67. int __init spiflash_init (void);
  68. void __exit spiflash_exit (void);
  69. static int spiflash_probe_chip (void);
  70. static int spiflash_erase (struct mtd_info *mtd,struct erase_info *instr);
  71. static int spiflash_read (struct mtd_info *mtd, loff_t from,size_t len,size_t *retlen,u_char *buf);
  72. static int spiflash_write (struct mtd_info *mtd,loff_t to,size_t len,size_t *retlen,const u_char *buf);
  73. /* Flash configuration table */
  74. struct flashconfig {
  75. __u32 byte_cnt;
  76. __u32 sector_cnt;
  77. __u32 sector_size;
  78. __u32 cs_addrmask;
  79. } flashconfig_tbl[MAX_FLASH] =
  80. {
  81. { 0, 0, 0, 0},
  82. { STM_1MB_BYTE_COUNT, STM_1MB_SECTOR_COUNT, STM_1MB_SECTOR_SIZE, 0x0},
  83. { STM_2MB_BYTE_COUNT, STM_2MB_SECTOR_COUNT, STM_2MB_SECTOR_SIZE, 0x0},
  84. { STM_4MB_BYTE_COUNT, STM_4MB_SECTOR_COUNT, STM_4MB_SECTOR_SIZE, 0x0},
  85. { STM_8MB_BYTE_COUNT, STM_8MB_SECTOR_COUNT, STM_8MB_SECTOR_SIZE, 0x0},
  86. { STM_16MB_BYTE_COUNT, STM_16MB_SECTOR_COUNT, STM_16MB_SECTOR_SIZE, 0x0}
  87. };
  88. /* Mapping of generic opcodes to STM serial flash opcodes */
  89. #define SPI_WRITE_ENABLE 0
  90. #define SPI_WRITE_DISABLE 1
  91. #define SPI_RD_STATUS 2
  92. #define SPI_WR_STATUS 3
  93. #define SPI_RD_DATA 4
  94. #define SPI_FAST_RD_DATA 5
  95. #define SPI_PAGE_PROGRAM 6
  96. #define SPI_SECTOR_ERASE 7
  97. #define SPI_BULK_ERASE 8
  98. #define SPI_DEEP_PWRDOWN 9
  99. #define SPI_RD_SIG 10
  100. #define SPI_MAX_OPCODES 11
  101. struct opcodes {
  102. __u16 code;
  103. __s8 tx_cnt;
  104. __s8 rx_cnt;
  105. } stm_opcodes[] = {
  106. {STM_OP_WR_ENABLE, 1, 0},
  107. {STM_OP_WR_DISABLE, 1, 0},
  108. {STM_OP_RD_STATUS, 1, 1},
  109. {STM_OP_WR_STATUS, 1, 0},
  110. {STM_OP_RD_DATA, 4, 4},
  111. {STM_OP_FAST_RD_DATA, 5, 0},
  112. {STM_OP_PAGE_PGRM, 8, 0},
  113. {STM_OP_SECTOR_ERASE, 4, 0},
  114. {STM_OP_BULK_ERASE, 1, 0},
  115. {STM_OP_DEEP_PWRDOWN, 1, 0},
  116. {STM_OP_RD_SIG, 4, 1},
  117. };
  118. /* Driver private data structure */
  119. struct spiflash_data {
  120. struct mtd_info *mtd;
  121. struct mtd_partition *parsed_parts; /* parsed partitions */
  122. void *readaddr; /* memory mapped data for read */
  123. void *mmraddr; /* memory mapped register space */
  124. wait_queue_head_t wq;
  125. spinlock_t mutex;
  126. int state;
  127. };
  128. enum {
  129. FL_READY,
  130. FL_READING,
  131. FL_ERASING,
  132. FL_WRITING
  133. };
  134. static struct spiflash_data *spidata;
  135. extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts);
  136. /***************************************************************************************************/
  137. static __u32
  138. spiflash_regread32(int reg)
  139. {
  140. volatile __u32 *data = (__u32 *)(spidata->mmraddr + reg);
  141. return (*data);
  142. }
  143. static void
  144. spiflash_regwrite32(int reg, __u32 data)
  145. {
  146. volatile __u32 *addr = (__u32 *)(spidata->mmraddr + reg);
  147. *addr = data;
  148. return;
  149. }
  150. static __u32
  151. spiflash_sendcmd (int op, u32 addr)
  152. {
  153. u32 reg;
  154. u32 mask;
  155. struct opcodes *ptr_opcode;
  156. ptr_opcode = &stm_opcodes[op];
  157. busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
  158. spiflash_regwrite32(SPI_FLASH_OPCODE, ((u32) ptr_opcode->code) | (addr << 8));
  159. reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | ptr_opcode->tx_cnt |
  160. (ptr_opcode->rx_cnt << 4) | SPI_CTL_START;
  161. spiflash_regwrite32(SPI_FLASH_CTL, reg);
  162. busy_wait(spiflash_regread32(SPI_FLASH_CTL) & SPI_CTL_BUSY, 0);
  163. if (!ptr_opcode->rx_cnt)
  164. return 0;
  165. reg = (__u32) spiflash_regread32(SPI_FLASH_DATA);
  166. switch (ptr_opcode->rx_cnt) {
  167. case 1:
  168. mask = 0x000000ff;
  169. break;
  170. case 2:
  171. mask = 0x0000ffff;
  172. break;
  173. case 3:
  174. mask = 0x00ffffff;
  175. break;
  176. default:
  177. mask = 0xffffffff;
  178. break;
  179. }
  180. reg &= mask;
  181. return reg;
  182. }
  183. /* Probe SPI flash device
  184. * Function returns 0 for failure.
  185. * and flashconfig_tbl array index for success.
  186. */
  187. static int
  188. spiflash_probe_chip (void)
  189. {
  190. __u32 sig;
  191. int flash_size;
  192. /* Read the signature on the flash device */
  193. spin_lock_bh(&spidata->mutex);
  194. sig = spiflash_sendcmd(SPI_RD_SIG, 0);
  195. spin_unlock_bh(&spidata->mutex);
  196. switch (sig) {
  197. case STM_8MBIT_SIGNATURE:
  198. flash_size = FLASH_1MB;
  199. break;
  200. case STM_16MBIT_SIGNATURE:
  201. flash_size = FLASH_2MB;
  202. break;
  203. case STM_32MBIT_SIGNATURE:
  204. flash_size = FLASH_4MB;
  205. break;
  206. case STM_64MBIT_SIGNATURE:
  207. flash_size = FLASH_8MB;
  208. break;
  209. case STM_128MBIT_SIGNATURE:
  210. flash_size = FLASH_16MB;
  211. break;
  212. default:
  213. printk (KERN_WARNING SPIFLASH "Read of flash device signature failed!\n");
  214. return (0);
  215. }
  216. return (flash_size);
  217. }
  218. /* wait until the flash chip is ready and grab a lock */
  219. static int spiflash_wait_ready(int state)
  220. {
  221. DECLARE_WAITQUEUE(wait, current);
  222. retry:
  223. spin_lock_bh(&spidata->mutex);
  224. if (spidata->state != FL_READY) {
  225. set_current_state(TASK_UNINTERRUPTIBLE);
  226. add_wait_queue(&spidata->wq, &wait);
  227. spin_unlock_bh(&spidata->mutex);
  228. schedule();
  229. remove_wait_queue(&spidata->wq, &wait);
  230. if(signal_pending(current))
  231. return 0;
  232. goto retry;
  233. }
  234. spidata->state = state;
  235. return 1;
  236. }
  237. static inline void spiflash_done(void)
  238. {
  239. spidata->state = FL_READY;
  240. spin_unlock_bh(&spidata->mutex);
  241. wake_up(&spidata->wq);
  242. }
  243. static int
  244. spiflash_erase (struct mtd_info *mtd,struct erase_info *instr)
  245. {
  246. struct opcodes *ptr_opcode;
  247. u32 temp, reg;
  248. /* sanity checks */
  249. if (instr->addr + instr->len > mtd->size) return (-EINVAL);
  250. if (!spiflash_wait_ready(FL_ERASING))
  251. return -EINTR;
  252. spiflash_sendcmd(SPI_WRITE_ENABLE, 0);
  253. busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
  254. reg = spiflash_regread32(SPI_FLASH_CTL);
  255. ptr_opcode = &stm_opcodes[SPI_SECTOR_ERASE];
  256. temp = ((__u32)instr->addr << 8) | (__u32)(ptr_opcode->code);
  257. spiflash_regwrite32(SPI_FLASH_OPCODE, temp);
  258. reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | ptr_opcode->tx_cnt | SPI_CTL_START;
  259. spiflash_regwrite32(SPI_FLASH_CTL, reg);
  260. /* this will take some time */
  261. spin_unlock_bh(&spidata->mutex);
  262. msleep(800);
  263. spin_lock_bh(&spidata->mutex);
  264. busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 20);
  265. spiflash_done();
  266. instr->state = MTD_ERASE_DONE;
  267. if (instr->callback) instr->callback (instr);
  268. return 0;
  269. }
  270. static int
  271. spiflash_read (struct mtd_info *mtd, loff_t from,size_t len,size_t *retlen,u_char *buf)
  272. {
  273. u8 *read_addr;
  274. /* sanity checks */
  275. if (!len) return (0);
  276. if (from + len > mtd->size) return (-EINVAL);
  277. /* we always read len bytes */
  278. *retlen = len;
  279. if (!spiflash_wait_ready(FL_READING))
  280. return -EINTR;
  281. read_addr = (u8 *)(spidata->readaddr + from);
  282. memcpy(buf, read_addr, len);
  283. spiflash_done();
  284. return 0;
  285. }
  286. static int
  287. spiflash_write (struct mtd_info *mtd,loff_t to,size_t len,size_t *retlen,const u_char *buf)
  288. {
  289. u32 opcode, bytes_left;
  290. *retlen = 0;
  291. /* sanity checks */
  292. if (!len) return (0);
  293. if (to + len > mtd->size) return (-EINVAL);
  294. opcode = stm_opcodes[SPI_PAGE_PROGRAM].code;
  295. bytes_left = len;
  296. do {
  297. u32 xact_len, reg, page_offset, spi_data = 0;
  298. xact_len = MIN(bytes_left, sizeof(__u32));
  299. /* 32-bit writes cannot span across a page boundary
  300. * (256 bytes). This types of writes require two page
  301. * program operations to handle it correctly. The STM part
  302. * will write the overflow data to the beginning of the
  303. * current page as opposed to the subsequent page.
  304. */
  305. page_offset = (to & (STM_PAGE_SIZE - 1)) + xact_len;
  306. if (page_offset > STM_PAGE_SIZE) {
  307. xact_len -= (page_offset - STM_PAGE_SIZE);
  308. }
  309. if (!spiflash_wait_ready(FL_WRITING))
  310. return -EINTR;
  311. spiflash_sendcmd(SPI_WRITE_ENABLE, 0);
  312. switch (xact_len) {
  313. case 1:
  314. spi_data = (u32) ((u8) *buf);
  315. break;
  316. case 2:
  317. spi_data = (buf[1] << 8) | buf[0];
  318. break;
  319. case 3:
  320. spi_data = (buf[2] << 16) | (buf[1] << 8) | buf[0];
  321. break;
  322. case 4:
  323. spi_data = (buf[3] << 24) | (buf[2] << 16) |
  324. (buf[1] << 8) | buf[0];
  325. break;
  326. default:
  327. spi_data = 0;
  328. break;
  329. }
  330. spiflash_regwrite32(SPI_FLASH_DATA, spi_data);
  331. opcode = (opcode & SPI_OPCODE_MASK) | ((__u32)to << 8);
  332. spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
  333. reg = spiflash_regread32(SPI_FLASH_CTL);
  334. reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | (xact_len + 4) | SPI_CTL_START;
  335. spiflash_regwrite32(SPI_FLASH_CTL, reg);
  336. /* give the chip some time before we start busy waiting */
  337. spin_unlock_bh(&spidata->mutex);
  338. schedule();
  339. spin_lock_bh(&spidata->mutex);
  340. busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 0);
  341. spiflash_done();
  342. bytes_left -= xact_len;
  343. to += xact_len;
  344. buf += xact_len;
  345. *retlen += xact_len;
  346. } while (bytes_left != 0);
  347. return 0;
  348. }
  349. #ifdef CONFIG_MTD_PARTITIONS
  350. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", "MyLoader", NULL };
  351. #endif
  352. static int spiflash_probe(struct platform_device *pdev)
  353. {
  354. int result = -1;
  355. int index, num_parts;
  356. struct mtd_info *mtd;
  357. spidata->mmraddr = ioremap_nocache(SPI_FLASH_MMR, SPI_FLASH_MMR_SIZE);
  358. spin_lock_init(&spidata->mutex);
  359. init_waitqueue_head(&spidata->wq);
  360. spidata->state = FL_READY;
  361. if (!spidata->mmraddr) {
  362. printk (KERN_WARNING SPIFLASH "Failed to map flash device\n");
  363. kfree(spidata);
  364. spidata = NULL;
  365. }
  366. mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  367. if (!mtd) {
  368. kfree(spidata);
  369. return -ENXIO;
  370. }
  371. if (!(index = spiflash_probe_chip())) {
  372. printk (KERN_WARNING SPIFLASH "Found no serial flash device\n");
  373. goto error;
  374. }
  375. spidata->readaddr = ioremap_nocache(SPI_FLASH_READ, flashconfig_tbl[index].byte_cnt);
  376. if (!spidata->readaddr) {
  377. printk (KERN_WARNING SPIFLASH "Failed to map flash device\n");
  378. goto error;
  379. }
  380. mtd->name = "spiflash";
  381. mtd->type = MTD_NORFLASH;
  382. mtd->flags = (MTD_CAP_NORFLASH|MTD_WRITEABLE);
  383. mtd->size = flashconfig_tbl[index].byte_cnt;
  384. mtd->erasesize = flashconfig_tbl[index].sector_size;
  385. mtd->writesize = 1;
  386. mtd->numeraseregions = 0;
  387. mtd->eraseregions = NULL;
  388. mtd->erase = spiflash_erase;
  389. mtd->read = spiflash_read;
  390. mtd->write = spiflash_write;
  391. mtd->owner = THIS_MODULE;
  392. /* parse redboot partitions */
  393. num_parts = parse_mtd_partitions(mtd, part_probe_types, &spidata->parsed_parts, 0);
  394. if (!num_parts)
  395. goto error;
  396. result = add_mtd_partitions(mtd, spidata->parsed_parts, num_parts);
  397. spidata->mtd = mtd;
  398. return (result);
  399. error:
  400. kfree(mtd);
  401. kfree(spidata);
  402. return -ENXIO;
  403. }
  404. static int spiflash_remove (struct platform_device *pdev)
  405. {
  406. del_mtd_partitions (spidata->mtd);
  407. kfree(spidata->mtd);
  408. return 0;
  409. }
  410. struct platform_driver spiflash_driver = {
  411. .driver.name = "spiflash",
  412. .probe = spiflash_probe,
  413. .remove = spiflash_remove,
  414. };
  415. int __init
  416. spiflash_init (void)
  417. {
  418. spidata = kmalloc(sizeof(struct spiflash_data), GFP_KERNEL);
  419. if (!spidata)
  420. return (-ENXIO);
  421. spin_lock_init(&spidata->mutex);
  422. platform_driver_register(&spiflash_driver);
  423. return 0;
  424. }
  425. void __exit
  426. spiflash_exit (void)
  427. {
  428. kfree(spidata);
  429. }
  430. module_init (spiflash_init);
  431. module_exit (spiflash_exit);
  432. MODULE_LICENSE("GPL");
  433. MODULE_AUTHOR("OpenWrt.org, Atheros Communications Inc");
  434. MODULE_DESCRIPTION("MTD driver for SPI Flash on Atheros SOC");