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>
24 #include "most/core.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_io_read - callback from HAL to read an I/O register
133 * @ptr32: register address
135 u32 dimcb_io_read(u32 __iomem *ptr32)
141 * dimcb_io_write - callback from HAL to write value to an I/O register
142 * @ptr32: register address
143 * @value: value to write
145 void dimcb_io_write(u32 __iomem *ptr32, u32 value)
147 writel(value, ptr32);
151 * dimcb_on_error - callback from HAL to report miscommunication between
153 * @error_id: Error ID
154 * @error_message: Error message. Some text in a free format
156 void dimcb_on_error(u8 error_id, const char *error_message)
158 pr_err("%s: error_id - %d, error_message - %s\n", __func__, error_id,
163 * try_start_dim_transfer - try to transfer a buffer on a channel
164 * @hdm_ch: channel specific data
166 * Transfer a buffer from pending_list if the channel is ready
168 static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
171 struct list_head *head = &hdm_ch->pending_list;
174 struct dim_ch_state_t st;
177 BUG_ON(!hdm_ch->is_initialized);
179 spin_lock_irqsave(&dim_lock, flags);
180 if (list_empty(head)) {
181 spin_unlock_irqrestore(&dim_lock, flags);
185 if (!dim_get_channel_state(&hdm_ch->ch, &st)->ready) {
186 spin_unlock_irqrestore(&dim_lock, flags);
190 mbo = list_first_entry(head, struct mbo, list);
191 buf_size = mbo->buffer_length;
193 if (dim_dbr_space(&hdm_ch->ch) < buf_size) {
194 spin_unlock_irqrestore(&dim_lock, flags);
198 BUG_ON(mbo->bus_address == 0);
199 if (!dim_enqueue_buffer(&hdm_ch->ch, mbo->bus_address, buf_size)) {
200 list_del(head->next);
201 spin_unlock_irqrestore(&dim_lock, flags);
202 mbo->processed_length = 0;
203 mbo->status = MBO_E_INVAL;
208 list_move_tail(head->next, &hdm_ch->started_list);
209 spin_unlock_irqrestore(&dim_lock, flags);
215 * deliver_netinfo_thread - thread to deliver network status to mostcore
216 * @data: private data
218 * Wait for network status and deliver it to mostcore once it is received
220 static int deliver_netinfo_thread(void *data)
222 struct dim2_hdm *dev = data;
224 while (!kthread_should_stop()) {
225 wait_event_interruptible(dev->netinfo_waitq,
226 dev->deliver_netinfo ||
227 kthread_should_stop());
229 if (dev->deliver_netinfo) {
230 dev->deliver_netinfo--;
231 if (dev->on_netinfo) {
232 dev->on_netinfo(&dev->most_iface,
243 * retrieve_netinfo - retrieve network status from received buffer
247 * Parse the message in buffer and get node address, link state, MAC address.
248 * Wake up a thread to deliver this status to mostcore
250 static void retrieve_netinfo(struct dim2_hdm *dev, struct mbo *mbo)
252 u8 *data = mbo->virt_address;
254 pr_info("Node Address: 0x%03x\n", (u16)data[16] << 8 | data[17]);
255 dev->link_state = data[18];
256 pr_info("NIState: %d\n", dev->link_state);
257 memcpy(dev->mac_addrs, data + 19, 6);
258 dev->deliver_netinfo++;
259 wake_up_interruptible(&dev->netinfo_waitq);
263 * service_done_flag - handle completed buffers
265 * @ch_idx: channel index
267 * Return back the completed buffers to mostcore, using completion callback
269 static void service_done_flag(struct dim2_hdm *dev, int ch_idx)
271 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
272 struct dim_ch_state_t st;
273 struct list_head *head;
280 BUG_ON(!hdm_ch->is_initialized);
282 spin_lock_irqsave(&dim_lock, flags);
284 done_buffers = dim_get_channel_state(&hdm_ch->ch, &st)->done_buffers;
286 spin_unlock_irqrestore(&dim_lock, flags);
290 if (!dim_detach_buffers(&hdm_ch->ch, done_buffers)) {
291 spin_unlock_irqrestore(&dim_lock, flags);
294 spin_unlock_irqrestore(&dim_lock, flags);
296 head = &hdm_ch->started_list;
298 while (done_buffers) {
299 spin_lock_irqsave(&dim_lock, flags);
300 if (list_empty(head)) {
301 spin_unlock_irqrestore(&dim_lock, flags);
302 pr_crit("hard error: started_mbo list is empty whereas DIM2 has sent buffers\n");
306 mbo = list_first_entry(head, struct mbo, list);
307 list_del(head->next);
308 spin_unlock_irqrestore(&dim_lock, flags);
310 data = mbo->virt_address;
312 if (hdm_ch->data_type == MOST_CH_ASYNC &&
313 hdm_ch->direction == MOST_CH_RX &&
314 PACKET_IS_NET_INFO(data)) {
315 retrieve_netinfo(dev, mbo);
317 spin_lock_irqsave(&dim_lock, flags);
318 list_add_tail(&mbo->list, &hdm_ch->pending_list);
319 spin_unlock_irqrestore(&dim_lock, flags);
321 if (hdm_ch->data_type == MOST_CH_CONTROL ||
322 hdm_ch->data_type == MOST_CH_ASYNC) {
323 u32 const data_size =
324 (u32)data[0] * 256 + data[1] + 2;
326 mbo->processed_length =
327 min_t(u32, data_size,
330 mbo->processed_length = mbo->buffer_length;
332 mbo->status = MBO_SUCCESS;
340 static struct dim_channel **get_active_channels(struct dim2_hdm *dev,
341 struct dim_channel **buffer)
346 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
347 if (dev->hch[ch_idx].is_initialized)
348 buffer[idx++] = &dev->hch[ch_idx].ch;
350 buffer[idx++] = NULL;
355 static irqreturn_t dim2_mlb_isr(int irq, void *_dev)
357 struct dim2_hdm *dev = _dev;
360 spin_lock_irqsave(&dim_lock, flags);
361 dim_service_mlb_int_irq();
362 spin_unlock_irqrestore(&dim_lock, flags);
364 if (dev->atx_idx >= 0 && dev->hch[dev->atx_idx].is_initialized)
365 while (!try_start_dim_transfer(dev->hch + dev->atx_idx))
372 * dim2_tasklet_fn - tasklet function
373 * @data: private data
375 * Service each initialized channel, if needed
377 static void dim2_tasklet_fn(unsigned long data)
379 struct dim2_hdm *dev = (struct dim2_hdm *)data;
383 for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
384 if (!dev->hch[ch_idx].is_initialized)
387 spin_lock_irqsave(&dim_lock, flags);
388 dim_service_channel(&dev->hch[ch_idx].ch);
389 spin_unlock_irqrestore(&dim_lock, flags);
391 service_done_flag(dev, ch_idx);
392 while (!try_start_dim_transfer(dev->hch + ch_idx))
398 * dim2_ahb_isr - interrupt service routine
400 * @_dev: private data
402 * Acknowledge the interrupt and schedule a tasklet to service channels.
403 * Return IRQ_HANDLED.
405 static irqreturn_t dim2_ahb_isr(int irq, void *_dev)
407 struct dim2_hdm *dev = _dev;
408 struct dim_channel *buffer[DMA_CHANNELS + 1];
411 spin_lock_irqsave(&dim_lock, flags);
412 dim_service_ahb_int_irq(get_active_channels(dev, buffer));
413 spin_unlock_irqrestore(&dim_lock, flags);
415 dim2_tasklet.data = (unsigned long)dev;
416 tasklet_schedule(&dim2_tasklet);
421 * complete_all_mbos - complete MBO's in a list
424 * Delete all the entries in list and return back MBO's to mostcore using
425 * completion call back.
427 static void complete_all_mbos(struct list_head *head)
433 spin_lock_irqsave(&dim_lock, flags);
434 if (list_empty(head)) {
435 spin_unlock_irqrestore(&dim_lock, flags);
439 mbo = list_first_entry(head, struct mbo, list);
440 list_del(head->next);
441 spin_unlock_irqrestore(&dim_lock, flags);
443 mbo->processed_length = 0;
444 mbo->status = MBO_E_CLOSE;
450 * configure_channel - initialize a channel
451 * @iface: interface the channel belongs to
452 * @channel: channel to be configured
453 * @channel_config: structure that holds the configuration information
455 * Receives configuration information from mostcore and initialize
456 * the corresponding channel. Return 0 on success, negative on failure.
458 static int configure_channel(struct most_interface *most_iface, int ch_idx,
459 struct most_channel_config *ccfg)
461 struct dim2_hdm *dev = iface_to_hdm(most_iface);
462 bool const is_tx = ccfg->direction == MOST_CH_TX;
463 u16 const sub_size = ccfg->subbuffer_size;
464 u16 const buf_size = ccfg->buffer_size;
468 int const ch_addr = ch_idx * 2 + 2;
469 struct hdm_channel *const hdm_ch = dev->hch + ch_idx;
471 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
473 if (hdm_ch->is_initialized)
476 /* do not reset if the property was set by user, see poison_channel */
477 hdm_ch->reset_dbr_size = ccfg->dbr_size ? NULL : &ccfg->dbr_size;
479 /* zero value is default dbr_size, see dim2 hal */
480 hdm_ch->ch.dbr_size = ccfg->dbr_size;
482 switch (ccfg->data_type) {
483 case MOST_CH_CONTROL:
484 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
486 pr_err("%s: too small buffer size\n", hdm_ch->name);
489 ccfg->buffer_size = new_size;
490 if (new_size != buf_size)
491 pr_warn("%s: fixed buffer size (%d -> %d)\n",
492 hdm_ch->name, buf_size, new_size);
493 spin_lock_irqsave(&dim_lock, flags);
494 hal_ret = dim_init_control(&hdm_ch->ch, is_tx, ch_addr,
495 is_tx ? new_size * 2 : new_size);
498 new_size = dim_norm_ctrl_async_buffer_size(buf_size);
500 pr_err("%s: too small buffer size\n", hdm_ch->name);
503 ccfg->buffer_size = new_size;
504 if (new_size != buf_size)
505 pr_warn("%s: fixed buffer size (%d -> %d)\n",
506 hdm_ch->name, buf_size, new_size);
507 spin_lock_irqsave(&dim_lock, flags);
508 hal_ret = dim_init_async(&hdm_ch->ch, is_tx, ch_addr,
509 is_tx ? new_size * 2 : new_size);
512 new_size = dim_norm_isoc_buffer_size(buf_size, sub_size);
514 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
518 ccfg->buffer_size = new_size;
519 if (new_size != buf_size)
520 pr_warn("%s: fixed buffer size (%d -> %d)\n",
521 hdm_ch->name, buf_size, new_size);
522 spin_lock_irqsave(&dim_lock, flags);
523 hal_ret = dim_init_isoc(&hdm_ch->ch, is_tx, ch_addr, sub_size);
526 new_size = dim_norm_sync_buffer_size(buf_size, sub_size);
528 pr_err("%s: invalid sub-buffer size or too small buffer size\n",
532 ccfg->buffer_size = new_size;
533 if (new_size != buf_size)
534 pr_warn("%s: fixed buffer size (%d -> %d)\n",
535 hdm_ch->name, buf_size, new_size);
536 spin_lock_irqsave(&dim_lock, flags);
537 hal_ret = dim_init_sync(&hdm_ch->ch, is_tx, ch_addr, sub_size);
540 pr_err("%s: configure failed, bad channel type: %d\n",
541 hdm_ch->name, ccfg->data_type);
545 if (hal_ret != DIM_NO_ERROR) {
546 spin_unlock_irqrestore(&dim_lock, flags);
547 pr_err("%s: configure failed (%d), type: %d, is_tx: %d\n",
548 hdm_ch->name, hal_ret, ccfg->data_type, (int)is_tx);
552 hdm_ch->data_type = ccfg->data_type;
553 hdm_ch->direction = ccfg->direction;
554 hdm_ch->is_initialized = true;
556 if (hdm_ch->data_type == MOST_CH_ASYNC &&
557 hdm_ch->direction == MOST_CH_TX &&
559 dev->atx_idx = ch_idx;
561 spin_unlock_irqrestore(&dim_lock, flags);
562 ccfg->dbr_size = hdm_ch->ch.dbr_size;
568 * enqueue - enqueue a buffer for data transfer
569 * @iface: intended interface
570 * @channel: ID of the channel the buffer is intended for
571 * @mbo: pointer to the buffer object
573 * Push the buffer into pending_list and try to transfer one buffer from
574 * pending_list. Return 0 on success, negative on failure.
576 static int enqueue(struct most_interface *most_iface, int ch_idx,
579 struct dim2_hdm *dev = iface_to_hdm(most_iface);
580 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
583 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
585 if (!hdm_ch->is_initialized)
588 if (mbo->bus_address == 0)
591 spin_lock_irqsave(&dim_lock, flags);
592 list_add_tail(&mbo->list, &hdm_ch->pending_list);
593 spin_unlock_irqrestore(&dim_lock, flags);
595 (void)try_start_dim_transfer(hdm_ch);
601 * request_netinfo - triggers retrieving of network info
602 * @iface: pointer to the interface
603 * @channel_id: corresponding channel ID
605 * Send a command to INIC which triggers retrieving of network info by means of
606 * "Message exchange over MDP/MEP". Return 0 on success, negative on failure.
608 static void request_netinfo(struct most_interface *most_iface, int ch_idx,
609 void (*on_netinfo)(struct most_interface *,
610 unsigned char, unsigned char *))
612 struct dim2_hdm *dev = iface_to_hdm(most_iface);
616 dev->on_netinfo = on_netinfo;
620 if (dev->atx_idx < 0) {
621 pr_err("Async Tx Not initialized\n");
625 mbo = most_get_mbo(&dev->most_iface, dev->atx_idx, NULL);
629 mbo->buffer_length = 5;
631 data = mbo->virt_address;
633 data[0] = 0x00; /* PML High byte */
634 data[1] = 0x03; /* PML Low byte */
635 data[2] = 0x02; /* PMHL */
636 data[3] = 0x08; /* FPH */
637 data[4] = 0x40; /* FMF (FIFO cmd msg - Triggers NAOverMDP) */
639 most_submit_mbo(mbo);
643 * poison_channel - poison buffers of a channel
644 * @iface: pointer to the interface the channel to be poisoned belongs to
645 * @channel_id: corresponding channel ID
647 * Destroy a channel and complete all the buffers in both started_list &
648 * pending_list. Return 0 on success, negative on failure.
650 static int poison_channel(struct most_interface *most_iface, int ch_idx)
652 struct dim2_hdm *dev = iface_to_hdm(most_iface);
653 struct hdm_channel *hdm_ch = dev->hch + ch_idx;
658 BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
660 if (!hdm_ch->is_initialized)
663 tasklet_disable(&dim2_tasklet);
664 spin_lock_irqsave(&dim_lock, flags);
665 hal_ret = dim_destroy_channel(&hdm_ch->ch);
666 hdm_ch->is_initialized = false;
667 if (ch_idx == dev->atx_idx)
669 spin_unlock_irqrestore(&dim_lock, flags);
670 tasklet_enable(&dim2_tasklet);
671 if (hal_ret != DIM_NO_ERROR) {
672 pr_err("HAL Failed to close channel %s\n", hdm_ch->name);
676 complete_all_mbos(&hdm_ch->started_list);
677 complete_all_mbos(&hdm_ch->pending_list);
678 if (hdm_ch->reset_dbr_size)
679 *hdm_ch->reset_dbr_size = 0;
684 static void *dma_alloc(struct mbo *mbo, u32 size)
686 struct device *dev = mbo->ifp->driver_dev;
688 return dma_alloc_coherent(dev, size, &mbo->bus_address, GFP_KERNEL);
691 static void dma_free(struct mbo *mbo, u32 size)
693 struct device *dev = mbo->ifp->driver_dev;
695 dma_free_coherent(dev, size, mbo->virt_address, mbo->bus_address);
698 static const struct of_device_id dim2_of_match[];
701 const char *clock_speed;
704 { "256fs", CLK_256FS },
705 { "512fs", CLK_512FS },
706 { "1024fs", CLK_1024FS },
707 { "2048fs", CLK_2048FS },
708 { "3072fs", CLK_3072FS },
709 { "4096fs", CLK_4096FS },
710 { "6144fs", CLK_6144FS },
711 { "8192fs", CLK_8192FS },
715 * get_dim2_clk_speed - converts string to DIM2 clock speed value
717 * @clock_speed: string in the format "{NUMBER}fs"
718 * @val: pointer to get one of the CLK_{NUMBER}FS values
720 * By success stores one of the CLK_{NUMBER}FS in the *val and returns 0,
721 * otherwise returns -EINVAL.
723 static int get_dim2_clk_speed(const char *clock_speed, u8 *val)
727 for (i = 0; i < ARRAY_SIZE(clk_mt); i++) {
728 if (!strcmp(clock_speed, clk_mt[i].clock_speed)) {
729 *val = clk_mt[i].clk_speed;
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;
754 enum { MLB_INT_IDX, AHB0_INT_IDX };
756 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
762 platform_set_drvdata(pdev, dev);
764 ret = of_property_read_string(pdev->dev.of_node,
765 "microchip,clock-speed", &clock_speed);
767 dev_err(&pdev->dev, "missing dt property clock-speed\n");
771 ret = get_dim2_clk_speed(clock_speed, &dev->clk_speed);
773 dev_err(&pdev->dev, "bad dt property clock-speed\n");
777 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
778 dev->io_base = devm_ioremap_resource(&pdev->dev, res);
779 if (IS_ERR(dev->io_base))
780 return PTR_ERR(dev->io_base);
782 of_id = of_match_node(dim2_of_match, pdev->dev.of_node);
784 ret = pdata && pdata->enable ? pdata->enable(pdev) : 0;
788 dev->disable_platform = pdata ? pdata->disable : NULL;
790 dev_info(&pdev->dev, "sync: num of frames per sub-buffer: %u\n", fcnt);
791 hal_ret = dim_startup(dev->io_base, dev->clk_speed, fcnt);
792 if (hal_ret != DIM_NO_ERROR) {
793 dev_err(&pdev->dev, "dim_startup failed: %d\n", hal_ret);
795 goto err_disable_platform;
798 irq = platform_get_irq(pdev, AHB0_INT_IDX);
800 dev_err(&pdev->dev, "failed to get ahb0_int irq: %d\n", irq);
802 goto err_shutdown_dim;
805 ret = devm_request_irq(&pdev->dev, irq, dim2_ahb_isr, 0,
806 "dim2_ahb0_int", dev);
808 dev_err(&pdev->dev, "failed to request ahb0_int irq %d\n", irq);
809 goto err_shutdown_dim;
812 irq = platform_get_irq(pdev, MLB_INT_IDX);
814 dev_err(&pdev->dev, "failed to get mlb_int irq: %d\n", irq);
816 goto err_shutdown_dim;
819 ret = devm_request_irq(&pdev->dev, irq, dim2_mlb_isr, 0,
820 "dim2_mlb_int", dev);
822 dev_err(&pdev->dev, "failed to request mlb_int irq %d\n", irq);
823 goto err_shutdown_dim;
826 init_waitqueue_head(&dev->netinfo_waitq);
827 dev->deliver_netinfo = 0;
828 dev->netinfo_task = kthread_run(&deliver_netinfo_thread, dev,
830 if (IS_ERR(dev->netinfo_task)) {
831 ret = PTR_ERR(dev->netinfo_task);
832 goto err_shutdown_dim;
835 for (i = 0; i < DMA_CHANNELS; i++) {
836 struct most_channel_capability *cap = dev->capabilities + i;
837 struct hdm_channel *hdm_ch = dev->hch + i;
839 INIT_LIST_HEAD(&hdm_ch->pending_list);
840 INIT_LIST_HEAD(&hdm_ch->started_list);
841 hdm_ch->is_initialized = false;
842 snprintf(hdm_ch->name, sizeof(hdm_ch->name), "ca%d", i * 2 + 2);
844 cap->name_suffix = hdm_ch->name;
845 cap->direction = MOST_CH_RX | MOST_CH_TX;
846 cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
847 MOST_CH_ISOC | MOST_CH_SYNC;
848 cap->num_buffers_packet = MAX_BUFFERS_PACKET;
849 cap->buffer_size_packet = MAX_BUF_SIZE_PACKET;
850 cap->num_buffers_streaming = MAX_BUFFERS_STREAMING;
851 cap->buffer_size_streaming = MAX_BUF_SIZE_STREAMING;
857 if (sizeof(res->start) == sizeof(long long))
858 fmt = "dim2-%016llx";
859 else if (sizeof(res->start) == sizeof(long))
864 snprintf(dev->name, sizeof(dev->name), fmt, res->start);
867 dev->most_iface.interface = ITYPE_MEDIALB_DIM2;
868 dev->most_iface.description = dev->name;
869 dev->most_iface.num_channels = DMA_CHANNELS;
870 dev->most_iface.channel_vector = dev->capabilities;
871 dev->most_iface.configure = configure_channel;
872 dev->most_iface.enqueue = enqueue;
873 dev->most_iface.dma_alloc = dma_alloc;
874 dev->most_iface.dma_free = dma_free;
875 dev->most_iface.poison_channel = poison_channel;
876 dev->most_iface.request_netinfo = request_netinfo;
877 dev->most_iface.driver_dev = &pdev->dev;
878 dev->dev.init_name = "dim2_state";
879 dev->dev.parent = &dev->most_iface.dev;
881 ret = most_register_interface(&dev->most_iface);
883 dev_err(&pdev->dev, "failed to register MOST interface\n");
884 goto err_stop_thread;
887 ret = dim2_sysfs_probe(&dev->dev);
889 dev_err(&pdev->dev, "failed to create sysfs attribute\n");
890 goto err_unreg_iface;
896 most_deregister_interface(&dev->most_iface);
898 kthread_stop(dev->netinfo_task);
901 err_disable_platform:
902 if (dev->disable_platform)
903 dev->disable_platform(pdev);
909 * dim2_remove - dim2 remove handler
910 * @pdev: platform device structure
912 * Unregister the interface from mostcore
914 static int dim2_remove(struct platform_device *pdev)
916 struct dim2_hdm *dev = platform_get_drvdata(pdev);
919 dim2_sysfs_destroy(&dev->dev);
920 most_deregister_interface(&dev->most_iface);
921 kthread_stop(dev->netinfo_task);
923 spin_lock_irqsave(&dim_lock, flags);
925 spin_unlock_irqrestore(&dim_lock, flags);
927 if (dev->disable_platform)
928 dev->disable_platform(pdev);
933 /* platform specific functions [[ */
935 static int fsl_mx6_enable(struct platform_device *pdev)
937 struct dim2_hdm *dev = platform_get_drvdata(pdev);
940 dev->clk = devm_clk_get(&pdev->dev, "mlb");
941 if (IS_ERR_OR_NULL(dev->clk)) {
942 dev_err(&pdev->dev, "unable to get mlb clock\n");
946 ret = clk_prepare_enable(dev->clk);
948 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
952 if (dev->clk_speed >= CLK_2048FS) {
954 dev->clk_pll = devm_clk_get(&pdev->dev, "pll8_mlb");
955 if (IS_ERR_OR_NULL(dev->clk_pll)) {
956 dev_err(&pdev->dev, "unable to get mlb pll clock\n");
957 clk_disable_unprepare(dev->clk);
961 writel(0x888, dev->io_base + 0x38);
962 clk_prepare_enable(dev->clk_pll);
968 static void fsl_mx6_disable(struct platform_device *pdev)
970 struct dim2_hdm *dev = platform_get_drvdata(pdev);
972 if (dev->clk_speed >= CLK_2048FS)
973 clk_disable_unprepare(dev->clk_pll);
975 clk_disable_unprepare(dev->clk);
978 static int rcar_h2_enable(struct platform_device *pdev)
980 struct dim2_hdm *dev = platform_get_drvdata(pdev);
983 dev->clk = devm_clk_get(&pdev->dev, NULL);
984 if (IS_ERR(dev->clk)) {
985 dev_err(&pdev->dev, "cannot get clock\n");
986 return PTR_ERR(dev->clk);
989 ret = clk_prepare_enable(dev->clk);
991 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
995 if (dev->clk_speed >= CLK_2048FS) {
996 /* enable MLP pll and LVDS drivers */
997 writel(0x03, dev->io_base + 0x600);
999 writel(0x888, dev->io_base + 0x38);
1002 writel(0x04, dev->io_base + 0x600);
1007 writel(0x03, dev->io_base + 0x500);
1008 writel(0x0002FF02, dev->io_base + 0x508);
1013 static void rcar_h2_disable(struct platform_device *pdev)
1015 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1017 clk_disable_unprepare(dev->clk);
1019 /* disable PLLs and LVDS drivers */
1020 writel(0x0, dev->io_base + 0x600);
1023 static int rcar_m3_enable(struct platform_device *pdev)
1025 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1026 u32 enable_512fs = dev->clk_speed == CLK_512FS;
1029 dev->clk = devm_clk_get(&pdev->dev, NULL);
1030 if (IS_ERR(dev->clk)) {
1031 dev_err(&pdev->dev, "cannot get clock\n");
1032 return PTR_ERR(dev->clk);
1035 ret = clk_prepare_enable(dev->clk);
1037 dev_err(&pdev->dev, "%s\n", "clk_prepare_enable failed");
1042 writel(0x04, dev->io_base + 0x600);
1044 writel(enable_512fs, dev->io_base + 0x604);
1047 writel(0x03, dev->io_base + 0x500);
1048 writel(0x0002FF02, dev->io_base + 0x508);
1053 static void rcar_m3_disable(struct platform_device *pdev)
1055 struct dim2_hdm *dev = platform_get_drvdata(pdev);
1057 clk_disable_unprepare(dev->clk);
1059 /* disable PLLs and LVDS drivers */
1060 writel(0x0, dev->io_base + 0x600);
1063 /* ]] platform specific functions */
1065 enum dim2_platforms { FSL_MX6, RCAR_H2, RCAR_M3 };
1067 static struct dim2_platform_data plat_data[] = {
1068 [FSL_MX6] = { .enable = fsl_mx6_enable, .disable = fsl_mx6_disable },
1069 [RCAR_H2] = { .enable = rcar_h2_enable, .disable = rcar_h2_disable },
1070 [RCAR_M3] = { .enable = rcar_m3_enable, .disable = rcar_m3_disable },
1073 static const struct of_device_id dim2_of_match[] = {
1075 .compatible = "fsl,imx6q-mlb150",
1076 .data = plat_data + FSL_MX6
1079 .compatible = "renesas,mlp",
1080 .data = plat_data + RCAR_H2
1083 .compatible = "rcar,medialb-dim2",
1084 .data = plat_data + RCAR_M3
1087 .compatible = "xlnx,axi4-os62420_3pin-1.00.a",
1090 .compatible = "xlnx,axi4-os62420_6pin-1.00.a",
1095 MODULE_DEVICE_TABLE(of, dim2_of_match);
1097 static struct platform_driver dim2_driver = {
1098 .probe = dim2_probe,
1099 .remove = dim2_remove,
1102 .of_match_table = dim2_of_match,
1106 module_platform_driver(dim2_driver);
1108 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
1109 MODULE_DESCRIPTION("MediaLB DIM2 Hardware Dependent Module");
1110 MODULE_LICENSE("GPL");