soft_spi.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2014 Google, Inc
  3. *
  4. * (C) Copyright 2002
  5. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  6. *
  7. * Influenced by code from:
  8. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0+
  11. */
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <errno.h>
  15. #include <fdtdec.h>
  16. #include <malloc.h>
  17. #include <spi.h>
  18. #include <asm/gpio.h>
  19. DECLARE_GLOBAL_DATA_PTR;
  20. struct soft_spi_platdata {
  21. struct gpio_desc cs;
  22. struct gpio_desc sclk;
  23. struct gpio_desc mosi;
  24. struct gpio_desc miso;
  25. int spi_delay_us;
  26. };
  27. struct soft_spi_priv {
  28. unsigned int mode;
  29. };
  30. static int soft_spi_scl(struct udevice *dev, int bit)
  31. {
  32. struct soft_spi_platdata *plat = dev->platdata;
  33. dm_gpio_set_value(&plat->sclk, bit);
  34. return 0;
  35. }
  36. static int soft_spi_sda(struct udevice *dev, int bit)
  37. {
  38. struct soft_spi_platdata *plat = dev->platdata;
  39. dm_gpio_set_value(&plat->mosi, bit);
  40. return 0;
  41. }
  42. static int soft_spi_cs_activate(struct udevice *dev)
  43. {
  44. struct soft_spi_platdata *plat = dev->platdata;
  45. dm_gpio_set_value(&plat->cs, 0);
  46. dm_gpio_set_value(&plat->sclk, 0);
  47. dm_gpio_set_value(&plat->cs, 1);
  48. return 0;
  49. }
  50. static int soft_spi_cs_deactivate(struct udevice *dev)
  51. {
  52. struct soft_spi_platdata *plat = dev->platdata;
  53. dm_gpio_set_value(&plat->cs, 0);
  54. return 0;
  55. }
  56. static int soft_spi_claim_bus(struct udevice *dev)
  57. {
  58. /*
  59. * Make sure the SPI clock is in idle state as defined for
  60. * this slave.
  61. */
  62. return soft_spi_scl(dev, 0);
  63. }
  64. static int soft_spi_release_bus(struct udevice *dev)
  65. {
  66. /* Nothing to do */
  67. return 0;
  68. }
  69. /*-----------------------------------------------------------------------
  70. * SPI transfer
  71. *
  72. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  73. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  74. *
  75. * The source of the outgoing bits is the "dout" parameter and the
  76. * destination of the input bits is the "din" parameter. Note that "dout"
  77. * and "din" can point to the same memory location, in which case the
  78. * input data overwrites the output data (since both are buffered by
  79. * temporary variables, this is OK).
  80. */
  81. static int soft_spi_xfer(struct udevice *dev, unsigned int bitlen,
  82. const void *dout, void *din, unsigned long flags)
  83. {
  84. struct soft_spi_priv *priv = dev_get_priv(dev);
  85. struct soft_spi_platdata *plat = dev->platdata;
  86. uchar tmpdin = 0;
  87. uchar tmpdout = 0;
  88. const u8 *txd = dout;
  89. u8 *rxd = din;
  90. int cpha = priv->mode & SPI_CPHA;
  91. unsigned int j;
  92. debug("spi_xfer: slave %s:%s dout %08X din %08X bitlen %u\n",
  93. dev->parent->name, dev->name, *(uint *)txd, *(uint *)rxd,
  94. bitlen);
  95. if (flags & SPI_XFER_BEGIN)
  96. soft_spi_cs_activate(dev);
  97. for (j = 0; j < bitlen; j++) {
  98. /*
  99. * Check if it is time to work on a new byte.
  100. */
  101. if ((j % 8) == 0) {
  102. if (txd)
  103. tmpdout = *txd++;
  104. else
  105. tmpdout = 0;
  106. if (j != 0) {
  107. if (rxd)
  108. *rxd++ = tmpdin;
  109. }
  110. tmpdin = 0;
  111. }
  112. if (!cpha)
  113. soft_spi_scl(dev, 0);
  114. soft_spi_sda(dev, tmpdout & 0x80);
  115. udelay(plat->spi_delay_us);
  116. if (cpha)
  117. soft_spi_scl(dev, 0);
  118. else
  119. soft_spi_scl(dev, 1);
  120. tmpdin <<= 1;
  121. tmpdin |= dm_gpio_get_value(&plat->miso);
  122. tmpdout <<= 1;
  123. udelay(plat->spi_delay_us);
  124. if (cpha)
  125. soft_spi_scl(dev, 1);
  126. }
  127. /*
  128. * If the number of bits isn't a multiple of 8, shift the last
  129. * bits over to left-justify them. Then store the last byte
  130. * read in.
  131. */
  132. if (rxd) {
  133. if ((bitlen % 8) != 0)
  134. tmpdin <<= 8 - (bitlen % 8);
  135. *rxd++ = tmpdin;
  136. }
  137. if (flags & SPI_XFER_END)
  138. soft_spi_cs_deactivate(dev);
  139. return 0;
  140. }
  141. static int soft_spi_set_speed(struct udevice *dev, unsigned int speed)
  142. {
  143. /* Accept any speed */
  144. return 0;
  145. }
  146. static int soft_spi_set_mode(struct udevice *dev, unsigned int mode)
  147. {
  148. struct soft_spi_priv *priv = dev_get_priv(dev);
  149. priv->mode = mode;
  150. return 0;
  151. }
  152. static const struct dm_spi_ops soft_spi_ops = {
  153. .claim_bus = soft_spi_claim_bus,
  154. .release_bus = soft_spi_release_bus,
  155. .xfer = soft_spi_xfer,
  156. .set_speed = soft_spi_set_speed,
  157. .set_mode = soft_spi_set_mode,
  158. };
  159. static int soft_spi_ofdata_to_platdata(struct udevice *dev)
  160. {
  161. struct soft_spi_platdata *plat = dev->platdata;
  162. const void *blob = gd->fdt_blob;
  163. int node = dev->of_offset;
  164. plat->spi_delay_us = fdtdec_get_int(blob, node, "spi-delay-us", 0);
  165. return 0;
  166. }
  167. static int soft_spi_probe(struct udevice *dev)
  168. {
  169. struct spi_slave *slave = dev_get_parent_priv(dev);
  170. struct soft_spi_platdata *plat = dev->platdata;
  171. int cs_flags, clk_flags;
  172. cs_flags = (slave->mode & SPI_CS_HIGH) ? 0 : GPIOD_ACTIVE_LOW;
  173. clk_flags = (slave->mode & SPI_CPOL) ? GPIOD_ACTIVE_LOW : 0;
  174. if (gpio_request_by_name(dev, "cs-gpio", 0, &plat->cs,
  175. GPIOD_IS_OUT | cs_flags) ||
  176. gpio_request_by_name(dev, "sclk-gpio", 0, &plat->sclk,
  177. GPIOD_IS_OUT | clk_flags) ||
  178. gpio_request_by_name(dev, "mosi-gpio", 0, &plat->mosi,
  179. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE) ||
  180. gpio_request_by_name(dev, "miso-gpio", 0, &plat->miso,
  181. GPIOD_IS_IN))
  182. return -EINVAL;
  183. return 0;
  184. }
  185. static const struct udevice_id soft_spi_ids[] = {
  186. { .compatible = "u-boot,soft-spi" },
  187. { }
  188. };
  189. U_BOOT_DRIVER(soft_spi) = {
  190. .name = "soft_spi",
  191. .id = UCLASS_SPI,
  192. .of_match = soft_spi_ids,
  193. .ops = &soft_spi_ops,
  194. .ofdata_to_platdata = soft_spi_ofdata_to_platdata,
  195. .platdata_auto_alloc_size = sizeof(struct soft_spi_platdata),
  196. .priv_auto_alloc_size = sizeof(struct soft_spi_priv),
  197. .probe = soft_spi_probe,
  198. };