cregit-Linux how code gets into the kernel

Release 4.14 drivers/vlynq/vlynq.c

Directory: drivers/vlynq
/*
 * Copyright (C) 2006, 2007 Eugene Konev <ejka@openwrt.org>
 *
 * This program 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.
 *
 * This program 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Parts of the VLYNQ specification can be found here:
 * http://www.ti.com/litv/pdf/sprue36a
 */

#include <linux/init.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/slab.h>
#include <linux/irq.h>

#include <linux/vlynq.h>


#define VLYNQ_CTRL_PM_ENABLE		0x80000000

#define VLYNQ_CTRL_CLOCK_INT		0x00008000

#define VLYNQ_CTRL_CLOCK_DIV(x)		(((x) & 7) << 16)

#define VLYNQ_CTRL_INT_LOCAL		0x00004000

#define VLYNQ_CTRL_INT_ENABLE		0x00002000

#define VLYNQ_CTRL_INT_VECTOR(x)	(((x) & 0x1f) << 8)

#define VLYNQ_CTRL_INT2CFG		0x00000080

#define VLYNQ_CTRL_RESET		0x00000001


#define VLYNQ_CTRL_CLOCK_MASK          (0x7 << 16)


#define VLYNQ_INT_OFFSET		0x00000014

#define VLYNQ_REMOTE_OFFSET		0x00000080


#define VLYNQ_STATUS_LINK		0x00000001

#define VLYNQ_STATUS_LERROR		0x00000080

#define VLYNQ_STATUS_RERROR		0x00000100


#define VINT_ENABLE			0x00000100

#define VINT_TYPE_EDGE			0x00000080

#define VINT_LEVEL_LOW			0x00000040

#define VINT_VECTOR(x)			((x) & 0x1f)

#define VINT_OFFSET(irq)		(8 * ((irq) % 4))


#define VLYNQ_AUTONEGO_V2		0x00010000


struct vlynq_regs {
	
u32 revision;
	
u32 control;
	
u32 status;
	
u32 int_prio;
	
u32 int_status;
	
u32 int_pending;
	
u32 int_ptr;
	
u32 tx_offset;
	
struct vlynq_mapping rx_mapping[4];
	
u32 chip;
	
u32 autonego;
	
u32 unused[6];
	
u32 int_device[8];
};

#ifdef CONFIG_VLYNQ_DEBUG

static void vlynq_dump_regs(struct vlynq_device *dev) { int i; printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n", dev->local, dev->remote); for (i = 0; i < 32; i++) { printk(KERN_DEBUG "VLYNQ: local %d: %08x\n", i + 1, ((u32 *)dev->local)[i]); printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n", i + 1, ((u32 *)dev->remote)[i]); } }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli89100.00%1100.00%
Total89100.00%1100.00%


static void vlynq_dump_mem(u32 *base, int count) { int i; for (i = 0; i < (count + 3) / 4; i++) { if (i % 4 == 0) printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4); printk(KERN_DEBUG " 0x%08x", *(base + i)); } printk(KERN_DEBUG "\n"); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli74100.00%1100.00%
Total74100.00%1100.00%

