1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
24 #include "lpc3180_nand_controller.h"
26 #include "replacements.h"
35 int lpc3180_nand_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, struct nand_device_s
*device
);
36 int lpc3180_register_commands(struct command_context_s
*cmd_ctx
);
37 int lpc3180_init(struct nand_device_s
*device
);
38 int lpc3180_reset(struct nand_device_s
*device
);
39 int lpc3180_command(struct nand_device_s
*device
, u8 command
);
40 int lpc3180_address(struct nand_device_s
*device
, u8 address
);
41 int lpc3180_write_data(struct nand_device_s
*device
, u16 data
);
42 int lpc3180_read_data(struct nand_device_s
*device
, void *data
);
43 int lpc3180_write_page(struct nand_device_s
*device
, u32 page
, u8
*data
, u32 data_size
, u8
*oob
, u32 oob_size
);
44 int lpc3180_read_page(struct nand_device_s
*device
, u32 page
, u8
*data
, u32 data_size
, u8
*oob
, u32 oob_size
);
45 int lpc3180_controller_ready(struct nand_device_s
*device
, int timeout
);
46 int lpc3180_nand_ready(struct nand_device_s
*device
, int timeout
);
48 int handle_lpc3180_select_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
50 nand_flash_controller_t lpc3180_nand_controller
=
53 .nand_device_command
= lpc3180_nand_device_command
,
54 .register_commands
= lpc3180_register_commands
,
56 .reset
= lpc3180_reset
,
57 .command
= lpc3180_command
,
58 .address
= lpc3180_address
,
59 .write_data
= lpc3180_write_data
,
60 .read_data
= lpc3180_read_data
,
61 .write_page
= lpc3180_write_page
,
62 .read_page
= lpc3180_read_page
,
63 .controller_ready
= lpc3180_controller_ready
,
64 .nand_ready
= lpc3180_nand_ready
,
67 /* nand device lpc3180 <target#> <oscillator_frequency>
69 int lpc3180_nand_device_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
, struct nand_device_s
*device
)
71 lpc3180_nand_controller_t
*lpc3180_info
;
75 LOG_WARNING("incomplete 'lpc3180' nand flash configuration");
76 return ERROR_FLASH_BANK_INVALID
;
79 lpc3180_info
= malloc(sizeof(lpc3180_nand_controller_t
));
80 device
->controller_priv
= lpc3180_info
;
82 lpc3180_info
->target
= get_target_by_num(strtoul(args
[1], NULL
, 0));
83 if (!lpc3180_info
->target
)
85 LOG_ERROR("no target '%s' configured", args
[1]);
86 return ERROR_NAND_DEVICE_INVALID
;
89 lpc3180_info
->osc_freq
= strtoul(args
[2], NULL
, 0);
90 if ((lpc3180_info
->osc_freq
< 1000) || (lpc3180_info
->osc_freq
> 20000))
92 LOG_WARNING("LPC3180 oscillator frequency should be between 1000 and 20000 kHz, was %i", lpc3180_info
->osc_freq
);
94 lpc3180_info
->selected_controller
= LPC3180_NO_CONTROLLER
;
95 lpc3180_info
->sw_write_protection
= 0;
96 lpc3180_info
->sw_wp_lower_bound
= 0x0;
97 lpc3180_info
->sw_wp_upper_bound
= 0x0;
102 int lpc3180_register_commands(struct command_context_s
*cmd_ctx
)
104 command_t
*lpc3180_cmd
= register_command(cmd_ctx
, NULL
, "lpc3180", NULL
, COMMAND_ANY
, "commands specific to the LPC3180 NAND flash controllers");
106 register_command(cmd_ctx
, lpc3180_cmd
, "select", handle_lpc3180_select_command
, COMMAND_EXEC
, "select <'mlc'|'slc'> controller (default is mlc)");
111 int lpc3180_pll(int fclkin
, u32 pll_ctrl
)
113 int bypass
= (pll_ctrl
& 0x8000) >> 15;
114 int direct
= (pll_ctrl
& 0x4000) >> 14;
115 int feedback
= (pll_ctrl
& 0x2000) >> 13;
116 int p
= (1 << ((pll_ctrl
& 0x1800) >> 11) * 2);
117 int n
= ((pll_ctrl
& 0x0600) >> 9) + 1;
118 int m
= ((pll_ctrl
& 0x01fe) >> 1) + 1;
119 int lock
= (pll_ctrl
& 0x1);
122 LOG_WARNING("PLL is not locked");
124 if (!bypass
&& direct
) /* direct mode */
125 return (m
* fclkin
) / n
;
127 if (bypass
&& !direct
) /* bypass mode */
128 return fclkin
/ (2 * p
);
130 if (bypass
& direct
) /* direct bypass mode */
133 if (feedback
) /* integer mode */
134 return m
* (fclkin
/ n
);
135 else /* non-integer mode */
136 return (m
/ (2 * p
)) * (fclkin
/ n
);
139 float lpc3180_cycle_time(lpc3180_nand_controller_t
*lpc3180_info
)
141 target_t
*target
= lpc3180_info
->target
;
142 u32 sysclk_ctrl
, pwr_ctrl
, hclkdiv_ctrl
, hclkpll_ctrl
;
148 /* calculate timings */
150 /* determine current SYSCLK (13'MHz or main oscillator) */
151 target_read_u32(target
, 0x40004050, &sysclk_ctrl
);
153 if ((sysclk_ctrl
& 1) == 0)
154 sysclk
= lpc3180_info
->osc_freq
;
158 /* determine selected HCLK source */
159 target_read_u32(target
, 0x40004044, &pwr_ctrl
);
161 if ((pwr_ctrl
& (1 << 2)) == 0) /* DIRECT RUN mode */
167 target_read_u32(target
, 0x40004058, &hclkpll_ctrl
);
168 hclk_pll
= lpc3180_pll(sysclk
, hclkpll_ctrl
);
170 target_read_u32(target
, 0x40004040, &hclkdiv_ctrl
);
172 if (pwr_ctrl
& (1 << 10)) /* ARM_CLK and HCLK use PERIPH_CLK */
174 hclk
= hclk_pll
/ (((hclkdiv_ctrl
& 0x7c) >> 2) + 1);
176 else /* HCLK uses HCLK_PLL */
178 hclk
= hclk_pll
/ (1 << (hclkdiv_ctrl
& 0x3));
182 LOG_DEBUG("LPC3180 HCLK currently clocked at %i kHz", hclk
);
184 cycle
= (1.0 / hclk
) * 1000000.0;
189 int lpc3180_init(struct nand_device_s
*device
)
191 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
192 target_t
*target
= lpc3180_info
->target
;
193 int bus_width
= (device
->bus_width
) ? (device
->bus_width
) : 8;
194 int address_cycles
= (device
->address_cycles
) ? (device
->address_cycles
) : 3;
195 int page_size
= (device
->page_size
) ? (device
->page_size
) : 512;
197 if (target
->state
!= TARGET_HALTED
)
199 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
200 return ERROR_NAND_OPERATION_FAILED
;
203 /* sanitize arguments */
204 if ((bus_width
!= 8) && (bus_width
!= 16))
206 LOG_ERROR("LPC3180 only supports 8 or 16 bit bus width, not %i", bus_width
);
207 return ERROR_NAND_OPERATION_NOT_SUPPORTED
;
210 /* The LPC3180 only brings out 8 bit NAND data bus, but the controller
211 * would support 16 bit, too, so we just warn about this for now
215 LOG_WARNING("LPC3180 only supports 8 bit bus width");
218 /* inform calling code about selected bus width */
219 device
->bus_width
= bus_width
;
221 if ((address_cycles
!= 3) && (address_cycles
!= 4))
223 LOG_ERROR("LPC3180 only supports 3 or 4 address cycles, not %i", address_cycles
);
224 return ERROR_NAND_OPERATION_NOT_SUPPORTED
;
227 if ((page_size
!= 512) && (page_size
!= 2048))
229 LOG_ERROR("LPC3180 only supports 512 or 2048 byte pages, not %i", page_size
);
230 return ERROR_NAND_OPERATION_NOT_SUPPORTED
;
233 /* select MLC controller if none is currently selected */
234 if (lpc3180_info
->selected_controller
== LPC3180_NO_CONTROLLER
)
236 LOG_DEBUG("no LPC3180 NAND flash controller selected, using default 'mlc'");
237 lpc3180_info
->selected_controller
= LPC3180_MLC_CONTROLLER
;
240 if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
242 u32 mlc_icr_value
= 0x0;
244 int twp
, twh
, trp
, treh
, trhz
, trbwb
, tcea
;
246 /* FLASHCLK_CTRL = 0x22 (enable clock for MLC flash controller) */
247 target_write_u32(target
, 0x400040c8, 0x22);
249 /* MLC_CEH = 0x0 (Force nCE assert) */
250 target_write_u32(target
, 0x200b804c, 0x0);
252 /* MLC_LOCK = 0xa25e (unlock protected registers) */
253 target_write_u32(target
, 0x200b8044, 0xa25e);
255 /* MLC_ICR = configuration */
256 if (lpc3180_info
->sw_write_protection
)
257 mlc_icr_value
|= 0x8;
258 if (page_size
== 2048)
259 mlc_icr_value
|= 0x4;
260 if (address_cycles
== 4)
261 mlc_icr_value
|= 0x2;
263 mlc_icr_value
|= 0x1;
264 target_write_u32(target
, 0x200b8030, mlc_icr_value
);
266 /* calculate NAND controller timings */
267 cycle
= lpc3180_cycle_time(lpc3180_info
);
269 twp
= ((40 / cycle
) + 1);
270 twh
= ((20 / cycle
) + 1);
271 trp
= ((30 / cycle
) + 1);
272 treh
= ((15 / cycle
) + 1);
273 trhz
= ((30 / cycle
) + 1);
274 trbwb
= ((100 / cycle
) + 1);
275 tcea
= ((45 / cycle
) + 1);
277 /* MLC_LOCK = 0xa25e (unlock protected registers) */
278 target_write_u32(target
, 0x200b8044, 0xa25e);
281 target_write_u32(target
, 0x200b8034, (twp
& 0xf) | ((twh
& 0xf) << 4) |
282 ((trp
& 0xf) << 8) | ((treh
& 0xf) << 12) | ((trhz
& 0x7) << 16) |
283 ((trbwb
& 0x1f) << 19) | ((tcea
& 0x3) << 24));
285 lpc3180_reset(device
);
287 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
290 int r_setup
, r_hold
, r_width
, r_rdy
;
291 int w_setup
, w_hold
, w_width
, w_rdy
;
293 /* FLASHCLK_CTRL = 0x05 (enable clock for SLC flash controller) */
294 target_write_u32(target
, 0x400040c8, 0x05);
296 /* SLC_CFG = 0x (Force nCE assert, ECC enabled, WIDTH = bus_width) */
297 target_write_u32(target
, 0x20020014, 0x28 | (bus_width
== 16) ? 1 : 0);
299 /* calculate NAND controller timings */
300 cycle
= lpc3180_cycle_time(lpc3180_info
);
302 r_setup
= w_setup
= 0;
303 r_hold
= w_hold
= 10 / cycle
;
304 r_width
= 30 / cycle
;
305 w_width
= 40 / cycle
;
306 r_rdy
= w_rdy
= 100 / cycle
;
308 /* SLC_TAC: SLC timing arcs register */
309 target_write_u32(target
, 0x2002002c, (r_setup
& 0xf) | ((r_hold
& 0xf) << 4) |
310 ((r_width
& 0xf) << 8) | ((r_rdy
& 0xf) << 12) | ((w_setup
& 0xf) << 16) |
311 ((w_hold
& 0xf) << 20) | ((w_width
& 0xf) << 24) | ((w_rdy
& 0xf) << 28));
313 lpc3180_reset(device
);
319 int lpc3180_reset(struct nand_device_s
*device
)
321 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
322 target_t
*target
= lpc3180_info
->target
;
324 if (target
->state
!= TARGET_HALTED
)
326 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
327 return ERROR_NAND_OPERATION_FAILED
;
330 if (lpc3180_info
->selected_controller
== LPC3180_NO_CONTROLLER
)
332 LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
333 return ERROR_NAND_OPERATION_FAILED
;
335 else if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
337 /* MLC_CMD = 0xff (reset controller and NAND device) */
338 target_write_u32(target
, 0x200b8000, 0xff);
340 if (!lpc3180_controller_ready(device
, 100))
342 LOG_ERROR("LPC3180 NAND controller timed out after reset");
343 return ERROR_NAND_OPERATION_TIMEOUT
;
346 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
348 /* SLC_CTRL = 0x6 (ECC_CLEAR, SW_RESET) */
349 target_write_u32(target
, 0x20020010, 0x6);
351 if (!lpc3180_controller_ready(device
, 100))
353 LOG_ERROR("LPC3180 NAND controller timed out after reset");
354 return ERROR_NAND_OPERATION_TIMEOUT
;
361 int lpc3180_command(struct nand_device_s
*device
, u8 command
)
363 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
364 target_t
*target
= lpc3180_info
->target
;
366 if (target
->state
!= TARGET_HALTED
)
368 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
369 return ERROR_NAND_OPERATION_FAILED
;
372 if (lpc3180_info
->selected_controller
== LPC3180_NO_CONTROLLER
)
374 LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
375 return ERROR_NAND_OPERATION_FAILED
;
377 else if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
379 /* MLC_CMD = command */
380 target_write_u32(target
, 0x200b8000, command
);
382 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
384 /* SLC_CMD = command */
385 target_write_u32(target
, 0x20020008, command
);
391 int lpc3180_address(struct nand_device_s
*device
, u8 address
)
393 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
394 target_t
*target
= lpc3180_info
->target
;
396 if (target
->state
!= TARGET_HALTED
)
398 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
399 return ERROR_NAND_OPERATION_FAILED
;
402 if (lpc3180_info
->selected_controller
== LPC3180_NO_CONTROLLER
)
404 LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
405 return ERROR_NAND_OPERATION_FAILED
;
407 else if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
409 /* MLC_ADDR = address */
410 target_write_u32(target
, 0x200b8004, address
);
412 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
414 /* SLC_ADDR = address */
415 target_write_u32(target
, 0x20020004, address
);
421 int lpc3180_write_data(struct nand_device_s
*device
, u16 data
)
423 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
424 target_t
*target
= lpc3180_info
->target
;
426 if (target
->state
!= TARGET_HALTED
)
428 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
429 return ERROR_NAND_OPERATION_FAILED
;
432 if (lpc3180_info
->selected_controller
== LPC3180_NO_CONTROLLER
)
434 LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
435 return ERROR_NAND_OPERATION_FAILED
;
437 else if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
439 /* MLC_DATA = data */
440 target_write_u32(target
, 0x200b0000, data
);
442 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
444 /* SLC_DATA = data */
445 target_write_u32(target
, 0x20020000, data
);
451 int lpc3180_read_data(struct nand_device_s
*device
, void *data
)
453 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
454 target_t
*target
= lpc3180_info
->target
;
456 if (target
->state
!= TARGET_HALTED
)
458 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
459 return ERROR_NAND_OPERATION_FAILED
;
462 if (lpc3180_info
->selected_controller
== LPC3180_NO_CONTROLLER
)
464 LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
465 return ERROR_NAND_OPERATION_FAILED
;
467 else if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
469 /* data = MLC_DATA, use sized access */
470 if (device
->bus_width
== 8)
473 target_read_u8(target
, 0x200b0000, data8
);
475 else if (device
->bus_width
== 16)
478 target_read_u16(target
, 0x200b0000, data16
);
482 LOG_ERROR("BUG: bus_width neither 8 nor 16 bit");
483 return ERROR_NAND_OPERATION_FAILED
;
486 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
490 /* data = SLC_DATA, must use 32-bit access */
491 target_read_u32(target
, 0x20020000, &data32
);
493 if (device
->bus_width
== 8)
496 *data8
= data32
& 0xff;
498 else if (device
->bus_width
== 16)
501 *data16
= data32
& 0xffff;
505 LOG_ERROR("BUG: bus_width neither 8 nor 16 bit");
506 return ERROR_NAND_OPERATION_FAILED
;
513 int lpc3180_write_page(struct nand_device_s
*device
, u32 page
, u8
*data
, u32 data_size
, u8
*oob
, u32 oob_size
)
515 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
516 target_t
*target
= lpc3180_info
->target
;
520 if (target
->state
!= TARGET_HALTED
)
522 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
523 return ERROR_NAND_OPERATION_FAILED
;
526 if (lpc3180_info
->selected_controller
== LPC3180_NO_CONTROLLER
)
528 LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
529 return ERROR_NAND_OPERATION_FAILED
;
531 else if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
535 int quarter
, num_quarters
;
539 LOG_ERROR("LPC3180 MLC controller can't write OOB data only");
540 return ERROR_NAND_OPERATION_NOT_SUPPORTED
;
543 if (oob
&& (oob_size
> 6))
545 LOG_ERROR("LPC3180 MLC controller can't write more than 6 bytes of OOB data");
546 return ERROR_NAND_OPERATION_NOT_SUPPORTED
;
549 if (data_size
> device
->page_size
)
551 LOG_ERROR("data size exceeds page size");
552 return ERROR_NAND_OPERATION_NOT_SUPPORTED
;
555 /* MLC_CMD = sequential input */
556 target_write_u32(target
, 0x200b8000, NAND_CMD_SEQIN
);
558 page_buffer
= malloc(512);
559 oob_buffer
= malloc(6);
561 if (device
->page_size
== 512)
563 /* MLC_ADDR = 0x0 (one column cycle) */
564 target_write_u32(target
, 0x200b8004, 0x0);
567 target_write_u32(target
, 0x200b8004, page
& 0xff);
568 target_write_u32(target
, 0x200b8004, (page
>> 8) & 0xff);
570 if (device
->address_cycles
== 4)
571 target_write_u32(target
, 0x200b8004, (page
>> 16) & 0xff);
575 /* MLC_ADDR = 0x0 (two column cycles) */
576 target_write_u32(target
, 0x200b8004, 0x0);
577 target_write_u32(target
, 0x200b8004, 0x0);
580 target_write_u32(target
, 0x200b8004, page
& 0xff);
581 target_write_u32(target
, 0x200b8004, (page
>> 8) & 0xff);
584 /* when using the MLC controller, we have to treat a large page device
585 * as being made out of four quarters, each the size of a small page device
587 num_quarters
= (device
->page_size
== 2048) ? 4 : 1;
589 for (quarter
= 0; quarter
< num_quarters
; quarter
++)
591 int thisrun_data_size
= (data_size
> 512) ? 512 : data_size
;
592 int thisrun_oob_size
= (oob_size
> 6) ? 6 : oob_size
;
594 memset(page_buffer
, 0xff, 512);
597 memcpy(page_buffer
, data
, thisrun_data_size
);
598 data_size
-= thisrun_data_size
;
599 data
+= thisrun_data_size
;
602 memset(oob_buffer
, 0xff, (device
->page_size
== 512) ? 6 : 24);
605 memcpy(page_buffer
, oob
, thisrun_oob_size
);
606 oob_size
-= thisrun_oob_size
;
607 oob
+= thisrun_oob_size
;
610 /* write MLC_ECC_ENC_REG to start encode cycle */
611 target_write_u32(target
, 0x200b8008, 0x0);
613 target
->type
->write_memory(target
, 0x200a8000, 4, 128, page_buffer
+ (quarter
* 512));
614 target
->type
->write_memory(target
, 0x200a8000, 1, 6, oob_buffer
+ (quarter
* 6));
616 /* write MLC_ECC_AUTO_ENC_REG to start auto encode */
617 target_write_u32(target
, 0x200b8010, 0x0);
619 if (!lpc3180_controller_ready(device
, 1000))
621 LOG_ERROR("timeout while waiting for completion of auto encode cycle");
622 return ERROR_NAND_OPERATION_FAILED
;
626 /* MLC_CMD = auto program command */
627 target_write_u32(target
, 0x200b8000, NAND_CMD_PAGEPROG
);
629 if ((retval
= nand_read_status(device
, &status
)) != ERROR_OK
)
631 LOG_ERROR("couldn't read status");
632 return ERROR_NAND_OPERATION_FAILED
;
635 if (status
& NAND_STATUS_FAIL
)
637 LOG_ERROR("write operation didn't pass, status: 0x%2.2x", status
);
638 return ERROR_NAND_OPERATION_FAILED
;
644 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
646 return nand_write_page_raw(device
, page
, data
, data_size
, oob
, oob_size
);
652 int lpc3180_read_page(struct nand_device_s
*device
, u32 page
, u8
*data
, u32 data_size
, u8
*oob
, u32 oob_size
)
654 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
655 target_t
*target
= lpc3180_info
->target
;
657 if (target
->state
!= TARGET_HALTED
)
659 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
660 return ERROR_NAND_OPERATION_FAILED
;
663 if (lpc3180_info
->selected_controller
== LPC3180_NO_CONTROLLER
)
665 LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
666 return ERROR_NAND_OPERATION_FAILED
;
668 else if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
672 u32 page_bytes_done
= 0;
673 u32 oob_bytes_done
= 0;
677 if (oob
&& (oob_size
> 6))
679 LOG_ERROR("LPC3180 MLC controller can't read more than 6 bytes of OOB data");
680 return ERROR_NAND_OPERATION_NOT_SUPPORTED
;
684 if (data_size
> device
->page_size
)
686 LOG_ERROR("data size exceeds page size");
687 return ERROR_NAND_OPERATION_NOT_SUPPORTED
;
690 if (device
->page_size
== 2048)
692 page_buffer
= malloc(2048);
693 oob_buffer
= malloc(64);
697 page_buffer
= malloc(512);
698 oob_buffer
= malloc(16);
703 /* MLC_CMD = Read OOB
704 * we can use the READOOB command on both small and large page devices,
705 * as the controller translates the 0x50 command to a 0x0 with appropriate
706 * positioning of the serial buffer read pointer
708 target_write_u32(target
, 0x200b8000, NAND_CMD_READOOB
);
712 /* MLC_CMD = Read0 */
713 target_write_u32(target
, 0x200b8000, NAND_CMD_READ0
);
716 if (device
->page_size
== 512)
718 /* small page device */
719 /* MLC_ADDR = 0x0 (one column cycle) */
720 target_write_u32(target
, 0x200b8004, 0x0);
723 target_write_u32(target
, 0x200b8004, page
& 0xff);
724 target_write_u32(target
, 0x200b8004, (page
>> 8) & 0xff);
726 if (device
->address_cycles
== 4)
727 target_write_u32(target
, 0x200b8004, (page
>> 16) & 0xff);
731 /* large page device */
732 /* MLC_ADDR = 0x0 (two column cycles) */
733 target_write_u32(target
, 0x200b8004, 0x0);
734 target_write_u32(target
, 0x200b8004, 0x0);
737 target_write_u32(target
, 0x200b8004, page
& 0xff);
738 target_write_u32(target
, 0x200b8004, (page
>> 8) & 0xff);
740 /* MLC_CMD = Read Start */
741 target_write_u32(target
, 0x200b8000, NAND_CMD_READSTART
);
744 while (page_bytes_done
< device
->page_size
)
746 /* MLC_ECC_AUTO_DEC_REG = dummy */
747 target_write_u32(target
, 0x200b8014, 0xaa55aa55);
749 if (!lpc3180_controller_ready(device
, 1000))
751 LOG_ERROR("timeout while waiting for completion of auto decode cycle");
752 return ERROR_NAND_OPERATION_FAILED
;
755 target_read_u32(target
, 0x200b8048, &mlc_isr
);
761 LOG_ERROR("uncorrectable error detected: 0x%2.2x", mlc_isr
);
762 return ERROR_NAND_OPERATION_FAILED
;
765 LOG_WARNING("%i symbol error detected and corrected", ((mlc_isr
& 0x30) >> 4) + 1);
770 target
->type
->read_memory(target
, 0x200a8000, 4, 128, page_buffer
+ page_bytes_done
);
775 target
->type
->read_memory(target
, 0x200a8000, 4, 4, oob_buffer
+ oob_bytes_done
);
778 page_bytes_done
+= 512;
779 oob_bytes_done
+= 16;
783 memcpy(data
, page_buffer
, data_size
);
786 memcpy(oob
, oob_buffer
, oob_size
);
791 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
793 return nand_read_page_raw(device
, page
, data
, data_size
, oob
, oob_size
);
799 int lpc3180_controller_ready(struct nand_device_s
*device
, int timeout
)
801 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
802 target_t
*target
= lpc3180_info
->target
;
805 if (target
->state
!= TARGET_HALTED
)
807 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
808 return ERROR_NAND_OPERATION_FAILED
;
813 if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
815 /* Read MLC_ISR, wait for controller to become ready */
816 target_read_u8(target
, 0x200b8048, &status
);
821 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
823 /* we pretend that the SLC controller is always ready */
828 } while (timeout
-- > 0);
833 int lpc3180_nand_ready(struct nand_device_s
*device
, int timeout
)
835 lpc3180_nand_controller_t
*lpc3180_info
= device
->controller_priv
;
836 target_t
*target
= lpc3180_info
->target
;
838 if (target
->state
!= TARGET_HALTED
)
840 LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
841 return ERROR_NAND_OPERATION_FAILED
;
846 if (lpc3180_info
->selected_controller
== LPC3180_MLC_CONTROLLER
)
850 /* Read MLC_ISR, wait for NAND flash device to become ready */
851 target_read_u8(target
, 0x200b8048, &status
);
856 else if (lpc3180_info
->selected_controller
== LPC3180_SLC_CONTROLLER
)
860 /* Read SLC_STAT and check READY bit */
861 target_read_u32(target
, 0x20020018, &status
);
868 } while (timeout
-- > 0);
873 int handle_lpc3180_select_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
875 nand_device_t
*device
= NULL
;
876 lpc3180_nand_controller_t
*lpc3180_info
= NULL
;
882 if ((argc
< 1) || (argc
> 2))
884 return ERROR_COMMAND_SYNTAX_ERROR
;
887 device
= get_nand_device_by_num(strtoul(args
[0], NULL
, 0));
890 command_print(cmd_ctx
, "nand device '#%s' is out of bounds", args
[0]);
894 lpc3180_info
= device
->controller_priv
;
898 if (strcmp(args
[1], "mlc") == 0)
900 lpc3180_info
->selected_controller
= LPC3180_MLC_CONTROLLER
;
902 else if (strcmp(args
[1], "slc") == 0)
904 lpc3180_info
->selected_controller
= LPC3180_SLC_CONTROLLER
;
908 return ERROR_COMMAND_SYNTAX_ERROR
;
912 command_print(cmd_ctx
, "%s controller selected", selected
[lpc3180_info
->selected_controller
]);