1a1917f14Szrj /*-
2a1917f14Szrj  * Copyright (c) 1998 - 2008 Søren Schmidt <sos@FreeBSD.org>
3a1917f14Szrj  * All rights reserved.
4a1917f14Szrj  *
5a1917f14Szrj  * Redistribution and use in source and binary forms, with or without
6a1917f14Szrj  * modification, are permitted provided that the following conditions
7a1917f14Szrj  * are met:
8a1917f14Szrj  * 1. Redistributions of source code must retain the above copyright
9a1917f14Szrj  *    notice, this list of conditions and the following disclaimer,
10a1917f14Szrj  *    without modification, immediately at the beginning of the file.
11a1917f14Szrj  * 2. Redistributions in binary form must reproduce the above copyright
12a1917f14Szrj  *    notice, this list of conditions and the following disclaimer in the
13a1917f14Szrj  *    documentation and/or other materials provided with the distribution.
14a1917f14Szrj  *
15a1917f14Szrj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16a1917f14Szrj  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17a1917f14Szrj  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18a1917f14Szrj  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19a1917f14Szrj  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20a1917f14Szrj  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21a1917f14Szrj  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22a1917f14Szrj  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23a1917f14Szrj  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24a1917f14Szrj  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a1917f14Szrj  */
26a1917f14Szrj 
27a1917f14Szrj /* local prototypes */
28a1917f14Szrj static int ata_national_chipinit(device_t dev);
29a1917f14Szrj static void ata_national_setmode(device_t dev, int mode);
30a1917f14Szrj 
31a1917f14Szrj /*
32a1917f14Szrj  * National chipset support functions
33a1917f14Szrj  */
34a1917f14Szrj int
ata_national_ident(device_t dev)35a1917f14Szrj ata_national_ident(device_t dev)
36a1917f14Szrj {
37a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
38a1917f14Szrj 
39a1917f14Szrj     /* this chip is a clone of the Cyrix chip, bugs and all */
40a1917f14Szrj     if (pci_get_devid(dev) == ATA_SC1100) {
41a1917f14Szrj 	device_set_desc(dev, "National Geode SC1100 ATA33 controller");
42a1917f14Szrj 	ctlr->chipinit = ata_national_chipinit;
43a1917f14Szrj 	return 0;
44a1917f14Szrj     }
45a1917f14Szrj     return ENXIO;
46a1917f14Szrj }
47a1917f14Szrj 
48a1917f14Szrj static int
ata_national_chipinit(device_t dev)49a1917f14Szrj ata_national_chipinit(device_t dev)
50a1917f14Szrj {
51a1917f14Szrj     struct ata_pci_controller *ctlr = device_get_softc(dev);
52a1917f14Szrj 
5343156ad7Szrj     if (ata_setup_interrupt(dev, ata_generic_intr))
54a1917f14Szrj 	return ENXIO;
55a1917f14Szrj 
56a1917f14Szrj     ctlr->setmode = ata_national_setmode;
57a1917f14Szrj     return 0;
58a1917f14Szrj }
59a1917f14Szrj 
60a1917f14Szrj static void
ata_national_setmode(device_t dev,int mode)61a1917f14Szrj ata_national_setmode(device_t dev, int mode)
62a1917f14Szrj {
63a1917f14Szrj     device_t gparent = GRANDPARENT(dev);
64a1917f14Szrj     struct ata_channel *ch = device_get_softc(device_get_parent(dev));
65a1917f14Szrj     struct ata_device *atadev = device_get_softc(dev);
66*2458a87aSzrj     int devno = (ch->unit << 1) + atadev->unit;
6759503772Szrj 	static const uint32_t piotiming[] =
68a1917f14Szrj 	    { 0x9172d132, 0x21717121, 0x00803020, 0x20102010, 0x00100010,
69a1917f14Szrj 	      0x00803020, 0x20102010, 0x00100010,
70a1917f14Szrj 	      0x00100010, 0x00100010, 0x00100010 };
7159503772Szrj 	static const uint32_t dmatiming[] =
7259503772Szrj 	    { 0x80077771, 0x80012121, 0x80002020 };
7359503772Szrj 	static const uint32_t udmatiming[] =
7459503772Szrj 	    { 0x80921250, 0x80911140, 0x80911030 };
75a1917f14Szrj     int error;
76a1917f14Szrj 
77a1917f14Szrj     ch->dma->alignment = 16;
78878a3234Szrj     ch->dma->max_iosize = 64 * DEV_BSIZE;
79a1917f14Szrj 
80a1917f14Szrj     mode = ata_limit_mode(dev, mode, ATA_UDMA2);
81a1917f14Szrj 
82a1917f14Szrj     error = ata_controlcmd(dev, ATA_SETFEATURES, ATA_SF_SETXFER, 0, mode);
83a1917f14Szrj 
84a1917f14Szrj     if (bootverbose)
85a1917f14Szrj 	device_printf(dev, "%s setting %s on National chip\n",
86a1917f14Szrj 		      (error) ? "failed" : "success", ata_mode2str(mode));
87a1917f14Szrj     if (!error) {
88a1917f14Szrj 	if (mode >= ATA_UDMA0) {
89a1917f14Szrj 	    pci_write_config(gparent, 0x44 + (devno << 3),
90a1917f14Szrj 			     udmatiming[mode & ATA_MODE_MASK], 4);
91a1917f14Szrj 	}
92a1917f14Szrj 	else if (mode >= ATA_WDMA0) {
93a1917f14Szrj 	    pci_write_config(gparent, 0x44 + (devno << 3),
94a1917f14Szrj 			     dmatiming[mode & ATA_MODE_MASK], 4);
95a1917f14Szrj 	}
96a1917f14Szrj 	else {
97a1917f14Szrj 	    pci_write_config(gparent, 0x44 + (devno << 3),
98a1917f14Szrj 			     pci_read_config(gparent, 0x44 + (devno << 3), 4) |
99a1917f14Szrj 			     0x80000000, 4);
100a1917f14Szrj 	}
101a1917f14Szrj 	pci_write_config(gparent, 0x40 + (devno << 3),
102a1917f14Szrj 			 piotiming[ata_mode2idx(mode)], 4);
103a1917f14Szrj 	atadev->mode = mode;
104a1917f14Szrj     }
105a1917f14Szrj }
106