blob: 8984236a8d0a5f93f4d31ba0d26d81abdcd9dea2 [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>
14#include <linux/init.h>
15#include <linux/ioport.h>
David S. Miller0e52fe82008-08-29 17:41:36 -070016#include <linux/of.h>
17#include <linux/of_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
David S. Miller29f7ac72006-06-24 23:27:00 -070019#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
21#include <asm/io.h>
22
23#include <linux/mtd/mtd.h>
24#include <linux/mtd/map.h>
25
26#define UFLASH_OBPNAME "flashprom"
David S. Miller0e52fe82008-08-29 17:41:36 -070027#define DRIVER_NAME "sun_uflash"
28#define PFX DRIVER_NAME ": "
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#define UFLASH_WINDOW_SIZE 0x200000
31#define UFLASH_BUSWIDTH 1 /* EBus is 8-bit */
32
David S. Miller29f7ac72006-06-24 23:27:00 -070033MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
34MODULE_DESCRIPTION("User-programmable flash device on Sun Microsystems boardsets");
David S. Miller0e52fe82008-08-29 17:41:36 -070035MODULE_SUPPORTED_DEVICE(DRIVER_NAME);
David S. Miller29f7ac72006-06-24 23:27:00 -070036MODULE_LICENSE("GPL");
David S. Miller0e52fe82008-08-29 17:41:36 -070037MODULE_VERSION("2.1");
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039struct uflash_dev {
Stephen Rothwellccf0dec2007-03-29 00:49:54 -070040 const char *name; /* device name */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 struct map_info map; /* mtd map info */
David S. Miller29f7ac72006-06-24 23:27:00 -070042 struct mtd_info *mtd; /* mtd info */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045struct map_info uflash_map_templ = {
David S. Miller29f7ac72006-06-24 23:27:00 -070046 .name = "SUNW,???-????",
47 .size = UFLASH_WINDOW_SIZE,
48 .bankwidth = UFLASH_BUSWIDTH,
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
David S. Miller0e52fe82008-08-29 17:41:36 -070051int uflash_devinit(struct of_device *op, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
David S. Miller29f7ac72006-06-24 23:27:00 -070053 struct uflash_dev *up;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
David S. Miller0e52fe82008-08-29 17:41:36 -070055 if (op->resource[1].flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /* Non-CFI userflash device-- once I find one we
57 * can work on supporting it.
58 */
David S. Miller0e52fe82008-08-29 17:41:36 -070059 printk(KERN_ERR PFX "Unsupported device at %s, 0x%llx\n",
60 dp->full_name, (unsigned long long)op->resource[0].start);
David S. Miller29f7ac72006-06-24 23:27:00 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 return -ENODEV;
63 }
64
David S. Miller29f7ac72006-06-24 23:27:00 -070065 up = kzalloc(sizeof(struct uflash_dev), GFP_KERNEL);
David S. Miller0e52fe82008-08-29 17:41:36 -070066 if (!up) {
67 printk(KERN_ERR PFX "Cannot allocate struct uflash_dev\n");
David S. Miller29f7ac72006-06-24 23:27:00 -070068 return -ENOMEM;
David S. Miller0e52fe82008-08-29 17:41:36 -070069 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +000070
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 /* copy defaults and tweak parameters */
David S. Miller29f7ac72006-06-24 23:27:00 -070072 memcpy(&up->map, &uflash_map_templ, sizeof(uflash_map_templ));
David S. Miller0e52fe82008-08-29 17:41:36 -070073
74 up->map.size = resource_size(&op->resource[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David S. Miller29f7ac72006-06-24 23:27:00 -070076 up->name = of_get_property(dp, "model", NULL);
77 if (up->name && 0 < strlen(up->name))
Stephen Rothwellccf0dec2007-03-29 00:49:54 -070078 up->map.name = (char *)up->name;
David S. Miller29f7ac72006-06-24 23:27:00 -070079
David S. Miller0e52fe82008-08-29 17:41:36 -070080 up->map.phys = op->resource[0].start;
David S. Miller29f7ac72006-06-24 23:27:00 -070081
David S. Miller0e52fe82008-08-29 17:41:36 -070082 up->map.virt = of_ioremap(&op->resource[0], 0, up->map.size,
83 DRIVER_NAME);
David S. Miller29f7ac72006-06-24 23:27:00 -070084 if (!up->map.virt) {
David S. Miller0e52fe82008-08-29 17:41:36 -070085 printk(KERN_ERR PFX "Failed to map device.\n");
David S. Miller29f7ac72006-06-24 23:27:00 -070086 kfree(up);
87
88 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90
David S. Miller29f7ac72006-06-24 23:27:00 -070091 simple_map_init(&up->map);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /* MTD registration */
David S. Miller29f7ac72006-06-24 23:27:00 -070094 up->mtd = do_map_probe("cfi_probe", &up->map);
95 if (!up->mtd) {
David S. Miller0e52fe82008-08-29 17:41:36 -070096 of_iounmap(&op->resource[0], up->map.virt, up->map.size);
David S. Miller29f7ac72006-06-24 23:27:00 -070097 kfree(up);
98
99 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
David S. Miller29f7ac72006-06-24 23:27:00 -0700102 up->mtd->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
David S. Miller29f7ac72006-06-24 23:27:00 -0700104 add_mtd_device(up->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
David S. Miller0e52fe82008-08-29 17:41:36 -0700106 dev_set_drvdata(&op->dev, up);
David S. Miller29f7ac72006-06-24 23:27:00 -0700107
108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
David S. Miller0e52fe82008-08-29 17:41:36 -0700111static int __devinit uflash_probe(struct of_device *op, const struct of_device_id *match)
David S. Miller29f7ac72006-06-24 23:27:00 -0700112{
Grant Likely61c7a082010-04-13 16:12:29 -0700113 struct device_node *dp = op->dev.of_node;
David S. Miller29f7ac72006-06-24 23:27:00 -0700114
David S. Miller0e52fe82008-08-29 17:41:36 -0700115 /* Flashprom must have the "user" property in order to
116 * be used by this driver.
117 */
118 if (!of_find_property(dp, "user", NULL))
David S. Miller29f7ac72006-06-24 23:27:00 -0700119 return -ENODEV;
120
David S. Miller0e52fe82008-08-29 17:41:36 -0700121 return uflash_devinit(op, dp);
David S. Miller29f7ac72006-06-24 23:27:00 -0700122}
123
David S. Miller0e52fe82008-08-29 17:41:36 -0700124static int __devexit uflash_remove(struct of_device *op)
David S. Miller29f7ac72006-06-24 23:27:00 -0700125{
David S. Miller0e52fe82008-08-29 17:41:36 -0700126 struct uflash_dev *up = dev_get_drvdata(&op->dev);
David S. Miller29f7ac72006-06-24 23:27:00 -0700127
128 if (up->mtd) {
129 del_mtd_device(up->mtd);
130 map_destroy(up->mtd);
131 }
132 if (up->map.virt) {
David S. Miller0e52fe82008-08-29 17:41:36 -0700133 of_iounmap(&op->resource[0], up->map.virt, up->map.size);
David S. Miller29f7ac72006-06-24 23:27:00 -0700134 up->map.virt = NULL;
135 }
136
137 kfree(up);
138
139 return 0;
140}
141
David S. Millerfd098312008-08-31 01:23:17 -0700142static const struct of_device_id uflash_match[] = {
David S. Miller29f7ac72006-06-24 23:27:00 -0700143 {
144 .name = UFLASH_OBPNAME,
145 },
146 {},
147};
148
149MODULE_DEVICE_TABLE(of, uflash_match);
150
151static struct of_platform_driver uflash_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700152 .driver = {
153 .name = DRIVER_NAME,
154 .owner = THIS_MODULE,
155 .of_match_table = uflash_match,
156 },
David S. Miller29f7ac72006-06-24 23:27:00 -0700157 .probe = uflash_probe,
158 .remove = __devexit_p(uflash_remove),
159};
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static int __init uflash_init(void)
162{
Grant Likely1ab1d632010-06-24 15:14:37 -0600163 return of_register_platform_driver(&uflash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
David S. Miller29f7ac72006-06-24 23:27:00 -0700166static void __exit uflash_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Grant Likely1ab1d632010-06-24 15:14:37 -0600168 of_unregister_platform_driver(&uflash_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171module_init(uflash_init);
David S. Miller29f7ac72006-06-24 23:27:00 -0700172module_exit(uflash_exit);