at91cf.c revision 1.1.2.1 1 /* $Id: at91cf.c,v 1.1.2.1 2007/11/10 02:56:29 matt Exp $ */
2 /* $NetBSD: at91cf.c,v 1.1.2.1 2007/11/10 02:56:29 matt Exp $ */
3
4 /*
5 * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
6 *
7 * Based on arch/evbarm/ep93xx/eppcic.c,
8 * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: at91cf.c,v 1.1.2.1 2007/11/10 02:56:29 matt Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/device.h>
40 #include <sys/kthread.h>
41 #include <uvm/uvm_param.h>
42 #include <machine/bus.h>
43 #include <dev/pcmcia/pcmciareg.h>
44 #include <dev/pcmcia/pcmciavar.h>
45 #include <dev/pcmcia/pcmciachip.h>
46 #include <arm/at91/at91reg.h>
47 #include <arm/at91/at91var.h>
48 #include <arm/at91/at91cfvar.h>
49
50 #include "at91pio.h"
51 #if NAT91PIO == 0
52 #error "at91cf requires at91pio"
53 #endif
54
55 #ifdef AT91CF_DEBUG
56 int at91cf_debug = AT91CF_DEBUG;
57 #define DPRINTFN(n,x) if (at91cf_debug>(n)) printf x;
58 #else
59 #define DPRINTFN(n,x)
60 #endif
61
62 struct at91cf_handle {
63 struct at91cf_softc *ph_sc;
64 struct device *ph_card;
65 int (*ph_ih_func)(void *);
66 void *ph_ih_arg;
67 struct proc *ph_event_thread;
68 int ph_type; /* current access type */
69 int ph_run; /* kthread running */
70 int ph_width; /* 8 or 16 */
71 int ph_vcc; /* 3 or 5 */
72 int ph_status; /* combined cd1 and cd2 */
73 struct {
74 bus_size_t reg;
75 bus_addr_t base;
76 bus_size_t size;
77 } ph_space[3];
78 #define IO 0
79 #define COMMON 1
80 #define ATTRIBUTE 2
81 };
82
83 static int at91cf_intr_carddetect(void *);
84 static int at91cf_intr_socket(void *);
85 static int at91cf_print(void *, const char *);
86 static void at91cf_create_event_thread(void *);
87 static void at91cf_event_thread(void *);
88 void at91cf_shutdown(void *);
89
90 static int at91cf_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
91 struct pcmcia_mem_handle *);
92 static void at91cf_mem_free(pcmcia_chipset_handle_t,
93 struct pcmcia_mem_handle *);
94 static int at91cf_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
95 struct pcmcia_mem_handle *, bus_size_t *, int *);
96 static void at91cf_mem_unmap(pcmcia_chipset_handle_t, int);
97 static int at91cf_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
98 bus_size_t, struct pcmcia_io_handle *);
99 static void at91cf_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
100 static int at91cf_io_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
101 struct pcmcia_io_handle *, int *);
102 static void at91cf_io_unmap(pcmcia_chipset_handle_t, int);
103 static void *at91cf_intr_establish(pcmcia_chipset_handle_t,
104 struct pcmcia_function *,
105 int, int (*)(void *), void *);
106 static void at91cf_intr_disestablish(pcmcia_chipset_handle_t, void *);
107 static void at91cf_socket_enable(pcmcia_chipset_handle_t);
108 static void at91cf_socket_disable(pcmcia_chipset_handle_t);
109 static void at91cf_socket_settype(pcmcia_chipset_handle_t, int);
110
111 static void at91cf_attach_socket(struct at91cf_handle *);
112 static void at91cf_config_socket(struct at91cf_handle *);
113 //static int at91cf_get_voltage(struct at91cf_handle *);
114
115 static struct pcmcia_chip_functions at91cf_functions = {
116 at91cf_mem_alloc, at91cf_mem_free,
117 at91cf_mem_map, at91cf_mem_unmap,
118 at91cf_io_alloc, at91cf_io_free,
119 at91cf_io_map, at91cf_io_unmap,
120 at91cf_intr_establish, at91cf_intr_disestablish,
121 at91cf_socket_enable, at91cf_socket_disable,
122 at91cf_socket_settype
123 };
124
125 #define MEMORY_BASE 0
126 #define MEMORY_SIZE 0x1000
127 #define COMMON_BASE 0x400000
128 #define COMMON_SIZE 0x1000
129 #define IO_BASE 0x800000
130 #define IO_SIZE 0x1000
131 #define MIN_SIZE (IO_BASE + IO_SIZE)
132
133 void
134 at91cf_attach_common(struct device *parent, struct device *self, void *aux,
135 at91cf_chipset_tag_t cscf)
136 {
137 struct at91cf_softc *sc = (struct at91cf_softc *)self;
138 struct at91bus_attach_args *sa = aux;
139 struct at91cf_handle *ph;
140
141 if (sa->sa_size < MIN_SIZE) {
142 printf("%s: it's not possible to map registers\n", self->dv_xname);
143 return;
144 }
145
146 sc->sc_iot = sa->sa_iot;
147 sc->sc_cscf = cscf;
148 sc->sc_enable = 0;
149
150 if (bus_space_map(sa->sa_iot, sa->sa_addr + MEMORY_BASE,
151 MEMORY_SIZE, 0, &sc->sc_memory_ioh)){
152 printf("%s: Cannot map memory space\n", self->dv_xname);
153 return;
154 }
155
156 if (bus_space_map(sa->sa_iot, sa->sa_addr + COMMON_BASE,
157 COMMON_SIZE, 0, &sc->sc_common_ioh)){
158 printf("%s: Cannot map common memory space\n", self->dv_xname);
159 bus_space_unmap(sa->sa_iot, sc->sc_memory_ioh, MEMORY_SIZE);
160 return;
161 }
162
163 if (bus_space_map(sa->sa_iot, sa->sa_addr + IO_BASE,
164 IO_SIZE, 0, &sc->sc_io_ioh)){
165 printf("%s: Cannot map I/O space\n", self->dv_xname);
166 bus_space_unmap(sa->sa_iot, sc->sc_memory_ioh, MEMORY_SIZE);
167 bus_space_unmap(sa->sa_iot, sc->sc_common_ioh, COMMON_SIZE);
168 return;
169 }
170
171 printf("\n");
172
173 /* socket 0 */
174 if (!(ph = malloc(sizeof(struct at91cf_handle), M_DEVBUF, M_NOWAIT))) {
175 printf("%s: Cannot allocate memory\n", self->dv_xname);
176 // @@@@ unmap? @@@@
177 return; /* ENOMEM */
178 }
179 sc->sc_ph = ph;
180 ph->ph_sc = sc;
181 ph->ph_space[IO].base = sa->sa_addr + IO_BASE;
182 ph->ph_space[IO].size = IO_SIZE;
183 ph->ph_space[COMMON].base = sa->sa_addr + COMMON_BASE;
184 ph->ph_space[COMMON].size = COMMON_SIZE;
185 ph->ph_space[ATTRIBUTE].base = sa->sa_addr + MEMORY_BASE;
186 ph->ph_space[ATTRIBUTE].size = MEMORY_SIZE;
187 at91cf_attach_socket(ph);
188
189 // @@@ reset CF now? @@@@
190
191 at91cf_config_socket(sc->sc_ph);
192 }
193
194 static void
195 at91cf_attach_socket(struct at91cf_handle *ph)
196 {
197 struct at91cf_softc *sc = ph->ph_sc;
198 at91cf_chipset_tag_t cscf = sc->sc_cscf;
199 int wait;
200
201 ph->ph_width = 16;
202 ph->ph_vcc = 3;
203 ph->ph_event_thread = NULL;
204 ph->ph_run = 0;
205 ph->ph_ih_func = NULL;
206 ph->ph_ih_arg = NULL;
207
208 ph->ph_status = (*cscf->card_detect)(sc);
209
210 wait = (cscf->power_ctl)(sc, POWER_OFF);
211 delay(wait);
212 wait = (cscf->power_ctl)(sc, POWER_ON);
213 delay(wait);
214 }
215
216 static void
217 at91cf_config_socket(struct at91cf_handle *ph)
218 {
219 struct at91cf_softc *sc = ph->ph_sc;
220 at91cf_chipset_tag_t cscf = sc->sc_cscf;
221 struct pcmciabus_attach_args paa;
222 int wait;
223
224 paa.paa_busname = "pcmcia";
225 paa.pct = (pcmcia_chipset_tag_t)&at91cf_functions;
226 paa.pch = (pcmcia_chipset_handle_t)ph;
227 paa.iobase = ph->ph_space[IO].base;
228 paa.iosize = ph->ph_space[IO].size;
229 ph->ph_card = config_found_ia((void*)sc, "pcmciabus", &paa,
230 at91cf_print);
231
232 (*cscf->intr_establish)(sc, CD_IRQ, IPL_BIO, at91cf_intr_carddetect, ph);
233 wait = (*cscf->power_ctl)(sc, POWER_OFF);
234 delay(wait);
235
236 kthread_create(at91cf_create_event_thread, ph);
237 }
238
239 static int
240 at91cf_print(void *arg, const char *pnp)
241 {
242 return (UNCONF);
243 }
244
245 static void
246 at91cf_create_event_thread(void *arg)
247 {
248 struct at91cf_handle *ph = arg;
249 struct at91cf_softc *sc = ph->ph_sc;
250 at91cf_chipset_tag_t cscf = sc->sc_cscf;
251
252 ph->ph_status = (*cscf->card_detect)(sc);
253
254 DPRINTFN(1, ("at91cf_create_event_thread: status=%d\n", ph->ph_status));
255
256 if (ph->ph_status)
257 pcmcia_card_attach(ph->ph_card);
258
259 ph->ph_run = 1;
260 kthread_create1(at91cf_event_thread, ph, &ph->ph_event_thread,
261 "%s", sc->sc_dev.dv_xname);
262 }
263
264 static void
265 at91cf_event_thread(void *arg)
266 {
267 struct at91cf_handle *ph = arg;
268 int status;
269
270 for (;;) {
271 status = ph->ph_status;
272 tsleep(ph, PWAIT, "CSC wait", 0);
273 if (!ph->ph_run)
274 break;
275
276 DPRINTFN(1, ("at91cf_event_thread: old status=%d, new status=%d\n", status, ph->ph_status));
277
278 if (!status && ph->ph_status)
279 pcmcia_card_attach(ph->ph_card);
280 else if (status && !ph->ph_status)
281 pcmcia_card_detach(ph->ph_card, DETACH_FORCE);
282 }
283
284 DPRINTFN(1, ("at91cf_event_thread: run=%d\n",ph->ph_run));
285 ph->ph_event_thread = NULL;
286 kthread_exit(0);
287 }
288
289 void
290 at91cf_shutdown(void *arg)
291 {
292 struct at91cf_handle *ph = arg;
293
294 DPRINTFN(1, ("at91cf_shutdown\n"));
295 ph->ph_run = 0;
296 wakeup(ph);
297 }
298
299 static int
300 at91cf_intr_carddetect(void *arg)
301 {
302 struct at91cf_handle *ph = arg;
303 struct at91cf_softc *sc = ph->ph_sc;
304 at91cf_chipset_tag_t cscf = sc->sc_cscf;
305 int nstatus;
306
307 nstatus = (*cscf->card_detect)(sc);
308
309 DPRINTFN(1, ("at91cf_intr: nstatus=%#x, ostatus=%#x\n", nstatus, ph->ph_status));
310
311 if (nstatus != ph->ph_status) {
312 ph->ph_status = nstatus;
313 wakeup(ph);
314 }
315
316 return 0;
317 }
318
319 static int
320 at91cf_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
321 struct pcmcia_mem_handle *pmh)
322 {
323 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
324 struct at91cf_softc *sc = ph->ph_sc;
325
326 DPRINTFN(1, ("at91cf_mem_alloc: size=%#x\n",(unsigned)size));
327
328 pmh->memt = sc->sc_iot;
329 return 0;
330 }
331
332 static void
333 at91cf_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
334 {
335 DPRINTFN(1, ("at91cf_mem_free\n"));
336 }
337
338 static int
339 at91cf_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
340 bus_size_t size, struct pcmcia_mem_handle *pmh,
341 bus_size_t *offsetp, int *windowp)
342 {
343 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
344 struct at91cf_softc *sc = ph->ph_sc;
345 bus_addr_t pa;
346 int err;
347
348 DPRINTFN(1, ("at91cf_mem_map: kind=%d, addr=%#x, size=%#x\n",kind,(unsigned)addr,(unsigned)size));
349
350 pa = addr;
351 *offsetp = 0;
352 size = round_page(size);
353 pmh->realsize = size;
354 if (kind & PCMCIA_WIDTH_MEM8)
355 ph->ph_width = 8;
356 else
357 ph->ph_width = 16;
358 switch (kind & ~PCMCIA_WIDTH_MEM_MASK) {
359 case PCMCIA_MEM_ATTR:
360 pa += ph->ph_space[ATTRIBUTE].base;
361 break;
362 case PCMCIA_MEM_COMMON:
363 pa += ph->ph_space[COMMON].base;
364 break;
365 default:
366 return -1;
367 }
368
369 DPRINTFN(1, ("at91cf_mem_map: pa=%#x, *offsetp=%#x, size=%#x\n",(unsigned)pa,(unsigned)addr,(unsigned)size));
370
371 if (!(err = bus_space_map(sc->sc_iot, pa, size, 0, &pmh->memh)))
372 *windowp = (int)pmh->memh;
373 return err;
374 }
375
376 static void
377 at91cf_mem_unmap(pcmcia_chipset_handle_t pch, int window)
378 {
379 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
380 struct at91cf_softc *sc = ph->ph_sc;
381
382 DPRINTFN(1, ("at91cf_mem_unmap: window=%#x\n",window));
383
384 bus_space_unmap(sc->sc_iot, (bus_addr_t)window, 0x1000);
385 }
386
387 static int
388 at91cf_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
389 bus_size_t align, struct pcmcia_io_handle *pih)
390 {
391 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
392 struct at91cf_softc *sc = ph->ph_sc;
393 bus_addr_t pa;
394
395 DPRINTFN(1, ("at91cf_io_alloc: start=%#x, size=%#x, align=%#x\n",(unsigned)start,(unsigned)size,(unsigned)align));
396
397 pih->iot = sc->sc_iot;
398 pih->addr = start;
399 pih->size = size;
400 pa = pih->addr + ph->ph_space[IO].base;
401 return bus_space_map(sc->sc_iot, pa, size, 0, &pih->ioh);
402 }
403
404 static void
405 at91cf_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
406 {
407 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
408 struct at91cf_softc *sc = ph->ph_sc;
409
410 DPRINTFN(1, ("at91cf_io_free\n"));
411
412 bus_space_unmap(sc->sc_iot, pih->ioh, pih->size);
413 }
414
415 static int
416 at91cf_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
417 bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
418 {
419 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
420
421 DPRINTFN(1, ("at91cf_io_map: offset=%#x, size=%#x, width=%d",(unsigned)offset,(unsigned)size,width));
422
423 switch (width) {
424 case PCMCIA_WIDTH_IO8:
425 DPRINTFN(1, ("(8bit)\n"));
426 ph->ph_width = 8;
427 break;
428 case PCMCIA_WIDTH_IO16:
429 case PCMCIA_WIDTH_AUTO: /* I don't understand how I check it */
430 DPRINTFN(1, ("(16bit)\n"));
431 ph->ph_width = 16;
432 break;
433 default:
434 DPRINTFN(1, ("(unknown)\n"));
435 return -1;
436 }
437 *windowp = 0; /* unused */
438 return 0;
439 }
440
441 static void
442 at91cf_io_unmap(pcmcia_chipset_handle_t pch, int window)
443 {
444 DPRINTFN(1, ("at91cf_io_unmap: window=%#x\n",window));
445 }
446
447 static void *
448 at91cf_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
449 int ipl, int (*ih_func)(void *), void *ih_arg)
450 {
451 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
452 struct at91cf_softc *sc = ph->ph_sc;
453 at91cf_chipset_tag_t cscf = sc->sc_cscf;
454
455 DPRINTFN(1, ("at91cf_intr_establish\n"));
456
457 if (ph->ph_ih_func)
458 return 0;
459
460 ph->ph_ih_func = ih_func;
461 ph->ph_ih_arg = ih_arg;
462
463 return (*cscf->intr_establish)(sc, CF_IRQ, ipl, at91cf_intr_socket, ph);
464 // return (*cscf->intr_establish)(sc, CF_IRQ, ipl, ih_func, ih_arg);
465 }
466
467 static void
468 at91cf_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
469 {
470 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
471 struct at91cf_softc *sc = ph->ph_sc;
472 at91cf_chipset_tag_t cscf = sc->sc_cscf;
473
474 DPRINTFN(1, ("at91cf_intr_disestablish\n"));
475
476 ph->ph_ih_func = NULL;
477 ph->ph_ih_arg = NULL;
478
479 (*cscf->intr_disestablish)(sc, CF_IRQ, ih);
480 }
481
482 static int
483 at91cf_intr_socket(void *arg)
484 {
485 struct at91cf_handle *ph = arg;
486 struct at91cf_softc *sc = ph->ph_sc;
487 at91cf_chipset_tag_t cscf = sc->sc_cscf;
488 int err = 0, irq;
489
490 if (ph->ph_ih_func) {
491 irq = (*cscf->irq_line)(sc);
492 if (ph->ph_type == PCMCIA_IFTYPE_IO)
493 irq = !irq;
494 if (irq)
495 err = (*ph->ph_ih_func)(ph->ph_ih_arg);
496 else
497 DPRINTFN(2,("%s: other edge ignored\n", __FUNCTION__));
498 }
499
500 return err;
501 }
502
503 static void
504 at91cf_socket_enable(pcmcia_chipset_handle_t pch)
505 {
506 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
507 struct at91cf_softc *sc = ph->ph_sc;
508 at91cf_chipset_tag_t cscf = sc->sc_cscf;
509 int wait;
510
511 DPRINTFN(1, ("at91cf_socket_enable\n"));
512
513 wait = (cscf->power_ctl)(sc, POWER_ON);
514 delay(wait);
515 }
516
517 static void
518 at91cf_socket_disable(pcmcia_chipset_handle_t pch)
519 {
520 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
521 struct at91cf_softc *sc = ph->ph_sc;
522 at91cf_chipset_tag_t cscf = sc->sc_cscf;
523 int wait;
524
525 DPRINTFN(1, ("at91cf_socket_disable\n"));
526
527 wait = (cscf->power_ctl)(sc, POWER_OFF);
528 delay(wait);
529 }
530
531 static void
532 at91cf_socket_settype(pcmcia_chipset_handle_t pch, int type)
533 {
534 struct at91cf_handle *ph = (struct at91cf_handle *)pch;
535
536 DPRINTFN(1, ("at91cf_socket_settype: type=%d",type));
537
538 ph->ph_type = type;
539
540 switch (type) {
541 case PCMCIA_IFTYPE_MEMORY:
542 DPRINTFN(1, ("(Memory)\n"));
543 break;
544 case PCMCIA_IFTYPE_IO:
545 DPRINTFN(1, ("(I/O)\n"));
546 break;
547 default:
548 DPRINTFN(1, ("(unknown)\n"));
549 return;
550 }
551 }
552
553 #if 0
554 static int
555 at91cf_get_voltage(struct at91cf_handle *ph)
556 {
557 struct at91cf_softc *sc = ph->ph_sc;
558 at91cf_chipset_tag_t cscf = sc->sc_cscf;
559 int cap, vcc = 0;
560
561 cap = (cscf->power_capability)(sc);
562 if (eppio_read(sc->sc_pio, ph->ph_port, ph->ph_vs[0])) {
563 if (cap | VCC_5V)
564 vcc = 5;
565 else
566 printf("%s: unsupported Vcc 5 Volts",
567 sc->sc_dev.dv_xname);
568 } else {
569 if (cap | VCC_3V)
570 vcc = 3;
571 else
572 printf("%s: unsupported Vcc 3.3 Volts",
573 sc->sc_dev.dv_xname);
574 }
575 DPRINTFN(1, ("at91cf_get_voltage: vs1=%d, vs2=%d (%dV)\n",eppio_read_bit(sc->sc_pio, ph->ph_port, ph->ph_vs[0]),eppio_read_bit(sc->sc_pio, ph->ph_port, ph->ph_vs[1]),vcc));
576 return vcc;
577 }
578
579 #endif
580