spi-dw.c 15 KB

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