pvclock: introduce seqcount-like API
[linux/fpc-iii.git] / drivers / char / tile-srom.c
blob69f6b4acc377143d87a54d5e46dd9710f4d53f73
1 /*
2 * Copyright 2011 Tilera Corporation. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
14 * SPI Flash ROM driver
16 * This source code is derived from code provided in "Linux Device
17 * Drivers, Third Edition", by Jonathan Corbet, Alessandro Rubini, and
18 * Greg Kroah-Hartman, published by O'Reilly Media, Inc.
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/kernel.h> /* printk() */
24 #include <linux/slab.h> /* kmalloc() */
25 #include <linux/fs.h> /* everything... */
26 #include <linux/errno.h> /* error codes */
27 #include <linux/types.h> /* size_t */
28 #include <linux/proc_fs.h>
29 #include <linux/fcntl.h> /* O_ACCMODE */
30 #include <linux/pagemap.h>
31 #include <linux/hugetlb.h>
32 #include <linux/uaccess.h>
33 #include <linux/platform_device.h>
34 #include <hv/hypervisor.h>
35 #include <linux/ioctl.h>
36 #include <linux/cdev.h>
37 #include <linux/delay.h>
38 #include <hv/drv_srom_intf.h>
41 * Size of our hypervisor I/O requests. We break up large transfers
42 * so that we don't spend large uninterrupted spans of time in the
43 * hypervisor. Erasing an SROM sector takes a significant fraction of
44 * a second, so if we allowed the user to, say, do one I/O to write the
45 * entire ROM, we'd get soft lockup timeouts, or worse.
47 #define SROM_CHUNK_SIZE ((size_t)4096)
50 * When hypervisor is busy (e.g. erasing), poll the status periodically.
54 * Interval to poll the state in msec
56 #define SROM_WAIT_TRY_INTERVAL 20
59 * Maximum times to poll the state
61 #define SROM_MAX_WAIT_TRY_TIMES 1000
63 struct srom_dev {
64 int hv_devhdl; /* Handle for hypervisor device */
65 u32 total_size; /* Size of this device */
66 u32 sector_size; /* Size of a sector */
67 u32 page_size; /* Size of a page */
68 struct mutex lock; /* Allow only one accessor at a time */
71 static int srom_major; /* Dynamic major by default */
72 module_param(srom_major, int, 0);
73 MODULE_AUTHOR("Tilera Corporation");
74 MODULE_LICENSE("GPL");
76 static int srom_devs; /* Number of SROM partitions */
77 static struct cdev srom_cdev;
78 static struct platform_device *srom_parent;
79 static struct class *srom_class;
80 static struct srom_dev *srom_devices;
83 * Handle calling the hypervisor and managing EAGAIN/EBUSY.
86 static ssize_t _srom_read(int hv_devhdl, void *buf,
87 loff_t off, size_t count)
89 int retval, retries = SROM_MAX_WAIT_TRY_TIMES;
90 for (;;) {
91 retval = hv_dev_pread(hv_devhdl, 0, (HV_VirtAddr)buf,
92 count, off);
93 if (retval >= 0)
94 return retval;
95 if (retval == HV_EAGAIN)
96 continue;
97 if (retval == HV_EBUSY && --retries > 0) {
98 msleep(SROM_WAIT_TRY_INTERVAL);
99 continue;
101 pr_err("_srom_read: error %d\n", retval);
102 return -EIO;
106 static ssize_t _srom_write(int hv_devhdl, const void *buf,
107 loff_t off, size_t count)
109 int retval, retries = SROM_MAX_WAIT_TRY_TIMES;
110 for (;;) {
111 retval = hv_dev_pwrite(hv_devhdl, 0, (HV_VirtAddr)buf,
112 count, off);
113 if (retval >= 0)
114 return retval;
115 if (retval == HV_EAGAIN)
116 continue;
117 if (retval == HV_EBUSY && --retries > 0) {
118 msleep(SROM_WAIT_TRY_INTERVAL);
119 continue;
121 pr_err("_srom_write: error %d\n", retval);
122 return -EIO;
127 * srom_open() - Device open routine.
128 * @inode: Inode for this device.
129 * @filp: File for this specific open of the device.
131 * Returns zero, or an error code.
133 static int srom_open(struct inode *inode, struct file *filp)
135 filp->private_data = &srom_devices[iminor(inode)];
136 return 0;
141 * srom_release() - Device release routine.
142 * @inode: Inode for this device.
143 * @filp: File for this specific open of the device.
145 * Returns zero, or an error code.
147 static int srom_release(struct inode *inode, struct file *filp)
149 struct srom_dev *srom = filp->private_data;
150 char dummy;
152 /* Make sure we've flushed anything written to the ROM. */
153 mutex_lock(&srom->lock);
154 if (srom->hv_devhdl >= 0)
155 _srom_write(srom->hv_devhdl, &dummy, SROM_FLUSH_OFF, 1);
156 mutex_unlock(&srom->lock);
158 filp->private_data = NULL;
160 return 0;
165 * srom_read() - Read data from the device.
166 * @filp: File for this specific open of the device.
167 * @buf: User's data buffer.
168 * @count: Number of bytes requested.
169 * @f_pos: File position.
171 * Returns number of bytes read, or an error code.
173 static ssize_t srom_read(struct file *filp, char __user *buf,
174 size_t count, loff_t *f_pos)
176 int retval = 0;
177 void *kernbuf;
178 struct srom_dev *srom = filp->private_data;
180 kernbuf = kmalloc(SROM_CHUNK_SIZE, GFP_KERNEL);
181 if (!kernbuf)
182 return -ENOMEM;
184 if (mutex_lock_interruptible(&srom->lock)) {
185 retval = -ERESTARTSYS;
186 kfree(kernbuf);
187 return retval;
190 while (count) {
191 int hv_retval;
192 int bytes_this_pass = min(count, SROM_CHUNK_SIZE);
194 hv_retval = _srom_read(srom->hv_devhdl, kernbuf,
195 *f_pos, bytes_this_pass);
196 if (hv_retval <= 0) {
197 if (retval == 0)
198 retval = hv_retval;
199 break;
202 if (copy_to_user(buf, kernbuf, hv_retval) != 0) {
203 retval = -EFAULT;
204 break;
207 retval += hv_retval;
208 *f_pos += hv_retval;
209 buf += hv_retval;
210 count -= hv_retval;
213 mutex_unlock(&srom->lock);
214 kfree(kernbuf);
216 return retval;
220 * srom_write() - Write data to the device.
221 * @filp: File for this specific open of the device.
222 * @buf: User's data buffer.
223 * @count: Number of bytes requested.
224 * @f_pos: File position.
226 * Returns number of bytes written, or an error code.
228 static ssize_t srom_write(struct file *filp, const char __user *buf,
229 size_t count, loff_t *f_pos)
231 int retval = 0;
232 void *kernbuf;
233 struct srom_dev *srom = filp->private_data;
235 kernbuf = kmalloc(SROM_CHUNK_SIZE, GFP_KERNEL);
236 if (!kernbuf)
237 return -ENOMEM;
239 if (mutex_lock_interruptible(&srom->lock)) {
240 retval = -ERESTARTSYS;
241 kfree(kernbuf);
242 return retval;
245 while (count) {
246 int hv_retval;
247 int bytes_this_pass = min(count, SROM_CHUNK_SIZE);
249 if (copy_from_user(kernbuf, buf, bytes_this_pass) != 0) {
250 retval = -EFAULT;
251 break;
254 hv_retval = _srom_write(srom->hv_devhdl, kernbuf,
255 *f_pos, bytes_this_pass);
256 if (hv_retval <= 0) {
257 if (retval == 0)
258 retval = hv_retval;
259 break;
262 retval += hv_retval;
263 *f_pos += hv_retval;
264 buf += hv_retval;
265 count -= hv_retval;
268 mutex_unlock(&srom->lock);
269 kfree(kernbuf);
271 return retval;
274 /* Provide our own implementation so we can use srom->total_size. */
275 loff_t srom_llseek(struct file *file, loff_t offset, int origin)
277 struct srom_dev *srom = file->private_data;
278 return fixed_size_llseek(file, offset, origin, srom->total_size);
281 static ssize_t total_size_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
284 struct srom_dev *srom = dev_get_drvdata(dev);
285 return sprintf(buf, "%u\n", srom->total_size);
287 static DEVICE_ATTR_RO(total_size);
289 static ssize_t sector_size_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct srom_dev *srom = dev_get_drvdata(dev);
293 return sprintf(buf, "%u\n", srom->sector_size);
295 static DEVICE_ATTR_RO(sector_size);
297 static ssize_t page_size_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
300 struct srom_dev *srom = dev_get_drvdata(dev);
301 return sprintf(buf, "%u\n", srom->page_size);
303 static DEVICE_ATTR_RO(page_size);
305 static struct attribute *srom_dev_attrs[] = {
306 &dev_attr_total_size.attr,
307 &dev_attr_sector_size.attr,
308 &dev_attr_page_size.attr,
309 NULL,
311 ATTRIBUTE_GROUPS(srom_dev);
313 static char *srom_devnode(struct device *dev, umode_t *mode)
315 *mode = S_IRUGO | S_IWUSR;
316 return kasprintf(GFP_KERNEL, "srom/%s", dev_name(dev));
320 * The fops
322 static const struct file_operations srom_fops = {
323 .owner = THIS_MODULE,
324 .llseek = srom_llseek,
325 .read = srom_read,
326 .write = srom_write,
327 .open = srom_open,
328 .release = srom_release,
332 * srom_setup_minor() - Initialize per-minor information.
333 * @srom: Per-device SROM state.
334 * @index: Device to set up.
336 static int srom_setup_minor(struct srom_dev *srom, int index)
338 struct device *dev;
339 int devhdl = srom->hv_devhdl;
341 mutex_init(&srom->lock);
343 if (_srom_read(devhdl, &srom->total_size,
344 SROM_TOTAL_SIZE_OFF, sizeof(srom->total_size)) < 0)
345 return -EIO;
346 if (_srom_read(devhdl, &srom->sector_size,
347 SROM_SECTOR_SIZE_OFF, sizeof(srom->sector_size)) < 0)
348 return -EIO;
349 if (_srom_read(devhdl, &srom->page_size,
350 SROM_PAGE_SIZE_OFF, sizeof(srom->page_size)) < 0)
351 return -EIO;
353 dev = device_create(srom_class, &srom_parent->dev,
354 MKDEV(srom_major, index), srom, "%d", index);
355 return PTR_ERR_OR_ZERO(dev);
358 /** srom_init() - Initialize the driver's module. */
359 static int srom_init(void)
361 int result, i;
362 dev_t dev = MKDEV(srom_major, 0);
365 * Start with a plausible number of partitions; the krealloc() call
366 * below will yield about log(srom_devs) additional allocations.
368 srom_devices = kzalloc(4 * sizeof(struct srom_dev), GFP_KERNEL);
370 /* Discover the number of srom partitions. */
371 for (i = 0; ; i++) {
372 int devhdl;
373 char buf[20];
374 struct srom_dev *new_srom_devices =
375 krealloc(srom_devices, (i+1) * sizeof(struct srom_dev),
376 GFP_KERNEL | __GFP_ZERO);
377 if (!new_srom_devices) {
378 result = -ENOMEM;
379 goto fail_mem;
381 srom_devices = new_srom_devices;
382 sprintf(buf, "srom/0/%d", i);
383 devhdl = hv_dev_open((HV_VirtAddr)buf, 0);
384 if (devhdl < 0) {
385 if (devhdl != HV_ENODEV)
386 pr_notice("srom/%d: hv_dev_open failed: %d.\n",
387 i, devhdl);
388 break;
390 srom_devices[i].hv_devhdl = devhdl;
392 srom_devs = i;
394 /* Bail out early if we have no partitions at all. */
395 if (srom_devs == 0) {
396 result = -ENODEV;
397 goto fail_mem;
400 /* Register our major, and accept a dynamic number. */
401 if (srom_major)
402 result = register_chrdev_region(dev, srom_devs, "srom");
403 else {
404 result = alloc_chrdev_region(&dev, 0, srom_devs, "srom");
405 srom_major = MAJOR(dev);
407 if (result < 0)
408 goto fail_mem;
410 /* Register a character device. */
411 cdev_init(&srom_cdev, &srom_fops);
412 srom_cdev.owner = THIS_MODULE;
413 srom_cdev.ops = &srom_fops;
414 result = cdev_add(&srom_cdev, dev, srom_devs);
415 if (result < 0)
416 goto fail_chrdev;
418 /* Create a parent device */
419 srom_parent = platform_device_register_simple("srom", -1, NULL, 0);
420 if (IS_ERR(srom_parent)) {
421 result = PTR_ERR(srom_parent);
422 goto fail_pdev;
425 /* Create a sysfs class. */
426 srom_class = class_create(THIS_MODULE, "srom");
427 if (IS_ERR(srom_class)) {
428 result = PTR_ERR(srom_class);
429 goto fail_cdev;
431 srom_class->dev_groups = srom_dev_groups;
432 srom_class->devnode = srom_devnode;
434 /* Do per-partition initialization */
435 for (i = 0; i < srom_devs; i++) {
436 result = srom_setup_minor(srom_devices + i, i);
437 if (result < 0)
438 goto fail_class;
441 return 0;
443 fail_class:
444 for (i = 0; i < srom_devs; i++)
445 device_destroy(srom_class, MKDEV(srom_major, i));
446 class_destroy(srom_class);
447 fail_cdev:
448 platform_device_unregister(srom_parent);
449 fail_pdev:
450 cdev_del(&srom_cdev);
451 fail_chrdev:
452 unregister_chrdev_region(dev, srom_devs);
453 fail_mem:
454 kfree(srom_devices);
455 return result;
458 /** srom_cleanup() - Clean up the driver's module. */
459 static void srom_cleanup(void)
461 int i;
462 for (i = 0; i < srom_devs; i++)
463 device_destroy(srom_class, MKDEV(srom_major, i));
464 class_destroy(srom_class);
465 cdev_del(&srom_cdev);
466 platform_device_unregister(srom_parent);
467 unregister_chrdev_region(MKDEV(srom_major, 0), srom_devs);
468 kfree(srom_devices);
471 module_init(srom_init);
472 module_exit(srom_cleanup);