Commit 58b32625 authored by Alessandro Rubini's avatar Alessandro Rubini

Initial structure of the code, pll is identified

parent d61e525c
/*
* core fine-delay driver (i.e., init and exit of the subsystems)
*
* Copyright (C) 2012 CERN (www.cern.ch)
* Author: Alessandro Rubini <rubini@gnudd.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2 as published by the Free Software Foundation or, at your
* option, any later version.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/io.h>
#include "spec.h"
#include "fine-delay.h"
#include "hw/fd_main_regs.h"
/* The reset function (by Tomasz) */
static void fd_do_reset(struct spec_fd *fd, int hw_reset)
{
if (hw_reset) {
writel(FD_RSTR_LOCK_W(0xdead) | FD_RSTR_RST_CORE_MASK,
fd->regs + FD_REG_RSTR);
udelay(10000);
writel(FD_RSTR_LOCK_W(0xdead) | FD_RSTR_RST_CORE_MASK
| FD_RSTR_RST_FMC_MASK,
fd->regs + FD_REG_RSTR);
/* TPS3307 supervisor needs time to de-assert master reset */
msleep(600);
return;
}
writel(FD_RSTR_LOCK_W(0xdead) | FD_RSTR_RST_FMC_MASK,
fd->regs + FD_REG_RSTR);
udelay(1000);
writel(FD_RSTR_LOCK_W(0xdead) | FD_RSTR_RST_FMC_MASK
| FD_RSTR_RST_CORE_MASK,
fd->regs + FD_REG_RSTR);
udelay(1000);
}
/* This structure lists the various subsystems */
struct modlist {
char *name;
int (*init)(struct spec_fd *);
void (*exit)(struct spec_fd *);
};
#define M(x) { #x, fd_ ## x ## _init, fd_ ## x ## _exit }
static struct modlist mods[] = {
M(spi),
M(pll),
//M(w1),
//M(i2c),
//M(acam),
//M(zio),
};
#undef M
/* probe and remove are called by fd-spec.c */
int fd_probe(struct spec_dev *dev)
{
struct modlist *m;
struct spec_fd *fd;
int i, ret;
pr_debug("%s\n",__func__);
fd = kzalloc(sizeof(*fd), GFP_KERNEL);
if (!fd) {
pr_err("%s: can't allocate device\n", __func__);
return -ENOMEM;
}
dev->sub_priv = fd;
fd->spec = dev;
fd->base = dev->remap[0];
fd->regs = fd->base + FD_REGS_OFFSET;
/* First, hardware reset */
fd_do_reset(fd, 1);
/* init all subsystems */
for (i = 0, m = mods; i < ARRAY_SIZE(mods); i++, m++) {
ret = m->init(fd);
if (ret < 0) {
pr_err("%s: error initializing %s\n", __func__,
m->name);
goto err;
}
}
return 0;
err:
while (--m, --i >= 0)
m->exit(fd);
return ret;
}
void fd_remove(struct spec_dev *dev)
{
struct modlist *m;
int i = ARRAY_SIZE(mods);
pr_debug("%s\n",__func__);
while (--i >= 0) {
m = mods + i;
m->exit(dev->sub_priv);
}
}
static int fd_init(void)
{
int ret;
pr_debug("%s\n",__func__);
ret = fd_zio_register();
if (ret < 0)
return ret;
ret = fd_spec_init();
if (ret < 0) {
fd_zio_unregister();
return ret;
}
return 0;
}
static void fd_exit(void)
{
fd_spec_exit();
fd_zio_unregister();
}
module_init(fd_init);
module_exit(fd_exit);
MODULE_LICENSE("GPL and additional rights"); /* LGPL */
/*
* SPEC interface for the fine-delay driver
*
* Copyright (C) 2012 CERN (www.cern.ch)
* Author: Alessandro Rubini <rubini@gnudd.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2 as published by the Free Software Foundation or, at your
* option, any later version.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/io.h>
#include "spec.h"
#include "fine-delay.h"
static int fd_is_valid(int bus, int devfn)
{
/* FIXME: restrict to some of the spec devices with moduleparam */
pr_debug("%s: %x %x\n", __func__, bus, devfn);
return 1;
}
int fd_spec_init(void)
{
struct spec_dev *dev;
int ret, retsave = 0, err = 0;
/* Scan the list and see what is there. Take hold of everything */
list_for_each_entry(dev, &spec_list, list) {
if (!fd_is_valid(dev->pdev->bus->number, dev->pdev->devfn))
continue;
pr_debug("%s: init %04x:%04x (%pR - %p)\n", __func__,
dev->pdev->bus->number, dev->pdev->devfn,
dev->area[0], dev->remap[0]);
ret = fd_probe(dev);
if (ret < 0) {
retsave = ret;
err++;
}
}
if (err) {
/* FIXME: doesn't undo the other boards */
return retsave;
}
return 0;
}
void fd_spec_exit(void)
{
struct spec_dev *dev;
list_for_each_entry(dev, &spec_list, list) {
if (!fd_is_valid(dev->pdev->bus->number, dev->pdev->devfn))
continue;
pr_debug("%s: release %04x:%04x (%pR - %p)\n", __func__,
dev->pdev->bus->number, dev->pdev->devfn,
dev->area[0], dev->remap[0]);
fd_remove(dev);
}
}
/*
* ZIO interface for the fine-delay driver
*
* Copyright (C) 2012 CERN (www.cern.ch)
* Author: Alessandro Rubini <rubini@gnudd.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2 as published by the Free Software Foundation or, at your
* option, any later version.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/io.h>
#include <linux/zio.h>
#include "fine-delay.h"
/* Register and unregister are used to set up the template driver */
int fd_zio_register(void)
{
/* FIXME */
return 0;
}
void fd_zio_unregister(void)
{
/* FIXME */
}
#ifndef __FINE_DELAY_H__
#define __FINE_DELAY_H__
struct spec_fd {
struct spec_dev *spec;
unsigned char __iomem *base; /* regs files are byte-oriented */
unsigned char __iomem *regs;
};
#define FD_REGS_OFFSET 0x84000
/* SPI Bus chip selects */
#define FD_CS_PLL 1 /* AD9516 PLL */
#define FD_CS_GPIO 2 /* MCP23S17 GPIO */
/* Functions exported by fd-core.c */
extern int fd_probe(struct spec_dev *dev);
extern void fd_remove(struct spec_dev *dev);
/* Functions exported by spi.c */
extern int fd_spi_xfer(struct spec_fd *fd, int ss, int num_bits,
uint32_t in, uint32_t *out);
extern int fd_spi_init(struct spec_fd *fd);
extern void fd_spi_exit(struct spec_fd *fd);
/* Functions exported by pll.c */
extern int fd_pll_init(struct spec_fd *fd);
extern void fd_pll_exit(struct spec_fd *fd);
/* Functions exported by fd-zio.c */
extern int fd_zio_register(void);
extern void fd_zio_unregister(void);
extern int fd_zio_init(struct spec_fd *fd);
extern void fd_zio_exit(struct spec_fd *fd);
/* Functions exported by fd-spec.c */
extern int fd_spec_init(void);
extern void fd_spec_exit(void);
#endif /* __FINE_DELAY_H__ */
/*
* PLL access (AD9516) for fine-delay card
*
* Copyright (C) 2012 CERN (www.cern.ch)
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
* Author: Alessandro Rubini <rubini@gnudd.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2 as published by the Free Software Foundation or, at your
* option, any later version.
*/
#include <linux/io.h>
#include "fine-delay.h"
static int pll_writel(struct spec_fd *fd, int val, int reg)
{
return fd_spi_xfer(fd, FD_CS_PLL, 24, (reg << 8) | val, NULL);
}
static int pll_readl(struct spec_fd *fd, int reg)
{
uint32_t ret;
int err;
err = fd_spi_xfer(fd, FD_CS_PLL, 24, (reg << 8) | (1 << 23), &ret);
if (err < 0)
return err;
return ret & 0xff;
}
int fd_pll_init(struct spec_fd *fd)
{
int reg;
pr_debug("%s\n",__func__);
if (pll_writel(fd, 0x99, 0x000) < 0)
goto out;
if (pll_writel(fd, 0x01, 0x232) < 0)
goto out;
reg = pll_readl(fd, 0x003);
if (reg < 0)
goto out;
if (reg != 0xc3) {
pr_err("%s: Error in PLL communication\n", KBUILD_MODNAME);
pr_err(" (got 0x%x, expected 0xc3)\n", reg);
return -EIO;
}
/* FIXME: program the pll */
pr_debug("success!\n");
return 0;
out:
pr_err("%s: Error in SPI communication\n", KBUILD_MODNAME);
return -EIO;
}
void fd_pll_exit(struct spec_fd *fd)
{
/* nothing to do */
}
/*
* SPI access to fine-delay internals
*
* Copyright (C) 2012 CERN (www.cern.ch)
* Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
* Author: Alessandro Rubini <rubini@gnudd.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2 as published by the Free Software Foundation or, at your
* option, any later version.
*/
#include <linux/jiffies.h>
#include <linux/io.h>
#include <linux/delay.h>
#include "fine-delay.h"
#include "hw/fd_main_regs.h"
int fd_spi_xfer(struct spec_fd *fd, int ss, int num_bits,
uint32_t in, uint32_t *out)
{
uint32_t scr = 0, r;
unsigned long j = jiffies + HZ;
scr = FD_SCR_DATA_W(in)| FD_SCR_CPOL;
if(ss == FD_CS_PLL)
scr |= FD_SCR_SEL_PLL;
else if(ss == FD_CS_GPIO)
scr |= FD_SCR_SEL_GPIO;
writel(scr, fd->regs + FD_REG_SCR);
writel(scr | FD_SCR_START, fd->regs + FD_REG_SCR);
while (!(readl(fd->regs + FD_REG_SCR) & FD_SCR_READY))
if (jiffies > j)
break;
if (!(readl(fd->regs + FD_REG_SCR) & FD_SCR_READY))
return -EIO;
scr = readl(fd->regs + FD_REG_SCR);
r = FD_SCR_DATA_R(scr);
if(out) *out=r;
udelay(100); /* FIXME: check */
return 0;
}
int fd_spi_init(struct spec_fd *fd)
{
/* nothing to do */
return 0;
}
void fd_spi_exit(struct spec_fd *fd)
{
/* nothing to do */
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment