testmmiotrace.c 2.7 KB
Newer Older
1
/*
2
 * Written by Pekka Paalanen, 2008-2009 <pq@iki.fi>
3 4
 */
#include <linux/module.h>
P
Pekka Paalanen 已提交
5
#include <linux/io.h>
6
#include <linux/mmiotrace.h>
7 8 9 10 11

#define MODULE_NAME "testmmiotrace"

static unsigned long mmio_address;
module_param(mmio_address, ulong, 0);
12 13 14 15 16 17 18
MODULE_PARM_DESC(mmio_address, " Start address of the mapping of 16 kB "
				"(or 8 MB if read_far is non-zero).");

static unsigned long read_far = 0x400100;
module_param(read_far, ulong, 0);
MODULE_PARM_DESC(read_far, " Offset of a 32-bit read within 8 MB "
				"(default: 0x400100).");
19

20 21 22 23 24 25 26 27 28 29
static unsigned v16(unsigned i)
{
	return i * 12 + 7;
}

static unsigned v32(unsigned i)
{
	return i * 212371 + 13;
}

30 31 32
static void do_write_test(void __iomem *p)
{
	unsigned int i;
33
	pr_info(MODULE_NAME ": write test.\n");
34
	mmiotrace_printk("Write test.\n");
35

36 37
	for (i = 0; i < 256; i++)
		iowrite8(i, p + i);
38

39
	for (i = 1024; i < (5 * 1024); i += 2)
40 41
		iowrite16(v16(i), p + i);

42
	for (i = (5 * 1024); i < (16 * 1024); i += 4)
43
		iowrite32(v32(i), p + i);
44 45 46 47 48
}

static void do_read_test(void __iomem *p)
{
	unsigned int i;
49
	unsigned errs[3] = { 0 };
50
	pr_info(MODULE_NAME ": read test.\n");
51
	mmiotrace_printk("Read test.\n");
52

53
	for (i = 0; i < 256; i++)
54 55 56
		if (ioread8(p + i) != i)
			++errs[0];

57
	for (i = 1024; i < (5 * 1024); i += 2)
58 59 60
		if (ioread16(p + i) != v16(i))
			++errs[1];

61
	for (i = (5 * 1024); i < (16 * 1024); i += 4)
62 63 64 65 66
		if (ioread32(p + i) != v32(i))
			++errs[2];

	mmiotrace_printk("Read errors: 8-bit %d, 16-bit %d, 32-bit %d.\n",
						errs[0], errs[1], errs[2]);
67 68
}

69 70 71 72 73 74 75 76 77
static void do_read_far_test(void __iomem *p)
{
	pr_info(MODULE_NAME ": read far test.\n");
	mmiotrace_printk("Read far test.\n");

	ioread32(p + read_far);
}

static void do_test(unsigned long size)
78
{
79
	void __iomem *p = ioremap_nocache(mmio_address, size);
80
	if (!p) {
81
		pr_err(MODULE_NAME ": could not ioremap, aborting.\n");
82 83
		return;
	}
84
	mmiotrace_printk("ioremap returned %p.\n", p);
85 86
	do_write_test(p);
	do_read_test(p);
87 88
	if (read_far && read_far < size - 4)
		do_read_far_test(p);
P
Pekka Paalanen 已提交
89
	iounmap(p);
90 91 92 93
}

static int __init init(void)
{
94 95
	unsigned long size = (read_far) ? (8 << 20) : (16 << 10);

96
	if (mmio_address == 0) {
97 98 99
		pr_err(MODULE_NAME ": you have to use the module argument "
							"mmio_address.\n");
		pr_err(MODULE_NAME ": DO NOT LOAD THIS MODULE UNLESS"
100 101 102 103
				" YOU REALLY KNOW WHAT YOU ARE DOING!\n");
		return -ENXIO;
	}

104 105 106 107 108
	pr_warning(MODULE_NAME ": WARNING: mapping %lu kB @ 0x%08lx in PCI "
		"address space, and writing 16 kB of rubbish in there.\n",
		 size >> 10, mmio_address);
	do_test(size);
	pr_info(MODULE_NAME ": All done.\n");
109 110 111 112 113
	return 0;
}

static void __exit cleanup(void)
{
114
	pr_debug(MODULE_NAME ": unloaded.\n");
115 116 117 118 119
}

module_init(init);
module_exit(cleanup);
MODULE_LICENSE("GPL");