#endif /* Check the VLYNQ link status with a given device */
static int vlynq_linked(struct vlynq_device *dev) { int i; for (i = 0; i < 100; i++) if (readl(&dev->local->status) & VLYNQ_STATUS_LINK) return 1; else cpu_relax(); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli51100.00%1100.00%
Total51100.00%1100.00%


static void vlynq_reset(struct vlynq_device *dev) { writel(readl(&dev->local->control) | VLYNQ_CTRL_RESET, &dev->local->control); /* Wait for the devices to finish resetting */ msleep(5); /* Remove reset bit */ writel(readl(&dev->local->control) & ~VLYNQ_CTRL_RESET, &dev->local->control); /* Give some time for the devices to settle */ msleep(5); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli69100.00%1100.00%
Total69100.00%1100.00%


static void vlynq_irq_unmask(struct irq_data *d) { struct vlynq_device *dev = irq_data_get_irq_chip_data(d); int virq; u32 val; BUG_ON(!dev); virq = d->irq - dev->irq_start; val = readl(&dev->remote->int_device[virq >> 2]); val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq); writel(val, &dev->remote->int_device[virq >> 2]); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli7987.78%150.00%
Thomas Gleixner1112.22%150.00%
Total90100.00%2100.00%


static void vlynq_irq_mask(struct irq_data *d) { struct vlynq_device *dev = irq_data_get_irq_chip_data(d); int virq; u32 val; BUG_ON(!dev); virq = d->irq - dev->irq_start; val = readl(&dev->remote->int_device[virq >> 2]); val &= ~(VINT_ENABLE << VINT_OFFSET(virq)); writel(val, &dev->remote->int_device[virq >> 2]); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli7887.64%150.00%
Thomas Gleixner1112.36%150.00%
Total89100.00%2100.00%


static int vlynq_irq_type(struct irq_data *d, unsigned int flow_type) { struct vlynq_device *dev = irq_data_get_irq_chip_data(d); int virq; u32 val; BUG_ON(!dev); virq = d->irq - dev->irq_start; val = readl(&dev->remote->int_device[virq >> 2]); switch (flow_type & IRQ_TYPE_SENSE_MASK) { case IRQ_TYPE_EDGE_RISING: case IRQ_TYPE_EDGE_FALLING: case IRQ_TYPE_EDGE_BOTH: val |= VINT_TYPE_EDGE << VINT_OFFSET(virq); val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq)); break; case IRQ_TYPE_LEVEL_HIGH: val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq)); val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq)); break; case IRQ_TYPE_LEVEL_LOW: val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq)); val |= VINT_LEVEL_LOW << VINT_OFFSET(virq); break; default: return -EINVAL; } writel(val, &dev->remote->int_device[virq >> 2]); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli17093.92%150.00%
Thomas Gleixner116.08%150.00%
Total181100.00%2100.00%


