spi-uclass.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * Copyright (c) 2014 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <fdtdec.h>
  10. #include <malloc.h>
  11. #include <spi.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/uclass-internal.h>
  14. #include <dm/lists.h>
  15. #include <dm/util.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
  18. {
  19. struct dm_spi_ops *ops;
  20. int ret;
  21. ops = spi_get_ops(bus);
  22. if (ops->set_speed)
  23. ret = ops->set_speed(bus, speed);
  24. else
  25. ret = -EINVAL;
  26. if (ret) {
  27. printf("Cannot set speed (err=%d)\n", ret);
  28. return ret;
  29. }
  30. if (ops->set_mode)
  31. ret = ops->set_mode(bus, mode);
  32. else
  33. ret = -EINVAL;
  34. if (ret) {
  35. printf("Cannot set mode (err=%d)\n", ret);
  36. return ret;
  37. }
  38. return 0;
  39. }
  40. int dm_spi_claim_bus(struct udevice *dev)
  41. {
  42. struct udevice *bus = dev->parent;
  43. struct dm_spi_ops *ops = spi_get_ops(bus);
  44. struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
  45. struct spi_slave *slave = dev_get_parent_priv(dev);
  46. int speed;
  47. int ret;
  48. speed = slave->max_hz;
  49. if (spi->max_hz) {
  50. if (speed)
  51. speed = min(speed, (int)spi->max_hz);
  52. else
  53. speed = spi->max_hz;
  54. }
  55. if (!speed)
  56. speed = 100000;
  57. if (speed != slave->speed) {
  58. ret = spi_set_speed_mode(bus, speed, slave->mode);
  59. if (ret)
  60. return ret;
  61. slave->speed = speed;
  62. }
  63. return ops->claim_bus ? ops->claim_bus(dev) : 0;
  64. }
  65. void dm_spi_release_bus(struct udevice *dev)
  66. {
  67. struct udevice *bus = dev->parent;
  68. struct dm_spi_ops *ops = spi_get_ops(bus);
  69. if (ops->release_bus)
  70. ops->release_bus(dev);
  71. }
  72. int dm_spi_xfer(struct udevice *dev, unsigned int bitlen,
  73. const void *dout, void *din, unsigned long flags)
  74. {
  75. struct udevice *bus = dev->parent;
  76. if (bus->uclass->uc_drv->id != UCLASS_SPI)
  77. return -EOPNOTSUPP;
  78. return spi_get_ops(bus)->xfer(dev, bitlen, dout, din, flags);
  79. }
  80. int spi_claim_bus(struct spi_slave *slave)
  81. {
  82. return dm_spi_claim_bus(slave->dev);
  83. }
  84. void spi_release_bus(struct spi_slave *slave)
  85. {
  86. dm_spi_release_bus(slave->dev);
  87. }
  88. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  89. const void *dout, void *din, unsigned long flags)
  90. {
  91. return dm_spi_xfer(slave->dev, bitlen, dout, din, flags);
  92. }
  93. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  94. static int spi_child_post_bind(struct udevice *dev)
  95. {
  96. struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
  97. if (dev->of_offset == -1)
  98. return 0;
  99. return spi_slave_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, plat);
  100. }
  101. #endif
  102. static int spi_post_probe(struct udevice *bus)
  103. {
  104. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  105. struct dm_spi_bus *spi = dev_get_uclass_priv(bus);
  106. spi->max_hz = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
  107. "spi-max-frequency", 0);
  108. #endif
  109. #if defined(CONFIG_NEEDS_MANUAL_RELOC)
  110. struct dm_spi_ops *ops = spi_get_ops(bus);
  111. if (ops->claim_bus)
  112. ops->claim_bus += gd->reloc_off;
  113. if (ops->release_bus)
  114. ops->release_bus += gd->reloc_off;
  115. if (ops->set_wordlen)
  116. ops->set_wordlen += gd->reloc_off;
  117. if (ops->xfer)
  118. ops->xfer += gd->reloc_off;
  119. if (ops->set_speed)
  120. ops->set_speed += gd->reloc_off;
  121. if (ops->set_mode)
  122. ops->set_mode += gd->reloc_off;
  123. if (ops->cs_info)
  124. ops->cs_info += gd->reloc_off;
  125. #endif
  126. return 0;
  127. }
  128. static int spi_child_pre_probe(struct udevice *dev)
  129. {
  130. struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
  131. struct spi_slave *slave = dev_get_parent_priv(dev);
  132. /*
  133. * This is needed because we pass struct spi_slave around the place
  134. * instead slave->dev (a struct udevice). So we have to have some
  135. * way to access the slave udevice given struct spi_slave. Once we
  136. * change the SPI API to use udevice instead of spi_slave, we can
  137. * drop this.
  138. */
  139. slave->dev = dev;
  140. slave->max_hz = plat->max_hz;
  141. slave->mode = plat->mode;
  142. slave->wordlen = SPI_DEFAULT_WORDLEN;
  143. return 0;
  144. }
  145. int spi_chip_select(struct udevice *dev)
  146. {
  147. struct dm_spi_slave_platdata *plat = dev_get_parent_platdata(dev);
  148. return plat ? plat->cs : -ENOENT;
  149. }
  150. int spi_find_chip_select(struct udevice *bus, int cs, struct udevice **devp)
  151. {
  152. struct udevice *dev;
  153. for (device_find_first_child(bus, &dev); dev;
  154. device_find_next_child(&dev)) {
  155. struct dm_spi_slave_platdata *plat;
  156. plat = dev_get_parent_platdata(dev);
  157. debug("%s: plat=%p, cs=%d\n", __func__, plat, plat->cs);
  158. if (plat->cs == cs) {
  159. *devp = dev;
  160. return 0;
  161. }
  162. }
  163. return -ENODEV;
  164. }
  165. int spi_cs_is_valid(unsigned int busnum, unsigned int cs)
  166. {
  167. struct spi_cs_info info;
  168. struct udevice *bus;
  169. int ret;
  170. ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
  171. if (ret) {
  172. debug("%s: No bus %d\n", __func__, busnum);
  173. return ret;
  174. }
  175. return spi_cs_info(bus, cs, &info);
  176. }
  177. int spi_cs_info(struct udevice *bus, uint cs, struct spi_cs_info *info)
  178. {
  179. struct spi_cs_info local_info;
  180. struct dm_spi_ops *ops;
  181. int ret;
  182. if (!info)
  183. info = &local_info;
  184. /* If there is a device attached, return it */
  185. info->dev = NULL;
  186. ret = spi_find_chip_select(bus, cs, &info->dev);
  187. if (!ret)
  188. return 0;
  189. /*
  190. * Otherwise ask the driver. For the moment we don't have CS info.
  191. * When we do we could provide the driver with a helper function
  192. * to figure out what chip selects are valid, or just handle the
  193. * request.
  194. */
  195. ops = spi_get_ops(bus);
  196. if (ops->cs_info)
  197. return ops->cs_info(bus, cs, info);
  198. /*
  199. * We could assume there is at least one valid chip select, but best
  200. * to be sure and return an error in this case. The driver didn't
  201. * care enough to tell us.
  202. */
  203. return -ENODEV;
  204. }
  205. int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
  206. struct udevice **devp)
  207. {
  208. struct udevice *bus, *dev;
  209. int ret;
  210. ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
  211. if (ret) {
  212. debug("%s: No bus %d\n", __func__, busnum);
  213. return ret;
  214. }
  215. ret = spi_find_chip_select(bus, cs, &dev);
  216. if (ret) {
  217. debug("%s: No cs %d\n", __func__, cs);
  218. return ret;
  219. }
  220. *busp = bus;
  221. *devp = dev;
  222. return ret;
  223. }
  224. int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
  225. const char *drv_name, const char *dev_name,
  226. struct udevice **busp, struct spi_slave **devp)
  227. {
  228. struct udevice *bus, *dev;
  229. struct dm_spi_slave_platdata *plat;
  230. bool created = false;
  231. int ret;
  232. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  233. ret = uclass_first_device_err(UCLASS_SPI, &bus);
  234. #else
  235. ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
  236. #endif
  237. if (ret) {
  238. printf("Invalid bus %d (err=%d)\n", busnum, ret);
  239. return ret;
  240. }
  241. ret = spi_find_chip_select(bus, cs, &dev);
  242. /*
  243. * If there is no such device, create one automatically. This means
  244. * that we don't need a device tree node or platform data for the
  245. * SPI flash chip - we will bind to the correct driver.
  246. */
  247. if (ret == -ENODEV && drv_name) {
  248. debug("%s: Binding new device '%s', busnum=%d, cs=%d, driver=%s\n",
  249. __func__, dev_name, busnum, cs, drv_name);
  250. ret = device_bind_driver(bus, drv_name, dev_name, &dev);
  251. if (ret) {
  252. debug("%s: Unable to bind driver (ret=%d)\n", __func__,
  253. ret);
  254. return ret;
  255. }
  256. plat = dev_get_parent_platdata(dev);
  257. plat->cs = cs;
  258. plat->max_hz = speed;
  259. plat->mode = mode;
  260. created = true;
  261. } else if (ret) {
  262. printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
  263. ret);
  264. return ret;
  265. }
  266. if (!device_active(dev)) {
  267. struct spi_slave *slave;
  268. ret = device_probe(dev);
  269. if (ret)
  270. goto err;
  271. slave = dev_get_parent_priv(dev);
  272. slave->dev = dev;
  273. }
  274. plat = dev_get_parent_platdata(dev);
  275. if (!speed) {
  276. speed = plat->max_hz;
  277. mode = plat->mode;
  278. }
  279. ret = spi_set_speed_mode(bus, speed, mode);
  280. if (ret)
  281. goto err;
  282. *busp = bus;
  283. *devp = dev_get_parent_priv(dev);
  284. debug("%s: bus=%p, slave=%p\n", __func__, bus, *devp);
  285. return 0;
  286. err:
  287. debug("%s: Error path, created=%d, device '%s'\n", __func__,
  288. created, dev->name);
  289. if (created) {
  290. device_remove(dev);
  291. device_unbind(dev);
  292. }
  293. return ret;
  294. }
  295. /* Compatibility function - to be removed */
  296. struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
  297. int bus_node)
  298. {
  299. struct udevice *bus, *dev;
  300. int ret;
  301. ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
  302. if (ret)
  303. return NULL;
  304. ret = device_get_child_by_of_offset(bus, node, &dev);
  305. if (ret)
  306. return NULL;
  307. return dev_get_parent_priv(dev);
  308. }
  309. /* Compatibility function - to be removed */
  310. struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
  311. unsigned int speed, unsigned int mode)
  312. {
  313. struct spi_slave *slave;
  314. struct udevice *dev;
  315. int ret;
  316. ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
  317. &slave);
  318. if (ret)
  319. return NULL;
  320. return slave;
  321. }
  322. void spi_free_slave(struct spi_slave *slave)
  323. {
  324. device_remove(slave->dev);
  325. slave->dev = NULL;
  326. }
  327. int spi_slave_ofdata_to_platdata(const void *blob, int node,
  328. struct dm_spi_slave_platdata *plat)
  329. {
  330. int mode = 0;
  331. int value;
  332. plat->cs = fdtdec_get_int(blob, node, "reg", -1);
  333. plat->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
  334. if (fdtdec_get_bool(blob, node, "spi-cpol"))
  335. mode |= SPI_CPOL;
  336. if (fdtdec_get_bool(blob, node, "spi-cpha"))
  337. mode |= SPI_CPHA;
  338. if (fdtdec_get_bool(blob, node, "spi-cs-high"))
  339. mode |= SPI_CS_HIGH;
  340. if (fdtdec_get_bool(blob, node, "spi-3wire"))
  341. mode |= SPI_3WIRE;
  342. if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
  343. mode |= SPI_PREAMBLE;
  344. /* Device DUAL/QUAD mode */
  345. value = fdtdec_get_uint(blob, node, "spi-tx-bus-width", 1);
  346. switch (value) {
  347. case 1:
  348. break;
  349. case 2:
  350. mode |= SPI_TX_DUAL;
  351. break;
  352. case 4:
  353. mode |= SPI_TX_QUAD;
  354. break;
  355. default:
  356. warn_non_spl("spi-tx-bus-width %d not supported\n", value);
  357. break;
  358. }
  359. value = fdtdec_get_uint(blob, node, "spi-rx-bus-width", 1);
  360. switch (value) {
  361. case 1:
  362. break;
  363. case 2:
  364. mode |= SPI_RX_DUAL;
  365. break;
  366. case 4:
  367. mode |= SPI_RX_QUAD;
  368. break;
  369. default:
  370. warn_non_spl("spi-rx-bus-width %d not supported\n", value);
  371. break;
  372. }
  373. plat->mode = mode;
  374. return 0;
  375. }
  376. UCLASS_DRIVER(spi) = {
  377. .id = UCLASS_SPI,
  378. .name = "spi",
  379. .flags = DM_UC_FLAG_SEQ_ALIAS,
  380. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  381. .post_bind = dm_scan_fdt_dev,
  382. #endif
  383. .post_probe = spi_post_probe,
  384. .child_pre_probe = spi_child_pre_probe,
  385. .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
  386. .per_child_auto_alloc_size = sizeof(struct spi_slave),
  387. .per_child_platdata_auto_alloc_size =
  388. sizeof(struct dm_spi_slave_platdata),
  389. #if !CONFIG_IS_ENABLED(OF_PLATDATA)
  390. .child_post_bind = spi_child_post_bind,
  391. #endif
  392. };
  393. UCLASS_DRIVER(spi_generic) = {
  394. .id = UCLASS_SPI_GENERIC,
  395. .name = "spi_generic",
  396. };
  397. U_BOOT_DRIVER(spi_generic_drv) = {
  398. .name = "spi_generic_drv",
  399. .id = UCLASS_SPI_GENERIC,
  400. };