Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 1 | /* |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 2 | * Written by Pekka Paalanen, 2008-2009 <pq@iki.fi> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 3 | */ |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 4 | |
| 5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 6 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 7 | #include <linux/module.h> |
Pekka Paalanen | 970e6fa | 2008-05-12 21:21:03 +0200 | [diff] [blame] | 8 | #include <linux/io.h> |
Pekka Paalanen | 9e57fb3 | 2008-09-16 22:00:34 +0300 | [diff] [blame] | 9 | #include <linux/mmiotrace.h> |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 10 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 11 | static unsigned long mmio_address; |
| 12 | module_param(mmio_address, ulong, 0); |
Pekka Paalanen | 5ff9369 | 2009-03-01 16:10:08 +0200 | [diff] [blame] | 13 | MODULE_PARM_DESC(mmio_address, " Start address of the mapping of 16 kB " |
| 14 | "(or 8 MB if read_far is non-zero)."); |
| 15 | |
| 16 | static unsigned long read_far = 0x400100; |
| 17 | module_param(read_far, ulong, 0); |
| 18 | MODULE_PARM_DESC(read_far, " Offset of a 32-bit read within 8 MB " |
| 19 | "(default: 0x400100)."); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 20 | |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 21 | static unsigned v16(unsigned i) |
| 22 | { |
| 23 | return i * 12 + 7; |
| 24 | } |
| 25 | |
| 26 | static unsigned v32(unsigned i) |
| 27 | { |
| 28 | return i * 212371 + 13; |
| 29 | } |
| 30 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 31 | static void do_write_test(void __iomem *p) |
| 32 | { |
| 33 | unsigned int i; |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 34 | pr_info("write test.\n"); |
Pekka Paalanen | 9e57fb3 | 2008-09-16 22:00:34 +0300 | [diff] [blame] | 35 | mmiotrace_printk("Write test.\n"); |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 36 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 37 | for (i = 0; i < 256; i++) |
| 38 | iowrite8(i, p + i); |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 39 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 40 | for (i = 1024; i < (5 * 1024); i += 2) |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 41 | iowrite16(v16(i), p + i); |
| 42 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 43 | for (i = (5 * 1024); i < (16 * 1024); i += 4) |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 44 | iowrite32(v32(i), p + i); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static void do_read_test(void __iomem *p) |
| 48 | { |
| 49 | unsigned int i; |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 50 | unsigned errs[3] = { 0 }; |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 51 | pr_info("read test.\n"); |
Pekka Paalanen | 9e57fb3 | 2008-09-16 22:00:34 +0300 | [diff] [blame] | 52 | mmiotrace_printk("Read test.\n"); |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 53 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 54 | for (i = 0; i < 256; i++) |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 55 | if (ioread8(p + i) != i) |
| 56 | ++errs[0]; |
| 57 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 58 | for (i = 1024; i < (5 * 1024); i += 2) |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 59 | if (ioread16(p + i) != v16(i)) |
| 60 | ++errs[1]; |
| 61 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 62 | for (i = (5 * 1024); i < (16 * 1024); i += 4) |
Pekka Paalanen | fab852a | 2009-03-01 16:09:14 +0200 | [diff] [blame] | 63 | if (ioread32(p + i) != v32(i)) |
| 64 | ++errs[2]; |
| 65 | |
| 66 | mmiotrace_printk("Read errors: 8-bit %d, 16-bit %d, 32-bit %d.\n", |
| 67 | errs[0], errs[1], errs[2]); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Pekka Paalanen | 5ff9369 | 2009-03-01 16:10:08 +0200 | [diff] [blame] | 70 | static void do_read_far_test(void __iomem *p) |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 71 | { |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 72 | pr_info("read far test.\n"); |
Pekka Paalanen | 5ff9369 | 2009-03-01 16:10:08 +0200 | [diff] [blame] | 73 | mmiotrace_printk("Read far test.\n"); |
| 74 | |
| 75 | ioread32(p + read_far); |
| 76 | } |
| 77 | |
| 78 | static void do_test(unsigned long size) |
| 79 | { |
| 80 | void __iomem *p = ioremap_nocache(mmio_address, size); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 81 | if (!p) { |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 82 | pr_err("could not ioremap, aborting.\n"); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 83 | return; |
| 84 | } |
Pekka Paalanen | 9e57fb3 | 2008-09-16 22:00:34 +0300 | [diff] [blame] | 85 | mmiotrace_printk("ioremap returned %p.\n", p); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 86 | do_write_test(p); |
| 87 | do_read_test(p); |
Pekka Paalanen | 5ff9369 | 2009-03-01 16:10:08 +0200 | [diff] [blame] | 88 | if (read_far && read_far < size - 4) |
| 89 | do_read_far_test(p); |
Pekka Paalanen | d61fc44 | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 90 | iounmap(p); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static int __init init(void) |
| 94 | { |
Pekka Paalanen | 5ff9369 | 2009-03-01 16:10:08 +0200 | [diff] [blame] | 95 | unsigned long size = (read_far) ? (8 << 20) : (16 << 10); |
| 96 | |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 97 | if (mmio_address == 0) { |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 98 | pr_err("you have to use the module argument mmio_address.\n"); |
| 99 | pr_err("DO NOT LOAD THIS MODULE UNLESS YOU REALLY KNOW WHAT YOU ARE DOING!\n"); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 100 | return -ENXIO; |
| 101 | } |
| 102 | |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 103 | pr_warning("WARNING: mapping %lu kB @ 0x%08lx in PCI address space, " |
| 104 | "and writing 16 kB of rubbish in there.\n", |
| 105 | size >> 10, mmio_address); |
Pekka Paalanen | 5ff9369 | 2009-03-01 16:10:08 +0200 | [diff] [blame] | 106 | do_test(size); |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 107 | pr_info("All done.\n"); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static void __exit cleanup(void) |
| 112 | { |
Joe Perches | 3c35586 | 2009-10-04 17:53:40 -0700 | [diff] [blame^] | 113 | pr_debug("unloaded.\n"); |
Pekka Paalanen | 8b7d89d | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | module_init(init); |
| 117 | module_exit(cleanup); |
| 118 | MODULE_LICENSE("GPL"); |