Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

writing_musb_glue_layer.rst: Enrich its ReST representation

This file is actually quite complex, and required several
manual handwork:

- add a title for the document;
- use the right tags for monospaced fonts;
- use c references where needed;
- adjust cross-reference to writing_usb_driver.rst
- hightlight cross-referenced lines.

With regards to C code snippet line highlights, the better would be
to use :linenos: for the C code snippets that are referenced by
the line number. However, at least with Sphinx 1.4.9, enabling
it cause the line number to be misaligned with the code,
making it even more confusing. So, instead, let's use
:emphasize-lines: tag to mark the lines that are referenced
at the text.

Signed-off-by: Mauro Carvalho Chehab <mchehab@s-opensource.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Jonathan Corbet <corbet@lwn.net>

authored by

Mauro Carvalho Chehab and committed by
Jonathan Corbet
67cc20e0 0e8c46d0

+292 -306
+292 -306
Documentation/driver-api/usb/writing_musb_glue_layer.rst
··· 1 - ========================== 2 - Writing an MUSB Glue Layer 3 - ========================== 1 + ========================= 2 + Writing a MUSB Glue Layer 3 + ========================= 4 4 5 5 :Author: Apelete Seketeli 6 6 ··· 21 21 As a self-taught exercise I have written an MUSB glue layer for the 22 22 Ingenic JZ4740 SoC, modelled after the many MUSB glue layers in the 23 23 kernel source tree. This layer can be found at 24 - drivers/usb/musb/jz4740.c. In this documentation I will walk through the 25 - basics of the jz4740.c glue layer, explaining the different pieces and 24 + ``drivers/usb/musb/jz4740.c``. In this documentation I will walk through the 25 + basics of the ``jz4740.c`` glue layer, explaining the different pieces and 26 26 what needs to be done in order to write your own device glue layer. 27 + 28 + .. _musb-basics: 27 29 28 30 Linux MUSB Basics 29 31 ================= ··· 41 39 42 40 Linux USB stack is a layered architecture in which the MUSB controller 43 41 hardware sits at the lowest. The MUSB controller driver abstract the 44 - MUSB controller hardware to the Linux USB stack. 42 + MUSB controller hardware to the Linux USB stack:: 45 43 46 - :: 47 - 48 - ------------------------ 49 - | | <------- drivers/usb/gadget 50 - | Linux USB Core Stack | <------- drivers/usb/host 51 - | | <------- drivers/usb/core 52 - ------------------------ 53 - 54 - -------------------------- 55 - | | <------ drivers/usb/musb/musb_gadget.c 56 - | MUSB Controller driver | <------ drivers/usb/musb/musb_host.c 57 - | | <------ drivers/usb/musb/musb_core.c 58 - -------------------------- 59 - 44 + ------------------------ 45 + | | <------- drivers/usb/gadget 46 + | Linux USB Core Stack | <------- drivers/usb/host 47 + | | <------- drivers/usb/core 48 + ------------------------ 49 + 50 + -------------------------- 51 + | | <------ drivers/usb/musb/musb_gadget.c 52 + | MUSB Controller driver | <------ drivers/usb/musb/musb_host.c 53 + | | <------ drivers/usb/musb/musb_core.c 54 + -------------------------- 55 + 60 56 --------------------------------- 61 57 | MUSB Platform Specific Driver | 62 58 | | <-- drivers/usb/musb/jz4740.c 63 59 | aka "Glue Layer" | 64 60 --------------------------------- 65 - 61 + 66 62 --------------------------------- 67 63 | MUSB Controller Hardware | 68 64 --------------------------------- 69 - 70 65 71 66 As outlined above, the glue layer is actually the platform specific code 72 67 sitting in between the controller driver and the controller hardware. ··· 77 78 run-time. 78 79 79 80 All of this information is passed to the MUSB controller driver through 80 - a platform_driver structure defined in the glue layer as: 81 - 82 - :: 81 + a :c:type:`platform_driver` structure defined in the glue layer as:: 83 82 84 83 static struct platform_driver jz4740_driver = { 85 - .probe = jz4740_probe, 86 - .remove = jz4740_remove, 87 - .driver = { 88 - .name = "musb-jz4740", 89 - }, 84 + .probe = jz4740_probe, 85 + .remove = jz4740_remove, 86 + .driver = { 87 + .name = "musb-jz4740", 88 + }, 90 89 }; 91 - 92 90 93 91 The probe and remove function pointers are called when a matching device 94 92 is detected and, respectively, released. The name string describes the 95 93 device supported by this glue layer. In the current case it matches a 96 - platform_device structure declared in arch/mips/jz4740/platform.c. Note 94 + platform_device structure declared in ``arch/mips/jz4740/platform.c``. Note 97 95 that we are not using device tree bindings here. 98 96 99 97 In order to register itself to the controller driver, the glue layer 100 98 goes through a few steps, basically allocating the controller hardware 101 99 resources and initialising a couple of circuits. To do so, it needs to 102 100 keep track of the information used throughout these steps. This is done 103 - by defining a private jz4740_glue structure: 104 - 105 - :: 101 + by defining a private ``jz4740_glue`` structure:: 106 102 107 103 struct jz4740_glue { 108 - struct device *dev; 109 - struct platform_device *musb; 110 - struct clk *clk; 104 + struct device *dev; 105 + struct platform_device *musb; 106 + struct clk *clk; 111 107 }; 112 108 113 109 ··· 115 121 Let's go through the steps of the probe function that leads the glue 116 122 layer to register itself to the controller driver. 117 123 118 - N.B.: For the sake of readability each function will be split in logical 119 - parts, each part being shown as if it was independent from the others. 124 + .. note:: 120 125 121 - :: 126 + For the sake of readability each function will be split in logical 127 + parts, each part being shown as if it was independent from the others. 128 + 129 + .. code-block:: c 130 + :emphasize-lines: 8,12,18 122 131 123 132 static int jz4740_probe(struct platform_device *pdev) 124 133 { 125 - struct platform_device *musb; 126 - struct jz4740_glue *glue; 127 - struct clk *clk; 128 - int ret; 134 + struct platform_device *musb; 135 + struct jz4740_glue *glue; 136 + struct clk *clk; 137 + int ret; 129 138 130 - glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL); 131 - if (!glue) 132 - return -ENOMEM; 139 + glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL); 140 + if (!glue) 141 + return -ENOMEM; 133 142 134 - musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO); 135 - if (!musb) { 136 - dev_err(&pdev->dev, "failed to allocate musb device\n"); 137 - return -ENOMEM; 138 - } 143 + musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO); 144 + if (!musb) { 145 + dev_err(&pdev->dev, "failed to allocate musb device\n"); 146 + return -ENOMEM; 147 + } 139 148 140 - clk = devm_clk_get(&pdev->dev, "udc"); 141 - if (IS_ERR(clk)) { 142 - dev_err(&pdev->dev, "failed to get clock\n"); 143 - ret = PTR_ERR(clk); 144 - goto err_platform_device_put; 145 - } 149 + clk = devm_clk_get(&pdev->dev, "udc"); 150 + if (IS_ERR(clk)) { 151 + dev_err(&pdev->dev, "failed to get clock\n"); 152 + ret = PTR_ERR(clk); 153 + goto err_platform_device_put; 154 + } 146 155 147 - ret = clk_prepare_enable(clk); 148 - if (ret) { 149 - dev_err(&pdev->dev, "failed to enable clock\n"); 150 - goto err_platform_device_put; 151 - } 156 + ret = clk_prepare_enable(clk); 157 + if (ret) { 158 + dev_err(&pdev->dev, "failed to enable clock\n"); 159 + goto err_platform_device_put; 160 + } 152 161 153 - musb->dev.parent = &pdev->dev; 162 + musb->dev.parent = &pdev->dev; 154 163 155 - glue->dev = &pdev->dev; 156 - glue->musb = musb; 157 - glue->clk = clk; 164 + glue->dev = &pdev->dev; 165 + glue->musb = musb; 166 + glue->clk = clk; 158 167 159 - return 0; 168 + return 0; 160 169 161 170 err_platform_device_put: 162 - platform_device_put(musb); 163 - return ret; 171 + platform_device_put(musb); 172 + return ret; 164 173 } 165 174 166 - 167 175 The first few lines of the probe function allocate and assign the glue, 168 - musb and clk variables. The GFP_KERNEL flag (line 8) allows the 176 + musb and clk variables. The ``GFP_KERNEL`` flag (line 8) allows the 169 177 allocation process to sleep and wait for memory, thus being usable in a 170 - blocking situation. The PLATFORM_DEVID_AUTO flag (line 12) allows 178 + locking situation. The ``PLATFORM_DEVID_AUTO`` flag (line 12) allows 171 179 automatic allocation and management of device IDs in order to avoid 172 - device namespace collisions with explicit IDs. With devm_clk_get() 180 + device namespace collisions with explicit IDs. With :c:func:`devm_clk_get` 173 181 (line 18) the glue layer allocates the clock -- the ``devm_`` prefix 174 - indicates that clk_get() is managed: it automatically frees the 182 + indicates that :c:func:`clk_get` is managed: it automatically frees the 175 183 allocated clock resource data when the device is released -- and enable 176 184 it. 177 185 186 + 187 + 178 188 Then comes the registration steps: 179 189 180 - :: 190 + .. code-block:: c 191 + :emphasize-lines: 3,5,7,9,16 181 192 182 193 static int jz4740_probe(struct platform_device *pdev) 183 194 { 184 - struct musb_hdrc_platform_data *pdata = &jz4740_musb_platform_data; 195 + struct musb_hdrc_platform_data *pdata = &jz4740_musb_platform_data; 185 196 186 - pdata->platform_ops = &jz4740_musb_ops; 197 + pdata->platform_ops = &jz4740_musb_ops; 187 198 188 - platform_set_drvdata(pdev, glue); 199 + platform_set_drvdata(pdev, glue); 189 200 190 - ret = platform_device_add_resources(musb, pdev->resource, 191 - pdev->num_resources); 192 - if (ret) { 193 - dev_err(&pdev->dev, "failed to add resources\n"); 194 - goto err_clk_disable; 195 - } 201 + ret = platform_device_add_resources(musb, pdev->resource, 202 + pdev->num_resources); 203 + if (ret) { 204 + dev_err(&pdev->dev, "failed to add resources\n"); 205 + goto err_clk_disable; 206 + } 196 207 197 - ret = platform_device_add_data(musb, pdata, sizeof(*pdata)); 198 - if (ret) { 199 - dev_err(&pdev->dev, "failed to add platform_data\n"); 200 - goto err_clk_disable; 201 - } 208 + ret = platform_device_add_data(musb, pdata, sizeof(*pdata)); 209 + if (ret) { 210 + dev_err(&pdev->dev, "failed to add platform_data\n"); 211 + goto err_clk_disable; 212 + } 202 213 203 - return 0; 214 + return 0; 204 215 205 216 err_clk_disable: 206 - clk_disable_unprepare(clk); 217 + clk_disable_unprepare(clk); 207 218 err_platform_device_put: 208 - platform_device_put(musb); 209 - return ret; 219 + platform_device_put(musb); 220 + return ret; 210 221 } 211 222 212 - 213 223 The first step is to pass the device data privately held by the glue 214 - layer on to the controller driver through platform_set_drvdata() (line 215 - 7). Next is passing on the device resources information, also privately 216 - held at that point, through platform_device_add_resources() (line 9). 224 + layer on to the controller driver through :c:func:`platform_set_drvdata` 225 + (line 7). Next is passing on the device resources information, also privately 226 + held at that point, through :c:func:`platform_device_add_resources` (line 9). 217 227 218 228 Finally comes passing on the platform specific data to the controller 219 - driver (line 16). Platform data will be discussed in `Chapter 220 - 4 <#device-platform-data>`__, but here we are looking at the 221 - platform_ops function pointer (line 5) in musb_hdrc_platform_data 229 + driver (line 16). Platform data will be discussed in 230 + :ref:`musb-dev-platform-data`, but here we are looking at the 231 + ``platform_ops`` function pointer (line 5) in ``musb_hdrc_platform_data`` 222 232 structure (line 3). This function pointer allows the MUSB controller 223 - driver to know which function to call for device operation: 224 - 225 - :: 233 + driver to know which function to call for device operation:: 226 234 227 235 static const struct musb_platform_ops jz4740_musb_ops = { 228 - .init = jz4740_musb_init, 229 - .exit = jz4740_musb_exit, 236 + .init = jz4740_musb_init, 237 + .exit = jz4740_musb_exit, 230 238 }; 231 - 232 239 233 240 Here we have the minimal case where only init and exit functions are 234 241 called by the controller driver when needed. Fact is the JZ4740 MUSB ··· 241 246 At that point of the registration process, the controller driver 242 247 actually calls the init function: 243 248 244 - :: 249 + .. code-block:: c 250 + :emphasize-lines: 12,14 245 251 246 252 static int jz4740_musb_init(struct musb *musb) 247 253 { 248 - musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2); 249 - if (!musb->xceiv) { 250 - pr_err("HS UDC: no transceiver configured\n"); 251 - return -ENODEV; 252 - } 254 + musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2); 255 + if (!musb->xceiv) { 256 + pr_err("HS UDC: no transceiver configured\n"); 257 + return -ENODEV; 258 + } 253 259 254 - /* Silicon does not implement ConfigData register. 255 - * Set dyn_fifo to avoid reading EP config from hardware. 256 - */ 257 - musb->dyn_fifo = true; 260 + /* Silicon does not implement ConfigData register. 261 + * Set dyn_fifo to avoid reading EP config from hardware. 262 + */ 263 + musb->dyn_fifo = true; 258 264 259 - musb->isr = jz4740_musb_interrupt; 265 + musb->isr = jz4740_musb_interrupt; 260 266 261 - return 0; 267 + return 0; 262 268 } 263 269 264 - 265 - The goal of jz4740_musb_init() is to get hold of the transceiver 270 + The goal of ``jz4740_musb_init()`` is to get hold of the transceiver 266 271 driver data of the MUSB controller hardware and pass it on to the MUSB 267 272 controller driver, as usual. The transceiver is the circuitry inside the 268 273 controller hardware responsible for sending/receiving the USB data. 269 274 Since it is an implementation of the physical layer of the OSI model, 270 275 the transceiver is also referred to as PHY. 271 276 272 - Getting hold of the MUSB PHY driver data is done with usb_get_phy() 277 + Getting hold of the ``MUSB PHY`` driver data is done with ``usb_get_phy()`` 273 278 which returns a pointer to the structure containing the driver instance 274 279 data. The next couple of instructions (line 12 and 14) are used as a 275 280 quirk and to setup IRQ handling respectively. Quirks and IRQ handling 276 - will be discussed later in `Chapter 5 <#device-quirks>`__ and `Chapter 277 - 3 <#handling-irqs>`__. 278 - 279 - :: 281 + will be discussed later in :ref:`musb-dev-quirks` and 282 + :ref:`musb-handling-irqs`\ :: 280 283 281 284 static int jz4740_musb_exit(struct musb *musb) 282 285 { 283 - usb_put_phy(musb->xceiv); 286 + usb_put_phy(musb->xceiv); 284 287 285 - return 0; 288 + return 0; 286 289 } 287 - 288 290 289 291 Acting as the counterpart of init, the exit function releases the MUSB 290 292 PHY driver when the controller hardware itself is about to be released. ··· 292 300 to take care of more processing in those two functions. 293 301 294 302 Returning from the init function, the MUSB controller driver jumps back 295 - into the probe function: 296 - 297 - :: 303 + into the probe function:: 298 304 299 305 static int jz4740_probe(struct platform_device *pdev) 300 306 { 301 - ret = platform_device_add(musb); 302 - if (ret) { 303 - dev_err(&pdev->dev, "failed to register musb device\n"); 304 - goto err_clk_disable; 305 - } 307 + ret = platform_device_add(musb); 308 + if (ret) { 309 + dev_err(&pdev->dev, "failed to register musb device\n"); 310 + goto err_clk_disable; 311 + } 306 312 307 - return 0; 313 + return 0; 308 314 309 315 err_clk_disable: 310 - clk_disable_unprepare(clk); 316 + clk_disable_unprepare(clk); 311 317 err_platform_device_put: 312 - platform_device_put(musb); 313 - return ret; 318 + platform_device_put(musb); 319 + return ret; 314 320 } 315 - 316 321 317 322 This is the last part of the device registration process where the glue 318 323 layer adds the controller hardware device to Linux kernel device 319 324 hierarchy: at this stage, all known information about the device is 320 - passed on to the Linux USB core stack. 325 + passed on to the Linux USB core stack: 321 326 322 - :: 327 + .. code-block:: c 328 + :emphasize-lines: 5,6 323 329 324 330 static int jz4740_remove(struct platform_device *pdev) 325 331 { 326 - struct jz4740_glue *glue = platform_get_drvdata(pdev); 332 + struct jz4740_glue *glue = platform_get_drvdata(pdev); 327 333 328 - platform_device_unregister(glue->musb); 329 - clk_disable_unprepare(glue->clk); 334 + platform_device_unregister(glue->musb); 335 + clk_disable_unprepare(glue->clk); 330 336 331 - return 0; 337 + return 0; 332 338 } 333 - 334 339 335 340 Acting as the counterpart of probe, the remove function unregister the 336 341 MUSB controller hardware (line 5) and disable the clock (line 6), 337 342 allowing it to be gated. 343 + 344 + .. _musb-handling-irqs: 338 345 339 346 Handling IRQs 340 347 ============= ··· 341 350 Additionally to the MUSB controller hardware basic setup and 342 351 registration, the glue layer is also responsible for handling the IRQs: 343 352 344 - :: 353 + .. code-block:: c 354 + :emphasize-lines: 7,9-11,14,24 345 355 346 356 static irqreturn_t jz4740_musb_interrupt(int irq, void *__hci) 347 357 { 348 - unsigned long flags; 349 - irqreturn_t retval = IRQ_NONE; 350 - struct musb *musb = __hci; 358 + unsigned long flags; 359 + irqreturn_t retval = IRQ_NONE; 360 + struct musb *musb = __hci; 351 361 352 - spin_lock_irqsave(&musb->lock, flags); 362 + spin_lock_irqsave(&musb->lock, flags); 353 363 354 - musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); 355 - musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); 356 - musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); 364 + musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); 365 + musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); 366 + musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); 357 367 358 - /* 359 - * The controller is gadget only, the state of the host mode IRQ bits is 360 - * undefined. Mask them to make sure that the musb driver core will 361 - * never see them set 362 - */ 363 - musb->int_usb &= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME | 364 - MUSB_INTR_RESET | MUSB_INTR_SOF; 368 + /* 369 + * The controller is gadget only, the state of the host mode IRQ bits is 370 + * undefined. Mask them to make sure that the musb driver core will 371 + * never see them set 372 + */ 373 + musb->int_usb &= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME | 374 + MUSB_INTR_RESET | MUSB_INTR_SOF; 365 375 366 - if (musb->int_usb || musb->int_tx || musb->int_rx) 367 - retval = musb_interrupt(musb); 376 + if (musb->int_usb || musb->int_tx || musb->int_rx) 377 + retval = musb_interrupt(musb); 368 378 369 - spin_unlock_irqrestore(&musb->lock, flags); 379 + spin_unlock_irqrestore(&musb->lock, flags); 370 380 371 - return retval; 381 + return retval; 372 382 } 373 - 374 383 375 384 Here the glue layer mostly has to read the relevant hardware registers 376 385 and pass their values on to the controller driver which will handle the 377 386 actual event that triggered the IRQ. 378 387 379 388 The interrupt handler critical section is protected by the 380 - spin_lock_irqsave() and counterpart spin_unlock_irqrestore() 389 + :c:func:`spin_lock_irqsave` and counterpart :c:func:`spin_unlock_irqrestore` 381 390 functions (line 7 and 24 respectively), which prevent the interrupt 382 391 handler code to be run by two different threads at the same time. 383 392 384 393 Then the relevant interrupt registers are read (line 9 to 11): 385 394 386 - - MUSB_INTRUSB: indicates which USB interrupts are currently active, 395 + - ``MUSB_INTRUSB``: indicates which USB interrupts are currently active, 387 396 388 - - MUSB_INTRTX: indicates which of the interrupts for TX endpoints are 397 + - ``MUSB_INTRTX``: indicates which of the interrupts for TX endpoints are 389 398 currently active, 390 399 391 - - MUSB_INTRRX: indicates which of the interrupts for TX endpoints are 400 + - ``MUSB_INTRRX``: indicates which of the interrupts for TX endpoints are 392 401 currently active. 393 402 394 - Note that musb_readb() is used to read 8-bit registers at most, while 395 - musb_readw() allows us to read at most 16-bit registers. There are 403 + Note that :c:func:`musb_readb` is used to read 8-bit registers at most, while 404 + :c:func:`musb_readw` allows us to read at most 16-bit registers. There are 396 405 other functions that can be used depending on the size of your device 397 - registers. See musb_io.h for more information. 406 + registers. See ``musb_io.h`` for more information. 398 407 399 408 Instruction on line 18 is another quirk specific to the JZ4740 USB 400 - device controller, which will be discussed later in `Chapter 401 - 5 <#device-quirks>`__. 409 + device controller, which will be discussed later in :ref:`musb-dev-quirks`. 402 410 403 411 The glue layer still needs to register the IRQ handler though. Remember 404 - the instruction on line 14 of the init function: 405 - 406 - :: 412 + the instruction on line 14 of the init function:: 407 413 408 414 static int jz4740_musb_init(struct musb *musb) 409 415 { 410 - musb->isr = jz4740_musb_interrupt; 416 + musb->isr = jz4740_musb_interrupt; 411 417 412 - return 0; 418 + return 0; 413 419 } 414 - 415 420 416 421 This instruction sets a pointer to the glue layer IRQ handler function, 417 422 in order for the controller hardware to call the handler back when an 418 423 IRQ comes from the controller hardware. The interrupt handler is now 419 424 implemented and registered. 425 + 426 + .. _musb-dev-platform-data: 420 427 421 428 Device Platform Data 422 429 ==================== ··· 424 435 is called the platform data. 425 436 426 437 Platform data is specific to your hardware, though it may cover a broad 427 - range of devices, and is generally found somewhere in the arch/ 438 + range of devices, and is generally found somewhere in the ``arch/`` 428 439 directory, depending on your device architecture. 429 440 430 441 For instance, platform data for the JZ4740 SoC is found in 431 - arch/mips/jz4740/platform.c. In the platform.c file each device of the 442 + ``arch/mips/jz4740/platform.c``. In the ``platform.c`` file each device of the 432 443 JZ4740 SoC is described through a set of structures. 433 444 434 - Here is the part of arch/mips/jz4740/platform.c that covers the USB 445 + Here is the part of ``arch/mips/jz4740/platform.c`` that covers the USB 435 446 Device Controller (UDC): 436 447 437 - :: 448 + .. code-block:: c 449 + :emphasize-lines: 2,7,14-17,21,22,25,26,28,29 438 450 439 451 /* USB Device Controller */ 440 452 struct platform_device jz4740_udc_xceiv_device = { 441 - .name = "usb_phy_gen_xceiv", 442 - .id = 0, 453 + .name = "usb_phy_gen_xceiv", 454 + .id = 0, 443 455 }; 444 456 445 457 static struct resource jz4740_udc_resources[] = { 446 - [0] = { 447 - .start = JZ4740_UDC_BASE_ADDR, 448 - .end = JZ4740_UDC_BASE_ADDR + 0x10000 - 1, 449 - .flags = IORESOURCE_MEM, 450 - }, 451 - [1] = { 452 - .start = JZ4740_IRQ_UDC, 453 - .end = JZ4740_IRQ_UDC, 454 - .flags = IORESOURCE_IRQ, 455 - .name = "mc", 456 - }, 458 + [0] = { 459 + .start = JZ4740_UDC_BASE_ADDR, 460 + .end = JZ4740_UDC_BASE_ADDR + 0x10000 - 1, 461 + .flags = IORESOURCE_MEM, 462 + }, 463 + [1] = { 464 + .start = JZ4740_IRQ_UDC, 465 + .end = JZ4740_IRQ_UDC, 466 + .flags = IORESOURCE_IRQ, 467 + .name = "mc", 468 + }, 457 469 }; 458 470 459 471 struct platform_device jz4740_udc_device = { 460 - .name = "musb-jz4740", 461 - .id = -1, 462 - .dev = { 463 - .dma_mask = &jz4740_udc_device.dev.coherent_dma_mask, 464 - .coherent_dma_mask = DMA_BIT_MASK(32), 465 - }, 466 - .num_resources = ARRAY_SIZE(jz4740_udc_resources), 467 - .resource = jz4740_udc_resources, 472 + .name = "musb-jz4740", 473 + .id = -1, 474 + .dev = { 475 + .dma_mask = &jz4740_udc_device.dev.coherent_dma_mask, 476 + .coherent_dma_mask = DMA_BIT_MASK(32), 477 + }, 478 + .num_resources = ARRAY_SIZE(jz4740_udc_resources), 479 + .resource = jz4740_udc_resources, 468 480 }; 469 481 470 - 471 - The jz4740_udc_xceiv_device platform device structure (line 2) 482 + The ``jz4740_udc_xceiv_device`` platform device structure (line 2) 472 483 describes the UDC transceiver with a name and id number. 473 484 474 - At the time of this writing, note that "usb_phy_gen_xceiv" is the 485 + At the time of this writing, note that ``usb_phy_gen_xceiv`` is the 475 486 specific name to be used for all transceivers that are either built-in 476 487 with reference USB IP or autonomous and doesn't require any PHY 477 - programming. You will need to set CONFIG_NOP_USB_XCEIV=y in the 488 + programming. You will need to set ``CONFIG_NOP_USB_XCEIV=y`` in the 478 489 kernel configuration to make use of the corresponding transceiver 479 490 driver. The id field could be set to -1 (equivalent to 480 - PLATFORM_DEVID_NONE), -2 (equivalent to PLATFORM_DEVID_AUTO) or 491 + ``PLATFORM_DEVID_NONE``), -2 (equivalent to ``PLATFORM_DEVID_AUTO``) or 481 492 start with 0 for the first device of this kind if we want a specific id 482 493 number. 483 494 484 - The jz4740_udc_resources resource structure (line 7) defines the UDC 495 + The ``jz4740_udc_resources`` resource structure (line 7) defines the UDC 485 496 registers base addresses. 486 497 487 498 The first array (line 9 to 11) defines the UDC registers base memory 488 499 addresses: start points to the first register memory address, end points 489 500 to the last register memory address and the flags member defines the 490 - type of resource we are dealing with. So IORESOURCE_MEM is used to 501 + type of resource we are dealing with. So ``IORESOURCE_MEM`` is used to 491 502 define the registers memory addresses. The second array (line 14 to 17) 492 503 defines the UDC IRQ registers addresses. Since there is only one IRQ 493 504 register available for the JZ4740 UDC, start and end point at the same 494 - address. The IORESOURCE_IRQ flag tells that we are dealing with IRQ 495 - resources, and the name "mc" is in fact hard-coded in the MUSB core in 505 + address. The ``IORESOURCE_IRQ`` flag tells that we are dealing with IRQ 506 + resources, and the name ``mc`` is in fact hard-coded in the MUSB core in 496 507 order for the controller driver to retrieve this IRQ resource by 497 508 querying it by its name. 498 509 499 - Finally, the jz4740_udc_device platform device structure (line 21) 510 + Finally, the ``jz4740_udc_device`` platform device structure (line 21) 500 511 describes the UDC itself. 501 512 502 - The "musb-jz4740" name (line 22) defines the MUSB driver that is used 513 + The ``musb-jz4740`` name (line 22) defines the MUSB driver that is used 503 514 for this device; remember this is in fact the name that we used in the 504 - jz4740_driver platform driver structure in `Chapter 505 - 2 <#linux-musb-basics>`__. The id field (line 23) is set to -1 506 - (equivalent to PLATFORM_DEVID_NONE) since we do not need an id for the 507 - device: the MUSB controller driver was already set to allocate an 508 - automatic id in `Chapter 2 <#linux-musb-basics>`__. In the dev field we 509 - care for DMA related information here. The dma_mask field (line 25) 515 + ``jz4740_driver`` platform driver structure in :ref:`musb-basics`. 516 + The id field (line 23) is set to -1 (equivalent to ``PLATFORM_DEVID_NONE``) 517 + since we do not need an id for the device: the MUSB controller driver was 518 + already set to allocate an automatic id in :ref:`musb-basics`. In the dev field 519 + we care for DMA related information here. The ``dma_mask`` field (line 25) 510 520 defines the width of the DMA mask that is going to be used, and 511 - coherent_dma_mask (line 26) has the same purpose but for the 512 - alloc_coherent DMA mappings: in both cases we are using a 32 bits mask. 521 + ``coherent_dma_mask`` (line 26) has the same purpose but for the 522 + ``alloc_coherent`` DMA mappings: in both cases we are using a 32 bits mask. 513 523 Then the resource field (line 29) is simply a pointer to the resource 514 - structure defined before, while the num_resources field (line 28) keeps 524 + structure defined before, while the ``num_resources`` field (line 28) keeps 515 525 track of the number of arrays defined in the resource structure (in this 516 526 case there were two resource arrays defined before). 517 527 518 - With this quick overview of the UDC platform data at the arch/ level now 528 + With this quick overview of the UDC platform data at the ``arch/`` level now 519 529 done, let's get back to the MUSB glue layer specific platform data in 520 - drivers/usb/musb/jz4740.c: 530 + ``drivers/usb/musb/jz4740.c``: 521 531 522 - :: 532 + .. code-block:: c 533 + :emphasize-lines: 3,5,7-9,11 523 534 524 535 static struct musb_hdrc_config jz4740_musb_config = { 525 - /* Silicon does not implement USB OTG. */ 526 - .multipoint = 0, 527 - /* Max EPs scanned, driver will decide which EP can be used. */ 528 - .num_eps = 4, 529 - /* RAMbits needed to configure EPs from table */ 530 - .ram_bits = 9, 531 - .fifo_cfg = jz4740_musb_fifo_cfg, 532 - .fifo_cfg_size = ARRAY_SIZE(jz4740_musb_fifo_cfg), 536 + /* Silicon does not implement USB OTG. */ 537 + .multipoint = 0, 538 + /* Max EPs scanned, driver will decide which EP can be used. */ 539 + .num_eps = 4, 540 + /* RAMbits needed to configure EPs from table */ 541 + .ram_bits = 9, 542 + .fifo_cfg = jz4740_musb_fifo_cfg, 543 + .fifo_cfg_size = ARRAY_SIZE(jz4740_musb_fifo_cfg), 533 544 }; 534 545 535 546 static struct musb_hdrc_platform_data jz4740_musb_platform_data = { 536 - .mode = MUSB_PERIPHERAL, 537 - .config = &jz4740_musb_config, 547 + .mode = MUSB_PERIPHERAL, 548 + .config = &jz4740_musb_config, 538 549 }; 539 - 540 550 541 551 First the glue layer configures some aspects of the controller driver 542 552 operation related to the controller hardware specifics. This is done 543 - through the jz4740_musb_config musb_hdrc_config structure. 553 + through the ``jz4740_musb_config`` :c:type:`musb_hdrc_config` structure. 544 554 545 555 Defining the OTG capability of the controller hardware, the multipoint 546 556 member (line 3) is set to 0 (equivalent to false) since the JZ4740 UDC 547 - is not OTG compatible. Then num_eps (line 5) defines the number of USB 557 + is not OTG compatible. Then ``num_eps`` (line 5) defines the number of USB 548 558 endpoints of the controller hardware, including endpoint 0: here we have 549 - 3 endpoints + endpoint 0. Next is ram_bits (line 7) which is the width 559 + 3 endpoints + endpoint 0. Next is ``ram_bits`` (line 7) which is the width 550 560 of the RAM address bus for the MUSB controller hardware. This 551 561 information is needed when the controller driver cannot automatically 552 562 configure endpoints by reading the relevant controller hardware 553 563 registers. This issue will be discussed when we get to device quirks in 554 - `Chapter 5 <#device-quirks>`__. Last two fields (line 8 and 9) are also 555 - about device quirks: fifo_cfg points to the USB endpoints configuration 556 - table and fifo_cfg_size keeps track of the size of the number of 557 - entries in that configuration table. More on that later in `Chapter 558 - 5 <#device-quirks>`__. 564 + :ref:`musb-dev-quirks`. Last two fields (line 8 and 9) are also 565 + about device quirks: ``fifo_cfg`` points to the USB endpoints configuration 566 + table and ``fifo_cfg_size`` keeps track of the size of the number of 567 + entries in that configuration table. More on that later in 568 + :ref:`musb-dev-quirks`. 559 569 560 - Then this configuration is embedded inside jz4740_musb_platform_data 561 - musb_hdrc_platform_data structure (line 11): config is a pointer to 570 + Then this configuration is embedded inside ``jz4740_musb_platform_data`` 571 + :c:type:`musb_hdrc_platform_data` structure (line 11): config is a pointer to 562 572 the configuration structure itself, and mode tells the controller driver 563 - if the controller hardware may be used as MUSB_HOST only, 564 - MUSB_PERIPHERAL only or MUSB_OTG which is a dual mode. 573 + if the controller hardware may be used as ``MUSB_HOST`` only, 574 + ``MUSB_PERIPHERAL`` only or ``MUSB_OTG`` which is a dual mode. 565 575 566 - Remember that jz4740_musb_platform_data is then used to convey 576 + Remember that ``jz4740_musb_platform_data`` is then used to convey 567 577 platform data information as we have seen in the probe function in 568 - `Chapter 2 <#linux-musb-basics>`__ 578 + :ref:`musb-basics`. 579 + 580 + .. _musb-dev-quirks: 569 581 570 582 Device Quirks 571 583 ============= ··· 583 593 584 594 Let's get back to the init function first: 585 595 586 - :: 596 + .. code-block:: c 597 + :emphasize-lines: 12 587 598 588 599 static int jz4740_musb_init(struct musb *musb) 589 600 { 590 - musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2); 591 - if (!musb->xceiv) { 592 - pr_err("HS UDC: no transceiver configured\n"); 593 - return -ENODEV; 594 - } 601 + musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2); 602 + if (!musb->xceiv) { 603 + pr_err("HS UDC: no transceiver configured\n"); 604 + return -ENODEV; 605 + } 595 606 596 - /* Silicon does not implement ConfigData register. 597 - * Set dyn_fifo to avoid reading EP config from hardware. 598 - */ 599 - musb->dyn_fifo = true; 607 + /* Silicon does not implement ConfigData register. 608 + * Set dyn_fifo to avoid reading EP config from hardware. 609 + */ 610 + musb->dyn_fifo = true; 600 611 601 - musb->isr = jz4740_musb_interrupt; 612 + musb->isr = jz4740_musb_interrupt; 602 613 603 - return 0; 614 + return 0; 604 615 } 605 - 606 616 607 617 Instruction on line 12 helps the MUSB controller driver to work around 608 618 the fact that the controller hardware is missing registers that are used ··· 611 621 Without these registers, the controller driver is unable to read the 612 622 endpoints configuration from the hardware, so we use line 12 instruction 613 623 to bypass reading the configuration from silicon, and rely on a 614 - hard-coded table that describes the endpoints configuration instead: 615 - 616 - :: 624 + hard-coded table that describes the endpoints configuration instead:: 617 625 618 626 static struct musb_fifo_cfg jz4740_musb_fifo_cfg[] = { 619 - { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, }, 620 - { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, }, 621 - { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 64, }, 627 + { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, }, 628 + { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, }, 629 + { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 64, }, 622 630 }; 623 631 624 - 625 632 Looking at the configuration table above, we see that each endpoints is 626 - described by three fields: hw_ep_num is the endpoint number, style is 627 - its direction (either FIFO_TX for the controller driver to send packets 628 - in the controller hardware, or FIFO_RX to receive packets from 633 + described by three fields: ``hw_ep_num`` is the endpoint number, style is 634 + its direction (either ``FIFO_TX`` for the controller driver to send packets 635 + in the controller hardware, or ``FIFO_RX`` to receive packets from 629 636 hardware), and maxpacket defines the maximum size of each data packet 630 637 that can be transmitted over that endpoint. Reading from the table, the 631 638 controller driver knows that endpoint 1 can be used to send and receive ··· 633 646 Note that there is no information about endpoint 0 here: that one is 634 647 implemented by default in every silicon design, with a predefined 635 648 configuration according to the USB specification. For more examples of 636 - endpoint configuration tables, see musb_core.c. 649 + endpoint configuration tables, see ``musb_core.c``. 637 650 638 651 Let's now get back to the interrupt handler function: 639 652 640 - :: 653 + .. code-block:: c 654 + :emphasize-lines: 18-19 641 655 642 656 static irqreturn_t jz4740_musb_interrupt(int irq, void *__hci) 643 657 { 644 - unsigned long flags; 645 - irqreturn_t retval = IRQ_NONE; 646 - struct musb *musb = __hci; 658 + unsigned long flags; 659 + irqreturn_t retval = IRQ_NONE; 660 + struct musb *musb = __hci; 647 661 648 - spin_lock_irqsave(&musb->lock, flags); 662 + spin_lock_irqsave(&musb->lock, flags); 649 663 650 - musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); 651 - musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); 652 - musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); 664 + musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); 665 + musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); 666 + musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); 653 667 654 - /* 655 - * The controller is gadget only, the state of the host mode IRQ bits is 656 - * undefined. Mask them to make sure that the musb driver core will 657 - * never see them set 658 - */ 659 - musb->int_usb &= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME | 660 - MUSB_INTR_RESET | MUSB_INTR_SOF; 668 + /* 669 + * The controller is gadget only, the state of the host mode IRQ bits is 670 + * undefined. Mask them to make sure that the musb driver core will 671 + * never see them set 672 + */ 673 + musb->int_usb &= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME | 674 + MUSB_INTR_RESET | MUSB_INTR_SOF; 661 675 662 - if (musb->int_usb || musb->int_tx || musb->int_rx) 663 - retval = musb_interrupt(musb); 676 + if (musb->int_usb || musb->int_tx || musb->int_rx) 677 + retval = musb_interrupt(musb); 664 678 665 - spin_unlock_irqrestore(&musb->lock, flags); 679 + spin_unlock_irqrestore(&musb->lock, flags); 666 680 667 - return retval; 681 + return retval; 668 682 } 669 - 670 683 671 684 Instruction on line 18 above is a way for the controller driver to work 672 685 around the fact that some interrupt bits used for USB host mode 673 - operation are missing in the MUSB_INTRUSB register, thus left in an 686 + operation are missing in the ``MUSB_INTRUSB`` register, thus left in an 674 687 undefined hardware state, since this MUSB controller hardware is used in 675 688 peripheral mode only. As a consequence, the glue layer masks these 676 689 missing bits out to avoid parasite interrupts by doing a logical AND 677 - operation between the value read from MUSB_INTRUSB and the bits that 690 + operation between the value read from ``MUSB_INTRUSB`` and the bits that 678 691 are actually implemented in the register. 679 692 680 693 These are only a couple of the quirks found in the JZ4740 USB device ··· 714 727 USB On-the-Go Basics: 715 728 http://www.maximintegrated.com/app-notes/index.mvp/id/1822 716 729 717 - Writing USB Device Drivers: 718 - https://www.kernel.org/doc/htmldocs/writing_usb_driver/index.html 730 + :ref:`Writing USB Device Drivers <writing-usb-driver>` 719 731 720 732 Texas Instruments USB Configuration Wiki Page: 721 733 http://processors.wiki.ti.com/index.php/Usbgeneralpage