xref: /openbsd-src/sys/dev/pci/jmb.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: jmb.c,v 1.8 2010/04/08 00:23:53 tedu Exp $ */
2 
3 /*
4  * Copyright (c) 2007 David Gwynne <dlg@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/buf.h>
22 #include <sys/kernel.h>
23 #include <sys/malloc.h>
24 #include <sys/device.h>
25 #include <sys/queue.h>
26 
27 #include <machine/bus.h>
28 
29 #include <dev/pci/pcireg.h>
30 #include <dev/pci/pcivar.h>
31 #include <dev/pci/pcidevs.h>
32 
33 /* JMicron registers */
34 #define JM_PCI_CTL0		0x40 /* control register 0 */
35 #define  JM_PCI_CTL0_ROM_EN		(1<<31)	/* External Option ROM */
36 #define  JM_PCI_CTL0_IDWR_EN		(1<<30) /* Device ID Write */
37 #define  JM_PCI_CTL0_MSI64_EN		(1<<25) /* 64bit MSI Addr Mode */
38 #define  JM_PCI_CTL0_MSI_EN		(1<<24) /* MSI Addr Mode */
39 #define  JM_PCI_CTL0_IDEDMA_CFG		(1<<23) /* PCIIDE DMA Chan Cfg */
40 #define  JM_PCI_CTL0_PCIIDE_CS		(1<<22) /* PCIIDE channels Swap */
41 #define  JM_PCI_CTL0_SATA_PS		(1<<21) /* SATA channel M/S swap */
42 #define  JM_PCI_CTL0_AHCI_PS		(1<<20) /* SATA AHCI ports swap */
43 #define  JM_PCI_CTL0_F1_SUBCLASS_M	0xc0000 /* subclass for func 1 */
44 #define  JM_PCI_CTL0_F0_SUBCLASS_M	0x30000 /* subclass for func 0 */
45 #define  JM_PCI_CTL0_SUBCLASS_IDE	0x0 /* IDE Controller */
46 #define  JM_PCI_CTL0_SUBCLASS_RAID	0x1 /* RAID Controller */
47 #define  JM_PCI_CTL0_SUBCLASS_AHCI	0x2 /* AHCI Controller */
48 #define  JM_PCI_CTL0_SUBCLASS_OTHER	0x3 /* Other Mass Storage */
49 #define  JM_PCI_CTL0_F1_SUBCLASS(_m)	((_m)<<18) /* subclass for func 1 */
50 #define  JM_PCI_CTL0_F0_SUBCLASS(_m)	((_m)<<16) /* subclass for func 0 */
51 #define  JM_PCI_CTL0_SATA1_AHCI		(1<<15) /* SATA port 1 AHCI enable */
52 #define  JM_PCI_CTL0_SATA1_IDE		(1<<14) /* SATA port 1 IDE enable */
53 #define  JM_PCI_CTL0_SATA0_AHCI		(1<<13) /* SATA port 0 AHCI enable */
54 #define  JM_PCI_CTL0_SATA0_IDE		(1<<12) /* SATA port 0 PCIIDE enable */
55 #define  JM_PCI_CTL0_AHCI_F1		(1<<9) /* AHCI on function 1 */
56 #define  JM_PCI_CTL0_AHCI_EN		(1<<8) /* ACHI enable */
57 #define  JM_PCI_CTL0_PATA0_RST		(1<<6) /* PATA port 0 reset */
58 #define  JM_PCI_CTL0_PATA0_EN		(1<<5) /* PATA port 0 enable */
59 #define  JM_PCI_CTL0_PATA0_SEC		(1<<4) /* PATA 0 enable on 2nd chan */
60 #define  JM_PCI_CTL0_PATA0_40P		(1<<3) /* PATA 0 40pin cable */
61 #define  JM_PCI_CTL0_PCIIDE_F1		(1<<1) /* PCIIDE on function 1 */
62 #define  JM_PCI_CTL0_PATA0_PRI		(1<<0) /* PATA 0 enable on 1st chan */
63 
64 #define JM_PCI_CTL5		0x80 /* control register 8 */
65 #define  JM_PCI_CTL5_PATA1_PRI		(1<<24) /* force PATA 1 on chan0 */
66 
67 int		jmb_match(struct device *, void *, void *);
68 void		jmb_attach(struct device *, struct device *, void *);
69 int		jmb_print(void *, const char *);
70 
71 struct jmb_softc {
72 	struct device		sc_dev;
73 };
74 
75 struct cfattach jmb_ca = {
76 	sizeof(struct jmb_softc),
77 	jmb_match,
78 	jmb_attach,
79 	config_detach_children
80 };
81 
82 struct cfdriver jmb_cd = {
83 	NULL, "jmb", DV_DULL
84 };
85 
86 static const struct pci_matchid jmb_devices[] = {
87 	{ PCI_VENDOR_JMICRON,	PCI_PRODUCT_JMICRON_JMB360 },
88 	{ PCI_VENDOR_JMICRON,	PCI_PRODUCT_JMICRON_JMB361 },
89 	{ PCI_VENDOR_JMICRON,	PCI_PRODUCT_JMICRON_JMB362 },
90 	{ PCI_VENDOR_JMICRON,	PCI_PRODUCT_JMICRON_JMB363 },
91 	{ PCI_VENDOR_JMICRON,	PCI_PRODUCT_JMICRON_JMB365 },
92 	{ PCI_VENDOR_JMICRON,	PCI_PRODUCT_JMICRON_JMB366 },
93 	{ PCI_VENDOR_JMICRON,	PCI_PRODUCT_JMICRON_JMB368 }
94 };
95 
96 int
97 jmb_match(struct device *parent, void *match, void *aux)
98 {
99 	struct pci_attach_args		*pa = aux;
100 
101 	return (pci_matchbyid(pa, jmb_devices,
102 	    sizeof(jmb_devices) / sizeof(jmb_devices[0])) * 3);
103 }
104 
105 void
106 jmb_attach(struct device *parent, struct device *self, void *aux)
107 {
108 	struct pci_attach_args		*pa = aux, jpa;
109 	u_int32_t			ctl0, ctl5;
110 	int				sata = 0, pata = 0;
111 
112 	ctl0 = pci_conf_read(pa->pa_pc, pa->pa_tag, JM_PCI_CTL0);
113 	ctl5 = pci_conf_read(pa->pa_pc, pa->pa_tag, JM_PCI_CTL5);
114 
115 	/* configure sata bits if it is on this function */
116 	if (pa->pa_function == (ISSET(ctl0, JM_PCI_CTL0_AHCI_F1) ? 1 : 0)) {
117 		ctl0 &= ~(JM_PCI_CTL0_AHCI_EN | JM_PCI_CTL0_SATA0_IDE |
118 		    JM_PCI_CTL0_SATA0_AHCI | JM_PCI_CTL0_SATA1_IDE |
119 		    JM_PCI_CTL0_SATA1_AHCI);
120 
121 		switch (PCI_PRODUCT(pa->pa_id)) {
122 		case PCI_PRODUCT_JMICRON_JMB360:
123 		case PCI_PRODUCT_JMICRON_JMB361:
124 		case PCI_PRODUCT_JMICRON_JMB362:
125 		case PCI_PRODUCT_JMICRON_JMB363:
126 		case PCI_PRODUCT_JMICRON_JMB365:
127 		case PCI_PRODUCT_JMICRON_JMB366:
128 			/* enable AHCI */
129 			ctl0 |= JM_PCI_CTL0_AHCI_EN | JM_PCI_CTL0_SATA0_AHCI |
130 			    JM_PCI_CTL0_SATA1_AHCI;
131 			sata = 1;
132 			break;
133 		}
134 	}
135 
136 	/* configure pata bits if it is on this function */
137 	if (pa->pa_function == (ISSET(ctl0, JM_PCI_CTL0_PCIIDE_F1) ? 1 : 0)) {
138 		ctl0 &= ~(JM_PCI_CTL0_PCIIDE_CS | JM_PCI_CTL0_IDEDMA_CFG);
139 		ctl5 &= ~JM_PCI_CTL5_PATA1_PRI;
140 
141 		switch (PCI_PRODUCT(pa->pa_id)) {
142 		case PCI_PRODUCT_JMICRON_JMB366:
143 		case PCI_PRODUCT_JMICRON_JMB365:
144 			/* wire the second PATA port in the right place */
145 			ctl5 |= JM_PCI_CTL5_PATA1_PRI;
146 			/* FALLTHROUGH */
147 		case PCI_PRODUCT_JMICRON_JMB363:
148 		case PCI_PRODUCT_JMICRON_JMB361:
149 		case PCI_PRODUCT_JMICRON_JMB368:
150 			ctl0 |= JM_PCI_CTL0_PCIIDE_CS | JM_PCI_CTL0_IDEDMA_CFG;
151 			pata = 1;
152 			break;
153 		}
154 	}
155 
156 	pci_conf_write(pa->pa_pc, pa->pa_tag, JM_PCI_CTL0, ctl0);
157 	pci_conf_write(pa->pa_pc, pa->pa_tag, JM_PCI_CTL5, ctl5);
158 
159 	printf("\n");
160 
161 	jpa = *pa;
162 
163 	if (sata) {
164 		/* tweak the class to look like ahci, then try to attach it */
165 		jpa.pa_class = (PCI_CLASS_MASS_STORAGE << PCI_CLASS_SHIFT) |
166 		    (PCI_SUBCLASS_MASS_STORAGE_SATA << PCI_SUBCLASS_SHIFT) |
167 		    (0x01 << PCI_INTERFACE_SHIFT); /* AHCI_PCI_INTERFACE */
168 		config_found(self, &jpa, jmb_print);
169 	}
170 
171 	if (pata) {
172 		/* set things up for pciide */
173 		jpa.pa_class = (PCI_CLASS_MASS_STORAGE << PCI_CLASS_SHIFT) |
174 		    (PCI_SUBCLASS_MASS_STORAGE_IDE << PCI_SUBCLASS_SHIFT) |
175 		    (0x85 << PCI_INTERFACE_SHIFT);
176 		config_found(self, &jpa, jmb_print);
177 	}
178 }
179 
180 int
181 jmb_print(void *aux, const char *pnp)
182 {
183 	struct pci_attach_args		*pa = aux;
184 	char				devinfo[256];
185 
186 	if (pnp != NULL) {
187 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo,
188 		    sizeof(devinfo));
189 		printf("%s at %s", devinfo, pnp);
190 	}
191 
192 	return (UNCONF);
193 }
194