ark_spi.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (C) 2008, Guennadi Liakhovetski <lg@denx.de>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. *
  19. */
  20. #include <common.h>
  21. #include <malloc.h>
  22. #include <spi.h>
  23. #include <errno.h>
  24. #include <asm/io.h>
  25. #include <asm/gpio.h>
  26. #include <asm/arch/gpio.h>
  27. #include <asm/arch/ark-spi.h>
  28. #include <asm/arch/clock.h>
  29. static unsigned long spi_bases[] = {
  30. CONFIG_SYS_SPI_BASE
  31. };
  32. #define OUT ARK_GPIO_DIRECTION_OUT
  33. #define reg_read readl
  34. #define reg_write(a, v) writel(v, a)
  35. struct ark_spi_slave {
  36. struct spi_slave slave;
  37. unsigned long base;
  38. u32 ctrl_reg;
  39. u32 cfg_reg;
  40. int ss_pol;
  41. };
  42. //static struct ark_spi_slave *ark_spi_ctxs = NULL;
  43. static inline struct ark_spi_slave *to_ark_spi_slave(struct spi_slave *slave)
  44. {
  45. return container_of(slave, struct ark_spi_slave, slave);
  46. }
  47. void spi_cs_activate(struct spi_slave *slave)
  48. {
  49. struct ark_spi_slave *arks = to_ark_spi_slave(slave);
  50. if(slave->cs == 0){
  51. if(arks->ss_pol)
  52. gpio_set_value(CONFIG_SPI_CS0_GPIO, 1);
  53. else
  54. gpio_set_value(CONFIG_SPI_CS0_GPIO, 0);
  55. }else if(slave->cs == 1){
  56. }
  57. }
  58. void spi_cs_deactivate(struct spi_slave *slave)
  59. {
  60. struct ark_spi_slave *arks = to_ark_spi_slave(slave);
  61. if(slave->cs == 0){
  62. if(arks->ss_pol)
  63. gpio_set_value(CONFIG_SPI_CS0_GPIO, 0);
  64. else
  65. gpio_set_value(CONFIG_SPI_CS0_GPIO, 1);
  66. }else if(slave->cs == 1){
  67. }
  68. }
  69. u32 get_cspi_div(u32 div)
  70. {
  71. int i;
  72. for (i = 0; i < 8; i++) {
  73. if (div <= (4 << i))
  74. return i;
  75. }
  76. return i;
  77. }
  78. static s32 spi_cfg_ark(struct ark_spi_slave *arks, unsigned int cs,
  79. unsigned int max_hz, unsigned int mode)
  80. {
  81. u32 clk_src = ark_get_syspll_clk();
  82. s32 pre_div = 0, post_div = 0, i, reg_ctrl, reg_config;
  83. struct cspi_regs *regs = (struct cspi_regs *)arks->base;
  84. if (max_hz == 0) {
  85. printf("Error: desired clock is 0\n");
  86. return -1;
  87. }
  88. //select syspll clk src
  89. #if defined (CONFIG_ARKN141FAMILY)
  90. #define rSYS_DEVICE_CLK_CFG0 *((volatile unsigned int *)(0x40408060))
  91. rSYS_DEVICE_CLK_CFG0 &= ~(0xFF << 16);
  92. rSYS_DEVICE_CLK_CFG0 |= (1 << 20);
  93. #elif defined(CONFIG_ARK1668FAMILY)
  94. #define rSYS_DEVICE_CLK_CFG0 *((volatile unsigned int *)(0xe4900060))
  95. rSYS_DEVICE_CLK_CFG0 &= ~(0xFF << 16);
  96. rSYS_DEVICE_CLK_CFG0 |= (1 << 20) | (2 << 16);
  97. clk_src = clk_src / (2 + 1) / 2;
  98. #endif
  99. if(cs == 0){
  100. #ifdef CONFIG_DM_GPIO
  101. gpio_request(CONFIG_SPI_CS0_GPIO, "cs0");
  102. #endif
  103. /* Program GPIO 100 to Output mode and Set Output to HIGH */
  104. if (mode & SPI_CS_HIGH)
  105. gpio_direction_output(CONFIG_SPI_CS0_GPIO, 0);
  106. else
  107. gpio_direction_output(CONFIG_SPI_CS0_GPIO, 1);
  108. }else if(cs == 1){
  109. }
  110. reg_ctrl = reg_read(&regs->ctrl);
  111. /* Reset spi */
  112. reg_write(&regs->ctrl, 0);
  113. reg_write(&regs->ctrl, (reg_ctrl | ARK_ECSPI_CTRL_EN));
  114. /*
  115. * The following computation is taken directly from Freescale's code.
  116. */
  117. if (clk_src > max_hz) {
  118. pre_div = clk_src / max_hz;
  119. if (pre_div > 16) {
  120. post_div = pre_div / 16;
  121. pre_div = 15;
  122. }
  123. if (post_div != 0) {
  124. for (i = 0; i < 16; i++) {
  125. if ((1 << i) >= post_div)
  126. break;
  127. }
  128. if (i == 16) {
  129. printf("Error: no divider for the freq: %d\n",
  130. max_hz);
  131. return -1;
  132. }
  133. post_div = i;
  134. }
  135. }
  136. debug("pre_div = %d, post_div=%d\n", pre_div, post_div);
  137. reg_ctrl = (reg_ctrl & ~ARK_ECSPI_CTRL_SELCHAN(3)) |
  138. ARK_ECSPI_CTRL_SELCHAN(cs);
  139. reg_ctrl = (reg_ctrl & ~ARK_ECSPI_CTRL_PREDIV(0x0F)) |
  140. ARK_ECSPI_CTRL_PREDIV(pre_div);
  141. reg_ctrl = (reg_ctrl & ~ARK_ECSPI_CTRL_POSTDIV(0x0F)) |
  142. ARK_ECSPI_CTRL_POSTDIV(post_div);
  143. /* always set to master mode */
  144. reg_ctrl |= ARK_ECSPI_CTRL_MODE(cs);
  145. /* We need to disable SPI before changing registers */
  146. reg_ctrl &= ~ARK_ECSPI_CTRL_EN;
  147. reg_config = reg_read(&regs->cfg);
  148. reg_config = (reg_config & ~ARK_ECSPI_CONFIG_SSBPOL(cs));
  149. reg_config = (reg_config & ~ARK_ECSPI_CONFIG_SCLKPOL(cs));
  150. reg_config = (reg_config & ~ARK_ECSPI_CONFIG_SCLKPHA(cs));
  151. if (mode & SPI_CS_HIGH)
  152. reg_config |= ARK_ECSPI_CONFIG_SSBPOL(cs);
  153. if (mode & SPI_CPOL)
  154. reg_config |= ARK_ECSPI_CONFIG_SCLKPOL(cs);
  155. if (mode & SPI_CPHA)
  156. reg_config |= ARK_ECSPI_CONFIG_SCLKPHA(cs);
  157. reg_config |= ARK_ECSPI_CONFIG_SBBCTRL(cs);
  158. debug("reg_ctrl = 0x%x\n", reg_ctrl);
  159. reg_write(&regs->ctrl, reg_ctrl);
  160. debug("reg_config = 0x%x\n", reg_config);
  161. reg_write(&regs->cfg, reg_config);
  162. /* save config register and control register */
  163. arks->ctrl_reg = reg_ctrl;
  164. arks->cfg_reg = reg_config;
  165. /* clear interrupt reg */
  166. reg_write(&regs->intr, 0);
  167. reg_write(&regs->stat, ARK_ECSPI_STAT_TC | ARK_ECSPI_STAT_RO | ARK_ECSPI_STAT_REN);
  168. return 0;
  169. }
  170. int spi_xchg_single(struct spi_slave *slave, unsigned int bitlen,
  171. const u8 *dout, u8 *din, unsigned long flags)
  172. {
  173. struct ark_spi_slave *arks = to_ark_spi_slave(slave);
  174. size_t nbytes = (bitlen + 7) / 8;
  175. u32 data, cnt, i;
  176. struct cspi_regs *regs = (struct cspi_regs *)arks->base;
  177. debug("%s: bitlen %d dout 0x%x din 0x%x\n",
  178. __func__, bitlen, (u32)dout, (u32)din);
  179. arks->ctrl_reg = (arks->ctrl_reg &
  180. ~ARK_ECSPI_CTRL_BITCOUNT(ARK_ECSPI_CTRL_MAXBITS)) |
  181. ARK_ECSPI_CTRL_BITCOUNT(bitlen - 1);
  182. reg_write(&regs->ctrl, arks->ctrl_reg | ARK_ECSPI_CTRL_EN);
  183. reg_write(&regs->cfg, arks->cfg_reg);
  184. /* Clear interrupt register */
  185. reg_write(&regs->stat, ARK_ECSPI_STAT_TC | ARK_ECSPI_STAT_RO | ARK_ECSPI_STAT_REN);
  186. /*
  187. * The SPI controller works only with words,
  188. * check if less than a word is sent.
  189. * Access to the FIFO is only 32 bit
  190. */
  191. if (bitlen % 32) {
  192. data = 0;
  193. cnt = (bitlen % 32) / 8;
  194. if (dout) {
  195. for (i = 0; i < cnt; i++) {
  196. #ifdef CONFIG_ARCH_ARKE
  197. data |= (*dout++ & 0xFF) << (8 * i);
  198. #else
  199. data = (data >> 8) | ((*dout++ & 0xFF) << 24);
  200. #endif
  201. }
  202. }
  203. debug("Sending SPI 0x%x\n", data);
  204. reg_write(&regs->txdata, data);
  205. nbytes -= cnt;
  206. }
  207. data = 0;
  208. while (nbytes > 0) {
  209. data = 0;
  210. if (dout) {
  211. /* Buffer is not 32-bit aligned */
  212. if ((unsigned long)dout & 0x03) {
  213. data = 0;
  214. for (i = 0; i < 4; i++)
  215. #ifdef CONFIG_ARCH_ARKE
  216. data |= (*dout++ & 0xFF) << (8 * i);
  217. #else
  218. data = (data >> 8) | ((*dout++ & 0xFF) << 24);
  219. #endif
  220. } else {
  221. data = *(u32 *)dout;
  222. }
  223. dout += 4;
  224. }
  225. debug("Sending SPI 0x%x\n", data);
  226. reg_write(&regs->txdata, data);
  227. nbytes -= 4;
  228. }
  229. /* FIFO is written, now starts the transfer setting the XCH bit */
  230. reg_write(&regs->ctrl, arks->ctrl_reg |
  231. ARK_ECSPI_CTRL_EN | ARK_ECSPI_CTRL_XCH);
  232. /* Wait until the TC (Transfer completed) bit is set */
  233. while ((reg_read(&regs->stat) & ARK_ECSPI_STAT_TC) == 0)
  234. ;
  235. nbytes = (bitlen + 7) / 8;
  236. cnt = nbytes % 32;
  237. if (bitlen % 32) {
  238. data = reg_read(&regs->rxdata);
  239. cnt = (bitlen % 32) / 8;
  240. #ifndef CONFIG_ARCH_ARKE
  241. data = data >> ((sizeof(data) - cnt) * 8);
  242. #endif
  243. debug("SPI Rx unaligned: 0x%x\n", data);
  244. if (din) {
  245. memcpy(din, &data, cnt);
  246. din += cnt;
  247. }
  248. nbytes -= cnt;
  249. }
  250. while (nbytes > 0) {
  251. u32 tmp;
  252. tmp = reg_read(&regs->rxdata);
  253. data = tmp;
  254. debug("SPI Rx: 0x%x 0x%x\n", tmp, data);
  255. cnt = min(nbytes, sizeof(data));
  256. if (din) {
  257. memcpy(din, &data, cnt);
  258. din += cnt;
  259. }
  260. nbytes -= cnt;
  261. }
  262. /* Transfer completed, clear any pending request */
  263. reg_write(&regs->stat, ARK_ECSPI_STAT_TC | ARK_ECSPI_STAT_RO);
  264. return 0;
  265. }
  266. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  267. void *din, unsigned long flags)
  268. {
  269. int n_bytes = (bitlen + 7) / 8;
  270. int n_bits;
  271. int ret;
  272. u32 blk_size;
  273. u8 *p_outbuf = (u8 *)dout;
  274. u8 *p_inbuf = (u8 *)din;
  275. if (!slave)
  276. return -1;
  277. if (flags & SPI_XFER_BEGIN)
  278. spi_cs_activate(slave);
  279. while (n_bytes > 0) {
  280. if (n_bytes < MAX_SPI_BYTES)
  281. blk_size = n_bytes;
  282. else
  283. blk_size = MAX_SPI_BYTES;
  284. n_bits = blk_size * 8;
  285. ret = spi_xchg_single(slave, n_bits, p_outbuf, p_inbuf, 0);
  286. if (ret)
  287. return ret;
  288. if (dout)
  289. p_outbuf += blk_size;
  290. if (din)
  291. p_inbuf += blk_size;
  292. n_bytes -= blk_size;
  293. }
  294. if (flags & SPI_XFER_END) {
  295. spi_cs_deactivate(slave);
  296. }
  297. return 0;
  298. }
  299. void spi_init(void)
  300. {
  301. }
  302. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  303. unsigned int max_hz, unsigned int mode)
  304. {
  305. struct ark_spi_slave *arks;
  306. int ret;
  307. if (bus >= ARRAY_SIZE(spi_bases))
  308. return NULL;
  309. //if (ark_spi_ctxs)
  310. // return &ark_spi_ctxs->slave;
  311. arks = spi_alloc_slave(struct ark_spi_slave, bus, cs);
  312. if (!arks) {
  313. puts("ark_spi: SPI Slave not allocated !\n");
  314. return NULL;
  315. }
  316. arks->base = spi_bases[bus];
  317. arks->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
  318. //ark_spi_ctxs = arks;
  319. ret = spi_cfg_ark(arks, cs, max_hz, mode);
  320. if (ret) {
  321. printf("ark_spi: cannot setup SPI controller\n");
  322. free(arks);
  323. return NULL;
  324. }
  325. return &arks->slave;
  326. }
  327. void spi_free_slave(struct spi_slave *slave)
  328. {
  329. struct ark_spi_slave *arks = to_ark_spi_slave(slave);
  330. free(arks);
  331. //ark_spi_ctxs = NULL;
  332. }
  333. int spi_claim_bus(struct spi_slave *slave)
  334. {
  335. struct ark_spi_slave *arks = to_ark_spi_slave(slave);
  336. struct cspi_regs *regs = (struct cspi_regs *)arks->base;
  337. reg_write(&regs->ctrl, arks->ctrl_reg);
  338. reg_write(&regs->intr, 0);
  339. return 0;
  340. }
  341. void spi_release_bus(struct spi_slave *slave)
  342. {
  343. /* TODO: Shut the controller down */
  344. }