blob: d459aca07881d6687045463481a3d51757f39df2 [file] [log] [blame]
David S. Miller0e52fe82008-08-29 17:41:36 -07001/* sun_uflash.c - Driver for user-programmable flash on
2 * Sun Microsystems SME boardsets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/fs.h>
13#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/ioport.h>
David S. Miller0e52fe82008-08-29 17:41:36 -070015#include <linux/of.h>
16#include <linux/of_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David S. Miller29f7ac72006-06-24 23:27:00 -070018#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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. Miller0e52fe82008-08-29 17:41:36 -070026#define DRIVER_NAME "sun_uflash"
27#define PFX DRIVER_NAME ": "
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#define UFLASH_WINDOW_SIZE 0x200000
30#define UFLASH_BUSWIDTH 1 /* EBus is 8-bit */
31
David S. Miller29f7ac72006-06-24 23:27:00 -070032MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
33MODULE_DESCRIPTION("User-programmable flash device on Sun Microsystems boardsets");
David S. Miller0e52fe82008-08-29 17:41:36 -070034MODULE_SUPPORTED_DEVICE(DRIVER_NAME);
David S. Miller29f7ac72006-06-24 23:27:00 -070035MODULE_LICENSE("GPL");
David S. Miller0e52fe82008-08-29 17:41:36 -070036MODULE_VERSION("2.1");
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038struct uflash_dev {
Stephen Rothwellccf0dec2007-03-29 00:49:54 -070039 const char *name; /* device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct map_info map; /* mtd map info */
David S. Miller29f7ac72006-06-24 23:27:00 -070041 struct mtd_info *mtd; /* mtd info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044struct map_info uflash_map_templ = {
David S. Miller29f7ac72006-06-24 23:27:00 -070045 .name = "SUNW,???-????",
46 .size = UFLASH_WINDOW_SIZE,
47 .bankwidth = UFLASH_BUSWIDTH,
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Grant Likely2dc11582010-08-06 09:25:50 -060050int uflash_devinit(struct platform_device *op, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
David S. Miller29f7ac72006-06-24 23:27:00 -070052 struct uflash_dev *up;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David S. Miller0e52fe82008-08-29 17:41:36 -070054 if (op->resource[1].flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /* Non-CFI userflash device-- once I find one we
56 * can work on supporting it.
57 */
David S. Miller0e52fe82008-08-29 17:41:36 -070058 printk(KERN_ERR PFX "Unsupported device at %s, 0x%llx\n",
59 dp->full_name, (unsigned long long)op->resource[0].start);
David S. Miller29f7ac72006-06-24 23:27:00 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 return -ENODEV;
62 }
63
David S. Miller29f7ac72006-06-24 23:27:00 -070064 up = kzalloc(sizeof(struct uflash_dev), GFP_KERNEL);
David S. Miller0e52fe82008-08-29 17:41:36 -070065 if (!up) {
66 printk(KERN_ERR PFX "Cannot allocate struct uflash_dev\n");
David S. Miller29f7ac72006-06-24 23:27:00 -070067 return -ENOMEM;
David S. Miller0e52fe82008-08-29 17:41:36 -070068 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +000069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 /* copy defaults and tweak parameters */
David S. Miller29f7ac72006-06-24 23:27:00 -070071 memcpy(&up->map, &uflash_map_templ, sizeof(uflash_map_templ));
David S. Miller0e52fe82008-08-29 17:41:36 -070072
73 up->map.size = resource_size(&op->resource[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
David S. Miller29f7ac72006-06-24 23:27:00 -070075 up->name = of_get_property(dp, "model", NULL);
76 if (up->name && 0 < strlen(up->name))
Geert Uytterhoeven7c4bb4f2013-11-12 20:07:15 +010077 up->map.name = up->name;
David S. Miller29f7ac72006-06-24 23:27:00 -070078
David S. Miller0e52fe82008-08-29 17:41:36 -070079 up->map.phys = op->resource[0].start;
David S. Miller29f7ac72006-06-24 23:27:00 -070080
David S. Miller0e52fe82008-08-29 17:41:36 -070081 up->map.virt = of_ioremap(&op->resource[0], 0, up->map.size,
82 DRIVER_NAME);
David S. Miller29f7ac72006-06-24 23:27:00 -070083 if (!up->map.virt) {
David S. Miller0e52fe82008-08-29 17:41:36 -070084 printk(KERN_ERR PFX "Failed to map device.\n");
David S. Miller29f7ac72006-06-24 23:27:00 -070085 kfree(up);
86
87 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
89
David S. Miller29f7ac72006-06-24 23:27:00 -070090 simple_map_init(&up->map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* MTD registration */
David S. Miller29f7ac72006-06-24 23:27:00 -070093 up->mtd = do_map_probe("cfi_probe", &up->map);
94 if (!up->mtd) {
David S. Miller0e52fe82008-08-29 17:41:36 -070095 of_iounmap(&op->resource[0], up->map.virt, up->map.size);
David S. Miller29f7ac72006-06-24 23:27:00 -070096 kfree(up);
97
98 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
David S. Miller29f7ac72006-06-24 23:27:00 -0700101 up->mtd->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100103 mtd_device_register(up->mtd, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
David S. Miller0e52fe82008-08-29 17:41:36 -0700105 dev_set_drvdata(&op->dev, up);
David S. Miller29f7ac72006-06-24 23:27:00 -0700106
107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Bill Pemberton06f25512012-11-19 13:23:07 -0500110static int uflash_probe(struct platform_device *op)
David S. Miller29f7ac72006-06-24 23:27:00 -0700111{
Grant Likely61c7a082010-04-13 16:12:29 -0700112 struct device_node *dp = op->dev.of_node;
David S. Miller29f7ac72006-06-24 23:27:00 -0700113
David S. Miller0e52fe82008-08-29 17:41:36 -0700114 /* 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. Miller29f7ac72006-06-24 23:27:00 -0700118 return -ENODEV;
119
David S. Miller0e52fe82008-08-29 17:41:36 -0700120 return uflash_devinit(op, dp);
David S. Miller29f7ac72006-06-24 23:27:00 -0700121}
122
Bill Pemberton810b7e02012-11-19 13:26:04 -0500123static int uflash_remove(struct platform_device *op)
David S. Miller29f7ac72006-06-24 23:27:00 -0700124{
David S. Miller0e52fe82008-08-29 17:41:36 -0700125 struct uflash_dev *up = dev_get_drvdata(&op->dev);
David S. Miller29f7ac72006-06-24 23:27:00 -0700126
127 if (up->mtd) {
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100128 mtd_device_unregister(up->mtd);
David S. Miller29f7ac72006-06-24 23:27:00 -0700129 map_destroy(up->mtd);
130 }
131 if (up->map.virt) {
David S. Miller0e52fe82008-08-29 17:41:36 -0700132 of_iounmap(&op->resource[0], up->map.virt, up->map.size);
David S. Miller29f7ac72006-06-24 23:27:00 -0700133 up->map.virt = NULL;
134 }
135
136 kfree(up);
137
138 return 0;
139}
140
David S. Millerfd098312008-08-31 01:23:17 -0700141static const struct of_device_id uflash_match[] = {
David S. Miller29f7ac72006-06-24 23:27:00 -0700142 {
143 .name = UFLASH_OBPNAME,
144 },
145 {},
146};
147
148MODULE_DEVICE_TABLE(of, uflash_match);
149
Grant Likely1c48a5c2011-02-17 02:43:24 -0700150static struct platform_driver uflash_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700151 .driver = {
152 .name = DRIVER_NAME,
Grant Likely40182942010-04-13 16:13:02 -0700153 .of_match_table = uflash_match,
154 },
David S. Miller29f7ac72006-06-24 23:27:00 -0700155 .probe = uflash_probe,
Bill Pemberton5153b882012-11-19 13:21:24 -0500156 .remove = uflash_remove,
David S. Miller29f7ac72006-06-24 23:27:00 -0700157};
158
Axel Linf99640d2011-11-27 20:45:03 +0800159module_platform_driver(uflash_driver);