ssp-cpld.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* arch/arm/mach-lh7a40x/ssp-cpld.c
  2. *
  3. * Copyright (C) 2004,2005 Marc Singer
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * version 2 as published by the Free Software Foundation.
  8. *
  9. * SSP/SPI driver for the CardEngine CPLD.
  10. *
  11. */
  12. /* NOTES
  13. -----
  14. o *** This driver is cribbed from the 7952x implementation.
  15. Some comments may not apply.
  16. o This driver contains sufficient logic to control either the
  17. serial EEPROMs or the audio codec. It is included in the kernel
  18. to support the codec. The EEPROMs are really the responsibility
  19. of the boot loader and should probably be left alone.
  20. o The code must be augmented to cope with multiple, simultaneous
  21. clients.
  22. o The audio codec writes to the codec chip whenever playback
  23. starts.
  24. o The touchscreen driver writes to the ads chip every time it
  25. samples.
  26. o The audio codec must write 16 bits, but the touch chip writes
  27. are 8 bits long.
  28. o We need to be able to keep these configurations separate while
  29. simultaneously active.
  30. */
  31. #include <linux/module.h>
  32. #include <linux/kernel.h>
  33. //#include <linux/sched.h>
  34. #include <linux/errno.h>
  35. #include <linux/interrupt.h>
  36. //#include <linux/ioport.h>
  37. #include <linux/init.h>
  38. #include <linux/delay.h>
  39. #include <linux/spinlock.h>
  40. #include <asm/io.h>
  41. #include <asm/irq.h>
  42. #include <asm/hardware.h>
  43. #include <asm/arch/ssp.h>
  44. //#define TALK
  45. #if defined (TALK)
  46. #define PRINTK(f...) printk (f)
  47. #else
  48. #define PRINTK(f...) do {} while (0)
  49. #endif
  50. #if defined (CONFIG_ARCH_LH7A400)
  51. # define CPLD_SPID __REGP16(CPLD06_VIRT) /* SPI data */
  52. # define CPLD_SPIC __REGP16(CPLD08_VIRT) /* SPI control */
  53. # define CPLD_SPIC_CS_CODEC (1<<0)
  54. # define CPLD_SPIC_CS_TOUCH (1<<1)
  55. # define CPLD_SPIC_WRITE (0<<2)
  56. # define CPLD_SPIC_READ (1<<2)
  57. # define CPLD_SPIC_DONE (1<<3) /* r/o */
  58. # define CPLD_SPIC_LOAD (1<<4)
  59. # define CPLD_SPIC_START (1<<4)
  60. # define CPLD_SPIC_LOADED (1<<5) /* r/o */
  61. #endif
  62. #define CPLD_SPI __REGP16(CPLD0A_VIRT) /* SPI operation */
  63. #define CPLD_SPI_CS_EEPROM (1<<3)
  64. #define CPLD_SPI_SCLK (1<<2)
  65. #define CPLD_SPI_TX_SHIFT (1)
  66. #define CPLD_SPI_TX (1<<CPLD_SPI_TX_SHIFT)
  67. #define CPLD_SPI_RX_SHIFT (0)
  68. #define CPLD_SPI_RX (1<<CPLD_SPI_RX_SHIFT)
  69. /* *** FIXME: these timing values are substantially larger than the
  70. *** chip requires. We may implement an nsleep () function. */
  71. #define T_SKH 1 /* Clock time high (us) */
  72. #define T_SKL 1 /* Clock time low (us) */
  73. #define T_CS 1 /* Minimum chip select low time (us) */
  74. #define T_CSS 1 /* Minimum chip select setup time (us) */
  75. #define T_DIS 1 /* Data setup time (us) */
  76. /* EEPROM SPI bits */
  77. #define P_START (1<<9)
  78. #define P_WRITE (1<<7)
  79. #define P_READ (2<<7)
  80. #define P_ERASE (3<<7)
  81. #define P_EWDS (0<<7)
  82. #define P_WRAL (0<<7)
  83. #define P_ERAL (0<<7)
  84. #define P_EWEN (0<<7)
  85. #define P_A_EWDS (0<<5)
  86. #define P_A_WRAL (1<<5)
  87. #define P_A_ERAL (2<<5)
  88. #define P_A_EWEN (3<<5)
  89. struct ssp_configuration {
  90. int device;
  91. int mode;
  92. int speed;
  93. int frame_size_write;
  94. int frame_size_read;
  95. };
  96. static struct ssp_configuration ssp_configuration;
  97. static spinlock_t ssp_lock;
  98. static void enable_cs (void)
  99. {
  100. switch (ssp_configuration.device) {
  101. case DEVICE_EEPROM:
  102. CPLD_SPI |= CPLD_SPI_CS_EEPROM;
  103. break;
  104. }
  105. udelay (T_CSS);
  106. }
  107. static void disable_cs (void)
  108. {
  109. switch (ssp_configuration.device) {
  110. case DEVICE_EEPROM:
  111. CPLD_SPI &= ~CPLD_SPI_CS_EEPROM;
  112. break;
  113. }
  114. udelay (T_CS);
  115. }
  116. static void pulse_clock (void)
  117. {
  118. CPLD_SPI |= CPLD_SPI_SCLK;
  119. udelay (T_SKH);
  120. CPLD_SPI &= ~CPLD_SPI_SCLK;
  121. udelay (T_SKL);
  122. }
  123. /* execute_spi_command
  124. sends an spi command to a device. It first sends cwrite bits from
  125. v. If cread is greater than zero it will read cread bits
  126. (discarding the leading 0 bit) and return them. If cread is less
  127. than zero it will check for completetion status and return 0 on
  128. success or -1 on timeout. If cread is zero it does nothing other
  129. than sending the command.
  130. On the LPD7A400, we can only read or write multiples of 8 bits on
  131. the codec and the touch screen device. Here, we round up.
  132. */
  133. static int execute_spi_command (int v, int cwrite, int cread)
  134. {
  135. unsigned long l = 0;
  136. #if defined (CONFIG_MACH_LPD7A400)
  137. /* The codec and touch devices cannot be bit-banged. Instead,
  138. * the CPLD provides an eight-bit shift register and a crude
  139. * interface. */
  140. if ( ssp_configuration.device == DEVICE_CODEC
  141. || ssp_configuration.device == DEVICE_TOUCH) {
  142. int select = 0;
  143. PRINTK ("spi(%d %d.%d) 0x%04x",
  144. ssp_configuration.device, cwrite, cread,
  145. v);
  146. #if defined (TALK)
  147. if (ssp_configuration.device == DEVICE_CODEC)
  148. PRINTK (" 0x%03x -> %2d", v & 0x1ff, (v >> 9) & 0x7f);
  149. #endif
  150. PRINTK ("\n");
  151. if (ssp_configuration.device == DEVICE_CODEC)
  152. select = CPLD_SPIC_CS_CODEC;
  153. if (ssp_configuration.device == DEVICE_TOUCH)
  154. select = CPLD_SPIC_CS_TOUCH;
  155. if (cwrite) {
  156. for (cwrite = (cwrite + 7)/8; cwrite-- > 0; ) {
  157. CPLD_SPID = (v >> (8*cwrite)) & 0xff;
  158. CPLD_SPIC = select | CPLD_SPIC_LOAD;
  159. while (!(CPLD_SPIC & CPLD_SPIC_LOADED))
  160. ;
  161. CPLD_SPIC = select;
  162. while (!(CPLD_SPIC & CPLD_SPIC_DONE))
  163. ;
  164. }
  165. v = 0;
  166. }
  167. if (cread) {
  168. mdelay (2); /* *** FIXME: required by ads7843? */
  169. v = 0;
  170. for (cread = (cread + 7)/8; cread-- > 0;) {
  171. CPLD_SPID = 0;
  172. CPLD_SPIC = select | CPLD_SPIC_READ
  173. | CPLD_SPIC_START;
  174. while (!(CPLD_SPIC & CPLD_SPIC_LOADED))
  175. ;
  176. CPLD_SPIC = select | CPLD_SPIC_READ;
  177. while (!(CPLD_SPIC & CPLD_SPIC_DONE))
  178. ;
  179. v = (v << 8) | CPLD_SPID;
  180. }
  181. }
  182. return v;
  183. }
  184. #endif
  185. PRINTK ("spi(%d) 0x%04x -> 0x%x\r\n", ssp_configuration.device,
  186. v & 0x1ff, (v >> 9) & 0x7f);
  187. enable_cs ();
  188. v <<= CPLD_SPI_TX_SHIFT; /* Correction for position of SPI_TX bit */
  189. while (cwrite--) {
  190. CPLD_SPI
  191. = (CPLD_SPI & ~CPLD_SPI_TX)
  192. | ((v >> cwrite) & CPLD_SPI_TX);
  193. udelay (T_DIS);
  194. pulse_clock ();
  195. }
  196. if (cread < 0) {
  197. int delay = 10;
  198. disable_cs ();
  199. udelay (1);
  200. enable_cs ();
  201. l = -1;
  202. do {
  203. if (CPLD_SPI & CPLD_SPI_RX) {
  204. l = 0;
  205. break;
  206. }
  207. } while (udelay (1), --delay);
  208. }
  209. else
  210. /* We pulse the clock before the data to skip the leading zero. */
  211. while (cread-- > 0) {
  212. pulse_clock ();
  213. l = (l<<1)
  214. | (((CPLD_SPI & CPLD_SPI_RX)
  215. >> CPLD_SPI_RX_SHIFT) & 0x1);
  216. }
  217. disable_cs ();
  218. return l;
  219. }
  220. static int ssp_init (void)
  221. {
  222. spin_lock_init (&ssp_lock);
  223. memset (&ssp_configuration, 0, sizeof (ssp_configuration));
  224. return 0;
  225. }
  226. /* ssp_chip_select
  227. drops the chip select line for the CPLD shift-register controlled
  228. devices. It doesn't enable chip
  229. */
  230. static void ssp_chip_select (int enable)
  231. {
  232. #if defined (CONFIG_MACH_LPD7A400)
  233. int select;
  234. if (ssp_configuration.device == DEVICE_CODEC)
  235. select = CPLD_SPIC_CS_CODEC;
  236. else if (ssp_configuration.device == DEVICE_TOUCH)
  237. select = CPLD_SPIC_CS_TOUCH;
  238. else
  239. return;
  240. if (enable)
  241. CPLD_SPIC = select;
  242. else
  243. CPLD_SPIC = 0;
  244. #endif
  245. }
  246. static void ssp_acquire (void)
  247. {
  248. spin_lock (&ssp_lock);
  249. }
  250. static void ssp_release (void)
  251. {
  252. ssp_chip_select (0); /* just in case */
  253. spin_unlock (&ssp_lock);
  254. }
  255. static int ssp_configure (int device, int mode, int speed,
  256. int frame_size_write, int frame_size_read)
  257. {
  258. ssp_configuration.device = device;
  259. ssp_configuration.mode = mode;
  260. ssp_configuration.speed = speed;
  261. ssp_configuration.frame_size_write = frame_size_write;
  262. ssp_configuration.frame_size_read = frame_size_read;
  263. return 0;
  264. }
  265. static int ssp_read (void)
  266. {
  267. return execute_spi_command (0, 0, ssp_configuration.frame_size_read);
  268. }
  269. static int ssp_write (u16 data)
  270. {
  271. execute_spi_command (data, ssp_configuration.frame_size_write, 0);
  272. return 0;
  273. }
  274. static int ssp_write_read (u16 data)
  275. {
  276. return execute_spi_command (data, ssp_configuration.frame_size_write,
  277. ssp_configuration.frame_size_read);
  278. }
  279. struct ssp_driver lh7a40x_cpld_ssp_driver = {
  280. .init = ssp_init,
  281. .acquire = ssp_acquire,
  282. .release = ssp_release,
  283. .configure = ssp_configure,
  284. .chip_select = ssp_chip_select,
  285. .read = ssp_read,
  286. .write = ssp_write,
  287. .write_read = ssp_write_read,
  288. };
  289. MODULE_AUTHOR("Marc Singer");
  290. MODULE_DESCRIPTION("LPD7A40X CPLD SPI driver");
  291. MODULE_LICENSE("GPL");