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