dw_spi.c 22 KB

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