tegra20_sflash.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 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 *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 = malloc(sizeof(struct tegra_spi_slave));
  116. if (!spi) {
  117. printf("SPI error: malloc of SPI structure failed\n");
  118. return NULL;
  119. }
  120. spi->slave.bus = bus;
  121. spi->slave.cs = cs;
  122. spi->ctrl = &spi_ctrls[bus];
  123. if (!spi->ctrl) {
  124. printf("SPI error: could not find controller for bus %d\n",
  125. bus);
  126. return NULL;
  127. }
  128. if (max_hz < spi->ctrl->freq) {
  129. debug("%s: limiting frequency from %u to %u\n", __func__,
  130. spi->ctrl->freq, max_hz);
  131. spi->ctrl->freq = max_hz;
  132. }
  133. spi->ctrl->mode = mode;
  134. return &spi->slave;
  135. }
  136. void spi_free_slave(struct spi_slave *slave)
  137. {
  138. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  139. free(spi);
  140. }
  141. void spi_init(void)
  142. {
  143. struct tegra_spi_ctrl *ctrl;
  144. int i;
  145. int node = 0;
  146. int count;
  147. int node_list[1];
  148. count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
  149. COMPAT_NVIDIA_TEGRA20_SFLASH,
  150. node_list,
  151. 1);
  152. for (i = 0; i < count; i++) {
  153. ctrl = &spi_ctrls[i];
  154. node = node_list[i];
  155. ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
  156. node, "reg");
  157. if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
  158. debug("%s: no slink register found\n", __func__);
  159. continue;
  160. }
  161. ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
  162. "spi-max-frequency", 0);
  163. if (!ctrl->freq) {
  164. debug("%s: no slink max frequency found\n", __func__);
  165. continue;
  166. }
  167. ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
  168. if (ctrl->periph_id == PERIPH_ID_NONE) {
  169. debug("%s: could not decode periph id\n", __func__);
  170. continue;
  171. }
  172. ctrl->valid = 1;
  173. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  174. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  175. }
  176. }
  177. int spi_claim_bus(struct spi_slave *slave)
  178. {
  179. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  180. struct spi_regs *regs = spi->ctrl->regs;
  181. u32 reg;
  182. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  183. clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
  184. spi->ctrl->freq);
  185. /* Clear stale status here */
  186. reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
  187. SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
  188. writel(reg, &regs->status);
  189. debug("spi_init: STATUS = %08x\n", readl(&regs->status));
  190. /*
  191. * Use sw-controlled CS, so we can clock in data after ReadID, etc.
  192. */
  193. reg = (spi->ctrl->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
  194. if (spi->ctrl->mode & 2)
  195. reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
  196. clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
  197. SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
  198. debug("spi_init: COMMAND = %08x\n", readl(&regs->command));
  199. /*
  200. * SPI pins on Tegra20 are muxed - change pinmux later due to UART
  201. * issue.
  202. */
  203. pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
  204. pinmux_tristate_disable(PINGRP_LSPI);
  205. pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH);
  206. return 0;
  207. }
  208. void spi_release_bus(struct spi_slave *slave)
  209. {
  210. /*
  211. * We can't release UART_DISABLE and set pinmux to UART4 here since
  212. * some code (e,g, spi_flash_probe) uses printf() while the SPI
  213. * bus is held. That is arguably bad, but it has the advantage of
  214. * already being in the source tree.
  215. */
  216. }
  217. void spi_cs_activate(struct spi_slave *slave)
  218. {
  219. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  220. struct spi_regs *regs = spi->ctrl->regs;
  221. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  222. setbits_le32(&regs->command, SPI_CMD_CS_VAL);
  223. }
  224. void spi_cs_deactivate(struct spi_slave *slave)
  225. {
  226. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  227. struct spi_regs *regs = spi->ctrl->regs;
  228. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  229. clrbits_le32(&regs->command, SPI_CMD_CS_VAL);
  230. }
  231. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  232. const void *data_out, void *data_in, unsigned long flags)
  233. {
  234. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  235. struct spi_regs *regs = spi->ctrl->regs;
  236. u32 reg, tmpdout, tmpdin = 0;
  237. const u8 *dout = data_out;
  238. u8 *din = data_in;
  239. int num_bytes;
  240. int ret;
  241. debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  242. slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
  243. if (bitlen % 8)
  244. return -1;
  245. num_bytes = bitlen / 8;
  246. ret = 0;
  247. reg = readl(&regs->status);
  248. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  249. debug("spi_xfer entry: STATUS = %08x\n", reg);
  250. reg = readl(&regs->command);
  251. reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
  252. writel(reg, &regs->command);
  253. debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
  254. if (flags & SPI_XFER_BEGIN)
  255. spi_cs_activate(slave);
  256. /* handle data in 32-bit chunks */
  257. while (num_bytes > 0) {
  258. int bytes;
  259. int is_read = 0;
  260. int tm, i;
  261. tmpdout = 0;
  262. bytes = (num_bytes > 4) ? 4 : num_bytes;
  263. if (dout != NULL) {
  264. for (i = 0; i < bytes; ++i)
  265. tmpdout = (tmpdout << 8) | dout[i];
  266. }
  267. num_bytes -= bytes;
  268. if (dout)
  269. dout += bytes;
  270. clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
  271. bytes * 8 - 1);
  272. writel(tmpdout, &regs->tx_fifo);
  273. setbits_le32(&regs->command, SPI_CMD_GO);
  274. /*
  275. * Wait for SPI transmit FIFO to empty, or to time out.
  276. * The RX FIFO status will be read and cleared last
  277. */
  278. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  279. u32 status;
  280. status = readl(&regs->status);
  281. /* We can exit when we've had both RX and TX activity */
  282. if (is_read && (status & SPI_STAT_TXF_EMPTY))
  283. break;
  284. if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
  285. SPI_STAT_RDY)
  286. tm++;
  287. else if (!(status & SPI_STAT_RXF_EMPTY)) {
  288. tmpdin = readl(&regs->rx_fifo);
  289. is_read = 1;
  290. /* swap bytes read in */
  291. if (din != NULL) {
  292. for (i = bytes - 1; i >= 0; --i) {
  293. din[i] = tmpdin & 0xff;
  294. tmpdin >>= 8;
  295. }
  296. din += bytes;
  297. }
  298. }
  299. }
  300. if (tm >= SPI_TIMEOUT)
  301. ret = tm;
  302. /* clear ACK RDY, etc. bits */
  303. writel(readl(&regs->status), &regs->status);
  304. }
  305. if (flags & SPI_XFER_END)
  306. spi_cs_deactivate(slave);
  307. debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
  308. tmpdin, readl(&regs->status));
  309. if (ret) {
  310. printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
  311. return -1;
  312. }
  313. return 0;
  314. }