tegra20_slink.c 10 KB

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