omap3_spi.c 18 KB

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