summaryrefslogtreecommitdiffstats
path: root/arch/arm/boards/skov-imx6/board.c
blob: f42a0cad00099ea9b1223bc4d9adf47a359003e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// SPDX-License-Identifier: GPL-2.0-only

#define pr_fmt(fmt) "skov-imx6: " fmt

#include <common.h>
#include <init.h>
#include <mach/bbu.h>
#include <environment.h>
#include <bootsource.h>
#include <globalvar.h>
#include <net.h>
#include <of_gpio.h>
#include <gpio.h>
#include <linux/micrel_phy.h>

#include "version.h"

static int eth_of_fixup_node(struct device_node *root, const char *node_path,
			     const u8 *ethaddr)
{
	struct device_node *node;
	int ret;

	if (!is_valid_ether_addr(ethaddr)) {
		unsigned char ethaddr_str[sizeof("xx:xx:xx:xx:xx:xx")];

		ethaddr_to_string(ethaddr, ethaddr_str);
		pr_err("The mac-address %s is invalid.\n", ethaddr_str);
		return -EINVAL;
	}

	node = of_find_node_by_path_from(root, node_path);
	if (!node) {
		pr_err("Did not find node %s to fix up with stored mac-address.\n",
		       node_path);
		return -ENOENT;
	}

	ret = of_set_property(node, "mac-address", ethaddr, ETH_ALEN, 1);
	if (ret)
		pr_err("Setting mac-address property of %s failed with: %s.\n",
		       node->full_name, strerror(-ret));

	return ret;
}

static int eth_of_fixup_node_from_eth_device(struct device_node *root,
					     const char *node_path,
					     const char *ethname)
{
	struct eth_device *edev;

	edev = eth_get_byname(ethname);
	if (!edev) {
		pr_err("Did not find eth device \"%s\" to copy mac-address from.\n", ethname);
		return -ENOENT;
	}

	return eth_of_fixup_node(root, node_path, edev->ethaddr);
}

static int get_mac_address_from_env_variable(const char *env, u8 ethaddr[ETH_ALEN])
{
	const char *ethaddr_str;
	int ret;

	ethaddr_str = getenv(env);
	if (!ethaddr_str) {
		pr_err("State variable %s storing the mac-address not found.\n", env);
		return -ENOENT;
	}

	ret = string_to_ethaddr(ethaddr_str, ethaddr);
	if (ret < 0) {
		pr_err("Could not convert \"%s\" in state variable %s into mac-address.\n",
		       ethaddr_str, env);
		return -EINVAL;
	}

	return 0;
}

static int get_default_mac_address_from_state_node(const char *state_node_path,
						   u8 ethaddr[ETH_ALEN])
{
	struct device_node *node;
	int ret;

	node = of_find_node_by_path(state_node_path);
	if (!node) {
		pr_err("Node %s defining the state variable not found.\n", state_node_path);
		return -ENOENT;
	}

	ret = of_property_read_u8_array(node, "default", ethaddr, ETH_ALEN);
	if (ret) {
		pr_err("Node %s has no property \"default\" of proper type.\n", state_node_path);
		return -ENOENT;
	}

	return 0;
}

static int eth2_of_fixup_node_individually(struct device_node *root,
					   const char *node_path,
					   const char *ethname,
					   const char *env,
					   const char *state_node_path)
{
	u8 env_ethaddr[ETH_ALEN], default_ethaddr[ETH_ALEN];
	int ret;

	ret = get_mac_address_from_env_variable(env, env_ethaddr);
	if (ret)
		goto copy_mac_from_eth0;

	ret = get_default_mac_address_from_state_node(state_node_path, default_ethaddr);
	if (ret)
		goto copy_mac_from_eth0;

	/*
	 * As the default is bogus copy the MAC address from eth0 if
	 * the state variable has not been set to a different variant
	 */
	if (memcmp(env_ethaddr, default_ethaddr, ETH_ALEN) == 0)
		goto copy_mac_from_eth0;

	return eth_of_fixup_node(root, node_path, env_ethaddr);

copy_mac_from_eth0:
	return eth_of_fixup_node_from_eth_device(root, node_path, ethname);
}

#define SKOV_GPIO_MDIO_BUS	0
#define SKOV_LAN1_PHY_ADDR	1

#define MAX_V_GPIO 8

struct board_description {
	const char *variant;
	const char *revision;
	const char *soc;
	const char *dts_compatible;
	const char *display;
	unsigned flags;
};

