2 * BCM47XX NAND flash driver
4 * Copyright (C) 2012 Rafał Miłecki <zajec5@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include "bcm47xxnflash.h"
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/bcma/bcma.h>
20 /* Broadcom uses 1'000'000 but it seems to be too many. Tests on WNDR4500 has
21 * shown ~1000 retries as maxiumum. */
22 #define NFLASH_READY_RETRIES 10000
24 #define NFLASH_SECTOR_SIZE 512
26 #define NCTL_CMD0 0x00010000
27 #define NCTL_COL 0x00020000 /* Update column with value from BCMA_CC_NFLASH_COL_ADDR */
28 #define NCTL_ROW 0x00040000 /* Update row (page) with value from BCMA_CC_NFLASH_ROW_ADDR */
29 #define NCTL_CMD1W 0x00080000
30 #define NCTL_READ 0x00100000
31 #define NCTL_WRITE 0x00200000
32 #define NCTL_SPECADDR 0x01000000
33 #define NCTL_READY 0x04000000
34 #define NCTL_ERR 0x08000000
35 #define NCTL_CSA 0x40000000
36 #define NCTL_START 0x80000000
38 /**************************************************
40 **************************************************/
42 static inline u8
bcm47xxnflash_ops_bcm4706_ns_to_cycle(u16 ns
, u16 clock
)
44 return ((ns
* 1000 * clock
) / 1000000) + 1;
47 static int bcm47xxnflash_ops_bcm4706_ctl_cmd(struct bcma_drv_cc
*cc
, u32 code
)
51 bcma_cc_write32(cc
, BCMA_CC_NFLASH_CTL
, NCTL_START
| code
);
52 for (i
= 0; i
< NFLASH_READY_RETRIES
; i
++) {
53 if (!(bcma_cc_read32(cc
, BCMA_CC_NFLASH_CTL
) & NCTL_START
)) {
59 pr_err("NFLASH control command not ready!\n");
65 static int bcm47xxnflash_ops_bcm4706_poll(struct bcma_drv_cc
*cc
)
69 for (i
= 0; i
< NFLASH_READY_RETRIES
; i
++) {
70 if (bcma_cc_read32(cc
, BCMA_CC_NFLASH_CTL
) & NCTL_READY
) {
71 if (bcma_cc_read32(cc
, BCMA_CC_NFLASH_CTL
) &
72 BCMA_CC_NFLASH_CTL_ERR
) {
73 pr_err("Error on polling\n");
81 pr_err("Polling timeout!\n");
85 /**************************************************
87 **************************************************/
89 static void bcm47xxnflash_ops_bcm4706_read(struct mtd_info
*mtd
, uint8_t *buf
,
92 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
93 struct bcm47xxnflash
*b47n
= nand_get_controller_data(nand_chip
);
96 u32
*dest
= (u32
*)buf
;
100 BUG_ON(b47n
->curr_page_addr
& ~nand_chip
->pagemask
);
101 /* Don't validate column using nand_chip->page_shift, it may be bigger
102 * when accessing OOB */
105 /* We can read maximum of 0x200 bytes at once */
106 toread
= min(len
, 0x200);
108 /* Set page and column */
109 bcma_cc_write32(b47n
->cc
, BCMA_CC_NFLASH_COL_ADDR
,
111 bcma_cc_write32(b47n
->cc
, BCMA_CC_NFLASH_ROW_ADDR
,
112 b47n
->curr_page_addr
);
114 /* Prepare to read */
115 ctlcode
= NCTL_CSA
| NCTL_CMD1W
| NCTL_ROW
| NCTL_COL
|
117 ctlcode
|= NAND_CMD_READSTART
<< 8;
118 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n
->cc
, ctlcode
))
120 if (bcm47xxnflash_ops_bcm4706_poll(b47n
->cc
))
123 /* Eventually read some data :) */
124 for (i
= 0; i
< toread
; i
+= 4, dest
++) {
125 ctlcode
= NCTL_CSA
| 0x30000000 | NCTL_READ
;
126 if (i
== toread
- 4) /* Last read goes without that */
127 ctlcode
&= ~NCTL_CSA
;
128 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n
->cc
,
131 *dest
= bcma_cc_read32(b47n
->cc
, BCMA_CC_NFLASH_DATA
);
134 b47n
->curr_column
+= toread
;
139 static void bcm47xxnflash_ops_bcm4706_write(struct mtd_info
*mtd
,
140 const uint8_t *buf
, int len
)
142 struct nand_chip
*nand_chip
= mtd_to_nand(mtd
);
143 struct bcm47xxnflash
*b47n
= nand_get_controller_data(nand_chip
);
144 struct bcma_drv_cc
*cc
= b47n
->cc
;
147 const u32
*data
= (u32
*)buf
;
150 BUG_ON(b47n
->curr_page_addr
& ~nand_chip
->pagemask
);
151 /* Don't validate column using nand_chip->page_shift, it may be bigger
152 * when accessing OOB */
154 for (i
= 0; i
< len
; i
+= 4, data
++) {
155 bcma_cc_write32(cc
, BCMA_CC_NFLASH_DATA
, *data
);
157 ctlcode
= NCTL_CSA
| 0x30000000 | NCTL_WRITE
;
158 if (i
== len
- 4) /* Last read goes without that */
159 ctlcode
&= ~NCTL_CSA
;
160 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(cc
, ctlcode
)) {
161 pr_err("%s ctl_cmd didn't work!\n", __func__
);
166 b47n
->curr_column
+= len
;
169 /**************************************************
171 **************************************************/
173 static void bcm47xxnflash_ops_bcm4706_cmd_ctrl(struct nand_chip
*nand_chip
,
174 int cmd
, unsigned int ctrl
)
176 struct bcm47xxnflash
*b47n
= nand_get_controller_data(nand_chip
);
179 if (cmd
== NAND_CMD_NONE
)
182 if (cmd
& NAND_CTRL_CLE
)
183 code
= cmd
| NCTL_CMD0
;
185 /* nCS is not needed for reset command */
186 if (cmd
!= NAND_CMD_RESET
)
189 bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n
->cc
, code
);
192 /* Default nand_select_chip calls cmd_ctrl, which is not used in BCM4706 */
193 static void bcm47xxnflash_ops_bcm4706_select_chip(struct nand_chip
*chip
,
199 static int bcm47xxnflash_ops_bcm4706_dev_ready(struct nand_chip
*nand_chip
)
201 struct bcm47xxnflash
*b47n
= nand_get_controller_data(nand_chip
);
203 return !!(bcma_cc_read32(b47n
->cc
, BCMA_CC_NFLASH_CTL
) & NCTL_READY
);
207 * Default nand_command and nand_command_lp don't match BCM4706 hardware layout.
208 * For example, reading chip id is performed in a non-standard way.
209 * Setting column and page is also handled differently, we use a special
210 * registers of ChipCommon core. Hacking cmd_ctrl to understand and convert
211 * standard commands would be much more complicated.
213 static void bcm47xxnflash_ops_bcm4706_cmdfunc(struct nand_chip
*nand_chip
,
214 unsigned command
, int column
,
217 struct mtd_info
*mtd
= nand_to_mtd(nand_chip
);
218 struct bcm47xxnflash
*b47n
= nand_get_controller_data(nand_chip
);
219 struct bcma_drv_cc
*cc
= b47n
->cc
;
224 b47n
->curr_column
= column
;
226 b47n
->curr_page_addr
= page_addr
;
230 nand_chip
->legacy
.cmd_ctrl(nand_chip
, command
, NAND_CTRL_CLE
);
233 nand_wait_ready(nand_chip
);
235 case NAND_CMD_READID
:
236 ctlcode
= NCTL_CSA
| 0x01000000 | NCTL_CMD1W
| NCTL_CMD0
;
237 ctlcode
|= NAND_CMD_READID
;
238 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n
->cc
, ctlcode
)) {
239 pr_err("READID error\n");
244 * Reading is specific, last one has to go without NCTL_CSA
245 * bit. We don't know how many reads NAND subsystem is going
246 * to perform, so cache everything.
248 for (i
= 0; i
< ARRAY_SIZE(b47n
->id_data
); i
++) {
249 ctlcode
= NCTL_CSA
| NCTL_READ
;
250 if (i
== ARRAY_SIZE(b47n
->id_data
) - 1)
251 ctlcode
&= ~NCTL_CSA
;
252 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(b47n
->cc
,
254 pr_err("READID error\n");
258 bcma_cc_read32(b47n
->cc
, BCMA_CC_NFLASH_DATA
)
263 case NAND_CMD_STATUS
:
264 ctlcode
= NCTL_CSA
| NCTL_CMD0
| NAND_CMD_STATUS
;
265 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(cc
, ctlcode
))
266 pr_err("STATUS command error\n");
270 case NAND_CMD_READOOB
:
272 b47n
->curr_column
+= mtd
->writesize
;
274 case NAND_CMD_ERASE1
:
275 bcma_cc_write32(cc
, BCMA_CC_NFLASH_ROW_ADDR
,
276 b47n
->curr_page_addr
);
277 ctlcode
= NCTL_ROW
| NCTL_CMD1W
| NCTL_CMD0
|
278 NAND_CMD_ERASE1
| (NAND_CMD_ERASE2
<< 8);
279 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(cc
, ctlcode
))
280 pr_err("ERASE1 failed\n");
282 case NAND_CMD_ERASE2
:
285 /* Set page and column */
286 bcma_cc_write32(cc
, BCMA_CC_NFLASH_COL_ADDR
,
288 bcma_cc_write32(cc
, BCMA_CC_NFLASH_ROW_ADDR
,
289 b47n
->curr_page_addr
);
291 /* Prepare to write */
292 ctlcode
= 0x40000000 | NCTL_ROW
| NCTL_COL
| NCTL_CMD0
;
293 ctlcode
|= NAND_CMD_SEQIN
;
294 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(cc
, ctlcode
))
295 pr_err("SEQIN failed\n");
297 case NAND_CMD_PAGEPROG
:
298 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(cc
, NCTL_CMD0
|
300 pr_err("PAGEPROG failed\n");
301 if (bcm47xxnflash_ops_bcm4706_poll(cc
))
302 pr_err("PAGEPROG not ready\n");
305 pr_err("Command 0x%X unsupported\n", command
);
308 b47n
->curr_command
= command
;
311 static u8
bcm47xxnflash_ops_bcm4706_read_byte(struct nand_chip
*nand_chip
)
313 struct mtd_info
*mtd
= nand_to_mtd(nand_chip
);
314 struct bcm47xxnflash
*b47n
= nand_get_controller_data(nand_chip
);
315 struct bcma_drv_cc
*cc
= b47n
->cc
;
318 switch (b47n
->curr_command
) {
319 case NAND_CMD_READID
:
320 if (b47n
->curr_column
>= ARRAY_SIZE(b47n
->id_data
)) {
321 pr_err("Requested invalid id_data: %d\n",
325 return b47n
->id_data
[b47n
->curr_column
++];
326 case NAND_CMD_STATUS
:
327 if (bcm47xxnflash_ops_bcm4706_ctl_cmd(cc
, NCTL_READ
))
329 return bcma_cc_read32(cc
, BCMA_CC_NFLASH_DATA
) & 0xff;
330 case NAND_CMD_READOOB
:
331 bcm47xxnflash_ops_bcm4706_read(mtd
, (u8
*)&tmp
, 4);
335 pr_err("Invalid command for byte read: 0x%X\n", b47n
->curr_command
);
339 static void bcm47xxnflash_ops_bcm4706_read_buf(struct nand_chip
*nand_chip
,
340 uint8_t *buf
, int len
)
342 struct bcm47xxnflash
*b47n
= nand_get_controller_data(nand_chip
);
344 switch (b47n
->curr_command
) {
346 case NAND_CMD_READOOB
:
347 bcm47xxnflash_ops_bcm4706_read(nand_to_mtd(nand_chip
), buf
,
352 pr_err("Invalid command for buf read: 0x%X\n", b47n
->curr_command
);
355 static void bcm47xxnflash_ops_bcm4706_write_buf(struct nand_chip
*nand_chip
,
356 const uint8_t *buf
, int len
)
358 struct bcm47xxnflash
*b47n
= nand_get_controller_data(nand_chip
);
360 switch (b47n
->curr_command
) {
362 bcm47xxnflash_ops_bcm4706_write(nand_to_mtd(nand_chip
), buf
,
367 pr_err("Invalid command for buf write: 0x%X\n", b47n
->curr_command
);
370 /**************************************************
372 **************************************************/
374 int bcm47xxnflash_ops_bcm4706_init(struct bcm47xxnflash
*b47n
)
376 struct nand_chip
*nand_chip
= (struct nand_chip
*)&b47n
->nand_chip
;
380 u8 w0
, w1
, w2
, w3
, w4
;
382 unsigned long chipsize
; /* MiB */
383 u8 tbits
, col_bits
, col_size
, row_bits
, row_bsize
;
386 nand_chip
->legacy
.select_chip
= bcm47xxnflash_ops_bcm4706_select_chip
;
387 nand_chip
->legacy
.cmd_ctrl
= bcm47xxnflash_ops_bcm4706_cmd_ctrl
;
388 nand_chip
->legacy
.dev_ready
= bcm47xxnflash_ops_bcm4706_dev_ready
;
389 b47n
->nand_chip
.legacy
.cmdfunc
= bcm47xxnflash_ops_bcm4706_cmdfunc
;
390 b47n
->nand_chip
.legacy
.read_byte
= bcm47xxnflash_ops_bcm4706_read_byte
;
391 b47n
->nand_chip
.legacy
.read_buf
= bcm47xxnflash_ops_bcm4706_read_buf
;
392 b47n
->nand_chip
.legacy
.write_buf
= bcm47xxnflash_ops_bcm4706_write_buf
;
393 b47n
->nand_chip
.legacy
.set_features
= nand_get_set_features_notsupp
;
394 b47n
->nand_chip
.legacy
.get_features
= nand_get_set_features_notsupp
;
396 nand_chip
->legacy
.chip_delay
= 50;
397 b47n
->nand_chip
.bbt_options
= NAND_BBT_USE_FLASH
;
398 b47n
->nand_chip
.ecc
.mode
= NAND_ECC_NONE
; /* TODO: implement ECC */
400 /* Enable NAND flash access */
401 bcma_cc_set32(b47n
->cc
, BCMA_CC_4706_FLASHSCFG
,
402 BCMA_CC_4706_FLASHSCFG_NF1
);
404 /* Configure wait counters */
405 if (b47n
->cc
->status
& BCMA_CC_CHIPST_4706_PKG_OPTION
) {
407 freq
= 400000000 / 4;
409 freq
= bcma_chipco_pll_read(b47n
->cc
, 4);
410 freq
= (freq
& 0xFFF) >> 3;
411 /* Fixed reference clock 25 MHz and m = 2 */
412 freq
= (freq
* 25000000 / 2) / 4;
414 clock
= freq
/ 1000000;
415 w0
= bcm47xxnflash_ops_bcm4706_ns_to_cycle(15, clock
);
416 w1
= bcm47xxnflash_ops_bcm4706_ns_to_cycle(20, clock
);
417 w2
= bcm47xxnflash_ops_bcm4706_ns_to_cycle(10, clock
);
418 w3
= bcm47xxnflash_ops_bcm4706_ns_to_cycle(10, clock
);
419 w4
= bcm47xxnflash_ops_bcm4706_ns_to_cycle(100, clock
);
420 bcma_cc_write32(b47n
->cc
, BCMA_CC_NFLASH_WAITCNT0
,
421 (w4
<< 24 | w3
<< 18 | w2
<< 12 | w1
<< 6 | w0
));
424 err
= nand_scan(&b47n
->nand_chip
, 1);
426 pr_err("Could not scan NAND flash: %d\n", err
);
430 /* Configure FLASH */
431 chipsize
= b47n
->nand_chip
.chipsize
>> 20;
432 tbits
= ffs(chipsize
); /* find first bit set */
433 if (!tbits
|| tbits
!= fls(chipsize
)) {
434 pr_err("Invalid flash size: 0x%lX\n", chipsize
);
438 tbits
+= 19; /* Broadcom increases *index* by 20, we increase *pos* */
440 col_bits
= b47n
->nand_chip
.page_shift
+ 1;
441 col_size
= (col_bits
+ 7) / 8;
443 row_bits
= tbits
- col_bits
+ 1;
444 row_bsize
= (row_bits
+ 7) / 8;
446 val
= ((row_bsize
- 1) << 6) | ((col_size
- 1) << 4) | 2;
447 bcma_cc_write32(b47n
->cc
, BCMA_CC_NFLASH_CONF
, val
);
451 bcma_cc_mask32(b47n
->cc
, BCMA_CC_4706_FLASHSCFG
,
452 ~BCMA_CC_4706_FLASHSCFG_NF1
);