MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / spi / mcf_qspi.c
blob95647a1edb004da781018db677e73c181e611da1
1 /************************************************************************/
2 /* */
3 /* mcf_qspi.c - QSPI driver for MCF5272, MCF5235, MCF5282 */
4 /* */
5 /* (C) Copyright 2001, Wayne Roberts (wroberts1@home.com) */
6 /* */
7 /* Driver has an 8bit mode, and a 16bit mode. */
8 /* Transfer size QMR[BITS] is set thru QSPIIOCS_BITS. */
9 /* When size is 8, driver works normally: */
10 /* a char is sent for every transfer */
11 /* When size is 9 to 16bits, driver reads & writes the QDRs with */
12 /* the buffer cast to unsigned shorts. The QTR & QRR registers can */
13 /* be filled with up to 16bits. The length passed to read/write must */
14 /* be of the number of chars (2x number of shorts). This has been */
15 /* tested with 10bit a/d and d/a converters. */
16 /* */
17 /* * QSPIIOCS_READDATA: */
18 /* data to send out during read */
19 /* * all other ioctls are global */
20 /* -------------------------------------------------------------------- */
21 /* Ported to linux-2.4.x by Ron Fial (ron@fial.com) August 26,2002 */
22 /* */
23 /* Added new include files */
24 /* Added module_init(),exit(), */
25 /* qspi_read(),qspi_write(): Revised qspi_read & write argument */
26 /* processing to handle new *filep argument. Changed i_rdev access */
27 /* to use filep->f_dentry->d_inode->i_rdev Changed memcpy_fromfs() */
28 /* to memcpy(). */
29 /* Added '__init' to compiled-in init routine for memory recovery */
30 /* Added '__exit' for loadable-driver module cleanup routine */
31 /* changed register_chrdev to devfs_register_chrdev */
32 /* changed unregister_chrdev to devfs_unregister_chrdev */
33 /* Changed various declarations from int to ssize_t or loff_t */
34 /* -------------------------------------------------------------------- */
35 /* Changed interruptible_sleep_on to sleep_on so the driver has */
36 /* chance to finish the current transfer before application */
37 /* quits when typing '^C'. Otherwise a write collision will */
38 /* most likely occur. */
39 /* Added safe_flags(); cli; and restore_flags() according to */
40 /* gerg@snapgear.com. Otherwise in some cases (higher clock */
41 /* rates) the transfer is finished before the current process */
42 /* is put to sleep and therefore never wakes up again. */
43 /* 09/12/2002 richard@opentcp.org */
44 /* -------------------------------------------------------------------- */
45 /* 02/06/2003 josef.baumgartner@telex.de */
46 /* */
47 /* Renamed cleanup_module() to qspi_exit() to be able to */
48 /* compile as module. */
49 /* Removed init_module() because module_init(qspi_init) does all */
50 /* we need. */
51 /* Changed */
52 /* SPI register settings will be saved for each instance to be able */
53 /* to use different communication settings for different tasks. */
54 /* An ioctl() does not longer write directly to the SPI registers. */
55 /* It saves the settings which will be copied into the SPI */
56 /* registers on every read()/write(). */
57 /* Added MODULE_LICENSE("GPL") to avoid tainted kernel message. */
58 /* I think it is GPL?? There's no comment about this?? */
59 /* Added polling mode */
60 /* Increases performance for small data transfers. */
61 /* Added odd mode */
62 /* If an odd number of bytes is transfered and 16bit transfers are */
63 /* used, the last byte is transfered in byte mode. */
64 /* Added dsp mode */
65 /* If dsp mode is set, transfers will be limited to 15 bytes */
66 /* instead of 16. This ensures that DSPs with 24bit words get */
67 /* whole words within one transfer. */
68 /* -------------------------------------------------------------------- */
69 /* 16/09/2003 ivan.zanin@bluewin.ch */
70 /* */
71 /* Changed init and exit code to support the MCF5249 */
72 /* -------------------------------------------------------------------- */
73 /* Oct 19, 2004 jsujjavanich@syntech-fuelmaster.com */
74 /* */
75 /* Adjusted minor number detection to work with one dev per QSPI_CS */
76 /* -------------------------------------------------------------------- */
77 /* 17/11/2004 chris_jones_oz@yahoo.com.au */
78 /* */
79 /* Changed init and exit code to support the MCF5282 */
80 /* -------------------------------------------------------------------- */
81 /* -------------------------------------------------------------------- */
82 /* Feb 3, 2005 ngustavson@emacinc.com */
83 /* */
84 /* Modularized function calls support kernel2kernel calls */
85 /* -------------------------------------------------------------------- */
86 /************************************************************************/
90 /* **********************************************************************
91 Chapter 14. (excerpt) Queued Serial Peripheral Interface (QSPI) Module
92 From: http://e-www.motorola.com/brdata/PDFDB/docs/MCF5272UM.pdf
94 The following steps are necessary to set up the QSPI 12-bit data transfers
95 and a QSPI_CLK of 4.125 MHz. The QSPI RAM is set up for a queue of 16
96 transfers. All four QSPI_CS signals are used in this example.
98 1. Enable all QSPI_CS pins on the MCF5272. Write PACNT with 0x0080_4000 to
99 enable QSPI_CS1 and QSPI_CS3.Write PDCNT with 0x0000_0030 to enable QSPI_CS2.
101 2. Write the QMR with 0xB308 to set up 12-bit data words with the data
102 shifted on the falling clock edge, and a clock frequency of 4.125 MHz
103 (assuming a 66-MHz CLKIN).
105 3. Write QDLYR with the desired delays.
107 4. Write QIR with 0xD00F to enable write collision, abort bus errors, and
108 clear any interrupts.
110 5. Write QAR with 0x0020 to select the first command RAM entry.
112 6. Write QDR with 0x7E00, 0x7E00, 0x7E00, 0x7E00, 0x7D00, 0x7D00, 0x7D00,
113 0x7D00, 0x7B00, 0x7B00, 0x7B00, 0x7B00, 0x7700, 0x7700, 0x7700, and 0x7700
114 to set up four transfers for each chip select. The chip selects are active
115 low in this example. NOTE: QDR value auto-increments after each write.
117 7. Write QAR with 0x0000 to select the first transmit RAM entry.
119 8. Write QDR with sixteen 12-bit words of data.
121 9. Write QWR with 0x0F00 to set up a queue beginning at entry 0 and ending
122 at entry 15.
124 10. Set QDLYR[SPE] to enable the transfers.
126 11.Wait until the transfers are complete. QIR[SPIF] is set when the
127 transfers are complete.
129 12. Write QAR with 0x0010 to select the first receive RAM entry.
131 13. Read QDR to get the received data for each transfer. NOTE: QDR
132 auto-increments.
134 14. Repeat steps 5 through 13 to do another transfer.
136 ************************************************************************* */
139 * Includes
140 *-------------------------------------------------------------------------------
142 #define EXPORT_SYMTAB
144 #include <asm/coldfire.h> /* gets us MCF_MBAR value */
145 #include <asm/mcfsim.h> /* MCFSIM offsets */
146 #include <asm/semaphore.h>
147 #include <asm/system.h> /* cli() and friends */
148 #include <asm/uaccess.h>
149 #include <linux/devfs_fs_kernel.h>
150 #include <linux/errno.h>
151 #include <linux/fs.h>
152 #include <linux/init.h>
153 #include <linux/kernel.h>
154 #include <linux/module.h>
155 #include <linux/sched.h>
156 #include <linux/slab.h>
157 #include <linux/types.h>
158 #include <linux/version.h>
159 #include <linux/wait.h>
160 #include <linux/interrupt.h>
162 /* Include versioning info, if needed */
163 #if (defined(MODULE) && defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS))
164 #define MODVERSIONS
165 #endif
167 #if defined(MODVERSIONS)
168 #include <linux/modversions.h>
169 #endif
171 #include <asm/mcf_qspi.h>
173 int __init qspi_init(void);
174 static int init(void);
175 void __exit qspi_exit(void);
179 * Declarations and global variables
180 *-------------------------------------------------------------------------------
182 #define DEVICE_NAME "qspi"
184 MODULE_LICENSE("GPL");
185 /* struct wait_queue *wqueue; */
186 static DECLARE_WAIT_QUEUE_HEAD(wqueue); /* use ver 2.4 static declaration - ron */
187 /* or should we use wait_queue_heat_t *wqueue ?? see page 141 */
189 static unsigned char dbuf[1024];
191 /* static struct semaphore sem = MUTEX; */
193 static DECLARE_MUTEX(sem);
198 * EXPORTED symbols
199 *-------------------------------------------------------------------------------
201 EXPORT_SYMBOL(qspi_mutex_down);
202 EXPORT_SYMBOL(qspi_mutex_up);
203 EXPORT_SYMBOL(qspi_create_device);
204 EXPORT_SYMBOL(qspi_destroy_device);
205 EXPORT_SYMBOL(qspi_internal_read);
206 EXPORT_SYMBOL(qspi_internal_write);
207 EXPORT_SYMBOL(qspi_control);
208 EXPORT_SYMBOL(qspi_BAUD);
211 * Kernel function calls (EXPORTED)
212 * These functions provide the interface to the hardware.
213 * They may only be called from Kernel space.
214 * To use them directly from user space, the standard legacy char driver
215 * API has been maintained in wrapper functions.
216 * -----------------------------------------------------------------------------
221 * qspi_mutex_down.
222 * get in line for the qspi mutex
223 * the internal kernel calls do not hold the mutex themselves and so down/up
224 * must be called manually. This introduces a new level of complexity,
225 * but is required, as it may be necessary for some drivers to
226 * hold the mutex through more than one transaction.
228 void qspi_mutex_down(void){
229 down_interruptible(&sem);
232 * qspi_mutex up
233 * signal the qspi mutex.
234 * see qspi_mutex_down
236 void qspi_mutex_up(void){
237 up(&sem);
241 * qspi_create_device
242 * Create a QSPI device.
243 * Configuration information is stored in this device structure, which
244 * is used by the read and write calls, to dynamically change the SPI's
245 * configuration(bitrate, CPOL, etc...)
246 * The elements of the structure are modifyable via control calls.
247 * This function allocates the space for the device with Kmalloc, so if
248 * the device is not destroyed(qspi_destroy_device, it will result in a
249 * memory leak.
250 * @return a newly allocated and initialized qspi device
252 qspi_dev *qspi_create_device(void){
253 qspi_dev *dev;
255 if ((dev = kmalloc(sizeof(qspi_dev), GFP_KERNEL)) == NULL) {
256 return(NULL);
259 /* set default values */
260 dev->read_data.length = 0;
261 dev->read_data.buf = NULL;
262 dev->read_data.loop = 0;
263 dev->poll_mod = 0; /* interrupt mode */
264 dev->bits = 8;
265 dev->baud = qspi_BAUD(DEFAULT_BIT_RATE);
266 dev->cpol = 0;
267 dev->cpha = 0;
268 dev->qcr_cont = 1;
269 dev->dsp_mod = 0; /* no DSP mode */
270 dev->odd_mod = 0; /* no ODD mode */
271 dev->qcd = 17;
272 dev->dtl = 1;
274 return(dev);
277 * qspi_destroy device
278 * free a previously created qspi device
279 * @param device the device to destroy
281 int qspi_destroy_device(qspi_dev *device){
282 kfree(device);
283 return 0;
287 * qspi_internal_read
288 * Read a block of SPI data into an array
289 * SPI mode is set at the beginning of the transfer to be whatever is
290 * stored in the device structure
291 * This function is not thread safe, qspi_mutex_up/down should be used.
292 * @param dev device node containing transfer mode information
293 * @buffer kernel space array to transfer the data into
294 * @length amount of data to transfer should not be >sizeof(buffer)
295 * @off unused
296 * @qcr_cs slave select bitmap to use, ie. 3 would toggle SS0 and 1
298 ssize_t qspi_internal_read(qspi_dev *dev,char *buffer, size_t length,
299 loff_t *off,int qcr_cs){
300 int total = 0;
301 int i = 0;
302 int max_trans;
303 unsigned char bits;
304 unsigned char word = 0;
305 //unsigned long flag;
306 int rdi = 0;
308 /* set the register with default values */
309 QMR = QMR_MSTR |
310 (dev->dohie << 14) |
311 (dev->bits << 10) |
312 (dev->cpol << 9) |
313 (dev->cpha << 8) |
314 (dev->baud);
316 QDLYR = (dev->qcd << 8) | dev->dtl;
318 if (dev->dsp_mod)
319 max_trans = 15;
320 else
321 max_trans = 16;
324 bits = dev->bits % 0x10;
325 if (bits == 0 || bits > 0x08)
326 word = 1; /* 9 to 16bit transfers */
328 //printk("\n READ driver -- ioctl xmit data fm dev->read_data.buf array %x %x %x %x \n",dev->read_data.buf[0],dev->read_data.buf[1],dev->read_data.buf[2],dev->read_data.buf[3]);
330 while (i < length) {
331 unsigned short *sp = (unsigned short *)&buffer[i];
332 unsigned char *cp = &buffer[i];
333 unsigned short *rd_sp = (unsigned short *)dev->read_data.buf;
334 int x;
335 int n;
337 QAR = TX_RAM_START; /* address first QTR */
338 QSPIDEBUG("writing from read buffer ");
339 for (n = 0; n < max_trans; n++) {
340 if (rdi != -1) {
341 if (word) {
342 QDR = rd_sp[rdi++];
343 if (rdi == dev->read_data.length >> 1)
344 rdi = dev->read_data.loop ? 0 : -1;
345 } else {
346 QSPIDEBUG("%x ",dev->read_data.buf[rdi]);
347 QDR = dev->read_data.buf[rdi++];
348 if (rdi == dev->read_data.length)
349 rdi = dev->read_data.loop ? 0 : -1;
351 } else
352 QDR = 0;
354 i++;
355 if (word)
356 i++;
357 if (i > length)
358 break;
360 QSPIDEBUG("\n");
363 QAR = COMMAND_RAM_START; /* address first QCR */
364 for (x = 0; x < n; x++) {
365 /* QCR write */
366 if (dev->qcr_cont) {
367 if (x == n - 1 && i == length)
368 QDR = QCR_SETUP | qcr_cs; /* last transfer */
369 else
370 QDR = QCR_CONT | QCR_SETUP | qcr_cs;
371 } else
372 QDR = QCR_SETUP | qcr_cs;
375 QWR = QWR_CSIV | ((n - 1) << 8);
377 /* check if we are using polling mode. Polling increases
378 * performance for small data transfers but is dangerous
379 * if we stay too long here, locking other tasks!!
381 if (dev->poll_mod) {
382 QIR = QIR_SETUP_POLL;
383 QDLYR |= QDLYR_SPE;
385 while ((QIR & QIR_SPIF) != QIR_SPIF)
387 QIR = QIR | QIR_SPIF;
388 } else {
389 QIR = QIR_SETUP;
390 //save_flags(flag); cli(); // like in write function - don't think we need this
392 QDLYR |= QDLYR_SPE;
393 // interruptible_sleep_on(&wqueue);
394 sleep_on(&wqueue); // changed richard@opentcp.org
395 //restore_flags(flag); // like in write function
399 QAR = RX_RAM_START; /* address: first QRR */
400 if (word) {
401 /* 9 to 16bit transfers */
402 for (x = 0; x < n; x++) {
403 *sp = *(volatile unsigned short *)(MCF_MBAR + MCFSIM_QDR);
404 sp++;
406 } else {
407 /* 8bit transfers */
408 QSPIDEBUG("8 bit read: ");
409 for (x = 0; x < n; x++){
410 *cp = *(volatile unsigned short *)(MCF_MBAR + MCFSIM_QDR);
411 QSPIDEBUG("%x ",*cp);
412 cp++;
413 }QSPIDEBUG("\n");
415 if (word)
416 n <<= 1;
418 total += n;
421 return(total);
425 * qspi_internal_write
426 * write an array of data to the queued SPI bus
427 * like qspi_internal_read, hardware configuration
428 * is done at the beginning of this routine.
429 * This function is not thread safe, qspi_mutex_up/down should be used.
430 * @param dev device node containing transfer mode information
431 * @buffer kernel space array containing the data to send
432 * @length amount of data to transfer should not be >sizeof(buffer)
433 * @off unused
434 * @qcr_cs slave select bitmap to use, ie. 3 would toggle SS0 and 1
435 * @return 0 on success
437 ssize_t qspi_internal_write(qspi_dev *dev, const char *buffer, size_t length,
438 loff_t *off,int qcr_cs){
439 int i = 0;
440 int total = 0;
441 int z;
442 int max_trans;
443 unsigned char bits;
444 unsigned char word = 0;
445 //unsigned long flag;
448 QMR = QMR_MSTR |
449 (dev->dohie << 14) |
450 (dev->bits << 10) |
451 (dev->cpol << 9) |
452 (dev->cpha << 8) |
453 (dev->baud);
455 QDLYR = (dev->qcd << 8) | dev->dtl;
457 bits = (QMR >> 10) % 0x10;
458 if (bits == 0 || bits > 0x08)
459 word = 1; /* 9 to 16 bit transfers */
462 /* next line was memcpy_fromfs() */
464 QSPIDEBUG("length = %x \n",length);
465 //printk("data to write is %x %x %x %X \n",dbuf[0],dbuf[1],dbuf[2],dbuf[3]);
467 if (dev->odd_mod)
468 z = QCR_SETUP8;
469 else
470 z = QCR_SETUP;
472 if (dev->dsp_mod)
473 max_trans = 15;
474 else
475 max_trans = 16;
477 while (i < length) {
478 int x;
479 int n;
481 QAR = TX_RAM_START; /* address: first QTR */
482 if (word) {
483 for (n = 0; n < max_trans; ) {
484 /* in odd mode last byte will be transfered in byte mode */
485 if (dev->odd_mod && (i + 1 == length)) {
486 QDR = buffer[i]; /* tx data: QDR write */
487 QSPIDEBUG("0x%X ", dbuf[i]);
488 n++;
489 i++;
490 break;
492 else {
493 QDR = (buffer[i] << 8) + buffer[i+1]; /* tx data: QDR write */
494 QSPIDEBUG("0x%X 0x%X ", dbuf[i], dbuf[i+1]);
495 n++;
496 i += 2;
497 if (i >= length)
498 break;
501 } else {
502 /* 8bit transfers */
503 QSPIDEBUG("8 bit write : ");
504 for (n = 0; n < max_trans; ) {
505 QSPIDEBUG("%x ",buffer[i]);
506 QDR = buffer[i]; /* tx data: QTR write */
507 n++;
508 i++;
509 if (i == length)
510 break;
512 QSPIDEBUG("\n");
515 QAR = COMMAND_RAM_START; /* address: first QCR */
516 for (x = 0; x < n; x++) {
517 /* QCR write */
518 if (dev->qcr_cont) {
519 if (x == n-1 && i == length)
520 if ((i % 2)!= 0)
521 QDR = z | qcr_cs; /* last transfer and odd number of chars */
522 else
523 QDR = QCR_SETUP | qcr_cs; /* last transfer */
524 else
525 QDR = QCR_CONT | QCR_SETUP | qcr_cs;
526 } else {
527 if (x == n - 1 && i == length)
528 QDR = z | qcr_cs; /* last transfer */
529 else
530 QDR = QCR_SETUP | qcr_cs;
534 QWR = QWR_CSIV | ((n - 1) << 8); /* QWR[ENDQP] = n << 8 */
536 /* check if we are using polling mode. Polling increases
537 * performance for small data transfers but is dangerous
538 * if we stay too long here, locking other tasks!!
540 if (dev->poll_mod) {
541 QIR = QIR_SETUP_POLL;
542 QDLYR |= QDLYR_SPE;
544 while ((QIR & QIR_SPIF) != QIR_SPIF)
546 QIR = QIR | QIR_SPIF;
547 } else {
548 QIR = QIR_SETUP;
549 // save_flags(flag); cli(); // added according to gerg@snapgear.com - handled by mutex?
550 QDLYR |= QDLYR_SPE;
552 // interruptible_sleep_on(&wqueue);
553 sleep_on(&wqueue); // changed richard@opentcp.org
555 //restore_flags(flag); // added according to gerg@snapgear.com
559 if (word)
560 n <<= 1;
562 total += n;
565 return(total);
569 * qspi_control
570 * Set configuration bits in the device structure or create an array
571 * of data to transfer during the next tranceive operation.
572 * This function does not actually interface with any hardware, it is used
573 * only for modifying the device structure. From kernel space this could
574 * be done more efficiently by directly modifying the structure.
575 * This function is not thread safe, qspi_mutex_up/down should be used.
576 * @param dev device node containing transfer mode information
577 * @param cmd IOCTL command see mcf_qspi.h
578 * @param arg IOCTL arg
579 * @return 0 on success
581 int qspi_control(qspi_dev *dev, unsigned int cmd, unsigned long arg){
582 int ret = 0;
583 struct qspi_read_data *read_data;
586 switch (cmd) {
587 /* Set QMR[DOHIE] (high-z Dout between transfers) */
588 case QSPIIOCS_DOUT_HIZ:
589 dev->dohie = (arg ? 1 : 0);
590 break;
592 /* Set QMR[BITS] */
593 case QSPIIOCS_BITS:
594 if (((arg > 0) && (arg < 8)) || (arg > 16)) {
595 ret = -EINVAL;
596 break;
599 dev->bits = (u8)arg;
600 break;
602 /* Get QMR[BITS] */
603 case QSPIIOCG_BITS:
604 *((int *)arg) = dev->bits;
605 break;
607 /* Set QMR[CPOL] (QSPI_CLK inactive state) */
608 case QSPIIOCS_CPOL:
609 dev->cpol = (arg ? 1 : 0);
610 break;
612 /* Set QMR[CPHA] (QSPI_CLK phase, 1 = rising edge) */
613 case QSPIIOCS_CPHA:
614 dev->cpha = (arg ? 1 : 0);
615 break;
617 /* Set QMR[BAUD] (QSPI_CLK baud rate divisor) */
618 case QSPIIOCS_BAUD:
619 if (arg > 255) {
620 ret = -EINVAL;
621 break;
624 dev->baud = (u8)arg;
625 break;
627 /* Set QDR[QCD] (QSPI_CS to QSPI_CLK setup) */
628 case QSPIIOCS_QCD:
629 if (arg > 127) {
630 ret = -EINVAL;
631 break;
634 dev->qcd = (u8)arg;
635 break;
637 /* Set QDR[DTL] (QSPI_CLK to QSPI_CS hold) */
638 case QSPIIOCS_DTL:
639 if (arg > 255) {
640 ret = -EINVAL;
641 break;
644 dev->dtl = (u8)arg;
645 break;
647 /* Set QCRn[CONT] (QSPI_CS continuous mode, 1 = remain
648 * asserted after transfer of 16 data words) */
649 case QSPIIOCS_CONT:
650 dev->qcr_cont = (arg ? 1 : 0);
651 break;
653 /* Set DSP mode, used to limit transfers to 15 bytes for
654 * 24-bit DSPs */
655 case QSPIIOCS_DSP_MOD:
656 dev->dsp_mod = (arg ? 1 : 0);
657 break;
659 /* If an odd count of bytes is transferred, force the transfer
660 * of the last byte to byte mode, even if word mode is used */
661 case QSPIIOCS_ODD_MOD:
662 dev->odd_mod = (arg ? 1 : 0);
663 break;
665 /* Set driver to use polling mode, which may increase
666 * performance for small transfers */
667 case QSPIIOCS_POLL_MOD:
668 dev->poll_mod = (arg ? 1 : 0);
669 break;
672 /* Kernel space copy function
673 * Set data buffer to be used as "send data" during reads
674 * */
675 case QSPIIOCS_READDATA:
676 read_data = (struct qspi_read_data *)arg;
677 dev->read_data.length = read_data->length;
678 dev->read_data.buf = read_data->buf;
679 dev->read_data.loop = read_data->loop;
680 break;
683 default:
684 ret = -EINVAL;
685 break;
689 return ret;
693 * Perform baud rate calculation for QMR register
694 *@param desired integer setting for desired baud rate
695 *@returns divider setting for QMR, -1 for invalid settings
697 u16 qspi_BAUD(int desired){
698 u16 baud = MCF_CLK/(2*desired);
699 if((baud>QMR_BAUD)||(baud<2))
700 return -1;//invalid setting
701 return baud;
704 /*------------------------------------------------------------------------------
705 * End Kernal function calls
711 * qspi_interrupt
712 * hooked at initialization.
713 * This logs error messages with printk and wakes up a process on
714 * the waitqueue.
716 #ifdef LINUX26
717 static irqreturn_t qspi_interrupt(int irq, void *dev_id, struct pt_regs *regs)
718 #else
719 static void qspi_interrupt(int irq, void *dev_id, struct pt_regs *regs)
720 #endif
722 u16 qir = (QIR & (QIR_WCEF | QIR_ABRT | QIR_SPIF));
724 /* Check write collision and transfer abort flags. Report any
725 * goofiness. */
726 if (qir & QIR_WCEF)
727 printk(KERN_INFO "%s: WCEF\n", __FILE__);
729 if (qir & QIR_ABRT)
730 printk(KERN_INFO "%s: ABRT\n", __FILE__);
732 /* Check for completed transfer. Wake any tasks sleeping on our
733 * global wait queue. */
734 if (qir & QIR_SPIF)
735 wake_up(&wqueue);
737 /* Clear any set flags. */
738 QIR |= qir;
740 #ifdef LINUX26
741 return IRQ_RETVAL(1);
742 #endif
746 * qspi_ioctl
747 * Standard ioctl system call for qspi character devices
748 * calls qspi_control protected by the qspi mutex.
750 static int qspi_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
751 unsigned long arg){
752 struct qspi_dev *dev = filp->private_data;
753 qspi_read_data *read_data;
754 int error;
755 int ret;
756 int length;
757 __u8 *kbuffer;
758 __u8 *ubuffer;
759 int i;
761 QSPIDEBUG("qspi ioctl\n");
763 /* User space copy function
764 * Set data buffer to be used as "send data" during reads
765 * */
766 if(cmd==QSPIIOCS_READDATA){
767 QSPIDEBUG("READDATA set\n");
768 read_data = (struct qspi_read_data *)arg;
770 error = verify_area(VERIFY_READ, read_data,
771 sizeof(struct qspi_read_data));
773 error = access_ok(VERIFY_READ, (void *)arg ,
774 sizeof(struct qspi_read_data));
776 if (!error) {
777 printk("couldn't read from %lx\n",(unsigned long) read_data);
778 return error;
780 //store old data buffer location
781 kbuffer = dev->read_data.buf;
782 //copy in user structure
783 copy_from_user(&dev->read_data, read_data,sizeof(struct qspi_read_data));
784 //get user data location
785 ubuffer = dev->read_data.buf;
786 length = dev->read_data.length;
787 dev->read_data.buf = kbuffer;//reset kernel pointer
789 /*make sure user buffer is <= kbuffer size*/
790 if(length>QSPIBSZ)
791 return(-EINVAL);
792 /*get user data*/
793 copy_from_user(kbuffer, ubuffer,length);
794 QSPIDEBUG("copied over : ");
795 for(i=0;i<length;i++)
796 QSPIDEBUG("%x ",dev->read_data.buf[i]);
797 QSPIDEBUG("\n");
798 return 0;
800 //use qspi_control if we don't have to map anything from user space
801 down_interruptible(&sem);
802 ret = qspi_control(dev,cmd,arg);
803 up(&sem);
804 return ret;
808 * qspi_open
809 * Standard open system call for character qspi devices
810 * creates a qspi device in private data
812 static int qspi_open(struct inode *inode, struct file *file){
814 qspi_dev *device = qspi_create_device();
815 if(device==NULL)
816 return(-ENOMEM);
818 if ((device->read_data.buf = kmalloc(QSPIBSZ, GFP_KERNEL)) == NULL) {
819 qspi_destroy_device(device);
820 return(-ENOMEM);
822 file->private_data = device;
824 return(0);
829 * qspi_release
830 * Standard close system call for character qspi devices
831 * destroys the qspi device in private data
833 static int qspi_release(struct inode *inode, struct file *file)
835 qspi_destroy_device(file->private_data);
836 file->private_data = NULL;
837 return(0);
841 * qspi_read
842 * Standard read system call for character qspi devices
843 * gets cs number from device minor.
844 * doesn't implement the offset arguement
845 * calls qspi_internal_read protected by the qspi mutex
847 static ssize_t qspi_read(struct file *filep, char *buffer, size_t length,
848 loff_t *off){
849 /* CS for QCR */
850 int qcr_cs = (~MINOR(filep->f_dentry->d_inode->i_rdev) << 8) & 0xf00;
851 int ret;
852 qspi_dev *dev = filep->private_data;
853 down_interruptible(&sem);
854 ret = qspi_internal_read(dev, dbuf, length,off,qcr_cs);
855 up(&sem);
857 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
858 memcpy_tofs(buffer, dbuf,length);
859 #else
860 copy_to_user(buffer, dbuf,length);
861 #endif
862 return ret;
866 * qspi_write
867 * Standard write system call for character qspi devices
868 * gets cs number from device number
869 * doesn't implement the offset arguement
870 * calls qspi_internal_write protected by the qspi mutex
872 static ssize_t qspi_write(struct file *filep, const char *buffer, size_t length,
873 loff_t *off){
874 int ret;
875 qspi_dev *dev= filep->private_data;
876 int qcr_cs = (~MINOR(filep->f_dentry->d_inode->i_rdev) << 8) & 0xf00; /* CS for QCR */
877 if(length>sizeof(dbuf))
878 return -ENOMEM;
880 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
881 memcpy_fromfs(dbuf, buffer,length);
882 #else
883 copy_from_user (dbuf, buffer, length);
884 #endif
885 down_interruptible(&sem);
886 ret = qspi_internal_write(dev,dbuf,length, off,qcr_cs);
887 up(&sem);
888 return ret;
892 * qspi character file operations structure
894 static struct file_operations Fops = {
895 owner: THIS_MODULE,
896 read: qspi_read,
897 write: qspi_write,
898 ioctl: qspi_ioctl,
899 open: qspi_open,
900 release: qspi_release /* a.k.a. close */
905 * init
906 * module initialization function.
907 * hooks the interrupt and initializes the hardware
909 static int init(void)
911 volatile u32 *lp;
912 volatile u8 *cp;
914 /* common init: driver or module: */
916 if (request_irq(MCFQSPI_IRQ_VECTOR, qspi_interrupt, SA_INTERRUPT, "ColdFire QSPI", NULL)) {
917 printk("QSPI: Unable to attach ColdFire QSPI interrupt "
918 "vector=%d\n", MCFQSPI_IRQ_VECTOR);
919 return(-EINVAL);
922 #if defined(CONFIG_M5249)
923 cp = (volatile u8 *)(MCF_MBAR + MCFSIM_ICR10);
924 *cp = 0x8f; /* autovector on, il=3, ip=3 */
926 lp = (volatile u32 *)(MCF_MBAR2 + 0x180);
927 *lp |= 0x00000800; /* activate qspi_in and qspi_clk */
929 lp = (volatile u32 *)(MCF_MBAR2 + MCFSIM2_GPIOFUNC);
930 *lp &= 0xdc9FFFFF; /* activate qspi_cs0 .. 3, qspi_dout */
932 lp = (volatile u32 *)(MCF_MBAR + MCFSIM_IMR);
933 *lp &= 0xFFFbFFFF; /* enable qspi interrupt */
934 #elif defined(CONFIG_M5235)
935 // interrupts mask here
937 volatile unsigned char *icrp;
938 icrp = (volatile unsigned char *)(MCF_MBAR + MCF5235ICM_INTC0 + MCFINTC0_ICR);
939 icrp[IRQ_SOURCE] = (( 3/*IL*/ & 0x3 ) << 3 ) | (3 /*IP*/ & 0x3);
942 volatile unsigned int *imrl;
943 imrl = (volatile unsigned int *)(MCF_MBAR + MCF5235ICM_INTC0 + MCF5235INTC_IMRL);
944 *imrl &= ~(1 << IRQ_SOURCE);
946 // GPIO here
948 volatile unsigned char *parp;
949 parp = (volatile unsigned char *)(MCF_MBAR + 0x10004A);
950 *parp = 0xFF;
952 #elif (defined(CONFIG_M5282) || defined(CONFIG_M5280)|| defined(CONFIG_M528x))
953 cp = (volatile u8 *) (MCF_IPSBAR + MCFICM_INTC0 + MCFINTC_ICR0 +
954 MCFINT_QSPI);
956 *cp = (5 << 3) + 3; /* level 5, priority 3 */
959 #ifdef CONFIG_SOM5282EM
960 cp = (volatile u8 *) (MCF_IPSBAR + MCF5282_GPIO_PQSPAR);
961 *cp = 0x3f; /* activate din, dout, clk and cs[0..2] // RTC has reverse polarity */
962 #else
963 cp = (volatile u8 *) (MCF_IPSBAR + MCF5282_GPIO_PQSPAR);
964 *cp = 0x7f; /* activate din, dout, clk and cs[0..3] */
965 #endif
966 lp = (volatile u32 *) (MCF_IPSBAR + MCFICM_INTC0 + MCFINTC_IMRL);
967 *lp &= ~(1 + (1 << MCFINT_QSPI)); /* enable qspi interrupt */
968 #else
969 /* set our IPL */
970 lp = (volatile u32 *)(MCF_MBAR + MCFSIM_ICR4);
971 *lp = (*lp & 0x07777777) | 0xd0000000;
973 /* 1) CS pin setup 17.2.x
974 * Dout, clk, cs0 always enabled. Din, cs[3:1] must be enabled.
975 * CS1: PACNT[23:22] = 10
976 * CS1: PBCNT[23:22] = 10 ?
977 * CS2: PDCNT[05:04] = 11
978 * CS3: PACNT[15:14] = 01
980 lp = (volatile u32 *)(MCF_MBAR + MCFSIM_PACNT);
981 *lp = (*lp & 0xFF3F3FFF) | 0x00804000; /* 17.2.1 QSPI CS1 & CS3 */
982 lp = (volatile u32 *)(MCF_MBAR + MCFSIM_PDCNT);
983 *lp = (*lp & 0xFFFFFFCF) | 0x00000030; /* QSPI_CS2 */
984 #endif
987 * These values have to be setup according to the applications
988 * using the qspi driver. Maybe some #defines at the beginning
989 * would be more appropriate. Especially the transfer size
990 * and speed settings
992 QMR = 0xA1A2; // default mode setup: 8 bits, baud, 160kHz clk.
993 // QMR = 0x81A2; // default mode setup: 16 bits, baud, 160kHz clk.
994 QDLYR = 0x0202; // default start & end delays
996 init_waitqueue_head(&wqueue); /* was init_waitqueue() --Ron */
998 #if defined(CONFIG_M5249)
999 printk("MCF5249 QSPI driver ok\n");
1000 #elif defined(CONFIG_M5235)
1001 printk("MCF5235 QSPI driver ok\n");
1002 #elif (defined(CONFIG_M5282) || defined(CONFIG_M5280)|| defined(CONFIG_M528x))
1003 printk("MCF5282 QSPI driver ok\n");
1004 #else
1005 printk("MCF5272 QSPI driver ok\n");
1006 #endif
1008 return(0);
1014 * And the bus type.
1017 struct bus_type qspi_bus_type = {
1018 .name = "mcfqspi",
1019 .match = qspi_match,
1023 /* init for qspi module */
1024 int __init qspi_init(void) /* the __init added by ron */
1026 int ret;
1027 #ifdef DEVFS
1028 if ((ret = devfs_register_chrdev(QSPI_MAJOR, DEVICE_NAME, &Fops) < 0)) {
1029 printk ("%s device failed with %d\n",
1030 "Sorry, registering the character", ret);
1031 return(ret);
1033 #else
1035 /*register mcf_qspi driver as a bus*/
1036 //if(ret = bus_register(&qspi_bus_type))
1037 // return ret;
1039 if ((ret = register_chrdev(QSPI_MAJOR, DEVICE_NAME, &Fops) < 0)) {
1040 printk ("%s device failed with %d\n",
1041 "Sorry, registering the character", ret);
1042 return(ret);
1044 #endif
1047 printk ("QSPI device driver installed OK\n");
1048 return(init());
1051 /* Cleanup - undid whatever init_module did */
1052 void __exit qspi_exit(void) /* the __exit added by ron */
1054 int ret;
1056 free_irq(MCFQSPI_IRQ_VECTOR, NULL);
1058 #if defined(CONFIG_M5249)
1059 /* autovector on, il=0, ip=0 */
1060 *(volatile u8 *)(MCF_MBAR + MCFSIM_ICR10) = 0x80;
1061 /* disable qspi interrupt */
1062 *(volatile u32 *)(MCF_MBAR + MCFSIM_IMR) |= 0x00040000;
1063 #elif defined(CONFIG_M5235)
1065 volatile unsigned char *icrp;
1066 icrp = (volatile unsigned char *)(MCF_MBAR + MCF5235ICM_INTC0 + MCFINTC0_ICR);
1067 icrp[IRQ_SOURCE] = 0;
1069 // GPIO here
1071 volatile unsigned char *parp;
1072 parp = (volatile unsigned char *)(MCF_MBAR + 0x10004A);
1073 *parp = 0x00;
1075 #elif (defined(CONFIG_M5282) || defined(CONFIG_M5280) || defined(CONFIG_M528x))
1076 /* interrupt level 0, priority 0 */
1077 *(volatile u8 *) (MCF_IPSBAR + MCFICM_INTC0 +
1078 MCFINTC_ICR0 + MCFINT_QSPI) = 0;
1079 /* disable qspi interrupt */
1080 *(volatile u32 *) (MCF_IPSBAR + MCFICM_INTC0 + MCFINTC_IMRL)
1081 |= (1 << MCFINT_QSPI);
1082 #else
1083 /* zero our IPL */
1084 *((volatile u32 *)(MCF_MBAR + MCFSIM_ICR4)) = 0x80000000;
1085 #endif
1087 /* Unregister the device */
1088 //bus_unregister(&qspi_bus_type);
1090 #ifdef DEVFS
1091 if ((ret = devfs_unregister_chrdev(QSPI_MAJOR, DEVICE_NAME)) < 0)
1092 printk("Error in unregister_chrdev: %d\n", ret);
1094 #else
1095 if ((ret = unregister_chrdev(QSPI_MAJOR, DEVICE_NAME)) < 0)
1096 printk("Error in unregister_chrdev: %d\n", ret);
1098 #endif
1102 module_init(qspi_init); /* added by ron so driver can compile directly into kernel */
1103 module_exit(qspi_exit); /* added by ron so driver can compile directly into kernel */