c2port-duramar2150.c 3.2 KB

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