Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sja1000_platform.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Sascha Hauer, Pengutronix
3  * Copyright (C) 2007 Wolfgang Grandegger <[email protected]>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the version 2 of the GNU General Public License
7  * as published by the Free Software Foundation
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/netdevice.h>
23 #include <linux/delay.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/irq.h>
27 #include <linux/can/dev.h>
29 #include <linux/io.h>
30 
31 #include "sja1000.h"
32 
33 #define DRV_NAME "sja1000_platform"
34 
35 MODULE_AUTHOR("Sascha Hauer <[email protected]>");
36 MODULE_DESCRIPTION("Socket-CAN driver for SJA1000 on the platform bus");
37 MODULE_LICENSE("GPL v2");
38 
39 static u8 sp_read_reg8(const struct sja1000_priv *priv, int reg)
40 {
41  return ioread8(priv->reg_base + reg);
42 }
43 
44 static void sp_write_reg8(const struct sja1000_priv *priv, int reg, u8 val)
45 {
46  iowrite8(val, priv->reg_base + reg);
47 }
48 
49 static u8 sp_read_reg16(const struct sja1000_priv *priv, int reg)
50 {
51  return ioread8(priv->reg_base + reg * 2);
52 }
53 
54 static void sp_write_reg16(const struct sja1000_priv *priv, int reg, u8 val)
55 {
56  iowrite8(val, priv->reg_base + reg * 2);
57 }
58 
59 static u8 sp_read_reg32(const struct sja1000_priv *priv, int reg)
60 {
61  return ioread8(priv->reg_base + reg * 4);
62 }
63 
64 static void sp_write_reg32(const struct sja1000_priv *priv, int reg, u8 val)
65 {
66  iowrite8(val, priv->reg_base + reg * 4);
67 }
68 
69 static int sp_probe(struct platform_device *pdev)
70 {
71  int err;
72  void __iomem *addr;
73  struct net_device *dev;
74  struct sja1000_priv *priv;
75  struct resource *res_mem, *res_irq;
77 
78  pdata = pdev->dev.platform_data;
79  if (!pdata) {
80  dev_err(&pdev->dev, "No platform data provided!\n");
81  err = -ENODEV;
82  goto exit;
83  }
84 
85  res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
86  res_irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
87  if (!res_mem || !res_irq) {
88  err = -ENODEV;
89  goto exit;
90  }
91 
92  if (!request_mem_region(res_mem->start, resource_size(res_mem),
93  DRV_NAME)) {
94  err = -EBUSY;
95  goto exit;
96  }
97 
98  addr = ioremap_nocache(res_mem->start, resource_size(res_mem));
99  if (!addr) {
100  err = -ENOMEM;
101  goto exit_release;
102  }
103 
104  dev = alloc_sja1000dev(0);
105  if (!dev) {
106  err = -ENOMEM;
107  goto exit_iounmap;
108  }
109  priv = netdev_priv(dev);
110 
111  dev->irq = res_irq->start;
112  priv->irq_flags = res_irq->flags & IRQF_TRIGGER_MASK;
113  if (res_irq->flags & IORESOURCE_IRQ_SHAREABLE)
114  priv->irq_flags |= IRQF_SHARED;
115  priv->reg_base = addr;
116  /* The CAN clock frequency is half the oscillator clock frequency */
117  priv->can.clock.freq = pdata->osc_freq / 2;
118  priv->ocr = pdata->ocr;
119  priv->cdr = pdata->cdr;
120 
121  switch (res_mem->flags & IORESOURCE_MEM_TYPE_MASK) {
123  priv->read_reg = sp_read_reg32;
124  priv->write_reg = sp_write_reg32;
125  break;
127  priv->read_reg = sp_read_reg16;
128  priv->write_reg = sp_write_reg16;
129  break;
130  case IORESOURCE_MEM_8BIT:
131  default:
132  priv->read_reg = sp_read_reg8;
133  priv->write_reg = sp_write_reg8;
134  break;
135  }
136 
137  dev_set_drvdata(&pdev->dev, dev);
138  SET_NETDEV_DEV(dev, &pdev->dev);
139 
140  err = register_sja1000dev(dev);
141  if (err) {
142  dev_err(&pdev->dev, "registering %s failed (err=%d)\n",
143  DRV_NAME, err);
144  goto exit_free;
145  }
146 
147  dev_info(&pdev->dev, "%s device registered (reg_base=%p, irq=%d)\n",
148  DRV_NAME, priv->reg_base, dev->irq);
149  return 0;
150 
151  exit_free:
152  free_sja1000dev(dev);
153  exit_iounmap:
154  iounmap(addr);
155  exit_release:
156  release_mem_region(res_mem->start, resource_size(res_mem));
157  exit:
158  return err;
159 }
160 
161 static int sp_remove(struct platform_device *pdev)
162 {
163  struct net_device *dev = dev_get_drvdata(&pdev->dev);
164  struct sja1000_priv *priv = netdev_priv(dev);
165  struct resource *res;
166 
168  dev_set_drvdata(&pdev->dev, NULL);
169 
170  if (priv->reg_base)
171  iounmap(priv->reg_base);
172 
173  res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
174  release_mem_region(res->start, resource_size(res));
175 
176  free_sja1000dev(dev);
177 
178  return 0;
179 }
180 
181 static struct platform_driver sp_driver = {
182  .probe = sp_probe,
183  .remove = sp_remove,
184  .driver = {
185  .name = DRV_NAME,
186  .owner = THIS_MODULE,
187  },
188 };
189 
190 module_platform_driver(sp_driver);