tegra20_sflash.c 9.3 KB

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