fsl-mph-dr-of.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Setup platform devices needed by the Freescale multi-port host
  3. * and/or dual-role USB controller modules based on the description
  4. * in flat device tree.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/fsl_devices.h>
  14. #include <linux/err.h>
  15. #include <linux/io.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/clk.h>
  18. #include <linux/module.h>
  19. struct fsl_usb2_dev_data {
  20. char *dr_mode; /* controller mode */
  21. char *drivers[3]; /* drivers to instantiate for this mode */
  22. enum fsl_usb2_operating_modes op_mode; /* operating mode */
  23. };
  24. struct fsl_usb2_dev_data dr_mode_data[] = {
  25. {
  26. .dr_mode = "host",
  27. .drivers = { "fsl-ehci", NULL, NULL, },
  28. .op_mode = FSL_USB2_DR_HOST,
  29. },
  30. {
  31. .dr_mode = "otg",
  32. .drivers = { "fsl-usb2-otg", "fsl-ehci", "fsl-usb2-udc", },
  33. .op_mode = FSL_USB2_DR_OTG,
  34. },
  35. {
  36. .dr_mode = "peripheral",
  37. .drivers = { "fsl-usb2-udc", NULL, NULL, },
  38. .op_mode = FSL_USB2_DR_DEVICE,
  39. },
  40. };
  41. struct fsl_usb2_dev_data *get_dr_mode_data(struct device_node *np)
  42. {
  43. const unsigned char *prop;
  44. int i;
  45. prop = of_get_property(np, "dr_mode", NULL);
  46. if (prop) {
  47. for (i = 0; i < ARRAY_SIZE(dr_mode_data); i++) {
  48. if (!strcmp(prop, dr_mode_data[i].dr_mode))
  49. return &dr_mode_data[i];
  50. }
  51. }
  52. pr_warn("%s: Invalid 'dr_mode' property, fallback to host mode\n",
  53. np->full_name);
  54. return &dr_mode_data[0]; /* mode not specified, use host */
  55. }
  56. static enum fsl_usb2_phy_modes determine_usb_phy(const char *phy_type)
  57. {
  58. if (!phy_type)
  59. return FSL_USB2_PHY_NONE;
  60. if (!strcasecmp(phy_type, "ulpi"))
  61. return FSL_USB2_PHY_ULPI;
  62. if (!strcasecmp(phy_type, "utmi"))
  63. return FSL_USB2_PHY_UTMI;
  64. if (!strcasecmp(phy_type, "utmi_wide"))
  65. return FSL_USB2_PHY_UTMI_WIDE;
  66. if (!strcasecmp(phy_type, "serial"))
  67. return FSL_USB2_PHY_SERIAL;
  68. return FSL_USB2_PHY_NONE;
  69. }
  70. struct platform_device *fsl_usb2_device_register(
  71. struct platform_device *ofdev,
  72. struct fsl_usb2_platform_data *pdata,
  73. const char *name, int id)
  74. {
  75. struct platform_device *pdev;
  76. const struct resource *res = ofdev->resource;
  77. unsigned int num = ofdev->num_resources;
  78. int retval;
  79. pdev = platform_device_alloc(name, id);
  80. if (!pdev) {
  81. retval = -ENOMEM;
  82. goto error;
  83. }
  84. pdev->dev.parent = &ofdev->dev;
  85. pdev->dev.coherent_dma_mask = ofdev->dev.coherent_dma_mask;
  86. *pdev->dev.dma_mask = *ofdev->dev.dma_mask;
  87. retval = platform_device_add_data(pdev, pdata, sizeof(*pdata));
  88. if (retval)
  89. goto error;
  90. if (num) {
  91. retval = platform_device_add_resources(pdev, res, num);
  92. if (retval)
  93. goto error;
  94. }
  95. retval = platform_device_add(pdev);
  96. if (retval)
  97. goto error;
  98. return pdev;
  99. error:
  100. platform_device_put(pdev);
  101. return ERR_PTR(retval);
  102. }
  103. static const struct of_device_id fsl_usb2_mph_dr_of_match[];
  104. static int usb_get_ver_info(struct device_node *np)
  105. {
  106. int ver = -1;
  107. /*
  108. * returns 1 for usb controller version 1.6
  109. * returns 2 for usb controller version 2.2
  110. * returns 0 otherwise
  111. */
  112. if (of_device_is_compatible(np, "fsl-usb2-dr")) {
  113. if (of_device_is_compatible(np, "fsl-usb2-dr-v1.6"))
  114. ver = FSL_USB_VER_1_6;
  115. else if (of_device_is_compatible(np, "fsl-usb2-dr-v2.2"))
  116. ver = FSL_USB_VER_2_2;
  117. else if (of_device_is_compatible(np, "fsl-usb2-dr-v2.4"))
  118. ver = FSL_USB_VER_2_4;
  119. else /* for previous controller versions */
  120. ver = FSL_USB_VER_OLD;
  121. if (ver > -1)
  122. return ver;
  123. }
  124. if (of_device_is_compatible(np, "fsl,mpc5121-usb2-dr"))
  125. return FSL_USB_VER_OLD;
  126. if (of_device_is_compatible(np, "fsl-usb2-mph")) {
  127. if (of_device_is_compatible(np, "fsl-usb2-mph-v1.6"))
  128. ver = FSL_USB_VER_1_6;
  129. else if (of_device_is_compatible(np, "fsl-usb2-mph-v2.2"))
  130. ver = FSL_USB_VER_2_2;
  131. else /* for previous controller versions */
  132. ver = FSL_USB_VER_OLD;
  133. }
  134. return ver;
  135. }
  136. static int fsl_usb2_mph_dr_of_probe(struct platform_device *ofdev)
  137. {
  138. struct device_node *np = ofdev->dev.of_node;
  139. struct platform_device *usb_dev;
  140. struct fsl_usb2_platform_data data, *pdata;
  141. struct fsl_usb2_dev_data *dev_data;
  142. const struct of_device_id *match;
  143. const unsigned char *prop;
  144. static unsigned int idx;
  145. int i;
  146. if (!of_device_is_available(np))
  147. return -ENODEV;
  148. match = of_match_device(fsl_usb2_mph_dr_of_match, &ofdev->dev);
  149. if (!match)
  150. return -ENODEV;
  151. pdata = &data;
  152. if (match->data)
  153. memcpy(pdata, match->data, sizeof(data));
  154. else
  155. memset(pdata, 0, sizeof(data));
  156. dev_data = get_dr_mode_data(np);
  157. if (of_device_is_compatible(np, "fsl-usb2-mph")) {
  158. if (of_get_property(np, "port0", NULL))
  159. pdata->port_enables |= FSL_USB2_PORT0_ENABLED;
  160. if (of_get_property(np, "port1", NULL))
  161. pdata->port_enables |= FSL_USB2_PORT1_ENABLED;
  162. pdata->operating_mode = FSL_USB2_MPH_HOST;
  163. } else {
  164. if (of_get_property(np, "fsl,invert-drvvbus", NULL))
  165. pdata->invert_drvvbus = 1;
  166. if (of_get_property(np, "fsl,invert-pwr-fault", NULL))
  167. pdata->invert_pwr_fault = 1;
  168. /* setup mode selected in the device tree */
  169. pdata->operating_mode = dev_data->op_mode;
  170. }
  171. prop = of_get_property(np, "phy_type", NULL);
  172. pdata->phy_mode = determine_usb_phy(prop);
  173. pdata->controller_ver = usb_get_ver_info(np);
  174. if (pdata->have_sysif_regs) {
  175. if (pdata->controller_ver < 0) {
  176. dev_warn(&ofdev->dev, "Could not get controller version\n");
  177. return -ENODEV;
  178. }
  179. }
  180. for (i = 0; i < ARRAY_SIZE(dev_data->drivers); i++) {
  181. if (!dev_data->drivers[i])
  182. continue;
  183. usb_dev = fsl_usb2_device_register(ofdev, pdata,
  184. dev_data->drivers[i], idx);
  185. if (IS_ERR(usb_dev)) {
  186. dev_err(&ofdev->dev, "Can't register usb device\n");
  187. return PTR_ERR(usb_dev);
  188. }
  189. }
  190. idx++;
  191. return 0;
  192. }
  193. static int __unregister_subdev(struct device *dev, void *d)
  194. {
  195. platform_device_unregister(to_platform_device(dev));
  196. return 0;
  197. }
  198. static int fsl_usb2_mph_dr_of_remove(struct platform_device *ofdev)
  199. {
  200. device_for_each_child(&ofdev->dev, NULL, __unregister_subdev);
  201. return 0;
  202. }
  203. #ifdef CONFIG_PPC_MPC512x
  204. #define USBGENCTRL 0x200 /* NOTE: big endian */
  205. #define GC_WU_INT_CLR (1 << 5) /* Wakeup int clear */
  206. #define GC_ULPI_SEL (1 << 4) /* ULPI i/f select (usb0 only)*/
  207. #define GC_PPP (1 << 3) /* Inv. Port Power Polarity */
  208. #define GC_PFP (1 << 2) /* Inv. Power Fault Polarity */
  209. #define GC_WU_ULPI_EN (1 << 1) /* Wakeup on ULPI event */
  210. #define GC_WU_IE (1 << 1) /* Wakeup interrupt enable */
  211. #define ISIPHYCTRL 0x204 /* NOTE: big endian */
  212. #define PHYCTRL_PHYE (1 << 4) /* On-chip UTMI PHY enable */
  213. #define PHYCTRL_BSENH (1 << 3) /* Bit Stuff Enable High */
  214. #define PHYCTRL_BSEN (1 << 2) /* Bit Stuff Enable */
  215. #define PHYCTRL_LSFE (1 << 1) /* Line State Filter Enable */
  216. #define PHYCTRL_PXE (1 << 0) /* PHY oscillator enable */
  217. int fsl_usb2_mpc5121_init(struct platform_device *pdev)
  218. {
  219. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  220. struct clk *clk;
  221. char clk_name[10];
  222. int base, clk_num;
  223. base = pdev->resource->start & 0xf000;
  224. if (base == 0x3000)
  225. clk_num = 1;
  226. else if (base == 0x4000)
  227. clk_num = 2;
  228. else
  229. return -ENODEV;
  230. snprintf(clk_name, sizeof(clk_name), "usb%d_clk", clk_num);
  231. clk = clk_get(&pdev->dev, clk_name);
  232. if (IS_ERR(clk)) {
  233. dev_err(&pdev->dev, "failed to get clk\n");
  234. return PTR_ERR(clk);
  235. }
  236. clk_enable(clk);
  237. pdata->clk = clk;
  238. if (pdata->phy_mode == FSL_USB2_PHY_UTMI_WIDE) {
  239. u32 reg = 0;
  240. if (pdata->invert_drvvbus)
  241. reg |= GC_PPP;
  242. if (pdata->invert_pwr_fault)
  243. reg |= GC_PFP;
  244. out_be32(pdata->regs + ISIPHYCTRL, PHYCTRL_PHYE | PHYCTRL_PXE);
  245. out_be32(pdata->regs + USBGENCTRL, reg);
  246. }
  247. return 0;
  248. }
  249. static void fsl_usb2_mpc5121_exit(struct platform_device *pdev)
  250. {
  251. struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
  252. pdata->regs = NULL;
  253. if (pdata->clk) {
  254. clk_disable(pdata->clk);
  255. clk_put(pdata->clk);
  256. }
  257. }
  258. static struct fsl_usb2_platform_data fsl_usb2_mpc5121_pd = {
  259. .big_endian_desc = 1,
  260. .big_endian_mmio = 1,
  261. .es = 1,
  262. .have_sysif_regs = 0,
  263. .le_setup_buf = 1,
  264. .init = fsl_usb2_mpc5121_init,
  265. .exit = fsl_usb2_mpc5121_exit,
  266. };
  267. #endif /* CONFIG_PPC_MPC512x */
  268. static struct fsl_usb2_platform_data fsl_usb2_mpc8xxx_pd = {
  269. .have_sysif_regs = 1,
  270. };
  271. static const struct of_device_id fsl_usb2_mph_dr_of_match[] = {
  272. { .compatible = "fsl-usb2-mph", .data = &fsl_usb2_mpc8xxx_pd, },
  273. { .compatible = "fsl-usb2-dr", .data = &fsl_usb2_mpc8xxx_pd, },
  274. #ifdef CONFIG_PPC_MPC512x
  275. { .compatible = "fsl,mpc5121-usb2-dr", .data = &fsl_usb2_mpc5121_pd, },
  276. #endif
  277. {},
  278. };
  279. static struct platform_driver fsl_usb2_mph_dr_driver = {
  280. .driver = {
  281. .name = "fsl-usb2-mph-dr",
  282. .owner = THIS_MODULE,
  283. .of_match_table = fsl_usb2_mph_dr_of_match,
  284. },
  285. .probe = fsl_usb2_mph_dr_of_probe,
  286. .remove = fsl_usb2_mph_dr_of_remove,
  287. };
  288. module_platform_driver(fsl_usb2_mph_dr_driver);
  289. MODULE_DESCRIPTION("FSL MPH DR OF devices driver");
  290. MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
  291. MODULE_LICENSE("GPL");