dspi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 = GPIO_PAR_DSPI_PCS5_PCS5 | GPIO_PAR_DSPI_PCS2_PCS2 |
  37. GPIO_PAR_DSPI_PCS1_PCS1 | GPIO_PAR_DSPI_PCS0_PCS0 |
  38. GPIO_PAR_DSPI_SIN_SIN | GPIO_PAR_DSPI_SOUT_SOUT |
  39. GPIO_PAR_DSPI_SCK_SCK;
  40. dspi->dmcr = DSPI_DMCR_MSTR | DSPI_DMCR_CSIS7 | DSPI_DMCR_CSIS6 |
  41. DSPI_DMCR_CSIS5 | DSPI_DMCR_CSIS4 | DSPI_DMCR_CSIS3 |
  42. DSPI_DMCR_CSIS2 | DSPI_DMCR_CSIS1 | DSPI_DMCR_CSIS0 |
  43. DSPI_DMCR_CRXF | DSPI_DMCR_CTXF;
  44. #ifdef CONFIG_SYS_DSPI_DCTAR0
  45. dspi->dctar0 = CONFIG_SYS_DSPI_DCTAR0;
  46. #endif
  47. #ifdef CONFIG_SYS_DSPI_DCTAR1
  48. dspi->dctar1 = CONFIG_SYS_DSPI_DCTAR1;
  49. #endif
  50. #ifdef CONFIG_SYS_DSPI_DCTAR2
  51. dspi->dctar2 = CONFIG_SYS_DSPI_DCTAR2;
  52. #endif
  53. #ifdef CONFIG_SYS_DSPI_DCTAR3
  54. dspi->dctar3 = CONFIG_SYS_DSPI_DCTAR3;
  55. #endif
  56. #ifdef CONFIG_SYS_DSPI_DCTAR4
  57. dspi->dctar4 = CONFIG_SYS_DSPI_DCTAR4;
  58. #endif
  59. #ifdef CONFIG_SYS_DSPI_DCTAR5
  60. dspi->dctar5 = CONFIG_SYS_DSPI_DCTAR5;
  61. #endif
  62. #ifdef CONFIG_SYS_DSPI_DCTAR6
  63. dspi->dctar6 = CONFIG_SYS_DSPI_DCTAR6;
  64. #endif
  65. #ifdef CONFIG_SYS_DSPI_DCTAR7
  66. dspi->dctar7 = CONFIG_SYS_DSPI_DCTAR7;
  67. #endif
  68. }
  69. void dspi_tx(int chipsel, u8 attrib, u16 data)
  70. {
  71. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  72. while ((dspi->dsr & 0x0000F000) >= 4) ;
  73. dspi->dtfr = (attrib << 24) | ((1 << chipsel) << 16) | data;
  74. }
  75. u16 dspi_rx(void)
  76. {
  77. volatile dspi_t *dspi = (dspi_t *) MMAP_DSPI;
  78. while ((dspi->dsr & 0x000000F0) == 0) ;
  79. return (dspi->drfr & 0xFFFF);
  80. }
  81. #if defined(CONFIG_CMD_SPI)
  82. void spi_init_f(void)
  83. {
  84. }
  85. void spi_init_r(void)
  86. {
  87. }
  88. void spi_init(void)
  89. {
  90. dspi_init();
  91. }
  92. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  93. unsigned int max_hz, unsigned int mode)
  94. {
  95. struct spi_slave *slave;
  96. slave = malloc(sizeof(struct spi_slave));
  97. if (!slave)
  98. return NULL;
  99. slave->bus = bus;
  100. slave->cs = cs;
  101. return slave;
  102. }
  103. void spi_free_slave(struct spi_slave *slave)
  104. {
  105. free(slave);
  106. }
  107. int spi_claim_bus(struct spi_slave *slave)
  108. {
  109. return 0;
  110. }
  111. void spi_release_bus(struct spi_slave *slave)
  112. {
  113. }
  114. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  115. void *din, unsigned long flags)
  116. {
  117. static int bWrite = 0;
  118. u8 *spi_rd, *spi_wr;
  119. int len = bitlen >> 3;
  120. spi_rd = (u8 *) din;
  121. spi_wr = (u8 *) dout;
  122. /* command handling */
  123. if (((len == 4) || (len == 1) || (len == 5)) && (dout != NULL)) {
  124. switch (*spi_wr) {
  125. case 0x02: /* Page Prog */
  126. bWrite = 1;
  127. dspi_tx(slave->cs, 0x80, spi_wr[0]);
  128. dspi_rx();
  129. dspi_tx(slave->cs, 0x80, spi_wr[1]);
  130. dspi_rx();
  131. dspi_tx(slave->cs, 0x80, spi_wr[2]);
  132. dspi_rx();
  133. dspi_tx(slave->cs, 0x80, spi_wr[3]);
  134. dspi_rx();
  135. return 0;
  136. case 0x05: /* Read Status */
  137. if (len == 4)
  138. if ((spi_wr[1] == 0xFF) && (spi_wr[2] == 0xFF)
  139. && (spi_wr[3] == 0xFF)) {
  140. dspi_tx(slave->cs, 0x80, *spi_wr);
  141. dspi_rx();
  142. }
  143. return 0;
  144. case 0x06: /* WREN */
  145. dspi_tx(slave->cs, 0x00, *spi_wr);
  146. dspi_rx();
  147. return 0;
  148. case 0x0B: /* Fast read */
  149. if ((len == 5) && (spi_wr[4] == 0)) {
  150. dspi_tx(slave->cs, 0x80, spi_wr[0]);
  151. dspi_rx();
  152. dspi_tx(slave->cs, 0x80, spi_wr[1]);
  153. dspi_rx();
  154. dspi_tx(slave->cs, 0x80, spi_wr[2]);
  155. dspi_rx();
  156. dspi_tx(slave->cs, 0x80, spi_wr[3]);
  157. dspi_rx();
  158. dspi_tx(slave->cs, 0x80, spi_wr[4]);
  159. dspi_rx();
  160. }
  161. return 0;
  162. case 0x9F: /* RDID */
  163. dspi_tx(slave->cs, 0x80, *spi_wr);
  164. dspi_rx();
  165. return 0;
  166. case 0xD8: /* Sector erase */
  167. if (len == 4)
  168. if ((spi_wr[2] == 0) && (spi_wr[3] == 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, 0x00, spi_wr[3]);
  176. dspi_rx();
  177. }
  178. return 0;
  179. }
  180. }
  181. if (bWrite)
  182. len--;
  183. while (len--) {
  184. if (dout != NULL) {
  185. dspi_tx(slave->cs, 0x80, *spi_wr);
  186. dspi_rx();
  187. spi_wr++;
  188. }
  189. if (din != NULL) {
  190. dspi_tx(slave->cs, 0x80, 0);
  191. *spi_rd = dspi_rx();
  192. spi_rd++;
  193. }
  194. }
  195. if (flags == SPI_XFER_END) {
  196. if (bWrite) {
  197. dspi_tx(slave->cs, 0x00, *spi_wr);
  198. dspi_rx();
  199. bWrite = 0;
  200. } else {
  201. dspi_tx(slave->cs, 0x00, 0);
  202. dspi_rx();
  203. }
  204. }
  205. return 0;
  206. }
  207. #endif /* CONFIG_CMD_SPI */
  208. #endif /* CONFIG_CF_DSPI */