/*
 * Copyright (C) 2021 NIKHEF Amsterdam
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/* 
 * File:   spec7_mod.c
 * Author: Ton Damen <tond@nikhef.nl>
 *
 * Created on April 23, 2020, 13:14 AM
 * Modified on September 14, 2021, Added BAR4
 */

#define DEBUG


#define pr_fmt(fmt)     KBUILD_MODNAME ":%s: " fmt, __func__

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include "spec7.h"
#include "sysfs.h"
#include "misc_dev.h"


#define MODULE_NAME "spec7"
#define MODULE_DESC "SPEC7 experimental Driver"
#define MODULE_RELDATE "September 2021"

MODULE_AUTHOR("<Ton Damen>tond@nikhef.nl");
MODULE_DESCRIPTION(MODULE_DESC);
MODULE_VERSION("0.2");
MODULE_LICENSE("GPL");

//pci_device_id=0x18d pci_vendor_id=0x10dc

static const struct pci_device_id pci_ids[] = {
    { PCI_DEVICE(0x10ee, 0x7022),},
    { PCI_DEVICE(0x10dc, 0x18d),},
    { 0,}
};

MODULE_DEVICE_TABLE(pci, pci_ids);

static int probe_one(struct pci_dev *pdev, const struct pci_device_id *id) {
    struct spec7_dev *spec;
    int rc;

    //pr_info("Buid %s %s\n", __DATE__, __TIME__);
    pr_info("PCI device id: [%x:%x], address: %s\n", pdev->vendor, pdev->device, dev_name(&pdev->dev));

    if (pci_enable_device(pdev) < 0)
        return -EINVAL;

    if ((spec = kzalloc(sizeof (struct spec7_dev), GFP_KERNEL)) == NULL)
        return -ENOMEM;

    spec->pdev = pdev;
    spec->bar0.size = pci_resource_len(pdev, BAR_0);
    spec->bar4.size = pci_resource_len(pdev, BAR_4);
    spec->bar0.phys = pci_resource_start(pdev, BAR_0);
    spec->bar4.phys = pci_resource_start(pdev, BAR_4);
    spec->bar0.base = pci_iomap(pdev, BAR_0, spec->bar0.size);
    spec->bar4.base = pci_iomap(pdev, BAR_4, spec->bar4.size);
    spec->bar0.idx = BAR_0;
    spec->bar4.idx = BAR_4;    
    spec->lm32_mem = spec->bar0.base;
    spec->syscon_reg_addr = spec->bar0.base + SYSCON_REG_ADDR;

    pci_set_drvdata(pdev, spec);

    populate_sysfs(spec);

    rc = miscdev_init(BAR0_FNAME, &spec->bar0.mdev);
    if (rc > 0)
        spec->bar0.inode_minor = rc;
    else
        return rc;

    rc = miscdev_init(BAR4_FNAME, &spec->bar4.mdev);
    if (rc > 0)
        spec->bar4.inode_minor = rc;
    else
        return rc;

    pr_info("size bar0 = %lu, bar4 = %lu\n", spec->bar0.size, spec->bar4.size);

    return 0;
}

static void remove_one(struct pci_dev *pdev) {
    struct spec7_dev *spec = pci_get_drvdata(pdev);
    misc_deregister(&spec->bar0.mdev);
    misc_deregister(&spec->bar4.mdev);
    cleanup_sysfs(spec);
    pci_set_drvdata(pdev, NULL);
    kfree(spec);
}

static struct pci_driver pci_driver = {
    .name = MODULE_NAME,
    .id_table = pci_ids,
    .probe = probe_one,
    .remove = remove_one
};

static int __init spec7_module_init(void) {
    int rc = pci_register_driver(&pci_driver);
    if (rc == 0)
        pr_info("loaded\n"); // (compiled at %s %s)\n", __DATE__, __TIME__);
    else
        pr_alert("failed. Could not register pci driver.");
    return rc;
}

static void __exit spec7_module_exit(void) {
    pci_unregister_driver(&pci_driver);
    pr_info("removed\n");
}

module_init(spec7_module_init);
module_exit(spec7_module_exit);
