tegra_slink.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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-tegra/tegra_slink.h>
  30. #include <spi.h>
  31. #include <fdtdec.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. struct tegra_spi_ctrl {
  34. struct slink_tegra *regs;
  35. unsigned int freq;
  36. unsigned int mode;
  37. int periph_id;
  38. int valid;
  39. };
  40. struct tegra_spi_slave {
  41. struct spi_slave slave;
  42. struct tegra_spi_ctrl *ctrl;
  43. };
  44. static struct tegra_spi_ctrl spi_ctrls[CONFIG_TEGRA_SLINK_CTRLS];
  45. static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
  46. {
  47. return container_of(slave, struct tegra_spi_slave, slave);
  48. }
  49. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  50. {
  51. if (bus >= CONFIG_TEGRA_SLINK_CTRLS || cs > 3 || !spi_ctrls[bus].valid)
  52. return 0;
  53. else
  54. return 1;
  55. }
  56. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  57. unsigned int max_hz, unsigned int mode)
  58. {
  59. struct tegra_spi_slave *spi;
  60. debug("%s: bus: %u, cs: %u, max_hz: %u, mode: %u\n", __func__,
  61. bus, cs, max_hz, mode);
  62. if (!spi_cs_is_valid(bus, cs)) {
  63. printf("SPI error: unsupported bus %d / chip select %d\n",
  64. bus, cs);
  65. return NULL;
  66. }
  67. if (max_hz > TEGRA_SPI_MAX_FREQ) {
  68. printf("SPI error: unsupported frequency %d Hz. Max frequency"
  69. " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
  70. return NULL;
  71. }
  72. spi = spi_alloc_slave(struct tegra_spi_slave, bus, cs);
  73. if (!spi) {
  74. printf("SPI error: malloc of SPI structure failed\n");
  75. return NULL;
  76. }
  77. spi->ctrl = &spi_ctrls[bus];
  78. if (!spi->ctrl) {
  79. printf("SPI error: could not find controller for bus %d\n",
  80. bus);
  81. return NULL;
  82. }
  83. if (max_hz < spi->ctrl->freq) {
  84. debug("%s: limiting frequency from %u to %u\n", __func__,
  85. spi->ctrl->freq, max_hz);
  86. spi->ctrl->freq = max_hz;
  87. }
  88. spi->ctrl->mode = mode;
  89. return &spi->slave;
  90. }
  91. void spi_free_slave(struct spi_slave *slave)
  92. {
  93. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  94. free(spi);
  95. }
  96. void spi_init(void)
  97. {
  98. struct tegra_spi_ctrl *ctrl;
  99. int i;
  100. #ifdef CONFIG_OF_CONTROL
  101. int node = 0;
  102. int count;
  103. int node_list[CONFIG_TEGRA_SLINK_CTRLS];
  104. count = fdtdec_find_aliases_for_id(gd->fdt_blob, "spi",
  105. COMPAT_NVIDIA_TEGRA20_SLINK,
  106. node_list,
  107. CONFIG_TEGRA_SLINK_CTRLS);
  108. for (i = 0; i < count; i++) {
  109. ctrl = &spi_ctrls[i];
  110. node = node_list[i];
  111. ctrl->regs = (struct slink_tegra *)fdtdec_get_addr(gd->fdt_blob,
  112. node, "reg");
  113. if ((fdt_addr_t)ctrl->regs == FDT_ADDR_T_NONE) {
  114. debug("%s: no slink register found\n", __func__);
  115. continue;
  116. }
  117. ctrl->freq = fdtdec_get_int(gd->fdt_blob, node,
  118. "spi-max-frequency", 0);
  119. if (!ctrl->freq) {
  120. debug("%s: no slink max frequency found\n", __func__);
  121. continue;
  122. }
  123. ctrl->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
  124. if (ctrl->periph_id == PERIPH_ID_NONE) {
  125. debug("%s: could not decode periph id\n", __func__);
  126. continue;
  127. }
  128. ctrl->valid = 1;
  129. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  130. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  131. }
  132. #else
  133. for (i = 0; i < CONFIG_TEGRA_SLINK_CTRLS; i++) {
  134. ctrl = &spi_ctrls[i];
  135. u32 base_regs[] = {
  136. NV_PA_SLINK1_BASE,
  137. NV_PA_SLINK2_BASE,
  138. NV_PA_SLINK3_BASE,
  139. NV_PA_SLINK4_BASE,
  140. NV_PA_SLINK5_BASE,
  141. NV_PA_SLINK6_BASE,
  142. };
  143. int periph_ids[] = {
  144. PERIPH_ID_SBC1,
  145. PERIPH_ID_SBC2,
  146. PERIPH_ID_SBC3,
  147. PERIPH_ID_SBC4,
  148. PERIPH_ID_SBC5,
  149. PERIPH_ID_SBC6,
  150. };
  151. ctrl->regs = (struct slink_tegra *)base_regs[i];
  152. ctrl->freq = TEGRA_SPI_MAX_FREQ;
  153. ctrl->periph_id = periph_ids[i];
  154. ctrl->valid = 1;
  155. debug("%s: found controller at %p, freq = %u, periph_id = %d\n",
  156. __func__, ctrl->regs, ctrl->freq, ctrl->periph_id);
  157. }
  158. #endif
  159. }
  160. int spi_claim_bus(struct spi_slave *slave)
  161. {
  162. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  163. struct slink_tegra *regs = spi->ctrl->regs;
  164. u32 reg;
  165. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  166. clock_start_periph_pll(spi->ctrl->periph_id, CLOCK_ID_PERIPH,
  167. spi->ctrl->freq);
  168. /* Clear stale status here */
  169. reg = SLINK_STAT_RDY | SLINK_STAT_RXF_FLUSH | SLINK_STAT_TXF_FLUSH | \
  170. SLINK_STAT_RXF_UNR | SLINK_STAT_TXF_OVF;
  171. writel(reg, &regs->status);
  172. debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
  173. /* Set master mode and sw controlled CS */
  174. reg = readl(&regs->command);
  175. reg |= SLINK_CMD_M_S | SLINK_CMD_CS_SOFT;
  176. writel(reg, &regs->command);
  177. debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
  178. return 0;
  179. }
  180. void spi_release_bus(struct spi_slave *slave)
  181. {
  182. }
  183. void spi_cs_activate(struct spi_slave *slave)
  184. {
  185. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  186. struct slink_tegra *regs = spi->ctrl->regs;
  187. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  188. setbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  189. }
  190. void spi_cs_deactivate(struct spi_slave *slave)
  191. {
  192. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  193. struct slink_tegra *regs = spi->ctrl->regs;
  194. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  195. clrbits_le32(&regs->command, SLINK_CMD_CS_VAL);
  196. }
  197. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  198. const void *data_out, void *data_in, unsigned long flags)
  199. {
  200. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  201. struct slink_tegra *regs = spi->ctrl->regs;
  202. u32 reg, tmpdout, tmpdin = 0;
  203. const u8 *dout = data_out;
  204. u8 *din = data_in;
  205. int num_bytes;
  206. int ret;
  207. debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
  208. __func__, slave->bus, slave->cs, dout, din, bitlen);
  209. if (bitlen % 8)
  210. return -1;
  211. num_bytes = bitlen / 8;
  212. ret = 0;
  213. reg = readl(&regs->status);
  214. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  215. debug("%s entry: STATUS = %08x\n", __func__, reg);
  216. reg = readl(&regs->status2);
  217. writel(reg, &regs->status2); /* Clear all STATUS2 events via R/W */
  218. debug("%s entry: STATUS2 = %08x\n", __func__, reg);
  219. debug("%s entry: COMMAND = %08x\n", __func__, readl(&regs->command));
  220. clrsetbits_le32(&regs->command2, SLINK_CMD2_SS_EN_MASK,
  221. SLINK_CMD2_TXEN | SLINK_CMD2_RXEN |
  222. (slave->cs << SLINK_CMD2_SS_EN_SHIFT));
  223. debug("%s entry: COMMAND2 = %08x\n", __func__, readl(&regs->command2));
  224. if (flags & SPI_XFER_BEGIN)
  225. spi_cs_activate(slave);
  226. /* handle data in 32-bit chunks */
  227. while (num_bytes > 0) {
  228. int bytes;
  229. int is_read = 0;
  230. int tm, i;
  231. tmpdout = 0;
  232. bytes = (num_bytes > 4) ? 4 : num_bytes;
  233. if (dout != NULL) {
  234. for (i = 0; i < bytes; ++i)
  235. tmpdout = (tmpdout << 8) | dout[i];
  236. dout += bytes;
  237. }
  238. num_bytes -= bytes;
  239. clrsetbits_le32(&regs->command, SLINK_CMD_BIT_LENGTH_MASK,
  240. bytes * 8 - 1);
  241. writel(tmpdout, &regs->tx_fifo);
  242. setbits_le32(&regs->command, SLINK_CMD_GO);
  243. /*
  244. * Wait for SPI transmit FIFO to empty, or to time out.
  245. * The RX FIFO status will be read and cleared last
  246. */
  247. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  248. u32 status;
  249. status = readl(&regs->status);
  250. /* We can exit when we've had both RX and TX activity */
  251. if (is_read && (status & SLINK_STAT_TXF_EMPTY))
  252. break;
  253. if ((status & (SLINK_STAT_BSY | SLINK_STAT_RDY)) !=
  254. SLINK_STAT_RDY)
  255. tm++;
  256. else if (!(status & SLINK_STAT_RXF_EMPTY)) {
  257. tmpdin = readl(&regs->rx_fifo);
  258. is_read = 1;
  259. /* swap bytes read in */
  260. if (din != NULL) {
  261. for (i = bytes - 1; i >= 0; --i) {
  262. din[i] = tmpdin & 0xff;
  263. tmpdin >>= 8;
  264. }
  265. din += bytes;
  266. }
  267. }
  268. }
  269. if (tm >= SPI_TIMEOUT)
  270. ret = tm;
  271. /* clear ACK RDY, etc. bits */
  272. writel(readl(&regs->status), &regs->status);
  273. }
  274. if (flags & SPI_XFER_END)
  275. spi_cs_deactivate(slave);
  276. debug("%s: transfer ended. Value=%08x, status = %08x\n",
  277. __func__, tmpdin, readl(&regs->status));
  278. if (ret) {
  279. printf("%s: timeout during SPI transfer, tm %d\n",
  280. __func__, ret);
  281. return -1;
  282. }
  283. return 0;
  284. }