omap3_spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * Copyright (C) 2016 Jagan Teki <jteki@openedev.com>
  3. * Christophe Ricard <christophe.ricard@gmail.com>
  4. *
  5. * Copyright (C) 2010 Dirk Behme <dirk.behme@googlemail.com>
  6. *
  7. * Driver for McSPI controller on OMAP3. Based on davinci_spi.c
  8. * Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
  9. *
  10. * Copyright (C) 2007 Atmel Corporation
  11. *
  12. * Parts taken from linux/drivers/spi/omap2_mcspi.c
  13. * Copyright (C) 2005, 2006 Nokia Corporation
  14. *
  15. * Modified by Ruslan Araslanov <ruslan.araslanov@vitecmm.com>
  16. *
  17. * SPDX-License-Identifier: GPL-2.0+
  18. */
  19. #include <common.h>
  20. #include <dm.h>
  21. #include <spi.h>
  22. #include <malloc.h>
  23. #include <asm/io.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. #if defined(CONFIG_AM33XX) || defined(CONFIG_AM43XX)
  26. #define OMAP3_MCSPI1_BASE 0x48030100
  27. #define OMAP3_MCSPI2_BASE 0x481A0100
  28. #else
  29. #define OMAP3_MCSPI1_BASE 0x48098000
  30. #define OMAP3_MCSPI2_BASE 0x4809A000
  31. #define OMAP3_MCSPI3_BASE 0x480B8000
  32. #define OMAP3_MCSPI4_BASE 0x480BA000
  33. #endif
  34. /* per-register bitmasks */
  35. #define OMAP3_MCSPI_SYSCONFIG_SMARTIDLE (2 << 3)
  36. #define OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
  37. #define OMAP3_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
  38. #define OMAP3_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
  39. #define OMAP3_MCSPI_SYSSTATUS_RESETDONE BIT(0)
  40. #define OMAP3_MCSPI_MODULCTRL_SINGLE BIT(0)
  41. #define OMAP3_MCSPI_MODULCTRL_MS BIT(2)
  42. #define OMAP3_MCSPI_MODULCTRL_STEST BIT(3)
  43. #define OMAP3_MCSPI_CHCONF_PHA BIT(0)
  44. #define OMAP3_MCSPI_CHCONF_POL BIT(1)
  45. #define OMAP3_MCSPI_CHCONF_CLKD_MASK GENMASK(5, 2)
  46. #define OMAP3_MCSPI_CHCONF_EPOL BIT(6)
  47. #define OMAP3_MCSPI_CHCONF_WL_MASK GENMASK(11, 7)
  48. #define OMAP3_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
  49. #define OMAP3_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
  50. #define OMAP3_MCSPI_CHCONF_TRM_MASK GENMASK(13, 12)
  51. #define OMAP3_MCSPI_CHCONF_DMAW BIT(14)
  52. #define OMAP3_MCSPI_CHCONF_DMAR BIT(15)
  53. #define OMAP3_MCSPI_CHCONF_DPE0 BIT(16)
  54. #define OMAP3_MCSPI_CHCONF_DPE1 BIT(17)
  55. #define OMAP3_MCSPI_CHCONF_IS BIT(18)
  56. #define OMAP3_MCSPI_CHCONF_TURBO BIT(19)
  57. #define OMAP3_MCSPI_CHCONF_FORCE BIT(20)
  58. #define OMAP3_MCSPI_CHSTAT_RXS BIT(0)
  59. #define OMAP3_MCSPI_CHSTAT_TXS BIT(1)
  60. #define OMAP3_MCSPI_CHSTAT_EOT BIT(2)
  61. #define OMAP3_MCSPI_CHCTRL_EN BIT(0)
  62. #define OMAP3_MCSPI_CHCTRL_DIS (0 << 0)
  63. #define OMAP3_MCSPI_WAKEUPENABLE_WKEN BIT(0)
  64. #define MCSPI_PINDIR_D0_IN_D1_OUT 0
  65. #define MCSPI_PINDIR_D0_OUT_D1_IN 1
  66. #define OMAP3_MCSPI_MAX_FREQ 48000000
  67. #define SPI_WAIT_TIMEOUT 10
  68. /* OMAP3 McSPI registers */
  69. struct mcspi_channel {
  70. unsigned int chconf; /* 0x2C, 0x40, 0x54, 0x68 */
  71. unsigned int chstat; /* 0x30, 0x44, 0x58, 0x6C */
  72. unsigned int chctrl; /* 0x34, 0x48, 0x5C, 0x70 */
  73. unsigned int tx; /* 0x38, 0x4C, 0x60, 0x74 */
  74. unsigned int rx; /* 0x3C, 0x50, 0x64, 0x78 */
  75. };
  76. struct mcspi {
  77. unsigned char res1[0x10];
  78. unsigned int sysconfig; /* 0x10 */
  79. unsigned int sysstatus; /* 0x14 */
  80. unsigned int irqstatus; /* 0x18 */
  81. unsigned int irqenable; /* 0x1C */
  82. unsigned int wakeupenable; /* 0x20 */
  83. unsigned int syst; /* 0x24 */
  84. unsigned int modulctrl; /* 0x28 */
  85. struct mcspi_channel channel[4];
  86. /* channel0: 0x2C - 0x3C, bus 0 & 1 & 2 & 3 */
  87. /* channel1: 0x40 - 0x50, bus 0 & 1 */
  88. /* channel2: 0x54 - 0x64, bus 0 & 1 */
  89. /* channel3: 0x68 - 0x78, bus 0 */
  90. };
  91. struct omap3_spi_priv {
  92. #ifndef CONFIG_DM_SPI
  93. struct spi_slave slave;
  94. #endif
  95. struct mcspi *regs;
  96. unsigned int cs;
  97. unsigned int freq;
  98. unsigned int mode;
  99. unsigned int wordlen;
  100. unsigned int pin_dir:1;
  101. };
  102. static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
  103. {
  104. writel(val, &priv->regs->channel[priv->cs].chconf);
  105. /* Flash post writes to make immediate effect */
  106. readl(&priv->regs->channel[priv->cs].chconf);
  107. }
  108. static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
  109. {
  110. writel(enable, &priv->regs->channel[priv->cs].chctrl);
  111. /* Flash post writes to make immediate effect */
  112. readl(&priv->regs->channel[priv->cs].chctrl);
  113. }
  114. static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
  115. const void *txp, unsigned long flags)
  116. {
  117. ulong start;
  118. int i, chconf;
  119. chconf = readl(&priv->regs->channel[priv->cs].chconf);
  120. /* Enable the channel */
  121. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
  122. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  123. chconf |= (priv->wordlen - 1) << 7;
  124. chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
  125. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  126. omap3_spi_write_chconf(priv, chconf);
  127. for (i = 0; i < len; i++) {
  128. /* wait till TX register is empty (TXS == 1) */
  129. start = get_timer(0);
  130. while (!(readl(&priv->regs->channel[priv->cs].chstat) &
  131. OMAP3_MCSPI_CHSTAT_TXS)) {
  132. if (get_timer(start) > SPI_WAIT_TIMEOUT) {
  133. printf("SPI TXS timed out, status=0x%08x\n",
  134. readl(&priv->regs->channel[priv->cs].chstat));
  135. return -1;
  136. }
  137. }
  138. /* Write the data */
  139. unsigned int *tx = &priv->regs->channel[priv->cs].tx;
  140. if (priv->wordlen > 16)
  141. writel(((u32 *)txp)[i], tx);
  142. else if (priv->wordlen > 8)
  143. writel(((u16 *)txp)[i], tx);
  144. else
  145. writel(((u8 *)txp)[i], tx);
  146. }
  147. /* wait to finish of transfer */
  148. while ((readl(&priv->regs->channel[priv->cs].chstat) &
  149. (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
  150. (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
  151. ;
  152. /* Disable the channel otherwise the next immediate RX will get affected */
  153. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
  154. if (flags & SPI_XFER_END) {
  155. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  156. omap3_spi_write_chconf(priv, chconf);
  157. }
  158. return 0;
  159. }
  160. static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
  161. void *rxp, unsigned long flags)
  162. {
  163. int i, chconf;
  164. ulong start;
  165. chconf = readl(&priv->regs->channel[priv->cs].chconf);
  166. /* Enable the channel */
  167. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
  168. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  169. chconf |= (priv->wordlen - 1) << 7;
  170. chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
  171. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  172. omap3_spi_write_chconf(priv, chconf);
  173. writel(0, &priv->regs->channel[priv->cs].tx);
  174. for (i = 0; i < len; i++) {
  175. start = get_timer(0);
  176. /* Wait till RX register contains data (RXS == 1) */
  177. while (!(readl(&priv->regs->channel[priv->cs].chstat) &
  178. OMAP3_MCSPI_CHSTAT_RXS)) {
  179. if (get_timer(start) > SPI_WAIT_TIMEOUT) {
  180. printf("SPI RXS timed out, status=0x%08x\n",
  181. readl(&priv->regs->channel[priv->cs].chstat));
  182. return -1;
  183. }
  184. }
  185. /* Disable the channel to prevent furher receiving */
  186. if (i == (len - 1))
  187. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
  188. /* Read the data */
  189. unsigned int *rx = &priv->regs->channel[priv->cs].rx;
  190. if (priv->wordlen > 16)
  191. ((u32 *)rxp)[i] = readl(rx);
  192. else if (priv->wordlen > 8)
  193. ((u16 *)rxp)[i] = (u16)readl(rx);
  194. else
  195. ((u8 *)rxp)[i] = (u8)readl(rx);
  196. }
  197. if (flags & SPI_XFER_END) {
  198. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  199. omap3_spi_write_chconf(priv, chconf);
  200. }
  201. return 0;
  202. }
  203. /*McSPI Transmit Receive Mode*/
  204. static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
  205. const void *txp, void *rxp, unsigned long flags)
  206. {
  207. ulong start;
  208. int chconf, i = 0;
  209. chconf = readl(&priv->regs->channel[priv->cs].chconf);
  210. /*Enable SPI channel*/
  211. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
  212. /*set TRANSMIT-RECEIVE Mode*/
  213. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  214. chconf |= (priv->wordlen - 1) << 7;
  215. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  216. omap3_spi_write_chconf(priv, chconf);
  217. /*Shift in and out 1 byte at time*/
  218. for (i=0; i < len; i++){
  219. /* Write: wait for TX empty (TXS == 1)*/
  220. start = get_timer(0);
  221. while (!(readl(&priv->regs->channel[priv->cs].chstat) &
  222. OMAP3_MCSPI_CHSTAT_TXS)) {
  223. if (get_timer(start) > SPI_WAIT_TIMEOUT) {
  224. printf("SPI TXS timed out, status=0x%08x\n",
  225. readl(&priv->regs->channel[priv->cs].chstat));
  226. return -1;
  227. }
  228. }
  229. /* Write the data */
  230. unsigned int *tx = &priv->regs->channel[priv->cs].tx;
  231. if (priv->wordlen > 16)
  232. writel(((u32 *)txp)[i], tx);
  233. else if (priv->wordlen > 8)
  234. writel(((u16 *)txp)[i], tx);
  235. else
  236. writel(((u8 *)txp)[i], tx);
  237. /*Read: wait for RX containing data (RXS == 1)*/
  238. start = get_timer(0);
  239. while (!(readl(&priv->regs->channel[priv->cs].chstat) &
  240. OMAP3_MCSPI_CHSTAT_RXS)) {
  241. if (get_timer(start) > SPI_WAIT_TIMEOUT) {
  242. printf("SPI RXS timed out, status=0x%08x\n",
  243. readl(&priv->regs->channel[priv->cs].chstat));
  244. return -1;
  245. }
  246. }
  247. /* Read the data */
  248. unsigned int *rx = &priv->regs->channel[priv->cs].rx;
  249. if (priv->wordlen > 16)
  250. ((u32 *)rxp)[i] = readl(rx);
  251. else if (priv->wordlen > 8)
  252. ((u16 *)rxp)[i] = (u16)readl(rx);
  253. else
  254. ((u8 *)rxp)[i] = (u8)readl(rx);
  255. }
  256. /* Disable the channel */
  257. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
  258. /*if transfer must be terminated disable the channel*/
  259. if (flags & SPI_XFER_END) {
  260. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  261. omap3_spi_write_chconf(priv, chconf);
  262. }
  263. return 0;
  264. }
  265. static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
  266. const void *dout, void *din, unsigned long flags)
  267. {
  268. unsigned int len;
  269. int ret = -1;
  270. if (priv->wordlen < 4 || priv->wordlen > 32) {
  271. printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
  272. return -1;
  273. }
  274. if (bitlen % priv->wordlen)
  275. return -1;
  276. len = bitlen / priv->wordlen;
  277. if (bitlen == 0) { /* only change CS */
  278. int chconf = readl(&priv->regs->channel[priv->cs].chconf);
  279. if (flags & SPI_XFER_BEGIN) {
  280. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
  281. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  282. omap3_spi_write_chconf(priv, chconf);
  283. }
  284. if (flags & SPI_XFER_END) {
  285. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  286. omap3_spi_write_chconf(priv, chconf);
  287. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
  288. }
  289. ret = 0;
  290. } else {
  291. if (dout != NULL && din != NULL)
  292. ret = omap3_spi_txrx(priv, len, dout, din, flags);
  293. else if (dout != NULL)
  294. ret = omap3_spi_write(priv, len, dout, flags);
  295. else if (din != NULL)
  296. ret = omap3_spi_read(priv, len, din, flags);
  297. }
  298. return ret;
  299. }
  300. static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
  301. {
  302. uint32_t confr, div = 0;
  303. confr = readl(&priv->regs->channel[priv->cs].chconf);
  304. /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
  305. if (priv->freq) {
  306. while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
  307. > priv->freq)
  308. div++;
  309. } else {
  310. div = 0xC;
  311. }
  312. /* set clock divisor */
  313. confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
  314. confr |= div << 2;
  315. omap3_spi_write_chconf(priv, confr);
  316. }
  317. static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
  318. {
  319. uint32_t confr;
  320. confr = readl(&priv->regs->channel[priv->cs].chconf);
  321. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  322. * REVISIT: this controller could support SPI_3WIRE mode.
  323. */
  324. if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  325. confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
  326. confr |= OMAP3_MCSPI_CHCONF_DPE0;
  327. } else {
  328. confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
  329. confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
  330. }
  331. /* set SPI mode 0..3 */
  332. confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
  333. if (priv->mode & SPI_CPHA)
  334. confr |= OMAP3_MCSPI_CHCONF_PHA;
  335. if (priv->mode & SPI_CPOL)
  336. confr |= OMAP3_MCSPI_CHCONF_POL;
  337. /* set chipselect polarity; manage with FORCE */
  338. if (!(priv->mode & SPI_CS_HIGH))
  339. confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
  340. else
  341. confr &= ~OMAP3_MCSPI_CHCONF_EPOL;
  342. /* Transmit & receive mode */
  343. confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  344. omap3_spi_write_chconf(priv, confr);
  345. }
  346. static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
  347. {
  348. unsigned int confr;
  349. /* McSPI individual channel configuration */
  350. confr = readl(&priv->regs->channel[priv->wordlen].chconf);
  351. /* wordlength */
  352. confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
  353. confr |= (priv->wordlen - 1) << 7;
  354. omap3_spi_write_chconf(priv, confr);
  355. }
  356. static void spi_reset(struct mcspi *regs)
  357. {
  358. unsigned int tmp;
  359. writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &regs->sysconfig);
  360. do {
  361. tmp = readl(&regs->sysstatus);
  362. } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
  363. writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
  364. OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
  365. OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, &regs->sysconfig);
  366. writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &regs->wakeupenable);
  367. }
  368. static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
  369. {
  370. unsigned int conf;
  371. spi_reset(priv->regs);
  372. /*
  373. * setup when switching from (reset default) slave mode
  374. * to single-channel master mode
  375. */
  376. conf = readl(&priv->regs->modulctrl);
  377. conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
  378. conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
  379. writel(conf, &priv->regs->modulctrl);
  380. _omap3_spi_set_mode(priv);
  381. _omap3_spi_set_speed(priv);
  382. }
  383. #ifndef CONFIG_DM_SPI
  384. static inline struct omap3_spi_priv *to_omap3_spi(struct spi_slave *slave)
  385. {
  386. return container_of(slave, struct omap3_spi_priv, slave);
  387. }
  388. void spi_init(void)
  389. {
  390. /* do nothing */
  391. }
  392. void spi_free_slave(struct spi_slave *slave)
  393. {
  394. struct omap3_spi_priv *priv = to_omap3_spi(slave);
  395. free(priv);
  396. }
  397. int spi_claim_bus(struct spi_slave *slave)
  398. {
  399. struct omap3_spi_priv *priv = to_omap3_spi(slave);
  400. _omap3_spi_claim_bus(priv);
  401. _omap3_spi_set_wordlen(priv);
  402. _omap3_spi_set_mode(priv);
  403. _omap3_spi_set_speed(priv);
  404. return 0;
  405. }
  406. void spi_release_bus(struct spi_slave *slave)
  407. {
  408. struct omap3_spi_priv *priv = to_omap3_spi(slave);
  409. /* Reset the SPI hardware */
  410. spi_reset(priv->regs);
  411. }
  412. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  413. unsigned int max_hz, unsigned int mode)
  414. {
  415. struct omap3_spi_priv *priv;
  416. struct mcspi *regs;
  417. /*
  418. * OMAP3 McSPI (MultiChannel SPI) has 4 busses (modules)
  419. * with different number of chip selects (CS, channels):
  420. * McSPI1 has 4 CS (bus 0, cs 0 - 3)
  421. * McSPI2 has 2 CS (bus 1, cs 0 - 1)
  422. * McSPI3 has 2 CS (bus 2, cs 0 - 1)
  423. * McSPI4 has 1 CS (bus 3, cs 0)
  424. */
  425. switch (bus) {
  426. case 0:
  427. regs = (struct mcspi *)OMAP3_MCSPI1_BASE;
  428. break;
  429. #ifdef OMAP3_MCSPI2_BASE
  430. case 1:
  431. regs = (struct mcspi *)OMAP3_MCSPI2_BASE;
  432. break;
  433. #endif
  434. #ifdef OMAP3_MCSPI3_BASE
  435. case 2:
  436. regs = (struct mcspi *)OMAP3_MCSPI3_BASE;
  437. break;
  438. #endif
  439. #ifdef OMAP3_MCSPI4_BASE
  440. case 3:
  441. regs = (struct mcspi *)OMAP3_MCSPI4_BASE;
  442. break;
  443. #endif
  444. default:
  445. printf("SPI error: unsupported bus %i. Supported busses 0 - 3\n", bus);
  446. return NULL;
  447. }
  448. if (((bus == 0) && (cs > 3)) ||
  449. ((bus == 1) && (cs > 1)) ||
  450. ((bus == 2) && (cs > 1)) ||
  451. ((bus == 3) && (cs > 0))) {
  452. printf("SPI error: unsupported chip select %i on bus %i\n", cs, bus);
  453. return NULL;
  454. }
  455. if (max_hz > OMAP3_MCSPI_MAX_FREQ) {
  456. printf("SPI error: unsupported frequency %i Hz. Max frequency is 48 Mhz\n", max_hz);
  457. return NULL;
  458. }
  459. if (mode > SPI_MODE_3) {
  460. printf("SPI error: unsupported SPI mode %i\n", mode);
  461. return NULL;
  462. }
  463. priv = spi_alloc_slave(struct omap3_spi_priv, bus, cs);
  464. if (!priv) {
  465. printf("SPI error: malloc of SPI structure failed\n");
  466. return NULL;
  467. }
  468. priv->regs = regs;
  469. priv->cs = cs;
  470. priv->freq = max_hz;
  471. priv->mode = mode;
  472. priv->wordlen = priv->slave.wordlen;
  473. #ifdef CONFIG_OMAP3_SPI_D0_D1_SWAPPED
  474. priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  475. #endif
  476. return &priv->slave;
  477. }
  478. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  479. const void *dout, void *din, unsigned long flags)
  480. {
  481. struct omap3_spi_priv *priv = to_omap3_spi(slave);
  482. return _spi_xfer(priv, bitlen, dout, din, flags);
  483. }
  484. #else
  485. static int omap3_spi_claim_bus(struct udevice *dev)
  486. {
  487. struct udevice *bus = dev->parent;
  488. struct omap3_spi_priv *priv = dev_get_priv(bus);
  489. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  490. priv->cs = slave_plat->cs;
  491. priv->mode = slave_plat->mode;
  492. priv->freq = slave_plat->max_hz;
  493. _omap3_spi_claim_bus(priv);
  494. return 0;
  495. }
  496. static int omap3_spi_release_bus(struct udevice *dev)
  497. {
  498. struct udevice *bus = dev->parent;
  499. struct omap3_spi_priv *priv = dev_get_priv(bus);
  500. /* Reset the SPI hardware */
  501. spi_reset(priv->regs);
  502. return 0;
  503. }
  504. static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
  505. {
  506. struct udevice *bus = dev->parent;
  507. struct omap3_spi_priv *priv = dev_get_priv(bus);
  508. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  509. priv->cs = slave_plat->cs;
  510. priv->wordlen = wordlen;
  511. _omap3_spi_set_wordlen(priv);
  512. return 0;
  513. }
  514. static int omap3_spi_probe(struct udevice *dev)
  515. {
  516. struct omap3_spi_priv *priv = dev_get_priv(dev);
  517. const void *blob = gd->fdt_blob;
  518. int node = dev->of_offset;
  519. priv->regs = (struct mcspi *)dev_get_addr(dev);
  520. priv->pin_dir = fdtdec_get_uint(blob, node, "ti,pindir-d0-out-d1-in",
  521. MCSPI_PINDIR_D0_IN_D1_OUT);
  522. priv->wordlen = SPI_DEFAULT_WORDLEN;
  523. return 0;
  524. }
  525. static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
  526. const void *dout, void *din, unsigned long flags)
  527. {
  528. struct udevice *bus = dev->parent;
  529. struct omap3_spi_priv *priv = dev_get_priv(bus);
  530. return _spi_xfer(priv, bitlen, dout, din, flags);
  531. }
  532. static int omap3_spi_set_speed(struct udevice *bus, unsigned int speed)
  533. {
  534. return 0;
  535. }
  536. static int omap3_spi_set_mode(struct udevice *bus, uint mode)
  537. {
  538. return 0;
  539. }
  540. static const struct dm_spi_ops omap3_spi_ops = {
  541. .claim_bus = omap3_spi_claim_bus,
  542. .release_bus = omap3_spi_release_bus,
  543. .set_wordlen = omap3_spi_set_wordlen,
  544. .xfer = omap3_spi_xfer,
  545. .set_speed = omap3_spi_set_speed,
  546. .set_mode = omap3_spi_set_mode,
  547. /*
  548. * cs_info is not needed, since we require all chip selects to be
  549. * in the device tree explicitly
  550. */
  551. };
  552. static const struct udevice_id omap3_spi_ids[] = {
  553. { .compatible = "ti,omap2-mcspi" },
  554. { .compatible = "ti,omap4-mcspi" },
  555. { }
  556. };
  557. U_BOOT_DRIVER(omap3_spi) = {
  558. .name = "omap3_spi",
  559. .id = UCLASS_SPI,
  560. .of_match = omap3_spi_ids,
  561. .probe = omap3_spi_probe,
  562. .ops = &omap3_spi_ops,
  563. .priv_auto_alloc_size = sizeof(struct omap3_spi_priv),
  564. .probe = omap3_spi_probe,
  565. };
  566. #endif