qemu/hw/mdio/mdio.c
<<
>>
Prefs
   1/*
   2 * QEMU Ethernet MDIO bus & PHY models
   3 *
   4 * Copyright (c) 2008 Edgar E. Iglesias,
   5 *                    Grant Likely (grant.likely@secretlab.ca).
   6 * Copyright (c) 2016 Xilinx Inc.
   7 *
   8 * Permission is hereby granted, free of charge, to any person obtaining a copy
   9 * of this software and associated documentation files (the "Software"), to deal
  10 * in the Software without restriction, including without limitation the rights
  11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12 * copies of the Software, and to permit persons to whom the Software is
  13 * furnished to do so, subject to the following conditions:
  14 *
  15 * The above copyright notice and this permission notice shall be included in
  16 * all copies or substantial portions of the Software.
  17 *
  18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24 * THE SOFTWARE.
  25 *
  26 * This is a generic MDIO implementation.
  27 *
  28 * TODO:
  29 * - Make the Model use mmio to communicate directly form IO register space.
  30 */
  31
  32#include "qemu/osdep.h"
  33#include "qemu-common.h"
  34#include "qemu/log.h"
  35#include "hw/mdio/mdio_slave.h"
  36#include "hw/mdio/mdio.h"
  37
  38#ifndef MDIO_DEBUG
  39#define MDIO_DEBUG 0
  40#endif
  41
  42#define DPRINT(fmt, args...) \
  43    do { \
  44        if (MDIO_DEBUG) { \
  45            qemu_log("%s: " fmt, __func__, ## args); \
  46        } \
  47    } while (0)
  48
  49static uint16_t mdio_read_req(struct MDIO *s, uint8_t addr, uint8_t reg)
  50{
  51    uint16_t val;
  52
  53    val = mdio_recv(s->bus, addr, reg);
  54    DPRINT("slave %d reg %d<- 0x%x\n", addr, reg, val);
  55    return val;
  56}
  57
  58static void mdio_write_req(struct MDIO *s, uint8_t addr, uint8_t reg,
  59                           uint16_t data)
  60{
  61     mdio_send(s->bus, addr, reg, data);
  62     DPRINT("slave %d reg %d<- 0x%x\n", addr, reg, data);
  63}
  64
  65static void mdio_init(Object *obj)
  66{
  67    MDIO *s = MDIO(obj);
  68
  69    s->read = mdio_read_req;
  70    s->write = mdio_write_req;
  71}
  72
  73static void mdio_realize(DeviceState *dev, Error **errp)
  74{
  75    MDIO *s = MDIO(dev);
  76
  77    s->bus = mdio_init_bus(dev, "mdio-bus");
  78
  79    if (!s->bus) {
  80        DPRINT("mdio bus un-initialized\n");
  81    }
  82}
  83
  84static void mdio_class_init(ObjectClass *klass, void *data)
  85{
  86    DeviceClass *k = DEVICE_CLASS(klass);
  87
  88    k->realize = mdio_realize;
  89}
  90
  91static const TypeInfo mdio_info = {
  92    .name = TYPE_MDIO,
  93    .parent = TYPE_SYS_BUS_DEVICE,
  94    .instance_size = sizeof(MDIO),
  95    .class_size = sizeof(MDIOClass),
  96    .class_init = mdio_class_init,
  97    .instance_init = mdio_init,
  98};
  99
 100static void mdio_register_types(void)
 101{
 102    type_register_static(&mdio_info);
 103}
 104
 105type_init(mdio_register_types)
 106