spi-mxs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. * Freescale MXS SPI master driver
  3. *
  4. * Copyright 2012 DENX Software Engineering, GmbH.
  5. * Copyright 2012 Freescale Semiconductor, Inc.
  6. * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
  7. *
  8. * Rework and transition to new API by:
  9. * Marek Vasut <marex@denx.de>
  10. *
  11. * Based on previous attempt by:
  12. * Fabio Estevam <fabio.estevam@freescale.com>
  13. *
  14. * Based on code from U-Boot bootloader by:
  15. * Marek Vasut <marex@denx.de>
  16. *
  17. * Based on spi-stmp.c, which is:
  18. * Author: Dmitry Pervushin <dimka@embeddedalley.com>
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/ioport.h>
  32. #include <linux/of.h>
  33. #include <linux/of_device.h>
  34. #include <linux/of_gpio.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/delay.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/dmaengine.h>
  40. #include <linux/highmem.h>
  41. #include <linux/clk.h>
  42. #include <linux/err.h>
  43. #include <linux/completion.h>
  44. #include <linux/gpio.h>
  45. #include <linux/regulator/consumer.h>
  46. #include <linux/module.h>
  47. #include <linux/stmp_device.h>
  48. #include <linux/spi/spi.h>
  49. #include <linux/spi/mxs-spi.h>
  50. #define DRIVER_NAME "mxs-spi"
  51. /* Use 10S timeout for very long transfers, it should suffice. */
  52. #define SSP_TIMEOUT 10000
  53. #define SG_MAXLEN 0xff00
  54. /*
  55. * Flags for txrx functions. More efficient that using an argument register for
  56. * each one.
  57. */
  58. #define TXRX_WRITE (1<<0) /* This is a write */
  59. #define TXRX_DEASSERT_CS (1<<1) /* De-assert CS at end of txrx */
  60. struct mxs_spi {
  61. struct mxs_ssp ssp;
  62. struct completion c;
  63. unsigned int sck; /* Rate requested (vs actual) */
  64. };
  65. static int mxs_spi_setup_transfer(struct spi_device *dev,
  66. const struct spi_transfer *t)
  67. {
  68. struct mxs_spi *spi = spi_master_get_devdata(dev->master);
  69. struct mxs_ssp *ssp = &spi->ssp;
  70. const unsigned int hz = min(dev->max_speed_hz, t->speed_hz);
  71. if (hz == 0) {
  72. dev_err(&dev->dev, "SPI clock rate of zero not allowed\n");
  73. return -EINVAL;
  74. }
  75. if (hz != spi->sck) {
  76. mxs_ssp_set_clk_rate(ssp, hz);
  77. /*
  78. * Save requested rate, hz, rather than the actual rate,
  79. * ssp->clk_rate. Otherwise we would set the rate every transfer
  80. * when the actual rate is not quite the same as requested rate.
  81. */
  82. spi->sck = hz;
  83. /*
  84. * Perhaps we should return an error if the actual clock is
  85. * nowhere close to what was requested?
  86. */
  87. }
  88. writel(BM_SSP_CTRL0_LOCK_CS,
  89. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  90. writel(BF_SSP_CTRL1_SSP_MODE(BV_SSP_CTRL1_SSP_MODE__SPI) |
  91. BF_SSP_CTRL1_WORD_LENGTH(BV_SSP_CTRL1_WORD_LENGTH__EIGHT_BITS) |
  92. ((dev->mode & SPI_CPOL) ? BM_SSP_CTRL1_POLARITY : 0) |
  93. ((dev->mode & SPI_CPHA) ? BM_SSP_CTRL1_PHASE : 0),
  94. ssp->base + HW_SSP_CTRL1(ssp));
  95. writel(0x0, ssp->base + HW_SSP_CMD0);
  96. writel(0x0, ssp->base + HW_SSP_CMD1);
  97. return 0;
  98. }
  99. static u32 mxs_spi_cs_to_reg(unsigned cs)
  100. {
  101. u32 select = 0;
  102. /*
  103. * i.MX28 Datasheet: 17.10.1: HW_SSP_CTRL0
  104. *
  105. * The bits BM_SSP_CTRL0_WAIT_FOR_CMD and BM_SSP_CTRL0_WAIT_FOR_IRQ
  106. * in HW_SSP_CTRL0 register do have multiple usage, please refer to
  107. * the datasheet for further details. In SPI mode, they are used to
  108. * toggle the chip-select lines (nCS pins).
  109. */
  110. if (cs & 1)
  111. select |= BM_SSP_CTRL0_WAIT_FOR_CMD;
  112. if (cs & 2)
  113. select |= BM_SSP_CTRL0_WAIT_FOR_IRQ;
  114. return select;
  115. }
  116. static int mxs_ssp_wait(struct mxs_spi *spi, int offset, int mask, bool set)
  117. {
  118. const unsigned long timeout = jiffies + msecs_to_jiffies(SSP_TIMEOUT);
  119. struct mxs_ssp *ssp = &spi->ssp;
  120. u32 reg;
  121. do {
  122. reg = readl_relaxed(ssp->base + offset);
  123. if (!set)
  124. reg = ~reg;
  125. reg &= mask;
  126. if (reg == mask)
  127. return 0;
  128. } while (time_before(jiffies, timeout));
  129. return -ETIMEDOUT;
  130. }
  131. static void mxs_ssp_dma_irq_callback(void *param)
  132. {
  133. struct mxs_spi *spi = param;
  134. complete(&spi->c);
  135. }
  136. static irqreturn_t mxs_ssp_irq_handler(int irq, void *dev_id)
  137. {
  138. struct mxs_ssp *ssp = dev_id;
  139. dev_err(ssp->dev, "%s[%i] CTRL1=%08x STATUS=%08x\n",
  140. __func__, __LINE__,
  141. readl(ssp->base + HW_SSP_CTRL1(ssp)),
  142. readl(ssp->base + HW_SSP_STATUS(ssp)));
  143. return IRQ_HANDLED;
  144. }
  145. static int mxs_spi_txrx_dma(struct mxs_spi *spi,
  146. unsigned char *buf, int len,
  147. unsigned int flags)
  148. {
  149. struct mxs_ssp *ssp = &spi->ssp;
  150. struct dma_async_tx_descriptor *desc = NULL;
  151. const bool vmalloced_buf = is_vmalloc_addr(buf);
  152. const int desc_len = vmalloced_buf ? PAGE_SIZE : SG_MAXLEN;
  153. const int sgs = DIV_ROUND_UP(len, desc_len);
  154. int sg_count;
  155. int min, ret;
  156. u32 ctrl0;
  157. struct page *vm_page;
  158. struct {
  159. u32 pio[4];
  160. struct scatterlist sg;
  161. } *dma_xfer;
  162. if (!len)
  163. return -EINVAL;
  164. dma_xfer = kcalloc(sgs, sizeof(*dma_xfer), GFP_KERNEL);
  165. if (!dma_xfer)
  166. return -ENOMEM;
  167. reinit_completion(&spi->c);
  168. /* Chip select was already programmed into CTRL0 */
  169. ctrl0 = readl(ssp->base + HW_SSP_CTRL0);
  170. ctrl0 &= ~(BM_SSP_CTRL0_XFER_COUNT | BM_SSP_CTRL0_IGNORE_CRC |
  171. BM_SSP_CTRL0_READ);
  172. ctrl0 |= BM_SSP_CTRL0_DATA_XFER;
  173. if (!(flags & TXRX_WRITE))
  174. ctrl0 |= BM_SSP_CTRL0_READ;
  175. /* Queue the DMA data transfer. */
  176. for (sg_count = 0; sg_count < sgs; sg_count++) {
  177. /* Prepare the transfer descriptor. */
  178. min = min(len, desc_len);
  179. /*
  180. * De-assert CS on last segment if flag is set (i.e., no more
  181. * transfers will follow)
  182. */
  183. if ((sg_count + 1 == sgs) && (flags & TXRX_DEASSERT_CS))
  184. ctrl0 |= BM_SSP_CTRL0_IGNORE_CRC;
  185. if (ssp->devid == IMX23_SSP) {
  186. ctrl0 &= ~BM_SSP_CTRL0_XFER_COUNT;
  187. ctrl0 |= min;
  188. }
  189. dma_xfer[sg_count].pio[0] = ctrl0;
  190. dma_xfer[sg_count].pio[3] = min;
  191. if (vmalloced_buf) {
  192. vm_page = vmalloc_to_page(buf);
  193. if (!vm_page) {
  194. ret = -ENOMEM;
  195. goto err_vmalloc;
  196. }
  197. sg_init_table(&dma_xfer[sg_count].sg, 1);
  198. sg_set_page(&dma_xfer[sg_count].sg, vm_page,
  199. min, offset_in_page(buf));
  200. } else {
  201. sg_init_one(&dma_xfer[sg_count].sg, buf, min);
  202. }
  203. ret = dma_map_sg(ssp->dev, &dma_xfer[sg_count].sg, 1,
  204. (flags & TXRX_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  205. len -= min;
  206. buf += min;
  207. /* Queue the PIO register write transfer. */
  208. desc = dmaengine_prep_slave_sg(ssp->dmach,
  209. (struct scatterlist *)dma_xfer[sg_count].pio,
  210. (ssp->devid == IMX23_SSP) ? 1 : 4,
  211. DMA_TRANS_NONE,
  212. sg_count ? DMA_PREP_INTERRUPT : 0);
  213. if (!desc) {
  214. dev_err(ssp->dev,
  215. "Failed to get PIO reg. write descriptor.\n");
  216. ret = -EINVAL;
  217. goto err_mapped;
  218. }
  219. desc = dmaengine_prep_slave_sg(ssp->dmach,
  220. &dma_xfer[sg_count].sg, 1,
  221. (flags & TXRX_WRITE) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  222. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  223. if (!desc) {
  224. dev_err(ssp->dev,
  225. "Failed to get DMA data write descriptor.\n");
  226. ret = -EINVAL;
  227. goto err_mapped;
  228. }
  229. }
  230. /*
  231. * The last descriptor must have this callback,
  232. * to finish the DMA transaction.
  233. */
  234. desc->callback = mxs_ssp_dma_irq_callback;
  235. desc->callback_param = spi;
  236. /* Start the transfer. */
  237. dmaengine_submit(desc);
  238. dma_async_issue_pending(ssp->dmach);
  239. if (!wait_for_completion_timeout(&spi->c,
  240. msecs_to_jiffies(SSP_TIMEOUT))) {
  241. dev_err(ssp->dev, "DMA transfer timeout\n");
  242. ret = -ETIMEDOUT;
  243. dmaengine_terminate_all(ssp->dmach);
  244. goto err_vmalloc;
  245. }
  246. ret = 0;
  247. err_vmalloc:
  248. while (--sg_count >= 0) {
  249. err_mapped:
  250. dma_unmap_sg(ssp->dev, &dma_xfer[sg_count].sg, 1,
  251. (flags & TXRX_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  252. }
  253. kfree(dma_xfer);
  254. return ret;
  255. }
  256. static int mxs_spi_txrx_pio(struct mxs_spi *spi,
  257. unsigned char *buf, int len,
  258. unsigned int flags)
  259. {
  260. struct mxs_ssp *ssp = &spi->ssp;
  261. writel(BM_SSP_CTRL0_IGNORE_CRC,
  262. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  263. while (len--) {
  264. if (len == 0 && (flags & TXRX_DEASSERT_CS))
  265. writel(BM_SSP_CTRL0_IGNORE_CRC,
  266. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  267. if (ssp->devid == IMX23_SSP) {
  268. writel(BM_SSP_CTRL0_XFER_COUNT,
  269. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  270. writel(1,
  271. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  272. } else {
  273. writel(1, ssp->base + HW_SSP_XFER_SIZE);
  274. }
  275. if (flags & TXRX_WRITE)
  276. writel(BM_SSP_CTRL0_READ,
  277. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  278. else
  279. writel(BM_SSP_CTRL0_READ,
  280. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  281. writel(BM_SSP_CTRL0_RUN,
  282. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  283. if (mxs_ssp_wait(spi, HW_SSP_CTRL0, BM_SSP_CTRL0_RUN, 1))
  284. return -ETIMEDOUT;
  285. if (flags & TXRX_WRITE)
  286. writel(*buf, ssp->base + HW_SSP_DATA(ssp));
  287. writel(BM_SSP_CTRL0_DATA_XFER,
  288. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  289. if (!(flags & TXRX_WRITE)) {
  290. if (mxs_ssp_wait(spi, HW_SSP_STATUS(ssp),
  291. BM_SSP_STATUS_FIFO_EMPTY, 0))
  292. return -ETIMEDOUT;
  293. *buf = (readl(ssp->base + HW_SSP_DATA(ssp)) & 0xff);
  294. }
  295. if (mxs_ssp_wait(spi, HW_SSP_CTRL0, BM_SSP_CTRL0_RUN, 0))
  296. return -ETIMEDOUT;
  297. buf++;
  298. }
  299. if (len <= 0)
  300. return 0;
  301. return -ETIMEDOUT;
  302. }
  303. static int mxs_spi_transfer_one(struct spi_master *master,
  304. struct spi_message *m)
  305. {
  306. struct mxs_spi *spi = spi_master_get_devdata(master);
  307. struct mxs_ssp *ssp = &spi->ssp;
  308. struct spi_transfer *t;
  309. unsigned int flag;
  310. int status = 0;
  311. /* Program CS register bits here, it will be used for all transfers. */
  312. writel(BM_SSP_CTRL0_WAIT_FOR_CMD | BM_SSP_CTRL0_WAIT_FOR_IRQ,
  313. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_CLR);
  314. writel(mxs_spi_cs_to_reg(m->spi->chip_select),
  315. ssp->base + HW_SSP_CTRL0 + STMP_OFFSET_REG_SET);
  316. list_for_each_entry(t, &m->transfers, transfer_list) {
  317. status = mxs_spi_setup_transfer(m->spi, t);
  318. if (status)
  319. break;
  320. /* De-assert on last transfer, inverted by cs_change flag */
  321. flag = (&t->transfer_list == m->transfers.prev) ^ t->cs_change ?
  322. TXRX_DEASSERT_CS : 0;
  323. /*
  324. * Small blocks can be transfered via PIO.
  325. * Measured by empiric means:
  326. *
  327. * dd if=/dev/mtdblock0 of=/dev/null bs=1024k count=1
  328. *
  329. * DMA only: 2.164808 seconds, 473.0KB/s
  330. * Combined: 1.676276 seconds, 610.9KB/s
  331. */
  332. if (t->len < 32) {
  333. writel(BM_SSP_CTRL1_DMA_ENABLE,
  334. ssp->base + HW_SSP_CTRL1(ssp) +
  335. STMP_OFFSET_REG_CLR);
  336. if (t->tx_buf)
  337. status = mxs_spi_txrx_pio(spi,
  338. (void *)t->tx_buf,
  339. t->len, flag | TXRX_WRITE);
  340. if (t->rx_buf)
  341. status = mxs_spi_txrx_pio(spi,
  342. t->rx_buf, t->len,
  343. flag);
  344. } else {
  345. writel(BM_SSP_CTRL1_DMA_ENABLE,
  346. ssp->base + HW_SSP_CTRL1(ssp) +
  347. STMP_OFFSET_REG_SET);
  348. if (t->tx_buf)
  349. status = mxs_spi_txrx_dma(spi,
  350. (void *)t->tx_buf, t->len,
  351. flag | TXRX_WRITE);
  352. if (t->rx_buf)
  353. status = mxs_spi_txrx_dma(spi,
  354. t->rx_buf, t->len,
  355. flag);
  356. }
  357. if (status) {
  358. stmp_reset_block(ssp->base);
  359. break;
  360. }
  361. m->actual_length += t->len;
  362. }
  363. m->status = status;
  364. spi_finalize_current_message(master);
  365. return status;
  366. }
  367. static const struct of_device_id mxs_spi_dt_ids[] = {
  368. { .compatible = "fsl,imx23-spi", .data = (void *) IMX23_SSP, },
  369. { .compatible = "fsl,imx28-spi", .data = (void *) IMX28_SSP, },
  370. { /* sentinel */ }
  371. };
  372. MODULE_DEVICE_TABLE(of, mxs_spi_dt_ids);
  373. static int mxs_spi_probe(struct platform_device *pdev)
  374. {
  375. const struct of_device_id *of_id =
  376. of_match_device(mxs_spi_dt_ids, &pdev->dev);
  377. struct device_node *np = pdev->dev.of_node;
  378. struct spi_master *master;
  379. struct mxs_spi *spi;
  380. struct mxs_ssp *ssp;
  381. struct resource *iores;
  382. struct clk *clk;
  383. void __iomem *base;
  384. int devid, clk_freq;
  385. int ret = 0, irq_err;
  386. /*
  387. * Default clock speed for the SPI core. 160MHz seems to
  388. * work reasonably well with most SPI flashes, so use this
  389. * as a default. Override with "clock-frequency" DT prop.
  390. */
  391. const int clk_freq_default = 160000000;
  392. iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  393. irq_err = platform_get_irq(pdev, 0);
  394. if (irq_err < 0)
  395. return irq_err;
  396. base = devm_ioremap_resource(&pdev->dev, iores);
  397. if (IS_ERR(base))
  398. return PTR_ERR(base);
  399. clk = devm_clk_get(&pdev->dev, NULL);
  400. if (IS_ERR(clk))
  401. return PTR_ERR(clk);
  402. devid = (enum mxs_ssp_id) of_id->data;
  403. ret = of_property_read_u32(np, "clock-frequency",
  404. &clk_freq);
  405. if (ret)
  406. clk_freq = clk_freq_default;
  407. master = spi_alloc_master(&pdev->dev, sizeof(*spi));
  408. if (!master)
  409. return -ENOMEM;
  410. master->transfer_one_message = mxs_spi_transfer_one;
  411. master->bits_per_word_mask = SPI_BPW_MASK(8);
  412. master->mode_bits = SPI_CPOL | SPI_CPHA;
  413. master->num_chipselect = 3;
  414. master->dev.of_node = np;
  415. master->flags = SPI_MASTER_HALF_DUPLEX;
  416. spi = spi_master_get_devdata(master);
  417. ssp = &spi->ssp;
  418. ssp->dev = &pdev->dev;
  419. ssp->clk = clk;
  420. ssp->base = base;
  421. ssp->devid = devid;
  422. init_completion(&spi->c);
  423. ret = devm_request_irq(&pdev->dev, irq_err, mxs_ssp_irq_handler, 0,
  424. dev_name(&pdev->dev), ssp);
  425. if (ret)
  426. goto out_master_free;
  427. ssp->dmach = dma_request_slave_channel(&pdev->dev, "rx-tx");
  428. if (!ssp->dmach) {
  429. dev_err(ssp->dev, "Failed to request DMA\n");
  430. ret = -ENODEV;
  431. goto out_master_free;
  432. }
  433. ret = clk_prepare_enable(ssp->clk);
  434. if (ret)
  435. goto out_dma_release;
  436. clk_set_rate(ssp->clk, clk_freq);
  437. ret = stmp_reset_block(ssp->base);
  438. if (ret)
  439. goto out_disable_clk;
  440. platform_set_drvdata(pdev, master);
  441. ret = devm_spi_register_master(&pdev->dev, master);
  442. if (ret) {
  443. dev_err(&pdev->dev, "Cannot register SPI master, %d\n", ret);
  444. goto out_disable_clk;
  445. }
  446. return 0;
  447. out_disable_clk:
  448. clk_disable_unprepare(ssp->clk);
  449. out_dma_release:
  450. dma_release_channel(ssp->dmach);
  451. out_master_free:
  452. spi_master_put(master);
  453. return ret;
  454. }
  455. static int mxs_spi_remove(struct platform_device *pdev)
  456. {
  457. struct spi_master *master;
  458. struct mxs_spi *spi;
  459. struct mxs_ssp *ssp;
  460. master = platform_get_drvdata(pdev);
  461. spi = spi_master_get_devdata(master);
  462. ssp = &spi->ssp;
  463. clk_disable_unprepare(ssp->clk);
  464. dma_release_channel(ssp->dmach);
  465. return 0;
  466. }
  467. static struct platform_driver mxs_spi_driver = {
  468. .probe = mxs_spi_probe,
  469. .remove = mxs_spi_remove,
  470. .driver = {
  471. .name = DRIVER_NAME,
  472. .of_match_table = mxs_spi_dt_ids,
  473. },
  474. };
  475. module_platform_driver(mxs_spi_driver);
  476. MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  477. MODULE_DESCRIPTION("MXS SPI master driver");
  478. MODULE_LICENSE("GPL");
  479. MODULE_ALIAS("platform:mxs-spi");