Commit | Line | Data |
---|---|---|
f1407d5c KM |
1 | /* |
2 | * Renesas USB driver | |
3 | * | |
4 | * Copyright (C) 2011 Renesas Solutions Corp. | |
5 | * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> | |
6 | * | |
7 | * This program is distributed in the hope that it will be useful, | |
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | * GNU General Public License for more details. | |
11 | * | |
12 | * You should have received a copy of the GNU General Public License | |
13 | * along with this program; if not, write to the Free Software | |
14 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
15 | * | |
16 | */ | |
17 | #include <linux/interrupt.h> | |
18 | ||
19 | #include "./common.h" | |
20 | #include "./mod.h" | |
21 | ||
22 | #define usbhs_priv_to_modinfo(priv) (&priv->mod_info) | |
23 | ||
24 | /* | |
25 | * host / gadget functions | |
26 | * | |
27 | * renesas_usbhs host/gadget can register itself by below functions. | |
28 | * these functions are called when probe | |
29 | * | |
30 | */ | |
31 | void usbhs_mod_register(struct usbhs_priv *priv, struct usbhs_mod *mod, int id) | |
32 | { | |
33 | struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); | |
34 | ||
35 | info->mod[id] = mod; | |
36 | mod->priv = priv; | |
37 | } | |
38 | ||
39 | struct usbhs_mod *usbhs_mod_get(struct usbhs_priv *priv, int id) | |
40 | { | |
41 | struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); | |
42 | struct usbhs_mod *ret = NULL; | |
43 | ||
44 | switch (id) { | |
45 | case USBHS_HOST: | |
46 | case USBHS_GADGET: | |
47 | ret = info->mod[id]; | |
48 | break; | |
49 | } | |
50 | ||
51 | return ret; | |
52 | } | |
53 | ||
54 | int usbhs_mod_is_host(struct usbhs_priv *priv, struct usbhs_mod *mod) | |
55 | { | |
56 | struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); | |
57 | ||
58 | if (!mod) | |
59 | return -EINVAL; | |
60 | ||
61 | return info->mod[USBHS_HOST] == mod; | |
62 | } | |
63 | ||
64 | struct usbhs_mod *usbhs_mod_get_current(struct usbhs_priv *priv) | |
65 | { | |
66 | struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); | |
67 | ||
68 | return info->curt; | |
69 | } | |
70 | ||
71 | int usbhs_mod_change(struct usbhs_priv *priv, int id) | |
72 | { | |
73 | struct usbhs_mod_info *info = usbhs_priv_to_modinfo(priv); | |
74 | struct usbhs_mod *mod = NULL; | |
75 | int ret = 0; | |
76 | ||
77 | /* id < 0 mean no current */ | |
78 | switch (id) { | |
79 | case USBHS_HOST: | |
80 | case USBHS_GADGET: | |
81 | mod = info->mod[id]; | |
82 | break; | |
83 | default: | |
84 | ret = -EINVAL; | |
85 | } | |
86 | info->curt = mod; | |
87 | ||
88 | return ret; | |
89 | } | |
90 | ||
91 | static irqreturn_t usbhs_interrupt(int irq, void *data); | |
92 | int usbhs_mod_probe(struct usbhs_priv *priv) | |
93 | { | |
94 | struct device *dev = usbhs_priv_to_dev(priv); | |
95 | int ret; | |
96 | ||
97 | /* irq settings */ | |
98 | ret = request_irq(priv->irq, usbhs_interrupt, | |
99 | IRQF_DISABLED, dev_name(dev), priv); | |
100 | if (ret) | |
101 | dev_err(dev, "irq request err\n"); | |
102 | ||
103 | return ret; | |
104 | } | |
105 | ||
106 | void usbhs_mod_remove(struct usbhs_priv *priv) | |
107 | { | |
108 | free_irq(priv->irq, priv); | |
109 | } | |
110 | ||
111 | /* | |
112 | * status functions | |
113 | */ | |
114 | int usbhs_status_get_usb_speed(struct usbhs_irq_state *irq_state) | |
115 | { | |
116 | switch (irq_state->dvstctr & RHST) { | |
117 | case RHST_LOW_SPEED: | |
118 | return USB_SPEED_LOW; | |
119 | case RHST_FULL_SPEED: | |
120 | return USB_SPEED_FULL; | |
121 | case RHST_HIGH_SPEED: | |
122 | return USB_SPEED_HIGH; | |
123 | } | |
124 | ||
125 | return USB_SPEED_UNKNOWN; | |
126 | } | |
127 | ||
128 | int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state) | |
129 | { | |
130 | int state = irq_state->intsts0 & DVSQ_MASK; | |
131 | ||
132 | switch (state) { | |
133 | case POWER_STATE: | |
134 | case DEFAULT_STATE: | |
135 | case ADDRESS_STATE: | |
136 | case CONFIGURATION_STATE: | |
137 | return state; | |
138 | } | |
139 | ||
140 | return -EIO; | |
141 | } | |
142 | ||
143 | int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state) | |
144 | { | |
145 | /* | |
146 | * return value | |
147 | * | |
148 | * IDLE_SETUP_STAGE | |
149 | * READ_DATA_STAGE | |
150 | * READ_STATUS_STAGE | |
151 | * WRITE_DATA_STAGE | |
152 | * WRITE_STATUS_STAGE | |
153 | * NODATA_STATUS_STAGE | |
154 | * SEQUENCE_ERROR | |
155 | */ | |
156 | return (int)irq_state->intsts0 & CTSQ_MASK; | |
157 | } | |
158 | ||
159 | static void usbhs_status_get_each_irq(struct usbhs_priv *priv, | |
160 | struct usbhs_irq_state *state) | |
161 | { | |
162 | struct usbhs_mod *mod = usbhs_mod_get_current(priv); | |
163 | ||
164 | state->intsts0 = usbhs_read(priv, INTSTS0); | |
165 | state->intsts1 = usbhs_read(priv, INTSTS1); | |
166 | ||
167 | state->brdysts = usbhs_read(priv, BRDYSTS); | |
168 | state->nrdysts = usbhs_read(priv, NRDYSTS); | |
169 | state->bempsts = usbhs_read(priv, BEMPSTS); | |
170 | ||
171 | state->dvstctr = usbhs_read(priv, DVSTCTR); | |
172 | ||
173 | /* mask */ | |
174 | state->bempsts &= mod->irq_bempsts; | |
175 | state->brdysts &= mod->irq_brdysts; | |
176 | } | |
177 | ||
178 | /* | |
179 | * interrupt | |
180 | */ | |
181 | #define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */ | |
182 | #define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */ | |
183 | static irqreturn_t usbhs_interrupt(int irq, void *data) | |
184 | { | |
185 | struct usbhs_priv *priv = data; | |
186 | struct usbhs_irq_state irq_state; | |
187 | ||
188 | usbhs_status_get_each_irq(priv, &irq_state); | |
189 | ||
190 | /* | |
191 | * clear interrupt | |
192 | * | |
193 | * The hardware is _very_ picky to clear interrupt bit. | |
194 | * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value. | |
195 | * | |
196 | * see | |
197 | * "Operation" | |
198 | * - "Control Transfer (DCP)" | |
199 | * - Function :: VALID bit should 0 | |
200 | */ | |
201 | usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC); | |
202 | usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC); | |
203 | ||
204 | usbhs_write(priv, BRDYSTS, 0); | |
205 | usbhs_write(priv, NRDYSTS, 0); | |
206 | usbhs_write(priv, BEMPSTS, 0); | |
207 | ||
208 | /* | |
209 | * call irq callback functions | |
210 | * see also | |
211 | * usbhs_irq_setting_update | |
212 | */ | |
213 | if (irq_state.intsts0 & DVST) | |
214 | usbhs_mod_call(priv, irq_dev_state, priv, &irq_state); | |
215 | ||
216 | if (irq_state.intsts0 & CTRT) | |
217 | usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state); | |
218 | ||
219 | if (irq_state.intsts0 & BEMP) | |
220 | usbhs_mod_call(priv, irq_empty, priv, &irq_state); | |
221 | ||
222 | if (irq_state.intsts0 & BRDY) | |
223 | usbhs_mod_call(priv, irq_ready, priv, &irq_state); | |
224 | ||
225 | return IRQ_HANDLED; | |
226 | } | |
227 | ||
228 | void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod) | |
229 | { | |
230 | u16 intenb0 = 0; | |
231 | ||
232 | usbhs_write(priv, INTENB0, 0); | |
233 | ||
234 | usbhs_write(priv, BEMPENB, 0); | |
235 | usbhs_write(priv, BRDYENB, 0); | |
236 | ||
237 | /* | |
238 | * see also | |
239 | * usbhs_interrupt | |
240 | */ | |
241 | ||
242 | /* | |
243 | * it don't enable DVSE (intenb0) here | |
244 | * but "mod->irq_dev_state" will be called. | |
245 | */ | |
246 | ||
247 | if (mod->irq_ctrl_stage) | |
248 | intenb0 |= CTRE; | |
249 | ||
250 | if (mod->irq_empty && mod->irq_bempsts) { | |
251 | usbhs_write(priv, BEMPENB, mod->irq_bempsts); | |
252 | intenb0 |= BEMPE; | |
253 | } | |
254 | ||
255 | if (mod->irq_ready && mod->irq_brdysts) { | |
256 | usbhs_write(priv, BRDYENB, mod->irq_brdysts); | |
257 | intenb0 |= BRDYE; | |
258 | } | |
259 | ||
260 | usbhs_write(priv, INTENB0, intenb0); | |
261 | } |