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