atmel_spi.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. DECLARE_GLOBAL_DATA_PTR;
  23. /* Register offsets */
  24. #define ATMEL_SPI_CR 0x0000
  25. #define ATMEL_SPI_MR 0x0004
  26. #define ATMEL_SPI_RDR 0x0008
  27. #define ATMEL_SPI_TDR 0x000c
  28. #define ATMEL_SPI_SR 0x0010
  29. #define ATMEL_SPI_IER 0x0014
  30. #define ATMEL_SPI_IDR 0x0018
  31. #define ATMEL_SPI_IMR 0x001c
  32. #define ATMEL_SPI_CSR(x) (0x0030 + 4 * (x))
  33. #define ATMEL_SPI_VERSION 0x00fc
  34. /* Bits in CR */
  35. #define ATMEL_SPI_CR_SPIEN BIT(0)
  36. #define ATMEL_SPI_CR_SPIDIS BIT(1)
  37. #define ATMEL_SPI_CR_SWRST BIT(7)
  38. #define ATMEL_SPI_CR_LASTXFER BIT(24)
  39. /* Bits in MR */
  40. #define ATMEL_SPI_MR_MSTR BIT(0)
  41. #define ATMEL_SPI_MR_PS BIT(1)
  42. #define ATMEL_SPI_MR_PCSDEC BIT(2)
  43. #define ATMEL_SPI_MR_FDIV BIT(3)
  44. #define ATMEL_SPI_MR_MODFDIS BIT(4)
  45. #define ATMEL_SPI_MR_WDRBT BIT(5)
  46. #define ATMEL_SPI_MR_LLB BIT(7)
  47. #define ATMEL_SPI_MR_PCS(x) (((x) & 15) << 16)
  48. #define ATMEL_SPI_MR_DLYBCS(x) ((x) << 24)
  49. /* Bits in RDR */
  50. #define ATMEL_SPI_RDR_RD(x) (x)
  51. #define ATMEL_SPI_RDR_PCS(x) ((x) << 16)
  52. /* Bits in TDR */
  53. #define ATMEL_SPI_TDR_TD(x) (x)
  54. #define ATMEL_SPI_TDR_PCS(x) ((x) << 16)
  55. #define ATMEL_SPI_TDR_LASTXFER BIT(24)
  56. /* Bits in SR/IER/IDR/IMR */
  57. #define ATMEL_SPI_SR_RDRF BIT(0)
  58. #define ATMEL_SPI_SR_TDRE BIT(1)
  59. #define ATMEL_SPI_SR_MODF BIT(2)
  60. #define ATMEL_SPI_SR_OVRES BIT(3)
  61. #define ATMEL_SPI_SR_ENDRX BIT(4)
  62. #define ATMEL_SPI_SR_ENDTX BIT(5)
  63. #define ATMEL_SPI_SR_RXBUFF BIT(6)
  64. #define ATMEL_SPI_SR_TXBUFE BIT(7)
  65. #define ATMEL_SPI_SR_NSSR BIT(8)
  66. #define ATMEL_SPI_SR_TXEMPTY BIT(9)
  67. #define ATMEL_SPI_SR_SPIENS BIT(16)
  68. /* Bits in CSRx */
  69. #define ATMEL_SPI_CSR_CPOL BIT(0)
  70. #define ATMEL_SPI_CSR_NCPHA BIT(1)
  71. #define ATMEL_SPI_CSR_CSAAT BIT(3)
  72. #define ATMEL_SPI_CSR_BITS(x) ((x) << 4)
  73. #define ATMEL_SPI_CSR_SCBR(x) ((x) << 8)
  74. #define ATMEL_SPI_CSR_SCBR_MAX GENMASK(7, 0)
  75. #define ATMEL_SPI_CSR_DLYBS(x) ((x) << 16)
  76. #define ATMEL_SPI_CSR_DLYBCT(x) ((x) << 24)
  77. /* Bits in VERSION */
  78. #define ATMEL_SPI_VERSION_REV(x) ((x) & 0xfff)
  79. #define ATMEL_SPI_VERSION_MFN(x) ((x) << 16)
  80. /* Constants for CSRx:BITS */
  81. #define ATMEL_SPI_BITS_8 0
  82. #define ATMEL_SPI_BITS_9 1
  83. #define ATMEL_SPI_BITS_10 2
  84. #define ATMEL_SPI_BITS_11 3
  85. #define ATMEL_SPI_BITS_12 4
  86. #define ATMEL_SPI_BITS_13 5
  87. #define ATMEL_SPI_BITS_14 6
  88. #define ATMEL_SPI_BITS_15 7
  89. #define ATMEL_SPI_BITS_16 8
  90. #define MAX_CS_COUNT 4
  91. struct atmel_spi_slave {
  92. void *regs;
  93. u32 mr;
  94. };
  95. struct atmel_spi_platdata {
  96. struct at91_spi *regs;
  97. };
  98. struct atmel_spi_priv {
  99. unsigned int freq; /* Default frequency */
  100. unsigned int mode;
  101. ulong bus_clk_rate;
  102. #ifdef CONFIG_DM_GPIO
  103. struct gpio_desc cs_gpios[MAX_CS_COUNT];
  104. #endif
  105. };
  106. static int atmel_spi_claim_bus(struct udevice *dev)
  107. {
  108. struct udevice *bus = dev_get_parent(dev);
  109. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  110. struct atmel_spi_priv *priv = dev_get_priv(bus);
  111. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  112. struct at91_spi *reg_base = bus_plat->regs;
  113. u32 cs = slave_plat->cs;
  114. u32 freq = priv->freq;
  115. u32 scbr, csrx, mode;
  116. scbr = (priv->bus_clk_rate + freq - 1) / freq;
  117. if (scbr > ATMEL_SPI_CSR_SCBR_MAX)
  118. return -EINVAL;
  119. if (scbr < 1)
  120. scbr = 1;
  121. csrx = ATMEL_SPI_CSR_SCBR(scbr);
  122. csrx |= ATMEL_SPI_CSR_BITS(ATMEL_SPI_BITS_8);
  123. if (!(priv->mode & SPI_CPHA))
  124. csrx |= ATMEL_SPI_CSR_NCPHA;
  125. if (priv->mode & SPI_CPOL)
  126. csrx |= ATMEL_SPI_CSR_CPOL;
  127. writel(csrx, &reg_base->csr[cs]);
  128. mode = ATMEL_SPI_MR_MSTR |
  129. ATMEL_SPI_MR_MODFDIS |
  130. ATMEL_SPI_MR_WDRBT |
  131. ATMEL_SPI_MR_PCS(~(1 << cs));
  132. writel(mode, &reg_base->mr);
  133. writel(ATMEL_SPI_CR_SPIEN, &reg_base->cr);
  134. return 0;
  135. }
  136. static int atmel_spi_release_bus(struct udevice *dev)
  137. {
  138. struct udevice *bus = dev_get_parent(dev);
  139. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  140. writel(ATMEL_SPI_CR_SPIDIS, &bus_plat->regs->cr);
  141. return 0;
  142. }
  143. static void atmel_spi_cs_activate(struct udevice *dev)
  144. {
  145. #ifdef CONFIG_DM_GPIO
  146. struct udevice *bus = dev_get_parent(dev);
  147. struct atmel_spi_priv *priv = dev_get_priv(bus);
  148. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  149. u32 cs = slave_plat->cs;
  150. if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
  151. return;
  152. dm_gpio_set_value(&priv->cs_gpios[cs], 0);
  153. #endif
  154. }
  155. static void atmel_spi_cs_deactivate(struct udevice *dev)
  156. {
  157. #ifdef CONFIG_DM_GPIO
  158. struct udevice *bus = dev_get_parent(dev);
  159. struct atmel_spi_priv *priv = dev_get_priv(bus);
  160. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  161. u32 cs = slave_plat->cs;
  162. if (!dm_gpio_is_valid(&priv->cs_gpios[cs]))
  163. return;
  164. dm_gpio_set_value(&priv->cs_gpios[cs], 1);
  165. #endif
  166. }
  167. static int atmel_spi_xfer(struct udevice *dev, unsigned int bitlen,
  168. const void *dout, void *din, unsigned long flags)
  169. {
  170. struct udevice *bus = dev_get_parent(dev);
  171. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  172. struct at91_spi *reg_base = bus_plat->regs;
  173. u32 len_tx, len_rx, len;
  174. u32 status;
  175. const u8 *txp = dout;
  176. u8 *rxp = din;
  177. u8 value;
  178. if (bitlen == 0)
  179. goto out;
  180. /*
  181. * The controller can do non-multiple-of-8 bit
  182. * transfers, but this driver currently doesn't support it.
  183. *
  184. * It's also not clear how such transfers are supposed to be
  185. * represented as a stream of bytes...this is a limitation of
  186. * the current SPI interface.
  187. */
  188. if (bitlen % 8) {
  189. /* Errors always terminate an ongoing transfer */
  190. flags |= SPI_XFER_END;
  191. goto out;
  192. }
  193. len = bitlen / 8;
  194. /*
  195. * The controller can do automatic CS control, but it is
  196. * somewhat quirky, and it doesn't really buy us much anyway
  197. * in the context of U-Boot.
  198. */
  199. if (flags & SPI_XFER_BEGIN) {
  200. atmel_spi_cs_activate(dev);
  201. /*
  202. * sometimes the RDR is not empty when we get here,
  203. * in theory that should not happen, but it DOES happen.
  204. * Read it here to be on the safe side.
  205. * That also clears the OVRES flag. Required if the
  206. * following loop exits due to OVRES!
  207. */
  208. readl(&reg_base->rdr);
  209. }
  210. for (len_tx = 0, len_rx = 0; len_rx < len; ) {
  211. status = readl(&reg_base->sr);
  212. if (status & ATMEL_SPI_SR_OVRES)
  213. return -1;
  214. if ((len_tx < len) && (status & ATMEL_SPI_SR_TDRE)) {
  215. if (txp)
  216. value = *txp++;
  217. else
  218. value = 0;
  219. writel(value, &reg_base->tdr);
  220. len_tx++;
  221. }
  222. if (status & ATMEL_SPI_SR_RDRF) {
  223. value = readl(&reg_base->rdr);
  224. if (rxp)
  225. *rxp++ = value;
  226. len_rx++;
  227. }
  228. }
  229. out:
  230. if (flags & SPI_XFER_END) {
  231. /*
  232. * Wait until the transfer is completely done before
  233. * we deactivate CS.
  234. */
  235. wait_for_bit_le32(&reg_base->sr,
  236. ATMEL_SPI_SR_TXEMPTY, true, 1000, false);
  237. atmel_spi_cs_deactivate(dev);
  238. }
  239. return 0;
  240. }
  241. static int atmel_spi_set_speed(struct udevice *bus, uint speed)
  242. {
  243. struct atmel_spi_priv *priv = dev_get_priv(bus);
  244. priv->freq = speed;
  245. return 0;
  246. }
  247. static int atmel_spi_set_mode(struct udevice *bus, uint mode)
  248. {
  249. struct atmel_spi_priv *priv = dev_get_priv(bus);
  250. priv->mode = mode;
  251. return 0;
  252. }
  253. static const struct dm_spi_ops atmel_spi_ops = {
  254. .claim_bus = atmel_spi_claim_bus,
  255. .release_bus = atmel_spi_release_bus,
  256. .xfer = atmel_spi_xfer,
  257. .set_speed = atmel_spi_set_speed,
  258. .set_mode = atmel_spi_set_mode,
  259. /*
  260. * cs_info is not needed, since we require all chip selects to be
  261. * in the device tree explicitly
  262. */
  263. };
  264. static int atmel_spi_enable_clk(struct udevice *bus)
  265. {
  266. struct atmel_spi_priv *priv = dev_get_priv(bus);
  267. struct clk clk;
  268. ulong clk_rate;
  269. int ret;
  270. ret = clk_get_by_index(bus, 0, &clk);
  271. if (ret)
  272. return -EINVAL;
  273. ret = clk_enable(&clk);
  274. if (ret)
  275. return ret;
  276. clk_rate = clk_get_rate(&clk);
  277. if (!clk_rate)
  278. return -EINVAL;
  279. priv->bus_clk_rate = clk_rate;
  280. clk_free(&clk);
  281. return 0;
  282. }
  283. static int atmel_spi_probe(struct udevice *bus)
  284. {
  285. struct atmel_spi_platdata *bus_plat = dev_get_platdata(bus);
  286. int ret;
  287. ret = atmel_spi_enable_clk(bus);
  288. if (ret)
  289. return ret;
  290. bus_plat->regs = (struct at91_spi *)devfdt_get_addr(bus);
  291. #ifdef CONFIG_DM_GPIO
  292. struct atmel_spi_priv *priv = dev_get_priv(bus);
  293. int i;
  294. ret = gpio_request_list_by_name(bus, "cs-gpios", priv->cs_gpios,
  295. ARRAY_SIZE(priv->cs_gpios), 0);
  296. if (ret < 0) {
  297. pr_err("Can't get %s gpios! Error: %d", bus->name, ret);
  298. return ret;
  299. }
  300. for (i = 0; i < ARRAY_SIZE(priv->cs_gpios); i++) {
  301. if (!dm_gpio_is_valid(&priv->cs_gpios[i]))
  302. continue;
  303. dm_gpio_set_dir_flags(&priv->cs_gpios[i],
  304. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  305. }
  306. #endif
  307. writel(ATMEL_SPI_CR_SWRST, &bus_plat->regs->cr);
  308. return 0;
  309. }
  310. static const struct udevice_id atmel_spi_ids[] = {
  311. { .compatible = "atmel,at91rm9200-spi" },
  312. { }
  313. };
  314. U_BOOT_DRIVER(atmel_spi) = {
  315. .name = "atmel_spi",
  316. .id = UCLASS_SPI,
  317. .of_match = atmel_spi_ids,
  318. .ops = &atmel_spi_ops,
  319. .platdata_auto_alloc_size = sizeof(struct atmel_spi_platdata),
  320. .priv_auto_alloc_size = sizeof(struct atmel_spi_priv),
  321. .probe = atmel_spi_probe,
  322. };