]> git.sur5r.net Git - openocd/blob - src/flash/nand/orion.c
NAND/ORION: remove private "target" copy
[openocd] / 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                 LOG_ERROR("arguments must be: <target_id> <NAND_address>\n");
125                 return ERROR_NAND_DEVICE_INVALID;
126         }
127
128         hw = calloc(1, sizeof(*hw));
129         if (!hw) {
130                 LOG_ERROR("no memory for nand controller\n");
131                 return ERROR_NAND_DEVICE_INVALID;
132         }
133
134         nand->controller_priv = hw;
135
136         COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], base);
137         cle = 0;
138         ale = 1;
139
140         hw->data = base;
141         hw->cmd = base + (1 << cle);
142         hw->addr = base + (1 << ale);
143
144         hw->io.target = nand->target;
145         hw->io.data = hw->data;
146         hw->io.op = ARM_NAND_NONE;
147
148         return ERROR_OK;
149 }
150
151 static int orion_nand_init(struct nand_device *nand)
152 {
153         return ERROR_OK;
154 }
155
156 struct nand_flash_controller orion_nand_controller =
157 {
158         .name                   = "orion",
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