summaryrefslogtreecommitdiffstats
path: root/Documentation/DocBook/writing_musb_glue_layer.tmpl
blob: 837eca77f274236464225ba280d32a4898640f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
	"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>

<book id="Writing-MUSB-Glue-Layer">
 <bookinfo>
  <title>Writing an MUSB Glue Layer</title>

  <authorgroup>
   <author>
    <firstname>Apelete</firstname>
    <surname>Seketeli</surname>
    <affiliation>
     <address>
      <email>apelete at seketeli.net</email>
     </address>
    </affiliation>
   </author>
  </authorgroup>

  <copyright>
   <year>2014</year>
   <holder>Apelete Seketeli</holder>
  </copyright>

  <legalnotice>
   <para>
     This documentation is free software; you can redistribute it
     and/or modify it under the terms of the GNU General Public
     License as published by the Free Software Foundation; either
     version 2 of the License, or (at your option) any later version.
   </para>

   <para>
     This documentation is distributed in the hope that it will be
     useful, but WITHOUT ANY WARRANTY; without even the implied
     warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     See the GNU General Public License for more details.
   </para>

   <para>
     You should have received a copy of the GNU General Public License
     along with this documentation; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
     02111-1307 USA
   </para>

   <para>
     For more details see the file COPYING in the Linux kernel source
     tree.
   </para>
  </legalnotice>
 </bookinfo>

<toc></toc>

  <chapter id="introduction">
    <title>Introduction</title>
    <para>
      The Linux MUSB subsystem is part of the larger Linux USB
      subsystem. It provides support for embedded USB Device Controllers
      (UDC) that do not use Universal Host Controller Interface (UHCI)
      or Open Host Controller Interface (OHCI).
    </para>
    <para>
      Instead, these embedded UDC rely on the USB On-the-Go (OTG)
      specification which they implement at least partially. The silicon
      reference design used in most cases is the Multipoint USB
      Highspeed Dual-Role Controller (MUSB HDRC) found in the Mentor
      Graphics Inventra™ design.
    </para>
    <para>
      As a self-taught exercise I have written an MUSB glue layer for
      the Ingenic JZ4740 SoC, modelled after the many MUSB glue layers
      in the kernel source tree. This layer can be found at
      drivers/usb/musb/jz4740.c. In this documentation I will walk
      through the basics of the jz4740.c glue layer, explaining the
      different pieces and what needs to be done in order to write your
      own device glue layer.
    </para>
  </chapter>

  <chapter id="linux-musb-basics">
    <title>Linux MUSB Basics</title>
    <para>
      To get started on the topic, please read USB On-the-Go Basics (see
      Resources) which provides an introduction of USB OTG operation at
      the hardware level. A couple of wiki pages by Texas Instruments
      and Analog Devices also provide an overview of the Linux kernel
      MUSB configuration, albeit focused on some specific devices
      provided by these companies. Finally, getting acquainted with the
      USB specification at USB home page may come in handy, with
      practical instance provided through the Writing USB Device Drivers
      documentation (again, see Resources).
    </para>
    <para>
      Linux USB stack is a layered architecture in which the MUSB
      controller hardware sits at the lowest. The MUSB controller driver
      abstract the MUSB controller hardware to the Linux USB stack.
    </para>
    <programlisting>
      ------------------------
      |                      | &lt;------- drivers/usb/gadget
      | Linux USB Core Stack | &lt;------- drivers/usb/host
      |                      | &lt;------- drivers/usb/core
      ------------------------

     --------------------------
     |                        | &lt;------ drivers/usb/musb/musb_gadget.c
     | MUSB Controller driver | &lt;------ drivers/usb/musb/musb_host.c
     |                        | &lt;------ drivers/usb/musb/musb_core.c
     --------------------------

  ---------------------------------
  | MUSB Platform Specific Driver |
  |                               | &lt;-- drivers/usb/musb/jz4740.c
  |       aka &quot;Glue Layer&quot;        |
  ---------------------------------

  ---------------------------------
  |   MUSB Controller Hardware    |
  ---------------------------------
    </programlisting>
    <para>
      As outlined above, the glue layer is actually the platform
      specific code sitting in between the controller driver and the
      controller hardware.
    </para>
    <para>
      Just like a Linux USB driver needs to register itself with the
      Linux USB subsystem, the MUSB glue layer needs first to register
      itself with the MUSB controller driver. This will allow the
      controller driver to know about which device the glue layer
      supports and which functions to call when a supported device is
      detected or released; remember we are talking about an embedded
      controller chip here, so no insertion or removal at run-time.
    </para>
    <para>
      All of this information is passed to the MUSB controller driver
      through a platform_driver structure defined in the glue layer as:
    </para>
    <programlisting linenumbering="numbered">
