3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
26 #include <linux/err.h>
29 #include <asm/arch/hardware.h>
30 #include <asm/arch/spr_smi.h>
32 #if !defined(CONFIG_SYS_NO_FLASH)
34 static struct smi_regs *const smicntl =
35 (struct smi_regs * const)CONFIG_SYS_SMI_BASE;
36 static ulong bank_base[CONFIG_SYS_MAX_FLASH_BANKS] =
37 CONFIG_SYS_FLASH_ADDR_BASE;
38 flash_info_t flash_info[CONFIG_SYS_MAX_FLASH_BANKS];
40 #define ST_M25Pxx_ID 0x00002020
42 static struct flash_dev flash_ids[] = {
43 {0x10, 0x10000, 2}, /* 64K Byte */
44 {0x11, 0x20000, 4}, /* 128K Byte */
45 {0x12, 0x40000, 4}, /* 256K Byte */
46 {0x13, 0x80000, 8}, /* 512K Byte */
47 {0x14, 0x100000, 16}, /* 1M Byte */
48 {0x15, 0x200000, 32}, /* 2M Byte */
49 {0x16, 0x400000, 64}, /* 4M Byte */
50 {0x17, 0x800000, 128}, /* 8M Byte */
51 {0x18, 0x1000000, 64}, /* 16M Byte */
56 * smi_wait_xfer_finish - Wait until TFF is set in status register
57 * @timeout: timeout in milliseconds
59 * Wait until TFF is set in status register
61 static void smi_wait_xfer_finish(int timeout)
64 if (readl(&smicntl->smi_sr) & TFF)
71 * smi_read_id - Read flash id
72 * @info: flash_info structure pointer
73 * @banknum: bank number
75 * Read the flash id present at bank #banknum
77 static unsigned int smi_read_id(flash_info_t *info, int banknum)
81 writel(readl(&smicntl->smi_cr1) | SW_MODE, &smicntl->smi_cr1);
82 writel(READ_ID, &smicntl->smi_tr);
83 writel((banknum << BANKSEL_SHIFT) | SEND | TX_LEN_1 | RX_LEN_3,
85 smi_wait_xfer_finish(XFER_FINISH_TOUT);
87 value = (readl(&smicntl->smi_rr) & 0x00FFFFFF);
89 writel(readl(&smicntl->smi_sr) & ~TFF, &smicntl->smi_sr);
90 writel(readl(&smicntl->smi_cr1) & ~SW_MODE, &smicntl->smi_cr1);
96 * flash_get_size - Detect the SMI flash by reading the ID.
97 * @base: Base address of the flash area bank #banknum
98 * @banknum: Bank number
100 * Detect the SMI flash by reading the ID. Initializes the flash_info structure
101 * with size, sector count etc.
103 static ulong flash_get_size(ulong base, int banknum)
105 flash_info_t *info = &flash_info[banknum];
106 struct flash_dev *dev;
108 unsigned int density;
111 value = smi_read_id(info, banknum);
112 density = (value >> 16) & 0xff;
114 for (i = 0, dev = &flash_ids[0]; dev->density != 0x0;
115 i++, dev = &flash_ids[i]) {
116 if (dev->density == density) {
117 info->size = dev->size;
118 info->sector_count = dev->sector_count;
123 if (dev->density == 0x0)
126 info->flash_id = value & 0xffff;
127 info->start[0] = base;
133 * smi_read_sr - Read status register of SMI
136 * This routine will get the status register of the flash chip present at the
139 static unsigned int smi_read_sr(int bank)
143 /* store the CTRL REG1 state */
144 ctrlreg1 = readl(&smicntl->smi_cr1);
146 /* Program SMI in HW Mode */
147 writel(readl(&smicntl->smi_cr1) & ~(SW_MODE | WB_MODE),
150 /* Performing a RSR instruction in HW mode */
151 writel((bank << BANKSEL_SHIFT) | RD_STATUS_REG, &smicntl->smi_cr2);
153 smi_wait_xfer_finish(XFER_FINISH_TOUT);
155 /* Restore the CTRL REG1 state */
156 writel(ctrlreg1, &smicntl->smi_cr1);
158 return readl(&smicntl->smi_sr);
162 * smi_wait_till_ready - Wait till last operation is over.
163 * @bank: bank number shifted.
164 * @timeout: timeout in milliseconds.
166 * This routine checks for WIP(write in progress)bit in Status register(SMSR-b0)
167 * The routine checks for #timeout loops, each at interval of 1 milli-second.
168 * If successful the routine returns 0.
170 static int smi_wait_till_ready(int bank, int timeout)
175 /* One chip guarantees max 5 msec wait here after page writes,
176 but potentially three seconds (!) after page erase. */
177 for (count = 0; count < timeout; count++) {
179 sr = smi_read_sr(bank);
182 else if (!(sr & WIP_BIT))
185 /* Try again after 1m-sec */
188 printf("SMI controller is still in wait, timeout=%d\n", timeout);
193 * smi_write_enable - Enable the flash to do write operation
196 * Set write enable latch with Write Enable command.
197 * Returns negative if error occurred.
199 static int smi_write_enable(int bank)
202 int timeout = WMODE_TOUT;
204 /* Store the CTRL REG1 state */
205 ctrlreg1 = readl(&smicntl->smi_cr1);
207 /* Program SMI in H/W Mode */
208 writel(readl(&smicntl->smi_cr1) & ~SW_MODE, &smicntl->smi_cr1);
210 /* Give the Flash, Write Enable command */
211 writel((bank << BANKSEL_SHIFT) | WE, &smicntl->smi_cr2);
213 smi_wait_xfer_finish(XFER_FINISH_TOUT);
215 /* Restore the CTRL REG1 state */
216 writel(ctrlreg1, &smicntl->smi_cr1);
219 if (smi_read_sr(bank) & (1 << (bank + WM_SHIFT)))
231 * smi_init - SMI initialization routine
233 * SMI initialization routine. Sets SMI control register1.
235 static void smi_init(void)
237 /* Setting the fast mode values. SMI working at 166/4 = 41.5 MHz */
238 writel(HOLD1 | FAST_MODE | BANK_EN | DSEL_TIME | PRESCAL4,
243 * smi_sector_erase - Erase flash sector
244 * @info: flash_info structure pointer
245 * @sector: sector number
247 * Set write enable latch with Write Enable command.
248 * Returns negative if error occurred.
250 static int smi_sector_erase(flash_info_t *info, unsigned int sector)
253 unsigned int sect_add;
254 unsigned int instruction;
256 switch (info->start[0]) {
273 sect_add = sector * (info->size / info->sector_count);
274 instruction = ((sect_add >> 8) & 0x0000FF00) | SECTOR_ERASE;
276 writel(readl(&smicntl->smi_sr) & ~(ERF1 | ERF2), &smicntl->smi_sr);
278 if (info->flash_id == ST_M25Pxx_ID) {
279 /* Wait until finished previous write command. */
280 if (smi_wait_till_ready(bank, CONFIG_SYS_FLASH_ERASE_TOUT))
283 /* Send write enable, before erase commands. */
284 if (smi_write_enable(bank))
287 /* Put SMI in SW mode */
288 writel(readl(&smicntl->smi_cr1) | SW_MODE, &smicntl->smi_cr1);
290 /* Send Sector Erase command in SW Mode */
291 writel(instruction, &smicntl->smi_tr);
292 writel((bank << BANKSEL_SHIFT) | SEND | TX_LEN_4,
294 smi_wait_xfer_finish(XFER_FINISH_TOUT);
296 if (smi_wait_till_ready(bank, CONFIG_SYS_FLASH_ERASE_TOUT))
299 /* Put SMI in HW mode */
300 writel(readl(&smicntl->smi_cr1) & ~SW_MODE,
305 /* Put SMI in HW mode */
306 writel(readl(&smicntl->smi_cr1) & ~SW_MODE,
313 * smi_write - Write to SMI flash
314 * @src_addr: source buffer
315 * @dst_addr: destination buffer
316 * @length: length to write in words
317 * @bank: bank base address
321 static int smi_write(unsigned int *src_addr, unsigned int *dst_addr,
322 unsigned int length, ulong bank_addr)
348 if (smi_wait_till_ready(banknum, CONFIG_SYS_FLASH_WRITE_TOUT))
351 /* Set SMI in Hardware Mode */
352 writel(readl(&smicntl->smi_cr1) & ~SW_MODE, &smicntl->smi_cr1);
354 if (smi_write_enable(banknum))
357 /* Perform the write command */
359 if (((ulong) (dst_addr) % SFLASH_PAGE_SIZE) == 0) {
360 if (smi_wait_till_ready(banknum,
361 CONFIG_SYS_FLASH_WRITE_TOUT))
364 if (smi_write_enable(banknum))
368 *dst_addr++ = *src_addr++;
370 if ((readl(&smicntl->smi_sr) & (ERF1 | ERF2)))
374 if (smi_wait_till_ready(banknum, CONFIG_SYS_FLASH_WRITE_TOUT))
377 writel(readl(&smicntl->smi_sr) & ~(WCF), &smicntl->smi_sr);
383 * write_buff - Write to SMI flash
384 * @info: flash info structure
385 * @src: source buffer
386 * @dest_addr: destination buffer
387 * @length: length to write in words
391 int write_buff(flash_info_t *info, uchar *src, ulong dest_addr, ulong length)
393 return smi_write((unsigned int *)src, (unsigned int *)dest_addr,
394 (length + 3) / 4, info->start[0]);
398 * flash_init - SMI flash initialization
400 * SMI flash initialization
402 unsigned long flash_init(void)
404 unsigned long size = 0;
409 for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
410 flash_info[i].flash_id = FLASH_UNKNOWN;
411 size += flash_info[i].size = flash_get_size(bank_base[i], i);
414 for (j = 0; j < CONFIG_SYS_MAX_FLASH_BANKS; j++) {
415 for (i = 1; i < flash_info[j].sector_count; i++)
416 flash_info[j].start[i] =
417 flash_info[j].start[i - 1] +
418 flash_info->size / flash_info->sector_count;
426 * flash_print_info - Print SMI flash information
428 * Print SMI flash information
430 void flash_print_info(flash_info_t *info)
433 if (info->flash_id == FLASH_UNKNOWN) {
434 puts("missing or unknown FLASH type\n");
437 printf(" Size: %ld MB in %d Sectors\n",
438 info->size >> 20, info->sector_count);
440 puts(" Sector Start Addresses:");
441 for (i = 0; i < info->sector_count; ++i) {
442 #ifdef CONFIG_SYS_FLASH_EMPTY_INFO
448 * Check if whole sector is erased
450 size = (info->size) / (info->sector_count);
451 flash = (u32 *) info->start[i];
452 size = size / sizeof(int);
454 while ((size--) && (*flash++ == ~0))
468 erased ? " E" : " ", info->protect[i] ? "RO " : " ");
473 info->start[i], info->protect[i] ? " (RO) " : " ");
481 * flash_erase - Erase SMI flash
485 int flash_erase(flash_info_t *info, int s_first, int s_last)
491 if (info->flash_id != ST_M25Pxx_ID) {
492 puts("Can't erase unknown flash type - aborted\n");
496 if ((s_first < 0) || (s_first > s_last)) {
497 puts("- no sectors to erase\n");
501 for (sect = s_first; sect <= s_last; ++sect) {
502 if (info->protect[sect])
506 printf("- Warning: %d protected sectors will not be erased!\n",
512 for (sect = s_first; sect <= s_last; sect++) {
513 if (info->protect[sect] == 0) {
514 if (smi_sector_erase(info, sect))