1 // SPDX-License-Identifier: GPL-2.0
3 * dim2.c - MediaLB DIM2 Hardware Dependent Module
5 * Copyright (C) 2015-2016, Microchip Technology Germany II GmbH & Co. KG
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
19 #include <linux/clk.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/sched.h>
22 #include <linux/kthread.h>
29 #define DMA_CHANNELS (32 - 1) /* channel 0 is a system channel */
31 #define MAX_BUFFERS_PACKET 32
32 #define MAX_BUFFERS_STREAMING 32
33 #define MAX_BUF_SIZE_PACKET 2048
34 #define MAX_BUF_SIZE_STREAMING (8 * 1024)
37 * The parameter representing the number of frames per sub-buffer for
38 * synchronous channels. Valid values: [0 .. 6].
40 * The values 0, 1, 2, 3, 4, 5, 6 represent corresponding number of frames per
41 * sub-buffer 1, 2, 4, 8, 16, 32, 64.
43 static u8 fcnt = 4; /* (1 << fcnt) frames per subbuffer */
44 module_param(fcnt, byte, 0000);
45 MODULE_PARM_DESC(fcnt, "Num of frames per sub-buffer for sync channels as a power of 2");
47 static DEFINE_SPINLOCK(dim_lock);
49 static void dim2_tasklet_fn(unsigned long data);
50 static DECLARE_TASKLET(dim2_tasklet, dim2_tasklet_fn, 0);
53 * struct hdm_channel - private structure to keep channel specific data
54 * @is_initialized: identifier to know whether the channel is initialized
55 * @ch: HAL specific channel data
56 * @pending_list: list to keep MBO's before starting transfer
57 * @started_list: list to keep MBO's after starting transfer
58 * @direction: channel direction (TX or RX)
59 * @data_type: channel data type
62 char name[sizeof "caNNN"];
64 struct dim_channel ch;
66 struct list_head pending_list; /* before dim_enqueue_buffer() */
67 struct list_head started_list; /* after dim_enqueue_buffer() */
68 enum most_channel_direction direction;
69 enum most_channel_data_type data_type;
73 * struct dim2_hdm - private structure to keep interface specific data
74 * @hch: an array of channel specific data
75 * @most_iface: most interface structure
76 * @capabilities: an array of channel capability data
77 * @io_base: I/O register base address
78 * @netinfo_task: thread to deliver network status
79 * @netinfo_waitq: waitq for the thread to sleep
80 * @deliver_netinfo: to identify whether network status received
81 * @mac_addrs: INIC mac address
82 * @link_state: network link state
83 * @atx_idx: index of async tx channel
87 struct hdm_channel hch[DMA_CHANNELS];
88 struct most_channel_capability capabilities[DMA_CHANNELS];
89 struct most_interface most_iface;
90 char name[16 + sizeof "dim2-"];
91 void __iomem *io_base;
95 struct task_struct *netinfo_task;
96 wait_queue_head_t netinfo_waitq;
98 unsigned char mac_addrs[6];
99 unsigned char link_state;
101 struct medialb_bus bus;
102 void (*on_netinfo)(struct most_interface *most_iface,
103 unsigned char link_state, unsigned char *addrs);
104 void (*disable_platform)(struct platform_device *);
107 struct dim2_platform_data {
108 int (*enable)(struct platform_device *);
109 void (*disable)(struct platform_device *);
112 #define iface_to_hdm(iface) container_of(iface, struct dim2_hdm, most_iface)
114 /* Macro to identify a network status message */
115 #define PACKET_IS_NET_INFO(p) \
116 (((p)[1] == 0x18) && ((p)[2] == 0x05) && ((p)[3] == 0x0C) && \
117 ((p)[13] == 0x3C) && ((p)[14] == 0x00) && ((p)[15] == 0x0A))
119 bool dim2_sysfs_get_state_cb(void)
124 spin_lock_irqsave(&dim_lock, flags);
125 state = dim_get_lock_state();
126 spin_unlock_irqrestore(&dim_lock, flags);
132 * dimcb_on_error - callback from HAL to report miscommunication between
134 * @error_id: Error ID
135 * @error_message: Error message. Some text in a free format
137 void dimcb_on_error(u8 error_id, const char *error_message)
139 pr_err("%s: error_id - %d, error_message - %s\n", __func__, error_id,
144 * try_start_dim_transfer - try to transfer a buffer on a channel
145 * @hdm_ch: channel specific data
147 * Transfer a buffer from pending_list if the channel is ready
149 static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
152 struct list_head *head = &hdm_ch->pending_list;
155 struct dim_ch_state_t st;
158 BUG_ON(!hdm_ch->is_initialized);
160 spin_lock_irqsave(&dim_lock, flags);
161 if (list_empty(head)) {
162 spin_unlock_irqrestore(&dim_lock, flags);
166 if (!dim_get_channel_state(&hdm_ch->ch, &st)->ready) {
167 spin_unlock_irqrestore(&dim_lock, flags);
171 mbo = list_first_entry(head, struct mbo, list);
172 buf_size = mbo->buffer_length;
174 if (dim_dbr_space(&hdm_ch->ch) < buf_size) {
175 spin_unlock_irqrestore(&dim_lock, flags);
179 BUG_ON(mbo->bus_address == 0);
180 if (!dim_enqueue_buffer(&hdm_ch->ch, mbo->bus_address, buf_size)) {
181 list_del(head->next);
182 spin_unlock_irqrestore(&dim_lock, flags);
183 mbo->processed_length = 0;
184 mbo->status = MBO_E_INVAL;
189 list_move_tail(head->next, &hdm_ch->started_list);
190 spin_unlock_irqrestore(&dim_lock, flags);
196 * deliver_netinfo_thread - thread to deliver network status to mostcore
197 * @data: private data
199 * Wait for network status and deliver it to mostcore once it is received
201 static int deliver_netinfo_thread(void *data)
203 struct dim2_hdm *dev = data;
205 while (!kthread_should_stop()) {
206 wait_event_interruptible(dev->netinfo_waitq,
207 dev->deliver_netinfo ||
208 kthread_should_stop());
210 if (dev->deliver_netinfo) {
211 dev->deliver_netinfo--;
212 if (dev->on_netinfo) {
213 dev->on_netinfo(&dev->most_iface,
224 * retrieve_netinfo - retrieve network status from received buffer
228 * Parse the message in buffer and get node address, link state, MAC address.
229 * Wake up a thread to deliver this status to mostcore
231 static void retrieve_netinfo(struct dim2_hdm *dev, struct mbo *mbo)
233 u8 *data = mbo->virt_address;
235 pr_info("Node Address: 0x%03x\n", (u16)data[16] << 8 | data[17]);
236 dev->link_state = data[18];
237 pr_info("NIState: %d\n", dev->link_state);
238 memcpy(dev->mac_addrs, data + 19, 6);
239 dev->deliver_netinfo++;
240 wake_up_interruptible(&dev->netinfo_waitq);
244 * service_done_flag - handle completed buffers
246 * @ch_idx: channel index
248 * Return back the completed buffers to mostcore, using completion callback
250 static void service_done_flag(struct dim2_hdm *dev, int ch_idx)
252 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
253 struct dim_ch_state_t st;
254 struct list_head *head;
261 BUG_ON(!hdm_ch->is_initialized);
263 spin_lock_irqsave(&dim_lock, flags);
265 done_buffers = dim_get_channel_state(&hdm_ch->ch, &st)->done_buffers;
267 spin_unlock_irqrestore(&dim_lock, flags);
271 if (!dim_detach_buffers(&hdm_ch->ch, done_buffers)) {
272 spin_unlock_irqrestore(&dim_lock, flags);
275 spin_unlock_irqrestore(&dim_lock, flags);
277 head = &hdm_ch->started_list;
279 while (done_buffers) {
280 spin_lock_irqsave(&dim_lock, flags);
281 if (list_empty(head)) {
282 spin_unlock_irqrestore(&dim_lock, flags);
283 pr_crit("hard error: started_mbo list is empty whereas DIM2 has sent buffers\n");
287 mbo = list_first_entry(head, struct mbo, list);
288 list_del(head->next);
289 spin_unlock_irqrestore(&dim_lock, flags);
291 data = mbo->virt_address;
293 if (hdm_ch->data_type == MOST_CH_ASYNC &&
294 hdm_ch->direction == MOST_CH_RX &&
295 PACKET_IS_NET_INFO(data)) {
296 retrieve_netinfo(dev, mbo);
298 spin_lock_irqsave(&dim_lock, flags);
299 list_add_tail(&mbo->list, &hdm_ch->pending_list);
300 spin_unlock_irqrestore(&dim_lock, flags);
302 if (hdm_ch->data_type == MOST_CH_CONTROL ||
303 hdm_ch->data_type == MOST_CH_ASYNC) {
304 u32 const data_size =
305 (u32)data[0] * 256 + data[1] + 2;
307 mbo->processed_length =
308 min_t(u32, data_size,
311 mbo->processed_length = mbo->buffer_length;
313 mbo->status = MBO_SUCCESS;
321 static struct dim_channel **get_active_channels(struct dim2_hdm *dev,
322 struct dim_channel **buffer)
327 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
328 if (dev->hch[ch_idx].is_initialized)
329 buffer[idx++] = &dev->hch[ch_idx].ch;
331 buffer[idx++] = NULL;
336 static irqreturn_t dim2_mlb_isr(int irq, void *_dev)
338 struct dim2_hdm *dev = _dev;
341 spin_lock_irqsave(&dim_lock, flags);
342 dim_service_mlb_int_irq();
343 spin_unlock_irqrestore(&dim_lock, flags);
345 if (dev->atx_idx >= 0 && dev->hch[dev->atx_idx].is_initialized)
346 while (!try_start_dim_transfer(dev->hch + dev->atx_idx))
353 * dim2_tasklet_fn - tasklet function
354 * @data: private data
356 * Service each initialized channel, if needed
358 static void dim2_tasklet_fn(unsigned long data)
360 struct dim2_hdm *dev = (struct dim2_hdm *)data;
364 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
365 if (!dev->hch[ch_idx].is_initialized)
368 spin_lock_irqsave(&dim_lock, flags);
369 dim_service_channel(&dev->hch[ch_idx].ch);
370 spin_unlock_irqrestore(&dim_lock, flags);
372 service_done_flag(dev, ch_idx);
373 while (!try_start_dim_transfer(dev->hch + ch_idx))
379 * dim2_ahb_isr - interrupt service routine
381 * @_dev: private data
383 * Acknowledge the interrupt and schedule a tasklet to service channels.
384 * Return IRQ_HANDLED.
386 static irqreturn_t dim2_ahb_isr(int irq, void *_dev)
388 struct dim2_hdm *dev = _dev;
389 struct dim_channel *buffer[DMA_CHANNELS + 1];
392 spin_lock_irqsave(&dim_lock, flags);
393 dim_service_ahb_int_irq(get_active_channels(dev, buffer));
394 spin_unlock_irqrestore(&dim_lock, flags);
396 dim2_tasklet.data = (unsigned long)dev;
397 tasklet_schedule(&dim2_tasklet);
402 * complete_all_mbos - complete MBO's in a list
405 * Delete all the entries in list and return back MBO's to mostcore using
406 * completion call back.
408 static void complete_all_mbos(struct list_head *head)
414 spin_lock_irqsave(&dim_lock, flags);
415 if (list_empty(head)) {
416 spin_unlock_irqrestore(&dim_lock, flags);
420 mbo = list_first_entry(head, struct mbo, list);
421 list_del(head->next);
422 spin_unlock_irqrestore(&dim_lock, flags);
424 mbo->processed_length = 0;
425 mbo->status = MBO_E_CLOSE;
431 * configure_channel - initialize a channel
432 * @iface: interface the channel belongs to
433 * @channel: channel to be configured
434 * @channel_config: structure that holds the configuration information
436 * Receives configuration information from mostcore and initialize
437 * the corresponding channel. Return 0 on success, negative on failure.
439 static int configure_channel(struct most_interface *most_iface, int ch_idx,
440 struct most_channel_config *ccfg)
442 struct dim2_hdm *dev = iface_to_hdm(most_iface);
443 bool const is_tx = ccfg->direction == MOST_CH_TX;
444 u16 const sub_size = ccfg->subbuffer_size;
445 u16 const buf_size = ccfg->buffer_size;
449 int const ch_addr = ch_idx * 2 + 2;
450 struct hdm_channel *const hdm_ch = dev->hch + ch_idx;
452 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
454 if (hdm_ch->is_initialized)
457 /* do not reset if the property was set by user, see poison_channel */
458 hdm_ch->reset_dbr_size = ccfg->dbr_size ? NULL : &ccfg->dbr_size;
460 /* zero value is default dbr_size, see dim2 hal */
461 hdm_ch->ch.dbr_size = ccfg->dbr_size;
463 switch (ccfg->data_type) {
464 case MOST_CH_CONTROL:
465 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
467 pr_err("%s: too small buffer size\n", hdm_ch->name);
470 ccfg->buffer_size = new_size;
471 if (new_size != buf_size)
472 pr_warn("%s: fixed buffer size (%d -> %d)\n",
473 hdm_ch->name, buf_size, new_size);
474 spin_lock_irqsave(&dim_lock, flags);
475 hal_ret = dim_init_control(&hdm_ch->ch, is_tx, ch_addr,
476 is_tx ? new_size * 2 : new_size);
479 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
481 pr_err("%s: too small buffer size\n", hdm_ch->name);
484 ccfg->buffer_size = new_size;
485 if (new_size != buf_size)
486 pr_warn("%s: fixed buffer size (%d -> %d)\n",
487 hdm_ch->name, buf_size, new_size);
488 spin_lock_irqsave(&dim_lock, flags);
489 hal_ret = dim_init_async(&hdm_ch->ch, is_tx, ch_addr,
490 is_tx ? new_size * 2 : new_size);
493 new_size = dim_norm_isoc_buffer_size(buf_size, sub_size);
495 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
499 ccfg->buffer_size = new_size;
500 if (new_size != buf_size)
501 pr_warn("%s: fixed buffer size (%d -> %d)\n",
502 hdm_ch->name, buf_size, new_size);
503 spin_lock_irqsave(&dim_lock, flags);
504 hal_ret = dim_init_isoc(&hdm_ch->ch, is_tx, ch_addr, sub_size);
507 new_size = dim_norm_sync_buffer_size(buf_size, sub_size);
509 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
513 ccfg->buffer_size = new_size;
514 if (new_size != buf_size)
515 pr_warn("%s: fixed buffer size (%d -> %d)\n",
516 hdm_ch->name, buf_size, new_size);
517 spin_lock_irqsave(&dim_lock, flags);
518 hal_ret = dim_init_sync(&hdm_ch->ch, is_tx, ch_addr, sub_size);
521 pr_err("%s: configure failed, bad channel type: %d\n",
522 hdm_ch->name, ccfg->data_type);
526 if (hal_ret != DIM_NO_ERROR) {
527 spin_unlock_irqrestore(&dim_lock, flags);
528 pr_err("%s: configure failed (%d), type: %d, is_tx: %d\n",
529 hdm_ch->name, hal_ret, ccfg->data_type, (int)is_tx);
533 hdm_ch->data_type = ccfg->data_type;
534 hdm_ch->direction = ccfg->direction;
535 hdm_ch->is_initialized = true;
537 if (hdm_ch->data_type == MOST_CH_ASYNC &&
538 hdm_ch->direction == MOST_CH_TX &&
540 dev->atx_idx = ch_idx;
542 spin_unlock_irqrestore(&dim_lock, flags);
543 ccfg->dbr_size = hdm_ch->ch.dbr_size;
549 * enqueue - enqueue a buffer for data transfer
550 * @iface: intended interface
551 * @channel: ID of the channel the buffer is intended for
552 * @mbo: pointer to the buffer object
554 * Push the buffer into pending_list and try to transfer one buffer from
555 * pending_list. Return 0 on success, negative on failure.
557 static int enqueue(struct most_interface *most_iface, int ch_idx,
560 struct dim2_hdm *dev = iface_to_hdm(most_iface);
561 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
564 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
566 if (!hdm_ch->is_initialized)
569 if (mbo->bus_address == 0)
572 spin_lock_irqsave(&dim_lock, flags);
573 list_add_tail(&mbo->list, &hdm_ch->pending_list);
574 spin_unlock_irqrestore(&dim_lock, flags);
576 (void)try_start_dim_transfer(hdm_ch);
582 * request_netinfo - triggers retrieving of network info
583 * @iface: pointer to the interface
584 * @channel_id: corresponding channel ID
586 * Send a command to INIC which triggers retrieving of network info by means of
587 * "Message exchange over MDP/MEP". Return 0 on success, negative on failure.
589 static void request_netinfo(struct most_interface *most_iface, int ch_idx,
590 void (*on_netinfo)(struct most_interface *,
591 unsigned char, unsigned char *))
593 struct dim2_hdm *dev = iface_to_hdm(most_iface);
597 dev->on_netinfo = on_netinfo;
601 if (dev->atx_idx < 0) {
602 pr_err("Async Tx Not initialized\n");
606 mbo = most_get_mbo(&dev->most_iface, dev->atx_idx, NULL);
610 mbo->buffer_length = 5;
612 data = mbo->virt_address;
614 data[0] = 0x00; /* PML High byte */
615 data[1] = 0x03; /* PML Low byte */
616 data[2] = 0x02; /* PMHL */
617 data[3] = 0x08; /* FPH */
618 data[4] = 0x40; /* FMF (FIFO cmd msg - Triggers NAOverMDP) */
620 most_submit_mbo(mbo);
624 * poison_channel - poison buffers of a channel
625 * @iface: pointer to the interface the channel to be poisoned belongs to
626 * @channel_id: corresponding channel ID
628 * Destroy a channel and complete all the buffers in both started_list &
629 * pending_list. Return 0 on success, negative on failure.
631 static int poison_channel(struct most_interface *most_iface, int ch_idx)
633 struct dim2_hdm *dev = iface_to_hdm(most_iface);
634 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
639 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
641 if (!hdm_ch->is_initialized)
644 tasklet_disable(&dim2_tasklet);
645 spin_lock_irqsave(&dim_lock, flags);
646 hal_ret = dim_destroy_channel(&hdm_ch->ch);
647 hdm_ch->is_initialized = false;
648 if (ch_idx == dev->atx_idx)
650 spin_unlock_irqrestore(&dim_lock, flags);
651 tasklet_enable(&dim2_tasklet);
652 if (hal_ret != DIM_NO_ERROR) {
653 pr_err("HAL Failed to close channel %s\n", hdm_ch->name);
657 complete_all_mbos(&hdm_ch->started_list);
658 complete_all_mbos(&hdm_ch->pending_list);
659 if (hdm_ch->reset_dbr_size)
660 *hdm_ch->reset_dbr_size = 0;
665 static void *dma_alloc(struct mbo *mbo, u32 size)
667 struct device *dev = mbo->ifp->driver_dev;
669 return dma_alloc_coherent(dev, size, &mbo->bus_address, GFP_KERNEL);
672 static void dma_free(struct mbo *mbo, u32 size)
674 struct device *dev = mbo->ifp->driver_dev;
676 dma_free_coherent(dev, size, mbo->virt_address, mbo->bus_address);
679 static const struct of_device_id dim2_of_match[];
682 const char *clock_speed;
685 { "256fs", CLK_256FS },
686 { "512fs", CLK_512FS },
687 { "1024fs", CLK_1024FS },
688 { "2048fs", CLK_2048FS },
689 { "3072fs", CLK_3072FS },
690 { "4096fs", CLK_4096FS },
691 { "6144fs", CLK_6144FS },
692 { "8192fs", CLK_8192FS },
696 * get_dim2_clk_speed - converts string to DIM2 clock speed value
698 * @clock_speed: string in the format "{NUMBER}fs"
699 * @val: pointer to get one of the CLK_{NUMBER}FS values
701 * By success stores one of the CLK_{NUMBER}FS in the *val and returns 0,
702 * otherwise returns -EINVAL.
704 static int get_dim2_clk_speed(const char *clock_speed, u8 *val)
708 for (i = 0; i < ARRAY_SIZE(clk_mt); i++) {
709 if (!strcmp(clock_speed, clk_mt[i].clock_speed)) {
710 *val = clk_mt[i].clk_speed;
718 * dim2_probe - dim2 probe handler
719 * @pdev: platform device structure
721 * Register the dim2 interface with mostcore and initialize it.
722 * Return 0 on success, negative on failure.
724 static int dim2_probe(struct platform_device *pdev)
726 const struct dim2_platform_data *pdata;
727 const struct of_device_id *of_id;
728 const char *clock_speed;
729 struct dim2_hdm *dev;
730 struct resource *res;
735 enum { MLB_INT_IDX, AHB0_INT_IDX };
737 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
743 platform_set_drvdata(pdev, dev);
745 ret = of_property_read_string(pdev->dev.of_node,
746 "microchip,clock-speed", &clock_speed);
748 dev_err(&pdev->dev, "missing dt property clock-speed\n");
752 ret = get_dim2_clk_speed(clock_speed, &dev->clk_speed);
754 dev_err(&pdev->dev, "bad dt property clock-speed\n");
758 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
759 dev->io_base = devm_ioremap_resource(&pdev->dev, res);
760 if (IS_ERR(dev->io_base))
761 return PTR_ERR(dev->io_base);
763 of_id = of_match_node(dim2_of_match, pdev->dev.of_node);
765 ret = pdata && pdata->enable ? pdata->enable(pdev) : 0;
769 dev->disable_platform = pdata ? pdata->disable : NULL;
771 dev_info(&pdev->dev, "sync: num of frames per sub-buffer: %u\n", fcnt);
772 hal_ret = dim_startup(dev->io_base, dev->clk_speed, fcnt);
773 if (hal_ret != DIM_NO_ERROR) {
774 dev_err(&pdev->dev, "dim_startup failed: %d\n", hal_ret);
776 goto err_disable_platform;
779 irq = platform_get_irq(pdev, AHB0_INT_IDX);
782 goto err_shutdown_dim;
785 ret = devm_request_irq(&pdev->dev, irq, dim2_ahb_isr, 0,
786 "dim2_ahb0_int", dev);
788 dev_err(&pdev->dev, "failed to request ahb0_int irq %d\n", irq);
789 goto err_shutdown_dim;
792 irq = platform_get_irq(pdev, MLB_INT_IDX);
795 goto err_shutdown_dim;
798 ret = devm_request_irq(&pdev->dev, irq, dim2_mlb_isr, 0,
799 "dim2_mlb_int", dev);
801 dev_err(&pdev->dev, "failed to request mlb_int irq %d\n", irq);
802 goto err_shutdown_dim;
805 init_waitqueue_head(&dev->netinfo_waitq);
806 dev->deliver_netinfo = 0;
807 dev->netinfo_task = kthread_run(&deliver_netinfo_thread, dev,
809 if (IS_ERR(dev->netinfo_task)) {
810 ret = PTR_ERR(dev->netinfo_task);
811 goto err_shutdown_dim;
814 for (i = 0; i < DMA_CHANNELS; i++) {
815 struct most_channel_capability *cap = dev->capabilities + i;
816 struct hdm_channel *hdm_ch = dev->hch + i;
818 INIT_LIST_HEAD(&hdm_ch->pending_list);
819 INIT_LIST_HEAD(&hdm_ch->started_list);
820 hdm_ch->is_initialized = false;
821 snprintf(hdm_ch->name, sizeof(hdm_ch->name), "ca%d", i * 2 + 2);
823 cap->name_suffix = hdm_ch->name;
824 cap->direction = MOST_CH_RX | MOST_CH_TX;
825 cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
826 MOST_CH_ISOC | MOST_CH_SYNC;
827 cap->num_buffers_packet = MAX_BUFFERS_PACKET;
828 cap->buffer_size_packet = MAX_BUF_SIZE_PACKET;
829 cap->num_buffers_streaming = MAX_BUFFERS_STREAMING;
830 cap->buffer_size_streaming = MAX_BUF_SIZE_STREAMING;
836 if (sizeof(res->start) == sizeof(long long))
837 fmt = "dim2-%016llx";
838 else if (sizeof(res->start) == sizeof(long))
843 snprintf(dev->name, sizeof(dev->name), fmt, res->start);
846 dev->most_iface.interface = ITYPE_MEDIALB_DIM2;
847 dev->most_iface.description = dev->name;
848 dev->most_iface.num_channels = DMA_CHANNELS;
849 dev->most_iface.channel_vector = dev->capabilities;
850 dev->most_iface.configure = configure_channel;
851 dev->most_iface.enqueue = enqueue;
852 dev->most_iface.dma_alloc = dma_alloc;
853 dev->most_iface.dma_free = dma_free;
854 dev->most_iface.poison_channel = poison_channel;
855 dev->most_iface.request_netinfo = request_netinfo;
856 dev->most_iface.driver_dev = &pdev->dev;
857 dev->dev.init_name = "dim2_state";
858 dev->dev.parent = &dev->most_iface.dev;
860 ret = most_register_interface(&dev->most_iface);
862 dev_err(&pdev->dev, "failed to register MOST interface\n");
863 goto err_stop_thread;
866 ret = dim2_sysfs_probe(&dev->dev);
868 dev_err(&pdev->dev, "failed to create sysfs attribute\n");
869 goto err_unreg_iface;
875 most_deregister_interface(&dev->most_iface);
877 kthread_stop(dev->netinfo_task);
880 err_disable_platform:
881 if (dev->disable_platform)
882 dev->disable_platform(pdev);
888 * dim2_remove - dim2 remove handler
889 * @pdev: platform device structure
891 * Unregister the interface from mostcore
893 static int dim2_remove(struct platform_device *pdev)
895 struct dim2_hdm *dev = platform_get_drvdata(pdev);
898 dim2_sysfs_destroy(&dev->dev);
899 most_deregister_interface(&dev->most_iface);
900 kthread_stop(dev->netinfo_task);
902 spin_lock_irqsave(&dim_lock, flags);
904 spin_unlock_irqrestore(&dim_lock, flags);
906 if (dev->disable_platform)
907 dev->disable_platform(pdev);
912 /* platform specific functions [[ */
914 static int fsl_mx6_enable(struct platform_device *pdev)
916 struct dim2_hdm *dev = platform_get_drvdata(pdev);
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");
925 ret = clk_prepare_enable(dev->clk);
927 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
931 if (dev->clk_speed >= CLK_2048FS) {
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);
940 writel(0x888, dev->io_base + 0x38);
941 clk_prepare_enable(dev->clk_pll);
947 static void fsl_mx6_disable(struct platform_device *pdev)
949 struct dim2_hdm *dev = platform_get_drvdata(pdev);
951 if (dev->clk_speed >= CLK_2048FS)
952 clk_disable_unprepare(dev->clk_pll);
954 clk_disable_unprepare(dev->clk);
957 static int rcar_h2_enable(struct platform_device *pdev)
959 struct dim2_hdm *dev = platform_get_drvdata(pdev);
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);
968 ret = clk_prepare_enable(dev->clk);
970 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
974 if (dev->clk_speed >= CLK_2048FS) {
975 /* enable MLP pll and LVDS drivers */
976 writel(0x03, dev->io_base + 0x600);
978 writel(0x888, dev->io_base + 0x38);
981 writel(0x04, dev->io_base + 0x600);
986 writel(0x03, dev->io_base + 0x500);
987 writel(0x0002FF02, dev->io_base + 0x508);
992 static void rcar_h2_disable(struct platform_device *pdev)
994 struct dim2_hdm *dev = platform_get_drvdata(pdev);
996 clk_disable_unprepare(dev->clk);
998 /* disable PLLs and LVDS drivers */
999 writel(0x0, dev->io_base + 0x600);
1002 static int rcar_m3_enable(struct platform_device *pdev)
1004 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1005 u32 enable_512fs = dev->clk_speed == CLK_512FS;
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);
1014 ret = clk_prepare_enable(dev->clk);
1016 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
1021 writel(0x04, dev->io_base + 0x600);
1023 writel(enable_512fs, dev->io_base + 0x604);
1026 writel(0x03, dev->io_base + 0x500);
1027 writel(0x0002FF02, dev->io_base + 0x508);
1032 static void rcar_m3_disable(struct platform_device *pdev)
1034 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1036 clk_disable_unprepare(dev->clk);
1038 /* disable PLLs and LVDS drivers */
1039 writel(0x0, dev->io_base + 0x600);
1042 /* ]] platform specific functions */
1044 enum dim2_platforms { FSL_MX6, RCAR_H2, RCAR_M3 };
1046 static 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 },
1052 static const struct of_device_id dim2_of_match[] = {
1054 .compatible = "fsl,imx6q-mlb150",
1055 .data = plat_data + FSL_MX6
1058 .compatible = "renesas,mlp",
1059 .data = plat_data + RCAR_H2
1062 .compatible = "rcar,medialb-dim2",
1063 .data = plat_data + RCAR_M3
1066 .compatible = "xlnx,axi4-os62420_3pin-1.00.a",
1069 .compatible = "xlnx,axi4-os62420_6pin-1.00.a",
1074 MODULE_DEVICE_TABLE(of, dim2_of_match);
1076 static struct platform_driver dim2_driver = {
1077 .probe = dim2_probe,
1078 .remove = dim2_remove,
1081 .of_match_table = dim2_of_match,
1085 module_platform_driver(dim2_driver);
1087 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
1088 MODULE_DESCRIPTION("MediaLB DIM2 Hardware Dependent Module");
1089 MODULE_LICENSE("GPL");