bm_visys_driver.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (C) 2021 Alibaba Group Holding Limited
  3. * Author: LuChongzhi <chongzhi.lcz@alibaba-inc.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <asm/io.h>
  10. #include <linux/cdev.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/delay.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/mm.h>
  17. #include <linux/timer.h>
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/poll.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/slab.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/debugfs.h>
  26. #include <linux/miscdevice.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/of.h>
  30. #include "bm_printk.h"
  31. #include "bm_visys_ioctl.h"
  32. #define BM_DRIVER_NAME "bm_visys"
  33. #define BM_DRIVER_MAXCNT 1
  34. struct bm_visys_drvdata {
  35. struct cdev cdev;
  36. dev_t devt;
  37. struct class *class;
  38. struct mutex mutex;
  39. unsigned int device_idx;
  40. void __iomem *base;
  41. void __iomem *reset;
  42. //int irq_num;
  43. void *private; // can be bm_visys_drvdata_private, but not use now
  44. };
  45. struct bm_visys_drvdata_private {
  46. int private_tmp;
  47. };
  48. static struct class *bm_driver_class;
  49. static unsigned int bm_driver_major = 0;
  50. static unsigned int bm_driver_minor = 0;
  51. static unsigned int device_register_index = 0;
  52. static struct bm_visys_drvdata *g_pdrvdata;
  53. #define check_retval(x)\
  54. do {\
  55. if ((x))\
  56. return -EIO;\
  57. } while (0)
  58. static unsigned int bm_visys_poll(struct file * filp, poll_table *wait)
  59. {
  60. return 0;
  61. }
  62. void bm_visys_work(struct work_struct *work)
  63. {
  64. }
  65. irqreturn_t bm_visys_irq(int irq, void *dev_id)
  66. {
  67. bm_info("enter %s\n", __func__);
  68. return IRQ_HANDLED;
  69. }
  70. static int bm_visys_open(struct inode * inode, struct file * file)
  71. {
  72. struct bm_visys_drvdata *drvdata;
  73. bm_info("enter %s\n", __func__);
  74. drvdata = container_of(inode->i_cdev, struct bm_visys_drvdata, cdev);
  75. file->private_data = drvdata;
  76. return 0;
  77. };
  78. int k_bm_visys_write_reg(uint32_t offset, uint32_t value)
  79. {
  80. writel(value, g_pdrvdata->base + offset);
  81. bm_info("%s addr 0x%08x val 0x%08x\n", __func__, offset, value);
  82. return 0;
  83. }
  84. int k_bm_visys_read_reg(uint32_t offset, uint32_t *value)
  85. {
  86. *value = readl(g_pdrvdata->base + offset);
  87. bm_info("%s addr 0x%08x val 0x%08x\n", __func__, offset, *value);
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(k_bm_visys_write_reg);
  91. EXPORT_SYMBOL(k_bm_visys_read_reg);
  92. static int bm_visys_write_reg(struct bm_visys_drvdata *drvdata, void *__user args)
  93. {
  94. struct bm_visys_reg_t reg;
  95. check_retval(copy_from_user(&reg, args, sizeof(reg)));
  96. writel(reg.value, drvdata->base + reg.offset);
  97. bm_info("%s addr 0x%08x val 0x%08x\n", __func__, reg.offset, reg.value);
  98. return 0;
  99. }
  100. static int bm_visys_read_reg(struct bm_visys_drvdata *drvdata, void *__user args)
  101. {
  102. struct bm_visys_reg_t reg;
  103. check_retval(copy_from_user(&reg, args, sizeof(reg)));
  104. reg.value = readl(drvdata->base + reg.offset);
  105. check_retval(copy_to_user(args, &reg, sizeof(reg)));
  106. bm_info("%s addr 0x%08x val 0x%08x\n", __func__, reg.offset, reg.value);
  107. return 0;
  108. }
  109. static long bm_visys_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  110. {
  111. long ret = 0;
  112. struct bm_visys_drvdata *drvdata;
  113. bm_info("enter %s\n", __func__);
  114. drvdata = file->private_data;
  115. if (drvdata == NULL) {
  116. bm_err("%s:file private is null point error\n", __func__);
  117. return -ENOMEM;
  118. }
  119. mutex_lock(&drvdata->mutex);
  120. switch (cmd) {
  121. case BMVISYSIOC_WRITE_REG:
  122. ret = bm_visys_write_reg(drvdata, (void *)arg);
  123. break;
  124. case BMVISYSIOC_READ_REG:
  125. ret = bm_visys_read_reg(drvdata, (void *)arg);
  126. break;
  127. default:
  128. ret = -EPERM;
  129. bm_err("%s: unsupported command %d", __func__, cmd);
  130. break;
  131. }
  132. mutex_unlock(&drvdata->mutex);
  133. return ret;
  134. };
  135. static int bm_visys_release(struct inode * inode, struct file * file)
  136. {
  137. bm_info("enter %s\n", __func__);
  138. return 0;
  139. };
  140. static int bm_visys_mmap(struct file *pFile, struct vm_area_struct *vma)
  141. {
  142. bm_info("enter %s\n", __func__);
  143. return 0;
  144. };
  145. struct file_operations bm_visys_fops = {
  146. .owner = THIS_MODULE,
  147. .open = bm_visys_open,
  148. .release = bm_visys_release,
  149. .unlocked_ioctl = bm_visys_ioctl,
  150. .mmap = bm_visys_mmap,
  151. .poll = bm_visys_poll,
  152. };
  153. static int bm_visys_probe(struct platform_device *pdev)
  154. {
  155. int ret = 0;
  156. struct bm_visys_drvdata *drvdata;
  157. struct resource *iores_mem;
  158. u32 value;
  159. bm_info("enter %s\n", __func__);
  160. pdev->id = device_register_index;
  161. if (pdev->id >= BM_DRIVER_MAXCNT) {
  162. bm_err("%s:pdev id is %d error\n", __func__, pdev->id);
  163. return -EINVAL;
  164. }
  165. drvdata = devm_kzalloc(&pdev->dev,sizeof(struct bm_visys_drvdata), GFP_KERNEL);
  166. if (drvdata == NULL) {
  167. bm_err("%s:alloc struct drvdata error\n", __func__);
  168. return -ENOMEM;
  169. }
  170. g_pdrvdata = drvdata;
  171. iores_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  172. drvdata->base = devm_ioremap_resource(&pdev->dev, iores_mem);
  173. bm_info("%s: [%s%d]: drvdata->base=0x%px, phy_addr base=0x%llx\n", __func__,
  174. BM_DRIVER_NAME, pdev->id, drvdata->base, iores_mem->start);
  175. drvdata->reset = NULL;
  176. drvdata->device_idx = pdev->id;
  177. mutex_init(&drvdata->mutex);
  178. //drvdata->irq_num = platform_get_irq(pdev, 0);
  179. //bm_info("%s:[%s%d]: pdriver_dev->irq_num=%d\n", __func__,
  180. // BM_VISYS_NAME, pdev->id, drvdata->irq_num);
  181. platform_set_drvdata(pdev, drvdata);
  182. if (pdev->id == 0) {
  183. if (bm_driver_major == 0) {
  184. ret = alloc_chrdev_region(&drvdata->devt, 0, BM_DRIVER_MAXCNT, BM_DRIVER_NAME);
  185. if (ret != 0) {
  186. bm_err("%s:alloc_chrdev_region error\n", __func__);
  187. return ret;
  188. }
  189. bm_driver_major = MAJOR(drvdata->devt);
  190. bm_driver_minor = MINOR(drvdata->devt);
  191. } else {
  192. drvdata->devt = MKDEV(bm_driver_major, bm_driver_minor);
  193. ret = register_chrdev_region(drvdata->devt, BM_DRIVER_MAXCNT, BM_DRIVER_NAME);
  194. if (ret) {
  195. bm_err("%s:register_chrdev_region error\n", __func__);
  196. return ret;
  197. }
  198. }
  199. bm_driver_class = class_create(THIS_MODULE, BM_DRIVER_NAME);
  200. if (IS_ERR(bm_driver_class)) {
  201. bm_err("%s[%d]:class_create error!\n", __func__, __LINE__);
  202. return -EINVAL;
  203. }
  204. }
  205. drvdata->devt = MKDEV(bm_driver_major, bm_driver_minor + pdev->id);
  206. cdev_init(&drvdata->cdev, &bm_visys_fops);
  207. ret = cdev_add(&drvdata->cdev, drvdata->devt, 1);
  208. if ( ret ) {
  209. bm_err("%s[%d]:cdev_add error!\n", __func__, __LINE__);
  210. return ret;
  211. } else {
  212. bm_info("%s[%d]:cdev_add OK, major=%d, minor=%d\n", __func__, __LINE__,
  213. bm_driver_major, bm_driver_minor + pdev->id);
  214. }
  215. drvdata->class = bm_driver_class;
  216. device_create(drvdata->class, NULL, drvdata->devt,
  217. drvdata, "%s%d", BM_DRIVER_NAME, pdev->id);
  218. value = readl(drvdata->base+0x4);
  219. bm_info("offset=04, value is:0x%08x\n", value);
  220. device_register_index++;
  221. bm_info("exit %s:[%s%d]\n", __func__, BM_DRIVER_NAME, pdev->id);
  222. return 0;
  223. //err_register:
  224. // When error occore, should free memory, destory device, unregister ...
  225. // but now, ignore it
  226. // return ret;
  227. }
  228. static int bm_visys_remove(struct platform_device *pdev)
  229. {
  230. struct bm_visys_drvdata *drvdata;
  231. bm_info("enter %s\n", __func__);
  232. device_register_index--;
  233. drvdata = platform_get_drvdata(pdev);
  234. //free_irq(drvdata->irq_num, drvdata);
  235. cdev_del(&drvdata->cdev);
  236. device_destroy(drvdata->class, drvdata->devt);
  237. unregister_chrdev_region(drvdata->devt, BM_DRIVER_MAXCNT);
  238. mutex_destroy(&drvdata->mutex);
  239. if (device_register_index == 0) {
  240. class_destroy(drvdata->class);
  241. }
  242. devm_kfree(&pdev->dev, drvdata);
  243. bm_info("exit %s\n", __func__);
  244. return 0;
  245. }
  246. static const struct of_device_id bm_visys_of_match[] = {
  247. { .compatible = "thead,light-bm-visys", },
  248. { /* sentinel */ },
  249. };
  250. MODULE_DEVICE_TABLE(of, bm_visys_of_match);
  251. static struct platform_driver bm_visys_driver = {
  252. .probe = bm_visys_probe,
  253. .remove = bm_visys_remove,
  254. .driver = {
  255. .name = BM_DRIVER_NAME,
  256. .owner = THIS_MODULE,
  257. .of_match_table = of_match_ptr(bm_visys_of_match),
  258. }
  259. };
  260. static int __init bm_visys_init_module(void)
  261. {
  262. int ret = 0;
  263. bm_info("enter %s\n", __func__);
  264. ret = platform_driver_register(&bm_visys_driver);
  265. if (ret) {
  266. bm_err("register platform driver failed.\n");
  267. return ret;
  268. }
  269. return ret;
  270. }
  271. static void __exit bm_visys_exit_module(void)
  272. {
  273. bm_info("enter %s\n", __func__);
  274. platform_driver_unregister(&bm_visys_driver);
  275. }
  276. module_init(bm_visys_init_module);
  277. module_exit(bm_visys_exit_module);
  278. MODULE_AUTHOR("Lu Chongzhi");
  279. MODULE_DESCRIPTION("BAREMETAL-VISYS");
  280. MODULE_LICENSE("GPL");