]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/most/hdm-dim2/dim2_hdm.c
Merge tag 'socfpga_for_v4.4_cleanup' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / drivers / staging / most / hdm-dim2 / dim2_hdm.c
1 /*
2  * dim2_hdm.c - MediaLB DIM2 Hardware Dependent Module
3  *
4  * Copyright (C) 2015, Microchip Technology Germany II GmbH & Co. KG
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  * GNU General Public License for more details.
10  *
11  * This file is licensed under GPLv2.
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/module.h>
17 #include <linux/printk.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/interrupt.h>
22 #include <linux/slab.h>
23 #include <linux/io.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/sched.h>
26 #include <linux/kthread.h>
27
28 #include <mostcore.h>
29 #include <networking.h>
30 #include "dim2_hal.h"
31 #include "dim2_hdm.h"
32 #include "dim2_errors.h"
33 #include "dim2_sysfs.h"
34
35 #define DMA_CHANNELS (32 - 1)  /* channel 0 is a system channel */
36
37 #define MAX_BUFFERS_PACKET      32
38 #define MAX_BUFFERS_STREAMING   32
39 #define MAX_BUF_SIZE_PACKET     2048
40 #define MAX_BUF_SIZE_STREAMING  (8*1024)
41
42 /* command line parameter to select clock speed */
43 static char *clock_speed;
44 module_param(clock_speed, charp, 0);
45 MODULE_PARM_DESC(clock_speed, "MediaLB Clock Speed");
46
47 /*
48  * #############################################################################
49  *
50  * The define below activates an utility function used by HAL-simu
51  * for calling DIM interrupt handler.
52  * It is used only for TEST PURPOSE and shall be commented before release.
53  *
54  * #############################################################################
55  */
56 /* #define ENABLE_HDM_TEST */
57
58 static DEFINE_SPINLOCK(dim_lock);
59
60 static void dim2_tasklet_fn(unsigned long data);
61 static DECLARE_TASKLET(dim2_tasklet, dim2_tasklet_fn, 0);
62
63 /**
64  * struct hdm_channel - private structure to keep channel specific data
65  * @is_initialized: identifier to know whether the channel is initialized
66  * @ch: HAL specific channel data
67  * @pending_list: list to keep MBO's before starting transfer
68  * @started_list: list to keep MBO's after starting transfer
69  * @direction: channel direction (TX or RX)
70  * @data_type: channel data type
71  */
72 struct hdm_channel {
73         char name[sizeof "caNNN"];
74         bool is_initialized;
75         struct dim_channel ch;
76         struct list_head pending_list;  /* before DIM_EnqueueBuffer() */
77         struct list_head started_list;  /* after DIM_EnqueueBuffer() */
78         enum most_channel_direction direction;
79         enum most_channel_data_type data_type;
80 };
81
82 /**
83  * struct dim2_hdm - private structure to keep interface specific data
84  * @hch: an array of channel specific data
85  * @most_iface: most interface structure
86  * @capabilities: an array of channel capability data
87  * @io_base: I/O register base address
88  * @irq_ahb0: dim2 AHB0 irq number
89  * @clk_speed: user selectable (through command line parameter) clock speed
90  * @netinfo_task: thread to deliver network status
91  * @netinfo_waitq: waitq for the thread to sleep
92  * @deliver_netinfo: to identify whether network status received
93  * @mac_addrs: INIC mac address
94  * @link_state: network link state
95  * @atx_idx: index of async tx channel
96  */
97 struct dim2_hdm {
98         struct hdm_channel hch[DMA_CHANNELS];
99         struct most_channel_capability capabilities[DMA_CHANNELS];
100         struct most_interface most_iface;
101         char name[16 + sizeof "dim2-"];
102         void *io_base;
103         unsigned int irq_ahb0;
104         int clk_speed;
105         struct task_struct *netinfo_task;
106         wait_queue_head_t netinfo_waitq;
107         int deliver_netinfo;
108         unsigned char mac_addrs[6];
109         unsigned char link_state;
110         int atx_idx;
111         struct medialb_bus bus;
112 };
113
114 #define iface_to_hdm(iface) container_of(iface, struct dim2_hdm, most_iface)
115
116 /* Macro to identify a network status message */
117 #define PACKET_IS_NET_INFO(p)  \
118         (((p)[1] == 0x18) && ((p)[2] == 0x05) && ((p)[3] == 0x0C) && \
119          ((p)[13] == 0x3C) && ((p)[14] == 0x00) && ((p)[15] == 0x0A))
120
121 #if defined(ENABLE_HDM_TEST)
122 static struct dim2_hdm *test_dev;
123 #endif
124
125 bool dim2_sysfs_get_state_cb(void)
126 {
127         bool state;
128         unsigned long flags;
129
130         spin_lock_irqsave(&dim_lock, flags);
131         state = DIM_GetLockState();
132         spin_unlock_irqrestore(&dim_lock, flags);
133
134         return state;
135 }
136
137 /**
138  * DIMCB_IoRead - callback from HAL to read an I/O register
139  * @ptr32: register address
140  */
141 u32 DIMCB_IoRead(u32 *ptr32)
142 {
143         return __raw_readl(ptr32);
144 }
145
146 /**
147  * DIMCB_IoWrite - callback from HAL to write value to an I/O register
148  * @ptr32: register address
149  * @value: value to write
150  */
151 void DIMCB_IoWrite(u32 *ptr32, u32 value)
152 {
153         __raw_writel(value, ptr32);
154 }
155
156 /**
157  * DIMCB_OnError - callback from HAL to report miscommunication between
158  * HDM and HAL
159  * @error_id: Error ID
160  * @error_message: Error message. Some text in a free format
161  */
162 void DIMCB_OnError(u8 error_id, const char *error_message)
163 {
164         pr_err("DIMCB_OnError: error_id - %d, error_message - %s\n", error_id,
165                error_message);
166 }
167
168 /**
169  * DIMCB_OnFail - callback from HAL to report unrecoverable errors
170  * @filename: Source file where the error happened
171  * @linenum: Line number of the file where the error happened
172  */
173 void DIMCB_OnFail(const char *filename, int linenum)
174 {
175         pr_err("DIMCB_OnFail: file - %s, line no. - %d\n", filename, linenum);
176 }
177
178 /**
179  * startup_dim - initialize the dim2 interface
180  * @pdev: platform device
181  *
182  * Get the value of command line parameter "clock_speed" if given or use the
183  * default value, enable the clock and PLL, and initialize the dim2 interface.
184  */
185 static int startup_dim(struct platform_device *pdev)
186 {
187         struct dim2_hdm *dev = platform_get_drvdata(pdev);
188         struct dim2_platform_data *pdata = pdev->dev.platform_data;
189         u8 hal_ret;
190
191         dev->clk_speed = -1;
192
193         if (clock_speed) {
194                 if (!strcmp(clock_speed, "256fs"))
195                         dev->clk_speed = CLK_256FS;
196                 else if (!strcmp(clock_speed, "512fs"))
197                         dev->clk_speed = CLK_512FS;
198                 else if (!strcmp(clock_speed, "1024fs"))
199                         dev->clk_speed = CLK_1024FS;
200                 else if (!strcmp(clock_speed, "2048fs"))
201                         dev->clk_speed = CLK_2048FS;
202                 else if (!strcmp(clock_speed, "3072fs"))
203                         dev->clk_speed = CLK_3072FS;
204                 else if (!strcmp(clock_speed, "4096fs"))
205                         dev->clk_speed = CLK_4096FS;
206                 else if (!strcmp(clock_speed, "6144fs"))
207                         dev->clk_speed = CLK_6144FS;
208                 else if (!strcmp(clock_speed, "8192fs"))
209                         dev->clk_speed = CLK_8192FS;
210         }
211
212         if (dev->clk_speed == -1) {
213                 pr_info("Bad or missing clock speed parameter,"
214                         " using default value: 3072fs\n");
215                 dev->clk_speed = CLK_3072FS;
216         } else
217                 pr_info("Selected clock speed: %s\n", clock_speed);
218
219         if (pdata && pdata->init) {
220                 int ret = pdata->init(pdata, dev->io_base, dev->clk_speed);
221
222                 if (ret)
223                         return ret;
224         }
225
226         hal_ret = DIM_Startup(dev->io_base, dev->clk_speed);
227         if (hal_ret != DIM_NO_ERROR) {
228                 pr_err("DIM_Startup failed: %d\n", hal_ret);
229                 if (pdata && pdata->destroy)
230                         pdata->destroy(pdata);
231                 return -ENODEV;
232         }
233
234         return 0;
235 }
236
237 /**
238  * try_start_dim_transfer - try to transfer a buffer on a channel
239  * @hdm_ch: channel specific data
240  *
241  * Transfer a buffer from pending_list if the channel is ready
242  */
243 static int try_start_dim_transfer(struct hdm_channel *hdm_ch)
244 {
245         u16 buf_size;
246         struct list_head *head = &hdm_ch->pending_list;
247         struct mbo *mbo;
248         unsigned long flags;
249         struct dim_ch_state_t st;
250
251         BUG_ON(hdm_ch == 0);
252         BUG_ON(!hdm_ch->is_initialized);
253
254         spin_lock_irqsave(&dim_lock, flags);
255         if (list_empty(head)) {
256                 spin_unlock_irqrestore(&dim_lock, flags);
257                 return -EAGAIN;
258         }
259
260         if (!DIM_GetChannelState(&hdm_ch->ch, &st)->ready) {
261                 spin_unlock_irqrestore(&dim_lock, flags);
262                 return -EAGAIN;
263         }
264
265         mbo = list_entry(head->next, struct mbo, list);
266         buf_size = mbo->buffer_length;
267
268         BUG_ON(mbo->bus_address == 0);
269         if (!DIM_EnqueueBuffer(&hdm_ch->ch, mbo->bus_address, buf_size)) {
270                 list_del(head->next);
271                 spin_unlock_irqrestore(&dim_lock, flags);
272                 mbo->processed_length = 0;
273                 mbo->status = MBO_E_INVAL;
274                 mbo->complete(mbo);
275                 return -EFAULT;
276         }
277
278         list_move_tail(head->next, &hdm_ch->started_list);
279         spin_unlock_irqrestore(&dim_lock, flags);
280
281         return 0;
282 }
283
284 /**
285  * deliver_netinfo_thread - thread to deliver network status to mostcore
286  * @data: private data
287  *
288  * Wait for network status and deliver it to mostcore once it is received
289  */
290 static int deliver_netinfo_thread(void *data)
291 {
292         struct dim2_hdm *dev = (struct dim2_hdm *)data;
293
294         while (!kthread_should_stop()) {
295                 wait_event_interruptible(dev->netinfo_waitq,
296                                          dev->deliver_netinfo ||
297                                          kthread_should_stop());
298
299                 if (dev->deliver_netinfo) {
300                         dev->deliver_netinfo--;
301                         most_deliver_netinfo(&dev->most_iface, dev->link_state,
302                                              dev->mac_addrs);
303                 }
304         }
305
306         return 0;
307 }
308
309 /**
310  * retrieve_netinfo - retrieve network status from received buffer
311  * @dev: private data
312  * @mbo: received MBO
313  *
314  * Parse the message in buffer and get node address, link state, MAC address.
315  * Wake up a thread to deliver this status to mostcore
316  */
317 static void retrieve_netinfo(struct dim2_hdm *dev, struct mbo *mbo)
318 {
319         u8 *data = mbo->virt_address;
320         u8 *mac = dev->mac_addrs;
321
322         pr_info("Node Address: 0x%03x\n", (u16)data[16] << 8 | data[17]);
323         dev->link_state = data[18];
324         pr_info("NIState: %d\n", dev->link_state);
325         memcpy(mac, data + 19, 6);
326         pr_info("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
327                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
328         dev->deliver_netinfo++;
329         wake_up_interruptible(&dev->netinfo_waitq);
330 }
331
332 /**
333  * service_done_flag - handle completed buffers
334  * @dev: private data
335  * @ch_idx: channel index
336  *
337  * Return back the completed buffers to mostcore, using completion callback
338  */
339 static void service_done_flag(struct dim2_hdm *dev, int ch_idx)
340 {
341         struct hdm_channel *hdm_ch = dev->hch + ch_idx;
342         struct dim_ch_state_t st;
343         struct list_head *head;
344         struct mbo *mbo;
345         int done_buffers;
346         unsigned long flags;
347         u8 *data;
348
349         BUG_ON(hdm_ch == 0);
350         BUG_ON(!hdm_ch->is_initialized);
351
352         spin_lock_irqsave(&dim_lock, flags);
353
354         done_buffers = DIM_GetChannelState(&hdm_ch->ch, &st)->done_buffers;
355         if (!done_buffers) {
356                 spin_unlock_irqrestore(&dim_lock, flags);
357                 return;
358         }
359
360         if (!DIM_DetachBuffers(&hdm_ch->ch, done_buffers)) {
361                 spin_unlock_irqrestore(&dim_lock, flags);
362                 return;
363         }
364         spin_unlock_irqrestore(&dim_lock, flags);
365
366         head = &hdm_ch->started_list;
367
368         while (done_buffers) {
369                 spin_lock_irqsave(&dim_lock, flags);
370                 if (list_empty(head)) {
371                         spin_unlock_irqrestore(&dim_lock, flags);
372                         pr_crit("hard error: started_mbo list is empty "
373                                 "whereas DIM2 has sent buffers\n");
374                         break;
375                 }
376
377                 mbo = list_entry(head->next, struct mbo, list);
378                 list_del(head->next);
379                 spin_unlock_irqrestore(&dim_lock, flags);
380
381                 data = mbo->virt_address;
382
383                 if (hdm_ch->data_type == MOST_CH_ASYNC &&
384                     hdm_ch->direction == MOST_CH_RX &&
385                     PACKET_IS_NET_INFO(data)) {
386
387                         retrieve_netinfo(dev, mbo);
388
389                         spin_lock_irqsave(&dim_lock, flags);
390                         list_add_tail(&mbo->list, &hdm_ch->pending_list);
391                         spin_unlock_irqrestore(&dim_lock, flags);
392                 } else {
393                         if (hdm_ch->data_type == MOST_CH_CONTROL ||
394                             hdm_ch->data_type == MOST_CH_ASYNC) {
395
396                                 u32 const data_size =
397                                         (u32)data[0] * 256 + data[1] + 2;
398
399                                 mbo->processed_length =
400                                         min(data_size, (u32)mbo->buffer_length);
401                         } else {
402                                 mbo->processed_length = mbo->buffer_length;
403                         }
404                         mbo->status = MBO_SUCCESS;
405                         mbo->complete(mbo);
406                 }
407
408                 done_buffers--;
409         }
410 }
411
412 static struct dim_channel **get_active_channels(struct dim2_hdm *dev,
413                 struct dim_channel **buffer)
414 {
415         int idx = 0;
416         int ch_idx;
417
418         for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
419                 if (dev->hch[ch_idx].is_initialized)
420                         buffer[idx++] = &dev->hch[ch_idx].ch;
421         }
422         buffer[idx++] = 0;
423
424         return buffer;
425 }
426
427 /**
428  * dim2_tasklet_fn - tasklet function
429  * @data: private data
430  *
431  * Service each initialized channel, if needed
432  */
433 static void dim2_tasklet_fn(unsigned long data)
434 {
435         struct dim2_hdm *dev = (struct dim2_hdm *)data;
436         unsigned long flags;
437         int ch_idx;
438
439         for (ch_idx = 0; ch_idx < DMA_CHANNELS; ch_idx++) {
440                 if (!dev->hch[ch_idx].is_initialized)
441                         continue;
442
443                 spin_lock_irqsave(&dim_lock, flags);
444                 DIM_ServiceChannel(&(dev->hch[ch_idx].ch));
445                 spin_unlock_irqrestore(&dim_lock, flags);
446
447                 service_done_flag(dev, ch_idx);
448                 while (!try_start_dim_transfer(dev->hch + ch_idx))
449                         continue;
450         }
451 }
452
453 /**
454  * dim2_ahb_isr - interrupt service routine
455  * @irq: irq number
456  * @_dev: private data
457  *
458  * Acknowledge the interrupt and schedule a tasklet to service channels.
459  * Return IRQ_HANDLED.
460  */
461 static irqreturn_t dim2_ahb_isr(int irq, void *_dev)
462 {
463         struct dim2_hdm *dev = (struct dim2_hdm *)_dev;
464         struct dim_channel *buffer[DMA_CHANNELS + 1];
465         unsigned long flags;
466
467         spin_lock_irqsave(&dim_lock, flags);
468         DIM_ServiceIrq(get_active_channels(dev, buffer));
469         spin_unlock_irqrestore(&dim_lock, flags);
470
471 #if !defined(ENABLE_HDM_TEST)
472         dim2_tasklet.data = (unsigned long)dev;
473         tasklet_schedule(&dim2_tasklet);
474 #else
475         dim2_tasklet_fn((unsigned long)dev);
476 #endif
477         return IRQ_HANDLED;
478 }
479
480 #if defined(ENABLE_HDM_TEST)
481
482 /*
483  * Utility function used by HAL-simu for calling DIM interrupt handler.
484  * It is used only for TEST PURPOSE.
485  */
486 void raise_dim_interrupt(void)
487 {
488         (void)dim2_ahb_isr(0, test_dev);
489 }
490 #endif
491
492 /**
493  * complete_all_mbos - complete MBO's in a list
494  * @head: list head
495  *
496  * Delete all the entries in list and return back MBO's to mostcore using
497  * completion call back.
498  */
499 static void complete_all_mbos(struct list_head *head)
500 {
501         unsigned long flags;
502         struct mbo *mbo;
503
504         for (;;) {
505                 spin_lock_irqsave(&dim_lock, flags);
506                 if (list_empty(head)) {
507                         spin_unlock_irqrestore(&dim_lock, flags);
508                         break;
509                 }
510
511                 mbo = list_entry(head->next, struct mbo, list);
512                 list_del(head->next);
513                 spin_unlock_irqrestore(&dim_lock, flags);
514
515                 mbo->processed_length = 0;
516                 mbo->status = MBO_E_CLOSE;
517                 mbo->complete(mbo);
518         }
519 }
520
521 /**
522  * configure_channel - initialize a channel
523  * @iface: interface the channel belongs to
524  * @channel: channel to be configured
525  * @channel_config: structure that holds the configuration information
526  *
527  * Receives configuration information from mostcore and initialize
528  * the corresponding channel. Return 0 on success, negative on failure.
529  */
530 static int configure_channel(struct most_interface *most_iface, int ch_idx,
531                              struct most_channel_config *ccfg)
532 {
533         struct dim2_hdm *dev = iface_to_hdm(most_iface);
534         bool const is_tx = ccfg->direction == MOST_CH_TX;
535         u16 const sub_size = ccfg->subbuffer_size;
536         u16 const buf_size = ccfg->buffer_size;
537         u16 new_size;
538         unsigned long flags;
539         u8 hal_ret;
540         int const ch_addr = ch_idx * 2 + 2;
541         struct hdm_channel *const hdm_ch = dev->hch + ch_idx;
542
543         BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
544
545         if (hdm_ch->is_initialized)
546                 return -EPERM;
547
548         switch (ccfg->data_type) {
549         case MOST_CH_CONTROL:
550                 new_size = DIM_NormCtrlAsyncBufferSize(buf_size);
551                 if (new_size == 0) {
552                         pr_err("%s: too small buffer size\n", hdm_ch->name);
553                         return -EINVAL;
554                 }
555                 ccfg->buffer_size = new_size;
556                 if (new_size != buf_size)
557                         pr_warn("%s: fixed buffer size (%d -> %d)\n",
558                                 hdm_ch->name, buf_size, new_size);
559                 spin_lock_irqsave(&dim_lock, flags);
560                 hal_ret = DIM_InitControl(&hdm_ch->ch, is_tx, ch_addr, buf_size);
561                 break;
562         case MOST_CH_ASYNC:
563                 new_size = DIM_NormCtrlAsyncBufferSize(buf_size);
564                 if (new_size == 0) {
565                         pr_err("%s: too small buffer size\n", hdm_ch->name);
566                         return -EINVAL;
567                 }
568                 ccfg->buffer_size = new_size;
569                 if (new_size != buf_size)
570                         pr_warn("%s: fixed buffer size (%d -> %d)\n",
571                                 hdm_ch->name, buf_size, new_size);
572                 spin_lock_irqsave(&dim_lock, flags);
573                 hal_ret = DIM_InitAsync(&hdm_ch->ch, is_tx, ch_addr, buf_size);
574                 break;
575         case MOST_CH_ISOC_AVP:
576                 new_size = DIM_NormIsocBufferSize(buf_size, sub_size);
577                 if (new_size == 0) {
578                         pr_err("%s: invalid sub-buffer size or "
579                                "too small buffer size\n", hdm_ch->name);
580                         return -EINVAL;
581                 }
582                 ccfg->buffer_size = new_size;
583                 if (new_size != buf_size)
584                         pr_warn("%s: fixed buffer size (%d -> %d)\n",
585                                 hdm_ch->name, buf_size, new_size);
586                 spin_lock_irqsave(&dim_lock, flags);
587                 hal_ret = DIM_InitIsoc(&hdm_ch->ch, is_tx, ch_addr, sub_size);
588                 break;
589         case MOST_CH_SYNC:
590                 new_size = DIM_NormSyncBufferSize(buf_size, sub_size);
591                 if (new_size == 0) {
592                         pr_err("%s: invalid sub-buffer size or "
593                                "too small buffer size\n", hdm_ch->name);
594                         return -EINVAL;
595                 }
596                 ccfg->buffer_size = new_size;
597                 if (new_size != buf_size)
598                         pr_warn("%s: fixed buffer size (%d -> %d)\n",
599                                 hdm_ch->name, buf_size, new_size);
600                 spin_lock_irqsave(&dim_lock, flags);
601                 hal_ret = DIM_InitSync(&hdm_ch->ch, is_tx, ch_addr, sub_size);
602                 break;
603         default:
604                 pr_err("%s: configure failed, bad channel type: %d\n",
605                        hdm_ch->name, ccfg->data_type);
606                 return -EINVAL;
607         }
608
609         if (hal_ret != DIM_NO_ERROR) {
610                 spin_unlock_irqrestore(&dim_lock, flags);
611                 pr_err("%s: configure failed (%d), type: %d, is_tx: %d\n",
612                        hdm_ch->name, hal_ret, ccfg->data_type, (int)is_tx);
613                 return -ENODEV;
614         }
615
616         hdm_ch->data_type = ccfg->data_type;
617         hdm_ch->direction = ccfg->direction;
618         hdm_ch->is_initialized = true;
619
620         if (hdm_ch->data_type == MOST_CH_ASYNC &&
621             hdm_ch->direction == MOST_CH_TX &&
622             dev->atx_idx < 0)
623                 dev->atx_idx = ch_idx;
624
625         spin_unlock_irqrestore(&dim_lock, flags);
626
627         return 0;
628 }
629
630 /**
631  * enqueue - enqueue a buffer for data transfer
632  * @iface: intended interface
633  * @channel: ID of the channel the buffer is intended for
634  * @mbo: pointer to the buffer object
635  *
636  * Push the buffer into pending_list and try to transfer one buffer from
637  * pending_list. Return 0 on success, negative on failure.
638  */
639 static int enqueue(struct most_interface *most_iface, int ch_idx,
640                    struct mbo *mbo)
641 {
642         struct dim2_hdm *dev = iface_to_hdm(most_iface);
643         struct hdm_channel *hdm_ch = dev->hch + ch_idx;
644         unsigned long flags;
645
646         BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
647
648         if (!hdm_ch->is_initialized)
649                 return -EPERM;
650
651         if (mbo->bus_address == 0)
652                 return -EFAULT;
653
654         spin_lock_irqsave(&dim_lock, flags);
655         list_add_tail(&mbo->list, &hdm_ch->pending_list);
656         spin_unlock_irqrestore(&dim_lock, flags);
657
658         (void)try_start_dim_transfer(hdm_ch);
659
660         return 0;
661 }
662
663 /**
664  * request_netinfo - triggers retrieving of network info
665  * @iface: pointer to the interface
666  * @channel_id: corresponding channel ID
667  *
668  * Send a command to INIC which triggers retrieving of network info by means of
669  * "Message exchange over MDP/MEP". Return 0 on success, negative on failure.
670  */
671 static void request_netinfo(struct most_interface *most_iface, int ch_idx)
672 {
673         struct dim2_hdm *dev = iface_to_hdm(most_iface);
674         struct mbo *mbo;
675         u8 *data;
676
677         if (dev->atx_idx < 0) {
678                 pr_err("Async Tx Not initialized\n");
679                 return;
680         }
681
682         mbo = most_get_mbo(&dev->most_iface, dev->atx_idx);
683         if (!mbo)
684                 return;
685
686         mbo->buffer_length = 5;
687
688         data = mbo->virt_address;
689
690         data[0] = 0x00; /* PML High byte */
691         data[1] = 0x03; /* PML Low byte */
692         data[2] = 0x02; /* PMHL */
693         data[3] = 0x08; /* FPH */
694         data[4] = 0x40; /* FMF (FIFO cmd msg - Triggers NAOverMDP) */
695
696         most_submit_mbo(mbo);
697 }
698
699 /**
700  * poison_channel - poison buffers of a channel
701  * @iface: pointer to the interface the channel to be poisoned belongs to
702  * @channel_id: corresponding channel ID
703  *
704  * Destroy a channel and complete all the buffers in both started_list &
705  * pending_list. Return 0 on success, negative on failure.
706  */
707 static int poison_channel(struct most_interface *most_iface, int ch_idx)
708 {
709         struct dim2_hdm *dev = iface_to_hdm(most_iface);
710         struct hdm_channel *hdm_ch = dev->hch + ch_idx;
711         unsigned long flags;
712         u8 hal_ret;
713         int ret = 0;
714
715         BUG_ON(ch_idx < 0 || ch_idx >= DMA_CHANNELS);
716
717         if (!hdm_ch->is_initialized)
718                 return -EPERM;
719
720         spin_lock_irqsave(&dim_lock, flags);
721         hal_ret = DIM_DestroyChannel(&hdm_ch->ch);
722         hdm_ch->is_initialized = false;
723         if (ch_idx == dev->atx_idx)
724                 dev->atx_idx = -1;
725         spin_unlock_irqrestore(&dim_lock, flags);
726         if (hal_ret != DIM_NO_ERROR) {
727                 pr_err("HAL Failed to close channel %s\n", hdm_ch->name);
728                 ret = -EFAULT;
729         }
730
731         complete_all_mbos(&hdm_ch->started_list);
732         complete_all_mbos(&hdm_ch->pending_list);
733
734         return ret;
735 }
736
737 /*
738  * dim2_probe - dim2 probe handler
739  * @pdev: platform device structure
740  *
741  * Register the dim2 interface with mostcore and initialize it.
742  * Return 0 on success, negative on failure.
743  */
744 static int dim2_probe(struct platform_device *pdev)
745 {
746         struct dim2_hdm *dev;
747         struct resource *res;
748         int ret, i;
749         struct kobject *kobj;
750
751         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
752         if (!dev)
753                 return -ENOMEM;
754
755         dev->atx_idx = -1;
756
757         platform_set_drvdata(pdev, dev);
758 #if defined(ENABLE_HDM_TEST)
759         test_dev = dev;
760 #else
761         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
762         if (!res) {
763                 pr_err("no memory region defined\n");
764                 ret = -ENOENT;
765                 goto err_free_dev;
766         }
767
768         if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
769                 pr_err("failed to request mem region\n");
770                 ret = -EBUSY;
771                 goto err_free_dev;
772         }
773
774         dev->io_base = ioremap(res->start, resource_size(res));
775         if (!dev->io_base) {
776                 pr_err("failed to ioremap\n");
777                 ret = -ENOMEM;
778                 goto err_release_mem;
779         }
780
781         ret = platform_get_irq(pdev, 0);
782         if (ret < 0) {
783                 pr_err("failed to get irq\n");
784                 goto err_unmap_io;
785         }
786         dev->irq_ahb0 = ret;
787
788         ret = request_irq(dev->irq_ahb0, dim2_ahb_isr, 0, "mlb_ahb0", dev);
789         if (ret) {
790                 pr_err("failed to request IRQ: %d, err: %d\n", dev->irq_ahb0, ret);
791                 goto err_unmap_io;
792         }
793 #endif
794         init_waitqueue_head(&dev->netinfo_waitq);
795         dev->deliver_netinfo = 0;
796         dev->netinfo_task = kthread_run(&deliver_netinfo_thread, (void *)dev,
797                                         "dim2_netinfo");
798         if (IS_ERR(dev->netinfo_task)) {
799                 ret = PTR_ERR(dev->netinfo_task);
800                 goto err_free_irq;
801         }
802
803         for (i = 0; i < DMA_CHANNELS; i++) {
804                 struct most_channel_capability *cap = dev->capabilities + i;
805                 struct hdm_channel *hdm_ch = dev->hch + i;
806
807                 INIT_LIST_HEAD(&hdm_ch->pending_list);
808                 INIT_LIST_HEAD(&hdm_ch->started_list);
809                 hdm_ch->is_initialized = false;
810                 snprintf(hdm_ch->name, sizeof(hdm_ch->name), "ca%d", i * 2 + 2);
811
812                 cap->name_suffix = hdm_ch->name;
813                 cap->direction = MOST_CH_RX | MOST_CH_TX;
814                 cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
815                                  MOST_CH_ISOC_AVP | MOST_CH_SYNC;
816                 cap->num_buffers_packet = MAX_BUFFERS_PACKET;
817                 cap->buffer_size_packet = MAX_BUF_SIZE_PACKET;
818                 cap->num_buffers_streaming = MAX_BUFFERS_STREAMING;
819                 cap->buffer_size_streaming = MAX_BUF_SIZE_STREAMING;
820         }
821
822         {
823                 const char *fmt;
824
825                 if (sizeof(res->start) == sizeof(long long))
826                         fmt = "dim2-%016llx";
827                 else if (sizeof(res->start) == sizeof(long))
828                         fmt = "dim2-%016lx";
829                 else
830                         fmt = "dim2-%016x";
831
832                 snprintf(dev->name, sizeof(dev->name), fmt, res->start);
833         }
834
835         dev->most_iface.interface = ITYPE_MEDIALB_DIM2;
836         dev->most_iface.description = dev->name;
837         dev->most_iface.num_channels = DMA_CHANNELS;
838         dev->most_iface.channel_vector = dev->capabilities;
839         dev->most_iface.configure = configure_channel;
840         dev->most_iface.enqueue = enqueue;
841         dev->most_iface.poison_channel = poison_channel;
842         dev->most_iface.request_netinfo = request_netinfo;
843
844         kobj = most_register_interface(&dev->most_iface);
845         if (IS_ERR(kobj)) {
846                 ret = PTR_ERR(kobj);
847                 pr_err("failed to register MOST interface\n");
848                 goto err_stop_thread;
849         }
850
851         ret = dim2_sysfs_probe(&dev->bus, kobj);
852         if (ret)
853                 goto err_unreg_iface;
854
855         ret = startup_dim(pdev);
856         if (ret) {
857                 pr_err("failed to initialize DIM2\n");
858                 goto err_destroy_bus;
859         }
860
861         return 0;
862
863 err_destroy_bus:
864         dim2_sysfs_destroy(&dev->bus);
865 err_unreg_iface:
866         most_deregister_interface(&dev->most_iface);
867 err_stop_thread:
868         kthread_stop(dev->netinfo_task);
869 err_free_irq:
870 #if !defined(ENABLE_HDM_TEST)
871         free_irq(dev->irq_ahb0, dev);
872 err_unmap_io:
873         iounmap(dev->io_base);
874 err_release_mem:
875         release_mem_region(res->start, resource_size(res));
876 err_free_dev:
877 #endif
878         kfree(dev);
879
880         return ret;
881 }
882
883 /**
884  * dim2_remove - dim2 remove handler
885  * @pdev: platform device structure
886  *
887  * Unregister the interface from mostcore
888  */
889 static int dim2_remove(struct platform_device *pdev)
890 {
891         struct dim2_hdm *dev = platform_get_drvdata(pdev);
892         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
893         struct dim2_platform_data *pdata = pdev->dev.platform_data;
894         unsigned long flags;
895
896         spin_lock_irqsave(&dim_lock, flags);
897         DIM_Shutdown();
898         spin_unlock_irqrestore(&dim_lock, flags);
899
900         if (pdata && pdata->destroy)
901                 pdata->destroy(pdata);
902
903         dim2_sysfs_destroy(&dev->bus);
904         most_deregister_interface(&dev->most_iface);
905         kthread_stop(dev->netinfo_task);
906 #if !defined(ENABLE_HDM_TEST)
907         free_irq(dev->irq_ahb0, dev);
908         iounmap(dev->io_base);
909         release_mem_region(res->start, resource_size(res));
910 #endif
911         kfree(dev);
912         platform_set_drvdata(pdev, NULL);
913
914         /*
915          * break link to local platform_device_id struct
916          * to prevent crash by unload platform device module
917          */
918         pdev->id_entry = 0;
919
920         return 0;
921 }
922
923 static struct platform_device_id dim2_id[] = {
924         { "medialb_dim2" },
925         { }, /* Terminating entry */
926 };
927
928 MODULE_DEVICE_TABLE(platform, dim2_id);
929
930 static struct platform_driver dim2_driver = {
931         .probe = dim2_probe,
932         .remove = dim2_remove,
933         .id_table = dim2_id,
934         .driver = {
935                 .name = "hdm_dim2",
936                 .owner = THIS_MODULE,
937         },
938 };
939
940 /**
941  * dim2_hdm_init - Driver Registration Routine
942  */
943 static int __init dim2_hdm_init(void)
944 {
945         pr_info("dim2_hdm_init()\n");
946         return platform_driver_register(&dim2_driver);
947 }
948
949 /**
950  * dim2_hdm_exit - Driver Cleanup Routine
951  **/
952 static void __exit dim2_hdm_exit(void)
953 {
954         pr_info("dim2_hdm_exit()\n");
955         platform_driver_unregister(&dim2_driver);
956 }
957
958 module_init(dim2_hdm_init);
959 module_exit(dim2_hdm_exit);
960
961 MODULE_AUTHOR("Jain Roy Ambi <JainRoy.Ambi@microchip.com>");
962 MODULE_AUTHOR("Andrey Shvetsov <andrey.shvetsov@k2l.de>");
963 MODULE_DESCRIPTION("MediaLB DIM2 Hardware Dependent Module");
964 MODULE_LICENSE("GPL");