dw_spi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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 wait_till_not_busy(struct dw_spi *dws)
  166. {
  167. unsigned long end = jiffies + 1 + usecs_to_jiffies(5000);
  168. while (time_before(jiffies, end)) {
  169. if (!(dw_readw(dws, sr) & SR_BUSY))
  170. return;
  171. cpu_relax();
  172. }
  173. dev_err(&dws->master->dev,
  174. "DW SPI: Status keeps busy for 5000us after a read/write!\n");
  175. }
  176. static int dw_writer(struct dw_spi *dws)
  177. {
  178. u32 max = tx_max(dws);
  179. u16 txw = 0;
  180. while (max--) {
  181. /* Set the tx word if the transfer's original "tx" is not null */
  182. if (dws->tx_end - dws->len) {
  183. if (dws->n_bytes == 1)
  184. txw = *(u8 *)(dws->tx);
  185. else
  186. txw = *(u16 *)(dws->tx);
  187. }
  188. dw_writew(dws, dr, txw);
  189. dws->tx += dws->n_bytes;
  190. }
  191. return 1;
  192. }
  193. static int dw_reader(struct dw_spi *dws)
  194. {
  195. u32 max = rx_max(dws);
  196. u16 rxw;
  197. while (max--) {
  198. rxw = dw_readw(dws, dr);
  199. /* Care rx only if the transfer's original "rx" is not null */
  200. if (dws->rx_end - dws->len) {
  201. if (dws->n_bytes == 1)
  202. *(u8 *)(dws->rx) = rxw;
  203. else
  204. *(u16 *)(dws->rx) = rxw;
  205. }
  206. dws->rx += dws->n_bytes;
  207. }
  208. return dws->rx == dws->rx_end;
  209. }
  210. static void *next_transfer(struct dw_spi *dws)
  211. {
  212. struct spi_message *msg = dws->cur_msg;
  213. struct spi_transfer *trans = dws->cur_transfer;
  214. /* Move to next transfer */
  215. if (trans->transfer_list.next != &msg->transfers) {
  216. dws->cur_transfer =
  217. list_entry(trans->transfer_list.next,
  218. struct spi_transfer,
  219. transfer_list);
  220. return RUNNING_STATE;
  221. } else
  222. return DONE_STATE;
  223. }
  224. /*
  225. * Note: first step is the protocol driver prepares
  226. * a dma-capable memory, and this func just need translate
  227. * the virt addr to physical
  228. */
  229. static int map_dma_buffers(struct dw_spi *dws)
  230. {
  231. if (!dws->cur_msg->is_dma_mapped
  232. || !dws->dma_inited
  233. || !dws->cur_chip->enable_dma
  234. || !dws->dma_ops)
  235. return 0;
  236. if (dws->cur_transfer->tx_dma)
  237. dws->tx_dma = dws->cur_transfer->tx_dma;
  238. if (dws->cur_transfer->rx_dma)
  239. dws->rx_dma = dws->cur_transfer->rx_dma;
  240. return 1;
  241. }
  242. /* Caller already set message->status; dma and pio irqs are blocked */
  243. static void giveback(struct dw_spi *dws)
  244. {
  245. struct spi_transfer *last_transfer;
  246. unsigned long flags;
  247. struct spi_message *msg;
  248. spin_lock_irqsave(&dws->lock, flags);
  249. msg = dws->cur_msg;
  250. dws->cur_msg = NULL;
  251. dws->cur_transfer = NULL;
  252. dws->prev_chip = dws->cur_chip;
  253. dws->cur_chip = NULL;
  254. dws->dma_mapped = 0;
  255. queue_work(dws->workqueue, &dws->pump_messages);
  256. spin_unlock_irqrestore(&dws->lock, flags);
  257. last_transfer = list_entry(msg->transfers.prev,
  258. struct spi_transfer,
  259. transfer_list);
  260. if (!last_transfer->cs_change && dws->cs_control)
  261. dws->cs_control(MRST_SPI_DEASSERT);
  262. msg->state = NULL;
  263. if (msg->complete)
  264. msg->complete(msg->context);
  265. }
  266. static void int_error_stop(struct dw_spi *dws, const char *msg)
  267. {
  268. /* Stop the hw */
  269. spi_enable_chip(dws, 0);
  270. dev_err(&dws->master->dev, "%s\n", msg);
  271. dws->cur_msg->state = ERROR_STATE;
  272. tasklet_schedule(&dws->pump_transfers);
  273. }
  274. void dw_spi_xfer_done(struct dw_spi *dws)
  275. {
  276. /* Update total byte transfered return count actual bytes read */
  277. dws->cur_msg->actual_length += dws->len;
  278. /* Move to next transfer */
  279. dws->cur_msg->state = next_transfer(dws);
  280. /* Handle end of message */
  281. if (dws->cur_msg->state == DONE_STATE) {
  282. dws->cur_msg->status = 0;
  283. giveback(dws);
  284. } else
  285. tasklet_schedule(&dws->pump_transfers);
  286. }
  287. EXPORT_SYMBOL_GPL(dw_spi_xfer_done);
  288. static irqreturn_t interrupt_transfer(struct dw_spi *dws)
  289. {
  290. u16 irq_status, irq_mask = 0x3f;
  291. u32 int_level = dws->fifo_len / 2;
  292. u32 left;
  293. irq_status = dw_readw(dws, isr) & irq_mask;
  294. /* Error handling */
  295. if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
  296. dw_readw(dws, txoicr);
  297. dw_readw(dws, rxoicr);
  298. dw_readw(dws, rxuicr);
  299. int_error_stop(dws, "interrupt_transfer: fifo overrun");
  300. return IRQ_HANDLED;
  301. }
  302. if (irq_status & SPI_INT_TXEI) {
  303. spi_mask_intr(dws, SPI_INT_TXEI);
  304. left = (dws->tx_end - dws->tx) / dws->n_bytes;
  305. left = (left > int_level) ? int_level : left;
  306. while (left--)
  307. dw_writer(dws);
  308. dw_reader(dws);
  309. /* Re-enable the IRQ if there is still data left to tx */
  310. if (dws->tx_end > dws->tx)
  311. spi_umask_intr(dws, SPI_INT_TXEI);
  312. else
  313. dw_spi_xfer_done(dws);
  314. }
  315. return IRQ_HANDLED;
  316. }
  317. static irqreturn_t dw_spi_irq(int irq, void *dev_id)
  318. {
  319. struct dw_spi *dws = dev_id;
  320. u16 irq_status, irq_mask = 0x3f;
  321. irq_status = dw_readw(dws, isr) & irq_mask;
  322. if (!irq_status)
  323. return IRQ_NONE;
  324. if (!dws->cur_msg) {
  325. spi_mask_intr(dws, SPI_INT_TXEI);
  326. /* Never fail */
  327. return IRQ_HANDLED;
  328. }
  329. return dws->transfer_handler(dws);
  330. }
  331. /* Must be called inside pump_transfers() */
  332. static void poll_transfer(struct dw_spi *dws)
  333. {
  334. do {
  335. dw_writer(dws);
  336. dw_reader(dws);
  337. cpu_relax();
  338. } while (dws->rx_end > dws->rx);
  339. dw_spi_xfer_done(dws);
  340. }
  341. static void pump_transfers(unsigned long data)
  342. {
  343. struct dw_spi *dws = (struct dw_spi *)data;
  344. struct spi_message *message = NULL;
  345. struct spi_transfer *transfer = NULL;
  346. struct spi_transfer *previous = NULL;
  347. struct spi_device *spi = NULL;
  348. struct chip_data *chip = NULL;
  349. u8 bits = 0;
  350. u8 imask = 0;
  351. u8 cs_change = 0;
  352. u16 txint_level = 0;
  353. u16 clk_div = 0;
  354. u32 speed = 0;
  355. u32 cr0 = 0;
  356. /* Get current state information */
  357. message = dws->cur_msg;
  358. transfer = dws->cur_transfer;
  359. chip = dws->cur_chip;
  360. spi = message->spi;
  361. if (unlikely(!chip->clk_div))
  362. chip->clk_div = dws->max_freq / chip->speed_hz;
  363. if (message->state == ERROR_STATE) {
  364. message->status = -EIO;
  365. goto early_exit;
  366. }
  367. /* Handle end of message */
  368. if (message->state == DONE_STATE) {
  369. message->status = 0;
  370. goto early_exit;
  371. }
  372. /* Delay if requested at end of transfer*/
  373. if (message->state == RUNNING_STATE) {
  374. previous = list_entry(transfer->transfer_list.prev,
  375. struct spi_transfer,
  376. transfer_list);
  377. if (previous->delay_usecs)
  378. udelay(previous->delay_usecs);
  379. }
  380. dws->n_bytes = chip->n_bytes;
  381. dws->dma_width = chip->dma_width;
  382. dws->cs_control = chip->cs_control;
  383. dws->rx_dma = transfer->rx_dma;
  384. dws->tx_dma = transfer->tx_dma;
  385. dws->tx = (void *)transfer->tx_buf;
  386. dws->tx_end = dws->tx + transfer->len;
  387. dws->rx = transfer->rx_buf;
  388. dws->rx_end = dws->rx + transfer->len;
  389. dws->cs_change = transfer->cs_change;
  390. dws->len = dws->cur_transfer->len;
  391. if (chip != dws->prev_chip)
  392. cs_change = 1;
  393. cr0 = chip->cr0;
  394. /* Handle per transfer options for bpw and speed */
  395. if (transfer->speed_hz) {
  396. speed = chip->speed_hz;
  397. if (transfer->speed_hz != speed) {
  398. speed = transfer->speed_hz;
  399. if (speed > dws->max_freq) {
  400. printk(KERN_ERR "MRST SPI0: unsupported"
  401. "freq: %dHz\n", speed);
  402. message->status = -EIO;
  403. goto early_exit;
  404. }
  405. /* clk_div doesn't support odd number */
  406. clk_div = dws->max_freq / speed;
  407. clk_div = (clk_div + 1) & 0xfffe;
  408. chip->speed_hz = speed;
  409. chip->clk_div = clk_div;
  410. }
  411. }
  412. if (transfer->bits_per_word) {
  413. bits = transfer->bits_per_word;
  414. switch (bits) {
  415. case 8:
  416. dws->n_bytes = 1;
  417. dws->dma_width = 1;
  418. break;
  419. case 16:
  420. dws->n_bytes = 2;
  421. dws->dma_width = 2;
  422. break;
  423. default:
  424. printk(KERN_ERR "MRST SPI0: unsupported bits:"
  425. "%db\n", bits);
  426. message->status = -EIO;
  427. goto early_exit;
  428. }
  429. cr0 = (bits - 1)
  430. | (chip->type << SPI_FRF_OFFSET)
  431. | (spi->mode << SPI_MODE_OFFSET)
  432. | (chip->tmode << SPI_TMOD_OFFSET);
  433. }
  434. message->state = RUNNING_STATE;
  435. /*
  436. * Adjust transfer mode if necessary. Requires platform dependent
  437. * chipselect mechanism.
  438. */
  439. if (dws->cs_control) {
  440. if (dws->rx && dws->tx)
  441. chip->tmode = SPI_TMOD_TR;
  442. else if (dws->rx)
  443. chip->tmode = SPI_TMOD_RO;
  444. else
  445. chip->tmode = SPI_TMOD_TO;
  446. cr0 &= ~SPI_TMOD_MASK;
  447. cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
  448. }
  449. /* Check if current transfer is a DMA transaction */
  450. dws->dma_mapped = map_dma_buffers(dws);
  451. /*
  452. * Interrupt mode
  453. * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
  454. */
  455. if (!dws->dma_mapped && !chip->poll_mode) {
  456. int templen = dws->len / dws->n_bytes;
  457. txint_level = dws->fifo_len / 2;
  458. txint_level = (templen > txint_level) ? txint_level : templen;
  459. imask |= SPI_INT_TXEI;
  460. dws->transfer_handler = interrupt_transfer;
  461. }
  462. /*
  463. * Reprogram registers only if
  464. * 1. chip select changes
  465. * 2. clk_div is changed
  466. * 3. control value changes
  467. */
  468. if (dw_readw(dws, ctrl0) != cr0 || cs_change || clk_div || imask) {
  469. spi_enable_chip(dws, 0);
  470. if (dw_readw(dws, ctrl0) != cr0)
  471. dw_writew(dws, ctrl0, cr0);
  472. spi_set_clk(dws, clk_div ? clk_div : chip->clk_div);
  473. spi_chip_sel(dws, spi->chip_select);
  474. /* Set the interrupt mask, for poll mode just disable all int */
  475. spi_mask_intr(dws, 0xff);
  476. if (imask)
  477. spi_umask_intr(dws, imask);
  478. if (txint_level)
  479. dw_writew(dws, txfltr, txint_level);
  480. spi_enable_chip(dws, 1);
  481. if (cs_change)
  482. dws->prev_chip = chip;
  483. }
  484. if (dws->dma_mapped)
  485. dws->dma_ops->dma_transfer(dws, cs_change);
  486. if (chip->poll_mode)
  487. poll_transfer(dws);
  488. return;
  489. early_exit:
  490. giveback(dws);
  491. return;
  492. }
  493. static void pump_messages(struct work_struct *work)
  494. {
  495. struct dw_spi *dws =
  496. container_of(work, struct dw_spi, pump_messages);
  497. unsigned long flags;
  498. /* Lock queue and check for queue work */
  499. spin_lock_irqsave(&dws->lock, flags);
  500. if (list_empty(&dws->queue) || dws->run == QUEUE_STOPPED) {
  501. dws->busy = 0;
  502. spin_unlock_irqrestore(&dws->lock, flags);
  503. return;
  504. }
  505. /* Make sure we are not already running a message */
  506. if (dws->cur_msg) {
  507. spin_unlock_irqrestore(&dws->lock, flags);
  508. return;
  509. }
  510. /* Extract head of queue */
  511. dws->cur_msg = list_entry(dws->queue.next, struct spi_message, queue);
  512. list_del_init(&dws->cur_msg->queue);
  513. /* Initial message state*/
  514. dws->cur_msg->state = START_STATE;
  515. dws->cur_transfer = list_entry(dws->cur_msg->transfers.next,
  516. struct spi_transfer,
  517. transfer_list);
  518. dws->cur_chip = spi_get_ctldata(dws->cur_msg->spi);
  519. /* Mark as busy and launch transfers */
  520. tasklet_schedule(&dws->pump_transfers);
  521. dws->busy = 1;
  522. spin_unlock_irqrestore(&dws->lock, flags);
  523. }
  524. /* spi_device use this to queue in their spi_msg */
  525. static int dw_spi_transfer(struct spi_device *spi, struct spi_message *msg)
  526. {
  527. struct dw_spi *dws = spi_master_get_devdata(spi->master);
  528. unsigned long flags;
  529. spin_lock_irqsave(&dws->lock, flags);
  530. if (dws->run == QUEUE_STOPPED) {
  531. spin_unlock_irqrestore(&dws->lock, flags);
  532. return -ESHUTDOWN;
  533. }
  534. msg->actual_length = 0;
  535. msg->status = -EINPROGRESS;
  536. msg->state = START_STATE;
  537. list_add_tail(&msg->queue, &dws->queue);
  538. if (dws->run == QUEUE_RUNNING && !dws->busy) {
  539. if (dws->cur_transfer || dws->cur_msg)
  540. queue_work(dws->workqueue,
  541. &dws->pump_messages);
  542. else {
  543. /* If no other data transaction in air, just go */
  544. spin_unlock_irqrestore(&dws->lock, flags);
  545. pump_messages(&dws->pump_messages);
  546. return 0;
  547. }
  548. }
  549. spin_unlock_irqrestore(&dws->lock, flags);
  550. return 0;
  551. }
  552. /* This may be called twice for each spi dev */
  553. static int dw_spi_setup(struct spi_device *spi)
  554. {
  555. struct dw_spi_chip *chip_info = NULL;
  556. struct chip_data *chip;
  557. if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
  558. return -EINVAL;
  559. /* Only alloc on first setup */
  560. chip = spi_get_ctldata(spi);
  561. if (!chip) {
  562. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  563. if (!chip)
  564. return -ENOMEM;
  565. }
  566. /*
  567. * Protocol drivers may change the chip settings, so...
  568. * if chip_info exists, use it
  569. */
  570. chip_info = spi->controller_data;
  571. /* chip_info doesn't always exist */
  572. if (chip_info) {
  573. if (chip_info->cs_control)
  574. chip->cs_control = chip_info->cs_control;
  575. chip->poll_mode = chip_info->poll_mode;
  576. chip->type = chip_info->type;
  577. chip->rx_threshold = 0;
  578. chip->tx_threshold = 0;
  579. chip->enable_dma = chip_info->enable_dma;
  580. }
  581. if (spi->bits_per_word <= 8) {
  582. chip->n_bytes = 1;
  583. chip->dma_width = 1;
  584. } else if (spi->bits_per_word <= 16) {
  585. chip->n_bytes = 2;
  586. chip->dma_width = 2;
  587. } else {
  588. /* Never take >16b case for MRST SPIC */
  589. dev_err(&spi->dev, "invalid wordsize\n");
  590. return -EINVAL;
  591. }
  592. chip->bits_per_word = spi->bits_per_word;
  593. if (!spi->max_speed_hz) {
  594. dev_err(&spi->dev, "No max speed HZ parameter\n");
  595. return -EINVAL;
  596. }
  597. chip->speed_hz = spi->max_speed_hz;
  598. chip->tmode = 0; /* Tx & Rx */
  599. /* Default SPI mode is SCPOL = 0, SCPH = 0 */
  600. chip->cr0 = (chip->bits_per_word - 1)
  601. | (chip->type << SPI_FRF_OFFSET)
  602. | (spi->mode << SPI_MODE_OFFSET)
  603. | (chip->tmode << SPI_TMOD_OFFSET);
  604. spi_set_ctldata(spi, chip);
  605. return 0;
  606. }
  607. static void dw_spi_cleanup(struct spi_device *spi)
  608. {
  609. struct chip_data *chip = spi_get_ctldata(spi);
  610. kfree(chip);
  611. }
  612. static int __devinit init_queue(struct dw_spi *dws)
  613. {
  614. INIT_LIST_HEAD(&dws->queue);
  615. spin_lock_init(&dws->lock);
  616. dws->run = QUEUE_STOPPED;
  617. dws->busy = 0;
  618. tasklet_init(&dws->pump_transfers,
  619. pump_transfers, (unsigned long)dws);
  620. INIT_WORK(&dws->pump_messages, pump_messages);
  621. dws->workqueue = create_singlethread_workqueue(
  622. dev_name(dws->master->dev.parent));
  623. if (dws->workqueue == NULL)
  624. return -EBUSY;
  625. return 0;
  626. }
  627. static int start_queue(struct dw_spi *dws)
  628. {
  629. unsigned long flags;
  630. spin_lock_irqsave(&dws->lock, flags);
  631. if (dws->run == QUEUE_RUNNING || dws->busy) {
  632. spin_unlock_irqrestore(&dws->lock, flags);
  633. return -EBUSY;
  634. }
  635. dws->run = QUEUE_RUNNING;
  636. dws->cur_msg = NULL;
  637. dws->cur_transfer = NULL;
  638. dws->cur_chip = NULL;
  639. dws->prev_chip = NULL;
  640. spin_unlock_irqrestore(&dws->lock, flags);
  641. queue_work(dws->workqueue, &dws->pump_messages);
  642. return 0;
  643. }
  644. static int stop_queue(struct dw_spi *dws)
  645. {
  646. unsigned long flags;
  647. unsigned limit = 50;
  648. int status = 0;
  649. spin_lock_irqsave(&dws->lock, flags);
  650. dws->run = QUEUE_STOPPED;
  651. while (!list_empty(&dws->queue) && dws->busy && limit--) {
  652. spin_unlock_irqrestore(&dws->lock, flags);
  653. msleep(10);
  654. spin_lock_irqsave(&dws->lock, flags);
  655. }
  656. if (!list_empty(&dws->queue) || dws->busy)
  657. status = -EBUSY;
  658. spin_unlock_irqrestore(&dws->lock, flags);
  659. return status;
  660. }
  661. static int destroy_queue(struct dw_spi *dws)
  662. {
  663. int status;
  664. status = stop_queue(dws);
  665. if (status != 0)
  666. return status;
  667. destroy_workqueue(dws->workqueue);
  668. return 0;
  669. }
  670. /* Restart the controller, disable all interrupts, clean rx fifo */
  671. static void spi_hw_init(struct dw_spi *dws)
  672. {
  673. spi_enable_chip(dws, 0);
  674. spi_mask_intr(dws, 0xff);
  675. spi_enable_chip(dws, 1);
  676. /*
  677. * Try to detect the FIFO depth if not set by interface driver,
  678. * the depth could be from 2 to 256 from HW spec
  679. */
  680. if (!dws->fifo_len) {
  681. u32 fifo;
  682. for (fifo = 2; fifo <= 257; fifo++) {
  683. dw_writew(dws, txfltr, fifo);
  684. if (fifo != dw_readw(dws, txfltr))
  685. break;
  686. }
  687. dws->fifo_len = (fifo == 257) ? 0 : fifo;
  688. dw_writew(dws, txfltr, 0);
  689. }
  690. }
  691. int __devinit dw_spi_add_host(struct dw_spi *dws)
  692. {
  693. struct spi_master *master;
  694. int ret;
  695. BUG_ON(dws == NULL);
  696. master = spi_alloc_master(dws->parent_dev, 0);
  697. if (!master) {
  698. ret = -ENOMEM;
  699. goto exit;
  700. }
  701. dws->master = master;
  702. dws->type = SSI_MOTO_SPI;
  703. dws->prev_chip = NULL;
  704. dws->dma_inited = 0;
  705. dws->dma_addr = (dma_addr_t)(dws->paddr + 0x60);
  706. ret = request_irq(dws->irq, dw_spi_irq, IRQF_SHARED,
  707. "dw_spi", dws);
  708. if (ret < 0) {
  709. dev_err(&master->dev, "can not get IRQ\n");
  710. goto err_free_master;
  711. }
  712. master->mode_bits = SPI_CPOL | SPI_CPHA;
  713. master->bus_num = dws->bus_num;
  714. master->num_chipselect = dws->num_cs;
  715. master->cleanup = dw_spi_cleanup;
  716. master->setup = dw_spi_setup;
  717. master->transfer = dw_spi_transfer;
  718. /* Basic HW init */
  719. spi_hw_init(dws);
  720. if (dws->dma_ops && dws->dma_ops->dma_init) {
  721. ret = dws->dma_ops->dma_init(dws);
  722. if (ret) {
  723. dev_warn(&master->dev, "DMA init failed\n");
  724. dws->dma_inited = 0;
  725. }
  726. }
  727. /* Initial and start queue */
  728. ret = init_queue(dws);
  729. if (ret) {
  730. dev_err(&master->dev, "problem initializing queue\n");
  731. goto err_diable_hw;
  732. }
  733. ret = start_queue(dws);
  734. if (ret) {
  735. dev_err(&master->dev, "problem starting queue\n");
  736. goto err_diable_hw;
  737. }
  738. spi_master_set_devdata(master, dws);
  739. ret = spi_register_master(master);
  740. if (ret) {
  741. dev_err(&master->dev, "problem registering spi master\n");
  742. goto err_queue_alloc;
  743. }
  744. mrst_spi_debugfs_init(dws);
  745. return 0;
  746. err_queue_alloc:
  747. destroy_queue(dws);
  748. if (dws->dma_ops && dws->dma_ops->dma_exit)
  749. dws->dma_ops->dma_exit(dws);
  750. err_diable_hw:
  751. spi_enable_chip(dws, 0);
  752. free_irq(dws->irq, dws);
  753. err_free_master:
  754. spi_master_put(master);
  755. exit:
  756. return ret;
  757. }
  758. EXPORT_SYMBOL_GPL(dw_spi_add_host);
  759. void __devexit dw_spi_remove_host(struct dw_spi *dws)
  760. {
  761. int status = 0;
  762. if (!dws)
  763. return;
  764. mrst_spi_debugfs_remove(dws);
  765. /* Remove the queue */
  766. status = destroy_queue(dws);
  767. if (status != 0)
  768. dev_err(&dws->master->dev, "dw_spi_remove: workqueue will not "
  769. "complete, message memory not freed\n");
  770. if (dws->dma_ops && dws->dma_ops->dma_exit)
  771. dws->dma_ops->dma_exit(dws);
  772. spi_enable_chip(dws, 0);
  773. /* Disable clk */
  774. spi_set_clk(dws, 0);
  775. free_irq(dws->irq, dws);
  776. /* Disconnect from the SPI framework */
  777. spi_unregister_master(dws->master);
  778. }
  779. EXPORT_SYMBOL_GPL(dw_spi_remove_host);
  780. int dw_spi_suspend_host(struct dw_spi *dws)
  781. {
  782. int ret = 0;
  783. ret = stop_queue(dws);
  784. if (ret)
  785. return ret;
  786. spi_enable_chip(dws, 0);
  787. spi_set_clk(dws, 0);
  788. return ret;
  789. }
  790. EXPORT_SYMBOL_GPL(dw_spi_suspend_host);
  791. int dw_spi_resume_host(struct dw_spi *dws)
  792. {
  793. int ret;
  794. spi_hw_init(dws);
  795. ret = start_queue(dws);
  796. if (ret)
  797. dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret);
  798. return ret;
  799. }
  800. EXPORT_SYMBOL_GPL(dw_spi_resume_host);
  801. MODULE_AUTHOR("Feng Tang <feng.tang@intel.com>");
  802. MODULE_DESCRIPTION("Driver for DesignWare SPI controller core");
  803. MODULE_LICENSE("GPL v2");