spi-dw.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. #include <linux/dma-mapping.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/module.h>
  22. #include <linux/highmem.h>
  23. #include <linux/delay.h>
  24. #include <linux/slab.h>
  25. #include <linux/spi/spi.h>
  26. #include "spi-dw.h"
  27. #ifdef CONFIG_DEBUG_FS
  28. #include <linux/debugfs.h>
  29. #endif
  30. #define START_STATE ((void *)0)
  31. #define RUNNING_STATE ((void *)1)
  32. #define DONE_STATE ((void *)2)
  33. #define ERROR_STATE ((void *)-1)
  34. #define QUEUE_RUNNING 0
  35. #define QUEUE_STOPPED 1
  36. #define MRST_SPI_DEASSERT 0
  37. #define MRST_SPI_ASSERT 1
  38. /* Slave spi_dev related */
  39. struct chip_data {
  40. u16 cr0;
  41. u8 cs; /* chip select pin */
  42. u8 n_bytes; /* current is a 1/2/4 byte op */
  43. u8 tmode; /* TR/TO/RO/EEPROM */
  44. u8 type; /* SPI/SSP/MicroWire */
  45. u8 poll_mode; /* 1 means use poll mode */
  46. u32 dma_width;
  47. u32 rx_threshold;
  48. u32 tx_threshold;
  49. u8 enable_dma;
  50. u8 bits_per_word;
  51. u16 clk_div; /* baud rate divider */
  52. u32 speed_hz; /* baud rate */
  53. void (*cs_control)(u32 command);
  54. };
  55. #ifdef CONFIG_DEBUG_FS
  56. #define SPI_REGS_BUFSIZE 1024
  57. static ssize_t spi_show_regs(struct file *file, char __user *user_buf,
  58. size_t count, loff_t *ppos)
  59. {
  60. struct dw_spi *dws;
  61. char *buf;
  62. u32 len = 0;
  63. ssize_t ret;
  64. dws = file->private_data;
  65. buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL);
  66. if (!buf)
  67. return 0;
  68. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  69. "MRST SPI0 registers:\n");
  70. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  71. "=================================\n");
  72. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  73. "CTRL0: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL0));
  74. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  75. "CTRL1: \t\t0x%08x\n", dw_readl(dws, DW_SPI_CTRL1));
  76. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  77. "SSIENR: \t0x%08x\n", dw_readl(dws, DW_SPI_SSIENR));
  78. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  79. "SER: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SER));
  80. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  81. "BAUDR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_BAUDR));
  82. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  83. "TXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_TXFLTR));
  84. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  85. "RXFTLR: \t0x%08x\n", dw_readl(dws, DW_SPI_RXFLTR));
  86. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  87. "TXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_TXFLR));
  88. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  89. "RXFLR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_RXFLR));
  90. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  91. "SR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_SR));
  92. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  93. "IMR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_IMR));
  94. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  95. "ISR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_ISR));
  96. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  97. "DMACR: \t\t0x%08x\n", dw_readl(dws, DW_SPI_DMACR));
  98. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  99. "DMATDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMATDLR));
  100. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  101. "DMARDLR: \t0x%08x\n", dw_readl(dws, DW_SPI_DMARDLR));
  102. len += snprintf(buf + len, SPI_REGS_BUFSIZE - len,
  103. "=================================\n");
  104. ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  105. kfree(buf);
  106. return ret;
  107. }
  108. static const struct file_operations mrst_spi_regs_ops = {
  109. .owner = THIS_MODULE,
  110. .open = simple_open,
  111. .read = spi_show_regs,
  112. .llseek = default_llseek,
  113. };
  114. static int mrst_spi_debugfs_init(struct dw_spi *dws)
  115. {
  116. dws->debugfs = debugfs_create_dir("mrst_spi", NULL);
  117. if (!dws->debugfs)
  118. return -ENOMEM;
  119. debugfs_create_file("registers", S_IFREG | S_IRUGO,
  120. dws->debugfs, (void *)dws, &mrst_spi_regs_ops);
  121. return 0;
  122. }
  123. static void mrst_spi_debugfs_remove(struct dw_spi *dws)
  124. {
  125. if (dws->debugfs)
  126. debugfs_remove_recursive(dws->debugfs);
  127. }
  128. #else
  129. static inline int mrst_spi_debugfs_init(struct dw_spi *dws)
  130. {
  131. return 0;
  132. }
  133. static inline void mrst_spi_debugfs_remove(struct dw_spi *dws)
  134. {
  135. }
  136. #endif /* CONFIG_DEBUG_FS */
  137. /* Return the max entries we can fill into tx fifo */
  138. static inline u32 tx_max(struct dw_spi *dws)
  139. {
  140. u32 tx_left, tx_room, rxtx_gap;
  141. tx_left = (dws->tx_end - dws->tx) / dws->n_bytes;
  142. tx_room = dws->fifo_len - dw_readw(dws, DW_SPI_TXFLR);
  143. /*
  144. * Another concern is about the tx/rx mismatch, we
  145. * though to use (dws->fifo_len - rxflr - txflr) as
  146. * one maximum value for tx, but it doesn't cover the
  147. * data which is out of tx/rx fifo and inside the
  148. * shift registers. So a control from sw point of
  149. * view is taken.
  150. */
  151. rxtx_gap = ((dws->rx_end - dws->rx) - (dws->tx_end - dws->tx))
  152. / dws->n_bytes;
  153. return min3(tx_left, tx_room, (u32) (dws->fifo_len - rxtx_gap));
  154. }
  155. /* Return the max entries we should read out of rx fifo */
  156. static inline u32 rx_max(struct dw_spi *dws)
  157. {
  158. u32 rx_left = (dws->rx_end - dws->rx) / dws->n_bytes;
  159. return min(rx_left, (u32)dw_readw(dws, DW_SPI_RXFLR));
  160. }
  161. static void dw_writer(struct dw_spi *dws)
  162. {
  163. u32 max = tx_max(dws);
  164. u16 txw = 0;
  165. while (max--) {
  166. /* Set the tx word if the transfer's original "tx" is not null */
  167. if (dws->tx_end - dws->len) {
  168. if (dws->n_bytes == 1)
  169. txw = *(u8 *)(dws->tx);
  170. else
  171. txw = *(u16 *)(dws->tx);
  172. }
  173. dw_writew(dws, DW_SPI_DR, txw);
  174. dws->tx += dws->n_bytes;
  175. }
  176. }
  177. static void dw_reader(struct dw_spi *dws)
  178. {
  179. u32 max = rx_max(dws);
  180. u16 rxw;
  181. while (max--) {
  182. rxw = dw_readw(dws, DW_SPI_DR);
  183. /* Care rx only if the transfer's original "rx" is not null */
  184. if (dws->rx_end - dws->len) {
  185. if (dws->n_bytes == 1)
  186. *(u8 *)(dws->rx) = rxw;
  187. else
  188. *(u16 *)(dws->rx) = rxw;
  189. }
  190. dws->rx += dws->n_bytes;
  191. }
  192. }
  193. static void *next_transfer(struct dw_spi *dws)
  194. {
  195. struct spi_message *msg = dws->cur_msg;
  196. struct spi_transfer *trans = dws->cur_transfer;
  197. /* Move to next transfer */
  198. if (trans->transfer_list.next != &msg->transfers) {
  199. dws->cur_transfer =
  200. list_entry(trans->transfer_list.next,
  201. struct spi_transfer,
  202. transfer_list);
  203. return RUNNING_STATE;
  204. } else
  205. return DONE_STATE;
  206. }
  207. /*
  208. * Note: first step is the protocol driver prepares
  209. * a dma-capable memory, and this func just need translate
  210. * the virt addr to physical
  211. */
  212. static int map_dma_buffers(struct dw_spi *dws)
  213. {
  214. if (!dws->cur_msg->is_dma_mapped
  215. || !dws->dma_inited
  216. || !dws->cur_chip->enable_dma
  217. || !dws->dma_ops)
  218. return 0;
  219. if (dws->cur_transfer->tx_dma)
  220. dws->tx_dma = dws->cur_transfer->tx_dma;
  221. if (dws->cur_transfer->rx_dma)
  222. dws->rx_dma = dws->cur_transfer->rx_dma;
  223. return 1;
  224. }
  225. /* Caller already set message->status; dma and pio irqs are blocked */
  226. static void giveback(struct dw_spi *dws)
  227. {
  228. struct spi_transfer *last_transfer;
  229. unsigned long flags;
  230. struct spi_message *msg;
  231. spin_lock_irqsave(&dws->lock, flags);
  232. msg = dws->cur_msg;
  233. dws->cur_msg = NULL;
  234. dws->cur_transfer = NULL;
  235. dws->prev_chip = dws->cur_chip;
  236. dws->cur_chip = NULL;
  237. dws->dma_mapped = 0;
  238. queue_work(dws->workqueue, &dws->pump_messages);
  239. spin_unlock_irqrestore(&dws->lock, flags);
  240. last_transfer = list_entry(msg->transfers.prev,
  241. struct spi_transfer,
  242. transfer_list);
  243. if (!last_transfer->cs_change && dws->cs_control)
  244. dws->cs_control(MRST_SPI_DEASSERT);
  245. msg->state = NULL;
  246. if (msg->complete)
  247. msg->complete(msg->context);
  248. }
  249. static void int_error_stop(struct dw_spi *dws, const char *msg)
  250. {
  251. /* Stop the hw */
  252. spi_enable_chip(dws, 0);
  253. dev_err(&dws->master->dev, "%s\n", msg);
  254. dws->cur_msg->state = ERROR_STATE;
  255. tasklet_schedule(&dws->pump_transfers);
  256. }
  257. void dw_spi_xfer_done(struct dw_spi *dws)
  258. {
  259. /* Update total byte transferred return count actual bytes read */
  260. dws->cur_msg->actual_length += dws->len;
  261. /* Move to next transfer */
  262. dws->cur_msg->state = next_transfer(dws);
  263. /* Handle end of message */
  264. if (dws->cur_msg->state == DONE_STATE) {
  265. dws->cur_msg->status = 0;
  266. giveback(dws);
  267. } else
  268. tasklet_schedule(&dws->pump_transfers);
  269. }
  270. EXPORT_SYMBOL_GPL(dw_spi_xfer_done);
  271. static irqreturn_t interrupt_transfer(struct dw_spi *dws)
  272. {
  273. u16 irq_status = dw_readw(dws, DW_SPI_ISR);
  274. /* Error handling */
  275. if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
  276. dw_readw(dws, DW_SPI_TXOICR);
  277. dw_readw(dws, DW_SPI_RXOICR);
  278. dw_readw(dws, DW_SPI_RXUICR);
  279. int_error_stop(dws, "interrupt_transfer: fifo overrun/underrun");
  280. return IRQ_HANDLED;
  281. }
  282. dw_reader(dws);
  283. if (dws->rx_end == dws->rx) {
  284. spi_mask_intr(dws, SPI_INT_TXEI);
  285. dw_spi_xfer_done(dws);
  286. return IRQ_HANDLED;
  287. }
  288. if (irq_status & SPI_INT_TXEI) {
  289. spi_mask_intr(dws, SPI_INT_TXEI);
  290. dw_writer(dws);
  291. /* Enable TX irq always, it will be disabled when RX finished */
  292. spi_umask_intr(dws, SPI_INT_TXEI);
  293. }
  294. return IRQ_HANDLED;
  295. }
  296. static irqreturn_t dw_spi_irq(int irq, void *dev_id)
  297. {
  298. struct dw_spi *dws = dev_id;
  299. u16 irq_status = dw_readw(dws, DW_SPI_ISR) & 0x3f;
  300. if (!irq_status)
  301. return IRQ_NONE;
  302. if (!dws->cur_msg) {
  303. spi_mask_intr(dws, SPI_INT_TXEI);
  304. return IRQ_HANDLED;
  305. }
  306. return dws->transfer_handler(dws);
  307. }
  308. /* Must be called inside pump_transfers() */
  309. static void poll_transfer(struct dw_spi *dws)
  310. {
  311. do {
  312. dw_writer(dws);
  313. dw_reader(dws);
  314. cpu_relax();
  315. } while (dws->rx_end > dws->rx);
  316. dw_spi_xfer_done(dws);
  317. }
  318. static void pump_transfers(unsigned long data)
  319. {
  320. struct dw_spi *dws = (struct dw_spi *)data;
  321. struct spi_message *message = NULL;
  322. struct spi_transfer *transfer = NULL;
  323. struct spi_transfer *previous = NULL;
  324. struct spi_device *spi = NULL;
  325. struct chip_data *chip = NULL;
  326. u8 bits = 0;
  327. u8 imask = 0;
  328. u8 cs_change = 0;
  329. u16 txint_level = 0;
  330. u16 clk_div = 0;
  331. u32 speed = 0;
  332. u32 cr0 = 0;
  333. /* Get current state information */
  334. message = dws->cur_msg;
  335. transfer = dws->cur_transfer;
  336. chip = dws->cur_chip;
  337. spi = message->spi;
  338. if (message->state == ERROR_STATE) {
  339. message->status = -EIO;
  340. goto early_exit;
  341. }
  342. /* Handle end of message */
  343. if (message->state == DONE_STATE) {
  344. message->status = 0;
  345. goto early_exit;
  346. }
  347. /* Delay if requested at end of transfer*/
  348. if (message->state == RUNNING_STATE) {
  349. previous = list_entry(transfer->transfer_list.prev,
  350. struct spi_transfer,
  351. transfer_list);
  352. if (previous->delay_usecs)
  353. udelay(previous->delay_usecs);
  354. }
  355. dws->n_bytes = chip->n_bytes;
  356. dws->dma_width = chip->dma_width;
  357. dws->cs_control = chip->cs_control;
  358. dws->rx_dma = transfer->rx_dma;
  359. dws->tx_dma = transfer->tx_dma;
  360. dws->tx = (void *)transfer->tx_buf;
  361. dws->tx_end = dws->tx + transfer->len;
  362. dws->rx = transfer->rx_buf;
  363. dws->rx_end = dws->rx + transfer->len;
  364. dws->cs_change = transfer->cs_change;
  365. dws->len = dws->cur_transfer->len;
  366. if (chip != dws->prev_chip)
  367. cs_change = 1;
  368. cr0 = chip->cr0;
  369. /* Handle per transfer options for bpw and speed */
  370. if (transfer->speed_hz) {
  371. speed = chip->speed_hz;
  372. if ((transfer->speed_hz != speed) || (!chip->clk_div)) {
  373. speed = transfer->speed_hz;
  374. if (speed > dws->max_freq) {
  375. printk(KERN_ERR "MRST SPI0: unsupported"
  376. "freq: %dHz\n", speed);
  377. message->status = -EIO;
  378. goto early_exit;
  379. }
  380. /* clk_div doesn't support odd number */
  381. clk_div = dws->max_freq / speed;
  382. clk_div = (clk_div + 1) & 0xfffe;
  383. chip->speed_hz = speed;
  384. chip->clk_div = clk_div;
  385. }
  386. }
  387. if (transfer->bits_per_word) {
  388. bits = transfer->bits_per_word;
  389. switch (bits) {
  390. case 8:
  391. case 16:
  392. dws->n_bytes = dws->dma_width = bits >> 3;
  393. break;
  394. default:
  395. printk(KERN_ERR "MRST SPI0: unsupported bits:"
  396. "%db\n", bits);
  397. message->status = -EIO;
  398. goto early_exit;
  399. }
  400. cr0 = (bits - 1)
  401. | (chip->type << SPI_FRF_OFFSET)
  402. | (spi->mode << SPI_MODE_OFFSET)
  403. | (chip->tmode << SPI_TMOD_OFFSET);
  404. }
  405. message->state = RUNNING_STATE;
  406. /*
  407. * Adjust transfer mode if necessary. Requires platform dependent
  408. * chipselect mechanism.
  409. */
  410. if (dws->cs_control) {
  411. if (dws->rx && dws->tx)
  412. chip->tmode = SPI_TMOD_TR;
  413. else if (dws->rx)
  414. chip->tmode = SPI_TMOD_RO;
  415. else
  416. chip->tmode = SPI_TMOD_TO;
  417. cr0 &= ~SPI_TMOD_MASK;
  418. cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
  419. }
  420. /* Check if current transfer is a DMA transaction */
  421. dws->dma_mapped = map_dma_buffers(dws);
  422. /*
  423. * Interrupt mode
  424. * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
  425. */
  426. if (!dws->dma_mapped && !chip->poll_mode) {
  427. int templen = dws->len / dws->n_bytes;
  428. txint_level = dws->fifo_len / 2;
  429. txint_level = (templen > txint_level) ? txint_level : templen;
  430. imask |= SPI_INT_TXEI | SPI_INT_TXOI | SPI_INT_RXUI | SPI_INT_RXOI;
  431. dws->transfer_handler = interrupt_transfer;
  432. }
  433. /*
  434. * Reprogram registers only if
  435. * 1. chip select changes
  436. * 2. clk_div is changed
  437. * 3. control value changes
  438. */
  439. if (dw_readw(dws, DW_SPI_CTRL0) != cr0 || cs_change || clk_div || imask) {
  440. spi_enable_chip(dws, 0);
  441. if (dw_readw(dws, DW_SPI_CTRL0) != cr0)
  442. dw_writew(dws, DW_SPI_CTRL0, cr0);
  443. spi_set_clk(dws, clk_div ? clk_div : chip->clk_div);
  444. spi_chip_sel(dws, spi->chip_select);
  445. /* Set the interrupt mask, for poll mode just disable all int */
  446. spi_mask_intr(dws, 0xff);
  447. if (imask)
  448. spi_umask_intr(dws, imask);
  449. if (txint_level)
  450. dw_writew(dws, DW_SPI_TXFLTR, txint_level);
  451. spi_enable_chip(dws, 1);
  452. if (cs_change)
  453. dws->prev_chip = chip;
  454. }
  455. if (dws->dma_mapped)
  456. dws->dma_ops->dma_transfer(dws, cs_change);
  457. if (chip->poll_mode)
  458. poll_transfer(dws);
  459. return;
  460. early_exit:
  461. giveback(dws);
  462. return;
  463. }
  464. static void pump_messages(struct work_struct *work)
  465. {
  466. struct dw_spi *dws =
  467. container_of(work, struct dw_spi, pump_messages);
  468. unsigned long flags;
  469. /* Lock queue and check for queue work */
  470. spin_lock_irqsave(&dws->lock, flags);
  471. if (list_empty(&dws->queue) || dws->run == QUEUE_STOPPED) {
  472. dws->busy = 0;
  473. spin_unlock_irqrestore(&dws->lock, flags);
  474. return;
  475. }
  476. /* Make sure we are not already running a message */
  477. if (dws->cur_msg) {
  478. spin_unlock_irqrestore(&dws->lock, flags);
  479. return;
  480. }
  481. /* Extract head of queue */
  482. dws->cur_msg = list_entry(dws->queue.next, struct spi_message, queue);
  483. list_del_init(&dws->cur_msg->queue);
  484. /* Initial message state*/
  485. dws->cur_msg->state = START_STATE;
  486. dws->cur_transfer = list_entry(dws->cur_msg->transfers.next,
  487. struct spi_transfer,
  488. transfer_list);
  489. dws->cur_chip = spi_get_ctldata(dws->cur_msg->spi);
  490. /* Mark as busy and launch transfers */
  491. tasklet_schedule(&dws->pump_transfers);
  492. dws->busy = 1;
  493. spin_unlock_irqrestore(&dws->lock, flags);
  494. }
  495. /* spi_device use this to queue in their spi_msg */
  496. static int dw_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  497. {
  498. struct dw_spi *dws = spi_master_get_devdata(spi->master);
  499. unsigned long flags;
  500. spin_lock_irqsave(&dws->lock, flags);
  501. if (dws->run == QUEUE_STOPPED) {
  502. spin_unlock_irqrestore(&dws->lock, flags);
  503. return -ESHUTDOWN;
  504. }
  505. msg->actual_length = 0;
  506. msg->status = -EINPROGRESS;
  507. msg->state = START_STATE;
  508. list_add_tail(&msg->queue, &dws->queue);
  509. if (dws->run == QUEUE_RUNNING && !dws->busy) {
  510. if (dws->cur_transfer || dws->cur_msg)
  511. queue_work(dws->workqueue,
  512. &dws->pump_messages);
  513. else {
  514. /* If no other data transaction in air, just go */
  515. spin_unlock_irqrestore(&dws->lock, flags);
  516. pump_messages(&dws->pump_messages);
  517. return 0;
  518. }
  519. }
  520. spin_unlock_irqrestore(&dws->lock, flags);
  521. return 0;
  522. }
  523. /* This may be called twice for each spi dev */
  524. static int dw_spi_setup(struct spi_device *spi)
  525. {
  526. struct dw_spi_chip *chip_info = NULL;
  527. struct chip_data *chip;
  528. if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
  529. return -EINVAL;
  530. /* Only alloc on first setup */
  531. chip = spi_get_ctldata(spi);
  532. if (!chip) {
  533. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  534. if (!chip)
  535. return -ENOMEM;
  536. }
  537. /*
  538. * Protocol drivers may change the chip settings, so...
  539. * if chip_info exists, use it
  540. */
  541. chip_info = spi->controller_data;
  542. /* chip_info doesn't always exist */
  543. if (chip_info) {
  544. if (chip_info->cs_control)
  545. chip->cs_control = chip_info->cs_control;
  546. chip->poll_mode = chip_info->poll_mode;
  547. chip->type = chip_info->type;
  548. chip->rx_threshold = 0;
  549. chip->tx_threshold = 0;
  550. chip->enable_dma = chip_info->enable_dma;
  551. }
  552. if (spi->bits_per_word <= 8) {
  553. chip->n_bytes = 1;
  554. chip->dma_width = 1;
  555. } else if (spi->bits_per_word <= 16) {
  556. chip->n_bytes = 2;
  557. chip->dma_width = 2;
  558. } else {
  559. /* Never take >16b case for MRST SPIC */
  560. dev_err(&spi->dev, "invalid wordsize\n");
  561. return -EINVAL;
  562. }
  563. chip->bits_per_word = spi->bits_per_word;
  564. if (!spi->max_speed_hz) {
  565. dev_err(&spi->dev, "No max speed HZ parameter\n");
  566. return -EINVAL;
  567. }
  568. chip->tmode = 0; /* Tx & Rx */
  569. /* Default SPI mode is SCPOL = 0, SCPH = 0 */
  570. chip->cr0 = (chip->bits_per_word - 1)
  571. | (chip->type << SPI_FRF_OFFSET)
  572. | (spi->mode << SPI_MODE_OFFSET)
  573. | (chip->tmode << SPI_TMOD_OFFSET);
  574. spi_set_ctldata(spi, chip);
  575. return 0;
  576. }
  577. static void dw_spi_cleanup(struct spi_device *spi)
  578. {
  579. struct chip_data *chip = spi_get_ctldata(spi);
  580. kfree(chip);
  581. }
  582. static int __devinit init_queue(struct dw_spi *dws)
  583. {
  584. INIT_LIST_HEAD(&dws->queue);
  585. spin_lock_init(&dws->lock);
  586. dws->run = QUEUE_STOPPED;
  587. dws->busy = 0;
  588. tasklet_init(&dws->pump_transfers,
  589. pump_transfers, (unsigned long)dws);
  590. INIT_WORK(&dws->pump_messages, pump_messages);
  591. dws->workqueue = create_singlethread_workqueue(
  592. dev_name(dws->master->dev.parent));
  593. if (dws->workqueue == NULL)
  594. return -EBUSY;
  595. return 0;
  596. }
  597. static int start_queue(struct dw_spi *dws)
  598. {
  599. unsigned long flags;
  600. spin_lock_irqsave(&dws->lock, flags);
  601. if (dws->run == QUEUE_RUNNING || dws->busy) {
  602. spin_unlock_irqrestore(&dws->lock, flags);
  603. return -EBUSY;
  604. }
  605. dws->run = QUEUE_RUNNING;
  606. dws->cur_msg = NULL;
  607. dws->cur_transfer = NULL;
  608. dws->cur_chip = NULL;
  609. dws->prev_chip = NULL;
  610. spin_unlock_irqrestore(&dws->lock, flags);
  611. queue_work(dws->workqueue, &dws->pump_messages);
  612. return 0;
  613. }
  614. static int stop_queue(struct dw_spi *dws)
  615. {
  616. unsigned long flags;
  617. unsigned limit = 50;
  618. int status = 0;
  619. spin_lock_irqsave(&dws->lock, flags);
  620. dws->run = QUEUE_STOPPED;
  621. while ((!list_empty(&dws->queue) || dws->busy) && limit--) {
  622. spin_unlock_irqrestore(&dws->lock, flags);
  623. msleep(10);
  624. spin_lock_irqsave(&dws->lock, flags);
  625. }
  626. if (!list_empty(&dws->queue) || dws->busy)
  627. status = -EBUSY;
  628. spin_unlock_irqrestore(&dws->lock, flags);
  629. return status;
  630. }
  631. static int destroy_queue(struct dw_spi *dws)
  632. {
  633. int status;
  634. status = stop_queue(dws);
  635. if (status != 0)
  636. return status;
  637. destroy_workqueue(dws->workqueue);
  638. return 0;
  639. }
  640. /* Restart the controller, disable all interrupts, clean rx fifo */
  641. static void spi_hw_init(struct dw_spi *dws)
  642. {
  643. spi_enable_chip(dws, 0);
  644. spi_mask_intr(dws, 0xff);
  645. spi_enable_chip(dws, 1);
  646. /*
  647. * Try to detect the FIFO depth if not set by interface driver,
  648. * the depth could be from 2 to 256 from HW spec
  649. */
  650. if (!dws->fifo_len) {
  651. u32 fifo;
  652. for (fifo = 1; fifo < 256; fifo++) {
  653. dw_writew(dws, DW_SPI_TXFLTR, fifo);
  654. if (fifo != dw_readw(dws, DW_SPI_TXFLTR))
  655. break;
  656. }
  657. dws->fifo_len = (fifo == 1) ? 0 : fifo;
  658. dw_writew(dws, DW_SPI_TXFLTR, 0);
  659. }
  660. }
  661. int __devinit dw_spi_add_host(struct dw_spi *dws)
  662. {
  663. struct spi_master *master;
  664. int ret;
  665. BUG_ON(dws == NULL);
  666. master = spi_alloc_master(dws->parent_dev, 0);
  667. if (!master) {
  668. ret = -ENOMEM;
  669. goto exit;
  670. }
  671. dws->master = master;
  672. dws->type = SSI_MOTO_SPI;
  673. dws->prev_chip = NULL;
  674. dws->dma_inited = 0;
  675. dws->dma_addr = (dma_addr_t)(dws->paddr + 0x60);
  676. snprintf(dws->name, sizeof(dws->name), "dw_spi%d",
  677. dws->bus_num);
  678. ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED,
  679. dws->name, dws);
  680. if (ret < 0) {
  681. dev_err(&master->dev, "can not get IRQ\n");
  682. goto err_free_master;
  683. }
  684. master->mode_bits = SPI_CPOL | SPI_CPHA;
  685. master->bus_num = dws->bus_num;
  686. master->num_chipselect = dws->num_cs;
  687. master->cleanup = dw_spi_cleanup;
  688. master->setup = dw_spi_setup;
  689. master->transfer = dw_spi_transfer;
  690. /* Basic HW init */
  691. spi_hw_init(dws);
  692. if (dws->dma_ops && dws->dma_ops->dma_init) {
  693. ret = dws->dma_ops->dma_init(dws);
  694. if (ret) {
  695. dev_warn(&master->dev, "DMA init failed\n");
  696. dws->dma_inited = 0;
  697. }
  698. }
  699. /* Initial and start queue */
  700. ret = init_queue(dws);
  701. if (ret) {
  702. dev_err(&master->dev, "problem initializing queue\n");
  703. goto err_diable_hw;
  704. }
  705. ret = start_queue(dws);
  706. if (ret) {
  707. dev_err(&master->dev, "problem starting queue\n");
  708. goto err_diable_hw;
  709. }
  710. spi_master_set_devdata(master, dws);
  711. ret = spi_register_master(master);
  712. if (ret) {
  713. dev_err(&master->dev, "problem registering spi master\n");
  714. goto err_queue_alloc;
  715. }
  716. mrst_spi_debugfs_init(dws);
  717. return 0;
  718. err_queue_alloc:
  719. destroy_queue(dws);
  720. if (dws->dma_ops && dws->dma_ops->dma_exit)
  721. dws->dma_ops->dma_exit(dws);
  722. err_diable_hw:
  723. spi_enable_chip(dws, 0);
  724. free_irq(dws->irq, dws);
  725. err_free_master:
  726. spi_master_put(master);
  727. exit:
  728. return ret;
  729. }
  730. EXPORT_SYMBOL_GPL(dw_spi_add_host);
  731. void __devexit dw_spi_remove_host(struct dw_spi *dws)
  732. {
  733. int status = 0;
  734. if (!dws)
  735. return;
  736. mrst_spi_debugfs_remove(dws);
  737. /* Remove the queue */
  738. status = destroy_queue(dws);
  739. if (status != 0)
  740. dev_err(&dws->master->dev, "dw_spi_remove: workqueue will not "
  741. "complete, message memory not freed\n");
  742. if (dws->dma_ops && dws->dma_ops->dma_exit)
  743. dws->dma_ops->dma_exit(dws);
  744. spi_enable_chip(dws, 0);
  745. /* Disable clk */
  746. spi_set_clk(dws, 0);
  747. free_irq(dws->irq, dws);
  748. /* Disconnect from the SPI framework */
  749. spi_unregister_master(dws->master);
  750. }
  751. EXPORT_SYMBOL_GPL(dw_spi_remove_host);
  752. int dw_spi_suspend_host(struct dw_spi *dws)
  753. {
  754. int ret = 0;
  755. ret = stop_queue(dws);
  756. if (ret)
  757. return ret;
  758. spi_enable_chip(dws, 0);
  759. spi_set_clk(dws, 0);
  760. return ret;
  761. }
  762. EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
  763. int dw_spi_resume_host(struct dw_spi *dws)
  764. {
  765. int ret;
  766. spi_hw_init(dws);
  767. ret = start_queue(dws);
  768. if (ret)
  769. dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret);
  770. return ret;
  771. }
  772. EXPORT_SYMBOL_GPL(dw_spi_resume_host);
  773. MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
  774. MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
  775. MODULE_LICENSE("GPL v2");