spi-dw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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[32];
  99. snprintf(name, 32, "dw_spi%d", dws->master->bus_num);
  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;
  157. u16 txw = 0;
  158. spin_lock(&dws->buf_lock);
  159. max = tx_max(dws);
  160. while (max--) {
  161. /* Set the tx word if the transfer's original "tx" is not null */
  162. if (dws->tx_end - dws->len) {
  163. if (dws->n_bytes == 1)
  164. txw = *(u8 *)(dws->tx);
  165. else
  166. txw = *(u16 *)(dws->tx);
  167. }
  168. dw_write_io_reg(dws, DW_SPI_DR, txw);
  169. dws->tx += dws->n_bytes;
  170. }
  171. spin_unlock(&dws->buf_lock);
  172. }
  173. static void dw_reader(struct dw_spi *dws)
  174. {
  175. u32 max;
  176. u16 rxw;
  177. spin_lock(&dws->buf_lock);
  178. max = rx_max(dws);
  179. while (max--) {
  180. rxw = dw_read_io_reg(dws, DW_SPI_DR);
  181. /* Care rx only if the transfer's original "rx" is not null */
  182. if (dws->rx_end - dws->len) {
  183. if (dws->n_bytes == 1)
  184. *(u8 *)(dws->rx) = rxw;
  185. else
  186. *(u16 *)(dws->rx) = rxw;
  187. }
  188. dws->rx += dws->n_bytes;
  189. }
  190. spin_unlock(&dws->buf_lock);
  191. }
  192. static void int_error_stop(struct dw_spi *dws, const char *msg)
  193. {
  194. spi_reset_chip(dws);
  195. dev_err(&dws->master->dev, "%s\n", msg);
  196. dws->master->cur_msg->status = -EIO;
  197. spi_finalize_current_transfer(dws->master);
  198. }
  199. static irqreturn_t interrupt_transfer(struct dw_spi *dws)
  200. {
  201. u16 irq_status = dw_readl(dws, DW_SPI_ISR);
  202. /* Error handling */
  203. if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
  204. dw_readl(dws, DW_SPI_ICR);
  205. int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
  206. return IRQ_HANDLED;
  207. }
  208. dw_reader(dws);
  209. if (dws->rx_end == dws->rx) {
  210. spi_mask_intr(dws, SPI_INT_TXEI);
  211. spi_finalize_current_transfer(dws->master);
  212. return IRQ_HANDLED;
  213. }
  214. if (irq_status & SPI_INT_TXEI) {
  215. spi_mask_intr(dws, SPI_INT_TXEI);
  216. dw_writer(dws);
  217. /* Enable TX irq always, it will be disabled when RX finished */
  218. spi_umask_intr(dws, SPI_INT_TXEI);
  219. }
  220. return IRQ_HANDLED;
  221. }
  222. static irqreturn_t dw_spi_irq(int irq, void *dev_id)
  223. {
  224. struct spi_master *master = dev_id;
  225. struct dw_spi *dws = spi_master_get_devdata(master);
  226. u16 irq_status = dw_readl(dws, DW_SPI_ISR) & 0x3f;
  227. if (!irq_status)
  228. return IRQ_NONE;
  229. if (!master->cur_msg) {
  230. spi_mask_intr(dws, SPI_INT_TXEI);
  231. return IRQ_HANDLED;
  232. }
  233. return dws->transfer_handler(dws);
  234. }
  235. /* Must be called inside pump_transfers() */
  236. static int poll_transfer(struct dw_spi *dws)
  237. {
  238. do {
  239. dw_writer(dws);
  240. dw_reader(dws);
  241. cpu_relax();
  242. } while (dws->rx_end > dws->rx);
  243. return 0;
  244. }
  245. static int dw_spi_transfer_one(struct spi_master *master,
  246. struct spi_device *spi, struct spi_transfer *transfer)
  247. {
  248. struct dw_spi *dws = spi_master_get_devdata(master);
  249. struct chip_data *chip = spi_get_ctldata(spi);
  250. unsigned long flags;
  251. u8 imask = 0;
  252. u16 txlevel = 0;
  253. u32 cr0;
  254. int ret;
  255. dws->dma_mapped = 0;
  256. spin_lock_irqsave(&dws->buf_lock, flags);
  257. dws->tx = (void *)transfer->tx_buf;
  258. dws->tx_end = dws->tx + transfer->len;
  259. dws->rx = transfer->rx_buf;
  260. dws->rx_end = dws->rx + transfer->len;
  261. dws->len = transfer->len;
  262. spin_unlock_irqrestore(&dws->buf_lock, flags);
  263. /* Ensure dw->rx and dw->rx_end are visible */
  264. smp_mb();
  265. spi_enable_chip(dws, 0);
  266. /* Handle per transfer options for bpw and speed */
  267. if (transfer->speed_hz != dws->current_freq) {
  268. if (transfer->speed_hz != chip->speed_hz) {
  269. /* clk_div doesn't support odd number */
  270. chip->clk_div = (DIV_ROUND_UP(dws->max_freq, transfer->speed_hz) + 1) & 0xfffe;
  271. chip->speed_hz = transfer->speed_hz;
  272. }
  273. dws->current_freq = transfer->speed_hz;
  274. spi_set_clk(dws, chip->clk_div);
  275. }
  276. if (transfer->bits_per_word == 8) {
  277. dws->n_bytes = 1;
  278. dws->dma_width = 1;
  279. } else if (transfer->bits_per_word == 16) {
  280. dws->n_bytes = 2;
  281. dws->dma_width = 2;
  282. } else {
  283. return -EINVAL;
  284. }
  285. /* Default SPI mode is SCPOL = 0, SCPH = 0 */
  286. cr0 = (transfer->bits_per_word - 1)
  287. | (chip->type << SPI_FRF_OFFSET)
  288. | (spi->mode << SPI_MODE_OFFSET)
  289. | (chip->tmode << SPI_TMOD_OFFSET);
  290. /*
  291. * Adjust transfer mode if necessary. Requires platform dependent
  292. * chipselect mechanism.
  293. */
  294. if (chip->cs_control) {
  295. if (dws->rx && dws->tx)
  296. chip->tmode = SPI_TMOD_TR;
  297. else if (dws->rx)
  298. chip->tmode = SPI_TMOD_RO;
  299. else
  300. chip->tmode = SPI_TMOD_TO;
  301. cr0 &= ~SPI_TMOD_MASK;
  302. cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
  303. }
  304. dw_writel(dws, DW_SPI_CTRL0, cr0);
  305. /* Check if current transfer is a DMA transaction */
  306. if (master->can_dma && master->can_dma(master, spi, transfer))
  307. dws->dma_mapped = master->cur_msg_mapped;
  308. /* For poll mode just disable all interrupts */
  309. spi_mask_intr(dws, 0xff);
  310. /*
  311. * Interrupt mode
  312. * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
  313. */
  314. if (dws->dma_mapped) {
  315. ret = dws->dma_ops->dma_setup(dws, transfer);
  316. if (ret < 0) {
  317. spi_enable_chip(dws, 1);
  318. return ret;
  319. }
  320. } else if (!chip->poll_mode) {
  321. txlevel = min_t(u16, dws->fifo_len / 2, dws->len / dws->n_bytes);
  322. dw_writel(dws, DW_SPI_TXFLTR, txlevel);
  323. /* Set the interrupt mask */
  324. imask |= SPI_INT_TXEI | SPI_INT_TXOI |
  325. SPI_INT_RXUI | SPI_INT_RXOI;
  326. spi_umask_intr(dws, imask);
  327. dws->transfer_handler = interrupt_transfer;
  328. }
  329. spi_enable_chip(dws, 1);
  330. if (dws->dma_mapped)
  331. return dws->dma_ops->dma_transfer(dws, transfer);
  332. if (chip->poll_mode)
  333. return poll_transfer(dws);
  334. return 1;
  335. }
  336. static void dw_spi_handle_err(struct spi_master *master,
  337. struct spi_message *msg)
  338. {
  339. struct dw_spi *dws = spi_master_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_master *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_master_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. /* Basic HW init */
  438. spi_hw_init(dev, dws);
  439. if (dws->dma_ops && dws->dma_ops->dma_init) {
  440. ret = dws->dma_ops->dma_init(dws);
  441. if (ret) {
  442. dev_warn(dev, "DMA init failed\n");
  443. dws->dma_inited = 0;
  444. } else {
  445. master->can_dma = dws->dma_ops->can_dma;
  446. master->flags |= SPI_CONTROLLER_MUST_TX;
  447. }
  448. }
  449. ret = spi_register_master(master);
  450. if (ret) {
  451. dev_err(&master->dev, "problem registering spi master\n");
  452. goto err_dma_exit;
  453. }
  454. dw_spi_debugfs_init(dws);
  455. return 0;
  456. err_dma_exit:
  457. if (dws->dma_ops && dws->dma_ops->dma_exit)
  458. dws->dma_ops->dma_exit(dws);
  459. spi_enable_chip(dws, 0);
  460. free_irq(dws->irq, master);
  461. err_free_master:
  462. spi_master_put(master);
  463. return ret;
  464. }
  465. EXPORT_SYMBOL_GPL(dw_spi_add_host);
  466. void dw_spi_remove_host(struct dw_spi *dws)
  467. {
  468. dw_spi_debugfs_remove(dws);
  469. spi_unregister_master(dws->master);
  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_master_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_master_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");