1 /***************************************************************************
2 * Copyright (C) 2009 by Marvell Semiconductors, Inc. *
3 * Written by Nicolas Pitre <nico at marvell.com> *
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 ***************************************************************************/
22 * NAND controller interface for Marvell Orion/Kirkwood SoCs.
29 #include <flash/arm_nandio.h>
30 #include <target/armv4_5.h>
33 struct orion_nand_controller
35 struct target
*target
;
37 struct arm_nand_data io
;
44 #define CHECK_HALTED \
46 if (target->state != TARGET_HALTED) { \
47 LOG_ERROR("NAND flash access requires halted target"); \
48 return ERROR_NAND_OPERATION_FAILED; \
52 static int orion_nand_command(struct nand_device
*nand
, uint8_t command
)
54 struct orion_nand_controller
*hw
= nand
->controller_priv
;
55 struct target
*target
= hw
->target
;
58 target_write_u8(target
, hw
->cmd
, command
);
62 static int orion_nand_address(struct nand_device
*nand
, uint8_t address
)
64 struct orion_nand_controller
*hw
= nand
->controller_priv
;
65 struct target
*target
= hw
->target
;
68 target_write_u8(target
, hw
->addr
, address
);
72 static int orion_nand_read(struct nand_device
*nand
, void *data
)
74 struct orion_nand_controller
*hw
= nand
->controller_priv
;
75 struct target
*target
= hw
->target
;
78 target_read_u8(target
, hw
->data
, data
);
82 static int orion_nand_write(struct nand_device
*nand
, uint16_t data
)
84 struct orion_nand_controller
*hw
= nand
->controller_priv
;
85 struct target
*target
= hw
->target
;
88 target_write_u8(target
, hw
->data
, data
);
92 static int orion_nand_slow_block_write(struct nand_device
*nand
, uint8_t *data
, int size
)
95 orion_nand_write(nand
, *data
++);
99 static int orion_nand_fast_block_write(struct nand_device
*nand
, uint8_t *data
, int size
)
101 struct orion_nand_controller
*hw
= nand
->controller_priv
;
104 hw
->io
.chunk_size
= nand
->page_size
;
106 retval
= arm_nandwrite(&hw
->io
, data
, size
);
107 if (retval
== ERROR_NAND_NO_BUFFER
)
108 retval
= orion_nand_slow_block_write(nand
, data
, size
);
113 static int orion_nand_reset(struct nand_device
*nand
)
115 return orion_nand_command(nand
, NAND_CMD_RESET
);
118 static int orion_nand_controller_ready(struct nand_device
*nand
, int timeout
)
123 NAND_DEVICE_COMMAND_HANDLER(orion_nand_device_command
)
125 struct orion_nand_controller
*hw
;
130 LOG_ERROR("arguments must be: <target_id> <NAND_address>\n");
131 return ERROR_NAND_DEVICE_INVALID
;
134 hw
= calloc(1, sizeof(*hw
));
136 LOG_ERROR("no memory for nand controller\n");
137 return ERROR_NAND_DEVICE_INVALID
;
140 nand
->controller_priv
= hw
;
141 hw
->target
= get_target(CMD_ARGV
[1]);
143 LOG_ERROR("target '%s' not defined", CMD_ARGV
[1]);
145 return ERROR_NAND_DEVICE_INVALID
;
148 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[2], base
);
153 hw
->cmd
= base
+ (1 << cle
);
154 hw
->addr
= base
+ (1 << ale
);
156 hw
->io
.target
= hw
->target
;
157 hw
->io
.data
= hw
->data
;
162 static int orion_nand_init(struct nand_device
*nand
)
167 struct nand_flash_controller orion_nand_controller
=
170 .command
= orion_nand_command
,
171 .address
= orion_nand_address
,
172 .read_data
= orion_nand_read
,
173 .write_data
= orion_nand_write
,
174 .write_block_data
= orion_nand_fast_block_write
,
175 .reset
= orion_nand_reset
,
176 .controller_ready
= orion_nand_controller_ready
,
177 .nand_device_command
= orion_nand_device_command
,
178 .init
= orion_nand_init
,