Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: board/tegra2/common/nand/HY27UF084G2B/tegra2_nand.c

Issue 6623073: Chromium: arm: tegra: Add NAND support (Closed) Base URL: http://git.chromium.org/git/u-boot-next.git@chromeos-v2010.09
Patch Set: Fix the issue that access to NAND device fails when jumper is set to SPI boot Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « board/tegra2/common/nand/HY27UF084G2B/tegra2_nand.h ('k') | board/tegra2/generic/Makefile » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * (C) Copyright 2006 Detlev Zundel, dzu@denx.de
3 * (C) Copyright 2006 DENX Software Engineering
4 * (C) Copyright 2011 NVIDIA Corporation <www.nvidia.com>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 */
24
25 #include <common.h>
26 #include <asm/io.h>
27 #include <nand.h>
28 #include <asm/arch/gpio.h>
29 #include <asm/arch/nvcommon.h>
30 #include "../../board.h"
31 #include "tegra2_nand.h"
32
33 #define NAND_PIO_CMD_TIMEOUT_MS 100
34 static int byte_count;
35
36 static struct nand_ecclayout nand_soft_eccoob = {
37 .oobfree = {
38 {.offset = 40, .length = 24 }}
39 };
40
41 /**
42 * tegra2_nand_read_byte - [DEFAULT] read one byte from the chip
43 * @mtd: MTD device structure
44 *
45 * Default read function for 8bit bus-width
46 */
47 static uint8_t tegra2_nand_read_byte(struct mtd_info *mtd)
48 {
49 struct nand_chip *chip = mtd->priv;
50 int dword_read;
51
52 dword_read = readl(chip->IO_ADDR_R + NAND_RESP_0);
53 dword_read = dword_read >> (8 * byte_count);
54 byte_count ++;
55 return (uint8_t) dword_read;
56 }
57
58 /**
59 * tegra2_nand_write_buf - [DEFAULT] write buffer to chip
60 * @mtd: MTD device structure
61 * @buf: data buffer
62 * @len: number of bytes to write
63 *
64 * Default write function for 8bit bus-width
65 */
66 static void tegra2_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
67 int len)
68 {
69 int i, j, l, l2;
70 struct nand_chip *chip = mtd->priv;
71
72 for (i = 0; i < len/4; i++)
73 {
74 l = ((int *)buf)[i];
75 writel(l, chip->IO_ADDR_W+ NAND_RESP_0);
76 writel(NAND_CMD_GO+ NAND_CMD_PIO+ NAND_CMD_TX+
77 (NAND_CMD_TRANS_SIZE_BYTES4<<NAND_CMD_TRANS_SIZE_SHIFT)
78 + NAND_CMD_A_VALID+ NAND_CMD_CE0,
79 chip->IO_ADDR_W+ NAND_COMMAND_0);
80
81 if (!tegra2_nand_waitfor_GO_cleared(mtd))
82 printf("Command timeout during write_buf\n");
83 }
84 if ((len % 4) != 0)
85 {
86 l = 0;
87 for (j=0; j<(len %4); j++)
88 {
89 l2 = (int) buf[i*4+j];
90 l |= (l2<< (8*j));
91 }
92 writel(l, chip->IO_ADDR_W+ NAND_RESP_0);
93 writel(NAND_CMD_GO+ NAND_CMD_PIO+NAND_CMD_TX+
94 (((len % 4)-1)<<NAND_CMD_TRANS_SIZE_SHIFT)+
95 NAND_CMD_A_VALID+ NAND_CMD_CE0,
96 chip->IO_ADDR_W+ NAND_COMMAND_0);
97 if (!tegra2_nand_waitfor_GO_cleared(mtd))
98 printf("Command timeout during write_buf\n");
99 }
100 }
101
102 /**
103 * tegra2_nand_read_buf - [DEFAULT] read chip data into buffer
104 * @mtd: MTD device structure
105 * @buf: buffer to store date
106 * @len: number of bytes to read
107 *
108 * Default read function for 8bit bus-width
109 */
110 static void tegra2_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
111 {
112 int i, j, l;
113 struct nand_chip *chip = mtd->priv;
114 int *buf_dword;
115
116 buf_dword = (int *) buf;
117 for (i = 0; i < len/4; i++)
118 {
119 writel(NAND_CMD_GO+ NAND_CMD_PIO+ NAND_CMD_RX+
120 (NAND_CMD_TRANS_SIZE_BYTES4<<NAND_CMD_TRANS_SIZE_SHIFT)
121 + NAND_CMD_A_VALID+ NAND_CMD_CE0,
122 chip->IO_ADDR_W+ NAND_COMMAND_0);
123 if (!tegra2_nand_waitfor_GO_cleared(mtd))
124 printf("Command timeout during read_buf\n");
125 l = readl(chip->IO_ADDR_R+ NAND_RESP_0);
126 buf_dword[i] = l;
127 }
128 if ((len % 4) != 0)
129 {
130 writel(NAND_CMD_GO+ NAND_CMD_PIO+NAND_CMD_RX+
131 (((len % 4)-1)<<NAND_CMD_TRANS_SIZE_SHIFT)+
132 NAND_CMD_A_VALID+ NAND_CMD_CE0,
133 chip->IO_ADDR_W + NAND_COMMAND_0);
134 if (!tegra2_nand_waitfor_GO_cleared(mtd))
135 printf("Command timeout during read_buf\n");
136 l = readl(chip->IO_ADDR_R+ NAND_RESP_0);
137 for (j=0; j<(len %4); j++)
138 {
139 buf[i*4+j] = (char) (l>>(8*j));
140 }
141 }
142 }
143
144 /*
145 * = 1 - ready
146 * 0 - not ready
147 */
148 static int tegra2_nand_waitfor_GO_cleared(struct mtd_info *mtd)
149 {
150 struct nand_chip *this = mtd->priv;
151 int i;
152
153 for (i=0; i< NAND_PIO_CMD_TIMEOUT_MS * 1000; i++)
154 {
155 if (!(readl(this->IO_ADDR_R + NAND_CMD_REG1_0) & NAND_CMD_GO))
156 break;
157 else
158 udelay(1);
159 }
160 if (i== NAND_PIO_CMD_TIMEOUT_MS * 1000)
161 return 0;
162 return 1;
163 }
164
165 /*
166 * = 1 - ready
167 * 0 - not ready
168 */
169 static int tegra2_nand_dev_ready(struct mtd_info *mtd)
170 {
171 register struct nand_chip *chip = mtd->priv;
172 int status;
173
174 writel(NAND_CMD_STATUS, chip->IO_ADDR_W + NAND_CMD_REG1_0);
175 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_PIO+ NAND_CMD_RX+
176 (NAND_CMD_TRANS_SIZE_BYTES1<<NAND_CMD_TRANS_SIZE_SHIFT)+
177 NAND_CMD_CE0,
178 chip->IO_ADDR_W+ NAND_COMMAND_0);
179 if (!(tegra2_nand_waitfor_GO_cleared(mtd)))
180 printf("NAND_CMD_STATUS command timeout\n");
181 udelay(1);
182 status = readl(chip->IO_ADDR_R + NAND_RESP_0);
183 if (status & NAND_STATUS_READY)
184 return 1;
185 return 0;
186 }
187
188 /*
189 * hardware specific access to control-lines
190 */
191 static void tegra2_nand_hwcontrol(struct mtd_info *mtd, int cmd,
192 unsigned int ctrl)
193 {
194 }
195
196 /**
197 * tegra2_nand_command - [DEFAULT] Send command to NAND device
198 * @mtd: MTD device structure
199 * @command: the command to be sent
200 * @column: the column address for this command, -1 if none
201 * @page_addr: the page address for this command, -1 if none
202 */
203 static void tegra2_nand_command(struct mtd_info *mtd, unsigned int command,
204 int column, int page_addr)
205 {
206 register struct nand_chip *chip = mtd->priv;
207
208 /*
209 * Write out the command to the device.
210 */
211 if (mtd->writesize < 2048) {
212 /* Only command NAND_CMD_RESET or NAND_CMD_READID will come
213 * here before mtd->writesize is initialized, we don't have
214 * any action here because page size of NAND HY27UF084G2B
215 * is 2048 bytes and mtd->writesize will be 2048 after
216 * initialized. */
217 }
218 else
219 {
220 /* Emulate NAND_CMD_READOOB */
221 if (command == NAND_CMD_READOOB)
222 {
223 column += mtd->writesize;
224 command = NAND_CMD_READ0;
225 }
226
227 if (column != -1 || page_addr != -1)
228 {
229 /* Serially input address */
230 if (column != -1)
231 {
232 /* Adjust columns for 16 bit buswidth */
233 if (chip->options & NAND_BUSWIDTH_16)
234 column >>= 1;
235 }
236 }
237 }
238
239 /*
240 * program and erase have their own busy handlers
241 * status and sequential in needs no delay
242 */
243 switch (command) {
244 case NAND_CMD_READID:
245 writel(NAND_CMD_READID, chip->IO_ADDR_W + NAND_CMD_REG1_0);
246 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+ NAND_CMD_PIO+
247 NAND_CMD_RX+
248 (NAND_CMD_TRANS_SIZE_BYTES4<<NAND_CMD_TRANS_SIZE_SHIFT)
249 + NAND_CMD_CE0,
250 chip->IO_ADDR_W + NAND_COMMAND_0);
251 byte_count = 0;
252 break;
253 case NAND_CMD_READ0:
254 writel(NAND_CMD_READ0, chip->IO_ADDR_W + NAND_CMD_REG1_0);
255 writel(NAND_CMD_READSTART, chip->IO_ADDR_W + NAND_CMD_REG2_0);
256 writel((page_addr <<16)+ (column & 0xFFFF),
257 chip->IO_ADDR_W+ NAND_ADDR_REG1_0);
258 writel(page_addr >>16, chip->IO_ADDR_W + NAND_ADDR_REG2_0);
259 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+ NAND_CMD_PIO+
260 NAND_CMD_SEC_CMD+ NAND_CMD_CE0+ NAND_CMD_ALE_BYTES5,
261 chip->IO_ADDR_W+ NAND_COMMAND_0);
262 byte_count = 0;
263 break;
264 case NAND_CMD_SEQIN:
265 writel(NAND_CMD_SEQIN, chip->IO_ADDR_W + NAND_CMD_REG1_0);
266 writel(NAND_CMD_PAGEPROG, chip->IO_ADDR_W + NAND_CMD_REG2_0);
267 writel((page_addr <<16)+ (column & 0xFFFF),
268 chip->IO_ADDR_W+ NAND_ADDR_REG1_0);
269 writel(page_addr >>16, chip->IO_ADDR_W + NAND_ADDR_REG2_0);
270 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+ NAND_CMD_PIO+
271 NAND_CMD_SEC_CMD+ NAND_CMD_AFT_DAT+ NAND_CMD_CE0+
272 NAND_CMD_ALE_BYTES5,
273 chip->IO_ADDR_W+ NAND_COMMAND_0);
274 break;
275 case NAND_CMD_PAGEPROG:
276 writel(NAND_CMD_PAGEPROG, chip->IO_ADDR_W + NAND_CMD_REG1_0);
277 writel(NAND_CMD_GO+NAND_CMD_CLE+NAND_CMD_CE0,
278 chip->IO_ADDR_W + NAND_COMMAND_0);
279 break;
280 case NAND_CMD_ERASE1:
281 writel(NAND_CMD_ERASE1, chip->IO_ADDR_W + NAND_CMD_REG1_0);
282 writel(NAND_CMD_ERASE2, chip->IO_ADDR_W + NAND_CMD_REG2_0);
283 writel(page_addr, chip->IO_ADDR_W + NAND_ADDR_REG1_0);
284 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+
285 NAND_CMD_SEC_CMD+ NAND_CMD_CE0+ NAND_CMD_ALE_BYTES3,
286 chip->IO_ADDR_W+ NAND_COMMAND_0);
287 break;
288 case NAND_CMD_RNDOUT:
289 writel(NAND_CMD_RNDOUT, chip->IO_ADDR_W + NAND_CMD_REG1_0);
290 writel(NAND_CMD_RNDOUTSTART, chip->IO_ADDR_W + NAND_CMD_REG2_0);
291 writel((column & 0xFFFF), chip->IO_ADDR_W + NAND_ADDR_REG1_0);
292 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+ NAND_CMD_PIO+
293 NAND_CMD_SEC_CMD+ NAND_CMD_CE0+ NAND_CMD_ALE_BYTES2,
294 chip->IO_ADDR_W+ NAND_COMMAND_0);
295 break;
296 case NAND_CMD_ERASE2:
297 return;
298 case NAND_CMD_STATUS:
299 writel(NAND_CMD_STATUS, chip->IO_ADDR_W + NAND_CMD_REG1_0);
300 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_PIO+ NAND_CMD_RX+
301 (NAND_CMD_TRANS_SIZE_BYTES1<<NAND_CMD_TRANS_SIZE_SHIFT)
302 +NAND_CMD_CE0,
303 chip->IO_ADDR_W+ NAND_COMMAND_0);
304 byte_count = 0;
305 break;
306
307 case NAND_CMD_RESET:
308 writel(NAND_CMD_RESET, chip->IO_ADDR_W + NAND_CMD_REG1_0);
309 writel(NAND_CMD_GO+NAND_CMD_CLE+NAND_CMD_CE0,
310 chip->IO_ADDR_W + NAND_COMMAND_0);
311 break;
312 default:
313 /*
314 * If we don't have access to the busy pin, we apply the given
315 * command delay
316 */
317 if (!chip->dev_ready)
318 {
319 udelay(chip->chip_delay);
320 return;
321 }
322 }
323 if (!tegra2_nand_waitfor_GO_cleared(mtd))
324 printf("Command 0x%02X timeout\n", command);
325
326 if (command == NAND_CMD_READ0)
327 /* Max tR time for HYNIX HY27UF4G2B */
328 udelay(25);
329 else
330 udelay(1);
331 }
332
333 /*
334 * Board-specific NAND initialization. The following members of the
335 * argument are board-specific (per include/linux/mtd/nand.h):
336 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
337 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
338 * - cmd_ctrl: hardwarespecific function for accesing control-lines
339 * - dev_ready: hardwarespecific function for accesing device ready/busy line
340 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
341 * only be provided if a hardware ECC is available
342 * - eccm.ode: mode of ecc, see defines
343 * - chip_delay: chip dependent delay for transfering data from array to
344 * read regs (tR)
345 * - options: various chip options. They can partly be set to inform
346 * nand_scan about special functionality. See the defines for further
347 * explanation
348 * Members with a "?" were not set in the merged testing-NAND branch,
349 * so they are not set here either.
350 */
351 int board_nand_init(struct nand_chip *nand)
352 {
353 int RegVal;
354
355 /* Assert RESET to NAND controller */
356 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
357 CLK_RST_CONTROLLER_RST_DEVICES_L_0);
358 RegVal |= (1 <<
359 CLK_RST_CONTROLLER_RST_DEVICES_L_0_SWR_NDFLASH_RST_SHIFT);
360 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
361 CLK_RST_CONTROLLER_RST_DEVICES_L_0);
362
363 /* enable clock to NAND controller */
364 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
365 CLK_RST_CONTROLLER_CLK_OUT_ENB_L_0);
366 RegVal |= (1 <<
367 CLK_RST_CONTROLLER_CLK_OUT_ENB_L_0_CLK_ENB_NDFLASH_SHIFT);
368 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
369 CLK_RST_CONTROLLER_CLK_OUT_ENB_L_0);
370
371 /* Set clock divisor
372 * 7 bits of d and 1 bit of h
373 * divisor= (ddddddd + 1) + (h x 0.5) */
374 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
375 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0);
376 RegVal &=
377 ~CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0_NDFLASH_CLK_DIVISOR_MASK;
378 /* 6 means the divisor is 4 based on above calculation */
379 RegVal |= 6;
380 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
381 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0);
382 udelay(1);
383
384 /* Set clock source as PLLP_OUT0 */
385 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
386 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0);
387 RegVal &= ~(
388 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0_NDFLASH_CLK_SRC_MASK <<
389 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0_NDFLASH_CLK_SRC_SHIFT);
390 RegVal |=
391 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0_NDFLASH_CLK_SRC_PLLP_OUT0;
392 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
393 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0);
394 udelay(2);
395
396 /* Deassert RESET to NAND controller */
397 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
398 CLK_RST_CONTROLLER_RST_DEVICES_L_0);
399 RegVal &= ~(1 <<
400 CLK_RST_CONTROLLER_RST_DEVICES_L_0_SWR_NDFLASH_RST_SHIFT);
401 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
402 CLK_RST_CONTROLLER_RST_DEVICES_L_0);
403
404 /* pinmux ATC_SEL uses NAND */
405 RegVal = readl(NV_ADDRESS_MAP_APB_MISC_BASE +
406 APB_MISC_PP_PIN_MUX_CTL_A_0);
407 RegVal &= ~(APB_MISC_PP_PIN_MUX_CTL_A_0_ATC_SEL_DEFAULT_MASK
408 << APB_MISC_PP_PIN_MUX_CTL_A_0_ATC_SEL_SHIFT);
409 RegVal |= (APB_MISC_PP_PIN_MUX_CTL_A_0_ATC_SEL_NAND
410 << APB_MISC_PP_PIN_MUX_CTL_A_0_ATC_SEL_SHIFT);
411 writel(RegVal, NV_ADDRESS_MAP_APB_MISC_BASE +
412 APB_MISC_PP_PIN_MUX_CTL_A_0);
413
414 RegVal = NAND_CONFIG_BUS_WIDTH_8BIT + NAND_CONFIG_PAGE_SIZE_2048;
415 writel(RegVal, NAND_BASE + NAND_CONFIG_0);
416
417 /* Frequence output of PLLP_OUT0 is set by BOOTROM to 6MHz
418 * to CLK_RST_CONTROLLER_PLLP_BASE_0,
419 * 6MHz / divisor 4 = 1.5MHZ
420 * 1 clock = 667 ns */
421
Tom Warren 2011/03/16 18:04:35 That doesn't sound right. PLLP is set to 216MHz in
422 /* Set timing for HYNIX HY27UF4G2B */
423 RegVal = 0 << NAND_TIMING_TRP_RESP_CNT_SHIFT +
424 0 << NAND_TIMING_TWB_CNT_SHIFT +
425 0 << NAND_TIMING_TCR_TAR_TRR_CNT_SHIFT +
426 0 << NAND_TIMING_TWHR_CNT_SHIFT +
427 0 << NAND_TIMING_TCS_CNT_SHIFT +
428 0 << NAND_TIMING_TWH_CNT_SHIFT +
429 0 << NAND_TIMING_TWP_CNT_SHIFT +
430 0 << NAND_TIMING_TRH_CNT_SHIFT +
431 0 << NAND_TIMING_TRP_CNT_SHIFT;
432 writel(RegVal, NAND_BASE + NAND_TIMING_0);
433
434 RegVal = 0 << NAND_TIMING2_TADL_CNT_SHIFT;
435 writel(RegVal, NAND_BASE + NAND_TIMING2_0);
436
Tom Warren 2011/03/16 18:04:35 So NAND_TIMING_0 and NAND_TIMING2_0 are being set
robotboy 2011/03/16 22:51:34 I agree, this seems wrong. These values should pr
437 #if (LINUX_MACH_TYPE == MACH_TYPE_SEABOARD)
438 /* GPIO port H bit 3, H.03, GMI_AD11->MFG_MODE_R, */
439 tg2_gpio_direction_output(7, 3, 1);
440 #endif
441 #if (LINUX_MACH_TYPE == MACH_TYPE_HARMONY)
442 /* GPIO port C bit 7, C.07, GMI_WP->NAND_WP */
443 tg2_gpio_direction_output(2, 7, 1);
444 #endif
445 nand->cmd_ctrl = tegra2_nand_hwcontrol;
446 nand->dev_ready = tegra2_nand_dev_ready;
447 nand->ecc.mode = NAND_ECC_NONE;
448 nand->ecc.layout = &nand_soft_eccoob;
449 nand->options = LP_OPTIONS;
450 nand->cmdfunc = tegra2_nand_command;
451 nand->read_byte = tegra2_nand_read_byte;
452 nand->read_buf = tegra2_nand_read_buf;
453 nand->write_buf = tegra2_nand_write_buf;
454 return 0;
455 }
OLDNEW
« no previous file with comments | « board/tegra2/common/nand/HY27UF084G2B/tegra2_nand.h ('k') | board/tegra2/generic/Makefile » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698