ti-musb.c 8.0 KB

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