aboutsummaryrefslogtreecommitdiffstats
path: root/meta-steppeeagle/recipes-kernel/amd-spi/files/spirom.c
blob: f3701291474067a436dcc0aa7f17e3bf0469022a (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*****************************************************************************
*
* spirom.c - SPI ROM client driver
*
* Copyright (c) 2014, Advanced Micro Devices, Inc.
*
* 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 St, Fifth Floor, Boston, MA  02110-1301  USA
*   
***************************************************************************/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/spi/spi.h>
#include <linux/amd_imc.h>
#include <asm/uaccess.h>

#include "spirom.h"

#define SPIROM_VERSION	"0.1"

/*
 * SPI has a character major number assigned.  We allocate minor numbers
 * dynamically using a bitmask.  You must use hotplug tools, such as udev
 * (or mdev with busybox) to create and destroy the /dev/spiromB.C device
 * nodes, since there is no fixed association of minor numbers with any
 * particular SPI bus or device.
 */
#define SPIROM_MAJOR			153	/* assigned */
#define N_SPI_MINORS			32	/* ... up to 256 */

#define SPI_BUS		0
#define SPI_BUS_CS1	0

static unsigned long	minors[N_SPI_MINORS / BITS_PER_LONG];


struct spirom_data {
	dev_t			devt;
	spinlock_t		spi_lock;
	struct spi_device	*spi;
	struct list_head	device_entry;
	struct completion	done;

	struct mutex		buf_lock;
	unsigned		users;
};

static LIST_HEAD(device_list);
static DEFINE_MUTEX(device_list_lock);

/*-------------------------------------------------------------------------*/

/*
 * We can't use the standard synchronous wrappers for file I/O; we
 * need to protect against async removal of the underlying spi_device.
 */
static void spirom_complete(void *arg)
{
	complete(arg);
}

static ssize_t
spirom_sync(struct spirom_data *spirom, struct spi_message *message)
{
	int status;

	message->complete = spirom_complete;
	message->context = &spirom->done;

	spin_lock_irq(&spirom->spi_lock);
	if (spirom->spi == NULL)
		status = -ESHUTDOWN;
	else
		status = spi_async(spirom->spi, message);
	spin_unlock_irq(&spirom->spi_lock);

	if (status == 0) {
		/*
		 * There might be cases where the controller driver has been
		 * unloaded in the middle of a transaction. So we might end up
		 * in a situation where we will be waiting for an event which
		 * will never happen. So we provide a timeout of 1 second for
		 * situations like this.
		 */
		wait_for_completion_timeout(&spirom->done, HZ);
		status = message->status;
		if (status == 0)
			status = message->actual_length;
	}
	return status;
}

static int spirom_message(struct spirom_data *spirom,
		struct spi_ioc_transfer *u_trans, unsigned long arg)
{
	struct spi_message msg;
	struct spi_transfer *transfer;
	u8 *buffer;
	int status = u_trans->len;

	buffer = u_trans->buf;
	spi_message_init(&msg);

	/* The very first spi_transfer will contain the command only */
	transfer = kzalloc(sizeof(struct spi_transfer), GFP_KERNEL);
	if (!transfer)
		return -ENOMEM;

	transfer->tx_buf = buffer;
	transfer->len = 1;
	buffer += transfer->len;
	spi_message_add_tail(transfer, &msg);

	/*
	 * If the command expects an address as its argument, we populate
	 * it in the very next spi_transfer.
	 */
	if (u_trans->addr_present) {
		transfer = kzalloc(sizeof(struct spi_transfer), GFP_KERNEL);
		if (!transfer)
			return -ENOMEM;

		transfer->tx_buf = buffer;
		transfer->len = 3; // 3-byte address
		buffer += transfer->len;
		spi_message_add_tail(transfer, &msg);
	}

	/*
	 * Next is data, which can have a maximum of 64-bytes, the size limited
	 * by the number of bytes that can stored in the controller FIFO.
	 */
	if (u_trans->len) {
		transfer = kzalloc(sizeof(struct spi_transfer), GFP_KERNEL);
		if (!transfer)
			return -ENOMEM;

		if (u_trans->direction == TRANSMIT)
			transfer->tx_buf = buffer;
		else if (u_trans->direction == RECEIVE)
			transfer->rx_buf = buffer;

		transfer->len = u_trans->len;
		/* No need to increment buffer pointer */
		spi_message_add_tail(transfer, &msg);
	}

	status = spirom_sync(spirom, &msg);

	if (u_trans->direction == RECEIVE) {
		/*
		 * The received data should have been populated in
		 * u_trans->buf, so we just need to copy it into the
		 * user-space buffer.
		 */
		buffer = u_trans->buf;
		if (u_trans->addr_present) {
			buffer += 4; // 1-byte command and 3-byte address
			if(__copy_to_user((u8 __user *)
				(((struct spi_ioc_transfer *)arg)->buf) + 4,
				buffer, u_trans->len)) {
				status = -EFAULT;
			}
		} else {
			buffer += 1; // 1-byte command only
			if(__copy_to_user((u8 __user *)
				(((struct spi_ioc_transfer *)arg)->buf) + 1,
				buffer, u_trans->len)) {
				status = -EFAULT;
			}
		}
	}

	/* Done with everything, free the memory taken by spi_transfer */
	while (msg.transfers.next != &msg.transfers) {
		transfer = list_entry(msg.transfers.next, struct spi_transfer,
				      transfer_list);
		msg.transfers.next = transfer->transfer_list.next;
		transfer->transfer_list.next->prev = &msg.transfers;
		kfree(transfer);
	}

	return status;
}

static long
spirom_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	int			err = 0;
	int			retval = 0;
	struct spirom_data	*spirom;
	struct spi_device	*spi;
	u32			tmp;
	struct spi_ioc_transfer	*ioc;

	/* Check type and command number */
	if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
		return -ENOTTY;

	/* Check access direction once here; don't repeat below.
	 * IOC_DIR is from the user perspective, while access_ok is
	 * from the kernel perspective; so they look reversed.
	 */
	if (_IOC_DIR(cmd) & _IOC_READ)
		err = !access_ok(VERIFY_WRITE,
				(void __user *)arg, _IOC_SIZE(cmd));
	if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
		err = !access_ok(VERIFY_READ,
				(void __user *)arg, _IOC_SIZE(cmd));
	if (err)
		return -EFAULT;

	/* guard against device removal before, or while,
	 * we issue this ioctl.
	 */
	spirom = filp->private_data;
	spin_lock_irq(&spirom->spi_lock);
	spi = spi_dev_get(spirom->spi);
	spin_unlock_irq(&spirom->spi_lock);

	if (spi == NULL)
		return -ESHUTDOWN;

	/* use the buffer lock here for triple duty:
	 *  - prevent I/O (from us) so calling spi_setup() is safe;
	 *  - prevent concurrent SPI_IOC_WR_* from morphing
	 *    data fields while SPI_IOC_RD_* reads them;
	 *  - SPI_IOC_MESSAGE needs the buffer locked "normally".
	 */
	mutex_lock(&spirom->buf_lock);

	/* segmented and/or full-duplex I/O request */
	if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0)) ||
	    _IOC_DIR(cmd) !=_IOC_WRITE) {
		retval = -ENOTTY;
		goto out;
	}

	tmp = sizeof(struct spi_ioc_transfer);

	/* copy into scratch area */
	ioc = kzalloc(tmp, GFP_KERNEL);
	if (!ioc) {
		retval = -ENOMEM;
		goto out;
	}
	if (__copy_from_user(ioc, (struct spi_ioc_transfer __user *)arg,
	    tmp)) {
		kfree(ioc);
		retval = -EFAULT;
		goto out;
	}

	/* translate to spi_message, execute */
	retval = spirom_message(spirom, ioc, arg);
	kfree(ioc);

