devices/ccat/module.c
changeset 2589 2b9c78543663
equal deleted inserted replaced
2415:af21f0bdc7c9 2589:2b9c78543663
       
     1 /**
       
     2     Network Driver for Beckhoff CCAT communication controller
       
     3     Copyright (C) 2014  Beckhoff Automation GmbH
       
     4     Author: Patrick Bruenn <p.bruenn@beckhoff.com>
       
     5 
       
     6     This program is free software; you can redistribute it and/or modify
       
     7     it under the terms of the GNU General Public License as published by
       
     8     the Free Software Foundation; either version 2 of the License, or
       
     9     (at your option) any later version.
       
    10 
       
    11     This program is distributed in the hope that it will be useful,
       
    12     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14     GNU General Public License for more details.
       
    15 
       
    16     You should have received a copy of the GNU General Public License along
       
    17     with this program; if not, write to the Free Software Foundation, Inc.,
       
    18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19 */
       
    20 
       
    21 #include <asm/dma.h>
       
    22 #include <linux/etherdevice.h>
       
    23 #include <linux/module.h>
       
    24 #include <linux/netdevice.h>
       
    25 #include <linux/version.h>
       
    26 #include "module.h"
       
    27 #include "netdev.h"
       
    28 #include "update.h"
       
    29 
       
    30 MODULE_DESCRIPTION(DRV_DESCRIPTION);
       
    31 MODULE_AUTHOR("Patrick Bruenn <p.bruenn@beckhoff.com>");
       
    32 MODULE_LICENSE("GPL");
       
    33 MODULE_VERSION(DRV_VERSION);
       
    34 
       
    35 static void ccat_bar_free(struct ccat_bar *bar)
       
    36 {
       
    37 	if (bar->ioaddr) {
       
    38 		const struct ccat_bar tmp = *bar;
       
    39 		memset(bar, 0, sizeof(*bar));
       
    40 		iounmap(tmp.ioaddr);
       
    41 		release_mem_region(tmp.start, tmp.len);
       
    42 	} else {
       
    43 		pr_warn("%s(): %p was already done.\n", __FUNCTION__, bar);
       
    44 	}
       
    45 }
       
    46 
       
    47 /**
       
    48  * ccat_bar_init() - Initialize a CCAT pci bar
       
    49  * @bar object which should be initialized
       
    50  * @index 0 and 2 are valid for CCAT, meaning pci bar0 or pci bar2
       
    51  * @pdev the pci device as which the CCAT was recognized before
       
    52  *
       
    53  * Reading PCI config space; request and map memory region.
       
    54  */
       
    55 static int ccat_bar_init(struct ccat_bar *bar, size_t index,
       
    56 			 struct pci_dev *pdev)
       
    57 {
       
    58 	struct resource *res;
       
    59 
       
    60 	bar->start = pci_resource_start(pdev, index);
       
    61 	bar->end = pci_resource_end(pdev, index);
       
    62 	bar->len = pci_resource_len(pdev, index);
       
    63 	bar->flags = pci_resource_flags(pdev, index);
       
    64 	if (!(IORESOURCE_MEM & bar->flags)) {
       
    65 		pr_info("bar%llu is no mem_region -> abort.\n", (u64) index);
       
    66 		return -EIO;
       
    67 	}
       
    68 
       
    69 	res = request_mem_region(bar->start, bar->len, KBUILD_MODNAME);
       
    70 	if (!res) {
       
    71 		pr_info("allocate mem_region failed.\n");
       
    72 		return -EIO;
       
    73 	}
       
    74 	pr_debug("bar%llu at [%lx,%lx] len=%lu res: %p.\n", (u64) index,
       
    75 		 bar->start, bar->end, bar->len, res);
       
    76 
       
    77 	bar->ioaddr = ioremap(bar->start, bar->len);
       
    78 	if (!bar->ioaddr) {
       
    79 		pr_info("bar%llu ioremap failed.\n", (u64) index);
       
    80 		release_mem_region(bar->start, bar->len);
       
    81 		return -EIO;
       
    82 	}
       
    83 	pr_debug("bar%llu I/O mem mapped to %p.\n", (u64) index, bar->ioaddr);
       
    84 	return 0;
       
    85 }
       
    86 
       
    87 void ccat_dma_free(struct ccat_dma *const dma)
       
    88 {
       
    89 	const struct ccat_dma tmp = *dma;
       
    90 
       
    91 	free_dma(dma->channel);
       
    92 	memset(dma, 0, sizeof(*dma));
       
    93 	dma_free_coherent(tmp.dev, tmp.size, tmp.virt, tmp.phys);
       
    94 }
       
    95 
       
    96 /**
       
    97  * ccat_dma_init() - Initialize CCAT and host memory for DMA transfer
       
    98  * @dma object for management data which will be initialized
       
    99  * @channel number of the DMA channel
       
   100  * @ioaddr of the pci bar2 configspace used to calculate the address of the pci dma configuration
       
   101  * @dev which should be configured for DMA
       
   102  */
       
   103 int ccat_dma_init(struct ccat_dma *const dma, size_t channel,
       
   104 		  void __iomem * const ioaddr, struct device *const dev)
       
   105 {
       
   106 	void *frame;
       
   107 	u64 addr;
       
   108 	u32 translateAddr;
       
   109 	u32 memTranslate;
       
   110 	u32 memSize;
       
   111 	u32 data = 0xffffffff;
       
   112 	u32 offset = (sizeof(u64) * channel) + 0x1000;
       
   113 
       
   114 	dma->channel = channel;
       
   115 	dma->dev = dev;
       
   116 
       
   117 	/* calculate size and alignments */
       
   118 	iowrite32(data, ioaddr + offset);
       
   119 	wmb();
       
   120 	data = ioread32(ioaddr + offset);
       
   121 	memTranslate = data & 0xfffffffc;
       
   122 	memSize = (~memTranslate) + 1;
       
   123 	dma->size = 2 * memSize - PAGE_SIZE;
       
   124 	dma->virt = dma_zalloc_coherent(dev, dma->size, &dma->phys, GFP_KERNEL);
       
   125 	if (!dma->virt || !dma->phys) {
       
   126 		pr_info("init DMA%llu memory failed.\n", (u64) channel);
       
   127 		return -1;
       
   128 	}
       
   129 
       
   130 	if (request_dma(channel, KBUILD_MODNAME)) {
       
   131 		pr_info("request dma channel %llu failed\n", (u64) channel);
       
   132 		ccat_dma_free(dma);
       
   133 		return -1;
       
   134 	}
       
   135 
       
   136 	translateAddr = (dma->phys + memSize - PAGE_SIZE) & memTranslate;
       
   137 	addr = translateAddr;
       
   138 	memcpy_toio(ioaddr + offset, &addr, sizeof(addr));
       
   139 	frame = dma->virt + translateAddr - dma->phys;
       
   140 	pr_debug
       
   141 	    ("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",
       
   142 	     (u64) channel, dma->virt, (u64) (dma->phys), addr,
       
   143 	     ioread32(ioaddr + offset + 4), ioread32(ioaddr + offset),
       
   144 	     memTranslate, (u64) dma->size);
       
   145 	return 0;
       
   146 }
       
   147 
       
   148 /**
       
   149  * Initialize all available CCAT functions.
       
   150  *
       
   151  * Return: count of failed functions
       
   152  */
       
   153 static int ccat_functions_init(struct ccat_device *const ccatdev)
       
   154 {
       
   155 	static const size_t block_size = sizeof(struct ccat_info_block);
       
   156 	void __iomem *addr = ccatdev->bar[0].ioaddr; /** first block is the CCAT information block entry */
       
   157 	const u8 num_func = ioread8(addr + 4); /** number of CCAT function blocks is at offset 0x4 */
       
   158 	const void __iomem *end = addr + (block_size * num_func);
       
   159 	int status = 0;	/** count init function failures */
       
   160 
       
   161 	while (addr < end) {
       
   162 		const u8 type = ioread16(addr);
       
   163 		switch (type) {
       
   164 		case CCATINFO_NOTUSED:
       
   165 			break;
       
   166 		case CCATINFO_EPCS_PROM:
       
   167 			pr_info("Found: CCAT update(EPCS_PROM) -> init()\n");
       
   168 			ccatdev->update = ccat_update_init(ccatdev, addr);
       
   169 			status += (NULL == ccatdev->update);
       
   170 			break;
       
   171 		case CCATINFO_ETHERCAT_MASTER_DMA:
       
   172 			pr_info("Found: ETHERCAT_MASTER_DMA -> init()\n");
       
   173 			ccatdev->ethdev = ccat_eth_init(ccatdev, addr);
       
   174 			status += (NULL == ccatdev->ethdev);
       
   175 			break;
       
   176 		default:
       
   177 			pr_info("Found: 0x%04x not supported\n", type);
       
   178 			break;
       
   179 		}
       
   180 		addr += block_size;
       
   181 	}
       
   182 	return status;
       
   183 }
       
   184 
       
   185 /**
       
   186  * Destroy all previously initialized CCAT functions
       
   187  */
       
   188 static void ccat_functions_remove(struct ccat_device *const ccatdev)
       
   189 {
       
   190 	if (!ccatdev->ethdev) {
       
   191 		pr_warn("%s(): 'ethdev' was not initialized.\n", __FUNCTION__);
       
   192 	} else {
       
   193 		struct ccat_eth_priv *const ethdev = ccatdev->ethdev;
       
   194 		ccatdev->ethdev = NULL;
       
   195 		ccat_eth_remove(ethdev);
       
   196 	}
       
   197 	if (!ccatdev->update) {
       
   198 		pr_warn("%s(): 'update' was not initialized.\n", __FUNCTION__);
       
   199 	} else {
       
   200 		struct ccat_update *const update = ccatdev->update;
       
   201 		ccatdev->update = NULL;
       
   202 		ccat_update_remove(update);
       
   203 	}
       
   204 }
       
   205 
       
   206 static int ccat_probe(struct pci_dev *pdev, const struct pci_device_id *id)
       
   207 {
       
   208 	int status;
       
   209 	u8 revision;
       
   210 	struct ccat_device *ccatdev = kmalloc(sizeof(*ccatdev), GFP_KERNEL);
       
   211 
       
   212 	if (!ccatdev) {
       
   213 		pr_err("%s() out of memory.\n", __FUNCTION__);
       
   214 		return -ENOMEM;
       
   215 	}
       
   216 	memset(ccatdev, 0, sizeof(*ccatdev));
       
   217 	ccatdev->pdev = pdev;
       
   218 	pci_set_drvdata(pdev, ccatdev);
       
   219 
       
   220 	status = pci_enable_device_mem(pdev);
       
   221 	if (status) {
       
   222 		pr_info("enable %s failed: %d\n", pdev->dev.kobj.name, status);
       
   223 		return status;
       
   224 	}
       
   225 
       
   226 	status = pci_read_config_byte(pdev, PCI_REVISION_ID, &revision);
       
   227 	if (status) {
       
   228 		pr_warn("read CCAT pci revision failed with %d\n", status);
       
   229 		return status;
       
   230 	}
       
   231 
       
   232 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0)
       
   233 	if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64))) {
       
   234 		pr_debug("64 bit DMA supported, pci rev: %u\n", revision);
       
   235 	} else if (!dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
       
   236 		pr_debug("32 bit DMA supported, pci rev: %u\n", revision);
       
   237 	} else {
       
   238 		pr_warn("No suitable DMA available, pci rev: %u\n", revision);
       
   239 	}
       
   240 #else
       
   241 	if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
       
   242 		pr_debug("64 bit DMA supported, pci rev: %u\n", revision);
       
   243 	} else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
       
   244 		pr_debug("32 bit DMA supported, pci rev: %u\n", revision);
       
   245 	} else {
       
   246 		pr_warn("No suitable DMA available, pci rev: %u\n", revision);
       
   247 	}
       
   248 #endif
       
   249 
       
   250 	if (ccat_bar_init(&ccatdev->bar[0], 0, pdev)) {
       
   251 		pr_warn("initialization of bar0 failed.\n");
       
   252 		return -EIO;
       
   253 	}
       
   254 
       
   255 	if (ccat_bar_init(&ccatdev->bar[2], 2, pdev)) {
       
   256 		pr_warn("initialization of bar2 failed.\n");
       
   257 		return -EIO;
       
   258 	}
       
   259 
       
   260 	pci_set_master(pdev);
       
   261 	if (ccat_functions_init(ccatdev)) {
       
   262 		pr_warn("some functions couldn't be initialized\n");
       
   263 	}
       
   264 	return 0;
       
   265 }
       
   266 
       
   267 static void ccat_remove(struct pci_dev *pdev)
       
   268 {
       
   269 	struct ccat_device *ccatdev = pci_get_drvdata(pdev);
       
   270 
       
   271 	if (ccatdev) {
       
   272 		ccat_functions_remove(ccatdev);
       
   273 		ccat_bar_free(&ccatdev->bar[2]);
       
   274 		ccat_bar_free(&ccatdev->bar[0]);
       
   275 		pci_disable_device(pdev);
       
   276 		pci_set_drvdata(pdev, NULL);
       
   277 		kfree(ccatdev);
       
   278 	}
       
   279 	pr_debug("%s() done.\n", __FUNCTION__);
       
   280 }
       
   281 
       
   282 #define PCI_DEVICE_ID_BECKHOFF_CCAT 0x5000
       
   283 #define PCI_VENDOR_ID_BECKHOFF 0x15EC
       
   284 
       
   285 static const struct pci_device_id pci_ids[] = {
       
   286 	{PCI_DEVICE(PCI_VENDOR_ID_BECKHOFF, PCI_DEVICE_ID_BECKHOFF_CCAT)},
       
   287 	{0,},
       
   288 };
       
   289 
       
   290 #if 0 /* prevent auto-loading */
       
   291 MODULE_DEVICE_TABLE(pci, pci_ids);
       
   292 #endif
       
   293 
       
   294 static struct pci_driver pci_driver = {
       
   295 	.name = KBUILD_MODNAME,
       
   296 	.id_table = pci_ids,
       
   297 	.probe = ccat_probe,
       
   298 	.remove = ccat_remove,
       
   299 };
       
   300 
       
   301 static void __exit ccat_exit_module(void)
       
   302 {
       
   303 	pci_unregister_driver(&pci_driver);
       
   304 }
       
   305 
       
   306 static int __init ccat_init_module(void)
       
   307 {
       
   308 	pr_info("%s, %s\n", DRV_DESCRIPTION, DRV_VERSION);
       
   309 	return pci_register_driver(&pci_driver);
       
   310 }
       
   311 
       
   312 module_exit(ccat_exit_module);
       
   313 module_init(ccat_init_module);