tegra20_slink.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 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 *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 = malloc(sizeof(struct tegra_spi_slave));
  124. if (!spi) {
  125. printf("SPI error: malloc of SPI structure failed\n");
  126. return NULL;
  127. }
  128. spi->slave.bus = bus;
  129. spi->slave.cs = cs;
  130. spi->ctrl = &spi_ctrls[bus];
  131. if (!spi->ctrl) {
  132. printf("SPI error: could not find controller for bus %d\n",
  133. bus);
  134. return NULL;
  135. }
  136. if (max_hz < spi->ctrl->freq) {
  137. debug("%s: limiting frequency from %u to %u\n", __func__,
  138. spi->ctrl->freq, max_hz);
  139. spi->ctrl->freq = max_hz;
  140. }
  141. spi->ctrl->mode = mode;
  142. return &spi->slave;
  143. }
  144. void spi_free_slave(struct spi_slave *slave)
  145. {
  146. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  147. free(spi);
  148. }
  149. void spi_init(void)
  150. {
  151. struct tegra_spi_ctrl *ctrl;
  152. int i;
  153. int node = 0;
  154. int count;
  155. int node_list[CONFIG_TEGRA_SLINK_CTRLS];
  156. count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
  157. COMPAT_NVIDIA_TEGRA20_SLINK,
  158. node_list,
  159. CONFIG_TEGRA_SLINK_CTRLS);
  160. for (i = 0; i < count; i++) {
  161. ctrl = &spi_ctrls[i];
  162. node = node_list[i];
  163. ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
  164. node, "reg");
  165. if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
  166. debug("%s: no slink register found\n", __func__);
  167. continue;
  168. }
  169. ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
  170. "spi-max-frequency", 0);
  171. if (!ctrl->freq) {
  172. debug("%s: no slink max frequency found\n", __func__);
  173. continue;
  174. }
  175. ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
  176. if (ctrl->periph_id == PERIPH_ID_NONE) {
  177. debug("%s: could not decode periph id\n", __func__);
  178. continue;
  179. }
  180. ctrl->valid = 1;
  181. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  182. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  183. }
  184. }
  185. int spi_claim_bus(struct spi_slave *slave)
  186. {
  187. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  188. struct spi_regs *regs = spi->ctrl->regs;
  189. u32 reg;
  190. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  191. clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
  192. spi->ctrl->freq);
  193. /* Clear stale status here */
  194. reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
  195. SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
  196. writel(reg, &regs->status);
  197. debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
  198. /* Set master mode and sw controlled CS */
  199. reg = readl(&regs->command);
  200. reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
  201. writel(reg, &regs->command);
  202. debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
  203. return 0;
  204. }
  205. void spi_release_bus(struct spi_slave *slave)
  206. {
  207. }
  208. void spi_cs_activate(struct spi_slave *slave)
  209. {
  210. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  211. struct spi_regs *regs = spi->ctrl->regs;
  212. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  213. setbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  214. }
  215. void spi_cs_deactivate(struct spi_slave *slave)
  216. {
  217. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  218. struct spi_regs *regs = spi->ctrl->regs;
  219. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  220. clrbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  221. }
  222. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  223. const void *data_out, void *data_in, unsigned long flags)
  224. {
  225. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  226. struct spi_regs *regs = spi->ctrl->regs;
  227. u32 reg, tmpdout, tmpdin = 0;
  228. const u8 *dout = data_out;
  229. u8 *din = data_in;
  230. int num_bytes;
  231. int ret;
  232. debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
  233. __func__, slave->bus, slave->cs, dout, din, bitlen);
  234. if (bitlen % 8)
  235. return -1;
  236. num_bytes = bitlen / 8;
  237. ret = 0;
  238. reg = readl(&regs->status);
  239. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  240. debug("%s entry: STATUS = %08x\n", __func__, reg);
  241. reg = readl(&regs->status2);
  242. writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
  243. debug("%s entry: STATUS2 = %08x\n", __func__, reg);
  244. debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
  245. clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
  246. SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
  247. (slave->cs << SLINK_CMD2_SS_EN_SHIFT));
  248. debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
  249. if (flags & SPI_XFER_BEGIN)
  250. spi_cs_activate(slave);
  251. /* handle data in 32-bit chunks */
  252. while (num_bytes > 0) {
  253. int bytes;
  254. int is_read = 0;
  255. int tm, i;
  256. tmpdout = 0;
  257. bytes = (num_bytes > 4) ? 4 : num_bytes;
  258. if (dout != NULL) {
  259. for (i = 0; i < bytes; ++i)
  260. tmpdout = (tmpdout << 8) | dout[i];
  261. dout += bytes;
  262. }
  263. num_bytes -= bytes;
  264. clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
  265. bytes * 8 - 1);
  266. writel(tmpdout, &regs->tx_fifo);
  267. setbits_le32(&regs->command, SLINK_CMD_GO);
  268. /*
  269. * Wait for SPI transmit FIFO to empty, or to time out.
  270. * The RX FIFO status will be read and cleared last
  271. */
  272. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  273. u32 status;
  274. status = readl(&regs->status);
  275. /* We can exit when we've had both RX and TX activity */
  276. if (is_read && (status & SLINK_STAT_TXF_EMPTY))
  277. break;
  278. if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
  279. SLINK_STAT_RDY)
  280. tm++;
  281. else if (!(status & SLINK_STAT_RXF_EMPTY)) {
  282. tmpdin = readl(&regs->rx_fifo);
  283. is_read = 1;
  284. /* swap bytes read in */
  285. if (din != NULL) {
  286. for (i = bytes - 1; i >= 0; --i) {
  287. din[i] = tmpdin & 0xff;
  288. tmpdin >>= 8;
  289. }
  290. din += bytes;
  291. }
  292. }
  293. }
  294. if (tm >= SPI_TIMEOUT)
  295. ret = tm;
  296. /* clear ACK RDY, etc. bits */
  297. writel(readl(&regs->status), &regs->status);
  298. }
  299. if (flags & SPI_XFER_END)
  300. spi_cs_deactivate(slave);
  301. debug("%s: transfer ended. Value=%08x, status = %08x\n",
  302. __func__, tmpdin, readl(&regs->status));
  303. if (ret) {
  304. printf("%s: timeout during SPI transfer, tm %d\n",
  305. __func__, ret);
  306. return -1;
  307. }
  308. return 0;
  309. }