out:
	mutex_unlock(&spirom->buf_lock);
	spi_dev_put(spi);
	return retval;
}

static int spirom_open(struct inode *inode, struct file *filp)
{
	struct spirom_data	*spirom;
	int			status = -ENXIO;

	mutex_lock(&device_list_lock);

	list_for_each_entry(spirom, &device_list, device_entry) {
		if (spirom->devt == inode->i_rdev) {
			status = 0;
			break;
		}
	}
	if (status == 0) {
		if (status == 0) {
			spirom->users++;
			filp->private_data = spirom;
			nonseekable_open(inode, filp);
		}
	} else
		pr_debug("spirom: nothing for minor %d\n", iminor(inode));

	mutex_unlock(&device_list_lock);

	/*
	 * In case IMC is enabled, we need to inform IMC to stop
	 * fetching code from the BIOS ROM. We will inform IMC when
	 * it is safe to start fetching from ROM again once we are
	 * done with our SPI transactions.
	 */
	amd_imc_enter_scratch_ram();

	return status;
}

static int spirom_release(struct inode *inode, struct file *filp)
{
	struct spirom_data	*spirom;
	int			status = 0;

	mutex_lock(&device_list_lock);
	spirom = filp->private_data;
	filp->private_data = NULL;

	/* last close? */
	spirom->users--;
	if (!spirom->users) {
		int		dofree;

		/* ... after we unbound from the underlying device? */
		spin_lock_irq(&spirom->spi_lock);
		dofree = (spirom->spi == NULL);
		spin_unlock_irq(&spirom->spi_lock);

		if (dofree)
			kfree(spirom);
	}
	mutex_unlock(&device_list_lock);

	/*
	 * In case IMC is enabled, we would have instructed IMC to stop
	 * fetching from ROM BIOS earlier in the code path. Now that we
	 * are done, we can safely inform IMC to start fetching from ROM
	 * again.
	 */
	amd_imc_exit_scratch_ram();

	return status;
}

