summaryrefslogtreecommitdiff
path: root/drivers/char/tegra_gmi_char.c
blob: bf5217a26d9ab0fc5705c80caff2923337a0976a (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * Copyright (C) 2013, NVIDIA Corporation.  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 as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * drivers/char/tegra_gmi_char.c
 *
 * MTD mapping driver for the internal SNOR controller in Tegra SoCs
 *
 */
#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include <mach/iomap.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
#include <linux/tegra_snor.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/platform_data/tegra_nor.h>

#ifdef CONFIG_TEGRA_GMI_ACCESS_CONTROL
#include <linux/tegra_gmi_access.h>
#endif

#define gmi_lock(info)  \
do {    \
	if ((info)->request_gmi_access) \
		(info)->request_gmi_access((info)->gmiLockHandle);      \
} while (0)

#define gmi_unlock(info)        \
do {    \
	if ((info)->release_gmi_access) \
		(info)->release_gmi_access();   \
} while (0)



struct tegra_gmi_char_info {
	struct tegra_nor_chip_parms *plat;
	struct device *dev;
	void __iomem *base;
	u32 init_config;
	u32 timing0_default, timing1_default;
	u32 timing0_read, timing1_read;
	unsigned long gmi_chrdev_is_open;
	u32 gmiLockHandle;
	int (*request_gmi_access)(u32 gmiLockHandle);
	void (*release_gmi_access)(void);
	dev_t gmi_dev_t;
	struct cdev gmi_cdev;
	struct class *gmi_class;
	struct device *gmi_device;
};

static struct tegra_gmi_char_info *info;


#define DRV_NAME "tegra-gmi-char"

static inline unsigned long snor_tegra_readl(struct tegra_gmi_char_info *tsnor,
					     unsigned long reg)
{
	return readl(tsnor->base + reg);
}

static inline void snor_tegra_writel(struct tegra_gmi_char_info *tsnor,
				     unsigned long val, unsigned long reg)
{
	writel(val, tsnor->base + reg);
}


static int tegra_gmi_char_prepare_regs(struct tegra_gmi_char_info *info)
{
	struct tegra_nor_chip_parms *chip_parm = info->plat;
	struct cs_info *csinfo = &chip_parm->csinfo;

	u32 width = chip_parm->BusWidth;
	u32 config = 0;

	config |= TEGRA_SNOR_CONFIG_DEVICE_MODE(0);
	config |= TEGRA_SNOR_CONFIG_SNOR_CS(csinfo->cs);
	config &= ~TEGRA_SNOR_CONFIG_DEVICE_TYPE;
	config |= TEGRA_SNOR_CONFIG_WP;	/* Enable writes */

	switch (width) {
	case 2:
		config &= ~TEGRA_SNOR_CONFIG_WORDWIDE;	/* 16 bit */
		break;
	case 4:
		config |= TEGRA_SNOR_CONFIG_WORDWIDE;	/* 32 bit */
		break;
	default:
		return -EINVAL;
	}

	switch (chip_parm->MuxMode) {
	case NorMuxMode_ADNonMux:
		config &= ~TEGRA_SNOR_CONFIG_MUX_MODE;
		break;
	case NorMuxMode_ADMux:
		config |= TEGRA_SNOR_CONFIG_MUX_MODE;
		break;
	default:
		return -EINVAL;
	}

	switch (chip_parm->ReadyActive) {
	case NorReadyActive_WithData:
		config &= ~TEGRA_SNOR_CONFIG_RDY_ACTIVE;
		break;
	case NorReadyActive_BeforeData:
		config |= TEGRA_SNOR_CONFIG_RDY_ACTIVE;
		break;
	default:
		return -EINVAL;
	}

	info->init_config = config;
	info->timing0_default = chip_parm->timing_default.timing0;
	info->timing0_read = chip_parm->timing_read.timing0;
	info->timing1_default = chip_parm->timing_default.timing1;
	info->timing1_read = chip_parm->timing_read.timing1;

	return 0;
}

static int gmichar_chrdev_open(struct inode *inode, struct file *file)
{
	/* Only one active open supported */
	if (test_and_set_bit(0, &info->gmi_chrdev_is_open))
		return -EBUSY;
	return 0;
}

static int gmichar_chrdev_release(struct inode *inode, struct file *file)
{
	clear_bit(0, &info->gmi_chrdev_is_open);
	return 0;
}


static ssize_t gmichar_chrdev_read_helper(char *dest, int *src,
						size_t size)
{
	int i;

	snor_tegra_writel(info, info->init_config, TEGRA_SNOR_CONFIG_REG);
	snor_tegra_writel(info, info->timing1_read, TEGRA_SNOR_TIMING1_REG);
	snor_tegra_writel(info, info->timing0_read, TEGRA_SNOR_TIMING0_REG);

	udelay(1);

	for (i = 0; i < size; i++)
		dest[i] = __raw_readb(src + i);

	udelay(1);

	return size;
}

static loff_t gmichar_chrdev_llseek(struct file *filp,
						loff_t offset, int origin)
{
	ssize_t ret;
	struct tegra_nor_chip_parms *chip_parm = info->plat;
	struct cs_info *csinfo = &chip_parm->csinfo;

	if (info == NULL)
		return -EINVAL;

	switch (origin) {
	case SEEK_END:
		offset += csinfo->size;
		break;
	case SEEK_CUR:
		offset += filp->f_pos;
		break;
	case SEEK_SET:
		break;
	default:
		ret = -EINVAL;
	}

	if (offset < 0 || offset > csinfo->size)
		offset = -EINVAL;
	else
		filp->f_pos = offset;

	return offset;
}

static ssize_t gmichar_chrdev_read(struct file *filp, char *data,
						size_t size, loff_t *loff)
{
	ssize_t ret;
	char *vbuf = NULL;
	unsigned int *ptr = NULL;
	struct tegra_nor_chip_parms *chip_parm = info->plat;
	struct cs_info *csinfo = &chip_parm->csinfo;

	if (info == NULL)
		return -EINVAL;

	if (*loff + size > csinfo->size)
		return -EINVAL;

	vbuf = vmalloc(size);

	if (vbuf == NULL)
		return -EFAULT;

	ptr = csinfo->virt;
	ptr = ptr + (*loff);

	gmi_lock(info);
	ret = gmichar_chrdev_read_helper(vbuf, ptr, size);

	if (copy_to_user(data, vbuf, size))
		return -EFAULT;

	*loff = *loff + ret;
	vfree(vbuf);
	gmi_unlock(info);
	return ret;
}


static ssize_t gmichar_chrdev_write_helper(unsigned int *dest, char *src,
						size_t size)
{
	int i;

	snor_tegra_writel(info, info->init_config, TEGRA_SNOR_CONFIG_REG);
	snor_tegra_writel(info, info->timing1_read, TEGRA_SNOR_TIMING1_REG);
	snor_tegra_writel(info, info->timing0_read, TEGRA_SNOR_TIMING0_REG);

	udelay(1);

	for (i = 0; i < size; i++)
		__raw_writeb(src[i], dest + i);

	udelay(1);
	return size;

}

static ssize_t gmichar_chrdev_write(struct file *filp, const char *data,
						size_t size, loff_t *loff)
{
	ssize_t ret;
	struct tegra_nor_chip_parms *chip_parm = info->plat;
	struct cs_info *csinfo = &chip_parm->csinfo;
	char *vbuf = NULL;
	unsigned int *ptr = NULL;

	if (info == NULL)
		return -EINVAL;

	vbuf = vmalloc(size);
	if (copy_from_user(vbuf, data, size))
		return -EFAULT;

	ptr = csinfo->virt;
	ptr = ptr +  *loff;
	gmi_lock(info);

	ret = gmichar_chrdev_write_helper(ptr, vbuf, size);
	*loff += size;

	gmi_unlock(info);
	vfree(vbuf);
	return ret;

}

const struct file_operations gmichar_fops = {
	.open           = gmichar_chrdev_open,
	.read           = gmichar_chrdev_read,
	.write          = gmichar_chrdev_write,
	.llseek		= gmichar_chrdev_llseek,
	.release        = gmichar_chrdev_release
};

static int __devinit tegra_gmi_char_probe(struct platform_device *pdev)
{
	int err = -ENODEV;
	struct tegra_nor_chip_parms *plat = pdev->dev.platform_data;
	struct device *dev = &pdev->dev;

	if (!plat) {
		pr_err("%s: no platform device info\n", __func__);
		return -EINVAL;
	}
	info = devm_kzalloc(dev, sizeof(struct tegra_gmi_char_info),
			    GFP_KERNEL);
	if (!info)
		return -ENOMEM;
	info->base = ((void __iomem *)IO_APB_VIRT +
				(TEGRA_SNOR_BASE - IO_APB_PHYS));
	info->plat = plat;
	info->dev = dev;

#ifdef CONFIG_TEGRA_GMI_ACCESS_CONTROL
	info->gmiLockHandle = register_gmi_device(DRV_NAME, 0);
	info->request_gmi_access = request_gmi_access;
	info->release_gmi_access = release_gmi_access;
	info->gmiLockHandle =	register_gmi_device(DRV_NAME, 0);
#else
	info->gmiLockHandle = NULL;
	info->request_gmi_access = NULL;
	info->release_gmi_access = NULL;
#endif

	err = tegra_gmi_char_prepare_regs(info);

	if (err) {
		dev_err(dev, "Error initializing reg values\n");
		return err;
	}

	platform_set_drvdata(pdev, info);

	info->gmi_class = class_create(THIS_MODULE, DRV_NAME);

	if (IS_ERR(info->gmi_class)) {
		err = PTR_ERR(info->gmi_class);
		return err;
	}

	err = alloc_chrdev_region(&info->gmi_dev_t, 0, 1, DRV_NAME);

	if (err < 0) {
		class_destroy(info->gmi_class);
		pr_err("%s: failed to allocate device region\n", DRV_NAME);
		return err;
	}

	cdev_init(&info->gmi_cdev, &gmichar_fops);
	err = cdev_add(&info->gmi_cdev, info->gmi_dev_t, 1);

	if (err)
		return err;

	info->gmi_device = device_create(info->gmi_class, NULL,
					info->gmi_dev_t, info, DRV_NAME);

	if (IS_ERR(info->gmi_device)) {
		pr_err("device create failed for %s\n", DRV_NAME);
		cdev_del(&info->gmi_cdev);
		goto fail;
	}

	return 0;
fail:
	pr_err("Tegra GMI CHAR probe failed\n");
	return err;
}

static int __devexit tegra_gmi_char_remove(struct platform_device *pdev)
{

	if (info->gmi_class)
		class_destroy(info->gmi_class);

	unregister_chrdev_region(info->gmi_dev_t, 1);

	return 0;
}

static struct platform_driver __refdata tegra_gmi_char_driver = {
	.probe = tegra_gmi_char_probe,
	.remove = __devexit_p(tegra_gmi_char_remove),
	.driver = {
		   .name = DRV_NAME,
		   .owner = THIS_MODULE,
		   },
};


module_platform_driver(tegra_gmi_char_driver);

MODULE_AUTHOR("Nitin Sehgal <nsehgal@nvidia.com>");
MODULE_DESCRIPTION("GMI Bus character driver interface for NVIDIA Tegra based boards");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRV_NAME);