tft_ll.c revision 1.3 1 /* $NetBSD: tft_ll.c,v 1.3 2011/06/18 06:44:27 matt Exp $ */
2
3 /*
4 * Copyright (c) 2006 Jachym Holecek
5 * All rights reserved.
6 *
7 * Written for DFC Design, s.r.o.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: tft_ll.c,v 1.3 2011/06/18 06:44:27 matt Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/kernel.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 #include <sys/device.h>
42 #include <sys/queue.h>
43
44 #include <uvm/uvm_extern.h>
45
46 #include <machine/bus.h>
47
48 /* XXX needed? */
49 #include <dev/wscons/wsdisplayvar.h>
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/rasops/rasops.h>
52 #include <dev/wscons/wsdisplay_vconsvar.h>
53
54 #include <evbppc/virtex/dev/xcvbusvar.h>
55 #include <evbppc/virtex/dev/cdmacreg.h>
56 #include <evbppc/virtex/dev/tftreg.h>
57 #include <evbppc/virtex/dev/tftvar.h>
58
59
60 struct ll_tft_control {
61 struct cdmac_descr cd_dsc;
62 u_char cd_img[];
63 } __packed;
64
65 struct ll_tft_softc {
66 struct tft_softc lsc_sc;
67
68 bus_space_tag_t lsc_dma_iot;
69 bus_space_handle_t lsc_dma_ioh;
70
71 bus_dma_tag_t lsc_dmat;
72 bus_dmamap_t lsc_dmap;
73
74 struct ll_tft_control *lsc_cd;
75 bus_dma_segment_t lsc_seg;
76 };
77
78 static void ll_tft_attach(device_t, device_t, void *);
79 static paddr_t ll_tft_mmap(void *, void *, off_t, int);
80 static void ll_tft_shutdown(void *);
81
82 CFATTACH_DECL_NEW(ll_tft, sizeof(struct ll_tft_softc),
83 xcvbus_child_match, ll_tft_attach, NULL, NULL);
84
85
86 static struct wsdisplay_accessops ll_tft_accessops = {
87 .mmap = ll_tft_mmap,
88 };
89
90
91 static void
92 ll_tft_attach(device_t parent, device_t self, void *aux)
93 {
94 struct xcvbus_attach_args *vaa = aux;
95 struct ll_dmac *tx = vaa->vaa_tx_dmac;
96 struct ll_tft_softc *lsc = device_private(self);
97 struct tft_softc *sc = &lsc->lsc_sc;
98 int nseg, error;
99
100 KASSERT(tx);
101
102 lsc->lsc_dma_iot = tx->dmac_iot;
103 lsc->lsc_dmat = vaa->vaa_dmat;
104 sc->sc_iot = vaa->vaa_iot;
105 sc->sc_dev = self;
106
107 aprint_normal(": LL_TFT\n");
108
109 if ((error = bus_space_map(sc->sc_iot, vaa->vaa_addr, TFT_SIZE,
110 0, &sc->sc_ioh)) != 0) {
111 aprint_error_dev(self, "could not map device registers\n");
112 goto fail_0;
113 }
114 if ((error = bus_space_map(lsc->lsc_dma_iot, tx->dmac_ctrl_addr,
115 CDMAC_CTRL_SIZE, 0, &lsc->lsc_dma_ioh)) != 0) {
116 aprint_error_dev(self, "could not map dmac registers\n");
117 goto fail_1;
118 }
119
120 /* Fill in resolution, depth, size. */
121 tft_mode(sc->sc_dev);
122
123 /* Allocate and map framebuffer control data. */
124 if ((error = bus_dmamem_alloc(lsc->lsc_dmat,
125 sizeof(struct ll_tft_control) + sc->sc_size, 8, 0,
126 &lsc->lsc_seg, 1, &nseg, 0)) != 0) {
127 aprint_error_dev(self, "could not allocate framebuffer\n");
128 goto fail_2;
129 }
130 if ((error = bus_dmamem_map(lsc->lsc_dmat, &lsc->lsc_seg, nseg,
131 sizeof(struct ll_tft_control) + sc->sc_size,
132 (void **)&lsc->lsc_cd, BUS_DMA_COHERENT)) != 0) {
133 aprint_error_dev(self, "could not map framebuffer\n");
134 goto fail_3;
135 }
136 if ((error = bus_dmamap_create(lsc->lsc_dmat,
137 sizeof(struct ll_tft_control) + sc->sc_size, 1,
138 sizeof(struct ll_tft_control) + sc->sc_size, 0, 0,
139 &lsc->lsc_dmap)) != 0) {
140 aprint_error_dev(self, "could not create framebuffer DMA map\n");
141 goto fail_4;
142 }
143 if ((error = bus_dmamap_load(lsc->lsc_dmat, lsc->lsc_dmap, lsc->lsc_cd,
144 sizeof(struct ll_tft_control) + sc->sc_size, NULL, 0)) != 0) {
145 aprint_error_dev(self, "could not load framebuffer DMA map\n");
146 goto fail_5;
147 }
148
149 /* Clear screen, setup descriptor. */
150 memset(lsc->lsc_cd, 0x00, sizeof(struct ll_tft_control));
151 sc->sc_image = lsc->lsc_cd->cd_img;
152
153 lsc->lsc_cd->cd_dsc.desc_next = lsc->lsc_dmap->dm_segs[0].ds_addr;
154 lsc->lsc_cd->cd_dsc.desc_addr = lsc->lsc_dmap->dm_segs[0].ds_addr +
155 offsetof(struct ll_tft_control, cd_img);
156 lsc->lsc_cd->cd_dsc.desc_size = sc->sc_size;
157 lsc->lsc_cd->cd_dsc.desc_stat = CDMAC_STAT_SOP;
158
159 bus_dmamap_sync(lsc->lsc_dmat, lsc->lsc_dmap, 0,
160 sizeof(struct ll_tft_control) + sc->sc_size,
161 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
162
163 sc->sc_sdhook = shutdownhook_establish(ll_tft_shutdown, sc);
164 if (sc->sc_sdhook == NULL)
165 aprint_error_dev(self,
166 "WARNING: unable to establish shutdown hook\n");
167
168 tft_attach(self, &ll_tft_accessops);
169
170 aprint_normal_dev(self, "video memory pa 0x%08x\n",
171 (uint32_t)lsc->lsc_cd->cd_dsc.desc_addr);
172
173 /* Timing sensitive... */
174 bus_space_write_4(sc->sc_iot, sc->sc_ioh, TFT_CTRL, CTRL_RESET);
175 bus_space_write_4(sc->sc_iot, sc->sc_ioh, TFT_CTRL, CTRL_ENABLE);
176 bus_space_write_4(lsc->lsc_dma_iot, lsc->lsc_dma_ioh, CDMAC_CURDESC,
177 lsc->lsc_dmap->dm_segs[0].ds_addr);
178
179 return ;
180
181 fail_5:
182 bus_dmamap_destroy(lsc->lsc_dmat, lsc->lsc_dmap);
183 fail_4:
184 bus_dmamem_unmap(lsc->lsc_dmat, (void *)lsc->lsc_cd,
185 sizeof(struct ll_tft_control) + sc->sc_size);
186 fail_3:
187 bus_dmamem_free(lsc->lsc_dmat, &lsc->lsc_seg, nseg);
188 fail_2:
189 bus_space_unmap(lsc->lsc_dma_iot, lsc->lsc_dma_ioh, CDMAC_CTRL_SIZE);
190 fail_1:
191 bus_space_unmap(sc->sc_iot, sc->sc_ioh, TFT_SIZE);
192 fail_0:
193 aprint_error_dev(self, "error %d\n", error);
194 }
195
196 static paddr_t
197 ll_tft_mmap(void *arg, void *scr, off_t offs, int prot)
198 {
199 struct ll_tft_softc *lsc = arg;
200 paddr_t pa;
201
202 if (offs < lsc->lsc_sc.sc_size) {
203 pa = bus_dmamem_mmap(lsc->lsc_dmat, &lsc->lsc_seg, 1,
204 offs + offsetof(struct ll_tft_control, cd_img),
205 prot, BUS_DMA_WAITOK | BUS_DMA_COHERENT);
206
207 return (pa);
208 }
209
210 return (-1);
211 }
212
213 static void
214 ll_tft_shutdown(void *arg)
215 {
216 struct ll_tft_softc *lsc = arg;
217
218 bus_space_write_4(lsc->lsc_dma_iot, lsc->lsc_dma_ioh, 0,
219 CDMAC_STAT_RESET);
220
221 tft_shutdown(&lsc->lsc_sc);
222 }
223