]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/char/tile-srom.c
Merge branch 'clockevents/4.13-fixes' of http://git.linaro.org/people/daniel.lezcano...
[mirror_ubuntu-artful-kernel.git] / drivers / char / tile-srom.c
1 /*
2 * Copyright 2011 Tilera Corporation. All Rights Reserved.
3 *
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.
7 *
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.
13 *
14 * SPI Flash ROM driver
15 *
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.
19 */
20
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>
39
40 /*
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.
46 */
47 #define SROM_CHUNK_SIZE ((size_t)4096)
48
49 /*
50 * When hypervisor is busy (e.g. erasing), poll the status periodically.
51 */
52
53 /*
54 * Interval to poll the state in msec
55 */
56 #define SROM_WAIT_TRY_INTERVAL 20
57
58 /*
59 * Maximum times to poll the state
60 */
61 #define SROM_MAX_WAIT_TRY_TIMES 1000
62
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 */
69 };
70
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");
75
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;
81
82 /*
83 * Handle calling the hypervisor and managing EAGAIN/EBUSY.
84 */
85
86 static ssize_t _srom_read(int hv_devhdl, void *buf,
87 loff_t off, size_t count)
88 {
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;
100 }
101 pr_err("_srom_read: error %d\n", retval);
102 return -EIO;
103 }
104 }
105
106 static ssize_t _srom_write(int hv_devhdl, const void *buf,
107 loff_t off, size_t count)
108 {
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;
120 }
121 pr_err("_srom_write: error %d\n", retval);
122 return -EIO;
123 }
124 }
125
126 /**
127 * srom_open() - Device open routine.
128 * @inode: Inode for this device.
129 * @filp: File for this specific open of the device.
130 *
131 * Returns zero, or an error code.
132 */
133 static int srom_open(struct inode *inode, struct file *filp)
134 {
135 filp->private_data = &srom_devices[iminor(inode)];
136 return 0;
137 }
138
139
140 /**
141 * srom_release() - Device release routine.
142 * @inode: Inode for this device.
143 * @filp: File for this specific open of the device.
144 *
145 * Returns zero, or an error code.
146 */
147 static int srom_release(struct inode *inode, struct file *filp)
148 {
149 struct srom_dev *srom = filp->private_data;
150 char dummy;
151
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);
157
158 filp->private_data = NULL;
159
160 return 0;
161 }
162
163
164 /**
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.
170 *
171 * Returns number of bytes read, or an error code.
172 */
173 static ssize_t srom_read(struct file *filp, char __user *buf,
174 size_t count, loff_t *f_pos)
175 {
176 int retval = 0;
177 void *kernbuf;
178 struct srom_dev *srom = filp->private_data;
179
180 kernbuf = kmalloc(SROM_CHUNK_SIZE, GFP_KERNEL);
181 if (!kernbuf)
182 return -ENOMEM;
183
184 if (mutex_lock_interruptible(&srom->lock)) {
185 retval = -ERESTARTSYS;
186 kfree(kernbuf);
187 return retval;
188 }
189
190 while (count) {
191 int hv_retval;
192 int bytes_this_pass = min(count, SROM_CHUNK_SIZE);
193
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;
200 }
201
202 if (copy_to_user(buf, kernbuf, hv_retval) != 0) {
203 retval = -EFAULT;
204 break;
205 }
206
207 retval += hv_retval;
208 *f_pos += hv_retval;
209 buf += hv_retval;
210 count -= hv_retval;
211 }
212
213 mutex_unlock(&srom->lock);
214 kfree(kernbuf);
215
216 return retval;
217 }
218
219 /**
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.
225 *
226 * Returns number of bytes written, or an error code.
227 */
228 static ssize_t srom_write(struct file *filp, const char __user *buf,
229 size_t count, loff_t *f_pos)
230 {
231 int retval = 0;
232 void *kernbuf;
233 struct srom_dev *srom = filp->private_data;
234
235 kernbuf = kmalloc(SROM_CHUNK_SIZE, GFP_KERNEL);
236 if (!kernbuf)
237 return -ENOMEM;
238
239 if (mutex_lock_interruptible(&srom->lock)) {
240 retval = -ERESTARTSYS;
241 kfree(kernbuf);
242 return retval;
243 }
244
245 while (count) {
246 int hv_retval;
247 int bytes_this_pass = min(count, SROM_CHUNK_SIZE);
248
249 if (copy_from_user(kernbuf, buf, bytes_this_pass) != 0) {
250 retval = -EFAULT;
251 break;
252 }
253
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;
260 }
261
262 retval += hv_retval;
263 *f_pos += hv_retval;
264 buf += hv_retval;
265 count -= hv_retval;
266 }
267
268 mutex_unlock(&srom->lock);
269 kfree(kernbuf);
270
271 return retval;
272 }
273
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)
276 {
277 struct srom_dev *srom = file->private_data;
278 return fixed_size_llseek(file, offset, origin, srom->total_size);
279 }
280
281 static ssize_t total_size_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
283 {
284 struct srom_dev *srom = dev_get_drvdata(dev);
285 return sprintf(buf, "%u\n", srom->total_size);
286 }
287 static DEVICE_ATTR_RO(total_size);
288
289 static ssize_t sector_size_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291 {
292 struct srom_dev *srom = dev_get_drvdata(dev);
293 return sprintf(buf, "%u\n", srom->sector_size);
294 }
295 static DEVICE_ATTR_RO(sector_size);
296
297 static ssize_t page_size_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
299 {
300 struct srom_dev *srom = dev_get_drvdata(dev);
301 return sprintf(buf, "%u\n", srom->page_size);
302 }
303 static DEVICE_ATTR_RO(page_size);
304
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,
310 };
311 ATTRIBUTE_GROUPS(srom_dev);
312
313 static char *srom_devnode(struct device *dev, umode_t *mode)
314 {
315 if (mode)
316 *mode = 0644;
317 return kasprintf(GFP_KERNEL, "srom/%s", dev_name(dev));
318 }
319
320 /*
321 * The fops
322 */
323 static const struct file_operations srom_fops = {
324 .owner = THIS_MODULE,
325 .llseek = srom_llseek,
326 .read = srom_read,
327 .write = srom_write,
328 .open = srom_open,
329 .release = srom_release,
330 };
331
332 /**
333 * srom_setup_minor() - Initialize per-minor information.
334 * @srom: Per-device SROM state.
335 * @devhdl: Partition device handle.
336 */
337 static int srom_setup_minor(struct srom_dev *srom, int devhdl)
338 {
339 srom->hv_devhdl = devhdl;
340 mutex_init(&srom->lock);
341
342 if (_srom_read(devhdl, &srom->total_size,
343 SROM_TOTAL_SIZE_OFF, sizeof(srom->total_size)) < 0)
344 return -EIO;
345 if (_srom_read(devhdl, &srom->sector_size,
346 SROM_SECTOR_SIZE_OFF, sizeof(srom->sector_size)) < 0)
347 return -EIO;
348 if (_srom_read(devhdl, &srom->page_size,
349 SROM_PAGE_SIZE_OFF, sizeof(srom->page_size)) < 0)
350 return -EIO;
351
352 return 0;
353 }
354
355 /** srom_init() - Initialize the driver's module. */
356 static int srom_init(void)
357 {
358 int result, i;
359 dev_t dev = MKDEV(srom_major, 0);
360
361 /*
362 * Start with a plausible number of partitions; the krealloc() call
363 * below will yield about log(srom_devs) additional allocations.
364 */
365 srom_devices = kmalloc(4 * sizeof(struct srom_dev), GFP_KERNEL);
366
367 /* Discover the number of srom partitions. */
368 for (i = 0; ; i++) {
369 int devhdl;
370 char buf[20];
371 struct srom_dev *new_srom_devices =
372 krealloc(srom_devices, (i+1) * sizeof(struct srom_dev),
373 GFP_KERNEL);
374 if (!new_srom_devices) {
375 result = -ENOMEM;
376 goto fail_mem;
377 }
378 srom_devices = new_srom_devices;
379 sprintf(buf, "srom/0/%d", i);
380 devhdl = hv_dev_open((HV_VirtAddr)buf, 0);
381 if (devhdl < 0) {
382 if (devhdl != HV_ENODEV)
383 pr_notice("srom/%d: hv_dev_open failed: %d.\n",
384 i, devhdl);
385 break;
386 }
387 result = srom_setup_minor(&srom_devices[i], devhdl);
388 if (result != 0)
389 goto fail_mem;
390 }
391 srom_devs = i;
392
393 /* Bail out early if we have no partitions at all. */
394 if (srom_devs == 0) {
395 result = -ENODEV;
396 goto fail_mem;
397 }
398
399 /* Register our major, and accept a dynamic number. */
400 if (srom_major)
401 result = register_chrdev_region(dev, srom_devs, "srom");
402 else {
403 result = alloc_chrdev_region(&dev, 0, srom_devs, "srom");
404 srom_major = MAJOR(dev);
405 }
406 if (result < 0)
407 goto fail_mem;
408
409 /* Register a character device. */
410 cdev_init(&srom_cdev, &srom_fops);
411 srom_cdev.owner = THIS_MODULE;
412 srom_cdev.ops = &srom_fops;
413 result = cdev_add(&srom_cdev, dev, srom_devs);
414 if (result < 0)
415 goto fail_chrdev;
416
417 /* Create a parent device */
418 srom_parent = platform_device_register_simple("srom", -1, NULL, 0);
419 if (IS_ERR(srom_parent)) {
420 result = PTR_ERR(srom_parent);
421 goto fail_pdev;
422 }
423
424 /* Create a sysfs class. */
425 srom_class = class_create(THIS_MODULE, "srom");
426 if (IS_ERR(srom_class)) {
427 result = PTR_ERR(srom_class);
428 goto fail_cdev;
429 }
430 srom_class->dev_groups = srom_dev_groups;
431 srom_class->devnode = srom_devnode;
432
433 /* Create per-partition devices */
434 for (i = 0; i < srom_devs; i++) {
435 struct device *dev =
436 device_create(srom_class, &srom_parent->dev,
437 MKDEV(srom_major, i), srom_devices + i,
438 "%d", i);
439 result = PTR_ERR_OR_ZERO(dev);
440 if (result < 0)
441 goto fail_class;
442 }
443
444 return 0;
445
446 fail_class:
447 for (i = 0; i < srom_devs; i++)
448 device_destroy(srom_class, MKDEV(srom_major, i));
449 class_destroy(srom_class);
450 fail_cdev:
451 platform_device_unregister(srom_parent);
452 fail_pdev:
453 cdev_del(&srom_cdev);
454 fail_chrdev:
455 unregister_chrdev_region(dev, srom_devs);
456 fail_mem:
457 kfree(srom_devices);
458 return result;
459 }
460
461 /** srom_cleanup() - Clean up the driver's module. */
462 static void srom_cleanup(void)
463 {
464 int i;
465 for (i = 0; i < srom_devs; i++)
466 device_destroy(srom_class, MKDEV(srom_major, i));
467 class_destroy(srom_class);
468 cdev_del(&srom_cdev);
469 platform_device_unregister(srom_parent);
470 unregister_chrdev_region(MKDEV(srom_major, 0), srom_devs);
471 kfree(srom_devices);
472 }
473
474 module_init(srom_init);
475 module_exit(srom_cleanup);