omap3_spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. * SPDX-License-Identifier: GPL-2.0+
  15. */
  16. #include <common.h>
  17. #include <spi.h>
  18. #include <malloc.h>
  19. #include <asm/io.h>
  20. #include "omap3_spi.h"
  21. #define SPI_WAIT_TIMEOUT 3000000
  22. static void spi_reset(struct omap3_spi_slave *ds)
  23. {
  24. unsigned int tmp;
  25. writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &ds->regs->sysconfig);
  26. do {
  27. tmp = readl(&ds->regs->sysstatus);
  28. } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
  29. writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
  30. OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
  31. OMAP3_MCSPI_SYSCONFIG_SMARTIDLE,
  32. &ds->regs->sysconfig);
  33. writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &ds->regs->wakeupenable);
  34. }
  35. static void omap3_spi_write_chconf(struct omap3_spi_slave *ds, int val)
  36. {
  37. writel(val, &ds->regs->channel[ds->slave.cs].chconf);
  38. /* Flash post writes to make immediate effect */
  39. readl(&ds->regs->channel[ds->slave.cs].chconf);
  40. }
  41. static void omap3_spi_set_enable(struct omap3_spi_slave *ds, int enable)
  42. {
  43. writel(enable, &ds->regs->channel[ds->slave.cs].chctrl);
  44. /* Flash post writes to make immediate effect */
  45. readl(&ds->regs->channel[ds->slave.cs].chctrl);
  46. }
  47. void spi_init()
  48. {
  49. /* do nothing */
  50. }
  51. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  52. unsigned int max_hz, unsigned int mode)
  53. {
  54. struct omap3_spi_slave *ds;
  55. struct mcspi *regs;
  56. /*
  57. * OMAP3 McSPI (MultiChannel SPI) has 4 busses (modules)
  58. * with different number of chip selects (CS, channels):
  59. * McSPI1 has 4 CS (bus 0, cs 0 - 3)
  60. * McSPI2 has 2 CS (bus 1, cs 0 - 1)
  61. * McSPI3 has 2 CS (bus 2, cs 0 - 1)
  62. * McSPI4 has 1 CS (bus 3, cs 0)
  63. */
  64. switch (bus) {
  65. case 0:
  66. regs = (struct mcspi *)OMAP3_MCSPI1_BASE;
  67. break;
  68. #ifdef OMAP3_MCSPI2_BASE
  69. case 1:
  70. regs = (struct mcspi *)OMAP3_MCSPI2_BASE;
  71. break;
  72. #endif
  73. #ifdef OMAP3_MCSPI3_BASE
  74. case 2:
  75. regs = (struct mcspi *)OMAP3_MCSPI3_BASE;
  76. break;
  77. #endif
  78. #ifdef OMAP3_MCSPI4_BASE
  79. case 3:
  80. regs = (struct mcspi *)OMAP3_MCSPI4_BASE;
  81. break;
  82. #endif
  83. default:
  84. printf("SPI error: unsupported bus %i. \
  85. Supported busses 0 - 3\n", bus);
  86. return NULL;
  87. }
  88. if (((bus == 0) && (cs > 3)) ||
  89. ((bus == 1) && (cs > 1)) ||
  90. ((bus == 2) && (cs > 1)) ||
  91. ((bus == 3) && (cs > 0))) {
  92. printf("SPI error: unsupported chip select %i \
  93. on bus %i\n", cs, bus);
  94. return NULL;
  95. }
  96. if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
  97. printf("SPI error: unsupported frequency %i Hz. \
  98. Max frequency is 48 Mhz\n", max_hz);
  99. return NULL;
  100. }
  101. if (mode > SPI_MODE_3) {
  102. printf("SPI error: unsupported SPI mode %i\n", mode);
  103. return NULL;
  104. }
  105. ds = spi_alloc_slave(struct omap3_spi_slave, bus, cs);
  106. if (!ds) {
  107. printf("SPI error: malloc of SPI structure failed\n");
  108. return NULL;
  109. }
  110. ds->regs = regs;
  111. ds->freq = max_hz;
  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. #ifdef CONFIG_OMAP3_SPI_D0_D1_SWAPPED
  147. /*
  148. * Some boards have D0 wired as MOSI / D1 as MISO instead of
  149. * The normal D0 as MISO / D1 as MOSI.
  150. */
  151. conf &= ~OMAP3_MCSPI_CHCONF_DPE0;
  152. conf |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
  153. #else
  154. conf &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
  155. conf |= OMAP3_MCSPI_CHCONF_DPE0;
  156. #endif
  157. /* wordlength */
  158. conf &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
  159. conf |= (ds->slave.wordlen - 1) << 7;
  160. /* set chipselect polarity; manage with FORCE */
  161. if (!(ds->mode & SPI_CS_HIGH))
  162. conf |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
  163. else
  164. conf &= ~OMAP3_MCSPI_CHCONF_EPOL;
  165. /* set clock divisor */
  166. conf &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
  167. conf |= div << 2;
  168. /* set SPI mode 0..3 */
  169. if (ds->mode & SPI_CPOL)
  170. conf |= OMAP3_MCSPI_CHCONF_POL;
  171. else
  172. conf &= ~OMAP3_MCSPI_CHCONF_POL;
  173. if (ds->mode & SPI_CPHA)
  174. conf |= OMAP3_MCSPI_CHCONF_PHA;
  175. else
  176. conf &= ~OMAP3_MCSPI_CHCONF_PHA;
  177. /* Transmit & receive mode */
  178. conf &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  179. omap3_spi_write_chconf(ds,conf);
  180. return 0;
  181. }
  182. void spi_release_bus(struct spi_slave *slave)
  183. {
  184. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  185. /* Reset the SPI hardware */
  186. spi_reset(ds);
  187. }
  188. int omap3_spi_write(struct spi_slave *slave, unsigned int len, const void *txp,
  189. unsigned long flags)
  190. {
  191. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  192. int i;
  193. int timeout = SPI_WAIT_TIMEOUT;
  194. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  195. /* Enable the channel */
  196. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_EN);
  197. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  198. chconf |= (ds->slave.wordlen - 1) << 7;
  199. chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
  200. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  201. omap3_spi_write_chconf(ds,chconf);
  202. for (i = 0; i < len; i++) {
  203. /* wait till TX register is empty (TXS == 1) */
  204. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  205. OMAP3_MCSPI_CHSTAT_TXS)) {
  206. if (--timeout <= 0) {
  207. printf("SPI TXS timed out, status=0x%08x\n",
  208. readl(&ds->regs->channel[ds->slave.cs].chstat));
  209. return -1;
  210. }
  211. }
  212. /* Write the data */
  213. unsigned int *tx = &ds->regs->channel[ds->slave.cs].tx;
  214. if (ds->slave.wordlen > 16)
  215. writel(((u32 *)txp)[i], tx);
  216. else if (ds->slave.wordlen > 8)
  217. writel(((u16 *)txp)[i], tx);
  218. else
  219. writel(((u8 *)txp)[i], tx);
  220. }
  221. /* wait to finish of transfer */
  222. while ((readl(&ds->regs->channel[ds->slave.cs].chstat) &
  223. (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
  224. (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS));
  225. /* Disable the channel otherwise the next immediate RX will get affected */
  226. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_DIS);
  227. if (flags & SPI_XFER_END) {
  228. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  229. omap3_spi_write_chconf(ds,chconf);
  230. }
  231. return 0;
  232. }
  233. int omap3_spi_read(struct spi_slave *slave, unsigned int len, void *rxp,
  234. unsigned long flags)
  235. {
  236. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  237. int i;
  238. int timeout = SPI_WAIT_TIMEOUT;
  239. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  240. /* Enable the channel */
  241. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_EN);
  242. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  243. chconf |= (ds->slave.wordlen - 1) << 7;
  244. chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
  245. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  246. omap3_spi_write_chconf(ds,chconf);
  247. writel(0, &ds->regs->channel[ds->slave.cs].tx);
  248. for (i = 0; i < len; i++) {
  249. /* Wait till RX register contains data (RXS == 1) */
  250. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  251. OMAP3_MCSPI_CHSTAT_RXS)) {
  252. if (--timeout <= 0) {
  253. printf("SPI RXS timed out, status=0x%08x\n",
  254. readl(&ds->regs->channel[ds->slave.cs].chstat));
  255. return -1;
  256. }
  257. }
  258. /* Disable the channel to prevent furher receiving */
  259. if(i == (len - 1))
  260. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_DIS);
  261. /* Read the data */
  262. unsigned int *rx = &ds->regs->channel[ds->slave.cs].rx;
  263. if (ds->slave.wordlen > 16)
  264. ((u32 *)rxp)[i] = readl(rx);
  265. else if (ds->slave.wordlen > 8)
  266. ((u16 *)rxp)[i] = (u16)readl(rx);
  267. else
  268. ((u8 *)rxp)[i] = (u8)readl(rx);
  269. }
  270. if (flags & SPI_XFER_END) {
  271. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  272. omap3_spi_write_chconf(ds,chconf);
  273. }
  274. return 0;
  275. }
  276. /*McSPI Transmit Receive Mode*/
  277. int omap3_spi_txrx(struct spi_slave *slave, unsigned int len,
  278. const void *txp, void *rxp, unsigned long flags)
  279. {
  280. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  281. int timeout = SPI_WAIT_TIMEOUT;
  282. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  283. int irqstatus = readl(&ds->regs->irqstatus);
  284. int i=0;
  285. /*Enable SPI channel*/
  286. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_EN);
  287. /*set TRANSMIT-RECEIVE Mode*/
  288. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  289. chconf |= (ds->slave.wordlen - 1) << 7;
  290. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  291. omap3_spi_write_chconf(ds,chconf);
  292. /*Shift in and out 1 byte at time*/
  293. for (i=0; i < len; i++){
  294. /* Write: wait for TX empty (TXS == 1)*/
  295. irqstatus |= (1<< (4*(ds->slave.bus)));
  296. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  297. OMAP3_MCSPI_CHSTAT_TXS)) {
  298. if (--timeout <= 0) {
  299. printf("SPI TXS timed out, status=0x%08x\n",
  300. readl(&ds->regs->channel[ds->slave.cs].chstat));
  301. return -1;
  302. }
  303. }
  304. /* Write the data */
  305. unsigned int *tx = &ds->regs->channel[ds->slave.cs].tx;
  306. if (ds->slave.wordlen > 16)
  307. writel(((u32 *)txp)[i], tx);
  308. else if (ds->slave.wordlen > 8)
  309. writel(((u16 *)txp)[i], tx);
  310. else
  311. writel(((u8 *)txp)[i], tx);
  312. /*Read: wait for RX containing data (RXS == 1)*/
  313. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  314. OMAP3_MCSPI_CHSTAT_RXS)) {
  315. if (--timeout <= 0) {
  316. printf("SPI RXS timed out, status=0x%08x\n",
  317. readl(&ds->regs->channel[ds->slave.cs].chstat));
  318. return -1;
  319. }
  320. }
  321. /* Read the data */
  322. unsigned int *rx = &ds->regs->channel[ds->slave.cs].rx;
  323. if (ds->slave.wordlen > 16)
  324. ((u32 *)rxp)[i] = readl(rx);
  325. else if (ds->slave.wordlen > 8)
  326. ((u16 *)rxp)[i] = (u16)readl(rx);
  327. else
  328. ((u8 *)rxp)[i] = (u8)readl(rx);
  329. }
  330. /* Disable the channel */
  331. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_DIS);
  332. /*if transfer must be terminated disable the channel*/
  333. if (flags & SPI_XFER_END) {
  334. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  335. omap3_spi_write_chconf(ds,chconf);
  336. }
  337. return 0;
  338. }
  339. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  340. const void *dout, void *din, unsigned long flags)
  341. {
  342. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  343. unsigned int len;
  344. int ret = -1;
  345. if (ds->slave.wordlen < 4 || ds->slave.wordlen > 32) {
  346. printf("omap3_spi: invalid wordlen %d\n", ds->slave.wordlen);
  347. return -1;
  348. }
  349. if (bitlen % ds->slave.wordlen)
  350. return -1;
  351. len = bitlen / ds->slave.wordlen;
  352. if (bitlen == 0) { /* only change CS */
  353. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  354. if (flags & SPI_XFER_BEGIN) {
  355. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_EN);
  356. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  357. omap3_spi_write_chconf(ds,chconf);
  358. }
  359. if (flags & SPI_XFER_END) {
  360. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  361. omap3_spi_write_chconf(ds,chconf);
  362. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_DIS);
  363. }
  364. ret = 0;
  365. } else {
  366. if (dout != NULL && din != NULL)
  367. ret = omap3_spi_txrx(slave, len, dout, din, flags);
  368. else if (dout != NULL)
  369. ret = omap3_spi_write(slave, len, dout, flags);
  370. else if (din != NULL)
  371. ret = omap3_spi_read(slave, len, din, flags);
  372. }
  373. return ret;
  374. }
  375. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  376. {
  377. return 1;
  378. }
  379. void spi_cs_activate(struct spi_slave *slave)
  380. {
  381. }
  382. void spi_cs_deactivate(struct spi_slave *slave)
  383. {
  384. }