David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 1 | /* sun_uflash.c - Driver for user-programmable flash on |
| 2 | * Sun Microsystems SME boardsets. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This driver does NOT provide access to the OBP-flash for |
| 5 | * safety reasons-- use <linux>/drivers/sbus/char/flash.c instead. |
| 6 | * |
| 7 | * Copyright (c) 2001 Eric Brower (ebrower@usa.net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/ioport.h> |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 18 | #include <asm/prom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/uaccess.h> |
| 20 | #include <asm/io.h> |
| 21 | |
| 22 | #include <linux/mtd/mtd.h> |
| 23 | #include <linux/mtd/map.h> |
| 24 | |
| 25 | #define UFLASH_OBPNAME "flashprom" |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 26 | #define DRIVER_NAME "sun_uflash" |
| 27 | #define PFX DRIVER_NAME ": " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #define UFLASH_WINDOW_SIZE 0x200000 |
| 30 | #define UFLASH_BUSWIDTH 1 /* EBus is 8-bit */ |
| 31 | |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 32 | MODULE_AUTHOR("Eric Brower <ebrower@usa.net>"); |
| 33 | MODULE_DESCRIPTION("User-programmable flash device on Sun Microsystems boardsets"); |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 34 | MODULE_SUPPORTED_DEVICE(DRIVER_NAME); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 35 | MODULE_LICENSE("GPL"); |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 36 | MODULE_VERSION("2.1"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | struct uflash_dev { |
Stephen Rothwell | ccf0dec | 2007-03-29 00:49:54 -0700 | [diff] [blame] | 39 | const char *name; /* device name */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct map_info map; /* mtd map info */ |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 41 | struct mtd_info *mtd; /* mtd info */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | struct map_info uflash_map_templ = { |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 45 | .name = "SUNW,???-????", |
| 46 | .size = UFLASH_WINDOW_SIZE, |
| 47 | .bankwidth = UFLASH_BUSWIDTH, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 50 | int uflash_devinit(struct platform_device *op, struct device_node *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | { |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 52 | struct uflash_dev *up; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 54 | if (op->resource[1].flags) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* Non-CFI userflash device-- once I find one we |
| 56 | * can work on supporting it. |
| 57 | */ |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 58 | printk(KERN_ERR PFX "Unsupported device at %s, 0x%llx\n", |
| 59 | dp->full_name, (unsigned long long)op->resource[0].start); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | return -ENODEV; |
| 62 | } |
| 63 | |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 64 | up = kzalloc(sizeof(struct uflash_dev), GFP_KERNEL); |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 65 | if (!up) { |
| 66 | printk(KERN_ERR PFX "Cannot allocate struct uflash_dev\n"); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 67 | return -ENOMEM; |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 68 | } |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | /* copy defaults and tweak parameters */ |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 71 | memcpy(&up->map, &uflash_map_templ, sizeof(uflash_map_templ)); |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 72 | |
| 73 | up->map.size = resource_size(&op->resource[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 75 | up->name = of_get_property(dp, "model", NULL); |
| 76 | if (up->name && 0 < strlen(up->name)) |
Geert Uytterhoeven | 7c4bb4f | 2013-11-12 20:07:15 +0100 | [diff] [blame] | 77 | up->map.name = up->name; |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 78 | |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 79 | up->map.phys = op->resource[0].start; |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 80 | |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 81 | up->map.virt = of_ioremap(&op->resource[0], 0, up->map.size, |
| 82 | DRIVER_NAME); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 83 | if (!up->map.virt) { |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 84 | printk(KERN_ERR PFX "Failed to map device.\n"); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 85 | kfree(up); |
| 86 | |
| 87 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } |
| 89 | |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 90 | simple_map_init(&up->map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | /* MTD registration */ |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 93 | up->mtd = do_map_probe("cfi_probe", &up->map); |
| 94 | if (!up->mtd) { |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 95 | of_iounmap(&op->resource[0], up->map.virt, up->map.size); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 96 | kfree(up); |
| 97 | |
| 98 | return -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 101 | up->mtd->owner = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 103 | mtd_device_register(up->mtd, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 105 | dev_set_drvdata(&op->dev, up); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 106 | |
| 107 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Bill Pemberton | 06f2551 | 2012-11-19 13:23:07 -0500 | [diff] [blame] | 110 | static int uflash_probe(struct platform_device *op) |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 111 | { |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 112 | struct device_node *dp = op->dev.of_node; |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 113 | |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 114 | /* Flashprom must have the "user" property in order to |
| 115 | * be used by this driver. |
| 116 | */ |
| 117 | if (!of_find_property(dp, "user", NULL)) |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 118 | return -ENODEV; |
| 119 | |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 120 | return uflash_devinit(op, dp); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Bill Pemberton | 810b7e0 | 2012-11-19 13:26:04 -0500 | [diff] [blame] | 123 | static int uflash_remove(struct platform_device *op) |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 124 | { |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 125 | struct uflash_dev *up = dev_get_drvdata(&op->dev); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 126 | |
| 127 | if (up->mtd) { |
Jamie Iles | ee0e87b | 2011-05-23 10:23:40 +0100 | [diff] [blame] | 128 | mtd_device_unregister(up->mtd); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 129 | map_destroy(up->mtd); |
| 130 | } |
| 131 | if (up->map.virt) { |
David S. Miller | 0e52fe8 | 2008-08-29 17:41:36 -0700 | [diff] [blame] | 132 | of_iounmap(&op->resource[0], up->map.virt, up->map.size); |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 133 | up->map.virt = NULL; |
| 134 | } |
| 135 | |
| 136 | kfree(up); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 141 | static const struct of_device_id uflash_match[] = { |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 142 | { |
| 143 | .name = UFLASH_OBPNAME, |
| 144 | }, |
| 145 | {}, |
| 146 | }; |
| 147 | |
| 148 | MODULE_DEVICE_TABLE(of, uflash_match); |
| 149 | |
Grant Likely | 1c48a5c | 2011-02-17 02:43:24 -0700 | [diff] [blame] | 150 | static struct platform_driver uflash_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 151 | .driver = { |
| 152 | .name = DRIVER_NAME, |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 153 | .of_match_table = uflash_match, |
| 154 | }, |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 155 | .probe = uflash_probe, |
Bill Pemberton | 5153b88 | 2012-11-19 13:21:24 -0500 | [diff] [blame] | 156 | .remove = uflash_remove, |
David S. Miller | 29f7ac7 | 2006-06-24 23:27:00 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Axel Lin | f99640d | 2011-11-27 20:45:03 +0800 | [diff] [blame] | 159 | module_platform_driver(uflash_driver); |