exynos_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * (C) Copyright 2012 SAMSUNG Electronics
  3. * Padmavathi Venna <padma.v@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <common.h>
  20. #include <malloc.h>
  21. #include <spi.h>
  22. #include <fdtdec.h>
  23. #include <asm/arch/clk.h>
  24. #include <asm/arch/clock.h>
  25. #include <asm/arch/cpu.h>
  26. #include <asm/arch/gpio.h>
  27. #include <asm/arch/pinmux.h>
  28. #include <asm/arch-exynos/spi.h>
  29. #include <asm/io.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. /* Information about each SPI controller */
  32. struct spi_bus {
  33. enum periph_id periph_id;
  34. s32 frequency; /* Default clock frequency, -1 for none */
  35. struct exynos_spi *regs;
  36. int inited; /* 1 if this bus is ready for use */
  37. int node;
  38. };
  39. /* A list of spi buses that we know about */
  40. static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
  41. static unsigned int bus_count;
  42. struct exynos_spi_slave {
  43. struct spi_slave slave;
  44. struct exynos_spi *regs;
  45. unsigned int freq; /* Default frequency */
  46. unsigned int mode;
  47. enum periph_id periph_id; /* Peripheral ID for this device */
  48. unsigned int fifo_size;
  49. };
  50. static struct spi_bus *spi_get_bus(unsigned dev_index)
  51. {
  52. if (dev_index < bus_count)
  53. return &spi_bus[dev_index];
  54. debug("%s: invalid bus %d", __func__, dev_index);
  55. return NULL;
  56. }
  57. static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
  58. {
  59. return container_of(slave, struct exynos_spi_slave, slave);
  60. }
  61. /**
  62. * Setup the driver private data
  63. *
  64. * @param bus ID of the bus that the slave is attached to
  65. * @param cs ID of the chip select connected to the slave
  66. * @param max_hz Required spi frequency
  67. * @param mode Required spi mode (clk polarity, clk phase and
  68. * master or slave)
  69. * @return new device or NULL
  70. */
  71. struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  72. unsigned int max_hz, unsigned int mode)
  73. {
  74. struct exynos_spi_slave *spi_slave;
  75. struct spi_bus *bus;
  76. if (!spi_cs_is_valid(busnum, cs)) {
  77. debug("%s: Invalid bus/chip select %d, %d\n", __func__,
  78. busnum, cs);
  79. return NULL;
  80. }
  81. spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
  82. if (!spi_slave) {
  83. debug("%s: Could not allocate spi_slave\n", __func__);
  84. return NULL;
  85. }
  86. bus = &spi_bus[busnum];
  87. spi_slave->regs = bus->regs;
  88. spi_slave->mode = mode;
  89. spi_slave->periph_id = bus->periph_id;
  90. if (bus->periph_id == PERIPH_ID_SPI1 ||
  91. bus->periph_id == PERIPH_ID_SPI2)
  92. spi_slave->fifo_size = 64;
  93. else
  94. spi_slave->fifo_size = 256;
  95. spi_slave->freq = bus->frequency;
  96. if (max_hz)
  97. spi_slave->freq = min(max_hz, spi_slave->freq);
  98. return &spi_slave->slave;
  99. }
  100. /**
  101. * Free spi controller
  102. *
  103. * @param slave Pointer to spi_slave to which controller has to
  104. * communicate with
  105. */
  106. void spi_free_slave(struct spi_slave *slave)
  107. {
  108. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  109. free(spi_slave);
  110. }
  111. /**
  112. * Flush spi tx, rx fifos and reset the SPI controller
  113. *
  114. * @param slave Pointer to spi_slave to which controller has to
  115. * communicate with
  116. */
  117. static void spi_flush_fifo(struct spi_slave *slave)
  118. {
  119. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  120. struct exynos_spi *regs = spi_slave->regs;
  121. clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
  122. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  123. setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
  124. }
  125. /**
  126. * Initialize the spi base registers, set the required clock frequency and
  127. * initialize the gpios
  128. *
  129. * @param slave Pointer to spi_slave to which controller has to
  130. * communicate with
  131. * @return zero on success else a negative value
  132. */
  133. int spi_claim_bus(struct spi_slave *slave)
  134. {
  135. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  136. struct exynos_spi *regs = spi_slave->regs;
  137. u32 reg = 0;
  138. int ret;
  139. ret = set_spi_clk(spi_slave->periph_id,
  140. spi_slave->freq);
  141. if (ret < 0) {
  142. debug("%s: Failed to setup spi clock\n", __func__);
  143. return ret;
  144. }
  145. exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
  146. spi_flush_fifo(slave);
  147. reg = readl(&regs->ch_cfg);
  148. reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
  149. if (spi_slave->mode & SPI_CPHA)
  150. reg |= SPI_CH_CPHA_B;
  151. if (spi_slave->mode & SPI_CPOL)
  152. reg |= SPI_CH_CPOL_L;
  153. writel(reg, &regs->ch_cfg);
  154. writel(SPI_FB_DELAY_180, &regs->fb_clk);
  155. return 0;
  156. }
  157. /**
  158. * Reset the spi H/W and flush the tx and rx fifos
  159. *
  160. * @param slave Pointer to spi_slave to which controller has to
  161. * communicate with
  162. */
  163. void spi_release_bus(struct spi_slave *slave)
  164. {
  165. spi_flush_fifo(slave);
  166. }
  167. static void spi_get_fifo_levels(struct exynos_spi *regs,
  168. int *rx_lvl, int *tx_lvl)
  169. {
  170. uint32_t spi_sts = readl(&regs->spi_sts);
  171. *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  172. *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  173. }
  174. /**
  175. * If there's something to transfer, do a software reset and set a
  176. * transaction size.
  177. *
  178. * @param regs SPI peripheral registers
  179. * @param count Number of bytes to transfer
  180. */
  181. static void spi_request_bytes(struct exynos_spi *regs, int count)
  182. {
  183. assert(count && count < (1 << 16));
  184. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  185. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  186. writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  187. }
  188. static void spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
  189. void **dinp, void const **doutp)
  190. {
  191. struct exynos_spi *regs = spi_slave->regs;
  192. uchar *rxp = *dinp;
  193. const uchar *txp = *doutp;
  194. int rx_lvl, tx_lvl;
  195. uint out_bytes, in_bytes;
  196. out_bytes = in_bytes = todo;
  197. /*
  198. * If there's something to send, do a software reset and set a
  199. * transaction size.
  200. */
  201. spi_request_bytes(regs, todo);
  202. /*
  203. * Bytes are transmitted/received in pairs. Wait to receive all the
  204. * data because then transmission will be done as well.
  205. */
  206. while (in_bytes) {
  207. int temp;
  208. /* Keep the fifos full/empty. */
  209. spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
  210. if (tx_lvl < spi_slave->fifo_size && out_bytes) {
  211. temp = txp ? *txp++ : 0xff;
  212. writel(temp, &regs->tx_data);
  213. out_bytes--;
  214. }
  215. if (rx_lvl > 0 && in_bytes) {
  216. temp = readl(&regs->rx_data);
  217. if (rxp)
  218. *rxp++ = temp;
  219. in_bytes--;
  220. }
  221. }
  222. *dinp = rxp;
  223. *doutp = txp;
  224. }
  225. /**
  226. * Transfer and receive data
  227. *
  228. * @param slave Pointer to spi_slave to which controller has to
  229. * communicate with
  230. * @param bitlen No of bits to tranfer or receive
  231. * @param dout Pointer to transfer buffer
  232. * @param din Pointer to receive buffer
  233. * @param flags Flags for transfer begin and end
  234. * @return zero on success else a negative value
  235. */
  236. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  237. void *din, unsigned long flags)
  238. {
  239. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  240. int upto, todo;
  241. int bytelen;
  242. /* spi core configured to do 8 bit transfers */
  243. if (bitlen % 8) {
  244. debug("Non byte aligned SPI transfer.\n");
  245. return -1;
  246. }
  247. /* Start the transaction, if necessary. */
  248. if ((flags & SPI_XFER_BEGIN))
  249. spi_cs_activate(slave);
  250. /* Exynos SPI limits each transfer to 65535 bytes */
  251. bytelen = bitlen / 8;
  252. for (upto = 0; upto < bytelen; upto += todo) {
  253. todo = min(bytelen - upto, (1 << 16) - 1);
  254. spi_rx_tx(spi_slave, todo, &din, &dout);
  255. }
  256. /* Stop the transaction, if necessary. */
  257. if ((flags & SPI_XFER_END))
  258. spi_cs_deactivate(slave);
  259. return 0;
  260. }
  261. /**
  262. * Validates the bus and chip select numbers
  263. *
  264. * @param bus ID of the bus that the slave is attached to
  265. * @param cs ID of the chip select connected to the slave
  266. * @return one on success else zero
  267. */
  268. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  269. {
  270. return spi_get_bus(bus) && cs == 0;
  271. }
  272. /**
  273. * Activate the CS by driving it LOW
  274. *
  275. * @param slave Pointer to spi_slave to which controller has to
  276. * communicate with
  277. */
  278. void spi_cs_activate(struct spi_slave *slave)
  279. {
  280. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  281. clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  282. debug("Activate CS, bus %d\n", spi_slave->slave.bus);
  283. }
  284. /**
  285. * Deactivate the CS by driving it HIGH
  286. *
  287. * @param slave Pointer to spi_slave to which controller has to
  288. * communicate with
  289. */
  290. void spi_cs_deactivate(struct spi_slave *slave)
  291. {
  292. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  293. setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  294. debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
  295. }
  296. static inline struct exynos_spi *get_spi_base(int dev_index)
  297. {
  298. if (dev_index < 3)
  299. return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
  300. else
  301. return (struct exynos_spi *)samsung_get_base_spi_isp() +
  302. (dev_index - 3);
  303. }
  304. /*
  305. * Read the SPI config from the device tree node.
  306. *
  307. * @param blob FDT blob to read from
  308. * @param node Node offset to read from
  309. * @param bus SPI bus structure to fill with information
  310. * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
  311. */
  312. #ifdef CONFIG_OF_CONTROL
  313. static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
  314. {
  315. bus->node = node;
  316. bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
  317. bus->periph_id = pinmux_decode_periph_id(blob, node);
  318. if (bus->periph_id == PERIPH_ID_NONE) {
  319. debug("%s: Invalid peripheral ID %d\n", __func__,
  320. bus->periph_id);
  321. return -FDT_ERR_NOTFOUND;
  322. }
  323. /* Use 500KHz as a suitable default */
  324. bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  325. 500000);
  326. return 0;
  327. }
  328. /*
  329. * Process a list of nodes, adding them to our list of SPI ports.
  330. *
  331. * @param blob fdt blob
  332. * @param node_list list of nodes to process (any <=0 are ignored)
  333. * @param count number of nodes to process
  334. * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
  335. * @return 0 if ok, -1 on error
  336. */
  337. static int process_nodes(const void *blob, int node_list[], int count)
  338. {
  339. int i;
  340. /* build the i2c_controllers[] for each controller */
  341. for (i = 0; i < count; i++) {
  342. int node = node_list[i];
  343. struct spi_bus *bus;
  344. if (node <= 0)
  345. continue;
  346. bus = &spi_bus[i];
  347. if (spi_get_config(blob, node, bus)) {
  348. printf("exynos spi_init: failed to decode bus %d\n",
  349. i);
  350. return -1;
  351. }
  352. debug("spi: controller bus %d at %p, periph_id %d\n",
  353. i, bus->regs, bus->periph_id);
  354. bus->inited = 1;
  355. bus_count++;
  356. }
  357. return 0;
  358. }
  359. #endif
  360. /* Sadly there is no error return from this function */
  361. void spi_init(void)
  362. {
  363. int count;
  364. #ifdef CONFIG_OF_CONTROL
  365. int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
  366. const void *blob = gd->fdt_blob;
  367. count = fdtdec_find_aliases_for_id(blob, "spi",
  368. COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
  369. EXYNOS5_SPI_NUM_CONTROLLERS);
  370. if (process_nodes(blob, node_list, count))
  371. return;
  372. #else
  373. struct spi_bus *bus;
  374. for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
  375. bus = &spi_bus[count];
  376. bus->regs = get_spi_base(count);
  377. bus->periph_id = PERIPH_ID_SPI0 + count;
  378. /* Although Exynos5 supports upto 50Mhz speed,
  379. * we are setting it to 10Mhz for safe side
  380. */
  381. bus->frequency = 10000000;
  382. bus->inited = 1;
  383. bus->node = 0;
  384. bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
  385. }
  386. #endif
  387. }