fsl-mph-dr-of.c 9.8 KB

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