atmel_spi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (C) 2007 Atmel Corporation
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <dm.h>
  9. #include <fdtdec.h>
  10. #include <spi.h>
  11. #include <malloc.h>
  12. #include <wait_bit.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/clk.h>
  15. #include <asm/arch/hardware.h>
  16. #ifdef CONFIG_DM_SPI
  17. #include <asm/arch/at91_spi.h>
  18. #endif
  19. #ifdef CONFIG_DM_GPIO
  20. #include <asm/gpio.h>
  21. #endif
  22. #include "atmel_spi.h"
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #define MAX_CS_COUNT 4
  25. struct atmel_spi_platdata {
  26. struct at91_spi *regs;
  27. };
  28. struct atmel_spi_priv {
  29. unsigned int freq; /* Default frequency */
  30. unsigned int mode;
  31. ulong bus_clk_rate;
  32. #ifdef CONFIG_DM_GPIO
  33. struct gpio_desc cs_gpios[MAX_CS_COUNT];
  34. #endif
  35. };
  36. static int atmel_spi_claim_bus(struct udevice *dev)
  37. {
  38. struct udevice *bus = dev_get_parent(dev);
  39. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  40. struct atmel_spi_priv *priv = dev_get_priv(bus);
  41. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  42. struct at91_spi *reg_base = bus_plat->regs;
  43. u32 cs = slave_plat->cs;
  44. u32 freq = priv->freq;
  45. u32 scbr, csrx, mode;
  46. scbr = (priv->bus_clk_rate + freq - 1) / freq;
  47. if (scbr > ATMEL_SPI_CSRx_SCBR_MAX)
  48. return -EINVAL;
  49. if (scbr < 1)
  50. scbr = 1;
  51. csrx = ATMEL_SPI_CSRx_SCBR(scbr);
  52. csrx |= ATMEL_SPI_CSRx_BITS(ATMEL_SPI_BITS_8);
  53. if (!(priv->mode & SPI_CPHA))
  54. csrx |= ATMEL_SPI_CSRx_NCPHA;
  55. if (priv->mode & SPI_CPOL)
  56. csrx |= ATMEL_SPI_CSRx_CPOL;
  57. writel(csrx, &reg_base->csr[cs]);
  58. mode = ATMEL_SPI_MR_MSTR |
  59. ATMEL_SPI_MR_MODFDIS |
  60. ATMEL_SPI_MR_WDRBT |
  61. ATMEL_SPI_MR_PCS(~(1 << cs));
  62. writel(mode, &reg_base->mr);
  63. writel(ATMEL_SPI_CR_SPIEN, &reg_base->cr);
  64. return 0;
  65. }
  66. static int atmel_spi_release_bus(struct udevice *dev)
  67. {
  68. struct udevice *bus = dev_get_parent(dev);
  69. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  70. writel(ATMEL_SPI_CR_SPIDIS, &bus_plat->regs->cr);
  71. return 0;
  72. }
  73. static void atmel_spi_cs_activate(struct udevice *dev)
  74. {
  75. #ifdef CONFIG_DM_GPIO
  76. struct udevice *bus = dev_get_parent(dev);
  77. struct atmel_spi_priv *priv = dev_get_priv(bus);
  78. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  79. u32 cs = slave_plat->cs;
  80. if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
  81. return;
  82. dm_gpio_set_value(&priv->cs_gpios[cs], 0);
  83. #endif
  84. }
  85. static void atmel_spi_cs_deactivate(struct udevice *dev)
  86. {
  87. #ifdef CONFIG_DM_GPIO
  88. struct udevice *bus = dev_get_parent(dev);
  89. struct atmel_spi_priv *priv = dev_get_priv(bus);
  90. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  91. u32 cs = slave_plat->cs;
  92. if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
  93. return;
  94. dm_gpio_set_value(&priv->cs_gpios[cs], 1);
  95. #endif
  96. }
  97. static int atmel_spi_xfer(struct udevice *dev, unsigned int bitlen,
  98. const void *dout, void *din, unsigned long flags)
  99. {
  100. struct udevice *bus = dev_get_parent(dev);
  101. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  102. struct at91_spi *reg_base = bus_plat->regs;
  103. u32 len_tx, len_rx, len;
  104. u32 status;
  105. const u8 *txp = dout;
  106. u8 *rxp = din;
  107. u8 value;
  108. if (bitlen == 0)
  109. goto out;
  110. /*
  111. * The controller can do non-multiple-of-8 bit
  112. * transfers, but this driver currently doesn't support it.
  113. *
  114. * It's also not clear how such transfers are supposed to be
  115. * represented as a stream of bytes...this is a limitation of
  116. * the current SPI interface.
  117. */
  118. if (bitlen % 8) {
  119. /* Errors always terminate an ongoing transfer */
  120. flags |= SPI_XFER_END;
  121. goto out;
  122. }
  123. len = bitlen / 8;
  124. /*
  125. * The controller can do automatic CS control, but it is
  126. * somewhat quirky, and it doesn't really buy us much anyway
  127. * in the context of U-Boot.
  128. */
  129. if (flags & SPI_XFER_BEGIN) {
  130. atmel_spi_cs_activate(dev);
  131. /*
  132. * sometimes the RDR is not empty when we get here,
  133. * in theory that should not happen, but it DOES happen.
  134. * Read it here to be on the safe side.
  135. * That also clears the OVRES flag. Required if the
  136. * following loop exits due to OVRES!
  137. */
  138. readl(&reg_base->rdr);
  139. }
  140. for (len_tx = 0, len_rx = 0; len_rx < len; ) {
  141. status = readl(&reg_base->sr);
  142. if (status & ATMEL_SPI_SR_OVRES)
  143. return -1;
  144. if ((len_tx < len) && (status & ATMEL_SPI_SR_TDRE)) {
  145. if (txp)
  146. value = *txp++;
  147. else
  148. value = 0;
  149. writel(value, &reg_base->tdr);
  150. len_tx++;
  151. }
  152. if (status & ATMEL_SPI_SR_RDRF) {
  153. value = readl(&reg_base->rdr);
  154. if (rxp)
  155. *rxp++ = value;
  156. len_rx++;
  157. }
  158. }
  159. out:
  160. if (flags & SPI_XFER_END) {
  161. /*
  162. * Wait until the transfer is completely done before
  163. * we deactivate CS.
  164. */
  165. wait_for_bit_le32(&reg_base->sr,
  166. ATMEL_SPI_SR_TXEMPTY, true, 1000, false);
  167. atmel_spi_cs_deactivate(dev);
  168. }
  169. return 0;
  170. }
  171. static int atmel_spi_set_speed(struct udevice *bus, uint speed)
  172. {
  173. struct atmel_spi_priv *priv = dev_get_priv(bus);
  174. priv->freq = speed;
  175. return 0;
  176. }
  177. static int atmel_spi_set_mode(struct udevice *bus, uint mode)
  178. {
  179. struct atmel_spi_priv *priv = dev_get_priv(bus);
  180. priv->mode = mode;
  181. return 0;
  182. }
  183. static const struct dm_spi_ops atmel_spi_ops = {
  184. .claim_bus = atmel_spi_claim_bus,
  185. .release_bus = atmel_spi_release_bus,
  186. .xfer = atmel_spi_xfer,
  187. .set_speed = atmel_spi_set_speed,
  188. .set_mode = atmel_spi_set_mode,
  189. /*
  190. * cs_info is not needed, since we require all chip selects to be
  191. * in the device tree explicitly
  192. */
  193. };
  194. static int atmel_spi_enable_clk(struct udevice *bus)
  195. {
  196. struct atmel_spi_priv *priv = dev_get_priv(bus);
  197. struct clk clk;
  198. ulong clk_rate;
  199. int ret;
  200. ret = clk_get_by_index(bus, 0, &clk);
  201. if (ret)
  202. return -EINVAL;
  203. ret = clk_enable(&clk);
  204. if (ret)
  205. return ret;
  206. clk_rate = clk_get_rate(&clk);
  207. if (!clk_rate)
  208. return -EINVAL;
  209. priv->bus_clk_rate = clk_rate;
  210. clk_free(&clk);
  211. return 0;
  212. }
  213. static int atmel_spi_probe(struct udevice *bus)
  214. {
  215. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  216. int ret;
  217. ret = atmel_spi_enable_clk(bus);
  218. if (ret)
  219. return ret;
  220. bus_plat->regs = (struct at91_spi *)devfdt_get_addr(bus);
  221. #ifdef CONFIG_DM_GPIO
  222. struct atmel_spi_priv *priv = dev_get_priv(bus);
  223. int i;
  224. ret = gpio_request_list_by_name(bus, "cs-gpios", priv->cs_gpios,
  225. ARRAY_SIZE(priv->cs_gpios), 0);
  226. if (ret < 0) {
  227. pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
  228. return ret;
  229. }
  230. for(i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
  231. if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
  232. continue;
  233. dm_gpio_set_dir_flags(&priv->cs_gpios[i],
  234. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  235. }
  236. #endif
  237. writel(ATMEL_SPI_CR_SWRST, &bus_plat->regs->cr);
  238. return 0;
  239. }
  240. static const struct udevice_id atmel_spi_ids[] = {
  241. { .compatible = "atmel,at91rm9200-spi" },
  242. { }
  243. };
  244. U_BOOT_DRIVER(atmel_spi) = {
  245. .name = "atmel_spi",
  246. .id = UCLASS_SPI,
  247. .of_match = atmel_spi_ids,
  248. .ops = &atmel_spi_ops,
  249. .platdata_auto_alloc_size = sizeof(struct atmel_spi_platdata),
  250. .priv_auto_alloc_size = sizeof(struct atmel_spi_priv),
  251. .probe = atmel_spi_probe,
  252. };