tegra20_slink.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 = 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 tegra30_spi_free_slave(struct spi_slave *slave)
  145. {
  146. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  147. free(spi);
  148. }
  149. int tegra30_spi_init(int *node_list, int count)
  150. {
  151. struct tegra_spi_ctrl *ctrl;
  152. int i;
  153. int node = 0;
  154. int found = 0;
  155. for (i = 0; i < count; i++) {
  156. ctrl = &spi_ctrls[i];
  157. node = node_list[i];
  158. ctrl->regs = (struct spi_regs *)fdtdec_get_addr(gd->fdt_blob,
  159. node, "reg");
  160. if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
  161. debug("%s: no slink register found\n", __func__);
  162. continue;
  163. }
  164. ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
  165. "spi-max-frequency", 0);
  166. if (!ctrl->freq) {
  167. debug("%s: no slink max frequency found\n", __func__);
  168. continue;
  169. }
  170. ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
  171. if (ctrl->periph_id == PERIPH_ID_NONE) {
  172. debug("%s: could not decode periph id\n", __func__);
  173. continue;
  174. }
  175. ctrl->valid = 1;
  176. found = 1;
  177. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  178. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  179. }
  180. return !found;
  181. }
  182. int tegra30_spi_claim_bus(struct spi_slave *slave)
  183. {
  184. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  185. struct spi_regs *regs = spi->ctrl->regs;
  186. u32 reg;
  187. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  188. clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
  189. spi->ctrl->freq);
  190. /* Clear stale status here */
  191. reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
  192. SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
  193. writel(reg, &regs->status);
  194. debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
  195. /* Set master mode and sw controlled CS */
  196. reg = readl(&regs->command);
  197. reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
  198. writel(reg, &regs->command);
  199. debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
  200. return 0;
  201. }
  202. void tegra30_spi_cs_activate(struct spi_slave *slave)
  203. {
  204. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  205. struct spi_regs *regs = spi->ctrl->regs;
  206. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  207. setbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  208. }
  209. void tegra30_spi_cs_deactivate(struct spi_slave *slave)
  210. {
  211. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  212. struct spi_regs *regs = spi->ctrl->regs;
  213. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  214. clrbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  215. }
  216. int tegra30_spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  217. const void *data_out, void *data_in, unsigned long flags)
  218. {
  219. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  220. struct spi_regs *regs = spi->ctrl->regs;
  221. u32 reg, tmpdout, tmpdin = 0;
  222. const u8 *dout = data_out;
  223. u8 *din = data_in;
  224. int num_bytes;
  225. int ret;
  226. debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
  227. __func__, slave->bus, slave->cs, dout, din, bitlen);
  228. if (bitlen % 8)
  229. return -1;
  230. num_bytes = bitlen / 8;
  231. ret = 0;
  232. reg = readl(&regs->status);
  233. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  234. debug("%s entry: STATUS = %08x\n", __func__, reg);
  235. reg = readl(&regs->status2);
  236. writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
  237. debug("%s entry: STATUS2 = %08x\n", __func__, reg);
  238. debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
  239. clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
  240. SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
  241. (slave->cs << SLINK_CMD2_SS_EN_SHIFT));
  242. debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
  243. if (flags & SPI_XFER_BEGIN)
  244. spi_cs_activate(slave);
  245. /* handle data in 32-bit chunks */
  246. while (num_bytes > 0) {
  247. int bytes;
  248. int is_read = 0;
  249. int tm, i;
  250. tmpdout = 0;
  251. bytes = (num_bytes > 4) ? 4 : num_bytes;
  252. if (dout != NULL) {
  253. for (i = 0; i < bytes; ++i)
  254. tmpdout = (tmpdout << 8) | dout[i];
  255. dout += bytes;
  256. }
  257. num_bytes -= bytes;
  258. clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
  259. bytes * 8 - 1);
  260. writel(tmpdout, &regs->tx_fifo);
  261. setbits_le32(&regs->command, SLINK_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 & SLINK_STAT_TXF_EMPTY))
  271. break;
  272. if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
  273. SLINK_STAT_RDY)
  274. tm++;
  275. else if (!(status & SLINK_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("%s: transfer ended. Value=%08x, status = %08x\n",
  296. __func__, tmpdin, readl(&regs->status));
  297. if (ret) {
  298. printf("%s: timeout during SPI transfer, tm %d\n",
  299. __func__, ret);
  300. return -1;
  301. }
  302. return 0;
  303. }