spi-uclass.c 9.9 KB

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