wcxb_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * wcxb SPI library
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. */
  9. /*
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2 as published by the
  18. * Free Software Foundation. See the LICENSE file included with
  19. * this program for more details.
  20. */
  21. #define DEBUG
  22. #include <linux/kernel.h>
  23. #include <linux/list.h>
  24. #include <linux/device.h>
  25. #include <linux/completion.h>
  26. #include <linux/sched.h>
  27. #include <linux/delay.h>
  28. #include <linux/slab.h>
  29. #include <linux/io.h>
  30. #include <dahdi/kernel.h>
  31. #include "wcxb_spi.h"
  32. #define BBIT(n) (1UL << (31UL - (n)))
  33. #define SPISRR 0x40
  34. #define SPISRR_RESET 0x0000000a /* Resets Device */
  35. #define SPICR 0x60
  36. #define SPICR_LSB_FIRST BBIT(22) /* LSB First. 0=MSB first transfer */
  37. #define SPICR_MASTER_INHIBIT BBIT(23) /* Master Transaction Inhibit */
  38. #define SPICR_SLAVE_SELECT BBIT(24) /* Manual Slave Select Assert Enable */
  39. #define SPICR_RX_FIFO_RESET BBIT(25) /* Receive FIFO Reset */
  40. #define SPICR_TX_FIFO_RESET BBIT(26) /* Transmit FIFO Reset */
  41. #define SPICR_CPHA BBIT(27) /* Clock Phase */
  42. #define SPICR_CPOL BBIT(28) /* Clock Polarity 0=Active High */
  43. #define SPICR_MASTER BBIT(29) /* Master Enable */
  44. #define SPICR_SPE BBIT(30) /* SPI System Enable */
  45. #define SPICR_LOOP BBIT(31) /* Local Loopback Mode */
  46. #define SPICR_START_TRANSFER (SPICR_CPHA | SPICR_CPOL | \
  47. SPICR_MASTER | SPICR_SPE)
  48. #define SPICR_READY_TRANSFER (SPICR_MASTER_INHIBIT | SPICR_START_TRANSFER)
  49. #define SPISR 0x64 /* SPI Status Register */
  50. #define SPISR_SLAVE_MODE_SEL BBIT(26) /* Slave Mode Select Flag */
  51. #define SPISR_MODF BBIT(27) /* Mode-Fault Error Flag */
  52. #define SPISR_TX_FULL BBIT(28) /* Transmit FIFO Full */
  53. #define SPISR_TX_EMPTY BBIT(29) /* Transmit FIFO Empty */
  54. #define SPISR_RX_FULL BBIT(30) /* Receive FIFO Full */
  55. #define SPISR_RX_EMPTY BBIT(31) /* Receive FIFO Empty */
  56. #define SPIDTR 0x68 /* SPI Data Transmit Register */
  57. #define SPIDRR 0x6c /* SPI Data Receive Register */
  58. #define SPISSR 0x70 /* SPI Slave Select Register */
  59. #undef SUCCESS
  60. #define SUCCESS 0
  61. struct wcxb_spi_master {
  62. struct device *parent;
  63. struct list_head message_queue;
  64. spinlock_t lock;
  65. void __iomem *base;
  66. struct wcxb_spi_message *cur_msg;
  67. struct wcxb_spi_transfer *cur_transfer;
  68. u16 bytes_left;
  69. u16 bytes_in_fifo;
  70. const u8 *cur_tx_byte;
  71. u8 *cur_rx_byte;
  72. u16 auto_cs:1;
  73. };
  74. static inline void _wcxb_assert_chip_select(struct wcxb_spi_master *master,
  75. unsigned int cs)
  76. {
  77. const int cs_mask = ~(1UL << cs);
  78. iowrite32be(cs_mask, master->base + SPISSR);
  79. ioread32be(master->base + SPISSR);
  80. }
  81. static inline void _wcxb_clear_chip_select(struct wcxb_spi_master *master)
  82. {
  83. iowrite32be(~(0), master->base + SPISSR);
  84. ioread32(master->base + SPISSR);
  85. }
  86. static inline void wcxb_spi_reset_controller(struct wcxb_spi_master *master)
  87. {
  88. u32 spicr = SPICR_READY_TRANSFER;
  89. spicr |= (master->auto_cs) ? 0 : SPICR_SLAVE_SELECT;
  90. iowrite32be(SPISRR_RESET, master->base + SPISRR);
  91. iowrite32be(spicr, master->base + SPICR);
  92. iowrite32be(0xffffffff, master->base + SPISSR);
  93. }
  94. struct wcxb_spi_master *wcxb_spi_master_create(struct device *parent,
  95. void __iomem *membase,
  96. bool auto_cs)
  97. {
  98. struct wcxb_spi_master *master = NULL;
  99. master = kzalloc(sizeof(struct wcxb_spi_master), GFP_KERNEL);
  100. if (!master)
  101. goto error_exit;
  102. spin_lock_init(&master->lock);
  103. INIT_LIST_HEAD(&master->message_queue);
  104. master->base = membase;
  105. master->parent = parent;
  106. master->auto_cs = (auto_cs) ? 1 : 0;
  107. wcxb_spi_reset_controller(master);
  108. return master;
  109. error_exit:
  110. kfree(master);
  111. return NULL;
  112. }
  113. void wcxb_spi_master_destroy(struct wcxb_spi_master *master)
  114. {
  115. struct wcxb_spi_message *m;
  116. if (!master)
  117. return;
  118. while (!list_empty(&master->message_queue)) {
  119. m = list_first_entry(&master->message_queue,
  120. struct wcxb_spi_message, node);
  121. list_del(&m->node);
  122. if (m->complete)
  123. m->complete(m->arg);
  124. }
  125. kfree(master);
  126. return;
  127. }
  128. static inline bool is_txfifo_empty(const struct wcxb_spi_master *master)
  129. {
  130. return ((ioread32(master->base + SPISR) &
  131. cpu_to_be32(SPISR_TX_EMPTY)) > 0);
  132. }
  133. static const u8 DUMMY_TX = 0xff;
  134. static u8 DUMMY_RX;
  135. static void _wcxb_spi_transfer_to_fifo(struct wcxb_spi_master *master)
  136. {
  137. const unsigned int FIFO_SIZE = 16;
  138. u32 spicr;
  139. while (master->bytes_left && master->bytes_in_fifo < FIFO_SIZE) {
  140. iowrite32be(*master->cur_tx_byte, master->base + SPIDTR);
  141. master->bytes_in_fifo++;
  142. master->bytes_left--;
  143. if (&DUMMY_TX != master->cur_tx_byte)
  144. master->cur_tx_byte++;
  145. }
  146. spicr = (master->auto_cs) ? SPICR_START_TRANSFER :
  147. SPICR_START_TRANSFER | SPICR_SLAVE_SELECT;
  148. iowrite32be(spicr, master->base + SPICR);
  149. }
  150. static void _wcxb_spi_transfer_from_fifo(struct wcxb_spi_master *master)
  151. {
  152. u32 spicr;
  153. while (master->bytes_in_fifo) {
  154. *master->cur_rx_byte = ioread32be(master->base + SPIDRR);
  155. if (&DUMMY_RX != master->cur_rx_byte)
  156. master->cur_rx_byte++;
  157. --master->bytes_in_fifo;
  158. }
  159. spicr = SPICR_START_TRANSFER;
  160. spicr |= (master->auto_cs) ? 0 : SPICR_SLAVE_SELECT;
  161. iowrite32be(spicr | SPICR_MASTER_INHIBIT, master->base + SPICR);
  162. }
  163. static void _wcxb_spi_start_transfer(struct wcxb_spi_master *master,
  164. struct wcxb_spi_transfer *t)
  165. {
  166. #ifdef DEBUG
  167. if (!t || !master || (!t->tx_buf && !t->rx_buf) ||
  168. master->cur_transfer) {
  169. WARN_ON(1);
  170. return;
  171. }
  172. #endif
  173. master->cur_transfer = t;
  174. master->bytes_left = t->len;
  175. master->cur_tx_byte = (t->tx_buf) ?: &DUMMY_TX;
  176. master->cur_rx_byte = (t->rx_buf) ?: &DUMMY_RX;
  177. _wcxb_spi_transfer_to_fifo(master);
  178. }
  179. /**
  180. * _wcxb_spi_start_message - Start a new message transferring.
  181. *
  182. * Must be called with master->lock held.
  183. *
  184. */
  185. static int _wcxb_spi_start_message(struct wcxb_spi_master *master,
  186. struct wcxb_spi_message *message)
  187. {
  188. struct wcxb_spi_transfer *t;
  189. if (master->cur_msg) {
  190. /* There is already a message in progress. Queue for later. */
  191. list_add_tail(&message->node, &master->message_queue);
  192. return 0;
  193. }
  194. if (!message->spi) {
  195. dev_dbg(master->parent,
  196. "Queueing message without SPI device specified?\n");
  197. return -EINVAL;
  198. };
  199. master->cur_msg = message;
  200. _wcxb_assert_chip_select(master, message->spi->chip_select);
  201. t = list_first_entry(&message->transfers,
  202. struct wcxb_spi_transfer, node);
  203. _wcxb_spi_start_transfer(master, t);
  204. return 0;
  205. }
  206. /**
  207. * wcxb_spi_complete_message - Complete the current message.
  208. *
  209. * Called after all transfers in current message have been completed. This will
  210. * complete the current message and start the next queued message if there are
  211. * any.
  212. *
  213. * Must be called with the master->lock held.
  214. *
  215. */
  216. static void _wcxb_spi_complete_cur_msg(struct wcxb_spi_master *master)
  217. {
  218. struct wcxb_spi_message *message;
  219. if (!master->cur_msg)
  220. return;
  221. message = master->cur_msg;
  222. message->status = SUCCESS;
  223. _wcxb_clear_chip_select(master);
  224. master->cur_msg = NULL;
  225. if (!list_empty(&master->message_queue)) {
  226. message = list_first_entry(&master->message_queue,
  227. struct wcxb_spi_message, node);
  228. list_del(&message->node);
  229. _wcxb_spi_start_message(master, message);
  230. }
  231. return;
  232. }
  233. static inline bool
  234. _wcxb_spi_is_last_transfer(const struct wcxb_spi_transfer *t,
  235. const struct wcxb_spi_message *message)
  236. {
  237. return t->node.next == &message->transfers;
  238. }
  239. static inline struct wcxb_spi_transfer *
  240. _wcxb_spi_next_transfer(struct wcxb_spi_transfer *t)
  241. {
  242. return list_entry(t->node.next, struct wcxb_spi_transfer, node);
  243. }
  244. /**
  245. * wcxb_spi_handle_interrupt - Drives the transfers forward.
  246. *
  247. * Doesn't necessarily need to be called in the context of a real interrupt, but
  248. * should be called with interrupts disabled on the local CPU.
  249. *
  250. */
  251. void wcxb_spi_handle_interrupt(struct wcxb_spi_master *master)
  252. {
  253. struct wcxb_spi_message *msg;
  254. struct wcxb_spi_transfer *t;
  255. void (*complete)(void *arg) = NULL;
  256. unsigned long flags;
  257. /* Check if we're not in the middle of a transfer, or not finished with
  258. * a part of one. */
  259. spin_lock_irqsave(&master->lock, flags);
  260. t = master->cur_transfer;
  261. msg = master->cur_msg;
  262. if (!msg || !is_txfifo_empty(master))
  263. goto done;
  264. #ifdef DEBUG
  265. if (!t) {
  266. dev_dbg(master->parent,
  267. "No current transfer in %s\n", __func__);
  268. goto done;
  269. }
  270. #endif
  271. /* First read any data out of the receive FIFO into the current
  272. * transfer. */
  273. _wcxb_spi_transfer_from_fifo(master);
  274. if (master->bytes_left) {
  275. /* The current transfer isn't finished. */
  276. _wcxb_spi_transfer_to_fifo(master);
  277. goto done;
  278. }
  279. /* The current transfer is finished. Check for another transfer in this
  280. * message or complete it and look for another message to start. */
  281. master->cur_transfer = NULL;
  282. if (_wcxb_spi_is_last_transfer(t, msg)) {
  283. complete = msg->complete;
  284. _wcxb_spi_complete_cur_msg(master);
  285. } else {
  286. t = _wcxb_spi_next_transfer(t);
  287. _wcxb_spi_start_transfer(master, t);
  288. }
  289. done:
  290. spin_unlock_irqrestore(&master->lock, flags);
  291. /* Do not call the complete call back under the bus lock. */
  292. if (complete)
  293. complete(msg->arg);
  294. return;
  295. }
  296. int wcxb_spi_async(struct wcxb_spi_device *spi,
  297. struct wcxb_spi_message *message)
  298. {
  299. int res;
  300. unsigned long flags;
  301. WARN_ON(!spi || !message || !spi->master);
  302. if (list_empty(&message->transfers)) {
  303. /* No transfers in this message? */
  304. if (message->complete)
  305. message->complete(message->arg);
  306. message->status = -EINVAL;
  307. return 0;
  308. }
  309. message->status = -EINPROGRESS;
  310. message->spi = spi;
  311. spin_lock_irqsave(&spi->master->lock, flags);
  312. res = _wcxb_spi_start_message(spi->master, message);
  313. spin_unlock_irqrestore(&spi->master->lock, flags);
  314. return res;
  315. }
  316. static void wcxb_spi_complete_message(void *arg)
  317. {
  318. complete((struct completion *)arg);
  319. }
  320. int wcxb_spi_sync(struct wcxb_spi_device *spi, struct wcxb_spi_message *message)
  321. {
  322. DECLARE_COMPLETION_ONSTACK(done);
  323. WARN_ON(!spi || !spi->master);
  324. message->complete = wcxb_spi_complete_message;
  325. message->arg = &done;
  326. wcxb_spi_async(spi, message);
  327. /* TODO: There has got to be a better way to do this. */
  328. while (!try_wait_for_completion(&done)) {
  329. wcxb_spi_handle_interrupt(spi->master);
  330. cpu_relax();
  331. }
  332. return message->status;
  333. }