tegra20_sflash.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2010-2013 NVIDIA Corporation
  3. * With help from the mpc8xxx SPI driver
  4. * With more help from omap3_spi SPI driver
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <malloc.h>
  26. #include <asm/io.h>
  27. #include <asm/gpio.h>
  28. #include <asm/arch/clock.h>
  29. #include <asm/arch/pinmux.h>
  30. #include <asm/arch-tegra/clk_rst.h>
  31. #include <asm/arch-tegra20/tegra20_sflash.h>
  32. #include <spi.h>
  33. #include <fdtdec.h>
  34. DECLARE_GLOBAL_DATA_PTR;
  35. #define SPI_CMD_GO (1 << 30)
  36. #define SPI_CMD_ACTIVE_SCLK_SHIFT 26
  37. #define SPI_CMD_ACTIVE_SCLK_MASK (3 << SPI_CMD_ACTIVE_SCLK_SHIFT)
  38. #define SPI_CMD_CK_SDA (1 << 21)
  39. #define SPI_CMD_ACTIVE_SDA_SHIFT 18
  40. #define SPI_CMD_ACTIVE_SDA_MASK (3 << SPI_CMD_ACTIVE_SDA_SHIFT)
  41. #define SPI_CMD_CS_POL (1 << 16)
  42. #define SPI_CMD_TXEN (1 << 15)
  43. #define SPI_CMD_RXEN (1 << 14)
  44. #define SPI_CMD_CS_VAL (1 << 13)
  45. #define SPI_CMD_CS_SOFT (1 << 12)
  46. #define SPI_CMD_CS_DELAY (1 << 9)
  47. #define SPI_CMD_CS3_EN (1 << 8)
  48. #define SPI_CMD_CS2_EN (1 << 7)
  49. #define SPI_CMD_CS1_EN (1 << 6)
  50. #define SPI_CMD_CS0_EN (1 << 5)
  51. #define SPI_CMD_BIT_LENGTH (1 << 4)
  52. #define SPI_CMD_BIT_LENGTH_MASK 0x0000001F
  53. #define SPI_STAT_BSY (1 << 31)
  54. #define SPI_STAT_RDY (1 << 30)
  55. #define SPI_STAT_RXF_FLUSH (1 << 29)
  56. #define SPI_STAT_TXF_FLUSH (1 << 28)
  57. #define SPI_STAT_RXF_UNR (1 << 27)
  58. #define SPI_STAT_TXF_OVF (1 << 26)
  59. #define SPI_STAT_RXF_EMPTY (1 << 25)
  60. #define SPI_STAT_RXF_FULL (1 << 24)
  61. #define SPI_STAT_TXF_EMPTY (1 << 23)
  62. #define SPI_STAT_TXF_FULL (1 << 22)
  63. #define SPI_STAT_SEL_TXRX_N (1 << 16)
  64. #define SPI_STAT_CUR_BLKCNT (1 << 15)
  65. #define SPI_TIMEOUT 1000
  66. #define TEGRA_SPI_MAX_FREQ 52000000
  67. struct spi_regs {
  68. u32 command; /* SPI_COMMAND_0 register */
  69. u32 status; /* SPI_STATUS_0 register */
  70. u32 rx_cmp; /* SPI_RX_CMP_0 register */
  71. u32 dma_ctl; /* SPI_DMA_CTL_0 register */
  72. u32 tx_fifo; /* SPI_TX_FIFO_0 register */
  73. u32 rsvd[3]; /* offsets 0x14 to 0x1F reserved */
  74. u32 rx_fifo; /* SPI_RX_FIFO_0 register */
  75. };
  76. struct tegra_spi_ctrl {
  77. struct spi_regs *regs;
  78. unsigned int freq;
  79. unsigned int mode;
  80. int periph_id;
  81. int valid;
  82. };
  83. struct tegra_spi_slave {
  84. struct spi_slave slave;
  85. struct tegra_spi_ctrl *ctrl;
  86. };
  87. /* tegra20 only supports one SFLASH controller */
  88. static struct tegra_spi_ctrl spi_ctrls[1];
  89. static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
  90. {
  91. return container_of(slave, struct tegra_spi_slave, slave);
  92. }
  93. int tegra20_spi_cs_is_valid(unsigned int bus, unsigned int cs)
  94. {
  95. /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
  96. if (bus != 0 || cs != 0)
  97. return 0;
  98. else
  99. return 1;
  100. }
  101. struct spi_slave *tegra20_spi_setup_slave(unsigned int bus, unsigned int cs,
  102. unsigned int max_hz, unsigned int mode)
  103. {
  104. struct tegra_spi_slave *spi;
  105. if (!spi_cs_is_valid(bus, cs)) {
  106. printf("SPI error: unsupported bus %d / chip select %d\n",
  107. bus, cs);
  108. return NULL;
  109. }
  110. if (max_hz > TEGRA_SPI_MAX_FREQ) {
  111. printf("SPI error: unsupported frequency %d Hz. Max frequency"
  112. " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
  113. return NULL;
  114. }
  115. spi = spi_alloc_slave(struct tegra_spi_slave, bus, cs);
  116. if (!spi) {
  117. printf("SPI error: malloc of SPI structure failed\n");
  118. return NULL;
  119. }
  120. spi->ctrl = &spi_ctrls[bus];
  121. if (!spi->ctrl) {
  122. printf("SPI error: could not find controller for bus %d\n",
  123. bus);
  124. return NULL;
  125. }
  126. if (max_hz < spi->ctrl->freq) {
  127. debug("%s: limiting frequency from %u to %u\n", __func__,
  128. spi->ctrl->freq, max_hz);
  129. spi->ctrl->freq = max_hz;
  130. }
  131. spi->ctrl->mode = mode;
  132. return &spi->slave;
  133. }
  134. void tegra20_spi_free_slave(struct spi_slave *slave)
  135. {
  136. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  137. free(spi);
  138. }
  139. int tegra20_spi_init(int *node_list, int count)
  140. {
  141. struct tegra_spi_ctrl *ctrl;
  142. int i;
  143. int node = 0;
  144. int found = 0;
  145. for (i = 0; i < count; i++) {
  146. ctrl = &spi_ctrls[i];
  147. node = node_list[i];
  148. ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
  149. node, "reg");
  150. if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
  151. debug("%s: no slink register found\n", __func__);
  152. continue;
  153. }
  154. ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
  155. "spi-max-frequency", 0);
  156. if (!ctrl->freq) {
  157. debug("%s: no slink max frequency found\n", __func__);
  158. continue;
  159. }
  160. ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
  161. if (ctrl->periph_id == PERIPH_ID_NONE) {
  162. debug("%s: could not decode periph id\n", __func__);
  163. continue;
  164. }
  165. ctrl->valid = 1;
  166. found = 1;
  167. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  168. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  169. }
  170. return !found;
  171. }
  172. int tegra20_spi_claim_bus(struct spi_slave *slave)
  173. {
  174. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  175. struct spi_regs *regs = spi->ctrl->regs;
  176. u32 reg;
  177. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  178. clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
  179. spi->ctrl->freq);
  180. /* Clear stale status here */
  181. reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
  182. SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
  183. writel(reg, &regs->status);
  184. debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
  185. /*
  186. * Use sw-controlled CS, so we can clock in data after ReadID, etc.
  187. */
  188. reg = (spi->ctrl->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
  189. if (spi->ctrl->mode & 2)
  190. reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
  191. clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
  192. SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
  193. debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
  194. /*
  195. * SPI pins on Tegra20 are muxed - change pinmux later due to UART
  196. * issue.
  197. */
  198. pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
  199. pinmux_tristate_disable(PINGRP_LSPI);
  200. pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
  201. return 0;
  202. }
  203. void tegra20_spi_cs_activate(struct spi_slave *slave)
  204. {
  205. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  206. struct spi_regs *regs = spi->ctrl->regs;
  207. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  208. setbits_le32(&regs->command, SPI_CMD_CS_VAL);
  209. }
  210. void tegra20_spi_cs_deactivate(struct spi_slave *slave)
  211. {
  212. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  213. struct spi_regs *regs = spi->ctrl->regs;
  214. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  215. clrbits_le32(&regs->command, SPI_CMD_CS_VAL);
  216. }
  217. int tegra20_spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  218. const void *data_out, void *data_in, unsigned long flags)
  219. {
  220. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  221. struct spi_regs *regs = spi->ctrl->regs;
  222. u32 reg, tmpdout, tmpdin = 0;
  223. const u8 *dout = data_out;
  224. u8 *din = data_in;
  225. int num_bytes;
  226. int ret;
  227. debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  228. slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
  229. if (bitlen % 8)
  230. return -1;
  231. num_bytes = bitlen / 8;
  232. ret = 0;
  233. reg = readl(&regs->status);
  234. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  235. debug("spi_xfer entry: STATUS = %08x\n", reg);
  236. reg = readl(&regs->command);
  237. reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
  238. writel(reg, &regs->command);
  239. debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
  240. if (flags & SPI_XFER_BEGIN)
  241. spi_cs_activate(slave);
  242. /* handle data in 32-bit chunks */
  243. while (num_bytes > 0) {
  244. int bytes;
  245. int is_read = 0;
  246. int tm, i;
  247. tmpdout = 0;
  248. bytes = (num_bytes > 4) ? 4 : num_bytes;
  249. if (dout != NULL) {
  250. for (i = 0; i < bytes; ++i)
  251. tmpdout = (tmpdout << 8) | dout[i];
  252. }
  253. num_bytes -= bytes;
  254. if (dout)
  255. dout += bytes;
  256. clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
  257. bytes * 8 - 1);
  258. writel(tmpdout, &regs->tx_fifo);
  259. setbits_le32(&regs->command, SPI_CMD_GO);
  260. /*
  261. * Wait for SPI transmit FIFO to empty, or to time out.
  262. * The RX FIFO status will be read and cleared last
  263. */
  264. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  265. u32 status;
  266. status = readl(&regs->status);
  267. /* We can exit when we've had both RX and TX activity */
  268. if (is_read && (status & SPI_STAT_TXF_EMPTY))
  269. break;
  270. if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
  271. SPI_STAT_RDY)
  272. tm++;
  273. else if (!(status & SPI_STAT_RXF_EMPTY)) {
  274. tmpdin = readl(&regs->rx_fifo);
  275. is_read = 1;
  276. /* swap bytes read in */
  277. if (din != NULL) {
  278. for (i = bytes - 1; i >= 0; --i) {
  279. din[i] = tmpdin & 0xff;
  280. tmpdin >>= 8;
  281. }
  282. din += bytes;
  283. }
  284. }
  285. }
  286. if (tm >= SPI_TIMEOUT)
  287. ret = tm;
  288. /* clear ACK RDY, etc. bits */
  289. writel(readl(&regs->status), &regs->status);
  290. }
  291. if (flags & SPI_XFER_END)
  292. spi_cs_deactivate(slave);
  293. debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
  294. tmpdin, readl(&regs->status));
  295. if (ret) {
  296. printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
  297. return -1;
  298. }
  299. return 0;
  300. }