exynos_spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. * @param step Number of bytes to transfer in each packet (1 or 4)
  176. */
  177. static void spi_request_bytes(struct exynos_spi *regs, int count, int step)
  178. {
  179. /* For word address we need to swap bytes */
  180. if (step == 4) {
  181. setbits_le32(&regs->mode_cfg,
  182. SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
  183. count /= 4;
  184. setbits_le32(&regs->swap_cfg, SPI_TX_SWAP_EN | SPI_RX_SWAP_EN |
  185. SPI_TX_BYTE_SWAP | SPI_RX_BYTE_SWAP |
  186. SPI_TX_HWORD_SWAP | SPI_RX_HWORD_SWAP);
  187. } else {
  188. /* Select byte access and clear the swap configuration */
  189. clrbits_le32(&regs->mode_cfg,
  190. SPI_MODE_CH_WIDTH_WORD | SPI_MODE_BUS_WIDTH_WORD);
  191. writel(0, &regs->swap_cfg);
  192. }
  193. assert(count && count < (1 << 16));
  194. setbits_le32(&regs->ch_cfg, SPI_CH_RST);
  195. clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
  196. writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
  197. }
  198. static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
  199. void **dinp, void const **doutp, unsigned long flags)
  200. {
  201. struct exynos_spi *regs = spi_slave->regs;
  202. uchar *rxp = *dinp;
  203. const uchar *txp = *doutp;
  204. int rx_lvl, tx_lvl;
  205. uint out_bytes, in_bytes;
  206. int toread;
  207. unsigned start = get_timer(0);
  208. int stopping;
  209. int step;
  210. out_bytes = in_bytes = todo;
  211. stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
  212. !(spi_slave->mode & SPI_SLAVE);
  213. /*
  214. * Try to transfer words if we can. This helps read performance at
  215. * SPI clock speeds above about 20MHz.
  216. */
  217. step = 1;
  218. if (!((todo | (uintptr_t)rxp | (uintptr_t)txp) & 3) &&
  219. !spi_slave->skip_preamble)
  220. step = 4;
  221. /*
  222. * If there's something to send, do a software reset and set a
  223. * transaction size.
  224. */
  225. spi_request_bytes(regs, todo, step);
  226. /*
  227. * Bytes are transmitted/received in pairs. Wait to receive all the
  228. * data because then transmission will be done as well.
  229. */
  230. toread = in_bytes;
  231. while (in_bytes) {
  232. int temp;
  233. /* Keep the fifos full/empty. */
  234. spi_get_fifo_levels(regs, &rx_lvl, &tx_lvl);
  235. /*
  236. * Don't completely fill the txfifo, since we don't want our
  237. * rxfifo to overflow, and it may already contain data.
  238. */
  239. while (tx_lvl < spi_slave->fifo_size/2 && out_bytes) {
  240. if (!txp)
  241. temp = -1;
  242. else if (step == 4)
  243. temp = *(uint32_t *)txp;
  244. else
  245. temp = *txp;
  246. writel(temp, &regs->tx_data);
  247. out_bytes -= step;
  248. if (txp)
  249. txp += step;
  250. tx_lvl += step;
  251. }
  252. if (rx_lvl >= step) {
  253. while (rx_lvl >= step) {
  254. temp = readl(&regs->rx_data);
  255. if (spi_slave->skip_preamble) {
  256. if (temp == SPI_PREAMBLE_END_BYTE) {
  257. spi_slave->skip_preamble = 0;
  258. stopping = 0;
  259. }
  260. } else {
  261. if (rxp || stopping) {
  262. if (step == 4)
  263. *(uint32_t *)rxp = temp;
  264. else
  265. *rxp = temp;
  266. rxp += step;
  267. }
  268. in_bytes -= step;
  269. }
  270. toread -= step;
  271. rx_lvl -= step;
  272. }
  273. } else if (!toread) {
  274. /*
  275. * We have run out of input data, but haven't read
  276. * enough bytes after the preamble yet. Read some more,
  277. * and make sure that we transmit dummy bytes too, to
  278. * keep things going.
  279. */
  280. assert(!out_bytes);
  281. out_bytes = in_bytes;
  282. toread = in_bytes;
  283. txp = NULL;
  284. spi_request_bytes(regs, toread, step);
  285. }
  286. if (spi_slave->skip_preamble && get_timer(start) > 100) {
  287. printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
  288. in_bytes, out_bytes);
  289. return -1;
  290. }
  291. }
  292. *dinp = rxp;
  293. *doutp = txp;
  294. return 0;
  295. }
  296. /**
  297. * Transfer and receive data
  298. *
  299. * @param slave Pointer to spi_slave to which controller has to
  300. * communicate with
  301. * @param bitlen No of bits to tranfer or receive
  302. * @param dout Pointer to transfer buffer
  303. * @param din Pointer to receive buffer
  304. * @param flags Flags for transfer begin and end
  305. * @return zero on success else a negative value
  306. */
  307. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  308. void *din, unsigned long flags)
  309. {
  310. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  311. int upto, todo;
  312. int bytelen;
  313. int ret = 0;
  314. /* spi core configured to do 8 bit transfers */
  315. if (bitlen % 8) {
  316. debug("Non byte aligned SPI transfer.\n");
  317. return -1;
  318. }
  319. /* Start the transaction, if necessary. */
  320. if ((flags & SPI_XFER_BEGIN))
  321. spi_cs_activate(slave);
  322. /*
  323. * Exynos SPI limits each transfer to 65535 transfers. To keep
  324. * things simple, allow a maximum of 65532 bytes. We could allow
  325. * more in word mode, but the performance difference is small.
  326. */
  327. bytelen = bitlen / 8;
  328. for (upto = 0; !ret && upto < bytelen; upto += todo) {
  329. todo = min(bytelen - upto, (1 << 16) - 4);
  330. ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
  331. if (ret)
  332. break;
  333. }
  334. /* Stop the transaction, if necessary. */
  335. if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
  336. spi_cs_deactivate(slave);
  337. if (spi_slave->skip_preamble) {
  338. assert(!spi_slave->skip_preamble);
  339. debug("Failed to complete premable transaction\n");
  340. ret = -1;
  341. }
  342. }
  343. return ret;
  344. }
  345. /**
  346. * Validates the bus and chip select numbers
  347. *
  348. * @param bus ID of the bus that the slave is attached to
  349. * @param cs ID of the chip select connected to the slave
  350. * @return one on success else zero
  351. */
  352. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  353. {
  354. return spi_get_bus(bus) && cs == 0;
  355. }
  356. /**
  357. * Activate the CS by driving it LOW
  358. *
  359. * @param slave Pointer to spi_slave to which controller has to
  360. * communicate with
  361. */
  362. void spi_cs_activate(struct spi_slave *slave)
  363. {
  364. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  365. /* If it's too soon to do another transaction, wait */
  366. if (spi_slave->bus->deactivate_delay_us &&
  367. spi_slave->last_transaction_us) {
  368. ulong delay_us; /* The delay completed so far */
  369. delay_us = timer_get_us() - spi_slave->last_transaction_us;
  370. if (delay_us < spi_slave->bus->deactivate_delay_us)
  371. udelay(spi_slave->bus->deactivate_delay_us - delay_us);
  372. }
  373. clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  374. debug("Activate CS, bus %d\n", spi_slave->slave.bus);
  375. spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
  376. }
  377. /**
  378. * Deactivate the CS by driving it HIGH
  379. *
  380. * @param slave Pointer to spi_slave to which controller has to
  381. * communicate with
  382. */
  383. void spi_cs_deactivate(struct spi_slave *slave)
  384. {
  385. struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
  386. setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
  387. /* Remember time of this transaction so we can honour the bus delay */
  388. if (spi_slave->bus->deactivate_delay_us)
  389. spi_slave->last_transaction_us = timer_get_us();
  390. debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
  391. }
  392. static inline struct exynos_spi *get_spi_base(int dev_index)
  393. {
  394. if (dev_index < 3)
  395. return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
  396. else
  397. return (struct exynos_spi *)samsung_get_base_spi_isp() +
  398. (dev_index - 3);
  399. }
  400. /*
  401. * Read the SPI config from the device tree node.
  402. *
  403. * @param blob FDT blob to read from
  404. * @param node Node offset to read from
  405. * @param bus SPI bus structure to fill with information
  406. * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
  407. */
  408. #ifdef CONFIG_OF_CONTROL
  409. static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
  410. {
  411. bus->node = node;
  412. bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
  413. bus->periph_id = pinmux_decode_periph_id(blob, node);
  414. if (bus->periph_id == PERIPH_ID_NONE) {
  415. debug("%s: Invalid peripheral ID %d\n", __func__,
  416. bus->periph_id);
  417. return -FDT_ERR_NOTFOUND;
  418. }
  419. /* Use 500KHz as a suitable default */
  420. bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  421. 500000);
  422. bus->deactivate_delay_us = fdtdec_get_int(blob, node,
  423. "spi-deactivate-delay", 0);
  424. return 0;
  425. }
  426. /*
  427. * Process a list of nodes, adding them to our list of SPI ports.
  428. *
  429. * @param blob fdt blob
  430. * @param node_list list of nodes to process (any <=0 are ignored)
  431. * @param count number of nodes to process
  432. * @param is_dvc 1 if these are DVC ports, 0 if standard I2C
  433. * @return 0 if ok, -1 on error
  434. */
  435. static int process_nodes(const void *blob, int node_list[], int count)
  436. {
  437. int i;
  438. /* build the i2c_controllers[] for each controller */
  439. for (i = 0; i < count; i++) {
  440. int node = node_list[i];
  441. struct spi_bus *bus;
  442. if (node <= 0)
  443. continue;
  444. bus = &spi_bus[i];
  445. if (spi_get_config(blob, node, bus)) {
  446. printf("exynos spi_init: failed to decode bus %d\n",
  447. i);
  448. return -1;
  449. }
  450. debug("spi: controller bus %d at %p, periph_id %d\n",
  451. i, bus->regs, bus->periph_id);
  452. bus->inited = 1;
  453. bus_count++;
  454. }
  455. return 0;
  456. }
  457. #endif
  458. /**
  459. * Set up a new SPI slave for an fdt node
  460. *
  461. * @param blob Device tree blob
  462. * @param node SPI peripheral node to use
  463. * @return 0 if ok, -1 on error
  464. */
  465. struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
  466. int spi_node)
  467. {
  468. struct spi_bus *bus;
  469. unsigned int i;
  470. for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) {
  471. if (bus->node == spi_node)
  472. return spi_base_setup_slave_fdt(blob, i, slave_node);
  473. }
  474. debug("%s: Failed to find bus node %d\n", __func__, spi_node);
  475. return NULL;
  476. }
  477. /* Sadly there is no error return from this function */
  478. void spi_init(void)
  479. {
  480. int count;
  481. #ifdef CONFIG_OF_CONTROL
  482. int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
  483. const void *blob = gd->fdt_blob;
  484. count = fdtdec_find_aliases_for_id(blob, "spi",
  485. COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
  486. EXYNOS5_SPI_NUM_CONTROLLERS);
  487. if (process_nodes(blob, node_list, count))
  488. return;
  489. #else
  490. struct spi_bus *bus;
  491. for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
  492. bus = &spi_bus[count];
  493. bus->regs = get_spi_base(count);
  494. bus->periph_id = PERIPH_ID_SPI0 + count;
  495. /* Although Exynos5 supports upto 50Mhz speed,
  496. * we are setting it to 10Mhz for safe side
  497. */
  498. bus->frequency = 10000000;
  499. bus->inited = 1;
  500. bus->node = 0;
  501. bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
  502. }
  503. #endif
  504. }