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>
23 #include <linux/most.h>
28 #define DMA_CHANNELS (32 - 1) /* channel 0 is a system channel */
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)
36 * The parameter representing the number of frames per sub-buffer for
37 * synchronous channels. Valid values: [0 .. 6].
39 * The values 0, 1, 2, 3, 4, 5, 6 represent corresponding number of frames per
40 * sub-buffer 1, 2, 4, 8, 16, 32, 64.
42 static u8 fcnt = 4; /* (1 << fcnt) frames per subbuffer */
43 module_param(fcnt, byte, 0000);
44 MODULE_PARM_DESC(fcnt, "Num of frames per sub-buffer for sync channels as a power of 2");
46 static DEFINE_SPINLOCK(dim_lock);
48 static void dim2_tasklet_fn(unsigned long data);
49 static DECLARE_TASKLET_OLD(dim2_tasklet, dim2_tasklet_fn);
52 * struct hdm_channel - private structure to keep channel specific data
53 * @is_initialized: identifier to know whether the channel is initialized
54 * @ch: HAL specific channel data
55 * @pending_list: list to keep MBO's before starting transfer
56 * @started_list: list to keep MBO's after starting transfer
57 * @direction: channel direction (TX or RX)
58 * @data_type: channel data type
61 char name[sizeof "caNNN"];
63 struct dim_channel ch;
65 struct list_head pending_list; /* before dim_enqueue_buffer() */
66 struct list_head started_list; /* after dim_enqueue_buffer() */
67 enum most_channel_direction direction;
68 enum most_channel_data_type data_type;
72 * struct dim2_hdm - private structure to keep interface specific data
73 * @hch: an array of channel specific data
74 * @most_iface: most interface structure
75 * @capabilities: an array of channel capability data
76 * @io_base: I/O register base address
77 * @netinfo_task: thread to deliver network status
78 * @netinfo_waitq: waitq for the thread to sleep
79 * @deliver_netinfo: to identify whether network status received
80 * @mac_addrs: INIC mac address
81 * @link_state: network link state
82 * @atx_idx: index of async tx channel
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;
94 struct task_struct *netinfo_task;
95 wait_queue_head_t netinfo_waitq;
97 unsigned char mac_addrs[6];
98 unsigned char link_state;
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 *pdev);
106 struct dim2_platform_data {
107 int (*enable)(struct platform_device *pdev);
108 void (*disable)(struct platform_device *pdev);
111 #define iface_to_hdm(iface) container_of(iface, struct dim2_hdm, most_iface)
113 /* Macro to identify a network status message */
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))
118 bool dim2_sysfs_get_state_cb(void)
123 spin_lock_irqsave(&dim_lock, flags);
124 state = dim_get_lock_state();
125 spin_unlock_irqrestore(&dim_lock, flags);
131 * dimcb_on_error - callback from HAL to report miscommunication between
133 * @error_id: Error ID
134 * @error_message: Error message. Some text in a free format
136 void dimcb_on_error(u8 error_id, const char *error_message)
138 pr_err("%s: error_id - %d, error_message - %s\n", __func__, error_id,
143 * try_start_dim_transfer - try to transfer a buffer on a channel
144 * @hdm_ch: channel specific data
146 * Transfer a buffer from pending_list if the channel is ready
148 static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
151 struct list_head *head = &hdm_ch->pending_list;
154 struct dim_ch_state_t st;
157 BUG_ON(!hdm_ch->is_initialized);
159 spin_lock_irqsave(&dim_lock, flags);
160 if (list_empty(head)) {
161 spin_unlock_irqrestore(&dim_lock, flags);
165 if (!dim_get_channel_state(&hdm_ch->ch, &st)->ready) {
166 spin_unlock_irqrestore(&dim_lock, flags);
170 mbo = list_first_entry(head, struct mbo, list);
171 buf_size = mbo->buffer_length;
173 if (dim_dbr_space(&hdm_ch->ch) < buf_size) {
174 spin_unlock_irqrestore(&dim_lock, flags);
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;
188 list_move_tail(head->next, &hdm_ch->started_list);
189 spin_unlock_irqrestore(&dim_lock, flags);
195 * deliver_netinfo_thread - thread to deliver network status to mostcore
196 * @data: private data
198 * Wait for network status and deliver it to mostcore once it is received
200 static int deliver_netinfo_thread(void *data)
202 struct dim2_hdm *dev = data;
204 while (!kthread_should_stop()) {
205 wait_event_interruptible(dev->netinfo_waitq,
206 dev->deliver_netinfo ||
207 kthread_should_stop());
209 if (dev->deliver_netinfo) {
210 dev->deliver_netinfo--;
211 if (dev->on_netinfo) {
212 dev->on_netinfo(&dev->most_iface,
223 * retrieve_netinfo - retrieve network status from received buffer
227 * Parse the message in buffer and get node address, link state, MAC address.
228 * Wake up a thread to deliver this status to mostcore
230 static void retrieve_netinfo(struct dim2_hdm *dev, struct mbo *mbo)
232 u8 *data = mbo->virt_address;
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);
243 * service_done_flag - handle completed buffers
245 * @ch_idx: channel index
247 * Return back the completed buffers to mostcore, using completion callback
249 static void service_done_flag(struct dim2_hdm *dev, int ch_idx)
251 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
252 struct dim_ch_state_t st;
253 struct list_head *head;
260 BUG_ON(!hdm_ch->is_initialized);
262 spin_lock_irqsave(&dim_lock, flags);
264 done_buffers = dim_get_channel_state(&hdm_ch->ch, &st)->done_buffers;
266 spin_unlock_irqrestore(&dim_lock, flags);
270 if (!dim_detach_buffers(&hdm_ch->ch, done_buffers)) {
271 spin_unlock_irqrestore(&dim_lock, flags);
274 spin_unlock_irqrestore(&dim_lock, flags);
276 head = &hdm_ch->started_list;
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");
286 mbo = list_first_entry(head, struct mbo, list);
287 list_del(head->next);
288 spin_unlock_irqrestore(&dim_lock, flags);
290 data = mbo->virt_address;
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);
297 spin_lock_irqsave(&dim_lock, flags);
298 list_add_tail(&mbo->list, &hdm_ch->pending_list);
299 spin_unlock_irqrestore(&dim_lock, flags);
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;
306 mbo->processed_length =
307 min_t(u32, data_size,
310 mbo->processed_length = mbo->buffer_length;
312 mbo->status = MBO_SUCCESS;
320 static struct dim_channel **get_active_channels(struct dim2_hdm *dev,
321 struct dim_channel **buffer)
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;
330 buffer[idx++] = NULL;
335 static irqreturn_t dim2_mlb_isr(int irq, void *_dev)
337 struct dim2_hdm *dev = _dev;
340 spin_lock_irqsave(&dim_lock, flags);
341 dim_service_mlb_int_irq();
342 spin_unlock_irqrestore(&dim_lock, flags);
344 if (dev->atx_idx >= 0 && dev->hch[dev->atx_idx].is_initialized)
345 while (!try_start_dim_transfer(dev->hch + dev->atx_idx))
352 * dim2_tasklet_fn - tasklet function
353 * @data: private data
355 * Service each initialized channel, if needed
357 static void dim2_tasklet_fn(unsigned long data)
359 struct dim2_hdm *dev = (struct dim2_hdm *)data;
363 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
364 if (!dev->hch[ch_idx].is_initialized)
367 spin_lock_irqsave(&dim_lock, flags);
368 dim_service_channel(&dev->hch[ch_idx].ch);
369 spin_unlock_irqrestore(&dim_lock, flags);
371 service_done_flag(dev, ch_idx);
372 while (!try_start_dim_transfer(dev->hch + ch_idx))
378 * dim2_ahb_isr - interrupt service routine
380 * @_dev: private data
382 * Acknowledge the interrupt and schedule a tasklet to service channels.
383 * Return IRQ_HANDLED.
385 static irqreturn_t dim2_ahb_isr(int irq, void *_dev)
387 struct dim2_hdm *dev = _dev;
388 struct dim_channel *buffer[DMA_CHANNELS + 1];
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);
395 dim2_tasklet.data = (unsigned long)dev;
396 tasklet_schedule(&dim2_tasklet);
401 * complete_all_mbos - complete MBO's in a list
404 * Delete all the entries in list and return back MBO's to mostcore using
405 * completion call back.
407 static void complete_all_mbos(struct list_head *head)
413 spin_lock_irqsave(&dim_lock, flags);
414 if (list_empty(head)) {
415 spin_unlock_irqrestore(&dim_lock, flags);
419 mbo = list_first_entry(head, struct mbo, list);
420 list_del(head->next);
421 spin_unlock_irqrestore(&dim_lock, flags);
423 mbo->processed_length = 0;
424 mbo->status = MBO_E_CLOSE;
430 * configure_channel - initialize a channel
431 * @iface: interface the channel belongs to
432 * @channel: channel to be configured
433 * @channel_config: structure that holds the configuration information
435 * Receives configuration information from mostcore and initialize
436 * the corresponding channel. Return 0 on success, negative on failure.
438 static int configure_channel(struct most_interface *most_iface, int ch_idx,
439 struct most_channel_config *ccfg)
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;
448 int const ch_addr = ch_idx * 2 + 2;
449 struct hdm_channel *const hdm_ch = dev->hch + ch_idx;
451 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
453 if (hdm_ch->is_initialized)
456 /* do not reset if the property was set by user, see poison_channel */
457 hdm_ch->reset_dbr_size = ccfg->dbr_size ? NULL : &ccfg->dbr_size;
459 /* zero value is default dbr_size, see dim2 hal */
460 hdm_ch->ch.dbr_size = ccfg->dbr_size;
462 switch (ccfg->data_type) {
463 case MOST_CH_CONTROL:
464 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
466 pr_err("%s: too small buffer size\n", hdm_ch->name);
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);
478 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
480 pr_err("%s: too small buffer size\n", hdm_ch->name);
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);
492 new_size = dim_norm_isoc_buffer_size(buf_size, sub_size);
494 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
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);
506 new_size = dim_norm_sync_buffer_size(buf_size, sub_size);
508 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
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);
520 pr_err("%s: configure failed, bad channel type: %d\n",
521 hdm_ch->name, ccfg->data_type);
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);
532 hdm_ch->data_type = ccfg->data_type;
533 hdm_ch->direction = ccfg->direction;
534 hdm_ch->is_initialized = true;
536 if (hdm_ch->data_type == MOST_CH_ASYNC &&
537 hdm_ch->direction == MOST_CH_TX &&
539 dev->atx_idx = ch_idx;
541 spin_unlock_irqrestore(&dim_lock, flags);
542 ccfg->dbr_size = hdm_ch->ch.dbr_size;
548 * enqueue - enqueue a buffer for data transfer
549 * @iface: intended interface
550 * @channel: ID of the channel the buffer is intended for
551 * @mbo: pointer to the buffer object
553 * Push the buffer into pending_list and try to transfer one buffer from
554 * pending_list. Return 0 on success, negative on failure.
556 static int enqueue(struct most_interface *most_iface, int ch_idx,
559 struct dim2_hdm *dev = iface_to_hdm(most_iface);
560 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
563 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
565 if (!hdm_ch->is_initialized)
568 if (mbo->bus_address == 0)
571 spin_lock_irqsave(&dim_lock, flags);
572 list_add_tail(&mbo->list, &hdm_ch->pending_list);
573 spin_unlock_irqrestore(&dim_lock, flags);
575 (void)try_start_dim_transfer(hdm_ch);
581 * request_netinfo - triggers retrieving of network info
582 * @iface: pointer to the interface
583 * @channel_id: corresponding channel ID
585 * Send a command to INIC which triggers retrieving of network info by means of
586 * "Message exchange over MDP/MEP". Return 0 on success, negative on failure.
588 static void request_netinfo(struct most_interface *most_iface, int ch_idx,
589 void (*on_netinfo)(struct most_interface *,
590 unsigned char, unsigned char *))
592 struct dim2_hdm *dev = iface_to_hdm(most_iface);
596 dev->on_netinfo = on_netinfo;
600 if (dev->atx_idx < 0) {
601 pr_err("Async Tx Not initialized\n");
605 mbo = most_get_mbo(&dev->most_iface, dev->atx_idx, NULL);
609 mbo->buffer_length = 5;
611 data = mbo->virt_address;
613 data[0] = 0x00; /* PML High byte */
614 data[1] = 0x03; /* PML Low byte */
615 data[2] = 0x02; /* PMHL */
616 data[3] = 0x08; /* FPH */
617 data[4] = 0x40; /* FMF (FIFO cmd msg - Triggers NAOverMDP) */
619 most_submit_mbo(mbo);
623 * poison_channel - poison buffers of a channel
624 * @iface: pointer to the interface the channel to be poisoned belongs to
625 * @channel_id: corresponding channel ID
627 * Destroy a channel and complete all the buffers in both started_list &
628 * pending_list. Return 0 on success, negative on failure.
630 static int poison_channel(struct most_interface *most_iface, int ch_idx)
632 struct dim2_hdm *dev = iface_to_hdm(most_iface);
633 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
638 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
640 if (!hdm_ch->is_initialized)
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)
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);
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;
664 static void *dma_alloc(struct mbo *mbo, u32 size)
666 struct device *dev = mbo->ifp->driver_dev;
668 return dma_alloc_coherent(dev, size, &mbo->bus_address, GFP_KERNEL);
671 static void dma_free(struct mbo *mbo, u32 size)
673 struct device *dev = mbo->ifp->driver_dev;
675 dma_free_coherent(dev, size, mbo->virt_address, mbo->bus_address);
678 static const struct of_device_id dim2_of_match[];
681 const char *clock_speed;
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 },
695 * get_dim2_clk_speed - converts string to DIM2 clock speed value
697 * @clock_speed: string in the format "{NUMBER}fs"
698 * @val: pointer to get one of the CLK_{NUMBER}FS values
700 * By success stores one of the CLK_{NUMBER}FS in the *val and returns 0,
701 * otherwise returns -EINVAL.
703 static int get_dim2_clk_speed(const char *clock_speed, u8 *val)
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;
717 * dim2_probe - dim2 probe handler
718 * @pdev: platform device structure
720 * Register the dim2 interface with mostcore and initialize it.
721 * Return 0 on success, negative on failure.
723 static int dim2_probe(struct platform_device *pdev)
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;
734 enum { MLB_INT_IDX, AHB0_INT_IDX };
736 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
742 platform_set_drvdata(pdev, dev);
744 ret = of_property_read_string(pdev->dev.of_node,
745 "microchip,clock-speed", &clock_speed);
747 dev_err(&pdev->dev, "missing dt property clock-speed\n");
751 ret = get_dim2_clk_speed(clock_speed, &dev->clk_speed);
753 dev_err(&pdev->dev, "bad dt property clock-speed\n");
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);
762 of_id = of_match_node(dim2_of_match, pdev->dev.of_node);
764 ret = pdata && pdata->enable ? pdata->enable(pdev) : 0;
768 dev->disable_platform = pdata ? pdata->disable : NULL;
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);
775 goto err_disable_platform;
778 irq = platform_get_irq(pdev, AHB0_INT_IDX);
781 goto err_shutdown_dim;
784 ret = devm_request_irq(&pdev->dev, irq, dim2_ahb_isr, 0,
785 "dim2_ahb0_int", dev);
787 dev_err(&pdev->dev, "failed to request ahb0_int irq %d\n", irq);
788 goto err_shutdown_dim;
791 irq = platform_get_irq(pdev, MLB_INT_IDX);
794 goto err_shutdown_dim;
797 ret = devm_request_irq(&pdev->dev, irq, dim2_mlb_isr, 0,
798 "dim2_mlb_int", dev);
800 dev_err(&pdev->dev, "failed to request mlb_int irq %d\n", irq);
801 goto err_shutdown_dim;
804 init_waitqueue_head(&dev->netinfo_waitq);
805 dev->deliver_netinfo = 0;
806 dev->netinfo_task = kthread_run(&deliver_netinfo_thread, dev,
808 if (IS_ERR(dev->netinfo_task)) {
809 ret = PTR_ERR(dev->netinfo_task);
810 goto err_shutdown_dim;
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;
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);
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;
835 if (sizeof(res->start) == sizeof(long long))
836 fmt = "dim2-%016llx";
837 else if (sizeof(res->start) == sizeof(long))
842 snprintf(dev->name, sizeof(dev->name), fmt, res->start);
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;
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);
1088 MODULE_DESCRIPTION("MediaLB DIM2 Hardware Dependent Module");
1089 MODULE_LICENSE("GPL");