tegra20_slink.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <malloc.h>
  25. #include <asm/io.h>
  26. #include <asm/gpio.h>
  27. #include <asm/arch/clock.h>
  28. #include <asm/arch-tegra/clk_rst.h>
  29. #include <asm/arch-tegra20/tegra20_slink.h>
  30. #include <spi.h>
  31. #include <fdtdec.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. /* COMMAND */
  34. #define SLINK_CMD_ENB (1 << 31)
  35. #define SLINK_CMD_GO (1 << 30)
  36. #define SLINK_CMD_M_S (1 << 28)
  37. #define SLINK_CMD_CK_SDA (1 << 21)
  38. #define SLINK_CMD_CS_POL (1 << 13)
  39. #define SLINK_CMD_CS_VAL (1 << 12)
  40. #define SLINK_CMD_CS_SOFT (1 << 11)
  41. #define SLINK_CMD_BIT_LENGTH (1 << 4)
  42. #define SLINK_CMD_BIT_LENGTH_MASK 0x0000001F
  43. /* COMMAND2 */
  44. #define SLINK_CMD2_TXEN (1 << 30)
  45. #define SLINK_CMD2_RXEN (1 << 31)
  46. #define SLINK_CMD2_SS_EN (1 << 18)
  47. #define SLINK_CMD2_SS_EN_SHIFT 18
  48. #define SLINK_CMD2_SS_EN_MASK 0x000C0000
  49. #define SLINK_CMD2_CS_ACTIVE_BETWEEN (1 << 17)
  50. /* STATUS */
  51. #define SLINK_STAT_BSY (1 << 31)
  52. #define SLINK_STAT_RDY (1 << 30)
  53. #define SLINK_STAT_ERR (1 << 29)
  54. #define SLINK_STAT_RXF_FLUSH (1 << 27)
  55. #define SLINK_STAT_TXF_FLUSH (1 << 26)
  56. #define SLINK_STAT_RXF_OVF (1 << 25)
  57. #define SLINK_STAT_TXF_UNR (1 << 24)
  58. #define SLINK_STAT_RXF_EMPTY (1 << 23)
  59. #define SLINK_STAT_RXF_FULL (1 << 22)
  60. #define SLINK_STAT_TXF_EMPTY (1 << 21)
  61. #define SLINK_STAT_TXF_FULL (1 << 20)
  62. #define SLINK_STAT_TXF_OVF (1 << 19)
  63. #define SLINK_STAT_RXF_UNR (1 << 18)
  64. #define SLINK_STAT_CUR_BLKCNT (1 << 15)
  65. /* STATUS2 */
  66. #define SLINK_STAT2_RXF_FULL_CNT (1 << 16)
  67. #define SLINK_STAT2_TXF_FULL_CNT (1 << 0)
  68. #define SPI_TIMEOUT 1000
  69. #define TEGRA_SPI_MAX_FREQ 52000000
  70. struct spi_regs {
  71. u32 command; /* SLINK_COMMAND_0 register */
  72. u32 command2; /* SLINK_COMMAND2_0 reg */
  73. u32 status; /* SLINK_STATUS_0 register */
  74. u32 reserved; /* Reserved offset 0C */
  75. u32 mas_data; /* SLINK_MAS_DATA_0 reg */
  76. u32 slav_data; /* SLINK_SLAVE_DATA_0 reg */
  77. u32 dma_ctl; /* SLINK_DMA_CTL_0 register */
  78. u32 status2; /* SLINK_STATUS2_0 reg */
  79. u32 rsvd[56]; /* 0x20 to 0xFF reserved */
  80. u32 tx_fifo; /* SLINK_TX_FIFO_0 reg off 100h */
  81. u32 rsvd2[31]; /* 0x104 to 0x17F reserved */
  82. u32 rx_fifo; /* SLINK_RX_FIFO_0 reg off 180h */
  83. };
  84. struct tegra_spi_ctrl {
  85. struct spi_regs *regs;
  86. unsigned int freq;
  87. unsigned int mode;
  88. int periph_id;
  89. int valid;
  90. };
  91. struct tegra_spi_slave {
  92. struct spi_slave slave;
  93. struct tegra_spi_ctrl *ctrl;
  94. };
  95. static struct tegra_spi_ctrl spi_ctrls[CONFIG_TEGRA_SLINK_CTRLS];
  96. static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
  97. {
  98. return container_of(slave, struct tegra_spi_slave, slave);
  99. }
  100. int tegra30_spi_cs_is_valid(unsigned int bus, unsigned int cs)
  101. {
  102. if (bus >= CONFIG_TEGRA_SLINK_CTRLS || cs > 3 || !spi_ctrls[bus].valid)
  103. return 0;
  104. else
  105. return 1;
  106. }
  107. struct spi_slave *tegra30_spi_setup_slave(unsigned int bus, unsigned int cs,
  108. unsigned int max_hz, unsigned int mode)
  109. {
  110. struct tegra_spi_slave *spi;
  111. debug("%s: bus: %u, cs: %u, max_hz: %u, mode: %u\n", __func__,
  112. bus, cs, max_hz, mode);
  113. if (!spi_cs_is_valid(bus, cs)) {
  114. printf("SPI error: unsupported bus %d / chip select %d\n",
  115. bus, cs);
  116. return NULL;
  117. }
  118. if (max_hz > TEGRA_SPI_MAX_FREQ) {
  119. printf("SPI error: unsupported frequency %d Hz. Max frequency"
  120. " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
  121. return NULL;
  122. }
  123. spi = spi_alloc_slave(struct tegra_spi_slave, bus, cs);
  124. if (!spi) {
  125. printf("SPI error: malloc of SPI structure failed\n");
  126. return NULL;
  127. }
  128. spi->ctrl = &spi_ctrls[bus];
  129. if (!spi->ctrl) {
  130. printf("SPI error: could not find controller for bus %d\n",
  131. bus);
  132. return NULL;
  133. }
  134. if (max_hz < spi->ctrl->freq) {
  135. debug("%s: limiting frequency from %u to %u\n", __func__,
  136. spi->ctrl->freq, max_hz);
  137. spi->ctrl->freq = max_hz;
  138. }
  139. spi->ctrl->mode = mode;
  140. return &spi->slave;
  141. }
  142. void tegra30_spi_free_slave(struct spi_slave *slave)
  143. {
  144. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  145. free(spi);
  146. }
  147. int tegra30_spi_init(int *node_list, int count)
  148. {
  149. struct tegra_spi_ctrl *ctrl;
  150. int i;
  151. int node = 0;
  152. int found = 0;
  153. for (i = 0; i < count; i++) {
  154. ctrl = &spi_ctrls[i];
  155. node = node_list[i];
  156. ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
  157. node, "reg");
  158. if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
  159. debug("%s: no slink register found\n", __func__);
  160. continue;
  161. }
  162. ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
  163. "spi-max-frequency", 0);
  164. if (!ctrl->freq) {
  165. debug("%s: no slink max frequency found\n", __func__);
  166. continue;
  167. }
  168. ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
  169. if (ctrl->periph_id == PERIPH_ID_NONE) {
  170. debug("%s: could not decode periph id\n", __func__);
  171. continue;
  172. }
  173. ctrl->valid = 1;
  174. found = 1;
  175. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  176. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  177. }
  178. return !found;
  179. }
  180. int tegra30_spi_claim_bus(struct spi_slave *slave)
  181. {
  182. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  183. struct spi_regs *regs = spi->ctrl->regs;
  184. u32 reg;
  185. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  186. clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
  187. spi->ctrl->freq);
  188. /* Clear stale status here */
  189. reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
  190. SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
  191. writel(reg, &regs->status);
  192. debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
  193. /* Set master mode and sw controlled CS */
  194. reg = readl(&regs->command);
  195. reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
  196. writel(reg, &regs->command);
  197. debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
  198. return 0;
  199. }
  200. void tegra30_spi_cs_activate(struct spi_slave *slave)
  201. {
  202. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  203. struct spi_regs *regs = spi->ctrl->regs;
  204. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  205. setbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  206. }
  207. void tegra30_spi_cs_deactivate(struct spi_slave *slave)
  208. {
  209. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  210. struct spi_regs *regs = spi->ctrl->regs;
  211. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  212. clrbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  213. }
  214. int tegra30_spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  215. const void *data_out, void *data_in, unsigned long flags)
  216. {
  217. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  218. struct spi_regs *regs = spi->ctrl->regs;
  219. u32 reg, tmpdout, tmpdin = 0;
  220. const u8 *dout = data_out;
  221. u8 *din = data_in;
  222. int num_bytes;
  223. int ret;
  224. debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
  225. __func__, slave->bus, slave->cs, dout, din, bitlen);
  226. if (bitlen % 8)
  227. return -1;
  228. num_bytes = bitlen / 8;
  229. ret = 0;
  230. reg = readl(&regs->status);
  231. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  232. debug("%s entry: STATUS = %08x\n", __func__, reg);
  233. reg = readl(&regs->status2);
  234. writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
  235. debug("%s entry: STATUS2 = %08x\n", __func__, reg);
  236. debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
  237. clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
  238. SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
  239. (slave->cs << SLINK_CMD2_SS_EN_SHIFT));
  240. debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
  241. if (flags & SPI_XFER_BEGIN)
  242. spi_cs_activate(slave);
  243. /* handle data in 32-bit chunks */
  244. while (num_bytes > 0) {
  245. int bytes;
  246. int is_read = 0;
  247. int tm, i;
  248. tmpdout = 0;
  249. bytes = (num_bytes > 4) ? 4 : num_bytes;
  250. if (dout != NULL) {
  251. for (i = 0; i < bytes; ++i)
  252. tmpdout = (tmpdout << 8) | dout[i];
  253. dout += bytes;
  254. }
  255. num_bytes -= bytes;
  256. clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
  257. bytes * 8 - 1);
  258. writel(tmpdout, &regs->tx_fifo);
  259. setbits_le32(&regs->command, SLINK_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 & SLINK_STAT_TXF_EMPTY))
  269. break;
  270. if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
  271. SLINK_STAT_RDY)
  272. tm++;
  273. else if (!(status & SLINK_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("%s: transfer ended. Value=%08x, status = %08x\n",
  294. __func__, tmpdin, readl(&regs->status));
  295. if (ret) {
  296. printf("%s: timeout during SPI transfer, tm %d\n",
  297. __func__, ret);
  298. return -1;
  299. }
  300. return 0;
  301. }