1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun #define pr_fmt(fmt) "ipmi_hardcode: " fmt
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/moduleparam.h>
6*4882a593Smuzhiyun #include <linux/platform_device.h>
7*4882a593Smuzhiyun #include "ipmi_si.h"
8*4882a593Smuzhiyun #include "ipmi_plat_data.h"
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun /*
11*4882a593Smuzhiyun * There can be 4 IO ports passed in (with or without IRQs), 4 addresses,
12*4882a593Smuzhiyun * a default IO port, and 1 ACPI/SPMI address. That sets SI_MAX_DRIVERS.
13*4882a593Smuzhiyun */
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define SI_MAX_PARMS 4
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define MAX_SI_TYPE_STR 30
18*4882a593Smuzhiyun static char si_type_str[MAX_SI_TYPE_STR] __initdata;
19*4882a593Smuzhiyun static unsigned long addrs[SI_MAX_PARMS];
20*4882a593Smuzhiyun static unsigned int num_addrs;
21*4882a593Smuzhiyun static unsigned int ports[SI_MAX_PARMS];
22*4882a593Smuzhiyun static unsigned int num_ports;
23*4882a593Smuzhiyun static int irqs[SI_MAX_PARMS] __initdata;
24*4882a593Smuzhiyun static unsigned int num_irqs __initdata;
25*4882a593Smuzhiyun static int regspacings[SI_MAX_PARMS] __initdata;
26*4882a593Smuzhiyun static unsigned int num_regspacings __initdata;
27*4882a593Smuzhiyun static int regsizes[SI_MAX_PARMS] __initdata;
28*4882a593Smuzhiyun static unsigned int num_regsizes __initdata;
29*4882a593Smuzhiyun static int regshifts[SI_MAX_PARMS] __initdata;
30*4882a593Smuzhiyun static unsigned int num_regshifts __initdata;
31*4882a593Smuzhiyun static int slave_addrs[SI_MAX_PARMS] __initdata;
32*4882a593Smuzhiyun static unsigned int num_slave_addrs __initdata;
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun module_param_string(type, si_type_str, MAX_SI_TYPE_STR, 0);
35*4882a593Smuzhiyun MODULE_PARM_DESC(type, "Defines the type of each interface, each"
36*4882a593Smuzhiyun " interface separated by commas. The types are 'kcs',"
37*4882a593Smuzhiyun " 'smic', and 'bt'. For example si_type=kcs,bt will set"
38*4882a593Smuzhiyun " the first interface to kcs and the second to bt");
39*4882a593Smuzhiyun module_param_hw_array(addrs, ulong, iomem, &num_addrs, 0);
40*4882a593Smuzhiyun MODULE_PARM_DESC(addrs, "Sets the memory address of each interface, the"
41*4882a593Smuzhiyun " addresses separated by commas. Only use if an interface"
42*4882a593Smuzhiyun " is in memory. Otherwise, set it to zero or leave"
43*4882a593Smuzhiyun " it blank.");
44*4882a593Smuzhiyun module_param_hw_array(ports, uint, ioport, &num_ports, 0);
45*4882a593Smuzhiyun MODULE_PARM_DESC(ports, "Sets the port address of each interface, the"
46*4882a593Smuzhiyun " addresses separated by commas. Only use if an interface"
47*4882a593Smuzhiyun " is a port. Otherwise, set it to zero or leave"
48*4882a593Smuzhiyun " it blank.");
49*4882a593Smuzhiyun module_param_hw_array(irqs, int, irq, &num_irqs, 0);
50*4882a593Smuzhiyun MODULE_PARM_DESC(irqs, "Sets the interrupt of each interface, the"
51*4882a593Smuzhiyun " addresses separated by commas. Only use if an interface"
52*4882a593Smuzhiyun " has an interrupt. Otherwise, set it to zero or leave"
53*4882a593Smuzhiyun " it blank.");
54*4882a593Smuzhiyun module_param_hw_array(regspacings, int, other, &num_regspacings, 0);
55*4882a593Smuzhiyun MODULE_PARM_DESC(regspacings, "The number of bytes between the start address"
56*4882a593Smuzhiyun " and each successive register used by the interface. For"
57*4882a593Smuzhiyun " instance, if the start address is 0xca2 and the spacing"
58*4882a593Smuzhiyun " is 2, then the second address is at 0xca4. Defaults"
59*4882a593Smuzhiyun " to 1.");
60*4882a593Smuzhiyun module_param_hw_array(regsizes, int, other, &num_regsizes, 0);
61*4882a593Smuzhiyun MODULE_PARM_DESC(regsizes, "The size of the specific IPMI register in bytes."
62*4882a593Smuzhiyun " This should generally be 1, 2, 4, or 8 for an 8-bit,"
63*4882a593Smuzhiyun " 16-bit, 32-bit, or 64-bit register. Use this if you"
64*4882a593Smuzhiyun " the 8-bit IPMI register has to be read from a larger"
65*4882a593Smuzhiyun " register.");
66*4882a593Smuzhiyun module_param_hw_array(regshifts, int, other, &num_regshifts, 0);
67*4882a593Smuzhiyun MODULE_PARM_DESC(regshifts, "The amount to shift the data read from the."
68*4882a593Smuzhiyun " IPMI register, in bits. For instance, if the data"
69*4882a593Smuzhiyun " is read from a 32-bit word and the IPMI data is in"
70*4882a593Smuzhiyun " bit 8-15, then the shift would be 8");
71*4882a593Smuzhiyun module_param_hw_array(slave_addrs, int, other, &num_slave_addrs, 0);
72*4882a593Smuzhiyun MODULE_PARM_DESC(slave_addrs, "Set the default IPMB slave address for"
73*4882a593Smuzhiyun " the controller. Normally this is 0x20, but can be"
74*4882a593Smuzhiyun " overridden by this parm. This is an array indexed"
75*4882a593Smuzhiyun " by interface number.");
76*4882a593Smuzhiyun
ipmi_hardcode_init_one(const char * si_type_str,unsigned int i,unsigned long addr,enum ipmi_addr_space addr_space)77*4882a593Smuzhiyun static void __init ipmi_hardcode_init_one(const char *si_type_str,
78*4882a593Smuzhiyun unsigned int i,
79*4882a593Smuzhiyun unsigned long addr,
80*4882a593Smuzhiyun enum ipmi_addr_space addr_space)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun struct ipmi_plat_data p;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun memset(&p, 0, sizeof(p));
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun p.iftype = IPMI_PLAT_IF_SI;
87*4882a593Smuzhiyun if (!si_type_str || !*si_type_str || strcmp(si_type_str, "kcs") == 0) {
88*4882a593Smuzhiyun p.type = SI_KCS;
89*4882a593Smuzhiyun } else if (strcmp(si_type_str, "smic") == 0) {
90*4882a593Smuzhiyun p.type = SI_SMIC;
91*4882a593Smuzhiyun } else if (strcmp(si_type_str, "bt") == 0) {
92*4882a593Smuzhiyun p.type = SI_BT;
93*4882a593Smuzhiyun } else if (strcmp(si_type_str, "invalid") == 0) {
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun * Allow a firmware-specified interface to be
96*4882a593Smuzhiyun * disabled.
97*4882a593Smuzhiyun */
98*4882a593Smuzhiyun p.type = SI_TYPE_INVALID;
99*4882a593Smuzhiyun } else {
100*4882a593Smuzhiyun pr_warn("Interface type specified for interface %d, was invalid: %s\n",
101*4882a593Smuzhiyun i, si_type_str);
102*4882a593Smuzhiyun return;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun p.regsize = regsizes[i];
106*4882a593Smuzhiyun p.slave_addr = slave_addrs[i];
107*4882a593Smuzhiyun p.addr_source = SI_HARDCODED;
108*4882a593Smuzhiyun p.regshift = regshifts[i];
109*4882a593Smuzhiyun p.regsize = regsizes[i];
110*4882a593Smuzhiyun p.addr = addr;
111*4882a593Smuzhiyun p.space = addr_space;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun ipmi_platform_add("hardcode-ipmi-si", i, &p);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
ipmi_hardcode_init(void)116*4882a593Smuzhiyun void __init ipmi_hardcode_init(void)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun unsigned int i;
119*4882a593Smuzhiyun char *str;
120*4882a593Smuzhiyun char *si_type[SI_MAX_PARMS];
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun memset(si_type, 0, sizeof(si_type));
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun /* Parse out the si_type string into its components. */
125*4882a593Smuzhiyun str = si_type_str;
126*4882a593Smuzhiyun if (*str != '\0') {
127*4882a593Smuzhiyun for (i = 0; (i < SI_MAX_PARMS) && (*str != '\0'); i++) {
128*4882a593Smuzhiyun si_type[i] = str;
129*4882a593Smuzhiyun str = strchr(str, ',');
130*4882a593Smuzhiyun if (str) {
131*4882a593Smuzhiyun *str = '\0';
132*4882a593Smuzhiyun str++;
133*4882a593Smuzhiyun } else {
134*4882a593Smuzhiyun break;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun for (i = 0; i < SI_MAX_PARMS; i++) {
140*4882a593Smuzhiyun if (i < num_ports && ports[i])
141*4882a593Smuzhiyun ipmi_hardcode_init_one(si_type[i], i, ports[i],
142*4882a593Smuzhiyun IPMI_IO_ADDR_SPACE);
143*4882a593Smuzhiyun if (i < num_addrs && addrs[i])
144*4882a593Smuzhiyun ipmi_hardcode_init_one(si_type[i], i, addrs[i],
145*4882a593Smuzhiyun IPMI_MEM_ADDR_SPACE);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun
ipmi_si_hardcode_exit(void)150*4882a593Smuzhiyun void ipmi_si_hardcode_exit(void)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun ipmi_remove_platform_device_by_name("hardcode-ipmi-si");
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /*
156*4882a593Smuzhiyun * Returns true of the given address exists as a hardcoded address,
157*4882a593Smuzhiyun * false if not.
158*4882a593Smuzhiyun */
ipmi_si_hardcode_match(int addr_space,unsigned long addr)159*4882a593Smuzhiyun int ipmi_si_hardcode_match(int addr_space, unsigned long addr)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun unsigned int i;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (addr_space == IPMI_IO_ADDR_SPACE) {
164*4882a593Smuzhiyun for (i = 0; i < num_ports; i++) {
165*4882a593Smuzhiyun if (ports[i] == addr)
166*4882a593Smuzhiyun return 1;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun } else {
169*4882a593Smuzhiyun for (i = 0; i < num_addrs; i++) {
170*4882a593Smuzhiyun if (addrs[i] == addr)
171*4882a593Smuzhiyun return 1;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun return 0;
176*4882a593Smuzhiyun }
177