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