jornada720_ssp.c 4.4 KB

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