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

Revision 9243, 22.5 kB (checked in by BrainSlayer, 2 years ago)

fix erase function for supporting ranges bigger than eraseblock

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         busy_wait(spiflash_regread32(SPI_FLASH_CTL) & SPI_CTL_BUSY, 0);
259  
260         if (!ptr_opcode->rx_cnt)
261                 return 0;
262
263         reg = (__u32) spiflash_regread32(SPI_FLASH_DATA);
264
265         switch (ptr_opcode->rx_cnt) {
266         case 1:
267                         mask = 0x000000ff;
268                         break;
269         case 2:
270                         mask = 0x0000ffff;
271                         break;
272         case 3:
273                         mask = 0x00ffffff;
274                         break;
275         default:
276                         mask = 0xffffffff;
277                         break;
278         }
279         reg &= mask;
280
281         return reg;
282 }
283
284
285
286 /* Probe SPI flash device
287  * Function returns 0 for failure.
288  * and flashconfig_tbl array index for success.
289  */
290 static int
291 spiflash_probe_chip (void)
292 {
293         __u32 sig;
294         int flash_size;
295        
296         /* Read the signature on the flash device */
297         spin_lock_bh(&spidata->mutex);
298         sig = spiflash_sendcmd(SPI_RD_SIG, 0);
299         spin_unlock_bh(&spidata->mutex);
300
301         switch (sig) {
302         case STM_8MBIT_SIGNATURE:
303                 flash_size = FLASH_1MB;
304                 break;
305         case STM_16MBIT_SIGNATURE:
306                 flash_size = FLASH_2MB;
307                 break;
308         case STM_32MBIT_SIGNATURE:
309                 flash_size = FLASH_4MB;
310                 break;
311         case STM_64MBIT_SIGNATURE:
312                 flash_size = FLASH_8MB;
313                 break;
314         case STM_128MBIT_SIGNATURE:
315                 flash_size = FLASH_16MB;
316                 break;
317         default:
318                 printk (KERN_WARNING SPIFLASH "Read of flash device signature failed!\n");
319                 return (0);
320         }
321
322         return (flash_size);
323 }
324
325
326 /* wait until the flash chip is ready and grab a lock */
327 static int spiflash_wait_ready(int state)
328 {
329         DECLARE_WAITQUEUE(wait, current);
330
331 retry:
332         spin_lock_bh(&spidata->mutex);
333         if (spidata->state != FL_READY) {
334                 set_current_state(TASK_UNINTERRUPTIBLE);
335                 add_wait_queue(&spidata->wq, &wait);
336                 spin_unlock_bh(&spidata->mutex);
337                 schedule();
338                 remove_wait_queue(&spidata->wq, &wait);
339                
340                 if(signal_pending(current))
341                         return 0;
342
343        
344                 goto retry;
345         }
346         spidata->state = state;
347
348         return 1;
349 }
350
351 static inline void spiflash_done(void)
352 {
353         spidata->state = FL_READY;
354         spin_unlock_bh(&spidata->mutex);
355         wake_up(&spidata->wq);
356 }
357
358 static int
359 spiflash_erase (struct mtd_info *mtd,struct erase_info *instr)
360 {
361         struct opcodes *ptr_opcode;
362         __u32 temp, reg;
363         int finished = 0;
364         unsigned int addr = instr->addr;
365
366 #ifdef SPIFLASH_DEBUG
367         printk (KERN_DEBUG "%s(addr = 0x%.8x, len = %d)\n",__FUNCTION__,instr->addr,instr->len);
368 #endif
369
370         /* sanity checks */
371         if (instr->addr + instr->len > mtd->size) return (-EINVAL);
372         if (!spiflash_wait_ready(FL_ERASING))
373                 return -EINTR;
374 for (addr=instr->addr;addr<instr->addr+instr->len;addr+=mtd->erasesize)
375 {
376
377         ptr_opcode = &stm_opcodes[SPI_SECTOR_ERASE];
378
379         temp = ((__u32)addr << 8) | (__u32)(ptr_opcode->code);
380         spiflash_sendcmd(SPI_WRITE_ENABLE,0);
381         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
382
383         spiflash_regwrite32(SPI_FLASH_OPCODE, temp);
384
385         reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | ptr_opcode->tx_cnt | SPI_CTL_START;
386         spiflash_regwrite32(SPI_FLASH_CTL, reg);
387
388         busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 20);
389 }
390         spiflash_done();
391
392         instr->state = MTD_ERASE_DONE;
393         if (instr->callback) instr->callback (instr);
394 #ifdef SPIFLASH_DEBUG
395         printk (KERN_DEBUG "%s return\n",__FUNCTION__);
396 #endif
397         return (0);
398 }
399
400 static int
401 spiflash_read (struct mtd_info *mtd, loff_t from,size_t len,size_t *retlen,u_char *buf)
402 {
403         u8 *read_addr;
404        
405         /* sanity checks */
406         if (!len) return (0);
407         if (from + len > mtd->size) return (-EINVAL);
408        
409         /* we always read len bytes */
410         *retlen = len;
411
412         if (!spiflash_wait_ready(FL_READING))
413                 return -EINTR;
414         read_addr = (u8 *)(spidata->readaddr + from);
415         memcpy(buf, read_addr, len);
416         spiflash_done();
417
418         return 0;
419 }
420
421 static int
422 spiflash_write (struct mtd_info *mtd,loff_t to,size_t len,size_t *retlen,const u_char *buf)
423 {
424         u32 opcode, bytes_left;
425
426         *retlen = 0;
427
428         /* sanity checks */
429         if (!len) return (0);
430         if (to + len > mtd->size) return (-EINVAL);
431        
432         opcode = stm_opcodes[SPI_PAGE_PROGRAM].code;
433         bytes_left = len;
434        
435         do {
436                 u32 xact_len, reg, page_offset, spi_data = 0;
437
438                 xact_len = MIN(bytes_left, sizeof(__u32));
439
440                 /* 32-bit writes cannot span across a page boundary
441                  * (256 bytes). This types of writes require two page
442                  * program operations to handle it correctly. The STM part
443                  * will write the overflow data to the beginning of the
444                  * current page as opposed to the subsequent page.
445                  */
446                 page_offset = (to & (STM_PAGE_SIZE - 1)) + xact_len;
447
448                 if (page_offset > STM_PAGE_SIZE) {
449                         xact_len -= (page_offset - STM_PAGE_SIZE);
450                 }
451
452                 if (!spiflash_wait_ready(FL_WRITING))
453                         return -EINTR;
454
455                 spiflash_sendcmd(SPI_WRITE_ENABLE, 0);
456                 switch (xact_len) {
457                         case 1:
458                                 spi_data = (u32) ((u8) *buf);
459                                 break;
460                         case 2:
461                                 spi_data = (buf[1] << 8) | buf[0];
462                                 break;
463                         case 3:
464                                 spi_data = (buf[2] << 16) | (buf[1] << 8) | buf[0];
465                                 break;
466                         case 4:
467                                 spi_data = (buf[3] << 24) | (buf[2] << 16) |
468                                                         (buf[1] << 8) | buf[0];
469                                 break;
470                         default:
471                                 spi_data = 0;
472                                 break;
473                 }
474
475                 spiflash_regwrite32(SPI_FLASH_DATA, spi_data);
476                 opcode = (opcode & SPI_OPCODE_MASK) | ((__u32)to << 8);
477                 spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
478
479                 reg = spiflash_regread32(SPI_FLASH_CTL);
480                 reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | (xact_len + 4) | SPI_CTL_START;
481                 spiflash_regwrite32(SPI_FLASH_CTL, reg);
482
483                 /* give the chip some time before we start busy waiting */
484                 spin_unlock_bh(&spidata->mutex);
485                 schedule();
486                 spin_lock_bh(&spidata->mutex);
487
488                 busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 0);
489                 spiflash_done();
490
491                 bytes_left -= xact_len;
492                 to += xact_len;
493                 buf += xact_len;
494
495                 *retlen += xact_len;
496         } while (bytes_left != 0);
497
498         return 0;
499 }
500
501 #ifdef CONFIG_MTD_SPIFLASH_PP
502
503 static void page_write(loff_t to, const u_char * buf)
504 {
505         __u32   reg, spi_data, opcode;
506         int             i;
507
508
509         /* We are going to write flash now, do write enable first. */
510         spiflash_sendcmd(SPI_WRITE_ENABLE, 0);
511
512         /* we are not really waiting for CPU spiflash activity, just need the value of the register. */
513         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
514
515         /* Prepare SPI opcode, data and control register values. */
516         opcode   = (stm_opcodes[SPI_PAGE_PROGRAM].code & SPI_OPCODE_MASK) | ((__u32)to << 8);
517         spi_data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; buf += 4;
518         reg      = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | 0x8 | SPI_CTL_START;
519
520         /* wait and mark our activity */
521         if (!spiflash_wait_ready(FL_WRITING))
522                 return -EINTR;
523         SET_SPI_ACTIVITY();
524         chip_select(0);
525
526         /* Send out the the first 4 bytes. */
527         spiflash_regwrite32(SPI_FLASH_DATA, spi_data);
528         spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
529         spiflash_regwrite32(SPI_FLASH_CTL, reg);
530
531         /* 31 loops, each loop send 8 bytes */
532         for (i=0; i<31; i++)
533         {
534                 busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
535
536                 /*
537                  * The sample code from the application node is:
538                  *
539                  *      spi_data = (UINT32)*((UINT32 *)buf);
540                  *      spi_data = cpi2le32(spi_data);
541                  *      spi_data_swapped =
542                  *                      (((spi_data>>8) & 0xff) << 24) |
543                  *                      (((spi_data>>24)& 0xff) << 8) |
544                  *                      (spi_data & 0x00ff00ff);
545                  */
546                 opcode   = (buf[3] <<  8) | (buf[2] << 16) | (buf[1] << 24) | buf[0]; buf += 4;
547                 spi_data = (buf[3] << 24) | (buf[2] << 16) | (buf[1] <<  8) | buf[0]; buf += 4;
548                 reg      = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | 0x8 | SPI_CTL_START;
549
550                 spiflash_regwrite32(SPI_FLASH_DATA, spi_data);
551                 spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
552                 spiflash_regwrite32(SPI_FLASH_CTL, reg);
553         }
554
555         /* send out the last 4 bytes */
556         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
557
558         opcode   = (buf[3] <<  8) | (buf[2] << 16) | (buf[1] << 24) | buf[0]; buf += 4;
559         reg      = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | 0x4 | SPI_CTL_START;
560
561         spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
562         spiflash_regwrite32(SPI_FLASH_CTL, reg);
563
564         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
565
566         /* Deactive chip select */
567         chip_select(1);
568         /* clean our activity */
569         CLEAR_SPI_ACTIVITY();
570        
571
572         busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 20);
573         spiflash_done();
574         return;
575 }
576
577 /*
578  * Do page programming test.
579  * The 'block' should be erase already.
580  * We try to use page programming mode to write flash,
581  * and erase this block again before return.
582  */
583 static int test_page_programming(struct mtd_info * mtd, loff_t block)
584 {
585         unsigned char   buffer[256];
586         unsigned char * flash;
587         struct opcodes *ptr_opcode;
588         __u32                   opcode, reg;
589         int                             i;
590
591
592         /* write the flash with known pattern */
593         for (i=0; i<256; i++) buffer[i] = (unsigned char)i;
594         page_write(block, buffer);
595         if (!spiflash_wait_ready(FL_WRITING))
596                 return -EINTR;
597
598         /* wait and mark our activity */
599         SET_SPI_ACTIVITY();
600        
601         /* read it back and check pattern */
602         flash = (unsigned char *)(spidata->readaddr + block);
603         printk(KERN_EMERG "%s(): checking @ 0x%.8x ...\n",__FUNCTION__,(__u32)flash);
604         for (i = 0; i < 8; i++)
605         {
606                 if (flash[i*4] != (unsigned char)(i*4))
607                 {
608                         printk(KERN_EMERG "unexpected value @ %d: 0x%02x !!\n", i*4, flash[i*4]);
609                         break;
610                 }
611         }
612
613         /* clean our activity */
614         CLEAR_SPI_ACTIVITY();
615         udelay(10);
616        
617         /* erase this block before return */
618         printk(KERN_EMERG "%s(): erasing block 0x%.8x ...\n",__FUNCTION__,(__u32)block);
619
620         /* we are going to erase sector, do write enable first */
621         spiflash_sendcmd(SPI_WRITE_ENABLE, 0);
622
623         /* wait and mark our activity */
624         SET_SPI_ACTIVITY();
625
626         /* we are not really waiting for CPU spiflash activity, just need the value of the register. */
627         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
628
629         /* send sector erase op. */
630         ptr_opcode = &stm_opcodes[SPI_SECTOR_ERASE];
631         opcode = ((__u32)ptr_opcode->code) | ((__u32)block << 8);
632         spiflash_regwrite32(SPI_FLASH_OPCODE, opcode);
633         reg = (reg & ~SPI_CTL_TX_RX_CNT_MASK) | ptr_opcode->tx_cnt | SPI_CTL_START;
634         spiflash_regwrite32(SPI_FLASH_CTL, reg);
635
636         /* wait for CPU spiflash activity */
637         busy_wait((reg = spiflash_regread32(SPI_FLASH_CTL)) & SPI_CTL_BUSY, 0);
638         /* clean our activity */
639         CLEAR_SPI_ACTIVITY();
640         udelay(10);
641
642         busy_wait(spiflash_sendcmd(SPI_RD_STATUS, 0) & SPI_STATUS_WIP, 20);
643         spiflash_done();
644         printk("SPI flash write test done (%d)!, page programming is %s!\n", i, i<8 ? "disabled":"enabled");
645         return (i<8 ? 1:0);
646 }
647
648 static int pp_mode = -1;
649 static int pp_enable = 1;
650
651 /* implementation for spiflash page programing. */
652 static int spiflash_page_write(struct mtd_info * mtd,
653                 loff_t to, size_t len, size_t * retlen, const u_char * buf)
654 {
655         size_t bytes_left = len;
656         size_t xact_len;
657         size_t written;
658         size_t offset;
659
660
661         /* If we already test page programming and failed,
662          * fall back to spiflash_write() directly. */
663         if (pp_mode > 0) return spiflash_write(mtd, to, len, retlen, buf);
664
665         *retlen = 0;
666         if (to + len > mtd->size) return (-EINVAL);
667
668         while (bytes_left > 0)
669         {
670                 offset = to % STM_PAGE_SIZE;
671                 xact_len = MIN(bytes_left, STM_PAGE_SIZE - offset);
672                 if (offset > 0 || xact_len < STM_PAGE_SIZE)
673                 {
674                         spiflash_write(mtd, to, xact_len, &written, buf);
675                 }
676                 else
677                 {
678                         /* test page program mode, if we did not test it before. */
679                         if (pp_mode < 0) pp_mode = test_page_programming(mtd, to);
680
681                         if (pp_enable && (pp_mode == 0)) page_write(to, buf);
682                         else spiflash_write(mtd, to, xact_len, &written, buf);
683                 }
684                 to += xact_len;
685                 bytes_left -= xact_len;
686                 buf += xact_len;
687                 *retlen += xact_len;
688         }
689
690         return 0;
691 }
692
693 static int __my_atoi(const char * buf)
694 {
695         int ret = 0;
696         while (*buf)
697         {
698                 if (*buf >= '0' && *buf <= '9') ret += (int)(*buf - '0');
699                 buf++;
700         }
701         return ret;
702 }
703
704 static int proc_read_pp_enable(char * buf, char ** start, off_t offset,
705                 int len, int * eof, void * data)
706 {
707         char * p = buf;
708         p += sprintf(p, "%d\n", pp_enable);
709         *eof = 1;
710         return p - buf;
711 }
712
713 static int proc_write_pp_enable(struct file * file, const char * buf,
714                 unsigned long count, void * data)
715 {
716         pp_enable = __my_atoi(buf);
717         printk("spiflash: %s page programming!\n", pp_enable ? "enable" : "disable");
718         if (pp_mode >= 0) printk("spiflash: H/W is %scapable of doing page programming!\n", pp_mode ? "not " : "");
719         return count;
720 }
721
722 static struct proc_dir_entry * root = NULL;
723 static struct proc_dir_entry * pp_enable_entry = NULL;
724
725 static int register_spi_proc(void)
726 {
727         root = proc_mkdir("spiflash", NULL);
728         if (root == NULL)
729         {
730                 printk("spiflash: fail to create /proc/spiflash !!\n");
731                 return -1;
732         }
733         pp_enable_entry = create_proc_entry("pp_enable", 0644, root);
734         if (pp_enable_entry == NULL)
735         {
736                 printk("spiflash: fail to create /proc/spiflash/pp_enable !!\n");
737                 remove_proc_entry("spiflash", root);
738                 root = NULL;
739                 return -1;
740         }
741         pp_enable_entry->data = 0;
742         pp_enable_entry->read_proc = proc_read_pp_enable;
743         pp_enable_entry->write_proc = proc_write_pp_enable;
744         pp_enable_entry->owner = THIS_MODULE;
745         printk("spiflash: /proc/spiflash/pp_enable created !!\n");
746         return 0;
747 }
748
749 static void remove_spi_proc(void)
750 {
751         if (pp_enable_entry) remove_proc_entry("pp_enable", root);
752         if (root) remove_proc_entry("spiflash", root);
753         pp_enable_entry = NULL;
754         root = NULL;
755 }
756
757 #endif
758
759 #ifdef CONFIG_MTD_PARTITIONS
760 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
761 #endif
762
763
764 static int spiflash_probe(struct platform_device *pdev)
765 {
766         int result = -1;
767         int index, num_parts;
768         struct mtd_info *mtd;
769
770         spidata->mmraddr = ioremap_nocache(SPI_FLASH_MMR, SPI_FLASH_MMR_SIZE);
771         spin_lock_init(&spidata->mutex);
772         init_waitqueue_head(&spidata->wq);
773         spidata->state = FL_READY;
774        
775         if (!spidata->mmraddr) {
776                 printk (KERN_WARNING SPIFLASH "Failed to map flash device\n");
777                 kfree(spidata);
778                 spidata = NULL;
779         }
780
781         mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
782         if (!mtd) {
783                 kfree(spidata);
784                 return -ENXIO;
785         }
786        
787         if (!(index = spiflash_probe_chip())) {
788         printk (KERN_WARNING SPIFLASH "Found no serial flash device\n");
789                 goto error;
790         }
791
792         spidata->readaddr = ioremap_nocache(SPI_FLASH_READ, flashconfig_tbl[index].byte_cnt);
793         if (!spidata->readaddr) {
794                 printk (KERN_WARNING SPIFLASH "Failed to map flash device\n");
795                 goto error;
796         }
797
798         mtd->name = "spiflash";
799         mtd->type = MTD_NORFLASH;
800         mtd->flags = (MTD_CAP_NORFLASH|MTD_WRITEABLE);
801         mtd->size = flashconfig_tbl[index].byte_cnt;
802         mtd->erasesize = flashconfig_tbl[index].sector_size;
803         mtd->writesize = 1;
804         mtd->numeraseregions = 0;
805         mtd->eraseregions = NULL;
806         mtd->erase = spiflash_erase;
807         mtd->read = spiflash_read;
808 #ifdef CONFIG_MTD_SPIFLASH_PP
809         mtd->write = spiflash_page_write;
810 #else
811         mtd->write = spiflash_write;
812 #endif
813         mtd->owner = THIS_MODULE;
814
815         /* parse redboot partitions */
816         num_parts = parse_mtd_partitions(mtd, part_probe_types, &spidata->parsed_parts, 0);
817         if (!num_parts)
818                 goto error;
819
820         result = add_mtd_partitions(mtd, spidata->parsed_parts, num_parts);
821
822 #ifdef CONFIG_MTD_SPIFLASH_PP
823                 register_spi_proc();
824 #endif
825
826
827         spidata->mtd = mtd;
828        
829         return (result);
830        
831 error:
832         kfree(mtd);
833         kfree(spidata);
834         return -ENXIO;
835 }
836
837 static int spiflash_remove (struct platform_device *pdev)
838 {
839         del_mtd_partitions (spidata->mtd);
840         kfree(spidata->mtd);
841         return 0;
842 }
843
844 struct platform_driver spiflash_driver = {
845         .driver.name = "spiflash",
846         .probe = spiflash_probe,
847         .remove = spiflash_remove,
848 };
849
850 int __init
851 spiflash_init (void)
852 {
853         spidata = kmalloc(sizeof(struct spiflash_data), GFP_KERNEL);
854         if (!spidata)
855                 return (-ENXIO);
856
857         spin_lock_init(&spidata->mutex);
858         platform_driver_register(&spiflash_driver);
859
860         return 0;
861 }
862
863 void __exit
864 spiflash_exit (void)
865 {
866         kfree(spidata);
867 #ifdef CONFIG_MTD_SPIFLASH_PP
868         remove_spi_proc();
869 #endif
870 }
871
872 module_init (spiflash_init);
873 module_exit (spiflash_exit);
874
875 MODULE_LICENSE("GPL");
876 MODULE_AUTHOR("OpenWrt.org, Atheros Communications Inc");
877 MODULE_DESCRIPTION("MTD driver for SPI Flash on Atheros SOC");
878
Note: See TracBrowser for help on using the browser.