spi-fsl-lib.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Freescale SPI/eSPI controller driver library.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. *
  8. * CPM SPI and QE buffer descriptors mode support:
  9. * Copyright (c) 2009 MontaVista Software, Inc.
  10. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * Copyright 2010 Freescale Semiconductor, Inc.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/fsl_devices.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/mm.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/spi/spi.h>
  26. #ifdef CONFIG_FSL_SOC
  27. #include <sysdev/fsl_soc.h>
  28. #endif
  29. #include "spi-fsl-lib.h"
  30. #define MPC8XXX_SPI_RX_BUF(type) \
  31. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  32. { \
  33. type *rx = mpc8xxx_spi->rx; \
  34. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  35. mpc8xxx_spi->rx = rx; \
  36. }
  37. #define MPC8XXX_SPI_TX_BUF(type) \
  38. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  39. { \
  40. u32 data; \
  41. const type *tx = mpc8xxx_spi->tx; \
  42. if (!tx) \
  43. return 0; \
  44. data = *tx++ << mpc8xxx_spi->tx_shift; \
  45. mpc8xxx_spi->tx = tx; \
  46. return data; \
  47. }
  48. MPC8XXX_SPI_RX_BUF(u8)
  49. MPC8XXX_SPI_RX_BUF(u16)
  50. MPC8XXX_SPI_RX_BUF(u32)
  51. MPC8XXX_SPI_TX_BUF(u8)
  52. MPC8XXX_SPI_TX_BUF(u16)
  53. MPC8XXX_SPI_TX_BUF(u32)
  54. struct mpc8xxx_spi_probe_info *to_of_pinfo(struct fsl_spi_platform_data *pdata)
  55. {
  56. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  57. }
  58. static void mpc8xxx_spi_work(struct work_struct *work)
  59. {
  60. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  61. work);
  62. spin_lock_irq(&mpc8xxx_spi->lock);
  63. while (!list_empty(&mpc8xxx_spi->queue)) {
  64. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  65. struct spi_message, queue);
  66. list_del_init(&m->queue);
  67. spin_unlock_irq(&mpc8xxx_spi->lock);
  68. if (mpc8xxx_spi->spi_do_one_msg)
  69. mpc8xxx_spi->spi_do_one_msg(m);
  70. spin_lock_irq(&mpc8xxx_spi->lock);
  71. }
  72. spin_unlock_irq(&mpc8xxx_spi->lock);
  73. }
  74. int mpc8xxx_spi_transfer(struct spi_device *spi,
  75. struct spi_message *m)
  76. {
  77. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  78. unsigned long flags;
  79. m->actual_length = 0;
  80. m->status = -EINPROGRESS;
  81. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  82. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  83. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  84. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  85. return 0;
  86. }
  87. void mpc8xxx_spi_cleanup(struct spi_device *spi)
  88. {
  89. kfree(spi->controller_state);
  90. }
  91. const char *mpc8xxx_spi_strmode(unsigned int flags)
  92. {
  93. if (flags & SPI_QE_CPU_MODE) {
  94. return "QE CPU";
  95. } else if (flags & SPI_CPM_MODE) {
  96. if (flags & SPI_QE)
  97. return "QE";
  98. else if (flags & SPI_CPM2)
  99. return "CPM2";
  100. else
  101. return "CPM1";
  102. }
  103. return "CPU";
  104. }
  105. int mpc8xxx_spi_probe(struct device *dev, struct resource *mem,
  106. unsigned int irq)
  107. {
  108. struct fsl_spi_platform_data *pdata = dev->platform_data;
  109. struct spi_master *master;
  110. struct mpc8xxx_spi *mpc8xxx_spi;
  111. int ret = 0;
  112. master = dev_get_drvdata(dev);
  113. /* the spi->mode bits understood by this driver: */
  114. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  115. | SPI_LSB_FIRST | SPI_LOOP;
  116. master->transfer = mpc8xxx_spi_transfer;
  117. master->cleanup = mpc8xxx_spi_cleanup;
  118. master->dev.of_node = dev->of_node;
  119. mpc8xxx_spi = spi_master_get_devdata(master);
  120. mpc8xxx_spi->dev = dev;
  121. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  122. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  123. mpc8xxx_spi->flags = pdata->flags;
  124. mpc8xxx_spi->spibrg = pdata->sysclk;
  125. mpc8xxx_spi->irq = irq;
  126. mpc8xxx_spi->rx_shift = 0;
  127. mpc8xxx_spi->tx_shift = 0;
  128. init_completion(&mpc8xxx_spi->done);
  129. master->bus_num = pdata->bus_num;
  130. master->num_chipselect = pdata->max_chipselect;
  131. spin_lock_init(&mpc8xxx_spi->lock);
  132. init_completion(&mpc8xxx_spi->done);
  133. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  134. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  135. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  136. dev_name(master->dev.parent));
  137. if (mpc8xxx_spi->workqueue == NULL) {
  138. ret = -EBUSY;
  139. goto err;
  140. }
  141. return 0;
  142. err:
  143. return ret;
  144. }
  145. int mpc8xxx_spi_remove(struct device *dev)
  146. {
  147. struct mpc8xxx_spi *mpc8xxx_spi;
  148. struct spi_master *master;
  149. master = dev_get_drvdata(dev);
  150. mpc8xxx_spi = spi_master_get_devdata(master);
  151. flush_workqueue(mpc8xxx_spi->workqueue);
  152. destroy_workqueue(mpc8xxx_spi->workqueue);
  153. spi_unregister_master(master);
  154. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  155. if (mpc8xxx_spi->spi_remove)
  156. mpc8xxx_spi->spi_remove(mpc8xxx_spi);
  157. return 0;
  158. }
  159. int of_mpc8xxx_spi_probe(struct platform_device *ofdev)
  160. {
  161. struct device *dev = &ofdev->dev;
  162. struct device_node *np = ofdev->dev.of_node;
  163. struct mpc8xxx_spi_probe_info *pinfo;
  164. struct fsl_spi_platform_data *pdata;
  165. const void *prop;
  166. int ret = -ENOMEM;
  167. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  168. if (!pinfo)
  169. return -ENOMEM;
  170. pdata = &pinfo->pdata;
  171. dev->platform_data = pdata;
  172. /* Allocate bus num dynamically. */
  173. pdata->bus_num = -1;
  174. #ifdef CONFIG_FSL_SOC
  175. /* SPI controller is either clocked from QE or SoC clock. */
  176. pdata->sysclk = get_brgfreq();
  177. if (pdata->sysclk == -1) {
  178. pdata->sysclk = fsl_get_sys_freq();
  179. if (pdata->sysclk == -1) {
  180. ret = -ENODEV;
  181. goto err;
  182. }
  183. }
  184. #else
  185. ret = of_property_read_u32(np, "clock-frequency", &pdata->sysclk);
  186. if (ret)
  187. goto err;
  188. #endif
  189. prop = of_get_property(np, "mode", NULL);
  190. if (prop && !strcmp(prop, "cpu-qe"))
  191. pdata->flags = SPI_QE_CPU_MODE;
  192. else if (prop && !strcmp(prop, "qe"))
  193. pdata->flags = SPI_CPM_MODE | SPI_QE;
  194. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  195. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  196. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  197. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  198. return 0;
  199. err:
  200. kfree(pinfo);
  201. return ret;
  202. }