fp@2589: /**
fp@2589: Network Driver for Beckhoff CCAT communication controller
fp@2589: Copyright (C) 2014 Beckhoff Automation GmbH
fp@2589: Author: Patrick Bruenn
fp@2589:
fp@2589: This program is free software; you can redistribute it and/or modify
fp@2589: it under the terms of the GNU General Public License as published by
fp@2589: the Free Software Foundation; either version 2 of the License, or
fp@2589: (at your option) any later version.
fp@2589:
fp@2589: This program is distributed in the hope that it will be useful,
fp@2589: but WITHOUT ANY WARRANTY; without even the implied warranty of
fp@2589: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fp@2589: GNU General Public License for more details.
fp@2589:
fp@2589: You should have received a copy of the GNU General Public License along
fp@2589: with this program; if not, write to the Free Software Foundation, Inc.,
fp@2589: 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fp@2589: */
fp@2589:
fp@2589: #include
fp@2589: #include
fp@2589: #include
fp@2589: #include
fp@2589: #include
fp@2589: #include "module.h"
fp@2589: #include "netdev.h"
fp@2589: #include "update.h"
fp@2589:
fp@2589: MODULE_DESCRIPTION(DRV_DESCRIPTION);
fp@2589: MODULE_AUTHOR("Patrick Bruenn ");
fp@2589: MODULE_LICENSE("GPL");
fp@2589: MODULE_VERSION(DRV_VERSION);
fp@2589:
fp@2589: static void ccat_bar_free(struct ccat_bar *bar)
fp@2589: {
fp@2589: if (bar->ioaddr) {
fp@2589: const struct ccat_bar tmp = *bar;
fp@2589: memset(bar, 0, sizeof(*bar));
fp@2589: iounmap(tmp.ioaddr);
fp@2589: release_mem_region(tmp.start, tmp.len);
fp@2589: } else {
fp@2589: pr_warn("%s(): %p was already done.\n", __FUNCTION__, bar);
fp@2589: }
fp@2589: }
fp@2589:
fp@2589: /**
fp@2589: * ccat_bar_init() - Initialize a CCAT pci bar
fp@2589: * @bar object which should be initialized
fp@2589: * @index 0 and 2 are valid for CCAT, meaning pci bar0 or pci bar2
fp@2589: * @pdev the pci device as which the CCAT was recognized before
fp@2589: *
fp@2589: * Reading PCI config space; request and map memory region.
fp@2589: */
fp@2589: static int ccat_bar_init(struct ccat_bar *bar, size_t index,
fp@2589: struct pci_dev *pdev)
fp@2589: {
fp@2589: struct resource *res;
fp@2589:
fp@2589: bar->start = pci_resource_start(pdev, index);
fp@2589: bar->end = pci_resource_end(pdev, index);
fp@2589: bar->len = pci_resource_len(pdev, index);
fp@2589: bar->flags = pci_resource_flags(pdev, index);
fp@2589: if (!(IORESOURCE_MEM & bar->flags)) {
fp@2589: pr_info("bar%llu is no mem_region -> abort.\n", (u64) index);
fp@2589: return -EIO;
fp@2589: }
fp@2589:
fp@2589: res = request_mem_region(bar->start, bar->len, KBUILD_MODNAME);
fp@2589: if (!res) {
fp@2589: pr_info("allocate mem_region failed.\n");
fp@2589: return -EIO;
fp@2589: }
fp@2589: pr_debug("bar%llu at [%lx,%lx] len=%lu res: %p.\n", (u64) index,
fp@2589: bar->start, bar->end, bar->len, res);
fp@2589:
fp@2589: bar->ioaddr = ioremap(bar->start, bar->len);
fp@2589: if (!bar->ioaddr) {
fp@2589: pr_info("bar%llu ioremap failed.\n", (u64) index);
fp@2589: release_mem_region(bar->start, bar->len);
fp@2589: return -EIO;
fp@2589: }
fp@2589: pr_debug("bar%llu I/O mem mapped to %p.\n", (u64) index, bar->ioaddr);
fp@2589: return 0;
fp@2589: }
fp@2589:
fp@2589: void ccat_dma_free(struct ccat_dma *const dma)
fp@2589: {
fp@2589: const struct ccat_dma tmp = *dma;
fp@2589:
fp@2589: free_dma(dma->channel);
fp@2589: memset(dma, 0, sizeof(*dma));
fp@2589: dma_free_coherent(tmp.dev, tmp.size, tmp.virt, tmp.phys);
fp@2589: }
fp@2589:
fp@2589: /**
fp@2589: * ccat_dma_init() - Initialize CCAT and host memory for DMA transfer
fp@2589: * @dma object for management data which will be initialized
fp@2589: * @channel number of the DMA channel
fp@2589: * @ioaddr of the pci bar2 configspace used to calculate the address of the pci dma configuration
fp@2589: * @dev which should be configured for DMA
fp@2589: */
fp@2589: int ccat_dma_init(struct ccat_dma *const dma, size_t channel,
fp@2589: void __iomem * const ioaddr, struct device *const dev)
fp@2589: {
fp@2589: void *frame;
fp@2589: u64 addr;
fp@2589: u32 translateAddr;
fp@2589: u32 memTranslate;
fp@2589: u32 memSize;
fp@2589: u32 data = 0xffffffff;
fp@2589: u32 offset = (sizeof(u64) * channel) + 0x1000;
fp@2589:
fp@2589: dma->channel = channel;
fp@2589: dma->dev = dev;
fp@2589:
fp@2589: /* calculate size and alignments */
fp@2589: iowrite32(data, ioaddr + offset);
fp@2589: wmb();
fp@2589: data = ioread32(ioaddr + offset);
fp@2589: memTranslate = data & 0xfffffffc;
fp@2589: memSize = (~memTranslate) + 1;
fp@2589: dma->size = 2 * memSize - PAGE_SIZE;
fp@2589: dma->virt = dma_zalloc_coherent(dev, dma->size, &dma->phys, GFP_KERNEL);
fp@2589: if (!dma->virt || !dma->phys) {
fp@2589: pr_info("init DMA%llu memory failed.\n", (u64) channel);
fp@2589: return -1;
fp@2589: }
fp@2589:
fp@2589: if (request_dma(channel, KBUILD_MODNAME)) {
fp@2589: pr_info("request dma channel %llu failed\n", (u64) channel);
fp@2589: ccat_dma_free(dma);
fp@2589: return -1;
fp@2589: }
fp@2589:
fp@2589: translateAddr = (dma->phys + memSize - PAGE_SIZE) & memTranslate;
fp@2589: addr = translateAddr;
fp@2589: memcpy_toio(ioaddr + offset, &addr, sizeof(addr));
fp@2589: frame = dma->virt + translateAddr - dma->phys;
fp@2589: pr_debug
fp@2589: ("DMA%llu mem initialized\n virt: 0x%p\n phys: 0x%llx\n translated: 0x%llx\n pci addr: 0x%08x%x\n memTranslate: 0x%x\n size: %llu bytes.\n",
fp@2589: (u64) channel, dma->virt, (u64) (dma->phys), addr,
fp@2589: ioread32(ioaddr + offset + 4), ioread32(ioaddr + offset),
fp@2589: memTranslate, (u64) dma->size);
fp@2589: return 0;
fp@2589: }
fp@2589:
fp@2589: /**
fp@2589: * Initialize all available CCAT functions.
fp@2589: *
fp@2589: * Return: count of failed functions
fp@2589: */
fp@2589: static int ccat_functions_init(struct ccat_device *const ccatdev)
fp@2589: {
fp@2589: static const size_t block_size = sizeof(struct ccat_info_block);
fp@2589: void __iomem *addr = ccatdev->bar[0].ioaddr; /** first block is the CCAT information block entry */
fp@2589: const u8 num_func = ioread8(addr + 4); /** number of CCAT function blocks is at offset 0x4 */
fp@2589: const void __iomem *end = addr + (block_size * num_func);
fp@2589: int status = 0; /** count init function failures */
fp@2589:
fp@2589: while (addr < end) {
fp@2589: const u8 type = ioread16(addr);
fp@2589: switch (type) {
fp@2589: case CCATINFO_NOTUSED:
fp@2589: break;
fp@2589: case CCATINFO_EPCS_PROM:
fp@2589: pr_info("Found: CCAT update(EPCS_PROM) -> init()\n");
fp@2589: ccatdev->update = ccat_update_init(ccatdev, addr);
fp@2589: status += (NULL == ccatdev->update);
fp@2589: break;
fp@2589: case CCATINFO_ETHERCAT_MASTER_DMA:
fp@2589: pr_info("Found: ETHERCAT_MASTER_DMA -> init()\n");
fp@2589: ccatdev->ethdev = ccat_eth_init(ccatdev, addr);
fp@2589: status += (NULL == ccatdev->ethdev);
fp@2589: break;
fp@2589: default:
fp@2589: pr_info("Found: 0x%04x not supported\n", type);
fp@2589: break;
fp@2589: }
fp@2589: addr += block_size;
fp@2589: }
fp@2589: return status;
fp@2589: }
fp@2589:
fp@2589: /**
fp@2589: * Destroy all previously initialized CCAT functions
fp@2589: */
fp@2589: static void ccat_functions_remove(struct ccat_device *const ccatdev)
fp@2589: {
fp@2589: if (!ccatdev->ethdev) {
fp@2589: pr_warn("%s(): 'ethdev' was not initialized.\n", __FUNCTION__);
fp@2589: } else {
fp@2589: struct ccat_eth_priv *const ethdev = ccatdev->ethdev;
fp@2589: ccatdev->ethdev = NULL;
fp@2589: ccat_eth_remove(ethdev);
fp@2589: }
fp@2589: if (!ccatdev->update) {
fp@2589: pr_warn("%s(): 'update' was not initialized.\n", __FUNCTION__);
fp@2589: } else {
fp@2589: struct ccat_update *const update = ccatdev->update;
fp@2589: ccatdev->update = NULL;
fp@2589: ccat_update_remove(update);
fp@2589: }
fp@2589: }
fp@2589:
fp@2589: static int ccat_probe(struct pci_dev *pdev, const struct pci_device_id *id)
fp@2589: {
fp@2589: int status;
fp@2589: u8 revision;
fp@2589: struct ccat_device *ccatdev = kmalloc(sizeof(*ccatdev), GFP_KERNEL);
fp@2589:
fp@2589: if (!ccatdev) {
fp@2589: pr_err("%s() out of memory.\n", __FUNCTION__);
fp@2589: return -ENOMEM;
fp@2589: }
fp@2589: memset(ccatdev, 0, sizeof(*ccatdev));
fp@2589: ccatdev->pdev = pdev;
fp@2589: pci_set_drvdata(pdev, ccatdev);
fp@2589:
fp@2589: status = pci_enable_device_mem(pdev);
fp@2589: if (status) {
fp@2589: pr_info("enable %s failed: %d\n", pdev->dev.kobj.name, status);
fp@2589: return status;
fp@2589: }
fp@2589:
fp@2589: status = pci_read_config_byte(pdev, PCI_REVISION_ID, &revision);
fp@2589: if (status) {
fp@2589: pr_warn("read CCAT pci revision failed with %d\n", status);
fp@2589: return status;
fp@2589: }
fp@2589:
fp@2589: #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
fp@2589: if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
fp@2589: pr_debug("64 bit DMA supported, pci rev: %u\n", revision);
fp@2589: } else if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
fp@2589: pr_debug("32 bit DMA supported, pci rev: %u\n", revision);
fp@2589: } else {
fp@2589: pr_warn("No suitable DMA available, pci rev: %u\n", revision);
fp@2589: }
fp@2589: #else
fp@2589: if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
fp@2589: pr_debug("64 bit DMA supported, pci rev: %u\n", revision);
fp@2589: } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
fp@2589: pr_debug("32 bit DMA supported, pci rev: %u\n", revision);
fp@2589: } else {
fp@2589: pr_warn("No suitable DMA available, pci rev: %u\n", revision);
fp@2589: }
fp@2589: #endif
fp@2589:
fp@2589: if (ccat_bar_init(&ccatdev->bar[0], 0, pdev)) {
fp@2589: pr_warn("initialization of bar0 failed.\n");
fp@2589: return -EIO;
fp@2589: }
fp@2589:
fp@2589: if (ccat_bar_init(&ccatdev->bar[2], 2, pdev)) {
fp@2589: pr_warn("initialization of bar2 failed.\n");
fp@2589: return -EIO;
fp@2589: }
fp@2589:
fp@2589: pci_set_master(pdev);
fp@2589: if (ccat_functions_init(ccatdev)) {
fp@2589: pr_warn("some functions couldn't be initialized\n");
fp@2589: }
fp@2589: return 0;
fp@2589: }
fp@2589:
fp@2589: static void ccat_remove(struct pci_dev *pdev)
fp@2589: {
fp@2589: struct ccat_device *ccatdev = pci_get_drvdata(pdev);
fp@2589:
fp@2589: if (ccatdev) {
fp@2589: ccat_functions_remove(ccatdev);
fp@2589: ccat_bar_free(&ccatdev->bar[2]);
fp@2589: ccat_bar_free(&ccatdev->bar[0]);
fp@2589: pci_disable_device(pdev);
fp@2589: pci_set_drvdata(pdev, NULL);
fp@2589: kfree(ccatdev);
fp@2589: }
fp@2589: pr_debug("%s() done.\n", __FUNCTION__);
fp@2589: }
fp@2589:
fp@2589: #define PCI_DEVICE_ID_BECKHOFF_CCAT 0x5000
fp@2589: #define PCI_VENDOR_ID_BECKHOFF 0x15EC
fp@2589:
fp@2589: static const struct pci_device_id pci_ids[] = {
fp@2589: {PCI_DEVICE(PCI_VENDOR_ID_BECKHOFF, PCI_DEVICE_ID_BECKHOFF_CCAT)},
fp@2589: {0,},
fp@2589: };
fp@2589:
fp@2589: #if 0 /* prevent auto-loading */
fp@2589: MODULE_DEVICE_TABLE(pci, pci_ids);
fp@2589: #endif
fp@2589:
fp@2589: static struct pci_driver pci_driver = {
fp@2589: .name = KBUILD_MODNAME,
fp@2589: .id_table = pci_ids,
fp@2589: .probe = ccat_probe,
fp@2589: .remove = ccat_remove,
fp@2589: };
fp@2589:
fp@2589: static void __exit ccat_exit_module(void)
fp@2589: {
fp@2589: pci_unregister_driver(&pci_driver);
fp@2589: }
fp@2589:
fp@2589: static int __init ccat_init_module(void)
fp@2589: {
fp@2589: pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
fp@2589: return pci_register_driver(&pci_driver);
fp@2589: }
fp@2589:
fp@2589: module_exit(ccat_exit_module);
fp@2589: module_init(ccat_init_module);