omap3_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #ifdef OMAP3_MCSPI2_BASE
  79. case 1:
  80. ds->regs = (struct mcspi *)OMAP3_MCSPI2_BASE;
  81. break;
  82. #endif
  83. #ifdef OMAP3_MCSPI3_BASE
  84. case 2:
  85. ds->regs = (struct mcspi *)OMAP3_MCSPI3_BASE;
  86. break;
  87. #endif
  88. #ifdef OMAP3_MCSPI4_BASE
  89. case 3:
  90. ds->regs = (struct mcspi *)OMAP3_MCSPI4_BASE;
  91. break;
  92. #endif
  93. default:
  94. printf("SPI error: unsupported bus %i. \
  95. Supported busses 0 - 3\n", bus);
  96. return NULL;
  97. }
  98. ds->slave.bus = bus;
  99. if (((bus == 0) && (cs > 3)) ||
  100. ((bus == 1) && (cs > 1)) ||
  101. ((bus == 2) && (cs > 1)) ||
  102. ((bus == 3) && (cs > 0))) {
  103. printf("SPI error: unsupported chip select %i \
  104. on bus %i\n", cs, bus);
  105. return NULL;
  106. }
  107. ds->slave.cs = cs;
  108. if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
  109. printf("SPI error: unsupported frequency %i Hz. \
  110. Max frequency is 48 Mhz\n", max_hz);
  111. return NULL;
  112. }
  113. ds->freq = max_hz;
  114. if (mode > SPI_MODE_3) {
  115. printf("SPI error: unsupported SPI mode %i\n", mode);
  116. return NULL;
  117. }
  118. ds->mode = mode;
  119. return &ds->slave;
  120. }
  121. void spi_free_slave(struct spi_slave *slave)
  122. {
  123. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  124. free(ds);
  125. }
  126. int spi_claim_bus(struct spi_slave *slave)
  127. {
  128. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  129. unsigned int conf, div = 0;
  130. /* McSPI global module configuration */
  131. /*
  132. * setup when switching from (reset default) slave mode
  133. * to single-channel master mode
  134. */
  135. spi_reset(ds);
  136. conf = readl(&ds->regs->modulctrl);
  137. conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
  138. conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
  139. writel(conf, &ds->regs->modulctrl);
  140. /* McSPI individual channel configuration */
  141. /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
  142. if (ds->freq) {
  143. while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
  144. > ds->freq)
  145. div++;
  146. } else
  147. div = 0xC;
  148. conf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  149. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  150. * REVISIT: this controller could support SPI_3WIRE mode.
  151. */
  152. #ifdef CONFIG_OMAP3_SPI_D0_D1_SWAPPED
  153. /*
  154. * Some boards have D0 wired as MOSI / D1 as MISO instead of
  155. * The normal D0 as MISO / D1 as MOSI.
  156. */
  157. conf &= ~OMAP3_MCSPI_CHCONF_DPE0;
  158. conf |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
  159. #else
  160. conf &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
  161. conf |= OMAP3_MCSPI_CHCONF_DPE0;
  162. #endif
  163. /* wordlength */
  164. conf &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
  165. conf |= (WORD_LEN - 1) << 7;
  166. /* set chipselect polarity; manage with FORCE */
  167. if (!(ds->mode & SPI_CS_HIGH))
  168. conf |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
  169. else
  170. conf &= ~OMAP3_MCSPI_CHCONF_EPOL;
  171. /* set clock divisor */
  172. conf &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
  173. conf |= div << 2;
  174. /* set SPI mode 0..3 */
  175. if (ds->mode & SPI_CPOL)
  176. conf |= OMAP3_MCSPI_CHCONF_POL;
  177. else
  178. conf &= ~OMAP3_MCSPI_CHCONF_POL;
  179. if (ds->mode & SPI_CPHA)
  180. conf |= OMAP3_MCSPI_CHCONF_PHA;
  181. else
  182. conf &= ~OMAP3_MCSPI_CHCONF_PHA;
  183. /* Transmit & receive mode */
  184. conf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  185. writel(conf, &ds->regs->channel[ds->slave.cs].chconf);
  186. return 0;
  187. }
  188. void spi_release_bus(struct spi_slave *slave)
  189. {
  190. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  191. /* Reset the SPI hardware */
  192. spi_reset(ds);
  193. }
  194. int omap3_spi_write(struct spi_slave *slave, unsigned int len, const u8 *txp,
  195. unsigned long flags)
  196. {
  197. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  198. int i;
  199. int timeout = SPI_WAIT_TIMEOUT;
  200. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  201. if (flags & SPI_XFER_BEGIN)
  202. writel(OMAP3_MCSPI_CHCTRL_EN,
  203. &ds->regs->channel[ds->slave.cs].chctrl);
  204. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  205. chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
  206. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  207. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  208. for (i = 0; i < len; i++) {
  209. /* wait till TX register is empty (TXS == 1) */
  210. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  211. OMAP3_MCSPI_CHSTAT_TXS)) {
  212. if (--timeout <= 0) {
  213. printf("SPI TXS timed out, status=0x%08x\n",
  214. readl(&ds->regs->channel[ds->slave.cs].chstat));
  215. return -1;
  216. }
  217. }
  218. /* Write the data */
  219. writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
  220. }
  221. if (flags & SPI_XFER_END) {
  222. /* wait to finish of transfer */
  223. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  224. OMAP3_MCSPI_CHSTAT_EOT));
  225. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  226. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  227. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  228. }
  229. return 0;
  230. }
  231. int omap3_spi_read(struct spi_slave *slave, unsigned int len, u8 *rxp,
  232. unsigned long flags)
  233. {
  234. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  235. int i;
  236. int timeout = SPI_WAIT_TIMEOUT;
  237. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  238. if (flags & SPI_XFER_BEGIN)
  239. writel(OMAP3_MCSPI_CHCTRL_EN,
  240. &ds->regs->channel[ds->slave.cs].chctrl);
  241. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  242. chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
  243. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  244. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  245. writel(0, &ds->regs->channel[ds->slave.cs].tx);
  246. for (i = 0; i < len; i++) {
  247. /* Wait till RX register contains data (RXS == 1) */
  248. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  249. OMAP3_MCSPI_CHSTAT_RXS)) {
  250. if (--timeout <= 0) {
  251. printf("SPI RXS timed out, status=0x%08x\n",
  252. readl(&ds->regs->channel[ds->slave.cs].chstat));
  253. return -1;
  254. }
  255. }
  256. /* Read the data */
  257. rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
  258. }
  259. if (flags & SPI_XFER_END) {
  260. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  261. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  262. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  263. }
  264. return 0;
  265. }
  266. /*McSPI Transmit Receive Mode*/
  267. int omap3_spi_txrx(struct spi_slave *slave,
  268. unsigned int len, const u8 *txp, u8 *rxp, unsigned long flags)
  269. {
  270. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  271. int timeout = SPI_WAIT_TIMEOUT;
  272. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  273. int irqstatus = readl(&ds->regs->irqstatus);
  274. int i=0;
  275. /*Enable SPI channel*/
  276. if (flags & SPI_XFER_BEGIN)
  277. writel(OMAP3_MCSPI_CHCTRL_EN,
  278. &ds->regs->channel[ds->slave.cs].chctrl);
  279. /*set TRANSMIT-RECEIVE Mode*/
  280. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  281. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  282. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  283. /*Shift in and out 1 byte at time*/
  284. for (i=0; i < len; i++){
  285. /* Write: wait for TX empty (TXS == 1)*/
  286. irqstatus |= (1<< (4*(ds->slave.bus)));
  287. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  288. OMAP3_MCSPI_CHSTAT_TXS)) {
  289. if (--timeout <= 0) {
  290. printf("SPI TXS timed out, status=0x%08x\n",
  291. readl(&ds->regs->channel[ds->slave.cs].chstat));
  292. return -1;
  293. }
  294. }
  295. /* Write the data */
  296. writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
  297. /*Read: wait for RX containing data (RXS == 1)*/
  298. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  299. OMAP3_MCSPI_CHSTAT_RXS)) {
  300. if (--timeout <= 0) {
  301. printf("SPI RXS timed out, status=0x%08x\n",
  302. readl(&ds->regs->channel[ds->slave.cs].chstat));
  303. return -1;
  304. }
  305. }
  306. /* Read the data */
  307. rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
  308. }
  309. /*if transfer must be terminated disable the channel*/
  310. if (flags & SPI_XFER_END) {
  311. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  312. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  313. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  314. }
  315. return 0;
  316. }
  317. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  318. const void *dout, void *din, unsigned long flags)
  319. {
  320. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  321. unsigned int len;
  322. const u8 *txp = dout;
  323. u8 *rxp = din;
  324. int ret = -1;
  325. if (bitlen % 8)
  326. return -1;
  327. len = bitlen / 8;
  328. if (bitlen == 0) { /* only change CS */
  329. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  330. if (flags & SPI_XFER_BEGIN) {
  331. writel(OMAP3_MCSPI_CHCTRL_EN,
  332. &ds->regs->channel[ds->slave.cs].chctrl);
  333. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  334. writel(chconf,
  335. &ds->regs->channel[ds->slave.cs].chconf);
  336. }
  337. if (flags & SPI_XFER_END) {
  338. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  339. writel(chconf,
  340. &ds->regs->channel[ds->slave.cs].chconf);
  341. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  342. }
  343. ret = 0;
  344. } else {
  345. if (dout != NULL && din != NULL)
  346. ret = omap3_spi_txrx(slave, len, txp, rxp, flags);
  347. else if (dout != NULL)
  348. ret = omap3_spi_write(slave, len, txp, flags);
  349. else if (din != NULL)
  350. ret = omap3_spi_read(slave, len, rxp, flags);
  351. }
  352. return ret;
  353. }
  354. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  355. {
  356. return 1;
  357. }
  358. void spi_cs_activate(struct spi_slave *slave)
  359. {
  360. }
  361. void spi_cs_deactivate(struct spi_slave *slave)
  362. {
  363. }