xref: /openbmc/linux/arch/sh/kernel/machvec.c (revision d0e22329)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arch/sh/kernel/machvec.c
4  *
5  * The SuperH machine vector setup handlers, yanked from setup.c
6  *
7  *  Copyright (C) 1999  Niibe Yutaka
8  *  Copyright (C) 2002 - 2007 Paul Mundt
9  */
10 #include <linux/init.h>
11 #include <linux/string.h>
12 #include <asm/machvec.h>
13 #include <asm/sections.h>
14 #include <asm/addrspace.h>
15 #include <asm/setup.h>
16 #include <asm/io.h>
17 #include <asm/irq.h>
18 
19 #define MV_NAME_SIZE 32
20 
21 #define for_each_mv(mv) \
22 	for ((mv) = (struct sh_machine_vector *)&__machvec_start; \
23 	     (mv) && (unsigned long)(mv) < (unsigned long)&__machvec_end; \
24 	     (mv)++)
25 
26 static struct sh_machine_vector * __init get_mv_byname(const char *name)
27 {
28 	struct sh_machine_vector *mv;
29 
30 	for_each_mv(mv)
31 		if (strcasecmp(name, mv->mv_name) == 0)
32 			return mv;
33 
34 	return NULL;
35 }
36 
37 static unsigned int __initdata machvec_selected;
38 
39 static int __init early_parse_mv(char *from)
40 {
41 	char mv_name[MV_NAME_SIZE] = "";
42 	char *mv_end;
43 	char *mv_comma;
44 	int mv_len;
45 	struct sh_machine_vector *mvp;
46 
47 	mv_end = strchr(from, ' ');
48 	if (mv_end == NULL)
49 		mv_end = from + strlen(from);
50 
51 	mv_comma = strchr(from, ',');
52 	mv_len = mv_end - from;
53 	if (mv_len > (MV_NAME_SIZE-1))
54 		mv_len = MV_NAME_SIZE-1;
55 	memcpy(mv_name, from, mv_len);
56 	mv_name[mv_len] = '\0';
57 	from = mv_end;
58 
59 	machvec_selected = 1;
60 
61 	/* Boot with the generic vector */
62 	if (strcmp(mv_name, "generic") == 0)
63 		return 0;
64 
65 	mvp = get_mv_byname(mv_name);
66 	if (unlikely(!mvp)) {
67 		printk("Available vectors:\n\n\t'%s', ", sh_mv.mv_name);
68 		for_each_mv(mvp)
69 			printk("'%s', ", mvp->mv_name);
70 		printk("\n\n");
71 		panic("Failed to select machvec '%s' -- halting.\n",
72 		      mv_name);
73 	} else
74 		sh_mv = *mvp;
75 
76 	return 0;
77 }
78 early_param("sh_mv", early_parse_mv);
79 
80 void __init sh_mv_setup(void)
81 {
82 	/*
83 	 * Only overload the machvec if one hasn't been selected on
84 	 * the command line with sh_mv=
85 	 */
86 	if (!machvec_selected) {
87 		unsigned long machvec_size;
88 
89 		machvec_size = ((unsigned long)&__machvec_end -
90 				(unsigned long)&__machvec_start);
91 
92 		/*
93 		 * Sanity check for machvec section alignment. Ensure
94 		 * __initmv hasn't been misused.
95 		 */
96 		if (machvec_size % sizeof(struct sh_machine_vector))
97 			panic("machvec misaligned, invalid __initmv use?");
98 
99 		/*
100 		 * If the machvec hasn't been preselected, use the first
101 		 * vector (usually the only one) from .machvec.init.
102 		 */
103 		if (machvec_size >= sizeof(struct sh_machine_vector))
104 			sh_mv = *(struct sh_machine_vector *)&__machvec_start;
105 	}
106 
107 	printk(KERN_NOTICE "Booting machvec: %s\n", get_system_type());
108 
109 	/*
110 	 * Manually walk the vec, fill in anything that the board hasn't yet
111 	 * by hand, wrapping to the generic implementation.
112 	 */
113 #define mv_set(elem) do { \
114 	if (!sh_mv.mv_##elem) \
115 		sh_mv.mv_##elem = generic_##elem; \
116 } while (0)
117 
118 	mv_set(irq_demux);
119 	mv_set(mode_pins);
120 	mv_set(mem_init);
121 }
122