1
2
3
4
5
6
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/module.h>
11#include <linux/of_platform.h>
12#include <linux/printk.h>
13#include <linux/kernel.h>
14#include <linux/init.h>
15#include <linux/platform_device.h>
16#include <linux/interrupt.h>
17#include <linux/slab.h>
18#include <linux/io.h>
19#include <linux/clk.h>
20#include <linux/dma-mapping.h>
21#include <linux/sched.h>
22#include <linux/kthread.h>
23#include <linux/most.h>
24#include "hal.h"
25#include "errors.h"
26#include "sysfs.h"
27
28#define DMA_CHANNELS (32 - 1)
29
30#define MAX_BUFFERS_PACKET 32
31#define MAX_BUFFERS_STREAMING 32
32#define MAX_BUF_SIZE_PACKET 2048
33#define MAX_BUF_SIZE_STREAMING (8 * 1024)
34
35
36
37
38
39
40
41
42static u8 fcnt = 4;
43module_param(fcnt, byte, 0000);
44MODULE_PARM_DESC(fcnt, "Num of frames per sub-buffer for sync channels as a power of 2");
45
46static DEFINE_SPINLOCK(dim_lock);
47
48static void dim2_tasklet_fn(unsigned long data);
49static DECLARE_TASKLET_OLD(dim2_tasklet, dim2_tasklet_fn);
50
51
52
53
54
55
56
57
58
59
60struct hdm_channel {
61 char name[sizeof "caNNN"];
62 bool is_initialized;
63 struct dim_channel ch;
64 u16 *reset_dbr_size;
65 struct list_head pending_list;
66 struct list_head started_list;
67 enum most_channel_direction direction;
68 enum most_channel_data_type data_type;
69};
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84struct dim2_hdm {
85 struct device dev;
86 struct hdm_channel hch[DMA_CHANNELS];
87 struct most_channel_capability capabilities[DMA_CHANNELS];
88 struct most_interface most_iface;
89 char name[16 + sizeof "dim2-"];
90 void __iomem *io_base;
91 u8 clk_speed;
92 struct clk *clk;
93 struct clk *clk_pll;
94 struct task_struct *netinfo_task;
95 wait_queue_head_t netinfo_waitq;
96 int deliver_netinfo;
97 unsigned char mac_addrs[6];
98 unsigned char link_state;
99 int atx_idx;
100 struct medialb_bus bus;
101 void (*on_netinfo)(struct most_interface *most_iface,
102 unsigned char link_state, unsigned char *addrs);
103 void (*disable_platform)(struct platform_device *);
104};
105
106struct dim2_platform_data {
107 int (*enable)(struct platform_device *);
108 void (*disable)(struct platform_device *);
109};
110
111#define iface_to_hdm(iface) container_of(iface, struct dim2_hdm, most_iface)
112
113
114#define PACKET_IS_NET_INFO(p) \
115 (((p)[1] == 0x18) && ((p)[2] == 0x05) && ((p)[3] == 0x0C) && \
116 ((p)[13] == 0x3C) && ((p)[14] == 0x00) && ((p)[15] == 0x0A))
117
118bool dim2_sysfs_get_state_cb(void)
119{
120 bool state;
121 unsigned long flags;
122
123 spin_lock_irqsave(&dim_lock, flags);
124 state = dim_get_lock_state();
125 spin_unlock_irqrestore(&dim_lock, flags);
126
127 return state;
128}
129
130
131
132
133
134
135
136void dimcb_on_error(u8 error_id, const char *error_message)
137{
138 pr_err("%s: error_id - %d, error_message - %s\n", __func__, error_id,
139 error_message);
140}
141
142
143
144
145
146
147
148static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
149{
150 u16 buf_size;
151 struct list_head *head = &hdm_ch->pending_list;
152 struct mbo *mbo;
153 unsigned long flags;
154 struct dim_ch_state_t st;
155
156 BUG_ON(!hdm_ch);
157 BUG_ON(!hdm_ch->is_initialized);
158
159 spin_lock_irqsave(&dim_lock, flags);
160 if (list_empty(head)) {
161 spin_unlock_irqrestore(&dim_lock, flags);
162 return -EAGAIN;
163 }
164
165 if (!dim_get_channel_state(&hdm_ch->ch, &st)->ready) {
166 spin_unlock_irqrestore(&dim_lock, flags);
167 return -EAGAIN;
168 }
169
170 mbo = list_first_entry(head, struct mbo, list);
171 buf_size = mbo->buffer_length;
172
173 if (dim_dbr_space(&hdm_ch->ch) < buf_size) {
174 spin_unlock_irqrestore(&dim_lock, flags);
175 return -EAGAIN;
176 }
177
178 BUG_ON(mbo->bus_address == 0);
179 if (!dim_enqueue_buffer(&hdm_ch->ch, mbo->bus_address, buf_size)) {
180 list_del(head->next);
181 spin_unlock_irqrestore(&dim_lock, flags);
182 mbo->processed_length = 0;
183 mbo->status = MBO_E_INVAL;
184 mbo->complete(mbo);
185 return -EFAULT;
186 }
187
188 list_move_tail(head->next, &hdm_ch->started_list);
189 spin_unlock_irqrestore(&dim_lock, flags);
190
191 return 0;
192}
193
194
195
196
197
198
199
200static int deliver_netinfo_thread(void *data)
201{
202 struct dim2_hdm *dev = data;
203
204 while (!kthread_should_stop()) {
205 wait_event_interruptible(dev->netinfo_waitq,
206 dev->deliver_netinfo ||
207 kthread_should_stop());
208
209 if (dev->deliver_netinfo) {
210 dev->deliver_netinfo--;
211 if (dev->on_netinfo) {
212 dev->on_netinfo(&dev->most_iface,
213 dev->link_state,
214 dev->mac_addrs);
215 }
216 }
217 }
218
219 return 0;
220}
221
222
223
224
225
226
227
228
229
230static void retrieve_netinfo(struct dim2_hdm *dev, struct mbo *mbo)
231{
232 u8 *data = mbo->virt_address;
233
234 pr_info("Node Address: 0x%03x\n", (u16)data[16] << 8 | data[17]);
235 dev->link_state = data[18];
236 pr_info("NIState: %d\n", dev->link_state);
237 memcpy(dev->mac_addrs, data + 19, 6);
238 dev->deliver_netinfo++;
239 wake_up_interruptible(&dev->netinfo_waitq);
240}
241
242
243
244
245
246
247
248
249static void service_done_flag(struct dim2_hdm *dev, int ch_idx)
250{
251 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
252 struct dim_ch_state_t st;
253 struct list_head *head;
254 struct mbo *mbo;
255 int done_buffers;
256 unsigned long flags;
257 u8 *data;
258
259 BUG_ON(!hdm_ch);
260 BUG_ON(!hdm_ch->is_initialized);
261
262 spin_lock_irqsave(&dim_lock, flags);
263
264 done_buffers = dim_get_channel_state(&hdm_ch->ch, &st)->done_buffers;
265 if (!done_buffers) {
266 spin_unlock_irqrestore(&dim_lock, flags);
267 return;
268 }
269
270 if (!dim_detach_buffers(&hdm_ch->ch, done_buffers)) {
271 spin_unlock_irqrestore(&dim_lock, flags);
272 return;
273 }
274 spin_unlock_irqrestore(&dim_lock, flags);
275
276 head = &hdm_ch->started_list;
277
278 while (done_buffers) {
279 spin_lock_irqsave(&dim_lock, flags);
280 if (list_empty(head)) {
281 spin_unlock_irqrestore(&dim_lock, flags);
282 pr_crit("hard error: started_mbo list is empty whereas DIM2 has sent buffers\n");
283 break;
284 }
285
286 mbo = list_first_entry(head, struct mbo, list);
287 list_del(head->next);
288 spin_unlock_irqrestore(&dim_lock, flags);
289
290 data = mbo->virt_address;
291
292 if (hdm_ch->data_type == MOST_CH_ASYNC &&
293 hdm_ch->direction == MOST_CH_RX &&
294 PACKET_IS_NET_INFO(data)) {
295 retrieve_netinfo(dev, mbo);
296
297 spin_lock_irqsave(&dim_lock, flags);
298 list_add_tail(&mbo->list, &hdm_ch->pending_list);
299 spin_unlock_irqrestore(&dim_lock, flags);
300 } else {
301 if (hdm_ch->data_type == MOST_CH_CONTROL ||
302 hdm_ch->data_type == MOST_CH_ASYNC) {
303 u32 const data_size =
304 (u32)data[0] * 256 + data[1] + 2;
305
306 mbo->processed_length =
307 min_t(u32, data_size,
308 mbo->buffer_length);
309 } else {
310 mbo->processed_length = mbo->buffer_length;
311 }
312 mbo->status = MBO_SUCCESS;
313 mbo->complete(mbo);
314 }
315
316 done_buffers--;
317 }
318}
319
320static struct dim_channel **get_active_channels(struct dim2_hdm *dev,
321 struct dim_channel **buffer)
322{
323 int idx = 0;
324 int ch_idx;
325
326 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
327 if (dev->hch[ch_idx].is_initialized)
328 buffer[idx++] = &dev->hch[ch_idx].ch;
329 }
330 buffer[idx++] = NULL;
331
332 return buffer;
333}
334
335static irqreturn_t dim2_mlb_isr(int irq, void *_dev)
336{
337 struct dim2_hdm *dev = _dev;
338 unsigned long flags;
339
340 spin_lock_irqsave(&dim_lock, flags);
341 dim_service_mlb_int_irq();
342 spin_unlock_irqrestore(&dim_lock, flags);
343
344 if (dev->atx_idx >= 0 && dev->hch[dev->atx_idx].is_initialized)
345 while (!try_start_dim_transfer(dev->hch + dev->atx_idx))
346 continue;
347
348 return IRQ_HANDLED;
349}
350
351
352
353
354
355
356
357static void dim2_tasklet_fn(unsigned long data)
358{
359 struct dim2_hdm *dev = (struct dim2_hdm *)data;
360 unsigned long flags;
361 int ch_idx;
362
363 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
364 if (!dev->hch[ch_idx].is_initialized)
365 continue;
366
367 spin_lock_irqsave(&dim_lock, flags);
368 dim_service_channel(&dev->hch[ch_idx].ch);
369 spin_unlock_irqrestore(&dim_lock, flags);
370
371 service_done_flag(dev, ch_idx);
372 while (!try_start_dim_transfer(dev->hch + ch_idx))
373 continue;
374 }
375}
376
377
378
379
380
381
382
383
384
385static irqreturn_t dim2_ahb_isr(int irq, void *_dev)
386{
387 struct dim2_hdm *dev = _dev;
388 struct dim_channel *buffer[DMA_CHANNELS + 1];
389 unsigned long flags;
390
391 spin_lock_irqsave(&dim_lock, flags);
392 dim_service_ahb_int_irq(get_active_channels(dev, buffer));
393 spin_unlock_irqrestore(&dim_lock, flags);
394
395 dim2_tasklet.data = (unsigned long)dev;
396 tasklet_schedule(&dim2_tasklet);
397 return IRQ_HANDLED;
398}
399
400
401
402
403
404
405
406
407static void complete_all_mbos(struct list_head *head)
408{
409 unsigned long flags;
410 struct mbo *mbo;
411
412 for (;;) {
413 spin_lock_irqsave(&dim_lock, flags);
414 if (list_empty(head)) {
415 spin_unlock_irqrestore(&dim_lock, flags);
416 break;
417 }
418
419 mbo = list_first_entry(head, struct mbo, list);
420 list_del(head->next);
421 spin_unlock_irqrestore(&dim_lock, flags);
422
423 mbo->processed_length = 0;
424 mbo->status = MBO_E_CLOSE;
425 mbo->complete(mbo);
426 }
427}
428
429
430
431
432
433
434
435
436
437
438static int configure_channel(struct most_interface *most_iface, int ch_idx,
439 struct most_channel_config *ccfg)
440{
441 struct dim2_hdm *dev = iface_to_hdm(most_iface);
442 bool const is_tx = ccfg->direction == MOST_CH_TX;
443 u16 const sub_size = ccfg->subbuffer_size;
444 u16 const buf_size = ccfg->buffer_size;
445 u16 new_size;
446 unsigned long flags;
447 u8 hal_ret;
448 int const ch_addr = ch_idx * 2 + 2;
449 struct hdm_channel *const hdm_ch = dev->hch + ch_idx;
450
451 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
452
453 if (hdm_ch->is_initialized)
454 return -EPERM;
455
456
457 hdm_ch->reset_dbr_size = ccfg->dbr_size ? NULL : &ccfg->dbr_size;
458
459
460 hdm_ch->ch.dbr_size = ccfg->dbr_size;
461
462 switch (ccfg->data_type) {
463 case MOST_CH_CONTROL:
464 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
465 if (new_size == 0) {
466 pr_err("%s: too small buffer size\n", hdm_ch->name);
467 return -EINVAL;
468 }
469 ccfg->buffer_size = new_size;
470 if (new_size != buf_size)
471 pr_warn("%s: fixed buffer size (%d -> %d)\n",
472 hdm_ch->name, buf_size, new_size);
473 spin_lock_irqsave(&dim_lock, flags);
474 hal_ret = dim_init_control(&hdm_ch->ch, is_tx, ch_addr,
475 is_tx ? new_size * 2 : new_size);
476 break;
477 case MOST_CH_ASYNC:
478 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
479 if (new_size == 0) {
480 pr_err("%s: too small buffer size\n", hdm_ch->name);
481 return -EINVAL;
482 }
483 ccfg->buffer_size = new_size;
484 if (new_size != buf_size)
485 pr_warn("%s: fixed buffer size (%d -> %d)\n",
486 hdm_ch->name, buf_size, new_size);
487 spin_lock_irqsave(&dim_lock, flags);
488 hal_ret = dim_init_async(&hdm_ch->ch, is_tx, ch_addr,
489 is_tx ? new_size * 2 : new_size);
490 break;
491 case MOST_CH_ISOC:
492 new_size = dim_norm_isoc_buffer_size(buf_size, sub_size);
493 if (new_size == 0) {
494 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
495 hdm_ch->name);
496 return -EINVAL;
497 }
498 ccfg->buffer_size = new_size;
499 if (new_size != buf_size)
500 pr_warn("%s: fixed buffer size (%d -> %d)\n",
501 hdm_ch->name, buf_size, new_size);
502 spin_lock_irqsave(&dim_lock, flags);
503 hal_ret = dim_init_isoc(&hdm_ch->ch, is_tx, ch_addr, sub_size);
504 break;
505 case MOST_CH_SYNC:
506 new_size = dim_norm_sync_buffer_size(buf_size, sub_size);
507 if (new_size == 0) {
508 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
509 hdm_ch->name);
510 return -EINVAL;
511 }
512 ccfg->buffer_size = new_size;
513 if (new_size != buf_size)
514 pr_warn("%s: fixed buffer size (%d -> %d)\n",
515 hdm_ch->name, buf_size, new_size);
516 spin_lock_irqsave(&dim_lock, flags);
517 hal_ret = dim_init_sync(&hdm_ch->ch, is_tx, ch_addr, sub_size);
518 break;
519 default:
520 pr_err("%s: configure failed, bad channel type: %d\n",
521 hdm_ch->name, ccfg->data_type);
522 return -EINVAL;
523 }
524
525 if (hal_ret != DIM_NO_ERROR) {
526 spin_unlock_irqrestore(&dim_lock, flags);
527 pr_err("%s: configure failed (%d), type: %d, is_tx: %d\n",
528 hdm_ch->name, hal_ret, ccfg->data_type, (int)is_tx);
529 return -ENODEV;
530 }
531
532 hdm_ch->data_type = ccfg->data_type;
533 hdm_ch->direction = ccfg->direction;
534 hdm_ch->is_initialized = true;
535
536 if (hdm_ch->data_type == MOST_CH_ASYNC &&
537 hdm_ch->direction == MOST_CH_TX &&
538 dev->atx_idx < 0)
539 dev->atx_idx = ch_idx;
540
541 spin_unlock_irqrestore(&dim_lock, flags);
542 ccfg->dbr_size = hdm_ch->ch.dbr_size;
543
544 return 0;
545}
546
547
548
549
550
551
552
553
554
555
556static int enqueue(struct most_interface *most_iface, int ch_idx,
557 struct mbo *mbo)
558{
559 struct dim2_hdm *dev = iface_to_hdm(most_iface);
560 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
561 unsigned long flags;
562
563 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
564
565 if (!hdm_ch->is_initialized)
566 return -EPERM;
567
568 if (mbo->bus_address == 0)
569 return -EFAULT;
570
571 spin_lock_irqsave(&dim_lock, flags);
572 list_add_tail(&mbo->list, &hdm_ch->pending_list);
573 spin_unlock_irqrestore(&dim_lock, flags);
574
575 (void)try_start_dim_transfer(hdm_ch);
576
577 return 0;
578}
579
580
581
582
583
584
585
586
587
588static void request_netinfo(struct most_interface *most_iface, int ch_idx,
589 void (*on_netinfo)(struct most_interface *,
590 unsigned char, unsigned char *))
591{
592 struct dim2_hdm *dev = iface_to_hdm(most_iface);
593 struct mbo *mbo;
594 u8 *data;
595
596 dev->on_netinfo = on_netinfo;
597 if (!on_netinfo)
598 return;
599
600 if (dev->atx_idx < 0) {
601 pr_err("Async Tx Not initialized\n");
602 return;
603 }
604
605 mbo = most_get_mbo(&dev->most_iface, dev->atx_idx, NULL);
606 if (!mbo)
607 return;
608
609 mbo->buffer_length = 5;
610
611 data = mbo->virt_address;
612
613 data[0] = 0x00;
614 data[1] = 0x03;
615 data[2] = 0x02;
616 data[3] = 0x08;
617 data[4] = 0x40;
618
619 most_submit_mbo(mbo);
620}
621
622
623
624
625
626
627
628
629
630static int poison_channel(struct most_interface *most_iface, int ch_idx)
631{
632 struct dim2_hdm *dev = iface_to_hdm(most_iface);
633 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
634 unsigned long flags;
635 u8 hal_ret;
636 int ret = 0;
637
638 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
639
640 if (!hdm_ch->is_initialized)
641 return -EPERM;
642
643 tasklet_disable(&dim2_tasklet);
644 spin_lock_irqsave(&dim_lock, flags);
645 hal_ret = dim_destroy_channel(&hdm_ch->ch);
646 hdm_ch->is_initialized = false;
647 if (ch_idx == dev->atx_idx)
648 dev->atx_idx = -1;
649 spin_unlock_irqrestore(&dim_lock, flags);
650 tasklet_enable(&dim2_tasklet);
651 if (hal_ret != DIM_NO_ERROR) {
652 pr_err("HAL Failed to close channel %s\n", hdm_ch->name);
653 ret = -EFAULT;
654 }
655
656 complete_all_mbos(&hdm_ch->started_list);
657 complete_all_mbos(&hdm_ch->pending_list);
658 if (hdm_ch->reset_dbr_size)
659 *hdm_ch->reset_dbr_size = 0;
660
661 return ret;
662}
663
664static void *dma_alloc(struct mbo *mbo, u32 size)
665{
666 struct device *dev = mbo->ifp->driver_dev;
667
668 return dma_alloc_coherent(dev, size, &mbo->bus_address, GFP_KERNEL);
669}
670
671static void dma_free(struct mbo *mbo, u32 size)
672{
673 struct device *dev = mbo->ifp->driver_dev;
674
675 dma_free_coherent(dev, size, mbo->virt_address, mbo->bus_address);
676}
677
678static const struct of_device_id dim2_of_match[];
679
680static struct {
681 const char *clock_speed;
682 u8 clk_speed;
683} clk_mt[] = {
684 { "256fs", CLK_256FS },
685 { "512fs", CLK_512FS },
686 { "1024fs", CLK_1024FS },
687 { "2048fs", CLK_2048FS },
688 { "3072fs", CLK_3072FS },
689 { "4096fs", CLK_4096FS },
690 { "6144fs", CLK_6144FS },
691 { "8192fs", CLK_8192FS },
692};
693
694
695
696
697
698
699
700
701
702
703static int get_dim2_clk_speed(const char *clock_speed, u8 *val)
704{
705 int i;
706
707 for (i = 0; i < ARRAY_SIZE(clk_mt); i++) {
708 if (!strcmp(clock_speed, clk_mt[i].clock_speed)) {
709 *val = clk_mt[i].clk_speed;
710 return 0;
711 }
712 }
713 return -EINVAL;
714}
715
716
717
718
719
720
721
722
723static int dim2_probe(struct platform_device *pdev)
724{
725 const struct dim2_platform_data *pdata;
726 const struct of_device_id *of_id;
727 const char *clock_speed;
728 struct dim2_hdm *dev;
729 struct resource *res;
730 int ret, i;
731 u8 hal_ret;
732 int irq;
733
734 enum { MLB_INT_IDX, AHB0_INT_IDX };
735
736 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
737 if (!dev)
738 return -ENOMEM;
739
740 dev->atx_idx = -1;
741
742 platform_set_drvdata(pdev, dev);
743
744 ret = of_property_read_string(pdev->dev.of_node,
745 "microchip,clock-speed", &clock_speed);
746 if (ret) {
747 dev_err(&pdev->dev, "missing dt property clock-speed\n");
748 return ret;
749 }
750
751 ret = get_dim2_clk_speed(clock_speed, &dev->clk_speed);
752 if (ret) {
753 dev_err(&pdev->dev, "bad dt property clock-speed\n");
754 return ret;
755 }
756
757 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
758 dev->io_base = devm_ioremap_resource(&pdev->dev, res);
759 if (IS_ERR(dev->io_base))
760 return PTR_ERR(dev->io_base);
761
762 of_id = of_match_node(dim2_of_match, pdev->dev.of_node);
763 pdata = of_id->data;
764 ret = pdata && pdata->enable ? pdata->enable(pdev) : 0;
765 if (ret)
766 return ret;
767
768 dev->disable_platform = pdata ? pdata->disable : NULL;
769
770 dev_info(&pdev->dev, "sync: num of frames per sub-buffer: %u\n", fcnt);
771 hal_ret = dim_startup(dev->io_base, dev->clk_speed, fcnt);
772 if (hal_ret != DIM_NO_ERROR) {
773 dev_err(&pdev->dev, "dim_startup failed: %d\n", hal_ret);
774 ret = -ENODEV;
775 goto err_disable_platform;
776 }
777
778 irq = platform_get_irq(pdev, AHB0_INT_IDX);
779 if (irq < 0) {
780 ret = irq;
781 goto err_shutdown_dim;
782 }
783
784 ret = devm_request_irq(&pdev->dev, irq, dim2_ahb_isr, 0,
785 "dim2_ahb0_int", dev);
786 if (ret) {
787 dev_err(&pdev->dev, "failed to request ahb0_int irq %d\n", irq);
788 goto err_shutdown_dim;
789 }
790
791 irq = platform_get_irq(pdev, MLB_INT_IDX);
792 if (irq < 0) {
793 ret = irq;
794 goto err_shutdown_dim;
795 }
796
797 ret = devm_request_irq(&pdev->dev, irq, dim2_mlb_isr, 0,
798 "dim2_mlb_int", dev);
799 if (ret) {
800 dev_err(&pdev->dev, "failed to request mlb_int irq %d\n", irq);
801 goto err_shutdown_dim;
802 }
803
804 init_waitqueue_head(&dev->netinfo_waitq);
805 dev->deliver_netinfo = 0;
806 dev->netinfo_task = kthread_run(&deliver_netinfo_thread, dev,
807 "dim2_netinfo");
808 if (IS_ERR(dev->netinfo_task)) {
809 ret = PTR_ERR(dev->netinfo_task);
810 goto err_shutdown_dim;
811 }
812
813 for (i = 0; i < DMA_CHANNELS; i++) {
814 struct most_channel_capability *cap = dev->capabilities + i;
815 struct hdm_channel *hdm_ch = dev->hch + i;
816
817 INIT_LIST_HEAD(&hdm_ch->pending_list);
818 INIT_LIST_HEAD(&hdm_ch->started_list);
819 hdm_ch->is_initialized = false;
820 snprintf(hdm_ch->name, sizeof(hdm_ch->name), "ca%d", i * 2 + 2);
821
822 cap->name_suffix = hdm_ch->name;
823 cap->direction = MOST_CH_RX | MOST_CH_TX;
824 cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
825 MOST_CH_ISOC | MOST_CH_SYNC;
826 cap->num_buffers_packet = MAX_BUFFERS_PACKET;
827 cap->buffer_size_packet = MAX_BUF_SIZE_PACKET;
828 cap->num_buffers_streaming = MAX_BUFFERS_STREAMING;
829 cap->buffer_size_streaming = MAX_BUF_SIZE_STREAMING;
830 }
831
832 {
833 const char *fmt;
834
835 if (sizeof(res->start) == sizeof(long long))
836 fmt = "dim2-%016llx";
837 else if (sizeof(res->start) == sizeof(long))
838 fmt = "dim2-%016lx";
839 else
840 fmt = "dim2-%016x";
841
842 snprintf(dev->name, sizeof(dev->name), fmt, res->start);
843 }
844
845 dev->most_iface.interface = ITYPE_MEDIALB_DIM2;
846 dev->most_iface.description = dev->name;
847 dev->most_iface.num_channels = DMA_CHANNELS;
848 dev->most_iface.channel_vector = dev->capabilities;
849 dev->most_iface.configure = configure_channel;
850 dev->most_iface.enqueue = enqueue;
851 dev->most_iface.dma_alloc = dma_alloc;
852 dev->most_iface.dma_free = dma_free;
853 dev->most_iface.poison_channel = poison_channel;
854 dev->most_iface.request_netinfo = request_netinfo;
855 dev->most_iface.driver_dev = &pdev->dev;
856 dev->most_iface.dev = &dev->dev;
857 dev->dev.init_name = "dim2_state";
858 dev->dev.parent = &pdev->dev;
859
860 ret = most_register_interface(&dev->most_iface);
861 if (ret) {
862 dev_err(&pdev->dev, "failed to register MOST interface\n");
863 goto err_stop_thread;
864 }
865
866 ret = dim2_sysfs_probe(&dev->dev);
867 if (ret) {
868 dev_err(&pdev->dev, "failed to create sysfs attribute\n");
869 goto err_unreg_iface;
870 }
871
872 return 0;
873
874err_unreg_iface:
875 most_deregister_interface(&dev->most_iface);
876err_stop_thread:
877 kthread_stop(dev->netinfo_task);
878err_shutdown_dim:
879 dim_shutdown();
880err_disable_platform:
881 if (dev->disable_platform)
882 dev->disable_platform(pdev);
883
884 return ret;
885}
886
887
888
889
890
891
892
893static int dim2_remove(struct platform_device *pdev)
894{
895 struct dim2_hdm *dev = platform_get_drvdata(pdev);
896 unsigned long flags;
897
898 dim2_sysfs_destroy(&dev->dev);
899 most_deregister_interface(&dev->most_iface);
900 kthread_stop(dev->netinfo_task);
901
902 spin_lock_irqsave(&dim_lock, flags);
903 dim_shutdown();
904 spin_unlock_irqrestore(&dim_lock, flags);
905
906 if (dev->disable_platform)
907 dev->disable_platform(pdev);
908
909 return 0;
910}
911
912
913
914static int fsl_mx6_enable(struct platform_device *pdev)
915{
916 struct dim2_hdm *dev = platform_get_drvdata(pdev);
917 int ret;
918
919 dev->clk = devm_clk_get(&pdev->dev, "mlb");
920 if (IS_ERR_OR_NULL(dev->clk)) {
921 dev_err(&pdev->dev, "unable to get mlb clock\n");
922 return -EFAULT;
923 }
924
925 ret = clk_prepare_enable(dev->clk);
926 if (ret) {
927 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
928 return ret;
929 }
930
931 if (dev->clk_speed >= CLK_2048FS) {
932
933 dev->clk_pll = devm_clk_get(&pdev->dev, "pll8_mlb");
934 if (IS_ERR_OR_NULL(dev->clk_pll)) {
935 dev_err(&pdev->dev, "unable to get mlb pll clock\n");
936 clk_disable_unprepare(dev->clk);
937 return -EFAULT;
938 }
939
940 writel(0x888, dev->io_base + 0x38);
941 clk_prepare_enable(dev->clk_pll);
942 }
943
944 return 0;
945}
946
947static void fsl_mx6_disable(struct platform_device *pdev)
948{
949 struct dim2_hdm *dev = platform_get_drvdata(pdev);
950
951 if (dev->clk_speed >= CLK_2048FS)
952 clk_disable_unprepare(dev->clk_pll);
953
954 clk_disable_unprepare(dev->clk);
955}
956
957static int rcar_h2_enable(struct platform_device *pdev)
958{
959 struct dim2_hdm *dev = platform_get_drvdata(pdev);
960 int ret;
961
962 dev->clk = devm_clk_get(&pdev->dev, NULL);
963 if (IS_ERR(dev->clk)) {
964 dev_err(&pdev->dev, "cannot get clock\n");
965 return PTR_ERR(dev->clk);
966 }
967
968 ret = clk_prepare_enable(dev->clk);
969 if (ret) {
970 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
971 return ret;
972 }
973
974 if (dev->clk_speed >= CLK_2048FS) {
975
976 writel(0x03, dev->io_base + 0x600);
977
978 writel(0x888, dev->io_base + 0x38);
979 } else {
980
981 writel(0x04, dev->io_base + 0x600);
982 }
983
984
985
986 writel(0x03, dev->io_base + 0x500);
987 writel(0x0002FF02, dev->io_base + 0x508);
988
989 return 0;
990}
991
992static void rcar_h2_disable(struct platform_device *pdev)
993{
994 struct dim2_hdm *dev = platform_get_drvdata(pdev);
995
996 clk_disable_unprepare(dev->clk);
997
998
999 writel(0x0, dev->io_base + 0x600);
1000}
1001
1002static int rcar_m3_enable(struct platform_device *pdev)
1003{
1004 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1005 u32 enable_512fs = dev->clk_speed == CLK_512FS;
1006 int ret;
1007
1008 dev->clk = devm_clk_get(&pdev->dev, NULL);
1009 if (IS_ERR(dev->clk)) {
1010 dev_err(&pdev->dev, "cannot get clock\n");
1011 return PTR_ERR(dev->clk);
1012 }
1013
1014 ret = clk_prepare_enable(dev->clk);
1015 if (ret) {
1016 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
1017 return ret;
1018 }
1019
1020
1021 writel(0x04, dev->io_base + 0x600);
1022
1023 writel(enable_512fs, dev->io_base + 0x604);
1024
1025
1026 writel(0x03, dev->io_base + 0x500);
1027 writel(0x0002FF02, dev->io_base + 0x508);
1028
1029 return 0;
1030}
1031
1032static void rcar_m3_disable(struct platform_device *pdev)
1033{
1034 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1035
1036 clk_disable_unprepare(dev->clk);
1037
1038
1039 writel(0x0, dev->io_base + 0x600);
1040}
1041
1042
1043
1044enum dim2_platforms { FSL_MX6, RCAR_H2, RCAR_M3 };
1045
1046static struct dim2_platform_data plat_data[] = {
1047 [FSL_MX6] = { .enable = fsl_mx6_enable, .disable = fsl_mx6_disable },
1048 [RCAR_H2] = { .enable = rcar_h2_enable, .disable = rcar_h2_disable },
1049 [RCAR_M3] = { .enable = rcar_m3_enable, .disable = rcar_m3_disable },
1050};
1051
1052static const struct of_device_id dim2_of_match[] = {
1053 {
1054 .compatible = "fsl,imx6q-mlb150",
1055 .data = plat_data + FSL_MX6
1056 },
1057 {
1058 .compatible = "renesas,mlp",
1059 .data = plat_data + RCAR_H2
1060 },
1061 {
1062 .compatible = "rcar,medialb-dim2",
1063 .data = plat_data + RCAR_M3
1064 },
1065 {
1066 .compatible = "xlnx,axi4-os62420_3pin-1.00.a",
1067 },
1068 {
1069 .compatible = "xlnx,axi4-os62420_6pin-1.00.a",
1070 },
1071 {},
1072};
1073
1074MODULE_DEVICE_TABLE(of, dim2_of_match);
1075
1076static struct platform_driver dim2_driver = {
1077 .probe = dim2_probe,
1078 .remove = dim2_remove,
1079 .driver = {
1080 .name = "hdm_dim2",
1081 .of_match_table = dim2_of_match,
1082 },
1083};
1084
1085module_platform_driver(dim2_driver);
1086
1087MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
1088MODULE_DESCRIPTION("MediaLB DIM2 Hardware Dependent Module");
1089MODULE_LICENSE("GPL");
1090