2 * Driver for Blackfin on-chip ATAPI controller.
4 * Enter bugs at http://blackfin.uclinux.org/
6 * Copyright (c) 2008 Analog Devices Inc.
8 * Licensed under the GPL-2 or later.
14 #include <asm/byteorder.h>
16 #include <asm/errno.h>
17 #include <asm/mach-common/bits/pata.h>
20 #include "pata_bfin.h"
22 static struct ata_port port[CONFIG_SYS_SATA_MAX_DEVICE];
25 * PIO Mode - Frequency compatibility
28 static const u32 pio_fsclk[] =
29 { 33333333, 33333333, 33333333, 33333333, 33333333 };
32 * MDMA Mode - Frequency compatibility
35 static const u32 mdma_fsclk[] = { 33333333, 33333333, 33333333 };
38 * UDMA Mode - Frequency compatibility
40 * UDMA5 - 100 MB/s - SCLK = 133 MHz
41 * UDMA4 - 66 MB/s - SCLK >= 80 MHz
42 * UDMA3 - 44.4 MB/s - SCLK >= 50 MHz
43 * UDMA2 - 33 MB/s - SCLK >= 40 MHz
45 /* mode: 0 1 2 3 4 5 */
46 static const u32 udma_fsclk[] =
47 { 33333333, 33333333, 40000000, 50000000, 80000000, 133333333 };
50 * Register transfer timing table
54 static const u32 reg_t0min[] = { 600, 383, 330, 180, 120 };
55 /* DIOR/DIOW to end cycle */
56 static const u32 reg_t2min[] = { 290, 290, 290, 70, 25 };
57 /* DIOR/DIOW asserted pulse width */
58 static const u32 reg_teocmin[] = { 290, 290, 290, 80, 70 };
65 static const u32 pio_t0min[] = { 600, 383, 240, 180, 120 };
66 /* Address valid to DIOR/DIORW */
67 static const u32 pio_t1min[] = { 70, 50, 30, 30, 25 };
68 /* DIOR/DIOW to end cycle */
69 static const u32 pio_t2min[] = { 165, 125, 100, 80, 70 };
70 /* DIOR/DIOW asserted pulse width */
71 static const u32 pio_teocmin[] = { 165, 125, 100, 70, 25 };
73 static const u32 pio_t4min[] = { 30, 20, 15, 10, 10 };
75 /* ******************************************************************
76 * Multiword DMA timing table
77 * ******************************************************************
81 static const u32 mdma_t0min[] = { 480, 150, 120 };
82 /* DIOR/DIOW asserted pulse width */
83 static const u32 mdma_tdmin[] = { 215, 80, 70 };
84 /* DMACK to read data released */
85 static const u32 mdma_thmin[] = { 20, 15, 10 };
86 /* DIOR/DIOW to DMACK hold */
87 static const u32 mdma_tjmin[] = { 20, 5, 5 };
88 /* DIOR negated pulse width */
89 static const u32 mdma_tkrmin[] = { 50, 50, 25 };
90 /* DIOR negated pulse width */
91 static const u32 mdma_tkwmin[] = { 215, 50, 25 };
92 /* CS[1:0] valid to DIOR/DIOW */
93 static const u32 mdma_tmmin[] = { 50, 30, 25 };
94 /* DMACK to read data released */
95 static const u32 mdma_tzmax[] = { 20, 25, 25 };
98 * Ultra DMA timing table
100 /* mode: 0 1 2 3 4 5 */
101 static const u32 udma_tcycmin[] = { 112, 73, 54, 39, 25, 17 };
102 static const u32 udma_tdvsmin[] = { 70, 48, 31, 20, 7, 5 };
103 static const u32 udma_tenvmax[] = { 70, 70, 70, 55, 55, 50 };
104 static const u32 udma_trpmin[] = { 160, 125, 100, 100, 100, 85 };
105 static const u32 udma_tmin[] = { 5, 5, 5, 5, 3, 3 };
108 static const u32 udma_tmlimin = 20;
109 static const u32 udma_tzahmin = 20;
110 static const u32 udma_tenvmin = 20;
111 static const u32 udma_tackmin = 20;
112 static const u32 udma_tssmin = 50;
114 static void msleep(int count)
118 for (i = 0; i < count; i++)
124 * Function: num_clocks_min
127 * calculate number of SCLK cycles to meet minimum timing
129 static unsigned short num_clocks_min(unsigned long tmin,
133 unsigned short result;
135 tmp = tmin * (fsclk/1000/1000) / 1000;
136 result = (unsigned short)tmp;
137 if ((tmp*1000*1000) < (tmin*(fsclk/1000)))
144 * bfin_set_piomode - Initialize host controller PATA PIO timings
145 * @ap: Port whose timings we are configuring
148 * Set PIO mode for device.
151 * None (inherited from caller).
154 static void bfin_set_piomode(struct ata_port *ap, int pio_mode)
156 int mode = pio_mode - XFER_PIO_0;
157 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
158 unsigned int fsclk = get_sclk();
159 unsigned short teoc_reg, t2_reg, teoc_pio;
160 unsigned short t4_reg, t2_pio, t1_reg;
161 unsigned short n0, n6, t6min = 5;
163 /* the most restrictive timing value is t6 and tc, the DIOW - data hold
164 * If one SCLK pulse is longer than this minimum value then register
165 * transfers cannot be supported at this frequency.
167 n6 = num_clocks_min(t6min, fsclk);
168 if (mode >= 0 && mode <= 4 && n6 >= 1) {
169 debug("set piomode: mode=%d, fsclk=%ud\n", mode, fsclk);
170 /* calculate the timing values for register transfers. */
171 while (mode > 0 && pio_fsclk[mode] > fsclk)
174 /* DIOR/DIOW to end cycle time */
175 t2_reg = num_clocks_min(reg_t2min[mode], fsclk);
176 /* DIOR/DIOW asserted pulse width */
177 teoc_reg = num_clocks_min(reg_teocmin[mode], fsclk);
179 n0 = num_clocks_min(reg_t0min[mode], fsclk);
181 /* increase t2 until we meed the minimum cycle length */
182 if (t2_reg + teoc_reg < n0)
183 t2_reg = n0 - teoc_reg;
185 /* calculate the timing values for pio transfers. */
187 /* DIOR/DIOW to end cycle time */
188 t2_pio = num_clocks_min(pio_t2min[mode], fsclk);
189 /* DIOR/DIOW asserted pulse width */
190 teoc_pio = num_clocks_min(pio_teocmin[mode], fsclk);
192 n0 = num_clocks_min(pio_t0min[mode], fsclk);
194 /* increase t2 until we meed the minimum cycle length */
195 if (t2_pio + teoc_pio < n0)
196 t2_pio = n0 - teoc_pio;
198 /* Address valid to DIOR/DIORW */
199 t1_reg = num_clocks_min(pio_t1min[mode], fsclk);
202 t4_reg = num_clocks_min(pio_t4min[mode], fsclk);
204 ATAPI_SET_REG_TIM_0(base, (teoc_reg<<8 | t2_reg));
205 ATAPI_SET_PIO_TIM_0(base, (t4_reg<<12 | t2_pio<<4 | t1_reg));
206 ATAPI_SET_PIO_TIM_1(base, teoc_pio);
208 ATAPI_SET_CONTROL(base,
209 ATAPI_GET_CONTROL(base) | IORDY_EN);
211 ATAPI_SET_CONTROL(base,
212 ATAPI_GET_CONTROL(base) & ~IORDY_EN);
215 /* Disable host ATAPI PIO interrupts */
216 ATAPI_SET_INT_MASK(base, ATAPI_GET_INT_MASK(base)
217 & ~(PIO_DONE_MASK | HOST_TERM_XFER_MASK));
224 * Function: wait_complete
226 * Description: Waits the interrupt from device
229 static inline void wait_complete(void __iomem *base, unsigned short mask)
231 unsigned short status;
234 for (i = 0; i < PATA_BFIN_WAIT_TIMEOUT; i++) {
235 status = ATAPI_GET_INT_STATUS(base) & mask;
240 ATAPI_SET_INT_STATUS(base, mask);
245 * Function: write_atapi_register
247 * Description: Writes to ATA Device Resgister
251 static void write_atapi_register(void __iomem *base,
252 unsigned long ata_reg, unsigned short value)
254 /* Program the ATA_DEV_TXBUF register with write data (to be
255 * written into the device).
257 ATAPI_SET_DEV_TXBUF(base, value);
259 /* Program the ATA_DEV_ADDR register with address of the
260 * device register (0x01 to 0x0F).
262 ATAPI_SET_DEV_ADDR(base, ata_reg);
264 /* Program the ATA_CTRL register with dir set to write (1)
266 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
268 /* ensure PIO DMA is not set */
269 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
271 /* and start the transfer */
272 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
274 /* Wait for the interrupt to indicate the end of the transfer.
275 * (We need to wait on and clear rhe ATA_DEV_INT interrupt status)
277 wait_complete(base, PIO_DONE_INT);
282 * Function: read_atapi_register
284 *Description: Reads from ATA Device Resgister
288 static unsigned short read_atapi_register(void __iomem *base,
289 unsigned long ata_reg)
291 /* Program the ATA_DEV_ADDR register with address of the
292 * device register (0x01 to 0x0F).
294 ATAPI_SET_DEV_ADDR(base, ata_reg);
296 /* Program the ATA_CTRL register with dir set to read (0) and
298 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
300 /* ensure PIO DMA is not set */
301 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
303 /* and start the transfer */
304 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
306 /* Wait for the interrupt to indicate the end of the transfer.
307 * (PIO_DONE interrupt is set and it doesn't seem to matter
308 * that we don't clear it)
310 wait_complete(base, PIO_DONE_INT);
312 /* Read the ATA_DEV_RXBUF register with write data (to be
313 * written into the device).
315 return ATAPI_GET_DEV_RXBUF(base);
320 * Function: write_atapi_register_data
322 * Description: Writes to ATA Device Resgister
326 static void write_atapi_data(void __iomem *base,
327 int len, unsigned short *buf)
331 /* Set transfer length to 1 */
332 ATAPI_SET_XFER_LEN(base, 1);
334 /* Program the ATA_DEV_ADDR register with address of the
337 ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
339 /* Program the ATA_CTRL register with dir set to write (1)
341 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | XFER_DIR));
343 /* ensure PIO DMA is not set */
344 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
346 for (i = 0; i < len; i++) {
347 /* Program the ATA_DEV_TXBUF register with write data (to be
348 * written into the device).
350 ATAPI_SET_DEV_TXBUF(base, buf[i]);
352 /* and start the transfer */
353 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
355 /* Wait for the interrupt to indicate the end of the transfer.
356 * (We need to wait on and clear rhe ATA_DEV_INT
359 wait_complete(base, PIO_DONE_INT);
365 * Function: read_atapi_register_data
367 * Description: Reads from ATA Device Resgister
371 static void read_atapi_data(void __iomem *base,
372 int len, unsigned short *buf)
376 /* Set transfer length to 1 */
377 ATAPI_SET_XFER_LEN(base, 1);
379 /* Program the ATA_DEV_ADDR register with address of the
382 ATAPI_SET_DEV_ADDR(base, ATA_REG_DATA);
384 /* Program the ATA_CTRL register with dir set to read (0) and
386 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~XFER_DIR));
388 /* ensure PIO DMA is not set */
389 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) & ~PIO_USE_DMA));
391 for (i = 0; i < len; i++) {
392 /* and start the transfer */
393 ATAPI_SET_CONTROL(base, (ATAPI_GET_CONTROL(base) | PIO_START));
395 /* Wait for the interrupt to indicate the end of the transfer.
396 * (PIO_DONE interrupt is set and it doesn't seem to matter
397 * that we don't clear it)
399 wait_complete(base, PIO_DONE_INT);
401 /* Read the ATA_DEV_RXBUF register with write data (to be
402 * written into the device).
404 buf[i] = ATAPI_GET_DEV_RXBUF(base);
409 * bfin_check_status - Read device status reg & clear interrupt
410 * @ap: port where the device is
412 * Note: Original code is ata_check_status().
415 static u8 bfin_check_status(struct ata_port *ap)
417 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
418 return read_atapi_register(base, ATA_REG_STATUS);
422 * bfin_check_altstatus - Read device alternate status reg
423 * @ap: port where the device is
426 static u8 bfin_check_altstatus(struct ata_port *ap)
428 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
429 return read_atapi_register(base, ATA_REG_ALTSTATUS);
433 * bfin_ata_busy_wait - Wait for a port status register
434 * @ap: Port to wait for.
435 * @bits: bits that must be clear
436 * @max: number of 10uS waits to perform
438 * Waits up to max*10 microseconds for the selected bits in the port's
439 * status register to be cleared.
440 * Returns final value of status register.
443 * Inherited from caller.
445 static inline u8 bfin_ata_busy_wait(struct ata_port *ap, unsigned int bits,
446 unsigned int max, u8 usealtstatus)
453 status = bfin_check_altstatus(ap);
455 status = bfin_check_status(ap);
457 } while (status != 0xff && (status & bits) && (max > 0));
463 * bfin_ata_busy_sleep - sleep until BSY clears, or timeout
464 * @ap: port containing status register to be polled
465 * @tmout_pat: impatience timeout in msecs
466 * @tmout: overall timeout in msecs
468 * Sleep until ATA Status register bit BSY clears,
469 * or a timeout occurs.
472 * 0 on success, -errno otherwise.
474 static int bfin_ata_busy_sleep(struct ata_port *ap,
475 long tmout_pat, unsigned long tmout)
479 status = bfin_ata_busy_wait(ap, ATA_BUSY, 300, 0);
480 while (status != 0xff && (status & ATA_BUSY) && tmout_pat > 0) {
483 status = bfin_ata_busy_wait(ap, ATA_BUSY, 3, 0);
486 if (status != 0xff && (status & ATA_BUSY))
487 printf("port is slow to respond, please be patient "
488 "(Status 0x%x)\n", status);
490 while (status != 0xff && (status & ATA_BUSY) && tmout_pat > 0) {
493 status = bfin_check_status(ap);
499 if (status & ATA_BUSY) {
500 printf("port failed to respond "
501 "(%lu secs, Status 0x%x)\n",
502 DIV_ROUND_UP(tmout, 1000), status);
510 * bfin_dev_select - Select device 0/1 on ATA bus
511 * @ap: ATA channel to manipulate
512 * @device: ATA device (numbered from zero) to select
514 * Note: Original code is ata_sff_dev_select().
517 static void bfin_dev_select(struct ata_port *ap, unsigned int device)
519 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
524 tmp = ATA_DEVICE_OBS;
526 tmp = ATA_DEVICE_OBS | ATA_DEV1;
528 write_atapi_register(base, ATA_REG_DEVICE, tmp);
533 * bfin_devchk - PATA device presence detection
534 * @ap: ATA channel to examine
535 * @device: Device to examine (starting at zero)
537 * Note: Original code is ata_devchk().
540 static unsigned int bfin_devchk(struct ata_port *ap,
543 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
546 bfin_dev_select(ap, device);
548 write_atapi_register(base, ATA_REG_NSECT, 0x55);
549 write_atapi_register(base, ATA_REG_LBAL, 0xaa);
551 write_atapi_register(base, ATA_REG_NSECT, 0xaa);
552 write_atapi_register(base, ATA_REG_LBAL, 0x55);
554 write_atapi_register(base, ATA_REG_NSECT, 0x55);
555 write_atapi_register(base, ATA_REG_LBAL, 0xaa);
557 nsect = read_atapi_register(base, ATA_REG_NSECT);
558 lbal = read_atapi_register(base, ATA_REG_LBAL);
560 if ((nsect == 0x55) && (lbal == 0xaa))
561 return 1; /* we found a device */
563 return 0; /* nothing found */
567 * bfin_bus_post_reset - PATA device post reset
569 * Note: Original code is ata_bus_post_reset().
572 static void bfin_bus_post_reset(struct ata_port *ap, unsigned int devmask)
574 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
575 unsigned int dev0 = devmask & (1 << 0);
576 unsigned int dev1 = devmask & (1 << 1);
579 /* if device 0 was found in ata_devchk, wait for its
583 bfin_ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
585 /* if device 1 was found in ata_devchk, wait for
586 * register access, then wait for BSY to clear
588 deadline = ATA_TMOUT_BOOT;
592 bfin_dev_select(ap, 1);
593 nsect = read_atapi_register(base, ATA_REG_NSECT);
594 lbal = read_atapi_register(base, ATA_REG_LBAL);
595 if ((nsect == 1) && (lbal == 1))
601 msleep(50); /* give drive a breather */
605 bfin_ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
607 /* is all this really necessary? */
608 bfin_dev_select(ap, 0);
610 bfin_dev_select(ap, 1);
612 bfin_dev_select(ap, 0);
616 * bfin_bus_softreset - PATA device software reset
618 * Note: Original code is ata_bus_softreset().
621 static unsigned int bfin_bus_softreset(struct ata_port *ap,
622 unsigned int devmask)
624 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
626 /* software reset. causes dev0 to be selected */
627 write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg);
629 write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg | ATA_SRST);
631 write_atapi_register(base, ATA_REG_CTRL, ap->ctl_reg);
633 /* spec mandates ">= 2ms" before checking status.
634 * We wait 150ms, because that was the magic delay used for
635 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
636 * between when the ATA command register is written, and then
637 * status is checked. Because waiting for "a while" before
638 * checking status is fine, post SRST, we perform this magic
639 * delay here as well.
641 * Old drivers/ide uses the 2mS rule and then waits for ready
645 /* Before we perform post reset processing we want to see if
646 * the bus shows 0xFF because the odd clown forgets the D7
649 if (bfin_check_status(ap) == 0xFF)
652 bfin_bus_post_reset(ap, devmask);
658 * bfin_softreset - reset host port via ATA SRST
661 * Note: Original code is ata_sff_softreset().
664 static int bfin_softreset(struct ata_port *ap)
666 unsigned int err_mask;
670 /* determine if device 0/1 are present.
671 * only one device is supported on one port by now.
673 if (bfin_devchk(ap, 0))
674 ap->dev_mask |= (1 << 0);
675 else if (bfin_devchk(ap, 1))
676 ap->dev_mask |= (1 << 1);
680 /* select device 0 again */
681 bfin_dev_select(ap, 0);
683 /* issue bus reset */
684 err_mask = bfin_bus_softreset(ap, ap->dev_mask);
686 printf("SRST failed (err_mask=0x%x)\n",
696 * bfin_irq_clear - Clear ATAPI interrupt.
697 * @ap: Port associated with this ATA transaction.
699 * Note: Original code is ata_sff_irq_clear().
702 static void bfin_irq_clear(struct ata_port *ap)
704 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
706 ATAPI_SET_INT_STATUS(base, ATAPI_GET_INT_STATUS(base)|ATAPI_DEV_INT
707 | MULTI_DONE_INT | UDMAIN_DONE_INT | UDMAOUT_DONE_INT
708 | MULTI_TERM_INT | UDMAIN_TERM_INT | UDMAOUT_TERM_INT);
711 static u8 bfin_wait_for_irq(struct ata_port *ap, unsigned int max)
713 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
716 if (ATAPI_GET_INT_STATUS(base) & (ATAPI_DEV_INT
717 | MULTI_DONE_INT | UDMAIN_DONE_INT | UDMAOUT_DONE_INT
718 | MULTI_TERM_INT | UDMAIN_TERM_INT | UDMAOUT_TERM_INT)) {
729 * bfin_ata_reset_port - initialize BFIN ATAPI port.
732 static int bfin_ata_reset_port(struct ata_port *ap)
734 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
736 unsigned short status;
738 /* Disable all ATAPI interrupts */
739 ATAPI_SET_INT_MASK(base, 0);
742 /* Assert the RESET signal 25us*/
743 ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) | DEV_RST);
746 /* Negate the RESET signal for 2ms*/
747 ATAPI_SET_CONTROL(base, ATAPI_GET_CONTROL(base) & ~DEV_RST);
750 /* Wait on Busy flag to clear */
753 status = read_atapi_register(base, ATA_REG_STATUS);
754 } while (--count && (status & ATA_BUSY));
756 /* Enable only ATAPI Device interrupt */
757 ATAPI_SET_INT_MASK(base, 1);
765 * Function: bfin_config_atapi_gpio
767 * Description: Configures the ATAPI pins for use
770 static int bfin_config_atapi_gpio(struct ata_port *ap)
772 bfin_write_PORTH_FER(bfin_read_PORTH_FER() | 0x4);
773 bfin_write_PORTH_MUX(bfin_read_PORTH_MUX() & ~0x30);
774 bfin_write_PORTH_DIR_SET(0x4);
776 bfin_write_PORTJ_FER(0x7f8);
777 bfin_write_PORTJ_MUX(bfin_read_PORTI_MUX() & ~0x3fffc0);
778 bfin_write_PORTJ_DIR_SET(0x5f8);
779 bfin_write_PORTJ_DIR_CLEAR(0x200);
780 bfin_write_PORTJ_INEN(0x200);
782 bfin_write_PINT2_ASSIGN(0x0707);
783 bfin_write_PINT2_MASK_SET(0x200);
790 * bfin_atapi_probe - attach a bfin atapi interface
791 * @pdev: platform device
793 * Register a bfin atapi interface.
796 * Platform devices are expected to contain 2 resources per port:
798 * - I/O Base (IORESOURCE_IO)
799 * - IRQ (IORESOURCE_IRQ)
802 static int bfin_ata_probe_port(struct ata_port *ap)
804 if (bfin_config_atapi_gpio(ap)) {
805 printf("Requesting Peripherals faild\n");
809 if (bfin_ata_reset_port(ap)) {
810 printf("Fail to reset ATAPI device\n");
814 if (ap->ata_mode >= XFER_PIO_0 && ap->ata_mode <= XFER_PIO_4)
815 bfin_set_piomode(ap, ap->ata_mode);
817 printf("Given ATA data transfer mode is not supported.\n");
824 #define ATA_SECTOR_WORDS (ATA_SECT_SIZE/2)
826 static void bfin_ata_identify(struct ata_port *ap, int dev)
828 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
830 static u16 iobuf[ATA_SECTOR_WORDS];
832 hd_driveid_t *iop = (hd_driveid_t *)iobuf;
834 memset(iobuf, 0, sizeof(iobuf));
836 if (!(ap->dev_mask & (1 << dev)))
839 debug("port=%d dev=%d\n", ap->port_no, dev);
841 bfin_dev_select(ap, dev);
844 /* Device Identify Command */
845 write_atapi_register(base, ATA_REG_CMD, ATA_CMD_ID_ATA);
846 bfin_check_altstatus(ap);
849 status = bfin_ata_busy_wait(ap, ATA_BUSY, 1000, 0);
850 if (status & ATA_ERR) {
851 printf("\ndevice not responding\n");
852 ap->dev_mask &= ~(1 << dev);
856 read_atapi_data(base, ATA_SECTOR_WORDS, iobuf);
858 ata_swap_buf_le16(iobuf, ATA_SECTOR_WORDS);
860 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
861 if (!ata_id_has_dma(iobuf) || !ata_id_has_lba(iobuf))
862 printf("ata%u: no dma/lba\n", ap->port_no);
868 n_sectors = ata_id_n_sectors(iobuf);
870 if (n_sectors == 0) {
871 ap->dev_mask &= ~(1 << dev);
875 ata_id_c_string(iobuf, (unsigned char *)sata_dev_desc[ap->port_no].revision,
876 ATA_ID_FW_REV, sizeof(sata_dev_desc[ap->port_no].revision));
877 ata_id_c_string(iobuf, (unsigned char *)sata_dev_desc[ap->port_no].vendor,
878 ATA_ID_PROD, sizeof(sata_dev_desc[ap->port_no].vendor));
879 ata_id_c_string(iobuf, (unsigned char *)sata_dev_desc[ap->port_no].product,
880 ATA_ID_SERNO, sizeof(sata_dev_desc[ap->port_no].product));
882 if ((iop->config & 0x0080) == 0x0080)
883 sata_dev_desc[ap->port_no].removable = 1;
885 sata_dev_desc[ap->port_no].removable = 0;
887 sata_dev_desc[ap->port_no].lba = (u32) n_sectors;
888 debug("lba=0x%x\n", sata_dev_desc[ap->port_no].lba);
891 if (iop->command_set_2 & 0x0400)
892 sata_dev_desc[ap->port_no].lba48 = 1;
894 sata_dev_desc[ap->port_no].lba48 = 0;
898 sata_dev_desc[ap->port_no].type = DEV_TYPE_HARDDISK;
899 sata_dev_desc[ap->port_no].blksz = ATA_SECT_SIZE;
900 sata_dev_desc[ap->port_no].lun = 0; /* just to fill something in... */
902 printf("PATA device#%d %s is found on ata port#%d.\n",
903 ap->port_no%PATA_DEV_NUM_PER_PORT,
904 sata_dev_desc[ap->port_no].vendor,
905 ap->port_no/PATA_DEV_NUM_PER_PORT);
908 static void bfin_ata_set_Feature_cmd(struct ata_port *ap, int dev)
910 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
913 if (!(ap->dev_mask & (1 << dev)))
916 bfin_dev_select(ap, dev);
918 write_atapi_register(base, ATA_REG_FEATURE, SETFEATURES_XFER);
919 write_atapi_register(base, ATA_REG_NSECT, ap->ata_mode);
920 write_atapi_register(base, ATA_REG_LBAL, 0);
921 write_atapi_register(base, ATA_REG_LBAM, 0);
922 write_atapi_register(base, ATA_REG_LBAH, 0);
924 write_atapi_register(base, ATA_REG_DEVICE, ATA_DEVICE_OBS);
925 write_atapi_register(base, ATA_REG_CMD, ATA_CMD_SET_FEATURES);
930 status = bfin_ata_busy_wait(ap, ATA_BUSY, 5000, 0);
931 if ((status & (ATA_BUSY | ATA_ERR))) {
932 printf("Error : status 0x%02x\n", status);
933 ap->dev_mask &= ~(1 << dev);
937 int scan_sata(int dev)
939 /* dev is the index of each ata device in the system. one PATA port
940 * contains 2 devices. one element in scan_done array indicates one
941 * PATA port. device connected to one PATA port is selected by
942 * bfin_dev_select() before access.
944 struct ata_port *ap = &port[dev];
945 static int scan_done[(CONFIG_SYS_SATA_MAX_DEVICE+1)/PATA_DEV_NUM_PER_PORT];
947 if (scan_done[dev/PATA_DEV_NUM_PER_PORT])
950 /* Check for attached device */
951 if (!bfin_ata_probe_port(ap)) {
952 if (bfin_softreset(ap)) {
953 /* soft reset failed, try a hard one */
954 bfin_ata_reset_port(ap);
955 if (bfin_softreset(ap))
956 scan_done[dev/PATA_DEV_NUM_PER_PORT] = 1;
958 scan_done[dev/PATA_DEV_NUM_PER_PORT] = 1;
961 if (scan_done[dev/PATA_DEV_NUM_PER_PORT]) {
962 /* Probe device and set xfer mode */
963 bfin_ata_identify(ap, dev%PATA_DEV_NUM_PER_PORT);
964 bfin_ata_set_Feature_cmd(ap, dev%PATA_DEV_NUM_PER_PORT);
965 init_part(&sata_dev_desc[dev]);
969 printf("PATA device#%d is not present on ATA port#%d.\n",
970 ap->port_no%PATA_DEV_NUM_PER_PORT,
971 ap->port_no/PATA_DEV_NUM_PER_PORT);
976 int init_sata(int dev)
978 struct ata_port *ap = &port[dev];
987 switch (dev/PATA_DEV_NUM_PER_PORT) {
989 ap->ioaddr.ctl_addr = ATAPI_CONTROL;
990 ap->ata_mode = CONFIG_BFIN_ATA_MODE;
993 printf("Tried to scan unknown port %d.\n", dev);
997 if (ap->ata_mode < XFER_PIO_0 || ap->ata_mode > XFER_PIO_4) {
998 ap->ata_mode = XFER_PIO_4;
999 printf("DMA mode is not supported. Set to PIO mode 4.\n");
1003 ap->ctl_reg = 0x8; /*Default value of control reg */
1009 /* Read up to 255 sectors
1011 * Returns sectors read
1013 static u8 do_one_read(struct ata_port *ap, u64 blknr, u8 blkcnt, u16 *buffer,
1016 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
1021 if (!(bfin_check_status(ap) & ATA_DRDY)) {
1022 printf("Device ata%d not ready\n", ap->port_no);
1026 /* Set up transfer */
1029 /* write high bits */
1030 write_atapi_register(base, ATA_REG_NSECT, 0);
1031 write_atapi_register(base, ATA_REG_LBAL, (blknr >> 24) & 0xFF);
1032 write_atapi_register(base, ATA_REG_LBAM, (blknr >> 32) & 0xFF);
1033 write_atapi_register(base, ATA_REG_LBAH, (blknr >> 40) & 0xFF);
1036 write_atapi_register(base, ATA_REG_NSECT, blkcnt);
1037 write_atapi_register(base, ATA_REG_LBAL, (blknr >> 0) & 0xFF);
1038 write_atapi_register(base, ATA_REG_LBAM, (blknr >> 8) & 0xFF);
1039 write_atapi_register(base, ATA_REG_LBAH, (blknr >> 16) & 0xFF);
1043 write_atapi_register(base, ATA_REG_DEVICE, ATA_LBA);
1044 write_atapi_register(base, ATA_REG_CMD, ATA_CMD_PIO_READ_EXT);
1048 write_atapi_register(base, ATA_REG_DEVICE, ATA_LBA | ((blknr >> 24) & 0xF));
1049 write_atapi_register(base, ATA_REG_CMD, ATA_CMD_PIO_READ);
1051 status = bfin_ata_busy_wait(ap, ATA_BUSY, 500000, 1);
1053 if (status & (ATA_BUSY | ATA_ERR)) {
1054 printf("Device %d not responding status 0x%x.\n", ap->port_no, status);
1055 err = read_atapi_register(base, ATA_REG_ERR);
1056 printf("Error reg = 0x%x\n", err);
1061 if (bfin_wait_for_irq(ap, 500)) {
1062 printf("ata%u irq failed\n", ap->port_no);
1066 status = bfin_check_status(ap);
1067 if (status & ATA_ERR) {
1068 err = read_atapi_register(base, ATA_REG_ERR);
1069 printf("ata%u error %d\n", ap->port_no, err);
1074 /* Read one sector */
1075 read_atapi_data(base, ATA_SECTOR_WORDS, buffer);
1076 buffer += ATA_SECTOR_WORDS;
1083 ulong sata_read(int dev, ulong block, ulong blkcnt, void *buff)
1085 struct ata_port *ap = &port[dev];
1087 u16 *buffer = (u16 *) buff;
1089 u64 blknr = (u64) block;
1090 unsigned char lba48 = 0;
1093 if (blknr > 0xfffffff) {
1094 if (!sata_dev_desc[dev].lba48) {
1095 printf("Drive doesn't support 48-bit addressing\n");
1098 /* more than 28 bits used, use 48bit mode */
1102 bfin_dev_select(ap, dev%PATA_DEV_NUM_PER_PORT);
1104 while (blkcnt > 0) {
1111 status = do_one_read(ap, blknr, sread, buffer, lba48);
1112 if (status != sread) {
1113 printf("Read failed\n");
1120 buffer += sread * ATA_SECTOR_WORDS;
1125 ulong sata_write(int dev, ulong block, ulong blkcnt, const void *buff)
1127 struct ata_port *ap = &port[dev];
1128 void __iomem *base = (void __iomem *)ap->ioaddr.ctl_addr;
1130 u16 *buffer = (u16 *) buff;
1131 unsigned char status = 0;
1132 u64 blknr = (u64) block;
1134 unsigned char lba48 = 0;
1136 if (blknr > 0xfffffff) {
1137 if (!sata_dev_desc[dev].lba48) {
1138 printf("Drive doesn't support 48-bit addressing\n");
1141 /* more than 28 bits used, use 48bit mode */
1146 bfin_dev_select(ap, dev%PATA_DEV_NUM_PER_PORT);
1148 while (blkcnt-- > 0) {
1149 status = bfin_ata_busy_wait(ap, ATA_BUSY, 50000, 0);
1150 if (status & ATA_BUSY) {
1151 printf("ata%u failed to respond\n", ap->port_no);
1156 /* write high bits */
1157 write_atapi_register(base, ATA_REG_NSECT, 0);
1158 write_atapi_register(base, ATA_REG_LBAL,
1159 (blknr >> 24) & 0xFF);
1160 write_atapi_register(base, ATA_REG_LBAM,
1161 (blknr >> 32) & 0xFF);
1162 write_atapi_register(base, ATA_REG_LBAH,
1163 (blknr >> 40) & 0xFF);
1166 write_atapi_register(base, ATA_REG_NSECT, 1);
1167 write_atapi_register(base, ATA_REG_LBAL, (blknr >> 0) & 0xFF);
1168 write_atapi_register(base, ATA_REG_LBAM, (blknr >> 8) & 0xFF);
1169 write_atapi_register(base, ATA_REG_LBAH, (blknr >> 16) & 0xFF);
1172 write_atapi_register(base, ATA_REG_DEVICE, ATA_LBA);
1173 write_atapi_register(base, ATA_REG_CMD,
1174 ATA_CMD_PIO_WRITE_EXT);
1178 write_atapi_register(base, ATA_REG_DEVICE,
1179 ATA_LBA | ((blknr >> 24) & 0xF));
1180 write_atapi_register(base, ATA_REG_CMD,
1184 /*may take up to 5 sec */
1185 status = bfin_ata_busy_wait(ap, ATA_BUSY, 50000, 0);
1186 if ((status & (ATA_DRQ | ATA_BUSY | ATA_ERR)) != ATA_DRQ) {
1187 printf("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
1188 ap->port_no, (ulong) blknr, status);
1192 write_atapi_data(base, ATA_SECTOR_WORDS, buffer);
1193 bfin_check_altstatus(ap);
1198 buffer += ATA_SECTOR_WORDS;