ti-musb.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * MISC driver for TI MUSB Glue.
  4. *
  5. * (C) Copyright 2016
  6. * Texas Instruments Incorporated, <www.ti.com>
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <console.h>
  11. #include <dm.h>
  12. #include <log.h>
  13. #include <malloc.h>
  14. #include <linux/usb/otg.h>
  15. #include <dm/device-internal.h>
  16. #include <dm/lists.h>
  17. #include <asm/io.h>
  18. #include <asm/omap_musb.h>
  19. #include "musb_uboot.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. #if CONFIG_IS_ENABLED(DM_USB)
  22. /* USB 2.0 PHY Control */
  23. #define CM_PHY_PWRDN (1 << 0)
  24. #define CM_PHY_OTG_PWRDN (1 << 1)
  25. #define OTGVDET_EN (1 << 19)
  26. #define OTGSESSENDEN (1 << 20)
  27. #define AM335X_USB0_CTRL 0x0
  28. #define AM335X_USB1_CTRL 0x8
  29. static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
  30. {
  31. struct ti_musb_plat *plat = dev_get_plat(dev);
  32. if (!plat->ctrl_mod_base)
  33. return;
  34. if (on) {
  35. clrsetbits_le32(plat->ctrl_mod_base,
  36. CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
  37. OTGVDET_EN | OTGSESSENDEN);
  38. } else {
  39. clrsetbits_le32(plat->ctrl_mod_base, 0,
  40. CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
  41. }
  42. }
  43. #if CONFIG_IS_ENABLED(OF_CONTROL)
  44. static int ti_musb_get_usb_index(int node)
  45. {
  46. const void *fdt = gd->fdt_blob;
  47. int i = 0;
  48. char path[64];
  49. const char *alias_path;
  50. char alias[16];
  51. fdt_get_path(fdt, node, path, sizeof(path));
  52. do {
  53. snprintf(alias, sizeof(alias), "usb%d", i);
  54. alias_path = fdt_get_alias(fdt, alias);
  55. if (alias_path == NULL) {
  56. debug("USB index not found\n");
  57. return -ENOENT;
  58. }
  59. if (!strcmp(path, alias_path))
  60. return i;
  61. i++;
  62. } while (alias_path);
  63. return -ENOENT;
  64. }
  65. static int ti_musb_of_to_plat(struct udevice *dev)
  66. {
  67. struct ti_musb_plat *plat = dev_get_plat(dev);
  68. const void *fdt = gd->fdt_blob;
  69. int node = dev_of_offset(dev);
  70. int phys;
  71. int ctrl_mod;
  72. int usb_index;
  73. struct musb_hdrc_config *musb_config;
  74. plat->base = (void *)devfdt_get_addr_index(dev, 1);
  75. phys = fdtdec_lookup_phandle(fdt, node, "phys");
  76. ctrl_mod = fdtdec_lookup_phandle(fdt, phys, "ti,ctrl_mod");
  77. plat->ctrl_mod_base = (void *)fdtdec_get_addr(fdt, ctrl_mod, "reg");
  78. usb_index = ti_musb_get_usb_index(node);
  79. switch (usb_index) {
  80. case 1:
  81. plat->ctrl_mod_base += AM335X_USB1_CTRL;
  82. break;
  83. case 0:
  84. plat->ctrl_mod_base += AM335X_USB0_CTRL;
  85. break;
  86. default:
  87. break;
  88. }
  89. musb_config = malloc(sizeof(struct musb_hdrc_config));
  90. memset(musb_config, 0, sizeof(struct musb_hdrc_config));
  91. musb_config->multipoint = fdtdec_get_int(fdt, node,
  92. "mentor,multipoint", -1);
  93. if (musb_config->multipoint < 0) {
  94. pr_err("MUSB multipoint DT entry missing\n");
  95. return -ENOENT;
  96. }
  97. musb_config->dyn_fifo = 1;
  98. musb_config->num_eps = fdtdec_get_int(fdt, node, "mentor,num-eps",
  99. -1);
  100. if (musb_config->num_eps < 0) {
  101. pr_err("MUSB num-eps DT entry missing\n");
  102. return -ENOENT;
  103. }
  104. musb_config->ram_bits = fdtdec_get_int(fdt, node, "mentor,ram-bits",
  105. -1);
  106. if (musb_config->ram_bits < 0) {
  107. pr_err("MUSB ram-bits DT entry missing\n");
  108. return -ENOENT;
  109. }
  110. plat->plat.config = musb_config;
  111. plat->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
  112. if (plat->plat.power < 0) {
  113. pr_err("MUSB mentor,power DT entry missing\n");
  114. return -ENOENT;
  115. }
  116. plat->plat.platform_ops = &musb_dsps_ops;
  117. return 0;
  118. }
  119. #endif
  120. static int ti_musb_host_probe(struct udevice *dev)
  121. {
  122. struct musb_host_data *host = dev_get_priv(dev);
  123. struct ti_musb_plat *plat = dev_get_plat(dev);
  124. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  125. int ret;
  126. priv->desc_before_addr = true;
  127. host->host = musb_init_controller(&plat->plat,
  128. NULL,
  129. plat->base);
  130. if (!host->host)
  131. return -EIO;
  132. ti_musb_set_phy_power(dev, 1);
  133. ret = musb_lowlevel_init(host);
  134. return ret;
  135. }
  136. static int ti_musb_host_remove(struct udevice *dev)
  137. {
  138. struct musb_host_data *host = dev_get_priv(dev);
  139. musb_stop(host->host);
  140. ti_musb_set_phy_power(dev, 0);
  141. return 0;
  142. }
  143. #if CONFIG_IS_ENABLED(OF_CONTROL)
  144. static int ti_musb_host_of_to_plat(struct udevice *dev)
  145. {
  146. struct ti_musb_plat *plat = dev_get_plat(dev);
  147. const void *fdt = gd->fdt_blob;
  148. int node = dev_of_offset(dev);
  149. int ret;
  150. ret = ti_musb_of_to_plat(dev);
  151. if (ret) {
  152. pr_err("plat dt parse error\n");
  153. return ret;
  154. }
  155. plat->plat.mode = MUSB_HOST;
  156. return 0;
  157. }
  158. #endif
  159. U_BOOT_DRIVER(ti_musb_host) = {
  160. .name = "ti-musb-host",
  161. .id = UCLASS_USB,
  162. #if CONFIG_IS_ENABLED(OF_CONTROL)
  163. .of_to_plat = ti_musb_host_of_to_plat,
  164. #endif
  165. .probe = ti_musb_host_probe,
  166. .remove = ti_musb_host_remove,
  167. .ops = &musb_usb_ops,
  168. .plat_auto = sizeof(struct ti_musb_plat),
  169. .priv_auto = sizeof(struct musb_host_data),
  170. };
  171. #if CONFIG_IS_ENABLED(DM_USB_GADGET)
  172. struct ti_musb_peripheral {
  173. struct musb *periph;
  174. };
  175. #if CONFIG_IS_ENABLED(OF_CONTROL)
  176. static int ti_musb_peripheral_of_to_plat(struct udevice *dev)
  177. {
  178. struct ti_musb_plat *plat = dev_get_plat(dev);
  179. const void *fdt = gd->fdt_blob;
  180. int node = dev_of_offset(dev);
  181. int ret;
  182. ret = ti_musb_of_to_plat(dev);
  183. if (ret) {
  184. pr_err("plat dt parse error\n");
  185. return ret;
  186. }
  187. plat->plat.mode = MUSB_PERIPHERAL;
  188. return 0;
  189. }
  190. #endif
  191. int dm_usb_gadget_handle_interrupts(struct udevice *dev)
  192. {
  193. struct ti_musb_peripheral *priv = dev_get_priv(dev);
  194. priv->periph->isr(0, priv->periph);
  195. return 0;
  196. }
  197. static int ti_musb_peripheral_probe(struct udevice *dev)
  198. {
  199. struct ti_musb_peripheral *priv = dev_get_priv(dev);
  200. struct ti_musb_plat *plat = dev_get_plat(dev);
  201. int ret;
  202. priv->periph = musb_init_controller(&plat->plat,
  203. NULL,
  204. plat->base);
  205. if (!priv->periph)
  206. return -EIO;
  207. ti_musb_set_phy_power(dev, 1);
  208. musb_gadget_setup(priv->periph);
  209. return usb_add_gadget_udc((struct device *)dev, &priv->periph->g);
  210. }
  211. static int ti_musb_peripheral_remove(struct udevice *dev)
  212. {
  213. struct ti_musb_peripheral *priv = dev_get_priv(dev);
  214. usb_del_gadget_udc(&priv->periph->g);
  215. ti_musb_set_phy_power(dev, 0);
  216. return 0;
  217. }
  218. U_BOOT_DRIVER(ti_musb_peripheral) = {
  219. .name = "ti-musb-peripheral",
  220. .id = UCLASS_USB_GADGET_GENERIC,
  221. #if CONFIG_IS_ENABLED(OF_CONTROL)
  222. .of_to_plat = ti_musb_peripheral_of_to_plat,
  223. #endif
  224. .probe = ti_musb_peripheral_probe,
  225. .remove = ti_musb_peripheral_remove,
  226. .ops = &musb_usb_ops,
  227. .plat_auto = sizeof(struct ti_musb_plat),
  228. .priv_auto = sizeof(struct ti_musb_peripheral),
  229. .flags = DM_FLAG_PRE_RELOC,
  230. };
  231. #endif
  232. #if CONFIG_IS_ENABLED(OF_CONTROL)
  233. static int ti_musb_wrapper_bind(struct udevice *parent)
  234. {
  235. ofnode node;
  236. int ret;
  237. ofnode_for_each_subnode(node, dev_ofnode(parent)) {
  238. struct udevice *dev;
  239. const char *name = ofnode_get_name(node);
  240. enum usb_dr_mode dr_mode;
  241. struct driver *drv;
  242. if (strncmp(name, "usb@", 4))
  243. continue;
  244. dr_mode = usb_get_dr_mode(node);
  245. switch (dr_mode) {
  246. case USB_DR_MODE_PERIPHERAL:
  247. /* Bind MUSB device */
  248. ret = device_bind_driver_to_node(parent,
  249. "ti-musb-peripheral",
  250. name,
  251. node,
  252. &dev);
  253. if (ret)
  254. pr_err("musb - not able to bind usb peripheral node\n");
  255. break;
  256. case USB_DR_MODE_HOST:
  257. /* Bind MUSB host */
  258. ret = device_bind_driver_to_node(parent,
  259. "ti-musb-host",
  260. name,
  261. node,
  262. &dev);
  263. if (ret)
  264. pr_err("musb - not able to bind usb host node\n");
  265. break;
  266. default:
  267. break;
  268. };
  269. }
  270. return 0;
  271. }
  272. static const struct udevice_id ti_musb_ids[] = {
  273. { .compatible = "ti,am33xx-usb" },
  274. { }
  275. };
  276. U_BOOT_DRIVER(ti_musb_wrapper) = {
  277. .name = "ti-musb-wrapper",
  278. .id = UCLASS_MISC,
  279. .of_match = ti_musb_ids,
  280. .bind = ti_musb_wrapper_bind,
  281. };
  282. #endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
  283. #endif /* CONFIG_IS_ENABLED(DM_USB) */