blob: 5be96c6e14166a08141c83e03485af423948e6eb [file] [log] [blame]
David Brownellc1ed6402006-12-07 14:03:49 -08001/*
2 * linux/arch/arm/mach-omap2/usb-tusb6010.c
3 *
4 * Copyright (C) 2006 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Paul Gortmakerd44b28c2011-07-31 10:52:44 -040011#include <linux/string.h>
David Brownellc1ed6402006-12-07 14:03:49 -080012#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/delay.h>
15#include <linux/platform_device.h>
Jarkko Nikulaf2d18fe2008-12-10 17:35:30 -080016#include <linux/gpio.h>
Paul Gortmakerdc280942011-07-31 16:17:29 -040017#include <linux/export.h>
Felipe Balbie8c4a7a2012-10-24 14:26:19 -070018#include <linux/platform_data/usb-omap.h>
David Brownellc1ed6402006-12-07 14:03:49 -080019
20#include <linux/usb/musb.h>
21
Afzal Mohammed3ef5d002012-10-05 10:37:27 +053022#include "gpmc.h"
David Brownellc1ed6402006-12-07 14:03:49 -080023
Tony Lindgrenf99bf162010-07-05 16:31:40 +030024#include "mux.h"
David Brownellc1ed6402006-12-07 14:03:49 -080025
26static u8 async_cs, sync_cs;
27static unsigned refclk_psec;
28
29
30/* t2_ps, when quantized to fclk units, must happen no earlier than
31 * the clock after after t1_NS.
32 *
33 * Return a possibly updated value of t2_ps, converted to nsec.
34 */
35static unsigned
36next_clk(unsigned t1_NS, unsigned t2_ps, unsigned fclk_ps)
37{
38 unsigned t1_ps = t1_NS * 1000;
39 unsigned t1_f, t2_f;
40
41 if ((t1_ps + fclk_ps) < t2_ps)
42 return t2_ps / 1000;
43
44 t1_f = (t1_ps + fclk_ps - 1) / fclk_ps;
45 t2_f = (t2_ps + fclk_ps - 1) / fclk_ps;
46
47 if (t1_f >= t2_f)
48 t2_f = t1_f + 1;
49
50 return (t2_f * fclk_ps) / 1000;
51}
52
53/* NOTE: timings are from tusb 6010 datasheet Rev 1.8, 12-Sept 2006 */
54
55static int tusb_set_async_mode(unsigned sysclk_ps, unsigned fclk_ps)
56{
57 struct gpmc_timings t;
58 unsigned t_acsnh_advnh = sysclk_ps + 3000;
59 unsigned tmp;
60
61 memset(&t, 0, sizeof(t));
62
63 /* CS_ON = t_acsnh_acsnl */
64 t.cs_on = 8;
65 /* ADV_ON = t_acsnh_advnh - t_advn */
66 t.adv_on = next_clk(t.cs_on, t_acsnh_advnh - 7000, fclk_ps);
67
68 /*
69 * READ ... from omap2420 TRM fig 12-13
70 */
71
72 /* ADV_RD_OFF = t_acsnh_advnh */
73 t.adv_rd_off = next_clk(t.adv_on, t_acsnh_advnh, fclk_ps);
74
75 /* OE_ON = t_acsnh_advnh + t_advn_oen (then wait for nRDY) */
76 t.oe_on = next_clk(t.adv_on, t_acsnh_advnh + 1000, fclk_ps);
77
78 /* ACCESS = counters continue only after nRDY */
79 tmp = t.oe_on * 1000 + 300;
80 t.access = next_clk(t.oe_on, tmp, fclk_ps);
81
82 /* OE_OFF = after data gets sampled */
83 tmp = t.access * 1000;
84 t.oe_off = next_clk(t.access, tmp, fclk_ps);
85
86 t.cs_rd_off = t.oe_off;
87
88 tmp = t.cs_rd_off * 1000 + 7000 /* t_acsn_rdy_z */;
89 t.rd_cycle = next_clk(t.cs_rd_off, tmp, fclk_ps);
90
91 /*
92 * WRITE ... from omap2420 TRM fig 12-15
93 */
94
95 /* ADV_WR_OFF = t_acsnh_advnh */
96 t.adv_wr_off = t.adv_rd_off;
97
98 /* WE_ON = t_acsnh_advnh + t_advn_wen (then wait for nRDY) */
99 t.we_on = next_clk(t.adv_wr_off, t_acsnh_advnh + 1000, fclk_ps);
100
101 /* WE_OFF = after data gets sampled */
102 tmp = t.we_on * 1000 + 300;
103 t.we_off = next_clk(t.we_on, tmp, fclk_ps);
104
105 t.cs_wr_off = t.we_off;
106
107 tmp = t.cs_wr_off * 1000 + 7000 /* t_acsn_rdy_z */;
108 t.wr_cycle = next_clk(t.cs_wr_off, tmp, fclk_ps);
109
110 return gpmc_cs_set_timings(async_cs, &t);
111}
112
113static int tusb_set_sync_mode(unsigned sysclk_ps, unsigned fclk_ps)
114{
115 struct gpmc_timings t;
116 unsigned t_scsnh_advnh = sysclk_ps + 3000;
117 unsigned tmp;
118
119 memset(&t, 0, sizeof(t));
120 t.cs_on = 8;
121
122 /* ADV_ON = t_acsnh_advnh - t_advn */
123 t.adv_on = next_clk(t.cs_on, t_scsnh_advnh - 7000, fclk_ps);
124
125 /* GPMC_CLK rate = fclk rate / div */
Adrian Huntera3551f52010-12-09 10:48:27 +0200126 t.sync_clk = 11100 /* 11.1 nsec */;
127 tmp = (t.sync_clk + fclk_ps - 1) / fclk_ps;
David Brownellc1ed6402006-12-07 14:03:49 -0800128 if (tmp > 4)
129 return -ERANGE;
Paul Walmsleyeeb37112012-04-13 06:34:32 -0600130 if (tmp == 0)
David Brownellc1ed6402006-12-07 14:03:49 -0800131 tmp = 1;
132 t.page_burst_access = (fclk_ps * tmp) / 1000;
133
134 /*
135 * READ ... based on omap2420 TRM fig 12-19, 12-20
136 */
137
138 /* ADV_RD_OFF = t_scsnh_advnh */
139 t.adv_rd_off = next_clk(t.adv_on, t_scsnh_advnh, fclk_ps);
140
141 /* OE_ON = t_scsnh_advnh + t_advn_oen * fclk_ps (then wait for nRDY) */
142 tmp = (t.adv_rd_off * 1000) + (3 * fclk_ps);
143 t.oe_on = next_clk(t.adv_on, tmp, fclk_ps);
144
145 /* ACCESS = number of clock cycles after t_adv_eon */
146 tmp = (t.oe_on * 1000) + (5 * fclk_ps);
147 t.access = next_clk(t.oe_on, tmp, fclk_ps);
148
149 /* OE_OFF = after data gets sampled */
150 tmp = (t.access * 1000) + (1 * fclk_ps);
151 t.oe_off = next_clk(t.access, tmp, fclk_ps);
152
153 t.cs_rd_off = t.oe_off;
154
155 tmp = t.cs_rd_off * 1000 + 7000 /* t_scsn_rdy_z */;
156 t.rd_cycle = next_clk(t.cs_rd_off, tmp, fclk_ps);
157
158 /*
159 * WRITE ... based on omap2420 TRM fig 12-21
160 */
161
162 /* ADV_WR_OFF = t_scsnh_advnh */
163 t.adv_wr_off = t.adv_rd_off;
164
165 /* WE_ON = t_scsnh_advnh + t_advn_wen * fclk_ps (then wait for nRDY) */
166 tmp = (t.adv_wr_off * 1000) + (3 * fclk_ps);
167 t.we_on = next_clk(t.adv_wr_off, tmp, fclk_ps);
168
169 /* WE_OFF = number of clock cycles after t_adv_wen */
170 tmp = (t.we_on * 1000) + (6 * fclk_ps);
171 t.we_off = next_clk(t.we_on, tmp, fclk_ps);
172
173 t.cs_wr_off = t.we_off;
174
175 tmp = t.cs_wr_off * 1000 + 7000 /* t_scsn_rdy_z */;
176 t.wr_cycle = next_clk(t.cs_wr_off, tmp, fclk_ps);
177
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530178 t.clk_activation = gpmc_ticks_to_ns(1);
179
David Brownellc1ed6402006-12-07 14:03:49 -0800180 return gpmc_cs_set_timings(sync_cs, &t);
181}
182
183extern unsigned long gpmc_get_fclk_period(void);
184
185/* tusb driver calls this when it changes the chip's clocking */
186int tusb6010_platform_retime(unsigned is_refclk)
187{
188 static const char error[] =
189 KERN_ERR "tusb6010 %s retime error %d\n";
190
191 unsigned fclk_ps = gpmc_get_fclk_period();
192 unsigned sysclk_ps;
193 int status;
194
Jarkko Nikulaeaf93932009-05-12 11:20:02 -0700195 if (!refclk_psec || fclk_ps == 0)
David Brownellc1ed6402006-12-07 14:03:49 -0800196 return -ENODEV;
197
198 sysclk_ps = is_refclk ? refclk_psec : TUSB6010_OSCCLK_60;
199
200 status = tusb_set_async_mode(sysclk_ps, fclk_ps);
201 if (status < 0) {
202 printk(error, "async", status);
203 goto done;
204 }
205 status = tusb_set_sync_mode(sysclk_ps, fclk_ps);
206 if (status < 0)
207 printk(error, "sync", status);
208done:
209 return status;
210}
211EXPORT_SYMBOL_GPL(tusb6010_platform_retime);
212
213static struct resource tusb_resources[] = {
214 /* Order is significant! The start/end fields
215 * are updated during setup..
216 */
217 { /* Asynchronous access */
218 .flags = IORESOURCE_MEM,
219 },
220 { /* Synchronous access */
221 .flags = IORESOURCE_MEM,
222 },
223 { /* IRQ */
Felipe Balbi6ec1e072010-12-23 12:17:49 -0800224 .name = "mc",
David Brownellc1ed6402006-12-07 14:03:49 -0800225 .flags = IORESOURCE_IRQ,
226 },
227};
228
229static u64 tusb_dmamask = ~(u32)0;
230
231static struct platform_device tusb_device = {
Felipe Balbi18688fb2010-12-02 09:13:54 +0200232 .name = "musb-tusb",
David Brownellc1ed6402006-12-07 14:03:49 -0800233 .id = -1,
234 .dev = {
235 .dma_mask = &tusb_dmamask,
236 .coherent_dma_mask = 0xffffffff,
237 },
238 .num_resources = ARRAY_SIZE(tusb_resources),
239 .resource = tusb_resources,
240};
241
242
243/* this may be called only from board-*.c setup code */
244int __init
245tusb6010_setup_interface(struct musb_hdrc_platform_data *data,
246 unsigned ps_refclk, unsigned waitpin,
247 unsigned async, unsigned sync,
248 unsigned irq, unsigned dmachan)
249{
250 int status;
251 static char error[] __initdata =
252 KERN_ERR "tusb6010 init error %d, %d\n";
253
254 /* ASYNC region, primarily for PIO */
255 status = gpmc_cs_request(async, SZ_16M, (unsigned long *)
256 &tusb_resources[0].start);
257 if (status < 0) {
258 printk(error, 1, status);
259 return status;
260 }
261 tusb_resources[0].end = tusb_resources[0].start + 0x9ff;
262 async_cs = async;
263 gpmc_cs_write_reg(async, GPMC_CS_CONFIG1,
264 GPMC_CONFIG1_PAGE_LEN(2)
265 | GPMC_CONFIG1_WAIT_READ_MON
266 | GPMC_CONFIG1_WAIT_WRITE_MON
267 | GPMC_CONFIG1_WAIT_PIN_SEL(waitpin)
268 | GPMC_CONFIG1_READTYPE_ASYNC
269 | GPMC_CONFIG1_WRITETYPE_ASYNC
270 | GPMC_CONFIG1_DEVICESIZE_16
271 | GPMC_CONFIG1_DEVICETYPE_NOR
272 | GPMC_CONFIG1_MUXADDDATA);
273
274
275 /* SYNC region, primarily for DMA */
276 status = gpmc_cs_request(sync, SZ_16M, (unsigned long *)
277 &tusb_resources[1].start);
278 if (status < 0) {
279 printk(error, 2, status);
280 return status;
281 }
282 tusb_resources[1].end = tusb_resources[1].start + 0x9ff;
283 sync_cs = sync;
284 gpmc_cs_write_reg(sync, GPMC_CS_CONFIG1,
285 GPMC_CONFIG1_READMULTIPLE_SUPP
286 | GPMC_CONFIG1_READTYPE_SYNC
287 | GPMC_CONFIG1_WRITEMULTIPLE_SUPP
288 | GPMC_CONFIG1_WRITETYPE_SYNC
David Brownellc1ed6402006-12-07 14:03:49 -0800289 | GPMC_CONFIG1_PAGE_LEN(2)
290 | GPMC_CONFIG1_WAIT_READ_MON
291 | GPMC_CONFIG1_WAIT_WRITE_MON
292 | GPMC_CONFIG1_WAIT_PIN_SEL(waitpin)
293 | GPMC_CONFIG1_DEVICESIZE_16
294 | GPMC_CONFIG1_DEVICETYPE_NOR
295 | GPMC_CONFIG1_MUXADDDATA
296 /* fclk divider gets set later */
297 );
298
299 /* IRQ */
Igor Grinbergbc593f52011-05-03 18:22:09 +0300300 status = gpio_request_one(irq, GPIOF_IN, "TUSB6010 irq");
David Brownellc1ed6402006-12-07 14:03:49 -0800301 if (status < 0) {
302 printk(error, 3, status);
303 return status;
304 }
Tony Lindgren3d09b332012-06-20 07:18:15 -0700305 tusb_resources[2].start = gpio_to_irq(irq);
David Brownellc1ed6402006-12-07 14:03:49 -0800306
307 /* set up memory timings ... can speed them up later */
308 if (!ps_refclk) {
309 printk(error, 4, status);
310 return -ENODEV;
311 }
312 refclk_psec = ps_refclk;
313 status = tusb6010_platform_retime(1);
314 if (status < 0) {
315 printk(error, 5, status);
316 return status;
317 }
318
319 /* finish device setup ... */
320 if (!data) {
321 printk(error, 6, status);
322 return -ENODEV;
323 }
David Brownellc1ed6402006-12-07 14:03:49 -0800324 tusb_device.dev.platform_data = data;
325
326 /* REVISIT let the driver know what DMA channels work */
327 if (!dmachan)
328 tusb_device.dev.dma_mask = NULL;
329 else {
330 /* assume OMAP 2420 ES2.0 and later */
331 if (dmachan & (1 << 0))
Tony Lindgrenf99bf162010-07-05 16:31:40 +0300332 omap_mux_init_signal("sys_ndmareq0", 0);
David Brownellc1ed6402006-12-07 14:03:49 -0800333 if (dmachan & (1 << 1))
Tony Lindgrenf99bf162010-07-05 16:31:40 +0300334 omap_mux_init_signal("sys_ndmareq1", 0);
David Brownellc1ed6402006-12-07 14:03:49 -0800335 if (dmachan & (1 << 2))
Tony Lindgrenf99bf162010-07-05 16:31:40 +0300336 omap_mux_init_signal("sys_ndmareq2", 0);
David Brownellc1ed6402006-12-07 14:03:49 -0800337 if (dmachan & (1 << 3))
Tony Lindgrenf99bf162010-07-05 16:31:40 +0300338 omap_mux_init_signal("sys_ndmareq3", 0);
David Brownellc1ed6402006-12-07 14:03:49 -0800339 if (dmachan & (1 << 4))
Tony Lindgrenf99bf162010-07-05 16:31:40 +0300340 omap_mux_init_signal("sys_ndmareq4", 0);
David Brownellc1ed6402006-12-07 14:03:49 -0800341 if (dmachan & (1 << 5))
Tony Lindgrenf99bf162010-07-05 16:31:40 +0300342 omap_mux_init_signal("sys_ndmareq5", 0);
David Brownellc1ed6402006-12-07 14:03:49 -0800343 }
344
345 /* so far so good ... register the device */
346 status = platform_device_register(&tusb_device);
347 if (status < 0) {
348 printk(error, 7, status);
349 return status;
350 }
351 return 0;
352}