ti-musb.c 7.3 KB

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