xref: /netbsd-src/sys/arch/evbarm/netwalker/netwalker_spi.c (revision 6cf6fe02a981b55727c49c3d37b0d8191a98c0ee)
1 /*	$NetBSD: netwalker_spi.c,v 1.1 2014/03/29 12:00:27 hkenken Exp $	*/
2 
3 /*-
4  * Copyright (c) 2009  Genetec Corporation.  All rights reserved.
5  * Written by Hashimoto Kenichi for Genetec Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netwalker_spi.c,v 1.1 2014/03/29 12:00:27 hkenken Exp $");
31 
32 #include "opt_imxspi.h"
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/device.h>
37 
38 #include <arm/imx/imx51reg.h>
39 #include <arm/imx/imx51var.h>
40 #include <arm/imx/imx51_ccmvar.h>
41 #include <arm/imx/imx51_ccmreg.h>
42 #include <arm/imx/imx51_iomuxreg.h>
43 #include <arm/imx/imxgpiovar.h>
44 #include <arm/imx/imxspivar.h>
45 
46 struct imx51spi_softc {
47 	struct imxspi_softc	sc_spi;
48 	struct spi_chipset_tag	sc_tag;
49 };
50 
51 CFATTACH_DECL_NEW(spi_netwalker, sizeof(struct imx51spi_softc),
52     imxspi_match, imxspi_attach, NULL, NULL);
53 
54 static int
55 imxspi_cs_enable(void *arg, int slave)
56 {
57 	switch (slave) {
58 	case 0:
59 		gpio_data_write(GPIO_NO(4, 24), 0);
60 		gpio_set_direction(GPIO_NO(4, 24), GPIO_DIR_OUT);
61 		break;
62 	case 1:
63 		gpio_data_write(GPIO_NO(4, 25), 0);
64 		gpio_set_direction(GPIO_NO(4, 25), GPIO_DIR_OUT);
65 		break;
66 	case 2:
67 		gpio_data_write(GPIO_NO(3, 0), 0);
68 		gpio_set_direction(GPIO_NO(3, 0), GPIO_DIR_OUT);
69 		break;
70 	}
71 
72 	return 0;
73 }
74 
75 static int
76 imxspi_cs_disable(void *arg, int slave)
77 {
78 	switch (slave) {
79 	case 0:
80 		gpio_data_write(GPIO_NO(4, 24), 1);
81 		gpio_set_direction(GPIO_NO(4, 24), GPIO_DIR_IN);
82 		break;
83 	case 1:
84 		gpio_data_write(GPIO_NO(4, 25), 1);
85 		gpio_set_direction(GPIO_NO(4, 25), GPIO_DIR_IN);
86 		break;
87 	case 2:
88 		gpio_data_write(GPIO_NO(3, 0), 1);
89 		gpio_set_direction(GPIO_NO(3, 0), GPIO_DIR_IN);
90 		break;
91 	}
92 
93 	return 0;
94 }
95 
96 int
97 imxspi_match(device_t parent, cfdata_t cf, void *aux)
98 {
99 	if (strcmp(cf->cf_name, "imxspi") == 0)
100 		return 1;
101 	if (cf->cf_unit != 0)
102 		return 1;
103 
104 	return 0;
105 }
106 
107 void
108 imxspi_attach(device_t parent, device_t self, void *aux)
109 {
110 	struct imx51spi_softc *sc = device_private(self);
111 	struct axi_attach_args *aa = aux;
112 	struct imxspi_attach_args saa;
113 	int cf_flags = device_cfdata(self)->cf_flags;
114 
115 	sc->sc_tag.cookie = sc;
116 
117 	if (device_cfdata(self)->cf_unit == 0) {
118 		/* CS 0 GPIO setting */
119 		gpio_data_write(GPIO_NO(4, 24), 1);
120 		gpio_set_direction(GPIO_NO(4, 24), GPIO_DIR_IN);
121 
122 		/* CS 1 GPIO setting */
123 		gpio_data_write(GPIO_NO(4, 25), 1);
124 		gpio_set_direction(GPIO_NO(4, 25), GPIO_DIR_IN);
125 
126 		/* CS 2 */
127 		/* OJ6SH-T25 Shutdown */
128 		gpio_data_write(GPIO_NO(3, 14), 0);
129 		gpio_set_direction(GPIO_NO(3, 14), GPIO_DIR_OUT);
130 
131 		/* CS 2 GPIO setting */
132 		gpio_data_write(GPIO_NO(3, 0), 1);
133 		gpio_set_direction(GPIO_NO(3, 0), GPIO_DIR_IN);
134 
135 		sc->sc_tag.spi_cs_enable = imxspi_cs_enable;
136 		sc->sc_tag.spi_cs_disable = imxspi_cs_disable;
137 	}
138 
139 	saa.saa_iot = aa->aa_iot;
140 	saa.saa_addr = aa->aa_addr;
141 	saa.saa_size = aa->aa_size;
142 	saa.saa_irq = aa->aa_irq;
143 	saa.saa_enhanced = cf_flags;
144 
145 	saa.saa_nslaves = IMXSPINSLAVES;
146 	saa.saa_freq = imx51_get_clock(IMX51CLK_CSPI_CLK_ROOT);
147 	saa.saa_tag = &sc->sc_tag;
148 
149 	sc->sc_spi.sc_dev = self;
150 
151 	imxspi_attach_common(parent, &sc->sc_spi, &saa);
152 }
153