omap3_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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_AM33XX
  153. /*
  154. * The reference design on AM33xx has D0 and D1 wired up opposite
  155. * of how it has been done on previous platforms. We assume that
  156. * custom hardware will also follow this convention.
  157. */
  158. conf &= OMAP3_MCSPI_CHCONF_DPE0;
  159. conf |= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
  160. #else
  161. conf &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
  162. conf |= OMAP3_MCSPI_CHCONF_DPE0;
  163. #endif
  164. /* wordlength */
  165. conf &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
  166. conf |= (WORD_LEN - 1) << 7;
  167. /* set chipselect polarity; manage with FORCE */
  168. if (!(ds->mode & SPI_CS_HIGH))
  169. conf |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
  170. else
  171. conf &= ~OMAP3_MCSPI_CHCONF_EPOL;
  172. /* set clock divisor */
  173. conf &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
  174. conf |= div << 2;
  175. /* set SPI mode 0..3 */
  176. if (ds->mode & SPI_CPOL)
  177. conf |= OMAP3_MCSPI_CHCONF_POL;
  178. else
  179. conf &= ~OMAP3_MCSPI_CHCONF_POL;
  180. if (ds->mode & SPI_CPHA)
  181. conf |= OMAP3_MCSPI_CHCONF_PHA;
  182. else
  183. conf &= ~OMAP3_MCSPI_CHCONF_PHA;
  184. /* Transmit & receive mode */
  185. conf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  186. writel(conf, &ds->regs->channel[ds->slave.cs].chconf);
  187. return 0;
  188. }
  189. void spi_release_bus(struct spi_slave *slave)
  190. {
  191. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  192. /* Reset the SPI hardware */
  193. spi_reset(ds);
  194. }
  195. int omap3_spi_write(struct spi_slave *slave, unsigned int len, const u8 *txp,
  196. unsigned long flags)
  197. {
  198. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  199. int i;
  200. int timeout = SPI_WAIT_TIMEOUT;
  201. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  202. if (flags & SPI_XFER_BEGIN)
  203. writel(OMAP3_MCSPI_CHCTRL_EN,
  204. &ds->regs->channel[ds->slave.cs].chctrl);
  205. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  206. chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
  207. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  208. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  209. for (i = 0; i < len; i++) {
  210. /* wait till TX register is empty (TXS == 1) */
  211. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  212. OMAP3_MCSPI_CHSTAT_TXS)) {
  213. if (--timeout <= 0) {
  214. printf("SPI TXS timed out, status=0x%08x\n",
  215. readl(&ds->regs->channel[ds->slave.cs].chstat));
  216. return -1;
  217. }
  218. }
  219. /* Write the data */
  220. writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
  221. }
  222. if (flags & SPI_XFER_END) {
  223. /* wait to finish of transfer */
  224. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  225. OMAP3_MCSPI_CHSTAT_EOT));
  226. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  227. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  228. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  229. }
  230. return 0;
  231. }
  232. int omap3_spi_read(struct spi_slave *slave, unsigned int len, u8 *rxp,
  233. unsigned long flags)
  234. {
  235. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  236. int i;
  237. int timeout = SPI_WAIT_TIMEOUT;
  238. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  239. if (flags & SPI_XFER_BEGIN)
  240. writel(OMAP3_MCSPI_CHCTRL_EN,
  241. &ds->regs->channel[ds->slave.cs].chctrl);
  242. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  243. chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
  244. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  245. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  246. writel(0, &ds->regs->channel[ds->slave.cs].tx);
  247. for (i = 0; i < len; i++) {
  248. /* Wait till RX register contains data (RXS == 1) */
  249. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  250. OMAP3_MCSPI_CHSTAT_RXS)) {
  251. if (--timeout <= 0) {
  252. printf("SPI RXS timed out, status=0x%08x\n",
  253. readl(&ds->regs->channel[ds->slave.cs].chstat));
  254. return -1;
  255. }
  256. }
  257. /* Read the data */
  258. rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
  259. }
  260. if (flags & SPI_XFER_END) {
  261. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  262. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  263. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  264. }
  265. return 0;
  266. }
  267. /*McSPI Transmit Receive Mode*/
  268. int omap3_spi_txrx(struct spi_slave *slave,
  269. unsigned int len, const u8 *txp, u8 *rxp, unsigned long flags)
  270. {
  271. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  272. int timeout = SPI_WAIT_TIMEOUT;
  273. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  274. int irqstatus = readl(&ds->regs->irqstatus);
  275. int i=0;
  276. /*Enable SPI channel*/
  277. if (flags & SPI_XFER_BEGIN)
  278. writel(OMAP3_MCSPI_CHCTRL_EN,
  279. &ds->regs->channel[ds->slave.cs].chctrl);
  280. /*set TRANSMIT-RECEIVE Mode*/
  281. chconf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  282. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  283. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  284. /*Shift in and out 1 byte at time*/
  285. for (i=0; i < len; i++){
  286. /* Write: wait for TX empty (TXS == 1)*/
  287. irqstatus |= (1<< (4*(ds->slave.bus)));
  288. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  289. OMAP3_MCSPI_CHSTAT_TXS)) {
  290. if (--timeout <= 0) {
  291. printf("SPI TXS timed out, status=0x%08x\n",
  292. readl(&ds->regs->channel[ds->slave.cs].chstat));
  293. return -1;
  294. }
  295. }
  296. /* Write the data */
  297. writel(txp[i], &ds->regs->channel[ds->slave.cs].tx);
  298. /*Read: wait for RX containing data (RXS == 1)*/
  299. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  300. OMAP3_MCSPI_CHSTAT_RXS)) {
  301. if (--timeout <= 0) {
  302. printf("SPI RXS timed out, status=0x%08x\n",
  303. readl(&ds->regs->channel[ds->slave.cs].chstat));
  304. return -1;
  305. }
  306. }
  307. /* Read the data */
  308. rxp[i] = readl(&ds->regs->channel[ds->slave.cs].rx);
  309. }
  310. /*if transfer must be terminated disable the channel*/
  311. if (flags & SPI_XFER_END) {
  312. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  313. writel(chconf, &ds->regs->channel[ds->slave.cs].chconf);
  314. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  315. }
  316. return 0;
  317. }
  318. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  319. const void *dout, void *din, unsigned long flags)
  320. {
  321. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  322. unsigned int len;
  323. const u8 *txp = dout;
  324. u8 *rxp = din;
  325. int ret = -1;
  326. if (bitlen % 8)
  327. return -1;
  328. len = bitlen / 8;
  329. if (bitlen == 0) { /* only change CS */
  330. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  331. if (flags & SPI_XFER_BEGIN) {
  332. writel(OMAP3_MCSPI_CHCTRL_EN,
  333. &ds->regs->channel[ds->slave.cs].chctrl);
  334. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  335. writel(chconf,
  336. &ds->regs->channel[ds->slave.cs].chconf);
  337. }
  338. if (flags & SPI_XFER_END) {
  339. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  340. writel(chconf,
  341. &ds->regs->channel[ds->slave.cs].chconf);
  342. writel(0, &ds->regs->channel[ds->slave.cs].chctrl);
  343. }
  344. ret = 0;
  345. } else {
  346. if (dout != NULL && din != NULL)
  347. ret = omap3_spi_txrx(slave, len, txp, rxp, flags);
  348. else if (dout != NULL)
  349. ret = omap3_spi_write(slave, len, txp, flags);
  350. else if (din != NULL)
  351. ret = omap3_spi_read(slave, len, rxp, flags);
  352. }
  353. return ret;
  354. }
  355. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  356. {
  357. return 1;
  358. }
  359. void spi_cs_activate(struct spi_slave *slave)
  360. {
  361. }
  362. void spi_cs_deactivate(struct spi_slave *slave)
  363. {
  364. }