omap3_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /*McSPI Transmit Receive Mode*/
  252. int omap3_spi_txrx(struct spi_slave *slave,
  253. unsigned int len, const u8 *txp, u8 *rxp, unsigned long flags)
  254. {
  255. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  256. int timeout = SPI_WAIT_TIMEOUT;
  257. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  258. int irqstatus = readl(&ds->regs->irqstatus);
  259. int i=0;
  260. /*Enable SPI channel*/
  261. if (flags & SPI_XFER_BEGIN)
  262. writel(OMAP3_MCSPI_CHCTRL_EN,
  263. &ds->regs->channel[ds->slave.cs].chctrl);
  264. /*set TRANSMIT-RECEIVE Mode*/
  265. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  266. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  267. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  268. /*Shift in and out 1 byte at time*/
  269. for (i=0; i < len; i++){
  270. /* Write: wait for TX empty (TXS == 1)*/
  271. irqstatus |= (1<< (4*(ds->slave.bus)));
  272. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  273. OMAP3_MCSPI_CHSTAT_TXS)) {
  274. if (--timeout <= 0) {
  275. printf("SPI TXS timed out, status=0x%08x\n",
  276. readl(&ds->regs->channel[ds->slave.cs].chstat));
  277. return -1;
  278. }
  279. }
  280. /* Write the data */
  281. writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
  282. /*Read: wait for RX containing data (RXS == 1)*/
  283. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  284. OMAP3_MCSPI_CHSTAT_RXS)) {
  285. if (--timeout <= 0) {
  286. printf("SPI RXS timed out, status=0x%08x\n",
  287. readl(&ds->regs->channel[ds->slave.cs].chstat));
  288. return -1;
  289. }
  290. }
  291. /* Read the data */
  292. rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
  293. }
  294. /*if transfer must be terminated disable the channel*/
  295. if (flags & SPI_XFER_END) {
  296. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  297. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  298. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  299. }
  300. return 0;
  301. }
  302. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  303. const void *dout, void *din, unsigned long flags)
  304. {
  305. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  306. unsigned int len;
  307. const u8 *txp = dout;
  308. u8 *rxp = din;
  309. int ret = -1;
  310. if (bitlen % 8)
  311. return -1;
  312. len = bitlen / 8;
  313. if (bitlen == 0) { /* only change CS */
  314. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  315. if (flags & SPI_XFER_BEGIN) {
  316. writel(OMAP3_MCSPI_CHCTRL_EN,
  317. &ds->regs->channel[ds->slave.cs].chctrl);
  318. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  319. writel(chconf,
  320. &ds->regs->channel[ds->slave.cs].chconf);
  321. }
  322. if (flags & SPI_XFER_END) {
  323. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  324. writel(chconf,
  325. &ds->regs->channel[ds->slave.cs].chconf);
  326. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  327. }
  328. ret = 0;
  329. } else {
  330. if (dout != NULL && din != NULL)
  331. ret = omap3_spi_txrx(slave, len, txp, rxp, flags);
  332. else if (dout != NULL)
  333. ret = omap3_spi_write(slave, len, txp, flags);
  334. else if (din != NULL)
  335. ret = omap3_spi_read(slave, len, rxp, flags);
  336. }
  337. return ret;
  338. }
  339. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  340. {
  341. return 1;
  342. }
  343. void spi_cs_activate(struct spi_slave *slave)
  344. {
  345. }
  346. void spi_cs_deactivate(struct spi_slave *slave)
  347. {
  348. }