1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <console/console.h>
5 #include <device/device.h>
11 static u16 ec_cmd_reg
= 0;
12 static u16 ec_data_reg
= 0;
14 static inline u8
__ec_read(u8 addr
)
16 outb(addr
, ec_cmd_reg
);
17 return inb(ec_data_reg
);
20 static inline void __ec_write(u8 addr
, u8 data
)
22 outb(addr
, ec_cmd_reg
);
23 outb(data
, ec_data_reg
);
26 static int ec_ready(void)
28 u16 timeout
= EC_TIMEOUT
;
30 if (!ec_cmd_reg
|| !ec_data_reg
) {
31 printk(BIOS_DEBUG
, "Invalid ports: cmd=0x%x data=0x%x\n",
32 ec_cmd_reg
, ec_data_reg
);
36 while (__ec_read(EC_MAILBOX_COMMAND
) != 0 && --timeout
) {
38 if ((timeout
& 0xff) == 0)
39 printk(BIOS_SPEW
, ".");
42 printk(BIOS_DEBUG
, "Timeout waiting for EC to be ready.\n");
48 int send_ec_command(u8 command
)
52 __ec_write(EC_MAILBOX_COMMAND
, command
);
56 int send_ec_command_data(u8 command
, u8 data
)
60 __ec_write(EC_MAILBOX_DATA
, data
);
61 __ec_write(EC_MAILBOX_COMMAND
, command
);
65 u8
read_ec_command_byte(u8 command
)
67 send_ec_command(command
);
68 return __ec_read(EC_MAILBOX_DATA
);
73 if (send_ec_command_data(EC_RAM_READ
, addr
) < 0)
75 return __ec_read(EC_MAILBOX_DATA
);
78 int ec_write(u8 addr
, u8 data
)
82 __ec_write(EC_MAILBOX_DATA
, addr
);
83 __ec_write(EC_MAILBOX_DATA_H
, data
);
84 __ec_write(EC_MAILBOX_COMMAND
, EC_RAM_WRITE
);
88 void ec_set_bit(u8 addr
, u8 bit
)
90 ec_write(addr
, ec_read(addr
) | (1 << bit
));
93 void ec_clr_bit(u8 addr
, u8 bit
)
95 ec_write(addr
, ec_read(addr
) & ~(1 << bit
));
98 void ec_set_ports(u16 cmd_reg
, u16 data_reg
)
100 ec_cmd_reg
= cmd_reg
;
101 ec_data_reg
= data_reg
;
104 static void mec1308_enable(struct device
*dev
)
106 DEVTREE_CONST
struct ec_smsc_mec1308_config
*conf
= dev
->chip_info
;
108 if (conf
->mailbox_port
) {
109 ec_cmd_reg
= conf
->mailbox_port
;
110 ec_data_reg
= conf
->mailbox_port
+ 1;
114 struct chip_operations ec_smsc_mec1308_ops
= {
115 .name
= "SMSC MEC1308 EC Mailbox Interface",
116 .enable_dev
= mec1308_enable