summaryrefslogtreecommitdiff
path: root/drivers/net/can/old/ccan/h7202_can.c
blob: 7dcc3e749248bcb45548f294ca3c0da5c114ff5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * drivers/can/h7202_can.c
 *
 * Copyright (C) 2007
 *
 * - Sascha Hauer, Marc Kleine-Budde, Pengutronix
 * - Simon Kallweit, intefo AG
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the version 2 of the GNU General Public License
 * as published by the Free Software Foundation
 *
 * 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
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/netdevice.h>
#include <linux/can.h>
#include <linux/can/dev.h>
#include <linux/io.h>
#include <asm/hardware.h>

#include "ccan.h"

#define DRV_NAME      "h7202can"
#define DELAY         5
#define CAN_ENABLE    0x0e

static u16 h7202can_read_reg(struct net_device *dev, enum c_regs reg)
{
	u16 val;
	volatile int i;

	/* The big kernel lock is used to prevent any other AMBA devices from
	 * interfering with the current register read operation. The register
	 * is read twice because of braindamaged hynix cpu.
	 */
	lock_kernel();
	val = inw(dev->base_addr + (reg<<1));
	for (i = 0; i < DELAY; i++);
	val = inw(dev->base_addr + (reg<<1));
	for (i = 0; i < DELAY; i++);
	unlock_kernel();

	return val;
}

static void h7202can_write_reg(struct net_device *dev, enum c_regs reg, u16 val)
{
	volatile int i;

	lock_kernel();
	outw(val, dev->base_addr + (reg<<1));
	for (i = 0; i < DELAY; i++);
	unlock_kernel();
}

static int h7202can_drv_probe(struct platform_device *pdev)
{
	struct net_device *dev;
	struct ccan_priv *priv;
	struct resource *mem;
	u32 mem_size;
	int ret = -ENODEV;

	dev = alloc_ccandev(sizeof(struct ccan_priv));
	if (!dev)
		return -ENOMEM;

	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	dev->irq = platform_get_irq(pdev, 0);
	if (!mem || !dev->irq)
		goto req_error;

	mem_size = mem->end - mem->start + 1;
	if (!request_mem_region(mem->start, mem_size, pdev->dev.driver->name)) {
		dev_err(&pdev->dev, "resource unavailable\n");
		goto req_error;
	}

	SET_NETDEV_DEV(dev, &pdev->dev);

	dev->base_addr = (unsigned long)ioremap_nocache(mem->start, mem_size);

	if (!dev->base_addr) {
		dev_err(&pdev->dev, "failed to map can port\n");
		ret = -ENOMEM;
		goto fail_map;
	}

	priv = netdev_priv(dev);
	priv->can.can_sys_clock = 8000000;
	priv->read_reg = h7202can_read_reg;
	priv->write_reg = h7202can_write_reg;

	platform_set_drvdata(pdev, dev);

	/* configure ports */
	switch (mem->start) {
	case CAN0_PHYS:
		CPU_REG(GPIO_C_VIRT, GPIO_EN) &= ~(3<<1);
		CPU_REG(GPIO_C_VIRT, GPIO_DIR) &= ~(1<<1);
		CPU_REG(GPIO_C_VIRT, GPIO_DIR) |= (1<<2);
		break;
	case CAN1_PHYS:
		CPU_REG(GPIO_E_VIRT, GPIO_EN) &= ~(3<<16);
		CPU_REG(GPIO_E_VIRT, GPIO_DIR) |= (1<<16);
		CPU_REG(GPIO_E_VIRT, GPIO_DIR) &= ~(1<<17);
		break;
	}

	/* enable can */
	h7202can_write_reg(dev, CAN_ENABLE, 1);

	ret = register_ccandev(dev);
	if (ret >= 0) {
		dev_info(&pdev->dev, "probe for a port 0x%lX done\n",
			 dev->base_addr);
		return ret;
	}

	iounmap((unsigned long *)dev->base_addr);
fail_map:
	release_mem_region(mem->start, mem_size);
req_error:
	free_ccandev(dev);
	dev_err(&pdev->dev, "probe failed\n");
	return ret;
}

static int h7202can_drv_remove(struct platform_device *pdev)
{
	struct net_device *dev = platform_get_drvdata(pdev);
	struct resource *mem;

	platform_set_drvdata(pdev, NULL);
	unregister_ccandev(dev);

	iounmap((volatile void __iomem *)(dev->base_addr));
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	release_mem_region(mem->start, mem->end - mem->start + 1);
	free_ccandev(dev);
	return 0;
}

#ifdef CONFIG_PM
static int h7202can_drv_suspend(struct platform_device *pdev,
				pm_message_t state)
{
	return 0;
}

static int h7202can_drv_resume(struct platform_device *pdev)
{
	return 0;
}
#endif /* CONFIG_PM */

static struct platform_driver h7202can_driver = {
	.driver		= {
		.name		= DRV_NAME,
	},
	.probe		= h7202can_drv_probe,
	.remove		= h7202can_drv_remove,
#ifdef CONFIG_PM
	.suspend	= h7202can_drv_suspend,
	.resume		= h7202can_drv_resume,
#endif	/* CONFIG_PM */
};

static int __init h7202can_init(void)
{
	printk(KERN_INFO "%s initializing\n", h7202can_driver.driver.name);
	return platform_driver_register(&h7202can_driver);
}

static void __exit h7202can_cleanup(void)
{
	platform_driver_unregister(&h7202can_driver);
	printk(KERN_INFO "%s unloaded\n", h7202can_driver.driver.name);
}

module_init(h7202can_init);
module_exit(h7202can_cleanup);

MODULE_AUTHOR("Sascha Hauer <s.hauer@pengutronix.de>");
MODULE_AUTHOR("Simon Kallweit <simon.kallweit@intefo.ch>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("CAN port driver Hynix H7202 processor");