c2port-duramar2150.c 3.2 KB

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