f2311ccaa04f5eded707182cf17464a7ad4c4440
[fw/openocd] / src / flash / nand / s3c24xx.c
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 /***************************************************************************
4  *   Copyright (C) 2007, 2008 by Ben Dooks                                 *
5  *   ben@fluff.org                                                         *
6  ***************************************************************************/
7
8 /*
9  * S3C24XX Series OpenOCD NAND Flash controller support.
10  *
11  * Many thanks to Simtec Electronics for sponsoring this work.
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18 #include "s3c24xx.h"
19
20 S3C24XX_DEVICE_COMMAND()
21 {
22         *info = NULL;
23
24         struct s3c24xx_nand_controller *s3c24xx_info;
25         s3c24xx_info = malloc(sizeof(struct s3c24xx_nand_controller));
26         if (!s3c24xx_info) {
27                 LOG_ERROR("no memory for nand controller");
28                 return -ENOMEM;
29         }
30
31         nand->controller_priv = s3c24xx_info;
32         *info = s3c24xx_info;
33
34         return ERROR_OK;
35 }
36
37 int s3c24xx_reset(struct nand_device *nand)
38 {
39         struct s3c24xx_nand_controller *s3c24xx_info = nand->controller_priv;
40         struct target *target = nand->target;
41
42         if (target->state != TARGET_HALTED) {
43                 LOG_ERROR("target must be halted to use S3C24XX NAND flash controller");
44                 return ERROR_NAND_OPERATION_FAILED;
45         }
46
47         target_write_u32(target, s3c24xx_info->cmd, 0xff);
48
49         return ERROR_OK;
50 }
51
52 int s3c24xx_command(struct nand_device *nand, uint8_t command)
53 {
54         struct s3c24xx_nand_controller *s3c24xx_info = nand->controller_priv;
55         struct target *target = nand->target;
56
57         if (target->state != TARGET_HALTED) {
58                 LOG_ERROR("target must be halted to use S3C24XX NAND flash controller");
59                 return ERROR_NAND_OPERATION_FAILED;
60         }
61
62         target_write_u16(target, s3c24xx_info->cmd, command);
63         return ERROR_OK;
64 }
65
66 int s3c24xx_address(struct nand_device *nand, uint8_t address)
67 {
68         struct s3c24xx_nand_controller *s3c24xx_info = nand->controller_priv;
69         struct target *target = nand->target;
70
71         if (target->state != TARGET_HALTED) {
72                 LOG_ERROR("target must be halted to use S3C24XX NAND flash controller");
73                 return ERROR_NAND_OPERATION_FAILED;
74         }
75
76         target_write_u16(target, s3c24xx_info->addr, address);
77         return ERROR_OK;
78 }
79
80 int s3c24xx_write_data(struct nand_device *nand, uint16_t data)
81 {
82         struct s3c24xx_nand_controller *s3c24xx_info = nand->controller_priv;
83         struct target *target = nand->target;
84
85         if (target->state != TARGET_HALTED) {
86                 LOG_ERROR("target must be halted to use S3C24XX NAND flash controller");
87                 return ERROR_NAND_OPERATION_FAILED;
88         }
89
90         target_write_u8(target, s3c24xx_info->data, data);
91         return ERROR_OK;
92 }
93
94 int s3c24xx_read_data(struct nand_device *nand, void *data)
95 {
96         struct s3c24xx_nand_controller *s3c24xx_info = nand->controller_priv;
97         struct target *target = nand->target;
98
99         if (target->state != TARGET_HALTED) {
100                 LOG_ERROR("target must be halted to use S3C24XX NAND flash controller");
101                 return ERROR_NAND_OPERATION_FAILED;
102         }
103
104         target_read_u8(target, s3c24xx_info->data, data);
105         return ERROR_OK;
106 }