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
44static 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
50static 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
65static struct musb_hdrc_config musb_config = {
66	.multipoint	= 1,
67	.dyn_fifo	= 1,
68	.num_eps	= 16,
69	.ram_bits	= 12,
70};
71
72static 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
79static 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
90static 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
107static void __init n8x0_usb_init(void) {}
108
109#endif /*CONFIG_USB_MUSB_TUSB6010 */
110
111
112static struct omap2_mcspi_device_config p54spi_mcspi_config = {
113	.turbo_mode	= 0,
114};
115
116static 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 */
139static int slot1_cover_open;
140static int slot2_cover_open;
141static struct device *mmc_device;
142
143static struct gpiod_lookup_table nokia8xx_mmc_gpio_table = {
144	.dev_id = "mmci-omap.0",
145	.table = {
146		/* Slot switch, GPIO 96 */
147		GPIO_LOOKUP("gpio-80-111", 16,
148			    "switch", GPIO_ACTIVE_HIGH),
149		{ }
150	},
151};
152
153static struct gpiod_lookup_table nokia810_mmc_gpio_table = {
154	.dev_id = "mmci-omap.0",
155	.table = {
156		/* Slot index 1, VSD power, GPIO 23 */
157		GPIO_LOOKUP_IDX("gpio-16-31", 7,
158				"vsd", 1, GPIO_ACTIVE_HIGH),
159		/* Slot index 1, VIO power, GPIO 9 */
160		GPIO_LOOKUP_IDX("gpio-0-15", 9,
161				"vio", 1, GPIO_ACTIVE_HIGH),
162		{ }
163	},
164};
165
166static int n8x0_mmc_set_power_menelaus(struct device *dev, int slot,
167					int power_on, int vdd)
168{
169	int mV;
170
171#ifdef CONFIG_MMC_DEBUG
172	dev_dbg(dev, "Set slot %d power: %s (vdd %d)\n", slot + 1,
173		power_on ? "on" : "off", vdd);
174#endif
175	if (slot == 0) {
176		if (!power_on)
177			return menelaus_set_vmmc(0);
178		switch (1 << vdd) {
179		case MMC_VDD_33_34:
180		case MMC_VDD_32_33:
181		case MMC_VDD_31_32:
182			mV = 3100;
183			break;
184		case MMC_VDD_30_31:
185			mV = 3000;
186			break;
187		case MMC_VDD_28_29:
188			mV = 2800;
189			break;
190		case MMC_VDD_165_195:
191			mV = 1850;
192			break;
193		default:
194			BUG();
195		}
196		return menelaus_set_vmmc(mV);
197	} else {
198		if (!power_on)
199			return menelaus_set_vdcdc(3, 0);
200		switch (1 << vdd) {
201		case MMC_VDD_33_34:
202		case MMC_VDD_32_33:
203			mV = 3300;
204			break;
205		case MMC_VDD_30_31:
206		case MMC_VDD_29_30:
207			mV = 3000;
208			break;
209		case MMC_VDD_28_29:
210		case MMC_VDD_27_28:
211			mV = 2800;
212			break;
213		case MMC_VDD_24_25:
214		case MMC_VDD_23_24:
215			mV = 2400;
216			break;
217		case MMC_VDD_22_23:
218		case MMC_VDD_21_22:
219			mV = 2200;
220			break;
221		case MMC_VDD_20_21:
222			mV = 2000;
223			break;
224		case MMC_VDD_165_195:
225			mV = 1800;
226			break;
227		default:
228			BUG();
229		}
230		return menelaus_set_vdcdc(3, mV);
231	}
232	return 0;
233}
234
235static int n8x0_mmc_set_power(struct device *dev, int slot, int power_on,
236			      int vdd)
237{
238	if (board_is_n800() || slot == 0)
239		return n8x0_mmc_set_power_menelaus(dev, slot, power_on, vdd);
240
241	/* The n810 power will be handled by GPIO code in the driver */
242
243	return 0;
244}
245
246static int n8x0_mmc_set_bus_mode(struct device *dev, int slot, int bus_mode)
247{
248	int r;
249
250	dev_dbg(dev, "Set slot %d bus mode %s\n", slot + 1,
251		bus_mode == MMC_BUSMODE_OPENDRAIN ? "open-drain" : "push-pull");
252	BUG_ON(slot != 0 && slot != 1);
253	slot++;
254	switch (bus_mode) {
255	case MMC_BUSMODE_OPENDRAIN:
256		r = menelaus_set_mmc_opendrain(slot, 1);
257		break;
258	case MMC_BUSMODE_PUSHPULL:
259		r = menelaus_set_mmc_opendrain(slot, 0);
260		break;
261	default:
262		BUG();
263	}
264	if (r != 0 && printk_ratelimit())
265		dev_err(dev, "MMC: unable to set bus mode for slot %d\n",
266			slot);
267	return r;
268}
269
270static int n8x0_mmc_get_cover_state(struct device *dev, int slot)
271{
272	slot++;
273	BUG_ON(slot != 1 && slot != 2);
274	if (slot == 1)
275		return slot1_cover_open;
276	else
277		return slot2_cover_open;
278}
279
280static void n8x0_mmc_callback(void *data, u8 card_mask)
281{
282#ifdef CONFIG_MMC_OMAP
283	int bit, *openp, index;
284
285	if (board_is_n800()) {
286		bit = 1 << 1;
287		openp = &slot2_cover_open;
288		index = 1;
289	} else {
290		bit = 1;
291		openp = &slot1_cover_open;
292		index = 0;
293	}
294
295	if (card_mask & bit)
296		*openp = 1;
297	else
298		*openp = 0;
299
300	omap_mmc_notify_cover_event(mmc_device, index, *openp);
301#else
302	pr_warn("MMC: notify cover event not available\n");
303#endif
304}
305
306static int n8x0_mmc_late_init(struct device *dev)
307{
308	int r, bit, *openp;
309	int vs2sel;
310
311	mmc_device = dev;
312
313	r = menelaus_set_slot_sel(1);
314	if (r < 0)
315		return r;
316
317	if (board_is_n800())
318		vs2sel = 0;
319	else
320		vs2sel = 2;
321
322	r = menelaus_set_mmc_slot(2, 0, vs2sel, 1);
323	if (r < 0)
324		return r;
325
326	n8x0_mmc_set_power(dev, 0, MMC_POWER_ON, 16); /* MMC_VDD_28_29 */
327	n8x0_mmc_set_power(dev, 1, MMC_POWER_ON, 16);
328
329	r = menelaus_set_mmc_slot(1, 1, 0, 1);
330	if (r < 0)
331		return r;
332	r = menelaus_set_mmc_slot(2, 1, vs2sel, 1);
333	if (r < 0)
334		return r;
335
336	r = menelaus_get_slot_pin_states();
337	if (r < 0)
338		return r;
339
340	if (board_is_n800()) {
341		bit = 1 << 1;
342		openp = &slot2_cover_open;
343	} else {
344		bit = 1;
345		openp = &slot1_cover_open;
346		slot2_cover_open = 0;
347	}
348
349	/* All slot pin bits seem to be inversed until first switch change */
350	if (r == 0xf || r == (0xf & ~bit))
351		r = ~r;
352
353	if (r & bit)
354		*openp = 1;
355	else
356		*openp = 0;
357
358	r = menelaus_register_mmc_callback(n8x0_mmc_callback, NULL);
359
360	return r;
361}
362
363static void n8x0_mmc_shutdown(struct device *dev)
364{
365	int vs2sel;
366
367	if (board_is_n800())
368		vs2sel = 0;
369	else
370		vs2sel = 2;
371
372	menelaus_set_mmc_slot(1, 0, 0, 0);
373	menelaus_set_mmc_slot(2, 0, vs2sel, 0);
374}
375
376static void n8x0_mmc_cleanup(struct device *dev)
377{
378	menelaus_unregister_mmc_callback();
379}
380
381/*
382 * MMC controller1 has two slots that are multiplexed via I2C.
383 * MMC controller2 is not in use.
384 */
385static struct omap_mmc_platform_data mmc1_data = {
386	.nr_slots			= 0,
387	.init				= n8x0_mmc_late_init,
388	.cleanup			= n8x0_mmc_cleanup,
389	.shutdown			= n8x0_mmc_shutdown,
390	.max_freq			= 24000000,
391	.slots[0] = {
392		.wires			= 4,
393		.set_power		= n8x0_mmc_set_power,
394		.set_bus_mode		= n8x0_mmc_set_bus_mode,
395		.get_cover_state	= n8x0_mmc_get_cover_state,
396		.ocr_mask		= MMC_VDD_165_195 | MMC_VDD_30_31 |
397						MMC_VDD_32_33   | MMC_VDD_33_34,
398		.name			= "internal",
399	},
400	.slots[1] = {
401		.set_power		= n8x0_mmc_set_power,
402		.set_bus_mode		= n8x0_mmc_set_bus_mode,
403		.get_cover_state	= n8x0_mmc_get_cover_state,
404		.ocr_mask		= MMC_VDD_165_195 | MMC_VDD_20_21 |
405						MMC_VDD_21_22 | MMC_VDD_22_23 |
406						MMC_VDD_23_24 | MMC_VDD_24_25 |
407						MMC_VDD_27_28 | MMC_VDD_28_29 |
408						MMC_VDD_29_30 | MMC_VDD_30_31 |
409						MMC_VDD_32_33 | MMC_VDD_33_34,
410		.name			= "external",
411	},
412};
413
414static struct omap_mmc_platform_data *mmc_data[OMAP24XX_NR_MMC];
415
416static void __init n8x0_mmc_init(void)
417{
418	gpiod_add_lookup_table(&nokia8xx_mmc_gpio_table);
419
420	if (board_is_n810()) {
421		mmc1_data.slots[0].name = "external";
422
423		/*
424		 * Some Samsung Movinand chips do not like open-ended
425		 * multi-block reads and fall to braind-dead state
426		 * while doing so. Reducing the number of blocks in
427		 * the transfer or delays in clock disable do not help
428		 */
429		mmc1_data.slots[1].name = "internal";
430		mmc1_data.slots[1].ban_openended = 1;
431		gpiod_add_lookup_table(&nokia810_mmc_gpio_table);
432	}
433
434	mmc1_data.nr_slots = 2;
435	mmc_data[0] = &mmc1_data;
436}
437#else
438static struct omap_mmc_platform_data mmc1_data;
439static void __init n8x0_mmc_init(void)
440{
441}
442#endif	/* CONFIG_MMC_OMAP */
443
444#ifdef CONFIG_MENELAUS
445
446static int n8x0_auto_sleep_regulators(void)
447{
448	u32 val;
449	int ret;
450
451	val = EN_VPLL_SLEEP | EN_VMMC_SLEEP    \
452		| EN_VAUX_SLEEP | EN_VIO_SLEEP \
453		| EN_VMEM_SLEEP | EN_DC3_SLEEP \
454		| EN_VC_SLEEP | EN_DC2_SLEEP;
455
456	ret = menelaus_set_regulator_sleep(1, val);
457	if (ret < 0) {
458		pr_err("Could not set regulators to sleep on menelaus: %u\n",
459		       ret);
460		return ret;
461	}
462	return 0;
463}
464
465static int n8x0_auto_voltage_scale(void)
466{
467	int ret;
468
469	ret = menelaus_set_vcore_hw(1400, 1050);
470	if (ret < 0) {
471		pr_err("Could not set VCORE voltage on menelaus: %u\n", ret);
472		return ret;
473	}
474	return 0;
475}
476
477static int n8x0_menelaus_late_init(struct device *dev)
478{
479	int ret;
480
481	ret = n8x0_auto_voltage_scale();
482	if (ret < 0)
483		return ret;
484	ret = n8x0_auto_sleep_regulators();
485	if (ret < 0)
486		return ret;
487	return 0;
488}
489
490#else
491static int n8x0_menelaus_late_init(struct device *dev)
492{
493	return 0;
494}
495#endif
496
497struct menelaus_platform_data n8x0_menelaus_platform_data = {
498	.late_init = n8x0_menelaus_late_init,
499};
500
501static int __init n8x0_late_initcall(void)
502{
503	if (!board_caps)
504		return -ENODEV;
505
506	n8x0_mmc_init();
507	n8x0_usb_init();
508
509	return 0;
510}
511omap_late_initcall(n8x0_late_initcall);
512
513/*
514 * Legacy init pdata init for n8x0. Note that we want to follow the
515 * I2C bus numbering starting at 0 for device tree like other omaps.
516 */
517void * __init n8x0_legacy_init(void)
518{
519	board_check_revision();
520	spi_register_board_info(n800_spi_board_info,
521				ARRAY_SIZE(n800_spi_board_info));
522	return &mmc1_data;
523}
524