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 = 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)
79 DEBUGMSG("(c%d)ns_dev_release_io (...)\n", candev->chip[0]->chip_idx);
81 /* Release I/O memory mapping */
82 iounmap(candev->dev_base_addr);
84 /* Release the memory region */
85 can_release_mem_region(candev->io_addr, IO_RANGE);
91 * ns_dev_reset - hardware reset routine
92 * @card: Number of the hardware card.
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
100 int ns_dev_reset(struct candevice_t *candev)
106 struct canchip_t *pchip = candev->chip[0];
108 enableTest = pchip->chipspecops->enable_configuration(pchip);
109 disableTest = pchip->chipspecops->disable_configuration(pchip);
111 if (enableTest || disableTest) {
112 CANMSG("Enable or Disable status failed!\n");
113 CANMSG("Please check your hardware.\n");
117 /* Check busoff status */
118 while ((c_can_read_reg_w(pchip, CCSR) & SR_BOFF) && (i <= 15)) {
124 CANMSG("Reset status timeout!\n");
125 CANMSG("Please check your hardware.\n");
128 //pchip->config_irqs(pchip, CR_MIE | CR_SIE | CR_EIE);
132 #define RESET_ADDR 0x0
137 * ns_dev_hw_data - Initialize hardware cards
138 * @candev: Pointer to candevice/board structure
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
146 int ns_dev_init_hw_data(struct candevice_t *candev)
148 can_ioptr_t sys_contVA = NULL;
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");
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);
161 /* We have finished with this mapping */
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;
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
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
186 int ns_dev_init_chip_data(struct candevice_t *candev, int chipnr)
188 /* Register chip operations */
189 c_can_fill_chipspecops(candev->chip[chipnr]);
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;
200 * ns_dev_init_obj_data - Initialize message buffers
201 * @chip: Pointer to chip specific structure
202 * @objnr: Number of the message buffer
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
207 * The entry @obj_base_addr represents the first memory address of the message
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
213 int ns_dev_init_obj_data(struct canchip_t *chip, int objnr)
216 DEBUGMSG("(c%d)calling ns_dev_init_obj_data( ...)\n", chip->chip_idx);
218 /* It seems, that there is no purpose to setup object base address */
219 chip->msgobj[objnr]->obj_base_addr = 0;
221 /*can_msgobj_test_fl(pmsgobj,RX_MODE_EXT); */
226 * ns_dev_write_register - Low level write register routine
227 * @data: data to be written
228 * @address: memory address to write to
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
236 void ns_dev_write_register(unsigned data, can_ioptr_t address)
239 //unsigned long usecs = 1;
241 can_writew(data, address);
243 for (i = 0; i < 5; i++) ;
247 * ns_dev_read_register - Low level read register routine
248 * @address: memory address to read from
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
256 unsigned ns_dev_read_register(can_ioptr_t address)
260 value = can_readw(address);
262 for (i = 0; i < 5; i++) ;
263 value = can_readw(address);
265 for (i = 0; i < 5; i++) ;
271 * ns_dev_program_irq - program interrupts
272 * @candev: Pointer to candevice/board structure
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
282 int ns_dev_program_irq(struct candevice_t *candev)
287 int ns_dev_register(struct hwspecops_t *hwspecops)
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;