jornada720_ssp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**
  2. * arch/arm/mac-sa1100/jornada720_ssp.c
  3. *
  4. * Copyright (C) 2006/2007 Kristoffer Ericson <Kristoffer.Ericson@gmail.com>
  5. * Copyright (C) 2006 Filip Zyzniewski <filip.zyzniewski@tefnet.pl>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * SSP driver for the HP Jornada 710/720/728
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/errno.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/sched.h>
  20. #include <linux/io.h>
  21. #include <mach/hardware.h>
  22. #include <mach/jornada720.h>
  23. #include <asm/hardware/ssp.h>
  24. static DEFINE_SPINLOCK(jornada_ssp_lock);
  25. static unsigned long jornada_ssp_flags;
  26. /**
  27. * jornada_ssp_reverse - reverses input byte
  28. *
  29. * we need to reverse all data we receive from the mcu due to its physical location
  30. * returns : 01110111 -> 11101110
  31. */
  32. inline u8 jornada_ssp_reverse(u8 byte)
  33. {
  34. return
  35. ((0x80 & byte) >> 7) |
  36. ((0x40 & byte) >> 5) |
  37. ((0x20 & byte) >> 3) |
  38. ((0x10 & byte) >> 1) |
  39. ((0x08 & byte) << 1) |
  40. ((0x04 & byte) << 3) |
  41. ((0x02 & byte) << 5) |
  42. ((0x01 & byte) << 7);
  43. };
  44. EXPORT_SYMBOL(jornada_ssp_reverse);
  45. /**
  46. * jornada_ssp_byte - waits for ready ssp bus and sends byte
  47. *
  48. * waits for fifo buffer to clear and then transmits, if it doesn't then we will
  49. * timeout after <timeout> rounds. Needs mcu running before its called.
  50. *
  51. * returns : %mcu output on success
  52. * : %-ETIMEDOUT on timeout
  53. */
  54. int jornada_ssp_byte(u8 byte)
  55. {
  56. int timeout = 400000;
  57. u16 ret;
  58. while ((GPLR & GPIO_GPIO10)) {
  59. if (!--timeout) {
  60. printk(KERN_WARNING "SSP: timeout while waiting for transmit\n");
  61. return -ETIMEDOUT;
  62. }
  63. cpu_relax();
  64. }
  65. ret = jornada_ssp_reverse(byte) << 8;
  66. ssp_write_word(ret);
  67. ssp_read_word(&ret);
  68. return jornada_ssp_reverse(ret);
  69. };
  70. EXPORT_SYMBOL(jornada_ssp_byte);
  71. /**
  72. * jornada_ssp_inout - decide if input is command or trading byte
  73. *
  74. * returns : (jornada_ssp_byte(byte)) on success
  75. * : %-ETIMEDOUT on timeout failure
  76. */
  77. int jornada_ssp_inout(u8 byte)
  78. {
  79. int ret, i;
  80. /* true means command byte */
  81. if (byte != TXDUMMY) {
  82. ret = jornada_ssp_byte(byte);
  83. /* Proper return to commands is TxDummy */
  84. if (ret != TXDUMMY) {
  85. for (i = 0; i < 256; i++)/* flushing bus */
  86. if (jornada_ssp_byte(TXDUMMY) == -1)
  87. break;
  88. return -ETIMEDOUT;
  89. }
  90. } else /* Exchange TxDummy for data */
  91. ret = jornada_ssp_byte(TXDUMMY);
  92. return ret;
  93. };
  94. EXPORT_SYMBOL(jornada_ssp_inout);
  95. /**
  96. * jornada_ssp_start - enable mcu
  97. *
  98. */
  99. void jornada_ssp_start(void)
  100. {
  101. spin_lock_irqsave(&jornada_ssp_lock, jornada_ssp_flags);
  102. GPCR = GPIO_GPIO25;
  103. udelay(50);
  104. return;
  105. };
  106. EXPORT_SYMBOL(jornada_ssp_start);
  107. /**
  108. * jornada_ssp_end - disable mcu and turn off lock
  109. *
  110. */
  111. void jornada_ssp_end(void)
  112. {
  113. GPSR = GPIO_GPIO25;
  114. spin_unlock_irqrestore(&jornada_ssp_lock, jornada_ssp_flags);
  115. return;
  116. };
  117. EXPORT_SYMBOL(jornada_ssp_end);
  118. static int jornada_ssp_probe(struct platform_device *dev)
  119. {
  120. int ret;
  121. GPSR = GPIO_GPIO25;
  122. ret = ssp_init();
  123. /* worked fine, lets not bother with anything else */
  124. if (!ret) {
  125. printk(KERN_INFO "SSP: device initialized with irq\n");
  126. return ret;
  127. }
  128. printk(KERN_WARNING "SSP: initialization failed, trying non-irq solution \n");
  129. /* init of Serial 4 port */
  130. Ser4MCCR0 = 0;
  131. Ser4SSCR0 = 0x0387;
  132. Ser4SSCR1 = 0x18;
  133. /* clear out any left over data */
  134. ssp_flush();
  135. /* enable MCU */
  136. jornada_ssp_start();
  137. /* see if return value makes sense */
  138. ret = jornada_ssp_inout(GETBRIGHTNESS);
  139. /* seems like it worked, just feed it with TxDummy to get rid of data */
  140. if (ret == TXDUMMY)
  141. jornada_ssp_inout(TXDUMMY);
  142. jornada_ssp_end();
  143. /* failed, lets just kill everything */
  144. if (ret == -ETIMEDOUT) {
  145. printk(KERN_WARNING "SSP: attempts failed, bailing\n");
  146. ssp_exit();
  147. return -ENODEV;
  148. }
  149. /* all fine */
  150. printk(KERN_INFO "SSP: device initialized\n");
  151. return 0;
  152. };
  153. static int jornada_ssp_remove(struct platform_device *dev)
  154. {
  155. /* Note that this doesn't actually remove the driver, since theres nothing to remove
  156. * It just makes sure everything is turned off */
  157. GPSR = GPIO_GPIO25;
  158. ssp_exit();
  159. return 0;
  160. };
  161. struct platform_driver jornadassp_driver = {
  162. .probe = jornada_ssp_probe,
  163. .remove = jornada_ssp_remove,
  164. .driver = {
  165. .name = "jornada_ssp",
  166. },
  167. };
  168. static int __init jornada_ssp_init(void)
  169. {
  170. return platform_driver_register(&jornadassp_driver);
  171. }
  172. module_init(jornada_ssp_init);