exynos_spi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * (C) Copyright 2012 SAMSUNG Electronics
  3. * Padmavathi Venna <padma.v@samsung.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <spi.h>
  10. #include <fdtdec.h>
  11. #include <asm/arch/clk.h>
  12. #include <asm/arch/clock.h>
  13. #include <asm/arch/cpu.h>
  14. #include <asm/arch/gpio.h>
  15. #include <asm/arch/pinmux.h>
  16. #include <asm/arch-exynos/spi.h>
  17. #include <asm/io.h>
  18. DECLARE_GLOBAL_DATA_PTR;
  19. /* Information about each SPI controller */
  20. struct spi_bus {
  21. enum periph_id periph_id;
  22. s32 frequency; /* Default clock frequency, -1 for none */
  23. struct exynos_spi *regs;
  24. int inited; /* 1 if this bus is ready for use */
  25. int node;
  26. uint deactivate_delay_us; /* Delay to wait after deactivate */
  27. };
  28. /* A list of spi buses that we know about */
  29. static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
  30. static unsigned int bus_count;
  31. struct exynos_spi_slave {
  32. struct spi_slave slave;
  33. struct exynos_spi *regs;
  34. unsigned int freq; /* Default frequency */
  35. unsigned int mode;
  36. enum periph_id periph_id; /* Peripheral ID for this device */
  37. unsigned int fifo_size;
  38. int skip_preamble;
  39. struct spi_bus *bus; /* Pointer to our SPI bus info */
  40. ulong last_transaction_us; /* Time of last transaction end */
  41. };
  42. static struct spi_bus *spi_get_bus(unsigned dev_index)
  43. {
  44. if (dev_index < bus_count)
  45. return &spi_bus[dev_index];
  46. debug("%s: invalid bus %d", __func__, dev_index);
  47. return NULL;
  48. }
  49. static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
  50. {
  51. return container_of(slave, struct exynos_spi_slave, slave);
  52. }
  53. /**
  54. * Setup the driver private data
  55. *
  56. * @param bus ID of the bus that the slave is attached to
  57. * @param cs ID of the chip select connected to the slave
  58. * @param max_hz Required spi frequency
  59. * @param mode Required spi mode (clk polarity, clk phase and
  60. * master or slave)
  61. * @return new device or NULL
  62. */
  63. struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  64. unsigned int max_hz, unsigned int mode)
  65. {
  66. struct exynos_spi_slave *spi_slave;
  67. struct spi_bus *bus;
  68. if (!spi_cs_is_valid(busnum, cs)) {
  69. debug("%s: Invalid bus/chip select %d, %d\n", __func__,
  70. busnum, cs);
  71. return NULL;
  72. }
  73. spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
  74. if (!spi_slave) {
  75. debug("%s: Could not allocate spi_slave\n", __func__);
  76. return NULL;
  77. }
  78. bus = &spi_bus[busnum];
  79. spi_slave->bus = bus;
  80. spi_slave->regs = bus->regs;
  81. spi_slave->mode = mode;
  82. spi_slave->periph_id = bus->periph_id;
  83. if (bus->periph_id == PERIPH_ID_SPI1 ||
  84. bus->periph_id == PERIPH_ID_SPI2)
  85. spi_slave->fifo_size = 64;
  86. else
  87. spi_slave->fifo_size = 256;
  88. spi_slave->skip_preamble = 0;
  89. spi_slave->last_transaction_us = timer_get_us();
  90. spi_slave->freq = bus->frequency;
  91. if (max_hz)
  92. spi_slave->freq = min(max_hz, spi_slave->freq);
  93. return &spi_slave->slave;
  94. }
  95. /**
  96. * Free spi controller
  97. *
  98. * @param slave Pointer to spi_slave to which controller has to
  99. * communicate with
  100. */
  101. void spi_free_slave(struct spi_slave *slave)
  102. {
  103. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  104. free(spi_slave);
  105. }
  106. /**
  107. * Flush spi tx, rx fifos and reset the SPI controller
  108. *
  109. * @param slave Pointer to spi_slave to which controller has to
  110. * communicate with
  111. */
  112. static void spi_flush_fifo(struct spi_slave *slave)
  113. {
  114. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  115. struct exynos_spi *regs = spi_slave->regs;
  116. clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
  117. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  118. setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
  119. }
  120. /**
  121. * Initialize the spi base registers, set the required clock frequency and
  122. * initialize the gpios
  123. *
  124. * @param slave Pointer to spi_slave to which controller has to
  125. * communicate with
  126. * @return zero on success else a negative value
  127. */
  128. int spi_claim_bus(struct spi_slave *slave)
  129. {
  130. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  131. struct exynos_spi *regs = spi_slave->regs;
  132. u32 reg = 0;
  133. int ret;
  134. ret = set_spi_clk(spi_slave->periph_id,
  135. spi_slave->freq);
  136. if (ret < 0) {
  137. debug("%s: Failed to setup spi clock\n", __func__);
  138. return ret;
  139. }
  140. exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
  141. spi_flush_fifo(slave);
  142. reg = readl(&regs->ch_cfg);
  143. reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
  144. if (spi_slave->mode & SPI_CPHA)
  145. reg |= SPI_CH_CPHA_B;
  146. if (spi_slave->mode & SPI_CPOL)
  147. reg |= SPI_CH_CPOL_L;
  148. writel(reg, &regs->ch_cfg);
  149. writel(SPI_FB_DELAY_180, &regs->fb_clk);
  150. return 0;
  151. }
  152. /**
  153. * Reset the spi H/W and flush the tx and rx fifos
  154. *
  155. * @param slave Pointer to spi_slave to which controller has to
  156. * communicate with
  157. */
  158. void spi_release_bus(struct spi_slave *slave)
  159. {
  160. spi_flush_fifo(slave);
  161. }
  162. static void spi_get_fifo_levels(struct exynos_spi *regs,
  163. int *rx_lvl, int *tx_lvl)
  164. {
  165. uint32_t spi_sts = readl(&regs->spi_sts);
  166. *rx_lvl = (spi_sts >> SPI_RX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  167. *tx_lvl = (spi_sts >> SPI_TX_LVL_OFFSET) & SPI_FIFO_LVL_MASK;
  168. }
  169. /**
  170. * If there's something to transfer, do a software reset and set a
  171. * transaction size.
  172. *
  173. * @param regs SPI peripheral registers
  174. * @param count Number of bytes to transfer
  175. */
  176. static void spi_request_bytes(struct exynos_spi *regs, int count)
  177. {
  178. assert(count && count < (1 << 16));
  179. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  180. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  181. writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  182. }
  183. static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
  184. void **dinp, void const **doutp, unsigned long flags)
  185. {
  186. struct exynos_spi *regs = spi_slave->regs;
  187. uchar *rxp = *dinp;
  188. const uchar *txp = *doutp;
  189. int rx_lvl, tx_lvl;
  190. uint out_bytes, in_bytes;
  191. int toread;
  192. unsigned start = get_timer(0);
  193. int stopping;
  194. out_bytes = in_bytes = todo;
  195. stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
  196. !(spi_slave->mode & SPI_SLAVE);
  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. toread = in_bytes;
  207. while (in_bytes) {
  208. int temp;
  209. /* Keep the fifos full/empty. */
  210. spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
  211. while (tx_lvl < spi_slave->fifo_size/2 && out_bytes) {
  212. temp = txp ? *txp++ : 0xff;
  213. writel(temp, &regs->tx_data);
  214. out_bytes--;
  215. tx_lvl++;
  216. }
  217. if (rx_lvl > 0) {
  218. while (rx_lvl > 0) {
  219. temp = readl(&regs->rx_data);
  220. if (spi_slave->skip_preamble) {
  221. if (temp == SPI_PREAMBLE_END_BYTE) {
  222. spi_slave->skip_preamble = 0;
  223. stopping = 0;
  224. }
  225. } else {
  226. if (rxp || stopping)
  227. *rxp++ = temp;
  228. in_bytes--;
  229. }
  230. toread--;
  231. rx_lvl--;
  232. } else if (!toread) {
  233. /*
  234. * We have run out of input data, but haven't read
  235. * enough bytes after the preamble yet. Read some more,
  236. * and make sure that we transmit dummy bytes too, to
  237. * keep things going.
  238. */
  239. assert(!out_bytes);
  240. out_bytes = in_bytes;
  241. toread = in_bytes;
  242. txp = NULL;
  243. spi_request_bytes(regs, toread);
  244. }
  245. if (spi_slave->skip_preamble && get_timer(start) > 100) {
  246. printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
  247. in_bytes, out_bytes);
  248. return -1;
  249. }
  250. }
  251. *dinp = rxp;
  252. *doutp = txp;
  253. return 0;
  254. }
  255. /**
  256. * Transfer and receive data
  257. *
  258. * @param slave Pointer to spi_slave to which controller has to
  259. * communicate with
  260. * @param bitlen No of bits to tranfer or receive
  261. * @param dout Pointer to transfer buffer
  262. * @param din Pointer to receive buffer
  263. * @param flags Flags for transfer begin and end
  264. * @return zero on success else a negative value
  265. */
  266. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  267. void *din, unsigned long flags)
  268. {
  269. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  270. int upto, todo;
  271. int bytelen;
  272. int ret = 0;
  273. /* spi core configured to do 8 bit transfers */
  274. if (bitlen % 8) {
  275. debug("Non byte aligned SPI transfer.\n");
  276. return -1;
  277. }
  278. /* Start the transaction, if necessary. */
  279. if ((flags & SPI_XFER_BEGIN))
  280. spi_cs_activate(slave);
  281. /* Exynos SPI limits each transfer to 65535 bytes */
  282. bytelen = bitlen / 8;
  283. for (upto = 0; !ret && upto < bytelen; upto += todo) {
  284. todo = min(bytelen - upto, (1 << 16) - 1);
  285. ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
  286. if (ret)
  287. break;
  288. }
  289. /* Stop the transaction, if necessary. */
  290. if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
  291. spi_cs_deactivate(slave);
  292. if (spi_slave->skip_preamble) {
  293. assert(!spi_slave->skip_preamble);
  294. debug("Failed to complete premable transaction\n");
  295. ret = -1;
  296. }
  297. }
  298. return ret;
  299. }
  300. /**
  301. * Validates the bus and chip select numbers
  302. *
  303. * @param bus ID of the bus that the slave is attached to
  304. * @param cs ID of the chip select connected to the slave
  305. * @return one on success else zero
  306. */
  307. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  308. {
  309. return spi_get_bus(bus) && cs == 0;
  310. }
  311. /**
  312. * Activate the CS by driving it LOW
  313. *
  314. * @param slave Pointer to spi_slave to which controller has to
  315. * communicate with
  316. */
  317. void spi_cs_activate(struct spi_slave *slave)
  318. {
  319. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  320. /* If it's too soon to do another transaction, wait */
  321. if (spi_slave->bus->deactivate_delay_us &&
  322. spi_slave->last_transaction_us) {
  323. ulong delay_us; /* The delay completed so far */
  324. delay_us = timer_get_us() - spi_slave->last_transaction_us;
  325. if (delay_us < spi_slave->bus->deactivate_delay_us)
  326. udelay(spi_slave->bus->deactivate_delay_us - delay_us);
  327. }
  328. clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  329. debug("Activate CS, bus %d\n", spi_slave->slave.bus);
  330. spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
  331. /* Remember time of this transaction so we can honour the bus delay */
  332. if (spi_slave->bus->deactivate_delay_us)
  333. spi_slave->last_transaction_us = timer_get_us();
  334. }
  335. /**
  336. * Deactivate the CS by driving it HIGH
  337. *
  338. * @param slave Pointer to spi_slave to which controller has to
  339. * communicate with
  340. */
  341. void spi_cs_deactivate(struct spi_slave *slave)
  342. {
  343. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  344. setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  345. debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
  346. }
  347. static inline struct exynos_spi *get_spi_base(int dev_index)
  348. {
  349. if (dev_index < 3)
  350. return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
  351. else
  352. return (struct exynos_spi *)samsung_get_base_spi_isp() +
  353. (dev_index - 3);
  354. }
  355. /*
  356. * Read the SPI config from the device tree node.
  357. *
  358. * @param blob FDT blob to read from
  359. * @param node Node offset to read from
  360. * @param bus SPI bus structure to fill with information
  361. * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
  362. */
  363. #ifdef CONFIG_OF_CONTROL
  364. static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
  365. {
  366. bus->node = node;
  367. bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
  368. bus->periph_id = pinmux_decode_periph_id(blob, node);
  369. if (bus->periph_id == PERIPH_ID_NONE) {
  370. debug("%s: Invalid peripheral ID %d\n", __func__,
  371. bus->periph_id);
  372. return -FDT_ERR_NOTFOUND;
  373. }
  374. /* Use 500KHz as a suitable default */
  375. bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  376. 500000);
  377. bus->deactivate_delay_us = fdtdec_get_int(blob, node,
  378. "spi-deactivate-delay", 0);
  379. return 0;
  380. }
  381. /*
  382. * Process a list of nodes, adding them to our list of SPI ports.
  383. *
  384. * @param blob fdt blob
  385. * @param node_list list of nodes to process (any <=0 are ignored)
  386. * @param count number of nodes to process
  387. * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
  388. * @return 0 if ok, -1 on error
  389. */
  390. static int process_nodes(const void *blob, int node_list[], int count)
  391. {
  392. int i;
  393. /* build the i2c_controllers[] for each controller */
  394. for (i = 0; i < count; i++) {
  395. int node = node_list[i];
  396. struct spi_bus *bus;
  397. if (node <= 0)
  398. continue;
  399. bus = &spi_bus[i];
  400. if (spi_get_config(blob, node, bus)) {
  401. printf("exynos spi_init: failed to decode bus %d\n",
  402. i);
  403. return -1;
  404. }
  405. debug("spi: controller bus %d at %p, periph_id %d\n",
  406. i, bus->regs, bus->periph_id);
  407. bus->inited = 1;
  408. bus_count++;
  409. }
  410. return 0;
  411. }
  412. #endif
  413. /**
  414. * Set up a new SPI slave for an fdt node
  415. *
  416. * @param blob Device tree blob
  417. * @param node SPI peripheral node to use
  418. * @return 0 if ok, -1 on error
  419. */
  420. struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
  421. unsigned int cs, unsigned int max_hz, unsigned int mode)
  422. {
  423. struct spi_bus *bus;
  424. unsigned int i;
  425. for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) {
  426. if (bus->node == node)
  427. return spi_setup_slave(i, cs, max_hz, mode);
  428. }
  429. debug("%s: Failed to find bus node %d\n", __func__, node);
  430. return NULL;
  431. }
  432. /* Sadly there is no error return from this function */
  433. void spi_init(void)
  434. {
  435. int count;
  436. #ifdef CONFIG_OF_CONTROL
  437. int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
  438. const void *blob = gd->fdt_blob;
  439. count = fdtdec_find_aliases_for_id(blob, "spi",
  440. COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
  441. EXYNOS5_SPI_NUM_CONTROLLERS);
  442. if (process_nodes(blob, node_list, count))
  443. return;
  444. #else
  445. struct spi_bus *bus;
  446. for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
  447. bus = &spi_bus[count];
  448. bus->regs = get_spi_base(count);
  449. bus->periph_id = PERIPH_ID_SPI0 + count;
  450. /* Although Exynos5 supports upto 50Mhz speed,
  451. * we are setting it to 10Mhz for safe side
  452. */
  453. bus->frequency = 10000000;
  454. bus->inited = 1;
  455. bus->node = 0;
  456. bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
  457. }
  458. #endif
  459. }