spi_fsl_lib.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/of_spi.h>
  26. #include <sysdev/fsl_soc.h>
  27. #include "spi_fsl_lib.h"
  28. #define MPC8XXX_SPI_RX_BUF(type) \
  29. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  30. { \
  31. type *rx = mpc8xxx_spi->rx; \
  32. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  33. mpc8xxx_spi->rx = rx; \
  34. }
  35. #define MPC8XXX_SPI_TX_BUF(type) \
  36. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  37. { \
  38. u32 data; \
  39. const type *tx = mpc8xxx_spi->tx; \
  40. if (!tx) \
  41. return 0; \
  42. data = *tx++ << mpc8xxx_spi->tx_shift; \
  43. mpc8xxx_spi->tx = tx; \
  44. return data; \
  45. }
  46. MPC8XXX_SPI_RX_BUF(u8)
  47. MPC8XXX_SPI_RX_BUF(u16)
  48. MPC8XXX_SPI_RX_BUF(u32)
  49. MPC8XXX_SPI_TX_BUF(u8)
  50. MPC8XXX_SPI_TX_BUF(u16)
  51. MPC8XXX_SPI_TX_BUF(u32)
  52. struct mpc8xxx_spi_probe_info *to_of_pinfo(struct fsl_spi_platform_data *pdata)
  53. {
  54. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  55. }
  56. void mpc8xxx_spi_work(struct work_struct *work)
  57. {
  58. struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
  59. work);
  60. spin_lock_irq(&mpc8xxx_spi->lock);
  61. while (!list_empty(&mpc8xxx_spi->queue)) {
  62. struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
  63. struct spi_message, queue);
  64. list_del_init(&m->queue);
  65. spin_unlock_irq(&mpc8xxx_spi->lock);
  66. if (mpc8xxx_spi->spi_do_one_msg)
  67. mpc8xxx_spi->spi_do_one_msg(m);
  68. spin_lock_irq(&mpc8xxx_spi->lock);
  69. }
  70. spin_unlock_irq(&mpc8xxx_spi->lock);
  71. }
  72. int mpc8xxx_spi_transfer(struct spi_device *spi,
  73. struct spi_message *m)
  74. {
  75. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  76. unsigned long flags;
  77. m->actual_length = 0;
  78. m->status = -EINPROGRESS;
  79. spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
  80. list_add_tail(&m->queue, &mpc8xxx_spi->queue);
  81. queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
  82. spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
  83. return 0;
  84. }
  85. void mpc8xxx_spi_cleanup(struct spi_device *spi)
  86. {
  87. kfree(spi->controller_state);
  88. }
  89. const char *mpc8xxx_spi_strmode(unsigned int flags)
  90. {
  91. if (flags & SPI_QE_CPU_MODE) {
  92. return "QE CPU";
  93. } else if (flags & SPI_CPM_MODE) {
  94. if (flags & SPI_QE)
  95. return "QE";
  96. else if (flags & SPI_CPM2)
  97. return "CPM2";
  98. else
  99. return "CPM1";
  100. }
  101. return "CPU";
  102. }
  103. int mpc8xxx_spi_probe(struct device *dev, struct resource *mem,
  104. unsigned int irq)
  105. {
  106. struct fsl_spi_platform_data *pdata = dev->platform_data;
  107. struct spi_master *master;
  108. struct mpc8xxx_spi *mpc8xxx_spi;
  109. int ret = 0;
  110. master = dev_get_drvdata(dev);
  111. /* the spi->mode bits understood by this driver: */
  112. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  113. | SPI_LSB_FIRST | SPI_LOOP;
  114. master->transfer = mpc8xxx_spi_transfer;
  115. master->cleanup = mpc8xxx_spi_cleanup;
  116. master->dev.of_node = dev->of_node;
  117. mpc8xxx_spi = spi_master_get_devdata(master);
  118. mpc8xxx_spi->dev = dev;
  119. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  120. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  121. mpc8xxx_spi->flags = pdata->flags;
  122. mpc8xxx_spi->spibrg = pdata->sysclk;
  123. mpc8xxx_spi->irq = irq;
  124. mpc8xxx_spi->rx_shift = 0;
  125. mpc8xxx_spi->tx_shift = 0;
  126. init_completion(&mpc8xxx_spi->done);
  127. master->bus_num = pdata->bus_num;
  128. master->num_chipselect = pdata->max_chipselect;
  129. spin_lock_init(&mpc8xxx_spi->lock);
  130. init_completion(&mpc8xxx_spi->done);
  131. INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
  132. INIT_LIST_HEAD(&mpc8xxx_spi->queue);
  133. mpc8xxx_spi->workqueue = create_singlethread_workqueue(
  134. dev_name(master->dev.parent));
  135. if (mpc8xxx_spi->workqueue == NULL) {
  136. ret = -EBUSY;
  137. goto err;
  138. }
  139. return 0;
  140. err:
  141. return ret;
  142. }
  143. int __devexit mpc8xxx_spi_remove(struct device *dev)
  144. {
  145. struct mpc8xxx_spi *mpc8xxx_spi;
  146. struct spi_master *master;
  147. master = dev_get_drvdata(dev);
  148. mpc8xxx_spi = spi_master_get_devdata(master);
  149. flush_workqueue(mpc8xxx_spi->workqueue);
  150. destroy_workqueue(mpc8xxx_spi->workqueue);
  151. spi_unregister_master(master);
  152. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  153. if (mpc8xxx_spi->spi_remove)
  154. mpc8xxx_spi->spi_remove(mpc8xxx_spi);
  155. return 0;
  156. }
  157. int __devinit of_mpc8xxx_spi_probe(struct platform_device *ofdev)
  158. {
  159. struct device *dev = &ofdev->dev;
  160. struct device_node *np = ofdev->dev.of_node;
  161. struct mpc8xxx_spi_probe_info *pinfo;
  162. struct fsl_spi_platform_data *pdata;
  163. const void *prop;
  164. int ret = -ENOMEM;
  165. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  166. if (!pinfo)
  167. return -ENOMEM;
  168. pdata = &pinfo->pdata;
  169. dev->platform_data = pdata;
  170. /* Allocate bus num dynamically. */
  171. pdata->bus_num = -1;
  172. /* SPI controller is either clocked from QE or SoC clock. */
  173. pdata->sysclk = get_brgfreq();
  174. if (pdata->sysclk == -1) {
  175. pdata->sysclk = fsl_get_sys_freq();
  176. if (pdata->sysclk == -1) {
  177. ret = -ENODEV;
  178. goto err;
  179. }
  180. }
  181. prop = of_get_property(np, "mode", NULL);
  182. if (prop && !strcmp(prop, "cpu-qe"))
  183. pdata->flags = SPI_QE_CPU_MODE;
  184. else if (prop && !strcmp(prop, "qe"))
  185. pdata->flags = SPI_CPM_MODE | SPI_QE;
  186. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  187. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  188. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  189. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  190. return 0;
  191. err:
  192. kfree(pinfo);
  193. return ret;
  194. }