summaryrefslogtreecommitdiff
path: root/drivers/mtd/nand/ccx9x_nand.c
blob: af2ace9b99b40b900f7c808f043cbd1828850344 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * drivers/mtd/nand/ccx9x_nand.c
 *
 * Copyright (C) 2008 by Digi International Inc.
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * Based on drivers/mtd/nand/ccx9x_nand.c by Markus Pietrek
 */

#include <linux/io.h>
#include <linux/module.h>
#include <linux/mtd/ccx9x_nand.h>
#include <linux/mtd/nand.h>
#include <linux/mtd/partitions.h>
#include <linux/platform_device.h>

#include <mach/gpio.h>

#define DRIVER_NAME "ccx9x_nand"
#define DEVICE_NAME "onboard_boot"

#define mi2ncd(mi) container_of(mi, struct nand_ccx9x, mtd)

#ifdef CONFIG_MTD_PARTITIONS
static const char *probes[] = { "cmdlinepart", NULL };
#endif

struct nand_ccx9x {
	struct mtd_info mtd;
	struct nand_chip chip;

	struct resource *mem;
	struct ccx9x_nand_info *nand_info;
};

/* read in gpio ready pin */
static int ccx9x_nand_dev_ready(struct mtd_info *mtd)
{
	struct nand_ccx9x *ncd = mi2ncd(mtd);

	return gpio_get_value(ncd->nand_info->busy_pin);
}

static void ccx9x_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
		unsigned int ctrl)
{
	struct nand_ccx9x *ncd = mi2ncd(mtd);

	if (cmd == NAND_CMD_NONE)
		return;

	if (ctrl & NAND_CLE)
		writeb(cmd, ncd->chip.IO_ADDR_W + ncd->nand_info->cmd_offset);
	else
		writeb(cmd, ncd->chip.IO_ADDR_W + ncd->nand_info->addr_offset);
}

static void ccx9x_nand_read_buf(struct mtd_info *mtd,
		u_char *buf, int len)
{
	struct nand_ccx9x *ncd = mi2ncd(mtd);

	while (len) {
		*buf = ioread8(ncd->chip.IO_ADDR_R);
		buf++;
		len--;
	}
}

static void ccx9x_nand_write_buf(struct mtd_info *mtd,
		const u_char *buf, int len)
{
	struct nand_ccx9x *ncd = mi2ncd(mtd);

	while (len) {
		iowrite8(*buf, ncd->chip.IO_ADDR_W);
		buf++;
		len--;
	}

	/* FIXME: 32bit burst writing */
}

static int ccx9x_nand_verify_buf(struct mtd_info *mtd,
		const u_char *buf, int len)
{
	static u_char *tmp;
	int i, ret = 0;

	tmp = kmalloc(len, GFP_KERNEL);
	if (!tmp)
		return -ENOMEM;

	/* read fromd device */
	ccx9x_nand_read_buf(mtd, tmp, len);

	/* compare */
	for (i = 0; i < len; i++)
		if (tmp[i] != buf[i]) {
			ret = -EFAULT;
			break;
		}

	kfree(tmp);
	return ret;

}

