]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ns_dev_can.c
Merge: Correction for 2.6.23-git kernel - unregister_chrdev() does not return value.
[lincan.git] / lincan / src / ns_dev_can.c
1 /* ns_dev_can.c - FPGA version of C_CAN ARM device specific code
2  * Linux CAN-bus device driver.
3  * Written by Sebastian Stolzenberg email:stolzi@sebastian-stolzenberg.de
4  * Based on code from Arnaud Westenberg email:arnaud@wanadoo.nl
5  * and Ake Hedman, eurosource, akhe@eurosource.se
6  * Rewritten for new CAN queues by Pavel Pisa - OCERA team member
7  * Ported to FS Forth-Systeme GmbH A9M9750DEVx development boards
8  * email:nbryan@embebidos.com
9  * This software is released under the GPL-License.
10  * Version lincan-0.3  17 Jun 2004
11  * This port 19 May 2005
12  *
13  */
14
15 #include <linux/delay.h>
16
17 #include "../include/can.h"
18 #include "../include/can_sysdep.h"
19 #include "../include/main.h"
20 #include "../include/c_can.h"
21 #include "../include/ns_dev_can.h"
22
23 /*
24  * IO range for the C_CAN 1.2 memory map is 0x100 (256bytes)
25  */
26 #define IO_RANGE 0x100
27
28 /**
29  * ns_dev_request_io: - reserve io or memory range for can board
30  * @candev: pointer to candevice/board which asks for io. Field @io_addr
31  *      of @candev is used in most cases to define start of the range
32  *
33  */
34 int ns_dev_request_io(struct candevice_t *candev)
35 {
36         /* Note hard-coded index for the chip number as this 
37          * only supports a single instance of the C_CAN controller.
38          */
39         DEBUGMSG("(c%d)ns_dev_request_io (...)\n", candev->chip[0]->chip_idx);
40
41         if (!can_request_mem_region(candev->io_addr, IO_RANGE, DEVICE_NAME)) {
42                 CANMSG("ns_dev failed to  mem region %lx.\n",
43                        (unsigned long)candev->io_addr);
44         }
45
46         if (!(candev->dev_base_addr = ioremap(candev->io_addr, IO_RANGE))) {
47                 DEBUGMSG
48                     ("Failed to map IO-memory: 0x%lx - 0x%lx, mapped to 0x%lx\n",
49                      (unsigned long)candev->io_addr,
50                      (unsigned long)candev->io_addr + IO_RANGE - 1,
51                      (unsigned long)candev->dev_base_addr);
52                 can_release_mem_region(candev->io_addr, IO_RANGE);
53                 return -ENODEV;
54         } else {
55
56                 DEBUGMSG("Mapped IO-memory: 0x%lx - 0x%lx, mapped to 0x%lx\n",
57                          (unsigned long)candev->io_addr,
58                          (unsigned long)candev->io_addr + IO_RANGE - 1,
59                          (unsigned long)candev->dev_base_addr);
60
61         }
62
63         candev->chip[0]->chip_base_addr = candev->dev_base_addr;
64         candev->chip[0]->chipspecops->start_chip(candev->chip[0]);
65
66         return 0;
67 }
68
69 /**
70  * ns_dev_release_io - free reserved io memory range
71  * @candev: pointer to candevice/board which releases io
72  *
73  * The function ns_dev_release_io() is used to free reserved io-memory.
74  * In case you have reserved more io memory, don't forget to free it here.
75  *
76  */
77 int ns_dev_release_io(struct candevice_t *candev)
78 {
79         DEBUGMSG("(c%d)ns_dev_release_io (...)\n", candev->chip[0]->chip_idx);
80
81         /* Release I/O memory mapping */
82         iounmap(candev->dev_base_addr);
83
84         /* Release the memory region */
85         can_release_mem_region(candev->io_addr, IO_RANGE);
86
87         return 0;
88 }
89
90 /**
91  * ns_dev_reset - hardware reset routine
92  * @card: Number of the hardware card.
93  *
94  * The function ns_dev_reset() is used to give a hardware reset. This is
95  * rather hardware specific so I haven't included example code. Don't forget to
96  * check the reset status of the chip before returning.
97  * Return Value: The function returns zero on success or %-ENODEV on failure
98  *
99  */
100 int ns_dev_reset(struct candevice_t *candev)
101 {
102         int i = 0;
103         int enableTest = 0;
104         int disableTest = 0;
105
106         struct canchip_t *pchip = candev->chip[0];
107
108         enableTest = pchip->chipspecops->enable_configuration(pchip);
109         disableTest = pchip->chipspecops->disable_configuration(pchip);
110
111         if (enableTest || disableTest) {
112                 CANMSG("Enable or Disable status failed!\n");
113                 CANMSG("Please check your hardware.\n");
114                 return -ENODEV;
115         }
116
117         /* Check busoff status */
118         while ((c_can_read_reg_w(pchip, CCSR) & SR_BOFF) && (i <= 15)) {
119                 udelay(2000);
120                 i++;
121         }
122
123         if (i >= 15) {
124                 CANMSG("Reset status timeout!\n");
125                 CANMSG("Please check your hardware.\n");
126                 return -ENODEV;
127         }
128         //pchip->config_irqs(pchip, CR_MIE | CR_SIE | CR_EIE);
129         return 0;
130 }
131
132 #define RESET_ADDR 0x0
133 #define NR_C_CAN 1
134 #define NR_MSGOBJ 32
135
136 /**
137  * ns_dev_hw_data - Initialize hardware cards
138  * @candev: Pointer to candevice/board structure
139  *
140  * The function ns_dev_init_hw_data() is used to initialize the hardware
141  * structure containing information about the installed CAN-board.
142  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
143  * the hardware uses programmable interrupts.
144  * Return Value: The function always returns zero
145  */
146 int ns_dev_init_hw_data(struct candevice_t *candev)
147 {
148         can_ioptr_t sys_contVA = NULL;
149
150         /* LUCAN : Magic numbers */
151         if (!(sys_contVA = ioremap(NS9750_PERIPHERAL_BASE_ADDRESS,
152                                          NS9750_PERIPHERAL_MAP_SIZE))) {
153                 DEBUGMSG("Failed to map FPGA memory\n");
154                 return -EIO;
155         } else {
156                 DEBUGMSG("Writing to NS9750 sys cont\n");
157                 can_writel((BUS_WIDTH_16BIT | ACTIVE_LOW_CHIP_SELECT),
158                        sys_contVA + NS9750_SYSTEM_CONTROLLER_OFFSET);
159         }
160
161         /* We have finished with this mapping */
162         iounmap(sys_contVA);
163
164         candev->nr_82527_chips = 0;
165         candev->nr_sja1000_chips = 0;
166         candev->nr_all_chips = NR_C_CAN;
167         candev->flags |= CANDEV_PROGRAMMABLE_IRQ;
168
169         return 0;
170 }
171
172 /**
173  * ns_dev_init_chip_data - Initialize chips
174  * @candev: Pointer to candevice/board structure
175  * @chipnr: Number of the CAN chip on the hardware card
176  *
177  * The function ns_dev_init_chip_data() is used to initialize the hardware
178  * structure containing information about the CAN chips.
179  * %CHIP_TYPE represents the type of CAN chip.
180  * The @chip_base_addr entry represents the start of the 'official' memory map
181  * of the installed chip. It's likely that this is the same as the @io_addr
182  * argument supplied at module loading time.
183  * The @clock entry holds the chip clock value in Hz.
184  * File: src/template.c
185  */
186 int ns_dev_init_chip_data(struct candevice_t *candev, int chipnr)
187 {
188         /* Register chip operations */
189         c_can_fill_chipspecops(candev->chip[chipnr]);
190
191         /* override chip provided default value */
192         candev->chip[chipnr]->max_objects = MAX_MSGOBJS;
193         candev->chip[chipnr]->chip_base_addr = candev->io_addr;
194         candev->chip[chipnr]->clock = C_CAN_CLOCK_INPUT_FREQUENCY;
195
196         return 0;
197 }
198
199 /**
200  * ns_dev_init_obj_data - Initialize message buffers
201  * @chip: Pointer to chip specific structure
202  * @objnr: Number of the message buffer
203  *
204  * The function ns_dev_init_obj_data() is used to initialize the hardware
205  * structure containing information about the different message objects on the
206  * CAN chip. 
207  * The entry @obj_base_addr represents the first memory address of the message 
208  * object. 
209  * Unless the hardware uses a segmented memory map, flags can be set zero.
210  * Return Value: The function always returns zero
211  * File: src/template.c
212  */
213 int ns_dev_init_obj_data(struct canchip_t *chip, int objnr)
214 {
215
216         DEBUGMSG("(c%d)calling ns_dev_init_obj_data( ...)\n", chip->chip_idx);
217
218         /* It seems, that there is no purpose to setup object base address */
219         chip->msgobj[objnr]->obj_base_addr = 0;
220
221         /*can_msgobj_test_fl(pmsgobj,RX_MODE_EXT); */
222         return 0;
223 }
224
225 /**
226  * ns_dev_write_register - Low level write register routine
227  * @data: data to be written
228  * @address: memory address to write to
229  *
230  * The function ns_dev_write_register() is used to write to hardware registers
231  * on the CAN chip. You should only have to edit this function if your hardware
232  * uses some specific write process.
233  * Return Value: The function does not return a value
234  * File: src/template.c
235  */
236 void ns_dev_write_register(unsigned data, can_ioptr_t address)
237 {
238         int i;
239         //unsigned long usecs = 1;
240
241         can_writew(data, address);
242         //udelay( usecs );
243         for (i = 0; i < 5; i++) ;
244 }
245
246 /**
247  * ns_dev_read_register - Low level read register routine
248  * @address: memory address to read from
249  *
250  * The function ns_dev_read_register() is used to read from hardware registers
251  * on the CAN chip. You should only have to edit this function if your hardware
252  * uses some specific read process.
253  * Return Value: The function returns the value stored in @address
254  * File: src/template.c
255  */
256 unsigned ns_dev_read_register(can_ioptr_t address)
257 {
258         u16 value, i;
259
260         value = can_readw(address);
261         //udelay( usecs );
262         for (i = 0; i < 5; i++) ;
263         value = can_readw(address);
264         //udelay( usecs );
265         for (i = 0; i < 5; i++) ;
266
267         return value;
268 }
269
270 /**
271  * ns_dev_program_irq - program interrupts
272  * @candev: Pointer to candevice/board structure
273  *
274  * The function ns_dev_program_irq() is used for hardware that uses 
275  * programmable interrupts. If your hardware doesn't use programmable interrupts
276  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
277  * leave this function unedited. Again this function is hardware specific so 
278  * there's no example code.
279  * Return value: The function returns zero on success or %-ENODEV on failure
280  * File: src/template.c
281  */
282 int ns_dev_program_irq(struct candevice_t *candev)
283 {
284         return 0;
285 }
286
287 int ns_dev_register(struct hwspecops_t *hwspecops)
288 {
289         hwspecops->request_io = ns_dev_request_io;
290         hwspecops->release_io = ns_dev_release_io;
291         hwspecops->reset = ns_dev_reset;
292         hwspecops->init_hw_data = ns_dev_init_hw_data;
293         hwspecops->init_chip_data = ns_dev_init_chip_data;
294         hwspecops->init_obj_data = ns_dev_init_obj_data;
295         hwspecops->write_register = ns_dev_write_register;
296         hwspecops->read_register = ns_dev_read_register;
297         hwspecops->program_irq = ns_dev_program_irq;
298         return 0;
299 }