omap3_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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));
  224. /* Disable the channel otherwise the next immediate RX will get affected */
  225. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_DIS);
  226. if (flags & SPI_XFER_END) {
  227. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  228. omap3_spi_write_chconf(ds,chconf);
  229. }
  230. return 0;
  231. }
  232. int omap3_spi_read(struct spi_slave *slave, unsigned int len, void *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. /* Enable the channel */
  240. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_EN);
  241. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  242. chconf |= (ds->slave.wordlen - 1) << 7;
  243. chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
  244. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  245. omap3_spi_write_chconf(ds,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. /* Disable the channel to prevent furher receiving */
  258. if(i == (len - 1))
  259. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_DIS);
  260. /* Read the data */
  261. unsigned int *rx = &ds->regs->channel[ds->slave.cs].rx;
  262. if (ds->slave.wordlen > 16)
  263. ((u32 *)rxp)[i] = readl(rx);
  264. else if (ds->slave.wordlen > 8)
  265. ((u16 *)rxp)[i] = (u16)readl(rx);
  266. else
  267. ((u8 *)rxp)[i] = (u8)readl(rx);
  268. }
  269. if (flags & SPI_XFER_END) {
  270. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  271. omap3_spi_write_chconf(ds,chconf);
  272. }
  273. return 0;
  274. }
  275. /*McSPI Transmit Receive Mode*/
  276. int omap3_spi_txrx(struct spi_slave *slave, unsigned int len,
  277. const void *txp, void *rxp, unsigned long flags)
  278. {
  279. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  280. int timeout = SPI_WAIT_TIMEOUT;
  281. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  282. int irqstatus = readl(&ds->regs->irqstatus);
  283. int i=0;
  284. /*Enable SPI channel*/
  285. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_EN);
  286. /*set TRANSMIT-RECEIVE Mode*/
  287. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  288. chconf |= (ds->slave.wordlen - 1) << 7;
  289. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  290. omap3_spi_write_chconf(ds,chconf);
  291. /*Shift in and out 1 byte at time*/
  292. for (i=0; i < len; i++){
  293. /* Write: wait for TX empty (TXS == 1)*/
  294. irqstatus |= (1<< (4*(ds->slave.bus)));
  295. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  296. OMAP3_MCSPI_CHSTAT_TXS)) {
  297. if (--timeout <= 0) {
  298. printf("SPI TXS timed out, status=0x%08x\n",
  299. readl(&ds->regs->channel[ds->slave.cs].chstat));
  300. return -1;
  301. }
  302. }
  303. /* Write the data */
  304. unsigned int *tx = &ds->regs->channel[ds->slave.cs].tx;
  305. if (ds->slave.wordlen > 16)
  306. writel(((u32 *)txp)[i], tx);
  307. else if (ds->slave.wordlen > 8)
  308. writel(((u16 *)txp)[i], tx);
  309. else
  310. writel(((u8 *)txp)[i], tx);
  311. /*Read: wait for RX containing data (RXS == 1)*/
  312. while (!(readl(&ds->regs->channel[ds->slave.cs].chstat) &
  313. OMAP3_MCSPI_CHSTAT_RXS)) {
  314. if (--timeout <= 0) {
  315. printf("SPI RXS timed out, status=0x%08x\n",
  316. readl(&ds->regs->channel[ds->slave.cs].chstat));
  317. return -1;
  318. }
  319. }
  320. /* Read the data */
  321. unsigned int *rx = &ds->regs->channel[ds->slave.cs].rx;
  322. if (ds->slave.wordlen > 16)
  323. ((u32 *)rxp)[i] = readl(rx);
  324. else if (ds->slave.wordlen > 8)
  325. ((u16 *)rxp)[i] = (u16)readl(rx);
  326. else
  327. ((u8 *)rxp)[i] = (u8)readl(rx);
  328. }
  329. /* Disable the channel */
  330. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_DIS);
  331. /*if transfer must be terminated disable the channel*/
  332. if (flags & SPI_XFER_END) {
  333. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  334. omap3_spi_write_chconf(ds,chconf);
  335. }
  336. return 0;
  337. }
  338. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  339. const void *dout, void *din, unsigned long flags)
  340. {
  341. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  342. unsigned int len;
  343. int ret = -1;
  344. if (ds->slave.wordlen < 4 || ds->slave.wordlen > 32) {
  345. printf("omap3_spi: invalid wordlen %d\n", ds->slave.wordlen);
  346. return -1;
  347. }
  348. if (bitlen % ds->slave.wordlen)
  349. return -1;
  350. len = bitlen / ds->slave.wordlen;
  351. if (bitlen == 0) { /* only change CS */
  352. int chconf = readl(&ds->regs->channel[ds->slave.cs].chconf);
  353. if (flags & SPI_XFER_BEGIN) {
  354. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_EN);
  355. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  356. omap3_spi_write_chconf(ds,chconf);
  357. }
  358. if (flags & SPI_XFER_END) {
  359. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  360. omap3_spi_write_chconf(ds,chconf);
  361. omap3_spi_set_enable(ds,OMAP3_MCSPI_CHCTRL_DIS);
  362. }
  363. ret = 0;
  364. } else {
  365. if (dout != NULL && din != NULL)
  366. ret = omap3_spi_txrx(slave, len, dout, din, flags);
  367. else if (dout != NULL)
  368. ret = omap3_spi_write(slave, len, dout, flags);
  369. else if (din != NULL)
  370. ret = omap3_spi_read(slave, len, din, flags);
  371. }
  372. return ret;
  373. }
  374. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  375. {
  376. return 1;
  377. }
  378. void spi_cs_activate(struct spi_slave *slave)
  379. {
  380. }
  381. void spi_cs_deactivate(struct spi_slave *slave)
  382. {
  383. }