spi-uclass.c 9.9 KB

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