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
10daeeb131SDaniel Scheller #include "ddbridge.h"
11daeeb131SDaniel Scheller #include "ddbridge-io.h"
12daeeb131SDaniel Scheller #include "ddbridge-mci.h"
13daeeb131SDaniel Scheller
14daeeb131SDaniel Scheller static LIST_HEAD(mci_list);
15daeeb131SDaniel Scheller
mci_reset(struct mci * state)16daeeb131SDaniel Scheller static int mci_reset(struct mci *state)
17daeeb131SDaniel Scheller {
18daeeb131SDaniel Scheller struct ddb_link *link = state->base->link;
19daeeb131SDaniel Scheller u32 status = 0;
20daeeb131SDaniel Scheller u32 timeout = 40;
21daeeb131SDaniel Scheller
22daeeb131SDaniel Scheller ddblwritel(link, MCI_CONTROL_RESET, MCI_CONTROL);
23daeeb131SDaniel Scheller ddblwritel(link, 0, MCI_CONTROL + 4); /* 1= no internal init */
24daeeb131SDaniel Scheller msleep(300);
25daeeb131SDaniel Scheller ddblwritel(link, 0, MCI_CONTROL);
26daeeb131SDaniel Scheller
27daeeb131SDaniel Scheller while (1) {
28daeeb131SDaniel Scheller status = ddblreadl(link, MCI_CONTROL);
29daeeb131SDaniel Scheller if ((status & MCI_CONTROL_READY) == MCI_CONTROL_READY)
30daeeb131SDaniel Scheller break;
31daeeb131SDaniel Scheller if (--timeout == 0)
32daeeb131SDaniel Scheller break;
33daeeb131SDaniel Scheller msleep(50);
34daeeb131SDaniel Scheller }
35daeeb131SDaniel Scheller if ((status & MCI_CONTROL_READY) == 0)
36daeeb131SDaniel Scheller return -1;
37daeeb131SDaniel Scheller if (link->ids.device == 0x0009)
38daeeb131SDaniel Scheller ddblwritel(link, SX8_TSCONFIG_MODE_NORMAL, SX8_TSCONFIG);
39daeeb131SDaniel Scheller return 0;
40daeeb131SDaniel Scheller }
41daeeb131SDaniel Scheller
ddb_mci_config(struct mci * state,u32 config)4284409a95SDaniel Scheller int ddb_mci_config(struct mci *state, u32 config)
43daeeb131SDaniel Scheller {
44daeeb131SDaniel Scheller struct ddb_link *link = state->base->link;
45daeeb131SDaniel Scheller
46daeeb131SDaniel Scheller if (link->ids.device != 0x0009)
47daeeb131SDaniel Scheller return -EINVAL;
48daeeb131SDaniel Scheller ddblwritel(link, config, SX8_TSCONFIG);
49daeeb131SDaniel Scheller return 0;
50daeeb131SDaniel Scheller }
51daeeb131SDaniel Scheller
_mci_cmd_unlocked(struct mci * state,u32 * cmd,u32 cmd_len,u32 * res,u32 res_len)52daeeb131SDaniel Scheller static int _mci_cmd_unlocked(struct mci *state,
53daeeb131SDaniel Scheller u32 *cmd, u32 cmd_len,
54daeeb131SDaniel Scheller u32 *res, u32 res_len)
55daeeb131SDaniel Scheller {
56daeeb131SDaniel Scheller struct ddb_link *link = state->base->link;
57daeeb131SDaniel Scheller u32 i, val;
58daeeb131SDaniel Scheller unsigned long stat;
59daeeb131SDaniel Scheller
60daeeb131SDaniel Scheller val = ddblreadl(link, MCI_CONTROL);
61daeeb131SDaniel Scheller if (val & (MCI_CONTROL_RESET | MCI_CONTROL_START_COMMAND))
62daeeb131SDaniel Scheller return -EIO;
63daeeb131SDaniel Scheller if (cmd && cmd_len)
64daeeb131SDaniel Scheller for (i = 0; i < cmd_len; i++)
65daeeb131SDaniel Scheller ddblwritel(link, cmd[i], MCI_COMMAND + i * 4);
66daeeb131SDaniel Scheller val |= (MCI_CONTROL_START_COMMAND | MCI_CONTROL_ENABLE_DONE_INTERRUPT);
67daeeb131SDaniel Scheller ddblwritel(link, val, MCI_CONTROL);
68daeeb131SDaniel Scheller
69daeeb131SDaniel Scheller stat = wait_for_completion_timeout(&state->base->completion, HZ);
70daeeb131SDaniel Scheller if (stat == 0) {
71daeeb131SDaniel Scheller dev_warn(state->base->dev, "MCI-%d: MCI timeout\n", state->nr);
72daeeb131SDaniel Scheller return -EIO;
73daeeb131SDaniel Scheller }
74daeeb131SDaniel Scheller if (res && res_len)
75daeeb131SDaniel Scheller for (i = 0; i < res_len; i++)
76daeeb131SDaniel Scheller res[i] = ddblreadl(link, MCI_RESULT + i * 4);
77daeeb131SDaniel Scheller return 0;
78daeeb131SDaniel Scheller }
79daeeb131SDaniel Scheller
ddb_mci_cmd(struct mci * state,struct mci_command * command,struct mci_result * result)8084409a95SDaniel Scheller int ddb_mci_cmd(struct mci *state,
81daeeb131SDaniel Scheller struct mci_command *command,
82daeeb131SDaniel Scheller struct mci_result *result)
83daeeb131SDaniel Scheller {
84daeeb131SDaniel Scheller int stat;
85daeeb131SDaniel Scheller
86daeeb131SDaniel Scheller mutex_lock(&state->base->mci_lock);
87daeeb131SDaniel Scheller stat = _mci_cmd_unlocked(state,
88daeeb131SDaniel Scheller (u32 *)command, sizeof(*command) / sizeof(u32),
89daeeb131SDaniel Scheller (u32 *)result, sizeof(*result) / sizeof(u32));
90daeeb131SDaniel Scheller mutex_unlock(&state->base->mci_lock);
91daeeb131SDaniel Scheller return stat;
92daeeb131SDaniel Scheller }
93daeeb131SDaniel Scheller
mci_handler(void * priv)94daeeb131SDaniel Scheller static void mci_handler(void *priv)
95daeeb131SDaniel Scheller {
96daeeb131SDaniel Scheller struct mci_base *base = (struct mci_base *)priv;
97daeeb131SDaniel Scheller
98daeeb131SDaniel Scheller complete(&base->completion);
99daeeb131SDaniel Scheller }
100daeeb131SDaniel Scheller
match_base(void * key)101daeeb131SDaniel Scheller static struct mci_base *match_base(void *key)
102daeeb131SDaniel Scheller {
103daeeb131SDaniel Scheller struct mci_base *p;
104daeeb131SDaniel Scheller
105daeeb131SDaniel Scheller list_for_each_entry(p, &mci_list, mci_list)
106daeeb131SDaniel Scheller if (p->key == key)
107daeeb131SDaniel Scheller return p;
108daeeb131SDaniel Scheller return NULL;
109daeeb131SDaniel Scheller }
110daeeb131SDaniel Scheller
probe(struct mci * state)111daeeb131SDaniel Scheller static int probe(struct mci *state)
112daeeb131SDaniel Scheller {
113daeeb131SDaniel Scheller mci_reset(state);
114daeeb131SDaniel Scheller return 0;
115daeeb131SDaniel Scheller }
116daeeb131SDaniel Scheller
117daeeb131SDaniel Scheller struct dvb_frontend
ddb_mci_attach(struct ddb_input * input,struct mci_cfg * cfg,int nr,int (** fn_set_input)(struct dvb_frontend * fe,int input))118*e5526848SDaniel Scheller *ddb_mci_attach(struct ddb_input *input, struct mci_cfg *cfg, int nr,
119caf2827cSDaniel Scheller int (**fn_set_input)(struct dvb_frontend *fe, int input))
120daeeb131SDaniel Scheller {
121daeeb131SDaniel Scheller struct ddb_port *port = input->port;
122daeeb131SDaniel Scheller struct ddb *dev = port->dev;
123daeeb131SDaniel Scheller struct ddb_link *link = &dev->link[port->lnr];
124daeeb131SDaniel Scheller struct mci_base *base;
125daeeb131SDaniel Scheller struct mci *state;
126*e5526848SDaniel Scheller void *key = cfg->type ? (void *)port : (void *)link;
127daeeb131SDaniel Scheller
128*e5526848SDaniel Scheller state = kzalloc(cfg->state_size, GFP_KERNEL);
129daeeb131SDaniel Scheller if (!state)
130daeeb131SDaniel Scheller return NULL;
131daeeb131SDaniel Scheller
132daeeb131SDaniel Scheller base = match_base(key);
133daeeb131SDaniel Scheller if (base) {
134daeeb131SDaniel Scheller base->count++;
135daeeb131SDaniel Scheller state->base = base;
136daeeb131SDaniel Scheller } else {
137*e5526848SDaniel Scheller base = kzalloc(cfg->base_size, GFP_KERNEL);
138daeeb131SDaniel Scheller if (!base)
139daeeb131SDaniel Scheller goto fail;
140daeeb131SDaniel Scheller base->key = key;
141daeeb131SDaniel Scheller base->count = 1;
142daeeb131SDaniel Scheller base->link = link;
143daeeb131SDaniel Scheller base->dev = dev->dev;
144daeeb131SDaniel Scheller mutex_init(&base->mci_lock);
145daeeb131SDaniel Scheller mutex_init(&base->tuner_lock);
146daeeb131SDaniel Scheller ddb_irq_set(dev, link->nr, 0, mci_handler, base);
147daeeb131SDaniel Scheller init_completion(&base->completion);
148daeeb131SDaniel Scheller state->base = base;
149daeeb131SDaniel Scheller if (probe(state) < 0) {
150daeeb131SDaniel Scheller kfree(base);
151daeeb131SDaniel Scheller goto fail;
152daeeb131SDaniel Scheller }
153daeeb131SDaniel Scheller list_add(&base->mci_list, &mci_list);
154*e5526848SDaniel Scheller if (cfg->base_init)
155*e5526848SDaniel Scheller cfg->base_init(base);
156daeeb131SDaniel Scheller }
157*e5526848SDaniel Scheller memcpy(&state->fe.ops, cfg->fe_ops, sizeof(struct dvb_frontend_ops));
158daeeb131SDaniel Scheller state->fe.demodulator_priv = state;
159daeeb131SDaniel Scheller state->nr = nr;
160*e5526848SDaniel Scheller *fn_set_input = cfg->set_input;
161daeeb131SDaniel Scheller state->tuner = nr;
162daeeb131SDaniel Scheller state->demod = nr;
163*e5526848SDaniel Scheller if (cfg->init)
164*e5526848SDaniel Scheller cfg->init(state);
165daeeb131SDaniel Scheller return &state->fe;
166daeeb131SDaniel Scheller fail:
167daeeb131SDaniel Scheller kfree(state);
168daeeb131SDaniel Scheller return NULL;
169daeeb131SDaniel Scheller }
170