omap3_spi.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (C) 2010 Dirk Behme <dirk.behme@googlemail.com>
  3. *
  4. * Driver for McSPI controller on OMAP3. Based on davinci_spi.c
  5. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  6. *
  7. * Copyright (C) 2007 Atmel Corporation
  8. *
  9. * Parts taken from linux/drivers/spi/omap2_mcspi.c
  10. * Copyright (C) 2005, 2006 Nokia Corporation
  11. *
  12. * Modified by Ruslan Araslanov <ruslan.araslanov@vitecmm.com>
  13. *
  14. * See file CREDITS for list of people who contributed to this
  15. * project.
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30. * MA 02111-1307 USA
  31. *
  32. */
  33. #include <common.h>
  34. #include <spi.h>
  35. #include <malloc.h>
  36. #include <asm/io.h>
  37. #include "omap3_spi.h"
  38. #define WORD_LEN 8
  39. #define SPI_WAIT_TIMEOUT 3000000;
  40. static void spi_reset(struct omap3_spi_slave *ds)
  41. {
  42. unsigned int tmp;
  43. writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &ds->regs->sysconfig);
  44. do {
  45. tmp = readl(&ds->regs->sysstatus);
  46. } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
  47. writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
  48. OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
  49. OMAP3_MCSPI_SYSCONFIG_SMARTIDLE,
  50. &ds->regs->sysconfig);
  51. writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &ds->regs->wakeupenable);
  52. }
  53. void spi_init()
  54. {
  55. /* do nothing */
  56. }
  57. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  58. unsigned int max_hz, unsigned int mode)
  59. {
  60. struct omap3_spi_slave *ds;
  61. ds = malloc(sizeof(struct omap3_spi_slave));
  62. if (!ds) {
  63. printf("SPI error: malloc of SPI structure failed\n");
  64. return NULL;
  65. }
  66. /*
  67. * OMAP3 McSPI (MultiChannel SPI) has 4 busses (modules)
  68. * with different number of chip selects (CS, channels):
  69. * McSPI1 has 4 CS (bus 0, cs 0 - 3)
  70. * McSPI2 has 2 CS (bus 1, cs 0 - 1)
  71. * McSPI3 has 2 CS (bus 2, cs 0 - 1)
  72. * McSPI4 has 1 CS (bus 3, cs 0)
  73. */
  74. switch (bus) {
  75. case 0:
  76. ds->regs = (struct mcspi *)OMAP3_MCSPI1_BASE;
  77. break;
  78. case 1:
  79. ds->regs = (struct mcspi *)OMAP3_MCSPI2_BASE;
  80. break;
  81. case 2:
  82. ds->regs = (struct mcspi *)OMAP3_MCSPI3_BASE;
  83. break;
  84. case 3:
  85. ds->regs = (struct mcspi *)OMAP3_MCSPI4_BASE;
  86. break;
  87. default:
  88. printf("SPI error: unsupported bus %i. \
  89. Supported busses 0 - 3\n", bus);
  90. return NULL;
  91. }
  92. ds->slave.bus = bus;
  93. if (((bus == 0) && (cs > 3)) ||
  94. ((bus == 1) && (cs > 1)) ||
  95. ((bus == 2) && (cs > 1)) ||
  96. ((bus == 3) && (cs > 0))) {
  97. printf("SPI error: unsupported chip select %i \
  98. on bus %i\n", cs, bus);
  99. return NULL;
  100. }
  101. ds->slave.cs = cs;
  102. if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
  103. printf("SPI error: unsupported frequency %i Hz. \
  104. Max frequency is 48 Mhz\n", max_hz);
  105. return NULL;
  106. }
  107. ds->freq = max_hz;
  108. if (mode > SPI_MODE_3) {
  109. printf("SPI error: unsupported SPI mode %i\n", mode);
  110. return NULL;
  111. }
  112. ds->mode = mode;
  113. return &ds->slave;
  114. }
  115. void spi_free_slave(struct spi_slave *slave)
  116. {
  117. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  118. free(ds);
  119. }
  120. int spi_claim_bus(struct spi_slave *slave)
  121. {
  122. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  123. unsigned int conf, div = 0;
  124. /* McSPI global module configuration */
  125. /*
  126. * setup when switching from (reset default) slave mode
  127. * to single-channel master mode
  128. */
  129. spi_reset(ds);
  130. conf = readl(&ds->regs->modulctrl);
  131. conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
  132. conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
  133. writel(conf, &ds->regs->modulctrl);
  134. /* McSPI individual channel configuration */
  135. /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
  136. if (ds->freq) {
  137. while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
  138. > ds->freq)
  139. div++;
  140. } else
  141. div = 0xC;
  142. conf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  143. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  144. * REVISIT: this controller could support SPI_3WIRE mode.
  145. */
  146. conf &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
  147. conf |= OMAP3_MCSPI_CHCONF_DPE0;
  148. /* wordlength */
  149. conf &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
  150. conf |= (WORD_LEN - 1) << 7;
  151. /* set chipselect polarity; manage with FORCE */
  152. if (!(ds->mode & SPI_CS_HIGH))
  153. conf |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
  154. else
  155. conf &= ~OMAP3_MCSPI_CHCONF_EPOL;
  156. /* set clock divisor */
  157. conf &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
  158. conf |= div << 2;
  159. /* set SPI mode 0..3 */
  160. if (ds->mode & SPI_CPOL)
  161. conf |= OMAP3_MCSPI_CHCONF_POL;
  162. else
  163. conf &= ~OMAP3_MCSPI_CHCONF_POL;
  164. if (ds->mode & SPI_CPHA)
  165. conf |= OMAP3_MCSPI_CHCONF_PHA;
  166. else
  167. conf &= ~OMAP3_MCSPI_CHCONF_PHA;
  168. /* Transmit & receive mode */
  169. conf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  170. writel(conf, &ds->regs->channel[ds->slave.cs].chconf);
  171. return 0;
  172. }
  173. void spi_release_bus(struct spi_slave *slave)
  174. {
  175. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  176. /* Reset the SPI hardware */
  177. spi_reset(ds);
  178. }
  179. int omap3_spi_write(struct spi_slave *slave, unsigned int len, const u8 *txp,
  180. unsigned long flags)
  181. {
  182. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  183. int i;
  184. int timeout = SPI_WAIT_TIMEOUT;
  185. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  186. if (flags & SPI_XFER_BEGIN)
  187. writel(OMAP3_MCSPI_CHCTRL_EN,
  188. &ds->regs->channel[ds->slave.cs].chctrl);
  189. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  190. chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
  191. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  192. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  193. for (i = 0; i < len; i++) {
  194. /* wait till TX register is empty (TXS == 1) */
  195. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  196. OMAP3_MCSPI_CHSTAT_TXS)) {
  197. if (--timeout <= 0) {
  198. printf("SPI TXS timed out, status=0x%08x\n",
  199. readl(&ds->regs->channel[ds->slave.cs].chstat));
  200. return -1;
  201. }
  202. }
  203. /* Write the data */
  204. writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
  205. }
  206. if (flags & SPI_XFER_END) {
  207. /* wait to finish of transfer */
  208. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  209. OMAP3_MCSPI_CHSTAT_EOT));
  210. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  211. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  212. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  213. }
  214. return 0;
  215. }
  216. int omap3_spi_read(struct spi_slave *slave, unsigned int len, u8 *rxp,
  217. unsigned long flags)
  218. {
  219. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  220. int i;
  221. int timeout = SPI_WAIT_TIMEOUT;
  222. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  223. if (flags & SPI_XFER_BEGIN)
  224. writel(OMAP3_MCSPI_CHCTRL_EN,
  225. &ds->regs->channel[ds->slave.cs].chctrl);
  226. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  227. chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
  228. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  229. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  230. writel(0, &ds->regs->channel[ds->slave.cs].tx);
  231. for (i = 0; i < len; i++) {
  232. /* Wait till RX register contains data (RXS == 1) */
  233. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  234. OMAP3_MCSPI_CHSTAT_RXS)) {
  235. if (--timeout <= 0) {
  236. printf("SPI RXS timed out, status=0x%08x\n",
  237. readl(&ds->regs->channel[ds->slave.cs].chstat));
  238. return -1;
  239. }
  240. }
  241. /* Read the data */
  242. rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
  243. }
  244. if (flags & SPI_XFER_END) {
  245. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  246. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  247. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  248. }
  249. return 0;
  250. }
  251. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  252. const void *dout, void *din, unsigned long flags)
  253. {
  254. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  255. unsigned int len;
  256. const u8 *txp = dout;
  257. u8 *rxp = din;
  258. int ret = -1;
  259. if (bitlen % 8)
  260. return -1;
  261. len = bitlen / 8;
  262. if (bitlen == 0) { /* only change CS */
  263. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  264. if (flags & SPI_XFER_BEGIN) {
  265. writel(OMAP3_MCSPI_CHCTRL_EN,
  266. &ds->regs->channel[ds->slave.cs].chctrl);
  267. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  268. writel(chconf,
  269. &ds->regs->channel[ds->slave.cs].chconf);
  270. }
  271. if (flags & SPI_XFER_END) {
  272. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  273. writel(chconf,
  274. &ds->regs->channel[ds->slave.cs].chconf);
  275. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  276. }
  277. ret = 0;
  278. } else {
  279. if (dout != NULL)
  280. ret = omap3_spi_write(slave, len, txp, flags);
  281. if (din != NULL)
  282. ret = omap3_spi_read(slave, len, rxp, flags);
  283. }
  284. return ret;
  285. }
  286. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  287. {
  288. return 1;
  289. }
  290. void spi_cs_activate(struct spi_slave *slave)
  291. {
  292. }
  293. void spi_cs_deactivate(struct spi_slave *slave)
  294. {
  295. }