2015-03-23 07:09:10 +09:00
|
|
|
!!! WARNING !!!
|
|
|
|
|
|
|
|
This guide describes to the old way of doing things. No new Ethernet drivers
|
|
|
|
should be implemented this way. All new drivers should be written against the
|
|
|
|
U-Boot core driver model. See doc/driver-model/README.txt
|
|
|
|
|
2009-09-10 03:41:22 +09:00
|
|
|
-----------------------
|
|
|
|
Ethernet Driver Guide
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
The networking stack in Das U-Boot is designed for multiple network devices
|
|
|
|
to be easily added and controlled at runtime. This guide is meant for people
|
|
|
|
who wish to review the net driver stack with an eye towards implementing your
|
|
|
|
own ethernet device driver. Here we will describe a new pseudo 'APE' driver.
|
|
|
|
|
|
|
|
------------------
|
|
|
|
Driver Functions
|
|
|
|
------------------
|
|
|
|
|
|
|
|
All functions you will be implementing in this document have the return value
|
|
|
|
meaning of 0 for success and non-zero for failure.
|
|
|
|
|
|
|
|
----------
|
|
|
|
Register
|
|
|
|
----------
|
|
|
|
|
|
|
|
When U-Boot initializes, it will call the common function eth_initialize().
|
|
|
|
This will in turn call the board-specific board_eth_init() (or if that fails,
|
|
|
|
the cpu-specific cpu_eth_init()). These board-specific functions can do random
|
|
|
|
system handling, but ultimately they will call the driver-specific register
|
|
|
|
function which in turn takes care of initializing that particular instance.
|
|
|
|
|
|
|
|
Keep in mind that you should code the driver to avoid storing state in global
|
2009-10-05 16:02:51 +09:00
|
|
|
data as someone might want to hook up two of the same devices to one board.
|
|
|
|
Any such information that is specific to an interface should be stored in a
|
|
|
|
private, driver-defined data structure and pointed to by eth->priv (see below).
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
So the call graph at this stage would look something like:
|
|
|
|
board_init()
|
|
|
|
eth_initialize()
|
|
|
|
board_eth_init() / cpu_eth_init()
|
|
|
|
driver_register()
|
|
|
|
initialize eth_device
|
|
|
|
eth_register()
|
|
|
|
|
|
|
|
At this point in time, the only thing you need to worry about is the driver's
|
|
|
|
register function. The pseudo code would look something like:
|
|
|
|
int ape_register(bd_t *bis, int iobase)
|
|
|
|
{
|
|
|
|
struct ape_priv *priv;
|
|
|
|
struct eth_device *dev;
|
2015-03-20 18:12:19 +09:00
|
|
|
struct mii_dev *bus;
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
priv = malloc(sizeof(*priv));
|
|
|
|
if (priv == NULL)
|
2015-03-20 18:12:19 +09:00
|
|
|
return -ENOMEM;
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
dev = malloc(sizeof(*dev));
|
|
|
|
if (dev == NULL) {
|
|
|
|
free(priv);
|
2015-03-20 18:12:19 +09:00
|
|
|
return -ENOMEM;
|
2009-09-10 03:41:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setup whatever private state you need */
|
|
|
|
|
|
|
|
memset(dev, 0, sizeof(*dev));
|
|
|
|
sprintf(dev->name, "APE");
|
|
|
|
|
2015-03-20 18:12:19 +09:00
|
|
|
/*
|
|
|
|
* if your device has dedicated hardware storage for the
|
2009-09-10 03:41:22 +09:00
|
|
|
* MAC, read it and initialize dev->enetaddr with it
|
|
|
|
*/
|
|
|
|
ape_mac_read(dev->enetaddr);
|
|
|
|
|
|
|
|
dev->iobase = iobase;
|
|
|
|
dev->priv = priv;
|
|
|
|
dev->init = ape_init;
|
|
|
|
dev->halt = ape_halt;
|
|
|
|
dev->send = ape_send;
|
|
|
|
dev->recv = ape_recv;
|
2010-04-27 03:11:46 +09:00
|
|
|
dev->write_hwaddr = ape_write_hwaddr;
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
eth_register(dev);
|
|
|
|
|
2015-03-20 18:12:19 +09:00
|
|
|
#ifdef CONFIG_PHYLIB
|
|
|
|
bus = mdio_alloc();
|
|
|
|
if (!bus) {
|
|
|
|
free(priv);
|
|
|
|
free(dev);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
bus->read = ape_mii_read;
|
|
|
|
bus->write = ape_mii_write;
|
|
|
|
mdio_register(bus);
|
2009-09-10 03:41:22 +09:00
|
|
|
#endif
|
|
|
|
|
2009-10-05 16:02:51 +09:00
|
|
|
return 1;
|
2009-09-10 03:41:22 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
The exact arguments needed to initialize your device are up to you. If you
|
|
|
|
need to pass more/less arguments, that's fine. You should also add the
|
2009-10-05 16:02:51 +09:00
|
|
|
prototype for your new register function to include/netdev.h.
|
|
|
|
|
|
|
|
The return value for this function should be as follows:
|
|
|
|
< 0 - failure (hardware failure, not probe failure)
|
|
|
|
>=0 - number of interfaces detected
|
|
|
|
|
2009-10-28 08:49:47 +09:00
|
|
|
You might notice that many drivers seem to use xxx_initialize() rather than
|
2009-10-05 16:02:51 +09:00
|
|
|
xxx_register(). This is the old naming convention and should be avoided as it
|
|
|
|
causes confusion with the driver-specific init function.
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
Other than locating the MAC address in dedicated hardware storage, you should
|
|
|
|
not touch the hardware in anyway. That step is handled in the driver-specific
|
|
|
|
init function. Remember that we are only registering the device here, we are
|
|
|
|
not checking its state or doing random probing.
|
|
|
|
|
|
|
|
-----------
|
|
|
|
Callbacks
|
|
|
|
-----------
|
|
|
|
|
|
|
|
Now that we've registered with the ethernet layer, we can start getting some
|
2010-04-27 03:11:46 +09:00
|
|
|
real work done. You will need five functions:
|
2009-09-10 03:41:22 +09:00
|
|
|
int ape_init(struct eth_device *dev, bd_t *bis);
|
|
|
|
int ape_send(struct eth_device *dev, volatile void *packet, int length);
|
|
|
|
int ape_recv(struct eth_device *dev);
|
|
|
|
int ape_halt(struct eth_device *dev);
|
2010-04-27 03:11:46 +09:00
|
|
|
int ape_write_hwaddr(struct eth_device *dev);
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
The init function checks the hardware (probing/identifying) and gets it ready
|
|
|
|
for send/recv operations. You often do things here such as resetting the MAC
|
|
|
|
and/or PHY, and waiting for the link to autonegotiate. You should also take
|
|
|
|
the opportunity to program the device's MAC address with the dev->enetaddr
|
|
|
|
member. This allows the rest of U-Boot to dynamically change the MAC address
|
|
|
|
and have the new settings be respected.
|
|
|
|
|
|
|
|
The send function does what you think -- transmit the specified packet whose
|
|
|
|
size is specified by length (in bytes). You should not return until the
|
|
|
|
transmission is complete, and you should leave the state such that the send
|
|
|
|
function can be called multiple times in a row.
|
|
|
|
|
|
|
|
The recv function should process packets as long as the hardware has them
|
|
|
|
readily available before returning. i.e. you should drain the hardware fifo.
|
2009-11-02 12:39:56 +09:00
|
|
|
For each packet you receive, you should call the NetReceive() function on it
|
|
|
|
along with the packet length. The common code sets up packet buffers for you
|
|
|
|
already in the .bss (NetRxPackets), so there should be no need to allocate your
|
|
|
|
own. This doesn't mean you must use the NetRxPackets array however; you're
|
|
|
|
free to call the NetReceive() function with any buffer you wish. So the pseudo
|
|
|
|
code here would look something like:
|
2009-09-10 03:41:22 +09:00
|
|
|
int ape_recv(struct eth_device *dev)
|
|
|
|
{
|
|
|
|
int length, i = 0;
|
|
|
|
...
|
|
|
|
while (packets_are_available()) {
|
|
|
|
...
|
|
|
|
length = ape_get_packet(&NetRxPackets[i]);
|
|
|
|
...
|
|
|
|
NetReceive(&NetRxPackets[i], length);
|
|
|
|
...
|
|
|
|
if (++i >= PKTBUFSRX)
|
|
|
|
i = 0;
|
|
|
|
...
|
|
|
|
}
|
|
|
|
...
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
The halt function should turn off / disable the hardware and place it back in
|
2009-11-02 12:39:56 +09:00
|
|
|
its reset state. It can be called at any time (before any call to the related
|
|
|
|
init function), so make sure it can handle this sort of thing.
|
2009-09-10 03:41:22 +09:00
|
|
|
|
2010-04-27 03:11:46 +09:00
|
|
|
The write_hwaddr function should program the MAC address stored in dev->enetaddr
|
|
|
|
into the Ethernet controller.
|
|
|
|
|
2009-09-10 03:41:22 +09:00
|
|
|
So the call graph at this stage would look something like:
|
|
|
|
some net operation (ping / tftp / whatever...)
|
|
|
|
eth_init()
|
|
|
|
dev->init()
|
|
|
|
eth_send()
|
|
|
|
dev->send()
|
|
|
|
eth_rx()
|
|
|
|
dev->recv()
|
|
|
|
eth_halt()
|
|
|
|
dev->halt()
|
|
|
|
|
2015-03-20 18:12:19 +09:00
|
|
|
--------------------------------
|
|
|
|
CONFIG_PHYLIB / CONFIG_CMD_MII
|
|
|
|
--------------------------------
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
If your device supports banging arbitrary values on the MII bus (pretty much
|
|
|
|
every device does), you should add support for the mii command. Doing so is
|
|
|
|
fairly trivial and makes debugging mii issues a lot easier at runtime.
|
|
|
|
|
|
|
|
After you have called eth_register() in your driver's register function, add
|
2015-03-20 18:12:19 +09:00
|
|
|
a call to mdio_alloc() and mdio_register() like so:
|
|
|
|
bus = mdio_alloc();
|
|
|
|
if (!bus) {
|
|
|
|
free(priv);
|
|
|
|
free(dev);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
bus->read = ape_mii_read;
|
|
|
|
bus->write = ape_mii_write;
|
|
|
|
mdio_register(bus);
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
And then define the mii_read and mii_write functions if you haven't already.
|
|
|
|
Their syntax is straightforward:
|
2015-03-20 18:12:19 +09:00
|
|
|
int mii_read(struct mii_dev *bus, int addr, int devad, int reg);
|
|
|
|
int mii_write(struct mii_dev *bus, int addr, int devad, int reg,
|
|
|
|
u16 val);
|
2009-09-10 03:41:22 +09:00
|
|
|
|
|
|
|
The read function should read the register 'reg' from the phy at address 'addr'
|
2015-03-20 18:12:19 +09:00
|
|
|
and return the result to its caller. The implementation for the write function
|
|
|
|
should logically follow.
|