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);
49 * struct hdm_channel - private structure to keep channel specific data
51 * @is_initialized: identifier to know whether the channel is initialized
52 * @ch: HAL specific channel data
53 * @reset_dbr_size: reset DBR data buffer size
54 * @pending_list: list to keep MBO's before starting transfer
55 * @started_list: list to keep MBO's after starting transfer
56 * @direction: channel direction (TX or RX)
57 * @data_type: channel data type
60 char name[sizeof "caNNN"];
62 struct dim_channel ch;
64 struct list_head pending_list; /* before dim_enqueue_buffer() */
65 struct list_head started_list; /* after dim_enqueue_buffer() */
66 enum most_channel_direction direction;
67 enum most_channel_data_type data_type;
71 * struct dim2_hdm - private structure to keep interface specific data
72 * @hch: an array of channel specific data
73 * @most_iface: most interface structure
74 * @capabilities: an array of channel capability data
75 * @io_base: I/O register base address
76 * @netinfo_task: thread to deliver network status
77 * @netinfo_waitq: waitq for the thread to sleep
78 * @deliver_netinfo: to identify whether network status received
79 * @mac_addrs: INIC mac address
80 * @link_state: network link state
81 * @atx_idx: index of async tx channel
85 struct hdm_channel hch[DMA_CHANNELS];
86 struct most_channel_capability capabilities[DMA_CHANNELS];
87 struct most_interface most_iface;
88 char name[16 + sizeof "dim2-"];
89 void __iomem *io_base;
93 struct task_struct *netinfo_task;
94 wait_queue_head_t netinfo_waitq;
96 unsigned char mac_addrs[6];
97 unsigned char link_state;
99 struct medialb_bus bus;
100 void (*on_netinfo)(struct most_interface *most_iface,
101 unsigned char link_state, unsigned char *addrs);
102 void (*disable_platform)(struct platform_device *pdev);
105 struct dim2_platform_data {
106 int (*enable)(struct platform_device *pdev);
107 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 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
124 spin_lock_irqsave(&dim_lock, flags);
125 state = dim_get_lock_state();
126 spin_unlock_irqrestore(&dim_lock, flags);
128 return sysfs_emit(buf, "%s\n", state ? "locked" : "");
131 static DEVICE_ATTR_RO(state);
133 static struct attribute *dim2_attrs[] = {
134 &dev_attr_state.attr,
138 ATTRIBUTE_GROUPS(dim2);
141 * dimcb_on_error - callback from HAL to report miscommunication between
143 * @error_id: Error ID
144 * @error_message: Error message. Some text in a free format
146 void dimcb_on_error(u8 error_id, const char *error_message)
148 pr_err("%s: error_id - %d, error_message - %s\n", __func__, error_id,
153 * try_start_dim_transfer - try to transfer a buffer on a channel
154 * @hdm_ch: channel specific data
156 * Transfer a buffer from pending_list if the channel is ready
158 static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
161 struct list_head *head = &hdm_ch->pending_list;
164 struct dim_ch_state_t st;
167 BUG_ON(!hdm_ch->is_initialized);
169 spin_lock_irqsave(&dim_lock, flags);
170 if (list_empty(head)) {
171 spin_unlock_irqrestore(&dim_lock, flags);
175 if (!dim_get_channel_state(&hdm_ch->ch, &st)->ready) {
176 spin_unlock_irqrestore(&dim_lock, flags);
180 mbo = list_first_entry(head, struct mbo, list);
181 buf_size = mbo->buffer_length;
183 if (dim_dbr_space(&hdm_ch->ch) < buf_size) {
184 spin_unlock_irqrestore(&dim_lock, flags);
188 BUG_ON(mbo->bus_address == 0);
189 if (!dim_enqueue_buffer(&hdm_ch->ch, mbo->bus_address, buf_size)) {
190 list_del(head->next);
191 spin_unlock_irqrestore(&dim_lock, flags);
192 mbo->processed_length = 0;
193 mbo->status = MBO_E_INVAL;
198 list_move_tail(head->next, &hdm_ch->started_list);
199 spin_unlock_irqrestore(&dim_lock, flags);
205 * deliver_netinfo_thread - thread to deliver network status to mostcore
206 * @data: private data
208 * Wait for network status and deliver it to mostcore once it is received
210 static int deliver_netinfo_thread(void *data)
212 struct dim2_hdm *dev = data;
214 while (!kthread_should_stop()) {
215 wait_event_interruptible(dev->netinfo_waitq,
216 dev->deliver_netinfo ||
217 kthread_should_stop());
219 if (dev->deliver_netinfo) {
220 dev->deliver_netinfo--;
221 if (dev->on_netinfo) {
222 dev->on_netinfo(&dev->most_iface,
233 * retrieve_netinfo - retrieve network status from received buffer
237 * Parse the message in buffer and get node address, link state, MAC address.
238 * Wake up a thread to deliver this status to mostcore
240 static void retrieve_netinfo(struct dim2_hdm *dev, struct mbo *mbo)
242 u8 *data = mbo->virt_address;
244 pr_info("Node Address: 0x%03x\n", (u16)data[16] << 8 | data[17]);
245 dev->link_state = data[18];
246 pr_info("NIState: %d\n", dev->link_state);
247 memcpy(dev->mac_addrs, data + 19, 6);
248 dev->deliver_netinfo++;
249 wake_up_interruptible(&dev->netinfo_waitq);
253 * service_done_flag - handle completed buffers
255 * @ch_idx: channel index
257 * Return back the completed buffers to mostcore, using completion callback
259 static void service_done_flag(struct dim2_hdm *dev, int ch_idx)
261 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
262 struct dim_ch_state_t st;
263 struct list_head *head;
270 BUG_ON(!hdm_ch->is_initialized);
272 spin_lock_irqsave(&dim_lock, flags);
274 done_buffers = dim_get_channel_state(&hdm_ch->ch, &st)->done_buffers;
276 spin_unlock_irqrestore(&dim_lock, flags);
280 if (!dim_detach_buffers(&hdm_ch->ch, done_buffers)) {
281 spin_unlock_irqrestore(&dim_lock, flags);
284 spin_unlock_irqrestore(&dim_lock, flags);
286 head = &hdm_ch->started_list;
288 while (done_buffers) {
289 spin_lock_irqsave(&dim_lock, flags);
290 if (list_empty(head)) {
291 spin_unlock_irqrestore(&dim_lock, flags);
292 pr_crit("hard error: started_mbo list is empty whereas DIM2 has sent buffers\n");
296 mbo = list_first_entry(head, struct mbo, list);
297 list_del(head->next);
298 spin_unlock_irqrestore(&dim_lock, flags);
300 data = mbo->virt_address;
302 if (hdm_ch->data_type == MOST_CH_ASYNC &&
303 hdm_ch->direction == MOST_CH_RX &&
304 PACKET_IS_NET_INFO(data)) {
305 retrieve_netinfo(dev, mbo);
307 spin_lock_irqsave(&dim_lock, flags);
308 list_add_tail(&mbo->list, &hdm_ch->pending_list);
309 spin_unlock_irqrestore(&dim_lock, flags);
311 if (hdm_ch->data_type == MOST_CH_CONTROL ||
312 hdm_ch->data_type == MOST_CH_ASYNC) {
313 u32 const data_size =
314 (u32)data[0] * 256 + data[1] + 2;
316 mbo->processed_length =
317 min_t(u32, data_size,
320 mbo->processed_length = mbo->buffer_length;
322 mbo->status = MBO_SUCCESS;
330 static struct dim_channel **get_active_channels(struct dim2_hdm *dev,
331 struct dim_channel **buffer)
336 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
337 if (dev->hch[ch_idx].is_initialized)
338 buffer[idx++] = &dev->hch[ch_idx].ch;
340 buffer[idx++] = NULL;
345 static irqreturn_t dim2_mlb_isr(int irq, void *_dev)
347 struct dim2_hdm *dev = _dev;
350 spin_lock_irqsave(&dim_lock, flags);
351 dim_service_mlb_int_irq();
352 spin_unlock_irqrestore(&dim_lock, flags);
354 if (dev->atx_idx >= 0 && dev->hch[dev->atx_idx].is_initialized)
355 while (!try_start_dim_transfer(dev->hch + dev->atx_idx))
361 static irqreturn_t dim2_task_irq(int irq, void *_dev)
363 struct dim2_hdm *dev = _dev;
367 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
368 if (!dev->hch[ch_idx].is_initialized)
371 spin_lock_irqsave(&dim_lock, flags);
372 dim_service_channel(&dev->hch[ch_idx].ch);
373 spin_unlock_irqrestore(&dim_lock, flags);
375 service_done_flag(dev, ch_idx);
376 while (!try_start_dim_transfer(dev->hch + ch_idx))
384 * dim2_ahb_isr - interrupt service routine
386 * @_dev: private data
388 * Acknowledge the interrupt and service each initialized channel,
389 * if needed, in task context.
391 static irqreturn_t dim2_ahb_isr(int irq, void *_dev)
393 struct dim2_hdm *dev = _dev;
394 struct dim_channel *buffer[DMA_CHANNELS + 1];
397 spin_lock_irqsave(&dim_lock, flags);
398 dim_service_ahb_int_irq(get_active_channels(dev, buffer));
399 spin_unlock_irqrestore(&dim_lock, flags);
401 return IRQ_WAKE_THREAD;
405 * complete_all_mbos - complete MBO's in a list
408 * Delete all the entries in list and return back MBO's to mostcore using
409 * completion call back.
411 static void complete_all_mbos(struct list_head *head)
417 spin_lock_irqsave(&dim_lock, flags);
418 if (list_empty(head)) {
419 spin_unlock_irqrestore(&dim_lock, flags);
423 mbo = list_first_entry(head, struct mbo, list);
424 list_del(head->next);
425 spin_unlock_irqrestore(&dim_lock, flags);
427 mbo->processed_length = 0;
428 mbo->status = MBO_E_CLOSE;
434 * configure_channel - initialize a channel
435 * @most_iface: interface the channel belongs to
436 * @ch_idx: channel index to be configured
437 * @ccfg: structure that holds the configuration information
439 * Receives configuration information from mostcore and initialize
440 * the corresponding channel. Return 0 on success, negative on failure.
442 static int configure_channel(struct most_interface *most_iface, int ch_idx,
443 struct most_channel_config *ccfg)
445 struct dim2_hdm *dev = iface_to_hdm(most_iface);
446 bool const is_tx = ccfg->direction == MOST_CH_TX;
447 u16 const sub_size = ccfg->subbuffer_size;
448 u16 const buf_size = ccfg->buffer_size;
452 int const ch_addr = ch_idx * 2 + 2;
453 struct hdm_channel *const hdm_ch = dev->hch + ch_idx;
455 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
457 if (hdm_ch->is_initialized)
460 /* do not reset if the property was set by user, see poison_channel */
461 hdm_ch->reset_dbr_size = ccfg->dbr_size ? NULL : &ccfg->dbr_size;
463 /* zero value is default dbr_size, see dim2 hal */
464 hdm_ch->ch.dbr_size = ccfg->dbr_size;
466 switch (ccfg->data_type) {
467 case MOST_CH_CONTROL:
468 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
470 pr_err("%s: too small buffer size\n", hdm_ch->name);
473 ccfg->buffer_size = new_size;
474 if (new_size != buf_size)
475 pr_warn("%s: fixed buffer size (%d -> %d)\n",
476 hdm_ch->name, buf_size, new_size);
477 spin_lock_irqsave(&dim_lock, flags);
478 hal_ret = dim_init_control(&hdm_ch->ch, is_tx, ch_addr,
479 is_tx ? new_size * 2 : new_size);
482 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
484 pr_err("%s: too small buffer size\n", hdm_ch->name);
487 ccfg->buffer_size = new_size;
488 if (new_size != buf_size)
489 pr_warn("%s: fixed buffer size (%d -> %d)\n",
490 hdm_ch->name, buf_size, new_size);
491 spin_lock_irqsave(&dim_lock, flags);
492 hal_ret = dim_init_async(&hdm_ch->ch, is_tx, ch_addr,
493 is_tx ? new_size * 2 : new_size);
496 new_size = dim_norm_isoc_buffer_size(buf_size, sub_size);
498 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
502 ccfg->buffer_size = new_size;
503 if (new_size != buf_size)
504 pr_warn("%s: fixed buffer size (%d -> %d)\n",
505 hdm_ch->name, buf_size, new_size);
506 spin_lock_irqsave(&dim_lock, flags);
507 hal_ret = dim_init_isoc(&hdm_ch->ch, is_tx, ch_addr, sub_size);
510 new_size = dim_norm_sync_buffer_size(buf_size, sub_size);
512 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
516 ccfg->buffer_size = new_size;
517 if (new_size != buf_size)
518 pr_warn("%s: fixed buffer size (%d -> %d)\n",
519 hdm_ch->name, buf_size, new_size);
520 spin_lock_irqsave(&dim_lock, flags);
521 hal_ret = dim_init_sync(&hdm_ch->ch, is_tx, ch_addr, sub_size);
524 pr_err("%s: configure failed, bad channel type: %d\n",
525 hdm_ch->name, ccfg->data_type);
529 if (hal_ret != DIM_NO_ERROR) {
530 spin_unlock_irqrestore(&dim_lock, flags);
531 pr_err("%s: configure failed (%d), type: %d, is_tx: %d\n",
532 hdm_ch->name, hal_ret, ccfg->data_type, (int)is_tx);
536 hdm_ch->data_type = ccfg->data_type;
537 hdm_ch->direction = ccfg->direction;
538 hdm_ch->is_initialized = true;
540 if (hdm_ch->data_type == MOST_CH_ASYNC &&
541 hdm_ch->direction == MOST_CH_TX &&
543 dev->atx_idx = ch_idx;
545 spin_unlock_irqrestore(&dim_lock, flags);
546 ccfg->dbr_size = hdm_ch->ch.dbr_size;
552 * enqueue - enqueue a buffer for data transfer
553 * @most_iface: intended interface
554 * @ch_idx: ID of the channel the buffer is intended for
555 * @mbo: pointer to the buffer object
557 * Push the buffer into pending_list and try to transfer one buffer from
558 * pending_list. Return 0 on success, negative on failure.
560 static int enqueue(struct most_interface *most_iface, int ch_idx,
563 struct dim2_hdm *dev = iface_to_hdm(most_iface);
564 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
567 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
569 if (!hdm_ch->is_initialized)
572 if (mbo->bus_address == 0)
575 spin_lock_irqsave(&dim_lock, flags);
576 list_add_tail(&mbo->list, &hdm_ch->pending_list);
577 spin_unlock_irqrestore(&dim_lock, flags);
579 (void)try_start_dim_transfer(hdm_ch);
585 * request_netinfo - triggers retrieving of network info
586 * @most_iface: pointer to the interface
587 * @ch_idx: corresponding channel ID
588 * @on_netinfo: call-back used to deliver network status to mostcore
590 * Send a command to INIC which triggers retrieving of network info by means of
591 * "Message exchange over MDP/MEP". Return 0 on success, negative on failure.
593 static void request_netinfo(struct most_interface *most_iface, int ch_idx,
594 void (*on_netinfo)(struct most_interface *,
595 unsigned char, unsigned char *))
597 struct dim2_hdm *dev = iface_to_hdm(most_iface);
601 dev->on_netinfo = on_netinfo;
605 if (dev->atx_idx < 0) {
606 pr_err("Async Tx Not initialized\n");
610 mbo = most_get_mbo(&dev->most_iface, dev->atx_idx, NULL);
614 mbo->buffer_length = 5;
616 data = mbo->virt_address;
618 data[0] = 0x00; /* PML High byte */
619 data[1] = 0x03; /* PML Low byte */
620 data[2] = 0x02; /* PMHL */
621 data[3] = 0x08; /* FPH */
622 data[4] = 0x40; /* FMF (FIFO cmd msg - Triggers NAOverMDP) */
624 most_submit_mbo(mbo);
628 * poison_channel - poison buffers of a channel
629 * @most_iface: pointer to the interface the channel to be poisoned belongs to
630 * @ch_idx: corresponding channel ID
632 * Destroy a channel and complete all the buffers in both started_list &
633 * pending_list. Return 0 on success, negative on failure.
635 static int poison_channel(struct most_interface *most_iface, int ch_idx)
637 struct dim2_hdm *dev = iface_to_hdm(most_iface);
638 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
643 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
645 if (!hdm_ch->is_initialized)
648 spin_lock_irqsave(&dim_lock, flags);
649 hal_ret = dim_destroy_channel(&hdm_ch->ch);
650 hdm_ch->is_initialized = false;
651 if (ch_idx == dev->atx_idx)
653 spin_unlock_irqrestore(&dim_lock, flags);
654 if (hal_ret != DIM_NO_ERROR) {
655 pr_err("HAL Failed to close channel %s\n", hdm_ch->name);
659 complete_all_mbos(&hdm_ch->started_list);
660 complete_all_mbos(&hdm_ch->pending_list);
661 if (hdm_ch->reset_dbr_size)
662 *hdm_ch->reset_dbr_size = 0;
667 static void *dma_alloc(struct mbo *mbo, u32 size)
669 struct device *dev = mbo->ifp->driver_dev;
671 return dma_alloc_coherent(dev, size, &mbo->bus_address, GFP_KERNEL);
674 static void dma_free(struct mbo *mbo, u32 size)
676 struct device *dev = mbo->ifp->driver_dev;
678 dma_free_coherent(dev, size, mbo->virt_address, mbo->bus_address);
681 static const struct of_device_id dim2_of_match[];
684 const char *clock_speed;
687 { "256fs", CLK_256FS },
688 { "512fs", CLK_512FS },
689 { "1024fs", CLK_1024FS },
690 { "2048fs", CLK_2048FS },
691 { "3072fs", CLK_3072FS },
692 { "4096fs", CLK_4096FS },
693 { "6144fs", CLK_6144FS },
694 { "8192fs", CLK_8192FS },
698 * get_dim2_clk_speed - converts string to DIM2 clock speed value
700 * @clock_speed: string in the format "{NUMBER}fs"
701 * @val: pointer to get one of the CLK_{NUMBER}FS values
703 * By success stores one of the CLK_{NUMBER}FS in the *val and returns 0,
704 * otherwise returns -EINVAL.
706 static int get_dim2_clk_speed(const char *clock_speed, u8 *val)
710 for (i = 0; i < ARRAY_SIZE(clk_mt); i++) {
711 if (!strcmp(clock_speed, clk_mt[i].clock_speed)) {
712 *val = clk_mt[i].clk_speed;
719 static void dim2_release(struct device *d)
721 struct dim2_hdm *dev = container_of(d, struct dim2_hdm, dev);
724 kthread_stop(dev->netinfo_task);
726 spin_lock_irqsave(&dim_lock, flags);
728 spin_unlock_irqrestore(&dim_lock, flags);
730 if (dev->disable_platform)
731 dev->disable_platform(to_platform_device(d->parent));
737 * dim2_probe - dim2 probe handler
738 * @pdev: platform device structure
740 * Register the dim2 interface with mostcore and initialize it.
741 * Return 0 on success, negative on failure.
743 static int dim2_probe(struct platform_device *pdev)
745 const struct dim2_platform_data *pdata;
746 const struct of_device_id *of_id;
747 const char *clock_speed;
748 struct dim2_hdm *dev;
749 struct resource *res;
755 enum { MLB_INT_IDX, AHB0_INT_IDX };
757 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
763 platform_set_drvdata(pdev, dev);
765 ret = of_property_read_string(pdev->dev.of_node,
766 "microchip,clock-speed", &clock_speed);
768 dev_err(&pdev->dev, "missing dt property clock-speed\n");
772 ret = get_dim2_clk_speed(clock_speed, &dev->clk_speed);
774 dev_err(&pdev->dev, "bad dt property clock-speed\n");
778 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
779 dev->io_base = devm_ioremap_resource(&pdev->dev, res);
780 if (IS_ERR(dev->io_base)) {
781 ret = PTR_ERR(dev->io_base);
785 of_id = of_match_node(dim2_of_match, pdev->dev.of_node);
789 ret = pdata->enable(pdev);
793 dev->disable_platform = pdata->disable;
795 dev_fcnt = pdata->fcnt;
798 dev_info(&pdev->dev, "sync: num of frames per sub-buffer: %u\n",
800 hal_ret = dim_startup(dev->io_base, dev->clk_speed, dev_fcnt);
801 if (hal_ret != DIM_NO_ERROR) {
802 dev_err(&pdev->dev, "dim_startup failed: %d\n", hal_ret);
804 goto err_disable_platform;
807 irq = platform_get_irq(pdev, AHB0_INT_IDX);
810 goto err_shutdown_dim;
813 ret = devm_request_threaded_irq(&pdev->dev, irq, dim2_ahb_isr,
814 dim2_task_irq, 0, "dim2_ahb0_int", dev);
816 dev_err(&pdev->dev, "failed to request ahb0_int irq %d\n", irq);
817 goto err_shutdown_dim;
820 irq = platform_get_irq(pdev, MLB_INT_IDX);
823 goto err_shutdown_dim;
826 ret = devm_request_irq(&pdev->dev, irq, dim2_mlb_isr, 0,
827 "dim2_mlb_int", dev);
829 dev_err(&pdev->dev, "failed to request mlb_int irq %d\n", irq);
830 goto err_shutdown_dim;
833 init_waitqueue_head(&dev->netinfo_waitq);
834 dev->deliver_netinfo = 0;
835 dev->netinfo_task = kthread_run(&deliver_netinfo_thread, dev,
837 if (IS_ERR(dev->netinfo_task)) {
838 ret = PTR_ERR(dev->netinfo_task);
839 goto err_shutdown_dim;
842 for (i = 0; i < DMA_CHANNELS; i++) {
843 struct most_channel_capability *cap = dev->capabilities + i;
844 struct hdm_channel *hdm_ch = dev->hch + i;
846 INIT_LIST_HEAD(&hdm_ch->pending_list);
847 INIT_LIST_HEAD(&hdm_ch->started_list);
848 hdm_ch->is_initialized = false;
849 snprintf(hdm_ch->name, sizeof(hdm_ch->name), "ca%d", i * 2 + 2);
851 cap->name_suffix = hdm_ch->name;
852 cap->direction = MOST_CH_RX | MOST_CH_TX;
853 cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
854 MOST_CH_ISOC | MOST_CH_SYNC;
855 cap->num_buffers_packet = MAX_BUFFERS_PACKET;
856 cap->buffer_size_packet = MAX_BUF_SIZE_PACKET;
857 cap->num_buffers_streaming = MAX_BUFFERS_STREAMING;
858 cap->buffer_size_streaming = MAX_BUF_SIZE_STREAMING;
864 if (sizeof(res->start) == sizeof(long long))
865 fmt = "dim2-%016llx";
866 else if (sizeof(res->start) == sizeof(long))
871 snprintf(dev->name, sizeof(dev->name), fmt, res->start);
874 dev->most_iface.interface = ITYPE_MEDIALB_DIM2;
875 dev->most_iface.description = dev->name;
876 dev->most_iface.num_channels = DMA_CHANNELS;
877 dev->most_iface.channel_vector = dev->capabilities;
878 dev->most_iface.configure = configure_channel;
879 dev->most_iface.enqueue = enqueue;
880 dev->most_iface.dma_alloc = dma_alloc;
881 dev->most_iface.dma_free = dma_free;
882 dev->most_iface.poison_channel = poison_channel;
883 dev->most_iface.request_netinfo = request_netinfo;
884 dev->most_iface.driver_dev = &pdev->dev;
885 dev->most_iface.dev = &dev->dev;
886 dev->dev.init_name = dev->name;
887 dev->dev.parent = &pdev->dev;
888 dev->dev.release = dim2_release;
890 return most_register_interface(&dev->most_iface);
894 err_disable_platform:
895 if (dev->disable_platform)
896 dev->disable_platform(pdev);
904 * dim2_remove - dim2 remove handler
905 * @pdev: platform device structure
907 * Unregister the interface from mostcore
909 static int dim2_remove(struct platform_device *pdev)
911 struct dim2_hdm *dev = platform_get_drvdata(pdev);
913 most_deregister_interface(&dev->most_iface);
918 /* platform specific functions [[ */
920 static int fsl_mx6_enable(struct platform_device *pdev)
922 struct dim2_hdm *dev = platform_get_drvdata(pdev);
925 dev->clk = devm_clk_get(&pdev->dev, "mlb");
926 if (IS_ERR_OR_NULL(dev->clk)) {
927 dev_err(&pdev->dev, "unable to get mlb clock\n");
931 ret = clk_prepare_enable(dev->clk);
933 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
937 if (dev->clk_speed >= CLK_2048FS) {
939 dev->clk_pll = devm_clk_get(&pdev->dev, "pll8_mlb");
940 if (IS_ERR_OR_NULL(dev->clk_pll)) {
941 dev_err(&pdev->dev, "unable to get mlb pll clock\n");
942 clk_disable_unprepare(dev->clk);
946 writel(0x888, dev->io_base + 0x38);
947 clk_prepare_enable(dev->clk_pll);
953 static void fsl_mx6_disable(struct platform_device *pdev)
955 struct dim2_hdm *dev = platform_get_drvdata(pdev);
957 if (dev->clk_speed >= CLK_2048FS)
958 clk_disable_unprepare(dev->clk_pll);
960 clk_disable_unprepare(dev->clk);
963 static int rcar_gen2_enable(struct platform_device *pdev)
965 struct dim2_hdm *dev = platform_get_drvdata(pdev);
968 dev->clk = devm_clk_get(&pdev->dev, NULL);
969 if (IS_ERR(dev->clk)) {
970 dev_err(&pdev->dev, "cannot get clock\n");
971 return PTR_ERR(dev->clk);
974 ret = clk_prepare_enable(dev->clk);
976 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
980 if (dev->clk_speed >= CLK_2048FS) {
981 /* enable MLP pll and LVDS drivers */
982 writel(0x03, dev->io_base + 0x600);
984 writel(0x888, dev->io_base + 0x38);
987 writel(0x04, dev->io_base + 0x600);
992 writel(0x03, dev->io_base + 0x500);
993 writel(0x0002FF02, dev->io_base + 0x508);
998 static void rcar_gen2_disable(struct platform_device *pdev)
1000 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1002 clk_disable_unprepare(dev->clk);
1004 /* disable PLLs and LVDS drivers */
1005 writel(0x0, dev->io_base + 0x600);
1008 static int rcar_gen3_enable(struct platform_device *pdev)
1010 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1011 u32 enable_512fs = dev->clk_speed == CLK_512FS;
1014 dev->clk = devm_clk_get(&pdev->dev, NULL);
1015 if (IS_ERR(dev->clk)) {
1016 dev_err(&pdev->dev, "cannot get clock\n");
1017 return PTR_ERR(dev->clk);
1020 ret = clk_prepare_enable(dev->clk);
1022 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
1027 writel(0x04, dev->io_base + 0x600);
1029 writel(enable_512fs, dev->io_base + 0x604);
1032 writel(0x03, dev->io_base + 0x500);
1033 writel(0x0002FF02, dev->io_base + 0x508);
1038 static void rcar_gen3_disable(struct platform_device *pdev)
1040 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1042 clk_disable_unprepare(dev->clk);
1044 /* disable PLLs and LVDS drivers */
1045 writel(0x0, dev->io_base + 0x600);
1048 /* ]] platform specific functions */
1050 enum dim2_platforms { FSL_MX6, RCAR_GEN2, RCAR_GEN3 };
1052 static struct dim2_platform_data plat_data[] = {
1054 .enable = fsl_mx6_enable,
1055 .disable = fsl_mx6_disable,
1058 .enable = rcar_gen2_enable,
1059 .disable = rcar_gen2_disable,
1062 .enable = rcar_gen3_enable,
1063 .disable = rcar_gen3_disable,
1068 static const struct of_device_id dim2_of_match[] = {
1070 .compatible = "fsl,imx6q-mlb150",
1071 .data = plat_data + FSL_MX6
1074 .compatible = "renesas,mlp",
1075 .data = plat_data + RCAR_GEN2
1078 .compatible = "renesas,rcar-gen3-mlp",
1079 .data = plat_data + RCAR_GEN3
1082 .compatible = "xlnx,axi4-os62420_3pin-1.00.a",
1085 .compatible = "xlnx,axi4-os62420_6pin-1.00.a",
1090 MODULE_DEVICE_TABLE(of, dim2_of_match);
1092 static struct platform_driver dim2_driver = {
1093 .probe = dim2_probe,
1094 .remove = dim2_remove,
1097 .of_match_table = dim2_of_match,
1098 .dev_groups = dim2_groups,
1102 module_platform_driver(dim2_driver);
1104 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
1105 MODULE_DESCRIPTION("MediaLB DIM2 Hardware Dependent Module");
1106 MODULE_LICENSE("GPL");