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

Side by Side Diff: board/tegra2/generic/nand/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: Remove some unexpected lines of code and files 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/generic/nand/tegra2_nand.h ('k') | drivers/mtd/nand/nand_base.c » ('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 10
34 #ifndef CONFIG_NAND_CLK_DIVISOR_DDDDDDDH
35 /* Set clock divisor
36 * 7 bits of D and 1 bit of H
37 * divisor= (DDDDDDD + 1) + (H x 0.5)
38 * clock = original clock / divisor
39 * 6 means /4 */
40 #define CONFIG_NAND_CLK_DIVISOR_DDDDDDDH 6
41 #endif
42 static int byte_count;
43
44 static struct nand_ecclayout nand_soft_eccoob = {
45 .oobfree = {
46 {.offset = 40, .length = 24 }}
47 };
48
49 /**
50 * tegra2_nand_read_byte - [DEFAULT] read one byte from the chip
51 * @mtd: MTD device structure
52 *
53 * Default read function for 8bit bus-width
54 */
55 static uint8_t tegra2_nand_read_byte(struct mtd_info *mtd)
56 {
57 struct nand_chip *chip = mtd->priv;
58 int dword_read;
59
60 dword_read = readl(chip->IO_ADDR_R + NAND_RESP_0);
61 dword_read = dword_read >> (8 * byte_count);
62 byte_count ++;
63 return (uint8_t) dword_read;
64 }
65
66 /**
67 * tegra2_nand_write_buf - [DEFAULT] write buffer to chip
68 * @mtd: MTD device structure
69 * @buf: data buffer
70 * @len: number of bytes to write
71 *
72 * Default write function for 8bit bus-width
73 */
74 static void tegra2_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
75 int len)
76 {
77 int i, j, l, l2;
78 struct nand_chip *chip = mtd->priv;
79
80 for (i = 0; i < len/4; i++)
81 {
82 l = ((int *)buf)[i];
83 writel(l, chip->IO_ADDR_W+ NAND_RESP_0);
84 writel(NAND_CMD_GO+ NAND_CMD_PIO+ NAND_CMD_TX+
85 (NAND_CMD_TRANS_SIZE_BYTES4<<NAND_CMD_TRANS_SIZE_SHIFT)
86 + NAND_CMD_A_VALID+ NAND_CMD_CE0,
87 chip->IO_ADDR_W+ NAND_COMMAND_0);
88
89 if (!tegra2_nand_waitfor_cmd_completion(mtd))
90 printf("Command timeout during write_buf\n");
91 }
92 if ((len % 4) != 0)
93 {
94 l = 0;
95 for (j=0; j<(len %4); j++)
96 {
97 l2 = (int) buf[i*4+j];
98 l |= (l2<< (8*j));
99 }
100 writel(l, chip->IO_ADDR_W+ NAND_RESP_0);
101 writel(NAND_CMD_GO+ NAND_CMD_PIO+NAND_CMD_TX+
102 (((len % 4)-1)<<NAND_CMD_TRANS_SIZE_SHIFT)+
103 NAND_CMD_A_VALID+ NAND_CMD_CE0,
104 chip->IO_ADDR_W+ NAND_COMMAND_0);
105 if (!tegra2_nand_waitfor_cmd_completion(mtd))
106 printf("Command timeout during write_buf\n");
107 }
108 }
109
110 /**
111 * tegra2_nand_read_buf - [DEFAULT] read chip data into buffer
112 * @mtd: MTD device structure
113 * @buf: buffer to store date
114 * @len: number of bytes to read
115 *
116 * Default read function for 8bit bus-width
117 */
118 static void tegra2_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
119 {
120 int i, j, l;
121 struct nand_chip *chip = mtd->priv;
122 int *buf_dword;
123
124 buf_dword = (int *) buf;
125 for (i = 0; i < len/4; i++)
126 {
127 writel(NAND_CMD_GO+ NAND_CMD_PIO+ NAND_CMD_RX+
128 (NAND_CMD_TRANS_SIZE_BYTES4<<NAND_CMD_TRANS_SIZE_SHIFT)
129 + NAND_CMD_A_VALID+ NAND_CMD_CE0,
130 chip->IO_ADDR_W+ NAND_COMMAND_0);
131 if (!tegra2_nand_waitfor_cmd_completion(mtd))
132 printf("Command timeout during read_buf\n");
133 l = readl(chip->IO_ADDR_R+ NAND_RESP_0);
134 buf_dword[i] = l;
135 }
136 if ((len % 4) != 0)
137 {
138 writel(NAND_CMD_GO+ NAND_CMD_PIO+NAND_CMD_RX+
139 (((len % 4)-1)<<NAND_CMD_TRANS_SIZE_SHIFT)+
140 NAND_CMD_A_VALID+ NAND_CMD_CE0,
141 chip->IO_ADDR_W + NAND_COMMAND_0);
142 if (!tegra2_nand_waitfor_cmd_completion(mtd))
143 printf("Command timeout during read_buf\n");
144 l = readl(chip->IO_ADDR_R+ NAND_RESP_0);
145 for (j=0; j<(len %4); j++)
146 {
147 buf[i*4+j] = (char) (l>>(8*j));
148 }
149 }
150 }
151
152 /*
153 * = 1 - Command completed
154 * 0 - Timeout
155 */
156 static int tegra2_nand_waitfor_cmd_completion(struct mtd_info *mtd)
157 {
158 struct nand_chip *this = mtd->priv;
159 int i;
160
161 for (i=0; i< NAND_PIO_CMD_TIMEOUT_MS * 1000; i++)
162 {
163 if (!(readl(this->IO_ADDR_R + NAND_CMD_REG1_0) & NAND_CMD_GO))
164 {
165 if (readl(this->IO_ADDR_R + NAND_STATUS_0) &
166 NAND_STATUS_RBSY0)
167 break;
168 }
169 udelay(1);
170 }
171 if (i== NAND_PIO_CMD_TIMEOUT_MS * 1000)
172 return 0;
173 return 1;
174 }
175
176 /*
177 * = 1 - ready
178 * 0 - not ready
179 */
180 static int tegra2_nand_dev_ready(struct mtd_info *mtd)
181 {
182 register struct nand_chip *chip = mtd->priv;
183 int RegVal;
184
185 RegVal= readl(chip->IO_ADDR_R + NAND_STATUS_0);
186 if (RegVal & NAND_STATUS_RBSY0)
187 return 1;
188 else
189 return 0;
190 }
191
192 /*
193 * hardware specific access to control-lines
194 */
195 static void tegra2_nand_hwcontrol(struct mtd_info *mtd, int cmd,
196 unsigned int ctrl)
197 {
198 }
199
200 /**
201 * tegra2_nand_command - [DEFAULT] Send command to NAND device
202 * @mtd: MTD device structure
203 * @command: the command to be sent
204 * @column: the column address for this command, -1 if none
205 * @page_addr: the page address for this command, -1 if none
206 */
207 static void tegra2_nand_command(struct mtd_info *mtd, unsigned int command,
208 int column, int page_addr)
209 {
210 register struct nand_chip *chip = mtd->priv;
211
212 /*
213 * Write out the command to the device.
214 */
215 if (mtd->writesize < 2048) {
216 /* Only command NAND_CMD_RESET or NAND_CMD_READID will come
217 * here before mtd->writesize is initialized, we don't have
218 * any action here because page size of NAND HY27UF084G2B
219 * is 2048 bytes and mtd->writesize will be 2048 after
220 * initialized. */
221 }
222 else
223 {
224 /* Emulate NAND_CMD_READOOB */
225 if (command == NAND_CMD_READOOB)
226 {
227 column += mtd->writesize;
228 command = NAND_CMD_READ0;
229 }
230
231 if (column != -1 || page_addr != -1)
232 {
233 /* Serially input address */
234 if (column != -1)
235 {
236 /* Adjust columns for 16 bit buswidth */
237 if (chip->options & NAND_BUSWIDTH_16)
238 column >>= 1;
239 }
240 }
241 }
242
243 /*
244 * program and erase have their own busy handlers
245 * status and sequential in needs no delay
246 */
247 switch (command) {
248 case NAND_CMD_READID:
249 writel(NAND_CMD_READID, chip->IO_ADDR_W + NAND_CMD_REG1_0);
250 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+ NAND_CMD_PIO+
251 NAND_CMD_RX+
252 (NAND_CMD_TRANS_SIZE_BYTES4<<NAND_CMD_TRANS_SIZE_SHIFT)
253 + NAND_CMD_CE0,
254 chip->IO_ADDR_W + NAND_COMMAND_0);
255 byte_count = 0;
256 break;
257 case NAND_CMD_READ0:
258 writel(NAND_CMD_READ0, chip->IO_ADDR_W + NAND_CMD_REG1_0);
259 writel(NAND_CMD_READSTART, chip->IO_ADDR_W + NAND_CMD_REG2_0);
260 writel((page_addr <<16)+ (column & 0xFFFF),
261 chip->IO_ADDR_W+ NAND_ADDR_REG1_0);
262 writel(page_addr >>16, chip->IO_ADDR_W + NAND_ADDR_REG2_0);
263 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+ NAND_CMD_PIO+
264 NAND_CMD_SEC_CMD+ NAND_CMD_CE0+ NAND_CMD_ALE_BYTES5,
265 chip->IO_ADDR_W+ NAND_COMMAND_0);
266 byte_count = 0;
267 break;
268 case NAND_CMD_SEQIN:
269 writel(NAND_CMD_SEQIN, chip->IO_ADDR_W + NAND_CMD_REG1_0);
270 writel(NAND_CMD_PAGEPROG, chip->IO_ADDR_W + NAND_CMD_REG2_0);
271 writel((page_addr <<16)+ (column & 0xFFFF),
272 chip->IO_ADDR_W+ NAND_ADDR_REG1_0);
273 writel(page_addr >>16, chip->IO_ADDR_W + NAND_ADDR_REG2_0);
274 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+ NAND_CMD_PIO+
275 NAND_CMD_SEC_CMD+ NAND_CMD_AFT_DAT+ NAND_CMD_CE0+
276 NAND_CMD_ALE_BYTES5,
277 chip->IO_ADDR_W+ NAND_COMMAND_0);
278 break;
279 case NAND_CMD_PAGEPROG:
280 writel(NAND_CMD_PAGEPROG, chip->IO_ADDR_W + NAND_CMD_REG1_0);
281 writel(NAND_CMD_GO+NAND_CMD_CLE+NAND_CMD_CE0,
282 chip->IO_ADDR_W + NAND_COMMAND_0);
283 break;
284 case NAND_CMD_ERASE1:
285 writel(NAND_CMD_ERASE1, chip->IO_ADDR_W + NAND_CMD_REG1_0);
286 writel(NAND_CMD_ERASE2, chip->IO_ADDR_W + NAND_CMD_REG2_0);
287 writel(page_addr, chip->IO_ADDR_W + NAND_ADDR_REG1_0);
288 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+
289 NAND_CMD_SEC_CMD+ NAND_CMD_CE0+ NAND_CMD_ALE_BYTES3,
290 chip->IO_ADDR_W+ NAND_COMMAND_0);
291 break;
292 case NAND_CMD_RNDOUT:
293 writel(NAND_CMD_RNDOUT, chip->IO_ADDR_W + NAND_CMD_REG1_0);
294 writel(NAND_CMD_RNDOUTSTART, chip->IO_ADDR_W + NAND_CMD_REG2_0);
295 writel((column & 0xFFFF), chip->IO_ADDR_W + NAND_ADDR_REG1_0);
296 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_ALE+ NAND_CMD_PIO+
297 NAND_CMD_SEC_CMD+ NAND_CMD_CE0+ NAND_CMD_ALE_BYTES2,
298 chip->IO_ADDR_W+ NAND_COMMAND_0);
299 break;
300 case NAND_CMD_ERASE2:
301 return;
302 case NAND_CMD_STATUS:
303 writel(NAND_CMD_STATUS, chip->IO_ADDR_W + NAND_CMD_REG1_0);
304 writel(NAND_CMD_GO+ NAND_CMD_CLE+ NAND_CMD_PIO+ NAND_CMD_RX+
305 (NAND_CMD_TRANS_SIZE_BYTES1<<NAND_CMD_TRANS_SIZE_SHIFT)
306 +NAND_CMD_CE0,
307 chip->IO_ADDR_W+ NAND_COMMAND_0);
308 byte_count = 0;
309 break;
310
311 case NAND_CMD_RESET:
312 writel(NAND_CMD_RESET, chip->IO_ADDR_W + NAND_CMD_REG1_0);
313 writel(NAND_CMD_GO+NAND_CMD_CLE+NAND_CMD_CE0,
314 chip->IO_ADDR_W + NAND_COMMAND_0);
315 break;
316 default:
317 /*
318 * If we don't have access to the busy pin, we apply the given
319 * command delay
320 */
321 if (!chip->dev_ready)
322 {
323 udelay(chip->chip_delay);
324 return;
325 }
326 }
327 if (!tegra2_nand_waitfor_cmd_completion(mtd))
328 printf("Command 0x%02X timeout\n", command);
329 }
330
331 /*
332 * Board-specific NAND initialization. The following members of the
333 * argument are board-specific (per include/linux/mtd/nand.h):
334 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
335 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
336 * - cmd_ctrl: hardwarespecific function for accesing control-lines
337 * - dev_ready: hardwarespecific function for accesing device ready/busy line
338 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
339 * only be provided if a hardware ECC is available
340 * - eccm.ode: mode of ecc, see defines
341 * - chip_delay: chip dependent delay for transfering data from array to
342 * read regs (tR)
343 * - options: various chip options. They can partly be set to inform
344 * nand_scan about special functionality. See the defines for further
345 * explanation
346 * Members with a "?" were not set in the merged testing-NAND branch,
347 * so they are not set here either.
348 */
349 int board_nand_init(struct nand_chip *nand)
350 {
351 int RegVal;
352
353 /* Assert RESET to NAND controller */
354 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
355 CLK_RST_CONTROLLER_RST_DEVICES_L_0);
356 RegVal |= (1 <<
357 CLK_RST_CONTROLLER_RST_DEVICES_L_0_SWR_NDFLASH_RST_SHIFT);
358 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
359 CLK_RST_CONTROLLER_RST_DEVICES_L_0);
360
361 /* enable clock to NAND controller */
362 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
363 CLK_RST_CONTROLLER_CLK_OUT_ENB_L_0);
364 RegVal |= (1 <<
365 CLK_RST_CONTROLLER_CLK_OUT_ENB_L_0_CLK_ENB_NDFLASH_SHIFT);
366 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
367 CLK_RST_CONTROLLER_CLK_OUT_ENB_L_0);
368
369 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
370 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0);
371 RegVal &=
372 ~CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0_NDFLASH_CLK_DIVISOR_MASK;
373 RegVal |= CONFIG_NAND_CLK_DIVISOR_DDDDDDDH;
374 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
375 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0);
376 udelay(1);
377
378 /* Set clock source as PLLP_OUT0 */
379 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
380 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0);
381 RegVal &= ~(
382 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0_NDFLASH_CLK_SRC_MASK <<
383 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0_NDFLASH_CLK_SRC_SHIFT);
384 RegVal |=
385 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0_NDFLASH_CLK_SRC_PLLP_OUT0;
386 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
387 CLK_RST_CONTROLLER_CLK_SOURCE_NDFLASH_0);
388 udelay(2);
389
390 /* Deassert RESET to NAND controller */
391 RegVal = readl(NV_ADDRESS_MAP_CLK_RST_BASE +
392 CLK_RST_CONTROLLER_RST_DEVICES_L_0);
393 RegVal &= ~(1 <<
394 CLK_RST_CONTROLLER_RST_DEVICES_L_0_SWR_NDFLASH_RST_SHIFT);
395 writel(RegVal, NV_ADDRESS_MAP_CLK_RST_BASE +
396 CLK_RST_CONTROLLER_RST_DEVICES_L_0);
397
398 /* pinmux ATC_SEL uses NAND */
399 RegVal = readl(NV_ADDRESS_MAP_APB_MISC_BASE +
400 APB_MISC_PP_PIN_MUX_CTL_A_0);
401 RegVal &= ~(APB_MISC_PP_PIN_MUX_CTL_A_0_ATC_SEL_DEFAULT_MASK
402 << APB_MISC_PP_PIN_MUX_CTL_A_0_ATC_SEL_SHIFT);
403 RegVal |= (APB_MISC_PP_PIN_MUX_CTL_A_0_ATC_SEL_NAND
404 << APB_MISC_PP_PIN_MUX_CTL_A_0_ATC_SEL_SHIFT);
405 writel(RegVal, NV_ADDRESS_MAP_APB_MISC_BASE +
406 APB_MISC_PP_PIN_MUX_CTL_A_0);
407
408 RegVal = NAND_CONFIG_BUS_WIDTH_8BIT + NAND_CONFIG_PAGE_SIZE_2048;
409 writel(RegVal, NAND_BASE + NAND_CONFIG_0);
410
411 /* Frequence output of PLLP_OUT0 is set by BOOTROM to 216MHz
412 * to CLK_RST_CONTROLLER_PLLP_BASE_0,
413 * 216MHz / divisor 4 = 54MHZ
414 * 1 clock = 18.5 ns */
415 /* Set timing for NAND device, defined in tegra2_nand.h.
416 * If not defined, then use timing that was set by BOOTROM. */
417 #ifdef CONFIG_TEGRA2_NAND_TIMING
418 writel(CONFIG_TEGRA2_NAND_TIMING, NAND_BASE + NAND_TIMING_0);
419 #endif
420 #ifdef CONFIG_TEGRA2_NAND_TIMING2
421 writel(CONFIG_TEGRA2_NAND_TIMING2, NAND_BASE + NAND_TIMING2_0);
422 #endif
423 #if (LINUX_MACH_TYPE == MACH_TYPE_SEABOARD)
424 /* GPIO port H bit 3, H.03, GMI_AD11->MFG_MODE_R, */
425 tg2_gpio_direction_output(7, 3, 1);
426 #endif
427 #if (LINUX_MACH_TYPE == MACH_TYPE_HARMONY)
428 /* GPIO port C bit 7, C.07, GMI_WP->NAND_WP */
429 tg2_gpio_direction_output(2, 7, 1);
430 #endif
431 nand->cmd_ctrl = tegra2_nand_hwcontrol;
432 nand->dev_ready = tegra2_nand_dev_ready;
433 nand->ecc.mode = NAND_ECC_NONE;
434 nand->ecc.layout = &nand_soft_eccoob;
435 nand->options = LP_OPTIONS;
436 nand->cmdfunc = tegra2_nand_command;
437 nand->read_byte = tegra2_nand_read_byte;
438 nand->read_buf = tegra2_nand_read_buf;
439 nand->write_buf = tegra2_nand_write_buf;
440 return 0;
441 }
442
OLDNEW
« no previous file with comments | « board/tegra2/generic/nand/tegra2_nand.h ('k') | drivers/mtd/nand/nand_base.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698