| 1 |
|
| 2 |
|
| 3 |
#include <console/console.h> |
| 4 |
#include <device/device.h> |
| 5 |
#include <device/pnp.h> |
| 6 |
#include <ec/acpi/ec.h> |
| 7 |
#include <option.h> |
| 8 |
#include <pc80/mc146818rtc.h> |
| 9 |
#include <halt.h> |
| 10 |
|
| 11 |
#include "ecdefs.h" |
| 12 |
#include "option_table.h" |
| 13 |
#include "ec.h" |
| 14 |
|
| 15 |
|
| 16 |
uint16_t ec_get_version(void) |
| 17 |
{ |
| 18 |
return (ec_read(ECRAM_MAJOR_VERSION) << 8) | ec_read(ECRAM_MINOR_VERSION); |
| 19 |
} |
| 20 |
|
| 21 |
static uint8_t get_cmos_value(uint32_t bit, uint32_t length) |
| 22 |
{ |
| 23 |
uint32_t byte, byte_bit; |
| 24 |
uint8_t uchar; |
| 25 |
|
| 26 |
byte = bit / 8; |
| 27 |
byte_bit = bit % 8; |
| 28 |
|
| 29 |
uchar = cmos_read(byte); |
| 30 |
uchar >>= byte_bit; |
| 31 |
|
| 32 |
return uchar & ((1U << length) - 1); |
| 33 |
} |
| 34 |
|
| 35 |
static uint8_t get_ec_value_from_option(const char *name, |
| 36 |
uint32_t fallback, |
| 37 |
const uint8_t *lut, |
| 38 |
size_t lut_size, |
| 39 |
uint32_t cmos_start_bit, |
| 40 |
uint32_t cmos_length) |
| 41 |
{ |
| 42 |
unsigned int index; |
| 43 |
|
| 44 |
if (cmos_start_bit != UINT_MAX) |
| 45 |
index = get_cmos_value(cmos_start_bit, cmos_length); |
| 46 |
else |
| 47 |
index = get_uint_option(name, fallback); |
| 48 |
|
| 49 |
if (index >= lut_size) |
| 50 |
index = fallback; |
| 51 |
return lut[index]; |
| 52 |
} |
| 53 |
|
| 54 |
static uint16_t ec_get_chip_id(unsigned int port) |
| 55 |
{ |
| 56 |
return pnp_read_index(port, NUVOTON_CHIPID); |
| 57 |
} |
| 58 |
|
| 59 |
static void merlin_init(struct device *dev) |
| 60 |
{ |
| 61 |
if (!dev->enabled) |
| 62 |
return; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
if (dev->path.pnp.port != NUVOTON_FIXED_ADDR) { |
| 71 |
printk(BIOS_ERR, "NUVOTON: Incorrect ports defined in devicetree.cb.\n"); |
| 72 |
printk(BIOS_ERR, "NUVOTON: Serious operational issues will arise.\n"); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
const uint16_t chip_id = ec_get_chip_id(dev->path.pnp.port); |
| 77 |
|
| 78 |
if (chip_id != NUVOTON_CHIPID_VAL) { |
| 79 |
printk(BIOS_ERR, "NUVOTON: Expected chip ID 0x%04x, but got 0x%04x instead.\n", |
| 80 |
NUVOTON_CHIPID_VAL, chip_id); |
| 81 |
return; |
| 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 |
const uint8_t kbl_timeout[] = { |
| 107 |
SEC_30, |
| 108 |
MIN_1, |
| 109 |
MIN_3, |
| 110 |
MIN_5, |
| 111 |
NEVER |
| 112 |
}; |
| 113 |
|
| 114 |
ec_write(ECRAM_KBL_TIMEOUT, |
| 115 |
get_ec_value_from_option("kbl_timeout", |
| 116 |
0, |
| 117 |
kbl_timeout, |
| 118 |
ARRAY_SIZE(kbl_timeout), |
| 119 |
UINT_MAX, |
| 120 |
UINT_MAX)); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
const uint8_t fn_ctrl_swap[] = { |
| 132 |
FN_CTRL, |
| 133 |
CTRL_FN |
| 134 |
}; |
| 135 |
|
| 136 |
ec_write(ECRAM_FN_CTRL_REVERSE, |
| 137 |
get_ec_value_from_option("fn_ctrl_swap", |
| 138 |
0, |
| 139 |
fn_ctrl_swap, |
| 140 |
ARRAY_SIZE(fn_ctrl_swap), |
| 141 |
UINT_MAX, |
| 142 |
UINT_MAX)); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
const uint8_t max_charge[] = { |
| 154 |
CHARGE_100, |
| 155 |
CHARGE_80, |
| 156 |
CHARGE_60 |
| 157 |
}; |
| 158 |
|
| 159 |
if (CONFIG(EC_STARLABS_MAX_CHARGE)) |
| 160 |
ec_write(ECRAM_MAX_CHARGE, |
| 161 |
get_ec_value_from_option("max_charge", |
| 162 |
0, |
| 163 |
max_charge, |
| 164 |
ARRAY_SIZE(max_charge), |
| 165 |
UINT_MAX, |
| 166 |
UINT_MAX)); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
const uint8_t fast_charge[] = { |
| 178 |
CHARGE_RATE_NORMAL, |
| 179 |
CHARGE_RATE_FAST |
| 180 |
}; |
| 181 |
|
| 182 |
if (CONFIG(EC_STARLABS_FAST_CHARGE)) |
| 183 |
ec_write(ECRAM_FAST_CHARGE, |
| 184 |
get_ec_value_from_option("fast_charge", |
| 185 |
0, |
| 186 |
fast_charge, |
| 187 |
ARRAY_SIZE(fast_charge), |
| 188 |
UINT_MAX, |
| 189 |
UINT_MAX)); |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
const uint8_t fan_mode[] = { |
| 201 |
FAN_NORMAL, |
| 202 |
FAN_AGGRESSIVE, |
| 203 |
FAN_QUIET |
| 204 |
}; |
| 205 |
|
| 206 |
if (CONFIG(EC_STARLABS_FAN)) |
| 207 |
ec_write(ECRAM_FAN_MODE, |
| 208 |
get_ec_value_from_option("fan_mode", |
| 209 |
0, |
| 210 |
fan_mode, |
| 211 |
ARRAY_SIZE(fan_mode), |
| 212 |
UINT_MAX, |
| 213 |
UINT_MAX)); |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
#ifdef CMOS_VLEN_fn_lock_state |
| 225 |
const uint8_t fn_lock_state[] = { |
| 226 |
UNLOCKED, |
| 227 |
LOCKED |
| 228 |
}; |
| 229 |
|
| 230 |
ec_write(ECRAM_FN_LOCK_STATE, |
| 231 |
get_ec_value_from_option("fn_lock_state", |
| 232 |
1, |
| 233 |
fn_lock_state, |
| 234 |
ARRAY_SIZE(fn_lock_state), |
| 235 |
CMOS_VSTART_fn_lock_state, |
| 236 |
CMOS_VLEN_fn_lock_state)); |
| 237 |
#endif |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
#ifdef CMOS_VSTART_trackpad_state |
| 249 |
const uint8_t trackpad_state[] = { |
| 250 |
TRACKPAD_ENABLED, |
| 251 |
TRACKPAD_DISABLED |
| 252 |
}; |
| 253 |
|
| 254 |
ec_write(ECRAM_TRACKPAD_STATE, |
| 255 |
get_ec_value_from_option("trackpad_state", |
| 256 |
0, |
| 257 |
trackpad_state, |
| 258 |
ARRAY_SIZE(trackpad_state), |
| 259 |
CMOS_VSTART_trackpad_state, |
| 260 |
CMOS_VLEN_trackpad_state)); |
| 261 |
#endif |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#ifdef CMOS_VSTART_kbl_brightness |
| 273 |
const uint8_t kbl_brightness[] = { |
| 274 |
KBL_ON, |
| 275 |
KBL_OFF, |
| 276 |
KBL_LOW, |
| 277 |
KBL_HIGH |
| 278 |
}; |
| 279 |
|
| 280 |
ec_write(ECRAM_KBL_BRIGHTNESS, |
| 281 |
get_ec_value_from_option("kbl_brightness", |
| 282 |
CONFIG(EC_STARLABS_KBL_LEVELS) ? 2 : 0, |
| 283 |
kbl_brightness, |
| 284 |
ARRAY_SIZE(kbl_brightness), |
| 285 |
CMOS_VSTART_kbl_brightness, |
| 286 |
CMOS_VLEN_kbl_brightness)); |
| 287 |
|
| 288 |
#endif |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
ec_write(ECRAM_KBL_STATE, KBL_ENABLED); |
| 304 |
} |
| 305 |
|
| 306 |
static struct device_operations ops = { |
| 307 |
.init = merlin_init, |
| 308 |
.read_resources = noop_read_resources, |
| 309 |
.set_resources = noop_set_resources, |
| 310 |
}; |
| 311 |
|
| 312 |
static struct pnp_info pnp_dev_info[] = { |
| 313 |
|
| 314 |
{ NULL, NUVOTON_MSWC, PNP_IO0 | PNP_IRQ0, 0xfff0, }, |
| 315 |
|
| 316 |
{ NULL, NUVOTON_KBCM, PNP_IRQ0, }, |
| 317 |
|
| 318 |
{ NULL, NUVOTON_KBCK, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, }, |
| 319 |
|
| 320 |
{ NULL, NUVOTON_SHM, PNP_IO0 | PNP_IRQ0, 0xfff0, }, |
| 321 |
|
| 322 |
{ NULL, NUVOTON_PM1, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, }, |
| 323 |
|
| 324 |
{ NULL, NUVOTON_PM2, PNP_IO0 | PNP_IO1 | PNP_IO2 | PNP_IRQ0, 0x07fc, |
| 325 |
0x07fc, 0xfff0, }, |
| 326 |
|
| 327 |
{ NULL, NUVOTON_PM3, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, }, |
| 328 |
|
| 329 |
{ NULL, NUVOTON_ESHM }, |
| 330 |
|
| 331 |
{ NULL, NUVOTON_PM4, PNP_IO0 | PNP_IO1 | PNP_IRQ0, 0x07ff, 0x07ff, }, |
| 332 |
}; |
| 333 |
|
| 334 |
static void enable_dev(struct device *dev) |
| 335 |
{ |
| 336 |
pnp_enable_devices(dev, &ops, ARRAY_SIZE(pnp_dev_info), pnp_dev_info); |
| 337 |
} |
| 338 |
|
| 339 |
struct chip_operations ec_starlabs_merlin_ops = { |
| 340 |
.name = "NUVOTON EC", |
| 341 |
.enable_dev = enable_dev |
| 342 |
}; |