spi-fsl-lib.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/module.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/spi/spi.h>
  27. #ifdef CONFIG_FSL_SOC
  28. #include <sysdev/fsl_soc.h>
  29. #endif
  30. #include "spi-fsl-lib.h"
  31. #define MPC8XXX_SPI_RX_BUF(type) \
  32. void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
  33. { \
  34. type *rx = mpc8xxx_spi->rx; \
  35. *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
  36. mpc8xxx_spi->rx = rx; \
  37. } \
  38. EXPORT_SYMBOL_GPL(mpc8xxx_spi_rx_buf_##type);
  39. #define MPC8XXX_SPI_TX_BUF(type) \
  40. u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
  41. { \
  42. u32 data; \
  43. const type *tx = mpc8xxx_spi->tx; \
  44. if (!tx) \
  45. return 0; \
  46. data = *tx++ << mpc8xxx_spi->tx_shift; \
  47. mpc8xxx_spi->tx = tx; \
  48. return data; \
  49. } \
  50. EXPORT_SYMBOL_GPL(mpc8xxx_spi_tx_buf_##type);
  51. MPC8XXX_SPI_RX_BUF(u8)
  52. MPC8XXX_SPI_RX_BUF(u16)
  53. MPC8XXX_SPI_RX_BUF(u32)
  54. MPC8XXX_SPI_TX_BUF(u8)
  55. MPC8XXX_SPI_TX_BUF(u16)
  56. MPC8XXX_SPI_TX_BUF(u32)
  57. struct mpc8xxx_spi_probe_info *to_of_pinfo(struct fsl_spi_platform_data *pdata)
  58. {
  59. return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
  60. }
  61. EXPORT_SYMBOL_GPL(to_of_pinfo);
  62. const char *mpc8xxx_spi_strmode(unsigned int flags)
  63. {
  64. if (flags & SPI_QE_CPU_MODE) {
  65. return "QE CPU";
  66. } else if (flags & SPI_CPM_MODE) {
  67. if (flags & SPI_QE)
  68. return "QE";
  69. else if (flags & SPI_CPM2)
  70. return "CPM2";
  71. else
  72. return "CPM1";
  73. }
  74. return "CPU";
  75. }
  76. EXPORT_SYMBOL_GPL(mpc8xxx_spi_strmode);
  77. void mpc8xxx_spi_probe(struct device *dev, struct resource *mem,
  78. unsigned int irq)
  79. {
  80. struct fsl_spi_platform_data *pdata = dev_get_platdata(dev);
  81. struct spi_master *master;
  82. struct mpc8xxx_spi *mpc8xxx_spi;
  83. master = dev_get_drvdata(dev);
  84. /* the spi->mode bits understood by this driver: */
  85. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
  86. | SPI_LSB_FIRST | SPI_LOOP;
  87. master->dev.of_node = dev->of_node;
  88. mpc8xxx_spi = spi_master_get_devdata(master);
  89. mpc8xxx_spi->dev = dev;
  90. mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
  91. mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
  92. mpc8xxx_spi->flags = pdata->flags;
  93. mpc8xxx_spi->spibrg = pdata->sysclk;
  94. mpc8xxx_spi->irq = irq;
  95. mpc8xxx_spi->rx_shift = 0;
  96. mpc8xxx_spi->tx_shift = 0;
  97. master->bus_num = pdata->bus_num;
  98. master->num_chipselect = pdata->max_chipselect;
  99. init_completion(&mpc8xxx_spi->done);
  100. }
  101. EXPORT_SYMBOL_GPL(mpc8xxx_spi_probe);
  102. int of_mpc8xxx_spi_probe(struct platform_device *ofdev)
  103. {
  104. struct device *dev = &ofdev->dev;
  105. struct device_node *np = ofdev->dev.of_node;
  106. struct mpc8xxx_spi_probe_info *pinfo;
  107. struct fsl_spi_platform_data *pdata;
  108. const void *prop;
  109. int ret = -ENOMEM;
  110. pinfo = devm_kzalloc(&ofdev->dev, sizeof(*pinfo), GFP_KERNEL);
  111. if (!pinfo)
  112. return ret;
  113. pdata = &pinfo->pdata;
  114. dev->platform_data = pdata;
  115. /* Allocate bus num dynamically. */
  116. pdata->bus_num = -1;
  117. #ifdef CONFIG_FSL_SOC
  118. /* SPI controller is either clocked from QE or SoC clock. */
  119. pdata->sysclk = get_brgfreq();
  120. if (pdata->sysclk == -1) {
  121. pdata->sysclk = fsl_get_sys_freq();
  122. if (pdata->sysclk == -1)
  123. return -ENODEV;
  124. }
  125. #else
  126. ret = of_property_read_u32(np, "clock-frequency", &pdata->sysclk);
  127. if (ret)
  128. return ret;
  129. #endif
  130. prop = of_get_property(np, "mode", NULL);
  131. if (prop && !strcmp(prop, "cpu-qe"))
  132. pdata->flags = SPI_QE_CPU_MODE;
  133. else if (prop && !strcmp(prop, "qe"))
  134. pdata->flags = SPI_CPM_MODE | SPI_QE;
  135. else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
  136. pdata->flags = SPI_CPM_MODE | SPI_CPM2;
  137. else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
  138. pdata->flags = SPI_CPM_MODE | SPI_CPM1;
  139. return 0;
  140. }
  141. EXPORT_SYMBOL_GPL(of_mpc8xxx_spi_probe);
  142. MODULE_LICENSE("GPL");