static const struct file_operations spirom_fops = {
	.owner =	THIS_MODULE,
	.unlocked_ioctl = spirom_ioctl,
	.open =		spirom_open,
	.release =	spirom_release,
};

static int __init add_spi_device_to_bus(void)
{
	struct spi_master *spi_master;
	struct spi_device *spi_device;
	struct spi_board_info spi_info;

	spi_master = spi_busnum_to_master(SPI_BUS);
	if (!spi_master) {
		printk(KERN_ALERT "Please make sure to \'modprobe "
			"spi_amd\' driver first\n");
		return -1;
	}
	memset(&spi_info, 0, sizeof(struct spi_board_info));

	strlcpy(spi_info.modalias, "spirom", SPI_NAME_SIZE);
	spi_info.bus_num = SPI_BUS; //Bus number of SPI master
	spi_info.chip_select = SPI_BUS_CS1; //CS on which SPI device is connected

	spi_device = spi_new_device(spi_master, &spi_info);
	if (!spi_device)
		return -ENODEV;

	return 0;
}

/*-------------------------------------------------------------------------*/

/* The main reason to have this class is to make mdev/udev create the
 * /dev/spiromB.C character device nodes exposing our userspace API.
 * It also simplifies memory management.
 */

static struct class *spirom_class;

/*-------------------------------------------------------------------------*/

