xref: /openbmc/linux/arch/arm/mach-omap2/board-n8x0.c (revision 086426dc64e0f650266a41766860b3f924826713)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * linux/arch/arm/mach-omap2/board-n8x0.c
4  *
5  * Copyright (C) 2005-2009 Nokia Corporation
6  * Author: Juha Yrjola <juha.yrjola@nokia.com>
7  *
8  * Modified from mach-omap2/board-generic.c
9  */
10 
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/gpio/machine.h>
14 #include <linux/gpio/consumer.h>
15 #include <linux/init.h>
16 #include <linux/io.h>
17 #include <linux/irq.h>
18 #include <linux/stddef.h>
19 #include <linux/i2c.h>
20 #include <linux/spi/spi.h>
21 #include <linux/usb/musb.h>
22 #include <linux/mmc/host.h>
23 #include <linux/platform_data/spi-omap2-mcspi.h>
24 #include <linux/platform_data/mmc-omap.h>
25 #include <linux/mfd/menelaus.h>
26 
27 #include <asm/mach/arch.h>
28 #include <asm/mach-types.h>
29 
30 #include "common.h"
31 #include "mmc.h"
32 #include "usb-tusb6010.h"
33 #include "soc.h"
34 #include "common-board-devices.h"
35 
36 #define TUSB6010_ASYNC_CS	1
37 #define TUSB6010_SYNC_CS	4
38 #define TUSB6010_DMACHAN	0x3f
39 
40 #define NOKIA_N810_WIMAX	(1 << 2)
41 #define NOKIA_N810		(1 << 1)
42 #define NOKIA_N800		(1 << 0)
43 
44 static u32 board_caps;
45 
46 #define board_is_n800()		(board_caps & NOKIA_N800)
47 #define board_is_n810()		(board_caps & NOKIA_N810)
48 #define board_is_n810_wimax()	(board_caps & NOKIA_N810_WIMAX)
49 
50 static void board_check_revision(void)
51 {
52 	if (of_machine_is_compatible("nokia,n800"))
53 		board_caps = NOKIA_N800;
54 	else if (of_machine_is_compatible("nokia,n810"))
55 		board_caps = NOKIA_N810;
56 	else if (of_machine_is_compatible("nokia,n810-wimax"))
57 		board_caps = NOKIA_N810_WIMAX;
58 
59 	if (!board_caps)
60 		pr_err("Unknown board\n");
61 }
62 
63 #if IS_ENABLED(CONFIG_USB_MUSB_TUSB6010)
64 
65 static struct musb_hdrc_config musb_config = {
66 	.multipoint	= 1,
67 	.dyn_fifo	= 1,
68 	.num_eps	= 16,
69 	.ram_bits	= 12,
70 };
71 
72 static struct musb_hdrc_platform_data tusb_data = {
73 	.mode		= MUSB_OTG,
74 	.min_power	= 25,	/* x2 = 50 mA drawn from VBUS as peripheral */
75 	.power		= 100,	/* Max 100 mA VBUS for host mode */
76 	.config		= &musb_config,
77 };
78 
79 static struct gpiod_lookup_table tusb_gpio_table = {
80 	.dev_id = "musb-tusb",
81 	.table = {
82 		GPIO_LOOKUP("gpio-0-15", 0, "enable",
83 			    GPIO_ACTIVE_HIGH),
84 		GPIO_LOOKUP("gpio-48-63", 10, "int",
85 			    GPIO_ACTIVE_HIGH),
86 		{ }
87 	},
88 };
89 
90 static void __init n8x0_usb_init(void)
91 {
92 	int ret = 0;
93 
94 	gpiod_add_lookup_table(&tusb_gpio_table);
95 	ret = tusb6010_setup_interface(&tusb_data, TUSB6010_REFCLK_19, 2,
96 				       TUSB6010_ASYNC_CS, TUSB6010_SYNC_CS,
97 				       TUSB6010_DMACHAN);
98 	if (ret != 0)
99 		return;
100 
101 	pr_info("TUSB 6010\n");
102 
103 	return;
104 }
105 #else
106 
107 static void __init n8x0_usb_init(void) {}
108 
109 #endif /*CONFIG_USB_MUSB_TUSB6010 */
110 
111 
112 static struct omap2_mcspi_device_config p54spi_mcspi_config = {
113 	.turbo_mode	= 0,
114 };
115 
116 static struct spi_board_info n800_spi_board_info[] __initdata = {
117 	{
118 		.modalias	= "p54spi",
119 		.bus_num	= 2,
120 		.chip_select	= 0,
121 		.max_speed_hz   = 48000000,
122 		.controller_data = &p54spi_mcspi_config,
123 	},
124 };
125 
126 #if defined(CONFIG_MENELAUS) && IS_ENABLED(CONFIG_MMC_OMAP)
127 
128 /*
129  * On both N800 and N810, only the first of the two MMC controllers is in use.
130  * The two MMC slots are multiplexed via Menelaus companion chip over I2C.
131  * On N800, both slots are powered via Menelaus. On N810, only one of the
132  * slots is powered via Menelaus. The N810 EMMC is powered via GPIO.
133  *
134  * VMMC				slot 1 on both N800 and N810
135  * VDCDC3_APE and VMCS2_APE	slot 2 on N800
136  * GPIO23 and GPIO9		slot 2 EMMC on N810
137  *
138  */
139 static int slot1_cover_open;
140 static int slot2_cover_open;
141 static struct device *mmc_device;
142 
143 static struct gpiod_lookup_table nokia800_mmc_gpio_table = {
144 	.dev_id = "mmci-omap.0",
145 	.table = {
146 		/* Slot switch, GPIO 96 */
147 		GPIO_LOOKUP("gpio-96-127", 0, "switch", GPIO_ACTIVE_HIGH),
148 		{ }
149 	},
150 };
151 
152 static struct gpiod_lookup_table nokia810_mmc_gpio_table = {
153 	.dev_id = "mmci-omap.0",
154 	.table = {
155 		/* Slot switch, GPIO 96 */
156 		GPIO_LOOKUP("gpio-96-127", 0, "switch", GPIO_ACTIVE_HIGH),
157 		/* Slot index 1, VSD power, GPIO 23 */
158 		GPIO_LOOKUP_IDX("gpio-0-31", 23, "vsd", 1, GPIO_ACTIVE_HIGH),
159 		/* Slot index 1, VIO power, GPIO 9 */
160 		GPIO_LOOKUP_IDX("gpio-0-31", 9, "vio", 1, GPIO_ACTIVE_HIGH),
161 		{ }
162 	},
163 };
164 
165 static int n8x0_mmc_set_power_menelaus(struct device *dev, int slot,
166 					int power_on, int vdd)
167 {
168 	int mV;
169 
170 #ifdef CONFIG_MMC_DEBUG
171 	dev_dbg(dev, "Set slot %d power: %s (vdd %d)\n", slot + 1,
172 		power_on ? "on" : "off", vdd);
173 #endif
174 	if (slot == 0) {
175 		if (!power_on)
176 			return menelaus_set_vmmc(0);
177 		switch (1 << vdd) {
178 		case MMC_VDD_33_34:
179 		case MMC_VDD_32_33:
180 		case MMC_VDD_31_32:
181 			mV = 3100;
182 			break;
183 		case MMC_VDD_30_31:
184 			mV = 3000;
185 			break;
186 		case MMC_VDD_28_29:
187 			mV = 2800;
188 			break;
189 		case MMC_VDD_165_195:
190 			mV = 1850;
191 			break;
192 		default:
193 			BUG();
194 		}
195 		return menelaus_set_vmmc(mV);
196 	} else {
197 		if (!power_on)
198 			return menelaus_set_vdcdc(3, 0);
199 		switch (1 << vdd) {
200 		case MMC_VDD_33_34:
201 		case MMC_VDD_32_33:
202 			mV = 3300;
203 			break;
204 		case MMC_VDD_30_31:
205 		case MMC_VDD_29_30:
206 			mV = 3000;
207 			break;
208 		case MMC_VDD_28_29:
209 		case MMC_VDD_27_28:
210 			mV = 2800;
211 			break;
212 		case MMC_VDD_24_25:
213 		case MMC_VDD_23_24:
214 			mV = 2400;
215 			break;
216 		case MMC_VDD_22_23:
217 		case MMC_VDD_21_22:
218 			mV = 2200;
219 			break;
220 		case MMC_VDD_20_21:
221 			mV = 2000;
222 			break;
223 		case MMC_VDD_165_195:
224 			mV = 1800;
225 			break;
226 		default:
227 			BUG();
228 		}
229 		return menelaus_set_vdcdc(3, mV);
230 	}
231 	return 0;
232 }
233 
234 static int n8x0_mmc_set_power(struct device *dev, int slot, int power_on,
235 			      int vdd)
236 {
237 	if (board_is_n800() || slot == 0)
238 		return n8x0_mmc_set_power_menelaus(dev, slot, power_on, vdd);
239 
240 	/* The n810 power will be handled by GPIO code in the driver */
241 
242 	return 0;
243 }
244 
245 static int n8x0_mmc_set_bus_mode(struct device *dev, int slot, int bus_mode)
246 {
247 	int r;
248 
249 	dev_dbg(dev, "Set slot %d bus mode %s\n", slot + 1,
250 		bus_mode == MMC_BUSMODE_OPENDRAIN ? "open-drain" : "push-pull");
251 	BUG_ON(slot != 0 && slot != 1);
252 	slot++;
253 	switch (bus_mode) {
254 	case MMC_BUSMODE_OPENDRAIN:
255 		r = menelaus_set_mmc_opendrain(slot, 1);
256 		break;
257 	case MMC_BUSMODE_PUSHPULL:
258 		r = menelaus_set_mmc_opendrain(slot, 0);
259 		break;
260 	default:
261 		BUG();
262 	}
263 	if (r != 0 && printk_ratelimit())
264 		dev_err(dev, "MMC: unable to set bus mode for slot %d\n",
265 			slot);
266 	return r;
267 }
268 
269 static int n8x0_mmc_get_cover_state(struct device *dev, int slot)
270 {
271 	slot++;
272 	BUG_ON(slot != 1 && slot != 2);
273 	if (slot == 1)
274 		return slot1_cover_open;
275 	else
276 		return slot2_cover_open;
277 }
278 
279 static void n8x0_mmc_callback(void *data, u8 card_mask)
280 {
281 #ifdef CONFIG_MMC_OMAP
282 	int bit, *openp, index;
283 
284 	if (board_is_n800()) {
285 		bit = 1 << 1;
286 		openp = &slot2_cover_open;
287 		index = 1;
288 	} else {
289 		bit = 1;
290 		openp = &slot1_cover_open;
291 		index = 0;
292 	}
293 
294 	if (card_mask & bit)
295 		*openp = 1;
296 	else
297 		*openp = 0;
298 
299 	omap_mmc_notify_cover_event(mmc_device, index, *openp);
300 #else
301 	pr_warn("MMC: notify cover event not available\n");
302 #endif
303 }
304 
305 static int n8x0_mmc_late_init(struct device *dev)
306 {
307 	int r, bit, *openp;
308 	int vs2sel;
309 
310 	mmc_device = dev;
311 
312 	r = menelaus_set_slot_sel(1);
313 	if (r < 0)
314 		return r;
315 
316 	if (board_is_n800())
317 		vs2sel = 0;
318 	else
319 		vs2sel = 2;
320 
321 	r = menelaus_set_mmc_slot(2, 0, vs2sel, 1);
322 	if (r < 0)
323 		return r;
324 
325 	n8x0_mmc_set_power(dev, 0, MMC_POWER_ON, 16); /* MMC_VDD_28_29 */
326 	n8x0_mmc_set_power(dev, 1, MMC_POWER_ON, 16);
327 
328 	r = menelaus_set_mmc_slot(1, 1, 0, 1);
329 	if (r < 0)
330 		return r;
331 	r = menelaus_set_mmc_slot(2, 1, vs2sel, 1);
332 	if (r < 0)
333 		return r;
334 
335 	r = menelaus_get_slot_pin_states();
336 	if (r < 0)
337 		return r;
338 
339 	if (board_is_n800()) {
340 		bit = 1 << 1;
341 		openp = &slot2_cover_open;
342 	} else {
343 		bit = 1;
344 		openp = &slot1_cover_open;
345 		slot2_cover_open = 0;
346 	}
347 
348 	/* All slot pin bits seem to be inversed until first switch change */
349 	if (r == 0xf || r == (0xf & ~bit))
350 		r = ~r;
351 
352 	if (r & bit)
353 		*openp = 1;
354 	else
355 		*openp = 0;
356 
357 	r = menelaus_register_mmc_callback(n8x0_mmc_callback, NULL);
358 
359 	return r;
360 }
361 
362 static void n8x0_mmc_shutdown(struct device *dev)
363 {
364 	int vs2sel;
365 
366 	if (board_is_n800())
367 		vs2sel = 0;
368 	else
369 		vs2sel = 2;
370 
371 	menelaus_set_mmc_slot(1, 0, 0, 0);
372 	menelaus_set_mmc_slot(2, 0, vs2sel, 0);
373 }
374 
375 static void n8x0_mmc_cleanup(struct device *dev)
376 {
377 	menelaus_unregister_mmc_callback();
378 }
379 
380 /*
381  * MMC controller1 has two slots that are multiplexed via I2C.
382  * MMC controller2 is not in use.
383  */
384 static struct omap_mmc_platform_data mmc1_data = {
385 	.nr_slots			= 0,
386 	.init				= n8x0_mmc_late_init,
387 	.cleanup			= n8x0_mmc_cleanup,
388 	.shutdown			= n8x0_mmc_shutdown,
389 	.max_freq			= 24000000,
390 	.slots[0] = {
391 		.wires			= 4,
392 		.set_power		= n8x0_mmc_set_power,
393 		.set_bus_mode		= n8x0_mmc_set_bus_mode,
394 		.get_cover_state	= n8x0_mmc_get_cover_state,
395 		.ocr_mask		= MMC_VDD_165_195 | MMC_VDD_30_31 |
396 						MMC_VDD_32_33   | MMC_VDD_33_34,
397 		.name			= "internal",
398 	},
399 	.slots[1] = {
400 		.set_power		= n8x0_mmc_set_power,
401 		.set_bus_mode		= n8x0_mmc_set_bus_mode,
402 		.get_cover_state	= n8x0_mmc_get_cover_state,
403 		.ocr_mask		= MMC_VDD_165_195 | MMC_VDD_20_21 |
404 						MMC_VDD_21_22 | MMC_VDD_22_23 |
405 						MMC_VDD_23_24 | MMC_VDD_24_25 |
406 						MMC_VDD_27_28 | MMC_VDD_28_29 |
407 						MMC_VDD_29_30 | MMC_VDD_30_31 |
408 						MMC_VDD_32_33 | MMC_VDD_33_34,
409 		.name			= "external",
410 	},
411 };
412 
413 static struct omap_mmc_platform_data *mmc_data[OMAP24XX_NR_MMC];
414 
415 static void __init n8x0_mmc_init(void)
416 {
417 	if (board_is_n810()) {
418 		mmc1_data.slots[0].name = "external";
419 
420 		/*
421 		 * Some Samsung Movinand chips do not like open-ended
422 		 * multi-block reads and fall to braind-dead state
423 		 * while doing so. Reducing the number of blocks in
424 		 * the transfer or delays in clock disable do not help
425 		 */
426 		mmc1_data.slots[1].name = "internal";
427 		mmc1_data.slots[1].ban_openended = 1;
428 		gpiod_add_lookup_table(&nokia810_mmc_gpio_table);
429 	} else {
430 		gpiod_add_lookup_table(&nokia800_mmc_gpio_table);
431 	}
432 
433 	mmc1_data.nr_slots = 2;
434 	mmc_data[0] = &mmc1_data;
435 }
436 #else
437 static struct omap_mmc_platform_data mmc1_data;
438 static void __init n8x0_mmc_init(void)
439 {
440 }
441 #endif	/* CONFIG_MMC_OMAP */
442 
443 #ifdef CONFIG_MENELAUS
444 
445 static int n8x0_auto_sleep_regulators(void)
446 {
447 	u32 val;
448 	int ret;
449 
450 	val = EN_VPLL_SLEEP | EN_VMMC_SLEEP    \
451 		| EN_VAUX_SLEEP | EN_VIO_SLEEP \
452 		| EN_VMEM_SLEEP | EN_DC3_SLEEP \
453 		| EN_VC_SLEEP | EN_DC2_SLEEP;
454 
455 	ret = menelaus_set_regulator_sleep(1, val);
456 	if (ret < 0) {
457 		pr_err("Could not set regulators to sleep on menelaus: %u\n",
458 		       ret);
459 		return ret;
460 	}
461 	return 0;
462 }
463 
464 static int n8x0_auto_voltage_scale(void)
465 {
466 	int ret;
467 
468 	ret = menelaus_set_vcore_hw(1400, 1050);
469 	if (ret < 0) {
470 		pr_err("Could not set VCORE voltage on menelaus: %u\n", ret);
471 		return ret;
472 	}
473 	return 0;
474 }
475 
476 static int n8x0_menelaus_late_init(struct device *dev)
477 {
478 	int ret;
479 
480 	ret = n8x0_auto_voltage_scale();
481 	if (ret < 0)
482 		return ret;
483 	ret = n8x0_auto_sleep_regulators();
484 	if (ret < 0)
485 		return ret;
486 	return 0;
487 }
488 
489 #else
490 static int n8x0_menelaus_late_init(struct device *dev)
491 {
492 	return 0;
493 }
494 #endif
495 
496 struct menelaus_platform_data n8x0_menelaus_platform_data = {
497 	.late_init = n8x0_menelaus_late_init,
498 };
499 
500 static int __init n8x0_late_initcall(void)
501 {
502 	if (!board_caps)
503 		return -ENODEV;
504 
505 	n8x0_mmc_init();
506 	n8x0_usb_init();
507 
508 	return 0;
509 }
510 omap_late_initcall(n8x0_late_initcall);
511 
512 /*
513  * Legacy init pdata init for n8x0. Note that we want to follow the
514  * I2C bus numbering starting at 0 for device tree like other omaps.
515  */
516 void * __init n8x0_legacy_init(void)
517 {
518 	board_check_revision();
519 	spi_register_board_info(n800_spi_board_info,
520 				ARRAY_SIZE(n800_spi_board_info));
521 	return &mmc1_data;
522 }
523