README.drivers.eth 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. -----------------------
  2. Ethernet Driver Guide
  3. -----------------------
  4. The networking stack in Das U-Boot is designed for multiple network devices
  5. to be easily added and controlled at runtime. This guide is meant for people
  6. who wish to review the net driver stack with an eye towards implementing your
  7. own ethernet device driver. Here we will describe a new pseudo 'APE' driver.
  8. ------------------
  9. Driver Functions
  10. ------------------
  11. All functions you will be implementing in this document have the return value
  12. meaning of 0 for success and non-zero for failure.
  13. ----------
  14. Register
  15. ----------
  16. When U-Boot initializes, it will call the common function eth_initialize().
  17. This will in turn call the board-specific board_eth_init() (or if that fails,
  18. the cpu-specific cpu_eth_init()). These board-specific functions can do random
  19. system handling, but ultimately they will call the driver-specific register
  20. function which in turn takes care of initializing that particular instance.
  21. Keep in mind that you should code the driver to avoid storing state in global
  22. data as someone might want to hook up two of the same devices to one board.
  23. Any such information that is specific to an interface should be stored in a
  24. private, driver-defined data structure and pointed to by eth->priv (see below).
  25. So the call graph at this stage would look something like:
  26. board_init()
  27. eth_initialize()
  28. board_eth_init() / cpu_eth_init()
  29. driver_register()
  30. initialize eth_device
  31. eth_register()
  32. At this point in time, the only thing you need to worry about is the driver's
  33. register function. The pseudo code would look something like:
  34. int ape_register(bd_t *bis, int iobase)
  35. {
  36. struct ape_priv *priv;
  37. struct eth_device *dev;
  38. priv = malloc(sizeof(*priv));
  39. if (priv == NULL)
  40. return 1;
  41. dev = malloc(sizeof(*dev));
  42. if (dev == NULL) {
  43. free(priv);
  44. return 1;
  45. }
  46. /* setup whatever private state you need */
  47. memset(dev, 0, sizeof(*dev));
  48. sprintf(dev->name, "APE");
  49. /* if your device has dedicated hardware storage for the
  50. * MAC, read it and initialize dev->enetaddr with it
  51. */
  52. ape_mac_read(dev->enetaddr);
  53. dev->iobase = iobase;
  54. dev->priv = priv;
  55. dev->init = ape_init;
  56. dev->halt = ape_halt;
  57. dev->send = ape_send;
  58. dev->recv = ape_recv;
  59. eth_register(dev);
  60. #ifdef CONFIG_CMD_MII)
  61. miiphy_register(dev->name, ape_mii_read, ape_mii_write);
  62. #endif
  63. return 1;
  64. }
  65. The exact arguments needed to initialize your device are up to you. If you
  66. need to pass more/less arguments, that's fine. You should also add the
  67. prototype for your new register function to include/netdev.h.
  68. The return value for this function should be as follows:
  69. < 0 - failure (hardware failure, not probe failure)
  70. >=0 - number of interfaces detected
  71. You might notice that many drivers seem to use xxx_initialize() rather than
  72. xxx_register(). This is the old naming convention and should be avoided as it
  73. causes confusion with the driver-specific init function.
  74. Other than locating the MAC address in dedicated hardware storage, you should
  75. not touch the hardware in anyway. That step is handled in the driver-specific
  76. init function. Remember that we are only registering the device here, we are
  77. not checking its state or doing random probing.
  78. -----------
  79. Callbacks
  80. -----------
  81. Now that we've registered with the ethernet layer, we can start getting some
  82. real work done. You will need four functions:
  83. int ape_init(struct eth_device *dev, bd_t *bis);
  84. int ape_send(struct eth_device *dev, volatile void *packet, int length);
  85. int ape_recv(struct eth_device *dev);
  86. int ape_halt(struct eth_device *dev);
  87. The init function checks the hardware (probing/identifying) and gets it ready
  88. for send/recv operations. You often do things here such as resetting the MAC
  89. and/or PHY, and waiting for the link to autonegotiate. You should also take
  90. the opportunity to program the device's MAC address with the dev->enetaddr
  91. member. This allows the rest of U-Boot to dynamically change the MAC address
  92. and have the new settings be respected.
  93. The send function does what you think -- transmit the specified packet whose
  94. size is specified by length (in bytes). You should not return until the
  95. transmission is complete, and you should leave the state such that the send
  96. function can be called multiple times in a row.
  97. The recv function should process packets as long as the hardware has them
  98. readily available before returning. i.e. you should drain the hardware fifo.
  99. The common code sets up packet buffers for you already (NetRxPackets), so there
  100. is no need to allocate your own. For each packet you receive, you should call
  101. the NetReceive() function on it with the packet length. So the pseudo code
  102. here would look something like:
  103. int ape_recv(struct eth_device *dev)
  104. {
  105. int length, i = 0;
  106. ...
  107. while (packets_are_available()) {
  108. ...
  109. length = ape_get_packet(&NetRxPackets[i]);
  110. ...
  111. NetReceive(&NetRxPackets[i], length);
  112. ...
  113. if (++i >= PKTBUFSRX)
  114. i = 0;
  115. ...
  116. }
  117. ...
  118. return 0;
  119. }
  120. The halt function should turn off / disable the hardware and place it back in
  121. its reset state.
  122. So the call graph at this stage would look something like:
  123. some net operation (ping / tftp / whatever...)
  124. eth_init()
  125. dev->init()
  126. eth_send()
  127. dev->send()
  128. eth_rx()
  129. dev->recv()
  130. eth_halt()
  131. dev->halt()
  132. -----------------------------
  133. CONFIG_MII / CONFIG_CMD_MII
  134. -----------------------------
  135. If your device supports banging arbitrary values on the MII bus (pretty much
  136. every device does), you should add support for the mii command. Doing so is
  137. fairly trivial and makes debugging mii issues a lot easier at runtime.
  138. After you have called eth_register() in your driver's register function, add
  139. a call to miiphy_register() like so:
  140. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  141. miiphy_register(dev->name, mii_read, mii_write);
  142. #endif
  143. And then define the mii_read and mii_write functions if you haven't already.
  144. Their syntax is straightforward:
  145. int mii_read(char *devname, uchar addr, uchar reg, ushort *val);
  146. int mii_write(char *devname, uchar addr, uchar reg, ushort val);
  147. The read function should read the register 'reg' from the phy at address 'addr'
  148. and store the result in the pointer 'val'. The implementation for the write
  149. function should logically follow.