root/src/linux/ar531x/linux-2.6.23/drivers/mtd/devices/spiflash.c

Revision 12400, 22.5 kB (checked in by BrainSlayer, 5 months ago)

more flexible EOC5610 partition layout, small performance increase by module remapping, lzma decoder speed improvements

Line 
1
2 /*
3  * MTD driver for the SPI Flash Memory support.
4  *
5  * Copyright (c) 2005-2006 Atheros Communications Inc.
6  * Copyright (C) 2006-2007 FON Technology, SL.
7  * Copyright (C) 2006-2007 Imre Kaloz <kaloz@openwrt.org>
8  * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org>
9  * Copyright (C) 2008 Sebastian Gottschall <s.gottschall@newmedia-net.de>
10  *
11  * This code is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  */
16
17 /*===========================================================================
18 ** !!!!  VERY IMPORTANT NOTICE !!!!  FLASH DATA STORED IN LITTLE ENDIAN FORMAT
19 **
20 ** This module contains the Serial Flash access routines for the Atheros SOC.
21 ** The Atheros SOC integrates a SPI flash controller that is used to access
22 ** serial flash parts. The SPI flash controller executes in "Little Endian"
23 ** mode. THEREFORE, all WRITES and READS from the MIPS CPU must be
24 ** BYTESWAPPED! The SPI Flash controller hardware by default performs READ
25 ** ONLY byteswapping when accessed via the SPI Flash Alias memory region
26 ** (Physical Address 0x0800_0000 - 0x0fff_ffff). The data stored in the
27 ** flash sectors is stored in "Little Endian" format.
28 **
29 ** The spiflash_write() routine performs byteswapping on all write
30 ** operations.
31 **===========================================================================*/
32
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/types.h>
36 #include <linux/version.h>
37 #include <linux/errno.h>
38 #include <linux/slab.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/mtd/partitions.h>
41 #include <linux/platform_device.h>
42 #include <linux/sched.h>
43 #include <linux/squashfs_fs.h>
44 #include <linux/root_dev.h>
45 #include <linux/delay.h>
46 #include <linux/proc_fs.h>
47 #include <asm/delay.h>
48 #include <asm/io.h>
49 #include "spiflash.h"
50
51 #ifndef __BIG_ENDIAN
52 #error This driver currently only works with big endian CPU.
53 #endif
54
55 #define MAX_PARTS 32
56
57 #define SPIFLASH "spiflash: "
58
59 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
60
61 #define busy_wait(condition, wait) \
62         do { \
63                 while (condition) { \
64                         spin_unlock_bh(&spidata->mutex); \
65                         if (wait > 1) \
66                                 msleep(wait); \
67                         else if ((wait == 1) && need_resched()) \
68                                 schedule(); \
69                         else \
70                                 udelay(1); \
71                         spin_lock_bh(&spidata->mutex); \
72                 } \
73         } while (0)
74                
75
76 static __u32 spiflash_regread32(int reg);
77 static void spiflash_regwrite32(int reg, __u32 data);
78 static __u32 spiflash_sendcmd (int op, u32 addr);
79
80 int __init spiflash_init (void);
81 void __exit spiflash_exit (void);
82 static int spiflash_probe_chip (void);
83 static int spiflash_erase (struct mtd_info *mtd,struct erase_info *instr);
84 static int spiflash_read (struct mtd_info *mtd, loff_t from,size_t len,size_t *retlen,u_char *buf);
85 static int spiflash_write (struct mtd_info *mtd,loff_t to,size_t len,size_t *retlen,const u_char *buf);
86
87 /* Flash configuration table */
88 struct flashconfig {
89     __u32 byte_cnt;
90     __u32 sector_cnt;
91     __u32 sector_size;
92     __u32 cs_addrmask;
93 } flashconfig_tbl[MAX_FLASH] =
94     {
95         { 0, 0, 0, 0},
96         { STM_1MB_BYTE_COUNT, STM_1MB_SECTOR_COUNT, STM_1MB_SECTOR_SIZE, 0x0},
97         { STM_2MB_BYTE_COUNT, STM_2MB_SECTOR_COUNT, STM_2MB_SECTOR_SIZE, 0x0},
98         { STM_4MB_BYTE_COUNT, STM_4MB_SECTOR_COUNT, STM_4MB_SECTOR_SIZE, 0x0},
99         { STM_8MB_BYTE_COUNT, STM_8MB_SECTOR_COUNT, STM_8MB_SECTOR_SIZE, 0x0},
100         { STM_16MB_BYTE_COUNT, STM_16MB_SECTOR_COUNT, STM_16MB_SECTOR_SIZE, 0x0}
101     };
102
103 /* Mapping of generic opcodes to STM serial flash opcodes */
104 #define SPI_WRITE_ENABLE    0
105 #define SPI_WRITE_DISABLE   1
106 #define SPI_RD_STATUS       2
107 #define SPI_WR_STATUS       3
108 #define SPI_RD_DATA         4
109 #define SPI_FAST_RD_DATA    5
110 #define SPI_PAGE_PROGRAM    6
111 #define SPI_SECTOR_ERASE    7
112 #define SPI_BULK_ERASE      8
113 #define SPI_DEEP_PWRDOWN    9
114 #define SPI_RD_SIG          10
115 #define SPI_MAX_OPCODES     11
116
117 struct opcodes {
118     __u16 code;
119     __s8 tx_cnt;
120     __s8 rx_cnt;
121 } stm_opcodes[] = {
122         {STM_OP_WR_ENABLE, 1, 0},
123         {STM_OP_WR_DISABLE, 1, 0},
124         {STM_OP_RD_STATUS, 1, 1},
125         {STM_OP_WR_STATUS, 1, 0},
126         {STM_OP_RD_DATA, 4, 4},
127         {STM_OP_FAST_RD_DATA, 5, 0},
128         {STM_OP_PAGE_PGRM, 8, 0},
129         {STM_OP_SECTOR_ERASE, 4, 0},
130         {STM_OP_BULK_ERASE, 1, 0},
131         {STM_OP_DEEP_PWRDOWN, 1, 0},
132         {STM_OP_RD_SIG, 4, 1},
133 };
134
135 /* Driver private data structure */
136 struct spiflash_data {
137         struct  mtd_info       *mtd;   
138         struct  mtd_partition  *parsed_parts;     /* parsed partitions */
139         void    *readaddr; /* memory mapped data for read  */
140         void    *mmraddr;  /* memory mapped register space */
141         wait_queue_head_t wq;
142         spinlock_t mutex;
143         int state;
144 };
145 enum {
146         FL_READY,
147         FL_READING,
148         FL_ERASING,
149         FL_WRITING
150 };
151
152 static struct spiflash_data *spidata;
153
154 extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts);
155
156 #ifdef CONFIG_MTD_SPIFLASH_PP
157 /*
158  * With AR2317, WRG-G19, we add the external circuit to implement page
159  * programming. The GPIO 0 is used to control the chip select of the SPI
160  * interface. The chip select is low active.
161  *
162  *                                                              david_hsieh@alphanetworks.com
163  */
164
165 /* The following part is cut from arch/mips/ar531x/ar531x.h */
166
167 #include <asm/addrspace.h>
168
169 #define AR5315_DSLBASE          0xB1000000      /* RESET CONTROL MMR */
170
171 /* GPIO */
172 #define AR5315_GPIO_DI          (AR5315_DSLBASE + 0x0088)
173 #define AR5315_GPIO_DO          (AR5315_DSLBASE + 0x0090)
174 #define AR5315_GPIO_CR          (AR5315_DSLBASE + 0x0098)
175 #define AR5315_GPIO_INT         (AR5315_DSLBASE + 0x00a0)
176
177 /* Chip Select GPIO for Page Programming */
178 #ifndef CONFIG_MTD_SPIFLASH_PP_GPIO
179 #define CONFIG_MTD_SPIFLASH_PP_GPIO 0
180 #endif
181 #define SPI_CS_BIT_MASK (1 << CONFIG_MTD_SPIFLASH_PP_GPIO)
182
183 typedef unsigned int AR531X_REG;
184 #define sysRegRead(phys)                (*(volatile AR531X_REG *)KSEG1ADDR(phys))
185 #define sysRegWrite(phys, val)  ((*(volatile AR531X_REG *)KSEG1ADDR(phys)) = (val))
186
187 static atomic_t spiflash_cs = ATOMIC_INIT(0);
188
189 static inline void chip_select(int value)
190 {
191         __u32 reg;
192
193         /* Set GPIO 0 as output. */
194         reg = sysRegRead(AR5315_GPIO_CR);
195         reg |= SPI_CS_BIT_MASK;
196         sysRegWrite(AR5315_GPIO_CR, reg);
197
198         /* Set GPIO 0 data. */
199         reg = sysRegRead(AR5315_GPIO_DO);
200         if (value) reg |= SPI_CS_BIT_MASK;
201         else reg &= ~SPI_CS_BIT_MASK;
202         sysRegWrite(AR5315_GPIO_DO, reg);
203 }
204
205 #define SET_SPI_ACTIVITY()                                              \
206 {                                                                                               \
207 }
208
209 #define CLEAR_SPI_ACTIVITY()                                    \
210 {                                                                                               \
211         chip_select(1);                                                         \
212 }
213
214 #else
215
216 #define SET_SPI_ACTIVITY()
217 #define CLEAR_SPI_ACTIVITY()
218
219 #endif
220
221
222
223 /***************************************************************************************************/
224
225 static __u32
226 spiflash_regread32(int reg)
227 {
228         volatile __u32 *data = (__u32 *)(spidata->mmraddr + reg);
229
230         return (*data);
231 }
232
233 static void
234 spiflash_regwrite32(int reg, __u32 data)
235 {
236         volatile __u32 *addr = (__u32 *)(spidata->mmraddr + reg);
237
238         *addr = data;
239         return;
240 }
241
242
243 static __u32
244 spiflash_sendcmd (int op, u32 addr)
245 {
246          u32 reg;
247          u32 mask;
248         struct opcodes *ptr_opcode;
249
250         ptr_opcode = &stm_opcodes[op];
251         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
252         spiflash_regwrite32(SPI_FLASH_OPCODE, ((u32) ptr_opcode->code) | (addr << 8));
253
254         reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | ptr_opcode->tx_cnt |
255                 (ptr_opcode->rx_cnt << 4) | SPI_CTL_START;
256
257         spiflash_regwrite32(SPI_FLASH_CTL, reg);
258
259         busy_wait(spiflash_regread32(SPI_FLASH_CTL) & SPI_CTL_BUSY, 0);
260  
261         if (!ptr_opcode->rx_cnt)
262                 return 0;
263
264         reg = (__u32) spiflash_regread32(SPI_FLASH_DATA);
265
266         switch (ptr_opcode->rx_cnt) {
267         case 1:
268                         mask = 0x000000ff;
269                         break;
270         case 2:
271                         mask = 0x0000ffff;
272                         break;
273         case 3:
274                         mask = 0x00ffffff;
275                         break;
276         default:
277                         mask = 0xffffffff;
278                         break;
279         }
280         reg &= mask;
281
282         return reg;
283 }
284
285
286
287 /* Probe SPI flash device
288  * Function returns 0 for failure.
289  * and flashconfig_tbl array index for success.
290  */
291 static int
292 spiflash_probe_chip (void)
293 {
294         __u32 sig;
295         int flash_size;
296        
297         /* Read the signature on the flash device */
298         spin_lock_bh(&spidata->mutex);
299         sig = spiflash_sendcmd(SPI_RD_SIG, 0);
300         spin_unlock_bh(&spidata->mutex);
301
302         switch (sig) {
303         case STM_8MBIT_SIGNATURE:
304                 flash_size = FLASH_1MB;
305                 break;
306         case STM_16MBIT_SIGNATURE:
307                 flash_size = FLASH_2MB;
308                 break;
309         case STM_32MBIT_SIGNATURE:
310                 flash_size = FLASH_4MB;
311                 break;
312         case STM_64MBIT_SIGNATURE:
313                 flash_size = FLASH_8MB;
314                 break;
315         case STM_128MBIT_SIGNATURE:
316                 flash_size = FLASH_16MB;
317                 break;
318         default:
319                 printk (KERN_WARNING SPIFLASH "Read of flash device signature failed!\n");
320                 return (0);
321         }
322
323         return (flash_size);
324 }
325
326
327 /* wait until the flash chip is ready and grab a lock */
328 static int spiflash_wait_ready(int state)
329 {
330         DECLARE_WAITQUEUE(wait, current);
331
332 retry:
333         spin_lock_bh(&spidata->mutex);
334         if (spidata->state != FL_READY) {
335                 set_current_state(TASK_UNINTERRUPTIBLE);
336                 add_wait_queue(&spidata->wq, &wait);
337                 spin_unlock_bh(&spidata->mutex);
338                 schedule();
339                 remove_wait_queue(&spidata->wq, &wait);
340                
341                 if(signal_pending(current))
342                         return 0;
343
344        
345                 goto retry;
346         }
347         spidata->state = state;
348
349         return 1;
350 }
351
352 static inline void spiflash_done(void)
353 {
354         spidata->state = FL_READY;
355         spin_unlock_bh(&spidata->mutex);
356         wake_up(&spidata->wq);
357 }
358
359 static int
360 spiflash_erase (struct mtd_info *mtd,struct erase_info *instr)
361 {
362         struct opcodes *ptr_opcode;
363         __u32 temp, reg;
364         int finished = 0;
365         unsigned int addr = instr->addr;
366
367 #ifdef SPIFLASH_DEBUG
368         printk (KERN_DEBUG "%s(addr = 0x%.8x, len = %d)\n",__FUNCTION__,instr->addr,instr->len);
369 #endif
370
371         /* sanity checks */
372         if (instr->addr + instr->len > mtd->size) return (-EINVAL);
373         if (!spiflash_wait_ready(FL_ERASING))
374                 return -EINTR;
375 for (addr=instr->addr;addr<instr->addr+instr->len;addr+=mtd->erasesize)
376 {
377
378         ptr_opcode = &stm_opcodes[SPI_SECTOR_ERASE];
379
380         temp = ((__u32)addr << 8) | (__u32)(ptr_opcode->code);
381         spiflash_sendcmd(SPI_WRITE_ENABLE,0);
382         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
383
384         spiflash_regwrite32(SPI_FLASH_OPCODE, temp);
385
386         reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | ptr_opcode->tx_cnt | SPI_CTL_START;
387         spiflash_regwrite32(SPI_FLASH_CTL, reg);
388
389         busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 20);
390 }
391         spiflash_done();
392
393         instr->state = MTD_ERASE_DONE;
394         if (instr->callback) instr->callback (instr);
395 #ifdef SPIFLASH_DEBUG
396         printk (KERN_DEBUG "%s return\n",__FUNCTION__);
397 #endif
398         return (0);
399 }
400
401 static int
402 spiflash_read (struct mtd_info *mtd, loff_t from,size_t len,size_t *retlen,u_char *buf)
403 {
404         u8 *read_addr;
405        
406         /* sanity checks */
407         if (!len) return (0);
408         if (from + len > mtd->size) return (-EINVAL);
409        
410         /* we always read len bytes */
411         *retlen = len;
412
413         if (!spiflash_wait_ready(FL_READING))
414                 return -EINTR;
415         read_addr = (u8 *)(spidata->readaddr + from);
416         memcpy(buf, read_addr, len);
417         spiflash_done();
418
419         return 0;
420 }
421
422 static int
423 spiflash_write (struct mtd_info *mtd,loff_t to,size_t len,size_t *retlen,const u_char *buf)
424 {
425         u32 opcode, bytes_left;
426
427         *retlen = 0;
428
429         /* sanity checks */
430         if (!len) return (0);
431         if (to + len > mtd->size) return (-EINVAL);
432        
433         opcode = stm_opcodes[SPI_PAGE_PROGRAM].code;
434         bytes_left = len;
435        
436         do {
437                 u32 xact_len, reg, page_offset, spi_data = 0;
438
439                 xact_len = MIN(bytes_left, sizeof(__u32));
440
441                 /* 32-bit writes cannot span across a page boundary
442                  * (256 bytes). This types of writes require two page
443                  * program operations to handle it correctly. The STM part
444                  * will write the overflow data to the beginning of the
445                  * current page as opposed to the subsequent page.
446                  */
447                 page_offset = (to & (STM_PAGE_SIZE - 1)) + xact_len;
448
449                 if (page_offset > STM_PAGE_SIZE) {
450                         xact_len -= (page_offset - STM_PAGE_SIZE);
451                 }
452
453                 if (!spiflash_wait_ready(FL_WRITING))
454                         return -EINTR;
455
456                 spiflash_sendcmd(SPI_WRITE_ENABLE, 0);
457                 switch (xact_len) {
458                         case 1:
459                                 spi_data = (u32) ((u8) *buf);
460                                 break;
461                         case 2:
462                                 spi_data = (buf[1] << 8) | buf[0];
463                                 break;
464                         case 3:
465                                 spi_data = (buf[2] << 16) | (buf[1] << 8) | buf[0];
466                                 break;
467                         case 4:
468                                 spi_data = (buf[3] << 24) | (buf[2] << 16) |
469                                                         (buf[1] << 8) | buf[0];
470                                 break;
471                         default:
472                                 spi_data = 0;
473                                 break;
474                 }
475
476                 spiflash_regwrite32(SPI_FLASH_DATA, spi_data);
477                 opcode = (opcode & SPI_OPCODE_MASK) | ((__u32)to << 8);
478                 spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
479
480                 reg = spiflash_regread32(SPI_FLASH_CTL);
481                 reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | (xact_len + 4) | SPI_CTL_START;
482                 spiflash_regwrite32(SPI_FLASH_CTL, reg);
483
484                 /* give the chip some time before we start busy waiting */
485                 spin_unlock_bh(&spidata->mutex);
486                 schedule();
487                 spin_lock_bh(&spidata->mutex);
488
489                 busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 0);
490                 spiflash_done();
491
492                 bytes_left -= xact_len;
493                 to += xact_len;
494                 buf += xact_len;
495
496                 *retlen += xact_len;
497         } while (bytes_left != 0);
498
499         return 0;
500 }
501
502 #ifdef CONFIG_MTD_SPIFLASH_PP
503
504 static void page_write(loff_t to, const u_char * buf)
505 {
506         __u32   reg, spi_data, opcode;
507         int             i;
508
509
510         /* We are going to write flash now, do write enable first. */
511         spiflash_sendcmd(SPI_WRITE_ENABLE, 0);
512
513         /* we are not really waiting for CPU spiflash activity, just need the value of the register. */
514         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
515
516         /* Prepare SPI opcode, data and control register values. */
517         opcode   = (stm_opcodes[SPI_PAGE_PROGRAM].code & SPI_OPCODE_MASK) | ((__u32)to << 8);
518         spi_data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; buf += 4;
519         reg      = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | 0x8 | SPI_CTL_START;
520
521         /* wait and mark our activity */
522         if (!spiflash_wait_ready(FL_WRITING))
523                 return -EINTR;
524         SET_SPI_ACTIVITY();
525         chip_select(0);
526
527         /* Send out the the first 4 bytes. */
528         spiflash_regwrite32(SPI_FLASH_DATA, spi_data);
529         spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
530         spiflash_regwrite32(SPI_FLASH_CTL, reg);
531
532         /* 31 loops, each loop send 8 bytes */
533         for (i=0; i<31; i++)
534         {
535                 busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
536
537                 /*
538                  * The sample code from the application node is:
539                  *
540                  *      spi_data = (UINT32)*((UINT32 *)buf);
541                  *      spi_data = cpi2le32(spi_data);
542                  *      spi_data_swapped =
543                  *                      (((spi_data>>8) & 0xff) << 24) |
544                  *                      (((spi_data>>24)& 0xff) << 8) |
545                  *                      (spi_data & 0x00ff00ff);
546                  */
547                 opcode   = (buf[3] <<  8) | (buf[2] << 16) | (buf[1] << 24) | buf[0]; buf += 4;
548                 spi_data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] <<  8) | buf[0]; buf += 4;
549                 reg      = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | 0x8 | SPI_CTL_START;
550
551                 spiflash_regwrite32(SPI_FLASH_DATA, spi_data);
552                 spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
553                 spiflash_regwrite32(SPI_FLASH_CTL, reg);
554         }
555
556         /* send out the last 4 bytes */
557         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
558
559         opcode   = (buf[3] <<  8) | (buf[2] << 16) | (buf[1] << 24) | buf[0]; buf += 4;
560         reg      = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | 0x4 | SPI_CTL_START;
561
562         spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
563         spiflash_regwrite32(SPI_FLASH_CTL, reg);
564
565         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
566
567         /* Deactive chip select */
568         chip_select(1);
569         /* clean our activity */
570         CLEAR_SPI_ACTIVITY();
571        
572
573         busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 20);
574         spiflash_done();
575         return;
576 }
577
578 /*
579  * Do page programming test.
580  * The 'block' should be erase already.
581  * We try to use page programming mode to write flash,
582  * and erase this block again before return.
583  */
584 static int test_page_programming(struct mtd_info * mtd, loff_t block)
585 {
586         unsigned char   buffer[256];
587         unsigned char * flash;
588         struct opcodes *ptr_opcode;
589         __u32                   opcode, reg;
590         int                             i;
591
592
593         /* write the flash with known pattern */
594         for (i=0; i<256; i++) buffer[i] = (unsigned char)i;
595         page_write(block, buffer);
596         if (!spiflash_wait_ready(FL_WRITING))
597                 return -EINTR;
598
599         /* wait and mark our activity */
600         SET_SPI_ACTIVITY();
601        
602         /* read it back and check pattern */
603         flash = (unsigned char *)(spidata->readaddr + block);
604         printk(KERN_EMERG "%s(): checking @ 0x%.8x ...\n",__FUNCTION__,(__u32)flash);
605         for (i = 0; i < 8; i++)
606         {
607                 if (flash[i*4] != (unsigned char)(i*4))
608                 {
609                         printk(KERN_EMERG "unexpected value @ %d: 0x%02x !!\n", i*4, flash[i*4]);
610                         break;
611                 }
612         }
613
614         /* clean our activity */
615         CLEAR_SPI_ACTIVITY();
616         udelay(10);
617        
618         /* erase this block before return */
619         printk(KERN_EMERG "%s(): erasing block 0x%.8x ...\n",__FUNCTION__,(__u32)block);
620
621         /* we are going to erase sector, do write enable first */
622         spiflash_sendcmd(SPI_WRITE_ENABLE, 0);
623
624         /* wait and mark our activity */
625         SET_SPI_ACTIVITY();
626
627         /* we are not really waiting for CPU spiflash activity, just need the value of the register. */
628         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
629
630         /* send sector erase op. */
631         ptr_opcode = &stm_opcodes[SPI_SECTOR_ERASE];
632         opcode = ((__u32)ptr_opcode->code) | ((__u32)block << 8);
633         spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
634         reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | ptr_opcode->tx_cnt | SPI_CTL_START;
635         spiflash_regwrite32(SPI_FLASH_CTL, reg);
636
637         /* wait for CPU spiflash activity */
638         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
639         /* clean our activity */
640         CLEAR_SPI_ACTIVITY();
641         udelay(10);
642
643         busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 20);
644         spiflash_done();
645         printk("SPI flash write test done (%d)!, page programming is %s!\n", i, i<8 ? "disabled":"enabled");
646         return (i<8 ? 1:0);
647 }
648
649 static int pp_mode = -1;
650 static int pp_enable = 1;
651
652 /* implementation for spiflash page programing. */
653 static int spiflash_page_write(struct mtd_info * mtd,
654                 loff_t to, size_t len, size_t * retlen, const u_char * buf)
655 {
656         size_t bytes_left = len;
657         size_t xact_len;
658         size_t written;
659         size_t offset;
660
661
662         /* If we already test page programming and failed,
663          * fall back to spiflash_write() directly. */
664         if (pp_mode > 0) return spiflash_write(mtd, to, len, retlen, buf);
665
666         *retlen = 0;
667         if (to + len > mtd->size) return (-EINVAL);
668
669         while (bytes_left > 0)
670         {
671                 offset = to % STM_PAGE_SIZE;
672                 xact_len = MIN(bytes_left, STM_PAGE_SIZE - offset);
673                 if (offset > 0 || xact_len < STM_PAGE_SIZE)
674                 {
675                         spiflash_write(mtd, to, xact_len, &written, buf);
676                 }
677                 else
678                 {
679                         /* test page program mode, if we did not test it before. */
680                         if (pp_mode < 0) pp_mode = test_page_programming(mtd, to);
681
682                         if (pp_enable && (pp_mode == 0)) page_write(to, buf);
683                         else spiflash_write(mtd, to, xact_len, &written, buf);
684                 }
685                 to += xact_len;
686                 bytes_left -= xact_len;
687                 buf += xact_len;
688                 *retlen += xact_len;
689         }
690
691         return 0;
692 }
693
694 static int __my_atoi(const char * buf)
695 {
696         int ret = 0;
697         while (*buf)
698         {
699                 if (*buf >= '0' && *buf <= '9') ret += (int)(*buf - '0');
700                 buf++;
701         }
702         return ret;
703 }
704
705 static int proc_read_pp_enable(char * buf, char ** start, off_t offset,
706                 int len, int * eof, void * data)
707 {
708         char * p = buf;
709         p += sprintf(p, "%d\n", pp_enable);
710         *eof = 1;
711         return p - buf;
712 }
713
714 static int proc_write_pp_enable(struct file * file, const char * buf,
715                 unsigned long count, void * data)
716 {
717         pp_enable = __my_atoi(buf);
718         printk("spiflash: %s page programming!\n", pp_enable ? "enable" : "disable");
719         if (pp_mode >= 0) printk("spiflash: H/W is %scapable of doing page programming!\n", pp_mode ? "not " : "");
720         return count;
721 }
722
723 static struct proc_dir_entry * root = NULL;
724 static struct proc_dir_entry * pp_enable_entry = NULL;
725
726 static int register_spi_proc(void)
727 {
728         root = proc_mkdir("spiflash", NULL);
729         if (root == NULL)
730         {
731                 printk("spiflash: fail to create /proc/spiflash !!\n");
732                 return -1;
733         }
734         pp_enable_entry = create_proc_entry("pp_enable", 0644, root);
735         if (pp_enable_entry == NULL)
736         {
737                 printk("spiflash: fail to create /proc/spiflash/pp_enable !!\n");
738                 remove_proc_entry("spiflash", root);
739                 root = NULL;
740                 return -1;
741         }
742         pp_enable_entry->data = 0;
743         pp_enable_entry->read_proc = proc_read_pp_enable;
744         pp_enable_entry->write_proc = proc_write_pp_enable;
745         pp_enable_entry->owner = THIS_MODULE;
746         printk("spiflash: /proc/spiflash/pp_enable created !!\n");
747         return 0;
748 }
749
750 static void remove_spi_proc(void)
751 {
752         if (pp_enable_entry) remove_proc_entry("pp_enable", root);
753         if (root) remove_proc_entry("spiflash", root);
754         pp_enable_entry = NULL;
755         root = NULL;
756 }
757
758 #endif
759
760 #ifdef CONFIG_MTD_PARTITIONS
761 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
762 #endif
763
764
765 static int spiflash_probe(struct platform_device *pdev)
766 {
767         int result = -1;
768         int index, num_parts;
769         struct mtd_info *mtd;
770
771         spidata->mmraddr = ioremap_nocache(SPI_FLASH_MMR, SPI_FLASH_MMR_SIZE);
772         spin_lock_init(&spidata->mutex);
773         init_waitqueue_head(&spidata->wq);
774         spidata->state = FL_READY;
775        
776         if (!spidata->mmraddr) {
777                 printk (KERN_WARNING SPIFLASH "Failed to map flash device\n");
778                 kfree(spidata);
779                 spidata = NULL;
780         }
781
782         mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
783         if (!mtd) {
784                 kfree(spidata);
785                 return -ENXIO;
786         }
787        
788         if (!(index = spiflash_probe_chip())) {
789         printk (KERN_WARNING SPIFLASH "Found no serial flash device\n");
790                 goto error;
791         }
792
793         spidata->readaddr = ioremap_nocache(SPI_FLASH_READ, flashconfig_tbl[index].byte_cnt);
794         if (!spidata->readaddr) {
795                 printk (KERN_WARNING SPIFLASH "Failed to map flash device\n");
796                 goto error;
797         }
798
799         mtd->name = "spiflash";
800         mtd->type = MTD_NORFLASH;
801         mtd->flags = (MTD_CAP_NORFLASH|MTD_WRITEABLE);
802         mtd->size = flashconfig_tbl[index].byte_cnt;
803         mtd->erasesize = flashconfig_tbl[index].sector_size;
804         mtd->writesize = 1;
805         mtd->numeraseregions = 0;
806         mtd->eraseregions = NULL;
807         mtd->erase = spiflash_erase;
808         mtd->read = spiflash_read;
809 #ifdef CONFIG_MTD_SPIFLASH_PP
810         mtd->write = spiflash_page_write;
811 #else
812         mtd->write = spiflash_write;
813 #endif
814         mtd->owner = THIS_MODULE;
815
816         /* parse redboot partitions */
817         num_parts = parse_mtd_partitions(mtd, part_probe_types, &spidata->parsed_parts, 0);
818         if (!num_parts)
819                 goto error;
820
821         result = add_mtd_partitions(mtd, spidata->parsed_parts, num_parts);
822
823 #ifdef CONFIG_MTD_SPIFLASH_PP
824                 register_spi_proc();
825 #endif
826
827
828         spidata->mtd = mtd;
829        
830         return (result);
831        
832 error:
833         kfree(mtd);
834         kfree(spidata);
835         return -ENXIO;
836 }
837
838 static int spiflash_remove (struct platform_device *pdev)
839 {
840         del_mtd_partitions (spidata->mtd);
841         kfree(spidata->mtd);
842         return 0;
843 }
844
845 struct platform_driver spiflash_driver = {
846         .driver.name = "spiflash",
847         .probe = spiflash_probe,
848         .remove = spiflash_remove,
849 };
850
851 int __init
852 spiflash_init (void)
853 {
854         spidata = kmalloc(sizeof(struct spiflash_data), GFP_KERNEL);
855         if (!spidata)
856                 return (-ENXIO);
857
858         spin_lock_init(&spidata->mutex);
859         platform_driver_register(&spiflash_driver);
860
861         return 0;
862 }
863
864 void __exit
865 spiflash_exit (void)
866 {
867         kfree(spidata);
868 #ifdef CONFIG_MTD_SPIFLASH_PP
869         remove_spi_proc();
870 #endif
871 }
872
873 module_init (spiflash_init);
874 module_exit (spiflash_exit);
875
876 MODULE_LICENSE("GPL");
877 MODULE_AUTHOR("OpenWrt.org, Atheros Communications Inc");
878 MODULE_DESCRIPTION("MTD driver for SPI Flash on Atheros SOC");
879
Note: See TracBrowser for help on using the browser.