static int ccx9x_nand_probe(struct platform_device *pdev)
{
	struct nand_ccx9x *ncd;
	struct resource *mem;
	struct mtd_partition *mtd_parts;
	int num_parts;
	int ret;

	ncd = kzalloc(sizeof(*ncd), GFP_KERNEL);
	if (!ncd) {
		ret = -ENOMEM;
		dev_dbg(&pdev->dev, "%s: err_alloc\n", __func__);
		goto err_alloc;
	}

	ncd->nand_info = pdev->dev.platform_data;
	if (!ncd->nand_info) {
		ret = -ENODEV;
		dev_dbg(&pdev->dev, "%s: err_pdata\n", __func__);
		goto err_pdata;
	}

	platform_set_drvdata(pdev, ncd);

	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!mem) {
		ret = -ENODEV;
		dev_dbg(&pdev->dev, "%s: err_get_mem\n", __func__);
		goto err_get_mem;
	}

	if (!request_mem_region(mem->start, mem->end - mem->start,
				DRIVER_NAME)) {
		ret = -EBUSY;
		dev_dbg(&pdev->dev, "%s: err_request_mem\n", __func__);
		goto err_request_mem;
	}

	ncd->chip.IO_ADDR_W = ioremap(mem->start, mem->end - mem->start);
	if (!ncd->chip.IO_ADDR_W) {
		ret = -ENOMEM;
		dev_dbg(&pdev->dev, "%s: err_ioremap\n", __func__);
		goto err_ioremap;
	}
	ncd->chip.IO_ADDR_R = ncd->chip.IO_ADDR_W;
	ncd->mem = mem;

	if (ncd->nand_info->busy_pin) {
		ret = gpio_request(ncd->nand_info->busy_pin, DRIVER_NAME);
		if (ret) {
			dev_dbg(&pdev->dev, "%s: err_gpio_request\n", __func__);
			goto err_gpio_request;
		}
		gpio_direction_input(ncd->nand_info->busy_pin);

		ncd->chip.dev_ready = ccx9x_nand_dev_ready;
	}

	ncd->chip.write_buf = ccx9x_nand_write_buf;
	ncd->chip.read_buf = ccx9x_nand_read_buf;
	ncd->chip.verify_buf = ccx9x_nand_verify_buf;
	ncd->chip.cmd_ctrl = ccx9x_nand_cmd_ctrl;
	ncd->chip.ecc.mode = NAND_ECC_SOFT;
	ncd->chip.chip_delay = ncd->nand_info->delay;
	ncd->chip.controller = NULL;

	ncd->mtd.name = DEVICE_NAME;
	ncd->mtd.priv = &ncd->chip;

	if (nand_scan(&ncd->mtd, 1)) {
		ret = -ENODEV;
		dev_dbg(&pdev->dev, "%s: err_scan\n", __func__);
		goto err_scan;
	}

	dev_dbg(&pdev->dev, "NAND Flash memory mapped to virtual %p\n",
			ncd->chip.IO_ADDR_W);

#ifdef CONFIG_MTD_CMDLINE_PARTS
	/* read partition information from cmdline*/
	num_parts = parse_mtd_partitions(&ncd->mtd, probes, &mtd_parts, 0);

	/* register partitions */
	add_mtd_partitions(&ncd->mtd, mtd_parts, num_parts);
#endif

	return 0;

err_scan:
	if (ncd->nand_info->busy_pin)
		gpio_free(ncd->nand_info->busy_pin);
err_gpio_request:
	iounmap(ncd->chip.IO_ADDR_W);
err_ioremap:
	release_mem_region(mem->start, mem->end - mem->start);
err_request_mem:
	release_resource(mem);
err_get_mem:
err_pdata:
	kfree(ncd);
err_alloc:
	return ret;
}

static int ccx9x_nand_remove(struct platform_device *pdev)
{
	struct nand_ccx9x *ncd = platform_get_drvdata(pdev);

	if (ncd->nand_info->busy_pin)
		gpio_free(ncd->nand_info->busy_pin);
	iounmap(ncd->chip.IO_ADDR_W);
	release_mem_region(ncd->mem->start,
			ncd->mem->end - ncd->mem->start);
	release_resource(ncd->mem);
	kfree(ncd);

	return 0;
}

static struct platform_driver ccx9x_nand_driver = {
	.probe = ccx9x_nand_probe,
	.remove = ccx9x_nand_remove,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	},
};

static int __init ccx9x_nand_init(void)
{
	int ret = -ENOMEM;

	ret = platform_driver_register(&ccx9x_nand_driver);
	if (ret) {
		pr_debug("%s: err_pdev_register\n", __func__);
		goto err_pdev_register;
	}

	return 0;

err_pdev_register:
	return ret;
}

static void __exit ccx9x_nand_exit(void)
{
	platform_driver_unregister(&ccx9x_nand_driver);
}

module_init(ccx9x_nand_init);
module_exit(ccx9x_nand_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Matthias Ludwig");
MODULE_DESCRIPTION("Digi CCx9x MTD NAND driver");