blob: f62f0c482cea593c274a1184433378c9c63a02d7 [file] [log] [blame]
Sebastian Reichelb209e042013-12-15 23:38:58 +01001/* OMAP SSI port driver.
2 *
3 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
4 * Copyright (C) 2014 Sebastian Reichel <sre@kernel.org>
5 *
6 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#include <linux/platform_device.h>
24#include <linux/dma-mapping.h>
25#include <linux/pm_runtime.h>
Sebastian Reichel4bcf7412016-01-31 01:52:38 +010026#include <linux/delay.h>
Sebastian Reichelb209e042013-12-15 23:38:58 +010027
Sebastian Reichel73e6ce02016-04-30 00:04:29 +020028#include <linux/gpio/consumer.h>
Arnd Bergmannac8e3ff2016-05-03 17:16:21 +020029#include <linux/pinctrl/consumer.h>
Sebastian Reichelb209e042013-12-15 23:38:58 +010030#include <linux/debugfs.h>
31
32#include "omap_ssi_regs.h"
33#include "omap_ssi.h"
34
35static inline int hsi_dummy_msg(struct hsi_msg *msg __maybe_unused)
36{
37 return 0;
38}
39
40static inline int hsi_dummy_cl(struct hsi_client *cl __maybe_unused)
41{
42 return 0;
43}
44
45static inline unsigned int ssi_wakein(struct hsi_port *port)
46{
47 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
Sebastian Reichel73e6ce02016-04-30 00:04:29 +020048 return gpiod_get_value(omap_port->wake_gpio);
Sebastian Reichelb209e042013-12-15 23:38:58 +010049}
50
51#ifdef CONFIG_DEBUG_FS
52static void ssi_debug_remove_port(struct hsi_port *port)
53{
54 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
55
56 debugfs_remove_recursive(omap_port->dir);
57}
58
59static int ssi_debug_port_show(struct seq_file *m, void *p __maybe_unused)
60{
61 struct hsi_port *port = m->private;
62 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
63 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
64 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
65 void __iomem *base = omap_ssi->sys;
66 unsigned int ch;
67
68 pm_runtime_get_sync(omap_port->pdev);
69 if (omap_port->wake_irq > 0)
70 seq_printf(m, "CAWAKE\t\t: %d\n", ssi_wakein(port));
71 seq_printf(m, "WAKE\t\t: 0x%08x\n",
72 readl(base + SSI_WAKE_REG(port->num)));
73 seq_printf(m, "MPU_ENABLE_IRQ%d\t: 0x%08x\n", 0,
74 readl(base + SSI_MPU_ENABLE_REG(port->num, 0)));
75 seq_printf(m, "MPU_STATUS_IRQ%d\t: 0x%08x\n", 0,
76 readl(base + SSI_MPU_STATUS_REG(port->num, 0)));
77 /* SST */
78 base = omap_port->sst_base;
79 seq_puts(m, "\nSST\n===\n");
80 seq_printf(m, "ID SST\t\t: 0x%08x\n",
81 readl(base + SSI_SST_ID_REG));
82 seq_printf(m, "MODE\t\t: 0x%08x\n",
83 readl(base + SSI_SST_MODE_REG));
84 seq_printf(m, "FRAMESIZE\t: 0x%08x\n",
85 readl(base + SSI_SST_FRAMESIZE_REG));
86 seq_printf(m, "DIVISOR\t\t: 0x%08x\n",
87 readl(base + SSI_SST_DIVISOR_REG));
88 seq_printf(m, "CHANNELS\t: 0x%08x\n",
89 readl(base + SSI_SST_CHANNELS_REG));
90 seq_printf(m, "ARBMODE\t\t: 0x%08x\n",
91 readl(base + SSI_SST_ARBMODE_REG));
92 seq_printf(m, "TXSTATE\t\t: 0x%08x\n",
93 readl(base + SSI_SST_TXSTATE_REG));
94 seq_printf(m, "BUFSTATE\t: 0x%08x\n",
95 readl(base + SSI_SST_BUFSTATE_REG));
96 seq_printf(m, "BREAK\t\t: 0x%08x\n",
97 readl(base + SSI_SST_BREAK_REG));
98 for (ch = 0; ch < omap_port->channels; ch++) {
99 seq_printf(m, "BUFFER_CH%d\t: 0x%08x\n", ch,
100 readl(base + SSI_SST_BUFFER_CH_REG(ch)));
101 }
102 /* SSR */
103 base = omap_port->ssr_base;
104 seq_puts(m, "\nSSR\n===\n");
105 seq_printf(m, "ID SSR\t\t: 0x%08x\n",
106 readl(base + SSI_SSR_ID_REG));
107 seq_printf(m, "MODE\t\t: 0x%08x\n",
108 readl(base + SSI_SSR_MODE_REG));
109 seq_printf(m, "FRAMESIZE\t: 0x%08x\n",
110 readl(base + SSI_SSR_FRAMESIZE_REG));
111 seq_printf(m, "CHANNELS\t: 0x%08x\n",
112 readl(base + SSI_SSR_CHANNELS_REG));
113 seq_printf(m, "TIMEOUT\t\t: 0x%08x\n",
114 readl(base + SSI_SSR_TIMEOUT_REG));
115 seq_printf(m, "RXSTATE\t\t: 0x%08x\n",
116 readl(base + SSI_SSR_RXSTATE_REG));
117 seq_printf(m, "BUFSTATE\t: 0x%08x\n",
118 readl(base + SSI_SSR_BUFSTATE_REG));
119 seq_printf(m, "BREAK\t\t: 0x%08x\n",
120 readl(base + SSI_SSR_BREAK_REG));
121 seq_printf(m, "ERROR\t\t: 0x%08x\n",
122 readl(base + SSI_SSR_ERROR_REG));
123 seq_printf(m, "ERRORACK\t: 0x%08x\n",
124 readl(base + SSI_SSR_ERRORACK_REG));
125 for (ch = 0; ch < omap_port->channels; ch++) {
126 seq_printf(m, "BUFFER_CH%d\t: 0x%08x\n", ch,
127 readl(base + SSI_SSR_BUFFER_CH_REG(ch)));
128 }
129 pm_runtime_put_sync(omap_port->pdev);
130
131 return 0;
132}
133
134static int ssi_port_regs_open(struct inode *inode, struct file *file)
135{
136 return single_open(file, ssi_debug_port_show, inode->i_private);
137}
138
139static const struct file_operations ssi_port_regs_fops = {
140 .open = ssi_port_regs_open,
141 .read = seq_read,
142 .llseek = seq_lseek,
143 .release = single_release,
144};
145
146static int ssi_div_get(void *data, u64 *val)
147{
148 struct hsi_port *port = data;
149 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
150
151 pm_runtime_get_sync(omap_port->pdev);
152 *val = readl(omap_port->sst_base + SSI_SST_DIVISOR_REG);
153 pm_runtime_put_sync(omap_port->pdev);
154
155 return 0;
156}
157
158static int ssi_div_set(void *data, u64 val)
159{
160 struct hsi_port *port = data;
161 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
162
163 if (val > 127)
164 return -EINVAL;
165
166 pm_runtime_get_sync(omap_port->pdev);
167 writel(val, omap_port->sst_base + SSI_SST_DIVISOR_REG);
168 omap_port->sst.divisor = val;
169 pm_runtime_put_sync(omap_port->pdev);
170
171 return 0;
172}
173
174DEFINE_SIMPLE_ATTRIBUTE(ssi_sst_div_fops, ssi_div_get, ssi_div_set, "%llu\n");
175
Sebastian Reichel0845e1f22016-04-30 01:01:06 +0200176static int ssi_debug_add_port(struct omap_ssi_port *omap_port,
Sebastian Reichelb209e042013-12-15 23:38:58 +0100177 struct dentry *dir)
178{
179 struct hsi_port *port = to_hsi_port(omap_port->dev);
180
181 dir = debugfs_create_dir(dev_name(omap_port->dev), dir);
Wei Yongjunc2acb7c2014-07-30 08:53:20 +0800182 if (!dir)
183 return -ENOMEM;
Sebastian Reichelb209e042013-12-15 23:38:58 +0100184 omap_port->dir = dir;
185 debugfs_create_file("regs", S_IRUGO, dir, port, &ssi_port_regs_fops);
186 dir = debugfs_create_dir("sst", dir);
Wei Yongjunc2acb7c2014-07-30 08:53:20 +0800187 if (!dir)
188 return -ENOMEM;
Sebastian Reichelb209e042013-12-15 23:38:58 +0100189 debugfs_create_file("divisor", S_IRUGO | S_IWUSR, dir, port,
190 &ssi_sst_div_fops);
191
192 return 0;
193}
194#endif
195
196static int ssi_claim_lch(struct hsi_msg *msg)
197{
198
199 struct hsi_port *port = hsi_get_port(msg->cl);
200 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
201 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
202 int lch;
203
204 for (lch = 0; lch < SSI_MAX_GDD_LCH; lch++)
205 if (!omap_ssi->gdd_trn[lch].msg) {
206 omap_ssi->gdd_trn[lch].msg = msg;
207 omap_ssi->gdd_trn[lch].sg = msg->sgt.sgl;
208 return lch;
209 }
210
211 return -EBUSY;
212}
213
214static int ssi_start_dma(struct hsi_msg *msg, int lch)
215{
216 struct hsi_port *port = hsi_get_port(msg->cl);
217 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
218 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
219 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
220 void __iomem *gdd = omap_ssi->gdd;
221 int err;
222 u16 csdp;
223 u16 ccr;
224 u32 s_addr;
225 u32 d_addr;
226 u32 tmp;
227
228 if (msg->ttype == HSI_MSG_READ) {
229 err = dma_map_sg(&ssi->device, msg->sgt.sgl, msg->sgt.nents,
230 DMA_FROM_DEVICE);
231 if (err < 0) {
232 dev_dbg(&ssi->device, "DMA map SG failed !\n");
233 return err;
234 }
235 csdp = SSI_DST_BURST_4x32_BIT | SSI_DST_MEMORY_PORT |
236 SSI_SRC_SINGLE_ACCESS0 | SSI_SRC_PERIPHERAL_PORT |
237 SSI_DATA_TYPE_S32;
238 ccr = msg->channel + 0x10 + (port->num * 8); /* Sync */
239 ccr |= SSI_DST_AMODE_POSTINC | SSI_SRC_AMODE_CONST |
240 SSI_CCR_ENABLE;
241 s_addr = omap_port->ssr_dma +
242 SSI_SSR_BUFFER_CH_REG(msg->channel);
243 d_addr = sg_dma_address(msg->sgt.sgl);
244 } else {
245 err = dma_map_sg(&ssi->device, msg->sgt.sgl, msg->sgt.nents,
246 DMA_TO_DEVICE);
247 if (err < 0) {
248 dev_dbg(&ssi->device, "DMA map SG failed !\n");
249 return err;
250 }
251 csdp = SSI_SRC_BURST_4x32_BIT | SSI_SRC_MEMORY_PORT |
252 SSI_DST_SINGLE_ACCESS0 | SSI_DST_PERIPHERAL_PORT |
253 SSI_DATA_TYPE_S32;
254 ccr = (msg->channel + 1 + (port->num * 8)) & 0xf; /* Sync */
255 ccr |= SSI_SRC_AMODE_POSTINC | SSI_DST_AMODE_CONST |
256 SSI_CCR_ENABLE;
257 s_addr = sg_dma_address(msg->sgt.sgl);
258 d_addr = omap_port->sst_dma +
259 SSI_SST_BUFFER_CH_REG(msg->channel);
260 }
261 dev_dbg(&ssi->device, "lch %d cdsp %08x ccr %04x s_addr %08x d_addr %08x\n",
262 lch, csdp, ccr, s_addr, d_addr);
263
264 /* Hold clocks during the transfer */
265 pm_runtime_get_sync(omap_port->pdev);
266
267 writew_relaxed(csdp, gdd + SSI_GDD_CSDP_REG(lch));
268 writew_relaxed(SSI_BLOCK_IE | SSI_TOUT_IE, gdd + SSI_GDD_CICR_REG(lch));
269 writel_relaxed(d_addr, gdd + SSI_GDD_CDSA_REG(lch));
270 writel_relaxed(s_addr, gdd + SSI_GDD_CSSA_REG(lch));
271 writew_relaxed(SSI_BYTES_TO_FRAMES(msg->sgt.sgl->length),
272 gdd + SSI_GDD_CEN_REG(lch));
273
274 spin_lock_bh(&omap_ssi->lock);
275 tmp = readl(omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
276 tmp |= SSI_GDD_LCH(lch);
277 writel_relaxed(tmp, omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
278 spin_unlock_bh(&omap_ssi->lock);
279 writew(ccr, gdd + SSI_GDD_CCR_REG(lch));
280 msg->status = HSI_STATUS_PROCEEDING;
281
282 return 0;
283}
284
285static int ssi_start_pio(struct hsi_msg *msg)
286{
287 struct hsi_port *port = hsi_get_port(msg->cl);
288 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
289 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
290 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
291 u32 val;
292
293 pm_runtime_get_sync(omap_port->pdev);
294 if (msg->ttype == HSI_MSG_WRITE) {
295 val = SSI_DATAACCEPT(msg->channel);
296 /* Hold clocks for pio writes */
297 pm_runtime_get_sync(omap_port->pdev);
298 } else {
299 val = SSI_DATAAVAILABLE(msg->channel) | SSI_ERROROCCURED;
300 }
301 dev_dbg(&port->device, "Single %s transfer\n",
302 msg->ttype ? "write" : "read");
303 val |= readl(omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
304 writel(val, omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
305 pm_runtime_put_sync(omap_port->pdev);
306 msg->actual_len = 0;
307 msg->status = HSI_STATUS_PROCEEDING;
308
309 return 0;
310}
311
312static int ssi_start_transfer(struct list_head *queue)
313{
314 struct hsi_msg *msg;
315 int lch = -1;
316
317 if (list_empty(queue))
318 return 0;
319 msg = list_first_entry(queue, struct hsi_msg, link);
320 if (msg->status != HSI_STATUS_QUEUED)
321 return 0;
322 if ((msg->sgt.nents) && (msg->sgt.sgl->length > sizeof(u32)))
323 lch = ssi_claim_lch(msg);
324 if (lch >= 0)
325 return ssi_start_dma(msg, lch);
326 else
327 return ssi_start_pio(msg);
328}
329
330static int ssi_async_break(struct hsi_msg *msg)
331{
332 struct hsi_port *port = hsi_get_port(msg->cl);
333 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
334 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
335 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
336 int err = 0;
337 u32 tmp;
338
339 pm_runtime_get_sync(omap_port->pdev);
340 if (msg->ttype == HSI_MSG_WRITE) {
341 if (omap_port->sst.mode != SSI_MODE_FRAME) {
342 err = -EINVAL;
343 goto out;
344 }
345 writel(1, omap_port->sst_base + SSI_SST_BREAK_REG);
346 msg->status = HSI_STATUS_COMPLETED;
347 msg->complete(msg);
348 } else {
349 if (omap_port->ssr.mode != SSI_MODE_FRAME) {
350 err = -EINVAL;
351 goto out;
352 }
353 spin_lock_bh(&omap_port->lock);
354 tmp = readl(omap_ssi->sys +
355 SSI_MPU_ENABLE_REG(port->num, 0));
356 writel(tmp | SSI_BREAKDETECTED,
357 omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
358 msg->status = HSI_STATUS_PROCEEDING;
359 list_add_tail(&msg->link, &omap_port->brkqueue);
360 spin_unlock_bh(&omap_port->lock);
361 }
362out:
363 pm_runtime_put_sync(omap_port->pdev);
364
365 return err;
366}
367
368static int ssi_async(struct hsi_msg *msg)
369{
370 struct hsi_port *port = hsi_get_port(msg->cl);
371 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
372 struct list_head *queue;
373 int err = 0;
374
375 BUG_ON(!msg);
376
377 if (msg->sgt.nents > 1)
378 return -ENOSYS; /* TODO: Add sg support */
379
380 if (msg->break_frame)
381 return ssi_async_break(msg);
382
383 if (msg->ttype) {
384 BUG_ON(msg->channel >= omap_port->sst.channels);
385 queue = &omap_port->txqueue[msg->channel];
386 } else {
387 BUG_ON(msg->channel >= omap_port->ssr.channels);
388 queue = &omap_port->rxqueue[msg->channel];
389 }
390 msg->status = HSI_STATUS_QUEUED;
391 spin_lock_bh(&omap_port->lock);
392 list_add_tail(&msg->link, queue);
393 err = ssi_start_transfer(queue);
394 if (err < 0) {
395 list_del(&msg->link);
396 msg->status = HSI_STATUS_ERROR;
397 }
398 spin_unlock_bh(&omap_port->lock);
399 dev_dbg(&port->device, "msg status %d ttype %d ch %d\n",
400 msg->status, msg->ttype, msg->channel);
401
402 return err;
403}
404
405static u32 ssi_calculate_div(struct hsi_controller *ssi)
406{
407 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
408 u32 tx_fckrate = (u32) omap_ssi->fck_rate;
409
410 /* / 2 : SSI TX clock is always half of the SSI functional clock */
411 tx_fckrate >>= 1;
412 /* Round down when tx_fckrate % omap_ssi->max_speed == 0 */
413 tx_fckrate--;
414 dev_dbg(&ssi->device, "TX div %d for fck_rate %lu Khz speed %d Kb/s\n",
415 tx_fckrate / omap_ssi->max_speed, omap_ssi->fck_rate,
416 omap_ssi->max_speed);
417
418 return tx_fckrate / omap_ssi->max_speed;
419}
420
421static void ssi_flush_queue(struct list_head *queue, struct hsi_client *cl)
422{
423 struct list_head *node, *tmp;
424 struct hsi_msg *msg;
425
426 list_for_each_safe(node, tmp, queue) {
427 msg = list_entry(node, struct hsi_msg, link);
428 if ((cl) && (cl != msg->cl))
429 continue;
430 list_del(node);
431 pr_debug("flush queue: ch %d, msg %p len %d type %d ctxt %p\n",
432 msg->channel, msg, msg->sgt.sgl->length,
433 msg->ttype, msg->context);
434 if (msg->destructor)
435 msg->destructor(msg);
436 else
437 hsi_free_msg(msg);
438 }
439}
440
441static int ssi_setup(struct hsi_client *cl)
442{
443 struct hsi_port *port = to_hsi_port(cl->device.parent);
444 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
445 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
446 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
447 void __iomem *sst = omap_port->sst_base;
448 void __iomem *ssr = omap_port->ssr_base;
449 u32 div;
450 u32 val;
451 int err = 0;
452
453 pm_runtime_get_sync(omap_port->pdev);
454 spin_lock_bh(&omap_port->lock);
455 if (cl->tx_cfg.speed)
456 omap_ssi->max_speed = cl->tx_cfg.speed;
457 div = ssi_calculate_div(ssi);
458 if (div > SSI_MAX_DIVISOR) {
459 dev_err(&cl->device, "Invalid TX speed %d Mb/s (div %d)\n",
460 cl->tx_cfg.speed, div);
461 err = -EINVAL;
462 goto out;
463 }
464 /* Set TX/RX module to sleep to stop TX/RX during cfg update */
465 writel_relaxed(SSI_MODE_SLEEP, sst + SSI_SST_MODE_REG);
466 writel_relaxed(SSI_MODE_SLEEP, ssr + SSI_SSR_MODE_REG);
467 /* Flush posted write */
468 val = readl(ssr + SSI_SSR_MODE_REG);
469 /* TX */
470 writel_relaxed(31, sst + SSI_SST_FRAMESIZE_REG);
471 writel_relaxed(div, sst + SSI_SST_DIVISOR_REG);
472 writel_relaxed(cl->tx_cfg.num_hw_channels, sst + SSI_SST_CHANNELS_REG);
473 writel_relaxed(cl->tx_cfg.arb_mode, sst + SSI_SST_ARBMODE_REG);
474 writel_relaxed(cl->tx_cfg.mode, sst + SSI_SST_MODE_REG);
475 /* RX */
476 writel_relaxed(31, ssr + SSI_SSR_FRAMESIZE_REG);
477 writel_relaxed(cl->rx_cfg.num_hw_channels, ssr + SSI_SSR_CHANNELS_REG);
478 writel_relaxed(0, ssr + SSI_SSR_TIMEOUT_REG);
479 /* Cleanup the break queue if we leave FRAME mode */
480 if ((omap_port->ssr.mode == SSI_MODE_FRAME) &&
481 (cl->rx_cfg.mode != SSI_MODE_FRAME))
482 ssi_flush_queue(&omap_port->brkqueue, cl);
483 writel_relaxed(cl->rx_cfg.mode, ssr + SSI_SSR_MODE_REG);
484 omap_port->channels = max(cl->rx_cfg.num_hw_channels,
485 cl->tx_cfg.num_hw_channels);
486 /* Shadow registering for OFF mode */
487 /* SST */
488 omap_port->sst.divisor = div;
489 omap_port->sst.frame_size = 31;
490 omap_port->sst.channels = cl->tx_cfg.num_hw_channels;
491 omap_port->sst.arb_mode = cl->tx_cfg.arb_mode;
492 omap_port->sst.mode = cl->tx_cfg.mode;
493 /* SSR */
494 omap_port->ssr.frame_size = 31;
495 omap_port->ssr.timeout = 0;
496 omap_port->ssr.channels = cl->rx_cfg.num_hw_channels;
497 omap_port->ssr.mode = cl->rx_cfg.mode;
498out:
499 spin_unlock_bh(&omap_port->lock);
500 pm_runtime_put_sync(omap_port->pdev);
501
502 return err;
503}
504
505static int ssi_flush(struct hsi_client *cl)
506{
507 struct hsi_port *port = hsi_get_port(cl);
508 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
509 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
510 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
511 struct hsi_msg *msg;
512 void __iomem *sst = omap_port->sst_base;
513 void __iomem *ssr = omap_port->ssr_base;
514 unsigned int i;
515 u32 err;
516
517 pm_runtime_get_sync(omap_port->pdev);
518 spin_lock_bh(&omap_port->lock);
Sebastian Reichel4bcf7412016-01-31 01:52:38 +0100519
520 /* stop all ssi communication */
521 pinctrl_pm_select_idle_state(omap_port->pdev);
522 udelay(1); /* wait for racing frames */
523
Sebastian Reichelb209e042013-12-15 23:38:58 +0100524 /* Stop all DMA transfers */
525 for (i = 0; i < SSI_MAX_GDD_LCH; i++) {
526 msg = omap_ssi->gdd_trn[i].msg;
527 if (!msg || (port != hsi_get_port(msg->cl)))
528 continue;
529 writew_relaxed(0, omap_ssi->gdd + SSI_GDD_CCR_REG(i));
530 if (msg->ttype == HSI_MSG_READ)
531 pm_runtime_put_sync(omap_port->pdev);
532 omap_ssi->gdd_trn[i].msg = NULL;
533 }
534 /* Flush all SST buffers */
535 writel_relaxed(0, sst + SSI_SST_BUFSTATE_REG);
536 writel_relaxed(0, sst + SSI_SST_TXSTATE_REG);
537 /* Flush all SSR buffers */
538 writel_relaxed(0, ssr + SSI_SSR_RXSTATE_REG);
539 writel_relaxed(0, ssr + SSI_SSR_BUFSTATE_REG);
540 /* Flush all errors */
541 err = readl(ssr + SSI_SSR_ERROR_REG);
542 writel_relaxed(err, ssr + SSI_SSR_ERRORACK_REG);
543 /* Flush break */
544 writel_relaxed(0, ssr + SSI_SSR_BREAK_REG);
545 /* Clear interrupts */
546 writel_relaxed(0, omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
547 writel_relaxed(0xffffff00,
548 omap_ssi->sys + SSI_MPU_STATUS_REG(port->num, 0));
549 writel_relaxed(0, omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
550 writel(0xff, omap_ssi->sys + SSI_GDD_MPU_IRQ_STATUS_REG);
551 /* Dequeue all pending requests */
552 for (i = 0; i < omap_port->channels; i++) {
553 /* Release write clocks */
554 if (!list_empty(&omap_port->txqueue[i]))
555 pm_runtime_put_sync(omap_port->pdev);
556 ssi_flush_queue(&omap_port->txqueue[i], NULL);
557 ssi_flush_queue(&omap_port->rxqueue[i], NULL);
558 }
559 ssi_flush_queue(&omap_port->brkqueue, NULL);
Sebastian Reichel4bcf7412016-01-31 01:52:38 +0100560
561 /* Resume SSI communication */
562 pinctrl_pm_select_default_state(omap_port->pdev);
563
Sebastian Reichelb209e042013-12-15 23:38:58 +0100564 spin_unlock_bh(&omap_port->lock);
565 pm_runtime_put_sync(omap_port->pdev);
566
567 return 0;
568}
569
Sebastian Reichel7c5d8162016-05-11 20:33:45 +0200570static void start_tx_work(struct work_struct *work)
571{
572 struct omap_ssi_port *omap_port =
573 container_of(work, struct omap_ssi_port, work);
574 struct hsi_port *port = to_hsi_port(omap_port->dev);
575 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
576 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
577
578 pm_runtime_get_sync(omap_port->pdev); /* Grab clocks */
579 writel(SSI_WAKE(0), omap_ssi->sys + SSI_SET_WAKE_REG(port->num));
580}
581
Sebastian Reichelb209e042013-12-15 23:38:58 +0100582static int ssi_start_tx(struct hsi_client *cl)
583{
584 struct hsi_port *port = hsi_get_port(cl);
585 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100586
587 dev_dbg(&port->device, "Wake out high %d\n", omap_port->wk_refcount);
588
589 spin_lock_bh(&omap_port->wk_lock);
590 if (omap_port->wk_refcount++) {
591 spin_unlock_bh(&omap_port->wk_lock);
592 return 0;
593 }
Sebastian Reichelb209e042013-12-15 23:38:58 +0100594 spin_unlock_bh(&omap_port->wk_lock);
595
Sebastian Reichel7c5d8162016-05-11 20:33:45 +0200596 schedule_work(&omap_port->work);
597
Sebastian Reichelb209e042013-12-15 23:38:58 +0100598 return 0;
599}
600
601static int ssi_stop_tx(struct hsi_client *cl)
602{
603 struct hsi_port *port = hsi_get_port(cl);
604 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
605 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
606 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
607
608 dev_dbg(&port->device, "Wake out low %d\n", omap_port->wk_refcount);
609
610 spin_lock_bh(&omap_port->wk_lock);
611 BUG_ON(!omap_port->wk_refcount);
612 if (--omap_port->wk_refcount) {
613 spin_unlock_bh(&omap_port->wk_lock);
614 return 0;
615 }
616 writel(SSI_WAKE(0), omap_ssi->sys + SSI_CLEAR_WAKE_REG(port->num));
Sebastian Reichelb209e042013-12-15 23:38:58 +0100617 spin_unlock_bh(&omap_port->wk_lock);
618
Sebastian Reichel7c5d8162016-05-11 20:33:45 +0200619 pm_runtime_put(omap_port->pdev); /* Release clocks */
620
Sebastian Reichelb209e042013-12-15 23:38:58 +0100621 return 0;
622}
623
624static void ssi_transfer(struct omap_ssi_port *omap_port,
625 struct list_head *queue)
626{
627 struct hsi_msg *msg;
628 int err = -1;
629
630 spin_lock_bh(&omap_port->lock);
631 while (err < 0) {
632 err = ssi_start_transfer(queue);
633 if (err < 0) {
634 msg = list_first_entry(queue, struct hsi_msg, link);
635 msg->status = HSI_STATUS_ERROR;
636 msg->actual_len = 0;
637 list_del(&msg->link);
638 spin_unlock_bh(&omap_port->lock);
639 msg->complete(msg);
640 spin_lock_bh(&omap_port->lock);
641 }
642 }
643 spin_unlock_bh(&omap_port->lock);
644}
645
646static void ssi_cleanup_queues(struct hsi_client *cl)
647{
648 struct hsi_port *port = hsi_get_port(cl);
649 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
650 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
651 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
652 struct hsi_msg *msg;
653 unsigned int i;
654 u32 rxbufstate = 0;
655 u32 txbufstate = 0;
656 u32 status = SSI_ERROROCCURED;
657 u32 tmp;
658
659 ssi_flush_queue(&omap_port->brkqueue, cl);
660 if (list_empty(&omap_port->brkqueue))
661 status |= SSI_BREAKDETECTED;
662
663 for (i = 0; i < omap_port->channels; i++) {
664 if (list_empty(&omap_port->txqueue[i]))
665 continue;
666 msg = list_first_entry(&omap_port->txqueue[i], struct hsi_msg,
667 link);
668 if ((msg->cl == cl) && (msg->status == HSI_STATUS_PROCEEDING)) {
669 txbufstate |= (1 << i);
670 status |= SSI_DATAACCEPT(i);
671 /* Release the clocks writes, also GDD ones */
672 pm_runtime_put_sync(omap_port->pdev);
673 }
674 ssi_flush_queue(&omap_port->txqueue[i], cl);
675 }
676 for (i = 0; i < omap_port->channels; i++) {
677 if (list_empty(&omap_port->rxqueue[i]))
678 continue;
679 msg = list_first_entry(&omap_port->rxqueue[i], struct hsi_msg,
680 link);
681 if ((msg->cl == cl) && (msg->status == HSI_STATUS_PROCEEDING)) {
682 rxbufstate |= (1 << i);
683 status |= SSI_DATAAVAILABLE(i);
684 }
685 ssi_flush_queue(&omap_port->rxqueue[i], cl);
686 /* Check if we keep the error detection interrupt armed */
687 if (!list_empty(&omap_port->rxqueue[i]))
688 status &= ~SSI_ERROROCCURED;
689 }
690 /* Cleanup write buffers */
691 tmp = readl(omap_port->sst_base + SSI_SST_BUFSTATE_REG);
692 tmp &= ~txbufstate;
693 writel_relaxed(tmp, omap_port->sst_base + SSI_SST_BUFSTATE_REG);
694 /* Cleanup read buffers */
695 tmp = readl(omap_port->ssr_base + SSI_SSR_BUFSTATE_REG);
696 tmp &= ~rxbufstate;
697 writel_relaxed(tmp, omap_port->ssr_base + SSI_SSR_BUFSTATE_REG);
698 /* Disarm and ack pending interrupts */
699 tmp = readl(omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
700 tmp &= ~status;
701 writel_relaxed(tmp, omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
702 writel_relaxed(status, omap_ssi->sys +
703 SSI_MPU_STATUS_REG(port->num, 0));
704}
705
706static void ssi_cleanup_gdd(struct hsi_controller *ssi, struct hsi_client *cl)
707{
708 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
709 struct hsi_port *port = hsi_get_port(cl);
710 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
711 struct hsi_msg *msg;
712 unsigned int i;
713 u32 val = 0;
714 u32 tmp;
715
716 for (i = 0; i < SSI_MAX_GDD_LCH; i++) {
717 msg = omap_ssi->gdd_trn[i].msg;
718 if ((!msg) || (msg->cl != cl))
719 continue;
720 writew_relaxed(0, omap_ssi->gdd + SSI_GDD_CCR_REG(i));
721 val |= (1 << i);
722 /*
723 * Clock references for write will be handled in
724 * ssi_cleanup_queues
725 */
726 if (msg->ttype == HSI_MSG_READ)
727 pm_runtime_put_sync(omap_port->pdev);
728 omap_ssi->gdd_trn[i].msg = NULL;
729 }
730 tmp = readl_relaxed(omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
731 tmp &= ~val;
732 writel_relaxed(tmp, omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
733 writel(val, omap_ssi->sys + SSI_GDD_MPU_IRQ_STATUS_REG);
734}
735
736static int ssi_set_port_mode(struct omap_ssi_port *omap_port, u32 mode)
737{
738 writel(mode, omap_port->sst_base + SSI_SST_MODE_REG);
739 writel(mode, omap_port->ssr_base + SSI_SSR_MODE_REG);
740 /* OCP barrier */
741 mode = readl(omap_port->ssr_base + SSI_SSR_MODE_REG);
742
743 return 0;
744}
745
746static int ssi_release(struct hsi_client *cl)
747{
748 struct hsi_port *port = hsi_get_port(cl);
749 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
750 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
751
752 spin_lock_bh(&omap_port->lock);
753 pm_runtime_get_sync(omap_port->pdev);
754 /* Stop all the pending DMA requests for that client */
755 ssi_cleanup_gdd(ssi, cl);
756 /* Now cleanup all the queues */
757 ssi_cleanup_queues(cl);
758 pm_runtime_put_sync(omap_port->pdev);
759 /* If it is the last client of the port, do extra checks and cleanup */
760 if (port->claimed <= 1) {
761 /*
762 * Drop the clock reference for the incoming wake line
763 * if it is still kept high by the other side.
764 */
Sebastian Reichel20830572016-04-30 17:11:54 +0200765 if (test_and_clear_bit(SSI_WAKE_EN, &omap_port->flags))
Sebastian Reichelb209e042013-12-15 23:38:58 +0100766 pm_runtime_put_sync(omap_port->pdev);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100767 pm_runtime_get_sync(omap_port->pdev);
768 /* Stop any SSI TX/RX without a client */
769 ssi_set_port_mode(omap_port, SSI_MODE_SLEEP);
770 omap_port->sst.mode = SSI_MODE_SLEEP;
771 omap_port->ssr.mode = SSI_MODE_SLEEP;
772 pm_runtime_put_sync(omap_port->pdev);
773 WARN_ON(omap_port->wk_refcount != 0);
774 }
775 spin_unlock_bh(&omap_port->lock);
776
777 return 0;
778}
779
780
781
782static void ssi_error(struct hsi_port *port)
783{
784 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
785 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
786 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
787 struct hsi_msg *msg;
788 unsigned int i;
789 u32 err;
790 u32 val;
791 u32 tmp;
792
793 /* ACK error */
794 err = readl(omap_port->ssr_base + SSI_SSR_ERROR_REG);
795 dev_err(&port->device, "SSI error: 0x%02x\n", err);
796 if (!err) {
797 dev_dbg(&port->device, "spurious SSI error ignored!\n");
798 return;
799 }
800 spin_lock(&omap_ssi->lock);
801 /* Cancel all GDD read transfers */
802 for (i = 0, val = 0; i < SSI_MAX_GDD_LCH; i++) {
803 msg = omap_ssi->gdd_trn[i].msg;
804 if ((msg) && (msg->ttype == HSI_MSG_READ)) {
805 writew_relaxed(0, omap_ssi->gdd + SSI_GDD_CCR_REG(i));
806 val |= (1 << i);
807 omap_ssi->gdd_trn[i].msg = NULL;
808 }
809 }
810 tmp = readl(omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
811 tmp &= ~val;
812 writel_relaxed(tmp, omap_ssi->sys + SSI_GDD_MPU_IRQ_ENABLE_REG);
813 spin_unlock(&omap_ssi->lock);
814 /* Cancel all PIO read transfers */
815 spin_lock(&omap_port->lock);
816 tmp = readl(omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
817 tmp &= 0xfeff00ff; /* Disable error & all dataavailable interrupts */
818 writel_relaxed(tmp, omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
819 /* ACK error */
820 writel_relaxed(err, omap_port->ssr_base + SSI_SSR_ERRORACK_REG);
821 writel_relaxed(SSI_ERROROCCURED,
822 omap_ssi->sys + SSI_MPU_STATUS_REG(port->num, 0));
823 /* Signal the error all current pending read requests */
824 for (i = 0; i < omap_port->channels; i++) {
825 if (list_empty(&omap_port->rxqueue[i]))
826 continue;
827 msg = list_first_entry(&omap_port->rxqueue[i], struct hsi_msg,
828 link);
829 list_del(&msg->link);
830 msg->status = HSI_STATUS_ERROR;
831 spin_unlock(&omap_port->lock);
832 msg->complete(msg);
833 /* Now restart queued reads if any */
834 ssi_transfer(omap_port, &omap_port->rxqueue[i]);
835 spin_lock(&omap_port->lock);
836 }
837 spin_unlock(&omap_port->lock);
838}
839
840static void ssi_break_complete(struct hsi_port *port)
841{
842 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
843 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
844 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
845 struct hsi_msg *msg;
846 struct hsi_msg *tmp;
847 u32 val;
848
849 dev_dbg(&port->device, "HWBREAK received\n");
850
851 spin_lock(&omap_port->lock);
852 val = readl(omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
853 val &= ~SSI_BREAKDETECTED;
854 writel_relaxed(val, omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
855 writel_relaxed(0, omap_port->ssr_base + SSI_SSR_BREAK_REG);
856 writel(SSI_BREAKDETECTED,
857 omap_ssi->sys + SSI_MPU_STATUS_REG(port->num, 0));
858 spin_unlock(&omap_port->lock);
859
860 list_for_each_entry_safe(msg, tmp, &omap_port->brkqueue, link) {
861 msg->status = HSI_STATUS_COMPLETED;
862 spin_lock(&omap_port->lock);
863 list_del(&msg->link);
864 spin_unlock(&omap_port->lock);
865 msg->complete(msg);
866 }
867
868}
869
870static void ssi_pio_complete(struct hsi_port *port, struct list_head *queue)
871{
872 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
873 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
874 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
875 struct hsi_msg *msg;
876 u32 *buf;
877 u32 reg;
878 u32 val;
879
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200880 spin_lock_bh(&omap_port->lock);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100881 msg = list_first_entry(queue, struct hsi_msg, link);
882 if ((!msg->sgt.nents) || (!msg->sgt.sgl->length)) {
883 msg->actual_len = 0;
884 msg->status = HSI_STATUS_PENDING;
885 }
886 if (msg->ttype == HSI_MSG_WRITE)
887 val = SSI_DATAACCEPT(msg->channel);
888 else
889 val = SSI_DATAAVAILABLE(msg->channel);
890 if (msg->status == HSI_STATUS_PROCEEDING) {
891 buf = sg_virt(msg->sgt.sgl) + msg->actual_len;
892 if (msg->ttype == HSI_MSG_WRITE)
893 writel(*buf, omap_port->sst_base +
894 SSI_SST_BUFFER_CH_REG(msg->channel));
895 else
896 *buf = readl(omap_port->ssr_base +
897 SSI_SSR_BUFFER_CH_REG(msg->channel));
898 dev_dbg(&port->device, "ch %d ttype %d 0x%08x\n", msg->channel,
899 msg->ttype, *buf);
900 msg->actual_len += sizeof(*buf);
901 if (msg->actual_len >= msg->sgt.sgl->length)
902 msg->status = HSI_STATUS_COMPLETED;
903 /*
904 * Wait for the last written frame to be really sent before
905 * we call the complete callback
906 */
907 if ((msg->status == HSI_STATUS_PROCEEDING) ||
908 ((msg->status == HSI_STATUS_COMPLETED) &&
909 (msg->ttype == HSI_MSG_WRITE))) {
910 writel(val, omap_ssi->sys +
911 SSI_MPU_STATUS_REG(port->num, 0));
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200912 spin_unlock_bh(&omap_port->lock);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100913
914 return;
915 }
916
917 }
918 /* Transfer completed at this point */
919 reg = readl(omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
920 if (msg->ttype == HSI_MSG_WRITE) {
921 /* Release clocks for write transfer */
922 pm_runtime_put_sync(omap_port->pdev);
923 }
924 reg &= ~val;
925 writel_relaxed(reg, omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
926 writel_relaxed(val, omap_ssi->sys + SSI_MPU_STATUS_REG(port->num, 0));
927 list_del(&msg->link);
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200928 spin_unlock_bh(&omap_port->lock);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100929 msg->complete(msg);
930 ssi_transfer(omap_port, queue);
931}
932
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200933static irqreturn_t ssi_pio_thread(int irq, void *ssi_port)
Sebastian Reichelb209e042013-12-15 23:38:58 +0100934{
935 struct hsi_port *port = (struct hsi_port *)ssi_port;
936 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
937 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
938 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
939 void __iomem *sys = omap_ssi->sys;
940 unsigned int ch;
941 u32 status_reg;
942
943 pm_runtime_get_sync(omap_port->pdev);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100944
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200945 do {
946 status_reg = readl(sys + SSI_MPU_STATUS_REG(port->num, 0));
947 status_reg &= readl(sys + SSI_MPU_ENABLE_REG(port->num, 0));
Sebastian Reichelb209e042013-12-15 23:38:58 +0100948
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200949 for (ch = 0; ch < omap_port->channels; ch++) {
950 if (status_reg & SSI_DATAACCEPT(ch))
951 ssi_pio_complete(port, &omap_port->txqueue[ch]);
952 if (status_reg & SSI_DATAAVAILABLE(ch))
953 ssi_pio_complete(port, &omap_port->rxqueue[ch]);
954 }
955 if (status_reg & SSI_BREAKDETECTED)
956 ssi_break_complete(port);
957 if (status_reg & SSI_ERROROCCURED)
958 ssi_error(port);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100959
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200960 status_reg = readl(sys + SSI_MPU_STATUS_REG(port->num, 0));
961 status_reg &= readl(sys + SSI_MPU_ENABLE_REG(port->num, 0));
Sebastian Reichelb209e042013-12-15 23:38:58 +0100962
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200963 /* TODO: sleep if we retry? */
964 } while (status_reg);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100965
Sebastian Reichelc4a62572016-06-17 21:18:09 +0200966 pm_runtime_put(omap_port->pdev);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100967 return IRQ_HANDLED;
968}
969
Sebastian Reichelcb70e4c2016-04-30 16:34:43 +0200970static irqreturn_t ssi_wake_thread(int irq __maybe_unused, void *ssi_port)
Sebastian Reichelb209e042013-12-15 23:38:58 +0100971{
972 struct hsi_port *port = (struct hsi_port *)ssi_port;
973 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
974 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
975 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
976
977 if (ssi_wakein(port)) {
978 /**
979 * We can have a quick High-Low-High transition in the line.
980 * In such a case if we have long interrupt latencies,
981 * we can miss the low event or get twice a high event.
982 * This workaround will avoid breaking the clock reference
983 * count when such a situation ocurrs.
984 */
Sebastian Reichel20830572016-04-30 17:11:54 +0200985 if (!test_and_set_bit(SSI_WAKE_EN, &omap_port->flags))
Sebastian Reichelb209e042013-12-15 23:38:58 +0100986 pm_runtime_get_sync(omap_port->pdev);
Sebastian Reichelb209e042013-12-15 23:38:58 +0100987 dev_dbg(&ssi->device, "Wake in high\n");
988 if (omap_port->wktest) { /* FIXME: HACK ! To be removed */
989 writel(SSI_WAKE(0),
990 omap_ssi->sys + SSI_SET_WAKE_REG(port->num));
991 }
992 hsi_event(port, HSI_EVENT_START_RX);
993 } else {
994 dev_dbg(&ssi->device, "Wake in low\n");
995 if (omap_port->wktest) { /* FIXME: HACK ! To be removed */
996 writel(SSI_WAKE(0),
997 omap_ssi->sys + SSI_CLEAR_WAKE_REG(port->num));
998 }
999 hsi_event(port, HSI_EVENT_STOP_RX);
Sebastian Reichel20830572016-04-30 17:11:54 +02001000 if (test_and_clear_bit(SSI_WAKE_EN, &omap_port->flags))
Sebastian Reichelb209e042013-12-15 23:38:58 +01001001 pm_runtime_put_sync(omap_port->pdev);
Sebastian Reichelb209e042013-12-15 23:38:58 +01001002 }
Sebastian Reichelb209e042013-12-15 23:38:58 +01001003
1004 return IRQ_HANDLED;
1005}
1006
Sebastian Reichel8c009f12016-04-30 16:05:59 +02001007static int ssi_port_irq(struct hsi_port *port, struct platform_device *pd)
Sebastian Reichelb209e042013-12-15 23:38:58 +01001008{
1009 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
1010 int err;
1011
Andrey Utkinb74d4952014-07-17 16:53:54 +03001012 err = platform_get_irq(pd, 0);
1013 if (err < 0) {
Sebastian Reichelb209e042013-12-15 23:38:58 +01001014 dev_err(&port->device, "Port IRQ resource missing\n");
Andrey Utkinb74d4952014-07-17 16:53:54 +03001015 return err;
Sebastian Reichelb209e042013-12-15 23:38:58 +01001016 }
Andrey Utkinb74d4952014-07-17 16:53:54 +03001017 omap_port->irq = err;
Sebastian Reichelc4a62572016-06-17 21:18:09 +02001018 err = devm_request_threaded_irq(&port->device, omap_port->irq, NULL,
1019 ssi_pio_thread, IRQF_ONESHOT, "SSI PORT", port);
Sebastian Reichelb209e042013-12-15 23:38:58 +01001020 if (err < 0)
1021 dev_err(&port->device, "Request IRQ %d failed (%d)\n",
1022 omap_port->irq, err);
1023 return err;
1024}
1025
Sebastian Reichel8c009f12016-04-30 16:05:59 +02001026static int ssi_wake_irq(struct hsi_port *port, struct platform_device *pd)
Sebastian Reichelb209e042013-12-15 23:38:58 +01001027{
1028 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
1029 int cawake_irq;
1030 int err;
1031
Sebastian Reichel73e6ce02016-04-30 00:04:29 +02001032 if (!omap_port->wake_gpio) {
Sebastian Reichelb209e042013-12-15 23:38:58 +01001033 omap_port->wake_irq = -1;
1034 return 0;
1035 }
1036
Sebastian Reichel73e6ce02016-04-30 00:04:29 +02001037 cawake_irq = gpiod_to_irq(omap_port->wake_gpio);
Sebastian Reichelb209e042013-12-15 23:38:58 +01001038 omap_port->wake_irq = cawake_irq;
Sebastian Reichelcb70e4c2016-04-30 16:34:43 +02001039
1040 err = devm_request_threaded_irq(&port->device, cawake_irq, NULL,
1041 ssi_wake_thread,
1042 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
1043 "SSI cawake", port);
Sebastian Reichelb209e042013-12-15 23:38:58 +01001044 if (err < 0)
1045 dev_err(&port->device, "Request Wake in IRQ %d failed %d\n",
1046 cawake_irq, err);
1047 err = enable_irq_wake(cawake_irq);
1048 if (err < 0)
1049 dev_err(&port->device, "Enable wake on the wakeline in irq %d failed %d\n",
1050 cawake_irq, err);
1051
1052 return err;
1053}
1054
Sebastian Reichel0845e1f22016-04-30 01:01:06 +02001055static void ssi_queues_init(struct omap_ssi_port *omap_port)
Sebastian Reichelb209e042013-12-15 23:38:58 +01001056{
1057 unsigned int ch;
1058
1059 for (ch = 0; ch < SSI_MAX_CHANNELS; ch++) {
1060 INIT_LIST_HEAD(&omap_port->txqueue[ch]);
1061 INIT_LIST_HEAD(&omap_port->rxqueue[ch]);
1062 }
1063 INIT_LIST_HEAD(&omap_port->brkqueue);
1064}
1065
Sebastian Reichel0845e1f22016-04-30 01:01:06 +02001066static int ssi_port_get_iomem(struct platform_device *pd,
Sebastian Reichelb209e042013-12-15 23:38:58 +01001067 const char *name, void __iomem **pbase, dma_addr_t *phy)
1068{
1069 struct hsi_port *port = platform_get_drvdata(pd);
1070 struct resource *mem;
1071 struct resource *ioarea;
1072 void __iomem *base;
1073
1074 mem = platform_get_resource_byname(pd, IORESOURCE_MEM, name);
1075 if (!mem) {
1076 dev_err(&pd->dev, "IO memory region missing (%s)\n", name);
1077 return -ENXIO;
1078 }
1079 ioarea = devm_request_mem_region(&port->device, mem->start,
1080 resource_size(mem), dev_name(&pd->dev));
1081 if (!ioarea) {
1082 dev_err(&pd->dev, "%s IO memory region request failed\n",
1083 mem->name);
1084 return -ENXIO;
1085 }
1086 base = devm_ioremap(&port->device, mem->start, resource_size(mem));
1087 if (!base) {
1088 dev_err(&pd->dev, "%s IO remap failed\n", mem->name);
1089 return -ENXIO;
1090 }
1091 *pbase = base;
1092
1093 if (phy)
1094 *phy = mem->start;
1095
1096 return 0;
1097}
1098
Sebastian Reichel0845e1f22016-04-30 01:01:06 +02001099static int ssi_port_probe(struct platform_device *pd)
Sebastian Reichelb209e042013-12-15 23:38:58 +01001100{
1101 struct device_node *np = pd->dev.of_node;
1102 struct hsi_port *port;
1103 struct omap_ssi_port *omap_port;
1104 struct hsi_controller *ssi = dev_get_drvdata(pd->dev.parent);
1105 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
Sebastian Reichel73e6ce02016-04-30 00:04:29 +02001106 struct gpio_desc *cawake_gpio = NULL;
Sebastian Reichelb209e042013-12-15 23:38:58 +01001107 u32 port_id;
1108 int err;
1109
1110 dev_dbg(&pd->dev, "init ssi port...\n");
1111
Sebastian Reichelb209e042013-12-15 23:38:58 +01001112 if (!ssi->port || !omap_ssi->port) {
1113 dev_err(&pd->dev, "ssi controller not initialized!\n");
1114 err = -ENODEV;
1115 goto error;
1116 }
1117
1118 /* get id of first uninitialized port in controller */
1119 for (port_id = 0; port_id < ssi->num_ports && omap_ssi->port[port_id];
1120 port_id++)
1121 ;
1122
1123 if (port_id >= ssi->num_ports) {
1124 dev_err(&pd->dev, "port id out of range!\n");
1125 err = -ENODEV;
1126 goto error;
1127 }
1128
1129 port = ssi->port[port_id];
1130
1131 if (!np) {
1132 dev_err(&pd->dev, "missing device tree data\n");
1133 err = -EINVAL;
1134 goto error;
1135 }
1136
Sebastian Reichel73e6ce02016-04-30 00:04:29 +02001137 cawake_gpio = devm_gpiod_get(&pd->dev, "ti,ssi-cawake", GPIOD_IN);
1138 if (IS_ERR(cawake_gpio)) {
1139 err = PTR_ERR(cawake_gpio);
1140 dev_err(&pd->dev, "couldn't get cawake gpio (err=%d)!\n", err);
Sebastian Reichelb209e042013-12-15 23:38:58 +01001141 goto error;
1142 }
1143
1144 omap_port = devm_kzalloc(&port->device, sizeof(*omap_port), GFP_KERNEL);
1145 if (!omap_port) {
1146 err = -ENOMEM;
1147 goto error;
1148 }
1149 omap_port->wake_gpio = cawake_gpio;
1150 omap_port->pdev = &pd->dev;
1151 omap_port->port_id = port_id;
1152
Sebastian Reichel7c5d8162016-05-11 20:33:45 +02001153 INIT_WORK(&omap_port->work, start_tx_work);
1154
Sebastian Reichelb209e042013-12-15 23:38:58 +01001155 /* initialize HSI port */
1156 port->async = ssi_async;
1157 port->setup = ssi_setup;
1158 port->flush = ssi_flush;
1159 port->start_tx = ssi_start_tx;
1160 port->stop_tx = ssi_stop_tx;
1161 port->release = ssi_release;
1162 hsi_port_set_drvdata(port, omap_port);
1163 omap_ssi->port[port_id] = omap_port;
1164
1165 platform_set_drvdata(pd, port);
1166
1167 err = ssi_port_get_iomem(pd, "tx", &omap_port->sst_base,
1168 &omap_port->sst_dma);
1169 if (err < 0)
1170 goto error;
1171 err = ssi_port_get_iomem(pd, "rx", &omap_port->ssr_base,
1172 &omap_port->ssr_dma);
1173 if (err < 0)
1174 goto error;
1175
1176 err = ssi_port_irq(port, pd);
1177 if (err < 0)
1178 goto error;
1179 err = ssi_wake_irq(port, pd);
1180 if (err < 0)
1181 goto error;
1182
1183 ssi_queues_init(omap_port);
1184 spin_lock_init(&omap_port->lock);
1185 spin_lock_init(&omap_port->wk_lock);
1186 omap_port->dev = &port->device;
1187
1188 pm_runtime_irq_safe(omap_port->pdev);
1189 pm_runtime_enable(omap_port->pdev);
1190
1191#ifdef CONFIG_DEBUG_FS
1192 err = ssi_debug_add_port(omap_port, omap_ssi->dir);
1193 if (err < 0) {
1194 pm_runtime_disable(omap_port->pdev);
1195 goto error;
1196 }
1197#endif
1198
1199 hsi_add_clients_from_dt(port, np);
1200
Sebastian Reichel73e6ce02016-04-30 00:04:29 +02001201 dev_info(&pd->dev, "ssi port %u successfully initialized\n", port_id);
Sebastian Reichelb209e042013-12-15 23:38:58 +01001202
1203 return 0;
1204
1205error:
1206 return err;
1207}
1208
Sebastian Reichel0845e1f22016-04-30 01:01:06 +02001209static int ssi_port_remove(struct platform_device *pd)
Sebastian Reichelb209e042013-12-15 23:38:58 +01001210{
1211 struct hsi_port *port = platform_get_drvdata(pd);
1212 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
1213 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
1214 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
1215
1216#ifdef CONFIG_DEBUG_FS
1217 ssi_debug_remove_port(port);
1218#endif
1219
1220 hsi_port_unregister_clients(port);
1221
Sebastian Reichelb209e042013-12-15 23:38:58 +01001222 port->async = hsi_dummy_msg;
1223 port->setup = hsi_dummy_cl;
1224 port->flush = hsi_dummy_cl;
1225 port->start_tx = hsi_dummy_cl;
1226 port->stop_tx = hsi_dummy_cl;
1227 port->release = hsi_dummy_cl;
1228
1229 omap_ssi->port[omap_port->port_id] = NULL;
1230 platform_set_drvdata(pd, NULL);
Sebastian Reichelb209e042013-12-15 23:38:58 +01001231 pm_runtime_disable(&pd->dev);
1232
1233 return 0;
1234}
1235
Arnd Bergmannc2f90a42016-05-03 17:16:22 +02001236static int ssi_restore_divisor(struct omap_ssi_port *omap_port)
1237{
1238 writel_relaxed(omap_port->sst.divisor,
1239 omap_port->sst_base + SSI_SST_DIVISOR_REG);
1240
1241 return 0;
1242}
1243
1244void omap_ssi_port_update_fclk(struct hsi_controller *ssi,
1245 struct omap_ssi_port *omap_port)
1246{
1247 /* update divisor */
1248 u32 div = ssi_calculate_div(ssi);
1249 omap_port->sst.divisor = div;
1250 ssi_restore_divisor(omap_port);
1251}
1252
Rafael J. Wysocki96a1c182014-12-04 01:07:01 +01001253#ifdef CONFIG_PM
Sebastian Reichelb209e042013-12-15 23:38:58 +01001254static int ssi_save_port_ctx(struct omap_ssi_port *omap_port)
1255{
1256 struct hsi_port *port = to_hsi_port(omap_port->dev);
1257 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
1258 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
1259
1260 omap_port->sys_mpu_enable = readl(omap_ssi->sys +
1261 SSI_MPU_ENABLE_REG(port->num, 0));
1262
1263 return 0;
1264}
1265
1266static int ssi_restore_port_ctx(struct omap_ssi_port *omap_port)
1267{
1268 struct hsi_port *port = to_hsi_port(omap_port->dev);
1269 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
1270 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
1271 void __iomem *base;
1272
1273 writel_relaxed(omap_port->sys_mpu_enable,
1274 omap_ssi->sys + SSI_MPU_ENABLE_REG(port->num, 0));
1275
1276 /* SST context */
1277 base = omap_port->sst_base;
1278 writel_relaxed(omap_port->sst.frame_size, base + SSI_SST_FRAMESIZE_REG);
1279 writel_relaxed(omap_port->sst.channels, base + SSI_SST_CHANNELS_REG);
1280 writel_relaxed(omap_port->sst.arb_mode, base + SSI_SST_ARBMODE_REG);
1281
1282 /* SSR context */
1283 base = omap_port->ssr_base;
1284 writel_relaxed(omap_port->ssr.frame_size, base + SSI_SSR_FRAMESIZE_REG);
1285 writel_relaxed(omap_port->ssr.channels, base + SSI_SSR_CHANNELS_REG);
1286 writel_relaxed(omap_port->ssr.timeout, base + SSI_SSR_TIMEOUT_REG);
1287
1288 return 0;
1289}
1290
1291static int ssi_restore_port_mode(struct omap_ssi_port *omap_port)
1292{
1293 u32 mode;
1294
1295 writel_relaxed(omap_port->sst.mode,
1296 omap_port->sst_base + SSI_SST_MODE_REG);
1297 writel_relaxed(omap_port->ssr.mode,
1298 omap_port->ssr_base + SSI_SSR_MODE_REG);
1299 /* OCP barrier */
1300 mode = readl(omap_port->ssr_base + SSI_SSR_MODE_REG);
1301
1302 return 0;
1303}
1304
Sebastian Reichelb209e042013-12-15 23:38:58 +01001305static int omap_ssi_port_runtime_suspend(struct device *dev)
1306{
1307 struct hsi_port *port = dev_get_drvdata(dev);
1308 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
1309 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
1310 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
1311
1312 dev_dbg(dev, "port runtime suspend!\n");
1313
1314 ssi_set_port_mode(omap_port, SSI_MODE_SLEEP);
1315 if (omap_ssi->get_loss)
1316 omap_port->loss_count =
1317 omap_ssi->get_loss(ssi->device.parent);
1318 ssi_save_port_ctx(omap_port);
1319
1320 return 0;
1321}
1322
1323static int omap_ssi_port_runtime_resume(struct device *dev)
1324{
1325 struct hsi_port *port = dev_get_drvdata(dev);
1326 struct omap_ssi_port *omap_port = hsi_port_drvdata(port);
1327 struct hsi_controller *ssi = to_hsi_controller(port->device.parent);
1328 struct omap_ssi_controller *omap_ssi = hsi_controller_drvdata(ssi);
1329
1330 dev_dbg(dev, "port runtime resume!\n");
1331
1332 if ((omap_ssi->get_loss) && (omap_port->loss_count ==
1333 omap_ssi->get_loss(ssi->device.parent)))
1334 goto mode; /* We always need to restore the mode & TX divisor */
1335
1336 ssi_restore_port_ctx(omap_port);
1337
1338mode:
1339 ssi_restore_divisor(omap_port);
1340 ssi_restore_port_mode(omap_port);
1341
1342 return 0;
1343}
1344
1345static const struct dev_pm_ops omap_ssi_port_pm_ops = {
1346 SET_RUNTIME_PM_OPS(omap_ssi_port_runtime_suspend,
1347 omap_ssi_port_runtime_resume, NULL)
1348};
1349
1350#define DEV_PM_OPS (&omap_ssi_port_pm_ops)
1351#else
1352#define DEV_PM_OPS NULL
1353#endif
1354
1355
1356#ifdef CONFIG_OF
1357static const struct of_device_id omap_ssi_port_of_match[] = {
1358 { .compatible = "ti,omap3-ssi-port", },
1359 {},
1360};
1361MODULE_DEVICE_TABLE(of, omap_ssi_port_of_match);
1362#else
1363#define omap_ssi_port_of_match NULL
1364#endif
1365
Sebastian Reichel0fae1982016-04-30 03:24:09 +02001366struct platform_driver ssi_port_pdriver = {
Sebastian Reichel0845e1f22016-04-30 01:01:06 +02001367 .probe = ssi_port_probe,
1368 .remove = ssi_port_remove,
Sebastian Reichelb209e042013-12-15 23:38:58 +01001369 .driver = {
1370 .name = "omap_ssi_port",
Sebastian Reichelb209e042013-12-15 23:38:58 +01001371 .of_match_table = omap_ssi_port_of_match,
1372 .pm = DEV_PM_OPS,
1373 },
1374};