spi.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Common SPI Interface: Controller-specific definitions
  3. *
  4. * (C) Copyright 2001
  5. * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #ifndef _SPI_H_
  10. #define _SPI_H_
  11. /* SPI mode flags */
  12. #define SPI_CPHA BIT(0) /* clock phase */
  13. #define SPI_CPOL BIT(1) /* clock polarity */
  14. #define SPI_MODE_0 (0|0) /* (original MicroWire) */
  15. #define SPI_MODE_1 (0|SPI_CPHA)
  16. #define SPI_MODE_2 (SPI_CPOL|0)
  17. #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA)
  18. #define SPI_CS_HIGH BIT(2) /* CS active high */
  19. #define SPI_LSB_FIRST BIT(3) /* per-word bits-on-wire */
  20. #define SPI_3WIRE BIT(4) /* SI/SO signals shared */
  21. #define SPI_LOOP BIT(5) /* loopback mode */
  22. #define SPI_SLAVE BIT(6) /* slave mode */
  23. #define SPI_PREAMBLE BIT(7) /* Skip preamble bytes */
  24. #define SPI_TX_BYTE BIT(8) /* transmit with 1 wire byte */
  25. #define SPI_TX_DUAL BIT(9) /* transmit with 2 wires */
  26. #define SPI_TX_QUAD BIT(10) /* transmit with 4 wires */
  27. #define SPI_RX_SLOW BIT(11) /* receive with 1 wire slow */
  28. #define SPI_RX_DUAL BIT(12) /* receive with 2 wires */
  29. #define SPI_RX_QUAD BIT(13) /* receive with 4 wires */
  30. /* Header byte that marks the start of the message */
  31. #define SPI_PREAMBLE_END_BYTE 0xec
  32. #define SPI_DEFAULT_WORDLEN 8
  33. #ifdef CONFIG_DM_SPI
  34. /* TODO(sjg@chromium.org): Remove this and use max_hz from struct spi_slave */
  35. struct dm_spi_bus {
  36. uint max_hz;
  37. };
  38. /**
  39. * struct dm_spi_platdata - platform data for all SPI slaves
  40. *
  41. * This describes a SPI slave, a child device of the SPI bus. To obtain this
  42. * struct from a spi_slave, use dev_get_parent_platdata(dev) or
  43. * dev_get_parent_platdata(slave->dev).
  44. *
  45. * This data is immuatable. Each time the device is probed, @max_hz and @mode
  46. * will be copied to struct spi_slave.
  47. *
  48. * @cs: Chip select number (0..n-1)
  49. * @max_hz: Maximum bus speed that this slave can tolerate
  50. * @mode: SPI mode to use for this device (see SPI mode flags)
  51. */
  52. struct dm_spi_slave_platdata {
  53. unsigned int cs;
  54. uint max_hz;
  55. uint mode;
  56. };
  57. #endif /* CONFIG_DM_SPI */
  58. /**
  59. * struct spi_slave - Representation of a SPI slave
  60. *
  61. * For driver model this is the per-child data used by the SPI bus. It can
  62. * be accessed using dev_get_parent_priv() on the slave device. The SPI uclass
  63. * sets uip per_child_auto_alloc_size to sizeof(struct spi_slave), and the
  64. * driver should not override it. Two platform data fields (max_hz and mode)
  65. * are copied into this structure to provide an initial value. This allows
  66. * them to be changed, since we should never change platform data in drivers.
  67. *
  68. * If not using driver model, drivers are expected to extend this with
  69. * controller-specific data.
  70. *
  71. * @dev: SPI slave device
  72. * @max_hz: Maximum speed for this slave
  73. * @speed: Current bus speed. This is 0 until the bus is first
  74. * claimed.
  75. * @bus: ID of the bus that the slave is attached to. For
  76. * driver model this is the sequence number of the SPI
  77. * bus (bus->seq) so does not need to be stored
  78. * @cs: ID of the chip select connected to the slave.
  79. * @mode: SPI mode to use for this slave (see SPI mode flags)
  80. * @wordlen: Size of SPI word in number of bits
  81. * @max_read_size: If non-zero, the maximum number of bytes which can
  82. * be read at once.
  83. * @max_write_size: If non-zero, the maximum number of bytes which can
  84. * be written at once.
  85. * @memory_map: Address of read-only SPI flash access.
  86. * @flags: Indication of SPI flags.
  87. */
  88. struct spi_slave {
  89. #ifdef CONFIG_DM_SPI
  90. struct udevice *dev; /* struct spi_slave is dev->parentdata */
  91. uint max_hz;
  92. uint speed;
  93. #else
  94. unsigned int bus;
  95. unsigned int cs;
  96. #endif
  97. uint mode;
  98. unsigned int wordlen;
  99. unsigned int max_read_size;
  100. unsigned int max_write_size;
  101. void *memory_map;
  102. u8 flags;
  103. #define SPI_XFER_BEGIN BIT(0) /* Assert CS before transfer */
  104. #define SPI_XFER_END BIT(1) /* Deassert CS after transfer */
  105. #define SPI_XFER_ONCE (SPI_XFER_BEGIN | SPI_XFER_END)
  106. #define SPI_XFER_MMAP BIT(2) /* Memory Mapped start */
  107. #define SPI_XFER_MMAP_END BIT(3) /* Memory Mapped End */
  108. };
  109. /**
  110. * Initialization, must be called once on start up.
  111. *
  112. * TODO: I don't think we really need this.
  113. */
  114. void spi_init(void);
  115. /**
  116. * spi_do_alloc_slave - Allocate a new SPI slave (internal)
  117. *
  118. * Allocate and zero all fields in the spi slave, and set the bus/chip
  119. * select. Use the helper macro spi_alloc_slave() to call this.
  120. *
  121. * @offset: Offset of struct spi_slave within slave structure.
  122. * @size: Size of slave structure.
  123. * @bus: Bus ID of the slave chip.
  124. * @cs: Chip select ID of the slave chip on the specified bus.
  125. */
  126. void *spi_do_alloc_slave(int offset, int size, unsigned int bus,
  127. unsigned int cs);
  128. /**
  129. * spi_alloc_slave - Allocate a new SPI slave
  130. *
  131. * Allocate and zero all fields in the spi slave, and set the bus/chip
  132. * select.
  133. *
  134. * @_struct: Name of structure to allocate (e.g. struct tegra_spi).
  135. * This structure must contain a member 'struct spi_slave *slave'.
  136. * @bus: Bus ID of the slave chip.
  137. * @cs: Chip select ID of the slave chip on the specified bus.
  138. */
  139. #define spi_alloc_slave(_struct, bus, cs) \
  140. spi_do_alloc_slave(offsetof(_struct, slave), \
  141. sizeof(_struct), bus, cs)
  142. /**
  143. * spi_alloc_slave_base - Allocate a new SPI slave with no private data
  144. *
  145. * Allocate and zero all fields in the spi slave, and set the bus/chip
  146. * select.
  147. *
  148. * @bus: Bus ID of the slave chip.
  149. * @cs: Chip select ID of the slave chip on the specified bus.
  150. */
  151. #define spi_alloc_slave_base(bus, cs) \
  152. spi_do_alloc_slave(0, sizeof(struct spi_slave), bus, cs)
  153. /**
  154. * Set up communications parameters for a SPI slave.
  155. *
  156. * This must be called once for each slave. Note that this function
  157. * usually doesn't touch any actual hardware, it only initializes the
  158. * contents of spi_slave so that the hardware can be easily
  159. * initialized later.
  160. *
  161. * @bus: Bus ID of the slave chip.
  162. * @cs: Chip select ID of the slave chip on the specified bus.
  163. * @max_hz: Maximum SCK rate in Hz.
  164. * @mode: Clock polarity, clock phase and other parameters.
  165. *
  166. * Returns: A spi_slave reference that can be used in subsequent SPI
  167. * calls, or NULL if one or more of the parameters are not supported.
  168. */
  169. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  170. unsigned int max_hz, unsigned int mode);
  171. /**
  172. * Free any memory associated with a SPI slave.
  173. *
  174. * @slave: The SPI slave
  175. */
  176. void spi_free_slave(struct spi_slave *slave);
  177. /**
  178. * Claim the bus and prepare it for communication with a given slave.
  179. *
  180. * This must be called before doing any transfers with a SPI slave. It
  181. * will enable and initialize any SPI hardware as necessary, and make
  182. * sure that the SCK line is in the correct idle state. It is not
  183. * allowed to claim the same bus for several slaves without releasing
  184. * the bus in between.
  185. *
  186. * @slave: The SPI slave
  187. *
  188. * Returns: 0 if the bus was claimed successfully, or a negative value
  189. * if it wasn't.
  190. */
  191. int spi_claim_bus(struct spi_slave *slave);
  192. /**
  193. * Release the SPI bus
  194. *
  195. * This must be called once for every call to spi_claim_bus() after
  196. * all transfers have finished. It may disable any SPI hardware as
  197. * appropriate.
  198. *
  199. * @slave: The SPI slave
  200. */
  201. void spi_release_bus(struct spi_slave *slave);
  202. /**
  203. * Set the word length for SPI transactions
  204. *
  205. * Set the word length (number of bits per word) for SPI transactions.
  206. *
  207. * @slave: The SPI slave
  208. * @wordlen: The number of bits in a word
  209. *
  210. * Returns: 0 on success, -1 on failure.
  211. */
  212. int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
  213. /**
  214. * SPI transfer
  215. *
  216. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  217. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  218. *
  219. * The source of the outgoing bits is the "dout" parameter and the
  220. * destination of the input bits is the "din" parameter. Note that "dout"
  221. * and "din" can point to the same memory location, in which case the
  222. * input data overwrites the output data (since both are buffered by
  223. * temporary variables, this is OK).
  224. *
  225. * spi_xfer() interface:
  226. * @slave: The SPI slave which will be sending/receiving the data.
  227. * @bitlen: How many bits to write and read.
  228. * @dout: Pointer to a string of bits to send out. The bits are
  229. * held in a byte array and are sent MSB first.
  230. * @din: Pointer to a string of bits that will be filled in.
  231. * @flags: A bitwise combination of SPI_XFER_* flags.
  232. *
  233. * Returns: 0 on success, not 0 on failure
  234. */
  235. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  236. void *din, unsigned long flags);
  237. /* Copy memory mapped data */
  238. void spi_flash_copy_mmap(void *data, void *offset, size_t len);
  239. /**
  240. * Determine if a SPI chipselect is valid.
  241. * This function is provided by the board if the low-level SPI driver
  242. * needs it to determine if a given chipselect is actually valid.
  243. *
  244. * Returns: 1 if bus:cs identifies a valid chip on this board, 0
  245. * otherwise.
  246. */
  247. int spi_cs_is_valid(unsigned int bus, unsigned int cs);
  248. #ifndef CONFIG_DM_SPI
  249. /**
  250. * Activate a SPI chipselect.
  251. * This function is provided by the board code when using a driver
  252. * that can't control its chipselects automatically (e.g.
  253. * common/soft_spi.c). When called, it should activate the chip select
  254. * to the device identified by "slave".
  255. */
  256. void spi_cs_activate(struct spi_slave *slave);
  257. /**
  258. * Deactivate a SPI chipselect.
  259. * This function is provided by the board code when using a driver
  260. * that can't control its chipselects automatically (e.g.
  261. * common/soft_spi.c). When called, it should deactivate the chip
  262. * select to the device identified by "slave".
  263. */
  264. void spi_cs_deactivate(struct spi_slave *slave);
  265. /**
  266. * Set transfer speed.
  267. * This sets a new speed to be applied for next spi_xfer().
  268. * @slave: The SPI slave
  269. * @hz: The transfer speed
  270. */
  271. void spi_set_speed(struct spi_slave *slave, uint hz);
  272. #endif
  273. /**
  274. * Write 8 bits, then read 8 bits.
  275. * @slave: The SPI slave we're communicating with
  276. * @byte: Byte to be written
  277. *
  278. * Returns: The value that was read, or a negative value on error.
  279. *
  280. * TODO: This function probably shouldn't be inlined.
  281. */
  282. static inline int spi_w8r8(struct spi_slave *slave, unsigned char byte)
  283. {
  284. unsigned char dout[2];
  285. unsigned char din[2];
  286. int ret;
  287. dout[0] = byte;
  288. dout[1] = 0;
  289. ret = spi_xfer(slave, 16, dout, din, SPI_XFER_BEGIN | SPI_XFER_END);
  290. return ret < 0 ? ret : din[1];
  291. }
  292. /**
  293. * Set up a SPI slave for a particular device tree node
  294. *
  295. * This calls spi_setup_slave() with the correct bus number. Call
  296. * spi_free_slave() to free it later.
  297. *
  298. * @param blob: Device tree blob
  299. * @param slave_node: Slave node to use
  300. * @param spi_node: SPI peripheral node to use
  301. * @return pointer to new spi_slave structure
  302. */
  303. struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
  304. int spi_node);
  305. /**
  306. * spi_base_setup_slave_fdt() - helper function to set up a SPI slace
  307. *
  308. * This decodes SPI properties from the slave node to determine the
  309. * chip select and SPI parameters.
  310. *
  311. * @blob: Device tree blob
  312. * @busnum: Bus number to use
  313. * @node: Device tree node for the SPI bus
  314. */
  315. struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
  316. int node);
  317. #ifdef CONFIG_DM_SPI
  318. /**
  319. * struct spi_cs_info - Information about a bus chip select
  320. *
  321. * @dev: Connected device, or NULL if none
  322. */
  323. struct spi_cs_info {
  324. struct udevice *dev;
  325. };
  326. /**
  327. * struct struct dm_spi_ops - Driver model SPI operations
  328. *
  329. * The uclass interface is implemented by all SPI devices which use
  330. * driver model.
  331. */
  332. struct dm_spi_ops {
  333. /**
  334. * Claim the bus and prepare it for communication.
  335. *
  336. * The device provided is the slave device. It's parent controller
  337. * will be used to provide the communication.
  338. *
  339. * This must be called before doing any transfers with a SPI slave. It
  340. * will enable and initialize any SPI hardware as necessary, and make
  341. * sure that the SCK line is in the correct idle state. It is not
  342. * allowed to claim the same bus for several slaves without releasing
  343. * the bus in between.
  344. *
  345. * @dev: The SPI slave
  346. *
  347. * Returns: 0 if the bus was claimed successfully, or a negative value
  348. * if it wasn't.
  349. */
  350. int (*claim_bus)(struct udevice *dev);
  351. /**
  352. * Release the SPI bus
  353. *
  354. * This must be called once for every call to spi_claim_bus() after
  355. * all transfers have finished. It may disable any SPI hardware as
  356. * appropriate.
  357. *
  358. * @dev: The SPI slave
  359. */
  360. int (*release_bus)(struct udevice *dev);
  361. /**
  362. * Set the word length for SPI transactions
  363. *
  364. * Set the word length (number of bits per word) for SPI transactions.
  365. *
  366. * @bus: The SPI slave
  367. * @wordlen: The number of bits in a word
  368. *
  369. * Returns: 0 on success, -ve on failure.
  370. */
  371. int (*set_wordlen)(struct udevice *dev, unsigned int wordlen);
  372. /**
  373. * SPI transfer
  374. *
  375. * This writes "bitlen" bits out the SPI MOSI port and simultaneously
  376. * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
  377. * works.
  378. *
  379. * The source of the outgoing bits is the "dout" parameter and the
  380. * destination of the input bits is the "din" parameter. Note that
  381. * "dout" and "din" can point to the same memory location, in which
  382. * case the input data overwrites the output data (since both are
  383. * buffered by temporary variables, this is OK).
  384. *
  385. * spi_xfer() interface:
  386. * @dev: The slave device to communicate with
  387. * @bitlen: How many bits to write and read.
  388. * @dout: Pointer to a string of bits to send out. The bits are
  389. * held in a byte array and are sent MSB first.
  390. * @din: Pointer to a string of bits that will be filled in.
  391. * @flags: A bitwise combination of SPI_XFER_* flags.
  392. *
  393. * Returns: 0 on success, not -1 on failure
  394. */
  395. int (*xfer)(struct udevice *dev, unsigned int bitlen, const void *dout,
  396. void *din, unsigned long flags);
  397. /**
  398. * Set transfer speed.
  399. * This sets a new speed to be applied for next spi_xfer().
  400. * @bus: The SPI bus
  401. * @hz: The transfer speed
  402. * @return 0 if OK, -ve on error
  403. */
  404. int (*set_speed)(struct udevice *bus, uint hz);
  405. /**
  406. * Set the SPI mode/flags
  407. *
  408. * It is unclear if we want to set speed and mode together instead
  409. * of separately.
  410. *
  411. * @bus: The SPI bus
  412. * @mode: Requested SPI mode (SPI_... flags)
  413. * @return 0 if OK, -ve on error
  414. */
  415. int (*set_mode)(struct udevice *bus, uint mode);
  416. /**
  417. * Get information on a chip select
  418. *
  419. * This is only called when the SPI uclass does not know about a
  420. * chip select, i.e. it has no attached device. It gives the driver
  421. * a chance to allow activity on that chip select even so.
  422. *
  423. * @bus: The SPI bus
  424. * @cs: The chip select (0..n-1)
  425. * @info: Returns information about the chip select, if valid.
  426. * On entry info->dev is NULL
  427. * @return 0 if OK (and @info is set up), -ENODEV if the chip select
  428. * is invalid, other -ve value on error
  429. */
  430. int (*cs_info)(struct udevice *bus, uint cs, struct spi_cs_info *info);
  431. };
  432. struct dm_spi_emul_ops {
  433. /**
  434. * SPI transfer
  435. *
  436. * This writes "bitlen" bits out the SPI MOSI port and simultaneously
  437. * clocks "bitlen" bits in the SPI MISO port. That's just the way SPI
  438. * works. Here the device is a slave.
  439. *
  440. * The source of the outgoing bits is the "dout" parameter and the
  441. * destination of the input bits is the "din" parameter. Note that
  442. * "dout" and "din" can point to the same memory location, in which
  443. * case the input data overwrites the output data (since both are
  444. * buffered by temporary variables, this is OK).
  445. *
  446. * spi_xfer() interface:
  447. * @slave: The SPI slave which will be sending/receiving the data.
  448. * @bitlen: How many bits to write and read.
  449. * @dout: Pointer to a string of bits sent to the device. The
  450. * bits are held in a byte array and are sent MSB first.
  451. * @din: Pointer to a string of bits that will be sent back to
  452. * the master.
  453. * @flags: A bitwise combination of SPI_XFER_* flags.
  454. *
  455. * Returns: 0 on success, not -1 on failure
  456. */
  457. int (*xfer)(struct udevice *slave, unsigned int bitlen,
  458. const void *dout, void *din, unsigned long flags);
  459. };
  460. /**
  461. * spi_find_bus_and_cs() - Find bus and slave devices by number
  462. *
  463. * Given a bus number and chip select, this finds the corresponding bus
  464. * device and slave device. Neither device is activated by this function,
  465. * although they may have been activated previously.
  466. *
  467. * @busnum: SPI bus number
  468. * @cs: Chip select to look for
  469. * @busp: Returns bus device
  470. * @devp: Return slave device
  471. * @return 0 if found, -ENODEV on error
  472. */
  473. int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
  474. struct udevice **devp);
  475. /**
  476. * spi_get_bus_and_cs() - Find and activate bus and slave devices by number
  477. *
  478. * Given a bus number and chip select, this finds the corresponding bus
  479. * device and slave device.
  480. *
  481. * If no such slave exists, and drv_name is not NULL, then a new slave device
  482. * is automatically bound on this chip select.
  483. *
  484. * Ths new slave device is probed ready for use with the given speed and mode.
  485. *
  486. * @busnum: SPI bus number
  487. * @cs: Chip select to look for
  488. * @speed: SPI speed to use for this slave
  489. * @mode: SPI mode to use for this slave
  490. * @drv_name: Name of driver to attach to this chip select
  491. * @dev_name: Name of the new device thus created
  492. * @busp: Returns bus device
  493. * @devp: Return slave device
  494. * @return 0 if found, -ve on error
  495. */
  496. int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
  497. const char *drv_name, const char *dev_name,
  498. struct udevice **busp, struct spi_slave **devp);
  499. /**
  500. * spi_chip_select() - Get the chip select for a slave
  501. *
  502. * @return the chip select this slave is attached to
  503. */
  504. int spi_chip_select(struct udevice *slave);
  505. /**
  506. * spi_find_chip_select() - Find the slave attached to chip select
  507. *
  508. * @bus: SPI bus to search
  509. * @cs: Chip select to look for
  510. * @devp: Returns the slave device if found
  511. * @return 0 if found, -ENODEV on error
  512. */
  513. int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp);
  514. /**
  515. * spi_slave_ofdata_to_platdata() - decode standard SPI platform data
  516. *
  517. * This decodes the speed and mode for a slave from a device tree node
  518. *
  519. * @blob: Device tree blob
  520. * @node: Node offset to read from
  521. * @plat: Place to put the decoded information
  522. */
  523. int spi_slave_ofdata_to_platdata(struct udevice *dev,
  524. struct dm_spi_slave_platdata *plat);
  525. /**
  526. * spi_cs_info() - Check information on a chip select
  527. *
  528. * This checks a particular chip select on a bus to see if it has a device
  529. * attached, or is even valid.
  530. *
  531. * @bus: The SPI bus
  532. * @cs: The chip select (0..n-1)
  533. * @info: Returns information about the chip select, if valid
  534. * @return 0 if OK (and @info is set up), -ENODEV if the chip select
  535. * is invalid, other -ve value on error
  536. */
  537. int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info);
  538. struct sandbox_state;
  539. /**
  540. * sandbox_spi_get_emul() - get an emulator for a SPI slave
  541. *
  542. * This provides a way to attach an emulated SPI device to a particular SPI
  543. * slave, so that xfer() operations on the slave will be handled by the
  544. * emulator. If a emulator already exists on that chip select it is returned.
  545. * Otherwise one is created.
  546. *
  547. * @state: Sandbox state
  548. * @bus: SPI bus requesting the emulator
  549. * @slave: SPI slave device requesting the emulator
  550. * @emuip: Returns pointer to emulator
  551. * @return 0 if OK, -ve on error
  552. */
  553. int sandbox_spi_get_emul(struct sandbox_state *state,
  554. struct udevice *bus, struct udevice *slave,
  555. struct udevice **emulp);
  556. /**
  557. * Claim the bus and prepare it for communication with a given slave.
  558. *
  559. * This must be called before doing any transfers with a SPI slave. It
  560. * will enable and initialize any SPI hardware as necessary, and make
  561. * sure that the SCK line is in the correct idle state. It is not
  562. * allowed to claim the same bus for several slaves without releasing
  563. * the bus in between.
  564. *
  565. * @dev: The SPI slave device
  566. *
  567. * Returns: 0 if the bus was claimed successfully, or a negative value
  568. * if it wasn't.
  569. */
  570. int dm_spi_claim_bus(struct udevice *dev);
  571. /**
  572. * Release the SPI bus
  573. *
  574. * This must be called once for every call to dm_spi_claim_bus() after
  575. * all transfers have finished. It may disable any SPI hardware as
  576. * appropriate.
  577. *
  578. * @slave: The SPI slave device
  579. */
  580. void dm_spi_release_bus(struct udevice *dev);
  581. /**
  582. * SPI transfer
  583. *
  584. * This writes "bitlen" bits out the SPI MOSI port and simultaneously clocks
  585. * "bitlen" bits in the SPI MISO port. That's just the way SPI works.
  586. *
  587. * The source of the outgoing bits is the "dout" parameter and the
  588. * destination of the input bits is the "din" parameter. Note that "dout"
  589. * and "din" can point to the same memory location, in which case the
  590. * input data overwrites the output data (since both are buffered by
  591. * temporary variables, this is OK).
  592. *
  593. * dm_spi_xfer() interface:
  594. * @dev: The SPI slave device which will be sending/receiving the data.
  595. * @bitlen: How many bits to write and read.
  596. * @dout: Pointer to a string of bits to send out. The bits are
  597. * held in a byte array and are sent MSB first.
  598. * @din: Pointer to a string of bits that will be filled in.
  599. * @flags: A bitwise combination of SPI_XFER_* flags.
  600. *
  601. * Returns: 0 on success, not 0 on failure
  602. */
  603. int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
  604. const void *dout, void *din, unsigned long flags);
  605. /* Access the operations for a SPI device */
  606. #define spi_get_ops(dev) ((struct dm_spi_ops *)(dev)->driver->ops)
  607. #define spi_emul_get_ops(dev) ((struct dm_spi_emul_ops *)(dev)->driver->ops)
  608. #endif /* CONFIG_DM_SPI */
  609. #endif /* _SPI_H_ */