3 * A driver to interface the 934 based sata core present in the ox820
5 * based on sata_oxnas driver by Ma Haijun <mahaijuns@gmail.com>
6 * based on ox820 sata code by:
7 * Copyright (c) 2007 Oxford Semiconductor Ltd.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/ata.h>
21 #include <linux/libata.h>
22 #include <linux/of_platform.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/of_address.h>
28 #include <linux/of_irq.h>
29 #include <linux/clk.h>
30 #include <linux/reset.h>
32 #include <mach/utils.h>
34 /* sgdma request structure */
35 struct sgdma_request
{
36 volatile u32 qualifier
;
40 } __packed
__aligned(4);
43 /* Controller information */
45 SATA_OXNAS_MAX_PRD
= 254,
46 SATA_OXNAS_DMA_SIZE
= SATA_OXNAS_MAX_PRD
*
47 sizeof(struct ata_bmdma_prd
) +
48 sizeof(struct sgdma_request
),
49 SATA_OXNAS_MAX_PORTS
= 2,
50 /** The different Oxsemi SATA core version numbers */
51 SATA_OXNAS_CORE_VERSION
= 0x1f3,
52 SATA_OXNAS_IRQ_FLAG
= IRQF_SHARED
,
53 SATA_OXNAS_HOST_FLAGS
= (ATA_FLAG_SATA
| ATA_FLAG_PIO_DMA
|
54 ATA_FLAG_NO_ATAPI
/*| ATA_FLAG_NCQ*/),
55 SATA_OXNAS_QUEUE_DEPTH
= 32,
57 SATA_OXNAS_DMA_BOUNDARY
= 0xFFFFFFFF,
65 /** sata host port register offsets */
84 /** These registers allow access to the link layer registers
85 that reside in a different clock domain to the processor bus */
98 /** sata port register bits */
101 * commands to issue in the master status to tell it to move shadow ,
102 * registers to the actual device ,
104 SATA_OPCODE_MASK
= 0x00000007,
105 CMD_WRITE_TO_ORB_REGS_NO_COMMAND
= 0x4,
106 CMD_WRITE_TO_ORB_REGS
= 0x2,
107 CMD_SYNC_ESCAPE
= 0x7,
108 CMD_CORE_BUSY
= (1 << 7),
109 CMD_DRIVE_SELECT_SHIFT
= 12,
110 CMD_DRIVE_SELECT_MASK
= (0xf << CMD_DRIVE_SELECT_SHIFT
),
112 /** interrupt bits */
113 INT_END_OF_CMD
= 1 << 0,
114 INT_LINK_SERROR
= 1 << 1,
116 INT_LINK_IRQ
= 1 << 3,
117 INT_REG_ACCESS_ERR
= 1 << 7,
118 INT_BIST_FIS
= 1 << 11,
119 INT_MASKABLE
= INT_END_OF_CMD
|
125 INT_WANT
= INT_END_OF_CMD
|
129 INT_ERRORS
= INT_LINK_SERROR
|
133 /** raw interrupt bits, unmaskable, but do not generate interrupts */
134 RAW_END_OF_CMD
= INT_END_OF_CMD
<< 16,
135 RAW_LINK_SERROR
= INT_LINK_SERROR
<< 16,
136 RAW_ERROR
= INT_ERROR
<< 16,
137 RAW_LINK_IRQ
= INT_LINK_IRQ
<< 16,
138 RAW_REG_ACCESS_ERR
= INT_REG_ACCESS_ERR
<< 16,
139 RAW_BIST_FIS
= INT_BIST_FIS
<< 16,
140 RAW_WANT
= INT_WANT
<< 16,
141 RAW_ERRORS
= INT_ERRORS
<< 16,
144 * variables to write to the device control register to set the current
145 * device, ie. master or slave.
150 SATA_CTL_ERR_MASK
= 0x00000016,
154 /* ATA SGDMA register offsets */
158 SGDMA_REQUESTPTR
= 0x8,
160 SGDMA_CORESIZE
= 0x10,
163 /* DMA controller register offsets */
168 DMA_CONTROL_RESET
= (1 << 12),
172 /* see DMA core docs for the values. Out means from memory (bus A) out
174 SGDMA_REQCTL0OUT
= 0x0497c03d,
175 /* burst mode disabled when no micro code used */
176 SGDMA_REQCTL0IN
= 0x0493a3c1,
177 SGDMA_REQCTL1OUT
= 0x0497c07d,
178 SGDMA_REQCTL1IN
= 0x0497a3c5,
179 SGDMA_CONTROL_NOGO
= 0x3e,
180 SGDMA_CONTROL_GO
= SGDMA_CONTROL_NOGO
| 1,
181 SGDMA_ERRORMASK
= 0x3f,
184 SGDMA_RESETS_CTRL
= 1 << 0,
185 SGDMA_RESETS_ARBT
= 1 << 1,
186 SGDMA_RESETS_AHB
= 1 << 2,
187 SGDMA_RESETS_ALL
= SGDMA_RESETS_CTRL
|
192 SGDMA_REQQUAL
= 0x00220001,
196 /** SATA core register offsets */
201 DATACOUNT_PORT0
= 0x010,
202 DATACOUNT_PORT1
= 0x014,
203 CORE_INT_STATUS
= 0x030,
204 CORE_INT_CLEAR
= 0x030,
205 CORE_INT_ENABLE
= 0x034,
206 CORE_INT_DISABLE
= 0x038,
207 CORE_REBUILD_ENABLE
= 0x050,
208 CORE_FAILED_PORT_R
= 0x054,
209 DEVICE_CONTROL
= 0x068,
211 RAID_SIZE_LOW
= 0x070,
212 RAID_SIZE_HIGH
= 0x074,
213 PORT_ERROR_MASK
= 0x078,
215 RAID_CONTROL
= 0x090,
216 DATA_PLANE_CTRL
= 0x0AC,
217 CORE_DATAPLANE_STAT
= 0x0b8,
222 UCODE_STORE
= 0x1000,
223 RAID_WP_BOT_LOW
= 0x1FF0,
224 RAID_WP_BOT_HIGH
= 0x1FF4,
225 RAID_WP_TOP_LOW
= 0x1FF8,
226 RAID_WP_TOP_HIGH
= 0x1FFC,
227 DATA_MUX_RAM0
= 0x8000,
228 DATA_MUX_RAM1
= 0xA000,
233 /* Sata core debug1 register bits */
234 CORE_PORT0_DATA_DIR_BIT
= 20,
235 CORE_PORT1_DATA_DIR_BIT
= 21,
236 CORE_PORT0_DATA_DIR
= 1 << CORE_PORT0_DATA_DIR_BIT
,
237 CORE_PORT1_DATA_DIR
= 1 << CORE_PORT1_DATA_DIR_BIT
,
239 /** sata core control register bits */
240 SCTL_CLR_ERR
= 0x00003016,
241 RAID_CLR_ERR
= 0x0000011e,
243 /* Interrupts direct from the ports */
244 NORMAL_INTS_WANTED
= 0x00000303,
246 /* shift these left by port number */
247 COREINT_HOST
= 0x00000001,
248 COREINT_END
= 0x00000100,
249 CORERAW_HOST
= COREINT_HOST
<< 16,
250 CORERAW_END
= COREINT_END
<< 16,
252 /* Interrupts from the RAID controller only */
253 RAID_INTS_WANTED
= 0x00008300,
255 /* The bits in the IDLE_STATUS that, when set indicate an idle core */
256 IDLE_CORES
= (1 << 18) | (1 << 19),
258 /* Data plane control error-mask mask and bit, these bit in the data
259 * plane control mask out errors from the ports that prevent the SGDMA
260 * care from sending an interrupt */
261 DPC_ERROR_MASK
= 0x00000300,
262 DPC_ERROR_MASK_BIT
= 0x00000100,
263 /* enable jbod micro-code */
264 DPC_JBOD_UCODE
= 1 << 0,
265 DPC_FIS_SWCH
= 1 << 1,
267 /** Device Control register bits */
268 DEVICE_CONTROL_DMABT
= 1 << 4,
269 DEVICE_CONTROL_ABORT
= 1 << 2,
270 DEVICE_CONTROL_PAD
= 1 << 3,
271 DEVICE_CONTROL_PADPAT
= 1 << 16,
272 DEVICE_CONTROL_PRTRST
= 1 << 8,
273 DEVICE_CONTROL_RAMRST
= 1 << 12,
274 DEVICE_CONTROL_ATA_ERR_OVERRIDE
= 1 << 28,
276 /** oxsemi HW raid modes */
277 OXNASSATA_NOTRAID
= 0,
280 /** OX820 specific HW-RAID register values */
284 CONFIG_IN_RESUME
= 2,
287 /* SATA PHY Registers */
294 STAT_READ_VALID
= (1 << 21),
295 STAT_CR_ACK
= (1 << 20),
296 STAT_CR_READ
= (1 << 19),
297 STAT_CR_WRITE
= (1 << 18),
298 STAT_CAP_DATA
= (1 << 17),
299 STAT_CAP_ADDR
= (1 << 16),
301 STAT_ACK_ANY
= STAT_CR_ACK
|
307 CR_READ_ENABLE
= (1 << 16),
308 CR_WRITE_ENABLE
= (1 << 17),
309 CR_CAP_DATA
= (1 << 18),
313 /* Link layer registers */
318 OXNAS_SATA_SOFTRESET
= 1,
319 OXNAS_SATA_REINIT
= 2,
323 OXNAS_SATA_UCODE_RAID0
,
324 OXNAS_SATA_UCODE_RAID1
,
325 OXNAS_SATA_UCODE_JBOD
,
326 OXNAS_SATA_UCODE_NONE
,
338 typedef irqreturn_t (*oxnas_sata_isr_callback_t
)(int, unsigned long, int);
340 struct sata_oxnas_host_priv
{
341 void __iomem
*port_base
;
342 void __iomem
*dmactl_base
;
343 void __iomem
*sgdma_base
;
344 void __iomem
*core_base
;
345 void __iomem
*phy_base
;
347 void __iomem
*dma_base_va
;
355 struct reset_control
*rst_sata
;
356 struct reset_control
*rst_link
;
357 struct reset_control
*rst_phy
;
359 spinlock_t core_lock
;
361 int reentrant_port_no
;
363 int direct_lock_count
;
365 int current_locker_type
;
366 int scsi_nonblocking_attempts
;
367 oxnas_sata_isr_callback_t isr_callback
;
369 wait_queue_head_t fast_wait_queue
;
370 wait_queue_head_t scsi_wait_queue
;
374 struct sata_oxnas_port_priv
{
375 void __iomem
*port_base
;
376 void __iomem
*dmactl_base
;
377 void __iomem
*sgdma_base
;
378 void __iomem
*core_base
;
379 struct sgdma_request
*sgdma_request
;
380 dma_addr_t sgdma_request_pa
;
383 static u8
sata_oxnas_check_status(struct ata_port
*ap
);
384 static int sata_oxnas_cleanup(struct ata_host
*ah
);
385 static void sata_oxnas_tf_load(struct ata_port
*ap
,
386 const struct ata_taskfile
*tf
);
387 static void sata_oxnas_irq_on(struct ata_port
*ap
);
388 static void sata_oxnas_post_reset_init(struct ata_port
*ap
);
390 static int sata_oxnas_acquire_hw(struct ata_port
*ap
, int may_sleep
,
391 int timeout_jiffies
);
392 static void sata_oxnas_release_hw(struct ata_port
*ap
);
394 static const void *HW_LOCKER_UID
= (void*)0xdeadbeef;
396 /***************************************************************************
398 ***************************************************************************/
399 static void wait_cr_ack(void __iomem
*phy_base
)
401 while ((ioread32(phy_base
+ PHY_STAT
) >> 16) & 0x1f)
402 ; /* wait for an ack bit to be set */
405 static u16
read_cr(void __iomem
*phy_base
, u16 address
)
407 iowrite32((u32
)address
, phy_base
+ PHY_STAT
);
408 wait_cr_ack(phy_base
);
409 iowrite32(CR_READ_ENABLE
, phy_base
+ PHY_DATA
);
410 wait_cr_ack(phy_base
);
411 return (u16
)ioread32(phy_base
+ PHY_STAT
);
414 static void write_cr(void __iomem
*phy_base
, u16 data
, u16 address
)
416 iowrite32((u32
)address
, phy_base
+ PHY_STAT
);
417 wait_cr_ack(phy_base
);
418 iowrite32((data
| CR_CAP_DATA
), phy_base
+ PHY_DATA
);
419 wait_cr_ack(phy_base
);
420 iowrite32(CR_WRITE_ENABLE
, phy_base
+ PHY_DATA
);
421 wait_cr_ack(phy_base
);
426 #define PH_GAIN_OFFSET 6
427 #define FR_GAIN_OFFSET 8
428 #define PH_GAIN_MASK (0x3 << PH_GAIN_OFFSET)
429 #define FR_GAIN_MASK (0x3 << FR_GAIN_OFFSET)
430 #define USE_INT_SETTING (1<<5)
432 void workaround5458(struct ata_host
*ah
)
434 struct sata_oxnas_host_priv
*hd
= ah
->private_data
;
435 void __iomem
*phy_base
= hd
->phy_base
;
439 for (i
= 0; i
< 2; i
++) {
440 rx_control
= read_cr(phy_base
, 0x201d + (i
<< 8));
441 rx_control
&= ~(PH_GAIN_MASK
| FR_GAIN_MASK
);
442 rx_control
|= PH_GAIN
<< PH_GAIN_OFFSET
;
443 rx_control
|= (FR_GAIN
<< FR_GAIN_OFFSET
) | USE_INT_SETTING
;
444 write_cr(phy_base
, rx_control
, 0x201d+(i
<<8));
449 * allows access to the link layer registers
450 * @param link_reg the link layer register to access (oxsemi indexing ie
451 * 00 = static config, 04 = phy ctrl)
453 void sata_oxnas_link_write(struct ata_port
*ap
, unsigned int link_reg
, u32 val
)
455 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
456 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
457 void __iomem
*port_base
= pd
->port_base
;
461 DPRINTK("P%d [0x%02x]->0x%08x\n", ap
->port_no
, link_reg
, val
);
463 spin_lock_irqsave(&hd
->phy_lock
, flags
);
464 iowrite32(val
, port_base
+ LINK_DATA
);
466 /* accessed twice as a work around for a bug in the SATA abp bridge
467 * hardware (bug 6828) */
468 iowrite32(link_reg
, port_base
+ LINK_WR_ADDR
);
469 ioread32(port_base
+ LINK_WR_ADDR
);
471 for (patience
= 0x100000; patience
> 0; --patience
) {
472 if (ioread32(port_base
+ LINK_CONTROL
) & 0x00000001)
475 spin_unlock_irqrestore(&hd
->phy_lock
, flags
);
478 static int sata_oxnas_scr_write_port(struct ata_port
*ap
, unsigned int sc_reg
,
481 sata_oxnas_link_write(ap
, 0x20 + (sc_reg
* 4), val
);
485 static int sata_oxnas_scr_write(struct ata_link
*link
, unsigned int sc_reg
,
488 return sata_oxnas_scr_write_port(link
->ap
, sc_reg
, val
);
491 u32
sata_oxnas_link_read(struct ata_port
*ap
, unsigned int link_reg
)
493 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
494 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
495 void __iomem
*port_base
= pd
->port_base
;
500 spin_lock_irqsave(&hd
->phy_lock
, flags
);
501 /* accessed twice as a work around for a bug in the SATA abp bridge
502 * hardware (bug 6828) */
503 iowrite32(link_reg
, port_base
+ LINK_RD_ADDR
);
504 ioread32(port_base
+ LINK_RD_ADDR
);
506 for (patience
= 0x100000; patience
> 0; --patience
) {
507 if (ioread32(port_base
+ LINK_CONTROL
) & 0x00000001)
511 DPRINTK("link read timed out for port %d\n", ap
->port_no
);
513 result
= ioread32(port_base
+ LINK_DATA
);
514 spin_unlock_irqrestore(&hd
->phy_lock
, flags
);
519 static int sata_oxnas_scr_read_port(struct ata_port
*ap
, unsigned int sc_reg
,
522 *val
= sata_oxnas_link_read(ap
, 0x20 + (sc_reg
*4));
526 static int sata_oxnas_scr_read(struct ata_link
*link
,
527 unsigned int sc_reg
, u32
*val
)
529 return sata_oxnas_scr_read_port(link
->ap
, sc_reg
, val
);
533 * sata_oxnas_irq_clear is called during probe just before the interrupt handler is
534 * registered, to be sure hardware is quiet. It clears and masks interrupt bits
537 * @param ap hardware with the registers in
539 static void sata_oxnas_irq_clear(struct ata_port
*ap
)
541 struct sata_oxnas_port_priv
*port_priv
= ap
->private_data
;
543 /* clear pending interrupts */
544 iowrite32(~0, port_priv
->port_base
+ INT_CLEAR
);
545 iowrite32(COREINT_END
, port_priv
->core_base
+ CORE_INT_CLEAR
);
549 * qc_issue is used to make a command active, once the hardware and S/G tables
550 * have been prepared. IDE BMDMA drivers use the helper function
551 * ata_qc_issue_prot() for taskfile protocol-based dispatch. More advanced
552 * drivers roll their own ->qc_issue implementation, using this as the
553 * "issue new ATA command to hardware" hook.
554 * @param qc the queued command to issue
556 static unsigned int sata_oxnas_qc_issue(struct ata_queued_cmd
*qc
)
558 struct sata_oxnas_port_priv
*pd
= qc
->ap
->private_data
;
559 struct sata_oxnas_host_priv
*hd
= qc
->ap
->host
->private_data
;
561 void __iomem
*port_base
= pd
->port_base
;
562 void __iomem
*core_base
= pd
->core_base
;
563 int port_no
= qc
->ap
->port_no
;
564 int no_microcode
= ( hd
->current_ucode
== UNKNOWN_MODE
);
567 /* check the core is idle */
568 if (ioread32(port_base
+ SATA_COMMAND
) & CMD_CORE_BUSY
) {
571 DPRINTK("core busy for a command on port %d\n",
576 DPRINTK("core busy for a command on port %d\n",
578 /* CrazyDumpDebug(); */
579 sata_oxnas_cleanup(qc
->ap
->host
);
581 } while (ioread32(port_base
+ SATA_COMMAND
) & CMD_CORE_BUSY
);
584 /* enable passing of error signals to DMA sub-core by clearing the
586 reg
= ioread32(core_base
+ DATA_PLANE_CTRL
);
588 reg
|= (DPC_ERROR_MASK_BIT
| (DPC_ERROR_MASK_BIT
<< 1));
589 reg
&= ~(DPC_ERROR_MASK_BIT
<< port_no
);
590 iowrite32(reg
, core_base
+ DATA_PLANE_CTRL
);
592 /* Disable all interrupts for ports and RAID controller */
593 iowrite32(~0, port_base
+ INT_DISABLE
);
595 /* Disable all interrupts for core */
596 iowrite32(~0, core_base
+ CORE_INT_DISABLE
);
599 /* Load the command settings into the orb registers */
600 sata_oxnas_tf_load(qc
->ap
, &qc
->tf
);
602 /* both pio and dma commands use dma */
603 if (ata_is_dma(qc
->tf
.protocol
) || ata_is_pio(qc
->tf
.protocol
)) {
605 iowrite32(SGDMA_CONTROL_GO
, pd
->sgdma_base
+ SGDMA_CONTROL
);
609 /* enable End of command interrupt */
610 iowrite32(INT_WANT
, port_base
+ INT_ENABLE
);
611 iowrite32(COREINT_END
, core_base
+ CORE_INT_ENABLE
);
614 /* Start the command */
615 reg
= ioread32(port_base
+ SATA_COMMAND
);
616 reg
&= ~SATA_OPCODE_MASK
;
617 reg
|= CMD_WRITE_TO_ORB_REGS
;
618 iowrite32(reg
, port_base
+ SATA_COMMAND
);
625 * Will schedule the libATA error handler on the premise that there has
626 * been a hotplug event on the port specified
628 void sata_oxnas_checkforhotplug(struct ata_port
*ap
)
632 ata_ehi_hotplugged(&ap
->link
.eh_info
);
637 /**************************************************************************/
639 /**************************************************************************/
641 * The underlying function that controls access to the sata core
643 * @return non-zero indicates that you have acquired exclusive access to the
646 static int __acquire_sata_core(
649 oxnas_sata_isr_callback_t callback
,
657 unsigned long end
= jiffies
+ timeout_jiffies
;
661 struct sata_oxnas_host_priv
*hd
;
668 hd
= ah
->private_data
;
670 spin_lock_irqsave(&hd
->core_lock
, flags
);
672 DPRINTK("Entered uid %p, port %d, h/w count %d, d count %d, callback %p, "
673 "hw_access %d, core_locked %d, reentrant_port_no %d, isr_callback %p\n",
674 uid
, port_no
, hd
->hw_lock_count
, hd
->direct_lock_count
, callback
, hw_access
,
675 hd
->core_locked
, hd
->reentrant_port_no
, hd
->isr_callback
);
678 if (hd
->core_locked
|| (!hw_access
&& hd
->scsi_nonblocking_attempts
)) {
679 /* Can only allow access if from SCSI/SATA stack and if
680 reentrant access is allowed and this access is to the same
681 port for which the lock is current held */
682 if (hw_access
&& (port_no
== hd
->reentrant_port_no
)) {
683 BUG_ON(!hd
->hw_lock_count
);
684 ++(hd
->hw_lock_count
);
686 DPRINTK("Allow SCSI/SATA re-entrant access to uid %p port %d\n", uid
, port_no
);
689 } else if (!hw_access
) {
690 if ((locker_type
== SATA_READER
) && (hd
->current_locker_type
== SATA_READER
)) {
692 "Already locked by reader, uid %p, locker_uid %p, port %d, "
693 "h/w count %d, d count %d, hw_access %d\n", uid
, hd
->locker_uid
,
694 port_no
, hd
->hw_lock_count
, hd
->direct_lock_count
, hw_access
);
698 if ((locker_type
!= SATA_READER
) && (locker_type
!= SATA_WRITER
)) {
703 WARN(uid
== hd
->locker_uid
, "Attempt to lock by locker type %d "
704 "uid %p, already locked by locker type %d with "
705 "locker_uid %p, port %d, h/w count %d, d count %d, "
706 "hw_access %d\n", locker_type
, uid
, hd
->current_locker_type
,
707 hd
->locker_uid
, port_no
, hd
->hw_lock_count
, hd
->direct_lock_count
, hw_access
);
710 WARN(hd
->hw_lock_count
|| hd
->direct_lock_count
, "Core unlocked but counts "
711 "non-zero: uid %p, locker_uid %p, port %d, h/w count %d, "
712 "d count %d, hw_access %d\n", uid
, hd
->locker_uid
, port_no
,
713 hd
->hw_lock_count
, hd
->direct_lock_count
, hw_access
);
715 BUG_ON(hd
->current_locker_type
!= SATA_UNLOCKED
);
717 WARN(hd
->locker_uid
, "Attempt to lock uid %p when locker_uid %p is "
718 "non-zero, port %d, h/w count %d, d count %d, hw_access %d\n",
719 uid
, hd
->locker_uid
, port_no
, hd
->hw_lock_count
, hd
->direct_lock_count
,
723 /* Direct access attempting to acquire non-contented lock */
724 BUG_ON(!callback
); // Must have callback for direct access
725 BUG_ON(hd
->reentrant_port_no
!= -1); // Sanity check lock state
727 hd
->isr_callback
= callback
;
729 ++(hd
->direct_lock_count
);
731 hd
->current_locker_type
= locker_type
;
733 /* SCSI/SATA attempting to acquire non-contented lock */
734 BUG_ON(callback
); // No callbacks for SCSI/SATA access
735 BUG_ON(arg
); // No callback args for SCSI/SATA access
737 BUG_ON(hd
->isr_callback
); // Sanity check lock state
738 BUG_ON(hd
->isr_arg
); // Sanity check lock state
740 ++(hd
->hw_lock_count
);
741 hd
->reentrant_port_no
= port_no
;
743 hd
->current_locker_type
= SATA_SCSI_STACK
;
747 hd
->locker_uid
= uid
;
754 DPRINTK("Denying for uid %p locker_type %d, hw_access %d, port %d, "
755 "current_locker_type %d as cannot sleep\n", uid
, locker_type
,
756 hw_access
, port_no
, hd
->current_locker_type
);
759 ++(hd
->scsi_nonblocking_attempts
);
764 // Core is locked and we're allowed to sleep, so wait to be awoken when
765 // the core is unlocked
767 prepare_to_wait(hw_access
? &hd
->scsi_wait_queue
: &hd
->fast_wait_queue
,
768 &wait
, TASK_UNINTERRUPTIBLE
);
769 if (!hd
->core_locked
&& !(!hw_access
&& hd
->scsi_nonblocking_attempts
)) {
770 // We're going to use variables that will have been changed by
771 // the waker prior to clearing core_locked so we need to ensure
772 // we see changes to all those variables
776 if (time_after(jiffies
, end
)) {
777 printk("__acquire_sata_core() uid %p failing for port %d timed out, "
778 "locker_uid %p, h/w count %d, d count %d, callback %p, hw_access %d, "
779 "core_locked %d, reentrant_port_no %d, isr_callback %p, "
780 "isr_arg %p\n", uid
, port_no
, hd
->locker_uid
,
781 hd
->hw_lock_count
, hd
->direct_lock_count
, callback
, hw_access
,
782 hd
->core_locked
, hd
->reentrant_port_no
, hd
->isr_callback
,
787 spin_unlock_irqrestore(&hd
->core_lock
, flags
);
788 if (!schedule_timeout(4*HZ
)) {
789 printk(KERN_INFO
"__acquire_sata_core() uid %p, locker_uid %p, "
790 "timed-out of schedule(), checking overall timeout\n",
791 uid
, hd
->locker_uid
);
793 spin_lock_irqsave(&hd
->core_lock
, flags
);
795 finish_wait(hw_access
? &hd
->scsi_wait_queue
: &hd
->fast_wait_queue
, &wait
);
798 if (hw_access
&& acquired
) {
799 if (hd
->scsi_nonblocking_attempts
) {
800 hd
->scsi_nonblocking_attempts
= 0;
803 // Wake any other SCSI/SATA waiters so they can get reentrant access to
804 // the same port if appropriate. This is because if the SATA core is
805 // locked by fast access, or SCSI/SATA access to other port, then can
806 // have >1 SCSI/SATA waiters on the wait list so want to give reentrant
807 // accessors a chance to get access ASAP
808 if (!list_empty(&hd
->scsi_wait_queue
.task_list
)) {
809 wake_up(&hd
->scsi_wait_queue
);
813 DPRINTK("Leaving uid %p with acquired = %d, port %d, callback %p\n", uid
, acquired
, port_no
, callback
);
815 spin_unlock_irqrestore(&hd
->core_lock
, flags
);
820 int sata_core_has_fast_waiters(struct ata_host
*ah
)
824 struct sata_oxnas_host_priv
*hd
= ah
->private_data
;
826 spin_lock_irqsave(&hd
->core_lock
, flags
);
827 has_waiters
= !list_empty(&hd
->fast_wait_queue
.task_list
);
828 spin_unlock_irqrestore(&hd
->core_lock
, flags
);
832 EXPORT_SYMBOL(sata_core_has_fast_waiters
);
834 int sata_core_has_scsi_waiters(struct ata_host
*ah
)
838 struct sata_oxnas_host_priv
*hd
= ah
->private_data
;
840 spin_lock_irqsave(&hd
->core_lock
, flags
);
841 has_waiters
= hd
->scsi_nonblocking_attempts
|| !list_empty(&hd
->scsi_wait_queue
.task_list
);
842 spin_unlock_irqrestore(&hd
->core_lock
, flags
);
846 EXPORT_SYMBOL(sata_core_has_scsi_waiters
);
849 * ata_port operation to gain ownership of the SATA hardware prior to issuing
850 * a command against a SATA host. Allows any number of users of the port against
851 * which the lock was first acquired, thus enforcing that only one SATA core
852 * port may be operated on at once.
854 static int sata_oxnas_acquire_hw(
859 return __acquire_sata_core(ap
->host
, ap
->port_no
, NULL
, 0, may_sleep
, timeout_jiffies
, 1, (void*)HW_LOCKER_UID
, SATA_SCSI_STACK
);
863 * operation to release ownership of the SATA hardware
865 static void sata_oxnas_release_hw(struct ata_port
*ap
)
869 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
871 spin_lock_irqsave(&hd
->core_lock
, flags
);
873 DPRINTK("Entered port_no = %d, h/w count %d, d count %d, core locked = %d, "
874 "reentrant_port_no = %d, isr_callback %p\n", ap
->port_no
,
875 hd
->hw_lock_count
, hd
->direct_lock_count
, hd
->core_locked
, hd
->reentrant_port_no
, hd
->isr_callback
);
877 if (!hd
->core_locked
) {
878 /* Nobody holds the SATA lock */
879 printk(KERN_WARNING
"Nobody holds SATA lock, port_no %d\n", ap
->port_no
);
881 } else if (!hd
->hw_lock_count
) {
882 /* SCSI/SATA has released without holding the lock */
883 printk(KERN_WARNING
"SCSI/SATA does not hold SATA lock, port_no %d\n", ap
->port_no
);
885 /* Trap incorrect usage */
886 BUG_ON(hd
->reentrant_port_no
== -1);
887 BUG_ON(ap
->port_no
!= hd
->reentrant_port_no
);
888 BUG_ON(hd
->direct_lock_count
);
889 BUG_ON(hd
->current_locker_type
!= SATA_SCSI_STACK
);
891 WARN(!hd
->locker_uid
|| (hd
->locker_uid
!= HW_LOCKER_UID
), "Invalid locker "
892 "uid %p, h/w count %d, d count %d, reentrant_port_no %d, "
893 "core_locked %d, isr_callback %p\n", hd
->locker_uid
,
894 hd
->hw_lock_count
, hd
->direct_lock_count
, hd
->reentrant_port_no
,
895 hd
->core_locked
, hd
->isr_callback
);
897 if (--(hd
->hw_lock_count
)) {
898 DPRINTK("Still nested port_no %d\n", ap
->port_no
);
900 DPRINTK("Release port_no %d\n", ap
->port_no
);
901 hd
->reentrant_port_no
= -1;
902 hd
->isr_callback
= NULL
;
903 hd
->current_locker_type
= SATA_UNLOCKED
;
907 wake_up(!list_empty(&hd
->scsi_wait_queue
.task_list
) ? &hd
->scsi_wait_queue
: &hd
->fast_wait_queue
);
911 DPRINTK("Leaving, port_no %d, count %d\n", ap
->port_no
, hd
->hw_lock_count
);
913 spin_unlock_irqrestore(&hd
->core_lock
, flags
);
915 /* CONFIG_SATA_OX820_DIRECT_HWRAID */
917 ox820hwraid_restart_queue();
921 static inline int sata_oxnas_is_host_frozen(struct ata_host
*ah
)
923 struct sata_oxnas_host_priv
*hd
= ah
->private_data
;
926 return hd
->port_in_eh
|| hd
->port_frozen
;
930 static inline u32
sata_oxnas_hostportbusy(struct ata_port
*ap
)
932 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
934 return (ioread32(hd
->port_base
+ SATA_COMMAND
) & CMD_CORE_BUSY
) ||
936 (ioread32(hd
->port_base
+ PORT_SIZE
+ SATA_COMMAND
) & CMD_CORE_BUSY
));
939 static inline u32
sata_oxnas_hostdmabusy(struct ata_port
*ap
)
941 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
943 return ioread32(pd
->sgdma_base
+ SGDMA_STATUS
) & SGDMA_BUSY
;
948 * Turns on the cores clock and resets it
950 static void sata_oxnas_reset_core(struct ata_host
*ah
)
952 struct sata_oxnas_host_priv
*host_priv
= ah
->private_data
;
956 clk_prepare_enable(host_priv
->clk
);
958 reset_control_assert(host_priv
->rst_sata
);
959 reset_control_assert(host_priv
->rst_link
);
960 reset_control_assert(host_priv
->rst_phy
);
964 /* un-reset the PHY, then Link and Controller */
965 reset_control_deassert(host_priv
->rst_phy
);
968 reset_control_deassert(host_priv
->rst_sata
);
969 reset_control_deassert(host_priv
->rst_link
);
973 /* tune for sata compatability */
974 sata_oxnas_link_write(ah
->ports
[0], 0x60, 0x2988);
976 for (n
=0; n
< host_priv
->n_ports
; n
++) {
977 /* each port in turn */
978 sata_oxnas_link_write(ah
->ports
[n
], 0x70, 0x55629);
985 * Called after an identify device command has worked out what kind of device
988 * @param port The port to configure
989 * @param pdev The hardware associated with controlling the port
991 static void sata_oxnas_dev_config(struct ata_device
*pdev
)
993 struct sata_oxnas_port_priv
*pd
= pdev
->link
->ap
->private_data
;
994 void __iomem
*port_base
= pd
->port_base
;
998 /* Set the bits to put the port into 28 or 48-bit node */
999 reg
= ioread32(port_base
+ DRIVE_CONTROL
);
1001 reg
|= (pdev
->flags
& ATA_DFLAG_LBA48
) ? DR_CON_48
: DR_CON_28
;
1002 iowrite32(reg
, port_base
+ DRIVE_CONTROL
);
1004 /* if this is an ATA-6 disk, put port into ATA-5 auto translate mode */
1005 if (pdev
->flags
& ATA_DFLAG_LBA48
) {
1006 reg
= ioread32(port_base
+ PORT_CONTROL
);
1008 iowrite32(reg
, port_base
+ PORT_CONTROL
);
1012 * called to write a taskfile into the ORB registers
1013 * @param ap hardware with the registers in
1014 * @param tf taskfile to write to the registers
1016 static void sata_oxnas_tf_load(struct ata_port
*ap
,
1017 const struct ata_taskfile
*tf
)
1026 struct sata_oxnas_port_priv
*port_priv
= ap
->private_data
;
1027 void __iomem
*port_base
= port_priv
->port_base
;
1028 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
1030 /* wait a maximum of 10ms for the core to be idle */
1032 Command_Reg
= ioread32(port_base
+ SATA_COMMAND
);
1033 if (!(Command_Reg
& CMD_CORE_BUSY
))
1037 } while (count
< 200);
1039 /* check if the ctl register has interrupts disabled or enabled and
1040 * modify the interrupt enable registers on the ata core as required */
1041 if (tf
->ctl
& ATA_NIEN
) {
1042 /* interrupts disabled */
1043 u32 mask
= (COREINT_END
<< ap
->port_no
);
1045 iowrite32(mask
, port_priv
->core_base
+ CORE_INT_DISABLE
);
1046 sata_oxnas_irq_clear(ap
);
1048 sata_oxnas_irq_on(ap
);
1051 Orb2
|= (tf
->command
) << 24;
1053 /* write 48 or 28 bit tf parameters */
1055 /* set LBA bit as it's an address */
1056 Orb1
|= (tf
->device
& ATA_LBA
) << 24;
1058 if (tf
->flags
& ATA_TFLAG_LBA48
) {
1059 Orb1
|= ATA_LBA
<< 24;
1060 Orb2
|= (tf
->hob_nsect
) << 8;
1061 Orb3
|= (tf
->hob_lbal
) << 24;
1062 Orb4
|= (tf
->hob_lbam
) << 0;
1063 Orb4
|= (tf
->hob_lbah
) << 8;
1064 Orb4
|= (tf
->hob_feature
) << 16;
1066 Orb3
|= (tf
->device
& 0xf) << 24;
1069 /* write 28-bit lba */
1070 Orb2
|= (tf
->nsect
) << 0;
1071 Orb2
|= (tf
->feature
) << 16;
1072 Orb3
|= (tf
->lbal
) << 0;
1073 Orb3
|= (tf
->lbam
) << 8;
1074 Orb3
|= (tf
->lbah
) << 16;
1075 Orb4
|= (tf
->ctl
) << 24;
1078 if (tf
->flags
& ATA_TFLAG_DEVICE
)
1079 Orb1
|= (tf
->device
) << 24;
1081 ap
->last_ctl
= tf
->ctl
;
1083 /* write values to registers */
1084 iowrite32(Orb1
, port_base
+ ORB1
);
1085 iowrite32(Orb2
, port_base
+ ORB2
);
1086 iowrite32(Orb3
, port_base
+ ORB3
);
1087 iowrite32(Orb4
, port_base
+ ORB4
);
1091 void sata_oxnas_set_mode(struct ata_host
*ah
, u32 mode
, u32 force
)
1093 struct sata_oxnas_host_priv
*host_priv
= ah
->private_data
;
1094 void __iomem
*core_base
= host_priv
->core_base
;
1098 unsigned int progmicrocode
= 0;
1099 unsigned int changeparameters
= 0;
1103 /* these micro-code programs _should_ include the version word */
1106 static const unsigned int jbod
[] = {
1107 0x07B400AC, 0x0228A280, 0x00200001, 0x00204002, 0x00224001,
1108 0x00EE0009, 0x00724901, 0x01A24903, 0x00E40009, 0x00224001,
1109 0x00621120, 0x0183C908, 0x00E20005, 0x00718908, 0x0198A206,
1110 0x00621124, 0x0183C908, 0x00E20046, 0x00621104, 0x0183C908,
1111 0x00E20015, 0x00EE009D, 0x01A3E301, 0x00E2001B, 0x0183C900,
1112 0x00E2001B, 0x00210001, 0x00EE0020, 0x01A3E302, 0x00E2009D,
1113 0x0183C901, 0x00E2009D, 0x00210002, 0x0235D700, 0x0208A204,
1114 0x0071C908, 0x000F8207, 0x000FC207, 0x0071C920, 0x000F8507,
1115 0x000FC507, 0x0228A240, 0x02269A40, 0x00094004, 0x00621104,
1116 0x0180C908, 0x00E40031, 0x00621112, 0x01A3C801, 0x00E2002B,
1117 0x00294000, 0x0228A220, 0x01A69ABF, 0x002F8000, 0x002FC000,
1118 0x0198A204, 0x0001C022, 0x01B1A220, 0x0001C106, 0x00088007,
1119 0x0183C903, 0x00E2009D, 0x0228A220, 0x0071890C, 0x0208A206,
1120 0x0198A206, 0x0001C022, 0x01B1A220, 0x0001C106, 0x00088007,
1121 0x00EE009D, 0x00621104, 0x0183C908, 0x00E2004A, 0x00EE009D,
1122 0x01A3C901, 0x00E20050, 0x0021E7FF, 0x0183E007, 0x00E2009D,
1123 0x00EE0054, 0x0061600B, 0x0021E7FF, 0x0183C507, 0x00E2009D,
1124 0x01A3E301, 0x00E2005A, 0x0183C900, 0x00E2005A, 0x00210001,
1125 0x00EE005F, 0x01A3E302, 0x00E20005, 0x0183C901, 0x00E20005,
1126 0x00210002, 0x0235D700, 0x0208A204, 0x000F8109, 0x000FC109,
1127 0x0071C918, 0x000F8407, 0x000FC407, 0x0001C022, 0x01A1A2BF,
1128 0x0001C106, 0x00088007, 0x02269A40, 0x00094004, 0x00621112,
1129 0x01A3C801, 0x00E4007F, 0x00621104, 0x0180C908, 0x00E4008D,
1130 0x00621128, 0x0183C908, 0x00E2006C, 0x01A3C901, 0x00E2007B,
1131 0x0021E7FF, 0x0183E007, 0x00E2007F, 0x00EE006C, 0x0061600B,
1132 0x0021E7FF, 0x0183C507, 0x00E4006C, 0x00621111, 0x01A3C801,
1133 0x00E2007F, 0x00621110, 0x01A3C801, 0x00E20082, 0x0228A220,
1134 0x00621119, 0x01A3C801, 0x00E20086, 0x0001C022, 0x01B1A220,
1135 0x0001C106, 0x00088007, 0x0198A204, 0x00294000, 0x01A69ABF,
1136 0x002F8000, 0x002FC000, 0x0183C903, 0x00E20005, 0x0228A220,
1137 0x0071890C, 0x0208A206, 0x0198A206, 0x0001C022, 0x01B1A220,
1138 0x0001C106, 0x00088007, 0x00EE009D, 0x00621128, 0x0183C908,
1139 0x00E20005, 0x00621104, 0x0183C908, 0x00E200A6, 0x0062111C,
1140 0x0183C908, 0x00E20005, 0x0071890C, 0x0208A206, 0x0198A206,
1141 0x00718908, 0x0208A206, 0x00EE0005, ~0
1144 /* Bi-Modal RAID-0/1 */
1145 static const unsigned int raid
[] = {
1146 0x00F20145, 0x00EE20FA, 0x00EE20A7, 0x0001C009, 0x00EE0004,
1147 0x00220000, 0x0001000B, 0x037003FF, 0x00700018, 0x037003FE,
1148 0x037043FD, 0x00704118, 0x037043FC, 0x01A3D240, 0x00E20017,
1149 0x00B3C235, 0x00E40018, 0x0093C104, 0x00E80014, 0x0093C004,
1150 0x00E80017, 0x01020000, 0x00274020, 0x00EE0083, 0x0080C904,
1151 0x0093C104, 0x00EA0020, 0x0093C103, 0x00EC001F, 0x00220002,
1152 0x00924104, 0x0005C009, 0x00EE0058, 0x0093CF04, 0x00E80026,
1153 0x00900F01, 0x00600001, 0x00910400, 0x00EE0058, 0x00601604,
1154 0x01A00003, 0x00E2002C, 0x01018000, 0x00274040, 0x00EE0083,
1155 0x0093CF03, 0x00EC0031, 0x00220003, 0x00924F04, 0x0005C009,
1156 0x00810104, 0x00B3C235, 0x00E20037, 0x0022C000, 0x00218210,
1157 0x00EE0039, 0x0022C001, 0x00218200, 0x00600401, 0x00A04901,
1158 0x00604101, 0x01A0C401, 0x00E20040, 0x00216202, 0x00EE0041,
1159 0x00216101, 0x02018506, 0x00EE2141, 0x00904901, 0x00E20049,
1160 0x00A00401, 0x00600001, 0x02E0C301, 0x00EE2141, 0x00216303,
1161 0x037003EE, 0x01A3C001, 0x00E40105, 0x00250080, 0x00204000,
1162 0x002042F1, 0x0004C001, 0x00230001, 0x00100006, 0x02C18605,
1163 0x00100006, 0x01A3D502, 0x00E20055, 0x00EE0053, 0x00004009,
1164 0x00000004, 0x00B3C235, 0x00E40062, 0x0022C001, 0x0020C000,
1165 0x00EE2141, 0x0020C001, 0x00EE2141, 0x00EE006B, 0x0022C000,
1166 0x0060D207, 0x00EE2141, 0x00B3C242, 0x00E20069, 0x01A3D601,
1167 0x00E2006E, 0x02E0C301, 0x00EE2141, 0x00230001, 0x00301303,
1168 0x00EE007B, 0x00218210, 0x01A3C301, 0x00E20073, 0x00216202,
1169 0x00EE0074, 0x00216101, 0x02018506, 0x00214000, 0x037003EE,
1170 0x01A3C001, 0x00E40108, 0x00230001, 0x00100006, 0x00250080,
1171 0x00204000, 0x002042F1, 0x0004C001, 0x00EE007F, 0x0024C000,
1172 0x01A3D1F0, 0x00E20088, 0x00230001, 0x00300000, 0x01A3D202,
1173 0x00E20085, 0x00EE00A5, 0x00B3C800, 0x00E20096, 0x00218000,
1174 0x00924709, 0x0005C009, 0x00B20802, 0x00E40093, 0x037103FD,
1175 0x00710418, 0x037103FC, 0x00EE0006, 0x00220000, 0x0001000F,
1176 0x00EE0006, 0x00800B0C, 0x00B00001, 0x00204000, 0x00208550,
1177 0x00208440, 0x002083E0, 0x00208200, 0x00208100, 0x01008000,
1178 0x037083EE, 0x02008212, 0x02008216, 0x01A3C201, 0x00E400A5,
1179 0x0100C000, 0x00EE20FA, 0x02800000, 0x00208000, 0x00B24C00,
1180 0x00E400AD, 0x00224001, 0x00724910, 0x0005C009, 0x00B3CDC4,
1181 0x00E200D5, 0x00B3CD29, 0x00E200D5, 0x00B3CD20, 0x00E200D5,
1182 0x00B3CD24, 0x00E200D5, 0x00B3CDC5, 0x00E200D2, 0x00B3CD39,
1183 0x00E200D2, 0x00B3CD30, 0x00E200D2, 0x00B3CD34, 0x00E200D2,
1184 0x00B3CDCA, 0x00E200CF, 0x00B3CD35, 0x00E200CF, 0x00B3CDC8,
1185 0x00E200CC, 0x00B3CD25, 0x00E200CC, 0x00B3CD40, 0x00E200CB,
1186 0x00B3CD42, 0x00E200CB, 0x01018000, 0x00EE0083, 0x0025C000,
1187 0x036083EE, 0x0000800D, 0x00EE00D8, 0x036083EE, 0x00208035,
1188 0x00EE00DA, 0x036083EE, 0x00208035, 0x00EE00DA, 0x00208007,
1189 0x036083EE, 0x00208025, 0x036083EF, 0x02400000, 0x01A3D208,
1190 0x00E200D8, 0x0067120A, 0x0021C000, 0x0021C224, 0x00220000,
1191 0x00404B1C, 0x00600105, 0x00800007, 0x0020C00E, 0x00214000,
1192 0x01004000, 0x01A0411F, 0x00404E01, 0x01A3C101, 0x00E200F1,
1193 0x00B20800, 0x00E400D8, 0x00220001, 0x0080490B, 0x00B04101,
1194 0x0040411C, 0x00EE00E1, 0x02269A01, 0x01020000, 0x02275D80,
1195 0x01A3D202, 0x00E200F4, 0x01B75D80, 0x01030000, 0x01B69A01,
1196 0x00EE00D8, 0x01A3D204, 0x00E40104, 0x00224000, 0x0020C00E,
1197 0x0020001E, 0x00214000, 0x01004000, 0x0212490E, 0x00214001,
1198 0x01004000, 0x02400000, 0x00B3D702, 0x00E80112, 0x00EE010E,
1199 0x00B3D702, 0x00E80112, 0x00B3D702, 0x00E4010E, 0x00230001,
1200 0x00EE0140, 0x00200005, 0x036003EE, 0x00204001, 0x00EE0116,
1201 0x00230001, 0x00100006, 0x02C18605, 0x00100006, 0x01A3D1F0,
1202 0x00E40083, 0x037003EE, 0x01A3C002, 0x00E20121, 0x0020A300,
1203 0x0183D102, 0x00E20124, 0x037003EE, 0x01A00005, 0x036003EE,
1204 0x01A0910F, 0x00B3C20F, 0x00E2012F, 0x01A3D502, 0x00E20116,
1205 0x01A3C002, 0x00E20116, 0x00B3D702, 0x00E4012C, 0x00300000,
1206 0x00EE011F, 0x02C18605, 0x00100006, 0x00EE0116, 0x01A3D1F0,
1207 0x00E40083, 0x037003EE, 0x01A3C004, 0x00E20088, 0x00200003,
1208 0x036003EE, 0x01A3D502, 0x00E20136, 0x00230001, 0x00B3C101,
1209 0x00E4012C, 0x00100006, 0x02C18605, 0x00100006, 0x00204000,
1210 0x00EE0116, 0x00100006, 0x01A3D1F0, 0x00E40083, 0x01000000,
1214 DPRINTK("ENTER: mode:%d, force:%d\n", mode
, force
);
1217 previous_mode
= UNKNOWN_MODE
;
1219 previous_mode
= host_priv
->current_ucode
;
1221 if (mode
== previous_mode
)
1224 host_priv
->current_ucode
= mode
;
1226 /* decide what needs to be done using the STD in my logbook */
1227 switch (previous_mode
) {
1228 case OXNASSATA_RAID1
:
1230 case OXNASSATA_RAID0
:
1231 changeparameters
= 1;
1233 case OXNASSATA_NOTRAID
:
1234 changeparameters
= 1;
1239 case OXNASSATA_RAID0
:
1241 case OXNASSATA_RAID1
:
1242 changeparameters
= 1;
1244 case OXNASSATA_NOTRAID
:
1245 changeparameters
= 1;
1250 case OXNASSATA_NOTRAID
:
1252 case OXNASSATA_RAID0
:
1253 case OXNASSATA_RAID1
:
1254 changeparameters
= 1;
1260 changeparameters
= 1;
1265 /* no need to reprogram everything if already in the right mode */
1266 if (progmicrocode
) {
1267 /* reset micro-code processor */
1268 iowrite32(1, core_base
+ PROC_RESET
);
1271 /* select micro-code */
1273 case OXNASSATA_RAID1
:
1274 case OXNASSATA_RAID0
:
1275 VPRINTK("Loading RAID micro-code\n");
1276 src
= (unsigned int *)&raid
[1];
1278 case OXNASSATA_NOTRAID
:
1279 VPRINTK("Loading JBOD micro-code\n");
1280 src
= (unsigned int *)&jbod
[1];
1287 /* load micro code */
1288 dst
= core_base
+ UCODE_STORE
;
1289 while (*src
!= ~0) {
1290 iowrite32(*src
, dst
);
1292 dst
+= sizeof(*src
);
1297 if (changeparameters
) {
1299 /* set other mode dependent flags */
1301 case OXNASSATA_RAID1
:
1302 /* clear JBOD mode */
1303 reg
= ioread32(core_base
+ DATA_PLANE_CTRL
);
1304 reg
|= DPC_JBOD_UCODE
;
1305 reg
&= ~DPC_FIS_SWCH
;
1306 iowrite32(reg
, core_base
+ DATA_PLANE_CTRL
);
1309 /* set the hardware up for RAID-1 */
1310 iowrite32(0, core_base
+ RAID_WP_BOT_LOW
);
1311 iowrite32(0, core_base
+ RAID_WP_BOT_HIGH
);
1312 iowrite32(0xffffffff, core_base
+ RAID_WP_TOP_LOW
);
1313 iowrite32(0x7fffffff, core_base
+ RAID_WP_TOP_HIGH
);
1314 iowrite32(0, core_base
+ RAID_SIZE_LOW
);
1315 iowrite32(0, core_base
+ RAID_SIZE_HIGH
);
1318 case OXNASSATA_RAID0
:
1319 /* clear JBOD mode */
1320 reg
= ioread32(core_base
+ DATA_PLANE_CTRL
);
1321 reg
|= DPC_JBOD_UCODE
;
1322 reg
&= ~DPC_FIS_SWCH
;
1323 iowrite32(reg
, core_base
+ DATA_PLANE_CTRL
);
1326 /* set the hardware up for RAID-1 */
1327 iowrite32(0, core_base
+ RAID_WP_BOT_LOW
);
1328 iowrite32(0, core_base
+ RAID_WP_BOT_HIGH
);
1329 iowrite32(0xffffffff, core_base
+ RAID_WP_TOP_LOW
);
1330 iowrite32(0x7fffffff, core_base
+ RAID_WP_TOP_HIGH
);
1331 iowrite32(0xffffffff, core_base
+ RAID_SIZE_LOW
);
1332 iowrite32(0x7fffffff, core_base
+ RAID_SIZE_HIGH
);
1335 case OXNASSATA_NOTRAID
:
1336 /* enable jbod mode */
1337 reg
= ioread32(core_base
+ DATA_PLANE_CTRL
);
1338 reg
&= ~DPC_JBOD_UCODE
;
1339 reg
&= ~DPC_FIS_SWCH
;
1340 iowrite32(reg
, core_base
+ DATA_PLANE_CTRL
);
1343 /* start micro-code processor*/
1344 iowrite32(1, core_base
+ PROC_START
);
1347 reg
= ioread32(core_base
+ DATA_PLANE_CTRL
);
1348 reg
|= DPC_JBOD_UCODE
;
1349 reg
&= ~DPC_FIS_SWCH
;
1350 iowrite32(reg
, core_base
+ DATA_PLANE_CTRL
);
1358 * sends a sync-escape if there is a link present
1360 static inline void sata_oxnas_send_sync_escape(struct ata_port
*ap
)
1362 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1365 /* read the SSTATUS register and only send a sync escape if there is a
1367 if ((sata_oxnas_link_read(ap
, 0x20) & 3) == 3) {
1368 reg
= ioread32(pd
->port_base
+ SATA_COMMAND
);
1369 reg
&= ~SATA_OPCODE_MASK
;
1370 reg
|= CMD_SYNC_ESCAPE
;
1371 iowrite32(reg
, pd
->port_base
+ SATA_COMMAND
);
1376 static inline void sata_oxnas_clear_CS_error(struct ata_port
*ap
)
1378 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1379 u32
*base
= pd
->port_base
;
1382 reg
= ioread32(base
+ SATA_CONTROL
);
1383 reg
&= SATA_CTL_ERR_MASK
;
1384 iowrite32(reg
, base
+ SATA_CONTROL
);
1387 static inline void sata_oxnas_reset_sgdma(struct ata_port
*ap
)
1389 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1390 iowrite32(SGDMA_RESETS_CTRL
, pd
->sgdma_base
+ SGDMA_RESETS
);
1393 static inline void sata_oxnas_reset_dma(struct ata_port
*ap
, int assert)
1395 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1398 reg
= ioread32(pd
->dmactl_base
+ DMA_CONTROL
);
1400 reg
|= DMA_CONTROL_RESET
;
1402 reg
&= ~DMA_CONTROL_RESET
;
1404 iowrite32(reg
, pd
->dmactl_base
+ DMA_CONTROL
);
1408 * Clears the error caused by the core's registers being accessed when the
1411 static inline void sata_oxnas_clear_reg_access_error(struct ata_port
*ap
)
1413 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1414 u32
*base
= pd
->port_base
;
1417 reg
= ioread32(base
+ INT_STATUS
);
1420 if (reg
& INT_REG_ACCESS_ERR
) {
1421 printk(KERN_INFO
"clearing register access error on port %d\n", ap
->port_no
);
1422 iowrite32(INT_REG_ACCESS_ERR
, base
+ INT_STATUS
);
1424 reg
= ioread32(base
+ INT_STATUS
);
1425 if (reg
& INT_REG_ACCESS_ERR
)
1426 printk(KERN_INFO
"register access error didn't clear\n");
1429 static inline void sata_oxnas_clear_sctl_error(struct ata_port
*ap
)
1431 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1432 u32
*base
= pd
->port_base
;
1435 reg
= ioread32(base
+ SATA_CONTROL
);
1436 reg
|= SCTL_CLR_ERR
;
1437 iowrite32(reg
, base
+ SATA_CONTROL
);
1440 static inline void sata_oxnas_clear_raid_error(struct ata_host
*ah
)
1446 * Clean up all the state machines in the sata core.
1447 * @return post cleanup action required
1449 static int sata_oxnas_cleanup(struct ata_host
*ah
)
1451 struct sata_oxnas_host_priv
*hd
= ah
->private_data
;
1452 int actions_required
= 0;
1454 printk(KERN_INFO
"sata_oxnas: reseting SATA core\n");
1455 /* core not recovering, reset it */
1457 sata_oxnas_reset_core(ah
);
1459 actions_required
|= OXNAS_SATA_REINIT
;
1460 /* Perform any SATA core re-initialisation after reset post reset init
1461 * needs to be called for both ports as there's one reset for both
1463 for (n
=0; n
< hd
->n_ports
; n
++)
1464 sata_oxnas_post_reset_init(ah
->ports
[n
]);
1467 return actions_required
;
1471 * ata_qc_new - Request an available ATA command, for queueing
1472 * @ap: Port associated with device @dev
1473 * @return non zero will refuse a new command, zero will may grant on subject
1474 * to conditions elsewhere.
1477 static int sata_oxnas_qc_new(struct ata_port
*ap
)
1479 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
1480 DPRINTK("port %d\n", ap
->port_no
);
1482 if (hd
->port_frozen
|| hd
->port_in_eh
)
1485 return !sata_oxnas_acquire_hw(ap
, 0, 0);
1489 * releases the lock on the port the command used
1491 static void sata_oxnas_qc_free(struct ata_queued_cmd
*qc
)
1494 sata_oxnas_release_hw(qc
->ap
);
1497 static void sata_oxnas_freeze(struct ata_port
* ap
)
1499 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
1501 hd
->port_frozen
|= BIT(ap
->port_no
);
1505 static void sata_oxnas_thaw(struct ata_port
* ap
)
1507 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
1509 hd
->port_frozen
&= ~BIT(ap
->port_no
);
1513 void sata_oxnas_freeze_host(struct ata_port
*ap
)
1515 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
1518 hd
->port_in_eh
|= BIT(ap
->port_no
);
1522 void sata_oxnas_thaw_host(struct ata_port
*ap
)
1524 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
1527 hd
->port_in_eh
&= ~BIT(ap
->port_no
);
1531 static void sata_oxnas_post_internal_cmd(struct ata_queued_cmd
*qc
)
1534 /* If the core is busy here, make it idle */
1535 if (qc
->flags
& ATA_QCFLAG_FAILED
)
1536 sata_oxnas_cleanup(qc
->ap
->host
);
1541 * turn on the interrupts
1543 * @param ap Hardware with the registers in
1545 static void sata_oxnas_irq_on(struct ata_port
*ap
)
1547 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1548 u32 mask
= (COREINT_END
<< ap
->port_no
);
1550 /* Clear pending interrupts */
1551 iowrite32(~0, pd
->port_base
+ INT_CLEAR
);
1552 iowrite32(mask
, pd
->core_base
+ CORE_INT_STATUS
);
1555 /* enable End of command interrupt */
1556 iowrite32(INT_WANT
, pd
->port_base
+ INT_ENABLE
);
1557 iowrite32(mask
, pd
->core_base
+ CORE_INT_ENABLE
);
1561 /** @return true if the port has a cable connected */
1562 int sata_oxnas_check_link(struct ata_port
*ap
)
1566 sata_oxnas_scr_read_port(ap
, SCR_STATUS
, ®
);
1567 /* Check for the cable present indicated by SCR status bit-0 set */
1572 * ata_std_postreset - standard postreset callback
1573 * @link: the target ata_link
1574 * @classes: classes of attached devices
1576 * This function is invoked after a successful reset. Note that
1577 * the device might have been reset more than once using
1578 * different reset methods before postreset is invoked.
1581 * Kernel thread context (may sleep)
1583 static void sata_oxnas_postreset(struct ata_link
*link
, unsigned int *classes
)
1585 struct ata_port
*ap
= link
->ap
;
1586 struct sata_oxnas_host_priv
*hd
= ap
->host
->private_data
;
1591 ata_std_postreset(link
, classes
);
1593 /* turn on phy error detection by removing the masks */
1594 sata_oxnas_link_write(ap
->host
->ports
[0], 0x0c, 0x30003);
1595 if (hd
->n_ports
> 1)
1596 sata_oxnas_link_write(ap
->host
->ports
[1], 0x0c, 0x30003);
1598 /* bail out if no device is present */
1599 if (classes
[0] == ATA_DEV_NONE
&& classes
[1] == ATA_DEV_NONE
) {
1600 DPRINTK("EXIT, no device\n");
1604 /* go through all the devices and configure them */
1605 for (dev
= 0; dev
< ATA_MAX_DEVICES
; ++dev
) {
1606 if (ap
->link
.device
[dev
].class == ATA_DEV_ATA
)
1607 sata_oxnas_dev_config(&(ap
->link
.device
[dev
]));
1614 * Called to read the hardware registers / DMA buffers, to
1615 * obtain the current set of taskfile register values.
1616 * @param ap hardware with the registers in
1617 * @param tf taskfile to read the registers into
1619 static void sata_oxnas_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
1621 struct sata_oxnas_port_priv
*port_priv
= ap
->private_data
;
1622 void __iomem
*port_base
= port_priv
->port_base
;
1623 /* read the orb registers */
1624 u32 Orb1
= ioread32(port_base
+ ORB1
);
1625 u32 Orb2
= ioread32(port_base
+ ORB2
);
1626 u32 Orb3
= ioread32(port_base
+ ORB3
);
1627 u32 Orb4
= ioread32(port_base
+ ORB4
);
1629 /* read common 28/48 bit tf parameters */
1630 tf
->device
= (Orb1
>> 24);
1631 tf
->nsect
= (Orb2
>> 0);
1632 tf
->feature
= (Orb2
>> 16);
1633 tf
->command
= sata_oxnas_check_status(ap
);
1635 /* read 48 or 28 bit tf parameters */
1636 if (tf
->flags
& ATA_TFLAG_LBA48
) {
1637 tf
->hob_nsect
= (Orb2
>> 8);
1638 tf
->lbal
= (Orb3
>> 0);
1639 tf
->lbam
= (Orb3
>> 8);
1640 tf
->lbah
= (Orb3
>> 16);
1641 tf
->hob_lbal
= (Orb3
>> 24);
1642 tf
->hob_lbam
= (Orb4
>> 0);
1643 tf
->hob_lbah
= (Orb4
>> 8);
1644 /* feature ext and control are write only */
1646 /* read 28-bit lba */
1647 tf
->lbal
= (Orb3
>> 0);
1648 tf
->lbam
= (Orb3
>> 8);
1649 tf
->lbah
= (Orb3
>> 16);
1654 * Read a result task-file from the sata core registers.
1656 static bool sata_oxnas_qc_fill_rtf(struct ata_queued_cmd
*qc
)
1658 /* Read the most recently received FIS from the SATA core ORB registers
1659 and convert to an ATA taskfile */
1660 sata_oxnas_tf_read(qc
->ap
, &qc
->result_tf
);
1665 * Reads the Status ATA shadow register from hardware.
1667 * @return The status register
1669 static u8
sata_oxnas_check_status(struct ata_port
*ap
)
1673 struct sata_oxnas_port_priv
*port_priv
= ap
->private_data
;
1674 void __iomem
*port_base
= port_priv
->port_base
;
1676 /* read byte 3 of Orb2 register */
1677 status
= ioread32(port_base
+ ORB2
) >> 24;
1679 /* check for the drive going missing indicated by SCR status bits
1681 sata_oxnas_scr_read_port(ap
, SCR_STATUS
, &Reg
);
1691 static inline void sata_oxnas_reset_ucode(struct ata_host
*ah
, int force
, int no_microcode
)
1693 struct sata_oxnas_host_priv
*hd
= ah
->private_data
;
1698 sata_oxnas_set_mode(ah
, UNKNOWN_MODE
, force
);
1699 reg
= ioread32(hd
->core_base
+ DEVICE_CONTROL
);
1700 reg
|= DEVICE_CONTROL_ATA_ERR_OVERRIDE
;
1701 iowrite32(reg
, hd
->core_base
+ DEVICE_CONTROL
);
1704 sata_oxnas_set_mode(ah
, OXNASSATA_NOTRAID
, force
);
1705 /* Turn the work around off as it may have been left on by any
1706 * HW-RAID code that we've been working with */
1707 iowrite32(0x0, hd
->core_base
+ PORT_ERROR_MASK
);
1712 * Prepare as much as possible for a command without involving anything that is
1713 * shared between ports.
1715 static void sata_oxnas_qc_prep(struct ata_queued_cmd
*qc
)
1717 struct sata_oxnas_port_priv
*pd
;
1718 int port_no
= qc
->ap
->port_no
;
1720 /* if the port's not connected, complete now with an error */
1721 if (!sata_oxnas_check_link(qc
->ap
)) {
1722 printk(KERN_ERR
"port %d not connected completing with error\n",
1724 qc
->err_mask
|= AC_ERR_ATA_BUS
;
1725 ata_qc_complete(qc
);
1728 sata_oxnas_reset_ucode(qc
->ap
->host
, 0, 0);
1730 /* both pio and dma commands use dma */
1731 if (ata_is_dma(qc
->tf
.protocol
) || ata_is_pio(qc
->tf
.protocol
)) {
1733 /* program the scatterlist into the prd table */
1734 ata_bmdma_qc_prep(qc
);
1736 /* point the sgdma controller at the dma request structure */
1737 pd
= qc
->ap
->private_data
;
1739 iowrite32(pd
->sgdma_request_pa
,
1740 pd
->sgdma_base
+ SGDMA_REQUESTPTR
);
1742 /* setup the request table */
1744 pd
->sgdma_request
->control
=
1745 (qc
->dma_dir
== DMA_FROM_DEVICE
) ?
1746 SGDMA_REQCTL0IN
: SGDMA_REQCTL0OUT
;
1748 pd
->sgdma_request
->control
=
1749 (qc
->dma_dir
== DMA_FROM_DEVICE
) ?
1750 SGDMA_REQCTL1IN
: SGDMA_REQCTL1OUT
;
1752 pd
->sgdma_request
->qualifier
= SGDMA_REQQUAL
;
1753 pd
->sgdma_request
->src_pa
= qc
->ap
->bmdma_prd_dma
;
1754 pd
->sgdma_request
->dst_pa
= qc
->ap
->bmdma_prd_dma
;
1757 /* tell it to wait */
1758 iowrite32(SGDMA_CONTROL_NOGO
, pd
->sgdma_base
+ SGDMA_CONTROL
);
1762 static int sata_oxnas_port_start(struct ata_port
*ap
)
1764 struct sata_oxnas_host_priv
*host_priv
= ap
->host
->private_data
;
1765 struct device
*dev
= ap
->host
->dev
;
1766 struct sata_oxnas_port_priv
*pp
;
1772 pp
= kzalloc(sizeof(*pp
), GFP_KERNEL
);
1776 pp
->port_base
= host_priv
->port_base
+
1777 (ap
->port_no
? PORT_SIZE
: 0);
1778 pp
->dmactl_base
= host_priv
->dmactl_base
+
1779 (ap
->port_no
? DMA_CORESIZE
: 0);
1780 pp
->sgdma_base
= host_priv
->sgdma_base
+
1781 (ap
->port_no
? SGDMA_CORESIZE
: 0);
1782 pp
->core_base
= host_priv
->core_base
;
1785 if (host_priv
->dma_size
>= SATA_OXNAS_DMA_SIZE
* host_priv
->n_ports
) {
1786 DPRINTK("using preallocated DMA\n");
1787 mem_dma
= host_priv
->dma_base
+
1788 (ap
->port_no
? SATA_OXNAS_DMA_SIZE
: 0);
1789 mem
= ioremap(mem_dma
, SATA_OXNAS_DMA_SIZE
);
1791 mem
= dma_alloc_coherent(dev
, SATA_OXNAS_DMA_SIZE
, &mem_dma
,
1797 pp
->sgdma_request_pa
= mem_dma
;
1798 pp
->sgdma_request
= mem
;
1800 ap
->bmdma_prd_dma
= mem_dma
+ sizeof(struct sgdma_request
);
1801 ap
->bmdma_prd
= mem
+ sizeof(struct sgdma_request
);
1803 ap
->private_data
= pp
;
1805 sata_oxnas_post_reset_init(ap
);
1815 static void sata_oxnas_port_stop(struct ata_port
*ap
)
1817 struct device
*dev
= ap
->host
->dev
;
1818 struct sata_oxnas_port_priv
*pp
= ap
->private_data
;
1819 struct sata_oxnas_host_priv
*host_priv
= ap
->host
->private_data
;
1822 ap
->private_data
= NULL
;
1823 if (host_priv
->dma_size
) {
1824 iounmap(pp
->sgdma_request
);
1826 dma_free_coherent(dev
, SATA_OXNAS_DMA_SIZE
,
1827 pp
->sgdma_request
, pp
->sgdma_request_pa
);
1834 static void sata_oxnas_post_reset_init(struct ata_port
*ap
)
1838 /* force to load u-code only once after reset */
1839 sata_oxnas_reset_ucode(ap
->host
, !ap
->port_no
, 0);
1841 /* turn on phy error detection by removing the masks */
1842 sata_oxnas_link_write(ap
, 0x0C, 0x30003);
1844 /* enable hotplug event detection */
1845 sata_oxnas_scr_write_port(ap
, SCR_ERROR
, ~0);
1846 sata_oxnas_scr_write_port(ap
, SERROR_IRQ_MASK
, 0x03feffff);
1847 sata_oxnas_scr_write_port(ap
, SCR_ACTIVE
, ~0 & ~(1 << 26) & ~(1 << 16));
1849 /* enable interrupts for ports */
1850 sata_oxnas_irq_on(ap
);
1852 /* go through all the devices and configure them */
1853 for (dev
= 0; dev
< ATA_MAX_DEVICES
; ++dev
) {
1854 if (ap
->link
.device
[dev
].class == ATA_DEV_ATA
) {
1855 sata_std_hardreset(&ap
->link
, NULL
, jiffies
+ HZ
);
1856 sata_oxnas_dev_config(&(ap
->link
.device
[dev
]));
1860 /* clean up any remaining errors */
1861 sata_oxnas_scr_write_port(ap
, SCR_ERROR
, ~0);
1866 * host_stop() is called when the rmmod or hot unplug process begins. The
1867 * hook must stop all hardware interrupts, DMA engines, etc.
1869 * @param ap hardware with the registers in
1871 static void sata_oxnas_host_stop(struct ata_host
*host_set
)
1877 #define ERROR_HW_ACQUIRE_TIMEOUT_JIFFIES (10 * HZ)
1878 static void sata_oxnas_error_handler(struct ata_port
*ap
)
1880 DPRINTK("Enter port_no %d\n", ap
->port_no
);
1881 sata_oxnas_freeze_host(ap
);
1883 /* If the core is busy here, make it idle */
1884 sata_oxnas_cleanup(ap
->host
);
1886 ata_std_error_handler(ap
);
1888 sata_oxnas_thaw_host(ap
);
1891 static int sata_oxnas_softreset(struct ata_link
*link
, unsigned int *class,
1892 unsigned long deadline
)
1894 struct ata_port
*ap
= link
->ap
;
1895 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1896 void __iomem
*port_base
= pd
->port_base
;
1899 struct ata_taskfile tf
;
1904 port_base
= pd
->port_base
;
1906 if (ata_link_offline(link
)) {
1907 DPRINTK("PHY reports no device\n");
1908 *class = ATA_DEV_NONE
;
1912 /* write value to register */
1913 iowrite32(0, port_base
+ ORB1
);
1914 iowrite32(0, port_base
+ ORB2
);
1915 iowrite32(0, port_base
+ ORB3
);
1916 iowrite32((ap
->ctl
) << 24, port_base
+ ORB4
);
1918 /* command the core to send a control FIS */
1919 Command_Reg
= ioread32(port_base
+ SATA_COMMAND
);
1920 Command_Reg
&= ~SATA_OPCODE_MASK
;
1921 Command_Reg
|= CMD_WRITE_TO_ORB_REGS_NO_COMMAND
;
1922 iowrite32(Command_Reg
, port_base
+ SATA_COMMAND
);
1923 udelay(20); /* FIXME: flush */
1925 /* write value to register */
1926 iowrite32((ap
->ctl
| ATA_SRST
) << 24, port_base
+ ORB4
);
1928 /* command the core to send a control FIS */
1929 Command_Reg
&= ~SATA_OPCODE_MASK
;
1930 Command_Reg
|= CMD_WRITE_TO_ORB_REGS_NO_COMMAND
;
1931 iowrite32(Command_Reg
, port_base
+ SATA_COMMAND
);
1932 udelay(20); /* FIXME: flush */
1934 /* write value to register */
1935 iowrite32((ap
->ctl
) << 24, port_base
+ ORB4
);
1937 /* command the core to send a control FIS */
1938 Command_Reg
&= ~SATA_OPCODE_MASK
;
1939 Command_Reg
|= CMD_WRITE_TO_ORB_REGS_NO_COMMAND
;
1940 iowrite32(Command_Reg
, port_base
+ SATA_COMMAND
);
1944 rc
= ata_sff_wait_ready(link
, deadline
);
1946 /* if link is occupied, -ENODEV too is an error */
1947 if (rc
&& (rc
!= -ENODEV
|| sata_scr_valid(link
))) {
1948 ata_link_printk(link
, KERN_ERR
, "SRST failed (errno=%d)\n", rc
);
1952 /* determine by signature whether we have ATA or ATAPI devices */
1953 sata_oxnas_tf_read(ap
, &tf
);
1954 *class = ata_dev_classify(&tf
);
1956 if (*class == ATA_DEV_UNKNOWN
)
1957 *class = ATA_DEV_NONE
;
1960 DPRINTK("EXIT, class=%u\n", *class);
1965 int sata_oxnas_init_controller(struct ata_host
*host
)
1973 * This code is a work around for a DMA hardware bug that will repeat the
1974 * penultimate 8-bytes on some reads. This code will check that the amount
1975 * of data transferred is a multiple of 512 bytes, if not the in it will
1976 * fetch the correct data from a buffer in the SATA core and copy it into
1979 * @param port SATA port to check and if necessary, correct.
1981 static int sata_oxnas_bug_6320_workaround(struct ata_port
*ap
)
1983 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
1984 void __iomem
*core_base
= pd
->core_base
;
1986 int quads_transferred
;
1988 int sector_quads_remaining
;
1989 int bug_present
= 0;
1991 /* Only want to apply fix to reads */
1992 is_read
= !(ioread32(core_base
+ DM_DBG1
) & (ap
->port_no
?
1993 BIT(CORE_PORT1_DATA_DIR_BIT
) :
1994 BIT(CORE_PORT0_DATA_DIR_BIT
)));
1996 /* Check for an incomplete transfer, i.e. not a multiple of 512 bytes
1997 transferred (datacount_port register counts quads transferred) */
1999 ioread32(core_base
+ (ap
->port_no
?
2000 DATACOUNT_PORT1
: DATACOUNT_PORT0
));
2002 remainder
= quads_transferred
& 0x7f;
2003 sector_quads_remaining
= remainder
? (0x80 - remainder
) : 0;
2005 if (is_read
&& (sector_quads_remaining
== 2)) {
2007 } else if (sector_quads_remaining
) {
2009 printk(KERN_WARNING
"SATA read fixup cannot deal with" \
2010 " %d quads remaining\n",
2011 sector_quads_remaining
);
2013 printk(KERN_WARNING
"SATA write fixup of %d quads" \
2014 " remaining not supported\n",
2015 sector_quads_remaining
);
2022 /* This port done an interrupt */
2023 static void sata_oxnas_port_irq(struct ata_port
*ap
, int force_error
)
2025 struct ata_queued_cmd
*qc
;
2026 struct sata_oxnas_port_priv
*pd
= ap
->private_data
;
2027 void __iomem
*port_base
= pd
->port_base
;
2030 unsigned long flags
= 0;
2032 DPRINTK("ENTER port %d irqstatus %x\n", ap
->port_no
, ioread32(port_base
+ INT_STATUS
));
2034 if (ap
->qc_active
& (1 << ATA_TAG_INTERNAL
)) {
2035 qc
= ata_qc_from_tag(ap
, ATA_TAG_INTERNAL
);
2036 DPRINTK("completing non-ncq cmd\n");
2039 ata_qc_complete(qc
);
2044 qc
= ata_qc_from_tag(ap
, ap
->link
.active_tag
);
2047 /* record the port's interrupt */
2048 int_status
= ioread32(port_base
+ INT_STATUS
);
2050 /* If there's no command associated with this IRQ, ignore it. We may get
2051 * spurious interrupts when cleaning-up after a failed command, ignore
2054 /* get the status before any error cleanup */
2055 qc
->err_mask
= ac_err_mask(sata_oxnas_check_status(ap
));
2057 /* Pretend there has been a link error */
2058 qc
->err_mask
|= AC_ERR_ATA_BUS
;
2059 DPRINTK(" ####force error####\n");
2061 /* tell libata we're done */
2062 local_irq_save(flags
);
2063 sata_oxnas_irq_clear(ap
);
2064 local_irq_restore(flags
);
2065 ata_qc_complete(qc
);
2067 VPRINTK("Ignoring interrupt, can't find the command tag=" \
2068 "%d %08x\n", ap
->link
.active_tag
, ap
->qc_active
);
2071 /* maybe a hotplug event */
2072 if (unlikely(int_status
& INT_LINK_SERROR
)) {
2075 sata_oxnas_scr_read_port(ap
, SCR_ERROR
, &serror
);
2076 if (serror
& (SERR_DEV_XCHG
| SERR_PHYRDY_CHG
)) {
2077 ata_ehi_hotplugged(&ap
->link
.eh_info
);
2078 ata_port_freeze(ap
);
2084 * irq_handler is the interrupt handling routine registered with the system,
2087 static irqreturn_t
sata_oxnas_interrupt(int irq
, void *dev_instance
)
2089 struct ata_host
*ah
= dev_instance
;
2090 struct sata_oxnas_host_priv
*hd
= ah
->private_data
;
2091 void __iomem
*core_base
= hd
->core_base
;
2094 irqreturn_t ret
= IRQ_NONE
;
2099 /* loop until there are no more interrupts */
2100 while ((int_status
= (ioread32(core_base
+ CORE_INT_STATUS
)) &
2101 (COREINT_END
| (COREINT_END
<< 1)) )) {
2103 /* clear any interrupt */
2104 iowrite32(int_status
, core_base
+ CORE_INT_CLEAR
);
2106 /* Only need workaround_bug_6320 for single disk systems as dual
2107 * disk will use uCode which prevents this read underrun problem
2109 * All single disk systems will use port 0 */
2110 for (port_no
= 0; port_no
< hd
->n_ports
; ++port_no
) {
2111 /* check the raw end of command interrupt to see if the
2113 mask
= (COREINT_END
<< port_no
);
2114 if (int_status
& mask
) {
2115 /* this port had an interrupt, clear it */
2116 iowrite32(mask
, core_base
+ CORE_INT_CLEAR
);
2117 bug_present
= ( hd
->current_ucode
== UNKNOWN_MODE
) ?
2118 sata_oxnas_bug_6320_workaround(
2119 ah
->ports
[port_no
]) : 0;
2120 sata_oxnas_port_irq(ah
->ports
[port_no
],
2131 * scsi mid-layer and libata interface structures
2133 static struct scsi_host_template sata_oxnas_sht
= {
2134 ATA_NCQ_SHT("sata_oxnas"),
2135 .can_queue
= SATA_OXNAS_QUEUE_DEPTH
,
2136 .sg_tablesize
= SATA_OXNAS_MAX_PRD
,
2137 .dma_boundary
= ATA_DMA_BOUNDARY
,
2138 .unchecked_isa_dma
= 0,
2142 static struct ata_port_operations sata_oxnas_ops
= {
2143 .inherits
= &sata_port_ops
,
2144 .qc_prep
= sata_oxnas_qc_prep
,
2145 .qc_issue
= sata_oxnas_qc_issue
,
2146 .qc_fill_rtf
= sata_oxnas_qc_fill_rtf
,
2147 .qc_new
= sata_oxnas_qc_new
,
2148 .qc_free
= sata_oxnas_qc_free
,
2150 .scr_read
= sata_oxnas_scr_read
,
2151 .scr_write
= sata_oxnas_scr_write
,
2153 .freeze
= sata_oxnas_freeze
,
2154 .thaw
= sata_oxnas_thaw
,
2155 .softreset
= sata_oxnas_softreset
,
2156 /* .hardreset = sata_oxnas_hardreset, */
2157 .postreset
= sata_oxnas_postreset
,
2158 .error_handler
= sata_oxnas_error_handler
,
2159 .post_internal_cmd
= sata_oxnas_post_internal_cmd
,
2161 .port_start
= sata_oxnas_port_start
,
2162 .port_stop
= sata_oxnas_port_stop
,
2164 .host_stop
= sata_oxnas_host_stop
,
2165 /* .pmp_attach = sata_oxnas_pmp_attach, */
2166 /* .pmp_detach = sata_oxnas_pmp_detach, */
2167 .sff_check_status
= sata_oxnas_check_status
,
2168 .acquire_hw
= sata_oxnas_acquire_hw
,
2171 static const struct ata_port_info sata_oxnas_port_info
= {
2172 .flags
= SATA_OXNAS_HOST_FLAGS
,
2173 .pio_mask
= ATA_PIO4
,
2174 .udma_mask
= ATA_UDMA6
,
2175 .port_ops
= &sata_oxnas_ops
,
2178 static int sata_oxnas_probe(struct platform_device
*ofdev
)
2180 int retval
= -ENXIO
;
2182 void __iomem
*port_base
= NULL
;
2183 void __iomem
*dmactl_base
= NULL
;
2184 void __iomem
*sgdma_base
= NULL
;
2185 void __iomem
*core_base
= NULL
;
2186 void __iomem
*phy_base
= NULL
;
2187 struct reset_control
*rstc
;
2189 struct resource res
= {};
2190 struct sata_oxnas_host_priv
*host_priv
= NULL
;
2192 struct ata_host
*host
= NULL
;
2193 struct clk
*clk
= NULL
;
2195 const struct ata_port_info
*ppi
[] = { &sata_oxnas_port_info
, NULL
};
2196 const struct ata_port_info
*dppi
[] = { &sata_oxnas_port_info
, &sata_oxnas_port_info
, NULL
};
2198 of_property_read_u32(ofdev
->dev
.of_node
, "nr-ports", &n_ports
);
2199 if (n_ports
< 1 || n_ports
> SATA_OXNAS_MAX_PORTS
)
2200 goto error_exit_with_cleanup
;
2202 port_base
= of_iomap(ofdev
->dev
.of_node
, 0);
2204 goto error_exit_with_cleanup
;
2206 dmactl_base
= of_iomap(ofdev
->dev
.of_node
, 1);
2208 goto error_exit_with_cleanup
;
2210 sgdma_base
= of_iomap(ofdev
->dev
.of_node
, 2);
2212 goto error_exit_with_cleanup
;
2214 core_base
= of_iomap(ofdev
->dev
.of_node
, 3);
2216 goto error_exit_with_cleanup
;
2218 phy_base
= of_iomap(ofdev
->dev
.of_node
, 4);
2220 goto error_exit_with_cleanup
;
2222 host_priv
= devm_kzalloc(&ofdev
->dev
,
2223 sizeof(struct sata_oxnas_host_priv
),
2226 goto error_exit_with_cleanup
;
2228 host_priv
->port_base
= port_base
;
2229 host_priv
->dmactl_base
= dmactl_base
;
2230 host_priv
->sgdma_base
= sgdma_base
;
2231 host_priv
->core_base
= core_base
;
2232 host_priv
->phy_base
= phy_base
;
2233 host_priv
->n_ports
= n_ports
;
2234 host_priv
->current_ucode
= UNKNOWN_MODE
;
2236 if (!of_address_to_resource(ofdev
->dev
.of_node
, 5, &res
)) {
2237 host_priv
->dma_base
= res
.start
;
2238 host_priv
->dma_size
= resource_size(&res
);
2241 irq
= irq_of_parse_and_map(ofdev
->dev
.of_node
, 0);
2243 dev_err(&ofdev
->dev
, "invalid irq from platform\n");
2244 goto error_exit_with_cleanup
;
2246 host_priv
->irq
= irq
;
2248 clk
= of_clk_get(ofdev
->dev
.of_node
, 0);
2250 retval
= PTR_ERR(clk
);
2252 goto error_exit_with_cleanup
;
2254 host_priv
->clk
= clk
;
2256 rstc
= devm_reset_control_get(&ofdev
->dev
, "sata");
2258 retval
= PTR_ERR(rstc
);
2259 goto error_exit_with_cleanup
;
2261 host_priv
->rst_sata
= rstc
;
2263 rstc
= devm_reset_control_get(&ofdev
->dev
, "link");
2265 retval
= PTR_ERR(rstc
);
2266 goto error_exit_with_cleanup
;
2268 host_priv
->rst_link
= rstc
;
2270 rstc
= devm_reset_control_get(&ofdev
->dev
, "phy");
2272 retval
= PTR_ERR(rstc
);
2273 goto error_exit_with_cleanup
;
2275 host_priv
->rst_phy
= rstc
;
2277 /* allocate host structure */
2278 host
= ata_host_alloc_pinfo(&ofdev
->dev
, n_ports
>1 ? dppi
: ppi
,
2283 goto error_exit_with_cleanup
;
2285 host
->private_data
= host_priv
;
2286 host
->iomap
= port_base
;
2288 /* initialize core locking and queues */
2289 init_waitqueue_head(&host_priv
->fast_wait_queue
);
2290 init_waitqueue_head(&host_priv
->scsi_wait_queue
);
2291 spin_lock_init(&host_priv
->phy_lock
);
2292 spin_lock_init(&host_priv
->core_lock
);
2293 host_priv
->core_locked
= 0;
2294 host_priv
->reentrant_port_no
= -1;
2295 host_priv
->hw_lock_count
= 0;
2296 host_priv
->direct_lock_count
= 0;
2297 host_priv
->locker_uid
= 0;
2298 host_priv
->current_locker_type
= SATA_UNLOCKED
;
2299 host_priv
->isr_arg
= NULL
;
2300 host_priv
->isr_callback
= NULL
;
2302 /* initialize host controller */
2303 retval
= sata_oxnas_init_controller(host
);
2305 goto error_exit_with_cleanup
;
2308 * Now, register with libATA core, this will also initiate the
2309 * device discovery process, invoking our port_start() handler &
2310 * error_handler() to execute a dummy softreset EH session
2312 ata_host_activate(host
, irq
, sata_oxnas_interrupt
, SATA_OXNAS_IRQ_FLAG
,
2317 error_exit_with_cleanup
:
2319 irq_dispose_mapping(host_priv
->irq
);
2323 ata_host_detach(host
);
2327 iounmap(sgdma_base
);
2336 static int sata_oxnas_remove(struct platform_device
*ofdev
)
2338 struct ata_host
*host
= dev_get_drvdata(&ofdev
->dev
);
2339 struct sata_oxnas_host_priv
*host_priv
= host
->private_data
;
2341 ata_host_detach(host
);
2343 irq_dispose_mapping(host_priv
->irq
);
2344 iounmap(host_priv
->port_base
);
2345 iounmap(host_priv
->sgdma_base
);
2346 iounmap(host_priv
->core_base
);
2348 /* reset Controller, Link and PHY */
2349 reset_control_assert(host_priv
->rst_sata
);
2350 reset_control_assert(host_priv
->rst_link
);
2351 reset_control_assert(host_priv
->rst_phy
);
2353 /* Disable the clock to the SATA block */
2354 clk_disable_unprepare(host_priv
->clk
);
2355 clk_put(host_priv
->clk
);
2361 static int sata_oxnas_suspend(struct platform_device
*op
, pm_message_t state
)
2363 struct ata_host
*host
= dev_get_drvdata(&op
->dev
);
2365 return ata_host_suspend(host
, state
);
2368 static int sata_oxnas_resume(struct platform_device
*op
)
2370 struct ata_host
*host
= dev_get_drvdata(&op
->dev
);
2373 ret
= sata_oxnas_init_controller(host
);
2375 dev_err(&op
->dev
, "Error initializing hardware\n");
2378 ata_host_resume(host
);
2385 static struct of_device_id oxnas_sata_match
[] = {
2387 .compatible
= "plxtech,nas782x-sata",
2392 MODULE_DEVICE_TABLE(of
, oxnas_sata_match
);
2394 static struct platform_driver oxnas_sata_driver
= {
2396 .name
= "oxnas-sata",
2397 .owner
= THIS_MODULE
,
2398 .of_match_table
= oxnas_sata_match
,
2400 .probe
= sata_oxnas_probe
,
2401 .remove
= sata_oxnas_remove
,
2403 .suspend
= sata_oxnas_suspend
,
2404 .resume
= sata_oxnas_resume
,
2408 module_platform_driver(oxnas_sata_driver
);
2410 MODULE_LICENSE("GPL");
2411 MODULE_VERSION("1.0");
2412 MODULE_AUTHOR("Oxford Semiconductor Ltd.");
2413 MODULE_DESCRIPTION("934 SATA core controler");