omap3_spi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. struct mcspi *regs;
  93. unsigned int cs;
  94. unsigned int freq;
  95. unsigned int mode;
  96. unsigned int wordlen;
  97. unsigned int pin_dir:1;
  98. };
  99. static void omap3_spi_write_chconf(struct omap3_spi_priv *priv, int val)
  100. {
  101. writel(val, &priv->regs->channel[priv->cs].chconf);
  102. /* Flash post writes to make immediate effect */
  103. readl(&priv->regs->channel[priv->cs].chconf);
  104. }
  105. static void omap3_spi_set_enable(struct omap3_spi_priv *priv, int enable)
  106. {
  107. writel(enable, &priv->regs->channel[priv->cs].chctrl);
  108. /* Flash post writes to make immediate effect */
  109. readl(&priv->regs->channel[priv->cs].chctrl);
  110. }
  111. static int omap3_spi_write(struct omap3_spi_priv *priv, unsigned int len,
  112. const void *txp, unsigned long flags)
  113. {
  114. ulong start;
  115. int i, chconf;
  116. chconf = readl(&priv->regs->channel[priv->cs].chconf);
  117. /* Enable the channel */
  118. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
  119. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  120. chconf |= (priv->wordlen - 1) << 7;
  121. chconf |= OMAP3_MCSPI_CHCONF_TRM_TX_ONLY;
  122. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  123. omap3_spi_write_chconf(priv, chconf);
  124. for (i = 0; i < len; i++) {
  125. /* wait till TX register is empty (TXS == 1) */
  126. start = get_timer(0);
  127. while (!(readl(&priv->regs->channel[priv->cs].chstat) &
  128. OMAP3_MCSPI_CHSTAT_TXS)) {
  129. if (get_timer(start) > SPI_WAIT_TIMEOUT) {
  130. printf("SPI TXS timed out, status=0x%08x\n",
  131. readl(&priv->regs->channel[priv->cs].chstat));
  132. return -1;
  133. }
  134. }
  135. /* Write the data */
  136. unsigned int *tx = &priv->regs->channel[priv->cs].tx;
  137. if (priv->wordlen > 16)
  138. writel(((u32 *)txp)[i], tx);
  139. else if (priv->wordlen > 8)
  140. writel(((u16 *)txp)[i], tx);
  141. else
  142. writel(((u8 *)txp)[i], tx);
  143. }
  144. /* wait to finish of transfer */
  145. while ((readl(&priv->regs->channel[priv->cs].chstat) &
  146. (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS)) !=
  147. (OMAP3_MCSPI_CHSTAT_EOT | OMAP3_MCSPI_CHSTAT_TXS))
  148. ;
  149. /* Disable the channel otherwise the next immediate RX will get affected */
  150. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
  151. if (flags & SPI_XFER_END) {
  152. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  153. omap3_spi_write_chconf(priv, chconf);
  154. }
  155. return 0;
  156. }
  157. static int omap3_spi_read(struct omap3_spi_priv *priv, unsigned int len,
  158. void *rxp, unsigned long flags)
  159. {
  160. int i, chconf;
  161. ulong start;
  162. chconf = readl(&priv->regs->channel[priv->cs].chconf);
  163. /* Enable the channel */
  164. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
  165. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  166. chconf |= (priv->wordlen - 1) << 7;
  167. chconf |= OMAP3_MCSPI_CHCONF_TRM_RX_ONLY;
  168. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  169. omap3_spi_write_chconf(priv, chconf);
  170. writel(0, &priv->regs->channel[priv->cs].tx);
  171. for (i = 0; i < len; i++) {
  172. start = get_timer(0);
  173. /* Wait till RX register contains data (RXS == 1) */
  174. while (!(readl(&priv->regs->channel[priv->cs].chstat) &
  175. OMAP3_MCSPI_CHSTAT_RXS)) {
  176. if (get_timer(start) > SPI_WAIT_TIMEOUT) {
  177. printf("SPI RXS timed out, status=0x%08x\n",
  178. readl(&priv->regs->channel[priv->cs].chstat));
  179. return -1;
  180. }
  181. }
  182. /* Disable the channel to prevent furher receiving */
  183. if (i == (len - 1))
  184. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
  185. /* Read the data */
  186. unsigned int *rx = &priv->regs->channel[priv->cs].rx;
  187. if (priv->wordlen > 16)
  188. ((u32 *)rxp)[i] = readl(rx);
  189. else if (priv->wordlen > 8)
  190. ((u16 *)rxp)[i] = (u16)readl(rx);
  191. else
  192. ((u8 *)rxp)[i] = (u8)readl(rx);
  193. }
  194. if (flags & SPI_XFER_END) {
  195. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  196. omap3_spi_write_chconf(priv, chconf);
  197. }
  198. return 0;
  199. }
  200. /*McSPI Transmit Receive Mode*/
  201. static int omap3_spi_txrx(struct omap3_spi_priv *priv, unsigned int len,
  202. const void *txp, void *rxp, unsigned long flags)
  203. {
  204. ulong start;
  205. int chconf, i = 0;
  206. chconf = readl(&priv->regs->channel[priv->cs].chconf);
  207. /*Enable SPI channel*/
  208. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
  209. /*set TRANSMIT-RECEIVE Mode*/
  210. chconf &= ~(OMAP3_MCSPI_CHCONF_TRM_MASK | OMAP3_MCSPI_CHCONF_WL_MASK);
  211. chconf |= (priv->wordlen - 1) << 7;
  212. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  213. omap3_spi_write_chconf(priv, chconf);
  214. /*Shift in and out 1 byte at time*/
  215. for (i=0; i < len; i++){
  216. /* Write: wait for TX empty (TXS == 1)*/
  217. start = get_timer(0);
  218. while (!(readl(&priv->regs->channel[priv->cs].chstat) &
  219. OMAP3_MCSPI_CHSTAT_TXS)) {
  220. if (get_timer(start) > SPI_WAIT_TIMEOUT) {
  221. printf("SPI TXS timed out, status=0x%08x\n",
  222. readl(&priv->regs->channel[priv->cs].chstat));
  223. return -1;
  224. }
  225. }
  226. /* Write the data */
  227. unsigned int *tx = &priv->regs->channel[priv->cs].tx;
  228. if (priv->wordlen > 16)
  229. writel(((u32 *)txp)[i], tx);
  230. else if (priv->wordlen > 8)
  231. writel(((u16 *)txp)[i], tx);
  232. else
  233. writel(((u8 *)txp)[i], tx);
  234. /*Read: wait for RX containing data (RXS == 1)*/
  235. start = get_timer(0);
  236. while (!(readl(&priv->regs->channel[priv->cs].chstat) &
  237. OMAP3_MCSPI_CHSTAT_RXS)) {
  238. if (get_timer(start) > SPI_WAIT_TIMEOUT) {
  239. printf("SPI RXS timed out, status=0x%08x\n",
  240. readl(&priv->regs->channel[priv->cs].chstat));
  241. return -1;
  242. }
  243. }
  244. /* Read the data */
  245. unsigned int *rx = &priv->regs->channel[priv->cs].rx;
  246. if (priv->wordlen > 16)
  247. ((u32 *)rxp)[i] = readl(rx);
  248. else if (priv->wordlen > 8)
  249. ((u16 *)rxp)[i] = (u16)readl(rx);
  250. else
  251. ((u8 *)rxp)[i] = (u8)readl(rx);
  252. }
  253. /* Disable the channel */
  254. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
  255. /*if transfer must be terminated disable the channel*/
  256. if (flags & SPI_XFER_END) {
  257. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  258. omap3_spi_write_chconf(priv, chconf);
  259. }
  260. return 0;
  261. }
  262. static int _spi_xfer(struct omap3_spi_priv *priv, unsigned int bitlen,
  263. const void *dout, void *din, unsigned long flags)
  264. {
  265. unsigned int len;
  266. int ret = -1;
  267. if (priv->wordlen < 4 || priv->wordlen > 32) {
  268. printf("omap3_spi: invalid wordlen %d\n", priv->wordlen);
  269. return -1;
  270. }
  271. if (bitlen % priv->wordlen)
  272. return -1;
  273. len = bitlen / priv->wordlen;
  274. if (bitlen == 0) { /* only change CS */
  275. int chconf = readl(&priv->regs->channel[priv->cs].chconf);
  276. if (flags & SPI_XFER_BEGIN) {
  277. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_EN);
  278. chconf |= OMAP3_MCSPI_CHCONF_FORCE;
  279. omap3_spi_write_chconf(priv, chconf);
  280. }
  281. if (flags & SPI_XFER_END) {
  282. chconf &= ~OMAP3_MCSPI_CHCONF_FORCE;
  283. omap3_spi_write_chconf(priv, chconf);
  284. omap3_spi_set_enable(priv, OMAP3_MCSPI_CHCTRL_DIS);
  285. }
  286. ret = 0;
  287. } else {
  288. if (dout != NULL && din != NULL)
  289. ret = omap3_spi_txrx(priv, len, dout, din, flags);
  290. else if (dout != NULL)
  291. ret = omap3_spi_write(priv, len, dout, flags);
  292. else if (din != NULL)
  293. ret = omap3_spi_read(priv, len, din, flags);
  294. }
  295. return ret;
  296. }
  297. static void _omap3_spi_set_speed(struct omap3_spi_priv *priv)
  298. {
  299. uint32_t confr, div = 0;
  300. confr = readl(&priv->regs->channel[priv->cs].chconf);
  301. /* Calculate clock divisor. Valid range: 0x0 - 0xC ( /1 - /4096 ) */
  302. if (priv->freq) {
  303. while (div <= 0xC && (OMAP3_MCSPI_MAX_FREQ / (1 << div))
  304. > priv->freq)
  305. div++;
  306. } else {
  307. div = 0xC;
  308. }
  309. /* set clock divisor */
  310. confr &= ~OMAP3_MCSPI_CHCONF_CLKD_MASK;
  311. confr |= div << 2;
  312. omap3_spi_write_chconf(priv, confr);
  313. }
  314. static void _omap3_spi_set_mode(struct omap3_spi_priv *priv)
  315. {
  316. uint32_t confr;
  317. confr = readl(&priv->regs->channel[priv->cs].chconf);
  318. /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
  319. * REVISIT: this controller could support SPI_3WIRE mode.
  320. */
  321. if (priv->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
  322. confr &= ~(OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1);
  323. confr |= OMAP3_MCSPI_CHCONF_DPE0;
  324. } else {
  325. confr &= ~OMAP3_MCSPI_CHCONF_DPE0;
  326. confr |= OMAP3_MCSPI_CHCONF_IS|OMAP3_MCSPI_CHCONF_DPE1;
  327. }
  328. /* set SPI mode 0..3 */
  329. confr &= ~(OMAP3_MCSPI_CHCONF_POL | OMAP3_MCSPI_CHCONF_PHA);
  330. if (priv->mode & SPI_CPHA)
  331. confr |= OMAP3_MCSPI_CHCONF_PHA;
  332. if (priv->mode & SPI_CPOL)
  333. confr |= OMAP3_MCSPI_CHCONF_POL;
  334. /* set chipselect polarity; manage with FORCE */
  335. if (!(priv->mode & SPI_CS_HIGH))
  336. confr |= OMAP3_MCSPI_CHCONF_EPOL; /* active-low; normal */
  337. else
  338. confr &= ~OMAP3_MCSPI_CHCONF_EPOL;
  339. /* Transmit & receive mode */
  340. confr &= ~OMAP3_MCSPI_CHCONF_TRM_MASK;
  341. omap3_spi_write_chconf(priv, confr);
  342. }
  343. static void _omap3_spi_set_wordlen(struct omap3_spi_priv *priv)
  344. {
  345. unsigned int confr;
  346. /* McSPI individual channel configuration */
  347. confr = readl(&priv->regs->channel[priv->wordlen].chconf);
  348. /* wordlength */
  349. confr &= ~OMAP3_MCSPI_CHCONF_WL_MASK;
  350. confr |= (priv->wordlen - 1) << 7;
  351. omap3_spi_write_chconf(priv, confr);
  352. }
  353. static void spi_reset(struct mcspi *regs)
  354. {
  355. unsigned int tmp;
  356. writel(OMAP3_MCSPI_SYSCONFIG_SOFTRESET, &regs->sysconfig);
  357. do {
  358. tmp = readl(&regs->sysstatus);
  359. } while (!(tmp & OMAP3_MCSPI_SYSSTATUS_RESETDONE));
  360. writel(OMAP3_MCSPI_SYSCONFIG_AUTOIDLE |
  361. OMAP3_MCSPI_SYSCONFIG_ENAWAKEUP |
  362. OMAP3_MCSPI_SYSCONFIG_SMARTIDLE, &regs->sysconfig);
  363. writel(OMAP3_MCSPI_WAKEUPENABLE_WKEN, &regs->wakeupenable);
  364. }
  365. static void _omap3_spi_claim_bus(struct omap3_spi_priv *priv)
  366. {
  367. unsigned int conf;
  368. spi_reset(priv->regs);
  369. /*
  370. * setup when switching from (reset default) slave mode
  371. * to single-channel master mode
  372. */
  373. conf = readl(&priv->regs->modulctrl);
  374. conf &= ~(OMAP3_MCSPI_MODULCTRL_STEST | OMAP3_MCSPI_MODULCTRL_MS);
  375. conf |= OMAP3_MCSPI_MODULCTRL_SINGLE;
  376. writel(conf, &priv->regs->modulctrl);
  377. _omap3_spi_set_mode(priv);
  378. _omap3_spi_set_speed(priv);
  379. }
  380. #ifndef CONFIG_DM_SPI
  381. struct omap3_spi_slave {
  382. struct spi_slave slave;
  383. struct omap3_spi_priv spi_priv;
  384. };
  385. struct omap3_spi_priv *priv;
  386. static inline struct omap3_spi_slave *to_omap3_spi(struct spi_slave *slave)
  387. {
  388. return container_of(slave, struct omap3_spi_slave, slave);
  389. }
  390. void spi_init(void)
  391. {
  392. /* do nothing */
  393. }
  394. void spi_free_slave(struct spi_slave *slave)
  395. {
  396. struct omap3_spi_slave *ds = to_omap3_spi(slave);
  397. free(ds);
  398. }
  399. int spi_claim_bus(struct spi_slave *slave)
  400. {
  401. _omap3_spi_claim_bus(priv);
  402. _omap3_spi_set_wordlen(priv);
  403. _omap3_spi_set_mode(priv);
  404. _omap3_spi_set_speed(priv);
  405. return 0;
  406. }
  407. void spi_release_bus(struct spi_slave *slave)
  408. {
  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_slave *ds;
  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. ds = spi_alloc_slave(struct omap3_spi_slave, bus, cs);
  464. if (!ds) {
  465. printf("SPI error: malloc of SPI structure failed\n");
  466. return NULL;
  467. }
  468. priv = &ds->spi_priv;
  469. priv->regs = regs;
  470. priv->cs = cs;
  471. priv->freq = max_hz;
  472. priv->mode = mode;
  473. priv->wordlen = ds->slave.wordlen;
  474. #ifdef CONFIG_OMAP3_SPI_D0_D1_SWAPPED
  475. priv->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
  476. #endif
  477. return &ds->slave;
  478. }
  479. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  480. const void *dout, void *din, unsigned long flags)
  481. { return _spi_xfer(priv, bitlen, dout, din, flags); }
  482. #else
  483. static int omap3_spi_claim_bus(struct udevice *dev)
  484. {
  485. struct udevice *bus = dev->parent;
  486. struct omap3_spi_priv *priv = dev_get_priv(bus);
  487. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  488. priv->cs = slave_plat->cs;
  489. priv->mode = slave_plat->mode;
  490. priv->freq = slave_plat->max_hz;
  491. _omap3_spi_claim_bus(priv);
  492. return 0;
  493. }
  494. static int omap3_spi_release_bus(struct udevice *dev)
  495. {
  496. struct udevice *bus = dev->parent;
  497. struct omap3_spi_priv *priv = dev_get_priv(bus);
  498. /* Reset the SPI hardware */
  499. spi_reset(priv->regs);
  500. return 0;
  501. }
  502. static int omap3_spi_set_wordlen(struct udevice *dev, unsigned int wordlen)
  503. {
  504. struct udevice *bus = dev->parent;
  505. struct omap3_spi_priv *priv = dev_get_priv(bus);
  506. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  507. priv->cs = slave_plat->cs;
  508. priv->wordlen = wordlen;
  509. _omap3_spi_set_wordlen(priv);
  510. return 0;
  511. }
  512. static int omap3_spi_probe(struct udevice *dev)
  513. {
  514. struct omap3_spi_priv *priv = dev_get_priv(dev);
  515. const void *blob = gd->fdt_blob;
  516. int node = dev->of_offset;
  517. priv->regs = (struct mcspi *)dev_get_addr(dev);
  518. priv->pin_dir = fdtdec_get_uint(blob, node, "ti,pindir-d0-out-d1-in",
  519. MCSPI_PINDIR_D0_IN_D1_OUT);
  520. priv->wordlen = SPI_DEFAULT_WORDLEN;
  521. return 0;
  522. }
  523. static int omap3_spi_xfer(struct udevice *dev, unsigned int bitlen,
  524. const void *dout, void *din, unsigned long flags)
  525. {
  526. struct udevice *bus = dev->parent;
  527. struct omap3_spi_priv *priv = dev_get_priv(bus);
  528. return _spi_xfer(priv, bitlen, dout, din, flags);
  529. }
  530. static int omap3_spi_set_speed(struct udevice *bus, unsigned int speed)
  531. {
  532. return 0;
  533. }
  534. static int omap3_spi_set_mode(struct udevice *bus, uint mode)
  535. {
  536. return 0;
  537. }
  538. static const struct dm_spi_ops omap3_spi_ops = {
  539. .claim_bus = omap3_spi_claim_bus,
  540. .release_bus = omap3_spi_release_bus,
  541. .set_wordlen = omap3_spi_set_wordlen,
  542. .xfer = omap3_spi_xfer,
  543. .set_speed = omap3_spi_set_speed,
  544. .set_mode = omap3_spi_set_mode,
  545. /*
  546. * cs_info is not needed, since we require all chip selects to be
  547. * in the device tree explicitly
  548. */
  549. };
  550. static const struct udevice_id omap3_spi_ids[] = {
  551. { .compatible = "ti,omap2-mcspi" },
  552. { .compatible = "ti,omap4-mcspi" },
  553. { }
  554. };
  555. U_BOOT_DRIVER(omap3_spi) = {
  556. .name = "omap3_spi",
  557. .id = UCLASS_SPI,
  558. .of_match = omap3_spi_ids,
  559. .probe = omap3_spi_probe,
  560. .ops = &omap3_spi_ops,
  561. .priv_auto_alloc_size = sizeof(struct omap3_spi_priv),
  562. .probe = omap3_spi_probe,
  563. };
  564. #endif