/* $NetBSD: lom.c,v 1.16 2018/09/03 16:29:27 riastradh Exp $ */
/* $OpenBSD: lom.c,v 1.21 2010/02/28 20:44:39 kettenis Exp $ */
/*
* Copyright (c) 2009 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* LOMlite is a so far unidentified microcontroller.
*/
#define LOM1_STATUS 0x00 /* R */
#define LOM1_STATUS_BUSY 0x80
#define LOM1_CMD 0x00 /* W */
#define LOM1_DATA 0x01 /* R/W */
/*
* LOMlite2 is implemented as a H8/3437 microcontroller which has its
* on-chip host interface hooked up to EBus.
*/
#define LOM2_DATA 0x00 /* R/W */
#define LOM2_CMD 0x01 /* W */
#define LOM2_STATUS 0x01 /* R */
#define LOM2_STATUS_OBF 0x01 /* Output Buffer Full */
#define LOM2_STATUS_IBF 0x02 /* Input Buffer Full */
static int
lom1_read_polled(struct lom_softc *sc, uint8_t reg, uint8_t *val)
{
uint8_t str;
int i;
/* Wait for input buffer to become available. */
for (i = 30; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
delay(1000);
if ((str & LOM1_STATUS_BUSY) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
/* Wait until the microcontroller fills output buffer. */
for (i = 30; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
delay(1000);
if ((str & LOM1_STATUS_BUSY) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
static int
lom1_write_polled(struct lom_softc *sc, uint8_t reg, uint8_t val)
{
uint8_t str;
int i;
/* Wait for input buffer to become available. */
for (i = 30; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
delay(1000);
if ((str & LOM1_STATUS_BUSY) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
/* Wait until the microcontroller fills output buffer. */
for (i = 30; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
delay(1000);
if ((str & LOM1_STATUS_BUSY) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM1_STATUS);
if (str & LOM1_STATUS_BUSY) {
if (sc->sc_retry++ < 30) {
callout_schedule(&sc->sc_state_to, mstohz(1));
return;
}
/*
* Looks like the microcontroller got wedged. Unwedge
* it by writing this magic value. Give it some time
* to recover.
*/
bus_space_write_1(sc->sc_iot, sc->sc_ioh, LOM1_DATA, 0xac);
callout_schedule(&sc->sc_state_to, mstohz(1000));
sc->sc_state = LOM_STATE_CMD;
return;
}
error = tsleep(&lc, PZERO, "lom2rd", hz);
if (error)
lom_dequeue_cmd(sc, &lc);
*val = lc.lc_data;
return (error);
}
static int
lom2_read_polled(struct lom_softc *sc, uint8_t reg, uint8_t *val)
{
uint8_t str;
int i;
/* Wait for input buffer to become available. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if ((str & LOM2_STATUS_IBF) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
/* Wait until the microcontroller fills output buffer. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if (str & LOM2_STATUS_OBF)
break;
}
if (i == 0)
return (ETIMEDOUT);
error = tsleep(&lc, PZERO, "lom2wr", hz);
if (error)
lom_dequeue_cmd(sc, &lc);
return (error);
}
static int
lom2_write_polled(struct lom_softc *sc, uint8_t reg, uint8_t val)
{
uint8_t str;
int i;
/* Wait for input buffer to become available. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if ((str & LOM2_STATUS_IBF) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
/* Wait until the microcontroller fills output buffer. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if (str & LOM2_STATUS_OBF)
break;
}
if (i == 0)
return (ETIMEDOUT);
/* Wait for input buffer to become available. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if ((str & LOM2_STATUS_IBF) == 0)
break;
}
if (i == 0)
return (ETIMEDOUT);
/* Wait until the microcontroller fills output buffer. */
for (i = 1000; i > 0; i--) {
str = bus_space_read_1(sc->sc_iot, sc->sc_ioh, LOM2_STATUS);
delay(10);
if (str & LOM2_STATUS_OBF)
break;
}
if (i == 0)
return (ETIMEDOUT);
/* Sensor type */
switch (edata->units) {
case ENVSYS_INDICATOR:
if (i < sc->sc_num_alarm)
lom_refresh_alarm(sc, edata, i);
else
lom_refresh_psu(sc, edata,
i - sc->sc_num_alarm - sc->sc_num_fan);
break;
case ENVSYS_SFANRPM:
lom_refresh_fan(sc, edata, i - sc->sc_num_alarm);
break;
case ENVSYS_STEMP:
lom_refresh_temp(sc, edata,
i - sc->sc_num_alarm - sc->sc_num_fan - sc->sc_num_psu);
break;
default:
edata->state = ENVSYS_SINVALID;
break;
}
/*
* If our hostname is set and differs from what's stored in
* the LOM, write the new hostname back to the LOM. Note that
* we include the terminating NUL when writing the hostname
* back to the LOM, otherwise the LOM will print any trailing
* garbage.
*/
if (i == 0 && hostnamelen > 0 &&
strncmp(sc->sc_hostname, hostname, sizeof(hostname)) != 0) {
if (sc->sc_type < LOM_LOMLITE2)
lom1_write_hostname(sc);
else
lom2_write_hostname(sc);
strlcpy(sc->sc_hostname, hostname, sizeof(hostname));
}
}
/* Fault LED or Alarms */
KASSERT(i < sc->sc_num_alarm);
/* Read new value at most once every second. */
if (ratecheck(&sc->sc_alarm_lastread, &refresh_interval)) {
if (lom_read(sc, LOM_IDX_ALARM, &val)) {
edata->state = ENVSYS_SINVALID;
return;
}
sc->sc_alarm_lastval = val;
} else {
val = sc->sc_alarm_lastval;
}
if (i == 0) {
/* Fault LED */
if ((val & LOM_ALARM_FAULT) == LOM_ALARM_FAULT)
edata->value_cur = 0;
else
edata->value_cur = 1;
} else {
/* Alarms */
if ((val & (LOM_ALARM_1 << (i - 1))) == 0)
edata->value_cur = 0;
else
edata->value_cur = 1;
}
edata->state = ENVSYS_SVALID;
}
/*
* LOMlite generally doesn't have enough space to store the
* fully qualified hostname. If the hostname is too long,
* strip off the domain name.
*/
strlcpy(name, hostname, sizeof(name));
if (hostnamelen >= sizeof(name)) {
p = strchr(name, '.');
if (p)
*p = '\0';
}
for (i = 0; i < strlen(name) + 1; i++)
if (lom_write(sc, LOM1_IDX_HOSTNAME1 + i, name[i]))
break;
}
static void
lom2_write_hostname(struct lom_softc *sc)
{
int i;
lom_write(sc, LOM2_IDX_HOSTNAMELEN, hostnamelen + 1);
for (i = 0; i < hostnamelen + 1; i++)
lom_write(sc, LOM2_IDX_HOSTNAME, hostname[i]);
}
static int
lom_sysctl_alarm(SYSCTLFN_ARGS)
{
struct sysctlnode node;
struct lom_softc *sc;
int i, tmp, error;
uint8_t val;
node = *rnode;
sc = node.sysctl_data;
for (i = 0; i < sc->sc_num_alarm; i++) {
if (node.sysctl_num == sc->sc_sysctl_num[i]) {
lom_refresh_alarm(sc, &sc->sc_alarm[i], i);
tmp = sc->sc_alarm[i].value_cur;
node.sysctl_data = &tmp;
error = sysctl_lookup(SYSCTLFN_CALL(&node));
if (error || newp == NULL)
return error;
if (tmp < 0 || tmp > 1)
return EINVAL;
if (lom_read(sc, LOM_IDX_ALARM, &val))
return EINVAL;
if (i == 0) {
/* Fault LED */
if (tmp != 0)
val &= ~LOM_ALARM_FAULT;
else
val |= LOM_ALARM_FAULT;
} else {
/* Alarms */
if (tmp != 0)
val |= LOM_ALARM_1 << (i - 1);
else
val &= ~(LOM_ALARM_1 << (i - 1));
}
if (lom_write(sc, LOM_IDX_ALARM, val))
return EINVAL;