spi-mxs.c 14 KB

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