OpenOCD
lpc3180.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2007 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  *
7  * Copyright (C) 2010 richard vegh <vegh.ricsi@gmail.com> *
8  * Copyright (C) 2010 Oyvind Harboe <oyvind.harboe@zylin.com> *
9  ***************************************************************************/
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 
15 #include "imp.h"
16 #include "lpc3180.h"
17 #include <target/target.h>
18 
19 static int lpc3180_reset(struct nand_device *nand);
20 static int lpc3180_controller_ready(struct nand_device *nand, int timeout);
21 static int lpc3180_tc_ready(struct nand_device *nand, int timeout);
22 
23 #define ECC_OFFS 0x120
24 #define SPARE_OFFS 0x140
25 #define DATA_OFFS 0x200
26 
27 /* nand device lpc3180 <target#> <oscillator_frequency>
28  */
29 NAND_DEVICE_COMMAND_HANDLER(lpc3180_nand_device_command)
30 {
31  if (CMD_ARGC < 3)
33 
34  uint32_t osc_freq;
35  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], osc_freq);
36 
37  struct lpc3180_nand_controller *lpc3180_info;
38  lpc3180_info = malloc(sizeof(struct lpc3180_nand_controller));
39  nand->controller_priv = lpc3180_info;
40 
41  lpc3180_info->osc_freq = osc_freq;
42 
43  if ((lpc3180_info->osc_freq < 1000) || (lpc3180_info->osc_freq > 20000))
45  "LPC3180 oscillator frequency should be between 1000 and 20000 kHz, was %i",
46  lpc3180_info->osc_freq);
48  lpc3180_info->sw_write_protection = 0;
49  lpc3180_info->sw_wp_lower_bound = 0x0;
50  lpc3180_info->sw_wp_upper_bound = 0x0;
51 
52  return ERROR_OK;
53 }
54 
55 static int lpc3180_pll(int fclkin, uint32_t pll_ctrl)
56 {
57  int bypass = (pll_ctrl & 0x8000) >> 15;
58  int direct = (pll_ctrl & 0x4000) >> 14;
59  int feedback = (pll_ctrl & 0x2000) >> 13;
60  int p = (1 << ((pll_ctrl & 0x1800) >> 11) * 2);
61  int n = ((pll_ctrl & 0x0600) >> 9) + 1;
62  int m = ((pll_ctrl & 0x01fe) >> 1) + 1;
63  int lock = (pll_ctrl & 0x1);
64 
65  if (!lock)
66  LOG_WARNING("PLL is not locked");
67 
68  if (!bypass && direct) /* direct mode */
69  return (m * fclkin) / n;
70 
71  if (bypass && !direct) /* bypass mode */
72  return fclkin / (2 * p);
73 
74  if (bypass & direct) /* direct bypass mode */
75  return fclkin;
76 
77  if (feedback) /* integer mode */
78  return m * (fclkin / n);
79  else /* non-integer mode */
80  return (m / (2 * p)) * (fclkin / n);
81 }
82 
83 static float lpc3180_cycle_time(struct nand_device *nand)
84 {
85  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
86  struct target *target = nand->target;
87  uint32_t sysclk_ctrl, pwr_ctrl, hclkdiv_ctrl, hclkpll_ctrl;
88  int sysclk;
89  int hclk;
90  int hclk_pll;
91 
92  /* calculate timings */
93 
94  /* determine current SYSCLK (13'MHz or main oscillator) */
95  target_read_u32(target, 0x40004050, &sysclk_ctrl);
96 
97  if ((sysclk_ctrl & 1) == 0)
98  sysclk = lpc3180_info->osc_freq;
99  else
100  sysclk = 13000;
101 
102  /* determine selected HCLK source */
103  target_read_u32(target, 0x40004044, &pwr_ctrl);
104 
105  if ((pwr_ctrl & (1 << 2)) == 0) /* DIRECT RUN mode */
106  hclk = sysclk;
107  else {
108  target_read_u32(target, 0x40004058, &hclkpll_ctrl);
109  hclk_pll = lpc3180_pll(sysclk, hclkpll_ctrl);
110 
111  target_read_u32(target, 0x40004040, &hclkdiv_ctrl);
112 
113  if (pwr_ctrl & (1 << 10)) /* ARM_CLK and HCLK use PERIPH_CLK */
114  hclk = hclk_pll / (((hclkdiv_ctrl & 0x7c) >> 2) + 1);
115  else /* HCLK uses HCLK_PLL */
116  hclk = hclk_pll / (1 << (hclkdiv_ctrl & 0x3));
117  }
118 
119  LOG_DEBUG("LPC3180 HCLK currently clocked at %i kHz", hclk);
120 
121  return (1.0 / hclk) * 1000000.0;
122 }
123 
124 static int lpc3180_init(struct nand_device *nand)
125 {
126  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
127  struct target *target = nand->target;
128  int bus_width = nand->bus_width ? nand->bus_width : 8;
129  int address_cycles = nand->address_cycles ? nand->address_cycles : 3;
130  int page_size = nand->page_size ? nand->page_size : 512;
131 
132  if (target->state != TARGET_HALTED) {
133  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
135  }
136 
137  /* sanitize arguments */
138  if ((bus_width != 8) && (bus_width != 16)) {
139  LOG_ERROR("LPC3180 only supports 8 or 16 bit bus width, not %i", bus_width);
141  }
142 
143  /* The LPC3180 only brings out 8 bit NAND data bus, but the controller
144  * would support 16 bit, too, so we just warn about this for now
145  */
146  if (bus_width == 16)
147  LOG_WARNING("LPC3180 only supports 8 bit bus width");
148 
149  /* inform calling code about selected bus width */
150  nand->bus_width = bus_width;
151 
152  if ((address_cycles != 3) && (address_cycles != 4)) {
153  LOG_ERROR("LPC3180 only supports 3 or 4 address cycles, not %i", address_cycles);
155  }
156 
157  if ((page_size != 512) && (page_size != 2048)) {
158  LOG_ERROR("LPC3180 only supports 512 or 2048 byte pages, not %i", page_size);
160  }
161 
162  /* select MLC controller if none is currently selected */
163  if (lpc3180_info->selected_controller == LPC3180_NO_CONTROLLER) {
164  LOG_DEBUG("no LPC3180 NAND flash controller selected, using default 'mlc'");
166  }
167 
168  if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
169  uint32_t mlc_icr_value = 0x0;
170  float cycle;
171  int twp, twh, trp, treh, trhz, trbwb, tcea;
172 
173  /* FLASHCLK_CTRL = 0x22 (enable clock for MLC flash controller) */
174  target_write_u32(target, 0x400040c8, 0x22);
175 
176  /* MLC_CEH = 0x0 (Force nCE assert) */
177  target_write_u32(target, 0x200b804c, 0x0);
178 
179  /* MLC_LOCK = 0xa25e (unlock protected registers) */
180  target_write_u32(target, 0x200b8044, 0xa25e);
181 
182  /* MLC_ICR = configuration */
183  if (lpc3180_info->sw_write_protection)
184  mlc_icr_value |= 0x8;
185  if (page_size == 2048)
186  mlc_icr_value |= 0x4;
187  if (address_cycles == 4)
188  mlc_icr_value |= 0x2;
189  if (bus_width == 16)
190  mlc_icr_value |= 0x1;
191  target_write_u32(target, 0x200b8030, mlc_icr_value);
192 
193  /* calculate NAND controller timings */
194  cycle = lpc3180_cycle_time(nand);
195 
196  twp = ((40 / cycle) + 1);
197  twh = ((20 / cycle) + 1);
198  trp = ((30 / cycle) + 1);
199  treh = ((15 / cycle) + 1);
200  trhz = ((30 / cycle) + 1);
201  trbwb = ((100 / cycle) + 1);
202  tcea = ((45 / cycle) + 1);
203 
204  /* MLC_LOCK = 0xa25e (unlock protected registers) */
205  target_write_u32(target, 0x200b8044, 0xa25e);
206 
207  /* MLC_TIME_REG */
208  target_write_u32(target, 0x200b8034, (twp & 0xf) | ((twh & 0xf) << 4) |
209  ((trp & 0xf) << 8) | ((treh & 0xf) << 12) | ((trhz & 0x7) << 16) |
210  ((trbwb & 0x1f) << 19) | ((tcea & 0x3) << 24));
211 
212  lpc3180_reset(nand);
213  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
214  float cycle;
215  int r_setup, r_hold, r_width, r_rdy;
216  int w_setup, w_hold, w_width, w_rdy;
217 
218  /* FLASHCLK_CTRL = 0x05 (enable clock for SLC flash controller) */
219  target_write_u32(target, 0x400040c8, 0x05);
220 
221  /* after reset set other registers of SLC so reset calling is here at the beginning */
222  lpc3180_reset(nand);
223 
224  /* SLC_CFG = 0x (Force nCE assert, DMA ECC enabled, ECC enabled, DMA burst enabled,
225  *DMA read from SLC, WIDTH = bus_width) */
226  target_write_u32(target, 0x20020014, 0x3e | ((bus_width == 16) ? 1 : 0));
227 
228  /* SLC_IEN = 3 (INT_RDY_EN = 1) ,(INT_TC_STAT = 1) */
229  target_write_u32(target, 0x20020020, 0x03);
230 
231  /* DMA configuration
232  * DMACLK_CTRL = 0x01 (enable clock for DMA controller) */
233  target_write_u32(target, 0x400040e8, 0x01);
234  /* DMACConfig = DMA enabled*/
235  target_write_u32(target, 0x31000030, 0x01);
236 
237 
238  /* calculate NAND controller timings */
239  cycle = lpc3180_cycle_time(nand);
240 
241  r_setup = w_setup = 0;
242  r_hold = w_hold = 10 / cycle;
243  r_width = 30 / cycle;
244  w_width = 40 / cycle;
245  r_rdy = w_rdy = 100 / cycle;
246 
247  /* SLC_TAC: SLC timing arcs register */
248  target_write_u32(target, 0x2002002c, (r_setup & 0xf) | ((r_hold & 0xf) << 4) |
249  ((r_width & 0xf) << 8) | ((r_rdy & 0xf) << 12) | ((w_setup & 0xf) << 16) |
250  ((w_hold & 0xf) << 20) | ((w_width & 0xf) << 24) | ((w_rdy & 0xf) << 28));
251 
252  }
253 
254  return ERROR_OK;
255 }
256 
257 static int lpc3180_reset(struct nand_device *nand)
258 {
259  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
260  struct target *target = nand->target;
261 
262  if (target->state != TARGET_HALTED) {
263  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
265  }
266 
267  if (lpc3180_info->selected_controller == LPC3180_NO_CONTROLLER) {
268  LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
270  } else if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
271  /* MLC_CMD = 0xff (reset controller and NAND device) */
272  target_write_u32(target, 0x200b8000, 0xff);
273 
274  if (!lpc3180_controller_ready(nand, 100)) {
275  LOG_ERROR("LPC3180 NAND controller timed out after reset");
277  }
278  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
279  /* SLC_CTRL = 0x6 (ECC_CLEAR, SW_RESET) */
280  target_write_u32(target, 0x20020010, 0x6);
281 
282  if (!lpc3180_controller_ready(nand, 100)) {
283  LOG_ERROR("LPC3180 NAND controller timed out after reset");
285  }
286  }
287 
288  return ERROR_OK;
289 }
290 
291 static int lpc3180_command(struct nand_device *nand, uint8_t command)
292 {
293  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
294  struct target *target = nand->target;
295 
296  if (target->state != TARGET_HALTED) {
297  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
299  }
300 
301  if (lpc3180_info->selected_controller == LPC3180_NO_CONTROLLER) {
302  LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
304  } else if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
305  /* MLC_CMD = command */
306  target_write_u32(target, 0x200b8000, command);
307  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
308  /* SLC_CMD = command */
309  target_write_u32(target, 0x20020008, command);
310  }
311 
312  return ERROR_OK;
313 }
314 
315 static int lpc3180_address(struct nand_device *nand, uint8_t address)
316 {
317  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
318  struct target *target = nand->target;
319 
320  if (target->state != TARGET_HALTED) {
321  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
323  }
324 
325  if (lpc3180_info->selected_controller == LPC3180_NO_CONTROLLER) {
326  LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
328  } else if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
329  /* MLC_ADDR = address */
330  target_write_u32(target, 0x200b8004, address);
331  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
332  /* SLC_ADDR = address */
333  target_write_u32(target, 0x20020004, address);
334  }
335 
336  return ERROR_OK;
337 }
338 
339 static int lpc3180_write_data(struct nand_device *nand, uint16_t data)
340 {
341  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
342  struct target *target = nand->target;
343 
344  if (target->state != TARGET_HALTED) {
345  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
347  }
348 
349  if (lpc3180_info->selected_controller == LPC3180_NO_CONTROLLER) {
350  LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
352  } else if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
353  /* MLC_DATA = data */
354  target_write_u32(target, 0x200b0000, data);
355  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
356  /* SLC_DATA = data */
357  target_write_u32(target, 0x20020000, data);
358  }
359 
360  return ERROR_OK;
361 }
362 
363 static int lpc3180_read_data(struct nand_device *nand, void *data)
364 {
365  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
366  struct target *target = nand->target;
367 
368  if (target->state != TARGET_HALTED) {
369  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
371  }
372 
373  if (lpc3180_info->selected_controller == LPC3180_NO_CONTROLLER) {
374  LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
376  } else if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
377  /* data = MLC_DATA, use sized access */
378  if (nand->bus_width == 8) {
379  uint8_t *data8 = data;
380  target_read_u8(target, 0x200b0000, data8);
381  } else if (nand->bus_width == 16) {
382  uint16_t *data16 = data;
383  target_read_u16(target, 0x200b0000, data16);
384  } else {
385  LOG_ERROR("BUG: bus_width neither 8 nor 16 bit");
387  }
388  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
389  uint32_t data32;
390 
391  /* data = SLC_DATA, must use 32-bit access */
392  target_read_u32(target, 0x20020000, &data32);
393 
394  if (nand->bus_width == 8) {
395  uint8_t *data8 = data;
396  *data8 = data32 & 0xff;
397  } else if (nand->bus_width == 16) {
398  uint16_t *data16 = data;
399  *data16 = data32 & 0xffff;
400  } else {
401  LOG_ERROR("BUG: bus_width neither 8 nor 16 bit");
403  }
404  }
405 
406  return ERROR_OK;
407 }
408 
409 static int lpc3180_write_page(struct nand_device *nand,
410  uint32_t page,
411  uint8_t *data,
412  uint32_t data_size,
413  uint8_t *oob,
414  uint32_t oob_size)
415 {
416  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
417  struct target *target = nand->target;
418  int retval;
419  uint8_t status;
420  uint8_t *page_buffer;
421 
422  if (target->state != TARGET_HALTED) {
423  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
425  }
426 
427  if (lpc3180_info->selected_controller == LPC3180_NO_CONTROLLER) {
428  LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
430  } else if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
431  uint8_t *oob_buffer;
432  int quarter, num_quarters;
433 
434  if (!data && oob) {
435  LOG_ERROR("LPC3180 MLC controller can't write OOB data only");
437  }
438 
439  if (oob && (oob_size > 24)) {
440  LOG_ERROR("LPC3180 MLC controller can't write more "
441  "than 6 bytes for each quarter's OOB data");
443  }
444 
445  if (data_size > (uint32_t)nand->page_size) {
446  LOG_ERROR("data size exceeds page size");
448  }
449 
450  /* MLC_CMD = sequential input */
451  target_write_u32(target, 0x200b8000, NAND_CMD_SEQIN);
452 
453  page_buffer = malloc(512);
454  oob_buffer = malloc(6);
455 
456  if (nand->page_size == 512) {
457  /* MLC_ADDR = 0x0 (one column cycle) */
458  target_write_u32(target, 0x200b8004, 0x0);
459 
460  /* MLC_ADDR = row */
461  target_write_u32(target, 0x200b8004, page & 0xff);
462  target_write_u32(target, 0x200b8004, (page >> 8) & 0xff);
463 
464  if (nand->address_cycles == 4)
465  target_write_u32(target, 0x200b8004, (page >> 16) & 0xff);
466  } else {
467  /* MLC_ADDR = 0x0 (two column cycles) */
468  target_write_u32(target, 0x200b8004, 0x0);
469  target_write_u32(target, 0x200b8004, 0x0);
470 
471  /* MLC_ADDR = row */
472  target_write_u32(target, 0x200b8004, page & 0xff);
473  target_write_u32(target, 0x200b8004, (page >> 8) & 0xff);
474  }
475 
476  /* when using the MLC controller, we have to treat a large page device
477  * as being made out of four quarters, each the size of a small page device
478  */
479  num_quarters = (nand->page_size == 2048) ? 4 : 1;
480 
481  for (quarter = 0; quarter < num_quarters; quarter++) {
482  int thisrun_data_size = (data_size > 512) ? 512 : data_size;
483  int thisrun_oob_size = (oob_size > 6) ? 6 : oob_size;
484 
485  memset(page_buffer, 0xff, 512);
486  if (data) {
487  memcpy(page_buffer, data, thisrun_data_size);
488  data_size -= thisrun_data_size;
489  data += thisrun_data_size;
490  }
491 
492  memset(oob_buffer, 0xff, 6);
493  if (oob) {
494  memcpy(oob_buffer, oob, thisrun_oob_size);
495  oob_size -= thisrun_oob_size;
496  oob += thisrun_oob_size;
497  }
498 
499  /* write MLC_ECC_ENC_REG to start encode cycle */
500  target_write_u32(target, 0x200b8008, 0x0);
501 
502  target_write_memory(target, 0x200a8000,
503  4, 128, page_buffer);
504  target_write_memory(target, 0x200a8000,
505  1, 6, oob_buffer);
506 
507  /* write MLC_ECC_AUTO_ENC_REG to start auto encode */
508  target_write_u32(target, 0x200b8010, 0x0);
509 
510  if (!lpc3180_controller_ready(nand, 1000)) {
511  LOG_ERROR("timeout while waiting for completion of auto encode cycle");
512  free(page_buffer);
513  free(oob_buffer);
515  }
516  }
517 
518  /* MLC_CMD = auto program command */
520 
521  retval = nand_read_status(nand, &status);
522  if (retval != ERROR_OK) {
523  LOG_ERROR("couldn't read status");
524  free(page_buffer);
525  free(oob_buffer);
527  }
528 
529  if (status & NAND_STATUS_FAIL) {
530  LOG_ERROR("write operation didn't pass, status: 0x%2.2x", status);
531  free(page_buffer);
532  free(oob_buffer);
534  }
535 
536  free(page_buffer);
537  free(oob_buffer);
538  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
539 
540  /**********************************************************************
541  * Write both SLC NAND flash page main area and spare area.
542  * Small page -
543  * ------------------------------------------
544  * | 512 bytes main | 16 bytes spare |
545  * ------------------------------------------
546  * Large page -
547  * ------------------------------------------
548  * | 2048 bytes main | 64 bytes spare |
549  * ------------------------------------------
550  * If DMA & ECC enabled, then the ECC generated for the 1st 256-byte
551  * data is written to the 3rd word of the spare area. The ECC
552  * generated for the 2nd 256-byte data is written to the 4th word
553  * of the spare area. The ECC generated for the 3rd 256-byte data is
554  * written to the 7th word of the spare area. The ECC generated
555  * for the 4th 256-byte data is written to the 8th word of the
556  * spare area and so on.
557  *
558  **********************************************************************/
559 
560  int i = 0, target_mem_base;
561  uint8_t *ecc_flash_buffer;
562  struct working_area *pworking_area;
563 
564  if (lpc3180_info->is_bulk) {
565 
566  if (!data && oob) {
567  /*if oob only mode is active original method is used as SLC
568  *controller hangs during DMA interworking. Anyway the code supports
569  *the oob only mode below. */
570  return nand_write_page_raw(nand,
571  page,
572  data,
573  data_size,
574  oob,
575  oob_size);
576  }
577  retval = nand_page_command(nand, page, NAND_CMD_SEQIN, !data);
578  if (retval != ERROR_OK)
579  return retval;
580 
581  /* allocate a working area */
582  if (target->working_area_size < (uint32_t) nand->page_size + 0x200) {
583  LOG_ERROR("Reserve at least 0x%x physical target working area",
584  nand->page_size + 0x200);
586  }
587  if (target->working_area_phys%4) {
588  LOG_ERROR(
589  "Reserve the physical target working area at word boundary");
591  }
593  &pworking_area) != ERROR_OK) {
594  LOG_ERROR("no working area specified, can't read LPC internal flash");
596  }
597  target_mem_base = target->working_area_phys;
598 
599  if (nand->page_size == 2048)
600  page_buffer = malloc(2048);
601  else
602  page_buffer = malloc(512);
603 
604  ecc_flash_buffer = malloc(64);
605 
606  /* SLC_CFG = 0x (Force nCE assert, DMA ECC enabled, ECC enabled, DMA burst
607  *enabled, DMA write to SLC, WIDTH = bus_width) */
608  target_write_u32(target, 0x20020014, 0x3c);
609 
610  if (data && !oob) {
611  /* set DMA LLI-s in target memory and in DMA*/
612  for (i = 0; i < nand->page_size/0x100; i++) {
613 
614  int tmp;
615  /* -------LLI for 256 byte block---------
616  * DMACC0SrcAddr = SRAM */
618  target_mem_base+0+i*32,
619  target_mem_base+DATA_OFFS+i*256);
620  if (i == 0)
622  0x31000100,
623  target_mem_base+DATA_OFFS);
624  /* DMACCxDestAddr = SLC_DMA_DATA */
625  target_write_u32(target, target_mem_base+4+i*32, 0x20020038);
626  if (i == 0)
627  target_write_u32(target, 0x31000104, 0x20020038);
628  /* DMACCxLLI = next element */
629  tmp = (target_mem_base+(1+i*2)*16)&0xfffffffc;
630  target_write_u32(target, target_mem_base+8+i*32, tmp);
631  if (i == 0)
632  target_write_u32(target, 0x31000108, tmp);
633  /* DMACCxControl = TransferSize =64, Source burst size =16,
634  * Destination burst size = 16, Source transfer width = 32 bit,
635  * Destination transfer width = 32 bit, Source AHB master select = M0,
636  * Destination AHB master select = M0, Source increment = 1,
637  * Destination increment = 0, Terminal count interrupt enable bit = 0*/
639  target_mem_base+12+i*32,
640  0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
641  0<<27 | 0<<31);
642  if (i == 0)
644  0x3100010c,
645  0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
646  0<<27 | 0<<31);
647 
648  /* -------LLI for 3 byte ECC---------
649  * DMACC0SrcAddr = SLC_ECC*/
650  target_write_u32(target, target_mem_base+16+i*32, 0x20020034);
651  /* DMACCxDestAddr = SRAM */
653  target_mem_base+20+i*32,
654  target_mem_base+SPARE_OFFS+8+16*(i>>1)+(i%2)*4);
655  /* DMACCxLLI = next element */
656  tmp = (target_mem_base+(2+i*2)*16)&0xfffffffc;
657  target_write_u32(target, target_mem_base+24+i*32, tmp);
658  /* DMACCxControl = TransferSize =1, Source burst size =4,
659  * Destination burst size = 4, Source transfer width = 32 bit,
660  * Destination transfer width = 32 bit, Source AHB master select = M0,
661  * Destination AHB master select = M0, Source increment = 0,
662  * Destination increment = 1, Terminal count interrupt enable bit = 0*/
664  target_mem_base+28+i*32,
665  0x01 | 1<<12 | 1<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
666  31);
667  }
668  } else if (data && oob) {
669  /* -------LLI for 512 or 2048 bytes page---------
670  * DMACC0SrcAddr = SRAM */
671  target_write_u32(target, target_mem_base, target_mem_base+DATA_OFFS);
672  target_write_u32(target, 0x31000100, target_mem_base+DATA_OFFS);
673  /* DMACCxDestAddr = SLC_DMA_DATA */
674  target_write_u32(target, target_mem_base+4, 0x20020038);
675  target_write_u32(target, 0x31000104, 0x20020038);
676  /* DMACCxLLI = next element */
678  target_mem_base+8,
679  (target_mem_base+32)&0xfffffffc);
680  target_write_u32(target, 0x31000108,
681  (target_mem_base+32)&0xfffffffc);
682  /* DMACCxControl = TransferSize =512 or 128, Source burst size =16,
683  * Destination burst size = 16, Source transfer width = 32 bit,
684  * Destination transfer width = 32 bit, Source AHB master select = M0,
685  * Destination AHB master select = M0, Source increment = 1,
686  * Destination increment = 0, Terminal count interrupt enable bit = 0*/
688  target_mem_base+12,
689  (nand->page_size ==
690  2048 ? 512 : 128) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 |
691  1<<26 | 0<<27 | 0<<31);
693  0x3100010c,
694  (nand->page_size ==
695  2048 ? 512 : 128) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 |
696  1<<26 | 0<<27 | 0<<31);
697  i = 1;
698  } else if (!data && oob)
699  i = 0;
700 
701  /* -------LLI for spare area---------
702  * DMACC0SrcAddr = SRAM*/
703  target_write_u32(target, target_mem_base+0+i*32, target_mem_base+SPARE_OFFS);
704  if (i == 0)
705  target_write_u32(target, 0x31000100, target_mem_base+SPARE_OFFS);
706  /* DMACCxDestAddr = SLC_DMA_DATA */
707  target_write_u32(target, target_mem_base+4+i*32, 0x20020038);
708  if (i == 0)
709  target_write_u32(target, 0x31000104, 0x20020038);
710  /* DMACCxLLI = next element = NULL */
711  target_write_u32(target, target_mem_base+8+i*32, 0);
712  if (i == 0)
713  target_write_u32(target, 0x31000108, 0);
714  /* DMACCxControl = TransferSize =16 for large page or 4 for small page,
715  * Source burst size =16, Destination burst size = 16, Source transfer width = 32 bit,
716  * Destination transfer width = 32 bit, Source AHB master select = M0,
717  * Destination AHB master select = M0, Source increment = 1,
718  * Destination increment = 0, Terminal count interrupt enable bit = 0*/
720  target_mem_base+12+i*32,
721  (nand->page_size ==
722  2048 ? 0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 1<<26 |
723  0<<27 | 0<<31);
724  if (i == 0)
725  target_write_u32(target, 0x3100010c,
726  (nand->page_size == 2048 ?
727  0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 |
728  0<<25 | 1<<26 | 0<<27 | 0<<31);
729 
730  memset(ecc_flash_buffer, 0xff, 64);
731  if (oob)
732  memcpy(ecc_flash_buffer, oob, oob_size);
734  target_mem_base+SPARE_OFFS,
735  4,
736  16,
737  ecc_flash_buffer);
738 
739  if (data) {
740  memset(page_buffer, 0xff, nand->page_size == 2048 ? 2048 : 512);
741  memcpy(page_buffer, data, data_size);
743  target_mem_base+DATA_OFFS,
744  4,
745  nand->page_size == 2048 ? 512 : 128,
746  page_buffer);
747  }
748 
749  free(page_buffer);
750  free(ecc_flash_buffer);
751 
752  /* Enable DMA after channel set up !
753  LLI only works when DMA is the flow controller!
754  */
755  /* DMACCxConfig= E=1, SrcPeripheral = 1 (SLC), DestPeripheral = 1 (SLC),
756  *FlowCntrl = 2 (Pher -> Mem, DMA), IE = 0, ITC = 0, L= 0, H=0*/
758  0x31000110,
759  1 | 1<<1 | 1<<6 | 2<<11 | 0<<14 | 0<<15 | 0<<16 | 0<<18);
760 
761  /* SLC_CTRL = 3 (START DMA), ECC_CLEAR */
762  target_write_u32(target, 0x20020010, 0x3);
763 
764  /* SLC_ICR = 2, INT_TC_CLR, clear pending TC*/
765  target_write_u32(target, 0x20020028, 2);
766 
767  /* SLC_TC */
768  if (!data && oob)
769  target_write_u32(target, 0x20020030,
770  (nand->page_size == 2048 ? 0x10 : 0x04));
771  else
772  target_write_u32(target, 0x20020030,
773  (nand->page_size == 2048 ? 0x840 : 0x210));
774 
775  nand_write_finish(nand);
776 
777  if (!lpc3180_tc_ready(nand, 1000)) {
778  LOG_ERROR("timeout while waiting for completion of DMA");
780  }
781 
782  target_free_working_area(target, pworking_area);
783 
784  LOG_INFO("Page = 0x%" PRIx32 " was written.", page);
785 
786  } else
787  return nand_write_page_raw(nand, page, data, data_size, oob, oob_size);
788  }
789 
790  return ERROR_OK;
791 }
792 
793 static int lpc3180_read_page(struct nand_device *nand,
794  uint32_t page,
795  uint8_t *data,
796  uint32_t data_size,
797  uint8_t *oob,
798  uint32_t oob_size)
799 {
800  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
801  struct target *target = nand->target;
802  uint8_t *page_buffer;
803 
804  if (target->state != TARGET_HALTED) {
805  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
807  }
808 
809  if (lpc3180_info->selected_controller == LPC3180_NO_CONTROLLER) {
810  LOG_ERROR("BUG: no LPC3180 NAND flash controller selected");
812  } else if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
813  uint8_t *oob_buffer;
814  uint32_t page_bytes_done = 0;
815  uint32_t oob_bytes_done = 0;
816  uint32_t mlc_isr;
817 
818 #if 0
819  if (oob && (oob_size > 6)) {
820  LOG_ERROR("LPC3180 MLC controller can't read more than 6 bytes of OOB data");
822  }
823 #endif
824 
825  if (data_size > (uint32_t)nand->page_size) {
826  LOG_ERROR("data size exceeds page size");
828  }
829 
830  if (nand->page_size == 2048) {
831  page_buffer = malloc(2048);
832  oob_buffer = malloc(64);
833  } else {
834  page_buffer = malloc(512);
835  oob_buffer = malloc(16);
836  }
837 
838  if (!data && oob) {
839  /* MLC_CMD = Read OOB
840  * we can use the READOOB command on both small and large page devices,
841  * as the controller translates the 0x50 command to a 0x0 with appropriate
842  * positioning of the serial buffer read pointer
843  */
845  } else {
846  /* MLC_CMD = Read0 */
847  target_write_u32(target, 0x200b8000, NAND_CMD_READ0);
848  }
849 
850  if (nand->page_size == 512) {
851  /* small page device
852  * MLC_ADDR = 0x0 (one column cycle) */
853  target_write_u32(target, 0x200b8004, 0x0);
854 
855  /* MLC_ADDR = row */
856  target_write_u32(target, 0x200b8004, page & 0xff);
857  target_write_u32(target, 0x200b8004, (page >> 8) & 0xff);
858 
859  if (nand->address_cycles == 4)
860  target_write_u32(target, 0x200b8004, (page >> 16) & 0xff);
861  } else {
862  /* large page device
863  * MLC_ADDR = 0x0 (two column cycles) */
864  target_write_u32(target, 0x200b8004, 0x0);
865  target_write_u32(target, 0x200b8004, 0x0);
866 
867  /* MLC_ADDR = row */
868  target_write_u32(target, 0x200b8004, page & 0xff);
869  target_write_u32(target, 0x200b8004, (page >> 8) & 0xff);
870 
871  /* MLC_CMD = Read Start */
873  }
874 
875  while (page_bytes_done < (uint32_t)nand->page_size) {
876  /* MLC_ECC_AUTO_DEC_REG = dummy */
877  target_write_u32(target, 0x200b8014, 0xaa55aa55);
878 
879  if (!lpc3180_controller_ready(nand, 1000)) {
880  LOG_ERROR("timeout while waiting for completion of auto decode cycle");
881  free(page_buffer);
882  free(oob_buffer);
884  }
885 
886  target_read_u32(target, 0x200b8048, &mlc_isr);
887 
888  if (mlc_isr & 0x8) {
889  if (mlc_isr & 0x40) {
890  LOG_ERROR("uncorrectable error detected: 0x%2.2" PRIx32, mlc_isr);
891  free(page_buffer);
892  free(oob_buffer);
894  }
895 
896  LOG_WARNING("%i symbol error detected and corrected",
897  ((int)(((mlc_isr & 0x30) >> 4) + 1)));
898  }
899 
900  if (data)
902  0x200a8000,
903  4,
904  128,
905  page_buffer + page_bytes_done);
906 
907  if (oob)
909  0x200a8000,
910  4,
911  4,
912  oob_buffer + oob_bytes_done);
913 
914  page_bytes_done += 512;
915  oob_bytes_done += 16;
916  }
917 
918  if (data)
919  memcpy(data, page_buffer, data_size);
920 
921  if (oob)
922  memcpy(oob, oob_buffer, oob_size);
923 
924  free(page_buffer);
925  free(oob_buffer);
926  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
927 
928  /**********************************************************************
929  * Read both SLC NAND flash page main area and spare area.
930  * Small page -
931  * ------------------------------------------
932  * | 512 bytes main | 16 bytes spare |
933  * ------------------------------------------
934  * Large page -
935  * ------------------------------------------
936  * | 2048 bytes main | 64 bytes spare |
937  * ------------------------------------------
938  * If DMA & ECC enabled, then the ECC generated for the 1st 256-byte
939  * data is compared with the 3rd word of the spare area. The ECC
940  * generated for the 2nd 256-byte data is compared with the 4th word
941  * of the spare area. The ECC generated for the 3rd 256-byte data is
942  * compared with the 7th word of the spare area. The ECC generated
943  * for the 4th 256-byte data is compared with the 8th word of the
944  * spare area and so on.
945  *
946  **********************************************************************/
947 
948  int retval, i, target_mem_base;
949  uint8_t *ecc_hw_buffer;
950  uint8_t *ecc_flash_buffer;
951  struct working_area *pworking_area;
952 
953  if (lpc3180_info->is_bulk) {
954 
955  /* read always the data and also oob areas*/
956 
957  retval = nand_page_command(nand, page, NAND_CMD_READ0, 0);
958  if (retval != ERROR_OK)
959  return retval;
960 
961  /* allocate a working area */
962  if (target->working_area_size < (uint32_t) nand->page_size + 0x200) {
963  LOG_ERROR("Reserve at least 0x%x physical target working area",
964  nand->page_size + 0x200);
966  }
967  if (target->working_area_phys%4) {
968  LOG_ERROR(
969  "Reserve the physical target working area at word boundary");
971  }
973  &pworking_area) != ERROR_OK) {
974  LOG_ERROR("no working area specified, can't read LPC internal flash");
976  }
977  target_mem_base = target->working_area_phys;
978 
979  if (nand->page_size == 2048)
980  page_buffer = malloc(2048);
981  else
982  page_buffer = malloc(512);
983 
984  ecc_hw_buffer = malloc(32);
985  ecc_flash_buffer = malloc(64);
986 
987  /* SLC_CFG = 0x (Force nCE assert, DMA ECC enabled, ECC enabled, DMA burst
988  *enabled, DMA read from SLC, WIDTH = bus_width) */
989  target_write_u32(target, 0x20020014, 0x3e);
990 
991  /* set DMA LLI-s in target memory and in DMA*/
992  for (i = 0; i < nand->page_size/0x100; i++) {
993  int tmp;
994  /* -------LLI for 256 byte block---------
995  * DMACC0SrcAddr = SLC_DMA_DATA*/
996  target_write_u32(target, target_mem_base+0+i*32, 0x20020038);
997  if (i == 0)
998  target_write_u32(target, 0x31000100, 0x20020038);
999  /* DMACCxDestAddr = SRAM */
1001  target_mem_base+4+i*32,
1002  target_mem_base+DATA_OFFS+i*256);
1003  if (i == 0)
1005  0x31000104,
1006  target_mem_base+DATA_OFFS);
1007  /* DMACCxLLI = next element */
1008  tmp = (target_mem_base+(1+i*2)*16)&0xfffffffc;
1009  target_write_u32(target, target_mem_base+8+i*32, tmp);
1010  if (i == 0)
1011  target_write_u32(target, 0x31000108, tmp);
1012  /* DMACCxControl = TransferSize =64, Source burst size =16,
1013  * Destination burst size = 16, Source transfer width = 32 bit,
1014  * Destination transfer width = 32 bit, Source AHB master select = M0,
1015  * Destination AHB master select = M0, Source increment = 0,
1016  * Destination increment = 1, Terminal count interrupt enable bit = 0*/
1018  target_mem_base+12+i*32,
1019  0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1020  31);
1021  if (i == 0)
1023  0x3100010c,
1024  0x40 | 3<<12 | 3<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1025  31);
1026 
1027  /* -------LLI for 3 byte ECC---------
1028  * DMACC0SrcAddr = SLC_ECC*/
1029  target_write_u32(target, target_mem_base+16+i*32, 0x20020034);
1030  /* DMACCxDestAddr = SRAM */
1032  target_mem_base+20+i*32,
1033  target_mem_base+ECC_OFFS+i*4);
1034  /* DMACCxLLI = next element */
1035  tmp = (target_mem_base+(2+i*2)*16)&0xfffffffc;
1036  target_write_u32(target, target_mem_base+24+i*32, tmp);
1037  /* DMACCxControl = TransferSize =1, Source burst size =4,
1038  * Destination burst size = 4, Source transfer width = 32 bit,
1039  * Destination transfer width = 32 bit, Source AHB master select = M0,
1040  * Destination AHB master select = M0, Source increment = 0,
1041  * Destination increment = 1, Terminal count interrupt enable bit = 0*/
1043  target_mem_base+28+i*32,
1044  0x01 | 1<<12 | 1<<15 | 2<<18 | 2<<21 | 0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<
1045  31);
1046  }
1047 
1048  /* -------LLI for spare area---------
1049  * DMACC0SrcAddr = SLC_DMA_DATA*/
1050  target_write_u32(target, target_mem_base+0+i*32, 0x20020038);
1051  /* DMACCxDestAddr = SRAM */
1052  target_write_u32(target, target_mem_base+4+i*32, target_mem_base+SPARE_OFFS);
1053  /* DMACCxLLI = next element = NULL */
1054  target_write_u32(target, target_mem_base+8+i*32, 0);
1055  /* DMACCxControl = TransferSize =16 for large page or 4 for small page,
1056  * Source burst size =16, Destination burst size = 16, Source transfer width = 32 bit,
1057  * Destination transfer width = 32 bit, Source AHB master select = M0,
1058  * Destination AHB master select = M0, Source increment = 0,
1059  * Destination increment = 1, Terminal count interrupt enable bit = 0*/
1061  target_mem_base + 12 + i * 32,
1062  (nand->page_size == 2048 ? 0x10 : 0x04) | 3<<12 | 3<<15 | 2<<18 | 2<<21 |
1063  0<<24 | 0<<25 | 0<<26 | 1<<27 | 0<<31);
1064 
1065  /* Enable DMA after channel set up !
1066  LLI only works when DMA is the flow controller!
1067  */
1068  /* DMACCxConfig= E=1, SrcPeripheral = 1 (SLC), DestPeripheral = 1 (SLC),
1069  *FlowCntrl = 2 (Pher-> Mem, DMA), IE = 0, ITC = 0, L= 0, H=0*/
1071  0x31000110,
1072  1 | 1<<1 | 1<<6 | 2<<11 | 0<<14 | 0<<15 | 0<<16 | 0<<18);
1073 
1074  /* SLC_CTRL = 3 (START DMA), ECC_CLEAR */
1075  target_write_u32(target, 0x20020010, 0x3);
1076 
1077  /* SLC_ICR = 2, INT_TC_CLR, clear pending TC*/
1078  target_write_u32(target, 0x20020028, 2);
1079 
1080  /* SLC_TC */
1081  target_write_u32(target, 0x20020030,
1082  (nand->page_size == 2048 ? 0x840 : 0x210));
1083 
1084  if (!lpc3180_tc_ready(nand, 1000)) {
1085  LOG_ERROR("timeout while waiting for completion of DMA");
1086  free(page_buffer);
1087  free(ecc_hw_buffer);
1088  free(ecc_flash_buffer);
1089  target_free_working_area(target, pworking_area);
1091  }
1092 
1093  if (data) {
1095  target_mem_base+DATA_OFFS,
1096  4,
1097  nand->page_size == 2048 ? 512 : 128,
1098  page_buffer);
1099  memcpy(data, page_buffer, data_size);
1100 
1101  LOG_INFO("Page = 0x%" PRIx32 " was read.", page);
1102 
1103  /* check hw generated ECC for each 256 bytes block with the saved
1104  *ECC in flash spare area*/
1105  int idx = nand->page_size/0x200;
1107  target_mem_base+SPARE_OFFS,
1108  4,
1109  16,
1110  ecc_flash_buffer);
1112  target_mem_base+ECC_OFFS,
1113  4,
1114  8,
1115  ecc_hw_buffer);
1116  for (i = 0; i < idx; i++) {
1117  if ((0x00ffffff & *(uint32_t *)(void *)(ecc_hw_buffer+i*8)) !=
1118  (0x00ffffff & *(uint32_t *)(void *)(ecc_flash_buffer+8+i*16)))
1119  LOG_WARNING(
1120  "ECC mismatch at 256 bytes size block= %d at page= 0x%" PRIx32,
1121  i * 2 + 1, page);
1122  if ((0x00ffffff & *(uint32_t *)(void *)(ecc_hw_buffer+4+i*8)) !=
1123  (0x00ffffff & *(uint32_t *)(void *)(ecc_flash_buffer+12+i*16)))
1124  LOG_WARNING(
1125  "ECC mismatch at 256 bytes size block= %d at page= 0x%" PRIx32,
1126  i * 2 + 2, page);
1127  }
1128  }
1129 
1130  if (oob)
1131  memcpy(oob, ecc_flash_buffer, oob_size);
1132 
1133  free(page_buffer);
1134  free(ecc_hw_buffer);
1135  free(ecc_flash_buffer);
1136 
1137  target_free_working_area(target, pworking_area);
1138 
1139  } else
1140  return nand_read_page_raw(nand, page, data, data_size, oob, oob_size);
1141  }
1142 
1143  return ERROR_OK;
1144 }
1145 
1146 static int lpc3180_controller_ready(struct nand_device *nand, int timeout)
1147 {
1148  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
1149  struct target *target = nand->target;
1150 
1151  if (target->state != TARGET_HALTED) {
1152  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
1154  }
1155 
1156  LOG_DEBUG("lpc3180_controller_ready count start=%d", timeout);
1157 
1158  do {
1159  if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
1160  uint8_t status;
1161 
1162  /* Read MLC_ISR, wait for controller to become ready */
1163  target_read_u8(target, 0x200b8048, &status);
1164 
1165  if (status & 2) {
1166  LOG_DEBUG("lpc3180_controller_ready count=%d",
1167  timeout);
1168  return 1;
1169  }
1170  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
1171  uint32_t status;
1172 
1173  /* Read SLC_STAT and check READY bit */
1174  target_read_u32(target, 0x20020018, &status);
1175 
1176  if (status & 1) {
1177  LOG_DEBUG("lpc3180_controller_ready count=%d",
1178  timeout);
1179  return 1;
1180  }
1181  }
1182 
1183  alive_sleep(1);
1184  } while (timeout-- > 0);
1185 
1186  return 0;
1187 }
1188 
1189 static int lpc3180_nand_ready(struct nand_device *nand, int timeout)
1190 {
1191  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
1192  struct target *target = nand->target;
1193 
1194  if (target->state != TARGET_HALTED) {
1195  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
1197  }
1198 
1199  LOG_DEBUG("lpc3180_nand_ready count start=%d", timeout);
1200 
1201  do {
1202  if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER) {
1203  uint8_t status = 0x0;
1204 
1205  /* Read MLC_ISR, wait for NAND flash device to become ready */
1206  target_read_u8(target, 0x200b8048, &status);
1207 
1208  if (status & 1) {
1209  LOG_DEBUG("lpc3180_nand_ready count end=%d",
1210  timeout);
1211  return 1;
1212  }
1213  } else if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
1214  uint32_t status = 0x0;
1215 
1216  /* Read SLC_STAT and check READY bit */
1217  target_read_u32(target, 0x20020018, &status);
1218 
1219  if (status & 1) {
1220  LOG_DEBUG("lpc3180_nand_ready count end=%d",
1221  timeout);
1222  return 1;
1223  }
1224  }
1225 
1226  alive_sleep(1);
1227  } while (timeout-- > 0);
1228 
1229  return 0;
1230 }
1231 
1232 static int lpc3180_tc_ready(struct nand_device *nand, int timeout)
1233 {
1234  struct lpc3180_nand_controller *lpc3180_info = nand->controller_priv;
1235  struct target *target = nand->target;
1236 
1237  if (target->state != TARGET_HALTED) {
1238  LOG_ERROR("target must be halted to use LPC3180 NAND flash controller");
1240  }
1241 
1242  LOG_DEBUG("lpc3180_tc_ready count start=%d",
1243  timeout);
1244 
1245  do {
1246  if (lpc3180_info->selected_controller == LPC3180_SLC_CONTROLLER) {
1247  uint32_t status = 0x0;
1248  /* Read SLC_INT_STAT and check INT_TC_STAT bit */
1249  target_read_u32(target, 0x2002001c, &status);
1250 
1251  if (status & 2) {
1252  LOG_DEBUG("lpc3180_tc_ready count=%d",
1253  timeout);
1254  return 1;
1255  }
1256  }
1257 
1258  alive_sleep(1);
1259  } while (timeout-- > 0);
1260 
1261  return 0;
1262 }
1263 
1264 COMMAND_HANDLER(handle_lpc3180_select_command)
1265 {
1266  struct lpc3180_nand_controller *lpc3180_info = NULL;
1267  char *selected[] = {
1268  "no", "mlc", "slc"
1269  };
1270 
1271  if ((CMD_ARGC < 1) || (CMD_ARGC > 3))
1273 
1274  unsigned int num;
1275  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], num);
1276  struct nand_device *nand = get_nand_device_by_num(num);
1277  if (!nand) {
1278  command_print(CMD, "nand device '#%s' is out of bounds", CMD_ARGV[0]);
1279  return ERROR_OK;
1280  }
1281 
1282  lpc3180_info = nand->controller_priv;
1283 
1284  if (CMD_ARGC >= 2) {
1285  if (strcmp(CMD_ARGV[1], "mlc") == 0)
1287  else if (strcmp(CMD_ARGV[1], "slc") == 0) {
1289  if (CMD_ARGC == 3 && strcmp(CMD_ARGV[2], "bulk") == 0)
1290  lpc3180_info->is_bulk = 1;
1291  else
1292  lpc3180_info->is_bulk = 0;
1293  } else
1295  }
1296 
1297  if (lpc3180_info->selected_controller == LPC3180_MLC_CONTROLLER)
1298  command_print(CMD, "%s controller selected",
1299  selected[lpc3180_info->selected_controller]);
1300  else
1302  lpc3180_info->is_bulk ? "%s controller selected bulk mode is available" :
1303  "%s controller selected bulk mode is not available",
1304  selected[lpc3180_info->selected_controller]);
1305 
1306  return ERROR_OK;
1307 }
1308 
1309 static const struct command_registration lpc3180_exec_command_handlers[] = {
1310  {
1311  .name = "select",
1312  .handler = handle_lpc3180_select_command,
1313  .mode = COMMAND_EXEC,
1314  .help =
1315  "select MLC or SLC controller (default is MLC), SLC can be set to bulk mode",
1316  .usage = "bank_id ['mlc'|'slc' ['bulk'] ]",
1317  },
1319 };
1320 static const struct command_registration lpc3180_command_handler[] = {
1321  {
1322  .name = "lpc3180",
1323  .mode = COMMAND_ANY,
1324  .help = "LPC3180 NAND flash controller commands",
1325  .usage = "",
1327  },
1329 };
1330 
1332  .name = "lpc3180",
1333  .commands = lpc3180_command_handler,
1334  .nand_device_command = lpc3180_nand_device_command,
1335  .init = lpc3180_init,
1336  .reset = lpc3180_reset,
1337  .command = lpc3180_command,
1338  .address = lpc3180_address,
1339  .write_data = lpc3180_write_data,
1340  .read_data = lpc3180_read_data,
1341  .write_page = lpc3180_write_page,
1342  .read_page = lpc3180_read_page,
1343  .nand_ready = lpc3180_nand_ready,
1344 };
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint32_t page_size
Page size.
Definition: dw-spi-helper.h:3
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
int nand_read_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
int nand_read_status(struct nand_device *nand, uint8_t *status)
int nand_write_finish(struct nand_device *nand)
int nand_write_page_raw(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
struct nand_device * get_nand_device_by_num(int num)
int nand_page_command(struct nand_device *nand, uint32_t page, uint8_t cmd, bool oob_only)
void alive_sleep(uint64_t ms)
Definition: log.c:478
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#define DATA_OFFS
Definition: lpc3180.c:25
static int lpc3180_init(struct nand_device *nand)
Definition: lpc3180.c:124
static int lpc3180_controller_ready(struct nand_device *nand, int timeout)
Definition: lpc3180.c:1146
static int lpc3180_pll(int fclkin, uint32_t pll_ctrl)
Definition: lpc3180.c:55
#define ECC_OFFS
Definition: lpc3180.c:23
static int lpc3180_read_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
Definition: lpc3180.c:793
COMMAND_HANDLER(handle_lpc3180_select_command)
Definition: lpc3180.c:1264
static const struct command_registration lpc3180_exec_command_handlers[]
Definition: lpc3180.c:1309
static int lpc3180_read_data(struct nand_device *nand, void *data)
Definition: lpc3180.c:363
static int lpc3180_command(struct nand_device *nand, uint8_t command)
Definition: lpc3180.c:291
static float lpc3180_cycle_time(struct nand_device *nand)
Definition: lpc3180.c:83
static int lpc3180_write_page(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size, uint8_t *oob, uint32_t oob_size)
Definition: lpc3180.c:409
static int lpc3180_write_data(struct nand_device *nand, uint16_t data)
Definition: lpc3180.c:339
static int lpc3180_tc_ready(struct nand_device *nand, int timeout)
Definition: lpc3180.c:1232
static int lpc3180_address(struct nand_device *nand, uint8_t address)
Definition: lpc3180.c:315
static int lpc3180_reset(struct nand_device *nand)
Definition: lpc3180.c:257
static int lpc3180_nand_ready(struct nand_device *nand, int timeout)
Definition: lpc3180.c:1189
#define SPARE_OFFS
Definition: lpc3180.c:24
NAND_DEVICE_COMMAND_HANDLER(lpc3180_nand_device_command)
Definition: lpc3180.c:29
static const struct command_registration lpc3180_command_handler[]
Definition: lpc3180.c:1320
@ LPC3180_MLC_CONTROLLER
Definition: lpc3180.h:13
@ LPC3180_SLC_CONTROLLER
Definition: lpc3180.h:14
@ LPC3180_NO_CONTROLLER
Definition: lpc3180.h:12
#define ERROR_NAND_OPERATION_TIMEOUT
Definition: nand/core.h:218
#define ERROR_NAND_OPERATION_FAILED
Definition: nand/core.h:217
@ NAND_CMD_SEQIN
Definition: nand/core.h:148
@ NAND_CMD_READSTART
Definition: nand/core.h:155
@ NAND_CMD_READOOB
Definition: nand/core.h:144
@ NAND_CMD_READ0
Definition: nand/core.h:140
@ NAND_CMD_PAGEPROG
Definition: nand/core.h:143
@ NAND_STATUS_FAIL
Definition: nand/core.h:162
#define ERROR_NAND_OPERATION_NOT_SUPPORTED
Definition: nand/core.h:219
const char * name
Definition: command.h:239
uint32_t sw_wp_upper_bound
Definition: lpc3180.h:23
enum lpc3180_selected_controller selected_controller
Definition: lpc3180.h:19
uint32_t sw_wp_lower_bound
Definition: lpc3180.h:22
void * controller_priv
Definition: nand/core.h:51
int page_size
Definition: nand/core.h:56
int address_cycles
Definition: nand/core.h:55
int bus_width
Definition: nand/core.h:54
struct target * target
Definition: nand/core.h:49
Interface for NAND flash controllers.
Definition: nand/driver.h:23
Definition: target.h:119
uint32_t working_area_size
Definition: target.h:161
enum target_state state
Definition: target.h:167
target_addr_t working_area_phys
Definition: target.h:160
Definition: psoc6.c:83
bool free
Definition: target.h:91
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
Definition: target.c:2601
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1288
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2090
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2635
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2148
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2581
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2561
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1260
@ TARGET_HALTED
Definition: target.h:58
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17