spi-fsl-lib.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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/dma-mapping.h>
  20. #include <linux/fsl_devices.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/kernel.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. const char *mpc8xxx_spi_strmode(unsigned int flags)
  88. {
  89. if (flags & SPI_QE_CPU_MODE) {
  90. return "QE CPU";
  91. } else if (flags & SPI_CPM_MODE) {
  92. if (flags & SPI_QE)
  93. return "QE";
  94. else if (flags & SPI_CPM2)
  95. return "CPM2";
  96. else
  97. return "CPM1";
  98. }
  99. return "CPU";
  100. }
  101. int mpc8xxx_spi_probe(struct device *dev, struct resource *mem,
  102. unsigned int irq)
  103. {
  104. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  105. struct spi_master *master;
  106. struct mpc8xxx_spi *mpc8xxx_spi;
  107. int ret = 0;
  108. master = dev_get_drvdata(dev);
  109. /* the spi->mode bits understood by this driver: */
  110. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  111. | SPI_LSB_FIRST | SPI_LOOP;
  112. master->transfer = mpc8xxx_spi_transfer;
  113. master->dev.of_node = dev->of_node;
  114. mpc8xxx_spi = spi_master_get_devdata(master);
  115. mpc8xxx_spi->dev = dev;
  116. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  117. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  118. mpc8xxx_spi->flags = pdata->flags;
  119. mpc8xxx_spi->spibrg = pdata->sysclk;
  120. mpc8xxx_spi->irq = irq;
  121. mpc8xxx_spi->rx_shift = 0;
  122. mpc8xxx_spi->tx_shift = 0;
  123. init_completion(&mpc8xxx_spi->done);
  124. master->bus_num = pdata->bus_num;
  125. master->num_chipselect = pdata->max_chipselect;
  126. spin_lock_init(&mpc8xxx_spi->lock);
  127. init_completion(&mpc8xxx_spi->done);
  128. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  129. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  130. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  131. dev_name(master->dev.parent));
  132. if (mpc8xxx_spi->workqueue == NULL) {
  133. ret = -EBUSY;
  134. goto err;
  135. }
  136. return 0;
  137. err:
  138. return ret;
  139. }
  140. int mpc8xxx_spi_remove(struct device *dev)
  141. {
  142. struct mpc8xxx_spi *mpc8xxx_spi;
  143. struct spi_master *master;
  144. master = dev_get_drvdata(dev);
  145. mpc8xxx_spi = spi_master_get_devdata(master);
  146. flush_workqueue(mpc8xxx_spi->workqueue);
  147. destroy_workqueue(mpc8xxx_spi->workqueue);
  148. spi_unregister_master(master);
  149. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  150. if (mpc8xxx_spi->spi_remove)
  151. mpc8xxx_spi->spi_remove(mpc8xxx_spi);
  152. return 0;
  153. }
  154. int of_mpc8xxx_spi_probe(struct platform_device *ofdev)
  155. {
  156. struct device *dev = &ofdev->dev;
  157. struct device_node *np = ofdev->dev.of_node;
  158. struct mpc8xxx_spi_probe_info *pinfo;
  159. struct fsl_spi_platform_data *pdata;
  160. const void *prop;
  161. int ret = -ENOMEM;
  162. pinfo = devm_kzalloc(&ofdev->dev, sizeof(*pinfo), GFP_KERNEL);
  163. if (!pinfo)
  164. return ret;
  165. pdata = &pinfo->pdata;
  166. dev->platform_data = pdata;
  167. /* Allocate bus num dynamically. */
  168. pdata->bus_num = -1;
  169. #ifdef CONFIG_FSL_SOC
  170. /* SPI controller is either clocked from QE or SoC clock. */
  171. pdata->sysclk = get_brgfreq();
  172. if (pdata->sysclk == -1) {
  173. pdata->sysclk = fsl_get_sys_freq();
  174. if (pdata->sysclk == -1)
  175. return -ENODEV;
  176. }
  177. #else
  178. ret = of_property_read_u32(np, "clock-frequency", &pdata->sysclk);
  179. if (ret)
  180. return ret;
  181. #endif
  182. prop = of_get_property(np, "mode", NULL);
  183. if (prop && !strcmp(prop, "cpu-qe"))
  184. pdata->flags = SPI_QE_CPU_MODE;
  185. else if (prop && !strcmp(prop, "qe"))
  186. pdata->flags = SPI_CPM_MODE | SPI_QE;
  187. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  188. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  189. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  190. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  191. return 0;
  192. }