st_smi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * (C) Copyright 2009
  3. * Vipin Kumar, ST Microelectronics, vipin.kumar@st.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <flash.h>
  25. #include <linux/err.h>
  26. #include <linux/mtd/st_smi.h>
  27. #include <asm/io.h>
  28. #include <asm/arch/hardware.h>
  29. #if !defined(CONFIG_SYS_NO_FLASH)
  30. static struct smi_regs *const smicntl =
  31. (struct smi_regs * const)CONFIG_SYS_SMI_BASE;
  32. static ulong bank_base[CONFIG_SYS_MAX_FLASH_BANKS] =
  33. CONFIG_SYS_FLASH_ADDR_BASE;
  34. flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
  35. #define ST_M25Pxx_ID 0x00002020
  36. static struct flash_dev flash_ids[] = {
  37. {0x10, 0x10000, 2}, /* 64K Byte */
  38. {0x11, 0x20000, 4}, /* 128K Byte */
  39. {0x12, 0x40000, 4}, /* 256K Byte */
  40. {0x13, 0x80000, 8}, /* 512K Byte */
  41. {0x14, 0x100000, 16}, /* 1M Byte */
  42. {0x15, 0x200000, 32}, /* 2M Byte */
  43. {0x16, 0x400000, 64}, /* 4M Byte */
  44. {0x17, 0x800000, 128}, /* 8M Byte */
  45. {0x18, 0x1000000, 64}, /* 16M Byte */
  46. {0x00,}
  47. };
  48. /*
  49. * smi_wait_xfer_finish - Wait until TFF is set in status register
  50. * @timeout: timeout in milliseconds
  51. *
  52. * Wait until TFF is set in status register
  53. */
  54. static int smi_wait_xfer_finish(int timeout)
  55. {
  56. do {
  57. if (readl(&smicntl->smi_sr) & TFF)
  58. return 0;
  59. udelay(1000);
  60. } while (timeout--);
  61. return -1;
  62. }
  63. /*
  64. * smi_read_id - Read flash id
  65. * @info: flash_info structure pointer
  66. * @banknum: bank number
  67. *
  68. * Read the flash id present at bank #banknum
  69. */
  70. static unsigned int smi_read_id(flash_info_t *info, int banknum)
  71. {
  72. unsigned int value;
  73. writel(readl(&smicntl->smi_cr1) | SW_MODE, &smicntl->smi_cr1);
  74. writel(READ_ID, &smicntl->smi_tr);
  75. writel((banknum << BANKSEL_SHIFT) | SEND | TX_LEN_1 | RX_LEN_3,
  76. &smicntl->smi_cr2);
  77. if (smi_wait_xfer_finish(XFER_FINISH_TOUT))
  78. return -EIO;
  79. value = (readl(&smicntl->smi_rr) & 0x00FFFFFF);
  80. writel(readl(&smicntl->smi_sr) & ~TFF, &smicntl->smi_sr);
  81. writel(readl(&smicntl->smi_cr1) & ~SW_MODE, &smicntl->smi_cr1);
  82. return value;
  83. }
  84. /*
  85. * flash_get_size - Detect the SMI flash by reading the ID.
  86. * @base: Base address of the flash area bank #banknum
  87. * @banknum: Bank number
  88. *
  89. * Detect the SMI flash by reading the ID. Initializes the flash_info structure
  90. * with size, sector count etc.
  91. */
  92. static ulong flash_get_size(ulong base, int banknum)
  93. {
  94. flash_info_t *info = &flash_info[banknum];
  95. struct flash_dev *dev;
  96. int value;
  97. unsigned int density;
  98. int i;
  99. value = smi_read_id(info, banknum);
  100. if (value < 0) {
  101. printf("Flash id could not be read\n");
  102. return 0;
  103. }
  104. density = (value >> 16) & 0xff;
  105. for (i = 0, dev = &flash_ids[0]; dev->density != 0x0;
  106. i++, dev = &flash_ids[i]) {
  107. if (dev->density == density) {
  108. info->size = dev->size;
  109. info->sector_count = dev->sector_count;
  110. break;
  111. }
  112. }
  113. if (dev->density == 0x0)
  114. return 0;
  115. info->flash_id = value & 0xffff;
  116. info->start[0] = base;
  117. return info->size;
  118. }
  119. /*
  120. * smi_read_sr - Read status register of SMI
  121. * @bank: bank number
  122. *
  123. * This routine will get the status register of the flash chip present at the
  124. * given bank
  125. */
  126. static int smi_read_sr(int bank)
  127. {
  128. u32 ctrlreg1, val;
  129. /* store the CTRL REG1 state */
  130. ctrlreg1 = readl(&smicntl->smi_cr1);
  131. /* Program SMI in HW Mode */
  132. writel(readl(&smicntl->smi_cr1) & ~(SW_MODE | WB_MODE),
  133. &smicntl->smi_cr1);
  134. /* Performing a RSR instruction in HW mode */
  135. writel((bank << BANKSEL_SHIFT) | RD_STATUS_REG, &smicntl->smi_cr2);
  136. if (smi_wait_xfer_finish(XFER_FINISH_TOUT))
  137. return -1;
  138. val = readl(&smicntl->smi_sr);
  139. /* Restore the CTRL REG1 state */
  140. writel(ctrlreg1, &smicntl->smi_cr1);
  141. return val;
  142. }
  143. /*
  144. * smi_wait_till_ready - Wait till last operation is over.
  145. * @bank: bank number shifted.
  146. * @timeout: timeout in milliseconds.
  147. *
  148. * This routine checks for WIP(write in progress)bit in Status register(SMSR-b0)
  149. * The routine checks for #timeout loops, each at interval of 1 milli-second.
  150. * If successful the routine returns 0.
  151. */
  152. static int smi_wait_till_ready(int bank, int timeout)
  153. {
  154. int sr;
  155. /* One chip guarantees max 5 msec wait here after page writes,
  156. but potentially three seconds (!) after page erase. */
  157. do {
  158. sr = smi_read_sr(bank);
  159. if (sr < 0)
  160. continue; /* try until timeout */
  161. else if (!(sr & WIP_BIT))
  162. return 0;
  163. /* Try again after 1m-sec */
  164. udelay(1000);
  165. } while (timeout--);
  166. printf("SMI controller is still in wait, timeout=%d\n", timeout);
  167. return -EIO;
  168. }
  169. /*
  170. * smi_write_enable - Enable the flash to do write operation
  171. * @bank: bank number
  172. *
  173. * Set write enable latch with Write Enable command.
  174. * Returns negative if error occurred.
  175. */
  176. static int smi_write_enable(int bank)
  177. {
  178. u32 ctrlreg1;
  179. int timeout = WMODE_TOUT;
  180. int sr;
  181. /* Store the CTRL REG1 state */
  182. ctrlreg1 = readl(&smicntl->smi_cr1);
  183. /* Program SMI in H/W Mode */
  184. writel(readl(&smicntl->smi_cr1) & ~SW_MODE, &smicntl->smi_cr1);
  185. /* Give the Flash, Write Enable command */
  186. writel((bank << BANKSEL_SHIFT) | WE, &smicntl->smi_cr2);
  187. if (smi_wait_xfer_finish(XFER_FINISH_TOUT))
  188. return -1;
  189. /* Restore the CTRL REG1 state */
  190. writel(ctrlreg1, &smicntl->smi_cr1);
  191. do {
  192. sr = smi_read_sr(bank);
  193. if (sr < 0)
  194. break;
  195. else if (sr & (1 << (bank + WM_SHIFT)))
  196. return 0;
  197. /* Try again after 1m-sec */
  198. udelay(1000);
  199. } while (timeout--);
  200. return -1;
  201. }
  202. /*
  203. * smi_init - SMI initialization routine
  204. *
  205. * SMI initialization routine. Sets SMI control register1.
  206. */
  207. void smi_init(void)
  208. {
  209. /* Setting the fast mode values. SMI working at 166/4 = 41.5 MHz */
  210. writel(HOLD1 | FAST_MODE | BANK_EN | DSEL_TIME | PRESCAL4,
  211. &smicntl->smi_cr1);
  212. }
  213. /*
  214. * smi_sector_erase - Erase flash sector
  215. * @info: flash_info structure pointer
  216. * @sector: sector number
  217. *
  218. * Set write enable latch with Write Enable command.
  219. * Returns negative if error occurred.
  220. */
  221. static int smi_sector_erase(flash_info_t *info, unsigned int sector)
  222. {
  223. int bank;
  224. unsigned int sect_add;
  225. unsigned int instruction;
  226. switch (info->start[0]) {
  227. case SMIBANK0_BASE:
  228. bank = BANK0;
  229. break;
  230. case SMIBANK1_BASE:
  231. bank = BANK1;
  232. break;
  233. case SMIBANK2_BASE:
  234. bank = BANK2;
  235. break;
  236. case SMIBANK3_BASE:
  237. bank = BANK3;
  238. break;
  239. default:
  240. return -1;
  241. }
  242. sect_add = sector * (info->size / info->sector_count);
  243. instruction = ((sect_add >> 8) & 0x0000FF00) | SECTOR_ERASE;
  244. writel(readl(&smicntl->smi_sr) & ~(ERF1 | ERF2), &smicntl->smi_sr);
  245. if (info->flash_id == ST_M25Pxx_ID) {
  246. /* Wait until finished previous write command. */
  247. if (smi_wait_till_ready(bank, CONFIG_SYS_FLASH_ERASE_TOUT))
  248. return -EBUSY;
  249. /* Send write enable, before erase commands. */
  250. if (smi_write_enable(bank))
  251. return -EIO;
  252. /* Put SMI in SW mode */
  253. writel(readl(&smicntl->smi_cr1) | SW_MODE, &smicntl->smi_cr1);
  254. /* Send Sector Erase command in SW Mode */
  255. writel(instruction, &smicntl->smi_tr);
  256. writel((bank << BANKSEL_SHIFT) | SEND | TX_LEN_4,
  257. &smicntl->smi_cr2);
  258. if (smi_wait_xfer_finish(XFER_FINISH_TOUT))
  259. return -EIO;
  260. if (smi_wait_till_ready(bank, CONFIG_SYS_FLASH_ERASE_TOUT))
  261. return -EBUSY;
  262. /* Put SMI in HW mode */
  263. writel(readl(&smicntl->smi_cr1) & ~SW_MODE,
  264. &smicntl->smi_cr1);
  265. return 0;
  266. } else {
  267. /* Put SMI in HW mode */
  268. writel(readl(&smicntl->smi_cr1) & ~SW_MODE,
  269. &smicntl->smi_cr1);
  270. return -EINVAL;
  271. }
  272. }
  273. /*
  274. * smi_write - Write to SMI flash
  275. * @src_addr: source buffer
  276. * @dst_addr: destination buffer
  277. * @length: length to write in words
  278. * @bank: bank base address
  279. *
  280. * Write to SMI flash
  281. */
  282. static int smi_write(unsigned int *src_addr, unsigned int *dst_addr,
  283. unsigned int length, ulong bank_addr)
  284. {
  285. int banknum;
  286. switch (bank_addr) {
  287. case SMIBANK0_BASE:
  288. banknum = BANK0;
  289. break;
  290. case SMIBANK1_BASE:
  291. banknum = BANK1;
  292. break;
  293. case SMIBANK2_BASE:
  294. banknum = BANK2;
  295. break;
  296. case SMIBANK3_BASE:
  297. banknum = BANK3;
  298. break;
  299. default:
  300. return -1;
  301. }
  302. if (smi_wait_till_ready(banknum, CONFIG_SYS_FLASH_WRITE_TOUT))
  303. return -EBUSY;
  304. /* Set SMI in Hardware Mode */
  305. writel(readl(&smicntl->smi_cr1) & ~SW_MODE, &smicntl->smi_cr1);
  306. if (smi_write_enable(banknum))
  307. return -EIO;
  308. /* Perform the write command */
  309. while (length--) {
  310. if (((ulong) (dst_addr) % SFLASH_PAGE_SIZE) == 0) {
  311. if (smi_wait_till_ready(banknum,
  312. CONFIG_SYS_FLASH_WRITE_TOUT))
  313. return -EBUSY;
  314. if (smi_write_enable(banknum))
  315. return -EIO;
  316. }
  317. *dst_addr++ = *src_addr++;
  318. if ((readl(&smicntl->smi_sr) & (ERF1 | ERF2)))
  319. return -EIO;
  320. }
  321. if (smi_wait_till_ready(banknum, CONFIG_SYS_FLASH_WRITE_TOUT))
  322. return -EBUSY;
  323. writel(readl(&smicntl->smi_sr) & ~(WCF), &smicntl->smi_sr);
  324. return 0;
  325. }
  326. /*
  327. * write_buff - Write to SMI flash
  328. * @info: flash info structure
  329. * @src: source buffer
  330. * @dest_addr: destination buffer
  331. * @length: length to write in words
  332. *
  333. * Write to SMI flash
  334. */
  335. int write_buff(flash_info_t *info, uchar *src, ulong dest_addr, ulong length)
  336. {
  337. return smi_write((unsigned int *)src, (unsigned int *)dest_addr,
  338. (length + 3) / 4, info->start[0]);
  339. }
  340. /*
  341. * flash_init - SMI flash initialization
  342. *
  343. * SMI flash initialization
  344. */
  345. unsigned long flash_init(void)
  346. {
  347. unsigned long size = 0;
  348. int i, j;
  349. smi_init();
  350. for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  351. flash_info[i].flash_id = FLASH_UNKNOWN;
  352. size += flash_info[i].size = flash_get_size(bank_base[i], i);
  353. }
  354. for (j = 0; j < CONFIG_SYS_MAX_FLASH_BANKS; j++) {
  355. for (i = 1; i < flash_info[j].sector_count; i++)
  356. flash_info[j].start[i] =
  357. flash_info[j].start[i - 1] +
  358. flash_info->size / flash_info->sector_count;
  359. }
  360. return size;
  361. }
  362. /*
  363. * flash_print_info - Print SMI flash information
  364. *
  365. * Print SMI flash information
  366. */
  367. void flash_print_info(flash_info_t *info)
  368. {
  369. int i;
  370. if (info->flash_id == FLASH_UNKNOWN) {
  371. puts("missing or unknown FLASH type\n");
  372. return;
  373. }
  374. printf(" Size: %ld MB in %d Sectors\n",
  375. info->size >> 20, info->sector_count);
  376. puts(" Sector Start Addresses:");
  377. for (i = 0; i < info->sector_count; ++i) {
  378. #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
  379. int size;
  380. int erased;
  381. u32 *flash;
  382. /*
  383. * Check if whole sector is erased
  384. */
  385. size = (info->size) / (info->sector_count);
  386. flash = (u32 *) info->start[i];
  387. size = size / sizeof(int);
  388. while ((size--) && (*flash++ == ~0))
  389. ;
  390. size++;
  391. if (size)
  392. erased = 0;
  393. else
  394. erased = 1;
  395. if ((i % 5) == 0)
  396. printf("\n");
  397. printf(" %08lX%s%s",
  398. info->start[i],
  399. erased ? " E" : " ", info->protect[i] ? "RO " : " ");
  400. #else
  401. if ((i % 5) == 0)
  402. printf("\n ");
  403. printf(" %08lX%s",
  404. info->start[i], info->protect[i] ? " (RO) " : " ");
  405. #endif
  406. }
  407. putc('\n');
  408. return;
  409. }
  410. /*
  411. * flash_erase - Erase SMI flash
  412. *
  413. * Erase SMI flash
  414. */
  415. int flash_erase(flash_info_t *info, int s_first, int s_last)
  416. {
  417. int rcode = 0;
  418. int prot = 0;
  419. flash_sect_t sect;
  420. if (info->flash_id != ST_M25Pxx_ID) {
  421. puts("Can't erase unknown flash type - aborted\n");
  422. return 1;
  423. }
  424. if ((s_first < 0) || (s_first > s_last)) {
  425. puts("- no sectors to erase\n");
  426. return 1;
  427. }
  428. for (sect = s_first; sect <= s_last; ++sect) {
  429. if (info->protect[sect])
  430. prot++;
  431. }
  432. if (prot) {
  433. printf("- Warning: %d protected sectors will not be erased!\n",
  434. prot);
  435. } else {
  436. putc('\n');
  437. }
  438. for (sect = s_first; sect <= s_last; sect++) {
  439. if (info->protect[sect] == 0) {
  440. if (smi_sector_erase(info, sect))
  441. rcode = 1;
  442. else
  443. putc('.');
  444. }
  445. }
  446. puts(" done\n");
  447. return rcode;
  448. }
  449. #endif