static void vlynq_local_ack(struct irq_data *d) { struct vlynq_device *dev = irq_data_get_irq_chip_data(d); u32 status = readl(&dev->local->status); pr_debug("%s: local status: 0x%08x\n", dev_name(&dev->dev), status); writel(status, &dev->local->status); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli5590.16%150.00%
Thomas Gleixner69.84%150.00%
Total61100.00%2100.00%


static void vlynq_remote_ack(struct irq_data *d) { struct vlynq_device *dev = irq_data_get_irq_chip_data(d); u32 status = readl(&dev->remote->status); pr_debug("%s: remote status: 0x%08x\n", dev_name(&dev->dev), status); writel(status, &dev->remote->status); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli5590.16%150.00%
Thomas Gleixner69.84%150.00%
Total61100.00%2100.00%


static irqreturn_t vlynq_irq(int irq, void *dev_id) { struct vlynq_device *dev = dev_id; u32 status; int virq = 0; status = readl(&dev->local->int_status); writel(status, &dev->local->int_status); if (unlikely(!status)) spurious_interrupt(); while (status) { if (status & 1) do_IRQ(dev->irq_start + virq); status >>= 1; virq++; } return IRQ_HANDLED; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli94100.00%1100.00%
Total94100.00%1100.00%

static struct irq_chip vlynq_irq_chip = { .name = "vlynq", .irq_unmask = vlynq_irq_unmask, .irq_mask = vlynq_irq_mask, .irq_set_type = vlynq_irq_type, }; static struct irq_chip vlynq_local_chip = { .name = "vlynq local error", .irq_unmask = vlynq_irq_unmask, .irq_mask = vlynq_irq_mask, .irq_ack = vlynq_local_ack, }; static struct irq_chip vlynq_remote_chip = { .name = "vlynq local error", .irq_unmask = vlynq_irq_unmask, .irq_mask = vlynq_irq_mask, .irq_ack = vlynq_remote_ack, };
static int vlynq_setup_irq(struct vlynq_device *dev) { u32 val; int i, virq; if (dev->local_irq == dev->remote_irq) { printk(KERN_ERR "%s: local vlynq irq should be different from remote\n", dev_name(&dev->dev)); return -EINVAL; } /* Clear local and remote error bits */ writel(readl(&dev->local->status), &dev->local->status); writel(readl(&dev->remote->status), &dev->remote->status); /* Now setup interrupts */ val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq); val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL | VLYNQ_CTRL_INT2CFG; val |= readl(&dev->local->control); writel(VLYNQ_INT_OFFSET, &dev->local->int_ptr); writel(val, &dev->local->control); val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq); val |= VLYNQ_CTRL_INT_ENABLE; val |= readl(&dev->remote->control); writel(VLYNQ_INT_OFFSET, &dev->remote->int_ptr); writel(val, &dev->remote->int_ptr); writel(val, &dev->remote->control); for (i = dev->irq_start; i <= dev->irq_end; i++) { virq = i - dev->irq_start; if (virq == dev->local_irq) { irq_set_chip_and_handler(i, &vlynq_local_chip, handle_level_irq); irq_set_chip_data(i, dev); } else if (virq == dev->remote_irq) { irq_set_chip_and_handler(i, &vlynq_remote_chip, handle_level_irq); irq_set_chip_data(i, dev); } else { irq_set_chip_and_handler(i, &vlynq_irq_chip, handle_simple_irq); irq_set_chip_data(i, dev); writel(0, &dev->remote->int_device[virq >> 2]); } } if (request_irq(dev->irq, vlynq_irq, IRQF_SHARED, "vlynq", dev)) { printk(KERN_ERR "%s: request_irq failed\n", dev_name(&dev->dev)); return -EAGAIN; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli35898.35%150.00%
Thomas Gleixner61.65%150.00%
Total364100.00%2100.00%


static void vlynq_device_release(struct device *dev) { struct vlynq_device *vdev = to_vlynq_device(dev); kfree(vdev); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli26100.00%1100.00%
Total26100.00%1100.00%


static int vlynq_device_match(struct device *dev, struct device_driver *drv) { struct vlynq_device *vdev = to_vlynq_device(dev); struct vlynq_driver *vdrv = to_vlynq_driver(drv); struct vlynq_device_id *ids = vdrv->id_table; while (ids->id) { if (ids->id == vdev->dev_id) { vdev->divisor = ids->divisor; vlynq_set_drvdata(vdev, ids); printk(KERN_INFO "Driver found for VLYNQ " "device: %08x\n", vdev->dev_id); return 1; } printk(KERN_DEBUG "Not using the %08x VLYNQ device's driver" " for VLYNQ device: %08x\n", ids->id, vdev->dev_id); ids++; } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli115100.00%1100.00%
Total115100.00%1100.00%


static int vlynq_device_probe(struct device *dev) { struct vlynq_device *vdev = to_vlynq_device(dev); struct vlynq_driver *drv = to_vlynq_driver(dev->driver); struct vlynq_device_id *id = vlynq_get_drvdata(vdev); int result = -ENODEV; if (drv->probe) result = drv->probe(vdev, id); if (result) put_device(dev); return result; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli78100.00%1100.00%
Total78100.00%1100.00%


static int vlynq_device_remove(struct device *dev) { struct vlynq_driver *drv = to_vlynq_driver(dev->driver); if (drv->remove) drv->remove(to_vlynq_device(dev)); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli42100.00%1100.00%
Total42100.00%1100.00%


int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner) { driver->driver.name = driver->name; driver->driver.bus = &vlynq_bus_type; return driver_register(&driver->driver); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli43100.00%1100.00%
Total43100.00%1100.00%

EXPORT_SYMBOL(__vlynq_register_driver);
void vlynq_unregister_driver(struct vlynq_driver *driver) { driver_unregister(&driver->driver); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli18100.00%1100.00%
Total18100.00%1100.00%

EXPORT_SYMBOL(vlynq_unregister_driver); /* * A VLYNQ remote device can clock the VLYNQ bus master * using a dedicated clock line. In that case, both the * remove device and the bus master should have the same * serial clock dividers configured. Iterate through the * 8 possible dividers until we actually link with the * device. */
static int __vlynq_try_remote(struct vlynq_device *dev) { int i; vlynq_reset(dev); for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ? i <= vlynq_rdiv8 : i >= vlynq_rdiv2; dev->dev_id ? i++ : i--) { if (!vlynq_linked(dev)) break; writel((readl(&dev->remote->control) & ~VLYNQ_CTRL_CLOCK_MASK) | VLYNQ_CTRL_CLOCK_INT | VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1), &dev->remote->control); writel((readl(&dev->local->control) & ~(VLYNQ_CTRL_CLOCK_INT | VLYNQ_CTRL_CLOCK_MASK)) | VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1), &dev->local->control); if (vlynq_linked(dev)) { printk(KERN_DEBUG "%s: using remote clock divisor %d\n", dev_name(&dev->dev), i - vlynq_rdiv1 + 1); dev->divisor = i; return 0; } else { vlynq_reset(dev); } } return -ENODEV; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli184100.00%1100.00%
Total184100.00%1100.00%

/* * A VLYNQ remote device can be clocked by the VLYNQ bus * master using a dedicated clock line. In that case, only * the bus master configures the serial clock divider. * Iterate through the 8 possible dividers until we * actually get a link with the device. */
static int __vlynq_try_local(struct vlynq_device *dev) { int i; vlynq_reset(dev); for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ? i <= vlynq_ldiv8 : i >= vlynq_ldiv2; dev->dev_id ? i++ : i--) { writel((readl(&dev->local->control) & ~VLYNQ_CTRL_CLOCK_MASK) | VLYNQ_CTRL_CLOCK_INT | VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1), &dev->local->control); if (vlynq_linked(dev)) { printk(KERN_DEBUG "%s: using local clock divisor %d\n", dev_name(&dev->dev), i - vlynq_ldiv1 + 1); dev->divisor = i; return 0; } else { vlynq_reset(dev); } } return -ENODEV; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli139100.00%1100.00%
Total139100.00%1100.00%

/* * When using external clocking method, serial clock * is supplied by an external oscillator, therefore we * should mask the local clock bit in the clock control * register for both the bus master and the remote device. */
static int __vlynq_try_external(struct vlynq_device *dev) { vlynq_reset(dev); if (!vlynq_linked(dev)) return -ENODEV; writel((readl(&dev->remote->control) & ~VLYNQ_CTRL_CLOCK_INT), &dev->remote->control); writel((readl(&dev->local->control) & ~VLYNQ_CTRL_CLOCK_INT), &dev->local->control); if (vlynq_linked(dev)) { printk(KERN_DEBUG "%s: using external clock\n", dev_name(&dev->dev)); dev->divisor = vlynq_div_external; return 0; } return -ENODEV; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli114100.00%1100.00%
Total114100.00%1100.00%


static int __vlynq_enable_device(struct vlynq_device *dev) { int result; struct plat_vlynq_ops *ops = dev->dev.platform_data; result = ops->on(dev); if (result) return result; switch (dev->divisor) { case vlynq_div_external: case vlynq_div_auto: /* When the device is brought from reset it should have clock * generation negotiated by hardware. * Check which device is generating clocks and perform setup * accordingly */ if (vlynq_linked(dev) && readl(&dev->remote->control) & VLYNQ_CTRL_CLOCK_INT) { if (!__vlynq_try_remote(dev) || !__vlynq_try_local(dev) || !__vlynq_try_external(dev)) return 0; } else { if (!__vlynq_try_external(dev) || !__vlynq_try_local(dev) || !__vlynq_try_remote(dev)) return 0; } break; case vlynq_ldiv1: case vlynq_ldiv2: case vlynq_ldiv3: case vlynq_ldiv4: case vlynq_ldiv5: case vlynq_ldiv6: case vlynq_ldiv7: case vlynq_ldiv8: writel(VLYNQ_CTRL_CLOCK_INT | VLYNQ_CTRL_CLOCK_DIV(dev->divisor - vlynq_ldiv1), &dev->local->control); writel(0, &dev->remote->control); if (vlynq_linked(dev)) { printk(KERN_DEBUG "%s: using local clock divisor %d\n", dev_name(&dev->dev), dev->divisor - vlynq_ldiv1 + 1); return 0; } break; case vlynq_rdiv1: case vlynq_rdiv2: case vlynq_rdiv3: case vlynq_rdiv4: case vlynq_rdiv5: case vlynq_rdiv6: case vlynq_rdiv7: case vlynq_rdiv8: writel(0, &dev->local->control); writel(VLYNQ_CTRL_CLOCK_INT | VLYNQ_CTRL_CLOCK_DIV(dev->divisor - vlynq_rdiv1), &dev->remote->control); if (vlynq_linked(dev)) { printk(KERN_DEBUG "%s: using remote clock divisor %d\n", dev_name(&dev->dev), dev->divisor - vlynq_rdiv1 + 1); return 0; } break; } ops->off(dev); return -ENODEV; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli322100.00%1100.00%
Total322100.00%1100.00%


int vlynq_enable_device(struct vlynq_device *dev) { struct plat_vlynq_ops *ops = dev->dev.platform_data; int result = -ENODEV; result = __vlynq_enable_device(dev); if (result) return result; result = vlynq_setup_irq(dev); if (result) ops->off(dev); dev->enabled = !result; return result; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli69100.00%1100.00%
Total69100.00%1100.00%

EXPORT_SYMBOL(vlynq_enable_device);
void vlynq_disable_device(struct vlynq_device *dev) { struct plat_vlynq_ops *ops = dev->dev.platform_data; dev->enabled = 0; free_irq(dev->irq, dev); ops->off(dev); }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli43100.00%1100.00%
Total43100.00%1100.00%

EXPORT_SYMBOL(vlynq_disable_device);
int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset, struct vlynq_mapping *mapping) { int i; if (!dev->enabled) return -ENXIO; writel(tx_offset, &dev->local->tx_offset); for (i = 0; i < 4; i++) { writel(mapping[i].offset, &dev->local->rx_mapping[i].offset); writel(mapping[i].size, &dev->local->rx_mapping[i].size); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli106100.00%1100.00%
Total106100.00%1100.00%

EXPORT_SYMBOL(vlynq_set_local_mapping);
int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset, struct vlynq_mapping *mapping) { int i; if (!dev->enabled) return -ENXIO; writel(tx_offset, &dev->remote->tx_offset); for (i = 0; i < 4; i++) { writel(mapping[i].offset, &dev->remote->rx_mapping[i].offset); writel(mapping[i].size, &dev->remote->rx_mapping[i].size); } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli106100.00%1100.00%
Total106100.00%1100.00%

EXPORT_SYMBOL(vlynq_set_remote_mapping);
int vlynq_set_local_irq(struct vlynq_device *dev, int virq) { int irq = dev->irq_start + virq; if (dev->enabled) return -EBUSY; if ((irq < dev->irq_start) || (irq > dev->irq_end)) return -EINVAL; if (virq == dev->remote_irq) return -EINVAL; dev->local_irq = virq; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Florian Fainelli75100.00%1100.00%
Total75100.00%1100.00%

EXPORT_SYMBOL(vlynq_set_local_irq);