spi-dw.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * Designware SPI core controller driver (refer pxa2xx_spi.c)
  3. *
  4. * Copyright (c) 2009, Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. */
  15. #include <linux/dma-mapping.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/highmem.h>
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/gpio.h>
  23. #include "spi-dw.h"
  24. #ifdef CONFIG_DEBUG_FS
  25. #include <linux/debugfs.h>
  26. #endif
  27. /* Slave spi_dev related */
  28. struct chip_data {
  29. u8 cs; /* chip select pin */
  30. u8 tmode; /* TR/TO/RO/EEPROM */
  31. u8 type; /* SPI/SSP/MicroWire */
  32. u8 poll_mode; /* 1 means use poll mode */
  33. u8 enable_dma;
  34. u16 clk_div; /* baud rate divider */
  35. u32 speed_hz; /* baud rate */
  36. void (*cs_control)(u32 command);
  37. };
  38. #ifdef CONFIG_DEBUG_FS
  39. #define SPI_REGS_BUFSIZE 1024
  40. static ssize_t dw_spi_show_regs(struct file *file, char __user *user_buf,
  41. size_t count, loff_t *ppos)
  42. {
  43. struct dw_spi *dws = file->private_data;
  44. char *buf;
  45. u32 len = 0;
  46. ssize_t ret;
  47. buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL);
  48. if (!buf)
  49. return 0;
  50. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  51. "%s registers:\n", dev_name(&dws->master->dev));
  52. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  53. "=================================\n");
  54. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  55. "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0));
  56. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  57. "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1));
  58. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  59. "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR));
  60. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  61. "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER));
  62. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  63. "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR));
  64. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  65. "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR));
  66. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  67. "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR));
  68. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  69. "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR));
  70. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  71. "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR));
  72. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  73. "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR));
  74. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  75. "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR));
  76. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  77. "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR));
  78. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  79. "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR));
  80. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  81. "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR));
  82. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  83. "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR));
  84. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  85. "=================================\n");
  86. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  87. kfree(buf);
  88. return ret;
  89. }
  90. static const struct file_operations dw_spi_regs_ops = {
  91. .owner = THIS_MODULE,
  92. .open = simple_open,
  93. .read = dw_spi_show_regs,
  94. .llseek = default_llseek,
  95. };
  96. static int dw_spi_debugfs_init(struct dw_spi *dws)
  97. {
  98. char name[128];
  99. snprintf(name, 128, "dw_spi-%s", dev_name(&dws->master->dev));
  100. dws->debugfs = debugfs_create_dir(name, NULL);
  101. if (!dws->debugfs)
  102. return -ENOMEM;
  103. debugfs_create_file("registers", S_IFREG | S_IRUGO,
  104. dws->debugfs, (void *)dws, &dw_spi_regs_ops);
  105. return 0;
  106. }
  107. static void dw_spi_debugfs_remove(struct dw_spi *dws)
  108. {
  109. debugfs_remove_recursive(dws->debugfs);
  110. }
  111. #else
  112. static inline int dw_spi_debugfs_init(struct dw_spi *dws)
  113. {
  114. return 0;
  115. }
  116. static inline void dw_spi_debugfs_remove(struct dw_spi *dws)
  117. {
  118. }
  119. #endif /* CONFIG_DEBUG_FS */
  120. static void dw_spi_set_cs(struct spi_device *spi, bool enable)
  121. {
  122. struct dw_spi *dws = spi_master_get_devdata(spi->master);
  123. struct chip_data *chip = spi_get_ctldata(spi);
  124. /* Chip select logic is inverted from spi_set_cs() */
  125. if (chip && chip->cs_control)
  126. chip->cs_control(!enable);
  127. if (!enable)
  128. dw_writel(dws, DW_SPI_SER, BIT(spi->chip_select));
  129. }
  130. /* Return the max entries we can fill into tx fifo */
  131. static inline u32 tx_max(struct dw_spi *dws)
  132. {
  133. u32 tx_left, tx_room, rxtx_gap;
  134. tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
  135. tx_room = dws->fifo_len - dw_readl(dws, DW_SPI_TXFLR);
  136. /*
  137. * Another concern is about the tx/rx mismatch, we
  138. * though to use (dws->fifo_len - rxflr - txflr) as
  139. * one maximum value for tx, but it doesn't cover the
  140. * data which is out of tx/rx fifo and inside the
  141. * shift registers. So a control from sw point of
  142. * view is taken.
  143. */
  144. rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
  145. / dws->n_bytes;
  146. return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
  147. }
  148. /* Return the max entries we should read out of rx fifo */
  149. static inline u32 rx_max(struct dw_spi *dws)
  150. {
  151. u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
  152. return min_t(u32, rx_left, dw_readl(dws, DW_SPI_RXFLR));
  153. }
  154. static void dw_writer(struct dw_spi *dws)
  155. {
  156. u32 max = tx_max(dws);
  157. u16 txw = 0;
  158. while (max--) {
  159. /* Set the tx word if the transfer's original "tx" is not null */
  160. if (dws->tx_end - dws->len) {
  161. if (dws->n_bytes == 1)
  162. txw = *(u8 *)(dws->tx);
  163. else
  164. txw = *(u16 *)(dws->tx);
  165. }
  166. dw_write_io_reg(dws, DW_SPI_DR, txw);
  167. dws->tx += dws->n_bytes;
  168. }
  169. }
  170. static void dw_reader(struct dw_spi *dws)
  171. {
  172. u32 max = rx_max(dws);
  173. u16 rxw;
  174. while (max--) {
  175. rxw = dw_read_io_reg(dws, DW_SPI_DR);
  176. /* Care rx only if the transfer's original "rx" is not null */
  177. if (dws->rx_end - dws->len) {
  178. if (dws->n_bytes == 1)
  179. *(u8 *)(dws->rx) = rxw;
  180. else
  181. *(u16 *)(dws->rx) = rxw;
  182. }
  183. dws->rx += dws->n_bytes;
  184. }
  185. }
  186. static void int_error_stop(struct dw_spi *dws, const char *msg)
  187. {
  188. spi_reset_chip(dws);
  189. dev_err(&dws->master->dev, "%s\n", msg);
  190. dws->master->cur_msg->status = -EIO;
  191. spi_finalize_current_transfer(dws->master);
  192. }
  193. static irqreturn_t interrupt_transfer(struct dw_spi *dws)
  194. {
  195. u16 irq_status = dw_readl(dws, DW_SPI_ISR);
  196. /* Error handling */
  197. if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
  198. dw_readl(dws, DW_SPI_ICR);
  199. int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
  200. return IRQ_HANDLED;
  201. }
  202. dw_reader(dws);
  203. if (dws->rx_end == dws->rx) {
  204. spi_mask_intr(dws, SPI_INT_TXEI);
  205. spi_finalize_current_transfer(dws->master);
  206. return IRQ_HANDLED;
  207. }
  208. if (irq_status & SPI_INT_TXEI) {
  209. spi_mask_intr(dws, SPI_INT_TXEI);
  210. dw_writer(dws);
  211. /* Enable TX irq always, it will be disabled when RX finished */
  212. spi_umask_intr(dws, SPI_INT_TXEI);
  213. }
  214. return IRQ_HANDLED;
  215. }
  216. static irqreturn_t dw_spi_irq(int irq, void *dev_id)
  217. {
  218. struct spi_master *master = dev_id;
  219. struct dw_spi *dws = spi_master_get_devdata(master);
  220. u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
  221. if (!irq_status)
  222. return IRQ_NONE;
  223. if (!master->cur_msg) {
  224. spi_mask_intr(dws, SPI_INT_TXEI);
  225. return IRQ_HANDLED;
  226. }
  227. return dws->transfer_handler(dws);
  228. }
  229. /* Must be called inside pump_transfers() */
  230. static int poll_transfer(struct dw_spi *dws)
  231. {
  232. do {
  233. dw_writer(dws);
  234. dw_reader(dws);
  235. cpu_relax();
  236. } while (dws->rx_end > dws->rx);
  237. return 0;
  238. }
  239. static int dw_spi_transfer_one(struct spi_master *master,
  240. struct spi_device *spi, struct spi_transfer *transfer)
  241. {
  242. struct dw_spi *dws = spi_master_get_devdata(master);
  243. struct chip_data *chip = spi_get_ctldata(spi);
  244. u8 imask = 0;
  245. u16 txlevel = 0;
  246. u16 clk_div;
  247. u32 cr0;
  248. int ret;
  249. dws->dma_mapped = 0;
  250. dws->tx = (void *)transfer->tx_buf;
  251. dws->tx_end = dws->tx + transfer->len;
  252. dws->rx = transfer->rx_buf;
  253. dws->rx_end = dws->rx + transfer->len;
  254. dws->len = transfer->len;
  255. spi_enable_chip(dws, 0);
  256. /* Handle per transfer options for bpw and speed */
  257. if (transfer->speed_hz != chip->speed_hz) {
  258. /* clk_div doesn't support odd number */
  259. clk_div = (dws->max_freq / transfer->speed_hz + 1) & 0xfffe;
  260. chip->speed_hz = transfer->speed_hz;
  261. chip->clk_div = clk_div;
  262. spi_set_clk(dws, chip->clk_div);
  263. }
  264. if (transfer->bits_per_word == 8) {
  265. dws->n_bytes = 1;
  266. dws->dma_width = 1;
  267. } else if (transfer->bits_per_word == 16) {
  268. dws->n_bytes = 2;
  269. dws->dma_width = 2;
  270. } else {
  271. return -EINVAL;
  272. }
  273. /* Default SPI mode is SCPOL = 0, SCPH = 0 */
  274. cr0 = (transfer->bits_per_word - 1)
  275. | (chip->type << SPI_FRF_OFFSET)
  276. | (spi->mode << SPI_MODE_OFFSET)
  277. | (chip->tmode << SPI_TMOD_OFFSET);
  278. /*
  279. * Adjust transfer mode if necessary. Requires platform dependent
  280. * chipselect mechanism.
  281. */
  282. if (chip->cs_control) {
  283. if (dws->rx && dws->tx)
  284. chip->tmode = SPI_TMOD_TR;
  285. else if (dws->rx)
  286. chip->tmode = SPI_TMOD_RO;
  287. else
  288. chip->tmode = SPI_TMOD_TO;
  289. cr0 &= ~SPI_TMOD_MASK;
  290. cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
  291. }
  292. dw_writel(dws, DW_SPI_CTRL0, cr0);
  293. /* Check if current transfer is a DMA transaction */
  294. if (master->can_dma && master->can_dma(master, spi, transfer))
  295. dws->dma_mapped = master->cur_msg_mapped;
  296. /* For poll mode just disable all interrupts */
  297. spi_mask_intr(dws, 0xff);
  298. /*
  299. * Interrupt mode
  300. * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
  301. */
  302. if (dws->dma_mapped) {
  303. ret = dws->dma_ops->dma_setup(dws, transfer);
  304. if (ret < 0) {
  305. spi_enable_chip(dws, 1);
  306. return ret;
  307. }
  308. } else if (!chip->poll_mode) {
  309. txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
  310. dw_writel(dws, DW_SPI_TXFLTR, txlevel);
  311. /* Set the interrupt mask */
  312. imask |= SPI_INT_TXEI | SPI_INT_TXOI |
  313. SPI_INT_RXUI | SPI_INT_RXOI;
  314. spi_umask_intr(dws, imask);
  315. dws->transfer_handler = interrupt_transfer;
  316. }
  317. spi_enable_chip(dws, 1);
  318. if (dws->dma_mapped) {
  319. ret = dws->dma_ops->dma_transfer(dws, transfer);
  320. if (ret < 0)
  321. return ret;
  322. }
  323. if (chip->poll_mode)
  324. return poll_transfer(dws);
  325. return 1;
  326. }
  327. static void dw_spi_handle_err(struct spi_master *master,
  328. struct spi_message *msg)
  329. {
  330. struct dw_spi *dws = spi_master_get_devdata(master);
  331. if (dws->dma_mapped)
  332. dws->dma_ops->dma_stop(dws);
  333. spi_reset_chip(dws);
  334. }
  335. /* This may be called twice for each spi dev */
  336. static int dw_spi_setup(struct spi_device *spi)
  337. {
  338. struct dw_spi_chip *chip_info = NULL;
  339. struct chip_data *chip;
  340. int ret;
  341. /* Only alloc on first setup */
  342. chip = spi_get_ctldata(spi);
  343. if (!chip) {
  344. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  345. if (!chip)
  346. return -ENOMEM;
  347. spi_set_ctldata(spi, chip);
  348. }
  349. /*
  350. * Protocol drivers may change the chip settings, so...
  351. * if chip_info exists, use it
  352. */
  353. chip_info = spi->controller_data;
  354. /* chip_info doesn't always exist */
  355. if (chip_info) {
  356. if (chip_info->cs_control)
  357. chip->cs_control = chip_info->cs_control;
  358. chip->poll_mode = chip_info->poll_mode;
  359. chip->type = chip_info->type;
  360. }
  361. chip->tmode = 0; /* Tx & Rx */
  362. if (gpio_is_valid(spi->cs_gpio)) {
  363. ret = gpio_direction_output(spi->cs_gpio,
  364. !(spi->mode & SPI_CS_HIGH));
  365. if (ret)
  366. return ret;
  367. }
  368. return 0;
  369. }
  370. static void dw_spi_cleanup(struct spi_device *spi)
  371. {
  372. struct chip_data *chip = spi_get_ctldata(spi);
  373. kfree(chip);
  374. spi_set_ctldata(spi, NULL);
  375. }
  376. /* Restart the controller, disable all interrupts, clean rx fifo */
  377. static void spi_hw_init(struct device *dev, struct dw_spi *dws)
  378. {
  379. spi_reset_chip(dws);
  380. /*
  381. * Try to detect the FIFO depth if not set by interface driver,
  382. * the depth could be from 2 to 256 from HW spec
  383. */
  384. if (!dws->fifo_len) {
  385. u32 fifo;
  386. for (fifo = 1; fifo < 256; fifo++) {
  387. dw_writel(dws, DW_SPI_TXFLTR, fifo);
  388. if (fifo != dw_readl(dws, DW_SPI_TXFLTR))
  389. break;
  390. }
  391. dw_writel(dws, DW_SPI_TXFLTR, 0);
  392. dws->fifo_len = (fifo == 1) ? 0 : fifo;
  393. dev_dbg(dev, "Detected FIFO size: %u bytes\n", dws->fifo_len);
  394. }
  395. }
  396. int dw_spi_add_host(struct device *dev, struct dw_spi *dws)
  397. {
  398. struct spi_master *master;
  399. int ret;
  400. BUG_ON(dws == NULL);
  401. master = spi_alloc_master(dev, 0);
  402. if (!master)
  403. return -ENOMEM;
  404. dws->master = master;
  405. dws->type = SSI_MOTO_SPI;
  406. dws->dma_inited = 0;
  407. dws->dma_addr = (dma_addr_t)(dws->paddr + DW_SPI_DR);
  408. snprintf(dws->name, sizeof(dws->name), "dw_spi%d", dws->bus_num);
  409. ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED, dws->name, master);
  410. if (ret < 0) {
  411. dev_err(dev, "can not get IRQ\n");
  412. goto err_free_master;
  413. }
  414. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
  415. master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
  416. master->bus_num = dws->bus_num;
  417. master->num_chipselect = dws->num_cs;
  418. master->setup = dw_spi_setup;
  419. master->cleanup = dw_spi_cleanup;
  420. master->set_cs = dw_spi_set_cs;
  421. master->transfer_one = dw_spi_transfer_one;
  422. master->handle_err = dw_spi_handle_err;
  423. master->max_speed_hz = dws->max_freq;
  424. master->dev.of_node = dev->of_node;
  425. /* Basic HW init */
  426. spi_hw_init(dev, dws);
  427. if (dws->dma_ops && dws->dma_ops->dma_init) {
  428. ret = dws->dma_ops->dma_init(dws);
  429. if (ret) {
  430. dev_warn(dev, "DMA init failed\n");
  431. dws->dma_inited = 0;
  432. } else {
  433. master->can_dma = dws->dma_ops->can_dma;
  434. }
  435. }
  436. spi_master_set_devdata(master, dws);
  437. ret = devm_spi_register_master(dev, master);
  438. if (ret) {
  439. dev_err(&master->dev, "problem registering spi master\n");
  440. goto err_dma_exit;
  441. }
  442. dw_spi_debugfs_init(dws);
  443. return 0;
  444. err_dma_exit:
  445. if (dws->dma_ops && dws->dma_ops->dma_exit)
  446. dws->dma_ops->dma_exit(dws);
  447. spi_enable_chip(dws, 0);
  448. free_irq(dws->irq, master);
  449. err_free_master:
  450. spi_master_put(master);
  451. return ret;
  452. }
  453. EXPORT_SYMBOL_GPL(dw_spi_add_host);
  454. void dw_spi_remove_host(struct dw_spi *dws)
  455. {
  456. dw_spi_debugfs_remove(dws);
  457. if (dws->dma_ops && dws->dma_ops->dma_exit)
  458. dws->dma_ops->dma_exit(dws);
  459. spi_shutdown_chip(dws);
  460. free_irq(dws->irq, dws->master);
  461. }
  462. EXPORT_SYMBOL_GPL(dw_spi_remove_host);
  463. int dw_spi_suspend_host(struct dw_spi *dws)
  464. {
  465. int ret;
  466. ret = spi_master_suspend(dws->master);
  467. if (ret)
  468. return ret;
  469. spi_shutdown_chip(dws);
  470. return 0;
  471. }
  472. EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
  473. int dw_spi_resume_host(struct dw_spi *dws)
  474. {
  475. int ret;
  476. spi_hw_init(&dws->master->dev, dws);
  477. ret = spi_master_resume(dws->master);
  478. if (ret)
  479. dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret);
  480. return ret;
  481. }
  482. EXPORT_SYMBOL_GPL(dw_spi_resume_host);
  483. MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
  484. MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
  485. MODULE_LICENSE("GPL v2");