fsl_dspi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * (C) Copyright 2000-2003
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Copyright (C) 2004-2009, 2015 Freescale Semiconductor, Inc.
  6. * TsiChung Liew (Tsi-Chung.Liew@freescale.com)
  7. * Chao Fu (B44548@freescale.com)
  8. * Haikun Wang (B53464@freescale.com)
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <common.h>
  16. #include <spi.h>
  17. #include <malloc.h>
  18. #include <asm/io.h>
  19. #include <fdtdec.h>
  20. #ifndef CONFIG_M68K
  21. #include <asm/arch/clock.h>
  22. #endif
  23. #include <fsl_dspi.h>
  24. DECLARE_GLOBAL_DATA_PTR;
  25. /* fsl_dspi_platdata flags */
  26. #define DSPI_FLAG_REGMAP_ENDIAN_BIG BIT(0)
  27. /* idle data value */
  28. #define DSPI_IDLE_VAL 0x0
  29. /* max chipselect signals number */
  30. #define FSL_DSPI_MAX_CHIPSELECT 6
  31. /* default SCK frequency, unit: HZ */
  32. #define FSL_DSPI_DEFAULT_SCK_FREQ 10000000
  33. /* tx/rx data wait timeout value, unit: us */
  34. #define DSPI_TXRX_WAIT_TIMEOUT 1000000
  35. /* CTAR register pre-configure value */
  36. #define DSPI_CTAR_DEFAULT_VALUE (DSPI_CTAR_TRSZ(7) | \
  37. DSPI_CTAR_PCSSCK_1CLK | \
  38. DSPI_CTAR_PASC(0) | \
  39. DSPI_CTAR_PDT(0) | \
  40. DSPI_CTAR_CSSCK(0) | \
  41. DSPI_CTAR_ASC(0) | \
  42. DSPI_CTAR_DT(0))
  43. /* CTAR register pre-configure mask */
  44. #define DSPI_CTAR_SET_MODE_MASK (DSPI_CTAR_TRSZ(15) | \
  45. DSPI_CTAR_PCSSCK(3) | \
  46. DSPI_CTAR_PASC(3) | \
  47. DSPI_CTAR_PDT(3) | \
  48. DSPI_CTAR_CSSCK(15) | \
  49. DSPI_CTAR_ASC(15) | \
  50. DSPI_CTAR_DT(15))
  51. /**
  52. * struct fsl_dspi_platdata - platform data for Freescale DSPI
  53. *
  54. * @flags: Flags for DSPI DSPI_FLAG_...
  55. * @speed_hz: Default SCK frequency
  56. * @num_chipselect: Number of DSPI chipselect signals
  57. * @regs_addr: Base address of DSPI registers
  58. */
  59. struct fsl_dspi_platdata {
  60. uint flags;
  61. uint speed_hz;
  62. uint num_chipselect;
  63. fdt_addr_t regs_addr;
  64. };
  65. /**
  66. * struct fsl_dspi_priv - private data for Freescale DSPI
  67. *
  68. * @flags: Flags for DSPI DSPI_FLAG_...
  69. * @mode: SPI mode to use for slave device (see SPI mode flags)
  70. * @mcr_val: MCR register configure value
  71. * @bus_clk: DSPI input clk frequency
  72. * @speed_hz: Default SCK frequency
  73. * @charbit: How many bits in every transfer
  74. * @num_chipselect: Number of DSPI chipselect signals
  75. * @ctar_val: CTAR register configure value of per chipselect slave device
  76. * @regs: Point to DSPI register structure for I/O access
  77. */
  78. struct fsl_dspi_priv {
  79. uint flags;
  80. uint mode;
  81. uint mcr_val;
  82. uint bus_clk;
  83. uint speed_hz;
  84. uint charbit;
  85. uint num_chipselect;
  86. uint ctar_val[FSL_DSPI_MAX_CHIPSELECT];
  87. struct dspi *regs;
  88. };
  89. #ifndef CONFIG_DM_SPI
  90. struct fsl_dspi {
  91. struct spi_slave slave;
  92. struct fsl_dspi_priv priv;
  93. };
  94. #endif
  95. __weak void cpu_dspi_port_conf(void)
  96. {
  97. }
  98. __weak int cpu_dspi_claim_bus(uint bus, uint cs)
  99. {
  100. return 0;
  101. }
  102. __weak void cpu_dspi_release_bus(uint bus, uint cs)
  103. {
  104. }
  105. static uint dspi_read32(uint flags, uint *addr)
  106. {
  107. return flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
  108. in_be32(addr) : in_le32(addr);
  109. }
  110. static void dspi_write32(uint flags, uint *addr, uint val)
  111. {
  112. flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ?
  113. out_be32(addr, val) : out_le32(addr, val);
  114. }
  115. static void dspi_halt(struct fsl_dspi_priv *priv, u8 halt)
  116. {
  117. uint mcr_val;
  118. mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
  119. if (halt)
  120. mcr_val |= DSPI_MCR_HALT;
  121. else
  122. mcr_val &= ~DSPI_MCR_HALT;
  123. dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
  124. }
  125. static void fsl_dspi_init_mcr(struct fsl_dspi_priv *priv, uint cfg_val)
  126. {
  127. /* halt DSPI module */
  128. dspi_halt(priv, 1);
  129. dspi_write32(priv->flags, &priv->regs->mcr, cfg_val);
  130. /* resume module */
  131. dspi_halt(priv, 0);
  132. priv->mcr_val = cfg_val;
  133. }
  134. static void fsl_dspi_cfg_cs_active_state(struct fsl_dspi_priv *priv,
  135. uint cs, uint state)
  136. {
  137. uint mcr_val;
  138. dspi_halt(priv, 1);
  139. mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
  140. if (state & SPI_CS_HIGH)
  141. /* CSx inactive state is low */
  142. mcr_val &= ~DSPI_MCR_PCSIS(cs);
  143. else
  144. /* CSx inactive state is high */
  145. mcr_val |= DSPI_MCR_PCSIS(cs);
  146. dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
  147. dspi_halt(priv, 0);
  148. }
  149. static int fsl_dspi_cfg_ctar_mode(struct fsl_dspi_priv *priv,
  150. uint cs, uint mode)
  151. {
  152. uint bus_setup;
  153. bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
  154. bus_setup &= ~DSPI_CTAR_SET_MODE_MASK;
  155. bus_setup |= priv->ctar_val[cs];
  156. bus_setup &= ~(DSPI_CTAR_CPOL | DSPI_CTAR_CPHA | DSPI_CTAR_LSBFE);
  157. if (mode & SPI_CPOL)
  158. bus_setup |= DSPI_CTAR_CPOL;
  159. if (mode & SPI_CPHA)
  160. bus_setup |= DSPI_CTAR_CPHA;
  161. if (mode & SPI_LSB_FIRST)
  162. bus_setup |= DSPI_CTAR_LSBFE;
  163. dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
  164. priv->charbit =
  165. ((dspi_read32(priv->flags, &priv->regs->ctar[0]) &
  166. DSPI_CTAR_TRSZ(15)) == DSPI_CTAR_TRSZ(15)) ? 16 : 8;
  167. return 0;
  168. }
  169. static void fsl_dspi_clr_fifo(struct fsl_dspi_priv *priv)
  170. {
  171. uint mcr_val;
  172. dspi_halt(priv, 1);
  173. mcr_val = dspi_read32(priv->flags, &priv->regs->mcr);
  174. /* flush RX and TX FIFO */
  175. mcr_val |= (DSPI_MCR_CTXF | DSPI_MCR_CRXF);
  176. dspi_write32(priv->flags, &priv->regs->mcr, mcr_val);
  177. dspi_halt(priv, 0);
  178. }
  179. static void dspi_tx(struct fsl_dspi_priv *priv, u32 ctrl, u16 data)
  180. {
  181. int timeout = DSPI_TXRX_WAIT_TIMEOUT;
  182. /* wait for empty entries in TXFIFO or timeout */
  183. while (DSPI_SR_TXCTR(dspi_read32(priv->flags, &priv->regs->sr)) >= 4 &&
  184. timeout--)
  185. udelay(1);
  186. if (timeout >= 0)
  187. dspi_write32(priv->flags, &priv->regs->tfr, (ctrl | data));
  188. else
  189. debug("dspi_tx: waiting timeout!\n");
  190. }
  191. static u16 dspi_rx(struct fsl_dspi_priv *priv)
  192. {
  193. int timeout = DSPI_TXRX_WAIT_TIMEOUT;
  194. /* wait for valid entries in RXFIFO or timeout */
  195. while (DSPI_SR_RXCTR(dspi_read32(priv->flags, &priv->regs->sr)) == 0 &&
  196. timeout--)
  197. udelay(1);
  198. if (timeout >= 0)
  199. return (u16)DSPI_RFR_RXDATA(
  200. dspi_read32(priv->flags, &priv->regs->rfr));
  201. else {
  202. debug("dspi_rx: waiting timeout!\n");
  203. return (u16)(~0);
  204. }
  205. }
  206. static int dspi_xfer(struct fsl_dspi_priv *priv, uint cs, unsigned int bitlen,
  207. const void *dout, void *din, unsigned long flags)
  208. {
  209. u16 *spi_rd16 = NULL, *spi_wr16 = NULL;
  210. u8 *spi_rd = NULL, *spi_wr = NULL;
  211. static u32 ctrl;
  212. uint len = bitlen >> 3;
  213. if (priv->charbit == 16) {
  214. bitlen >>= 1;
  215. spi_wr16 = (u16 *)dout;
  216. spi_rd16 = (u16 *)din;
  217. } else {
  218. spi_wr = (u8 *)dout;
  219. spi_rd = (u8 *)din;
  220. }
  221. if ((flags & SPI_XFER_BEGIN) == SPI_XFER_BEGIN)
  222. ctrl |= DSPI_TFR_CONT;
  223. ctrl = ctrl & DSPI_TFR_CONT;
  224. ctrl = ctrl | DSPI_TFR_CTAS(0) | DSPI_TFR_PCS(cs);
  225. if (len > 1) {
  226. int tmp_len = len - 1;
  227. while (tmp_len--) {
  228. if (dout != NULL) {
  229. if (priv->charbit == 16)
  230. dspi_tx(priv, ctrl, *spi_wr16++);
  231. else
  232. dspi_tx(priv, ctrl, *spi_wr++);
  233. dspi_rx(priv);
  234. }
  235. if (din != NULL) {
  236. dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
  237. if (priv->charbit == 16)
  238. *spi_rd16++ = dspi_rx(priv);
  239. else
  240. *spi_rd++ = dspi_rx(priv);
  241. }
  242. }
  243. len = 1; /* remaining byte */
  244. }
  245. if ((flags & SPI_XFER_END) == SPI_XFER_END)
  246. ctrl &= ~DSPI_TFR_CONT;
  247. if (len) {
  248. if (dout != NULL) {
  249. if (priv->charbit == 16)
  250. dspi_tx(priv, ctrl, *spi_wr16);
  251. else
  252. dspi_tx(priv, ctrl, *spi_wr);
  253. dspi_rx(priv);
  254. }
  255. if (din != NULL) {
  256. dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
  257. if (priv->charbit == 16)
  258. *spi_rd16 = dspi_rx(priv);
  259. else
  260. *spi_rd = dspi_rx(priv);
  261. }
  262. } else {
  263. /* dummy read */
  264. dspi_tx(priv, ctrl, DSPI_IDLE_VAL);
  265. dspi_rx(priv);
  266. }
  267. return 0;
  268. }
  269. /**
  270. * Calculate the divide value between input clk frequency and expected SCK frequency
  271. * Formula: SCK = (clkrate/pbr) x ((1+dbr)/br)
  272. * Dbr: use default value 0
  273. *
  274. * @pbr: return Baud Rate Prescaler value
  275. * @br: return Baud Rate Scaler value
  276. * @speed_hz: expected SCK frequency
  277. * @clkrate: input clk frequency
  278. */
  279. static int fsl_dspi_hz_to_spi_baud(int *pbr, int *br,
  280. int speed_hz, uint clkrate)
  281. {
  282. /* Valid baud rate pre-scaler values */
  283. int pbr_tbl[4] = {2, 3, 5, 7};
  284. int brs[16] = {2, 4, 6, 8,
  285. 16, 32, 64, 128,
  286. 256, 512, 1024, 2048,
  287. 4096, 8192, 16384, 32768};
  288. int temp, i = 0, j = 0;
  289. temp = clkrate / speed_hz;
  290. for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
  291. for (j = 0; j < ARRAY_SIZE(brs); j++) {
  292. if (pbr_tbl[i] * brs[j] >= temp) {
  293. *pbr = i;
  294. *br = j;
  295. return 0;
  296. }
  297. }
  298. debug("Can not find valid baud rate,speed_hz is %d, ", speed_hz);
  299. debug("clkrate is %d, we use the max prescaler value.\n", clkrate);
  300. *pbr = ARRAY_SIZE(pbr_tbl) - 1;
  301. *br = ARRAY_SIZE(brs) - 1;
  302. return -EINVAL;
  303. }
  304. static int fsl_dspi_cfg_speed(struct fsl_dspi_priv *priv, uint speed)
  305. {
  306. int ret;
  307. uint bus_setup;
  308. int best_i, best_j, bus_clk;
  309. bus_clk = priv->bus_clk;
  310. debug("DSPI set_speed: expected SCK speed %u, bus_clk %u.\n",
  311. speed, bus_clk);
  312. bus_setup = dspi_read32(priv->flags, &priv->regs->ctar[0]);
  313. bus_setup &= ~(DSPI_CTAR_DBR | DSPI_CTAR_PBR(0x3) | DSPI_CTAR_BR(0xf));
  314. ret = fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
  315. if (ret) {
  316. speed = priv->speed_hz;
  317. debug("DSPI set_speed use default SCK rate %u.\n", speed);
  318. fsl_dspi_hz_to_spi_baud(&best_i, &best_j, speed, bus_clk);
  319. }
  320. bus_setup |= (DSPI_CTAR_PBR(best_i) | DSPI_CTAR_BR(best_j));
  321. dspi_write32(priv->flags, &priv->regs->ctar[0], bus_setup);
  322. priv->speed_hz = speed;
  323. return 0;
  324. }
  325. #ifndef CONFIG_DM_SPI
  326. void spi_init(void)
  327. {
  328. /* Nothing to do */
  329. }
  330. void spi_init_f(void)
  331. {
  332. /* Nothing to do */
  333. }
  334. void spi_init_r(void)
  335. {
  336. /* Nothing to do */
  337. }
  338. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  339. {
  340. if (((cs >= 0) && (cs < 8)) && ((bus >= 0) && (bus < 8)))
  341. return 1;
  342. else
  343. return 0;
  344. }
  345. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  346. unsigned int max_hz, unsigned int mode)
  347. {
  348. struct fsl_dspi *dspi;
  349. uint mcr_cfg_val;
  350. dspi = spi_alloc_slave(struct fsl_dspi, bus, cs);
  351. if (!dspi)
  352. return NULL;
  353. cpu_dspi_port_conf();
  354. #ifdef CONFIG_SYS_FSL_DSPI_BE
  355. dspi->priv.flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
  356. #endif
  357. dspi->priv.regs = (struct dspi *)MMAP_DSPI;
  358. #ifdef CONFIG_M68K
  359. dspi->priv.bus_clk = gd->bus_clk;
  360. #else
  361. dspi->priv.bus_clk = mxc_get_clock(MXC_DSPI_CLK);
  362. #endif
  363. dspi->priv.speed_hz = FSL_DSPI_DEFAULT_SCK_FREQ;
  364. /* default: all CS signals inactive state is high */
  365. mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
  366. DSPI_MCR_CRXF | DSPI_MCR_CTXF;
  367. fsl_dspi_init_mcr(&dspi->priv, mcr_cfg_val);
  368. for (i = 0; i < FSL_DSPI_MAX_CHIPSELECT; i++)
  369. dspi->priv.ctar_val[i] = DSPI_CTAR_DEFAULT_VALUE;
  370. #ifdef CONFIG_SYS_DSPI_CTAR0
  371. if (FSL_DSPI_MAX_CHIPSELECT > 0)
  372. dspi->priv.ctar_val[0] = CONFIG_SYS_DSPI_CTAR0;
  373. #endif
  374. #ifdef CONFIG_SYS_DSPI_CTAR1
  375. if (FSL_DSPI_MAX_CHIPSELECT > 1)
  376. dspi->priv.ctar_val[1] = CONFIG_SYS_DSPI_CTAR1;
  377. #endif
  378. #ifdef CONFIG_SYS_DSPI_CTAR2
  379. if (FSL_DSPI_MAX_CHIPSELECT > 2)
  380. dspi->priv.ctar_val[2] = CONFIG_SYS_DSPI_CTAR2;
  381. #endif
  382. #ifdef CONFIG_SYS_DSPI_CTAR3
  383. if (FSL_DSPI_MAX_CHIPSELECT > 3)
  384. dspi->priv.ctar_val[3] = CONFIG_SYS_DSPI_CTAR3;
  385. #endif
  386. #ifdef CONFIG_SYS_DSPI_CTAR4
  387. if (FSL_DSPI_MAX_CHIPSELECT > 4)
  388. dspi->priv.ctar_val[4] = CONFIG_SYS_DSPI_CTAR4;
  389. #endif
  390. #ifdef CONFIG_SYS_DSPI_CTAR5
  391. if (FSL_DSPI_MAX_CHIPSELECT > 5)
  392. dspi->priv.ctar_val[5] = CONFIG_SYS_DSPI_CTAR5;
  393. #endif
  394. #ifdef CONFIG_SYS_DSPI_CTAR6
  395. if (FSL_DSPI_MAX_CHIPSELECT > 6)
  396. dspi->priv.ctar_val[6] = CONFIG_SYS_DSPI_CTAR6;
  397. #endif
  398. #ifdef CONFIG_SYS_DSPI_CTAR7
  399. if (FSL_DSPI_MAX_CHIPSELECT > 7)
  400. dspi->priv.ctar_val[7] = CONFIG_SYS_DSPI_CTAR7;
  401. #endif
  402. fsl_dspi_cfg_speed(&dspi->priv, max_hz);
  403. /* configure transfer mode */
  404. fsl_dspi_cfg_ctar_mode(&dspi->priv, cs, mode);
  405. /* configure active state of CSX */
  406. fsl_dspi_cfg_cs_active_state(&dspi->priv, cs, mode);
  407. return &dspi->slave;
  408. }
  409. void spi_free_slave(struct spi_slave *slave)
  410. {
  411. free(slave);
  412. }
  413. int spi_claim_bus(struct spi_slave *slave)
  414. {
  415. uint sr_val;
  416. struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
  417. cpu_dspi_claim_bus(slave->bus, slave->cs);
  418. fsl_dspi_clr_fifo(&dspi->priv);
  419. /* check module TX and RX status */
  420. sr_val = dspi_read32(dspi->priv.flags, &dspi->priv.regs->sr);
  421. if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
  422. debug("DSPI RX/TX not ready!\n");
  423. return -EIO;
  424. }
  425. return 0;
  426. }
  427. void spi_release_bus(struct spi_slave *slave)
  428. {
  429. struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
  430. dspi_halt(&dspi->priv, 1);
  431. cpu_dspi_release_bus(slave->bus.slave->cs);
  432. }
  433. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  434. void *din, unsigned long flags)
  435. {
  436. struct fsl_dspi *dspi = (struct fsl_dspi *)slave;
  437. return dspi_xfer(&dspi->priv, slave->cs, bitlen, dout, din, flags);
  438. }
  439. #else
  440. static int fsl_dspi_child_pre_probe(struct udevice *dev)
  441. {
  442. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  443. struct fsl_dspi_priv *priv = dev_get_priv(dev->parent);
  444. if (slave_plat->cs >= priv->num_chipselect) {
  445. debug("DSPI invalid chipselect number %d(max %d)!\n",
  446. slave_plat->cs, priv->num_chipselect - 1);
  447. return -EINVAL;
  448. }
  449. priv->ctar_val[slave_plat->cs] = DSPI_CTAR_DEFAULT_VALUE;
  450. debug("DSPI pre_probe slave device on CS %u, max_hz %u, mode 0x%x.\n",
  451. slave_plat->cs, slave_plat->max_hz, slave_plat->mode);
  452. return 0;
  453. }
  454. static int fsl_dspi_probe(struct udevice *bus)
  455. {
  456. struct fsl_dspi_platdata *plat = dev_get_platdata(bus);
  457. struct fsl_dspi_priv *priv = dev_get_priv(bus);
  458. struct dm_spi_bus *dm_spi_bus;
  459. uint mcr_cfg_val;
  460. dm_spi_bus = bus->uclass_priv;
  461. /* cpu speical pin muxing configure */
  462. cpu_dspi_port_conf();
  463. /* get input clk frequency */
  464. priv->regs = (struct dspi *)plat->regs_addr;
  465. priv->flags = plat->flags;
  466. #ifdef CONFIG_M68K
  467. priv->bus_clk = gd->bus_clk;
  468. #else
  469. priv->bus_clk = mxc_get_clock(MXC_DSPI_CLK);
  470. #endif
  471. priv->num_chipselect = plat->num_chipselect;
  472. priv->speed_hz = plat->speed_hz;
  473. /* frame data length in bits, default 8bits */
  474. priv->charbit = 8;
  475. dm_spi_bus->max_hz = plat->speed_hz;
  476. /* default: all CS signals inactive state is high */
  477. mcr_cfg_val = DSPI_MCR_MSTR | DSPI_MCR_PCSIS_MASK |
  478. DSPI_MCR_CRXF | DSPI_MCR_CTXF;
  479. fsl_dspi_init_mcr(priv, mcr_cfg_val);
  480. debug("%s probe done, bus-num %d.\n", bus->name, bus->seq);
  481. return 0;
  482. }
  483. static int fsl_dspi_claim_bus(struct udevice *dev)
  484. {
  485. uint sr_val;
  486. struct fsl_dspi_priv *priv;
  487. struct udevice *bus = dev->parent;
  488. struct dm_spi_slave_platdata *slave_plat =
  489. dev_get_parent_platdata(dev);
  490. priv = dev_get_priv(bus);
  491. /* processor special preparation work */
  492. cpu_dspi_claim_bus(bus->seq, slave_plat->cs);
  493. /* configure transfer mode */
  494. fsl_dspi_cfg_ctar_mode(priv, slave_plat->cs, priv->mode);
  495. /* configure active state of CSX */
  496. fsl_dspi_cfg_cs_active_state(priv, slave_plat->cs,
  497. priv->mode);
  498. fsl_dspi_clr_fifo(priv);
  499. /* check module TX and RX status */
  500. sr_val = dspi_read32(priv->flags, &priv->regs->sr);
  501. if ((sr_val & DSPI_SR_TXRXS) != DSPI_SR_TXRXS) {
  502. debug("DSPI RX/TX not ready!\n");
  503. return -EIO;
  504. }
  505. return 0;
  506. }
  507. static int fsl_dspi_release_bus(struct udevice *dev)
  508. {
  509. struct udevice *bus = dev->parent;
  510. struct fsl_dspi_priv *priv = dev_get_priv(bus);
  511. struct dm_spi_slave_platdata *slave_plat =
  512. dev_get_parent_platdata(dev);
  513. /* halt module */
  514. dspi_halt(priv, 1);
  515. /* processor special release work */
  516. cpu_dspi_release_bus(bus->seq, slave_plat->cs);
  517. return 0;
  518. }
  519. /**
  520. * This function doesn't do anything except help with debugging
  521. */
  522. static int fsl_dspi_bind(struct udevice *bus)
  523. {
  524. debug("%s assigned req_seq %d.\n", bus->name, bus->req_seq);
  525. return 0;
  526. }
  527. static int fsl_dspi_ofdata_to_platdata(struct udevice *bus)
  528. {
  529. fdt_addr_t addr;
  530. struct fsl_dspi_platdata *plat = bus->platdata;
  531. const void *blob = gd->fdt_blob;
  532. int node = dev_of_offset(bus);
  533. if (fdtdec_get_bool(blob, node, "big-endian"))
  534. plat->flags |= DSPI_FLAG_REGMAP_ENDIAN_BIG;
  535. plat->num_chipselect =
  536. fdtdec_get_int(blob, node, "num-cs", FSL_DSPI_MAX_CHIPSELECT);
  537. addr = devfdt_get_addr(bus);
  538. if (addr == FDT_ADDR_T_NONE) {
  539. debug("DSPI: Can't get base address or size\n");
  540. return -ENOMEM;
  541. }
  542. plat->regs_addr = addr;
  543. plat->speed_hz = fdtdec_get_int(blob,
  544. node, "spi-max-frequency", FSL_DSPI_DEFAULT_SCK_FREQ);
  545. debug("DSPI: regs=%pa, max-frequency=%d, endianess=%s, num-cs=%d\n",
  546. &plat->regs_addr, plat->speed_hz,
  547. plat->flags & DSPI_FLAG_REGMAP_ENDIAN_BIG ? "be" : "le",
  548. plat->num_chipselect);
  549. return 0;
  550. }
  551. static int fsl_dspi_xfer(struct udevice *dev, unsigned int bitlen,
  552. const void *dout, void *din, unsigned long flags)
  553. {
  554. struct fsl_dspi_priv *priv;
  555. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  556. struct udevice *bus;
  557. bus = dev->parent;
  558. priv = dev_get_priv(bus);
  559. return dspi_xfer(priv, slave_plat->cs, bitlen, dout, din, flags);
  560. }
  561. static int fsl_dspi_set_speed(struct udevice *bus, uint speed)
  562. {
  563. struct fsl_dspi_priv *priv = dev_get_priv(bus);
  564. return fsl_dspi_cfg_speed(priv, speed);
  565. }
  566. static int fsl_dspi_set_mode(struct udevice *bus, uint mode)
  567. {
  568. struct fsl_dspi_priv *priv = dev_get_priv(bus);
  569. debug("DSPI set_mode: mode 0x%x.\n", mode);
  570. /*
  571. * We store some chipselect special configure value in priv->ctar_val,
  572. * and we can't get the correct chipselect number here,
  573. * so just store mode value.
  574. * Do really configuration when claim_bus.
  575. */
  576. priv->mode = mode;
  577. return 0;
  578. }
  579. static const struct dm_spi_ops fsl_dspi_ops = {
  580. .claim_bus = fsl_dspi_claim_bus,
  581. .release_bus = fsl_dspi_release_bus,
  582. .xfer = fsl_dspi_xfer,
  583. .set_speed = fsl_dspi_set_speed,
  584. .set_mode = fsl_dspi_set_mode,
  585. };
  586. static const struct udevice_id fsl_dspi_ids[] = {
  587. { .compatible = "fsl,vf610-dspi" },
  588. { }
  589. };
  590. U_BOOT_DRIVER(fsl_dspi) = {
  591. .name = "fsl_dspi",
  592. .id = UCLASS_SPI,
  593. .of_match = fsl_dspi_ids,
  594. .ops = &fsl_dspi_ops,
  595. .ofdata_to_platdata = fsl_dspi_ofdata_to_platdata,
  596. .platdata_auto_alloc_size = sizeof(struct fsl_dspi_platdata),
  597. .priv_auto_alloc_size = sizeof(struct fsl_dspi_priv),
  598. .probe = fsl_dspi_probe,
  599. .child_pre_probe = fsl_dspi_child_pre_probe,
  600. .bind = fsl_dspi_bind,
  601. };
  602. #endif