1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/arm/mach-omap2/usb-tusb6010.c
4  *
5  * Copyright (C) 2006 Nokia Corporation
6  */
7 
8 #include <linux/err.h>
9 #include <linux/string.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/delay.h>
13 #include <linux/platform_device.h>
14 #include <linux/gpio.h>
15 #include <linux/export.h>
16 #include <linux/platform_data/usb-omap.h>
17 
18 #include <linux/usb/musb.h>
19 
20 #include "gpmc.h"
21 
22 static u8		async_cs, sync_cs;
23 static unsigned		refclk_psec;
24 
25 static struct gpmc_settings tusb_async = {
26 	.wait_on_read	= true,
27 	.wait_on_write	= true,
28 	.device_width	= GPMC_DEVWIDTH_16BIT,
29 	.mux_add_data	= GPMC_MUX_AD,
30 };
31 
32 static struct gpmc_settings tusb_sync = {
33 	.burst_read	= true,
34 	.burst_write	= true,
35 	.sync_read	= true,
36 	.sync_write	= true,
37 	.wait_on_read	= true,
38 	.wait_on_write	= true,
39 	.burst_len	= GPMC_BURST_16,
40 	.device_width	= GPMC_DEVWIDTH_16BIT,
41 	.mux_add_data	= GPMC_MUX_AD,
42 };
43 
44 /* NOTE:  timings are from tusb 6010 datasheet Rev 1.8, 12-Sept 2006 */
45 
46 static int tusb_set_async_mode(unsigned sysclk_ps)
47 {
48 	struct gpmc_device_timings dev_t;
49 	struct gpmc_timings	t;
50 	unsigned		t_acsnh_advnh = sysclk_ps + 3000;
51 
52 	memset(&dev_t, 0, sizeof(dev_t));
53 
54 	dev_t.t_ceasu = 8 * 1000;
55 	dev_t.t_avdasu = t_acsnh_advnh - 7000;
56 	dev_t.t_ce_avd = 1000;
57 	dev_t.t_avdp_r = t_acsnh_advnh;
58 	dev_t.t_oeasu = t_acsnh_advnh + 1000;
59 	dev_t.t_oe = 300;
60 	dev_t.t_cez_r = 7000;
61 	dev_t.t_cez_w = dev_t.t_cez_r;
62 	dev_t.t_avdp_w = t_acsnh_advnh;
63 	dev_t.t_weasu = t_acsnh_advnh + 1000;
64 	dev_t.t_wpl = 300;
65 	dev_t.cyc_aavdh_we = 1;
66 
67 	gpmc_calc_timings(&t, &tusb_async, &dev_t);
68 
69 	return gpmc_cs_set_timings(async_cs, &t, &tusb_async);
70 }
71 
72 static int tusb_set_sync_mode(unsigned sysclk_ps)
73 {
74 	struct gpmc_device_timings dev_t;
75 	struct gpmc_timings	t;
76 	unsigned		t_scsnh_advnh = sysclk_ps + 3000;
77 
78 	memset(&dev_t, 0, sizeof(dev_t));
79 
80 	dev_t.clk = 11100;
81 	dev_t.t_bacc = 1000;
82 	dev_t.t_ces = 1000;
83 	dev_t.t_ceasu = 8 * 1000;
84 	dev_t.t_avdasu = t_scsnh_advnh - 7000;
85 	dev_t.t_ce_avd = 1000;
86 	dev_t.t_avdp_r = t_scsnh_advnh;
87 	dev_t.cyc_aavdh_oe = 3;
88 	dev_t.cyc_oe = 5;
89 	dev_t.t_ce_rdyz = 7000;
90 	dev_t.t_avdp_w = t_scsnh_advnh;
91 	dev_t.cyc_aavdh_we = 3;
92 	dev_t.cyc_wpl = 6;
93 
94 	gpmc_calc_timings(&t, &tusb_sync, &dev_t);
95 
96 	return gpmc_cs_set_timings(sync_cs, &t, &tusb_sync);
97 }
98 
99 /* tusb driver calls this when it changes the chip's clocking */
100 static int tusb6010_platform_retime(unsigned is_refclk)
101 {
102 	static const char	error[] =
103 		KERN_ERR "tusb6010 %s retime error %d\n";
104 
105 	unsigned	sysclk_ps;
106 	int		status;
107 
108 	if (!refclk_psec)
109 		return -ENODEV;
110 
111 	sysclk_ps = is_refclk ? refclk_psec : TUSB6010_OSCCLK_60;
112 
113 	status = tusb_set_async_mode(sysclk_ps);
114 	if (status < 0) {
115 		printk(error, "async", status);
116 		goto done;
117 	}
118 	status = tusb_set_sync_mode(sysclk_ps);
119 	if (status < 0)
120 		printk(error, "sync", status);
121 done:
122 	return status;
123 }
124 
125 static struct resource tusb_resources[] = {
126 	/* Order is significant!  The start/end fields
127 	 * are updated during setup..
128 	 */
129 	{ /* Asynchronous access */
130 		.flags	= IORESOURCE_MEM,
131 	},
132 	{ /* Synchronous access */
133 		.flags	= IORESOURCE_MEM,
134 	},
135 	{ /* IRQ */
136 		.name	= "mc",
137 		.flags	= IORESOURCE_IRQ,
138 	},
139 };
140 
141 static u64 tusb_dmamask = ~(u32)0;
142 
143 static struct platform_device tusb_device = {
144 	.name		= "musb-tusb",
145 	.id		= -1,
146 	.dev = {
147 		.dma_mask		= &tusb_dmamask,
148 		.coherent_dma_mask	= 0xffffffff,
149 	},
150 	.num_resources	= ARRAY_SIZE(tusb_resources),
151 	.resource	= tusb_resources,
152 };
153 
154 
155 /* this may be called only from board-*.c setup code */
156 int __init tusb6010_setup_interface(struct musb_hdrc_platform_data *data,
157 		unsigned ps_refclk, unsigned waitpin,
158 		unsigned async, unsigned sync,
159 		unsigned irq, unsigned dmachan)
160 {
161 	int		status;
162 	static char	error[] __initdata =
163 		KERN_ERR "tusb6010 init error %d, %d\n";
164 
165 	/* ASYNC region, primarily for PIO */
166 	status = gpmc_cs_request(async, SZ_16M, (unsigned long *)
167 				&tusb_resources[0].start);
168 	if (status < 0) {
169 		printk(error, 1, status);
170 		return status;
171 	}
172 	tusb_resources[0].end = tusb_resources[0].start + 0x9ff;
173 	tusb_async.wait_pin = waitpin;
174 	async_cs = async;
175 
176 	status = gpmc_cs_program_settings(async_cs, &tusb_async);
177 	if (status < 0)
178 		return status;
179 
180 	/* SYNC region, primarily for DMA */
181 	status = gpmc_cs_request(sync, SZ_16M, (unsigned long *)
182 				&tusb_resources[1].start);
183 	if (status < 0) {
184 		printk(error, 2, status);
185 		return status;
186 	}
187 	tusb_resources[1].end = tusb_resources[1].start + 0x9ff;
188 	tusb_sync.wait_pin = waitpin;
189 	sync_cs = sync;
190 
191 	status = gpmc_cs_program_settings(sync_cs, &tusb_sync);
192 	if (status < 0)
193 		return status;
194 
195 	/* IRQ */
196 	status = gpio_request_one(irq, GPIOF_IN, "TUSB6010 irq");
197 	if (status < 0) {
198 		printk(error, 3, status);
199 		return status;
200 	}
201 	tusb_resources[2].start = gpio_to_irq(irq);
202 
203 	/* set up memory timings ... can speed them up later */
204 	if (!ps_refclk) {
205 		printk(error, 4, status);
206 		return -ENODEV;
207 	}
208 	refclk_psec = ps_refclk;
209 	status = tusb6010_platform_retime(1);
210 	if (status < 0) {
211 		printk(error, 5, status);
212 		return status;
213 	}
214 
215 	/* finish device setup ... */
216 	if (!data) {
217 		printk(error, 6, status);
218 		return -ENODEV;
219 	}
220 	tusb_device.dev.platform_data = data;
221 
222 	/* so far so good ... register the device */
223 	status = platform_device_register(&tusb_device);
224 	if (status < 0) {
225 		printk(error, 7, status);
226 		return status;
227 	}
228 	return 0;
229 }
230