1daeeb131SDaniel Scheller // SPDX-License-Identifier: GPL-2.0 2daeeb131SDaniel Scheller /* 3daeeb131SDaniel Scheller * ddbridge-mci.c: Digital Devices microcode interface 4daeeb131SDaniel Scheller * 53addf0faSDaniel Scheller * Copyright (C) 2017-2018 Digital Devices GmbH 6daeeb131SDaniel Scheller * Ralph Metzler <rjkm@metzlerbros.de> 7daeeb131SDaniel Scheller * Marcus Metzler <mocm@metzlerbros.de> 8daeeb131SDaniel Scheller * 9daeeb131SDaniel Scheller * This program is free software; you can redistribute it and/or 10daeeb131SDaniel Scheller * modify it under the terms of the GNU General Public License 11daeeb131SDaniel Scheller * version 2 only, as published by the Free Software Foundation. 12daeeb131SDaniel Scheller * 13daeeb131SDaniel Scheller * This program is distributed in the hope that it will be useful, 14daeeb131SDaniel Scheller * but WITHOUT ANY WARRANTY; without even the implied warranty of 15daeeb131SDaniel Scheller * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16daeeb131SDaniel Scheller * GNU General Public License for more details. 17daeeb131SDaniel Scheller */ 18daeeb131SDaniel Scheller 19daeeb131SDaniel Scheller #include "ddbridge.h" 20daeeb131SDaniel Scheller #include "ddbridge-io.h" 21daeeb131SDaniel Scheller #include "ddbridge-mci.h" 22daeeb131SDaniel Scheller 23daeeb131SDaniel Scheller static LIST_HEAD(mci_list); 24daeeb131SDaniel Scheller 25daeeb131SDaniel Scheller static int mci_reset(struct mci *state) 26daeeb131SDaniel Scheller { 27daeeb131SDaniel Scheller struct ddb_link *link = state->base->link; 28daeeb131SDaniel Scheller u32 status = 0; 29daeeb131SDaniel Scheller u32 timeout = 40; 30daeeb131SDaniel Scheller 31daeeb131SDaniel Scheller ddblwritel(link, MCI_CONTROL_RESET, MCI_CONTROL); 32daeeb131SDaniel Scheller ddblwritel(link, 0, MCI_CONTROL + 4); /* 1= no internal init */ 33daeeb131SDaniel Scheller msleep(300); 34daeeb131SDaniel Scheller ddblwritel(link, 0, MCI_CONTROL); 35daeeb131SDaniel Scheller 36daeeb131SDaniel Scheller while (1) { 37daeeb131SDaniel Scheller status = ddblreadl(link, MCI_CONTROL); 38daeeb131SDaniel Scheller if ((status & MCI_CONTROL_READY) == MCI_CONTROL_READY) 39daeeb131SDaniel Scheller break; 40daeeb131SDaniel Scheller if (--timeout == 0) 41daeeb131SDaniel Scheller break; 42daeeb131SDaniel Scheller msleep(50); 43daeeb131SDaniel Scheller } 44daeeb131SDaniel Scheller if ((status & MCI_CONTROL_READY) == 0) 45daeeb131SDaniel Scheller return -1; 46daeeb131SDaniel Scheller if (link->ids.device == 0x0009) 47daeeb131SDaniel Scheller ddblwritel(link, SX8_TSCONFIG_MODE_NORMAL, SX8_TSCONFIG); 48daeeb131SDaniel Scheller return 0; 49daeeb131SDaniel Scheller } 50daeeb131SDaniel Scheller 5184409a95SDaniel Scheller int ddb_mci_config(struct mci *state, u32 config) 52daeeb131SDaniel Scheller { 53daeeb131SDaniel Scheller struct ddb_link *link = state->base->link; 54daeeb131SDaniel Scheller 55daeeb131SDaniel Scheller if (link->ids.device != 0x0009) 56daeeb131SDaniel Scheller return -EINVAL; 57daeeb131SDaniel Scheller ddblwritel(link, config, SX8_TSCONFIG); 58daeeb131SDaniel Scheller return 0; 59daeeb131SDaniel Scheller } 60daeeb131SDaniel Scheller 61daeeb131SDaniel Scheller static int _mci_cmd_unlocked(struct mci *state, 62daeeb131SDaniel Scheller u32 *cmd, u32 cmd_len, 63daeeb131SDaniel Scheller u32 *res, u32 res_len) 64daeeb131SDaniel Scheller { 65daeeb131SDaniel Scheller struct ddb_link *link = state->base->link; 66daeeb131SDaniel Scheller u32 i, val; 67daeeb131SDaniel Scheller unsigned long stat; 68daeeb131SDaniel Scheller 69daeeb131SDaniel Scheller val = ddblreadl(link, MCI_CONTROL); 70daeeb131SDaniel Scheller if (val & (MCI_CONTROL_RESET | MCI_CONTROL_START_COMMAND)) 71daeeb131SDaniel Scheller return -EIO; 72daeeb131SDaniel Scheller if (cmd && cmd_len) 73daeeb131SDaniel Scheller for (i = 0; i < cmd_len; i++) 74daeeb131SDaniel Scheller ddblwritel(link, cmd[i], MCI_COMMAND + i * 4); 75daeeb131SDaniel Scheller val |= (MCI_CONTROL_START_COMMAND | MCI_CONTROL_ENABLE_DONE_INTERRUPT); 76daeeb131SDaniel Scheller ddblwritel(link, val, MCI_CONTROL); 77daeeb131SDaniel Scheller 78daeeb131SDaniel Scheller stat = wait_for_completion_timeout(&state->base->completion, HZ); 79daeeb131SDaniel Scheller if (stat == 0) { 80daeeb131SDaniel Scheller dev_warn(state->base->dev, "MCI-%d: MCI timeout\n", state->nr); 81daeeb131SDaniel Scheller return -EIO; 82daeeb131SDaniel Scheller } 83daeeb131SDaniel Scheller if (res && res_len) 84daeeb131SDaniel Scheller for (i = 0; i < res_len; i++) 85daeeb131SDaniel Scheller res[i] = ddblreadl(link, MCI_RESULT + i * 4); 86daeeb131SDaniel Scheller return 0; 87daeeb131SDaniel Scheller } 88daeeb131SDaniel Scheller 8984409a95SDaniel Scheller int ddb_mci_cmd(struct mci *state, 90daeeb131SDaniel Scheller struct mci_command *command, 91daeeb131SDaniel Scheller struct mci_result *result) 92daeeb131SDaniel Scheller { 93daeeb131SDaniel Scheller int stat; 94daeeb131SDaniel Scheller 95daeeb131SDaniel Scheller mutex_lock(&state->base->mci_lock); 96daeeb131SDaniel Scheller stat = _mci_cmd_unlocked(state, 97daeeb131SDaniel Scheller (u32 *)command, sizeof(*command) / sizeof(u32), 98daeeb131SDaniel Scheller (u32 *)result, sizeof(*result) / sizeof(u32)); 99daeeb131SDaniel Scheller mutex_unlock(&state->base->mci_lock); 100daeeb131SDaniel Scheller return stat; 101daeeb131SDaniel Scheller } 102daeeb131SDaniel Scheller 103daeeb131SDaniel Scheller static void mci_handler(void *priv) 104daeeb131SDaniel Scheller { 105daeeb131SDaniel Scheller struct mci_base *base = (struct mci_base *)priv; 106daeeb131SDaniel Scheller 107daeeb131SDaniel Scheller complete(&base->completion); 108daeeb131SDaniel Scheller } 109daeeb131SDaniel Scheller 110daeeb131SDaniel Scheller static struct mci_base *match_base(void *key) 111daeeb131SDaniel Scheller { 112daeeb131SDaniel Scheller struct mci_base *p; 113daeeb131SDaniel Scheller 114daeeb131SDaniel Scheller list_for_each_entry(p, &mci_list, mci_list) 115daeeb131SDaniel Scheller if (p->key == key) 116daeeb131SDaniel Scheller return p; 117daeeb131SDaniel Scheller return NULL; 118daeeb131SDaniel Scheller } 119daeeb131SDaniel Scheller 120daeeb131SDaniel Scheller static int probe(struct mci *state) 121daeeb131SDaniel Scheller { 122daeeb131SDaniel Scheller mci_reset(state); 123daeeb131SDaniel Scheller return 0; 124daeeb131SDaniel Scheller } 125daeeb131SDaniel Scheller 126daeeb131SDaniel Scheller struct dvb_frontend 127*e5526848SDaniel Scheller *ddb_mci_attach(struct ddb_input *input, struct mci_cfg *cfg, int nr, 128caf2827cSDaniel Scheller int (**fn_set_input)(struct dvb_frontend *fe, int input)) 129daeeb131SDaniel Scheller { 130daeeb131SDaniel Scheller struct ddb_port *port = input->port; 131daeeb131SDaniel Scheller struct ddb *dev = port->dev; 132daeeb131SDaniel Scheller struct ddb_link *link = &dev->link[port->lnr]; 133daeeb131SDaniel Scheller struct mci_base *base; 134daeeb131SDaniel Scheller struct mci *state; 135*e5526848SDaniel Scheller void *key = cfg->type ? (void *)port : (void *)link; 136daeeb131SDaniel Scheller 137*e5526848SDaniel Scheller state = kzalloc(cfg->state_size, GFP_KERNEL); 138daeeb131SDaniel Scheller if (!state) 139daeeb131SDaniel Scheller return NULL; 140daeeb131SDaniel Scheller 141daeeb131SDaniel Scheller base = match_base(key); 142daeeb131SDaniel Scheller if (base) { 143daeeb131SDaniel Scheller base->count++; 144daeeb131SDaniel Scheller state->base = base; 145daeeb131SDaniel Scheller } else { 146*e5526848SDaniel Scheller base = kzalloc(cfg->base_size, GFP_KERNEL); 147daeeb131SDaniel Scheller if (!base) 148daeeb131SDaniel Scheller goto fail; 149daeeb131SDaniel Scheller base->key = key; 150daeeb131SDaniel Scheller base->count = 1; 151daeeb131SDaniel Scheller base->link = link; 152daeeb131SDaniel Scheller base->dev = dev->dev; 153daeeb131SDaniel Scheller mutex_init(&base->mci_lock); 154daeeb131SDaniel Scheller mutex_init(&base->tuner_lock); 155daeeb131SDaniel Scheller ddb_irq_set(dev, link->nr, 0, mci_handler, base); 156daeeb131SDaniel Scheller init_completion(&base->completion); 157daeeb131SDaniel Scheller state->base = base; 158daeeb131SDaniel Scheller if (probe(state) < 0) { 159daeeb131SDaniel Scheller kfree(base); 160daeeb131SDaniel Scheller goto fail; 161daeeb131SDaniel Scheller } 162daeeb131SDaniel Scheller list_add(&base->mci_list, &mci_list); 163*e5526848SDaniel Scheller if (cfg->base_init) 164*e5526848SDaniel Scheller cfg->base_init(base); 165daeeb131SDaniel Scheller } 166*e5526848SDaniel Scheller memcpy(&state->fe.ops, cfg->fe_ops, sizeof(struct dvb_frontend_ops)); 167daeeb131SDaniel Scheller state->fe.demodulator_priv = state; 168daeeb131SDaniel Scheller state->nr = nr; 169*e5526848SDaniel Scheller *fn_set_input = cfg->set_input; 170daeeb131SDaniel Scheller state->tuner = nr; 171daeeb131SDaniel Scheller state->demod = nr; 172*e5526848SDaniel Scheller if (cfg->init) 173*e5526848SDaniel Scheller cfg->init(state); 174daeeb131SDaniel Scheller return &state->fe; 175daeeb131SDaniel Scheller fail: 176daeeb131SDaniel Scheller kfree(state); 177daeeb131SDaniel Scheller return NULL; 178daeeb131SDaniel Scheller } 179