tile-srom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. * @devhdl: Partition device handle.
  291. */
  292. static int srom_setup_minor(struct srom_dev *srom, int devhdl)
  293. {
  294. srom->hv_devhdl = devhdl;
  295. mutex_init(&srom->lock);
  296. if (_srom_read(devhdl, &srom->total_size,
  297. SROM_TOTAL_SIZE_OFF, sizeof(srom->total_size)) < 0)
  298. return -EIO;
  299. if (_srom_read(devhdl, &srom->sector_size,
  300. SROM_SECTOR_SIZE_OFF, sizeof(srom->sector_size)) < 0)
  301. return -EIO;
  302. if (_srom_read(devhdl, &srom->page_size,
  303. SROM_PAGE_SIZE_OFF, sizeof(srom->page_size)) < 0)
  304. return -EIO;
  305. return 0;
  306. }
  307. /** srom_init() - Initialize the driver's module. */
  308. static int srom_init(void)
  309. {
  310. int result, i;
  311. dev_t dev = MKDEV(srom_major, 0);
  312. /*
  313. * Start with a plausible number of partitions; the krealloc() call
  314. * below will yield about log(srom_devs) additional allocations.
  315. */
  316. srom_devices = kmalloc(4 * sizeof(struct srom_dev), GFP_KERNEL);
  317. /* Discover the number of srom partitions. */
  318. for (i = 0; ; i++) {
  319. int devhdl;
  320. char buf[20];
  321. struct srom_dev *new_srom_devices =
  322. krealloc(srom_devices, (i+1) * sizeof(struct srom_dev),
  323. GFP_KERNEL);
  324. if (!new_srom_devices) {
  325. result = -ENOMEM;
  326. goto fail_mem;
  327. }
  328. srom_devices = new_srom_devices;
  329. sprintf(buf, "srom/0/%d", i);
  330. devhdl = hv_dev_open((HV_VirtAddr)buf, 0);
  331. if (devhdl < 0) {
  332. if (devhdl != HV_ENODEV)
  333. pr_notice("srom/%d: hv_dev_open failed: %d.\n",
  334. i, devhdl);
  335. break;
  336. }
  337. result = srom_setup_minor(&srom_devices[i], devhdl);
  338. if (result != 0)
  339. goto fail_mem;
  340. }
  341. srom_devs = i;
  342. /* Bail out early if we have no partitions at all. */
  343. if (srom_devs == 0) {
  344. result = -ENODEV;
  345. goto fail_mem;
  346. }
  347. /* Register our major, and accept a dynamic number. */
  348. if (srom_major)
  349. result = register_chrdev_region(dev, srom_devs, "srom");
  350. else {
  351. result = alloc_chrdev_region(&dev, 0, srom_devs, "srom");
  352. srom_major = MAJOR(dev);
  353. }
  354. if (result < 0)
  355. goto fail_mem;
  356. /* Register a character device. */
  357. cdev_init(&srom_cdev, &srom_fops);
  358. srom_cdev.owner = THIS_MODULE;
  359. srom_cdev.ops = &srom_fops;
  360. result = cdev_add(&srom_cdev, dev, srom_devs);
  361. if (result < 0)
  362. goto fail_chrdev;
  363. /* Create a parent device */
  364. srom_parent = platform_device_register_simple("srom", -1, NULL, 0);
  365. if (IS_ERR(srom_parent)) {
  366. result = PTR_ERR(srom_parent);
  367. goto fail_pdev;
  368. }
  369. /* Create a sysfs class. */
  370. srom_class = class_create(THIS_MODULE, "srom");
  371. if (IS_ERR(srom_class)) {
  372. result = PTR_ERR(srom_class);
  373. goto fail_cdev;
  374. }
  375. srom_class->dev_groups = srom_dev_groups;
  376. srom_class->devnode = srom_devnode;
  377. /* Create per-partition devices */
  378. for (i = 0; i < srom_devs; i++) {
  379. struct device *dev =
  380. device_create(srom_class, &srom_parent->dev,
  381. MKDEV(srom_major, i), srom_devices + i,
  382. "%d", i);
  383. result = PTR_ERR_OR_ZERO(dev);
  384. if (result < 0)
  385. goto fail_class;
  386. }
  387. return 0;
  388. fail_class:
  389. for (i = 0; i < srom_devs; i++)
  390. device_destroy(srom_class, MKDEV(srom_major, i));
  391. class_destroy(srom_class);
  392. fail_cdev:
  393. platform_device_unregister(srom_parent);
  394. fail_pdev:
  395. cdev_del(&srom_cdev);
  396. fail_chrdev:
  397. unregister_chrdev_region(dev, srom_devs);
  398. fail_mem:
  399. kfree(srom_devices);
  400. return result;
  401. }
  402. /** srom_cleanup() - Clean up the driver's module. */
  403. static void srom_cleanup(void)
  404. {
  405. int i;
  406. for (i = 0; i < srom_devs; i++)
  407. device_destroy(srom_class, MKDEV(srom_major, i));
  408. class_destroy(srom_class);
  409. cdev_del(&srom_cdev);
  410. platform_device_unregister(srom_parent);
  411. unregister_chrdev_region(MKDEV(srom_major, 0), srom_devs);
  412. kfree(srom_devices);
  413. }
  414. module_init(srom_init);
  415. module_exit(srom_cleanup);