#define SKOV_NEED_ENABLE_RMII	BIT(0)
#define SKOV_DISPLAY_PARALLEL	BIT(1)
#define SKOV_ENABLE_MMC_POWER	BIT(2)
#define SKOV_DISPLAY_LVDS	BIT(3)

static const struct board_description imx6_variants[] = {
	[0] = {
		.variant = "high performance",
		.revision = "A",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6-imxq-revA",
		.flags = SKOV_NEED_ENABLE_RMII | SKOV_DISPLAY_PARALLEL,
	},
	[1] = {
		.variant = "low cost",
		.revision = "A",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6-imxdl-revA",
		.flags = SKOV_NEED_ENABLE_RMII | SKOV_DISPLAY_PARALLEL,
	},
	[2] = {
		.variant = "high performance",
		.revision = "A",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6-imxq-revA",
		.flags = SKOV_NEED_ENABLE_RMII | SKOV_DISPLAY_PARALLEL,
	},
	[4] = {
		.variant = "low cost",
		.revision = "A",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6-imxdl-revA",
		.flags = SKOV_NEED_ENABLE_RMII | SKOV_DISPLAY_PARALLEL,
	},
	[8] = {
		.variant = "high performance",
		.revision = "A",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6-imxq-revA",
		.flags = SKOV_NEED_ENABLE_RMII | SKOV_DISPLAY_PARALLEL,
	},
	[9] = {
		.variant = "minimum cost",
		.revision = "B",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6-imxdl-revB",
		.flags = SKOV_DISPLAY_PARALLEL,
	},
	[10] = {
		.variant = "low cost",
		.revision = "B",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6-imxdl-revB",
		.flags = SKOV_DISPLAY_PARALLEL,
	},
	[11] = {
		.variant = "high performance",
		.revision = "B",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6-imxq-revB",
		.flags = SKOV_DISPLAY_PARALLEL,
	},
	[12] = {
		/* FIXME this one is a revision 'C' according to the schematics */
		.variant = "max performance",
		.revision = "B",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6q-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_DISPLAY_PARALLEL,
	},
	[13] = {
		.variant = "low cost",
		.revision = "C",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6dl-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[14] = {
		.variant = "high performance",
		.revision = "C",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6q-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[15] = {
		.variant = "middle performance",
		.revision = "C",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6dl-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[16] = {
		.variant = "Solo_R512M_F4G",
		.revision = "C",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6dl-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[17] = {
		.variant = "Quad_R2G_F8G",
		.revision = "C",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6q-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[18] = {
		.variant = "QuadPlus_R4G_F16G",
		.revision = "C",
		.soc = "i.MX6Q+",
		.dts_compatible = "skov,imx6q-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[19] = {
		.variant = "Solo_R512M_F2G",
		.revision = "C",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6dl-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[20] = {
		.variant = "Quad_R1G_F4G",
		.revision = "C",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6q-skov-revc-lt2",
		.display = "l2rt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[21] = {
		.variant = "Solo_R512M_F2G",
		.revision = "C",
		.soc = "i.MX6S",
		.dts_compatible = "skov,imx6dl-skov-revc-lt6",
		.display = "l6whrt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[22] = {
		.variant = "Quad_R1G_F4G",
		.revision = "C",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6q-skov-revc-lt6",
		.display = "l6whrt",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_PARALLEL,
	},
	[24] = {
		.variant = "Quad_R1G_F4G",
		.revision = "E",
		.soc = "i.MX6Q",
		.dts_compatible = "skov,imx6q-skov-reve-mi1010ait-1cp1",
		.display = "mi1010ait-1cp1",
		.flags = SKOV_ENABLE_MMC_POWER | SKOV_DISPLAY_LVDS,
	},
};

static int skov_board_no = -1;
static bool skov_have_switch = true;
static const char *no_switch_suffix = "-noswitch";

static void fixup_machine_compatible(const char *compat,
				     struct device_node *root)
{
	int cclen = 0, clen = strlen(compat) + 1;
	const char *curcompat;
	void *buf;

	if (!root) {
		root = of_get_root_node();
		if (!root)
			return;
	}

	curcompat = of_get_property(root, "compatible", &cclen);

	buf = xzalloc(cclen + clen);

	memcpy(buf, compat, clen);
	memcpy(buf + clen, curcompat, cclen);

	/*
	 * Prepend the compatible from board entry to the machine compatible.
	 * Used to match bootspec entries against it.
	 */
	of_set_property(root, "compatible", buf, cclen + clen, true);

	free(buf);
}

static void fixup_noswitch_machine_compatible(struct device_node *root)
{
	const char *compat = imx6_variants[skov_board_no].dts_compatible;
	const char *generic = "skov,imx6";
	size_t size, size_generic;
	char *buf;

	size = strlen(compat) + strlen(no_switch_suffix) + 1;
	size_generic = strlen(generic) + strlen(no_switch_suffix) + 1;
	size = max(size, size_generic);

	/* add generic compatible, so systemd&co can make right decisions */
	buf = xasprintf("%s%s", generic, no_switch_suffix);
	fixup_machine_compatible(buf, root);

	/* add specific compatible as fallback, in case this board has new
	 * challenges.
	 */
	buf = xasprintf("%s%s", compat, no_switch_suffix);
	fixup_machine_compatible(buf, root);

	free(buf);
}

static void skov_imx6_no_switch(struct device_node *root)
{
	const char *fec_alias = "ethernet0";
	struct device_node *node;
	int ret;

	fixup_noswitch_machine_compatible(root);

	node = of_find_node_by_alias(root, fec_alias);
	if (node) {
		ret = of_device_disable(node);
		if (ret)
			pr_warn("Can't disable %s\n", fec_alias);
	} else {
		pr_warn("Can't find node by alias: %s\n", fec_alias);
	}

	node = of_find_node_by_alias(root, "mdio-gpio0");
	if (node) {
		ret = of_device_disable(node);
		if (ret)
			pr_warn("Can't disable mdio-gpio0 node\n");
	} else {
		pr_warn("Can't find mdio-gpio0 node\n");
	}
}

static void skov_imx6_switch(struct device_node *root)
{
	eth_of_fixup_node_from_eth_device(root,
			"/mdio-gpio/ksz8873@3/ports/ports@0", "eth0");
	eth2_of_fixup_node_individually(root,
		"/mdio-gpio/ksz8873@3/ports/ports@1", "eth0",
		"state.ethaddr.eth2", "/state/ethaddr/eth2");
}

static int skov_imx6_fixup(struct device_node *root, void *unused)
{
	struct device_node *chosen = of_create_node(root, "/chosen");
	struct device_node *node;
	uint32_t brightness;
	const char *val;
	int ret;

	if (skov_have_switch)
		skov_imx6_switch(root);
	else
		skov_imx6_no_switch(root);

	switch (bootsource_get()) {
	case BOOTSOURCE_MMC:
		/* use default variant of state variable defined in devicetree */
		brightness = 8;
		break;
	default:
		val = getenv("state.display.brightness");
		if (!val) {
			pr_err("could not get default display brightness\n");
			return 0;
		}

		brightness = simple_strtoul(val, NULL, 0);
		break;
	}

	for_each_compatible_node_from(node, root, NULL, "pwm-backlight") {
		ret = of_property_write_u32(node, "default-brightness-level", brightness);
		if (ret)
			pr_err("error %d while setting default-brightness-level property on node %s to %d\n",
				ret, node->name, brightness);
	}

	of_property_write_u32(chosen, "skov,imx6-board-version", skov_board_no);
	of_property_write_string(chosen, "skov,imx6-board-variant",
				 imx6_variants[skov_board_no].variant);

	return 0;
}

/*
 * Some variants need tweaks to make them work
 *
 * Revision A has no backlight control, since revision B it is present (GPIO6/23)
 * Revision A needs GPIO1/24 to be low to make network working
 * Revision C can control the SD main power supply
 */
static void skov_init_board(const struct board_description *variant)
{
	struct device_node *np;
	char *environment_path, *envdev;
	int ret;

	imx6_bbu_internal_spi_i2c_register_handler("spiflash", "/dev/m25p0.barebox",
		BBU_HANDLER_FLAG_DEFAULT);

	of_register_fixup(skov_imx6_fixup, NULL);

	switch (bootsource_get()) {
	case BOOTSOURCE_MMC:
		environment_path = "/chosen/environment-sd";
		envdev = "MMC";
		break;
	default:
		environment_path = "/chosen/environment-spinor";
		envdev = "SPI NOR flash";
		break;
	}

	pr_notice("Using environment in %s\n", envdev);

	ret = of_device_enable_path(environment_path);
	if (ret < 0)
		pr_warn("Failed to enable environment partition '%s' (%d)\n",
			environment_path, ret);

	if (variant->flags & SKOV_NEED_ENABLE_RMII) {
		/*
		 * MX6QDL_PAD_ENET_RX_ER__GPIO1_IO24 is a gpio which must be
		 * low to enable the RMII from the switch point of view
		 */
		gpio_request(24, "must_be_low");
		gpio_direction_output(24, 0);
	}

	/* SD card handling */
	gpio_request(205, "mmc io supply");
	gpio_direction_output(205, 0); /* select 3.3 V IO voltage */

	if (variant->flags & SKOV_ENABLE_MMC_POWER) {
		/*
		 * keep in sync with devicetree's 'regulator-boot-on' setting for
		 * this regulator
		 */
		gpio_request(200, "mmc power supply");
		gpio_direction_output(200, 0); /* switch on */
		mdelay(1);
		gpio_direction_output(200, 1); /* switch on */
	}

	if (variant->flags & SKOV_DISPLAY_PARALLEL) {
		np = of_find_compatible_node(NULL, NULL, "fsl,imx-parallel-display");
		if (np)
			of_device_enable_and_register(np);
		else
			pr_err("Cannot find \"fsl,imx-parallel-display\" node\n");
	}

	if (variant->flags & SKOV_DISPLAY_LVDS) {
		np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-ldb");
		if (np)
			of_device_enable_and_register(np);
		else
			pr_err("Cannot find \"fsl,imx6q-ldb\" node\n");

		/* ... as well as its channel 0 */
		np = of_find_node_by_name(np, "lvds-channel@0");
		if (np)
			of_device_enable(np);
		else
			pr_err("Cannot find \"lvds-channel@0\" node\n");
	}
}

static int skov_switch_test(void)
{
	struct phy_device *phydev;
	struct device_d *eth0;
	struct mii_bus *mii;
	int ret;

	if (skov_board_no < 0)
		return 0;

	/* On this boards, we have only one MDIO bus. So, it is enough to take
	 * the first one.
	 */
	mii = mdiobus_get_bus(SKOV_GPIO_MDIO_BUS);
	/* We can't read the switch ID, but we get get ID of the first PHY,
	 * which is enough to test if the switch is attached.
	 */
	phydev = get_phy_device(mii, SKOV_LAN1_PHY_ADDR);
	if (IS_ERR(phydev))
		goto no_switch;

	if (phydev->phy_id != PHY_ID_KSZ886X)
		goto no_switch;

	return 0;

no_switch:
	skov_have_switch = false;

	pr_notice("No-switch variant is detected\n");

	eth0 = get_device_by_name("eth0");
	if (eth0) {
		ret = dev_set_param(eth0, "mode", "disabled");
		if (ret)
			pr_warn("Can't set eth0 mode\n");
	} else {
		pr_warn("Can't disable eth0\n");
	}

	return 0;
}
late_initcall(skov_switch_test);

static int skov_imx6_probe(struct device_d *dev)
{
	unsigned v = 0;
	const struct board_description *variant;

	v = skov_imx6_get_version();

	if (v >= ARRAY_SIZE(imx6_variants)) {
		dev_err(dev, "Invalid variant %u\n", v);
		return -EINVAL;
	}

	variant = &imx6_variants[v];

	if (!variant->variant) {
		dev_err(dev, "Invalid variant %u\n", v);
		return -EINVAL;
	}

	skov_board_no = v;

	globalvar_add_simple_int("board.no", &skov_board_no, "%u");
	globalvar_add_simple("board.variant", variant->variant);
	globalvar_add_simple("board.revision",variant->revision);
	globalvar_add_simple("board.soc", variant->soc);
	globalvar_add_simple("board.dts", variant->dts_compatible);
	globalvar_add_simple("board.display", variant->display ?: NULL);

	fixup_machine_compatible(variant->dts_compatible, NULL);

	skov_init_board(variant);

	return 0;
}

static __maybe_unused struct of_device_id skov_version_ids[] = {
	{
		.compatible = "skov,imx6",
	}, {
		/* sentinel */
	}
};

static struct driver_d skov_version_driver = {
	.name = "skov-imx6",
	.probe = skov_imx6_probe,
	.of_compatible = DRV_OF_COMPAT(skov_version_ids),
};
coredevice_platform_driver(skov_version_driver);

static void skov_imx6_devices_shutdown(void)
{
	const char *external;

	if (skov_board_no < 0)
		return;

	external = getenv("state.display.external");
	if (!external) {
		pr_err("could not get state variable display.external\n");
		return;
	}

	if (!strcmp(external, "0"))
		setenv("backlight0.brightness", "0");
}
predevshutdown_exitcall(skov_imx6_devices_shutdown);