static struct platform_driver jz4740_driver = {
	.probe		= jz4740_probe,
	.remove		= jz4740_remove,
	.driver		= {
		.name	= "musb-jz4740",
	},
};
    </programlisting>
    <para>
      The probe and remove function pointers are called when a matching
      device is detected and, respectively, released. The name string
      describes the device supported by this glue layer. In the current
      case it matches a platform_device structure declared in
      arch/mips/jz4740/platform.c. Note that we are not using device
      tree bindings here.
    </para>
    <para>
      In order to register itself to the controller driver, the glue
      layer goes through a few steps, basically allocating the
      controller hardware resources and initialising a couple of
      circuits. To do so, it needs to keep track of the information used
      throughout these steps. This is done by defining a private
      jz4740_glue structure:
    </para>
    <programlisting linenumbering="numbered">
struct jz4740_glue {
	struct device           *dev;
	struct platform_device  *musb;
	struct clk		*clk;
};
    </programlisting>
    <para>
      The dev and musb members are both device structure variables. The
      first one holds generic information about the device, since it's
      the basic device structure, and the latter holds information more
      closely related to the subsystem the device is registered to. The
      clk variable keeps information related to the device clock
      operation.
    </para>
    <para>
      Let's go through the steps of the probe function that leads the
      glue layer to register itself to the controller driver.
    </para>
    <para>
      N.B.: For the sake of readability each function will be split in
      logical parts, each part being shown as if it was independent from
      the others.
    </para>
    <programlisting linenumbering="numbered">
static int jz4740_probe(struct platform_device *pdev)
{
	struct platform_device		*musb;
	struct jz4740_glue		*glue;
	struct clk                      *clk;
	int				ret;

	glue = devm_kzalloc(&amp;pdev->dev, sizeof(*glue), GFP_KERNEL);
	if (!glue)
		return -ENOMEM;

	musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
	if (!musb) {
		dev_err(&amp;pdev->dev, "failed to allocate musb device\n");
		return -ENOMEM;
	}

	clk = devm_clk_get(&amp;pdev->dev, "udc");
	if (IS_ERR(clk)) {
		dev_err(&amp;pdev->dev, "failed to get clock\n");
		ret = PTR_ERR(clk);
		goto err_platform_device_put;
	}

	ret = clk_prepare_enable(clk);
	if (ret) {
		dev_err(&amp;pdev->dev, "failed to enable clock\n");
		goto err_platform_device_put;
	}

	musb->dev.parent		= &amp;pdev->dev;

	glue->dev			= &amp;pdev->dev;
	glue->musb			= musb;
	glue->clk			= clk;

	return 0;

err_platform_device_put:
	platform_device_put(musb);
	return ret;
}
    </programlisting>
    <para>
      The first few lines of the probe function allocate and assign the
      glue, musb and clk variables. The GFP_KERNEL flag (line 8) allows
      the allocation process to sleep and wait for memory, thus being
      usable in a blocking situation. The PLATFORM_DEVID_AUTO flag (line
      12) allows automatic allocation and management of device IDs in
      order to avoid device namespace collisions with explicit IDs. With
      devm_clk_get() (line 18) the glue layer allocates the clock -- the
      <literal>devm_</literal> prefix indicates that clk_get() is
      managed: it automatically frees the allocated clock resource data
      when the device is released -- and enable it.
    </para>
    <para>
      Then comes the registration steps:
    </para>
    <programlisting linenumbering="numbered">
static int jz4740_probe(struct platform_device *pdev)
{
	struct musb_hdrc_platform_data	*pdata = &amp;jz4740_musb_platform_data;

	pdata->platform_ops		= &amp;jz4740_musb_ops;

	platform_set_drvdata(pdev, glue);

	ret = platform_device_add_resources(musb, pdev->resource,
					    pdev->num_resources);
	if (ret) {
		dev_err(&amp;pdev->dev, "failed to add resources\n");
		goto err_clk_disable;
	}

	ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
	if (ret) {
		dev_err(&amp;pdev->dev, "failed to add platform_data\n");
		goto err_clk_disable;
	}

	return 0;

err_clk_disable:
	clk_disable_unprepare(clk);
err_platform_device_put:
	platform_device_put(musb);
	return ret;
}
    </programlisting>
    <para>
      The first step is to pass the device data privately held by the
      glue layer on to the controller driver through
      platform_set_drvdata() (line 7). Next is passing on the device
      resources information, also privately held at that point, through
      platform_device_add_resources() (line 9).
    </para>
    <para>
      Finally comes passing on the platform specific data to the
      controller driver (line 16). Platform data will be discussed in
      <link linkend="device-platform-data">Chapter 4</link>, but here
      we are looking at the platform_ops function pointer (line 5) in
      musb_hdrc_platform_data structure (line 3).  This function
      pointer allows the MUSB controller driver to know which function
      to call for device operation:
    </para>
    <programlisting linenumbering="numbered">
