Feature #38

tp_smapi support on ThinkPads

Added by Iru Cai over 3 years ago. Updated almost 2 years ago.

Status:NewStart date:03/15/2016
Priority:NormalDue date:
Assignee:Alexander Couzens% Done:

50%

Category:-
Target version:-

Description

tp_smapi kernel module: https://github.com/evgeni/tp_smapi

SMAPI a useful feature for setting the battery charge threshold. The firmware part of SMAPI is implemented in the boot firmware (IMO the SMM part). Maybe we have to reverse engineer the factory firmware to know how the firmware code talk to the EC. We also need to change the cmos.layout of the supported laptops.

History

#1 Updated by Alexander Couzens over 3 years ago

  • Assignee set to Alexander Couzens

#2 Updated by Alexander Couzens over 3 years ago

Correct, near everything is implemented in smm on lenovo. But smm is a huge backdoor. But we don't need smm for that. You're right in the EC stuff. We only have to talk to the EC.

Can you test please:
cd coreboot/util/ectool ; make;

/* start charging when less than 0x46 (70 %) */
./ectool -w 0xb0 -z 0x46

/* stop charging when 0x5a (90 %) */
./ectool -w 0xb1 -w 0x5a

#3 Updated by Iru Cai over 3 years ago

I tested on my laptops.

Lenovo T520:
stop_charge_threshold can be set with:
./ectool -w 0xb1 -w 0x5a
start_charge_threshold doesn't work (and not supported in tp_smapi)

Lenovo X200:
Writing to EC RAM doesn't work, maybe I'm wrinting to a wrong address.

In tp_smapi module, the SMAPI port is read from the NVRAM. In T520, it's 0x00b2. I don't have a factory X200 firmware so I don't know the SMAPI port.

#4 Updated by Iru Cai over 3 years ago

  • % Done changed from 0 to 50

X200/X200s uses the same smapi port as xx20 models, but the way BIOS talk to EC is different in these machines.

I just tried smapi and ectool on a X200 with factory firmware, and I found these:

EC RAM addr 0x03: bit 2 (0x4) selects start_charge_thresh/stop_charge_thresh, 0 means start_charge_thresh and 1 means stop_charge_thresh, the default value of EC[0x03] is 0x40 or 0x44.

After running ectool -w 0x03 -z 0x40, EC RAM will give the start_charge_thresh value at EC[0x24], running ectool -w 0x03 -z 0x44 EC RAM will give the stop_charge_thresh value at EC[0x24]. However, 0x23 and 0x24 are read only and it's no use writing to these EC address.

#5 Updated by Michael Widlok almost 3 years ago

Are there any news about this subject recently? I'm very interested in getting battery charging control (thresholds) on thinkpad T400.

I've tested ectool commands on my laptop (T400 with coreboot CBET4000 1c84243 from 09/07/2016) but no luck... If there is anything to test on T400, then pleas tell me - maybe I will be able to help. I can also perform some trial and error tests if needed, I'm not afraid to disassembly and re-flash.

Please also point me some threads/literature about EC support in coreboot on thinkpads - I was not able to find anything but this "feature".
Thank You, Michael Widlok

#6 Updated by Daniel Kulesz over 2 years ago

Are you able to load the tp-smapi module while running coreboot? I noticed, that it loads fine on a X200s while it fails on a X200 with the following message:

tp_smapi 0.42 loading...
SMAPI not supported (ID=0x73)
tp_smapi init failed (ret=-6)

#7 Updated by Daniel Kulesz over 2 years ago

Adding to that: I flashed the exact same coreboot image from the X200s on the X200, and on the X200 the module still fails to load. I also swapped the drives between the two machines, and both operating systems loaded the module on the X200s while they failed on the X200. NVRAM is not activated.

#8 Updated by Arthur Heymans about 2 years ago

Iru Cai wrote:

X200/X200s uses the same smapi port as xx20 models, but the way BIOS talk to EC is different in these machines.

I just tried smapi and ectool on a X200 with factory firmware, and I found these:

EC RAM addr 0x03: bit 2 (0x4) selects start_charge_thresh/stop_charge_thresh, 0 means start_charge_thresh and 1 means stop_charge_thresh, the default value of EC[0x03] is 0x40 or 0x44.

After running ectool -w 0x03 -z 0x40, EC RAM will give the start_charge_thresh value at EC[0x24], running ectool -w 0x03 -z 0x44 EC RAM will give the stop_charge_thresh value at EC[0x24]. However, 0x23 and 0x24 are read only and it's no use writing to these EC address.

This was very close to what is needed. So EC[0x03],bit2 is indeed trigger between start (0) and stop (1). EC[0x24],bit0-6 are the configuration bits for the thresholds and EC[0x24],bit7 needs to be one when writing to this register. So for instance to set stop threshold:
ectool -w 0x3 -z 0x44 (well just flip bit2)
ectool -w 0x24 -z 0xd0 (bit7 + threshold of 90%)
you should now see 0x50 at offset 0x24 (ectool -d)
to set start threshold:
ectool -w 0x3 -z 0x40 (well just flip bit2 again)
ectool -w 0x24 -z 0xc6 (bit7 + threshold of 70%)
and you should see 0x46 at offset 0x24.

Not sure if it is possible to have this done 'automatically' in an ACPI driver. Other options are to write a little wrapper script that runs at boot or have coreboot do this.

#9 Updated by Remi Reuvekamp about 2 years ago

Arthur Heymans wrote:

This was very close to what is needed. So EC[0x03],bit2 is indeed trigger between start (0) and stop (1). EC[0x24],bit0-6 are the configuration bits for the thresholds and EC[0x24],bit7 needs to be one when writing to this register. So for instance to set stop threshold:
ectool -w 0x3 -z 0x44 (well just flip bit2)
ectool -w 0x24 -z 0xd0 (bit7 + threshold of 90%)
you should now see 0x50 at offset 0x24 (ectool -d)
to set start threshold:
ectool -w 0x3 -z 0x40 (well just flip bit2 again)
ectool -w 0x24 -z 0xc6 (bit7 + threshold of 70%)
and you should see 0x46 at offset 0x24.

Not sure if it is possible to have this done 'automatically' in an ACPI driver. Other options are to write a little wrapper script that runs at boot or have coreboot do this.

I can confirm that this works as expected on an X220. I've currently got this in a script that runs on boot!
(Even though that's not necessary when keeping the battery plugged in.)

Minor correction: 0xd0 is not 90% but 80% (208-128).

#10 Updated by Arthur Heymans almost 2 years ago

Implemented the acpi functions tpacpi-bat uses in https://review.coreboot.org/#/c/coreboot/+/20913/. This can easily be extended to later EC firmwares. I guess this can be closed?

Also available in: Atom PDF