Change return value on error.
[openocd.git] / src / flash / nand / orion.c
1 /***************************************************************************
2 * Copyright (C) 2009 by Marvell Semiconductors, Inc. *
3 * Written by Nicolas Pitre <nico at marvell.com> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 /*
22 * NAND controller interface for Marvell Orion/Kirkwood SoCs.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "imp.h"
30 #include "arm_io.h"
31 #include <target/arm.h>
32
33
34 struct orion_nand_controller
35 {
36 struct arm_nand_data io;
37
38 uint32_t cmd;
39 uint32_t addr;
40 uint32_t data;
41 };
42
43 #define CHECK_HALTED \
44 do { \
45 if (target->state != TARGET_HALTED) { \
46 LOG_ERROR("NAND flash access requires halted target"); \
47 return ERROR_NAND_OPERATION_FAILED; \
48 } \
49 } while (0)
50
51 static int orion_nand_command(struct nand_device *nand, uint8_t command)
52 {
53 struct orion_nand_controller *hw = nand->controller_priv;
54 struct target *target = nand->target;
55
56 CHECK_HALTED;
57 target_write_u8(target, hw->cmd, command);
58 return ERROR_OK;
59 }
60
61 static int orion_nand_address(struct nand_device *nand, uint8_t address)
62 {
63 struct orion_nand_controller *hw = nand->controller_priv;
64 struct target *target = nand->target;
65
66 CHECK_HALTED;
67 target_write_u8(target, hw->addr, address);
68 return ERROR_OK;
69 }
70
71 static int orion_nand_read(struct nand_device *nand, void *data)
72 {
73 struct orion_nand_controller *hw = nand->controller_priv;
74 struct target *target = nand->target;
75
76 CHECK_HALTED;
77 target_read_u8(target, hw->data, data);
78 return ERROR_OK;
79 }
80
81 static int orion_nand_write(struct nand_device *nand, uint16_t data)
82 {
83 struct orion_nand_controller *hw = nand->controller_priv;
84 struct target *target = nand->target;
85
86 CHECK_HALTED;
87 target_write_u8(target, hw->data, data);
88 return ERROR_OK;
89 }
90
91 static int orion_nand_slow_block_write(struct nand_device *nand, uint8_t *data, int size)
92 {
93 while (size--)
94 orion_nand_write(nand, *data++);
95 return ERROR_OK;
96 }
97
98 static int orion_nand_fast_block_write(struct nand_device *nand, uint8_t *data, int size)
99 {
100 struct orion_nand_controller *hw = nand->controller_priv;
101 int retval;
102
103 hw->io.chunk_size = nand->page_size;
104
105 retval = arm_nandwrite(&hw->io, data, size);
106 if (retval == ERROR_NAND_NO_BUFFER)
107 retval = orion_nand_slow_block_write(nand, data, size);
108
109 return retval;
110 }
111
112 static int orion_nand_reset(struct nand_device *nand)
113 {
114 return orion_nand_command(nand, NAND_CMD_RESET);
115 }
116
117 NAND_DEVICE_COMMAND_HANDLER(orion_nand_device_command)
118 {
119 struct orion_nand_controller *hw;
120 uint32_t base;
121 uint8_t ale, cle;
122
123 if (CMD_ARGC != 3) {
124 return ERROR_COMMAND_SYNTAX_ERROR;
125 }
126
127 hw = calloc(1, sizeof(*hw));
128 if (!hw) {
129 LOG_ERROR("no memory for nand controller");
130 return ERROR_NAND_DEVICE_INVALID;
131 }
132
133 nand->controller_priv = hw;
134
135 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], base);
136 cle = 0;
137 ale = 1;
138
139 hw->data = base;
140 hw->cmd = base + (1 << cle);
141 hw->addr = base + (1 << ale);
142
143 hw->io.target = nand->target;
144 hw->io.data = hw->data;
145 hw->io.op = ARM_NAND_NONE;
146
147 return ERROR_OK;
148 }
149
150 static int orion_nand_init(struct nand_device *nand)
151 {
152 return ERROR_OK;
153 }
154
155 struct nand_flash_controller orion_nand_controller =
156 {
157 .name = "orion",
158 .usage = "<target_id> <NAND_address>",
159 .command = orion_nand_command,
160 .address = orion_nand_address,
161 .read_data = orion_nand_read,
162 .write_data = orion_nand_write,
163 .write_block_data = orion_nand_fast_block_write,
164 .reset = orion_nand_reset,
165 .nand_device_command = orion_nand_device_command,
166 .init = orion_nand_init,
167 };
168

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)