tegra20_slink.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVIDIA Tegra SPI-SLINK controller
  4. *
  5. * Copyright (c) 2010-2013 NVIDIA Corporation
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/clock.h>
  11. #include <asm/arch-tegra/clk_rst.h>
  12. #include <spi.h>
  13. #include <fdtdec.h>
  14. #include "tegra_spi.h"
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /* COMMAND */
  17. #define SLINK_CMD_ENB BIT(31)
  18. #define SLINK_CMD_GO BIT(30)
  19. #define SLINK_CMD_M_S BIT(28)
  20. #define SLINK_CMD_IDLE_SCLK_DRIVE_LOW (0 << 24)
  21. #define SLINK_CMD_IDLE_SCLK_DRIVE_HIGH BIT(24)
  22. #define SLINK_CMD_IDLE_SCLK_PULL_LOW (2 << 24)
  23. #define SLINK_CMD_IDLE_SCLK_PULL_HIGH (3 << 24)
  24. #define SLINK_CMD_IDLE_SCLK_MASK (3 << 24)
  25. #define SLINK_CMD_CK_SDA BIT(21)
  26. #define SLINK_CMD_CS_POL BIT(13)
  27. #define SLINK_CMD_CS_VAL BIT(12)
  28. #define SLINK_CMD_CS_SOFT BIT(11)
  29. #define SLINK_CMD_BIT_LENGTH BIT(4)
  30. #define SLINK_CMD_BIT_LENGTH_MASK GENMASK(4, 0)
  31. /* COMMAND2 */
  32. #define SLINK_CMD2_TXEN BIT(30)
  33. #define SLINK_CMD2_RXEN BIT(31)
  34. #define SLINK_CMD2_SS_EN BIT(18)
  35. #define SLINK_CMD2_SS_EN_SHIFT 18
  36. #define SLINK_CMD2_SS_EN_MASK GENMASK(19, 18)
  37. #define SLINK_CMD2_CS_ACTIVE_BETWEEN BIT(17)
  38. /* STATUS */
  39. #define SLINK_STAT_BSY BIT(31)
  40. #define SLINK_STAT_RDY BIT(30)
  41. #define SLINK_STAT_ERR BIT(29)
  42. #define SLINK_STAT_RXF_FLUSH BIT(27)
  43. #define SLINK_STAT_TXF_FLUSH BIT(26)
  44. #define SLINK_STAT_RXF_OVF BIT(25)
  45. #define SLINK_STAT_TXF_UNR BIT(24)
  46. #define SLINK_STAT_RXF_EMPTY BIT(23)
  47. #define SLINK_STAT_RXF_FULL BIT(22)
  48. #define SLINK_STAT_TXF_EMPTY BIT(21)
  49. #define SLINK_STAT_TXF_FULL BIT(20)
  50. #define SLINK_STAT_TXF_OVF BIT(19)
  51. #define SLINK_STAT_RXF_UNR BIT(18)
  52. #define SLINK_STAT_CUR_BLKCNT BIT(15)
  53. /* STATUS2 */
  54. #define SLINK_STAT2_RXF_FULL_CNT BIT(16)
  55. #define SLINK_STAT2_TXF_FULL_CNT BIT(0)
  56. #define SPI_TIMEOUT 1000
  57. #define TEGRA_SPI_MAX_FREQ 52000000
  58. struct spi_regs {
  59. u32 command; /* SLINK_COMMAND_0 register */
  60. u32 command2; /* SLINK_COMMAND2_0 reg */
  61. u32 status; /* SLINK_STATUS_0 register */
  62. u32 reserved; /* Reserved offset 0C */
  63. u32 mas_data; /* SLINK_MAS_DATA_0 reg */
  64. u32 slav_data; /* SLINK_SLAVE_DATA_0 reg */
  65. u32 dma_ctl; /* SLINK_DMA_CTL_0 register */
  66. u32 status2; /* SLINK_STATUS2_0 reg */
  67. u32 rsvd[56]; /* 0x20 to 0xFF reserved */
  68. u32 tx_fifo; /* SLINK_TX_FIFO_0 reg off 100h */
  69. u32 rsvd2[31]; /* 0x104 to 0x17F reserved */
  70. u32 rx_fifo; /* SLINK_RX_FIFO_0 reg off 180h */
  71. };
  72. struct tegra30_spi_priv {
  73. struct spi_regs *regs;
  74. unsigned int freq;
  75. unsigned int mode;
  76. int periph_id;
  77. int valid;
  78. int last_transaction_us;
  79. };
  80. struct tegra_spi_slave {
  81. struct spi_slave slave;
  82. struct tegra30_spi_priv *ctrl;
  83. };
  84. static int tegra30_spi_ofdata_to_platdata(struct udevice *bus)
  85. {
  86. struct tegra_spi_platdata *plat = bus->platdata;
  87. const void *blob = gd->fdt_blob;
  88. int node = dev_of_offset(bus);
  89. plat->base = devfdt_get_addr(bus);
  90. plat->periph_id = clock_decode_periph_id(bus);
  91. if (plat->periph_id == PERIPH_ID_NONE) {
  92. debug("%s: could not decode periph id %d\n", __func__,
  93. plat->periph_id);
  94. return -FDT_ERR_NOTFOUND;
  95. }
  96. /* Use 500KHz as a suitable default */
  97. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  98. 500000);
  99. plat->deactivate_delay_us = fdtdec_get_int(blob, node,
  100. "spi-deactivate-delay", 0);
  101. debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
  102. __func__, plat->base, plat->periph_id, plat->frequency,
  103. plat->deactivate_delay_us);
  104. return 0;
  105. }
  106. static int tegra30_spi_probe(struct udevice *bus)
  107. {
  108. struct tegra_spi_platdata *plat = dev_get_platdata(bus);
  109. struct tegra30_spi_priv *priv = dev_get_priv(bus);
  110. priv->regs = (struct spi_regs *)plat->base;
  111. priv->last_transaction_us = timer_get_us();
  112. priv->freq = plat->frequency;
  113. priv->periph_id = plat->periph_id;
  114. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  115. clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
  116. priv->freq);
  117. return 0;
  118. }
  119. static int tegra30_spi_claim_bus(struct udevice *dev)
  120. {
  121. struct udevice *bus = dev->parent;
  122. struct tegra30_spi_priv *priv = dev_get_priv(bus);
  123. struct spi_regs *regs = priv->regs;
  124. u32 reg;
  125. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  126. clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
  127. priv->freq);
  128. /* Clear stale status here */
  129. reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
  130. SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
  131. writel(reg, &regs->status);
  132. debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
  133. /* Set master mode and sw controlled CS */
  134. reg = readl(&regs->command);
  135. reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
  136. writel(reg, &regs->command);
  137. debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
  138. return 0;
  139. }
  140. static void spi_cs_activate(struct udevice *dev)
  141. {
  142. struct udevice *bus = dev->parent;
  143. struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
  144. struct tegra30_spi_priv *priv = dev_get_priv(bus);
  145. /* If it's too soon to do another transaction, wait */
  146. if (pdata->deactivate_delay_us &&
  147. priv->last_transaction_us) {
  148. ulong delay_us; /* The delay completed so far */
  149. delay_us = timer_get_us() - priv->last_transaction_us;
  150. if (delay_us < pdata->deactivate_delay_us)
  151. udelay(pdata->deactivate_delay_us - delay_us);
  152. }
  153. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  154. setbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
  155. }
  156. static void spi_cs_deactivate(struct udevice *dev)
  157. {
  158. struct udevice *bus = dev->parent;
  159. struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
  160. struct tegra30_spi_priv *priv = dev_get_priv(bus);
  161. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  162. clrbits_le32(&priv->regs->command, SLINK_CMD_CS_VAL);
  163. /* Remember time of this transaction so we can honour the bus delay */
  164. if (pdata->deactivate_delay_us)
  165. priv->last_transaction_us = timer_get_us();
  166. }
  167. static int tegra30_spi_xfer(struct udevice *dev, unsigned int bitlen,
  168. const void *data_out, void *data_in,
  169. unsigned long flags)
  170. {
  171. struct udevice *bus = dev->parent;
  172. struct tegra30_spi_priv *priv = dev_get_priv(bus);
  173. struct spi_regs *regs = priv->regs;
  174. u32 reg, tmpdout, tmpdin = 0;
  175. const u8 *dout = data_out;
  176. u8 *din = data_in;
  177. int num_bytes;
  178. int ret;
  179. debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
  180. __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
  181. if (bitlen % 8)
  182. return -1;
  183. num_bytes = bitlen / 8;
  184. ret = 0;
  185. reg = readl(&regs->status);
  186. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  187. debug("%s entry: STATUS = %08x\n", __func__, reg);
  188. reg = readl(&regs->status2);
  189. writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
  190. debug("%s entry: STATUS2 = %08x\n", __func__, reg);
  191. debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
  192. clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
  193. SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
  194. (spi_chip_select(dev) << SLINK_CMD2_SS_EN_SHIFT));
  195. debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
  196. if (flags & SPI_XFER_BEGIN)
  197. spi_cs_activate(dev);
  198. /* handle data in 32-bit chunks */
  199. while (num_bytes > 0) {
  200. int bytes;
  201. int is_read = 0;
  202. int tm, i;
  203. tmpdout = 0;
  204. bytes = (num_bytes > 4) ? 4 : num_bytes;
  205. if (dout != NULL) {
  206. for (i = 0; i < bytes; ++i)
  207. tmpdout = (tmpdout << 8) | dout[i];
  208. dout += bytes;
  209. }
  210. num_bytes -= bytes;
  211. clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
  212. bytes * 8 - 1);
  213. writel(tmpdout, &regs->tx_fifo);
  214. setbits_le32(&regs->command, SLINK_CMD_GO);
  215. /*
  216. * Wait for SPI transmit FIFO to empty, or to time out.
  217. * The RX FIFO status will be read and cleared last
  218. */
  219. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  220. u32 status;
  221. status = readl(&regs->status);
  222. /* We can exit when we've had both RX and TX activity */
  223. if (is_read && (status & SLINK_STAT_TXF_EMPTY))
  224. break;
  225. if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
  226. SLINK_STAT_RDY)
  227. tm++;
  228. else if (!(status & SLINK_STAT_RXF_EMPTY)) {
  229. tmpdin = readl(&regs->rx_fifo);
  230. is_read = 1;
  231. /* swap bytes read in */
  232. if (din != NULL) {
  233. for (i = bytes - 1; i >= 0; --i) {
  234. din[i] = tmpdin & 0xff;
  235. tmpdin >>= 8;
  236. }
  237. din += bytes;
  238. }
  239. }
  240. }
  241. if (tm >= SPI_TIMEOUT)
  242. ret = tm;
  243. /* clear ACK RDY, etc. bits */
  244. writel(readl(&regs->status), &regs->status);
  245. }
  246. if (flags & SPI_XFER_END)
  247. spi_cs_deactivate(dev);
  248. debug("%s: transfer ended. Value=%08x, status = %08x\n",
  249. __func__, tmpdin, readl(&regs->status));
  250. if (ret) {
  251. printf("%s: timeout during SPI transfer, tm %d\n",
  252. __func__, ret);
  253. return -1;
  254. }
  255. return 0;
  256. }
  257. static int tegra30_spi_set_speed(struct udevice *bus, uint speed)
  258. {
  259. struct tegra_spi_platdata *plat = bus->platdata;
  260. struct tegra30_spi_priv *priv = dev_get_priv(bus);
  261. if (speed > plat->frequency)
  262. speed = plat->frequency;
  263. priv->freq = speed;
  264. debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
  265. return 0;
  266. }
  267. static int tegra30_spi_set_mode(struct udevice *bus, uint mode)
  268. {
  269. struct tegra30_spi_priv *priv = dev_get_priv(bus);
  270. struct spi_regs *regs = priv->regs;
  271. u32 reg;
  272. reg = readl(&regs->command);
  273. /* Set CPOL and CPHA */
  274. reg &= ~(SLINK_CMD_IDLE_SCLK_MASK | SLINK_CMD_CK_SDA);
  275. if (mode & SPI_CPHA)
  276. reg |= SLINK_CMD_CK_SDA;
  277. if (mode & SPI_CPOL)
  278. reg |= SLINK_CMD_IDLE_SCLK_DRIVE_HIGH;
  279. else
  280. reg |= SLINK_CMD_IDLE_SCLK_DRIVE_LOW;
  281. writel(reg, &regs->command);
  282. priv->mode = mode;
  283. debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
  284. return 0;
  285. }
  286. static const struct dm_spi_ops tegra30_spi_ops = {
  287. .claim_bus = tegra30_spi_claim_bus,
  288. .xfer = tegra30_spi_xfer,
  289. .set_speed = tegra30_spi_set_speed,
  290. .set_mode = tegra30_spi_set_mode,
  291. /*
  292. * cs_info is not needed, since we require all chip selects to be
  293. * in the device tree explicitly
  294. */
  295. };
  296. static const struct udevice_id tegra30_spi_ids[] = {
  297. { .compatible = "nvidia,tegra20-slink" },
  298. { }
  299. };
  300. U_BOOT_DRIVER(tegra30_spi) = {
  301. .name = "tegra20_slink",
  302. .id = UCLASS_SPI,
  303. .of_match = tegra30_spi_ids,
  304. .ops = &tegra30_spi_ops,
  305. .ofdata_to_platdata = tegra30_spi_ofdata_to_platdata,
  306. .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
  307. .priv_auto_alloc_size = sizeof(struct tegra30_spi_priv),
  308. .probe = tegra30_spi_probe,
  309. };