static int spirom_probe(struct spi_device *spi)
{
	struct spirom_data	*spirom;
	int			status;
	unsigned long		minor;

	/* Allocate driver data */
	spirom = kzalloc(sizeof(*spirom), GFP_KERNEL);
	if (!spirom)
		return -ENOMEM;

	/* Initialize the driver data */
	spirom->spi = spi;
	spin_lock_init(&spirom->spi_lock);
	mutex_init(&spirom->buf_lock);

	INIT_LIST_HEAD(&spirom->device_entry);
	init_completion(&spirom->done);

	/* If we can allocate a minor number, hook up this device.
	 * Reusing minors is fine so long as udev or mdev is working.
	 */
	mutex_lock(&device_list_lock);
	minor = find_first_zero_bit(minors, N_SPI_MINORS);
	if (minor < N_SPI_MINORS) {
		struct device *dev;

		spirom->devt = MKDEV(SPIROM_MAJOR, minor);
		dev = device_create(spirom_class, &spi->dev, spirom->devt,
				    spirom, "spirom%d.%d",
				    spi->master->bus_num, spi->chip_select);
		status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
	} else {
		dev_dbg(&spi->dev, "no minor number available!\n");
		status = -ENODEV;
	}
	if (status == 0) {
		set_bit(minor, minors);
		list_add(&spirom->device_entry, &device_list);
	}
	mutex_unlock(&device_list_lock);

	if (status == 0)
		spi_set_drvdata(spi, spirom);
	else
		kfree(spirom);

	return status;
}

static int spirom_remove(struct spi_device *spi)
{
	struct spirom_data	*spirom = spi_get_drvdata(spi);

	/* make sure ops on existing fds can abort cleanly */
	spin_lock_irq(&spirom->spi_lock);
	spirom->spi = NULL;
	spi_set_drvdata(spi, NULL);
	spin_unlock_irq(&spirom->spi_lock);

	/* prevent new opens */
	mutex_lock(&device_list_lock);
	list_del(&spirom->device_entry);
	clear_bit(MINOR(spirom->devt), minors);
	device_destroy(spirom_class, spirom->devt);
	if (spirom->users == 0)
		kfree(spirom);
	mutex_unlock(&device_list_lock);

	return 0;
}

static struct spi_driver spirom_spi = {
	.driver = {
		.name =		"spirom",
		.owner =	THIS_MODULE,
	},
	.probe =	spirom_probe,
	.remove =	spirom_remove,

	/* NOTE:  suspend/resume methods are not necessary here.
	 * We don't do anything except pass the requests to/from
	 * the underlying controller.  The refrigerator handles
	 * most issues; the controller driver handles the rest.
	 */
};

/*-------------------------------------------------------------------------*/

static int __init spirom_init(void)
{
	int status;

	pr_info("AMD SPIROM Driver v%s\n", SPIROM_VERSION);

	/* Claim our 256 reserved device numbers.  Then register a class
	 * that will key udev/mdev to add/remove /dev nodes.  Last, register
	 * the driver which manages those device numbers.
	 */
	BUILD_BUG_ON(N_SPI_MINORS > 256);
	status = register_chrdev(SPIROM_MAJOR, "spi", &spirom_fops);
	if (status < 0)
		return status;

	spirom_class = class_create(THIS_MODULE, "spirom");
	if (IS_ERR(spirom_class)) {
		unregister_chrdev(SPIROM_MAJOR, spirom_spi.driver.name);
		return PTR_ERR(spirom_class);
	}

	status = spi_register_driver(&spirom_spi);
	if (status < 0) {
		class_destroy(spirom_class);
		unregister_chrdev(SPIROM_MAJOR, spirom_spi.driver.name);
	}

	status = add_spi_device_to_bus();
	if (status < 0) {
		spi_unregister_driver(&spirom_spi);
		class_destroy(spirom_class);
		unregister_chrdev(SPIROM_MAJOR, spirom_spi.driver.name);
	}

	return status;
}
module_init(spirom_init);

static void __exit spirom_exit(void)
{
	spi_unregister_driver(&spirom_spi);
	class_destroy(spirom_class);
	unregister_chrdev(SPIROM_MAJOR, spirom_spi.driver.name);
}
module_exit(spirom_exit);

MODULE_AUTHOR("Arindam Nath <arindam.nath@amd.com>");
MODULE_DESCRIPTION("User mode SPI ROM interface");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("spi:spirom");