dspi.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. *
  3. * (C) Copyright 2000-2003
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * Copyright (C) 2004-2008 Freescale Semiconductor, Inc.
  7. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <spi.h>
  29. #include <malloc.h>
  30. #if defined(CONFIG_CF_DSPI)
  31. #include <asm/immap.h>
  32. void dspi_init(void)
  33. {
  34. volatile gpio_t *gpio = (gpio_t *) MMAP_GPIO;
  35. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  36. gpio->par_dspi =
  37. GPIO_PAR_DSPI_SIN_SIN | GPIO_PAR_DSPI_SOUT_SOUT |
  38. GPIO_PAR_DSPI_SCK_SCK;
  39. dspi->dmcr = DSPI_DMCR_MSTR | DSPI_DMCR_CSIS7 | DSPI_DMCR_CSIS6 |
  40. DSPI_DMCR_CSIS5 | DSPI_DMCR_CSIS4 | DSPI_DMCR_CSIS3 |
  41. DSPI_DMCR_CSIS2 | DSPI_DMCR_CSIS1 | DSPI_DMCR_CSIS0 |
  42. DSPI_DMCR_CRXF | DSPI_DMCR_CTXF;
  43. #ifdef CONFIG_SYS_DSPI_DCTAR0
  44. dspi->dctar0 = CONFIG_SYS_DSPI_DCTAR0;
  45. #endif
  46. #ifdef CONFIG_SYS_DSPI_DCTAR1
  47. dspi->dctar1 = CONFIG_SYS_DSPI_DCTAR1;
  48. #endif
  49. #ifdef CONFIG_SYS_DSPI_DCTAR2
  50. dspi->dctar2 = CONFIG_SYS_DSPI_DCTAR2;
  51. #endif
  52. #ifdef CONFIG_SYS_DSPI_DCTAR3
  53. dspi->dctar3 = CONFIG_SYS_DSPI_DCTAR3;
  54. #endif
  55. #ifdef CONFIG_SYS_DSPI_DCTAR4
  56. dspi->dctar4 = CONFIG_SYS_DSPI_DCTAR4;
  57. #endif
  58. #ifdef CONFIG_SYS_DSPI_DCTAR5
  59. dspi->dctar5 = CONFIG_SYS_DSPI_DCTAR5;
  60. #endif
  61. #ifdef CONFIG_SYS_DSPI_DCTAR6
  62. dspi->dctar6 = CONFIG_SYS_DSPI_DCTAR6;
  63. #endif
  64. #ifdef CONFIG_SYS_DSPI_DCTAR7
  65. dspi->dctar7 = CONFIG_SYS_DSPI_DCTAR7;
  66. #endif
  67. }
  68. void dspi_tx(int chipsel, u8 attrib, u16 data)
  69. {
  70. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  71. while ((dspi->dsr & 0x0000F000) >= 4) ;
  72. dspi->dtfr = (attrib << 24) | ((1 << chipsel) << 16) | data;
  73. }
  74. u16 dspi_rx(void)
  75. {
  76. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  77. while ((dspi->dsr & 0x000000F0) == 0) ;
  78. return (dspi->drfr & 0xFFFF);
  79. }
  80. #if defined(CONFIG_CMD_SPI)
  81. void spi_init_f(void)
  82. {
  83. }
  84. void spi_init_r(void)
  85. {
  86. }
  87. void spi_init(void)
  88. {
  89. dspi_init();
  90. }
  91. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  92. unsigned int max_hz, unsigned int mode)
  93. {
  94. volatile gpio_t *gpio = (gpio_t *) MMAP_GPIO;
  95. struct spi_slave *slave;
  96. slave = malloc(sizeof(struct spi_slave));
  97. if (!slave)
  98. return NULL;
  99. switch (cs) {
  100. case 0:
  101. gpio->par_dspi &= ~GPIO_PAR_DSPI_PCS0_PCS0;
  102. gpio->par_dspi |= GPIO_PAR_DSPI_PCS0_PCS0;
  103. break;
  104. case 2:
  105. gpio->par_timer &= GPIO_PAR_TIMER_T2IN_MASK;
  106. gpio->par_timer |= GPIO_PAR_TIMER_T2IN_DSPIPCS2;
  107. break;
  108. }
  109. slave->bus = bus;
  110. slave->cs = cs;
  111. return slave;
  112. }
  113. void spi_free_slave(struct spi_slave *slave)
  114. {
  115. volatile gpio_t *gpio = (gpio_t *) MMAP_GPIO;
  116. switch (slave->cs) {
  117. case 0:
  118. gpio->par_dspi &= ~GPIO_PAR_DSPI_PCS0_PCS0;
  119. break;
  120. case 2:
  121. gpio->par_timer &= GPIO_PAR_TIMER_T2IN_MASK;
  122. break;
  123. }
  124. free(slave);
  125. }
  126. int spi_claim_bus(struct spi_slave *slave)
  127. {
  128. return 0;
  129. }
  130. void spi_release_bus(struct spi_slave *slave)
  131. {
  132. }
  133. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  134. void *din, unsigned long flags)
  135. {
  136. static int bWrite = 0;
  137. u8 *spi_rd, *spi_wr;
  138. int len = bitlen >> 3;
  139. spi_rd = (u8 *) din;
  140. spi_wr = (u8 *) dout;
  141. /* command handling */
  142. if (((len == 4) || (len == 1) || (len == 5)) && (dout != NULL)) {
  143. switch (*spi_wr) {
  144. case 0x02: /* Page Prog */
  145. bWrite = 1;
  146. dspi_tx(slave->cs, 0x80, spi_wr[0]);
  147. dspi_rx();
  148. dspi_tx(slave->cs, 0x80, spi_wr[1]);
  149. dspi_rx();
  150. dspi_tx(slave->cs, 0x80, spi_wr[2]);
  151. dspi_rx();
  152. dspi_tx(slave->cs, 0x80, spi_wr[3]);
  153. dspi_rx();
  154. return 0;
  155. case 0x05: /* Read Status */
  156. if (len == 4)
  157. if ((spi_wr[1] == 0xFF) && (spi_wr[2] == 0xFF)
  158. && (spi_wr[3] == 0xFF)) {
  159. dspi_tx(slave->cs, 0x80, *spi_wr);
  160. dspi_rx();
  161. }
  162. return 0;
  163. case 0x06: /* WREN */
  164. dspi_tx(slave->cs, 0x00, *spi_wr);
  165. dspi_rx();
  166. return 0;
  167. case 0x0B: /* Fast read */
  168. if ((len == 5) && (spi_wr[4] == 0)) {
  169. dspi_tx(slave->cs, 0x80, spi_wr[0]);
  170. dspi_rx();
  171. dspi_tx(slave->cs, 0x80, spi_wr[1]);
  172. dspi_rx();
  173. dspi_tx(slave->cs, 0x80, spi_wr[2]);
  174. dspi_rx();
  175. dspi_tx(slave->cs, 0x80, spi_wr[3]);
  176. dspi_rx();
  177. dspi_tx(slave->cs, 0x80, spi_wr[4]);
  178. dspi_rx();
  179. }
  180. return 0;
  181. case 0x9F: /* RDID */
  182. dspi_tx(slave->cs, 0x80, *spi_wr);
  183. dspi_rx();
  184. return 0;
  185. case 0xD8: /* Sector erase */
  186. if (len == 4)
  187. if ((spi_wr[2] == 0) && (spi_wr[3] == 0)) {
  188. dspi_tx(slave->cs, 0x80, spi_wr[0]);
  189. dspi_rx();
  190. dspi_tx(slave->cs, 0x80, spi_wr[1]);
  191. dspi_rx();
  192. dspi_tx(slave->cs, 0x80, spi_wr[2]);
  193. dspi_rx();
  194. dspi_tx(slave->cs, 0x00, spi_wr[3]);
  195. dspi_rx();
  196. }
  197. return 0;
  198. }
  199. }
  200. if (bWrite)
  201. len--;
  202. while (len--) {
  203. if (dout != NULL) {
  204. dspi_tx(slave->cs, 0x80, *spi_wr);
  205. dspi_rx();
  206. spi_wr++;
  207. }
  208. if (din != NULL) {
  209. dspi_tx(slave->cs, 0x80, 0);
  210. *spi_rd = dspi_rx();
  211. spi_rd++;
  212. }
  213. }
  214. if (flags == SPI_XFER_END) {
  215. if (bWrite) {
  216. dspi_tx(slave->cs, 0x00, *spi_wr);
  217. dspi_rx();
  218. bWrite = 0;
  219. } else {
  220. dspi_tx(slave->cs, 0x00, 0);
  221. dspi_rx();
  222. }
  223. }
  224. return 0;
  225. }
  226. #endif /* CONFIG_CMD_SPI */
  227. #endif /* CONFIG_CF_DSPI */