d49b943407acc0af578a88594e7a49ee480ae265
[fw/openocd] / src / flash / nand / driver.h
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath <Dominic.Rath@gmx.de>              *
3  *   Copyright (C) 2007,2008 Ã˜yvind Harboe <oyvind.harboe@zylin.com>       *
4  *   Copyright (C) 2008 by Spencer Oliver <spen@spen-soft.co.uk>           *
5  *   Copyright (C) 2009 Zachary T Welch <zw@superlucidity.net>             *
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  *   This program is distributed in the hope that it will be useful,       *
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
15  *   GNU General Public License for more details.                          *
16  *                                                                         *
17  *   You should have received a copy of the GNU General Public License     *
18  *   along with this program; if not, write to the                         *
19  *   Free Software Foundation, Inc.,                                       *
20  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
21  ***************************************************************************/
22
23 #ifndef FLASH_NAND_DRIVER_H
24 #define FLASH_NAND_DRIVER_H
25
26 struct nand_device;
27
28 #define __NAND_DEVICE_COMMAND(name) \
29         COMMAND_HELPER(name, struct nand_device *nand)
30
31 /**
32  * Interface for NAND flash controllers.  Not all of these functions are
33  * required for full functionality of the NAND driver, but better performance
34  * can be achieved by implementing each function.
35  */
36 struct nand_flash_controller {
37         /** Driver name that is used to select it from configuration files. */
38         const char *name;
39
40         /** Usage of flash command registration. */
41         const char *usage;
42
43         const struct command_registration *commands;
44
45         /** NAND device command called when driver is instantiated during configuration. */
46         __NAND_DEVICE_COMMAND((*nand_device_command));
47
48         /** Initialize the NAND device. */
49         int (*init)(struct nand_device *nand);
50
51         /** Reset the NAND device. */
52         int (*reset)(struct nand_device *nand);
53
54         /** Issue a command to the NAND device. */
55         int (*command)(struct nand_device *nand, uint8_t command);
56
57         /** Write an address to the NAND device. */
58         int (*address)(struct nand_device *nand, uint8_t address);
59
60         /** Write word of data to the NAND device. */
61         int (*write_data)(struct nand_device *nand, uint16_t data);
62
63         /** Read word of data from the NAND device. */
64         int (*read_data)(struct nand_device *nand, void *data);
65
66         /** Write a block of data to the NAND device. */
67         int (*write_block_data)(struct nand_device *nand, uint8_t *data, int size);
68
69         /** Read a block of data from the NAND device. */
70         int (*read_block_data)(struct nand_device *nand, uint8_t *data, int size);
71
72         /** Write a page to the NAND device. */
73         int (*write_page)(struct nand_device *nand, uint32_t page, uint8_t *data,
74                           uint32_t data_size, uint8_t *oob, uint32_t oob_size);
75
76         /** Read a page from the NAND device. */
77         int (*read_page)(struct nand_device *nand, uint32_t page, uint8_t *data, uint32_t data_size,
78                          uint8_t *oob, uint32_t oob_size);
79
80         /** Check if the NAND device is ready for more instructions with timeout. */
81         int (*nand_ready)(struct nand_device *nand, int timeout);
82 };
83
84 #define NAND_DEVICE_COMMAND_HANDLER(name) static __NAND_DEVICE_COMMAND(name)
85
86 /**
87  * Find a NAND flash controller by name.
88  * @param name Identifies the NAND controller to find.
89  * @returns The nand_flash_controller named @c name, or NULL if not found.
90  */
91 struct nand_flash_controller *nand_driver_find_by_name(const char *name);
92
93 /** Signature for callback functions passed to nand_driver_walk */
94 typedef int (*nand_driver_walker_t)(struct nand_flash_controller *c, void *);
95 /**
96  * Walk the list of drivers, encapsulating the data structure type.
97  * Application state/context can be passed through the @c x pointer.
98  * @param f The callback function to invoke for each function.
99  * @param x For use as private data storate, passed directly to @c f.
100  * @returns ERROR_OK if successful, or the non-zero return value of @c f.
101  * This allows a walker to terminate the loop early.
102  */
103 int nand_driver_walk(nand_driver_walker_t f, void *x);
104
105 #endif  /* FLASH_NAND_DRIVER_H */