spi.h 23 KB

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