ti-musb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 <linux/usb/otg.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/lists.h>
  15. #include <asm/io.h>
  16. #include <asm/omap_musb.h>
  17. #include "musb_uboot.h"
  18. DECLARE_GLOBAL_DATA_PTR;
  19. #ifdef CONFIG_DM_USB
  20. /* USB 2.0 PHY Control */
  21. #define CM_PHY_PWRDN (1 << 0)
  22. #define CM_PHY_OTG_PWRDN (1 << 1)
  23. #define OTGVDET_EN (1 << 19)
  24. #define OTGSESSENDEN (1 << 20)
  25. #define AM335X_USB1_CTRL 0x8
  26. struct ti_musb_platdata {
  27. void *base;
  28. void *ctrl_mod_base;
  29. struct musb_hdrc_platform_data plat;
  30. struct musb_hdrc_config musb_config;
  31. struct omap_musb_board_data otg_board_data;
  32. };
  33. static int ti_musb_get_usb_index(int node)
  34. {
  35. const void *fdt = gd->fdt_blob;
  36. int i = 0;
  37. char path[64];
  38. const char *alias_path;
  39. char alias[16];
  40. fdt_get_path(fdt, node, path, sizeof(path));
  41. do {
  42. snprintf(alias, sizeof(alias), "usb%d", i);
  43. alias_path = fdt_get_alias(fdt, alias);
  44. if (alias_path == NULL) {
  45. debug("USB index not found\n");
  46. return -ENOENT;
  47. }
  48. if (!strcmp(path, alias_path))
  49. return i;
  50. i++;
  51. } while (alias_path);
  52. return -ENOENT;
  53. }
  54. static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
  55. {
  56. struct ti_musb_platdata *platdata = dev_get_platdata(dev);
  57. if (on) {
  58. clrsetbits_le32(platdata->ctrl_mod_base,
  59. CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
  60. OTGVDET_EN | OTGSESSENDEN);
  61. } else {
  62. clrsetbits_le32(platdata->ctrl_mod_base, 0,
  63. CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
  64. }
  65. }
  66. static int ti_musb_ofdata_to_platdata(struct udevice *dev)
  67. {
  68. struct ti_musb_platdata *platdata = dev_get_platdata(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. platdata->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. platdata->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. platdata->ctrl_mod_base += AM335X_USB1_CTRL;
  82. case 0:
  83. default:
  84. break;
  85. }
  86. platdata->musb_config.multipoint = fdtdec_get_int(fdt, node,
  87. "mentor,multipoint",
  88. -1);
  89. if (platdata->musb_config.multipoint < 0) {
  90. pr_err("MUSB multipoint DT entry missing\n");
  91. return -ENOENT;
  92. }
  93. platdata->musb_config.dyn_fifo = 1;
  94. platdata->musb_config.num_eps = fdtdec_get_int(fdt, node,
  95. "mentor,num-eps", -1);
  96. if (platdata->musb_config.num_eps < 0) {
  97. pr_err("MUSB num-eps DT entry missing\n");
  98. return -ENOENT;
  99. }
  100. platdata->musb_config.ram_bits = fdtdec_get_int(fdt, node,
  101. "mentor,ram-bits", -1);
  102. if (platdata->musb_config.ram_bits < 0) {
  103. pr_err("MUSB ram-bits DT entry missing\n");
  104. return -ENOENT;
  105. }
  106. platdata->otg_board_data.set_phy_power = ti_musb_set_phy_power;
  107. platdata->otg_board_data.dev = dev;
  108. platdata->plat.config = &platdata->musb_config;
  109. platdata->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
  110. if (platdata->plat.power < 0) {
  111. pr_err("MUSB mentor,power DT entry missing\n");
  112. return -ENOENT;
  113. }
  114. platdata->plat.platform_ops = &musb_dsps_ops;
  115. platdata->plat.board_data = &platdata->otg_board_data;
  116. return 0;
  117. }
  118. static int ti_musb_host_probe(struct udevice *dev)
  119. {
  120. struct musb_host_data *host = dev_get_priv(dev);
  121. struct ti_musb_platdata *platdata = dev_get_platdata(dev);
  122. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  123. struct omap_musb_board_data *otg_board_data;
  124. int ret;
  125. priv->desc_before_addr = true;
  126. otg_board_data = &platdata->otg_board_data;
  127. host->host = musb_init_controller(&platdata->plat,
  128. (struct device *)otg_board_data,
  129. platdata->base);
  130. if (!host->host)
  131. return -EIO;
  132. ret = musb_lowlevel_init(host);
  133. return ret;
  134. }
  135. static int ti_musb_host_remove(struct udevice *dev)
  136. {
  137. struct musb_host_data *host = dev_get_priv(dev);
  138. musb_stop(host->host);
  139. return 0;
  140. }
  141. static int ti_musb_host_ofdata_to_platdata(struct udevice *dev)
  142. {
  143. struct ti_musb_platdata *platdata = dev_get_platdata(dev);
  144. const void *fdt = gd->fdt_blob;
  145. int node = dev_of_offset(dev);
  146. int ret;
  147. ret = ti_musb_ofdata_to_platdata(dev);
  148. if (ret) {
  149. pr_err("platdata dt parse error\n");
  150. return ret;
  151. }
  152. platdata->plat.mode = MUSB_HOST;
  153. return 0;
  154. }
  155. U_BOOT_DRIVER(ti_musb_host) = {
  156. .name = "ti-musb-host",
  157. .id = UCLASS_USB,
  158. .ofdata_to_platdata = ti_musb_host_ofdata_to_platdata,
  159. .probe = ti_musb_host_probe,
  160. .remove = ti_musb_host_remove,
  161. .ops = &musb_usb_ops,
  162. .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata),
  163. .priv_auto_alloc_size = sizeof(struct musb_host_data),
  164. };
  165. static int ti_musb_wrapper_bind(struct udevice *parent)
  166. {
  167. const void *fdt = gd->fdt_blob;
  168. int node;
  169. int ret;
  170. for (node = fdt_first_subnode(fdt, dev_of_offset(parent)); node > 0;
  171. node = fdt_next_subnode(fdt, node)) {
  172. struct udevice *dev;
  173. const char *name = fdt_get_name(fdt, node, NULL);
  174. enum usb_dr_mode dr_mode;
  175. struct driver *drv;
  176. if (strncmp(name, "usb@", 4))
  177. continue;
  178. dr_mode = usb_get_dr_mode(node);
  179. switch (dr_mode) {
  180. case USB_DR_MODE_PERIPHERAL:
  181. /* Bind MUSB device */
  182. break;
  183. case USB_DR_MODE_HOST:
  184. /* Bind MUSB host */
  185. ret = device_bind_driver_to_node(parent, "ti-musb-host",
  186. name, offset_to_ofnode(node), &dev);
  187. if (ret) {
  188. pr_err("musb - not able to bind usb host node\n");
  189. return ret;
  190. }
  191. break;
  192. default:
  193. break;
  194. };
  195. }
  196. return 0;
  197. }
  198. static const struct udevice_id ti_musb_ids[] = {
  199. { .compatible = "ti,am33xx-usb" },
  200. { }
  201. };
  202. U_BOOT_DRIVER(ti_musb_wrapper) = {
  203. .name = "ti-musb-wrapper",
  204. .id = UCLASS_MISC,
  205. .of_match = ti_musb_ids,
  206. .bind = ti_musb_wrapper_bind,
  207. };
  208. #endif /* CONFIG_DM_USB */