platform_device.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Only works for ARM.
  3. Uses:
  4. - hw/misc/lkmc_platform_device.c minimal device added in our QEMU fork
  5. - the device tree entry we added to our Linux kernel fork:
  6. https://github.com/cirosantilli/linux/blob/361bb623671a52a36a077a6dd45843389a687a33/arch/arm/boot/dts/versatile-pb.dts#L42
  7. See: https://stackoverflow.com/questions/28315265/how-to-add-a-new-device-in-qemu-source-code/44612957#44612957
  8. Expected outcome after insmod:
  9. - QEMU reports MMIO with printfs
  10. - IRQs are generated and handled by this module, which logs to dmesg
  11. Also without insmodding this module, try:
  12. devmem 0x101e9000 w 0x12345678
  13. which touches from userland through /dev/mem.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_device.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/platform_device.h>
  24. static struct resource res;
  25. static unsigned int irq;
  26. static void __iomem *map;
  27. static irqreturn_t lkmc_irq_handler(int irq, void *dev)
  28. {
  29. /* TODO this 34 and not 18 as in the DTS, likely the interrupt controller moves it around.
  30. * Understand precisely. 34 = 18 + 16, I think 16 is by how much the controller will shift it. */
  31. pr_info("lkmc_irq_handler irq = %d dev = %llx\n", irq, *(unsigned long long *)dev);
  32. /* ACK the IRQ. */
  33. iowrite32(0x9ABCDEF0, map + 4);
  34. return IRQ_HANDLED;
  35. }
  36. static int lkmc_platform_device_probe(struct platform_device *pdev)
  37. {
  38. int asdf;
  39. struct device *dev = &pdev->dev;
  40. struct device_node *np = dev->of_node;
  41. dev_info(dev, "probe\n");
  42. /* Play with our custom device tree poperty. */
  43. if (of_property_read_u32(np, "lkmc-asdf", &asdf) ) {
  44. dev_err(dev, "of_property_read_u32\n");
  45. return -EINVAL;
  46. }
  47. if (asdf != 0x12345678) {
  48. dev_err(dev, "asdf = %llx\n", (unsigned long long)asdf);
  49. return -EINVAL;
  50. }
  51. /* IRQ. Shared so that other test modules may snoop it. */
  52. irq = irq_of_parse_and_map(dev->of_node, 0);
  53. if (request_irq(irq, lkmc_irq_handler, IRQF_SHARED, "lkmc_platform_device", dev) < 0) {
  54. dev_err(dev, "request_irq");
  55. return -EINVAL;
  56. }
  57. dev_info(dev, "irq = %u\n", irq);
  58. /* MMIO. */
  59. if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
  60. dev_err(dev, "of_address_to_resource");
  61. return -EINVAL;
  62. }
  63. if (!request_mem_region(res.start, resource_size(&res), "lkmc_platform_device")) {
  64. dev_err(dev, "request_mem_region");
  65. return -EINVAL;
  66. }
  67. map = of_iomap(pdev->dev.of_node, 0);
  68. if (!map) {
  69. dev_err(dev, "of_iomap");
  70. return -EINVAL;
  71. }
  72. dev_info(dev, "res.start = %llx resource_size = %llx\n",
  73. (unsigned long long)res.start, (unsigned long long)resource_size(&res));
  74. /* Test MMIO and IRQ: writing to the register generates an IRQ. */
  75. iowrite32(0x12345678, map);
  76. /* Test register read. */
  77. if (ioread32(map + 0) != 0x12340000) panic("assert");
  78. if (ioread32(map + 8) != 0x12340008) panic("assert");
  79. return 0;
  80. }
  81. static int lkmc_platform_device_remove(struct platform_device *pdev)
  82. {
  83. dev_info(&pdev->dev, "remove\n");
  84. free_irq(irq, &pdev->dev);
  85. iounmap(map);
  86. release_mem_region(res.start, resource_size(&res));
  87. return 0;
  88. }
  89. static const struct of_device_id of_lkmc_platform_device_match[] = {
  90. /* This tells our driver which device tree node it will use.
  91. * It matches the kmc_platform_device@XXXX entry that we added to the device tree. */
  92. { .compatible = "lkmc_platform_device", },
  93. {},
  94. };
  95. MODULE_DEVICE_TABLE(of, of_lkmc_platform_device_match);
  96. static struct platform_driver lkmc_plaform_driver = {
  97. .probe = lkmc_platform_device_probe,
  98. .remove = lkmc_platform_device_remove,
  99. .driver = {
  100. .name = "lkmc_platform_device",
  101. .of_match_table = of_lkmc_platform_device_match,
  102. .owner = THIS_MODULE,
  103. },
  104. };
  105. static int lkmc_platform_device_init(void)
  106. {
  107. pr_info("lkmc_platform_device_init\n");
  108. return platform_driver_register(&lkmc_plaform_driver);
  109. }
  110. static void lkmc_platform_device_exit(void)
  111. {
  112. pr_info("lkmc_platform_device_exit\n");
  113. platform_driver_unregister(&lkmc_plaform_driver);
  114. }
  115. module_init(lkmc_platform_device_init)
  116. module_exit(lkmc_platform_device_exit)
  117. MODULE_LICENSE("GPL");