c2port-duramar2150.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Silicon Labs C2 port Linux support for Eurotech Duramar 2150
  4. *
  5. * Copyright (c) 2008 Rodolfo Giometti <[email protected]>
  6. * Copyright (c) 2008 Eurotech S.p.A. <[email protected]>
  7. */
  8. #include <linux/errno.h>
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/c2port.h>
  16. #define DATA_PORT 0x325
  17. #define DIR_PORT 0x326
  18. #define C2D (1 << 0)
  19. #define C2CK (1 << 1)
  20. static DEFINE_MUTEX(update_lock);
  21. /*
  22. * C2 port operations
  23. */
  24. static void duramar2150_c2port_access(struct c2port_device *dev, int status)
  25. {
  26. u8 v;
  27. mutex_lock(&update_lock);
  28. v = inb(DIR_PORT);
  29. /* 0 = input, 1 = output */
  30. if (status)
  31. outb(v | (C2D | C2CK), DIR_PORT);
  32. else
  33. /* When access is "off" is important that both lines are set
  34. * as inputs or hi-impedance */
  35. outb(v & ~(C2D | C2CK), DIR_PORT);
  36. mutex_unlock(&update_lock);
  37. }
  38. static void duramar2150_c2port_c2d_dir(struct c2port_device *dev, int dir)
  39. {
  40. u8 v;
  41. mutex_lock(&update_lock);
  42. v = inb(DIR_PORT);
  43. if (dir)
  44. outb(v & ~C2D, DIR_PORT);
  45. else
  46. outb(v | C2D, DIR_PORT);
  47. mutex_unlock(&update_lock);
  48. }
  49. static int duramar2150_c2port_c2d_get(struct c2port_device *dev)
  50. {
  51. return inb(DATA_PORT) & C2D;
  52. }
  53. static void duramar2150_c2port_c2d_set(struct c2port_device *dev, int status)
  54. {
  55. u8 v;
  56. mutex_lock(&update_lock);
  57. v = inb(DATA_PORT);
  58. if (status)
  59. outb(v | C2D, DATA_PORT);
  60. else
  61. outb(v & ~C2D, DATA_PORT);
  62. mutex_unlock(&update_lock);
  63. }
  64. static void duramar2150_c2port_c2ck_set(struct c2port_device *dev, int status)
  65. {
  66. u8 v;
  67. mutex_lock(&update_lock);
  68. v = inb(DATA_PORT);
  69. if (status)
  70. outb(v | C2CK, DATA_PORT);
  71. else
  72. outb(v & ~C2CK, DATA_PORT);
  73. mutex_unlock(&update_lock);
  74. }
  75. static struct c2port_ops duramar2150_c2port_ops = {
  76. .block_size = 512, /* bytes */
  77. .blocks_num = 30, /* total flash size: 15360 bytes */
  78. .access = duramar2150_c2port_access,
  79. .c2d_dir = duramar2150_c2port_c2d_dir,
  80. .c2d_get = duramar2150_c2port_c2d_get,
  81. .c2d_set = duramar2150_c2port_c2d_set,
  82. .c2ck_set = duramar2150_c2port_c2ck_set,
  83. };
  84. static struct c2port_device *duramar2150_c2port_dev;
  85. /*
  86. * Module stuff
  87. */
  88. static int __init duramar2150_c2port_init(void)
  89. {
  90. struct resource *res;
  91. int ret = 0;
  92. res = request_region(0x325, 2, "c2port");
  93. if (!res)
  94. return -EBUSY;
  95. duramar2150_c2port_dev = c2port_device_register("uc",
  96. &duramar2150_c2port_ops, NULL);
  97. if (IS_ERR(duramar2150_c2port_dev)) {
  98. ret = PTR_ERR(duramar2150_c2port_dev);
  99. goto free_region;
  100. }
  101. return 0;
  102. free_region:
  103. release_region(0x325, 2);
  104. return ret;
  105. }
  106. static void __exit duramar2150_c2port_exit(void)
  107. {
  108. /* Setup the GPIOs as input by default (access = 0) */
  109. duramar2150_c2port_access(duramar2150_c2port_dev, 0);
  110. c2port_device_unregister(duramar2150_c2port_dev);
  111. release_region(0x325, 2);
  112. }
  113. module_init(duramar2150_c2port_init);
  114. module_exit(duramar2150_c2port_exit);
  115. MODULE_AUTHOR("Rodolfo Giometti <[email protected]>");
  116. MODULE_DESCRIPTION("Silicon Labs C2 port Linux support for Duramar 2150");
  117. MODULE_LICENSE("GPL");