blob: 20a802ecaa158e73dd6c27b009afacc9052b0268 [file] [log] [blame]
Guennadi Liakhovetski54e4026b2009-04-15 14:25:33 +02001/*
2 * Copyright (C) 2009
3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
4 *
5 * Description:
6 * Helper routines for i.MX3x SoCs from Freescale, needed by the fsl_usb2_udc.c
7 * driver to function correctly on these systems.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 */
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/fsl_devices.h>
18#include <linux/platform_device.h>
19
Sascha Hauerba593e52010-02-04 14:40:59 +010020#include <mach/hardware.h>
21
Guennadi Liakhovetski54e4026b2009-04-15 14:25:33 +020022static struct clk *mxc_ahb_clk;
23static struct clk *mxc_usb_clk;
24
25int fsl_udc_clk_init(struct platform_device *pdev)
26{
27 struct fsl_usb2_platform_data *pdata;
28 unsigned long freq;
29 int ret;
30
31 pdata = pdev->dev.platform_data;
32
Sascha Hauerba593e52010-02-04 14:40:59 +010033 if (!cpu_is_mx35()) {
34 mxc_ahb_clk = clk_get(&pdev->dev, "usb_ahb");
35 if (IS_ERR(mxc_ahb_clk))
36 return PTR_ERR(mxc_ahb_clk);
Guennadi Liakhovetski54e4026b2009-04-15 14:25:33 +020037
Sascha Hauerba593e52010-02-04 14:40:59 +010038 ret = clk_enable(mxc_ahb_clk);
39 if (ret < 0) {
40 dev_err(&pdev->dev, "clk_enable(\"usb_ahb\") failed\n");
41 goto eenahb;
42 }
Guennadi Liakhovetski54e4026b2009-04-15 14:25:33 +020043 }
44
45 /* make sure USB_CLK is running at 60 MHz +/- 1000 Hz */
46 mxc_usb_clk = clk_get(&pdev->dev, "usb");
47 if (IS_ERR(mxc_usb_clk)) {
48 dev_err(&pdev->dev, "clk_get(\"usb\") failed\n");
49 ret = PTR_ERR(mxc_usb_clk);
50 goto egusb;
51 }
52
53 freq = clk_get_rate(mxc_usb_clk);
54 if (pdata->phy_mode != FSL_USB2_PHY_ULPI &&
55 (freq < 59999000 || freq > 60001000)) {
56 dev_err(&pdev->dev, "USB_CLK=%lu, should be 60MHz\n", freq);
Sascha Hauerba593e52010-02-04 14:40:59 +010057 ret = -EINVAL;
Guennadi Liakhovetski54e4026b2009-04-15 14:25:33 +020058 goto eclkrate;
59 }
60
61 ret = clk_enable(mxc_usb_clk);
62 if (ret < 0) {
63 dev_err(&pdev->dev, "clk_enable(\"usb_clk\") failed\n");
64 goto eenusb;
65 }
66
67 return 0;
68
69eenusb:
70eclkrate:
71 clk_put(mxc_usb_clk);
72 mxc_usb_clk = NULL;
73egusb:
Sascha Hauerba593e52010-02-04 14:40:59 +010074 if (!cpu_is_mx35())
75 clk_disable(mxc_ahb_clk);
Guennadi Liakhovetski54e4026b2009-04-15 14:25:33 +020076eenahb:
Sascha Hauerba593e52010-02-04 14:40:59 +010077 if (!cpu_is_mx35())
78 clk_put(mxc_ahb_clk);
Guennadi Liakhovetski54e4026b2009-04-15 14:25:33 +020079 return ret;
80}
81
82void fsl_udc_clk_finalize(struct platform_device *pdev)
83{
84 struct fsl_usb2_platform_data *pdata = pdev->dev.platform_data;
85
86 /* ULPI transceivers don't need usbpll */
87 if (pdata->phy_mode == FSL_USB2_PHY_ULPI) {
88 clk_disable(mxc_usb_clk);
89 clk_put(mxc_usb_clk);
90 mxc_usb_clk = NULL;
91 }
92}
93
94void fsl_udc_clk_release(void)
95{
96 if (mxc_usb_clk) {
97 clk_disable(mxc_usb_clk);
98 clk_put(mxc_usb_clk);
99 }
Sascha Hauerba593e52010-02-04 14:40:59 +0100100 if (!cpu_is_mx35()) {
101 clk_disable(mxc_ahb_clk);
102 clk_put(mxc_ahb_clk);
103 }
Guennadi Liakhovetski54e4026b2009-04-15 14:25:33 +0200104}