static const struct musb_platform_ops jz4740_musb_ops = {
	.init		= jz4740_musb_init,
	.exit		= jz4740_musb_exit,
};
    </programlisting>
    <para>
      Here we have the minimal case where only init and exit functions
      are called by the controller driver when needed. Fact is the
      JZ4740 MUSB controller is a basic controller, lacking some
      features found in other controllers, otherwise we may also have
      pointers to a few other functions like a power management function
      or a function to switch between OTG and non-OTG modes, for
      instance.
    </para>
    <para>
      At that point of the registration process, the controller driver
      actually calls the init function:
    </para>
    <programlisting linenumbering="numbered">
static int jz4740_musb_init(struct musb *musb)
{
	musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
	if (!musb->xceiv) {
		pr_err("HS UDC: no transceiver configured\n");
		return -ENODEV;
	}

	/* Silicon does not implement ConfigData register.
	 * Set dyn_fifo to avoid reading EP config from hardware.
	 */
	musb->dyn_fifo = true;

	musb->isr = jz4740_musb_interrupt;

	return 0;
}
    </programlisting>
    <para>
      The goal of jz4740_musb_init() is to get hold of the transceiver
      driver data of the MUSB controller hardware and pass it on to the
      MUSB controller driver, as usual. The transceiver is the circuitry
      inside the controller hardware responsible for sending/receiving
      the USB data. Since it is an implementation of the physical layer
      of the OSI model, the transceiver is also referred to as PHY.
    </para>
    <para>
      Getting hold of the MUSB PHY driver data is done with
      usb_get_phy() which returns a pointer to the structure
      containing the driver instance data. The next couple of
      instructions (line 12 and 14) are used as a quirk and to setup
      IRQ handling respectively. Quirks and IRQ handling will be
      discussed later in <link linkend="device-quirks">Chapter
      5</link> and <link linkend="handling-irqs">Chapter 3</link>.
    </para>
    <programlisting linenumbering="numbered">
static int jz4740_musb_exit(struct musb *musb)
{
	usb_put_phy(musb->xceiv);

	return 0;
}
    </programlisting>
    <para>
      Acting as the counterpart of init, the exit function releases the
      MUSB PHY driver when the controller hardware itself is about to be
      released.
    </para>
    <para>
      Again, note that init and exit are fairly simple in this case due
      to the basic set of features of the JZ4740 controller hardware.
      When writing an musb glue layer for a more complex controller
      hardware, you might need to take care of more processing in those
      two functions.
    </para>
    <para>
      Returning from the init function, the MUSB controller driver jumps
      back into the probe function:
    </para>
    <programlisting linenumbering="numbered">
static int jz4740_probe(struct platform_device *pdev)
{
	ret = platform_device_add(musb);
	if (ret) {
		dev_err(&amp;pdev->dev, "failed to register musb device\n");
		goto err_clk_disable;
	}

	return 0;

err_clk_disable:
	clk_disable_unprepare(clk);
err_platform_device_put:
	platform_device_put(musb);
	return ret;
}
    </programlisting>
    <para>
      This is the last part of the device registration process where the
      glue layer adds the controller hardware device to Linux kernel
      device hierarchy: at this stage, all known information about the
      device is passed on to the Linux USB core stack.
    </para>
    <programlisting linenumbering="numbered">
static int jz4740_remove(struct platform_device *pdev)
{
	struct jz4740_glue	*glue = platform_get_drvdata(pdev);

	platform_device_unregister(glue->musb);
	clk_disable_unprepare(glue->clk);

	return 0;
}
    </programlisting>
    <para>
      Acting as the counterpart of probe, the remove function unregister
      the MUSB controller hardware (line 5) and disable the clock (line
      6), allowing it to be gated.
    </para>
  </chapter>

  <chapter id="handling-irqs">
    <title>Handling IRQs</title>
    <para>
      Additionally to the MUSB controller hardware basic setup and
      registration, the glue layer is also responsible for handling the
      IRQs:
    </para>
    <programlisting linenumbering="numbered">
static irqreturn_t jz4740_musb_interrupt(int irq, void *__hci)
{
	unsigned long   flags;
	irqreturn_t     retval = IRQ_NONE;
	struct musb     *musb = __hci;

	spin_lock_irqsave(&amp;musb->lock, flags);

	musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
	musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
	musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);

	/*
	 * The controller is gadget only, the state of the host mode IRQ bits is
	 * undefined. Mask them to make sure that the musb driver core will
	 * never see them set
	 */
	musb->int_usb &amp;= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME |
	    MUSB_INTR_RESET | MUSB_INTR_SOF;

	if (musb->int_usb || musb->int_tx || musb->int_rx)
		retval = musb_interrupt(musb);

	spin_unlock_irqrestore(&amp;musb->lock, flags);

	return retval;
}
    </programlisting>
    <para>
      Here the glue layer mostly has to read the relevant hardware
      registers and pass their values on to the controller driver which
      will handle the actual event that triggered the IRQ.
    </para>
    <para>
      The interrupt handler critical section is protected by the
      spin_lock_irqsave() and counterpart spin_unlock_irqrestore()
      functions (line 7 and 24 respectively), which prevent th