tile-srom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/kernel.h> /* printk() */
  23. #include <linux/slab.h> /* kmalloc() */
  24. #include <linux/fs.h> /* everything... */
  25. #include <linux/errno.h> /* error codes */
  26. #include <linux/types.h> /* size_t */
  27. #include <linux/proc_fs.h>
  28. #include <linux/fcntl.h> /* O_ACCMODE */
  29. #include <linux/pagemap.h>
  30. #include <linux/hugetlb.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/platform_device.h>
  33. #include <hv/hypervisor.h>
  34. #include <linux/ioctl.h>
  35. #include <linux/cdev.h>
  36. #include <linux/delay.h>
  37. #include <hv/drv_srom_intf.h>
  38. /*
  39. * Size of our hypervisor I/O requests. We break up large transfers
  40. * so that we don't spend large uninterrupted spans of time in the
  41. * hypervisor. Erasing an SROM sector takes a significant fraction of
  42. * a second, so if we allowed the user to, say, do one I/O to write the
  43. * entire ROM, we'd get soft lockup timeouts, or worse.
  44. */
  45. #define SROM_CHUNK_SIZE ((size_t)4096)
  46. /*
  47. * When hypervisor is busy (e.g. erasing), poll the status periodically.
  48. */
  49. /*
  50. * Interval to poll the state in msec
  51. */
  52. #define SROM_WAIT_TRY_INTERVAL 20
  53. /*
  54. * Maximum times to poll the state
  55. */
  56. #define SROM_MAX_WAIT_TRY_TIMES 1000
  57. struct srom_dev {
  58. int hv_devhdl; /* Handle for hypervisor device */
  59. u32 total_size; /* Size of this device */
  60. u32 sector_size; /* Size of a sector */
  61. u32 page_size; /* Size of a page */
  62. struct mutex lock; /* Allow only one accessor at a time */
  63. };
  64. static int srom_major; /* Dynamic major by default */
  65. module_param(srom_major, int, 0);
  66. MODULE_AUTHOR("Tilera Corporation");
  67. MODULE_LICENSE("GPL");
  68. static int srom_devs; /* Number of SROM partitions */
  69. static struct cdev srom_cdev;
  70. static struct platform_device *srom_parent;
  71. static struct class *srom_class;
  72. static struct srom_dev *srom_devices;
  73. /*
  74. * Handle calling the hypervisor and managing EAGAIN/EBUSY.
  75. */
  76. static ssize_t _srom_read(int hv_devhdl, void *buf,
  77. loff_t off, size_t count)
  78. {
  79. int retval, retries = SROM_MAX_WAIT_TRY_TIMES;
  80. for (;;) {
  81. retval = hv_dev_pread(hv_devhdl, 0, (HV_VirtAddr)buf,
  82. count, off);
  83. if (retval >= 0)
  84. return retval;
  85. if (retval == HV_EAGAIN)
  86. continue;
  87. if (retval == HV_EBUSY && --retries > 0) {
  88. msleep(SROM_WAIT_TRY_INTERVAL);
  89. continue;
  90. }
  91. pr_err("_srom_read: error %d\n", retval);
  92. return -EIO;
  93. }
  94. }
  95. static ssize_t _srom_write(int hv_devhdl, const void *buf,
  96. loff_t off, size_t count)
  97. {
  98. int retval, retries = SROM_MAX_WAIT_TRY_TIMES;
  99. for (;;) {
  100. retval = hv_dev_pwrite(hv_devhdl, 0, (HV_VirtAddr)buf,
  101. count, off);
  102. if (retval >= 0)
  103. return retval;
  104. if (retval == HV_EAGAIN)
  105. continue;
  106. if (retval == HV_EBUSY && --retries > 0) {
  107. msleep(SROM_WAIT_TRY_INTERVAL);
  108. continue;
  109. }
  110. pr_err("_srom_write: error %d\n", retval);
  111. return -EIO;
  112. }
  113. }
  114. /**
  115. * srom_open() - Device open routine.
  116. * @inode: Inode for this device.
  117. * @filp: File for this specific open of the device.
  118. *
  119. * Returns zero, or an error code.
  120. */
  121. static int srom_open(struct inode *inode, struct file *filp)
  122. {
  123. filp->private_data = &srom_devices[iminor(inode)];
  124. return 0;
  125. }
  126. /**
  127. * srom_release() - Device release 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_release(struct inode *inode, struct file *filp)
  134. {
  135. struct srom_dev *srom = filp->private_data;
  136. char dummy;
  137. /* Make sure we've flushed anything written to the ROM. */
  138. mutex_lock(&srom->lock);
  139. if (srom->hv_devhdl >= 0)
  140. _srom_write(srom->hv_devhdl, &dummy, SROM_FLUSH_OFF, 1);
  141. mutex_unlock(&srom->lock);
  142. filp->private_data = NULL;
  143. return 0;
  144. }
  145. /**
  146. * srom_read() - Read data from the device.
  147. * @filp: File for this specific open of the device.
  148. * @buf: User's data buffer.
  149. * @count: Number of bytes requested.
  150. * @f_pos: File position.
  151. *
  152. * Returns number of bytes read, or an error code.
  153. */
  154. static ssize_t srom_read(struct file *filp, char __user *buf,
  155. size_t count, loff_t *f_pos)
  156. {
  157. int retval = 0;
  158. void *kernbuf;
  159. struct srom_dev *srom = filp->private_data;
  160. kernbuf = kmalloc(SROM_CHUNK_SIZE, GFP_KERNEL);
  161. if (!kernbuf)
  162. return -ENOMEM;
  163. if (mutex_lock_interruptible(&srom->lock)) {
  164. retval = -ERESTARTSYS;
  165. kfree(kernbuf);
  166. return retval;
  167. }
  168. while (count) {
  169. int hv_retval;
  170. int bytes_this_pass = min(count, SROM_CHUNK_SIZE);
  171. hv_retval = _srom_read(srom->hv_devhdl, kernbuf,
  172. *f_pos, bytes_this_pass);
  173. if (hv_retval <= 0) {
  174. if (retval == 0)
  175. retval = hv_retval;
  176. break;
  177. }
  178. if (copy_to_user(buf, kernbuf, hv_retval) != 0) {
  179. retval = -EFAULT;
  180. break;
  181. }
  182. retval += hv_retval;
  183. *f_pos += hv_retval;
  184. buf += hv_retval;
  185. count -= hv_retval;
  186. }
  187. mutex_unlock(&srom->lock);
  188. kfree(kernbuf);
  189. return retval;
  190. }
  191. /**
  192. * srom_write() - Write data to the device.
  193. * @filp: File for this specific open of the device.
  194. * @buf: User's data buffer.
  195. * @count: Number of bytes requested.
  196. * @f_pos: File position.
  197. *
  198. * Returns number of bytes written, or an error code.
  199. */
  200. static ssize_t srom_write(struct file *filp, const char __user *buf,
  201. size_t count, loff_t *f_pos)
  202. {
  203. int retval = 0;
  204. void *kernbuf;
  205. struct srom_dev *srom = filp->private_data;
  206. kernbuf = kmalloc(SROM_CHUNK_SIZE, GFP_KERNEL);
  207. if (!kernbuf)
  208. return -ENOMEM;
  209. if (mutex_lock_interruptible(&srom->lock)) {
  210. retval = -ERESTARTSYS;
  211. kfree(kernbuf);
  212. return retval;
  213. }
  214. while (count) {
  215. int hv_retval;
  216. int bytes_this_pass = min(count, SROM_CHUNK_SIZE);
  217. if (copy_from_user(kernbuf, buf, bytes_this_pass) != 0) {
  218. retval = -EFAULT;
  219. break;
  220. }
  221. hv_retval = _srom_write(srom->hv_devhdl, kernbuf,
  222. *f_pos, bytes_this_pass);
  223. if (hv_retval <= 0) {
  224. if (retval == 0)
  225. retval = hv_retval;
  226. break;
  227. }
  228. retval += hv_retval;
  229. *f_pos += hv_retval;
  230. buf += hv_retval;
  231. count -= hv_retval;
  232. }
  233. mutex_unlock(&srom->lock);
  234. kfree(kernbuf);
  235. return retval;
  236. }
  237. /* Provide our own implementation so we can use srom->total_size. */
  238. loff_t srom_llseek(struct file *file, loff_t offset, int origin)
  239. {
  240. struct srom_dev *srom = file->private_data;
  241. return fixed_size_llseek(file, offset, origin, srom->total_size);
  242. }
  243. static ssize_t total_size_show(struct device *dev,
  244. struct device_attribute *attr, char *buf)
  245. {
  246. struct srom_dev *srom = dev_get_drvdata(dev);
  247. return sprintf(buf, "%u\n", srom->total_size);
  248. }
  249. static DEVICE_ATTR_RO(total_size);
  250. static ssize_t sector_size_show(struct device *dev,
  251. struct device_attribute *attr, char *buf)
  252. {
  253. struct srom_dev *srom = dev_get_drvdata(dev);
  254. return sprintf(buf, "%u\n", srom->sector_size);
  255. }
  256. static DEVICE_ATTR_RO(sector_size);
  257. static ssize_t page_size_show(struct device *dev,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. struct srom_dev *srom = dev_get_drvdata(dev);
  261. return sprintf(buf, "%u\n", srom->page_size);
  262. }
  263. static DEVICE_ATTR_RO(page_size);
  264. static struct attribute *srom_dev_attrs[] = {
  265. &dev_attr_total_size.attr,
  266. &dev_attr_sector_size.attr,
  267. &dev_attr_page_size.attr,
  268. NULL,
  269. };
  270. ATTRIBUTE_GROUPS(srom_dev);
  271. static char *srom_devnode(struct device *dev, umode_t *mode)
  272. {
  273. *mode = S_IRUGO | S_IWUSR;
  274. return kasprintf(GFP_KERNEL, "srom/%s", dev_name(dev));
  275. }
  276. /*
  277. * The fops
  278. */
  279. static const struct file_operations srom_fops = {
  280. .owner = THIS_MODULE,
  281. .llseek = srom_llseek,
  282. .read = srom_read,
  283. .write = srom_write,
  284. .open = srom_open,
  285. .release = srom_release,
  286. };
  287. /**
  288. * srom_setup_minor() - Initialize per-minor information.
  289. * @srom: Per-device SROM state.
  290. * @index: Device to set up.
  291. */
  292. static int srom_setup_minor(struct srom_dev *srom, int index)
  293. {
  294. struct device *dev;
  295. int devhdl = srom->hv_devhdl;
  296. mutex_init(&srom->lock);
  297. if (_srom_read(devhdl, &srom->total_size,
  298. SROM_TOTAL_SIZE_OFF, sizeof(srom->total_size)) < 0)
  299. return -EIO;
  300. if (_srom_read(devhdl, &srom->sector_size,
  301. SROM_SECTOR_SIZE_OFF, sizeof(srom->sector_size)) < 0)
  302. return -EIO;
  303. if (_srom_read(devhdl, &srom->page_size,
  304. SROM_PAGE_SIZE_OFF, sizeof(srom->page_size)) < 0)
  305. return -EIO;
  306. dev = device_create(srom_class, &srom_parent->dev,
  307. MKDEV(srom_major, index), srom, "%d", index);
  308. return PTR_ERR_OR_ZERO(dev);
  309. }
  310. /** srom_init() - Initialize the driver's module. */
  311. static int srom_init(void)
  312. {
  313. int result, i;
  314. dev_t dev = MKDEV(srom_major, 0);
  315. /*
  316. * Start with a plausible number of partitions; the krealloc() call
  317. * below will yield about log(srom_devs) additional allocations.
  318. */
  319. srom_devices = kzalloc(4 * sizeof(struct srom_dev), GFP_KERNEL);
  320. /* Discover the number of srom partitions. */
  321. for (i = 0; ; i++) {
  322. int devhdl;
  323. char buf[20];
  324. struct srom_dev *new_srom_devices =
  325. krealloc(srom_devices, (i+1) * sizeof(struct srom_dev),
  326. GFP_KERNEL | __GFP_ZERO);
  327. if (!new_srom_devices) {
  328. result = -ENOMEM;
  329. goto fail_mem;
  330. }
  331. srom_devices = new_srom_devices;
  332. sprintf(buf, "srom/0/%d", i);
  333. devhdl = hv_dev_open((HV_VirtAddr)buf, 0);
  334. if (devhdl < 0) {
  335. if (devhdl != HV_ENODEV)
  336. pr_notice("srom/%d: hv_dev_open failed: %d.\n",
  337. i, devhdl);
  338. break;
  339. }
  340. srom_devices[i].hv_devhdl = devhdl;
  341. }
  342. srom_devs = i;
  343. /* Bail out early if we have no partitions at all. */
  344. if (srom_devs == 0) {
  345. result = -ENODEV;
  346. goto fail_mem;
  347. }
  348. /* Register our major, and accept a dynamic number. */
  349. if (srom_major)
  350. result = register_chrdev_region(dev, srom_devs, "srom");
  351. else {
  352. result = alloc_chrdev_region(&dev, 0, srom_devs, "srom");
  353. srom_major = MAJOR(dev);
  354. }
  355. if (result < 0)
  356. goto fail_mem;
  357. /* Register a character device. */
  358. cdev_init(&srom_cdev, &srom_fops);
  359. srom_cdev.owner = THIS_MODULE;
  360. srom_cdev.ops = &srom_fops;
  361. result = cdev_add(&srom_cdev, dev, srom_devs);
  362. if (result < 0)
  363. goto fail_chrdev;
  364. /* Create a parent device */
  365. srom_parent = platform_device_register_simple("srom", -1, NULL, 0);
  366. if (IS_ERR(srom_parent)) {
  367. result = PTR_ERR(srom_parent);
  368. goto fail_pdev;
  369. }
  370. /* Create a sysfs class. */
  371. srom_class = class_create(THIS_MODULE, "srom");
  372. if (IS_ERR(srom_class)) {
  373. result = PTR_ERR(srom_class);
  374. goto fail_cdev;
  375. }
  376. srom_class->dev_groups = srom_dev_groups;
  377. srom_class->devnode = srom_devnode;
  378. /* Do per-partition initialization */
  379. for (i = 0; i < srom_devs; i++) {
  380. result = srom_setup_minor(srom_devices + i, i);
  381. if (result < 0)
  382. goto fail_class;
  383. }
  384. return 0;
  385. fail_class:
  386. for (i = 0; i < srom_devs; i++)
  387. device_destroy(srom_class, MKDEV(srom_major, i));
  388. class_destroy(srom_class);
  389. fail_cdev:
  390. platform_device_unregister(srom_parent);
  391. fail_pdev:
  392. cdev_del(&srom_cdev);
  393. fail_chrdev:
  394. unregister_chrdev_region(dev, srom_devs);
  395. fail_mem:
  396. kfree(srom_devices);
  397. return result;
  398. }
  399. /** srom_cleanup() - Clean up the driver's module. */
  400. static void srom_cleanup(void)
  401. {
  402. int i;
  403. for (i = 0; i < srom_devs; i++)
  404. device_destroy(srom_class, MKDEV(srom_major, i));
  405. class_destroy(srom_class);
  406. cdev_del(&srom_cdev);
  407. platform_device_unregister(srom_parent);
  408. unregister_chrdev_region(MKDEV(srom_major, 0), srom_devs);
  409. kfree(srom_devices);
  410. }
  411. module_init(srom_init);
  412. module_exit(srom_cleanup);