blob: d0f5f67e07499ab7e2b82aa1da3fe0535069d2d2 [file] [log] [blame]
Kuninori Morimotof1407d52011-04-04 13:44:59 +09001/*
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 */
31void 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
39struct 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
54int 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
64struct 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
71int 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
91static irqreturn_t usbhs_interrupt(int irq, void *data);
92int usbhs_mod_probe(struct usbhs_priv *priv)
93{
94 struct device *dev = usbhs_priv_to_dev(priv);
95 int ret;
96
Kuninori Morimoto2f983822011-04-05 11:40:54 +090097 /*
98 * install host/gadget driver
99 */
100 ret = usbhs_mod_gadget_probe(priv);
101 if (ret < 0)
102 return ret;
103
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900104 /* irq settings */
105 ret = request_irq(priv->irq, usbhs_interrupt,
106 IRQF_DISABLED, dev_name(dev), priv);
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900107 if (ret) {
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900108 dev_err(dev, "irq request err\n");
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900109 goto mod_init_gadget_err;
110 }
111
112 return ret;
113
114mod_init_gadget_err:
115 usbhs_mod_gadget_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900116
117 return ret;
118}
119
120void usbhs_mod_remove(struct usbhs_priv *priv)
121{
Kuninori Morimoto2f983822011-04-05 11:40:54 +0900122 usbhs_mod_gadget_remove(priv);
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900123 free_irq(priv->irq, priv);
124}
125
126/*
127 * status functions
128 */
129int usbhs_status_get_usb_speed(struct usbhs_irq_state *irq_state)
130{
131 switch (irq_state->dvstctr & RHST) {
132 case RHST_LOW_SPEED:
133 return USB_SPEED_LOW;
134 case RHST_FULL_SPEED:
135 return USB_SPEED_FULL;
136 case RHST_HIGH_SPEED:
137 return USB_SPEED_HIGH;
138 }
139
140 return USB_SPEED_UNKNOWN;
141}
142
143int usbhs_status_get_device_state(struct usbhs_irq_state *irq_state)
144{
145 int state = irq_state->intsts0 & DVSQ_MASK;
146
147 switch (state) {
148 case POWER_STATE:
149 case DEFAULT_STATE:
150 case ADDRESS_STATE:
151 case CONFIGURATION_STATE:
152 return state;
153 }
154
155 return -EIO;
156}
157
158int usbhs_status_get_ctrl_stage(struct usbhs_irq_state *irq_state)
159{
160 /*
161 * return value
162 *
163 * IDLE_SETUP_STAGE
164 * READ_DATA_STAGE
165 * READ_STATUS_STAGE
166 * WRITE_DATA_STAGE
167 * WRITE_STATUS_STAGE
168 * NODATA_STATUS_STAGE
169 * SEQUENCE_ERROR
170 */
171 return (int)irq_state->intsts0 & CTSQ_MASK;
172}
173
174static void usbhs_status_get_each_irq(struct usbhs_priv *priv,
175 struct usbhs_irq_state *state)
176{
177 struct usbhs_mod *mod = usbhs_mod_get_current(priv);
178
179 state->intsts0 = usbhs_read(priv, INTSTS0);
180 state->intsts1 = usbhs_read(priv, INTSTS1);
181
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900182 state->dvstctr = usbhs_read(priv, DVSTCTR);
183
184 /* mask */
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900185 if (mod) {
186 state->brdysts = usbhs_read(priv, BRDYSTS);
187 state->nrdysts = usbhs_read(priv, NRDYSTS);
188 state->bempsts = usbhs_read(priv, BEMPSTS);
189
190 state->bempsts &= mod->irq_bempsts;
191 state->brdysts &= mod->irq_brdysts;
192 }
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900193}
194
195/*
196 * interrupt
197 */
198#define INTSTS0_MAGIC 0xF800 /* acknowledge magical interrupt sources */
199#define INTSTS1_MAGIC 0xA870 /* acknowledge magical interrupt sources */
200static irqreturn_t usbhs_interrupt(int irq, void *data)
201{
202 struct usbhs_priv *priv = data;
203 struct usbhs_irq_state irq_state;
204
205 usbhs_status_get_each_irq(priv, &irq_state);
206
207 /*
208 * clear interrupt
209 *
210 * The hardware is _very_ picky to clear interrupt bit.
211 * Especially INTSTS0_MAGIC, INTSTS1_MAGIC value.
212 *
213 * see
214 * "Operation"
215 * - "Control Transfer (DCP)"
216 * - Function :: VALID bit should 0
217 */
218 usbhs_write(priv, INTSTS0, ~irq_state.intsts0 & INTSTS0_MAGIC);
219 usbhs_write(priv, INTSTS1, ~irq_state.intsts1 & INTSTS1_MAGIC);
220
221 usbhs_write(priv, BRDYSTS, 0);
222 usbhs_write(priv, NRDYSTS, 0);
223 usbhs_write(priv, BEMPSTS, 0);
224
225 /*
226 * call irq callback functions
227 * see also
228 * usbhs_irq_setting_update
229 */
230 if (irq_state.intsts0 & DVST)
231 usbhs_mod_call(priv, irq_dev_state, priv, &irq_state);
232
233 if (irq_state.intsts0 & CTRT)
234 usbhs_mod_call(priv, irq_ctrl_stage, priv, &irq_state);
235
236 if (irq_state.intsts0 & BEMP)
237 usbhs_mod_call(priv, irq_empty, priv, &irq_state);
238
239 if (irq_state.intsts0 & BRDY)
240 usbhs_mod_call(priv, irq_ready, priv, &irq_state);
241
242 return IRQ_HANDLED;
243}
244
245void usbhs_irq_callback_update(struct usbhs_priv *priv, struct usbhs_mod *mod)
246{
247 u16 intenb0 = 0;
248
249 usbhs_write(priv, INTENB0, 0);
250
251 usbhs_write(priv, BEMPENB, 0);
252 usbhs_write(priv, BRDYENB, 0);
253
254 /*
255 * see also
256 * usbhs_interrupt
257 */
258
259 /*
260 * it don't enable DVSE (intenb0) here
261 * but "mod->irq_dev_state" will be called.
262 */
263
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900264 if (mod) {
265 if (mod->irq_ctrl_stage)
266 intenb0 |= CTRE;
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900267
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900268 if (mod->irq_empty && mod->irq_bempsts) {
269 usbhs_write(priv, BEMPENB, mod->irq_bempsts);
270 intenb0 |= BEMPE;
271 }
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900272
Kuninori Morimoto5ea68d52011-04-28 16:41:07 +0900273 if (mod->irq_ready && mod->irq_brdysts) {
274 usbhs_write(priv, BRDYENB, mod->irq_brdysts);
275 intenb0 |= BRDYE;
276 }
Kuninori Morimotof1407d52011-04-04 13:44:59 +0900277 }
278
279 usbhs_write(priv, INTENB0, intenb0);
280}