]> rtime.felk.cvut.cz Git - lincan.git/blob - lincan/src/ns_dev_can.c
The use of chip->chip_data is unnecessary, chip->hostdevice points to corresponding...
[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 = (long)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         u16 tempReg;
80         int i;
81
82         DEBUGMSG("(c%d)ns_dev_release_io (...)\n", candev->chip[0]->chip_idx);
83
84         /* Release I/O memory mapping */
85         iounmap((void *)candev->dev_base_addr);
86
87         /* Release the memory region */
88         can_release_mem_region(candev->io_addr, IO_RANGE);
89
90         return 0;
91 }
92
93 /**
94  * ns_dev_reset - hardware reset routine
95  * @card: Number of the hardware card.
96  *
97  * The function ns_dev_reset() is used to give a hardware reset. This is
98  * rather hardware specific so I haven't included example code. Don't forget to
99  * check the reset status of the chip before returning.
100  * Return Value: The function returns zero on success or %-ENODEV on failure
101  *
102  */
103 int ns_dev_reset(struct candevice_t *candev)
104 {
105         int i = 0;
106         int enableTest = 0;
107         int disableTest = 0;
108
109         struct canchip_t *pchip = candev->chip[0];
110
111         enableTest = pchip->chipspecops->enable_configuration(pchip);
112         disableTest = pchip->chipspecops->disable_configuration(pchip);
113
114         if (enableTest || disableTest) {
115                 CANMSG("Enable or Disable status failed!\n");
116                 CANMSG("Please check your hardware.\n");
117                 return -ENODEV;
118         }
119
120         /* Check busoff status */
121         while ((c_can_read_reg_w(pchip, CCSR) & SR_BOFF) && (i <= 15)) {
122                 udelay(2000);
123                 i++;
124         }
125
126         if (i >= 15) {
127                 CANMSG("Reset status timeout!\n");
128                 CANMSG("Please check your hardware.\n");
129                 return -ENODEV;
130         }
131         //pchip->config_irqs(pchip, CR_MIE | CR_SIE | CR_EIE);
132         return 0;
133 }
134
135 #define RESET_ADDR 0x0
136 #define NR_C_CAN 1
137 #define NR_MSGOBJ 32
138
139 /**
140  * ns_dev_hw_data - Initialize hardware cards
141  * @candev: Pointer to candevice/board structure
142  *
143  * The function ns_dev_init_hw_data() is used to initialize the hardware
144  * structure containing information about the installed CAN-board.
145  * The flags entry can currently only be %CANDEV_PROGRAMMABLE_IRQ to indicate that
146  * the hardware uses programmable interrupts.
147  * Return Value: The function always returns zero
148  */
149 int ns_dev_init_hw_data(struct candevice_t *candev)
150 {
151         u32 sys_contVA = 0;
152
153         /* LUCAN : Magic numbers */
154         if (!(sys_contVA = (u32) ioremap(NS9750_PERIPHERAL_BASE_ADDRESS,
155                                          NS9750_PERIPHERAL_MAP_SIZE))) {
156                 DEBUGMSG("Failed to map FPGA memory\n");
157                 return -EIO;
158         } else {
159                 DEBUGMSG("Writing to NS9750 sys cont\n");
160                 writel((BUS_WIDTH_16BIT | ACTIVE_LOW_CHIP_SELECT),
161                        sys_contVA + NS9750_SYSTEM_CONTROLLER_OFFSET);
162         }
163
164         /* We have finished with this mapping */
165         iounmap((void *)sys_contVA);
166
167         candev->nr_82527_chips = 0;
168         candev->nr_sja1000_chips = 0;
169         candev->nr_all_chips = NR_C_CAN;
170         candev->flags |= CANDEV_PROGRAMMABLE_IRQ;
171
172         return 0;
173 }
174
175 /**
176  * ns_dev_init_chip_data - Initialize chips
177  * @candev: Pointer to candevice/board structure
178  * @chipnr: Number of the CAN chip on the hardware card
179  *
180  * The function ns_dev_init_chip_data() is used to initialize the hardware
181  * structure containing information about the CAN chips.
182  * %CHIP_TYPE represents the type of CAN chip.
183  * The @chip_base_addr entry represents the start of the 'official' memory map
184  * of the installed chip. It's likely that this is the same as the @io_addr
185  * argument supplied at module loading time.
186  * The @clock entry holds the chip clock value in Hz.
187  * File: src/template.c
188  */
189 int ns_dev_init_chip_data(struct candevice_t *candev, int chipnr)
190 {
191         /* Register chip operations */
192         c_can_fill_chipspecops(candev->chip[chipnr]);
193
194         /* override chip provided default value */
195         candev->chip[chipnr]->max_objects = MAX_MSGOBJS;
196         candev->chip[chipnr]->chip_base_addr = candev->io_addr;
197         candev->chip[chipnr]->clock = C_CAN_CLOCK_INPUT_FREQUENCY;
198
199         return 0;
200 }
201
202 /**
203  * ns_dev_init_obj_data - Initialize message buffers
204  * @chip: Pointer to chip specific structure
205  * @objnr: Number of the message buffer
206  *
207  * The function ns_dev_init_obj_data() is used to initialize the hardware
208  * structure containing information about the different message objects on the
209  * CAN chip. 
210  * The entry @obj_base_addr represents the first memory address of the message 
211  * object. 
212  * Unless the hardware uses a segmented memory map, flags can be set zero.
213  * Return Value: The function always returns zero
214  * File: src/template.c
215  */
216 int ns_dev_init_obj_data(struct canchip_t *chip, int objnr)
217 {
218
219         DEBUGMSG("(c%d)calling ns_dev_init_obj_data( ...)\n", chip->chip_idx);
220
221         /* It seems, that there is no purpose to setup object base address */
222         chip->msgobj[objnr]->obj_base_addr = 0;
223
224         /*can_msgobj_test_fl(pmsgobj,RX_MODE_EXT); */
225         return 0;
226 }
227
228 /**
229  * ns_dev_write_register - Low level write register routine
230  * @data: data to be written
231  * @address: memory address to write to
232  *
233  * The function ns_dev_write_register() is used to write to hardware registers
234  * on the CAN chip. You should only have to edit this function if your hardware
235  * uses some specific write process.
236  * Return Value: The function does not return a value
237  * File: src/template.c
238  */
239 void ns_dev_write_register(unsigned data, unsigned long address)
240 {
241         int i;
242         //unsigned long usecs = 1;
243
244         writew(data, address);
245         //udelay( usecs );
246         for (i = 0; i < 5; i++) ;
247 }
248
249 /**
250  * ns_dev_read_register - Low level read register routine
251  * @address: memory address to read from
252  *
253  * The function ns_dev_read_register() is used to read from hardware registers
254  * on the CAN chip. You should only have to edit this function if your hardware
255  * uses some specific read process.
256  * Return Value: The function returns the value stored in @address
257  * File: src/template.c
258  */
259 unsigned ns_dev_read_register(unsigned long address)
260 {
261         u16 value, i;
262
263         value = readw(address);
264         //udelay( usecs );
265         for (i = 0; i < 5; i++) ;
266         value = readw(address);
267         //udelay( usecs );
268         for (i = 0; i < 5; i++) ;
269
270         return value;
271 }
272
273 /**
274  * ns_dev_program_irq - program interrupts
275  * @candev: Pointer to candevice/board structure
276  *
277  * The function ns_dev_program_irq() is used for hardware that uses 
278  * programmable interrupts. If your hardware doesn't use programmable interrupts
279  * you should not set the @candevices_t->flags entry to %CANDEV_PROGRAMMABLE_IRQ and 
280  * leave this function unedited. Again this function is hardware specific so 
281  * there's no example code.
282  * Return value: The function returns zero on success or %-ENODEV on failure
283  * File: src/template.c
284  */
285 int ns_dev_program_irq(struct candevice_t *candev)
286 {
287         return 0;
288 }
289
290 int ns_dev_register(struct hwspecops_t *hwspecops)
291 {
292         hwspecops->request_io = ns_dev_request_io;
293         hwspecops->release_io = ns_dev_release_io;
294         hwspecops->reset = ns_dev_reset;
295         hwspecops->init_hw_data = ns_dev_init_hw_data;
296         hwspecops->init_chip_data = ns_dev_init_chip_data;
297         hwspecops->init_obj_data = ns_dev_init_obj_data;
298         hwspecops->write_register = ns_dev_write_register;
299         hwspecops->read_register = ns_dev_read_register;
300         hwspecops->program_irq = ns_dev_program_irq;
301         return 0;
302 }