soft_spi.c 5.6 KB

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