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
15 #include <linux/delay.h>
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"
24 * IO range for the C_CAN 1.2 memory map is 0x100 (256bytes)
26 #define IO_RANGE 0x100
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
34 int ns_dev_request_io(struct candevice_t *candev)
36 /* Note hard-coded index for the chip number as this
37 * only supports a single instance of the C_CAN controller.
39 DEBUGMSG("(c%d)ns_dev_request_io (...)\n", candev->chip[0]->chip_idx);
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);
46 if (!(candev->dev_base_addr = (long)ioremap(candev->io_addr, IO_RANGE))) {
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);
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);
63 candev->chip[0]->chip_base_addr = candev->dev_base_addr;
64 candev->chip[0]->chipspecops->start_chip(candev->chip[0]);
70 * ns_dev_release_io - free reserved io memory range
71 * @candev: pointer to candevice/board which releases io
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.
77 int ns_dev_release_io(struct candevice_t *candev)
82 DEBUGMSG("(c%d)ns_dev_release_io (...)\n", candev->chip[0]->chip_idx);
84 /* Release I/O memory mapping */
85 iounmap((void *)candev->dev_base_addr);
87 /* Release the memory region */
88 can_release_mem_region(candev->io_addr, IO_RANGE);
94 * ns_dev_reset - hardware reset routine
95 * @card: Number of the hardware card.
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
103 int ns_dev_reset(struct candevice_t *candev)
109 struct canchip_t *pchip = candev->chip[0];
111 enableTest = pchip->chipspecops->enable_configuration(pchip);
112 disableTest = pchip->chipspecops->disable_configuration(pchip);
114 if (enableTest || disableTest) {
115 CANMSG("Enable or Disable status failed!\n");
116 CANMSG("Please check your hardware.\n");
120 /* Check busoff status */
121 while ((c_can_read_reg_w(pchip, CCSR) & SR_BOFF) && (i <= 15)) {
127 CANMSG("Reset status timeout!\n");
128 CANMSG("Please check your hardware.\n");
131 //pchip->config_irqs(pchip, CR_MIE | CR_SIE | CR_EIE);
135 #define RESET_ADDR 0x0
140 * ns_dev_hw_data - Initialize hardware cards
141 * @candev: Pointer to candevice/board structure
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
149 int ns_dev_init_hw_data(struct candevice_t *candev)
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");
159 DEBUGMSG("Writing to NS9750 sys cont\n");
160 writel((BUS_WIDTH_16BIT | ACTIVE_LOW_CHIP_SELECT),
161 sys_contVA + NS9750_SYSTEM_CONTROLLER_OFFSET);
164 /* We have finished with this mapping */
165 iounmap((void *)sys_contVA);
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;
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
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
189 int ns_dev_init_chip_data(struct candevice_t *candev, int chipnr)
191 /* Register chip operations */
192 c_can_fill_chipspecops(candev->chip[chipnr]);
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;
203 * ns_dev_init_obj_data - Initialize message buffers
204 * @chip: Pointer to chip specific structure
205 * @objnr: Number of the message buffer
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
210 * The entry @obj_base_addr represents the first memory address of the message
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
216 int ns_dev_init_obj_data(struct canchip_t *chip, int objnr)
219 DEBUGMSG("(c%d)calling ns_dev_init_obj_data( ...)\n", chip->chip_idx);
221 /* It seems, that there is no purpose to setup object base address */
222 chip->msgobj[objnr]->obj_base_addr = 0;
224 /*can_msgobj_test_fl(pmsgobj,RX_MODE_EXT); */
229 * ns_dev_write_register - Low level write register routine
230 * @data: data to be written
231 * @address: memory address to write to
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
239 void ns_dev_write_register(unsigned data, unsigned long address)
242 //unsigned long usecs = 1;
244 writew(data, address);
246 for (i = 0; i < 5; i++) ;
250 * ns_dev_read_register - Low level read register routine
251 * @address: memory address to read from
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
259 unsigned ns_dev_read_register(unsigned long address)
263 value = readw(address);
265 for (i = 0; i < 5; i++) ;
266 value = readw(address);
268 for (i = 0; i < 5; i++) ;
274 * ns_dev_program_irq - program interrupts
275 * @candev: Pointer to candevice/board structure
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
285 int ns_dev_program_irq(struct candevice_t *candev)
290 int ns_dev_register(struct hwspecops_t *hwspecops)
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;